xref: /linux/drivers/net/vxlan/vxlan_core.c (revision 0e59523b440c90ed1899476f32ad4e949282f43e)
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 = ipv6_neigh_lookup(dev, &msg->target);
2095 	if (n) {
2096 		struct vxlan_rdst *rdst = NULL;
2097 		u8 ha[ETH_ALEN] __aligned(2);
2098 		struct vxlan_fdb *f;
2099 		struct sk_buff *reply;
2100 
2101 		if (!(READ_ONCE(n->nud_state) & NUD_CONNECTED)) {
2102 			neigh_release(n);
2103 			goto out;
2104 		}
2105 
2106 		neigh_ha_snapshot(ha, n, n->dev);
2107 		f = vxlan_find_mac_tx(vxlan, cfg, ha, vni);
2108 		if (f)
2109 			rdst = first_remote_rcu(f);
2110 		if (rdst && vxlan_addr_any(&rdst->remote_ip)) {
2111 			/* bridge-local neighbor */
2112 			neigh_release(n);
2113 			goto out;
2114 		}
2115 
2116 		reply = vxlan_na_create(skb, n, ha,
2117 					!!(f ? f->flags & NTF_ROUTER : 0));
2118 
2119 		neigh_release(n);
2120 
2121 		if (reply == NULL)
2122 			goto out;
2123 
2124 		if (netif_rx(reply) == NET_RX_DROP) {
2125 			dev_dstats_rx_dropped(dev);
2126 			vxlan_vnifilter_count(vxlan, cfg, vni, NULL,
2127 					      VXLAN_VNI_STATS_RX_DROPS, 0);
2128 		}
2129 	} else if (cfg->flags & VXLAN_F_L3MISS) {
2130 		union vxlan_addr ipa = {
2131 			.sin6.sin6_addr = msg->target,
2132 			.sin6.sin6_family = AF_INET6,
2133 		};
2134 
2135 		vxlan_ip_miss(dev, &ipa);
2136 	}
2137 
2138 out:
2139 	rcu_read_unlock();
2140 	consume_skb(skb);
2141 	return NETDEV_TX_OK;
2142 }
2143 #endif
2144 
2145 static bool route_shortcircuit(struct net_device *dev, struct sk_buff *skb,
2146 			       const struct vxlan_config *cfg)
2147 {
2148 	struct neigh_table *tbl;
2149 	struct neighbour *n;
2150 
2151 	if (is_multicast_ether_addr(eth_hdr(skb)->h_dest))
2152 		return false;
2153 
2154 	n = NULL;
2155 	switch (ntohs(eth_hdr(skb)->h_proto)) {
2156 	case ETH_P_IP:
2157 	{
2158 		struct iphdr *pip;
2159 
2160 		if (!pskb_network_may_pull(skb, sizeof(struct iphdr)))
2161 			return false;
2162 
2163 		tbl = arp_table(dev_net(dev));
2164 		pip = ip_hdr(skb);
2165 		n = neigh_lookup(tbl, &pip->daddr, dev);
2166 		if (!n && (cfg->flags & VXLAN_F_L3MISS)) {
2167 			union vxlan_addr ipa = {
2168 				.sin.sin_addr.s_addr = pip->daddr,
2169 				.sin.sin_family = AF_INET,
2170 			};
2171 
2172 			vxlan_ip_miss(dev, &ipa);
2173 			return false;
2174 		}
2175 
2176 		break;
2177 	}
2178 #if IS_ENABLED(CONFIG_IPV6)
2179 	case ETH_P_IPV6:
2180 	{
2181 		struct ipv6hdr *pip6;
2182 
2183 		/* check if ipv6.disable=1 set during boot was set
2184 		 * during booting so nd_tbl is not initialized
2185 		 */
2186 		if (!ipv6_mod_enabled())
2187 			return false;
2188 		if (!pskb_network_may_pull(skb, sizeof(struct ipv6hdr)))
2189 			return false;
2190 
2191 		pip6 = ipv6_hdr(skb);
2192 		n = ipv6_neigh_lookup(dev, &pip6->daddr);
2193 		if (!n && (cfg->flags & VXLAN_F_L3MISS)) {
2194 			union vxlan_addr ipa = {
2195 				.sin6.sin6_addr = pip6->daddr,
2196 				.sin6.sin6_family = AF_INET6,
2197 			};
2198 
2199 			vxlan_ip_miss(dev, &ipa);
2200 			return false;
2201 		}
2202 
2203 		break;
2204 	}
2205 #endif
2206 	default:
2207 		return false;
2208 	}
2209 
2210 	if (n) {
2211 		u8 haddr[ETH_ALEN];
2212 		bool diff;
2213 
2214 		neigh_ha_snapshot(haddr, n, dev);
2215 		diff = !ether_addr_equal_unaligned(eth_hdr(skb)->h_dest, haddr);
2216 		if (diff) {
2217 			if (skb_cow_head(skb, 0)) {
2218 				neigh_release(n);
2219 				return false;
2220 			}
2221 			memcpy(eth_hdr(skb)->h_source, eth_hdr(skb)->h_dest,
2222 				dev->addr_len);
2223 			memcpy(eth_hdr(skb)->h_dest, haddr, dev->addr_len);
2224 		}
2225 		neigh_release(n);
2226 		return diff;
2227 	}
2228 
2229 	return false;
2230 }
2231 
2232 static int vxlan_build_gpe_hdr(struct vxlanhdr *vxh, __be16 protocol)
2233 {
2234 	struct vxlanhdr_gpe *gpe = (struct vxlanhdr_gpe *)vxh;
2235 
2236 	gpe->np_applied = 1;
2237 	gpe->next_protocol = tun_p_from_eth_p(protocol);
2238 	if (!gpe->next_protocol)
2239 		return -EPFNOSUPPORT;
2240 	return 0;
2241 }
2242 
2243 static int vxlan_build_skb(struct sk_buff *skb, struct dst_entry *dst,
2244 			   int iphdr_len, __be32 vni,
2245 			   struct vxlan_metadata *md, u32 vxflags,
2246 			   bool udp_sum)
2247 {
2248 	int type = udp_sum ? SKB_GSO_UDP_TUNNEL_CSUM : SKB_GSO_UDP_TUNNEL;
2249 	__be16 inner_protocol = htons(ETH_P_TEB);
2250 	struct vxlanhdr *vxh;
2251 	bool double_encap;
2252 	int min_headroom;
2253 	int err;
2254 
2255 	if ((vxflags & VXLAN_F_REMCSUM_TX) &&
2256 	    skb->ip_summed == CHECKSUM_PARTIAL) {
2257 		int csum_start = skb_checksum_start_offset(skb);
2258 
2259 		if (csum_start <= VXLAN_MAX_REMCSUM_START &&
2260 		    !(csum_start & VXLAN_RCO_SHIFT_MASK) &&
2261 		    (skb->csum_offset == offsetof(struct udphdr, check) ||
2262 		     skb->csum_offset == offsetof(struct tcphdr, check)))
2263 			type |= SKB_GSO_TUNNEL_REMCSUM;
2264 	}
2265 
2266 	min_headroom = LL_RESERVED_SPACE(dst->dev) + dst->header_len
2267 			+ VXLAN_HLEN + iphdr_len;
2268 
2269 	/* Need space for new headers (invalidates iph ptr) */
2270 	err = skb_cow_head(skb, min_headroom);
2271 	if (unlikely(err))
2272 		return err;
2273 
2274 	double_encap = udp_tunnel_handle_partial(skb);
2275 	err = iptunnel_handle_offloads(skb, type);
2276 	if (err)
2277 		return err;
2278 
2279 	vxh = __skb_push(skb, sizeof(*vxh));
2280 	vxh->vx_flags = VXLAN_HF_VNI;
2281 	vxh->vx_vni = vxlan_vni_field(vni);
2282 
2283 	if (type & SKB_GSO_TUNNEL_REMCSUM) {
2284 		unsigned int start;
2285 
2286 		start = skb_checksum_start_offset(skb) - sizeof(struct vxlanhdr);
2287 		vxh->vx_vni |= vxlan_compute_rco(start, skb->csum_offset);
2288 		vxh->vx_flags |= VXLAN_HF_RCO;
2289 
2290 		if (!skb_is_gso(skb)) {
2291 			skb->ip_summed = CHECKSUM_NONE;
2292 			skb->encapsulation = 0;
2293 		}
2294 	}
2295 
2296 	if (vxflags & VXLAN_F_GBP)
2297 		vxlan_build_gbp_hdr(vxh, md);
2298 	if (vxflags & VXLAN_F_GPE) {
2299 		err = vxlan_build_gpe_hdr(vxh, skb->protocol);
2300 		if (err < 0)
2301 			return err;
2302 		inner_protocol = skb->protocol;
2303 	}
2304 
2305 	udp_tunnel_set_inner_protocol(skb, double_encap, inner_protocol);
2306 	return 0;
2307 }
2308 
2309 /* Bypass encapsulation if the destination is local */
2310 static void vxlan_encap_bypass(struct sk_buff *skb, struct vxlan_dev *src_vxlan,
2311 			       struct vxlan_dev *dst_vxlan,
2312 			       const struct vxlan_config *src_cfg,
2313 			       __be32 vni, bool snoop)
2314 {
2315 	const struct vxlan_config *dst_cfg;
2316 	union vxlan_addr loopback;
2317 	unsigned int len = skb->len;
2318 	struct net_device *dev = dst_vxlan->dev;
2319 
2320 	skb->pkt_type = PACKET_HOST;
2321 	skb->encapsulation = 0;
2322 	skb->dev = dev;
2323 	__skb_pull(skb, skb_network_offset(skb));
2324 
2325 	rcu_read_lock();
2326 	dst_cfg = rcu_dereference(dst_vxlan->cfg);
2327 	if (unlikely(!(dev->flags & IFF_UP))) {
2328 		kfree_skb_reason(skb, SKB_DROP_REASON_DEV_READY);
2329 		goto drop;
2330 	}
2331 
2332 	if (dst_cfg->remote_ip.sa.sa_family == AF_INET) {
2333 		loopback.sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
2334 		loopback.sa.sa_family =  AF_INET;
2335 #if IS_ENABLED(CONFIG_IPV6)
2336 	} else {
2337 		loopback.sin6.sin6_addr = in6addr_loopback;
2338 		loopback.sa.sa_family =  AF_INET6;
2339 #endif
2340 	}
2341 
2342 	if ((dst_cfg->flags & VXLAN_F_LEARN) && snoop)
2343 		vxlan_snoop(dev, dst_cfg, &loopback, eth_hdr(skb)->h_source, 0, vni);
2344 
2345 	dev_dstats_tx_add(src_vxlan->dev, len);
2346 	vxlan_vnifilter_count(src_vxlan, src_cfg, vni, NULL, VXLAN_VNI_STATS_TX, len);
2347 
2348 	if (__netif_rx(skb) == NET_RX_SUCCESS) {
2349 		dev_dstats_rx_add(dst_vxlan->dev, len);
2350 		vxlan_vnifilter_count(dst_vxlan, dst_cfg, vni, NULL, VXLAN_VNI_STATS_RX,
2351 				      len);
2352 	} else {
2353 drop:
2354 		dev_dstats_rx_dropped(dev);
2355 		vxlan_vnifilter_count(dst_vxlan, dst_cfg, vni, NULL,
2356 				      VXLAN_VNI_STATS_RX_DROPS, 0);
2357 	}
2358 	rcu_read_unlock();
2359 }
2360 
2361 static int encap_bypass_if_local(struct sk_buff *skb, struct net_device *dev,
2362 				 struct vxlan_dev *vxlan,
2363 				 const struct vxlan_config *cfg,
2364 				 int addr_family,
2365 				 __be16 dst_port, int dst_ifindex, __be32 vni,
2366 				 struct dst_entry *dst,
2367 				 u32 rt_flags)
2368 {
2369 #if IS_ENABLED(CONFIG_IPV6)
2370 	/* IPv6 rt-flags are checked against RTF_LOCAL, but the value of
2371 	 * RTF_LOCAL is equal to RTCF_LOCAL. So to keep code simple
2372 	 * we can use RTCF_LOCAL which works for ipv4 and ipv6 route entry.
2373 	 */
2374 	BUILD_BUG_ON(RTCF_LOCAL != RTF_LOCAL);
2375 #endif
2376 	/* Bypass encapsulation if the destination is local */
2377 	if (rt_flags & RTCF_LOCAL &&
2378 	    !(rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST)) &&
2379 	    cfg->flags & VXLAN_F_LOCALBYPASS) {
2380 		struct vxlan_dev *dst_vxlan;
2381 
2382 		dst_release(dst);
2383 		dst_vxlan = vxlan_find_vni(vxlan->net, dst_ifindex, vni,
2384 					   addr_family, dst_port,
2385 					   cfg->flags);
2386 		if (!dst_vxlan) {
2387 			DEV_STATS_INC(dev, tx_errors);
2388 			vxlan_vnifilter_count(vxlan, cfg, vni, NULL,
2389 					      VXLAN_VNI_STATS_TX_ERRORS, 0);
2390 			kfree_skb_reason(skb, SKB_DROP_REASON_VXLAN_VNI_NOT_FOUND);
2391 
2392 			return -ENOENT;
2393 		}
2394 		vxlan_encap_bypass(skb, vxlan, dst_vxlan, cfg, vni, true);
2395 		return 1;
2396 	}
2397 
2398 	return 0;
2399 }
2400 
2401 void vxlan_xmit_one(struct sk_buff *skb, struct net_device *dev,
2402 		    const struct vxlan_config *cfg,
2403 		    __be32 default_vni, struct vxlan_rdst *rdst, bool did_rsc)
2404 {
2405 	unsigned int pkt_len = skb->len;
2406 	struct vxlan_metadata _md = {};
2407 	__be16 src_port = 0, dst_port;
2408 	struct dst_entry *ndst = NULL;
2409 	enum skb_drop_reason reason;
2410 	struct dst_cache *dst_cache;
2411 	const struct iphdr *old_iph;
2412 	struct ip_tunnel_info *info;
2413 	struct ip_tunnel_key *pkey;
2414 	struct vxlan_metadata *md;
2415 	struct ip_tunnel_key key;
2416 	struct vxlan_dev *vxlan;
2417 	u32 flags = cfg->flags;
2418 	bool udp_sum = false;
2419 	bool no_eth_encap;
2420 	int addr_family;
2421 	bool use_cache;
2422 	__be32 vni = 0;
2423 	__u8 tos, ttl;
2424 	int ifindex;
2425 	int err = 0;
2426 	bool xnet;
2427 
2428 	vxlan = netdev_priv(dev);
2429 	xnet = !net_eq(vxlan->net, dev_net(vxlan->dev));
2430 	md = &_md;
2431 
2432 	no_eth_encap = flags & VXLAN_F_GPE && skb->protocol != htons(ETH_P_TEB);
2433 	reason = skb_vlan_inet_prepare(skb, no_eth_encap);
2434 	if (reason)
2435 		goto drop;
2436 
2437 	reason = SKB_DROP_REASON_NOT_SPECIFIED;
2438 	old_iph = ip_hdr(skb);
2439 
2440 	info = skb_tunnel_info(skb);
2441 	use_cache = ip_tunnel_dst_cache_usable(skb, info);
2442 
2443 	if (rdst) {
2444 		memset(&key, 0, sizeof(key));
2445 		pkey = &key;
2446 
2447 		if (vxlan_addr_any(&rdst->remote_ip)) {
2448 			if (did_rsc) {
2449 				/* short-circuited back to local bridge */
2450 				vxlan_encap_bypass(skb, vxlan, vxlan, cfg,
2451 						   default_vni, true);
2452 				return;
2453 			}
2454 			goto drop;
2455 		}
2456 
2457 		addr_family = cfg->saddr.sa.sa_family;
2458 		dst_port = rdst->remote_port ? rdst->remote_port : cfg->dst_port;
2459 		vni = (rdst->remote_vni) ? : default_vni;
2460 		ifindex = rdst->remote_ifindex;
2461 
2462 		if (addr_family == AF_INET) {
2463 			key.u.ipv4.src = cfg->saddr.sin.sin_addr.s_addr;
2464 			key.u.ipv4.dst = rdst->remote_ip.sin.sin_addr.s_addr;
2465 		} else {
2466 			key.u.ipv6.src = cfg->saddr.sin6.sin6_addr;
2467 			key.u.ipv6.dst = rdst->remote_ip.sin6.sin6_addr;
2468 		}
2469 
2470 		dst_cache = &rdst->dst_cache;
2471 		md->gbp = skb->mark;
2472 		if (flags & VXLAN_F_TTL_INHERIT) {
2473 			ttl = ip_tunnel_get_ttl(old_iph, skb);
2474 		} else {
2475 			ttl = cfg->ttl;
2476 			if (!ttl && vxlan_addr_multicast(&rdst->remote_ip))
2477 				ttl = 1;
2478 		}
2479 		tos = cfg->tos;
2480 		if (tos == 1)
2481 			tos = ip_tunnel_get_dsfield(old_iph, skb);
2482 		if (tos && !info)
2483 			use_cache = false;
2484 
2485 		if (addr_family == AF_INET)
2486 			udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM_TX);
2487 		else
2488 			udp_sum = !(flags & VXLAN_F_UDP_ZERO_CSUM6_TX);
2489 #if IS_ENABLED(CONFIG_IPV6)
2490 		switch (cfg->label_policy) {
2491 		case VXLAN_LABEL_FIXED:
2492 			key.label = cfg->label;
2493 			break;
2494 		case VXLAN_LABEL_INHERIT:
2495 			key.label = ip_tunnel_get_flowlabel(old_iph, skb);
2496 			break;
2497 		default:
2498 			DEBUG_NET_WARN_ON_ONCE(1);
2499 			goto drop;
2500 		}
2501 #endif
2502 	} else {
2503 		if (!info) {
2504 			WARN_ONCE(1, "%s: Missing encapsulation instructions\n",
2505 				  dev->name);
2506 			goto drop;
2507 		}
2508 		pkey = &info->key;
2509 		addr_family = ip_tunnel_info_af(info);
2510 		dst_port = info->key.tp_dst ? : cfg->dst_port;
2511 		vni = tunnel_id_to_key32(info->key.tun_id);
2512 		ifindex = 0;
2513 		dst_cache = &info->dst_cache;
2514 		if (test_bit(IP_TUNNEL_VXLAN_OPT_BIT, info->key.tun_flags)) {
2515 			if (info->options_len < sizeof(*md))
2516 				goto drop;
2517 			md = ip_tunnel_info_opts(info);
2518 		}
2519 		ttl = info->key.ttl;
2520 		tos = info->key.tos;
2521 		udp_sum = test_bit(IP_TUNNEL_CSUM_BIT, info->key.tun_flags);
2522 	}
2523 	src_port = udp_flow_src_port(dev_net(dev), skb, cfg->port_min,
2524 				     cfg->port_max, true);
2525 
2526 	rcu_read_lock();
2527 	if (addr_family == AF_INET) {
2528 		struct vxlan_sock *sock4;
2529 		u16 ipcb_flags = 0;
2530 		struct rtable *rt;
2531 		__be16 df = 0;
2532 		__be32 saddr;
2533 
2534 		sock4 = rcu_dereference(vxlan->vn4_sock);
2535 		if (unlikely(!sock4)) {
2536 			reason = SKB_DROP_REASON_DEV_READY;
2537 			goto tx_error;
2538 		}
2539 
2540 		if (!ifindex)
2541 			ifindex = sock4->sk->sk_bound_dev_if;
2542 
2543 		rt = udp_tunnel_dst_lookup(skb, dev, vxlan->net, ifindex,
2544 					   &saddr, pkey, src_port, dst_port,
2545 					   tos, use_cache ? dst_cache : NULL);
2546 		if (IS_ERR(rt)) {
2547 			err = PTR_ERR(rt);
2548 			reason = SKB_DROP_REASON_IP_OUTNOROUTES;
2549 			goto tx_error;
2550 		}
2551 
2552 		if (flags & VXLAN_F_MC_ROUTE)
2553 			ipcb_flags |= IPSKB_MCROUTE;
2554 
2555 		if (!info) {
2556 			/* Bypass encapsulation if the destination is local */
2557 			err = encap_bypass_if_local(skb, dev, vxlan, cfg, AF_INET,
2558 						    dst_port, ifindex, vni,
2559 						    &rt->dst, rt->rt_flags);
2560 			if (err)
2561 				goto out_unlock;
2562 
2563 			if (cfg->df == VXLAN_DF_SET) {
2564 				df = htons(IP_DF);
2565 			} else if (cfg->df == VXLAN_DF_INHERIT) {
2566 				struct ethhdr *eth = eth_hdr(skb);
2567 
2568 				if (ntohs(eth->h_proto) == ETH_P_IPV6 ||
2569 				    (ntohs(eth->h_proto) == ETH_P_IP &&
2570 				     old_iph->frag_off & htons(IP_DF)))
2571 					df = htons(IP_DF);
2572 			}
2573 		} else if (test_bit(IP_TUNNEL_DONT_FRAGMENT_BIT,
2574 				    info->key.tun_flags)) {
2575 			df = htons(IP_DF);
2576 		}
2577 
2578 		ndst = &rt->dst;
2579 		err = skb_tunnel_check_pmtu(skb, ndst, vxlan_headroom(flags & VXLAN_F_GPE),
2580 					    netif_is_any_bridge_port(dev));
2581 		if (err < 0) {
2582 			goto tx_error;
2583 		} else if (err) {
2584 			if (info) {
2585 				struct ip_tunnel_info *unclone;
2586 
2587 				unclone = skb_tunnel_info_unclone(skb);
2588 				if (unlikely(!unclone))
2589 					goto tx_error;
2590 
2591 				unclone->key.u.ipv4.src = pkey->u.ipv4.dst;
2592 				unclone->key.u.ipv4.dst = saddr;
2593 			}
2594 			vxlan_encap_bypass(skb, vxlan, vxlan, cfg, vni, false);
2595 			dst_release(ndst);
2596 			goto out_unlock;
2597 		}
2598 
2599 		tos = ip_tunnel_ecn_encap(tos, ip_hdr(skb), skb);
2600 		ttl = ttl ? : ip4_dst_hoplimit(&rt->dst);
2601 		err = vxlan_build_skb(skb, ndst, sizeof(struct iphdr),
2602 				      vni, md, flags, udp_sum);
2603 		if (err < 0) {
2604 			reason = SKB_DROP_REASON_NOMEM;
2605 			goto tx_error;
2606 		}
2607 
2608 		udp_tunnel_xmit_skb(rt, sock4->sk, skb, saddr,
2609 				    pkey->u.ipv4.dst, tos, ttl, df,
2610 				    src_port, dst_port, xnet, !udp_sum,
2611 				    ipcb_flags);
2612 #if IS_ENABLED(CONFIG_IPV6)
2613 	} else {
2614 		struct vxlan_sock *sock6;
2615 		struct in6_addr saddr;
2616 		u16 ip6cb_flags = 0;
2617 
2618 		sock6 = rcu_dereference(vxlan->vn6_sock);
2619 		if (unlikely(!sock6)) {
2620 			reason = SKB_DROP_REASON_DEV_READY;
2621 			goto tx_error;
2622 		}
2623 
2624 		if (!ifindex)
2625 			ifindex = sock6->sk->sk_bound_dev_if;
2626 
2627 		ndst = udp_tunnel6_dst_lookup(skb, dev, vxlan->net, sock6->sk,
2628 					      ifindex, &saddr, pkey,
2629 					      src_port, dst_port, tos,
2630 					      use_cache ? dst_cache : NULL);
2631 		if (IS_ERR(ndst)) {
2632 			err = PTR_ERR(ndst);
2633 			ndst = NULL;
2634 			reason = SKB_DROP_REASON_IP_OUTNOROUTES;
2635 			goto tx_error;
2636 		}
2637 
2638 		if (flags & VXLAN_F_MC_ROUTE)
2639 			ip6cb_flags |= IP6SKB_MCROUTE;
2640 
2641 		if (!info) {
2642 			u32 rt6i_flags = dst_rt6_info(ndst)->rt6i_flags;
2643 
2644 			err = encap_bypass_if_local(skb, dev, vxlan, cfg, AF_INET6,
2645 						    dst_port, ifindex, vni,
2646 						    ndst, rt6i_flags);
2647 			if (err)
2648 				goto out_unlock;
2649 		}
2650 
2651 		err = skb_tunnel_check_pmtu(skb, ndst,
2652 					    vxlan_headroom((flags & VXLAN_F_GPE) | VXLAN_F_IPV6),
2653 					    netif_is_any_bridge_port(dev));
2654 		if (err < 0) {
2655 			goto tx_error;
2656 		} else if (err) {
2657 			if (info) {
2658 				struct ip_tunnel_info *unclone;
2659 
2660 				unclone = skb_tunnel_info_unclone(skb);
2661 				if (unlikely(!unclone))
2662 					goto tx_error;
2663 
2664 				unclone->key.u.ipv6.src = pkey->u.ipv6.dst;
2665 				unclone->key.u.ipv6.dst = saddr;
2666 			}
2667 
2668 			vxlan_encap_bypass(skb, vxlan, vxlan, cfg, vni, false);
2669 			dst_release(ndst);
2670 			goto out_unlock;
2671 		}
2672 
2673 		tos = ip_tunnel_ecn_encap(tos, ip_hdr(skb), skb);
2674 		ttl = ttl ? : ip6_dst_hoplimit(ndst);
2675 		skb_scrub_packet(skb, xnet);
2676 		err = vxlan_build_skb(skb, ndst, sizeof(struct ipv6hdr),
2677 				      vni, md, flags, udp_sum);
2678 		if (err < 0) {
2679 			reason = SKB_DROP_REASON_NOMEM;
2680 			goto tx_error;
2681 		}
2682 
2683 		udp_tunnel6_xmit_skb(ndst, sock6->sk, skb, dev,
2684 				     &saddr, &pkey->u.ipv6.dst, tos, ttl,
2685 				     pkey->label, src_port, dst_port, !udp_sum,
2686 				     ip6cb_flags);
2687 #endif
2688 	}
2689 	vxlan_vnifilter_count(vxlan, cfg, vni, NULL, VXLAN_VNI_STATS_TX, pkt_len);
2690 out_unlock:
2691 	rcu_read_unlock();
2692 	return;
2693 
2694 drop:
2695 	dev_dstats_tx_dropped(dev);
2696 	vxlan_vnifilter_count(vxlan, cfg, vni, NULL, VXLAN_VNI_STATS_TX_DROPS, 0);
2697 	kfree_skb_reason(skb, reason);
2698 	return;
2699 
2700 tx_error:
2701 	rcu_read_unlock();
2702 	if (err == -ELOOP)
2703 		DEV_STATS_INC(dev, collisions);
2704 	else if (err == -ENETUNREACH)
2705 		DEV_STATS_INC(dev, tx_carrier_errors);
2706 	dst_release(ndst);
2707 	DEV_STATS_INC(dev, tx_errors);
2708 	vxlan_vnifilter_count(vxlan, cfg, vni, NULL, VXLAN_VNI_STATS_TX_ERRORS, 0);
2709 	kfree_skb_reason(skb, reason);
2710 }
2711 
2712 static void vxlan_xmit_nh(struct sk_buff *skb, struct net_device *dev,
2713 			  const struct vxlan_config *cfg,
2714 			  struct vxlan_fdb *f, __be32 vni, bool did_rsc)
2715 {
2716 	struct vxlan_rdst nh_rdst;
2717 	struct nexthop *nh;
2718 	bool do_xmit;
2719 	u32 hash;
2720 
2721 	memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
2722 	hash = skb_get_hash(skb);
2723 
2724 	nh = rcu_dereference(f->nh);
2725 	if (!nh)
2726 		goto drop;
2727 	do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
2728 
2729 	if (likely(do_xmit))
2730 		vxlan_xmit_one(skb, dev, cfg, vni, &nh_rdst, did_rsc);
2731 	else
2732 		goto drop;
2733 
2734 	return;
2735 
2736 drop:
2737 	dev_dstats_tx_dropped(dev);
2738 	vxlan_vnifilter_count(netdev_priv(dev), cfg, vni, NULL,
2739 			      VXLAN_VNI_STATS_TX_DROPS, 0);
2740 	dev_kfree_skb(skb);
2741 }
2742 
2743 static netdev_tx_t vxlan_xmit_nhid(struct sk_buff *skb, struct net_device *dev,
2744 				   u32 nhid, __be32 vni,
2745 				   const struct vxlan_config *cfg)
2746 {
2747 	struct vxlan_rdst nh_rdst;
2748 	struct nexthop *nh;
2749 	bool do_xmit;
2750 	u32 hash;
2751 
2752 	memset(&nh_rdst, 0, sizeof(struct vxlan_rdst));
2753 	hash = skb_get_hash(skb);
2754 
2755 	rcu_read_lock();
2756 	nh = nexthop_find_by_id(dev_net(dev), nhid);
2757 	if (unlikely(!nh || !nexthop_is_fdb(nh) || !nexthop_is_multipath(nh))) {
2758 		rcu_read_unlock();
2759 		goto drop;
2760 	}
2761 	do_xmit = vxlan_fdb_nh_path_select(nh, hash, &nh_rdst);
2762 	rcu_read_unlock();
2763 
2764 	if (cfg->saddr.sa.sa_family != nh_rdst.remote_ip.sa.sa_family)
2765 		goto drop;
2766 
2767 	if (likely(do_xmit))
2768 		vxlan_xmit_one(skb, dev, cfg, vni, &nh_rdst, false);
2769 	else
2770 		goto drop;
2771 
2772 	return NETDEV_TX_OK;
2773 
2774 drop:
2775 	dev_dstats_tx_dropped(dev);
2776 	vxlan_vnifilter_count(netdev_priv(dev), cfg, vni, NULL,
2777 			      VXLAN_VNI_STATS_TX_DROPS, 0);
2778 	dev_kfree_skb(skb);
2779 	return NETDEV_TX_OK;
2780 }
2781 
2782 /* Transmit local packets over Vxlan
2783  *
2784  * Outer IP header inherits ECN and DF from inner header.
2785  * Outer UDP destination is the VXLAN assigned port.
2786  *           source port is based on hash of flow
2787  */
2788 static netdev_tx_t vxlan_xmit(struct sk_buff *skb, struct net_device *dev)
2789 {
2790 	struct vxlan_dev *vxlan = netdev_priv(dev);
2791 	struct vxlan_rdst *rdst, *fdst = NULL;
2792 	const struct ip_tunnel_info *info;
2793 	const struct vxlan_config *cfg;
2794 	__be32 default_vni;
2795 	struct vxlan_fdb *f;
2796 	struct ethhdr *eth;
2797 	__be32 vni = 0;
2798 	bool did_rsc;
2799 	u32 nhid = 0;
2800 	u32 flags;
2801 
2802 	rcu_read_lock();
2803 	cfg = rcu_dereference(vxlan->cfg);
2804 	flags = cfg->flags;
2805 	default_vni = cfg->vni;
2806 
2807 	info = skb_tunnel_info(skb);
2808 
2809 	skb_reset_mac_header(skb);
2810 
2811 	if (flags & VXLAN_F_COLLECT_METADATA) {
2812 		if (info && info->mode & IP_TUNNEL_INFO_BRIDGE &&
2813 		    info->mode & IP_TUNNEL_INFO_TX) {
2814 			vni = tunnel_id_to_key32(info->key.tun_id);
2815 			nhid = info->key.nhid;
2816 		} else {
2817 			if (info && info->mode & IP_TUNNEL_INFO_TX)
2818 				vxlan_xmit_one(skb, dev, cfg, vni, NULL, false);
2819 			else
2820 				kfree_skb_reason(skb, SKB_DROP_REASON_TUNNEL_TXINFO);
2821 			rcu_read_unlock();
2822 			return NETDEV_TX_OK;
2823 		}
2824 	}
2825 
2826 	if (flags & VXLAN_F_PROXY) {
2827 		eth = eth_hdr(skb);
2828 		if (ntohs(eth->h_proto) == ETH_P_ARP) {
2829 			netdev_tx_t res = arp_reduce(dev, skb, cfg, vni);
2830 
2831 			rcu_read_unlock();
2832 			return res;
2833 		}
2834 #if IS_ENABLED(CONFIG_IPV6)
2835 		else if (ntohs(eth->h_proto) == ETH_P_IPV6 &&
2836 			 pskb_network_may_pull(skb, sizeof(struct ipv6hdr) +
2837 						    sizeof(struct nd_msg)) &&
2838 			 ipv6_hdr(skb)->nexthdr == IPPROTO_ICMPV6) {
2839 			struct nd_msg *m = (struct nd_msg *)(ipv6_hdr(skb) + 1);
2840 
2841 			if (m->icmph.icmp6_code == 0 &&
2842 			    m->icmph.icmp6_type == NDISC_NEIGHBOUR_SOLICITATION) {
2843 				netdev_tx_t res = neigh_reduce(dev, skb, cfg, vni);
2844 
2845 				rcu_read_unlock();
2846 				return res;
2847 			}
2848 		}
2849 #endif
2850 	}
2851 
2852 	if (nhid) {
2853 		netdev_tx_t res = vxlan_xmit_nhid(skb, dev, nhid, vni, cfg);
2854 
2855 		rcu_read_unlock();
2856 		return res;
2857 	}
2858 
2859 	if (test_bit(VXLAN_DEV_F_MDB, &vxlan->flags)) {
2860 		struct vxlan_mdb_entry *mdb_entry;
2861 
2862 		mdb_entry = vxlan_mdb_entry_skb_get(vxlan, cfg, skb, vni);
2863 		if (mdb_entry) {
2864 			netdev_tx_t ret = vxlan_mdb_xmit(vxlan, cfg, mdb_entry, skb);
2865 
2866 			rcu_read_unlock();
2867 			return ret;
2868 		}
2869 	}
2870 
2871 	eth = eth_hdr(skb);
2872 	f = vxlan_find_mac_tx(vxlan, cfg, eth->h_dest, vni);
2873 	did_rsc = false;
2874 
2875 	if (f && (f->flags & NTF_ROUTER) && (flags & VXLAN_F_RSC) &&
2876 	    (ntohs(eth->h_proto) == ETH_P_IP ||
2877 	     ntohs(eth->h_proto) == ETH_P_IPV6)) {
2878 		did_rsc = route_shortcircuit(dev, skb, cfg);
2879 		eth = eth_hdr(skb);
2880 		if (did_rsc)
2881 			f = vxlan_find_mac_tx(vxlan, cfg, eth->h_dest, vni);
2882 	}
2883 
2884 	if (f == NULL) {
2885 		f = vxlan_find_mac_tx(vxlan, cfg, all_zeros_mac, vni);
2886 		if (f == NULL) {
2887 			if ((flags & VXLAN_F_L2MISS) &&
2888 			    !is_multicast_ether_addr(eth->h_dest))
2889 				vxlan_fdb_miss(vxlan, eth->h_dest);
2890 
2891 			dev_dstats_tx_dropped(dev);
2892 			vxlan_vnifilter_count(vxlan, cfg, vni, NULL,
2893 					      VXLAN_VNI_STATS_TX_DROPS, 0);
2894 			kfree_skb_reason(skb, SKB_DROP_REASON_NO_TX_TARGET);
2895 			goto out;
2896 		}
2897 	}
2898 
2899 	if (rcu_access_pointer(f->nh)) {
2900 		vxlan_xmit_nh(skb, dev, cfg, f,
2901 			      (vni ? : default_vni), did_rsc);
2902 	} else {
2903 		list_for_each_entry_rcu(rdst, &f->remotes, list) {
2904 			struct sk_buff *skb1;
2905 
2906 			if (!fdst) {
2907 				fdst = rdst;
2908 				continue;
2909 			}
2910 			skb1 = skb_clone(skb, GFP_ATOMIC);
2911 			if (skb1)
2912 				vxlan_xmit_one(skb1, dev, cfg, vni, rdst, did_rsc);
2913 		}
2914 		if (fdst)
2915 			vxlan_xmit_one(skb, dev, cfg, vni, fdst, did_rsc);
2916 		else
2917 			kfree_skb_reason(skb, SKB_DROP_REASON_NO_TX_TARGET);
2918 	}
2919 
2920 out:
2921 	rcu_read_unlock();
2922 	return NETDEV_TX_OK;
2923 }
2924 
2925 /* Walk the forwarding table and purge stale entries */
2926 static void vxlan_cleanup(struct timer_list *t)
2927 {
2928 	struct vxlan_dev *vxlan = timer_container_of(vxlan, t, age_timer);
2929 	unsigned long next_timer = jiffies + FDB_AGE_INTERVAL;
2930 	const struct vxlan_config *cfg;
2931 	struct vxlan_fdb *f;
2932 
2933 	if (!netif_running(vxlan->dev))
2934 		return;
2935 
2936 	rcu_read_lock();
2937 	cfg = rcu_dereference(vxlan->cfg);
2938 
2939 	hlist_for_each_entry_rcu(f, &vxlan->fdb_list, fdb_node) {
2940 		unsigned long timeout;
2941 
2942 		if (f->state & (NUD_PERMANENT | NUD_NOARP))
2943 			continue;
2944 
2945 		if (f->flags & NTF_EXT_LEARNED)
2946 			continue;
2947 
2948 		timeout = READ_ONCE(f->updated) + cfg->age_interval * HZ;
2949 		if (time_before_eq(timeout, jiffies)) {
2950 			spin_lock(&vxlan->hash_lock);
2951 			if (!hlist_unhashed(&f->fdb_node)) {
2952 				netdev_dbg(vxlan->dev, "garbage collect %pM\n",
2953 					   f->key.eth_addr);
2954 				f->state = NUD_STALE;
2955 				vxlan_fdb_destroy(vxlan, f, true, true);
2956 			}
2957 			spin_unlock(&vxlan->hash_lock);
2958 		} else if (time_before(timeout, next_timer)) {
2959 			next_timer = timeout;
2960 		}
2961 	}
2962 	rcu_read_unlock();
2963 
2964 	mod_timer(&vxlan->age_timer, next_timer);
2965 }
2966 
2967 static void vxlan_vs_del_dev(struct vxlan_dev *vxlan)
2968 {
2969 	ASSERT_RTNL();
2970 
2971 	hlist_del_init_rcu(&vxlan->hlist4.hlist);
2972 #if IS_ENABLED(CONFIG_IPV6)
2973 	hlist_del_init_rcu(&vxlan->hlist6.hlist);
2974 #endif
2975 }
2976 
2977 static void vxlan_vs_add_dev(struct vxlan_sock *vs, struct vxlan_dev *vxlan,
2978 			     struct vxlan_dev_node *node)
2979 {
2980 	const struct vxlan_config *cfg;
2981 	__be32 vni;
2982 
2983 	ASSERT_RTNL();
2984 
2985 	cfg = rtnl_dereference(vxlan->cfg);
2986 	vni = cfg->vni;
2987 
2988 	node->vxlan = vxlan;
2989 	hlist_add_head_rcu(&node->hlist, vni_head(vs, vni));
2990 }
2991 
2992 /* Setup stats when device is created */
2993 static int vxlan_init(struct net_device *dev)
2994 {
2995 	struct vxlan_dev *vxlan = netdev_priv(dev);
2996 	const struct vxlan_config *cfg;
2997 	int err;
2998 
2999 	cfg = rtnl_dereference(vxlan->cfg);
3000 
3001 	err = rhashtable_init(&vxlan->fdb_hash_tbl, &vxlan_fdb_rht_params);
3002 	if (err)
3003 		return err;
3004 
3005 	if (cfg->flags & VXLAN_F_VNIFILTER) {
3006 		err = vxlan_vnigroup_init(vxlan);
3007 		if (err)
3008 			goto err_rhashtable_destroy;
3009 	}
3010 
3011 	err = gro_cells_init(&vxlan->gro_cells, dev);
3012 	if (err)
3013 		goto err_vnigroup_uninit;
3014 
3015 	err = vxlan_mdb_init(vxlan);
3016 	if (err)
3017 		goto err_gro_cells_destroy;
3018 
3019 	netdev_lockdep_set_classes(dev);
3020 	return 0;
3021 
3022 err_gro_cells_destroy:
3023 	gro_cells_destroy(&vxlan->gro_cells);
3024 err_vnigroup_uninit:
3025 	if (cfg->flags & VXLAN_F_VNIFILTER)
3026 		vxlan_vnigroup_uninit(vxlan);
3027 err_rhashtable_destroy:
3028 	rhashtable_destroy(&vxlan->fdb_hash_tbl);
3029 	return err;
3030 }
3031 
3032 static void vxlan_uninit(struct net_device *dev)
3033 {
3034 	struct vxlan_dev *vxlan = netdev_priv(dev);
3035 	const struct vxlan_config *cfg;
3036 
3037 	cfg = rtnl_dereference(vxlan->cfg);
3038 
3039 	vxlan_mdb_fini(vxlan);
3040 
3041 	if (cfg->flags & VXLAN_F_VNIFILTER)
3042 		vxlan_vnigroup_uninit(vxlan);
3043 
3044 	gro_cells_destroy(&vxlan->gro_cells);
3045 
3046 	rhashtable_destroy(&vxlan->fdb_hash_tbl);
3047 }
3048 
3049 /* Start ageing timer and join group when device is brought up */
3050 static int vxlan_open(struct net_device *dev)
3051 {
3052 	struct vxlan_dev *vxlan = netdev_priv(dev);
3053 	const struct vxlan_config *cfg;
3054 	int ret;
3055 
3056 	ret = vxlan_sock_add(vxlan);
3057 	if (ret < 0)
3058 		return ret;
3059 
3060 	ret = vxlan_multicast_join(vxlan);
3061 	if (ret) {
3062 		vxlan_sock_release(vxlan);
3063 		return ret;
3064 	}
3065 
3066 	cfg = rtnl_dereference(vxlan->cfg);
3067 	if (cfg->age_interval)
3068 		mod_timer(&vxlan->age_timer, jiffies + FDB_AGE_INTERVAL);
3069 
3070 	return ret;
3071 }
3072 
3073 struct vxlan_fdb_flush_desc {
3074 	bool				ignore_default_entry;
3075 	unsigned long                   state;
3076 	unsigned long			state_mask;
3077 	unsigned long                   flags;
3078 	unsigned long			flags_mask;
3079 	__be32				src_vni;
3080 	u32				nhid;
3081 	__be32				vni;
3082 	__be16				port;
3083 	union vxlan_addr		dst_ip;
3084 };
3085 
3086 static bool vxlan_fdb_is_default_entry(const struct vxlan_fdb *f,
3087 				       const struct vxlan_dev *vxlan)
3088 {
3089 	const struct vxlan_config *cfg = rcu_dereference_rtnl(vxlan->cfg);
3090 
3091 	return is_zero_ether_addr(f->key.eth_addr) &&
3092 	       f->key.vni == cfg->vni;
3093 }
3094 
3095 static bool vxlan_fdb_nhid_matches(const struct vxlan_fdb *f, u32 nhid)
3096 {
3097 	struct nexthop *nh = rtnl_dereference(f->nh);
3098 
3099 	return nh && nh->id == nhid;
3100 }
3101 
3102 static bool vxlan_fdb_flush_matches(const struct vxlan_fdb *f,
3103 				    const struct vxlan_dev *vxlan,
3104 				    const struct vxlan_fdb_flush_desc *desc)
3105 {
3106 	if (desc->state_mask && (f->state & desc->state_mask) != desc->state)
3107 		return false;
3108 
3109 	if (desc->flags_mask && (f->flags & desc->flags_mask) != desc->flags)
3110 		return false;
3111 
3112 	if (desc->ignore_default_entry && vxlan_fdb_is_default_entry(f, vxlan))
3113 		return false;
3114 
3115 	if (desc->src_vni && f->key.vni != desc->src_vni)
3116 		return false;
3117 
3118 	if (desc->nhid && !vxlan_fdb_nhid_matches(f, desc->nhid))
3119 		return false;
3120 
3121 	return true;
3122 }
3123 
3124 static bool
3125 vxlan_fdb_flush_should_match_remotes(const struct vxlan_fdb_flush_desc *desc)
3126 {
3127 	return desc->vni || desc->port || desc->dst_ip.sa.sa_family;
3128 }
3129 
3130 static bool
3131 vxlan_fdb_flush_remote_matches(const struct vxlan_fdb_flush_desc *desc,
3132 			       const struct vxlan_rdst *rd)
3133 {
3134 	if (desc->vni && rd->remote_vni != desc->vni)
3135 		return false;
3136 
3137 	if (desc->port && rd->remote_port != desc->port)
3138 		return false;
3139 
3140 	if (desc->dst_ip.sa.sa_family &&
3141 	    !vxlan_addr_equal(&rd->remote_ip, &desc->dst_ip))
3142 		return false;
3143 
3144 	return true;
3145 }
3146 
3147 static void
3148 vxlan_fdb_flush_match_remotes(struct vxlan_fdb *f, struct vxlan_dev *vxlan,
3149 			      const struct vxlan_fdb_flush_desc *desc,
3150 			      bool *p_destroy_fdb)
3151 {
3152 	struct vxlan_rdst *rd, *tmp;
3153 
3154 	list_for_each_entry_safe(rd, tmp, &f->remotes, list) {
3155 		if (!vxlan_fdb_flush_remote_matches(desc, rd))
3156 			continue;
3157 
3158 		if (list_is_singular(&f->remotes)) {
3159 			*p_destroy_fdb = true;
3160 			return;
3161 		}
3162 
3163 		vxlan_fdb_dst_destroy(vxlan, f, rd, true);
3164 	}
3165 }
3166 
3167 /* Purge the forwarding table */
3168 static void vxlan_flush(struct vxlan_dev *vxlan,
3169 			const struct vxlan_fdb_flush_desc *desc)
3170 {
3171 	bool match_remotes = vxlan_fdb_flush_should_match_remotes(desc);
3172 	struct vxlan_fdb *f;
3173 
3174 	rcu_read_lock();
3175 	hlist_for_each_entry_rcu(f, &vxlan->fdb_list, fdb_node) {
3176 		if (!vxlan_fdb_flush_matches(f, vxlan, desc))
3177 			continue;
3178 
3179 		spin_lock_bh(&vxlan->hash_lock);
3180 		if (hlist_unhashed(&f->fdb_node))
3181 			goto unlock;
3182 
3183 		if (match_remotes) {
3184 			bool destroy_fdb = false;
3185 
3186 			vxlan_fdb_flush_match_remotes(f, vxlan, desc,
3187 						      &destroy_fdb);
3188 
3189 			if (!destroy_fdb)
3190 				goto unlock;
3191 		}
3192 
3193 		vxlan_fdb_destroy(vxlan, f, true, true);
3194 unlock:
3195 		spin_unlock_bh(&vxlan->hash_lock);
3196 	}
3197 	rcu_read_unlock();
3198 }
3199 
3200 static const struct nla_policy vxlan_del_bulk_policy[NDA_MAX + 1] = {
3201 	[NDA_SRC_VNI]   = { .type = NLA_U32 },
3202 	[NDA_NH_ID]	= { .type = NLA_U32 },
3203 	[NDA_VNI]	= { .type = NLA_U32 },
3204 	[NDA_PORT]	= { .type = NLA_U16 },
3205 	[NDA_DST]	= NLA_POLICY_RANGE(NLA_BINARY, sizeof(struct in_addr),
3206 					   sizeof(struct in6_addr)),
3207 	[NDA_NDM_STATE_MASK]	= { .type = NLA_U16 },
3208 	[NDA_NDM_FLAGS_MASK]	= { .type = NLA_U8 },
3209 };
3210 
3211 #define VXLAN_FDB_FLUSH_IGNORED_NDM_FLAGS (NTF_MASTER | NTF_SELF)
3212 #define VXLAN_FDB_FLUSH_ALLOWED_NDM_STATES (NUD_PERMANENT | NUD_NOARP)
3213 #define VXLAN_FDB_FLUSH_ALLOWED_NDM_FLAGS (NTF_EXT_LEARNED | NTF_OFFLOADED | \
3214 					   NTF_ROUTER)
3215 
3216 static int vxlan_fdb_delete_bulk(struct nlmsghdr *nlh, struct net_device *dev,
3217 				 struct netlink_ext_ack *extack)
3218 {
3219 	struct vxlan_dev *vxlan = netdev_priv(dev);
3220 	struct vxlan_fdb_flush_desc desc = {};
3221 	struct ndmsg *ndm = nlmsg_data(nlh);
3222 	struct nlattr *tb[NDA_MAX + 1];
3223 	u8 ndm_flags;
3224 	int err;
3225 
3226 	ndm_flags = ndm->ndm_flags & ~VXLAN_FDB_FLUSH_IGNORED_NDM_FLAGS;
3227 
3228 	err = nlmsg_parse(nlh, sizeof(*ndm), tb, NDA_MAX, vxlan_del_bulk_policy,
3229 			  extack);
3230 	if (err)
3231 		return err;
3232 
3233 	if (ndm_flags & ~VXLAN_FDB_FLUSH_ALLOWED_NDM_FLAGS) {
3234 		NL_SET_ERR_MSG(extack, "Unsupported fdb flush ndm flag bits set");
3235 		return -EINVAL;
3236 	}
3237 	if (ndm->ndm_state & ~VXLAN_FDB_FLUSH_ALLOWED_NDM_STATES) {
3238 		NL_SET_ERR_MSG(extack, "Unsupported fdb flush ndm state bits set");
3239 		return -EINVAL;
3240 	}
3241 
3242 	desc.state = ndm->ndm_state;
3243 	desc.flags = ndm_flags;
3244 
3245 	if (tb[NDA_NDM_STATE_MASK])
3246 		desc.state_mask = nla_get_u16(tb[NDA_NDM_STATE_MASK]);
3247 
3248 	if (tb[NDA_NDM_FLAGS_MASK])
3249 		desc.flags_mask = nla_get_u8(tb[NDA_NDM_FLAGS_MASK]);
3250 
3251 	if (tb[NDA_SRC_VNI])
3252 		desc.src_vni = cpu_to_be32(nla_get_u32(tb[NDA_SRC_VNI]));
3253 
3254 	if (tb[NDA_NH_ID])
3255 		desc.nhid = nla_get_u32(tb[NDA_NH_ID]);
3256 
3257 	if (tb[NDA_VNI])
3258 		desc.vni = cpu_to_be32(nla_get_u32(tb[NDA_VNI]));
3259 
3260 	if (tb[NDA_PORT])
3261 		desc.port = nla_get_be16(tb[NDA_PORT]);
3262 
3263 	if (tb[NDA_DST]) {
3264 		union vxlan_addr ip;
3265 
3266 		err = vxlan_nla_get_addr(&ip, tb[NDA_DST]);
3267 		if (err) {
3268 			NL_SET_ERR_MSG_ATTR(extack, tb[NDA_DST],
3269 					    "Unsupported address family");
3270 			return err;
3271 		}
3272 		desc.dst_ip = ip;
3273 	}
3274 
3275 	vxlan_flush(vxlan, &desc);
3276 
3277 	return 0;
3278 }
3279 
3280 /* Cleanup timer and forwarding table on shutdown */
3281 static int vxlan_stop(struct net_device *dev)
3282 {
3283 	struct vxlan_dev *vxlan = netdev_priv(dev);
3284 	struct vxlan_fdb_flush_desc desc = {
3285 		/* Default entry is deleted at vxlan_dellink. */
3286 		.ignore_default_entry = true,
3287 		.state = 0,
3288 		.state_mask = NUD_PERMANENT | NUD_NOARP,
3289 	};
3290 
3291 	vxlan_multicast_leave(vxlan);
3292 
3293 	timer_delete_sync(&vxlan->age_timer);
3294 
3295 	vxlan_flush(vxlan, &desc);
3296 	vxlan_sock_release(vxlan);
3297 
3298 	return 0;
3299 }
3300 
3301 /* Stub, nothing needs to be done. */
3302 static void vxlan_set_multicast_list(struct net_device *dev)
3303 {
3304 }
3305 
3306 static int vxlan_change_mtu(struct net_device *dev, int new_mtu)
3307 {
3308 	struct vxlan_dev *vxlan = netdev_priv(dev);
3309 	const struct vxlan_config *cfg;
3310 	struct net_device *lowerdev;
3311 
3312 	cfg = rtnl_dereference(vxlan->cfg);
3313 
3314 	lowerdev = __dev_get_by_index(vxlan->net, cfg->remote_ifindex);
3315 
3316 	/* This check is different than dev->max_mtu, because it looks at
3317 	 * the lowerdev->mtu, rather than the static dev->max_mtu
3318 	 */
3319 	if (lowerdev) {
3320 		int max_mtu = lowerdev->mtu - vxlan_headroom(cfg->flags);
3321 		if (new_mtu > max_mtu)
3322 			return -EINVAL;
3323 	}
3324 
3325 	WRITE_ONCE(dev->mtu, new_mtu);
3326 	return 0;
3327 }
3328 
3329 static int vxlan_fill_metadata_dst(struct net_device *dev, struct sk_buff *skb)
3330 {
3331 	struct vxlan_dev *vxlan = netdev_priv(dev);
3332 	struct ip_tunnel_info *info = skb_tunnel_info(skb);
3333 	const struct vxlan_config *cfg;
3334 	__be16 sport, dport;
3335 
3336 	cfg = rcu_dereference(vxlan->cfg);
3337 
3338 	sport = udp_flow_src_port(dev_net(dev), skb, cfg->port_min,
3339 				  cfg->port_max, true);
3340 	dport = info->key.tp_dst ? : cfg->dst_port;
3341 
3342 	if (ip_tunnel_info_af(info) == AF_INET) {
3343 		struct vxlan_sock *sock4 = rcu_dereference(vxlan->vn4_sock);
3344 		struct rtable *rt;
3345 
3346 		if (!sock4)
3347 			return -EIO;
3348 
3349 		rt = udp_tunnel_dst_lookup(skb, dev, vxlan->net, 0,
3350 					   &info->key.u.ipv4.src,
3351 					   &info->key,
3352 					   sport, dport, info->key.tos,
3353 					   &info->dst_cache);
3354 		if (IS_ERR(rt))
3355 			return PTR_ERR(rt);
3356 		ip_rt_put(rt);
3357 	} else {
3358 #if IS_ENABLED(CONFIG_IPV6)
3359 		struct vxlan_sock *sock6 = rcu_dereference(vxlan->vn6_sock);
3360 		struct dst_entry *ndst;
3361 
3362 		if (!sock6)
3363 			return -EIO;
3364 
3365 		ndst = udp_tunnel6_dst_lookup(skb, dev, vxlan->net, sock6->sk,
3366 					      0, &info->key.u.ipv6.src,
3367 					      &info->key,
3368 					      sport, dport, info->key.tos,
3369 					      &info->dst_cache);
3370 		if (IS_ERR(ndst))
3371 			return PTR_ERR(ndst);
3372 		dst_release(ndst);
3373 #else /* !CONFIG_IPV6 */
3374 		return -EPFNOSUPPORT;
3375 #endif
3376 	}
3377 	info->key.tp_src = sport;
3378 	info->key.tp_dst = dport;
3379 	return 0;
3380 }
3381 
3382 static const struct net_device_ops vxlan_netdev_ether_ops = {
3383 	.ndo_init		= vxlan_init,
3384 	.ndo_uninit		= vxlan_uninit,
3385 	.ndo_open		= vxlan_open,
3386 	.ndo_stop		= vxlan_stop,
3387 	.ndo_start_xmit		= vxlan_xmit,
3388 	.ndo_set_rx_mode	= vxlan_set_multicast_list,
3389 	.ndo_change_mtu		= vxlan_change_mtu,
3390 	.ndo_validate_addr	= eth_validate_addr,
3391 	.ndo_set_mac_address	= eth_mac_addr,
3392 	.ndo_fdb_add		= vxlan_fdb_add,
3393 	.ndo_fdb_del		= vxlan_fdb_delete,
3394 	.ndo_fdb_del_bulk	= vxlan_fdb_delete_bulk,
3395 	.ndo_fdb_dump		= vxlan_fdb_dump,
3396 	.ndo_fdb_get		= vxlan_fdb_get,
3397 	.ndo_mdb_add		= vxlan_mdb_add,
3398 	.ndo_mdb_del		= vxlan_mdb_del,
3399 	.ndo_mdb_del_bulk	= vxlan_mdb_del_bulk,
3400 	.ndo_mdb_dump		= vxlan_mdb_dump,
3401 	.ndo_mdb_get		= vxlan_mdb_get,
3402 	.ndo_fill_metadata_dst	= vxlan_fill_metadata_dst,
3403 };
3404 
3405 static const struct net_device_ops vxlan_netdev_raw_ops = {
3406 	.ndo_init		= vxlan_init,
3407 	.ndo_uninit		= vxlan_uninit,
3408 	.ndo_open		= vxlan_open,
3409 	.ndo_stop		= vxlan_stop,
3410 	.ndo_start_xmit		= vxlan_xmit,
3411 	.ndo_change_mtu		= vxlan_change_mtu,
3412 	.ndo_fill_metadata_dst	= vxlan_fill_metadata_dst,
3413 };
3414 
3415 /* Info for udev, that this is a virtual tunnel endpoint */
3416 static const struct device_type vxlan_type = {
3417 	.name = "vxlan",
3418 };
3419 
3420 /* Calls the ndo_udp_tunnel_add of the caller in order to
3421  * supply the listening VXLAN udp ports. Callers are expected
3422  * to implement the ndo_udp_tunnel_add.
3423  */
3424 static void vxlan_offload_rx_ports(struct net_device *dev, bool push)
3425 {
3426 	struct vxlan_sock *vs;
3427 	struct net *net = dev_net(dev);
3428 	struct vxlan_net *vn = net_generic(net, vxlan_net_id);
3429 	unsigned int i;
3430 
3431 	ASSERT_RTNL();
3432 
3433 	for (i = 0; i < PORT_HASH_SIZE; ++i) {
3434 		hlist_for_each_entry(vs, &vn->sock_list[i], hlist) {
3435 			unsigned short type;
3436 
3437 			if (vs->flags & VXLAN_F_GPE)
3438 				type = UDP_TUNNEL_TYPE_VXLAN_GPE;
3439 			else
3440 				type = UDP_TUNNEL_TYPE_VXLAN;
3441 
3442 			if (push)
3443 				udp_tunnel_push_rx_port(dev, vs->sk, type);
3444 			else
3445 				udp_tunnel_drop_rx_port(dev, vs->sk, type);
3446 		}
3447 	}
3448 }
3449 
3450 static void vxlan_free_dev(struct net_device *dev)
3451 {
3452 	struct vxlan_dev *vxlan = netdev_priv(dev);
3453 	struct vxlan_config *cfg = rcu_dereference_protected(vxlan->cfg, 1);
3454 
3455 	RCU_INIT_POINTER(vxlan->cfg, NULL);
3456 	kfree(cfg);
3457 }
3458 
3459 /* Initialize the device structure. */
3460 static void vxlan_setup(struct net_device *dev)
3461 {
3462 	struct vxlan_dev *vxlan = netdev_priv(dev);
3463 
3464 	eth_hw_addr_random(dev);
3465 	ether_setup(dev);
3466 
3467 	dev->priv_destructor = vxlan_free_dev;
3468 
3469 	dev->needs_free_netdev = true;
3470 	SET_NETDEV_DEVTYPE(dev, &vxlan_type);
3471 
3472 	dev->features	|= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
3473 	dev->features   |= NETIF_F_RXCSUM;
3474 	dev->features   |= NETIF_F_GSO_SOFTWARE;
3475 
3476 	/* Partial features are disabled by default. */
3477 	dev->vlan_features = dev->features;
3478 	dev->hw_features |= NETIF_F_SG | NETIF_F_HW_CSUM | NETIF_F_FRAGLIST;
3479 	dev->hw_features |= NETIF_F_RXCSUM;
3480 	dev->hw_features |= NETIF_F_GSO_SOFTWARE;
3481 	dev->hw_features |= UDP_TUNNEL_PARTIAL_FEATURES;
3482 	dev->hw_features |= NETIF_F_GSO_PARTIAL;
3483 
3484 	dev->hw_enc_features = dev->hw_features;
3485 	dev->gso_partial_features = UDP_TUNNEL_PARTIAL_FEATURES;
3486 	dev->mangleid_features = NETIF_F_GSO_PARTIAL;
3487 
3488 	netif_keep_dst(dev);
3489 	netif_set_tso_max_size(dev, GSO_MAX_SIZE);
3490 
3491 	dev->priv_flags |= IFF_NO_QUEUE;
3492 	dev->change_proto_down = true;
3493 	dev->lltx = true;
3494 
3495 	/* MTU range: 68 - 65535 */
3496 	dev->min_mtu = ETH_MIN_MTU;
3497 	dev->max_mtu = ETH_MAX_MTU;
3498 
3499 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS;
3500 	INIT_LIST_HEAD(&vxlan->next);
3501 	spin_lock_init(&vxlan->hash_lock);
3502 
3503 	timer_setup(&vxlan->age_timer, vxlan_cleanup, TIMER_DEFERRABLE);
3504 
3505 	vxlan->dev = dev;
3506 
3507 	INIT_HLIST_HEAD(&vxlan->fdb_list);
3508 }
3509 
3510 static void vxlan_ether_setup(struct net_device *dev)
3511 {
3512 	dev->priv_flags &= ~IFF_TX_SKB_SHARING;
3513 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
3514 	dev->netdev_ops = &vxlan_netdev_ether_ops;
3515 }
3516 
3517 static void vxlan_raw_setup(struct net_device *dev)
3518 {
3519 	dev->header_ops = NULL;
3520 	dev->type = ARPHRD_NONE;
3521 	dev->hard_header_len = 0;
3522 	dev->addr_len = 0;
3523 	dev->flags = IFF_POINTOPOINT | IFF_NOARP | IFF_MULTICAST;
3524 	dev->netdev_ops = &vxlan_netdev_raw_ops;
3525 }
3526 
3527 static const struct nla_policy vxlan_policy[IFLA_VXLAN_MAX + 1] = {
3528 	[IFLA_VXLAN_UNSPEC]     = { .strict_start_type = IFLA_VXLAN_LOCALBYPASS },
3529 	[IFLA_VXLAN_ID]		= { .type = NLA_U32 },
3530 	[IFLA_VXLAN_GROUP]	= { .len = sizeof_field(struct iphdr, daddr) },
3531 	[IFLA_VXLAN_GROUP6]	= { .len = sizeof(struct in6_addr) },
3532 	[IFLA_VXLAN_LINK]	= { .type = NLA_U32 },
3533 	[IFLA_VXLAN_LOCAL]	= { .len = sizeof_field(struct iphdr, saddr) },
3534 	[IFLA_VXLAN_LOCAL6]	= { .len = sizeof(struct in6_addr) },
3535 	[IFLA_VXLAN_TOS]	= { .type = NLA_U8 },
3536 	[IFLA_VXLAN_TTL]	= { .type = NLA_U8 },
3537 	[IFLA_VXLAN_LABEL]	= { .type = NLA_U32 },
3538 	[IFLA_VXLAN_LEARNING]	= { .type = NLA_U8 },
3539 	[IFLA_VXLAN_AGEING]	= { .type = NLA_U32 },
3540 	[IFLA_VXLAN_LIMIT]	= { .type = NLA_U32 },
3541 	[IFLA_VXLAN_PORT_RANGE] = { .len  = sizeof(struct ifla_vxlan_port_range) },
3542 	[IFLA_VXLAN_PROXY]	= { .type = NLA_U8 },
3543 	[IFLA_VXLAN_RSC]	= { .type = NLA_U8 },
3544 	[IFLA_VXLAN_L2MISS]	= { .type = NLA_U8 },
3545 	[IFLA_VXLAN_L3MISS]	= { .type = NLA_U8 },
3546 	[IFLA_VXLAN_COLLECT_METADATA]	= { .type = NLA_U8 },
3547 	[IFLA_VXLAN_PORT]	= { .type = NLA_U16 },
3548 	[IFLA_VXLAN_UDP_CSUM]	= { .type = NLA_U8 },
3549 	[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]	= { .type = NLA_U8 },
3550 	[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]	= { .type = NLA_U8 },
3551 	[IFLA_VXLAN_REMCSUM_TX]	= { .type = NLA_U8 },
3552 	[IFLA_VXLAN_REMCSUM_RX]	= { .type = NLA_U8 },
3553 	[IFLA_VXLAN_GBP]	= { .type = NLA_FLAG, },
3554 	[IFLA_VXLAN_GPE]	= { .type = NLA_FLAG, },
3555 	[IFLA_VXLAN_REMCSUM_NOPARTIAL]	= { .type = NLA_FLAG },
3556 	[IFLA_VXLAN_TTL_INHERIT]	= { .type = NLA_FLAG },
3557 	[IFLA_VXLAN_DF]		= { .type = NLA_U8 },
3558 	[IFLA_VXLAN_VNIFILTER]	= { .type = NLA_U8 },
3559 	[IFLA_VXLAN_LOCALBYPASS]	= NLA_POLICY_MAX(NLA_U8, 1),
3560 	[IFLA_VXLAN_LABEL_POLICY]       = NLA_POLICY_MAX(NLA_U32, VXLAN_LABEL_MAX),
3561 	[IFLA_VXLAN_RESERVED_BITS] = NLA_POLICY_EXACT_LEN(sizeof(struct vxlanhdr)),
3562 	[IFLA_VXLAN_MC_ROUTE]		= NLA_POLICY_MAX(NLA_U8, 1),
3563 };
3564 
3565 static int vxlan_validate(struct nlattr *tb[], struct nlattr *data[],
3566 			  struct netlink_ext_ack *extack)
3567 {
3568 	if (tb[IFLA_ADDRESS]) {
3569 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
3570 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3571 					    "Provided link layer address is not Ethernet");
3572 			return -EINVAL;
3573 		}
3574 
3575 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
3576 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_ADDRESS],
3577 					    "Provided Ethernet address is not unicast");
3578 			return -EADDRNOTAVAIL;
3579 		}
3580 	}
3581 
3582 	if (tb[IFLA_MTU]) {
3583 		u32 mtu = nla_get_u32(tb[IFLA_MTU]);
3584 
3585 		if (mtu < ETH_MIN_MTU || mtu > ETH_MAX_MTU) {
3586 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
3587 					    "MTU must be between 68 and 65535");
3588 			return -EINVAL;
3589 		}
3590 	}
3591 
3592 	if (!data) {
3593 		NL_SET_ERR_MSG(extack,
3594 			       "Required attributes not provided to perform the operation");
3595 		return -EINVAL;
3596 	}
3597 
3598 	if (data[IFLA_VXLAN_ID]) {
3599 		u32 id = nla_get_u32(data[IFLA_VXLAN_ID]);
3600 
3601 		if (id >= VXLAN_N_VID) {
3602 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_ID],
3603 					    "VXLAN ID must be lower than 16777216");
3604 			return -ERANGE;
3605 		}
3606 	}
3607 
3608 	if (data[IFLA_VXLAN_PORT_RANGE]) {
3609 		const struct ifla_vxlan_port_range *p
3610 			= nla_data(data[IFLA_VXLAN_PORT_RANGE]);
3611 
3612 		if (ntohs(p->high) < ntohs(p->low)) {
3613 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_PORT_RANGE],
3614 					    "Invalid source port range");
3615 			return -EINVAL;
3616 		}
3617 	}
3618 
3619 	if (data[IFLA_VXLAN_DF]) {
3620 		enum ifla_vxlan_df df = nla_get_u8(data[IFLA_VXLAN_DF]);
3621 
3622 		if (df < 0 || df > VXLAN_DF_MAX) {
3623 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_DF],
3624 					    "Invalid DF attribute");
3625 			return -EINVAL;
3626 		}
3627 	}
3628 
3629 	return 0;
3630 }
3631 
3632 static void vxlan_get_drvinfo(struct net_device *netdev,
3633 			      struct ethtool_drvinfo *drvinfo)
3634 {
3635 	strscpy(drvinfo->version, VXLAN_VERSION, sizeof(drvinfo->version));
3636 	strscpy(drvinfo->driver, "vxlan", sizeof(drvinfo->driver));
3637 }
3638 
3639 static int vxlan_get_link_ksettings(struct net_device *dev,
3640 				    struct ethtool_link_ksettings *cmd)
3641 {
3642 	struct vxlan_dev *vxlan = netdev_priv(dev);
3643 	const struct vxlan_config *cfg;
3644 	struct net_device *lowerdev;
3645 
3646 	cfg = rtnl_dereference(vxlan->cfg);
3647 	lowerdev = __dev_get_by_index(vxlan->net, cfg->remote_ifindex);
3648 
3649 	if (!lowerdev) {
3650 		cmd->base.duplex = DUPLEX_UNKNOWN;
3651 		cmd->base.port = PORT_OTHER;
3652 		cmd->base.speed = SPEED_UNKNOWN;
3653 
3654 		return 0;
3655 	}
3656 
3657 	return __ethtool_get_link_ksettings(lowerdev, cmd);
3658 }
3659 
3660 static const struct ethtool_ops vxlan_ethtool_ops = {
3661 	.get_drvinfo		= vxlan_get_drvinfo,
3662 	.get_link		= ethtool_op_get_link,
3663 	.get_link_ksettings	= vxlan_get_link_ksettings,
3664 };
3665 
3666 static struct sock *vxlan_create_sock(struct net *net, bool ipv6,
3667 				      __be16 port, u32 flags, int ifindex)
3668 {
3669 	struct socket *sock;
3670 	struct udp_port_cfg udp_conf;
3671 	int err;
3672 
3673 	memset(&udp_conf, 0, sizeof(udp_conf));
3674 
3675 	if (ipv6) {
3676 		udp_conf.family = AF_INET6;
3677 		udp_conf.use_udp6_rx_checksums =
3678 		    !(flags & VXLAN_F_UDP_ZERO_CSUM6_RX);
3679 		udp_conf.ipv6_v6only = 1;
3680 	} else {
3681 		udp_conf.family = AF_INET;
3682 	}
3683 
3684 	udp_conf.local_udp_port = port;
3685 	udp_conf.bind_ifindex = ifindex;
3686 
3687 	/* Open UDP socket */
3688 	err = udp_sock_create(net, &udp_conf, &sock);
3689 	if (err < 0)
3690 		return ERR_PTR(err);
3691 
3692 	udp_allow_gso(sock->sk);
3693 	return sock->sk;
3694 }
3695 
3696 /* Create new listen socket if needed */
3697 static struct vxlan_sock *vxlan_socket_create(struct net *net, bool ipv6,
3698 					      __be16 port, u32 flags,
3699 					      int ifindex)
3700 {
3701 	struct udp_tunnel_sock_cfg tunnel_cfg;
3702 	struct vxlan_sock *vs;
3703 	struct sock *sk;
3704 	unsigned int h;
3705 
3706 	ASSERT_RTNL();
3707 
3708 	vs = kzalloc_obj(*vs);
3709 	if (!vs)
3710 		return ERR_PTR(-ENOMEM);
3711 
3712 	for (h = 0; h < VNI_HASH_SIZE; ++h)
3713 		INIT_HLIST_HEAD(&vs->vni_list[h]);
3714 
3715 	sk = vxlan_create_sock(net, ipv6, port, flags, ifindex);
3716 	if (IS_ERR(sk)) {
3717 		kfree(vs);
3718 		return ERR_CAST(sk);
3719 	}
3720 
3721 	vs->sk = sk;
3722 	refcount_set(&vs->refcnt, 1);
3723 	vs->flags = (flags & VXLAN_F_RCV_FLAGS);
3724 
3725 	hlist_add_head_rcu(&vs->hlist, vs_head(net, port));
3726 	udp_tunnel_notify_add_rx_port(sk,
3727 				      (vs->flags & VXLAN_F_GPE) ?
3728 				      UDP_TUNNEL_TYPE_VXLAN_GPE :
3729 				      UDP_TUNNEL_TYPE_VXLAN);
3730 
3731 	/* Mark socket as an encapsulation socket. */
3732 	memset(&tunnel_cfg, 0, sizeof(tunnel_cfg));
3733 	tunnel_cfg.sk_user_data = vs;
3734 	tunnel_cfg.encap_type = 1;
3735 	tunnel_cfg.encap_rcv = vxlan_rcv;
3736 	tunnel_cfg.encap_err_lookup = vxlan_err_lookup;
3737 	tunnel_cfg.encap_destroy = NULL;
3738 	if (vs->flags & VXLAN_F_GPE) {
3739 		tunnel_cfg.gro_receive = vxlan_gpe_gro_receive;
3740 		tunnel_cfg.gro_complete = vxlan_gpe_gro_complete;
3741 	} else {
3742 		tunnel_cfg.gro_receive = vxlan_gro_receive;
3743 		tunnel_cfg.gro_complete = vxlan_gro_complete;
3744 	}
3745 
3746 	setup_udp_tunnel_sock(net, sk, &tunnel_cfg);
3747 
3748 	return vs;
3749 }
3750 
3751 static int __vxlan_sock_add(struct vxlan_dev *vxlan, bool ipv6)
3752 {
3753 	const struct vxlan_config *cfg = rtnl_dereference(vxlan->cfg);
3754 	bool metadata = cfg->flags & VXLAN_F_COLLECT_METADATA;
3755 	struct vxlan_sock *vs = NULL;
3756 	struct vxlan_dev_node *node;
3757 	int l3mdev_index = 0;
3758 
3759 	ASSERT_RTNL();
3760 
3761 	if (cfg->remote_ifindex)
3762 		l3mdev_index = l3mdev_master_upper_ifindex_by_index(
3763 			vxlan->net, cfg->remote_ifindex);
3764 
3765 	if (!cfg->no_share) {
3766 		rcu_read_lock();
3767 		vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
3768 				     cfg->dst_port, cfg->flags,
3769 				     l3mdev_index);
3770 		if (vs && !refcount_inc_not_zero(&vs->refcnt)) {
3771 			rcu_read_unlock();
3772 			return -EBUSY;
3773 		}
3774 		rcu_read_unlock();
3775 	}
3776 	if (!vs)
3777 		vs = vxlan_socket_create(vxlan->net, ipv6,
3778 					 cfg->dst_port, cfg->flags,
3779 					 l3mdev_index);
3780 	if (IS_ERR(vs))
3781 		return PTR_ERR(vs);
3782 #if IS_ENABLED(CONFIG_IPV6)
3783 	if (ipv6) {
3784 		rcu_assign_pointer(vxlan->vn6_sock, vs);
3785 		node = &vxlan->hlist6;
3786 	} else
3787 #endif
3788 	{
3789 		rcu_assign_pointer(vxlan->vn4_sock, vs);
3790 		node = &vxlan->hlist4;
3791 	}
3792 
3793 	if (metadata && (cfg->flags & VXLAN_F_VNIFILTER))
3794 		vxlan_vs_add_vnigrp(vxlan, vs, ipv6);
3795 	else
3796 		vxlan_vs_add_dev(vs, vxlan, node);
3797 
3798 	return 0;
3799 }
3800 
3801 static int vxlan_sock_add(struct vxlan_dev *vxlan)
3802 {
3803 	const struct vxlan_config *cfg = rtnl_dereference(vxlan->cfg);
3804 	bool metadata, ipv6, ipv4;
3805 	int ret = 0;
3806 
3807 	metadata = cfg->flags & VXLAN_F_COLLECT_METADATA;
3808 	ipv6 = (cfg->flags & VXLAN_F_IPV6) || metadata;
3809 	ipv4 = !ipv6 || metadata;
3810 
3811 	RCU_INIT_POINTER(vxlan->vn4_sock, NULL);
3812 #if IS_ENABLED(CONFIG_IPV6)
3813 	RCU_INIT_POINTER(vxlan->vn6_sock, NULL);
3814 	if (ipv6) {
3815 		ret = __vxlan_sock_add(vxlan, true);
3816 		if (ret < 0 && ret != -EAFNOSUPPORT)
3817 			ipv4 = false;
3818 	}
3819 #endif
3820 	if (ipv4)
3821 		ret = __vxlan_sock_add(vxlan, false);
3822 	if (ret < 0)
3823 		vxlan_sock_release(vxlan);
3824 	return ret;
3825 }
3826 
3827 int vxlan_vni_in_use(struct net *src_net, struct vxlan_dev *vxlan,
3828 		     const struct vxlan_config *conf, __be32 vni)
3829 {
3830 	struct vxlan_net *vn = net_generic(src_net, vxlan_net_id);
3831 	struct vxlan_dev *tmp;
3832 
3833 	list_for_each_entry(tmp, &vn->vxlan_list, next) {
3834 		const struct vxlan_config *tmp_cfg;
3835 
3836 		if (tmp == vxlan)
3837 			continue;
3838 
3839 		tmp_cfg = rtnl_dereference(tmp->cfg);
3840 
3841 		if (tmp_cfg->flags & VXLAN_F_VNIFILTER) {
3842 			if (!vxlan_vnifilter_lookup(tmp, vni))
3843 				continue;
3844 		} else if (tmp_cfg->vni != vni) {
3845 			continue;
3846 		}
3847 		if (tmp_cfg->dst_port != conf->dst_port)
3848 			continue;
3849 		if ((tmp_cfg->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)) !=
3850 		    (conf->flags & (VXLAN_F_RCV_FLAGS | VXLAN_F_IPV6)))
3851 			continue;
3852 
3853 		if ((conf->flags & VXLAN_F_IPV6_LINKLOCAL) &&
3854 		    tmp_cfg->remote_ifindex != conf->remote_ifindex)
3855 			continue;
3856 
3857 		return -EEXIST;
3858 	}
3859 
3860 	return 0;
3861 }
3862 
3863 static int vxlan_config_validate(struct net *src_net, struct vxlan_config *conf,
3864 				 struct net_device **lower,
3865 				 struct vxlan_dev *old,
3866 				 struct netlink_ext_ack *extack)
3867 {
3868 	bool use_ipv6 = false;
3869 
3870 	if (conf->flags & VXLAN_F_GPE) {
3871 		/* For now, allow GPE only together with
3872 		 * COLLECT_METADATA. This can be relaxed later; in such
3873 		 * case, the other side of the PtP link will have to be
3874 		 * provided.
3875 		 */
3876 		if ((conf->flags & ~VXLAN_F_ALLOWED_GPE) ||
3877 		    !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3878 			NL_SET_ERR_MSG(extack,
3879 				       "VXLAN GPE does not support this combination of attributes");
3880 			return -EINVAL;
3881 		}
3882 	}
3883 
3884 	if (!conf->remote_ip.sa.sa_family && !conf->saddr.sa.sa_family) {
3885 		/* Unless IPv6 is explicitly requested, assume IPv4 */
3886 		conf->remote_ip.sa.sa_family = AF_INET;
3887 		conf->saddr.sa.sa_family = AF_INET;
3888 	} else if (!conf->remote_ip.sa.sa_family) {
3889 		conf->remote_ip.sa.sa_family = conf->saddr.sa.sa_family;
3890 	} else if (!conf->saddr.sa.sa_family) {
3891 		conf->saddr.sa.sa_family = conf->remote_ip.sa.sa_family;
3892 	}
3893 
3894 	if (conf->saddr.sa.sa_family != conf->remote_ip.sa.sa_family) {
3895 		NL_SET_ERR_MSG(extack,
3896 			       "Local and remote address must be from the same family");
3897 		return -EINVAL;
3898 	}
3899 
3900 	if (vxlan_addr_multicast(&conf->saddr)) {
3901 		NL_SET_ERR_MSG(extack, "Local address cannot be multicast");
3902 		return -EINVAL;
3903 	}
3904 
3905 	if (conf->saddr.sa.sa_family == AF_INET6) {
3906 		if (!IS_ENABLED(CONFIG_IPV6)) {
3907 			NL_SET_ERR_MSG(extack,
3908 				       "IPv6 support not enabled in the kernel");
3909 			return -EPFNOSUPPORT;
3910 		}
3911 		use_ipv6 = true;
3912 		conf->flags |= VXLAN_F_IPV6;
3913 
3914 		if (!(conf->flags & VXLAN_F_COLLECT_METADATA)) {
3915 			int local_type =
3916 				ipv6_addr_type(&conf->saddr.sin6.sin6_addr);
3917 			int remote_type =
3918 				ipv6_addr_type(&conf->remote_ip.sin6.sin6_addr);
3919 
3920 			if (local_type & IPV6_ADDR_LINKLOCAL) {
3921 				if (!(remote_type & IPV6_ADDR_LINKLOCAL) &&
3922 				    (remote_type != IPV6_ADDR_ANY)) {
3923 					NL_SET_ERR_MSG(extack,
3924 						       "Invalid combination of local and remote address scopes");
3925 					return -EINVAL;
3926 				}
3927 
3928 				conf->flags |= VXLAN_F_IPV6_LINKLOCAL;
3929 			} else {
3930 				if (remote_type ==
3931 				    (IPV6_ADDR_UNICAST | IPV6_ADDR_LINKLOCAL)) {
3932 					NL_SET_ERR_MSG(extack,
3933 						       "Invalid combination of local and remote address scopes");
3934 					return -EINVAL;
3935 				}
3936 
3937 				conf->flags &= ~VXLAN_F_IPV6_LINKLOCAL;
3938 			}
3939 		}
3940 	}
3941 
3942 	if (conf->label && !use_ipv6) {
3943 		NL_SET_ERR_MSG(extack,
3944 			       "Label attribute only applies to IPv6 VXLAN devices");
3945 		return -EINVAL;
3946 	}
3947 
3948 	if (conf->label_policy && !use_ipv6) {
3949 		NL_SET_ERR_MSG(extack,
3950 			       "Label policy only applies to IPv6 VXLAN devices");
3951 		return -EINVAL;
3952 	}
3953 
3954 	if (conf->remote_ifindex) {
3955 		struct net_device *lowerdev;
3956 
3957 		lowerdev = __dev_get_by_index(src_net, conf->remote_ifindex);
3958 		if (!lowerdev) {
3959 			NL_SET_ERR_MSG(extack,
3960 				       "Invalid local interface, device not found");
3961 			return -ENODEV;
3962 		}
3963 
3964 #if IS_ENABLED(CONFIG_IPV6)
3965 		if (use_ipv6) {
3966 			struct inet6_dev *idev = __in6_dev_get(lowerdev);
3967 
3968 			if (idev && idev->cnf.disable_ipv6) {
3969 				NL_SET_ERR_MSG(extack,
3970 					       "IPv6 support disabled by administrator");
3971 				return -EPERM;
3972 			}
3973 		}
3974 #endif
3975 
3976 		*lower = lowerdev;
3977 	} else {
3978 		if (vxlan_addr_multicast(&conf->remote_ip)) {
3979 			NL_SET_ERR_MSG(extack,
3980 				       "Local interface required for multicast remote destination");
3981 
3982 			return -EINVAL;
3983 		}
3984 
3985 		if ((conf->flags & VXLAN_F_VNIFILTER) && old &&
3986 		    vxlan_vnifilter_has_multicast(old)) {
3987 			NL_SET_ERR_MSG(extack,
3988 				       "Local interface required for multicast remote group");
3989 			return -EINVAL;
3990 		}
3991 
3992 #if IS_ENABLED(CONFIG_IPV6)
3993 		if (conf->flags & VXLAN_F_IPV6_LINKLOCAL) {
3994 			NL_SET_ERR_MSG(extack,
3995 				       "Local interface required for link-local local/remote addresses");
3996 			return -EINVAL;
3997 		}
3998 #endif
3999 
4000 		*lower = NULL;
4001 	}
4002 
4003 	if (!conf->dst_port) {
4004 		if (conf->flags & VXLAN_F_GPE)
4005 			conf->dst_port = htons(IANA_VXLAN_GPE_UDP_PORT);
4006 		else
4007 			conf->dst_port = htons(vxlan_port);
4008 	}
4009 
4010 	if (!conf->age_interval)
4011 		conf->age_interval = FDB_AGE_DEFAULT;
4012 
4013 	if (vxlan_vni_in_use(src_net, old, conf, conf->vni)) {
4014 		NL_SET_ERR_MSG(extack,
4015 			       "A VXLAN device with the specified VNI already exists");
4016 		return -EEXIST;
4017 	}
4018 
4019 	return 0;
4020 }
4021 
4022 static void vxlan_config_apply(struct net_device *dev,
4023 			       struct vxlan_config *new_cfg,
4024 			       struct net_device *lowerdev,
4025 			       struct net *src_net,
4026 			       bool changelink, bool lowerdev_changed)
4027 {
4028 	struct vxlan_dev *vxlan = netdev_priv(dev);
4029 	unsigned short needed_headroom = ETH_HLEN;
4030 	struct vxlan_config *old_cfg;
4031 	int max_mtu = ETH_MAX_MTU;
4032 	u32 flags = new_cfg->flags;
4033 
4034 	if (!changelink) {
4035 		if (flags & VXLAN_F_GPE)
4036 			vxlan_raw_setup(dev);
4037 		else
4038 			vxlan_ether_setup(dev);
4039 
4040 		if (new_cfg->mtu)
4041 			WRITE_ONCE(dev->mtu, new_cfg->mtu);
4042 
4043 		vxlan->net = src_net;
4044 	}
4045 
4046 	dev->needed_tailroom = 0;
4047 	if (lowerdev) {
4048 		netif_inherit_tso_max(dev, lowerdev);
4049 
4050 		needed_headroom = lowerdev->hard_header_len;
4051 		needed_headroom += lowerdev->needed_headroom;
4052 
4053 		dev->needed_tailroom = lowerdev->needed_tailroom;
4054 
4055 		max_mtu = READ_ONCE(lowerdev->mtu) - vxlan_headroom(flags);
4056 		if (max_mtu < ETH_MIN_MTU)
4057 			max_mtu = ETH_MIN_MTU;
4058 
4059 		if (!changelink && !new_cfg->mtu)
4060 			WRITE_ONCE(dev->mtu, max_mtu);
4061 	}
4062 
4063 	/* A changelink leaving the lower device alone must not shrink the mtu */
4064 	if (lowerdev_changed && READ_ONCE(dev->mtu) > max_mtu)
4065 		WRITE_ONCE(dev->mtu, max_mtu);
4066 
4067 	if (flags & VXLAN_F_COLLECT_METADATA)
4068 		flags |= VXLAN_F_IPV6;
4069 	needed_headroom += vxlan_headroom(flags);
4070 	dev->needed_headroom = needed_headroom;
4071 
4072 	old_cfg = rtnl_dereference(vxlan->cfg);
4073 	rcu_assign_pointer(vxlan->cfg, new_cfg);
4074 	if (old_cfg)
4075 		kfree_rcu(old_cfg, rcu);
4076 }
4077 
4078 static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,
4079 			       struct vxlan_config *conf,
4080 			       struct netlink_ext_ack *extack)
4081 {
4082 	struct vxlan_dev *vxlan = netdev_priv(dev);
4083 	struct net_device *lowerdev;
4084 	struct vxlan_config *new_cfg;
4085 	int ret;
4086 
4087 	ret = vxlan_config_validate(src_net, conf, &lowerdev, vxlan, extack);
4088 	if (ret)
4089 		return ret;
4090 
4091 	new_cfg = kmemdup(conf, sizeof(*conf), GFP_KERNEL);
4092 	if (!new_cfg)
4093 		return -ENOMEM;
4094 
4095 	vxlan_config_apply(dev, new_cfg, lowerdev, src_net, false, true);
4096 
4097 	return 0;
4098 }
4099 
4100 static int vxlan_dev_create(struct net *net, struct net_device *dev,
4101 			    struct vxlan_config *conf,
4102 			    struct netlink_ext_ack *extack)
4103 {
4104 	struct vxlan_net *vn = net_generic(net, vxlan_net_id);
4105 	struct vxlan_dev *vxlan = netdev_priv(dev);
4106 	struct net_device *remote_dev = NULL;
4107 	const struct vxlan_config *cfg;
4108 	int err;
4109 
4110 	err = vxlan_dev_configure(net, dev, conf, extack);
4111 	if (err)
4112 		return err;
4113 
4114 	cfg = rtnl_dereference(vxlan->cfg);
4115 
4116 	dev->ethtool_ops = &vxlan_ethtool_ops;
4117 
4118 	err = register_netdevice(dev);
4119 	if (err) {
4120 		vxlan_free_dev(dev);
4121 		return err;
4122 	}
4123 
4124 	if (cfg->remote_ifindex) {
4125 		remote_dev = __dev_get_by_index(net, cfg->remote_ifindex);
4126 		if (!remote_dev) {
4127 			err = -ENODEV;
4128 			goto unregister;
4129 		}
4130 
4131 		err = netdev_upper_dev_link(remote_dev, dev, extack);
4132 		if (err)
4133 			goto unregister;
4134 
4135 		vxlan->lowerdev = remote_dev;
4136 	}
4137 
4138 	err = rtnl_configure_link(dev, NULL, 0, NULL);
4139 	if (err < 0)
4140 		goto unlink;
4141 
4142 	/* create an fdb entry for a valid default destination */
4143 	if (!vxlan_addr_any(&cfg->remote_ip)) {
4144 		union vxlan_addr rip = cfg->remote_ip;
4145 
4146 		spin_lock_bh(&vxlan->hash_lock);
4147 		err = vxlan_fdb_update(vxlan, all_zeros_mac,
4148 				       &rip,
4149 				       NUD_REACHABLE | NUD_PERMANENT,
4150 				       NLM_F_EXCL | NLM_F_CREATE,
4151 				       cfg->dst_port,
4152 				       cfg->vni,
4153 				       cfg->vni,
4154 				       cfg->remote_ifindex,
4155 				       NTF_SELF, 0, true, extack);
4156 		spin_unlock_bh(&vxlan->hash_lock);
4157 		if (err)
4158 			goto unlink;
4159 	}
4160 
4161 	list_add(&vxlan->next, &vn->vxlan_list);
4162 
4163 	return 0;
4164 
4165 unlink:
4166 	if (remote_dev)
4167 		netdev_upper_dev_unlink(remote_dev, dev);
4168 unregister:
4169 	unregister_netdevice(dev);
4170 	return err;
4171 }
4172 
4173 /* Set/clear flags based on attribute */
4174 static int vxlan_nl2flag(struct vxlan_config *conf, struct nlattr *tb[],
4175 			  int attrtype, unsigned long mask, bool changelink,
4176 			  bool changelink_supported,
4177 			  struct netlink_ext_ack *extack)
4178 {
4179 	unsigned long flags;
4180 
4181 	if (!tb[attrtype])
4182 		return 0;
4183 
4184 	if (changelink && !changelink_supported) {
4185 		vxlan_flag_attr_error(attrtype, extack);
4186 		return -EOPNOTSUPP;
4187 	}
4188 
4189 	if (vxlan_policy[attrtype].type == NLA_FLAG)
4190 		flags = conf->flags | mask;
4191 	else if (nla_get_u8(tb[attrtype]))
4192 		flags = conf->flags | mask;
4193 	else
4194 		flags = conf->flags & ~mask;
4195 
4196 	conf->flags = flags;
4197 
4198 	return 0;
4199 }
4200 
4201 static int vxlan_nl2conf(struct nlattr *tb[], struct nlattr *data[],
4202 			 struct net_device *dev, struct vxlan_config *conf,
4203 			 bool changelink, struct netlink_ext_ack *extack)
4204 {
4205 	struct vxlanhdr used_bits = {
4206 		.vx_flags = VXLAN_HF_VNI,
4207 		.vx_vni = VXLAN_VNI_MASK,
4208 	};
4209 	struct vxlan_dev *vxlan = netdev_priv(dev);
4210 	int err = 0;
4211 
4212 	memset(conf, 0, sizeof(*conf));
4213 
4214 	/* if changelink operation, start with old existing cfg */
4215 	if (changelink) {
4216 		const struct vxlan_config *cfg = rtnl_dereference(vxlan->cfg);
4217 
4218 		if (cfg)
4219 			memcpy(conf, cfg, sizeof(*conf));
4220 	}
4221 
4222 	if (data[IFLA_VXLAN_ID]) {
4223 		__be32 vni = cpu_to_be32(nla_get_u32(data[IFLA_VXLAN_ID]));
4224 
4225 		if (changelink && (vni != conf->vni)) {
4226 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_ID], "Cannot change VNI");
4227 			return -EOPNOTSUPP;
4228 		}
4229 		conf->vni = vni;
4230 	}
4231 
4232 	if (data[IFLA_VXLAN_GROUP]) {
4233 		if (changelink && (conf->remote_ip.sa.sa_family != AF_INET)) {
4234 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP], "New group address family does not match old group");
4235 			return -EOPNOTSUPP;
4236 		}
4237 
4238 		conf->remote_ip.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_GROUP]);
4239 		conf->remote_ip.sa.sa_family = AF_INET;
4240 	} else if (data[IFLA_VXLAN_GROUP6]) {
4241 		if (!IS_ENABLED(CONFIG_IPV6)) {
4242 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "IPv6 support not enabled in the kernel");
4243 			return -EPFNOSUPPORT;
4244 		}
4245 
4246 		if (changelink && (conf->remote_ip.sa.sa_family != AF_INET6)) {
4247 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_GROUP6], "New group address family does not match old group");
4248 			return -EOPNOTSUPP;
4249 		}
4250 
4251 		conf->remote_ip.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_GROUP6]);
4252 		conf->remote_ip.sa.sa_family = AF_INET6;
4253 	}
4254 
4255 	if (data[IFLA_VXLAN_LOCAL]) {
4256 		if (changelink && (conf->saddr.sa.sa_family != AF_INET)) {
4257 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL], "New local address family does not match old");
4258 			return -EOPNOTSUPP;
4259 		}
4260 
4261 		conf->saddr.sin.sin_addr.s_addr = nla_get_in_addr(data[IFLA_VXLAN_LOCAL]);
4262 		conf->saddr.sa.sa_family = AF_INET;
4263 	} else if (data[IFLA_VXLAN_LOCAL6]) {
4264 		if (!IS_ENABLED(CONFIG_IPV6)) {
4265 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "IPv6 support not enabled in the kernel");
4266 			return -EPFNOSUPPORT;
4267 		}
4268 
4269 		if (changelink && (conf->saddr.sa.sa_family != AF_INET6)) {
4270 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LOCAL6], "New local address family does not match old");
4271 			return -EOPNOTSUPP;
4272 		}
4273 
4274 		/* TODO: respect scope id */
4275 		conf->saddr.sin6.sin6_addr = nla_get_in6_addr(data[IFLA_VXLAN_LOCAL6]);
4276 		conf->saddr.sa.sa_family = AF_INET6;
4277 	}
4278 
4279 	if (data[IFLA_VXLAN_LINK])
4280 		conf->remote_ifindex = nla_get_u32(data[IFLA_VXLAN_LINK]);
4281 
4282 	if (data[IFLA_VXLAN_TOS])
4283 		conf->tos  = nla_get_u8(data[IFLA_VXLAN_TOS]);
4284 
4285 	if (data[IFLA_VXLAN_TTL])
4286 		conf->ttl = nla_get_u8(data[IFLA_VXLAN_TTL]);
4287 
4288 	if (data[IFLA_VXLAN_TTL_INHERIT]) {
4289 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_TTL_INHERIT,
4290 				    VXLAN_F_TTL_INHERIT, changelink, false,
4291 				    extack);
4292 		if (err)
4293 			return err;
4294 
4295 	}
4296 
4297 	if (data[IFLA_VXLAN_LABEL])
4298 		conf->label = nla_get_be32(data[IFLA_VXLAN_LABEL]) &
4299 			     IPV6_FLOWLABEL_MASK;
4300 	if (data[IFLA_VXLAN_LABEL_POLICY])
4301 		conf->label_policy = nla_get_u32(data[IFLA_VXLAN_LABEL_POLICY]);
4302 
4303 	if (data[IFLA_VXLAN_LEARNING]) {
4304 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LEARNING,
4305 				    VXLAN_F_LEARN, changelink, true,
4306 				    extack);
4307 		if (err)
4308 			return err;
4309 	} else if (!changelink) {
4310 		/* default to learn on a new device */
4311 		conf->flags |= VXLAN_F_LEARN;
4312 	}
4313 
4314 	if (data[IFLA_VXLAN_AGEING])
4315 		conf->age_interval = nla_get_u32(data[IFLA_VXLAN_AGEING]);
4316 
4317 	if (data[IFLA_VXLAN_PROXY]) {
4318 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_PROXY,
4319 				    VXLAN_F_PROXY, changelink, false,
4320 				    extack);
4321 		if (err)
4322 			return err;
4323 	}
4324 
4325 	if (data[IFLA_VXLAN_RSC]) {
4326 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_RSC,
4327 				    VXLAN_F_RSC, changelink, false,
4328 				    extack);
4329 		if (err)
4330 			return err;
4331 	}
4332 
4333 	if (data[IFLA_VXLAN_L2MISS]) {
4334 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L2MISS,
4335 				    VXLAN_F_L2MISS, changelink, false,
4336 				    extack);
4337 		if (err)
4338 			return err;
4339 	}
4340 
4341 	if (data[IFLA_VXLAN_L3MISS]) {
4342 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_L3MISS,
4343 				    VXLAN_F_L3MISS, changelink, false,
4344 				    extack);
4345 		if (err)
4346 			return err;
4347 	}
4348 
4349 	if (data[IFLA_VXLAN_LIMIT]) {
4350 		if (changelink) {
4351 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_LIMIT],
4352 					    "Cannot change limit");
4353 			return -EOPNOTSUPP;
4354 		}
4355 		conf->addrmax = nla_get_u32(data[IFLA_VXLAN_LIMIT]);
4356 	}
4357 
4358 	if (data[IFLA_VXLAN_COLLECT_METADATA]) {
4359 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_COLLECT_METADATA,
4360 				    VXLAN_F_COLLECT_METADATA, changelink, false,
4361 				    extack);
4362 		if (err)
4363 			return err;
4364 	}
4365 
4366 	if (data[IFLA_VXLAN_PORT_RANGE]) {
4367 		if (!changelink) {
4368 			const struct ifla_vxlan_port_range *p
4369 				= nla_data(data[IFLA_VXLAN_PORT_RANGE]);
4370 			conf->port_min = ntohs(p->low);
4371 			conf->port_max = ntohs(p->high);
4372 		} else {
4373 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT_RANGE],
4374 					    "Cannot change port range");
4375 			return -EOPNOTSUPP;
4376 		}
4377 	}
4378 
4379 	if (data[IFLA_VXLAN_PORT]) {
4380 		if (changelink) {
4381 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_PORT],
4382 					    "Cannot change port");
4383 			return -EOPNOTSUPP;
4384 		}
4385 		conf->dst_port = nla_get_be16(data[IFLA_VXLAN_PORT]);
4386 	}
4387 
4388 	if (data[IFLA_VXLAN_UDP_CSUM]) {
4389 		if (changelink) {
4390 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_VXLAN_UDP_CSUM],
4391 					    "Cannot change UDP_CSUM flag");
4392 			return -EOPNOTSUPP;
4393 		}
4394 		if (!nla_get_u8(data[IFLA_VXLAN_UDP_CSUM]))
4395 			conf->flags |= VXLAN_F_UDP_ZERO_CSUM_TX;
4396 	}
4397 
4398 	if (data[IFLA_VXLAN_LOCALBYPASS]) {
4399 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_LOCALBYPASS,
4400 				    VXLAN_F_LOCALBYPASS, changelink,
4401 				    true, extack);
4402 		if (err)
4403 			return err;
4404 	} else if (!changelink) {
4405 		/* default to local bypass on a new device */
4406 		conf->flags |= VXLAN_F_LOCALBYPASS;
4407 	}
4408 
4409 	if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_TX]) {
4410 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4411 				    VXLAN_F_UDP_ZERO_CSUM6_TX, changelink,
4412 				    false, extack);
4413 		if (err)
4414 			return err;
4415 	}
4416 
4417 	if (data[IFLA_VXLAN_UDP_ZERO_CSUM6_RX]) {
4418 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4419 				    VXLAN_F_UDP_ZERO_CSUM6_RX, changelink,
4420 				    false, extack);
4421 		if (err)
4422 			return err;
4423 	}
4424 
4425 	if (data[IFLA_VXLAN_REMCSUM_TX]) {
4426 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_TX,
4427 				    VXLAN_F_REMCSUM_TX, changelink, false,
4428 				    extack);
4429 		if (err)
4430 			return err;
4431 	}
4432 
4433 	if (data[IFLA_VXLAN_REMCSUM_RX]) {
4434 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_RX,
4435 				    VXLAN_F_REMCSUM_RX, changelink, false,
4436 				    extack);
4437 		if (err)
4438 			return err;
4439 		used_bits.vx_flags |= VXLAN_HF_RCO;
4440 		used_bits.vx_vni |= ~VXLAN_VNI_MASK;
4441 	}
4442 
4443 	if (data[IFLA_VXLAN_GBP]) {
4444 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GBP,
4445 				    VXLAN_F_GBP, changelink, false, extack);
4446 		if (err)
4447 			return err;
4448 		used_bits.vx_flags |= VXLAN_GBP_USED_BITS;
4449 	}
4450 
4451 	if (data[IFLA_VXLAN_GPE]) {
4452 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_GPE,
4453 				    VXLAN_F_GPE, changelink, false,
4454 				    extack);
4455 		if (err)
4456 			return err;
4457 
4458 		used_bits.vx_flags |= VXLAN_GPE_USED_BITS;
4459 	}
4460 
4461 	if (data[IFLA_VXLAN_RESERVED_BITS]) {
4462 		struct vxlanhdr reserved_bits;
4463 
4464 		if (changelink) {
4465 			NL_SET_ERR_MSG_ATTR(extack,
4466 					    data[IFLA_VXLAN_RESERVED_BITS],
4467 					    "Cannot change reserved_bits");
4468 			return -EOPNOTSUPP;
4469 		}
4470 
4471 		nla_memcpy(&reserved_bits, data[IFLA_VXLAN_RESERVED_BITS],
4472 			   sizeof(reserved_bits));
4473 		if (used_bits.vx_flags & reserved_bits.vx_flags ||
4474 		    used_bits.vx_vni & reserved_bits.vx_vni) {
4475 			__be64 ub_be64, rb_be64;
4476 
4477 			memcpy(&ub_be64, &used_bits, sizeof(ub_be64));
4478 			memcpy(&rb_be64, &reserved_bits, sizeof(rb_be64));
4479 
4480 			NL_SET_ERR_MSG_ATTR_FMT(extack,
4481 						data[IFLA_VXLAN_RESERVED_BITS],
4482 						"Used bits %#018llx cannot overlap reserved bits %#018llx",
4483 						be64_to_cpu(ub_be64),
4484 						be64_to_cpu(rb_be64));
4485 			return -EINVAL;
4486 		}
4487 
4488 		conf->reserved_bits = reserved_bits;
4489 	} else {
4490 		/* For backwards compatibility, only allow reserved fields to be
4491 		 * used by VXLAN extensions if explicitly requested.
4492 		 */
4493 		conf->reserved_bits = (struct vxlanhdr) {
4494 			.vx_flags = ~used_bits.vx_flags,
4495 			.vx_vni = ~used_bits.vx_vni,
4496 		};
4497 	}
4498 
4499 	if (data[IFLA_VXLAN_REMCSUM_NOPARTIAL]) {
4500 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_REMCSUM_NOPARTIAL,
4501 				    VXLAN_F_REMCSUM_NOPARTIAL, changelink,
4502 				    false, extack);
4503 		if (err)
4504 			return err;
4505 	}
4506 
4507 	if (data[IFLA_VXLAN_MC_ROUTE]) {
4508 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_MC_ROUTE,
4509 				    VXLAN_F_MC_ROUTE, changelink,
4510 				    true, extack);
4511 		if (err)
4512 			return err;
4513 	}
4514 
4515 	if (tb[IFLA_MTU]) {
4516 		if (changelink) {
4517 			NL_SET_ERR_MSG_ATTR(extack, tb[IFLA_MTU],
4518 					    "Cannot change mtu");
4519 			return -EOPNOTSUPP;
4520 		}
4521 		conf->mtu = nla_get_u32(tb[IFLA_MTU]);
4522 	}
4523 
4524 	if (data[IFLA_VXLAN_DF])
4525 		conf->df = nla_get_u8(data[IFLA_VXLAN_DF]);
4526 
4527 	if (data[IFLA_VXLAN_VNIFILTER]) {
4528 		err = vxlan_nl2flag(conf, data, IFLA_VXLAN_VNIFILTER,
4529 				    VXLAN_F_VNIFILTER, changelink, false,
4530 				    extack);
4531 		if (err)
4532 			return err;
4533 
4534 		if ((conf->flags & VXLAN_F_VNIFILTER) &&
4535 		    !(conf->flags & VXLAN_F_COLLECT_METADATA)) {
4536 			NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VXLAN_VNIFILTER],
4537 					    "vxlan vnifilter only valid in collect metadata mode");
4538 			return -EINVAL;
4539 		}
4540 	}
4541 
4542 	return 0;
4543 }
4544 
4545 static int vxlan_newlink(struct net_device *dev,
4546 			 struct rtnl_newlink_params *params,
4547 			 struct netlink_ext_ack *extack)
4548 {
4549 	struct net *link_net = rtnl_newlink_link_net(params);
4550 	struct nlattr **data = params->data;
4551 	struct nlattr **tb = params->tb;
4552 	struct vxlan_config conf;
4553 	int err;
4554 
4555 	err = vxlan_nl2conf(tb, data, dev, &conf, false, extack);
4556 	if (err)
4557 		return err;
4558 
4559 	return vxlan_dev_create(link_net, dev, &conf, extack);
4560 }
4561 
4562 static int vxlan_changelink(struct net_device *dev, struct nlattr *tb[],
4563 			    struct nlattr *data[],
4564 			    struct netlink_ext_ack *extack)
4565 {
4566 	bool lowerdev_changed, rem_ip_changed, change_igmp;
4567 	struct vxlan_dev *vxlan = netdev_priv(dev);
4568 	const struct vxlan_config *cfg;
4569 	struct vxlan_config *new_cfg;
4570 	struct net_device *lowerdev;
4571 	struct vxlan_config conf;
4572 	int err;
4573 
4574 	cfg = rtnl_dereference(vxlan->cfg);
4575 
4576 	if (!rtnl_dev_link_net_capable(dev, vxlan->net))
4577 		return -EPERM;
4578 
4579 	err = vxlan_nl2conf(tb, data, dev, &conf, true, extack);
4580 	if (err)
4581 		return err;
4582 
4583 	err = vxlan_config_validate(vxlan->net, &conf, &lowerdev,
4584 				    vxlan, extack);
4585 	if (err)
4586 		return err;
4587 
4588 	new_cfg = kmemdup(&conf, sizeof(conf), GFP_KERNEL);
4589 	if (!new_cfg)
4590 		return -ENOMEM;
4591 
4592 	lowerdev_changed = vxlan->lowerdev != lowerdev;
4593 	if (lowerdev_changed) {
4594 		err = netdev_adjacent_change_prepare(vxlan->lowerdev, lowerdev,
4595 						     dev, extack);
4596 		if (err) {
4597 			kfree(new_cfg);
4598 			return err;
4599 		}
4600 	}
4601 
4602 	rem_ip_changed = !vxlan_addr_equal(&conf.remote_ip, &cfg->remote_ip);
4603 	change_igmp = vxlan->dev->flags & IFF_UP &&
4604 		      (rem_ip_changed ||
4605 		       cfg->remote_ifindex != conf.remote_ifindex);
4606 
4607 	/* handle default dst entry */
4608 	if (rem_ip_changed || cfg->remote_ifindex != conf.remote_ifindex) {
4609 		spin_lock_bh(&vxlan->hash_lock);
4610 		if (!vxlan_addr_any(&conf.remote_ip)) {
4611 			err = vxlan_fdb_update(vxlan, all_zeros_mac,
4612 					       &conf.remote_ip,
4613 					       NUD_REACHABLE | NUD_PERMANENT,
4614 					       NLM_F_APPEND | NLM_F_CREATE,
4615 					       cfg->dst_port,
4616 					       conf.vni, conf.vni,
4617 					       conf.remote_ifindex,
4618 					       NTF_SELF, 0, true, extack);
4619 			if (err) {
4620 				spin_unlock_bh(&vxlan->hash_lock);
4621 				if (lowerdev_changed)
4622 					netdev_adjacent_change_abort(vxlan->lowerdev,
4623 								     lowerdev, dev);
4624 				kfree(new_cfg);
4625 				return err;
4626 			}
4627 		}
4628 		if (!vxlan_addr_any(&cfg->remote_ip))
4629 			__vxlan_fdb_delete(vxlan, all_zeros_mac,
4630 					   cfg->remote_ip,
4631 					   cfg->dst_port,
4632 					   cfg->vni,
4633 					   cfg->vni,
4634 					   cfg->remote_ifindex,
4635 					   true);
4636 		spin_unlock_bh(&vxlan->hash_lock);
4637 
4638 		/* If vni filtering device, also update default fdb entries of
4639 		 * all vnis
4640 		 */
4641 		if (cfg->flags & VXLAN_F_VNIFILTER) {
4642 			err = vxlan_vnilist_update_group(vxlan, &cfg->remote_ip,
4643 							 &conf.remote_ip,
4644 							 cfg->remote_ifindex,
4645 							 conf.remote_ifindex,
4646 							 extack);
4647 			if (err) {
4648 				vxlan_update_default_fdb_entry(vxlan, conf.vni,
4649 							       &conf.remote_ip,
4650 							       &cfg->remote_ip,
4651 							       conf.remote_ifindex,
4652 							       cfg->remote_ifindex,
4653 							       NULL);
4654 				if (lowerdev_changed)
4655 					netdev_adjacent_change_abort(vxlan->lowerdev,
4656 								     lowerdev, dev);
4657 				kfree(new_cfg);
4658 				return err;
4659 			}
4660 		}
4661 	}
4662 
4663 	if (change_igmp &&
4664 	    (vxlan_addr_multicast(&cfg->remote_ip) ||
4665 	     (cfg->flags & VXLAN_F_VNIFILTER)))
4666 		err = vxlan_multicast_leave(vxlan);
4667 
4668 	if (netif_running(dev) && conf.age_interval != cfg->age_interval)
4669 		mod_timer(&vxlan->age_timer, jiffies);
4670 
4671 	if (lowerdev_changed) {
4672 		if (lowerdev)
4673 			netdev_adjacent_change_commit(vxlan->lowerdev, lowerdev,
4674 						      dev);
4675 		else
4676 			netdev_upper_dev_unlink(vxlan->lowerdev, dev);
4677 		vxlan->lowerdev = lowerdev;
4678 	}
4679 	vxlan_config_apply(dev, new_cfg, lowerdev, vxlan->net, true,
4680 			   lowerdev_changed);
4681 
4682 	if (change_igmp &&
4683 	    (vxlan_addr_multicast(&new_cfg->remote_ip) ||
4684 	     (new_cfg->flags & VXLAN_F_VNIFILTER))) {
4685 		int join_err = vxlan_multicast_join(vxlan);
4686 
4687 		if (join_err)
4688 			err = join_err;
4689 	}
4690 
4691 	return err;
4692 }
4693 
4694 static void vxlan_dellink(struct net_device *dev, struct list_head *head)
4695 {
4696 	struct vxlan_dev *vxlan = netdev_priv(dev);
4697 	struct vxlan_fdb_flush_desc desc = {};
4698 
4699 	vxlan_flush(vxlan, &desc);
4700 
4701 	list_del(&vxlan->next);
4702 	unregister_netdevice_queue(dev, head);
4703 	if (vxlan->lowerdev)
4704 		netdev_upper_dev_unlink(vxlan->lowerdev, dev);
4705 }
4706 
4707 static size_t vxlan_get_size(const struct net_device *dev)
4708 {
4709 	return nla_total_size(sizeof(__u32)) +	/* IFLA_VXLAN_ID */
4710 		nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_GROUP{6} */
4711 		nla_total_size(sizeof(__u32)) +	/* IFLA_VXLAN_LINK */
4712 		nla_total_size(sizeof(struct in6_addr)) + /* IFLA_VXLAN_LOCAL{6} */
4713 		nla_total_size(sizeof(__u8)) +	/* IFLA_VXLAN_TTL */
4714 		nla_total_size(sizeof(__u8)) +	/* IFLA_VXLAN_TTL_INHERIT */
4715 		nla_total_size(sizeof(__u8)) +	/* IFLA_VXLAN_TOS */
4716 		nla_total_size(sizeof(__u8)) +	/* IFLA_VXLAN_DF */
4717 		nla_total_size(sizeof(__be32)) + /* IFLA_VXLAN_LABEL */
4718 		nla_total_size(sizeof(__u32)) +  /* IFLA_VXLAN_LABEL_POLICY */
4719 		nla_total_size(sizeof(__u8)) +	/* IFLA_VXLAN_LEARNING */
4720 		nla_total_size(sizeof(__u8)) +	/* IFLA_VXLAN_PROXY */
4721 		nla_total_size(sizeof(__u8)) +	/* IFLA_VXLAN_RSC */
4722 		nla_total_size(sizeof(__u8)) +	/* IFLA_VXLAN_L2MISS */
4723 		nla_total_size(sizeof(__u8)) +	/* IFLA_VXLAN_L3MISS */
4724 		nla_total_size(sizeof(__u8)) +	/* IFLA_VXLAN_COLLECT_METADATA */
4725 		nla_total_size(sizeof(__u32)) +	/* IFLA_VXLAN_AGEING */
4726 		nla_total_size(sizeof(__u32)) +	/* IFLA_VXLAN_LIMIT */
4727 		nla_total_size(sizeof(__be16)) + /* IFLA_VXLAN_PORT */
4728 		nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_CSUM */
4729 		nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_TX */
4730 		nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_UDP_ZERO_CSUM6_RX */
4731 		nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_TX */
4732 		nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_REMCSUM_RX */
4733 		nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_LOCALBYPASS */
4734 		/* IFLA_VXLAN_PORT_RANGE */
4735 		nla_total_size(sizeof(struct ifla_vxlan_port_range)) +
4736 		nla_total_size(0) + /* IFLA_VXLAN_GBP */
4737 		nla_total_size(0) + /* IFLA_VXLAN_GPE */
4738 		nla_total_size(0) + /* IFLA_VXLAN_REMCSUM_NOPARTIAL */
4739 		nla_total_size(sizeof(__u8)) + /* IFLA_VXLAN_VNIFILTER */
4740 		/* IFLA_VXLAN_RESERVED_BITS */
4741 		nla_total_size(sizeof(struct vxlanhdr)) +
4742 		0;
4743 }
4744 
4745 static int vxlan_fill_info(struct sk_buff *skb, const struct net_device *dev)
4746 {
4747 	const struct vxlan_dev *vxlan = netdev_priv(dev);
4748 	struct ifla_vxlan_port_range ports;
4749 	const struct vxlan_config *cfg;
4750 
4751 	rcu_read_lock();
4752 	cfg = rcu_dereference(vxlan->cfg);
4753 
4754 	if (nla_put_u32(skb, IFLA_VXLAN_ID, be32_to_cpu(cfg->vni)))
4755 		goto nla_put_failure;
4756 
4757 	if (!vxlan_addr_any(&cfg->remote_ip)) {
4758 		if (cfg->remote_ip.sa.sa_family == AF_INET) {
4759 			if (nla_put_in_addr(skb, IFLA_VXLAN_GROUP,
4760 					    cfg->remote_ip.sin.sin_addr.s_addr))
4761 				goto nla_put_failure;
4762 #if IS_ENABLED(CONFIG_IPV6)
4763 		} else {
4764 			if (nla_put_in6_addr(skb, IFLA_VXLAN_GROUP6,
4765 					     &cfg->remote_ip.sin6.sin6_addr))
4766 				goto nla_put_failure;
4767 #endif
4768 		}
4769 	}
4770 
4771 	if (cfg->remote_ifindex && nla_put_u32(skb, IFLA_VXLAN_LINK, cfg->remote_ifindex))
4772 		goto nla_put_failure;
4773 
4774 	if (!vxlan_addr_any(&cfg->saddr)) {
4775 		if (cfg->saddr.sa.sa_family == AF_INET) {
4776 			if (nla_put_in_addr(skb, IFLA_VXLAN_LOCAL,
4777 					    cfg->saddr.sin.sin_addr.s_addr))
4778 				goto nla_put_failure;
4779 #if IS_ENABLED(CONFIG_IPV6)
4780 		} else {
4781 			if (nla_put_in6_addr(skb, IFLA_VXLAN_LOCAL6,
4782 					     &cfg->saddr.sin6.sin6_addr))
4783 				goto nla_put_failure;
4784 #endif
4785 		}
4786 	}
4787 
4788 	if (nla_put_u8(skb, IFLA_VXLAN_TTL, cfg->ttl) ||
4789 	    nla_put_u8(skb, IFLA_VXLAN_TTL_INHERIT,
4790 		       !!(cfg->flags & VXLAN_F_TTL_INHERIT)) ||
4791 	    nla_put_u8(skb, IFLA_VXLAN_TOS, cfg->tos) ||
4792 	    nla_put_u8(skb, IFLA_VXLAN_DF, cfg->df) ||
4793 	    nla_put_be32(skb, IFLA_VXLAN_LABEL, cfg->label) ||
4794 	    nla_put_u32(skb, IFLA_VXLAN_LABEL_POLICY, cfg->label_policy) ||
4795 	    nla_put_u8(skb, IFLA_VXLAN_LEARNING,
4796 		       !!(cfg->flags & VXLAN_F_LEARN)) ||
4797 	    nla_put_u8(skb, IFLA_VXLAN_PROXY,
4798 		       !!(cfg->flags & VXLAN_F_PROXY)) ||
4799 	    nla_put_u8(skb, IFLA_VXLAN_RSC,
4800 		       !!(cfg->flags & VXLAN_F_RSC)) ||
4801 	    nla_put_u8(skb, IFLA_VXLAN_L2MISS,
4802 		       !!(cfg->flags & VXLAN_F_L2MISS)) ||
4803 	    nla_put_u8(skb, IFLA_VXLAN_L3MISS,
4804 		       !!(cfg->flags & VXLAN_F_L3MISS)) ||
4805 	    nla_put_u8(skb, IFLA_VXLAN_COLLECT_METADATA,
4806 		       !!(cfg->flags & VXLAN_F_COLLECT_METADATA)) ||
4807 	    nla_put_u32(skb, IFLA_VXLAN_AGEING, cfg->age_interval) ||
4808 	    nla_put_u32(skb, IFLA_VXLAN_LIMIT, cfg->addrmax) ||
4809 	    nla_put_be16(skb, IFLA_VXLAN_PORT, cfg->dst_port) ||
4810 	    nla_put_u8(skb, IFLA_VXLAN_UDP_CSUM,
4811 		       !(cfg->flags & VXLAN_F_UDP_ZERO_CSUM_TX)) ||
4812 	    nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_TX,
4813 		       !!(cfg->flags & VXLAN_F_UDP_ZERO_CSUM6_TX)) ||
4814 	    nla_put_u8(skb, IFLA_VXLAN_UDP_ZERO_CSUM6_RX,
4815 		       !!(cfg->flags & VXLAN_F_UDP_ZERO_CSUM6_RX)) ||
4816 	    nla_put_u8(skb, IFLA_VXLAN_REMCSUM_TX,
4817 		       !!(cfg->flags & VXLAN_F_REMCSUM_TX)) ||
4818 	    nla_put_u8(skb, IFLA_VXLAN_REMCSUM_RX,
4819 		       !!(cfg->flags & VXLAN_F_REMCSUM_RX)) ||
4820 	    nla_put_u8(skb, IFLA_VXLAN_LOCALBYPASS,
4821 		       !!(cfg->flags & VXLAN_F_LOCALBYPASS)))
4822 		goto nla_put_failure;
4823 
4824 	ports.low = htons(cfg->port_min);
4825 	ports.high = htons(cfg->port_max);
4826 	if (nla_put(skb, IFLA_VXLAN_PORT_RANGE, sizeof(ports), &ports))
4827 		goto nla_put_failure;
4828 
4829 	if (cfg->flags & VXLAN_F_GBP &&
4830 	    nla_put_flag(skb, IFLA_VXLAN_GBP))
4831 		goto nla_put_failure;
4832 
4833 	if (cfg->flags & VXLAN_F_GPE &&
4834 	    nla_put_flag(skb, IFLA_VXLAN_GPE))
4835 		goto nla_put_failure;
4836 
4837 	if (cfg->flags & VXLAN_F_REMCSUM_NOPARTIAL &&
4838 	    nla_put_flag(skb, IFLA_VXLAN_REMCSUM_NOPARTIAL))
4839 		goto nla_put_failure;
4840 
4841 	if (cfg->flags & VXLAN_F_VNIFILTER &&
4842 	    nla_put_u8(skb, IFLA_VXLAN_VNIFILTER,
4843 		       !!(cfg->flags & VXLAN_F_VNIFILTER)))
4844 		goto nla_put_failure;
4845 
4846 	if (nla_put(skb, IFLA_VXLAN_RESERVED_BITS,
4847 		    sizeof(cfg->reserved_bits),
4848 		    &cfg->reserved_bits))
4849 		goto nla_put_failure;
4850 
4851 	rcu_read_unlock();
4852 	return 0;
4853 
4854 nla_put_failure:
4855 	rcu_read_unlock();
4856 	return -EMSGSIZE;
4857 }
4858 
4859 static struct net *vxlan_get_link_net(const struct net_device *dev)
4860 {
4861 	struct vxlan_dev *vxlan = netdev_priv(dev);
4862 
4863 	return READ_ONCE(vxlan->net);
4864 }
4865 
4866 static struct rtnl_link_ops vxlan_link_ops __read_mostly = {
4867 	.kind		= "vxlan",
4868 	.maxtype	= IFLA_VXLAN_MAX,
4869 	.policy		= vxlan_policy,
4870 	.priv_size	= sizeof(struct vxlan_dev),
4871 	.setup		= vxlan_setup,
4872 	.validate	= vxlan_validate,
4873 	.newlink	= vxlan_newlink,
4874 	.changelink	= vxlan_changelink,
4875 	.dellink	= vxlan_dellink,
4876 	.get_size	= vxlan_get_size,
4877 	.fill_info	= vxlan_fill_info,
4878 	.get_link_net	= vxlan_get_link_net,
4879 };
4880 
4881 static void vxlan_handle_lowerdev_unregister(struct vxlan_net *vn,
4882 					     struct net_device *dev)
4883 {
4884 	struct vxlan_dev *vxlan, *next;
4885 	LIST_HEAD(list_kill);
4886 
4887 	list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next) {
4888 		const struct vxlan_config *cfg = rtnl_dereference(vxlan->cfg);
4889 
4890 		/* In case we created vxlan device with carrier
4891 		 * and we loose the carrier due to module unload
4892 		 * we also need to remove vxlan device. In other
4893 		 * cases, it's not necessary and remote_ifindex
4894 		 * is 0 here, so no matches.
4895 		 */
4896 		if (cfg->remote_ifindex == dev->ifindex)
4897 			vxlan_dellink(vxlan->dev, &list_kill);
4898 	}
4899 
4900 	unregister_netdevice_many(&list_kill);
4901 }
4902 
4903 static int vxlan_netdevice_event(struct notifier_block *unused,
4904 				 unsigned long event, void *ptr)
4905 {
4906 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
4907 	struct vxlan_net *vn = net_generic(dev_net(dev), vxlan_net_id);
4908 
4909 	if ((event == NETDEV_REGISTER || event == NETDEV_UNREGISTER) &&
4910 	    netif_is_vxlan(dev))
4911 		vxlan_vnifilter_seq_inc(dev_net(dev));
4912 
4913 	if (event == NETDEV_UNREGISTER)
4914 		vxlan_handle_lowerdev_unregister(vn, dev);
4915 	else if (event == NETDEV_UDP_TUNNEL_PUSH_INFO)
4916 		vxlan_offload_rx_ports(dev, true);
4917 	else if (event == NETDEV_UDP_TUNNEL_DROP_INFO)
4918 		vxlan_offload_rx_ports(dev, false);
4919 
4920 	return NOTIFY_DONE;
4921 }
4922 
4923 static struct notifier_block vxlan_notifier_block __read_mostly = {
4924 	.notifier_call = vxlan_netdevice_event,
4925 };
4926 
4927 static void
4928 vxlan_fdb_offloaded_set(struct net_device *dev,
4929 			struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4930 {
4931 	struct vxlan_dev *vxlan = netdev_priv(dev);
4932 	struct vxlan_rdst *rdst;
4933 	struct vxlan_fdb *f;
4934 
4935 	spin_lock_bh(&vxlan->hash_lock);
4936 
4937 	f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4938 	if (!f)
4939 		goto out;
4940 
4941 	rdst = vxlan_fdb_find_rdst(f, &fdb_info->remote_ip,
4942 				   fdb_info->remote_port,
4943 				   fdb_info->remote_vni,
4944 				   fdb_info->remote_ifindex);
4945 	if (!rdst)
4946 		goto out;
4947 
4948 	rdst->offloaded = fdb_info->offloaded;
4949 
4950 out:
4951 	spin_unlock_bh(&vxlan->hash_lock);
4952 }
4953 
4954 static int
4955 vxlan_fdb_external_learn_add(struct net_device *dev,
4956 			     struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4957 {
4958 	struct vxlan_dev *vxlan = netdev_priv(dev);
4959 	struct netlink_ext_ack *extack;
4960 	int err;
4961 
4962 	extack = switchdev_notifier_info_to_extack(&fdb_info->info);
4963 
4964 	spin_lock_bh(&vxlan->hash_lock);
4965 	err = vxlan_fdb_update(vxlan, fdb_info->eth_addr, &fdb_info->remote_ip,
4966 			       NUD_REACHABLE,
4967 			       NLM_F_CREATE | NLM_F_REPLACE,
4968 			       fdb_info->remote_port,
4969 			       fdb_info->vni,
4970 			       fdb_info->remote_vni,
4971 			       fdb_info->remote_ifindex,
4972 			       NTF_USE | NTF_SELF | NTF_EXT_LEARNED,
4973 			       0, false, extack);
4974 	spin_unlock_bh(&vxlan->hash_lock);
4975 
4976 	return err;
4977 }
4978 
4979 static int
4980 vxlan_fdb_external_learn_del(struct net_device *dev,
4981 			     struct switchdev_notifier_vxlan_fdb_info *fdb_info)
4982 {
4983 	struct vxlan_dev *vxlan = netdev_priv(dev);
4984 	struct vxlan_fdb *f;
4985 	int err = 0;
4986 
4987 	spin_lock_bh(&vxlan->hash_lock);
4988 
4989 	f = vxlan_find_mac(vxlan, fdb_info->eth_addr, fdb_info->vni);
4990 	if (!f)
4991 		err = -ENOENT;
4992 	else if (f->flags & NTF_EXT_LEARNED)
4993 		err = __vxlan_fdb_delete(vxlan, fdb_info->eth_addr,
4994 					 fdb_info->remote_ip,
4995 					 fdb_info->remote_port,
4996 					 fdb_info->vni,
4997 					 fdb_info->remote_vni,
4998 					 fdb_info->remote_ifindex,
4999 					 false);
5000 
5001 	spin_unlock_bh(&vxlan->hash_lock);
5002 
5003 	return err;
5004 }
5005 
5006 static int vxlan_switchdev_event(struct notifier_block *unused,
5007 				 unsigned long event, void *ptr)
5008 {
5009 	struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
5010 	struct switchdev_notifier_vxlan_fdb_info *fdb_info;
5011 	int err = 0;
5012 
5013 	switch (event) {
5014 	case SWITCHDEV_VXLAN_FDB_OFFLOADED:
5015 		vxlan_fdb_offloaded_set(dev, ptr);
5016 		break;
5017 	case SWITCHDEV_VXLAN_FDB_ADD_TO_BRIDGE:
5018 		fdb_info = ptr;
5019 		err = vxlan_fdb_external_learn_add(dev, fdb_info);
5020 		if (err) {
5021 			err = notifier_from_errno(err);
5022 			break;
5023 		}
5024 		fdb_info->offloaded = true;
5025 		vxlan_fdb_offloaded_set(dev, fdb_info);
5026 		break;
5027 	case SWITCHDEV_VXLAN_FDB_DEL_TO_BRIDGE:
5028 		fdb_info = ptr;
5029 		err = vxlan_fdb_external_learn_del(dev, fdb_info);
5030 		if (err) {
5031 			err = notifier_from_errno(err);
5032 			break;
5033 		}
5034 		fdb_info->offloaded = false;
5035 		vxlan_fdb_offloaded_set(dev, fdb_info);
5036 		break;
5037 	}
5038 
5039 	return err;
5040 }
5041 
5042 static struct notifier_block vxlan_switchdev_notifier_block __read_mostly = {
5043 	.notifier_call = vxlan_switchdev_event,
5044 };
5045 
5046 static void vxlan_fdb_nh_flush(struct nexthop *nh)
5047 {
5048 	struct vxlan_fdb *fdb;
5049 	struct vxlan_dev *vxlan;
5050 
5051 	rcu_read_lock();
5052 	list_for_each_entry_rcu(fdb, &nh->fdb_list, nh_list) {
5053 		vxlan = rcu_dereference(fdb->vdev);
5054 		WARN_ON(!vxlan);
5055 		spin_lock_bh(&vxlan->hash_lock);
5056 		if (!hlist_unhashed(&fdb->fdb_node))
5057 			vxlan_fdb_destroy(vxlan, fdb, false, false);
5058 		spin_unlock_bh(&vxlan->hash_lock);
5059 	}
5060 	rcu_read_unlock();
5061 }
5062 
5063 static int vxlan_nexthop_event(struct notifier_block *nb,
5064 			       unsigned long event, void *ptr)
5065 {
5066 	struct nh_notifier_info *info = ptr;
5067 	struct nexthop *nh;
5068 
5069 	if (event != NEXTHOP_EVENT_DEL)
5070 		return NOTIFY_DONE;
5071 
5072 	nh = nexthop_find_by_id(info->net, info->id);
5073 	if (!nh)
5074 		return NOTIFY_DONE;
5075 
5076 	vxlan_fdb_nh_flush(nh);
5077 
5078 	return NOTIFY_DONE;
5079 }
5080 
5081 static __net_init int vxlan_init_net(struct net *net)
5082 {
5083 	struct vxlan_net *vn = net_generic(net, vxlan_net_id);
5084 	unsigned int h;
5085 
5086 	INIT_LIST_HEAD(&vn->vxlan_list);
5087 	vn->nexthop_notifier_block.notifier_call = vxlan_nexthop_event;
5088 
5089 	for (h = 0; h < PORT_HASH_SIZE; ++h)
5090 		INIT_HLIST_HEAD(&vn->sock_list[h]);
5091 
5092 	return register_nexthop_notifier(net, &vn->nexthop_notifier_block,
5093 					 NULL);
5094 }
5095 
5096 static void __net_exit vxlan_destroy_tunnels(struct vxlan_net *vn,
5097 					     struct list_head *dev_to_kill)
5098 {
5099 	struct vxlan_dev *vxlan, *next;
5100 
5101 	list_for_each_entry_safe(vxlan, next, &vn->vxlan_list, next)
5102 		vxlan_dellink(vxlan->dev, dev_to_kill);
5103 }
5104 
5105 static void __net_exit vxlan_exit_rtnl(struct net *net,
5106 				       struct list_head *dev_to_kill)
5107 {
5108 	struct vxlan_net *vn = net_generic(net, vxlan_net_id);
5109 
5110 	ASSERT_RTNL_NET(net);
5111 
5112 	__unregister_nexthop_notifier(net, &vn->nexthop_notifier_block);
5113 	vxlan_destroy_tunnels(vn, dev_to_kill);
5114 }
5115 
5116 static void __net_exit vxlan_exit_net(struct net *net)
5117 {
5118 	struct vxlan_net *vn = net_generic(net, vxlan_net_id);
5119 	unsigned int h;
5120 
5121 	for (h = 0; h < PORT_HASH_SIZE; ++h)
5122 		WARN_ON_ONCE(!hlist_empty(&vn->sock_list[h]));
5123 }
5124 
5125 static struct pernet_operations vxlan_net_ops = {
5126 	.init = vxlan_init_net,
5127 	.exit_rtnl = vxlan_exit_rtnl,
5128 	.exit = vxlan_exit_net,
5129 	.id   = &vxlan_net_id,
5130 	.size = sizeof(struct vxlan_net),
5131 };
5132 
5133 static int __init vxlan_init_module(void)
5134 {
5135 	int rc;
5136 
5137 	rc = register_pernet_subsys(&vxlan_net_ops);
5138 	if (rc)
5139 		goto out1;
5140 
5141 	rc = register_netdevice_notifier(&vxlan_notifier_block);
5142 	if (rc)
5143 		goto out2;
5144 
5145 	rc = register_switchdev_notifier(&vxlan_switchdev_notifier_block);
5146 	if (rc)
5147 		goto out3;
5148 
5149 	rc = rtnl_link_register(&vxlan_link_ops);
5150 	if (rc)
5151 		goto out4;
5152 
5153 	rc = vxlan_vnifilter_init();
5154 	if (rc)
5155 		goto out5;
5156 
5157 	return 0;
5158 out5:
5159 	rtnl_link_unregister(&vxlan_link_ops);
5160 out4:
5161 	unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
5162 out3:
5163 	unregister_netdevice_notifier(&vxlan_notifier_block);
5164 out2:
5165 	unregister_pernet_subsys(&vxlan_net_ops);
5166 out1:
5167 	return rc;
5168 }
5169 late_initcall(vxlan_init_module);
5170 
5171 static void __exit vxlan_cleanup_module(void)
5172 {
5173 	vxlan_vnifilter_uninit();
5174 	rtnl_link_unregister(&vxlan_link_ops);
5175 	unregister_switchdev_notifier(&vxlan_switchdev_notifier_block);
5176 	unregister_netdevice_notifier(&vxlan_notifier_block);
5177 	unregister_pernet_subsys(&vxlan_net_ops);
5178 	/* rcu_barrier() is called by netns */
5179 }
5180 module_exit(vxlan_cleanup_module);
5181 
5182 MODULE_LICENSE("GPL");
5183 MODULE_VERSION(VXLAN_VERSION);
5184 MODULE_AUTHOR("Stephen Hemminger <stephen@networkplumber.org>");
5185 MODULE_DESCRIPTION("Driver for VXLAN encapsulated traffic");
5186 MODULE_ALIAS_RTNL_LINK("vxlan");
5187