Merge 'next' into 'master'.
[sliver-openvswitch.git] / datapath / tunnel.c
1 /*
2  * Copyright (c) 2010, 2011 Nicira Networks.
3  * Distributed under the terms of the GNU GPL version 2.
4  *
5  * Significant portions of this file may be copied from parts of the Linux
6  * kernel, by Linus Torvalds and others.
7  */
8
9 #include <linux/if_arp.h>
10 #include <linux/if_ether.h>
11 #include <linux/ip.h>
12 #include <linux/if_vlan.h>
13 #include <linux/in.h>
14 #include <linux/in_route.h>
15 #include <linux/jhash.h>
16 #include <linux/kernel.h>
17 #include <linux/version.h>
18 #include <linux/workqueue.h>
19
20 #include <net/dsfield.h>
21 #include <net/dst.h>
22 #include <net/icmp.h>
23 #include <net/inet_ecn.h>
24 #include <net/ip.h>
25 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
26 #include <net/ipv6.h>
27 #endif
28 #include <net/route.h>
29 #include <net/xfrm.h>
30
31 #include "actions.h"
32 #include "checksum.h"
33 #include "datapath.h"
34 #include "table.h"
35 #include "tunnel.h"
36 #include "vlan.h"
37 #include "vport.h"
38 #include "vport-generic.h"
39 #include "vport-internal_dev.h"
40
41 #ifdef NEED_CACHE_TIMEOUT
42 /*
43  * On kernels where we can't quickly detect changes in the rest of the system
44  * we use an expiration time to invalidate the cache.  A shorter expiration
45  * reduces the length of time that we may potentially blackhole packets while
46  * a longer time increases performance by reducing the frequency that the
47  * cache needs to be rebuilt.  A variety of factors may cause the cache to be
48  * invalidated before the expiration time but this is the maximum.  The time
49  * is expressed in jiffies.
50  */
51 #define MAX_CACHE_EXP HZ
52 #endif
53
54 /*
55  * Interval to check for and remove caches that are no longer valid.  Caches
56  * are checked for validity before they are used for packet encapsulation and
57  * old caches are removed at that time.  However, if no packets are sent through
58  * the tunnel then the cache will never be destroyed.  Since it holds
59  * references to a number of system objects, the cache will continue to use
60  * system resources by not allowing those objects to be destroyed.  The cache
61  * cleaner is periodically run to free invalid caches.  It does not
62  * significantly affect system performance.  A lower interval will release
63  * resources faster but will itself consume resources by requiring more frequent
64  * checks.  A longer interval may result in messages being printed to the kernel
65  * message buffer about unreleased resources.  The interval is expressed in
66  * jiffies.
67  */
68 #define CACHE_CLEANER_INTERVAL (5 * HZ)
69
70 #define CACHE_DATA_ALIGN 16
71
72 static struct tbl __rcu *port_table __read_mostly;
73
74 static void cache_cleaner(struct work_struct *work);
75 static DECLARE_DELAYED_WORK(cache_cleaner_wq, cache_cleaner);
76
77 /*
78  * These are just used as an optimization: they don't require any kind of
79  * synchronization because we could have just as easily read the value before
80  * the port change happened.
81  */
82 static unsigned int key_local_remote_ports __read_mostly;
83 static unsigned int key_remote_ports __read_mostly;
84 static unsigned int local_remote_ports __read_mostly;
85 static unsigned int remote_ports __read_mostly;
86
87 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
88 #define rt_dst(rt) (rt->dst)
89 #else
90 #define rt_dst(rt) (rt->u.dst)
91 #endif
92
93 static inline struct vport *tnl_vport_to_vport(const struct tnl_vport *tnl_vport)
94 {
95         return vport_from_priv(tnl_vport);
96 }
97
98 static inline struct tnl_vport *tnl_vport_table_cast(const struct tbl_node *node)
99 {
100         return container_of(node, struct tnl_vport, tbl_node);
101 }
102
103 /* This is analogous to rtnl_dereference for the tunnel cache.  It checks that
104  * cache_lock is held, so it is only for update side code.
105  */
106 static inline struct tnl_cache *cache_dereference(struct tnl_vport *tnl_vport)
107 {
108         return rcu_dereference_protected(tnl_vport->cache,
109                                          lockdep_is_held(&tnl_vport->cache_lock));
110 }
111
112 static inline void schedule_cache_cleaner(void)
113 {
114         schedule_delayed_work(&cache_cleaner_wq, CACHE_CLEANER_INTERVAL);
115 }
116
117 static void free_cache(struct tnl_cache *cache)
118 {
119         if (!cache)
120                 return;
121
122         flow_put(cache->flow);
123         ip_rt_put(cache->rt);
124         kfree(cache);
125 }
126
127 static void free_config_rcu(struct rcu_head *rcu)
128 {
129         struct tnl_mutable_config *c = container_of(rcu, struct tnl_mutable_config, rcu);
130         kfree(c);
131 }
132
133 static void free_cache_rcu(struct rcu_head *rcu)
134 {
135         struct tnl_cache *c = container_of(rcu, struct tnl_cache, rcu);
136         free_cache(c);
137 }
138
139 static void assign_config_rcu(struct vport *vport,
140                               struct tnl_mutable_config *new_config)
141 {
142         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
143         struct tnl_mutable_config *old_config;
144
145         old_config = rtnl_dereference(tnl_vport->mutable);
146         rcu_assign_pointer(tnl_vport->mutable, new_config);
147         call_rcu(&old_config->rcu, free_config_rcu);
148 }
149
150 static void assign_cache_rcu(struct vport *vport, struct tnl_cache *new_cache)
151 {
152         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
153         struct tnl_cache *old_cache;
154
155         old_cache = cache_dereference(tnl_vport);
156         rcu_assign_pointer(tnl_vport->cache, new_cache);
157
158         if (old_cache)
159                 call_rcu(&old_cache->rcu, free_cache_rcu);
160 }
161
162 static unsigned int *find_port_pool(const struct tnl_mutable_config *mutable)
163 {
164         if (mutable->flags & TNL_F_IN_KEY_MATCH) {
165                 if (mutable->saddr)
166                         return &local_remote_ports;
167                 else
168                         return &remote_ports;
169         } else {
170                 if (mutable->saddr)
171                         return &key_local_remote_ports;
172                 else
173                         return &key_remote_ports;
174         }
175 }
176
177 struct port_lookup_key {
178         const struct tnl_mutable_config *mutable;
179         __be64 key;
180         u32 tunnel_type;
181         __be32 saddr;
182         __be32 daddr;
183 };
184
185 /*
186  * Modifies 'target' to store the rcu_dereferenced pointer that was used to do
187  * the comparision.
188  */
189 static int port_cmp(const struct tbl_node *node, void *target, int unused)
190 {
191         const struct tnl_vport *tnl_vport = tnl_vport_table_cast(node);
192         struct port_lookup_key *lookup = target;
193
194         lookup->mutable = rcu_dereference_rtnl(tnl_vport->mutable);
195
196         return (lookup->mutable->tunnel_type == lookup->tunnel_type &&
197                 lookup->mutable->daddr == lookup->daddr &&
198                 lookup->mutable->in_key == lookup->key &&
199                 lookup->mutable->saddr == lookup->saddr);
200 }
201
202 static u32 port_hash(struct port_lookup_key *k)
203 {
204         u32 x = jhash_3words((__force u32)k->saddr, (__force u32)k->daddr,
205                              k->tunnel_type, 0);
206         return jhash_2words((__force u64)k->key >> 32, (__force u32)k->key, x);
207 }
208
209 static u32 mutable_hash(const struct tnl_mutable_config *mutable)
210 {
211         struct port_lookup_key lookup;
212
213         lookup.saddr = mutable->saddr;
214         lookup.daddr = mutable->daddr;
215         lookup.key = mutable->in_key;
216         lookup.tunnel_type = mutable->tunnel_type;
217
218         return port_hash(&lookup);
219 }
220
221 static void check_table_empty(void)
222 {
223         struct tbl *old_table = rtnl_dereference(port_table);
224
225         if (tbl_count(old_table) == 0) {
226                 cancel_delayed_work_sync(&cache_cleaner_wq);
227                 rcu_assign_pointer(port_table, NULL);
228                 tbl_deferred_destroy(old_table, NULL);
229         }
230 }
231
232 static int add_port(struct vport *vport)
233 {
234         struct tbl *cur_table = rtnl_dereference(port_table);
235         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
236         int err;
237
238         if (!port_table) {
239                 struct tbl *new_table;
240
241                 new_table = tbl_create(TBL_MIN_BUCKETS);
242                 if (!new_table)
243                         return -ENOMEM;
244
245                 rcu_assign_pointer(port_table, new_table);
246                 schedule_cache_cleaner();
247
248         } else if (tbl_count(cur_table) > tbl_n_buckets(cur_table)) {
249                 struct tbl *new_table;
250
251                 new_table = tbl_expand(cur_table);
252                 if (IS_ERR(new_table))
253                         return PTR_ERR(new_table);
254
255                 rcu_assign_pointer(port_table, new_table);
256                 tbl_deferred_destroy(cur_table, NULL);
257         }
258
259         err = tbl_insert(rtnl_dereference(port_table), &tnl_vport->tbl_node,
260                          mutable_hash(rtnl_dereference(tnl_vport->mutable)));
261         if (err) {
262                 check_table_empty();
263                 return err;
264         }
265
266         (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))++;
267
268         return 0;
269 }
270
271 static int move_port(struct vport *vport, struct tnl_mutable_config *new_mutable)
272 {
273         int err;
274         struct tbl *cur_table = rtnl_dereference(port_table);
275         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
276         u32 hash;
277
278         hash = mutable_hash(new_mutable);
279         if (hash == tnl_vport->tbl_node.hash)
280                 goto table_updated;
281
282         /*
283          * Ideally we should make this move atomic to avoid having gaps in
284          * finding tunnels or the possibility of failure.  However, if we do
285          * find a tunnel it will always be consistent.
286          */
287         err = tbl_remove(cur_table, &tnl_vport->tbl_node);
288         if (err)
289                 return err;
290
291         err = tbl_insert(cur_table, &tnl_vport->tbl_node, hash);
292         if (err) {
293                 (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
294                 check_table_empty();
295                 return err;
296         }
297
298 table_updated:
299         (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
300         assign_config_rcu(vport, new_mutable);
301         (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))++;
302
303         return 0;
304 }
305
306 static int del_port(struct vport *vport)
307 {
308         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
309         int err;
310
311         err = tbl_remove(rtnl_dereference(port_table), &tnl_vport->tbl_node);
312         if (err)
313                 return err;
314
315         check_table_empty();
316         (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
317
318         return 0;
319 }
320
321 struct vport *tnl_find_port(__be32 saddr, __be32 daddr, __be64 key,
322                             int tunnel_type,
323                             const struct tnl_mutable_config **mutable)
324 {
325         struct port_lookup_key lookup;
326         struct tbl *table = rcu_dereference_rtnl(port_table);
327         struct tbl_node *tbl_node;
328
329         if (unlikely(!table))
330                 return NULL;
331
332         lookup.saddr = saddr;
333         lookup.daddr = daddr;
334
335         if (tunnel_type & TNL_T_KEY_EXACT) {
336                 lookup.key = key;
337                 lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_MATCH;
338
339                 if (key_local_remote_ports) {
340                         tbl_node = tbl_lookup(table, &lookup, sizeof(lookup),
341                                               port_hash(&lookup), port_cmp);
342                         if (tbl_node)
343                                 goto found;
344                 }
345
346                 if (key_remote_ports) {
347                         lookup.saddr = 0;
348
349                         tbl_node = tbl_lookup(table, &lookup, sizeof(lookup),
350                                               port_hash(&lookup), port_cmp);
351                         if (tbl_node)
352                                 goto found;
353
354                         lookup.saddr = saddr;
355                 }
356         }
357
358         if (tunnel_type & TNL_T_KEY_MATCH) {
359                 lookup.key = 0;
360                 lookup.tunnel_type = tunnel_type & ~TNL_T_KEY_EXACT;
361
362                 if (local_remote_ports) {
363                         tbl_node = tbl_lookup(table, &lookup, sizeof(lookup),
364                                               port_hash(&lookup), port_cmp);
365                         if (tbl_node)
366                                 goto found;
367                 }
368
369                 if (remote_ports) {
370                         lookup.saddr = 0;
371
372                         tbl_node = tbl_lookup(table, &lookup, sizeof(lookup),
373                                               port_hash(&lookup), port_cmp);
374                         if (tbl_node)
375                                 goto found;
376                 }
377         }
378
379         return NULL;
380
381 found:
382         *mutable = lookup.mutable;
383         return tnl_vport_to_vport(tnl_vport_table_cast(tbl_node));
384 }
385
386 static void ecn_decapsulate(struct sk_buff *skb, u8 tos)
387 {
388         if (unlikely(INET_ECN_is_ce(tos))) {
389                 __be16 protocol = skb->protocol;
390
391                 skb_set_network_header(skb, ETH_HLEN);
392
393                 if (protocol == htons(ETH_P_8021Q)) {
394                         if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
395                                 return;
396
397                         protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
398                         skb_set_network_header(skb, VLAN_ETH_HLEN);
399                 }
400
401                 if (protocol == htons(ETH_P_IP)) {
402                         if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
403                             + sizeof(struct iphdr))))
404                                 return;
405
406                         IP_ECN_set_ce(ip_hdr(skb));
407                 }
408 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
409                 else if (protocol == htons(ETH_P_IPV6)) {
410                         if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
411                             + sizeof(struct ipv6hdr))))
412                                 return;
413
414                         IP6_ECN_set_ce(ipv6_hdr(skb));
415                 }
416 #endif
417         }
418 }
419
420 /**
421  *      tnl_rcv - ingress point for generic tunnel code
422  *
423  * @vport: port this packet was received on
424  * @skb: received packet
425  * @tos: ToS from encapsulating IP packet, used to copy ECN bits
426  *
427  * Must be called with rcu_read_lock.
428  *
429  * Packets received by this function are in the following state:
430  * - skb->data points to the inner Ethernet header.
431  * - The inner Ethernet header is in the linear data area.
432  * - skb->csum does not include the inner Ethernet header.
433  * - The layer pointers are undefined.
434  */
435 void tnl_rcv(struct vport *vport, struct sk_buff *skb, u8 tos)
436 {
437         struct ethhdr *eh;
438
439         skb_reset_mac_header(skb);
440         eh = eth_hdr(skb);
441
442         if (likely(ntohs(eh->h_proto) >= 1536))
443                 skb->protocol = eh->h_proto;
444         else
445                 skb->protocol = htons(ETH_P_802_2);
446
447         skb_dst_drop(skb);
448         nf_reset(skb);
449         skb_clear_rxhash(skb);
450         secpath_reset(skb);
451
452         ecn_decapsulate(skb, tos);
453         compute_ip_summed(skb, false);
454         vlan_set_tci(skb, 0);
455
456         vport_receive(vport, skb);
457 }
458
459 static bool check_ipv4_address(__be32 addr)
460 {
461         if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr)
462             || ipv4_is_loopback(addr) || ipv4_is_zeronet(addr))
463                 return false;
464
465         return true;
466 }
467
468 static bool ipv4_should_icmp(struct sk_buff *skb)
469 {
470         struct iphdr *old_iph = ip_hdr(skb);
471
472         /* Don't respond to L2 broadcast. */
473         if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
474                 return false;
475
476         /* Don't respond to L3 broadcast or invalid addresses. */
477         if (!check_ipv4_address(old_iph->daddr) ||
478             !check_ipv4_address(old_iph->saddr))
479                 return false;
480
481         /* Only respond to the first fragment. */
482         if (old_iph->frag_off & htons(IP_OFFSET))
483                 return false;
484
485         /* Don't respond to ICMP error messages. */
486         if (old_iph->protocol == IPPROTO_ICMP) {
487                 u8 icmp_type, *icmp_typep;
488
489                 icmp_typep = skb_header_pointer(skb, (u8 *)old_iph +
490                                                 (old_iph->ihl << 2) +
491                                                 offsetof(struct icmphdr, type) -
492                                                 skb->data, sizeof(icmp_type),
493                                                 &icmp_type);
494
495                 if (!icmp_typep)
496                         return false;
497
498                 if (*icmp_typep > NR_ICMP_TYPES
499                         || (*icmp_typep <= ICMP_PARAMETERPROB
500                                 && *icmp_typep != ICMP_ECHOREPLY
501                                 && *icmp_typep != ICMP_ECHO))
502                         return false;
503         }
504
505         return true;
506 }
507
508 static void ipv4_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
509                             unsigned int mtu, unsigned int payload_length)
510 {
511         struct iphdr *iph, *old_iph = ip_hdr(skb);
512         struct icmphdr *icmph;
513         u8 *payload;
514
515         iph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
516         icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
517         payload = skb_put(nskb, payload_length);
518
519         /* IP */
520         iph->version            =       4;
521         iph->ihl                =       sizeof(struct iphdr) >> 2;
522         iph->tos                =       (old_iph->tos & IPTOS_TOS_MASK) |
523                                         IPTOS_PREC_INTERNETCONTROL;
524         iph->tot_len            =       htons(sizeof(struct iphdr)
525                                               + sizeof(struct icmphdr)
526                                               + payload_length);
527         get_random_bytes(&iph->id, sizeof(iph->id));
528         iph->frag_off           =       0;
529         iph->ttl                =       IPDEFTTL;
530         iph->protocol           =       IPPROTO_ICMP;
531         iph->daddr              =       old_iph->saddr;
532         iph->saddr              =       old_iph->daddr;
533
534         ip_send_check(iph);
535
536         /* ICMP */
537         icmph->type             =       ICMP_DEST_UNREACH;
538         icmph->code             =       ICMP_FRAG_NEEDED;
539         icmph->un.gateway       =       htonl(mtu);
540         icmph->checksum         =       0;
541
542         nskb->csum = csum_partial((u8 *)icmph, sizeof(struct icmphdr), 0);
543         nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_iph - skb->data,
544                                             payload, payload_length,
545                                             nskb->csum);
546         icmph->checksum = csum_fold(nskb->csum);
547 }
548
549 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
550 static bool ipv6_should_icmp(struct sk_buff *skb)
551 {
552         struct ipv6hdr *old_ipv6h = ipv6_hdr(skb);
553         int addr_type;
554         int payload_off = (u8 *)(old_ipv6h + 1) - skb->data;
555         u8 nexthdr = ipv6_hdr(skb)->nexthdr;
556
557         /* Check source address is valid. */
558         addr_type = ipv6_addr_type(&old_ipv6h->saddr);
559         if (addr_type & IPV6_ADDR_MULTICAST || addr_type == IPV6_ADDR_ANY)
560                 return false;
561
562         /* Don't reply to unspecified addresses. */
563         if (ipv6_addr_type(&old_ipv6h->daddr) == IPV6_ADDR_ANY)
564                 return false;
565
566         /* Don't respond to ICMP error messages. */
567         payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr);
568         if (payload_off < 0)
569                 return false;
570
571         if (nexthdr == NEXTHDR_ICMP) {
572                 u8 icmp_type, *icmp_typep;
573
574                 icmp_typep = skb_header_pointer(skb, payload_off +
575                                                 offsetof(struct icmp6hdr,
576                                                         icmp6_type),
577                                                 sizeof(icmp_type), &icmp_type);
578
579                 if (!icmp_typep || !(*icmp_typep & ICMPV6_INFOMSG_MASK))
580                         return false;
581         }
582
583         return true;
584 }
585
586 static void ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
587                             unsigned int mtu, unsigned int payload_length)
588 {
589         struct ipv6hdr *ipv6h, *old_ipv6h = ipv6_hdr(skb);
590         struct icmp6hdr *icmp6h;
591         u8 *payload;
592
593         ipv6h = (struct ipv6hdr *)skb_put(nskb, sizeof(struct ipv6hdr));
594         icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
595         payload = skb_put(nskb, payload_length);
596
597         /* IPv6 */
598         ipv6h->version          =       6;
599         ipv6h->priority         =       0;
600         memset(&ipv6h->flow_lbl, 0, sizeof(ipv6h->flow_lbl));
601         ipv6h->payload_len      =       htons(sizeof(struct icmp6hdr)
602                                               + payload_length);
603         ipv6h->nexthdr          =       NEXTHDR_ICMP;
604         ipv6h->hop_limit        =       IPV6_DEFAULT_HOPLIMIT;
605         ipv6_addr_copy(&ipv6h->daddr, &old_ipv6h->saddr);
606         ipv6_addr_copy(&ipv6h->saddr, &old_ipv6h->daddr);
607
608         /* ICMPv6 */
609         icmp6h->icmp6_type      =       ICMPV6_PKT_TOOBIG;
610         icmp6h->icmp6_code      =       0;
611         icmp6h->icmp6_cksum     =       0;
612         icmp6h->icmp6_mtu       =       htonl(mtu);
613
614         nskb->csum = csum_partial((u8 *)icmp6h, sizeof(struct icmp6hdr), 0);
615         nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_ipv6h - skb->data,
616                                             payload, payload_length,
617                                             nskb->csum);
618         icmp6h->icmp6_cksum = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
619                                                 sizeof(struct icmp6hdr)
620                                                 + payload_length,
621                                                 ipv6h->nexthdr, nskb->csum);
622 }
623 #endif /* IPv6 */
624
625 bool tnl_frag_needed(struct vport *vport, const struct tnl_mutable_config *mutable,
626                      struct sk_buff *skb, unsigned int mtu, __be64 flow_key)
627 {
628         unsigned int eth_hdr_len = ETH_HLEN;
629         unsigned int total_length = 0, header_length = 0, payload_length;
630         struct ethhdr *eh, *old_eh = eth_hdr(skb);
631         struct sk_buff *nskb;
632
633         /* Sanity check */
634         if (skb->protocol == htons(ETH_P_IP)) {
635                 if (mtu < IP_MIN_MTU)
636                         return false;
637
638                 if (!ipv4_should_icmp(skb))
639                         return true;
640         }
641 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
642         else if (skb->protocol == htons(ETH_P_IPV6)) {
643                 if (mtu < IPV6_MIN_MTU)
644                         return false;
645
646                 /*
647                  * In theory we should do PMTUD on IPv6 multicast messages but
648                  * we don't have an address to send from so just fragment.
649                  */
650                 if (ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST)
651                         return false;
652
653                 if (!ipv6_should_icmp(skb))
654                         return true;
655         }
656 #endif
657         else
658                 return false;
659
660         /* Allocate */
661         if (old_eh->h_proto == htons(ETH_P_8021Q))
662                 eth_hdr_len = VLAN_ETH_HLEN;
663
664         payload_length = skb->len - eth_hdr_len;
665         if (skb->protocol == htons(ETH_P_IP)) {
666                 header_length = sizeof(struct iphdr) + sizeof(struct icmphdr);
667                 total_length = min_t(unsigned int, header_length +
668                                                    payload_length, 576);
669         }
670 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
671         else {
672                 header_length = sizeof(struct ipv6hdr) +
673                                 sizeof(struct icmp6hdr);
674                 total_length = min_t(unsigned int, header_length +
675                                                   payload_length, IPV6_MIN_MTU);
676         }
677 #endif
678
679         payload_length = total_length - header_length;
680
681         nskb = dev_alloc_skb(NET_IP_ALIGN + eth_hdr_len + header_length +
682                              payload_length);
683         if (!nskb)
684                 return false;
685
686         skb_reserve(nskb, NET_IP_ALIGN);
687
688         /* Ethernet / VLAN */
689         eh = (struct ethhdr *)skb_put(nskb, eth_hdr_len);
690         memcpy(eh->h_dest, old_eh->h_source, ETH_ALEN);
691         memcpy(eh->h_source, mutable->eth_addr, ETH_ALEN);
692         nskb->protocol = eh->h_proto = old_eh->h_proto;
693         if (old_eh->h_proto == htons(ETH_P_8021Q)) {
694                 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)eh;
695
696                 vh->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
697                 vh->h_vlan_encapsulated_proto = skb->protocol;
698         } else
699                 vlan_set_tci(nskb, vlan_get_tci(skb));
700         skb_reset_mac_header(nskb);
701
702         /* Protocol */
703         if (skb->protocol == htons(ETH_P_IP))
704                 ipv4_build_icmp(skb, nskb, mtu, payload_length);
705 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
706         else
707                 ipv6_build_icmp(skb, nskb, mtu, payload_length);
708 #endif
709
710         /*
711          * Assume that flow based keys are symmetric with respect to input
712          * and output and use the key that we were going to put on the
713          * outgoing packet for the fake received packet.  If the keys are
714          * not symmetric then PMTUD needs to be disabled since we won't have
715          * any way of synthesizing packets.
716          */
717         if ((mutable->flags & (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION)) ==
718             (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION))
719                 OVS_CB(nskb)->tun_id = flow_key;
720
721         compute_ip_summed(nskb, false);
722         vport_receive(vport, nskb);
723
724         return true;
725 }
726
727 static bool check_mtu(struct sk_buff *skb,
728                       struct vport *vport,
729                       const struct tnl_mutable_config *mutable,
730                       const struct rtable *rt, __be16 *frag_offp)
731 {
732         bool df_inherit = mutable->flags & TNL_F_DF_INHERIT;
733         bool pmtud = mutable->flags & TNL_F_PMTUD;
734         __be16 frag_off = mutable->flags & TNL_F_DF_DEFAULT ? htons(IP_DF) : 0;
735         int mtu = 0;
736         unsigned int packet_length = skb->len - ETH_HLEN;
737
738         /* Allow for one level of tagging in the packet length. */
739         if (!vlan_tx_tag_present(skb) &&
740             eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
741                 packet_length -= VLAN_HLEN;
742
743         if (pmtud) {
744                 int vlan_header = 0;
745
746                 /* The tag needs to go in packet regardless of where it
747                  * currently is, so subtract it from the MTU.
748                  */
749                 if (vlan_tx_tag_present(skb) ||
750                     eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
751                         vlan_header = VLAN_HLEN;
752
753                 mtu = dst_mtu(&rt_dst(rt))
754                         - ETH_HLEN
755                         - mutable->tunnel_hlen
756                         - vlan_header;
757         }
758
759         if (skb->protocol == htons(ETH_P_IP)) {
760                 struct iphdr *iph = ip_hdr(skb);
761
762                 if (df_inherit)
763                         frag_off = iph->frag_off & htons(IP_DF);
764
765                 if (pmtud && iph->frag_off & htons(IP_DF)) {
766                         mtu = max(mtu, IP_MIN_MTU);
767
768                         if (packet_length > mtu &&
769                             tnl_frag_needed(vport, mutable, skb, mtu,
770                                             OVS_CB(skb)->tun_id))
771                                 return false;
772                 }
773         }
774 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
775         else if (skb->protocol == htons(ETH_P_IPV6)) {
776                 /* IPv6 requires end hosts to do fragmentation
777                  * if the packet is above the minimum MTU.
778                  */
779                 if (df_inherit && packet_length > IPV6_MIN_MTU)
780                         frag_off = htons(IP_DF);
781
782                 if (pmtud) {
783                         mtu = max(mtu, IPV6_MIN_MTU);
784
785                         if (packet_length > mtu &&
786                             tnl_frag_needed(vport, mutable, skb, mtu,
787                                             OVS_CB(skb)->tun_id))
788                                 return false;
789                 }
790         }
791 #endif
792
793         *frag_offp = frag_off;
794         return true;
795 }
796
797 static void create_tunnel_header(const struct vport *vport,
798                                  const struct tnl_mutable_config *mutable,
799                                  const struct rtable *rt, void *header)
800 {
801         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
802         struct iphdr *iph = header;
803
804         iph->version    = 4;
805         iph->ihl        = sizeof(struct iphdr) >> 2;
806         iph->frag_off   = htons(IP_DF);
807         iph->protocol   = tnl_vport->tnl_ops->ipproto;
808         iph->tos        = mutable->tos;
809         iph->daddr      = rt->rt_dst;
810         iph->saddr      = rt->rt_src;
811         iph->ttl        = mutable->ttl;
812         if (!iph->ttl)
813                 iph->ttl = ip4_dst_hoplimit(&rt_dst(rt));
814
815         tnl_vport->tnl_ops->build_header(vport, mutable, iph + 1);
816 }
817
818 static inline void *get_cached_header(const struct tnl_cache *cache)
819 {
820         return (void *)cache + ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN);
821 }
822
823 static inline bool check_cache_valid(const struct tnl_cache *cache,
824                                      const struct tnl_mutable_config *mutable)
825 {
826         return cache &&
827 #ifdef NEED_CACHE_TIMEOUT
828                 time_before(jiffies, cache->expiration) &&
829 #endif
830 #ifdef HAVE_RT_GENID
831                 atomic_read(&init_net.ipv4.rt_genid) == cache->rt->rt_genid &&
832 #endif
833 #ifdef HAVE_HH_SEQ
834                 rt_dst(cache->rt).hh->hh_lock.sequence == cache->hh_seq &&
835 #endif
836                 mutable->seq == cache->mutable_seq &&
837                 (!is_internal_dev(rt_dst(cache->rt).dev) ||
838                 (cache->flow && !cache->flow->dead));
839 }
840
841 static int cache_cleaner_cb(struct tbl_node *tbl_node, void *aux)
842 {
843         struct tnl_vport *tnl_vport = tnl_vport_table_cast(tbl_node);
844         const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
845         const struct tnl_cache *cache = rcu_dereference(tnl_vport->cache);
846
847         if (cache && !check_cache_valid(cache, mutable) &&
848             spin_trylock_bh(&tnl_vport->cache_lock)) {
849                 assign_cache_rcu(tnl_vport_to_vport(tnl_vport), NULL);
850                 spin_unlock_bh(&tnl_vport->cache_lock);
851         }
852
853         return 0;
854 }
855
856 static void cache_cleaner(struct work_struct *work)
857 {
858         schedule_cache_cleaner();
859
860         rcu_read_lock();
861         tbl_foreach(rcu_dereference(port_table), cache_cleaner_cb, NULL);
862         rcu_read_unlock();
863 }
864
865 static inline void create_eth_hdr(struct tnl_cache *cache,
866                                   const struct rtable *rt)
867 {
868         void *cache_data = get_cached_header(cache);
869         int hh_len = rt_dst(rt).hh->hh_len;
870         int hh_off = HH_DATA_ALIGN(rt_dst(rt).hh->hh_len) - hh_len;
871
872 #ifdef HAVE_HH_SEQ
873         unsigned hh_seq;
874
875         do {
876                 hh_seq = read_seqbegin(&rt_dst(rt).hh->hh_lock);
877                 memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
878         } while (read_seqretry(&rt_dst(rt).hh->hh_lock, hh_seq));
879
880         cache->hh_seq = hh_seq;
881 #else
882         read_lock_bh(&rt_dst(rt).hh->hh_lock);
883         memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
884         read_unlock_bh(&rt_dst(rt).hh->hh_lock);
885 #endif
886 }
887
888 static struct tnl_cache *build_cache(struct vport *vport,
889                                      const struct tnl_mutable_config *mutable,
890                                      struct rtable *rt)
891 {
892         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
893         struct tnl_cache *cache;
894         void *cache_data;
895         int cache_len;
896
897         if (!(mutable->flags & TNL_F_HDR_CACHE))
898                 return NULL;
899
900         /*
901          * If there is no entry in the ARP cache or if this device does not
902          * support hard header caching just fall back to the IP stack.
903          */
904         if (!rt_dst(rt).hh)
905                 return NULL;
906
907         /*
908          * If lock is contended fall back to directly building the header.
909          * We're not going to help performance by sitting here spinning.
910          */
911         if (!spin_trylock_bh(&tnl_vport->cache_lock))
912                 return NULL;
913
914         cache = cache_dereference(tnl_vport);
915         if (check_cache_valid(cache, mutable))
916                 goto unlock;
917         else
918                 cache = NULL;
919
920         cache_len = rt_dst(rt).hh->hh_len + mutable->tunnel_hlen;
921
922         cache = kzalloc(ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN) +
923                         cache_len, GFP_ATOMIC);
924         if (!cache)
925                 goto unlock;
926
927         cache->len = cache_len;
928
929         create_eth_hdr(cache, rt);
930         cache_data = get_cached_header(cache) + rt_dst(rt).hh->hh_len;
931
932         create_tunnel_header(vport, mutable, rt, cache_data);
933
934         cache->mutable_seq = mutable->seq;
935         cache->rt = rt;
936 #ifdef NEED_CACHE_TIMEOUT
937         cache->expiration = jiffies + tnl_vport->cache_exp_interval;
938 #endif
939
940         if (is_internal_dev(rt_dst(rt).dev)) {
941                 struct sw_flow_key flow_key;
942                 struct tbl_node *flow_node;
943                 struct vport *dst_vport;
944                 struct sk_buff *skb;
945                 bool is_frag;
946                 int err;
947                 int flow_key_len;
948
949                 dst_vport = internal_dev_get_vport(rt_dst(rt).dev);
950                 if (!dst_vport)
951                         goto done;
952
953                 skb = alloc_skb(cache->len, GFP_ATOMIC);
954                 if (!skb)
955                         goto done;
956
957                 __skb_put(skb, cache->len);
958                 memcpy(skb->data, get_cached_header(cache), cache->len);
959
960                 err = flow_extract(skb, dst_vport->port_no, &flow_key,
961                                    &flow_key_len, &is_frag);
962
963                 kfree_skb(skb);
964                 if (err || is_frag)
965                         goto done;
966
967                 flow_node = tbl_lookup(rcu_dereference(dst_vport->dp->table),
968                                        &flow_key, flow_key_len,
969                                        flow_hash(&flow_key, flow_key_len),
970                                        flow_cmp);
971                 if (flow_node) {
972                         struct sw_flow *flow = flow_cast(flow_node);
973
974                         cache->flow = flow;
975                         flow_hold(flow);
976                 }
977         }
978
979 done:
980         assign_cache_rcu(vport, cache);
981
982 unlock:
983         spin_unlock_bh(&tnl_vport->cache_lock);
984
985         return cache;
986 }
987
988 static struct rtable *find_route(struct vport *vport,
989                                  const struct tnl_mutable_config *mutable,
990                                  u8 tos, struct tnl_cache **cache)
991 {
992         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
993         struct tnl_cache *cur_cache = rcu_dereference(tnl_vport->cache);
994
995         *cache = NULL;
996         tos = RT_TOS(tos);
997
998         if (likely(tos == mutable->tos && check_cache_valid(cur_cache, mutable))) {
999                 *cache = cur_cache;
1000                 return cur_cache->rt;
1001         } else {
1002                 struct rtable *rt;
1003 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
1004                 struct flowi fl = { .nl_u = { .ip4_u =
1005                                               { .daddr = mutable->daddr,
1006                                                 .saddr = mutable->saddr,
1007                                                 .tos = tos } },
1008                                     .proto = tnl_vport->tnl_ops->ipproto };
1009
1010                 if (unlikely(ip_route_output_key(&init_net, &rt, &fl)))
1011                         return NULL;
1012 #else
1013                 struct flowi4 fl = { .daddr = mutable->daddr,
1014                                      .saddr = mutable->saddr,
1015                                      .flowi4_tos = tos,
1016                                      .flowi4_proto = tnl_vport->tnl_ops->ipproto };
1017
1018                 rt = ip_route_output_key(&init_net, &fl);
1019                 if (IS_ERR(rt))
1020                         return NULL;
1021 #endif
1022
1023                 if (likely(tos == mutable->tos))
1024                         *cache = build_cache(vport, mutable, rt);
1025
1026                 return rt;
1027         }
1028 }
1029
1030 static struct sk_buff *check_headroom(struct sk_buff *skb, int headroom)
1031 {
1032         if (skb_headroom(skb) < headroom || skb_header_cloned(skb)) {
1033                 struct sk_buff *nskb = skb_realloc_headroom(skb, headroom + 16);
1034                 if (unlikely(!nskb)) {
1035                         kfree_skb(skb);
1036                         return ERR_PTR(-ENOMEM);
1037                 }
1038
1039                 set_skb_csum_bits(skb, nskb);
1040
1041                 if (skb->sk)
1042                         skb_set_owner_w(nskb, skb->sk);
1043
1044                 kfree_skb(skb);
1045                 return nskb;
1046         }
1047
1048         return skb;
1049 }
1050
1051 static inline bool need_linearize(const struct sk_buff *skb)
1052 {
1053         int i;
1054
1055         if (unlikely(skb_shinfo(skb)->frag_list))
1056                 return true;
1057
1058         /*
1059          * Generally speaking we should linearize if there are paged frags.
1060          * However, if all of the refcounts are 1 we know nobody else can
1061          * change them from underneath us and we can skip the linearization.
1062          */
1063         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1064                 if (unlikely(page_count(skb_shinfo(skb)->frags[i].page) > 1))
1065                         return true;
1066
1067         return false;
1068 }
1069
1070 static struct sk_buff *handle_offloads(struct sk_buff *skb,
1071                                        const struct tnl_mutable_config *mutable,
1072                                        const struct rtable *rt)
1073 {
1074         int min_headroom;
1075         int err;
1076
1077         forward_ip_summed(skb);
1078
1079         err = vswitch_skb_checksum_setup(skb);
1080         if (unlikely(err))
1081                 goto error_free;
1082
1083         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
1084                         + mutable->tunnel_hlen
1085                         + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1086
1087         skb = check_headroom(skb, min_headroom);
1088         if (IS_ERR(skb)) {
1089                 err = PTR_ERR(skb);
1090                 goto error;
1091         }
1092
1093         if (skb_is_gso(skb)) {
1094                 struct sk_buff *nskb;
1095
1096                 nskb = skb_gso_segment(skb, 0);
1097                 kfree_skb(skb);
1098                 if (IS_ERR(nskb)) {
1099                         err = PTR_ERR(nskb);
1100                         goto error;
1101                 }
1102
1103                 skb = nskb;
1104         } else if (skb->ip_summed == CHECKSUM_PARTIAL) {
1105                 /* Pages aren't locked and could change at any time.
1106                  * If this happens after we compute the checksum, the
1107                  * checksum will be wrong.  We linearize now to avoid
1108                  * this problem.
1109                  */
1110                 if (unlikely(need_linearize(skb))) {
1111                         err = __skb_linearize(skb);
1112                         if (unlikely(err))
1113                                 goto error_free;
1114                 }
1115
1116                 err = skb_checksum_help(skb);
1117                 if (unlikely(err))
1118                         goto error_free;
1119         } else if (skb->ip_summed == CHECKSUM_COMPLETE)
1120                 skb->ip_summed = CHECKSUM_NONE;
1121
1122         return skb;
1123
1124 error_free:
1125         kfree_skb(skb);
1126 error:
1127         return ERR_PTR(err);
1128 }
1129
1130 static int send_frags(struct sk_buff *skb,
1131                       const struct tnl_mutable_config *mutable)
1132 {
1133         int sent_len;
1134
1135         sent_len = 0;
1136         while (skb) {
1137                 struct sk_buff *next = skb->next;
1138                 int frag_len = skb->len - mutable->tunnel_hlen;
1139                 int err;
1140
1141                 skb->next = NULL;
1142                 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
1143
1144                 err = ip_local_out(skb);
1145                 skb = next;
1146                 if (unlikely(net_xmit_eval(err)))
1147                         goto free_frags;
1148                 sent_len += frag_len;
1149         }
1150
1151         return sent_len;
1152
1153 free_frags:
1154         /*
1155          * There's no point in continuing to send fragments once one has been
1156          * dropped so just free the rest.  This may help improve the congestion
1157          * that caused the first packet to be dropped.
1158          */
1159         tnl_free_linked_skbs(skb);
1160         return sent_len;
1161 }
1162
1163 int tnl_send(struct vport *vport, struct sk_buff *skb)
1164 {
1165         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1166         const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
1167
1168         enum vport_err_type err = VPORT_E_TX_ERROR;
1169         struct rtable *rt;
1170         struct dst_entry *unattached_dst = NULL;
1171         struct tnl_cache *cache;
1172         int sent_len = 0;
1173         __be16 frag_off = 0;
1174         u8 ttl;
1175         u8 inner_tos;
1176         u8 tos;
1177
1178         /* Validate the protocol headers before we try to use them. */
1179         if (skb->protocol == htons(ETH_P_8021Q) &&
1180             !vlan_tx_tag_present(skb)) {
1181                 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
1182                         goto error_free;
1183
1184                 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1185                 skb_set_network_header(skb, VLAN_ETH_HLEN);
1186         }
1187
1188         if (skb->protocol == htons(ETH_P_IP)) {
1189                 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1190                     + sizeof(struct iphdr))))
1191                         skb->protocol = 0;
1192         }
1193 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1194         else if (skb->protocol == htons(ETH_P_IPV6)) {
1195                 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1196                     + sizeof(struct ipv6hdr))))
1197                         skb->protocol = 0;
1198         }
1199 #endif
1200
1201         /* ToS */
1202         if (skb->protocol == htons(ETH_P_IP))
1203                 inner_tos = ip_hdr(skb)->tos;
1204 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1205         else if (skb->protocol == htons(ETH_P_IPV6))
1206                 inner_tos = ipv6_get_dsfield(ipv6_hdr(skb));
1207 #endif
1208         else
1209                 inner_tos = 0;
1210
1211         if (mutable->flags & TNL_F_TOS_INHERIT)
1212                 tos = inner_tos;
1213         else
1214                 tos = mutable->tos;
1215
1216         tos = INET_ECN_encapsulate(tos, inner_tos);
1217
1218         /* Route lookup */
1219         rt = find_route(vport, mutable, tos, &cache);
1220         if (unlikely(!rt))
1221                 goto error_free;
1222         if (unlikely(!cache))
1223                 unattached_dst = &rt_dst(rt);
1224
1225         /* Reset SKB */
1226         nf_reset(skb);
1227         secpath_reset(skb);
1228         skb_dst_drop(skb);
1229         skb_clear_rxhash(skb);
1230
1231         /* Offloading */
1232         skb = handle_offloads(skb, mutable, rt);
1233         if (IS_ERR(skb))
1234                 goto error;
1235
1236         /* MTU */
1237         if (unlikely(!check_mtu(skb, vport, mutable, rt, &frag_off))) {
1238                 err = VPORT_E_TX_DROPPED;
1239                 goto error_free;
1240         }
1241
1242         /*
1243          * If we are over the MTU, allow the IP stack to handle fragmentation.
1244          * Fragmentation is a slow path anyways.
1245          */
1246         if (unlikely(skb->len + mutable->tunnel_hlen > dst_mtu(&rt_dst(rt)) &&
1247                      cache)) {
1248                 unattached_dst = &rt_dst(rt);
1249                 dst_hold(unattached_dst);
1250                 cache = NULL;
1251         }
1252
1253         /* TTL */
1254         ttl = mutable->ttl;
1255         if (!ttl)
1256                 ttl = ip4_dst_hoplimit(&rt_dst(rt));
1257
1258         if (mutable->flags & TNL_F_TTL_INHERIT) {
1259                 if (skb->protocol == htons(ETH_P_IP))
1260                         ttl = ip_hdr(skb)->ttl;
1261 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1262                 else if (skb->protocol == htons(ETH_P_IPV6))
1263                         ttl = ipv6_hdr(skb)->hop_limit;
1264 #endif
1265         }
1266
1267         while (skb) {
1268                 struct iphdr *iph;
1269                 struct sk_buff *next_skb = skb->next;
1270                 skb->next = NULL;
1271
1272                 if (unlikely(vlan_deaccel_tag(skb)))
1273                         goto next;
1274
1275                 if (likely(cache)) {
1276                         skb_push(skb, cache->len);
1277                         memcpy(skb->data, get_cached_header(cache), cache->len);
1278                         skb_reset_mac_header(skb);
1279                         skb_set_network_header(skb, rt_dst(rt).hh->hh_len);
1280
1281                 } else {
1282                         skb_push(skb, mutable->tunnel_hlen);
1283                         create_tunnel_header(vport, mutable, rt, skb->data);
1284                         skb_reset_network_header(skb);
1285
1286                         if (next_skb)
1287                                 skb_dst_set(skb, dst_clone(unattached_dst));
1288                         else {
1289                                 skb_dst_set(skb, unattached_dst);
1290                                 unattached_dst = NULL;
1291                         }
1292                 }
1293                 skb_set_transport_header(skb, skb_network_offset(skb) + sizeof(struct iphdr));
1294
1295                 iph = ip_hdr(skb);
1296                 iph->tos = tos;
1297                 iph->ttl = ttl;
1298                 iph->frag_off = frag_off;
1299                 ip_select_ident(iph, &rt_dst(rt), NULL);
1300
1301                 skb = tnl_vport->tnl_ops->update_header(vport, mutable, &rt_dst(rt), skb);
1302                 if (unlikely(!skb))
1303                         goto next;
1304
1305                 if (likely(cache)) {
1306                         int orig_len = skb->len - cache->len;
1307                         struct vport *cache_vport = internal_dev_get_vport(rt_dst(rt).dev);
1308
1309                         skb->protocol = htons(ETH_P_IP);
1310                         iph = ip_hdr(skb);
1311                         iph->tot_len = htons(skb->len - skb_network_offset(skb));
1312                         ip_send_check(iph);
1313
1314                         if (cache_vport) {
1315                                 OVS_CB(skb)->flow = cache->flow;
1316                                 compute_ip_summed(skb, true);
1317                                 vport_receive(cache_vport, skb);
1318                                 sent_len += orig_len;
1319                         } else {
1320                                 int xmit_err;
1321
1322                                 skb->dev = rt_dst(rt).dev;
1323                                 xmit_err = dev_queue_xmit(skb);
1324
1325                                 if (likely(net_xmit_eval(xmit_err) == 0))
1326                                         sent_len += orig_len;
1327                         }
1328                 } else
1329                         sent_len += send_frags(skb, mutable);
1330
1331 next:
1332                 skb = next_skb;
1333         }
1334
1335         if (unlikely(sent_len == 0))
1336                 vport_record_error(vport, VPORT_E_TX_DROPPED);
1337
1338         goto out;
1339
1340 error_free:
1341         tnl_free_linked_skbs(skb);
1342 error:
1343         vport_record_error(vport, err);
1344 out:
1345         dst_release(unattached_dst);
1346         return sent_len;
1347 }
1348
1349 static const struct nla_policy tnl_policy[ODP_TUNNEL_ATTR_MAX + 1] = {
1350         [ODP_TUNNEL_ATTR_FLAGS]    = { .type = NLA_U32 },
1351         [ODP_TUNNEL_ATTR_DST_IPV4] = { .type = NLA_U32 },
1352         [ODP_TUNNEL_ATTR_SRC_IPV4] = { .type = NLA_U32 },
1353         [ODP_TUNNEL_ATTR_OUT_KEY]  = { .type = NLA_U64 },
1354         [ODP_TUNNEL_ATTR_IN_KEY]   = { .type = NLA_U64 },
1355         [ODP_TUNNEL_ATTR_TOS]      = { .type = NLA_U8 },
1356         [ODP_TUNNEL_ATTR_TTL]      = { .type = NLA_U8 },
1357 };
1358
1359 /* Sets ODP_TUNNEL_ATTR_* fields in 'mutable', which must initially be zeroed. */
1360 static int tnl_set_config(struct nlattr *options, const struct tnl_ops *tnl_ops,
1361                           const struct vport *cur_vport,
1362                           struct tnl_mutable_config *mutable)
1363 {
1364         const struct vport *old_vport;
1365         const struct tnl_mutable_config *old_mutable;
1366         struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
1367         int err;
1368
1369         if (!options)
1370                 return -EINVAL;
1371
1372         err = nla_parse_nested(a, ODP_TUNNEL_ATTR_MAX, options, tnl_policy);
1373         if (err)
1374                 return err;
1375
1376         if (!a[ODP_TUNNEL_ATTR_FLAGS] || !a[ODP_TUNNEL_ATTR_DST_IPV4])
1377                 return -EINVAL;
1378
1379         mutable->flags = nla_get_u32(a[ODP_TUNNEL_ATTR_FLAGS]) & TNL_F_PUBLIC;
1380
1381         if (a[ODP_TUNNEL_ATTR_SRC_IPV4])
1382                 mutable->saddr = nla_get_be32(a[ODP_TUNNEL_ATTR_SRC_IPV4]);
1383         mutable->daddr = nla_get_be32(a[ODP_TUNNEL_ATTR_DST_IPV4]);
1384
1385         if (a[ODP_TUNNEL_ATTR_TOS]) {
1386                 mutable->tos = nla_get_u8(a[ODP_TUNNEL_ATTR_TOS]);
1387                 if (mutable->tos != RT_TOS(mutable->tos))
1388                         return -EINVAL;
1389         }
1390
1391         if (a[ODP_TUNNEL_ATTR_TTL])
1392                 mutable->ttl = nla_get_u8(a[ODP_TUNNEL_ATTR_TTL]);
1393
1394         mutable->tunnel_type = tnl_ops->tunnel_type;
1395         if (!a[ODP_TUNNEL_ATTR_IN_KEY]) {
1396                 mutable->tunnel_type |= TNL_T_KEY_MATCH;
1397                 mutable->flags |= TNL_F_IN_KEY_MATCH;
1398         } else {
1399                 mutable->tunnel_type |= TNL_T_KEY_EXACT;
1400                 mutable->in_key = nla_get_be64(a[ODP_TUNNEL_ATTR_IN_KEY]);
1401         }
1402
1403         if (!a[ODP_TUNNEL_ATTR_OUT_KEY])
1404                 mutable->flags |= TNL_F_OUT_KEY_ACTION;
1405         else
1406                 mutable->out_key = nla_get_be64(a[ODP_TUNNEL_ATTR_OUT_KEY]);
1407
1408         mutable->tunnel_hlen = tnl_ops->hdr_len(mutable);
1409         if (mutable->tunnel_hlen < 0)
1410                 return mutable->tunnel_hlen;
1411
1412         mutable->tunnel_hlen += sizeof(struct iphdr);
1413
1414         old_vport = tnl_find_port(mutable->saddr, mutable->daddr,
1415                                   mutable->in_key, mutable->tunnel_type,
1416                                   &old_mutable);
1417
1418         if (old_vport && old_vport != cur_vport)
1419                 return -EEXIST;
1420
1421         return 0;
1422 }
1423
1424 struct vport *tnl_create(const struct vport_parms *parms,
1425                          const struct vport_ops *vport_ops,
1426                          const struct tnl_ops *tnl_ops)
1427 {
1428         struct vport *vport;
1429         struct tnl_vport *tnl_vport;
1430         struct tnl_mutable_config *mutable;
1431         int initial_frag_id;
1432         int err;
1433
1434         vport = vport_alloc(sizeof(struct tnl_vport), vport_ops, parms);
1435         if (IS_ERR(vport)) {
1436                 err = PTR_ERR(vport);
1437                 goto error;
1438         }
1439
1440         tnl_vport = tnl_vport_priv(vport);
1441
1442         strcpy(tnl_vport->name, parms->name);
1443         tnl_vport->tnl_ops = tnl_ops;
1444
1445         mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1446         if (!mutable) {
1447                 err = -ENOMEM;
1448                 goto error_free_vport;
1449         }
1450
1451         vport_gen_rand_ether_addr(mutable->eth_addr);
1452
1453         get_random_bytes(&initial_frag_id, sizeof(int));
1454         atomic_set(&tnl_vport->frag_id, initial_frag_id);
1455
1456         err = tnl_set_config(parms->options, tnl_ops, NULL, mutable);
1457         if (err)
1458                 goto error_free_mutable;
1459
1460         spin_lock_init(&tnl_vport->cache_lock);
1461
1462 #ifdef NEED_CACHE_TIMEOUT
1463         tnl_vport->cache_exp_interval = MAX_CACHE_EXP -
1464                                        (net_random() % (MAX_CACHE_EXP / 2));
1465 #endif
1466
1467         rcu_assign_pointer(tnl_vport->mutable, mutable);
1468
1469         err = add_port(vport);
1470         if (err)
1471                 goto error_free_mutable;
1472
1473         return vport;
1474
1475 error_free_mutable:
1476         kfree(mutable);
1477 error_free_vport:
1478         vport_free(vport);
1479 error:
1480         return ERR_PTR(err);
1481 }
1482
1483 int tnl_set_options(struct vport *vport, struct nlattr *options)
1484 {
1485         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1486         const struct tnl_mutable_config *old_mutable;
1487         struct tnl_mutable_config *mutable;
1488         int err;
1489
1490         mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1491         if (!mutable) {
1492                 err = -ENOMEM;
1493                 goto error;
1494         }
1495
1496         /* Copy fields whose values should be retained. */
1497         old_mutable = rtnl_dereference(tnl_vport->mutable);
1498         mutable->seq = old_mutable->seq + 1;
1499         memcpy(mutable->eth_addr, old_mutable->eth_addr, ETH_ALEN);
1500
1501         /* Parse the others configured by userspace. */
1502         err = tnl_set_config(options, tnl_vport->tnl_ops, vport, mutable);
1503         if (err)
1504                 goto error_free;
1505
1506         err = move_port(vport, mutable);
1507         if (err)
1508                 goto error_free;
1509
1510         return 0;
1511
1512 error_free:
1513         kfree(mutable);
1514 error:
1515         return err;
1516 }
1517
1518 int tnl_get_options(const struct vport *vport, struct sk_buff *skb)
1519 {
1520         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1521         const struct tnl_mutable_config *mutable = rcu_dereference_rtnl(tnl_vport->mutable);
1522
1523         NLA_PUT_U32(skb, ODP_TUNNEL_ATTR_FLAGS, mutable->flags & TNL_F_PUBLIC);
1524         NLA_PUT_BE32(skb, ODP_TUNNEL_ATTR_DST_IPV4, mutable->daddr);
1525
1526         if (!(mutable->flags & TNL_F_IN_KEY_MATCH))
1527                 NLA_PUT_BE64(skb, ODP_TUNNEL_ATTR_IN_KEY, mutable->in_key);
1528         if (!(mutable->flags & TNL_F_OUT_KEY_ACTION))
1529                 NLA_PUT_BE64(skb, ODP_TUNNEL_ATTR_OUT_KEY, mutable->out_key);
1530         if (mutable->saddr)
1531                 NLA_PUT_BE32(skb, ODP_TUNNEL_ATTR_SRC_IPV4, mutable->saddr);
1532         if (mutable->tos)
1533                 NLA_PUT_U8(skb, ODP_TUNNEL_ATTR_TOS, mutable->tos);
1534         if (mutable->ttl)
1535                 NLA_PUT_U8(skb, ODP_TUNNEL_ATTR_TTL, mutable->ttl);
1536
1537         return 0;
1538
1539 nla_put_failure:
1540         return -EMSGSIZE;
1541 }
1542
1543 static void free_port_rcu(struct rcu_head *rcu)
1544 {
1545         struct tnl_vport *tnl_vport = container_of(rcu,
1546                                                    struct tnl_vport, rcu);
1547
1548         free_cache((struct tnl_cache __force *)tnl_vport->cache);
1549         kfree((struct tnl_mutable __force *)tnl_vport->mutable);
1550         vport_free(tnl_vport_to_vport(tnl_vport));
1551 }
1552
1553 int tnl_destroy(struct vport *vport)
1554 {
1555         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1556         const struct tnl_mutable_config *mutable, *old_mutable;
1557
1558         mutable = rtnl_dereference(tnl_vport->mutable);
1559
1560         if (vport == tnl_find_port(mutable->saddr, mutable->daddr,
1561                                    mutable->in_key, mutable->tunnel_type,
1562                                    &old_mutable))
1563                 del_port(vport);
1564
1565         call_rcu(&tnl_vport->rcu, free_port_rcu);
1566
1567         return 0;
1568 }
1569
1570 int tnl_set_addr(struct vport *vport, const unsigned char *addr)
1571 {
1572         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1573         struct tnl_mutable_config *mutable;
1574
1575         mutable = kmemdup(rtnl_dereference(tnl_vport->mutable),
1576                           sizeof(struct tnl_mutable_config), GFP_KERNEL);
1577         if (!mutable)
1578                 return -ENOMEM;
1579
1580         memcpy(mutable->eth_addr, addr, ETH_ALEN);
1581         assign_config_rcu(vport, mutable);
1582
1583         return 0;
1584 }
1585
1586 const char *tnl_get_name(const struct vport *vport)
1587 {
1588         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1589         return tnl_vport->name;
1590 }
1591
1592 const unsigned char *tnl_get_addr(const struct vport *vport)
1593 {
1594         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1595         return rcu_dereference_rtnl(tnl_vport->mutable)->eth_addr;
1596 }
1597
1598 void tnl_free_linked_skbs(struct sk_buff *skb)
1599 {
1600         while (skb) {
1601                 struct sk_buff *next = skb->next;
1602                 kfree_skb(skb);
1603                 skb = next;
1604         }
1605 }