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