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