]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/net/vxlan.c
vxlan: introduce vxlan_fdb_parse
[mirror_ubuntu-artful-kernel.git] / drivers / net / vxlan.c
CommitLineData
d342894c 1/*
eb5ce439 2 * VXLAN: Virtual eXtensible Local Area Network
d342894c 3 *
3b8df3c6 4 * Copyright (c) 2012-2013 Vyatta Inc.
d342894c 5 *
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
d342894c 11 * - 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>
d342894c 30#include <linux/hash.h>
1b13c97f 31#include <linux/ethtool.h>
e4f67add
DS
32#include <net/arp.h>
33#include <net/ndisc.h>
d342894c 34#include <net/ip.h>
c5441932 35#include <net/ip_tunnels.h>
d342894c 36#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
553675fb 47#define PORT_HASH_BITS 8
48#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
d342894c 49#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)
52b702ff
AD
58/* IP header + UDP + VXLAN + Ethernet header */
59#define VXLAN_HEADROOM (20 + 8 + 8 + 14)
d342894c 60
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
23c578bf 69/* UDP port for VXLAN traffic.
70 * The IANA assigned port is 4789, but the Linux default is 8472
234f5b73 71 * for compatibility with early adopters.
23c578bf 72 */
9daaa397
SH
73static unsigned short vxlan_port __read_mostly = 8472;
74module_param_named(udp_port, vxlan_port, ushort, 0444);
d342894c 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
60d9d4c6 81static int vxlan_net_id;
553675fb 82
afbd8bae
MR
83static const u8 all_zeros_mac[ETH_ALEN];
84
553675fb 85/* per UDP socket information */
86struct vxlan_sock {
87 struct hlist_node hlist;
88 struct rcu_head rcu;
89 struct work_struct del_work;
7c47cedf 90 atomic_t refcnt;
553675fb 91 struct socket *sock;
d342894c 92 struct hlist_head vni_list[VNI_HASH_SIZE];
93};
94
553675fb 95/* per-network namespace private data for this module */
96struct vxlan_net {
97 struct list_head vxlan_list;
98 struct hlist_head sock_list[PORT_HASH_SIZE];
1c51a915 99 spinlock_t sock_lock;
553675fb 100};
101
6681712d 102struct vxlan_rdst {
6681712d
DS
103 __be32 remote_ip;
104 __be16 remote_port;
105 u32 remote_vni;
106 u32 remote_ifindex;
3e61aa8f 107 struct list_head list;
6681712d
DS
108};
109
d342894c 110/* Forwarding table entry */
111struct vxlan_fdb {
112 struct hlist_node hlist; /* linked list of entries */
113 struct rcu_head rcu;
114 unsigned long updated; /* jiffies */
115 unsigned long used;
3e61aa8f 116 struct list_head remotes;
d342894c 117 u16 state; /* see ndm_state */
ae884082 118 u8 flags; /* see ndm_flags */
d342894c 119 u8 eth_addr[ETH_ALEN];
120};
121
d342894c 122/* Pseudo network device */
123struct vxlan_dev {
553675fb 124 struct hlist_node hlist; /* vni hash table */
125 struct list_head next; /* vxlan's per namespace list */
126 struct vxlan_sock *vn_sock; /* listening socket */
d342894c 127 struct net_device *dev;
c7995c43 128 struct vxlan_rdst default_dst; /* default destination */
d342894c 129 __be32 saddr; /* source address */
823aa873 130 __be16 dst_port;
05f47d69 131 __u16 port_min; /* source port range */
132 __u16 port_max;
d342894c 133 __u8 tos; /* TOS override */
134 __u8 ttl;
e4f67add 135 u32 flags; /* VXLAN_F_* below */
d342894c 136
1c51a915 137 struct work_struct sock_work;
7c47cedf 138 struct work_struct igmp_work;
1c51a915 139
d342894c 140 unsigned long age_interval;
141 struct timer_list age_timer;
142 spinlock_t hash_lock;
143 unsigned int addrcnt;
144 unsigned int addrmax;
d342894c 145
146 struct hlist_head fdb_head[FDB_HASH_SIZE];
147};
148
e4f67add
DS
149#define VXLAN_F_LEARN 0x01
150#define VXLAN_F_PROXY 0x02
151#define VXLAN_F_RSC 0x04
152#define VXLAN_F_L2MISS 0x08
153#define VXLAN_F_L3MISS 0x10
154
d342894c 155/* salt for hash table */
156static u32 vxlan_salt __read_mostly;
758c57d1 157static struct workqueue_struct *vxlan_wq;
d342894c 158
1c51a915
SH
159static void vxlan_sock_work(struct work_struct *work);
160
553675fb 161/* Virtual Network hash table head */
162static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
163{
164 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
165}
166
167/* Socket hash table head */
168static inline struct hlist_head *vs_head(struct net *net, __be16 port)
d342894c 169{
170 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
171
553675fb 172 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
173}
174
3e61aa8f
SH
175/* First remote destination for a forwarding entry.
176 * Guaranteed to be non-NULL because remotes are never deleted.
177 */
178static inline struct vxlan_rdst *first_remote(struct vxlan_fdb *fdb)
179{
180 return list_first_or_null_rcu(&fdb->remotes, struct vxlan_rdst, list);
181}
182
553675fb 183/* Find VXLAN socket based on network namespace and UDP port */
184static struct vxlan_sock *vxlan_find_port(struct net *net, __be16 port)
185{
186 struct vxlan_sock *vs;
187
188 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
189 if (inet_sk(vs->sock->sk)->inet_sport == port)
190 return vs;
191 }
192 return NULL;
d342894c 193}
194
195/* Look up VNI in a per net namespace table */
553675fb 196static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
d342894c 197{
553675fb 198 struct vxlan_sock *vs;
d342894c 199 struct vxlan_dev *vxlan;
d342894c 200
553675fb 201 vs = vxlan_find_port(net, port);
202 if (!vs)
203 return NULL;
204
205 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
c7995c43 206 if (vxlan->default_dst.remote_vni == id)
d342894c 207 return vxlan;
208 }
209
210 return NULL;
211}
212
213/* Fill in neighbour message in skbuff. */
214static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
234f5b73
SH
215 const struct vxlan_fdb *fdb,
216 u32 portid, u32 seq, int type, unsigned int flags,
217 const struct vxlan_rdst *rdst)
d342894c 218{
219 unsigned long now = jiffies;
220 struct nda_cacheinfo ci;
221 struct nlmsghdr *nlh;
222 struct ndmsg *ndm;
e4f67add 223 bool send_ip, send_eth;
d342894c 224
225 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
226 if (nlh == NULL)
227 return -EMSGSIZE;
228
229 ndm = nlmsg_data(nlh);
230 memset(ndm, 0, sizeof(*ndm));
e4f67add
DS
231
232 send_eth = send_ip = true;
233
234 if (type == RTM_GETNEIGH) {
235 ndm->ndm_family = AF_INET;
6681712d 236 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
e4f67add
DS
237 send_eth = !is_zero_ether_addr(fdb->eth_addr);
238 } else
239 ndm->ndm_family = AF_BRIDGE;
d342894c 240 ndm->ndm_state = fdb->state;
241 ndm->ndm_ifindex = vxlan->dev->ifindex;
ae884082 242 ndm->ndm_flags = fdb->flags;
d342894c 243 ndm->ndm_type = NDA_DST;
244
e4f67add 245 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
d342894c 246 goto nla_put_failure;
247
6681712d
DS
248 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
249 goto nla_put_failure;
250
823aa873 251 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
6681712d
DS
252 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
253 goto nla_put_failure;
c7995c43 254 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
60d9d4c6 255 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
6681712d
DS
256 goto nla_put_failure;
257 if (rdst->remote_ifindex &&
258 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
d342894c 259 goto nla_put_failure;
260
261 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
262 ci.ndm_confirmed = 0;
263 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
264 ci.ndm_refcnt = 0;
265
266 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
267 goto nla_put_failure;
268
269 return nlmsg_end(skb, nlh);
270
271nla_put_failure:
272 nlmsg_cancel(skb, nlh);
273 return -EMSGSIZE;
274}
275
276static inline size_t vxlan_nlmsg_size(void)
277{
278 return NLMSG_ALIGN(sizeof(struct ndmsg))
279 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
280 + nla_total_size(sizeof(__be32)) /* NDA_DST */
73cf3317 281 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
6681712d
DS
282 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
283 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
d342894c 284 + nla_total_size(sizeof(struct nda_cacheinfo));
285}
286
287static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
3e61aa8f 288 struct vxlan_fdb *fdb, int type)
d342894c 289{
290 struct net *net = dev_net(vxlan->dev);
291 struct sk_buff *skb;
292 int err = -ENOBUFS;
293
294 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
295 if (skb == NULL)
296 goto errout;
297
3e61aa8f 298 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, first_remote(fdb));
d342894c 299 if (err < 0) {
300 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
301 WARN_ON(err == -EMSGSIZE);
302 kfree_skb(skb);
303 goto errout;
304 }
305
306 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
307 return;
308errout:
309 if (err < 0)
310 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
311}
312
e4f67add
DS
313static void vxlan_ip_miss(struct net_device *dev, __be32 ipa)
314{
315 struct vxlan_dev *vxlan = netdev_priv(dev);
bb3fd687
SH
316 struct vxlan_fdb f = {
317 .state = NUD_STALE,
318 };
319 struct vxlan_rdst remote = {
320 .remote_ip = ipa, /* goes to NDA_DST */
321 .remote_vni = VXLAN_N_VID,
322 };
3e61aa8f
SH
323
324 INIT_LIST_HEAD(&f.remotes);
325 list_add_rcu(&remote.list, &f.remotes);
e4f67add
DS
326
327 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
328}
329
330static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
331{
bb3fd687
SH
332 struct vxlan_fdb f = {
333 .state = NUD_STALE,
334 };
e4f67add 335
3e61aa8f 336 INIT_LIST_HEAD(&f.remotes);
e4f67add
DS
337 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
338
339 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
340}
341
d342894c 342/* Hash Ethernet address */
343static u32 eth_hash(const unsigned char *addr)
344{
345 u64 value = get_unaligned((u64 *)addr);
346
347 /* only want 6 bytes */
348#ifdef __BIG_ENDIAN
d342894c 349 value >>= 16;
321fb991 350#else
351 value <<= 16;
d342894c 352#endif
353 return hash_64(value, FDB_HASH_BITS);
354}
355
356/* Hash chain to use given mac address */
357static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
358 const u8 *mac)
359{
360 return &vxlan->fdb_head[eth_hash(mac)];
361}
362
363/* Look up Ethernet address in forwarding table */
014be2c8 364static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
d342894c 365 const u8 *mac)
366
367{
368 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
369 struct vxlan_fdb *f;
d342894c 370
b67bfe0d 371 hlist_for_each_entry_rcu(f, head, hlist) {
d342894c 372 if (compare_ether_addr(mac, f->eth_addr) == 0)
373 return f;
374 }
375
376 return NULL;
377}
378
014be2c8
SS
379static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
380 const u8 *mac)
381{
382 struct vxlan_fdb *f;
383
384 f = __vxlan_find_mac(vxlan, mac);
385 if (f)
386 f->used = jiffies;
387
388 return f;
389}
390
a5e7c10a
MR
391/* caller should hold vxlan->hash_lock */
392static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
393 __be32 ip, __be16 port,
394 __u32 vni, __u32 ifindex)
6681712d 395{
3e61aa8f 396 struct vxlan_rdst *rd;
6681712d 397
3e61aa8f 398 list_for_each_entry(rd, &f->remotes, list) {
6681712d
DS
399 if (rd->remote_ip == ip &&
400 rd->remote_port == port &&
401 rd->remote_vni == vni &&
402 rd->remote_ifindex == ifindex)
a5e7c10a 403 return rd;
6681712d 404 }
3e61aa8f 405
a5e7c10a
MR
406 return NULL;
407}
408
409/* Add/update destinations for multicast */
410static int vxlan_fdb_append(struct vxlan_fdb *f,
411 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
412{
413 struct vxlan_rdst *rd;
414
415 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
416 if (rd)
417 return 0;
418
6681712d
DS
419 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
420 if (rd == NULL)
421 return -ENOBUFS;
422 rd->remote_ip = ip;
423 rd->remote_port = port;
424 rd->remote_vni = vni;
425 rd->remote_ifindex = ifindex;
3e61aa8f
SH
426
427 list_add_tail_rcu(&rd->list, &f->remotes);
428
6681712d
DS
429 return 1;
430}
431
d342894c 432/* Add new entry to forwarding table -- assumes lock held */
433static int vxlan_fdb_create(struct vxlan_dev *vxlan,
434 const u8 *mac, __be32 ip,
6681712d 435 __u16 state, __u16 flags,
73cf3317 436 __be16 port, __u32 vni, __u32 ifindex,
ae884082 437 __u8 ndm_flags)
d342894c 438{
439 struct vxlan_fdb *f;
440 int notify = 0;
441
014be2c8 442 f = __vxlan_find_mac(vxlan, mac);
d342894c 443 if (f) {
444 if (flags & NLM_F_EXCL) {
445 netdev_dbg(vxlan->dev,
446 "lost race to create %pM\n", mac);
447 return -EEXIST;
448 }
449 if (f->state != state) {
450 f->state = state;
451 f->updated = jiffies;
452 notify = 1;
453 }
ae884082
DS
454 if (f->flags != ndm_flags) {
455 f->flags = ndm_flags;
456 f->updated = jiffies;
457 notify = 1;
458 }
6681712d
DS
459 if ((flags & NLM_F_APPEND) &&
460 is_multicast_ether_addr(f->eth_addr)) {
461 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
462
463 if (rc < 0)
464 return rc;
465 notify |= rc;
466 }
d342894c 467 } else {
468 if (!(flags & NLM_F_CREATE))
469 return -ENOENT;
470
471 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
472 return -ENOSPC;
473
474 netdev_dbg(vxlan->dev, "add %pM -> %pI4\n", mac, &ip);
475 f = kmalloc(sizeof(*f), GFP_ATOMIC);
476 if (!f)
477 return -ENOMEM;
478
479 notify = 1;
d342894c 480 f->state = state;
ae884082 481 f->flags = ndm_flags;
d342894c 482 f->updated = f->used = jiffies;
3e61aa8f 483 INIT_LIST_HEAD(&f->remotes);
d342894c 484 memcpy(f->eth_addr, mac, ETH_ALEN);
485
3e61aa8f
SH
486 vxlan_fdb_append(f, ip, port, vni, ifindex);
487
d342894c 488 ++vxlan->addrcnt;
489 hlist_add_head_rcu(&f->hlist,
490 vxlan_fdb_head(vxlan, mac));
491 }
492
493 if (notify)
494 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
495
496 return 0;
497}
498
6706c82e 499static void vxlan_fdb_free(struct rcu_head *head)
6681712d
DS
500{
501 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
3e61aa8f 502 struct vxlan_rdst *rd, *nd;
6681712d 503
3e61aa8f 504 list_for_each_entry_safe(rd, nd, &f->remotes, list)
6681712d 505 kfree(rd);
6681712d
DS
506 kfree(f);
507}
508
d342894c 509static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
510{
511 netdev_dbg(vxlan->dev,
512 "delete %pM\n", f->eth_addr);
513
514 --vxlan->addrcnt;
515 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
516
517 hlist_del_rcu(&f->hlist);
6681712d 518 call_rcu(&f->rcu, vxlan_fdb_free);
d342894c 519}
520
f0b074be
MR
521static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
522 __be32 *ip, __be16 *port, u32 *vni, u32 *ifindex)
d342894c 523{
6681712d 524 struct net *net = dev_net(vxlan->dev);
d342894c 525
f0b074be
MR
526 if (tb[NDA_DST]) {
527 if (nla_len(tb[NDA_DST]) != sizeof(__be32))
528 return -EAFNOSUPPORT;
d342894c 529
f0b074be
MR
530 *ip = nla_get_be32(tb[NDA_DST]);
531 } else {
532 *ip = htonl(INADDR_ANY);
533 }
d342894c 534
6681712d 535 if (tb[NDA_PORT]) {
73cf3317 536 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
6681712d 537 return -EINVAL;
f0b074be
MR
538 *port = nla_get_be16(tb[NDA_PORT]);
539 } else {
540 *port = vxlan->dst_port;
541 }
6681712d
DS
542
543 if (tb[NDA_VNI]) {
544 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
545 return -EINVAL;
f0b074be
MR
546 *vni = nla_get_u32(tb[NDA_VNI]);
547 } else {
548 *vni = vxlan->default_dst.remote_vni;
549 }
6681712d
DS
550
551 if (tb[NDA_IFINDEX]) {
5abb0029 552 struct net_device *tdev;
6681712d
DS
553
554 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
555 return -EINVAL;
f0b074be
MR
556 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
557 tdev = dev_get_by_index(net, *ifindex);
5abb0029 558 if (!tdev)
6681712d 559 return -EADDRNOTAVAIL;
5abb0029 560 dev_put(tdev);
f0b074be
MR
561 } else {
562 *ifindex = 0;
563 }
564
565 return 0;
566}
567
568/* Add static entry (via netlink) */
569static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
570 struct net_device *dev,
571 const unsigned char *addr, u16 flags)
572{
573 struct vxlan_dev *vxlan = netdev_priv(dev);
574 /* struct net *net = dev_net(vxlan->dev); */
575 __be32 ip;
576 __be16 port;
577 u32 vni, ifindex;
578 int err;
579
580 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
581 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
582 ndm->ndm_state);
583 return -EINVAL;
584 }
585
586 if (tb[NDA_DST] == NULL)
587 return -EINVAL;
588
589 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
590 if (err)
591 return err;
6681712d 592
d342894c 593 spin_lock_bh(&vxlan->hash_lock);
73cf3317 594 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
595 port, vni, ifindex, ndm->ndm_flags);
d342894c 596 spin_unlock_bh(&vxlan->hash_lock);
597
598 return err;
599}
600
601/* Delete entry (via netlink) */
1690be63
VY
602static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
603 struct net_device *dev,
d342894c 604 const unsigned char *addr)
605{
606 struct vxlan_dev *vxlan = netdev_priv(dev);
607 struct vxlan_fdb *f;
608 int err = -ENOENT;
609
610 spin_lock_bh(&vxlan->hash_lock);
611 f = vxlan_find_mac(vxlan, addr);
612 if (f) {
613 vxlan_fdb_destroy(vxlan, f);
614 err = 0;
615 }
616 spin_unlock_bh(&vxlan->hash_lock);
617
618 return err;
619}
620
621/* Dump forwarding table */
622static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
623 struct net_device *dev, int idx)
624{
625 struct vxlan_dev *vxlan = netdev_priv(dev);
626 unsigned int h;
627
628 for (h = 0; h < FDB_HASH_SIZE; ++h) {
629 struct vxlan_fdb *f;
d342894c 630 int err;
631
b67bfe0d 632 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
6681712d 633 struct vxlan_rdst *rd;
6681712d 634
3e61aa8f
SH
635 if (idx < cb->args[0])
636 goto skip;
637
638 list_for_each_entry_rcu(rd, &f->remotes, list) {
6681712d
DS
639 err = vxlan_fdb_info(skb, vxlan, f,
640 NETLINK_CB(cb->skb).portid,
641 cb->nlh->nlmsg_seq,
642 RTM_NEWNEIGH,
643 NLM_F_MULTI, rd);
644 if (err < 0)
3e61aa8f 645 goto out;
6681712d 646 }
3e61aa8f
SH
647skip:
648 ++idx;
d342894c 649 }
650 }
3e61aa8f 651out:
d342894c 652 return idx;
653}
654
655/* Watch incoming packets to learn mapping between Ethernet address
656 * and Tunnel endpoint.
26a41ae6 657 * Return true if packet is bogus and should be droppped.
d342894c 658 */
26a41ae6 659static bool vxlan_snoop(struct net_device *dev,
d342894c 660 __be32 src_ip, const u8 *src_mac)
661{
662 struct vxlan_dev *vxlan = netdev_priv(dev);
663 struct vxlan_fdb *f;
d342894c 664
665 f = vxlan_find_mac(vxlan, src_mac);
666 if (likely(f)) {
3e61aa8f
SH
667 struct vxlan_rdst *rdst = first_remote(f);
668
669 if (likely(rdst->remote_ip == src_ip))
26a41ae6 670 return false;
671
672 /* Don't migrate static entries, drop packets */
eb064c3b 673 if (f->state & NUD_NOARP)
26a41ae6 674 return true;
d342894c 675
676 if (net_ratelimit())
677 netdev_info(dev,
678 "%pM migrated from %pI4 to %pI4\n",
3e61aa8f 679 src_mac, &rdst->remote_ip, &src_ip);
d342894c 680
3e61aa8f 681 rdst->remote_ip = src_ip;
d342894c 682 f->updated = jiffies;
8385f50a 683 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
d342894c 684 } else {
685 /* learned new entry */
686 spin_lock(&vxlan->hash_lock);
3bf74b1a 687
688 /* close off race between vxlan_flush and incoming packets */
689 if (netif_running(dev))
690 vxlan_fdb_create(vxlan, src_mac, src_ip,
691 NUD_REACHABLE,
692 NLM_F_EXCL|NLM_F_CREATE,
693 vxlan->dst_port,
694 vxlan->default_dst.remote_vni,
695 0, NTF_SELF);
d342894c 696 spin_unlock(&vxlan->hash_lock);
697 }
26a41ae6 698
699 return false;
d342894c 700}
701
702
703/* See if multicast group is already in use by other ID */
7c47cedf 704static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
d342894c 705{
553675fb 706 struct vxlan_dev *vxlan;
d342894c 707
553675fb 708 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
553675fb 709 if (!netif_running(vxlan->dev))
710 continue;
d342894c 711
7c47cedf 712 if (vxlan->default_dst.remote_ip == remote_ip)
553675fb 713 return true;
714 }
d342894c 715
716 return false;
717}
718
7c47cedf 719static void vxlan_sock_hold(struct vxlan_sock *vs)
d342894c 720{
7c47cedf
SH
721 atomic_inc(&vs->refcnt);
722}
d342894c 723
1c51a915 724static void vxlan_sock_release(struct vxlan_net *vn, struct vxlan_sock *vs)
7c47cedf
SH
725{
726 if (!atomic_dec_and_test(&vs->refcnt))
727 return;
d342894c 728
1c51a915 729 spin_lock(&vn->sock_lock);
7c47cedf 730 hlist_del_rcu(&vs->hlist);
1c51a915
SH
731 spin_unlock(&vn->sock_lock);
732
7c47cedf 733 queue_work(vxlan_wq, &vs->del_work);
d342894c 734}
735
7c47cedf
SH
736/* Callback to update multicast group membership.
737 * Scheduled when vxlan goes up/down.
738 */
739static void vxlan_igmp_work(struct work_struct *work)
d342894c 740{
7c47cedf
SH
741 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_work);
742 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
743 struct vxlan_sock *vs = vxlan->vn_sock;
744 struct sock *sk = vs->sock->sk;
d342894c 745 struct ip_mreqn mreq = {
c7995c43
AW
746 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
747 .imr_ifindex = vxlan->default_dst.remote_ifindex,
d342894c 748 };
749
d342894c 750 lock_sock(sk);
7c47cedf
SH
751 if (vxlan_group_used(vn, vxlan->default_dst.remote_ip))
752 ip_mc_join_group(sk, &mreq);
753 else
754 ip_mc_leave_group(sk, &mreq);
d342894c 755 release_sock(sk);
d342894c 756
1c51a915 757 vxlan_sock_release(vn, vs);
7c47cedf 758 dev_put(vxlan->dev);
d342894c 759}
760
761/* Callback from net/ipv4/udp.c to receive packets */
762static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
763{
764 struct iphdr *oip;
765 struct vxlanhdr *vxh;
766 struct vxlan_dev *vxlan;
e8171045 767 struct pcpu_tstats *stats;
553675fb 768 __be16 port;
d342894c 769 __u32 vni;
770 int err;
771
772 /* pop off outer UDP header */
773 __skb_pull(skb, sizeof(struct udphdr));
774
775 /* Need Vxlan and inner Ethernet header to be present */
776 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
777 goto error;
778
779 /* Drop packets with reserved bits set */
780 vxh = (struct vxlanhdr *) skb->data;
781 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
782 (vxh->vx_vni & htonl(0xff))) {
783 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
784 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
785 goto error;
786 }
787
788 __skb_pull(skb, sizeof(struct vxlanhdr));
d342894c 789
790 /* Is this VNI defined? */
791 vni = ntohl(vxh->vx_vni) >> 8;
553675fb 792 port = inet_sk(sk)->inet_sport;
793 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
d342894c 794 if (!vxlan) {
553675fb 795 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
796 vni, ntohs(port));
d342894c 797 goto drop;
798 }
799
800 if (!pskb_may_pull(skb, ETH_HLEN)) {
801 vxlan->dev->stats.rx_length_errors++;
802 vxlan->dev->stats.rx_errors++;
803 goto drop;
804 }
805
e4f67add
DS
806 skb_reset_mac_header(skb);
807
d342894c 808 /* Re-examine inner Ethernet packet */
809 oip = ip_hdr(skb);
810 skb->protocol = eth_type_trans(skb, vxlan->dev);
d342894c 811
812 /* Ignore packet loops (and multicast echo) */
813 if (compare_ether_addr(eth_hdr(skb)->h_source,
814 vxlan->dev->dev_addr) == 0)
815 goto drop;
816
26a41ae6 817 if ((vxlan->flags & VXLAN_F_LEARN) &&
818 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
819 goto drop;
d342894c 820
821 __skb_tunnel_rx(skb, vxlan->dev);
822 skb_reset_network_header(skb);
0afb1666
JG
823
824 /* If the NIC driver gave us an encapsulated packet with
825 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
826 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
827 * for us. Otherwise force the upper layers to verify it.
828 */
829 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
830 !(vxlan->dev->features & NETIF_F_RXCSUM))
831 skb->ip_summed = CHECKSUM_NONE;
832
833 skb->encapsulation = 0;
d342894c 834
835 err = IP_ECN_decapsulate(oip, skb);
836 if (unlikely(err)) {
837 if (log_ecn_error)
838 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
839 &oip->saddr, oip->tos);
840 if (err > 1) {
841 ++vxlan->dev->stats.rx_frame_errors;
842 ++vxlan->dev->stats.rx_errors;
843 goto drop;
844 }
845 }
846
e8171045 847 stats = this_cpu_ptr(vxlan->dev->tstats);
d342894c 848 u64_stats_update_begin(&stats->syncp);
849 stats->rx_packets++;
850 stats->rx_bytes += skb->len;
851 u64_stats_update_end(&stats->syncp);
852
853 netif_rx(skb);
854
855 return 0;
856error:
857 /* Put UDP header back */
858 __skb_push(skb, sizeof(struct udphdr));
859
860 return 1;
861drop:
862 /* Consume bad packet */
863 kfree_skb(skb);
864 return 0;
865}
866
e4f67add
DS
867static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
868{
869 struct vxlan_dev *vxlan = netdev_priv(dev);
870 struct arphdr *parp;
871 u8 *arpptr, *sha;
872 __be32 sip, tip;
873 struct neighbour *n;
874
875 if (dev->flags & IFF_NOARP)
876 goto out;
877
878 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
879 dev->stats.tx_dropped++;
880 goto out;
881 }
882 parp = arp_hdr(skb);
883
884 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
885 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
886 parp->ar_pro != htons(ETH_P_IP) ||
887 parp->ar_op != htons(ARPOP_REQUEST) ||
888 parp->ar_hln != dev->addr_len ||
889 parp->ar_pln != 4)
890 goto out;
891 arpptr = (u8 *)parp + sizeof(struct arphdr);
892 sha = arpptr;
893 arpptr += dev->addr_len; /* sha */
894 memcpy(&sip, arpptr, sizeof(sip));
895 arpptr += sizeof(sip);
896 arpptr += dev->addr_len; /* tha */
897 memcpy(&tip, arpptr, sizeof(tip));
898
899 if (ipv4_is_loopback(tip) ||
900 ipv4_is_multicast(tip))
901 goto out;
902
903 n = neigh_lookup(&arp_tbl, &tip, dev);
904
905 if (n) {
e4f67add
DS
906 struct vxlan_fdb *f;
907 struct sk_buff *reply;
908
909 if (!(n->nud_state & NUD_CONNECTED)) {
910 neigh_release(n);
911 goto out;
912 }
913
914 f = vxlan_find_mac(vxlan, n->ha);
3e61aa8f 915 if (f && first_remote(f)->remote_ip == htonl(INADDR_ANY)) {
e4f67add
DS
916 /* bridge-local neighbor */
917 neigh_release(n);
918 goto out;
919 }
920
921 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
922 n->ha, sha);
923
924 neigh_release(n);
925
926 skb_reset_mac_header(reply);
927 __skb_pull(reply, skb_network_offset(reply));
928 reply->ip_summed = CHECKSUM_UNNECESSARY;
929 reply->pkt_type = PACKET_HOST;
930
931 if (netif_rx_ni(reply) == NET_RX_DROP)
932 dev->stats.rx_dropped++;
933 } else if (vxlan->flags & VXLAN_F_L3MISS)
934 vxlan_ip_miss(dev, tip);
935out:
936 consume_skb(skb);
937 return NETDEV_TX_OK;
938}
939
940static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
941{
942 struct vxlan_dev *vxlan = netdev_priv(dev);
943 struct neighbour *n;
944 struct iphdr *pip;
945
946 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
947 return false;
948
949 n = NULL;
950 switch (ntohs(eth_hdr(skb)->h_proto)) {
951 case ETH_P_IP:
952 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
953 return false;
954 pip = ip_hdr(skb);
955 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
956 break;
957 default:
958 return false;
959 }
960
961 if (n) {
962 bool diff;
963
964 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
965 if (diff) {
966 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
967 dev->addr_len);
968 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
969 }
970 neigh_release(n);
971 return diff;
972 } else if (vxlan->flags & VXLAN_F_L3MISS)
973 vxlan_ip_miss(dev, pip->daddr);
974 return false;
975}
976
553675fb 977static void vxlan_sock_put(struct sk_buff *skb)
1cad8715 978{
979 sock_put(skb->sk);
980}
981
982/* On transmit, associate with the tunnel socket */
983static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
984{
553675fb 985 struct vxlan_dev *vxlan = netdev_priv(dev);
986 struct sock *sk = vxlan->vn_sock->sock->sk;
1cad8715 987
988 skb_orphan(skb);
989 sock_hold(sk);
990 skb->sk = sk;
553675fb 991 skb->destructor = vxlan_sock_put;
1cad8715 992}
993
05f47d69 994/* Compute source port for outgoing packet
995 * first choice to use L4 flow hash since it will spread
996 * better and maybe available from hardware
997 * secondary choice is to use jhash on the Ethernet header
998 */
7d836a76 999static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
05f47d69 1000{
1001 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
1002 u32 hash;
1003
1004 hash = skb_get_rxhash(skb);
1005 if (!hash)
1006 hash = jhash(skb->data, 2 * ETH_ALEN,
1007 (__force u32) skb->protocol);
1008
7d836a76 1009 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
05f47d69 1010}
1011
05c0db08
PS
1012static int handle_offloads(struct sk_buff *skb)
1013{
1014 if (skb_is_gso(skb)) {
1015 int err = skb_unclone(skb, GFP_ATOMIC);
1016 if (unlikely(err))
1017 return err;
1018
f6ace502 1019 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
05c0db08
PS
1020 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1021 skb->ip_summed = CHECKSUM_NONE;
1022
1023 return 0;
1024}
1025
9dcc71e1
SS
1026/* Bypass encapsulation if the destination is local */
1027static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1028 struct vxlan_dev *dst_vxlan)
1029{
1030 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1031 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
1032
1033 skb->pkt_type = PACKET_HOST;
1034 skb->encapsulation = 0;
1035 skb->dev = dst_vxlan->dev;
1036 __skb_pull(skb, skb_network_offset(skb));
1037
1038 if (dst_vxlan->flags & VXLAN_F_LEARN)
9d9f163c
MR
1039 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
1040 eth_hdr(skb)->h_source);
9dcc71e1
SS
1041
1042 u64_stats_update_begin(&tx_stats->syncp);
1043 tx_stats->tx_packets++;
1044 tx_stats->tx_bytes += skb->len;
1045 u64_stats_update_end(&tx_stats->syncp);
1046
1047 if (netif_rx(skb) == NET_RX_SUCCESS) {
1048 u64_stats_update_begin(&rx_stats->syncp);
1049 rx_stats->rx_packets++;
1050 rx_stats->rx_bytes += skb->len;
1051 u64_stats_update_end(&rx_stats->syncp);
1052 } else {
1053 skb->dev->stats.rx_dropped++;
1054 }
1055}
1056
4ad16930
SH
1057static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1058 struct vxlan_rdst *rdst, bool did_rsc)
d342894c 1059{
1060 struct vxlan_dev *vxlan = netdev_priv(dev);
1061 struct rtable *rt;
d342894c 1062 const struct iphdr *old_iph;
d342894c 1063 struct vxlanhdr *vxh;
1064 struct udphdr *uh;
1065 struct flowi4 fl4;
d342894c 1066 __be32 dst;
7d836a76 1067 __be16 src_port, dst_port;
234f5b73 1068 u32 vni;
d342894c 1069 __be16 df = 0;
1070 __u8 tos, ttl;
0e6fbc5b 1071 int err;
d342894c 1072
823aa873 1073 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
6681712d
DS
1074 vni = rdst->remote_vni;
1075 dst = rdst->remote_ip;
e4f67add
DS
1076
1077 if (!dst) {
1078 if (did_rsc) {
e4f67add 1079 /* short-circuited back to local bridge */
9dcc71e1 1080 vxlan_encap_bypass(skb, vxlan, vxlan);
4ad16930 1081 return;
e4f67add 1082 }
ef59febe 1083 goto drop;
e4f67add 1084 }
ef59febe 1085
d6727fe3
JG
1086 if (!skb->encapsulation) {
1087 skb_reset_inner_headers(skb);
1088 skb->encapsulation = 1;
1089 }
1090
d342894c 1091 /* Need space for new headers (invalidates iph ptr) */
1092 if (skb_cow_head(skb, VXLAN_HEADROOM))
1093 goto drop;
1094
d342894c 1095 old_iph = ip_hdr(skb);
1096
d342894c 1097 ttl = vxlan->ttl;
1098 if (!ttl && IN_MULTICAST(ntohl(dst)))
1099 ttl = 1;
1100
1101 tos = vxlan->tos;
1102 if (tos == 1)
206aaafc 1103 tos = ip_tunnel_get_dsfield(old_iph, skb);
d342894c 1104
05f47d69 1105 src_port = vxlan_src_port(vxlan, skb);
d342894c 1106
ca78f181 1107 memset(&fl4, 0, sizeof(fl4));
6681712d 1108 fl4.flowi4_oif = rdst->remote_ifindex;
ca78f181 1109 fl4.flowi4_tos = RT_TOS(tos);
1110 fl4.daddr = dst;
1111 fl4.saddr = vxlan->saddr;
1112
1113 rt = ip_route_output_key(dev_net(dev), &fl4);
d342894c 1114 if (IS_ERR(rt)) {
1115 netdev_dbg(dev, "no route to %pI4\n", &dst);
1116 dev->stats.tx_carrier_errors++;
1117 goto tx_error;
1118 }
1119
1120 if (rt->dst.dev == dev) {
1121 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1122 ip_rt_put(rt);
1123 dev->stats.collisions++;
1124 goto tx_error;
1125 }
1126
9dcc71e1 1127 /* Bypass encapsulation if the destination is local */
ab09a6d0
MR
1128 if (rt->rt_flags & RTCF_LOCAL &&
1129 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
9dcc71e1
SS
1130 struct vxlan_dev *dst_vxlan;
1131
1132 ip_rt_put(rt);
553675fb 1133 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
9dcc71e1
SS
1134 if (!dst_vxlan)
1135 goto tx_error;
1136 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
4ad16930 1137 return;
9dcc71e1 1138 }
d342894c 1139 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1140 vxh->vx_flags = htonl(VXLAN_FLAGS);
6681712d 1141 vxh->vx_vni = htonl(vni << 8);
d342894c 1142
1143 __skb_push(skb, sizeof(*uh));
1144 skb_reset_transport_header(skb);
1145 uh = udp_hdr(skb);
1146
73cf3317 1147 uh->dest = dst_port;
7d836a76 1148 uh->source = src_port;
d342894c 1149
1150 uh->len = htons(skb->len);
1151 uh->check = 0;
1152
1cad8715 1153 vxlan_set_owner(dev, skb);
1154
05c0db08
PS
1155 if (handle_offloads(skb))
1156 goto drop;
d342894c 1157
0e6fbc5b
PS
1158 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1159 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1160
1161 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1162 IPPROTO_UDP, tos, ttl, df);
1163 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1164
4ad16930 1165 return;
d342894c 1166
1167drop:
1168 dev->stats.tx_dropped++;
1169 goto tx_free;
1170
1171tx_error:
1172 dev->stats.tx_errors++;
1173tx_free:
1174 dev_kfree_skb(skb);
d342894c 1175}
1176
6681712d
DS
1177/* Transmit local packets over Vxlan
1178 *
1179 * Outer IP header inherits ECN and DF from inner header.
1180 * Outer UDP destination is the VXLAN assigned port.
1181 * source port is based on hash of flow
1182 */
1183static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1184{
1185 struct vxlan_dev *vxlan = netdev_priv(dev);
1186 struct ethhdr *eth;
1187 bool did_rsc = false;
afbd8bae 1188 struct vxlan_rdst *rdst;
6681712d 1189 struct vxlan_fdb *f;
6681712d
DS
1190
1191 skb_reset_mac_header(skb);
1192 eth = eth_hdr(skb);
1193
1194 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1195 return arp_reduce(dev, skb);
6681712d
DS
1196
1197 f = vxlan_find_mac(vxlan, eth->h_dest);
ae884082
DS
1198 did_rsc = false;
1199
1200 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1201 ntohs(eth->h_proto) == ETH_P_IP) {
1202 did_rsc = route_shortcircuit(dev, skb);
1203 if (did_rsc)
1204 f = vxlan_find_mac(vxlan, eth->h_dest);
1205 }
1206
6681712d 1207 if (f == NULL) {
afbd8bae
MR
1208 f = vxlan_find_mac(vxlan, all_zeros_mac);
1209 if (f == NULL) {
1210 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1211 !is_multicast_ether_addr(eth->h_dest))
1212 vxlan_fdb_miss(vxlan, eth->h_dest);
1213
1214 dev->stats.tx_dropped++;
1215 dev_kfree_skb(skb);
1216 return NETDEV_TX_OK;
1217 }
1218 }
6681712d 1219
afbd8bae
MR
1220 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1221 struct sk_buff *skb1;
6681712d 1222
afbd8bae
MR
1223 skb1 = skb_clone(skb, GFP_ATOMIC);
1224 if (skb1)
1225 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
6681712d
DS
1226 }
1227
afbd8bae 1228 dev_kfree_skb(skb);
4ad16930 1229 return NETDEV_TX_OK;
6681712d
DS
1230}
1231
d342894c 1232/* Walk the forwarding table and purge stale entries */
1233static void vxlan_cleanup(unsigned long arg)
1234{
1235 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1236 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1237 unsigned int h;
1238
1239 if (!netif_running(vxlan->dev))
1240 return;
1241
1242 spin_lock_bh(&vxlan->hash_lock);
1243 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1244 struct hlist_node *p, *n;
1245 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1246 struct vxlan_fdb *f
1247 = container_of(p, struct vxlan_fdb, hlist);
1248 unsigned long timeout;
1249
3c172868 1250 if (f->state & NUD_PERMANENT)
d342894c 1251 continue;
1252
1253 timeout = f->used + vxlan->age_interval * HZ;
1254 if (time_before_eq(timeout, jiffies)) {
1255 netdev_dbg(vxlan->dev,
1256 "garbage collect %pM\n",
1257 f->eth_addr);
1258 f->state = NUD_STALE;
1259 vxlan_fdb_destroy(vxlan, f);
1260 } else if (time_before(timeout, next_timer))
1261 next_timer = timeout;
1262 }
1263 }
1264 spin_unlock_bh(&vxlan->hash_lock);
1265
1266 mod_timer(&vxlan->age_timer, next_timer);
1267}
1268
1269/* Setup stats when device is created */
1270static int vxlan_init(struct net_device *dev)
1271{
1c51a915
SH
1272 struct vxlan_dev *vxlan = netdev_priv(dev);
1273 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1274 struct vxlan_sock *vs;
1275 __u32 vni = vxlan->default_dst.remote_vni;
1276
e8171045
PS
1277 dev->tstats = alloc_percpu(struct pcpu_tstats);
1278 if (!dev->tstats)
d342894c 1279 return -ENOMEM;
1280
1c51a915
SH
1281 spin_lock(&vn->sock_lock);
1282 vs = vxlan_find_port(dev_net(dev), vxlan->dst_port);
1283 if (vs) {
1284 /* If we have a socket with same port already, reuse it */
1285 atomic_inc(&vs->refcnt);
1286 vxlan->vn_sock = vs;
1287 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1288 } else {
1289 /* otherwise make new socket outside of RTNL */
1290 dev_hold(dev);
1291 queue_work(vxlan_wq, &vxlan->sock_work);
1292 }
1293 spin_unlock(&vn->sock_lock);
1294
d342894c 1295 return 0;
1296}
1297
afbd8bae
MR
1298static void vxlan_fdb_delete_defualt(struct vxlan_dev *vxlan)
1299{
1300 struct vxlan_fdb *f;
1301
1302 spin_lock_bh(&vxlan->hash_lock);
1303 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1304 if (f)
1305 vxlan_fdb_destroy(vxlan, f);
1306 spin_unlock_bh(&vxlan->hash_lock);
1307}
1308
ebf4063e
SH
1309static void vxlan_uninit(struct net_device *dev)
1310{
1311 struct vxlan_dev *vxlan = netdev_priv(dev);
1312 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1313 struct vxlan_sock *vs = vxlan->vn_sock;
1314
afbd8bae
MR
1315 vxlan_fdb_delete_defualt(vxlan);
1316
ebf4063e
SH
1317 if (vs)
1318 vxlan_sock_release(vn, vs);
1319 free_percpu(dev->tstats);
1320}
1321
d342894c 1322/* Start ageing timer and join group when device is brought up */
1323static int vxlan_open(struct net_device *dev)
1324{
1325 struct vxlan_dev *vxlan = netdev_priv(dev);
1c51a915
SH
1326 struct vxlan_sock *vs = vxlan->vn_sock;
1327
1328 /* socket hasn't been created */
1329 if (!vs)
1330 return -ENOTCONN;
d342894c 1331
c7995c43 1332 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1c51a915 1333 vxlan_sock_hold(vs);
7c47cedf
SH
1334 dev_hold(dev);
1335 queue_work(vxlan_wq, &vxlan->igmp_work);
d342894c 1336 }
1337
1338 if (vxlan->age_interval)
1339 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1340
1341 return 0;
1342}
1343
1344/* Purge the forwarding table */
1345static void vxlan_flush(struct vxlan_dev *vxlan)
1346{
31fec5aa 1347 unsigned int h;
d342894c 1348
1349 spin_lock_bh(&vxlan->hash_lock);
1350 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1351 struct hlist_node *p, *n;
1352 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1353 struct vxlan_fdb *f
1354 = container_of(p, struct vxlan_fdb, hlist);
afbd8bae
MR
1355 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1356 if (!is_zero_ether_addr(f->eth_addr))
1357 vxlan_fdb_destroy(vxlan, f);
d342894c 1358 }
1359 }
1360 spin_unlock_bh(&vxlan->hash_lock);
1361}
1362
1363/* Cleanup timer and forwarding table on shutdown */
1364static int vxlan_stop(struct net_device *dev)
1365{
1366 struct vxlan_dev *vxlan = netdev_priv(dev);
1c51a915 1367 struct vxlan_sock *vs = vxlan->vn_sock;
d342894c 1368
1c51a915
SH
1369 if (vs && IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1370 vxlan_sock_hold(vs);
7c47cedf
SH
1371 dev_hold(dev);
1372 queue_work(vxlan_wq, &vxlan->igmp_work);
1373 }
d342894c 1374
1375 del_timer_sync(&vxlan->age_timer);
1376
1377 vxlan_flush(vxlan);
1378
1379 return 0;
1380}
1381
d342894c 1382/* Stub, nothing needs to be done. */
1383static void vxlan_set_multicast_list(struct net_device *dev)
1384{
1385}
1386
1387static const struct net_device_ops vxlan_netdev_ops = {
1388 .ndo_init = vxlan_init,
ebf4063e 1389 .ndo_uninit = vxlan_uninit,
d342894c 1390 .ndo_open = vxlan_open,
1391 .ndo_stop = vxlan_stop,
1392 .ndo_start_xmit = vxlan_xmit,
e8171045 1393 .ndo_get_stats64 = ip_tunnel_get_stats64,
d342894c 1394 .ndo_set_rx_mode = vxlan_set_multicast_list,
1395 .ndo_change_mtu = eth_change_mtu,
1396 .ndo_validate_addr = eth_validate_addr,
1397 .ndo_set_mac_address = eth_mac_addr,
1398 .ndo_fdb_add = vxlan_fdb_add,
1399 .ndo_fdb_del = vxlan_fdb_delete,
1400 .ndo_fdb_dump = vxlan_fdb_dump,
1401};
1402
1403/* Info for udev, that this is a virtual tunnel endpoint */
1404static struct device_type vxlan_type = {
1405 .name = "vxlan",
1406};
1407
d342894c 1408/* Initialize the device structure. */
1409static void vxlan_setup(struct net_device *dev)
1410{
1411 struct vxlan_dev *vxlan = netdev_priv(dev);
31fec5aa 1412 unsigned int h;
05f47d69 1413 int low, high;
d342894c 1414
1415 eth_hw_addr_random(dev);
1416 ether_setup(dev);
2840bf22 1417 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
d342894c 1418
1419 dev->netdev_ops = &vxlan_netdev_ops;
ebf4063e 1420 dev->destructor = free_netdev;
d342894c 1421 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1422
1423 dev->tx_queue_len = 0;
1424 dev->features |= NETIF_F_LLTX;
1425 dev->features |= NETIF_F_NETNS_LOCAL;
d6727fe3 1426 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
0afb1666 1427 dev->features |= NETIF_F_RXCSUM;
05c0db08 1428 dev->features |= NETIF_F_GSO_SOFTWARE;
0afb1666
JG
1429
1430 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
05c0db08 1431 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
d342894c 1432 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
6602d007 1433 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
d342894c 1434
553675fb 1435 INIT_LIST_HEAD(&vxlan->next);
d342894c 1436 spin_lock_init(&vxlan->hash_lock);
7c47cedf 1437 INIT_WORK(&vxlan->igmp_work, vxlan_igmp_work);
1c51a915 1438 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
d342894c 1439
1440 init_timer_deferrable(&vxlan->age_timer);
1441 vxlan->age_timer.function = vxlan_cleanup;
1442 vxlan->age_timer.data = (unsigned long) vxlan;
1443
05f47d69 1444 inet_get_local_port_range(&low, &high);
1445 vxlan->port_min = low;
1446 vxlan->port_max = high;
823aa873 1447 vxlan->dst_port = htons(vxlan_port);
05f47d69 1448
d342894c 1449 vxlan->dev = dev;
1450
1451 for (h = 0; h < FDB_HASH_SIZE; ++h)
1452 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1453}
1454
1455static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1456 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
5d174dd8 1457 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
d342894c 1458 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1459 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1460 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1461 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1462 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1463 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1464 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
05f47d69 1465 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
e4f67add
DS
1466 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1467 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1468 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1469 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
823aa873 1470 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
d342894c 1471};
1472
1473static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1474{
1475 if (tb[IFLA_ADDRESS]) {
1476 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1477 pr_debug("invalid link address (not ethernet)\n");
1478 return -EINVAL;
1479 }
1480
1481 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1482 pr_debug("invalid all zero ethernet address\n");
1483 return -EADDRNOTAVAIL;
1484 }
1485 }
1486
1487 if (!data)
1488 return -EINVAL;
1489
1490 if (data[IFLA_VXLAN_ID]) {
1491 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1492 if (id >= VXLAN_VID_MASK)
1493 return -ERANGE;
1494 }
1495
05f47d69 1496 if (data[IFLA_VXLAN_PORT_RANGE]) {
1497 const struct ifla_vxlan_port_range *p
1498 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1499
1500 if (ntohs(p->high) < ntohs(p->low)) {
1501 pr_debug("port range %u .. %u not valid\n",
1502 ntohs(p->low), ntohs(p->high));
1503 return -EINVAL;
1504 }
1505 }
1506
d342894c 1507 return 0;
1508}
1509
1b13c97f
YB
1510static void vxlan_get_drvinfo(struct net_device *netdev,
1511 struct ethtool_drvinfo *drvinfo)
1512{
1513 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1514 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1515}
1516
1517static const struct ethtool_ops vxlan_ethtool_ops = {
1518 .get_drvinfo = vxlan_get_drvinfo,
1519 .get_link = ethtool_op_get_link,
1520};
1521
553675fb 1522static void vxlan_del_work(struct work_struct *work)
1523{
1524 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1525
1526 sk_release_kernel(vs->sock->sk);
1527 kfree_rcu(vs, rcu);
1528}
1529
553675fb 1530static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1531{
1532 struct vxlan_sock *vs;
1533 struct sock *sk;
1534 struct sockaddr_in vxlan_addr = {
1535 .sin_family = AF_INET,
1536 .sin_addr.s_addr = htonl(INADDR_ANY),
bb3fd687 1537 .sin_port = port,
553675fb 1538 };
1539 int rc;
31fec5aa 1540 unsigned int h;
553675fb 1541
1542 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1543 if (!vs)
1544 return ERR_PTR(-ENOMEM);
1545
1546 for (h = 0; h < VNI_HASH_SIZE; ++h)
1547 INIT_HLIST_HEAD(&vs->vni_list[h]);
1548
1549 INIT_WORK(&vs->del_work, vxlan_del_work);
1550
1551 /* Create UDP socket for encapsulation receive. */
1552 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1553 if (rc < 0) {
1554 pr_debug("UDP socket create failed\n");
1555 kfree(vs);
1556 return ERR_PTR(rc);
1557 }
1558
1559 /* Put in proper namespace */
1560 sk = vs->sock->sk;
1561 sk_change_net(sk, net);
1562
553675fb 1563 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1564 sizeof(vxlan_addr));
1565 if (rc < 0) {
1566 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1567 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1568 sk_release_kernel(sk);
1569 kfree(vs);
1570 return ERR_PTR(rc);
1571 }
1572
1573 /* Disable multicast loopback */
1574 inet_sk(sk)->mc_loop = 0;
1575
1576 /* Mark socket as an encapsulation socket. */
1577 udp_sk(sk)->encap_type = 1;
1578 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1579 udp_encap_enable();
7c47cedf 1580 atomic_set(&vs->refcnt, 1);
553675fb 1581
553675fb 1582 return vs;
1583}
1584
1c51a915
SH
1585/* Scheduled at device creation to bind to a socket */
1586static void vxlan_sock_work(struct work_struct *work)
1587{
1588 struct vxlan_dev *vxlan
1589 = container_of(work, struct vxlan_dev, sock_work);
1590 struct net_device *dev = vxlan->dev;
1591 struct net *net = dev_net(dev);
1592 __u32 vni = vxlan->default_dst.remote_vni;
1593 __be16 port = vxlan->dst_port;
1594 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
1595 struct vxlan_sock *nvs, *ovs;
1596
1597 nvs = vxlan_socket_create(net, port);
1598 if (IS_ERR(nvs)) {
1599 netdev_err(vxlan->dev, "Can not create UDP socket, %ld\n",
1600 PTR_ERR(nvs));
1601 goto out;
1602 }
1603
1604 spin_lock(&vn->sock_lock);
1605 /* Look again to see if can reuse socket */
1606 ovs = vxlan_find_port(net, port);
1607 if (ovs) {
1608 atomic_inc(&ovs->refcnt);
1609 vxlan->vn_sock = ovs;
1610 hlist_add_head_rcu(&vxlan->hlist, vni_head(ovs, vni));
1611 spin_unlock(&vn->sock_lock);
1612
1613 sk_release_kernel(nvs->sock->sk);
1614 kfree(nvs);
1615 } else {
1616 vxlan->vn_sock = nvs;
1617 hlist_add_head_rcu(&nvs->hlist, vs_head(net, port));
1618 hlist_add_head_rcu(&vxlan->hlist, vni_head(nvs, vni));
1619 spin_unlock(&vn->sock_lock);
1620 }
1621out:
1622 dev_put(dev);
1623}
1624
d342894c 1625static int vxlan_newlink(struct net *net, struct net_device *dev,
1626 struct nlattr *tb[], struct nlattr *data[])
1627{
553675fb 1628 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
d342894c 1629 struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 1630 struct vxlan_rdst *dst = &vxlan->default_dst;
d342894c 1631 __u32 vni;
1632 int err;
1633
1634 if (!data[IFLA_VXLAN_ID])
1635 return -EINVAL;
1636
1637 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
c7995c43 1638 dst->remote_vni = vni;
d342894c 1639
5d174dd8 1640 if (data[IFLA_VXLAN_GROUP])
1641 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
d342894c 1642
1643 if (data[IFLA_VXLAN_LOCAL])
1644 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1645
34e02aa1 1646 if (data[IFLA_VXLAN_LINK] &&
c7995c43 1647 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
34e02aa1 1648 struct net_device *lowerdev
c7995c43 1649 = __dev_get_by_index(net, dst->remote_ifindex);
34e02aa1 1650
1651 if (!lowerdev) {
c7995c43 1652 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
34e02aa1 1653 return -ENODEV;
1654 }
d342894c 1655
34e02aa1 1656 if (!tb[IFLA_MTU])
d342894c 1657 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
1ba56fb4
AD
1658
1659 /* update header length based on lower device */
1660 dev->hard_header_len = lowerdev->hard_header_len +
1661 VXLAN_HEADROOM;
d342894c 1662 }
1663
1664 if (data[IFLA_VXLAN_TOS])
1665 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1666
afb97186
VB
1667 if (data[IFLA_VXLAN_TTL])
1668 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1669
d342894c 1670 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
e4f67add 1671 vxlan->flags |= VXLAN_F_LEARN;
d342894c 1672
1673 if (data[IFLA_VXLAN_AGEING])
1674 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1675 else
1676 vxlan->age_interval = FDB_AGE_DEFAULT;
1677
e4f67add
DS
1678 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1679 vxlan->flags |= VXLAN_F_PROXY;
1680
1681 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1682 vxlan->flags |= VXLAN_F_RSC;
1683
1684 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1685 vxlan->flags |= VXLAN_F_L2MISS;
1686
1687 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1688 vxlan->flags |= VXLAN_F_L3MISS;
1689
d342894c 1690 if (data[IFLA_VXLAN_LIMIT])
1691 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1692
05f47d69 1693 if (data[IFLA_VXLAN_PORT_RANGE]) {
1694 const struct ifla_vxlan_port_range *p
1695 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1696 vxlan->port_min = ntohs(p->low);
1697 vxlan->port_max = ntohs(p->high);
1698 }
1699
823aa873 1700 if (data[IFLA_VXLAN_PORT])
1701 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1702
553675fb 1703 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1704 pr_info("duplicate VNI %u\n", vni);
1705 return -EEXIST;
1706 }
1707
1b13c97f
YB
1708 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1709
afbd8bae
MR
1710 /* create an fdb entry for default destination */
1711 err = vxlan_fdb_create(vxlan, all_zeros_mac,
1712 vxlan->default_dst.remote_ip,
1713 NUD_REACHABLE|NUD_PERMANENT,
1714 NLM_F_EXCL|NLM_F_CREATE,
1715 vxlan->dst_port, vxlan->default_dst.remote_vni,
1716 vxlan->default_dst.remote_ifindex, NTF_SELF);
1c51a915 1717 if (err)
553675fb 1718 return err;
d342894c 1719
afbd8bae
MR
1720 err = register_netdevice(dev);
1721 if (err) {
1722 vxlan_fdb_delete_defualt(vxlan);
1723 return err;
1724 }
1725
553675fb 1726 list_add(&vxlan->next, &vn->vxlan_list);
553675fb 1727
1728 return 0;
d342894c 1729}
1730
1731static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1732{
1733 struct vxlan_dev *vxlan = netdev_priv(dev);
1734
1735 hlist_del_rcu(&vxlan->hlist);
553675fb 1736 list_del(&vxlan->next);
d342894c 1737 unregister_netdevice_queue(dev, head);
1738}
1739
1740static size_t vxlan_get_size(const struct net_device *dev)
1741{
1742
1743 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
5d174dd8 1744 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
d342894c 1745 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1746 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1747 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1748 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1749 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
e4f67add
DS
1750 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1751 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1752 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1753 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
d342894c 1754 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1755 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
05f47d69 1756 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
823aa873 1757 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
d342894c 1758 0;
1759}
1760
1761static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1762{
1763 const struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 1764 const struct vxlan_rdst *dst = &vxlan->default_dst;
05f47d69 1765 struct ifla_vxlan_port_range ports = {
1766 .low = htons(vxlan->port_min),
1767 .high = htons(vxlan->port_max),
1768 };
d342894c 1769
c7995c43 1770 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
d342894c 1771 goto nla_put_failure;
1772
5d174dd8 1773 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
d342894c 1774 goto nla_put_failure;
1775
c7995c43 1776 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
d342894c 1777 goto nla_put_failure;
1778
7c41c42c 1779 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
d342894c 1780 goto nla_put_failure;
1781
1782 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1783 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
e4f67add
DS
1784 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1785 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1786 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1787 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1788 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1789 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1790 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1791 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1792 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
d342894c 1793 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
823aa873 1794 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1795 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
d342894c 1796 goto nla_put_failure;
1797
05f47d69 1798 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1799 goto nla_put_failure;
1800
d342894c 1801 return 0;
1802
1803nla_put_failure:
1804 return -EMSGSIZE;
1805}
1806
1807static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1808 .kind = "vxlan",
1809 .maxtype = IFLA_VXLAN_MAX,
1810 .policy = vxlan_policy,
1811 .priv_size = sizeof(struct vxlan_dev),
1812 .setup = vxlan_setup,
1813 .validate = vxlan_validate,
1814 .newlink = vxlan_newlink,
1815 .dellink = vxlan_dellink,
1816 .get_size = vxlan_get_size,
1817 .fill_info = vxlan_fill_info,
1818};
1819
1820static __net_init int vxlan_init_net(struct net *net)
1821{
1822 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
31fec5aa 1823 unsigned int h;
d342894c 1824
553675fb 1825 INIT_LIST_HEAD(&vn->vxlan_list);
1c51a915 1826 spin_lock_init(&vn->sock_lock);
d342894c 1827
553675fb 1828 for (h = 0; h < PORT_HASH_SIZE; ++h)
1829 INIT_HLIST_HEAD(&vn->sock_list[h]);
d342894c 1830
1831 return 0;
1832}
1833
1834static __net_exit void vxlan_exit_net(struct net *net)
1835{
1836 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
9cb6cb7e 1837 struct vxlan_dev *vxlan;
9cb6cb7e
ZM
1838
1839 rtnl_lock();
553675fb 1840 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1841 dev_close(vxlan->dev);
9cb6cb7e 1842 rtnl_unlock();
d342894c 1843}
1844
1845static struct pernet_operations vxlan_net_ops = {
1846 .init = vxlan_init_net,
1847 .exit = vxlan_exit_net,
1848 .id = &vxlan_net_id,
1849 .size = sizeof(struct vxlan_net),
1850};
1851
1852static int __init vxlan_init_module(void)
1853{
1854 int rc;
1855
758c57d1
SH
1856 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1857 if (!vxlan_wq)
1858 return -ENOMEM;
1859
d342894c 1860 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1861
1862 rc = register_pernet_device(&vxlan_net_ops);
1863 if (rc)
1864 goto out1;
1865
1866 rc = rtnl_link_register(&vxlan_link_ops);
1867 if (rc)
1868 goto out2;
1869
1870 return 0;
1871
1872out2:
1873 unregister_pernet_device(&vxlan_net_ops);
1874out1:
758c57d1 1875 destroy_workqueue(vxlan_wq);
d342894c 1876 return rc;
1877}
7332a13b 1878late_initcall(vxlan_init_module);
d342894c 1879
1880static void __exit vxlan_cleanup_module(void)
1881{
d342894c 1882 unregister_pernet_device(&vxlan_net_ops);
b7153984 1883 rtnl_link_unregister(&vxlan_link_ops);
758c57d1 1884 destroy_workqueue(vxlan_wq);
6681712d 1885 rcu_barrier();
d342894c 1886}
1887module_exit(vxlan_cleanup_module);
1888
1889MODULE_LICENSE("GPL");
1890MODULE_VERSION(VXLAN_VERSION);
3b8df3c6 1891MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
d342894c 1892MODULE_ALIAS_RTNL_LINK("vxlan");