networking: convert many more places to skb_put_zero()
[linux-2.6-block.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>
d342894c 14#include <linux/module.h>
15#include <linux/errno.h>
16#include <linux/slab.h>
d342894c 17#include <linux/udp.h>
18#include <linux/igmp.h>
d342894c 19#include <linux/if_ether.h>
1b13c97f 20#include <linux/ethtool.h>
e4f67add
DS
21#include <net/arp.h>
22#include <net/ndisc.h>
d342894c 23#include <net/ip.h>
24#include <net/icmp.h>
d342894c 25#include <net/rtnetlink.h>
d342894c 26#include <net/inet_ecn.h>
27#include <net/net_namespace.h>
28#include <net/netns/generic.h>
012a5729 29#include <net/vxlan.h>
40d29af0 30
e4c7ed41 31#if IS_ENABLED(CONFIG_IPV6)
e4c7ed41 32#include <net/ip6_tunnel.h>
660d98ca 33#include <net/ip6_checksum.h>
e4c7ed41 34#endif
d342894c 35
36#define VXLAN_VERSION "0.1"
37
553675fb 38#define PORT_HASH_BITS 8
39#define PORT_HASH_SIZE (1<<PORT_HASH_BITS)
d342894c 40#define FDB_AGE_DEFAULT 300 /* 5 min */
41#define FDB_AGE_INTERVAL (10 * HZ) /* rescan interval */
42
23c578bf 43/* UDP port for VXLAN traffic.
44 * The IANA assigned port is 4789, but the Linux default is 8472
234f5b73 45 * for compatibility with early adopters.
23c578bf 46 */
9daaa397
SH
47static unsigned short vxlan_port __read_mostly = 8472;
48module_param_named(udp_port, vxlan_port, ushort, 0444);
d342894c 49MODULE_PARM_DESC(udp_port, "Destination UDP port");
50
51static bool log_ecn_error = true;
52module_param(log_ecn_error, bool, 0644);
53MODULE_PARM_DESC(log_ecn_error, "Log packets received with corrupted ECN");
54
c7d03a00 55static unsigned int vxlan_net_id;
0dfbdf41 56static struct rtnl_link_ops vxlan_link_ops;
553675fb 57
7256eac1 58static const u8 all_zeros_mac[ETH_ALEN + 2];
afbd8bae 59
205f356d 60static int vxlan_sock_add(struct vxlan_dev *vxlan);
614732ea 61
a53cb29b
MB
62static void vxlan_vs_del_dev(struct vxlan_dev *vxlan);
63
553675fb 64/* per-network namespace private data for this module */
65struct vxlan_net {
66 struct list_head vxlan_list;
67 struct hlist_head sock_list[PORT_HASH_SIZE];
1c51a915 68 spinlock_t sock_lock;
553675fb 69};
70
d342894c 71/* Forwarding table entry */
72struct vxlan_fdb {
73 struct hlist_node hlist; /* linked list of entries */
74 struct rcu_head rcu;
75 unsigned long updated; /* jiffies */
76 unsigned long used;
3e61aa8f 77 struct list_head remotes;
7177a3b0 78 u8 eth_addr[ETH_ALEN];
d342894c 79 u16 state; /* see ndm_state */
3ad7a4b1 80 __be32 vni;
ae884082 81 u8 flags; /* see ndm_flags */
d342894c 82};
83
d342894c 84/* salt for hash table */
85static u32 vxlan_salt __read_mostly;
86
ee122c79
TG
87static inline bool vxlan_collect_metadata(struct vxlan_sock *vs)
88{
e7030878
TG
89 return vs->flags & VXLAN_F_COLLECT_METADATA ||
90 ip_tunnel_collect_metadata();
ee122c79
TG
91}
92
e4c7ed41
CW
93#if IS_ENABLED(CONFIG_IPV6)
94static inline
95bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
96{
f0ef3126
JB
97 if (a->sa.sa_family != b->sa.sa_family)
98 return false;
99 if (a->sa.sa_family == AF_INET6)
100 return ipv6_addr_equal(&a->sin6.sin6_addr, &b->sin6.sin6_addr);
101 else
102 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
e4c7ed41
CW
103}
104
105static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
106{
f0ef3126
JB
107 if (ipa->sa.sa_family == AF_INET6)
108 return ipv6_addr_any(&ipa->sin6.sin6_addr);
109 else
110 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
e4c7ed41
CW
111}
112
113static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
114{
f0ef3126
JB
115 if (ipa->sa.sa_family == AF_INET6)
116 return ipv6_addr_is_multicast(&ipa->sin6.sin6_addr);
117 else
118 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
e4c7ed41
CW
119}
120
121static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
122{
f0ef3126 123 if (nla_len(nla) >= sizeof(struct in6_addr)) {
67b61f6c 124 ip->sin6.sin6_addr = nla_get_in6_addr(nla);
f0ef3126
JB
125 ip->sa.sa_family = AF_INET6;
126 return 0;
127 } else if (nla_len(nla) >= sizeof(__be32)) {
67b61f6c 128 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
f0ef3126
JB
129 ip->sa.sa_family = AF_INET;
130 return 0;
131 } else {
132 return -EAFNOSUPPORT;
133 }
e4c7ed41
CW
134}
135
136static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
f0ef3126 137 const union vxlan_addr *ip)
e4c7ed41 138{
f0ef3126 139 if (ip->sa.sa_family == AF_INET6)
930345ea 140 return nla_put_in6_addr(skb, attr, &ip->sin6.sin6_addr);
f0ef3126 141 else
930345ea 142 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
e4c7ed41
CW
143}
144
145#else /* !CONFIG_IPV6 */
146
147static inline
148bool vxlan_addr_equal(const union vxlan_addr *a, const union vxlan_addr *b)
149{
f0ef3126 150 return a->sin.sin_addr.s_addr == b->sin.sin_addr.s_addr;
e4c7ed41
CW
151}
152
153static inline bool vxlan_addr_any(const union vxlan_addr *ipa)
154{
f0ef3126 155 return ipa->sin.sin_addr.s_addr == htonl(INADDR_ANY);
e4c7ed41
CW
156}
157
158static inline bool vxlan_addr_multicast(const union vxlan_addr *ipa)
159{
f0ef3126 160 return IN_MULTICAST(ntohl(ipa->sin.sin_addr.s_addr));
e4c7ed41
CW
161}
162
163static int vxlan_nla_get_addr(union vxlan_addr *ip, struct nlattr *nla)
164{
f0ef3126
JB
165 if (nla_len(nla) >= sizeof(struct in6_addr)) {
166 return -EAFNOSUPPORT;
167 } else if (nla_len(nla) >= sizeof(__be32)) {
67b61f6c 168 ip->sin.sin_addr.s_addr = nla_get_in_addr(nla);
f0ef3126
JB
169 ip->sa.sa_family = AF_INET;
170 return 0;
171 } else {
172 return -EAFNOSUPPORT;
173 }
e4c7ed41
CW
174}
175
176static int vxlan_nla_put_addr(struct sk_buff *skb, int attr,
f0ef3126 177 const union vxlan_addr *ip)
e4c7ed41 178{
930345ea 179 return nla_put_in_addr(skb, attr, ip->sin.sin_addr.s_addr);
e4c7ed41
CW
180}
181#endif
182
553675fb 183/* Virtual Network hash table head */
54bfd872 184static inline struct hlist_head *vni_head(struct vxlan_sock *vs, __be32 vni)
553675fb 185{
54bfd872 186 return &vs->vni_list[hash_32((__force u32)vni, VNI_HASH_BITS)];
553675fb 187}
188
189/* Socket hash table head */
190static inline struct hlist_head *vs_head(struct net *net, __be16 port)
d342894c 191{
192 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
193
553675fb 194 return &vn->sock_list[hash_32(ntohs(port), PORT_HASH_BITS)];
195}
196
3e61aa8f
SH
197/* First remote destination for a forwarding entry.
198 * Guaranteed to be non-NULL because remotes are never deleted.
199 */
5ca5461c 200static inline struct vxlan_rdst *first_remote_rcu(struct vxlan_fdb *fdb)
3e61aa8f 201{
5ca5461c 202 return list_entry_rcu(fdb->remotes.next, struct vxlan_rdst, list);
203}
204
205static inline struct vxlan_rdst *first_remote_rtnl(struct vxlan_fdb *fdb)
206{
207 return list_first_entry(&fdb->remotes, struct vxlan_rdst, list);
3e61aa8f
SH
208}
209
ac5132d1
TG
210/* Find VXLAN socket based on network namespace, address family and UDP port
211 * and enabled unshareable flags.
212 */
213static struct vxlan_sock *vxlan_find_sock(struct net *net, sa_family_t family,
214 __be16 port, u32 flags)
553675fb 215{
216 struct vxlan_sock *vs;
af33c1ad
TH
217
218 flags &= VXLAN_F_RCV_FLAGS;
553675fb 219
220 hlist_for_each_entry_rcu(vs, vs_head(net, port), hlist) {
19ca9fc1 221 if (inet_sk(vs->sock->sk)->inet_sport == port &&
705cc62f 222 vxlan_get_sk_family(vs) == family &&
af33c1ad 223 vs->flags == flags)
553675fb 224 return vs;
225 }
226 return NULL;
d342894c 227}
228
54bfd872 229static struct vxlan_dev *vxlan_vs_find_vni(struct vxlan_sock *vs, __be32 vni)
d342894c 230{
231 struct vxlan_dev *vxlan;
d342894c 232
b9167b2e
JB
233 /* For flow based devices, map all packets to VNI 0 */
234 if (vs->flags & VXLAN_F_COLLECT_METADATA)
235 vni = 0;
236
54bfd872
JB
237 hlist_for_each_entry_rcu(vxlan, vni_head(vs, vni), hlist) {
238 if (vxlan->default_dst.remote_vni == vni)
d342894c 239 return vxlan;
240 }
241
242 return NULL;
243}
244
5cfccc5a 245/* Look up VNI in a per net namespace table */
54bfd872 246static struct vxlan_dev *vxlan_find_vni(struct net *net, __be32 vni,
ac5132d1
TG
247 sa_family_t family, __be16 port,
248 u32 flags)
5cfccc5a
PS
249{
250 struct vxlan_sock *vs;
251
ac5132d1 252 vs = vxlan_find_sock(net, family, port, flags);
5cfccc5a
PS
253 if (!vs)
254 return NULL;
255
54bfd872 256 return vxlan_vs_find_vni(vs, vni);
5cfccc5a
PS
257}
258
d342894c 259/* Fill in neighbour message in skbuff. */
260static int vxlan_fdb_info(struct sk_buff *skb, struct vxlan_dev *vxlan,
234f5b73
SH
261 const struct vxlan_fdb *fdb,
262 u32 portid, u32 seq, int type, unsigned int flags,
263 const struct vxlan_rdst *rdst)
d342894c 264{
265 unsigned long now = jiffies;
266 struct nda_cacheinfo ci;
267 struct nlmsghdr *nlh;
268 struct ndmsg *ndm;
e4f67add 269 bool send_ip, send_eth;
d342894c 270
271 nlh = nlmsg_put(skb, portid, seq, type, sizeof(*ndm), flags);
272 if (nlh == NULL)
273 return -EMSGSIZE;
274
275 ndm = nlmsg_data(nlh);
276 memset(ndm, 0, sizeof(*ndm));
e4f67add
DS
277
278 send_eth = send_ip = true;
279
280 if (type == RTM_GETNEIGH) {
e4c7ed41 281 send_ip = !vxlan_addr_any(&rdst->remote_ip);
e4f67add 282 send_eth = !is_zero_ether_addr(fdb->eth_addr);
8f48ba71 283 ndm->ndm_family = send_ip ? rdst->remote_ip.sa.sa_family : AF_INET;
e4f67add
DS
284 } else
285 ndm->ndm_family = AF_BRIDGE;
d342894c 286 ndm->ndm_state = fdb->state;
287 ndm->ndm_ifindex = vxlan->dev->ifindex;
ae884082 288 ndm->ndm_flags = fdb->flags;
545469f7 289 ndm->ndm_type = RTN_UNICAST;
d342894c 290
193523bf 291 if (!net_eq(dev_net(vxlan->dev), vxlan->net) &&
4967082b 292 nla_put_s32(skb, NDA_LINK_NETNSID,
38f507f1 293 peernet2id(dev_net(vxlan->dev), vxlan->net)))
193523bf
ND
294 goto nla_put_failure;
295
e4f67add 296 if (send_eth && nla_put(skb, NDA_LLADDR, ETH_ALEN, &fdb->eth_addr))
d342894c 297 goto nla_put_failure;
298
e4c7ed41 299 if (send_ip && vxlan_nla_put_addr(skb, NDA_DST, &rdst->remote_ip))
6681712d
DS
300 goto nla_put_failure;
301
0dfbdf41 302 if (rdst->remote_port && rdst->remote_port != vxlan->cfg.dst_port &&
6681712d
DS
303 nla_put_be16(skb, NDA_PORT, rdst->remote_port))
304 goto nla_put_failure;
c7995c43 305 if (rdst->remote_vni != vxlan->default_dst.remote_vni &&
54bfd872 306 nla_put_u32(skb, NDA_VNI, be32_to_cpu(rdst->remote_vni)))
6681712d 307 goto nla_put_failure;
3ad7a4b1
RP
308 if ((vxlan->flags & VXLAN_F_COLLECT_METADATA) && fdb->vni &&
309 nla_put_u32(skb, NDA_SRC_VNI,
310 be32_to_cpu(fdb->vni)))
311 goto nla_put_failure;
6681712d
DS
312 if (rdst->remote_ifindex &&
313 nla_put_u32(skb, NDA_IFINDEX, rdst->remote_ifindex))
d342894c 314 goto nla_put_failure;
315
316 ci.ndm_used = jiffies_to_clock_t(now - fdb->used);
317 ci.ndm_confirmed = 0;
318 ci.ndm_updated = jiffies_to_clock_t(now - fdb->updated);
319 ci.ndm_refcnt = 0;
320
321 if (nla_put(skb, NDA_CACHEINFO, sizeof(ci), &ci))
322 goto nla_put_failure;
323
053c095a
JB
324 nlmsg_end(skb, nlh);
325 return 0;
d342894c 326
327nla_put_failure:
328 nlmsg_cancel(skb, nlh);
329 return -EMSGSIZE;
330}
331
332static inline size_t vxlan_nlmsg_size(void)
333{
334 return NLMSG_ALIGN(sizeof(struct ndmsg))
335 + nla_total_size(ETH_ALEN) /* NDA_LLADDR */
e4c7ed41 336 + nla_total_size(sizeof(struct in6_addr)) /* NDA_DST */
73cf3317 337 + nla_total_size(sizeof(__be16)) /* NDA_PORT */
6681712d
DS
338 + nla_total_size(sizeof(__be32)) /* NDA_VNI */
339 + nla_total_size(sizeof(__u32)) /* NDA_IFINDEX */
4967082b 340 + nla_total_size(sizeof(__s32)) /* NDA_LINK_NETNSID */
d342894c 341 + nla_total_size(sizeof(struct nda_cacheinfo));
342}
343
9e4b93f9
ND
344static void vxlan_fdb_notify(struct vxlan_dev *vxlan, struct vxlan_fdb *fdb,
345 struct vxlan_rdst *rd, int type)
d342894c 346{
347 struct net *net = dev_net(vxlan->dev);
348 struct sk_buff *skb;
349 int err = -ENOBUFS;
350
351 skb = nlmsg_new(vxlan_nlmsg_size(), GFP_ATOMIC);
352 if (skb == NULL)
353 goto errout;
354
9e4b93f9 355 err = vxlan_fdb_info(skb, vxlan, fdb, 0, 0, type, 0, rd);
d342894c 356 if (err < 0) {
357 /* -EMSGSIZE implies BUG in vxlan_nlmsg_size() */
358 WARN_ON(err == -EMSGSIZE);
359 kfree_skb(skb);
360 goto errout;
361 }
362
363 rtnl_notify(skb, net, 0, RTNLGRP_NEIGH, NULL, GFP_ATOMIC);
364 return;
365errout:
366 if (err < 0)
367 rtnl_set_sk_err(net, RTNLGRP_NEIGH, err);
368}
369
e4c7ed41 370static void vxlan_ip_miss(struct net_device *dev, union vxlan_addr *ipa)
e4f67add
DS
371{
372 struct vxlan_dev *vxlan = netdev_priv(dev);
bb3fd687
SH
373 struct vxlan_fdb f = {
374 .state = NUD_STALE,
375 };
376 struct vxlan_rdst remote = {
e4c7ed41 377 .remote_ip = *ipa, /* goes to NDA_DST */
54bfd872 378 .remote_vni = cpu_to_be32(VXLAN_N_VID),
bb3fd687 379 };
3e61aa8f 380
9e4b93f9 381 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
e4f67add
DS
382}
383
384static void vxlan_fdb_miss(struct vxlan_dev *vxlan, const u8 eth_addr[ETH_ALEN])
385{
bb3fd687
SH
386 struct vxlan_fdb f = {
387 .state = NUD_STALE,
388 };
9e4b93f9 389 struct vxlan_rdst remote = { };
e4f67add 390
e4f67add
DS
391 memcpy(f.eth_addr, eth_addr, ETH_ALEN);
392
9e4b93f9 393 vxlan_fdb_notify(vxlan, &f, &remote, RTM_GETNEIGH);
e4f67add
DS
394}
395
d342894c 396/* Hash Ethernet address */
397static u32 eth_hash(const unsigned char *addr)
398{
399 u64 value = get_unaligned((u64 *)addr);
400
401 /* only want 6 bytes */
402#ifdef __BIG_ENDIAN
d342894c 403 value >>= 16;
321fb991 404#else
405 value <<= 16;
d342894c 406#endif
407 return hash_64(value, FDB_HASH_BITS);
408}
409
3ad7a4b1
RP
410static u32 eth_vni_hash(const unsigned char *addr, __be32 vni)
411{
412 /* use 1 byte of OUI and 3 bytes of NIC */
413 u32 key = get_unaligned((u32 *)(addr + 2));
414
415 return jhash_2words(key, vni, vxlan_salt) & (FDB_HASH_SIZE - 1);
416}
417
d342894c 418/* Hash chain to use given mac address */
419static inline struct hlist_head *vxlan_fdb_head(struct vxlan_dev *vxlan,
3ad7a4b1 420 const u8 *mac, __be32 vni)
d342894c 421{
3ad7a4b1
RP
422 if (vxlan->flags & VXLAN_F_COLLECT_METADATA)
423 return &vxlan->fdb_head[eth_vni_hash(mac, vni)];
424 else
425 return &vxlan->fdb_head[eth_hash(mac)];
d342894c 426}
427
428/* Look up Ethernet address in forwarding table */
014be2c8 429static struct vxlan_fdb *__vxlan_find_mac(struct vxlan_dev *vxlan,
3ad7a4b1 430 const u8 *mac, __be32 vni)
d342894c 431{
3ad7a4b1 432 struct hlist_head *head = vxlan_fdb_head(vxlan, mac, vni);
d342894c 433 struct vxlan_fdb *f;
d342894c 434
b67bfe0d 435 hlist_for_each_entry_rcu(f, head, hlist) {
3ad7a4b1
RP
436 if (ether_addr_equal(mac, f->eth_addr)) {
437 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
438 if (vni == f->vni)
439 return f;
440 } else {
441 return f;
442 }
443 }
d342894c 444 }
445
446 return NULL;
447}
448
014be2c8 449static struct vxlan_fdb *vxlan_find_mac(struct vxlan_dev *vxlan,
3ad7a4b1 450 const u8 *mac, __be32 vni)
014be2c8
SS
451{
452 struct vxlan_fdb *f;
453
3ad7a4b1 454 f = __vxlan_find_mac(vxlan, mac, vni);
014be2c8
SS
455 if (f)
456 f->used = jiffies;
457
458 return f;
459}
460
a5e7c10a
MR
461/* caller should hold vxlan->hash_lock */
462static struct vxlan_rdst *vxlan_fdb_find_rdst(struct vxlan_fdb *f,
e4c7ed41 463 union vxlan_addr *ip, __be16 port,
54bfd872 464 __be32 vni, __u32 ifindex)
6681712d 465{
3e61aa8f 466 struct vxlan_rdst *rd;
6681712d 467
3e61aa8f 468 list_for_each_entry(rd, &f->remotes, list) {
e4c7ed41 469 if (vxlan_addr_equal(&rd->remote_ip, ip) &&
6681712d
DS
470 rd->remote_port == port &&
471 rd->remote_vni == vni &&
472 rd->remote_ifindex == ifindex)
a5e7c10a 473 return rd;
6681712d 474 }
3e61aa8f 475
a5e7c10a
MR
476 return NULL;
477}
478
906dc186
TR
479/* Replace destination of unicast mac */
480static int vxlan_fdb_replace(struct vxlan_fdb *f,
54bfd872
JB
481 union vxlan_addr *ip, __be16 port, __be32 vni,
482 __u32 ifindex)
906dc186
TR
483{
484 struct vxlan_rdst *rd;
485
486 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
487 if (rd)
488 return 0;
489
490 rd = list_first_entry_or_null(&f->remotes, struct vxlan_rdst, list);
491 if (!rd)
492 return 0;
0c1d70af
PA
493
494 dst_cache_reset(&rd->dst_cache);
e4c7ed41 495 rd->remote_ip = *ip;
906dc186
TR
496 rd->remote_port = port;
497 rd->remote_vni = vni;
498 rd->remote_ifindex = ifindex;
499 return 1;
500}
501
a5e7c10a
MR
502/* Add/update destinations for multicast */
503static int vxlan_fdb_append(struct vxlan_fdb *f,
54bfd872 504 union vxlan_addr *ip, __be16 port, __be32 vni,
9e4b93f9 505 __u32 ifindex, struct vxlan_rdst **rdp)
a5e7c10a
MR
506{
507 struct vxlan_rdst *rd;
508
509 rd = vxlan_fdb_find_rdst(f, ip, port, vni, ifindex);
510 if (rd)
511 return 0;
512
6681712d
DS
513 rd = kmalloc(sizeof(*rd), GFP_ATOMIC);
514 if (rd == NULL)
515 return -ENOBUFS;
0c1d70af
PA
516
517 if (dst_cache_init(&rd->dst_cache, GFP_ATOMIC)) {
518 kfree(rd);
519 return -ENOBUFS;
520 }
521
e4c7ed41 522 rd->remote_ip = *ip;
6681712d
DS
523 rd->remote_port = port;
524 rd->remote_vni = vni;
525 rd->remote_ifindex = ifindex;
3e61aa8f
SH
526
527 list_add_tail_rcu(&rd->list, &f->remotes);
528
9e4b93f9 529 *rdp = rd;
6681712d
DS
530 return 1;
531}
532
dfd8645e
TH
533static struct vxlanhdr *vxlan_gro_remcsum(struct sk_buff *skb,
534 unsigned int off,
535 struct vxlanhdr *vh, size_t hdrlen,
54bfd872
JB
536 __be32 vni_field,
537 struct gro_remcsum *grc,
0ace2ca8 538 bool nopartial)
dfd8645e 539{
b7fe10e5 540 size_t start, offset;
dfd8645e
TH
541
542 if (skb->remcsum_offload)
b7fe10e5 543 return vh;
dfd8645e
TH
544
545 if (!NAPI_GRO_CB(skb)->csum_valid)
546 return NULL;
547
54bfd872
JB
548 start = vxlan_rco_start(vni_field);
549 offset = start + vxlan_rco_offset(vni_field);
dfd8645e 550
b7fe10e5
TH
551 vh = skb_gro_remcsum_process(skb, (void *)vh, off, hdrlen,
552 start, offset, grc, nopartial);
dfd8645e
TH
553
554 skb->remcsum_offload = 1;
555
556 return vh;
557}
558
5602c48c
TH
559static struct sk_buff **vxlan_gro_receive(struct sock *sk,
560 struct sk_buff **head,
561 struct sk_buff *skb)
dc01e7d3
OG
562{
563 struct sk_buff *p, **pp = NULL;
564 struct vxlanhdr *vh, *vh2;
9b174d88 565 unsigned int hlen, off_vx;
dc01e7d3 566 int flush = 1;
5602c48c 567 struct vxlan_sock *vs = rcu_dereference_sk_user_data(sk);
54bfd872 568 __be32 flags;
26c4f7da
TH
569 struct gro_remcsum grc;
570
571 skb_gro_remcsum_init(&grc);
dc01e7d3
OG
572
573 off_vx = skb_gro_offset(skb);
574 hlen = off_vx + sizeof(*vh);
575 vh = skb_gro_header_fast(skb, off_vx);
576 if (skb_gro_header_hard(skb, hlen)) {
577 vh = skb_gro_header_slow(skb, hlen, off_vx);
578 if (unlikely(!vh))
579 goto out;
580 }
dc01e7d3 581
dfd8645e
TH
582 skb_gro_postpull_rcsum(skb, vh, sizeof(struct vxlanhdr));
583
54bfd872 584 flags = vh->vx_flags;
dfd8645e
TH
585
586 if ((flags & VXLAN_HF_RCO) && (vs->flags & VXLAN_F_REMCSUM_RX)) {
587 vh = vxlan_gro_remcsum(skb, off_vx, vh, sizeof(struct vxlanhdr),
54bfd872 588 vh->vx_vni, &grc,
0ace2ca8
TH
589 !!(vs->flags &
590 VXLAN_F_REMCSUM_NOPARTIAL));
dfd8645e
TH
591
592 if (!vh)
593 goto out;
594 }
595
b7fe10e5
TH
596 skb_gro_pull(skb, sizeof(struct vxlanhdr)); /* pull vxlan header */
597
dc01e7d3
OG
598 for (p = *head; p; p = p->next) {
599 if (!NAPI_GRO_CB(p)->same_flow)
600 continue;
601
602 vh2 = (struct vxlanhdr *)(p->data + off_vx);
3511494c
TG
603 if (vh->vx_flags != vh2->vx_flags ||
604 vh->vx_vni != vh2->vx_vni) {
dc01e7d3
OG
605 NAPI_GRO_CB(p)->same_flow = 0;
606 continue;
607 }
dc01e7d3
OG
608 }
609
fcd91dd4 610 pp = call_gro_receive(eth_gro_receive, head, skb);
c194cf93 611 flush = 0;
dc01e7d3 612
dc01e7d3 613out:
26c4f7da 614 skb_gro_remcsum_cleanup(skb, &grc);
dc01e7d3
OG
615 NAPI_GRO_CB(skb)->flush |= flush;
616
617 return pp;
618}
619
5602c48c 620static int vxlan_gro_complete(struct sock *sk, struct sk_buff *skb, int nhoff)
dc01e7d3 621{
229740c6
JR
622 /* Sets 'skb->inner_mac_header' since we are always called with
623 * 'skb->encapsulation' set.
624 */
9b174d88 625 return eth_gro_complete(skb, nhoff + sizeof(struct vxlanhdr));
dc01e7d3
OG
626}
627
d342894c 628/* Add new entry to forwarding table -- assumes lock held */
629static int vxlan_fdb_create(struct vxlan_dev *vxlan,
e4c7ed41 630 const u8 *mac, union vxlan_addr *ip,
6681712d 631 __u16 state, __u16 flags,
3ad7a4b1
RP
632 __be16 port, __be32 src_vni, __be32 vni,
633 __u32 ifindex, __u8 ndm_flags)
d342894c 634{
9e4b93f9 635 struct vxlan_rdst *rd = NULL;
d342894c 636 struct vxlan_fdb *f;
637 int notify = 0;
17b46365 638 int rc;
d342894c 639
3ad7a4b1 640 f = __vxlan_find_mac(vxlan, mac, src_vni);
d342894c 641 if (f) {
642 if (flags & NLM_F_EXCL) {
643 netdev_dbg(vxlan->dev,
644 "lost race to create %pM\n", mac);
645 return -EEXIST;
646 }
647 if (f->state != state) {
648 f->state = state;
649 f->updated = jiffies;
650 notify = 1;
651 }
ae884082
DS
652 if (f->flags != ndm_flags) {
653 f->flags = ndm_flags;
654 f->updated = jiffies;
655 notify = 1;
656 }
906dc186
TR
657 if ((flags & NLM_F_REPLACE)) {
658 /* Only change unicasts */
659 if (!(is_multicast_ether_addr(f->eth_addr) ||
660 is_zero_ether_addr(f->eth_addr))) {
60840429 661 notify |= vxlan_fdb_replace(f, ip, port, vni,
906dc186 662 ifindex);
906dc186
TR
663 } else
664 return -EOPNOTSUPP;
665 }
6681712d 666 if ((flags & NLM_F_APPEND) &&
58e4c767
MR
667 (is_multicast_ether_addr(f->eth_addr) ||
668 is_zero_ether_addr(f->eth_addr))) {
17b46365 669 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
6681712d
DS
670
671 if (rc < 0)
672 return rc;
673 notify |= rc;
674 }
d342894c 675 } else {
676 if (!(flags & NLM_F_CREATE))
677 return -ENOENT;
678
0dfbdf41
TG
679 if (vxlan->cfg.addrmax &&
680 vxlan->addrcnt >= vxlan->cfg.addrmax)
d342894c 681 return -ENOSPC;
682
906dc186
TR
683 /* Disallow replace to add a multicast entry */
684 if ((flags & NLM_F_REPLACE) &&
685 (is_multicast_ether_addr(mac) || is_zero_ether_addr(mac)))
686 return -EOPNOTSUPP;
687
e4c7ed41 688 netdev_dbg(vxlan->dev, "add %pM -> %pIS\n", mac, ip);
d342894c 689 f = kmalloc(sizeof(*f), GFP_ATOMIC);
690 if (!f)
691 return -ENOMEM;
692
693 notify = 1;
d342894c 694 f->state = state;
ae884082 695 f->flags = ndm_flags;
d342894c 696 f->updated = f->used = jiffies;
3ad7a4b1 697 f->vni = src_vni;
3e61aa8f 698 INIT_LIST_HEAD(&f->remotes);
d342894c 699 memcpy(f->eth_addr, mac, ETH_ALEN);
700
17b46365
HY
701 rc = vxlan_fdb_append(f, ip, port, vni, ifindex, &rd);
702 if (rc < 0) {
703 kfree(f);
704 return rc;
705 }
3e61aa8f 706
d342894c 707 ++vxlan->addrcnt;
708 hlist_add_head_rcu(&f->hlist,
3ad7a4b1 709 vxlan_fdb_head(vxlan, mac, src_vni));
d342894c 710 }
711
9e4b93f9
ND
712 if (notify) {
713 if (rd == NULL)
714 rd = first_remote_rtnl(f);
715 vxlan_fdb_notify(vxlan, f, rd, RTM_NEWNEIGH);
716 }
d342894c 717
718 return 0;
719}
720
6706c82e 721static void vxlan_fdb_free(struct rcu_head *head)
6681712d
DS
722{
723 struct vxlan_fdb *f = container_of(head, struct vxlan_fdb, rcu);
3e61aa8f 724 struct vxlan_rdst *rd, *nd;
6681712d 725
0c1d70af
PA
726 list_for_each_entry_safe(rd, nd, &f->remotes, list) {
727 dst_cache_destroy(&rd->dst_cache);
6681712d 728 kfree(rd);
0c1d70af 729 }
6681712d
DS
730 kfree(f);
731}
732
d342894c 733static void vxlan_fdb_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f)
734{
735 netdev_dbg(vxlan->dev,
736 "delete %pM\n", f->eth_addr);
737
738 --vxlan->addrcnt;
9e4b93f9 739 vxlan_fdb_notify(vxlan, f, first_remote_rtnl(f), RTM_DELNEIGH);
d342894c 740
741 hlist_del_rcu(&f->hlist);
6681712d 742 call_rcu(&f->rcu, vxlan_fdb_free);
d342894c 743}
744
35cf2845
LR
745static void vxlan_dst_free(struct rcu_head *head)
746{
747 struct vxlan_rdst *rd = container_of(head, struct vxlan_rdst, rcu);
748
749 dst_cache_destroy(&rd->dst_cache);
750 kfree(rd);
751}
752
753static void vxlan_fdb_dst_destroy(struct vxlan_dev *vxlan, struct vxlan_fdb *f,
754 struct vxlan_rdst *rd)
755{
756 list_del_rcu(&rd->list);
757 vxlan_fdb_notify(vxlan, f, rd, RTM_DELNEIGH);
758 call_rcu(&rd->rcu, vxlan_dst_free);
759}
760
f0b074be 761static int vxlan_fdb_parse(struct nlattr *tb[], struct vxlan_dev *vxlan,
3ad7a4b1
RP
762 union vxlan_addr *ip, __be16 *port, __be32 *src_vni,
763 __be32 *vni, u32 *ifindex)
d342894c 764{
6681712d 765 struct net *net = dev_net(vxlan->dev);
e4c7ed41 766 int err;
d342894c 767
f0b074be 768 if (tb[NDA_DST]) {
e4c7ed41
CW
769 err = vxlan_nla_get_addr(ip, tb[NDA_DST]);
770 if (err)
771 return err;
f0b074be 772 } else {
e4c7ed41
CW
773 union vxlan_addr *remote = &vxlan->default_dst.remote_ip;
774 if (remote->sa.sa_family == AF_INET) {
775 ip->sin.sin_addr.s_addr = htonl(INADDR_ANY);
776 ip->sa.sa_family = AF_INET;
777#if IS_ENABLED(CONFIG_IPV6)
778 } else {
779 ip->sin6.sin6_addr = in6addr_any;
780 ip->sa.sa_family = AF_INET6;
781#endif
782 }
f0b074be 783 }
d342894c 784
6681712d 785 if (tb[NDA_PORT]) {
73cf3317 786 if (nla_len(tb[NDA_PORT]) != sizeof(__be16))
6681712d 787 return -EINVAL;
f0b074be
MR
788 *port = nla_get_be16(tb[NDA_PORT]);
789 } else {
0dfbdf41 790 *port = vxlan->cfg.dst_port;
f0b074be 791 }
6681712d
DS
792
793 if (tb[NDA_VNI]) {
794 if (nla_len(tb[NDA_VNI]) != sizeof(u32))
795 return -EINVAL;
54bfd872 796 *vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
f0b074be
MR
797 } else {
798 *vni = vxlan->default_dst.remote_vni;
799 }
6681712d 800
3ad7a4b1
RP
801 if (tb[NDA_SRC_VNI]) {
802 if (nla_len(tb[NDA_SRC_VNI]) != sizeof(u32))
803 return -EINVAL;
804 *src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
805 } else {
806 *src_vni = vxlan->default_dst.remote_vni;
807 }
808
6681712d 809 if (tb[NDA_IFINDEX]) {
5abb0029 810 struct net_device *tdev;
6681712d
DS
811
812 if (nla_len(tb[NDA_IFINDEX]) != sizeof(u32))
813 return -EINVAL;
f0b074be 814 *ifindex = nla_get_u32(tb[NDA_IFINDEX]);
73763949 815 tdev = __dev_get_by_index(net, *ifindex);
5abb0029 816 if (!tdev)
6681712d 817 return -EADDRNOTAVAIL;
f0b074be
MR
818 } else {
819 *ifindex = 0;
820 }
821
822 return 0;
823}
824
825/* Add static entry (via netlink) */
826static int vxlan_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
827 struct net_device *dev,
f6f6424b 828 const unsigned char *addr, u16 vid, u16 flags)
f0b074be
MR
829{
830 struct vxlan_dev *vxlan = netdev_priv(dev);
831 /* struct net *net = dev_net(vxlan->dev); */
e4c7ed41 832 union vxlan_addr ip;
f0b074be 833 __be16 port;
3ad7a4b1 834 __be32 src_vni, vni;
54bfd872 835 u32 ifindex;
f0b074be
MR
836 int err;
837
838 if (!(ndm->ndm_state & (NUD_PERMANENT|NUD_REACHABLE))) {
839 pr_info("RTM_NEWNEIGH with invalid state %#x\n",
840 ndm->ndm_state);
841 return -EINVAL;
842 }
843
844 if (tb[NDA_DST] == NULL)
845 return -EINVAL;
846
3ad7a4b1 847 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
f0b074be
MR
848 if (err)
849 return err;
6681712d 850
5933a7bb
MR
851 if (vxlan->default_dst.remote_ip.sa.sa_family != ip.sa.sa_family)
852 return -EAFNOSUPPORT;
853
d342894c 854 spin_lock_bh(&vxlan->hash_lock);
e4c7ed41 855 err = vxlan_fdb_create(vxlan, addr, &ip, ndm->ndm_state, flags,
3ad7a4b1 856 port, src_vni, vni, ifindex, ndm->ndm_flags);
d342894c 857 spin_unlock_bh(&vxlan->hash_lock);
858
859 return err;
860}
861
3ad7a4b1
RP
862static int __vxlan_fdb_delete(struct vxlan_dev *vxlan,
863 const unsigned char *addr, union vxlan_addr ip,
864 __be16 port, __be32 src_vni, u32 vni, u32 ifindex,
865 u16 vid)
d342894c 866{
d342894c 867 struct vxlan_fdb *f;
bc7892ba 868 struct vxlan_rdst *rd = NULL;
3ad7a4b1 869 int err = -ENOENT;
bc7892ba 870
3ad7a4b1 871 f = vxlan_find_mac(vxlan, addr, src_vni);
bc7892ba 872 if (!f)
3ad7a4b1 873 return err;
bc7892ba 874
e4c7ed41
CW
875 if (!vxlan_addr_any(&ip)) {
876 rd = vxlan_fdb_find_rdst(f, &ip, port, vni, ifindex);
bc7892ba
MR
877 if (!rd)
878 goto out;
879 }
880
bc7892ba
MR
881 /* remove a destination if it's not the only one on the list,
882 * otherwise destroy the fdb entry
883 */
884 if (rd && !list_is_singular(&f->remotes)) {
35cf2845 885 vxlan_fdb_dst_destroy(vxlan, f, rd);
bc7892ba 886 goto out;
d342894c 887 }
bc7892ba
MR
888
889 vxlan_fdb_destroy(vxlan, f);
890
891out:
3ad7a4b1
RP
892 return 0;
893}
894
895/* Delete entry (via netlink) */
896static int vxlan_fdb_delete(struct ndmsg *ndm, struct nlattr *tb[],
897 struct net_device *dev,
898 const unsigned char *addr, u16 vid)
899{
900 struct vxlan_dev *vxlan = netdev_priv(dev);
901 union vxlan_addr ip;
902 __be32 src_vni, vni;
903 __be16 port;
904 u32 ifindex;
905 int err;
906
907 err = vxlan_fdb_parse(tb, vxlan, &ip, &port, &src_vni, &vni, &ifindex);
908 if (err)
909 return err;
910
911 spin_lock_bh(&vxlan->hash_lock);
912 err = __vxlan_fdb_delete(vxlan, addr, ip, port, src_vni, vni, ifindex,
913 vid);
d342894c 914 spin_unlock_bh(&vxlan->hash_lock);
915
916 return err;
917}
918
919/* Dump forwarding table */
920static int vxlan_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
5d5eacb3 921 struct net_device *dev,
d297653d 922 struct net_device *filter_dev, int *idx)
d342894c 923{
924 struct vxlan_dev *vxlan = netdev_priv(dev);
925 unsigned int h;
d297653d 926 int err = 0;
d342894c 927
928 for (h = 0; h < FDB_HASH_SIZE; ++h) {
929 struct vxlan_fdb *f;
d342894c 930
b67bfe0d 931 hlist_for_each_entry_rcu(f, &vxlan->fdb_head[h], hlist) {
6681712d 932 struct vxlan_rdst *rd;
6681712d 933
3e61aa8f 934 list_for_each_entry_rcu(rd, &f->remotes, list) {
d297653d 935 if (*idx < cb->args[2])
07a51cd3
AW
936 goto skip;
937
6681712d
DS
938 err = vxlan_fdb_info(skb, vxlan, f,
939 NETLINK_CB(cb->skb).portid,
940 cb->nlh->nlmsg_seq,
941 RTM_NEWNEIGH,
942 NLM_F_MULTI, rd);
d297653d 943 if (err < 0)
3e61aa8f 944 goto out;
3e61aa8f 945skip:
d297653d 946 *idx += 1;
07a51cd3 947 }
d342894c 948 }
949 }
3e61aa8f 950out:
d297653d 951 return err;
d342894c 952}
953
954/* Watch incoming packets to learn mapping between Ethernet address
955 * and Tunnel endpoint.
c4b49512 956 * Return true if packet is bogus and should be dropped.
d342894c 957 */
26a41ae6 958static bool vxlan_snoop(struct net_device *dev,
3ad7a4b1
RP
959 union vxlan_addr *src_ip, const u8 *src_mac,
960 __be32 vni)
d342894c 961{
962 struct vxlan_dev *vxlan = netdev_priv(dev);
963 struct vxlan_fdb *f;
d342894c 964
3ad7a4b1 965 f = vxlan_find_mac(vxlan, src_mac, vni);
d342894c 966 if (likely(f)) {
5ca5461c 967 struct vxlan_rdst *rdst = first_remote_rcu(f);
3e61aa8f 968
e4c7ed41 969 if (likely(vxlan_addr_equal(&rdst->remote_ip, src_ip)))
26a41ae6 970 return false;
971
972 /* Don't migrate static entries, drop packets */
e0090a9e 973 if (f->state & (NUD_PERMANENT | NUD_NOARP))
26a41ae6 974 return true;
d342894c 975
976 if (net_ratelimit())
977 netdev_info(dev,
e4c7ed41 978 "%pM migrated from %pIS to %pIS\n",
a4870f79 979 src_mac, &rdst->remote_ip.sa, &src_ip->sa);
d342894c 980
e4c7ed41 981 rdst->remote_ip = *src_ip;
d342894c 982 f->updated = jiffies;
9e4b93f9 983 vxlan_fdb_notify(vxlan, f, rdst, RTM_NEWNEIGH);
d342894c 984 } else {
985 /* learned new entry */
986 spin_lock(&vxlan->hash_lock);
3bf74b1a 987
988 /* close off race between vxlan_flush and incoming packets */
989 if (netif_running(dev))
990 vxlan_fdb_create(vxlan, src_mac, src_ip,
991 NUD_REACHABLE,
992 NLM_F_EXCL|NLM_F_CREATE,
0dfbdf41 993 vxlan->cfg.dst_port,
3ad7a4b1 994 vni,
3bf74b1a 995 vxlan->default_dst.remote_vni,
996 0, NTF_SELF);
d342894c 997 spin_unlock(&vxlan->hash_lock);
998 }
26a41ae6 999
1000 return false;
d342894c 1001}
1002
d342894c 1003/* See if multicast group is already in use by other ID */
95ab0991 1004static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
d342894c 1005{
553675fb 1006 struct vxlan_dev *vxlan;
c6fcc4fc 1007 struct vxlan_sock *sock4;
4053ab1b
AB
1008#if IS_ENABLED(CONFIG_IPV6)
1009 struct vxlan_sock *sock6;
1010#endif
b1be00a6 1011 unsigned short family = dev->default_dst.remote_ip.sa.sa_family;
d342894c 1012
c6fcc4fc 1013 sock4 = rtnl_dereference(dev->vn4_sock);
1014
95ab0991
G
1015 /* The vxlan_sock is only used by dev, leaving group has
1016 * no effect on other vxlan devices.
1017 */
c6fcc4fc 1018 if (family == AF_INET && sock4 && atomic_read(&sock4->refcnt) == 1)
95ab0991 1019 return false;
b1be00a6 1020#if IS_ENABLED(CONFIG_IPV6)
c6fcc4fc 1021 sock6 = rtnl_dereference(dev->vn6_sock);
1022 if (family == AF_INET6 && sock6 && atomic_read(&sock6->refcnt) == 1)
b1be00a6
JB
1023 return false;
1024#endif
95ab0991 1025
553675fb 1026 list_for_each_entry(vxlan, &vn->vxlan_list, next) {
95ab0991 1027 if (!netif_running(vxlan->dev) || vxlan == dev)
553675fb 1028 continue;
d342894c 1029
c6fcc4fc 1030 if (family == AF_INET &&
1031 rtnl_dereference(vxlan->vn4_sock) != sock4)
95ab0991 1032 continue;
b1be00a6 1033#if IS_ENABLED(CONFIG_IPV6)
c6fcc4fc 1034 if (family == AF_INET6 &&
1035 rtnl_dereference(vxlan->vn6_sock) != sock6)
b1be00a6
JB
1036 continue;
1037#endif
95ab0991
G
1038
1039 if (!vxlan_addr_equal(&vxlan->default_dst.remote_ip,
1040 &dev->default_dst.remote_ip))
1041 continue;
1042
1043 if (vxlan->default_dst.remote_ifindex !=
1044 dev->default_dst.remote_ifindex)
1045 continue;
1046
1047 return true;
553675fb 1048 }
d342894c 1049
1050 return false;
1051}
1052
544a773a 1053static bool __vxlan_sock_release_prep(struct vxlan_sock *vs)
7c47cedf 1054{
b1be00a6 1055 struct vxlan_net *vn;
012a5729 1056
b1be00a6 1057 if (!vs)
544a773a 1058 return false;
7c47cedf 1059 if (!atomic_dec_and_test(&vs->refcnt))
544a773a 1060 return false;
d342894c 1061
b1be00a6 1062 vn = net_generic(sock_net(vs->sock->sk), vxlan_net_id);
1c51a915 1063 spin_lock(&vn->sock_lock);
7c47cedf 1064 hlist_del_rcu(&vs->hlist);
e7b3db5e 1065 udp_tunnel_notify_del_rx_port(vs->sock,
b9adcd69
AD
1066 (vs->flags & VXLAN_F_GPE) ?
1067 UDP_TUNNEL_TYPE_VXLAN_GPE :
e7b3db5e 1068 UDP_TUNNEL_TYPE_VXLAN);
1c51a915
SH
1069 spin_unlock(&vn->sock_lock);
1070
544a773a 1071 return true;
d342894c 1072}
1073
b1be00a6
JB
1074static void vxlan_sock_release(struct vxlan_dev *vxlan)
1075{
c6fcc4fc 1076 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
b1be00a6 1077#if IS_ENABLED(CONFIG_IPV6)
c6fcc4fc 1078 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1079
57d88182 1080 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
544a773a
HFS
1081#endif
1082
57d88182 1083 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
544a773a
HFS
1084 synchronize_net();
1085
a53cb29b
MB
1086 vxlan_vs_del_dev(vxlan);
1087
c6fcc4fc 1088 if (__vxlan_sock_release_prep(sock4)) {
1089 udp_tunnel_sock_release(sock4->sock);
1090 kfree(sock4);
544a773a
HFS
1091 }
1092
1093#if IS_ENABLED(CONFIG_IPV6)
c6fcc4fc 1094 if (__vxlan_sock_release_prep(sock6)) {
1095 udp_tunnel_sock_release(sock6->sock);
1096 kfree(sock6);
544a773a 1097 }
b1be00a6
JB
1098#endif
1099}
1100
56ef9c90 1101/* Update multicast group membership when first VNI on
c4b49512 1102 * multicast address is brought up
7c47cedf 1103 */
56ef9c90 1104static int vxlan_igmp_join(struct vxlan_dev *vxlan)
d342894c 1105{
b1be00a6 1106 struct sock *sk;
e4c7ed41
CW
1107 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1108 int ifindex = vxlan->default_dst.remote_ifindex;
149d7549 1109 int ret = -EINVAL;
d342894c 1110
e4c7ed41 1111 if (ip->sa.sa_family == AF_INET) {
c6fcc4fc 1112 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
e4c7ed41
CW
1113 struct ip_mreqn mreq = {
1114 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1115 .imr_ifindex = ifindex,
1116 };
1117
c6fcc4fc 1118 sk = sock4->sock->sk;
b1be00a6 1119 lock_sock(sk);
56ef9c90 1120 ret = ip_mc_join_group(sk, &mreq);
b1be00a6 1121 release_sock(sk);
e4c7ed41
CW
1122#if IS_ENABLED(CONFIG_IPV6)
1123 } else {
c6fcc4fc 1124 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1125
1126 sk = sock6->sock->sk;
b1be00a6 1127 lock_sock(sk);
56ef9c90
MRL
1128 ret = ipv6_stub->ipv6_sock_mc_join(sk, ifindex,
1129 &ip->sin6.sin6_addr);
b1be00a6 1130 release_sock(sk);
e4c7ed41
CW
1131#endif
1132 }
3fc2de2f 1133
56ef9c90 1134 return ret;
3fc2de2f 1135}
1136
1137/* Inverse of vxlan_igmp_join when last VNI is brought down */
56ef9c90 1138static int vxlan_igmp_leave(struct vxlan_dev *vxlan)
3fc2de2f 1139{
b1be00a6 1140 struct sock *sk;
e4c7ed41
CW
1141 union vxlan_addr *ip = &vxlan->default_dst.remote_ip;
1142 int ifindex = vxlan->default_dst.remote_ifindex;
149d7549 1143 int ret = -EINVAL;
3fc2de2f 1144
e4c7ed41 1145 if (ip->sa.sa_family == AF_INET) {
c6fcc4fc 1146 struct vxlan_sock *sock4 = rtnl_dereference(vxlan->vn4_sock);
e4c7ed41
CW
1147 struct ip_mreqn mreq = {
1148 .imr_multiaddr.s_addr = ip->sin.sin_addr.s_addr,
1149 .imr_ifindex = ifindex,
1150 };
1151
c6fcc4fc 1152 sk = sock4->sock->sk;
b1be00a6 1153 lock_sock(sk);
56ef9c90 1154 ret = ip_mc_leave_group(sk, &mreq);
b1be00a6 1155 release_sock(sk);
e4c7ed41
CW
1156#if IS_ENABLED(CONFIG_IPV6)
1157 } else {
c6fcc4fc 1158 struct vxlan_sock *sock6 = rtnl_dereference(vxlan->vn6_sock);
1159
1160 sk = sock6->sock->sk;
b1be00a6 1161 lock_sock(sk);
56ef9c90
MRL
1162 ret = ipv6_stub->ipv6_sock_mc_drop(sk, ifindex,
1163 &ip->sin6.sin6_addr);
b1be00a6 1164 release_sock(sk);
e4c7ed41
CW
1165#endif
1166 }
d342894c 1167
56ef9c90 1168 return ret;
d342894c 1169}
1170
f14ecebb
JB
1171static bool vxlan_remcsum(struct vxlanhdr *unparsed,
1172 struct sk_buff *skb, u32 vxflags)
dfd8645e 1173{
7d34fa75 1174 size_t start, offset;
dfd8645e 1175
f14ecebb
JB
1176 if (!(unparsed->vx_flags & VXLAN_HF_RCO) || skb->remcsum_offload)
1177 goto out;
b7fe10e5 1178
f14ecebb
JB
1179 start = vxlan_rco_start(unparsed->vx_vni);
1180 offset = start + vxlan_rco_offset(unparsed->vx_vni);
dfd8645e 1181
7d34fa75 1182 if (!pskb_may_pull(skb, offset + sizeof(u16)))
be5cfeab 1183 return false;
dfd8645e 1184
be5cfeab
JB
1185 skb_remcsum_process(skb, (void *)(vxlan_hdr(skb) + 1), start, offset,
1186 !!(vxflags & VXLAN_F_REMCSUM_NOPARTIAL));
f14ecebb
JB
1187out:
1188 unparsed->vx_flags &= ~VXLAN_HF_RCO;
1189 unparsed->vx_vni &= VXLAN_VNI_MASK;
be5cfeab 1190 return true;
dfd8645e
TH
1191}
1192
f14ecebb 1193static void vxlan_parse_gbp_hdr(struct vxlanhdr *unparsed,
64f87d36 1194 struct sk_buff *skb, u32 vxflags,
10a5af23 1195 struct vxlan_metadata *md)
3288af08 1196{
f14ecebb 1197 struct vxlanhdr_gbp *gbp = (struct vxlanhdr_gbp *)unparsed;
10a5af23 1198 struct metadata_dst *tun_dst;
f14ecebb
JB
1199
1200 if (!(unparsed->vx_flags & VXLAN_HF_GBP))
1201 goto out;
3288af08 1202
3288af08
JB
1203 md->gbp = ntohs(gbp->policy_id);
1204
10a5af23 1205 tun_dst = (struct metadata_dst *)skb_dst(skb);
810813c4 1206 if (tun_dst) {
3288af08 1207 tun_dst->u.tun_info.key.tun_flags |= TUNNEL_VXLAN_OPT;
810813c4
DM
1208 tun_dst->u.tun_info.options_len = sizeof(*md);
1209 }
3288af08
JB
1210 if (gbp->dont_learn)
1211 md->gbp |= VXLAN_GBP_DONT_LEARN;
1212
1213 if (gbp->policy_applied)
1214 md->gbp |= VXLAN_GBP_POLICY_APPLIED;
f14ecebb 1215
64f87d36
JB
1216 /* In flow-based mode, GBP is carried in dst_metadata */
1217 if (!(vxflags & VXLAN_F_COLLECT_METADATA))
1218 skb->mark = md->gbp;
f14ecebb
JB
1219out:
1220 unparsed->vx_flags &= ~VXLAN_GBP_USED_BITS;
3288af08
JB
1221}
1222
e1e5314d 1223static bool vxlan_parse_gpe_hdr(struct vxlanhdr *unparsed,
61618eea 1224 __be16 *protocol,
e1e5314d
JB
1225 struct sk_buff *skb, u32 vxflags)
1226{
1227 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)unparsed;
1228
1229 /* Need to have Next Protocol set for interfaces in GPE mode. */
1230 if (!gpe->np_applied)
1231 return false;
1232 /* "The initial version is 0. If a receiver does not support the
1233 * version indicated it MUST drop the packet.
1234 */
1235 if (gpe->version != 0)
1236 return false;
1237 /* "When the O bit is set to 1, the packet is an OAM packet and OAM
1238 * processing MUST occur." However, we don't implement OAM
1239 * processing, thus drop the packet.
1240 */
1241 if (gpe->oam_flag)
1242 return false;
1243
1244 switch (gpe->next_protocol) {
1245 case VXLAN_GPE_NP_IPV4:
1246 *protocol = htons(ETH_P_IP);
1247 break;
1248 case VXLAN_GPE_NP_IPV6:
1249 *protocol = htons(ETH_P_IPV6);
1250 break;
1251 case VXLAN_GPE_NP_ETHERNET:
1252 *protocol = htons(ETH_P_TEB);
1253 break;
1254 default:
1255 return false;
1256 }
1257
1258 unparsed->vx_flags &= ~VXLAN_GPE_USED_BITS;
1259 return true;
1260}
1261
1ab016e2
JB
1262static bool vxlan_set_mac(struct vxlan_dev *vxlan,
1263 struct vxlan_sock *vs,
3ad7a4b1 1264 struct sk_buff *skb, __be32 vni)
614732ea 1265{
614732ea 1266 union vxlan_addr saddr;
614732ea 1267
614732ea 1268 skb_reset_mac_header(skb);
614732ea
TG
1269 skb->protocol = eth_type_trans(skb, vxlan->dev);
1270 skb_postpull_rcsum(skb, eth_hdr(skb), ETH_HLEN);
1271
1272 /* Ignore packet loops (and multicast echo) */
1273 if (ether_addr_equal(eth_hdr(skb)->h_source, vxlan->dev->dev_addr))
1ab016e2 1274 return false;
614732ea 1275
760c6805 1276 /* Get address from the outer IP header */
ce212d0f 1277 if (vxlan_get_sk_family(vs) == AF_INET) {
1ab016e2 1278 saddr.sin.sin_addr.s_addr = ip_hdr(skb)->saddr;
614732ea
TG
1279 saddr.sa.sa_family = AF_INET;
1280#if IS_ENABLED(CONFIG_IPV6)
1281 } else {
1ab016e2 1282 saddr.sin6.sin6_addr = ipv6_hdr(skb)->saddr;
614732ea
TG
1283 saddr.sa.sa_family = AF_INET6;
1284#endif
1285 }
1286
1ab016e2 1287 if ((vxlan->flags & VXLAN_F_LEARN) &&
3ad7a4b1 1288 vxlan_snoop(skb->dev, &saddr, eth_hdr(skb)->h_source, vni))
1ab016e2
JB
1289 return false;
1290
1291 return true;
1292}
1293
760c6805
JB
1294static bool vxlan_ecn_decapsulate(struct vxlan_sock *vs, void *oiph,
1295 struct sk_buff *skb)
1296{
1297 int err = 0;
1298
1299 if (vxlan_get_sk_family(vs) == AF_INET)
1300 err = IP_ECN_decapsulate(oiph, skb);
1301#if IS_ENABLED(CONFIG_IPV6)
1302 else
1303 err = IP6_ECN_decapsulate(oiph, skb);
1304#endif
1305
1306 if (unlikely(err) && log_ecn_error) {
1307 if (vxlan_get_sk_family(vs) == AF_INET)
1308 net_info_ratelimited("non-ECT from %pI4 with TOS=%#x\n",
1309 &((struct iphdr *)oiph)->saddr,
1310 ((struct iphdr *)oiph)->tos);
1311 else
1312 net_info_ratelimited("non-ECT from %pI6\n",
1313 &((struct ipv6hdr *)oiph)->saddr);
1314 }
1315 return err <= 1;
1316}
1317
d342894c 1318/* Callback from net/ipv4/udp.c to receive packets */
f2d1968e 1319static int vxlan_rcv(struct sock *sk, struct sk_buff *skb)
d342894c 1320{
f2d1968e 1321 struct pcpu_sw_netstats *stats;
c9e78efb 1322 struct vxlan_dev *vxlan;
5cfccc5a 1323 struct vxlan_sock *vs;
f14ecebb 1324 struct vxlanhdr unparsed;
ee122c79
TG
1325 struct vxlan_metadata _md;
1326 struct vxlan_metadata *md = &_md;
61618eea 1327 __be16 protocol = htons(ETH_P_TEB);
e1e5314d 1328 bool raw_proto = false;
f2d1968e 1329 void *oiph;
3ad7a4b1 1330 __be32 vni = 0;
d342894c 1331
e1e5314d 1332 /* Need UDP and VXLAN header to be present */
7ce04758 1333 if (!pskb_may_pull(skb, VXLAN_HLEN))
e5aed006 1334 goto drop;
d342894c 1335
f14ecebb 1336 unparsed = *vxlan_hdr(skb);
288b01c8
JB
1337 /* VNI flag always required to be set */
1338 if (!(unparsed.vx_flags & VXLAN_HF_VNI)) {
1339 netdev_dbg(skb->dev, "invalid vxlan flags=%#x vni=%#x\n",
1340 ntohl(vxlan_hdr(skb)->vx_flags),
1341 ntohl(vxlan_hdr(skb)->vx_vni));
1342 /* Return non vxlan pkt */
e5aed006 1343 goto drop;
d342894c 1344 }
288b01c8
JB
1345 unparsed.vx_flags &= ~VXLAN_HF_VNI;
1346 unparsed.vx_vni &= ~VXLAN_VNI_MASK;
d342894c 1347
559835ea 1348 vs = rcu_dereference_sk_user_data(sk);
5cfccc5a 1349 if (!vs)
d342894c 1350 goto drop;
d342894c 1351
3ad7a4b1
RP
1352 vni = vxlan_vni(vxlan_hdr(skb)->vx_vni);
1353
3ad7a4b1 1354 vxlan = vxlan_vs_find_vni(vs, vni);
c9e78efb
JB
1355 if (!vxlan)
1356 goto drop;
1357
e1e5314d
JB
1358 /* For backwards compatibility, only allow reserved fields to be
1359 * used by VXLAN extensions if explicitly requested.
1360 */
1361 if (vs->flags & VXLAN_F_GPE) {
1362 if (!vxlan_parse_gpe_hdr(&unparsed, &protocol, skb, vs->flags))
1363 goto drop;
1364 raw_proto = true;
1365 }
1366
1367 if (__iptunnel_pull_header(skb, VXLAN_HLEN, protocol, raw_proto,
1368 !net_eq(vxlan->net, dev_net(vxlan->dev))))
1369 goto drop;
c9e78efb 1370
ee122c79 1371 if (vxlan_collect_metadata(vs)) {
10a5af23 1372 struct metadata_dst *tun_dst;
07dabf20 1373
c29a70d2 1374 tun_dst = udp_tun_rx_dst(skb, vxlan_get_sk_family(vs), TUNNEL_KEY,
d817f432 1375 key32_to_tunnel_id(vni), sizeof(*md));
c29a70d2 1376
ee122c79
TG
1377 if (!tun_dst)
1378 goto drop;
1379
0f1b7354 1380 md = ip_tunnel_info_opts(&tun_dst->u.tun_info);
10a5af23
JB
1381
1382 skb_dst_set(skb, (struct dst_entry *)tun_dst);
ee122c79
TG
1383 } else {
1384 memset(md, 0, sizeof(*md));
1385 }
1386
f14ecebb
JB
1387 if (vs->flags & VXLAN_F_REMCSUM_RX)
1388 if (!vxlan_remcsum(&unparsed, skb, vs->flags))
1389 goto drop;
1390 if (vs->flags & VXLAN_F_GBP)
10a5af23 1391 vxlan_parse_gbp_hdr(&unparsed, skb, vs->flags, md);
e1e5314d
JB
1392 /* Note that GBP and GPE can never be active together. This is
1393 * ensured in vxlan_dev_configure.
1394 */
3511494c 1395
f14ecebb 1396 if (unparsed.vx_flags || unparsed.vx_vni) {
3bf39475
TH
1397 /* If there are any unprocessed flags remaining treat
1398 * this as a malformed packet. This behavior diverges from
1399 * VXLAN RFC (RFC7348) which stipulates that bits in reserved
1400 * in reserved fields are to be ignored. The approach here
c4b49512 1401 * maintains compatibility with previous stack code, and also
3bf39475
TH
1402 * is more robust and provides a little more security in
1403 * adding extensions to VXLAN.
1404 */
288b01c8 1405 goto drop;
3bf39475
TH
1406 }
1407
e1e5314d 1408 if (!raw_proto) {
3ad7a4b1 1409 if (!vxlan_set_mac(vxlan, vs, skb, vni))
e1e5314d
JB
1410 goto drop;
1411 } else {
8be0cfa4 1412 skb_reset_mac_header(skb);
e1e5314d
JB
1413 skb->dev = vxlan->dev;
1414 skb->pkt_type = PACKET_HOST;
1415 }
f2d1968e 1416
f2d1968e
JB
1417 oiph = skb_network_header(skb);
1418 skb_reset_network_header(skb);
1419
1420 if (!vxlan_ecn_decapsulate(vs, oiph, skb)) {
1421 ++vxlan->dev->stats.rx_frame_errors;
1422 ++vxlan->dev->stats.rx_errors;
1423 goto drop;
1424 }
1425
1426 stats = this_cpu_ptr(vxlan->dev->tstats);
1427 u64_stats_update_begin(&stats->syncp);
1428 stats->rx_packets++;
1429 stats->rx_bytes += skb->len;
1430 u64_stats_update_end(&stats->syncp);
1431
1432 gro_cells_receive(&vxlan->gro_cells, skb);
5cfccc5a
PS
1433 return 0;
1434
1435drop:
288b01c8
JB
1436 /* Consume bad packet */
1437 kfree_skb(skb);
1438 return 0;
5cfccc5a
PS
1439}
1440
3ad7a4b1 1441static int arp_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
e4f67add
DS
1442{
1443 struct vxlan_dev *vxlan = netdev_priv(dev);
1444 struct arphdr *parp;
1445 u8 *arpptr, *sha;
1446 __be32 sip, tip;
1447 struct neighbour *n;
1448
1449 if (dev->flags & IFF_NOARP)
1450 goto out;
1451
1452 if (!pskb_may_pull(skb, arp_hdr_len(dev))) {
1453 dev->stats.tx_dropped++;
1454 goto out;
1455 }
1456 parp = arp_hdr(skb);
1457
1458 if ((parp->ar_hrd != htons(ARPHRD_ETHER) &&
1459 parp->ar_hrd != htons(ARPHRD_IEEE802)) ||
1460 parp->ar_pro != htons(ETH_P_IP) ||
1461 parp->ar_op != htons(ARPOP_REQUEST) ||
1462 parp->ar_hln != dev->addr_len ||
1463 parp->ar_pln != 4)
1464 goto out;
1465 arpptr = (u8 *)parp + sizeof(struct arphdr);
1466 sha = arpptr;
1467 arpptr += dev->addr_len; /* sha */
1468 memcpy(&sip, arpptr, sizeof(sip));
1469 arpptr += sizeof(sip);
1470 arpptr += dev->addr_len; /* tha */
1471 memcpy(&tip, arpptr, sizeof(tip));
1472
1473 if (ipv4_is_loopback(tip) ||
1474 ipv4_is_multicast(tip))
1475 goto out;
1476
1477 n = neigh_lookup(&arp_tbl, &tip, dev);
1478
1479 if (n) {
e4f67add
DS
1480 struct vxlan_fdb *f;
1481 struct sk_buff *reply;
1482
1483 if (!(n->nud_state & NUD_CONNECTED)) {
1484 neigh_release(n);
1485 goto out;
1486 }
1487
3ad7a4b1 1488 f = vxlan_find_mac(vxlan, n->ha, vni);
e4c7ed41 1489 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
e4f67add
DS
1490 /* bridge-local neighbor */
1491 neigh_release(n);
1492 goto out;
1493 }
1494
1495 reply = arp_create(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
1496 n->ha, sha);
1497
1498 neigh_release(n);
1499
7346135d
DS
1500 if (reply == NULL)
1501 goto out;
1502
e4f67add
DS
1503 skb_reset_mac_header(reply);
1504 __skb_pull(reply, skb_network_offset(reply));
1505 reply->ip_summed = CHECKSUM_UNNECESSARY;
1506 reply->pkt_type = PACKET_HOST;
1507
1508 if (netif_rx_ni(reply) == NET_RX_DROP)
1509 dev->stats.rx_dropped++;
e4c7ed41
CW
1510 } else if (vxlan->flags & VXLAN_F_L3MISS) {
1511 union vxlan_addr ipa = {
1512 .sin.sin_addr.s_addr = tip,
a45e92a5 1513 .sin.sin_family = AF_INET,
e4c7ed41
CW
1514 };
1515
1516 vxlan_ip_miss(dev, &ipa);
1517 }
e4f67add
DS
1518out:
1519 consume_skb(skb);
1520 return NETDEV_TX_OK;
1521}
1522
f564f45c 1523#if IS_ENABLED(CONFIG_IPV6)
4b29dba9
DS
1524static struct sk_buff *vxlan_na_create(struct sk_buff *request,
1525 struct neighbour *n, bool isrouter)
1526{
1527 struct net_device *dev = request->dev;
1528 struct sk_buff *reply;
1529 struct nd_msg *ns, *na;
1530 struct ipv6hdr *pip6;
1531 u8 *daddr;
1532 int na_olen = 8; /* opt hdr + ETH_ALEN for target */
1533 int ns_olen;
1534 int i, len;
1535
f1fb08f6 1536 if (dev == NULL || !pskb_may_pull(request, request->len))
4b29dba9
DS
1537 return NULL;
1538
1539 len = LL_RESERVED_SPACE(dev) + sizeof(struct ipv6hdr) +
1540 sizeof(*na) + na_olen + dev->needed_tailroom;
1541 reply = alloc_skb(len, GFP_ATOMIC);
1542 if (reply == NULL)
1543 return NULL;
1544
1545 reply->protocol = htons(ETH_P_IPV6);
1546 reply->dev = dev;
1547 skb_reserve(reply, LL_RESERVED_SPACE(request->dev));
1548 skb_push(reply, sizeof(struct ethhdr));
6297b91c 1549 skb_reset_mac_header(reply);
4b29dba9 1550
f1fb08f6 1551 ns = (struct nd_msg *)(ipv6_hdr(request) + 1);
4b29dba9
DS
1552
1553 daddr = eth_hdr(request)->h_source;
f1fb08f6
VB
1554 ns_olen = request->len - skb_network_offset(request) -
1555 sizeof(struct ipv6hdr) - sizeof(*ns);
4b29dba9
DS
1556 for (i = 0; i < ns_olen-1; i += (ns->opt[i+1]<<3)) {
1557 if (ns->opt[i] == ND_OPT_SOURCE_LL_ADDR) {
1558 daddr = ns->opt + i + sizeof(struct nd_opt_hdr);
1559 break;
1560 }
1561 }
1562
1563 /* Ethernet header */
1564 ether_addr_copy(eth_hdr(reply)->h_dest, daddr);
1565 ether_addr_copy(eth_hdr(reply)->h_source, n->ha);
1566 eth_hdr(reply)->h_proto = htons(ETH_P_IPV6);
1567 reply->protocol = htons(ETH_P_IPV6);
1568
1569 skb_pull(reply, sizeof(struct ethhdr));
6297b91c 1570 skb_reset_network_header(reply);
4b29dba9
DS
1571 skb_put(reply, sizeof(struct ipv6hdr));
1572
1573 /* IPv6 header */
1574
1575 pip6 = ipv6_hdr(reply);
1576 memset(pip6, 0, sizeof(struct ipv6hdr));
1577 pip6->version = 6;
1578 pip6->priority = ipv6_hdr(request)->priority;
1579 pip6->nexthdr = IPPROTO_ICMPV6;
1580 pip6->hop_limit = 255;
1581 pip6->daddr = ipv6_hdr(request)->saddr;
1582 pip6->saddr = *(struct in6_addr *)n->primary_key;
1583
1584 skb_pull(reply, sizeof(struct ipv6hdr));
6297b91c 1585 skb_reset_transport_header(reply);
4b29dba9 1586
4b29dba9 1587 /* Neighbor Advertisement */
b080db58 1588 na = skb_put_zero(reply, sizeof(*na) + na_olen);
4b29dba9
DS
1589 na->icmph.icmp6_type = NDISC_NEIGHBOUR_ADVERTISEMENT;
1590 na->icmph.icmp6_router = isrouter;
1591 na->icmph.icmp6_override = 1;
1592 na->icmph.icmp6_solicited = 1;
1593 na->target = ns->target;
1594 ether_addr_copy(&na->opt[2], n->ha);
1595 na->opt[0] = ND_OPT_TARGET_LL_ADDR;
1596 na->opt[1] = na_olen >> 3;
1597
1598 na->icmph.icmp6_cksum = csum_ipv6_magic(&pip6->saddr,
1599 &pip6->daddr, sizeof(*na)+na_olen, IPPROTO_ICMPV6,
1600 csum_partial(na, sizeof(*na)+na_olen, 0));
1601
1602 pip6->payload_len = htons(sizeof(*na)+na_olen);
1603
1604 skb_push(reply, sizeof(struct ipv6hdr));
1605
1606 reply->ip_summed = CHECKSUM_UNNECESSARY;
1607
1608 return reply;
1609}
1610
3ad7a4b1 1611static int neigh_reduce(struct net_device *dev, struct sk_buff *skb, __be32 vni)
f564f45c
CW
1612{
1613 struct vxlan_dev *vxlan = netdev_priv(dev);
4b29dba9 1614 struct nd_msg *msg;
f564f45c 1615 const struct ipv6hdr *iphdr;
8dcd81a9 1616 const struct in6_addr *daddr;
4b29dba9
DS
1617 struct neighbour *n;
1618 struct inet6_dev *in6_dev;
f564f45c
CW
1619
1620 in6_dev = __in6_dev_get(dev);
1621 if (!in6_dev)
1622 goto out;
1623
f1fb08f6
VB
1624 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + sizeof(struct nd_msg)))
1625 goto out;
1626
f564f45c 1627 iphdr = ipv6_hdr(skb);
f564f45c
CW
1628 daddr = &iphdr->daddr;
1629
f1fb08f6 1630 msg = (struct nd_msg *)(iphdr + 1);
f564f45c
CW
1631 if (msg->icmph.icmp6_code != 0 ||
1632 msg->icmph.icmp6_type != NDISC_NEIGHBOUR_SOLICITATION)
1633 goto out;
1634
4b29dba9
DS
1635 if (ipv6_addr_loopback(daddr) ||
1636 ipv6_addr_is_multicast(&msg->target))
1637 goto out;
1638
1639 n = neigh_lookup(ipv6_stub->nd_tbl, &msg->target, dev);
f564f45c
CW
1640
1641 if (n) {
1642 struct vxlan_fdb *f;
4b29dba9 1643 struct sk_buff *reply;
f564f45c
CW
1644
1645 if (!(n->nud_state & NUD_CONNECTED)) {
1646 neigh_release(n);
1647 goto out;
1648 }
1649
3ad7a4b1 1650 f = vxlan_find_mac(vxlan, n->ha, vni);
f564f45c
CW
1651 if (f && vxlan_addr_any(&(first_remote_rcu(f)->remote_ip))) {
1652 /* bridge-local neighbor */
1653 neigh_release(n);
1654 goto out;
1655 }
1656
4b29dba9
DS
1657 reply = vxlan_na_create(skb, n,
1658 !!(f ? f->flags & NTF_ROUTER : 0));
1659
f564f45c 1660 neigh_release(n);
4b29dba9
DS
1661
1662 if (reply == NULL)
1663 goto out;
1664
1665 if (netif_rx_ni(reply) == NET_RX_DROP)
1666 dev->stats.rx_dropped++;
1667
f564f45c 1668 } else if (vxlan->flags & VXLAN_F_L3MISS) {
4b29dba9
DS
1669 union vxlan_addr ipa = {
1670 .sin6.sin6_addr = msg->target,
a45e92a5 1671 .sin6.sin6_family = AF_INET6,
4b29dba9
DS
1672 };
1673
f564f45c
CW
1674 vxlan_ip_miss(dev, &ipa);
1675 }
1676
1677out:
1678 consume_skb(skb);
1679 return NETDEV_TX_OK;
1680}
1681#endif
1682
e4f67add
DS
1683static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb)
1684{
1685 struct vxlan_dev *vxlan = netdev_priv(dev);
1686 struct neighbour *n;
e4f67add
DS
1687
1688 if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
1689 return false;
1690
1691 n = NULL;
1692 switch (ntohs(eth_hdr(skb)->h_proto)) {
1693 case ETH_P_IP:
e15a00aa
CW
1694 {
1695 struct iphdr *pip;
1696
e4f67add
DS
1697 if (!pskb_may_pull(skb, sizeof(struct iphdr)))
1698 return false;
1699 pip = ip_hdr(skb);
1700 n = neigh_lookup(&arp_tbl, &pip->daddr, dev);
e4c7ed41
CW
1701 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1702 union vxlan_addr ipa = {
1703 .sin.sin_addr.s_addr = pip->daddr,
a45e92a5 1704 .sin.sin_family = AF_INET,
e4c7ed41
CW
1705 };
1706
1707 vxlan_ip_miss(dev, &ipa);
1708 return false;
1709 }
1710
e4f67add 1711 break;
e15a00aa
CW
1712 }
1713#if IS_ENABLED(CONFIG_IPV6)
1714 case ETH_P_IPV6:
1715 {
1716 struct ipv6hdr *pip6;
1717
1718 if (!pskb_may_pull(skb, sizeof(struct ipv6hdr)))
1719 return false;
1720 pip6 = ipv6_hdr(skb);
1721 n = neigh_lookup(ipv6_stub->nd_tbl, &pip6->daddr, dev);
1722 if (!n && (vxlan->flags & VXLAN_F_L3MISS)) {
1723 union vxlan_addr ipa = {
1724 .sin6.sin6_addr = pip6->daddr,
a45e92a5 1725 .sin6.sin6_family = AF_INET6,
e15a00aa
CW
1726 };
1727
1728 vxlan_ip_miss(dev, &ipa);
1729 return false;
1730 }
1731
1732 break;
1733 }
1734#endif
e4f67add
DS
1735 default:
1736 return false;
1737 }
1738
1739 if (n) {
1740 bool diff;
1741
7367d0b5 1742 diff = !ether_addr_equal(eth_hdr(skb)->h_dest, n->ha);
e4f67add
DS
1743 if (diff) {
1744 memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
1745 dev->addr_len);
1746 memcpy(eth_hdr(skb)->h_dest, n->ha, dev->addr_len);
1747 }
1748 neigh_release(n);
1749 return diff;
e4c7ed41
CW
1750 }
1751
e4f67add
DS
1752 return false;
1753}
1754
af33c1ad 1755static void vxlan_build_gbp_hdr(struct vxlanhdr *vxh, u32 vxflags,
3511494c
TG
1756 struct vxlan_metadata *md)
1757{
1758 struct vxlanhdr_gbp *gbp;
1759
db79a621
TG
1760 if (!md->gbp)
1761 return;
1762
3511494c 1763 gbp = (struct vxlanhdr_gbp *)vxh;
54bfd872 1764 vxh->vx_flags |= VXLAN_HF_GBP;
3511494c
TG
1765
1766 if (md->gbp & VXLAN_GBP_DONT_LEARN)
1767 gbp->dont_learn = 1;
1768
1769 if (md->gbp & VXLAN_GBP_POLICY_APPLIED)
1770 gbp->policy_applied = 1;
1771
1772 gbp->policy_id = htons(md->gbp & VXLAN_GBP_ID_MASK);
1773}
1774
e1e5314d
JB
1775static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, u32 vxflags,
1776 __be16 protocol)
1777{
1778 struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
1779
1780 gpe->np_applied = 1;
1781
1782 switch (protocol) {
1783 case htons(ETH_P_IP):
1784 gpe->next_protocol = VXLAN_GPE_NP_IPV4;
1785 return 0;
1786 case htons(ETH_P_IPV6):
1787 gpe->next_protocol = VXLAN_GPE_NP_IPV6;
1788 return 0;
1789 case htons(ETH_P_TEB):
1790 gpe->next_protocol = VXLAN_GPE_NP_ETHERNET;
1791 return 0;
1792 }
1793 return -EPFNOSUPPORT;
1794}
1795
f491e56d
JB
1796static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
1797 int iphdr_len, __be32 vni,
1798 struct vxlan_metadata *md, u32 vxflags,
b4ed5cad 1799 bool udp_sum)
e4c7ed41 1800{
e4c7ed41 1801 struct vxlanhdr *vxh;
e4c7ed41
CW
1802 int min_headroom;
1803 int err;
dfd8645e 1804 int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
e1e5314d 1805 __be16 inner_protocol = htons(ETH_P_TEB);
dfd8645e 1806
af33c1ad 1807 if ((vxflags & VXLAN_F_REMCSUM_TX) &&
dfd8645e
TH
1808 skb->ip_summed == CHECKSUM_PARTIAL) {
1809 int csum_start = skb_checksum_start_offset(skb);
1810
1811 if (csum_start <= VXLAN_MAX_REMCSUM_START &&
1812 !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
1813 (skb->csum_offset == offsetof(struct udphdr, check) ||
b5708501 1814 skb->csum_offset == offsetof(struct tcphdr, check)))
dfd8645e 1815 type |= SKB_GSO_TUNNEL_REMCSUM;
dfd8645e 1816 }
e4c7ed41 1817
e4c7ed41 1818 min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
4a4f86cc 1819 + VXLAN_HLEN + iphdr_len;
649c5b8b
PS
1820
1821 /* Need space for new headers (invalidates iph ptr) */
1822 err = skb_cow_head(skb, min_headroom);
e1e5314d 1823 if (unlikely(err))
c46b7897 1824 return err;
649c5b8b 1825
aed069df
AD
1826 err = iptunnel_handle_offloads(skb, type);
1827 if (err)
c46b7897 1828 return err;
b736a623 1829
49560532 1830 vxh = (struct vxlanhdr *) __skb_push(skb, sizeof(*vxh));
54bfd872
JB
1831 vxh->vx_flags = VXLAN_HF_VNI;
1832 vxh->vx_vni = vxlan_vni_field(vni);
49560532 1833
dfd8645e 1834 if (type & SKB_GSO_TUNNEL_REMCSUM) {
54bfd872 1835 unsigned int start;
dfd8645e 1836
54bfd872
JB
1837 start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
1838 vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
1839 vxh->vx_flags |= VXLAN_HF_RCO;
dfd8645e
TH
1840
1841 if (!skb_is_gso(skb)) {
1842 skb->ip_summed = CHECKSUM_NONE;
1843 skb->encapsulation = 0;
1844 }
1845 }
1846
af33c1ad
TH
1847 if (vxflags & VXLAN_F_GBP)
1848 vxlan_build_gbp_hdr(vxh, vxflags, md);
e1e5314d
JB
1849 if (vxflags & VXLAN_F_GPE) {
1850 err = vxlan_build_gpe_hdr(vxh, vxflags, skb->protocol);
1851 if (err < 0)
c46b7897 1852 return err;
e1e5314d
JB
1853 inner_protocol = skb->protocol;
1854 }
3511494c 1855
e1e5314d 1856 skb_set_inner_protocol(skb, inner_protocol);
039f5062 1857 return 0;
49560532 1858}
49560532 1859
655c3de1 1860static struct rtable *vxlan_get_route(struct vxlan_dev *vxlan, struct net_device *dev,
1861 struct vxlan_sock *sock4,
1a8496ba 1862 struct sk_buff *skb, int oif, u8 tos,
4ecb1d83 1863 __be32 daddr, __be32 *saddr, __be16 dport, __be16 sport,
0c1d70af 1864 struct dst_cache *dst_cache,
db3c6139 1865 const struct ip_tunnel_info *info)
1a8496ba 1866{
db3c6139 1867 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
1a8496ba
JB
1868 struct rtable *rt = NULL;
1869 struct flowi4 fl4;
1870
655c3de1 1871 if (!sock4)
1872 return ERR_PTR(-EIO);
1873
db3c6139
DB
1874 if (tos && !info)
1875 use_cache = false;
1876 if (use_cache) {
0c1d70af
PA
1877 rt = dst_cache_get_ip4(dst_cache, saddr);
1878 if (rt)
1879 return rt;
1880 }
1881
1a8496ba
JB
1882 memset(&fl4, 0, sizeof(fl4));
1883 fl4.flowi4_oif = oif;
1884 fl4.flowi4_tos = RT_TOS(tos);
1885 fl4.flowi4_mark = skb->mark;
1886 fl4.flowi4_proto = IPPROTO_UDP;
1887 fl4.daddr = daddr;
272d96a5 1888 fl4.saddr = *saddr;
4ecb1d83
MP
1889 fl4.fl4_dport = dport;
1890 fl4.fl4_sport = sport;
1a8496ba
JB
1891
1892 rt = ip_route_output_key(vxlan->net, &fl4);
655c3de1 1893 if (likely(!IS_ERR(rt))) {
1894 if (rt->dst.dev == dev) {
1895 netdev_dbg(dev, "circular route to %pI4\n", &daddr);
1896 ip_rt_put(rt);
1897 return ERR_PTR(-ELOOP);
1898 }
1899
1a8496ba 1900 *saddr = fl4.saddr;
0c1d70af
PA
1901 if (use_cache)
1902 dst_cache_set_ip4(dst_cache, &rt->dst, fl4.saddr);
655c3de1 1903 } else {
1904 netdev_dbg(dev, "no route to %pI4\n", &daddr);
1905 return ERR_PTR(-ENETUNREACH);
0c1d70af 1906 }
1a8496ba
JB
1907 return rt;
1908}
1909
e5d4b29f
JB
1910#if IS_ENABLED(CONFIG_IPV6)
1911static struct dst_entry *vxlan6_get_route(struct vxlan_dev *vxlan,
655c3de1 1912 struct net_device *dev,
03dc52a8 1913 struct vxlan_sock *sock6,
1400615d 1914 struct sk_buff *skb, int oif, u8 tos,
e7f70af1 1915 __be32 label,
e5d4b29f 1916 const struct in6_addr *daddr,
0c1d70af 1917 struct in6_addr *saddr,
4ecb1d83 1918 __be16 dport, __be16 sport,
db3c6139
DB
1919 struct dst_cache *dst_cache,
1920 const struct ip_tunnel_info *info)
e5d4b29f 1921{
db3c6139 1922 bool use_cache = ip_tunnel_dst_cache_usable(skb, info);
e5d4b29f
JB
1923 struct dst_entry *ndst;
1924 struct flowi6 fl6;
1925 int err;
1926
c6fcc4fc 1927 if (!sock6)
1928 return ERR_PTR(-EIO);
1929
1400615d
DB
1930 if (tos && !info)
1931 use_cache = false;
db3c6139 1932 if (use_cache) {
0c1d70af
PA
1933 ndst = dst_cache_get_ip6(dst_cache, saddr);
1934 if (ndst)
1935 return ndst;
1936 }
1937
e5d4b29f
JB
1938 memset(&fl6, 0, sizeof(fl6));
1939 fl6.flowi6_oif = oif;
1940 fl6.daddr = *daddr;
272d96a5 1941 fl6.saddr = *saddr;
eaa93bf4 1942 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tos), label);
e5d4b29f
JB
1943 fl6.flowi6_mark = skb->mark;
1944 fl6.flowi6_proto = IPPROTO_UDP;
4ecb1d83
MP
1945 fl6.fl6_dport = dport;
1946 fl6.fl6_sport = sport;
e5d4b29f
JB
1947
1948 err = ipv6_stub->ipv6_dst_lookup(vxlan->net,
c6fcc4fc 1949 sock6->sock->sk,
e5d4b29f 1950 &ndst, &fl6);
655c3de1 1951 if (unlikely(err < 0)) {
1952 netdev_dbg(dev, "no route to %pI6\n", daddr);
1953 return ERR_PTR(-ENETUNREACH);
1954 }
1955
1956 if (unlikely(ndst->dev == dev)) {
1957 netdev_dbg(dev, "circular route to %pI6\n", daddr);
1958 dst_release(ndst);
1959 return ERR_PTR(-ELOOP);
1960 }
e5d4b29f
JB
1961
1962 *saddr = fl6.saddr;
db3c6139 1963 if (use_cache)
0c1d70af 1964 dst_cache_set_ip6(dst_cache, ndst, saddr);
e5d4b29f
JB
1965 return ndst;
1966}
1967#endif
1968
9dcc71e1
SS
1969/* Bypass encapsulation if the destination is local */
1970static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
3ad7a4b1 1971 struct vxlan_dev *dst_vxlan, __be32 vni)
9dcc71e1 1972{
8f84985f 1973 struct pcpu_sw_netstats *tx_stats, *rx_stats;
e4c7ed41
CW
1974 union vxlan_addr loopback;
1975 union vxlan_addr *remote_ip = &dst_vxlan->default_dst.remote_ip;
ce6502a8
LR
1976 struct net_device *dev = skb->dev;
1977 int len = skb->len;
9dcc71e1 1978
8f84985f
LR
1979 tx_stats = this_cpu_ptr(src_vxlan->dev->tstats);
1980 rx_stats = this_cpu_ptr(dst_vxlan->dev->tstats);
9dcc71e1
SS
1981 skb->pkt_type = PACKET_HOST;
1982 skb->encapsulation = 0;
1983 skb->dev = dst_vxlan->dev;
1984 __skb_pull(skb, skb_network_offset(skb));
1985
e4c7ed41
CW
1986 if (remote_ip->sa.sa_family == AF_INET) {
1987 loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
1988 loopback.sa.sa_family = AF_INET;
1989#if IS_ENABLED(CONFIG_IPV6)
1990 } else {
1991 loopback.sin6.sin6_addr = in6addr_loopback;
1992 loopback.sa.sa_family = AF_INET6;
1993#endif
1994 }
1995
9dcc71e1 1996 if (dst_vxlan->flags & VXLAN_F_LEARN)
3ad7a4b1 1997 vxlan_snoop(skb->dev, &loopback, eth_hdr(skb)->h_source, vni);
9dcc71e1
SS
1998
1999 u64_stats_update_begin(&tx_stats->syncp);
2000 tx_stats->tx_packets++;
ce6502a8 2001 tx_stats->tx_bytes += len;
9dcc71e1
SS
2002 u64_stats_update_end(&tx_stats->syncp);
2003
2004 if (netif_rx(skb) == NET_RX_SUCCESS) {
2005 u64_stats_update_begin(&rx_stats->syncp);
2006 rx_stats->rx_packets++;
ce6502a8 2007 rx_stats->rx_bytes += len;
9dcc71e1
SS
2008 u64_stats_update_end(&rx_stats->syncp);
2009 } else {
ce6502a8 2010 dev->stats.rx_dropped++;
9dcc71e1
SS
2011 }
2012}
2013
fee1fad7 2014static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2015 struct vxlan_dev *vxlan, union vxlan_addr *daddr,
1f6cc07e 2016 __be16 dst_port, __be32 vni, struct dst_entry *dst,
fee1fad7 2017 u32 rt_flags)
2018{
2019#if IS_ENABLED(CONFIG_IPV6)
2020 /* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2021 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2022 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2023 */
2024 BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2025#endif
2026 /* Bypass encapsulation if the destination is local */
2027 if (rt_flags & RTCF_LOCAL &&
2028 !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))) {
2029 struct vxlan_dev *dst_vxlan;
2030
2031 dst_release(dst);
2032 dst_vxlan = vxlan_find_vni(vxlan->net, vni,
2033 daddr->sa.sa_family, dst_port,
2034 vxlan->flags);
2035 if (!dst_vxlan) {
2036 dev->stats.tx_errors++;
2037 kfree_skb(skb);
2038
2039 return -ENOENT;
2040 }
3ad7a4b1 2041 vxlan_encap_bypass(skb, vxlan, dst_vxlan, vni);
fee1fad7 2042 return 1;
2043 }
2044
2045 return 0;
2046}
2047
4ad16930 2048static void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
3ad7a4b1
RP
2049 __be32 default_vni, struct vxlan_rdst *rdst,
2050 bool did_rsc)
d342894c 2051{
d71785ff 2052 struct dst_cache *dst_cache;
3093fbe7 2053 struct ip_tunnel_info *info;
d342894c 2054 struct vxlan_dev *vxlan = netdev_priv(dev);
0770b53b 2055 const struct iphdr *old_iph = ip_hdr(skb);
e4c7ed41 2056 union vxlan_addr *dst;
272d96a5 2057 union vxlan_addr remote_ip, local_ip;
ee122c79
TG
2058 struct vxlan_metadata _md;
2059 struct vxlan_metadata *md = &_md;
e4c7ed41 2060 __be16 src_port = 0, dst_port;
655c3de1 2061 struct dst_entry *ndst = NULL;
e7f70af1 2062 __be32 vni, label;
d342894c 2063 __u8 tos, ttl;
0e6fbc5b 2064 int err;
ee122c79 2065 u32 flags = vxlan->flags;
b4ed5cad 2066 bool udp_sum = false;
f491e56d 2067 bool xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
d342894c 2068
61adedf3 2069 info = skb_tunnel_info(skb);
3093fbe7 2070
ee122c79 2071 if (rdst) {
0770b53b 2072 dst = &rdst->remote_ip;
2073 if (vxlan_addr_any(dst)) {
2074 if (did_rsc) {
2075 /* short-circuited back to local bridge */
3ad7a4b1 2076 vxlan_encap_bypass(skb, vxlan, vxlan, default_vni);
0770b53b 2077 return;
2078 }
2079 goto drop;
2080 }
2081
0dfbdf41 2082 dst_port = rdst->remote_port ? rdst->remote_port : vxlan->cfg.dst_port;
3ad7a4b1 2083 vni = (rdst->remote_vni) ? : default_vni;
1158632b 2084 local_ip = vxlan->cfg.saddr;
d71785ff 2085 dst_cache = &rdst->dst_cache;
0770b53b 2086 md->gbp = skb->mark;
2087 ttl = vxlan->cfg.ttl;
2088 if (!ttl && vxlan_addr_multicast(dst))
2089 ttl = 1;
2090
2091 tos = vxlan->cfg.tos;
2092 if (tos == 1)
2093 tos = ip_tunnel_get_dsfield(old_iph, skb);
2094
2095 if (dst->sa.sa_family == AF_INET)
2096 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2097 else
2098 udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2099 label = vxlan->cfg.label;
ee122c79
TG
2100 } else {
2101 if (!info) {
2102 WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2103 dev->name);
2104 goto drop;
2105 }
b1be00a6 2106 remote_ip.sa.sa_family = ip_tunnel_info_af(info);
272d96a5 2107 if (remote_ip.sa.sa_family == AF_INET) {
a725e514 2108 remote_ip.sin.sin_addr.s_addr = info->key.u.ipv4.dst;
272d96a5 2109 local_ip.sin.sin_addr.s_addr = info->key.u.ipv4.src;
2110 } else {
a725e514 2111 remote_ip.sin6.sin6_addr = info->key.u.ipv6.dst;
272d96a5 2112 local_ip.sin6.sin6_addr = info->key.u.ipv6.src;
2113 }
ee122c79 2114 dst = &remote_ip;
0770b53b 2115 dst_port = info->key.tp_dst ? : vxlan->cfg.dst_port;
2116 vni = tunnel_id_to_key32(info->key.tun_id);
d71785ff 2117 dst_cache = &info->dst_cache;
0770b53b 2118 if (info->options_len)
2119 md = ip_tunnel_info_opts(info);
a725e514
JB
2120 ttl = info->key.ttl;
2121 tos = info->key.tos;
e7f70af1 2122 label = info->key.label;
b4ed5cad 2123 udp_sum = !!(info->key.tun_flags & TUNNEL_CSUM);
a725e514 2124 }
0770b53b 2125 src_port = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2126 vxlan->cfg.port_max, true);
a725e514 2127
56de859e 2128 rcu_read_lock();
e4c7ed41 2129 if (dst->sa.sa_family == AF_INET) {
c6fcc4fc 2130 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
c46b7897 2131 struct rtable *rt;
0770b53b 2132 __be16 df = 0;
c6fcc4fc 2133
655c3de1 2134 rt = vxlan_get_route(vxlan, dev, sock4, skb,
1a8496ba 2135 rdst ? rdst->remote_ifindex : 0, tos,
272d96a5 2136 dst->sin.sin_addr.s_addr,
1158632b 2137 &local_ip.sin.sin_addr.s_addr,
4ecb1d83 2138 dst_port, src_port,
d71785ff 2139 dst_cache, info);
8ebd115b
DM
2140 if (IS_ERR(rt)) {
2141 err = PTR_ERR(rt);
c46b7897 2142 goto tx_error;
8ebd115b 2143 }
e4c7ed41
CW
2144
2145 /* Bypass encapsulation if the destination is local */
fee1fad7 2146 if (!info) {
2147 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2148 dst_port, vni, &rt->dst,
2149 rt->rt_flags);
2150 if (err)
56de859e 2151 goto out_unlock;
fee1fad7 2152 } else if (info->key.tun_flags & TUNNEL_DONT_FRAGMENT) {
6ceb31ca 2153 df = htons(IP_DF);
fee1fad7 2154 }
6ceb31ca 2155
c46b7897 2156 ndst = &rt->dst;
e4c7ed41
CW
2157 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
2158 ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
c46b7897 2159 err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
54bfd872 2160 vni, md, flags, udp_sum);
f491e56d 2161 if (err < 0)
c46b7897 2162 goto tx_error;
f491e56d 2163
1158632b 2164 udp_tunnel_xmit_skb(rt, sock4->sock->sk, skb, local_ip.sin.sin_addr.s_addr,
f491e56d
JB
2165 dst->sin.sin_addr.s_addr, tos, ttl, df,
2166 src_port, dst_port, xnet, !udp_sum);
e4c7ed41
CW
2167#if IS_ENABLED(CONFIG_IPV6)
2168 } else {
c6fcc4fc 2169 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
e4c7ed41 2170
655c3de1 2171 ndst = vxlan6_get_route(vxlan, dev, sock6, skb,
1400615d 2172 rdst ? rdst->remote_ifindex : 0, tos,
272d96a5 2173 label, &dst->sin6.sin6_addr,
1158632b 2174 &local_ip.sin6.sin6_addr,
4ecb1d83 2175 dst_port, src_port,
db3c6139 2176 dst_cache, info);
e5d4b29f 2177 if (IS_ERR(ndst)) {
8ebd115b 2178 err = PTR_ERR(ndst);
c46b7897 2179 ndst = NULL;
9dcc71e1 2180 goto tx_error;
e4c7ed41 2181 }
655c3de1 2182
fee1fad7 2183 if (!info) {
2184 u32 rt6i_flags = ((struct rt6_info *)ndst)->rt6i_flags;
49560532 2185
fee1fad7 2186 err = encap_bypass_if_local(skb, dev, vxlan, dst,
2187 dst_port, vni, ndst,
2188 rt6i_flags);
2189 if (err)
56de859e 2190 goto out_unlock;
fee1fad7 2191 }
35e2d115 2192
1400615d 2193 tos = ip_tunnel_ecn_encap(tos, old_iph, skb);
e4c7ed41 2194 ttl = ttl ? : ip6_dst_hoplimit(ndst);
f491e56d
JB
2195 skb_scrub_packet(skb, xnet);
2196 err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
54bfd872 2197 vni, md, flags, udp_sum);
c46b7897 2198 if (err < 0)
2199 goto tx_error;
2200
0770b53b 2201 udp_tunnel6_xmit_skb(ndst, sock6->sock->sk, skb, dev,
1158632b 2202 &local_ip.sin6.sin6_addr,
272d96a5 2203 &dst->sin6.sin6_addr, tos, ttl,
e7f70af1 2204 label, src_port, dst_port, !udp_sum);
e4c7ed41
CW
2205#endif
2206 }
56de859e
JK
2207out_unlock:
2208 rcu_read_unlock();
4ad16930 2209 return;
d342894c 2210
2211drop:
2212 dev->stats.tx_dropped++;
c46b7897 2213 dev_kfree_skb(skb);
2214 return;
d342894c 2215
2216tx_error:
56de859e 2217 rcu_read_unlock();
655c3de1 2218 if (err == -ELOOP)
2219 dev->stats.collisions++;
2220 else if (err == -ENETUNREACH)
2221 dev->stats.tx_carrier_errors++;
c46b7897 2222 dst_release(ndst);
d342894c 2223 dev->stats.tx_errors++;
c46b7897 2224 kfree_skb(skb);
d342894c 2225}
2226
6681712d
DS
2227/* Transmit local packets over Vxlan
2228 *
2229 * Outer IP header inherits ECN and DF from inner header.
2230 * Outer UDP destination is the VXLAN assigned port.
2231 * source port is based on hash of flow
2232 */
2233static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2234{
2235 struct vxlan_dev *vxlan = netdev_priv(dev);
3093fbe7 2236 const struct ip_tunnel_info *info;
6681712d
DS
2237 struct ethhdr *eth;
2238 bool did_rsc = false;
8f646c92 2239 struct vxlan_rdst *rdst, *fdst = NULL;
6681712d 2240 struct vxlan_fdb *f;
3ad7a4b1 2241 __be32 vni = 0;
6681712d 2242
61adedf3 2243 info = skb_tunnel_info(skb);
3093fbe7 2244
6681712d 2245 skb_reset_mac_header(skb);
6681712d 2246
47e5d1b0 2247 if (vxlan->flags & VXLAN_F_COLLECT_METADATA) {
3ad7a4b1
RP
2248 if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2249 info->mode & IP_TUNNEL_INFO_TX) {
2250 vni = tunnel_id_to_key32(info->key.tun_id);
2251 } else {
2252 if (info && info->mode & IP_TUNNEL_INFO_TX)
2253 vxlan_xmit_one(skb, dev, vni, NULL, false);
2254 else
2255 kfree_skb(skb);
2256 return NETDEV_TX_OK;
2257 }
47e5d1b0
JB
2258 }
2259
2260 if (vxlan->flags & VXLAN_F_PROXY) {
2261 eth = eth_hdr(skb);
f564f45c 2262 if (ntohs(eth->h_proto) == ETH_P_ARP)
3ad7a4b1 2263 return arp_reduce(dev, skb, vni);
f564f45c 2264#if IS_ENABLED(CONFIG_IPV6)
f1fb08f6
VB
2265 else if (ntohs(eth->h_proto) == ETH_P_IPV6) {
2266 struct ipv6hdr *hdr, _hdr;
2267 if ((hdr = skb_header_pointer(skb,
2268 skb_network_offset(skb),
2269 sizeof(_hdr), &_hdr)) &&
2270 hdr->nexthdr == IPPROTO_ICMPV6)
2271 return neigh_reduce(dev, skb, vni);
f564f45c
CW
2272 }
2273#endif
2274 }
6681712d 2275
47e5d1b0 2276 eth = eth_hdr(skb);
3ad7a4b1 2277 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
ae884082
DS
2278 did_rsc = false;
2279
2280 if (f && (f->flags & NTF_ROUTER) && (vxlan->flags & VXLAN_F_RSC) &&
e15a00aa
CW
2281 (ntohs(eth->h_proto) == ETH_P_IP ||
2282 ntohs(eth->h_proto) == ETH_P_IPV6)) {
ae884082
DS
2283 did_rsc = route_shortcircuit(dev, skb);
2284 if (did_rsc)
3ad7a4b1 2285 f = vxlan_find_mac(vxlan, eth->h_dest, vni);
ae884082
DS
2286 }
2287
6681712d 2288 if (f == NULL) {
3ad7a4b1 2289 f = vxlan_find_mac(vxlan, all_zeros_mac, vni);
afbd8bae
MR
2290 if (f == NULL) {
2291 if ((vxlan->flags & VXLAN_F_L2MISS) &&
2292 !is_multicast_ether_addr(eth->h_dest))
2293 vxlan_fdb_miss(vxlan, eth->h_dest);
2294
2295 dev->stats.tx_dropped++;
8f646c92 2296 kfree_skb(skb);
afbd8bae
MR
2297 return NETDEV_TX_OK;
2298 }
2299 }
6681712d 2300
afbd8bae
MR
2301 list_for_each_entry_rcu(rdst, &f->remotes, list) {
2302 struct sk_buff *skb1;
6681712d 2303
8f646c92
ED
2304 if (!fdst) {
2305 fdst = rdst;
2306 continue;
2307 }
afbd8bae
MR
2308 skb1 = skb_clone(skb, GFP_ATOMIC);
2309 if (skb1)
3ad7a4b1 2310 vxlan_xmit_one(skb1, dev, vni, rdst, did_rsc);
6681712d
DS
2311 }
2312
8f646c92 2313 if (fdst)
3ad7a4b1 2314 vxlan_xmit_one(skb, dev, vni, fdst, did_rsc);
8f646c92
ED
2315 else
2316 kfree_skb(skb);
4ad16930 2317 return NETDEV_TX_OK;
6681712d
DS
2318}
2319
d342894c 2320/* Walk the forwarding table and purge stale entries */
2321static void vxlan_cleanup(unsigned long arg)
2322{
2323 struct vxlan_dev *vxlan = (struct vxlan_dev *) arg;
2324 unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2325 unsigned int h;
2326
2327 if (!netif_running(vxlan->dev))
2328 return;
2329
d342894c 2330 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2331 struct hlist_node *p, *n;
14e1d0fa
SD
2332
2333 spin_lock_bh(&vxlan->hash_lock);
d342894c 2334 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2335 struct vxlan_fdb *f
2336 = container_of(p, struct vxlan_fdb, hlist);
2337 unsigned long timeout;
2338
efb5f68f 2339 if (f->state & (NUD_PERMANENT | NUD_NOARP))
d342894c 2340 continue;
2341
def499c9
RP
2342 if (f->flags & NTF_EXT_LEARNED)
2343 continue;
2344
0dfbdf41 2345 timeout = f->used + vxlan->cfg.age_interval * HZ;
d342894c 2346 if (time_before_eq(timeout, jiffies)) {
2347 netdev_dbg(vxlan->dev,
2348 "garbage collect %pM\n",
2349 f->eth_addr);
2350 f->state = NUD_STALE;
2351 vxlan_fdb_destroy(vxlan, f);
2352 } else if (time_before(timeout, next_timer))
2353 next_timer = timeout;
2354 }
14e1d0fa 2355 spin_unlock_bh(&vxlan->hash_lock);
d342894c 2356 }
d342894c 2357
2358 mod_timer(&vxlan->age_timer, next_timer);
2359}
2360
a53cb29b
MB
2361static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2362{
2363 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2364
2365 spin_lock(&vn->sock_lock);
2366 hlist_del_init_rcu(&vxlan->hlist);
2367 spin_unlock(&vn->sock_lock);
2368}
2369
9c2e24e1
PS
2370static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan)
2371{
56ef9c90 2372 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
54bfd872 2373 __be32 vni = vxlan->default_dst.remote_vni;
9c2e24e1 2374
56ef9c90 2375 spin_lock(&vn->sock_lock);
9c2e24e1 2376 hlist_add_head_rcu(&vxlan->hlist, vni_head(vs, vni));
56ef9c90 2377 spin_unlock(&vn->sock_lock);
9c2e24e1
PS
2378}
2379
d342894c 2380/* Setup stats when device is created */
2381static int vxlan_init(struct net_device *dev)
2382{
1c213bd2 2383 dev->tstats = netdev_alloc_pcpu_stats(struct pcpu_sw_netstats);
e8171045 2384 if (!dev->tstats)
d342894c 2385 return -ENOMEM;
2386
2387 return 0;
2388}
2389
3ad7a4b1 2390static void vxlan_fdb_delete_default(struct vxlan_dev *vxlan, __be32 vni)
afbd8bae
MR
2391{
2392 struct vxlan_fdb *f;
2393
2394 spin_lock_bh(&vxlan->hash_lock);
3ad7a4b1 2395 f = __vxlan_find_mac(vxlan, all_zeros_mac, vni);
afbd8bae
MR
2396 if (f)
2397 vxlan_fdb_destroy(vxlan, f);
2398 spin_unlock_bh(&vxlan->hash_lock);
2399}
2400
ebf4063e
SH
2401static void vxlan_uninit(struct net_device *dev)
2402{
2403 struct vxlan_dev *vxlan = netdev_priv(dev);
ebf4063e 2404
3ad7a4b1 2405 vxlan_fdb_delete_default(vxlan, vxlan->cfg.vni);
afbd8bae 2406
ebf4063e
SH
2407 free_percpu(dev->tstats);
2408}
2409
d342894c 2410/* Start ageing timer and join group when device is brought up */
2411static int vxlan_open(struct net_device *dev)
2412{
2413 struct vxlan_dev *vxlan = netdev_priv(dev);
205f356d 2414 int ret;
56ef9c90 2415
205f356d
JB
2416 ret = vxlan_sock_add(vxlan);
2417 if (ret < 0)
2418 return ret;
d342894c 2419
79d4a94f 2420 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
56ef9c90 2421 ret = vxlan_igmp_join(vxlan);
bef0057b
MRL
2422 if (ret == -EADDRINUSE)
2423 ret = 0;
56ef9c90 2424 if (ret) {
205f356d 2425 vxlan_sock_release(vxlan);
56ef9c90
MRL
2426 return ret;
2427 }
d342894c 2428 }
2429
0dfbdf41 2430 if (vxlan->cfg.age_interval)
d342894c 2431 mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
2432
56ef9c90 2433 return ret;
d342894c 2434}
2435
2436/* Purge the forwarding table */
8b3f9337 2437static void vxlan_flush(struct vxlan_dev *vxlan, bool do_all)
d342894c 2438{
31fec5aa 2439 unsigned int h;
d342894c 2440
2441 spin_lock_bh(&vxlan->hash_lock);
2442 for (h = 0; h < FDB_HASH_SIZE; ++h) {
2443 struct hlist_node *p, *n;
2444 hlist_for_each_safe(p, n, &vxlan->fdb_head[h]) {
2445 struct vxlan_fdb *f
2446 = container_of(p, struct vxlan_fdb, hlist);
8b3f9337
RP
2447 if (!do_all && (f->state & (NUD_PERMANENT | NUD_NOARP)))
2448 continue;
afbd8bae
MR
2449 /* the all_zeros_mac entry is deleted at vxlan_uninit */
2450 if (!is_zero_ether_addr(f->eth_addr))
2451 vxlan_fdb_destroy(vxlan, f);
d342894c 2452 }
2453 }
2454 spin_unlock_bh(&vxlan->hash_lock);
2455}
2456
2457/* Cleanup timer and forwarding table on shutdown */
2458static int vxlan_stop(struct net_device *dev)
2459{
2460 struct vxlan_dev *vxlan = netdev_priv(dev);
f01ec1c0 2461 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
56ef9c90 2462 int ret = 0;
d342894c 2463
24c0e683 2464 if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
f13b1689 2465 !vxlan_group_used(vn, vxlan))
56ef9c90 2466 ret = vxlan_igmp_leave(vxlan);
d342894c 2467
2468 del_timer_sync(&vxlan->age_timer);
2469
8b3f9337 2470 vxlan_flush(vxlan, false);
205f356d 2471 vxlan_sock_release(vxlan);
d342894c 2472
56ef9c90 2473 return ret;
d342894c 2474}
2475
d342894c 2476/* Stub, nothing needs to be done. */
2477static void vxlan_set_multicast_list(struct net_device *dev)
2478{
2479}
2480
91572088 2481static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
345010b5 2482{
91572088
JW
2483 struct vxlan_dev *vxlan = netdev_priv(dev);
2484 struct vxlan_rdst *dst = &vxlan->default_dst;
2485 struct net_device *lowerdev = __dev_get_by_index(vxlan->net,
2486 dst->remote_ifindex);
2487 bool use_ipv6 = false;
345010b5
DB
2488
2489 if (dst->remote_ip.sa.sa_family == AF_INET6)
91572088 2490 use_ipv6 = true;
345010b5 2491
91572088
JW
2492 /* This check is different than dev->max_mtu, because it looks at
2493 * the lowerdev->mtu, rather than the static dev->max_mtu
2494 */
2495 if (lowerdev) {
2496 int max_mtu = lowerdev->mtu -
2497 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2498 if (new_mtu > max_mtu)
72564b59 2499 return -EINVAL;
72564b59
DW
2500 }
2501
345010b5
DB
2502 dev->mtu = new_mtu;
2503 return 0;
2504}
2505
fc4099f1
PS
2506static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
2507{
2508 struct vxlan_dev *vxlan = netdev_priv(dev);
2509 struct ip_tunnel_info *info = skb_tunnel_info(skb);
2510 __be16 sport, dport;
2511
2512 sport = udp_flow_src_port(dev_net(dev), skb, vxlan->cfg.port_min,
2513 vxlan->cfg.port_max, true);
2514 dport = info->key.tp_dst ? : vxlan->cfg.dst_port;
2515
239e944f 2516 if (ip_tunnel_info_af(info) == AF_INET) {
c6fcc4fc 2517 struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
1a8496ba
JB
2518 struct rtable *rt;
2519
655c3de1 2520 rt = vxlan_get_route(vxlan, dev, sock4, skb, 0, info->key.tos,
1a8496ba 2521 info->key.u.ipv4.dst,
22f0708a
PA
2522 &info->key.u.ipv4.src, dport, sport,
2523 &info->dst_cache, info);
1a8496ba
JB
2524 if (IS_ERR(rt))
2525 return PTR_ERR(rt);
2526 ip_rt_put(rt);
239e944f
JB
2527 } else {
2528#if IS_ENABLED(CONFIG_IPV6)
03dc52a8 2529 struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
239e944f
JB
2530 struct dst_entry *ndst;
2531
655c3de1 2532 ndst = vxlan6_get_route(vxlan, dev, sock6, skb, 0, info->key.tos,
e7f70af1 2533 info->key.label, &info->key.u.ipv6.dst,
22f0708a
PA
2534 &info->key.u.ipv6.src, dport, sport,
2535 &info->dst_cache, info);
239e944f
JB
2536 if (IS_ERR(ndst))
2537 return PTR_ERR(ndst);
2538 dst_release(ndst);
239e944f
JB
2539#else /* !CONFIG_IPV6 */
2540 return -EPFNOSUPPORT;
2541#endif
2542 }
1a8496ba
JB
2543 info->key.tp_src = sport;
2544 info->key.tp_dst = dport;
239e944f 2545 return 0;
fc4099f1
PS
2546}
2547
0c867c9b 2548static const struct net_device_ops vxlan_netdev_ether_ops = {
d342894c 2549 .ndo_init = vxlan_init,
ebf4063e 2550 .ndo_uninit = vxlan_uninit,
d342894c 2551 .ndo_open = vxlan_open,
2552 .ndo_stop = vxlan_stop,
2553 .ndo_start_xmit = vxlan_xmit,
e8171045 2554 .ndo_get_stats64 = ip_tunnel_get_stats64,
d342894c 2555 .ndo_set_rx_mode = vxlan_set_multicast_list,
345010b5 2556 .ndo_change_mtu = vxlan_change_mtu,
d342894c 2557 .ndo_validate_addr = eth_validate_addr,
2558 .ndo_set_mac_address = eth_mac_addr,
2559 .ndo_fdb_add = vxlan_fdb_add,
2560 .ndo_fdb_del = vxlan_fdb_delete,
2561 .ndo_fdb_dump = vxlan_fdb_dump,
fc4099f1 2562 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
d342894c 2563};
2564
e1e5314d
JB
2565static const struct net_device_ops vxlan_netdev_raw_ops = {
2566 .ndo_init = vxlan_init,
2567 .ndo_uninit = vxlan_uninit,
2568 .ndo_open = vxlan_open,
2569 .ndo_stop = vxlan_stop,
2570 .ndo_start_xmit = vxlan_xmit,
2571 .ndo_get_stats64 = ip_tunnel_get_stats64,
2572 .ndo_change_mtu = vxlan_change_mtu,
2573 .ndo_fill_metadata_dst = vxlan_fill_metadata_dst,
2574};
2575
d342894c 2576/* Info for udev, that this is a virtual tunnel endpoint */
2577static struct device_type vxlan_type = {
2578 .name = "vxlan",
2579};
2580
e5de25dc 2581/* Calls the ndo_udp_tunnel_add of the caller in order to
35e42379 2582 * supply the listening VXLAN udp ports. Callers are expected
e5de25dc 2583 * to implement the ndo_udp_tunnel_add.
53cf5275 2584 */
b7aade15 2585static void vxlan_push_rx_ports(struct net_device *dev)
53cf5275
JG
2586{
2587 struct vxlan_sock *vs;
2588 struct net *net = dev_net(dev);
2589 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
35e42379 2590 unsigned int i;
53cf5275
JG
2591
2592 spin_lock(&vn->sock_lock);
2593 for (i = 0; i < PORT_HASH_SIZE; ++i) {
e7b3db5e
AD
2594 hlist_for_each_entry_rcu(vs, &vn->sock_list[i], hlist)
2595 udp_tunnel_push_rx_port(dev, vs->sock,
b9adcd69
AD
2596 (vs->flags & VXLAN_F_GPE) ?
2597 UDP_TUNNEL_TYPE_VXLAN_GPE :
e7b3db5e 2598 UDP_TUNNEL_TYPE_VXLAN);
53cf5275
JG
2599 }
2600 spin_unlock(&vn->sock_lock);
2601}
53cf5275 2602
d342894c 2603/* Initialize the device structure. */
2604static void vxlan_setup(struct net_device *dev)
2605{
2606 struct vxlan_dev *vxlan = netdev_priv(dev);
31fec5aa 2607 unsigned int h;
d342894c 2608
65226ef8
JB
2609 eth_hw_addr_random(dev);
2610 ether_setup(dev);
2611
cf124db5 2612 dev->needs_free_netdev = true;
d342894c 2613 SET_NETDEV_DEVTYPE(dev, &vxlan_type);
2614
d342894c 2615 dev->features |= NETIF_F_LLTX;
d6727fe3 2616 dev->features |= NETIF_F_SG | NETIF_F_HW_CSUM;
0afb1666 2617 dev->features |= NETIF_F_RXCSUM;
05c0db08 2618 dev->features |= NETIF_F_GSO_SOFTWARE;
0afb1666 2619
1eaa8178 2620 dev->vlan_features = dev->features;
0afb1666 2621 dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_RXCSUM;
05c0db08 2622 dev->hw_features |= NETIF_F_GSO_SOFTWARE;
02875878 2623 netif_keep_dst(dev);
0c867c9b 2624 dev->priv_flags |= IFF_NO_QUEUE;
d342894c 2625
553675fb 2626 INIT_LIST_HEAD(&vxlan->next);
d342894c 2627 spin_lock_init(&vxlan->hash_lock);
2628
2629 init_timer_deferrable(&vxlan->age_timer);
2630 vxlan->age_timer.function = vxlan_cleanup;
2631 vxlan->age_timer.data = (unsigned long) vxlan;
2632
0dfbdf41 2633 vxlan->cfg.dst_port = htons(vxlan_port);
05f47d69 2634
d342894c 2635 vxlan->dev = dev;
2636
58ce31cc
TH
2637 gro_cells_init(&vxlan->gro_cells, dev);
2638
d342894c 2639 for (h = 0; h < FDB_HASH_SIZE; ++h)
2640 INIT_HLIST_HEAD(&vxlan->fdb_head[h]);
2641}
2642
0c867c9b
JB
2643static void vxlan_ether_setup(struct net_device *dev)
2644{
0c867c9b
JB
2645 dev->priv_flags &= ~IFF_TX_SKB_SHARING;
2646 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
2647 dev->netdev_ops = &vxlan_netdev_ether_ops;
2648}
2649
e1e5314d
JB
2650static void vxlan_raw_setup(struct net_device *dev)
2651{
65226ef8 2652 dev->header_ops = NULL;
e1e5314d
JB
2653 dev->type = ARPHRD_NONE;
2654 dev->hard_header_len = 0;
2655 dev->addr_len = 0;
e1e5314d
JB
2656 dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
2657 dev->netdev_ops = &vxlan_netdev_raw_ops;
2658}
2659
d342894c 2660static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
2661 [IFLA_VXLAN_ID] = { .type = NLA_U32 },
5d174dd8 2662 [IFLA_VXLAN_GROUP] = { .len = FIELD_SIZEOF(struct iphdr, daddr) },
e4c7ed41 2663 [IFLA_VXLAN_GROUP6] = { .len = sizeof(struct in6_addr) },
d342894c 2664 [IFLA_VXLAN_LINK] = { .type = NLA_U32 },
2665 [IFLA_VXLAN_LOCAL] = { .len = FIELD_SIZEOF(struct iphdr, saddr) },
e4c7ed41 2666 [IFLA_VXLAN_LOCAL6] = { .len = sizeof(struct in6_addr) },
d342894c 2667 [IFLA_VXLAN_TOS] = { .type = NLA_U8 },
2668 [IFLA_VXLAN_TTL] = { .type = NLA_U8 },
e7f70af1 2669 [IFLA_VXLAN_LABEL] = { .type = NLA_U32 },
d342894c 2670 [IFLA_VXLAN_LEARNING] = { .type = NLA_U8 },
2671 [IFLA_VXLAN_AGEING] = { .type = NLA_U32 },
2672 [IFLA_VXLAN_LIMIT] = { .type = NLA_U32 },
05f47d69 2673 [IFLA_VXLAN_PORT_RANGE] = { .len = sizeof(struct ifla_vxlan_port_range) },
e4f67add
DS
2674 [IFLA_VXLAN_PROXY] = { .type = NLA_U8 },
2675 [IFLA_VXLAN_RSC] = { .type = NLA_U8 },
2676 [IFLA_VXLAN_L2MISS] = { .type = NLA_U8 },
2677 [IFLA_VXLAN_L3MISS] = { .type = NLA_U8 },
f8a9b1bc 2678 [IFLA_VXLAN_COLLECT_METADATA] = { .type = NLA_U8 },
823aa873 2679 [IFLA_VXLAN_PORT] = { .type = NLA_U16 },
5c91ae08
TH
2680 [IFLA_VXLAN_UDP_CSUM] = { .type = NLA_U8 },
2681 [IFLA_VXLAN_UDP_ZERO_CSUM6_TX] = { .type = NLA_U8 },
2682 [IFLA_VXLAN_UDP_ZERO_CSUM6_RX] = { .type = NLA_U8 },
dfd8645e
TH
2683 [IFLA_VXLAN_REMCSUM_TX] = { .type = NLA_U8 },
2684 [IFLA_VXLAN_REMCSUM_RX] = { .type = NLA_U8 },
3511494c 2685 [IFLA_VXLAN_GBP] = { .type = NLA_FLAG, },
e1e5314d 2686 [IFLA_VXLAN_GPE] = { .type = NLA_FLAG, },
0ace2ca8 2687 [IFLA_VXLAN_REMCSUM_NOPARTIAL] = { .type = NLA_FLAG },
d342894c 2688};
2689
2690static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[])
2691{
2692 if (tb[IFLA_ADDRESS]) {
2693 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
2694 pr_debug("invalid link address (not ethernet)\n");
2695 return -EINVAL;
2696 }
2697
2698 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
2699 pr_debug("invalid all zero ethernet address\n");
2700 return -EADDRNOTAVAIL;
2701 }
2702 }
2703
2704 if (!data)
2705 return -EINVAL;
2706
2707 if (data[IFLA_VXLAN_ID]) {
2708 __u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
4e37d691 2709 if (id >= VXLAN_N_VID)
d342894c 2710 return -ERANGE;
2711 }
2712
05f47d69 2713 if (data[IFLA_VXLAN_PORT_RANGE]) {
2714 const struct ifla_vxlan_port_range *p
2715 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
2716
2717 if (ntohs(p->high) < ntohs(p->low)) {
2718 pr_debug("port range %u .. %u not valid\n",
2719 ntohs(p->low), ntohs(p->high));
2720 return -EINVAL;
2721 }
2722 }
2723
d342894c 2724 return 0;
2725}
2726
1b13c97f
YB
2727static void vxlan_get_drvinfo(struct net_device *netdev,
2728 struct ethtool_drvinfo *drvinfo)
2729{
2730 strlcpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
2731 strlcpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
2732}
2733
2734static const struct ethtool_ops vxlan_ethtool_ops = {
2735 .get_drvinfo = vxlan_get_drvinfo,
2736 .get_link = ethtool_op_get_link,
2737};
2738
3ee64f39
TH
2739static struct socket *vxlan_create_sock(struct net *net, bool ipv6,
2740 __be16 port, u32 flags)
553675fb 2741{
e4c7ed41 2742 struct socket *sock;
3ee64f39
TH
2743 struct udp_port_cfg udp_conf;
2744 int err;
e4c7ed41 2745
3ee64f39 2746 memset(&udp_conf, 0, sizeof(udp_conf));
e4c7ed41 2747
3ee64f39
TH
2748 if (ipv6) {
2749 udp_conf.family = AF_INET6;
3ee64f39 2750 udp_conf.use_udp6_rx_checksums =
3dc2b6a8 2751 !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
a43a9ef6 2752 udp_conf.ipv6_v6only = 1;
3ee64f39
TH
2753 } else {
2754 udp_conf.family = AF_INET;
e4c7ed41 2755 }
e4c7ed41 2756
3ee64f39 2757 udp_conf.local_udp_port = port;
553675fb 2758
3ee64f39
TH
2759 /* Open UDP socket */
2760 err = udp_sock_create(net, &udp_conf, &sock);
2761 if (err < 0)
2762 return ERR_PTR(err);
e4c7ed41 2763
39deb2c7 2764 return sock;
e4c7ed41
CW
2765}
2766
2767/* Create new listen socket if needed */
b1be00a6
JB
2768static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
2769 __be16 port, u32 flags)
e4c7ed41
CW
2770{
2771 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2772 struct vxlan_sock *vs;
2773 struct socket *sock;
e4c7ed41 2774 unsigned int h;
acbf74a7 2775 struct udp_tunnel_sock_cfg tunnel_cfg;
e4c7ed41 2776
dc01e7d3 2777 vs = kzalloc(sizeof(*vs), GFP_KERNEL);
e4c7ed41
CW
2778 if (!vs)
2779 return ERR_PTR(-ENOMEM);
2780
2781 for (h = 0; h < VNI_HASH_SIZE; ++h)
2782 INIT_HLIST_HEAD(&vs->vni_list[h]);
2783
3ee64f39 2784 sock = vxlan_create_sock(net, ipv6, port, flags);
39deb2c7 2785 if (IS_ERR(sock)) {
553675fb 2786 kfree(vs);
e50fddc8 2787 return ERR_CAST(sock);
553675fb 2788 }
e4c7ed41
CW
2789
2790 vs->sock = sock;
9c2e24e1 2791 atomic_set(&vs->refcnt, 1);
af33c1ad 2792 vs->flags = (flags & VXLAN_F_RCV_FLAGS);
553675fb 2793
9c2e24e1
PS
2794 spin_lock(&vn->sock_lock);
2795 hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
e7b3db5e 2796 udp_tunnel_notify_add_rx_port(sock,
b9adcd69
AD
2797 (vs->flags & VXLAN_F_GPE) ?
2798 UDP_TUNNEL_TYPE_VXLAN_GPE :
e7b3db5e 2799 UDP_TUNNEL_TYPE_VXLAN);
9c2e24e1 2800 spin_unlock(&vn->sock_lock);
553675fb 2801
2802 /* Mark socket as an encapsulation socket. */
5602c48c 2803 memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
acbf74a7
AZ
2804 tunnel_cfg.sk_user_data = vs;
2805 tunnel_cfg.encap_type = 1;
f2d1968e 2806 tunnel_cfg.encap_rcv = vxlan_rcv;
acbf74a7 2807 tunnel_cfg.encap_destroy = NULL;
5602c48c
TH
2808 tunnel_cfg.gro_receive = vxlan_gro_receive;
2809 tunnel_cfg.gro_complete = vxlan_gro_complete;
acbf74a7
AZ
2810
2811 setup_udp_tunnel_sock(net, sock, &tunnel_cfg);
e4c7ed41 2812
9c2e24e1
PS
2813 return vs;
2814}
2815
b1be00a6 2816static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
9c2e24e1 2817{
205f356d
JB
2818 struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2819 struct vxlan_sock *vs = NULL;
9c2e24e1 2820
205f356d 2821 if (!vxlan->cfg.no_share) {
56ef9c90 2822 spin_lock(&vn->sock_lock);
205f356d
JB
2823 vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2824 vxlan->cfg.dst_port, vxlan->flags);
2825 if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
56ef9c90 2826 spin_unlock(&vn->sock_lock);
205f356d 2827 return -EBUSY;
56ef9c90
MRL
2828 }
2829 spin_unlock(&vn->sock_lock);
2830 }
205f356d 2831 if (!vs)
b1be00a6
JB
2832 vs = vxlan_socket_create(vxlan->net, ipv6,
2833 vxlan->cfg.dst_port, vxlan->flags);
205f356d
JB
2834 if (IS_ERR(vs))
2835 return PTR_ERR(vs);
b1be00a6
JB
2836#if IS_ENABLED(CONFIG_IPV6)
2837 if (ipv6)
c6fcc4fc 2838 rcu_assign_pointer(vxlan->vn6_sock, vs);
b1be00a6
JB
2839 else
2840#endif
c6fcc4fc 2841 rcu_assign_pointer(vxlan->vn4_sock, vs);
205f356d
JB
2842 vxlan_vs_add_dev(vs, vxlan);
2843 return 0;
553675fb 2844}
2845
b1be00a6
JB
2846static int vxlan_sock_add(struct vxlan_dev *vxlan)
2847{
b1be00a6 2848 bool metadata = vxlan->flags & VXLAN_F_COLLECT_METADATA;
d074bf96
JB
2849 bool ipv6 = vxlan->flags & VXLAN_F_IPV6 || metadata;
2850 bool ipv4 = !ipv6 || metadata;
b1be00a6
JB
2851 int ret = 0;
2852
c6fcc4fc 2853 RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
b1be00a6 2854#if IS_ENABLED(CONFIG_IPV6)
c6fcc4fc 2855 RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
d074bf96 2856 if (ipv6) {
b1be00a6 2857 ret = __vxlan_sock_add(vxlan, true);
d074bf96
JB
2858 if (ret < 0 && ret != -EAFNOSUPPORT)
2859 ipv4 = false;
2860 }
b1be00a6 2861#endif
d074bf96 2862 if (ipv4)
b1be00a6
JB
2863 ret = __vxlan_sock_add(vxlan, false);
2864 if (ret < 0)
2865 vxlan_sock_release(vxlan);
2866 return ret;
2867}
2868
0dfbdf41 2869static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
8bcdc4f3
RP
2870 struct vxlan_config *conf,
2871 bool changelink)
d342894c 2872{
33564bbb 2873 struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
07b9b37c 2874 struct vxlan_dev *vxlan = netdev_priv(dev), *tmp;
c7995c43 2875 struct vxlan_rdst *dst = &vxlan->default_dst;
b1be00a6 2876 unsigned short needed_headroom = ETH_HLEN;
e4c7ed41 2877 bool use_ipv6 = false;
0dfbdf41 2878 __be16 default_port = vxlan->cfg.dst_port;
7e059158 2879 struct net_device *lowerdev = NULL;
d342894c 2880
8bcdc4f3
RP
2881 if (!changelink) {
2882 if (conf->flags & VXLAN_F_GPE) {
2883 /* For now, allow GPE only together with
2884 * COLLECT_METADATA. This can be relaxed later; in such
2885 * case, the other side of the PtP link will have to be
2886 * provided.
2887 */
2888 if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
2889 !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
2890 pr_info("unsupported combination of extensions\n");
2891 return -EINVAL;
2892 }
2893 vxlan_raw_setup(dev);
2894 } else {
2895 vxlan_ether_setup(dev);
3555621d 2896 }
e1e5314d 2897
8bcdc4f3
RP
2898 /* MTU range: 68 - 65535 */
2899 dev->min_mtu = ETH_MIN_MTU;
2900 dev->max_mtu = ETH_MAX_MTU;
2901 vxlan->net = src_net;
e1e5314d 2902 }
0c867c9b 2903
0dfbdf41 2904 dst->remote_vni = conf->vni;
d342894c 2905
0dfbdf41 2906 memcpy(&dst->remote_ip, &conf->remote_ip, sizeof(conf->remote_ip));
d342894c 2907
0dfbdf41
TG
2908 /* Unless IPv6 is explicitly requested, assume IPv4 */
2909 if (!dst->remote_ip.sa.sa_family)
2910 dst->remote_ip.sa.sa_family = AF_INET;
e4c7ed41 2911
0dfbdf41 2912 if (dst->remote_ip.sa.sa_family == AF_INET6 ||
057ba29b
JB
2913 vxlan->cfg.saddr.sa.sa_family == AF_INET6) {
2914 if (!IS_ENABLED(CONFIG_IPV6))
2915 return -EPFNOSUPPORT;
e4c7ed41 2916 use_ipv6 = true;
b1be00a6 2917 vxlan->flags |= VXLAN_F_IPV6;
057ba29b 2918 }
d342894c 2919
e7f70af1
DB
2920 if (conf->label && !use_ipv6) {
2921 pr_info("label only supported in use with IPv6\n");
2922 return -EINVAL;
2923 }
2924
8bcdc4f3
RP
2925 if (conf->remote_ifindex &&
2926 conf->remote_ifindex != vxlan->cfg.remote_ifindex) {
7e059158 2927 lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
0dfbdf41 2928 dst->remote_ifindex = conf->remote_ifindex;
34e02aa1 2929
2930 if (!lowerdev) {
8bcdc4f3
RP
2931 pr_info("ifindex %d does not exist\n",
2932 dst->remote_ifindex);
34e02aa1 2933 return -ENODEV;
2934 }
d342894c 2935
e4c7ed41
CW
2936#if IS_ENABLED(CONFIG_IPV6)
2937 if (use_ipv6) {
2938 struct inet6_dev *idev = __in6_dev_get(lowerdev);
2939 if (idev && idev->cnf.disable_ipv6) {
2940 pr_info("IPv6 is disabled via sysctl\n");
2941 return -EPERM;
2942 }
e4c7ed41
CW
2943 }
2944#endif
2945
0dfbdf41 2946 if (!conf->mtu)
91572088
JW
2947 dev->mtu = lowerdev->mtu -
2948 (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
1ba56fb4 2949
b1be00a6 2950 needed_headroom = lowerdev->hard_header_len;
8bcdc4f3
RP
2951 } else if (!conf->remote_ifindex &&
2952 vxlan_addr_multicast(&dst->remote_ip)) {
9b4cdd51
JB
2953 pr_info("multicast destination requires interface to be specified\n");
2954 return -EINVAL;
9dc2ad10 2955 }
d342894c 2956
d6acfeb1
FM
2957 if (lowerdev) {
2958 dev->gso_max_size = lowerdev->gso_max_size;
2959 dev->gso_max_segs = lowerdev->gso_max_segs;
2960 }
2961
7e059158 2962 if (conf->mtu) {
91572088
JW
2963 int max_mtu = ETH_MAX_MTU;
2964
2965 if (lowerdev)
2966 max_mtu = lowerdev->mtu;
2967
2968 max_mtu -= (use_ipv6 ? VXLAN6_HEADROOM : VXLAN_HEADROOM);
2969
2970 if (conf->mtu < dev->min_mtu || conf->mtu > dev->max_mtu)
2971 return -EINVAL;
2972
2973 dev->mtu = conf->mtu;
2974
2975 if (conf->mtu > max_mtu)
2976 dev->mtu = max_mtu;
7e059158
DW
2977 }
2978
b1be00a6
JB
2979 if (use_ipv6 || conf->flags & VXLAN_F_COLLECT_METADATA)
2980 needed_headroom += VXLAN6_HEADROOM;
2981 else
2982 needed_headroom += VXLAN_HEADROOM;
2983 dev->needed_headroom = needed_headroom;
2984
0dfbdf41 2985 memcpy(&vxlan->cfg, conf, sizeof(*conf));
e1e5314d
JB
2986 if (!vxlan->cfg.dst_port) {
2987 if (conf->flags & VXLAN_F_GPE)
d5ff72d9 2988 vxlan->cfg.dst_port = htons(4790); /* IANA VXLAN-GPE port */
e1e5314d
JB
2989 else
2990 vxlan->cfg.dst_port = default_port;
2991 }
0dfbdf41
TG
2992 vxlan->flags |= conf->flags;
2993
2994 if (!vxlan->cfg.age_interval)
2995 vxlan->cfg.age_interval = FDB_AGE_DEFAULT;
2996
8bcdc4f3
RP
2997 if (changelink)
2998 return 0;
2999
07b9b37c
ND
3000 list_for_each_entry(tmp, &vn->vxlan_list, next) {
3001 if (tmp->cfg.vni == conf->vni &&
3002 (tmp->default_dst.remote_ip.sa.sa_family == AF_INET6 ||
3003 tmp->cfg.saddr.sa.sa_family == AF_INET6) == use_ipv6 &&
3004 tmp->cfg.dst_port == vxlan->cfg.dst_port &&
3005 (tmp->flags & VXLAN_F_RCV_FLAGS) ==
3555621d
JB
3006 (vxlan->flags & VXLAN_F_RCV_FLAGS)) {
3007 pr_info("duplicate VNI %u\n", be32_to_cpu(conf->vni));
3008 return -EEXIST;
3009 }
07b9b37c 3010 }
0dfbdf41 3011
0dfbdf41
TG
3012 return 0;
3013}
3014
c80498e3
ND
3015static int __vxlan_dev_create(struct net *net, struct net_device *dev,
3016 struct vxlan_config *conf)
3017{
3018 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3019 struct vxlan_dev *vxlan = netdev_priv(dev);
3020 int err;
3021
3022 err = vxlan_dev_configure(net, dev, conf, false);
3023 if (err)
3024 return err;
3025
3026 dev->ethtool_ops = &vxlan_ethtool_ops;
3027
3028 /* create an fdb entry for a valid default destination */
3029 if (!vxlan_addr_any(&vxlan->default_dst.remote_ip)) {
3030 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3031 &vxlan->default_dst.remote_ip,
3032 NUD_REACHABLE | NUD_PERMANENT,
3033 NLM_F_EXCL | NLM_F_CREATE,
3034 vxlan->cfg.dst_port,
3035 vxlan->default_dst.remote_vni,
3036 vxlan->default_dst.remote_vni,
3037 vxlan->default_dst.remote_ifindex,
3038 NTF_SELF);
3039 if (err)
3040 return err;
3041 }
3042
3043 err = register_netdevice(dev);
3044 if (err) {
3045 vxlan_fdb_delete_default(vxlan, vxlan->default_dst.remote_vni);
3046 return err;
3047 }
3048
3049 list_add(&vxlan->next, &vn->vxlan_list);
3050 return 0;
3051}
3052
8bcdc4f3
RP
3053static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
3054 struct net_device *dev, struct vxlan_config *conf,
3055 bool changelink)
0dfbdf41 3056{
8bcdc4f3 3057 struct vxlan_dev *vxlan = netdev_priv(dev);
0dfbdf41 3058
8bcdc4f3 3059 memset(conf, 0, sizeof(*conf));
e277de5f 3060
8bcdc4f3
RP
3061 /* if changelink operation, start with old existing cfg */
3062 if (changelink)
3063 memcpy(conf, &vxlan->cfg, sizeof(*conf));
3064
3065 if (data[IFLA_VXLAN_ID]) {
3066 __be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3067
3068 if (changelink && (vni != conf->vni))
3069 return -EOPNOTSUPP;
3070 conf->vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
3071 }
0dfbdf41
TG
3072
3073 if (data[IFLA_VXLAN_GROUP]) {
8bcdc4f3 3074 conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
0dfbdf41
TG
3075 } else if (data[IFLA_VXLAN_GROUP6]) {
3076 if (!IS_ENABLED(CONFIG_IPV6))
3077 return -EPFNOSUPPORT;
3078
8bcdc4f3
RP
3079 conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
3080 conf->remote_ip.sa.sa_family = AF_INET6;
0dfbdf41
TG
3081 }
3082
3083 if (data[IFLA_VXLAN_LOCAL]) {
8bcdc4f3
RP
3084 conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
3085 conf->saddr.sa.sa_family = AF_INET;
0dfbdf41
TG
3086 } else if (data[IFLA_VXLAN_LOCAL6]) {
3087 if (!IS_ENABLED(CONFIG_IPV6))
3088 return -EPFNOSUPPORT;
3089
3090 /* TODO: respect scope id */
8bcdc4f3
RP
3091 conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
3092 conf->saddr.sa.sa_family = AF_INET6;
0dfbdf41
TG
3093 }
3094
3095 if (data[IFLA_VXLAN_LINK])
8bcdc4f3 3096 conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
0dfbdf41 3097
d342894c 3098 if (data[IFLA_VXLAN_TOS])
8bcdc4f3 3099 conf->tos = nla_get_u8(data[IFLA_VXLAN_TOS]);
d342894c 3100
afb97186 3101 if (data[IFLA_VXLAN_TTL])
8bcdc4f3 3102 conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
afb97186 3103
e7f70af1 3104 if (data[IFLA_VXLAN_LABEL])
8bcdc4f3 3105 conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
e7f70af1
DB
3106 IPV6_FLOWLABEL_MASK;
3107
8bcdc4f3
RP
3108 if (data[IFLA_VXLAN_LEARNING]) {
3109 if (nla_get_u8(data[IFLA_VXLAN_LEARNING])) {
3110 conf->flags |= VXLAN_F_LEARN;
3111 } else {
3112 conf->flags &= ~VXLAN_F_LEARN;
3113 vxlan->flags &= ~VXLAN_F_LEARN;
3114 }
3115 } else if (!changelink) {
3116 /* default to learn on a new device */
3117 conf->flags |= VXLAN_F_LEARN;
3118 }
d342894c 3119
8bcdc4f3
RP
3120 if (data[IFLA_VXLAN_AGEING]) {
3121 if (changelink)
3122 return -EOPNOTSUPP;
3123 conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
3124 }
d342894c 3125
8bcdc4f3
RP
3126 if (data[IFLA_VXLAN_PROXY]) {
3127 if (changelink)
3128 return -EOPNOTSUPP;
3129 if (nla_get_u8(data[IFLA_VXLAN_PROXY]))
3130 conf->flags |= VXLAN_F_PROXY;
3131 }
e4f67add 3132
8bcdc4f3
RP
3133 if (data[IFLA_VXLAN_RSC]) {
3134 if (changelink)
3135 return -EOPNOTSUPP;
3136 if (nla_get_u8(data[IFLA_VXLAN_RSC]))
3137 conf->flags |= VXLAN_F_RSC;
3138 }
e4f67add 3139
8bcdc4f3
RP
3140 if (data[IFLA_VXLAN_L2MISS]) {
3141 if (changelink)
3142 return -EOPNOTSUPP;
3143 if (nla_get_u8(data[IFLA_VXLAN_L2MISS]))
3144 conf->flags |= VXLAN_F_L2MISS;
3145 }
e4f67add 3146
8bcdc4f3
RP
3147 if (data[IFLA_VXLAN_L3MISS]) {
3148 if (changelink)
3149 return -EOPNOTSUPP;
3150 if (nla_get_u8(data[IFLA_VXLAN_L3MISS]))
3151 conf->flags |= VXLAN_F_L3MISS;
3152 }
e4f67add 3153
8bcdc4f3
RP
3154 if (data[IFLA_VXLAN_LIMIT]) {
3155 if (changelink)
3156 return -EOPNOTSUPP;
3157 conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
3158 }
d342894c 3159
8bcdc4f3
RP
3160 if (data[IFLA_VXLAN_COLLECT_METADATA]) {
3161 if (changelink)
3162 return -EOPNOTSUPP;
3163 if (nla_get_u8(data[IFLA_VXLAN_COLLECT_METADATA]))
3164 conf->flags |= VXLAN_F_COLLECT_METADATA;
3165 }
f8a9b1bc 3166
05f47d69 3167 if (data[IFLA_VXLAN_PORT_RANGE]) {
8bcdc4f3
RP
3168 if (!changelink) {
3169 const struct ifla_vxlan_port_range *p
3170 = nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3171 conf->port_min = ntohs(p->low);
3172 conf->port_max = ntohs(p->high);
3173 } else {
3174 return -EOPNOTSUPP;
3175 }
05f47d69 3176 }
3177
8bcdc4f3
RP
3178 if (data[IFLA_VXLAN_PORT]) {
3179 if (changelink)
3180 return -EOPNOTSUPP;
3181 conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
3182 }
823aa873 3183
8bcdc4f3
RP
3184 if (data[IFLA_VXLAN_UDP_CSUM]) {
3185 if (changelink)
3186 return -EOPNOTSUPP;
3187 if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
3188 conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
3189 }
359a0ea9 3190
8bcdc4f3
RP
3191 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
3192 if (changelink)
3193 return -EOPNOTSUPP;
3194 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]))
3195 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_TX;
3196 }
359a0ea9 3197
8bcdc4f3
RP
3198 if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
3199 if (changelink)
3200 return -EOPNOTSUPP;
3201 if (nla_get_u8(data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]))
3202 conf->flags |= VXLAN_F_UDP_ZERO_CSUM6_RX;
3203 }
359a0ea9 3204
8bcdc4f3
RP
3205 if (data[IFLA_VXLAN_REMCSUM_TX]) {
3206 if (changelink)
3207 return -EOPNOTSUPP;
3208 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_TX]))
3209 conf->flags |= VXLAN_F_REMCSUM_TX;
3210 }
dfd8645e 3211
8bcdc4f3
RP
3212 if (data[IFLA_VXLAN_REMCSUM_RX]) {
3213 if (changelink)
3214 return -EOPNOTSUPP;
3215 if (nla_get_u8(data[IFLA_VXLAN_REMCSUM_RX]))
3216 conf->flags |= VXLAN_F_REMCSUM_RX;
3217 }
3218
3219 if (data[IFLA_VXLAN_GBP]) {
3220 if (changelink)
3221 return -EOPNOTSUPP;
3222 conf->flags |= VXLAN_F_GBP;
3223 }
3224
3225 if (data[IFLA_VXLAN_GPE]) {
3226 if (changelink)
3227 return -EOPNOTSUPP;
3228 conf->flags |= VXLAN_F_GPE;
3229 }
3230
3231 if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
3232 if (changelink)
3233 return -EOPNOTSUPP;
3234 conf->flags |= VXLAN_F_REMCSUM_NOPARTIAL;
3235 }
3236
3237 if (tb[IFLA_MTU]) {
3238 if (changelink)
3239 return -EOPNOTSUPP;
3240 conf->mtu = nla_get_u32(tb[IFLA_MTU]);
3241 }
3242
3243 return 0;
3244}
3245
3246static int vxlan_newlink(struct net *src_net, struct net_device *dev,
3247 struct nlattr *tb[], struct nlattr *data[])
3248{
8bcdc4f3
RP
3249 struct vxlan_config conf;
3250 int err;
3251
3252 err = vxlan_nl2conf(tb, data, dev, &conf, false);
3253 if (err)
3254 return err;
3255
c80498e3 3256 return __vxlan_dev_create(src_net, dev, &conf);
8bcdc4f3 3257}
dfd8645e 3258
8bcdc4f3
RP
3259static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
3260 struct nlattr *data[])
3261{
3262 struct vxlan_dev *vxlan = netdev_priv(dev);
3263 struct vxlan_rdst *dst = &vxlan->default_dst;
3264 struct vxlan_rdst old_dst;
3265 struct vxlan_config conf;
3266 int err;
3267
3268 err = vxlan_nl2conf(tb, data,
3269 dev, &conf, true);
3270 if (err)
3271 return err;
3511494c 3272
8bcdc4f3 3273 memcpy(&old_dst, dst, sizeof(struct vxlan_rdst));
e1e5314d 3274
8bcdc4f3
RP
3275 err = vxlan_dev_configure(vxlan->net, dev, &conf, true);
3276 if (err)
3277 return err;
0ace2ca8 3278
8bcdc4f3
RP
3279 /* handle default dst entry */
3280 if (!vxlan_addr_equal(&dst->remote_ip, &old_dst.remote_ip)) {
3281 spin_lock_bh(&vxlan->hash_lock);
3282 if (!vxlan_addr_any(&old_dst.remote_ip))
3283 __vxlan_fdb_delete(vxlan, all_zeros_mac,
3284 old_dst.remote_ip,
3285 vxlan->cfg.dst_port,
3286 old_dst.remote_vni,
3287 old_dst.remote_vni,
3288 old_dst.remote_ifindex, 0);
3289
3290 if (!vxlan_addr_any(&dst->remote_ip)) {
3291 err = vxlan_fdb_create(vxlan, all_zeros_mac,
3292 &dst->remote_ip,
3293 NUD_REACHABLE | NUD_PERMANENT,
3294 NLM_F_CREATE | NLM_F_APPEND,
3295 vxlan->cfg.dst_port,
3296 dst->remote_vni,
3297 dst->remote_vni,
3298 dst->remote_ifindex,
3299 NTF_SELF);
3300 if (err) {
3301 spin_unlock_bh(&vxlan->hash_lock);
3302 return err;
3303 }
3304 }
3305 spin_unlock_bh(&vxlan->hash_lock);
3306 }
ce577668 3307
8bcdc4f3 3308 return 0;
d342894c 3309}
3310
3311static void vxlan_dellink(struct net_device *dev, struct list_head *head)
3312{
3313 struct vxlan_dev *vxlan = netdev_priv(dev);
3314
8b3f9337
RP
3315 vxlan_flush(vxlan, true);
3316
58ce31cc 3317 gro_cells_destroy(&vxlan->gro_cells);
553675fb 3318 list_del(&vxlan->next);
d342894c 3319 unregister_netdevice_queue(dev, head);
3320}
3321
3322static size_t vxlan_get_size(const struct net_device *dev)
3323{
3324
3325 return nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_ID */
e4c7ed41 3326 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
d342894c 3327 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LINK */
e4c7ed41 3328 nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
d342894c 3329 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TTL */
3330 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_TOS */
e7f70af1 3331 nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
d342894c 3332 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LEARNING */
e4f67add
DS
3333 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_PROXY */
3334 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_RSC */
3335 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L2MISS */
3336 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_L3MISS */
da8b43c0 3337 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_COLLECT_METADATA */
d342894c 3338 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_AGEING */
3339 nla_total_size(sizeof(__u32)) + /* IFLA_VXLAN_LIMIT */
05f47d69 3340 nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
359a0ea9
TH
3341 nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
3342 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
3343 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
3344 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
dfd8645e
TH
3345 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
3346 nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
d342894c 3347 0;
3348}
3349
3350static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
3351{
3352 const struct vxlan_dev *vxlan = netdev_priv(dev);
c7995c43 3353 const struct vxlan_rdst *dst = &vxlan->default_dst;
05f47d69 3354 struct ifla_vxlan_port_range ports = {
0dfbdf41
TG
3355 .low = htons(vxlan->cfg.port_min),
3356 .high = htons(vxlan->cfg.port_max),
05f47d69 3357 };
d342894c 3358
54bfd872 3359 if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(dst->remote_vni)))
d342894c 3360 goto nla_put_failure;
3361
e4c7ed41
CW
3362 if (!vxlan_addr_any(&dst->remote_ip)) {
3363 if (dst->remote_ip.sa.sa_family == AF_INET) {
930345ea
JB
3364 if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
3365 dst->remote_ip.sin.sin_addr.s_addr))
e4c7ed41
CW
3366 goto nla_put_failure;
3367#if IS_ENABLED(CONFIG_IPV6)
3368 } else {
930345ea
JB
3369 if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
3370 &dst->remote_ip.sin6.sin6_addr))
e4c7ed41
CW
3371 goto nla_put_failure;
3372#endif
3373 }
3374 }
d342894c 3375
c7995c43 3376 if (dst->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, dst->remote_ifindex))
d342894c 3377 goto nla_put_failure;
3378
0dfbdf41
TG
3379 if (!vxlan_addr_any(&vxlan->cfg.saddr)) {
3380 if (vxlan->cfg.saddr.sa.sa_family == AF_INET) {
930345ea 3381 if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
0dfbdf41 3382 vxlan->cfg.saddr.sin.sin_addr.s_addr))
e4c7ed41
CW
3383 goto nla_put_failure;
3384#if IS_ENABLED(CONFIG_IPV6)
3385 } else {
930345ea 3386 if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
0dfbdf41 3387 &vxlan->cfg.saddr.sin6.sin6_addr))
e4c7ed41
CW
3388 goto nla_put_failure;
3389#endif
3390 }
3391 }
d342894c 3392
0dfbdf41
TG
3393 if (nla_put_u8(skb, IFLA_VXLAN_TTL, vxlan->cfg.ttl) ||
3394 nla_put_u8(skb, IFLA_VXLAN_TOS, vxlan->cfg.tos) ||
e7f70af1 3395 nla_put_be32(skb, IFLA_VXLAN_LABEL, vxlan->cfg.label) ||
e4f67add
DS
3396 nla_put_u8(skb, IFLA_VXLAN_LEARNING,
3397 !!(vxlan->flags & VXLAN_F_LEARN)) ||
3398 nla_put_u8(skb, IFLA_VXLAN_PROXY,
3399 !!(vxlan->flags & VXLAN_F_PROXY)) ||
3400 nla_put_u8(skb, IFLA_VXLAN_RSC, !!(vxlan->flags & VXLAN_F_RSC)) ||
3401 nla_put_u8(skb, IFLA_VXLAN_L2MISS,
3402 !!(vxlan->flags & VXLAN_F_L2MISS)) ||
3403 nla_put_u8(skb, IFLA_VXLAN_L3MISS,
3404 !!(vxlan->flags & VXLAN_F_L3MISS)) ||
da8b43c0
AS
3405 nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
3406 !!(vxlan->flags & VXLAN_F_COLLECT_METADATA)) ||
0dfbdf41
TG
3407 nla_put_u32(skb, IFLA_VXLAN_AGEING, vxlan->cfg.age_interval) ||
3408 nla_put_u32(skb, IFLA_VXLAN_LIMIT, vxlan->cfg.addrmax) ||
3409 nla_put_be16(skb, IFLA_VXLAN_PORT, vxlan->cfg.dst_port) ||
359a0ea9 3410 nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
6ceb31ca 3411 !(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
359a0ea9
TH
3412 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
3413 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
3414 nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
dfd8645e
TH
3415 !!(vxlan->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
3416 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
3417 !!(vxlan->flags & VXLAN_F_REMCSUM_TX)) ||
3418 nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
3419 !!(vxlan->flags & VXLAN_F_REMCSUM_RX)))
d342894c 3420 goto nla_put_failure;
3421
05f47d69 3422 if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
3423 goto nla_put_failure;
3424
3511494c
TG
3425 if (vxlan->flags & VXLAN_F_GBP &&
3426 nla_put_flag(skb, IFLA_VXLAN_GBP))
3427 goto nla_put_failure;
3428
e1e5314d
JB
3429 if (vxlan->flags & VXLAN_F_GPE &&
3430 nla_put_flag(skb, IFLA_VXLAN_GPE))
3431 goto nla_put_failure;
3432
0ace2ca8
TH
3433 if (vxlan->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
3434 nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
3435 goto nla_put_failure;
3436
d342894c 3437 return 0;
3438
3439nla_put_failure:
3440 return -EMSGSIZE;
3441}
3442
1728d4fa
ND
3443static struct net *vxlan_get_link_net(const struct net_device *dev)
3444{
3445 struct vxlan_dev *vxlan = netdev_priv(dev);
3446
3447 return vxlan->net;
3448}
3449
d342894c 3450static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
3451 .kind = "vxlan",
3452 .maxtype = IFLA_VXLAN_MAX,
3453 .policy = vxlan_policy,
3454 .priv_size = sizeof(struct vxlan_dev),
3455 .setup = vxlan_setup,
3456 .validate = vxlan_validate,
3457 .newlink = vxlan_newlink,
8bcdc4f3 3458 .changelink = vxlan_changelink,
d342894c 3459 .dellink = vxlan_dellink,
3460 .get_size = vxlan_get_size,
3461 .fill_info = vxlan_fill_info,
1728d4fa 3462 .get_link_net = vxlan_get_link_net,
d342894c 3463};
3464
cf5da330
ND
3465struct net_device *vxlan_dev_create(struct net *net, const char *name,
3466 u8 name_assign_type,
3467 struct vxlan_config *conf)
3468{
3469 struct nlattr *tb[IFLA_MAX + 1];
3470 struct net_device *dev;
3471 int err;
3472
3473 memset(&tb, 0, sizeof(tb));
3474
3475 dev = rtnl_create_link(net, name, name_assign_type,
3476 &vxlan_link_ops, tb);
3477 if (IS_ERR(dev))
3478 return dev;
3479
c80498e3 3480 err = __vxlan_dev_create(net, dev, conf);
cf5da330
ND
3481 if (err < 0) {
3482 free_netdev(dev);
3483 return ERR_PTR(err);
3484 }
3485
3486 err = rtnl_configure_link(dev, NULL);
3487 if (err < 0) {
3488 LIST_HEAD(list_kill);
3489
3490 vxlan_dellink(dev, &list_kill);
3491 unregister_netdevice_many(&list_kill);
3492 return ERR_PTR(err);
3493 }
3494
3495 return dev;
3496}
3497EXPORT_SYMBOL_GPL(vxlan_dev_create);
3498
acaf4e70
DB
3499static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
3500 struct net_device *dev)
3501{
3502 struct vxlan_dev *vxlan, *next;
3503 LIST_HEAD(list_kill);
3504
3505 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3506 struct vxlan_rdst *dst = &vxlan->default_dst;
3507
3508 /* In case we created vxlan device with carrier
3509 * and we loose the carrier due to module unload
3510 * we also need to remove vxlan device. In other
3511 * cases, it's not necessary and remote_ifindex
3512 * is 0 here, so no matches.
3513 */
3514 if (dst->remote_ifindex == dev->ifindex)
3515 vxlan_dellink(vxlan->dev, &list_kill);
3516 }
3517
3518 unregister_netdevice_many(&list_kill);
3519}
3520
b7aade15
HFS
3521static int vxlan_netdevice_event(struct notifier_block *unused,
3522 unsigned long event, void *ptr)
acaf4e70
DB
3523{
3524 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
783c1463 3525 struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
acaf4e70 3526
783c1463 3527 if (event == NETDEV_UNREGISTER)
acaf4e70 3528 vxlan_handle_lowerdev_unregister(vn, dev);
7c46a640 3529 else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
b7aade15 3530 vxlan_push_rx_ports(dev);
acaf4e70
DB
3531
3532 return NOTIFY_DONE;
3533}
3534
3535static struct notifier_block vxlan_notifier_block __read_mostly = {
b7aade15 3536 .notifier_call = vxlan_netdevice_event,
acaf4e70
DB
3537};
3538
d342894c 3539static __net_init int vxlan_init_net(struct net *net)
3540{
3541 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
31fec5aa 3542 unsigned int h;
d342894c 3543
553675fb 3544 INIT_LIST_HEAD(&vn->vxlan_list);
1c51a915 3545 spin_lock_init(&vn->sock_lock);
d342894c 3546
553675fb 3547 for (h = 0; h < PORT_HASH_SIZE; ++h)
3548 INIT_HLIST_HEAD(&vn->sock_list[h]);
d342894c 3549
3550 return 0;
3551}
3552
f01ec1c0
ND
3553static void __net_exit vxlan_exit_net(struct net *net)
3554{
3555 struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3556 struct vxlan_dev *vxlan, *next;
3557 struct net_device *dev, *aux;
3558 LIST_HEAD(list);
3559
3560 rtnl_lock();
3561 for_each_netdev_safe(net, dev, aux)
3562 if (dev->rtnl_link_ops == &vxlan_link_ops)
3563 unregister_netdevice_queue(dev, &list);
3564
3565 list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
3566 /* If vxlan->dev is in the same netns, it has already been added
3567 * to the list by the previous loop.
3568 */
58ce31cc
TH
3569 if (!net_eq(dev_net(vxlan->dev), net)) {
3570 gro_cells_destroy(&vxlan->gro_cells);
13c3ed6a 3571 unregister_netdevice_queue(vxlan->dev, &list);
58ce31cc 3572 }
f01ec1c0
ND
3573 }
3574
3575 unregister_netdevice_many(&list);
3576 rtnl_unlock();
3577}
3578
d342894c 3579static struct pernet_operations vxlan_net_ops = {
3580 .init = vxlan_init_net,
f01ec1c0 3581 .exit = vxlan_exit_net,
d342894c 3582 .id = &vxlan_net_id,
3583 .size = sizeof(struct vxlan_net),
3584};
3585
3586static int __init vxlan_init_module(void)
3587{
3588 int rc;
3589
3590 get_random_bytes(&vxlan_salt, sizeof(vxlan_salt));
3591
783c1463 3592 rc = register_pernet_subsys(&vxlan_net_ops);
d342894c 3593 if (rc)
3594 goto out1;
3595
acaf4e70 3596 rc = register_netdevice_notifier(&vxlan_notifier_block);
d342894c 3597 if (rc)
3598 goto out2;
3599
acaf4e70
DB
3600 rc = rtnl_link_register(&vxlan_link_ops);
3601 if (rc)
3602 goto out3;
d342894c 3603
acaf4e70
DB
3604 return 0;
3605out3:
3606 unregister_netdevice_notifier(&vxlan_notifier_block);
d342894c 3607out2:
783c1463 3608 unregister_pernet_subsys(&vxlan_net_ops);
d342894c 3609out1:
3610 return rc;
3611}
7332a13b 3612late_initcall(vxlan_init_module);
d342894c 3613
3614static void __exit vxlan_cleanup_module(void)
3615{
b7153984 3616 rtnl_link_unregister(&vxlan_link_ops);
acaf4e70 3617 unregister_netdevice_notifier(&vxlan_notifier_block);
783c1463
DB
3618 unregister_pernet_subsys(&vxlan_net_ops);
3619 /* rcu_barrier() is called by netns */
d342894c 3620}
3621module_exit(vxlan_cleanup_module);
3622
3623MODULE_LICENSE("GPL");
3624MODULE_VERSION(VXLAN_VERSION);
3b8df3c6 3625MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
ead5139a 3626MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
d342894c 3627MODULE_ALIAS_RTNL_LINK("vxlan");