]>
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> | |
33 | #include <net/ip6_route.h> | |
34 | #include <net/rtnetlink.h> | |
35 | #include <net/route.h> | |
36 | #include <net/addrconf.h> | |
37 | #include <net/vrf.h> | |
38 | ||
39 | #define DRV_NAME "vrf" | |
40 | #define DRV_VERSION "1.0" | |
41 | ||
42 | #define vrf_is_slave(dev) ((dev)->flags & IFF_SLAVE) | |
43 | ||
44 | #define vrf_master_get_rcu(dev) \ | |
45 | ((struct net_device *)rcu_dereference(dev->rx_handler_data)) | |
46 | ||
47 | struct pcpu_dstats { | |
48 | u64 tx_pkts; | |
49 | u64 tx_bytes; | |
50 | u64 tx_drps; | |
51 | u64 rx_pkts; | |
52 | u64 rx_bytes; | |
53 | struct u64_stats_sync syncp; | |
54 | }; | |
55 | ||
56 | static struct dst_entry *vrf_ip_check(struct dst_entry *dst, u32 cookie) | |
57 | { | |
58 | return dst; | |
59 | } | |
60 | ||
61 | static int vrf_ip_local_out(struct sk_buff *skb) | |
62 | { | |
63 | return ip_local_out(skb); | |
64 | } | |
65 | ||
66 | static unsigned int vrf_v4_mtu(const struct dst_entry *dst) | |
67 | { | |
68 | /* TO-DO: return max ethernet size? */ | |
69 | return dst->dev->mtu; | |
70 | } | |
71 | ||
72 | static void vrf_dst_destroy(struct dst_entry *dst) | |
73 | { | |
74 | /* our dst lives forever - or until the device is closed */ | |
75 | } | |
76 | ||
77 | static unsigned int vrf_default_advmss(const struct dst_entry *dst) | |
78 | { | |
79 | return 65535 - 40; | |
80 | } | |
81 | ||
82 | static struct dst_ops vrf_dst_ops = { | |
83 | .family = AF_INET, | |
84 | .local_out = vrf_ip_local_out, | |
85 | .check = vrf_ip_check, | |
86 | .mtu = vrf_v4_mtu, | |
87 | .destroy = vrf_dst_destroy, | |
88 | .default_advmss = vrf_default_advmss, | |
89 | }; | |
90 | ||
91 | static bool is_ip_rx_frame(struct sk_buff *skb) | |
92 | { | |
93 | switch (skb->protocol) { | |
94 | case htons(ETH_P_IP): | |
95 | case htons(ETH_P_IPV6): | |
96 | return true; | |
97 | } | |
98 | return false; | |
99 | } | |
100 | ||
57b8efa1 NA |
101 | static void vrf_tx_error(struct net_device *vrf_dev, struct sk_buff *skb) |
102 | { | |
103 | vrf_dev->stats.tx_errors++; | |
104 | kfree_skb(skb); | |
105 | } | |
106 | ||
193125db DA |
107 | /* note: already called with rcu_read_lock */ |
108 | static rx_handler_result_t vrf_handle_frame(struct sk_buff **pskb) | |
109 | { | |
110 | struct sk_buff *skb = *pskb; | |
111 | ||
112 | if (is_ip_rx_frame(skb)) { | |
113 | struct net_device *dev = vrf_master_get_rcu(skb->dev); | |
114 | struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats); | |
115 | ||
116 | u64_stats_update_begin(&dstats->syncp); | |
117 | dstats->rx_pkts++; | |
118 | dstats->rx_bytes += skb->len; | |
119 | u64_stats_update_end(&dstats->syncp); | |
120 | ||
121 | skb->dev = dev; | |
122 | ||
123 | return RX_HANDLER_ANOTHER; | |
124 | } | |
125 | return RX_HANDLER_PASS; | |
126 | } | |
127 | ||
128 | static struct rtnl_link_stats64 *vrf_get_stats64(struct net_device *dev, | |
129 | struct rtnl_link_stats64 *stats) | |
130 | { | |
131 | int i; | |
132 | ||
133 | for_each_possible_cpu(i) { | |
134 | const struct pcpu_dstats *dstats; | |
135 | u64 tbytes, tpkts, tdrops, rbytes, rpkts; | |
136 | unsigned int start; | |
137 | ||
138 | dstats = per_cpu_ptr(dev->dstats, i); | |
139 | do { | |
140 | start = u64_stats_fetch_begin_irq(&dstats->syncp); | |
141 | tbytes = dstats->tx_bytes; | |
142 | tpkts = dstats->tx_pkts; | |
143 | tdrops = dstats->tx_drps; | |
144 | rbytes = dstats->rx_bytes; | |
145 | rpkts = dstats->rx_pkts; | |
146 | } while (u64_stats_fetch_retry_irq(&dstats->syncp, start)); | |
147 | stats->tx_bytes += tbytes; | |
148 | stats->tx_packets += tpkts; | |
149 | stats->tx_dropped += tdrops; | |
150 | stats->rx_bytes += rbytes; | |
151 | stats->rx_packets += rpkts; | |
152 | } | |
153 | return stats; | |
154 | } | |
155 | ||
156 | static netdev_tx_t vrf_process_v6_outbound(struct sk_buff *skb, | |
157 | struct net_device *dev) | |
158 | { | |
57b8efa1 NA |
159 | vrf_tx_error(dev, skb); |
160 | return NET_XMIT_DROP; | |
193125db DA |
161 | } |
162 | ||
163 | static int vrf_send_v4_prep(struct sk_buff *skb, struct flowi4 *fl4, | |
164 | struct net_device *vrf_dev) | |
165 | { | |
166 | struct rtable *rt; | |
167 | int err = 1; | |
168 | ||
169 | rt = ip_route_output_flow(dev_net(vrf_dev), fl4, NULL); | |
170 | if (IS_ERR(rt)) | |
171 | goto out; | |
172 | ||
173 | /* TO-DO: what about broadcast ? */ | |
174 | if (rt->rt_type != RTN_UNICAST && rt->rt_type != RTN_LOCAL) { | |
175 | ip_rt_put(rt); | |
176 | goto out; | |
177 | } | |
178 | ||
179 | skb_dst_drop(skb); | |
180 | skb_dst_set(skb, &rt->dst); | |
181 | err = 0; | |
182 | out: | |
183 | return err; | |
184 | } | |
185 | ||
186 | static netdev_tx_t vrf_process_v4_outbound(struct sk_buff *skb, | |
187 | struct net_device *vrf_dev) | |
188 | { | |
189 | struct iphdr *ip4h = ip_hdr(skb); | |
190 | int ret = NET_XMIT_DROP; | |
191 | struct flowi4 fl4 = { | |
192 | /* needed to match OIF rule */ | |
193 | .flowi4_oif = vrf_dev->ifindex, | |
194 | .flowi4_iif = LOOPBACK_IFINDEX, | |
195 | .flowi4_tos = RT_TOS(ip4h->tos), | |
58189ca7 DA |
196 | .flowi4_flags = FLOWI_FLAG_ANYSRC | FLOWI_FLAG_VRFSRC | |
197 | FLOWI_FLAG_SKIP_NH_OIF, | |
193125db DA |
198 | .daddr = ip4h->daddr, |
199 | }; | |
200 | ||
201 | if (vrf_send_v4_prep(skb, &fl4, vrf_dev)) | |
202 | goto err; | |
203 | ||
204 | if (!ip4h->saddr) { | |
205 | ip4h->saddr = inet_select_addr(skb_dst(skb)->dev, 0, | |
206 | RT_SCOPE_LINK); | |
207 | } | |
208 | ||
209 | ret = ip_local_out(skb); | |
210 | if (unlikely(net_xmit_eval(ret))) | |
211 | vrf_dev->stats.tx_errors++; | |
212 | else | |
213 | ret = NET_XMIT_SUCCESS; | |
214 | ||
215 | out: | |
216 | return ret; | |
217 | err: | |
57b8efa1 | 218 | vrf_tx_error(vrf_dev, skb); |
193125db DA |
219 | goto out; |
220 | } | |
221 | ||
222 | static netdev_tx_t is_ip_tx_frame(struct sk_buff *skb, struct net_device *dev) | |
223 | { | |
8f58336d DA |
224 | /* strip the ethernet header added for pass through VRF device */ |
225 | __skb_pull(skb, skb_network_offset(skb)); | |
226 | ||
193125db DA |
227 | switch (skb->protocol) { |
228 | case htons(ETH_P_IP): | |
229 | return vrf_process_v4_outbound(skb, dev); | |
230 | case htons(ETH_P_IPV6): | |
231 | return vrf_process_v6_outbound(skb, dev); | |
232 | default: | |
57b8efa1 | 233 | vrf_tx_error(dev, skb); |
193125db DA |
234 | return NET_XMIT_DROP; |
235 | } | |
236 | } | |
237 | ||
238 | static netdev_tx_t vrf_xmit(struct sk_buff *skb, struct net_device *dev) | |
239 | { | |
240 | netdev_tx_t ret = is_ip_tx_frame(skb, dev); | |
241 | ||
242 | if (likely(ret == NET_XMIT_SUCCESS || ret == NET_XMIT_CN)) { | |
243 | struct pcpu_dstats *dstats = this_cpu_ptr(dev->dstats); | |
244 | ||
245 | u64_stats_update_begin(&dstats->syncp); | |
246 | dstats->tx_pkts++; | |
247 | dstats->tx_bytes += skb->len; | |
248 | u64_stats_update_end(&dstats->syncp); | |
249 | } else { | |
250 | this_cpu_inc(dev->dstats->tx_drps); | |
251 | } | |
252 | ||
253 | return ret; | |
254 | } | |
255 | ||
8f58336d DA |
256 | /* modelled after ip_finish_output2 */ |
257 | static int vrf_finish_output(struct sock *sk, struct sk_buff *skb) | |
193125db | 258 | { |
8f58336d DA |
259 | struct dst_entry *dst = skb_dst(skb); |
260 | struct rtable *rt = (struct rtable *)dst; | |
261 | struct net_device *dev = dst->dev; | |
262 | unsigned int hh_len = LL_RESERVED_SPACE(dev); | |
263 | struct neighbour *neigh; | |
264 | u32 nexthop; | |
265 | int ret = -EINVAL; | |
266 | ||
267 | /* Be paranoid, rather than too clever. */ | |
268 | if (unlikely(skb_headroom(skb) < hh_len && dev->header_ops)) { | |
269 | struct sk_buff *skb2; | |
270 | ||
271 | skb2 = skb_realloc_headroom(skb, LL_RESERVED_SPACE(dev)); | |
272 | if (!skb2) { | |
273 | ret = -ENOMEM; | |
274 | goto err; | |
275 | } | |
276 | if (skb->sk) | |
277 | skb_set_owner_w(skb2, skb->sk); | |
278 | ||
279 | consume_skb(skb); | |
280 | skb = skb2; | |
281 | } | |
282 | ||
283 | rcu_read_lock_bh(); | |
284 | ||
285 | nexthop = (__force u32)rt_nexthop(rt, ip_hdr(skb)->daddr); | |
286 | neigh = __ipv4_neigh_lookup_noref(dev, nexthop); | |
287 | if (unlikely(!neigh)) | |
288 | neigh = __neigh_create(&arp_tbl, &nexthop, dev, false); | |
289 | if (!IS_ERR(neigh)) | |
290 | ret = dst_neigh_output(dst, neigh, skb); | |
291 | ||
292 | rcu_read_unlock_bh(); | |
293 | err: | |
294 | if (unlikely(ret < 0)) | |
295 | vrf_tx_error(skb->dev, skb); | |
296 | return ret; | |
193125db DA |
297 | } |
298 | ||
299 | static int vrf_output(struct sock *sk, struct sk_buff *skb) | |
300 | { | |
301 | struct net_device *dev = skb_dst(skb)->dev; | |
302 | ||
303 | IP_UPD_PO_STATS(dev_net(dev), IPSTATS_MIB_OUT, skb->len); | |
304 | ||
305 | skb->dev = dev; | |
306 | skb->protocol = htons(ETH_P_IP); | |
307 | ||
308 | return NF_HOOK_COND(NFPROTO_IPV4, NF_INET_POST_ROUTING, sk, skb, | |
309 | NULL, dev, | |
8f58336d | 310 | vrf_finish_output, |
193125db DA |
311 | !(IPCB(skb)->flags & IPSKB_REROUTED)); |
312 | } | |
313 | ||
314 | static void vrf_rtable_destroy(struct net_vrf *vrf) | |
315 | { | |
316 | struct dst_entry *dst = (struct dst_entry *)vrf->rth; | |
317 | ||
3a4a27d3 | 318 | dst_destroy(dst); |
193125db DA |
319 | vrf->rth = NULL; |
320 | } | |
321 | ||
322 | static struct rtable *vrf_rtable_create(struct net_device *dev) | |
323 | { | |
324 | struct rtable *rth; | |
325 | ||
326 | rth = dst_alloc(&vrf_dst_ops, dev, 2, | |
327 | DST_OBSOLETE_NONE, | |
328 | (DST_HOST | DST_NOPOLICY | DST_NOXFRM)); | |
329 | if (rth) { | |
330 | rth->dst.output = vrf_output; | |
331 | rth->rt_genid = rt_genid_ipv4(dev_net(dev)); | |
332 | rth->rt_flags = 0; | |
333 | rth->rt_type = RTN_UNICAST; | |
334 | rth->rt_is_input = 0; | |
335 | rth->rt_iif = 0; | |
336 | rth->rt_pmtu = 0; | |
337 | rth->rt_gateway = 0; | |
338 | rth->rt_uses_gateway = 0; | |
339 | INIT_LIST_HEAD(&rth->rt_uncached); | |
340 | rth->rt_uncached_list = NULL; | |
193125db DA |
341 | } |
342 | ||
343 | return rth; | |
344 | } | |
345 | ||
346 | /**************************** device handling ********************/ | |
347 | ||
348 | /* cycle interface to flush neighbor cache and move routes across tables */ | |
349 | static void cycle_netdev(struct net_device *dev) | |
350 | { | |
351 | unsigned int flags = dev->flags; | |
352 | int ret; | |
353 | ||
354 | if (!netif_running(dev)) | |
355 | return; | |
356 | ||
357 | ret = dev_change_flags(dev, flags & ~IFF_UP); | |
358 | if (ret >= 0) | |
359 | ret = dev_change_flags(dev, flags); | |
360 | ||
361 | if (ret < 0) { | |
362 | netdev_err(dev, | |
363 | "Failed to cycle device %s; route tables might be wrong!\n", | |
364 | dev->name); | |
365 | } | |
366 | } | |
367 | ||
368 | static struct slave *__vrf_find_slave_dev(struct slave_queue *queue, | |
369 | struct net_device *dev) | |
370 | { | |
371 | struct list_head *head = &queue->all_slaves; | |
372 | struct slave *slave; | |
373 | ||
374 | list_for_each_entry(slave, head, list) { | |
375 | if (slave->dev == dev) | |
376 | return slave; | |
377 | } | |
378 | ||
379 | return NULL; | |
380 | } | |
381 | ||
382 | /* inverse of __vrf_insert_slave */ | |
383 | static void __vrf_remove_slave(struct slave_queue *queue, struct slave *slave) | |
384 | { | |
193125db | 385 | list_del(&slave->list); |
193125db DA |
386 | } |
387 | ||
388 | static void __vrf_insert_slave(struct slave_queue *queue, struct slave *slave) | |
389 | { | |
193125db | 390 | list_add(&slave->list, &queue->all_slaves); |
193125db DA |
391 | } |
392 | ||
393 | static int do_vrf_add_slave(struct net_device *dev, struct net_device *port_dev) | |
394 | { | |
395 | struct net_vrf_dev *vrf_ptr = kmalloc(sizeof(*vrf_ptr), GFP_KERNEL); | |
396 | struct slave *slave = kzalloc(sizeof(*slave), GFP_KERNEL); | |
193125db DA |
397 | struct net_vrf *vrf = netdev_priv(dev); |
398 | struct slave_queue *queue = &vrf->queue; | |
399 | int ret = -ENOMEM; | |
400 | ||
401 | if (!slave || !vrf_ptr) | |
402 | goto out_fail; | |
403 | ||
404 | slave->dev = port_dev; | |
193125db DA |
405 | vrf_ptr->ifindex = dev->ifindex; |
406 | vrf_ptr->tb_id = vrf->tb_id; | |
407 | ||
193125db DA |
408 | /* register the packet handler for slave ports */ |
409 | ret = netdev_rx_handler_register(port_dev, vrf_handle_frame, dev); | |
410 | if (ret) { | |
411 | netdev_err(port_dev, | |
412 | "Device %s failed to register rx_handler\n", | |
413 | port_dev->name); | |
15df5e71 | 414 | goto out_fail; |
193125db DA |
415 | } |
416 | ||
417 | ret = netdev_master_upper_dev_link(port_dev, dev); | |
418 | if (ret < 0) | |
419 | goto out_unregister; | |
420 | ||
421 | port_dev->flags |= IFF_SLAVE; | |
15df5e71 | 422 | __vrf_insert_slave(queue, slave); |
193125db DA |
423 | rcu_assign_pointer(port_dev->vrf_ptr, vrf_ptr); |
424 | cycle_netdev(port_dev); | |
425 | ||
426 | return 0; | |
427 | ||
428 | out_unregister: | |
429 | netdev_rx_handler_unregister(port_dev); | |
193125db DA |
430 | out_fail: |
431 | kfree(vrf_ptr); | |
432 | kfree(slave); | |
433 | return ret; | |
434 | } | |
435 | ||
436 | static int vrf_add_slave(struct net_device *dev, struct net_device *port_dev) | |
437 | { | |
2640b39c | 438 | if (netif_is_vrf(port_dev) || vrf_is_slave(port_dev)) |
193125db DA |
439 | return -EINVAL; |
440 | ||
441 | return do_vrf_add_slave(dev, port_dev); | |
442 | } | |
443 | ||
444 | /* inverse of do_vrf_add_slave */ | |
445 | static int do_vrf_del_slave(struct net_device *dev, struct net_device *port_dev) | |
446 | { | |
447 | struct net_vrf_dev *vrf_ptr = rtnl_dereference(port_dev->vrf_ptr); | |
448 | struct net_vrf *vrf = netdev_priv(dev); | |
449 | struct slave_queue *queue = &vrf->queue; | |
450 | struct slave *slave; | |
451 | ||
452 | RCU_INIT_POINTER(port_dev->vrf_ptr, NULL); | |
453 | ||
454 | netdev_upper_dev_unlink(port_dev, dev); | |
455 | port_dev->flags &= ~IFF_SLAVE; | |
456 | ||
457 | netdev_rx_handler_unregister(port_dev); | |
458 | ||
459 | /* after netdev_rx_handler_unregister for synchronize_rcu */ | |
460 | kfree(vrf_ptr); | |
461 | ||
462 | cycle_netdev(port_dev); | |
463 | ||
464 | slave = __vrf_find_slave_dev(queue, port_dev); | |
465 | if (slave) | |
466 | __vrf_remove_slave(queue, slave); | |
467 | ||
468 | kfree(slave); | |
469 | ||
470 | return 0; | |
471 | } | |
472 | ||
473 | static int vrf_del_slave(struct net_device *dev, struct net_device *port_dev) | |
474 | { | |
193125db DA |
475 | return do_vrf_del_slave(dev, port_dev); |
476 | } | |
477 | ||
478 | static void vrf_dev_uninit(struct net_device *dev) | |
479 | { | |
480 | struct net_vrf *vrf = netdev_priv(dev); | |
481 | struct slave_queue *queue = &vrf->queue; | |
482 | struct list_head *head = &queue->all_slaves; | |
483 | struct slave *slave, *next; | |
484 | ||
485 | vrf_rtable_destroy(vrf); | |
486 | ||
487 | list_for_each_entry_safe(slave, next, head, list) | |
488 | vrf_del_slave(dev, slave->dev); | |
489 | ||
3a4a27d3 | 490 | free_percpu(dev->dstats); |
193125db DA |
491 | dev->dstats = NULL; |
492 | } | |
493 | ||
494 | static int vrf_dev_init(struct net_device *dev) | |
495 | { | |
496 | struct net_vrf *vrf = netdev_priv(dev); | |
497 | ||
498 | INIT_LIST_HEAD(&vrf->queue.all_slaves); | |
499 | ||
500 | dev->dstats = netdev_alloc_pcpu_stats(struct pcpu_dstats); | |
501 | if (!dev->dstats) | |
502 | goto out_nomem; | |
503 | ||
504 | /* create the default dst which points back to us */ | |
505 | vrf->rth = vrf_rtable_create(dev); | |
506 | if (!vrf->rth) | |
507 | goto out_stats; | |
508 | ||
509 | dev->flags = IFF_MASTER | IFF_NOARP; | |
510 | ||
511 | return 0; | |
512 | ||
513 | out_stats: | |
514 | free_percpu(dev->dstats); | |
515 | dev->dstats = NULL; | |
516 | out_nomem: | |
517 | return -ENOMEM; | |
518 | } | |
519 | ||
520 | static const struct net_device_ops vrf_netdev_ops = { | |
521 | .ndo_init = vrf_dev_init, | |
522 | .ndo_uninit = vrf_dev_uninit, | |
523 | .ndo_start_xmit = vrf_xmit, | |
524 | .ndo_get_stats64 = vrf_get_stats64, | |
525 | .ndo_add_slave = vrf_add_slave, | |
526 | .ndo_del_slave = vrf_del_slave, | |
527 | }; | |
528 | ||
529 | static void vrf_get_drvinfo(struct net_device *dev, | |
530 | struct ethtool_drvinfo *info) | |
531 | { | |
532 | strlcpy(info->driver, DRV_NAME, sizeof(info->driver)); | |
533 | strlcpy(info->version, DRV_VERSION, sizeof(info->version)); | |
534 | } | |
535 | ||
536 | static const struct ethtool_ops vrf_ethtool_ops = { | |
537 | .get_drvinfo = vrf_get_drvinfo, | |
538 | }; | |
539 | ||
540 | static void vrf_setup(struct net_device *dev) | |
541 | { | |
542 | ether_setup(dev); | |
543 | ||
544 | /* Initialize the device structure. */ | |
545 | dev->netdev_ops = &vrf_netdev_ops; | |
546 | dev->ethtool_ops = &vrf_ethtool_ops; | |
547 | dev->destructor = free_netdev; | |
548 | ||
549 | /* Fill in device structure with ethernet-generic values. */ | |
550 | eth_hw_addr_random(dev); | |
551 | ||
552 | /* don't acquire vrf device's netif_tx_lock when transmitting */ | |
553 | dev->features |= NETIF_F_LLTX; | |
554 | ||
555 | /* don't allow vrf devices to change network namespaces. */ | |
556 | dev->features |= NETIF_F_NETNS_LOCAL; | |
557 | } | |
558 | ||
559 | static int vrf_validate(struct nlattr *tb[], struct nlattr *data[]) | |
560 | { | |
561 | if (tb[IFLA_ADDRESS]) { | |
562 | if (nla_len(tb[IFLA_ADDRESS]) != ETH_ALEN) | |
563 | return -EINVAL; | |
564 | if (!is_valid_ether_addr(nla_data(tb[IFLA_ADDRESS]))) | |
565 | return -EADDRNOTAVAIL; | |
566 | } | |
567 | return 0; | |
568 | } | |
569 | ||
570 | static void vrf_dellink(struct net_device *dev, struct list_head *head) | |
571 | { | |
572 | struct net_vrf_dev *vrf_ptr = rtnl_dereference(dev->vrf_ptr); | |
573 | ||
574 | RCU_INIT_POINTER(dev->vrf_ptr, NULL); | |
575 | kfree_rcu(vrf_ptr, rcu); | |
576 | unregister_netdevice_queue(dev, head); | |
577 | } | |
578 | ||
579 | static int vrf_newlink(struct net *src_net, struct net_device *dev, | |
580 | struct nlattr *tb[], struct nlattr *data[]) | |
581 | { | |
582 | struct net_vrf *vrf = netdev_priv(dev); | |
583 | struct net_vrf_dev *vrf_ptr; | |
584 | int err; | |
585 | ||
586 | if (!data || !data[IFLA_VRF_TABLE]) | |
587 | return -EINVAL; | |
588 | ||
589 | vrf->tb_id = nla_get_u32(data[IFLA_VRF_TABLE]); | |
590 | ||
591 | dev->priv_flags |= IFF_VRF_MASTER; | |
592 | ||
593 | err = -ENOMEM; | |
594 | vrf_ptr = kmalloc(sizeof(*dev->vrf_ptr), GFP_KERNEL); | |
595 | if (!vrf_ptr) | |
596 | goto out_fail; | |
597 | ||
598 | vrf_ptr->ifindex = dev->ifindex; | |
599 | vrf_ptr->tb_id = vrf->tb_id; | |
600 | ||
601 | err = register_netdevice(dev); | |
602 | if (err < 0) | |
603 | goto out_fail; | |
604 | ||
605 | rcu_assign_pointer(dev->vrf_ptr, vrf_ptr); | |
606 | ||
607 | return 0; | |
608 | ||
609 | out_fail: | |
610 | kfree(vrf_ptr); | |
611 | free_netdev(dev); | |
612 | return err; | |
613 | } | |
614 | ||
615 | static size_t vrf_nl_getsize(const struct net_device *dev) | |
616 | { | |
617 | return nla_total_size(sizeof(u32)); /* IFLA_VRF_TABLE */ | |
618 | } | |
619 | ||
620 | static int vrf_fillinfo(struct sk_buff *skb, | |
621 | const struct net_device *dev) | |
622 | { | |
623 | struct net_vrf *vrf = netdev_priv(dev); | |
624 | ||
625 | return nla_put_u32(skb, IFLA_VRF_TABLE, vrf->tb_id); | |
626 | } | |
627 | ||
628 | static const struct nla_policy vrf_nl_policy[IFLA_VRF_MAX + 1] = { | |
629 | [IFLA_VRF_TABLE] = { .type = NLA_U32 }, | |
630 | }; | |
631 | ||
632 | static struct rtnl_link_ops vrf_link_ops __read_mostly = { | |
633 | .kind = DRV_NAME, | |
634 | .priv_size = sizeof(struct net_vrf), | |
635 | ||
636 | .get_size = vrf_nl_getsize, | |
637 | .policy = vrf_nl_policy, | |
638 | .validate = vrf_validate, | |
639 | .fill_info = vrf_fillinfo, | |
640 | ||
641 | .newlink = vrf_newlink, | |
642 | .dellink = vrf_dellink, | |
643 | .setup = vrf_setup, | |
644 | .maxtype = IFLA_VRF_MAX, | |
645 | }; | |
646 | ||
647 | static int vrf_device_event(struct notifier_block *unused, | |
648 | unsigned long event, void *ptr) | |
649 | { | |
650 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); | |
651 | ||
652 | /* only care about unregister events to drop slave references */ | |
653 | if (event == NETDEV_UNREGISTER) { | |
654 | struct net_vrf_dev *vrf_ptr = rtnl_dereference(dev->vrf_ptr); | |
655 | struct net_device *vrf_dev; | |
656 | ||
657 | if (!vrf_ptr || netif_is_vrf(dev)) | |
658 | goto out; | |
659 | ||
58aa9087 NA |
660 | vrf_dev = netdev_master_upper_dev_get(dev); |
661 | vrf_del_slave(vrf_dev, dev); | |
193125db DA |
662 | } |
663 | out: | |
664 | return NOTIFY_DONE; | |
665 | } | |
666 | ||
667 | static struct notifier_block vrf_notifier_block __read_mostly = { | |
668 | .notifier_call = vrf_device_event, | |
669 | }; | |
670 | ||
671 | static int __init vrf_init_module(void) | |
672 | { | |
673 | int rc; | |
674 | ||
675 | vrf_dst_ops.kmem_cachep = | |
676 | kmem_cache_create("vrf_ip_dst_cache", | |
677 | sizeof(struct rtable), 0, | |
e367da02 | 678 | SLAB_HWCACHE_ALIGN, |
193125db DA |
679 | NULL); |
680 | ||
681 | if (!vrf_dst_ops.kmem_cachep) | |
682 | return -ENOMEM; | |
683 | ||
684 | register_netdevice_notifier(&vrf_notifier_block); | |
685 | ||
686 | rc = rtnl_link_register(&vrf_link_ops); | |
687 | if (rc < 0) | |
688 | goto error; | |
689 | ||
690 | return 0; | |
691 | ||
692 | error: | |
693 | unregister_netdevice_notifier(&vrf_notifier_block); | |
694 | kmem_cache_destroy(vrf_dst_ops.kmem_cachep); | |
695 | return rc; | |
696 | } | |
697 | ||
698 | static void __exit vrf_cleanup_module(void) | |
699 | { | |
700 | rtnl_link_unregister(&vrf_link_ops); | |
701 | unregister_netdevice_notifier(&vrf_notifier_block); | |
702 | kmem_cache_destroy(vrf_dst_ops.kmem_cachep); | |
703 | } | |
704 | ||
705 | module_init(vrf_init_module); | |
706 | module_exit(vrf_cleanup_module); | |
707 | MODULE_AUTHOR("Shrijeet Mukherjee, David Ahern"); | |
708 | MODULE_DESCRIPTION("Device driver to instantiate VRF domains"); | |
709 | MODULE_LICENSE("GPL"); | |
710 | MODULE_ALIAS_RTNL_LINK(DRV_NAME); | |
711 | MODULE_VERSION(DRV_VERSION); |