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 #include <linux/version.h>
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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>
43 #include <net/ndisc.h>
46 #include <net/ip_tunnels.h>
49 #include <net/udp_tunnel.h>
50 #include <net/rtnetlink.h>
51 #include <net/route.h>
52 #include <net/dsfield.h>
53 #include <net/inet_ecn.h>
54 #include <net/net_namespace.h>
55 #include <net/netns/generic.h>
56 #include <net/vxlan.h>
63 #ifndef USE_UPSTREAM_VXLAN
65 /* VXLAN protocol header */
71 /* Callback from net/ipv4/udp.c to receive packets */
72 static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
74 struct vxlan_sock *vs;
77 struct vxlan_metadata md = {0};
79 /* Need Vxlan and inner Ethernet header to be present */
80 if (!pskb_may_pull(skb, VXLAN_HLEN))
83 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
84 flags = ntohl(vxh->vx_flags);
85 vni = ntohl(vxh->vx_vni);
87 if (flags & VXLAN_HF_VNI) {
88 flags &= ~VXLAN_HF_VNI;
90 /* VNI flag always required to be set */
94 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
97 vs = rcu_dereference_sk_user_data(sk);
101 /* For backwards compatibility, only allow reserved fields to be
102 * used by VXLAN extensions if explicitly requested.
104 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
105 struct vxlanhdr_gbp *gbp;
107 gbp = (struct vxlanhdr_gbp *)vxh;
108 md.gbp = ntohs(gbp->policy_id);
111 md.gbp |= VXLAN_GBP_DONT_LEARN;
113 if (gbp->policy_applied)
114 md.gbp |= VXLAN_GBP_POLICY_APPLIED;
116 flags &= ~VXLAN_GBP_USED_BITS;
119 if (flags || (vni & 0xff)) {
120 /* If there are any unprocessed flags remaining treat
121 * this as a malformed packet. This behavior diverges from
122 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
123 * in reserved fields are to be ignored. The approach here
124 * maintains compatbility with previous stack code, and also
125 * is more robust and provides a little more security in
126 * adding extensions to VXLAN.
132 md.vni = vxh->vx_vni;
133 vs->rcv(vs, skb, &md);
137 /* Consume bad packet */
141 pr_debug("invalid vxlan flags=%#x vni=%#x\n",
142 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
145 /* Return non vxlan pkt */
149 static void vxlan_sock_put(struct sk_buff *skb)
154 /* On transmit, associate with the tunnel socket */
155 static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
160 skb->destructor = vxlan_sock_put;
163 static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
164 struct vxlan_metadata *md)
166 struct vxlanhdr_gbp *gbp;
171 gbp = (struct vxlanhdr_gbp *)vxh;
172 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
174 if (md->gbp & VXLAN_GBP_DONT_LEARN)
177 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
178 gbp->policy_applied = 1;
180 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
183 int rpl_vxlan_xmit_skb(struct rtable *rt, struct sock *sk, struct sk_buff *skb,
184 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
185 __be16 src_port, __be16 dst_port,
186 struct vxlan_metadata *md, bool xnet, u32 vxflags)
188 struct vxlanhdr *vxh;
191 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
193 min_headroom = LL_RESERVED_SPACE(rt_dst(rt).dev) + rt_dst(rt).header_len
194 + VXLAN_HLEN + sizeof(struct iphdr)
195 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
197 /* Need space for new headers (invalidates iph ptr) */
198 err = skb_cow_head(skb, min_headroom);
204 skb = vlan_hwaccel_push_inside(skb);
208 skb = udp_tunnel_handle_offloads(skb, udp_sum, true);
212 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
213 vxh->vx_flags = htonl(VXLAN_HF_VNI);
214 vxh->vx_vni = md->vni;
216 if (vxflags & VXLAN_F_GBP)
217 vxlan_build_gbp_hdr(vxh, vxflags, md);
219 vxlan_set_owner(sk, skb);
221 ovs_skb_set_inner_protocol(skb, htons(ETH_P_TEB));
223 return udp_tunnel_xmit_skb(rt, sk, skb, src, dst, tos,
224 ttl, df, src_port, dst_port, xnet,
227 EXPORT_SYMBOL_GPL(rpl_vxlan_xmit_skb);
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 udp_tunnel_sock_release(vs->sock);
241 call_rcu(&vs->rcu, rcu_free_vs);
244 static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
245 __be16 port, u32 flags)
248 struct udp_port_cfg udp_conf;
251 memset(&udp_conf, 0, sizeof(udp_conf));
254 udp_conf.family = AF_INET6;
255 /* The checksum flag is silently ignored but it
256 * doesn't make sense here anyways because OVS enables
257 * checksums on a finer granularity than per-socket.
260 udp_conf.family = AF_INET;
261 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
264 udp_conf.local_udp_port = port;
266 /* Open UDP socket */
267 err = udp_sock_create(net, &udp_conf, &sock);
274 static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
275 vxlan_rcv_t *rcv, void *data, u32 flags)
277 struct vxlan_sock *vs;
279 struct udp_tunnel_sock_cfg tunnel_cfg;
281 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
283 pr_debug("memory alocation failure\n");
284 return ERR_PTR(-ENOMEM);
287 INIT_WORK(&vs->del_work, vxlan_del_work);
289 sock = vxlan_create_sock(net, false, port, flags);
292 return ERR_CAST(sock);
298 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
300 tunnel_cfg.sk_user_data = vs;
301 tunnel_cfg.encap_type = 1;
302 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
303 tunnel_cfg.encap_destroy = NULL;
305 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
310 struct vxlan_sock *rpl_vxlan_sock_add(struct net *net, __be16 port,
311 vxlan_rcv_t *rcv, void *data,
312 bool no_share, u32 flags)
314 return vxlan_socket_create(net, port, rcv, data, flags);
316 EXPORT_SYMBOL_GPL(rpl_vxlan_sock_add);
318 void rpl_vxlan_sock_release(struct vxlan_sock *vs)
322 queue_work(system_wq, &vs->del_work);
324 EXPORT_SYMBOL_GPL(rpl_vxlan_sock_release);
326 #endif /* !USE_UPSTREAM_VXLAN */