blob: 9b626c169554fe8e95dfdedf2c26c432d0f272de [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * vrf.c: device driver to encapsulate a VRF space
4 *
5 * Copyright (c) 2015 Cumulus Networks. All rights reserved.
6 * Copyright (c) 2015 Shrijeet Mukherjee <shm@cumulusnetworks.com>
7 * Copyright (c) 2015 David Ahern <dsa@cumulusnetworks.com>
8 *
9 * Based on dummy, team and ipvlan drivers
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000010 */
11
12#include <linux/module.h>
13#include <linux/kernel.h>
14#include <linux/netdevice.h>
15#include <linux/etherdevice.h>
16#include <linux/ip.h>
17#include <linux/init.h>
18#include <linux/moduleparam.h>
19#include <linux/netfilter.h>
20#include <linux/rtnetlink.h>
21#include <net/rtnetlink.h>
22#include <linux/u64_stats_sync.h>
23#include <linux/hashtable.h>
24
25#include <linux/inetdevice.h>
26#include <net/arp.h>
27#include <net/ip.h>
28#include <net/ip_fib.h>
29#include <net/ip6_fib.h>
30#include <net/ip6_route.h>
31#include <net/route.h>
32#include <net/addrconf.h>
33#include <net/l3mdev.h>
34#include <net/fib_rules.h>
35#include <net/netns/generic.h>
36
37#define DRV_NAME "vrf"
38#define DRV_VERSION "1.0"
39
40#define FIB_RULE_PREF 1000 /* default preference for FIB rules */
41
42static unsigned int vrf_net_id;
43
44struct net_vrf {
45 struct rtable __rcu *rth;
46 struct rt6_info __rcu *rt6;
47#if IS_ENABLED(CONFIG_IPV6)
48 struct fib6_table *fib6_table;
49#endif
50 u32 tb_id;
51};
52
53struct pcpu_dstats {
54 u64 tx_pkts;
55 u64 tx_bytes;
56 u64 tx_drps;
57 u64 rx_pkts;
58 u64 rx_bytes;
59 u64 rx_drps;
60 struct u64_stats_sync syncp;
61};
62
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
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
79static void 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}
105
106/* by default VRF devices do not have a qdisc and are expected
107 * to be created with only a single queue.
108 */
109static bool qdisc_tx_is_default(const struct net_device *dev)
110{
111 struct netdev_queue *txq;
112 struct Qdisc *qdisc;
113
114 if (dev->num_tx_queues > 1)
115 return false;
116
117 txq = netdev_get_tx_queue(dev, 0);
118 qdisc = rcu_access_pointer(txq->qdisc);
119
120 return !qdisc->enqueue;
121}
122
123/* Local traffic destined to local address. Reinsert the packet to rx
124 * path, similar to loopback handling.
125 */
126static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev,
127 struct dst_entry *dst)
128{
129 int len = skb->len;
130
131 skb_orphan(skb);
132
133 skb_dst_set(skb, dst);
134
135 /* set pkt_type to avoid skb hitting packet taps twice -
136 * once on Tx and again in Rx processing
137 */
138 skb->pkt_type = PACKET_LOOPBACK;
139
140 skb->protocol = eth_type_trans(skb, dev);
141
142 if (likely(netif_rx(skb) == NET_RX_SUCCESS))
143 vrf_rx_stats(dev, len);
144 else
145 this_cpu_inc(dev->dstats->rx_drps);
146
147 return NETDEV_TX_OK;
148}
149
150#if IS_ENABLED(CONFIG_IPV6)
151static int vrf_ip6_local_out(struct net *net, struct sock *sk,
152 struct sk_buff *skb)
153{
154 int err;
155
156 err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net,
157 sk, skb, NULL, skb_dst(skb)->dev, dst_output);
158
159 if (likely(err == 1))
160 err = dst_output(net, sk, skb);
161
162 return err;
163}
164
165static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
166 struct net_device *dev)
167{
David Brazdil0f672f62019-12-10 10:32:29 +0000168 const struct ipv6hdr *iph;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000169 struct net *net = dev_net(skb->dev);
David Brazdil0f672f62019-12-10 10:32:29 +0000170 struct flowi6 fl6;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000171 int ret = NET_XMIT_DROP;
172 struct dst_entry *dst;
173 struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
174
David Brazdil0f672f62019-12-10 10:32:29 +0000175 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct ipv6hdr)))
176 goto err;
177
178 iph = ipv6_hdr(skb);
179
180 memset(&fl6, 0, sizeof(fl6));
181 /* needed to match OIF rule */
182 fl6.flowi6_oif = dev->ifindex;
183 fl6.flowi6_iif = LOOPBACK_IFINDEX;
184 fl6.daddr = iph->daddr;
185 fl6.saddr = iph->saddr;
186 fl6.flowlabel = ip6_flowinfo(iph);
187 fl6.flowi6_mark = skb->mark;
188 fl6.flowi6_proto = iph->nexthdr;
189 fl6.flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF;
190
Olivier Deprez0e641232021-09-23 10:07:05 +0200191 dst = ip6_dst_lookup_flow(net, NULL, &fl6, NULL);
192 if (IS_ERR(dst) || dst == dst_null)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000193 goto err;
194
195 skb_dst_drop(skb);
196
197 /* if dst.dev is loopback or the VRF device again this is locally
198 * originated traffic destined to a local address. Short circuit
199 * to Rx path
200 */
201 if (dst->dev == dev)
202 return vrf_local_xmit(skb, dev, dst);
203
204 skb_dst_set(skb, dst);
205
206 /* strip the ethernet header added for pass through VRF device */
207 __skb_pull(skb, skb_network_offset(skb));
208
209 ret = vrf_ip6_local_out(net, skb->sk, skb);
210 if (unlikely(net_xmit_eval(ret)))
211 dev->stats.tx_errors++;
212 else
213 ret = NET_XMIT_SUCCESS;
214
215 return ret;
216err:
217 vrf_tx_error(dev, skb);
218 return NET_XMIT_DROP;
219}
220#else
221static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
222 struct net_device *dev)
223{
224 vrf_tx_error(dev, skb);
225 return NET_XMIT_DROP;
226}
227#endif
228
229/* based on ip_local_out; can't use it b/c the dst is switched pointing to us */
230static int vrf_ip_local_out(struct net *net, struct sock *sk,
231 struct sk_buff *skb)
232{
233 int err;
234
235 err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
236 skb, NULL, skb_dst(skb)->dev, dst_output);
237 if (likely(err == 1))
238 err = dst_output(net, sk, skb);
239
240 return err;
241}
242
243static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
244 struct net_device *vrf_dev)
245{
David Brazdil0f672f62019-12-10 10:32:29 +0000246 struct iphdr *ip4h;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000247 int ret = NET_XMIT_DROP;
David Brazdil0f672f62019-12-10 10:32:29 +0000248 struct flowi4 fl4;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000249 struct net *net = dev_net(vrf_dev);
250 struct rtable *rt;
251
David Brazdil0f672f62019-12-10 10:32:29 +0000252 if (!pskb_may_pull(skb, ETH_HLEN + sizeof(struct iphdr)))
253 goto err;
254
255 ip4h = ip_hdr(skb);
256
257 memset(&fl4, 0, sizeof(fl4));
258 /* needed to match OIF rule */
259 fl4.flowi4_oif = vrf_dev->ifindex;
260 fl4.flowi4_iif = LOOPBACK_IFINDEX;
261 fl4.flowi4_tos = RT_TOS(ip4h->tos);
262 fl4.flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_SKIP_NH_OIF;
263 fl4.flowi4_proto = ip4h->protocol;
264 fl4.daddr = ip4h->daddr;
265 fl4.saddr = ip4h->saddr;
266
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000267 rt = ip_route_output_flow(net, &fl4, NULL);
268 if (IS_ERR(rt))
269 goto err;
270
271 skb_dst_drop(skb);
272
273 /* if dst.dev is loopback or the VRF device again this is locally
274 * originated traffic destined to a local address. Short circuit
275 * to Rx path
276 */
277 if (rt->dst.dev == vrf_dev)
278 return vrf_local_xmit(skb, vrf_dev, &rt->dst);
279
280 skb_dst_set(skb, &rt->dst);
281
282 /* strip the ethernet header added for pass through VRF device */
283 __skb_pull(skb, skb_network_offset(skb));
284
285 if (!ip4h->saddr) {
286 ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
287 RT_SCOPE_LINK);
288 }
289
290 ret = vrf_ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
291 if (unlikely(net_xmit_eval(ret)))
292 vrf_dev->stats.tx_errors++;
293 else
294 ret = NET_XMIT_SUCCESS;
295
296out:
297 return ret;
298err:
299 vrf_tx_error(vrf_dev, skb);
300 goto out;
301}
302
303static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
304{
305 switch (skb->protocol) {
306 case htons(ETH_P_IP):
307 return vrf_process_v4_outbound(skb, dev);
308 case htons(ETH_P_IPV6):
309 return vrf_process_v6_outbound(skb, dev);
310 default:
311 vrf_tx_error(dev, skb);
312 return NET_XMIT_DROP;
313 }
314}
315
316static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
317{
318 int len = skb->len;
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 += 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
Olivier Deprez0e641232021-09-23 10:07:05 +0200335static void vrf_finish_direct(struct sk_buff *skb)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000336{
337 struct net_device *vrf_dev = skb->dev;
338
339 if (!list_empty(&vrf_dev->ptype_all) &&
340 likely(skb_headroom(skb) >= ETH_HLEN)) {
341 struct ethhdr *eth = skb_push(skb, ETH_HLEN);
342
343 ether_addr_copy(eth->h_source, vrf_dev->dev_addr);
344 eth_zero_addr(eth->h_dest);
345 eth->h_proto = skb->protocol;
346
347 rcu_read_lock_bh();
348 dev_queue_xmit_nit(skb, vrf_dev);
349 rcu_read_unlock_bh();
350
351 skb_pull(skb, ETH_HLEN);
352 }
353
Olivier Deprez0e641232021-09-23 10:07:05 +0200354 /* reset skb device */
355 nf_reset_ct(skb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000356}
357
358#if IS_ENABLED(CONFIG_IPV6)
359/* modelled after ip6_finish_output2 */
360static int vrf_finish_output6(struct net *net, struct sock *sk,
361 struct sk_buff *skb)
362{
363 struct dst_entry *dst = skb_dst(skb);
364 struct net_device *dev = dst->dev;
David Brazdil0f672f62019-12-10 10:32:29 +0000365 const struct in6_addr *nexthop;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000366 struct neighbour *neigh;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000367 int ret;
368
David Brazdil0f672f62019-12-10 10:32:29 +0000369 nf_reset_ct(skb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000370
371 skb->protocol = htons(ETH_P_IPV6);
372 skb->dev = dev;
373
374 rcu_read_lock_bh();
375 nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
376 neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
377 if (unlikely(!neigh))
378 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
379 if (!IS_ERR(neigh)) {
380 sock_confirm_neigh(skb, neigh);
David Brazdil0f672f62019-12-10 10:32:29 +0000381 ret = neigh_output(neigh, skb, false);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000382 rcu_read_unlock_bh();
383 return ret;
384 }
385 rcu_read_unlock_bh();
386
387 IP6_INC_STATS(dev_net(dst->dev),
388 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
389 kfree_skb(skb);
390 return -EINVAL;
391}
392
393/* modelled after ip6_output */
394static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
395{
396 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
397 net, sk, skb, NULL, skb_dst(skb)->dev,
398 vrf_finish_output6,
399 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
400}
401
402/* set dst on skb to send packet to us via dev_xmit path. Allows
403 * packet to go through device based features such as qdisc, netfilter
404 * hooks and packet sockets with skb->dev set to vrf device.
405 */
406static struct sk_buff *vrf_ip6_out_redirect(struct net_device *vrf_dev,
407 struct sk_buff *skb)
408{
409 struct net_vrf *vrf = netdev_priv(vrf_dev);
410 struct dst_entry *dst = NULL;
411 struct rt6_info *rt6;
412
413 rcu_read_lock();
414
415 rt6 = rcu_dereference(vrf->rt6);
416 if (likely(rt6)) {
417 dst = &rt6->dst;
418 dst_hold(dst);
419 }
420
421 rcu_read_unlock();
422
423 if (unlikely(!dst)) {
424 vrf_tx_error(vrf_dev, skb);
425 return NULL;
426 }
427
428 skb_dst_drop(skb);
429 skb_dst_set(skb, dst);
430
431 return skb;
432}
433
Olivier Deprez0e641232021-09-23 10:07:05 +0200434static int vrf_output6_direct_finish(struct net *net, struct sock *sk,
435 struct sk_buff *skb)
436{
437 vrf_finish_direct(skb);
438
439 return vrf_ip6_local_out(net, sk, skb);
440}
441
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000442static int vrf_output6_direct(struct net *net, struct sock *sk,
443 struct sk_buff *skb)
444{
Olivier Deprez0e641232021-09-23 10:07:05 +0200445 int err = 1;
446
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000447 skb->protocol = htons(ETH_P_IPV6);
448
Olivier Deprez0e641232021-09-23 10:07:05 +0200449 if (!(IPCB(skb)->flags & IPSKB_REROUTED))
450 err = nf_hook(NFPROTO_IPV6, NF_INET_POST_ROUTING, net, sk, skb,
451 NULL, skb->dev, vrf_output6_direct_finish);
452
453 if (likely(err == 1))
454 vrf_finish_direct(skb);
455
456 return err;
457}
458
459static int vrf_ip6_out_direct_finish(struct net *net, struct sock *sk,
460 struct sk_buff *skb)
461{
462 int err;
463
464 err = vrf_output6_direct(net, sk, skb);
465 if (likely(err == 1))
466 err = vrf_ip6_local_out(net, sk, skb);
467
468 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000469}
470
471static struct sk_buff *vrf_ip6_out_direct(struct net_device *vrf_dev,
472 struct sock *sk,
473 struct sk_buff *skb)
474{
475 struct net *net = dev_net(vrf_dev);
476 int err;
477
478 skb->dev = vrf_dev;
479
480 err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk,
Olivier Deprez0e641232021-09-23 10:07:05 +0200481 skb, NULL, vrf_dev, vrf_ip6_out_direct_finish);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000482
483 if (likely(err == 1))
484 err = vrf_output6_direct(net, sk, skb);
485
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000486 if (likely(err == 1))
Olivier Deprez0e641232021-09-23 10:07:05 +0200487 return skb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000488
Olivier Deprez0e641232021-09-23 10:07:05 +0200489 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000490}
491
492static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
493 struct sock *sk,
494 struct sk_buff *skb)
495{
496 /* don't divert link scope packets */
497 if (rt6_need_strict(&ipv6_hdr(skb)->daddr))
498 return skb;
499
Olivier Deprez0e641232021-09-23 10:07:05 +0200500 if (qdisc_tx_is_default(vrf_dev) ||
501 IP6CB(skb)->flags & IP6SKB_XFRM_TRANSFORMED)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000502 return vrf_ip6_out_direct(vrf_dev, sk, skb);
503
504 return vrf_ip6_out_redirect(vrf_dev, skb);
505}
506
507/* holding rtnl */
508static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
509{
510 struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
511 struct net *net = dev_net(dev);
512 struct dst_entry *dst;
513
514 RCU_INIT_POINTER(vrf->rt6, NULL);
515 synchronize_rcu();
516
517 /* move dev in dst's to loopback so this VRF device can be deleted
518 * - based on dst_ifdown
519 */
520 if (rt6) {
521 dst = &rt6->dst;
522 dev_put(dst->dev);
523 dst->dev = net->loopback_dev;
524 dev_hold(dst->dev);
525 dst_release(dst);
526 }
527}
528
529static int vrf_rt6_create(struct net_device *dev)
530{
531 int flags = DST_HOST | DST_NOPOLICY | DST_NOXFRM;
532 struct net_vrf *vrf = netdev_priv(dev);
533 struct net *net = dev_net(dev);
534 struct rt6_info *rt6;
535 int rc = -ENOMEM;
536
537 /* IPv6 can be CONFIG enabled and then disabled runtime */
538 if (!ipv6_mod_enabled())
539 return 0;
540
541 vrf->fib6_table = fib6_new_table(net, vrf->tb_id);
542 if (!vrf->fib6_table)
543 goto out;
544
545 /* create a dst for routing packets out a VRF device */
546 rt6 = ip6_dst_alloc(net, dev, flags);
547 if (!rt6)
548 goto out;
549
550 rt6->dst.output = vrf_output6;
551
552 rcu_assign_pointer(vrf->rt6, rt6);
553
554 rc = 0;
555out:
556 return rc;
557}
558#else
559static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
560 struct sock *sk,
561 struct sk_buff *skb)
562{
563 return skb;
564}
565
566static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
567{
568}
569
570static int vrf_rt6_create(struct net_device *dev)
571{
572 return 0;
573}
574#endif
575
576/* modelled after ip_finish_output2 */
577static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
578{
579 struct dst_entry *dst = skb_dst(skb);
580 struct rtable *rt = (struct rtable *)dst;
581 struct net_device *dev = dst->dev;
582 unsigned int hh_len = LL_RESERVED_SPACE(dev);
583 struct neighbour *neigh;
David Brazdil0f672f62019-12-10 10:32:29 +0000584 bool is_v6gw = false;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000585 int ret = -EINVAL;
586
David Brazdil0f672f62019-12-10 10:32:29 +0000587 nf_reset_ct(skb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000588
589 /* Be paranoid, rather than too clever. */
590 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
591 struct sk_buff *skb2;
592
593 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
594 if (!skb2) {
595 ret = -ENOMEM;
596 goto err;
597 }
598 if (skb->sk)
599 skb_set_owner_w(skb2, skb->sk);
600
601 consume_skb(skb);
602 skb = skb2;
603 }
604
605 rcu_read_lock_bh();
606
David Brazdil0f672f62019-12-10 10:32:29 +0000607 neigh = ip_neigh_for_gw(rt, skb, &is_v6gw);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000608 if (!IS_ERR(neigh)) {
609 sock_confirm_neigh(skb, neigh);
David Brazdil0f672f62019-12-10 10:32:29 +0000610 /* if crossing protocols, can not use the cached header */
611 ret = neigh_output(neigh, skb, is_v6gw);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000612 rcu_read_unlock_bh();
613 return ret;
614 }
615
616 rcu_read_unlock_bh();
617err:
618 vrf_tx_error(skb->dev, skb);
619 return ret;
620}
621
622static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
623{
624 struct net_device *dev = skb_dst(skb)->dev;
625
626 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
627
628 skb->dev = dev;
629 skb->protocol = htons(ETH_P_IP);
630
631 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
632 net, sk, skb, NULL, dev,
633 vrf_finish_output,
634 !(IPCB(skb)->flags & IPSKB_REROUTED));
635}
636
637/* set dst on skb to send packet to us via dev_xmit path. Allows
638 * packet to go through device based features such as qdisc, netfilter
639 * hooks and packet sockets with skb->dev set to vrf device.
640 */
641static struct sk_buff *vrf_ip_out_redirect(struct net_device *vrf_dev,
642 struct sk_buff *skb)
643{
644 struct net_vrf *vrf = netdev_priv(vrf_dev);
645 struct dst_entry *dst = NULL;
646 struct rtable *rth;
647
648 rcu_read_lock();
649
650 rth = rcu_dereference(vrf->rth);
651 if (likely(rth)) {
652 dst = &rth->dst;
653 dst_hold(dst);
654 }
655
656 rcu_read_unlock();
657
658 if (unlikely(!dst)) {
659 vrf_tx_error(vrf_dev, skb);
660 return NULL;
661 }
662
663 skb_dst_drop(skb);
664 skb_dst_set(skb, dst);
665
666 return skb;
667}
668
Olivier Deprez0e641232021-09-23 10:07:05 +0200669static int vrf_output_direct_finish(struct net *net, struct sock *sk,
670 struct sk_buff *skb)
671{
672 vrf_finish_direct(skb);
673
674 return vrf_ip_local_out(net, sk, skb);
675}
676
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000677static int vrf_output_direct(struct net *net, struct sock *sk,
678 struct sk_buff *skb)
679{
Olivier Deprez0e641232021-09-23 10:07:05 +0200680 int err = 1;
681
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000682 skb->protocol = htons(ETH_P_IP);
683
Olivier Deprez0e641232021-09-23 10:07:05 +0200684 if (!(IPCB(skb)->flags & IPSKB_REROUTED))
685 err = nf_hook(NFPROTO_IPV4, NF_INET_POST_ROUTING, net, sk, skb,
686 NULL, skb->dev, vrf_output_direct_finish);
687
688 if (likely(err == 1))
689 vrf_finish_direct(skb);
690
691 return err;
692}
693
694static int vrf_ip_out_direct_finish(struct net *net, struct sock *sk,
695 struct sk_buff *skb)
696{
697 int err;
698
699 err = vrf_output_direct(net, sk, skb);
700 if (likely(err == 1))
701 err = vrf_ip_local_out(net, sk, skb);
702
703 return err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000704}
705
706static struct sk_buff *vrf_ip_out_direct(struct net_device *vrf_dev,
707 struct sock *sk,
708 struct sk_buff *skb)
709{
710 struct net *net = dev_net(vrf_dev);
711 int err;
712
713 skb->dev = vrf_dev;
714
715 err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
Olivier Deprez0e641232021-09-23 10:07:05 +0200716 skb, NULL, vrf_dev, vrf_ip_out_direct_finish);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000717
718 if (likely(err == 1))
719 err = vrf_output_direct(net, sk, skb);
720
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000721 if (likely(err == 1))
Olivier Deprez0e641232021-09-23 10:07:05 +0200722 return skb;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000723
Olivier Deprez0e641232021-09-23 10:07:05 +0200724 return NULL;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000725}
726
727static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev,
728 struct sock *sk,
729 struct sk_buff *skb)
730{
731 /* don't divert multicast or local broadcast */
732 if (ipv4_is_multicast(ip_hdr(skb)->daddr) ||
733 ipv4_is_lbcast(ip_hdr(skb)->daddr))
734 return skb;
735
Olivier Deprez0e641232021-09-23 10:07:05 +0200736 if (qdisc_tx_is_default(vrf_dev) ||
737 IPCB(skb)->flags & IPSKB_XFRM_TRANSFORMED)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000738 return vrf_ip_out_direct(vrf_dev, sk, skb);
739
740 return vrf_ip_out_redirect(vrf_dev, skb);
741}
742
743/* called with rcu lock held */
744static struct sk_buff *vrf_l3_out(struct net_device *vrf_dev,
745 struct sock *sk,
746 struct sk_buff *skb,
747 u16 proto)
748{
749 switch (proto) {
750 case AF_INET:
751 return vrf_ip_out(vrf_dev, sk, skb);
752 case AF_INET6:
753 return vrf_ip6_out(vrf_dev, sk, skb);
754 }
755
756 return skb;
757}
758
759/* holding rtnl */
760static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf)
761{
762 struct rtable *rth = rtnl_dereference(vrf->rth);
763 struct net *net = dev_net(dev);
764 struct dst_entry *dst;
765
766 RCU_INIT_POINTER(vrf->rth, NULL);
767 synchronize_rcu();
768
769 /* move dev in dst's to loopback so this VRF device can be deleted
770 * - based on dst_ifdown
771 */
772 if (rth) {
773 dst = &rth->dst;
774 dev_put(dst->dev);
775 dst->dev = net->loopback_dev;
776 dev_hold(dst->dev);
777 dst_release(dst);
778 }
779}
780
781static int vrf_rtable_create(struct net_device *dev)
782{
783 struct net_vrf *vrf = netdev_priv(dev);
784 struct rtable *rth;
785
786 if (!fib_new_table(dev_net(dev), vrf->tb_id))
787 return -ENOMEM;
788
789 /* create a dst for routing packets out through a VRF device */
790 rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1, 1, 0);
791 if (!rth)
792 return -ENOMEM;
793
794 rth->dst.output = vrf_output;
795
796 rcu_assign_pointer(vrf->rth, rth);
797
798 return 0;
799}
800
801/**************************** device handling ********************/
802
803/* cycle interface to flush neighbor cache and move routes across tables */
David Brazdil0f672f62019-12-10 10:32:29 +0000804static void cycle_netdev(struct net_device *dev,
805 struct netlink_ext_ack *extack)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000806{
807 unsigned int flags = dev->flags;
808 int ret;
809
810 if (!netif_running(dev))
811 return;
812
David Brazdil0f672f62019-12-10 10:32:29 +0000813 ret = dev_change_flags(dev, flags & ~IFF_UP, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000814 if (ret >= 0)
David Brazdil0f672f62019-12-10 10:32:29 +0000815 ret = dev_change_flags(dev, flags, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000816
817 if (ret < 0) {
818 netdev_err(dev,
819 "Failed to cycle device %s; route tables might be wrong!\n",
820 dev->name);
821 }
822}
823
824static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev,
825 struct netlink_ext_ack *extack)
826{
827 int ret;
828
829 /* do not allow loopback device to be enslaved to a VRF.
830 * The vrf device acts as the loopback for the vrf.
831 */
832 if (port_dev == dev_net(dev)->loopback_dev) {
833 NL_SET_ERR_MSG(extack,
834 "Can not enslave loopback device to a VRF");
835 return -EOPNOTSUPP;
836 }
837
838 port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
839 ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL, extack);
840 if (ret < 0)
841 goto err;
842
David Brazdil0f672f62019-12-10 10:32:29 +0000843 cycle_netdev(port_dev, extack);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000844
845 return 0;
846
847err:
848 port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
849 return ret;
850}
851
852static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev,
853 struct netlink_ext_ack *extack)
854{
855 if (netif_is_l3_master(port_dev)) {
856 NL_SET_ERR_MSG(extack,
857 "Can not enslave an L3 master device to a VRF");
858 return -EINVAL;
859 }
860
861 if (netif_is_l3_slave(port_dev))
862 return -EINVAL;
863
864 return do_vrf_add_slave(dev, port_dev, extack);
865}
866
867/* inverse of do_vrf_add_slave */
868static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
869{
870 netdev_upper_dev_unlink(port_dev, dev);
871 port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
872
David Brazdil0f672f62019-12-10 10:32:29 +0000873 cycle_netdev(port_dev, NULL);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000874
875 return 0;
876}
877
878static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
879{
880 return do_vrf_del_slave(dev, port_dev);
881}
882
883static void vrf_dev_uninit(struct net_device *dev)
884{
885 struct net_vrf *vrf = netdev_priv(dev);
886
887 vrf_rtable_release(dev, vrf);
888 vrf_rt6_release(dev, vrf);
889
890 free_percpu(dev->dstats);
891 dev->dstats = NULL;
892}
893
894static int vrf_dev_init(struct net_device *dev)
895{
896 struct net_vrf *vrf = netdev_priv(dev);
897
898 dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
899 if (!dev->dstats)
900 goto out_nomem;
901
902 /* create the default dst which points back to us */
903 if (vrf_rtable_create(dev) != 0)
904 goto out_stats;
905
906 if (vrf_rt6_create(dev) != 0)
907 goto out_rth;
908
909 dev->flags = IFF_MASTER | IFF_NOARP;
910
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000911 /* similarly, oper state is irrelevant; set to up to avoid confusion */
912 dev->operstate = IF_OPER_UP;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000913 return 0;
914
915out_rth:
916 vrf_rtable_release(dev, vrf);
917out_stats:
918 free_percpu(dev->dstats);
919 dev->dstats = NULL;
920out_nomem:
921 return -ENOMEM;
922}
923
924static const struct net_device_ops vrf_netdev_ops = {
925 .ndo_init = vrf_dev_init,
926 .ndo_uninit = vrf_dev_uninit,
927 .ndo_start_xmit = vrf_xmit,
David Brazdil0f672f62019-12-10 10:32:29 +0000928 .ndo_set_mac_address = eth_mac_addr,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000929 .ndo_get_stats64 = vrf_get_stats64,
930 .ndo_add_slave = vrf_add_slave,
931 .ndo_del_slave = vrf_del_slave,
932};
933
934static u32 vrf_fib_table(const struct net_device *dev)
935{
936 struct net_vrf *vrf = netdev_priv(dev);
937
938 return vrf->tb_id;
939}
940
941static int vrf_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
942{
943 kfree_skb(skb);
944 return 0;
945}
946
947static struct sk_buff *vrf_rcv_nfhook(u8 pf, unsigned int hook,
948 struct sk_buff *skb,
949 struct net_device *dev)
950{
951 struct net *net = dev_net(dev);
952
953 if (nf_hook(pf, hook, net, NULL, skb, dev, NULL, vrf_rcv_finish) != 1)
954 skb = NULL; /* kfree_skb(skb) handled by nf code */
955
956 return skb;
957}
958
959#if IS_ENABLED(CONFIG_IPV6)
960/* neighbor handling is done with actual device; do not want
961 * to flip skb->dev for those ndisc packets. This really fails
962 * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
963 * a start.
964 */
965static bool ipv6_ndisc_frame(const struct sk_buff *skb)
966{
967 const struct ipv6hdr *iph = ipv6_hdr(skb);
968 bool rc = false;
969
970 if (iph->nexthdr == NEXTHDR_ICMP) {
971 const struct icmp6hdr *icmph;
972 struct icmp6hdr _icmph;
973
974 icmph = skb_header_pointer(skb, sizeof(*iph),
975 sizeof(_icmph), &_icmph);
976 if (!icmph)
977 goto out;
978
979 switch (icmph->icmp6_type) {
980 case NDISC_ROUTER_SOLICITATION:
981 case NDISC_ROUTER_ADVERTISEMENT:
982 case NDISC_NEIGHBOUR_SOLICITATION:
983 case NDISC_NEIGHBOUR_ADVERTISEMENT:
984 case NDISC_REDIRECT:
985 rc = true;
986 break;
987 }
988 }
989
990out:
991 return rc;
992}
993
994static struct rt6_info *vrf_ip6_route_lookup(struct net *net,
995 const struct net_device *dev,
996 struct flowi6 *fl6,
997 int ifindex,
998 const struct sk_buff *skb,
999 int flags)
1000{
1001 struct net_vrf *vrf = netdev_priv(dev);
1002
1003 return ip6_pol_route(net, vrf->fib6_table, ifindex, fl6, skb, flags);
1004}
1005
1006static void vrf_ip6_input_dst(struct sk_buff *skb, struct net_device *vrf_dev,
1007 int ifindex)
1008{
1009 const struct ipv6hdr *iph = ipv6_hdr(skb);
1010 struct flowi6 fl6 = {
1011 .flowi6_iif = ifindex,
1012 .flowi6_mark = skb->mark,
1013 .flowi6_proto = iph->nexthdr,
1014 .daddr = iph->daddr,
1015 .saddr = iph->saddr,
1016 .flowlabel = ip6_flowinfo(iph),
1017 };
1018 struct net *net = dev_net(vrf_dev);
1019 struct rt6_info *rt6;
1020
1021 rt6 = vrf_ip6_route_lookup(net, vrf_dev, &fl6, ifindex, skb,
1022 RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_IFACE);
1023 if (unlikely(!rt6))
1024 return;
1025
1026 if (unlikely(&rt6->dst == &net->ipv6.ip6_null_entry->dst))
1027 return;
1028
1029 skb_dst_set(skb, &rt6->dst);
1030}
1031
1032static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
1033 struct sk_buff *skb)
1034{
1035 int orig_iif = skb->skb_iif;
David Brazdil0f672f62019-12-10 10:32:29 +00001036 bool need_strict = rt6_need_strict(&ipv6_hdr(skb)->daddr);
1037 bool is_ndisc = ipv6_ndisc_frame(skb);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001038
Olivier Deprez0e641232021-09-23 10:07:05 +02001039 nf_reset_ct(skb);
1040
David Brazdil0f672f62019-12-10 10:32:29 +00001041 /* loopback, multicast & non-ND link-local traffic; do not push through
Olivier Deprez0e641232021-09-23 10:07:05 +02001042 * packet taps again. Reset pkt_type for upper layers to process skb.
1043 * For strict packets with a source LLA, determine the dst using the
1044 * original ifindex.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001045 */
David Brazdil0f672f62019-12-10 10:32:29 +00001046 if (skb->pkt_type == PACKET_LOOPBACK || (need_strict && !is_ndisc)) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001047 skb->dev = vrf_dev;
1048 skb->skb_iif = vrf_dev->ifindex;
1049 IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
Olivier Deprez0e641232021-09-23 10:07:05 +02001050
David Brazdil0f672f62019-12-10 10:32:29 +00001051 if (skb->pkt_type == PACKET_LOOPBACK)
1052 skb->pkt_type = PACKET_HOST;
Olivier Deprez0e641232021-09-23 10:07:05 +02001053 else if (ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)
1054 vrf_ip6_input_dst(skb, vrf_dev, orig_iif);
1055
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001056 goto out;
1057 }
1058
David Brazdil0f672f62019-12-10 10:32:29 +00001059 /* if packet is NDISC then keep the ingress interface */
1060 if (!is_ndisc) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001061 vrf_rx_stats(vrf_dev, skb->len);
1062 skb->dev = vrf_dev;
1063 skb->skb_iif = vrf_dev->ifindex;
1064
1065 if (!list_empty(&vrf_dev->ptype_all)) {
1066 skb_push(skb, skb->mac_len);
1067 dev_queue_xmit_nit(skb, vrf_dev);
1068 skb_pull(skb, skb->mac_len);
1069 }
1070
1071 IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
1072 }
1073
1074 if (need_strict)
1075 vrf_ip6_input_dst(skb, vrf_dev, orig_iif);
1076
1077 skb = vrf_rcv_nfhook(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, vrf_dev);
1078out:
1079 return skb;
1080}
1081
1082#else
1083static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
1084 struct sk_buff *skb)
1085{
1086 return skb;
1087}
1088#endif
1089
1090static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev,
1091 struct sk_buff *skb)
1092{
1093 skb->dev = vrf_dev;
1094 skb->skb_iif = vrf_dev->ifindex;
1095 IPCB(skb)->flags |= IPSKB_L3SLAVE;
1096
Olivier Deprez0e641232021-09-23 10:07:05 +02001097 nf_reset_ct(skb);
1098
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001099 if (ipv4_is_multicast(ip_hdr(skb)->daddr))
1100 goto out;
1101
1102 /* loopback traffic; do not push through packet taps again.
1103 * Reset pkt_type for upper layers to process skb
1104 */
1105 if (skb->pkt_type == PACKET_LOOPBACK) {
1106 skb->pkt_type = PACKET_HOST;
1107 goto out;
1108 }
1109
1110 vrf_rx_stats(vrf_dev, skb->len);
1111
1112 if (!list_empty(&vrf_dev->ptype_all)) {
1113 skb_push(skb, skb->mac_len);
1114 dev_queue_xmit_nit(skb, vrf_dev);
1115 skb_pull(skb, skb->mac_len);
1116 }
1117
1118 skb = vrf_rcv_nfhook(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, vrf_dev);
1119out:
1120 return skb;
1121}
1122
1123/* called with rcu lock held */
1124static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev,
1125 struct sk_buff *skb,
1126 u16 proto)
1127{
1128 switch (proto) {
1129 case AF_INET:
1130 return vrf_ip_rcv(vrf_dev, skb);
1131 case AF_INET6:
1132 return vrf_ip6_rcv(vrf_dev, skb);
1133 }
1134
1135 return skb;
1136}
1137
1138#if IS_ENABLED(CONFIG_IPV6)
1139/* send to link-local or multicast address via interface enslaved to
1140 * VRF device. Force lookup to VRF table without changing flow struct
David Brazdil0f672f62019-12-10 10:32:29 +00001141 * Note: Caller to this function must hold rcu_read_lock() and no refcnt
1142 * is taken on the dst by this function.
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001143 */
1144static struct dst_entry *vrf_link_scope_lookup(const struct net_device *dev,
1145 struct flowi6 *fl6)
1146{
1147 struct net *net = dev_net(dev);
David Brazdil0f672f62019-12-10 10:32:29 +00001148 int flags = RT6_LOOKUP_F_IFACE | RT6_LOOKUP_F_DST_NOREF;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001149 struct dst_entry *dst = NULL;
1150 struct rt6_info *rt;
1151
1152 /* VRF device does not have a link-local address and
1153 * sending packets to link-local or mcast addresses over
1154 * a VRF device does not make sense
1155 */
1156 if (fl6->flowi6_oif == dev->ifindex) {
1157 dst = &net->ipv6.ip6_null_entry->dst;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001158 return dst;
1159 }
1160
1161 if (!ipv6_addr_any(&fl6->saddr))
1162 flags |= RT6_LOOKUP_F_HAS_SADDR;
1163
1164 rt = vrf_ip6_route_lookup(net, dev, fl6, fl6->flowi6_oif, NULL, flags);
1165 if (rt)
1166 dst = &rt->dst;
1167
1168 return dst;
1169}
1170#endif
1171
1172static const struct l3mdev_ops vrf_l3mdev_ops = {
1173 .l3mdev_fib_table = vrf_fib_table,
1174 .l3mdev_l3_rcv = vrf_l3_rcv,
1175 .l3mdev_l3_out = vrf_l3_out,
1176#if IS_ENABLED(CONFIG_IPV6)
1177 .l3mdev_link_scope_lookup = vrf_link_scope_lookup,
1178#endif
1179};
1180
1181static void vrf_get_drvinfo(struct net_device *dev,
1182 struct ethtool_drvinfo *info)
1183{
1184 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1185 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1186}
1187
1188static const struct ethtool_ops vrf_ethtool_ops = {
1189 .get_drvinfo = vrf_get_drvinfo,
1190};
1191
1192static inline size_t vrf_fib_rule_nl_size(void)
1193{
1194 size_t sz;
1195
1196 sz = NLMSG_ALIGN(sizeof(struct fib_rule_hdr));
1197 sz += nla_total_size(sizeof(u8)); /* FRA_L3MDEV */
1198 sz += nla_total_size(sizeof(u32)); /* FRA_PRIORITY */
1199 sz += nla_total_size(sizeof(u8)); /* FRA_PROTOCOL */
1200
1201 return sz;
1202}
1203
1204static int vrf_fib_rule(const struct net_device *dev, __u8 family, bool add_it)
1205{
1206 struct fib_rule_hdr *frh;
1207 struct nlmsghdr *nlh;
1208 struct sk_buff *skb;
1209 int err;
1210
David Brazdil0f672f62019-12-10 10:32:29 +00001211 if ((family == AF_INET6 || family == RTNL_FAMILY_IP6MR) &&
1212 !ipv6_mod_enabled())
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001213 return 0;
1214
1215 skb = nlmsg_new(vrf_fib_rule_nl_size(), GFP_KERNEL);
1216 if (!skb)
1217 return -ENOMEM;
1218
1219 nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*frh), 0);
1220 if (!nlh)
1221 goto nla_put_failure;
1222
1223 /* rule only needs to appear once */
1224 nlh->nlmsg_flags |= NLM_F_EXCL;
1225
1226 frh = nlmsg_data(nlh);
1227 memset(frh, 0, sizeof(*frh));
1228 frh->family = family;
1229 frh->action = FR_ACT_TO_TBL;
1230
1231 if (nla_put_u8(skb, FRA_PROTOCOL, RTPROT_KERNEL))
1232 goto nla_put_failure;
1233
1234 if (nla_put_u8(skb, FRA_L3MDEV, 1))
1235 goto nla_put_failure;
1236
1237 if (nla_put_u32(skb, FRA_PRIORITY, FIB_RULE_PREF))
1238 goto nla_put_failure;
1239
1240 nlmsg_end(skb, nlh);
1241
1242 /* fib_nl_{new,del}rule handling looks for net from skb->sk */
1243 skb->sk = dev_net(dev)->rtnl;
1244 if (add_it) {
1245 err = fib_nl_newrule(skb, nlh, NULL);
1246 if (err == -EEXIST)
1247 err = 0;
1248 } else {
1249 err = fib_nl_delrule(skb, nlh, NULL);
1250 if (err == -ENOENT)
1251 err = 0;
1252 }
1253 nlmsg_free(skb);
1254
1255 return err;
1256
1257nla_put_failure:
1258 nlmsg_free(skb);
1259
1260 return -EMSGSIZE;
1261}
1262
1263static int vrf_add_fib_rules(const struct net_device *dev)
1264{
1265 int err;
1266
1267 err = vrf_fib_rule(dev, AF_INET, true);
1268 if (err < 0)
1269 goto out_err;
1270
1271 err = vrf_fib_rule(dev, AF_INET6, true);
1272 if (err < 0)
1273 goto ipv6_err;
1274
1275#if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES)
1276 err = vrf_fib_rule(dev, RTNL_FAMILY_IPMR, true);
1277 if (err < 0)
1278 goto ipmr_err;
1279#endif
1280
David Brazdil0f672f62019-12-10 10:32:29 +00001281#if IS_ENABLED(CONFIG_IPV6_MROUTE_MULTIPLE_TABLES)
1282 err = vrf_fib_rule(dev, RTNL_FAMILY_IP6MR, true);
1283 if (err < 0)
1284 goto ip6mr_err;
1285#endif
1286
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001287 return 0;
1288
David Brazdil0f672f62019-12-10 10:32:29 +00001289#if IS_ENABLED(CONFIG_IPV6_MROUTE_MULTIPLE_TABLES)
1290ip6mr_err:
1291 vrf_fib_rule(dev, RTNL_FAMILY_IPMR, false);
1292#endif
1293
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001294#if IS_ENABLED(CONFIG_IP_MROUTE_MULTIPLE_TABLES)
1295ipmr_err:
1296 vrf_fib_rule(dev, AF_INET6, false);
1297#endif
1298
1299ipv6_err:
1300 vrf_fib_rule(dev, AF_INET, false);
1301
1302out_err:
1303 netdev_err(dev, "Failed to add FIB rules.\n");
1304 return err;
1305}
1306
1307static void vrf_setup(struct net_device *dev)
1308{
1309 ether_setup(dev);
1310
1311 /* Initialize the device structure. */
1312 dev->netdev_ops = &vrf_netdev_ops;
1313 dev->l3mdev_ops = &vrf_l3mdev_ops;
1314 dev->ethtool_ops = &vrf_ethtool_ops;
1315 dev->needs_free_netdev = true;
1316
1317 /* Fill in device structure with ethernet-generic values. */
1318 eth_hw_addr_random(dev);
1319
1320 /* don't acquire vrf device's netif_tx_lock when transmitting */
1321 dev->features |= NETIF_F_LLTX;
1322
1323 /* don't allow vrf devices to change network namespaces. */
1324 dev->features |= NETIF_F_NETNS_LOCAL;
1325
1326 /* does not make sense for a VLAN to be added to a vrf device */
1327 dev->features |= NETIF_F_VLAN_CHALLENGED;
1328
1329 /* enable offload features */
1330 dev->features |= NETIF_F_GSO_SOFTWARE;
1331 dev->features |= NETIF_F_RXCSUM | NETIF_F_HW_CSUM | NETIF_F_SCTP_CRC;
1332 dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA;
1333
1334 dev->hw_features = dev->features;
1335 dev->hw_enc_features = dev->features;
1336
1337 /* default to no qdisc; user can add if desired */
1338 dev->priv_flags |= IFF_NO_QUEUE;
David Brazdil0f672f62019-12-10 10:32:29 +00001339 dev->priv_flags |= IFF_NO_RX_HANDLER;
1340 dev->priv_flags |= IFF_LIVE_ADDR_CHANGE;
1341
1342 /* VRF devices do not care about MTU, but if the MTU is set
1343 * too low then the ipv4 and ipv6 protocols are disabled
1344 * which breaks networking.
1345 */
1346 dev->min_mtu = IPV6_MIN_MTU;
Olivier Deprez0e641232021-09-23 10:07:05 +02001347 dev->max_mtu = IP6_MAX_MTU;
1348 dev->mtu = dev->max_mtu;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001349}
1350
1351static int vrf_validate(struct nlattr *tb[], struct nlattr *data[],
1352 struct netlink_ext_ack *extack)
1353{
1354 if (tb[IFLA_ADDRESS]) {
1355 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) {
1356 NL_SET_ERR_MSG(extack, "Invalid hardware address");
1357 return -EINVAL;
1358 }
1359 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) {
1360 NL_SET_ERR_MSG(extack, "Invalid hardware address");
1361 return -EADDRNOTAVAIL;
1362 }
1363 }
1364 return 0;
1365}
1366
1367static void vrf_dellink(struct net_device *dev, struct list_head *head)
1368{
1369 struct net_device *port_dev;
1370 struct list_head *iter;
1371
1372 netdev_for_each_lower_dev(dev, port_dev, iter)
1373 vrf_del_slave(dev, port_dev);
1374
1375 unregister_netdevice_queue(dev, head);
1376}
1377
1378static int vrf_newlink(struct net *src_net, struct net_device *dev,
1379 struct nlattr *tb[], struct nlattr *data[],
1380 struct netlink_ext_ack *extack)
1381{
1382 struct net_vrf *vrf = netdev_priv(dev);
1383 bool *add_fib_rules;
1384 struct net *net;
1385 int err;
1386
1387 if (!data || !data[IFLA_VRF_TABLE]) {
1388 NL_SET_ERR_MSG(extack, "VRF table id is missing");
1389 return -EINVAL;
1390 }
1391
1392 vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
1393 if (vrf->tb_id == RT_TABLE_UNSPEC) {
1394 NL_SET_ERR_MSG_ATTR(extack, data[IFLA_VRF_TABLE],
1395 "Invalid VRF table id");
1396 return -EINVAL;
1397 }
1398
1399 dev->priv_flags |= IFF_L3MDEV_MASTER;
1400
1401 err = register_netdevice(dev);
1402 if (err)
1403 goto out;
1404
1405 net = dev_net(dev);
1406 add_fib_rules = net_generic(net, vrf_net_id);
1407 if (*add_fib_rules) {
1408 err = vrf_add_fib_rules(dev);
1409 if (err) {
1410 unregister_netdevice(dev);
1411 goto out;
1412 }
1413 *add_fib_rules = false;
1414 }
1415
1416out:
1417 return err;
1418}
1419
1420static size_t vrf_nl_getsize(const struct net_device *dev)
1421{
1422 return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
1423}
1424
1425static int vrf_fillinfo(struct sk_buff *skb,
1426 const struct net_device *dev)
1427{
1428 struct net_vrf *vrf = netdev_priv(dev);
1429
1430 return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
1431}
1432
1433static size_t vrf_get_slave_size(const struct net_device *bond_dev,
1434 const struct net_device *slave_dev)
1435{
1436 return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */
1437}
1438
1439static int vrf_fill_slave_info(struct sk_buff *skb,
1440 const struct net_device *vrf_dev,
1441 const struct net_device *slave_dev)
1442{
1443 struct net_vrf *vrf = netdev_priv(vrf_dev);
1444
1445 if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id))
1446 return -EMSGSIZE;
1447
1448 return 0;
1449}
1450
1451static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
1452 [IFLA_VRF_TABLE] = { .type = NLA_U32 },
1453};
1454
1455static struct rtnl_link_ops vrf_link_ops __read_mostly = {
1456 .kind = DRV_NAME,
1457 .priv_size = sizeof(struct net_vrf),
1458
1459 .get_size = vrf_nl_getsize,
1460 .policy = vrf_nl_policy,
1461 .validate = vrf_validate,
1462 .fill_info = vrf_fillinfo,
1463
1464 .get_slave_size = vrf_get_slave_size,
1465 .fill_slave_info = vrf_fill_slave_info,
1466
1467 .newlink = vrf_newlink,
1468 .dellink = vrf_dellink,
1469 .setup = vrf_setup,
1470 .maxtype = IFLA_VRF_MAX,
1471};
1472
1473static int vrf_device_event(struct notifier_block *unused,
1474 unsigned long event, void *ptr)
1475{
1476 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1477
1478 /* only care about unregister events to drop slave references */
1479 if (event == NETDEV_UNREGISTER) {
1480 struct net_device *vrf_dev;
1481
1482 if (!netif_is_l3_slave(dev))
1483 goto out;
1484
1485 vrf_dev = netdev_master_upper_dev_get(dev);
1486 vrf_del_slave(vrf_dev, dev);
1487 }
1488out:
1489 return NOTIFY_DONE;
1490}
1491
1492static struct notifier_block vrf_notifier_block __read_mostly = {
1493 .notifier_call = vrf_device_event,
1494};
1495
1496/* Initialize per network namespace state */
1497static int __net_init vrf_netns_init(struct net *net)
1498{
1499 bool *add_fib_rules = net_generic(net, vrf_net_id);
1500
1501 *add_fib_rules = true;
1502
1503 return 0;
1504}
1505
1506static struct pernet_operations vrf_net_ops __net_initdata = {
1507 .init = vrf_netns_init,
1508 .id = &vrf_net_id,
1509 .size = sizeof(bool),
1510};
1511
1512static int __init vrf_init_module(void)
1513{
1514 int rc;
1515
1516 register_netdevice_notifier(&vrf_notifier_block);
1517
1518 rc = register_pernet_subsys(&vrf_net_ops);
1519 if (rc < 0)
1520 goto error;
1521
1522 rc = rtnl_link_register(&vrf_link_ops);
1523 if (rc < 0) {
1524 unregister_pernet_subsys(&vrf_net_ops);
1525 goto error;
1526 }
1527
1528 return 0;
1529
1530error:
1531 unregister_netdevice_notifier(&vrf_notifier_block);
1532 return rc;
1533}
1534
1535module_init(vrf_init_module);
1536MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
1537MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
1538MODULE_LICENSE("GPL");
1539MODULE_ALIAS_RTNL_LINK(DRV_NAME);
1540MODULE_VERSION(DRV_VERSION);