]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/ipv6/ip6_fib.c
inet: frags: call inet_frags_fini() after unregister_pernet_subsys()
[mirror_ubuntu-bionic-kernel.git] / net / ipv6 / ip6_fib.c
1 /*
2 * Linux INET6 implementation
3 * Forwarding Information Database
4 *
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 * Changes:
14 * Yuji SEKIYA @USAGI: Support default route on router node;
15 * remove ip6_null_entry from the top of
16 * routing table.
17 * Ville Nuorvala: Fixed routing subtrees.
18 */
19
20 #define pr_fmt(fmt) "IPv6: " fmt
21
22 #include <linux/errno.h>
23 #include <linux/types.h>
24 #include <linux/net.h>
25 #include <linux/route.h>
26 #include <linux/netdevice.h>
27 #include <linux/in6.h>
28 #include <linux/init.h>
29 #include <linux/list.h>
30 #include <linux/slab.h>
31
32 #include <net/ipv6.h>
33 #include <net/ndisc.h>
34 #include <net/addrconf.h>
35 #include <net/lwtunnel.h>
36 #include <net/fib_notifier.h>
37
38 #include <net/ip6_fib.h>
39 #include <net/ip6_route.h>
40
41 static struct kmem_cache *fib6_node_kmem __read_mostly;
42
43 struct fib6_cleaner {
44 struct fib6_walker w;
45 struct net *net;
46 int (*func)(struct rt6_info *, void *arg);
47 int sernum;
48 void *arg;
49 };
50
51 #ifdef CONFIG_IPV6_SUBTREES
52 #define FWS_INIT FWS_S
53 #else
54 #define FWS_INIT FWS_L
55 #endif
56
57 static struct rt6_info *fib6_find_prefix(struct net *net,
58 struct fib6_table *table,
59 struct fib6_node *fn);
60 static struct fib6_node *fib6_repair_tree(struct net *net,
61 struct fib6_table *table,
62 struct fib6_node *fn);
63 static int fib6_walk(struct net *net, struct fib6_walker *w);
64 static int fib6_walk_continue(struct fib6_walker *w);
65
66 /*
67 * A routing update causes an increase of the serial number on the
68 * affected subtree. This allows for cached routes to be asynchronously
69 * tested when modifications are made to the destination cache as a
70 * result of redirects, path MTU changes, etc.
71 */
72
73 static void fib6_gc_timer_cb(struct timer_list *t);
74
75 #define FOR_WALKERS(net, w) \
76 list_for_each_entry(w, &(net)->ipv6.fib6_walkers, lh)
77
78 static void fib6_walker_link(struct net *net, struct fib6_walker *w)
79 {
80 write_lock_bh(&net->ipv6.fib6_walker_lock);
81 list_add(&w->lh, &net->ipv6.fib6_walkers);
82 write_unlock_bh(&net->ipv6.fib6_walker_lock);
83 }
84
85 static void fib6_walker_unlink(struct net *net, struct fib6_walker *w)
86 {
87 write_lock_bh(&net->ipv6.fib6_walker_lock);
88 list_del(&w->lh);
89 write_unlock_bh(&net->ipv6.fib6_walker_lock);
90 }
91
92 static int fib6_new_sernum(struct net *net)
93 {
94 int new, old;
95
96 do {
97 old = atomic_read(&net->ipv6.fib6_sernum);
98 new = old < INT_MAX ? old + 1 : 1;
99 } while (atomic_cmpxchg(&net->ipv6.fib6_sernum,
100 old, new) != old);
101 return new;
102 }
103
104 enum {
105 FIB6_NO_SERNUM_CHANGE = 0,
106 };
107
108 void fib6_update_sernum(struct rt6_info *rt)
109 {
110 struct fib6_table *table = rt->rt6i_table;
111 struct net *net = dev_net(rt->dst.dev);
112 struct fib6_node *fn;
113
114 spin_lock_bh(&table->tb6_lock);
115 fn = rcu_dereference_protected(rt->rt6i_node,
116 lockdep_is_held(&table->tb6_lock));
117 if (fn)
118 fn->fn_sernum = fib6_new_sernum(net);
119 spin_unlock_bh(&table->tb6_lock);
120 }
121
122 /*
123 * Auxiliary address test functions for the radix tree.
124 *
125 * These assume a 32bit processor (although it will work on
126 * 64bit processors)
127 */
128
129 /*
130 * test bit
131 */
132 #if defined(__LITTLE_ENDIAN)
133 # define BITOP_BE32_SWIZZLE (0x1F & ~7)
134 #else
135 # define BITOP_BE32_SWIZZLE 0
136 #endif
137
138 static __be32 addr_bit_set(const void *token, int fn_bit)
139 {
140 const __be32 *addr = token;
141 /*
142 * Here,
143 * 1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)
144 * is optimized version of
145 * htonl(1 << ((~fn_bit)&0x1F))
146 * See include/asm-generic/bitops/le.h.
147 */
148 return (__force __be32)(1 << ((~fn_bit ^ BITOP_BE32_SWIZZLE) & 0x1f)) &
149 addr[fn_bit >> 5];
150 }
151
152 static struct fib6_node *node_alloc(struct net *net)
153 {
154 struct fib6_node *fn;
155
156 fn = kmem_cache_zalloc(fib6_node_kmem, GFP_ATOMIC);
157 if (fn)
158 net->ipv6.rt6_stats->fib_nodes++;
159
160 return fn;
161 }
162
163 static void node_free_immediate(struct net *net, struct fib6_node *fn)
164 {
165 kmem_cache_free(fib6_node_kmem, fn);
166 net->ipv6.rt6_stats->fib_nodes--;
167 }
168
169 static void node_free_rcu(struct rcu_head *head)
170 {
171 struct fib6_node *fn = container_of(head, struct fib6_node, rcu);
172
173 kmem_cache_free(fib6_node_kmem, fn);
174 }
175
176 static void node_free(struct net *net, struct fib6_node *fn)
177 {
178 call_rcu(&fn->rcu, node_free_rcu);
179 net->ipv6.rt6_stats->fib_nodes--;
180 }
181
182 void rt6_free_pcpu(struct rt6_info *non_pcpu_rt)
183 {
184 int cpu;
185
186 if (!non_pcpu_rt->rt6i_pcpu)
187 return;
188
189 for_each_possible_cpu(cpu) {
190 struct rt6_info **ppcpu_rt;
191 struct rt6_info *pcpu_rt;
192
193 ppcpu_rt = per_cpu_ptr(non_pcpu_rt->rt6i_pcpu, cpu);
194 pcpu_rt = *ppcpu_rt;
195 if (pcpu_rt) {
196 dst_dev_put(&pcpu_rt->dst);
197 dst_release(&pcpu_rt->dst);
198 *ppcpu_rt = NULL;
199 }
200 }
201 }
202 EXPORT_SYMBOL_GPL(rt6_free_pcpu);
203
204 static void fib6_free_table(struct fib6_table *table)
205 {
206 inetpeer_invalidate_tree(&table->tb6_peers);
207 kfree(table);
208 }
209
210 static void fib6_link_table(struct net *net, struct fib6_table *tb)
211 {
212 unsigned int h;
213
214 /*
215 * Initialize table lock at a single place to give lockdep a key,
216 * tables aren't visible prior to being linked to the list.
217 */
218 spin_lock_init(&tb->tb6_lock);
219 h = tb->tb6_id & (FIB6_TABLE_HASHSZ - 1);
220
221 /*
222 * No protection necessary, this is the only list mutatation
223 * operation, tables never disappear once they exist.
224 */
225 hlist_add_head_rcu(&tb->tb6_hlist, &net->ipv6.fib_table_hash[h]);
226 }
227
228 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
229
230 static struct fib6_table *fib6_alloc_table(struct net *net, u32 id)
231 {
232 struct fib6_table *table;
233
234 table = kzalloc(sizeof(*table), GFP_ATOMIC);
235 if (table) {
236 table->tb6_id = id;
237 rcu_assign_pointer(table->tb6_root.leaf,
238 net->ipv6.ip6_null_entry);
239 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
240 inet_peer_base_init(&table->tb6_peers);
241 }
242
243 return table;
244 }
245
246 struct fib6_table *fib6_new_table(struct net *net, u32 id)
247 {
248 struct fib6_table *tb;
249
250 if (id == 0)
251 id = RT6_TABLE_MAIN;
252 tb = fib6_get_table(net, id);
253 if (tb)
254 return tb;
255
256 tb = fib6_alloc_table(net, id);
257 if (tb)
258 fib6_link_table(net, tb);
259
260 return tb;
261 }
262 EXPORT_SYMBOL_GPL(fib6_new_table);
263
264 struct fib6_table *fib6_get_table(struct net *net, u32 id)
265 {
266 struct fib6_table *tb;
267 struct hlist_head *head;
268 unsigned int h;
269
270 if (id == 0)
271 id = RT6_TABLE_MAIN;
272 h = id & (FIB6_TABLE_HASHSZ - 1);
273 rcu_read_lock();
274 head = &net->ipv6.fib_table_hash[h];
275 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
276 if (tb->tb6_id == id) {
277 rcu_read_unlock();
278 return tb;
279 }
280 }
281 rcu_read_unlock();
282
283 return NULL;
284 }
285 EXPORT_SYMBOL_GPL(fib6_get_table);
286
287 static void __net_init fib6_tables_init(struct net *net)
288 {
289 fib6_link_table(net, net->ipv6.fib6_main_tbl);
290 fib6_link_table(net, net->ipv6.fib6_local_tbl);
291 }
292 #else
293
294 struct fib6_table *fib6_new_table(struct net *net, u32 id)
295 {
296 return fib6_get_table(net, id);
297 }
298
299 struct fib6_table *fib6_get_table(struct net *net, u32 id)
300 {
301 return net->ipv6.fib6_main_tbl;
302 }
303
304 struct dst_entry *fib6_rule_lookup(struct net *net, struct flowi6 *fl6,
305 int flags, pol_lookup_t lookup)
306 {
307 struct rt6_info *rt;
308
309 rt = lookup(net, net->ipv6.fib6_main_tbl, fl6, flags);
310 if (rt->dst.error == -EAGAIN) {
311 ip6_rt_put(rt);
312 rt = net->ipv6.ip6_null_entry;
313 dst_hold(&rt->dst);
314 }
315
316 return &rt->dst;
317 }
318
319 static void __net_init fib6_tables_init(struct net *net)
320 {
321 fib6_link_table(net, net->ipv6.fib6_main_tbl);
322 }
323
324 #endif
325
326 unsigned int fib6_tables_seq_read(struct net *net)
327 {
328 unsigned int h, fib_seq = 0;
329
330 rcu_read_lock();
331 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
332 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
333 struct fib6_table *tb;
334
335 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
336 fib_seq += tb->fib_seq;
337 }
338 rcu_read_unlock();
339
340 return fib_seq;
341 }
342
343 static int call_fib6_entry_notifier(struct notifier_block *nb, struct net *net,
344 enum fib_event_type event_type,
345 struct rt6_info *rt)
346 {
347 struct fib6_entry_notifier_info info = {
348 .rt = rt,
349 };
350
351 return call_fib6_notifier(nb, net, event_type, &info.info);
352 }
353
354 static int call_fib6_entry_notifiers(struct net *net,
355 enum fib_event_type event_type,
356 struct rt6_info *rt,
357 struct netlink_ext_ack *extack)
358 {
359 struct fib6_entry_notifier_info info = {
360 .info.extack = extack,
361 .rt = rt,
362 };
363
364 rt->rt6i_table->fib_seq++;
365 return call_fib6_notifiers(net, event_type, &info.info);
366 }
367
368 struct fib6_dump_arg {
369 struct net *net;
370 struct notifier_block *nb;
371 };
372
373 static void fib6_rt_dump(struct rt6_info *rt, struct fib6_dump_arg *arg)
374 {
375 if (rt == arg->net->ipv6.ip6_null_entry)
376 return;
377 call_fib6_entry_notifier(arg->nb, arg->net, FIB_EVENT_ENTRY_ADD, rt);
378 }
379
380 static int fib6_node_dump(struct fib6_walker *w)
381 {
382 struct rt6_info *rt;
383
384 for_each_fib6_walker_rt(w)
385 fib6_rt_dump(rt, w->args);
386 w->leaf = NULL;
387 return 0;
388 }
389
390 static void fib6_table_dump(struct net *net, struct fib6_table *tb,
391 struct fib6_walker *w)
392 {
393 w->root = &tb->tb6_root;
394 spin_lock_bh(&tb->tb6_lock);
395 fib6_walk(net, w);
396 spin_unlock_bh(&tb->tb6_lock);
397 }
398
399 /* Called with rcu_read_lock() */
400 int fib6_tables_dump(struct net *net, struct notifier_block *nb)
401 {
402 struct fib6_dump_arg arg;
403 struct fib6_walker *w;
404 unsigned int h;
405
406 w = kzalloc(sizeof(*w), GFP_ATOMIC);
407 if (!w)
408 return -ENOMEM;
409
410 w->func = fib6_node_dump;
411 arg.net = net;
412 arg.nb = nb;
413 w->args = &arg;
414
415 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
416 struct hlist_head *head = &net->ipv6.fib_table_hash[h];
417 struct fib6_table *tb;
418
419 hlist_for_each_entry_rcu(tb, head, tb6_hlist)
420 fib6_table_dump(net, tb, w);
421 }
422
423 kfree(w);
424
425 return 0;
426 }
427
428 static int fib6_dump_node(struct fib6_walker *w)
429 {
430 int res;
431 struct rt6_info *rt;
432
433 for_each_fib6_walker_rt(w) {
434 res = rt6_dump_route(rt, w->args);
435 if (res < 0) {
436 /* Frame is full, suspend walking */
437 w->leaf = rt;
438 return 1;
439 }
440
441 /* Multipath routes are dumped in one route with the
442 * RTA_MULTIPATH attribute. Jump 'rt' to point to the
443 * last sibling of this route (no need to dump the
444 * sibling routes again)
445 */
446 if (rt->rt6i_nsiblings)
447 rt = list_last_entry(&rt->rt6i_siblings,
448 struct rt6_info,
449 rt6i_siblings);
450 }
451 w->leaf = NULL;
452 return 0;
453 }
454
455 static void fib6_dump_end(struct netlink_callback *cb)
456 {
457 struct net *net = sock_net(cb->skb->sk);
458 struct fib6_walker *w = (void *)cb->args[2];
459
460 if (w) {
461 if (cb->args[4]) {
462 cb->args[4] = 0;
463 fib6_walker_unlink(net, w);
464 }
465 cb->args[2] = 0;
466 kfree(w);
467 }
468 cb->done = (void *)cb->args[3];
469 cb->args[1] = 3;
470 }
471
472 static int fib6_dump_done(struct netlink_callback *cb)
473 {
474 fib6_dump_end(cb);
475 return cb->done ? cb->done(cb) : 0;
476 }
477
478 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
479 struct netlink_callback *cb)
480 {
481 struct net *net = sock_net(skb->sk);
482 struct fib6_walker *w;
483 int res;
484
485 w = (void *)cb->args[2];
486 w->root = &table->tb6_root;
487
488 if (cb->args[4] == 0) {
489 w->count = 0;
490 w->skip = 0;
491
492 spin_lock_bh(&table->tb6_lock);
493 res = fib6_walk(net, w);
494 spin_unlock_bh(&table->tb6_lock);
495 if (res > 0) {
496 cb->args[4] = 1;
497 cb->args[5] = w->root->fn_sernum;
498 }
499 } else {
500 if (cb->args[5] != w->root->fn_sernum) {
501 /* Begin at the root if the tree changed */
502 cb->args[5] = w->root->fn_sernum;
503 w->state = FWS_INIT;
504 w->node = w->root;
505 w->skip = w->count;
506 } else
507 w->skip = 0;
508
509 spin_lock_bh(&table->tb6_lock);
510 res = fib6_walk_continue(w);
511 spin_unlock_bh(&table->tb6_lock);
512 if (res <= 0) {
513 fib6_walker_unlink(net, w);
514 cb->args[4] = 0;
515 }
516 }
517
518 return res;
519 }
520
521 static int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
522 {
523 struct net *net = sock_net(skb->sk);
524 unsigned int h, s_h;
525 unsigned int e = 0, s_e;
526 struct rt6_rtnl_dump_arg arg;
527 struct fib6_walker *w;
528 struct fib6_table *tb;
529 struct hlist_head *head;
530 int res = 0;
531
532 s_h = cb->args[0];
533 s_e = cb->args[1];
534
535 w = (void *)cb->args[2];
536 if (!w) {
537 /* New dump:
538 *
539 * 1. hook callback destructor.
540 */
541 cb->args[3] = (long)cb->done;
542 cb->done = fib6_dump_done;
543
544 /*
545 * 2. allocate and initialize walker.
546 */
547 w = kzalloc(sizeof(*w), GFP_ATOMIC);
548 if (!w)
549 return -ENOMEM;
550 w->func = fib6_dump_node;
551 cb->args[2] = (long)w;
552 }
553
554 arg.skb = skb;
555 arg.cb = cb;
556 arg.net = net;
557 w->args = &arg;
558
559 rcu_read_lock();
560 for (h = s_h; h < FIB6_TABLE_HASHSZ; h++, s_e = 0) {
561 e = 0;
562 head = &net->ipv6.fib_table_hash[h];
563 hlist_for_each_entry_rcu(tb, head, tb6_hlist) {
564 if (e < s_e)
565 goto next;
566 res = fib6_dump_table(tb, skb, cb);
567 if (res != 0)
568 goto out;
569 next:
570 e++;
571 }
572 }
573 out:
574 rcu_read_unlock();
575 cb->args[1] = e;
576 cb->args[0] = h;
577
578 res = res < 0 ? res : skb->len;
579 if (res <= 0)
580 fib6_dump_end(cb);
581 return res;
582 }
583
584 /*
585 * Routing Table
586 *
587 * return the appropriate node for a routing tree "add" operation
588 * by either creating and inserting or by returning an existing
589 * node.
590 */
591
592 static struct fib6_node *fib6_add_1(struct net *net,
593 struct fib6_table *table,
594 struct fib6_node *root,
595 struct in6_addr *addr, int plen,
596 int offset, int allow_create,
597 int replace_required,
598 struct netlink_ext_ack *extack)
599 {
600 struct fib6_node *fn, *in, *ln;
601 struct fib6_node *pn = NULL;
602 struct rt6key *key;
603 int bit;
604 __be32 dir = 0;
605
606 RT6_TRACE("fib6_add_1\n");
607
608 /* insert node in tree */
609
610 fn = root;
611
612 do {
613 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
614 lockdep_is_held(&table->tb6_lock));
615 key = (struct rt6key *)((u8 *)leaf + offset);
616
617 /*
618 * Prefix match
619 */
620 if (plen < fn->fn_bit ||
621 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit)) {
622 if (!allow_create) {
623 if (replace_required) {
624 NL_SET_ERR_MSG(extack,
625 "Can not replace route - no match found");
626 pr_warn("Can't replace route, no match found\n");
627 return ERR_PTR(-ENOENT);
628 }
629 pr_warn("NLM_F_CREATE should be set when creating new route\n");
630 }
631 goto insert_above;
632 }
633
634 /*
635 * Exact match ?
636 */
637
638 if (plen == fn->fn_bit) {
639 /* clean up an intermediate node */
640 if (!(fn->fn_flags & RTN_RTINFO)) {
641 RCU_INIT_POINTER(fn->leaf, NULL);
642 rt6_release(leaf);
643 /* remove null_entry in the root node */
644 } else if (fn->fn_flags & RTN_TL_ROOT &&
645 rcu_access_pointer(fn->leaf) ==
646 net->ipv6.ip6_null_entry) {
647 RCU_INIT_POINTER(fn->leaf, NULL);
648 }
649
650 return fn;
651 }
652
653 /*
654 * We have more bits to go
655 */
656
657 /* Try to walk down on tree. */
658 dir = addr_bit_set(addr, fn->fn_bit);
659 pn = fn;
660 fn = dir ?
661 rcu_dereference_protected(fn->right,
662 lockdep_is_held(&table->tb6_lock)) :
663 rcu_dereference_protected(fn->left,
664 lockdep_is_held(&table->tb6_lock));
665 } while (fn);
666
667 if (!allow_create) {
668 /* We should not create new node because
669 * NLM_F_REPLACE was specified without NLM_F_CREATE
670 * I assume it is safe to require NLM_F_CREATE when
671 * REPLACE flag is used! Later we may want to remove the
672 * check for replace_required, because according
673 * to netlink specification, NLM_F_CREATE
674 * MUST be specified if new route is created.
675 * That would keep IPv6 consistent with IPv4
676 */
677 if (replace_required) {
678 NL_SET_ERR_MSG(extack,
679 "Can not replace route - no match found");
680 pr_warn("Can't replace route, no match found\n");
681 return ERR_PTR(-ENOENT);
682 }
683 pr_warn("NLM_F_CREATE should be set when creating new route\n");
684 }
685 /*
686 * We walked to the bottom of tree.
687 * Create new leaf node without children.
688 */
689
690 ln = node_alloc(net);
691
692 if (!ln)
693 return ERR_PTR(-ENOMEM);
694 ln->fn_bit = plen;
695 RCU_INIT_POINTER(ln->parent, pn);
696
697 if (dir)
698 rcu_assign_pointer(pn->right, ln);
699 else
700 rcu_assign_pointer(pn->left, ln);
701
702 return ln;
703
704
705 insert_above:
706 /*
707 * split since we don't have a common prefix anymore or
708 * we have a less significant route.
709 * we've to insert an intermediate node on the list
710 * this new node will point to the one we need to create
711 * and the current
712 */
713
714 pn = rcu_dereference_protected(fn->parent,
715 lockdep_is_held(&table->tb6_lock));
716
717 /* find 1st bit in difference between the 2 addrs.
718
719 See comment in __ipv6_addr_diff: bit may be an invalid value,
720 but if it is >= plen, the value is ignored in any case.
721 */
722
723 bit = __ipv6_addr_diff(addr, &key->addr, sizeof(*addr));
724
725 /*
726 * (intermediate)[in]
727 * / \
728 * (new leaf node)[ln] (old node)[fn]
729 */
730 if (plen > bit) {
731 in = node_alloc(net);
732 ln = node_alloc(net);
733
734 if (!in || !ln) {
735 if (in)
736 node_free_immediate(net, in);
737 if (ln)
738 node_free_immediate(net, ln);
739 return ERR_PTR(-ENOMEM);
740 }
741
742 /*
743 * new intermediate node.
744 * RTN_RTINFO will
745 * be off since that an address that chooses one of
746 * the branches would not match less specific routes
747 * in the other branch
748 */
749
750 in->fn_bit = bit;
751
752 RCU_INIT_POINTER(in->parent, pn);
753 in->leaf = fn->leaf;
754 atomic_inc(&rcu_dereference_protected(in->leaf,
755 lockdep_is_held(&table->tb6_lock))->rt6i_ref);
756
757 /* update parent pointer */
758 if (dir)
759 rcu_assign_pointer(pn->right, in);
760 else
761 rcu_assign_pointer(pn->left, in);
762
763 ln->fn_bit = plen;
764
765 RCU_INIT_POINTER(ln->parent, in);
766 rcu_assign_pointer(fn->parent, in);
767
768 if (addr_bit_set(addr, bit)) {
769 rcu_assign_pointer(in->right, ln);
770 rcu_assign_pointer(in->left, fn);
771 } else {
772 rcu_assign_pointer(in->left, ln);
773 rcu_assign_pointer(in->right, fn);
774 }
775 } else { /* plen <= bit */
776
777 /*
778 * (new leaf node)[ln]
779 * / \
780 * (old node)[fn] NULL
781 */
782
783 ln = node_alloc(net);
784
785 if (!ln)
786 return ERR_PTR(-ENOMEM);
787
788 ln->fn_bit = plen;
789
790 RCU_INIT_POINTER(ln->parent, pn);
791
792 if (addr_bit_set(&key->addr, plen))
793 RCU_INIT_POINTER(ln->right, fn);
794 else
795 RCU_INIT_POINTER(ln->left, fn);
796
797 rcu_assign_pointer(fn->parent, ln);
798
799 if (dir)
800 rcu_assign_pointer(pn->right, ln);
801 else
802 rcu_assign_pointer(pn->left, ln);
803 }
804 return ln;
805 }
806
807 static bool rt6_qualify_for_ecmp(struct rt6_info *rt)
808 {
809 return (rt->rt6i_flags & (RTF_GATEWAY|RTF_ADDRCONF|RTF_DYNAMIC)) ==
810 RTF_GATEWAY;
811 }
812
813 static void fib6_copy_metrics(u32 *mp, const struct mx6_config *mxc)
814 {
815 int i;
816
817 for (i = 0; i < RTAX_MAX; i++) {
818 if (test_bit(i, mxc->mx_valid))
819 mp[i] = mxc->mx[i];
820 }
821 }
822
823 static int fib6_commit_metrics(struct dst_entry *dst, struct mx6_config *mxc)
824 {
825 if (!mxc->mx)
826 return 0;
827
828 if (dst->flags & DST_HOST) {
829 u32 *mp = dst_metrics_write_ptr(dst);
830
831 if (unlikely(!mp))
832 return -ENOMEM;
833
834 fib6_copy_metrics(mp, mxc);
835 } else {
836 dst_init_metrics(dst, mxc->mx, false);
837
838 /* We've stolen mx now. */
839 mxc->mx = NULL;
840 }
841
842 return 0;
843 }
844
845 static void fib6_purge_rt(struct rt6_info *rt, struct fib6_node *fn,
846 struct net *net)
847 {
848 struct fib6_table *table = rt->rt6i_table;
849
850 if (atomic_read(&rt->rt6i_ref) != 1) {
851 /* This route is used as dummy address holder in some split
852 * nodes. It is not leaked, but it still holds other resources,
853 * which must be released in time. So, scan ascendant nodes
854 * and replace dummy references to this route with references
855 * to still alive ones.
856 */
857 while (fn) {
858 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
859 lockdep_is_held(&table->tb6_lock));
860 struct rt6_info *new_leaf;
861 if (!(fn->fn_flags & RTN_RTINFO) && leaf == rt) {
862 new_leaf = fib6_find_prefix(net, table, fn);
863 atomic_inc(&new_leaf->rt6i_ref);
864 rcu_assign_pointer(fn->leaf, new_leaf);
865 rt6_release(rt);
866 }
867 fn = rcu_dereference_protected(fn->parent,
868 lockdep_is_held(&table->tb6_lock));
869 }
870 }
871 }
872
873 /*
874 * Insert routing information in a node.
875 */
876
877 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
878 struct nl_info *info, struct mx6_config *mxc,
879 struct netlink_ext_ack *extack)
880 {
881 struct rt6_info *leaf = rcu_dereference_protected(fn->leaf,
882 lockdep_is_held(&rt->rt6i_table->tb6_lock));
883 struct rt6_info *iter = NULL;
884 struct rt6_info __rcu **ins;
885 struct rt6_info __rcu **fallback_ins = NULL;
886 int replace = (info->nlh &&
887 (info->nlh->nlmsg_flags & NLM_F_REPLACE));
888 int add = (!info->nlh ||
889 (info->nlh->nlmsg_flags & NLM_F_CREATE));
890 int found = 0;
891 bool rt_can_ecmp = rt6_qualify_for_ecmp(rt);
892 u16 nlflags = NLM_F_EXCL;
893 int err;
894
895 if (info->nlh && (info->nlh->nlmsg_flags & NLM_F_APPEND))
896 nlflags |= NLM_F_APPEND;
897
898 ins = &fn->leaf;
899
900 for (iter = leaf; iter;
901 iter = rcu_dereference_protected(iter->dst.rt6_next,
902 lockdep_is_held(&rt->rt6i_table->tb6_lock))) {
903 /*
904 * Search for duplicates
905 */
906
907 if (iter->rt6i_metric == rt->rt6i_metric) {
908 /*
909 * Same priority level
910 */
911 if (info->nlh &&
912 (info->nlh->nlmsg_flags & NLM_F_EXCL))
913 return -EEXIST;
914
915 nlflags &= ~NLM_F_EXCL;
916 if (replace) {
917 if (rt_can_ecmp == rt6_qualify_for_ecmp(iter)) {
918 found++;
919 break;
920 }
921 if (rt_can_ecmp)
922 fallback_ins = fallback_ins ?: ins;
923 goto next_iter;
924 }
925
926 if (rt6_duplicate_nexthop(iter, rt)) {
927 if (rt->rt6i_nsiblings)
928 rt->rt6i_nsiblings = 0;
929 if (!(iter->rt6i_flags & RTF_EXPIRES))
930 return -EEXIST;
931 if (!(rt->rt6i_flags & RTF_EXPIRES))
932 rt6_clean_expires(iter);
933 else
934 rt6_set_expires(iter, rt->dst.expires);
935 iter->rt6i_pmtu = rt->rt6i_pmtu;
936 return -EEXIST;
937 }
938 /* If we have the same destination and the same metric,
939 * but not the same gateway, then the route we try to
940 * add is sibling to this route, increment our counter
941 * of siblings, and later we will add our route to the
942 * list.
943 * Only static routes (which don't have flag
944 * RTF_EXPIRES) are used for ECMPv6.
945 *
946 * To avoid long list, we only had siblings if the
947 * route have a gateway.
948 */
949 if (rt_can_ecmp &&
950 rt6_qualify_for_ecmp(iter))
951 rt->rt6i_nsiblings++;
952 }
953
954 if (iter->rt6i_metric > rt->rt6i_metric)
955 break;
956
957 next_iter:
958 ins = &iter->dst.rt6_next;
959 }
960
961 if (fallback_ins && !found) {
962 /* No ECMP-able route found, replace first non-ECMP one */
963 ins = fallback_ins;
964 iter = rcu_dereference_protected(*ins,
965 lockdep_is_held(&rt->rt6i_table->tb6_lock));
966 found++;
967 }
968
969 /* Reset round-robin state, if necessary */
970 if (ins == &fn->leaf)
971 fn->rr_ptr = NULL;
972
973 /* Link this route to others same route. */
974 if (rt->rt6i_nsiblings) {
975 unsigned int rt6i_nsiblings;
976 struct rt6_info *sibling, *temp_sibling;
977
978 /* Find the first route that have the same metric */
979 sibling = leaf;
980 while (sibling) {
981 if (sibling->rt6i_metric == rt->rt6i_metric &&
982 rt6_qualify_for_ecmp(sibling)) {
983 list_add_tail(&rt->rt6i_siblings,
984 &sibling->rt6i_siblings);
985 break;
986 }
987 sibling = rcu_dereference_protected(sibling->dst.rt6_next,
988 lockdep_is_held(&rt->rt6i_table->tb6_lock));
989 }
990 /* For each sibling in the list, increment the counter of
991 * siblings. BUG() if counters does not match, list of siblings
992 * is broken!
993 */
994 rt6i_nsiblings = 0;
995 list_for_each_entry_safe(sibling, temp_sibling,
996 &rt->rt6i_siblings, rt6i_siblings) {
997 sibling->rt6i_nsiblings++;
998 BUG_ON(sibling->rt6i_nsiblings != rt->rt6i_nsiblings);
999 rt6i_nsiblings++;
1000 }
1001 BUG_ON(rt6i_nsiblings != rt->rt6i_nsiblings);
1002 }
1003
1004 /*
1005 * insert node
1006 */
1007 if (!replace) {
1008 if (!add)
1009 pr_warn("NLM_F_CREATE should be set when creating new route\n");
1010
1011 add:
1012 nlflags |= NLM_F_CREATE;
1013 err = fib6_commit_metrics(&rt->dst, mxc);
1014 if (err)
1015 return err;
1016
1017 rcu_assign_pointer(rt->dst.rt6_next, iter);
1018 atomic_inc(&rt->rt6i_ref);
1019 rcu_assign_pointer(rt->rt6i_node, fn);
1020 rcu_assign_pointer(*ins, rt);
1021 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_ADD,
1022 rt, extack);
1023 if (!info->skip_notify)
1024 inet6_rt_notify(RTM_NEWROUTE, rt, info, nlflags);
1025 info->nl_net->ipv6.rt6_stats->fib_rt_entries++;
1026
1027 if (!(fn->fn_flags & RTN_RTINFO)) {
1028 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1029 fn->fn_flags |= RTN_RTINFO;
1030 }
1031
1032 } else {
1033 int nsiblings;
1034
1035 if (!found) {
1036 if (add)
1037 goto add;
1038 pr_warn("NLM_F_REPLACE set, but no existing node found!\n");
1039 return -ENOENT;
1040 }
1041
1042 err = fib6_commit_metrics(&rt->dst, mxc);
1043 if (err)
1044 return err;
1045
1046 atomic_inc(&rt->rt6i_ref);
1047 rcu_assign_pointer(rt->rt6i_node, fn);
1048 rt->dst.rt6_next = iter->dst.rt6_next;
1049 rcu_assign_pointer(*ins, rt);
1050 call_fib6_entry_notifiers(info->nl_net, FIB_EVENT_ENTRY_REPLACE,
1051 rt, extack);
1052 if (!info->skip_notify)
1053 inet6_rt_notify(RTM_NEWROUTE, rt, info, NLM_F_REPLACE);
1054 if (!(fn->fn_flags & RTN_RTINFO)) {
1055 info->nl_net->ipv6.rt6_stats->fib_route_nodes++;
1056 fn->fn_flags |= RTN_RTINFO;
1057 }
1058 nsiblings = iter->rt6i_nsiblings;
1059 iter->rt6i_node = NULL;
1060 fib6_purge_rt(iter, fn, info->nl_net);
1061 if (rcu_access_pointer(fn->rr_ptr) == iter)
1062 fn->rr_ptr = NULL;
1063 rt6_release(iter);
1064
1065 if (nsiblings) {
1066 /* Replacing an ECMP route, remove all siblings */
1067 ins = &rt->dst.rt6_next;
1068 iter = rcu_dereference_protected(*ins,
1069 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1070 while (iter) {
1071 if (iter->rt6i_metric > rt->rt6i_metric)
1072 break;
1073 if (rt6_qualify_for_ecmp(iter)) {
1074 *ins = iter->dst.rt6_next;
1075 iter->rt6i_node = NULL;
1076 fib6_purge_rt(iter, fn, info->nl_net);
1077 if (rcu_access_pointer(fn->rr_ptr) == iter)
1078 fn->rr_ptr = NULL;
1079 rt6_release(iter);
1080 nsiblings--;
1081 info->nl_net->ipv6.rt6_stats->fib_rt_entries--;
1082 } else {
1083 ins = &iter->dst.rt6_next;
1084 }
1085 iter = rcu_dereference_protected(*ins,
1086 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1087 }
1088 WARN_ON(nsiblings != 0);
1089 }
1090 }
1091
1092 return 0;
1093 }
1094
1095 static void fib6_start_gc(struct net *net, struct rt6_info *rt)
1096 {
1097 if (!timer_pending(&net->ipv6.ip6_fib_timer) &&
1098 (rt->rt6i_flags & (RTF_EXPIRES | RTF_CACHE)))
1099 mod_timer(&net->ipv6.ip6_fib_timer,
1100 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1101 }
1102
1103 void fib6_force_start_gc(struct net *net)
1104 {
1105 if (!timer_pending(&net->ipv6.ip6_fib_timer))
1106 mod_timer(&net->ipv6.ip6_fib_timer,
1107 jiffies + net->ipv6.sysctl.ip6_rt_gc_interval);
1108 }
1109
1110 static void fib6_update_sernum_upto_root(struct rt6_info *rt,
1111 int sernum)
1112 {
1113 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1114 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1115
1116 /* paired with smp_rmb() in rt6_get_cookie_safe() */
1117 smp_wmb();
1118 while (fn) {
1119 fn->fn_sernum = sernum;
1120 fn = rcu_dereference_protected(fn->parent,
1121 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1122 }
1123 }
1124
1125 /*
1126 * Add routing information to the routing tree.
1127 * <destination addr>/<source addr>
1128 * with source addr info in sub-trees
1129 * Need to own table->tb6_lock
1130 */
1131
1132 int fib6_add(struct fib6_node *root, struct rt6_info *rt,
1133 struct nl_info *info, struct mx6_config *mxc,
1134 struct netlink_ext_ack *extack)
1135 {
1136 struct fib6_table *table = rt->rt6i_table;
1137 struct fib6_node *fn, *pn = NULL;
1138 int err = -ENOMEM;
1139 int allow_create = 1;
1140 int replace_required = 0;
1141 int sernum = fib6_new_sernum(info->nl_net);
1142
1143 if (WARN_ON_ONCE(!atomic_read(&rt->dst.__refcnt)))
1144 return -EINVAL;
1145 if (WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE))
1146 return -EINVAL;
1147
1148 if (info->nlh) {
1149 if (!(info->nlh->nlmsg_flags & NLM_F_CREATE))
1150 allow_create = 0;
1151 if (info->nlh->nlmsg_flags & NLM_F_REPLACE)
1152 replace_required = 1;
1153 }
1154 if (!allow_create && !replace_required)
1155 pr_warn("RTM_NEWROUTE with no NLM_F_CREATE or NLM_F_REPLACE\n");
1156
1157 fn = fib6_add_1(info->nl_net, table, root,
1158 &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
1159 offsetof(struct rt6_info, rt6i_dst), allow_create,
1160 replace_required, extack);
1161 if (IS_ERR(fn)) {
1162 err = PTR_ERR(fn);
1163 fn = NULL;
1164 goto out;
1165 }
1166
1167 pn = fn;
1168
1169 #ifdef CONFIG_IPV6_SUBTREES
1170 if (rt->rt6i_src.plen) {
1171 struct fib6_node *sn;
1172
1173 if (!rcu_access_pointer(fn->subtree)) {
1174 struct fib6_node *sfn;
1175
1176 /*
1177 * Create subtree.
1178 *
1179 * fn[main tree]
1180 * |
1181 * sfn[subtree root]
1182 * \
1183 * sn[new leaf node]
1184 */
1185
1186 /* Create subtree root node */
1187 sfn = node_alloc(info->nl_net);
1188 if (!sfn)
1189 goto failure;
1190
1191 atomic_inc(&info->nl_net->ipv6.ip6_null_entry->rt6i_ref);
1192 rcu_assign_pointer(sfn->leaf,
1193 info->nl_net->ipv6.ip6_null_entry);
1194 sfn->fn_flags = RTN_ROOT;
1195
1196 /* Now add the first leaf node to new subtree */
1197
1198 sn = fib6_add_1(info->nl_net, table, sfn,
1199 &rt->rt6i_src.addr, rt->rt6i_src.plen,
1200 offsetof(struct rt6_info, rt6i_src),
1201 allow_create, replace_required, extack);
1202
1203 if (IS_ERR(sn)) {
1204 /* If it is failed, discard just allocated
1205 root, and then (in failure) stale node
1206 in main tree.
1207 */
1208 node_free_immediate(info->nl_net, sfn);
1209 err = PTR_ERR(sn);
1210 goto failure;
1211 }
1212
1213 /* Now link new subtree to main tree */
1214 rcu_assign_pointer(sfn->parent, fn);
1215 rcu_assign_pointer(fn->subtree, sfn);
1216 } else {
1217 sn = fib6_add_1(info->nl_net, table, FIB6_SUBTREE(fn),
1218 &rt->rt6i_src.addr, rt->rt6i_src.plen,
1219 offsetof(struct rt6_info, rt6i_src),
1220 allow_create, replace_required, extack);
1221
1222 if (IS_ERR(sn)) {
1223 err = PTR_ERR(sn);
1224 goto failure;
1225 }
1226 }
1227
1228 if (!rcu_access_pointer(fn->leaf)) {
1229 if (fn->fn_flags & RTN_TL_ROOT) {
1230 /* put back null_entry for root node */
1231 rcu_assign_pointer(fn->leaf,
1232 info->nl_net->ipv6.ip6_null_entry);
1233 } else {
1234 atomic_inc(&rt->rt6i_ref);
1235 rcu_assign_pointer(fn->leaf, rt);
1236 }
1237 }
1238 fn = sn;
1239 }
1240 #endif
1241
1242 err = fib6_add_rt2node(fn, rt, info, mxc, extack);
1243 if (!err) {
1244 fib6_update_sernum_upto_root(rt, sernum);
1245 fib6_start_gc(info->nl_net, rt);
1246 }
1247
1248 out:
1249 if (err) {
1250 #ifdef CONFIG_IPV6_SUBTREES
1251 /*
1252 * If fib6_add_1 has cleared the old leaf pointer in the
1253 * super-tree leaf node we have to find a new one for it.
1254 */
1255 if (pn != fn) {
1256 struct rt6_info *pn_leaf =
1257 rcu_dereference_protected(pn->leaf,
1258 lockdep_is_held(&table->tb6_lock));
1259 if (pn_leaf == rt) {
1260 pn_leaf = NULL;
1261 RCU_INIT_POINTER(pn->leaf, NULL);
1262 atomic_dec(&rt->rt6i_ref);
1263 }
1264 if (!pn_leaf && !(pn->fn_flags & RTN_RTINFO)) {
1265 pn_leaf = fib6_find_prefix(info->nl_net, table,
1266 pn);
1267 #if RT6_DEBUG >= 2
1268 if (!pn_leaf) {
1269 WARN_ON(!pn_leaf);
1270 pn_leaf =
1271 info->nl_net->ipv6.ip6_null_entry;
1272 }
1273 #endif
1274 atomic_inc(&pn_leaf->rt6i_ref);
1275 rcu_assign_pointer(pn->leaf, pn_leaf);
1276 }
1277 }
1278 #endif
1279 goto failure;
1280 }
1281 return err;
1282
1283 failure:
1284 /* fn->leaf could be NULL and fib6_repair_tree() needs to be called if:
1285 * 1. fn is an intermediate node and we failed to add the new
1286 * route to it in both subtree creation failure and fib6_add_rt2node()
1287 * failure case.
1288 * 2. fn is the root node in the table and we fail to add the first
1289 * default route to it.
1290 */
1291 if (fn &&
1292 (!(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)) ||
1293 (fn->fn_flags & RTN_TL_ROOT &&
1294 !rcu_access_pointer(fn->leaf))))
1295 fib6_repair_tree(info->nl_net, table, fn);
1296 /* Always release dst as dst->__refcnt is guaranteed
1297 * to be taken before entering this function
1298 */
1299 dst_release_immediate(&rt->dst);
1300 return err;
1301 }
1302
1303 /*
1304 * Routing tree lookup
1305 *
1306 */
1307
1308 struct lookup_args {
1309 int offset; /* key offset on rt6_info */
1310 const struct in6_addr *addr; /* search key */
1311 };
1312
1313 static struct fib6_node *fib6_lookup_1(struct fib6_node *root,
1314 struct lookup_args *args)
1315 {
1316 struct fib6_node *fn;
1317 __be32 dir;
1318
1319 if (unlikely(args->offset == 0))
1320 return NULL;
1321
1322 /*
1323 * Descend on a tree
1324 */
1325
1326 fn = root;
1327
1328 for (;;) {
1329 struct fib6_node *next;
1330
1331 dir = addr_bit_set(args->addr, fn->fn_bit);
1332
1333 next = dir ? rcu_dereference(fn->right) :
1334 rcu_dereference(fn->left);
1335
1336 if (next) {
1337 fn = next;
1338 continue;
1339 }
1340 break;
1341 }
1342
1343 while (fn) {
1344 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1345
1346 if (subtree || fn->fn_flags & RTN_RTINFO) {
1347 struct rt6_info *leaf = rcu_dereference(fn->leaf);
1348 struct rt6key *key;
1349
1350 if (!leaf)
1351 goto backtrack;
1352
1353 key = (struct rt6key *) ((u8 *)leaf + args->offset);
1354
1355 if (ipv6_prefix_equal(&key->addr, args->addr, key->plen)) {
1356 #ifdef CONFIG_IPV6_SUBTREES
1357 if (subtree) {
1358 struct fib6_node *sfn;
1359 sfn = fib6_lookup_1(subtree, args + 1);
1360 if (!sfn)
1361 goto backtrack;
1362 fn = sfn;
1363 }
1364 #endif
1365 if (fn->fn_flags & RTN_RTINFO)
1366 return fn;
1367 }
1368 }
1369 backtrack:
1370 if (fn->fn_flags & RTN_ROOT)
1371 break;
1372
1373 fn = rcu_dereference(fn->parent);
1374 }
1375
1376 return NULL;
1377 }
1378
1379 /* called with rcu_read_lock() held
1380 */
1381 struct fib6_node *fib6_lookup(struct fib6_node *root, const struct in6_addr *daddr,
1382 const struct in6_addr *saddr)
1383 {
1384 struct fib6_node *fn;
1385 struct lookup_args args[] = {
1386 {
1387 .offset = offsetof(struct rt6_info, rt6i_dst),
1388 .addr = daddr,
1389 },
1390 #ifdef CONFIG_IPV6_SUBTREES
1391 {
1392 .offset = offsetof(struct rt6_info, rt6i_src),
1393 .addr = saddr,
1394 },
1395 #endif
1396 {
1397 .offset = 0, /* sentinel */
1398 }
1399 };
1400
1401 fn = fib6_lookup_1(root, daddr ? args : args + 1);
1402 if (!fn || fn->fn_flags & RTN_TL_ROOT)
1403 fn = root;
1404
1405 return fn;
1406 }
1407
1408 /*
1409 * Get node with specified destination prefix (and source prefix,
1410 * if subtrees are used)
1411 * exact_match == true means we try to find fn with exact match of
1412 * the passed in prefix addr
1413 * exact_match == false means we try to find fn with longest prefix
1414 * match of the passed in prefix addr. This is useful for finding fn
1415 * for cached route as it will be stored in the exception table under
1416 * the node with longest prefix length.
1417 */
1418
1419
1420 static struct fib6_node *fib6_locate_1(struct fib6_node *root,
1421 const struct in6_addr *addr,
1422 int plen, int offset,
1423 bool exact_match)
1424 {
1425 struct fib6_node *fn, *prev = NULL;
1426
1427 for (fn = root; fn ; ) {
1428 struct rt6_info *leaf = rcu_dereference(fn->leaf);
1429 struct rt6key *key;
1430
1431 /* This node is being deleted */
1432 if (!leaf) {
1433 if (plen <= fn->fn_bit)
1434 goto out;
1435 else
1436 goto next;
1437 }
1438
1439 key = (struct rt6key *)((u8 *)leaf + offset);
1440
1441 /*
1442 * Prefix match
1443 */
1444 if (plen < fn->fn_bit ||
1445 !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
1446 goto out;
1447
1448 if (plen == fn->fn_bit)
1449 return fn;
1450
1451 prev = fn;
1452
1453 next:
1454 /*
1455 * We have more bits to go
1456 */
1457 if (addr_bit_set(addr, fn->fn_bit))
1458 fn = rcu_dereference(fn->right);
1459 else
1460 fn = rcu_dereference(fn->left);
1461 }
1462 out:
1463 if (exact_match)
1464 return NULL;
1465 else
1466 return prev;
1467 }
1468
1469 struct fib6_node *fib6_locate(struct fib6_node *root,
1470 const struct in6_addr *daddr, int dst_len,
1471 const struct in6_addr *saddr, int src_len,
1472 bool exact_match)
1473 {
1474 struct fib6_node *fn;
1475
1476 fn = fib6_locate_1(root, daddr, dst_len,
1477 offsetof(struct rt6_info, rt6i_dst),
1478 exact_match);
1479
1480 #ifdef CONFIG_IPV6_SUBTREES
1481 if (src_len) {
1482 WARN_ON(saddr == NULL);
1483 if (fn) {
1484 struct fib6_node *subtree = FIB6_SUBTREE(fn);
1485
1486 if (subtree) {
1487 fn = fib6_locate_1(subtree, saddr, src_len,
1488 offsetof(struct rt6_info, rt6i_src),
1489 exact_match);
1490 }
1491 }
1492 }
1493 #endif
1494
1495 if (fn && fn->fn_flags & RTN_RTINFO)
1496 return fn;
1497
1498 return NULL;
1499 }
1500
1501
1502 /*
1503 * Deletion
1504 *
1505 */
1506
1507 static struct rt6_info *fib6_find_prefix(struct net *net,
1508 struct fib6_table *table,
1509 struct fib6_node *fn)
1510 {
1511 struct fib6_node *child_left, *child_right;
1512
1513 if (fn->fn_flags & RTN_ROOT)
1514 return net->ipv6.ip6_null_entry;
1515
1516 while (fn) {
1517 child_left = rcu_dereference_protected(fn->left,
1518 lockdep_is_held(&table->tb6_lock));
1519 child_right = rcu_dereference_protected(fn->right,
1520 lockdep_is_held(&table->tb6_lock));
1521 if (child_left)
1522 return rcu_dereference_protected(child_left->leaf,
1523 lockdep_is_held(&table->tb6_lock));
1524 if (child_right)
1525 return rcu_dereference_protected(child_right->leaf,
1526 lockdep_is_held(&table->tb6_lock));
1527
1528 fn = FIB6_SUBTREE(fn);
1529 }
1530 return NULL;
1531 }
1532
1533 /*
1534 * Called to trim the tree of intermediate nodes when possible. "fn"
1535 * is the node we want to try and remove.
1536 * Need to own table->tb6_lock
1537 */
1538
1539 static struct fib6_node *fib6_repair_tree(struct net *net,
1540 struct fib6_table *table,
1541 struct fib6_node *fn)
1542 {
1543 int children;
1544 int nstate;
1545 struct fib6_node *child;
1546 struct fib6_walker *w;
1547 int iter = 0;
1548
1549 /* Set fn->leaf to null_entry for root node. */
1550 if (fn->fn_flags & RTN_TL_ROOT) {
1551 rcu_assign_pointer(fn->leaf, net->ipv6.ip6_null_entry);
1552 return fn;
1553 }
1554
1555 for (;;) {
1556 struct fib6_node *fn_r = rcu_dereference_protected(fn->right,
1557 lockdep_is_held(&table->tb6_lock));
1558 struct fib6_node *fn_l = rcu_dereference_protected(fn->left,
1559 lockdep_is_held(&table->tb6_lock));
1560 struct fib6_node *pn = rcu_dereference_protected(fn->parent,
1561 lockdep_is_held(&table->tb6_lock));
1562 struct fib6_node *pn_r = rcu_dereference_protected(pn->right,
1563 lockdep_is_held(&table->tb6_lock));
1564 struct fib6_node *pn_l = rcu_dereference_protected(pn->left,
1565 lockdep_is_held(&table->tb6_lock));
1566 struct rt6_info *fn_leaf = rcu_dereference_protected(fn->leaf,
1567 lockdep_is_held(&table->tb6_lock));
1568 struct rt6_info *pn_leaf = rcu_dereference_protected(pn->leaf,
1569 lockdep_is_held(&table->tb6_lock));
1570 struct rt6_info *new_fn_leaf;
1571
1572 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
1573 iter++;
1574
1575 WARN_ON(fn->fn_flags & RTN_RTINFO);
1576 WARN_ON(fn->fn_flags & RTN_TL_ROOT);
1577 WARN_ON(fn_leaf);
1578
1579 children = 0;
1580 child = NULL;
1581 if (fn_r)
1582 child = fn_r, children |= 1;
1583 if (fn_l)
1584 child = fn_l, children |= 2;
1585
1586 if (children == 3 || FIB6_SUBTREE(fn)
1587 #ifdef CONFIG_IPV6_SUBTREES
1588 /* Subtree root (i.e. fn) may have one child */
1589 || (children && fn->fn_flags & RTN_ROOT)
1590 #endif
1591 ) {
1592 new_fn_leaf = fib6_find_prefix(net, table, fn);
1593 #if RT6_DEBUG >= 2
1594 if (!new_fn_leaf) {
1595 WARN_ON(!new_fn_leaf);
1596 new_fn_leaf = net->ipv6.ip6_null_entry;
1597 }
1598 #endif
1599 atomic_inc(&new_fn_leaf->rt6i_ref);
1600 rcu_assign_pointer(fn->leaf, new_fn_leaf);
1601 return pn;
1602 }
1603
1604 #ifdef CONFIG_IPV6_SUBTREES
1605 if (FIB6_SUBTREE(pn) == fn) {
1606 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1607 RCU_INIT_POINTER(pn->subtree, NULL);
1608 nstate = FWS_L;
1609 } else {
1610 WARN_ON(fn->fn_flags & RTN_ROOT);
1611 #endif
1612 if (pn_r == fn)
1613 rcu_assign_pointer(pn->right, child);
1614 else if (pn_l == fn)
1615 rcu_assign_pointer(pn->left, child);
1616 #if RT6_DEBUG >= 2
1617 else
1618 WARN_ON(1);
1619 #endif
1620 if (child)
1621 rcu_assign_pointer(child->parent, pn);
1622 nstate = FWS_R;
1623 #ifdef CONFIG_IPV6_SUBTREES
1624 }
1625 #endif
1626
1627 read_lock(&net->ipv6.fib6_walker_lock);
1628 FOR_WALKERS(net, w) {
1629 if (!child) {
1630 if (w->node == fn) {
1631 RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1632 w->node = pn;
1633 w->state = nstate;
1634 }
1635 } else {
1636 if (w->node == fn) {
1637 w->node = child;
1638 if (children&2) {
1639 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1640 w->state = w->state >= FWS_R ? FWS_U : FWS_INIT;
1641 } else {
1642 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1643 w->state = w->state >= FWS_C ? FWS_U : FWS_INIT;
1644 }
1645 }
1646 }
1647 }
1648 read_unlock(&net->ipv6.fib6_walker_lock);
1649
1650 node_free(net, fn);
1651 if (pn->fn_flags & RTN_RTINFO || FIB6_SUBTREE(pn))
1652 return pn;
1653
1654 RCU_INIT_POINTER(pn->leaf, NULL);
1655 rt6_release(pn_leaf);
1656 fn = pn;
1657 }
1658 }
1659
1660 static void fib6_del_route(struct fib6_table *table, struct fib6_node *fn,
1661 struct rt6_info __rcu **rtp, struct nl_info *info)
1662 {
1663 struct fib6_walker *w;
1664 struct rt6_info *rt = rcu_dereference_protected(*rtp,
1665 lockdep_is_held(&table->tb6_lock));
1666 struct net *net = info->nl_net;
1667
1668 RT6_TRACE("fib6_del_route\n");
1669
1670 WARN_ON_ONCE(rt->rt6i_flags & RTF_CACHE);
1671
1672 /* Unlink it */
1673 *rtp = rt->dst.rt6_next;
1674 rt->rt6i_node = NULL;
1675 net->ipv6.rt6_stats->fib_rt_entries--;
1676 net->ipv6.rt6_stats->fib_discarded_routes++;
1677
1678 /* Flush all cached dst in exception table */
1679 rt6_flush_exceptions(rt);
1680
1681 /* Reset round-robin state, if necessary */
1682 if (rcu_access_pointer(fn->rr_ptr) == rt)
1683 fn->rr_ptr = NULL;
1684
1685 /* Remove this entry from other siblings */
1686 if (rt->rt6i_nsiblings) {
1687 struct rt6_info *sibling, *next_sibling;
1688
1689 list_for_each_entry_safe(sibling, next_sibling,
1690 &rt->rt6i_siblings, rt6i_siblings)
1691 sibling->rt6i_nsiblings--;
1692 rt->rt6i_nsiblings = 0;
1693 list_del_init(&rt->rt6i_siblings);
1694 }
1695
1696 /* Adjust walkers */
1697 read_lock(&net->ipv6.fib6_walker_lock);
1698 FOR_WALKERS(net, w) {
1699 if (w->state == FWS_C && w->leaf == rt) {
1700 RT6_TRACE("walker %p adjusted by delroute\n", w);
1701 w->leaf = rcu_dereference_protected(rt->dst.rt6_next,
1702 lockdep_is_held(&table->tb6_lock));
1703 if (!w->leaf)
1704 w->state = FWS_U;
1705 }
1706 }
1707 read_unlock(&net->ipv6.fib6_walker_lock);
1708
1709 /* If it was last route, call fib6_repair_tree() to:
1710 * 1. For root node, put back null_entry as how the table was created.
1711 * 2. For other nodes, expunge its radix tree node.
1712 */
1713 if (!rcu_access_pointer(fn->leaf)) {
1714 if (!(fn->fn_flags & RTN_TL_ROOT)) {
1715 fn->fn_flags &= ~RTN_RTINFO;
1716 net->ipv6.rt6_stats->fib_route_nodes--;
1717 }
1718 fn = fib6_repair_tree(net, table, fn);
1719 }
1720
1721 fib6_purge_rt(rt, fn, net);
1722
1723 call_fib6_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, rt, NULL);
1724 if (!info->skip_notify)
1725 inet6_rt_notify(RTM_DELROUTE, rt, info, 0);
1726 rt6_release(rt);
1727 }
1728
1729 /* Need to own table->tb6_lock */
1730 int fib6_del(struct rt6_info *rt, struct nl_info *info)
1731 {
1732 struct fib6_node *fn = rcu_dereference_protected(rt->rt6i_node,
1733 lockdep_is_held(&rt->rt6i_table->tb6_lock));
1734 struct fib6_table *table = rt->rt6i_table;
1735 struct net *net = info->nl_net;
1736 struct rt6_info __rcu **rtp;
1737 struct rt6_info __rcu **rtp_next;
1738
1739 #if RT6_DEBUG >= 2
1740 if (rt->dst.obsolete > 0) {
1741 WARN_ON(fn);
1742 return -ENOENT;
1743 }
1744 #endif
1745 if (!fn || rt == net->ipv6.ip6_null_entry)
1746 return -ENOENT;
1747
1748 WARN_ON(!(fn->fn_flags & RTN_RTINFO));
1749
1750 /* remove cached dst from exception table */
1751 if (rt->rt6i_flags & RTF_CACHE)
1752 return rt6_remove_exception_rt(rt);
1753
1754 /*
1755 * Walk the leaf entries looking for ourself
1756 */
1757
1758 for (rtp = &fn->leaf; *rtp; rtp = rtp_next) {
1759 struct rt6_info *cur = rcu_dereference_protected(*rtp,
1760 lockdep_is_held(&table->tb6_lock));
1761 if (rt == cur) {
1762 fib6_del_route(table, fn, rtp, info);
1763 return 0;
1764 }
1765 rtp_next = &cur->dst.rt6_next;
1766 }
1767 return -ENOENT;
1768 }
1769
1770 /*
1771 * Tree traversal function.
1772 *
1773 * Certainly, it is not interrupt safe.
1774 * However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1775 * It means, that we can modify tree during walking
1776 * and use this function for garbage collection, clone pruning,
1777 * cleaning tree when a device goes down etc. etc.
1778 *
1779 * It guarantees that every node will be traversed,
1780 * and that it will be traversed only once.
1781 *
1782 * Callback function w->func may return:
1783 * 0 -> continue walking.
1784 * positive value -> walking is suspended (used by tree dumps,
1785 * and probably by gc, if it will be split to several slices)
1786 * negative value -> terminate walking.
1787 *
1788 * The function itself returns:
1789 * 0 -> walk is complete.
1790 * >0 -> walk is incomplete (i.e. suspended)
1791 * <0 -> walk is terminated by an error.
1792 *
1793 * This function is called with tb6_lock held.
1794 */
1795
1796 static int fib6_walk_continue(struct fib6_walker *w)
1797 {
1798 struct fib6_node *fn, *pn, *left, *right;
1799
1800 /* w->root should always be table->tb6_root */
1801 WARN_ON_ONCE(!(w->root->fn_flags & RTN_TL_ROOT));
1802
1803 for (;;) {
1804 fn = w->node;
1805 if (!fn)
1806 return 0;
1807
1808 switch (w->state) {
1809 #ifdef CONFIG_IPV6_SUBTREES
1810 case FWS_S:
1811 if (FIB6_SUBTREE(fn)) {
1812 w->node = FIB6_SUBTREE(fn);
1813 continue;
1814 }
1815 w->state = FWS_L;
1816 #endif
1817 /* fall through */
1818 case FWS_L:
1819 left = rcu_dereference_protected(fn->left, 1);
1820 if (left) {
1821 w->node = left;
1822 w->state = FWS_INIT;
1823 continue;
1824 }
1825 w->state = FWS_R;
1826 /* fall through */
1827 case FWS_R:
1828 right = rcu_dereference_protected(fn->right, 1);
1829 if (right) {
1830 w->node = right;
1831 w->state = FWS_INIT;
1832 continue;
1833 }
1834 w->state = FWS_C;
1835 w->leaf = rcu_dereference_protected(fn->leaf, 1);
1836 /* fall through */
1837 case FWS_C:
1838 if (w->leaf && fn->fn_flags & RTN_RTINFO) {
1839 int err;
1840
1841 if (w->skip) {
1842 w->skip--;
1843 goto skip;
1844 }
1845
1846 err = w->func(w);
1847 if (err)
1848 return err;
1849
1850 w->count++;
1851 continue;
1852 }
1853 skip:
1854 w->state = FWS_U;
1855 /* fall through */
1856 case FWS_U:
1857 if (fn == w->root)
1858 return 0;
1859 pn = rcu_dereference_protected(fn->parent, 1);
1860 left = rcu_dereference_protected(pn->left, 1);
1861 right = rcu_dereference_protected(pn->right, 1);
1862 w->node = pn;
1863 #ifdef CONFIG_IPV6_SUBTREES
1864 if (FIB6_SUBTREE(pn) == fn) {
1865 WARN_ON(!(fn->fn_flags & RTN_ROOT));
1866 w->state = FWS_L;
1867 continue;
1868 }
1869 #endif
1870 if (left == fn) {
1871 w->state = FWS_R;
1872 continue;
1873 }
1874 if (right == fn) {
1875 w->state = FWS_C;
1876 w->leaf = rcu_dereference_protected(w->node->leaf, 1);
1877 continue;
1878 }
1879 #if RT6_DEBUG >= 2
1880 WARN_ON(1);
1881 #endif
1882 }
1883 }
1884 }
1885
1886 static int fib6_walk(struct net *net, struct fib6_walker *w)
1887 {
1888 int res;
1889
1890 w->state = FWS_INIT;
1891 w->node = w->root;
1892
1893 fib6_walker_link(net, w);
1894 res = fib6_walk_continue(w);
1895 if (res <= 0)
1896 fib6_walker_unlink(net, w);
1897 return res;
1898 }
1899
1900 static int fib6_clean_node(struct fib6_walker *w)
1901 {
1902 int res;
1903 struct rt6_info *rt;
1904 struct fib6_cleaner *c = container_of(w, struct fib6_cleaner, w);
1905 struct nl_info info = {
1906 .nl_net = c->net,
1907 };
1908
1909 if (c->sernum != FIB6_NO_SERNUM_CHANGE &&
1910 w->node->fn_sernum != c->sernum)
1911 w->node->fn_sernum = c->sernum;
1912
1913 if (!c->func) {
1914 WARN_ON_ONCE(c->sernum == FIB6_NO_SERNUM_CHANGE);
1915 w->leaf = NULL;
1916 return 0;
1917 }
1918
1919 for_each_fib6_walker_rt(w) {
1920 res = c->func(rt, c->arg);
1921 if (res < 0) {
1922 w->leaf = rt;
1923 res = fib6_del(rt, &info);
1924 if (res) {
1925 #if RT6_DEBUG >= 2
1926 pr_debug("%s: del failed: rt=%p@%p err=%d\n",
1927 __func__, rt,
1928 rcu_access_pointer(rt->rt6i_node),
1929 res);
1930 #endif
1931 continue;
1932 }
1933 return 0;
1934 }
1935 WARN_ON(res != 0);
1936 }
1937 w->leaf = rt;
1938 return 0;
1939 }
1940
1941 /*
1942 * Convenient frontend to tree walker.
1943 *
1944 * func is called on each route.
1945 * It may return -1 -> delete this route.
1946 * 0 -> continue walking
1947 */
1948
1949 static void fib6_clean_tree(struct net *net, struct fib6_node *root,
1950 int (*func)(struct rt6_info *, void *arg),
1951 int sernum, void *arg)
1952 {
1953 struct fib6_cleaner c;
1954
1955 c.w.root = root;
1956 c.w.func = fib6_clean_node;
1957 c.w.count = 0;
1958 c.w.skip = 0;
1959 c.func = func;
1960 c.sernum = sernum;
1961 c.arg = arg;
1962 c.net = net;
1963
1964 fib6_walk(net, &c.w);
1965 }
1966
1967 static void __fib6_clean_all(struct net *net,
1968 int (*func)(struct rt6_info *, void *),
1969 int sernum, void *arg)
1970 {
1971 struct fib6_table *table;
1972 struct hlist_head *head;
1973 unsigned int h;
1974
1975 rcu_read_lock();
1976 for (h = 0; h < FIB6_TABLE_HASHSZ; h++) {
1977 head = &net->ipv6.fib_table_hash[h];
1978 hlist_for_each_entry_rcu(table, head, tb6_hlist) {
1979 spin_lock_bh(&table->tb6_lock);
1980 fib6_clean_tree(net, &table->tb6_root,
1981 func, sernum, arg);
1982 spin_unlock_bh(&table->tb6_lock);
1983 }
1984 }
1985 rcu_read_unlock();
1986 }
1987
1988 void fib6_clean_all(struct net *net, int (*func)(struct rt6_info *, void *),
1989 void *arg)
1990 {
1991 __fib6_clean_all(net, func, FIB6_NO_SERNUM_CHANGE, arg);
1992 }
1993
1994 static void fib6_flush_trees(struct net *net)
1995 {
1996 int new_sernum = fib6_new_sernum(net);
1997
1998 __fib6_clean_all(net, NULL, new_sernum, NULL);
1999 }
2000
2001 /*
2002 * Garbage collection
2003 */
2004
2005 static int fib6_age(struct rt6_info *rt, void *arg)
2006 {
2007 struct fib6_gc_args *gc_args = arg;
2008 unsigned long now = jiffies;
2009
2010 /*
2011 * check addrconf expiration here.
2012 * Routes are expired even if they are in use.
2013 */
2014
2015 if (rt->rt6i_flags & RTF_EXPIRES && rt->dst.expires) {
2016 if (time_after(now, rt->dst.expires)) {
2017 RT6_TRACE("expiring %p\n", rt);
2018 return -1;
2019 }
2020 gc_args->more++;
2021 }
2022
2023 /* Also age clones in the exception table.
2024 * Note, that clones are aged out
2025 * only if they are not in use now.
2026 */
2027 rt6_age_exceptions(rt, gc_args, now);
2028
2029 return 0;
2030 }
2031
2032 void fib6_run_gc(unsigned long expires, struct net *net, bool force)
2033 {
2034 struct fib6_gc_args gc_args;
2035 unsigned long now;
2036
2037 if (force) {
2038 spin_lock_bh(&net->ipv6.fib6_gc_lock);
2039 } else if (!spin_trylock_bh(&net->ipv6.fib6_gc_lock)) {
2040 mod_timer(&net->ipv6.ip6_fib_timer, jiffies + HZ);
2041 return;
2042 }
2043 gc_args.timeout = expires ? (int)expires :
2044 net->ipv6.sysctl.ip6_rt_gc_interval;
2045 gc_args.more = 0;
2046
2047 fib6_clean_all(net, fib6_age, &gc_args);
2048 now = jiffies;
2049 net->ipv6.ip6_rt_last_gc = now;
2050
2051 if (gc_args.more)
2052 mod_timer(&net->ipv6.ip6_fib_timer,
2053 round_jiffies(now
2054 + net->ipv6.sysctl.ip6_rt_gc_interval));
2055 else
2056 del_timer(&net->ipv6.ip6_fib_timer);
2057 spin_unlock_bh(&net->ipv6.fib6_gc_lock);
2058 }
2059
2060 static void fib6_gc_timer_cb(struct timer_list *t)
2061 {
2062 struct net *arg = from_timer(arg, t, ipv6.ip6_fib_timer);
2063
2064 fib6_run_gc(0, arg, true);
2065 }
2066
2067 static int __net_init fib6_net_init(struct net *net)
2068 {
2069 size_t size = sizeof(struct hlist_head) * FIB6_TABLE_HASHSZ;
2070 int err;
2071
2072 err = fib6_notifier_init(net);
2073 if (err)
2074 return err;
2075
2076 spin_lock_init(&net->ipv6.fib6_gc_lock);
2077 rwlock_init(&net->ipv6.fib6_walker_lock);
2078 INIT_LIST_HEAD(&net->ipv6.fib6_walkers);
2079 timer_setup(&net->ipv6.ip6_fib_timer, fib6_gc_timer_cb, 0);
2080
2081 net->ipv6.rt6_stats = kzalloc(sizeof(*net->ipv6.rt6_stats), GFP_KERNEL);
2082 if (!net->ipv6.rt6_stats)
2083 goto out_timer;
2084
2085 /* Avoid false sharing : Use at least a full cache line */
2086 size = max_t(size_t, size, L1_CACHE_BYTES);
2087
2088 net->ipv6.fib_table_hash = kzalloc(size, GFP_KERNEL);
2089 if (!net->ipv6.fib_table_hash)
2090 goto out_rt6_stats;
2091
2092 net->ipv6.fib6_main_tbl = kzalloc(sizeof(*net->ipv6.fib6_main_tbl),
2093 GFP_KERNEL);
2094 if (!net->ipv6.fib6_main_tbl)
2095 goto out_fib_table_hash;
2096
2097 net->ipv6.fib6_main_tbl->tb6_id = RT6_TABLE_MAIN;
2098 rcu_assign_pointer(net->ipv6.fib6_main_tbl->tb6_root.leaf,
2099 net->ipv6.ip6_null_entry);
2100 net->ipv6.fib6_main_tbl->tb6_root.fn_flags =
2101 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2102 inet_peer_base_init(&net->ipv6.fib6_main_tbl->tb6_peers);
2103
2104 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2105 net->ipv6.fib6_local_tbl = kzalloc(sizeof(*net->ipv6.fib6_local_tbl),
2106 GFP_KERNEL);
2107 if (!net->ipv6.fib6_local_tbl)
2108 goto out_fib6_main_tbl;
2109 net->ipv6.fib6_local_tbl->tb6_id = RT6_TABLE_LOCAL;
2110 rcu_assign_pointer(net->ipv6.fib6_local_tbl->tb6_root.leaf,
2111 net->ipv6.ip6_null_entry);
2112 net->ipv6.fib6_local_tbl->tb6_root.fn_flags =
2113 RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
2114 inet_peer_base_init(&net->ipv6.fib6_local_tbl->tb6_peers);
2115 #endif
2116 fib6_tables_init(net);
2117
2118 return 0;
2119
2120 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
2121 out_fib6_main_tbl:
2122 kfree(net->ipv6.fib6_main_tbl);
2123 #endif
2124 out_fib_table_hash:
2125 kfree(net->ipv6.fib_table_hash);
2126 out_rt6_stats:
2127 kfree(net->ipv6.rt6_stats);
2128 out_timer:
2129 fib6_notifier_exit(net);
2130 return -ENOMEM;
2131 }
2132
2133 static void fib6_net_exit(struct net *net)
2134 {
2135 unsigned int i;
2136
2137 rt6_ifdown(net, NULL);
2138 del_timer_sync(&net->ipv6.ip6_fib_timer);
2139
2140 for (i = 0; i < FIB6_TABLE_HASHSZ; i++) {
2141 struct hlist_head *head = &net->ipv6.fib_table_hash[i];
2142 struct hlist_node *tmp;
2143 struct fib6_table *tb;
2144
2145 hlist_for_each_entry_safe(tb, tmp, head, tb6_hlist) {
2146 hlist_del(&tb->tb6_hlist);
2147 fib6_free_table(tb);
2148 }
2149 }
2150
2151 kfree(net->ipv6.fib_table_hash);
2152 kfree(net->ipv6.rt6_stats);
2153 fib6_notifier_exit(net);
2154 }
2155
2156 static struct pernet_operations fib6_net_ops = {
2157 .init = fib6_net_init,
2158 .exit = fib6_net_exit,
2159 };
2160
2161 int __init fib6_init(void)
2162 {
2163 int ret = -ENOMEM;
2164
2165 fib6_node_kmem = kmem_cache_create("fib6_nodes",
2166 sizeof(struct fib6_node),
2167 0, SLAB_HWCACHE_ALIGN,
2168 NULL);
2169 if (!fib6_node_kmem)
2170 goto out;
2171
2172 ret = register_pernet_subsys(&fib6_net_ops);
2173 if (ret)
2174 goto out_kmem_cache_create;
2175
2176 ret = __rtnl_register(PF_INET6, RTM_GETROUTE, NULL, inet6_dump_fib,
2177 0);
2178 if (ret)
2179 goto out_unregister_subsys;
2180
2181 __fib6_flush_trees = fib6_flush_trees;
2182 out:
2183 return ret;
2184
2185 out_unregister_subsys:
2186 unregister_pernet_subsys(&fib6_net_ops);
2187 out_kmem_cache_create:
2188 kmem_cache_destroy(fib6_node_kmem);
2189 goto out;
2190 }
2191
2192 void fib6_gc_cleanup(void)
2193 {
2194 unregister_pernet_subsys(&fib6_net_ops);
2195 kmem_cache_destroy(fib6_node_kmem);
2196 }
2197
2198 #ifdef CONFIG_PROC_FS
2199
2200 struct ipv6_route_iter {
2201 struct seq_net_private p;
2202 struct fib6_walker w;
2203 loff_t skip;
2204 struct fib6_table *tbl;
2205 int sernum;
2206 };
2207
2208 static int ipv6_route_seq_show(struct seq_file *seq, void *v)
2209 {
2210 struct rt6_info *rt = v;
2211 struct ipv6_route_iter *iter = seq->private;
2212
2213 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_dst.addr, rt->rt6i_dst.plen);
2214
2215 #ifdef CONFIG_IPV6_SUBTREES
2216 seq_printf(seq, "%pi6 %02x ", &rt->rt6i_src.addr, rt->rt6i_src.plen);
2217 #else
2218 seq_puts(seq, "00000000000000000000000000000000 00 ");
2219 #endif
2220 if (rt->rt6i_flags & RTF_GATEWAY)
2221 seq_printf(seq, "%pi6", &rt->rt6i_gateway);
2222 else
2223 seq_puts(seq, "00000000000000000000000000000000");
2224
2225 seq_printf(seq, " %08x %08x %08x %08x %8s\n",
2226 rt->rt6i_metric, atomic_read(&rt->dst.__refcnt),
2227 rt->dst.__use, rt->rt6i_flags,
2228 rt->dst.dev ? rt->dst.dev->name : "");
2229 iter->w.leaf = NULL;
2230 return 0;
2231 }
2232
2233 static int ipv6_route_yield(struct fib6_walker *w)
2234 {
2235 struct ipv6_route_iter *iter = w->args;
2236
2237 if (!iter->skip)
2238 return 1;
2239
2240 do {
2241 iter->w.leaf = rcu_dereference_protected(
2242 iter->w.leaf->dst.rt6_next,
2243 lockdep_is_held(&iter->tbl->tb6_lock));
2244 iter->skip--;
2245 if (!iter->skip && iter->w.leaf)
2246 return 1;
2247 } while (iter->w.leaf);
2248
2249 return 0;
2250 }
2251
2252 static void ipv6_route_seq_setup_walk(struct ipv6_route_iter *iter,
2253 struct net *net)
2254 {
2255 memset(&iter->w, 0, sizeof(iter->w));
2256 iter->w.func = ipv6_route_yield;
2257 iter->w.root = &iter->tbl->tb6_root;
2258 iter->w.state = FWS_INIT;
2259 iter->w.node = iter->w.root;
2260 iter->w.args = iter;
2261 iter->sernum = iter->w.root->fn_sernum;
2262 INIT_LIST_HEAD(&iter->w.lh);
2263 fib6_walker_link(net, &iter->w);
2264 }
2265
2266 static struct fib6_table *ipv6_route_seq_next_table(struct fib6_table *tbl,
2267 struct net *net)
2268 {
2269 unsigned int h;
2270 struct hlist_node *node;
2271
2272 if (tbl) {
2273 h = (tbl->tb6_id & (FIB6_TABLE_HASHSZ - 1)) + 1;
2274 node = rcu_dereference_bh(hlist_next_rcu(&tbl->tb6_hlist));
2275 } else {
2276 h = 0;
2277 node = NULL;
2278 }
2279
2280 while (!node && h < FIB6_TABLE_HASHSZ) {
2281 node = rcu_dereference_bh(
2282 hlist_first_rcu(&net->ipv6.fib_table_hash[h++]));
2283 }
2284 return hlist_entry_safe(node, struct fib6_table, tb6_hlist);
2285 }
2286
2287 static void ipv6_route_check_sernum(struct ipv6_route_iter *iter)
2288 {
2289 if (iter->sernum != iter->w.root->fn_sernum) {
2290 iter->sernum = iter->w.root->fn_sernum;
2291 iter->w.state = FWS_INIT;
2292 iter->w.node = iter->w.root;
2293 WARN_ON(iter->w.skip);
2294 iter->w.skip = iter->w.count;
2295 }
2296 }
2297
2298 static void *ipv6_route_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2299 {
2300 int r;
2301 struct rt6_info *n;
2302 struct net *net = seq_file_net(seq);
2303 struct ipv6_route_iter *iter = seq->private;
2304
2305 if (!v)
2306 goto iter_table;
2307
2308 n = rcu_dereference_bh(((struct rt6_info *)v)->dst.rt6_next);
2309 if (n) {
2310 ++*pos;
2311 return n;
2312 }
2313
2314 iter_table:
2315 ipv6_route_check_sernum(iter);
2316 spin_lock_bh(&iter->tbl->tb6_lock);
2317 r = fib6_walk_continue(&iter->w);
2318 spin_unlock_bh(&iter->tbl->tb6_lock);
2319 if (r > 0) {
2320 if (v)
2321 ++*pos;
2322 return iter->w.leaf;
2323 } else if (r < 0) {
2324 fib6_walker_unlink(net, &iter->w);
2325 return NULL;
2326 }
2327 fib6_walker_unlink(net, &iter->w);
2328
2329 iter->tbl = ipv6_route_seq_next_table(iter->tbl, net);
2330 if (!iter->tbl)
2331 return NULL;
2332
2333 ipv6_route_seq_setup_walk(iter, net);
2334 goto iter_table;
2335 }
2336
2337 static void *ipv6_route_seq_start(struct seq_file *seq, loff_t *pos)
2338 __acquires(RCU_BH)
2339 {
2340 struct net *net = seq_file_net(seq);
2341 struct ipv6_route_iter *iter = seq->private;
2342
2343 rcu_read_lock_bh();
2344 iter->tbl = ipv6_route_seq_next_table(NULL, net);
2345 iter->skip = *pos;
2346
2347 if (iter->tbl) {
2348 ipv6_route_seq_setup_walk(iter, net);
2349 return ipv6_route_seq_next(seq, NULL, pos);
2350 } else {
2351 return NULL;
2352 }
2353 }
2354
2355 static bool ipv6_route_iter_active(struct ipv6_route_iter *iter)
2356 {
2357 struct fib6_walker *w = &iter->w;
2358 return w->node && !(w->state == FWS_U && w->node == w->root);
2359 }
2360
2361 static void ipv6_route_seq_stop(struct seq_file *seq, void *v)
2362 __releases(RCU_BH)
2363 {
2364 struct net *net = seq_file_net(seq);
2365 struct ipv6_route_iter *iter = seq->private;
2366
2367 if (ipv6_route_iter_active(iter))
2368 fib6_walker_unlink(net, &iter->w);
2369
2370 rcu_read_unlock_bh();
2371 }
2372
2373 static const struct seq_operations ipv6_route_seq_ops = {
2374 .start = ipv6_route_seq_start,
2375 .next = ipv6_route_seq_next,
2376 .stop = ipv6_route_seq_stop,
2377 .show = ipv6_route_seq_show
2378 };
2379
2380 int ipv6_route_open(struct inode *inode, struct file *file)
2381 {
2382 return seq_open_net(inode, file, &ipv6_route_seq_ops,
2383 sizeof(struct ipv6_route_iter));
2384 }
2385
2386 #endif /* CONFIG_PROC_FS */