2 * vrf.c: device driver to encapsulate a VRF space
4 * Copyright (c) 2015 Cumulus Networks. All rights reserved.
5 * Copyright (c) 2015 Shrijeet Mukherjee <shm@cumulusnetworks.com>
6 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
8 * Based on dummy, team and ipvlan drivers
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
16 #include <linux/module.h>
17 #include <linux/kernel.h>
18 #include <linux/netdevice.h>
19 #include <linux/etherdevice.h>
21 #include <linux/init.h>
22 #include <linux/moduleparam.h>
23 #include <linux/netfilter.h>
24 #include <linux/rtnetlink.h>
25 #include <net/rtnetlink.h>
26 #include <linux/u64_stats_sync.h>
27 #include <linux/hashtable.h>
29 #include <linux/inetdevice.h>
32 #include <net/ip_fib.h>
33 #include <net/ip6_fib.h>
34 #include <net/ip6_route.h>
35 #include <net/rtnetlink.h>
36 #include <net/route.h>
37 #include <net/addrconf.h>
38 #include <net/l3mdev.h>
40 #define RT_FL_TOS(oldflp4) \
41 ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
43 #define DRV_NAME "vrf"
44 #define DRV_VERSION "1.0"
46 #define vrf_master_get_rcu(dev) \
47 ((struct net_device *)rcu_dereference(dev->rx_handler_data))
50 struct list_head list;
51 struct net_device *dev;
55 struct list_head all_slaves;
59 struct slave_queue queue;
71 struct u64_stats_sync syncp;
74 static struct dst_entry *vrf_ip_check(struct dst_entry *dst, u32 cookie)
79 static int vrf_ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
81 return ip_local_out(net, sk, skb);
84 static unsigned int vrf_v4_mtu(const struct dst_entry *dst)
86 /* TO-DO: return max ethernet size? */
90 static void vrf_dst_destroy(struct dst_entry *dst)
92 /* our dst lives forever - or until the device is closed */
95 static unsigned int vrf_default_advmss(const struct dst_entry *dst)
100 static struct dst_ops vrf_dst_ops = {
102 .local_out = vrf_ip_local_out,
103 .check = vrf_ip_check,
105 .destroy = vrf_dst_destroy,
106 .default_advmss = vrf_default_advmss,
109 /* neighbor handling is done with actual device; do not want
110 * to flip skb->dev for those ndisc packets. This really fails
111 * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
114 #if IS_ENABLED(CONFIG_IPV6)
115 static bool check_ipv6_frame(const struct sk_buff *skb)
117 const struct ipv6hdr *ipv6h;
118 struct ipv6hdr _ipv6h;
121 ipv6h = skb_header_pointer(skb, 0, sizeof(_ipv6h), &_ipv6h);
125 if (ipv6h->nexthdr == NEXTHDR_ICMP) {
126 const struct icmp6hdr *icmph;
127 struct icmp6hdr _icmph;
129 icmph = skb_header_pointer(skb, sizeof(_ipv6h),
130 sizeof(_icmph), &_icmph);
134 switch (icmph->icmp6_type) {
135 case NDISC_ROUTER_SOLICITATION:
136 case NDISC_ROUTER_ADVERTISEMENT:
137 case NDISC_NEIGHBOUR_SOLICITATION:
138 case NDISC_NEIGHBOUR_ADVERTISEMENT:
149 static bool check_ipv6_frame(const struct sk_buff *skb)
155 static bool is_ip_rx_frame(struct sk_buff *skb)
157 switch (skb->protocol) {
158 case htons(ETH_P_IP):
160 case htons(ETH_P_IPV6):
161 return check_ipv6_frame(skb);
166 static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
168 vrf_dev->stats.tx_errors++;
172 /* note: already called with rcu_read_lock */
173 static rx_handler_result_t vrf_handle_frame(struct sk_buff **pskb)
175 struct sk_buff *skb = *pskb;
177 if (is_ip_rx_frame(skb)) {
178 struct net_device *dev = vrf_master_get_rcu(skb->dev);
179 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
181 u64_stats_update_begin(&dstats->syncp);
183 dstats->rx_bytes += skb->len;
184 u64_stats_update_end(&dstats->syncp);
188 return RX_HANDLER_ANOTHER;
190 return RX_HANDLER_PASS;
193 static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
194 struct rtnl_link_stats64 *stats)
198 for_each_possible_cpu(i) {
199 const struct pcpu_dstats *dstats;
200 u64 tbytes, tpkts, tdrops, rbytes, rpkts;
203 dstats = per_cpu_ptr(dev->dstats, i);
205 start = u64_stats_fetch_begin_irq(&dstats->syncp);
206 tbytes = dstats->tx_bytes;
207 tpkts = dstats->tx_pkts;
208 tdrops = dstats->tx_drps;
209 rbytes = dstats->rx_bytes;
210 rpkts = dstats->rx_pkts;
211 } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
212 stats->tx_bytes += tbytes;
213 stats->tx_packets += tpkts;
214 stats->tx_dropped += tdrops;
215 stats->rx_bytes += rbytes;
216 stats->rx_packets += rpkts;
221 #if IS_ENABLED(CONFIG_IPV6)
222 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
223 struct net_device *dev)
225 const struct ipv6hdr *iph = ipv6_hdr(skb);
226 struct net *net = dev_net(skb->dev);
227 struct flowi6 fl6 = {
228 /* needed to match OIF rule */
229 .flowi6_oif = dev->ifindex,
230 .flowi6_iif = LOOPBACK_IFINDEX,
233 .flowlabel = ip6_flowinfo(iph),
234 .flowi6_mark = skb->mark,
235 .flowi6_proto = iph->nexthdr,
236 .flowi6_flags = FLOWI_FLAG_L3MDEV_SRC | FLOWI_FLAG_SKIP_NH_OIF,
238 int ret = NET_XMIT_DROP;
239 struct dst_entry *dst;
240 struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
242 dst = ip6_route_output(net, NULL, &fl6);
247 skb_dst_set(skb, dst);
249 ret = ip6_local_out(net, skb->sk, skb);
250 if (unlikely(net_xmit_eval(ret)))
251 dev->stats.tx_errors++;
253 ret = NET_XMIT_SUCCESS;
257 vrf_tx_error(dev, skb);
258 return NET_XMIT_DROP;
261 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
262 struct net_device *dev)
264 vrf_tx_error(dev, skb);
265 return NET_XMIT_DROP;
269 static int vrf_send_v4_prep(struct sk_buff *skb, struct flowi4 *fl4,
270 struct net_device *vrf_dev)
275 rt = ip_route_output_flow(dev_net(vrf_dev), fl4, NULL);
279 /* TO-DO: what about broadcast ? */
280 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
286 skb_dst_set(skb, &rt->dst);
292 static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
293 struct net_device *vrf_dev)
295 struct iphdr *ip4h = ip_hdr(skb);
296 int ret = NET_XMIT_DROP;
297 struct flowi4 fl4 = {
298 /* needed to match OIF rule */
299 .flowi4_oif = vrf_dev->ifindex,
300 .flowi4_iif = LOOPBACK_IFINDEX,
301 .flowi4_tos = RT_TOS(ip4h->tos),
302 .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_L3MDEV_SRC |
303 FLOWI_FLAG_SKIP_NH_OIF,
304 .daddr = ip4h->daddr,
307 if (vrf_send_v4_prep(skb, &fl4, vrf_dev))
311 ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
315 ret = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
316 if (unlikely(net_xmit_eval(ret)))
317 vrf_dev->stats.tx_errors++;
319 ret = NET_XMIT_SUCCESS;
324 vrf_tx_error(vrf_dev, skb);
328 static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
330 /* strip the ethernet header added for pass through VRF device */
331 __skb_pull(skb, skb_network_offset(skb));
333 switch (skb->protocol) {
334 case htons(ETH_P_IP):
335 return vrf_process_v4_outbound(skb, dev);
336 case htons(ETH_P_IPV6):
337 return vrf_process_v6_outbound(skb, dev);
339 vrf_tx_error(dev, skb);
340 return NET_XMIT_DROP;
344 static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
346 netdev_tx_t ret = is_ip_tx_frame(skb, dev);
348 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
349 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
351 u64_stats_update_begin(&dstats->syncp);
353 dstats->tx_bytes += skb->len;
354 u64_stats_update_end(&dstats->syncp);
356 this_cpu_inc(dev->dstats->tx_drps);
362 #if IS_ENABLED(CONFIG_IPV6)
363 static struct dst_entry *vrf_ip6_check(struct dst_entry *dst, u32 cookie)
368 static struct dst_ops vrf_dst_ops6 = {
370 .local_out = ip6_local_out,
371 .check = vrf_ip6_check,
373 .destroy = vrf_dst_destroy,
374 .default_advmss = vrf_default_advmss,
377 static int init_dst_ops6_kmem_cachep(void)
379 vrf_dst_ops6.kmem_cachep = kmem_cache_create("vrf_ip6_dst_cache",
380 sizeof(struct rt6_info),
385 if (!vrf_dst_ops6.kmem_cachep)
391 static void free_dst_ops6_kmem_cachep(void)
393 kmem_cache_destroy(vrf_dst_ops6.kmem_cachep);
396 static int vrf_input6(struct sk_buff *skb)
398 skb->dev->stats.rx_errors++;
403 /* modelled after ip6_finish_output2 */
404 static int vrf_finish_output6(struct net *net, struct sock *sk,
407 struct dst_entry *dst = skb_dst(skb);
408 struct net_device *dev = dst->dev;
409 struct neighbour *neigh;
410 struct in6_addr *nexthop;
413 skb->protocol = htons(ETH_P_IPV6);
417 nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
418 neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
419 if (unlikely(!neigh))
420 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
421 if (!IS_ERR(neigh)) {
422 ret = dst_neigh_output(dst, neigh, skb);
423 rcu_read_unlock_bh();
426 rcu_read_unlock_bh();
428 IP6_INC_STATS(dev_net(dst->dev),
429 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
434 /* modelled after ip6_output */
435 static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
437 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
438 net, sk, skb, NULL, skb_dst(skb)->dev,
440 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
443 static void vrf_rt6_destroy(struct net_vrf *vrf)
445 dst_destroy(&vrf->rt6->dst);
446 free_percpu(vrf->rt6->rt6i_pcpu);
450 static int vrf_rt6_create(struct net_device *dev)
452 struct net_vrf *vrf = netdev_priv(dev);
453 struct dst_entry *dst;
454 struct rt6_info *rt6;
458 rt6 = dst_alloc(&vrf_dst_ops6, dev, 0,
460 (DST_HOST | DST_NOPOLICY | DST_NOXFRM));
466 rt6->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_KERNEL);
467 if (!rt6->rt6i_pcpu) {
471 for_each_possible_cpu(cpu) {
472 struct rt6_info **p = per_cpu_ptr(rt6->rt6i_pcpu, cpu);
476 memset(dst + 1, 0, sizeof(*rt6) - sizeof(*dst));
478 INIT_LIST_HEAD(&rt6->rt6i_siblings);
479 INIT_LIST_HEAD(&rt6->rt6i_uncached);
481 rt6->dst.input = vrf_input6;
482 rt6->dst.output = vrf_output6;
484 rt6->rt6i_table = fib6_get_table(dev_net(dev), vrf->tb_id);
486 atomic_set(&rt6->dst.__refcnt, 2);
494 static int init_dst_ops6_kmem_cachep(void)
499 static void free_dst_ops6_kmem_cachep(void)
503 static void vrf_rt6_destroy(struct net_vrf *vrf)
507 static int vrf_rt6_create(struct net_device *dev)
513 /* modelled after ip_finish_output2 */
514 static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
516 struct dst_entry *dst = skb_dst(skb);
517 struct rtable *rt = (struct rtable *)dst;
518 struct net_device *dev = dst->dev;
519 unsigned int hh_len = LL_RESERVED_SPACE(dev);
520 struct neighbour *neigh;
524 /* Be paranoid, rather than too clever. */
525 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
526 struct sk_buff *skb2;
528 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
534 skb_set_owner_w(skb2, skb->sk);
542 nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
543 neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
544 if (unlikely(!neigh))
545 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
547 ret = dst_neigh_output(dst, neigh, skb);
549 rcu_read_unlock_bh();
551 if (unlikely(ret < 0))
552 vrf_tx_error(skb->dev, skb);
556 static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
558 struct net_device *dev = skb_dst(skb)->dev;
560 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
563 skb->protocol = htons(ETH_P_IP);
565 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
566 net, sk, skb, NULL, dev,
568 !(IPCB(skb)->flags & IPSKB_REROUTED));
571 static void vrf_rtable_destroy(struct net_vrf *vrf)
573 struct dst_entry *dst = (struct dst_entry *)vrf->rth;
579 static struct rtable *vrf_rtable_create(struct net_device *dev)
581 struct net_vrf *vrf = netdev_priv(dev);
584 rth = dst_alloc(&vrf_dst_ops, dev, 2,
586 (DST_HOST | DST_NOPOLICY | DST_NOXFRM));
588 rth->dst.output = vrf_output;
589 rth->rt_genid = rt_genid_ipv4(dev_net(dev));
591 rth->rt_type = RTN_UNICAST;
592 rth->rt_is_input = 0;
596 rth->rt_uses_gateway = 0;
597 rth->rt_table_id = vrf->tb_id;
598 INIT_LIST_HEAD(&rth->rt_uncached);
599 rth->rt_uncached_list = NULL;
605 /**************************** device handling ********************/
607 /* cycle interface to flush neighbor cache and move routes across tables */
608 static void cycle_netdev(struct net_device *dev)
610 unsigned int flags = dev->flags;
613 if (!netif_running(dev))
616 ret = dev_change_flags(dev, flags & ~IFF_UP);
618 ret = dev_change_flags(dev, flags);
622 "Failed to cycle device %s; route tables might be wrong!\n",
627 static struct slave *__vrf_find_slave_dev(struct slave_queue *queue,
628 struct net_device *dev)
630 struct list_head *head = &queue->all_slaves;
633 list_for_each_entry(slave, head, list) {
634 if (slave->dev == dev)
641 /* inverse of __vrf_insert_slave */
642 static void __vrf_remove_slave(struct slave_queue *queue, struct slave *slave)
644 list_del(&slave->list);
647 static void __vrf_insert_slave(struct slave_queue *queue, struct slave *slave)
649 list_add(&slave->list, &queue->all_slaves);
652 static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
654 struct slave *slave = kzalloc(sizeof(*slave), GFP_KERNEL);
655 struct net_vrf *vrf = netdev_priv(dev);
656 struct slave_queue *queue = &vrf->queue;
662 slave->dev = port_dev;
664 /* register the packet handler for slave ports */
665 ret = netdev_rx_handler_register(port_dev, vrf_handle_frame, dev);
668 "Device %s failed to register rx_handler\n",
673 ret = netdev_master_upper_dev_link(port_dev, dev);
677 port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
678 __vrf_insert_slave(queue, slave);
679 cycle_netdev(port_dev);
684 netdev_rx_handler_unregister(port_dev);
690 static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
692 if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev))
695 return do_vrf_add_slave(dev, port_dev);
698 /* inverse of do_vrf_add_slave */
699 static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
701 struct net_vrf *vrf = netdev_priv(dev);
702 struct slave_queue *queue = &vrf->queue;
705 netdev_upper_dev_unlink(port_dev, dev);
706 port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
708 netdev_rx_handler_unregister(port_dev);
710 cycle_netdev(port_dev);
712 slave = __vrf_find_slave_dev(queue, port_dev);
714 __vrf_remove_slave(queue, slave);
721 static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
723 return do_vrf_del_slave(dev, port_dev);
726 static void vrf_dev_uninit(struct net_device *dev)
728 struct net_vrf *vrf = netdev_priv(dev);
729 struct slave_queue *queue = &vrf->queue;
730 struct list_head *head = &queue->all_slaves;
731 struct slave *slave, *next;
733 vrf_rtable_destroy(vrf);
734 vrf_rt6_destroy(vrf);
736 list_for_each_entry_safe(slave, next, head, list)
737 vrf_del_slave(dev, slave->dev);
739 free_percpu(dev->dstats);
743 static int vrf_dev_init(struct net_device *dev)
745 struct net_vrf *vrf = netdev_priv(dev);
747 INIT_LIST_HEAD(&vrf->queue.all_slaves);
749 dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
753 /* create the default dst which points back to us */
754 vrf->rth = vrf_rtable_create(dev);
758 if (vrf_rt6_create(dev) != 0)
761 dev->flags = IFF_MASTER | IFF_NOARP;
766 vrf_rtable_destroy(vrf);
768 free_percpu(dev->dstats);
774 static const struct net_device_ops vrf_netdev_ops = {
775 .ndo_init = vrf_dev_init,
776 .ndo_uninit = vrf_dev_uninit,
777 .ndo_start_xmit = vrf_xmit,
778 .ndo_get_stats64 = vrf_get_stats64,
779 .ndo_add_slave = vrf_add_slave,
780 .ndo_del_slave = vrf_del_slave,
783 static u32 vrf_fib_table(const struct net_device *dev)
785 struct net_vrf *vrf = netdev_priv(dev);
790 static struct rtable *vrf_get_rtable(const struct net_device *dev,
791 const struct flowi4 *fl4)
793 struct rtable *rth = NULL;
795 if (!(fl4->flowi4_flags & FLOWI_FLAG_L3MDEV_SRC)) {
796 struct net_vrf *vrf = netdev_priv(dev);
799 atomic_inc(&rth->dst.__refcnt);
805 /* called under rcu_read_lock */
806 static int vrf_get_saddr(struct net_device *dev, struct flowi4 *fl4)
808 struct fib_result res = { .tclassid = 0 };
809 struct net *net = dev_net(dev);
810 u32 orig_tos = fl4->flowi4_tos;
811 u8 flags = fl4->flowi4_flags;
812 u8 scope = fl4->flowi4_scope;
813 u8 tos = RT_FL_TOS(fl4);
816 if (unlikely(!fl4->daddr))
819 fl4->flowi4_flags |= FLOWI_FLAG_SKIP_NH_OIF;
820 fl4->flowi4_iif = LOOPBACK_IFINDEX;
821 fl4->flowi4_tos = tos & IPTOS_RT_MASK;
822 fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
823 RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
825 rc = fib_lookup(net, fl4, &res, 0);
827 if (res.type == RTN_LOCAL)
828 fl4->saddr = res.fi->fib_prefsrc ? : fl4->daddr;
830 fib_select_path(net, &res, fl4, -1);
833 fl4->flowi4_flags = flags;
834 fl4->flowi4_tos = orig_tos;
835 fl4->flowi4_scope = scope;
840 #if IS_ENABLED(CONFIG_IPV6)
841 static struct dst_entry *vrf_get_rt6_dst(const struct net_device *dev,
842 const struct flowi6 *fl6)
844 struct rt6_info *rt = NULL;
846 if (!(fl6->flowi6_flags & FLOWI_FLAG_L3MDEV_SRC)) {
847 struct net_vrf *vrf = netdev_priv(dev);
850 atomic_inc(&rt->dst.__refcnt);
853 return (struct dst_entry *)rt;
857 static const struct l3mdev_ops vrf_l3mdev_ops = {
858 .l3mdev_fib_table = vrf_fib_table,
859 .l3mdev_get_rtable = vrf_get_rtable,
860 .l3mdev_get_saddr = vrf_get_saddr,
861 #if IS_ENABLED(CONFIG_IPV6)
862 .l3mdev_get_rt6_dst = vrf_get_rt6_dst,
866 static void vrf_get_drvinfo(struct net_device *dev,
867 struct ethtool_drvinfo *info)
869 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
870 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
873 static const struct ethtool_ops vrf_ethtool_ops = {
874 .get_drvinfo = vrf_get_drvinfo,
877 static void vrf_setup(struct net_device *dev)
881 /* Initialize the device structure. */
882 dev->netdev_ops = &vrf_netdev_ops;
883 dev->l3mdev_ops = &vrf_l3mdev_ops;
884 dev->ethtool_ops = &vrf_ethtool_ops;
885 dev->destructor = free_netdev;
887 /* Fill in device structure with ethernet-generic values. */
888 eth_hw_addr_random(dev);
890 /* don't acquire vrf device's netif_tx_lock when transmitting */
891 dev->features |= NETIF_F_LLTX;
893 /* don't allow vrf devices to change network namespaces. */
894 dev->features |= NETIF_F_NETNS_LOCAL;
897 static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
899 if (tb[IFLA_ADDRESS]) {
900 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
902 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
903 return -EADDRNOTAVAIL;
908 static void vrf_dellink(struct net_device *dev, struct list_head *head)
910 unregister_netdevice_queue(dev, head);
913 static int vrf_newlink(struct net *src_net, struct net_device *dev,
914 struct nlattr *tb[], struct nlattr *data[])
916 struct net_vrf *vrf = netdev_priv(dev);
918 if (!data || !data[IFLA_VRF_TABLE])
921 vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
923 dev->priv_flags |= IFF_L3MDEV_MASTER;
925 return register_netdevice(dev);
928 static size_t vrf_nl_getsize(const struct net_device *dev)
930 return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
933 static int vrf_fillinfo(struct sk_buff *skb,
934 const struct net_device *dev)
936 struct net_vrf *vrf = netdev_priv(dev);
938 return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
941 static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
942 [IFLA_VRF_TABLE] = { .type = NLA_U32 },
945 static struct rtnl_link_ops vrf_link_ops __read_mostly = {
947 .priv_size = sizeof(struct net_vrf),
949 .get_size = vrf_nl_getsize,
950 .policy = vrf_nl_policy,
951 .validate = vrf_validate,
952 .fill_info = vrf_fillinfo,
954 .newlink = vrf_newlink,
955 .dellink = vrf_dellink,
957 .maxtype = IFLA_VRF_MAX,
960 static int vrf_device_event(struct notifier_block *unused,
961 unsigned long event, void *ptr)
963 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
965 /* only care about unregister events to drop slave references */
966 if (event == NETDEV_UNREGISTER) {
967 struct net_device *vrf_dev;
969 if (!netif_is_l3_slave(dev))
972 vrf_dev = netdev_master_upper_dev_get(dev);
973 vrf_del_slave(vrf_dev, dev);
979 static struct notifier_block vrf_notifier_block __read_mostly = {
980 .notifier_call = vrf_device_event,
983 static int __init vrf_init_module(void)
987 vrf_dst_ops.kmem_cachep =
988 kmem_cache_create("vrf_ip_dst_cache",
989 sizeof(struct rtable), 0,
993 if (!vrf_dst_ops.kmem_cachep)
996 rc = init_dst_ops6_kmem_cachep();
1000 register_netdevice_notifier(&vrf_notifier_block);
1002 rc = rtnl_link_register(&vrf_link_ops);
1009 unregister_netdevice_notifier(&vrf_notifier_block);
1010 free_dst_ops6_kmem_cachep();
1012 kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
1016 static void __exit vrf_cleanup_module(void)
1018 rtnl_link_unregister(&vrf_link_ops);
1019 unregister_netdevice_notifier(&vrf_notifier_block);
1020 kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
1021 free_dst_ops6_kmem_cachep();
1024 module_init(vrf_init_module);
1025 module_exit(vrf_cleanup_module);
1026 MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
1027 MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
1028 MODULE_LICENSE("GPL");
1029 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
1030 MODULE_VERSION(DRV_VERSION);