blob: 72f1892ebad0380f76ba84fbcaeba673860e3c78 [file] [log] [blame]
David Ahern193125d2015-08-13 14:59:10 -06001/*
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 Ahern8f583362015-08-27 10:10:50 -070030#include <net/arp.h>
David Ahern193125d2015-08-13 14:59:10 -060031#include <net/ip.h>
32#include <net/ip_fib.h>
33#include <net/ip6_route.h>
34#include <net/rtnetlink.h>
35#include <net/route.h>
36#include <net/addrconf.h>
37#include <net/vrf.h>
David Ahernee15ee52015-09-29 20:07:12 -070038#include <net/l3mdev.h>
David Ahern193125d2015-08-13 14:59:10 -060039
40#define DRV_NAME "vrf"
41#define DRV_VERSION "1.0"
42
43#define vrf_is_slave(dev) ((dev)->flags & IFF_SLAVE)
44
45#define vrf_master_get_rcu(dev) \
46 ((struct net_device *)rcu_dereference(dev->rx_handler_data))
47
48struct pcpu_dstats {
49 u64 tx_pkts;
50 u64 tx_bytes;
51 u64 tx_drps;
52 u64 rx_pkts;
53 u64 rx_bytes;
54 struct u64_stats_sync syncp;
55};
56
57static struct dst_entry *vrf_ip_check(struct dst_entry *dst, u32 cookie)
58{
59 return dst;
60}
61
62static int vrf_ip_local_out(struct sk_buff *skb)
63{
64 return ip_local_out(skb);
65}
66
67static unsigned int vrf_v4_mtu(const struct dst_entry *dst)
68{
69 /* TO-DO: return max ethernet size? */
70 return dst->dev->mtu;
71}
72
73static void vrf_dst_destroy(struct dst_entry *dst)
74{
75 /* our dst lives forever - or until the device is closed */
76}
77
78static unsigned int vrf_default_advmss(const struct dst_entry *dst)
79{
80 return 65535 - 40;
81}
82
83static struct dst_ops vrf_dst_ops = {
84 .family = AF_INET,
85 .local_out = vrf_ip_local_out,
86 .check = vrf_ip_check,
87 .mtu = vrf_v4_mtu,
88 .destroy = vrf_dst_destroy,
89 .default_advmss = vrf_default_advmss,
90};
91
92static bool is_ip_rx_frame(struct sk_buff *skb)
93{
94 switch (skb->protocol) {
95 case htons(ETH_P_IP):
96 case htons(ETH_P_IPV6):
97 return true;
98 }
99 return false;
100}
101
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +0300102static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
103{
104 vrf_dev->stats.tx_errors++;
105 kfree_skb(skb);
106}
107
David Ahern193125d2015-08-13 14:59:10 -0600108/* note: already called with rcu_read_lock */
109static rx_handler_result_t vrf_handle_frame(struct sk_buff **pskb)
110{
111 struct sk_buff *skb = *pskb;
112
113 if (is_ip_rx_frame(skb)) {
114 struct net_device *dev = vrf_master_get_rcu(skb->dev);
115 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
116
117 u64_stats_update_begin(&dstats->syncp);
118 dstats->rx_pkts++;
119 dstats->rx_bytes += skb->len;
120 u64_stats_update_end(&dstats->syncp);
121
122 skb->dev = dev;
123
124 return RX_HANDLER_ANOTHER;
125 }
126 return RX_HANDLER_PASS;
127}
128
129static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
130 struct rtnl_link_stats64 *stats)
131{
132 int i;
133
134 for_each_possible_cpu(i) {
135 const struct pcpu_dstats *dstats;
136 u64 tbytes, tpkts, tdrops, rbytes, rpkts;
137 unsigned int start;
138
139 dstats = per_cpu_ptr(dev->dstats, i);
140 do {
141 start = u64_stats_fetch_begin_irq(&dstats->syncp);
142 tbytes = dstats->tx_bytes;
143 tpkts = dstats->tx_pkts;
144 tdrops = dstats->tx_drps;
145 rbytes = dstats->rx_bytes;
146 rpkts = dstats->rx_pkts;
147 } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
148 stats->tx_bytes += tbytes;
149 stats->tx_packets += tpkts;
150 stats->tx_dropped += tdrops;
151 stats->rx_bytes += rbytes;
152 stats->rx_packets += rpkts;
153 }
154 return stats;
155}
156
157static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
158 struct net_device *dev)
159{
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +0300160 vrf_tx_error(dev, skb);
161 return NET_XMIT_DROP;
David Ahern193125d2015-08-13 14:59:10 -0600162}
163
164static int vrf_send_v4_prep(struct sk_buff *skb, struct flowi4 *fl4,
165 struct net_device *vrf_dev)
166{
167 struct rtable *rt;
168 int err = 1;
169
170 rt = ip_route_output_flow(dev_net(vrf_dev), fl4, NULL);
171 if (IS_ERR(rt))
172 goto out;
173
174 /* TO-DO: what about broadcast ? */
175 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
176 ip_rt_put(rt);
177 goto out;
178 }
179
180 skb_dst_drop(skb);
181 skb_dst_set(skb, &rt->dst);
182 err = 0;
183out:
184 return err;
185}
186
187static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
188 struct net_device *vrf_dev)
189{
190 struct iphdr *ip4h = ip_hdr(skb);
191 int ret = NET_XMIT_DROP;
192 struct flowi4 fl4 = {
193 /* needed to match OIF rule */
194 .flowi4_oif = vrf_dev->ifindex,
195 .flowi4_iif = LOOPBACK_IFINDEX,
196 .flowi4_tos = RT_TOS(ip4h->tos),
David Ahern58189ca2015-09-15 15:10:50 -0700197 .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_VRFSRC |
198 FLOWI_FLAG_SKIP_NH_OIF,
David Ahern193125d2015-08-13 14:59:10 -0600199 .daddr = ip4h->daddr,
200 };
201
202 if (vrf_send_v4_prep(skb, &fl4, vrf_dev))
203 goto err;
204
205 if (!ip4h->saddr) {
206 ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
207 RT_SCOPE_LINK);
208 }
209
210 ret = ip_local_out(skb);
211 if (unlikely(net_xmit_eval(ret)))
212 vrf_dev->stats.tx_errors++;
213 else
214 ret = NET_XMIT_SUCCESS;
215
216out:
217 return ret;
218err:
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +0300219 vrf_tx_error(vrf_dev, skb);
David Ahern193125d2015-08-13 14:59:10 -0600220 goto out;
221}
222
223static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
224{
David Ahern8f583362015-08-27 10:10:50 -0700225 /* strip the ethernet header added for pass through VRF device */
226 __skb_pull(skb, skb_network_offset(skb));
227
David Ahern193125d2015-08-13 14:59:10 -0600228 switch (skb->protocol) {
229 case htons(ETH_P_IP):
230 return vrf_process_v4_outbound(skb, dev);
231 case htons(ETH_P_IPV6):
232 return vrf_process_v6_outbound(skb, dev);
233 default:
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +0300234 vrf_tx_error(dev, skb);
David Ahern193125d2015-08-13 14:59:10 -0600235 return NET_XMIT_DROP;
236 }
237}
238
239static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
240{
241 netdev_tx_t ret = is_ip_tx_frame(skb, dev);
242
243 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
244 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
245
246 u64_stats_update_begin(&dstats->syncp);
247 dstats->tx_pkts++;
248 dstats->tx_bytes += skb->len;
249 u64_stats_update_end(&dstats->syncp);
250 } else {
251 this_cpu_inc(dev->dstats->tx_drps);
252 }
253
254 return ret;
255}
256
David Ahern8f583362015-08-27 10:10:50 -0700257/* modelled after ip_finish_output2 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500258static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
David Ahern193125d2015-08-13 14:59:10 -0600259{
David Ahern8f583362015-08-27 10:10:50 -0700260 struct dst_entry *dst = skb_dst(skb);
261 struct rtable *rt = (struct rtable *)dst;
262 struct net_device *dev = dst->dev;
263 unsigned int hh_len = LL_RESERVED_SPACE(dev);
264 struct neighbour *neigh;
265 u32 nexthop;
266 int ret = -EINVAL;
267
268 /* Be paranoid, rather than too clever. */
269 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
270 struct sk_buff *skb2;
271
272 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
273 if (!skb2) {
274 ret = -ENOMEM;
275 goto err;
276 }
277 if (skb->sk)
278 skb_set_owner_w(skb2, skb->sk);
279
280 consume_skb(skb);
281 skb = skb2;
282 }
283
284 rcu_read_lock_bh();
285
286 nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
287 neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
288 if (unlikely(!neigh))
289 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
290 if (!IS_ERR(neigh))
291 ret = dst_neigh_output(dst, neigh, skb);
292
293 rcu_read_unlock_bh();
294err:
295 if (unlikely(ret < 0))
296 vrf_tx_error(skb->dev, skb);
297 return ret;
David Ahern193125d2015-08-13 14:59:10 -0600298}
299
300static int vrf_output(struct sock *sk, struct sk_buff *skb)
301{
302 struct net_device *dev = skb_dst(skb)->dev;
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500303 struct net *net = dev_net(dev);
David Ahern193125d2015-08-13 14:59:10 -0600304
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500305 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
David Ahern193125d2015-08-13 14:59:10 -0600306
307 skb->dev = dev;
308 skb->protocol = htons(ETH_P_IP);
309
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500310 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
311 net, sk, skb, NULL, dev,
David Ahern8f583362015-08-27 10:10:50 -0700312 vrf_finish_output,
David Ahern193125d2015-08-13 14:59:10 -0600313 !(IPCB(skb)->flags & IPSKB_REROUTED));
314}
315
316static void vrf_rtable_destroy(struct net_vrf *vrf)
317{
318 struct dst_entry *dst = (struct dst_entry *)vrf->rth;
319
Nikolay Aleksandrov3a4a27d2015-08-18 20:28:03 +0300320 dst_destroy(dst);
David Ahern193125d2015-08-13 14:59:10 -0600321 vrf->rth = NULL;
322}
323
324static struct rtable *vrf_rtable_create(struct net_device *dev)
325{
David Ahernb7503e02015-09-02 13:58:35 -0700326 struct net_vrf *vrf = netdev_priv(dev);
David Ahern193125d2015-08-13 14:59:10 -0600327 struct rtable *rth;
328
329 rth = dst_alloc(&vrf_dst_ops, dev, 2,
330 DST_OBSOLETE_NONE,
331 (DST_HOST | DST_NOPOLICY | DST_NOXFRM));
332 if (rth) {
333 rth->dst.output = vrf_output;
334 rth->rt_genid = rt_genid_ipv4(dev_net(dev));
335 rth->rt_flags = 0;
336 rth->rt_type = RTN_UNICAST;
337 rth->rt_is_input = 0;
338 rth->rt_iif = 0;
339 rth->rt_pmtu = 0;
340 rth->rt_gateway = 0;
341 rth->rt_uses_gateway = 0;
David Ahernb7503e02015-09-02 13:58:35 -0700342 rth->rt_table_id = vrf->tb_id;
David Ahern193125d2015-08-13 14:59:10 -0600343 INIT_LIST_HEAD(&rth->rt_uncached);
344 rth->rt_uncached_list = NULL;
David Ahern193125d2015-08-13 14:59:10 -0600345 }
346
347 return rth;
348}
349
350/**************************** device handling ********************/
351
352/* cycle interface to flush neighbor cache and move routes across tables */
353static void cycle_netdev(struct net_device *dev)
354{
355 unsigned int flags = dev->flags;
356 int ret;
357
358 if (!netif_running(dev))
359 return;
360
361 ret = dev_change_flags(dev, flags & ~IFF_UP);
362 if (ret >= 0)
363 ret = dev_change_flags(dev, flags);
364
365 if (ret < 0) {
366 netdev_err(dev,
367 "Failed to cycle device %s; route tables might be wrong!\n",
368 dev->name);
369 }
370}
371
372static struct slave *__vrf_find_slave_dev(struct slave_queue *queue,
373 struct net_device *dev)
374{
375 struct list_head *head = &queue->all_slaves;
376 struct slave *slave;
377
378 list_for_each_entry(slave, head, list) {
379 if (slave->dev == dev)
380 return slave;
381 }
382
383 return NULL;
384}
385
386/* inverse of __vrf_insert_slave */
387static void __vrf_remove_slave(struct slave_queue *queue, struct slave *slave)
388{
David Ahern193125d2015-08-13 14:59:10 -0600389 list_del(&slave->list);
David Ahern193125d2015-08-13 14:59:10 -0600390}
391
392static void __vrf_insert_slave(struct slave_queue *queue, struct slave *slave)
393{
David Ahern193125d2015-08-13 14:59:10 -0600394 list_add(&slave->list, &queue->all_slaves);
David Ahern193125d2015-08-13 14:59:10 -0600395}
396
397static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
398{
399 struct net_vrf_dev *vrf_ptr = kmalloc(sizeof(*vrf_ptr), GFP_KERNEL);
400 struct slave *slave = kzalloc(sizeof(*slave), GFP_KERNEL);
David Ahern193125d2015-08-13 14:59:10 -0600401 struct net_vrf *vrf = netdev_priv(dev);
402 struct slave_queue *queue = &vrf->queue;
403 int ret = -ENOMEM;
404
405 if (!slave || !vrf_ptr)
406 goto out_fail;
407
408 slave->dev = port_dev;
David Ahern193125d2015-08-13 14:59:10 -0600409 vrf_ptr->ifindex = dev->ifindex;
410 vrf_ptr->tb_id = vrf->tb_id;
411
David Ahern193125d2015-08-13 14:59:10 -0600412 /* register the packet handler for slave ports */
413 ret = netdev_rx_handler_register(port_dev, vrf_handle_frame, dev);
414 if (ret) {
415 netdev_err(port_dev,
416 "Device %s failed to register rx_handler\n",
417 port_dev->name);
Nikolay Aleksandrov15df5e72015-08-19 06:27:09 +0300418 goto out_fail;
David Ahern193125d2015-08-13 14:59:10 -0600419 }
420
421 ret = netdev_master_upper_dev_link(port_dev, dev);
422 if (ret < 0)
423 goto out_unregister;
424
425 port_dev->flags |= IFF_SLAVE;
Nikolay Aleksandrov15df5e72015-08-19 06:27:09 +0300426 __vrf_insert_slave(queue, slave);
David Ahern193125d2015-08-13 14:59:10 -0600427 rcu_assign_pointer(port_dev->vrf_ptr, vrf_ptr);
428 cycle_netdev(port_dev);
429
430 return 0;
431
432out_unregister:
433 netdev_rx_handler_unregister(port_dev);
David Ahern193125d2015-08-13 14:59:10 -0600434out_fail:
435 kfree(vrf_ptr);
436 kfree(slave);
437 return ret;
438}
439
440static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
441{
David Ahern007979e2015-09-29 20:07:10 -0700442 if (netif_is_l3_master(port_dev) || vrf_is_slave(port_dev))
David Ahern193125d2015-08-13 14:59:10 -0600443 return -EINVAL;
444
445 return do_vrf_add_slave(dev, port_dev);
446}
447
448/* inverse of do_vrf_add_slave */
449static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
450{
451 struct net_vrf_dev *vrf_ptr = rtnl_dereference(port_dev->vrf_ptr);
452 struct net_vrf *vrf = netdev_priv(dev);
453 struct slave_queue *queue = &vrf->queue;
454 struct slave *slave;
455
456 RCU_INIT_POINTER(port_dev->vrf_ptr, NULL);
457
458 netdev_upper_dev_unlink(port_dev, dev);
459 port_dev->flags &= ~IFF_SLAVE;
460
461 netdev_rx_handler_unregister(port_dev);
462
463 /* after netdev_rx_handler_unregister for synchronize_rcu */
464 kfree(vrf_ptr);
465
466 cycle_netdev(port_dev);
467
468 slave = __vrf_find_slave_dev(queue, port_dev);
469 if (slave)
470 __vrf_remove_slave(queue, slave);
471
472 kfree(slave);
473
474 return 0;
475}
476
477static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
478{
David Ahern193125d2015-08-13 14:59:10 -0600479 return do_vrf_del_slave(dev, port_dev);
480}
481
482static void vrf_dev_uninit(struct net_device *dev)
483{
484 struct net_vrf *vrf = netdev_priv(dev);
485 struct slave_queue *queue = &vrf->queue;
486 struct list_head *head = &queue->all_slaves;
487 struct slave *slave, *next;
488
489 vrf_rtable_destroy(vrf);
490
491 list_for_each_entry_safe(slave, next, head, list)
492 vrf_del_slave(dev, slave->dev);
493
Nikolay Aleksandrov3a4a27d2015-08-18 20:28:03 +0300494 free_percpu(dev->dstats);
David Ahern193125d2015-08-13 14:59:10 -0600495 dev->dstats = NULL;
496}
497
498static int vrf_dev_init(struct net_device *dev)
499{
500 struct net_vrf *vrf = netdev_priv(dev);
501
502 INIT_LIST_HEAD(&vrf->queue.all_slaves);
503
504 dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
505 if (!dev->dstats)
506 goto out_nomem;
507
508 /* create the default dst which points back to us */
509 vrf->rth = vrf_rtable_create(dev);
510 if (!vrf->rth)
511 goto out_stats;
512
513 dev->flags = IFF_MASTER | IFF_NOARP;
514
515 return 0;
516
517out_stats:
518 free_percpu(dev->dstats);
519 dev->dstats = NULL;
520out_nomem:
521 return -ENOMEM;
522}
523
524static 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 Ahernee15ee52015-09-29 20:07:12 -0700533static 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
540static struct rtable *vrf_get_rtable(const struct net_device *dev,
541 const struct flowi4 *fl4)
542{
543 struct rtable *rth = NULL;
544
545 if (!(fl4->flowi4_flags & FLOWI_FLAG_VRFSRC)) {
546 struct net_vrf *vrf = netdev_priv(dev);
547
548 rth = vrf->rth;
549 atomic_inc(&rth->dst.__refcnt);
550 }
551
552 return rth;
553}
554
555static const struct l3mdev_ops vrf_l3mdev_ops = {
556 .l3mdev_fib_table = vrf_fib_table,
557 .l3mdev_get_rtable = vrf_get_rtable,
558};
559
David Ahern193125d2015-08-13 14:59:10 -0600560static void vrf_get_drvinfo(struct net_device *dev,
561 struct ethtool_drvinfo *info)
562{
563 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
564 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
565}
566
567static const struct ethtool_ops vrf_ethtool_ops = {
568 .get_drvinfo = vrf_get_drvinfo,
569};
570
571static void vrf_setup(struct net_device *dev)
572{
573 ether_setup(dev);
574
575 /* Initialize the device structure. */
576 dev->netdev_ops = &vrf_netdev_ops;
David Ahernee15ee52015-09-29 20:07:12 -0700577 dev->l3mdev_ops = &vrf_l3mdev_ops;
David Ahern193125d2015-08-13 14:59:10 -0600578 dev->ethtool_ops = &vrf_ethtool_ops;
579 dev->destructor = free_netdev;
580
581 /* Fill in device structure with ethernet-generic values. */
582 eth_hw_addr_random(dev);
583
584 /* don't acquire vrf device's netif_tx_lock when transmitting */
585 dev->features |= NETIF_F_LLTX;
586
587 /* don't allow vrf devices to change network namespaces. */
588 dev->features |= NETIF_F_NETNS_LOCAL;
589}
590
591static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
592{
593 if (tb[IFLA_ADDRESS]) {
594 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
595 return -EINVAL;
596 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
597 return -EADDRNOTAVAIL;
598 }
599 return 0;
600}
601
602static void vrf_dellink(struct net_device *dev, struct list_head *head)
603{
604 struct net_vrf_dev *vrf_ptr = rtnl_dereference(dev->vrf_ptr);
605
606 RCU_INIT_POINTER(dev->vrf_ptr, NULL);
607 kfree_rcu(vrf_ptr, rcu);
608 unregister_netdevice_queue(dev, head);
609}
610
611static int vrf_newlink(struct net *src_net, struct net_device *dev,
612 struct nlattr *tb[], struct nlattr *data[])
613{
614 struct net_vrf *vrf = netdev_priv(dev);
615 struct net_vrf_dev *vrf_ptr;
616 int err;
617
618 if (!data || !data[IFLA_VRF_TABLE])
619 return -EINVAL;
620
621 vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
622
David Ahern007979e2015-09-29 20:07:10 -0700623 dev->priv_flags |= IFF_L3MDEV_MASTER;
David Ahern193125d2015-08-13 14:59:10 -0600624
625 err = -ENOMEM;
626 vrf_ptr = kmalloc(sizeof(*dev->vrf_ptr), GFP_KERNEL);
627 if (!vrf_ptr)
628 goto out_fail;
629
630 vrf_ptr->ifindex = dev->ifindex;
631 vrf_ptr->tb_id = vrf->tb_id;
632
633 err = register_netdevice(dev);
634 if (err < 0)
635 goto out_fail;
636
637 rcu_assign_pointer(dev->vrf_ptr, vrf_ptr);
638
639 return 0;
640
641out_fail:
642 kfree(vrf_ptr);
643 free_netdev(dev);
644 return err;
645}
646
647static size_t vrf_nl_getsize(const struct net_device *dev)
648{
649 return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
650}
651
652static int vrf_fillinfo(struct sk_buff *skb,
653 const struct net_device *dev)
654{
655 struct net_vrf *vrf = netdev_priv(dev);
656
657 return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
658}
659
660static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
661 [IFLA_VRF_TABLE] = { .type = NLA_U32 },
662};
663
664static struct rtnl_link_ops vrf_link_ops __read_mostly = {
665 .kind = DRV_NAME,
666 .priv_size = sizeof(struct net_vrf),
667
668 .get_size = vrf_nl_getsize,
669 .policy = vrf_nl_policy,
670 .validate = vrf_validate,
671 .fill_info = vrf_fillinfo,
672
673 .newlink = vrf_newlink,
674 .dellink = vrf_dellink,
675 .setup = vrf_setup,
676 .maxtype = IFLA_VRF_MAX,
677};
678
679static int vrf_device_event(struct notifier_block *unused,
680 unsigned long event, void *ptr)
681{
682 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
683
684 /* only care about unregister events to drop slave references */
685 if (event == NETDEV_UNREGISTER) {
686 struct net_vrf_dev *vrf_ptr = rtnl_dereference(dev->vrf_ptr);
687 struct net_device *vrf_dev;
688
David Ahern007979e2015-09-29 20:07:10 -0700689 if (!vrf_ptr || netif_is_l3_master(dev))
David Ahern193125d2015-08-13 14:59:10 -0600690 goto out;
691
Nikolay Aleksandrov58aa9082015-08-18 20:28:04 +0300692 vrf_dev = netdev_master_upper_dev_get(dev);
693 vrf_del_slave(vrf_dev, dev);
David Ahern193125d2015-08-13 14:59:10 -0600694 }
695out:
696 return NOTIFY_DONE;
697}
698
699static struct notifier_block vrf_notifier_block __read_mostly = {
700 .notifier_call = vrf_device_event,
701};
702
703static int __init vrf_init_module(void)
704{
705 int rc;
706
707 vrf_dst_ops.kmem_cachep =
708 kmem_cache_create("vrf_ip_dst_cache",
709 sizeof(struct rtable), 0,
Nikolay Aleksandrove367da02015-08-19 06:27:07 +0300710 SLAB_HWCACHE_ALIGN,
David Ahern193125d2015-08-13 14:59:10 -0600711 NULL);
712
713 if (!vrf_dst_ops.kmem_cachep)
714 return -ENOMEM;
715
716 register_netdevice_notifier(&vrf_notifier_block);
717
718 rc = rtnl_link_register(&vrf_link_ops);
719 if (rc < 0)
720 goto error;
721
722 return 0;
723
724error:
725 unregister_netdevice_notifier(&vrf_notifier_block);
726 kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
727 return rc;
728}
729
730static void __exit vrf_cleanup_module(void)
731{
732 rtnl_link_unregister(&vrf_link_ops);
733 unregister_netdevice_notifier(&vrf_notifier_block);
734 kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
735}
736
737module_init(vrf_init_module);
738module_exit(vrf_cleanup_module);
739MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
740MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
741MODULE_LICENSE("GPL");
742MODULE_ALIAS_RTNL_LINK(DRV_NAME);
743MODULE_VERSION(DRV_VERSION);