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