Merge branch 'vrf-lite'
authorDavid S. Miller <davem@davemloft.net>
Fri, 14 Aug 2015 05:43:22 +0000 (22:43 -0700)
committerDavid S. Miller <davem@davemloft.net>
Fri, 14 Aug 2015 05:43:22 +0000 (22:43 -0700)
David Ahern says:

====================
VRF-lite - v6

In the context of internet scale routing a requirement that always comes
up is the need to partition the available routing tables into disjoint
routing planes. A specific use case is the multi-tenancy problem where
each tenant has their own unique routing tables and in the very least
need different default gateways.

This patch allows the ability to create virtual router domains (aka VRFs
(VRF-lite to be specific) in the linux packet forwarding stack. The main
observation is that through the use of rules and socket binding to interfaces,
all the facilities that we need are already present in the infrastructure. What
is missing is a handle that identifies a routing domain and can be used to
gather applicable rules/tables and uniqify neighbor selection. The scheme used
needs to preserves the notions of ECMP, and general routing principles.

This driver is a cross between functionality that the IPVLAN driver
and the Team drivers provide where a device is created and packets
into/out of the routing domain are shuttled through this device. The
device is then used as a handle to identify the applicable rules. The
VRF device is thus the layer3 equivalent of a vlan device.

The very important point to note is that this is only a Layer3 concept
so L2 tools (e.g., LLDP) do not need to be run in each VRF, processes can
run in unaware mode or select a VRF to be talking through. Also the
behavioral model is a generalized application of the familiar VRF-Lite
model with some performance paths that need optimization. (Specifically
the output route selector that Roopa, Robert, Thomas and EricB are
currently discussing on the MPLS thread)

High Level points
=================
1. Simple overlay driver (minimal changes to current stack)
   * uses the existing fib tables and fib rules infrastructure
2. Modelled closely after the ipvlan driver
3. Uses current API and infrastructure.
   * Applications can use SO_BINDTODEVICE or cmsg device indentifiers
     to pick VRF (ping, traceroute just work)
   * Standard IP Rules work, and since they are aggregated against the
     device, scale is manageable
4. Completely orthogonal to Namespaces and only provides separation in
   the routing plane (and ARP)

                                                 N2
           N1 (all configs here)          +---------------+
    +--------------+                      |               |
    |swp1 :10.0.1.1+----------------------+swp1 :10.0.1.2 |
    |              |                      |               |
    |swp2 :10.0.2.1+----------------------+swp2 :10.0.2.2 |
    |              |                      +---------------+
    | VRF 1        |
    | table 5      |
    |              |
    +---------------+
    |              |
    | VRF 2        |                             N3
    | table 6      |                      +---------------+
    |              |                      |               |
    |swp3 :10.0.2.1+----------------------+swp1 :10.0.2.2 |
    |              |                      |               |
    |swp4 :10.0.3.1+----------------------+swp2 :10.0.3.2 |
    +--------------+                      +---------------+

Given the topology above, the setup needed to get the basic VRF
functions working would be

Create the VRF devices and associate with a table
    ip link add vrf1 type vrf table 5
    ip link add vrf2 type vrf table 6

Install the lookup rules that map table to VRF domain
    ip rule add pref 200 oif vrf1 lookup 5
    ip rule add pref 200 iif vrf1 lookup 5
    ip rule add pref 200 oif vrf2 lookup 6
    ip rule add pref 200 iif vrf2 lookup 6

    ip link set vrf1 up
    ip link set vrf2 up

Enslave the routing member interfaces
    ip link set swp1 master vrf1
    ip link set swp2 master vrf1
    ip link set swp3 master vrf2
    ip link set swp4 master vrf2

Connected and local routes are automatically moved from main and local
tables to the VRF table.

ping using VRF0 is simply
    ping -I vrf0 10.0.1.2

Design Highlights
=================
If a device is enslaved to a VRF device (ie., associated with a VRF)
then:
1. Rx path
   The master device index is used as the iif for all lookups.

2. Tx path
   Similarly, for Tx the VRF device oif is used in the flow to direct
   lookups to the table associated with the VRF via its rule. From there
   the FLOWI_FLAG_VRFSRC flag is used to indicate that the oif should
   not be used for FIB table lookups.

3. Connected and local routes
   On link up for a device, connected and local routes are added to the
   table associated with the VRF device, rather than the local and main
   tables.

4. Socket lookups
   Sockets operating in the VRF must be bound to the VRF device. As such
   socket lookups compare the VRF device index to sk_bound_dev_if.

5. Neighbor entries
   Neighbor entries are not impacted by the VRF device. Entries are
   associated with a particular interface; the VRF association is indirect
   via the interface-to-VRF device enslavement.

Version 6
- addressed comments from DaveM

- added patch to properly set oif in ip_send_unicast_reply. Needs to be
  set to VRF device for proper FIB lookup

- added patch to handle IP fragments

Version 5
- dropped patch regarding socket lookups; no longer needed
  + removed vrf helpers no longer needed after this patch is dropped
- removed dev_open and close operations
  + no need to reset vrf data on an ifdown and creates problems if a
    slave is deleted while the vrf interface is down (Thanks, Nikolay)
- cleanups for sparse warnings
  + make C=2 is now clean for vrf driver

Version 4
- builds are clean with and without VRF device enabled (no, yes and module)
- tightened the driver implementation
  + device add/delete, slave add/remove, and module unload are all clean
- fixed RCU references
  + with RCU and lock debugging enabled changes are clean through the
    suite of tests
- TX path uses custom dst, so patch refactoring rtable allocation is
  dropped along with the patch adding rt_nexthop helper
- dropped the task patch that adds default bind to interface for sockets
  and the associated chvrf example command
  + the patches are a convenience for running unmodified code. They
    are not needed for the core functionality. Any application with
    support for SO_BINDTODEVICE works properly with this patch set.

Version 3
- addressed comments from first 2 RFCs with the exception of the name
  Nicolas: We will do the name conversion once we agree on what the
           correct name should be (vrf, mrf or something else)

-  packets flow through the VRF device in both directions allowing the
   following:
   - tcpdump -i vrf<n>
   - tc rules on vrf device
   - netfilter rules on vrf device

TO-DO
=====
1. IPv6

2. ipsec, xfrms
   - dst patch accepted into ipsec-next; will post VRF patch once merge happens

3. listen filter to allow 1 socket to work with multiple VRF devices
   - i.e., bind to VRF's a, b, c only or NOT VRFs e, f, g

Eric B:
  I have ipsec working with VRFs implemented using the VRF driver,
  including the worst case scenario of complete duplication in the
  networking config.

Thanks to Nikolay for his many, many code reviews whipping the device
driver into shape, and bug-Fixes and ideas from Hannes, Roopa Prabhu,
Jon Toppins, Jamal.
====================

Signed-off-by: David S. Miller <davem@davemloft.net>
18 files changed:
drivers/net/Kconfig
drivers/net/Makefile
drivers/net/vrf.c [new file with mode: 0644]
include/linux/netdevice.h
include/net/flow.h
include/net/route.h
include/net/vrf.h [new file with mode: 0644]
include/uapi/linux/if_link.h
net/ipv4/af_inet.c
net/ipv4/arp.c
net/ipv4/fib_frontend.c
net/ipv4/fib_semantics.c
net/ipv4/fib_trie.c
net/ipv4/icmp.c
net/ipv4/ip_fragment.c
net/ipv4/ip_output.c
net/ipv4/route.c
net/ipv4/udp.c

index c18f9e62a9fa2ea181c7ab2c75faaa9bc713b2ec..e58468b02987f28c57252c1acdbfa8ac4c6365ca 100644 (file)
@@ -297,6 +297,13 @@ config NLMON
          diagnostics, etc. This is mostly intended for developers or support
          to debug netlink issues. If unsure, say N.
 
+config NET_VRF
+       tristate "Virtual Routing and Forwarding (Lite)"
+       depends on IP_MULTIPLE_TABLES && IPV6_MULTIPLE_TABLES
+       ---help---
+         This option enables the support for mapping interfaces into VRF's. The
+         support enables VRF devices.
+
 endif # NET_CORE
 
 config SUNGEM_PHY
index c12cb22478a7daa39e2935f1c0aa22c26ab04bdc..ca16dd689b36ba7836b71a8a24c9b81913b5ca67 100644 (file)
@@ -25,6 +25,7 @@ obj-$(CONFIG_VIRTIO_NET) += virtio_net.o
 obj-$(CONFIG_VXLAN) += vxlan.o
 obj-$(CONFIG_GENEVE) += geneve.o
 obj-$(CONFIG_NLMON) += nlmon.o
+obj-$(CONFIG_NET_VRF) += vrf.o
 
 #
 # Networking Drivers
diff --git a/drivers/net/vrf.c b/drivers/net/vrf.c
new file mode 100644 (file)
index 0000000..95097cb
--- /dev/null
@@ -0,0 +1,685 @@
+/*
+ * vrf.c: device driver to encapsulate a VRF space
+ *
+ * Copyright (c) 2015 Cumulus Networks. All rights reserved.
+ * Copyright (c) 2015 Shrijeet Mukherjee <shm@cumulusnetworks.com>
+ * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
+ *
+ * Based on dummy, team and ipvlan drivers
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#include <linux/module.h>
+#include <linux/kernel.h>
+#include <linux/netdevice.h>
+#include <linux/etherdevice.h>
+#include <linux/ip.h>
+#include <linux/init.h>
+#include <linux/moduleparam.h>
+#include <linux/netfilter.h>
+#include <linux/rtnetlink.h>
+#include <net/rtnetlink.h>
+#include <linux/u64_stats_sync.h>
+#include <linux/hashtable.h>
+
+#include <linux/inetdevice.h>
+#include <net/ip.h>
+#include <net/ip_fib.h>
+#include <net/ip6_route.h>
+#include <net/rtnetlink.h>
+#include <net/route.h>
+#include <net/addrconf.h>
+#include <net/vrf.h>
+
+#define DRV_NAME       "vrf"
+#define DRV_VERSION    "1.0"
+
+#define vrf_is_slave(dev)   ((dev)->flags & IFF_SLAVE)
+
+#define vrf_master_get_rcu(dev) \
+       ((struct net_device *)rcu_dereference(dev->rx_handler_data))
+
+struct pcpu_dstats {
+       u64                     tx_pkts;
+       u64                     tx_bytes;
+       u64                     tx_drps;
+       u64                     rx_pkts;
+       u64                     rx_bytes;
+       struct u64_stats_sync   syncp;
+};
+
+static struct dst_entry *vrf_ip_check(struct dst_entry *dst, u32 cookie)
+{
+       return dst;
+}
+
+static int vrf_ip_local_out(struct sk_buff *skb)
+{
+       return ip_local_out(skb);
+}
+
+static unsigned int vrf_v4_mtu(const struct dst_entry *dst)
+{
+       /* TO-DO: return max ethernet size? */
+       return dst->dev->mtu;
+}
+
+static void vrf_dst_destroy(struct dst_entry *dst)
+{
+       /* our dst lives forever - or until the device is closed */
+}
+
+static unsigned int vrf_default_advmss(const struct dst_entry *dst)
+{
+       return 65535 - 40;
+}
+
+static struct dst_ops vrf_dst_ops = {
+       .family         = AF_INET,
+       .local_out      = vrf_ip_local_out,
+       .check          = vrf_ip_check,
+       .mtu            = vrf_v4_mtu,
+       .destroy        = vrf_dst_destroy,
+       .default_advmss = vrf_default_advmss,
+};
+
+static bool is_ip_rx_frame(struct sk_buff *skb)
+{
+       switch (skb->protocol) {
+       case htons(ETH_P_IP):
+       case htons(ETH_P_IPV6):
+               return true;
+       }
+       return false;
+}
+
+/* note: already called with rcu_read_lock */
+static rx_handler_result_t vrf_handle_frame(struct sk_buff **pskb)
+{
+       struct sk_buff *skb = *pskb;
+
+       if (is_ip_rx_frame(skb)) {
+               struct net_device *dev = vrf_master_get_rcu(skb->dev);
+               struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
+
+               u64_stats_update_begin(&dstats->syncp);
+               dstats->rx_pkts++;
+               dstats->rx_bytes += skb->len;
+               u64_stats_update_end(&dstats->syncp);
+
+               skb->dev = dev;
+
+               return RX_HANDLER_ANOTHER;
+       }
+       return RX_HANDLER_PASS;
+}
+
+static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
+                                                struct rtnl_link_stats64 *stats)
+{
+       int i;
+
+       for_each_possible_cpu(i) {
+               const struct pcpu_dstats *dstats;
+               u64 tbytes, tpkts, tdrops, rbytes, rpkts;
+               unsigned int start;
+
+               dstats = per_cpu_ptr(dev->dstats, i);
+               do {
+                       start = u64_stats_fetch_begin_irq(&dstats->syncp);
+                       tbytes = dstats->tx_bytes;
+                       tpkts = dstats->tx_pkts;
+                       tdrops = dstats->tx_drps;
+                       rbytes = dstats->rx_bytes;
+                       rpkts = dstats->rx_pkts;
+               } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
+               stats->tx_bytes += tbytes;
+               stats->tx_packets += tpkts;
+               stats->tx_dropped += tdrops;
+               stats->rx_bytes += rbytes;
+               stats->rx_packets += rpkts;
+       }
+       return stats;
+}
+
+static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
+                                          struct net_device *dev)
+{
+       return 0;
+}
+
+static int vrf_send_v4_prep(struct sk_buff *skb, struct flowi4 *fl4,
+                           struct net_device *vrf_dev)
+{
+       struct rtable *rt;
+       int err = 1;
+
+       rt = ip_route_output_flow(dev_net(vrf_dev), fl4, NULL);
+       if (IS_ERR(rt))
+               goto out;
+
+       /* TO-DO: what about broadcast ? */
+       if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
+               ip_rt_put(rt);
+               goto out;
+       }
+
+       skb_dst_drop(skb);
+       skb_dst_set(skb, &rt->dst);
+       err = 0;
+out:
+       return err;
+}
+
+static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
+                                          struct net_device *vrf_dev)
+{
+       struct iphdr *ip4h = ip_hdr(skb);
+       int ret = NET_XMIT_DROP;
+       struct flowi4 fl4 = {
+               /* needed to match OIF rule */
+               .flowi4_oif = vrf_dev->ifindex,
+               .flowi4_iif = LOOPBACK_IFINDEX,
+               .flowi4_tos = RT_TOS(ip4h->tos),
+               .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_VRFSRC,
+               .daddr = ip4h->daddr,
+       };
+
+       if (vrf_send_v4_prep(skb, &fl4, vrf_dev))
+               goto err;
+
+       if (!ip4h->saddr) {
+               ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
+                                              RT_SCOPE_LINK);
+       }
+
+       ret = ip_local_out(skb);
+       if (unlikely(net_xmit_eval(ret)))
+               vrf_dev->stats.tx_errors++;
+       else
+               ret = NET_XMIT_SUCCESS;
+
+out:
+       return ret;
+err:
+       vrf_dev->stats.tx_errors++;
+       kfree_skb(skb);
+       goto out;
+}
+
+static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
+{
+       switch (skb->protocol) {
+       case htons(ETH_P_IP):
+               return vrf_process_v4_outbound(skb, dev);
+       case htons(ETH_P_IPV6):
+               return vrf_process_v6_outbound(skb, dev);
+       default:
+               return NET_XMIT_DROP;
+       }
+}
+
+static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
+{
+       netdev_tx_t ret = is_ip_tx_frame(skb, dev);
+
+       if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
+               struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
+
+               u64_stats_update_begin(&dstats->syncp);
+               dstats->tx_pkts++;
+               dstats->tx_bytes += skb->len;
+               u64_stats_update_end(&dstats->syncp);
+       } else {
+               this_cpu_inc(dev->dstats->tx_drps);
+       }
+
+       return ret;
+}
+
+static netdev_tx_t vrf_finish(struct sock *sk, struct sk_buff *skb)
+{
+       return dev_queue_xmit(skb);
+}
+
+static int vrf_output(struct sock *sk, struct sk_buff *skb)
+{
+       struct net_device *dev = skb_dst(skb)->dev;
+
+       IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len);
+
+       skb->dev = dev;
+       skb->protocol = htons(ETH_P_IP);
+
+       return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, skb,
+                           NULL, dev,
+                           vrf_finish,
+                           !(IPCB(skb)->flags & IPSKB_REROUTED));
+}
+
+static void vrf_rtable_destroy(struct net_vrf *vrf)
+{
+       struct dst_entry *dst = (struct dst_entry *)vrf->rth;
+
+       if (dst)
+               dst_destroy(dst);
+       vrf->rth = NULL;
+}
+
+static struct rtable *vrf_rtable_create(struct net_device *dev)
+{
+       struct rtable *rth;
+
+       rth = dst_alloc(&vrf_dst_ops, dev, 2,
+                       DST_OBSOLETE_NONE,
+                       (DST_HOST | DST_NOPOLICY | DST_NOXFRM));
+       if (rth) {
+               rth->dst.output = vrf_output;
+               rth->rt_genid   = rt_genid_ipv4(dev_net(dev));
+               rth->rt_flags   = 0;
+               rth->rt_type    = RTN_UNICAST;
+               rth->rt_is_input = 0;
+               rth->rt_iif     = 0;
+               rth->rt_pmtu    = 0;
+               rth->rt_gateway = 0;
+               rth->rt_uses_gateway = 0;
+               INIT_LIST_HEAD(&rth->rt_uncached);
+               rth->rt_uncached_list = NULL;
+               rth->rt_lwtstate = NULL;
+       }
+
+       return rth;
+}
+
+/**************************** device handling ********************/
+
+/* cycle interface to flush neighbor cache and move routes across tables */
+static void cycle_netdev(struct net_device *dev)
+{
+       unsigned int flags = dev->flags;
+       int ret;
+
+       if (!netif_running(dev))
+               return;
+
+       ret = dev_change_flags(dev, flags & ~IFF_UP);
+       if (ret >= 0)
+               ret = dev_change_flags(dev, flags);
+
+       if (ret < 0) {
+               netdev_err(dev,
+                          "Failed to cycle device %s; route tables might be wrong!\n",
+                          dev->name);
+       }
+}
+
+static struct slave *__vrf_find_slave_dev(struct slave_queue *queue,
+                                         struct net_device *dev)
+{
+       struct list_head *head = &queue->all_slaves;
+       struct slave *slave;
+
+       list_for_each_entry(slave, head, list) {
+               if (slave->dev == dev)
+                       return slave;
+       }
+
+       return NULL;
+}
+
+/* inverse of __vrf_insert_slave */
+static void __vrf_remove_slave(struct slave_queue *queue, struct slave *slave)
+{
+       dev_put(slave->dev);
+       list_del(&slave->list);
+       queue->num_slaves--;
+}
+
+static void __vrf_insert_slave(struct slave_queue *queue, struct slave *slave)
+{
+       dev_hold(slave->dev);
+       list_add(&slave->list, &queue->all_slaves);
+       queue->num_slaves++;
+}
+
+static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
+{
+       struct net_vrf_dev *vrf_ptr = kmalloc(sizeof(*vrf_ptr), GFP_KERNEL);
+       struct slave *slave = kzalloc(sizeof(*slave), GFP_KERNEL);
+       struct slave *duplicate_slave;
+       struct net_vrf *vrf = netdev_priv(dev);
+       struct slave_queue *queue = &vrf->queue;
+       int ret = -ENOMEM;
+
+       if (!slave || !vrf_ptr)
+               goto out_fail;
+
+       slave->dev = port_dev;
+
+       vrf_ptr->ifindex = dev->ifindex;
+       vrf_ptr->tb_id = vrf->tb_id;
+
+       duplicate_slave = __vrf_find_slave_dev(queue, port_dev);
+       if (duplicate_slave) {
+               ret = -EBUSY;
+               goto out_fail;
+       }
+
+       __vrf_insert_slave(queue, slave);
+
+       /* register the packet handler for slave ports */
+       ret = netdev_rx_handler_register(port_dev, vrf_handle_frame, dev);
+       if (ret) {
+               netdev_err(port_dev,
+                          "Device %s failed to register rx_handler\n",
+                          port_dev->name);
+               goto out_remove;
+       }
+
+       ret = netdev_master_upper_dev_link(port_dev, dev);
+       if (ret < 0)
+               goto out_unregister;
+
+       port_dev->flags |= IFF_SLAVE;
+
+       rcu_assign_pointer(port_dev->vrf_ptr, vrf_ptr);
+       cycle_netdev(port_dev);
+
+       return 0;
+
+out_unregister:
+       netdev_rx_handler_unregister(port_dev);
+out_remove:
+       __vrf_remove_slave(queue, slave);
+out_fail:
+       kfree(vrf_ptr);
+       kfree(slave);
+       return ret;
+}
+
+static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
+{
+       if (!netif_is_vrf(dev) || netif_is_vrf(port_dev) ||
+           vrf_is_slave(port_dev))
+               return -EINVAL;
+
+       return do_vrf_add_slave(dev, port_dev);
+}
+
+/* inverse of do_vrf_add_slave */
+static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
+{
+       struct net_vrf_dev *vrf_ptr = rtnl_dereference(port_dev->vrf_ptr);
+       struct net_vrf *vrf = netdev_priv(dev);
+       struct slave_queue *queue = &vrf->queue;
+       struct slave *slave;
+
+       RCU_INIT_POINTER(port_dev->vrf_ptr, NULL);
+
+       netdev_upper_dev_unlink(port_dev, dev);
+       port_dev->flags &= ~IFF_SLAVE;
+
+       netdev_rx_handler_unregister(port_dev);
+
+       /* after netdev_rx_handler_unregister for synchronize_rcu */
+       kfree(vrf_ptr);
+
+       cycle_netdev(port_dev);
+
+       slave = __vrf_find_slave_dev(queue, port_dev);
+       if (slave)
+               __vrf_remove_slave(queue, slave);
+
+       kfree(slave);
+
+       return 0;
+}
+
+static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
+{
+       if (!netif_is_vrf(dev))
+               return -EINVAL;
+
+       return do_vrf_del_slave(dev, port_dev);
+}
+
+static void vrf_dev_uninit(struct net_device *dev)
+{
+       struct net_vrf *vrf = netdev_priv(dev);
+       struct slave_queue *queue = &vrf->queue;
+       struct list_head *head = &queue->all_slaves;
+       struct slave *slave, *next;
+
+       vrf_rtable_destroy(vrf);
+
+       list_for_each_entry_safe(slave, next, head, list)
+               vrf_del_slave(dev, slave->dev);
+
+       if (dev->dstats)
+               free_percpu(dev->dstats);
+       dev->dstats = NULL;
+}
+
+static int vrf_dev_init(struct net_device *dev)
+{
+       struct net_vrf *vrf = netdev_priv(dev);
+
+       INIT_LIST_HEAD(&vrf->queue.all_slaves);
+
+       dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
+       if (!dev->dstats)
+               goto out_nomem;
+
+       /* create the default dst which points back to us */
+       vrf->rth = vrf_rtable_create(dev);
+       if (!vrf->rth)
+               goto out_stats;
+
+       dev->flags = IFF_MASTER | IFF_NOARP;
+
+       return 0;
+
+out_stats:
+       free_percpu(dev->dstats);
+       dev->dstats = NULL;
+out_nomem:
+       return -ENOMEM;
+}
+
+static const struct net_device_ops vrf_netdev_ops = {
+       .ndo_init               = vrf_dev_init,
+       .ndo_uninit             = vrf_dev_uninit,
+       .ndo_start_xmit         = vrf_xmit,
+       .ndo_get_stats64        = vrf_get_stats64,
+       .ndo_add_slave          = vrf_add_slave,
+       .ndo_del_slave          = vrf_del_slave,
+};
+
+static void vrf_get_drvinfo(struct net_device *dev,
+                           struct ethtool_drvinfo *info)
+{
+       strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
+       strlcpy(info->version, DRV_VERSION, sizeof(info->version));
+}
+
+static const struct ethtool_ops vrf_ethtool_ops = {
+       .get_drvinfo    = vrf_get_drvinfo,
+};
+
+static void vrf_setup(struct net_device *dev)
+{
+       ether_setup(dev);
+
+       /* Initialize the device structure. */
+       dev->netdev_ops = &vrf_netdev_ops;
+       dev->ethtool_ops = &vrf_ethtool_ops;
+       dev->destructor = free_netdev;
+
+       /* Fill in device structure with ethernet-generic values. */
+       eth_hw_addr_random(dev);
+
+       /* don't acquire vrf device's netif_tx_lock when transmitting */
+       dev->features |= NETIF_F_LLTX;
+
+       /* don't allow vrf devices to change network namespaces. */
+       dev->features |= NETIF_F_NETNS_LOCAL;
+}
+
+static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
+{
+       if (tb[IFLA_ADDRESS]) {
+               if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
+                       return -EINVAL;
+               if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
+                       return -EADDRNOTAVAIL;
+       }
+       return 0;
+}
+
+static void vrf_dellink(struct net_device *dev, struct list_head *head)
+{
+       struct net_vrf_dev *vrf_ptr = rtnl_dereference(dev->vrf_ptr);
+
+       RCU_INIT_POINTER(dev->vrf_ptr, NULL);
+       kfree_rcu(vrf_ptr, rcu);
+       unregister_netdevice_queue(dev, head);
+}
+
+static int vrf_newlink(struct net *src_net, struct net_device *dev,
+                      struct nlattr *tb[], struct nlattr *data[])
+{
+       struct net_vrf *vrf = netdev_priv(dev);
+       struct net_vrf_dev *vrf_ptr;
+       int err;
+
+       if (!data || !data[IFLA_VRF_TABLE])
+               return -EINVAL;
+
+       vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
+
+       dev->priv_flags |= IFF_VRF_MASTER;
+
+       err = -ENOMEM;
+       vrf_ptr = kmalloc(sizeof(*dev->vrf_ptr), GFP_KERNEL);
+       if (!vrf_ptr)
+               goto out_fail;
+
+       vrf_ptr->ifindex = dev->ifindex;
+       vrf_ptr->tb_id = vrf->tb_id;
+
+       err = register_netdevice(dev);
+       if (err < 0)
+               goto out_fail;
+
+       rcu_assign_pointer(dev->vrf_ptr, vrf_ptr);
+
+       return 0;
+
+out_fail:
+       kfree(vrf_ptr);
+       free_netdev(dev);
+       return err;
+}
+
+static size_t vrf_nl_getsize(const struct net_device *dev)
+{
+       return nla_total_size(sizeof(u32));  /* IFLA_VRF_TABLE */
+}
+
+static int vrf_fillinfo(struct sk_buff *skb,
+                       const struct net_device *dev)
+{
+       struct net_vrf *vrf = netdev_priv(dev);
+
+       return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
+}
+
+static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
+       [IFLA_VRF_TABLE] = { .type = NLA_U32 },
+};
+
+static struct rtnl_link_ops vrf_link_ops __read_mostly = {
+       .kind           = DRV_NAME,
+       .priv_size      = sizeof(struct net_vrf),
+
+       .get_size       = vrf_nl_getsize,
+       .policy         = vrf_nl_policy,
+       .validate       = vrf_validate,
+       .fill_info      = vrf_fillinfo,
+
+       .newlink        = vrf_newlink,
+       .dellink        = vrf_dellink,
+       .setup          = vrf_setup,
+       .maxtype        = IFLA_VRF_MAX,
+};
+
+static int vrf_device_event(struct notifier_block *unused,
+                           unsigned long event, void *ptr)
+{
+       struct net_device *dev = netdev_notifier_info_to_dev(ptr);
+
+       /* only care about unregister events to drop slave references */
+       if (event == NETDEV_UNREGISTER) {
+               struct net_vrf_dev *vrf_ptr = rtnl_dereference(dev->vrf_ptr);
+               struct net_device *vrf_dev;
+
+               if (!vrf_ptr || netif_is_vrf(dev))
+                       goto out;
+
+               vrf_dev = __dev_get_by_index(dev_net(dev), vrf_ptr->ifindex);
+               if (vrf_dev)
+                       vrf_del_slave(vrf_dev, dev);
+       }
+out:
+       return NOTIFY_DONE;
+}
+
+static struct notifier_block vrf_notifier_block __read_mostly = {
+       .notifier_call = vrf_device_event,
+};
+
+static int __init vrf_init_module(void)
+{
+       int rc;
+
+       vrf_dst_ops.kmem_cachep =
+               kmem_cache_create("vrf_ip_dst_cache",
+                                 sizeof(struct rtable), 0,
+                                 SLAB_HWCACHE_ALIGN | SLAB_PANIC,
+                                 NULL);
+
+       if (!vrf_dst_ops.kmem_cachep)
+               return -ENOMEM;
+
+       register_netdevice_notifier(&vrf_notifier_block);
+
+       rc = rtnl_link_register(&vrf_link_ops);
+       if (rc < 0)
+               goto error;
+
+       return 0;
+
+error:
+       unregister_netdevice_notifier(&vrf_notifier_block);
+       kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
+       return rc;
+}
+
+static void __exit vrf_cleanup_module(void)
+{
+       rtnl_link_unregister(&vrf_link_ops);
+       unregister_netdevice_notifier(&vrf_notifier_block);
+       kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
+}
+
+module_init(vrf_init_module);
+module_exit(vrf_cleanup_module);
+MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
+MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
+MODULE_LICENSE("GPL");
+MODULE_ALIAS_RTNL_LINK(DRV_NAME);
+MODULE_VERSION(DRV_VERSION);
index 607b5f41f46f93e506adbefd7b3ed11ed8acfb67..f7a6ef2fae3a46a82aba6088b25ed15afd0f6952 100644 (file)
@@ -1289,6 +1289,7 @@ enum netdev_priv_flags {
        IFF_XMIT_DST_RELEASE_PERM       = 1<<22,
        IFF_IPVLAN_MASTER               = 1<<23,
        IFF_IPVLAN_SLAVE                = 1<<24,
+       IFF_VRF_MASTER                  = 1<<25,
 };
 
 #define IFF_802_1Q_VLAN                        IFF_802_1Q_VLAN
