blob: b061c98474ee6553af6d9c05cfbe85c97eafb54c [file] [log] [blame]
stephen hemmingerd3428942012-10-01 12:32:35 +00001/*
Rami Roseneb5ce432012-11-13 13:29:15 +00002 * VXLAN: Virtual eXtensible Local Area Network
stephen hemmingerd3428942012-10-01 12:32:35 +00003 *
stephen hemminger3b8df3c2013-04-27 11:31:52 +00004 * Copyright (c) 2012-2013 Vyatta Inc.
stephen hemmingerd3428942012-10-01 12:32:35 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * TODO
stephen hemmingerd3428942012-10-01 12:32:35 +000011 * - IPv6 (not in RFC)
12 */
13
14#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15
16#include <linux/kernel.h>
17#include <linux/types.h>
18#include <linux/module.h>
19#include <linux/errno.h>
20#include <linux/slab.h>
21#include <linux/skbuff.h>
22#include <linux/rculist.h>
23#include <linux/netdevice.h>
24#include <linux/in.h>
25#include <linux/ip.h>
26#include <linux/udp.h>
27#include <linux/igmp.h>
28#include <linux/etherdevice.h>
29#include <linux/if_ether.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000030#include <linux/hash.h>
Yan Burman1b13c972013-01-29 23:43:07 +000031#include <linux/ethtool.h>
David Stevense4f67ad2012-11-20 02:50:14 +000032#include <net/arp.h>
33#include <net/ndisc.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000034#include <net/ip.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000035#include <net/ip_tunnels.h>
stephen hemmingerd3428942012-10-01 12:32:35 +000036#include <net/icmp.h>
37#include <net/udp.h>
38#include <net/rtnetlink.h>
39#include <net/route.h>
40#include <net/dsfield.h>
41#include <net/inet_ecn.h>
42#include <net/net_namespace.h>
43#include <net/netns/generic.h>
44
45#define VXLAN_VERSION "0.1"
46
stephen hemminger553675f2013-05-16 11:35:20 +000047#define PORT_HASH_BITS 8
48#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
stephen hemmingerd3428942012-10-01 12:32:35 +000049#define VNI_HASH_BITS 10
50#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
51#define FDB_HASH_BITS 8
52#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
53#define FDB_AGE_DEFAULT 300 /* 5 min */
54#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
55
56#define VXLAN_N_VID (1u << 24)
57#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
Alexander Duyck52b702f2012-11-09 13:35:24 +000058/* IP header + UDP + VXLAN + Ethernet header */
59#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
stephen hemmingerd3428942012-10-01 12:32:35 +000060
61#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
62
63/* VXLAN protocol header */
64struct vxlanhdr {
65 __be32 vx_flags;
66 __be32 vx_vni;
67};
68
stephen hemminger23c578b2013-04-27 11:31:53 +000069/* UDP port for VXLAN traffic.
70 * The IANA assigned port is 4789, but the Linux default is 8472
71 * for compatability with early adopters.
72 */
stephen hemmingerd3428942012-10-01 12:32:35 +000073static unsigned int vxlan_port __read_mostly = 8472;
74module_param_named(udp_port, vxlan_port, uint, 0444);
75MODULE_PARM_DESC(udp_port, "Destination UDP port");
76
77static bool log_ecn_error = true;
78module_param(log_ecn_error, bool, 0644);
79MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
80
stephen hemmingerd3428942012-10-01 12:32:35 +000081static unsigned int vxlan_net_id;
stephen hemminger553675f2013-05-16 11:35:20 +000082
83/* per UDP socket information */
84struct vxlan_sock {
85 struct hlist_node hlist;
86 struct rcu_head rcu;
87 struct work_struct del_work;
Stephen Hemminger7c47ced2013-06-17 14:16:10 -070088 atomic_t refcnt;
stephen hemminger553675f2013-05-16 11:35:20 +000089 struct socket *sock;
stephen hemmingerd3428942012-10-01 12:32:35 +000090 struct hlist_head vni_list[VNI_HASH_SIZE];
91};
92
stephen hemminger553675f2013-05-16 11:35:20 +000093/* per-network namespace private data for this module */
94struct vxlan_net {
95 struct list_head vxlan_list;
96 struct hlist_head sock_list[PORT_HASH_SIZE];
97};
98
David Stevens66817122013-03-15 04:35:51 +000099struct vxlan_rdst {
David Stevens66817122013-03-15 04:35:51 +0000100 __be32 remote_ip;
101 __be16 remote_port;
102 u32 remote_vni;
103 u32 remote_ifindex;
104 struct vxlan_rdst *remote_next;
105};
106
stephen hemmingerd3428942012-10-01 12:32:35 +0000107/* Forwarding table entry */
108struct vxlan_fdb {
109 struct hlist_node hlist; /* linked list of entries */
110 struct rcu_head rcu;
111 unsigned long updated; /* jiffies */
112 unsigned long used;
David Stevens66817122013-03-15 04:35:51 +0000113 struct vxlan_rdst remote;
stephen hemmingerd3428942012-10-01 12:32:35 +0000114 u16 state; /* see ndm_state */
David Stevensae884082013-04-19 00:36:26 +0000115 u8 flags; /* see ndm_flags */
stephen hemmingerd3428942012-10-01 12:32:35 +0000116 u8 eth_addr[ETH_ALEN];
117};
118
stephen hemmingerd3428942012-10-01 12:32:35 +0000119/* Pseudo network device */
120struct vxlan_dev {
stephen hemminger553675f2013-05-16 11:35:20 +0000121 struct hlist_node hlist; /* vni hash table */
122 struct list_head next; /* vxlan's per namespace list */
123 struct vxlan_sock *vn_sock; /* listening socket */
stephen hemmingerd3428942012-10-01 12:32:35 +0000124 struct net_device *dev;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000125 struct vxlan_rdst default_dst; /* default destination */
stephen hemmingerd3428942012-10-01 12:32:35 +0000126 __be32 saddr; /* source address */
stephen hemminger823aa872013-04-27 11:31:57 +0000127 __be16 dst_port;
stephen hemminger05f47d62012-10-09 20:35:50 +0000128 __u16 port_min; /* source port range */
129 __u16 port_max;
stephen hemmingerd3428942012-10-01 12:32:35 +0000130 __u8 tos; /* TOS override */
131 __u8 ttl;
David Stevense4f67ad2012-11-20 02:50:14 +0000132 u32 flags; /* VXLAN_F_* below */
stephen hemmingerd3428942012-10-01 12:32:35 +0000133
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700134 struct work_struct igmp_work;
stephen hemmingerd3428942012-10-01 12:32:35 +0000135 unsigned long age_interval;
136 struct timer_list age_timer;
137 spinlock_t hash_lock;
138 unsigned int addrcnt;
139 unsigned int addrmax;
stephen hemmingerd3428942012-10-01 12:32:35 +0000140
141 struct hlist_head fdb_head[FDB_HASH_SIZE];
142};
143
David Stevense4f67ad2012-11-20 02:50:14 +0000144#define VXLAN_F_LEARN 0x01
145#define VXLAN_F_PROXY 0x02
146#define VXLAN_F_RSC 0x04
147#define VXLAN_F_L2MISS 0x08
148#define VXLAN_F_L3MISS 0x10
149
stephen hemmingerd3428942012-10-01 12:32:35 +0000150/* salt for hash table */
151static u32 vxlan_salt __read_mostly;
Stephen Hemminger758c57d2013-06-17 14:16:09 -0700152static struct workqueue_struct *vxlan_wq;
stephen hemmingerd3428942012-10-01 12:32:35 +0000153
stephen hemminger553675f2013-05-16 11:35:20 +0000154/* Virtual Network hash table head */
155static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
156{
157 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
158}
159
160/* Socket hash table head */
161static inline struct hlist_head *vs_head(struct net *net, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000162{
163 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
164
stephen hemminger553675f2013-05-16 11:35:20 +0000165 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
166}
167
168/* Find VXLAN socket based on network namespace and UDP port */
169static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
170{
171 struct vxlan_sock *vs;
172
173 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
174 if (inet_sk(vs->sock->sk)->inet_sport == port)
175 return vs;
176 }
177 return NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000178}
179
180/* Look up VNI in a per net namespace table */
stephen hemminger553675f2013-05-16 11:35:20 +0000181static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
stephen hemmingerd3428942012-10-01 12:32:35 +0000182{
stephen hemminger553675f2013-05-16 11:35:20 +0000183 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +0000184 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000185
stephen hemminger553675f2013-05-16 11:35:20 +0000186 vs = vxlan_find_port(net, port);
187 if (!vs)
188 return NULL;
189
190 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000191 if (vxlan->default_dst.remote_vni == id)
stephen hemmingerd3428942012-10-01 12:32:35 +0000192 return vxlan;
193 }
194
195 return NULL;
196}
197
198/* Fill in neighbour message in skbuff. */
199static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
200 const struct vxlan_fdb *fdb,
David Stevens66817122013-03-15 04:35:51 +0000201 u32 portid, u32 seq, int type, unsigned int flags,
202 const struct vxlan_rdst *rdst)
stephen hemmingerd3428942012-10-01 12:32:35 +0000203{
204 unsigned long now = jiffies;
205 struct nda_cacheinfo ci;
206 struct nlmsghdr *nlh;
207 struct ndmsg *ndm;
David Stevense4f67ad2012-11-20 02:50:14 +0000208 bool send_ip, send_eth;
stephen hemmingerd3428942012-10-01 12:32:35 +0000209
210 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
211 if (nlh == NULL)
212 return -EMSGSIZE;
213
214 ndm = nlmsg_data(nlh);
215 memset(ndm, 0, sizeof(*ndm));
David Stevense4f67ad2012-11-20 02:50:14 +0000216
217 send_eth = send_ip = true;
218
219 if (type == RTM_GETNEIGH) {
220 ndm->ndm_family = AF_INET;
David Stevens66817122013-03-15 04:35:51 +0000221 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
David Stevense4f67ad2012-11-20 02:50:14 +0000222 send_eth = !is_zero_ether_addr(fdb->eth_addr);
223 } else
224 ndm->ndm_family = AF_BRIDGE;
stephen hemmingerd3428942012-10-01 12:32:35 +0000225 ndm->ndm_state = fdb->state;
226 ndm->ndm_ifindex = vxlan->dev->ifindex;
David Stevensae884082013-04-19 00:36:26 +0000227 ndm->ndm_flags = fdb->flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000228 ndm->ndm_type = NDA_DST;
229
David Stevense4f67ad2012-11-20 02:50:14 +0000230 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
stephen hemmingerd3428942012-10-01 12:32:35 +0000231 goto nla_put_failure;
232
David Stevens66817122013-03-15 04:35:51 +0000233 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
234 goto nla_put_failure;
235
stephen hemminger823aa872013-04-27 11:31:57 +0000236 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
David Stevens66817122013-03-15 04:35:51 +0000237 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
238 goto nla_put_failure;
Atzm Watanabec7995c42013-04-16 02:50:52 +0000239 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
David Stevens66817122013-03-15 04:35:51 +0000240 nla_put_be32(skb, NDA_VNI, rdst->remote_vni))
241 goto nla_put_failure;
242 if (rdst->remote_ifindex &&
243 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +0000244 goto nla_put_failure;
245
246 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
247 ci.ndm_confirmed = 0;
248 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
249 ci.ndm_refcnt = 0;
250
251 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
252 goto nla_put_failure;
253
254 return nlmsg_end(skb, nlh);
255
256nla_put_failure:
257 nlmsg_cancel(skb, nlh);
258 return -EMSGSIZE;
259}
260
261static inline size_t vxlan_nlmsg_size(void)
262{
263 return NLMSG_ALIGN(sizeof(struct ndmsg))
264 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
265 + nla_total_size(sizeof(__be32)) /* NDA_DST */
stephen hemminger73cf3312013-04-27 11:31:54 +0000266 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
David Stevens66817122013-03-15 04:35:51 +0000267 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
268 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
stephen hemmingerd3428942012-10-01 12:32:35 +0000269 + nla_total_size(sizeof(struct nda_cacheinfo));
270}
271
272static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
273 const struct vxlan_fdb *fdb, int type)
274{
275 struct net *net = dev_net(vxlan->dev);
276 struct sk_buff *skb;
277 int err = -ENOBUFS;
278
279 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
280 if (skb == NULL)
281 goto errout;
282
David Stevens66817122013-03-15 04:35:51 +0000283 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote);
stephen hemmingerd3428942012-10-01 12:32:35 +0000284 if (err < 0) {
285 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
286 WARN_ON(err == -EMSGSIZE);
287 kfree_skb(skb);
288 goto errout;
289 }
290
291 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
292 return;
293errout:
294 if (err < 0)
295 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
296}
297
David Stevense4f67ad2012-11-20 02:50:14 +0000298static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
299{
300 struct vxlan_dev *vxlan = netdev_priv(dev);
301 struct vxlan_fdb f;
302
303 memset(&f, 0, sizeof f);
304 f.state = NUD_STALE;
David Stevens66817122013-03-15 04:35:51 +0000305 f.remote.remote_ip = ipa; /* goes to NDA_DST */
306 f.remote.remote_vni = VXLAN_N_VID;
David Stevense4f67ad2012-11-20 02:50:14 +0000307
308 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
309}
310
311static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
312{
313 struct vxlan_fdb f;
314
315 memset(&f, 0, sizeof f);
316 f.state = NUD_STALE;
317 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
318
319 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
320}
321
stephen hemmingerd3428942012-10-01 12:32:35 +0000322/* Hash Ethernet address */
323static u32 eth_hash(const unsigned char *addr)
324{
325 u64 value = get_unaligned((u64 *)addr);
326
327 /* only want 6 bytes */
328#ifdef __BIG_ENDIAN
stephen hemmingerd3428942012-10-01 12:32:35 +0000329 value >>= 16;
stephen hemminger321fb992012-10-09 20:35:47 +0000330#else
331 value <<= 16;
stephen hemmingerd3428942012-10-01 12:32:35 +0000332#endif
333 return hash_64(value, FDB_HASH_BITS);
334}
335
336/* Hash chain to use given mac address */
337static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
338 const u8 *mac)
339{
340 return &vxlan->fdb_head[eth_hash(mac)];
341}
342
343/* Look up Ethernet address in forwarding table */
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000344static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
stephen hemmingerd3428942012-10-01 12:32:35 +0000345 const u8 *mac)
346
347{
348 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
349 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000350
Sasha Levinb67bfe02013-02-27 17:06:00 -0800351 hlist_for_each_entry_rcu(f, head, hlist) {
stephen hemmingerd3428942012-10-01 12:32:35 +0000352 if (compare_ether_addr(mac, f->eth_addr) == 0)
353 return f;
354 }
355
356 return NULL;
357}
358
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000359static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
360 const u8 *mac)
361{
362 struct vxlan_fdb *f;
363
364 f = __vxlan_find_mac(vxlan, mac);
365 if (f)
366 f->used = jiffies;
367
368 return f;
369}
370
David Stevens66817122013-03-15 04:35:51 +0000371/* Add/update destinations for multicast */
372static int vxlan_fdb_append(struct vxlan_fdb *f,
stephen hemminger73cf3312013-04-27 11:31:54 +0000373 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
David Stevens66817122013-03-15 04:35:51 +0000374{
375 struct vxlan_rdst *rd_prev, *rd;
376
377 rd_prev = NULL;
378 for (rd = &f->remote; rd; rd = rd->remote_next) {
379 if (rd->remote_ip == ip &&
380 rd->remote_port == port &&
381 rd->remote_vni == vni &&
382 rd->remote_ifindex == ifindex)
383 return 0;
384 rd_prev = rd;
385 }
386 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
387 if (rd == NULL)
388 return -ENOBUFS;
389 rd->remote_ip = ip;
390 rd->remote_port = port;
391 rd->remote_vni = vni;
392 rd->remote_ifindex = ifindex;
393 rd->remote_next = NULL;
394 rd_prev->remote_next = rd;
395 return 1;
396}
397
stephen hemmingerd3428942012-10-01 12:32:35 +0000398/* Add new entry to forwarding table -- assumes lock held */
399static int vxlan_fdb_create(struct vxlan_dev *vxlan,
400 const u8 *mac, __be32 ip,
David Stevens66817122013-03-15 04:35:51 +0000401 __u16 state, __u16 flags,
stephen hemminger73cf3312013-04-27 11:31:54 +0000402 __be16 port, __u32 vni, __u32 ifindex,
David Stevensae884082013-04-19 00:36:26 +0000403 __u8 ndm_flags)
stephen hemmingerd3428942012-10-01 12:32:35 +0000404{
405 struct vxlan_fdb *f;
406 int notify = 0;
407
Sridhar Samudrala014be2c2013-05-17 06:39:07 +0000408 f = __vxlan_find_mac(vxlan, mac);
stephen hemmingerd3428942012-10-01 12:32:35 +0000409 if (f) {
410 if (flags & NLM_F_EXCL) {
411 netdev_dbg(vxlan->dev,
412 "lost race to create %pM\n", mac);
413 return -EEXIST;
414 }
415 if (f->state != state) {
416 f->state = state;
417 f->updated = jiffies;
418 notify = 1;
419 }
David Stevensae884082013-04-19 00:36:26 +0000420 if (f->flags != ndm_flags) {
421 f->flags = ndm_flags;
422 f->updated = jiffies;
423 notify = 1;
424 }
David Stevens66817122013-03-15 04:35:51 +0000425 if ((flags & NLM_F_APPEND) &&
426 is_multicast_ether_addr(f->eth_addr)) {
427 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
428
429 if (rc < 0)
430 return rc;
431 notify |= rc;
432 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000433 } else {
434 if (!(flags & NLM_F_CREATE))
435 return -ENOENT;
436
437 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
438 return -ENOSPC;
439
440 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
441 f = kmalloc(sizeof(*f), GFP_ATOMIC);
442 if (!f)
443 return -ENOMEM;
444
445 notify = 1;
David Stevens66817122013-03-15 04:35:51 +0000446 f->remote.remote_ip = ip;
447 f->remote.remote_port = port;
448 f->remote.remote_vni = vni;
449 f->remote.remote_ifindex = ifindex;
450 f->remote.remote_next = NULL;
stephen hemmingerd3428942012-10-01 12:32:35 +0000451 f->state = state;
David Stevensae884082013-04-19 00:36:26 +0000452 f->flags = ndm_flags;
stephen hemmingerd3428942012-10-01 12:32:35 +0000453 f->updated = f->used = jiffies;
454 memcpy(f->eth_addr, mac, ETH_ALEN);
455
456 ++vxlan->addrcnt;
457 hlist_add_head_rcu(&f->hlist,
458 vxlan_fdb_head(vxlan, mac));
459 }
460
461 if (notify)
462 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
463
464 return 0;
465}
466
Wei Yongjun6706c822013-04-11 19:00:35 +0000467static void vxlan_fdb_free(struct rcu_head *head)
David Stevens66817122013-03-15 04:35:51 +0000468{
469 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
470
471 while (f->remote.remote_next) {
472 struct vxlan_rdst *rd = f->remote.remote_next;
473
474 f->remote.remote_next = rd->remote_next;
475 kfree(rd);
476 }
477 kfree(f);
478}
479
stephen hemmingerd3428942012-10-01 12:32:35 +0000480static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
481{
482 netdev_dbg(vxlan->dev,
483 "delete %pM\n", f->eth_addr);
484
485 --vxlan->addrcnt;
486 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
487
488 hlist_del_rcu(&f->hlist);
David Stevens66817122013-03-15 04:35:51 +0000489 call_rcu(&f->rcu, vxlan_fdb_free);
stephen hemmingerd3428942012-10-01 12:32:35 +0000490}
491
492/* Add static entry (via netlink) */
493static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
494 struct net_device *dev,
495 const unsigned char *addr, u16 flags)
496{
497 struct vxlan_dev *vxlan = netdev_priv(dev);
David Stevens66817122013-03-15 04:35:51 +0000498 struct net *net = dev_net(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000499 __be32 ip;
stephen hemminger73cf3312013-04-27 11:31:54 +0000500 __be16 port;
501 u32 vni, ifindex;
stephen hemmingerd3428942012-10-01 12:32:35 +0000502 int err;
503
504 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
505 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
506 ndm->ndm_state);
507 return -EINVAL;
508 }
509
510 if (tb[NDA_DST] == NULL)
511 return -EINVAL;
512
513 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
514 return -EAFNOSUPPORT;
515
516 ip = nla_get_be32(tb[NDA_DST]);
517
David Stevens66817122013-03-15 04:35:51 +0000518 if (tb[NDA_PORT]) {
stephen hemminger73cf3312013-04-27 11:31:54 +0000519 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
David Stevens66817122013-03-15 04:35:51 +0000520 return -EINVAL;
stephen hemminger73cf3312013-04-27 11:31:54 +0000521 port = nla_get_be16(tb[NDA_PORT]);
David Stevens66817122013-03-15 04:35:51 +0000522 } else
stephen hemminger823aa872013-04-27 11:31:57 +0000523 port = vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +0000524
525 if (tb[NDA_VNI]) {
526 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
527 return -EINVAL;
528 vni = nla_get_u32(tb[NDA_VNI]);
529 } else
Atzm Watanabec7995c42013-04-16 02:50:52 +0000530 vni = vxlan->default_dst.remote_vni;
David Stevens66817122013-03-15 04:35:51 +0000531
532 if (tb[NDA_IFINDEX]) {
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000533 struct net_device *tdev;
David Stevens66817122013-03-15 04:35:51 +0000534
535 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
536 return -EINVAL;
537 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000538 tdev = dev_get_by_index(net, ifindex);
539 if (!tdev)
David Stevens66817122013-03-15 04:35:51 +0000540 return -EADDRNOTAVAIL;
Pravin B Shelar5abb0022013-03-26 08:29:30 +0000541 dev_put(tdev);
David Stevens66817122013-03-15 04:35:51 +0000542 } else
543 ifindex = 0;
544
stephen hemmingerd3428942012-10-01 12:32:35 +0000545 spin_lock_bh(&vxlan->hash_lock);
stephen hemminger73cf3312013-04-27 11:31:54 +0000546 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
547 port, vni, ifindex, ndm->ndm_flags);
stephen hemmingerd3428942012-10-01 12:32:35 +0000548 spin_unlock_bh(&vxlan->hash_lock);
549
550 return err;
551}
552
553/* Delete entry (via netlink) */
Vlad Yasevich1690be62013-02-13 12:00:18 +0000554static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
555 struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000556 const unsigned char *addr)
557{
558 struct vxlan_dev *vxlan = netdev_priv(dev);
559 struct vxlan_fdb *f;
560 int err = -ENOENT;
561
562 spin_lock_bh(&vxlan->hash_lock);
563 f = vxlan_find_mac(vxlan, addr);
564 if (f) {
565 vxlan_fdb_destroy(vxlan, f);
566 err = 0;
567 }
568 spin_unlock_bh(&vxlan->hash_lock);
569
570 return err;
571}
572
573/* Dump forwarding table */
574static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
575 struct net_device *dev, int idx)
576{
577 struct vxlan_dev *vxlan = netdev_priv(dev);
578 unsigned int h;
579
580 for (h = 0; h < FDB_HASH_SIZE; ++h) {
581 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000582 int err;
583
Sasha Levinb67bfe02013-02-27 17:06:00 -0800584 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
David Stevens66817122013-03-15 04:35:51 +0000585 struct vxlan_rdst *rd;
586 for (rd = &f->remote; rd; rd = rd->remote_next) {
587 if (idx < cb->args[0])
588 goto skip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000589
David Stevens66817122013-03-15 04:35:51 +0000590 err = vxlan_fdb_info(skb, vxlan, f,
591 NETLINK_CB(cb->skb).portid,
592 cb->nlh->nlmsg_seq,
593 RTM_NEWNEIGH,
594 NLM_F_MULTI, rd);
595 if (err < 0)
596 break;
stephen hemmingerd3428942012-10-01 12:32:35 +0000597skip:
David Stevens66817122013-03-15 04:35:51 +0000598 ++idx;
599 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000600 }
601 }
602
603 return idx;
604}
605
606/* Watch incoming packets to learn mapping between Ethernet address
607 * and Tunnel endpoint.
stephen hemminger26a41ae62013-06-17 12:09:58 -0700608 * Return true if packet is bogus and should be droppped.
stephen hemmingerd3428942012-10-01 12:32:35 +0000609 */
stephen hemminger26a41ae62013-06-17 12:09:58 -0700610static bool vxlan_snoop(struct net_device *dev,
stephen hemmingerd3428942012-10-01 12:32:35 +0000611 __be32 src_ip, const u8 *src_mac)
612{
613 struct vxlan_dev *vxlan = netdev_priv(dev);
614 struct vxlan_fdb *f;
stephen hemmingerd3428942012-10-01 12:32:35 +0000615
616 f = vxlan_find_mac(vxlan, src_mac);
617 if (likely(f)) {
David Stevens66817122013-03-15 04:35:51 +0000618 if (likely(f->remote.remote_ip == src_ip))
stephen hemminger26a41ae62013-06-17 12:09:58 -0700619 return false;
620
621 /* Don't migrate static entries, drop packets */
stephen hemmingereb064c32013-06-18 14:27:01 -0700622 if (f->state & NUD_NOARP)
stephen hemminger26a41ae62013-06-17 12:09:58 -0700623 return true;
stephen hemmingerd3428942012-10-01 12:32:35 +0000624
625 if (net_ratelimit())
626 netdev_info(dev,
627 "%pM migrated from %pI4 to %pI4\n",
David Stevens66817122013-03-15 04:35:51 +0000628 src_mac, &f->remote.remote_ip, &src_ip);
stephen hemmingerd3428942012-10-01 12:32:35 +0000629
David Stevens66817122013-03-15 04:35:51 +0000630 f->remote.remote_ip = src_ip;
stephen hemmingerd3428942012-10-01 12:32:35 +0000631 f->updated = jiffies;
632 } else {
633 /* learned new entry */
634 spin_lock(&vxlan->hash_lock);
stephen hemminger3bf74b12013-06-17 12:09:57 -0700635
636 /* close off race between vxlan_flush and incoming packets */
637 if (netif_running(dev))
638 vxlan_fdb_create(vxlan, src_mac, src_ip,
639 NUD_REACHABLE,
640 NLM_F_EXCL|NLM_F_CREATE,
641 vxlan->dst_port,
642 vxlan->default_dst.remote_vni,
643 0, NTF_SELF);
stephen hemmingerd3428942012-10-01 12:32:35 +0000644 spin_unlock(&vxlan->hash_lock);
645 }
stephen hemminger26a41ae62013-06-17 12:09:58 -0700646
647 return false;
stephen hemmingerd3428942012-10-01 12:32:35 +0000648}
649
650
651/* See if multicast group is already in use by other ID */
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700652static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
stephen hemmingerd3428942012-10-01 12:32:35 +0000653{
stephen hemminger553675f2013-05-16 11:35:20 +0000654 struct vxlan_dev *vxlan;
stephen hemmingerd3428942012-10-01 12:32:35 +0000655
stephen hemminger553675f2013-05-16 11:35:20 +0000656 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
stephen hemminger553675f2013-05-16 11:35:20 +0000657 if (!netif_running(vxlan->dev))
658 continue;
stephen hemmingerd3428942012-10-01 12:32:35 +0000659
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700660 if (vxlan->default_dst.remote_ip == remote_ip)
stephen hemminger553675f2013-05-16 11:35:20 +0000661 return true;
662 }
stephen hemmingerd3428942012-10-01 12:32:35 +0000663
664 return false;
665}
666
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700667static void vxlan_sock_hold(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000668{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700669 atomic_inc(&vs->refcnt);
stephen hemmingerd3428942012-10-01 12:32:35 +0000670}
671
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700672static void vxlan_sock_release(struct vxlan_sock *vs)
stephen hemmingerd3428942012-10-01 12:32:35 +0000673{
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700674 if (!atomic_dec_and_test(&vs->refcnt))
675 return;
676
677 hlist_del_rcu(&vs->hlist);
678 queue_work(vxlan_wq, &vs->del_work);
679}
680
681/* Callback to update multicast group membership.
682 * Scheduled when vxlan goes up/down.
683 */
684static void vxlan_igmp_work(struct work_struct *work)
685{
686 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_work);
687 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
688 struct vxlan_sock *vs = vxlan->vn_sock;
689 struct sock *sk = vs->sock->sk;
stephen hemmingerd3428942012-10-01 12:32:35 +0000690 struct ip_mreqn mreq = {
Atzm Watanabec7995c42013-04-16 02:50:52 +0000691 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
692 .imr_ifindex = vxlan->default_dst.remote_ifindex,
stephen hemmingerd3428942012-10-01 12:32:35 +0000693 };
694
stephen hemmingerd3428942012-10-01 12:32:35 +0000695 lock_sock(sk);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700696 if (vxlan_group_used(vn, vxlan->default_dst.remote_ip))
697 ip_mc_join_group(sk, &mreq);
698 else
699 ip_mc_leave_group(sk, &mreq);
stephen hemmingerd3428942012-10-01 12:32:35 +0000700 release_sock(sk);
stephen hemmingerd3428942012-10-01 12:32:35 +0000701
Stephen Hemminger7c47ced2013-06-17 14:16:10 -0700702 vxlan_sock_release(vs);
703 dev_put(vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000704}
705
706/* Callback from net/ipv4/udp.c to receive packets */
707static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
708{
709 struct iphdr *oip;
710 struct vxlanhdr *vxh;
711 struct vxlan_dev *vxlan;
Pravin B Shelare8171042013-03-25 14:49:46 +0000712 struct pcpu_tstats *stats;
stephen hemminger553675f2013-05-16 11:35:20 +0000713 __be16 port;
stephen hemmingerd3428942012-10-01 12:32:35 +0000714 __u32 vni;
715 int err;
716
717 /* pop off outer UDP header */
718 __skb_pull(skb, sizeof(struct udphdr));
719
720 /* Need Vxlan and inner Ethernet header to be present */
721 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
722 goto error;
723
724 /* Drop packets with reserved bits set */
725 vxh = (struct vxlanhdr *) skb->data;
726 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
727 (vxh->vx_vni & htonl(0xff))) {
728 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
729 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
730 goto error;
731 }
732
733 __skb_pull(skb, sizeof(struct vxlanhdr));
stephen hemmingerd3428942012-10-01 12:32:35 +0000734
735 /* Is this VNI defined? */
736 vni = ntohl(vxh->vx_vni) >> 8;
stephen hemminger553675f2013-05-16 11:35:20 +0000737 port = inet_sk(sk)->inet_sport;
738 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
stephen hemmingerd3428942012-10-01 12:32:35 +0000739 if (!vxlan) {
stephen hemminger553675f2013-05-16 11:35:20 +0000740 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
741 vni, ntohs(port));
stephen hemmingerd3428942012-10-01 12:32:35 +0000742 goto drop;
743 }
744
745 if (!pskb_may_pull(skb, ETH_HLEN)) {
746 vxlan->dev->stats.rx_length_errors++;
747 vxlan->dev->stats.rx_errors++;
748 goto drop;
749 }
750
David Stevense4f67ad2012-11-20 02:50:14 +0000751 skb_reset_mac_header(skb);
752
stephen hemmingerd3428942012-10-01 12:32:35 +0000753 /* Re-examine inner Ethernet packet */
754 oip = ip_hdr(skb);
755 skb->protocol = eth_type_trans(skb, vxlan->dev);
stephen hemmingerd3428942012-10-01 12:32:35 +0000756
757 /* Ignore packet loops (and multicast echo) */
758 if (compare_ether_addr(eth_hdr(skb)->h_source,
759 vxlan->dev->dev_addr) == 0)
760 goto drop;
761
stephen hemminger26a41ae62013-06-17 12:09:58 -0700762 if ((vxlan->flags & VXLAN_F_LEARN) &&
763 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
764 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +0000765
766 __skb_tunnel_rx(skb, vxlan->dev);
767 skb_reset_network_header(skb);
Joseph Gasparakis0afb1662012-12-07 14:14:18 +0000768
769 /* If the NIC driver gave us an encapsulated packet with
770 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
771 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
772 * for us. Otherwise force the upper layers to verify it.
773 */
774 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
775 !(vxlan->dev->features & NETIF_F_RXCSUM))
776 skb->ip_summed = CHECKSUM_NONE;
777
778 skb->encapsulation = 0;
stephen hemmingerd3428942012-10-01 12:32:35 +0000779
780 err = IP_ECN_decapsulate(oip, skb);
781 if (unlikely(err)) {
782 if (log_ecn_error)
783 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
784 &oip->saddr, oip->tos);
785 if (err > 1) {
786 ++vxlan->dev->stats.rx_frame_errors;
787 ++vxlan->dev->stats.rx_errors;
788 goto drop;
789 }
790 }
791
Pravin B Shelare8171042013-03-25 14:49:46 +0000792 stats = this_cpu_ptr(vxlan->dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +0000793 u64_stats_update_begin(&stats->syncp);
794 stats->rx_packets++;
795 stats->rx_bytes += skb->len;
796 u64_stats_update_end(&stats->syncp);
797
798 netif_rx(skb);
799
800 return 0;
801error:
802 /* Put UDP header back */
803 __skb_push(skb, sizeof(struct udphdr));
804
805 return 1;
806drop:
807 /* Consume bad packet */
808 kfree_skb(skb);
809 return 0;
810}
811
David Stevense4f67ad2012-11-20 02:50:14 +0000812static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
813{
814 struct vxlan_dev *vxlan = netdev_priv(dev);
815 struct arphdr *parp;
816 u8 *arpptr, *sha;
817 __be32 sip, tip;
818 struct neighbour *n;
819
820 if (dev->flags & IFF_NOARP)
821 goto out;
822
823 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
824 dev->stats.tx_dropped++;
825 goto out;
826 }
827 parp = arp_hdr(skb);
828
829 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
830 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
831 parp->ar_pro != htons(ETH_P_IP) ||
832 parp->ar_op != htons(ARPOP_REQUEST) ||
833 parp->ar_hln != dev->addr_len ||
834 parp->ar_pln != 4)
835 goto out;
836 arpptr = (u8 *)parp + sizeof(struct arphdr);
837 sha = arpptr;
838 arpptr += dev->addr_len; /* sha */
839 memcpy(&sip, arpptr, sizeof(sip));
840 arpptr += sizeof(sip);
841 arpptr += dev->addr_len; /* tha */
842 memcpy(&tip, arpptr, sizeof(tip));
843
844 if (ipv4_is_loopback(tip) ||
845 ipv4_is_multicast(tip))
846 goto out;
847
848 n = neigh_lookup(&arp_tbl, &tip, dev);
849
850 if (n) {
David Stevense4f67ad2012-11-20 02:50:14 +0000851 struct vxlan_fdb *f;
852 struct sk_buff *reply;
853
854 if (!(n->nud_state & NUD_CONNECTED)) {
855 neigh_release(n);
856 goto out;
857 }
858
859 f = vxlan_find_mac(vxlan, n->ha);
David Stevens66817122013-03-15 04:35:51 +0000860 if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
David Stevense4f67ad2012-11-20 02:50:14 +0000861 /* bridge-local neighbor */
862 neigh_release(n);
863 goto out;
864 }
865
866 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
867 n->ha, sha);
868
869 neigh_release(n);
870
871 skb_reset_mac_header(reply);
872 __skb_pull(reply, skb_network_offset(reply));
873 reply->ip_summed = CHECKSUM_UNNECESSARY;
874 reply->pkt_type = PACKET_HOST;
875
876 if (netif_rx_ni(reply) == NET_RX_DROP)
877 dev->stats.rx_dropped++;
878 } else if (vxlan->flags & VXLAN_F_L3MISS)
879 vxlan_ip_miss(dev, tip);
880out:
881 consume_skb(skb);
882 return NETDEV_TX_OK;
883}
884
885static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
886{
887 struct vxlan_dev *vxlan = netdev_priv(dev);
888 struct neighbour *n;
889 struct iphdr *pip;
890
891 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
892 return false;
893
894 n = NULL;
895 switch (ntohs(eth_hdr(skb)->h_proto)) {
896 case ETH_P_IP:
897 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
898 return false;
899 pip = ip_hdr(skb);
900 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
901 break;
902 default:
903 return false;
904 }
905
906 if (n) {
907 bool diff;
908
909 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
910 if (diff) {
911 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
912 dev->addr_len);
913 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
914 }
915 neigh_release(n);
916 return diff;
917 } else if (vxlan->flags & VXLAN_F_L3MISS)
918 vxlan_ip_miss(dev, pip->daddr);
919 return false;
920}
921
stephen hemminger553675f2013-05-16 11:35:20 +0000922static void vxlan_sock_put(struct sk_buff *skb)
stephen hemminger1cad8712012-10-09 20:35:49 +0000923{
924 sock_put(skb->sk);
925}
926
927/* On transmit, associate with the tunnel socket */
928static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
929{
stephen hemminger553675f2013-05-16 11:35:20 +0000930 struct vxlan_dev *vxlan = netdev_priv(dev);
931 struct sock *sk = vxlan->vn_sock->sock->sk;
stephen hemminger1cad8712012-10-09 20:35:49 +0000932
933 skb_orphan(skb);
934 sock_hold(sk);
935 skb->sk = sk;
stephen hemminger553675f2013-05-16 11:35:20 +0000936 skb->destructor = vxlan_sock_put;
stephen hemminger1cad8712012-10-09 20:35:49 +0000937}
938
stephen hemminger05f47d62012-10-09 20:35:50 +0000939/* Compute source port for outgoing packet
940 * first choice to use L4 flow hash since it will spread
941 * better and maybe available from hardware
942 * secondary choice is to use jhash on the Ethernet header
943 */
stephen hemminger7d836a72013-04-27 11:31:56 +0000944static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
stephen hemminger05f47d62012-10-09 20:35:50 +0000945{
946 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
947 u32 hash;
948
949 hash = skb_get_rxhash(skb);
950 if (!hash)
951 hash = jhash(skb->data, 2 * ETH_ALEN,
952 (__force u32) skb->protocol);
953
stephen hemminger7d836a72013-04-27 11:31:56 +0000954 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
stephen hemminger05f47d62012-10-09 20:35:50 +0000955}
956
Pravin B Shelar05c0db02013-03-07 13:22:36 +0000957static int handle_offloads(struct sk_buff *skb)
958{
959 if (skb_is_gso(skb)) {
960 int err = skb_unclone(skb, GFP_ATOMIC);
961 if (unlikely(err))
962 return err;
963
Dmitry Kravkovf6ace502013-04-28 08:16:01 +0000964 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
Pravin B Shelar05c0db02013-03-07 13:22:36 +0000965 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
966 skb->ip_summed = CHECKSUM_NONE;
967
968 return 0;
969}
970
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000971/* Bypass encapsulation if the destination is local */
972static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
973 struct vxlan_dev *dst_vxlan)
974{
975 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
976 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
977
978 skb->pkt_type = PACKET_HOST;
979 skb->encapsulation = 0;
980 skb->dev = dst_vxlan->dev;
981 __skb_pull(skb, skb_network_offset(skb));
982
983 if (dst_vxlan->flags & VXLAN_F_LEARN)
Mike Rapoport9d9f1632013-04-13 23:21:39 +0000984 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
985 eth_hdr(skb)->h_source);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +0000986
987 u64_stats_update_begin(&tx_stats->syncp);
988 tx_stats->tx_packets++;
989 tx_stats->tx_bytes += skb->len;
990 u64_stats_update_end(&tx_stats->syncp);
991
992 if (netif_rx(skb) == NET_RX_SUCCESS) {
993 u64_stats_update_begin(&rx_stats->syncp);
994 rx_stats->rx_packets++;
995 rx_stats->rx_bytes += skb->len;
996 u64_stats_update_end(&rx_stats->syncp);
997 } else {
998 skb->dev->stats.rx_dropped++;
999 }
1000}
1001
David Stevens66817122013-03-15 04:35:51 +00001002static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1003 struct vxlan_rdst *rdst, bool did_rsc)
stephen hemmingerd3428942012-10-01 12:32:35 +00001004{
1005 struct vxlan_dev *vxlan = netdev_priv(dev);
1006 struct rtable *rt;
stephen hemmingerd3428942012-10-01 12:32:35 +00001007 const struct iphdr *old_iph;
stephen hemmingerd3428942012-10-01 12:32:35 +00001008 struct vxlanhdr *vxh;
1009 struct udphdr *uh;
1010 struct flowi4 fl4;
stephen hemmingerd3428942012-10-01 12:32:35 +00001011 __be32 dst;
stephen hemminger7d836a72013-04-27 11:31:56 +00001012 __be16 src_port, dst_port;
David Stevens66817122013-03-15 04:35:51 +00001013 u32 vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001014 __be16 df = 0;
1015 __u8 tos, ttl;
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001016 int err;
stephen hemmingerd3428942012-10-01 12:32:35 +00001017
stephen hemminger823aa872013-04-27 11:31:57 +00001018 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
David Stevens66817122013-03-15 04:35:51 +00001019 vni = rdst->remote_vni;
1020 dst = rdst->remote_ip;
David Stevense4f67ad2012-11-20 02:50:14 +00001021
1022 if (!dst) {
1023 if (did_rsc) {
David Stevense4f67ad2012-11-20 02:50:14 +00001024 /* short-circuited back to local bridge */
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001025 vxlan_encap_bypass(skb, vxlan, vxlan);
David Stevense4f67ad2012-11-20 02:50:14 +00001026 return NETDEV_TX_OK;
1027 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001028 goto drop;
David Stevense4f67ad2012-11-20 02:50:14 +00001029 }
stephen hemmingeref59feb2012-10-09 20:35:46 +00001030
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001031 if (!skb->encapsulation) {
1032 skb_reset_inner_headers(skb);
1033 skb->encapsulation = 1;
1034 }
1035
stephen hemmingerd3428942012-10-01 12:32:35 +00001036 /* Need space for new headers (invalidates iph ptr) */
1037 if (skb_cow_head(skb, VXLAN_HEADROOM))
1038 goto drop;
1039
stephen hemmingerd3428942012-10-01 12:32:35 +00001040 old_iph = ip_hdr(skb);
1041
stephen hemmingerd3428942012-10-01 12:32:35 +00001042 ttl = vxlan->ttl;
1043 if (!ttl && IN_MULTICAST(ntohl(dst)))
1044 ttl = 1;
1045
1046 tos = vxlan->tos;
1047 if (tos == 1)
Pravin B Shelar206aaaf2013-03-25 14:49:53 +00001048 tos = ip_tunnel_get_dsfield(old_iph, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001049
stephen hemminger05f47d62012-10-09 20:35:50 +00001050 src_port = vxlan_src_port(vxlan, skb);
stephen hemmingerd3428942012-10-01 12:32:35 +00001051
stephen hemmingerca78f182012-10-09 20:35:48 +00001052 memset(&fl4, 0, sizeof(fl4));
David Stevens66817122013-03-15 04:35:51 +00001053 fl4.flowi4_oif = rdst->remote_ifindex;
stephen hemmingerca78f182012-10-09 20:35:48 +00001054 fl4.flowi4_tos = RT_TOS(tos);
1055 fl4.daddr = dst;
1056 fl4.saddr = vxlan->saddr;
1057
1058 rt = ip_route_output_key(dev_net(dev), &fl4);
stephen hemmingerd3428942012-10-01 12:32:35 +00001059 if (IS_ERR(rt)) {
1060 netdev_dbg(dev, "no route to %pI4\n", &dst);
1061 dev->stats.tx_carrier_errors++;
1062 goto tx_error;
1063 }
1064
1065 if (rt->dst.dev == dev) {
1066 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1067 ip_rt_put(rt);
1068 dev->stats.collisions++;
1069 goto tx_error;
1070 }
1071
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001072 /* Bypass encapsulation if the destination is local */
Mike Rapoportab09a6d2013-04-13 23:21:51 +00001073 if (rt->rt_flags & RTCF_LOCAL &&
1074 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001075 struct vxlan_dev *dst_vxlan;
1076
1077 ip_rt_put(rt);
stephen hemminger553675f2013-05-16 11:35:20 +00001078 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
Sridhar Samudrala9dcc71e2013-04-02 12:31:52 +00001079 if (!dst_vxlan)
1080 goto tx_error;
1081 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1082 return NETDEV_TX_OK;
1083 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001084 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1085 vxh->vx_flags = htonl(VXLAN_FLAGS);
David Stevens66817122013-03-15 04:35:51 +00001086 vxh->vx_vni = htonl(vni << 8);
stephen hemmingerd3428942012-10-01 12:32:35 +00001087
1088 __skb_push(skb, sizeof(*uh));
1089 skb_reset_transport_header(skb);
1090 uh = udp_hdr(skb);
1091
stephen hemminger73cf3312013-04-27 11:31:54 +00001092 uh->dest = dst_port;
stephen hemminger7d836a72013-04-27 11:31:56 +00001093 uh->source = src_port;
stephen hemmingerd3428942012-10-01 12:32:35 +00001094
1095 uh->len = htons(skb->len);
1096 uh->check = 0;
1097
stephen hemminger1cad8712012-10-09 20:35:49 +00001098 vxlan_set_owner(dev, skb);
1099
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001100 if (handle_offloads(skb))
1101 goto drop;
stephen hemmingerd3428942012-10-01 12:32:35 +00001102
Pravin B Shelar0e6fbc52013-06-17 17:49:56 -07001103 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1104 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1105
1106 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1107 IPPROTO_UDP, tos, ttl, df);
1108 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1109
stephen hemmingerd3428942012-10-01 12:32:35 +00001110 return NETDEV_TX_OK;
1111
1112drop:
1113 dev->stats.tx_dropped++;
1114 goto tx_free;
1115
1116tx_error:
1117 dev->stats.tx_errors++;
1118tx_free:
1119 dev_kfree_skb(skb);
1120 return NETDEV_TX_OK;
1121}
1122
David Stevens66817122013-03-15 04:35:51 +00001123/* Transmit local packets over Vxlan
1124 *
1125 * Outer IP header inherits ECN and DF from inner header.
1126 * Outer UDP destination is the VXLAN assigned port.
1127 * source port is based on hash of flow
1128 */
1129static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1130{
1131 struct vxlan_dev *vxlan = netdev_priv(dev);
1132 struct ethhdr *eth;
1133 bool did_rsc = false;
Atzm Watanabec7995c42013-04-16 02:50:52 +00001134 struct vxlan_rdst *rdst0, *rdst;
David Stevens66817122013-03-15 04:35:51 +00001135 struct vxlan_fdb *f;
1136 int rc1, rc;
1137
1138 skb_reset_mac_header(skb);
1139 eth = eth_hdr(skb);
1140
1141 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1142 return arp_reduce(dev, skb);
David Stevens66817122013-03-15 04:35:51 +00001143
1144 f = vxlan_find_mac(vxlan, eth->h_dest);
David Stevensae884082013-04-19 00:36:26 +00001145 did_rsc = false;
1146
1147 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1148 ntohs(eth->h_proto) == ETH_P_IP) {
1149 did_rsc = route_shortcircuit(dev, skb);
1150 if (did_rsc)
1151 f = vxlan_find_mac(vxlan, eth->h_dest);
1152 }
1153
David Stevens66817122013-03-15 04:35:51 +00001154 if (f == NULL) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001155 rdst0 = &vxlan->default_dst;
David Stevens66817122013-03-15 04:35:51 +00001156
Atzm Watanabec7995c42013-04-16 02:50:52 +00001157 if (rdst0->remote_ip == htonl(INADDR_ANY) &&
David Stevens66817122013-03-15 04:35:51 +00001158 (vxlan->flags & VXLAN_F_L2MISS) &&
1159 !is_multicast_ether_addr(eth->h_dest))
1160 vxlan_fdb_miss(vxlan, eth->h_dest);
1161 } else
1162 rdst0 = &f->remote;
1163
1164 rc = NETDEV_TX_OK;
1165
1166 /* if there are multiple destinations, send copies */
1167 for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
1168 struct sk_buff *skb1;
1169
1170 skb1 = skb_clone(skb, GFP_ATOMIC);
stephen hemminger7aa27232013-06-17 12:09:59 -07001171 if (skb1) {
1172 rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1173 if (rc == NETDEV_TX_OK)
1174 rc = rc1;
1175 }
David Stevens66817122013-03-15 04:35:51 +00001176 }
1177
1178 rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1179 if (rc == NETDEV_TX_OK)
1180 rc = rc1;
1181 return rc;
1182}
1183
stephen hemmingerd3428942012-10-01 12:32:35 +00001184/* Walk the forwarding table and purge stale entries */
1185static void vxlan_cleanup(unsigned long arg)
1186{
1187 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1188 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1189 unsigned int h;
1190
1191 if (!netif_running(vxlan->dev))
1192 return;
1193
1194 spin_lock_bh(&vxlan->hash_lock);
1195 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1196 struct hlist_node *p, *n;
1197 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1198 struct vxlan_fdb *f
1199 = container_of(p, struct vxlan_fdb, hlist);
1200 unsigned long timeout;
1201
stephen hemminger3c172862012-10-26 06:24:34 +00001202 if (f->state & NUD_PERMANENT)
stephen hemmingerd3428942012-10-01 12:32:35 +00001203 continue;
1204
1205 timeout = f->used + vxlan->age_interval * HZ;
1206 if (time_before_eq(timeout, jiffies)) {
1207 netdev_dbg(vxlan->dev,
1208 "garbage collect %pM\n",
1209 f->eth_addr);
1210 f->state = NUD_STALE;
1211 vxlan_fdb_destroy(vxlan, f);
1212 } else if (time_before(timeout, next_timer))
1213 next_timer = timeout;
1214 }
1215 }
1216 spin_unlock_bh(&vxlan->hash_lock);
1217
1218 mod_timer(&vxlan->age_timer, next_timer);
1219}
1220
1221/* Setup stats when device is created */
1222static int vxlan_init(struct net_device *dev)
1223{
Pravin B Shelare8171042013-03-25 14:49:46 +00001224 dev->tstats = alloc_percpu(struct pcpu_tstats);
1225 if (!dev->tstats)
stephen hemmingerd3428942012-10-01 12:32:35 +00001226 return -ENOMEM;
1227
1228 return 0;
1229}
1230
1231/* Start ageing timer and join group when device is brought up */
1232static int vxlan_open(struct net_device *dev)
1233{
1234 struct vxlan_dev *vxlan = netdev_priv(dev);
stephen hemmingerd3428942012-10-01 12:32:35 +00001235
Atzm Watanabec7995c42013-04-16 02:50:52 +00001236 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001237 vxlan_sock_hold(vxlan->vn_sock);
1238 dev_hold(dev);
1239 queue_work(vxlan_wq, &vxlan->igmp_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001240 }
1241
1242 if (vxlan->age_interval)
1243 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1244
1245 return 0;
1246}
1247
1248/* Purge the forwarding table */
1249static void vxlan_flush(struct vxlan_dev *vxlan)
1250{
Cong Wang31fec5a2013-05-27 22:35:52 +00001251 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001252
1253 spin_lock_bh(&vxlan->hash_lock);
1254 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1255 struct hlist_node *p, *n;
1256 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1257 struct vxlan_fdb *f
1258 = container_of(p, struct vxlan_fdb, hlist);
1259 vxlan_fdb_destroy(vxlan, f);
1260 }
1261 }
1262 spin_unlock_bh(&vxlan->hash_lock);
1263}
1264
1265/* Cleanup timer and forwarding table on shutdown */
1266static int vxlan_stop(struct net_device *dev)
1267{
1268 struct vxlan_dev *vxlan = netdev_priv(dev);
1269
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001270 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1271 vxlan_sock_hold(vxlan->vn_sock);
1272 dev_hold(dev);
1273 queue_work(vxlan_wq, &vxlan->igmp_work);
1274 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001275
1276 del_timer_sync(&vxlan->age_timer);
1277
1278 vxlan_flush(vxlan);
1279
1280 return 0;
1281}
1282
stephen hemmingerd3428942012-10-01 12:32:35 +00001283/* Stub, nothing needs to be done. */
1284static void vxlan_set_multicast_list(struct net_device *dev)
1285{
1286}
1287
1288static const struct net_device_ops vxlan_netdev_ops = {
1289 .ndo_init = vxlan_init,
1290 .ndo_open = vxlan_open,
1291 .ndo_stop = vxlan_stop,
1292 .ndo_start_xmit = vxlan_xmit,
Pravin B Shelare8171042013-03-25 14:49:46 +00001293 .ndo_get_stats64 = ip_tunnel_get_stats64,
stephen hemmingerd3428942012-10-01 12:32:35 +00001294 .ndo_set_rx_mode = vxlan_set_multicast_list,
1295 .ndo_change_mtu = eth_change_mtu,
1296 .ndo_validate_addr = eth_validate_addr,
1297 .ndo_set_mac_address = eth_mac_addr,
1298 .ndo_fdb_add = vxlan_fdb_add,
1299 .ndo_fdb_del = vxlan_fdb_delete,
1300 .ndo_fdb_dump = vxlan_fdb_dump,
1301};
1302
1303/* Info for udev, that this is a virtual tunnel endpoint */
1304static struct device_type vxlan_type = {
1305 .name = "vxlan",
1306};
1307
1308static void vxlan_free(struct net_device *dev)
1309{
Pravin B Shelare8171042013-03-25 14:49:46 +00001310 free_percpu(dev->tstats);
stephen hemmingerd3428942012-10-01 12:32:35 +00001311 free_netdev(dev);
1312}
1313
1314/* Initialize the device structure. */
1315static void vxlan_setup(struct net_device *dev)
1316{
1317 struct vxlan_dev *vxlan = netdev_priv(dev);
Cong Wang31fec5a2013-05-27 22:35:52 +00001318 unsigned int h;
stephen hemminger05f47d62012-10-09 20:35:50 +00001319 int low, high;
stephen hemmingerd3428942012-10-01 12:32:35 +00001320
1321 eth_hw_addr_random(dev);
1322 ether_setup(dev);
stephen hemminger2840bf22012-10-09 20:35:51 +00001323 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001324
1325 dev->netdev_ops = &vxlan_netdev_ops;
1326 dev->destructor = vxlan_free;
1327 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1328
1329 dev->tx_queue_len = 0;
1330 dev->features |= NETIF_F_LLTX;
1331 dev->features |= NETIF_F_NETNS_LOCAL;
Joseph Gasparakisd6727fe2012-12-07 14:14:16 +00001332 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001333 dev->features |= NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001334 dev->features |= NETIF_F_GSO_SOFTWARE;
Joseph Gasparakis0afb1662012-12-07 14:14:18 +00001335
1336 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
Pravin B Shelar05c0db02013-03-07 13:22:36 +00001337 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001338 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
stephen hemminger6602d002012-12-31 12:00:21 +00001339 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
stephen hemmingerd3428942012-10-01 12:32:35 +00001340
stephen hemminger553675f2013-05-16 11:35:20 +00001341 INIT_LIST_HEAD(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001342 spin_lock_init(&vxlan->hash_lock);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001343 INIT_WORK(&vxlan->igmp_work, vxlan_igmp_work);
stephen hemmingerd3428942012-10-01 12:32:35 +00001344
1345 init_timer_deferrable(&vxlan->age_timer);
1346 vxlan->age_timer.function = vxlan_cleanup;
1347 vxlan->age_timer.data = (unsigned long) vxlan;
1348
stephen hemminger05f47d62012-10-09 20:35:50 +00001349 inet_get_local_port_range(&low, &high);
1350 vxlan->port_min = low;
1351 vxlan->port_max = high;
stephen hemminger823aa872013-04-27 11:31:57 +00001352 vxlan->dst_port = htons(vxlan_port);
stephen hemminger05f47d62012-10-09 20:35:50 +00001353
stephen hemmingerd3428942012-10-01 12:32:35 +00001354 vxlan->dev = dev;
1355
1356 for (h = 0; h < FDB_HASH_SIZE; ++h)
1357 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1358}
1359
1360static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1361 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
stephen hemminger5d174dd2013-04-27 11:31:55 +00001362 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
stephen hemmingerd3428942012-10-01 12:32:35 +00001363 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1364 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1365 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1366 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1367 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1368 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1369 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
stephen hemminger05f47d62012-10-09 20:35:50 +00001370 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
David Stevense4f67ad2012-11-20 02:50:14 +00001371 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1372 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1373 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1374 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
stephen hemminger823aa872013-04-27 11:31:57 +00001375 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
stephen hemmingerd3428942012-10-01 12:32:35 +00001376};
1377
1378static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1379{
1380 if (tb[IFLA_ADDRESS]) {
1381 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1382 pr_debug("invalid link address (not ethernet)\n");
1383 return -EINVAL;
1384 }
1385
1386 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1387 pr_debug("invalid all zero ethernet address\n");
1388 return -EADDRNOTAVAIL;
1389 }
1390 }
1391
1392 if (!data)
1393 return -EINVAL;
1394
1395 if (data[IFLA_VXLAN_ID]) {
1396 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1397 if (id >= VXLAN_VID_MASK)
1398 return -ERANGE;
1399 }
1400
stephen hemminger05f47d62012-10-09 20:35:50 +00001401 if (data[IFLA_VXLAN_PORT_RANGE]) {
1402 const struct ifla_vxlan_port_range *p
1403 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1404
1405 if (ntohs(p->high) < ntohs(p->low)) {
1406 pr_debug("port range %u .. %u not valid\n",
1407 ntohs(p->low), ntohs(p->high));
1408 return -EINVAL;
1409 }
1410 }
1411
stephen hemmingerd3428942012-10-01 12:32:35 +00001412 return 0;
1413}
1414
Yan Burman1b13c972013-01-29 23:43:07 +00001415static void vxlan_get_drvinfo(struct net_device *netdev,
1416 struct ethtool_drvinfo *drvinfo)
1417{
1418 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1419 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1420}
1421
1422static const struct ethtool_ops vxlan_ethtool_ops = {
1423 .get_drvinfo = vxlan_get_drvinfo,
1424 .get_link = ethtool_op_get_link,
1425};
1426
stephen hemminger553675f2013-05-16 11:35:20 +00001427static void vxlan_del_work(struct work_struct *work)
1428{
1429 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1430
1431 sk_release_kernel(vs->sock->sk);
1432 kfree_rcu(vs, rcu);
1433}
1434
1435/* Create new listen socket if needed */
1436static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1437{
1438 struct vxlan_sock *vs;
1439 struct sock *sk;
1440 struct sockaddr_in vxlan_addr = {
1441 .sin_family = AF_INET,
1442 .sin_addr.s_addr = htonl(INADDR_ANY),
1443 };
1444 int rc;
Cong Wang31fec5a2013-05-27 22:35:52 +00001445 unsigned int h;
stephen hemminger553675f2013-05-16 11:35:20 +00001446
1447 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1448 if (!vs)
1449 return ERR_PTR(-ENOMEM);
1450
1451 for (h = 0; h < VNI_HASH_SIZE; ++h)
1452 INIT_HLIST_HEAD(&vs->vni_list[h]);
1453
1454 INIT_WORK(&vs->del_work, vxlan_del_work);
1455
1456 /* Create UDP socket for encapsulation receive. */
1457 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1458 if (rc < 0) {
1459 pr_debug("UDP socket create failed\n");
1460 kfree(vs);
1461 return ERR_PTR(rc);
1462 }
1463
1464 /* Put in proper namespace */
1465 sk = vs->sock->sk;
1466 sk_change_net(sk, net);
1467
1468 vxlan_addr.sin_port = port;
1469
1470 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1471 sizeof(vxlan_addr));
1472 if (rc < 0) {
1473 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1474 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1475 sk_release_kernel(sk);
1476 kfree(vs);
1477 return ERR_PTR(rc);
1478 }
1479
1480 /* Disable multicast loopback */
1481 inet_sk(sk)->mc_loop = 0;
1482
1483 /* Mark socket as an encapsulation socket. */
1484 udp_sk(sk)->encap_type = 1;
1485 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1486 udp_encap_enable();
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001487 atomic_set(&vs->refcnt, 1);
stephen hemminger553675f2013-05-16 11:35:20 +00001488
stephen hemminger553675f2013-05-16 11:35:20 +00001489 return vs;
1490}
1491
stephen hemmingerd3428942012-10-01 12:32:35 +00001492static int vxlan_newlink(struct net *net, struct net_device *dev,
1493 struct nlattr *tb[], struct nlattr *data[])
1494{
stephen hemminger553675f2013-05-16 11:35:20 +00001495 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
stephen hemmingerd3428942012-10-01 12:32:35 +00001496 struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001497 struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger553675f2013-05-16 11:35:20 +00001498 struct vxlan_sock *vs;
stephen hemmingerd3428942012-10-01 12:32:35 +00001499 __u32 vni;
1500 int err;
1501
1502 if (!data[IFLA_VXLAN_ID])
1503 return -EINVAL;
1504
1505 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001506 dst->remote_vni = vni;
stephen hemmingerd3428942012-10-01 12:32:35 +00001507
stephen hemminger5d174dd2013-04-27 11:31:55 +00001508 if (data[IFLA_VXLAN_GROUP])
1509 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001510
1511 if (data[IFLA_VXLAN_LOCAL])
1512 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1513
stephen hemminger34e02aa2012-10-09 20:35:53 +00001514 if (data[IFLA_VXLAN_LINK] &&
Atzm Watanabec7995c42013-04-16 02:50:52 +00001515 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
stephen hemminger34e02aa2012-10-09 20:35:53 +00001516 struct net_device *lowerdev
Atzm Watanabec7995c42013-04-16 02:50:52 +00001517 = __dev_get_by_index(net, dst->remote_ifindex);
stephen hemmingerd3428942012-10-01 12:32:35 +00001518
stephen hemminger34e02aa2012-10-09 20:35:53 +00001519 if (!lowerdev) {
Atzm Watanabec7995c42013-04-16 02:50:52 +00001520 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
stephen hemminger34e02aa2012-10-09 20:35:53 +00001521 return -ENODEV;
stephen hemmingerd3428942012-10-01 12:32:35 +00001522 }
stephen hemminger34e02aa2012-10-09 20:35:53 +00001523
1524 if (!tb[IFLA_MTU])
1525 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
Alexander Duyck1ba56fb2012-11-13 13:10:59 +00001526
1527 /* update header length based on lower device */
1528 dev->hard_header_len = lowerdev->hard_header_len +
1529 VXLAN_HEADROOM;
stephen hemmingerd3428942012-10-01 12:32:35 +00001530 }
1531
1532 if (data[IFLA_VXLAN_TOS])
1533 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1534
Vincent Bernatafb97182012-10-30 10:27:16 +00001535 if (data[IFLA_VXLAN_TTL])
1536 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1537
stephen hemmingerd3428942012-10-01 12:32:35 +00001538 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
David Stevense4f67ad2012-11-20 02:50:14 +00001539 vxlan->flags |= VXLAN_F_LEARN;
stephen hemmingerd3428942012-10-01 12:32:35 +00001540
1541 if (data[IFLA_VXLAN_AGEING])
1542 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1543 else
1544 vxlan->age_interval = FDB_AGE_DEFAULT;
1545
David Stevense4f67ad2012-11-20 02:50:14 +00001546 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1547 vxlan->flags |= VXLAN_F_PROXY;
1548
1549 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1550 vxlan->flags |= VXLAN_F_RSC;
1551
1552 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1553 vxlan->flags |= VXLAN_F_L2MISS;
1554
1555 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1556 vxlan->flags |= VXLAN_F_L3MISS;
1557
stephen hemmingerd3428942012-10-01 12:32:35 +00001558 if (data[IFLA_VXLAN_LIMIT])
1559 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1560
stephen hemminger05f47d62012-10-09 20:35:50 +00001561 if (data[IFLA_VXLAN_PORT_RANGE]) {
1562 const struct ifla_vxlan_port_range *p
1563 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1564 vxlan->port_min = ntohs(p->low);
1565 vxlan->port_max = ntohs(p->high);
1566 }
1567
stephen hemminger823aa872013-04-27 11:31:57 +00001568 if (data[IFLA_VXLAN_PORT])
1569 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1570
stephen hemminger553675f2013-05-16 11:35:20 +00001571 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1572 pr_info("duplicate VNI %u\n", vni);
1573 return -EEXIST;
1574 }
1575
1576 vs = vxlan_find_port(net, vxlan->dst_port);
1577 if (vs)
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001578 atomic_inc(&vs->refcnt);
stephen hemminger553675f2013-05-16 11:35:20 +00001579 else {
1580 /* Drop lock because socket create acquires RTNL lock */
1581 rtnl_unlock();
1582 vs = vxlan_socket_create(net, vxlan->dst_port);
1583 rtnl_lock();
1584 if (IS_ERR(vs))
1585 return PTR_ERR(vs);
1586
1587 hlist_add_head_rcu(&vs->hlist, vs_head(net, vxlan->dst_port));
1588 }
1589 vxlan->vn_sock = vs;
1590
Yan Burman1b13c972013-01-29 23:43:07 +00001591 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1592
stephen hemmingerd3428942012-10-01 12:32:35 +00001593 err = register_netdevice(dev);
stephen hemminger553675f2013-05-16 11:35:20 +00001594 if (err) {
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001595 vxlan_sock_release(vs);
stephen hemminger553675f2013-05-16 11:35:20 +00001596 return err;
1597 }
stephen hemmingerd3428942012-10-01 12:32:35 +00001598
stephen hemminger553675f2013-05-16 11:35:20 +00001599 list_add(&vxlan->next, &vn->vxlan_list);
1600 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1601
1602 return 0;
stephen hemmingerd3428942012-10-01 12:32:35 +00001603}
1604
1605static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1606{
1607 struct vxlan_dev *vxlan = netdev_priv(dev);
stephen hemminger553675f2013-05-16 11:35:20 +00001608 struct vxlan_sock *vs = vxlan->vn_sock;
stephen hemmingerd3428942012-10-01 12:32:35 +00001609
1610 hlist_del_rcu(&vxlan->hlist);
stephen hemminger553675f2013-05-16 11:35:20 +00001611 list_del(&vxlan->next);
stephen hemmingerd3428942012-10-01 12:32:35 +00001612 unregister_netdevice_queue(dev, head);
Stephen Hemminger7c47ced2013-06-17 14:16:10 -07001613 vxlan_sock_release(vs);
stephen hemmingerd3428942012-10-01 12:32:35 +00001614}
1615
1616static size_t vxlan_get_size(const struct net_device *dev)
1617{
1618
1619 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
stephen hemminger5d174dd2013-04-27 11:31:55 +00001620 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
stephen hemmingerd3428942012-10-01 12:32:35 +00001621 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1622 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1623 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1624 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1625 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
David Stevense4f67ad2012-11-20 02:50:14 +00001626 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1627 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1628 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1629 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
stephen hemmingerd3428942012-10-01 12:32:35 +00001630 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1631 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
stephen hemminger05f47d62012-10-09 20:35:50 +00001632 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
stephen hemminger823aa872013-04-27 11:31:57 +00001633 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
stephen hemmingerd3428942012-10-01 12:32:35 +00001634 0;
1635}
1636
1637static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1638{
1639 const struct vxlan_dev *vxlan = netdev_priv(dev);
Atzm Watanabec7995c42013-04-16 02:50:52 +00001640 const struct vxlan_rdst *dst = &vxlan->default_dst;
stephen hemminger05f47d62012-10-09 20:35:50 +00001641 struct ifla_vxlan_port_range ports = {
1642 .low = htons(vxlan->port_min),
1643 .high = htons(vxlan->port_max),
1644 };
stephen hemmingerd3428942012-10-01 12:32:35 +00001645
Atzm Watanabec7995c42013-04-16 02:50:52 +00001646 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
stephen hemmingerd3428942012-10-01 12:32:35 +00001647 goto nla_put_failure;
1648
stephen hemminger5d174dd2013-04-27 11:31:55 +00001649 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
stephen hemmingerd3428942012-10-01 12:32:35 +00001650 goto nla_put_failure;
1651
Atzm Watanabec7995c42013-04-16 02:50:52 +00001652 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
stephen hemmingerd3428942012-10-01 12:32:35 +00001653 goto nla_put_failure;
1654
Stephen Hemminger7c41c422012-10-08 14:55:30 -07001655 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
stephen hemmingerd3428942012-10-01 12:32:35 +00001656 goto nla_put_failure;
1657
1658 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1659 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
David Stevense4f67ad2012-11-20 02:50:14 +00001660 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1661 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1662 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1663 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1664 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1665 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1666 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1667 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1668 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
stephen hemmingerd3428942012-10-01 12:32:35 +00001669 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
stephen hemminger823aa872013-04-27 11:31:57 +00001670 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1671 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
stephen hemmingerd3428942012-10-01 12:32:35 +00001672 goto nla_put_failure;
1673
stephen hemminger05f47d62012-10-09 20:35:50 +00001674 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1675 goto nla_put_failure;
1676
stephen hemmingerd3428942012-10-01 12:32:35 +00001677 return 0;
1678
1679nla_put_failure:
1680 return -EMSGSIZE;
1681}
1682
1683static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1684 .kind = "vxlan",
1685 .maxtype = IFLA_VXLAN_MAX,
1686 .policy = vxlan_policy,
1687 .priv_size = sizeof(struct vxlan_dev),
1688 .setup = vxlan_setup,
1689 .validate = vxlan_validate,
1690 .newlink = vxlan_newlink,
1691 .dellink = vxlan_dellink,
1692 .get_size = vxlan_get_size,
1693 .fill_info = vxlan_fill_info,
1694};
1695
1696static __net_init int vxlan_init_net(struct net *net)
1697{
1698 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Cong Wang31fec5a2013-05-27 22:35:52 +00001699 unsigned int h;
stephen hemmingerd3428942012-10-01 12:32:35 +00001700
stephen hemminger553675f2013-05-16 11:35:20 +00001701 INIT_LIST_HEAD(&vn->vxlan_list);
stephen hemmingerd3428942012-10-01 12:32:35 +00001702
stephen hemminger553675f2013-05-16 11:35:20 +00001703 for (h = 0; h < PORT_HASH_SIZE; ++h)
1704 INIT_HLIST_HEAD(&vn->sock_list[h]);
stephen hemmingerd3428942012-10-01 12:32:35 +00001705
1706 return 0;
1707}
1708
1709static __net_exit void vxlan_exit_net(struct net *net)
1710{
1711 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001712 struct vxlan_dev *vxlan;
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001713
1714 rtnl_lock();
stephen hemminger553675f2013-05-16 11:35:20 +00001715 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1716 dev_close(vxlan->dev);
Zang MingJie9cb6cb72013-03-06 04:37:37 +00001717 rtnl_unlock();
stephen hemmingerd3428942012-10-01 12:32:35 +00001718}
1719
1720static struct pernet_operations vxlan_net_ops = {
1721 .init = vxlan_init_net,
1722 .exit = vxlan_exit_net,
1723 .id = &vxlan_net_id,
1724 .size = sizeof(struct vxlan_net),
1725};
1726
1727static int __init vxlan_init_module(void)
1728{
1729 int rc;
1730
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001731 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1732 if (!vxlan_wq)
1733 return -ENOMEM;
1734
stephen hemmingerd3428942012-10-01 12:32:35 +00001735 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1736
1737 rc = register_pernet_device(&vxlan_net_ops);
1738 if (rc)
1739 goto out1;
1740
1741 rc = rtnl_link_register(&vxlan_link_ops);
1742 if (rc)
1743 goto out2;
1744
1745 return 0;
1746
1747out2:
1748 unregister_pernet_device(&vxlan_net_ops);
1749out1:
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001750 destroy_workqueue(vxlan_wq);
stephen hemmingerd3428942012-10-01 12:32:35 +00001751 return rc;
1752}
Cong Wang7332a132013-05-27 22:35:53 +00001753late_initcall(vxlan_init_module);
stephen hemmingerd3428942012-10-01 12:32:35 +00001754
1755static void __exit vxlan_cleanup_module(void)
1756{
stephen hemmingerd3428942012-10-01 12:32:35 +00001757 unregister_pernet_device(&vxlan_net_ops);
Stephen Hemmingerb7153982013-06-17 14:16:09 -07001758 rtnl_link_unregister(&vxlan_link_ops);
Stephen Hemminger758c57d2013-06-17 14:16:09 -07001759 destroy_workqueue(vxlan_wq);
David Stevens66817122013-03-15 04:35:51 +00001760 rcu_barrier();
stephen hemmingerd3428942012-10-01 12:32:35 +00001761}
1762module_exit(vxlan_cleanup_module);
1763
1764MODULE_LICENSE("GPL");
1765MODULE_VERSION(VXLAN_VERSION);
stephen hemminger3b8df3c2013-04-27 11:31:52 +00001766MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
stephen hemmingerd3428942012-10-01 12:32:35 +00001767MODULE_ALIAS_RTNL_LINK("vxlan");