fb4854af83bf261bc5c8ec60b13cce444cfd2d74
[cascardo/ovs.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.tunnel_type = tunnel_type;
277                 vport = port_table_lookup(&lookup, mutable);
278                 if (vport)
279                         return vport;
280         }
281         return NULL;
282 }
283
284 static void ecn_decapsulate(struct sk_buff *skb)
285 {
286         if (unlikely(INET_ECN_is_ce(OVS_CB(skb)->tun_key->ipv4_tos))) {
287                 __be16 protocol = skb->protocol;
288
289                 skb_set_network_header(skb, ETH_HLEN);
290
291                 if (protocol == htons(ETH_P_8021Q)) {
292                         if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
293                                 return;
294
295                         protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
296                         skb_set_network_header(skb, VLAN_ETH_HLEN);
297                 }
298
299                 if (protocol == htons(ETH_P_IP)) {
300                         if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
301                             + sizeof(struct iphdr))))
302                                 return;
303
304                         IP_ECN_set_ce(ip_hdr(skb));
305                 }
306 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
307                 else if (protocol == htons(ETH_P_IPV6)) {
308                         if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
309                             + sizeof(struct ipv6hdr))))
310                                 return;
311
312                         IP6_ECN_set_ce(ipv6_hdr(skb));
313                 }
314 #endif
315         }
316 }
317
318 /**
319  *      ovs_tnl_rcv - ingress point for generic tunnel code
320  *
321  * @vport: port this packet was received on
322  * @skb: received packet
323  * @tos: ToS from encapsulating IP packet, used to copy ECN bits
324  *
325  * Must be called with rcu_read_lock.
326  *
327  * Packets received by this function are in the following state:
328  * - skb->data points to the inner Ethernet header.
329  * - The inner Ethernet header is in the linear data area.
330  * - skb->csum does not include the inner Ethernet header.
331  * - The layer pointers are undefined.
332  */
333 void ovs_tnl_rcv(struct vport *vport, struct sk_buff *skb)
334 {
335         struct ethhdr *eh;
336
337         skb_reset_mac_header(skb);
338         eh = eth_hdr(skb);
339
340         if (likely(ntohs(eh->h_proto) >= 1536))
341                 skb->protocol = eh->h_proto;
342         else
343                 skb->protocol = htons(ETH_P_802_2);
344
345         skb_dst_drop(skb);
346         nf_reset(skb);
347         skb_clear_rxhash(skb);
348         secpath_reset(skb);
349
350         ecn_decapsulate(skb);
351         vlan_set_tci(skb, 0);
352
353         if (unlikely(compute_ip_summed(skb, false))) {
354                 kfree_skb(skb);
355                 return;
356         }
357
358         ovs_vport_receive(vport, skb);
359 }
360
361 static bool check_ipv4_address(__be32 addr)
362 {
363         if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr)
364             || ipv4_is_loopback(addr) || ipv4_is_zeronet(addr))
365                 return false;
366
367         return true;
368 }
369
370 static bool ipv4_should_icmp(struct sk_buff *skb)
371 {
372         struct iphdr *old_iph = ip_hdr(skb);
373
374         /* Don't respond to L2 broadcast. */
375         if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
376                 return false;
377
378         /* Don't respond to L3 broadcast or invalid addresses. */
379         if (!check_ipv4_address(old_iph->daddr) ||
380             !check_ipv4_address(old_iph->saddr))
381                 return false;
382
383         /* Only respond to the first fragment. */
384         if (old_iph->frag_off & htons(IP_OFFSET))
385                 return false;
386
387         /* Don't respond to ICMP error messages. */
388         if (old_iph->protocol == IPPROTO_ICMP) {
389                 u8 icmp_type, *icmp_typep;
390
391                 icmp_typep = skb_header_pointer(skb, (u8 *)old_iph +
392                                                 (old_iph->ihl << 2) +
393                                                 offsetof(struct icmphdr, type) -
394                                                 skb->data, sizeof(icmp_type),
395                                                 &icmp_type);
396
397                 if (!icmp_typep)
398                         return false;
399
400                 if (*icmp_typep > NR_ICMP_TYPES
401                         || (*icmp_typep <= ICMP_PARAMETERPROB
402                                 && *icmp_typep != ICMP_ECHOREPLY
403                                 && *icmp_typep != ICMP_ECHO))
404                         return false;
405         }
406
407         return true;
408 }
409
410 static void ipv4_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
411                             unsigned int mtu, unsigned int payload_length)
412 {
413         struct iphdr *iph, *old_iph = ip_hdr(skb);
414         struct icmphdr *icmph;
415         u8 *payload;
416
417         iph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
418         icmph = (struct icmphdr *)skb_put(nskb, sizeof(struct icmphdr));
419         payload = skb_put(nskb, payload_length);
420
421         /* IP */
422         iph->version            =       4;
423         iph->ihl                =       sizeof(struct iphdr) >> 2;
424         iph->tos                =       (old_iph->tos & IPTOS_TOS_MASK) |
425                                         IPTOS_PREC_INTERNETCONTROL;
426         iph->tot_len            =       htons(sizeof(struct iphdr)
427                                               + sizeof(struct icmphdr)
428                                               + payload_length);
429         get_random_bytes(&iph->id, sizeof(iph->id));
430         iph->frag_off           =       0;
431         iph->ttl                =       IPDEFTTL;
432         iph->protocol           =       IPPROTO_ICMP;
433         iph->daddr              =       old_iph->saddr;
434         iph->saddr              =       old_iph->daddr;
435
436         ip_send_check(iph);
437
438         /* ICMP */
439         icmph->type             =       ICMP_DEST_UNREACH;
440         icmph->code             =       ICMP_FRAG_NEEDED;
441         icmph->un.gateway       =       htonl(mtu);
442         icmph->checksum         =       0;
443
444         nskb->csum = csum_partial((u8 *)icmph, sizeof(struct icmphdr), 0);
445         nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_iph - skb->data,
446                                             payload, payload_length,
447                                             nskb->csum);
448         icmph->checksum = csum_fold(nskb->csum);
449 }
450
451 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
452 static bool ipv6_should_icmp(struct sk_buff *skb)
453 {
454         struct ipv6hdr *old_ipv6h = ipv6_hdr(skb);
455         int addr_type;
456         int payload_off = (u8 *)(old_ipv6h + 1) - skb->data;
457         u8 nexthdr = ipv6_hdr(skb)->nexthdr;
458         __be16 frag_off;
459
460         /* Check source address is valid. */
461         addr_type = ipv6_addr_type(&old_ipv6h->saddr);
462         if (addr_type & IPV6_ADDR_MULTICAST || addr_type == IPV6_ADDR_ANY)
463                 return false;
464
465         /* Don't reply to unspecified addresses. */
466         if (ipv6_addr_type(&old_ipv6h->daddr) == IPV6_ADDR_ANY)
467                 return false;
468
469         /* Don't respond to ICMP error messages. */
470         payload_off = ipv6_skip_exthdr(skb, payload_off, &nexthdr, &frag_off);
471         if (payload_off < 0)
472                 return false;
473
474         if (nexthdr == NEXTHDR_ICMP) {
475                 u8 icmp_type, *icmp_typep;
476
477                 icmp_typep = skb_header_pointer(skb, payload_off +
478                                                 offsetof(struct icmp6hdr,
479                                                         icmp6_type),
480                                                 sizeof(icmp_type), &icmp_type);
481
482                 if (!icmp_typep || !(*icmp_typep & ICMPV6_INFOMSG_MASK))
483                         return false;
484         }
485
486         return true;
487 }
488
489 static void ipv6_build_icmp(struct sk_buff *skb, struct sk_buff *nskb,
490                             unsigned int mtu, unsigned int payload_length)
491 {
492         struct ipv6hdr *ipv6h, *old_ipv6h = ipv6_hdr(skb);
493         struct icmp6hdr *icmp6h;
494         u8 *payload;
495
496         ipv6h = (struct ipv6hdr *)skb_put(nskb, sizeof(struct ipv6hdr));
497         icmp6h = (struct icmp6hdr *)skb_put(nskb, sizeof(struct icmp6hdr));
498         payload = skb_put(nskb, payload_length);
499
500         /* IPv6 */
501         ipv6h->version          =       6;
502         ipv6h->priority         =       0;
503         memset(&ipv6h->flow_lbl, 0, sizeof(ipv6h->flow_lbl));
504         ipv6h->payload_len      =       htons(sizeof(struct icmp6hdr)
505                                               + payload_length);
506         ipv6h->nexthdr          =       NEXTHDR_ICMP;
507         ipv6h->hop_limit        =       IPV6_DEFAULT_HOPLIMIT;
508         ipv6h->daddr            =       old_ipv6h->saddr;
509         ipv6h->saddr            =       old_ipv6h->daddr;
510
511         /* ICMPv6 */
512         icmp6h->icmp6_type      =       ICMPV6_PKT_TOOBIG;
513         icmp6h->icmp6_code      =       0;
514         icmp6h->icmp6_cksum     =       0;
515         icmp6h->icmp6_mtu       =       htonl(mtu);
516
517         nskb->csum = csum_partial((u8 *)icmp6h, sizeof(struct icmp6hdr), 0);
518         nskb->csum = skb_copy_and_csum_bits(skb, (u8 *)old_ipv6h - skb->data,
519                                             payload, payload_length,
520                                             nskb->csum);
521         icmp6h->icmp6_cksum = csum_ipv6_magic(&ipv6h->saddr, &ipv6h->daddr,
522                                                 sizeof(struct icmp6hdr)
523                                                 + payload_length,
524                                                 ipv6h->nexthdr, nskb->csum);
525 }
526 #endif /* IPv6 */
527
528 bool ovs_tnl_frag_needed(struct vport *vport,
529                          const struct tnl_mutable_config *mutable,
530                          struct sk_buff *skb, unsigned int mtu)
531 {
532         unsigned int eth_hdr_len = ETH_HLEN;
533         unsigned int total_length = 0, header_length = 0, payload_length;
534         struct ethhdr *eh, *old_eh = eth_hdr(skb);
535         struct sk_buff *nskb;
536
537         /* Sanity check */
538         if (skb->protocol == htons(ETH_P_IP)) {
539                 if (mtu < IP_MIN_MTU)
540                         return false;
541
542                 if (!ipv4_should_icmp(skb))
543                         return true;
544         }
545 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
546         else if (skb->protocol == htons(ETH_P_IPV6)) {
547                 if (mtu < IPV6_MIN_MTU)
548                         return false;
549
550                 /*
551                  * In theory we should do PMTUD on IPv6 multicast messages but
552                  * we don't have an address to send from so just fragment.
553                  */
554                 if (ipv6_addr_type(&ipv6_hdr(skb)->daddr) & IPV6_ADDR_MULTICAST)
555                         return false;
556
557                 if (!ipv6_should_icmp(skb))
558                         return true;
559         }
560 #endif
561         else
562                 return false;
563
564         /* Allocate */
565         if (old_eh->h_proto == htons(ETH_P_8021Q))
566                 eth_hdr_len = VLAN_ETH_HLEN;
567
568         payload_length = skb->len - eth_hdr_len;
569         if (skb->protocol == htons(ETH_P_IP)) {
570                 header_length = sizeof(struct iphdr) + sizeof(struct icmphdr);
571                 total_length = min_t(unsigned int, header_length +
572                                                    payload_length, 576);
573         }
574 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
575         else {
576                 header_length = sizeof(struct ipv6hdr) +
577                                 sizeof(struct icmp6hdr);
578                 total_length = min_t(unsigned int, header_length +
579                                                   payload_length, IPV6_MIN_MTU);
580         }
581 #endif
582
583         payload_length = total_length - header_length;
584
585         nskb = dev_alloc_skb(NET_IP_ALIGN + eth_hdr_len + header_length +
586                              payload_length);
587         if (!nskb)
588                 return false;
589
590         skb_reserve(nskb, NET_IP_ALIGN);
591
592         /* Ethernet / VLAN */
593         eh = (struct ethhdr *)skb_put(nskb, eth_hdr_len);
594         memcpy(eh->h_dest, old_eh->h_source, ETH_ALEN);
595         memcpy(eh->h_source, mutable->eth_addr, ETH_ALEN);
596         nskb->protocol = eh->h_proto = old_eh->h_proto;
597         if (old_eh->h_proto == htons(ETH_P_8021Q)) {
598                 struct vlan_ethhdr *vh = (struct vlan_ethhdr *)eh;
599
600                 vh->h_vlan_TCI = vlan_eth_hdr(skb)->h_vlan_TCI;
601                 vh->h_vlan_encapsulated_proto = skb->protocol;
602         } else
603                 vlan_set_tci(nskb, vlan_get_tci(skb));
604         skb_reset_mac_header(nskb);
605
606         /* Protocol */
607         if (skb->protocol == htons(ETH_P_IP))
608                 ipv4_build_icmp(skb, nskb, mtu, payload_length);
609 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
610         else
611                 ipv6_build_icmp(skb, nskb, mtu, payload_length);
612 #endif
613
614         if (unlikely(compute_ip_summed(nskb, false))) {
615                 kfree_skb(nskb);
616                 return false;
617         }
618
619         ovs_vport_receive(vport, nskb);
620
621         return true;
622 }
623
624 static bool check_mtu(struct sk_buff *skb,
625                       struct vport *vport,
626                       const struct tnl_mutable_config *mutable,
627                       const struct rtable *rt, __be16 *frag_offp,
628                       int tunnel_hlen)
629 {
630         bool df_inherit;
631         bool pmtud;
632         __be16 frag_off;
633         int mtu = 0;
634         unsigned int packet_length = skb->len - ETH_HLEN;
635
636         if (OVS_CB(skb)->tun_key->ipv4_dst) {
637                 df_inherit = false;
638                 pmtud = false;
639                 frag_off = OVS_CB(skb)->tun_key->tun_flags & OVS_TNL_F_DONT_FRAGMENT ?
640                                   htons(IP_DF) : 0;
641         } else {
642                 df_inherit = mutable->flags & TNL_F_DF_INHERIT;
643                 pmtud = mutable->flags & TNL_F_PMTUD;
644                 frag_off = mutable->flags & TNL_F_DF_DEFAULT ? htons(IP_DF) : 0;
645         }
646
647         /* Allow for one level of tagging in the packet length. */
648         if (!vlan_tx_tag_present(skb) &&
649             eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
650                 packet_length -= VLAN_HLEN;
651
652         if (pmtud) {
653                 int vlan_header = 0;
654
655                 /* The tag needs to go in packet regardless of where it
656                  * currently is, so subtract it from the MTU.
657                  */
658                 if (vlan_tx_tag_present(skb) ||
659                     eth_hdr(skb)->h_proto == htons(ETH_P_8021Q))
660                         vlan_header = VLAN_HLEN;
661
662                 mtu = dst_mtu(&rt_dst(rt))
663                         - ETH_HLEN
664                         - tunnel_hlen
665                         - vlan_header;
666         }
667
668         if (skb->protocol == htons(ETH_P_IP)) {
669                 struct iphdr *iph = ip_hdr(skb);
670
671                 if (df_inherit)
672                         frag_off = iph->frag_off & htons(IP_DF);
673
674                 if (pmtud && iph->frag_off & htons(IP_DF)) {
675                         mtu = max(mtu, IP_MIN_MTU);
676
677                         if (packet_length > mtu &&
678                             ovs_tnl_frag_needed(vport, mutable, skb, mtu))
679                                 return false;
680                 }
681         }
682 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
683         else if (skb->protocol == htons(ETH_P_IPV6)) {
684                 /* IPv6 requires end hosts to do fragmentation
685                  * if the packet is above the minimum MTU.
686                  */
687                 if (df_inherit && packet_length > IPV6_MIN_MTU)
688                         frag_off = htons(IP_DF);
689
690                 if (pmtud) {
691                         mtu = max(mtu, IPV6_MIN_MTU);
692
693                         if (packet_length > mtu &&
694                             ovs_tnl_frag_needed(vport, mutable, skb, mtu))
695                                 return false;
696                 }
697         }
698 #endif
699
700         *frag_offp = frag_off;
701         return true;
702 }
703
704 static struct rtable *find_route(struct net *net,
705                 __be32 *saddr, __be32 daddr, u8 ipproto,
706                 u8 tos)
707 {
708         struct rtable *rt;
709         /* Tunnel configuration keeps DSCP part of TOS bits, But Linux
710          * router expect RT_TOS bits only. */
711
712 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
713         struct flowi fl = { .nl_u = { .ip4_u = {
714                                         .daddr = daddr,
715                                         .saddr = *saddr,
716                                         .tos   = RT_TOS(tos) } },
717                                         .proto = ipproto };
718
719         if (unlikely(ip_route_output_key(net, &rt, &fl)))
720                 return ERR_PTR(-EADDRNOTAVAIL);
721         *saddr = fl.nl_u.ip4_u.saddr;
722         return rt;
723 #else
724         struct flowi4 fl = { .daddr = daddr,
725                              .saddr = *saddr,
726                              .flowi4_tos = RT_TOS(tos),
727                              .flowi4_proto = ipproto };
728
729         rt = ip_route_output_key(net, &fl);
730         *saddr = fl.saddr;
731         return rt;
732 #endif
733 }
734
735 static bool need_linearize(const struct sk_buff *skb)
736 {
737         int i;
738
739         if (unlikely(skb_shinfo(skb)->frag_list))
740                 return true;
741
742         /*
743          * Generally speaking we should linearize if there are paged frags.
744          * However, if all of the refcounts are 1 we know nobody else can
745          * change them from underneath us and we can skip the linearization.
746          */
747         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
748                 if (unlikely(page_count(skb_frag_page(&skb_shinfo(skb)->frags[i])) > 1))
749                         return true;
750
751         return false;
752 }
753
754 static struct sk_buff *handle_offloads(struct sk_buff *skb,
755                                        const struct tnl_mutable_config *mutable,
756                                        const struct rtable *rt,
757                                        int tunnel_hlen)
758 {
759         int min_headroom;
760         int err;
761
762         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
763                         + tunnel_hlen
764                         + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
765
766         if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
767                 int head_delta = SKB_DATA_ALIGN(min_headroom -
768                                                 skb_headroom(skb) +
769                                                 16);
770                 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
771                                         0, GFP_ATOMIC);
772                 if (unlikely(err))
773                         goto error_free;
774         }
775
776         forward_ip_summed(skb, true);
777
778         if (skb_is_gso(skb)) {
779                 struct sk_buff *nskb;
780
781                 nskb = skb_gso_segment(skb, 0);
782                 if (IS_ERR(nskb)) {
783                         kfree_skb(skb);
784                         err = PTR_ERR(nskb);
785                         goto error;
786                 }
787
788                 consume_skb(skb);
789                 skb = nskb;
790         } else if (get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
791                 /* Pages aren't locked and could change at any time.
792                  * If this happens after we compute the checksum, the
793                  * checksum will be wrong.  We linearize now to avoid
794                  * this problem.
795                  */
796                 if (unlikely(need_linearize(skb))) {
797                         err = __skb_linearize(skb);
798                         if (unlikely(err))
799                                 goto error_free;
800                 }
801
802                 err = skb_checksum_help(skb);
803                 if (unlikely(err))
804                         goto error_free;
805         }
806
807         set_ip_summed(skb, OVS_CSUM_NONE);
808
809         return skb;
810
811 error_free:
812         kfree_skb(skb);
813 error:
814         return ERR_PTR(err);
815 }
816
817 static int send_frags(struct sk_buff *skb,
818                       int tunnel_hlen)
819 {
820         int sent_len;
821
822         sent_len = 0;
823         while (skb) {
824                 struct sk_buff *next = skb->next;
825                 int frag_len = skb->len - tunnel_hlen;
826                 int err;
827
828                 skb->next = NULL;
829                 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
830
831                 err = ip_local_out(skb);
832                 skb = next;
833                 if (unlikely(net_xmit_eval(err)))
834                         goto free_frags;
835                 sent_len += frag_len;
836         }
837
838         return sent_len;
839
840 free_frags:
841         /*
842          * There's no point in continuing to send fragments once one has been
843          * dropped so just free the rest.  This may help improve the congestion
844          * that caused the first packet to be dropped.
845          */
846         ovs_tnl_free_linked_skbs(skb);
847         return sent_len;
848 }
849
850 int ovs_tnl_send(struct vport *vport, struct sk_buff *skb)
851 {
852         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
853         const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
854         enum vport_err_type err = VPORT_E_TX_ERROR;
855         struct rtable *rt;
856         struct ovs_key_ipv4_tunnel tun_key;
857         int sent_len = 0;
858         int tunnel_hlen;
859         __be16 frag_off = 0;
860         __be32 daddr;
861         __be32 saddr;
862         u8 ttl;
863         u8 tos;
864
865         /* Validate the protocol headers before we try to use them. */
866         if (skb->protocol == htons(ETH_P_8021Q) &&
867             !vlan_tx_tag_present(skb)) {
868                 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
869                         goto error_free;
870
871                 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
872                 skb_set_network_header(skb, VLAN_ETH_HLEN);
873         }
874
875         if (skb->protocol == htons(ETH_P_IP)) {
876                 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
877                     + sizeof(struct iphdr))))
878                         skb->protocol = 0;
879         }
880 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
881         else if (skb->protocol == htons(ETH_P_IPV6)) {
882                 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
883                     + sizeof(struct ipv6hdr))))
884                         skb->protocol = 0;
885         }
886 #endif
887
888         /* If OVS_CB(skb)->tun_key is NULL, point it at the local tun_key here,
889          * and zero it out.
890          */
891         if (!OVS_CB(skb)->tun_key) {
892                 memset(&tun_key, 0, sizeof(tun_key));
893                 OVS_CB(skb)->tun_key = &tun_key;
894         }
895
896         tunnel_hlen = tnl_vport->tnl_ops->hdr_len(mutable, OVS_CB(skb)->tun_key);
897         if (unlikely(tunnel_hlen < 0)) {
898                 err = VPORT_E_TX_DROPPED;
899                 goto error_free;
900         }
901         tunnel_hlen += sizeof(struct iphdr);
902
903         if (OVS_CB(skb)->tun_key->ipv4_dst) {
904                 daddr = OVS_CB(skb)->tun_key->ipv4_dst;
905                 saddr = OVS_CB(skb)->tun_key->ipv4_src;
906                 tos = OVS_CB(skb)->tun_key->ipv4_tos;
907                 ttl = OVS_CB(skb)->tun_key->ipv4_ttl;
908         } else {
909                 u8 inner_tos;
910                 daddr = mutable->key.daddr;
911                 saddr = mutable->key.saddr;
912
913                 if (unlikely(!daddr)) {
914                         /* Trying to sent packet from Null-port without
915                          * tunnel info? Drop this packet. */
916                         err = VPORT_E_TX_DROPPED;
917                         goto error_free;
918                 }
919
920                 /* ToS */
921                 if (skb->protocol == htons(ETH_P_IP))
922                         inner_tos = ip_hdr(skb)->tos;
923 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
924                 else if (skb->protocol == htons(ETH_P_IPV6))
925                         inner_tos = ipv6_get_dsfield(ipv6_hdr(skb));
926 #endif
927                 else
928                         inner_tos = 0;
929
930                 if (mutable->flags & TNL_F_TOS_INHERIT)
931                         tos = inner_tos;
932                 else
933                         tos = mutable->tos;
934
935                 tos = INET_ECN_encapsulate(tos, inner_tos);
936
937                 /* TTL */
938                 ttl = mutable->ttl;
939                 if (mutable->flags & TNL_F_TTL_INHERIT) {
940                         if (skb->protocol == htons(ETH_P_IP))
941                                 ttl = ip_hdr(skb)->ttl;
942 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
943                         else if (skb->protocol == htons(ETH_P_IPV6))
944                                 ttl = ipv6_hdr(skb)->hop_limit;
945 #endif
946                 }
947
948         }
949
950         /* Route lookup */
951         rt = find_route(port_key_get_net(&mutable->key), &saddr, daddr,
952                           tnl_vport->tnl_ops->ipproto, tos);
953         if (IS_ERR(rt))
954                 goto error_free;
955
956         /* Reset SKB */
957         nf_reset(skb);
958         secpath_reset(skb);
959         skb_dst_drop(skb);
960         skb_clear_rxhash(skb);
961
962         /* Offloading */
963         skb = handle_offloads(skb, mutable, rt, tunnel_hlen);
964         if (IS_ERR(skb)) {
965                 skb = NULL;
966                 goto err_free_rt;
967         }
968
969         /* MTU */
970         if (unlikely(!check_mtu(skb, vport, mutable, rt, &frag_off, tunnel_hlen))) {
971                 err = VPORT_E_TX_DROPPED;
972                 goto err_free_rt;
973         }
974
975         /* TTL Fixup. */
976         if (!OVS_CB(skb)->tun_key->ipv4_dst) {
977                 if (!(mutable->flags & TNL_F_TTL_INHERIT)) {
978                         if (!ttl)
979                                 ttl = ip4_dst_hoplimit(&rt_dst(rt));
980                 }
981         }
982
983         while (skb) {
984                 struct iphdr *iph;
985                 struct sk_buff *next_skb = skb->next;
986                 skb->next = NULL;
987
988                 if (unlikely(vlan_deaccel_tag(skb)))
989                         goto next;
990
991                 skb_push(skb, tunnel_hlen);
992                 skb_reset_network_header(skb);
993                 skb_set_transport_header(skb, sizeof(struct iphdr));
994
995                 if (next_skb)
996                         skb_dst_set(skb, dst_clone(&rt_dst(rt)));
997                 else
998                         skb_dst_set(skb, &rt_dst(rt));
999
1000                 /* Push IP header. */
1001                 iph = ip_hdr(skb);
1002                 iph->version    = 4;
1003                 iph->ihl        = sizeof(struct iphdr) >> 2;
1004                 iph->protocol   = tnl_vport->tnl_ops->ipproto;
1005                 iph->daddr      = daddr;
1006                 iph->saddr      = saddr;
1007                 iph->tos        = tos;
1008                 iph->ttl        = ttl;
1009                 iph->frag_off   = frag_off;
1010                 ip_select_ident(iph, &rt_dst(rt), NULL);
1011
1012                 /* Push Tunnel header. */
1013                 skb = tnl_vport->tnl_ops->build_header(vport, mutable,
1014                                                         &rt_dst(rt), skb, tunnel_hlen);
1015                 if (unlikely(!skb))
1016                         goto next;
1017
1018                 sent_len += send_frags(skb, tunnel_hlen);
1019
1020 next:
1021                 skb = next_skb;
1022         }
1023
1024         if (unlikely(sent_len == 0))
1025                 ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
1026
1027         return sent_len;
1028
1029 err_free_rt:
1030         ip_rt_put(rt);
1031 error_free:
1032         ovs_tnl_free_linked_skbs(skb);
1033         ovs_vport_record_error(vport, err);
1034         return sent_len;
1035 }
1036
1037 static const struct nla_policy tnl_policy[OVS_TUNNEL_ATTR_MAX + 1] = {
1038         [OVS_TUNNEL_ATTR_FLAGS]    = { .type = NLA_U32 },
1039         [OVS_TUNNEL_ATTR_DST_IPV4] = { .type = NLA_U32 },
1040         [OVS_TUNNEL_ATTR_SRC_IPV4] = { .type = NLA_U32 },
1041         [OVS_TUNNEL_ATTR_OUT_KEY]  = { .type = NLA_U64 },
1042         [OVS_TUNNEL_ATTR_IN_KEY]   = { .type = NLA_U64 },
1043         [OVS_TUNNEL_ATTR_TOS]      = { .type = NLA_U8 },
1044         [OVS_TUNNEL_ATTR_TTL]      = { .type = NLA_U8 },
1045 };
1046
1047 /* Sets OVS_TUNNEL_ATTR_* fields in 'mutable', which must initially be
1048  * zeroed. */
1049 static int tnl_set_config(struct net *net, struct nlattr *options,
1050                           const struct tnl_ops *tnl_ops,
1051                           const struct vport *cur_vport,
1052                           struct tnl_mutable_config *mutable)
1053 {
1054         const struct vport *old_vport;
1055         const struct tnl_mutable_config *old_mutable;
1056         struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
1057         int err;
1058
1059         port_key_set_net(&mutable->key, net);
1060         mutable->key.tunnel_type = tnl_ops->tunnel_type;
1061         if (!options)
1062                 goto out;
1063
1064         err = nla_parse_nested(a, OVS_TUNNEL_ATTR_MAX, options, tnl_policy);
1065         if (err)
1066                 return err;
1067
1068         if (!a[OVS_TUNNEL_ATTR_FLAGS] || !a[OVS_TUNNEL_ATTR_DST_IPV4])
1069                 return -EINVAL;
1070
1071         mutable->flags = nla_get_u32(a[OVS_TUNNEL_ATTR_FLAGS]) & TNL_F_PUBLIC;
1072         mutable->key.daddr = nla_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
1073
1074         if (a[OVS_TUNNEL_ATTR_SRC_IPV4]) {
1075                 if (ipv4_is_multicast(mutable->key.daddr))
1076                         return -EINVAL;
1077                 mutable->key.saddr = nla_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
1078         }
1079
1080         if (a[OVS_TUNNEL_ATTR_TOS]) {
1081                 mutable->tos = nla_get_u8(a[OVS_TUNNEL_ATTR_TOS]);
1082                 /* Reject ToS config with ECN bits set. */
1083                 if (mutable->tos & INET_ECN_MASK)
1084                         return -EINVAL;
1085         }
1086
1087         if (a[OVS_TUNNEL_ATTR_TTL])
1088                 mutable->ttl = nla_get_u8(a[OVS_TUNNEL_ATTR_TTL]);
1089
1090         if (!a[OVS_TUNNEL_ATTR_IN_KEY]) {
1091                 mutable->key.tunnel_type |= TNL_T_KEY_MATCH;
1092                 mutable->flags |= TNL_F_IN_KEY_MATCH;
1093         } else {
1094                 mutable->key.tunnel_type |= TNL_T_KEY_EXACT;
1095                 mutable->key.in_key = nla_get_be64(a[OVS_TUNNEL_ATTR_IN_KEY]);
1096         }
1097
1098         if (!a[OVS_TUNNEL_ATTR_OUT_KEY])
1099                 mutable->flags |= TNL_F_OUT_KEY_ACTION;
1100         else
1101                 mutable->out_key = nla_get_be64(a[OVS_TUNNEL_ATTR_OUT_KEY]);
1102
1103         mutable->mlink = 0;
1104         if (ipv4_is_multicast(mutable->key.daddr)) {
1105                 struct net_device *dev;
1106                 struct rtable *rt;
1107                 __be32 saddr = mutable->key.saddr;
1108
1109                 rt = find_route(port_key_get_net(&mutable->key),
1110                              &saddr, mutable->key.daddr,
1111                              tnl_ops->ipproto, mutable->tos);
1112                 if (IS_ERR(rt))
1113                         return -EADDRNOTAVAIL;
1114                 dev = rt_dst(rt).dev;
1115                 ip_rt_put(rt);
1116                 if (__in_dev_get_rtnl(dev) == NULL)
1117                         return -EADDRNOTAVAIL;
1118                 mutable->mlink = dev->ifindex;
1119                 ip_mc_inc_group(__in_dev_get_rtnl(dev), mutable->key.daddr);
1120         }
1121
1122 out:
1123         old_vport = port_table_lookup(&mutable->key, &old_mutable);
1124         if (old_vport && old_vport != cur_vport)
1125                 return -EEXIST;
1126
1127         return 0;
1128 }
1129
1130 struct vport *ovs_tnl_create(const struct vport_parms *parms,
1131                              const struct vport_ops *vport_ops,
1132                              const struct tnl_ops *tnl_ops)
1133 {
1134         struct vport *vport;
1135         struct tnl_vport *tnl_vport;
1136         struct tnl_mutable_config *mutable;
1137         int initial_frag_id;
1138         int err;
1139
1140         vport = ovs_vport_alloc(sizeof(struct tnl_vport), vport_ops, parms);
1141         if (IS_ERR(vport)) {
1142                 err = PTR_ERR(vport);
1143                 goto error;
1144         }
1145
1146         tnl_vport = tnl_vport_priv(vport);
1147
1148         strcpy(tnl_vport->name, parms->name);
1149         tnl_vport->tnl_ops = tnl_ops;
1150
1151         mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1152         if (!mutable) {
1153                 err = -ENOMEM;
1154                 goto error_free_vport;
1155         }
1156
1157         random_ether_addr(mutable->eth_addr);
1158
1159         get_random_bytes(&initial_frag_id, sizeof(int));
1160         atomic_set(&tnl_vport->frag_id, initial_frag_id);
1161
1162         err = tnl_set_config(ovs_dp_get_net(parms->dp), parms->options, tnl_ops,
1163                              NULL, mutable);
1164         if (err)
1165                 goto error_free_mutable;
1166
1167         rcu_assign_pointer(tnl_vport->mutable, mutable);
1168
1169         port_table_add_port(vport);
1170         return vport;
1171
1172 error_free_mutable:
1173         free_mutable_rtnl(mutable);
1174         kfree(mutable);
1175 error_free_vport:
1176         ovs_vport_free(vport);
1177 error:
1178         return ERR_PTR(err);
1179 }
1180
1181 int ovs_tnl_set_options(struct vport *vport, struct nlattr *options)
1182 {
1183         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1184         const struct tnl_mutable_config *old_mutable;
1185         struct tnl_mutable_config *mutable;
1186         int err;
1187
1188         old_mutable = rtnl_dereference(tnl_vport->mutable);
1189         if (!old_mutable->key.daddr)
1190                 return -EINVAL;
1191
1192         mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
1193         if (!mutable) {
1194                 err = -ENOMEM;
1195                 goto error;
1196         }
1197
1198         /* Copy fields whose values should be retained. */
1199         mutable->seq = old_mutable->seq + 1;
1200         memcpy(mutable->eth_addr, old_mutable->eth_addr, ETH_ALEN);
1201
1202         /* Parse the others configured by userspace. */
1203         err = tnl_set_config(ovs_dp_get_net(vport->dp), options, tnl_vport->tnl_ops,
1204                              vport, mutable);
1205         if (err)
1206                 goto error_free;
1207
1208         if (port_hash(&mutable->key) != port_hash(&old_mutable->key))
1209                 port_table_move_port(vport, mutable);
1210         else
1211                 assign_config_rcu(vport, mutable);
1212
1213         return 0;
1214
1215 error_free:
1216         free_mutable_rtnl(mutable);
1217         kfree(mutable);
1218 error:
1219         return err;
1220 }
1221
1222 int ovs_tnl_get_options(const struct vport *vport, struct sk_buff *skb)
1223 {
1224         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1225         const struct tnl_mutable_config *mutable = rcu_dereference_rtnl(tnl_vport->mutable);
1226
1227         if (nla_put_u32(skb, OVS_TUNNEL_ATTR_FLAGS,
1228                       mutable->flags & TNL_F_PUBLIC) ||
1229             nla_put_be32(skb, OVS_TUNNEL_ATTR_DST_IPV4, mutable->key.daddr))
1230                 goto nla_put_failure;
1231
1232         if (!(mutable->flags & TNL_F_IN_KEY_MATCH) &&
1233             nla_put_be64(skb, OVS_TUNNEL_ATTR_IN_KEY, mutable->key.in_key))
1234                 goto nla_put_failure;
1235         if (!(mutable->flags & TNL_F_OUT_KEY_ACTION) &&
1236             nla_put_be64(skb, OVS_TUNNEL_ATTR_OUT_KEY, mutable->out_key))
1237                 goto nla_put_failure;
1238         if (mutable->key.saddr &&
1239             nla_put_be32(skb, OVS_TUNNEL_ATTR_SRC_IPV4, mutable->key.saddr))
1240                 goto nla_put_failure;
1241         if (mutable->tos && nla_put_u8(skb, OVS_TUNNEL_ATTR_TOS, mutable->tos))
1242                 goto nla_put_failure;
1243         if (mutable->ttl && nla_put_u8(skb, OVS_TUNNEL_ATTR_TTL, mutable->ttl))
1244                 goto nla_put_failure;
1245
1246         return 0;
1247
1248 nla_put_failure:
1249         return -EMSGSIZE;
1250 }
1251
1252 static void free_port_rcu(struct rcu_head *rcu)
1253 {
1254         struct tnl_vport *tnl_vport = container_of(rcu,
1255                                                    struct tnl_vport, rcu);
1256
1257         kfree((struct tnl_mutable __force *)tnl_vport->mutable);
1258         ovs_vport_free(tnl_vport_to_vport(tnl_vport));
1259 }
1260
1261 void ovs_tnl_destroy(struct vport *vport)
1262 {
1263         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1264         struct tnl_mutable_config *mutable;
1265
1266         mutable = rtnl_dereference(tnl_vport->mutable);
1267         port_table_remove_port(vport);
1268         free_mutable_rtnl(mutable);
1269         call_rcu(&tnl_vport->rcu, free_port_rcu);
1270 }
1271
1272 int ovs_tnl_set_addr(struct vport *vport, const unsigned char *addr)
1273 {
1274         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1275         struct tnl_mutable_config *old_mutable, *mutable;
1276
1277         old_mutable = rtnl_dereference(tnl_vport->mutable);
1278         mutable = kmemdup(old_mutable, sizeof(struct tnl_mutable_config), GFP_KERNEL);
1279         if (!mutable)
1280                 return -ENOMEM;
1281
1282         old_mutable->mlink = 0;
1283
1284         memcpy(mutable->eth_addr, addr, ETH_ALEN);
1285         assign_config_rcu(vport, mutable);
1286
1287         return 0;
1288 }
1289
1290 const char *ovs_tnl_get_name(const struct vport *vport)
1291 {
1292         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1293         return tnl_vport->name;
1294 }
1295
1296 const unsigned char *ovs_tnl_get_addr(const struct vport *vport)
1297 {
1298         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
1299         return rcu_dereference_rtnl(tnl_vport->mutable)->eth_addr;
1300 }
1301
1302 void ovs_tnl_free_linked_skbs(struct sk_buff *skb)
1303 {
1304         while (skb) {
1305                 struct sk_buff *next = skb->next;
1306                 kfree_skb(skb);
1307                 skb = next;
1308         }
1309 }
1310
1311 int ovs_tnl_init(void)
1312 {
1313         int i;
1314
1315         port_table = kmalloc(PORT_TABLE_SIZE * sizeof(struct hlist_head *),
1316                              GFP_KERNEL);
1317         if (!port_table)
1318                 return -ENOMEM;
1319
1320         for (i = 0; i < PORT_TABLE_SIZE; i++)
1321                 INIT_HLIST_HEAD(&port_table[i]);
1322
1323         return 0;
1324 }
1325
1326 void ovs_tnl_exit(void)
1327 {
1328         kfree(port_table);
1329 }