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> |
| 30 | #include <net/ip.h> |
| 31 | #include <net/ip_fib.h> |
| 32 | #include <net/ip6_route.h> |
| 33 | #include <net/rtnetlink.h> |
| 34 | #include <net/route.h> |
| 35 | #include <net/addrconf.h> |
| 36 | #include <net/vrf.h> |
| 37 | |
| 38 | #define DRV_NAME "vrf" |
| 39 | #define DRV_VERSION "1.0" |
| 40 | |
| 41 | #define vrf_is_slave(dev) ((dev)->flags & IFF_SLAVE) |
| 42 | |
| 43 | #define vrf_master_get_rcu(dev) \ |
| 44 | ((struct net_device *)rcu_dereference(dev->rx_handler_data)) |
| 45 | |
| 46 | struct pcpu_dstats { |
| 47 | u64 tx_pkts; |
| 48 | u64 tx_bytes; |
| 49 | u64 tx_drps; |
| 50 | u64 rx_pkts; |
| 51 | u64 rx_bytes; |
| 52 | struct u64_stats_sync syncp; |
| 53 | }; |
| 54 | |
| 55 | static struct dst_entry *vrf_ip_check(struct dst_entry *dst, u32 cookie) |
| 56 | { |
| 57 | return dst; |
| 58 | } |
| 59 | |
| 60 | static int vrf_ip_local_out(struct sk_buff *skb) |
| 61 | { |
| 62 | return ip_local_out(skb); |
| 63 | } |
| 64 | |
| 65 | static unsigned int vrf_v4_mtu(const struct dst_entry *dst) |
| 66 | { |
| 67 | /* TO-DO: return max ethernet size? */ |
| 68 | return dst->dev->mtu; |
| 69 | } |
| 70 | |
| 71 | static void vrf_dst_destroy(struct dst_entry *dst) |
| 72 | { |
| 73 | /* our dst lives forever - or until the device is closed */ |
| 74 | } |
| 75 | |
| 76 | static unsigned int vrf_default_advmss(const struct dst_entry *dst) |
| 77 | { |
| 78 | return 65535 - 40; |
| 79 | } |
| 80 | |
| 81 | static struct dst_ops vrf_dst_ops = { |
| 82 | .family = AF_INET, |
| 83 | .local_out = vrf_ip_local_out, |
| 84 | .check = vrf_ip_check, |
| 85 | .mtu = vrf_v4_mtu, |
| 86 | .destroy = vrf_dst_destroy, |
| 87 | .default_advmss = vrf_default_advmss, |
| 88 | }; |
| 89 | |
| 90 | static bool is_ip_rx_frame(struct sk_buff *skb) |
| 91 | { |
| 92 | switch (skb->protocol) { |
| 93 | case htons(ETH_P_IP): |
| 94 | case htons(ETH_P_IPV6): |
| 95 | return true; |
| 96 | } |
| 97 | return false; |
| 98 | } |
| 99 | |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame^] | 100 | static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb) |
| 101 | { |
| 102 | vrf_dev->stats.tx_errors++; |
| 103 | kfree_skb(skb); |
| 104 | } |
| 105 | |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 106 | /* note: already called with rcu_read_lock */ |
| 107 | static rx_handler_result_t vrf_handle_frame(struct sk_buff **pskb) |
| 108 | { |
| 109 | struct sk_buff *skb = *pskb; |
| 110 | |
| 111 | if (is_ip_rx_frame(skb)) { |
| 112 | struct net_device *dev = vrf_master_get_rcu(skb->dev); |
| 113 | struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats); |
| 114 | |
| 115 | u64_stats_update_begin(&dstats->syncp); |
| 116 | dstats->rx_pkts++; |
| 117 | dstats->rx_bytes += skb->len; |
| 118 | u64_stats_update_end(&dstats->syncp); |
| 119 | |
| 120 | skb->dev = dev; |
| 121 | |
| 122 | return RX_HANDLER_ANOTHER; |
| 123 | } |
| 124 | return RX_HANDLER_PASS; |
| 125 | } |
| 126 | |
| 127 | static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev, |
| 128 | struct rtnl_link_stats64 *stats) |
| 129 | { |
| 130 | int i; |
| 131 | |
| 132 | for_each_possible_cpu(i) { |
| 133 | const struct pcpu_dstats *dstats; |
| 134 | u64 tbytes, tpkts, tdrops, rbytes, rpkts; |
| 135 | unsigned int start; |
| 136 | |
| 137 | dstats = per_cpu_ptr(dev->dstats, i); |
| 138 | do { |
| 139 | start = u64_stats_fetch_begin_irq(&dstats->syncp); |
| 140 | tbytes = dstats->tx_bytes; |
| 141 | tpkts = dstats->tx_pkts; |
| 142 | tdrops = dstats->tx_drps; |
| 143 | rbytes = dstats->rx_bytes; |
| 144 | rpkts = dstats->rx_pkts; |
| 145 | } while (u64_stats_fetch_retry_irq(&dstats->syncp, start)); |
| 146 | stats->tx_bytes += tbytes; |
| 147 | stats->tx_packets += tpkts; |
| 148 | stats->tx_dropped += tdrops; |
| 149 | stats->rx_bytes += rbytes; |
| 150 | stats->rx_packets += rpkts; |
| 151 | } |
| 152 | return stats; |
| 153 | } |
| 154 | |
| 155 | static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb, |
| 156 | struct net_device *dev) |
| 157 | { |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame^] | 158 | vrf_tx_error(dev, skb); |
| 159 | return NET_XMIT_DROP; |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | static int vrf_send_v4_prep(struct sk_buff *skb, struct flowi4 *fl4, |
| 163 | struct net_device *vrf_dev) |
| 164 | { |
| 165 | struct rtable *rt; |
| 166 | int err = 1; |
| 167 | |
| 168 | rt = ip_route_output_flow(dev_net(vrf_dev), fl4, NULL); |
| 169 | if (IS_ERR(rt)) |
| 170 | goto out; |
| 171 | |
| 172 | /* TO-DO: what about broadcast ? */ |
| 173 | if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) { |
| 174 | ip_rt_put(rt); |
| 175 | goto out; |
| 176 | } |
| 177 | |
| 178 | skb_dst_drop(skb); |
| 179 | skb_dst_set(skb, &rt->dst); |
| 180 | err = 0; |
| 181 | out: |
| 182 | return err; |
| 183 | } |
| 184 | |
| 185 | static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb, |
| 186 | struct net_device *vrf_dev) |
| 187 | { |
| 188 | struct iphdr *ip4h = ip_hdr(skb); |
| 189 | int ret = NET_XMIT_DROP; |
| 190 | struct flowi4 fl4 = { |
| 191 | /* needed to match OIF rule */ |
| 192 | .flowi4_oif = vrf_dev->ifindex, |
| 193 | .flowi4_iif = LOOPBACK_IFINDEX, |
| 194 | .flowi4_tos = RT_TOS(ip4h->tos), |
| 195 | .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_VRFSRC, |
| 196 | .daddr = ip4h->daddr, |
| 197 | }; |
| 198 | |
| 199 | if (vrf_send_v4_prep(skb, &fl4, vrf_dev)) |
| 200 | goto err; |
| 201 | |
| 202 | if (!ip4h->saddr) { |
| 203 | ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0, |
| 204 | RT_SCOPE_LINK); |
| 205 | } |
| 206 | |
| 207 | ret = ip_local_out(skb); |
| 208 | if (unlikely(net_xmit_eval(ret))) |
| 209 | vrf_dev->stats.tx_errors++; |
| 210 | else |
| 211 | ret = NET_XMIT_SUCCESS; |
| 212 | |
| 213 | out: |
| 214 | return ret; |
| 215 | err: |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame^] | 216 | vrf_tx_error(vrf_dev, skb); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 217 | goto out; |
| 218 | } |
| 219 | |
| 220 | static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev) |
| 221 | { |
| 222 | switch (skb->protocol) { |
| 223 | case htons(ETH_P_IP): |
| 224 | return vrf_process_v4_outbound(skb, dev); |
| 225 | case htons(ETH_P_IPV6): |
| 226 | return vrf_process_v6_outbound(skb, dev); |
| 227 | default: |
Nikolay Aleksandrov | 57b8efa | 2015-08-19 06:12:29 +0300 | [diff] [blame^] | 228 | vrf_tx_error(dev, skb); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 229 | return NET_XMIT_DROP; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev) |
| 234 | { |
| 235 | netdev_tx_t ret = is_ip_tx_frame(skb, dev); |
| 236 | |
| 237 | if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { |
| 238 | struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats); |
| 239 | |
| 240 | u64_stats_update_begin(&dstats->syncp); |
| 241 | dstats->tx_pkts++; |
| 242 | dstats->tx_bytes += skb->len; |
| 243 | u64_stats_update_end(&dstats->syncp); |
| 244 | } else { |
| 245 | this_cpu_inc(dev->dstats->tx_drps); |
| 246 | } |
| 247 | |
| 248 | return ret; |
| 249 | } |
| 250 | |
| 251 | static netdev_tx_t vrf_finish(struct sock *sk, struct sk_buff *skb) |
| 252 | { |
| 253 | return dev_queue_xmit(skb); |
| 254 | } |
| 255 | |
| 256 | static int vrf_output(struct sock *sk, struct sk_buff *skb) |
| 257 | { |
| 258 | struct net_device *dev = skb_dst(skb)->dev; |
| 259 | |
| 260 | IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len); |
| 261 | |
| 262 | skb->dev = dev; |
| 263 | skb->protocol = htons(ETH_P_IP); |
| 264 | |
| 265 | return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, skb, |
| 266 | NULL, dev, |
| 267 | vrf_finish, |
| 268 | !(IPCB(skb)->flags & IPSKB_REROUTED)); |
| 269 | } |
| 270 | |
| 271 | static void vrf_rtable_destroy(struct net_vrf *vrf) |
| 272 | { |
| 273 | struct dst_entry *dst = (struct dst_entry *)vrf->rth; |
| 274 | |
Nikolay Aleksandrov | 3a4a27d | 2015-08-18 20:28:03 +0300 | [diff] [blame] | 275 | dst_destroy(dst); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 276 | vrf->rth = NULL; |
| 277 | } |
| 278 | |
| 279 | static struct rtable *vrf_rtable_create(struct net_device *dev) |
| 280 | { |
| 281 | struct rtable *rth; |
| 282 | |
| 283 | rth = dst_alloc(&vrf_dst_ops, dev, 2, |
| 284 | DST_OBSOLETE_NONE, |
| 285 | (DST_HOST | DST_NOPOLICY | DST_NOXFRM)); |
| 286 | if (rth) { |
| 287 | rth->dst.output = vrf_output; |
| 288 | rth->rt_genid = rt_genid_ipv4(dev_net(dev)); |
| 289 | rth->rt_flags = 0; |
| 290 | rth->rt_type = RTN_UNICAST; |
| 291 | rth->rt_is_input = 0; |
| 292 | rth->rt_iif = 0; |
| 293 | rth->rt_pmtu = 0; |
| 294 | rth->rt_gateway = 0; |
| 295 | rth->rt_uses_gateway = 0; |
| 296 | INIT_LIST_HEAD(&rth->rt_uncached); |
| 297 | rth->rt_uncached_list = NULL; |
| 298 | rth->rt_lwtstate = NULL; |
| 299 | } |
| 300 | |
| 301 | return rth; |
| 302 | } |
| 303 | |
| 304 | /**************************** device handling ********************/ |
| 305 | |
| 306 | /* cycle interface to flush neighbor cache and move routes across tables */ |
| 307 | static void cycle_netdev(struct net_device *dev) |
| 308 | { |
| 309 | unsigned int flags = dev->flags; |
| 310 | int ret; |
| 311 | |
| 312 | if (!netif_running(dev)) |
| 313 | return; |
| 314 | |
| 315 | ret = dev_change_flags(dev, flags & ~IFF_UP); |
| 316 | if (ret >= 0) |
| 317 | ret = dev_change_flags(dev, flags); |
| 318 | |
| 319 | if (ret < 0) { |
| 320 | netdev_err(dev, |
| 321 | "Failed to cycle device %s; route tables might be wrong!\n", |
| 322 | dev->name); |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | static struct slave *__vrf_find_slave_dev(struct slave_queue *queue, |
| 327 | struct net_device *dev) |
| 328 | { |
| 329 | struct list_head *head = &queue->all_slaves; |
| 330 | struct slave *slave; |
| 331 | |
| 332 | list_for_each_entry(slave, head, list) { |
| 333 | if (slave->dev == dev) |
| 334 | return slave; |
| 335 | } |
| 336 | |
| 337 | return NULL; |
| 338 | } |
| 339 | |
| 340 | /* inverse of __vrf_insert_slave */ |
| 341 | static void __vrf_remove_slave(struct slave_queue *queue, struct slave *slave) |
| 342 | { |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 343 | list_del(&slave->list); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 344 | } |
| 345 | |
| 346 | static void __vrf_insert_slave(struct slave_queue *queue, struct slave *slave) |
| 347 | { |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 348 | list_add(&slave->list, &queue->all_slaves); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 349 | } |
| 350 | |
| 351 | static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev) |
| 352 | { |
| 353 | struct net_vrf_dev *vrf_ptr = kmalloc(sizeof(*vrf_ptr), GFP_KERNEL); |
| 354 | struct slave *slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
| 355 | struct slave *duplicate_slave; |
| 356 | struct net_vrf *vrf = netdev_priv(dev); |
| 357 | struct slave_queue *queue = &vrf->queue; |
| 358 | int ret = -ENOMEM; |
| 359 | |
| 360 | if (!slave || !vrf_ptr) |
| 361 | goto out_fail; |
| 362 | |
| 363 | slave->dev = port_dev; |
| 364 | |
| 365 | vrf_ptr->ifindex = dev->ifindex; |
| 366 | vrf_ptr->tb_id = vrf->tb_id; |
| 367 | |
| 368 | duplicate_slave = __vrf_find_slave_dev(queue, port_dev); |
| 369 | if (duplicate_slave) { |
| 370 | ret = -EBUSY; |
| 371 | goto out_fail; |
| 372 | } |
| 373 | |
| 374 | __vrf_insert_slave(queue, slave); |
| 375 | |
| 376 | /* register the packet handler for slave ports */ |
| 377 | ret = netdev_rx_handler_register(port_dev, vrf_handle_frame, dev); |
| 378 | if (ret) { |
| 379 | netdev_err(port_dev, |
| 380 | "Device %s failed to register rx_handler\n", |
| 381 | port_dev->name); |
| 382 | goto out_remove; |
| 383 | } |
| 384 | |
| 385 | ret = netdev_master_upper_dev_link(port_dev, dev); |
| 386 | if (ret < 0) |
| 387 | goto out_unregister; |
| 388 | |
| 389 | port_dev->flags |= IFF_SLAVE; |
| 390 | |
| 391 | rcu_assign_pointer(port_dev->vrf_ptr, vrf_ptr); |
| 392 | cycle_netdev(port_dev); |
| 393 | |
| 394 | return 0; |
| 395 | |
| 396 | out_unregister: |
| 397 | netdev_rx_handler_unregister(port_dev); |
| 398 | out_remove: |
| 399 | __vrf_remove_slave(queue, slave); |
| 400 | out_fail: |
| 401 | kfree(vrf_ptr); |
| 402 | kfree(slave); |
| 403 | return ret; |
| 404 | } |
| 405 | |
| 406 | static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev) |
| 407 | { |
| 408 | if (!netif_is_vrf(dev) || netif_is_vrf(port_dev) || |
| 409 | vrf_is_slave(port_dev)) |
| 410 | return -EINVAL; |
| 411 | |
| 412 | return do_vrf_add_slave(dev, port_dev); |
| 413 | } |
| 414 | |
| 415 | /* inverse of do_vrf_add_slave */ |
| 416 | static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev) |
| 417 | { |
| 418 | struct net_vrf_dev *vrf_ptr = rtnl_dereference(port_dev->vrf_ptr); |
| 419 | struct net_vrf *vrf = netdev_priv(dev); |
| 420 | struct slave_queue *queue = &vrf->queue; |
| 421 | struct slave *slave; |
| 422 | |
| 423 | RCU_INIT_POINTER(port_dev->vrf_ptr, NULL); |
| 424 | |
| 425 | netdev_upper_dev_unlink(port_dev, dev); |
| 426 | port_dev->flags &= ~IFF_SLAVE; |
| 427 | |
| 428 | netdev_rx_handler_unregister(port_dev); |
| 429 | |
| 430 | /* after netdev_rx_handler_unregister for synchronize_rcu */ |
| 431 | kfree(vrf_ptr); |
| 432 | |
| 433 | cycle_netdev(port_dev); |
| 434 | |
| 435 | slave = __vrf_find_slave_dev(queue, port_dev); |
| 436 | if (slave) |
| 437 | __vrf_remove_slave(queue, slave); |
| 438 | |
| 439 | kfree(slave); |
| 440 | |
| 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev) |
| 445 | { |
| 446 | if (!netif_is_vrf(dev)) |
| 447 | return -EINVAL; |
| 448 | |
| 449 | return do_vrf_del_slave(dev, port_dev); |
| 450 | } |
| 451 | |
| 452 | static void vrf_dev_uninit(struct net_device *dev) |
| 453 | { |
| 454 | struct net_vrf *vrf = netdev_priv(dev); |
| 455 | struct slave_queue *queue = &vrf->queue; |
| 456 | struct list_head *head = &queue->all_slaves; |
| 457 | struct slave *slave, *next; |
| 458 | |
| 459 | vrf_rtable_destroy(vrf); |
| 460 | |
| 461 | list_for_each_entry_safe(slave, next, head, list) |
| 462 | vrf_del_slave(dev, slave->dev); |
| 463 | |
Nikolay Aleksandrov | 3a4a27d | 2015-08-18 20:28:03 +0300 | [diff] [blame] | 464 | free_percpu(dev->dstats); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 465 | dev->dstats = NULL; |
| 466 | } |
| 467 | |
| 468 | static int vrf_dev_init(struct net_device *dev) |
| 469 | { |
| 470 | struct net_vrf *vrf = netdev_priv(dev); |
| 471 | |
| 472 | INIT_LIST_HEAD(&vrf->queue.all_slaves); |
| 473 | |
| 474 | dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats); |
| 475 | if (!dev->dstats) |
| 476 | goto out_nomem; |
| 477 | |
| 478 | /* create the default dst which points back to us */ |
| 479 | vrf->rth = vrf_rtable_create(dev); |
| 480 | if (!vrf->rth) |
| 481 | goto out_stats; |
| 482 | |
| 483 | dev->flags = IFF_MASTER | IFF_NOARP; |
| 484 | |
| 485 | return 0; |
| 486 | |
| 487 | out_stats: |
| 488 | free_percpu(dev->dstats); |
| 489 | dev->dstats = NULL; |
| 490 | out_nomem: |
| 491 | return -ENOMEM; |
| 492 | } |
| 493 | |
| 494 | static const struct net_device_ops vrf_netdev_ops = { |
| 495 | .ndo_init = vrf_dev_init, |
| 496 | .ndo_uninit = vrf_dev_uninit, |
| 497 | .ndo_start_xmit = vrf_xmit, |
| 498 | .ndo_get_stats64 = vrf_get_stats64, |
| 499 | .ndo_add_slave = vrf_add_slave, |
| 500 | .ndo_del_slave = vrf_del_slave, |
| 501 | }; |
| 502 | |
| 503 | static void vrf_get_drvinfo(struct net_device *dev, |
| 504 | struct ethtool_drvinfo *info) |
| 505 | { |
| 506 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); |
| 507 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); |
| 508 | } |
| 509 | |
| 510 | static const struct ethtool_ops vrf_ethtool_ops = { |
| 511 | .get_drvinfo = vrf_get_drvinfo, |
| 512 | }; |
| 513 | |
| 514 | static void vrf_setup(struct net_device *dev) |
| 515 | { |
| 516 | ether_setup(dev); |
| 517 | |
| 518 | /* Initialize the device structure. */ |
| 519 | dev->netdev_ops = &vrf_netdev_ops; |
| 520 | dev->ethtool_ops = &vrf_ethtool_ops; |
| 521 | dev->destructor = free_netdev; |
| 522 | |
| 523 | /* Fill in device structure with ethernet-generic values. */ |
| 524 | eth_hw_addr_random(dev); |
| 525 | |
| 526 | /* don't acquire vrf device's netif_tx_lock when transmitting */ |
| 527 | dev->features |= NETIF_F_LLTX; |
| 528 | |
| 529 | /* don't allow vrf devices to change network namespaces. */ |
| 530 | dev->features |= NETIF_F_NETNS_LOCAL; |
| 531 | } |
| 532 | |
| 533 | static int vrf_validate(struct nlattr *tb[], struct nlattr *data[]) |
| 534 | { |
| 535 | if (tb[IFLA_ADDRESS]) { |
| 536 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) |
| 537 | return -EINVAL; |
| 538 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) |
| 539 | return -EADDRNOTAVAIL; |
| 540 | } |
| 541 | return 0; |
| 542 | } |
| 543 | |
| 544 | static void vrf_dellink(struct net_device *dev, struct list_head *head) |
| 545 | { |
| 546 | struct net_vrf_dev *vrf_ptr = rtnl_dereference(dev->vrf_ptr); |
| 547 | |
| 548 | RCU_INIT_POINTER(dev->vrf_ptr, NULL); |
| 549 | kfree_rcu(vrf_ptr, rcu); |
| 550 | unregister_netdevice_queue(dev, head); |
| 551 | } |
| 552 | |
| 553 | static int vrf_newlink(struct net *src_net, struct net_device *dev, |
| 554 | struct nlattr *tb[], struct nlattr *data[]) |
| 555 | { |
| 556 | struct net_vrf *vrf = netdev_priv(dev); |
| 557 | struct net_vrf_dev *vrf_ptr; |
| 558 | int err; |
| 559 | |
| 560 | if (!data || !data[IFLA_VRF_TABLE]) |
| 561 | return -EINVAL; |
| 562 | |
| 563 | vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]); |
| 564 | |
| 565 | dev->priv_flags |= IFF_VRF_MASTER; |
| 566 | |
| 567 | err = -ENOMEM; |
| 568 | vrf_ptr = kmalloc(sizeof(*dev->vrf_ptr), GFP_KERNEL); |
| 569 | if (!vrf_ptr) |
| 570 | goto out_fail; |
| 571 | |
| 572 | vrf_ptr->ifindex = dev->ifindex; |
| 573 | vrf_ptr->tb_id = vrf->tb_id; |
| 574 | |
| 575 | err = register_netdevice(dev); |
| 576 | if (err < 0) |
| 577 | goto out_fail; |
| 578 | |
| 579 | rcu_assign_pointer(dev->vrf_ptr, vrf_ptr); |
| 580 | |
| 581 | return 0; |
| 582 | |
| 583 | out_fail: |
| 584 | kfree(vrf_ptr); |
| 585 | free_netdev(dev); |
| 586 | return err; |
| 587 | } |
| 588 | |
| 589 | static size_t vrf_nl_getsize(const struct net_device *dev) |
| 590 | { |
| 591 | return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */ |
| 592 | } |
| 593 | |
| 594 | static int vrf_fillinfo(struct sk_buff *skb, |
| 595 | const struct net_device *dev) |
| 596 | { |
| 597 | struct net_vrf *vrf = netdev_priv(dev); |
| 598 | |
| 599 | return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id); |
| 600 | } |
| 601 | |
| 602 | static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = { |
| 603 | [IFLA_VRF_TABLE] = { .type = NLA_U32 }, |
| 604 | }; |
| 605 | |
| 606 | static struct rtnl_link_ops vrf_link_ops __read_mostly = { |
| 607 | .kind = DRV_NAME, |
| 608 | .priv_size = sizeof(struct net_vrf), |
| 609 | |
| 610 | .get_size = vrf_nl_getsize, |
| 611 | .policy = vrf_nl_policy, |
| 612 | .validate = vrf_validate, |
| 613 | .fill_info = vrf_fillinfo, |
| 614 | |
| 615 | .newlink = vrf_newlink, |
| 616 | .dellink = vrf_dellink, |
| 617 | .setup = vrf_setup, |
| 618 | .maxtype = IFLA_VRF_MAX, |
| 619 | }; |
| 620 | |
| 621 | static int vrf_device_event(struct notifier_block *unused, |
| 622 | unsigned long event, void *ptr) |
| 623 | { |
| 624 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 625 | |
| 626 | /* only care about unregister events to drop slave references */ |
| 627 | if (event == NETDEV_UNREGISTER) { |
| 628 | struct net_vrf_dev *vrf_ptr = rtnl_dereference(dev->vrf_ptr); |
| 629 | struct net_device *vrf_dev; |
| 630 | |
| 631 | if (!vrf_ptr || netif_is_vrf(dev)) |
| 632 | goto out; |
| 633 | |
Nikolay Aleksandrov | 58aa908 | 2015-08-18 20:28:04 +0300 | [diff] [blame] | 634 | vrf_dev = netdev_master_upper_dev_get(dev); |
| 635 | vrf_del_slave(vrf_dev, dev); |
David Ahern | 193125d | 2015-08-13 14:59:10 -0600 | [diff] [blame] | 636 | } |
| 637 | out: |
| 638 | return NOTIFY_DONE; |
| 639 | } |
| 640 | |
| 641 | static struct notifier_block vrf_notifier_block __read_mostly = { |
| 642 | .notifier_call = vrf_device_event, |
| 643 | }; |
| 644 | |
| 645 | static int __init vrf_init_module(void) |
| 646 | { |
| 647 | int rc; |
| 648 | |
| 649 | vrf_dst_ops.kmem_cachep = |
| 650 | kmem_cache_create("vrf_ip_dst_cache", |
| 651 | sizeof(struct rtable), 0, |
| 652 | SLAB_HWCACHE_ALIGN | SLAB_PANIC, |
| 653 | NULL); |
| 654 | |
| 655 | if (!vrf_dst_ops.kmem_cachep) |
| 656 | return -ENOMEM; |
| 657 | |
| 658 | register_netdevice_notifier(&vrf_notifier_block); |
| 659 | |
| 660 | rc = rtnl_link_register(&vrf_link_ops); |
| 661 | if (rc < 0) |
| 662 | goto error; |
| 663 | |
| 664 | return 0; |
| 665 | |
| 666 | error: |
| 667 | unregister_netdevice_notifier(&vrf_notifier_block); |
| 668 | kmem_cache_destroy(vrf_dst_ops.kmem_cachep); |
| 669 | return rc; |
| 670 | } |
| 671 | |
| 672 | static void __exit vrf_cleanup_module(void) |
| 673 | { |
| 674 | rtnl_link_unregister(&vrf_link_ops); |
| 675 | unregister_netdevice_notifier(&vrf_notifier_block); |
| 676 | kmem_cache_destroy(vrf_dst_ops.kmem_cachep); |
| 677 | } |
| 678 | |
| 679 | module_init(vrf_init_module); |
| 680 | module_exit(vrf_cleanup_module); |
| 681 | MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern"); |
| 682 | MODULE_DESCRIPTION("Device driver to instantiate VRF domains"); |
| 683 | MODULE_LICENSE("GPL"); |
| 684 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); |
| 685 | MODULE_VERSION(DRV_VERSION); |