i40e: don't add zero MAC filter
[firefly-linux-kernel-4.4.55.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/rtnetlink.h>
36 #include <net/route.h>
37 #include <net/addrconf.h>
38 #include <net/l3mdev.h>
39
40 #define RT_FL_TOS(oldflp4) \
41         ((oldflp4)->flowi4_tos & (IPTOS_RT_MASK | RTO_ONLINK))
42
43 #define DRV_NAME        "vrf"
44 #define DRV_VERSION     "1.0"
45
46 #define vrf_master_get_rcu(dev) \
47         ((struct net_device *)rcu_dereference(dev->rx_handler_data))
48
49 struct slave {
50         struct list_head        list;
51         struct net_device       *dev;
52 };
53
54 struct slave_queue {
55         struct list_head        all_slaves;
56 };
57
58 struct net_vrf {
59         struct slave_queue      queue;
60         struct rtable           *rth;
61         struct rt6_info         *rt6;
62         u32                     tb_id;
63 };
64
65 struct pcpu_dstats {
66         u64                     tx_pkts;
67         u64                     tx_bytes;
68         u64                     tx_drps;
69         u64                     rx_pkts;
70         u64                     rx_bytes;
71         struct u64_stats_sync   syncp;
72 };
73
74 static struct dst_entry *vrf_ip_check(struct dst_entry *dst, u32 cookie)
75 {
76         return dst;
77 }
78
79 static int vrf_ip_local_out(struct net *net, struct sock *sk, struct sk_buff *skb)
80 {
81         return ip_local_out(net, sk, skb);
82 }
83
84 static unsigned int vrf_v4_mtu(const struct dst_entry *dst)
85 {
86         /* TO-DO: return max ethernet size? */
87         return dst->dev->mtu;
88 }
89
90 static void vrf_dst_destroy(struct dst_entry *dst)
91 {
92         /* our dst lives forever - or until the device is closed */
93 }
94
95 static unsigned int vrf_default_advmss(const struct dst_entry *dst)
96 {
97         return 65535 - 40;
98 }
99
100 static struct dst_ops vrf_dst_ops = {
101         .family         = AF_INET,
102         .local_out      = vrf_ip_local_out,
103         .check          = vrf_ip_check,
104         .mtu            = vrf_v4_mtu,
105         .destroy        = vrf_dst_destroy,
106         .default_advmss = vrf_default_advmss,
107 };
108
109 /* neighbor handling is done with actual device; do not want
110  * to flip skb->dev for those ndisc packets. This really fails
111  * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
112  * a start.
113  */
114 #if IS_ENABLED(CONFIG_IPV6)
115 static bool check_ipv6_frame(const struct sk_buff *skb)
116 {
117         const struct ipv6hdr *ipv6h;
118         struct ipv6hdr _ipv6h;
119         bool rc = true;
120
121         ipv6h = skb_header_pointer(skb, 0, sizeof(_ipv6h), &_ipv6h);
122         if (!ipv6h)
123                 goto out;
124
125         if (ipv6h->nexthdr == NEXTHDR_ICMP) {
126                 const struct icmp6hdr *icmph;
127                 struct icmp6hdr _icmph;
128
129                 icmph = skb_header_pointer(skb, sizeof(_ipv6h),
130                                            sizeof(_icmph), &_icmph);
131                 if (!icmph)
132                         goto out;
133
134                 switch (icmph->icmp6_type) {
135                 case NDISC_ROUTER_SOLICITATION:
136                 case NDISC_ROUTER_ADVERTISEMENT:
137                 case NDISC_NEIGHBOUR_SOLICITATION:
138                 case NDISC_NEIGHBOUR_ADVERTISEMENT:
139                 case NDISC_REDIRECT:
140                         rc = false;
141                         break;
142                 }
143         }
144
145 out:
146         return rc;
147 }
148 #else
149 static bool check_ipv6_frame(const struct sk_buff *skb)
150 {
151         return false;
152 }
153 #endif
154
155 static bool is_ip_rx_frame(struct sk_buff *skb)
156 {
157         switch (skb->protocol) {
158         case htons(ETH_P_IP):
159                 return true;
160         case htons(ETH_P_IPV6):
161                 return check_ipv6_frame(skb);
162         }
163         return false;
164 }
165
166 static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
167 {
168         vrf_dev->stats.tx_errors++;
169         kfree_skb(skb);
170 }
171
172 /* note: already called with rcu_read_lock */
173 static rx_handler_result_t vrf_handle_frame(struct sk_buff **pskb)
174 {
175         struct sk_buff *skb = *pskb;
176
177         if (is_ip_rx_frame(skb)) {
178                 struct net_device *dev = vrf_master_get_rcu(skb->dev);
179                 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
180
181                 u64_stats_update_begin(&dstats->syncp);
182                 dstats->rx_pkts++;
183                 dstats->rx_bytes += skb->len;
184                 u64_stats_update_end(&dstats->syncp);
185
186                 skb->dev = dev;
187
188                 return RX_HANDLER_ANOTHER;
189         }
190         return RX_HANDLER_PASS;
191 }
192
193 static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
194                                                  struct rtnl_link_stats64 *stats)
195 {
196         int i;
197
198         for_each_possible_cpu(i) {
199                 const struct pcpu_dstats *dstats;
200                 u64 tbytes, tpkts, tdrops, rbytes, rpkts;
201                 unsigned int start;
202
203                 dstats = per_cpu_ptr(dev->dstats, i);
204                 do {
205                         start = u64_stats_fetch_begin_irq(&dstats->syncp);
206                         tbytes = dstats->tx_bytes;
207                         tpkts = dstats->tx_pkts;
208                         tdrops = dstats->tx_drps;
209                         rbytes = dstats->rx_bytes;
210                         rpkts = dstats->rx_pkts;
211                 } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
212                 stats->tx_bytes += tbytes;
213                 stats->tx_packets += tpkts;
214                 stats->tx_dropped += tdrops;
215                 stats->rx_bytes += rbytes;
216                 stats->rx_packets += rpkts;
217         }
218         return stats;
219 }
220
221 #if IS_ENABLED(CONFIG_IPV6)
222 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
223                                            struct net_device *dev)
224 {
225         const struct ipv6hdr *iph = ipv6_hdr(skb);
226         struct net *net = dev_net(skb->dev);
227         struct flowi6 fl6 = {
228                 /* needed to match OIF rule */
229                 .flowi6_oif = dev->ifindex,
230                 .flowi6_iif = LOOPBACK_IFINDEX,
231                 .daddr = iph->daddr,
232                 .saddr = iph->saddr,
233                 .flowlabel = ip6_flowinfo(iph),
234                 .flowi6_mark = skb->mark,
235                 .flowi6_proto = iph->nexthdr,
236                 .flowi6_flags = FLOWI_FLAG_L3MDEV_SRC | FLOWI_FLAG_SKIP_NH_OIF,
237         };
238         int ret = NET_XMIT_DROP;
239         struct dst_entry *dst;
240         struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
241
242         dst = ip6_route_output(net, NULL, &fl6);
243         if (dst == dst_null)
244                 goto err;
245
246         skb_dst_drop(skb);
247         skb_dst_set(skb, dst);
248
249         ret = ip6_local_out(net, skb->sk, skb);
250         if (unlikely(net_xmit_eval(ret)))
251                 dev->stats.tx_errors++;
252         else
253                 ret = NET_XMIT_SUCCESS;
254
255         return ret;
256 err:
257         vrf_tx_error(dev, skb);
258         return NET_XMIT_DROP;
259 }
260 #else
261 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
262                                            struct net_device *dev)
263 {
264         vrf_tx_error(dev, skb);
265         return NET_XMIT_DROP;
266 }
267 #endif
268
269 static int vrf_send_v4_prep(struct sk_buff *skb, struct flowi4 *fl4,
270                             struct net_device *vrf_dev)
271 {
272         struct rtable *rt;
273         int err = 1;
274
275         rt = ip_route_output_flow(dev_net(vrf_dev), fl4, NULL);
276         if (IS_ERR(rt))
277                 goto out;
278
279         /* TO-DO: what about broadcast ? */
280         if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
281                 ip_rt_put(rt);
282                 goto out;
283         }
284
285         skb_dst_drop(skb);
286         skb_dst_set(skb, &rt->dst);
287         err = 0;
288 out:
289         return err;
290 }
291
292 static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
293                                            struct net_device *vrf_dev)
294 {
295         struct iphdr *ip4h = ip_hdr(skb);
296         int ret = NET_XMIT_DROP;
297         struct flowi4 fl4 = {
298                 /* needed to match OIF rule */
299                 .flowi4_oif = vrf_dev->ifindex,
300                 .flowi4_iif = LOOPBACK_IFINDEX,
301                 .flowi4_tos = RT_TOS(ip4h->tos),
302                 .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_L3MDEV_SRC |
303                                 FLOWI_FLAG_SKIP_NH_OIF,
304                 .daddr = ip4h->daddr,
305         };
306
307         if (vrf_send_v4_prep(skb, &fl4, vrf_dev))
308                 goto err;
309
310         if (!ip4h->saddr) {
311                 ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
312                                                RT_SCOPE_LINK);
313         }
314
315         ret = ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
316         if (unlikely(net_xmit_eval(ret)))
317                 vrf_dev->stats.tx_errors++;
318         else
319                 ret = NET_XMIT_SUCCESS;
320
321 out:
322         return ret;
323 err:
324         vrf_tx_error(vrf_dev, skb);
325         goto out;
326 }
327
328 static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
329 {
330         /* strip the ethernet header added for pass through VRF device */
331         __skb_pull(skb, skb_network_offset(skb));
332
333         switch (skb->protocol) {
334         case htons(ETH_P_IP):
335                 return vrf_process_v4_outbound(skb, dev);
336         case htons(ETH_P_IPV6):
337                 return vrf_process_v6_outbound(skb, dev);
338         default:
339                 vrf_tx_error(dev, skb);
340                 return NET_XMIT_DROP;
341         }
342 }
343
344 static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
345 {
346         netdev_tx_t ret = is_ip_tx_frame(skb, dev);
347
348         if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
349                 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
350
351                 u64_stats_update_begin(&dstats->syncp);
352                 dstats->tx_pkts++;
353                 dstats->tx_bytes += skb->len;
354                 u64_stats_update_end(&dstats->syncp);
355         } else {
356                 this_cpu_inc(dev->dstats->tx_drps);
357         }
358
359         return ret;
360 }
361
362 #if IS_ENABLED(CONFIG_IPV6)
363 static struct dst_entry *vrf_ip6_check(struct dst_entry *dst, u32 cookie)
364 {
365         return dst;
366 }
367
368 static struct dst_ops vrf_dst_ops6 = {
369         .family         = AF_INET6,
370         .local_out      = ip6_local_out,
371         .check          = vrf_ip6_check,
372         .mtu            = vrf_v4_mtu,
373         .destroy        = vrf_dst_destroy,
374         .default_advmss = vrf_default_advmss,
375 };
376
377 static int init_dst_ops6_kmem_cachep(void)
378 {
379         vrf_dst_ops6.kmem_cachep = kmem_cache_create("vrf_ip6_dst_cache",
380                                                      sizeof(struct rt6_info),
381                                                      0,
382                                                      SLAB_HWCACHE_ALIGN,
383                                                      NULL);
384
385         if (!vrf_dst_ops6.kmem_cachep)
386                 return -ENOMEM;
387
388         return 0;
389 }
390
391 static void free_dst_ops6_kmem_cachep(void)
392 {
393         kmem_cache_destroy(vrf_dst_ops6.kmem_cachep);
394 }
395
396 static int vrf_input6(struct sk_buff *skb)
397 {
398         skb->dev->stats.rx_errors++;
399         kfree_skb(skb);
400         return 0;
401 }
402
403 /* modelled after ip6_finish_output2 */
404 static int vrf_finish_output6(struct net *net, struct sock *sk,
405                               struct sk_buff *skb)
406 {
407         struct dst_entry *dst = skb_dst(skb);
408         struct net_device *dev = dst->dev;
409         struct neighbour *neigh;
410         struct in6_addr *nexthop;
411         int ret;
412
413         skb->protocol = htons(ETH_P_IPV6);
414         skb->dev = dev;
415
416         rcu_read_lock_bh();
417         nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
418         neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
419         if (unlikely(!neigh))
420                 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
421         if (!IS_ERR(neigh)) {
422                 ret = dst_neigh_output(dst, neigh, skb);
423                 rcu_read_unlock_bh();
424                 return ret;
425         }
426         rcu_read_unlock_bh();
427
428         IP6_INC_STATS(dev_net(dst->dev),
429                       ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
430         kfree_skb(skb);
431         return -EINVAL;
432 }
433
434 /* modelled after ip6_output */
435 static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
436 {
437         return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
438                             net, sk, skb, NULL, skb_dst(skb)->dev,
439                             vrf_finish_output6,
440                             !(IP6CB(skb)->flags & IP6SKB_REROUTED));
441 }
442
443 static void vrf_rt6_destroy(struct net_vrf *vrf)
444 {
445         dst_destroy(&vrf->rt6->dst);
446         free_percpu(vrf->rt6->rt6i_pcpu);
447         vrf->rt6 = NULL;
448 }
449
450 static int vrf_rt6_create(struct net_device *dev)
451 {
452         struct net_vrf *vrf = netdev_priv(dev);
453         struct dst_entry *dst;
454         struct rt6_info *rt6;
455         int cpu;
456         int rc = -ENOMEM;
457
458         rt6 = dst_alloc(&vrf_dst_ops6, dev, 0,
459                         DST_OBSOLETE_NONE,
460                         (DST_HOST | DST_NOPOLICY | DST_NOXFRM));
461         if (!rt6)
462                 goto out;
463
464         dst = &rt6->dst;
465
466         rt6->rt6i_pcpu = alloc_percpu_gfp(struct rt6_info *, GFP_KERNEL);
467         if (!rt6->rt6i_pcpu) {
468                 dst_destroy(dst);
469                 goto out;
470         }
471         for_each_possible_cpu(cpu) {
472                 struct rt6_info **p = per_cpu_ptr(rt6->rt6i_pcpu, cpu);
473                 *p =  NULL;
474         }
475
476         memset(dst + 1, 0, sizeof(*rt6) - sizeof(*dst));
477
478         INIT_LIST_HEAD(&rt6->rt6i_siblings);
479         INIT_LIST_HEAD(&rt6->rt6i_uncached);
480
481         rt6->dst.input  = vrf_input6;
482         rt6->dst.output = vrf_output6;
483
484         rt6->rt6i_table = fib6_get_table(dev_net(dev), vrf->tb_id);
485
486         atomic_set(&rt6->dst.__refcnt, 2);
487
488         vrf->rt6 = rt6;
489         rc = 0;
490 out:
491         return rc;
492 }
493 #else
494 static int init_dst_ops6_kmem_cachep(void)
495 {
496         return 0;
497 }
498
499 static void free_dst_ops6_kmem_cachep(void)
500 {
501 }
502
503 static void vrf_rt6_destroy(struct net_vrf *vrf)
504 {
505 }
506
507 static int vrf_rt6_create(struct net_device *dev)
508 {
509         return 0;
510 }
511 #endif
512
513 /* modelled after ip_finish_output2 */
514 static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
515 {
516         struct dst_entry *dst = skb_dst(skb);
517         struct rtable *rt = (struct rtable *)dst;
518         struct net_device *dev = dst->dev;
519         unsigned int hh_len = LL_RESERVED_SPACE(dev);
520         struct neighbour *neigh;
521         u32 nexthop;
522         int ret = -EINVAL;
523
524         /* Be paranoid, rather than too clever. */
525         if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
526                 struct sk_buff *skb2;
527
528                 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
529                 if (!skb2) {
530                         ret = -ENOMEM;
531                         goto err;
532                 }
533                 if (skb->sk)
534                         skb_set_owner_w(skb2, skb->sk);
535
536                 consume_skb(skb);
537                 skb = skb2;
538         }
539
540         rcu_read_lock_bh();
541
542         nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
543         neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
544         if (unlikely(!neigh))
545                 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
546         if (!IS_ERR(neigh))
547                 ret = dst_neigh_output(dst, neigh, skb);
548
549         rcu_read_unlock_bh();
550 err:
551         if (unlikely(ret < 0))
552                 vrf_tx_error(skb->dev, skb);
553         return ret;
554 }
555
556 static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
557 {
558         struct net_device *dev = skb_dst(skb)->dev;
559
560         IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
561
562         skb->dev = dev;
563         skb->protocol = htons(ETH_P_IP);
564
565         return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
566                             net, sk, skb, NULL, dev,
567                             vrf_finish_output,
568                             !(IPCB(skb)->flags & IPSKB_REROUTED));
569 }
570
571 static void vrf_rtable_destroy(struct net_vrf *vrf)
572 {
573         struct dst_entry *dst = (struct dst_entry *)vrf->rth;
574
575         dst_destroy(dst);
576         vrf->rth = NULL;
577 }
578
579 static struct rtable *vrf_rtable_create(struct net_device *dev)
580 {
581         struct net_vrf *vrf = netdev_priv(dev);
582         struct rtable *rth;
583
584         rth = dst_alloc(&vrf_dst_ops, dev, 2,
585                         DST_OBSOLETE_NONE,
586                         (DST_HOST | DST_NOPOLICY | DST_NOXFRM));
587         if (rth) {
588                 rth->dst.output = vrf_output;
589                 rth->rt_genid   = rt_genid_ipv4(dev_net(dev));
590                 rth->rt_flags   = 0;
591                 rth->rt_type    = RTN_UNICAST;
592                 rth->rt_is_input = 0;
593                 rth->rt_iif     = 0;
594                 rth->rt_pmtu    = 0;
595                 rth->rt_gateway = 0;
596                 rth->rt_uses_gateway = 0;
597                 rth->rt_table_id = vrf->tb_id;
598                 INIT_LIST_HEAD(&rth->rt_uncached);
599                 rth->rt_uncached_list = NULL;
600         }
601
602         return rth;
603 }
604
605 /**************************** device handling ********************/
606
607 /* cycle interface to flush neighbor cache and move routes across tables */
608 static void cycle_netdev(struct net_device *dev)
609 {
610         unsigned int flags = dev->flags;
611         int ret;
612
613         if (!netif_running(dev))
614                 return;
615
616         ret = dev_change_flags(dev, flags & ~IFF_UP);
617         if (ret >= 0)
618                 ret = dev_change_flags(dev, flags);
619
620         if (ret < 0) {
621                 netdev_err(dev,
622                            "Failed to cycle device %s; route tables might be wrong!\n",
623                            dev->name);
624         }
625 }
626
627 static struct slave *__vrf_find_slave_dev(struct slave_queue *queue,
628                                           struct net_device *dev)
629 {
630         struct list_head *head = &queue->all_slaves;
631         struct slave *slave;
632
633         list_for_each_entry(slave, head, list) {
634                 if (slave->dev == dev)
635                         return slave;
636         }
637
638         return NULL;
639 }
640
641 /* inverse of __vrf_insert_slave */
642 static void __vrf_remove_slave(struct slave_queue *queue, struct slave *slave)
643 {
644         list_del(&slave->list);
645 }
646
647 static void __vrf_insert_slave(struct slave_queue *queue, struct slave *slave)
648 {
649         list_add(&slave->list, &queue->all_slaves);
650 }
651
652 static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
653 {
654         struct slave *slave = kzalloc(sizeof(*slave), GFP_KERNEL);
655         struct net_vrf *vrf = netdev_priv(dev);
656         struct slave_queue *queue = &vrf->queue;
657         int ret = -ENOMEM;
658
659         if (!slave)
660                 goto out_fail;
661
662         slave->dev = port_dev;
663
664         /* register the packet handler for slave ports */
665         ret = netdev_rx_handler_register(port_dev, vrf_handle_frame, dev);
666         if (ret) {
667                 netdev_err(port_dev,
668                            "Device %s failed to register rx_handler\n",
669                            port_dev->name);
670                 goto out_fail;
671         }
672
673         ret = netdev_master_upper_dev_link(port_dev, dev);
674         if (ret < 0)
675                 goto out_unregister;
676
677         port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
678         __vrf_insert_slave(queue, slave);
679         cycle_netdev(port_dev);
680
681         return 0;
682
683 out_unregister:
684         netdev_rx_handler_unregister(port_dev);
685 out_fail:
686         kfree(slave);
687         return ret;
688 }
689
690 static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
691 {
692         if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev))
693                 return -EINVAL;
694
695         return do_vrf_add_slave(dev, port_dev);
696 }
697
698 /* inverse of do_vrf_add_slave */
699 static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
700 {
701         struct net_vrf *vrf = netdev_priv(dev);
702         struct slave_queue *queue = &vrf->queue;
703         struct slave *slave;
704
705         netdev_upper_dev_unlink(port_dev, dev);
706         port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
707
708         netdev_rx_handler_unregister(port_dev);
709
710         cycle_netdev(port_dev);
711
712         slave = __vrf_find_slave_dev(queue, port_dev);
713         if (slave)
714                 __vrf_remove_slave(queue, slave);
715
716         kfree(slave);
717
718         return 0;
719 }
720
721 static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
722 {
723         return do_vrf_del_slave(dev, port_dev);
724 }
725
726 static void vrf_dev_uninit(struct net_device *dev)
727 {
728         struct net_vrf *vrf = netdev_priv(dev);
729         struct slave_queue *queue = &vrf->queue;
730         struct list_head *head = &queue->all_slaves;
731         struct slave *slave, *next;
732
733         vrf_rtable_destroy(vrf);
734         vrf_rt6_destroy(vrf);
735
736         list_for_each_entry_safe(slave, next, head, list)
737                 vrf_del_slave(dev, slave->dev);
738
739         free_percpu(dev->dstats);
740         dev->dstats = NULL;
741 }
742
743 static int vrf_dev_init(struct net_device *dev)
744 {
745         struct net_vrf *vrf = netdev_priv(dev);
746
747         INIT_LIST_HEAD(&vrf->queue.all_slaves);
748
749         dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
750         if (!dev->dstats)
751                 goto out_nomem;
752
753         /* create the default dst which points back to us */
754         vrf->rth = vrf_rtable_create(dev);
755         if (!vrf->rth)
756                 goto out_stats;
757
758         if (vrf_rt6_create(dev) != 0)
759                 goto out_rth;
760
761         dev->flags = IFF_MASTER | IFF_NOARP;
762
763         return 0;
764
765 out_rth:
766         vrf_rtable_destroy(vrf);
767 out_stats:
768         free_percpu(dev->dstats);
769         dev->dstats = NULL;
770 out_nomem:
771         return -ENOMEM;
772 }
773
774 static const struct net_device_ops vrf_netdev_ops = {
775         .ndo_init               = vrf_dev_init,
776         .ndo_uninit             = vrf_dev_uninit,
777         .ndo_start_xmit         = vrf_xmit,
778         .ndo_get_stats64        = vrf_get_stats64,
779         .ndo_add_slave          = vrf_add_slave,
780         .ndo_del_slave          = vrf_del_slave,
781 };
782
783 static u32 vrf_fib_table(const struct net_device *dev)
784 {
785         struct net_vrf *vrf = netdev_priv(dev);
786
787         return vrf->tb_id;
788 }
789
790 static struct rtable *vrf_get_rtable(const struct net_device *dev,
791                                      const struct flowi4 *fl4)
792 {
793         struct rtable *rth = NULL;
794
795         if (!(fl4->flowi4_flags & FLOWI_FLAG_L3MDEV_SRC)) {
796                 struct net_vrf *vrf = netdev_priv(dev);
797
798                 rth = vrf->rth;
799                 atomic_inc(&rth->dst.__refcnt);
800         }
801
802         return rth;
803 }
804
805 /* called under rcu_read_lock */
806 static int vrf_get_saddr(struct net_device *dev, struct flowi4 *fl4)
807 {
808         struct fib_result res = { .tclassid = 0 };
809         struct net *net = dev_net(dev);
810         u32 orig_tos = fl4->flowi4_tos;
811         u8 flags = fl4->flowi4_flags;
812         u8 scope = fl4->flowi4_scope;
813         u8 tos = RT_FL_TOS(fl4);
814         int rc;
815
816         if (unlikely(!fl4->daddr))
817                 return 0;
818
819         fl4->flowi4_flags |= FLOWI_FLAG_SKIP_NH_OIF;
820         fl4->flowi4_iif = LOOPBACK_IFINDEX;
821         fl4->flowi4_tos = tos & IPTOS_RT_MASK;
822         fl4->flowi4_scope = ((tos & RTO_ONLINK) ?
823                              RT_SCOPE_LINK : RT_SCOPE_UNIVERSE);
824
825         rc = fib_lookup(net, fl4, &res, 0);
826         if (!rc) {
827                 if (res.type == RTN_LOCAL)
828                         fl4->saddr = res.fi->fib_prefsrc ? : fl4->daddr;
829                 else
830                         fib_select_path(net, &res, fl4, -1);
831         }
832
833         fl4->flowi4_flags = flags;
834         fl4->flowi4_tos = orig_tos;
835         fl4->flowi4_scope = scope;
836
837         return rc;
838 }
839
840 #if IS_ENABLED(CONFIG_IPV6)
841 static struct dst_entry *vrf_get_rt6_dst(const struct net_device *dev,
842                                          const struct flowi6 *fl6)
843 {
844         struct rt6_info *rt = NULL;
845
846         if (!(fl6->flowi6_flags & FLOWI_FLAG_L3MDEV_SRC)) {
847                 struct net_vrf *vrf = netdev_priv(dev);
848
849                 rt = vrf->rt6;
850                 atomic_inc(&rt->dst.__refcnt);
851         }
852
853         return (struct dst_entry *)rt;
854 }
855 #endif
856
857 static const struct l3mdev_ops vrf_l3mdev_ops = {
858         .l3mdev_fib_table       = vrf_fib_table,
859         .l3mdev_get_rtable      = vrf_get_rtable,
860         .l3mdev_get_saddr       = vrf_get_saddr,
861 #if IS_ENABLED(CONFIG_IPV6)
862         .l3mdev_get_rt6_dst     = vrf_get_rt6_dst,
863 #endif
864 };
865
866 static void vrf_get_drvinfo(struct net_device *dev,
867                             struct ethtool_drvinfo *info)
868 {
869         strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
870         strlcpy(info->version, DRV_VERSION, sizeof(info->version));
871 }
872
873 static const struct ethtool_ops vrf_ethtool_ops = {
874         .get_drvinfo    = vrf_get_drvinfo,
875 };
876
877 static void vrf_setup(struct net_device *dev)
878 {
879         ether_setup(dev);
880
881         /* Initialize the device structure. */
882         dev->netdev_ops = &vrf_netdev_ops;
883         dev->l3mdev_ops = &vrf_l3mdev_ops;
884         dev->ethtool_ops = &vrf_ethtool_ops;
885         dev->destructor = free_netdev;
886
887         /* Fill in device structure with ethernet-generic values. */
888         eth_hw_addr_random(dev);
889
890         /* don't acquire vrf device's netif_tx_lock when transmitting */
891         dev->features |= NETIF_F_LLTX;
892
893         /* don't allow vrf devices to change network namespaces. */
894         dev->features |= NETIF_F_NETNS_LOCAL;
895 }
896
897 static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
898 {
899         if (tb[IFLA_ADDRESS]) {
900                 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
901                         return -EINVAL;
902                 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
903                         return -EADDRNOTAVAIL;
904         }
905         return 0;
906 }
907
908 static void vrf_dellink(struct net_device *dev, struct list_head *head)
909 {
910         unregister_netdevice_queue(dev, head);
911 }
912
913 static int vrf_newlink(struct net *src_net, struct net_device *dev,
914                        struct nlattr *tb[], struct nlattr *data[])
915 {
916         struct net_vrf *vrf = netdev_priv(dev);
917
918         if (!data || !data[IFLA_VRF_TABLE])
919                 return -EINVAL;
920
921         vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
922
923         dev->priv_flags |= IFF_L3MDEV_MASTER;
924
925         return register_netdevice(dev);
926 }
927
928 static size_t vrf_nl_getsize(const struct net_device *dev)
929 {
930         return nla_total_size(sizeof(u32));  /* IFLA_VRF_TABLE */
931 }
932
933 static int vrf_fillinfo(struct sk_buff *skb,
934                         const struct net_device *dev)
935 {
936         struct net_vrf *vrf = netdev_priv(dev);
937
938         return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
939 }
940
941 static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
942         [IFLA_VRF_TABLE] = { .type = NLA_U32 },
943 };
944
945 static struct rtnl_link_ops vrf_link_ops __read_mostly = {
946         .kind           = DRV_NAME,
947         .priv_size      = sizeof(struct net_vrf),
948
949         .get_size       = vrf_nl_getsize,
950         .policy         = vrf_nl_policy,
951         .validate       = vrf_validate,
952         .fill_info      = vrf_fillinfo,
953
954         .newlink        = vrf_newlink,
955         .dellink        = vrf_dellink,
956         .setup          = vrf_setup,
957         .maxtype        = IFLA_VRF_MAX,
958 };
959
960 static int vrf_device_event(struct notifier_block *unused,
961                             unsigned long event, void *ptr)
962 {
963         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
964
965         /* only care about unregister events to drop slave references */
966         if (event == NETDEV_UNREGISTER) {
967                 struct net_device *vrf_dev;
968
969                 if (!netif_is_l3_slave(dev))
970                         goto out;
971
972                 vrf_dev = netdev_master_upper_dev_get(dev);
973                 vrf_del_slave(vrf_dev, dev);
974         }
975 out:
976         return NOTIFY_DONE;
977 }
978
979 static struct notifier_block vrf_notifier_block __read_mostly = {
980         .notifier_call = vrf_device_event,
981 };
982
983 static int __init vrf_init_module(void)
984 {
985         int rc;
986
987         vrf_dst_ops.kmem_cachep =
988                 kmem_cache_create("vrf_ip_dst_cache",
989                                   sizeof(struct rtable), 0,
990                                   SLAB_HWCACHE_ALIGN,
991                                   NULL);
992
993         if (!vrf_dst_ops.kmem_cachep)
994                 return -ENOMEM;
995
996         rc = init_dst_ops6_kmem_cachep();
997         if (rc != 0)
998                 goto error2;
999
1000         register_netdevice_notifier(&vrf_notifier_block);
1001
1002         rc = rtnl_link_register(&vrf_link_ops);
1003         if (rc < 0)
1004                 goto error;
1005
1006         return 0;
1007
1008 error:
1009         unregister_netdevice_notifier(&vrf_notifier_block);
1010         free_dst_ops6_kmem_cachep();
1011 error2:
1012         kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
1013         return rc;
1014 }
1015
1016 static void __exit vrf_cleanup_module(void)
1017 {
1018         rtnl_link_unregister(&vrf_link_ops);
1019         unregister_netdevice_notifier(&vrf_notifier_block);
1020         kmem_cache_destroy(vrf_dst_ops.kmem_cachep);
1021         free_dst_ops6_kmem_cachep();
1022 }
1023
1024 module_init(vrf_init_module);
1025 module_exit(vrf_cleanup_module);
1026 MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
1027 MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
1028 MODULE_LICENSE("GPL");
1029 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
1030 MODULE_VERSION(DRV_VERSION);