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