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