blob: 42c9480acdc70b597c277398cea7021684679052 [file] [log] [blame]
David Ahern193125d2015-08-13 14:59:10 -06001/*
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>
David Ahern8f583362015-08-27 10:10:50 -070030#include <net/arp.h>
David Ahern193125d2015-08-13 14:59:10 -060031#include <net/ip.h>
32#include <net/ip_fib.h>
David Ahern35402e32015-10-12 11:47:09 -070033#include <net/ip6_fib.h>
David Ahern193125d2015-08-13 14:59:10 -060034#include <net/ip6_route.h>
David Ahern193125d2015-08-13 14:59:10 -060035#include <net/route.h>
36#include <net/addrconf.h>
David Ahernee15ee52015-09-29 20:07:12 -070037#include <net/l3mdev.h>
David Ahern1aa6c4f2016-06-08 10:55:40 -070038#include <net/fib_rules.h>
David Ahern386ed382017-06-08 11:31:11 -060039#include <net/netns/generic.h>
David Ahern193125d2015-08-13 14:59:10 -060040
41#define DRV_NAME "vrf"
42#define DRV_VERSION "1.0"
43
David Ahern1aa6c4f2016-06-08 10:55:40 -070044#define FIB_RULE_PREF 1000 /* default preference for FIB rules */
David Ahern386ed382017-06-08 11:31:11 -060045
46static unsigned int vrf_net_id;
David Ahern1aa6c4f2016-06-08 10:55:40 -070047
David Ahernec539512015-09-29 20:07:17 -070048struct net_vrf {
David Ahernb0e95cc2016-05-13 12:23:45 -070049 struct rtable __rcu *rth;
David Ahernafe80a42016-06-06 20:50:39 -070050 struct rtable __rcu *rth_local;
David Ahernb0e95cc2016-05-13 12:23:45 -070051 struct rt6_info __rcu *rt6;
David Ahernb4869aa2016-06-06 20:50:40 -070052 struct rt6_info __rcu *rt6_local;
David Ahernec539512015-09-29 20:07:17 -070053 u32 tb_id;
54};
55
David Ahern193125d2015-08-13 14:59:10 -060056struct pcpu_dstats {
57 u64 tx_pkts;
58 u64 tx_bytes;
59 u64 tx_drps;
60 u64 rx_pkts;
61 u64 rx_bytes;
David Ahernafe80a42016-06-06 20:50:39 -070062 u64 rx_drps;
David Ahern193125d2015-08-13 14:59:10 -060063 struct u64_stats_sync syncp;
64};
65
David Ahernafe80a42016-06-06 20:50:39 -070066static void vrf_rx_stats(struct net_device *dev, int len)
67{
68 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
69
70 u64_stats_update_begin(&dstats->syncp);
71 dstats->rx_pkts++;
72 dstats->rx_bytes += len;
73 u64_stats_update_end(&dstats->syncp);
74}
75
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +030076static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb)
77{
78 vrf_dev->stats.tx_errors++;
79 kfree_skb(skb);
80}
81
David Ahern193125d2015-08-13 14:59:10 -060082static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev,
83 struct rtnl_link_stats64 *stats)
84{
85 int i;
86
87 for_each_possible_cpu(i) {
88 const struct pcpu_dstats *dstats;
89 u64 tbytes, tpkts, tdrops, rbytes, rpkts;
90 unsigned int start;
91
92 dstats = per_cpu_ptr(dev->dstats, i);
93 do {
94 start = u64_stats_fetch_begin_irq(&dstats->syncp);
95 tbytes = dstats->tx_bytes;
96 tpkts = dstats->tx_pkts;
97 tdrops = dstats->tx_drps;
98 rbytes = dstats->rx_bytes;
99 rpkts = dstats->rx_pkts;
100 } while (u64_stats_fetch_retry_irq(&dstats->syncp, start));
101 stats->tx_bytes += tbytes;
102 stats->tx_packets += tpkts;
103 stats->tx_dropped += tdrops;
104 stats->rx_bytes += rbytes;
105 stats->rx_packets += rpkts;
106 }
107 return stats;
108}
109
David Ahernafe80a42016-06-06 20:50:39 -0700110/* Local traffic destined to local address. Reinsert the packet to rx
111 * path, similar to loopback handling.
112 */
113static int vrf_local_xmit(struct sk_buff *skb, struct net_device *dev,
114 struct dst_entry *dst)
115{
116 int len = skb->len;
117
118 skb_orphan(skb);
119
120 skb_dst_set(skb, dst);
121 skb_dst_force(skb);
122
123 /* set pkt_type to avoid skb hitting packet taps twice -
124 * once on Tx and again in Rx processing
125 */
126 skb->pkt_type = PACKET_LOOPBACK;
127
128 skb->protocol = eth_type_trans(skb, dev);
129
130 if (likely(netif_rx(skb) == NET_RX_SUCCESS))
131 vrf_rx_stats(dev, len);
132 else
133 this_cpu_inc(dev->dstats->rx_drps);
134
135 return NETDEV_TX_OK;
136}
137
David Ahern35402e32015-10-12 11:47:09 -0700138#if IS_ENABLED(CONFIG_IPV6)
David Ahern4c1feac2016-09-10 12:09:56 -0700139static int vrf_ip6_local_out(struct net *net, struct sock *sk,
140 struct sk_buff *skb)
141{
142 int err;
143
144 err = nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net,
145 sk, skb, NULL, skb_dst(skb)->dev, dst_output);
146
147 if (likely(err == 1))
148 err = dst_output(net, sk, skb);
149
150 return err;
151}
152
David Ahern35402e32015-10-12 11:47:09 -0700153static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
154 struct net_device *dev)
155{
156 const struct ipv6hdr *iph = ipv6_hdr(skb);
157 struct net *net = dev_net(skb->dev);
158 struct flowi6 fl6 = {
159 /* needed to match OIF rule */
160 .flowi6_oif = dev->ifindex,
161 .flowi6_iif = LOOPBACK_IFINDEX,
162 .daddr = iph->daddr,
163 .saddr = iph->saddr,
164 .flowlabel = ip6_flowinfo(iph),
165 .flowi6_mark = skb->mark,
166 .flowi6_proto = iph->nexthdr,
David Ahernc71ad3d2016-09-10 12:10:02 -0700167 .flowi6_flags = FLOWI_FLAG_SKIP_NH_OIF,
David Ahern35402e32015-10-12 11:47:09 -0700168 };
169 int ret = NET_XMIT_DROP;
170 struct dst_entry *dst;
171 struct dst_entry *dst_null = &net->ipv6.ip6_null_entry->dst;
172
173 dst = ip6_route_output(net, NULL, &fl6);
174 if (dst == dst_null)
175 goto err;
176
177 skb_dst_drop(skb);
David Ahernb4869aa2016-06-06 20:50:40 -0700178
179 /* if dst.dev is loopback or the VRF device again this is locally
180 * originated traffic destined to a local address. Short circuit
181 * to Rx path using our local dst
182 */
183 if (dst->dev == net->loopback_dev || dst->dev == dev) {
184 struct net_vrf *vrf = netdev_priv(dev);
185 struct rt6_info *rt6_local;
186
187 /* release looked up dst and use cached local dst */
188 dst_release(dst);
189
190 rcu_read_lock();
191
192 rt6_local = rcu_dereference(vrf->rt6_local);
193 if (unlikely(!rt6_local)) {
194 rcu_read_unlock();
195 goto err;
196 }
197
198 /* Ordering issue: cached local dst is created on newlink
199 * before the IPv6 initialization. Using the local dst
200 * requires rt6i_idev to be set so make sure it is.
201 */
202 if (unlikely(!rt6_local->rt6i_idev)) {
203 rt6_local->rt6i_idev = in6_dev_get(dev);
204 if (!rt6_local->rt6i_idev) {
205 rcu_read_unlock();
206 goto err;
207 }
208 }
209
210 dst = &rt6_local->dst;
211 dst_hold(dst);
212
213 rcu_read_unlock();
214
215 return vrf_local_xmit(skb, dev, &rt6_local->dst);
216 }
217
David Ahern35402e32015-10-12 11:47:09 -0700218 skb_dst_set(skb, dst);
219
David Ahern911a66f2016-06-06 20:50:38 -0700220 /* strip the ethernet header added for pass through VRF device */
221 __skb_pull(skb, skb_network_offset(skb));
222
David Ahern4c1feac2016-09-10 12:09:56 -0700223 ret = vrf_ip6_local_out(net, skb->sk, skb);
David Ahern35402e32015-10-12 11:47:09 -0700224 if (unlikely(net_xmit_eval(ret)))
225 dev->stats.tx_errors++;
226 else
227 ret = NET_XMIT_SUCCESS;
228
229 return ret;
230err:
231 vrf_tx_error(dev, skb);
232 return NET_XMIT_DROP;
233}
234#else
David Ahern193125d2015-08-13 14:59:10 -0600235static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb,
236 struct net_device *dev)
237{
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +0300238 vrf_tx_error(dev, skb);
239 return NET_XMIT_DROP;
David Ahern193125d2015-08-13 14:59:10 -0600240}
David Ahern35402e32015-10-12 11:47:09 -0700241#endif
David Ahern193125d2015-08-13 14:59:10 -0600242
David Ahernebfc1022016-09-10 12:09:55 -0700243/* based on ip_local_out; can't use it b/c the dst is switched pointing to us */
244static int vrf_ip_local_out(struct net *net, struct sock *sk,
245 struct sk_buff *skb)
246{
247 int err;
248
249 err = nf_hook(NFPROTO_IPV4, NF_INET_LOCAL_OUT, net, sk,
250 skb, NULL, skb_dst(skb)->dev, dst_output);
251 if (likely(err == 1))
252 err = dst_output(net, sk, skb);
253
254 return err;
255}
256
David Ahern193125d2015-08-13 14:59:10 -0600257static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb,
258 struct net_device *vrf_dev)
259{
260 struct iphdr *ip4h = ip_hdr(skb);
261 int ret = NET_XMIT_DROP;
262 struct flowi4 fl4 = {
263 /* needed to match OIF rule */
264 .flowi4_oif = vrf_dev->ifindex,
265 .flowi4_iif = LOOPBACK_IFINDEX,
266 .flowi4_tos = RT_TOS(ip4h->tos),
David Ahernc71ad3d2016-09-10 12:10:02 -0700267 .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_SKIP_NH_OIF,
David Ahern7b7a5a82017-01-10 14:37:35 -0800268 .flowi4_proto = ip4h->protocol,
David Ahern193125d2015-08-13 14:59:10 -0600269 .daddr = ip4h->daddr,
David Ahern7b7a5a82017-01-10 14:37:35 -0800270 .saddr = ip4h->saddr,
David Ahern193125d2015-08-13 14:59:10 -0600271 };
David Ahern911a66f2016-06-06 20:50:38 -0700272 struct net *net = dev_net(vrf_dev);
273 struct rtable *rt;
David Ahern193125d2015-08-13 14:59:10 -0600274
David Ahern911a66f2016-06-06 20:50:38 -0700275 rt = ip_route_output_flow(net, &fl4, NULL);
276 if (IS_ERR(rt))
David Ahern193125d2015-08-13 14:59:10 -0600277 goto err;
278
David Ahern911a66f2016-06-06 20:50:38 -0700279 if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) {
280 ip_rt_put(rt);
281 goto err;
282 }
283
284 skb_dst_drop(skb);
David Ahernafe80a42016-06-06 20:50:39 -0700285
286 /* if dst.dev is loopback or the VRF device again this is locally
287 * originated traffic destined to a local address. Short circuit
288 * to Rx path using our local dst
289 */
290 if (rt->dst.dev == net->loopback_dev || rt->dst.dev == vrf_dev) {
291 struct net_vrf *vrf = netdev_priv(vrf_dev);
292 struct rtable *rth_local;
293 struct dst_entry *dst = NULL;
294
295 ip_rt_put(rt);
296
297 rcu_read_lock();
298
299 rth_local = rcu_dereference(vrf->rth_local);
300 if (likely(rth_local)) {
301 dst = &rth_local->dst;
302 dst_hold(dst);
303 }
304
305 rcu_read_unlock();
306
307 if (unlikely(!dst))
308 goto err;
309
310 return vrf_local_xmit(skb, vrf_dev, dst);
311 }
312
David Ahern911a66f2016-06-06 20:50:38 -0700313 skb_dst_set(skb, &rt->dst);
314
315 /* strip the ethernet header added for pass through VRF device */
316 __skb_pull(skb, skb_network_offset(skb));
317
David Ahern193125d2015-08-13 14:59:10 -0600318 if (!ip4h->saddr) {
319 ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0,
320 RT_SCOPE_LINK);
321 }
322
David Ahernebfc1022016-09-10 12:09:55 -0700323 ret = vrf_ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb);
David Ahern193125d2015-08-13 14:59:10 -0600324 if (unlikely(net_xmit_eval(ret)))
325 vrf_dev->stats.tx_errors++;
326 else
327 ret = NET_XMIT_SUCCESS;
328
329out:
330 return ret;
331err:
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +0300332 vrf_tx_error(vrf_dev, skb);
David Ahern193125d2015-08-13 14:59:10 -0600333 goto out;
334}
335
336static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev)
337{
338 switch (skb->protocol) {
339 case htons(ETH_P_IP):
340 return vrf_process_v4_outbound(skb, dev);
341 case htons(ETH_P_IPV6):
342 return vrf_process_v6_outbound(skb, dev);
343 default:
Nikolay Aleksandrov57b8efa2015-08-19 06:12:29 +0300344 vrf_tx_error(dev, skb);
David Ahern193125d2015-08-13 14:59:10 -0600345 return NET_XMIT_DROP;
346 }
347}
348
349static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev)
350{
David Aherndb6e7792017-03-06 08:53:04 -0800351 int len = skb->len;
David Ahern193125d2015-08-13 14:59:10 -0600352 netdev_tx_t ret = is_ip_tx_frame(skb, dev);
353
354 if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) {
355 struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats);
356
357 u64_stats_update_begin(&dstats->syncp);
358 dstats->tx_pkts++;
David Aherndb6e7792017-03-06 08:53:04 -0800359 dstats->tx_bytes += len;
David Ahern193125d2015-08-13 14:59:10 -0600360 u64_stats_update_end(&dstats->syncp);
361 } else {
362 this_cpu_inc(dev->dstats->tx_drps);
363 }
364
365 return ret;
366}
367
David Ahern35402e32015-10-12 11:47:09 -0700368#if IS_ENABLED(CONFIG_IPV6)
David Ahern35402e32015-10-12 11:47:09 -0700369/* modelled after ip6_finish_output2 */
370static int vrf_finish_output6(struct net *net, struct sock *sk,
371 struct sk_buff *skb)
372{
373 struct dst_entry *dst = skb_dst(skb);
374 struct net_device *dev = dst->dev;
375 struct neighbour *neigh;
376 struct in6_addr *nexthop;
377 int ret;
378
David Ahern8b8fbe52016-12-14 14:31:11 -0800379 nf_reset(skb);
380
David Ahern35402e32015-10-12 11:47:09 -0700381 skb->protocol = htons(ETH_P_IPV6);
382 skb->dev = dev;
383
384 rcu_read_lock_bh();
385 nexthop = rt6_nexthop((struct rt6_info *)dst, &ipv6_hdr(skb)->daddr);
386 neigh = __ipv6_neigh_lookup_noref(dst->dev, nexthop);
387 if (unlikely(!neigh))
388 neigh = __neigh_create(&nd_tbl, nexthop, dst->dev, false);
389 if (!IS_ERR(neigh)) {
390 ret = dst_neigh_output(dst, neigh, skb);
391 rcu_read_unlock_bh();
392 return ret;
393 }
394 rcu_read_unlock_bh();
395
396 IP6_INC_STATS(dev_net(dst->dev),
397 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
398 kfree_skb(skb);
399 return -EINVAL;
400}
401
402/* modelled after ip6_output */
403static int vrf_output6(struct net *net, struct sock *sk, struct sk_buff *skb)
404{
405 return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING,
406 net, sk, skb, NULL, skb_dst(skb)->dev,
407 vrf_finish_output6,
408 !(IP6CB(skb)->flags & IP6SKB_REROUTED));
409}
410
David Ahern4c1feac2016-09-10 12:09:56 -0700411/* set dst on skb to send packet to us via dev_xmit path. Allows
412 * packet to go through device based features such as qdisc, netfilter
413 * hooks and packet sockets with skb->dev set to vrf device.
414 */
415static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
416 struct sock *sk,
417 struct sk_buff *skb)
418{
419 struct net_vrf *vrf = netdev_priv(vrf_dev);
420 struct dst_entry *dst = NULL;
421 struct rt6_info *rt6;
422
423 /* don't divert link scope packets */
424 if (rt6_need_strict(&ipv6_hdr(skb)->daddr))
425 return skb;
426
427 rcu_read_lock();
428
429 rt6 = rcu_dereference(vrf->rt6);
430 if (likely(rt6)) {
431 dst = &rt6->dst;
432 dst_hold(dst);
433 }
434
435 rcu_read_unlock();
436
437 if (unlikely(!dst)) {
438 vrf_tx_error(vrf_dev, skb);
439 return NULL;
440 }
441
442 skb_dst_drop(skb);
443 skb_dst_set(skb, dst);
444
445 return skb;
446}
447
David Ahernb0e95cc2016-05-13 12:23:45 -0700448/* holding rtnl */
David Ahern810e5302016-06-14 11:37:21 -0700449static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
David Ahern35402e32015-10-12 11:47:09 -0700450{
David Ahernb0e95cc2016-05-13 12:23:45 -0700451 struct rt6_info *rt6 = rtnl_dereference(vrf->rt6);
David Ahernb4869aa2016-06-06 20:50:40 -0700452 struct rt6_info *rt6_local = rtnl_dereference(vrf->rt6_local);
David Ahern810e5302016-06-14 11:37:21 -0700453 struct net *net = dev_net(dev);
454 struct dst_entry *dst;
David Ahernb0e95cc2016-05-13 12:23:45 -0700455
David Ahernb4869aa2016-06-06 20:50:40 -0700456 RCU_INIT_POINTER(vrf->rt6, NULL);
457 RCU_INIT_POINTER(vrf->rt6_local, NULL);
458 synchronize_rcu();
David Ahernb0e95cc2016-05-13 12:23:45 -0700459
David Ahern810e5302016-06-14 11:37:21 -0700460 /* move dev in dst's to loopback so this VRF device can be deleted
461 * - based on dst_ifdown
462 */
463 if (rt6) {
464 dst = &rt6->dst;
465 dev_put(dst->dev);
466 dst->dev = net->loopback_dev;
467 dev_hold(dst->dev);
468 dst_release(dst);
469 }
David Ahernb4869aa2016-06-06 20:50:40 -0700470
471 if (rt6_local) {
David Ahern4e7c8212017-03-17 16:07:11 -0700472 if (rt6_local->rt6i_idev) {
David Ahernb4869aa2016-06-06 20:50:40 -0700473 in6_dev_put(rt6_local->rt6i_idev);
David Ahern4e7c8212017-03-17 16:07:11 -0700474 rt6_local->rt6i_idev = NULL;
475 }
David Ahernb4869aa2016-06-06 20:50:40 -0700476
David Ahern810e5302016-06-14 11:37:21 -0700477 dst = &rt6_local->dst;
478 dev_put(dst->dev);
479 dst->dev = net->loopback_dev;
480 dev_hold(dst->dev);
481 dst_release(dst);
David Ahernb4869aa2016-06-06 20:50:40 -0700482 }
David Ahern35402e32015-10-12 11:47:09 -0700483}
484
485static int vrf_rt6_create(struct net_device *dev)
486{
David Ahernb4869aa2016-06-06 20:50:40 -0700487 int flags = DST_HOST | DST_NOPOLICY | DST_NOXFRM | DST_NOCACHE;
David Ahern35402e32015-10-12 11:47:09 -0700488 struct net_vrf *vrf = netdev_priv(dev);
David Ahern9ab179d2016-04-07 11:10:06 -0700489 struct net *net = dev_net(dev);
David Ahernb3b46632016-05-04 21:46:12 -0700490 struct fib6_table *rt6i_table;
David Ahernb4869aa2016-06-06 20:50:40 -0700491 struct rt6_info *rt6, *rt6_local;
David Ahern35402e32015-10-12 11:47:09 -0700492 int rc = -ENOMEM;
493
David Aherne4348632016-06-09 10:21:00 -0700494 /* IPv6 can be CONFIG enabled and then disabled runtime */
495 if (!ipv6_mod_enabled())
496 return 0;
497
David Ahernb3b46632016-05-04 21:46:12 -0700498 rt6i_table = fib6_new_table(net, vrf->tb_id);
499 if (!rt6i_table)
500 goto out;
501
David Ahernb4869aa2016-06-06 20:50:40 -0700502 /* create a dst for routing packets out a VRF device */
503 rt6 = ip6_dst_alloc(net, dev, flags);
David Ahern35402e32015-10-12 11:47:09 -0700504 if (!rt6)
505 goto out;
506
David Ahern9ab179d2016-04-07 11:10:06 -0700507 dst_hold(&rt6->dst);
David Ahernb3b46632016-05-04 21:46:12 -0700508
509 rt6->rt6i_table = rt6i_table;
510 rt6->dst.output = vrf_output6;
David Ahernb4869aa2016-06-06 20:50:40 -0700511
512 /* create a dst for local routing - packets sent locally
513 * to local address via the VRF device as a loopback
514 */
515 rt6_local = ip6_dst_alloc(net, dev, flags);
516 if (!rt6_local) {
517 dst_release(&rt6->dst);
518 goto out;
519 }
520
521 dst_hold(&rt6_local->dst);
522
523 rt6_local->rt6i_idev = in6_dev_get(dev);
524 rt6_local->rt6i_flags = RTF_UP | RTF_NONEXTHOP | RTF_LOCAL;
525 rt6_local->rt6i_table = rt6i_table;
526 rt6_local->dst.input = ip6_input;
527
David Ahernb0e95cc2016-05-13 12:23:45 -0700528 rcu_assign_pointer(vrf->rt6, rt6);
David Ahernb4869aa2016-06-06 20:50:40 -0700529 rcu_assign_pointer(vrf->rt6_local, rt6_local);
David Ahernb0e95cc2016-05-13 12:23:45 -0700530
David Ahern35402e32015-10-12 11:47:09 -0700531 rc = 0;
532out:
533 return rc;
534}
535#else
David Ahern4c1feac2016-09-10 12:09:56 -0700536static struct sk_buff *vrf_ip6_out(struct net_device *vrf_dev,
537 struct sock *sk,
538 struct sk_buff *skb)
539{
540 return skb;
541}
542
David Ahern810e5302016-06-14 11:37:21 -0700543static void vrf_rt6_release(struct net_device *dev, struct net_vrf *vrf)
David Ahern35402e32015-10-12 11:47:09 -0700544{
545}
546
547static int vrf_rt6_create(struct net_device *dev)
548{
549 return 0;
550}
551#endif
552
David Ahern8f583362015-08-27 10:10:50 -0700553/* modelled after ip_finish_output2 */
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -0500554static int vrf_finish_output(struct net *net, struct sock *sk, struct sk_buff *skb)
David Ahern193125d2015-08-13 14:59:10 -0600555{
David Ahern8f583362015-08-27 10:10:50 -0700556 struct dst_entry *dst = skb_dst(skb);
557 struct rtable *rt = (struct rtable *)dst;
558 struct net_device *dev = dst->dev;
559 unsigned int hh_len = LL_RESERVED_SPACE(dev);
560 struct neighbour *neigh;
561 u32 nexthop;
562 int ret = -EINVAL;
563
David Ahern8b8fbe52016-12-14 14:31:11 -0800564 nf_reset(skb);
565
David Ahern8f583362015-08-27 10:10:50 -0700566 /* Be paranoid, rather than too clever. */
567 if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) {
568 struct sk_buff *skb2;
569
570 skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev));
571 if (!skb2) {
572 ret = -ENOMEM;
573 goto err;
574 }
575 if (skb->sk)
576 skb_set_owner_w(skb2, skb->sk);
577
578 consume_skb(skb);
579 skb = skb2;
580 }
581
582 rcu_read_lock_bh();
583
584 nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr);
585 neigh = __ipv4_neigh_lookup_noref(dev, nexthop);
586 if (unlikely(!neigh))
587 neigh = __neigh_create(&arp_tbl, &nexthop, dev, false);
David Ahern89cd6582018-03-29 12:49:52 -0700588 if (!IS_ERR(neigh)) {
David Ahern8f583362015-08-27 10:10:50 -0700589 ret = dst_neigh_output(dst, neigh, skb);
David Ahern89cd6582018-03-29 12:49:52 -0700590 rcu_read_unlock_bh();
591 return ret;
592 }
David Ahern8f583362015-08-27 10:10:50 -0700593
594 rcu_read_unlock_bh();
595err:
David Ahern89cd6582018-03-29 12:49:52 -0700596 vrf_tx_error(skb->dev, skb);
David Ahern8f583362015-08-27 10:10:50 -0700597 return ret;
David Ahern193125d2015-08-13 14:59:10 -0600598}
599
Eric W. Biedermanede20592015-10-07 16:48:47 -0500600static int vrf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
David Ahern193125d2015-08-13 14:59:10 -0600601{
602 struct net_device *dev = skb_dst(skb)->dev;
603
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500604 IP_UPD_PO_STATS(net, IPSTATS_MIB_OUT, skb->len);
David Ahern193125d2015-08-13 14:59:10 -0600605
606 skb->dev = dev;
607 skb->protocol = htons(ETH_P_IP);
608
Eric W. Biederman29a26a52015-09-15 20:04:16 -0500609 return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING,
610 net, sk, skb, NULL, dev,
David Ahern8f583362015-08-27 10:10:50 -0700611 vrf_finish_output,
David Ahern193125d2015-08-13 14:59:10 -0600612 !(IPCB(skb)->flags & IPSKB_REROUTED));
613}
614
David Ahernebfc1022016-09-10 12:09:55 -0700615/* set dst on skb to send packet to us via dev_xmit path. Allows
616 * packet to go through device based features such as qdisc, netfilter
617 * hooks and packet sockets with skb->dev set to vrf device.
618 */
619static struct sk_buff *vrf_ip_out(struct net_device *vrf_dev,
620 struct sock *sk,
621 struct sk_buff *skb)
622{
623 struct net_vrf *vrf = netdev_priv(vrf_dev);
624 struct dst_entry *dst = NULL;
625 struct rtable *rth;
626
627 rcu_read_lock();
628
629 rth = rcu_dereference(vrf->rth);
630 if (likely(rth)) {
631 dst = &rth->dst;
632 dst_hold(dst);
633 }
634
635 rcu_read_unlock();
636
637 if (unlikely(!dst)) {
638 vrf_tx_error(vrf_dev, skb);
639 return NULL;
640 }
641
642 skb_dst_drop(skb);
643 skb_dst_set(skb, dst);
644
645 return skb;
646}
647
648/* called with rcu lock held */
649static struct sk_buff *vrf_l3_out(struct net_device *vrf_dev,
650 struct sock *sk,
651 struct sk_buff *skb,
652 u16 proto)
653{
654 switch (proto) {
655 case AF_INET:
656 return vrf_ip_out(vrf_dev, sk, skb);
David Ahern4c1feac2016-09-10 12:09:56 -0700657 case AF_INET6:
658 return vrf_ip6_out(vrf_dev, sk, skb);
David Ahernebfc1022016-09-10 12:09:55 -0700659 }
660
661 return skb;
662}
663
David Ahernb0e95cc2016-05-13 12:23:45 -0700664/* holding rtnl */
David Ahern810e5302016-06-14 11:37:21 -0700665static void vrf_rtable_release(struct net_device *dev, struct net_vrf *vrf)
David Ahern193125d2015-08-13 14:59:10 -0600666{
David Ahernb0e95cc2016-05-13 12:23:45 -0700667 struct rtable *rth = rtnl_dereference(vrf->rth);
David Ahernafe80a42016-06-06 20:50:39 -0700668 struct rtable *rth_local = rtnl_dereference(vrf->rth_local);
David Ahern810e5302016-06-14 11:37:21 -0700669 struct net *net = dev_net(dev);
670 struct dst_entry *dst;
David Ahern193125d2015-08-13 14:59:10 -0600671
David Ahernafe80a42016-06-06 20:50:39 -0700672 RCU_INIT_POINTER(vrf->rth, NULL);
673 RCU_INIT_POINTER(vrf->rth_local, NULL);
674 synchronize_rcu();
David Ahernb0e95cc2016-05-13 12:23:45 -0700675
David Ahern810e5302016-06-14 11:37:21 -0700676 /* move dev in dst's to loopback so this VRF device can be deleted
677 * - based on dst_ifdown
678 */
679 if (rth) {
680 dst = &rth->dst;
681 dev_put(dst->dev);
682 dst->dev = net->loopback_dev;
683 dev_hold(dst->dev);
684 dst_release(dst);
685 }
David Ahernafe80a42016-06-06 20:50:39 -0700686
David Ahern810e5302016-06-14 11:37:21 -0700687 if (rth_local) {
688 dst = &rth_local->dst;
689 dev_put(dst->dev);
690 dst->dev = net->loopback_dev;
691 dev_hold(dst->dev);
692 dst_release(dst);
693 }
David Ahern193125d2015-08-13 14:59:10 -0600694}
695
David Ahernb0e95cc2016-05-13 12:23:45 -0700696static int vrf_rtable_create(struct net_device *dev)
David Ahern193125d2015-08-13 14:59:10 -0600697{
David Ahernb7503e02015-09-02 13:58:35 -0700698 struct net_vrf *vrf = netdev_priv(dev);
David Ahernafe80a42016-06-06 20:50:39 -0700699 struct rtable *rth, *rth_local;
David Ahern193125d2015-08-13 14:59:10 -0600700
David Ahernb3b46632016-05-04 21:46:12 -0700701 if (!fib_new_table(dev_net(dev), vrf->tb_id))
David Ahernb0e95cc2016-05-13 12:23:45 -0700702 return -ENOMEM;
David Ahernb3b46632016-05-04 21:46:12 -0700703
David Ahernafe80a42016-06-06 20:50:39 -0700704 /* create a dst for routing packets out through a VRF device */
David Ahern9ab179d2016-04-07 11:10:06 -0700705 rth = rt_dst_alloc(dev, 0, RTN_UNICAST, 1, 1, 0);
David Ahernb0e95cc2016-05-13 12:23:45 -0700706 if (!rth)
707 return -ENOMEM;
David Ahern193125d2015-08-13 14:59:10 -0600708
David Ahernafe80a42016-06-06 20:50:39 -0700709 /* create a dst for local ingress routing - packets sent locally
710 * to local address via the VRF device as a loopback
711 */
712 rth_local = rt_dst_alloc(dev, RTCF_LOCAL, RTN_LOCAL, 1, 1, 0);
713 if (!rth_local) {
714 dst_release(&rth->dst);
715 return -ENOMEM;
716 }
717
David Ahernb0e95cc2016-05-13 12:23:45 -0700718 rth->dst.output = vrf_output;
719 rth->rt_table_id = vrf->tb_id;
720
David Ahernafe80a42016-06-06 20:50:39 -0700721 rth_local->rt_table_id = vrf->tb_id;
722
David Ahernb0e95cc2016-05-13 12:23:45 -0700723 rcu_assign_pointer(vrf->rth, rth);
David Ahernafe80a42016-06-06 20:50:39 -0700724 rcu_assign_pointer(vrf->rth_local, rth_local);
David Ahernb0e95cc2016-05-13 12:23:45 -0700725
726 return 0;
David Ahern193125d2015-08-13 14:59:10 -0600727}
728
729/**************************** device handling ********************/
730
731/* cycle interface to flush neighbor cache and move routes across tables */
732static void cycle_netdev(struct net_device *dev)
733{
734 unsigned int flags = dev->flags;
735 int ret;
736
737 if (!netif_running(dev))
738 return;
739
740 ret = dev_change_flags(dev, flags & ~IFF_UP);
741 if (ret >= 0)
742 ret = dev_change_flags(dev, flags);
743
744 if (ret < 0) {
745 netdev_err(dev,
746 "Failed to cycle device %s; route tables might be wrong!\n",
747 dev->name);
748 }
749}
750
David Ahern193125d2015-08-13 14:59:10 -0600751static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
752{
Nikolay Aleksandrovbad53162015-11-24 14:29:16 +0100753 int ret;
David Ahern193125d2015-08-13 14:59:10 -0600754
Jiri Pirko29bf24a2015-12-03 12:12:11 +0100755 ret = netdev_master_upper_dev_link(port_dev, dev, NULL, NULL);
David Ahern193125d2015-08-13 14:59:10 -0600756 if (ret < 0)
David Ahern74b20582016-05-10 11:19:50 -0700757 return ret;
David Ahern193125d2015-08-13 14:59:10 -0600758
David Ahernfee6d4c2015-10-05 08:51:24 -0700759 port_dev->priv_flags |= IFF_L3MDEV_SLAVE;
David Ahern193125d2015-08-13 14:59:10 -0600760 cycle_netdev(port_dev);
761
762 return 0;
David Ahern193125d2015-08-13 14:59:10 -0600763}
764
765static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev)
766{
David Ahernfee6d4c2015-10-05 08:51:24 -0700767 if (netif_is_l3_master(port_dev) || netif_is_l3_slave(port_dev))
David Ahern193125d2015-08-13 14:59:10 -0600768 return -EINVAL;
769
770 return do_vrf_add_slave(dev, port_dev);
771}
772
773/* inverse of do_vrf_add_slave */
774static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
775{
David Ahern193125d2015-08-13 14:59:10 -0600776 netdev_upper_dev_unlink(port_dev, dev);
David Ahernfee6d4c2015-10-05 08:51:24 -0700777 port_dev->priv_flags &= ~IFF_L3MDEV_SLAVE;
David Ahern193125d2015-08-13 14:59:10 -0600778
David Ahern193125d2015-08-13 14:59:10 -0600779 cycle_netdev(port_dev);
780
David Ahern193125d2015-08-13 14:59:10 -0600781 return 0;
782}
783
784static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev)
785{
David Ahern193125d2015-08-13 14:59:10 -0600786 return do_vrf_del_slave(dev, port_dev);
787}
788
789static void vrf_dev_uninit(struct net_device *dev)
790{
791 struct net_vrf *vrf = netdev_priv(dev);
David Ahern193125d2015-08-13 14:59:10 -0600792
David Ahern810e5302016-06-14 11:37:21 -0700793 vrf_rtable_release(dev, vrf);
794 vrf_rt6_release(dev, vrf);
David Ahern193125d2015-08-13 14:59:10 -0600795
Nikolay Aleksandrov3a4a27d2015-08-18 20:28:03 +0300796 free_percpu(dev->dstats);
David Ahern193125d2015-08-13 14:59:10 -0600797 dev->dstats = NULL;
798}
799
800static int vrf_dev_init(struct net_device *dev)
801{
802 struct net_vrf *vrf = netdev_priv(dev);
803
David Ahern193125d2015-08-13 14:59:10 -0600804 dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats);
805 if (!dev->dstats)
806 goto out_nomem;
807
808 /* create the default dst which points back to us */
David Ahernb0e95cc2016-05-13 12:23:45 -0700809 if (vrf_rtable_create(dev) != 0)
David Ahern193125d2015-08-13 14:59:10 -0600810 goto out_stats;
811
David Ahern35402e32015-10-12 11:47:09 -0700812 if (vrf_rt6_create(dev) != 0)
813 goto out_rth;
814
David Ahern193125d2015-08-13 14:59:10 -0600815 dev->flags = IFF_MASTER | IFF_NOARP;
816
David Ahernb87ab6b2016-06-01 21:16:39 -0700817 /* MTU is irrelevant for VRF device; set to 64k similar to lo */
818 dev->mtu = 64 * 1024;
819
820 /* similarly, oper state is irrelevant; set to up to avoid confusion */
821 dev->operstate = IF_OPER_UP;
Eric Dumazet78e7a2a2016-06-09 07:45:13 -0700822 netdev_lockdep_set_classes(dev);
David Ahern193125d2015-08-13 14:59:10 -0600823 return 0;
824
David Ahern35402e32015-10-12 11:47:09 -0700825out_rth:
David Ahern810e5302016-06-14 11:37:21 -0700826 vrf_rtable_release(dev, vrf);
David Ahern193125d2015-08-13 14:59:10 -0600827out_stats:
828 free_percpu(dev->dstats);
829 dev->dstats = NULL;
830out_nomem:
831 return -ENOMEM;
832}
833
834static const struct net_device_ops vrf_netdev_ops = {
835 .ndo_init = vrf_dev_init,
836 .ndo_uninit = vrf_dev_uninit,
837 .ndo_start_xmit = vrf_xmit,
838 .ndo_get_stats64 = vrf_get_stats64,
839 .ndo_add_slave = vrf_add_slave,
840 .ndo_del_slave = vrf_del_slave,
841};
842
David Ahernee15ee52015-09-29 20:07:12 -0700843static u32 vrf_fib_table(const struct net_device *dev)
844{
845 struct net_vrf *vrf = netdev_priv(dev);
846
847 return vrf->tb_id;
848}
849
David Ahern73e20b72016-07-04 18:47:41 -0700850static int vrf_rcv_finish(struct net *net, struct sock *sk, struct sk_buff *skb)
851{
Gao Feng8f1f08b2017-05-09 18:27:33 +0800852 kfree_skb(skb);
David Ahern73e20b72016-07-04 18:47:41 -0700853 return 0;
854}
855
856static struct sk_buff *vrf_rcv_nfhook(u8 pf, unsigned int hook,
857 struct sk_buff *skb,
858 struct net_device *dev)
859{
860 struct net *net = dev_net(dev);
861
Gao Feng8f1f08b2017-05-09 18:27:33 +0800862 if (nf_hook(pf, hook, net, NULL, skb, dev, NULL, vrf_rcv_finish) != 1)
David Ahern73e20b72016-07-04 18:47:41 -0700863 skb = NULL; /* kfree_skb(skb) handled by nf code */
864
865 return skb;
866}
867
David Ahern35402e32015-10-12 11:47:09 -0700868#if IS_ENABLED(CONFIG_IPV6)
David Ahern74b20582016-05-10 11:19:50 -0700869/* neighbor handling is done with actual device; do not want
870 * to flip skb->dev for those ndisc packets. This really fails
871 * for multiple next protocols (e.g., NEXTHDR_HOP). But it is
872 * a start.
873 */
874static bool ipv6_ndisc_frame(const struct sk_buff *skb)
875{
876 const struct ipv6hdr *iph = ipv6_hdr(skb);
877 bool rc = false;
878
879 if (iph->nexthdr == NEXTHDR_ICMP) {
880 const struct icmp6hdr *icmph;
881 struct icmp6hdr _icmph;
882
883 icmph = skb_header_pointer(skb, sizeof(*iph),
884 sizeof(_icmph), &_icmph);
885 if (!icmph)
886 goto out;
887
888 switch (icmph->icmp6_type) {
889 case NDISC_ROUTER_SOLICITATION:
890 case NDISC_ROUTER_ADVERTISEMENT:
891 case NDISC_NEIGHBOUR_SOLICITATION:
892 case NDISC_NEIGHBOUR_ADVERTISEMENT:
893 case NDISC_REDIRECT:
894 rc = true;
895 break;
896 }
897 }
898
899out:
900 return rc;
901}
902
David Ahern9ff74382016-06-13 13:44:19 -0700903static struct rt6_info *vrf_ip6_route_lookup(struct net *net,
904 const struct net_device *dev,
905 struct flowi6 *fl6,
906 int ifindex,
907 int flags)
908{
909 struct net_vrf *vrf = netdev_priv(dev);
910 struct fib6_table *table = NULL;
911 struct rt6_info *rt6;
912
913 rcu_read_lock();
914
915 /* fib6_table does not have a refcnt and can not be freed */
916 rt6 = rcu_dereference(vrf->rt6);
917 if (likely(rt6))
918 table = rt6->rt6i_table;
919
920 rcu_read_unlock();
921
922 if (!table)
923 return NULL;
924
925 return ip6_pol_route(net, table, ifindex, fl6, flags);
926}
927
928static void vrf_ip6_input_dst(struct sk_buff *skb, struct net_device *vrf_dev,
929 int ifindex)
930{
931 const struct ipv6hdr *iph = ipv6_hdr(skb);
932 struct flowi6 fl6 = {
933 .daddr = iph->daddr,
934 .saddr = iph->saddr,
935 .flowlabel = ip6_flowinfo(iph),
936 .flowi6_mark = skb->mark,
937 .flowi6_proto = iph->nexthdr,
938 .flowi6_iif = ifindex,
939 };
940 struct net *net = dev_net(vrf_dev);
941 struct rt6_info *rt6;
942
943 rt6 = vrf_ip6_route_lookup(net, vrf_dev, &fl6, ifindex,
944 RT6_LOOKUP_F_HAS_SADDR | RT6_LOOKUP_F_IFACE);
945 if (unlikely(!rt6))
946 return;
947
948 if (unlikely(&rt6->dst == &net->ipv6.ip6_null_entry->dst))
949 return;
950
951 skb_dst_set(skb, &rt6->dst);
952}
953
David Ahern74b20582016-05-10 11:19:50 -0700954static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
955 struct sk_buff *skb)
956{
David Ahern9ff74382016-06-13 13:44:19 -0700957 int orig_iif = skb->skb_iif;
958 bool need_strict;
959
David Ahernb4869aa2016-06-06 20:50:40 -0700960 /* loopback traffic; do not push through packet taps again.
961 * Reset pkt_type for upper layers to process skb
962 */
963 if (skb->pkt_type == PACKET_LOOPBACK) {
964 skb->dev = vrf_dev;
965 skb->skb_iif = vrf_dev->ifindex;
David Aherna04a4802016-10-16 20:02:52 -0700966 IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
David Ahernb4869aa2016-06-06 20:50:40 -0700967 skb->pkt_type = PACKET_HOST;
968 goto out;
969 }
970
David Ahern9ff74382016-06-13 13:44:19 -0700971 /* if packet is NDISC or addressed to multicast or link-local
972 * then keep the ingress interface
973 */
974 need_strict = rt6_need_strict(&ipv6_hdr(skb)->daddr);
975 if (!ipv6_ndisc_frame(skb) && !need_strict) {
David Aherna4d205a2017-01-03 09:37:55 -0800976 vrf_rx_stats(vrf_dev, skb->len);
David Ahern74b20582016-05-10 11:19:50 -0700977 skb->dev = vrf_dev;
978 skb->skb_iif = vrf_dev->ifindex;
979
980 skb_push(skb, skb->mac_len);
981 dev_queue_xmit_nit(skb, vrf_dev);
982 skb_pull(skb, skb->mac_len);
983
984 IP6CB(skb)->flags |= IP6SKB_L3SLAVE;
985 }
986
David Ahern9ff74382016-06-13 13:44:19 -0700987 if (need_strict)
988 vrf_ip6_input_dst(skb, vrf_dev, orig_iif);
989
David Ahern73e20b72016-07-04 18:47:41 -0700990 skb = vrf_rcv_nfhook(NFPROTO_IPV6, NF_INET_PRE_ROUTING, skb, vrf_dev);
David Ahernb4869aa2016-06-06 20:50:40 -0700991out:
David Ahern74b20582016-05-10 11:19:50 -0700992 return skb;
993}
994
995#else
996static struct sk_buff *vrf_ip6_rcv(struct net_device *vrf_dev,
997 struct sk_buff *skb)
998{
999 return skb;
1000}
1001#endif
1002
1003static struct sk_buff *vrf_ip_rcv(struct net_device *vrf_dev,
1004 struct sk_buff *skb)
1005{
1006 skb->dev = vrf_dev;
1007 skb->skb_iif = vrf_dev->ifindex;
David Aherna04a4802016-10-16 20:02:52 -07001008 IPCB(skb)->flags |= IPSKB_L3SLAVE;
David Ahern74b20582016-05-10 11:19:50 -07001009
David Ahernafe80a42016-06-06 20:50:39 -07001010 /* loopback traffic; do not push through packet taps again.
1011 * Reset pkt_type for upper layers to process skb
1012 */
1013 if (skb->pkt_type == PACKET_LOOPBACK) {
1014 skb->pkt_type = PACKET_HOST;
1015 goto out;
1016 }
1017
David Aherna4d205a2017-01-03 09:37:55 -08001018 vrf_rx_stats(vrf_dev, skb->len);
1019
David Ahern74b20582016-05-10 11:19:50 -07001020 skb_push(skb, skb->mac_len);
1021 dev_queue_xmit_nit(skb, vrf_dev);
1022 skb_pull(skb, skb->mac_len);
1023
David Ahern73e20b72016-07-04 18:47:41 -07001024 skb = vrf_rcv_nfhook(NFPROTO_IPV4, NF_INET_PRE_ROUTING, skb, vrf_dev);
David Ahernafe80a42016-06-06 20:50:39 -07001025out:
David Ahern74b20582016-05-10 11:19:50 -07001026 return skb;
1027}
1028
1029/* called with rcu lock held */
1030static struct sk_buff *vrf_l3_rcv(struct net_device *vrf_dev,
1031 struct sk_buff *skb,
1032 u16 proto)
1033{
1034 switch (proto) {
1035 case AF_INET:
1036 return vrf_ip_rcv(vrf_dev, skb);
1037 case AF_INET6:
1038 return vrf_ip6_rcv(vrf_dev, skb);
1039 }
1040
1041 return skb;
1042}
1043
1044#if IS_ENABLED(CONFIG_IPV6)
David Ahern4c1feac2016-09-10 12:09:56 -07001045/* send to link-local or multicast address via interface enslaved to
1046 * VRF device. Force lookup to VRF table without changing flow struct
1047 */
1048static struct dst_entry *vrf_link_scope_lookup(const struct net_device *dev,
1049 struct flowi6 *fl6)
David Ahern35402e32015-10-12 11:47:09 -07001050{
David Ahern9ff74382016-06-13 13:44:19 -07001051 struct net *net = dev_net(dev);
David Ahern4c1feac2016-09-10 12:09:56 -07001052 int flags = RT6_LOOKUP_F_IFACE;
David Ahernb0e95cc2016-05-13 12:23:45 -07001053 struct dst_entry *dst = NULL;
David Ahern9ff74382016-06-13 13:44:19 -07001054 struct rt6_info *rt;
David Ahern35402e32015-10-12 11:47:09 -07001055
David Ahern4c1feac2016-09-10 12:09:56 -07001056 /* VRF device does not have a link-local address and
1057 * sending packets to link-local or mcast addresses over
1058 * a VRF device does not make sense
1059 */
1060 if (fl6->flowi6_oif == dev->ifindex) {
1061 dst = &net->ipv6.ip6_null_entry->dst;
1062 dst_hold(dst);
1063 return dst;
David Ahern35402e32015-10-12 11:47:09 -07001064 }
1065
David Ahern4c1feac2016-09-10 12:09:56 -07001066 if (!ipv6_addr_any(&fl6->saddr))
1067 flags |= RT6_LOOKUP_F_HAS_SADDR;
1068
1069 rt = vrf_ip6_route_lookup(net, dev, fl6, fl6->flowi6_oif, flags);
1070 if (rt)
1071 dst = &rt->dst;
David Ahern9ff74382016-06-13 13:44:19 -07001072
David Ahernb0e95cc2016-05-13 12:23:45 -07001073 return dst;
David Ahern35402e32015-10-12 11:47:09 -07001074}
1075#endif
1076
David Ahernee15ee52015-09-29 20:07:12 -07001077static const struct l3mdev_ops vrf_l3mdev_ops = {
1078 .l3mdev_fib_table = vrf_fib_table,
David Ahern74b20582016-05-10 11:19:50 -07001079 .l3mdev_l3_rcv = vrf_l3_rcv,
David Ahernebfc1022016-09-10 12:09:55 -07001080 .l3mdev_l3_out = vrf_l3_out,
David Ahern35402e32015-10-12 11:47:09 -07001081#if IS_ENABLED(CONFIG_IPV6)
David Ahern4c1feac2016-09-10 12:09:56 -07001082 .l3mdev_link_scope_lookup = vrf_link_scope_lookup,
David Ahern35402e32015-10-12 11:47:09 -07001083#endif
David Ahernee15ee52015-09-29 20:07:12 -07001084};
1085
David Ahern193125d2015-08-13 14:59:10 -06001086static void vrf_get_drvinfo(struct net_device *dev,
1087 struct ethtool_drvinfo *info)
1088{
1089 strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
1090 strlcpy(info->version, DRV_VERSION, sizeof(info->version));
1091}
1092
1093static const struct ethtool_ops vrf_ethtool_ops = {
1094 .get_drvinfo = vrf_get_drvinfo,
1095};
1096
David Ahern1aa6c4f2016-06-08 10:55:40 -07001097static inline size_t vrf_fib_rule_nl_size(void)
1098{
1099 size_t sz;
1100
1101 sz = NLMSG_ALIGN(sizeof(struct fib_rule_hdr));
1102 sz += nla_total_size(sizeof(u8)); /* FRA_L3MDEV */
1103 sz += nla_total_size(sizeof(u32)); /* FRA_PRIORITY */
1104
1105 return sz;
1106}
1107
1108static int vrf_fib_rule(const struct net_device *dev, __u8 family, bool add_it)
1109{
1110 struct fib_rule_hdr *frh;
1111 struct nlmsghdr *nlh;
1112 struct sk_buff *skb;
1113 int err;
1114
David Aherne4348632016-06-09 10:21:00 -07001115 if (family == AF_INET6 && !ipv6_mod_enabled())
1116 return 0;
1117
David Ahern1aa6c4f2016-06-08 10:55:40 -07001118 skb = nlmsg_new(vrf_fib_rule_nl_size(), GFP_KERNEL);
1119 if (!skb)
1120 return -ENOMEM;
1121
1122 nlh = nlmsg_put(skb, 0, 0, 0, sizeof(*frh), 0);
1123 if (!nlh)
1124 goto nla_put_failure;
1125
1126 /* rule only needs to appear once */
David Ahernb4580d62017-04-13 10:57:15 -06001127 nlh->nlmsg_flags |= NLM_F_EXCL;
David Ahern1aa6c4f2016-06-08 10:55:40 -07001128
1129 frh = nlmsg_data(nlh);
1130 memset(frh, 0, sizeof(*frh));
1131 frh->family = family;
1132 frh->action = FR_ACT_TO_TBL;
1133
Jeff Barnhill58b21b02017-11-01 14:58:09 +00001134 if (nla_put_u8(skb, FRA_L3MDEV, 1))
David Ahern1aa6c4f2016-06-08 10:55:40 -07001135 goto nla_put_failure;
1136
1137 if (nla_put_u32(skb, FRA_PRIORITY, FIB_RULE_PREF))
1138 goto nla_put_failure;
1139
1140 nlmsg_end(skb, nlh);
1141
1142 /* fib_nl_{new,del}rule handling looks for net from skb->sk */
1143 skb->sk = dev_net(dev)->rtnl;
1144 if (add_it) {
1145 err = fib_nl_newrule(skb, nlh);
1146 if (err == -EEXIST)
1147 err = 0;
1148 } else {
1149 err = fib_nl_delrule(skb, nlh);
1150 if (err == -ENOENT)
1151 err = 0;
1152 }
1153 nlmsg_free(skb);
1154
1155 return err;
1156
1157nla_put_failure:
1158 nlmsg_free(skb);
1159
1160 return -EMSGSIZE;
1161}
1162
1163static int vrf_add_fib_rules(const struct net_device *dev)
1164{
1165 int err;
1166
1167 err = vrf_fib_rule(dev, AF_INET, true);
1168 if (err < 0)
1169 goto out_err;
1170
1171 err = vrf_fib_rule(dev, AF_INET6, true);
1172 if (err < 0)
1173 goto ipv6_err;
1174
1175 return 0;
1176
1177ipv6_err:
1178 vrf_fib_rule(dev, AF_INET, false);
1179
1180out_err:
1181 netdev_err(dev, "Failed to add FIB rules.\n");
1182 return err;
1183}
1184
David Ahern193125d2015-08-13 14:59:10 -06001185static void vrf_setup(struct net_device *dev)
1186{
1187 ether_setup(dev);
1188
1189 /* Initialize the device structure. */
1190 dev->netdev_ops = &vrf_netdev_ops;
David Ahernee15ee52015-09-29 20:07:12 -07001191 dev->l3mdev_ops = &vrf_l3mdev_ops;
David Ahern193125d2015-08-13 14:59:10 -06001192 dev->ethtool_ops = &vrf_ethtool_ops;
1193 dev->destructor = free_netdev;
1194
1195 /* Fill in device structure with ethernet-generic values. */
1196 eth_hw_addr_random(dev);
1197
1198 /* don't acquire vrf device's netif_tx_lock when transmitting */
1199 dev->features |= NETIF_F_LLTX;
1200
1201 /* don't allow vrf devices to change network namespaces. */
1202 dev->features |= NETIF_F_NETNS_LOCAL;
David Ahern78896812016-06-13 17:14:12 -07001203
1204 /* does not make sense for a VLAN to be added to a vrf device */
1205 dev->features |= NETIF_F_VLAN_CHALLENGED;
1206
1207 /* enable offload features */
1208 dev->features |= NETIF_F_GSO_SOFTWARE;
1209 dev->features |= NETIF_F_RXCSUM | NETIF_F_HW_CSUM;
1210 dev->features |= NETIF_F_SG | NETIF_F_FRAGLIST | NETIF_F_HIGHDMA;
1211
1212 dev->hw_features = dev->features;
1213 dev->hw_enc_features = dev->features;
1214
1215 /* default to no qdisc; user can add if desired */
1216 dev->priv_flags |= IFF_NO_QUEUE;
David Ahern193125d2015-08-13 14:59:10 -06001217}
1218
1219static int vrf_validate(struct nlattr *tb[], struct nlattr *data[])
1220{
1221 if (tb[IFLA_ADDRESS]) {
1222 if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN)
1223 return -EINVAL;
1224 if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS])))
1225 return -EADDRNOTAVAIL;
1226 }
1227 return 0;
1228}
1229
1230static void vrf_dellink(struct net_device *dev, struct list_head *head)
1231{
Nikolay Aleksandrove6577f12017-07-06 15:24:40 +03001232 struct net_device *port_dev;
1233 struct list_head *iter;
1234
1235 netdev_for_each_lower_dev(dev, port_dev, iter)
1236 vrf_del_slave(dev, port_dev);
1237
David Ahern193125d2015-08-13 14:59:10 -06001238 unregister_netdevice_queue(dev, head);
1239}
1240
1241static int vrf_newlink(struct net *src_net, struct net_device *dev,
1242 struct nlattr *tb[], struct nlattr *data[])
1243{
1244 struct net_vrf *vrf = netdev_priv(dev);
David Ahern386ed382017-06-08 11:31:11 -06001245 bool *add_fib_rules;
1246 struct net *net;
David Ahern1aa6c4f2016-06-08 10:55:40 -07001247 int err;
David Ahern193125d2015-08-13 14:59:10 -06001248
1249 if (!data || !data[IFLA_VRF_TABLE])
1250 return -EINVAL;
1251
1252 vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]);
David Aherne425ed12017-01-10 15:22:25 -08001253 if (vrf->tb_id == RT_TABLE_UNSPEC)
1254 return -EINVAL;
David Ahern193125d2015-08-13 14:59:10 -06001255
David Ahern007979e2015-09-29 20:07:10 -07001256 dev->priv_flags |= IFF_L3MDEV_MASTER;
David Ahern193125d2015-08-13 14:59:10 -06001257
David Ahern1aa6c4f2016-06-08 10:55:40 -07001258 err = register_netdevice(dev);
1259 if (err)
1260 goto out;
1261
David Ahern386ed382017-06-08 11:31:11 -06001262 net = dev_net(dev);
1263 add_fib_rules = net_generic(net, vrf_net_id);
1264 if (*add_fib_rules) {
David Ahern1aa6c4f2016-06-08 10:55:40 -07001265 err = vrf_add_fib_rules(dev);
1266 if (err) {
1267 unregister_netdevice(dev);
1268 goto out;
1269 }
David Ahern386ed382017-06-08 11:31:11 -06001270 *add_fib_rules = false;
David Ahern1aa6c4f2016-06-08 10:55:40 -07001271 }
1272
1273out:
1274 return err;
David Ahern193125d2015-08-13 14:59:10 -06001275}
1276
1277static size_t vrf_nl_getsize(const struct net_device *dev)
1278{
1279 return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */
1280}
1281
1282static int vrf_fillinfo(struct sk_buff *skb,
1283 const struct net_device *dev)
1284{
1285 struct net_vrf *vrf = netdev_priv(dev);
1286
1287 return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id);
1288}
1289
David Ahern67eb0332016-02-02 07:43:45 -08001290static size_t vrf_get_slave_size(const struct net_device *bond_dev,
1291 const struct net_device *slave_dev)
1292{
1293 return nla_total_size(sizeof(u32)); /* IFLA_VRF_PORT_TABLE */
1294}
1295
1296static int vrf_fill_slave_info(struct sk_buff *skb,
1297 const struct net_device *vrf_dev,
1298 const struct net_device *slave_dev)
1299{
1300 struct net_vrf *vrf = netdev_priv(vrf_dev);
1301
1302 if (nla_put_u32(skb, IFLA_VRF_PORT_TABLE, vrf->tb_id))
1303 return -EMSGSIZE;
1304
1305 return 0;
1306}
1307
David Ahern193125d2015-08-13 14:59:10 -06001308static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = {
1309 [IFLA_VRF_TABLE] = { .type = NLA_U32 },
1310};
1311
1312static struct rtnl_link_ops vrf_link_ops __read_mostly = {
1313 .kind = DRV_NAME,
1314 .priv_size = sizeof(struct net_vrf),
1315
1316 .get_size = vrf_nl_getsize,
1317 .policy = vrf_nl_policy,
1318 .validate = vrf_validate,
1319 .fill_info = vrf_fillinfo,
1320
David Ahern67eb0332016-02-02 07:43:45 -08001321 .get_slave_size = vrf_get_slave_size,
1322 .fill_slave_info = vrf_fill_slave_info,
1323
David Ahern193125d2015-08-13 14:59:10 -06001324 .newlink = vrf_newlink,
1325 .dellink = vrf_dellink,
1326 .setup = vrf_setup,
1327 .maxtype = IFLA_VRF_MAX,
1328};
1329
1330static int vrf_device_event(struct notifier_block *unused,
1331 unsigned long event, void *ptr)
1332{
1333 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1334
1335 /* only care about unregister events to drop slave references */
1336 if (event == NETDEV_UNREGISTER) {
David Ahern193125d2015-08-13 14:59:10 -06001337 struct net_device *vrf_dev;
1338
David Ahernfee6d4c2015-10-05 08:51:24 -07001339 if (!netif_is_l3_slave(dev))
David Ahern193125d2015-08-13 14:59:10 -06001340 goto out;
1341
Nikolay Aleksandrov58aa9082015-08-18 20:28:04 +03001342 vrf_dev = netdev_master_upper_dev_get(dev);
1343 vrf_del_slave(vrf_dev, dev);
David Ahern193125d2015-08-13 14:59:10 -06001344 }
1345out:
1346 return NOTIFY_DONE;
1347}
1348
1349static struct notifier_block vrf_notifier_block __read_mostly = {
1350 .notifier_call = vrf_device_event,
1351};
1352
David Ahern386ed382017-06-08 11:31:11 -06001353/* Initialize per network namespace state */
1354static int __net_init vrf_netns_init(struct net *net)
1355{
1356 bool *add_fib_rules = net_generic(net, vrf_net_id);
1357
1358 *add_fib_rules = true;
1359
1360 return 0;
1361}
1362
1363static struct pernet_operations vrf_net_ops __net_initdata = {
1364 .init = vrf_netns_init,
1365 .id = &vrf_net_id,
1366 .size = sizeof(bool),
1367};
1368
David Ahern193125d2015-08-13 14:59:10 -06001369static int __init vrf_init_module(void)
1370{
1371 int rc;
1372
David Ahern193125d2015-08-13 14:59:10 -06001373 register_netdevice_notifier(&vrf_notifier_block);
1374
David Ahern386ed382017-06-08 11:31:11 -06001375 rc = register_pernet_subsys(&vrf_net_ops);
David Ahern193125d2015-08-13 14:59:10 -06001376 if (rc < 0)
1377 goto error;
1378
David Ahern386ed382017-06-08 11:31:11 -06001379 rc = rtnl_link_register(&vrf_link_ops);
1380 if (rc < 0) {
1381 unregister_pernet_subsys(&vrf_net_ops);
1382 goto error;
1383 }
1384
David Ahern193125d2015-08-13 14:59:10 -06001385 return 0;
1386
1387error:
1388 unregister_netdevice_notifier(&vrf_notifier_block);
David Ahern193125d2015-08-13 14:59:10 -06001389 return rc;
1390}
1391
David Ahern193125d2015-08-13 14:59:10 -06001392module_init(vrf_init_module);
David Ahern193125d2015-08-13 14:59:10 -06001393MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern");
1394MODULE_DESCRIPTION("Device driver to instantiate VRF domains");
1395MODULE_LICENSE("GPL");
1396MODULE_ALIAS_RTNL_LINK(DRV_NAME);
1397MODULE_VERSION(DRV_VERSION);