xen-netback: add control ring boilerplate
[cascardo/linux.git] / drivers / net / vrf.c
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/arp.h>
31 #include <net/ip.h>
32 #include <net/ip_fib.h>
33 #include <net/ip6_fib.h>
34 #include <net/ip6_route.h>
35 #include <net/route.h>
36 #include <net/addrconf.h>
37 #include <net/l3mdev.h>
38
39 #define RT_FL_TOS(oldflp4) \
40         ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
41
42 #define DRV_NAME        "vrf"
43 #define DRV_VERSION     "1.0"
44
45 struct net_vrf {
46         struct rtable           *rth;
47         struct rt6_info         *rt6;
48         u32                     tb_id;
49 };
50
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
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
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
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
134 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
135                                            struct net_device *dev)
136 {
137         vrf_tx_error(dev, skb);
138         return NET_XMIT_DROP;
139 }
140 #endif
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),
175                 .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_L3MDEV_SRC |
176                                 FLOWI_FLAG_SKIP_NH_OIF,
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
188         ret = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
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:
197         vrf_tx_error(vrf_dev, skb);
198         goto out;
199 }
200
201 static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
202 {
203         /* strip the ethernet header added for pass through VRF device */
204         __skb_pull(skb, skb_network_offset(skb));
205
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:
212                 vrf_tx_error(dev, skb);
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
235 #if IS_ENABLED(CONFIG_IPV6)
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
276 static void vrf_rt6_release(struct net_vrf *vrf)
277 {
278         dst_release(&vrf->rt6->dst);
279         vrf->rt6 = NULL;
280 }
281
282 static int vrf_rt6_create(struct net_device *dev)
283 {
284         struct net_vrf *vrf = netdev_priv(dev);
285         struct net *net = dev_net(dev);
286         struct fib6_table *rt6i_table;
287         struct rt6_info *rt6;
288         int rc = -ENOMEM;
289
290         rt6i_table = fib6_new_table(net, vrf->tb_id);
291         if (!rt6i_table)
292                 goto out;
293
294         rt6 = ip6_dst_alloc(net, dev,
295                             DST_HOST | DST_NOPOLICY | DST_NOXFRM | DST_NOCACHE);
296         if (!rt6)
297                 goto out;
298
299         dst_hold(&rt6->dst);
300
301         rt6->rt6i_table = rt6i_table;
302         rt6->dst.output = vrf_output6;
303         vrf->rt6 = rt6;
304         rc = 0;
305 out:
306         return rc;
307 }
308 #else
309 static void vrf_rt6_release(struct net_vrf *vrf)
310 {
311 }
312
313 static int vrf_rt6_create(struct net_device *dev)
314 {
315         return 0;
316 }
317 #endif
318
319 /* modelled after ip_finish_output2 */
320 static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
321 {
322         struct dst_entry *dst = skb_dst(skb);
323         struct rtable *rt = (struct rtable *)dst;
324         struct net_device *dev = dst->dev;
325         unsigned int hh_len = LL_RESERVED_SPACE(dev);
326         struct neighbour *neigh;
327         u32 nexthop;
328         int ret = -EINVAL;
329
330         /* Be paranoid, rather than too clever. */
331         if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
332                 struct sk_buff *skb2;
333
334                 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
335                 if (!skb2) {
336                         ret = -ENOMEM;
337                         goto err;
338                 }
339                 if (skb->sk)
340                         skb_set_owner_w(skb2, skb->sk);
341
342                 consume_skb(skb);
343                 skb = skb2;
344         }
345
346         rcu_read_lock_bh();
347
348         nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
349         neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
350         if (unlikely(!neigh))
351                 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
352         if (!IS_ERR(neigh))
353                 ret = dst_neigh_output(dst, neigh, skb);
354
355         rcu_read_unlock_bh();
356 err:
357         if (unlikely(ret < 0))
358                 vrf_tx_error(skb->dev, skb);
359         return ret;
360 }
361
362 static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
363 {
364         struct net_device *dev = skb_dst(skb)->dev;
365
366         IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
367
368         skb->dev = dev;
369         skb->protocol = htons(ETH_P_IP);
370
371         return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
372                             net, sk, skb, NULL, dev,
373                             vrf_finish_output,
374                             !(IPCB(skb)->flags & IPSKB_REROUTED));
375 }
376
377 static void vrf_rtable_release(struct net_vrf *vrf)
378 {
379         struct dst_entry *dst = (struct dst_entry *)vrf->rth;
380
381         dst_release(dst);
382         vrf->rth = NULL;
383 }
384
385 static struct rtable *vrf_rtable_create(struct net_device *dev)
386 {
387         struct net_vrf *vrf = netdev_priv(dev);
388         struct rtable *rth;
389
390         if (!fib_new_table(dev_net(dev), vrf->tb_id))
391                 return NULL;
392
393         rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1, 1, 0);
394         if (rth) {
395                 rth->dst.output = vrf_output;
396                 rth->rt_table_id = vrf->tb_id;
397         }
398
399         return rth;
400 }
401
402 /**************************** device handling ********************/
403
404 /* cycle interface to flush neighbor cache and move routes across tables */
405 static void cycle_netdev(struct net_device *dev)
406 {
407         unsigned int flags = dev->flags;
408         int ret;
409
410         if (!netif_running(dev))
411                 return;
412
413         ret = dev_change_flags(dev, flags & ~IFF_UP);
414         if (ret >= 0)
415                 ret = dev_change_flags(dev, flags);
416
417         if (ret < 0) {
418                 netdev_err(dev,
419                            "Failed to cycle device %s; route tables might be wrong!\n",
420                            dev->name);
421         }
422 }
423
424 static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
425 {
426         int ret;
427
428         ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL);
429         if (ret < 0)
430                 return ret;
431
432         port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
433         cycle_netdev(port_dev);
434
435         return 0;
436 }
437
438 static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
439 {
440         if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev))
441                 return -EINVAL;
442
443         return do_vrf_add_slave(dev, port_dev);
444 }
445
446 /* inverse of do_vrf_add_slave */
447 static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
448 {
449         netdev_upper_dev_unlink(port_dev, dev);
450         port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
451
452         cycle_netdev(port_dev);
453
454         return 0;
455 }
456
457 static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
458 {
459         return do_vrf_del_slave(dev, port_dev);
460 }
461
462 static void vrf_dev_uninit(struct net_device *dev)
463 {
464         struct net_vrf *vrf = netdev_priv(dev);
465         struct net_device *port_dev;
466         struct list_head *iter;
467
468         vrf_rtable_release(vrf);
469         vrf_rt6_release(vrf);
470
471         netdev_for_each_lower_dev(dev, port_dev, iter)
472                 vrf_del_slave(dev, port_dev);
473
474         free_percpu(dev->dstats);
475         dev->dstats = NULL;
476 }
477
478 static int vrf_dev_init(struct net_device *dev)
479 {
480         struct net_vrf *vrf = netdev_priv(dev);
481
482         dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
483         if (!dev->dstats)
484                 goto out_nomem;
485
486         /* create the default dst which points back to us */
487         vrf->rth = vrf_rtable_create(dev);
488         if (!vrf->rth)
489                 goto out_stats;
490
491         if (vrf_rt6_create(dev) != 0)
492                 goto out_rth;
493
494         dev->flags = IFF_MASTER | IFF_NOARP;
495
496         return 0;
497
498 out_rth:
499         vrf_rtable_release(vrf);
500 out_stats:
501         free_percpu(dev->dstats);
502         dev->dstats = NULL;
503 out_nomem:
504         return -ENOMEM;
505 }
506
507 static const struct net_device_ops vrf_netdev_ops = {
508         .ndo_init               = vrf_dev_init,
509         .ndo_uninit             = vrf_dev_uninit,
510         .ndo_start_xmit         = vrf_xmit,
511         .ndo_get_stats64        = vrf_get_stats64,
512         .ndo_add_slave          = vrf_add_slave,
513         .ndo_del_slave          = vrf_del_slave,
514 };
515
516 static u32 vrf_fib_table(const struct net_device *dev)
517 {
518         struct net_vrf *vrf = netdev_priv(dev);
519
520         return vrf->tb_id;
521 }
522
523 static struct rtable *vrf_get_rtable(const struct net_device *dev,
524                                      const struct flowi4 *fl4)
525 {
526         struct rtable *rth = NULL;
527
528         if (!(fl4->flowi4_flags & FLOWI_FLAG_L3MDEV_SRC)) {
529                 struct net_vrf *vrf = netdev_priv(dev);
530
531                 rth = vrf->rth;
532                 dst_hold(&rth->dst);
533         }
534
535         return rth;
536 }
537
538 /* called under rcu_read_lock */
539 static int vrf_get_saddr(struct net_device *dev, struct flowi4 *fl4)
540 {
541         struct fib_result res = { .tclassid = 0 };
542         struct net *net = dev_net(dev);
543         u32 orig_tos = fl4->flowi4_tos;
544         u8 flags = fl4->flowi4_flags;
545         u8 scope = fl4->flowi4_scope;
546         u8 tos = RT_FL_TOS(fl4);
547         int rc;
548
549         if (unlikely(!fl4->daddr))
550                 return 0;
551
552         fl4->flowi4_flags |= FLOWI_FLAG_SKIP_NH_OIF;
553         fl4->flowi4_iif = LOOPBACK_IFINDEX;
554         /* make sure oif is set to VRF device for lookup */
555         fl4->flowi4_oif = dev->ifindex;
556         fl4->flowi4_tos = tos & IPTOS_RT_MASK;
557         fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
558                              RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
559
560         rc = fib_lookup(net, fl4, &res, 0);
561         if (!rc) {
562                 if (res.type == RTN_LOCAL)
563                         fl4->saddr = res.fi->fib_prefsrc ? : fl4->daddr;
564                 else
565                         fib_select_path(net, &res, fl4, -1);
566         }
567
568         fl4->flowi4_flags = flags;
569         fl4->flowi4_tos = orig_tos;
570         fl4->flowi4_scope = scope;
571
572         return rc;
573 }
574
575 #if IS_ENABLED(CONFIG_IPV6)
576 /* neighbor handling is done with actual device; do not want
577  * to flip skb->dev for those ndisc packets. This really fails
578  * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
579  * a start.
580  */
581 static bool ipv6_ndisc_frame(const struct sk_buff *skb)
582 {
583         const struct ipv6hdr *iph = ipv6_hdr(skb);
584         bool rc = false;
585
586         if (iph->nexthdr == NEXTHDR_ICMP) {
587                 const struct icmp6hdr *icmph;
588                 struct icmp6hdr _icmph;
589
590                 icmph = skb_header_pointer(skb, sizeof(*iph),
591                                            sizeof(_icmph), &_icmph);
592                 if (!icmph)
593                         goto out;
594
595                 switch (icmph->icmp6_type) {
596                 case NDISC_ROUTER_SOLICITATION:
597                 case NDISC_ROUTER_ADVERTISEMENT:
598                 case NDISC_NEIGHBOUR_SOLICITATION:
599                 case NDISC_NEIGHBOUR_ADVERTISEMENT:
600                 case NDISC_REDIRECT:
601                         rc = true;
602                         break;
603                 }
604         }
605
606 out:
607         return rc;
608 }
609
610 static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
611                                    struct sk_buff *skb)
612 {
613         /* if packet is NDISC keep the ingress interface */
614         if (!ipv6_ndisc_frame(skb)) {
615                 skb->dev = vrf_dev;
616                 skb->skb_iif = vrf_dev->ifindex;
617
618                 skb_push(skb, skb->mac_len);
619                 dev_queue_xmit_nit(skb, vrf_dev);
620                 skb_pull(skb, skb->mac_len);
621
622                 IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
623         }
624
625         return skb;
626 }
627
628 #else
629 static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
630                                    struct sk_buff *skb)
631 {
632         return skb;
633 }
634 #endif
635
636 static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev,
637                                   struct sk_buff *skb)
638 {
639         skb->dev = vrf_dev;
640         skb->skb_iif = vrf_dev->ifindex;
641
642         skb_push(skb, skb->mac_len);
643         dev_queue_xmit_nit(skb, vrf_dev);
644         skb_pull(skb, skb->mac_len);
645
646         return skb;
647 }
648
649 /* called with rcu lock held */
650 static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev,
651                                   struct sk_buff *skb,
652                                   u16 proto)
653 {
654         switch (proto) {
655         case AF_INET:
656                 return vrf_ip_rcv(vrf_dev, skb);
657         case AF_INET6:
658                 return vrf_ip6_rcv(vrf_dev, skb);
659         }
660
661         return skb;
662 }
663
664 #if IS_ENABLED(CONFIG_IPV6)
665 static struct dst_entry *vrf_get_rt6_dst(const struct net_device *dev,
666                                          const struct flowi6 *fl6)
667 {
668         struct rt6_info *rt = NULL;
669
670         if (!(fl6->flowi6_flags & FLOWI_FLAG_L3MDEV_SRC)) {
671                 struct net_vrf *vrf = netdev_priv(dev);
672
673                 rt = vrf->rt6;
674                 dst_hold(&rt->dst);
675         }
676
677         return (struct dst_entry *)rt;
678 }
679 #endif
680
681 static const struct l3mdev_ops vrf_l3mdev_ops = {
682         .l3mdev_fib_table       = vrf_fib_table,
683         .l3mdev_get_rtable      = vrf_get_rtable,
684         .l3mdev_get_saddr       = vrf_get_saddr,
685         .l3mdev_l3_rcv          = vrf_l3_rcv,
686 #if IS_ENABLED(CONFIG_IPV6)
687         .l3mdev_get_rt6_dst     = vrf_get_rt6_dst,
688 #endif
689 };
690
691 static void vrf_get_drvinfo(struct net_device *dev,
692                             struct ethtool_drvinfo *info)
693 {
694         strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
695         strlcpy(info->version, DRV_VERSION, sizeof(info->version));
696 }
697
698 static const struct ethtool_ops vrf_ethtool_ops = {
699         .get_drvinfo    = vrf_get_drvinfo,
700 };
701
702 static void vrf_setup(struct net_device *dev)
703 {
704         ether_setup(dev);
705
706         /* Initialize the device structure. */
707         dev->netdev_ops = &vrf_netdev_ops;
708         dev->l3mdev_ops = &vrf_l3mdev_ops;
709         dev->ethtool_ops = &vrf_ethtool_ops;
710         dev->destructor = free_netdev;
711
712         /* Fill in device structure with ethernet-generic values. */
713         eth_hw_addr_random(dev);
714
715         /* don't acquire vrf device's netif_tx_lock when transmitting */
716         dev->features |= NETIF_F_LLTX;
717
718         /* don't allow vrf devices to change network namespaces. */
719         dev->features |= NETIF_F_NETNS_LOCAL;
720 }
721
722 static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
723 {
724         if (tb[IFLA_ADDRESS]) {
725                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
726                         return -EINVAL;
727                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
728                         return -EADDRNOTAVAIL;
729         }
730         return 0;
731 }
732
733 static void vrf_dellink(struct net_device *dev, struct list_head *head)
734 {
735         unregister_netdevice_queue(dev, head);
736 }
737
738 static int vrf_newlink(struct net *src_net, struct net_device *dev,
739                        struct nlattr *tb[], struct nlattr *data[])
740 {
741         struct net_vrf *vrf = netdev_priv(dev);
742
743         if (!data || !data[IFLA_VRF_TABLE])
744                 return -EINVAL;
745
746         vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
747
748         dev->priv_flags |= IFF_L3MDEV_MASTER;
749
750         return register_netdevice(dev);
751 }
752
753 static size_t vrf_nl_getsize(const struct net_device *dev)
754 {
755         return nla_total_size(sizeof(u32));  /* IFLA_VRF_TABLE */
756 }
757
758 static int vrf_fillinfo(struct sk_buff *skb,
759                         const struct net_device *dev)
760 {
761         struct net_vrf *vrf = netdev_priv(dev);
762
763         return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
764 }
765
766 static size_t vrf_get_slave_size(const struct net_device *bond_dev,
767                                  const struct net_device *slave_dev)
768 {
769         return nla_total_size(sizeof(u32));  /* IFLA_VRF_PORT_TABLE */
770 }
771
772 static int vrf_fill_slave_info(struct sk_buff *skb,
773                                const struct net_device *vrf_dev,
774                                const struct net_device *slave_dev)
775 {
776         struct net_vrf *vrf = netdev_priv(vrf_dev);
777
778         if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id))
779                 return -EMSGSIZE;
780
781         return 0;
782 }
783
784 static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
785         [IFLA_VRF_TABLE] = { .type = NLA_U32 },
786 };
787
788 static struct rtnl_link_ops vrf_link_ops __read_mostly = {
789         .kind           = DRV_NAME,
790         .priv_size      = sizeof(struct net_vrf),
791
792         .get_size       = vrf_nl_getsize,
793         .policy         = vrf_nl_policy,
794         .validate       = vrf_validate,
795         .fill_info      = vrf_fillinfo,
796
797         .get_slave_size  = vrf_get_slave_size,
798         .fill_slave_info = vrf_fill_slave_info,
799
800         .newlink        = vrf_newlink,
801         .dellink        = vrf_dellink,
802         .setup          = vrf_setup,
803         .maxtype        = IFLA_VRF_MAX,
804 };
805
806 static int vrf_device_event(struct notifier_block *unused,
807                             unsigned long event, void *ptr)
808 {
809         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
810
811         /* only care about unregister events to drop slave references */
812         if (event == NETDEV_UNREGISTER) {
813                 struct net_device *vrf_dev;
814
815                 if (!netif_is_l3_slave(dev))
816                         goto out;
817
818                 vrf_dev = netdev_master_upper_dev_get(dev);
819                 vrf_del_slave(vrf_dev, dev);
820         }
821 out:
822         return NOTIFY_DONE;
823 }
824
825 static struct notifier_block vrf_notifier_block __read_mostly = {
826         .notifier_call = vrf_device_event,
827 };
828
829 static int __init vrf_init_module(void)
830 {
831         int rc;
832
833         register_netdevice_notifier(&vrf_notifier_block);
834
835         rc = rtnl_link_register(&vrf_link_ops);
836         if (rc < 0)
837                 goto error;
838
839         return 0;
840
841 error:
842         unregister_netdevice_notifier(&vrf_notifier_block);
843         return rc;
844 }
845
846 module_init(vrf_init_module);
847 MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
848 MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
849 MODULE_LICENSE("GPL");
850 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
851 MODULE_VERSION(DRV_VERSION);