David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1 | /* |
| 2 | * vrf.c: device driver to encapsulate a VRF space |
| 3 | * |
| 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> |
| 7 | * |
| 8 | * Based on dummy, team and ipvlan drivers |
| 9 | * |
| 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. |
| 14 | */ |
| 15 | |
| 16 | #include <linux/module.h> |
| 17 | #include <linux/kernel.h> |
| 18 | #include <linux/netdevice.h> |
| 19 | #include <linux/etherdevice.h> |
| 20 | #include <linux/ip.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> |
| 28 | |
| 29 | #include <linux/inetdevice.h> |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 30 | #include <net/arp.h> |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 31 | #include <net/ip.h> |
| 32 | #include <net/ip_fib.h> |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 33 | #include <net/ip6_fib.h> |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 34 | #include <net/ip6_route.h> |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 35 | #include <net/route.h> |
| 36 | #include <net/addrconf.h> |
David Ahern | ee15ee5 | 2015-09-29 20:07:12 -0700 | [diff] [blame] | 37 | #include <net/l3mdev.h> |
David Ahern | 1aa6c4f | 2016-06-08 10:55:40 -0700 | [diff] [blame] | 38 | #include <net/fib_rules.h> |
David Ahern | 386ed38 | 2017-06-08 11:31:11 -0600 | [diff] [blame] | 39 | #include <net/netns/generic.h> |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 40 | |
| 41 | #define DRV_NAME "vrf" |
| 42 | #define DRV_VERSION "1.0" |
| 43 | |
David Ahern | 1aa6c4f | 2016-06-08 10:55:40 -0700 | [diff] [blame] | 44 | #define FIB_RULE_PREF 1000 /* default preference for FIB rules */ |
David Ahern | 386ed38 | 2017-06-08 11:31:11 -0600 | [diff] [blame] | 45 | |
| 46 | static unsigned int vrf_net_id; |
David Ahern | 1aa6c4f | 2016-06-08 10:55:40 -0700 | [diff] [blame] | 47 | |
David Ahern | ec53951 | 2015-09-29 20:07:17 -0700 | [diff] [blame] | 48 | struct net_vrf { |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 49 | struct rtable __rcu *rth; |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 50 | struct rtable __rcu *rth_local; |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 51 | struct rt6_info __rcu *rt6; |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 52 | struct rt6_info __rcu *rt6_local; |
David Ahern | ec53951 | 2015-09-29 20:07:17 -0700 | [diff] [blame] | 53 | u32 tb_id; |
| 54 | }; |
| 55 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 56 | struct pcpu_dstats { |
| 57 | u64 tx_pkts; |
| 58 | u64 tx_bytes; |
| 59 | u64 tx_drps; |
| 60 | u64 rx_pkts; |
| 61 | u64 rx_bytes; |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 62 | u64 rx_drps; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 63 | struct u64_stats_sync syncp; |
| 64 | }; |
| 65 | |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 66 | static void vrf_rx_stats(struct net_device *dev, int len) |
| 67 | { |
| 68 | struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats); |
| 69 | |
| 70 | u64_stats_update_begin(&dstats->syncp); |
| 71 | dstats->rx_pkts++; |
| 72 | dstats->rx_bytes += len; |
| 73 | u64_stats_update_end(&dstats->syncp); |
| 74 | } |
| 75 | |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame] | 76 | static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb) |
| 77 | { |
| 78 | vrf_dev->stats.tx_errors++; |
| 79 | kfree_skb(skb); |
| 80 | } |
| 81 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 82 | static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev, |
| 83 | struct rtnl_link_stats64 *stats) |
| 84 | { |
| 85 | int i; |
| 86 | |
| 87 | for_each_possible_cpu(i) { |
| 88 | const struct pcpu_dstats *dstats; |
| 89 | u64 tbytes, tpkts, tdrops, rbytes, rpkts; |
| 90 | unsigned int start; |
| 91 | |
| 92 | dstats = per_cpu_ptr(dev->dstats, i); |
| 93 | do { |
| 94 | start = u64_stats_fetch_begin_irq(&dstats->syncp); |
| 95 | tbytes = dstats->tx_bytes; |
| 96 | tpkts = dstats->tx_pkts; |
| 97 | tdrops = dstats->tx_drps; |
| 98 | rbytes = dstats->rx_bytes; |
| 99 | rpkts = dstats->rx_pkts; |
| 100 | } while (u64_stats_fetch_retry_irq(&dstats->syncp, start)); |
| 101 | stats->tx_bytes += tbytes; |
| 102 | stats->tx_packets += tpkts; |
| 103 | stats->tx_dropped += tdrops; |
| 104 | stats->rx_bytes += rbytes; |
| 105 | stats->rx_packets += rpkts; |
| 106 | } |
| 107 | return stats; |
| 108 | } |
| 109 | |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 110 | /* Local traffic destined to local address. Reinsert the packet to rx |
| 111 | * path, similar to loopback handling. |
| 112 | */ |
| 113 | static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev, |
| 114 | struct dst_entry *dst) |
| 115 | { |
| 116 | int len = skb->len; |
| 117 | |
| 118 | skb_orphan(skb); |
| 119 | |
| 120 | skb_dst_set(skb, dst); |
| 121 | skb_dst_force(skb); |
| 122 | |
| 123 | /* set pkt_type to avoid skb hitting packet taps twice - |
| 124 | * once on Tx and again in Rx processing |
| 125 | */ |
| 126 | skb->pkt_type = PACKET_LOOPBACK; |
| 127 | |
| 128 | skb->protocol = eth_type_trans(skb, dev); |
| 129 | |
| 130 | if (likely(netif_rx(skb) == NET_RX_SUCCESS)) |
| 131 | vrf_rx_stats(dev, len); |
| 132 | else |
| 133 | this_cpu_inc(dev->dstats->rx_drps); |
| 134 | |
| 135 | return NETDEV_TX_OK; |
| 136 | } |
| 137 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 138 | #if IS_ENABLED(CONFIG_IPV6) |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 139 | static int vrf_ip6_local_out(struct net *net, struct sock *sk, |
| 140 | struct sk_buff *skb) |
| 141 | { |
| 142 | int err; |
| 143 | |
| 144 | err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, |
| 145 | sk, skb, NULL, skb_dst(skb)->dev, dst_output); |
| 146 | |
| 147 | if (likely(err == 1)) |
| 148 | err = dst_output(net, sk, skb); |
| 149 | |
| 150 | return err; |
| 151 | } |
| 152 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 153 | static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb, |
| 154 | struct net_device *dev) |
| 155 | { |
| 156 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 157 | struct net *net = dev_net(skb->dev); |
| 158 | struct flowi6 fl6 = { |
| 159 | /* needed to match OIF rule */ |
| 160 | .flowi6_oif = dev->ifindex, |
| 161 | .flowi6_iif = LOOPBACK_IFINDEX, |
| 162 | .daddr = iph->daddr, |
| 163 | .saddr = iph->saddr, |
| 164 | .flowlabel = ip6_flowinfo(iph), |
| 165 | .flowi6_mark = skb->mark, |
| 166 | .flowi6_proto = iph->nexthdr, |
David Ahern | c71ad3d | 2016-09-10 12:10:02 -0700 | [diff] [blame] | 167 | .flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF, |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 168 | }; |
| 169 | int ret = NET_XMIT_DROP; |
| 170 | struct dst_entry *dst; |
| 171 | struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst; |
| 172 | |
| 173 | dst = ip6_route_output(net, NULL, &fl6); |
| 174 | if (dst == dst_null) |
| 175 | goto err; |
| 176 | |
| 177 | skb_dst_drop(skb); |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 178 | |
| 179 | /* if dst.dev is loopback or the VRF device again this is locally |
| 180 | * originated traffic destined to a local address. Short circuit |
| 181 | * to Rx path using our local dst |
| 182 | */ |
| 183 | if (dst->dev == net->loopback_dev || dst->dev == dev) { |
| 184 | struct net_vrf *vrf = netdev_priv(dev); |
| 185 | struct rt6_info *rt6_local; |
| 186 | |
| 187 | /* release looked up dst and use cached local dst */ |
| 188 | dst_release(dst); |
| 189 | |
| 190 | rcu_read_lock(); |
| 191 | |
| 192 | rt6_local = rcu_dereference(vrf->rt6_local); |
| 193 | if (unlikely(!rt6_local)) { |
| 194 | rcu_read_unlock(); |
| 195 | goto err; |
| 196 | } |
| 197 | |
| 198 | /* Ordering issue: cached local dst is created on newlink |
| 199 | * before the IPv6 initialization. Using the local dst |
| 200 | * requires rt6i_idev to be set so make sure it is. |
| 201 | */ |
| 202 | if (unlikely(!rt6_local->rt6i_idev)) { |
| 203 | rt6_local->rt6i_idev = in6_dev_get(dev); |
| 204 | if (!rt6_local->rt6i_idev) { |
| 205 | rcu_read_unlock(); |
| 206 | goto err; |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | dst = &rt6_local->dst; |
| 211 | dst_hold(dst); |
| 212 | |
| 213 | rcu_read_unlock(); |
| 214 | |
| 215 | return vrf_local_xmit(skb, dev, &rt6_local->dst); |
| 216 | } |
| 217 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 218 | skb_dst_set(skb, dst); |
| 219 | |
David Ahern | 911a66f | 2016-06-06 20:50:38 -0700 | [diff] [blame] | 220 | /* strip the ethernet header added for pass through VRF device */ |
| 221 | __skb_pull(skb, skb_network_offset(skb)); |
| 222 | |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 223 | ret = vrf_ip6_local_out(net, skb->sk, skb); |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 224 | if (unlikely(net_xmit_eval(ret))) |
| 225 | dev->stats.tx_errors++; |
| 226 | else |
| 227 | ret = NET_XMIT_SUCCESS; |
| 228 | |
| 229 | return ret; |
| 230 | err: |
| 231 | vrf_tx_error(dev, skb); |
| 232 | return NET_XMIT_DROP; |
| 233 | } |
| 234 | #else |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 235 | static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb, |
| 236 | struct net_device *dev) |
| 237 | { |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame] | 238 | vrf_tx_error(dev, skb); |
| 239 | return NET_XMIT_DROP; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 240 | } |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 241 | #endif |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 242 | |
David Ahern | ebfc102 | 2016-09-10 12:09:55 -0700 | [diff] [blame] | 243 | /* based on ip_local_out; can't use it b/c the dst is switched pointing to us */ |
| 244 | static int vrf_ip_local_out(struct net *net, struct sock *sk, |
| 245 | struct sk_buff *skb) |
| 246 | { |
| 247 | int err; |
| 248 | |
| 249 | err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk, |
| 250 | skb, NULL, skb_dst(skb)->dev, dst_output); |
| 251 | if (likely(err == 1)) |
| 252 | err = dst_output(net, sk, skb); |
| 253 | |
| 254 | return err; |
| 255 | } |
| 256 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 257 | static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb, |
| 258 | struct net_device *vrf_dev) |
| 259 | { |
| 260 | struct iphdr *ip4h = ip_hdr(skb); |
| 261 | int ret = NET_XMIT_DROP; |
| 262 | struct flowi4 fl4 = { |
| 263 | /* needed to match OIF rule */ |
| 264 | .flowi4_oif = vrf_dev->ifindex, |
| 265 | .flowi4_iif = LOOPBACK_IFINDEX, |
| 266 | .flowi4_tos = RT_TOS(ip4h->tos), |
David Ahern | c71ad3d | 2016-09-10 12:10:02 -0700 | [diff] [blame] | 267 | .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_SKIP_NH_OIF, |
David Ahern | 7b7a5a8 | 2017-01-10 14:37:35 -0800 | [diff] [blame] | 268 | .flowi4_proto = ip4h->protocol, |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 269 | .daddr = ip4h->daddr, |
David Ahern | 7b7a5a8 | 2017-01-10 14:37:35 -0800 | [diff] [blame] | 270 | .saddr = ip4h->saddr, |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 271 | }; |
David Ahern | 911a66f | 2016-06-06 20:50:38 -0700 | [diff] [blame] | 272 | struct net *net = dev_net(vrf_dev); |
| 273 | struct rtable *rt; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 274 | |
David Ahern | 911a66f | 2016-06-06 20:50:38 -0700 | [diff] [blame] | 275 | rt = ip_route_output_flow(net, &fl4, NULL); |
| 276 | if (IS_ERR(rt)) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 277 | goto err; |
| 278 | |
David Ahern | 911a66f | 2016-06-06 20:50:38 -0700 | [diff] [blame] | 279 | if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) { |
| 280 | ip_rt_put(rt); |
| 281 | goto err; |
| 282 | } |
| 283 | |
| 284 | skb_dst_drop(skb); |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 285 | |
| 286 | /* if dst.dev is loopback or the VRF device again this is locally |
| 287 | * originated traffic destined to a local address. Short circuit |
| 288 | * to Rx path using our local dst |
| 289 | */ |
| 290 | if (rt->dst.dev == net->loopback_dev || rt->dst.dev == vrf_dev) { |
| 291 | struct net_vrf *vrf = netdev_priv(vrf_dev); |
| 292 | struct rtable *rth_local; |
| 293 | struct dst_entry *dst = NULL; |
| 294 | |
| 295 | ip_rt_put(rt); |
| 296 | |
| 297 | rcu_read_lock(); |
| 298 | |
| 299 | rth_local = rcu_dereference(vrf->rth_local); |
| 300 | if (likely(rth_local)) { |
| 301 | dst = &rth_local->dst; |
| 302 | dst_hold(dst); |
| 303 | } |
| 304 | |
| 305 | rcu_read_unlock(); |
| 306 | |
| 307 | if (unlikely(!dst)) |
| 308 | goto err; |
| 309 | |
| 310 | return vrf_local_xmit(skb, vrf_dev, dst); |
| 311 | } |
| 312 | |
David Ahern | 911a66f | 2016-06-06 20:50:38 -0700 | [diff] [blame] | 313 | skb_dst_set(skb, &rt->dst); |
| 314 | |
| 315 | /* strip the ethernet header added for pass through VRF device */ |
| 316 | __skb_pull(skb, skb_network_offset(skb)); |
| 317 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 318 | if (!ip4h->saddr) { |
| 319 | ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0, |
| 320 | RT_SCOPE_LINK); |
| 321 | } |
| 322 | |
David Ahern | ebfc102 | 2016-09-10 12:09:55 -0700 | [diff] [blame] | 323 | ret = vrf_ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 324 | if (unlikely(net_xmit_eval(ret))) |
| 325 | vrf_dev->stats.tx_errors++; |
| 326 | else |
| 327 | ret = NET_XMIT_SUCCESS; |
| 328 | |
| 329 | out: |
| 330 | return ret; |
| 331 | err: |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame] | 332 | vrf_tx_error(vrf_dev, skb); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 333 | goto out; |
| 334 | } |
| 335 | |
| 336 | static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev) |
| 337 | { |
| 338 | switch (skb->protocol) { |
| 339 | case htons(ETH_P_IP): |
| 340 | return vrf_process_v4_outbound(skb, dev); |
| 341 | case htons(ETH_P_IPV6): |
| 342 | return vrf_process_v6_outbound(skb, dev); |
| 343 | default: |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame] | 344 | vrf_tx_error(dev, skb); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 345 | return NET_XMIT_DROP; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev) |
| 350 | { |
David Ahern | db6e779 | 2017-03-06 08:53:04 -0800 | [diff] [blame] | 351 | int len = skb->len; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 352 | netdev_tx_t ret = is_ip_tx_frame(skb, dev); |
| 353 | |
| 354 | if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { |
| 355 | struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats); |
| 356 | |
| 357 | u64_stats_update_begin(&dstats->syncp); |
| 358 | dstats->tx_pkts++; |
David Ahern | db6e779 | 2017-03-06 08:53:04 -0800 | [diff] [blame] | 359 | dstats->tx_bytes += len; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 360 | u64_stats_update_end(&dstats->syncp); |
| 361 | } else { |
| 362 | this_cpu_inc(dev->dstats->tx_drps); |
| 363 | } |
| 364 | |
| 365 | return ret; |
| 366 | } |
| 367 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 368 | #if IS_ENABLED(CONFIG_IPV6) |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 369 | /* modelled after ip6_finish_output2 */ |
| 370 | static int vrf_finish_output6(struct net *net, struct sock *sk, |
| 371 | struct sk_buff *skb) |
| 372 | { |
| 373 | struct dst_entry *dst = skb_dst(skb); |
| 374 | struct net_device *dev = dst->dev; |
| 375 | struct neighbour *neigh; |
| 376 | struct in6_addr *nexthop; |
| 377 | int ret; |
| 378 | |
David Ahern | 8b8fbe5 | 2016-12-14 14:31:11 -0800 | [diff] [blame] | 379 | nf_reset(skb); |
| 380 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 381 | skb->protocol = htons(ETH_P_IPV6); |
| 382 | skb->dev = dev; |
| 383 | |
| 384 | rcu_read_lock_bh(); |
| 385 | nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr); |
| 386 | neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop); |
| 387 | if (unlikely(!neigh)) |
| 388 | neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false); |
| 389 | if (!IS_ERR(neigh)) { |
| 390 | ret = dst_neigh_output(dst, neigh, skb); |
| 391 | rcu_read_unlock_bh(); |
| 392 | return ret; |
| 393 | } |
| 394 | rcu_read_unlock_bh(); |
| 395 | |
| 396 | IP6_INC_STATS(dev_net(dst->dev), |
| 397 | ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES); |
| 398 | kfree_skb(skb); |
| 399 | return -EINVAL; |
| 400 | } |
| 401 | |
| 402 | /* modelled after ip6_output */ |
| 403 | static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 404 | { |
| 405 | return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, |
| 406 | net, sk, skb, NULL, skb_dst(skb)->dev, |
| 407 | vrf_finish_output6, |
| 408 | !(IP6CB(skb)->flags & IP6SKB_REROUTED)); |
| 409 | } |
| 410 | |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 411 | /* set dst on skb to send packet to us via dev_xmit path. Allows |
| 412 | * packet to go through device based features such as qdisc, netfilter |
| 413 | * hooks and packet sockets with skb->dev set to vrf device. |
| 414 | */ |
| 415 | static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev, |
| 416 | struct sock *sk, |
| 417 | struct sk_buff *skb) |
| 418 | { |
| 419 | struct net_vrf *vrf = netdev_priv(vrf_dev); |
| 420 | struct dst_entry *dst = NULL; |
| 421 | struct rt6_info *rt6; |
| 422 | |
| 423 | /* don't divert link scope packets */ |
| 424 | if (rt6_need_strict(&ipv6_hdr(skb)->daddr)) |
| 425 | return skb; |
| 426 | |
| 427 | rcu_read_lock(); |
| 428 | |
| 429 | rt6 = rcu_dereference(vrf->rt6); |
| 430 | if (likely(rt6)) { |
| 431 | dst = &rt6->dst; |
| 432 | dst_hold(dst); |
| 433 | } |
| 434 | |
| 435 | rcu_read_unlock(); |
| 436 | |
| 437 | if (unlikely(!dst)) { |
| 438 | vrf_tx_error(vrf_dev, skb); |
| 439 | return NULL; |
| 440 | } |
| 441 | |
| 442 | skb_dst_drop(skb); |
| 443 | skb_dst_set(skb, dst); |
| 444 | |
| 445 | return skb; |
| 446 | } |
| 447 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 448 | /* holding rtnl */ |
David Ahern | 810e530 | 2016-06-14 11:37:21 -0700 | [diff] [blame] | 449 | static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf) |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 450 | { |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 451 | struct rt6_info *rt6 = rtnl_dereference(vrf->rt6); |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 452 | struct rt6_info *rt6_local = rtnl_dereference(vrf->rt6_local); |
David Ahern | 810e530 | 2016-06-14 11:37:21 -0700 | [diff] [blame] | 453 | struct net *net = dev_net(dev); |
| 454 | struct dst_entry *dst; |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 455 | |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 456 | RCU_INIT_POINTER(vrf->rt6, NULL); |
| 457 | RCU_INIT_POINTER(vrf->rt6_local, NULL); |
| 458 | synchronize_rcu(); |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 459 | |
David Ahern | 810e530 | 2016-06-14 11:37:21 -0700 | [diff] [blame] | 460 | /* move dev in dst's to loopback so this VRF device can be deleted |
| 461 | * - based on dst_ifdown |
| 462 | */ |
| 463 | if (rt6) { |
| 464 | dst = &rt6->dst; |
| 465 | dev_put(dst->dev); |
| 466 | dst->dev = net->loopback_dev; |
| 467 | dev_hold(dst->dev); |
| 468 | dst_release(dst); |
| 469 | } |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 470 | |
| 471 | if (rt6_local) { |
David Ahern | 4e7c821 | 2017-03-17 16:07:11 -0700 | [diff] [blame] | 472 | if (rt6_local->rt6i_idev) { |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 473 | in6_dev_put(rt6_local->rt6i_idev); |
David Ahern | 4e7c821 | 2017-03-17 16:07:11 -0700 | [diff] [blame] | 474 | rt6_local->rt6i_idev = NULL; |
| 475 | } |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 476 | |
David Ahern | 810e530 | 2016-06-14 11:37:21 -0700 | [diff] [blame] | 477 | dst = &rt6_local->dst; |
| 478 | dev_put(dst->dev); |
| 479 | dst->dev = net->loopback_dev; |
| 480 | dev_hold(dst->dev); |
| 481 | dst_release(dst); |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 482 | } |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static int vrf_rt6_create(struct net_device *dev) |
| 486 | { |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 487 | int flags = DST_HOST | DST_NOPOLICY | DST_NOXFRM | DST_NOCACHE; |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 488 | struct net_vrf *vrf = netdev_priv(dev); |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 489 | struct net *net = dev_net(dev); |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 490 | struct fib6_table *rt6i_table; |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 491 | struct rt6_info *rt6, *rt6_local; |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 492 | int rc = -ENOMEM; |
| 493 | |
David Ahern | e434863 | 2016-06-09 10:21:00 -0700 | [diff] [blame] | 494 | /* IPv6 can be CONFIG enabled and then disabled runtime */ |
| 495 | if (!ipv6_mod_enabled()) |
| 496 | return 0; |
| 497 | |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 498 | rt6i_table = fib6_new_table(net, vrf->tb_id); |
| 499 | if (!rt6i_table) |
| 500 | goto out; |
| 501 | |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 502 | /* create a dst for routing packets out a VRF device */ |
| 503 | rt6 = ip6_dst_alloc(net, dev, flags); |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 504 | if (!rt6) |
| 505 | goto out; |
| 506 | |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 507 | dst_hold(&rt6->dst); |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 508 | |
| 509 | rt6->rt6i_table = rt6i_table; |
| 510 | rt6->dst.output = vrf_output6; |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 511 | |
| 512 | /* create a dst for local routing - packets sent locally |
| 513 | * to local address via the VRF device as a loopback |
| 514 | */ |
| 515 | rt6_local = ip6_dst_alloc(net, dev, flags); |
| 516 | if (!rt6_local) { |
| 517 | dst_release(&rt6->dst); |
| 518 | goto out; |
| 519 | } |
| 520 | |
| 521 | dst_hold(&rt6_local->dst); |
| 522 | |
| 523 | rt6_local->rt6i_idev = in6_dev_get(dev); |
| 524 | rt6_local->rt6i_flags = RTF_UP | RTF_NONEXTHOP | RTF_LOCAL; |
| 525 | rt6_local->rt6i_table = rt6i_table; |
| 526 | rt6_local->dst.input = ip6_input; |
| 527 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 528 | rcu_assign_pointer(vrf->rt6, rt6); |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 529 | rcu_assign_pointer(vrf->rt6_local, rt6_local); |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 530 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 531 | rc = 0; |
| 532 | out: |
| 533 | return rc; |
| 534 | } |
| 535 | #else |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 536 | static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev, |
| 537 | struct sock *sk, |
| 538 | struct sk_buff *skb) |
| 539 | { |
| 540 | return skb; |
| 541 | } |
| 542 | |
David Ahern | 810e530 | 2016-06-14 11:37:21 -0700 | [diff] [blame] | 543 | static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf) |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 544 | { |
| 545 | } |
| 546 | |
| 547 | static int vrf_rt6_create(struct net_device *dev) |
| 548 | { |
| 549 | return 0; |
| 550 | } |
| 551 | #endif |
| 552 | |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 553 | /* modelled after ip_finish_output2 */ |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 554 | static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 555 | { |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 556 | struct dst_entry *dst = skb_dst(skb); |
| 557 | struct rtable *rt = (struct rtable *)dst; |
| 558 | struct net_device *dev = dst->dev; |
| 559 | unsigned int hh_len = LL_RESERVED_SPACE(dev); |
| 560 | struct neighbour *neigh; |
| 561 | u32 nexthop; |
| 562 | int ret = -EINVAL; |
| 563 | |
David Ahern | 8b8fbe5 | 2016-12-14 14:31:11 -0800 | [diff] [blame] | 564 | nf_reset(skb); |
| 565 | |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 566 | /* Be paranoid, rather than too clever. */ |
| 567 | if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { |
| 568 | struct sk_buff *skb2; |
| 569 | |
| 570 | skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev)); |
| 571 | if (!skb2) { |
| 572 | ret = -ENOMEM; |
| 573 | goto err; |
| 574 | } |
| 575 | if (skb->sk) |
| 576 | skb_set_owner_w(skb2, skb->sk); |
| 577 | |
| 578 | consume_skb(skb); |
| 579 | skb = skb2; |
| 580 | } |
| 581 | |
| 582 | rcu_read_lock_bh(); |
| 583 | |
| 584 | nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr); |
| 585 | neigh = __ipv4_neigh_lookup_noref(dev, nexthop); |
| 586 | if (unlikely(!neigh)) |
| 587 | neigh = __neigh_create(&arp_tbl, &nexthop, dev, false); |
David Ahern | 89cd658 | 2018-03-29 12:49:52 -0700 | [diff] [blame^] | 588 | if (!IS_ERR(neigh)) { |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 589 | ret = dst_neigh_output(dst, neigh, skb); |
David Ahern | 89cd658 | 2018-03-29 12:49:52 -0700 | [diff] [blame^] | 590 | rcu_read_unlock_bh(); |
| 591 | return ret; |
| 592 | } |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 593 | |
| 594 | rcu_read_unlock_bh(); |
| 595 | err: |
David Ahern | 89cd658 | 2018-03-29 12:49:52 -0700 | [diff] [blame^] | 596 | vrf_tx_error(skb->dev, skb); |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 597 | return ret; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 598 | } |
| 599 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 600 | static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 601 | { |
| 602 | struct net_device *dev = skb_dst(skb)->dev; |
| 603 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 604 | IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 605 | |
| 606 | skb->dev = dev; |
| 607 | skb->protocol = htons(ETH_P_IP); |
| 608 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 609 | return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, |
| 610 | net, sk, skb, NULL, dev, |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 611 | vrf_finish_output, |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 612 | !(IPCB(skb)->flags & IPSKB_REROUTED)); |
| 613 | } |
| 614 | |
David Ahern | ebfc102 | 2016-09-10 12:09:55 -0700 | [diff] [blame] | 615 | /* set dst on skb to send packet to us via dev_xmit path. Allows |
| 616 | * packet to go through device based features such as qdisc, netfilter |
| 617 | * hooks and packet sockets with skb->dev set to vrf device. |
| 618 | */ |
| 619 | static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev, |
| 620 | struct sock *sk, |
| 621 | struct sk_buff *skb) |
| 622 | { |
| 623 | struct net_vrf *vrf = netdev_priv(vrf_dev); |
| 624 | struct dst_entry *dst = NULL; |
| 625 | struct rtable *rth; |
| 626 | |
| 627 | rcu_read_lock(); |
| 628 | |
| 629 | rth = rcu_dereference(vrf->rth); |
| 630 | if (likely(rth)) { |
| 631 | dst = &rth->dst; |
| 632 | dst_hold(dst); |
| 633 | } |
| 634 | |
| 635 | rcu_read_unlock(); |
| 636 | |
| 637 | if (unlikely(!dst)) { |
| 638 | vrf_tx_error(vrf_dev, skb); |
| 639 | return NULL; |
| 640 | } |
| 641 | |
| 642 | skb_dst_drop(skb); |
| 643 | skb_dst_set(skb, dst); |
| 644 | |
| 645 | return skb; |
| 646 | } |
| 647 | |
| 648 | /* called with rcu lock held */ |
| 649 | static struct sk_buff *vrf_l3_out(struct net_device *vrf_dev, |
| 650 | struct sock *sk, |
| 651 | struct sk_buff *skb, |
| 652 | u16 proto) |
| 653 | { |
| 654 | switch (proto) { |
| 655 | case AF_INET: |
| 656 | return vrf_ip_out(vrf_dev, sk, skb); |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 657 | case AF_INET6: |
| 658 | return vrf_ip6_out(vrf_dev, sk, skb); |
David Ahern | ebfc102 | 2016-09-10 12:09:55 -0700 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | return skb; |
| 662 | } |
| 663 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 664 | /* holding rtnl */ |
David Ahern | 810e530 | 2016-06-14 11:37:21 -0700 | [diff] [blame] | 665 | static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 666 | { |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 667 | struct rtable *rth = rtnl_dereference(vrf->rth); |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 668 | struct rtable *rth_local = rtnl_dereference(vrf->rth_local); |
David Ahern | 810e530 | 2016-06-14 11:37:21 -0700 | [diff] [blame] | 669 | struct net *net = dev_net(dev); |
| 670 | struct dst_entry *dst; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 671 | |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 672 | RCU_INIT_POINTER(vrf->rth, NULL); |
| 673 | RCU_INIT_POINTER(vrf->rth_local, NULL); |
| 674 | synchronize_rcu(); |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 675 | |
David Ahern | 810e530 | 2016-06-14 11:37:21 -0700 | [diff] [blame] | 676 | /* move dev in dst's to loopback so this VRF device can be deleted |
| 677 | * - based on dst_ifdown |
| 678 | */ |
| 679 | if (rth) { |
| 680 | dst = &rth->dst; |
| 681 | dev_put(dst->dev); |
| 682 | dst->dev = net->loopback_dev; |
| 683 | dev_hold(dst->dev); |
| 684 | dst_release(dst); |
| 685 | } |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 686 | |
David Ahern | 810e530 | 2016-06-14 11:37:21 -0700 | [diff] [blame] | 687 | if (rth_local) { |
| 688 | dst = &rth_local->dst; |
| 689 | dev_put(dst->dev); |
| 690 | dst->dev = net->loopback_dev; |
| 691 | dev_hold(dst->dev); |
| 692 | dst_release(dst); |
| 693 | } |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 694 | } |
| 695 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 696 | static int vrf_rtable_create(struct net_device *dev) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 697 | { |
David Ahern | b7503e0 | 2015-09-02 13:58:35 -0700 | [diff] [blame] | 698 | struct net_vrf *vrf = netdev_priv(dev); |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 699 | struct rtable *rth, *rth_local; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 700 | |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 701 | if (!fib_new_table(dev_net(dev), vrf->tb_id)) |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 702 | return -ENOMEM; |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 703 | |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 704 | /* create a dst for routing packets out through a VRF device */ |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 705 | rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1, 1, 0); |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 706 | if (!rth) |
| 707 | return -ENOMEM; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 708 | |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 709 | /* create a dst for local ingress routing - packets sent locally |
| 710 | * to local address via the VRF device as a loopback |
| 711 | */ |
| 712 | rth_local = rt_dst_alloc(dev, RTCF_LOCAL, RTN_LOCAL, 1, 1, 0); |
| 713 | if (!rth_local) { |
| 714 | dst_release(&rth->dst); |
| 715 | return -ENOMEM; |
| 716 | } |
| 717 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 718 | rth->dst.output = vrf_output; |
| 719 | rth->rt_table_id = vrf->tb_id; |
| 720 | |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 721 | rth_local->rt_table_id = vrf->tb_id; |
| 722 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 723 | rcu_assign_pointer(vrf->rth, rth); |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 724 | rcu_assign_pointer(vrf->rth_local, rth_local); |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 725 | |
| 726 | return 0; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 727 | } |
| 728 | |
| 729 | /**************************** device handling ********************/ |
| 730 | |
| 731 | /* cycle interface to flush neighbor cache and move routes across tables */ |
| 732 | static void cycle_netdev(struct net_device *dev) |
| 733 | { |
| 734 | unsigned int flags = dev->flags; |
| 735 | int ret; |
| 736 | |
| 737 | if (!netif_running(dev)) |
| 738 | return; |
| 739 | |
| 740 | ret = dev_change_flags(dev, flags & ~IFF_UP); |
| 741 | if (ret >= 0) |
| 742 | ret = dev_change_flags(dev, flags); |
| 743 | |
| 744 | if (ret < 0) { |
| 745 | netdev_err(dev, |
| 746 | "Failed to cycle device %s; route tables might be wrong!\n", |
| 747 | dev->name); |
| 748 | } |
| 749 | } |
| 750 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 751 | static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev) |
| 752 | { |
Nikolay Aleksandrov | bad5316 | 2015-11-24 14:29:16 +0100 | [diff] [blame] | 753 | int ret; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 754 | |
Jiri Pirko | 29bf24a | 2015-12-03 12:12:11 +0100 | [diff] [blame] | 755 | ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 756 | if (ret < 0) |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 757 | return ret; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 758 | |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 759 | port_dev->priv_flags |= IFF_L3MDEV_SLAVE; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 760 | cycle_netdev(port_dev); |
| 761 | |
| 762 | return 0; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev) |
| 766 | { |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 767 | if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev)) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 768 | return -EINVAL; |
| 769 | |
| 770 | return do_vrf_add_slave(dev, port_dev); |
| 771 | } |
| 772 | |
| 773 | /* inverse of do_vrf_add_slave */ |
| 774 | static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev) |
| 775 | { |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 776 | netdev_upper_dev_unlink(port_dev, dev); |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 777 | port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 778 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 779 | cycle_netdev(port_dev); |
| 780 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 781 | return 0; |
| 782 | } |
| 783 | |
| 784 | static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev) |
| 785 | { |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 786 | return do_vrf_del_slave(dev, port_dev); |
| 787 | } |
| 788 | |
| 789 | static void vrf_dev_uninit(struct net_device *dev) |
| 790 | { |
| 791 | struct net_vrf *vrf = netdev_priv(dev); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 792 | |
David Ahern | 810e530 | 2016-06-14 11:37:21 -0700 | [diff] [blame] | 793 | vrf_rtable_release(dev, vrf); |
| 794 | vrf_rt6_release(dev, vrf); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 795 | |
Nikolay Aleksandrov | 3a4a27d | 2015-08-18 20:28:03 +0300 | [diff] [blame] | 796 | free_percpu(dev->dstats); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 797 | dev->dstats = NULL; |
| 798 | } |
| 799 | |
| 800 | static int vrf_dev_init(struct net_device *dev) |
| 801 | { |
| 802 | struct net_vrf *vrf = netdev_priv(dev); |
| 803 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 804 | dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats); |
| 805 | if (!dev->dstats) |
| 806 | goto out_nomem; |
| 807 | |
| 808 | /* create the default dst which points back to us */ |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 809 | if (vrf_rtable_create(dev) != 0) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 810 | goto out_stats; |
| 811 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 812 | if (vrf_rt6_create(dev) != 0) |
| 813 | goto out_rth; |
| 814 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 815 | dev->flags = IFF_MASTER | IFF_NOARP; |
| 816 | |
David Ahern | b87ab6b | 2016-06-01 21:16:39 -0700 | [diff] [blame] | 817 | /* MTU is irrelevant for VRF device; set to 64k similar to lo */ |
| 818 | dev->mtu = 64 * 1024; |
| 819 | |
| 820 | /* similarly, oper state is irrelevant; set to up to avoid confusion */ |
| 821 | dev->operstate = IF_OPER_UP; |
Eric Dumazet | 78e7a2a | 2016-06-09 07:45:13 -0700 | [diff] [blame] | 822 | netdev_lockdep_set_classes(dev); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 823 | return 0; |
| 824 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 825 | out_rth: |
David Ahern | 810e530 | 2016-06-14 11:37:21 -0700 | [diff] [blame] | 826 | vrf_rtable_release(dev, vrf); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 827 | out_stats: |
| 828 | free_percpu(dev->dstats); |
| 829 | dev->dstats = NULL; |
| 830 | out_nomem: |
| 831 | return -ENOMEM; |
| 832 | } |
| 833 | |
| 834 | static const struct net_device_ops vrf_netdev_ops = { |
| 835 | .ndo_init = vrf_dev_init, |
| 836 | .ndo_uninit = vrf_dev_uninit, |
| 837 | .ndo_start_xmit = vrf_xmit, |
| 838 | .ndo_get_stats64 = vrf_get_stats64, |
| 839 | .ndo_add_slave = vrf_add_slave, |
| 840 | .ndo_del_slave = vrf_del_slave, |
| 841 | }; |
| 842 | |
David Ahern | ee15ee5 | 2015-09-29 20:07:12 -0700 | [diff] [blame] | 843 | static u32 vrf_fib_table(const struct net_device *dev) |
| 844 | { |
| 845 | struct net_vrf *vrf = netdev_priv(dev); |
| 846 | |
| 847 | return vrf->tb_id; |
| 848 | } |
| 849 | |
David Ahern | 73e20b7 | 2016-07-04 18:47:41 -0700 | [diff] [blame] | 850 | static int vrf_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 851 | { |
Gao Feng | 8f1f08b | 2017-05-09 18:27:33 +0800 | [diff] [blame] | 852 | kfree_skb(skb); |
David Ahern | 73e20b7 | 2016-07-04 18:47:41 -0700 | [diff] [blame] | 853 | return 0; |
| 854 | } |
| 855 | |
| 856 | static struct sk_buff *vrf_rcv_nfhook(u8 pf, unsigned int hook, |
| 857 | struct sk_buff *skb, |
| 858 | struct net_device *dev) |
| 859 | { |
| 860 | struct net *net = dev_net(dev); |
| 861 | |
Gao Feng | 8f1f08b | 2017-05-09 18:27:33 +0800 | [diff] [blame] | 862 | if (nf_hook(pf, hook, net, NULL, skb, dev, NULL, vrf_rcv_finish) != 1) |
David Ahern | 73e20b7 | 2016-07-04 18:47:41 -0700 | [diff] [blame] | 863 | skb = NULL; /* kfree_skb(skb) handled by nf code */ |
| 864 | |
| 865 | return skb; |
| 866 | } |
| 867 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 868 | #if IS_ENABLED(CONFIG_IPV6) |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 869 | /* neighbor handling is done with actual device; do not want |
| 870 | * to flip skb->dev for those ndisc packets. This really fails |
| 871 | * for multiple next protocols (e.g., NEXTHDR_HOP). But it is |
| 872 | * a start. |
| 873 | */ |
| 874 | static bool ipv6_ndisc_frame(const struct sk_buff *skb) |
| 875 | { |
| 876 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 877 | bool rc = false; |
| 878 | |
| 879 | if (iph->nexthdr == NEXTHDR_ICMP) { |
| 880 | const struct icmp6hdr *icmph; |
| 881 | struct icmp6hdr _icmph; |
| 882 | |
| 883 | icmph = skb_header_pointer(skb, sizeof(*iph), |
| 884 | sizeof(_icmph), &_icmph); |
| 885 | if (!icmph) |
| 886 | goto out; |
| 887 | |
| 888 | switch (icmph->icmp6_type) { |
| 889 | case NDISC_ROUTER_SOLICITATION: |
| 890 | case NDISC_ROUTER_ADVERTISEMENT: |
| 891 | case NDISC_NEIGHBOUR_SOLICITATION: |
| 892 | case NDISC_NEIGHBOUR_ADVERTISEMENT: |
| 893 | case NDISC_REDIRECT: |
| 894 | rc = true; |
| 895 | break; |
| 896 | } |
| 897 | } |
| 898 | |
| 899 | out: |
| 900 | return rc; |
| 901 | } |
| 902 | |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 903 | static struct rt6_info *vrf_ip6_route_lookup(struct net *net, |
| 904 | const struct net_device *dev, |
| 905 | struct flowi6 *fl6, |
| 906 | int ifindex, |
| 907 | int flags) |
| 908 | { |
| 909 | struct net_vrf *vrf = netdev_priv(dev); |
| 910 | struct fib6_table *table = NULL; |
| 911 | struct rt6_info *rt6; |
| 912 | |
| 913 | rcu_read_lock(); |
| 914 | |
| 915 | /* fib6_table does not have a refcnt and can not be freed */ |
| 916 | rt6 = rcu_dereference(vrf->rt6); |
| 917 | if (likely(rt6)) |
| 918 | table = rt6->rt6i_table; |
| 919 | |
| 920 | rcu_read_unlock(); |
| 921 | |
| 922 | if (!table) |
| 923 | return NULL; |
| 924 | |
| 925 | return ip6_pol_route(net, table, ifindex, fl6, flags); |
| 926 | } |
| 927 | |
| 928 | static void vrf_ip6_input_dst(struct sk_buff *skb, struct net_device *vrf_dev, |
| 929 | int ifindex) |
| 930 | { |
| 931 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 932 | struct flowi6 fl6 = { |
| 933 | .daddr = iph->daddr, |
| 934 | .saddr = iph->saddr, |
| 935 | .flowlabel = ip6_flowinfo(iph), |
| 936 | .flowi6_mark = skb->mark, |
| 937 | .flowi6_proto = iph->nexthdr, |
| 938 | .flowi6_iif = ifindex, |
| 939 | }; |
| 940 | struct net *net = dev_net(vrf_dev); |
| 941 | struct rt6_info *rt6; |
| 942 | |
| 943 | rt6 = vrf_ip6_route_lookup(net, vrf_dev, &fl6, ifindex, |
| 944 | RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_IFACE); |
| 945 | if (unlikely(!rt6)) |
| 946 | return; |
| 947 | |
| 948 | if (unlikely(&rt6->dst == &net->ipv6.ip6_null_entry->dst)) |
| 949 | return; |
| 950 | |
| 951 | skb_dst_set(skb, &rt6->dst); |
| 952 | } |
| 953 | |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 954 | static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev, |
| 955 | struct sk_buff *skb) |
| 956 | { |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 957 | int orig_iif = skb->skb_iif; |
| 958 | bool need_strict; |
| 959 | |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 960 | /* loopback traffic; do not push through packet taps again. |
| 961 | * Reset pkt_type for upper layers to process skb |
| 962 | */ |
| 963 | if (skb->pkt_type == PACKET_LOOPBACK) { |
| 964 | skb->dev = vrf_dev; |
| 965 | skb->skb_iif = vrf_dev->ifindex; |
David Ahern | a04a480 | 2016-10-16 20:02:52 -0700 | [diff] [blame] | 966 | IP6CB(skb)->flags |= IP6SKB_L3SLAVE; |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 967 | skb->pkt_type = PACKET_HOST; |
| 968 | goto out; |
| 969 | } |
| 970 | |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 971 | /* if packet is NDISC or addressed to multicast or link-local |
| 972 | * then keep the ingress interface |
| 973 | */ |
| 974 | need_strict = rt6_need_strict(&ipv6_hdr(skb)->daddr); |
| 975 | if (!ipv6_ndisc_frame(skb) && !need_strict) { |
David Ahern | a4d205a | 2017-01-03 09:37:55 -0800 | [diff] [blame] | 976 | vrf_rx_stats(vrf_dev, skb->len); |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 977 | skb->dev = vrf_dev; |
| 978 | skb->skb_iif = vrf_dev->ifindex; |
| 979 | |
| 980 | skb_push(skb, skb->mac_len); |
| 981 | dev_queue_xmit_nit(skb, vrf_dev); |
| 982 | skb_pull(skb, skb->mac_len); |
| 983 | |
| 984 | IP6CB(skb)->flags |= IP6SKB_L3SLAVE; |
| 985 | } |
| 986 | |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 987 | if (need_strict) |
| 988 | vrf_ip6_input_dst(skb, vrf_dev, orig_iif); |
| 989 | |
David Ahern | 73e20b7 | 2016-07-04 18:47:41 -0700 | [diff] [blame] | 990 | skb = vrf_rcv_nfhook(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, vrf_dev); |
David Ahern | b4869aa | 2016-06-06 20:50:40 -0700 | [diff] [blame] | 991 | out: |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 992 | return skb; |
| 993 | } |
| 994 | |
| 995 | #else |
| 996 | static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev, |
| 997 | struct sk_buff *skb) |
| 998 | { |
| 999 | return skb; |
| 1000 | } |
| 1001 | #endif |
| 1002 | |
| 1003 | static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev, |
| 1004 | struct sk_buff *skb) |
| 1005 | { |
| 1006 | skb->dev = vrf_dev; |
| 1007 | skb->skb_iif = vrf_dev->ifindex; |
David Ahern | a04a480 | 2016-10-16 20:02:52 -0700 | [diff] [blame] | 1008 | IPCB(skb)->flags |= IPSKB_L3SLAVE; |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 1009 | |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 1010 | /* loopback traffic; do not push through packet taps again. |
| 1011 | * Reset pkt_type for upper layers to process skb |
| 1012 | */ |
| 1013 | if (skb->pkt_type == PACKET_LOOPBACK) { |
| 1014 | skb->pkt_type = PACKET_HOST; |
| 1015 | goto out; |
| 1016 | } |
| 1017 | |
David Ahern | a4d205a | 2017-01-03 09:37:55 -0800 | [diff] [blame] | 1018 | vrf_rx_stats(vrf_dev, skb->len); |
| 1019 | |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 1020 | skb_push(skb, skb->mac_len); |
| 1021 | dev_queue_xmit_nit(skb, vrf_dev); |
| 1022 | skb_pull(skb, skb->mac_len); |
| 1023 | |
David Ahern | 73e20b7 | 2016-07-04 18:47:41 -0700 | [diff] [blame] | 1024 | skb = vrf_rcv_nfhook(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, vrf_dev); |
David Ahern | afe80a4 | 2016-06-06 20:50:39 -0700 | [diff] [blame] | 1025 | out: |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 1026 | return skb; |
| 1027 | } |
| 1028 | |
| 1029 | /* called with rcu lock held */ |
| 1030 | static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev, |
| 1031 | struct sk_buff *skb, |
| 1032 | u16 proto) |
| 1033 | { |
| 1034 | switch (proto) { |
| 1035 | case AF_INET: |
| 1036 | return vrf_ip_rcv(vrf_dev, skb); |
| 1037 | case AF_INET6: |
| 1038 | return vrf_ip6_rcv(vrf_dev, skb); |
| 1039 | } |
| 1040 | |
| 1041 | return skb; |
| 1042 | } |
| 1043 | |
| 1044 | #if IS_ENABLED(CONFIG_IPV6) |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 1045 | /* send to link-local or multicast address via interface enslaved to |
| 1046 | * VRF device. Force lookup to VRF table without changing flow struct |
| 1047 | */ |
| 1048 | static struct dst_entry *vrf_link_scope_lookup(const struct net_device *dev, |
| 1049 | struct flowi6 *fl6) |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 1050 | { |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 1051 | struct net *net = dev_net(dev); |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 1052 | int flags = RT6_LOOKUP_F_IFACE; |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 1053 | struct dst_entry *dst = NULL; |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 1054 | struct rt6_info *rt; |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 1055 | |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 1056 | /* VRF device does not have a link-local address and |
| 1057 | * sending packets to link-local or mcast addresses over |
| 1058 | * a VRF device does not make sense |
| 1059 | */ |
| 1060 | if (fl6->flowi6_oif == dev->ifindex) { |
| 1061 | dst = &net->ipv6.ip6_null_entry->dst; |
| 1062 | dst_hold(dst); |
| 1063 | return dst; |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 1064 | } |
| 1065 | |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 1066 | if (!ipv6_addr_any(&fl6->saddr)) |
| 1067 | flags |= RT6_LOOKUP_F_HAS_SADDR; |
| 1068 | |
| 1069 | rt = vrf_ip6_route_lookup(net, dev, fl6, fl6->flowi6_oif, flags); |
| 1070 | if (rt) |
| 1071 | dst = &rt->dst; |
David Ahern | 9ff7438 | 2016-06-13 13:44:19 -0700 | [diff] [blame] | 1072 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 1073 | return dst; |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 1074 | } |
| 1075 | #endif |
| 1076 | |
David Ahern | ee15ee5 | 2015-09-29 20:07:12 -0700 | [diff] [blame] | 1077 | static const struct l3mdev_ops vrf_l3mdev_ops = { |
| 1078 | .l3mdev_fib_table = vrf_fib_table, |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 1079 | .l3mdev_l3_rcv = vrf_l3_rcv, |
David Ahern | ebfc102 | 2016-09-10 12:09:55 -0700 | [diff] [blame] | 1080 | .l3mdev_l3_out = vrf_l3_out, |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 1081 | #if IS_ENABLED(CONFIG_IPV6) |
David Ahern | 4c1feac | 2016-09-10 12:09:56 -0700 | [diff] [blame] | 1082 | .l3mdev_link_scope_lookup = vrf_link_scope_lookup, |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 1083 | #endif |
David Ahern | ee15ee5 | 2015-09-29 20:07:12 -0700 | [diff] [blame] | 1084 | }; |
| 1085 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1086 | static void vrf_get_drvinfo(struct net_device *dev, |
| 1087 | struct ethtool_drvinfo *info) |
| 1088 | { |
| 1089 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 1090 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); |
| 1091 | } |
| 1092 | |
| 1093 | static const struct ethtool_ops vrf_ethtool_ops = { |
| 1094 | .get_drvinfo = vrf_get_drvinfo, |
| 1095 | }; |
| 1096 | |
David Ahern | 1aa6c4f | 2016-06-08 10:55:40 -0700 | [diff] [blame] | 1097 | static inline size_t vrf_fib_rule_nl_size(void) |
| 1098 | { |
| 1099 | size_t sz; |
| 1100 | |
| 1101 | sz = NLMSG_ALIGN(sizeof(struct fib_rule_hdr)); |
| 1102 | sz += nla_total_size(sizeof(u8)); /* FRA_L3MDEV */ |
| 1103 | sz += nla_total_size(sizeof(u32)); /* FRA_PRIORITY */ |
| 1104 | |
| 1105 | return sz; |
| 1106 | } |
| 1107 | |
| 1108 | static int vrf_fib_rule(const struct net_device *dev, __u8 family, bool add_it) |
| 1109 | { |
| 1110 | struct fib_rule_hdr *frh; |
| 1111 | struct nlmsghdr *nlh; |
| 1112 | struct sk_buff *skb; |
| 1113 | int err; |
| 1114 | |
David Ahern | e434863 | 2016-06-09 10:21:00 -0700 | [diff] [blame] | 1115 | if (family == AF_INET6 && !ipv6_mod_enabled()) |
| 1116 | return 0; |
| 1117 | |
David Ahern | 1aa6c4f | 2016-06-08 10:55:40 -0700 | [diff] [blame] | 1118 | skb = nlmsg_new(vrf_fib_rule_nl_size(), GFP_KERNEL); |
| 1119 | if (!skb) |
| 1120 | return -ENOMEM; |
| 1121 | |
| 1122 | nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*frh), 0); |
| 1123 | if (!nlh) |
| 1124 | goto nla_put_failure; |
| 1125 | |
| 1126 | /* rule only needs to appear once */ |
David Ahern | b4580d6 | 2017-04-13 10:57:15 -0600 | [diff] [blame] | 1127 | nlh->nlmsg_flags |= NLM_F_EXCL; |
David Ahern | 1aa6c4f | 2016-06-08 10:55:40 -0700 | [diff] [blame] | 1128 | |
| 1129 | frh = nlmsg_data(nlh); |
| 1130 | memset(frh, 0, sizeof(*frh)); |
| 1131 | frh->family = family; |
| 1132 | frh->action = FR_ACT_TO_TBL; |
| 1133 | |
Jeff Barnhill | 58b21b0 | 2017-11-01 14:58:09 +0000 | [diff] [blame] | 1134 | if (nla_put_u8(skb, FRA_L3MDEV, 1)) |
David Ahern | 1aa6c4f | 2016-06-08 10:55:40 -0700 | [diff] [blame] | 1135 | goto nla_put_failure; |
| 1136 | |
| 1137 | if (nla_put_u32(skb, FRA_PRIORITY, FIB_RULE_PREF)) |
| 1138 | goto nla_put_failure; |
| 1139 | |
| 1140 | nlmsg_end(skb, nlh); |
| 1141 | |
| 1142 | /* fib_nl_{new,del}rule handling looks for net from skb->sk */ |
| 1143 | skb->sk = dev_net(dev)->rtnl; |
| 1144 | if (add_it) { |
| 1145 | err = fib_nl_newrule(skb, nlh); |
| 1146 | if (err == -EEXIST) |
| 1147 | err = 0; |
| 1148 | } else { |
| 1149 | err = fib_nl_delrule(skb, nlh); |
| 1150 | if (err == -ENOENT) |
| 1151 | err = 0; |
| 1152 | } |
| 1153 | nlmsg_free(skb); |
| 1154 | |
| 1155 | return err; |
| 1156 | |
| 1157 | nla_put_failure: |
| 1158 | nlmsg_free(skb); |
| 1159 | |
| 1160 | return -EMSGSIZE; |
| 1161 | } |
| 1162 | |
| 1163 | static int vrf_add_fib_rules(const struct net_device *dev) |
| 1164 | { |
| 1165 | int err; |
| 1166 | |
| 1167 | err = vrf_fib_rule(dev, AF_INET, true); |
| 1168 | if (err < 0) |
| 1169 | goto out_err; |
| 1170 | |
| 1171 | err = vrf_fib_rule(dev, AF_INET6, true); |
| 1172 | if (err < 0) |
| 1173 | goto ipv6_err; |
| 1174 | |
| 1175 | return 0; |
| 1176 | |
| 1177 | ipv6_err: |
| 1178 | vrf_fib_rule(dev, AF_INET, false); |
| 1179 | |
| 1180 | out_err: |
| 1181 | netdev_err(dev, "Failed to add FIB rules.\n"); |
| 1182 | return err; |
| 1183 | } |
| 1184 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1185 | static void vrf_setup(struct net_device *dev) |
| 1186 | { |
| 1187 | ether_setup(dev); |
| 1188 | |
| 1189 | /* Initialize the device structure. */ |
| 1190 | dev->netdev_ops = &vrf_netdev_ops; |
David Ahern | ee15ee5 | 2015-09-29 20:07:12 -0700 | [diff] [blame] | 1191 | dev->l3mdev_ops = &vrf_l3mdev_ops; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1192 | dev->ethtool_ops = &vrf_ethtool_ops; |
| 1193 | dev->destructor = free_netdev; |
| 1194 | |
| 1195 | /* Fill in device structure with ethernet-generic values. */ |
| 1196 | eth_hw_addr_random(dev); |
| 1197 | |
| 1198 | /* don't acquire vrf device's netif_tx_lock when transmitting */ |
| 1199 | dev->features |= NETIF_F_LLTX; |
| 1200 | |
| 1201 | /* don't allow vrf devices to change network namespaces. */ |
| 1202 | dev->features |= NETIF_F_NETNS_LOCAL; |
David Ahern | 7889681 | 2016-06-13 17:14:12 -0700 | [diff] [blame] | 1203 | |
| 1204 | /* does not make sense for a VLAN to be added to a vrf device */ |
| 1205 | dev->features |= NETIF_F_VLAN_CHALLENGED; |
| 1206 | |
| 1207 | /* enable offload features */ |
| 1208 | dev->features |= NETIF_F_GSO_SOFTWARE; |
| 1209 | dev->features |= NETIF_F_RXCSUM | NETIF_F_HW_CSUM; |
| 1210 | dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA; |
| 1211 | |
| 1212 | dev->hw_features = dev->features; |
| 1213 | dev->hw_enc_features = dev->features; |
| 1214 | |
| 1215 | /* default to no qdisc; user can add if desired */ |
| 1216 | dev->priv_flags |= IFF_NO_QUEUE; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1217 | } |
| 1218 | |
| 1219 | static int vrf_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 1220 | { |
| 1221 | if (tb[IFLA_ADDRESS]) { |
| 1222 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 1223 | return -EINVAL; |
| 1224 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 1225 | return -EADDRNOTAVAIL; |
| 1226 | } |
| 1227 | return 0; |
| 1228 | } |
| 1229 | |
| 1230 | static void vrf_dellink(struct net_device *dev, struct list_head *head) |
| 1231 | { |
Nikolay Aleksandrov | e6577f1 | 2017-07-06 15:24:40 +0300 | [diff] [blame] | 1232 | struct net_device *port_dev; |
| 1233 | struct list_head *iter; |
| 1234 | |
| 1235 | netdev_for_each_lower_dev(dev, port_dev, iter) |
| 1236 | vrf_del_slave(dev, port_dev); |
| 1237 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1238 | unregister_netdevice_queue(dev, head); |
| 1239 | } |
| 1240 | |
| 1241 | static int vrf_newlink(struct net *src_net, struct net_device *dev, |
| 1242 | struct nlattr *tb[], struct nlattr *data[]) |
| 1243 | { |
| 1244 | struct net_vrf *vrf = netdev_priv(dev); |
David Ahern | 386ed38 | 2017-06-08 11:31:11 -0600 | [diff] [blame] | 1245 | bool *add_fib_rules; |
| 1246 | struct net *net; |
David Ahern | 1aa6c4f | 2016-06-08 10:55:40 -0700 | [diff] [blame] | 1247 | int err; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1248 | |
| 1249 | if (!data || !data[IFLA_VRF_TABLE]) |
| 1250 | return -EINVAL; |
| 1251 | |
| 1252 | vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]); |
David Ahern | e425ed1 | 2017-01-10 15:22:25 -0800 | [diff] [blame] | 1253 | if (vrf->tb_id == RT_TABLE_UNSPEC) |
| 1254 | return -EINVAL; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1255 | |
David Ahern | 007979e | 2015-09-29 20:07:10 -0700 | [diff] [blame] | 1256 | dev->priv_flags |= IFF_L3MDEV_MASTER; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1257 | |
David Ahern | 1aa6c4f | 2016-06-08 10:55:40 -0700 | [diff] [blame] | 1258 | err = register_netdevice(dev); |
| 1259 | if (err) |
| 1260 | goto out; |
| 1261 | |
David Ahern | 386ed38 | 2017-06-08 11:31:11 -0600 | [diff] [blame] | 1262 | net = dev_net(dev); |
| 1263 | add_fib_rules = net_generic(net, vrf_net_id); |
| 1264 | if (*add_fib_rules) { |
David Ahern | 1aa6c4f | 2016-06-08 10:55:40 -0700 | [diff] [blame] | 1265 | err = vrf_add_fib_rules(dev); |
| 1266 | if (err) { |
| 1267 | unregister_netdevice(dev); |
| 1268 | goto out; |
| 1269 | } |
David Ahern | 386ed38 | 2017-06-08 11:31:11 -0600 | [diff] [blame] | 1270 | *add_fib_rules = false; |
David Ahern | 1aa6c4f | 2016-06-08 10:55:40 -0700 | [diff] [blame] | 1271 | } |
| 1272 | |
| 1273 | out: |
| 1274 | return err; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1275 | } |
| 1276 | |
| 1277 | static size_t vrf_nl_getsize(const struct net_device *dev) |
| 1278 | { |
| 1279 | return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */ |
| 1280 | } |
| 1281 | |
| 1282 | static int vrf_fillinfo(struct sk_buff *skb, |
| 1283 | const struct net_device *dev) |
| 1284 | { |
| 1285 | struct net_vrf *vrf = netdev_priv(dev); |
| 1286 | |
| 1287 | return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id); |
| 1288 | } |
| 1289 | |
David Ahern | 67eb033 | 2016-02-02 07:43:45 -0800 | [diff] [blame] | 1290 | static size_t vrf_get_slave_size(const struct net_device *bond_dev, |
| 1291 | const struct net_device *slave_dev) |
| 1292 | { |
| 1293 | return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */ |
| 1294 | } |
| 1295 | |
| 1296 | static int vrf_fill_slave_info(struct sk_buff *skb, |
| 1297 | const struct net_device *vrf_dev, |
| 1298 | const struct net_device *slave_dev) |
| 1299 | { |
| 1300 | struct net_vrf *vrf = netdev_priv(vrf_dev); |
| 1301 | |
| 1302 | if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id)) |
| 1303 | return -EMSGSIZE; |
| 1304 | |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1308 | static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = { |
| 1309 | [IFLA_VRF_TABLE] = { .type = NLA_U32 }, |
| 1310 | }; |
| 1311 | |
| 1312 | static struct rtnl_link_ops vrf_link_ops __read_mostly = { |
| 1313 | .kind = DRV_NAME, |
| 1314 | .priv_size = sizeof(struct net_vrf), |
| 1315 | |
| 1316 | .get_size = vrf_nl_getsize, |
| 1317 | .policy = vrf_nl_policy, |
| 1318 | .validate = vrf_validate, |
| 1319 | .fill_info = vrf_fillinfo, |
| 1320 | |
David Ahern | 67eb033 | 2016-02-02 07:43:45 -0800 | [diff] [blame] | 1321 | .get_slave_size = vrf_get_slave_size, |
| 1322 | .fill_slave_info = vrf_fill_slave_info, |
| 1323 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1324 | .newlink = vrf_newlink, |
| 1325 | .dellink = vrf_dellink, |
| 1326 | .setup = vrf_setup, |
| 1327 | .maxtype = IFLA_VRF_MAX, |
| 1328 | }; |
| 1329 | |
| 1330 | static int vrf_device_event(struct notifier_block *unused, |
| 1331 | unsigned long event, void *ptr) |
| 1332 | { |
| 1333 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 1334 | |
| 1335 | /* only care about unregister events to drop slave references */ |
| 1336 | if (event == NETDEV_UNREGISTER) { |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1337 | struct net_device *vrf_dev; |
| 1338 | |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 1339 | if (!netif_is_l3_slave(dev)) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1340 | goto out; |
| 1341 | |
Nikolay Aleksandrov | 58aa908 | 2015-08-18 20:28:04 +0300 | [diff] [blame] | 1342 | vrf_dev = netdev_master_upper_dev_get(dev); |
| 1343 | vrf_del_slave(vrf_dev, dev); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1344 | } |
| 1345 | out: |
| 1346 | return NOTIFY_DONE; |
| 1347 | } |
| 1348 | |
| 1349 | static struct notifier_block vrf_notifier_block __read_mostly = { |
| 1350 | .notifier_call = vrf_device_event, |
| 1351 | }; |
| 1352 | |
David Ahern | 386ed38 | 2017-06-08 11:31:11 -0600 | [diff] [blame] | 1353 | /* Initialize per network namespace state */ |
| 1354 | static int __net_init vrf_netns_init(struct net *net) |
| 1355 | { |
| 1356 | bool *add_fib_rules = net_generic(net, vrf_net_id); |
| 1357 | |
| 1358 | *add_fib_rules = true; |
| 1359 | |
| 1360 | return 0; |
| 1361 | } |
| 1362 | |
| 1363 | static struct pernet_operations vrf_net_ops __net_initdata = { |
| 1364 | .init = vrf_netns_init, |
| 1365 | .id = &vrf_net_id, |
| 1366 | .size = sizeof(bool), |
| 1367 | }; |
| 1368 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1369 | static int __init vrf_init_module(void) |
| 1370 | { |
| 1371 | int rc; |
| 1372 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1373 | register_netdevice_notifier(&vrf_notifier_block); |
| 1374 | |
David Ahern | 386ed38 | 2017-06-08 11:31:11 -0600 | [diff] [blame] | 1375 | rc = register_pernet_subsys(&vrf_net_ops); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1376 | if (rc < 0) |
| 1377 | goto error; |
| 1378 | |
David Ahern | 386ed38 | 2017-06-08 11:31:11 -0600 | [diff] [blame] | 1379 | rc = rtnl_link_register(&vrf_link_ops); |
| 1380 | if (rc < 0) { |
| 1381 | unregister_pernet_subsys(&vrf_net_ops); |
| 1382 | goto error; |
| 1383 | } |
| 1384 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1385 | return 0; |
| 1386 | |
| 1387 | error: |
| 1388 | unregister_netdevice_notifier(&vrf_notifier_block); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1389 | return rc; |
| 1390 | } |
| 1391 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1392 | module_init(vrf_init_module); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 1393 | MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern"); |
| 1394 | MODULE_DESCRIPTION("Device driver to instantiate VRF domains"); |
| 1395 | MODULE_LICENSE("GPL"); |
| 1396 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); |
| 1397 | MODULE_VERSION(DRV_VERSION); |