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