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