26d9014c492fef1f127b35fb02378f614c95c0ce
[sliver-openvswitch.git] / datapath / tunnel.c
1 /*
2  * Copyright (c) 2007-2012 Nicira, Inc.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 2 of the GNU General Public
6  * License as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
16  * 02110-1301, USA
17  */
18
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20
21 #include <linux/if_arp.h>
22 #include <linux/if_ether.h>
23 #include <linux/ip.h>
24 #include <linux/if_vlan.h>
25 #include <linux/igmp.h>
26 #include <linux/in.h>
27 #include <linux/in_route.h>
28 #include <linux/inetdevice.h>
29 #include <linux/jhash.h>
30 #include <linux/list.h>
31 #include <linux/kernel.h>
32 #include <linux/version.h>
33 #include <linux/workqueue.h>
34 #include <linux/rculist.h>
35
36 #include <net/dsfield.h>
37 #include <net/dst.h>
38 #include <net/icmp.h>
39 #include <net/inet_ecn.h>
40 #include <net/ip.h>
41 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
42 #include <net/ipv6.h>
43 #endif
44 #include <net/route.h>
45 #include <net/xfrm.h>
46
47 #include "checksum.h"
48 #include "datapath.h"
49 #include "tunnel.h"
50 #include "vlan.h"
51 #include "vport.h"
52 #include "vport-generic.h"
53 #include "vport-internal_dev.h"
54
55 #define PORT_TABLE_SIZE  1024
56
57 static struct hlist_head *port_table __read_mostly;
58
59 /*
60  * These are just used as an optimization: they don't require any kind of
61  * synchronization because we could have just as easily read the value before
62  * the port change happened.
63  */
64 static unsigned int key_local_remote_ports __read_mostly;
65 static unsigned int key_remote_ports __read_mostly;
66 static unsigned int key_multicast_ports __read_mostly;
67 static unsigned int local_remote_ports __read_mostly;
68 static unsigned int remote_ports __read_mostly;
69 static unsigned int null_ports __read_mostly;
70 static unsigned int multicast_ports __read_mostly;
71
72 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
73 #define rt_dst(rt) (rt->dst)
74 #else
75 #define rt_dst(rt) (rt->u.dst)
76 #endif
77
78 static struct vport *tnl_vport_to_vport(const struct tnl_vport *tnl_vport)
79 {
80         return vport_from_priv(tnl_vport);
81 }
82
83 static void free_config_rcu(struct rcu_head *rcu)
84 {
85         struct tnl_mutable_config *c = container_of(rcu, struct tnl_mutable_config, rcu);
86         kfree(c);
87 }
88
89 /* Frees the portion of 'mutable' that requires RTNL and thus can't happen
90  * within an RCU callback.  Fortunately this part doesn't require waiting for
91  * an RCU grace period.
92  */
93 static void free_mutable_rtnl(struct tnl_mutable_config *mutable)
94 {
95         ASSERT_RTNL();
96         if (ipv4_is_multicast(mutable->key.daddr) && mutable->mlink) {
97                 struct in_device *in_dev;
98                 in_dev = inetdev_by_index(port_key_get_net(&mutable->key), mutable->mlink);
99                 if (in_dev)
100                         ip_mc_dec_group(in_dev, mutable->key.daddr);
101         }
102 }
103
104 static void assign_config_rcu(struct vport *vport,
105                               struct tnl_mutable_config *new_config)
106 {
107         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
108         struct tnl_mutable_config *old_config;
109
110         old_config = rtnl_dereference(tnl_vport->mutable);
111         rcu_assign_pointer(tnl_vport->mutable, new_config);
112
113         free_mutable_rtnl(old_config);
114         call_rcu(&old_config->rcu, free_config_rcu);
115 }
116
117 static unsigned int *find_port_pool(const struct tnl_mutable_config *mutable)
118 {
119         bool is_multicast = ipv4_is_multicast(mutable->key.daddr);
120
121         if (mutable->flags & TNL_F_IN_KEY_MATCH) {
122                 if (mutable->key.saddr)
123                         return &local_remote_ports;
124                 else if (is_multicast)
125                         return &multicast_ports;
126                 else
127                         return &remote_ports;
128         } else {
129                 if (mutable->key.saddr)
130                         return &key_local_remote_ports;
131                 else if (is_multicast)
132                         return &key_multicast_ports;
133                 else if (mutable->key.daddr)
134                         return &key_remote_ports;
135                 else
136                         return &null_ports;
137         }
138 }
139
140 static u32 port_hash(const struct port_lookup_key *key)
141 {
142         return jhash2((u32 *)key, (PORT_KEY_LEN / sizeof(u32)), 0);
143 }
144
145 static struct hlist_head *find_bucket(u32 hash)
146 {
147         return &port_table[(hash & (PORT_TABLE_SIZE - 1))];
148 }
149
150 static void port_table_add_port(struct vport *vport)
151 {
152         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
153         const struct tnl_mutable_config *mutable;
154         u32 hash;
155
156         mutable = rtnl_dereference(tnl_vport->mutable);
157         hash = port_hash(&mutable->key);
158         hlist_add_head_rcu(&tnl_vport->hash_node, find_bucket(hash));
159
160         (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))++;
161 }
162
163 static void port_table_move_port(struct vport *vport,
164                       struct tnl_mutable_config *new_mutable)
165 {
166         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
167         u32 hash;
168
169         hash = port_hash(&new_mutable->key);
170         hlist_del_init_rcu(&tnl_vport->hash_node);
171         hlist_add_head_rcu(&tnl_vport->hash_node, find_bucket(hash));
172
173         (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
174         assign_config_rcu(vport, new_mutable);
175         (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))++;
176 }
177
178 static void port_table_remove_port(struct vport *vport)
179 {
180         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
181
182         hlist_del_init_rcu(&tnl_vport->hash_node);
183
184         (*find_port_pool(rtnl_dereference(tnl_vport->mutable)))--;
185 }
186
187 static struct vport *port_table_lookup(struct port_lookup_key *key,
188                                        const struct tnl_mutable_config **pmutable)
189 {
190         struct hlist_node *n;
191         struct hlist_head *bucket;
192         u32 hash = port_hash(key);
193         struct tnl_vport *tnl_vport;
194
195         bucket = find_bucket(hash);
196
197         hlist_for_each_entry_rcu(tnl_vport, n, bucket, hash_node) {
198                 struct tnl_mutable_config *mutable;
199
200                 mutable = rcu_dereference_rtnl(tnl_vport->mutable);
201                 if (!memcmp(&mutable->key, key, PORT_KEY_LEN)) {
202                         *pmutable = mutable;
203                         return tnl_vport_to_vport(tnl_vport);
204                 }
205         }
206
207         return NULL;
208 }
209
210 struct vport *ovs_tnl_find_port(struct net *net, __be32 saddr, __be32 daddr,
211                                 __be64 key, int tunnel_type,
212                                 const struct tnl_mutable_config **mutable)
213 {
214         struct port_lookup_key lookup;
215         struct vport *vport;
216         bool is_multicast = ipv4_is_multicast(saddr);
217
218         port_key_set_net(&lookup, net);
219         lookup.saddr = saddr;
220         lookup.daddr = daddr;
221
222         /* First try for exact match on in_key. */
223         lookup.in_key = key;
224         lookup.tunnel_type = tunnel_type | TNL_T_KEY_EXACT;
225         if (!is_multicast && key_local_remote_ports) {
226                 vport = port_table_lookup(&lookup, mutable);
227                 if (vport)
228                         return vport;
229         }
230         if (key_remote_ports) {
231                 lookup.saddr = 0;
232                 vport = port_table_lookup(&lookup, mutable);
233                 if (vport)
234                         return vport;
235
236                 lookup.saddr = saddr;
237         }
238
239         /* Then try matches that wildcard in_key. */
240         lookup.in_key = 0;
241         lookup.tunnel_type = tunnel_type | TNL_T_KEY_MATCH;
242         if (!is_multicast && local_remote_ports) {
243                 vport = port_table_lookup(&lookup, mutable);
244                 if (vport)
245                         return vport;
246         }
247         if (remote_ports) {
248                 lookup.saddr = 0;
249                 vport = port_table_lookup(&lookup, mutable);
250                 if (vport)
251                         return vport;
252         }
253
254         if (is_multicast) {
255                 lookup.saddr = 0;
256                 lookup.daddr = saddr;
257                 if (key_multicast_ports) {
258                         lookup.tunnel_type = tunnel_type | TNL_T_KEY_EXACT;
259                         lookup.in_key = key;
260                         vport = port_table_lookup(&lookup, mutable);
261                         if (vport)
262                                 return vport;
263                 }
264                 if (multicast_ports) {
265                         lookup.tunnel_type = tunnel_type | TNL_T_KEY_MATCH;
266                         lookup.in_key = 0;
267                         vport = port_table_lookup(&lookup, mutable);
268                         if (vport)
269                                 return vport;
270                 }
271         }
272
273         if (null_ports) {
274                 lookup.daddr = 0;
275                 lookup.saddr = 0;
276                 lookup.in_key = 0;
277                 lookup.tunnel_type = tunnel_type;
278                 vport = port_table_lookup(&lookup, mutable);
279                 if (vport)
280                         return vport;
281         }
282         return NULL;
283 }
284
285 static void ecn_decapsulate(struct sk_buff *skb)
286 {
287         if (unlikely(INET_ECN_is_ce(OVS_CB(skb)->tun_key->ipv4_tos))) {
288                 __be16 protocol = skb->protocol;
289
290                 skb_set_network_header(skb, ETH_HLEN);
291
292                 if (protocol == htons(ETH_P_8021Q)) {
293                         if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
294                                 return;
295
296                         protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
297                         skb_set_network_header(skb, VLAN_ETH_HLEN);
298                 }
299
300                 if (protocol == htons(ETH_P_IP)) {
301                         if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
302                             + sizeof(struct iphdr))))
303                                 return;
304
305                         IP_ECN_set_ce(ip_hdr(skb));
306                 }
307 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
308                 else if (protocol == htons(ETH_P_IPV6)) {
309                         if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
310                             + sizeof(struct ipv6hdr))))
311                                 return;
312
313                         IP6_ECN_set_ce(ipv6_hdr(skb));
314                 }
315 #endif
316         }
317 }
318
319 /**
320  *      ovs_tnl_rcv - ingress point for generic tunnel code
321  *
322  * @vport: port this packet was received on
323  * @skb: received packet
324  * @tos: ToS from encapsulating IP packet, used to copy ECN bits
325  *
326  * Must be called with rcu_read_lock.
327  *
328  * Packets received by this function are in the following state:
329  * - skb->data points to the inner Ethernet header.
330  * - The inner Ethernet header is in the linear data area.
331  * - skb->csum does not include the inner Ethernet header.
332  * - The layer pointers are undefined.
333  */
334 void ovs_tnl_rcv(struct vport *vport, struct sk_buff *skb)
335 {
336         struct ethhdr *eh;
337
338         skb_reset_mac_header(skb);
339         eh = eth_hdr(skb);
340
341         if (likely(ntohs(eh->h_proto) >= 1536))
342                 skb->protocol = eh->h_proto;
343         else
344                 skb->protocol = htons(ETH_P_802_2);
345
346         skb_dst_drop(skb);
347         nf_reset(skb);
348         skb_clear_rxhash(skb);
349         secpath_reset(skb);
350
351         ecn_decapsulate(skb);
352         vlan_set_tci(skb, 0);
353
354         if (unlikely(compute_ip_summed(skb, false))) {
355                 kfree_skb(skb);
356                 return;
357         }
358
359         ovs_vport_receive(vport, skb);
360 }
361
362 static bool check_ipv4_address(__be32 addr)
363 {
364         if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr)
365             || ipv4_is_loopback(addr) || ipv4_is_zeronet(addr))
366                 return false;
367
368         return true;
369 }
370
371 static bool ipv4_should_icmp(struct sk_buff *skb)
372 {
373         struct iphdr *old_iph = ip_hdr(skb);
374
375         /* Don't respond to L2 broadcast. */
376         if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
377                 return false;
378
379         /* Don't respond to L3 broadcast or invalid addresses. */
380         if (!check_ipv4_address(old_iph->daddr) ||
381             !check_ipv4_address(old_iph->saddr))
382                 return false;
383
384         /* Only respond to the first fragment. */
385         if (old_iph->frag_off & htons(IP_OFFSET))
386                 return false;
387
388         /* Don't respond to ICMP error messages. */
389         if (old_iph->protocol == IPPROTO_ICMP) {
390                 u8 icmp_type, *icmp_typep;
391
392                 icmp_typep = skb_header_pointer(skb, (u8 *)old_iph +
393                                                 (old_iph->ihl << 2) +
394                                                 offsetof(struct icmphdr, type) -
395                                                 skb->data, sizeof(icmp_type),
396                                                 &icmp_type);
397
398                 if (!icmp_typep)
399                         return false;
400
401                 if (*icmp_typep > NR_ICMP_TYPES
402                         || (*icmp_typep <= ICMP_PARAMETERPROB
403                                 && *icmp_typep != ICMP_ECHOREPLY
404                                 && *icmp_typep != ICMP_ECHO))
405                         return false;
406         }
407
408         return true;
409 }
410
411 static void ipv4_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
412                             unsigned int mtu, unsigned int payload_length)
413 {
414         struct iphdr *iph, *old_iph = ip_hdr(skb);
415         struct icmphdr *icmph;
416         u8 *payload;
417
418         iph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
419         icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
420         payload = skb_put(nskb, payload_length);
421
422         /* IP */
423         iph->version            =       4;
424         iph->ihl                =       sizeof(struct iphdr) >> 2;
425         iph->tos                =       (old_iph->tos & IPTOS_TOS_MASK) |
426                                         IPTOS_PREC_INTERNETCONTROL;
427         iph->tot_len            =       htons(sizeof(struct iphdr)
428                                               + sizeof(struct icmphdr)
429                                               + payload_length);
430         get_random_bytes(&iph->id, sizeof(iph->id));
431         iph->frag_off           =       0;
432         iph->ttl                =       IPDEFTTL;
433         iph->protocol           =       IPPROTO_ICMP;
434         iph->daddr              =       old_iph->saddr;
435         iph->saddr              =       old_iph->daddr;
436
437         ip_send_check(iph);
438
439         /* ICMP */
440         icmph->type             =       ICMP_DEST_UNREACH;
441         icmph->code             =       ICMP_FRAG_NEEDED;
442         icmph->un.gateway       =       htonl(mtu);
443         icmph->checksum         =       0;
444
445         nskb->csum = csum_partial((u8 *)icmph, sizeof(struct icmphdr), 0);
446         nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_iph - skb->data,
447                                             payload, payload_length,
448                                             nskb->csum);
449         icmph->checksum = csum_fold(nskb->csum);
450 }
451
452 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
453 static bool ipv6_should_icmp(struct sk_buff *skb)
454 {
455         struct ipv6hdr *old_ipv6h = ipv6_hdr(skb);
456         int addr_type;
457         int payload_off = (u8 *)(old_ipv6h + 1) - skb->data;
458         u8 nexthdr = ipv6_hdr(skb)->nexthdr;
459         __be16 frag_off;
460
461         /* Check source address is valid. */
462         addr_type = ipv6_addr_type(&old_ipv6h->saddr);
463         if (addr_type & IPV6_ADDR_MULTICAST || addr_type == IPV6_ADDR_ANY)
464                 return false;
465
466         /* Don't reply to unspecified addresses. */
467         if (ipv6_addr_type(&old_ipv6h->daddr) == IPV6_ADDR_ANY)
468                 return false;
469
470         /* Don't respond to ICMP error messages. */
471         payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr, &frag_off);
472         if (payload_off < 0)
473                 return false;
474
475         if (nexthdr == NEXTHDR_ICMP) {
476                 u8 icmp_type, *icmp_typep;
477
478                 icmp_typep = skb_header_pointer(skb, payload_off +
479                                                 offsetof(struct icmp6hdr,
480                                                         icmp6_type),
481                                                 sizeof(icmp_type), &icmp_type);
482
483                 if (!icmp_typep || !(*icmp_typep & ICMPV6_INFOMSG_MASK))
484                         return false;
485         }
486
487         return true;
488 }
489
490 static void ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
491                             unsigned int mtu, unsigned int payload_length)
492 {
493         struct ipv6hdr *ipv6h, *old_ipv6h = ipv6_hdr(skb);
494         struct icmp6hdr *icmp6h;
495         u8 *payload;
496
497         ipv6h = (struct ipv6hdr *)skb_put(nskb, sizeof(struct ipv6hdr));
498         icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
499         payload = skb_put(nskb, payload_length);
500
501         /* IPv6 */
502         ipv6h->version          =       6;
503         ipv6h->priority         =       0;
504         memset(&ipv6h->flow_lbl, 0, sizeof(ipv6h->flow_lbl));
505         ipv6h->payload_len      =       htons(sizeof(struct icmp6hdr)
506                                               + payload_length);
507         ipv6h->nexthdr          =       NEXTHDR_ICMP;
508         ipv6h->hop_limit        =       IPV6_DEFAULT_HOPLIMIT;
509         ipv6h->daddr            =       old_ipv6h->saddr;
510         ipv6h->saddr            =       old_ipv6h->daddr;
511
512         /* ICMPv6 */
513         icmp6h->icmp6_type      =       ICMPV6_PKT_TOOBIG;
514         icmp6h->icmp6_code      =       0;
515         icmp6h->icmp6_cksum     =       0;
516         icmp6h->icmp6_mtu       =       htonl(mtu);
517
518         nskb->csum = csum_partial((u8 *)icmp6h, sizeof(struct icmp6hdr), 0);
519         nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_ipv6h - skb->data,
520                                             payload, payload_length,
521                                             nskb->csum);
522         icmp6h->icmp6_cksum = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
523                                                 sizeof(struct icmp6hdr)
524                                                 + payload_length,
525                                                 ipv6h->nexthdr, nskb->csum);
526 }
527 #endif /* IPv6 */
528
529 bool ovs_tnl_frag_needed(struct vport *vport,
530                          const struct tnl_mutable_config *mutable,
531                          struct sk_buff *skb, unsigned int mtu)
532 {
533         unsigned int eth_hdr_len = ETH_HLEN;
534         unsigned int total_length = 0, header_length = 0, payload_length;
535         struct ethhdr *eh, *old_eh = eth_hdr(skb);
536         struct sk_buff *nskb;
537
538         /* Sanity check */
539         if (skb->protocol == htons(ETH_P_IP)) {
540                 if (mtu < IP_MIN_MTU)
541                         return false;
542
543                 if (!ipv4_should_icmp(skb))
544                         return true;
545         }
546 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
547         else if (skb->protocol == htons(ETH_P_IPV6)) {
548                 if (mtu < IPV6_MIN_MTU)
549                         return false;
550
551                 /*
552                  * In theory we should do PMTUD on IPv6 multicast messages but
553                  * we don't have an address to send from so just fragment.
554                  */
555                 if (ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST)
556                         return false;
557
558                 if (!ipv6_should_icmp(skb))
559                         return true;
560         }
561 #endif
562         else
563                 return false;
564
565         /* Allocate */
566         if (old_eh->h_proto == htons(ETH_P_8021Q))
567                 eth_hdr_len = VLAN_ETH_HLEN;
568
569         payload_length = skb->len - eth_hdr_len;
570         if (skb->protocol == htons(ETH_P_IP)) {
571                 header_length = sizeof(struct iphdr) + sizeof(struct icmphdr);
572                 total_length = min_t(unsigned int, header_length +
573                                                    payload_length, 576);
574         }
575 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
576         else {
577                 header_length = sizeof(struct ipv6hdr) +
578                                 sizeof(struct icmp6hdr);
579                 total_length = min_t(unsigned int, header_length +
580                                                   payload_length, IPV6_MIN_MTU);
581         }
582 #endif
583
584         payload_length = total_length - header_length;
585
586         nskb = dev_alloc_skb(NET_IP_ALIGN + eth_hdr_len + header_length +
587                              payload_length);
588         if (!nskb)
589                 return false;
590
591         skb_reserve(nskb, NET_IP_ALIGN);
592
593         /* Ethernet / VLAN */
594         eh = (struct ethhdr *)skb_put(nskb, eth_hdr_len);
595         memcpy(eh->h_dest, old_eh->h_source, ETH_ALEN);
596         memcpy(eh->h_source, mutable->eth_addr, ETH_ALEN);
597         nskb->protocol = eh->h_proto = old_eh->h_proto;
598         if (old_eh->h_proto == htons(ETH_P_8021Q)) {
599                 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)eh;
600
601                 vh->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
602                 vh->h_vlan_encapsulated_proto = skb->protocol;
603         } else
604                 vlan_set_tci(nskb, vlan_get_tci(skb));
605         skb_reset_mac_header(nskb);
606
607         /* Protocol */
608         if (skb->protocol == htons(ETH_P_IP))
609                 ipv4_build_icmp(skb, nskb, mtu, payload_length);
610 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
611         else
612                 ipv6_build_icmp(skb, nskb, mtu, payload_length);
613 #endif
614
615         if (unlikely(compute_ip_summed(nskb, false))) {
616                 kfree_skb(nskb);
617                 return false;
618         }
619
620         ovs_vport_receive(vport, nskb);
621
622         return true;
623 }
624
625 static bool check_mtu(struct sk_buff *skb,
626                       struct vport *vport,
627                       const struct tnl_mutable_config *mutable,
628                       const struct rtable *rt, __be16 *frag_offp,
629                       int tunnel_hlen)
630 {
631         bool df_inherit;
632         bool pmtud;
633         __be16 frag_off;
634         int mtu = 0;
635         unsigned int packet_length = skb->len - ETH_HLEN;
636
637         if (OVS_CB(skb)->tun_key->ipv4_dst) {
638                 df_inherit = false;
639                 pmtud = false;
640                 frag_off = OVS_CB(skb)->tun_key->tun_flags & OVS_TNL_F_DONT_FRAGMENT ?
641                                   htons(IP_DF) : 0;
642         } else {
643                 df_inherit = mutable->flags & TNL_F_DF_INHERIT;
644                 pmtud = mutable->flags & TNL_F_PMTUD;
645                 frag_off = mutable->flags & TNL_F_DF_DEFAULT ? htons(IP_DF) : 0;
646         }
647
648         /* Allow for one level of tagging in the packet length. */
649         if (!vlan_tx_tag_present(skb) &&
650             eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
651                 packet_length -= VLAN_HLEN;
652
653         if (pmtud) {
654                 int vlan_header = 0;
655
656                 /* The tag needs to go in packet regardless of where it
657                  * currently is, so subtract it from the MTU.
658                  */
659                 if (vlan_tx_tag_present(skb) ||
660                     eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
661                         vlan_header = VLAN_HLEN;
662
663                 mtu = dst_mtu(&rt_dst(rt))
664                         - ETH_HLEN
665                         - tunnel_hlen
666                         - vlan_header;
667         }
668
669         if (skb->protocol == htons(ETH_P_IP)) {
670                 struct iphdr *iph = ip_hdr(skb);
671
672                 if (df_inherit)
673                         frag_off = iph->frag_off & htons(IP_DF);
674
675                 if (pmtud && iph->frag_off & htons(IP_DF)) {
676                         mtu = max(mtu, IP_MIN_MTU);
677
678                         if (packet_length > mtu &&
679                             ovs_tnl_frag_needed(vport, mutable, skb, mtu))
680                                 return false;
681                 }
682         }
683 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
684         else if (skb->protocol == htons(ETH_P_IPV6)) {
685                 /* IPv6 requires end hosts to do fragmentation
686                  * if the packet is above the minimum MTU.
687                  */
688                 if (df_inherit && packet_length > IPV6_MIN_MTU)
689                         frag_off = htons(IP_DF);
690
691                 if (pmtud) {
692                         mtu = max(mtu, IPV6_MIN_MTU);
693
694                         if (packet_length > mtu &&
695                             ovs_tnl_frag_needed(vport, mutable, skb, mtu))
696                                 return false;
697                 }
698         }
699 #endif
700
701         *frag_offp = frag_off;
702         return true;
703 }
704
705 static struct rtable *find_route(struct net *net,
706                 __be32 *saddr, __be32 daddr, u8 ipproto,
707                 u8 tos)
708 {
709         struct rtable *rt;
710         /* Tunnel configuration keeps DSCP part of TOS bits, But Linux
711          * router expect RT_TOS bits only. */
712
713 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
714         struct flowi fl = { .nl_u = { .ip4_u = {
715                                         .daddr = daddr,
716                                         .saddr = *saddr,
717                                         .tos   = RT_TOS(tos) } },
718                                         .proto = ipproto };
719
720         if (unlikely(ip_route_output_key(net, &rt, &fl)))
721                 return ERR_PTR(-EADDRNOTAVAIL);
722         *saddr = fl.nl_u.ip4_u.saddr;
723         return rt;
724 #else
725         struct flowi4 fl = { .daddr = daddr,
726                              .saddr = *saddr,
727                              .flowi4_tos = RT_TOS(tos),
728                              .flowi4_proto = ipproto };
729
730         rt = ip_route_output_key(net, &fl);
731         *saddr = fl.saddr;
732         return rt;
733 #endif
734 }
735
736 static bool need_linearize(const struct sk_buff *skb)
737 {
738         int i;
739
740         if (unlikely(skb_shinfo(skb)->frag_list))
741                 return true;
742
743         /*
744          * Generally speaking we should linearize if there are paged frags.
745          * However, if all of the refcounts are 1 we know nobody else can
746          * change them from underneath us and we can skip the linearization.
747          */
748         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
749                 if (unlikely(page_count(skb_frag_page(&skb_shinfo(skb)->frags[i])) > 1))
750                         return true;
751
752         return false;
753 }
754
755 static struct sk_buff *handle_offloads(struct sk_buff *skb,
756                                        const struct tnl_mutable_config *mutable,
757                                        const struct rtable *rt,
758                                        int tunnel_hlen)
759 {
760         int min_headroom;
761         int err;
762
763         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
764                         + tunnel_hlen
765                         + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
766
767         if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
768                 int head_delta = SKB_DATA_ALIGN(min_headroom -
769                                                 skb_headroom(skb) +
770                                                 16);
771                 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
772                                         0, GFP_ATOMIC);
773                 if (unlikely(err))
774                         goto error_free;
775         }
776
777         forward_ip_summed(skb, true);
778
779         if (skb_is_gso(skb)) {
780                 struct sk_buff *nskb;
781
782                 nskb = skb_gso_segment(skb, 0);
783                 if (IS_ERR(nskb)) {
784                         kfree_skb(skb);
785                         err = PTR_ERR(nskb);
786                         goto error;
787                 }
788
789                 consume_skb(skb);
790                 skb = nskb;
791         } else if (get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
792                 /* Pages aren't locked and could change at any time.
793                  * If this happens after we compute the checksum, the
794                  * checksum will be wrong.  We linearize now to avoid
795                  * this problem.
796                  */
797                 if (unlikely(need_linearize(skb))) {
798                         err = __skb_linearize(skb);
799                         if (unlikely(err))
800                                 goto error_free;
801                 }
802
803                 err = skb_checksum_help(skb);
804                 if (unlikely(err))
805                         goto error_free;
806         }
807
808         set_ip_summed(skb, OVS_CSUM_NONE);
809
810         return skb;
811
812 error_free:
813         kfree_skb(skb);
814 error:
815         return ERR_PTR(err);
816 }
817
818 static int send_frags(struct sk_buff *skb,
819                       int tunnel_hlen)
820 {
821         int sent_len;
822
823         sent_len = 0;
824         while (skb) {
825                 struct sk_buff *next = skb->next;
826                 int frag_len = skb->len - tunnel_hlen;
827                 int err;
828
829                 skb->next = NULL;
830                 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
831
832                 err = ip_local_out(skb);
833                 skb = next;
834                 if (unlikely(net_xmit_eval(err)))
835                         goto free_frags;
836                 sent_len += frag_len;
837         }
838
839         return sent_len;
840
841 free_frags:
842         /*
843          * There's no point in continuing to send fragments once one has been
844          * dropped so just free the rest.  This may help improve the congestion
845          * that caused the first packet to be dropped.
846          */
847         ovs_tnl_free_linked_skbs(skb);
848         return sent_len;
849 }
850
851 int ovs_tnl_send(struct vport *vport, struct sk_buff *skb)
852 {
853         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
854         const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
855         enum vport_err_type err = VPORT_E_TX_ERROR;
856         struct rtable *rt;
857         struct ovs_key_ipv4_tunnel tun_key;
858         int sent_len = 0;
859         int tunnel_hlen;
860         __be16 frag_off = 0;
861         __be32 daddr;
862         __be32 saddr;
863         u8 ttl;
864         u8 tos;
865
866         /* Validate the protocol headers before we try to use them. */
867         if (skb->protocol == htons(ETH_P_8021Q) &&
868             !vlan_tx_tag_present(skb)) {
869                 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
870                         goto error_free;
871
872                 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
873                 skb_set_network_header(skb, VLAN_ETH_HLEN);
874         }
875
876         if (skb->protocol == htons(ETH_P_IP)) {
877                 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
878                     + sizeof(struct iphdr))))
879                         skb->protocol = 0;
880         }
881 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
882         else if (skb->protocol == htons(ETH_P_IPV6)) {
883                 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
884                     + sizeof(struct ipv6hdr))))
885                         skb->protocol = 0;
886         }
887 #endif
888
889         /* If OVS_CB(skb)->tun_key is NULL, point it at the local tun_key here,
890          * and zero it out.
891          */
892         if (!OVS_CB(skb)->tun_key) {
893                 memset(&tun_key, 0, sizeof(tun_key));
894                 OVS_CB(skb)->tun_key = &tun_key;
895         }
896
897         tunnel_hlen = tnl_vport->tnl_ops->hdr_len(mutable, OVS_CB(skb)->tun_key);
898         if (unlikely(tunnel_hlen < 0)) {
899                 err = VPORT_E_TX_DROPPED;
900                 goto error_free;
901         }
902         tunnel_hlen += sizeof(struct iphdr);
903
904         if (OVS_CB(skb)->tun_key->ipv4_dst) {
905                 daddr = OVS_CB(skb)->tun_key->ipv4_dst;
906                 saddr = OVS_CB(skb)->tun_key->ipv4_src;
907                 tos = OVS_CB(skb)->tun_key->ipv4_tos;
908                 ttl = OVS_CB(skb)->tun_key->ipv4_ttl;
909         } else {
910                 u8 inner_tos;
911                 daddr = mutable->key.daddr;
912                 saddr = mutable->key.saddr;
913
914                 if (unlikely(!daddr)) {
915                         /* Trying to sent packet from Null-port without
916                          * tunnel info? Drop this packet. */
917                         err = VPORT_E_TX_DROPPED;
918                         goto error_free;
919                 }
920
921                 /* ToS */
922                 if (skb->protocol == htons(ETH_P_IP))
923                         inner_tos = ip_hdr(skb)->tos;
924 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
925                 else if (skb->protocol == htons(ETH_P_IPV6))
926                         inner_tos = ipv6_get_dsfield(ipv6_hdr(skb));
927 #endif
928                 else
929                         inner_tos = 0;
930
931                 if (mutable->flags & TNL_F_TOS_INHERIT)
932                         tos = inner_tos;
933                 else
934                         tos = mutable->tos;
935
936                 tos = INET_ECN_encapsulate(tos, inner_tos);
937
938                 /* TTL */
939                 ttl = mutable->ttl;
940                 if (mutable->flags & TNL_F_TTL_INHERIT) {
941                         if (skb->protocol == htons(ETH_P_IP))
942                                 ttl = ip_hdr(skb)->ttl;
943 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
944                         else if (skb->protocol == htons(ETH_P_IPV6))
945                                 ttl = ipv6_hdr(skb)->hop_limit;
946 #endif
947                 }
948
949         }
950
951         /* Route lookup */
952         rt = find_route(port_key_get_net(&mutable->key), &saddr, daddr,
953                           tnl_vport->tnl_ops->ipproto, tos);
954         if (IS_ERR(rt))
955                 goto error_free;
956
957         /* Reset SKB */
958         nf_reset(skb);
959         secpath_reset(skb);
960         skb_dst_drop(skb);
961         skb_clear_rxhash(skb);
962
963         /* Offloading */
964         skb = handle_offloads(skb, mutable, rt, tunnel_hlen);
965         if (IS_ERR(skb)) {
966                 skb = NULL;
967                 goto err_free_rt;
968         }
969
970         /* MTU */
971         if (unlikely(!check_mtu(skb, vport, mutable, rt, &frag_off, tunnel_hlen))) {
972                 err = VPORT_E_TX_DROPPED;
973                 goto err_free_rt;
974         }
975
976         /* TTL Fixup. */
977         if (!OVS_CB(skb)->tun_key->ipv4_dst) {
978                 if (!(mutable->flags & TNL_F_TTL_INHERIT)) {
979                         if (!ttl)
980                                 ttl = ip4_dst_hoplimit(&rt_dst(rt));
981                 }
982         }
983
984         while (skb) {
985                 struct iphdr *iph;
986                 struct sk_buff *next_skb = skb->next;
987                 skb->next = NULL;
988
989                 if (unlikely(vlan_deaccel_tag(skb)))
990                         goto next;
991
992                 skb_push(skb, tunnel_hlen);
993                 skb_reset_network_header(skb);
994                 skb_set_transport_header(skb, sizeof(struct iphdr));
995
996                 if (next_skb)
997                         skb_dst_set(skb, dst_clone(&rt_dst(rt)));
998                 else
999                         skb_dst_set(skb, &rt_dst(rt));
1000
1001                 /* Push IP header. */
1002                 iph = ip_hdr(skb);
1003                 iph->version    = 4;
1004                 iph->ihl        = sizeof(struct iphdr) >> 2;
1005                 iph->protocol   = tnl_vport->tnl_ops->ipproto;
1006                 iph->daddr      = daddr;
1007                 iph->saddr      = saddr;
1008                 iph->tos        = tos;
1009                 iph->ttl        = ttl;
1010                 iph->frag_off   = frag_off;
1011                 ip_select_ident(iph, &rt_dst(rt), NULL);
1012
1013                 /* Push Tunnel header. */
1014                 skb = tnl_vport->tnl_ops->build_header(vport, mutable,
1015                                                         &rt_dst(rt), skb, tunnel_hlen);
1016                 if (unlikely(!skb))
1017                         goto next;
1018
1019                 sent_len += send_frags(skb, tunnel_hlen);
1020
1021 next:
1022                 skb = next_skb;
1023         }
1024
1025         if (unlikely(sent_len == 0))
1026                 ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
1027
1028         return sent_len;
1029
1030 err_free_rt:
1031         ip_rt_put(rt);
1032 error_free:
1033         ovs_tnl_free_linked_skbs(skb);
1034         ovs_vport_record_error(vport, err);
1035         return sent_len;
1036 }
1037
1038 static const struct nla_policy tnl_policy[OVS_TUNNEL_ATTR_MAX + 1] = {
1039         [OVS_TUNNEL_ATTR_FLAGS]    = { .type = NLA_U32 },
1040         [OVS_TUNNEL_ATTR_DST_IPV4] = { .type = NLA_U32 },
1041         [OVS_TUNNEL_ATTR_SRC_IPV4] = { .type = NLA_U32 },
1042         [OVS_TUNNEL_ATTR_OUT_KEY]  = { .type = NLA_U64 },
1043         [OVS_TUNNEL_ATTR_IN_KEY]   = { .type = NLA_U64 },
1044         [OVS_TUNNEL_ATTR_TOS]      = { .type = NLA_U8 },
1045         [OVS_TUNNEL_ATTR_TTL]      = { .type = NLA_U8 },
1046         [OVS_TUNNEL_ATTR_DST_PORT] = { .type = NLA_U16 },
1047 };
1048
1049 /* Sets OVS_TUNNEL_ATTR_* fields in 'mutable', which must initially be
1050  * zeroed. */
1051 static int tnl_set_config(struct net *net, struct nlattr *options,
1052                           const struct tnl_ops *tnl_ops,
1053                           const struct vport *cur_vport,
1054                           struct tnl_mutable_config *mutable)
1055 {
1056         const struct vport *old_vport;
1057         const struct tnl_mutable_config *old_mutable;
1058         struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
1059         int err;
1060
1061         port_key_set_net(&mutable->key, net);
1062         mutable->key.tunnel_type = tnl_ops->tunnel_type;
1063         if (!options)
1064                 goto out;
1065
1066         err = nla_parse_nested(a, OVS_TUNNEL_ATTR_MAX, options, tnl_policy);
1067         if (err)
1068                 return err;
1069
1070         if (!a[OVS_TUNNEL_ATTR_FLAGS] || !a[OVS_TUNNEL_ATTR_DST_IPV4])
1071                 return -EINVAL;
1072
1073         mutable->flags = nla_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]) & TNL_F_PUBLIC;
1074         mutable->key.daddr = nla_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
1075
1076         if (a[OVS_TUNNEL_ATTR_SRC_IPV4]) {
1077                 if (ipv4_is_multicast(mutable->key.daddr))
1078                         return -EINVAL;
1079                 mutable->key.saddr = nla_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
1080         }
1081
1082         if (a[OVS_TUNNEL_ATTR_TOS]) {
1083                 mutable->tos = nla_get_u8(a[OVS_TUNNEL_ATTR_TOS]);
1084                 /* Reject ToS config with ECN bits set. */
1085                 if (mutable->tos & INET_ECN_MASK)
1086                         return -EINVAL;
1087         }
1088
1089         if (a[OVS_TUNNEL_ATTR_TTL])
1090                 mutable->ttl = nla_get_u8(a[OVS_TUNNEL_ATTR_TTL]);
1091
1092         if (a[OVS_TUNNEL_ATTR_DST_PORT])
1093                 mutable->dst_port =
1094                         htons(nla_get_u16(a[OVS_TUNNEL_ATTR_DST_PORT]));
1095
1096         if (!a[OVS_TUNNEL_ATTR_IN_KEY]) {
1097                 mutable->key.tunnel_type |= TNL_T_KEY_MATCH;
1098                 mutable->flags |= TNL_F_IN_KEY_MATCH;
1099         } else {
1100                 mutable->key.tunnel_type |= TNL_T_KEY_EXACT;
1101                 mutable->key.in_key = nla_get_be64(a[OVS_TUNNEL_ATTR_IN_KEY]);
1102         }
1103
1104         if (!a[OVS_TUNNEL_ATTR_OUT_KEY])
1105                 mutable->flags |= TNL_F_OUT_KEY_ACTION;
1106         else
1107                 mutable->out_key = nla_get_be64(a[OVS_TUNNEL_ATTR_OUT_KEY]);
1108
1109         mutable->mlink = 0;
1110         if (ipv4_is_multicast(mutable->key.daddr)) {
1111                 struct net_device *dev;
1112                 struct rtable *rt;
1113                 __be32 saddr = mutable->key.saddr;
1114
1115                 rt = find_route(port_key_get_net(&mutable->key),
1116                              &saddr, mutable->key.daddr,
1117                              tnl_ops->ipproto, mutable->tos);
1118                 if (IS_ERR(rt))
1119                         return -EADDRNOTAVAIL;
1120                 dev = rt_dst(rt).dev;
1121                 ip_rt_put(rt);
1122                 if (__in_dev_get_rtnl(dev) == NULL)
1123                         return -EADDRNOTAVAIL;
1124                 mutable->mlink = dev->ifindex;
1125                 ip_mc_inc_group(__in_dev_get_rtnl(dev), mutable->key.daddr);
1126         }
1127
1128 out:
1129         old_vport = port_table_lookup(&mutable->key, &old_mutable);
1130         if (old_vport && old_vport != cur_vport)
1131                 return -EEXIST;
1132
1133         return 0;
1134 }
1135
1136 struct vport *ovs_tnl_create(const struct vport_parms *parms,
1137                              const struct vport_ops *vport_ops,
1138                              const struct tnl_ops *tnl_ops)
1139 {
1140         struct vport *vport;
1141         struct tnl_vport *tnl_vport;
1142         struct tnl_mutable_config *mutable;
1143         int initial_frag_id;
1144         int err;
1145
1146         vport = ovs_vport_alloc(sizeof(struct tnl_vport), vport_ops, parms);
1147         if (IS_ERR(vport)) {
1148                 err = PTR_ERR(vport);
1149                 goto error;
1150         }
1151
1152         tnl_vport = tnl_vport_priv(vport);
1153
1154         strcpy(tnl_vport->name, parms->name);
1155         tnl_vport->tnl_ops = tnl_ops;
1156
1157         mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1158         if (!mutable) {
1159                 err = -ENOMEM;
1160                 goto error_free_vport;
1161         }
1162
1163         random_ether_addr(mutable->eth_addr);
1164
1165         get_random_bytes(&initial_frag_id, sizeof(int));
1166         atomic_set(&tnl_vport->frag_id, initial_frag_id);
1167
1168         err = tnl_set_config(ovs_dp_get_net(parms->dp), parms->options, tnl_ops,
1169                              NULL, mutable);
1170         if (err)
1171                 goto error_free_mutable;
1172
1173         rcu_assign_pointer(tnl_vport->mutable, mutable);
1174
1175         port_table_add_port(vport);
1176         return vport;
1177
1178 error_free_mutable:
1179         free_mutable_rtnl(mutable);
1180         kfree(mutable);
1181 error_free_vport:
1182         ovs_vport_free(vport);
1183 error:
1184         return ERR_PTR(err);
1185 }
1186
1187 int ovs_tnl_set_options(struct vport *vport, struct nlattr *options)
1188 {
1189         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1190         const struct tnl_mutable_config *old_mutable;
1191         struct tnl_mutable_config *mutable;
1192         int err;
1193
1194         old_mutable = rtnl_dereference(tnl_vport->mutable);
1195         if (!old_mutable->key.daddr)
1196                 return -EINVAL;
1197
1198         mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1199         if (!mutable) {
1200                 err = -ENOMEM;
1201                 goto error;
1202         }
1203
1204         /* Copy fields whose values should be retained. */
1205         mutable->seq = old_mutable->seq + 1;
1206         memcpy(mutable->eth_addr, old_mutable->eth_addr, ETH_ALEN);
1207
1208         /* Parse the others configured by userspace. */
1209         err = tnl_set_config(ovs_dp_get_net(vport->dp), options, tnl_vport->tnl_ops,
1210                              vport, mutable);
1211         if (err)
1212                 goto error_free;
1213
1214         if (port_hash(&mutable->key) != port_hash(&old_mutable->key))
1215                 port_table_move_port(vport, mutable);
1216         else
1217                 assign_config_rcu(vport, mutable);
1218
1219         return 0;
1220
1221 error_free:
1222         free_mutable_rtnl(mutable);
1223         kfree(mutable);
1224 error:
1225         return err;
1226 }
1227
1228 int ovs_tnl_get_options(const struct vport *vport, struct sk_buff *skb)
1229 {
1230         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1231         const struct tnl_mutable_config *mutable = rcu_dereference_rtnl(tnl_vport->mutable);
1232
1233         if (nla_put_u32(skb, OVS_TUNNEL_ATTR_FLAGS,
1234                       mutable->flags & TNL_F_PUBLIC) ||
1235             nla_put_be32(skb, OVS_TUNNEL_ATTR_DST_IPV4, mutable->key.daddr))
1236                 goto nla_put_failure;
1237
1238         if (!(mutable->flags & TNL_F_IN_KEY_MATCH) &&
1239             nla_put_be64(skb, OVS_TUNNEL_ATTR_IN_KEY, mutable->key.in_key))
1240                 goto nla_put_failure;
1241         if (!(mutable->flags & TNL_F_OUT_KEY_ACTION) &&
1242             nla_put_be64(skb, OVS_TUNNEL_ATTR_OUT_KEY, mutable->out_key))
1243                 goto nla_put_failure;
1244         if (mutable->key.saddr &&
1245             nla_put_be32(skb, OVS_TUNNEL_ATTR_SRC_IPV4, mutable->key.saddr))
1246                 goto nla_put_failure;
1247         if (mutable->tos && nla_put_u8(skb, OVS_TUNNEL_ATTR_TOS, mutable->tos))
1248                 goto nla_put_failure;
1249         if (mutable->ttl && nla_put_u8(skb, OVS_TUNNEL_ATTR_TTL, mutable->ttl))
1250                 goto nla_put_failure;
1251         if (mutable->dst_port && nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT,
1252                                              ntohs(mutable->dst_port)))
1253                 goto nla_put_failure;
1254
1255         return 0;
1256
1257 nla_put_failure:
1258         return -EMSGSIZE;
1259 }
1260
1261 static void free_port_rcu(struct rcu_head *rcu)
1262 {
1263         struct tnl_vport *tnl_vport = container_of(rcu,
1264                                                    struct tnl_vport, rcu);
1265
1266         kfree((struct tnl_mutable __force *)tnl_vport->mutable);
1267         ovs_vport_free(tnl_vport_to_vport(tnl_vport));
1268 }
1269
1270 void ovs_tnl_destroy(struct vport *vport)
1271 {
1272         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1273         struct tnl_mutable_config *mutable;
1274
1275         mutable = rtnl_dereference(tnl_vport->mutable);
1276         port_table_remove_port(vport);
1277         free_mutable_rtnl(mutable);
1278         call_rcu(&tnl_vport->rcu, free_port_rcu);
1279 }
1280
1281 int ovs_tnl_set_addr(struct vport *vport, const unsigned char *addr)
1282 {
1283         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1284         struct tnl_mutable_config *old_mutable, *mutable;
1285
1286         old_mutable = rtnl_dereference(tnl_vport->mutable);
1287         mutable = kmemdup(old_mutable, sizeof(struct tnl_mutable_config), GFP_KERNEL);
1288         if (!mutable)
1289                 return -ENOMEM;
1290
1291         old_mutable->mlink = 0;
1292
1293         memcpy(mutable->eth_addr, addr, ETH_ALEN);
1294         assign_config_rcu(vport, mutable);
1295
1296         return 0;
1297 }
1298
1299 const char *ovs_tnl_get_name(const struct vport *vport)
1300 {
1301         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1302         return tnl_vport->name;
1303 }
1304
1305 const unsigned char *ovs_tnl_get_addr(const struct vport *vport)
1306 {
1307         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1308         return rcu_dereference_rtnl(tnl_vport->mutable)->eth_addr;
1309 }
1310
1311 void ovs_tnl_free_linked_skbs(struct sk_buff *skb)
1312 {
1313         while (skb) {
1314                 struct sk_buff *next = skb->next;
1315                 kfree_skb(skb);
1316                 skb = next;
1317         }
1318 }
1319
1320 int ovs_tnl_init(void)
1321 {
1322         int i;
1323
1324         port_table = kmalloc(PORT_TABLE_SIZE * sizeof(struct hlist_head *),
1325                              GFP_KERNEL);
1326         if (!port_table)
1327                 return -ENOMEM;
1328
1329         for (i = 0; i < PORT_TABLE_SIZE; i++)
1330                 INIT_HLIST_HEAD(&port_table[i]);
1331
1332         return 0;
1333 }
1334
1335 void ovs_tnl_exit(void)
1336 {
1337         kfree(port_table);
1338 }