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