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