datapath: Further mirror checksum offloading state on old kernels.
[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         vlan_set_tci(skb, 0);
454
455         if (unlikely(compute_ip_summed(skb, false))) {
456                 kfree_skb(skb);
457                 return;
458         }
459
460         vport_receive(vport, skb);
461 }
462
463 static bool check_ipv4_address(__be32 addr)
464 {
465         if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr)
466             || ipv4_is_loopback(addr) || ipv4_is_zeronet(addr))
467                 return false;
468
469         return true;
470 }
471
472 static bool ipv4_should_icmp(struct sk_buff *skb)
473 {
474         struct iphdr *old_iph = ip_hdr(skb);
475
476         /* Don't respond to L2 broadcast. */
477         if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
478                 return false;
479
480         /* Don't respond to L3 broadcast or invalid addresses. */
481         if (!check_ipv4_address(old_iph->daddr) ||
482             !check_ipv4_address(old_iph->saddr))
483                 return false;
484
485         /* Only respond to the first fragment. */
486         if (old_iph->frag_off & htons(IP_OFFSET))
487                 return false;
488
489         /* Don't respond to ICMP error messages. */
490         if (old_iph->protocol == IPPROTO_ICMP) {
491                 u8 icmp_type, *icmp_typep;
492
493                 icmp_typep = skb_header_pointer(skb, (u8 *)old_iph +
494                                                 (old_iph->ihl << 2) +
495                                                 offsetof(struct icmphdr, type) -
496                                                 skb->data, sizeof(icmp_type),
497                                                 &icmp_type);
498
499                 if (!icmp_typep)
500                         return false;
501
502                 if (*icmp_typep > NR_ICMP_TYPES
503                         || (*icmp_typep <= ICMP_PARAMETERPROB
504                                 && *icmp_typep != ICMP_ECHOREPLY
505                                 && *icmp_typep != ICMP_ECHO))
506                         return false;
507         }
508
509         return true;
510 }
511
512 static void ipv4_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
513                             unsigned int mtu, unsigned int payload_length)
514 {
515         struct iphdr *iph, *old_iph = ip_hdr(skb);
516         struct icmphdr *icmph;
517         u8 *payload;
518
519         iph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
520         icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
521         payload = skb_put(nskb, payload_length);
522
523         /* IP */
524         iph->version            =       4;
525         iph->ihl                =       sizeof(struct iphdr) >> 2;
526         iph->tos                =       (old_iph->tos & IPTOS_TOS_MASK) |
527                                         IPTOS_PREC_INTERNETCONTROL;
528         iph->tot_len            =       htons(sizeof(struct iphdr)
529                                               + sizeof(struct icmphdr)
530                                               + payload_length);
531         get_random_bytes(&iph->id, sizeof(iph->id));
532         iph->frag_off           =       0;
533         iph->ttl                =       IPDEFTTL;
534         iph->protocol           =       IPPROTO_ICMP;
535         iph->daddr              =       old_iph->saddr;
536         iph->saddr              =       old_iph->daddr;
537
538         ip_send_check(iph);
539
540         /* ICMP */
541         icmph->type             =       ICMP_DEST_UNREACH;
542         icmph->code             =       ICMP_FRAG_NEEDED;
543         icmph->un.gateway       =       htonl(mtu);
544         icmph->checksum         =       0;
545
546         nskb->csum = csum_partial((u8 *)icmph, sizeof(struct icmphdr), 0);
547         nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_iph - skb->data,
548                                             payload, payload_length,
549                                             nskb->csum);
550         icmph->checksum = csum_fold(nskb->csum);
551 }
552
553 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
554 static bool ipv6_should_icmp(struct sk_buff *skb)
555 {
556         struct ipv6hdr *old_ipv6h = ipv6_hdr(skb);
557         int addr_type;
558         int payload_off = (u8 *)(old_ipv6h + 1) - skb->data;
559         u8 nexthdr = ipv6_hdr(skb)->nexthdr;
560
561         /* Check source address is valid. */
562         addr_type = ipv6_addr_type(&old_ipv6h->saddr);
563         if (addr_type & IPV6_ADDR_MULTICAST || addr_type == IPV6_ADDR_ANY)
564                 return false;
565
566         /* Don't reply to unspecified addresses. */
567         if (ipv6_addr_type(&old_ipv6h->daddr) == IPV6_ADDR_ANY)
568                 return false;
569
570         /* Don't respond to ICMP error messages. */
571         payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr);
572         if (payload_off < 0)
573                 return false;
574
575         if (nexthdr == NEXTHDR_ICMP) {
576                 u8 icmp_type, *icmp_typep;
577
578                 icmp_typep = skb_header_pointer(skb, payload_off +
579                                                 offsetof(struct icmp6hdr,
580                                                         icmp6_type),
581                                                 sizeof(icmp_type), &icmp_type);
582
583                 if (!icmp_typep || !(*icmp_typep & ICMPV6_INFOMSG_MASK))
584                         return false;
585         }
586
587         return true;
588 }
589
590 static void ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
591                             unsigned int mtu, unsigned int payload_length)
592 {
593         struct ipv6hdr *ipv6h, *old_ipv6h = ipv6_hdr(skb);
594         struct icmp6hdr *icmp6h;
595         u8 *payload;
596
597         ipv6h = (struct ipv6hdr *)skb_put(nskb, sizeof(struct ipv6hdr));
598         icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
599         payload = skb_put(nskb, payload_length);
600
601         /* IPv6 */
602         ipv6h->version          =       6;
603         ipv6h->priority         =       0;
604         memset(&ipv6h->flow_lbl, 0, sizeof(ipv6h->flow_lbl));
605         ipv6h->payload_len      =       htons(sizeof(struct icmp6hdr)
606                                               + payload_length);
607         ipv6h->nexthdr          =       NEXTHDR_ICMP;
608         ipv6h->hop_limit        =       IPV6_DEFAULT_HOPLIMIT;
609         ipv6_addr_copy(&ipv6h->daddr, &old_ipv6h->saddr);
610         ipv6_addr_copy(&ipv6h->saddr, &old_ipv6h->daddr);
611
612         /* ICMPv6 */
613         icmp6h->icmp6_type      =       ICMPV6_PKT_TOOBIG;
614         icmp6h->icmp6_code      =       0;
615         icmp6h->icmp6_cksum     =       0;
616         icmp6h->icmp6_mtu       =       htonl(mtu);
617
618         nskb->csum = csum_partial((u8 *)icmp6h, sizeof(struct icmp6hdr), 0);
619         nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_ipv6h - skb->data,
620                                             payload, payload_length,
621                                             nskb->csum);
622         icmp6h->icmp6_cksum = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
623                                                 sizeof(struct icmp6hdr)
624                                                 + payload_length,
625                                                 ipv6h->nexthdr, nskb->csum);
626 }
627 #endif /* IPv6 */
628
629 bool tnl_frag_needed(struct vport *vport, const struct tnl_mutable_config *mutable,
630                      struct sk_buff *skb, unsigned int mtu, __be64 flow_key)
631 {
632         unsigned int eth_hdr_len = ETH_HLEN;
633         unsigned int total_length = 0, header_length = 0, payload_length;
634         struct ethhdr *eh, *old_eh = eth_hdr(skb);
635         struct sk_buff *nskb;
636
637         /* Sanity check */
638         if (skb->protocol == htons(ETH_P_IP)) {
639                 if (mtu < IP_MIN_MTU)
640                         return false;
641
642                 if (!ipv4_should_icmp(skb))
643                         return true;
644         }
645 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
646         else if (skb->protocol == htons(ETH_P_IPV6)) {
647                 if (mtu < IPV6_MIN_MTU)
648                         return false;
649
650                 /*
651                  * In theory we should do PMTUD on IPv6 multicast messages but
652                  * we don't have an address to send from so just fragment.
653                  */
654                 if (ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST)
655                         return false;
656
657                 if (!ipv6_should_icmp(skb))
658                         return true;
659         }
660 #endif
661         else
662                 return false;
663
664         /* Allocate */
665         if (old_eh->h_proto == htons(ETH_P_8021Q))
666                 eth_hdr_len = VLAN_ETH_HLEN;
667
668         payload_length = skb->len - eth_hdr_len;
669         if (skb->protocol == htons(ETH_P_IP)) {
670                 header_length = sizeof(struct iphdr) + sizeof(struct icmphdr);
671                 total_length = min_t(unsigned int, header_length +
672                                                    payload_length, 576);
673         }
674 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
675         else {
676                 header_length = sizeof(struct ipv6hdr) +
677                                 sizeof(struct icmp6hdr);
678                 total_length = min_t(unsigned int, header_length +
679                                                   payload_length, IPV6_MIN_MTU);
680         }
681 #endif
682
683         payload_length = total_length - header_length;
684
685         nskb = dev_alloc_skb(NET_IP_ALIGN + eth_hdr_len + header_length +
686                              payload_length);
687         if (!nskb)
688                 return false;
689
690         skb_reserve(nskb, NET_IP_ALIGN);
691
692         /* Ethernet / VLAN */
693         eh = (struct ethhdr *)skb_put(nskb, eth_hdr_len);
694         memcpy(eh->h_dest, old_eh->h_source, ETH_ALEN);
695         memcpy(eh->h_source, mutable->eth_addr, ETH_ALEN);
696         nskb->protocol = eh->h_proto = old_eh->h_proto;
697         if (old_eh->h_proto == htons(ETH_P_8021Q)) {
698                 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)eh;
699
700                 vh->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
701                 vh->h_vlan_encapsulated_proto = skb->protocol;
702         } else
703                 vlan_set_tci(nskb, vlan_get_tci(skb));
704         skb_reset_mac_header(nskb);
705
706         /* Protocol */
707         if (skb->protocol == htons(ETH_P_IP))
708                 ipv4_build_icmp(skb, nskb, mtu, payload_length);
709 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
710         else
711                 ipv6_build_icmp(skb, nskb, mtu, payload_length);
712 #endif
713
714         /*
715          * Assume that flow based keys are symmetric with respect to input
716          * and output and use the key that we were going to put on the
717          * outgoing packet for the fake received packet.  If the keys are
718          * not symmetric then PMTUD needs to be disabled since we won't have
719          * any way of synthesizing packets.
720          */
721         if ((mutable->flags & (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION)) ==
722             (TNL_F_IN_KEY_MATCH | TNL_F_OUT_KEY_ACTION))
723                 OVS_CB(nskb)->tun_id = flow_key;
724
725         if (unlikely(compute_ip_summed(nskb, false))) {
726                 kfree_skb(nskb);
727                 return false;
728         }
729
730         vport_receive(vport, nskb);
731
732         return true;
733 }
734
735 static bool check_mtu(struct sk_buff *skb,
736                       struct vport *vport,
737                       const struct tnl_mutable_config *mutable,
738                       const struct rtable *rt, __be16 *frag_offp)
739 {
740         bool df_inherit = mutable->flags & TNL_F_DF_INHERIT;
741         bool pmtud = mutable->flags & TNL_F_PMTUD;
742         __be16 frag_off = mutable->flags & TNL_F_DF_DEFAULT ? htons(IP_DF) : 0;
743         int mtu = 0;
744         unsigned int packet_length = skb->len - ETH_HLEN;
745
746         /* Allow for one level of tagging in the packet length. */
747         if (!vlan_tx_tag_present(skb) &&
748             eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
749                 packet_length -= VLAN_HLEN;
750
751         if (pmtud) {
752                 int vlan_header = 0;
753
754                 /* The tag needs to go in packet regardless of where it
755                  * currently is, so subtract it from the MTU.
756                  */
757                 if (vlan_tx_tag_present(skb) ||
758                     eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
759                         vlan_header = VLAN_HLEN;
760
761                 mtu = dst_mtu(&rt_dst(rt))
762                         - ETH_HLEN
763                         - mutable->tunnel_hlen
764                         - vlan_header;
765         }
766
767         if (skb->protocol == htons(ETH_P_IP)) {
768                 struct iphdr *iph = ip_hdr(skb);
769
770                 if (df_inherit)
771                         frag_off = iph->frag_off & htons(IP_DF);
772
773                 if (pmtud && iph->frag_off & htons(IP_DF)) {
774                         mtu = max(mtu, IP_MIN_MTU);
775
776                         if (packet_length > mtu &&
777                             tnl_frag_needed(vport, mutable, skb, mtu,
778                                             OVS_CB(skb)->tun_id))
779                                 return false;
780                 }
781         }
782 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
783         else if (skb->protocol == htons(ETH_P_IPV6)) {
784                 /* IPv6 requires end hosts to do fragmentation
785                  * if the packet is above the minimum MTU.
786                  */
787                 if (df_inherit && packet_length > IPV6_MIN_MTU)
788                         frag_off = htons(IP_DF);
789
790                 if (pmtud) {
791                         mtu = max(mtu, IPV6_MIN_MTU);
792
793                         if (packet_length > mtu &&
794                             tnl_frag_needed(vport, mutable, skb, mtu,
795                                             OVS_CB(skb)->tun_id))
796                                 return false;
797                 }
798         }
799 #endif
800
801         *frag_offp = frag_off;
802         return true;
803 }
804
805 static void create_tunnel_header(const struct vport *vport,
806                                  const struct tnl_mutable_config *mutable,
807                                  const struct rtable *rt, void *header)
808 {
809         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
810         struct iphdr *iph = header;
811
812         iph->version    = 4;
813         iph->ihl        = sizeof(struct iphdr) >> 2;
814         iph->frag_off   = htons(IP_DF);
815         iph->protocol   = tnl_vport->tnl_ops->ipproto;
816         iph->tos        = mutable->tos;
817         iph->daddr      = rt->rt_dst;
818         iph->saddr      = rt->rt_src;
819         iph->ttl        = mutable->ttl;
820         if (!iph->ttl)
821                 iph->ttl = ip4_dst_hoplimit(&rt_dst(rt));
822
823         tnl_vport->tnl_ops->build_header(vport, mutable, iph + 1);
824 }
825
826 static inline void *get_cached_header(const struct tnl_cache *cache)
827 {
828         return (void *)cache + ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN);
829 }
830
831 static inline bool check_cache_valid(const struct tnl_cache *cache,
832                                      const struct tnl_mutable_config *mutable)
833 {
834         return cache &&
835 #ifdef NEED_CACHE_TIMEOUT
836                 time_before(jiffies, cache->expiration) &&
837 #endif
838 #ifdef HAVE_RT_GENID
839                 atomic_read(&init_net.ipv4.rt_genid) == cache->rt->rt_genid &&
840 #endif
841 #ifdef HAVE_HH_SEQ
842                 rt_dst(cache->rt).hh->hh_lock.sequence == cache->hh_seq &&
843 #endif
844                 mutable->seq == cache->mutable_seq &&
845                 (!is_internal_dev(rt_dst(cache->rt).dev) ||
846                 (cache->flow && !cache->flow->dead));
847 }
848
849 static int cache_cleaner_cb(struct tbl_node *tbl_node, void *aux)
850 {
851         struct tnl_vport *tnl_vport = tnl_vport_table_cast(tbl_node);
852         const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
853         const struct tnl_cache *cache = rcu_dereference(tnl_vport->cache);
854
855         if (cache && !check_cache_valid(cache, mutable) &&
856             spin_trylock_bh(&tnl_vport->cache_lock)) {
857                 assign_cache_rcu(tnl_vport_to_vport(tnl_vport), NULL);
858                 spin_unlock_bh(&tnl_vport->cache_lock);
859         }
860
861         return 0;
862 }
863
864 static void cache_cleaner(struct work_struct *work)
865 {
866         schedule_cache_cleaner();
867
868         rcu_read_lock();
869         tbl_foreach(rcu_dereference(port_table), cache_cleaner_cb, NULL);
870         rcu_read_unlock();
871 }
872
873 static inline void create_eth_hdr(struct tnl_cache *cache,
874                                   const struct rtable *rt)
875 {
876         void *cache_data = get_cached_header(cache);
877         int hh_len = rt_dst(rt).hh->hh_len;
878         int hh_off = HH_DATA_ALIGN(rt_dst(rt).hh->hh_len) - hh_len;
879
880 #ifdef HAVE_HH_SEQ
881         unsigned hh_seq;
882
883         do {
884                 hh_seq = read_seqbegin(&rt_dst(rt).hh->hh_lock);
885                 memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
886         } while (read_seqretry(&rt_dst(rt).hh->hh_lock, hh_seq));
887
888         cache->hh_seq = hh_seq;
889 #else
890         read_lock_bh(&rt_dst(rt).hh->hh_lock);
891         memcpy(cache_data, (void *)rt_dst(rt).hh->hh_data + hh_off, hh_len);
892         read_unlock_bh(&rt_dst(rt).hh->hh_lock);
893 #endif
894 }
895
896 static struct tnl_cache *build_cache(struct vport *vport,
897                                      const struct tnl_mutable_config *mutable,
898                                      struct rtable *rt)
899 {
900         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
901         struct tnl_cache *cache;
902         void *cache_data;
903         int cache_len;
904
905         if (!(mutable->flags & TNL_F_HDR_CACHE))
906                 return NULL;
907
908         /*
909          * If there is no entry in the ARP cache or if this device does not
910          * support hard header caching just fall back to the IP stack.
911          */
912         if (!rt_dst(rt).hh)
913                 return NULL;
914
915         /*
916          * If lock is contended fall back to directly building the header.
917          * We're not going to help performance by sitting here spinning.
918          */
919         if (!spin_trylock_bh(&tnl_vport->cache_lock))
920                 return NULL;
921
922         cache = cache_dereference(tnl_vport);
923         if (check_cache_valid(cache, mutable))
924                 goto unlock;
925         else
926                 cache = NULL;
927
928         cache_len = rt_dst(rt).hh->hh_len + mutable->tunnel_hlen;
929
930         cache = kzalloc(ALIGN(sizeof(struct tnl_cache), CACHE_DATA_ALIGN) +
931                         cache_len, GFP_ATOMIC);
932         if (!cache)
933                 goto unlock;
934
935         cache->len = cache_len;
936
937         create_eth_hdr(cache, rt);
938         cache_data = get_cached_header(cache) + rt_dst(rt).hh->hh_len;
939
940         create_tunnel_header(vport, mutable, rt, cache_data);
941
942         cache->mutable_seq = mutable->seq;
943         cache->rt = rt;
944 #ifdef NEED_CACHE_TIMEOUT
945         cache->expiration = jiffies + tnl_vport->cache_exp_interval;
946 #endif
947
948         if (is_internal_dev(rt_dst(rt).dev)) {
949                 struct sw_flow_key flow_key;
950                 struct tbl_node *flow_node;
951                 struct vport *dst_vport;
952                 struct sk_buff *skb;
953                 bool is_frag;
954                 int err;
955                 int flow_key_len;
956
957                 dst_vport = internal_dev_get_vport(rt_dst(rt).dev);
958                 if (!dst_vport)
959                         goto done;
960
961                 skb = alloc_skb(cache->len, GFP_ATOMIC);
962                 if (!skb)
963                         goto done;
964
965                 __skb_put(skb, cache->len);
966                 memcpy(skb->data, get_cached_header(cache), cache->len);
967
968                 err = flow_extract(skb, dst_vport->port_no, &flow_key,
969                                    &flow_key_len, &is_frag);
970
971                 kfree_skb(skb);
972                 if (err || is_frag)
973                         goto done;
974
975                 flow_node = tbl_lookup(rcu_dereference(dst_vport->dp->table),
976                                        &flow_key, flow_key_len,
977                                        flow_hash(&flow_key, flow_key_len),
978                                        flow_cmp);
979                 if (flow_node) {
980                         struct sw_flow *flow = flow_cast(flow_node);
981
982                         cache->flow = flow;
983                         flow_hold(flow);
984                 }
985         }
986
987 done:
988         assign_cache_rcu(vport, cache);
989
990 unlock:
991         spin_unlock_bh(&tnl_vport->cache_lock);
992
993         return cache;
994 }
995
996 static struct rtable *find_route(struct vport *vport,
997                                  const struct tnl_mutable_config *mutable,
998                                  u8 tos, struct tnl_cache **cache)
999 {
1000         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1001         struct tnl_cache *cur_cache = rcu_dereference(tnl_vport->cache);
1002
1003         *cache = NULL;
1004         tos = RT_TOS(tos);
1005
1006         if (likely(tos == mutable->tos && check_cache_valid(cur_cache, mutable))) {
1007                 *cache = cur_cache;
1008                 return cur_cache->rt;
1009         } else {
1010                 struct rtable *rt;
1011 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
1012                 struct flowi fl = { .nl_u = { .ip4_u =
1013                                               { .daddr = mutable->daddr,
1014                                                 .saddr = mutable->saddr,
1015                                                 .tos = tos } },
1016                                     .proto = tnl_vport->tnl_ops->ipproto };
1017
1018                 if (unlikely(ip_route_output_key(&init_net, &rt, &fl)))
1019                         return NULL;
1020 #else
1021                 struct flowi4 fl = { .daddr = mutable->daddr,
1022                                      .saddr = mutable->saddr,
1023                                      .flowi4_tos = tos,
1024                                      .flowi4_proto = tnl_vport->tnl_ops->ipproto };
1025
1026                 rt = ip_route_output_key(&init_net, &fl);
1027                 if (IS_ERR(rt))
1028                         return NULL;
1029 #endif
1030
1031                 if (likely(tos == mutable->tos))
1032                         *cache = build_cache(vport, mutable, rt);
1033
1034                 return rt;
1035         }
1036 }
1037
1038 static inline bool need_linearize(const struct sk_buff *skb)
1039 {
1040         int i;
1041
1042         if (unlikely(skb_shinfo(skb)->frag_list))
1043                 return true;
1044
1045         /*
1046          * Generally speaking we should linearize if there are paged frags.
1047          * However, if all of the refcounts are 1 we know nobody else can
1048          * change them from underneath us and we can skip the linearization.
1049          */
1050         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
1051                 if (unlikely(page_count(skb_shinfo(skb)->frags[i].page) > 1))
1052                         return true;
1053
1054         return false;
1055 }
1056
1057 static struct sk_buff *handle_offloads(struct sk_buff *skb,
1058                                        const struct tnl_mutable_config *mutable,
1059                                        const struct rtable *rt)
1060 {
1061         int min_headroom;
1062         int err;
1063
1064         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
1065                         + mutable->tunnel_hlen
1066                         + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1067
1068         if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
1069                 int head_delta = SKB_DATA_ALIGN(min_headroom -
1070                                                 skb_headroom(skb) +
1071                                                 16);
1072                 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
1073                                         0, GFP_ATOMIC);
1074                 if (unlikely(err))
1075                         goto error_free;
1076         }
1077
1078         forward_ip_summed(skb, true);
1079
1080         if (skb_is_gso(skb)) {
1081                 struct sk_buff *nskb;
1082
1083                 nskb = skb_gso_segment(skb, 0);
1084                 kfree_skb(skb);
1085                 if (IS_ERR(nskb)) {
1086                         err = PTR_ERR(nskb);
1087                         goto error;
1088                 }
1089
1090                 skb = nskb;
1091         } else if (get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
1092                 /* Pages aren't locked and could change at any time.
1093                  * If this happens after we compute the checksum, the
1094                  * checksum will be wrong.  We linearize now to avoid
1095                  * this problem.
1096                  */
1097                 if (unlikely(need_linearize(skb))) {
1098                         err = __skb_linearize(skb);
1099                         if (unlikely(err))
1100                                 goto error_free;
1101                 }
1102
1103                 err = skb_checksum_help(skb);
1104                 if (unlikely(err))
1105                         goto error_free;
1106         }
1107
1108         set_ip_summed(skb, OVS_CSUM_NONE);
1109
1110         return skb;
1111
1112 error_free:
1113         kfree_skb(skb);
1114 error:
1115         return ERR_PTR(err);
1116 }
1117
1118 static int send_frags(struct sk_buff *skb,
1119                       const struct tnl_mutable_config *mutable)
1120 {
1121         int sent_len;
1122
1123         sent_len = 0;
1124         while (skb) {
1125                 struct sk_buff *next = skb->next;
1126                 int frag_len = skb->len - mutable->tunnel_hlen;
1127                 int err;
1128
1129                 skb->next = NULL;
1130                 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
1131
1132                 err = ip_local_out(skb);
1133                 skb = next;
1134                 if (unlikely(net_xmit_eval(err)))
1135                         goto free_frags;
1136                 sent_len += frag_len;
1137         }
1138
1139         return sent_len;
1140
1141 free_frags:
1142         /*
1143          * There's no point in continuing to send fragments once one has been
1144          * dropped so just free the rest.  This may help improve the congestion
1145          * that caused the first packet to be dropped.
1146          */
1147         tnl_free_linked_skbs(skb);
1148         return sent_len;
1149 }
1150
1151 int tnl_send(struct vport *vport, struct sk_buff *skb)
1152 {
1153         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1154         const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
1155
1156         enum vport_err_type err = VPORT_E_TX_ERROR;
1157         struct rtable *rt;
1158         struct dst_entry *unattached_dst = NULL;
1159         struct tnl_cache *cache;
1160         int sent_len = 0;
1161         __be16 frag_off = 0;
1162         u8 ttl;
1163         u8 inner_tos;
1164         u8 tos;
1165
1166         /* Validate the protocol headers before we try to use them. */
1167         if (skb->protocol == htons(ETH_P_8021Q) &&
1168             !vlan_tx_tag_present(skb)) {
1169                 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
1170                         goto error_free;
1171
1172                 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
1173                 skb_set_network_header(skb, VLAN_ETH_HLEN);
1174         }
1175
1176         if (skb->protocol == htons(ETH_P_IP)) {
1177                 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1178                     + sizeof(struct iphdr))))
1179                         skb->protocol = 0;
1180         }
1181 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1182         else if (skb->protocol == htons(ETH_P_IPV6)) {
1183                 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
1184                     + sizeof(struct ipv6hdr))))
1185                         skb->protocol = 0;
1186         }
1187 #endif
1188
1189         /* ToS */
1190         if (skb->protocol == htons(ETH_P_IP))
1191                 inner_tos = ip_hdr(skb)->tos;
1192 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1193         else if (skb->protocol == htons(ETH_P_IPV6))
1194                 inner_tos = ipv6_get_dsfield(ipv6_hdr(skb));
1195 #endif
1196         else
1197                 inner_tos = 0;
1198
1199         if (mutable->flags & TNL_F_TOS_INHERIT)
1200                 tos = inner_tos;
1201         else
1202                 tos = mutable->tos;
1203
1204         tos = INET_ECN_encapsulate(tos, inner_tos);
1205
1206         /* Route lookup */
1207         rt = find_route(vport, mutable, tos, &cache);
1208         if (unlikely(!rt))
1209                 goto error_free;
1210         if (unlikely(!cache))
1211                 unattached_dst = &rt_dst(rt);
1212
1213         /* Reset SKB */
1214         nf_reset(skb);
1215         secpath_reset(skb);
1216         skb_dst_drop(skb);
1217         skb_clear_rxhash(skb);
1218
1219         /* Offloading */
1220         skb = handle_offloads(skb, mutable, rt);
1221         if (IS_ERR(skb))
1222                 goto error;
1223
1224         /* MTU */
1225         if (unlikely(!check_mtu(skb, vport, mutable, rt, &frag_off))) {
1226                 err = VPORT_E_TX_DROPPED;
1227                 goto error_free;
1228         }
1229
1230         /*
1231          * If we are over the MTU, allow the IP stack to handle fragmentation.
1232          * Fragmentation is a slow path anyways.
1233          */
1234         if (unlikely(skb->len + mutable->tunnel_hlen > dst_mtu(&rt_dst(rt)) &&
1235                      cache)) {
1236                 unattached_dst = &rt_dst(rt);
1237                 dst_hold(unattached_dst);
1238                 cache = NULL;
1239         }
1240
1241         /* TTL */
1242         ttl = mutable->ttl;
1243         if (!ttl)
1244                 ttl = ip4_dst_hoplimit(&rt_dst(rt));
1245
1246         if (mutable->flags & TNL_F_TTL_INHERIT) {
1247                 if (skb->protocol == htons(ETH_P_IP))
1248                         ttl = ip_hdr(skb)->ttl;
1249 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
1250                 else if (skb->protocol == htons(ETH_P_IPV6))
1251                         ttl = ipv6_hdr(skb)->hop_limit;
1252 #endif
1253         }
1254
1255         while (skb) {
1256                 struct iphdr *iph;
1257                 struct sk_buff *next_skb = skb->next;
1258                 skb->next = NULL;
1259
1260                 if (unlikely(vlan_deaccel_tag(skb)))
1261                         goto next;
1262
1263                 if (likely(cache)) {
1264                         skb_push(skb, cache->len);
1265                         memcpy(skb->data, get_cached_header(cache), cache->len);
1266                         skb_reset_mac_header(skb);
1267                         skb_set_network_header(skb, rt_dst(rt).hh->hh_len);
1268
1269                 } else {
1270                         skb_push(skb, mutable->tunnel_hlen);
1271                         create_tunnel_header(vport, mutable, rt, skb->data);
1272                         skb_reset_network_header(skb);
1273
1274                         if (next_skb)
1275                                 skb_dst_set(skb, dst_clone(unattached_dst));
1276                         else {
1277                                 skb_dst_set(skb, unattached_dst);
1278                                 unattached_dst = NULL;
1279                         }
1280                 }
1281                 skb_set_transport_header(skb, skb_network_offset(skb) + sizeof(struct iphdr));
1282
1283                 iph = ip_hdr(skb);
1284                 iph->tos = tos;
1285                 iph->ttl = ttl;
1286                 iph->frag_off = frag_off;
1287                 ip_select_ident(iph, &rt_dst(rt), NULL);
1288
1289                 skb = tnl_vport->tnl_ops->update_header(vport, mutable, &rt_dst(rt), skb);
1290                 if (unlikely(!skb))
1291                         goto next;
1292
1293                 if (likely(cache)) {
1294                         int orig_len = skb->len - cache->len;
1295                         struct vport *cache_vport = internal_dev_get_vport(rt_dst(rt).dev);
1296
1297                         skb->protocol = htons(ETH_P_IP);
1298                         iph = ip_hdr(skb);
1299                         iph->tot_len = htons(skb->len - skb_network_offset(skb));
1300                         ip_send_check(iph);
1301
1302                         if (cache_vport) {
1303                                 if (unlikely(compute_ip_summed(skb, true))) {
1304                                         kfree_skb(skb);
1305                                         goto next;
1306                                 }
1307
1308                                 OVS_CB(skb)->flow = cache->flow;
1309                                 vport_receive(cache_vport, skb);
1310                                 sent_len += orig_len;
1311                         } else {
1312                                 int xmit_err;
1313
1314                                 skb->dev = rt_dst(rt).dev;
1315                                 xmit_err = dev_queue_xmit(skb);
1316
1317                                 if (likely(net_xmit_eval(xmit_err) == 0))
1318                                         sent_len += orig_len;
1319                         }
1320                 } else
1321                         sent_len += send_frags(skb, mutable);
1322
1323 next:
1324                 skb = next_skb;
1325         }
1326
1327         if (unlikely(sent_len == 0))
1328                 vport_record_error(vport, VPORT_E_TX_DROPPED);
1329
1330         goto out;
1331
1332 error_free:
1333         tnl_free_linked_skbs(skb);
1334 error:
1335         vport_record_error(vport, err);
1336 out:
1337         dst_release(unattached_dst);
1338         return sent_len;
1339 }
1340
1341 static const struct nla_policy tnl_policy[ODP_TUNNEL_ATTR_MAX + 1] = {
1342         [ODP_TUNNEL_ATTR_FLAGS]    = { .type = NLA_U32 },
1343         [ODP_TUNNEL_ATTR_DST_IPV4] = { .type = NLA_U32 },
1344         [ODP_TUNNEL_ATTR_SRC_IPV4] = { .type = NLA_U32 },
1345         [ODP_TUNNEL_ATTR_OUT_KEY]  = { .type = NLA_U64 },
1346         [ODP_TUNNEL_ATTR_IN_KEY]   = { .type = NLA_U64 },
1347         [ODP_TUNNEL_ATTR_TOS]      = { .type = NLA_U8 },
1348         [ODP_TUNNEL_ATTR_TTL]      = { .type = NLA_U8 },
1349 };
1350
1351 /* Sets ODP_TUNNEL_ATTR_* fields in 'mutable', which must initially be zeroed. */
1352 static int tnl_set_config(struct nlattr *options, const struct tnl_ops *tnl_ops,
1353                           const struct vport *cur_vport,
1354                           struct tnl_mutable_config *mutable)
1355 {
1356         const struct vport *old_vport;
1357         const struct tnl_mutable_config *old_mutable;
1358         struct nlattr *a[ODP_TUNNEL_ATTR_MAX + 1];
1359         int err;
1360
1361         if (!options)
1362                 return -EINVAL;
1363
1364         err = nla_parse_nested(a, ODP_TUNNEL_ATTR_MAX, options, tnl_policy);
1365         if (err)
1366                 return err;
1367
1368         if (!a[ODP_TUNNEL_ATTR_FLAGS] || !a[ODP_TUNNEL_ATTR_DST_IPV4])
1369                 return -EINVAL;
1370
1371         mutable->flags = nla_get_u32(a[ODP_TUNNEL_ATTR_FLAGS]) & TNL_F_PUBLIC;
1372
1373         if (a[ODP_TUNNEL_ATTR_SRC_IPV4])
1374                 mutable->saddr = nla_get_be32(a[ODP_TUNNEL_ATTR_SRC_IPV4]);
1375         mutable->daddr = nla_get_be32(a[ODP_TUNNEL_ATTR_DST_IPV4]);
1376
1377         if (a[ODP_TUNNEL_ATTR_TOS]) {
1378                 mutable->tos = nla_get_u8(a[ODP_TUNNEL_ATTR_TOS]);
1379                 if (mutable->tos != RT_TOS(mutable->tos))
1380                         return -EINVAL;
1381         }
1382
1383         if (a[ODP_TUNNEL_ATTR_TTL])
1384                 mutable->ttl = nla_get_u8(a[ODP_TUNNEL_ATTR_TTL]);
1385
1386         mutable->tunnel_type = tnl_ops->tunnel_type;
1387         if (!a[ODP_TUNNEL_ATTR_IN_KEY]) {
1388                 mutable->tunnel_type |= TNL_T_KEY_MATCH;
1389                 mutable->flags |= TNL_F_IN_KEY_MATCH;
1390         } else {
1391                 mutable->tunnel_type |= TNL_T_KEY_EXACT;
1392                 mutable->in_key = nla_get_be64(a[ODP_TUNNEL_ATTR_IN_KEY]);
1393         }
1394
1395         if (!a[ODP_TUNNEL_ATTR_OUT_KEY])
1396                 mutable->flags |= TNL_F_OUT_KEY_ACTION;
1397         else
1398                 mutable->out_key = nla_get_be64(a[ODP_TUNNEL_ATTR_OUT_KEY]);
1399
1400         mutable->tunnel_hlen = tnl_ops->hdr_len(mutable);
1401         if (mutable->tunnel_hlen < 0)
1402                 return mutable->tunnel_hlen;
1403
1404         mutable->tunnel_hlen += sizeof(struct iphdr);
1405
1406         old_vport = tnl_find_port(mutable->saddr, mutable->daddr,
1407                                   mutable->in_key, mutable->tunnel_type,
1408                                   &old_mutable);
1409
1410         if (old_vport && old_vport != cur_vport)
1411                 return -EEXIST;
1412
1413         return 0;
1414 }
1415
1416 struct vport *tnl_create(const struct vport_parms *parms,
1417                          const struct vport_ops *vport_ops,
1418                          const struct tnl_ops *tnl_ops)
1419 {
1420         struct vport *vport;
1421         struct tnl_vport *tnl_vport;
1422         struct tnl_mutable_config *mutable;
1423         int initial_frag_id;
1424         int err;
1425
1426         vport = vport_alloc(sizeof(struct tnl_vport), vport_ops, parms);
1427         if (IS_ERR(vport)) {
1428                 err = PTR_ERR(vport);
1429                 goto error;
1430         }
1431
1432         tnl_vport = tnl_vport_priv(vport);
1433
1434         strcpy(tnl_vport->name, parms->name);
1435         tnl_vport->tnl_ops = tnl_ops;
1436
1437         mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1438         if (!mutable) {
1439                 err = -ENOMEM;
1440                 goto error_free_vport;
1441         }
1442
1443         vport_gen_rand_ether_addr(mutable->eth_addr);
1444
1445         get_random_bytes(&initial_frag_id, sizeof(int));
1446         atomic_set(&tnl_vport->frag_id, initial_frag_id);
1447
1448         err = tnl_set_config(parms->options, tnl_ops, NULL, mutable);
1449         if (err)
1450                 goto error_free_mutable;
1451
1452         spin_lock_init(&tnl_vport->cache_lock);
1453
1454 #ifdef NEED_CACHE_TIMEOUT
1455         tnl_vport->cache_exp_interval = MAX_CACHE_EXP -
1456                                        (net_random() % (MAX_CACHE_EXP / 2));
1457 #endif
1458
1459         rcu_assign_pointer(tnl_vport->mutable, mutable);
1460
1461         err = add_port(vport);
1462         if (err)
1463                 goto error_free_mutable;
1464
1465         return vport;
1466
1467 error_free_mutable:
1468         kfree(mutable);
1469 error_free_vport:
1470         vport_free(vport);
1471 error:
1472         return ERR_PTR(err);
1473 }
1474
1475 int tnl_set_options(struct vport *vport, struct nlattr *options)
1476 {
1477         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1478         const struct tnl_mutable_config *old_mutable;
1479         struct tnl_mutable_config *mutable;
1480         int err;
1481
1482         mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1483         if (!mutable) {
1484                 err = -ENOMEM;
1485                 goto error;
1486         }
1487
1488         /* Copy fields whose values should be retained. */
1489         old_mutable = rtnl_dereference(tnl_vport->mutable);
1490         mutable->seq = old_mutable->seq + 1;
1491         memcpy(mutable->eth_addr, old_mutable->eth_addr, ETH_ALEN);
1492
1493         /* Parse the others configured by userspace. */
1494         err = tnl_set_config(options, tnl_vport->tnl_ops, vport, mutable);
1495         if (err)
1496                 goto error_free;
1497
1498         err = move_port(vport, mutable);
1499         if (err)
1500                 goto error_free;
1501
1502         return 0;
1503
1504 error_free:
1505         kfree(mutable);
1506 error:
1507         return err;
1508 }
1509
1510 int tnl_get_options(const struct vport *vport, struct sk_buff *skb)
1511 {
1512         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1513         const struct tnl_mutable_config *mutable = rcu_dereference_rtnl(tnl_vport->mutable);
1514
1515         NLA_PUT_U32(skb, ODP_TUNNEL_ATTR_FLAGS, mutable->flags & TNL_F_PUBLIC);
1516         NLA_PUT_BE32(skb, ODP_TUNNEL_ATTR_DST_IPV4, mutable->daddr);
1517
1518         if (!(mutable->flags & TNL_F_IN_KEY_MATCH))
1519                 NLA_PUT_BE64(skb, ODP_TUNNEL_ATTR_IN_KEY, mutable->in_key);
1520         if (!(mutable->flags & TNL_F_OUT_KEY_ACTION))
1521                 NLA_PUT_BE64(skb, ODP_TUNNEL_ATTR_OUT_KEY, mutable->out_key);
1522         if (mutable->saddr)
1523                 NLA_PUT_BE32(skb, ODP_TUNNEL_ATTR_SRC_IPV4, mutable->saddr);
1524         if (mutable->tos)
1525                 NLA_PUT_U8(skb, ODP_TUNNEL_ATTR_TOS, mutable->tos);
1526         if (mutable->ttl)
1527                 NLA_PUT_U8(skb, ODP_TUNNEL_ATTR_TTL, mutable->ttl);
1528
1529         return 0;
1530
1531 nla_put_failure:
1532         return -EMSGSIZE;
1533 }
1534
1535 static void free_port_rcu(struct rcu_head *rcu)
1536 {
1537         struct tnl_vport *tnl_vport = container_of(rcu,
1538                                                    struct tnl_vport, rcu);
1539
1540         free_cache((struct tnl_cache __force *)tnl_vport->cache);
1541         kfree((struct tnl_mutable __force *)tnl_vport->mutable);
1542         vport_free(tnl_vport_to_vport(tnl_vport));
1543 }
1544
1545 int tnl_destroy(struct vport *vport)
1546 {
1547         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1548         const struct tnl_mutable_config *mutable, *old_mutable;
1549
1550         mutable = rtnl_dereference(tnl_vport->mutable);
1551
1552         if (vport == tnl_find_port(mutable->saddr, mutable->daddr,
1553                                    mutable->in_key, mutable->tunnel_type,
1554                                    &old_mutable))
1555                 del_port(vport);
1556
1557         call_rcu(&tnl_vport->rcu, free_port_rcu);
1558
1559         return 0;
1560 }
1561
1562 int tnl_set_addr(struct vport *vport, const unsigned char *addr)
1563 {
1564         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1565         struct tnl_mutable_config *mutable;
1566
1567         mutable = kmemdup(rtnl_dereference(tnl_vport->mutable),
1568                           sizeof(struct tnl_mutable_config), GFP_KERNEL);
1569         if (!mutable)
1570                 return -ENOMEM;
1571
1572         memcpy(mutable->eth_addr, addr, ETH_ALEN);
1573         assign_config_rcu(vport, mutable);
1574
1575         return 0;
1576 }
1577
1578 const char *tnl_get_name(const struct vport *vport)
1579 {
1580         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1581         return tnl_vport->name;
1582 }
1583
1584 const unsigned char *tnl_get_addr(const struct vport *vport)
1585 {
1586         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1587         return rcu_dereference_rtnl(tnl_vport->mutable)->eth_addr;
1588 }
1589
1590 void tnl_free_linked_skbs(struct sk_buff *skb)
1591 {
1592         while (skb) {
1593                 struct sk_buff *next = skb->next;
1594                 kfree_skb(skb);
1595                 skb = next;
1596         }
1597 }