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