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