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 VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
61 #define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
63 /* VXLAN protocol header */
69 /* Callback from net/ipv4/udp.c to receive packets */
70 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
72 struct vxlan_sock *vs;
75 /* Need Vxlan and inner Ethernet header to be present */
76 if (!pskb_may_pull(skb, VXLAN_HLEN))
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));
88 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
91 vs = rcu_dereference_sk_user_data(sk);
95 vs->rcv(vs, skb, vxh->vx_vni);
99 /* Consume bad packet */
104 /* Return non vxlan pkt */
108 static void vxlan_sock_put(struct sk_buff *skb)
113 /* On transmit, associate with the tunnel socket */
114 static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
119 skb->destructor = vxlan_sock_put;
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
127 __be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
129 unsigned int range = (port_max - port_min) + 1;
132 hash = skb_get_rxhash(skb);
134 hash = jhash(skb->data, 2 * ETH_ALEN,
135 (__force u32) skb->protocol);
137 return htons((((u64) hash * range) >> 32) + port_min);
140 static void vxlan_gso(struct sk_buff *skb)
142 int udp_offset = skb_transport_offset(skb);
146 uh->len = htons(skb->len - udp_offset);
148 /* csum segment if tunnel sets skb with csum. */
149 if (unlikely(uh->check)) {
150 struct iphdr *iph = ip_hdr(skb);
152 uh->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
153 skb->len - udp_offset,
155 uh->check = csum_fold(skb_checksum(skb, udp_offset,
156 skb->len - udp_offset, 0));
159 uh->check = CSUM_MANGLED_0;
162 skb->ip_summed = CHECKSUM_NONE;
165 static int handle_offloads(struct sk_buff *skb)
167 if (skb_is_gso(skb)) {
168 OVS_GSO_CB(skb)->fix_segment = vxlan_gso;
170 if (skb->ip_summed != CHECKSUM_PARTIAL)
171 skb->ip_summed = CHECKSUM_NONE;
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)
181 struct vxlanhdr *vxh;
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);
190 /* Need space for new headers (invalidates iph ptr) */
191 err = skb_cow_head(skb, min_headroom);
195 if (vlan_tx_tag_present(skb)) {
196 if (unlikely(!__vlan_put_tag(skb,
198 vlan_tx_tag_get(skb))))
201 vlan_set_tci(skb, 0);
204 skb_reset_inner_headers(skb);
206 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
207 vxh->vx_flags = htonl(VXLAN_FLAGS);
210 __skb_push(skb, sizeof(*uh));
211 skb_reset_transport_header(skb);
215 uh->source = src_port;
217 uh->len = htons(skb->len);
220 vxlan_set_owner(vs->sock->sk, skb);
222 err = handle_offloads(skb);
226 return iptunnel_xmit(rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df);
229 static void rcu_free_vs(struct rcu_head *rcu)
231 struct vxlan_sock *vs = container_of(rcu, struct vxlan_sock, rcu);
236 static void vxlan_del_work(struct work_struct *work)
238 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
240 sk_release_kernel(vs->sock->sk);
241 call_rcu(&vs->rcu, rcu_free_vs);
244 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
245 vxlan_rcv_t *rcv, void *data)
247 struct vxlan_sock *vs;
249 struct sockaddr_in vxlan_addr = {
250 .sin_family = AF_INET,
251 .sin_addr.s_addr = htonl(INADDR_ANY),
256 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
258 pr_debug("memory alocation failure\n");
259 return ERR_PTR(-ENOMEM);
262 INIT_WORK(&vs->del_work, vxlan_del_work);
264 /* Create UDP socket for encapsulation receive. */
265 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
267 pr_debug("UDP socket create failed\n");
272 /* Put in proper namespace */
274 sk_change_net(sk, net);
276 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
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);
288 /* Disable multicast loopback */
289 inet_sk(sk)->mc_loop = 0;
290 rcu_assign_sk_user_data(vs->sock->sk, vs);
292 /* Mark socket as an encapsulation socket. */
293 udp_sk(sk)->encap_type = 1;
294 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
299 struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
300 vxlan_rcv_t *rcv, void *data,
303 return vxlan_socket_create(net, port, rcv, data);
306 void vxlan_sock_release(struct vxlan_sock *vs)
309 rcu_assign_sk_user_data(vs->sock->sk, NULL);
311 queue_work(system_wq, &vs->del_work);