vxlan: send notification when MAC migrates
[linux-2.6-block.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
71 * for compatability with early adopters.
72 */
d342894c 73static 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
d342894c 81static unsigned int vxlan_net_id;
553675fb 82
83/* per UDP socket information */
84struct vxlan_sock {
85 struct hlist_node hlist;
86 struct rcu_head rcu;
87 struct work_struct del_work;
7c47cedf 88 atomic_t refcnt;
553675fb 89 struct socket *sock;
d342894c 90 struct hlist_head vni_list[VNI_HASH_SIZE];
91};
92
553675fb 93/* 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
6681712d 99struct vxlan_rdst {
6681712d
DS
100 __be32 remote_ip;
101 __be16 remote_port;
102 u32 remote_vni;
103 u32 remote_ifindex;
104 struct vxlan_rdst *remote_next;
105};
106
d342894c 107/* 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;
6681712d 113 struct vxlan_rdst remote;
d342894c 114 u16 state; /* see ndm_state */
ae884082 115 u8 flags; /* see ndm_flags */
d342894c 116 u8 eth_addr[ETH_ALEN];
117};
118
d342894c 119/* Pseudo network device */
120struct vxlan_dev {
553675fb 121 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 */
d342894c 124 struct net_device *dev;
c7995c43 125 struct vxlan_rdst default_dst; /* default destination */
d342894c 126 __be32 saddr; /* source address */
823aa873 127 __be16 dst_port;
05f47d69 128 __u16 port_min; /* source port range */
129 __u16 port_max;
d342894c 130 __u8 tos; /* TOS override */
131 __u8 ttl;
e4f67add 132 u32 flags; /* VXLAN_F_* below */
d342894c 133
7c47cedf 134 struct work_struct igmp_work;
d342894c 135 unsigned long age_interval;
136 struct timer_list age_timer;
137 spinlock_t hash_lock;
138 unsigned int addrcnt;
139 unsigned int addrmax;
d342894c 140
141 struct hlist_head fdb_head[FDB_HASH_SIZE];
142};
143
e4f67add
DS
144#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
d342894c 150/* salt for hash table */
151static u32 vxlan_salt __read_mostly;
758c57d1 152static struct workqueue_struct *vxlan_wq;
d342894c 153
553675fb 154/* 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)
d342894c 162{
163 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
164
553675fb 165 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;
d342894c 178}
179
180/* Look up VNI in a per net namespace table */
553675fb 181static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
d342894c 182{
553675fb 183 struct vxlan_sock *vs;
d342894c 184 struct vxlan_dev *vxlan;
d342894c 185
553675fb 186 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) {
c7995c43 191 if (vxlan->default_dst.remote_vni == id)
d342894c 192 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,
6681712d
DS
201 u32 portid, u32 seq, int type, unsigned int flags,
202 const struct vxlan_rdst *rdst)
d342894c 203{
204 unsigned long now = jiffies;
205 struct nda_cacheinfo ci;
206 struct nlmsghdr *nlh;
207 struct ndmsg *ndm;
e4f67add 208 bool send_ip, send_eth;
d342894c 209
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));
e4f67add
DS
216
217 send_eth = send_ip = true;
218
219 if (type == RTM_GETNEIGH) {
220 ndm->ndm_family = AF_INET;
6681712d 221 send_ip = rdst->remote_ip != htonl(INADDR_ANY);
e4f67add
DS
222 send_eth = !is_zero_ether_addr(fdb->eth_addr);
223 } else
224 ndm->ndm_family = AF_BRIDGE;
d342894c 225 ndm->ndm_state = fdb->state;
226 ndm->ndm_ifindex = vxlan->dev->ifindex;
ae884082 227 ndm->ndm_flags = fdb->flags;
d342894c 228 ndm->ndm_type = NDA_DST;
229
e4f67add 230 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
d342894c 231 goto nla_put_failure;
232
6681712d
DS
233 if (send_ip && nla_put_be32(skb, NDA_DST, rdst->remote_ip))
234 goto nla_put_failure;
235
823aa873 236 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
6681712d
DS
237 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
238 goto nla_put_failure;
c7995c43 239 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
6681712d
DS
240 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))
d342894c 244 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 */
73cf3317 266 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
6681712d
DS
267 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
268 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
d342894c 269 + 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
6681712d 283 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, &fdb->remote);
d342894c 284 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
e4f67add
DS
298static 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;
6681712d
DS
305 f.remote.remote_ip = ipa; /* goes to NDA_DST */
306 f.remote.remote_vni = VXLAN_N_VID;
e4f67add
DS
307
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
d342894c 322/* 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
d342894c 329 value >>= 16;
321fb991 330#else
331 value <<= 16;
d342894c 332#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 */
014be2c8 344static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
d342894c 345 const u8 *mac)
346
347{
348 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
349 struct vxlan_fdb *f;
d342894c 350
b67bfe0d 351 hlist_for_each_entry_rcu(f, head, hlist) {
d342894c 352 if (compare_ether_addr(mac, f->eth_addr) == 0)
353 return f;
354 }
355
356 return NULL;
357}
358
014be2c8
SS
359static 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
6681712d
DS
371/* Add/update destinations for multicast */
372static int vxlan_fdb_append(struct vxlan_fdb *f,
73cf3317 373 __be32 ip, __be16 port, __u32 vni, __u32 ifindex)
6681712d
DS
374{
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
d342894c 398/* Add new entry to forwarding table -- assumes lock held */
399static int vxlan_fdb_create(struct vxlan_dev *vxlan,
400 const u8 *mac, __be32 ip,
6681712d 401 __u16 state, __u16 flags,
73cf3317 402 __be16 port, __u32 vni, __u32 ifindex,
ae884082 403 __u8 ndm_flags)
d342894c 404{
405 struct vxlan_fdb *f;
406 int notify = 0;
407
014be2c8 408 f = __vxlan_find_mac(vxlan, mac);
d342894c 409 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 }
ae884082
DS
420 if (f->flags != ndm_flags) {
421 f->flags = ndm_flags;
422 f->updated = jiffies;
423 notify = 1;
424 }
6681712d
DS
425 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 }
d342894c 433 } 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;
6681712d
DS
446 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;
d342894c 451 f->state = state;
ae884082 452 f->flags = ndm_flags;
d342894c 453 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
6706c82e 467static void vxlan_fdb_free(struct rcu_head *head)
6681712d
DS
468{
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
d342894c 480static 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);
6681712d 489 call_rcu(&f->rcu, vxlan_fdb_free);
d342894c 490}
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);
6681712d 498 struct net *net = dev_net(vxlan->dev);
d342894c 499 __be32 ip;
73cf3317 500 __be16 port;
501 u32 vni, ifindex;
d342894c 502 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
6681712d 518 if (tb[NDA_PORT]) {
73cf3317 519 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
6681712d 520 return -EINVAL;
73cf3317 521 port = nla_get_be16(tb[NDA_PORT]);
6681712d 522 } else
823aa873 523 port = vxlan->dst_port;
6681712d
DS
524
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
c7995c43 530 vni = vxlan->default_dst.remote_vni;
6681712d
DS
531
532 if (tb[NDA_IFINDEX]) {
5abb0029 533 struct net_device *tdev;
6681712d
DS
534
535 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
536 return -EINVAL;
537 ifindex = nla_get_u32(tb[NDA_IFINDEX]);
5abb0029
PS
538 tdev = dev_get_by_index(net, ifindex);
539 if (!tdev)
6681712d 540 return -EADDRNOTAVAIL;
5abb0029 541 dev_put(tdev);
6681712d
DS
542 } else
543 ifindex = 0;
544
d342894c 545 spin_lock_bh(&vxlan->hash_lock);
73cf3317 546 err = vxlan_fdb_create(vxlan, addr, ip, ndm->ndm_state, flags,
547 port, vni, ifindex, ndm->ndm_flags);
d342894c 548 spin_unlock_bh(&vxlan->hash_lock);
549
550 return err;
551}
552
553/* Delete entry (via netlink) */
1690be63
VY
554static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
555 struct net_device *dev,
d342894c 556 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;
d342894c 582 int err;
583
b67bfe0d 584 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
6681712d
DS
585 struct vxlan_rdst *rd;
586 for (rd = &f->remote; rd; rd = rd->remote_next) {
587 if (idx < cb->args[0])
588 goto skip;
589
590 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;
d342894c 597skip:
6681712d
DS
598 ++idx;
599 }
d342894c 600 }
601 }
602
603 return idx;
604}
605
606/* Watch incoming packets to learn mapping between Ethernet address
607 * and Tunnel endpoint.
26a41ae6 608 * Return true if packet is bogus and should be droppped.
d342894c 609 */
26a41ae6 610static bool vxlan_snoop(struct net_device *dev,
d342894c 611 __be32 src_ip, const u8 *src_mac)
612{
613 struct vxlan_dev *vxlan = netdev_priv(dev);
614 struct vxlan_fdb *f;
d342894c 615
616 f = vxlan_find_mac(vxlan, src_mac);
617 if (likely(f)) {
6681712d 618 if (likely(f->remote.remote_ip == src_ip))
26a41ae6 619 return false;
620
621 /* Don't migrate static entries, drop packets */
eb064c3b 622 if (f->state & NUD_NOARP)
26a41ae6 623 return true;
d342894c 624
625 if (net_ratelimit())
626 netdev_info(dev,
627 "%pM migrated from %pI4 to %pI4\n",
6681712d 628 src_mac, &f->remote.remote_ip, &src_ip);
d342894c 629
6681712d 630 f->remote.remote_ip = src_ip;
d342894c 631 f->updated = jiffies;
8385f50a 632 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
d342894c 633 } else {
634 /* learned new entry */
635 spin_lock(&vxlan->hash_lock);
3bf74b1a 636
637 /* close off race between vxlan_flush and incoming packets */
638 if (netif_running(dev))
639 vxlan_fdb_create(vxlan, src_mac, src_ip,
640 NUD_REACHABLE,
641 NLM_F_EXCL|NLM_F_CREATE,
642 vxlan->dst_port,
643 vxlan->default_dst.remote_vni,
644 0, NTF_SELF);
d342894c 645 spin_unlock(&vxlan->hash_lock);
646 }
26a41ae6 647
648 return false;
d342894c 649}
650
651
652/* See if multicast group is already in use by other ID */
7c47cedf 653static bool vxlan_group_used(struct vxlan_net *vn, __be32 remote_ip)
d342894c 654{
553675fb 655 struct vxlan_dev *vxlan;
d342894c 656
553675fb 657 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
553675fb 658 if (!netif_running(vxlan->dev))
659 continue;
d342894c 660
7c47cedf 661 if (vxlan->default_dst.remote_ip == remote_ip)
553675fb 662 return true;
663 }
d342894c 664
665 return false;
666}
667
7c47cedf 668static void vxlan_sock_hold(struct vxlan_sock *vs)
d342894c 669{
7c47cedf
SH
670 atomic_inc(&vs->refcnt);
671}
d342894c 672
7c47cedf
SH
673static void vxlan_sock_release(struct vxlan_sock *vs)
674{
675 if (!atomic_dec_and_test(&vs->refcnt))
676 return;
d342894c 677
7c47cedf
SH
678 hlist_del_rcu(&vs->hlist);
679 queue_work(vxlan_wq, &vs->del_work);
d342894c 680}
681
7c47cedf
SH
682/* Callback to update multicast group membership.
683 * Scheduled when vxlan goes up/down.
684 */
685static void vxlan_igmp_work(struct work_struct *work)
d342894c 686{
7c47cedf
SH
687 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_work);
688 struct vxlan_net *vn = net_generic(dev_net(vxlan->dev), vxlan_net_id);
689 struct vxlan_sock *vs = vxlan->vn_sock;
690 struct sock *sk = vs->sock->sk;
d342894c 691 struct ip_mreqn mreq = {
c7995c43
AW
692 .imr_multiaddr.s_addr = vxlan->default_dst.remote_ip,
693 .imr_ifindex = vxlan->default_dst.remote_ifindex,
d342894c 694 };
695
d342894c 696 lock_sock(sk);
7c47cedf
SH
697 if (vxlan_group_used(vn, vxlan->default_dst.remote_ip))
698 ip_mc_join_group(sk, &mreq);
699 else
700 ip_mc_leave_group(sk, &mreq);
d342894c 701 release_sock(sk);
d342894c 702
7c47cedf
SH
703 vxlan_sock_release(vs);
704 dev_put(vxlan->dev);
d342894c 705}
706
707/* Callback from net/ipv4/udp.c to receive packets */
708static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
709{
710 struct iphdr *oip;
711 struct vxlanhdr *vxh;
712 struct vxlan_dev *vxlan;
e8171045 713 struct pcpu_tstats *stats;
553675fb 714 __be16 port;
d342894c 715 __u32 vni;
716 int err;
717
718 /* pop off outer UDP header */
719 __skb_pull(skb, sizeof(struct udphdr));
720
721 /* Need Vxlan and inner Ethernet header to be present */
722 if (!pskb_may_pull(skb, sizeof(struct vxlanhdr)))
723 goto error;
724
725 /* Drop packets with reserved bits set */
726 vxh = (struct vxlanhdr *) skb->data;
727 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
728 (vxh->vx_vni & htonl(0xff))) {
729 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
730 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
731 goto error;
732 }
733
734 __skb_pull(skb, sizeof(struct vxlanhdr));
d342894c 735
736 /* Is this VNI defined? */
737 vni = ntohl(vxh->vx_vni) >> 8;
553675fb 738 port = inet_sk(sk)->inet_sport;
739 vxlan = vxlan_find_vni(sock_net(sk), vni, port);
d342894c 740 if (!vxlan) {
553675fb 741 netdev_dbg(skb->dev, "unknown vni %d port %u\n",
742 vni, ntohs(port));
d342894c 743 goto drop;
744 }
745
746 if (!pskb_may_pull(skb, ETH_HLEN)) {
747 vxlan->dev->stats.rx_length_errors++;
748 vxlan->dev->stats.rx_errors++;
749 goto drop;
750 }
751
e4f67add
DS
752 skb_reset_mac_header(skb);
753
d342894c 754 /* Re-examine inner Ethernet packet */
755 oip = ip_hdr(skb);
756 skb->protocol = eth_type_trans(skb, vxlan->dev);
d342894c 757
758 /* Ignore packet loops (and multicast echo) */
759 if (compare_ether_addr(eth_hdr(skb)->h_source,
760 vxlan->dev->dev_addr) == 0)
761 goto drop;
762
26a41ae6 763 if ((vxlan->flags & VXLAN_F_LEARN) &&
764 vxlan_snoop(skb->dev, oip->saddr, eth_hdr(skb)->h_source))
765 goto drop;
d342894c 766
767 __skb_tunnel_rx(skb, vxlan->dev);
768 skb_reset_network_header(skb);
0afb1666
JG
769
770 /* If the NIC driver gave us an encapsulated packet with
771 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
772 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
773 * for us. Otherwise force the upper layers to verify it.
774 */
775 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
776 !(vxlan->dev->features & NETIF_F_RXCSUM))
777 skb->ip_summed = CHECKSUM_NONE;
778
779 skb->encapsulation = 0;
d342894c 780
781 err = IP_ECN_decapsulate(oip, skb);
782 if (unlikely(err)) {
783 if (log_ecn_error)
784 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
785 &oip->saddr, oip->tos);
786 if (err > 1) {
787 ++vxlan->dev->stats.rx_frame_errors;
788 ++vxlan->dev->stats.rx_errors;
789 goto drop;
790 }
791 }
792
e8171045 793 stats = this_cpu_ptr(vxlan->dev->tstats);
d342894c 794 u64_stats_update_begin(&stats->syncp);
795 stats->rx_packets++;
796 stats->rx_bytes += skb->len;
797 u64_stats_update_end(&stats->syncp);
798
799 netif_rx(skb);
800
801 return 0;
802error:
803 /* Put UDP header back */
804 __skb_push(skb, sizeof(struct udphdr));
805
806 return 1;
807drop:
808 /* Consume bad packet */
809 kfree_skb(skb);
810 return 0;
811}
812
e4f67add
DS
813static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
814{
815 struct vxlan_dev *vxlan = netdev_priv(dev);
816 struct arphdr *parp;
817 u8 *arpptr, *sha;
818 __be32 sip, tip;
819 struct neighbour *n;
820
821 if (dev->flags & IFF_NOARP)
822 goto out;
823
824 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
825 dev->stats.tx_dropped++;
826 goto out;
827 }
828 parp = arp_hdr(skb);
829
830 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
831 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
832 parp->ar_pro != htons(ETH_P_IP) ||
833 parp->ar_op != htons(ARPOP_REQUEST) ||
834 parp->ar_hln != dev->addr_len ||
835 parp->ar_pln != 4)
836 goto out;
837 arpptr = (u8 *)parp + sizeof(struct arphdr);
838 sha = arpptr;
839 arpptr += dev->addr_len; /* sha */
840 memcpy(&sip, arpptr, sizeof(sip));
841 arpptr += sizeof(sip);
842 arpptr += dev->addr_len; /* tha */
843 memcpy(&tip, arpptr, sizeof(tip));
844
845 if (ipv4_is_loopback(tip) ||
846 ipv4_is_multicast(tip))
847 goto out;
848
849 n = neigh_lookup(&arp_tbl, &tip, dev);
850
851 if (n) {
e4f67add
DS
852 struct vxlan_fdb *f;
853 struct sk_buff *reply;
854
855 if (!(n->nud_state & NUD_CONNECTED)) {
856 neigh_release(n);
857 goto out;
858 }
859
860 f = vxlan_find_mac(vxlan, n->ha);
6681712d 861 if (f && f->remote.remote_ip == htonl(INADDR_ANY)) {
e4f67add
DS
862 /* bridge-local neighbor */
863 neigh_release(n);
864 goto out;
865 }
866
867 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
868 n->ha, sha);
869
870 neigh_release(n);
871
872 skb_reset_mac_header(reply);
873 __skb_pull(reply, skb_network_offset(reply));
874 reply->ip_summed = CHECKSUM_UNNECESSARY;
875 reply->pkt_type = PACKET_HOST;
876
877 if (netif_rx_ni(reply) == NET_RX_DROP)
878 dev->stats.rx_dropped++;
879 } else if (vxlan->flags & VXLAN_F_L3MISS)
880 vxlan_ip_miss(dev, tip);
881out:
882 consume_skb(skb);
883 return NETDEV_TX_OK;
884}
885
886static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
887{
888 struct vxlan_dev *vxlan = netdev_priv(dev);
889 struct neighbour *n;
890 struct iphdr *pip;
891
892 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
893 return false;
894
895 n = NULL;
896 switch (ntohs(eth_hdr(skb)->h_proto)) {
897 case ETH_P_IP:
898 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
899 return false;
900 pip = ip_hdr(skb);
901 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
902 break;
903 default:
904 return false;
905 }
906
907 if (n) {
908 bool diff;
909
910 diff = compare_ether_addr(eth_hdr(skb)->h_dest, n->ha) != 0;
911 if (diff) {
912 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
913 dev->addr_len);
914 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
915 }
916 neigh_release(n);
917 return diff;
918 } else if (vxlan->flags & VXLAN_F_L3MISS)
919 vxlan_ip_miss(dev, pip->daddr);
920 return false;
921}
922
553675fb 923static void vxlan_sock_put(struct sk_buff *skb)
1cad8715 924{
925 sock_put(skb->sk);
926}
927
928/* On transmit, associate with the tunnel socket */
929static void vxlan_set_owner(struct net_device *dev, struct sk_buff *skb)
930{
553675fb 931 struct vxlan_dev *vxlan = netdev_priv(dev);
932 struct sock *sk = vxlan->vn_sock->sock->sk;
1cad8715 933
934 skb_orphan(skb);
935 sock_hold(sk);
936 skb->sk = sk;
553675fb 937 skb->destructor = vxlan_sock_put;
1cad8715 938}
939
05f47d69 940/* Compute source port for outgoing packet
941 * first choice to use L4 flow hash since it will spread
942 * better and maybe available from hardware
943 * secondary choice is to use jhash on the Ethernet header
944 */
7d836a76 945static __be16 vxlan_src_port(const struct vxlan_dev *vxlan, struct sk_buff *skb)
05f47d69 946{
947 unsigned int range = (vxlan->port_max - vxlan->port_min) + 1;
948 u32 hash;
949
950 hash = skb_get_rxhash(skb);
951 if (!hash)
952 hash = jhash(skb->data, 2 * ETH_ALEN,
953 (__force u32) skb->protocol);
954
7d836a76 955 return htons((((u64) hash * range) >> 32) + vxlan->port_min);
05f47d69 956}
957
05c0db08
PS
958static int handle_offloads(struct sk_buff *skb)
959{
960 if (skb_is_gso(skb)) {
961 int err = skb_unclone(skb, GFP_ATOMIC);
962 if (unlikely(err))
963 return err;
964
f6ace502 965 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
05c0db08
PS
966 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
967 skb->ip_summed = CHECKSUM_NONE;
968
969 return 0;
970}
971
9dcc71e1
SS
972/* Bypass encapsulation if the destination is local */
973static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
974 struct vxlan_dev *dst_vxlan)
975{
976 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
977 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
978
979 skb->pkt_type = PACKET_HOST;
980 skb->encapsulation = 0;
981 skb->dev = dst_vxlan->dev;
982 __skb_pull(skb, skb_network_offset(skb));
983
984 if (dst_vxlan->flags & VXLAN_F_LEARN)
9d9f163c
MR
985 vxlan_snoop(skb->dev, htonl(INADDR_LOOPBACK),
986 eth_hdr(skb)->h_source);
9dcc71e1
SS
987
988 u64_stats_update_begin(&tx_stats->syncp);
989 tx_stats->tx_packets++;
990 tx_stats->tx_bytes += skb->len;
991 u64_stats_update_end(&tx_stats->syncp);
992
993 if (netif_rx(skb) == NET_RX_SUCCESS) {
994 u64_stats_update_begin(&rx_stats->syncp);
995 rx_stats->rx_packets++;
996 rx_stats->rx_bytes += skb->len;
997 u64_stats_update_end(&rx_stats->syncp);
998 } else {
999 skb->dev->stats.rx_dropped++;
1000 }
1001}
1002
6681712d
DS
1003static netdev_tx_t vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1004 struct vxlan_rdst *rdst, bool did_rsc)
d342894c 1005{
1006 struct vxlan_dev *vxlan = netdev_priv(dev);
1007 struct rtable *rt;
d342894c 1008 const struct iphdr *old_iph;
d342894c 1009 struct vxlanhdr *vxh;
1010 struct udphdr *uh;
1011 struct flowi4 fl4;
d342894c 1012 __be32 dst;
7d836a76 1013 __be16 src_port, dst_port;
6681712d 1014 u32 vni;
d342894c 1015 __be16 df = 0;
1016 __u8 tos, ttl;
0e6fbc5b 1017 int err;
d342894c 1018
823aa873 1019 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
6681712d
DS
1020 vni = rdst->remote_vni;
1021 dst = rdst->remote_ip;
e4f67add
DS
1022
1023 if (!dst) {
1024 if (did_rsc) {
e4f67add 1025 /* short-circuited back to local bridge */
9dcc71e1 1026 vxlan_encap_bypass(skb, vxlan, vxlan);
e4f67add
DS
1027 return NETDEV_TX_OK;
1028 }
ef59febe 1029 goto drop;
e4f67add 1030 }
ef59febe 1031
d6727fe3
JG
1032 if (!skb->encapsulation) {
1033 skb_reset_inner_headers(skb);
1034 skb->encapsulation = 1;
1035 }
1036
d342894c 1037 /* Need space for new headers (invalidates iph ptr) */
1038 if (skb_cow_head(skb, VXLAN_HEADROOM))
1039 goto drop;
1040
d342894c 1041 old_iph = ip_hdr(skb);
1042
d342894c 1043 ttl = vxlan->ttl;
1044 if (!ttl && IN_MULTICAST(ntohl(dst)))
1045 ttl = 1;
1046
1047 tos = vxlan->tos;
1048 if (tos == 1)
206aaafc 1049 tos = ip_tunnel_get_dsfield(old_iph, skb);
d342894c 1050
05f47d69 1051 src_port = vxlan_src_port(vxlan, skb);
d342894c 1052
ca78f181 1053 memset(&fl4, 0, sizeof(fl4));
6681712d 1054 fl4.flowi4_oif = rdst->remote_ifindex;
ca78f181 1055 fl4.flowi4_tos = RT_TOS(tos);
1056 fl4.daddr = dst;
1057 fl4.saddr = vxlan->saddr;
1058
1059 rt = ip_route_output_key(dev_net(dev), &fl4);
d342894c 1060 if (IS_ERR(rt)) {
1061 netdev_dbg(dev, "no route to %pI4\n", &dst);
1062 dev->stats.tx_carrier_errors++;
1063 goto tx_error;
1064 }
1065
1066 if (rt->dst.dev == dev) {
1067 netdev_dbg(dev, "circular route to %pI4\n", &dst);
1068 ip_rt_put(rt);
1069 dev->stats.collisions++;
1070 goto tx_error;
1071 }
1072
9dcc71e1 1073 /* Bypass encapsulation if the destination is local */
ab09a6d0
MR
1074 if (rt->rt_flags & RTCF_LOCAL &&
1075 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
9dcc71e1
SS
1076 struct vxlan_dev *dst_vxlan;
1077
1078 ip_rt_put(rt);
553675fb 1079 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
9dcc71e1
SS
1080 if (!dst_vxlan)
1081 goto tx_error;
1082 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1083 return NETDEV_TX_OK;
1084 }
d342894c 1085 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1086 vxh->vx_flags = htonl(VXLAN_FLAGS);
6681712d 1087 vxh->vx_vni = htonl(vni << 8);
d342894c 1088
1089 __skb_push(skb, sizeof(*uh));
1090 skb_reset_transport_header(skb);
1091 uh = udp_hdr(skb);
1092
73cf3317 1093 uh->dest = dst_port;
7d836a76 1094 uh->source = src_port;
d342894c 1095
1096 uh->len = htons(skb->len);
1097 uh->check = 0;
1098
1cad8715 1099 vxlan_set_owner(dev, skb);
1100
05c0db08
PS
1101 if (handle_offloads(skb))
1102 goto drop;
d342894c 1103
0e6fbc5b
PS
1104 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1105 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
1106
1107 err = iptunnel_xmit(dev_net(dev), rt, skb, fl4.saddr, dst,
1108 IPPROTO_UDP, tos, ttl, df);
1109 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1110
d342894c 1111 return NETDEV_TX_OK;
1112
1113drop:
1114 dev->stats.tx_dropped++;
1115 goto tx_free;
1116
1117tx_error:
1118 dev->stats.tx_errors++;
1119tx_free:
1120 dev_kfree_skb(skb);
1121 return NETDEV_TX_OK;
1122}
1123
6681712d
DS
1124/* Transmit local packets over Vxlan
1125 *
1126 * Outer IP header inherits ECN and DF from inner header.
1127 * Outer UDP destination is the VXLAN assigned port.
1128 * source port is based on hash of flow
1129 */
1130static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1131{
1132 struct vxlan_dev *vxlan = netdev_priv(dev);
1133 struct ethhdr *eth;
1134 bool did_rsc = false;
c7995c43 1135 struct vxlan_rdst *rdst0, *rdst;
6681712d
DS
1136 struct vxlan_fdb *f;
1137 int rc1, rc;
1138
1139 skb_reset_mac_header(skb);
1140 eth = eth_hdr(skb);
1141
1142 if ((vxlan->flags & VXLAN_F_PROXY) && ntohs(eth->h_proto) == ETH_P_ARP)
1143 return arp_reduce(dev, skb);
6681712d
DS
1144
1145 f = vxlan_find_mac(vxlan, eth->h_dest);
ae884082
DS
1146 did_rsc = false;
1147
1148 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
1149 ntohs(eth->h_proto) == ETH_P_IP) {
1150 did_rsc = route_shortcircuit(dev, skb);
1151 if (did_rsc)
1152 f = vxlan_find_mac(vxlan, eth->h_dest);
1153 }
1154
6681712d 1155 if (f == NULL) {
c7995c43
AW
1156 rdst0 = &vxlan->default_dst;
1157
1158 if (rdst0->remote_ip == htonl(INADDR_ANY) &&
6681712d
DS
1159 (vxlan->flags & VXLAN_F_L2MISS) &&
1160 !is_multicast_ether_addr(eth->h_dest))
1161 vxlan_fdb_miss(vxlan, eth->h_dest);
1162 } else
1163 rdst0 = &f->remote;
1164
1165 rc = NETDEV_TX_OK;
1166
1167 /* if there are multiple destinations, send copies */
1168 for (rdst = rdst0->remote_next; rdst; rdst = rdst->remote_next) {
1169 struct sk_buff *skb1;
1170
1171 skb1 = skb_clone(skb, GFP_ATOMIC);
7aa27238 1172 if (skb1) {
1173 rc1 = vxlan_xmit_one(skb1, dev, rdst, did_rsc);
1174 if (rc == NETDEV_TX_OK)
1175 rc = rc1;
1176 }
6681712d
DS
1177 }
1178
1179 rc1 = vxlan_xmit_one(skb, dev, rdst0, did_rsc);
1180 if (rc == NETDEV_TX_OK)
1181 rc = rc1;
1182 return rc;
1183}
1184
d342894c 1185/* Walk the forwarding table and purge stale entries */
1186static void vxlan_cleanup(unsigned long arg)
1187{
1188 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1189 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1190 unsigned int h;
1191
1192 if (!netif_running(vxlan->dev))
1193 return;
1194
1195 spin_lock_bh(&vxlan->hash_lock);
1196 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1197 struct hlist_node *p, *n;
1198 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1199 struct vxlan_fdb *f
1200 = container_of(p, struct vxlan_fdb, hlist);
1201 unsigned long timeout;
1202
3c172868 1203 if (f->state & NUD_PERMANENT)
d342894c 1204 continue;
1205
1206 timeout = f->used + vxlan->age_interval * HZ;
1207 if (time_before_eq(timeout, jiffies)) {
1208 netdev_dbg(vxlan->dev,
1209 "garbage collect %pM\n",
1210 f->eth_addr);
1211 f->state = NUD_STALE;
1212 vxlan_fdb_destroy(vxlan, f);
1213 } else if (time_before(timeout, next_timer))
1214 next_timer = timeout;
1215 }
1216 }
1217 spin_unlock_bh(&vxlan->hash_lock);
1218
1219 mod_timer(&vxlan->age_timer, next_timer);
1220}
1221
1222/* Setup stats when device is created */
1223static int vxlan_init(struct net_device *dev)
1224{
e8171045
PS
1225 dev->tstats = alloc_percpu(struct pcpu_tstats);
1226 if (!dev->tstats)
d342894c 1227 return -ENOMEM;
1228
1229 return 0;
1230}
1231
1232/* Start ageing timer and join group when device is brought up */
1233static int vxlan_open(struct net_device *dev)
1234{
1235 struct vxlan_dev *vxlan = netdev_priv(dev);
d342894c 1236
c7995c43 1237 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
7c47cedf
SH
1238 vxlan_sock_hold(vxlan->vn_sock);
1239 dev_hold(dev);
1240 queue_work(vxlan_wq, &vxlan->igmp_work);
d342894c 1241 }
1242
1243 if (vxlan->age_interval)
1244 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1245
1246 return 0;
1247}
1248
1249/* Purge the forwarding table */
1250static void vxlan_flush(struct vxlan_dev *vxlan)
1251{
31fec5aa 1252 unsigned int h;
d342894c 1253
1254 spin_lock_bh(&vxlan->hash_lock);
1255 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1256 struct hlist_node *p, *n;
1257 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1258 struct vxlan_fdb *f
1259 = container_of(p, struct vxlan_fdb, hlist);
1260 vxlan_fdb_destroy(vxlan, f);
1261 }
1262 }
1263 spin_unlock_bh(&vxlan->hash_lock);
1264}
1265
1266/* Cleanup timer and forwarding table on shutdown */
1267static int vxlan_stop(struct net_device *dev)
1268{
1269 struct vxlan_dev *vxlan = netdev_priv(dev);
1270
7c47cedf
SH
1271 if (IN_MULTICAST(ntohl(vxlan->default_dst.remote_ip))) {
1272 vxlan_sock_hold(vxlan->vn_sock);
1273 dev_hold(dev);
1274 queue_work(vxlan_wq, &vxlan->igmp_work);
1275 }
d342894c 1276
1277 del_timer_sync(&vxlan->age_timer);
1278
1279 vxlan_flush(vxlan);
1280
1281 return 0;
1282}
1283
d342894c 1284/* Stub, nothing needs to be done. */
1285static void vxlan_set_multicast_list(struct net_device *dev)
1286{
1287}
1288
1289static const struct net_device_ops vxlan_netdev_ops = {
1290 .ndo_init = vxlan_init,
1291 .ndo_open = vxlan_open,
1292 .ndo_stop = vxlan_stop,
1293 .ndo_start_xmit = vxlan_xmit,
e8171045 1294 .ndo_get_stats64 = ip_tunnel_get_stats64,
d342894c 1295 .ndo_set_rx_mode = vxlan_set_multicast_list,
1296 .ndo_change_mtu = eth_change_mtu,
1297 .ndo_validate_addr = eth_validate_addr,
1298 .ndo_set_mac_address = eth_mac_addr,
1299 .ndo_fdb_add = vxlan_fdb_add,
1300 .ndo_fdb_del = vxlan_fdb_delete,
1301 .ndo_fdb_dump = vxlan_fdb_dump,
1302};
1303
1304/* Info for udev, that this is a virtual tunnel endpoint */
1305static struct device_type vxlan_type = {
1306 .name = "vxlan",
1307};
1308
1309static void vxlan_free(struct net_device *dev)
1310{
e8171045 1311 free_percpu(dev->tstats);
d342894c 1312 free_netdev(dev);
1313}
1314
1315/* Initialize the device structure. */
1316static void vxlan_setup(struct net_device *dev)
1317{
1318 struct vxlan_dev *vxlan = netdev_priv(dev);
31fec5aa 1319 unsigned int h;
05f47d69 1320 int low, high;
d342894c 1321
1322 eth_hw_addr_random(dev);
1323 ether_setup(dev);
2840bf22 1324 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
d342894c 1325
1326 dev->netdev_ops = &vxlan_netdev_ops;
1327 dev->destructor = vxlan_free;
1328 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
1329
1330 dev->tx_queue_len = 0;
1331 dev->features |= NETIF_F_LLTX;
1332 dev->features |= NETIF_F_NETNS_LOCAL;
d6727fe3 1333 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
0afb1666 1334 dev->features |= NETIF_F_RXCSUM;
05c0db08 1335 dev->features |= NETIF_F_GSO_SOFTWARE;
0afb1666
JG
1336
1337 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
05c0db08 1338 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
d342894c 1339 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
6602d007 1340 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
d342894c 1341
553675fb 1342 INIT_LIST_HEAD(&vxlan->next);
d342894c 1343 spin_lock_init(&vxlan->hash_lock);
7c47cedf 1344 INIT_WORK(&vxlan->igmp_work, vxlan_igmp_work);
d342894c 1345
1346 init_timer_deferrable(&vxlan->age_timer);
1347 vxlan->age_timer.function = vxlan_cleanup;
1348 vxlan->age_timer.data = (unsigned long) vxlan;
1349
05f47d69 1350 inet_get_local_port_range(&low, &high);
1351 vxlan->port_min = low;
1352 vxlan->port_max = high;
823aa873 1353 vxlan->dst_port = htons(vxlan_port);
05f47d69 1354
d342894c 1355 vxlan->dev = dev;
1356
1357 for (h = 0; h < FDB_HASH_SIZE; ++h)
1358 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
1359}
1360
1361static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
1362 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
5d174dd8 1363 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
d342894c 1364 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
1365 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
1366 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
1367 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
1368 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
1369 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
1370 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
05f47d69 1371 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
e4f67add
DS
1372 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
1373 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
1374 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
1375 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
823aa873 1376 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
d342894c 1377};
1378
1379static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
1380{
1381 if (tb[IFLA_ADDRESS]) {
1382 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1383 pr_debug("invalid link address (not ethernet)\n");
1384 return -EINVAL;
1385 }
1386
1387 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1388 pr_debug("invalid all zero ethernet address\n");
1389 return -EADDRNOTAVAIL;
1390 }
1391 }
1392
1393 if (!data)
1394 return -EINVAL;
1395
1396 if (data[IFLA_VXLAN_ID]) {
1397 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
1398 if (id >= VXLAN_VID_MASK)
1399 return -ERANGE;
1400 }
1401
05f47d69 1402 if (data[IFLA_VXLAN_PORT_RANGE]) {
1403 const struct ifla_vxlan_port_range *p
1404 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1405
1406 if (ntohs(p->high) < ntohs(p->low)) {
1407 pr_debug("port range %u .. %u not valid\n",
1408 ntohs(p->low), ntohs(p->high));
1409 return -EINVAL;
1410 }
1411 }
1412
d342894c 1413 return 0;
1414}
1415
1b13c97f
YB
1416static void vxlan_get_drvinfo(struct net_device *netdev,
1417 struct ethtool_drvinfo *drvinfo)
1418{
1419 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
1420 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
1421}
1422
1423static const struct ethtool_ops vxlan_ethtool_ops = {
1424 .get_drvinfo = vxlan_get_drvinfo,
1425 .get_link = ethtool_op_get_link,
1426};
1427
553675fb 1428static void vxlan_del_work(struct work_struct *work)
1429{
1430 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
1431
1432 sk_release_kernel(vs->sock->sk);
1433 kfree_rcu(vs, rcu);
1434}
1435
1436/* Create new listen socket if needed */
1437static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port)
1438{
1439 struct vxlan_sock *vs;
1440 struct sock *sk;
1441 struct sockaddr_in vxlan_addr = {
1442 .sin_family = AF_INET,
1443 .sin_addr.s_addr = htonl(INADDR_ANY),
1444 };
1445 int rc;
31fec5aa 1446 unsigned int h;
553675fb 1447
1448 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
1449 if (!vs)
1450 return ERR_PTR(-ENOMEM);
1451
1452 for (h = 0; h < VNI_HASH_SIZE; ++h)
1453 INIT_HLIST_HEAD(&vs->vni_list[h]);
1454
1455 INIT_WORK(&vs->del_work, vxlan_del_work);
1456
1457 /* Create UDP socket for encapsulation receive. */
1458 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &vs->sock);
1459 if (rc < 0) {
1460 pr_debug("UDP socket create failed\n");
1461 kfree(vs);
1462 return ERR_PTR(rc);
1463 }
1464
1465 /* Put in proper namespace */
1466 sk = vs->sock->sk;
1467 sk_change_net(sk, net);
1468
1469 vxlan_addr.sin_port = port;
1470
1471 rc = kernel_bind(vs->sock, (struct sockaddr *) &vxlan_addr,
1472 sizeof(vxlan_addr));
1473 if (rc < 0) {
1474 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
1475 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
1476 sk_release_kernel(sk);
1477 kfree(vs);
1478 return ERR_PTR(rc);
1479 }
1480
1481 /* Disable multicast loopback */
1482 inet_sk(sk)->mc_loop = 0;
1483
1484 /* Mark socket as an encapsulation socket. */
1485 udp_sk(sk)->encap_type = 1;
1486 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
1487 udp_encap_enable();
7c47cedf 1488 atomic_set(&vs->refcnt, 1);
553675fb 1489
553675fb 1490 return vs;
1491}
1492
d342894c 1493static int vxlan_newlink(struct net *net, struct net_device *dev,
1494 struct nlattr *tb[], struct nlattr *data[])
1495{
553675fb 1496 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
d342894c 1497 struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 1498 struct vxlan_rdst *dst = &vxlan->default_dst;
553675fb 1499 struct vxlan_sock *vs;
d342894c 1500 __u32 vni;
1501 int err;
1502
1503 if (!data[IFLA_VXLAN_ID])
1504 return -EINVAL;
1505
1506 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
c7995c43 1507 dst->remote_vni = vni;
d342894c 1508
5d174dd8 1509 if (data[IFLA_VXLAN_GROUP])
1510 dst->remote_ip = nla_get_be32(data[IFLA_VXLAN_GROUP]);
d342894c 1511
1512 if (data[IFLA_VXLAN_LOCAL])
1513 vxlan->saddr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
1514
34e02aa1 1515 if (data[IFLA_VXLAN_LINK] &&
c7995c43 1516 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
34e02aa1 1517 struct net_device *lowerdev
c7995c43 1518 = __dev_get_by_index(net, dst->remote_ifindex);
34e02aa1 1519
1520 if (!lowerdev) {
c7995c43 1521 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
34e02aa1 1522 return -ENODEV;
1523 }
d342894c 1524
34e02aa1 1525 if (!tb[IFLA_MTU])
d342894c 1526 dev->mtu = lowerdev->mtu - VXLAN_HEADROOM;
1ba56fb4
AD
1527
1528 /* update header length based on lower device */
1529 dev->hard_header_len = lowerdev->hard_header_len +
1530 VXLAN_HEADROOM;
d342894c 1531 }
1532
1533 if (data[IFLA_VXLAN_TOS])
1534 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
1535
afb97186
VB
1536 if (data[IFLA_VXLAN_TTL])
1537 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
1538
d342894c 1539 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
e4f67add 1540 vxlan->flags |= VXLAN_F_LEARN;
d342894c 1541
1542 if (data[IFLA_VXLAN_AGEING])
1543 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
1544 else
1545 vxlan->age_interval = FDB_AGE_DEFAULT;
1546
e4f67add
DS
1547 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
1548 vxlan->flags |= VXLAN_F_PROXY;
1549
1550 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
1551 vxlan->flags |= VXLAN_F_RSC;
1552
1553 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
1554 vxlan->flags |= VXLAN_F_L2MISS;
1555
1556 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
1557 vxlan->flags |= VXLAN_F_L3MISS;
1558
d342894c 1559 if (data[IFLA_VXLAN_LIMIT])
1560 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
1561
05f47d69 1562 if (data[IFLA_VXLAN_PORT_RANGE]) {
1563 const struct ifla_vxlan_port_range *p
1564 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
1565 vxlan->port_min = ntohs(p->low);
1566 vxlan->port_max = ntohs(p->high);
1567 }
1568
823aa873 1569 if (data[IFLA_VXLAN_PORT])
1570 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
1571
553675fb 1572 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
1573 pr_info("duplicate VNI %u\n", vni);
1574 return -EEXIST;
1575 }
1576
1577 vs = vxlan_find_port(net, vxlan->dst_port);
1578 if (vs)
7c47cedf 1579 atomic_inc(&vs->refcnt);
553675fb 1580 else {
1581 /* Drop lock because socket create acquires RTNL lock */
1582 rtnl_unlock();
1583 vs = vxlan_socket_create(net, vxlan->dst_port);
1584 rtnl_lock();
1585 if (IS_ERR(vs))
1586 return PTR_ERR(vs);
1587
1588 hlist_add_head_rcu(&vs->hlist, vs_head(net, vxlan->dst_port));
1589 }
1590 vxlan->vn_sock = vs;
1591
1b13c97f
YB
1592 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
1593
d342894c 1594 err = register_netdevice(dev);
553675fb 1595 if (err) {
7c47cedf 1596 vxlan_sock_release(vs);
553675fb 1597 return err;
1598 }
d342894c 1599
553675fb 1600 list_add(&vxlan->next, &vn->vxlan_list);
1601 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1602
1603 return 0;
d342894c 1604}
1605
1606static void vxlan_dellink(struct net_device *dev, struct list_head *head)
1607{
1608 struct vxlan_dev *vxlan = netdev_priv(dev);
553675fb 1609 struct vxlan_sock *vs = vxlan->vn_sock;
d342894c 1610
1611 hlist_del_rcu(&vxlan->hlist);
553675fb 1612 list_del(&vxlan->next);
d342894c 1613 unregister_netdevice_queue(dev, head);
7c47cedf 1614 vxlan_sock_release(vs);
d342894c 1615}
1616
1617static size_t vxlan_get_size(const struct net_device *dev)
1618{
1619
1620 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
5d174dd8 1621 nla_total_size(sizeof(__be32)) +/* IFLA_VXLAN_GROUP */
d342894c 1622 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
1623 nla_total_size(sizeof(__be32))+ /* IFLA_VXLAN_LOCAL */
1624 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
1625 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
1626 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
e4f67add
DS
1627 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
1628 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
1629 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
1630 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
d342894c 1631 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
1632 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
05f47d69 1633 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
823aa873 1634 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
d342894c 1635 0;
1636}
1637
1638static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
1639{
1640 const struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 1641 const struct vxlan_rdst *dst = &vxlan->default_dst;
05f47d69 1642 struct ifla_vxlan_port_range ports = {
1643 .low = htons(vxlan->port_min),
1644 .high = htons(vxlan->port_max),
1645 };
d342894c 1646
c7995c43 1647 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
d342894c 1648 goto nla_put_failure;
1649
5d174dd8 1650 if (dst->remote_ip && nla_put_be32(skb, IFLA_VXLAN_GROUP, dst->remote_ip))
d342894c 1651 goto nla_put_failure;
1652
c7995c43 1653 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
d342894c 1654 goto nla_put_failure;
1655
7c41c42c 1656 if (vxlan->saddr && nla_put_be32(skb, IFLA_VXLAN_LOCAL, vxlan->saddr))
d342894c 1657 goto nla_put_failure;
1658
1659 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
1660 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
e4f67add
DS
1661 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
1662 !!(vxlan->flags & VXLAN_F_LEARN)) ||
1663 nla_put_u8(skb, IFLA_VXLAN_PROXY,
1664 !!(vxlan->flags & VXLAN_F_PROXY)) ||
1665 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
1666 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
1667 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
1668 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
1669 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
d342894c 1670 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
823aa873 1671 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
1672 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
d342894c 1673 goto nla_put_failure;
1674
05f47d69 1675 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
1676 goto nla_put_failure;
1677
d342894c 1678 return 0;
1679
1680nla_put_failure:
1681 return -EMSGSIZE;
1682}
1683
1684static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
1685 .kind = "vxlan",
1686 .maxtype = IFLA_VXLAN_MAX,
1687 .policy = vxlan_policy,
1688 .priv_size = sizeof(struct vxlan_dev),
1689 .setup = vxlan_setup,
1690 .validate = vxlan_validate,
1691 .newlink = vxlan_newlink,
1692 .dellink = vxlan_dellink,
1693 .get_size = vxlan_get_size,
1694 .fill_info = vxlan_fill_info,
1695};
1696
1697static __net_init int vxlan_init_net(struct net *net)
1698{
1699 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
31fec5aa 1700 unsigned int h;
d342894c 1701
553675fb 1702 INIT_LIST_HEAD(&vn->vxlan_list);
d342894c 1703
553675fb 1704 for (h = 0; h < PORT_HASH_SIZE; ++h)
1705 INIT_HLIST_HEAD(&vn->sock_list[h]);
d342894c 1706
1707 return 0;
1708}
1709
1710static __net_exit void vxlan_exit_net(struct net *net)
1711{
1712 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
9cb6cb7e 1713 struct vxlan_dev *vxlan;
9cb6cb7e
ZM
1714
1715 rtnl_lock();
553675fb 1716 list_for_each_entry(vxlan, &vn->vxlan_list, next)
1717 dev_close(vxlan->dev);
9cb6cb7e 1718 rtnl_unlock();
d342894c 1719}
1720
1721static struct pernet_operations vxlan_net_ops = {
1722 .init = vxlan_init_net,
1723 .exit = vxlan_exit_net,
1724 .id = &vxlan_net_id,
1725 .size = sizeof(struct vxlan_net),
1726};
1727
1728static int __init vxlan_init_module(void)
1729{
1730 int rc;
1731
758c57d1
SH
1732 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
1733 if (!vxlan_wq)
1734 return -ENOMEM;
1735
d342894c 1736 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
1737
1738 rc = register_pernet_device(&vxlan_net_ops);
1739 if (rc)
1740 goto out1;
1741
1742 rc = rtnl_link_register(&vxlan_link_ops);
1743 if (rc)
1744 goto out2;
1745
1746 return 0;
1747
1748out2:
1749 unregister_pernet_device(&vxlan_net_ops);
1750out1:
758c57d1 1751 destroy_workqueue(vxlan_wq);
d342894c 1752 return rc;
1753}
7332a13b 1754late_initcall(vxlan_init_module);
d342894c 1755
1756static void __exit vxlan_cleanup_module(void)
1757{
d342894c 1758 unregister_pernet_device(&vxlan_net_ops);
b7153984 1759 rtnl_link_unregister(&vxlan_link_ops);
758c57d1 1760 destroy_workqueue(vxlan_wq);
6681712d 1761 rcu_barrier();
d342894c 1762}
1763module_exit(vxlan_cleanup_module);
1764
1765MODULE_LICENSE("GPL");
1766MODULE_VERSION(VXLAN_VERSION);
3b8df3c6 1767MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
d342894c 1768MODULE_ALIAS_RTNL_LINK("vxlan");