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