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