datapath: compat: introduce ovs_iptunnel_handle_offloads()
[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 #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 /* Callback from net/ipv4/udp.c to receive packets */
74 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
75 {
76         struct vxlan_sock *vs;
77         struct vxlanhdr *vxh;
78
79         /* Need Vxlan and inner Ethernet header to be present */
80         if (!pskb_may_pull(skb, VXLAN_HLEN))
81                 goto error;
82
83         /* Return packets with reserved bits set */
84         vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
85         if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
86             (vxh->vx_vni & htonl(0xff))) {
87                 pr_warn("invalid vxlan flags=%#x vni=%#x\n",
88                         ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
89                 goto error;
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         vs->rcv(vs, skb, vxh->vx_vni);
100         return 0;
101
102 drop:
103         /* Consume bad packet */
104         kfree_skb(skb);
105         return 0;
106
107 error:
108         /* Return non vxlan pkt */
109         return 1;
110 }
111
112 static void vxlan_sock_put(struct sk_buff *skb)
113 {
114         sock_put(skb->sk);
115 }
116
117 /* On transmit, associate with the tunnel socket */
118 static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
119 {
120         skb_orphan(skb);
121         sock_hold(sk);
122         skb->sk = sk;
123         skb->destructor = vxlan_sock_put;
124 }
125
126 /* Compute source port for outgoing packet
127  *   first choice to use L4 flow hash since it will spread
128  *     better and maybe available from hardware
129  *   secondary choice is to use jhash on the Ethernet header
130  */
131 __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
132 {
133         unsigned int range = (port_max - port_min) + 1;
134         u32 hash;
135
136         hash = skb_get_hash(skb);
137         if (!hash)
138                 hash = jhash(skb->data, 2 * ETH_ALEN,
139                              (__force u32) skb->protocol);
140
141         return htons((((u64) hash * range) >> 32) + port_min);
142 }
143
144 static void vxlan_gso(struct sk_buff *skb)
145 {
146         int udp_offset = skb_transport_offset(skb);
147         struct udphdr *uh;
148
149         uh = udp_hdr(skb);
150         uh->len = htons(skb->len - udp_offset);
151
152         /* csum segment if tunnel sets skb with csum. */
153         if (unlikely(uh->check)) {
154                 struct iphdr *iph = ip_hdr(skb);
155
156                 uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
157                                                skb->len - udp_offset,
158                                                IPPROTO_UDP, 0);
159                 uh->check = csum_fold(skb_checksum(skb, udp_offset,
160                                       skb->len - udp_offset, 0));
161
162                 if (uh->check == 0)
163                         uh->check = CSUM_MANGLED_0;
164
165         }
166         skb->ip_summed = CHECKSUM_NONE;
167 }
168
169 static struct sk_buff *handle_offloads(struct sk_buff *skb)
170 {
171         return ovs_iptunnel_handle_offloads(skb, false, vxlan_gso);
172 }
173
174 int vxlan_xmit_skb(struct vxlan_sock *vs,
175                    struct rtable *rt, struct sk_buff *skb,
176                    __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
177                    __be16 src_port, __be16 dst_port, __be32 vni)
178 {
179         struct vxlanhdr *vxh;
180         struct udphdr *uh;
181         int min_headroom;
182         int err;
183
184         min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
185                         + VXLAN_HLEN + sizeof(struct iphdr)
186                         + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
187
188         /* Need space for new headers (invalidates iph ptr) */
189         err = skb_cow_head(skb, min_headroom);
190         if (unlikely(err))
191                 return err;
192
193         if (vlan_tx_tag_present(skb)) {
194                 if (unlikely(!__vlan_put_tag(skb,
195                                                 skb->vlan_proto,
196                                                 vlan_tx_tag_get(skb))))
197                         return -ENOMEM;
198
199                 vlan_set_tci(skb, 0);
200         }
201
202         skb_reset_inner_headers(skb);
203
204         vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
205         vxh->vx_flags = htonl(VXLAN_FLAGS);
206         vxh->vx_vni = vni;
207
208         __skb_push(skb, sizeof(*uh));
209         skb_reset_transport_header(skb);
210         uh = udp_hdr(skb);
211
212         uh->dest = dst_port;
213         uh->source = src_port;
214
215         uh->len = htons(skb->len);
216         uh->check = 0;
217
218         vxlan_set_owner(vs->sock->sk, skb);
219
220         skb = handle_offloads(skb);
221         if (IS_ERR(skb))
222                 return 0;
223
224         return iptunnel_xmit(vs->sock->sk, rt, skb, src, dst, IPPROTO_UDP,
225                              tos, ttl, df, false);
226 }
227
228 static void rcu_free_vs(struct rcu_head *rcu)
229 {
230         struct vxlan_sock *vs = container_of(rcu, struct vxlan_sock, rcu);
231
232         kfree(vs);
233 }
234
235 static void vxlan_del_work(struct work_struct *work)
236 {
237         struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
238
239         sk_release_kernel(vs->sock->sk);
240         call_rcu(&vs->rcu, rcu_free_vs);
241 }
242
243 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
244                                               vxlan_rcv_t *rcv, void *data)
245 {
246         struct vxlan_sock *vs;
247         struct sock *sk;
248         struct sockaddr_in vxlan_addr = {
249                 .sin_family = AF_INET,
250                 .sin_addr.s_addr = htonl(INADDR_ANY),
251                 .sin_port = port,
252         };
253         int rc;
254
255         vs = kmalloc(sizeof(*vs), GFP_KERNEL);
256         if (!vs) {
257                 pr_debug("memory alocation failure\n");
258                 return ERR_PTR(-ENOMEM);
259         }
260
261         INIT_WORK(&vs->del_work, vxlan_del_work);
262
263         /* Create UDP socket for encapsulation receive. */
264         rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
265         if (rc < 0) {
266                 pr_debug("UDP socket create failed\n");
267                 kfree(vs);
268                 return ERR_PTR(rc);
269         }
270
271         /* Put in proper namespace */
272         sk = vs->sock->sk;
273         sk_change_net(sk, net);
274
275         rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
276                         sizeof(vxlan_addr));
277         if (rc < 0) {
278                 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
279                                 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
280                 sk_release_kernel(sk);
281                 kfree(vs);
282                 return ERR_PTR(rc);
283         }
284         vs->rcv = rcv;
285         vs->data = data;
286
287         /* Disable multicast loopback */
288         inet_sk(sk)->mc_loop = 0;
289         rcu_assign_sk_user_data(vs->sock->sk, vs);
290
291         /* Mark socket as an encapsulation socket. */
292         udp_sk(sk)->encap_type = 1;
293         udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
294         udp_encap_enable();
295         return vs;
296 }
297
298 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
299                                   vxlan_rcv_t *rcv, void *data,
300                                   bool no_share, bool ipv6)
301 {
302         return vxlan_socket_create(net, port, rcv, data);
303 }
304
305 void vxlan_sock_release(struct vxlan_sock *vs)
306 {
307         ASSERT_OVSL();
308         rcu_assign_sk_user_data(vs->sock->sk, NULL);
309
310         queue_work(system_wq, &vs->del_work);
311 }
312
313 #endif /* 3.12 */