datapath: Enable OVS GSO to be used up to 3.18 if necessary.
[cascardo/ovs.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 #ifndef USE_UPSTREAM_VXLAN
22
23 #include <linux/version.h>
24
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26
27 #include <linux/kernel.h>
28 #include <linux/types.h>
29 #include <linux/module.h>
30 #include <linux/errno.h>
31 #include <linux/slab.h>
32 #include <linux/skbuff.h>
33 #include <linux/rculist.h>
34 #include <linux/netdevice.h>
35 #include <linux/in.h>
36 #include <linux/ip.h>
37 #include <linux/udp.h>
38 #include <linux/igmp.h>
39 #include <linux/etherdevice.h>
40 #include <linux/if_ether.h>
41 #include <linux/if_vlan.h>
42 #include <linux/hash.h>
43 #include <linux/ethtool.h>
44 #include <net/arp.h>
45 #include <net/ndisc.h>
46 #include <net/ip.h>
47 #include <net/gre.h>
48 #include <net/ip_tunnels.h>
49 #include <net/icmp.h>
50 #include <net/udp.h>
51 #include <net/udp_tunnel.h>
52 #include <net/rtnetlink.h>
53 #include <net/route.h>
54 #include <net/dsfield.h>
55 #include <net/inet_ecn.h>
56 #include <net/net_namespace.h>
57 #include <net/netns/generic.h>
58 #include <net/vxlan.h>
59
60 #include "compat.h"
61 #include "datapath.h"
62 #include "gso.h"
63 #include "vlan.h"
64
65 /* VXLAN protocol header */
66 struct vxlanhdr {
67         __be32 vx_flags;
68         __be32 vx_vni;
69 };
70
71 /* Callback from net/ipv4/udp.c to receive packets */
72 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
73 {
74         struct vxlan_sock *vs;
75         struct vxlanhdr *vxh;
76         u32 flags, vni;
77         struct vxlan_metadata md = {0};
78
79         /* Need Vxlan and inner Ethernet header to be present */
80         if (!pskb_may_pull(skb, VXLAN_HLEN))
81                 goto error;
82
83         vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
84         flags = ntohl(vxh->vx_flags);
85         vni = ntohl(vxh->vx_vni);
86
87         if (flags & VXLAN_HF_VNI) {
88                 flags &= ~VXLAN_HF_VNI;
89         } else {
90                 /* VNI flag always required to be set */
91                 goto bad_flags;
92         }
93
94         if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
95                 goto drop;
96
97         vs = rcu_dereference_sk_user_data(sk);
98         if (!vs)
99                 goto drop;
100
101         /* For backwards compatibility, only allow reserved fields to be
102         * used by VXLAN extensions if explicitly requested.
103         */
104         if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
105                 struct vxlanhdr_gbp *gbp;
106
107                 gbp = (struct vxlanhdr_gbp *)vxh;
108                 md.gbp = ntohs(gbp->policy_id);
109
110                 if (gbp->dont_learn)
111                         md.gbp |= VXLAN_GBP_DONT_LEARN;
112
113                 if (gbp->policy_applied)
114                         md.gbp |= VXLAN_GBP_POLICY_APPLIED;
115
116                 flags &= ~VXLAN_GBP_USED_BITS;
117         }
118
119         if (flags || (vni & 0xff)) {
120                 /* If there are any unprocessed flags remaining treat
121                 * this as a malformed packet. This behavior diverges from
122                 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
123                 * in reserved fields are to be ignored. The approach here
124                 * maintains compatbility with previous stack code, and also
125                 * is more robust and provides a little more security in
126                 * adding extensions to VXLAN.
127                 */
128
129                 goto bad_flags;
130         }
131
132         md.vni = vxh->vx_vni;
133         vs->rcv(vs, skb, &md);
134         return 0;
135
136 drop:
137         /* Consume bad packet */
138         kfree_skb(skb);
139         return 0;
140 bad_flags:
141         pr_debug("invalid vxlan flags=%#x vni=%#x\n",
142                  ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
143
144 error:
145         /* Return non vxlan pkt */
146         return 1;
147 }
148
149 static void vxlan_sock_put(struct sk_buff *skb)
150 {
151         sock_put(skb->sk);
152 }
153
154 /* On transmit, associate with the tunnel socket */
155 static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
156 {
157         skb_orphan(skb);
158         sock_hold(sk);
159         skb->sk = sk;
160         skb->destructor = vxlan_sock_put;
161 }
162
163 static void vxlan_gso(struct sk_buff *skb)
164 {
165         int udp_offset = skb_transport_offset(skb);
166         struct udphdr *uh;
167
168         uh = udp_hdr(skb);
169         uh->len = htons(skb->len - udp_offset);
170
171         /* csum segment if tunnel sets skb with csum. */
172         if (unlikely(uh->check)) {
173                 struct iphdr *iph = ip_hdr(skb);
174
175                 uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
176                                                skb->len - udp_offset,
177                                                IPPROTO_UDP, 0);
178                 uh->check = csum_fold(skb_checksum(skb, udp_offset,
179                                       skb->len - udp_offset, 0));
180
181                 if (uh->check == 0)
182                         uh->check = CSUM_MANGLED_0;
183
184         }
185         skb->ip_summed = CHECKSUM_NONE;
186 }
187
188 static struct sk_buff *handle_offloads(struct sk_buff *skb)
189 {
190         return ovs_iptunnel_handle_offloads(skb, false, vxlan_gso);
191 }
192
193 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
194                                 struct vxlan_metadata *md)
195 {
196         struct vxlanhdr_gbp *gbp;
197
198         if (!md->gbp)
199                 return;
200
201         gbp = (struct vxlanhdr_gbp *)vxh;
202         vxh->vx_flags |= htonl(VXLAN_HF_GBP);
203
204         if (md->gbp & VXLAN_GBP_DONT_LEARN)
205                 gbp->dont_learn = 1;
206
207         if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
208                 gbp->policy_applied = 1;
209
210         gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
211 }
212
213 int vxlan_xmit_skb(struct vxlan_sock *vs,
214                    struct rtable *rt, struct sk_buff *skb,
215                    __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
216                    __be16 src_port, __be16 dst_port,
217                    struct vxlan_metadata *md, bool xnet, u32 vxflags)
218 {
219         struct vxlanhdr *vxh;
220         struct udphdr *uh;
221         int min_headroom;
222         int err;
223
224         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
225                         + VXLAN_HLEN + sizeof(struct iphdr)
226                         + (skb_vlan_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                 kfree_skb(skb);
232                 return err;
233         }
234
235         if (skb_vlan_tag_present(skb)) {
236                 if (unlikely(!vlan_insert_tag_set_proto(skb,
237                                                         skb->vlan_proto,
238                                                         skb_vlan_tag_get(skb))))
239                         return -ENOMEM;
240
241                 vlan_set_tci(skb, 0);
242         }
243
244         skb_reset_inner_headers(skb);
245
246         vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
247         vxh->vx_flags = htonl(VXLAN_HF_VNI);
248         vxh->vx_vni = md->vni;
249
250         if (vxflags & VXLAN_F_GBP)
251                 vxlan_build_gbp_hdr(vxh, vxflags, md);
252
253         __skb_push(skb, sizeof(*uh));
254         skb_reset_transport_header(skb);
255         uh = udp_hdr(skb);
256
257         uh->dest = dst_port;
258         uh->source = src_port;
259
260         uh->len = htons(skb->len);
261         uh->check = 0;
262
263         vxlan_set_owner(vs->sock->sk, skb);
264
265         skb = handle_offloads(skb);
266         if (IS_ERR(skb))
267                 return PTR_ERR(skb);
268
269         return iptunnel_xmit(vs->sock->sk, rt, skb, src, dst, IPPROTO_UDP,
270                              tos, ttl, df, xnet);
271 }
272
273 static void rcu_free_vs(struct rcu_head *rcu)
274 {
275         struct vxlan_sock *vs = container_of(rcu, struct vxlan_sock, rcu);
276
277         kfree(vs);
278 }
279
280 static void vxlan_del_work(struct work_struct *work)
281 {
282         struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
283
284         sk_release_kernel(vs->sock->sk);
285         call_rcu(&vs->rcu, rcu_free_vs);
286 }
287
288 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
289                                         __be16 port, u32 flags)
290 {
291         struct socket *sock;
292         struct udp_port_cfg udp_conf;
293         int err;
294
295         memset(&udp_conf, 0, sizeof(udp_conf));
296
297         if (ipv6) {
298                 udp_conf.family = AF_INET6;
299                 /* The checksum flag is silently ignored but it
300                  * doesn't make sense here anyways because OVS enables
301                  * checksums on a finer granularity than per-socket.
302                  */
303         } else {
304                 udp_conf.family = AF_INET;
305                 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
306         }
307
308         udp_conf.local_udp_port = port;
309
310         /* Open UDP socket */
311         err = udp_sock_create(net, &udp_conf, &sock);
312         if (err < 0)
313                 return ERR_PTR(err);
314
315         /* Disable multicast loopback */
316         inet_sk(sock->sk)->mc_loop = 0;
317
318         return sock;
319 }
320
321 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
322                                               vxlan_rcv_t *rcv, void *data, u32 flags)
323 {
324         struct vxlan_sock *vs;
325         struct socket *sock;
326         struct sock *sk;
327
328         vs = kmalloc(sizeof(*vs), GFP_KERNEL);
329         if (!vs) {
330                 pr_debug("memory alocation failure\n");
331                 return ERR_PTR(-ENOMEM);
332         }
333
334         INIT_WORK(&vs->del_work, vxlan_del_work);
335
336         sock = vxlan_create_sock(net, false, port, flags);
337         if (IS_ERR(sock)) {
338                 kfree(vs);
339                 return ERR_CAST(sock);
340         }
341
342         vs->sock = sock;
343         sk = sock->sk;
344         vs->rcv = rcv;
345         vs->data = data;
346         vs->flags = (flags & VXLAN_F_RCV_FLAGS);
347
348         /* Disable multicast loopback */
349         inet_sk(sk)->mc_loop = 0;
350         rcu_assign_sk_user_data(vs->sock->sk, vs);
351
352         /* Mark socket as an encapsulation socket. */
353         udp_sk(sk)->encap_type = 1;
354         udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
355         udp_encap_enable();
356         return vs;
357 }
358
359 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
360                                   vxlan_rcv_t *rcv, void *data,
361                                   bool no_share, u32 flags)
362 {
363         return vxlan_socket_create(net, port, rcv, data, flags);
364 }
365
366 void vxlan_sock_release(struct vxlan_sock *vs)
367 {
368         ASSERT_OVSL();
369         rcu_assign_sk_user_data(vs->sock->sk, NULL);
370
371         queue_work(system_wq, &vs->del_work);
372 }
373
374 #endif /* !USE_UPSTREAM_VXLAN */