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