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/route.h>
36 #include <net/addrconf.h>
37 #include <net/l3mdev.h>
39 #define RT_FL_TOS(oldflp4) \
40 ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
42 #define DRV_NAME "vrf"
43 #define DRV_VERSION "1.0"
46 struct rtable __rcu *rth;
47 struct rt6_info __rcu *rt6;
57 struct u64_stats_sync syncp;
60 static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
62 vrf_dev->stats.tx_errors++;
66 static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
67 struct rtnl_link_stats64 *stats)
71 for_each_possible_cpu(i) {
72 const struct pcpu_dstats *dstats;
73 u64 tbytes, tpkts, tdrops, rbytes, rpkts;
76 dstats = per_cpu_ptr(dev->dstats, i);
78 start = u64_stats_fetch_begin_irq(&dstats->syncp);
79 tbytes = dstats->tx_bytes;
80 tpkts = dstats->tx_pkts;
81 tdrops = dstats->tx_drps;
82 rbytes = dstats->rx_bytes;
83 rpkts = dstats->rx_pkts;
84 } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
85 stats->tx_bytes += tbytes;
86 stats->tx_packets += tpkts;
87 stats->tx_dropped += tdrops;
88 stats->rx_bytes += rbytes;
89 stats->rx_packets += rpkts;
94 #if IS_ENABLED(CONFIG_IPV6)
95 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
96 struct net_device *dev)
98 const struct ipv6hdr *iph = ipv6_hdr(skb);
99 struct net *net = dev_net(skb->dev);
100 struct flowi6 fl6 = {
101 /* needed to match OIF rule */
102 .flowi6_oif = dev->ifindex,
103 .flowi6_iif = LOOPBACK_IFINDEX,
106 .flowlabel = ip6_flowinfo(iph),
107 .flowi6_mark = skb->mark,
108 .flowi6_proto = iph->nexthdr,
109 .flowi6_flags = FLOWI_FLAG_L3MDEV_SRC | FLOWI_FLAG_SKIP_NH_OIF,
111 int ret = NET_XMIT_DROP;
112 struct dst_entry *dst;
113 struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
115 dst = ip6_route_output(net, NULL, &fl6);
120 skb_dst_set(skb, dst);
122 /* strip the ethernet header added for pass through VRF device */
123 __skb_pull(skb, skb_network_offset(skb));
125 ret = ip6_local_out(net, skb->sk, skb);
126 if (unlikely(net_xmit_eval(ret)))
127 dev->stats.tx_errors++;
129 ret = NET_XMIT_SUCCESS;
133 vrf_tx_error(dev, skb);
134 return NET_XMIT_DROP;
137 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
138 struct net_device *dev)
140 vrf_tx_error(dev, skb);
141 return NET_XMIT_DROP;
145 static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
146 struct net_device *vrf_dev)
148 struct iphdr *ip4h = ip_hdr(skb);
149 int ret = NET_XMIT_DROP;
150 struct flowi4 fl4 = {
151 /* needed to match OIF rule */
152 .flowi4_oif = vrf_dev->ifindex,
153 .flowi4_iif = LOOPBACK_IFINDEX,
154 .flowi4_tos = RT_TOS(ip4h->tos),
155 .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_L3MDEV_SRC |
156 FLOWI_FLAG_SKIP_NH_OIF,
157 .daddr = ip4h->daddr,
159 struct net *net = dev_net(vrf_dev);
162 rt = ip_route_output_flow(net, &fl4, NULL);
166 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
172 skb_dst_set(skb, &rt->dst);
174 /* strip the ethernet header added for pass through VRF device */
175 __skb_pull(skb, skb_network_offset(skb));
178 ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
182 ret = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
183 if (unlikely(net_xmit_eval(ret)))
184 vrf_dev->stats.tx_errors++;
186 ret = NET_XMIT_SUCCESS;
191 vrf_tx_error(vrf_dev, skb);
195 static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
197 switch (skb->protocol) {
198 case htons(ETH_P_IP):
199 return vrf_process_v4_outbound(skb, dev);
200 case htons(ETH_P_IPV6):
201 return vrf_process_v6_outbound(skb, dev);
203 vrf_tx_error(dev, skb);
204 return NET_XMIT_DROP;
208 static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
210 netdev_tx_t ret = is_ip_tx_frame(skb, dev);
212 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
213 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
215 u64_stats_update_begin(&dstats->syncp);
217 dstats->tx_bytes += skb->len;
218 u64_stats_update_end(&dstats->syncp);
220 this_cpu_inc(dev->dstats->tx_drps);
226 #if IS_ENABLED(CONFIG_IPV6)
227 /* modelled after ip6_finish_output2 */
228 static int vrf_finish_output6(struct net *net, struct sock *sk,
231 struct dst_entry *dst = skb_dst(skb);
232 struct net_device *dev = dst->dev;
233 struct neighbour *neigh;
234 struct in6_addr *nexthop;
237 skb->protocol = htons(ETH_P_IPV6);
241 nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
242 neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
243 if (unlikely(!neigh))
244 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
245 if (!IS_ERR(neigh)) {
246 ret = dst_neigh_output(dst, neigh, skb);
247 rcu_read_unlock_bh();
250 rcu_read_unlock_bh();
252 IP6_INC_STATS(dev_net(dst->dev),
253 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
258 /* modelled after ip6_output */
259 static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
261 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
262 net, sk, skb, NULL, skb_dst(skb)->dev,
264 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
268 static void vrf_rt6_release(struct net_vrf *vrf)
270 struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
272 rcu_assign_pointer(vrf->rt6, NULL);
275 dst_release(&rt6->dst);
278 static int vrf_rt6_create(struct net_device *dev)
280 struct net_vrf *vrf = netdev_priv(dev);
281 struct net *net = dev_net(dev);
282 struct fib6_table *rt6i_table;
283 struct rt6_info *rt6;
286 rt6i_table = fib6_new_table(net, vrf->tb_id);
290 rt6 = ip6_dst_alloc(net, dev,
291 DST_HOST | DST_NOPOLICY | DST_NOXFRM | DST_NOCACHE);
297 rt6->rt6i_table = rt6i_table;
298 rt6->dst.output = vrf_output6;
299 rcu_assign_pointer(vrf->rt6, rt6);
306 static void vrf_rt6_release(struct net_vrf *vrf)
310 static int vrf_rt6_create(struct net_device *dev)
316 /* modelled after ip_finish_output2 */
317 static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
319 struct dst_entry *dst = skb_dst(skb);
320 struct rtable *rt = (struct rtable *)dst;
321 struct net_device *dev = dst->dev;
322 unsigned int hh_len = LL_RESERVED_SPACE(dev);
323 struct neighbour *neigh;
327 /* Be paranoid, rather than too clever. */
328 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
329 struct sk_buff *skb2;
331 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
337 skb_set_owner_w(skb2, skb->sk);
345 nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
346 neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
347 if (unlikely(!neigh))
348 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
350 ret = dst_neigh_output(dst, neigh, skb);
352 rcu_read_unlock_bh();
354 if (unlikely(ret < 0))
355 vrf_tx_error(skb->dev, skb);
359 static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
361 struct net_device *dev = skb_dst(skb)->dev;
363 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
366 skb->protocol = htons(ETH_P_IP);
368 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
369 net, sk, skb, NULL, dev,
371 !(IPCB(skb)->flags & IPSKB_REROUTED));
375 static void vrf_rtable_release(struct net_vrf *vrf)
377 struct rtable *rth = rtnl_dereference(vrf->rth);
379 rcu_assign_pointer(vrf->rth, NULL);
382 dst_release(&rth->dst);
385 static int vrf_rtable_create(struct net_device *dev)
387 struct net_vrf *vrf = netdev_priv(dev);
390 if (!fib_new_table(dev_net(dev), vrf->tb_id))
393 rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1, 1, 0);
397 rth->dst.output = vrf_output;
398 rth->rt_table_id = vrf->tb_id;
400 rcu_assign_pointer(vrf->rth, rth);
405 /**************************** device handling ********************/
407 /* cycle interface to flush neighbor cache and move routes across tables */
408 static void cycle_netdev(struct net_device *dev)
410 unsigned int flags = dev->flags;
413 if (!netif_running(dev))
416 ret = dev_change_flags(dev, flags & ~IFF_UP);
418 ret = dev_change_flags(dev, flags);
422 "Failed to cycle device %s; route tables might be wrong!\n",
427 static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
431 ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL);
435 port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
436 cycle_netdev(port_dev);
441 static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
443 if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev))
446 return do_vrf_add_slave(dev, port_dev);
449 /* inverse of do_vrf_add_slave */
450 static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
452 netdev_upper_dev_unlink(port_dev, dev);
453 port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
455 cycle_netdev(port_dev);
460 static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
462 return do_vrf_del_slave(dev, port_dev);
465 static void vrf_dev_uninit(struct net_device *dev)
467 struct net_vrf *vrf = netdev_priv(dev);
468 struct net_device *port_dev;
469 struct list_head *iter;
471 vrf_rtable_release(vrf);
472 vrf_rt6_release(vrf);
474 netdev_for_each_lower_dev(dev, port_dev, iter)
475 vrf_del_slave(dev, port_dev);
477 free_percpu(dev->dstats);
481 static int vrf_dev_init(struct net_device *dev)
483 struct net_vrf *vrf = netdev_priv(dev);
485 dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
489 /* create the default dst which points back to us */
490 if (vrf_rtable_create(dev) != 0)
493 if (vrf_rt6_create(dev) != 0)
496 dev->flags = IFF_MASTER | IFF_NOARP;
498 /* MTU is irrelevant for VRF device; set to 64k similar to lo */
499 dev->mtu = 64 * 1024;
501 /* similarly, oper state is irrelevant; set to up to avoid confusion */
502 dev->operstate = IF_OPER_UP;
507 vrf_rtable_release(vrf);
509 free_percpu(dev->dstats);
515 static const struct net_device_ops vrf_netdev_ops = {
516 .ndo_init = vrf_dev_init,
517 .ndo_uninit = vrf_dev_uninit,
518 .ndo_start_xmit = vrf_xmit,
519 .ndo_get_stats64 = vrf_get_stats64,
520 .ndo_add_slave = vrf_add_slave,
521 .ndo_del_slave = vrf_del_slave,
524 static u32 vrf_fib_table(const struct net_device *dev)
526 struct net_vrf *vrf = netdev_priv(dev);
531 static struct rtable *vrf_get_rtable(const struct net_device *dev,
532 const struct flowi4 *fl4)
534 struct rtable *rth = NULL;
536 if (!(fl4->flowi4_flags & FLOWI_FLAG_L3MDEV_SRC)) {
537 struct net_vrf *vrf = netdev_priv(dev);
541 rth = rcu_dereference(vrf->rth);
551 /* called under rcu_read_lock */
552 static int vrf_get_saddr(struct net_device *dev, struct flowi4 *fl4)
554 struct fib_result res = { .tclassid = 0 };
555 struct net *net = dev_net(dev);
556 u32 orig_tos = fl4->flowi4_tos;
557 u8 flags = fl4->flowi4_flags;
558 u8 scope = fl4->flowi4_scope;
559 u8 tos = RT_FL_TOS(fl4);
562 if (unlikely(!fl4->daddr))
565 fl4->flowi4_flags |= FLOWI_FLAG_SKIP_NH_OIF;
566 fl4->flowi4_iif = LOOPBACK_IFINDEX;
567 /* make sure oif is set to VRF device for lookup */
568 fl4->flowi4_oif = dev->ifindex;
569 fl4->flowi4_tos = tos & IPTOS_RT_MASK;
570 fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
571 RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
573 rc = fib_lookup(net, fl4, &res, 0);
575 if (res.type == RTN_LOCAL)
576 fl4->saddr = res.fi->fib_prefsrc ? : fl4->daddr;
578 fib_select_path(net, &res, fl4, -1);
581 fl4->flowi4_flags = flags;
582 fl4->flowi4_tos = orig_tos;
583 fl4->flowi4_scope = scope;
588 #if IS_ENABLED(CONFIG_IPV6)
589 /* neighbor handling is done with actual device; do not want
590 * to flip skb->dev for those ndisc packets. This really fails
591 * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
594 static bool ipv6_ndisc_frame(const struct sk_buff *skb)
596 const struct ipv6hdr *iph = ipv6_hdr(skb);
599 if (iph->nexthdr == NEXTHDR_ICMP) {
600 const struct icmp6hdr *icmph;
601 struct icmp6hdr _icmph;
603 icmph = skb_header_pointer(skb, sizeof(*iph),
604 sizeof(_icmph), &_icmph);
608 switch (icmph->icmp6_type) {
609 case NDISC_ROUTER_SOLICITATION:
610 case NDISC_ROUTER_ADVERTISEMENT:
611 case NDISC_NEIGHBOUR_SOLICITATION:
612 case NDISC_NEIGHBOUR_ADVERTISEMENT:
623 static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
626 /* if packet is NDISC keep the ingress interface */
627 if (!ipv6_ndisc_frame(skb)) {
629 skb->skb_iif = vrf_dev->ifindex;
631 skb_push(skb, skb->mac_len);
632 dev_queue_xmit_nit(skb, vrf_dev);
633 skb_pull(skb, skb->mac_len);
635 IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
642 static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
649 static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev,
653 skb->skb_iif = vrf_dev->ifindex;
655 skb_push(skb, skb->mac_len);
656 dev_queue_xmit_nit(skb, vrf_dev);
657 skb_pull(skb, skb->mac_len);
662 /* called with rcu lock held */
663 static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev,
669 return vrf_ip_rcv(vrf_dev, skb);
671 return vrf_ip6_rcv(vrf_dev, skb);
677 #if IS_ENABLED(CONFIG_IPV6)
678 static struct dst_entry *vrf_get_rt6_dst(const struct net_device *dev,
679 const struct flowi6 *fl6)
681 struct dst_entry *dst = NULL;
683 if (!(fl6->flowi6_flags & FLOWI_FLAG_L3MDEV_SRC)) {
684 struct net_vrf *vrf = netdev_priv(dev);
689 rt = rcu_dereference(vrf->rt6);
702 static const struct l3mdev_ops vrf_l3mdev_ops = {
703 .l3mdev_fib_table = vrf_fib_table,
704 .l3mdev_get_rtable = vrf_get_rtable,
705 .l3mdev_get_saddr = vrf_get_saddr,
706 .l3mdev_l3_rcv = vrf_l3_rcv,
707 #if IS_ENABLED(CONFIG_IPV6)
708 .l3mdev_get_rt6_dst = vrf_get_rt6_dst,
712 static void vrf_get_drvinfo(struct net_device *dev,
713 struct ethtool_drvinfo *info)
715 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
716 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
719 static const struct ethtool_ops vrf_ethtool_ops = {
720 .get_drvinfo = vrf_get_drvinfo,
723 static void vrf_setup(struct net_device *dev)
727 /* Initialize the device structure. */
728 dev->netdev_ops = &vrf_netdev_ops;
729 dev->l3mdev_ops = &vrf_l3mdev_ops;
730 dev->ethtool_ops = &vrf_ethtool_ops;
731 dev->destructor = free_netdev;
733 /* Fill in device structure with ethernet-generic values. */
734 eth_hw_addr_random(dev);
736 /* don't acquire vrf device's netif_tx_lock when transmitting */
737 dev->features |= NETIF_F_LLTX;
739 /* don't allow vrf devices to change network namespaces. */
740 dev->features |= NETIF_F_NETNS_LOCAL;
743 static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
745 if (tb[IFLA_ADDRESS]) {
746 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
748 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
749 return -EADDRNOTAVAIL;
754 static void vrf_dellink(struct net_device *dev, struct list_head *head)
756 unregister_netdevice_queue(dev, head);
759 static int vrf_newlink(struct net *src_net, struct net_device *dev,
760 struct nlattr *tb[], struct nlattr *data[])
762 struct net_vrf *vrf = netdev_priv(dev);
764 if (!data || !data[IFLA_VRF_TABLE])
767 vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
769 dev->priv_flags |= IFF_L3MDEV_MASTER;
771 return register_netdevice(dev);
774 static size_t vrf_nl_getsize(const struct net_device *dev)
776 return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
779 static int vrf_fillinfo(struct sk_buff *skb,
780 const struct net_device *dev)
782 struct net_vrf *vrf = netdev_priv(dev);
784 return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
787 static size_t vrf_get_slave_size(const struct net_device *bond_dev,
788 const struct net_device *slave_dev)
790 return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */
793 static int vrf_fill_slave_info(struct sk_buff *skb,
794 const struct net_device *vrf_dev,
795 const struct net_device *slave_dev)
797 struct net_vrf *vrf = netdev_priv(vrf_dev);
799 if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id))
805 static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
806 [IFLA_VRF_TABLE] = { .type = NLA_U32 },
809 static struct rtnl_link_ops vrf_link_ops __read_mostly = {
811 .priv_size = sizeof(struct net_vrf),
813 .get_size = vrf_nl_getsize,
814 .policy = vrf_nl_policy,
815 .validate = vrf_validate,
816 .fill_info = vrf_fillinfo,
818 .get_slave_size = vrf_get_slave_size,
819 .fill_slave_info = vrf_fill_slave_info,
821 .newlink = vrf_newlink,
822 .dellink = vrf_dellink,
824 .maxtype = IFLA_VRF_MAX,
827 static int vrf_device_event(struct notifier_block *unused,
828 unsigned long event, void *ptr)
830 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
832 /* only care about unregister events to drop slave references */
833 if (event == NETDEV_UNREGISTER) {
834 struct net_device *vrf_dev;
836 if (!netif_is_l3_slave(dev))
839 vrf_dev = netdev_master_upper_dev_get(dev);
840 vrf_del_slave(vrf_dev, dev);
846 static struct notifier_block vrf_notifier_block __read_mostly = {
847 .notifier_call = vrf_device_event,
850 static int __init vrf_init_module(void)
854 register_netdevice_notifier(&vrf_notifier_block);
856 rc = rtnl_link_register(&vrf_link_ops);
863 unregister_netdevice_notifier(&vrf_notifier_block);
867 module_init(vrf_init_module);
868 MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
869 MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
870 MODULE_LICENSE("GPL");
871 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
872 MODULE_VERSION(DRV_VERSION);