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