]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/net/vxlan.c
net: ep93xx_eth: Delete unnecessary checks before the function call "kfree"
[mirror_ubuntu-hirsute-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.
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>
3ee64f39 36#include <net/udp_tunnel.h>
d342894c 37#include <net/rtnetlink.h>
38#include <net/route.h>
39#include <net/dsfield.h>
40#include <net/inet_ecn.h>
41#include <net/net_namespace.h>
42#include <net/netns/generic.h>
012a5729 43#include <net/vxlan.h>
dc01e7d3 44#include <net/protocol.h>
acbf74a7 45#include <net/udp_tunnel.h>
e4c7ed41
CW
46#if IS_ENABLED(CONFIG_IPV6)
47#include <net/ipv6.h>
48#include <net/addrconf.h>
49#include <net/ip6_tunnel.h>
660d98ca 50#include <net/ip6_checksum.h>
e4c7ed41 51#endif
d342894c 52
53#define VXLAN_VERSION "0.1"
54
553675fb 55#define PORT_HASH_BITS 8
56#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
d342894c 57#define VNI_HASH_BITS 10
58#define VNI_HASH_SIZE (1<<VNI_HASH_BITS)
59#define FDB_HASH_BITS 8
60#define FDB_HASH_SIZE (1<<FDB_HASH_BITS)
61#define FDB_AGE_DEFAULT 300 /* 5 min */
62#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
63
23c578bf 64/* UDP port for VXLAN traffic.
65 * The IANA assigned port is 4789, but the Linux default is 8472
234f5b73 66 * for compatibility with early adopters.
23c578bf 67 */
9daaa397
SH
68static unsigned short vxlan_port __read_mostly = 8472;
69module_param_named(udp_port, vxlan_port, ushort, 0444);
d342894c 70MODULE_PARM_DESC(udp_port, "Destination UDP port");
71
72static bool log_ecn_error = true;
73module_param(log_ecn_error, bool, 0644);
74MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
75
60d9d4c6 76static int vxlan_net_id;
553675fb 77
afbd8bae
MR
78static const u8 all_zeros_mac[ETH_ALEN];
79
553675fb 80/* per-network namespace private data for this module */
81struct vxlan_net {
82 struct list_head vxlan_list;
83 struct hlist_head sock_list[PORT_HASH_SIZE];
1c51a915 84 spinlock_t sock_lock;
553675fb 85};
86
e4c7ed41
CW
87union vxlan_addr {
88 struct sockaddr_in sin;
89 struct sockaddr_in6 sin6;
90 struct sockaddr sa;
91};
92
6681712d 93struct vxlan_rdst {
e4c7ed41 94 union vxlan_addr remote_ip;
6681712d
DS
95 __be16 remote_port;
96 u32 remote_vni;
97 u32 remote_ifindex;
3e61aa8f 98 struct list_head list;
bc7892ba 99 struct rcu_head rcu;
6681712d
DS
100};
101
d342894c 102/* Forwarding table entry */
103struct vxlan_fdb {
104 struct hlist_node hlist; /* linked list of entries */
105 struct rcu_head rcu;
106 unsigned long updated; /* jiffies */
107 unsigned long used;
3e61aa8f 108 struct list_head remotes;
d342894c 109 u16 state; /* see ndm_state */
ae884082 110 u8 flags; /* see ndm_flags */
d342894c 111 u8 eth_addr[ETH_ALEN];
112};
113
d342894c 114/* Pseudo network device */
115struct vxlan_dev {
553675fb 116 struct hlist_node hlist; /* vni hash table */
117 struct list_head next; /* vxlan's per namespace list */
118 struct vxlan_sock *vn_sock; /* listening socket */
d342894c 119 struct net_device *dev;
f01ec1c0 120 struct net *net; /* netns for packet i/o */
c7995c43 121 struct vxlan_rdst default_dst; /* default destination */
e4c7ed41 122 union vxlan_addr 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;
359a0ea9 128 u32 flags; /* VXLAN_F_* in vxlan.h */
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
143/* salt for hash table */
144static u32 vxlan_salt __read_mostly;
758c57d1 145static struct workqueue_struct *vxlan_wq;
d342894c 146
1c51a915
SH
147static void vxlan_sock_work(struct work_struct *work);
148
e4c7ed41
CW
149#if IS_ENABLED(CONFIG_IPV6)
150static inline
151bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
152{
153 if (a->sa.sa_family != b->sa.sa_family)
154 return false;
155 if (a->sa.sa_family == AF_INET6)
156 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
157 else
158 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
159}
160
161static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
162{
163 if (ipa->sa.sa_family == AF_INET6)
164 return ipv6_addr_any(&ipa->sin6.sin6_addr);
165 else
166 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
167}
168
169static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
170{
171 if (ipa->sa.sa_family == AF_INET6)
172 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
173 else
174 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
175}
176
177static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
178{
179 if (nla_len(nla) >= sizeof(struct in6_addr)) {
180 nla_memcpy(&ip->sin6.sin6_addr, nla, sizeof(struct in6_addr));
181 ip->sa.sa_family = AF_INET6;
182 return 0;
183 } else if (nla_len(nla) >= sizeof(__be32)) {
184 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
185 ip->sa.sa_family = AF_INET;
186 return 0;
187 } else {
188 return -EAFNOSUPPORT;
189 }
190}
191
192static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
193 const union vxlan_addr *ip)
194{
195 if (ip->sa.sa_family == AF_INET6)
196 return nla_put(skb, attr, sizeof(struct in6_addr), &ip->sin6.sin6_addr);
197 else
198 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
199}
200
201#else /* !CONFIG_IPV6 */
202
203static inline
204bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
205{
206 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
207}
208
209static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
210{
211 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
212}
213
214static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
215{
216 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
217}
218
219static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
220{
221 if (nla_len(nla) >= sizeof(struct in6_addr)) {
222 return -EAFNOSUPPORT;
223 } else if (nla_len(nla) >= sizeof(__be32)) {
224 ip->sin.sin_addr.s_addr = nla_get_be32(nla);
225 ip->sa.sa_family = AF_INET;
226 return 0;
227 } else {
228 return -EAFNOSUPPORT;
229 }
230}
231
232static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
233 const union vxlan_addr *ip)
234{
235 return nla_put_be32(skb, attr, ip->sin.sin_addr.s_addr);
236}
237#endif
238
553675fb 239/* Virtual Network hash table head */
240static inline struct hlist_head *vni_head(struct vxlan_sock *vs, u32 id)
241{
242 return &vs->vni_list[hash_32(id, VNI_HASH_BITS)];
243}
244
245/* Socket hash table head */
246static inline struct hlist_head *vs_head(struct net *net, __be16 port)
d342894c 247{
248 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
249
553675fb 250 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
251}
252
3e61aa8f
SH
253/* First remote destination for a forwarding entry.
254 * Guaranteed to be non-NULL because remotes are never deleted.
255 */
5ca5461c 256static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
3e61aa8f 257{
5ca5461c 258 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
259}
260
261static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
262{
263 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
3e61aa8f
SH
264}
265
ac5132d1
TG
266/* Find VXLAN socket based on network namespace, address family and UDP port
267 * and enabled unshareable flags.
268 */
269static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
270 __be16 port, u32 flags)
553675fb 271{
272 struct vxlan_sock *vs;
af33c1ad
TH
273
274 flags &= VXLAN_F_RCV_FLAGS;
553675fb 275
276 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
19ca9fc1 277 if (inet_sk(vs->sock->sk)->inet_sport == port &&
ac5132d1 278 inet_sk(vs->sock->sk)->sk.sk_family == family &&
af33c1ad 279 vs->flags == flags)
553675fb 280 return vs;
281 }
282 return NULL;
d342894c 283}
284
5cfccc5a 285static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, u32 id)
d342894c 286{
287 struct vxlan_dev *vxlan;
d342894c 288
553675fb 289 hlist_for_each_entry_rcu(vxlan, vni_head(vs, id), hlist) {
c7995c43 290 if (vxlan->default_dst.remote_vni == id)
d342894c 291 return vxlan;
292 }
293
294 return NULL;
295}
296
5cfccc5a 297/* Look up VNI in a per net namespace table */
19ca9fc1 298static struct vxlan_dev *vxlan_find_vni(struct net *net, u32 id,
ac5132d1
TG
299 sa_family_t family, __be16 port,
300 u32 flags)
5cfccc5a
PS
301{
302 struct vxlan_sock *vs;
303
ac5132d1 304 vs = vxlan_find_sock(net, family, port, flags);
5cfccc5a
PS
305 if (!vs)
306 return NULL;
307
308 return vxlan_vs_find_vni(vs, id);
309}
310
d342894c 311/* Fill in neighbour message in skbuff. */
312static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
234f5b73
SH
313 const struct vxlan_fdb *fdb,
314 u32 portid, u32 seq, int type, unsigned int flags,
315 const struct vxlan_rdst *rdst)
d342894c 316{
317 unsigned long now = jiffies;
318 struct nda_cacheinfo ci;
319 struct nlmsghdr *nlh;
320 struct ndmsg *ndm;
e4f67add 321 bool send_ip, send_eth;
d342894c 322
323 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
324 if (nlh == NULL)
325 return -EMSGSIZE;
326
327 ndm = nlmsg_data(nlh);
328 memset(ndm, 0, sizeof(*ndm));
e4f67add
DS
329
330 send_eth = send_ip = true;
331
332 if (type == RTM_GETNEIGH) {
333 ndm->ndm_family = AF_INET;
e4c7ed41 334 send_ip = !vxlan_addr_any(&rdst->remote_ip);
e4f67add
DS
335 send_eth = !is_zero_ether_addr(fdb->eth_addr);
336 } else
337 ndm->ndm_family = AF_BRIDGE;
d342894c 338 ndm->ndm_state = fdb->state;
339 ndm->ndm_ifindex = vxlan->dev->ifindex;
ae884082 340 ndm->ndm_flags = fdb->flags;
545469f7 341 ndm->ndm_type = RTN_UNICAST;
d342894c 342
193523bf 343 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
4967082b
ND
344 nla_put_s32(skb, NDA_LINK_NETNSID,
345 peernet2id(dev_net(vxlan->dev), vxlan->net)))
193523bf
ND
346 goto nla_put_failure;
347
e4f67add 348 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
d342894c 349 goto nla_put_failure;
350
e4c7ed41 351 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
6681712d
DS
352 goto nla_put_failure;
353
823aa873 354 if (rdst->remote_port && rdst->remote_port != vxlan->dst_port &&
6681712d
DS
355 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
356 goto nla_put_failure;
c7995c43 357 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
60d9d4c6 358 nla_put_u32(skb, NDA_VNI, rdst->remote_vni))
6681712d
DS
359 goto nla_put_failure;
360 if (rdst->remote_ifindex &&
361 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
d342894c 362 goto nla_put_failure;
363
364 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
365 ci.ndm_confirmed = 0;
366 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
367 ci.ndm_refcnt = 0;
368
369 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
370 goto nla_put_failure;
371
053c095a
JB
372 nlmsg_end(skb, nlh);
373 return 0;
d342894c 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 */
4967082b 388 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
d342894c 389 + nla_total_size(sizeof(struct nda_cacheinfo));
390}
391
9e4b93f9
ND
392static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
393 struct vxlan_rdst *rd, int type)
d342894c 394{
395 struct net *net = dev_net(vxlan->dev);
396 struct sk_buff *skb;
397 int err = -ENOBUFS;
398
399 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
400 if (skb == NULL)
401 goto errout;
402
9e4b93f9 403 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
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 428
9e4b93f9 429 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
e4f67add
DS
430}
431
432static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
433{
bb3fd687
SH
434 struct vxlan_fdb f = {
435 .state = NUD_STALE,
436 };
9e4b93f9 437 struct vxlan_rdst remote = { };
e4f67add 438
e4f67add
DS
439 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
440
9e4b93f9 441 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
e4f67add
DS
442}
443
d342894c 444/* Hash Ethernet address */
445static u32 eth_hash(const unsigned char *addr)
446{
447 u64 value = get_unaligned((u64 *)addr);
448
449 /* only want 6 bytes */
450#ifdef __BIG_ENDIAN
d342894c 451 value >>= 16;
321fb991 452#else
453 value <<= 16;
d342894c 454#endif
455 return hash_64(value, FDB_HASH_BITS);
456}
457
458/* Hash chain to use given mac address */
459static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
460 const u8 *mac)
461{
462 return &vxlan->fdb_head[eth_hash(mac)];
463}
464
465/* Look up Ethernet address in forwarding table */
014be2c8 466static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
d342894c 467 const u8 *mac)
d342894c 468{
469 struct hlist_head *head = vxlan_fdb_head(vxlan, mac);
470 struct vxlan_fdb *f;
d342894c 471
b67bfe0d 472 hlist_for_each_entry_rcu(f, head, hlist) {
7367d0b5 473 if (ether_addr_equal(mac, f->eth_addr))
d342894c 474 return f;
475 }
476
477 return NULL;
478}
479
014be2c8
SS
480static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
481 const u8 *mac)
482{
483 struct vxlan_fdb *f;
484
485 f = __vxlan_find_mac(vxlan, mac);
486 if (f)
487 f->used = jiffies;
488
489 return f;
490}
491
a5e7c10a
MR
492/* caller should hold vxlan->hash_lock */
493static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
e4c7ed41 494 union vxlan_addr *ip, __be16 port,
a5e7c10a 495 __u32 vni, __u32 ifindex)
6681712d 496{
3e61aa8f 497 struct vxlan_rdst *rd;
6681712d 498
3e61aa8f 499 list_for_each_entry(rd, &f->remotes, list) {
e4c7ed41 500 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
6681712d
DS
501 rd->remote_port == port &&
502 rd->remote_vni == vni &&
503 rd->remote_ifindex == ifindex)
a5e7c10a 504 return rd;
6681712d 505 }
3e61aa8f 506
a5e7c10a
MR
507 return NULL;
508}
509
906dc186
TR
510/* Replace destination of unicast mac */
511static int vxlan_fdb_replace(struct vxlan_fdb *f,
e4c7ed41 512 union vxlan_addr *ip, __be16 port, __u32 vni, __u32 ifindex)
906dc186
TR
513{
514 struct vxlan_rdst *rd;
515
516 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
517 if (rd)
518 return 0;
519
520 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
521 if (!rd)
522 return 0;
e4c7ed41 523 rd->remote_ip = *ip;
906dc186
TR
524 rd->remote_port = port;
525 rd->remote_vni = vni;
526 rd->remote_ifindex = ifindex;
527 return 1;
528}
529
a5e7c10a
MR
530/* Add/update destinations for multicast */
531static int vxlan_fdb_append(struct vxlan_fdb *f,
9e4b93f9
ND
532 union vxlan_addr *ip, __be16 port, __u32 vni,
533 __u32 ifindex, struct vxlan_rdst **rdp)
a5e7c10a
MR
534{
535 struct vxlan_rdst *rd;
536
537 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
538 if (rd)
539 return 0;
540
6681712d
DS
541 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
542 if (rd == NULL)
543 return -ENOBUFS;
e4c7ed41 544 rd->remote_ip = *ip;
6681712d
DS
545 rd->remote_port = port;
546 rd->remote_vni = vni;
547 rd->remote_ifindex = ifindex;
3e61aa8f
SH
548
549 list_add_tail_rcu(&rd->list, &f->remotes);
550
9e4b93f9 551 *rdp = rd;
6681712d
DS
552 return 1;
553}
554
dfd8645e
TH
555static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
556 unsigned int off,
557 struct vxlanhdr *vh, size_t hdrlen,
558 u32 data)
559{
560 size_t start, offset, plen;
dfd8645e
TH
561
562 if (skb->remcsum_offload)
563 return vh;
564
565 if (!NAPI_GRO_CB(skb)->csum_valid)
566 return NULL;
567
568 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
569 offset = start + ((data & VXLAN_RCO_UDP) ?
570 offsetof(struct udphdr, check) :
571 offsetof(struct tcphdr, check));
572
573 plen = hdrlen + offset + sizeof(u16);
574
575 /* Pull checksum that will be written */
576 if (skb_gro_header_hard(skb, off + plen)) {
577 vh = skb_gro_header_slow(skb, off + plen, off);
578 if (!vh)
579 return NULL;
580 }
581
dcdc8994 582 skb_gro_remcsum_process(skb, (void *)vh + hdrlen, start, offset);
dfd8645e
TH
583
584 skb->remcsum_offload = 1;
585
586 return vh;
587}
588
a2b12f3c
TH
589static struct sk_buff **vxlan_gro_receive(struct sk_buff **head,
590 struct sk_buff *skb,
591 struct udp_offload *uoff)
dc01e7d3
OG
592{
593 struct sk_buff *p, **pp = NULL;
594 struct vxlanhdr *vh, *vh2;
9b174d88 595 unsigned int hlen, off_vx;
dc01e7d3 596 int flush = 1;
dfd8645e
TH
597 struct vxlan_sock *vs = container_of(uoff, struct vxlan_sock,
598 udp_offloads);
599 u32 flags;
dc01e7d3
OG
600
601 off_vx = skb_gro_offset(skb);
602 hlen = off_vx + sizeof(*vh);
603 vh = skb_gro_header_fast(skb, off_vx);
604 if (skb_gro_header_hard(skb, hlen)) {
605 vh = skb_gro_header_slow(skb, hlen, off_vx);
606 if (unlikely(!vh))
607 goto out;
608 }
dc01e7d3 609
dfd8645e
TH
610 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
611 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
612
613 flags = ntohl(vh->vx_flags);
614
615 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
616 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
617 ntohl(vh->vx_vni));
618
619 if (!vh)
620 goto out;
621 }
622
dc01e7d3
OG
623 flush = 0;
624
625 for (p = *head; p; p = p->next) {
626 if (!NAPI_GRO_CB(p)->same_flow)
627 continue;
628
629 vh2 = (struct vxlanhdr *)(p->data + off_vx);
3511494c
TG
630 if (vh->vx_flags != vh2->vx_flags ||
631 vh->vx_vni != vh2->vx_vni) {
dc01e7d3
OG
632 NAPI_GRO_CB(p)->same_flow = 0;
633 continue;
634 }
dc01e7d3
OG
635 }
636
9b174d88 637 pp = eth_gro_receive(head, skb);
dc01e7d3 638
dc01e7d3
OG
639out:
640 NAPI_GRO_CB(skb)->flush |= flush;
641
642 return pp;
643}
644
a2b12f3c
TH
645static int vxlan_gro_complete(struct sk_buff *skb, int nhoff,
646 struct udp_offload *uoff)
dc01e7d3 647{
cfdf1e1b
JG
648 udp_tunnel_gro_complete(skb, nhoff);
649
9b174d88 650 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
dc01e7d3
OG
651}
652
53cf5275 653/* Notify netdevs that UDP port started listening */
dc01e7d3 654static void vxlan_notify_add_rx_port(struct vxlan_sock *vs)
53cf5275
JG
655{
656 struct net_device *dev;
dc01e7d3 657 struct sock *sk = vs->sock->sk;
53cf5275
JG
658 struct net *net = sock_net(sk);
659 sa_family_t sa_family = sk->sk_family;
35e42379 660 __be16 port = inet_sk(sk)->inet_sport;
dc01e7d3
OG
661 int err;
662
663 if (sa_family == AF_INET) {
664 err = udp_add_offload(&vs->udp_offloads);
665 if (err)
666 pr_warn("vxlan: udp_add_offload failed with status %d\n", err);
667 }
53cf5275
JG
668
669 rcu_read_lock();
670 for_each_netdev_rcu(net, dev) {
671 if (dev->netdev_ops->ndo_add_vxlan_port)
672 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
673 port);
674 }
675 rcu_read_unlock();
676}
677
678/* Notify netdevs that UDP port is no more listening */
dc01e7d3 679static void vxlan_notify_del_rx_port(struct vxlan_sock *vs)
53cf5275
JG
680{
681 struct net_device *dev;
dc01e7d3 682 struct sock *sk = vs->sock->sk;
53cf5275
JG
683 struct net *net = sock_net(sk);
684 sa_family_t sa_family = sk->sk_family;
35e42379 685 __be16 port = inet_sk(sk)->inet_sport;
53cf5275
JG
686
687 rcu_read_lock();
688 for_each_netdev_rcu(net, dev) {
689 if (dev->netdev_ops->ndo_del_vxlan_port)
690 dev->netdev_ops->ndo_del_vxlan_port(dev, sa_family,
691 port);
692 }
693 rcu_read_unlock();
dc01e7d3
OG
694
695 if (sa_family == AF_INET)
696 udp_del_offload(&vs->udp_offloads);
53cf5275
JG
697}
698
d342894c 699/* Add new entry to forwarding table -- assumes lock held */
700static int vxlan_fdb_create(struct vxlan_dev *vxlan,
e4c7ed41 701 const u8 *mac, union vxlan_addr *ip,
6681712d 702 __u16 state, __u16 flags,
73cf3317 703 __be16 port, __u32 vni, __u32 ifindex,
ae884082 704 __u8 ndm_flags)
d342894c 705{
9e4b93f9 706 struct vxlan_rdst *rd = NULL;
d342894c 707 struct vxlan_fdb *f;
708 int notify = 0;
709
014be2c8 710 f = __vxlan_find_mac(vxlan, mac);
d342894c 711 if (f) {
712 if (flags & NLM_F_EXCL) {
713 netdev_dbg(vxlan->dev,
714 "lost race to create %pM\n", mac);
715 return -EEXIST;
716 }
717 if (f->state != state) {
718 f->state = state;
719 f->updated = jiffies;
720 notify = 1;
721 }
ae884082
DS
722 if (f->flags != ndm_flags) {
723 f->flags = ndm_flags;
724 f->updated = jiffies;
725 notify = 1;
726 }
906dc186
TR
727 if ((flags & NLM_F_REPLACE)) {
728 /* Only change unicasts */
729 if (!(is_multicast_ether_addr(f->eth_addr) ||
730 is_zero_ether_addr(f->eth_addr))) {
731 int rc = vxlan_fdb_replace(f, ip, port, vni,
732 ifindex);
733
734 if (rc < 0)
735 return rc;
736 notify |= rc;
737 } else
738 return -EOPNOTSUPP;
739 }
6681712d 740 if ((flags & NLM_F_APPEND) &&
58e4c767
MR
741 (is_multicast_ether_addr(f->eth_addr) ||
742 is_zero_ether_addr(f->eth_addr))) {
9e4b93f9
ND
743 int rc = vxlan_fdb_append(f, ip, port, vni, ifindex,
744 &rd);
6681712d
DS
745
746 if (rc < 0)
747 return rc;
748 notify |= rc;
749 }
d342894c 750 } else {
751 if (!(flags & NLM_F_CREATE))
752 return -ENOENT;
753
754 if (vxlan->addrmax && vxlan->addrcnt >= vxlan->addrmax)
755 return -ENOSPC;
756
906dc186
TR
757 /* Disallow replace to add a multicast entry */
758 if ((flags & NLM_F_REPLACE) &&
759 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
760 return -EOPNOTSUPP;
761
e4c7ed41 762 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
d342894c 763 f = kmalloc(sizeof(*f), GFP_ATOMIC);
764 if (!f)
765 return -ENOMEM;
766
767 notify = 1;
d342894c 768 f->state = state;
ae884082 769 f->flags = ndm_flags;
d342894c 770 f->updated = f->used = jiffies;
3e61aa8f 771 INIT_LIST_HEAD(&f->remotes);
d342894c 772 memcpy(f->eth_addr, mac, ETH_ALEN);
773
9e4b93f9 774 vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
3e61aa8f 775
d342894c 776 ++vxlan->addrcnt;
777 hlist_add_head_rcu(&f->hlist,
778 vxlan_fdb_head(vxlan, mac));
779 }
780
9e4b93f9
ND
781 if (notify) {
782 if (rd == NULL)
783 rd = first_remote_rtnl(f);
784 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
785 }
d342894c 786
787 return 0;
788}
789
6706c82e 790static void vxlan_fdb_free(struct rcu_head *head)
6681712d
DS
791{
792 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
3e61aa8f 793 struct vxlan_rdst *rd, *nd;
6681712d 794
3e61aa8f 795 list_for_each_entry_safe(rd, nd, &f->remotes, list)
6681712d 796 kfree(rd);
6681712d
DS
797 kfree(f);
798}
799
d342894c 800static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
801{
802 netdev_dbg(vxlan->dev,
803 "delete %pM\n", f->eth_addr);
804
805 --vxlan->addrcnt;
9e4b93f9 806 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
d342894c 807
808 hlist_del_rcu(&f->hlist);
6681712d 809 call_rcu(&f->rcu, vxlan_fdb_free);
d342894c 810}
811
f0b074be 812static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
e4c7ed41 813 union vxlan_addr *ip, __be16 *port, u32 *vni, u32 *ifindex)
d342894c 814{
6681712d 815 struct net *net = dev_net(vxlan->dev);
e4c7ed41 816 int err;
d342894c 817
f0b074be 818 if (tb[NDA_DST]) {
e4c7ed41
CW
819 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
820 if (err)
821 return err;
f0b074be 822 } else {
e4c7ed41
CW
823 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
824 if (remote->sa.sa_family == AF_INET) {
825 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
826 ip->sa.sa_family = AF_INET;
827#if IS_ENABLED(CONFIG_IPV6)
828 } else {
829 ip->sin6.sin6_addr = in6addr_any;
830 ip->sa.sa_family = AF_INET6;
831#endif
832 }
f0b074be 833 }
d342894c 834
6681712d 835 if (tb[NDA_PORT]) {
73cf3317 836 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
6681712d 837 return -EINVAL;
f0b074be
MR
838 *port = nla_get_be16(tb[NDA_PORT]);
839 } else {
840 *port = vxlan->dst_port;
841 }
6681712d
DS
842
843 if (tb[NDA_VNI]) {
844 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
845 return -EINVAL;
f0b074be
MR
846 *vni = nla_get_u32(tb[NDA_VNI]);
847 } else {
848 *vni = vxlan->default_dst.remote_vni;
849 }
6681712d
DS
850
851 if (tb[NDA_IFINDEX]) {
5abb0029 852 struct net_device *tdev;
6681712d
DS
853
854 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
855 return -EINVAL;
f0b074be 856 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
73763949 857 tdev = __dev_get_by_index(net, *ifindex);
5abb0029 858 if (!tdev)
6681712d 859 return -EADDRNOTAVAIL;
f0b074be
MR
860 } else {
861 *ifindex = 0;
862 }
863
864 return 0;
865}
866
867/* Add static entry (via netlink) */
868static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
869 struct net_device *dev,
f6f6424b 870 const unsigned char *addr, u16 vid, u16 flags)
f0b074be
MR
871{
872 struct vxlan_dev *vxlan = netdev_priv(dev);
873 /* struct net *net = dev_net(vxlan->dev); */
e4c7ed41 874 union vxlan_addr ip;
f0b074be
MR
875 __be16 port;
876 u32 vni, ifindex;
877 int err;
878
879 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
880 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
881 ndm->ndm_state);
882 return -EINVAL;
883 }
884
885 if (tb[NDA_DST] == NULL)
886 return -EINVAL;
887
888 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
889 if (err)
890 return err;
6681712d 891
5933a7bb
MR
892 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
893 return -EAFNOSUPPORT;
894
d342894c 895 spin_lock_bh(&vxlan->hash_lock);
e4c7ed41 896 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
73cf3317 897 port, vni, ifindex, ndm->ndm_flags);
d342894c 898 spin_unlock_bh(&vxlan->hash_lock);
899
900 return err;
901}
902
903/* Delete entry (via netlink) */
1690be63
VY
904static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
905 struct net_device *dev,
f6f6424b 906 const unsigned char *addr, u16 vid)
d342894c 907{
908 struct vxlan_dev *vxlan = netdev_priv(dev);
909 struct vxlan_fdb *f;
bc7892ba 910 struct vxlan_rdst *rd = NULL;
e4c7ed41 911 union vxlan_addr ip;
bc7892ba
MR
912 __be16 port;
913 u32 vni, ifindex;
914 int err;
915
916 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &vni, &ifindex);
917 if (err)
918 return err;
919
920 err = -ENOENT;
d342894c 921
922 spin_lock_bh(&vxlan->hash_lock);
923 f = vxlan_find_mac(vxlan, addr);
bc7892ba
MR
924 if (!f)
925 goto out;
926
e4c7ed41
CW
927 if (!vxlan_addr_any(&ip)) {
928 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
bc7892ba
MR
929 if (!rd)
930 goto out;
931 }
932
933 err = 0;
934
935 /* remove a destination if it's not the only one on the list,
936 * otherwise destroy the fdb entry
937 */
938 if (rd && !list_is_singular(&f->remotes)) {
939 list_del_rcu(&rd->list);
9e4b93f9 940 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
dcdc7a59 941 kfree_rcu(rd, rcu);
bc7892ba 942 goto out;
d342894c 943 }
bc7892ba
MR
944
945 vxlan_fdb_destroy(vxlan, f);
946
947out:
d342894c 948 spin_unlock_bh(&vxlan->hash_lock);
949
950 return err;
951}
952
953/* Dump forwarding table */
954static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
5d5eacb3
JHS
955 struct net_device *dev,
956 struct net_device *filter_dev, int idx)
d342894c 957{
958 struct vxlan_dev *vxlan = netdev_priv(dev);
959 unsigned int h;
960
961 for (h = 0; h < FDB_HASH_SIZE; ++h) {
962 struct vxlan_fdb *f;
d342894c 963 int err;
964
b67bfe0d 965 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
6681712d 966 struct vxlan_rdst *rd;
6681712d 967
3e61aa8f
SH
968 if (idx < cb->args[0])
969 goto skip;
970
971 list_for_each_entry_rcu(rd, &f->remotes, list) {
6681712d
DS
972 err = vxlan_fdb_info(skb, vxlan, f,
973 NETLINK_CB(cb->skb).portid,
974 cb->nlh->nlmsg_seq,
975 RTM_NEWNEIGH,
976 NLM_F_MULTI, rd);
977 if (err < 0)
3e61aa8f 978 goto out;
6681712d 979 }
3e61aa8f
SH
980skip:
981 ++idx;
d342894c 982 }
983 }
3e61aa8f 984out:
d342894c 985 return idx;
986}
987
988/* Watch incoming packets to learn mapping between Ethernet address
989 * and Tunnel endpoint.
26a41ae6 990 * Return true if packet is bogus and should be droppped.
d342894c 991 */
26a41ae6 992static bool vxlan_snoop(struct net_device *dev,
e4c7ed41 993 union vxlan_addr *src_ip, const u8 *src_mac)
d342894c 994{
995 struct vxlan_dev *vxlan = netdev_priv(dev);
996 struct vxlan_fdb *f;
d342894c 997
998 f = vxlan_find_mac(vxlan, src_mac);
999 if (likely(f)) {
5ca5461c 1000 struct vxlan_rdst *rdst = first_remote_rcu(f);
3e61aa8f 1001
e4c7ed41 1002 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
26a41ae6 1003 return false;
1004
1005 /* Don't migrate static entries, drop packets */
eb064c3b 1006 if (f->state & NUD_NOARP)
26a41ae6 1007 return true;
d342894c 1008
1009 if (net_ratelimit())
1010 netdev_info(dev,
e4c7ed41 1011 "%pM migrated from %pIS to %pIS\n",
3e61aa8f 1012 src_mac, &rdst->remote_ip, &src_ip);
d342894c 1013
e4c7ed41 1014 rdst->remote_ip = *src_ip;
d342894c 1015 f->updated = jiffies;
9e4b93f9 1016 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
d342894c 1017 } else {
1018 /* learned new entry */
1019 spin_lock(&vxlan->hash_lock);
3bf74b1a 1020
1021 /* close off race between vxlan_flush and incoming packets */
1022 if (netif_running(dev))
1023 vxlan_fdb_create(vxlan, src_mac, src_ip,
1024 NUD_REACHABLE,
1025 NLM_F_EXCL|NLM_F_CREATE,
1026 vxlan->dst_port,
1027 vxlan->default_dst.remote_vni,
1028 0, NTF_SELF);
d342894c 1029 spin_unlock(&vxlan->hash_lock);
1030 }
26a41ae6 1031
1032 return false;
d342894c 1033}
1034
d342894c 1035/* See if multicast group is already in use by other ID */
95ab0991 1036static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
d342894c 1037{
553675fb 1038 struct vxlan_dev *vxlan;
d342894c 1039
95ab0991
G
1040 /* The vxlan_sock is only used by dev, leaving group has
1041 * no effect on other vxlan devices.
1042 */
1043 if (atomic_read(&dev->vn_sock->refcnt) == 1)
1044 return false;
1045
553675fb 1046 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
95ab0991 1047 if (!netif_running(vxlan->dev) || vxlan == dev)
553675fb 1048 continue;
d342894c 1049
95ab0991
G
1050 if (vxlan->vn_sock != dev->vn_sock)
1051 continue;
1052
1053 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1054 &dev->default_dst.remote_ip))
1055 continue;
1056
1057 if (vxlan->default_dst.remote_ifindex !=
1058 dev->default_dst.remote_ifindex)
1059 continue;
1060
1061 return true;
553675fb 1062 }
d342894c 1063
1064 return false;
1065}
1066
7c47cedf 1067static void vxlan_sock_hold(struct vxlan_sock *vs)
d342894c 1068{
7c47cedf
SH
1069 atomic_inc(&vs->refcnt);
1070}
d342894c 1071
012a5729 1072void vxlan_sock_release(struct vxlan_sock *vs)
7c47cedf 1073{
53cf5275
JG
1074 struct sock *sk = vs->sock->sk;
1075 struct net *net = sock_net(sk);
1076 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
012a5729 1077
7c47cedf
SH
1078 if (!atomic_dec_and_test(&vs->refcnt))
1079 return;
d342894c 1080
1c51a915 1081 spin_lock(&vn->sock_lock);
7c47cedf 1082 hlist_del_rcu(&vs->hlist);
dc01e7d3 1083 vxlan_notify_del_rx_port(vs);
1c51a915
SH
1084 spin_unlock(&vn->sock_lock);
1085
7c47cedf 1086 queue_work(vxlan_wq, &vs->del_work);
d342894c 1087}
012a5729 1088EXPORT_SYMBOL_GPL(vxlan_sock_release);
d342894c 1089
3fc2de2f 1090/* Callback to update multicast group membership when first VNI on
1091 * multicast asddress is brought up
1092 * Done as workqueue because ip_mc_join_group acquires RTNL.
7c47cedf 1093 */
3fc2de2f 1094static void vxlan_igmp_join(struct work_struct *work)
d342894c 1095{
3fc2de2f 1096 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_join);
7c47cedf
SH
1097 struct vxlan_sock *vs = vxlan->vn_sock;
1098 struct sock *sk = vs->sock->sk;
e4c7ed41
CW
1099 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1100 int ifindex = vxlan->default_dst.remote_ifindex;
d342894c 1101
d342894c 1102 lock_sock(sk);
e4c7ed41
CW
1103 if (ip->sa.sa_family == AF_INET) {
1104 struct ip_mreqn mreq = {
1105 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1106 .imr_ifindex = ifindex,
1107 };
1108
1109 ip_mc_join_group(sk, &mreq);
1110#if IS_ENABLED(CONFIG_IPV6)
1111 } else {
1112 ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1113 &ip->sin6.sin6_addr);
1114#endif
1115 }
3fc2de2f 1116 release_sock(sk);
1117
012a5729 1118 vxlan_sock_release(vs);
3fc2de2f 1119 dev_put(vxlan->dev);
1120}
1121
1122/* Inverse of vxlan_igmp_join when last VNI is brought down */
1123static void vxlan_igmp_leave(struct work_struct *work)
1124{
1125 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, igmp_leave);
3fc2de2f 1126 struct vxlan_sock *vs = vxlan->vn_sock;
1127 struct sock *sk = vs->sock->sk;
e4c7ed41
CW
1128 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1129 int ifindex = vxlan->default_dst.remote_ifindex;
3fc2de2f 1130
1131 lock_sock(sk);
e4c7ed41
CW
1132 if (ip->sa.sa_family == AF_INET) {
1133 struct ip_mreqn mreq = {
1134 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1135 .imr_ifindex = ifindex,
1136 };
1137
1138 ip_mc_leave_group(sk, &mreq);
1139#if IS_ENABLED(CONFIG_IPV6)
1140 } else {
1141 ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1142 &ip->sin6.sin6_addr);
1143#endif
1144 }
1145
d342894c 1146 release_sock(sk);
d342894c 1147
012a5729 1148 vxlan_sock_release(vs);
7c47cedf 1149 dev_put(vxlan->dev);
d342894c 1150}
1151
dfd8645e
TH
1152static struct vxlanhdr *vxlan_remcsum(struct sk_buff *skb, struct vxlanhdr *vh,
1153 size_t hdrlen, u32 data)
1154{
1155 size_t start, offset, plen;
dfd8645e
TH
1156
1157 if (skb->remcsum_offload) {
1158 /* Already processed in GRO path */
1159 skb->remcsum_offload = 0;
1160 return vh;
1161 }
1162
1163 start = (data & VXLAN_RCO_MASK) << VXLAN_RCO_SHIFT;
1164 offset = start + ((data & VXLAN_RCO_UDP) ?
1165 offsetof(struct udphdr, check) :
1166 offsetof(struct tcphdr, check));
1167
1168 plen = hdrlen + offset + sizeof(u16);
1169
1170 if (!pskb_may_pull(skb, plen))
1171 return NULL;
1172
1173 vh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
1174
dcdc8994 1175 skb_remcsum_process(skb, (void *)vh + hdrlen, start, offset);
dfd8645e
TH
1176
1177 return vh;
1178}
1179
d342894c 1180/* Callback from net/ipv4/udp.c to receive packets */
1181static int vxlan_udp_encap_recv(struct sock *sk, struct sk_buff *skb)
1182{
5cfccc5a 1183 struct vxlan_sock *vs;
d342894c 1184 struct vxlanhdr *vxh;
3bf39475 1185 u32 flags, vni;
3511494c 1186 struct vxlan_metadata md = {0};
d342894c 1187
d342894c 1188 /* Need Vxlan and inner Ethernet header to be present */
7ce04758 1189 if (!pskb_may_pull(skb, VXLAN_HLEN))
d342894c 1190 goto error;
1191
7ce04758 1192 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
3bf39475
TH
1193 flags = ntohl(vxh->vx_flags);
1194 vni = ntohl(vxh->vx_vni);
1195
1196 if (flags & VXLAN_HF_VNI) {
1197 flags &= ~VXLAN_HF_VNI;
1198 } else {
1199 /* VNI flag always required to be set */
1200 goto bad_flags;
d342894c 1201 }
1202
5cfccc5a
PS
1203 if (iptunnel_pull_header(skb, VXLAN_HLEN, htons(ETH_P_TEB)))
1204 goto drop;
dfd8645e 1205 vxh = (struct vxlanhdr *)(udp_hdr(skb) + 1);
5cfccc5a 1206
559835ea 1207 vs = rcu_dereference_sk_user_data(sk);
5cfccc5a 1208 if (!vs)
d342894c 1209 goto drop;
d342894c 1210
dfd8645e
TH
1211 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
1212 vxh = vxlan_remcsum(skb, vxh, sizeof(struct vxlanhdr), vni);
1213 if (!vxh)
1214 goto drop;
1215
1216 flags &= ~VXLAN_HF_RCO;
1217 vni &= VXLAN_VID_MASK;
1218 }
1219
3511494c
TG
1220 /* For backwards compatibility, only allow reserved fields to be
1221 * used by VXLAN extensions if explicitly requested.
1222 */
1223 if ((flags & VXLAN_HF_GBP) && (vs->flags & VXLAN_F_GBP)) {
1224 struct vxlanhdr_gbp *gbp;
1225
1226 gbp = (struct vxlanhdr_gbp *)vxh;
1227 md.gbp = ntohs(gbp->policy_id);
1228
1229 if (gbp->dont_learn)
1230 md.gbp |= VXLAN_GBP_DONT_LEARN;
1231
1232 if (gbp->policy_applied)
1233 md.gbp |= VXLAN_GBP_POLICY_APPLIED;
1234
1235 flags &= ~VXLAN_GBP_USED_BITS;
1236 }
1237
dfd8645e 1238 if (flags || (vni & ~VXLAN_VID_MASK)) {
3bf39475
TH
1239 /* If there are any unprocessed flags remaining treat
1240 * this as a malformed packet. This behavior diverges from
1241 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1242 * in reserved fields are to be ignored. The approach here
1243 * maintains compatbility with previous stack code, and also
1244 * is more robust and provides a little more security in
1245 * adding extensions to VXLAN.
1246 */
1247
1248 goto bad_flags;
1249 }
1250
3511494c
TG
1251 md.vni = vxh->vx_vni;
1252 vs->rcv(vs, skb, &md);
5cfccc5a
PS
1253 return 0;
1254
1255drop:
1256 /* Consume bad packet */
1257 kfree_skb(skb);
1258 return 0;
1259
3bf39475
TH
1260bad_flags:
1261 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1262 ntohl(vxh->vx_flags), ntohl(vxh->vx_vni));
1263
5cfccc5a
PS
1264error:
1265 /* Return non vxlan pkt */
1266 return 1;
1267}
1268
3511494c
TG
1269static void vxlan_rcv(struct vxlan_sock *vs, struct sk_buff *skb,
1270 struct vxlan_metadata *md)
5cfccc5a 1271{
e4c7ed41
CW
1272 struct iphdr *oip = NULL;
1273 struct ipv6hdr *oip6 = NULL;
5cfccc5a 1274 struct vxlan_dev *vxlan;
8f84985f 1275 struct pcpu_sw_netstats *stats;
e4c7ed41 1276 union vxlan_addr saddr;
5cfccc5a 1277 __u32 vni;
e4c7ed41
CW
1278 int err = 0;
1279 union vxlan_addr *remote_ip;
5cfccc5a 1280
3511494c 1281 vni = ntohl(md->vni) >> 8;
5cfccc5a
PS
1282 /* Is this VNI defined? */
1283 vxlan = vxlan_vs_find_vni(vs, vni);
1284 if (!vxlan)
d342894c 1285 goto drop;
d342894c 1286
e4c7ed41 1287 remote_ip = &vxlan->default_dst.remote_ip;
e4f67add 1288 skb_reset_mac_header(skb);
f01ec1c0 1289 skb_scrub_packet(skb, !net_eq(vxlan->net, dev_net(vxlan->dev)));
d342894c 1290 skb->protocol = eth_type_trans(skb, vxlan->dev);
f79b064c 1291 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
d342894c 1292
1293 /* Ignore packet loops (and multicast echo) */
7367d0b5 1294 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
d342894c 1295 goto drop;
1296
7ce04758 1297 /* Re-examine inner Ethernet packet */
e4c7ed41
CW
1298 if (remote_ip->sa.sa_family == AF_INET) {
1299 oip = ip_hdr(skb);
1300 saddr.sin.sin_addr.s_addr = oip->saddr;
1301 saddr.sa.sa_family = AF_INET;
1302#if IS_ENABLED(CONFIG_IPV6)
1303 } else {
1304 oip6 = ipv6_hdr(skb);
1305 saddr.sin6.sin6_addr = oip6->saddr;
1306 saddr.sa.sa_family = AF_INET6;
1307#endif
1308 }
1309
26a41ae6 1310 if ((vxlan->flags & VXLAN_F_LEARN) &&
e4c7ed41 1311 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source))
26a41ae6 1312 goto drop;
d342894c 1313
d342894c 1314 skb_reset_network_header(skb);
3511494c 1315 skb->mark = md->gbp;
0afb1666 1316
e4c7ed41
CW
1317 if (oip6)
1318 err = IP6_ECN_decapsulate(oip6, skb);
1319 if (oip)
1320 err = IP_ECN_decapsulate(oip, skb);
1321
d342894c 1322 if (unlikely(err)) {
e4c7ed41
CW
1323 if (log_ecn_error) {
1324 if (oip6)
1325 net_info_ratelimited("non-ECT from %pI6\n",
1326 &oip6->saddr);
1327 if (oip)
1328 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1329 &oip->saddr, oip->tos);
1330 }
d342894c 1331 if (err > 1) {
1332 ++vxlan->dev->stats.rx_frame_errors;
1333 ++vxlan->dev->stats.rx_errors;
1334 goto drop;
1335 }
1336 }
1337
e8171045 1338 stats = this_cpu_ptr(vxlan->dev->tstats);
d342894c 1339 u64_stats_update_begin(&stats->syncp);
1340 stats->rx_packets++;
1341 stats->rx_bytes += skb->len;
1342 u64_stats_update_end(&stats->syncp);
1343
1344 netif_rx(skb);
1345
5cfccc5a 1346 return;
d342894c 1347drop:
1348 /* Consume bad packet */
1349 kfree_skb(skb);
d342894c 1350}
1351
e4f67add
DS
1352static int arp_reduce(struct net_device *dev, struct sk_buff *skb)
1353{
1354 struct vxlan_dev *vxlan = netdev_priv(dev);
1355 struct arphdr *parp;
1356 u8 *arpptr, *sha;
1357 __be32 sip, tip;
1358 struct neighbour *n;
1359
1360 if (dev->flags & IFF_NOARP)
1361 goto out;
1362
1363 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1364 dev->stats.tx_dropped++;
1365 goto out;
1366 }
1367 parp = arp_hdr(skb);
1368
1369 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1370 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1371 parp->ar_pro != htons(ETH_P_IP) ||
1372 parp->ar_op != htons(ARPOP_REQUEST) ||
1373 parp->ar_hln != dev->addr_len ||
1374 parp->ar_pln != 4)
1375 goto out;
1376 arpptr = (u8 *)parp + sizeof(struct arphdr);
1377 sha = arpptr;
1378 arpptr += dev->addr_len; /* sha */
1379 memcpy(&sip, arpptr, sizeof(sip));
1380 arpptr += sizeof(sip);
1381 arpptr += dev->addr_len; /* tha */
1382 memcpy(&tip, arpptr, sizeof(tip));
1383
1384 if (ipv4_is_loopback(tip) ||
1385 ipv4_is_multicast(tip))
1386 goto out;
1387
1388 n = neigh_lookup(&arp_tbl, &tip, dev);
1389
1390 if (n) {
e4f67add
DS
1391 struct vxlan_fdb *f;
1392 struct sk_buff *reply;
1393
1394 if (!(n->nud_state & NUD_CONNECTED)) {
1395 neigh_release(n);
1396 goto out;
1397 }
1398
1399 f = vxlan_find_mac(vxlan, n->ha);
e4c7ed41 1400 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
e4f67add
DS
1401 /* bridge-local neighbor */
1402 neigh_release(n);
1403 goto out;
1404 }
1405
1406 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1407 n->ha, sha);
1408
1409 neigh_release(n);
1410
7346135d
DS
1411 if (reply == NULL)
1412 goto out;
1413
e4f67add
DS
1414 skb_reset_mac_header(reply);
1415 __skb_pull(reply, skb_network_offset(reply));
1416 reply->ip_summed = CHECKSUM_UNNECESSARY;
1417 reply->pkt_type = PACKET_HOST;
1418
1419 if (netif_rx_ni(reply) == NET_RX_DROP)
1420 dev->stats.rx_dropped++;
e4c7ed41
CW
1421 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1422 union vxlan_addr ipa = {
1423 .sin.sin_addr.s_addr = tip,
a45e92a5 1424 .sin.sin_family = AF_INET,
e4c7ed41
CW
1425 };
1426
1427 vxlan_ip_miss(dev, &ipa);
1428 }
e4f67add
DS
1429out:
1430 consume_skb(skb);
1431 return NETDEV_TX_OK;
1432}
1433
f564f45c 1434#if IS_ENABLED(CONFIG_IPV6)
4b29dba9
DS
1435static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1436 struct neighbour *n, bool isrouter)
1437{
1438 struct net_device *dev = request->dev;
1439 struct sk_buff *reply;
1440 struct nd_msg *ns, *na;
1441 struct ipv6hdr *pip6;
1442 u8 *daddr;
1443 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1444 int ns_olen;
1445 int i, len;
1446
1447 if (dev == NULL)
1448 return NULL;
1449
1450 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1451 sizeof(*na) + na_olen + dev->needed_tailroom;
1452 reply = alloc_skb(len, GFP_ATOMIC);
1453 if (reply == NULL)
1454 return NULL;
1455
1456 reply->protocol = htons(ETH_P_IPV6);
1457 reply->dev = dev;
1458 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1459 skb_push(reply, sizeof(struct ethhdr));
1460 skb_set_mac_header(reply, 0);
1461
1462 ns = (struct nd_msg *)skb_transport_header(request);
1463
1464 daddr = eth_hdr(request)->h_source;
1465 ns_olen = request->len - skb_transport_offset(request) - sizeof(*ns);
1466 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1467 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1468 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1469 break;
1470 }
1471 }
1472
1473 /* Ethernet header */
1474 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1475 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1476 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1477 reply->protocol = htons(ETH_P_IPV6);
1478
1479 skb_pull(reply, sizeof(struct ethhdr));
1480 skb_set_network_header(reply, 0);
1481 skb_put(reply, sizeof(struct ipv6hdr));
1482
1483 /* IPv6 header */
1484
1485 pip6 = ipv6_hdr(reply);
1486 memset(pip6, 0, sizeof(struct ipv6hdr));
1487 pip6->version = 6;
1488 pip6->priority = ipv6_hdr(request)->priority;
1489 pip6->nexthdr = IPPROTO_ICMPV6;
1490 pip6->hop_limit = 255;
1491 pip6->daddr = ipv6_hdr(request)->saddr;
1492 pip6->saddr = *(struct in6_addr *)n->primary_key;
1493
1494 skb_pull(reply, sizeof(struct ipv6hdr));
1495 skb_set_transport_header(reply, 0);
1496
1497 na = (struct nd_msg *)skb_put(reply, sizeof(*na) + na_olen);
1498
1499 /* Neighbor Advertisement */
1500 memset(na, 0, sizeof(*na)+na_olen);
1501 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1502 na->icmph.icmp6_router = isrouter;
1503 na->icmph.icmp6_override = 1;
1504 na->icmph.icmp6_solicited = 1;
1505 na->target = ns->target;
1506 ether_addr_copy(&na->opt[2], n->ha);
1507 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1508 na->opt[1] = na_olen >> 3;
1509
1510 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1511 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1512 csum_partial(na, sizeof(*na)+na_olen, 0));
1513
1514 pip6->payload_len = htons(sizeof(*na)+na_olen);
1515
1516 skb_push(reply, sizeof(struct ipv6hdr));
1517
1518 reply->ip_summed = CHECKSUM_UNNECESSARY;
1519
1520 return reply;
1521}
1522
f564f45c
CW
1523static int neigh_reduce(struct net_device *dev, struct sk_buff *skb)
1524{
1525 struct vxlan_dev *vxlan = netdev_priv(dev);
4b29dba9 1526 struct nd_msg *msg;
f564f45c
CW
1527 const struct ipv6hdr *iphdr;
1528 const struct in6_addr *saddr, *daddr;
4b29dba9
DS
1529 struct neighbour *n;
1530 struct inet6_dev *in6_dev;
f564f45c
CW
1531
1532 in6_dev = __in6_dev_get(dev);
1533 if (!in6_dev)
1534 goto out;
1535
f564f45c
CW
1536 iphdr = ipv6_hdr(skb);
1537 saddr = &iphdr->saddr;
1538 daddr = &iphdr->daddr;
1539
f564f45c
CW
1540 msg = (struct nd_msg *)skb_transport_header(skb);
1541 if (msg->icmph.icmp6_code != 0 ||
1542 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1543 goto out;
1544
4b29dba9
DS
1545 if (ipv6_addr_loopback(daddr) ||
1546 ipv6_addr_is_multicast(&msg->target))
1547 goto out;
1548
1549 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
f564f45c
CW
1550
1551 if (n) {
1552 struct vxlan_fdb *f;
4b29dba9 1553 struct sk_buff *reply;
f564f45c
CW
1554
1555 if (!(n->nud_state & NUD_CONNECTED)) {
1556 neigh_release(n);
1557 goto out;
1558 }
1559
1560 f = vxlan_find_mac(vxlan, n->ha);
1561 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1562 /* bridge-local neighbor */
1563 neigh_release(n);
1564 goto out;
1565 }
1566
4b29dba9
DS
1567 reply = vxlan_na_create(skb, n,
1568 !!(f ? f->flags & NTF_ROUTER : 0));
1569
f564f45c 1570 neigh_release(n);
4b29dba9
DS
1571
1572 if (reply == NULL)
1573 goto out;
1574
1575 if (netif_rx_ni(reply) == NET_RX_DROP)
1576 dev->stats.rx_dropped++;
1577
f564f45c 1578 } else if (vxlan->flags & VXLAN_F_L3MISS) {
4b29dba9
DS
1579 union vxlan_addr ipa = {
1580 .sin6.sin6_addr = msg->target,
a45e92a5 1581 .sin6.sin6_family = AF_INET6,
4b29dba9
DS
1582 };
1583
f564f45c
CW
1584 vxlan_ip_miss(dev, &ipa);
1585 }
1586
1587out:
1588 consume_skb(skb);
1589 return NETDEV_TX_OK;
1590}
1591#endif
1592
e4f67add
DS
1593static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1594{
1595 struct vxlan_dev *vxlan = netdev_priv(dev);
1596 struct neighbour *n;
e4f67add
DS
1597
1598 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1599 return false;
1600
1601 n = NULL;
1602 switch (ntohs(eth_hdr(skb)->h_proto)) {
1603 case ETH_P_IP:
e15a00aa
CW
1604 {
1605 struct iphdr *pip;
1606
e4f67add
DS
1607 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1608 return false;
1609 pip = ip_hdr(skb);
1610 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
e4c7ed41
CW
1611 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1612 union vxlan_addr ipa = {
1613 .sin.sin_addr.s_addr = pip->daddr,
a45e92a5 1614 .sin.sin_family = AF_INET,
e4c7ed41
CW
1615 };
1616
1617 vxlan_ip_miss(dev, &ipa);
1618 return false;
1619 }
1620
e4f67add 1621 break;
e15a00aa
CW
1622 }
1623#if IS_ENABLED(CONFIG_IPV6)
1624 case ETH_P_IPV6:
1625 {
1626 struct ipv6hdr *pip6;
1627
1628 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1629 return false;
1630 pip6 = ipv6_hdr(skb);
1631 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1632 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1633 union vxlan_addr ipa = {
1634 .sin6.sin6_addr = pip6->daddr,
a45e92a5 1635 .sin6.sin6_family = AF_INET6,
e15a00aa
CW
1636 };
1637
1638 vxlan_ip_miss(dev, &ipa);
1639 return false;
1640 }
1641
1642 break;
1643 }
1644#endif
e4f67add
DS
1645 default:
1646 return false;
1647 }
1648
1649 if (n) {
1650 bool diff;
1651
7367d0b5 1652 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
e4f67add
DS
1653 if (diff) {
1654 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1655 dev->addr_len);
1656 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1657 }
1658 neigh_release(n);
1659 return diff;
e4c7ed41
CW
1660 }
1661
e4f67add
DS
1662 return false;
1663}
1664
af33c1ad 1665static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
3511494c
TG
1666 struct vxlan_metadata *md)
1667{
1668 struct vxlanhdr_gbp *gbp;
1669
1670 gbp = (struct vxlanhdr_gbp *)vxh;
1671 vxh->vx_flags |= htonl(VXLAN_HF_GBP);
1672
1673 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1674 gbp->dont_learn = 1;
1675
1676 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1677 gbp->policy_applied = 1;
1678
1679 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1680}
1681
e4c7ed41 1682#if IS_ENABLED(CONFIG_IPV6)
af33c1ad 1683static int vxlan6_xmit_skb(struct dst_entry *dst, struct sk_buff *skb,
e4c7ed41
CW
1684 struct net_device *dev, struct in6_addr *saddr,
1685 struct in6_addr *daddr, __u8 prio, __u8 ttl,
3511494c 1686 __be16 src_port, __be16 dst_port,
af33c1ad 1687 struct vxlan_metadata *md, bool xnet, u32 vxflags)
e4c7ed41 1688{
e4c7ed41 1689 struct vxlanhdr *vxh;
e4c7ed41
CW
1690 int min_headroom;
1691 int err;
af33c1ad 1692 bool udp_sum = !(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX);
dfd8645e
TH
1693 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1694 u16 hdrlen = sizeof(struct vxlanhdr);
1695
af33c1ad 1696 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
dfd8645e
TH
1697 skb->ip_summed == CHECKSUM_PARTIAL) {
1698 int csum_start = skb_checksum_start_offset(skb);
1699
1700 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1701 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1702 (skb->csum_offset == offsetof(struct udphdr, check) ||
1703 skb->csum_offset == offsetof(struct tcphdr, check))) {
1704 udp_sum = false;
1705 type |= SKB_GSO_TUNNEL_REMCSUM;
1706 }
1707 }
e4c7ed41 1708
dfd8645e 1709 skb = iptunnel_handle_offloads(skb, udp_sum, type);
74f47278
PS
1710 if (IS_ERR(skb)) {
1711 err = -EINVAL;
1712 goto err;
1713 }
e4c7ed41 1714
f01ec1c0 1715 skb_scrub_packet(skb, xnet);
963a88b3 1716
e4c7ed41
CW
1717 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
1718 + VXLAN_HLEN + sizeof(struct ipv6hdr)
df8a39de 1719 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
e4c7ed41
CW
1720
1721 /* Need space for new headers (invalidates iph ptr) */
1722 err = skb_cow_head(skb, min_headroom);
74f47278
PS
1723 if (unlikely(err)) {
1724 kfree_skb(skb);
1725 goto err;
1726 }
e4c7ed41 1727
5968250c 1728 skb = vlan_hwaccel_push_inside(skb);
74f47278
PS
1729 if (WARN_ON(!skb)) {
1730 err = -ENOMEM;
1731 goto err;
1732 }
e4c7ed41
CW
1733
1734 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
3bf39475 1735 vxh->vx_flags = htonl(VXLAN_HF_VNI);
3511494c 1736 vxh->vx_vni = md->vni;
e4c7ed41 1737
dfd8645e
TH
1738 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1739 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1740 VXLAN_RCO_SHIFT;
1741
1742 if (skb->csum_offset == offsetof(struct udphdr, check))
1743 data |= VXLAN_RCO_UDP;
1744
1745 vxh->vx_vni |= htonl(data);
1746 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1747
1748 if (!skb_is_gso(skb)) {
1749 skb->ip_summed = CHECKSUM_NONE;
1750 skb->encapsulation = 0;
1751 }
1752 }
1753
af33c1ad
TH
1754 if (vxflags & VXLAN_F_GBP)
1755 vxlan_build_gbp_hdr(vxh, vxflags, md);
3511494c 1756
996c9fd1
TH
1757 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1758
d998f8ef
TH
1759 udp_tunnel6_xmit_skb(dst, skb, dev, saddr, daddr, prio,
1760 ttl, src_port, dst_port,
af33c1ad 1761 !!(vxflags & VXLAN_F_UDP_ZERO_CSUM6_TX));
e4c7ed41 1762 return 0;
74f47278
PS
1763err:
1764 dst_release(dst);
1765 return err;
e4c7ed41
CW
1766}
1767#endif
1768
af33c1ad 1769int vxlan_xmit_skb(struct rtable *rt, struct sk_buff *skb,
49560532 1770 __be32 src, __be32 dst, __u8 tos, __u8 ttl, __be16 df,
3511494c 1771 __be16 src_port, __be16 dst_port,
af33c1ad 1772 struct vxlan_metadata *md, bool xnet, u32 vxflags)
49560532
PS
1773{
1774 struct vxlanhdr *vxh;
649c5b8b 1775 int min_headroom;
49560532 1776 int err;
af33c1ad 1777 bool udp_sum = !!(vxflags & VXLAN_F_UDP_CSUM);
dfd8645e
TH
1778 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
1779 u16 hdrlen = sizeof(struct vxlanhdr);
1780
af33c1ad 1781 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
dfd8645e
TH
1782 skb->ip_summed == CHECKSUM_PARTIAL) {
1783 int csum_start = skb_checksum_start_offset(skb);
1784
1785 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1786 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1787 (skb->csum_offset == offsetof(struct udphdr, check) ||
1788 skb->csum_offset == offsetof(struct tcphdr, check))) {
1789 udp_sum = false;
1790 type |= SKB_GSO_TUNNEL_REMCSUM;
1791 }
1792 }
49560532 1793
dfd8645e 1794 skb = iptunnel_handle_offloads(skb, udp_sum, type);
359a0ea9 1795 if (IS_ERR(skb))
74f47278 1796 return PTR_ERR(skb);
49560532 1797
649c5b8b 1798 min_headroom = LL_RESERVED_SPACE(rt->dst.dev) + rt->dst.header_len
1eaa8178 1799 + VXLAN_HLEN + sizeof(struct iphdr)
df8a39de 1800 + (skb_vlan_tag_present(skb) ? VLAN_HLEN : 0);
649c5b8b
PS
1801
1802 /* Need space for new headers (invalidates iph ptr) */
1803 err = skb_cow_head(skb, min_headroom);
74f47278
PS
1804 if (unlikely(err)) {
1805 kfree_skb(skb);
649c5b8b 1806 return err;
74f47278 1807 }
649c5b8b 1808
5968250c
JP
1809 skb = vlan_hwaccel_push_inside(skb);
1810 if (WARN_ON(!skb))
1811 return -ENOMEM;
1eaa8178 1812
49560532 1813 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
3bf39475 1814 vxh->vx_flags = htonl(VXLAN_HF_VNI);
3511494c 1815 vxh->vx_vni = md->vni;
49560532 1816
dfd8645e
TH
1817 if (type & SKB_GSO_TUNNEL_REMCSUM) {
1818 u32 data = (skb_checksum_start_offset(skb) - hdrlen) >>
1819 VXLAN_RCO_SHIFT;
1820
1821 if (skb->csum_offset == offsetof(struct udphdr, check))
1822 data |= VXLAN_RCO_UDP;
1823
1824 vxh->vx_vni |= htonl(data);
1825 vxh->vx_flags |= htonl(VXLAN_HF_RCO);
1826
1827 if (!skb_is_gso(skb)) {
1828 skb->ip_summed = CHECKSUM_NONE;
1829 skb->encapsulation = 0;
1830 }
1831 }
1832
af33c1ad
TH
1833 if (vxflags & VXLAN_F_GBP)
1834 vxlan_build_gbp_hdr(vxh, vxflags, md);
3511494c 1835
996c9fd1
TH
1836 skb_set_inner_protocol(skb, htons(ETH_P_TEB));
1837
d998f8ef
TH
1838 return udp_tunnel_xmit_skb(rt, skb, src, dst, tos,
1839 ttl, df, src_port, dst_port, xnet,
af33c1ad 1840 !(vxflags & VXLAN_F_UDP_CSUM));
49560532
PS
1841}
1842EXPORT_SYMBOL_GPL(vxlan_xmit_skb);
1843
9dcc71e1
SS
1844/* Bypass encapsulation if the destination is local */
1845static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
1846 struct vxlan_dev *dst_vxlan)
1847{
8f84985f 1848 struct pcpu_sw_netstats *tx_stats, *rx_stats;
e4c7ed41
CW
1849 union vxlan_addr loopback;
1850 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
ce6502a8
LR
1851 struct net_device *dev = skb->dev;
1852 int len = skb->len;
9dcc71e1 1853
8f84985f
LR
1854 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1855 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
9dcc71e1
SS
1856 skb->pkt_type = PACKET_HOST;
1857 skb->encapsulation = 0;
1858 skb->dev = dst_vxlan->dev;
1859 __skb_pull(skb, skb_network_offset(skb));
1860
e4c7ed41
CW
1861 if (remote_ip->sa.sa_family == AF_INET) {
1862 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1863 loopback.sa.sa_family = AF_INET;
1864#if IS_ENABLED(CONFIG_IPV6)
1865 } else {
1866 loopback.sin6.sin6_addr = in6addr_loopback;
1867 loopback.sa.sa_family = AF_INET6;
1868#endif
1869 }
1870
9dcc71e1 1871 if (dst_vxlan->flags & VXLAN_F_LEARN)
e4c7ed41 1872 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source);
9dcc71e1
SS
1873
1874 u64_stats_update_begin(&tx_stats->syncp);
1875 tx_stats->tx_packets++;
ce6502a8 1876 tx_stats->tx_bytes += len;
9dcc71e1
SS
1877 u64_stats_update_end(&tx_stats->syncp);
1878
1879 if (netif_rx(skb) == NET_RX_SUCCESS) {
1880 u64_stats_update_begin(&rx_stats->syncp);
1881 rx_stats->rx_packets++;
ce6502a8 1882 rx_stats->rx_bytes += len;
9dcc71e1
SS
1883 u64_stats_update_end(&rx_stats->syncp);
1884 } else {
ce6502a8 1885 dev->stats.rx_dropped++;
9dcc71e1
SS
1886 }
1887}
1888
4ad16930
SH
1889static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
1890 struct vxlan_rdst *rdst, bool did_rsc)
d342894c 1891{
1892 struct vxlan_dev *vxlan = netdev_priv(dev);
e4c7ed41 1893 struct rtable *rt = NULL;
d342894c 1894 const struct iphdr *old_iph;
d342894c 1895 struct flowi4 fl4;
e4c7ed41 1896 union vxlan_addr *dst;
3511494c 1897 struct vxlan_metadata md;
e4c7ed41 1898 __be16 src_port = 0, dst_port;
234f5b73 1899 u32 vni;
d342894c 1900 __be16 df = 0;
1901 __u8 tos, ttl;
0e6fbc5b 1902 int err;
d342894c 1903
823aa873 1904 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->dst_port;
6681712d 1905 vni = rdst->remote_vni;
e4c7ed41 1906 dst = &rdst->remote_ip;
e4f67add 1907
e4c7ed41 1908 if (vxlan_addr_any(dst)) {
e4f67add 1909 if (did_rsc) {
e4f67add 1910 /* short-circuited back to local bridge */
9dcc71e1 1911 vxlan_encap_bypass(skb, vxlan, vxlan);
4ad16930 1912 return;
e4f67add 1913 }
ef59febe 1914 goto drop;
e4f67add 1915 }
ef59febe 1916
d342894c 1917 old_iph = ip_hdr(skb);
1918
d342894c 1919 ttl = vxlan->ttl;
e4c7ed41 1920 if (!ttl && vxlan_addr_multicast(dst))
d342894c 1921 ttl = 1;
1922
1923 tos = vxlan->tos;
1924 if (tos == 1)
206aaafc 1925 tos = ip_tunnel_get_dsfield(old_iph, skb);
d342894c 1926
535fb8d0
TH
1927 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->port_min,
1928 vxlan->port_max, true);
d342894c 1929
e4c7ed41
CW
1930 if (dst->sa.sa_family == AF_INET) {
1931 memset(&fl4, 0, sizeof(fl4));
1932 fl4.flowi4_oif = rdst->remote_ifindex;
1933 fl4.flowi4_tos = RT_TOS(tos);
1934 fl4.daddr = dst->sin.sin_addr.s_addr;
1935 fl4.saddr = vxlan->saddr.sin.sin_addr.s_addr;
1936
f01ec1c0 1937 rt = ip_route_output_key(vxlan->net, &fl4);
e4c7ed41
CW
1938 if (IS_ERR(rt)) {
1939 netdev_dbg(dev, "no route to %pI4\n",
1940 &dst->sin.sin_addr.s_addr);
1941 dev->stats.tx_carrier_errors++;
1942 goto tx_error;
1943 }
d342894c 1944
e4c7ed41
CW
1945 if (rt->dst.dev == dev) {
1946 netdev_dbg(dev, "circular route to %pI4\n",
1947 &dst->sin.sin_addr.s_addr);
1948 dev->stats.collisions++;
fffc15a5 1949 goto rt_tx_error;
e4c7ed41
CW
1950 }
1951
1952 /* Bypass encapsulation if the destination is local */
1953 if (rt->rt_flags & RTCF_LOCAL &&
1954 !(rt->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
1955 struct vxlan_dev *dst_vxlan;
1956
1957 ip_rt_put(rt);
19ca9fc1 1958 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
ac5132d1
TG
1959 dst->sa.sa_family, dst_port,
1960 vxlan->flags);
e4c7ed41
CW
1961 if (!dst_vxlan)
1962 goto tx_error;
1963 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
1964 return;
1965 }
1966
1967 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
1968 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
3511494c
TG
1969 md.vni = htonl(vni << 8);
1970 md.gbp = skb->mark;
d342894c 1971
af33c1ad
TH
1972 err = vxlan_xmit_skb(rt, skb, fl4.saddr,
1973 dst->sin.sin_addr.s_addr, tos, ttl, df,
1974 src_port, dst_port, &md,
1975 !net_eq(vxlan->net, dev_net(vxlan->dev)),
1976 vxlan->flags);
74f47278
PS
1977 if (err < 0) {
1978 /* skb is already freed. */
1979 skb = NULL;
e4c7ed41 1980 goto rt_tx_error;
74f47278
PS
1981 }
1982
e4c7ed41
CW
1983 iptunnel_xmit_stats(err, &dev->stats, dev->tstats);
1984#if IS_ENABLED(CONFIG_IPV6)
1985 } else {
1986 struct sock *sk = vxlan->vn_sock->sock->sk;
1987 struct dst_entry *ndst;
1988 struct flowi6 fl6;
1989 u32 flags;
1990
1991 memset(&fl6, 0, sizeof(fl6));
1992 fl6.flowi6_oif = rdst->remote_ifindex;
1993 fl6.daddr = dst->sin6.sin6_addr;
1994 fl6.saddr = vxlan->saddr.sin6.sin6_addr;
8c1bb79f 1995 fl6.flowi6_proto = IPPROTO_UDP;
e4c7ed41
CW
1996
1997 if (ipv6_stub->ipv6_dst_lookup(sk, &ndst, &fl6)) {
1998 netdev_dbg(dev, "no route to %pI6\n",
1999 &dst->sin6.sin6_addr);
2000 dev->stats.tx_carrier_errors++;
9dcc71e1 2001 goto tx_error;
e4c7ed41 2002 }
d342894c 2003
e4c7ed41
CW
2004 if (ndst->dev == dev) {
2005 netdev_dbg(dev, "circular route to %pI6\n",
2006 &dst->sin6.sin6_addr);
2007 dst_release(ndst);
2008 dev->stats.collisions++;
2009 goto tx_error;
2010 }
0e6fbc5b 2011
e4c7ed41
CW
2012 /* Bypass encapsulation if the destination is local */
2013 flags = ((struct rt6_info *)ndst)->rt6i_flags;
2014 if (flags & RTF_LOCAL &&
2015 !(flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2016 struct vxlan_dev *dst_vxlan;
2017
2018 dst_release(ndst);
19ca9fc1 2019 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
ac5132d1
TG
2020 dst->sa.sa_family, dst_port,
2021 vxlan->flags);
e4c7ed41
CW
2022 if (!dst_vxlan)
2023 goto tx_error;
2024 vxlan_encap_bypass(skb, vxlan, dst_vxlan);
2025 return;
2026 }
49560532 2027
e4c7ed41 2028 ttl = ttl ? : ip6_dst_hoplimit(ndst);
3511494c
TG
2029 md.vni = htonl(vni << 8);
2030 md.gbp = skb->mark;
e4c7ed41 2031
af33c1ad
TH
2032 err = vxlan6_xmit_skb(ndst, skb, dev, &fl6.saddr, &fl6.daddr,
2033 0, ttl, src_port, dst_port, &md,
2034 !net_eq(vxlan->net, dev_net(vxlan->dev)),
2035 vxlan->flags);
e4c7ed41
CW
2036#endif
2037 }
0e6fbc5b 2038
4ad16930 2039 return;
d342894c 2040
2041drop:
2042 dev->stats.tx_dropped++;
2043 goto tx_free;
2044
49560532
PS
2045rt_tx_error:
2046 ip_rt_put(rt);
d342894c 2047tx_error:
2048 dev->stats.tx_errors++;
2049tx_free:
2050 dev_kfree_skb(skb);
d342894c 2051}
2052
6681712d
DS
2053/* Transmit local packets over Vxlan
2054 *
2055 * Outer IP header inherits ECN and DF from inner header.
2056 * Outer UDP destination is the VXLAN assigned port.
2057 * source port is based on hash of flow
2058 */
2059static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2060{
2061 struct vxlan_dev *vxlan = netdev_priv(dev);
2062 struct ethhdr *eth;
2063 bool did_rsc = false;
8f646c92 2064 struct vxlan_rdst *rdst, *fdst = NULL;
6681712d 2065 struct vxlan_fdb *f;
6681712d
DS
2066
2067 skb_reset_mac_header(skb);
2068 eth = eth_hdr(skb);
2069
f564f45c
CW
2070 if ((vxlan->flags & VXLAN_F_PROXY)) {
2071 if (ntohs(eth->h_proto) == ETH_P_ARP)
2072 return arp_reduce(dev, skb);
2073#if IS_ENABLED(CONFIG_IPV6)
2074 else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
91269e39
LR
2075 pskb_may_pull(skb, sizeof(struct ipv6hdr)
2076 + sizeof(struct nd_msg)) &&
f564f45c
CW
2077 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2078 struct nd_msg *msg;
2079
2080 msg = (struct nd_msg *)skb_transport_header(skb);
2081 if (msg->icmph.icmp6_code == 0 &&
2082 msg->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION)
2083 return neigh_reduce(dev, skb);
2084 }
7a9f526f 2085 eth = eth_hdr(skb);
f564f45c
CW
2086#endif
2087 }
6681712d
DS
2088
2089 f = vxlan_find_mac(vxlan, eth->h_dest);
ae884082
DS
2090 did_rsc = false;
2091
2092 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
e15a00aa
CW
2093 (ntohs(eth->h_proto) == ETH_P_IP ||
2094 ntohs(eth->h_proto) == ETH_P_IPV6)) {
ae884082
DS
2095 did_rsc = route_shortcircuit(dev, skb);
2096 if (did_rsc)
2097 f = vxlan_find_mac(vxlan, eth->h_dest);
2098 }
2099
6681712d 2100 if (f == NULL) {
afbd8bae
MR
2101 f = vxlan_find_mac(vxlan, all_zeros_mac);
2102 if (f == NULL) {
2103 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2104 !is_multicast_ether_addr(eth->h_dest))
2105 vxlan_fdb_miss(vxlan, eth->h_dest);
2106
2107 dev->stats.tx_dropped++;
8f646c92 2108 kfree_skb(skb);
afbd8bae
MR
2109 return NETDEV_TX_OK;
2110 }
2111 }
6681712d 2112
afbd8bae
MR
2113 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2114 struct sk_buff *skb1;
6681712d 2115
8f646c92
ED
2116 if (!fdst) {
2117 fdst = rdst;
2118 continue;
2119 }
afbd8bae
MR
2120 skb1 = skb_clone(skb, GFP_ATOMIC);
2121 if (skb1)
2122 vxlan_xmit_one(skb1, dev, rdst, did_rsc);
6681712d
DS
2123 }
2124
8f646c92
ED
2125 if (fdst)
2126 vxlan_xmit_one(skb, dev, fdst, did_rsc);
2127 else
2128 kfree_skb(skb);
4ad16930 2129 return NETDEV_TX_OK;
6681712d
DS
2130}
2131
d342894c 2132/* Walk the forwarding table and purge stale entries */
2133static void vxlan_cleanup(unsigned long arg)
2134{
2135 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2136 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2137 unsigned int h;
2138
2139 if (!netif_running(vxlan->dev))
2140 return;
2141
2142 spin_lock_bh(&vxlan->hash_lock);
2143 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2144 struct hlist_node *p, *n;
2145 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2146 struct vxlan_fdb *f
2147 = container_of(p, struct vxlan_fdb, hlist);
2148 unsigned long timeout;
2149
3c172868 2150 if (f->state & NUD_PERMANENT)
d342894c 2151 continue;
2152
2153 timeout = f->used + vxlan->age_interval * HZ;
2154 if (time_before_eq(timeout, jiffies)) {
2155 netdev_dbg(vxlan->dev,
2156 "garbage collect %pM\n",
2157 f->eth_addr);
2158 f->state = NUD_STALE;
2159 vxlan_fdb_destroy(vxlan, f);
2160 } else if (time_before(timeout, next_timer))
2161 next_timer = timeout;
2162 }
2163 }
2164 spin_unlock_bh(&vxlan->hash_lock);
2165
2166 mod_timer(&vxlan->age_timer, next_timer);
2167}
2168
9c2e24e1
PS
2169static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2170{
2171 __u32 vni = vxlan->default_dst.remote_vni;
2172
2173 vxlan->vn_sock = vs;
2174 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
2175}
2176
d342894c 2177/* Setup stats when device is created */
2178static int vxlan_init(struct net_device *dev)
2179{
1c51a915 2180 struct vxlan_dev *vxlan = netdev_priv(dev);
f01ec1c0 2181 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
1c51a915 2182 struct vxlan_sock *vs;
19ca9fc1 2183 bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
1c51a915 2184
1c213bd2 2185 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
e8171045 2186 if (!dev->tstats)
d342894c 2187 return -ENOMEM;
2188
1c51a915 2189 spin_lock(&vn->sock_lock);
19ca9fc1 2190 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
ac5132d1 2191 vxlan->dst_port, vxlan->flags);
00c83b01 2192 if (vs && atomic_add_unless(&vs->refcnt, 1, 0)) {
1c51a915 2193 /* If we have a socket with same port already, reuse it */
9c2e24e1 2194 vxlan_vs_add_dev(vs, vxlan);
1c51a915
SH
2195 } else {
2196 /* otherwise make new socket outside of RTNL */
2197 dev_hold(dev);
2198 queue_work(vxlan_wq, &vxlan->sock_work);
2199 }
2200 spin_unlock(&vn->sock_lock);
2201
d342894c 2202 return 0;
2203}
2204
ba609e9b 2205static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan)
afbd8bae
MR
2206{
2207 struct vxlan_fdb *f;
2208
2209 spin_lock_bh(&vxlan->hash_lock);
2210 f = __vxlan_find_mac(vxlan, all_zeros_mac);
2211 if (f)
2212 vxlan_fdb_destroy(vxlan, f);
2213 spin_unlock_bh(&vxlan->hash_lock);
2214}
2215
ebf4063e
SH
2216static void vxlan_uninit(struct net_device *dev)
2217{
2218 struct vxlan_dev *vxlan = netdev_priv(dev);
ebf4063e
SH
2219 struct vxlan_sock *vs = vxlan->vn_sock;
2220
ba609e9b 2221 vxlan_fdb_delete_default(vxlan);
afbd8bae 2222
ebf4063e 2223 if (vs)
012a5729 2224 vxlan_sock_release(vs);
ebf4063e
SH
2225 free_percpu(dev->tstats);
2226}
2227
d342894c 2228/* Start ageing timer and join group when device is brought up */
2229static int vxlan_open(struct net_device *dev)
2230{
2231 struct vxlan_dev *vxlan = netdev_priv(dev);
1c51a915
SH
2232 struct vxlan_sock *vs = vxlan->vn_sock;
2233
2234 /* socket hasn't been created */
2235 if (!vs)
2236 return -ENOTCONN;
d342894c 2237
79d4a94f 2238 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
1c51a915 2239 vxlan_sock_hold(vs);
7c47cedf 2240 dev_hold(dev);
3fc2de2f 2241 queue_work(vxlan_wq, &vxlan->igmp_join);
d342894c 2242 }
2243
2244 if (vxlan->age_interval)
2245 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2246
2247 return 0;
2248}
2249
2250/* Purge the forwarding table */
2251static void vxlan_flush(struct vxlan_dev *vxlan)
2252{
31fec5aa 2253 unsigned int h;
d342894c 2254
2255 spin_lock_bh(&vxlan->hash_lock);
2256 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2257 struct hlist_node *p, *n;
2258 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2259 struct vxlan_fdb *f
2260 = container_of(p, struct vxlan_fdb, hlist);
afbd8bae
MR
2261 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2262 if (!is_zero_ether_addr(f->eth_addr))
2263 vxlan_fdb_destroy(vxlan, f);
d342894c 2264 }
2265 }
2266 spin_unlock_bh(&vxlan->hash_lock);
2267}
2268
2269/* Cleanup timer and forwarding table on shutdown */
2270static int vxlan_stop(struct net_device *dev)
2271{
2272 struct vxlan_dev *vxlan = netdev_priv(dev);
f01ec1c0 2273 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
1c51a915 2274 struct vxlan_sock *vs = vxlan->vn_sock;
d342894c 2275
e4c7ed41 2276 if (vs && vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
95ab0991 2277 !vxlan_group_used(vn, vxlan)) {
1c51a915 2278 vxlan_sock_hold(vs);
7c47cedf 2279 dev_hold(dev);
3fc2de2f 2280 queue_work(vxlan_wq, &vxlan->igmp_leave);
7c47cedf 2281 }
d342894c 2282
2283 del_timer_sync(&vxlan->age_timer);
2284
2285 vxlan_flush(vxlan);
2286
2287 return 0;
2288}
2289
d342894c 2290/* Stub, nothing needs to be done. */
2291static void vxlan_set_multicast_list(struct net_device *dev)
2292{
2293}
2294
345010b5
DB
2295static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
2296{
2297 struct vxlan_dev *vxlan = netdev_priv(dev);
2298 struct vxlan_rdst *dst = &vxlan->default_dst;
2299 struct net_device *lowerdev;
2300 int max_mtu;
2301
f01ec1c0 2302 lowerdev = __dev_get_by_index(vxlan->net, dst->remote_ifindex);
345010b5
DB
2303 if (lowerdev == NULL)
2304 return eth_change_mtu(dev, new_mtu);
2305
2306 if (dst->remote_ip.sa.sa_family == AF_INET6)
2307 max_mtu = lowerdev->mtu - VXLAN6_HEADROOM;
2308 else
2309 max_mtu = lowerdev->mtu - VXLAN_HEADROOM;
2310
2311 if (new_mtu < 68 || new_mtu > max_mtu)
2312 return -EINVAL;
2313
2314 dev->mtu = new_mtu;
2315 return 0;
2316}
2317
d342894c 2318static const struct net_device_ops vxlan_netdev_ops = {
2319 .ndo_init = vxlan_init,
ebf4063e 2320 .ndo_uninit = vxlan_uninit,
d342894c 2321 .ndo_open = vxlan_open,
2322 .ndo_stop = vxlan_stop,
2323 .ndo_start_xmit = vxlan_xmit,
e8171045 2324 .ndo_get_stats64 = ip_tunnel_get_stats64,
d342894c 2325 .ndo_set_rx_mode = vxlan_set_multicast_list,
345010b5 2326 .ndo_change_mtu = vxlan_change_mtu,
d342894c 2327 .ndo_validate_addr = eth_validate_addr,
2328 .ndo_set_mac_address = eth_mac_addr,
2329 .ndo_fdb_add = vxlan_fdb_add,
2330 .ndo_fdb_del = vxlan_fdb_delete,
2331 .ndo_fdb_dump = vxlan_fdb_dump,
2332};
2333
2334/* Info for udev, that this is a virtual tunnel endpoint */
2335static struct device_type vxlan_type = {
2336 .name = "vxlan",
2337};
2338
53cf5275 2339/* Calls the ndo_add_vxlan_port of the caller in order to
35e42379
JG
2340 * supply the listening VXLAN udp ports. Callers are expected
2341 * to implement the ndo_add_vxlan_port.
53cf5275
JG
2342 */
2343void vxlan_get_rx_port(struct net_device *dev)
2344{
2345 struct vxlan_sock *vs;
2346 struct net *net = dev_net(dev);
2347 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2348 sa_family_t sa_family;
35e42379
JG
2349 __be16 port;
2350 unsigned int i;
53cf5275
JG
2351
2352 spin_lock(&vn->sock_lock);
2353 for (i = 0; i < PORT_HASH_SIZE; ++i) {
35e42379
JG
2354 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist) {
2355 port = inet_sk(vs->sock->sk)->inet_sport;
53cf5275
JG
2356 sa_family = vs->sock->sk->sk_family;
2357 dev->netdev_ops->ndo_add_vxlan_port(dev, sa_family,
2358 port);
2359 }
2360 }
2361 spin_unlock(&vn->sock_lock);
2362}
2363EXPORT_SYMBOL_GPL(vxlan_get_rx_port);
2364
d342894c 2365/* Initialize the device structure. */
2366static void vxlan_setup(struct net_device *dev)
2367{
2368 struct vxlan_dev *vxlan = netdev_priv(dev);
31fec5aa 2369 unsigned int h;
d342894c 2370
2371 eth_hw_addr_random(dev);
2372 ether_setup(dev);
e4c7ed41 2373 if (vxlan->default_dst.remote_ip.sa.sa_family == AF_INET6)
2853af6a 2374 dev->needed_headroom = ETH_HLEN + VXLAN6_HEADROOM;
e4c7ed41 2375 else
2853af6a 2376 dev->needed_headroom = ETH_HLEN + VXLAN_HEADROOM;
d342894c 2377
2378 dev->netdev_ops = &vxlan_netdev_ops;
ebf4063e 2379 dev->destructor = free_netdev;
d342894c 2380 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2381
2382 dev->tx_queue_len = 0;
2383 dev->features |= NETIF_F_LLTX;
d6727fe3 2384 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
0afb1666 2385 dev->features |= NETIF_F_RXCSUM;
05c0db08 2386 dev->features |= NETIF_F_GSO_SOFTWARE;
0afb1666 2387
1eaa8178
PS
2388 dev->vlan_features = dev->features;
2389 dev->features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
0afb1666 2390 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
05c0db08 2391 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
1eaa8178 2392 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_HW_VLAN_STAG_TX;
02875878 2393 netif_keep_dst(dev);
6602d007 2394 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
d342894c 2395
553675fb 2396 INIT_LIST_HEAD(&vxlan->next);
d342894c 2397 spin_lock_init(&vxlan->hash_lock);
3fc2de2f 2398 INIT_WORK(&vxlan->igmp_join, vxlan_igmp_join);
2399 INIT_WORK(&vxlan->igmp_leave, vxlan_igmp_leave);
1c51a915 2400 INIT_WORK(&vxlan->sock_work, vxlan_sock_work);
d342894c 2401
2402 init_timer_deferrable(&vxlan->age_timer);
2403 vxlan->age_timer.function = vxlan_cleanup;
2404 vxlan->age_timer.data = (unsigned long) vxlan;
2405
823aa873 2406 vxlan->dst_port = htons(vxlan_port);
05f47d69 2407
d342894c 2408 vxlan->dev = dev;
2409
2410 for (h = 0; h < FDB_HASH_SIZE; ++h)
2411 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2412}
2413
2414static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2415 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
5d174dd8 2416 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
e4c7ed41 2417 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
d342894c 2418 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2419 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
e4c7ed41 2420 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
d342894c 2421 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2422 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
2423 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2424 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2425 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
05f47d69 2426 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
e4f67add
DS
2427 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2428 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2429 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2430 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
823aa873 2431 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
5c91ae08
TH
2432 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2433 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2434 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
dfd8645e
TH
2435 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2436 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
3511494c 2437 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
d342894c 2438};
2439
2440static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2441{
2442 if (tb[IFLA_ADDRESS]) {
2443 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2444 pr_debug("invalid link address (not ethernet)\n");
2445 return -EINVAL;
2446 }
2447
2448 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2449 pr_debug("invalid all zero ethernet address\n");
2450 return -EADDRNOTAVAIL;
2451 }
2452 }
2453
2454 if (!data)
2455 return -EINVAL;
2456
2457 if (data[IFLA_VXLAN_ID]) {
2458 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
2459 if (id >= VXLAN_VID_MASK)
2460 return -ERANGE;
2461 }
2462
05f47d69 2463 if (data[IFLA_VXLAN_PORT_RANGE]) {
2464 const struct ifla_vxlan_port_range *p
2465 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2466
2467 if (ntohs(p->high) < ntohs(p->low)) {
2468 pr_debug("port range %u .. %u not valid\n",
2469 ntohs(p->low), ntohs(p->high));
2470 return -EINVAL;
2471 }
2472 }
2473
d342894c 2474 return 0;
2475}
2476
1b13c97f
YB
2477static void vxlan_get_drvinfo(struct net_device *netdev,
2478 struct ethtool_drvinfo *drvinfo)
2479{
2480 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2481 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2482}
2483
2484static const struct ethtool_ops vxlan_ethtool_ops = {
2485 .get_drvinfo = vxlan_get_drvinfo,
2486 .get_link = ethtool_op_get_link,
2487};
2488
553675fb 2489static void vxlan_del_work(struct work_struct *work)
2490{
2491 struct vxlan_sock *vs = container_of(work, struct vxlan_sock, del_work);
acbf74a7 2492 udp_tunnel_sock_release(vs->sock);
553675fb 2493 kfree_rcu(vs, rcu);
2494}
2495
3ee64f39
TH
2496static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2497 __be16 port, u32 flags)
553675fb 2498{
e4c7ed41 2499 struct socket *sock;
3ee64f39
TH
2500 struct udp_port_cfg udp_conf;
2501 int err;
e4c7ed41 2502
3ee64f39 2503 memset(&udp_conf, 0, sizeof(udp_conf));
e4c7ed41 2504
3ee64f39
TH
2505 if (ipv6) {
2506 udp_conf.family = AF_INET6;
3ee64f39 2507 udp_conf.use_udp6_rx_checksums =
3dc2b6a8 2508 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
3ee64f39
TH
2509 } else {
2510 udp_conf.family = AF_INET;
2511 udp_conf.local_ip.s_addr = INADDR_ANY;
e4c7ed41 2512 }
e4c7ed41 2513
3ee64f39 2514 udp_conf.local_udp_port = port;
553675fb 2515
3ee64f39
TH
2516 /* Open UDP socket */
2517 err = udp_sock_create(net, &udp_conf, &sock);
2518 if (err < 0)
2519 return ERR_PTR(err);
e4c7ed41 2520
39deb2c7 2521 return sock;
e4c7ed41
CW
2522}
2523
2524/* Create new listen socket if needed */
2525static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
359a0ea9
TH
2526 vxlan_rcv_t *rcv, void *data,
2527 u32 flags)
e4c7ed41
CW
2528{
2529 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2530 struct vxlan_sock *vs;
2531 struct socket *sock;
e4c7ed41 2532 unsigned int h;
359a0ea9 2533 bool ipv6 = !!(flags & VXLAN_F_IPV6);
acbf74a7 2534 struct udp_tunnel_sock_cfg tunnel_cfg;
e4c7ed41 2535
dc01e7d3 2536 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
e4c7ed41
CW
2537 if (!vs)
2538 return ERR_PTR(-ENOMEM);
2539
2540 for (h = 0; h < VNI_HASH_SIZE; ++h)
2541 INIT_HLIST_HEAD(&vs->vni_list[h]);
2542
2543 INIT_WORK(&vs->del_work, vxlan_del_work);
2544
3ee64f39 2545 sock = vxlan_create_sock(net, ipv6, port, flags);
39deb2c7 2546 if (IS_ERR(sock)) {
553675fb 2547 kfree(vs);
e50fddc8 2548 return ERR_CAST(sock);
553675fb 2549 }
e4c7ed41
CW
2550
2551 vs->sock = sock;
9c2e24e1 2552 atomic_set(&vs->refcnt, 1);
5cfccc5a 2553 vs->rcv = rcv;
012a5729 2554 vs->data = data;
af33c1ad 2555 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
553675fb 2556
dc01e7d3
OG
2557 /* Initialize the vxlan udp offloads structure */
2558 vs->udp_offloads.port = port;
2559 vs->udp_offloads.callbacks.gro_receive = vxlan_gro_receive;
2560 vs->udp_offloads.callbacks.gro_complete = vxlan_gro_complete;
2561
9c2e24e1
PS
2562 spin_lock(&vn->sock_lock);
2563 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
dc01e7d3 2564 vxlan_notify_add_rx_port(vs);
9c2e24e1 2565 spin_unlock(&vn->sock_lock);
553675fb 2566
2567 /* Mark socket as an encapsulation socket. */
acbf74a7
AZ
2568 tunnel_cfg.sk_user_data = vs;
2569 tunnel_cfg.encap_type = 1;
2570 tunnel_cfg.encap_rcv = vxlan_udp_encap_recv;
2571 tunnel_cfg.encap_destroy = NULL;
2572
2573 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
e4c7ed41 2574
9c2e24e1
PS
2575 return vs;
2576}
2577
012a5729
PS
2578struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2579 vxlan_rcv_t *rcv, void *data,
359a0ea9 2580 bool no_share, u32 flags)
9c2e24e1
PS
2581{
2582 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2583 struct vxlan_sock *vs;
19ca9fc1 2584 bool ipv6 = flags & VXLAN_F_IPV6;
9c2e24e1 2585
359a0ea9 2586 vs = vxlan_socket_create(net, port, rcv, data, flags);
9c2e24e1
PS
2587 if (!IS_ERR(vs))
2588 return vs;
553675fb 2589
012a5729
PS
2590 if (no_share) /* Return error if sharing is not allowed. */
2591 return vs;
2592
9c2e24e1 2593 spin_lock(&vn->sock_lock);
ac5132d1 2594 vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port, flags);
00c83b01
ML
2595 if (vs && ((vs->rcv != rcv) ||
2596 !atomic_add_unless(&vs->refcnt, 1, 0)))
5cfccc5a 2597 vs = ERR_PTR(-EBUSY);
5cfccc5a
PS
2598 spin_unlock(&vn->sock_lock);
2599
2600 if (!vs)
9c2e24e1
PS
2601 vs = ERR_PTR(-EINVAL);
2602
553675fb 2603 return vs;
2604}
012a5729 2605EXPORT_SYMBOL_GPL(vxlan_sock_add);
553675fb 2606
1c51a915
SH
2607/* Scheduled at device creation to bind to a socket */
2608static void vxlan_sock_work(struct work_struct *work)
2609{
9c2e24e1 2610 struct vxlan_dev *vxlan = container_of(work, struct vxlan_dev, sock_work);
f01ec1c0 2611 struct net *net = vxlan->net;
1c51a915 2612 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
9c2e24e1
PS
2613 __be16 port = vxlan->dst_port;
2614 struct vxlan_sock *nvs;
1c51a915 2615
359a0ea9 2616 nvs = vxlan_sock_add(net, port, vxlan_rcv, NULL, false, vxlan->flags);
1c51a915 2617 spin_lock(&vn->sock_lock);
9c2e24e1
PS
2618 if (!IS_ERR(nvs))
2619 vxlan_vs_add_dev(nvs, vxlan);
2620 spin_unlock(&vn->sock_lock);
2621
2622 dev_put(vxlan->dev);
1c51a915
SH
2623}
2624
d342894c 2625static int vxlan_newlink(struct net *net, struct net_device *dev,
2626 struct nlattr *tb[], struct nlattr *data[])
2627{
553675fb 2628 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
d342894c 2629 struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 2630 struct vxlan_rdst *dst = &vxlan->default_dst;
d342894c 2631 __u32 vni;
2632 int err;
e4c7ed41 2633 bool use_ipv6 = false;
d342894c 2634
2635 if (!data[IFLA_VXLAN_ID])
2636 return -EINVAL;
2637
f01ec1c0
ND
2638 vxlan->net = dev_net(dev);
2639
d342894c 2640 vni = nla_get_u32(data[IFLA_VXLAN_ID]);
c7995c43 2641 dst->remote_vni = vni;
d342894c 2642
5933a7bb
MR
2643 /* Unless IPv6 is explicitly requested, assume IPv4 */
2644 dst->remote_ip.sa.sa_family = AF_INET;
e4c7ed41
CW
2645 if (data[IFLA_VXLAN_GROUP]) {
2646 dst->remote_ip.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_GROUP]);
e4c7ed41
CW
2647 } else if (data[IFLA_VXLAN_GROUP6]) {
2648 if (!IS_ENABLED(CONFIG_IPV6))
2649 return -EPFNOSUPPORT;
2650
2651 nla_memcpy(&dst->remote_ip.sin6.sin6_addr, data[IFLA_VXLAN_GROUP6],
2652 sizeof(struct in6_addr));
2653 dst->remote_ip.sa.sa_family = AF_INET6;
2654 use_ipv6 = true;
2655 }
d342894c 2656
e4c7ed41
CW
2657 if (data[IFLA_VXLAN_LOCAL]) {
2658 vxlan->saddr.sin.sin_addr.s_addr = nla_get_be32(data[IFLA_VXLAN_LOCAL]);
2659 vxlan->saddr.sa.sa_family = AF_INET;
2660 } else if (data[IFLA_VXLAN_LOCAL6]) {
2661 if (!IS_ENABLED(CONFIG_IPV6))
2662 return -EPFNOSUPPORT;
2663
2664 /* TODO: respect scope id */
2665 nla_memcpy(&vxlan->saddr.sin6.sin6_addr, data[IFLA_VXLAN_LOCAL6],
2666 sizeof(struct in6_addr));
2667 vxlan->saddr.sa.sa_family = AF_INET6;
2668 use_ipv6 = true;
2669 }
d342894c 2670
34e02aa1 2671 if (data[IFLA_VXLAN_LINK] &&
c7995c43 2672 (dst->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]))) {
34e02aa1 2673 struct net_device *lowerdev
c7995c43 2674 = __dev_get_by_index(net, dst->remote_ifindex);
34e02aa1 2675
2676 if (!lowerdev) {
c7995c43 2677 pr_info("ifindex %d does not exist\n", dst->remote_ifindex);
34e02aa1 2678 return -ENODEV;
2679 }
d342894c 2680
e4c7ed41
CW
2681#if IS_ENABLED(CONFIG_IPV6)
2682 if (use_ipv6) {
2683 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2684 if (idev && idev->cnf.disable_ipv6) {
2685 pr_info("IPv6 is disabled via sysctl\n");
2686 return -EPERM;
2687 }
2688 vxlan->flags |= VXLAN_F_IPV6;
2689 }
2690#endif
2691
34e02aa1 2692 if (!tb[IFLA_MTU])
e4c7ed41 2693 dev->mtu = lowerdev->mtu - (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
1ba56fb4 2694
2853af6a 2695 dev->needed_headroom = lowerdev->hard_header_len +
e4c7ed41 2696 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
7bda701e 2697 } else if (use_ipv6)
2698 vxlan->flags |= VXLAN_F_IPV6;
d342894c 2699
2700 if (data[IFLA_VXLAN_TOS])
2701 vxlan->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
2702
afb97186
VB
2703 if (data[IFLA_VXLAN_TTL])
2704 vxlan->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
2705
d342894c 2706 if (!data[IFLA_VXLAN_LEARNING] || nla_get_u8(data[IFLA_VXLAN_LEARNING]))
e4f67add 2707 vxlan->flags |= VXLAN_F_LEARN;
d342894c 2708
2709 if (data[IFLA_VXLAN_AGEING])
2710 vxlan->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
2711 else
2712 vxlan->age_interval = FDB_AGE_DEFAULT;
2713
e4f67add
DS
2714 if (data[IFLA_VXLAN_PROXY] && nla_get_u8(data[IFLA_VXLAN_PROXY]))
2715 vxlan->flags |= VXLAN_F_PROXY;
2716
2717 if (data[IFLA_VXLAN_RSC] && nla_get_u8(data[IFLA_VXLAN_RSC]))
2718 vxlan->flags |= VXLAN_F_RSC;
2719
2720 if (data[IFLA_VXLAN_L2MISS] && nla_get_u8(data[IFLA_VXLAN_L2MISS]))
2721 vxlan->flags |= VXLAN_F_L2MISS;
2722
2723 if (data[IFLA_VXLAN_L3MISS] && nla_get_u8(data[IFLA_VXLAN_L3MISS]))
2724 vxlan->flags |= VXLAN_F_L3MISS;
2725
d342894c 2726 if (data[IFLA_VXLAN_LIMIT])
2727 vxlan->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
2728
05f47d69 2729 if (data[IFLA_VXLAN_PORT_RANGE]) {
2730 const struct ifla_vxlan_port_range *p
2731 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2732 vxlan->port_min = ntohs(p->low);
2733 vxlan->port_max = ntohs(p->high);
2734 }
2735
823aa873 2736 if (data[IFLA_VXLAN_PORT])
2737 vxlan->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
2738
359a0ea9
TH
2739 if (data[IFLA_VXLAN_UDP_CSUM] && nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
2740 vxlan->flags |= VXLAN_F_UDP_CSUM;
2741
2742 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX] &&
2743 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
2744 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
2745
2746 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX] &&
2747 nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
2748 vxlan->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
2749
dfd8645e
TH
2750 if (data[IFLA_VXLAN_REMCSUM_TX] &&
2751 nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
2752 vxlan->flags |= VXLAN_F_REMCSUM_TX;
2753
2754 if (data[IFLA_VXLAN_REMCSUM_RX] &&
2755 nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
2756 vxlan->flags |= VXLAN_F_REMCSUM_RX;
2757
3511494c
TG
2758 if (data[IFLA_VXLAN_GBP])
2759 vxlan->flags |= VXLAN_F_GBP;
2760
19ca9fc1 2761 if (vxlan_find_vni(net, vni, use_ipv6 ? AF_INET6 : AF_INET,
ac5132d1 2762 vxlan->dst_port, vxlan->flags)) {
553675fb 2763 pr_info("duplicate VNI %u\n", vni);
2764 return -EEXIST;
2765 }
2766
7ad24ea4 2767 dev->ethtool_ops = &vxlan_ethtool_ops;
1b13c97f 2768
2936b6ab
SS
2769 /* create an fdb entry for a valid default destination */
2770 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
2771 err = vxlan_fdb_create(vxlan, all_zeros_mac,
2772 &vxlan->default_dst.remote_ip,
2773 NUD_REACHABLE|NUD_PERMANENT,
2774 NLM_F_EXCL|NLM_F_CREATE,
2775 vxlan->dst_port,
2776 vxlan->default_dst.remote_vni,
2777 vxlan->default_dst.remote_ifindex,
2778 NTF_SELF);
2779 if (err)
2780 return err;
2781 }
d342894c 2782
afbd8bae
MR
2783 err = register_netdevice(dev);
2784 if (err) {
ba609e9b 2785 vxlan_fdb_delete_default(vxlan);
afbd8bae
MR
2786 return err;
2787 }
2788
553675fb 2789 list_add(&vxlan->next, &vn->vxlan_list);
553675fb 2790
2791 return 0;
d342894c 2792}
2793
2794static void vxlan_dellink(struct net_device *dev, struct list_head *head)
2795{
2796 struct vxlan_dev *vxlan = netdev_priv(dev);
f01ec1c0 2797 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
d342894c 2798
fe5c3561 2799 spin_lock(&vn->sock_lock);
9c2e24e1
PS
2800 if (!hlist_unhashed(&vxlan->hlist))
2801 hlist_del_rcu(&vxlan->hlist);
fe5c3561 2802 spin_unlock(&vn->sock_lock);
2803
553675fb 2804 list_del(&vxlan->next);
d342894c 2805 unregister_netdevice_queue(dev, head);
2806}
2807
2808static size_t vxlan_get_size(const struct net_device *dev)
2809{
2810
2811 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
e4c7ed41 2812 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
d342894c 2813 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
e4c7ed41 2814 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
d342894c 2815 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
2816 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
2817 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
e4f67add
DS
2818 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
2819 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
2820 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
2821 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
d342894c 2822 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
2823 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
05f47d69 2824 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
359a0ea9
TH
2825 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
2826 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
2827 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
2828 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
dfd8645e
TH
2829 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
2830 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
d342894c 2831 0;
2832}
2833
2834static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
2835{
2836 const struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 2837 const struct vxlan_rdst *dst = &vxlan->default_dst;
05f47d69 2838 struct ifla_vxlan_port_range ports = {
2839 .low = htons(vxlan->port_min),
2840 .high = htons(vxlan->port_max),
2841 };
d342894c 2842
c7995c43 2843 if (nla_put_u32(skb, IFLA_VXLAN_ID, dst->remote_vni))
d342894c 2844 goto nla_put_failure;
2845
e4c7ed41
CW
2846 if (!vxlan_addr_any(&dst->remote_ip)) {
2847 if (dst->remote_ip.sa.sa_family == AF_INET) {
2848 if (nla_put_be32(skb, IFLA_VXLAN_GROUP,
2849 dst->remote_ip.sin.sin_addr.s_addr))
2850 goto nla_put_failure;
2851#if IS_ENABLED(CONFIG_IPV6)
2852 } else {
2853 if (nla_put(skb, IFLA_VXLAN_GROUP6, sizeof(struct in6_addr),
2854 &dst->remote_ip.sin6.sin6_addr))
2855 goto nla_put_failure;
2856#endif
2857 }
2858 }
d342894c 2859
c7995c43 2860 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
d342894c 2861 goto nla_put_failure;
2862
e4c7ed41
CW
2863 if (!vxlan_addr_any(&vxlan->saddr)) {
2864 if (vxlan->saddr.sa.sa_family == AF_INET) {
2865 if (nla_put_be32(skb, IFLA_VXLAN_LOCAL,
2866 vxlan->saddr.sin.sin_addr.s_addr))
2867 goto nla_put_failure;
2868#if IS_ENABLED(CONFIG_IPV6)
2869 } else {
2870 if (nla_put(skb, IFLA_VXLAN_LOCAL6, sizeof(struct in6_addr),
2871 &vxlan->saddr.sin6.sin6_addr))
2872 goto nla_put_failure;
2873#endif
2874 }
2875 }
d342894c 2876
2877 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->ttl) ||
2878 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->tos) ||
e4f67add
DS
2879 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
2880 !!(vxlan->flags & VXLAN_F_LEARN)) ||
2881 nla_put_u8(skb, IFLA_VXLAN_PROXY,
2882 !!(vxlan->flags & VXLAN_F_PROXY)) ||
2883 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
2884 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
2885 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
2886 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
2887 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
d342894c 2888 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->age_interval) ||
823aa873 2889 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->addrmax) ||
359a0ea9
TH
2890 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->dst_port) ||
2891 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
2892 !!(vxlan->flags & VXLAN_F_UDP_CSUM)) ||
2893 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
2894 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
2895 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
dfd8645e
TH
2896 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
2897 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
2898 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
2899 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
2900 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
d342894c 2901 goto nla_put_failure;
2902
05f47d69 2903 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
2904 goto nla_put_failure;
2905
3511494c
TG
2906 if (vxlan->flags & VXLAN_F_GBP &&
2907 nla_put_flag(skb, IFLA_VXLAN_GBP))
2908 goto nla_put_failure;
2909
d342894c 2910 return 0;
2911
2912nla_put_failure:
2913 return -EMSGSIZE;
2914}
2915
1728d4fa
ND
2916static struct net *vxlan_get_link_net(const struct net_device *dev)
2917{
2918 struct vxlan_dev *vxlan = netdev_priv(dev);
2919
2920 return vxlan->net;
2921}
2922
d342894c 2923static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
2924 .kind = "vxlan",
2925 .maxtype = IFLA_VXLAN_MAX,
2926 .policy = vxlan_policy,
2927 .priv_size = sizeof(struct vxlan_dev),
2928 .setup = vxlan_setup,
2929 .validate = vxlan_validate,
2930 .newlink = vxlan_newlink,
2931 .dellink = vxlan_dellink,
2932 .get_size = vxlan_get_size,
2933 .fill_info = vxlan_fill_info,
1728d4fa 2934 .get_link_net = vxlan_get_link_net,
d342894c 2935};
2936
acaf4e70
DB
2937static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
2938 struct net_device *dev)
2939{
2940 struct vxlan_dev *vxlan, *next;
2941 LIST_HEAD(list_kill);
2942
2943 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
2944 struct vxlan_rdst *dst = &vxlan->default_dst;
2945
2946 /* In case we created vxlan device with carrier
2947 * and we loose the carrier due to module unload
2948 * we also need to remove vxlan device. In other
2949 * cases, it's not necessary and remote_ifindex
2950 * is 0 here, so no matches.
2951 */
2952 if (dst->remote_ifindex == dev->ifindex)
2953 vxlan_dellink(vxlan->dev, &list_kill);
2954 }
2955
2956 unregister_netdevice_many(&list_kill);
2957}
2958
2959static int vxlan_lowerdev_event(struct notifier_block *unused,
2960 unsigned long event, void *ptr)
2961{
2962 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
783c1463 2963 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
acaf4e70 2964
783c1463 2965 if (event == NETDEV_UNREGISTER)
acaf4e70
DB
2966 vxlan_handle_lowerdev_unregister(vn, dev);
2967
2968 return NOTIFY_DONE;
2969}
2970
2971static struct notifier_block vxlan_notifier_block __read_mostly = {
2972 .notifier_call = vxlan_lowerdev_event,
2973};
2974
d342894c 2975static __net_init int vxlan_init_net(struct net *net)
2976{
2977 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
31fec5aa 2978 unsigned int h;
d342894c 2979
553675fb 2980 INIT_LIST_HEAD(&vn->vxlan_list);
1c51a915 2981 spin_lock_init(&vn->sock_lock);
d342894c 2982
553675fb 2983 for (h = 0; h < PORT_HASH_SIZE; ++h)
2984 INIT_HLIST_HEAD(&vn->sock_list[h]);
d342894c 2985
2986 return 0;
2987}
2988
f01ec1c0
ND
2989static void __net_exit vxlan_exit_net(struct net *net)
2990{
2991 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2992 struct vxlan_dev *vxlan, *next;
2993 struct net_device *dev, *aux;
2994 LIST_HEAD(list);
2995
2996 rtnl_lock();
2997 for_each_netdev_safe(net, dev, aux)
2998 if (dev->rtnl_link_ops == &vxlan_link_ops)
2999 unregister_netdevice_queue(dev, &list);
3000
3001 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3002 /* If vxlan->dev is in the same netns, it has already been added
3003 * to the list by the previous loop.
3004 */
3005 if (!net_eq(dev_net(vxlan->dev), net))
3006 unregister_netdevice_queue(dev, &list);
3007 }
3008
3009 unregister_netdevice_many(&list);
3010 rtnl_unlock();
3011}
3012
d342894c 3013static struct pernet_operations vxlan_net_ops = {
3014 .init = vxlan_init_net,
f01ec1c0 3015 .exit = vxlan_exit_net,
d342894c 3016 .id = &vxlan_net_id,
3017 .size = sizeof(struct vxlan_net),
3018};
3019
3020static int __init vxlan_init_module(void)
3021{
3022 int rc;
3023
758c57d1
SH
3024 vxlan_wq = alloc_workqueue("vxlan", 0, 0);
3025 if (!vxlan_wq)
3026 return -ENOMEM;
3027
d342894c 3028 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3029
783c1463 3030 rc = register_pernet_subsys(&vxlan_net_ops);
d342894c 3031 if (rc)
3032 goto out1;
3033
acaf4e70 3034 rc = register_netdevice_notifier(&vxlan_notifier_block);
d342894c 3035 if (rc)
3036 goto out2;
3037
acaf4e70
DB
3038 rc = rtnl_link_register(&vxlan_link_ops);
3039 if (rc)
3040 goto out3;
d342894c 3041
acaf4e70
DB
3042 return 0;
3043out3:
3044 unregister_netdevice_notifier(&vxlan_notifier_block);
d342894c 3045out2:
783c1463 3046 unregister_pernet_subsys(&vxlan_net_ops);
d342894c 3047out1:
758c57d1 3048 destroy_workqueue(vxlan_wq);
d342894c 3049 return rc;
3050}
7332a13b 3051late_initcall(vxlan_init_module);
d342894c 3052
3053static void __exit vxlan_cleanup_module(void)
3054{
b7153984 3055 rtnl_link_unregister(&vxlan_link_ops);
acaf4e70 3056 unregister_netdevice_notifier(&vxlan_notifier_block);
758c57d1 3057 destroy_workqueue(vxlan_wq);
783c1463
DB
3058 unregister_pernet_subsys(&vxlan_net_ops);
3059 /* rcu_barrier() is called by netns */
d342894c 3060}
3061module_exit(vxlan_cleanup_module);
3062
3063MODULE_LICENSE("GPL");
3064MODULE_VERSION(VXLAN_VERSION);
3b8df3c6 3065MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
ead5139a 3066MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
d342894c 3067MODULE_ALIAS_RTNL_LINK("vxlan");