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 #include <net/fib_rules.h>
39 #include <net/netns/generic.h>
40
41 #define DRV_NAME "vrf"
42 #define DRV_VERSION "1.0"
43
44 #define FIB_RULE_PREF 1000 /* default preference for FIB rules */
45
46 static unsigned int vrf_net_id;
47
48 struct net_vrf {
49 struct rtable __rcu *rth;
50 struct rt6_info __rcu *rt6;
51 u32 tb_id;
52 };
53
54 struct pcpu_dstats {
55 u64 tx_pkts;
56 u64 tx_bytes;
57 u64 tx_drps;
58 u64 rx_pkts;
59 u64 rx_bytes;
60 u64 rx_drps;
61 struct u64_stats_sync syncp;
62 };
63
vrf_rx_stats(struct net_device * dev,int len)64 static void vrf_rx_stats(struct net_device *dev, int len)
65 {
66 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
67
68 u64_stats_update_begin(&dstats->syncp);
69 dstats->rx_pkts++;
70 dstats->rx_bytes += len;
71 u64_stats_update_end(&dstats->syncp);
72 }
73
vrf_tx_error(struct net_device * vrf_dev,struct sk_buff * skb)74 static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
75 {
76 vrf_dev->stats.tx_errors++;
77 kfree_skb(skb);
78 }
79
vrf_get_stats64(struct net_device * dev,struct rtnl_link_stats64 * stats)80 static void vrf_get_stats64(struct net_device *dev,
81 struct rtnl_link_stats64 *stats)
82 {
83 int i;
84
85 for_each_possible_cpu(i) {
86 const struct pcpu_dstats *dstats;
87 u64 tbytes, tpkts, tdrops, rbytes, rpkts;
88 unsigned int start;
89
90 dstats = per_cpu_ptr(dev->dstats, i);
91 do {
92 start = u64_stats_fetch_begin_irq(&dstats->syncp);
93 tbytes = dstats->tx_bytes;
94 tpkts = dstats->tx_pkts;
95 tdrops = dstats->tx_drps;
96 rbytes = dstats->rx_bytes;
97 rpkts = dstats->rx_pkts;
98 } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
99 stats->tx_bytes += tbytes;
100 stats->tx_packets += tpkts;
101 stats->tx_dropped += tdrops;
102 stats->rx_bytes += rbytes;
103 stats->rx_packets += rpkts;
104 }
105 }
106
107 /* by default VRF devices do not have a qdisc and are expected
108 * to be created with only a single queue.
109 */
qdisc_tx_is_default(const struct net_device * dev)110 static bool qdisc_tx_is_default(const struct net_device *dev)
111 {
112 struct netdev_queue *txq;
113 struct Qdisc *qdisc;
114
115 if (dev->num_tx_queues > 1)
116 return false;
117
118 txq = netdev_get_tx_queue(dev, 0);
119 qdisc = rcu_access_pointer(txq->qdisc);
120
121 return !qdisc->enqueue;
122 }
123
124 /* Local traffic destined to local address. Reinsert the packet to rx
125 * path, similar to loopback handling.
126 */
vrf_local_xmit(struct sk_buff * skb,struct net_device * dev,struct dst_entry * dst)127 static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev,
128 struct dst_entry *dst)
129 {
130 int len = skb->len;
131
132 skb_orphan(skb);
133
134 skb_dst_set(skb, dst);
135 skb_dst_force(skb);
136
137 /* set pkt_type to avoid skb hitting packet taps twice -
138 * once on Tx and again in Rx processing
139 */
140 skb->pkt_type = PACKET_LOOPBACK;
141
142 skb->protocol = eth_type_trans(skb, dev);
143
144 if (likely(netif_rx(skb) == NET_RX_SUCCESS))
145 vrf_rx_stats(dev, len);
146 else
147 this_cpu_inc(dev->dstats->rx_drps);
148
149 return NETDEV_TX_OK;
150 }
151
152 #if IS_ENABLED(CONFIG_IPV6)
vrf_ip6_local_out(struct net * net,struct sock * sk,struct sk_buff * skb)153 static int vrf_ip6_local_out(struct net *net, struct sock *sk,
154 struct sk_buff *skb)
155 {
156 int err;
157
158 err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net,
159 sk, skb, NULL, skb_dst(skb)->dev, dst_output);
160
161 if (likely(err == 1))
162 err = dst_output(net, sk, skb);
163
164 return err;
165 }
166
vrf_process_v6_outbound(struct sk_buff * skb,struct net_device * dev)167 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
168 struct net_device *dev)
169 {
170 const struct ipv6hdr *iph;
171 struct net *net = dev_net(skb->dev);
172 struct flowi6 fl6;
173 int ret = NET_XMIT_DROP;
174 struct dst_entry *dst;
175 struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
176
177 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr)))
178 goto err;
179
180 iph = ipv6_hdr(skb);
181
182 memset(&fl6, 0, sizeof(fl6));
183 /* needed to match OIF rule */
184 fl6.flowi6_oif = dev->ifindex;
185 fl6.flowi6_iif = LOOPBACK_IFINDEX;
186 fl6.daddr = iph->daddr;
187 fl6.saddr = iph->saddr;
188 fl6.flowlabel = ip6_flowinfo(iph);
189 fl6.flowi6_mark = skb->mark;
190 fl6.flowi6_proto = iph->nexthdr;
191 fl6.flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF;
192
193 dst = ip6_route_output(net, NULL, &fl6);
194 if (dst == dst_null)
195 goto err;
196
197 skb_dst_drop(skb);
198
199 /* if dst.dev is loopback or the VRF device again this is locally
200 * originated traffic destined to a local address. Short circuit
201 * to Rx path
202 */
203 if (dst->dev == dev)
204 return vrf_local_xmit(skb, dev, dst);
205
206 skb_dst_set(skb, dst);
207
208 /* strip the ethernet header added for pass through VRF device */
209 __skb_pull(skb, skb_network_offset(skb));
210
211 ret = vrf_ip6_local_out(net, skb->sk, skb);
212 if (unlikely(net_xmit_eval(ret)))
213 dev->stats.tx_errors++;
214 else
215 ret = NET_XMIT_SUCCESS;
216
217 return ret;
218 err:
219 vrf_tx_error(dev, skb);
220 return NET_XMIT_DROP;
221 }
222 #else
vrf_process_v6_outbound(struct sk_buff * skb,struct net_device * dev)223 static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
224 struct net_device *dev)
225 {
226 vrf_tx_error(dev, skb);
227 return NET_XMIT_DROP;
228 }
229 #endif
230
231 /* based on ip_local_out; can't use it b/c the dst is switched pointing to us */
vrf_ip_local_out(struct net * net,struct sock * sk,struct sk_buff * skb)232 static int vrf_ip_local_out(struct net *net, struct sock *sk,
233 struct sk_buff *skb)
234 {
235 int err;
236
237 err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
238 skb, NULL, skb_dst(skb)->dev, dst_output);
239 if (likely(err == 1))
240 err = dst_output(net, sk, skb);
241
242 return err;
243 }
244
vrf_process_v4_outbound(struct sk_buff * skb,struct net_device * vrf_dev)245 static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
246 struct net_device *vrf_dev)
247 {
248 struct iphdr *ip4h;
249 int ret = NET_XMIT_DROP;
250 struct flowi4 fl4;
251 struct net *net = dev_net(vrf_dev);
252 struct rtable *rt;
253
254 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr)))
255 goto err;
256
257 ip4h = ip_hdr(skb);
258
259 memset(&fl4, 0, sizeof(fl4));
260 /* needed to match OIF rule */
261 fl4.flowi4_oif = vrf_dev->ifindex;
262 fl4.flowi4_iif = LOOPBACK_IFINDEX;
263 fl4.flowi4_tos = RT_TOS(ip4h->tos);
264 fl4.flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_SKIP_NH_OIF;
265 fl4.flowi4_proto = ip4h->protocol;
266 fl4.daddr = ip4h->daddr;
267 fl4.saddr = ip4h->saddr;
268
269 rt = ip_route_output_flow(net, &fl4, NULL);
270 if (IS_ERR(rt))
271 goto err;
272
273 skb_dst_drop(skb);
274
275 /* if dst.dev is loopback or the VRF device again this is locally
276 * originated traffic destined to a local address. Short circuit
277 * to Rx path
278 */
279 if (rt->dst.dev == vrf_dev)
280 return vrf_local_xmit(skb, vrf_dev, &rt->dst);
281
282 skb_dst_set(skb, &rt->dst);
283
284 /* strip the ethernet header added for pass through VRF device */
285 __skb_pull(skb, skb_network_offset(skb));
286
287 if (!ip4h->saddr) {
288 ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
289 RT_SCOPE_LINK);
290 }
291
292 ret = vrf_ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
293 if (unlikely(net_xmit_eval(ret)))
294 vrf_dev->stats.tx_errors++;
295 else
296 ret = NET_XMIT_SUCCESS;
297
298 out:
299 return ret;
300 err:
301 vrf_tx_error(vrf_dev, skb);
302 goto out;
303 }
304
is_ip_tx_frame(struct sk_buff * skb,struct net_device * dev)305 static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
306 {
307 switch (skb->protocol) {
308 case htons(ETH_P_IP):
309 return vrf_process_v4_outbound(skb, dev);
310 case htons(ETH_P_IPV6):
311 return vrf_process_v6_outbound(skb, dev);
312 default:
313 vrf_tx_error(dev, skb);
314 return NET_XMIT_DROP;
315 }
316 }
317
vrf_xmit(struct sk_buff * skb,struct net_device * dev)318 static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
319 {
320 int len = skb->len;
321 netdev_tx_t ret = is_ip_tx_frame(skb, dev);
322
323 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
324 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
325
326 u64_stats_update_begin(&dstats->syncp);
327 dstats->tx_pkts++;
328 dstats->tx_bytes += len;
329 u64_stats_update_end(&dstats->syncp);
330 } else {
331 this_cpu_inc(dev->dstats->tx_drps);
332 }
333
334 return ret;
335 }
336
vrf_finish_direct(struct net * net,struct sock * sk,struct sk_buff * skb)337 static int vrf_finish_direct(struct net *net, struct sock *sk,
338 struct sk_buff *skb)
339 {
340 struct net_device *vrf_dev = skb->dev;
341
342 if (!list_empty(&vrf_dev->ptype_all) &&
343 likely(skb_headroom(skb) >= ETH_HLEN)) {
344 struct ethhdr *eth = skb_push(skb, ETH_HLEN);
345
346 ether_addr_copy(eth->h_source, vrf_dev->dev_addr);
347 eth_zero_addr(eth->h_dest);
348 eth->h_proto = skb->protocol;
349
350 rcu_read_lock_bh();
351 dev_queue_xmit_nit(skb, vrf_dev);
352 rcu_read_unlock_bh();
353
354 skb_pull(skb, ETH_HLEN);
355 }
356
357 return 1;
358 }
359
360 #if IS_ENABLED(CONFIG_IPV6)
361 /* modelled after ip6_finish_output2 */
vrf_finish_output6(struct net * net,struct sock * sk,struct sk_buff * skb)362 static int vrf_finish_output6(struct net *net, struct sock *sk,
363 struct sk_buff *skb)
364 {
365 struct dst_entry *dst = skb_dst(skb);
366 struct net_device *dev = dst->dev;
367 struct neighbour *neigh;
368 struct in6_addr *nexthop;
369 int ret;
370
371 nf_reset(skb);
372
373 skb->protocol = htons(ETH_P_IPV6);
374 skb->dev = dev;
375
376 rcu_read_lock_bh();
377 nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
378 neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
379 if (unlikely(!neigh))
380 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
381 if (!IS_ERR(neigh)) {
382 sock_confirm_neigh(skb, neigh);
383 ret = neigh_output(neigh, skb);
384 rcu_read_unlock_bh();
385 return ret;
386 }
387 rcu_read_unlock_bh();
388
389 IP6_INC_STATS(dev_net(dst->dev),
390 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
391 kfree_skb(skb);
392 return -EINVAL;
393 }
394
395 /* modelled after ip6_output */
vrf_output6(struct net * net,struct sock * sk,struct sk_buff * skb)396 static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
397 {
398 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
399 net, sk, skb, NULL, skb_dst(skb)->dev,
400 vrf_finish_output6,
401 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
402 }
403
404 /* set dst on skb to send packet to us via dev_xmit path. Allows
405 * packet to go through device based features such as qdisc, netfilter
406 * hooks and packet sockets with skb->dev set to vrf device.
407 */
vrf_ip6_out_redirect(struct net_device * vrf_dev,struct sk_buff * skb)408 static struct sk_buff *vrf_ip6_out_redirect(struct net_device *vrf_dev,
409 struct sk_buff *skb)
410 {
411 struct net_vrf *vrf = netdev_priv(vrf_dev);
412 struct dst_entry *dst = NULL;
413 struct rt6_info *rt6;
414
415 rcu_read_lock();
416
417 rt6 = rcu_dereference(vrf->rt6);
418 if (likely(rt6)) {
419 dst = &rt6->dst;
420 dst_hold(dst);
421 }
422
423 rcu_read_unlock();
424
425 if (unlikely(!dst)) {
426 vrf_tx_error(vrf_dev, skb);
427 return NULL;
428 }
429
430 skb_dst_drop(skb);
431 skb_dst_set(skb, dst);
432
433 return skb;
434 }
435
vrf_output6_direct(struct net * net,struct sock * sk,struct sk_buff * skb)436 static int vrf_output6_direct(struct net *net, struct sock *sk,
437 struct sk_buff *skb)
438 {
439 skb->protocol = htons(ETH_P_IPV6);
440
441 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
442 net, sk, skb, NULL, skb->dev,
443 vrf_finish_direct,
444 !(IPCB(skb)->flags & IPSKB_REROUTED));
445 }
446
vrf_ip6_out_direct(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb)447 static struct sk_buff *vrf_ip6_out_direct(struct net_device *vrf_dev,
448 struct sock *sk,
449 struct sk_buff *skb)
450 {
451 struct net *net = dev_net(vrf_dev);
452 int err;
453
454 skb->dev = vrf_dev;
455
456 err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk,
457 skb, NULL, vrf_dev, vrf_output6_direct);
458
459 if (likely(err == 1))
460 err = vrf_output6_direct(net, sk, skb);
461
462 /* reset skb device */
463 if (likely(err == 1))
464 nf_reset(skb);
465 else
466 skb = NULL;
467
468 return skb;
469 }
470
vrf_ip6_out(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb)471 static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
472 struct sock *sk,
473 struct sk_buff *skb)
474 {
475 /* don't divert link scope packets */
476 if (rt6_need_strict(&ipv6_hdr(skb)->daddr))
477 return skb;
478
479 if (qdisc_tx_is_default(vrf_dev))
480 return vrf_ip6_out_direct(vrf_dev, sk, skb);
481
482 return vrf_ip6_out_redirect(vrf_dev, skb);
483 }
484
485 /* holding rtnl */
vrf_rt6_release(struct net_device * dev,struct net_vrf * vrf)486 static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
487 {
488 struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
489 struct net *net = dev_net(dev);
490 struct dst_entry *dst;
491
492 RCU_INIT_POINTER(vrf->rt6, NULL);
493 synchronize_rcu();
494
495 /* move dev in dst's to loopback so this VRF device can be deleted
496 * - based on dst_ifdown
497 */
498 if (rt6) {
499 dst = &rt6->dst;
500 dev_put(dst->dev);
501 dst->dev = net->loopback_dev;
502 dev_hold(dst->dev);
503 dst_release(dst);
504 }
505 }
506
vrf_rt6_create(struct net_device * dev)507 static int vrf_rt6_create(struct net_device *dev)
508 {
509 int flags = DST_HOST | DST_NOPOLICY | DST_NOXFRM;
510 struct net_vrf *vrf = netdev_priv(dev);
511 struct net *net = dev_net(dev);
512 struct fib6_table *rt6i_table;
513 struct rt6_info *rt6;
514 int rc = -ENOMEM;
515
516 /* IPv6 can be CONFIG enabled and then disabled runtime */
517 if (!ipv6_mod_enabled())
518 return 0;
519
520 rt6i_table = fib6_new_table(net, vrf->tb_id);
521 if (!rt6i_table)
522 goto out;
523
524 /* create a dst for routing packets out a VRF device */
525 rt6 = ip6_dst_alloc(net, dev, flags);
526 if (!rt6)
527 goto out;
528
529 rt6->rt6i_table = rt6i_table;
530 rt6->dst.output = vrf_output6;
531
532 rcu_assign_pointer(vrf->rt6, rt6);
533
534 rc = 0;
535 out:
536 return rc;
537 }
538 #else
vrf_ip6_out(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb)539 static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
540 struct sock *sk,
541 struct sk_buff *skb)
542 {
543 return skb;
544 }
545
vrf_rt6_release(struct net_device * dev,struct net_vrf * vrf)546 static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
547 {
548 }
549
vrf_rt6_create(struct net_device * dev)550 static int vrf_rt6_create(struct net_device *dev)
551 {
552 return 0;
553 }
554 #endif
555
556 /* modelled after ip_finish_output2 */
vrf_finish_output(struct net * net,struct sock * sk,struct sk_buff * skb)557 static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
558 {
559 struct dst_entry *dst = skb_dst(skb);
560 struct rtable *rt = (struct rtable *)dst;
561 struct net_device *dev = dst->dev;
562 unsigned int hh_len = LL_RESERVED_SPACE(dev);
563 struct neighbour *neigh;
564 u32 nexthop;
565 int ret = -EINVAL;
566
567 nf_reset(skb);
568
569 /* Be paranoid, rather than too clever. */
570 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
571 struct sk_buff *skb2;
572
573 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
574 if (!skb2) {
575 ret = -ENOMEM;
576 goto err;
577 }
578 if (skb->sk)
579 skb_set_owner_w(skb2, skb->sk);
580
581 consume_skb(skb);
582 skb = skb2;
583 }
584
585 rcu_read_lock_bh();
586
587 nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
588 neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
589 if (unlikely(!neigh))
590 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
591 if (!IS_ERR(neigh)) {
592 sock_confirm_neigh(skb, neigh);
593 ret = neigh_output(neigh, skb);
594 rcu_read_unlock_bh();
595 return ret;
596 }
597
598 rcu_read_unlock_bh();
599 err:
600 vrf_tx_error(skb->dev, skb);
601 return ret;
602 }
603
vrf_output(struct net * net,struct sock * sk,struct sk_buff * skb)604 static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
605 {
606 struct net_device *dev = skb_dst(skb)->dev;
607
608 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
609
610 skb->dev = dev;
611 skb->protocol = htons(ETH_P_IP);
612
613 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
614 net, sk, skb, NULL, dev,
615 vrf_finish_output,
616 !(IPCB(skb)->flags & IPSKB_REROUTED));
617 }
618
619 /* set dst on skb to send packet to us via dev_xmit path. Allows
620 * packet to go through device based features such as qdisc, netfilter
621 * hooks and packet sockets with skb->dev set to vrf device.
622 */
vrf_ip_out_redirect(struct net_device * vrf_dev,struct sk_buff * skb)623 static struct sk_buff *vrf_ip_out_redirect(struct net_device *vrf_dev,
624 struct sk_buff *skb)
625 {
626 struct net_vrf *vrf = netdev_priv(vrf_dev);
627 struct dst_entry *dst = NULL;
628 struct rtable *rth;
629
630 rcu_read_lock();
631
632 rth = rcu_dereference(vrf->rth);
633 if (likely(rth)) {
634 dst = &rth->dst;
635 dst_hold(dst);
636 }
637
638 rcu_read_unlock();
639
640 if (unlikely(!dst)) {
641 vrf_tx_error(vrf_dev, skb);
642 return NULL;
643 }
644
645 skb_dst_drop(skb);
646 skb_dst_set(skb, dst);
647
648 return skb;
649 }
650
vrf_output_direct(struct net * net,struct sock * sk,struct sk_buff * skb)651 static int vrf_output_direct(struct net *net, struct sock *sk,
652 struct sk_buff *skb)
653 {
654 skb->protocol = htons(ETH_P_IP);
655
656 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
657 net, sk, skb, NULL, skb->dev,
658 vrf_finish_direct,
659 !(IPCB(skb)->flags & IPSKB_REROUTED));
660 }
661
vrf_ip_out_direct(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb)662 static struct sk_buff *vrf_ip_out_direct(struct net_device *vrf_dev,
663 struct sock *sk,
664 struct sk_buff *skb)
665 {
666 struct net *net = dev_net(vrf_dev);
667 int err;
668
669 skb->dev = vrf_dev;
670
671 err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
672 skb, NULL, vrf_dev, vrf_output_direct);
673
674 if (likely(err == 1))
675 err = vrf_output_direct(net, sk, skb);
676
677 /* reset skb device */
678 if (likely(err == 1))
679 nf_reset(skb);
680 else
681 skb = NULL;
682
683 return skb;
684 }
685
vrf_ip_out(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb)686 static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev,
687 struct sock *sk,
688 struct sk_buff *skb)
689 {
690 /* don't divert multicast or local broadcast */
691 if (ipv4_is_multicast(ip_hdr(skb)->daddr) ||
692 ipv4_is_lbcast(ip_hdr(skb)->daddr))
693 return skb;
694
695 if (qdisc_tx_is_default(vrf_dev))
696 return vrf_ip_out_direct(vrf_dev, sk, skb);
697
698 return vrf_ip_out_redirect(vrf_dev, skb);
699 }
700
701 /* called with rcu lock held */
vrf_l3_out(struct net_device * vrf_dev,struct sock * sk,struct sk_buff * skb,u16 proto)702 static struct sk_buff *vrf_l3_out(struct net_device *vrf_dev,
703 struct sock *sk,
704 struct sk_buff *skb,
705 u16 proto)
706 {
707 switch (proto) {
708 case AF_INET:
709 return vrf_ip_out(vrf_dev, sk, skb);
710 case AF_INET6:
711 return vrf_ip6_out(vrf_dev, sk, skb);
712 }
713
714 return skb;
715 }
716
717 /* holding rtnl */
vrf_rtable_release(struct net_device * dev,struct net_vrf * vrf)718 static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf)
719 {
720 struct rtable *rth = rtnl_dereference(vrf->rth);
721 struct net *net = dev_net(dev);
722 struct dst_entry *dst;
723
724 RCU_INIT_POINTER(vrf->rth, NULL);
725 synchronize_rcu();
726
727 /* move dev in dst's to loopback so this VRF device can be deleted
728 * - based on dst_ifdown
729 */
730 if (rth) {
731 dst = &rth->dst;
732 dev_put(dst->dev);
733 dst->dev = net->loopback_dev;
734 dev_hold(dst->dev);
735 dst_release(dst);
736 }
737 }
738
vrf_rtable_create(struct net_device * dev)739 static int vrf_rtable_create(struct net_device *dev)
740 {
741 struct net_vrf *vrf = netdev_priv(dev);
742 struct rtable *rth;
743
744 if (!fib_new_table(dev_net(dev), vrf->tb_id))
745 return -ENOMEM;
746
747 /* create a dst for routing packets out through a VRF device */
748 rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1, 1, 0);
749 if (!rth)
750 return -ENOMEM;
751
752 rth->dst.output = vrf_output;
753 rth->rt_table_id = vrf->tb_id;
754
755 rcu_assign_pointer(vrf->rth, rth);
756
757 return 0;
758 }
759
760 /**************************** device handling ********************/
761
762 /* cycle interface to flush neighbor cache and move routes across tables */
cycle_netdev(struct net_device * dev)763 static void cycle_netdev(struct net_device *dev)
764 {
765 unsigned int flags = dev->flags;
766 int ret;
767
768 if (!netif_running(dev))
769 return;
770
771 ret = dev_change_flags(dev, flags & ~IFF_UP);
772 if (ret >= 0)
773 ret = dev_change_flags(dev, flags);
774
775 if (ret < 0) {
776 netdev_err(dev,
777 "Failed to cycle device %s; route tables might be wrong!\n",
778 dev->name);
779 }
780 }
781
do_vrf_add_slave(struct net_device * dev,struct net_device * port_dev)782 static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
783 {
784 int ret;
785
786 /* do not allow loopback device to be enslaved to a VRF.
787 * The vrf device acts as the loopback for the vrf.
788 */
789 if (port_dev == dev_net(dev)->loopback_dev)
790 return -EOPNOTSUPP;
791
792 port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
793 ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL);
794 if (ret < 0)
795 goto err;
796
797 cycle_netdev(port_dev);
798
799 return 0;
800
801 err:
802 port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
803 return ret;
804 }
805
vrf_add_slave(struct net_device * dev,struct net_device * port_dev)806 static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
807 {
808 if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev))
809 return -EINVAL;
810
811 return do_vrf_add_slave(dev, port_dev);
812 }
813
814 /* inverse of do_vrf_add_slave */
do_vrf_del_slave(struct net_device * dev,struct net_device * port_dev)815 static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
816 {
817 netdev_upper_dev_unlink(port_dev, dev);
818 port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
819
820 cycle_netdev(port_dev);
821
822 return 0;
823 }
824
vrf_del_slave(struct net_device * dev,struct net_device * port_dev)825 static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
826 {
827 return do_vrf_del_slave(dev, port_dev);
828 }
829
vrf_dev_uninit(struct net_device * dev)830 static void vrf_dev_uninit(struct net_device *dev)
831 {
832 struct net_vrf *vrf = netdev_priv(dev);
833
834 vrf_rtable_release(dev, vrf);
835 vrf_rt6_release(dev, vrf);
836
837 free_percpu(dev->dstats);
838 dev->dstats = NULL;
839 }
840
vrf_dev_init(struct net_device * dev)841 static int vrf_dev_init(struct net_device *dev)
842 {
843 struct net_vrf *vrf = netdev_priv(dev);
844
845 dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
846 if (!dev->dstats)
847 goto out_nomem;
848
849 /* create the default dst which points back to us */
850 if (vrf_rtable_create(dev) != 0)
851 goto out_stats;
852
853 if (vrf_rt6_create(dev) != 0)
854 goto out_rth;
855
856 dev->flags = IFF_MASTER | IFF_NOARP;
857
858 /* MTU is irrelevant for VRF device; set to 64k similar to lo */
859 dev->mtu = 64 * 1024;
860
861 /* similarly, oper state is irrelevant; set to up to avoid confusion */
862 dev->operstate = IF_OPER_UP;
863 netdev_lockdep_set_classes(dev);
864 return 0;
865
866 out_rth:
867 vrf_rtable_release(dev, vrf);
868 out_stats:
869 free_percpu(dev->dstats);
870 dev->dstats = NULL;
871 out_nomem:
872 return -ENOMEM;
873 }
874
875 static const struct net_device_ops vrf_netdev_ops = {
876 .ndo_init = vrf_dev_init,
877 .ndo_uninit = vrf_dev_uninit,
878 .ndo_start_xmit = vrf_xmit,
879 .ndo_get_stats64 = vrf_get_stats64,
880 .ndo_add_slave = vrf_add_slave,
881 .ndo_del_slave = vrf_del_slave,
882 };
883
vrf_fib_table(const struct net_device * dev)884 static u32 vrf_fib_table(const struct net_device *dev)
885 {
886 struct net_vrf *vrf = netdev_priv(dev);
887
888 return vrf->tb_id;
889 }
890
vrf_rcv_finish(struct net * net,struct sock * sk,struct sk_buff * skb)891 static int vrf_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
892 {
893 kfree_skb(skb);
894 return 0;
895 }
896
vrf_rcv_nfhook(u8 pf,unsigned int hook,struct sk_buff * skb,struct net_device * dev)897 static struct sk_buff *vrf_rcv_nfhook(u8 pf, unsigned int hook,
898 struct sk_buff *skb,
899 struct net_device *dev)
900 {
901 struct net *net = dev_net(dev);
902
903 if (nf_hook(pf, hook, net, NULL, skb, dev, NULL, vrf_rcv_finish) != 1)
904 skb = NULL; /* kfree_skb(skb) handled by nf code */
905
906 return skb;
907 }
908
909 #if IS_ENABLED(CONFIG_IPV6)
910 /* neighbor handling is done with actual device; do not want
911 * to flip skb->dev for those ndisc packets. This really fails
912 * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
913 * a start.
914 */
ipv6_ndisc_frame(const struct sk_buff * skb)915 static bool ipv6_ndisc_frame(const struct sk_buff *skb)
916 {
917 const struct ipv6hdr *iph = ipv6_hdr(skb);
918 bool rc = false;
919
920 if (iph->nexthdr == NEXTHDR_ICMP) {
921 const struct icmp6hdr *icmph;
922 struct icmp6hdr _icmph;
923
924 icmph = skb_header_pointer(skb, sizeof(*iph),
925 sizeof(_icmph), &_icmph);
926 if (!icmph)
927 goto out;
928
929 switch (icmph->icmp6_type) {
930 case NDISC_ROUTER_SOLICITATION:
931 case NDISC_ROUTER_ADVERTISEMENT:
932 case NDISC_NEIGHBOUR_SOLICITATION:
933 case NDISC_NEIGHBOUR_ADVERTISEMENT:
934 case NDISC_REDIRECT:
935 rc = true;
936 break;
937 }
938 }
939
940 out:
941 return rc;
942 }
943
vrf_ip6_route_lookup(struct net * net,const struct net_device * dev,struct flowi6 * fl6,int ifindex,int flags)944 static struct rt6_info *vrf_ip6_route_lookup(struct net *net,
945 const struct net_device *dev,
946 struct flowi6 *fl6,
947 int ifindex,
948 int flags)
949 {
950 struct net_vrf *vrf = netdev_priv(dev);
951 struct fib6_table *table = NULL;
952 struct rt6_info *rt6;
953
954 rcu_read_lock();
955
956 /* fib6_table does not have a refcnt and can not be freed */
957 rt6 = rcu_dereference(vrf->rt6);
958 if (likely(rt6))
959 table = rt6->rt6i_table;
960
961 rcu_read_unlock();
962
963 if (!table)
964 return NULL;
965
966 return ip6_pol_route(net, table, ifindex, fl6, flags);
967 }
968
vrf_ip6_input_dst(struct sk_buff * skb,struct net_device * vrf_dev,int ifindex)969 static void vrf_ip6_input_dst(struct sk_buff *skb, struct net_device *vrf_dev,
970 int ifindex)
971 {
972 const struct ipv6hdr *iph = ipv6_hdr(skb);
973 struct flowi6 fl6 = {
974 .flowi6_iif = ifindex,
975 .flowi6_mark = skb->mark,
976 .flowi6_proto = iph->nexthdr,
977 .daddr = iph->daddr,
978 .saddr = iph->saddr,
979 .flowlabel = ip6_flowinfo(iph),
980 };
981 struct net *net = dev_net(vrf_dev);
982 struct rt6_info *rt6;
983
984 rt6 = vrf_ip6_route_lookup(net, vrf_dev, &fl6, ifindex,
985 RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_IFACE);
986 if (unlikely(!rt6))
987 return;
988
989 if (unlikely(&rt6->dst == &net->ipv6.ip6_null_entry->dst))
990 return;
991
992 skb_dst_set(skb, &rt6->dst);
993 }
994
vrf_ip6_rcv(struct net_device * vrf_dev,struct sk_buff * skb)995 static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
996 struct sk_buff *skb)
997 {
998 int orig_iif = skb->skb_iif;
999 bool need_strict;
1000
1001 /* loopback traffic; do not push through packet taps again.
1002 * Reset pkt_type for upper layers to process skb
1003 */
1004 if (skb->pkt_type == PACKET_LOOPBACK) {
1005 skb->dev = vrf_dev;
1006 skb->skb_iif = vrf_dev->ifindex;
1007 IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
1008 skb->pkt_type = PACKET_HOST;
1009 goto out;
1010 }
1011
1012 /* if packet is NDISC or addressed to multicast or link-local
1013 * then keep the ingress interface
1014 */
1015 need_strict = rt6_need_strict(&ipv6_hdr(skb)->daddr);
1016 if (!ipv6_ndisc_frame(skb) && !need_strict) {
1017 vrf_rx_stats(vrf_dev, skb->len);
1018 skb->dev = vrf_dev;
1019 skb->skb_iif = vrf_dev->ifindex;
1020
1021 if (!list_empty(&vrf_dev->ptype_all)) {
1022 skb_push(skb, skb->mac_len);
1023 dev_queue_xmit_nit(skb, vrf_dev);
1024 skb_pull(skb, skb->mac_len);
1025 }
1026
1027 IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
1028 }
1029
1030 if (need_strict)
1031 vrf_ip6_input_dst(skb, vrf_dev, orig_iif);
1032
1033 skb = vrf_rcv_nfhook(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, vrf_dev);
1034 out:
1035 return skb;
1036 }
1037
1038 #else
vrf_ip6_rcv(struct net_device * vrf_dev,struct sk_buff * skb)1039 static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
1040 struct sk_buff *skb)
1041 {
1042 return skb;
1043 }
1044 #endif
1045
vrf_ip_rcv(struct net_device * vrf_dev,struct sk_buff * skb)1046 static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev,
1047 struct sk_buff *skb)
1048 {
1049 skb->dev = vrf_dev;
1050 skb->skb_iif = vrf_dev->ifindex;
1051 IPCB(skb)->flags |= IPSKB_L3SLAVE;
1052
1053 if (ipv4_is_multicast(ip_hdr(skb)->daddr))
1054 goto out;
1055
1056 /* loopback traffic; do not push through packet taps again.
1057 * Reset pkt_type for upper layers to process skb
1058 */
1059 if (skb->pkt_type == PACKET_LOOPBACK) {
1060 skb->pkt_type = PACKET_HOST;
1061 goto out;
1062 }
1063
1064 vrf_rx_stats(vrf_dev, skb->len);
1065
1066 if (!list_empty(&vrf_dev->ptype_all)) {
1067 skb_push(skb, skb->mac_len);
1068 dev_queue_xmit_nit(skb, vrf_dev);
1069 skb_pull(skb, skb->mac_len);
1070 }
1071
1072 skb = vrf_rcv_nfhook(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, vrf_dev);
1073 out:
1074 return skb;
1075 }
1076
1077 /* called with rcu lock held */
vrf_l3_rcv(struct net_device * vrf_dev,struct sk_buff * skb,u16 proto)1078 static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev,
1079 struct sk_buff *skb,
1080 u16 proto)
1081 {
1082 switch (proto) {
1083 case AF_INET:
1084 return vrf_ip_rcv(vrf_dev, skb);
1085 case AF_INET6:
1086 return vrf_ip6_rcv(vrf_dev, skb);
1087 }
1088
1089 return skb;
1090 }
1091
1092 #if IS_ENABLED(CONFIG_IPV6)
1093 /* send to link-local or multicast address via interface enslaved to
1094 * VRF device. Force lookup to VRF table without changing flow struct
1095 */
vrf_link_scope_lookup(const struct net_device * dev,struct flowi6 * fl6)1096 static struct dst_entry *vrf_link_scope_lookup(const struct net_device *dev,
1097 struct flowi6 *fl6)
1098 {
1099 struct net *net = dev_net(dev);
1100 int flags = RT6_LOOKUP_F_IFACE;
1101 struct dst_entry *dst = NULL;
1102 struct rt6_info *rt;
1103
1104 /* VRF device does not have a link-local address and
1105 * sending packets to link-local or mcast addresses over
1106 * a VRF device does not make sense
1107 */
1108 if (fl6->flowi6_oif == dev->ifindex) {
1109 dst = &net->ipv6.ip6_null_entry->dst;
1110 dst_hold(dst);
1111 return dst;
1112 }
1113
1114 if (!ipv6_addr_any(&fl6->saddr))
1115 flags |= RT6_LOOKUP_F_HAS_SADDR;
1116
1117 rt = vrf_ip6_route_lookup(net, dev, fl6, fl6->flowi6_oif, flags);
1118 if (rt)
1119 dst = &rt->dst;
1120
1121 return dst;
1122 }
1123 #endif
1124
1125 static const struct l3mdev_ops vrf_l3mdev_ops = {
1126 .l3mdev_fib_table = vrf_fib_table,
1127 .l3mdev_l3_rcv = vrf_l3_rcv,
1128 .l3mdev_l3_out = vrf_l3_out,
1129 #if IS_ENABLED(CONFIG_IPV6)
1130 .l3mdev_link_scope_lookup = vrf_link_scope_lookup,
1131 #endif
1132 };
1133
vrf_get_drvinfo(struct net_device * dev,struct ethtool_drvinfo * info)1134 static void vrf_get_drvinfo(struct net_device *dev,
1135 struct ethtool_drvinfo *info)
1136 {
1137 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1138 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1139 }
1140
1141 static const struct ethtool_ops vrf_ethtool_ops = {
1142 .get_drvinfo = vrf_get_drvinfo,
1143 };
1144
vrf_fib_rule_nl_size(void)1145 static inline size_t vrf_fib_rule_nl_size(void)
1146 {
1147 size_t sz;
1148
1149 sz = NLMSG_ALIGN(sizeof(struct fib_rule_hdr));
1150 sz += nla_total_size(sizeof(u8)); /* FRA_L3MDEV */
1151 sz += nla_total_size(sizeof(u32)); /* FRA_PRIORITY */
1152
1153 return sz;
1154 }
1155
vrf_fib_rule(const struct net_device * dev,__u8 family,bool add_it)1156 static int vrf_fib_rule(const struct net_device *dev, __u8 family, bool add_it)
1157 {
1158 struct fib_rule_hdr *frh;
1159 struct nlmsghdr *nlh;
1160 struct sk_buff *skb;
1161 int err;
1162
1163 if (family == AF_INET6 && !ipv6_mod_enabled())
1164 return 0;
1165
1166 skb = nlmsg_new(vrf_fib_rule_nl_size(), GFP_KERNEL);
1167 if (!skb)
1168 return -ENOMEM;
1169
1170 nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*frh), 0);
1171 if (!nlh)
1172 goto nla_put_failure;
1173
1174 /* rule only needs to appear once */
1175 nlh->nlmsg_flags |= NLM_F_EXCL;
1176
1177 frh = nlmsg_data(nlh);
1178 memset(frh, 0, sizeof(*frh));
1179 frh->family = family;
1180 frh->action = FR_ACT_TO_TBL;
1181
1182 if (nla_put_u8(skb, FRA_L3MDEV, 1))
1183 goto nla_put_failure;
1184
1185 if (nla_put_u32(skb, FRA_PRIORITY, FIB_RULE_PREF))
1186 goto nla_put_failure;
1187
1188 nlmsg_end(skb, nlh);
1189
1190 /* fib_nl_{new,del}rule handling looks for net from skb->sk */
1191 skb->sk = dev_net(dev)->rtnl;
1192 if (add_it) {
1193 err = fib_nl_newrule(skb, nlh, NULL);
1194 if (err == -EEXIST)
1195 err = 0;
1196 } else {
1197 err = fib_nl_delrule(skb, nlh, NULL);
1198 if (err == -ENOENT)
1199 err = 0;
1200 }
1201 nlmsg_free(skb);
1202
1203 return err;
1204
1205 nla_put_failure:
1206 nlmsg_free(skb);
1207
1208 return -EMSGSIZE;
1209 }
1210
vrf_add_fib_rules(const struct net_device * dev)1211 static int vrf_add_fib_rules(const struct net_device *dev)
1212 {
1213 int err;
1214
1215 err = vrf_fib_rule(dev, AF_INET, true);
1216 if (err < 0)
1217 goto out_err;
1218
1219 err = vrf_fib_rule(dev, AF_INET6, true);
1220 if (err < 0)
1221 goto ipv6_err;
1222
1223 #if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES)
1224 err = vrf_fib_rule(dev, RTNL_FAMILY_IPMR, true);
1225 if (err < 0)
1226 goto ipmr_err;
1227 #endif
1228
1229 return 0;
1230
1231 #if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES)
1232 ipmr_err:
1233 vrf_fib_rule(dev, AF_INET6, false);
1234 #endif
1235
1236 ipv6_err:
1237 vrf_fib_rule(dev, AF_INET, false);
1238
1239 out_err:
1240 netdev_err(dev, "Failed to add FIB rules.\n");
1241 return err;
1242 }
1243
vrf_setup(struct net_device * dev)1244 static void vrf_setup(struct net_device *dev)
1245 {
1246 ether_setup(dev);
1247
1248 /* Initialize the device structure. */
1249 dev->netdev_ops = &vrf_netdev_ops;
1250 dev->l3mdev_ops = &vrf_l3mdev_ops;
1251 dev->ethtool_ops = &vrf_ethtool_ops;
1252 dev->needs_free_netdev = true;
1253
1254 /* Fill in device structure with ethernet-generic values. */
1255 eth_hw_addr_random(dev);
1256
1257 /* don't acquire vrf device's netif_tx_lock when transmitting */
1258 dev->features |= NETIF_F_LLTX;
1259
1260 /* don't allow vrf devices to change network namespaces. */
1261 dev->features |= NETIF_F_NETNS_LOCAL;
1262
1263 /* does not make sense for a VLAN to be added to a vrf device */
1264 dev->features |= NETIF_F_VLAN_CHALLENGED;
1265
1266 /* enable offload features */
1267 dev->features |= NETIF_F_GSO_SOFTWARE;
1268 dev->features |= NETIF_F_RXCSUM | NETIF_F_HW_CSUM;
1269 dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA;
1270
1271 dev->hw_features = dev->features;
1272 dev->hw_enc_features = dev->features;
1273
1274 /* default to no qdisc; user can add if desired */
1275 dev->priv_flags |= IFF_NO_QUEUE;
1276 }
1277
vrf_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1278 static int vrf_validate(struct nlattr *tb[], struct nlattr *data[],
1279 struct netlink_ext_ack *extack)
1280 {
1281 if (tb[IFLA_ADDRESS]) {
1282 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1283 NL_SET_ERR_MSG(extack, "Invalid hardware address");
1284 return -EINVAL;
1285 }
1286 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1287 NL_SET_ERR_MSG(extack, "Invalid hardware address");
1288 return -EADDRNOTAVAIL;
1289 }
1290 }
1291 return 0;
1292 }
1293
vrf_dellink(struct net_device * dev,struct list_head * head)1294 static void vrf_dellink(struct net_device *dev, struct list_head *head)
1295 {
1296 struct net_device *port_dev;
1297 struct list_head *iter;
1298
1299 netdev_for_each_lower_dev(dev, port_dev, iter)
1300 vrf_del_slave(dev, port_dev);
1301
1302 unregister_netdevice_queue(dev, head);
1303 }
1304
vrf_newlink(struct net * src_net,struct net_device * dev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)1305 static int vrf_newlink(struct net *src_net, struct net_device *dev,
1306 struct nlattr *tb[], struct nlattr *data[],
1307 struct netlink_ext_ack *extack)
1308 {
1309 struct net_vrf *vrf = netdev_priv(dev);
1310 bool *add_fib_rules;
1311 struct net *net;
1312 int err;
1313
1314 if (!data || !data[IFLA_VRF_TABLE]) {
1315 NL_SET_ERR_MSG(extack, "VRF table id is missing");
1316 return -EINVAL;
1317 }
1318
1319 vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
1320 if (vrf->tb_id == RT_TABLE_UNSPEC) {
1321 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VRF_TABLE],
1322 "Invalid VRF table id");
1323 return -EINVAL;
1324 }
1325
1326 dev->priv_flags |= IFF_L3MDEV_MASTER;
1327
1328 err = register_netdevice(dev);
1329 if (err)
1330 goto out;
1331
1332 net = dev_net(dev);
1333 add_fib_rules = net_generic(net, vrf_net_id);
1334 if (*add_fib_rules) {
1335 err = vrf_add_fib_rules(dev);
1336 if (err) {
1337 unregister_netdevice(dev);
1338 goto out;
1339 }
1340 *add_fib_rules = false;
1341 }
1342
1343 out:
1344 return err;
1345 }
1346
vrf_nl_getsize(const struct net_device * dev)1347 static size_t vrf_nl_getsize(const struct net_device *dev)
1348 {
1349 return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
1350 }
1351
vrf_fillinfo(struct sk_buff * skb,const struct net_device * dev)1352 static int vrf_fillinfo(struct sk_buff *skb,
1353 const struct net_device *dev)
1354 {
1355 struct net_vrf *vrf = netdev_priv(dev);
1356
1357 return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
1358 }
1359
vrf_get_slave_size(const struct net_device * bond_dev,const struct net_device * slave_dev)1360 static size_t vrf_get_slave_size(const struct net_device *bond_dev,
1361 const struct net_device *slave_dev)
1362 {
1363 return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */
1364 }
1365
vrf_fill_slave_info(struct sk_buff * skb,const struct net_device * vrf_dev,const struct net_device * slave_dev)1366 static int vrf_fill_slave_info(struct sk_buff *skb,
1367 const struct net_device *vrf_dev,
1368 const struct net_device *slave_dev)
1369 {
1370 struct net_vrf *vrf = netdev_priv(vrf_dev);
1371
1372 if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id))
1373 return -EMSGSIZE;
1374
1375 return 0;
1376 }
1377
1378 static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
1379 [IFLA_VRF_TABLE] = { .type = NLA_U32 },
1380 };
1381
1382 static struct rtnl_link_ops vrf_link_ops __read_mostly = {
1383 .kind = DRV_NAME,
1384 .priv_size = sizeof(struct net_vrf),
1385
1386 .get_size = vrf_nl_getsize,
1387 .policy = vrf_nl_policy,
1388 .validate = vrf_validate,
1389 .fill_info = vrf_fillinfo,
1390
1391 .get_slave_size = vrf_get_slave_size,
1392 .fill_slave_info = vrf_fill_slave_info,
1393
1394 .newlink = vrf_newlink,
1395 .dellink = vrf_dellink,
1396 .setup = vrf_setup,
1397 .maxtype = IFLA_VRF_MAX,
1398 };
1399
vrf_device_event(struct notifier_block * unused,unsigned long event,void * ptr)1400 static int vrf_device_event(struct notifier_block *unused,
1401 unsigned long event, void *ptr)
1402 {
1403 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1404
1405 /* only care about unregister events to drop slave references */
1406 if (event == NETDEV_UNREGISTER) {
1407 struct net_device *vrf_dev;
1408
1409 if (!netif_is_l3_slave(dev))
1410 goto out;
1411
1412 vrf_dev = netdev_master_upper_dev_get(dev);
1413 vrf_del_slave(vrf_dev, dev);
1414 }
1415 out:
1416 return NOTIFY_DONE;
1417 }
1418
1419 static struct notifier_block vrf_notifier_block __read_mostly = {
1420 .notifier_call = vrf_device_event,
1421 };
1422
1423 /* Initialize per network namespace state */
vrf_netns_init(struct net * net)1424 static int __net_init vrf_netns_init(struct net *net)
1425 {
1426 bool *add_fib_rules = net_generic(net, vrf_net_id);
1427
1428 *add_fib_rules = true;
1429
1430 return 0;
1431 }
1432
1433 static struct pernet_operations vrf_net_ops __net_initdata = {
1434 .init = vrf_netns_init,
1435 .id = &vrf_net_id,
1436 .size = sizeof(bool),
1437 };
1438
vrf_init_module(void)1439 static int __init vrf_init_module(void)
1440 {
1441 int rc;
1442
1443 register_netdevice_notifier(&vrf_notifier_block);
1444
1445 rc = register_pernet_subsys(&vrf_net_ops);
1446 if (rc < 0)
1447 goto error;
1448
1449 rc = rtnl_link_register(&vrf_link_ops);
1450 if (rc < 0) {
1451 unregister_pernet_subsys(&vrf_net_ops);
1452 goto error;
1453 }
1454
1455 return 0;
1456
1457 error:
1458 unregister_netdevice_notifier(&vrf_notifier_block);
1459 return rc;
1460 }
1461
1462 module_init(vrf_init_module);
1463 MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
1464 MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
1465 MODULE_LICENSE("GPL");
1466 MODULE_ALIAS_RTNL_LINK(DRV_NAME);
1467 MODULE_VERSION(DRV_VERSION);
1468