datapath: Remove vlan compat support
[sliver-openvswitch.git] / datapath / linux / compat / vxlan.c
1 /*
2  * Copyright (c) 2007-2013 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  * This code is derived from kernel vxlan module.
19  */
20
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/kernel.h>
24 #include <linux/types.h>
25 #include <linux/module.h>
26 #include <linux/errno.h>
27 #include <linux/slab.h>
28 #include <linux/skbuff.h>
29 #include <linux/rculist.h>
30 #include <linux/netdevice.h>
31 #include <linux/in.h>
32 #include <linux/ip.h>
33 #include <linux/udp.h>
34 #include <linux/igmp.h>
35 #include <linux/etherdevice.h>
36 #include <linux/if_ether.h>
37 #include <linux/if_vlan.h>
38 #include <linux/hash.h>
39 #include <linux/ethtool.h>
40 #include <net/arp.h>
41 #include <net/ndisc.h>
42 #include <net/ip.h>
43 #include <net/ip_tunnels.h>
44 #include <net/icmp.h>
45 #include <net/udp.h>
46 #include <net/rtnetlink.h>
47 #include <net/route.h>
48 #include <net/dsfield.h>
49 #include <net/inet_ecn.h>
50 #include <net/net_namespace.h>
51 #include <net/netns/generic.h>
52 #include <net/vxlan.h>
53
54 #include "compat.h"
55 #include "gso.h"
56 #include "vlan.h"
57
58 #define PORT_HASH_BITS  8
59 #define PORT_HASH_SIZE  (1<<PORT_HASH_BITS)
60
61 /* IP header + UDP + VXLAN + Ethernet header */
62 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
63 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
64
65 #define VXLAN_FLAGS 0x08000000  /* struct vxlanhdr.vx_flags required value. */
66
67 /* VXLAN protocol header */
68 struct vxlanhdr {
69         __be32 vx_flags;
70         __be32 vx_vni;
71 };
72
73 static int vxlan_net_id;
74
75 static int vxlan_init_module(void);
76 static void vxlan_cleanup_module(void);
77
78 /* per-network namespace private data for this module */
79 struct vxlan_net {
80         struct hlist_head sock_list[PORT_HASH_SIZE];
81         spinlock_t  sock_lock;
82 };
83
84 /* Socket hash table head */
85 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
86 {
87         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
88
89         return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
90 }
91
92 /* Find VXLAN socket based on network namespace and UDP port */
93
94 static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
95 {
96         struct vxlan_sock *vs;
97
98         hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
99                 if (inet_sport(vs->sock->sk) == port)
100                         return vs;
101         }
102         return NULL;
103 }
104
105 /* Callback from net/ipv4/udp.c to receive packets */
106 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
107 {
108         struct vxlan_sock *vs;
109         struct vxlanhdr *vxh;
110
111         /* Need Vxlan and inner Ethernet header to be present */
112         if (!pskb_may_pull(skb, VXLAN_HLEN))
113                 goto error;
114
115         /* Return packets with reserved bits set */
116         vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
117         if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
118             (vxh->vx_vni & htonl(0xff))) {
119                 pr_warn("invalid vxlan flags=%#x vni=%#x\n",
120                         ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
121                 goto error;
122         }
123
124         if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
125                 goto drop;
126
127         vs = vxlan_find_sock(sock_net(sk), inet_sport(sk));
128         if (!vs)
129                 goto drop;
130
131         vs->rcv(vs, skb, vxh->vx_vni);
132         return 0;
133
134 drop:
135         /* Consume bad packet */
136         kfree_skb(skb);
137         return 0;
138
139 error:
140         /* Return non vxlan pkt */
141         return 1;
142 }
143
144 static void vxlan_sock_put(struct sk_buff *skb)
145 {
146         sock_put(skb->sk);
147 }
148
149 /* On transmit, associate with the tunnel socket */
150 static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
151 {
152         skb_orphan(skb);
153         sock_hold(sk);
154         skb->sk = sk;
155         skb->destructor = vxlan_sock_put;
156 }
157
158 /* Compute source port for outgoing packet
159  *   first choice to use L4 flow hash since it will spread
160  *     better and maybe available from hardware
161  *   secondary choice is to use jhash on the Ethernet header
162  */
163 __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
164 {
165         unsigned int range = (port_max - port_min) + 1;
166         u32 hash;
167
168         hash = skb_get_rxhash(skb);
169         if (!hash)
170                 hash = jhash(skb->data, 2 * ETH_ALEN,
171                              (__force u32) skb->protocol);
172
173         return htons((((u64) hash * range) >> 32) + port_min);
174 }
175
176 static void vxlan_gso(struct sk_buff *skb)
177 {
178         int udp_offset = skb_transport_offset(skb);
179         struct udphdr *uh;
180
181         uh = udp_hdr(skb);
182         uh->len = htons(skb->len - udp_offset);
183
184         /* csum segment if tunnel sets skb with csum. */
185         if (unlikely(uh->check)) {
186                 struct iphdr *iph = ip_hdr(skb);
187
188                 uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
189                                                skb->len - udp_offset,
190                                                IPPROTO_UDP, 0);
191                 uh->check = csum_fold(skb_checksum(skb, udp_offset,
192                                       skb->len - udp_offset, 0));
193
194                 if (uh->check == 0)
195                         uh->check = CSUM_MANGLED_0;
196
197         }
198         skb->ip_summed = CHECKSUM_NONE;
199 }
200
201 static int handle_offloads(struct sk_buff *skb)
202 {
203         if (skb_is_gso(skb)) {
204                 OVS_GSO_CB(skb)->fix_segment = vxlan_gso;
205         } else {
206                 if (skb->ip_summed != CHECKSUM_PARTIAL)
207                         skb->ip_summed = CHECKSUM_NONE;
208         }
209         return 0;
210 }
211
212 int vxlan_xmit_skb(struct net *net, struct vxlan_sock *vs,
213                    struct rtable *rt, struct sk_buff *skb,
214                    __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
215                    __be16 src_port, __be16 dst_port, __be32 vni)
216 {
217         struct vxlanhdr *vxh;
218         struct udphdr *uh;
219         int min_headroom;
220         int err;
221
222         skb_reset_inner_headers(skb);
223
224         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
225                         + VXLAN_HLEN + sizeof(struct iphdr)
226                         + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
227
228         /* Need space for new headers (invalidates iph ptr) */
229         err = skb_cow_head(skb, min_headroom);
230         if (unlikely(err))
231                 return err;
232
233         if (vlan_tx_tag_present(skb)) {
234                 if (unlikely(!__vlan_put_tag(skb,
235                                                 skb->vlan_proto,
236                                                 vlan_tx_tag_get(skb))))
237                         return -ENOMEM;
238
239                 vlan_set_tci(skb, 0);
240         }
241
242         vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
243         vxh->vx_flags = htonl(VXLAN_FLAGS);
244         vxh->vx_vni = vni;
245
246         __skb_push(skb, sizeof(*uh));
247         skb_reset_transport_header(skb);
248         uh = udp_hdr(skb);
249
250         uh->dest = dst_port;
251         uh->source = src_port;
252
253         uh->len = htons(skb->len);
254         uh->check = 0;
255
256         vxlan_set_owner(vs->sock->sk, skb);
257
258         err = handle_offloads(skb);
259         if (err)
260                 return err;
261
262         return iptunnel_xmit(net, rt, skb, src, dst,
263                         IPPROTO_UDP, tos, ttl, df);
264 }
265
266 static void rcu_free_vs(struct rcu_head *rcu)
267 {
268         struct vxlan_sock *vs = container_of(rcu, struct vxlan_sock, rcu);
269
270         kfree(vs);
271 }
272
273 static void vxlan_del_work(struct work_struct *work)
274 {
275         struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
276
277         sk_release_kernel(vs->sock->sk);
278         call_rcu(&vs->rcu, rcu_free_vs);
279         vxlan_cleanup_module();
280 }
281
282 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
283                                               vxlan_rcv_t *rcv, void *data)
284 {
285         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
286         struct vxlan_sock *vs;
287         struct sock *sk;
288         struct sockaddr_in vxlan_addr = {
289                 .sin_family = AF_INET,
290                 .sin_addr.s_addr = htonl(INADDR_ANY),
291                 .sin_port = port,
292         };
293         int rc;
294
295         vs = kmalloc(sizeof(*vs), GFP_KERNEL);
296         if (!vs) {
297                 pr_debug("memory alocation failure\n");
298                 return ERR_PTR(-ENOMEM);
299         }
300
301         INIT_WORK(&vs->del_work, vxlan_del_work);
302
303         /* Create UDP socket for encapsulation receive. */
304         rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
305         if (rc < 0) {
306                 pr_debug("UDP socket create failed\n");
307                 kfree(vs);
308                 return ERR_PTR(rc);
309         }
310
311         /* Put in proper namespace */
312         sk = vs->sock->sk;
313         sk_change_net(sk, net);
314
315         rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
316                         sizeof(vxlan_addr));
317         if (rc < 0) {
318                 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
319                                 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
320                 sk_release_kernel(sk);
321                 kfree(vs);
322                 return ERR_PTR(rc);
323         }
324         vs->rcv = rcv;
325         vs->data = data;
326
327         /* Disable multicast loopback */
328         inet_sk(sk)->mc_loop = 0;
329         spin_lock(&vn->sock_lock);
330         hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
331         spin_unlock(&vn->sock_lock);
332
333         /* Mark socket as an encapsulation socket. */
334         udp_sk(sk)->encap_type = 1;
335         udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
336         udp_encap_enable();
337         return vs;
338 }
339
340 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
341                                   vxlan_rcv_t *rcv, void *data,
342                                   bool no_share)
343 {
344         struct vxlan_net *vn;
345         struct vxlan_sock *vs;
346         int err;
347
348         err = vxlan_init_module();
349         if (err)
350                 return ERR_PTR(err);
351
352         vn = net_generic(net, vxlan_net_id);
353         vs = vxlan_socket_create(net, port, rcv, data);
354         return vs;
355 }
356
357 void vxlan_sock_release(struct vxlan_sock *vs)
358 {
359         struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
360
361         spin_lock(&vn->sock_lock);
362         hlist_del_rcu(&vs->hlist);
363         spin_unlock(&vn->sock_lock);
364
365         queue_work(&vs->del_work);
366 }
367
368 static int vxlan_init_net(struct net *net)
369 {
370         struct vxlan_net *vn = net_generic(net, vxlan_net_id);
371         unsigned int h;
372
373         spin_lock_init(&vn->sock_lock);
374
375         for (h = 0; h < PORT_HASH_SIZE; ++h)
376                 INIT_HLIST_HEAD(&vn->sock_list[h]);
377
378         return 0;
379 }
380
381 static struct pernet_operations vxlan_net_ops = {
382         .init = vxlan_init_net,
383         .id   = &vxlan_net_id,
384         .size = sizeof(struct vxlan_net),
385 };
386
387 static int refcnt;
388 static DEFINE_MUTEX(init_lock);
389 DEFINE_COMPAT_PNET_REG_FUNC(device);
390
391 static int vxlan_init_module(void)
392 {
393         int err = 0;
394
395         mutex_lock(&init_lock);
396         if (refcnt)
397                 goto out;
398         err = register_pernet_device(&vxlan_net_ops);
399 out:
400         if (!err)
401                 refcnt++;
402         mutex_unlock(&init_lock);
403         return err;
404 }
405
406 static void vxlan_cleanup_module(void)
407 {
408         mutex_lock(&init_lock);
409         refcnt--;
410         if (refcnt)
411                 goto out;
412         unregister_pernet_device(&vxlan_net_ops);
413 out:
414         mutex_unlock(&init_lock);
415 }