xref: /openbmc/linux/drivers/net/vrf.c (revision 718a752b)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * vrf.c: device driver to encapsulate a VRF space
4  *
5  * Copyright (c) 2015 Cumulus Networks. All rights reserved.
6  * Copyright (c) 2015 Shrijeet Mukherjee <shm@cumulusnetworks.com>
7  * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
8  *
9  * Based on dummy, team and ipvlan drivers
10  */
11 
12 #include <linux/ethtool.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ip.h>
18 #include <linux/init.h>
19 #include <linux/moduleparam.h>
20 #include <linux/netfilter.h>
21 #include <linux/rtnetlink.h>
22 #include <net/rtnetlink.h>
23 #include <linux/u64_stats_sync.h>
24 #include <linux/hashtable.h>
25 #include <linux/spinlock_types.h>
26 
27 #include <linux/inetdevice.h>
28 #include <net/arp.h>
29 #include <net/ip.h>
30 #include <net/ip_fib.h>
31 #include <net/ip6_fib.h>
32 #include <net/ip6_route.h>
33 #include <net/route.h>
34 #include <net/addrconf.h>
35 #include <net/l3mdev.h>
36 #include <net/fib_rules.h>
37 #include <net/sch_generic.h>
38 #include <net/netns/generic.h>
39 #include <net/netfilter/nf_conntrack.h>
40 
41 #define DRV_NAME	"vrf"
42 #define DRV_VERSION	"1.1"
43 
44 #define FIB_RULE_PREF  1000       /* default preference for FIB rules */
45 
46 #define HT_MAP_BITS	4
47 #define HASH_INITVAL	((u32)0xcafef00d)
48 
49 struct  vrf_map {
50 	DECLARE_HASHTABLE(ht, HT_MAP_BITS);
51 	spinlock_t vmap_lock;
52 
53 	/* shared_tables:
54 	 * count how many distinct tables do not comply with the strict mode
55 	 * requirement.
56 	 * shared_tables value must be 0 in order to enable the strict mode.
57 	 *
58 	 * example of the evolution of shared_tables:
59 	 *                                                        | time
60 	 * add  vrf0 --> table 100        shared_tables = 0       | t0
61 	 * add  vrf1 --> table 101        shared_tables = 0       | t1
62 	 * add  vrf2 --> table 100        shared_tables = 1       | t2
63 	 * add  vrf3 --> table 100        shared_tables = 1       | t3
64 	 * add  vrf4 --> table 101        shared_tables = 2       v t4
65 	 *
66 	 * shared_tables is a "step function" (or "staircase function")
67 	 * and it is increased by one when the second vrf is associated to a
68 	 * table.
69 	 *
70 	 * at t2, vrf0 and vrf2 are bound to table 100: shared_tables = 1.
71 	 *
72 	 * at t3, another dev (vrf3) is bound to the same table 100 but the
73 	 * value of shared_tables is still 1.
74 	 * This means that no matter how many new vrfs will register on the
75 	 * table 100, the shared_tables will not increase (considering only
76 	 * table 100).
77 	 *
78 	 * at t4, vrf4 is bound to table 101, and shared_tables = 2.
79 	 *
80 	 * Looking at the value of shared_tables we can immediately know if
81 	 * the strict_mode can or cannot be enforced. Indeed, strict_mode
82 	 * can be enforced iff shared_tables = 0.
83 	 *
84 	 * Conversely, shared_tables is decreased when a vrf is de-associated
85 	 * from a table with exactly two associated vrfs.
86 	 */
87 	u32 shared_tables;
88 
89 	bool strict_mode;
90 };
91 
92 struct vrf_map_elem {
93 	struct hlist_node hnode;
94 	struct list_head vrf_list;  /* VRFs registered to this table */
95 
96 	u32 table_id;
97 	int users;
98 	int ifindex;
99 };
100 
101 static unsigned int vrf_net_id;
102 
103 /* per netns vrf data */
104 struct netns_vrf {
105 	/* protected by rtnl lock */
106 	bool add_fib_rules;
107 
108 	struct vrf_map vmap;
109 	struct ctl_table_header	*ctl_hdr;
110 };
111 
112 struct net_vrf {
113 	struct rtable __rcu	*rth;
114 	struct rt6_info	__rcu	*rt6;
115 #if IS_ENABLED(CONFIG_IPV6)
116 	struct fib6_table	*fib6_table;
117 #endif
118 	u32                     tb_id;
119 
120 	struct list_head	me_list;   /* entry in vrf_map_elem */
121 	int			ifindex;
122 };
123 
vrf_rx_stats(struct net_device * dev,int len)124 static void vrf_rx_stats(struct net_device *dev, int len)
125 {
126 	struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
127 
128 	u64_stats_update_begin(&dstats->syncp);
129 	dstats->rx_packets++;
130 	dstats->rx_bytes += len;
131 	u64_stats_update_end(&dstats->syncp);
132 }
133 
vrf_tx_error(struct net_device * vrf_dev,struct sk_buff * skb)134 static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
135 {
136 	vrf_dev->stats.tx_errors++;
137 	kfree_skb(skb);
138 }
139 
vrf_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)140 static void vrf_get_stats64(struct net_device *dev,
141 			    struct rtnl_link_stats64 *stats)
142 {
143 	int i;
144 
145 	for_each_possible_cpu(i) {
146 		const struct pcpu_dstats *dstats;
147 		u64 tbytes, tpkts, tdrops, rbytes, rpkts;
148 		unsigned int start;
149 
150 		dstats = per_cpu_ptr(dev->dstats, i);
151 		do {
152 			start = u64_stats_fetch_begin(&dstats->syncp);
153 			tbytes = dstats->tx_bytes;
154 			tpkts = dstats->tx_packets;
155 			tdrops = dstats->tx_drops;
156 			rbytes = dstats->rx_bytes;
157 			rpkts = dstats->rx_packets;
158 		} while (u64_stats_fetch_retry(&dstats->syncp, start));
159 		stats->tx_bytes += tbytes;
160 		stats->tx_packets += tpkts;
161 		stats->tx_dropped += tdrops;
162 		stats->rx_bytes += rbytes;
163 		stats->rx_packets += rpkts;
164 	}
165 }
166 
netns_vrf_map(struct net * net)167 static struct vrf_map *netns_vrf_map(struct net *net)
168 {
169 	struct netns_vrf *nn_vrf = net_generic(net, vrf_net_id);
170 
171 	return &nn_vrf->vmap;
172 }
173 
netns_vrf_map_by_dev(struct net_device * dev)174 static struct vrf_map *netns_vrf_map_by_dev(struct net_device *dev)
175 {
176 	return netns_vrf_map(dev_net(dev));
177 }
178 
vrf_map_elem_get_vrf_ifindex(struct vrf_map_elem * me)179 static int vrf_map_elem_get_vrf_ifindex(struct vrf_map_elem *me)
180 {
181 	struct list_head *me_head = &me->vrf_list;
182 	struct net_vrf *vrf;
183 
184 	if (list_empty(me_head))
185 		return -ENODEV;
186 
187 	vrf = list_first_entry(me_head, struct net_vrf, me_list);
188 
189 	return vrf->ifindex;
190 }
191 
vrf_map_elem_alloc(gfp_t flags)192 static struct vrf_map_elem *vrf_map_elem_alloc(gfp_t flags)
193 {
194 	struct vrf_map_elem *me;
195 
196 	me = kmalloc(sizeof(*me), flags);
197 	if (!me)
198 		return NULL;
199 
200 	return me;
201 }
202 
vrf_map_elem_free(struct vrf_map_elem * me)203 static void vrf_map_elem_free(struct vrf_map_elem *me)
204 {
205 	kfree(me);
206 }
207 
vrf_map_elem_init(struct vrf_map_elem * me,int table_id,int ifindex,int users)208 static void vrf_map_elem_init(struct vrf_map_elem *me, int table_id,
209 			      int ifindex, int users)
210 {
211 	me->table_id = table_id;
212 	me->ifindex = ifindex;
213 	me->users = users;
214 	INIT_LIST_HEAD(&me->vrf_list);
215 }
216 
vrf_map_lookup_elem(struct vrf_map * vmap,u32 table_id)217 static struct vrf_map_elem *vrf_map_lookup_elem(struct vrf_map *vmap,
218 						u32 table_id)
219 {
220 	struct vrf_map_elem *me;
221 	u32 key;
222 
223 	key = jhash_1word(table_id, HASH_INITVAL);
224 	hash_for_each_possible(vmap->ht, me, hnode, key) {
225 		if (me->table_id == table_id)
226 			return me;
227 	}
228 
229 	return NULL;
230 }
231 
vrf_map_add_elem(struct vrf_map * vmap,struct vrf_map_elem * me)232 static void vrf_map_add_elem(struct vrf_map *vmap, struct vrf_map_elem *me)
233 {
234 	u32 table_id = me->table_id;
235 	u32 key;
236 
237 	key = jhash_1word(table_id, HASH_INITVAL);
238 	hash_add(vmap->ht, &me->hnode, key);
239 }
240 
vrf_map_del_elem(struct vrf_map_elem * me)241 static void vrf_map_del_elem(struct vrf_map_elem *me)
242 {
243 	hash_del(&me->hnode);
244 }
245 
vrf_map_lock(struct vrf_map * vmap)246 static void vrf_map_lock(struct vrf_map *vmap) __acquires(&vmap->vmap_lock)
247 {
248 	spin_lock(&vmap->vmap_lock);
249 }
250 
vrf_map_unlock(struct vrf_map * vmap)251 static void vrf_map_unlock(struct vrf_map *vmap) __releases(&vmap->vmap_lock)
252 {
253 	spin_unlock(&vmap->vmap_lock);
254 }
255 
256 /* called with rtnl lock held */
257 static int
vrf_map_register_dev(struct net_device * dev,struct netlink_ext_ack * extack)258 vrf_map_register_dev(struct net_device *dev, struct netlink_ext_ack *extack)
259 {
260 	struct vrf_map *vmap = netns_vrf_map_by_dev(dev);
261 	struct net_vrf *vrf = netdev_priv(dev);
262 	struct vrf_map_elem *new_me, *me;
263 	u32 table_id = vrf->tb_id;
264 	bool free_new_me = false;
265 	int users;
266 	int res;
267 
268 	/* we pre-allocate elements used in the spin-locked section (so that we
269 	 * keep the spinlock as short as possible).
270 	 */
271 	new_me = vrf_map_elem_alloc(GFP_KERNEL);
272 	if (!new_me)
273 		return -ENOMEM;
274 
275 	vrf_map_elem_init(new_me, table_id, dev->ifindex, 0);
276 
277 	vrf_map_lock(vmap);
278 
279 	me = vrf_map_lookup_elem(vmap, table_id);
280 	if (!me) {
281 		me = new_me;
282 		vrf_map_add_elem(vmap, me);
283 		goto link_vrf;
284 	}
285 
286 	/* we already have an entry in the vrf_map, so it means there is (at
287 	 * least) a vrf registered on the specific table.
288 	 */
289 	free_new_me = true;
290 	if (vmap->strict_mode) {
291 		/* vrfs cannot share the same table */
292 		NL_SET_ERR_MSG(extack, "Table is used by another VRF");
293 		res = -EBUSY;
294 		goto unlock;
295 	}
296 
297 link_vrf:
298 	users = ++me->users;
299 	if (users == 2)
300 		++vmap->shared_tables;
301 
302 	list_add(&vrf->me_list, &me->vrf_list);
303 
304 	res = 0;
305 
306 unlock:
307 	vrf_map_unlock(vmap);
308 
309 	/* clean-up, if needed */
310 	if (free_new_me)
311 		vrf_map_elem_free(new_me);
312 
313 	return res;
314 }
315 
316 /* called with rtnl lock held */
vrf_map_unregister_dev(struct net_device * dev)317 static void vrf_map_unregister_dev(struct net_device *dev)
318 {
319 	struct vrf_map *vmap = netns_vrf_map_by_dev(dev);
320 	struct net_vrf *vrf = netdev_priv(dev);
321 	u32 table_id = vrf->tb_id;
322 	struct vrf_map_elem *me;
323 	int users;
324 
325 	vrf_map_lock(vmap);
326 
327 	me = vrf_map_lookup_elem(vmap, table_id);
328 	if (!me)
329 		goto unlock;
330 
331 	list_del(&vrf->me_list);
332 
333 	users = --me->users;
334 	if (users == 1) {
335 		--vmap->shared_tables;
336 	} else if (users == 0) {
337 		vrf_map_del_elem(me);
338 
339 		/* no one will refer to this element anymore */
340 		vrf_map_elem_free(me);
341 	}
342 
343 unlock:
344 	vrf_map_unlock(vmap);
345 }
346 
347 /* return the vrf device index associated with the table_id */
vrf_ifindex_lookup_by_table_id(struct net * net,u32 table_id)348 static int vrf_ifindex_lookup_by_table_id(struct net *net, u32 table_id)
349 {
350 	struct vrf_map *vmap = netns_vrf_map(net);
351 	struct vrf_map_elem *me;
352 	int ifindex;
353 
354 	vrf_map_lock(vmap);
355 
356 	if (!vmap->strict_mode) {
357 		ifindex = -EPERM;
358 		goto unlock;
359 	}
360 
361 	me = vrf_map_lookup_elem(vmap, table_id);
362 	if (!me) {
363 		ifindex = -ENODEV;
364 		goto unlock;
365 	}
366 
367 	ifindex = vrf_map_elem_get_vrf_ifindex(me);
368 
369 unlock:
370 	vrf_map_unlock(vmap);
371 
372 	return ifindex;
373 }
374 
375 /* by default VRF devices do not have a qdisc and are expected
376  * to be created with only a single queue.
377  */
qdisc_tx_is_default(const struct net_device * dev)378 static bool qdisc_tx_is_default(const struct net_device *dev)
379 {
380 	struct netdev_queue *txq;
381 	struct Qdisc *qdisc;
382 
383 	if (dev->num_tx_queues > 1)
384 		return false;
385 
386 	txq = netdev_get_tx_queue(dev, 0);
387 	qdisc = rcu_access_pointer(txq->qdisc);
388 
389 	return !qdisc->enqueue;
390 }
391 
392 /* Local traffic destined to local address. Reinsert the packet to rx
393  * path, similar to loopback handling.
394  */
vrf_local_xmit(struct sk_buff * skb,struct net_device * dev,struct dst_entry * dst)395 static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev,
396 			  struct dst_entry *dst)
397 {
398 	int len = skb->len;
399 
400 	skb_orphan(skb);
401 
402 	skb_dst_set(skb, dst);
403 
404 	/* set pkt_type to avoid skb hitting packet taps twice -
405 	 * once on Tx and again in Rx processing
406 	 */
407 	skb->pkt_type = PACKET_LOOPBACK;
408 
409 	skb->protocol = eth_type_trans(skb, dev);
410 
411 	if (likely(__netif_rx(skb) == NET_RX_SUCCESS))
412 		vrf_rx_stats(dev, len);
413 	else
414 		this_cpu_inc(dev->dstats->rx_drops);
415 
416 	return NETDEV_TX_OK;
417 }
418 
vrf_nf_set_untracked(struct sk_buff * skb)419 static void vrf_nf_set_untracked(struct sk_buff *skb)
420 {
421 	if (skb_get_nfct(skb) == 0)
422 		nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
423 }
424 
vrf_nf_reset_ct(struct sk_buff * skb)425 static void vrf_nf_reset_ct(struct sk_buff *skb)
426 {
427 	if (skb_get_nfct(skb) == IP_CT_UNTRACKED)
428 		nf_reset_ct(skb);
429 }
430 
431 #if IS_ENABLED(CONFIG_IPV6)
vrf_ip6_local_out(struct net * net,struct sock * sk,struct sk_buff * skb)432 static int vrf_ip6_local_out(struct net *net, struct sock *sk,
433 			     struct sk_buff *skb)
434 {
435 	int err;
436 
437 	vrf_nf_reset_ct(skb);
438 
439 	err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net,
440 		      sk, skb, NULL, skb_dst(skb)->dev, dst_output);
441 
442 	if (likely(err == 1))
443 		err = dst_output(net, sk, skb);
444 
445 	return err;
446 }
447 
vrf_process_v6_outbound(struct sk_buff * skb,struct net_device * dev)448 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
449 					   struct net_device *dev)
450 {
451 	const struct ipv6hdr *iph;
452 	struct net *net = dev_net(skb->dev);
453 	struct flowi6 fl6;
454 	int ret = NET_XMIT_DROP;
455 	struct dst_entry *dst;
456 	struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
457 
458 	if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr)))
459 		goto err;
460 
461 	iph = ipv6_hdr(skb);
462 
463 	memset(&fl6, 0, sizeof(fl6));
464 	/* needed to match OIF rule */
465 	fl6.flowi6_l3mdev = dev->ifindex;
466 	fl6.flowi6_iif = LOOPBACK_IFINDEX;
467 	fl6.daddr = iph->daddr;
468 	fl6.saddr = iph->saddr;
469 	fl6.flowlabel = ip6_flowinfo(iph);
470 	fl6.flowi6_mark = skb->mark;
471 	fl6.flowi6_proto = iph->nexthdr;
472 
473 	dst = ip6_dst_lookup_flow(net, NULL, &fl6, NULL);
474 	if (IS_ERR(dst) || dst == dst_null)
475 		goto err;
476 
477 	skb_dst_drop(skb);
478 
479 	/* if dst.dev is the VRF device again this is locally originated traffic
480 	 * destined to a local address. Short circuit to Rx path.
481 	 */
482 	if (dst->dev == dev)
483 		return vrf_local_xmit(skb, dev, dst);
484 
485 	skb_dst_set(skb, dst);
486 
487 	/* strip the ethernet header added for pass through VRF device */
488 	__skb_pull(skb, skb_network_offset(skb));
489 
490 	memset(IP6CB(skb), 0, sizeof(*IP6CB(skb)));
491 	ret = vrf_ip6_local_out(net, skb->sk, skb);
492 	if (unlikely(net_xmit_eval(ret)))
493 		dev->stats.tx_errors++;
494 	else
495 		ret = NET_XMIT_SUCCESS;
496 
497 	return ret;
498 err:
499 	vrf_tx_error(dev, skb);
500 	return NET_XMIT_DROP;
501 }
502 #else
vrf_process_v6_outbound(struct sk_buff * skb,struct net_device * dev)503 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
504 					   struct net_device *dev)
505 {
506 	vrf_tx_error(dev, skb);
507 	return NET_XMIT_DROP;
508 }
509 #endif
510 
511 /* based on ip_local_out; can't use it b/c the dst is switched pointing to us */
vrf_ip_local_out(struct net * net,struct sock * sk,struct sk_buff * skb)512 static int vrf_ip_local_out(struct net *net, struct sock *sk,
513 			    struct sk_buff *skb)
514 {
515 	int err;
516 
517 	vrf_nf_reset_ct(skb);
518 
519 	err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
520 		      skb, NULL, skb_dst(skb)->dev, dst_output);
521 	if (likely(err == 1))
522 		err = dst_output(net, sk, skb);
523 
524 	return err;
525 }
526 
vrf_process_v4_outbound(struct sk_buff * skb,struct net_device * vrf_dev)527 static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
528 					   struct net_device *vrf_dev)
529 {
530 	struct iphdr *ip4h;
531 	int ret = NET_XMIT_DROP;
532 	struct flowi4 fl4;
533 	struct net *net = dev_net(vrf_dev);
534 	struct rtable *rt;
535 
536 	if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr)))
537 		goto err;
538 
539 	ip4h = ip_hdr(skb);
540 
541 	memset(&fl4, 0, sizeof(fl4));
542 	/* needed to match OIF rule */
543 	fl4.flowi4_l3mdev = vrf_dev->ifindex;
544 	fl4.flowi4_iif = LOOPBACK_IFINDEX;
545 	fl4.flowi4_tos = RT_TOS(ip4h->tos);
546 	fl4.flowi4_flags = FLOWI_FLAG_ANYSRC;
547 	fl4.flowi4_proto = ip4h->protocol;
548 	fl4.daddr = ip4h->daddr;
549 	fl4.saddr = ip4h->saddr;
550 
551 	rt = ip_route_output_flow(net, &fl4, NULL);
552 	if (IS_ERR(rt))
553 		goto err;
554 
555 	skb_dst_drop(skb);
556 
557 	/* if dst.dev is the VRF device again this is locally originated traffic
558 	 * destined to a local address. Short circuit to Rx path.
559 	 */
560 	if (rt->dst.dev == vrf_dev)
561 		return vrf_local_xmit(skb, vrf_dev, &rt->dst);
562 
563 	skb_dst_set(skb, &rt->dst);
564 
565 	/* strip the ethernet header added for pass through VRF device */
566 	__skb_pull(skb, skb_network_offset(skb));
567 
568 	if (!ip4h->saddr) {
569 		ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
570 					       RT_SCOPE_LINK);
571 	}
572 
573 	memset(IPCB(skb), 0, sizeof(*IPCB(skb)));
574 	ret = vrf_ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
575 	if (unlikely(net_xmit_eval(ret)))
576 		vrf_dev->stats.tx_errors++;
577 	else
578 		ret = NET_XMIT_SUCCESS;
579 
580 out:
581 	return ret;
582 err:
583 	vrf_tx_error(vrf_dev, skb);
584 	goto out;
585 }
586 
is_ip_tx_frame(struct sk_buff * skb,struct net_device * dev)587 static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
588 {
589 	switch (skb->protocol) {
590 	case htons(ETH_P_IP):
591 		return vrf_process_v4_outbound(skb, dev);
592 	case htons(ETH_P_IPV6):
593 		return vrf_process_v6_outbound(skb, dev);
594 	default:
595 		vrf_tx_error(dev, skb);
596 		return NET_XMIT_DROP;
597 	}
598 }
599 
vrf_xmit(struct sk_buff * skb,struct net_device * dev)600 static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
601 {
602 	int len = skb->len;
603 	netdev_tx_t ret = is_ip_tx_frame(skb, dev);
604 
605 	if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
606 		struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
607 
608 		u64_stats_update_begin(&dstats->syncp);
609 		dstats->tx_packets++;
610 		dstats->tx_bytes += len;
611 		u64_stats_update_end(&dstats->syncp);
612 	} else {
613 		this_cpu_inc(dev->dstats->tx_drops);
614 	}
615 
616 	return ret;
617 }
618 
vrf_finish_direct(struct sk_buff * skb)619 static void vrf_finish_direct(struct sk_buff *skb)
620 {
621 	struct net_device *vrf_dev = skb->dev;
622 
623 	if (!list_empty(&vrf_dev->ptype_all) &&
624 	    likely(skb_headroom(skb) >= ETH_HLEN)) {
625 		struct ethhdr *eth = skb_push(skb, ETH_HLEN);
626 
627 		ether_addr_copy(eth->h_source, vrf_dev->dev_addr);
628 		eth_zero_addr(eth->h_dest);
629 		eth->h_proto = skb->protocol;
630 
631 		rcu_read_lock_bh();
632 		dev_queue_xmit_nit(skb, vrf_dev);
633 		rcu_read_unlock_bh();
634 
635 		skb_pull(skb, ETH_HLEN);
636 	}
637 
638 	vrf_nf_reset_ct(skb);
639 }
640 
641 #if IS_ENABLED(CONFIG_IPV6)
642 /* modelled after ip6_finish_output2 */
vrf_finish_output6(struct net * net,struct sock * sk,struct sk_buff * skb)643 static int vrf_finish_output6(struct net *net, struct sock *sk,
644 			      struct sk_buff *skb)
645 {
646 	struct dst_entry *dst = skb_dst(skb);
647 	struct net_device *dev = dst->dev;
648 	const struct in6_addr *nexthop;
649 	struct neighbour *neigh;
650 	int ret;
651 
652 	vrf_nf_reset_ct(skb);
653 
654 	skb->protocol = htons(ETH_P_IPV6);
655 	skb->dev = dev;
656 
657 	rcu_read_lock();
658 	nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
659 	neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
660 	if (unlikely(!neigh))
661 		neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
662 	if (!IS_ERR(neigh)) {
663 		sock_confirm_neigh(skb, neigh);
664 		ret = neigh_output(neigh, skb, false);
665 		rcu_read_unlock();
666 		return ret;
667 	}
668 	rcu_read_unlock();
669 
670 	IP6_INC_STATS(dev_net(dst->dev),
671 		      ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
672 	kfree_skb(skb);
673 	return -EINVAL;
674 }
675 
676 /* modelled after ip6_output */
vrf_output6(struct net * net,struct sock * sk,struct sk_buff * skb)677 static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
678 {
679 	return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
680 			    net, sk, skb, NULL, skb_dst(skb)->dev,
681 			    vrf_finish_output6,
682 			    !(IP6CB(skb)->flags & IP6SKB_REROUTED));
683 }
684 
685 /* set dst on skb to send packet to us via dev_xmit path. Allows
686  * packet to go through device based features such as qdisc, netfilter
687  * hooks and packet sockets with skb->dev set to vrf device.
688  */
vrf_ip6_out_redirect(struct net_device * vrf_dev,struct sk_buff * skb)689 static struct sk_buff *vrf_ip6_out_redirect(struct net_device *vrf_dev,
690 					    struct sk_buff *skb)
691 {
692 	struct net_vrf *vrf = netdev_priv(vrf_dev);
693 	struct dst_entry *dst = NULL;
694 	struct rt6_info *rt6;
695 
696 	rcu_read_lock();
697 
698 	rt6 = rcu_dereference(vrf->rt6);
699 	if (likely(rt6)) {
700 		dst = &rt6->dst;
701 		dst_hold(dst);
702 	}
703 
704 	rcu_read_unlock();
705 
706 	if (unlikely(!dst)) {
707 		vrf_tx_error(vrf_dev, skb);
708 		return NULL;
709 	}
710 
711 	skb_dst_drop(skb);
712 	skb_dst_set(skb, dst);
713 
714 	return skb;
715 }
716 
vrf_output6_direct_finish(struct net * net,struct sock * sk,struct sk_buff * skb)717 static int vrf_output6_direct_finish(struct net *net, struct sock *sk,
718 				     struct sk_buff *skb)
719 {
720 	vrf_finish_direct(skb);
721 
722 	return vrf_ip6_local_out(net, sk, skb);
723 }
724 
vrf_output6_direct(struct net * net,struct sock * sk,struct sk_buff * skb)725 static int vrf_output6_direct(struct net *net, struct sock *sk,
726 			      struct sk_buff *skb)
727 {
728 	int err = 1;
729 
730 	skb->protocol = htons(ETH_P_IPV6);
731 
732 	if (!(IPCB(skb)->flags & IPSKB_REROUTED))
733 		err = nf_hook(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, sk, skb,
734 			      NULL, skb->dev, vrf_output6_direct_finish);
735 
736 	if (likely(err == 1))
737 		vrf_finish_direct(skb);
738 
739 	return err;
740 }
741 
vrf_ip6_out_direct_finish(struct net * net,struct sock * sk,struct sk_buff * skb)742 static int vrf_ip6_out_direct_finish(struct net *net, struct sock *sk,
743 				     struct sk_buff *skb)
744 {
745 	int err;
746 
747 	err = vrf_output6_direct(net, sk, skb);
748 	if (likely(err == 1))
749 		err = vrf_ip6_local_out(net, sk, skb);
750 
751 	return err;
752 }
753 
vrf_ip6_out_direct(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb)754 static struct sk_buff *vrf_ip6_out_direct(struct net_device *vrf_dev,
755 					  struct sock *sk,
756 					  struct sk_buff *skb)
757 {
758 	struct net *net = dev_net(vrf_dev);
759 	int err;
760 
761 	skb->dev = vrf_dev;
762 
763 	err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk,
764 		      skb, NULL, vrf_dev, vrf_ip6_out_direct_finish);
765 
766 	if (likely(err == 1))
767 		err = vrf_output6_direct(net, sk, skb);
768 
769 	if (likely(err == 1))
770 		return skb;
771 
772 	return NULL;
773 }
774 
vrf_ip6_out(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb)775 static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
776 				   struct sock *sk,
777 				   struct sk_buff *skb)
778 {
779 	/* don't divert link scope packets */
780 	if (rt6_need_strict(&ipv6_hdr(skb)->daddr))
781 		return skb;
782 
783 	vrf_nf_set_untracked(skb);
784 
785 	if (qdisc_tx_is_default(vrf_dev) ||
786 	    IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
787 		return vrf_ip6_out_direct(vrf_dev, sk, skb);
788 
789 	return vrf_ip6_out_redirect(vrf_dev, skb);
790 }
791 
792 /* holding rtnl */
vrf_rt6_release(struct net_device * dev,struct net_vrf * vrf)793 static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
794 {
795 	struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
796 	struct net *net = dev_net(dev);
797 	struct dst_entry *dst;
798 
799 	RCU_INIT_POINTER(vrf->rt6, NULL);
800 	synchronize_rcu();
801 
802 	/* move dev in dst's to loopback so this VRF device can be deleted
803 	 * - based on dst_ifdown
804 	 */
805 	if (rt6) {
806 		dst = &rt6->dst;
807 		netdev_ref_replace(dst->dev, net->loopback_dev,
808 				   &dst->dev_tracker, GFP_KERNEL);
809 		dst->dev = net->loopback_dev;
810 		dst_release(dst);
811 	}
812 }
813 
vrf_rt6_create(struct net_device * dev)814 static int vrf_rt6_create(struct net_device *dev)
815 {
816 	int flags = DST_NOPOLICY | DST_NOXFRM;
817 	struct net_vrf *vrf = netdev_priv(dev);
818 	struct net *net = dev_net(dev);
819 	struct rt6_info *rt6;
820 	int rc = -ENOMEM;
821 
822 	/* IPv6 can be CONFIG enabled and then disabled runtime */
823 	if (!ipv6_mod_enabled())
824 		return 0;
825 
826 	vrf->fib6_table = fib6_new_table(net, vrf->tb_id);
827 	if (!vrf->fib6_table)
828 		goto out;
829 
830 	/* create a dst for routing packets out a VRF device */
831 	rt6 = ip6_dst_alloc(net, dev, flags);
832 	if (!rt6)
833 		goto out;
834 
835 	rt6->dst.output	= vrf_output6;
836 
837 	rcu_assign_pointer(vrf->rt6, rt6);
838 
839 	rc = 0;
840 out:
841 	return rc;
842 }
843 #else
vrf_ip6_out(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb)844 static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
845 				   struct sock *sk,
846 				   struct sk_buff *skb)
847 {
848 	return skb;
849 }
850 
vrf_rt6_release(struct net_device * dev,struct net_vrf * vrf)851 static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
852 {
853 }
854 
vrf_rt6_create(struct net_device * dev)855 static int vrf_rt6_create(struct net_device *dev)
856 {
857 	return 0;
858 }
859 #endif
860 
861 /* modelled after ip_finish_output2 */
vrf_finish_output(struct net * net,struct sock * sk,struct sk_buff * skb)862 static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
863 {
864 	struct dst_entry *dst = skb_dst(skb);
865 	struct rtable *rt = (struct rtable *)dst;
866 	struct net_device *dev = dst->dev;
867 	unsigned int hh_len = LL_RESERVED_SPACE(dev);
868 	struct neighbour *neigh;
869 	bool is_v6gw = false;
870 
871 	vrf_nf_reset_ct(skb);
872 
873 	/* Be paranoid, rather than too clever. */
874 	if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
875 		skb = skb_expand_head(skb, hh_len);
876 		if (!skb) {
877 			dev->stats.tx_errors++;
878 			return -ENOMEM;
879 		}
880 	}
881 
882 	rcu_read_lock();
883 
884 	neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
885 	if (!IS_ERR(neigh)) {
886 		int ret;
887 
888 		sock_confirm_neigh(skb, neigh);
889 		/* if crossing protocols, can not use the cached header */
890 		ret = neigh_output(neigh, skb, is_v6gw);
891 		rcu_read_unlock();
892 		return ret;
893 	}
894 
895 	rcu_read_unlock();
896 	vrf_tx_error(skb->dev, skb);
897 	return -EINVAL;
898 }
899 
vrf_output(struct net * net,struct sock * sk,struct sk_buff * skb)900 static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
901 {
902 	struct net_device *dev = skb_dst(skb)->dev;
903 
904 	IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
905 
906 	skb->dev = dev;
907 	skb->protocol = htons(ETH_P_IP);
908 
909 	return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
910 			    net, sk, skb, NULL, dev,
911 			    vrf_finish_output,
912 			    !(IPCB(skb)->flags & IPSKB_REROUTED));
913 }
914 
915 /* set dst on skb to send packet to us via dev_xmit path. Allows
916  * packet to go through device based features such as qdisc, netfilter
917  * hooks and packet sockets with skb->dev set to vrf device.
918  */
vrf_ip_out_redirect(struct net_device * vrf_dev,struct sk_buff * skb)919 static struct sk_buff *vrf_ip_out_redirect(struct net_device *vrf_dev,
920 					   struct sk_buff *skb)
921 {
922 	struct net_vrf *vrf = netdev_priv(vrf_dev);
923 	struct dst_entry *dst = NULL;
924 	struct rtable *rth;
925 
926 	rcu_read_lock();
927 
928 	rth = rcu_dereference(vrf->rth);
929 	if (likely(rth)) {
930 		dst = &rth->dst;
931 		dst_hold(dst);
932 	}
933 
934 	rcu_read_unlock();
935 
936 	if (unlikely(!dst)) {
937 		vrf_tx_error(vrf_dev, skb);
938 		return NULL;
939 	}
940 
941 	skb_dst_drop(skb);
942 	skb_dst_set(skb, dst);
943 
944 	return skb;
945 }
946 
vrf_output_direct_finish(struct net * net,struct sock * sk,struct sk_buff * skb)947 static int vrf_output_direct_finish(struct net *net, struct sock *sk,
948 				    struct sk_buff *skb)
949 {
950 	vrf_finish_direct(skb);
951 
952 	return vrf_ip_local_out(net, sk, skb);
953 }
954 
vrf_output_direct(struct net * net,struct sock * sk,struct sk_buff * skb)955 static int vrf_output_direct(struct net *net, struct sock *sk,
956 			     struct sk_buff *skb)
957 {
958 	int err = 1;
959 
960 	skb->protocol = htons(ETH_P_IP);
961 
962 	if (!(IPCB(skb)->flags & IPSKB_REROUTED))
963 		err = nf_hook(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, sk, skb,
964 			      NULL, skb->dev, vrf_output_direct_finish);
965 
966 	if (likely(err == 1))
967 		vrf_finish_direct(skb);
968 
969 	return err;
970 }
971 
vrf_ip_out_direct_finish(struct net * net,struct sock * sk,struct sk_buff * skb)972 static int vrf_ip_out_direct_finish(struct net *net, struct sock *sk,
973 				    struct sk_buff *skb)
974 {
975 	int err;
976 
977 	err = vrf_output_direct(net, sk, skb);
978 	if (likely(err == 1))
979 		err = vrf_ip_local_out(net, sk, skb);
980 
981 	return err;
982 }
983 
vrf_ip_out_direct(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb)984 static struct sk_buff *vrf_ip_out_direct(struct net_device *vrf_dev,
985 					 struct sock *sk,
986 					 struct sk_buff *skb)
987 {
988 	struct net *net = dev_net(vrf_dev);
989 	int err;
990 
991 	skb->dev = vrf_dev;
992 
993 	err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
994 		      skb, NULL, vrf_dev, vrf_ip_out_direct_finish);
995 
996 	if (likely(err == 1))
997 		err = vrf_output_direct(net, sk, skb);
998 
999 	if (likely(err == 1))
1000 		return skb;
1001 
1002 	return NULL;
1003 }
1004 
vrf_ip_out(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb)1005 static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev,
1006 				  struct sock *sk,
1007 				  struct sk_buff *skb)
1008 {
1009 	/* don't divert multicast or local broadcast */
1010 	if (ipv4_is_multicast(ip_hdr(skb)->daddr) ||
1011 	    ipv4_is_lbcast(ip_hdr(skb)->daddr))
1012 		return skb;
1013 
1014 	vrf_nf_set_untracked(skb);
1015 
1016 	if (qdisc_tx_is_default(vrf_dev) ||
1017 	    IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
1018 		return vrf_ip_out_direct(vrf_dev, sk, skb);
1019 
1020 	return vrf_ip_out_redirect(vrf_dev, skb);
1021 }
1022 
1023 /* called with rcu lock held */
vrf_l3_out(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb,u16 proto)1024 static struct sk_buff *vrf_l3_out(struct net_device *vrf_dev,
1025 				  struct sock *sk,
1026 				  struct sk_buff *skb,
1027 				  u16 proto)
1028 {
1029 	switch (proto) {
1030 	case AF_INET:
1031 		return vrf_ip_out(vrf_dev, sk, skb);
1032 	case AF_INET6:
1033 		return vrf_ip6_out(vrf_dev, sk, skb);
1034 	}
1035 
1036 	return skb;
1037 }
1038 
1039 /* holding rtnl */
vrf_rtable_release(struct net_device * dev,struct net_vrf * vrf)1040 static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf)
1041 {
1042 	struct rtable *rth = rtnl_dereference(vrf->rth);
1043 	struct net *net = dev_net(dev);
1044 	struct dst_entry *dst;
1045 
1046 	RCU_INIT_POINTER(vrf->rth, NULL);
1047 	synchronize_rcu();
1048 
1049 	/* move dev in dst's to loopback so this VRF device can be deleted
1050 	 * - based on dst_ifdown
1051 	 */
1052 	if (rth) {
1053 		dst = &rth->dst;
1054 		netdev_ref_replace(dst->dev, net->loopback_dev,
1055 				   &dst->dev_tracker, GFP_KERNEL);
1056 		dst->dev = net->loopback_dev;
1057 		dst_release(dst);
1058 	}
1059 }
1060 
vrf_rtable_create(struct net_device * dev)1061 static int vrf_rtable_create(struct net_device *dev)
1062 {
1063 	struct net_vrf *vrf = netdev_priv(dev);
1064 	struct rtable *rth;
1065 
1066 	if (!fib_new_table(dev_net(dev), vrf->tb_id))
1067 		return -ENOMEM;
1068 
1069 	/* create a dst for routing packets out through a VRF device */
1070 	rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1);
1071 	if (!rth)
1072 		return -ENOMEM;
1073 
1074 	rth->dst.output	= vrf_output;
1075 
1076 	rcu_assign_pointer(vrf->rth, rth);
1077 
1078 	return 0;
1079 }
1080 
1081 /**************************** device handling ********************/
1082 
1083 /* cycle interface to flush neighbor cache and move routes across tables */
cycle_netdev(struct net_device * dev,struct netlink_ext_ack * extack)1084 static void cycle_netdev(struct net_device *dev,
1085 			 struct netlink_ext_ack *extack)
1086 {
1087 	unsigned int flags = dev->flags;
1088 	int ret;
1089 
1090 	if (!netif_running(dev))
1091 		return;
1092 
1093 	ret = dev_change_flags(dev, flags & ~IFF_UP, extack);
1094 	if (ret >= 0)
1095 		ret = dev_change_flags(dev, flags, extack);
1096 
1097 	if (ret < 0) {
1098 		netdev_err(dev,
1099 			   "Failed to cycle device %s; route tables might be wrong!\n",
1100 			   dev->name);
1101 	}
1102 }
1103 
do_vrf_add_slave(struct net_device * dev,struct net_device * port_dev,struct netlink_ext_ack * extack)1104 static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev,
1105 			    struct netlink_ext_ack *extack)
1106 {
1107 	int ret;
1108 
1109 	/* do not allow loopback device to be enslaved to a VRF.
1110 	 * The vrf device acts as the loopback for the vrf.
1111 	 */
1112 	if (port_dev == dev_net(dev)->loopback_dev) {
1113 		NL_SET_ERR_MSG(extack,
1114 			       "Can not enslave loopback device to a VRF");
1115 		return -EOPNOTSUPP;
1116 	}
1117 
1118 	port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
1119 	ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL, extack);
1120 	if (ret < 0)
1121 		goto err;
1122 
1123 	cycle_netdev(port_dev, extack);
1124 
1125 	return 0;
1126 
1127 err:
1128 	port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
1129 	return ret;
1130 }
1131 
vrf_add_slave(struct net_device * dev,struct net_device * port_dev,struct netlink_ext_ack * extack)1132 static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev,
1133 			 struct netlink_ext_ack *extack)
1134 {
1135 	if (netif_is_l3_master(port_dev)) {
1136 		NL_SET_ERR_MSG(extack,
1137 			       "Can not enslave an L3 master device to a VRF");
1138 		return -EINVAL;
1139 	}
1140 
1141 	if (netif_is_l3_slave(port_dev))
1142 		return -EINVAL;
1143 
1144 	return do_vrf_add_slave(dev, port_dev, extack);
1145 }
1146 
1147 /* inverse of do_vrf_add_slave */
do_vrf_del_slave(struct net_device * dev,struct net_device * port_dev)1148 static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
1149 {
1150 	netdev_upper_dev_unlink(port_dev, dev);
1151 	port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
1152 
1153 	cycle_netdev(port_dev, NULL);
1154 
1155 	return 0;
1156 }
1157 
vrf_del_slave(struct net_device * dev,struct net_device * port_dev)1158 static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
1159 {
1160 	return do_vrf_del_slave(dev, port_dev);
1161 }
1162 
vrf_dev_uninit(struct net_device * dev)1163 static void vrf_dev_uninit(struct net_device *dev)
1164 {
1165 	struct net_vrf *vrf = netdev_priv(dev);
1166 
1167 	vrf_rtable_release(dev, vrf);
1168 	vrf_rt6_release(dev, vrf);
1169 }
1170 
vrf_dev_init(struct net_device * dev)1171 static int vrf_dev_init(struct net_device *dev)
1172 {
1173 	struct net_vrf *vrf = netdev_priv(dev);
1174 
1175 	/* create the default dst which points back to us */
1176 	if (vrf_rtable_create(dev) != 0)
1177 		goto out_nomem;
1178 
1179 	if (vrf_rt6_create(dev) != 0)
1180 		goto out_rth;
1181 
1182 	dev->flags = IFF_MASTER | IFF_NOARP;
1183 
1184 	/* similarly, oper state is irrelevant; set to up to avoid confusion */
1185 	dev->operstate = IF_OPER_UP;
1186 	netdev_lockdep_set_classes(dev);
1187 	return 0;
1188 
1189 out_rth:
1190 	vrf_rtable_release(dev, vrf);
1191 out_nomem:
1192 	return -ENOMEM;
1193 }
1194 
1195 static const struct net_device_ops vrf_netdev_ops = {
1196 	.ndo_init		= vrf_dev_init,
1197 	.ndo_uninit		= vrf_dev_uninit,
1198 	.ndo_start_xmit		= vrf_xmit,
1199 	.ndo_set_mac_address	= eth_mac_addr,
1200 	.ndo_get_stats64	= vrf_get_stats64,
1201 	.ndo_add_slave		= vrf_add_slave,
1202 	.ndo_del_slave		= vrf_del_slave,
1203 };
1204 
vrf_fib_table(const struct net_device * dev)1205 static u32 vrf_fib_table(const struct net_device *dev)
1206 {
1207 	struct net_vrf *vrf = netdev_priv(dev);
1208 
1209 	return vrf->tb_id;
1210 }
1211 
vrf_rcv_finish(struct net * net,struct sock * sk,struct sk_buff * skb)1212 static int vrf_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
1213 {
1214 	kfree_skb(skb);
1215 	return 0;
1216 }
1217 
vrf_rcv_nfhook(u8 pf,unsigned int hook,struct sk_buff * skb,struct net_device * dev)1218 static struct sk_buff *vrf_rcv_nfhook(u8 pf, unsigned int hook,
1219 				      struct sk_buff *skb,
1220 				      struct net_device *dev)
1221 {
1222 	struct net *net = dev_net(dev);
1223 
1224 	if (nf_hook(pf, hook, net, NULL, skb, dev, NULL, vrf_rcv_finish) != 1)
1225 		skb = NULL;    /* kfree_skb(skb) handled by nf code */
1226 
1227 	return skb;
1228 }
1229 
vrf_prepare_mac_header(struct sk_buff * skb,struct net_device * vrf_dev,u16 proto)1230 static int vrf_prepare_mac_header(struct sk_buff *skb,
1231 				  struct net_device *vrf_dev, u16 proto)
1232 {
1233 	struct ethhdr *eth;
1234 	int err;
1235 
1236 	/* in general, we do not know if there is enough space in the head of
1237 	 * the packet for hosting the mac header.
1238 	 */
1239 	err = skb_cow_head(skb, LL_RESERVED_SPACE(vrf_dev));
1240 	if (unlikely(err))
1241 		/* no space in the skb head */
1242 		return -ENOBUFS;
1243 
1244 	__skb_push(skb, ETH_HLEN);
1245 	eth = (struct ethhdr *)skb->data;
1246 
1247 	skb_reset_mac_header(skb);
1248 	skb_reset_mac_len(skb);
1249 
1250 	/* we set the ethernet destination and the source addresses to the
1251 	 * address of the VRF device.
1252 	 */
1253 	ether_addr_copy(eth->h_dest, vrf_dev->dev_addr);
1254 	ether_addr_copy(eth->h_source, vrf_dev->dev_addr);
1255 	eth->h_proto = htons(proto);
1256 
1257 	/* the destination address of the Ethernet frame corresponds to the
1258 	 * address set on the VRF interface; therefore, the packet is intended
1259 	 * to be processed locally.
1260 	 */
1261 	skb->protocol = eth->h_proto;
1262 	skb->pkt_type = PACKET_HOST;
1263 
1264 	skb_postpush_rcsum(skb, skb->data, ETH_HLEN);
1265 
1266 	skb_pull_inline(skb, ETH_HLEN);
1267 
1268 	return 0;
1269 }
1270 
1271 /* prepare and add the mac header to the packet if it was not set previously.
1272  * In this way, packet sniffers such as tcpdump can parse the packet correctly.
1273  * If the mac header was already set, the original mac header is left
1274  * untouched and the function returns immediately.
1275  */
vrf_add_mac_header_if_unset(struct sk_buff * skb,struct net_device * vrf_dev,u16 proto,struct net_device * orig_dev)1276 static int vrf_add_mac_header_if_unset(struct sk_buff *skb,
1277 				       struct net_device *vrf_dev,
1278 				       u16 proto, struct net_device *orig_dev)
1279 {
1280 	if (skb_mac_header_was_set(skb) && dev_has_header(orig_dev))
1281 		return 0;
1282 
1283 	return vrf_prepare_mac_header(skb, vrf_dev, proto);
1284 }
1285 
1286 #if IS_ENABLED(CONFIG_IPV6)
1287 /* neighbor handling is done with actual device; do not want
1288  * to flip skb->dev for those ndisc packets. This really fails
1289  * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
1290  * a start.
1291  */
ipv6_ndisc_frame(const struct sk_buff * skb)1292 static bool ipv6_ndisc_frame(const struct sk_buff *skb)
1293 {
1294 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1295 	bool rc = false;
1296 
1297 	if (iph->nexthdr == NEXTHDR_ICMP) {
1298 		const struct icmp6hdr *icmph;
1299 		struct icmp6hdr _icmph;
1300 
1301 		icmph = skb_header_pointer(skb, sizeof(*iph),
1302 					   sizeof(_icmph), &_icmph);
1303 		if (!icmph)
1304 			goto out;
1305 
1306 		switch (icmph->icmp6_type) {
1307 		case NDISC_ROUTER_SOLICITATION:
1308 		case NDISC_ROUTER_ADVERTISEMENT:
1309 		case NDISC_NEIGHBOUR_SOLICITATION:
1310 		case NDISC_NEIGHBOUR_ADVERTISEMENT:
1311 		case NDISC_REDIRECT:
1312 			rc = true;
1313 			break;
1314 		}
1315 	}
1316 
1317 out:
1318 	return rc;
1319 }
1320 
vrf_ip6_route_lookup(struct net * net,const struct net_device * dev,struct flowi6 * fl6,int ifindex,const struct sk_buff * skb,int flags)1321 static struct rt6_info *vrf_ip6_route_lookup(struct net *net,
1322 					     const struct net_device *dev,
1323 					     struct flowi6 *fl6,
1324 					     int ifindex,
1325 					     const struct sk_buff *skb,
1326 					     int flags)
1327 {
1328 	struct net_vrf *vrf = netdev_priv(dev);
1329 
1330 	return ip6_pol_route(net, vrf->fib6_table, ifindex, fl6, skb, flags);
1331 }
1332 
vrf_ip6_input_dst(struct sk_buff * skb,struct net_device * vrf_dev,int ifindex)1333 static void vrf_ip6_input_dst(struct sk_buff *skb, struct net_device *vrf_dev,
1334 			      int ifindex)
1335 {
1336 	const struct ipv6hdr *iph = ipv6_hdr(skb);
1337 	struct flowi6 fl6 = {
1338 		.flowi6_iif     = ifindex,
1339 		.flowi6_mark    = skb->mark,
1340 		.flowi6_proto   = iph->nexthdr,
1341 		.daddr          = iph->daddr,
1342 		.saddr          = iph->saddr,
1343 		.flowlabel      = ip6_flowinfo(iph),
1344 	};
1345 	struct net *net = dev_net(vrf_dev);
1346 	struct rt6_info *rt6;
1347 
1348 	rt6 = vrf_ip6_route_lookup(net, vrf_dev, &fl6, ifindex, skb,
1349 				   RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_IFACE);
1350 	if (unlikely(!rt6))
1351 		return;
1352 
1353 	if (unlikely(&rt6->dst == &net->ipv6.ip6_null_entry->dst))
1354 		return;
1355 
1356 	skb_dst_set(skb, &rt6->dst);
1357 }
1358 
vrf_ip6_rcv(struct net_device * vrf_dev,struct sk_buff * skb)1359 static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
1360 				   struct sk_buff *skb)
1361 {
1362 	int orig_iif = skb->skb_iif;
1363 	bool need_strict = rt6_need_strict(&ipv6_hdr(skb)->daddr);
1364 	bool is_ndisc = ipv6_ndisc_frame(skb);
1365 
1366 	/* loopback, multicast & non-ND link-local traffic; do not push through
1367 	 * packet taps again. Reset pkt_type for upper layers to process skb.
1368 	 * For non-loopback strict packets, determine the dst using the original
1369 	 * ifindex.
1370 	 */
1371 	if (skb->pkt_type == PACKET_LOOPBACK || (need_strict && !is_ndisc)) {
1372 		skb->dev = vrf_dev;
1373 		skb->skb_iif = vrf_dev->ifindex;
1374 		IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
1375 
1376 		if (skb->pkt_type == PACKET_LOOPBACK)
1377 			skb->pkt_type = PACKET_HOST;
1378 		else
1379 			vrf_ip6_input_dst(skb, vrf_dev, orig_iif);
1380 
1381 		goto out;
1382 	}
1383 
1384 	/* if packet is NDISC then keep the ingress interface */
1385 	if (!is_ndisc) {
1386 		struct net_device *orig_dev = skb->dev;
1387 
1388 		vrf_rx_stats(vrf_dev, skb->len);
1389 		skb->dev = vrf_dev;
1390 		skb->skb_iif = vrf_dev->ifindex;
1391 
1392 		if (!list_empty(&vrf_dev->ptype_all)) {
1393 			int err;
1394 
1395 			err = vrf_add_mac_header_if_unset(skb, vrf_dev,
1396 							  ETH_P_IPV6,
1397 							  orig_dev);
1398 			if (likely(!err)) {
1399 				skb_push(skb, skb->mac_len);
1400 				dev_queue_xmit_nit(skb, vrf_dev);
1401 				skb_pull(skb, skb->mac_len);
1402 			}
1403 		}
1404 
1405 		IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
1406 	}
1407 
1408 	if (need_strict)
1409 		vrf_ip6_input_dst(skb, vrf_dev, orig_iif);
1410 
1411 	skb = vrf_rcv_nfhook(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, vrf_dev);
1412 out:
1413 	return skb;
1414 }
1415 
1416 #else
vrf_ip6_rcv(struct net_device * vrf_dev,struct sk_buff * skb)1417 static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
1418 				   struct sk_buff *skb)
1419 {
1420 	return skb;
1421 }
1422 #endif
1423 
vrf_ip_rcv(struct net_device * vrf_dev,struct sk_buff * skb)1424 static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev,
1425 				  struct sk_buff *skb)
1426 {
1427 	struct net_device *orig_dev = skb->dev;
1428 
1429 	skb->dev = vrf_dev;
1430 	skb->skb_iif = vrf_dev->ifindex;
1431 	IPCB(skb)->flags |= IPSKB_L3SLAVE;
1432 
1433 	if (ipv4_is_multicast(ip_hdr(skb)->daddr))
1434 		goto out;
1435 
1436 	/* loopback traffic; do not push through packet taps again.
1437 	 * Reset pkt_type for upper layers to process skb
1438 	 */
1439 	if (skb->pkt_type == PACKET_LOOPBACK) {
1440 		skb->pkt_type = PACKET_HOST;
1441 		goto out;
1442 	}
1443 
1444 	vrf_rx_stats(vrf_dev, skb->len);
1445 
1446 	if (!list_empty(&vrf_dev->ptype_all)) {
1447 		int err;
1448 
1449 		err = vrf_add_mac_header_if_unset(skb, vrf_dev, ETH_P_IP,
1450 						  orig_dev);
1451 		if (likely(!err)) {
1452 			skb_push(skb, skb->mac_len);
1453 			dev_queue_xmit_nit(skb, vrf_dev);
1454 			skb_pull(skb, skb->mac_len);
1455 		}
1456 	}
1457 
1458 	skb = vrf_rcv_nfhook(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, vrf_dev);
1459 out:
1460 	return skb;
1461 }
1462 
1463 /* called with rcu lock held */
vrf_l3_rcv(struct net_device * vrf_dev,struct sk_buff * skb,u16 proto)1464 static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev,
1465 				  struct sk_buff *skb,
1466 				  u16 proto)
1467 {
1468 	switch (proto) {
1469 	case AF_INET:
1470 		return vrf_ip_rcv(vrf_dev, skb);
1471 	case AF_INET6:
1472 		return vrf_ip6_rcv(vrf_dev, skb);
1473 	}
1474 
1475 	return skb;
1476 }
1477 
1478 #if IS_ENABLED(CONFIG_IPV6)
1479 /* send to link-local or multicast address via interface enslaved to
1480  * VRF device. Force lookup to VRF table without changing flow struct
1481  * Note: Caller to this function must hold rcu_read_lock() and no refcnt
1482  * is taken on the dst by this function.
1483  */
vrf_link_scope_lookup(const struct net_device * dev,struct flowi6 * fl6)1484 static struct dst_entry *vrf_link_scope_lookup(const struct net_device *dev,
1485 					      struct flowi6 *fl6)
1486 {
1487 	struct net *net = dev_net(dev);
1488 	int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_DST_NOREF;
1489 	struct dst_entry *dst = NULL;
1490 	struct rt6_info *rt;
1491 
1492 	/* VRF device does not have a link-local address and
1493 	 * sending packets to link-local or mcast addresses over
1494 	 * a VRF device does not make sense
1495 	 */
1496 	if (fl6->flowi6_oif == dev->ifindex) {
1497 		dst = &net->ipv6.ip6_null_entry->dst;
1498 		return dst;
1499 	}
1500 
1501 	if (!ipv6_addr_any(&fl6->saddr))
1502 		flags |= RT6_LOOKUP_F_HAS_SADDR;
1503 
1504 	rt = vrf_ip6_route_lookup(net, dev, fl6, fl6->flowi6_oif, NULL, flags);
1505 	if (rt)
1506 		dst = &rt->dst;
1507 
1508 	return dst;
1509 }
1510 #endif
1511 
1512 static const struct l3mdev_ops vrf_l3mdev_ops = {
1513 	.l3mdev_fib_table	= vrf_fib_table,
1514 	.l3mdev_l3_rcv		= vrf_l3_rcv,
1515 	.l3mdev_l3_out		= vrf_l3_out,
1516 #if IS_ENABLED(CONFIG_IPV6)
1517 	.l3mdev_link_scope_lookup = vrf_link_scope_lookup,
1518 #endif
1519 };
1520 
vrf_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)1521 static void vrf_get_drvinfo(struct net_device *dev,
1522 			    struct ethtool_drvinfo *info)
1523 {
1524 	strscpy(info->driver, DRV_NAME, sizeof(info->driver));
1525 	strscpy(info->version, DRV_VERSION, sizeof(info->version));
1526 }
1527 
1528 static const struct ethtool_ops vrf_ethtool_ops = {
1529 	.get_drvinfo	= vrf_get_drvinfo,
1530 };
1531 
vrf_fib_rule_nl_size(void)1532 static inline size_t vrf_fib_rule_nl_size(void)
1533 {
1534 	size_t sz;
1535 
1536 	sz  = NLMSG_ALIGN(sizeof(struct fib_rule_hdr));
1537 	sz += nla_total_size(sizeof(u8));	/* FRA_L3MDEV */
1538 	sz += nla_total_size(sizeof(u32));	/* FRA_PRIORITY */
1539 	sz += nla_total_size(sizeof(u8));       /* FRA_PROTOCOL */
1540 
1541 	return sz;
1542 }
1543 
vrf_fib_rule(const struct net_device * dev,__u8 family,bool add_it)1544 static int vrf_fib_rule(const struct net_device *dev, __u8 family, bool add_it)
1545 {
1546 	struct fib_rule_hdr *frh;
1547 	struct nlmsghdr *nlh;
1548 	struct sk_buff *skb;
1549 	int err;
1550 
1551 	if ((family == AF_INET6 || family == RTNL_FAMILY_IP6MR) &&
1552 	    !ipv6_mod_enabled())
1553 		return 0;
1554 
1555 	skb = nlmsg_new(vrf_fib_rule_nl_size(), GFP_KERNEL);
1556 	if (!skb)
1557 		return -ENOMEM;
1558 
1559 	nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*frh), 0);
1560 	if (!nlh)
1561 		goto nla_put_failure;
1562 
1563 	/* rule only needs to appear once */
1564 	nlh->nlmsg_flags |= NLM_F_EXCL;
1565 
1566 	frh = nlmsg_data(nlh);
1567 	memset(frh, 0, sizeof(*frh));
1568 	frh->family = family;
1569 	frh->action = FR_ACT_TO_TBL;
1570 
1571 	if (nla_put_u8(skb, FRA_PROTOCOL, RTPROT_KERNEL))
1572 		goto nla_put_failure;
1573 
1574 	if (nla_put_u8(skb, FRA_L3MDEV, 1))
1575 		goto nla_put_failure;
1576 
1577 	if (nla_put_u32(skb, FRA_PRIORITY, FIB_RULE_PREF))
1578 		goto nla_put_failure;
1579 
1580 	nlmsg_end(skb, nlh);
1581 
1582 	/* fib_nl_{new,del}rule handling looks for net from skb->sk */
1583 	skb->sk = dev_net(dev)->rtnl;
1584 	if (add_it) {
1585 		err = fib_nl_newrule(skb, nlh, NULL);
1586 		if (err == -EEXIST)
1587 			err = 0;
1588 	} else {
1589 		err = fib_nl_delrule(skb, nlh, NULL);
1590 		if (err == -ENOENT)
1591 			err = 0;
1592 	}
1593 	nlmsg_free(skb);
1594 
1595 	return err;
1596 
1597 nla_put_failure:
1598 	nlmsg_free(skb);
1599 
1600 	return -EMSGSIZE;
1601 }
1602 
vrf_add_fib_rules(const struct net_device * dev)1603 static int vrf_add_fib_rules(const struct net_device *dev)
1604 {
1605 	int err;
1606 
1607 	err = vrf_fib_rule(dev, AF_INET,  true);
1608 	if (err < 0)
1609 		goto out_err;
1610 
1611 	err = vrf_fib_rule(dev, AF_INET6, true);
1612 	if (err < 0)
1613 		goto ipv6_err;
1614 
1615 #if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES)
1616 	err = vrf_fib_rule(dev, RTNL_FAMILY_IPMR, true);
1617 	if (err < 0)
1618 		goto ipmr_err;
1619 #endif
1620 
1621 #if IS_ENABLED(CONFIG_IPV6_MROUTE_MULTIPLE_TABLES)
1622 	err = vrf_fib_rule(dev, RTNL_FAMILY_IP6MR, true);
1623 	if (err < 0)
1624 		goto ip6mr_err;
1625 #endif
1626 
1627 	return 0;
1628 
1629 #if IS_ENABLED(CONFIG_IPV6_MROUTE_MULTIPLE_TABLES)
1630 ip6mr_err:
1631 	vrf_fib_rule(dev, RTNL_FAMILY_IPMR,  false);
1632 #endif
1633 
1634 #if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES)
1635 ipmr_err:
1636 	vrf_fib_rule(dev, AF_INET6,  false);
1637 #endif
1638 
1639 ipv6_err:
1640 	vrf_fib_rule(dev, AF_INET,  false);
1641 
1642 out_err:
1643 	netdev_err(dev, "Failed to add FIB rules.\n");
1644 	return err;
1645 }
1646 
vrf_setup(struct net_device * dev)1647 static void vrf_setup(struct net_device *dev)
1648 {
1649 	ether_setup(dev);
1650 
1651 	/* Initialize the device structure. */
1652 	dev->netdev_ops = &vrf_netdev_ops;
1653 	dev->l3mdev_ops = &vrf_l3mdev_ops;
1654 	dev->ethtool_ops = &vrf_ethtool_ops;
1655 	dev->needs_free_netdev = true;
1656 
1657 	/* Fill in device structure with ethernet-generic values. */
1658 	eth_hw_addr_random(dev);
1659 
1660 	/* don't acquire vrf device's netif_tx_lock when transmitting */
1661 	dev->features |= NETIF_F_LLTX;
1662 
1663 	/* don't allow vrf devices to change network namespaces. */
1664 	dev->features |= NETIF_F_NETNS_LOCAL;
1665 
1666 	/* does not make sense for a VLAN to be added to a vrf device */
1667 	dev->features   |= NETIF_F_VLAN_CHALLENGED;
1668 
1669 	/* enable offload features */
1670 	dev->features   |= NETIF_F_GSO_SOFTWARE;
1671 	dev->features   |= NETIF_F_RXCSUM | NETIF_F_HW_CSUM | NETIF_F_SCTP_CRC;
1672 	dev->features   |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA;
1673 
1674 	dev->hw_features = dev->features;
1675 	dev->hw_enc_features = dev->features;
1676 
1677 	/* default to no qdisc; user can add if desired */
1678 	dev->priv_flags |= IFF_NO_QUEUE;
1679 	dev->priv_flags |= IFF_NO_RX_HANDLER;
1680 	dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1681 
1682 	/* VRF devices do not care about MTU, but if the MTU is set
1683 	 * too low then the ipv4 and ipv6 protocols are disabled
1684 	 * which breaks networking.
1685 	 */
1686 	dev->min_mtu = IPV6_MIN_MTU;
1687 	dev->max_mtu = IP6_MAX_MTU;
1688 	dev->mtu = dev->max_mtu;
1689 
1690 	dev->pcpu_stat_type = NETDEV_PCPU_STAT_DSTATS;
1691 }
1692 
vrf_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1693 static int vrf_validate(struct nlattr *tb[], struct nlattr *data[],
1694 			struct netlink_ext_ack *extack)
1695 {
1696 	if (tb[IFLA_ADDRESS]) {
1697 		if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1698 			NL_SET_ERR_MSG(extack, "Invalid hardware address");
1699 			return -EINVAL;
1700 		}
1701 		if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1702 			NL_SET_ERR_MSG(extack, "Invalid hardware address");
1703 			return -EADDRNOTAVAIL;
1704 		}
1705 	}
1706 	return 0;
1707 }
1708 
vrf_dellink(struct net_device * dev,struct list_head * head)1709 static void vrf_dellink(struct net_device *dev, struct list_head *head)
1710 {
1711 	struct net_device *port_dev;
1712 	struct list_head *iter;
1713 
1714 	netdev_for_each_lower_dev(dev, port_dev, iter)
1715 		vrf_del_slave(dev, port_dev);
1716 
1717 	vrf_map_unregister_dev(dev);
1718 
1719 	unregister_netdevice_queue(dev, head);
1720 }
1721 
vrf_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1722 static int vrf_newlink(struct net *src_net, struct net_device *dev,
1723 		       struct nlattr *tb[], struct nlattr *data[],
1724 		       struct netlink_ext_ack *extack)
1725 {
1726 	struct net_vrf *vrf = netdev_priv(dev);
1727 	struct netns_vrf *nn_vrf;
1728 	bool *add_fib_rules;
1729 	struct net *net;
1730 	int err;
1731 
1732 	if (!data || !data[IFLA_VRF_TABLE]) {
1733 		NL_SET_ERR_MSG(extack, "VRF table id is missing");
1734 		return -EINVAL;
1735 	}
1736 
1737 	vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
1738 	if (vrf->tb_id == RT_TABLE_UNSPEC) {
1739 		NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VRF_TABLE],
1740 				    "Invalid VRF table id");
1741 		return -EINVAL;
1742 	}
1743 
1744 	dev->priv_flags |= IFF_L3MDEV_MASTER;
1745 
1746 	err = register_netdevice(dev);
1747 	if (err)
1748 		goto out;
1749 
1750 	/* mapping between table_id and vrf;
1751 	 * note: such binding could not be done in the dev init function
1752 	 * because dev->ifindex id is not available yet.
1753 	 */
1754 	vrf->ifindex = dev->ifindex;
1755 
1756 	err = vrf_map_register_dev(dev, extack);
1757 	if (err) {
1758 		unregister_netdevice(dev);
1759 		goto out;
1760 	}
1761 
1762 	net = dev_net(dev);
1763 	nn_vrf = net_generic(net, vrf_net_id);
1764 
1765 	add_fib_rules = &nn_vrf->add_fib_rules;
1766 	if (*add_fib_rules) {
1767 		err = vrf_add_fib_rules(dev);
1768 		if (err) {
1769 			vrf_map_unregister_dev(dev);
1770 			unregister_netdevice(dev);
1771 			goto out;
1772 		}
1773 		*add_fib_rules = false;
1774 	}
1775 
1776 out:
1777 	return err;
1778 }
1779 
vrf_nl_getsize(const struct net_device * dev)1780 static size_t vrf_nl_getsize(const struct net_device *dev)
1781 {
1782 	return nla_total_size(sizeof(u32));  /* IFLA_VRF_TABLE */
1783 }
1784 
vrf_fillinfo(struct sk_buff * skb,const struct net_device * dev)1785 static int vrf_fillinfo(struct sk_buff *skb,
1786 			const struct net_device *dev)
1787 {
1788 	struct net_vrf *vrf = netdev_priv(dev);
1789 
1790 	return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
1791 }
1792 
vrf_get_slave_size(const struct net_device * bond_dev,const struct net_device * slave_dev)1793 static size_t vrf_get_slave_size(const struct net_device *bond_dev,
1794 				 const struct net_device *slave_dev)
1795 {
1796 	return nla_total_size(sizeof(u32));  /* IFLA_VRF_PORT_TABLE */
1797 }
1798 
vrf_fill_slave_info(struct sk_buff * skb,const struct net_device * vrf_dev,const struct net_device * slave_dev)1799 static int vrf_fill_slave_info(struct sk_buff *skb,
1800 			       const struct net_device *vrf_dev,
1801 			       const struct net_device *slave_dev)
1802 {
1803 	struct net_vrf *vrf = netdev_priv(vrf_dev);
1804 
1805 	if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id))
1806 		return -EMSGSIZE;
1807 
1808 	return 0;
1809 }
1810 
1811 static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
1812 	[IFLA_VRF_TABLE] = { .type = NLA_U32 },
1813 };
1814 
1815 static struct rtnl_link_ops vrf_link_ops __read_mostly = {
1816 	.kind		= DRV_NAME,
1817 	.priv_size	= sizeof(struct net_vrf),
1818 
1819 	.get_size	= vrf_nl_getsize,
1820 	.policy		= vrf_nl_policy,
1821 	.validate	= vrf_validate,
1822 	.fill_info	= vrf_fillinfo,
1823 
1824 	.get_slave_size  = vrf_get_slave_size,
1825 	.fill_slave_info = vrf_fill_slave_info,
1826 
1827 	.newlink	= vrf_newlink,
1828 	.dellink	= vrf_dellink,
1829 	.setup		= vrf_setup,
1830 	.maxtype	= IFLA_VRF_MAX,
1831 };
1832 
vrf_device_event(struct notifier_block * unused,unsigned long event,void * ptr)1833 static int vrf_device_event(struct notifier_block *unused,
1834 			    unsigned long event, void *ptr)
1835 {
1836 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1837 
1838 	/* only care about unregister events to drop slave references */
1839 	if (event == NETDEV_UNREGISTER) {
1840 		struct net_device *vrf_dev;
1841 
1842 		if (!netif_is_l3_slave(dev))
1843 			goto out;
1844 
1845 		vrf_dev = netdev_master_upper_dev_get(dev);
1846 		vrf_del_slave(vrf_dev, dev);
1847 	}
1848 out:
1849 	return NOTIFY_DONE;
1850 }
1851 
1852 static struct notifier_block vrf_notifier_block __read_mostly = {
1853 	.notifier_call = vrf_device_event,
1854 };
1855 
vrf_map_init(struct vrf_map * vmap)1856 static int vrf_map_init(struct vrf_map *vmap)
1857 {
1858 	spin_lock_init(&vmap->vmap_lock);
1859 	hash_init(vmap->ht);
1860 
1861 	vmap->strict_mode = false;
1862 
1863 	return 0;
1864 }
1865 
1866 #ifdef CONFIG_SYSCTL
vrf_strict_mode(struct vrf_map * vmap)1867 static bool vrf_strict_mode(struct vrf_map *vmap)
1868 {
1869 	bool strict_mode;
1870 
1871 	vrf_map_lock(vmap);
1872 	strict_mode = vmap->strict_mode;
1873 	vrf_map_unlock(vmap);
1874 
1875 	return strict_mode;
1876 }
1877 
vrf_strict_mode_change(struct vrf_map * vmap,bool new_mode)1878 static int vrf_strict_mode_change(struct vrf_map *vmap, bool new_mode)
1879 {
1880 	bool *cur_mode;
1881 	int res = 0;
1882 
1883 	vrf_map_lock(vmap);
1884 
1885 	cur_mode = &vmap->strict_mode;
1886 	if (*cur_mode == new_mode)
1887 		goto unlock;
1888 
1889 	if (*cur_mode) {
1890 		/* disable strict mode */
1891 		*cur_mode = false;
1892 	} else {
1893 		if (vmap->shared_tables) {
1894 			/* we cannot allow strict_mode because there are some
1895 			 * vrfs that share one or more tables.
1896 			 */
1897 			res = -EBUSY;
1898 			goto unlock;
1899 		}
1900 
1901 		/* no tables are shared among vrfs, so we can go back
1902 		 * to 1:1 association between a vrf with its table.
1903 		 */
1904 		*cur_mode = true;
1905 	}
1906 
1907 unlock:
1908 	vrf_map_unlock(vmap);
1909 
1910 	return res;
1911 }
1912 
vrf_shared_table_handler(struct ctl_table * table,int write,void * buffer,size_t * lenp,loff_t * ppos)1913 static int vrf_shared_table_handler(struct ctl_table *table, int write,
1914 				    void *buffer, size_t *lenp, loff_t *ppos)
1915 {
1916 	struct net *net = (struct net *)table->extra1;
1917 	struct vrf_map *vmap = netns_vrf_map(net);
1918 	int proc_strict_mode = 0;
1919 	struct ctl_table tmp = {
1920 		.procname	= table->procname,
1921 		.data		= &proc_strict_mode,
1922 		.maxlen		= sizeof(int),
1923 		.mode		= table->mode,
1924 		.extra1		= SYSCTL_ZERO,
1925 		.extra2		= SYSCTL_ONE,
1926 	};
1927 	int ret;
1928 
1929 	if (!write)
1930 		proc_strict_mode = vrf_strict_mode(vmap);
1931 
1932 	ret = proc_dointvec_minmax(&tmp, write, buffer, lenp, ppos);
1933 
1934 	if (write && ret == 0)
1935 		ret = vrf_strict_mode_change(vmap, (bool)proc_strict_mode);
1936 
1937 	return ret;
1938 }
1939 
1940 static const struct ctl_table vrf_table[] = {
1941 	{
1942 		.procname	= "strict_mode",
1943 		.data		= NULL,
1944 		.maxlen		= sizeof(int),
1945 		.mode		= 0644,
1946 		.proc_handler	= vrf_shared_table_handler,
1947 		/* set by the vrf_netns_init */
1948 		.extra1		= NULL,
1949 	},
1950 	{ },
1951 };
1952 
vrf_netns_init_sysctl(struct net * net,struct netns_vrf * nn_vrf)1953 static int vrf_netns_init_sysctl(struct net *net, struct netns_vrf *nn_vrf)
1954 {
1955 	struct ctl_table *table;
1956 
1957 	table = kmemdup(vrf_table, sizeof(vrf_table), GFP_KERNEL);
1958 	if (!table)
1959 		return -ENOMEM;
1960 
1961 	/* init the extra1 parameter with the reference to current netns */
1962 	table[0].extra1 = net;
1963 
1964 	nn_vrf->ctl_hdr = register_net_sysctl_sz(net, "net/vrf", table,
1965 						 ARRAY_SIZE(vrf_table));
1966 	if (!nn_vrf->ctl_hdr) {
1967 		kfree(table);
1968 		return -ENOMEM;
1969 	}
1970 
1971 	return 0;
1972 }
1973 
vrf_netns_exit_sysctl(struct net * net)1974 static void vrf_netns_exit_sysctl(struct net *net)
1975 {
1976 	struct netns_vrf *nn_vrf = net_generic(net, vrf_net_id);
1977 	struct ctl_table *table;
1978 
1979 	table = nn_vrf->ctl_hdr->ctl_table_arg;
1980 	unregister_net_sysctl_table(nn_vrf->ctl_hdr);
1981 	kfree(table);
1982 }
1983 #else
vrf_netns_init_sysctl(struct net * net,struct netns_vrf * nn_vrf)1984 static int vrf_netns_init_sysctl(struct net *net, struct netns_vrf *nn_vrf)
1985 {
1986 	return 0;
1987 }
1988 
vrf_netns_exit_sysctl(struct net * net)1989 static void vrf_netns_exit_sysctl(struct net *net)
1990 {
1991 }
1992 #endif
1993 
1994 /* Initialize per network namespace state */
vrf_netns_init(struct net * net)1995 static int __net_init vrf_netns_init(struct net *net)
1996 {
1997 	struct netns_vrf *nn_vrf = net_generic(net, vrf_net_id);
1998 
1999 	nn_vrf->add_fib_rules = true;
2000 	vrf_map_init(&nn_vrf->vmap);
2001 
2002 	return vrf_netns_init_sysctl(net, nn_vrf);
2003 }
2004 
vrf_netns_exit(struct net * net)2005 static void __net_exit vrf_netns_exit(struct net *net)
2006 {
2007 	vrf_netns_exit_sysctl(net);
2008 }
2009 
2010 static struct pernet_operations vrf_net_ops __net_initdata = {
2011 	.init = vrf_netns_init,
2012 	.exit = vrf_netns_exit,
2013 	.id   = &vrf_net_id,
2014 	.size = sizeof(struct netns_vrf),
2015 };
2016 
vrf_init_module(void)2017 static int __init vrf_init_module(void)
2018 {
2019 	int rc;
2020 
2021 	register_netdevice_notifier(&vrf_notifier_block);
2022 
2023 	rc = register_pernet_subsys(&vrf_net_ops);
2024 	if (rc < 0)
2025 		goto error;
2026 
2027 	rc = l3mdev_table_lookup_register(L3MDEV_TYPE_VRF,
2028 					  vrf_ifindex_lookup_by_table_id);
2029 	if (rc < 0)
2030 		goto unreg_pernet;
2031 
2032 	rc = rtnl_link_register(&vrf_link_ops);
2033 	if (rc < 0)
2034 		goto table_lookup_unreg;
2035 
2036 	return 0;
2037 
2038 table_lookup_unreg:
2039 	l3mdev_table_lookup_unregister(L3MDEV_TYPE_VRF,
2040 				       vrf_ifindex_lookup_by_table_id);
2041 
2042 unreg_pernet:
2043 	unregister_pernet_subsys(&vrf_net_ops);
2044 
2045 error:
2046 	unregister_netdevice_notifier(&vrf_notifier_block);
2047 	return rc;
2048 }
2049 
2050 module_init(vrf_init_module);
2051 MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
2052 MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
2053 MODULE_LICENSE("GPL");
2054 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
2055 MODULE_VERSION(DRV_VERSION);
2056