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