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