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