2 * Copyright (c) 2007-2013 Nicira, Inc.
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.
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.
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
18 * This code is derived from kernel vxlan module.
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
41 #include <net/ndisc.h>
43 #include <net/ip_tunnels.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>
59 #define PORT_HASH_BITS 8
60 #define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
62 /* IP header + UDP + VXLAN + Ethernet header */
63 #define VXLAN_HEADROOM (20 + 8 + 8 + 14)
64 #define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
66 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
68 /* VXLAN protocol header */
74 static int vxlan_net_id;
76 static int vxlan_init_module(void);
77 static void vxlan_cleanup_module(void);
79 /* per-network namespace private data for this module */
81 struct hlist_head sock_list[PORT_HASH_SIZE];
85 /* Socket hash table head */
86 static inline struct hlist_head *vs_head(struct net *net, __be16 port)
88 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
90 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
93 /* Find VXLAN socket based on network namespace and UDP port */
95 static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
97 struct vxlan_sock *vs;
99 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
100 if (inet_sport(vs->sock->sk) == port)
106 /* Callback from net/ipv4/udp.c to receive packets */
107 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
109 struct vxlan_sock *vs;
110 struct vxlanhdr *vxh;
112 /* Need Vxlan and inner Ethernet header to be present */
113 if (!pskb_may_pull(skb, VXLAN_HLEN))
116 /* Return packets with reserved bits set */
117 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
118 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
119 (vxh->vx_vni & htonl(0xff))) {
120 pr_warn("invalid vxlan flags=%#x vni=%#x\n",
121 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
125 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
128 vs = vxlan_find_sock(sock_net(sk), inet_sport(sk));
132 vs->rcv(vs, skb, vxh->vx_vni);
136 /* Consume bad packet */
141 /* Return non vxlan pkt */
145 static void vxlan_sock_put(struct sk_buff *skb)
150 /* On transmit, associate with the tunnel socket */
151 static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
156 skb->destructor = vxlan_sock_put;
159 /* Compute source port for outgoing packet
160 * first choice to use L4 flow hash since it will spread
161 * better and maybe available from hardware
162 * secondary choice is to use jhash on the Ethernet header
164 __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
166 unsigned int range = (port_max - port_min) + 1;
169 hash = skb_get_rxhash(skb);
171 hash = jhash(skb->data, 2 * ETH_ALEN,
172 (__force u32) skb->protocol);
174 return htons((((u64) hash * range) >> 32) + port_min);
177 static void vxlan_gso(struct sk_buff *skb)
179 int udp_offset = skb_transport_offset(skb);
183 uh->len = htons(skb->len - udp_offset);
185 /* csum segment if tunnel sets skb with csum. */
186 if (unlikely(uh->check)) {
187 struct iphdr *iph = ip_hdr(skb);
189 uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
190 skb->len - udp_offset,
192 uh->check = csum_fold(skb_checksum(skb, udp_offset,
193 skb->len - udp_offset, 0));
196 uh->check = CSUM_MANGLED_0;
199 skb->ip_summed = CHECKSUM_NONE;
202 static int handle_offloads(struct sk_buff *skb)
204 if (skb_is_gso(skb)) {
205 OVS_GSO_CB(skb)->fix_segment = vxlan_gso;
207 if (skb->ip_summed != CHECKSUM_PARTIAL)
208 skb->ip_summed = CHECKSUM_NONE;
213 int vxlan_xmit_skb(struct net *net, 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, __be32 vni)
218 struct vxlanhdr *vxh;
223 skb_reset_inner_headers(skb);
225 min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
226 + VXLAN_HLEN + sizeof(struct iphdr)
227 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
229 /* Need space for new headers (invalidates iph ptr) */
230 err = skb_cow_head(skb, min_headroom);
234 if (unlikely(vlan_deaccel_tag(skb)))
237 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
238 vxh->vx_flags = htonl(VXLAN_FLAGS);
241 __skb_push(skb, sizeof(*uh));
242 skb_reset_transport_header(skb);
246 uh->source = src_port;
248 uh->len = htons(skb->len);
251 vxlan_set_owner(vs->sock->sk, skb);
253 err = handle_offloads(skb);
257 return iptunnel_xmit(net, rt, skb, src, dst,
258 IPPROTO_UDP, tos, ttl, df);
261 static void rcu_free_vs(struct rcu_head *rcu)
263 struct vxlan_sock *vs = container_of(rcu, struct vxlan_sock, rcu);
268 static void vxlan_del_work(struct work_struct *work)
270 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
272 sk_release_kernel(vs->sock->sk);
273 call_rcu(&vs->rcu, rcu_free_vs);
274 vxlan_cleanup_module();
277 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
278 vxlan_rcv_t *rcv, void *data)
280 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
281 struct vxlan_sock *vs;
283 struct sockaddr_in vxlan_addr = {
284 .sin_family = AF_INET,
285 .sin_addr.s_addr = htonl(INADDR_ANY),
290 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
292 pr_debug("memory alocation failure\n");
293 return ERR_PTR(-ENOMEM);
296 INIT_WORK(&vs->del_work, vxlan_del_work);
298 /* Create UDP socket for encapsulation receive. */
299 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
301 pr_debug("UDP socket create failed\n");
306 /* Put in proper namespace */
308 sk_change_net(sk, net);
310 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
313 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
314 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
315 sk_release_kernel(sk);
322 /* Disable multicast loopback */
323 inet_sk(sk)->mc_loop = 0;
324 spin_lock(&vn->sock_lock);
325 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
326 spin_unlock(&vn->sock_lock);
328 /* Mark socket as an encapsulation socket. */
329 udp_sk(sk)->encap_type = 1;
330 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
335 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
336 vxlan_rcv_t *rcv, void *data,
339 struct vxlan_net *vn;
340 struct vxlan_sock *vs;
343 err = vxlan_init_module();
347 vn = net_generic(net, vxlan_net_id);
348 vs = vxlan_socket_create(net, port, rcv, data);
352 void vxlan_sock_release(struct vxlan_sock *vs)
354 struct vxlan_net *vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
356 spin_lock(&vn->sock_lock);
357 hlist_del_rcu(&vs->hlist);
358 spin_unlock(&vn->sock_lock);
360 queue_work(&vs->del_work);
363 static int vxlan_init_net(struct net *net)
365 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
368 spin_lock_init(&vn->sock_lock);
370 for (h = 0; h < PORT_HASH_SIZE; ++h)
371 INIT_HLIST_HEAD(&vn->sock_list[h]);
376 static struct pernet_operations vxlan_net_ops = {
377 .init = vxlan_init_net,
379 .size = sizeof(struct vxlan_net),
383 static DEFINE_MUTEX(init_lock);
384 DEFINE_COMPAT_PNET_REG_FUNC(device);
386 static int vxlan_init_module(void)
390 mutex_lock(&init_lock);
393 err = register_pernet_device(&vxlan_net_ops);
397 mutex_unlock(&init_lock);
401 static void vxlan_cleanup_module(void)
403 mutex_lock(&init_lock);
407 unregister_pernet_device(&vxlan_net_ops);
409 mutex_unlock(&init_lock);