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