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 | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 38 | |
David Ahern | 8cbb512c | 2015-10-05 08:51:26 -0700 | [diff] [blame] | 39 | #define RT_FL_TOS(oldflp4) \ |
| 40 | ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK)) |
| 41 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 42 | #define DRV_NAME "vrf" |
| 43 | #define DRV_VERSION "1.0" |
| 44 | |
David Ahern | ec53951 | 2015-09-29 20:07:17 -0700 | [diff] [blame] | 45 | struct net_vrf { |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 46 | struct rtable __rcu *rth; |
| 47 | struct rt6_info __rcu *rt6; |
David Ahern | ec53951 | 2015-09-29 20:07:17 -0700 | [diff] [blame] | 48 | u32 tb_id; |
| 49 | }; |
| 50 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 51 | struct pcpu_dstats { |
| 52 | u64 tx_pkts; |
| 53 | u64 tx_bytes; |
| 54 | u64 tx_drps; |
| 55 | u64 rx_pkts; |
| 56 | u64 rx_bytes; |
| 57 | struct u64_stats_sync syncp; |
| 58 | }; |
| 59 | |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame] | 60 | static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb) |
| 61 | { |
| 62 | vrf_dev->stats.tx_errors++; |
| 63 | kfree_skb(skb); |
| 64 | } |
| 65 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 66 | static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev, |
| 67 | struct rtnl_link_stats64 *stats) |
| 68 | { |
| 69 | int i; |
| 70 | |
| 71 | for_each_possible_cpu(i) { |
| 72 | const struct pcpu_dstats *dstats; |
| 73 | u64 tbytes, tpkts, tdrops, rbytes, rpkts; |
| 74 | unsigned int start; |
| 75 | |
| 76 | dstats = per_cpu_ptr(dev->dstats, i); |
| 77 | do { |
| 78 | start = u64_stats_fetch_begin_irq(&dstats->syncp); |
| 79 | tbytes = dstats->tx_bytes; |
| 80 | tpkts = dstats->tx_pkts; |
| 81 | tdrops = dstats->tx_drps; |
| 82 | rbytes = dstats->rx_bytes; |
| 83 | rpkts = dstats->rx_pkts; |
| 84 | } while (u64_stats_fetch_retry_irq(&dstats->syncp, start)); |
| 85 | stats->tx_bytes += tbytes; |
| 86 | stats->tx_packets += tpkts; |
| 87 | stats->tx_dropped += tdrops; |
| 88 | stats->rx_bytes += rbytes; |
| 89 | stats->rx_packets += rpkts; |
| 90 | } |
| 91 | return stats; |
| 92 | } |
| 93 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 94 | #if IS_ENABLED(CONFIG_IPV6) |
| 95 | static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb, |
| 96 | struct net_device *dev) |
| 97 | { |
| 98 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 99 | struct net *net = dev_net(skb->dev); |
| 100 | struct flowi6 fl6 = { |
| 101 | /* needed to match OIF rule */ |
| 102 | .flowi6_oif = dev->ifindex, |
| 103 | .flowi6_iif = LOOPBACK_IFINDEX, |
| 104 | .daddr = iph->daddr, |
| 105 | .saddr = iph->saddr, |
| 106 | .flowlabel = ip6_flowinfo(iph), |
| 107 | .flowi6_mark = skb->mark, |
| 108 | .flowi6_proto = iph->nexthdr, |
| 109 | .flowi6_flags = FLOWI_FLAG_L3MDEV_SRC | FLOWI_FLAG_SKIP_NH_OIF, |
| 110 | }; |
| 111 | int ret = NET_XMIT_DROP; |
| 112 | struct dst_entry *dst; |
| 113 | struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst; |
| 114 | |
| 115 | dst = ip6_route_output(net, NULL, &fl6); |
| 116 | if (dst == dst_null) |
| 117 | goto err; |
| 118 | |
| 119 | skb_dst_drop(skb); |
| 120 | skb_dst_set(skb, dst); |
| 121 | |
| 122 | ret = ip6_local_out(net, skb->sk, skb); |
| 123 | if (unlikely(net_xmit_eval(ret))) |
| 124 | dev->stats.tx_errors++; |
| 125 | else |
| 126 | ret = NET_XMIT_SUCCESS; |
| 127 | |
| 128 | return ret; |
| 129 | err: |
| 130 | vrf_tx_error(dev, skb); |
| 131 | return NET_XMIT_DROP; |
| 132 | } |
| 133 | #else |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 134 | static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb, |
| 135 | struct net_device *dev) |
| 136 | { |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame] | 137 | vrf_tx_error(dev, skb); |
| 138 | return NET_XMIT_DROP; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 139 | } |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 140 | #endif |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 141 | |
| 142 | static int vrf_send_v4_prep(struct sk_buff *skb, struct flowi4 *fl4, |
| 143 | struct net_device *vrf_dev) |
| 144 | { |
| 145 | struct rtable *rt; |
| 146 | int err = 1; |
| 147 | |
| 148 | rt = ip_route_output_flow(dev_net(vrf_dev), fl4, NULL); |
| 149 | if (IS_ERR(rt)) |
| 150 | goto out; |
| 151 | |
| 152 | /* TO-DO: what about broadcast ? */ |
| 153 | if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) { |
| 154 | ip_rt_put(rt); |
| 155 | goto out; |
| 156 | } |
| 157 | |
| 158 | skb_dst_drop(skb); |
| 159 | skb_dst_set(skb, &rt->dst); |
| 160 | err = 0; |
| 161 | out: |
| 162 | return err; |
| 163 | } |
| 164 | |
| 165 | static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb, |
| 166 | struct net_device *vrf_dev) |
| 167 | { |
| 168 | struct iphdr *ip4h = ip_hdr(skb); |
| 169 | int ret = NET_XMIT_DROP; |
| 170 | struct flowi4 fl4 = { |
| 171 | /* needed to match OIF rule */ |
| 172 | .flowi4_oif = vrf_dev->ifindex, |
| 173 | .flowi4_iif = LOOPBACK_IFINDEX, |
| 174 | .flowi4_tos = RT_TOS(ip4h->tos), |
David Ahern | 6e2895a | 2015-10-05 08:51:23 -0700 | [diff] [blame] | 175 | .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_L3MDEV_SRC | |
David Ahern | 58189ca | 2015-09-15 15:10:50 -0700 | [diff] [blame] | 176 | FLOWI_FLAG_SKIP_NH_OIF, |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 177 | .daddr = ip4h->daddr, |
| 178 | }; |
| 179 | |
| 180 | if (vrf_send_v4_prep(skb, &fl4, vrf_dev)) |
| 181 | goto err; |
| 182 | |
| 183 | if (!ip4h->saddr) { |
| 184 | ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0, |
| 185 | RT_SCOPE_LINK); |
| 186 | } |
| 187 | |
Eric W. Biederman | 33224b1 | 2015-10-07 16:48:46 -0500 | [diff] [blame] | 188 | 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] | 189 | if (unlikely(net_xmit_eval(ret))) |
| 190 | vrf_dev->stats.tx_errors++; |
| 191 | else |
| 192 | ret = NET_XMIT_SUCCESS; |
| 193 | |
| 194 | out: |
| 195 | return ret; |
| 196 | err: |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame] | 197 | vrf_tx_error(vrf_dev, skb); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 198 | goto out; |
| 199 | } |
| 200 | |
| 201 | static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev) |
| 202 | { |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 203 | /* strip the ethernet header added for pass through VRF device */ |
| 204 | __skb_pull(skb, skb_network_offset(skb)); |
| 205 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 206 | switch (skb->protocol) { |
| 207 | case htons(ETH_P_IP): |
| 208 | return vrf_process_v4_outbound(skb, dev); |
| 209 | case htons(ETH_P_IPV6): |
| 210 | return vrf_process_v6_outbound(skb, dev); |
| 211 | default: |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame] | 212 | vrf_tx_error(dev, skb); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 213 | return NET_XMIT_DROP; |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev) |
| 218 | { |
| 219 | netdev_tx_t ret = is_ip_tx_frame(skb, dev); |
| 220 | |
| 221 | if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { |
| 222 | struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats); |
| 223 | |
| 224 | u64_stats_update_begin(&dstats->syncp); |
| 225 | dstats->tx_pkts++; |
| 226 | dstats->tx_bytes += skb->len; |
| 227 | u64_stats_update_end(&dstats->syncp); |
| 228 | } else { |
| 229 | this_cpu_inc(dev->dstats->tx_drps); |
| 230 | } |
| 231 | |
| 232 | return ret; |
| 233 | } |
| 234 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 235 | #if IS_ENABLED(CONFIG_IPV6) |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 236 | /* modelled after ip6_finish_output2 */ |
| 237 | static int vrf_finish_output6(struct net *net, struct sock *sk, |
| 238 | struct sk_buff *skb) |
| 239 | { |
| 240 | struct dst_entry *dst = skb_dst(skb); |
| 241 | struct net_device *dev = dst->dev; |
| 242 | struct neighbour *neigh; |
| 243 | struct in6_addr *nexthop; |
| 244 | int ret; |
| 245 | |
| 246 | skb->protocol = htons(ETH_P_IPV6); |
| 247 | skb->dev = dev; |
| 248 | |
| 249 | rcu_read_lock_bh(); |
| 250 | nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr); |
| 251 | neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop); |
| 252 | if (unlikely(!neigh)) |
| 253 | neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false); |
| 254 | if (!IS_ERR(neigh)) { |
| 255 | ret = dst_neigh_output(dst, neigh, skb); |
| 256 | rcu_read_unlock_bh(); |
| 257 | return ret; |
| 258 | } |
| 259 | rcu_read_unlock_bh(); |
| 260 | |
| 261 | IP6_INC_STATS(dev_net(dst->dev), |
| 262 | ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES); |
| 263 | kfree_skb(skb); |
| 264 | return -EINVAL; |
| 265 | } |
| 266 | |
| 267 | /* modelled after ip6_output */ |
| 268 | static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb) |
| 269 | { |
| 270 | return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, |
| 271 | net, sk, skb, NULL, skb_dst(skb)->dev, |
| 272 | vrf_finish_output6, |
| 273 | !(IP6CB(skb)->flags & IP6SKB_REROUTED)); |
| 274 | } |
| 275 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 276 | /* holding rtnl */ |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 277 | static void vrf_rt6_release(struct net_vrf *vrf) |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 278 | { |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 279 | struct rt6_info *rt6 = rtnl_dereference(vrf->rt6); |
| 280 | |
| 281 | rcu_assign_pointer(vrf->rt6, NULL); |
| 282 | |
| 283 | if (rt6) |
| 284 | dst_release(&rt6->dst); |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static int vrf_rt6_create(struct net_device *dev) |
| 288 | { |
| 289 | struct net_vrf *vrf = netdev_priv(dev); |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 290 | struct net *net = dev_net(dev); |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 291 | struct fib6_table *rt6i_table; |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 292 | struct rt6_info *rt6; |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 293 | int rc = -ENOMEM; |
| 294 | |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 295 | rt6i_table = fib6_new_table(net, vrf->tb_id); |
| 296 | if (!rt6i_table) |
| 297 | goto out; |
| 298 | |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 299 | rt6 = ip6_dst_alloc(net, dev, |
| 300 | DST_HOST | DST_NOPOLICY | DST_NOXFRM | DST_NOCACHE); |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 301 | if (!rt6) |
| 302 | goto out; |
| 303 | |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 304 | dst_hold(&rt6->dst); |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 305 | |
| 306 | rt6->rt6i_table = rt6i_table; |
| 307 | rt6->dst.output = vrf_output6; |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 308 | rcu_assign_pointer(vrf->rt6, rt6); |
| 309 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 310 | rc = 0; |
| 311 | out: |
| 312 | return rc; |
| 313 | } |
| 314 | #else |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 315 | static void vrf_rt6_release(struct net_vrf *vrf) |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 316 | { |
| 317 | } |
| 318 | |
| 319 | static int vrf_rt6_create(struct net_device *dev) |
| 320 | { |
| 321 | return 0; |
| 322 | } |
| 323 | #endif |
| 324 | |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 325 | /* modelled after ip_finish_output2 */ |
Eric W. Biederman | 0c4b51f | 2015-09-15 20:04:18 -0500 | [diff] [blame] | 326 | 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] | 327 | { |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 328 | struct dst_entry *dst = skb_dst(skb); |
| 329 | struct rtable *rt = (struct rtable *)dst; |
| 330 | struct net_device *dev = dst->dev; |
| 331 | unsigned int hh_len = LL_RESERVED_SPACE(dev); |
| 332 | struct neighbour *neigh; |
| 333 | u32 nexthop; |
| 334 | int ret = -EINVAL; |
| 335 | |
| 336 | /* Be paranoid, rather than too clever. */ |
| 337 | if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { |
| 338 | struct sk_buff *skb2; |
| 339 | |
| 340 | skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev)); |
| 341 | if (!skb2) { |
| 342 | ret = -ENOMEM; |
| 343 | goto err; |
| 344 | } |
| 345 | if (skb->sk) |
| 346 | skb_set_owner_w(skb2, skb->sk); |
| 347 | |
| 348 | consume_skb(skb); |
| 349 | skb = skb2; |
| 350 | } |
| 351 | |
| 352 | rcu_read_lock_bh(); |
| 353 | |
| 354 | nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr); |
| 355 | neigh = __ipv4_neigh_lookup_noref(dev, nexthop); |
| 356 | if (unlikely(!neigh)) |
| 357 | neigh = __neigh_create(&arp_tbl, &nexthop, dev, false); |
| 358 | if (!IS_ERR(neigh)) |
| 359 | ret = dst_neigh_output(dst, neigh, skb); |
| 360 | |
| 361 | rcu_read_unlock_bh(); |
| 362 | err: |
| 363 | if (unlikely(ret < 0)) |
| 364 | vrf_tx_error(skb->dev, skb); |
| 365 | return ret; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 366 | } |
| 367 | |
Eric W. Biederman | ede2059 | 2015-10-07 16:48:47 -0500 | [diff] [blame] | 368 | 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] | 369 | { |
| 370 | struct net_device *dev = skb_dst(skb)->dev; |
| 371 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 372 | IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 373 | |
| 374 | skb->dev = dev; |
| 375 | skb->protocol = htons(ETH_P_IP); |
| 376 | |
Eric W. Biederman | 29a26a5 | 2015-09-15 20:04:16 -0500 | [diff] [blame] | 377 | return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, |
| 378 | net, sk, skb, NULL, dev, |
David Ahern | 8f58336 | 2015-08-27 10:10:50 -0700 | [diff] [blame] | 379 | vrf_finish_output, |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 380 | !(IPCB(skb)->flags & IPSKB_REROUTED)); |
| 381 | } |
| 382 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 383 | /* holding rtnl */ |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 384 | static void vrf_rtable_release(struct net_vrf *vrf) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 385 | { |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 386 | struct rtable *rth = rtnl_dereference(vrf->rth); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 387 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 388 | rcu_assign_pointer(vrf->rth, NULL); |
| 389 | |
| 390 | if (rth) |
| 391 | dst_release(&rth->dst); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 392 | } |
| 393 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 394 | static int vrf_rtable_create(struct net_device *dev) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 395 | { |
David Ahern | b7503e0 | 2015-09-02 13:58:35 -0700 | [diff] [blame] | 396 | struct net_vrf *vrf = netdev_priv(dev); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 397 | struct rtable *rth; |
| 398 | |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 399 | if (!fib_new_table(dev_net(dev), vrf->tb_id)) |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 400 | return -ENOMEM; |
David Ahern | b3b4663 | 2016-05-04 21:46:12 -0700 | [diff] [blame] | 401 | |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 402 | rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1, 1, 0); |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 403 | if (!rth) |
| 404 | return -ENOMEM; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 405 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 406 | rth->dst.output = vrf_output; |
| 407 | rth->rt_table_id = vrf->tb_id; |
| 408 | |
| 409 | rcu_assign_pointer(vrf->rth, rth); |
| 410 | |
| 411 | return 0; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | /**************************** device handling ********************/ |
| 415 | |
| 416 | /* cycle interface to flush neighbor cache and move routes across tables */ |
| 417 | static void cycle_netdev(struct net_device *dev) |
| 418 | { |
| 419 | unsigned int flags = dev->flags; |
| 420 | int ret; |
| 421 | |
| 422 | if (!netif_running(dev)) |
| 423 | return; |
| 424 | |
| 425 | ret = dev_change_flags(dev, flags & ~IFF_UP); |
| 426 | if (ret >= 0) |
| 427 | ret = dev_change_flags(dev, flags); |
| 428 | |
| 429 | if (ret < 0) { |
| 430 | netdev_err(dev, |
| 431 | "Failed to cycle device %s; route tables might be wrong!\n", |
| 432 | dev->name); |
| 433 | } |
| 434 | } |
| 435 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 436 | static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev) |
| 437 | { |
Nikolay Aleksandrov | bad5316 | 2015-11-24 14:29:16 +0100 | [diff] [blame] | 438 | int ret; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 439 | |
Jiri Pirko | 29bf24a | 2015-12-03 12:12:11 +0100 | [diff] [blame] | 440 | ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 441 | if (ret < 0) |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 442 | return ret; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 443 | |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 444 | port_dev->priv_flags |= IFF_L3MDEV_SLAVE; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 445 | cycle_netdev(port_dev); |
| 446 | |
| 447 | return 0; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev) |
| 451 | { |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 452 | 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] | 453 | return -EINVAL; |
| 454 | |
| 455 | return do_vrf_add_slave(dev, port_dev); |
| 456 | } |
| 457 | |
| 458 | /* inverse of do_vrf_add_slave */ |
| 459 | static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev) |
| 460 | { |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 461 | netdev_upper_dev_unlink(port_dev, dev); |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 462 | port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 463 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 464 | cycle_netdev(port_dev); |
| 465 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 466 | return 0; |
| 467 | } |
| 468 | |
| 469 | static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev) |
| 470 | { |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 471 | return do_vrf_del_slave(dev, port_dev); |
| 472 | } |
| 473 | |
| 474 | static void vrf_dev_uninit(struct net_device *dev) |
| 475 | { |
| 476 | struct net_vrf *vrf = netdev_priv(dev); |
Nikolay Aleksandrov | bad5316 | 2015-11-24 14:29:16 +0100 | [diff] [blame] | 477 | struct net_device *port_dev; |
| 478 | struct list_head *iter; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 479 | |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 480 | vrf_rtable_release(vrf); |
| 481 | vrf_rt6_release(vrf); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 482 | |
Nikolay Aleksandrov | bad5316 | 2015-11-24 14:29:16 +0100 | [diff] [blame] | 483 | netdev_for_each_lower_dev(dev, port_dev, iter) |
| 484 | vrf_del_slave(dev, port_dev); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 485 | |
Nikolay Aleksandrov | 3a4a27d | 2015-08-18 20:28:03 +0300 | [diff] [blame] | 486 | free_percpu(dev->dstats); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 487 | dev->dstats = NULL; |
| 488 | } |
| 489 | |
| 490 | static int vrf_dev_init(struct net_device *dev) |
| 491 | { |
| 492 | struct net_vrf *vrf = netdev_priv(dev); |
| 493 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 494 | dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats); |
| 495 | if (!dev->dstats) |
| 496 | goto out_nomem; |
| 497 | |
| 498 | /* create the default dst which points back to us */ |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 499 | if (vrf_rtable_create(dev) != 0) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 500 | goto out_stats; |
| 501 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 502 | if (vrf_rt6_create(dev) != 0) |
| 503 | goto out_rth; |
| 504 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 505 | dev->flags = IFF_MASTER | IFF_NOARP; |
| 506 | |
David Ahern | b87ab6b | 2016-06-01 21:16:39 -0700 | [diff] [blame^] | 507 | /* MTU is irrelevant for VRF device; set to 64k similar to lo */ |
| 508 | dev->mtu = 64 * 1024; |
| 509 | |
| 510 | /* similarly, oper state is irrelevant; set to up to avoid confusion */ |
| 511 | dev->operstate = IF_OPER_UP; |
| 512 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 513 | return 0; |
| 514 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 515 | out_rth: |
David Ahern | 9ab179d | 2016-04-07 11:10:06 -0700 | [diff] [blame] | 516 | vrf_rtable_release(vrf); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 517 | out_stats: |
| 518 | free_percpu(dev->dstats); |
| 519 | dev->dstats = NULL; |
| 520 | out_nomem: |
| 521 | return -ENOMEM; |
| 522 | } |
| 523 | |
| 524 | static const struct net_device_ops vrf_netdev_ops = { |
| 525 | .ndo_init = vrf_dev_init, |
| 526 | .ndo_uninit = vrf_dev_uninit, |
| 527 | .ndo_start_xmit = vrf_xmit, |
| 528 | .ndo_get_stats64 = vrf_get_stats64, |
| 529 | .ndo_add_slave = vrf_add_slave, |
| 530 | .ndo_del_slave = vrf_del_slave, |
| 531 | }; |
| 532 | |
David Ahern | ee15ee5 | 2015-09-29 20:07:12 -0700 | [diff] [blame] | 533 | static u32 vrf_fib_table(const struct net_device *dev) |
| 534 | { |
| 535 | struct net_vrf *vrf = netdev_priv(dev); |
| 536 | |
| 537 | return vrf->tb_id; |
| 538 | } |
| 539 | |
| 540 | static struct rtable *vrf_get_rtable(const struct net_device *dev, |
| 541 | const struct flowi4 *fl4) |
| 542 | { |
| 543 | struct rtable *rth = NULL; |
| 544 | |
David Ahern | 6e2895a | 2015-10-05 08:51:23 -0700 | [diff] [blame] | 545 | if (!(fl4->flowi4_flags & FLOWI_FLAG_L3MDEV_SRC)) { |
David Ahern | ee15ee5 | 2015-09-29 20:07:12 -0700 | [diff] [blame] | 546 | struct net_vrf *vrf = netdev_priv(dev); |
| 547 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 548 | rcu_read_lock(); |
| 549 | |
| 550 | rth = rcu_dereference(vrf->rth); |
| 551 | if (likely(rth)) |
| 552 | dst_hold(&rth->dst); |
| 553 | |
| 554 | rcu_read_unlock(); |
David Ahern | ee15ee5 | 2015-09-29 20:07:12 -0700 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | return rth; |
| 558 | } |
| 559 | |
David Ahern | 8cbb512c | 2015-10-05 08:51:26 -0700 | [diff] [blame] | 560 | /* called under rcu_read_lock */ |
David Ahern | b5bdacf | 2016-01-04 09:09:27 -0800 | [diff] [blame] | 561 | static int vrf_get_saddr(struct net_device *dev, struct flowi4 *fl4) |
David Ahern | 8cbb512c | 2015-10-05 08:51:26 -0700 | [diff] [blame] | 562 | { |
| 563 | struct fib_result res = { .tclassid = 0 }; |
| 564 | struct net *net = dev_net(dev); |
| 565 | u32 orig_tos = fl4->flowi4_tos; |
| 566 | u8 flags = fl4->flowi4_flags; |
| 567 | u8 scope = fl4->flowi4_scope; |
| 568 | u8 tos = RT_FL_TOS(fl4); |
David Ahern | b5bdacf | 2016-01-04 09:09:27 -0800 | [diff] [blame] | 569 | int rc; |
David Ahern | 8cbb512c | 2015-10-05 08:51:26 -0700 | [diff] [blame] | 570 | |
| 571 | if (unlikely(!fl4->daddr)) |
David Ahern | b5bdacf | 2016-01-04 09:09:27 -0800 | [diff] [blame] | 572 | return 0; |
David Ahern | 8cbb512c | 2015-10-05 08:51:26 -0700 | [diff] [blame] | 573 | |
| 574 | fl4->flowi4_flags |= FLOWI_FLAG_SKIP_NH_OIF; |
| 575 | fl4->flowi4_iif = LOOPBACK_IFINDEX; |
David Ahern | 1ff23be | 2016-05-07 16:49:00 -0700 | [diff] [blame] | 576 | /* make sure oif is set to VRF device for lookup */ |
| 577 | fl4->flowi4_oif = dev->ifindex; |
David Ahern | 8cbb512c | 2015-10-05 08:51:26 -0700 | [diff] [blame] | 578 | fl4->flowi4_tos = tos & IPTOS_RT_MASK; |
| 579 | fl4->flowi4_scope = ((tos & RTO_ONLINK) ? |
| 580 | RT_SCOPE_LINK : RT_SCOPE_UNIVERSE); |
| 581 | |
David Ahern | b5bdacf | 2016-01-04 09:09:27 -0800 | [diff] [blame] | 582 | rc = fib_lookup(net, fl4, &res, 0); |
| 583 | if (!rc) { |
David Ahern | 8cbb512c | 2015-10-05 08:51:26 -0700 | [diff] [blame] | 584 | if (res.type == RTN_LOCAL) |
| 585 | fl4->saddr = res.fi->fib_prefsrc ? : fl4->daddr; |
| 586 | else |
| 587 | fib_select_path(net, &res, fl4, -1); |
| 588 | } |
| 589 | |
| 590 | fl4->flowi4_flags = flags; |
| 591 | fl4->flowi4_tos = orig_tos; |
| 592 | fl4->flowi4_scope = scope; |
David Ahern | b5bdacf | 2016-01-04 09:09:27 -0800 | [diff] [blame] | 593 | |
| 594 | return rc; |
David Ahern | 8cbb512c | 2015-10-05 08:51:26 -0700 | [diff] [blame] | 595 | } |
| 596 | |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 597 | #if IS_ENABLED(CONFIG_IPV6) |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 598 | /* neighbor handling is done with actual device; do not want |
| 599 | * to flip skb->dev for those ndisc packets. This really fails |
| 600 | * for multiple next protocols (e.g., NEXTHDR_HOP). But it is |
| 601 | * a start. |
| 602 | */ |
| 603 | static bool ipv6_ndisc_frame(const struct sk_buff *skb) |
| 604 | { |
| 605 | const struct ipv6hdr *iph = ipv6_hdr(skb); |
| 606 | bool rc = false; |
| 607 | |
| 608 | if (iph->nexthdr == NEXTHDR_ICMP) { |
| 609 | const struct icmp6hdr *icmph; |
| 610 | struct icmp6hdr _icmph; |
| 611 | |
| 612 | icmph = skb_header_pointer(skb, sizeof(*iph), |
| 613 | sizeof(_icmph), &_icmph); |
| 614 | if (!icmph) |
| 615 | goto out; |
| 616 | |
| 617 | switch (icmph->icmp6_type) { |
| 618 | case NDISC_ROUTER_SOLICITATION: |
| 619 | case NDISC_ROUTER_ADVERTISEMENT: |
| 620 | case NDISC_NEIGHBOUR_SOLICITATION: |
| 621 | case NDISC_NEIGHBOUR_ADVERTISEMENT: |
| 622 | case NDISC_REDIRECT: |
| 623 | rc = true; |
| 624 | break; |
| 625 | } |
| 626 | } |
| 627 | |
| 628 | out: |
| 629 | return rc; |
| 630 | } |
| 631 | |
| 632 | static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev, |
| 633 | struct sk_buff *skb) |
| 634 | { |
| 635 | /* if packet is NDISC keep the ingress interface */ |
| 636 | if (!ipv6_ndisc_frame(skb)) { |
| 637 | skb->dev = vrf_dev; |
| 638 | skb->skb_iif = vrf_dev->ifindex; |
| 639 | |
| 640 | skb_push(skb, skb->mac_len); |
| 641 | dev_queue_xmit_nit(skb, vrf_dev); |
| 642 | skb_pull(skb, skb->mac_len); |
| 643 | |
| 644 | IP6CB(skb)->flags |= IP6SKB_L3SLAVE; |
| 645 | } |
| 646 | |
| 647 | return skb; |
| 648 | } |
| 649 | |
| 650 | #else |
| 651 | static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev, |
| 652 | struct sk_buff *skb) |
| 653 | { |
| 654 | return skb; |
| 655 | } |
| 656 | #endif |
| 657 | |
| 658 | static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev, |
| 659 | struct sk_buff *skb) |
| 660 | { |
| 661 | skb->dev = vrf_dev; |
| 662 | skb->skb_iif = vrf_dev->ifindex; |
| 663 | |
| 664 | skb_push(skb, skb->mac_len); |
| 665 | dev_queue_xmit_nit(skb, vrf_dev); |
| 666 | skb_pull(skb, skb->mac_len); |
| 667 | |
| 668 | return skb; |
| 669 | } |
| 670 | |
| 671 | /* called with rcu lock held */ |
| 672 | static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev, |
| 673 | struct sk_buff *skb, |
| 674 | u16 proto) |
| 675 | { |
| 676 | switch (proto) { |
| 677 | case AF_INET: |
| 678 | return vrf_ip_rcv(vrf_dev, skb); |
| 679 | case AF_INET6: |
| 680 | return vrf_ip6_rcv(vrf_dev, skb); |
| 681 | } |
| 682 | |
| 683 | return skb; |
| 684 | } |
| 685 | |
| 686 | #if IS_ENABLED(CONFIG_IPV6) |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 687 | static struct dst_entry *vrf_get_rt6_dst(const struct net_device *dev, |
| 688 | const struct flowi6 *fl6) |
| 689 | { |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 690 | struct dst_entry *dst = NULL; |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 691 | |
| 692 | if (!(fl6->flowi6_flags & FLOWI_FLAG_L3MDEV_SRC)) { |
| 693 | struct net_vrf *vrf = netdev_priv(dev); |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 694 | struct rt6_info *rt; |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 695 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 696 | rcu_read_lock(); |
| 697 | |
| 698 | rt = rcu_dereference(vrf->rt6); |
| 699 | if (likely(rt)) { |
| 700 | dst = &rt->dst; |
| 701 | dst_hold(dst); |
| 702 | } |
| 703 | |
| 704 | rcu_read_unlock(); |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 705 | } |
| 706 | |
David Ahern | b0e95cc | 2016-05-13 12:23:45 -0700 | [diff] [blame] | 707 | return dst; |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 708 | } |
| 709 | #endif |
| 710 | |
David Ahern | ee15ee5 | 2015-09-29 20:07:12 -0700 | [diff] [blame] | 711 | static const struct l3mdev_ops vrf_l3mdev_ops = { |
| 712 | .l3mdev_fib_table = vrf_fib_table, |
| 713 | .l3mdev_get_rtable = vrf_get_rtable, |
David Ahern | 8cbb512c | 2015-10-05 08:51:26 -0700 | [diff] [blame] | 714 | .l3mdev_get_saddr = vrf_get_saddr, |
David Ahern | 74b2058 | 2016-05-10 11:19:50 -0700 | [diff] [blame] | 715 | .l3mdev_l3_rcv = vrf_l3_rcv, |
David Ahern | 35402e3 | 2015-10-12 11:47:09 -0700 | [diff] [blame] | 716 | #if IS_ENABLED(CONFIG_IPV6) |
| 717 | .l3mdev_get_rt6_dst = vrf_get_rt6_dst, |
| 718 | #endif |
David Ahern | ee15ee5 | 2015-09-29 20:07:12 -0700 | [diff] [blame] | 719 | }; |
| 720 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 721 | static void vrf_get_drvinfo(struct net_device *dev, |
| 722 | struct ethtool_drvinfo *info) |
| 723 | { |
| 724 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 725 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); |
| 726 | } |
| 727 | |
| 728 | static const struct ethtool_ops vrf_ethtool_ops = { |
| 729 | .get_drvinfo = vrf_get_drvinfo, |
| 730 | }; |
| 731 | |
| 732 | static void vrf_setup(struct net_device *dev) |
| 733 | { |
| 734 | ether_setup(dev); |
| 735 | |
| 736 | /* Initialize the device structure. */ |
| 737 | dev->netdev_ops = &vrf_netdev_ops; |
David Ahern | ee15ee5 | 2015-09-29 20:07:12 -0700 | [diff] [blame] | 738 | dev->l3mdev_ops = &vrf_l3mdev_ops; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 739 | dev->ethtool_ops = &vrf_ethtool_ops; |
| 740 | dev->destructor = free_netdev; |
| 741 | |
| 742 | /* Fill in device structure with ethernet-generic values. */ |
| 743 | eth_hw_addr_random(dev); |
| 744 | |
| 745 | /* don't acquire vrf device's netif_tx_lock when transmitting */ |
| 746 | dev->features |= NETIF_F_LLTX; |
| 747 | |
| 748 | /* don't allow vrf devices to change network namespaces. */ |
| 749 | dev->features |= NETIF_F_NETNS_LOCAL; |
| 750 | } |
| 751 | |
| 752 | static int vrf_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 753 | { |
| 754 | if (tb[IFLA_ADDRESS]) { |
| 755 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 756 | return -EINVAL; |
| 757 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 758 | return -EADDRNOTAVAIL; |
| 759 | } |
| 760 | return 0; |
| 761 | } |
| 762 | |
| 763 | static void vrf_dellink(struct net_device *dev, struct list_head *head) |
| 764 | { |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 765 | unregister_netdevice_queue(dev, head); |
| 766 | } |
| 767 | |
| 768 | static int vrf_newlink(struct net *src_net, struct net_device *dev, |
| 769 | struct nlattr *tb[], struct nlattr *data[]) |
| 770 | { |
| 771 | struct net_vrf *vrf = netdev_priv(dev); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 772 | |
| 773 | if (!data || !data[IFLA_VRF_TABLE]) |
| 774 | return -EINVAL; |
| 775 | |
| 776 | vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]); |
| 777 | |
David Ahern | 007979e | 2015-09-29 20:07:10 -0700 | [diff] [blame] | 778 | dev->priv_flags |= IFF_L3MDEV_MASTER; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 779 | |
Nikolay Aleksandrov | 7f109f7 | 2015-11-21 19:46:19 +0100 | [diff] [blame] | 780 | return register_netdevice(dev); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | static size_t vrf_nl_getsize(const struct net_device *dev) |
| 784 | { |
| 785 | return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */ |
| 786 | } |
| 787 | |
| 788 | static int vrf_fillinfo(struct sk_buff *skb, |
| 789 | const struct net_device *dev) |
| 790 | { |
| 791 | struct net_vrf *vrf = netdev_priv(dev); |
| 792 | |
| 793 | return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id); |
| 794 | } |
| 795 | |
David Ahern | 67eb033 | 2016-02-02 07:43:45 -0800 | [diff] [blame] | 796 | static size_t vrf_get_slave_size(const struct net_device *bond_dev, |
| 797 | const struct net_device *slave_dev) |
| 798 | { |
| 799 | return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */ |
| 800 | } |
| 801 | |
| 802 | static int vrf_fill_slave_info(struct sk_buff *skb, |
| 803 | const struct net_device *vrf_dev, |
| 804 | const struct net_device *slave_dev) |
| 805 | { |
| 806 | struct net_vrf *vrf = netdev_priv(vrf_dev); |
| 807 | |
| 808 | if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id)) |
| 809 | return -EMSGSIZE; |
| 810 | |
| 811 | return 0; |
| 812 | } |
| 813 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 814 | static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = { |
| 815 | [IFLA_VRF_TABLE] = { .type = NLA_U32 }, |
| 816 | }; |
| 817 | |
| 818 | static struct rtnl_link_ops vrf_link_ops __read_mostly = { |
| 819 | .kind = DRV_NAME, |
| 820 | .priv_size = sizeof(struct net_vrf), |
| 821 | |
| 822 | .get_size = vrf_nl_getsize, |
| 823 | .policy = vrf_nl_policy, |
| 824 | .validate = vrf_validate, |
| 825 | .fill_info = vrf_fillinfo, |
| 826 | |
David Ahern | 67eb033 | 2016-02-02 07:43:45 -0800 | [diff] [blame] | 827 | .get_slave_size = vrf_get_slave_size, |
| 828 | .fill_slave_info = vrf_fill_slave_info, |
| 829 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 830 | .newlink = vrf_newlink, |
| 831 | .dellink = vrf_dellink, |
| 832 | .setup = vrf_setup, |
| 833 | .maxtype = IFLA_VRF_MAX, |
| 834 | }; |
| 835 | |
| 836 | static int vrf_device_event(struct notifier_block *unused, |
| 837 | unsigned long event, void *ptr) |
| 838 | { |
| 839 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 840 | |
| 841 | /* only care about unregister events to drop slave references */ |
| 842 | if (event == NETDEV_UNREGISTER) { |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 843 | struct net_device *vrf_dev; |
| 844 | |
David Ahern | fee6d4c | 2015-10-05 08:51:24 -0700 | [diff] [blame] | 845 | if (!netif_is_l3_slave(dev)) |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 846 | goto out; |
| 847 | |
Nikolay Aleksandrov | 58aa908 | 2015-08-18 20:28:04 +0300 | [diff] [blame] | 848 | vrf_dev = netdev_master_upper_dev_get(dev); |
| 849 | vrf_del_slave(vrf_dev, dev); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 850 | } |
| 851 | out: |
| 852 | return NOTIFY_DONE; |
| 853 | } |
| 854 | |
| 855 | static struct notifier_block vrf_notifier_block __read_mostly = { |
| 856 | .notifier_call = vrf_device_event, |
| 857 | }; |
| 858 | |
| 859 | static int __init vrf_init_module(void) |
| 860 | { |
| 861 | int rc; |
| 862 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 863 | register_netdevice_notifier(&vrf_notifier_block); |
| 864 | |
| 865 | rc = rtnl_link_register(&vrf_link_ops); |
| 866 | if (rc < 0) |
| 867 | goto error; |
| 868 | |
| 869 | return 0; |
| 870 | |
| 871 | error: |
| 872 | unregister_netdevice_notifier(&vrf_notifier_block); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 873 | return rc; |
| 874 | } |
| 875 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 876 | module_init(vrf_init_module); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 877 | MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern"); |
| 878 | MODULE_DESCRIPTION("Device driver to instantiate VRF domains"); |
| 879 | MODULE_LICENSE("GPL"); |
| 880 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); |
| 881 | MODULE_VERSION(DRV_VERSION); |