net: codel: Avoid undefined behavior from signed overflow
[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.
d342894c 9 */
10
11#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13#include <linux/kernel.h>
14#include <linux/types.h>
15#include <linux/module.h>
16#include <linux/errno.h>
17#include <linux/slab.h>
18#include <linux/skbuff.h>
19#include <linux/rculist.h>
20#include <linux/netdevice.h>
21#include <linux/in.h>
22#include <linux/ip.h>
23#include <linux/udp.h>
24#include <linux/igmp.h>
25#include <linux/etherdevice.h>
26#include <linux/if_ether.h>
1eaa8178 27#include <linux/if_vlan.h>
d342894c 28#include <linux/hash.h>
1b13c97f 29#include <linux/ethtool.h>
e4f67add
DS
30#include <net/arp.h>
31#include <net/ndisc.h>
d342894c 32#include <net/ip.h>
c5441932 33#include <net/ip_tunnels.h>
d342894c 34#include <net/icmp.h>
35#include <net/udp.h>
36#include <net/rtnetlink.h>
37#include <net/route.h>
38#include <net/dsfield.h>
39#include <net/inet_ecn.h>
40#include <net/net_namespace.h>
41#include <net/netns/generic.h>
012a5729 42#include <net/vxlan.h>
e4c7ed41
CW
43#if IS_ENABLED(CONFIG_IPV6)
44#include <net/ipv6.h>
45#include <net/addrconf.h>
46#include <net/ip6_tunnel.h>
660d98ca 47#include <net/ip6_checksum.h>
e4c7ed41 48#endif
d342894c 49
50#define VXLAN_VERSION "0.1"
51
553675fb 52#define PORT_HASH_BITS 8
53#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
d342894c 54#define VNI_HASH_BITS 10
55#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
56#define FDB_HASH_BITS 8
57#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
58#define FDB_AGE_DEFAULT 300 /* 5 min */
59#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
60
61#define VXLAN_N_VID (1u << 24)
62#define VXLAN_VID_MASK (VXLAN_N_VID - 1)
7ce04758 63#define VXLAN_HLEN (sizeof(struct udphdr) + sizeof(struct vxlanhdr))
d342894c 64
65#define VXLAN_FLAGS 0x08000000 /* struct vxlanhdr.vx_flags required value. */
66
67/* VXLAN protocol header */
68struct vxlanhdr {
69 __be32 vx_flags;
70 __be32 vx_vni;
71};
72
23c578bf 73/* UDP port for VXLAN traffic.
74 * The IANA assigned port is 4789, but the Linux default is 8472
234f5b73 75 * for compatibility with early adopters.
23c578bf 76 */
9daaa397
SH
77static unsigned short vxlan_port __read_mostly = 8472;
78module_param_named(udp_port, vxlan_port, ushort, 0444);
d342894c 79MODULE_PARM_DESC(udp_port, "Destination UDP port");
80
81static bool log_ecn_error = true;
82module_param(log_ecn_error, bool, 0644);
83MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
84
60d9d4c6 85static int vxlan_net_id;
553675fb 86
afbd8bae
MR
87static const u8 all_zeros_mac[ETH_ALEN];
88
553675fb 89/* per-network namespace private data for this module */
90struct vxlan_net {
91 struct list_head vxlan_list;
92 struct hlist_head sock_list[PORT_HASH_SIZE];
1c51a915 93 spinlock_t sock_lock;
553675fb 94};
95
e4c7ed41
CW
96union vxlan_addr {
97 struct sockaddr_in sin;
98 struct sockaddr_in6 sin6;
99 struct sockaddr sa;
100};
101
6681712d 102struct vxlan_rdst {
e4c7ed41 103 union vxlan_addr remote_ip;
6681712d
DS
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 */
e4c7ed41 130 union vxlan_addr 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
e4c7ed41 156#define VXLAN_F_IPV6 0x20 /* internal flag */
e4f67add 157
d342894c 158/* salt for hash table */
159static u32 vxlan_salt __read_mostly;
758c57d1 160static struct workqueue_struct *vxlan_wq;
d342894c 161
1c51a915
SH
162static void vxlan_sock_work(struct work_struct *work);
163
e4c7ed41
CW
164#if IS_ENABLED(CONFIG_IPV6)
165static inline
166bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
167{
168 if (a->sa.sa_family != b->sa.sa_family)
169 return false;
170 if (a->sa.sa_family == AF_INET6)
171 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
172 else
173 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
174}
175
176static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
177{
178 if (ipa->sa.sa_family == AF_INET6)
179 return ipv6_addr_any(&ipa->sin6.sin6_addr);
180 else
181 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
182}
183
184static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
185{
186 if (ipa->sa.sa_family == AF_INET6)
187 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
188 else
189 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
190}
191
192static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
193{
194 if (nla_len(nla) >= sizeof(struct in6_addr)) {
195 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
196 ip->sa.sa_family = AF_INET6;
197 return 0;
198 } else if (nla_len(nla) >= sizeof(__be32)) {
199 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
200 ip->sa.sa_family = AF_INET;
201 return 0;
202 } else {
203 return -EAFNOSUPPORT;
204 }
205}
206
207static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
208 const union vxlan_addr *ip)
209{
210 if (ip->sa.sa_family == AF_INET6)
211 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
212 else
213 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
214}
215
216#else /* !CONFIG_IPV6 */
217
218static inline
219bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
220{
221 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
222}
223
224static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
225{
226 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
227}
228
229static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
230{
231 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
232}
233
234static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
235{
236 if (nla_len(nla) >= sizeof(struct in6_addr)) {
237 return -EAFNOSUPPORT;
238 } else if (nla_len(nla) >= sizeof(__be32)) {
239 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
240 ip->sa.sa_family = AF_INET;
241 return 0;
242 } else {
243 return -EAFNOSUPPORT;
244 }
245}
246
247static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
248 const union vxlan_addr *ip)
249{
250 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
251}
252#endif
253
553675fb 254/* Virtual Network hash table head */
255static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
256{
257 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
258}
259
260/* Socket hash table head */
261static inline struct hlist_head *vs_head(struct net *net, __be16 port)
d342894c 262{
263 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
264
553675fb 265 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
266}
267
3e61aa8f
SH
268/* First remote destination for a forwarding entry.
269 * Guaranteed to be non-NULL because remotes are never deleted.
270 */
5ca5461c 271static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
3e61aa8f 272{
5ca5461c 273 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
274}
275
276static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
277{
278 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
3e61aa8f
SH
279}
280
553675fb 281/* Find VXLAN socket based on network namespace and UDP port */
9c2e24e1 282static struct vxlan_sock *vxlan_find_sock(struct net *net, __be16 port)
553675fb 283{
284 struct vxlan_sock *vs;
285
286 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
287 if (inet_sk(vs->sock->sk)->inet_sport == port)
288 return vs;
289 }
290 return NULL;
d342894c 291}
292
5cfccc5a 293static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
d342894c 294{
295 struct vxlan_dev *vxlan;
d342894c 296
553675fb 297 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
c7995c43 298 if (vxlan->default_dst.remote_vni == id)
d342894c 299 return vxlan;
300 }
301
302 return NULL;
303}
304
5cfccc5a
PS
305/* Look up VNI in a per net namespace table */
306static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id, __be16 port)
307{
308 struct vxlan_sock *vs;
309
310 vs = vxlan_find_sock(net, port);
311 if (!vs)
312 return NULL;
313
314 return vxlan_vs_find_vni(vs, id);
315}
316
d342894c 317/* Fill in neighbour message in skbuff. */
318static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
234f5b73
SH
319 const struct vxlan_fdb *fdb,
320 u32 portid, u32 seq, int type, unsigned int flags,
321 const struct vxlan_rdst *rdst)
d342894c 322{
323 unsigned long now = jiffies;
324 struct nda_cacheinfo ci;
325 struct nlmsghdr *nlh;
326 struct ndmsg *ndm;
e4f67add 327 bool send_ip, send_eth;
d342894c 328
329 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
330 if (nlh == NULL)
331 return -EMSGSIZE;
332
333 ndm = nlmsg_data(nlh);
334 memset(ndm, 0, sizeof(*ndm));
e4f67add
DS
335
336 send_eth = send_ip = true;
337
338 if (type == RTM_GETNEIGH) {
339 ndm->ndm_family = AF_INET;
e4c7ed41 340 send_ip = !vxlan_addr_any(&rdst->remote_ip);
e4f67add
DS
341 send_eth = !is_zero_ether_addr(fdb->eth_addr);
342 } else
343 ndm->ndm_family = AF_BRIDGE;
d342894c 344 ndm->ndm_state = fdb->state;
345 ndm->ndm_ifindex = vxlan->dev->ifindex;
ae884082 346 ndm->ndm_flags = fdb->flags;
d342894c 347 ndm->ndm_type = NDA_DST;
348
e4f67add 349 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
d342894c 350 goto nla_put_failure;
351
e4c7ed41 352 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
6681712d
DS
353 goto nla_put_failure;
354
823aa873 355 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
6681712d
DS
356 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
357 goto nla_put_failure;
c7995c43 358 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
60d9d4c6 359 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
6681712d
DS
360 goto nla_put_failure;
361 if (rdst->remote_ifindex &&
362 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
d342894c 363 goto nla_put_failure;
364
365 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
366 ci.ndm_confirmed = 0;
367 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
368 ci.ndm_refcnt = 0;
369
370 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
371 goto nla_put_failure;
372
373 return nlmsg_end(skb, nlh);
374
375nla_put_failure:
376 nlmsg_cancel(skb, nlh);
377 return -EMSGSIZE;
378}
379
380static inline size_t vxlan_nlmsg_size(void)
381{
382 return NLMSG_ALIGN(sizeof(struct ndmsg))
383 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
e4c7ed41 384 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
73cf3317 385 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
6681712d
DS
386 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
387 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
d342894c 388 + nla_total_size(sizeof(struct nda_cacheinfo));
389}
390
391static void vxlan_fdb_notify(struct vxlan_dev *vxlan,
3e61aa8f 392 struct vxlan_fdb *fdb, int type)
d342894c 393{
394 struct net *net = dev_net(vxlan->dev);
395 struct sk_buff *skb;
396 int err = -ENOBUFS;
397
398 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
399 if (skb == NULL)
400 goto errout;
401
5ca5461c 402 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0,
403 first_remote_rtnl(fdb));
d342894c 404 if (err < 0) {
405 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
406 WARN_ON(err == -EMSGSIZE);
407 kfree_skb(skb);
408 goto errout;
409 }
410
411 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
412 return;
413errout:
414 if (err < 0)
415 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
416}
417
e4c7ed41 418static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
e4f67add
DS
419{
420 struct vxlan_dev *vxlan = netdev_priv(dev);
bb3fd687
SH
421 struct vxlan_fdb f = {
422 .state = NUD_STALE,
423 };
424 struct vxlan_rdst remote = {
e4c7ed41 425 .remote_ip = *ipa, /* goes to NDA_DST */
bb3fd687
SH
426 .remote_vni = VXLAN_N_VID,
427 };
3e61aa8f
SH
428
429 INIT_LIST_HEAD(&f.remotes);
430 list_add_rcu(&remote.list, &f.remotes);
e4f67add
DS
431
432 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
433}
434
435static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
436{
bb3fd687
SH
437 struct vxlan_fdb f = {
438 .state = NUD_STALE,
439 };
e4f67add 440
3e61aa8f 441 INIT_LIST_HEAD(&f.remotes);
e4f67add
DS
442 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
443
444 vxlan_fdb_notify(vxlan, &f, RTM_GETNEIGH);
445}
446
d342894c 447/* Hash Ethernet address */
448static u32 eth_hash(const unsigned char *addr)
449{
450 u64 value = get_unaligned((u64 *)addr);
451
452 /* only want 6 bytes */
453#ifdef __BIG_ENDIAN
d342894c 454 value >>= 16;
321fb991 455#else
456 value <<= 16;
d342894c 457#endif
458 return hash_64(value, FDB_HASH_BITS);
459}
460
461/* Hash chain to use given mac address */
462static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
463 const u8 *mac)
464{
465 return &vxlan->fdb_head[eth_hash(mac)];
466}
467
468/* Look up Ethernet address in forwarding table */
014be2c8 469static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
d342894c 470 const u8 *mac)
471
472{
473 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
474 struct vxlan_fdb *f;
d342894c 475
b67bfe0d 476 hlist_for_each_entry_rcu(f, head, hlist) {
7367d0b5 477 if (ether_addr_equal(mac, f->eth_addr))
d342894c 478 return f;
479 }
480
481 return NULL;
482}
483
014be2c8
SS
484static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
485 const u8 *mac)
486{
487 struct vxlan_fdb *f;
488
489 f = __vxlan_find_mac(vxlan, mac);
490 if (f)
491 f->used = jiffies;
492
493 return f;
494}
495
a5e7c10a
MR
496/* caller should hold vxlan->hash_lock */
497static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
e4c7ed41 498 union vxlan_addr *ip, __be16 port,
a5e7c10a 499 __u32 vni, __u32 ifindex)
6681712d 500{
3e61aa8f 501 struct vxlan_rdst *rd;
6681712d 502
3e61aa8f 503 list_for_each_entry(rd, &f->remotes, list) {
e4c7ed41 504 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
6681712d
DS
505 rd->remote_port == port &&
506 rd->remote_vni == vni &&
507 rd->remote_ifindex == ifindex)
a5e7c10a 508 return rd;
6681712d 509 }
3e61aa8f 510
a5e7c10a
MR
511 return NULL;
512}
513
906dc186
TR
514/* Replace destination of unicast mac */
515static int vxlan_fdb_replace(struct vxlan_fdb *f,
e4c7ed41 516 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
906dc186
TR
517{
518 struct vxlan_rdst *rd;
519
520 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
521 if (rd)
522 return 0;
523
524 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
525 if (!rd)
526 return 0;
e4c7ed41 527 rd->remote_ip = *ip;
906dc186
TR
528 rd->remote_port = port;
529 rd->remote_vni = vni;
530 rd->remote_ifindex = ifindex;
531 return 1;
532}
533
a5e7c10a
MR
534/* Add/update destinations for multicast */
535static int vxlan_fdb_append(struct vxlan_fdb *f,
e4c7ed41 536 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
a5e7c10a
MR
537{
538 struct vxlan_rdst *rd;
539
540 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
541 if (rd)
542 return 0;
543
6681712d
DS
544 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
545 if (rd == NULL)
546 return -ENOBUFS;
e4c7ed41 547 rd->remote_ip = *ip;
6681712d
DS
548 rd->remote_port = port;
549 rd->remote_vni = vni;
550 rd->remote_ifindex = ifindex;
3e61aa8f
SH
551
552 list_add_tail_rcu(&rd->list, &f->remotes);
553
6681712d
DS
554 return 1;
555}
556
53cf5275
JG
557/* Notify netdevs that UDP port started listening */
558static void vxlan_notify_add_rx_port(struct sock *sk)
559{
560 struct net_device *dev;
561 struct net *net = sock_net(sk);
562 sa_family_t sa_family = sk->sk_family;
35e42379 563 __be16 port = inet_sk(sk)->inet_sport;
53cf5275
JG
564
565 rcu_read_lock();
566 for_each_netdev_rcu(net, dev) {
567 if (dev->netdev_ops->ndo_add_vxlan_port)
568 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
569 port);
570 }
571 rcu_read_unlock();
572}
573
574/* Notify netdevs that UDP port is no more listening */
575static void vxlan_notify_del_rx_port(struct sock *sk)
576{
577 struct net_device *dev;
578 struct net *net = sock_net(sk);
579 sa_family_t sa_family = sk->sk_family;
35e42379 580 __be16 port = inet_sk(sk)->inet_sport;
53cf5275
JG
581
582 rcu_read_lock();
583 for_each_netdev_rcu(net, dev) {
584 if (dev->netdev_ops->ndo_del_vxlan_port)
585 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
586 port);
587 }
588 rcu_read_unlock();
589}
590
d342894c 591/* Add new entry to forwarding table -- assumes lock held */
592static int vxlan_fdb_create(struct vxlan_dev *vxlan,
e4c7ed41 593 const u8 *mac, union vxlan_addr *ip,
6681712d 594 __u16 state, __u16 flags,
73cf3317 595 __be16 port, __u32 vni, __u32 ifindex,
ae884082 596 __u8 ndm_flags)
d342894c 597{
598 struct vxlan_fdb *f;
599 int notify = 0;
600
014be2c8 601 f = __vxlan_find_mac(vxlan, mac);
d342894c 602 if (f) {
603 if (flags & NLM_F_EXCL) {
604 netdev_dbg(vxlan->dev,
605 "lost race to create %pM\n", mac);
606 return -EEXIST;
607 }
608 if (f->state != state) {
609 f->state = state;
610 f->updated = jiffies;
611 notify = 1;
612 }
ae884082
DS
613 if (f->flags != ndm_flags) {
614 f->flags = ndm_flags;
615 f->updated = jiffies;
616 notify = 1;
617 }
906dc186
TR
618 if ((flags & NLM_F_REPLACE)) {
619 /* Only change unicasts */
620 if (!(is_multicast_ether_addr(f->eth_addr) ||
621 is_zero_ether_addr(f->eth_addr))) {
622 int rc = vxlan_fdb_replace(f, ip, port, vni,
623 ifindex);
624
625 if (rc < 0)
626 return rc;
627 notify |= rc;
628 } else
629 return -EOPNOTSUPP;
630 }
6681712d 631 if ((flags & NLM_F_APPEND) &&
58e4c767
MR
632 (is_multicast_ether_addr(f->eth_addr) ||
633 is_zero_ether_addr(f->eth_addr))) {
6681712d
DS
634 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex);
635
636 if (rc < 0)
637 return rc;
638 notify |= rc;
639 }
d342894c 640 } else {
641 if (!(flags & NLM_F_CREATE))
642 return -ENOENT;
643
644 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
645 return -ENOSPC;
646
906dc186
TR
647 /* Disallow replace to add a multicast entry */
648 if ((flags & NLM_F_REPLACE) &&
649 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
650 return -EOPNOTSUPP;
651
e4c7ed41 652 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
d342894c 653 f = kmalloc(sizeof(*f), GFP_ATOMIC);
654 if (!f)
655 return -ENOMEM;
656
657 notify = 1;
d342894c 658 f->state = state;
ae884082 659 f->flags = ndm_flags;
d342894c 660 f->updated = f->used = jiffies;
3e61aa8f 661 INIT_LIST_HEAD(&f->remotes);
d342894c 662 memcpy(f->eth_addr, mac, ETH_ALEN);
663
3e61aa8f
SH
664 vxlan_fdb_append(f, ip, port, vni, ifindex);
665
d342894c 666 ++vxlan->addrcnt;
667 hlist_add_head_rcu(&f->hlist,
668 vxlan_fdb_head(vxlan, mac));
669 }
670
671 if (notify)
672 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
673
674 return 0;
675}
676
6706c82e 677static void vxlan_fdb_free(struct rcu_head *head)
6681712d
DS
678{
679 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
3e61aa8f 680 struct vxlan_rdst *rd, *nd;
6681712d 681
3e61aa8f 682 list_for_each_entry_safe(rd, nd, &f->remotes, list)
6681712d 683 kfree(rd);
6681712d
DS
684 kfree(f);
685}
686
d342894c 687static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
688{
689 netdev_dbg(vxlan->dev,
690 "delete %pM\n", f->eth_addr);
691
692 --vxlan->addrcnt;
693 vxlan_fdb_notify(vxlan, f, RTM_DELNEIGH);
694
695 hlist_del_rcu(&f->hlist);
6681712d 696 call_rcu(&f->rcu, vxlan_fdb_free);
d342894c 697}
698
f0b074be 699static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
e4c7ed41 700 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
d342894c 701{
6681712d 702 struct net *net = dev_net(vxlan->dev);
e4c7ed41 703 int err;
d342894c 704
f0b074be 705 if (tb[NDA_DST]) {
e4c7ed41
CW
706 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
707 if (err)
708 return err;
f0b074be 709 } else {
e4c7ed41
CW
710 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
711 if (remote->sa.sa_family == AF_INET) {
712 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
713 ip->sa.sa_family = AF_INET;
714#if IS_ENABLED(CONFIG_IPV6)
715 } else {
716 ip->sin6.sin6_addr = in6addr_any;
717 ip->sa.sa_family = AF_INET6;
718#endif
719 }
f0b074be 720 }
d342894c 721
6681712d 722 if (tb[NDA_PORT]) {
73cf3317 723 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
6681712d 724 return -EINVAL;
f0b074be
MR
725 *port = nla_get_be16(tb[NDA_PORT]);
726 } else {
727 *port = vxlan->dst_port;
728 }
6681712d
DS
729
730 if (tb[NDA_VNI]) {
731 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
732 return -EINVAL;
f0b074be
MR
733 *vni = nla_get_u32(tb[NDA_VNI]);
734 } else {
735 *vni = vxlan->default_dst.remote_vni;
736 }
6681712d
DS
737
738 if (tb[NDA_IFINDEX]) {
5abb0029 739 struct net_device *tdev;
6681712d
DS
740
741 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
742 return -EINVAL;
f0b074be
MR
743 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
744 tdev = dev_get_by_index(net, *ifindex);
5abb0029 745 if (!tdev)
6681712d 746 return -EADDRNOTAVAIL;
5abb0029 747 dev_put(tdev);
f0b074be
MR
748 } else {
749 *ifindex = 0;
750 }
751
752 return 0;
753}
754
755/* Add static entry (via netlink) */
756static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
757 struct net_device *dev,
758 const unsigned char *addr, u16 flags)
759{
760 struct vxlan_dev *vxlan = netdev_priv(dev);
761 /* struct net *net = dev_net(vxlan->dev); */
e4c7ed41 762 union vxlan_addr ip;
f0b074be
MR
763 __be16 port;
764 u32 vni, ifindex;
765 int err;
766
767 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
768 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
769 ndm->ndm_state);
770 return -EINVAL;
771 }
772
773 if (tb[NDA_DST] == NULL)
774 return -EINVAL;
775
776 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
777 if (err)
778 return err;
6681712d 779
d342894c 780 spin_lock_bh(&vxlan->hash_lock);
e4c7ed41 781 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
73cf3317 782 port, vni, ifindex, ndm->ndm_flags);
d342894c 783 spin_unlock_bh(&vxlan->hash_lock);
784
785 return err;
786}
787
788/* Delete entry (via netlink) */
1690be63
VY
789static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
790 struct net_device *dev,
d342894c 791 const unsigned char *addr)
792{
793 struct vxlan_dev *vxlan = netdev_priv(dev);
794 struct vxlan_fdb *f;
bc7892ba 795 struct vxlan_rdst *rd = NULL;
e4c7ed41 796 union vxlan_addr ip;
bc7892ba
MR
797 __be16 port;
798 u32 vni, ifindex;
799 int err;
800
801 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
802 if (err)
803 return err;
804
805 err = -ENOENT;
d342894c 806
807 spin_lock_bh(&vxlan->hash_lock);
808 f = vxlan_find_mac(vxlan, addr);
bc7892ba
MR
809 if (!f)
810 goto out;
811
e4c7ed41
CW
812 if (!vxlan_addr_any(&ip)) {
813 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
bc7892ba
MR
814 if (!rd)
815 goto out;
816 }
817
818 err = 0;
819
820 /* remove a destination if it's not the only one on the list,
821 * otherwise destroy the fdb entry
822 */
823 if (rd && !list_is_singular(&f->remotes)) {
824 list_del_rcu(&rd->list);
dcdc7a59 825 kfree_rcu(rd, rcu);
bc7892ba 826 goto out;
d342894c 827 }
bc7892ba
MR
828
829 vxlan_fdb_destroy(vxlan, f);
830
831out:
d342894c 832 spin_unlock_bh(&vxlan->hash_lock);
833
834 return err;
835}
836
837/* Dump forwarding table */
838static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
839 struct net_device *dev, int idx)
840{
841 struct vxlan_dev *vxlan = netdev_priv(dev);
842 unsigned int h;
843
844 for (h = 0; h < FDB_HASH_SIZE; ++h) {
845 struct vxlan_fdb *f;
d342894c 846 int err;
847
b67bfe0d 848 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
6681712d 849 struct vxlan_rdst *rd;
6681712d 850
3e61aa8f
SH
851 if (idx < cb->args[0])
852 goto skip;
853
854 list_for_each_entry_rcu(rd, &f->remotes, list) {
6681712d
DS
855 err = vxlan_fdb_info(skb, vxlan, f,
856 NETLINK_CB(cb->skb).portid,
857 cb->nlh->nlmsg_seq,
858 RTM_NEWNEIGH,
859 NLM_F_MULTI, rd);
860 if (err < 0)
3e61aa8f 861 goto out;
6681712d 862 }
3e61aa8f
SH
863skip:
864 ++idx;
d342894c 865 }
866 }
3e61aa8f 867out:
d342894c 868 return idx;
869}
870
871/* Watch incoming packets to learn mapping between Ethernet address
872 * and Tunnel endpoint.
26a41ae6 873 * Return true if packet is bogus and should be droppped.
d342894c 874 */
26a41ae6 875static bool vxlan_snoop(struct net_device *dev,
e4c7ed41 876 union vxlan_addr *src_ip, const u8 *src_mac)
d342894c 877{
878 struct vxlan_dev *vxlan = netdev_priv(dev);
879 struct vxlan_fdb *f;
d342894c 880
881 f = vxlan_find_mac(vxlan, src_mac);
882 if (likely(f)) {
5ca5461c 883 struct vxlan_rdst *rdst = first_remote_rcu(f);
3e61aa8f 884
e4c7ed41 885 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
26a41ae6 886 return false;
887
888 /* Don't migrate static entries, drop packets */
eb064c3b 889 if (f->state & NUD_NOARP)
26a41ae6 890 return true;
d342894c 891
892 if (net_ratelimit())
893 netdev_info(dev,
e4c7ed41 894 "%pM migrated from %pIS to %pIS\n",
3e61aa8f 895 src_mac, &rdst->remote_ip, &src_ip);
d342894c 896
e4c7ed41 897 rdst->remote_ip = *src_ip;
d342894c 898 f->updated = jiffies;
8385f50a 899 vxlan_fdb_notify(vxlan, f, RTM_NEWNEIGH);
d342894c 900 } else {
901 /* learned new entry */
902 spin_lock(&vxlan->hash_lock);
3bf74b1a 903
904 /* close off race between vxlan_flush and incoming packets */
905 if (netif_running(dev))
906 vxlan_fdb_create(vxlan, src_mac, src_ip,
907 NUD_REACHABLE,
908 NLM_F_EXCL|NLM_F_CREATE,
909 vxlan->dst_port,
910 vxlan->default_dst.remote_vni,
911 0, NTF_SELF);
d342894c 912 spin_unlock(&vxlan->hash_lock);
913 }
26a41ae6 914
915 return false;
d342894c 916}
917
d342894c 918/* See if multicast group is already in use by other ID */
e4c7ed41 919static bool vxlan_group_used(struct vxlan_net *vn, union vxlan_addr *remote_ip)
d342894c 920{
553675fb 921 struct vxlan_dev *vxlan;
d342894c 922
553675fb 923 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
553675fb 924 if (!netif_running(vxlan->dev))
925 continue;
d342894c 926
e4c7ed41
CW
927 if (vxlan_addr_equal(&vxlan->default_dst.remote_ip,
928 remote_ip))
553675fb 929 return true;
930 }
d342894c 931
932 return false;
933}
934
7c47cedf 935static void vxlan_sock_hold(struct vxlan_sock *vs)
d342894c 936{
7c47cedf
SH
937 atomic_inc(&vs->refcnt);
938}
d342894c 939
012a5729 940void vxlan_sock_release(struct vxlan_sock *vs)
7c47cedf 941{
53cf5275
JG
942 struct sock *sk = vs->sock->sk;
943 struct net *net = sock_net(sk);
944 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
012a5729 945
7c47cedf
SH
946 if (!atomic_dec_and_test(&vs->refcnt))
947 return;
d342894c 948
1c51a915 949 spin_lock(&vn->sock_lock);
7c47cedf 950 hlist_del_rcu(&vs->hlist);
559835ea 951 rcu_assign_sk_user_data(vs->sock->sk, NULL);
53cf5275 952 vxlan_notify_del_rx_port(sk);
1c51a915
SH
953 spin_unlock(&vn->sock_lock);
954
7c47cedf 955 queue_work(vxlan_wq, &vs->del_work);
d342894c 956}
012a5729 957EXPORT_SYMBOL_GPL(vxlan_sock_release);
d342894c 958
3fc2de2f 959/* Callback to update multicast group membership when first VNI on
960 * multicast asddress is brought up
961 * Done as workqueue because ip_mc_join_group acquires RTNL.
7c47cedf 962 */
3fc2de2f 963static void vxlan_igmp_join(struct work_struct *work)
d342894c 964{
3fc2de2f 965 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
7c47cedf
SH
966 struct vxlan_sock *vs = vxlan->vn_sock;
967 struct sock *sk = vs->sock->sk;
e4c7ed41
CW
968 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
969 int ifindex = vxlan->default_dst.remote_ifindex;
d342894c 970
d342894c 971 lock_sock(sk);
e4c7ed41
CW
972 if (ip->sa.sa_family == AF_INET) {
973 struct ip_mreqn mreq = {
974 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
975 .imr_ifindex = ifindex,
976 };
977
978 ip_mc_join_group(sk, &mreq);
979#if IS_ENABLED(CONFIG_IPV6)
980 } else {
981 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
982 &ip->sin6.sin6_addr);
983#endif
984 }
3fc2de2f 985 release_sock(sk);
986
012a5729 987 vxlan_sock_release(vs);
3fc2de2f 988 dev_put(vxlan->dev);
989}
990
991/* Inverse of vxlan_igmp_join when last VNI is brought down */
992static void vxlan_igmp_leave(struct work_struct *work)
993{
994 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
3fc2de2f 995 struct vxlan_sock *vs = vxlan->vn_sock;
996 struct sock *sk = vs->sock->sk;
e4c7ed41
CW
997 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
998 int ifindex = vxlan->default_dst.remote_ifindex;
3fc2de2f 999
1000 lock_sock(sk);
e4c7ed41
CW
1001 if (ip->sa.sa_family == AF_INET) {
1002 struct ip_mreqn mreq = {
1003 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1004 .imr_ifindex = ifindex,
1005 };
1006
1007 ip_mc_leave_group(sk, &mreq);
1008#if IS_ENABLED(CONFIG_IPV6)
1009 } else {
1010 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1011 &ip->sin6.sin6_addr);
1012#endif
1013 }
1014
d342894c 1015 release_sock(sk);
d342894c 1016
012a5729 1017 vxlan_sock_release(vs);
7c47cedf 1018 dev_put(vxlan->dev);
d342894c 1019}
1020
1021/* Callback from net/ipv4/udp.c to receive packets */
1022static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1023{
5cfccc5a 1024 struct vxlan_sock *vs;
d342894c 1025 struct vxlanhdr *vxh;
553675fb 1026 __be16 port;
d342894c 1027
d342894c 1028 /* Need Vxlan and inner Ethernet header to be present */
7ce04758 1029 if (!pskb_may_pull(skb, VXLAN_HLEN))
d342894c 1030 goto error;
1031
7ce04758
PS
1032 /* Return packets with reserved bits set */
1033 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
d342894c 1034 if (vxh->vx_flags != htonl(VXLAN_FLAGS) ||
1035 (vxh->vx_vni & htonl(0xff))) {
1036 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1037 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1038 goto error;
1039 }
1040
5cfccc5a
PS
1041 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1042 goto drop;
1043
553675fb 1044 port = inet_sk(sk)->inet_sport;
5cfccc5a 1045
559835ea 1046 vs = rcu_dereference_sk_user_data(sk);
5cfccc5a 1047 if (!vs)
d342894c 1048 goto drop;
d342894c 1049
5cfccc5a
PS
1050 vs->rcv(vs, skb, vxh->vx_vni);
1051 return 0;
1052
1053drop:
1054 /* Consume bad packet */
1055 kfree_skb(skb);
1056 return 0;
1057
1058error:
1059 /* Return non vxlan pkt */
1060 return 1;
1061}
1062
1063static void vxlan_rcv(struct vxlan_sock *vs,
1064 struct sk_buff *skb, __be32 vx_vni)
1065{
e4c7ed41
CW
1066 struct iphdr *oip = NULL;
1067 struct ipv6hdr *oip6 = NULL;
5cfccc5a
PS
1068 struct vxlan_dev *vxlan;
1069 struct pcpu_tstats *stats;
e4c7ed41 1070 union vxlan_addr saddr;
5cfccc5a 1071 __u32 vni;
e4c7ed41
CW
1072 int err = 0;
1073 union vxlan_addr *remote_ip;
5cfccc5a
PS
1074
1075 vni = ntohl(vx_vni) >> 8;
1076 /* Is this VNI defined? */
1077 vxlan = vxlan_vs_find_vni(vs, vni);
1078 if (!vxlan)
d342894c 1079 goto drop;
d342894c 1080
e4c7ed41 1081 remote_ip = &vxlan->default_dst.remote_ip;
e4f67add 1082 skb_reset_mac_header(skb);
d342894c 1083 skb->protocol = eth_type_trans(skb, vxlan->dev);
d342894c 1084
1085 /* Ignore packet loops (and multicast echo) */
7367d0b5 1086 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
d342894c 1087 goto drop;
1088
7ce04758 1089 /* Re-examine inner Ethernet packet */
e4c7ed41
CW
1090 if (remote_ip->sa.sa_family == AF_INET) {
1091 oip = ip_hdr(skb);
1092 saddr.sin.sin_addr.s_addr = oip->saddr;
1093 saddr.sa.sa_family = AF_INET;
1094#if IS_ENABLED(CONFIG_IPV6)
1095 } else {
1096 oip6 = ipv6_hdr(skb);
1097 saddr.sin6.sin6_addr = oip6->saddr;
1098 saddr.sa.sa_family = AF_INET6;
1099#endif
1100 }
1101
26a41ae6 1102 if ((vxlan->flags & VXLAN_F_LEARN) &&
e4c7ed41 1103 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
26a41ae6 1104 goto drop;
d342894c 1105
d342894c 1106 skb_reset_network_header(skb);
0afb1666
JG
1107
1108 /* If the NIC driver gave us an encapsulated packet with
1109 * CHECKSUM_UNNECESSARY and Rx checksum feature is enabled,
1110 * leave the CHECKSUM_UNNECESSARY, the device checksummed it
1111 * for us. Otherwise force the upper layers to verify it.
1112 */
1113 if (skb->ip_summed != CHECKSUM_UNNECESSARY || !skb->encapsulation ||
1114 !(vxlan->dev->features & NETIF_F_RXCSUM))
1115 skb->ip_summed = CHECKSUM_NONE;
1116
1117 skb->encapsulation = 0;
d342894c 1118
e4c7ed41
CW
1119 if (oip6)
1120 err = IP6_ECN_decapsulate(oip6, skb);
1121 if (oip)
1122 err = IP_ECN_decapsulate(oip, skb);
1123
d342894c 1124 if (unlikely(err)) {
e4c7ed41
CW
1125 if (log_ecn_error) {
1126 if (oip6)
1127 net_info_ratelimited("non-ECT from %pI6\n",
1128 &oip6->saddr);
1129 if (oip)
1130 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1131 &oip->saddr, oip->tos);
1132 }
d342894c 1133 if (err > 1) {
1134 ++vxlan->dev->stats.rx_frame_errors;
1135 ++vxlan->dev->stats.rx_errors;
1136 goto drop;
1137 }
1138 }
1139
e8171045 1140 stats = this_cpu_ptr(vxlan->dev->tstats);
d342894c 1141 u64_stats_update_begin(&stats->syncp);
1142 stats->rx_packets++;
1143 stats->rx_bytes += skb->len;
1144 u64_stats_update_end(&stats->syncp);
1145
1146 netif_rx(skb);
1147
5cfccc5a 1148 return;
d342894c 1149drop:
1150 /* Consume bad packet */
1151 kfree_skb(skb);
d342894c 1152}
1153
e4f67add
DS
1154static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1155{
1156 struct vxlan_dev *vxlan = netdev_priv(dev);
1157 struct arphdr *parp;
1158 u8 *arpptr, *sha;
1159 __be32 sip, tip;
1160 struct neighbour *n;
1161
1162 if (dev->flags & IFF_NOARP)
1163 goto out;
1164
1165 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1166 dev->stats.tx_dropped++;
1167 goto out;
1168 }
1169 parp = arp_hdr(skb);
1170
1171 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1172 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1173 parp->ar_pro != htons(ETH_P_IP) ||
1174 parp->ar_op != htons(ARPOP_REQUEST) ||
1175 parp->ar_hln != dev->addr_len ||
1176 parp->ar_pln != 4)
1177 goto out;
1178 arpptr = (u8 *)parp + sizeof(struct arphdr);
1179 sha = arpptr;
1180 arpptr += dev->addr_len; /* sha */
1181 memcpy(&sip, arpptr, sizeof(sip));
1182 arpptr += sizeof(sip);
1183 arpptr += dev->addr_len; /* tha */
1184 memcpy(&tip, arpptr, sizeof(tip));
1185
1186 if (ipv4_is_loopback(tip) ||
1187 ipv4_is_multicast(tip))
1188 goto out;
1189
1190 n = neigh_lookup(&arp_tbl, &tip, dev);
1191
1192 if (n) {
e4f67add
DS
1193 struct vxlan_fdb *f;
1194 struct sk_buff *reply;
1195
1196 if (!(n->nud_state & NUD_CONNECTED)) {
1197 neigh_release(n);
1198 goto out;
1199 }
1200
1201 f = vxlan_find_mac(vxlan, n->ha);
e4c7ed41 1202 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
e4f67add
DS
1203 /* bridge-local neighbor */
1204 neigh_release(n);
1205 goto out;
1206 }
1207
1208 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1209 n->ha, sha);
1210
1211 neigh_release(n);
1212
1213 skb_reset_mac_header(reply);
1214 __skb_pull(reply, skb_network_offset(reply));
1215 reply->ip_summed = CHECKSUM_UNNECESSARY;
1216 reply->pkt_type = PACKET_HOST;
1217
1218 if (netif_rx_ni(reply) == NET_RX_DROP)
1219 dev->stats.rx_dropped++;
e4c7ed41
CW
1220 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1221 union vxlan_addr ipa = {
1222 .sin.sin_addr.s_addr = tip,
1223 .sa.sa_family = AF_INET,
1224 };
1225
1226 vxlan_ip_miss(dev, &ipa);
1227 }
e4f67add
DS
1228out:
1229 consume_skb(skb);
1230 return NETDEV_TX_OK;
1231}
1232
f564f45c
CW
1233#if IS_ENABLED(CONFIG_IPV6)
1234static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1235{
1236 struct vxlan_dev *vxlan = netdev_priv(dev);
1237 struct neighbour *n;
1238 union vxlan_addr ipa;
1239 const struct ipv6hdr *iphdr;
1240 const struct in6_addr *saddr, *daddr;
1241 struct nd_msg *msg;
1242 struct inet6_dev *in6_dev = NULL;
1243
1244 in6_dev = __in6_dev_get(dev);
1245 if (!in6_dev)
1246 goto out;
1247
1248 if (!pskb_may_pull(skb, skb->len))
1249 goto out;
1250
1251 iphdr = ipv6_hdr(skb);
1252 saddr = &iphdr->saddr;
1253 daddr = &iphdr->daddr;
1254
1255 if (ipv6_addr_loopback(daddr) ||
1256 ipv6_addr_is_multicast(daddr))
1257 goto out;
1258
1259 msg = (struct nd_msg *)skb_transport_header(skb);
1260 if (msg->icmph.icmp6_code != 0 ||
1261 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1262 goto out;
1263
1264 n = neigh_lookup(ipv6_stub->nd_tbl, daddr, dev);
1265
1266 if (n) {
1267 struct vxlan_fdb *f;
1268
1269 if (!(n->nud_state & NUD_CONNECTED)) {
1270 neigh_release(n);
1271 goto out;
1272 }
1273
1274 f = vxlan_find_mac(vxlan, n->ha);
1275 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1276 /* bridge-local neighbor */
1277 neigh_release(n);
1278 goto out;
1279 }
1280
1281 ipv6_stub->ndisc_send_na(dev, n, saddr, &msg->target,
1282 !!in6_dev->cnf.forwarding,
1283 true, false, false);
1284 neigh_release(n);
1285 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1286 ipa.sin6.sin6_addr = *daddr;
1287 ipa.sa.sa_family = AF_INET6;
1288 vxlan_ip_miss(dev, &ipa);
1289 }
1290
1291out:
1292 consume_skb(skb);
1293 return NETDEV_TX_OK;
1294}
1295#endif
1296
e4f67add
DS
1297static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1298{
1299 struct vxlan_dev *vxlan = netdev_priv(dev);
1300 struct neighbour *n;
e4f67add
DS
1301
1302 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1303 return false;
1304
1305 n = NULL;
1306 switch (ntohs(eth_hdr(skb)->h_proto)) {
1307 case ETH_P_IP:
e15a00aa
CW
1308 {
1309 struct iphdr *pip;
1310
e4f67add
DS
1311 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1312 return false;
1313 pip = ip_hdr(skb);
1314 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
e4c7ed41
CW
1315 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1316 union vxlan_addr ipa = {
1317 .sin.sin_addr.s_addr = pip->daddr,
1318 .sa.sa_family = AF_INET,
1319 };
1320
1321 vxlan_ip_miss(dev, &ipa);
1322 return false;
1323 }
1324
e4f67add 1325 break;
e15a00aa
CW
1326 }
1327#if IS_ENABLED(CONFIG_IPV6)
1328 case ETH_P_IPV6:
1329 {
1330 struct ipv6hdr *pip6;
1331
1332 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1333 return false;
1334 pip6 = ipv6_hdr(skb);
1335 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1336 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1337 union vxlan_addr ipa = {
1338 .sin6.sin6_addr = pip6->daddr,
1339 .sa.sa_family = AF_INET6,
1340 };
1341
1342 vxlan_ip_miss(dev, &ipa);
1343 return false;
1344 }
1345
1346 break;
1347 }
1348#endif
e4f67add
DS
1349 default:
1350 return false;
1351 }
1352
1353 if (n) {
1354 bool diff;
1355
7367d0b5 1356 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
e4f67add
DS
1357 if (diff) {
1358 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1359 dev->addr_len);
1360 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1361 }
1362 neigh_release(n);
1363 return diff;
e4c7ed41
CW
1364 }
1365
e4f67add
DS
1366 return false;
1367}
1368
553675fb 1369static void vxlan_sock_put(struct sk_buff *skb)
1cad8715 1370{
1371 sock_put(skb->sk);
1372}
1373
1374/* On transmit, associate with the tunnel socket */
49560532 1375static void vxlan_set_owner(struct sock *sk, struct sk_buff *skb)
1cad8715 1376{
1cad8715 1377 skb_orphan(skb);
1378 sock_hold(sk);
1379 skb->sk = sk;
553675fb 1380 skb->destructor = vxlan_sock_put;
1cad8715 1381}
1382
05f47d69 1383/* Compute source port for outgoing packet
1384 * first choice to use L4 flow hash since it will spread
1385 * better and maybe available from hardware
1386 * secondary choice is to use jhash on the Ethernet header
1387 */
49560532 1388__be16 vxlan_src_port(__u16 port_min, __u16 port_max, struct sk_buff *skb)
05f47d69 1389{
49560532 1390 unsigned int range = (port_max - port_min) + 1;
05f47d69 1391 u32 hash;
1392
1393 hash = skb_get_rxhash(skb);
1394 if (!hash)
1395 hash = jhash(skb->data, 2 * ETH_ALEN,
1396 (__force u32) skb->protocol);
1397
49560532 1398 return htons((((u64) hash * range) >> 32) + port_min);
05f47d69 1399}
49560532 1400EXPORT_SYMBOL_GPL(vxlan_src_port);
05f47d69 1401
05c0db08
PS
1402static int handle_offloads(struct sk_buff *skb)
1403{
1404 if (skb_is_gso(skb)) {
1405 int err = skb_unclone(skb, GFP_ATOMIC);
1406 if (unlikely(err))
1407 return err;
1408
f6ace502 1409 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL;
05c0db08
PS
1410 } else if (skb->ip_summed != CHECKSUM_PARTIAL)
1411 skb->ip_summed = CHECKSUM_NONE;
1412
1413 return 0;
1414}
1415
e4c7ed41 1416#if IS_ENABLED(CONFIG_IPV6)
11796187 1417static int vxlan6_xmit_skb(struct vxlan_sock *vs,
e4c7ed41
CW
1418 struct dst_entry *dst, struct sk_buff *skb,
1419 struct net_device *dev, struct in6_addr *saddr,
1420 struct in6_addr *daddr, __u8 prio, __u8 ttl,
1421 __be16 src_port, __be16 dst_port, __be32 vni)
1422{
1423 struct ipv6hdr *ip6h;
1424 struct vxlanhdr *vxh;
1425 struct udphdr *uh;
1426 int min_headroom;
1427 int err;
1428
1429 if (!skb->encapsulation) {
1430 skb_reset_inner_headers(skb);
1431 skb->encapsulation = 1;
1432 }
1433
963a88b3
ND
1434 skb_scrub_packet(skb, false);
1435
e4c7ed41
CW
1436 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1437 + VXLAN_HLEN + sizeof(struct ipv6hdr)
1438 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
1439
1440 /* Need space for new headers (invalidates iph ptr) */
1441 err = skb_cow_head(skb, min_headroom);
1442 if (unlikely(err))
1443 return err;
1444
1445 if (vlan_tx_tag_present(skb)) {
1446 if (WARN_ON(!__vlan_put_tag(skb,
1447 skb->vlan_proto,
1448 vlan_tx_tag_get(skb))))
1449 return -ENOMEM;
1450
1451 skb->vlan_tci = 0;
1452 }
1453
1454 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1455 vxh->vx_flags = htonl(VXLAN_FLAGS);
1456 vxh->vx_vni = vni;
1457
1458 __skb_push(skb, sizeof(*uh));
1459 skb_reset_transport_header(skb);
1460 uh = udp_hdr(skb);
1461
1462 uh->dest = dst_port;
1463 uh->source = src_port;
1464
1465 uh->len = htons(skb->len);
1466 uh->check = 0;
1467
1468 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1469 IPCB(skb)->flags &= ~(IPSKB_XFRM_TUNNEL_SIZE | IPSKB_XFRM_TRANSFORMED |
1470 IPSKB_REROUTED);
e4c7ed41
CW
1471 skb_dst_set(skb, dst);
1472
1473 if (!skb_is_gso(skb) && !(dst->dev->features & NETIF_F_IPV6_CSUM)) {
1474 __wsum csum = skb_checksum(skb, 0, skb->len, 0);
1475 skb->ip_summed = CHECKSUM_UNNECESSARY;
1476 uh->check = csum_ipv6_magic(saddr, daddr, skb->len,
1477 IPPROTO_UDP, csum);
1478 if (uh->check == 0)
1479 uh->check = CSUM_MANGLED_0;
1480 } else {
1481 skb->ip_summed = CHECKSUM_PARTIAL;
1482 skb->csum_start = skb_transport_header(skb) - skb->head;
1483 skb->csum_offset = offsetof(struct udphdr, check);
1484 uh->check = ~csum_ipv6_magic(saddr, daddr,
1485 skb->len, IPPROTO_UDP, 0);
1486 }
1487
1488 __skb_push(skb, sizeof(*ip6h));
1489 skb_reset_network_header(skb);
1490 ip6h = ipv6_hdr(skb);
1491 ip6h->version = 6;
1492 ip6h->priority = prio;
1493 ip6h->flow_lbl[0] = 0;
1494 ip6h->flow_lbl[1] = 0;
1495 ip6h->flow_lbl[2] = 0;
1496 ip6h->payload_len = htons(skb->len);
1497 ip6h->nexthdr = IPPROTO_UDP;
1498 ip6h->hop_limit = ttl;
1499 ip6h->daddr = *daddr;
1500 ip6h->saddr = *saddr;
1501
1502 vxlan_set_owner(vs->sock->sk, skb);
1503
1504 err = handle_offloads(skb);
1505 if (err)
1506 return err;
1507
1508 ip6tunnel_xmit(skb, dev);
1509 return 0;
1510}
1511#endif
1512
11796187 1513int vxlan_xmit_skb(struct vxlan_sock *vs,
49560532
PS
1514 struct rtable *rt, struct sk_buff *skb,
1515 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
1516 __be16 src_port, __be16 dst_port, __be32 vni)
1517{
1518 struct vxlanhdr *vxh;
1519 struct udphdr *uh;
649c5b8b 1520 int min_headroom;
49560532
PS
1521 int err;
1522
1523 if (!skb->encapsulation) {
1524 skb_reset_inner_headers(skb);
1525 skb->encapsulation = 1;
1526 }
1527
649c5b8b 1528 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1eaa8178
PS
1529 + VXLAN_HLEN + sizeof(struct iphdr)
1530 + (vlan_tx_tag_present(skb) ? VLAN_HLEN : 0);
649c5b8b
PS
1531
1532 /* Need space for new headers (invalidates iph ptr) */
1533 err = skb_cow_head(skb, min_headroom);
1534 if (unlikely(err))
1535 return err;
1536
1eaa8178
PS
1537 if (vlan_tx_tag_present(skb)) {
1538 if (WARN_ON(!__vlan_put_tag(skb,
1539 skb->vlan_proto,
1540 vlan_tx_tag_get(skb))))
1541 return -ENOMEM;
1542
1543 skb->vlan_tci = 0;
1544 }
1545
49560532
PS
1546 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
1547 vxh->vx_flags = htonl(VXLAN_FLAGS);
1548 vxh->vx_vni = vni;
1549
1550 __skb_push(skb, sizeof(*uh));
1551 skb_reset_transport_header(skb);
1552 uh = udp_hdr(skb);
1553
1554 uh->dest = dst_port;
1555 uh->source = src_port;
1556
1557 uh->len = htons(skb->len);
1558 uh->check = 0;
1559
1560 vxlan_set_owner(vs->sock->sk, skb);
1561
1562 err = handle_offloads(skb);
1563 if (err)
1564 return err;
1565
963a88b3
ND
1566 return iptunnel_xmit(rt, skb, src, dst, IPPROTO_UDP, tos, ttl, df,
1567 false);
49560532
PS
1568}
1569EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1570
9dcc71e1
SS
1571/* Bypass encapsulation if the destination is local */
1572static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1573 struct vxlan_dev *dst_vxlan)
1574{
1575 struct pcpu_tstats *tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1576 struct pcpu_tstats *rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
e4c7ed41
CW
1577 union vxlan_addr loopback;
1578 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
9dcc71e1
SS
1579
1580 skb->pkt_type = PACKET_HOST;
1581 skb->encapsulation = 0;
1582 skb->dev = dst_vxlan->dev;
1583 __skb_pull(skb, skb_network_offset(skb));
1584
e4c7ed41
CW
1585 if (remote_ip->sa.sa_family == AF_INET) {
1586 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1587 loopback.sa.sa_family = AF_INET;
1588#if IS_ENABLED(CONFIG_IPV6)
1589 } else {
1590 loopback.sin6.sin6_addr = in6addr_loopback;
1591 loopback.sa.sa_family = AF_INET6;
1592#endif
1593 }
1594
9dcc71e1 1595 if (dst_vxlan->flags & VXLAN_F_LEARN)
e4c7ed41 1596 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
9dcc71e1
SS
1597
1598 u64_stats_update_begin(&tx_stats->syncp);
1599 tx_stats->tx_packets++;
1600 tx_stats->tx_bytes += skb->len;
1601 u64_stats_update_end(&tx_stats->syncp);
1602
1603 if (netif_rx(skb) == NET_RX_SUCCESS) {
1604 u64_stats_update_begin(&rx_stats->syncp);
1605 rx_stats->rx_packets++;
1606 rx_stats->rx_bytes += skb->len;
1607 u64_stats_update_end(&rx_stats->syncp);
1608 } else {
1609 skb->dev->stats.rx_dropped++;
1610 }
1611}
1612
4ad16930
SH
1613static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1614 struct vxlan_rdst *rdst, bool did_rsc)
d342894c 1615{
1616 struct vxlan_dev *vxlan = netdev_priv(dev);
e4c7ed41 1617 struct rtable *rt = NULL;
d342894c 1618 const struct iphdr *old_iph;
d342894c 1619 struct flowi4 fl4;
e4c7ed41
CW
1620 union vxlan_addr *dst;
1621 __be16 src_port = 0, dst_port;
234f5b73 1622 u32 vni;
d342894c 1623 __be16 df = 0;
1624 __u8 tos, ttl;
0e6fbc5b 1625 int err;
d342894c 1626
823aa873 1627 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
6681712d 1628 vni = rdst->remote_vni;
e4c7ed41 1629 dst = &rdst->remote_ip;
e4f67add 1630
e4c7ed41 1631 if (vxlan_addr_any(dst)) {
e4f67add 1632 if (did_rsc) {
e4f67add 1633 /* short-circuited back to local bridge */
9dcc71e1 1634 vxlan_encap_bypass(skb, vxlan, vxlan);
4ad16930 1635 return;
e4f67add 1636 }
ef59febe 1637 goto drop;
e4f67add 1638 }
ef59febe 1639
d342894c 1640 old_iph = ip_hdr(skb);
1641
d342894c 1642 ttl = vxlan->ttl;
e4c7ed41 1643 if (!ttl && vxlan_addr_multicast(dst))
d342894c 1644 ttl = 1;
1645
1646 tos = vxlan->tos;
1647 if (tos == 1)
206aaafc 1648 tos = ip_tunnel_get_dsfield(old_iph, skb);
d342894c 1649
49560532 1650 src_port = vxlan_src_port(vxlan->port_min, vxlan->port_max, skb);
d342894c 1651
e4c7ed41
CW
1652 if (dst->sa.sa_family == AF_INET) {
1653 memset(&fl4, 0, sizeof(fl4));
1654 fl4.flowi4_oif = rdst->remote_ifindex;
1655 fl4.flowi4_tos = RT_TOS(tos);
1656 fl4.daddr = dst->sin.sin_addr.s_addr;
1657 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1658
1659 rt = ip_route_output_key(dev_net(dev), &fl4);
1660 if (IS_ERR(rt)) {
1661 netdev_dbg(dev, "no route to %pI4\n",
1662 &dst->sin.sin_addr.s_addr);
1663 dev->stats.tx_carrier_errors++;
1664 goto tx_error;
1665 }
d342894c 1666
e4c7ed41
CW
1667 if (rt->dst.dev == dev) {
1668 netdev_dbg(dev, "circular route to %pI4\n",
1669 &dst->sin.sin_addr.s_addr);
1670 dev->stats.collisions++;
1671 goto tx_error;
1672 }
1673
1674 /* Bypass encapsulation if the destination is local */
1675 if (rt->rt_flags & RTCF_LOCAL &&
1676 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1677 struct vxlan_dev *dst_vxlan;
1678
1679 ip_rt_put(rt);
1680 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1681 if (!dst_vxlan)
1682 goto tx_error;
1683 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1684 return;
1685 }
1686
1687 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1688 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
d342894c 1689
11796187 1690 err = vxlan_xmit_skb(vxlan->vn_sock, rt, skb,
e4c7ed41
CW
1691 fl4.saddr, dst->sin.sin_addr.s_addr,
1692 tos, ttl, df, src_port, dst_port,
1693 htonl(vni << 8));
9dcc71e1 1694
e4c7ed41
CW
1695 if (err < 0)
1696 goto rt_tx_error;
1697 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1698#if IS_ENABLED(CONFIG_IPV6)
1699 } else {
1700 struct sock *sk = vxlan->vn_sock->sock->sk;
1701 struct dst_entry *ndst;
1702 struct flowi6 fl6;
1703 u32 flags;
1704
1705 memset(&fl6, 0, sizeof(fl6));
1706 fl6.flowi6_oif = rdst->remote_ifindex;
1707 fl6.daddr = dst->sin6.sin6_addr;
1708 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
8c1bb79f 1709 fl6.flowi6_proto = IPPROTO_UDP;
e4c7ed41
CW
1710
1711 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1712 netdev_dbg(dev, "no route to %pI6\n",
1713 &dst->sin6.sin6_addr);
1714 dev->stats.tx_carrier_errors++;
9dcc71e1 1715 goto tx_error;
e4c7ed41 1716 }
d342894c 1717
e4c7ed41
CW
1718 if (ndst->dev == dev) {
1719 netdev_dbg(dev, "circular route to %pI6\n",
1720 &dst->sin6.sin6_addr);
1721 dst_release(ndst);
1722 dev->stats.collisions++;
1723 goto tx_error;
1724 }
0e6fbc5b 1725
e4c7ed41
CW
1726 /* Bypass encapsulation if the destination is local */
1727 flags = ((struct rt6_info *)ndst)->rt6i_flags;
1728 if (flags & RTF_LOCAL &&
1729 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1730 struct vxlan_dev *dst_vxlan;
1731
1732 dst_release(ndst);
1733 dst_vxlan = vxlan_find_vni(dev_net(dev), vni, dst_port);
1734 if (!dst_vxlan)
1735 goto tx_error;
1736 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1737 return;
1738 }
49560532 1739
e4c7ed41
CW
1740 ttl = ttl ? : ip6_dst_hoplimit(ndst);
1741
11796187 1742 err = vxlan6_xmit_skb(vxlan->vn_sock, ndst, skb,
e4c7ed41
CW
1743 dev, &fl6.saddr, &fl6.daddr, 0, ttl,
1744 src_port, dst_port, htonl(vni << 8));
1745#endif
1746 }
0e6fbc5b 1747
4ad16930 1748 return;
d342894c 1749
1750drop:
1751 dev->stats.tx_dropped++;
1752 goto tx_free;
1753
49560532
PS
1754rt_tx_error:
1755 ip_rt_put(rt);
d342894c 1756tx_error:
1757 dev->stats.tx_errors++;
1758tx_free:
1759 dev_kfree_skb(skb);
d342894c 1760}
1761
6681712d
DS
1762/* Transmit local packets over Vxlan
1763 *
1764 * Outer IP header inherits ECN and DF from inner header.
1765 * Outer UDP destination is the VXLAN assigned port.
1766 * source port is based on hash of flow
1767 */
1768static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
1769{
1770 struct vxlan_dev *vxlan = netdev_priv(dev);
1771 struct ethhdr *eth;
1772 bool did_rsc = false;
afbd8bae 1773 struct vxlan_rdst *rdst;
6681712d 1774 struct vxlan_fdb *f;
6681712d
DS
1775
1776 skb_reset_mac_header(skb);
1777 eth = eth_hdr(skb);
1778
f564f45c
CW
1779 if ((vxlan->flags & VXLAN_F_PROXY)) {
1780 if (ntohs(eth->h_proto) == ETH_P_ARP)
1781 return arp_reduce(dev, skb);
1782#if IS_ENABLED(CONFIG_IPV6)
1783 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
1784 skb->len >= sizeof(struct ipv6hdr) + sizeof(struct nd_msg) &&
1785 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
1786 struct nd_msg *msg;
1787
1788 msg = (struct nd_msg *)skb_transport_header(skb);
1789 if (msg->icmph.icmp6_code == 0 &&
1790 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
1791 return neigh_reduce(dev, skb);
1792 }
1793#endif
1794 }
6681712d
DS
1795
1796 f = vxlan_find_mac(vxlan, eth->h_dest);
ae884082
DS
1797 did_rsc = false;
1798
1799 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
e15a00aa
CW
1800 (ntohs(eth->h_proto) == ETH_P_IP ||
1801 ntohs(eth->h_proto) == ETH_P_IPV6)) {
ae884082
DS
1802 did_rsc = route_shortcircuit(dev, skb);
1803 if (did_rsc)
1804 f = vxlan_find_mac(vxlan, eth->h_dest);
1805 }
1806
6681712d 1807 if (f == NULL) {
afbd8bae
MR
1808 f = vxlan_find_mac(vxlan, all_zeros_mac);
1809 if (f == NULL) {
1810 if ((vxlan->flags & VXLAN_F_L2MISS) &&
1811 !is_multicast_ether_addr(eth->h_dest))
1812 vxlan_fdb_miss(vxlan, eth->h_dest);
1813
1814 dev->stats.tx_dropped++;
1815 dev_kfree_skb(skb);
1816 return NETDEV_TX_OK;
1817 }
1818 }
6681712d 1819
afbd8bae
MR
1820 list_for_each_entry_rcu(rdst, &f->remotes, list) {
1821 struct sk_buff *skb1;
6681712d 1822
afbd8bae
MR
1823 skb1 = skb_clone(skb, GFP_ATOMIC);
1824 if (skb1)
1825 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
6681712d
DS
1826 }
1827
afbd8bae 1828 dev_kfree_skb(skb);
4ad16930 1829 return NETDEV_TX_OK;
6681712d
DS
1830}
1831
d342894c 1832/* Walk the forwarding table and purge stale entries */
1833static void vxlan_cleanup(unsigned long arg)
1834{
1835 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
1836 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
1837 unsigned int h;
1838
1839 if (!netif_running(vxlan->dev))
1840 return;
1841
1842 spin_lock_bh(&vxlan->hash_lock);
1843 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1844 struct hlist_node *p, *n;
1845 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1846 struct vxlan_fdb *f
1847 = container_of(p, struct vxlan_fdb, hlist);
1848 unsigned long timeout;
1849
3c172868 1850 if (f->state & NUD_PERMANENT)
d342894c 1851 continue;
1852
1853 timeout = f->used + vxlan->age_interval * HZ;
1854 if (time_before_eq(timeout, jiffies)) {
1855 netdev_dbg(vxlan->dev,
1856 "garbage collect %pM\n",
1857 f->eth_addr);
1858 f->state = NUD_STALE;
1859 vxlan_fdb_destroy(vxlan, f);
1860 } else if (time_before(timeout, next_timer))
1861 next_timer = timeout;
1862 }
1863 }
1864 spin_unlock_bh(&vxlan->hash_lock);
1865
1866 mod_timer(&vxlan->age_timer, next_timer);
1867}
1868
9c2e24e1
PS
1869static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
1870{
1871 __u32 vni = vxlan->default_dst.remote_vni;
1872
1873 vxlan->vn_sock = vs;
1874 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
1875}
1876
d342894c 1877/* Setup stats when device is created */
1878static int vxlan_init(struct net_device *dev)
1879{
1c51a915
SH
1880 struct vxlan_dev *vxlan = netdev_priv(dev);
1881 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
1882 struct vxlan_sock *vs;
1c51a915 1883
e8171045
PS
1884 dev->tstats = alloc_percpu(struct pcpu_tstats);
1885 if (!dev->tstats)
d342894c 1886 return -ENOMEM;
1887
1c51a915 1888 spin_lock(&vn->sock_lock);
9c2e24e1 1889 vs = vxlan_find_sock(dev_net(dev), vxlan->dst_port);
1c51a915
SH
1890 if (vs) {
1891 /* If we have a socket with same port already, reuse it */
1892 atomic_inc(&vs->refcnt);
9c2e24e1 1893 vxlan_vs_add_dev(vs, vxlan);
1c51a915
SH
1894 } else {
1895 /* otherwise make new socket outside of RTNL */
1896 dev_hold(dev);
1897 queue_work(vxlan_wq, &vxlan->sock_work);
1898 }
1899 spin_unlock(&vn->sock_lock);
1900
d342894c 1901 return 0;
1902}
1903
ba609e9b 1904static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
afbd8bae
MR
1905{
1906 struct vxlan_fdb *f;
1907
1908 spin_lock_bh(&vxlan->hash_lock);
1909 f = __vxlan_find_mac(vxlan, all_zeros_mac);
1910 if (f)
1911 vxlan_fdb_destroy(vxlan, f);
1912 spin_unlock_bh(&vxlan->hash_lock);
1913}
1914
ebf4063e
SH
1915static void vxlan_uninit(struct net_device *dev)
1916{
1917 struct vxlan_dev *vxlan = netdev_priv(dev);
ebf4063e
SH
1918 struct vxlan_sock *vs = vxlan->vn_sock;
1919
ba609e9b 1920 vxlan_fdb_delete_default(vxlan);
afbd8bae 1921
ebf4063e 1922 if (vs)
012a5729 1923 vxlan_sock_release(vs);
ebf4063e
SH
1924 free_percpu(dev->tstats);
1925}
1926
d342894c 1927/* Start ageing timer and join group when device is brought up */
1928static int vxlan_open(struct net_device *dev)
1929{
3fc2de2f 1930 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
d342894c 1931 struct vxlan_dev *vxlan = netdev_priv(dev);
1c51a915
SH
1932 struct vxlan_sock *vs = vxlan->vn_sock;
1933
1934 /* socket hasn't been created */
1935 if (!vs)
1936 return -ENOTCONN;
d342894c 1937
e4c7ed41
CW
1938 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1939 vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
1c51a915 1940 vxlan_sock_hold(vs);
7c47cedf 1941 dev_hold(dev);
3fc2de2f 1942 queue_work(vxlan_wq, &vxlan->igmp_join);
d342894c 1943 }
1944
1945 if (vxlan->age_interval)
1946 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
1947
1948 return 0;
1949}
1950
1951/* Purge the forwarding table */
1952static void vxlan_flush(struct vxlan_dev *vxlan)
1953{
31fec5aa 1954 unsigned int h;
d342894c 1955
1956 spin_lock_bh(&vxlan->hash_lock);
1957 for (h = 0; h < FDB_HASH_SIZE; ++h) {
1958 struct hlist_node *p, *n;
1959 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
1960 struct vxlan_fdb *f
1961 = container_of(p, struct vxlan_fdb, hlist);
afbd8bae
MR
1962 /* the all_zeros_mac entry is deleted at vxlan_uninit */
1963 if (!is_zero_ether_addr(f->eth_addr))
1964 vxlan_fdb_destroy(vxlan, f);
d342894c 1965 }
1966 }
1967 spin_unlock_bh(&vxlan->hash_lock);
1968}
1969
1970/* Cleanup timer and forwarding table on shutdown */
1971static int vxlan_stop(struct net_device *dev)
1972{
3fc2de2f 1973 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
d342894c 1974 struct vxlan_dev *vxlan = netdev_priv(dev);
1c51a915 1975 struct vxlan_sock *vs = vxlan->vn_sock;
d342894c 1976
e4c7ed41
CW
1977 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
1978 ! vxlan_group_used(vn, &vxlan->default_dst.remote_ip)) {
1c51a915 1979 vxlan_sock_hold(vs);
7c47cedf 1980 dev_hold(dev);
3fc2de2f 1981 queue_work(vxlan_wq, &vxlan->igmp_leave);
7c47cedf 1982 }
d342894c 1983
1984 del_timer_sync(&vxlan->age_timer);
1985
1986 vxlan_flush(vxlan);
1987
1988 return 0;
1989}
1990
d342894c 1991/* Stub, nothing needs to be done. */
1992static void vxlan_set_multicast_list(struct net_device *dev)
1993{
1994}
1995
1996static const struct net_device_ops vxlan_netdev_ops = {
1997 .ndo_init = vxlan_init,
ebf4063e 1998 .ndo_uninit = vxlan_uninit,
d342894c 1999 .ndo_open = vxlan_open,
2000 .ndo_stop = vxlan_stop,
2001 .ndo_start_xmit = vxlan_xmit,
e8171045 2002 .ndo_get_stats64 = ip_tunnel_get_stats64,
d342894c 2003 .ndo_set_rx_mode = vxlan_set_multicast_list,
2004 .ndo_change_mtu = eth_change_mtu,
2005 .ndo_validate_addr = eth_validate_addr,
2006 .ndo_set_mac_address = eth_mac_addr,
2007 .ndo_fdb_add = vxlan_fdb_add,
2008 .ndo_fdb_del = vxlan_fdb_delete,
2009 .ndo_fdb_dump = vxlan_fdb_dump,
2010};
2011
2012/* Info for udev, that this is a virtual tunnel endpoint */
2013static struct device_type vxlan_type = {
2014 .name = "vxlan",
2015};
2016
53cf5275 2017/* Calls the ndo_add_vxlan_port of the caller in order to
35e42379
JG
2018 * supply the listening VXLAN udp ports. Callers are expected
2019 * to implement the ndo_add_vxlan_port.
53cf5275
JG
2020 */
2021void vxlan_get_rx_port(struct net_device *dev)
2022{
2023 struct vxlan_sock *vs;
2024 struct net *net = dev_net(dev);
2025 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2026 sa_family_t sa_family;
35e42379
JG
2027 __be16 port;
2028 unsigned int i;
53cf5275
JG
2029
2030 spin_lock(&vn->sock_lock);
2031 for (i = 0; i < PORT_HASH_SIZE; ++i) {
35e42379
JG
2032 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2033 port = inet_sk(vs->sock->sk)->inet_sport;
53cf5275
JG
2034 sa_family = vs->sock->sk->sk_family;
2035 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2036 port);
2037 }
2038 }
2039 spin_unlock(&vn->sock_lock);
2040}
2041EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2042
d342894c 2043/* Initialize the device structure. */
2044static void vxlan_setup(struct net_device *dev)
2045{
2046 struct vxlan_dev *vxlan = netdev_priv(dev);
31fec5aa 2047 unsigned int h;
05f47d69 2048 int low, high;
d342894c 2049
2050 eth_hw_addr_random(dev);
2051 ether_setup(dev);
e4c7ed41
CW
2052 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
2053 dev->hard_header_len = ETH_HLEN + VXLAN6_HEADROOM;
2054 else
2055 dev->hard_header_len = ETH_HLEN + VXLAN_HEADROOM;
d342894c 2056
2057 dev->netdev_ops = &vxlan_netdev_ops;
ebf4063e 2058 dev->destructor = free_netdev;
d342894c 2059 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2060
2061 dev->tx_queue_len = 0;
2062 dev->features |= NETIF_F_LLTX;
2063 dev->features |= NETIF_F_NETNS_LOCAL;
d6727fe3 2064 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
0afb1666 2065 dev->features |= NETIF_F_RXCSUM;
05c0db08 2066 dev->features |= NETIF_F_GSO_SOFTWARE;
0afb1666 2067
1eaa8178
PS
2068 dev->vlan_features = dev->features;
2069 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
0afb1666 2070 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
05c0db08 2071 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1eaa8178 2072 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
d342894c 2073 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
6602d007 2074 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
d342894c 2075
553675fb 2076 INIT_LIST_HEAD(&vxlan->next);
d342894c 2077 spin_lock_init(&vxlan->hash_lock);
3fc2de2f 2078 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2079 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
1c51a915 2080 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
d342894c 2081
2082 init_timer_deferrable(&vxlan->age_timer);
2083 vxlan->age_timer.function = vxlan_cleanup;
2084 vxlan->age_timer.data = (unsigned long) vxlan;
2085
0bbf87d8 2086 inet_get_local_port_range(dev_net(dev), &low, &high);
05f47d69 2087 vxlan->port_min = low;
2088 vxlan->port_max = high;
823aa873 2089 vxlan->dst_port = htons(vxlan_port);
05f47d69 2090
d342894c 2091 vxlan->dev = dev;
2092
2093 for (h = 0; h < FDB_HASH_SIZE; ++h)
2094 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2095}
2096
2097static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2098 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
5d174dd8 2099 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
e4c7ed41 2100 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
d342894c 2101 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2102 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
e4c7ed41 2103 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
d342894c 2104 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2105 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2106 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2107 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2108 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
05f47d69 2109 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
e4f67add
DS
2110 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2111 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2112 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2113 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
823aa873 2114 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
d342894c 2115};
2116
2117static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2118{
2119 if (tb[IFLA_ADDRESS]) {
2120 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2121 pr_debug("invalid link address (not ethernet)\n");
2122 return -EINVAL;
2123 }
2124
2125 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2126 pr_debug("invalid all zero ethernet address\n");
2127 return -EADDRNOTAVAIL;
2128 }
2129 }
2130
2131 if (!data)
2132 return -EINVAL;
2133
2134 if (data[IFLA_VXLAN_ID]) {
2135 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2136 if (id >= VXLAN_VID_MASK)
2137 return -ERANGE;
2138 }
2139
05f47d69 2140 if (data[IFLA_VXLAN_PORT_RANGE]) {
2141 const struct ifla_vxlan_port_range *p
2142 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2143
2144 if (ntohs(p->high) < ntohs(p->low)) {
2145 pr_debug("port range %u .. %u not valid\n",
2146 ntohs(p->low), ntohs(p->high));
2147 return -EINVAL;
2148 }
2149 }
2150
d342894c 2151 return 0;
2152}
2153
1b13c97f
YB
2154static void vxlan_get_drvinfo(struct net_device *netdev,
2155 struct ethtool_drvinfo *drvinfo)
2156{
2157 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2158 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2159}
2160
2161static const struct ethtool_ops vxlan_ethtool_ops = {
2162 .get_drvinfo = vxlan_get_drvinfo,
2163 .get_link = ethtool_op_get_link,
2164};
2165
553675fb 2166static void vxlan_del_work(struct work_struct *work)
2167{
2168 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
2169
2170 sk_release_kernel(vs->sock->sk);
2171 kfree_rcu(vs, rcu);
2172}
2173
e4c7ed41
CW
2174#if IS_ENABLED(CONFIG_IPV6)
2175/* Create UDP socket for encapsulation receive. AF_INET6 socket
2176 * could be used for both IPv4 and IPv6 communications, but
2177 * users may set bindv6only=1.
2178 */
39deb2c7 2179static struct socket *create_v6_sock(struct net *net, __be16 port)
553675fb 2180{
553675fb 2181 struct sock *sk;
e4c7ed41
CW
2182 struct socket *sock;
2183 struct sockaddr_in6 vxlan_addr = {
2184 .sin6_family = AF_INET6,
2185 .sin6_port = port,
2186 };
2187 int rc, val = 1;
2188
2189 rc = sock_create_kern(AF_INET6, SOCK_DGRAM, IPPROTO_UDP, &sock);
2190 if (rc < 0) {
2191 pr_debug("UDPv6 socket create failed\n");
39deb2c7 2192 return ERR_PTR(rc);
e4c7ed41
CW
2193 }
2194
2195 /* Put in proper namespace */
2196 sk = sock->sk;
2197 sk_change_net(sk, net);
2198
2199 kernel_setsockopt(sock, SOL_IPV6, IPV6_V6ONLY,
2200 (char *)&val, sizeof(val));
2201 rc = kernel_bind(sock, (struct sockaddr *)&vxlan_addr,
2202 sizeof(struct sockaddr_in6));
2203 if (rc < 0) {
2204 pr_debug("bind for UDPv6 socket %pI6:%u (%d)\n",
2205 &vxlan_addr.sin6_addr, ntohs(vxlan_addr.sin6_port), rc);
2206 sk_release_kernel(sk);
39deb2c7 2207 return ERR_PTR(rc);
e4c7ed41
CW
2208 }
2209 /* At this point, IPv6 module should have been loaded in
2210 * sock_create_kern().
2211 */
2212 BUG_ON(!ipv6_stub);
2213
e4c7ed41
CW
2214 /* Disable multicast loopback */
2215 inet_sk(sk)->mc_loop = 0;
39deb2c7 2216 return sock;
e4c7ed41
CW
2217}
2218
2219#else
2220
39deb2c7 2221static struct socket *create_v6_sock(struct net *net, __be16 port)
e4c7ed41 2222{
39deb2c7 2223 return ERR_PTR(-EPFNOSUPPORT);
e4c7ed41
CW
2224}
2225#endif
2226
39deb2c7 2227static struct socket *create_v4_sock(struct net *net, __be16 port)
e4c7ed41
CW
2228{
2229 struct sock *sk;
2230 struct socket *sock;
553675fb 2231 struct sockaddr_in vxlan_addr = {
2232 .sin_family = AF_INET,
2233 .sin_addr.s_addr = htonl(INADDR_ANY),
bb3fd687 2234 .sin_port = port,
553675fb 2235 };
2236 int rc;
553675fb 2237
2238 /* Create UDP socket for encapsulation receive. */
e4c7ed41 2239 rc = sock_create_kern(AF_INET, SOCK_DGRAM, IPPROTO_UDP, &sock);
553675fb 2240 if (rc < 0) {
2241 pr_debug("UDP socket create failed\n");
39deb2c7 2242 return ERR_PTR(rc);
553675fb 2243 }
2244
2245 /* Put in proper namespace */
e4c7ed41 2246 sk = sock->sk;
553675fb 2247 sk_change_net(sk, net);
2248
e4c7ed41 2249 rc = kernel_bind(sock, (struct sockaddr *) &vxlan_addr,
553675fb 2250 sizeof(vxlan_addr));
2251 if (rc < 0) {
2252 pr_debug("bind for UDP socket %pI4:%u (%d)\n",
2253 &vxlan_addr.sin_addr, ntohs(vxlan_addr.sin_port), rc);
2254 sk_release_kernel(sk);
39deb2c7 2255 return ERR_PTR(rc);
e4c7ed41
CW
2256 }
2257
e4c7ed41
CW
2258 /* Disable multicast loopback */
2259 inet_sk(sk)->mc_loop = 0;
39deb2c7 2260 return sock;
e4c7ed41
CW
2261}
2262
2263/* Create new listen socket if needed */
2264static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
2265 vxlan_rcv_t *rcv, void *data, bool ipv6)
2266{
2267 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2268 struct vxlan_sock *vs;
2269 struct socket *sock;
2270 struct sock *sk;
e4c7ed41
CW
2271 unsigned int h;
2272
2273 vs = kmalloc(sizeof(*vs), GFP_KERNEL);
2274 if (!vs)
2275 return ERR_PTR(-ENOMEM);
2276
2277 for (h = 0; h < VNI_HASH_SIZE; ++h)
2278 INIT_HLIST_HEAD(&vs->vni_list[h]);
2279
2280 INIT_WORK(&vs->del_work, vxlan_del_work);
2281
2282 if (ipv6)
39deb2c7 2283 sock = create_v6_sock(net, port);
e4c7ed41 2284 else
39deb2c7
ZYW
2285 sock = create_v4_sock(net, port);
2286 if (IS_ERR(sock)) {
553675fb 2287 kfree(vs);
39deb2c7 2288 return ERR_PTR(PTR_ERR(sock));
553675fb 2289 }
e4c7ed41
CW
2290
2291 vs->sock = sock;
2292 sk = sock->sk;
9c2e24e1 2293 atomic_set(&vs->refcnt, 1);
5cfccc5a 2294 vs->rcv = rcv;
012a5729 2295 vs->data = data;
559835ea 2296 rcu_assign_sk_user_data(vs->sock->sk, vs);
553675fb 2297
9c2e24e1
PS
2298 spin_lock(&vn->sock_lock);
2299 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
53cf5275 2300 vxlan_notify_add_rx_port(sk);
9c2e24e1 2301 spin_unlock(&vn->sock_lock);
553675fb 2302
2303 /* Mark socket as an encapsulation socket. */
2304 udp_sk(sk)->encap_type = 1;
2305 udp_sk(sk)->encap_rcv = vxlan_udp_encap_recv;
e4c7ed41
CW
2306#if IS_ENABLED(CONFIG_IPV6)
2307 if (ipv6)
2308 ipv6_stub->udpv6_encap_enable();
2309 else
2310#endif
2311 udp_encap_enable();
2312
9c2e24e1
PS
2313 return vs;
2314}
2315
012a5729
PS
2316struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2317 vxlan_rcv_t *rcv, void *data,
e4c7ed41 2318 bool no_share, bool ipv6)
9c2e24e1
PS
2319{
2320 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2321 struct vxlan_sock *vs;
2322
e4c7ed41 2323 vs = vxlan_socket_create(net, port, rcv, data, ipv6);
9c2e24e1
PS
2324 if (!IS_ERR(vs))
2325 return vs;
553675fb 2326
012a5729
PS
2327 if (no_share) /* Return error if sharing is not allowed. */
2328 return vs;
2329
9c2e24e1
PS
2330 spin_lock(&vn->sock_lock);
2331 vs = vxlan_find_sock(net, port);
5cfccc5a
PS
2332 if (vs) {
2333 if (vs->rcv == rcv)
2334 atomic_inc(&vs->refcnt);
2335 else
2336 vs = ERR_PTR(-EBUSY);
2337 }
2338 spin_unlock(&vn->sock_lock);
2339
2340 if (!vs)
9c2e24e1
PS
2341 vs = ERR_PTR(-EINVAL);
2342
553675fb 2343 return vs;
2344}
012a5729 2345EXPORT_SYMBOL_GPL(vxlan_sock_add);
553675fb 2346
1c51a915
SH
2347/* Scheduled at device creation to bind to a socket */
2348static void vxlan_sock_work(struct work_struct *work)
2349{
9c2e24e1
PS
2350 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
2351 struct net *net = dev_net(vxlan->dev);
1c51a915 2352 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
9c2e24e1
PS
2353 __be16 port = vxlan->dst_port;
2354 struct vxlan_sock *nvs;
1c51a915 2355
e4c7ed41 2356 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags & VXLAN_F_IPV6);
1c51a915 2357 spin_lock(&vn->sock_lock);
9c2e24e1
PS
2358 if (!IS_ERR(nvs))
2359 vxlan_vs_add_dev(nvs, vxlan);
2360 spin_unlock(&vn->sock_lock);
2361
2362 dev_put(vxlan->dev);
1c51a915
SH
2363}
2364
d342894c 2365static int vxlan_newlink(struct net *net, struct net_device *dev,
2366 struct nlattr *tb[], struct nlattr *data[])
2367{
553675fb 2368 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
d342894c 2369 struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 2370 struct vxlan_rdst *dst = &vxlan->default_dst;
d342894c 2371 __u32 vni;
2372 int err;
e4c7ed41 2373 bool use_ipv6 = false;
d342894c 2374
2375 if (!data[IFLA_VXLAN_ID])
2376 return -EINVAL;
2377
2378 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
c7995c43 2379 dst->remote_vni = vni;
d342894c 2380
e4c7ed41
CW
2381 if (data[IFLA_VXLAN_GROUP]) {
2382 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
2383 dst->remote_ip.sa.sa_family = AF_INET;
2384 } else if (data[IFLA_VXLAN_GROUP6]) {
2385 if (!IS_ENABLED(CONFIG_IPV6))
2386 return -EPFNOSUPPORT;
2387
2388 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2389 sizeof(struct in6_addr));
2390 dst->remote_ip.sa.sa_family = AF_INET6;
2391 use_ipv6 = true;
2392 }
d342894c 2393
e4c7ed41
CW
2394 if (data[IFLA_VXLAN_LOCAL]) {
2395 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2396 vxlan->saddr.sa.sa_family = AF_INET;
2397 } else if (data[IFLA_VXLAN_LOCAL6]) {
2398 if (!IS_ENABLED(CONFIG_IPV6))
2399 return -EPFNOSUPPORT;
2400
2401 /* TODO: respect scope id */
2402 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2403 sizeof(struct in6_addr));
2404 vxlan->saddr.sa.sa_family = AF_INET6;
2405 use_ipv6 = true;
2406 }
d342894c 2407
34e02aa1 2408 if (data[IFLA_VXLAN_LINK] &&
c7995c43 2409 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
34e02aa1 2410 struct net_device *lowerdev
c7995c43 2411 = __dev_get_by_index(net, dst->remote_ifindex);
34e02aa1 2412
2413 if (!lowerdev) {
c7995c43 2414 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
34e02aa1 2415 return -ENODEV;
2416 }
d342894c 2417
e4c7ed41
CW
2418#if IS_ENABLED(CONFIG_IPV6)
2419 if (use_ipv6) {
2420 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2421 if (idev && idev->cnf.disable_ipv6) {
2422 pr_info("IPv6 is disabled via sysctl\n");
2423 return -EPERM;
2424 }
2425 vxlan->flags |= VXLAN_F_IPV6;
2426 }
2427#endif
2428
34e02aa1 2429 if (!tb[IFLA_MTU])
e4c7ed41 2430 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
1ba56fb4
AD
2431
2432 /* update header length based on lower device */
2433 dev->hard_header_len = lowerdev->hard_header_len +
e4c7ed41 2434 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
d342894c 2435 }
2436
2437 if (data[IFLA_VXLAN_TOS])
2438 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2439
afb97186
VB
2440 if (data[IFLA_VXLAN_TTL])
2441 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2442
d342894c 2443 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
e4f67add 2444 vxlan->flags |= VXLAN_F_LEARN;
d342894c 2445
2446 if (data[IFLA_VXLAN_AGEING])
2447 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2448 else
2449 vxlan->age_interval = FDB_AGE_DEFAULT;
2450
e4f67add
DS
2451 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2452 vxlan->flags |= VXLAN_F_PROXY;
2453
2454 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2455 vxlan->flags |= VXLAN_F_RSC;
2456
2457 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2458 vxlan->flags |= VXLAN_F_L2MISS;
2459
2460 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2461 vxlan->flags |= VXLAN_F_L3MISS;
2462
d342894c 2463 if (data[IFLA_VXLAN_LIMIT])
2464 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2465
05f47d69 2466 if (data[IFLA_VXLAN_PORT_RANGE]) {
2467 const struct ifla_vxlan_port_range *p
2468 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2469 vxlan->port_min = ntohs(p->low);
2470 vxlan->port_max = ntohs(p->high);
2471 }
2472
823aa873 2473 if (data[IFLA_VXLAN_PORT])
2474 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2475
553675fb 2476 if (vxlan_find_vni(net, vni, vxlan->dst_port)) {
2477 pr_info("duplicate VNI %u\n", vni);
2478 return -EEXIST;
2479 }
2480
1b13c97f
YB
2481 SET_ETHTOOL_OPS(dev, &vxlan_ethtool_ops);
2482
2936b6ab
SS
2483 /* create an fdb entry for a valid default destination */
2484 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2485 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2486 &vxlan->default_dst.remote_ip,
2487 NUD_REACHABLE|NUD_PERMANENT,
2488 NLM_F_EXCL|NLM_F_CREATE,
2489 vxlan->dst_port,
2490 vxlan->default_dst.remote_vni,
2491 vxlan->default_dst.remote_ifindex,
2492 NTF_SELF);
2493 if (err)
2494 return err;
2495 }
d342894c 2496
afbd8bae
MR
2497 err = register_netdevice(dev);
2498 if (err) {
ba609e9b 2499 vxlan_fdb_delete_default(vxlan);
afbd8bae
MR
2500 return err;
2501 }
2502
553675fb 2503 list_add(&vxlan->next, &vn->vxlan_list);
553675fb 2504
2505 return 0;
d342894c 2506}
2507
2508static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2509{
fe5c3561 2510 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
d342894c 2511 struct vxlan_dev *vxlan = netdev_priv(dev);
2512
fe5c3561 2513 spin_lock(&vn->sock_lock);
9c2e24e1
PS
2514 if (!hlist_unhashed(&vxlan->hlist))
2515 hlist_del_rcu(&vxlan->hlist);
fe5c3561 2516 spin_unlock(&vn->sock_lock);
2517
553675fb 2518 list_del(&vxlan->next);
d342894c 2519 unregister_netdevice_queue(dev, head);
2520}
2521
2522static size_t vxlan_get_size(const struct net_device *dev)
2523{
2524
2525 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
e4c7ed41 2526 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
d342894c 2527 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
e4c7ed41 2528 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
d342894c 2529 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2530 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2531 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
e4f67add
DS
2532 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2533 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2534 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2535 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
d342894c 2536 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2537 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
05f47d69 2538 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
823aa873 2539 nla_total_size(sizeof(__be16))+ /* IFLA_VXLAN_PORT */
d342894c 2540 0;
2541}
2542
2543static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2544{
2545 const struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 2546 const struct vxlan_rdst *dst = &vxlan->default_dst;
05f47d69 2547 struct ifla_vxlan_port_range ports = {
2548 .low = htons(vxlan->port_min),
2549 .high = htons(vxlan->port_max),
2550 };
d342894c 2551
c7995c43 2552 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
d342894c 2553 goto nla_put_failure;
2554
e4c7ed41
CW
2555 if (!vxlan_addr_any(&dst->remote_ip)) {
2556 if (dst->remote_ip.sa.sa_family == AF_INET) {
2557 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2558 dst->remote_ip.sin.sin_addr.s_addr))
2559 goto nla_put_failure;
2560#if IS_ENABLED(CONFIG_IPV6)
2561 } else {
2562 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2563 &dst->remote_ip.sin6.sin6_addr))
2564 goto nla_put_failure;
2565#endif
2566 }
2567 }
d342894c 2568
c7995c43 2569 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
d342894c 2570 goto nla_put_failure;
2571
e4c7ed41
CW
2572 if (!vxlan_addr_any(&vxlan->saddr)) {
2573 if (vxlan->saddr.sa.sa_family == AF_INET) {
2574 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2575 vxlan->saddr.sin.sin_addr.s_addr))
2576 goto nla_put_failure;
2577#if IS_ENABLED(CONFIG_IPV6)
2578 } else {
2579 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2580 &vxlan->saddr.sin6.sin6_addr))
2581 goto nla_put_failure;
2582#endif
2583 }
2584 }
d342894c 2585
2586 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2587 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
e4f67add
DS
2588 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2589 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2590 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2591 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2592 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2593 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2594 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2595 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2596 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
d342894c 2597 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
823aa873 2598 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
2599 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port))
d342894c 2600 goto nla_put_failure;
2601
05f47d69 2602 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2603 goto nla_put_failure;
2604
d342894c 2605 return 0;
2606
2607nla_put_failure:
2608 return -EMSGSIZE;
2609}
2610
2611static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2612 .kind = "vxlan",
2613 .maxtype = IFLA_VXLAN_MAX,
2614 .policy = vxlan_policy,
2615 .priv_size = sizeof(struct vxlan_dev),
2616 .setup = vxlan_setup,
2617 .validate = vxlan_validate,
2618 .newlink = vxlan_newlink,
2619 .dellink = vxlan_dellink,
2620 .get_size = vxlan_get_size,
2621 .fill_info = vxlan_fill_info,
2622};
2623
2624static __net_init int vxlan_init_net(struct net *net)
2625{
2626 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
31fec5aa 2627 unsigned int h;
d342894c 2628
553675fb 2629 INIT_LIST_HEAD(&vn->vxlan_list);
1c51a915 2630 spin_lock_init(&vn->sock_lock);
d342894c 2631
553675fb 2632 for (h = 0; h < PORT_HASH_SIZE; ++h)
2633 INIT_HLIST_HEAD(&vn->sock_list[h]);
d342894c 2634
2635 return 0;
2636}
2637
2638static __net_exit void vxlan_exit_net(struct net *net)
2639{
2640 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
9cb6cb7e 2641 struct vxlan_dev *vxlan;
372675a4 2642 LIST_HEAD(list);
9cb6cb7e
ZM
2643
2644 rtnl_lock();
553675fb 2645 list_for_each_entry(vxlan, &vn->vxlan_list, next)
372675a4 2646 unregister_netdevice_queue(vxlan->dev, &list);
2647 unregister_netdevice_many(&list);
9cb6cb7e 2648 rtnl_unlock();
d342894c 2649}
2650
2651static struct pernet_operations vxlan_net_ops = {
2652 .init = vxlan_init_net,
2653 .exit = vxlan_exit_net,
2654 .id = &vxlan_net_id,
2655 .size = sizeof(struct vxlan_net),
2656};
2657
2658static int __init vxlan_init_module(void)
2659{
2660 int rc;
2661
758c57d1
SH
2662 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
2663 if (!vxlan_wq)
2664 return -ENOMEM;
2665
d342894c 2666 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
2667
2668 rc = register_pernet_device(&vxlan_net_ops);
2669 if (rc)
2670 goto out1;
2671
2672 rc = rtnl_link_register(&vxlan_link_ops);
2673 if (rc)
2674 goto out2;
2675
2676 return 0;
2677
2678out2:
2679 unregister_pernet_device(&vxlan_net_ops);
2680out1:
758c57d1 2681 destroy_workqueue(vxlan_wq);
d342894c 2682 return rc;
2683}
7332a13b 2684late_initcall(vxlan_init_module);
d342894c 2685
2686static void __exit vxlan_cleanup_module(void)
2687{
b7153984 2688 rtnl_link_unregister(&vxlan_link_ops);
758c57d1 2689 destroy_workqueue(vxlan_wq);
f89e57c4 2690 unregister_pernet_device(&vxlan_net_ops);
6681712d 2691 rcu_barrier();
d342894c 2692}
2693module_exit(vxlan_cleanup_module);
2694
2695MODULE_LICENSE("GPL");
2696MODULE_VERSION(VXLAN_VERSION);
3b8df3c6 2697MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
d342894c 2698MODULE_ALIAS_RTNL_LINK("vxlan");
This page took 0.481074 seconds and 5 git commands to generate.