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