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