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