datapath: Account for "vxlan: Eliminate dependency on UDP socket in transmit path"
[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/rtnetlink.h>
52 #include <net/route.h>
53 #include <net/dsfield.h>
54 #include <net/inet_ecn.h>
55 #include <net/net_namespace.h>
56 #include <net/netns/generic.h>
57 #include <net/vxlan.h>
58
59 #include "compat.h"
60 #include "datapath.h"
61 #include "gso.h"
62 #include "vlan.h"
63
64 #if LINUX_VERSION_CODE < KERNEL_VERSION(3,18,0)
65 /* VXLAN protocol header */
66 struct vxlanhdr {
67         __be32 vx_flags;
68         __be32 vx_vni;
69 };
70 #endif
71
72 /* Callback from net/ipv4/udp.c to receive packets */
73 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
74 {
75         struct vxlan_sock *vs;
76         struct vxlanhdr *vxh;
77         u32 flags, vni;
78         struct vxlan_metadata md = {0};
79
80         /* Need Vxlan and inner Ethernet header to be present */
81         if (!pskb_may_pull(skb, VXLAN_HLEN))
82                 goto error;
83
84         vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
85         flags = ntohl(vxh->vx_flags);
86         vni = ntohl(vxh->vx_vni);
87
88         if (flags & VXLAN_HF_VNI) {
89                 flags &= ~VXLAN_HF_VNI;
90         } else {
91                 /* VNI flag always required to be set */
92                 goto bad_flags;
93         }
94
95         if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
96                 goto drop;
97
98         vs = rcu_dereference_sk_user_data(sk);
99         if (!vs)
100                 goto drop;
101
102         /* For backwards compatibility, only allow reserved fields to be
103         * used by VXLAN extensions if explicitly requested.
104         */
105         if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
106                 struct vxlanhdr_gbp *gbp;
107
108                 gbp = (struct vxlanhdr_gbp *)vxh;
109                 md.gbp = ntohs(gbp->policy_id);
110
111                 if (gbp->dont_learn)
112                         md.gbp |= VXLAN_GBP_DONT_LEARN;
113
114                 if (gbp->policy_applied)
115                         md.gbp |= VXLAN_GBP_POLICY_APPLIED;
116
117                 flags &= ~VXLAN_GBP_USED_BITS;
118         }
119
120         if (flags || (vni & 0xff)) {
121                 /* If there are any unprocessed flags remaining treat
122                 * this as a malformed packet. This behavior diverges from
123                 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
124                 * in reserved fields are to be ignored. The approach here
125                 * maintains compatbility with previous stack code, and also
126                 * is more robust and provides a little more security in
127                 * adding extensions to VXLAN.
128                 */
129
130                 goto bad_flags;
131         }
132
133         md.vni = vxh->vx_vni;
134         vs->rcv(vs, skb, &md);
135         return 0;
136
137 drop:
138         /* Consume bad packet */
139         kfree_skb(skb);
140         return 0;
141 bad_flags:
142         pr_debug("invalid vxlan flags=%#x vni=%#x\n",
143                  ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
144
145 error:
146         /* Return non vxlan pkt */
147         return 1;
148 }
149
150 static void vxlan_sock_put(struct sk_buff *skb)
151 {
152         sock_put(skb->sk);
153 }
154
155 /* On transmit, associate with the tunnel socket */
156 static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
157 {
158         skb_orphan(skb);
159         sock_hold(sk);
160         skb->sk = sk;
161         skb->destructor = vxlan_sock_put;
162 }
163
164 /* Compute source port for outgoing packet
165  *   first choice to use L4 flow hash since it will spread
166  *     better and maybe available from hardware
167  *   secondary choice is to use jhash on the Ethernet header
168  */
169 __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
170 {
171         unsigned int range = (port_max - port_min) + 1;
172         u32 hash;
173
174         hash = skb_get_hash(skb);
175         if (!hash)
176                 hash = jhash(skb->data, 2 * ETH_ALEN,
177                              (__force u32) skb->protocol);
178
179         return htons((((u64) hash * range) >> 32) + port_min);
180 }
181
182 static void vxlan_gso(struct sk_buff *skb)
183 {
184         int udp_offset = skb_transport_offset(skb);
185         struct udphdr *uh;
186
187         uh = udp_hdr(skb);
188         uh->len = htons(skb->len - udp_offset);
189
190         /* csum segment if tunnel sets skb with csum. */
191         if (unlikely(uh->check)) {
192                 struct iphdr *iph = ip_hdr(skb);
193
194                 uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
195                                                skb->len - udp_offset,
196                                                IPPROTO_UDP, 0);
197                 uh->check = csum_fold(skb_checksum(skb, udp_offset,
198                                       skb->len - udp_offset, 0));
199
200                 if (uh->check == 0)
201                         uh->check = CSUM_MANGLED_0;
202
203         }
204         skb->ip_summed = CHECKSUM_NONE;
205 }
206
207 static struct sk_buff *handle_offloads(struct sk_buff *skb)
208 {
209         return ovs_iptunnel_handle_offloads(skb, false, vxlan_gso);
210 }
211
212 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
213                                 struct vxlan_metadata *md)
214 {
215         struct vxlanhdr_gbp *gbp;
216
217         gbp = (struct vxlanhdr_gbp *)vxh;
218         vxh->vx_flags |= htonl(VXLAN_HF_GBP);
219
220         if (md->gbp & VXLAN_GBP_DONT_LEARN)
221                 gbp->dont_learn = 1;
222
223         if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
224                 gbp->policy_applied = 1;
225
226         gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
227 }
228
229 int vxlan_xmit_skb(struct vxlan_sock *vs,
230                    struct rtable *rt, struct sk_buff *skb,
231                    __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
232                    __be16 src_port, __be16 dst_port,
233                    struct vxlan_metadata *md, bool xnet, u32 vxflags)
234 {
235         struct vxlanhdr *vxh;
236         struct udphdr *uh;
237         int min_headroom;
238         int err;
239
240         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
241                         + VXLAN_HLEN + sizeof(struct iphdr)
242                         + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
243
244         /* Need space for new headers (invalidates iph ptr) */
245         err = skb_cow_head(skb, min_headroom);
246         if (unlikely(err)) {
247                 kfree_skb(skb);
248                 return err;
249         }
250
251         if (skb_vlan_tag_present(skb)) {
252                 if (unlikely(!vlan_insert_tag_set_proto(skb,
253                                                         skb->vlan_proto,
254                                                         skb_vlan_tag_get(skb))))
255                         return -ENOMEM;
256
257                 vlan_set_tci(skb, 0);
258         }
259
260         skb_reset_inner_headers(skb);
261
262         vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
263         vxh->vx_flags = htonl(VXLAN_HF_VNI);
264         vxh->vx_vni = md->vni;
265
266         if (vxflags & VXLAN_F_GBP)
267                 vxlan_build_gbp_hdr(vxh, vxflags, md);
268
269         __skb_push(skb, sizeof(*uh));
270         skb_reset_transport_header(skb);
271         uh = udp_hdr(skb);
272
273         uh->dest = dst_port;
274         uh->source = src_port;
275
276         uh->len = htons(skb->len);
277         uh->check = 0;
278
279         vxlan_set_owner(vs->sock->sk, skb);
280
281         skb = handle_offloads(skb);
282         if (IS_ERR(skb))
283                 return PTR_ERR(skb);
284
285         return iptunnel_xmit(vs->sock->sk, rt, skb, src, dst, IPPROTO_UDP,
286                              tos, ttl, df, xnet);
287 }
288
289 static void rcu_free_vs(struct rcu_head *rcu)
290 {
291         struct vxlan_sock *vs = container_of(rcu, struct vxlan_sock, rcu);
292
293         kfree(vs);
294 }
295
296 static void vxlan_del_work(struct work_struct *work)
297 {
298         struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
299
300         sk_release_kernel(vs->sock->sk);
301         call_rcu(&vs->rcu, rcu_free_vs);
302 }
303
304 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
305                                               vxlan_rcv_t *rcv, void *data, u32 flags)
306 {
307         struct vxlan_sock *vs;
308         struct sock *sk;
309         struct sockaddr_in vxlan_addr = {
310                 .sin_family = AF_INET,
311                 .sin_addr.s_addr = htonl(INADDR_ANY),
312                 .sin_port = port,
313         };
314         int rc;
315
316         vs = kmalloc(sizeof(*vs), GFP_KERNEL);
317         if (!vs) {
318                 pr_debug("memory alocation failure\n");
319                 return ERR_PTR(-ENOMEM);
320         }
321
322         INIT_WORK(&vs->del_work, vxlan_del_work);
323
324         /* Create UDP socket for encapsulation receive. */
325         rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
326         if (rc < 0) {
327                 pr_debug("UDP socket create failed\n");
328                 kfree(vs);
329                 return ERR_PTR(rc);
330         }
331
332         /* Put in proper namespace */
333         sk = vs->sock->sk;
334         sk_change_net(sk, net);
335
336         rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
337                         sizeof(vxlan_addr));
338         if (rc < 0) {
339                 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
340                                 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
341                 sk_release_kernel(sk);
342                 kfree(vs);
343                 return ERR_PTR(rc);
344         }
345         vs->rcv = rcv;
346         vs->data = data;
347         vs->flags = (flags & VXLAN_F_RCV_FLAGS);
348
349         /* Disable multicast loopback */
350         inet_sk(sk)->mc_loop = 0;
351         rcu_assign_sk_user_data(vs->sock->sk, vs);
352
353         /* Mark socket as an encapsulation socket. */
354         udp_sk(sk)->encap_type = 1;
355         udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
356         udp_encap_enable();
357         return vs;
358 }
359
360 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
361                                   vxlan_rcv_t *rcv, void *data,
362                                   bool no_share, u32 flags)
363 {
364         return vxlan_socket_create(net, port, rcv, data, flags);
365 }
366
367 void vxlan_sock_release(struct vxlan_sock *vs)
368 {
369         ASSERT_OVSL();
370         rcu_assign_sk_user_data(vs->sock->sk, NULL);
371
372         queue_work(system_wq, &vs->del_work);
373 }
374
375 #endif /* !USE_UPSTREAM_VXLAN */