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