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