3964208a458c4c1fe9bbffda855d790e9c4d5391
[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 "compat.h"
49 #include "datapath.h"
50 #include "tunnel.h"
51 #include "vlan.h"
52 #include "vport.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 struct rtable *find_route(struct net *net,
363                 __be32 *saddr, __be32 daddr, u8 ipproto,
364                 u8 tos, u32 skb_mark)
365 {
366         struct rtable *rt;
367         /* Tunnel configuration keeps DSCP part of TOS bits, But Linux
368          * router expect RT_TOS bits only. */
369
370 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,39)
371         struct flowi fl = { .nl_u = { .ip4_u = {
372                                         .daddr = daddr,
373                                         .saddr = *saddr,
374 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20)
375                                         .fwmark = skb_mark,
376 #endif
377                                         .tos   = RT_TOS(tos) } },
378 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
379                                         .mark = skb_mark,
380 #endif
381                                         .proto = ipproto };
382
383         if (unlikely(ip_route_output_key(net, &rt, &fl)))
384                 return ERR_PTR(-EADDRNOTAVAIL);
385         *saddr = fl.nl_u.ip4_u.saddr;
386         return rt;
387 #else
388         struct flowi4 fl = { .daddr = daddr,
389                              .saddr = *saddr,
390                              .flowi4_tos = RT_TOS(tos),
391                              .flowi4_mark = skb_mark,
392                              .flowi4_proto = ipproto };
393
394         rt = ip_route_output_key(net, &fl);
395         *saddr = fl.saddr;
396         return rt;
397 #endif
398 }
399
400 static bool need_linearize(const struct sk_buff *skb)
401 {
402         int i;
403
404         if (unlikely(skb_shinfo(skb)->frag_list))
405                 return true;
406
407         /*
408          * Generally speaking we should linearize if there are paged frags.
409          * However, if all of the refcounts are 1 we know nobody else can
410          * change them from underneath us and we can skip the linearization.
411          */
412         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++)
413                 if (unlikely(page_count(skb_frag_page(&skb_shinfo(skb)->frags[i])) > 1))
414                         return true;
415
416         return false;
417 }
418
419 static struct sk_buff *handle_offloads(struct sk_buff *skb,
420                                        const struct tnl_mutable_config *mutable,
421                                        const struct rtable *rt,
422                                        int tunnel_hlen)
423 {
424         int min_headroom;
425         int err;
426
427         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
428                         + tunnel_hlen
429                         + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
430
431         if (skb_headroom(skb) < min_headroom || skb_header_cloned(skb)) {
432                 int head_delta = SKB_DATA_ALIGN(min_headroom -
433                                                 skb_headroom(skb) +
434                                                 16);
435                 err = pskb_expand_head(skb, max_t(int, head_delta, 0),
436                                         0, GFP_ATOMIC);
437                 if (unlikely(err))
438                         goto error_free;
439         }
440
441         forward_ip_summed(skb, true);
442
443         if (skb_is_gso(skb)) {
444                 struct sk_buff *nskb;
445
446                 nskb = __skb_gso_segment(skb, 0, false);
447                 if (IS_ERR(nskb)) {
448                         kfree_skb(skb);
449                         err = PTR_ERR(nskb);
450                         goto error;
451                 }
452
453                 consume_skb(skb);
454                 skb = nskb;
455         } else if (get_ip_summed(skb) == OVS_CSUM_PARTIAL) {
456                 /* Pages aren't locked and could change at any time.
457                  * If this happens after we compute the checksum, the
458                  * checksum will be wrong.  We linearize now to avoid
459                  * this problem.
460                  */
461                 if (unlikely(need_linearize(skb))) {
462                         err = __skb_linearize(skb);
463                         if (unlikely(err))
464                                 goto error_free;
465                 }
466
467                 err = skb_checksum_help(skb);
468                 if (unlikely(err))
469                         goto error_free;
470         }
471
472         set_ip_summed(skb, OVS_CSUM_NONE);
473
474         return skb;
475
476 error_free:
477         kfree_skb(skb);
478 error:
479         return ERR_PTR(err);
480 }
481
482 static int send_frags(struct sk_buff *skb,
483                       int tunnel_hlen)
484 {
485         int sent_len;
486
487         sent_len = 0;
488         while (skb) {
489                 struct sk_buff *next = skb->next;
490                 int frag_len = skb->len - tunnel_hlen;
491                 int err;
492
493                 skb->next = NULL;
494                 memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
495
496                 err = ip_local_out(skb);
497                 skb = next;
498                 if (unlikely(net_xmit_eval(err)))
499                         goto free_frags;
500                 sent_len += frag_len;
501         }
502
503         return sent_len;
504
505 free_frags:
506         /*
507          * There's no point in continuing to send fragments once one has been
508          * dropped so just free the rest.  This may help improve the congestion
509          * that caused the first packet to be dropped.
510          */
511         ovs_tnl_free_linked_skbs(skb);
512         return sent_len;
513 }
514
515 int ovs_tnl_send(struct vport *vport, struct sk_buff *skb)
516 {
517         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
518         const struct tnl_mutable_config *mutable = rcu_dereference(tnl_vport->mutable);
519         enum vport_err_type err = VPORT_E_TX_ERROR;
520         struct rtable *rt;
521         struct ovs_key_ipv4_tunnel tun_key;
522         int sent_len = 0;
523         int tunnel_hlen;
524         __be16 frag_off;
525         __be32 daddr;
526         __be32 saddr;
527         u32 skb_mark;
528         u8 ttl;
529         u8 tos;
530
531         /* Validate the protocol headers before we try to use them. */
532         if (skb->protocol == htons(ETH_P_8021Q) &&
533             !vlan_tx_tag_present(skb)) {
534                 if (unlikely(!pskb_may_pull(skb, VLAN_ETH_HLEN)))
535                         goto error_free;
536
537                 skb->protocol = vlan_eth_hdr(skb)->h_vlan_encapsulated_proto;
538                 skb_set_network_header(skb, VLAN_ETH_HLEN);
539         }
540
541         if (skb->protocol == htons(ETH_P_IP)) {
542                 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
543                     + sizeof(struct iphdr))))
544                         skb->protocol = 0;
545         }
546 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
547         else if (skb->protocol == htons(ETH_P_IPV6)) {
548                 if (unlikely(!pskb_may_pull(skb, skb_network_offset(skb)
549                     + sizeof(struct ipv6hdr))))
550                         skb->protocol = 0;
551         }
552 #endif
553
554         /* If OVS_CB(skb)->tun_key is NULL, point it at the local tun_key here,
555          * and zero it out.
556          */
557         if (!OVS_CB(skb)->tun_key) {
558                 memset(&tun_key, 0, sizeof(tun_key));
559                 OVS_CB(skb)->tun_key = &tun_key;
560         }
561
562         tunnel_hlen = tnl_vport->tnl_ops->hdr_len(mutable, OVS_CB(skb)->tun_key);
563         if (unlikely(tunnel_hlen < 0)) {
564                 err = VPORT_E_TX_DROPPED;
565                 goto error_free;
566         }
567         tunnel_hlen += sizeof(struct iphdr);
568
569         if (OVS_CB(skb)->tun_key->ipv4_dst) {
570                 daddr = OVS_CB(skb)->tun_key->ipv4_dst;
571                 saddr = OVS_CB(skb)->tun_key->ipv4_src;
572                 tos = OVS_CB(skb)->tun_key->ipv4_tos;
573                 ttl = OVS_CB(skb)->tun_key->ipv4_ttl;
574                 frag_off = OVS_CB(skb)->tun_key->tun_flags &
575                                 OVS_TNL_F_DONT_FRAGMENT ?  htons(IP_DF) : 0;
576         } else {
577                 u8 inner_tos;
578                 daddr = mutable->key.daddr;
579                 saddr = mutable->key.saddr;
580
581                 if (unlikely(!daddr)) {
582                         /* Trying to sent packet from Null-port without
583                          * tunnel info? Drop this packet. */
584                         err = VPORT_E_TX_DROPPED;
585                         goto error_free;
586                 }
587
588                 /* ToS */
589                 if (skb->protocol == htons(ETH_P_IP))
590                         inner_tos = ip_hdr(skb)->tos;
591 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
592                 else if (skb->protocol == htons(ETH_P_IPV6))
593                         inner_tos = ipv6_get_dsfield(ipv6_hdr(skb));
594 #endif
595                 else
596                         inner_tos = 0;
597
598                 if (mutable->flags & TNL_F_TOS_INHERIT)
599                         tos = inner_tos;
600                 else
601                         tos = mutable->tos;
602
603                 tos = INET_ECN_encapsulate(tos, inner_tos);
604
605                 /* TTL */
606                 ttl = mutable->ttl;
607                 if (mutable->flags & TNL_F_TTL_INHERIT) {
608                         if (skb->protocol == htons(ETH_P_IP))
609                                 ttl = ip_hdr(skb)->ttl;
610 #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE)
611                         else if (skb->protocol == htons(ETH_P_IPV6))
612                                 ttl = ipv6_hdr(skb)->hop_limit;
613 #endif
614                 }
615
616                 frag_off = mutable->flags & TNL_F_DF_DEFAULT ? htons(IP_DF) : 0;
617         }
618
619         /* Route lookup */
620         skb_mark = skb_get_mark(skb);
621         rt = find_route(port_key_get_net(&mutable->key), &saddr, daddr,
622                           tnl_vport->tnl_ops->ipproto, tos, skb_mark);
623         if (IS_ERR(rt))
624                 goto error_free;
625
626         /* Reset SKB */
627         nf_reset(skb);
628         secpath_reset(skb);
629         skb_dst_drop(skb);
630         skb_clear_rxhash(skb);
631
632         /* Offloading */
633         skb = handle_offloads(skb, mutable, rt, tunnel_hlen);
634         if (IS_ERR(skb)) {
635                 skb = NULL;
636                 goto err_free_rt;
637         }
638
639         /* TTL Fixup. */
640         if (!OVS_CB(skb)->tun_key->ipv4_dst) {
641                 if (!(mutable->flags & TNL_F_TTL_INHERIT)) {
642                         if (!ttl)
643                                 ttl = ip4_dst_hoplimit(&rt_dst(rt));
644                 }
645         }
646
647         while (skb) {
648                 struct iphdr *iph;
649                 struct sk_buff *next_skb = skb->next;
650                 skb->next = NULL;
651
652                 if (unlikely(vlan_deaccel_tag(skb)))
653                         goto next;
654
655                 skb_push(skb, tunnel_hlen);
656                 skb_reset_network_header(skb);
657                 skb_set_transport_header(skb, sizeof(struct iphdr));
658
659                 if (next_skb)
660                         skb_dst_set(skb, dst_clone(&rt_dst(rt)));
661                 else
662                         skb_dst_set(skb, &rt_dst(rt));
663
664                 /* Push IP header. */
665                 iph = ip_hdr(skb);
666                 iph->version    = 4;
667                 iph->ihl        = sizeof(struct iphdr) >> 2;
668                 iph->protocol   = tnl_vport->tnl_ops->ipproto;
669                 iph->daddr      = daddr;
670                 iph->saddr      = saddr;
671                 iph->tos        = tos;
672                 iph->ttl        = ttl;
673                 iph->frag_off   = frag_off;
674                 ip_select_ident(iph, &rt_dst(rt), NULL);
675
676                 /* Push Tunnel header. */
677                 skb = tnl_vport->tnl_ops->build_header(vport, mutable,
678                                                         &rt_dst(rt), skb, tunnel_hlen);
679                 if (unlikely(!skb))
680                         goto next;
681
682                 sent_len += send_frags(skb, tunnel_hlen);
683
684 next:
685                 skb = next_skb;
686         }
687
688         if (unlikely(sent_len == 0))
689                 ovs_vport_record_error(vport, VPORT_E_TX_DROPPED);
690
691         return sent_len;
692
693 err_free_rt:
694         ip_rt_put(rt);
695 error_free:
696         ovs_tnl_free_linked_skbs(skb);
697         ovs_vport_record_error(vport, err);
698         return sent_len;
699 }
700
701 static const struct nla_policy tnl_policy[OVS_TUNNEL_ATTR_MAX + 1] = {
702         [OVS_TUNNEL_ATTR_FLAGS]    = { .type = NLA_U32 },
703         [OVS_TUNNEL_ATTR_DST_IPV4] = { .type = NLA_U32 },
704         [OVS_TUNNEL_ATTR_SRC_IPV4] = { .type = NLA_U32 },
705         [OVS_TUNNEL_ATTR_OUT_KEY]  = { .type = NLA_U64 },
706         [OVS_TUNNEL_ATTR_IN_KEY]   = { .type = NLA_U64 },
707         [OVS_TUNNEL_ATTR_TOS]      = { .type = NLA_U8 },
708         [OVS_TUNNEL_ATTR_TTL]      = { .type = NLA_U8 },
709         [OVS_TUNNEL_ATTR_DST_PORT] = { .type = NLA_U16 },
710 };
711
712 /* Sets OVS_TUNNEL_ATTR_* fields in 'mutable', which must initially be
713  * zeroed. */
714 static int tnl_set_config(struct net *net, struct nlattr *options,
715                           const struct tnl_ops *tnl_ops,
716                           const struct vport *cur_vport,
717                           struct tnl_mutable_config *mutable)
718 {
719         const struct vport *old_vport;
720         const struct tnl_mutable_config *old_mutable;
721         struct nlattr *a[OVS_TUNNEL_ATTR_MAX + 1];
722         int err;
723
724         port_key_set_net(&mutable->key, net);
725         mutable->key.tunnel_type = tnl_ops->tunnel_type;
726         if (!options)
727                 goto out;
728
729         err = nla_parse_nested(a, OVS_TUNNEL_ATTR_MAX, options, tnl_policy);
730         if (err)
731                 return err;
732
733         /* Process attributes possibly useful for null_ports first */
734         if (a[OVS_TUNNEL_ATTR_DST_PORT])
735                 mutable->dst_port =
736                         htons(nla_get_u16(a[OVS_TUNNEL_ATTR_DST_PORT]));
737
738         if (a[OVS_TUNNEL_ATTR_DST_IPV4])
739                 mutable->key.daddr = nla_get_be32(a[OVS_TUNNEL_ATTR_DST_IPV4]);
740
741         /* Skip the rest if configuring a null_port */
742         if (!mutable->key.daddr)
743                 goto out;
744
745         if (a[OVS_TUNNEL_ATTR_FLAGS])
746                 mutable->flags = nla_get_u32(a[OVS_TUNNEL_ATTR_FLAGS])
747                         & TNL_F_PUBLIC;
748
749         if (a[OVS_TUNNEL_ATTR_SRC_IPV4]) {
750                 if (ipv4_is_multicast(mutable->key.daddr))
751                         return -EINVAL;
752                 mutable->key.saddr = nla_get_be32(a[OVS_TUNNEL_ATTR_SRC_IPV4]);
753         }
754
755         if (a[OVS_TUNNEL_ATTR_TOS]) {
756                 mutable->tos = nla_get_u8(a[OVS_TUNNEL_ATTR_TOS]);
757                 /* Reject ToS config with ECN bits set. */
758                 if (mutable->tos & INET_ECN_MASK)
759                         return -EINVAL;
760         }
761
762         if (a[OVS_TUNNEL_ATTR_TTL])
763                 mutable->ttl = nla_get_u8(a[OVS_TUNNEL_ATTR_TTL]);
764
765         if (!a[OVS_TUNNEL_ATTR_IN_KEY]) {
766                 mutable->key.tunnel_type |= TNL_T_KEY_MATCH;
767                 mutable->flags |= TNL_F_IN_KEY_MATCH;
768         } else {
769                 mutable->key.tunnel_type |= TNL_T_KEY_EXACT;
770                 mutable->key.in_key = nla_get_be64(a[OVS_TUNNEL_ATTR_IN_KEY]);
771         }
772
773         if (!a[OVS_TUNNEL_ATTR_OUT_KEY])
774                 mutable->flags |= TNL_F_OUT_KEY_ACTION;
775         else
776                 mutable->out_key = nla_get_be64(a[OVS_TUNNEL_ATTR_OUT_KEY]);
777
778         mutable->mlink = 0;
779         if (ipv4_is_multicast(mutable->key.daddr)) {
780                 struct net_device *dev;
781                 struct rtable *rt;
782                 __be32 saddr = mutable->key.saddr;
783
784                 rt = find_route(port_key_get_net(&mutable->key),
785                              &saddr, mutable->key.daddr,
786                              tnl_ops->ipproto, mutable->tos, 0);
787                 if (IS_ERR(rt))
788                         return -EADDRNOTAVAIL;
789                 dev = rt_dst(rt).dev;
790                 ip_rt_put(rt);
791                 if (__in_dev_get_rtnl(dev) == NULL)
792                         return -EADDRNOTAVAIL;
793                 mutable->mlink = dev->ifindex;
794                 ip_mc_inc_group(__in_dev_get_rtnl(dev), mutable->key.daddr);
795         }
796
797 out:
798         old_vport = port_table_lookup(&mutable->key, &old_mutable);
799         if (old_vport && old_vport != cur_vport)
800                 return -EEXIST;
801
802         return 0;
803 }
804
805 struct vport *ovs_tnl_create(const struct vport_parms *parms,
806                              const struct vport_ops *vport_ops,
807                              const struct tnl_ops *tnl_ops)
808 {
809         struct vport *vport;
810         struct tnl_vport *tnl_vport;
811         struct tnl_mutable_config *mutable;
812         int err;
813
814         vport = ovs_vport_alloc(sizeof(struct tnl_vport), vport_ops, parms);
815         if (IS_ERR(vport)) {
816                 err = PTR_ERR(vport);
817                 goto error;
818         }
819
820         tnl_vport = tnl_vport_priv(vport);
821
822         strcpy(tnl_vport->name, parms->name);
823         tnl_vport->tnl_ops = tnl_ops;
824
825         mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
826         if (!mutable) {
827                 err = -ENOMEM;
828                 goto error_free_vport;
829         }
830
831         err = tnl_set_config(ovs_dp_get_net(parms->dp), parms->options, tnl_ops,
832                              NULL, mutable);
833         if (err)
834                 goto error_free_mutable;
835
836         rcu_assign_pointer(tnl_vport->mutable, mutable);
837
838         port_table_add_port(vport);
839         return vport;
840
841 error_free_mutable:
842         free_mutable_rtnl(mutable);
843         kfree(mutable);
844 error_free_vport:
845         ovs_vport_free(vport);
846 error:
847         return ERR_PTR(err);
848 }
849
850 int ovs_tnl_set_options(struct vport *vport, struct nlattr *options)
851 {
852         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
853         const struct tnl_mutable_config *old_mutable;
854         struct tnl_mutable_config *mutable;
855         int err;
856
857         old_mutable = rtnl_dereference(tnl_vport->mutable);
858         if (!old_mutable->key.daddr)
859                 return -EINVAL;
860
861         mutable = kzalloc(sizeof(struct tnl_mutable_config), GFP_KERNEL);
862         if (!mutable) {
863                 err = -ENOMEM;
864                 goto error;
865         }
866
867         /* Parse the others configured by userspace. */
868         err = tnl_set_config(ovs_dp_get_net(vport->dp), options, tnl_vport->tnl_ops,
869                              vport, mutable);
870         if (err)
871                 goto error_free;
872
873         if (port_hash(&mutable->key) != port_hash(&old_mutable->key))
874                 port_table_move_port(vport, mutable);
875         else
876                 assign_config_rcu(vport, mutable);
877
878         return 0;
879
880 error_free:
881         free_mutable_rtnl(mutable);
882         kfree(mutable);
883 error:
884         return err;
885 }
886
887 int ovs_tnl_get_options(const struct vport *vport, struct sk_buff *skb)
888 {
889         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
890         const struct tnl_mutable_config *mutable = rcu_dereference_rtnl(tnl_vport->mutable);
891
892         if (mutable->dst_port && nla_put_u16(skb, OVS_TUNNEL_ATTR_DST_PORT,
893                                              ntohs(mutable->dst_port)))
894                 goto nla_put_failure;
895
896         /* Skip the rest for null_ports */
897         if (!mutable->key.daddr)
898                 return 0;
899
900         if (nla_put_be32(skb, OVS_TUNNEL_ATTR_DST_IPV4, mutable->key.daddr))
901                 goto nla_put_failure;
902         if (nla_put_u32(skb, OVS_TUNNEL_ATTR_FLAGS,
903                         mutable->flags & TNL_F_PUBLIC))
904                 goto nla_put_failure;
905         if (!(mutable->flags & TNL_F_IN_KEY_MATCH) &&
906             nla_put_be64(skb, OVS_TUNNEL_ATTR_IN_KEY, mutable->key.in_key))
907                 goto nla_put_failure;
908         if (!(mutable->flags & TNL_F_OUT_KEY_ACTION) &&
909             nla_put_be64(skb, OVS_TUNNEL_ATTR_OUT_KEY, mutable->out_key))
910                 goto nla_put_failure;
911         if (mutable->key.saddr &&
912             nla_put_be32(skb, OVS_TUNNEL_ATTR_SRC_IPV4, mutable->key.saddr))
913                 goto nla_put_failure;
914         if (mutable->tos && nla_put_u8(skb, OVS_TUNNEL_ATTR_TOS, mutable->tos))
915                 goto nla_put_failure;
916         if (mutable->ttl && nla_put_u8(skb, OVS_TUNNEL_ATTR_TTL, mutable->ttl))
917                 goto nla_put_failure;
918
919         return 0;
920
921 nla_put_failure:
922         return -EMSGSIZE;
923 }
924
925 static void free_port_rcu(struct rcu_head *rcu)
926 {
927         struct tnl_vport *tnl_vport = container_of(rcu,
928                                                    struct tnl_vport, rcu);
929
930         kfree((struct tnl_mutable __force *)tnl_vport->mutable);
931         ovs_vport_free(tnl_vport_to_vport(tnl_vport));
932 }
933
934 void ovs_tnl_destroy(struct vport *vport)
935 {
936         struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
937         struct tnl_mutable_config *mutable;
938
939         mutable = rtnl_dereference(tnl_vport->mutable);
940         port_table_remove_port(vport);
941         free_mutable_rtnl(mutable);
942         call_rcu(&tnl_vport->rcu, free_port_rcu);
943 }
944
945 const char *ovs_tnl_get_name(const struct vport *vport)
946 {
947         const struct tnl_vport *tnl_vport = tnl_vport_priv(vport);
948         return tnl_vport->name;
949 }
950
951 void ovs_tnl_free_linked_skbs(struct sk_buff *skb)
952 {
953         while (skb) {
954                 struct sk_buff *next = skb->next;
955                 kfree_skb(skb);
956                 skb = next;
957         }
958 }
959
960 int ovs_tnl_init(void)
961 {
962         int i;
963
964         port_table = kmalloc(PORT_TABLE_SIZE * sizeof(struct hlist_head *),
965                              GFP_KERNEL);
966         if (!port_table)
967                 return -ENOMEM;
968
969         for (i = 0; i < PORT_TABLE_SIZE; i++)
970                 INIT_HLIST_HEAD(&port_table[i]);
971
972         return 0;
973 }
974
975 void ovs_tnl_exit(void)
976 {
977         kfree(port_table);
978 }