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