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