@@ -1316,6 +1317,7 @@ enum netdev_priv_flags {
 #define IFF_XMIT_DST_RELEASE_PERM      IFF_XMIT_DST_RELEASE_PERM
 #define IFF_IPVLAN_MASTER              IFF_IPVLAN_MASTER
 #define IFF_IPVLAN_SLAVE               IFF_IPVLAN_SLAVE
+#define IFF_VRF_MASTER                 IFF_VRF_MASTER
 
 /**
  *     struct net_device - The DEVICE structure.
@@ -1432,6 +1434,7 @@ enum netdev_priv_flags {
  *     @dn_ptr:        DECnet specific data
  *     @ip6_ptr:       IPv6 specific data
  *     @ax25_ptr:      AX.25 specific data
+ *     @vrf_ptr:       VRF specific data
  *     @ieee80211_ptr: IEEE 802.11 specific data, assign before registering
  *
  *     @last_rx:       Time of last Rx
@@ -1650,6 +1653,7 @@ struct net_device {
        struct dn_dev __rcu     *dn_ptr;
        struct inet6_dev __rcu  *ip6_ptr;
        void                    *ax25_ptr;
+       struct net_vrf_dev __rcu *vrf_ptr;
        struct wireless_dev     *ieee80211_ptr;
        struct wpan_dev         *ieee802154_ptr;
 #if IS_ENABLED(CONFIG_MPLS_ROUTING)
@@ -3808,6 +3812,22 @@ static inline bool netif_supports_nofcs(struct net_device *dev)
        return dev->priv_flags & IFF_SUPP_NOFCS;
 }
 
+static inline bool netif_is_vrf(const struct net_device *dev)
+{
+       return dev->priv_flags & IFF_VRF_MASTER;
+}
+
+static inline bool netif_index_is_vrf(struct net *net, int ifindex)
+{
+       struct net_device *dev = dev_get_by_index_rcu(net, ifindex);
+       bool rc = false;
+
+       if (dev)
+               rc = netif_is_vrf(dev);
+
+       return rc;
+}
+
 /* This device needs to keep skb dst for qdisc enqueue or ndo_start_xmit() */
 static inline void netif_keep_dst(struct net_device *dev)
 {
index 3098ae33a1784f920e26dd445a041e9deac1888e..f305588fc16276fc530e165c3bcb7596e7939f0b 100644 (file)
@@ -33,6 +33,7 @@ struct flowi_common {
        __u8    flowic_flags;
 #define FLOWI_FLAG_ANYSRC              0x01
 #define FLOWI_FLAG_KNOWN_NH            0x02
+#define FLOWI_FLAG_VRFSRC              0x04
        __u32   flowic_secid;
        struct flowi_tunnel flowic_tun_key;
 };
index 2d45f419477fedadeef42818e7159c148dfd6b84..6dda2c1bf8c6d12951b7e3d81273ca77d16a1720 100644 (file)
@@ -189,8 +189,12 @@ void ipv4_sk_redirect(struct sk_buff *skb, struct sock *sk);
 void ip_rt_send_redirect(struct sk_buff *skb);
 
 unsigned int inet_addr_type(struct net *net, __be32 addr);
+unsigned int inet_addr_type_table(struct net *net, __be32 addr, int tb_id);
 unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
                                __be32 addr);
+unsigned int inet_addr_type_dev_table(struct net *net,
+                                     const struct net_device *dev,
+                                     __be32 addr);
 void ip_rt_multicast_event(struct in_device *);
 int ip_rt_ioctl(struct net *, unsigned int cmd, void __user *arg);
 void ip_rt_get_source(u8 *src, struct sk_buff *skb, struct rtable *rt);
@@ -251,6 +255,9 @@ static inline void ip_route_connect_init(struct flowi4 *fl4, __be32 dst, __be32
        if (inet_sk(sk)->transparent)
                flow_flags |= FLOWI_FLAG_ANYSRC;
 
+       if (netif_index_is_vrf(sock_net(sk), oif))
+               flow_flags |= FLOWI_FLAG_VRFSRC;
+
        flowi4_init_output(fl4, oif, sk->sk_mark, tos, RT_SCOPE_UNIVERSE,
                           protocol, flow_flags, dst, src, dport, sport);
 }
diff --git a/include/net/vrf.h b/include/net/vrf.h
new file mode 100644 (file)
index 0000000..0484d29
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * include/net/net_vrf.h - adds vrf dev structure definitions
+ * Copyright (c) 2015 Cumulus Networks
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ */
+
+#ifndef __LINUX_NET_VRF_H
+#define __LINUX_NET_VRF_H
+
+struct net_vrf_dev {
+       struct rcu_head         rcu;
+       int                     ifindex; /* ifindex of master dev */
+       u32                     tb_id;   /* table id for VRF */
+};
+
+struct slave {
+       struct list_head        list;
+       struct net_device       *dev;
+};
+
+struct slave_queue {
+       struct list_head        all_slaves;
+       int                     num_slaves;
+};
+
+struct net_vrf {
+       struct slave_queue      queue;
+       struct rtable           *rth;
+       u32                     tb_id;
+};
+
+
+#if IS_ENABLED(CONFIG_NET_VRF)
+/* called with rcu_read_lock() */
+static inline int vrf_master_ifindex_rcu(const struct net_device *dev)
+{
+       struct net_vrf_dev *vrf_ptr;
+       int ifindex = 0;
+
+       if (!dev)
+               return 0;
+
+       if (netif_is_vrf(dev))
+               ifindex = dev->ifindex;
+       else {
+               vrf_ptr = rcu_dereference(dev->vrf_ptr);
+               if (vrf_ptr)
+                       ifindex = vrf_ptr->ifindex;
+       }
+
+       return ifindex;
+}
+
+/* called with rcu_read_lock */
+static inline int vrf_dev_table_rcu(const struct net_device *dev)
+{
+       int tb_id = 0;
+
+       if (dev) {
+               struct net_vrf_dev *vrf_ptr;
+
+               vrf_ptr = rcu_dereference(dev->vrf_ptr);
+               if (vrf_ptr)
+                       tb_id = vrf_ptr->tb_id;
+       }
+       return tb_id;
+}
+
+static inline int vrf_dev_table(const struct net_device *dev)
+{
+       int tb_id;
+
+       rcu_read_lock();
+       tb_id = vrf_dev_table_rcu(dev);
+       rcu_read_unlock();
+
+       return tb_id;
+}
+
+/* called with rtnl */
+static inline int vrf_dev_table_rtnl(const struct net_device *dev)
+{
+       int tb_id = 0;
+
+       if (dev) {
+               struct net_vrf_dev *vrf_ptr;
+
+               vrf_ptr = rtnl_dereference(dev->vrf_ptr);
+               if (vrf_ptr)
+                       tb_id = vrf_ptr->tb_id;
+       }
+       return tb_id;
+}
+
+/* caller has already checked netif_is_vrf(dev) */
+static inline struct rtable *vrf_dev_get_rth(const struct net_device *dev)
+{
+       struct rtable *rth = ERR_PTR(-ENETUNREACH);
+       struct net_vrf *vrf = netdev_priv(dev);
+
+       if (vrf) {
+               rth = vrf->rth;
+               atomic_inc(&rth->dst.__refcnt);
+       }
+       return rth;
+}
+
+#else
+static inline int vrf_master_ifindex_rcu(const struct net_device *dev)
+{
+       return 0;
+}
+
+static inline int vrf_dev_table_rcu(const struct net_device *dev)
+{
+       return 0;
+}
+
+static inline int vrf_dev_table(const struct net_device *dev)
+{
+       return 0;
+}
+
+static inline int vrf_dev_table_rtnl(const struct net_device *dev)
+{
+       return 0;
+}
+
+static inline struct rtable *vrf_dev_get_rth(const struct net_device *dev)
+{
+       return ERR_PTR(-ENETUNREACH);
+}
+#endif
+
+#endif /* __LINUX_NET_VRF_H */
index d450be36add200bdd5c9d8d92aafc0d8c34845aa..313c305fd1ad47c862fa0c4da2c44c7a496c47af 100644 (file)
@@ -341,6 +341,15 @@ enum macvlan_macaddr_mode {
 
 #define MACVLAN_FLAG_NOPROMISC 1
 
+/* VRF section */
+enum {
+       IFLA_VRF_UNSPEC,
+       IFLA_VRF_TABLE,
+       __IFLA_VRF_MAX
+};
+
+#define IFLA_VRF_MAX (__IFLA_VRF_MAX - 1)
+
 /* IPVLAN section */
 enum {
        IFLA_IPVLAN_UNSPEC,
index cc4e498a0ccf390115c2f6f0306248650f4a2c35..c8b855882fa522011405cf43209a7671bd3272c7 100644 (file)
 #ifdef CONFIG_IP_MROUTE
 #include <linux/mroute.h>
 #endif
+#include <net/vrf.h>
 
 
 /* The inetsw table contains everything that inet_create needs to
@@ -427,6 +428,7 @@ int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
        struct net *net = sock_net(sk);
        unsigned short snum;
        int chk_addr_ret;
+       int tb_id = RT_TABLE_LOCAL;
        int err;
 
        /* If the socket has its own bind function then use it. (RAW) */
@@ -448,7 +450,16 @@ int inet_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
                        goto out;
        }
 
-       chk_addr_ret = inet_addr_type(net, addr->sin_addr.s_addr);
+       if (sk->sk_bound_dev_if) {
+               struct net_device *dev;
+
+               rcu_read_lock();
+               dev = dev_get_by_index_rcu(net, sk->sk_bound_dev_if);
+               if (dev)
+                       tb_id = vrf_dev_table_rcu(dev) ? : tb_id;
+               rcu_read_unlock();
+       }
+       chk_addr_ret = inet_addr_type_table(net, addr->sin_addr.s_addr, tb_id);
 
        /* Not specified by any standard per-se, however it breaks too
         * many applications when removed.  It is unfortunate since
index 34a308573f4b0e7bcae0ad9ffa5e45b19c72c1d1..30409b75e92503cca0daacc48010a63936c6aa20 100644 (file)
@@ -233,7 +233,7 @@ static int arp_constructor(struct neighbour *neigh)
                return -EINVAL;
        }
 
-       neigh->type = inet_addr_type(dev_net(dev), addr);
+       neigh->type = inet_addr_type_dev_table(dev_net(dev), dev, addr);
 
        parms = in_dev->arp_parms;
        __neigh_parms_put(neigh->parms);
@@ -343,7 +343,7 @@ static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
        switch (IN_DEV_ARP_ANNOUNCE(in_dev)) {
        default:
        case 0:         /* By default announce any local IP */
-               if (skb && inet_addr_type(dev_net(dev),
+               if (skb && inet_addr_type_dev_table(dev_net(dev), dev,
                                          ip_hdr(skb)->saddr) == RTN_LOCAL)
                        saddr = ip_hdr(skb)->saddr;
                break;
@@ -351,7 +351,8 @@ static void arp_solicit(struct neighbour *neigh, struct sk_buff *skb)
                if (!skb)
                        break;
                saddr = ip_hdr(skb)->saddr;
-               if (inet_addr_type(dev_net(dev), saddr) == RTN_LOCAL) {
+               if (inet_addr_type_dev_table(dev_net(dev), dev,
+                                            saddr) == RTN_LOCAL) {
                        /* saddr should be known to target */
                        if (inet_addr_onlink(in_dev, target, saddr))
                                break;
@@ -751,7 +752,7 @@ static int arp_process(struct sock *sk, struct sk_buff *skb)
        /* Special case: IPv4 duplicate address detection packet (RFC2131) */
        if (sip == 0) {
                if (arp->ar_op == htons(ARPOP_REQUEST) &&
-                   inet_addr_type(net, tip) == RTN_LOCAL &&
+                   inet_addr_type_dev_table(net, dev, tip) == RTN_LOCAL &&
                    !arp_ignore(in_dev, sip, tip))
                        arp_send(ARPOP_REPLY, ETH_P_ARP, sip, dev, tip, sha,
                                 dev->dev_addr, sha);
@@ -811,16 +812,18 @@ static int arp_process(struct sock *sk, struct sk_buff *skb)
        n = __neigh_lookup(&arp_tbl, &sip, dev, 0);
 
        if (IN_DEV_ARP_ACCEPT(in_dev)) {
+               unsigned int addr_type = inet_addr_type_dev_table(net, dev, sip);
+
                /* Unsolicited ARP is not accepted by default.
                   It is possible, that this option should be enabled for some
                   devices (strip is candidate)
                 */
                is_garp = arp->ar_op == htons(ARPOP_REQUEST) && tip == sip &&
-                         inet_addr_type(net, sip) == RTN_UNICAST;
+                         addr_type == RTN_UNICAST;
 
                if (!n &&
                    ((arp->ar_op == htons(ARPOP_REPLY)  &&
-                     inet_addr_type(net, sip) == RTN_UNICAST) || is_garp))
+                               addr_type == RTN_UNICAST) || is_garp))
                        n = __neigh_lookup(&arp_tbl, &sip, dev, 1);
        }
 
index 6b98de0d79498d575a44d6c20bd3abb5a38ea75c..7fa277176c33ba95008cf1627e47ee3b2bb417e6 100644 (file)
@@ -45,6 +45,7 @@
 #include <net/ip_fib.h>
 #include <net/rtnetlink.h>
 #include <net/xfrm.h>
+#include <net/vrf.h>
 
 #ifndef CONFIG_IP_MULTIPLE_TABLES
 
@@ -211,12 +212,12 @@ void fib_flush_external(struct net *net)
  */
 static inline unsigned int __inet_dev_addr_type(struct net *net,
                                                const struct net_device *dev,
-                                               __be32 addr)
+                                               __be32 addr, int tb_id)
 {
        struct flowi4           fl4 = { .daddr = addr };
        struct fib_result       res;
        unsigned int ret = RTN_BROADCAST;
-       struct fib_table *local_table;
+       struct fib_table *table;
 
        if (ipv4_is_zeronet(addr) || ipv4_is_lbcast(addr))
                return RTN_BROADCAST;
@@ -225,10 +226,10 @@ static inline unsigned int __inet_dev_addr_type(struct net *net,
 
        rcu_read_lock();
 
-       local_table = fib_get_table(net, RT_TABLE_LOCAL);
-       if (local_table) {
+       table = fib_get_table(net, tb_id);
+       if (table) {
                ret = RTN_UNICAST;
-               if (!fib_table_lookup(local_table, &fl4, &res, FIB_LOOKUP_NOREF)) {
+               if (!fib_table_lookup(table, &fl4, &res, FIB_LOOKUP_NOREF)) {
                        if (!dev || dev == res.fi->fib_dev)
                                ret = res.type;
                }
@@ -238,19 +239,40 @@ static inline unsigned int __inet_dev_addr_type(struct net *net,
        return ret;
 }
 
+unsigned int inet_addr_type_table(struct net *net, __be32 addr, int tb_id)
+{
+       return __inet_dev_addr_type(net, NULL, addr, tb_id);
+}
+EXPORT_SYMBOL(inet_addr_type_table);
+
 unsigned int inet_addr_type(struct net *net, __be32 addr)
 {
-       return __inet_dev_addr_type(net, NULL, addr);
+       return __inet_dev_addr_type(net, NULL, addr, RT_TABLE_LOCAL);
 }
 EXPORT_SYMBOL(inet_addr_type);
 
 unsigned int inet_dev_addr_type(struct net *net, const struct net_device *dev,
                                __be32 addr)
 {
-       return __inet_dev_addr_type(net, dev, addr);
+       int rt_table = vrf_dev_table(dev) ? : RT_TABLE_LOCAL;
+
+       return __inet_dev_addr_type(net, dev, addr, rt_table);
 }
 EXPORT_SYMBOL(inet_dev_addr_type);
 
+/* inet_addr_type with dev == NULL but using the table from a dev
+ * if one is associated
+ */
+unsigned int inet_addr_type_dev_table(struct net *net,
+                                     const struct net_device *dev,
+                                     __be32 addr)
+{
+       int rt_table = vrf_dev_table(dev) ? : RT_TABLE_LOCAL;
+
+       return __inet_dev_addr_type(net, NULL, addr, rt_table);
+}
+EXPORT_SYMBOL(inet_addr_type_dev_table);
+
 __be32 fib_compute_spec_dst(struct sk_buff *skb)
 {
        struct net_device *dev = skb->dev;
@@ -309,7 +331,9 @@ static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
        bool dev_match;
 
        fl4.flowi4_oif = 0;
-       fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX;
+       fl4.flowi4_iif = vrf_master_ifindex_rcu(dev);
+       if (!fl4.flowi4_iif)
+               fl4.flowi4_iif = oif ? : LOOPBACK_IFINDEX;
        fl4.daddr = src;
        fl4.saddr = dst;
        fl4.flowi4_tos = tos;
@@ -339,6 +363,9 @@ static int __fib_validate_source(struct sk_buff *skb, __be32 src, __be32 dst,
                if (nh->nh_dev == dev) {
                        dev_match = true;
                        break;
+               } else if (vrf_master_ifindex_rcu(nh->nh_dev) == dev->ifindex) {
+                       dev_match = true;
+                       break;
                }
        }
 #else
@@ -496,9 +523,12 @@ static int rtentry_to_fib_config(struct net *net, int cmd, struct rtentry *rt,
 
        addr = sk_extract_addr(&rt->rt_gateway);
        if (rt->rt_gateway.sa_family == AF_INET && addr) {
+               unsigned int addr_type;
+
                cfg->fc_gw = addr;
+               addr_type = inet_addr_type_table(net, addr, cfg->fc_table);
                if (rt->rt_flags & RTF_GATEWAY &&
-                   inet_addr_type(net, addr) == RTN_UNICAST)
+                   addr_type == RTN_UNICAST)
                        cfg->fc_scope = RT_SCOPE_UNIVERSE;
        }
 
@@ -770,6 +800,7 @@ out:
 static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifaddr *ifa)
 {
        struct net *net = dev_net(ifa->ifa_dev->dev);
+       int tb_id = vrf_dev_table_rtnl(ifa->ifa_dev->dev);
        struct fib_table *tb;
        struct fib_config cfg = {
                .fc_protocol = RTPROT_KERNEL,
@@ -784,11 +815,10 @@ static void fib_magic(int cmd, int type, __be32 dst, int dst_len, struct in_ifad
                },
        };
 
-       if (type == RTN_UNICAST)
-               tb = fib_new_table(net, RT_TABLE_MAIN);
-       else
-               tb = fib_new_table(net, RT_TABLE_LOCAL);
+       if (!tb_id)
+               tb_id = (type == RTN_UNICAST) ? RT_TABLE_MAIN : RT_TABLE_LOCAL;
 
+       tb = fib_new_table(net, tb_id);
        if (!tb)
                return;
 
@@ -970,11 +1000,14 @@ void fib_del_ifaddr(struct in_ifaddr *ifa, struct in_ifaddr *iprim)
                        fib_magic(RTM_DELROUTE, RTN_BROADCAST, any, 32, prim);
        }
        if (!(ok & LOCAL_OK)) {
+               unsigned int addr_type;
+
                fib_magic(RTM_DELROUTE, RTN_LOCAL, ifa->ifa_local, 32, prim);
 
                /* Check, that this local address finally disappeared. */
-               if (gone &&
-                   inet_addr_type(dev_net(dev), ifa->ifa_local) != RTN_LOCAL) {
+               addr_type = inet_addr_type_dev_table(dev_net(dev), dev,
+                                                    ifa->ifa_local);
+               if (gone && addr_type != RTN_LOCAL) {
                        /* And the last, but not the least thing.
                         * We must flush stray FIB entries.
                         *
index 558e196bae0f5a10a6bc81102c657db00b726ad1..b7f1d20a96154b0295ece902def96d637622affe 100644 (file)
@@ -670,16 +670,18 @@ static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
                struct fib_result res;
 
                if (nh->nh_flags & RTNH_F_ONLINK) {
+                       unsigned int addr_type;
 
                        if (cfg->fc_scope >= RT_SCOPE_LINK)
                                return -EINVAL;
-                       if (inet_addr_type(net, nh->nh_gw) != RTN_UNICAST)
-                               return -EINVAL;
                        dev = __dev_get_by_index(net, nh->nh_oif);
                        if (!dev)
                                return -ENODEV;
                        if (!(dev->flags & IFF_UP))
                                return -ENETDOWN;
+                       addr_type = inet_addr_type_dev_table(net, dev, nh->nh_gw);
+                       if (addr_type != RTN_UNICAST)
+                               return -EINVAL;
                        if (!netif_carrier_ok(dev))
                                nh->nh_flags |= RTNH_F_LINKDOWN;
                        nh->nh_dev = dev;
@@ -689,6 +691,7 @@ static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
                }
                rcu_read_lock();
                {
+                       struct fib_table *tbl = NULL;
                        struct flowi4 fl4 = {
                                .daddr = nh->nh_gw,
                                .flowi4_scope = cfg->fc_scope + 1,
@@ -699,8 +702,16 @@ static int fib_check_nh(struct fib_config *cfg, struct fib_info *fi,
                        /* It is not necessary, but requires a bit of thinking */
                        if (fl4.flowi4_scope < RT_SCOPE_LINK)
                                fl4.flowi4_scope = RT_SCOPE_LINK;
-                       err = fib_lookup(net, &fl4, &res,
-                                        FIB_LOOKUP_IGNORE_LINKSTATE);
+
+                       if (cfg->fc_table)
+                               tbl = fib_get_table(net, cfg->fc_table);
+
+                       if (tbl)
+                               err = fib_table_lookup(tbl, &fl4, &res,
+                                                  FIB_LOOKUP_IGNORE_LINKSTATE);
+                       else
+                               err = fib_lookup(net, &fl4, &res,
+                                                FIB_LOOKUP_IGNORE_LINKSTATE);
                        if (err) {
                                rcu_read_unlock();
                                return err;
@@ -836,6 +847,23 @@ __be32 fib_info_update_nh_saddr(struct net *net, struct fib_nh *nh)
        return nh->nh_saddr;
 }
 
+static bool fib_valid_prefsrc(struct fib_config *cfg, __be32 fib_prefsrc)
+{
+       if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
+           fib_prefsrc != cfg->fc_dst) {
+               int tb_id = cfg->fc_table;
+
+               if (tb_id == RT_TABLE_MAIN)
+                       tb_id = RT_TABLE_LOCAL;
+
+               if (inet_addr_type_table(cfg->fc_nlinfo.nl_net,
+                                        fib_prefsrc, tb_id) != RTN_LOCAL) {
+                       return false;
+               }
+       }
+       return true;
+}
+
 struct fib_info *fib_create_info(struct fib_config *cfg)
 {
        int err;
@@ -1031,12 +1059,8 @@ struct fib_info *fib_create_info(struct fib_config *cfg)
                        fi->fib_flags |= RTNH_F_LINKDOWN;
        }
 
-       if (fi->fib_prefsrc) {
-               if (cfg->fc_type != RTN_LOCAL || !cfg->fc_dst ||
-                   fi->fib_prefsrc != cfg->fc_dst)
-                       if (inet_addr_type(net, fi->fib_prefsrc) != RTN_LOCAL)
-                               goto err_inval;
-       }
+       if (fi->fib_prefsrc && !fib_valid_prefsrc(cfg, fi->fib_prefsrc))
+               goto err_inval;
 
        change_nexthops(fi) {
                fib_info_update_nh_saddr(net, nexthop_nh);
index 37c4bb89a7082bbe36b40d928f7fd1d95bfe8252..1243c79cb5b0178052cae82e7a53728950f302e3 100644 (file)
@@ -1423,8 +1423,11 @@ found:
                            nh->nh_flags & RTNH_F_LINKDOWN &&
                            !(fib_flags & FIB_LOOKUP_IGNORE_LINKSTATE))
                                continue;
-                       if (flp->flowi4_oif && flp->flowi4_oif != nh->nh_oif)
-                               continue;
+                       if (!(flp->flowi4_flags & FLOWI_FLAG_VRFSRC)) {
+                               if (flp->flowi4_oif &&
+                                   flp->flowi4_oif != nh->nh_oif)
+                                       continue;
+                       }
 
                        if (!(fib_flags & FIB_LOOKUP_NOREF))
                                atomic_inc(&fi->fib_clntref);
index c0556f1e4bf09233970c8d5c3fd68afa9a78489f..c6f1ce149ffb868af6c31cfc86207f13dad16238 100644 (file)
@@ -96,6 +96,7 @@
 #include <net/xfrm.h>
 #include <net/inet_common.h>
 #include <net/ip_fib.h>
+#include <net/vrf.h>
 
 /*
  *     Build xmit assembly blocks
@@ -425,6 +426,7 @@ static void icmp_reply(struct icmp_bxm *icmp_param, struct sk_buff *skb)
        fl4.flowi4_mark = mark;
        fl4.flowi4_tos = RT_TOS(ip_hdr(skb)->tos);
        fl4.flowi4_proto = IPPROTO_ICMP;
+       fl4.flowi4_oif = vrf_master_ifindex_rcu(skb->dev) ? : skb->dev->ifindex;
        security_skb_classify_flow(skb, flowi4_to_flowi(&fl4));
        rt = ip_route_output_key(net, &fl4);
        if (IS_ERR(rt))
@@ -458,6 +460,8 @@ static struct rtable *icmp_route_lookup(struct net *net,
        fl4->flowi4_proto = IPPROTO_ICMP;
        fl4->fl4_icmp_type = type;
        fl4->fl4_icmp_code = code;
+       fl4->flowi4_oif = vrf_master_ifindex_rcu(skb_in->dev) ? : skb_in->dev->ifindex;
+
        security_skb_classify_flow(skb_in, flowi4_to_flowi(fl4));
        rt = __ip_route_output_key(net, fl4);
        if (IS_ERR(rt))
@@ -480,7 +484,8 @@ static struct rtable *icmp_route_lookup(struct net *net,
        if (err)
                goto relookup_failed;
 
-       if (inet_addr_type(net, fl4_dec.saddr) == RTN_LOCAL) {
+       if (inet_addr_type_dev_table(net, skb_in->dev,
+                                    fl4_dec.saddr) == RTN_LOCAL) {
                rt2 = __ip_route_output_key(net, &fl4_dec);
                if (IS_ERR(rt2))
                        err = PTR_ERR(rt2);
@@ -829,7 +834,7 @@ static bool icmp_unreach(struct sk_buff *skb)
         */
 
        if (!net->ipv4.sysctl_icmp_ignore_bogus_error_responses &&
-           inet_addr_type(net, iph->daddr) == RTN_BROADCAST) {
+           inet_addr_type_dev_table(net, skb->dev, iph->daddr) == RTN_BROADCAST) {
                net_warn_ratelimited("%pI4 sent an invalid ICMP type %u, code %u error to a broadcast: %pI4 on %s\n",
                                     &ip_hdr(skb)->saddr,
                                     icmph->type, icmph->code,
index d96722ae89796ef27ec725a695b60fbce4c47fbc..15762e758861b8d7101996a8faef749e2e8c7c3e 100644 (file)
@@ -48,6 +48,7 @@
 #include <linux/inet.h>
 #include <linux/netfilter_ipv4.h>
 #include <net/inet_ecn.h>
+#include <net/vrf.h>
 
 /* NOTE. Logic of IP defragmentation is parallel to corresponding IPv6
  * code now. If you change something here, _PLEASE_ update ipv6/reassembly.c
@@ -77,6 +78,7 @@ struct ipq {
        u8              ecn; /* RFC3168 support */
        u16             max_df_size; /* largest frag with DF set seen */
        int             iif;
+       int             vif;   /* VRF device index */
        unsigned int    rid;
        struct inet_peer *peer;
 };
@@ -99,6 +101,7 @@ static int ip_frag_reasm(struct ipq *qp, struct sk_buff *prev,
 struct ip4_create_arg {
        struct iphdr *iph;
        u32 user;
+       int vif;
 };
 
 static unsigned int ipqhashfn(__be16 id, __be32 saddr, __be32 daddr, u8 prot)
@@ -127,7 +130,8 @@ static bool ip4_frag_match(const struct inet_frag_queue *q, const void *a)
                qp->saddr == arg->iph->saddr &&
                qp->daddr == arg->iph->daddr &&
                qp->protocol == arg->iph->protocol &&
-               qp->user == arg->user;
+               qp->user == arg->user &&
+               qp->vif == arg->vif;
 }
 
 static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
@@ -144,6 +148,7 @@ static void ip4_frag_init(struct inet_frag_queue *q, const void *a)
        qp->ecn = ip4_frag_ecn(arg->iph->tos);
        qp->saddr = arg->iph->saddr;
        qp->daddr = arg->iph->daddr;
+       qp->vif = arg->vif;
        qp->user = arg->user;
        qp->peer = sysctl_ipfrag_max_dist ?
                inet_getpeer_v4(net->ipv4.peers, arg->iph->saddr, 1) : NULL;
@@ -244,7 +249,8 @@ out:
 /* Find the correct entry in the "incomplete datagrams" queue for
  * this IP datagram, and create new one, if nothing is found.
  */
-static struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
+static struct ipq *ip_find(struct net *net, struct iphdr *iph,
+                          u32 user, int vif)
 {
        struct inet_frag_queue *q;
        struct ip4_create_arg arg;
@@ -252,6 +258,7 @@ static struct ipq *ip_find(struct net *net, struct iphdr *iph, u32 user)
 
        arg.iph = iph;
        arg.user = user;
+       arg.vif = vif;
 
        hash = ipqhashfn(iph->id, iph->saddr, iph->daddr, iph->protocol);
 
@@ -648,14 +655,15 @@ out_fail:
 /* Process an incoming IP datagram fragment. */
 int ip_defrag(struct sk_buff *skb, u32 user)
 {
+       struct net_device *dev = skb->dev ? : skb_dst(skb)->dev;
+       int vif = vrf_master_ifindex_rcu(dev);
+       struct net *net = dev_net(dev);
        struct ipq *qp;
-       struct net *net;
 
-       net = skb->dev ? dev_net(skb->dev) : dev_net(skb_dst(skb)->dev);
        IP_INC_STATS_BH(net, IPSTATS_MIB_REASMREQDS);
 
        /* Lookup (or create) queue header */
-       qp = ip_find(net, ip_hdr(skb), user);
+       qp = ip_find(net, ip_hdr(skb), user, vif);
        if (qp) {
                int ret;
 
index 6bf89a6312bc1c71da41ad0a1ebdbf819504367d..0138fada0951b17b175be0b215c248fc7224dc5a 100644 (file)
@@ -1542,6 +1542,7 @@ void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
        struct net *net = sock_net(sk);
        struct sk_buff *nskb;
        int err;
+       int oif;
 
        if (__ip_options_echo(&replyopts.opt.opt, skb, sopt))
                return;
@@ -1559,7 +1560,11 @@ void ip_send_unicast_reply(struct sock *sk, struct sk_buff *skb,
                        daddr = replyopts.opt.opt.faddr;
        }
 
-       flowi4_init_output(&fl4, arg->bound_dev_if,
+       oif = arg->bound_dev_if;
+       if (!oif && netif_index_is_vrf(net, skb->skb_iif))
+               oif = skb->skb_iif;
+
+       flowi4_init_output(&fl4, oif,
                           IP4_REPLY_MARK(net, skb->mark),
                           RT_TOS(arg->tos),
                           RT_SCOPE_UNIVERSE, ip_hdr(skb)->protocol,
index 18fd7c9095c706c13240b4624aa291ead0b0171a..2c89d294b669803323d2b48f32aeb7726d00b235 100644 (file)
 #endif
 #include <net/secure_seq.h>
 #include <net/ip_tunnels.h>
+#include <net/vrf.h>
 
 #define RT_FL_TOS(oldflp4) \
        ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
@@ -1726,7 +1727,7 @@ static int ip_route_input_slow(struct sk_buff *skb, __be32 daddr, __be32 saddr,
         *      Now we are ready to route packet.
         */
        fl4.flowi4_oif = 0;
-       fl4.flowi4_iif = dev->ifindex;
+       fl4.flowi4_iif = vrf_master_ifindex_rcu(dev) ? : dev->ifindex;
        fl4.flowi4_mark = skb->mark;
        fl4.flowi4_tos = tos;
        fl4.flowi4_scope = RT_SCOPE_UNIVERSE;
@@ -2130,6 +2131,11 @@ struct rtable *__ip_route_output_key(struct net *net, struct flowi4 *fl4)
                                fl4->saddr = inet_select_addr(dev_out, 0,
                                                              RT_SCOPE_HOST);
                }
+               if (netif_is_vrf(dev_out) &&
+                   !(fl4->flowi4_flags & FLOWI_FLAG_VRFSRC)) {
+                       rth = vrf_dev_get_rth(dev_out);
+                       goto out;
+               }
        }
 
        if (!fl4->daddr) {
index 1b8c5ba7d5f732ea2220ada929049c5c7cfd5e83..c0a15e7f359fe54e4edcffca5d59acb418dad116 100644 (file)
@@ -1013,11 +1013,31 @@ int udp_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
 
        if (!rt) {
                struct net *net = sock_net(sk);
+               __u8 flow_flags = inet_sk_flowi_flags(sk);
 
                fl4 = &fl4_stack;
+
+               /* unconnected socket. If output device is enslaved to a VRF
+                * device lookup source address from VRF table. This mimics
+                * behavior of ip_route_connect{_init}.
+                */
+               if (netif_index_is_vrf(net, ipc.oif)) {
+                       flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
+                                          RT_SCOPE_UNIVERSE, sk->sk_protocol,
+                                          (flow_flags | FLOWI_FLAG_VRFSRC),
+                                          faddr, saddr, dport,
+                                          inet->inet_sport);
+
+                       rt = ip_route_output_flow(net, fl4, sk);
+                       if (!IS_ERR(rt)) {
+                               saddr = fl4->saddr;
+                               ip_rt_put(rt);
+                       }
+               }
+
                flowi4_init_output(fl4, ipc.oif, sk->sk_mark, tos,
                                   RT_SCOPE_UNIVERSE, sk->sk_protocol,
-                                  inet_sk_flowi_flags(sk),
+                                  flow_flags,
                                   faddr, saddr, dport, inet->inet_sport);
 
                security_sk_classify_flow(sk, flowi4_to_flowi(fl4));