[IPv6] route: FIB6 configuration using struct fib6_config
[safe/jmp/linux-2.6] / 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  *      $Id: ip6_fib.c,v 1.25 2001/10/31 21:55:55 davem Exp $
9  *
10  *      This program is free software; you can redistribute it and/or
11  *      modify it under the terms of the GNU General Public License
12  *      as published by the Free Software Foundation; either version
13  *      2 of the License, or (at your option) any later version.
14  */
15
16 /*
17  *      Changes:
18  *      Yuji SEKIYA @USAGI:     Support default route on router node;
19  *                              remove ip6_null_entry from the top of
20  *                              routing table.
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
31 #ifdef  CONFIG_PROC_FS
32 #include <linux/proc_fs.h>
33 #endif
34
35 #include <net/ipv6.h>
36 #include <net/ndisc.h>
37 #include <net/addrconf.h>
38
39 #include <net/ip6_fib.h>
40 #include <net/ip6_route.h>
41
42 #define RT6_DEBUG 2
43
44 #if RT6_DEBUG >= 3
45 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
46 #else
47 #define RT6_TRACE(x...) do { ; } while (0)
48 #endif
49
50 struct rt6_statistics   rt6_stats;
51
52 static kmem_cache_t * fib6_node_kmem __read_mostly;
53
54 enum fib_walk_state_t
55 {
56 #ifdef CONFIG_IPV6_SUBTREES
57         FWS_S,
58 #endif
59         FWS_L,
60         FWS_R,
61         FWS_C,
62         FWS_U
63 };
64
65 struct fib6_cleaner_t
66 {
67         struct fib6_walker_t w;
68         int (*func)(struct rt6_info *, void *arg);
69         void *arg;
70 };
71
72 static DEFINE_RWLOCK(fib6_walker_lock);
73
74 #ifdef CONFIG_IPV6_SUBTREES
75 #define FWS_INIT FWS_S
76 #define SUBTREE(fn) ((fn)->subtree)
77 #else
78 #define FWS_INIT FWS_L
79 #define SUBTREE(fn) NULL
80 #endif
81
82 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
83 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
84 static int fib6_walk(struct fib6_walker_t *w);
85 static int fib6_walk_continue(struct fib6_walker_t *w);
86
87 /*
88  *      A routing update causes an increase of the serial number on the
89  *      affected subtree. This allows for cached routes to be asynchronously
90  *      tested when modifications are made to the destination cache as a
91  *      result of redirects, path MTU changes, etc.
92  */
93
94 static __u32 rt_sernum;
95
96 static DEFINE_TIMER(ip6_fib_timer, fib6_run_gc, 0, 0);
97
98 static struct fib6_walker_t fib6_walker_list = {
99         .prev   = &fib6_walker_list,
100         .next   = &fib6_walker_list, 
101 };
102
103 #define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
104
105 static inline void fib6_walker_link(struct fib6_walker_t *w)
106 {
107         write_lock_bh(&fib6_walker_lock);
108         w->next = fib6_walker_list.next;
109         w->prev = &fib6_walker_list;
110         w->next->prev = w;
111         w->prev->next = w;
112         write_unlock_bh(&fib6_walker_lock);
113 }
114
115 static inline void fib6_walker_unlink(struct fib6_walker_t *w)
116 {
117         write_lock_bh(&fib6_walker_lock);
118         w->next->prev = w->prev;
119         w->prev->next = w->next;
120         w->prev = w->next = w;
121         write_unlock_bh(&fib6_walker_lock);
122 }
123 static __inline__ u32 fib6_new_sernum(void)
124 {
125         u32 n = ++rt_sernum;
126         if ((__s32)n <= 0)
127                 rt_sernum = n = 1;
128         return n;
129 }
130
131 /*
132  *      Auxiliary address test functions for the radix tree.
133  *
134  *      These assume a 32bit processor (although it will work on 
135  *      64bit processors)
136  */
137
138 /*
139  *      test bit
140  */
141
142 static __inline__ int addr_bit_set(void *token, int fn_bit)
143 {
144         __u32 *addr = token;
145
146         return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
147 }
148
149 static __inline__ struct fib6_node * node_alloc(void)
150 {
151         struct fib6_node *fn;
152
153         if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
154                 memset(fn, 0, sizeof(struct fib6_node));
155
156         return fn;
157 }
158
159 static __inline__ void node_free(struct fib6_node * fn)
160 {
161         kmem_cache_free(fib6_node_kmem, fn);
162 }
163
164 static __inline__ void rt6_release(struct rt6_info *rt)
165 {
166         if (atomic_dec_and_test(&rt->rt6i_ref))
167                 dst_free(&rt->u.dst);
168 }
169
170 static struct fib6_table fib6_main_tbl = {
171         .tb6_id         = RT6_TABLE_MAIN,
172         .tb6_lock       = RW_LOCK_UNLOCKED,
173         .tb6_root       = {
174                 .leaf           = &ip6_null_entry,
175                 .fn_flags       = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
176         },
177 };
178
179 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
180 #define FIB_TABLE_HASHSZ 256
181 #else
182 #define FIB_TABLE_HASHSZ 1
183 #endif
184 static struct hlist_head fib_table_hash[FIB_TABLE_HASHSZ];
185
186 static void fib6_link_table(struct fib6_table *tb)
187 {
188         unsigned int h;
189
190         h = tb->tb6_id & (FIB_TABLE_HASHSZ - 1);
191
192         /*
193          * No protection necessary, this is the only list mutatation
194          * operation, tables never disappear once they exist.
195          */
196         hlist_add_head_rcu(&tb->tb6_hlist, &fib_table_hash[h]);
197 }
198
199 #ifdef CONFIG_IPV6_MULTIPLE_TABLES
200 static struct fib6_table fib6_local_tbl = {
201         .tb6_id         = RT6_TABLE_LOCAL,
202         .tb6_lock       = RW_LOCK_UNLOCKED,
203         .tb6_root       = {
204                 .leaf           = &ip6_null_entry,
205                 .fn_flags       = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO,
206         },
207 };
208
209 static struct fib6_table *fib6_alloc_table(u32 id)
210 {
211         struct fib6_table *table;
212
213         table = kzalloc(sizeof(*table), GFP_ATOMIC);
214         if (table != NULL) {
215                 table->tb6_id = id;
216                 table->tb6_lock = RW_LOCK_UNLOCKED;
217                 table->tb6_root.leaf = &ip6_null_entry;
218                 table->tb6_root.fn_flags = RTN_ROOT | RTN_TL_ROOT | RTN_RTINFO;
219         }
220
221         return table;
222 }
223
224 struct fib6_table *fib6_new_table(u32 id)
225 {
226         struct fib6_table *tb;
227
228         if (id == 0)
229                 id = RT6_TABLE_MAIN;
230         tb = fib6_get_table(id);
231         if (tb)
232                 return tb;
233
234         tb = fib6_alloc_table(id);
235         if (tb != NULL)
236                 fib6_link_table(tb);
237
238         return tb;
239 }
240
241 struct fib6_table *fib6_get_table(u32 id)
242 {
243         struct fib6_table *tb;
244         struct hlist_node *node;
245         unsigned int h;
246
247         if (id == 0)
248                 id = RT6_TABLE_MAIN;
249         h = id & (FIB_TABLE_HASHSZ - 1);
250         rcu_read_lock();
251         hlist_for_each_entry_rcu(tb, node, &fib_table_hash[h], tb6_hlist) {
252                 if (tb->tb6_id == id) {
253                         rcu_read_unlock();
254                         return tb;
255                 }
256         }
257         rcu_read_unlock();
258
259         return NULL;
260 }
261
262 static void __init fib6_tables_init(void)
263 {
264         fib6_link_table(&fib6_main_tbl);
265         fib6_link_table(&fib6_local_tbl);
266 }
267
268 #else
269
270 struct fib6_table *fib6_new_table(u32 id)
271 {
272         return fib6_get_table(id);
273 }
274
275 struct fib6_table *fib6_get_table(u32 id)
276 {
277         return &fib6_main_tbl;
278 }
279
280 struct dst_entry *fib6_rule_lookup(struct flowi *fl, int flags,
281                                    pol_lookup_t lookup)
282 {
283         return (struct dst_entry *) lookup(&fib6_main_tbl, fl, flags);
284 }
285
286 static void __init fib6_tables_init(void)
287 {
288         fib6_link_table(&fib6_main_tbl);
289 }
290
291 #endif
292
293 static int fib6_dump_node(struct fib6_walker_t *w)
294 {
295         int res;
296         struct rt6_info *rt;
297
298         for (rt = w->leaf; rt; rt = rt->u.next) {
299                 res = rt6_dump_route(rt, w->args);
300                 if (res < 0) {
301                         /* Frame is full, suspend walking */
302                         w->leaf = rt;
303                         return 1;
304                 }
305                 BUG_TRAP(res!=0);
306         }
307         w->leaf = NULL;
308         return 0;
309 }
310
311 static void fib6_dump_end(struct netlink_callback *cb)
312 {
313         struct fib6_walker_t *w = (void*)cb->args[2];
314
315         if (w) {
316                 cb->args[2] = 0;
317                 kfree(w);
318         }
319         cb->done = (void*)cb->args[3];
320         cb->args[1] = 3;
321 }
322
323 static int fib6_dump_done(struct netlink_callback *cb)
324 {
325         fib6_dump_end(cb);
326         return cb->done ? cb->done(cb) : 0;
327 }
328
329 static int fib6_dump_table(struct fib6_table *table, struct sk_buff *skb,
330                            struct netlink_callback *cb)
331 {
332         struct fib6_walker_t *w;
333         int res;
334
335         w = (void *)cb->args[2];
336         w->root = &table->tb6_root;
337
338         if (cb->args[4] == 0) {
339                 read_lock_bh(&table->tb6_lock);
340                 res = fib6_walk(w);
341                 read_unlock_bh(&table->tb6_lock);
342                 if (res > 0)
343                         cb->args[4] = 1;
344         } else {
345                 read_lock_bh(&table->tb6_lock);
346                 res = fib6_walk_continue(w);
347                 read_unlock_bh(&table->tb6_lock);
348                 if (res != 0) {
349                         if (res < 0)
350                                 fib6_walker_unlink(w);
351                         goto end;
352                 }
353                 fib6_walker_unlink(w);
354                 cb->args[4] = 0;
355         }
356 end:
357         return res;
358 }
359
360 int inet6_dump_fib(struct sk_buff *skb, struct netlink_callback *cb)
361 {
362         unsigned int h, s_h;
363         unsigned int e = 0, s_e;
364         struct rt6_rtnl_dump_arg arg;
365         struct fib6_walker_t *w;
366         struct fib6_table *tb;
367         struct hlist_node *node;
368         int res = 0;
369
370         s_h = cb->args[0];
371         s_e = cb->args[1];
372
373         w = (void *)cb->args[2];
374         if (w == NULL) {
375                 /* New dump:
376                  *
377                  * 1. hook callback destructor.
378                  */
379                 cb->args[3] = (long)cb->done;
380                 cb->done = fib6_dump_done;
381
382                 /*
383                  * 2. allocate and initialize walker.
384                  */
385                 w = kzalloc(sizeof(*w), GFP_ATOMIC);
386                 if (w == NULL)
387                         return -ENOMEM;
388                 w->func = fib6_dump_node;
389                 cb->args[2] = (long)w;
390         }
391
392         arg.skb = skb;
393         arg.cb = cb;
394         w->args = &arg;
395
396         for (h = s_h; h < FIB_TABLE_HASHSZ; h++, s_e = 0) {
397                 e = 0;
398                 hlist_for_each_entry(tb, node, &fib_table_hash[h], tb6_hlist) {
399                         if (e < s_e)
400                                 goto next;
401                         res = fib6_dump_table(tb, skb, cb);
402                         if (res != 0)
403                                 goto out;
404 next:
405                         e++;
406                 }
407         }
408 out:
409         cb->args[1] = e;
410         cb->args[0] = h;
411
412         res = res < 0 ? res : skb->len;
413         if (res <= 0)
414                 fib6_dump_end(cb);
415         return res;
416 }
417
418 /*
419  *      Routing Table
420  *
421  *      return the appropriate node for a routing tree "add" operation
422  *      by either creating and inserting or by returning an existing
423  *      node.
424  */
425
426 static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
427                                      int addrlen, int plen,
428                                      int offset)
429 {
430         struct fib6_node *fn, *in, *ln;
431         struct fib6_node *pn = NULL;
432         struct rt6key *key;
433         int     bit;
434         int     dir = 0;
435         __u32   sernum = fib6_new_sernum();
436
437         RT6_TRACE("fib6_add_1\n");
438
439         /* insert node in tree */
440
441         fn = root;
442
443         do {
444                 key = (struct rt6key *)((u8 *)fn->leaf + offset);
445
446                 /*
447                  *      Prefix match
448                  */
449                 if (plen < fn->fn_bit ||
450                     !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
451                         goto insert_above;
452                 
453                 /*
454                  *      Exact match ?
455                  */
456                          
457                 if (plen == fn->fn_bit) {
458                         /* clean up an intermediate node */
459                         if ((fn->fn_flags & RTN_RTINFO) == 0) {
460                                 rt6_release(fn->leaf);
461                                 fn->leaf = NULL;
462                         }
463                         
464                         fn->fn_sernum = sernum;
465                                 
466                         return fn;
467                 }
468
469                 /*
470                  *      We have more bits to go
471                  */
472                          
473                 /* Try to walk down on tree. */
474                 fn->fn_sernum = sernum;
475                 dir = addr_bit_set(addr, fn->fn_bit);
476                 pn = fn;
477                 fn = dir ? fn->right: fn->left;
478         } while (fn);
479
480         /*
481          *      We walked to the bottom of tree.
482          *      Create new leaf node without children.
483          */
484
485         ln = node_alloc();
486
487         if (ln == NULL)
488                 return NULL;
489         ln->fn_bit = plen;
490                         
491         ln->parent = pn;
492         ln->fn_sernum = sernum;
493
494         if (dir)
495                 pn->right = ln;
496         else
497                 pn->left  = ln;
498
499         return ln;
500
501
502 insert_above:
503         /*
504          * split since we don't have a common prefix anymore or 
505          * we have a less significant route.
506          * we've to insert an intermediate node on the list
507          * this new node will point to the one we need to create
508          * and the current
509          */
510
511         pn = fn->parent;
512
513         /* find 1st bit in difference between the 2 addrs.
514
515            See comment in __ipv6_addr_diff: bit may be an invalid value,
516            but if it is >= plen, the value is ignored in any case.
517          */
518         
519         bit = __ipv6_addr_diff(addr, &key->addr, addrlen);
520
521         /* 
522          *              (intermediate)[in]      
523          *                /        \
524          *      (new leaf node)[ln] (old node)[fn]
525          */
526         if (plen > bit) {
527                 in = node_alloc();
528                 ln = node_alloc();
529                 
530                 if (in == NULL || ln == NULL) {
531                         if (in)
532                                 node_free(in);
533                         if (ln)
534                                 node_free(ln);
535                         return NULL;
536                 }
537
538                 /* 
539                  * new intermediate node. 
540                  * RTN_RTINFO will
541                  * be off since that an address that chooses one of
542                  * the branches would not match less specific routes
543                  * in the other branch
544                  */
545
546                 in->fn_bit = bit;
547
548                 in->parent = pn;
549                 in->leaf = fn->leaf;
550                 atomic_inc(&in->leaf->rt6i_ref);
551
552                 in->fn_sernum = sernum;
553
554                 /* update parent pointer */
555                 if (dir)
556                         pn->right = in;
557                 else
558                         pn->left  = in;
559
560                 ln->fn_bit = plen;
561
562                 ln->parent = in;
563                 fn->parent = in;
564
565                 ln->fn_sernum = sernum;
566
567                 if (addr_bit_set(addr, bit)) {
568                         in->right = ln;
569                         in->left  = fn;
570                 } else {
571                         in->left  = ln;
572                         in->right = fn;
573                 }
574         } else { /* plen <= bit */
575
576                 /* 
577                  *              (new leaf node)[ln]
578                  *                /        \
579                  *           (old node)[fn] NULL
580                  */
581
582                 ln = node_alloc();
583
584                 if (ln == NULL)
585                         return NULL;
586
587                 ln->fn_bit = plen;
588
589                 ln->parent = pn;
590
591                 ln->fn_sernum = sernum;
592                 
593                 if (dir)
594                         pn->right = ln;
595                 else
596                         pn->left  = ln;
597
598                 if (addr_bit_set(&key->addr, plen))
599                         ln->right = fn;
600                 else
601                         ln->left  = fn;
602
603                 fn->parent = ln;
604         }
605         return ln;
606 }
607
608 /*
609  *      Insert routing information in a node.
610  */
611
612 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
613                             struct nl_info *info)
614 {
615         struct rt6_info *iter = NULL;
616         struct rt6_info **ins;
617
618         ins = &fn->leaf;
619
620         if (fn->fn_flags&RTN_TL_ROOT &&
621             fn->leaf == &ip6_null_entry &&
622             !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF)) ){
623                 fn->leaf = rt;
624                 rt->u.next = NULL;
625                 goto out;
626         }
627
628         for (iter = fn->leaf; iter; iter=iter->u.next) {
629                 /*
630                  *      Search for duplicates
631                  */
632
633                 if (iter->rt6i_metric == rt->rt6i_metric) {
634                         /*
635                          *      Same priority level
636                          */
637
638                         if (iter->rt6i_dev == rt->rt6i_dev &&
639                             iter->rt6i_idev == rt->rt6i_idev &&
640                             ipv6_addr_equal(&iter->rt6i_gateway,
641                                             &rt->rt6i_gateway)) {
642                                 if (!(iter->rt6i_flags&RTF_EXPIRES))
643                                         return -EEXIST;
644                                 iter->rt6i_expires = rt->rt6i_expires;
645                                 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
646                                         iter->rt6i_flags &= ~RTF_EXPIRES;
647                                         iter->rt6i_expires = 0;
648                                 }
649                                 return -EEXIST;
650                         }
651                 }
652
653                 if (iter->rt6i_metric > rt->rt6i_metric)
654                         break;
655
656                 ins = &iter->u.next;
657         }
658
659         /*
660          *      insert node
661          */
662
663 out:
664         rt->u.next = iter;
665         *ins = rt;
666         rt->rt6i_node = fn;
667         atomic_inc(&rt->rt6i_ref);
668         inet6_rt_notify(RTM_NEWROUTE, rt, info);
669         rt6_stats.fib_rt_entries++;
670
671         if ((fn->fn_flags & RTN_RTINFO) == 0) {
672                 rt6_stats.fib_route_nodes++;
673                 fn->fn_flags |= RTN_RTINFO;
674         }
675
676         return 0;
677 }
678
679 static __inline__ void fib6_start_gc(struct rt6_info *rt)
680 {
681         if (ip6_fib_timer.expires == 0 &&
682             (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
683                 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
684 }
685
686 void fib6_force_start_gc(void)
687 {
688         if (ip6_fib_timer.expires == 0)
689                 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
690 }
691
692 /*
693  *      Add routing information to the routing tree.
694  *      <destination addr>/<source addr>
695  *      with source addr info in sub-trees
696  */
697
698 int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nl_info *info)
699 {
700         struct fib6_node *fn;
701         int err = -ENOMEM;
702
703         fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
704                         rt->rt6i_dst.plen, offsetof(struct rt6_info, rt6i_dst));
705
706         if (fn == NULL)
707                 goto out;
708
709 #ifdef CONFIG_IPV6_SUBTREES
710         if (rt->rt6i_src.plen) {
711                 struct fib6_node *sn;
712
713                 if (fn->subtree == NULL) {
714                         struct fib6_node *sfn;
715
716                         /*
717                          * Create subtree.
718                          *
719                          *              fn[main tree]
720                          *              |
721                          *              sfn[subtree root]
722                          *                 \
723                          *                  sn[new leaf node]
724                          */
725
726                         /* Create subtree root node */
727                         sfn = node_alloc();
728                         if (sfn == NULL)
729                                 goto st_failure;
730
731                         sfn->leaf = &ip6_null_entry;
732                         atomic_inc(&ip6_null_entry.rt6i_ref);
733                         sfn->fn_flags = RTN_ROOT;
734                         sfn->fn_sernum = fib6_new_sernum();
735
736                         /* Now add the first leaf node to new subtree */
737
738                         sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
739                                         sizeof(struct in6_addr), rt->rt6i_src.plen,
740                                         offsetof(struct rt6_info, rt6i_src));
741
742                         if (sn == NULL) {
743                                 /* If it is failed, discard just allocated
744                                    root, and then (in st_failure) stale node
745                                    in main tree.
746                                  */
747                                 node_free(sfn);
748                                 goto st_failure;
749                         }
750
751                         /* Now link new subtree to main tree */
752                         sfn->parent = fn;
753                         fn->subtree = sfn;
754                         if (fn->leaf == NULL) {
755                                 fn->leaf = rt;
756                                 atomic_inc(&rt->rt6i_ref);
757                         }
758                 } else {
759                         sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
760                                         sizeof(struct in6_addr), rt->rt6i_src.plen,
761                                         offsetof(struct rt6_info, rt6i_src));
762
763                         if (sn == NULL)
764                                 goto st_failure;
765                 }
766
767                 fn = sn;
768         }
769 #endif
770
771         err = fib6_add_rt2node(fn, rt, info);
772
773         if (err == 0) {
774                 fib6_start_gc(rt);
775                 if (!(rt->rt6i_flags&RTF_CACHE))
776                         fib6_prune_clones(fn, rt);
777         }
778
779 out:
780         if (err)
781                 dst_free(&rt->u.dst);
782         return err;
783
784 #ifdef CONFIG_IPV6_SUBTREES
785         /* Subtree creation failed, probably main tree node
786            is orphan. If it is, shoot it.
787          */
788 st_failure:
789         if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
790                 fib6_repair_tree(fn);
791         dst_free(&rt->u.dst);
792         return err;
793 #endif
794 }
795
796 /*
797  *      Routing tree lookup
798  *
799  */
800
801 struct lookup_args {
802         int             offset;         /* key offset on rt6_info       */
803         struct in6_addr *addr;          /* search key                   */
804 };
805
806 static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
807                                         struct lookup_args *args)
808 {
809         struct fib6_node *fn;
810         int dir;
811
812         /*
813          *      Descend on a tree
814          */
815
816         fn = root;
817
818         for (;;) {
819                 struct fib6_node *next;
820
821                 dir = addr_bit_set(args->addr, fn->fn_bit);
822
823                 next = dir ? fn->right : fn->left;
824
825                 if (next) {
826                         fn = next;
827                         continue;
828                 }
829
830                 break;
831         }
832
833         while ((fn->fn_flags & RTN_ROOT) == 0) {
834 #ifdef CONFIG_IPV6_SUBTREES
835                 if (fn->subtree) {
836                         struct fib6_node *st;
837                         struct lookup_args *narg;
838
839                         narg = args + 1;
840
841                         if (narg->addr) {
842                                 st = fib6_lookup_1(fn->subtree, narg);
843
844                                 if (st && !(st->fn_flags & RTN_ROOT))
845                                         return st;
846                         }
847                 }
848 #endif
849
850                 if (fn->fn_flags & RTN_RTINFO) {
851                         struct rt6key *key;
852
853                         key = (struct rt6key *) ((u8 *) fn->leaf +
854                                                  args->offset);
855
856                         if (ipv6_prefix_equal(&key->addr, args->addr, key->plen))
857                                 return fn;
858                 }
859
860                 fn = fn->parent;
861         }
862
863         return NULL;
864 }
865
866 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
867                                struct in6_addr *saddr)
868 {
869         struct lookup_args args[2];
870         struct fib6_node *fn;
871
872         args[0].offset = offsetof(struct rt6_info, rt6i_dst);
873         args[0].addr = daddr;
874
875 #ifdef CONFIG_IPV6_SUBTREES
876         args[1].offset = offsetof(struct rt6_info, rt6i_src);
877         args[1].addr = saddr;
878 #endif
879
880         fn = fib6_lookup_1(root, args);
881
882         if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
883                 fn = root;
884
885         return fn;
886 }
887
888 /*
889  *      Get node with specified destination prefix (and source prefix,
890  *      if subtrees are used)
891  */
892
893
894 static struct fib6_node * fib6_locate_1(struct fib6_node *root,
895                                         struct in6_addr *addr,
896                                         int plen, int offset)
897 {
898         struct fib6_node *fn;
899
900         for (fn = root; fn ; ) {
901                 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
902
903                 /*
904                  *      Prefix match
905                  */
906                 if (plen < fn->fn_bit ||
907                     !ipv6_prefix_equal(&key->addr, addr, fn->fn_bit))
908                         return NULL;
909
910                 if (plen == fn->fn_bit)
911                         return fn;
912
913                 /*
914                  *      We have more bits to go
915                  */
916                 if (addr_bit_set(addr, fn->fn_bit))
917                         fn = fn->right;
918                 else
919                         fn = fn->left;
920         }
921         return NULL;
922 }
923
924 struct fib6_node * fib6_locate(struct fib6_node *root,
925                                struct in6_addr *daddr, int dst_len,
926                                struct in6_addr *saddr, int src_len)
927 {
928         struct fib6_node *fn;
929
930         fn = fib6_locate_1(root, daddr, dst_len,
931                            offsetof(struct rt6_info, rt6i_dst));
932
933 #ifdef CONFIG_IPV6_SUBTREES
934         if (src_len) {
935                 BUG_TRAP(saddr!=NULL);
936                 if (fn == NULL)
937                         fn = fn->subtree;
938                 if (fn)
939                         fn = fib6_locate_1(fn, saddr, src_len,
940                                            offsetof(struct rt6_info, rt6i_src));
941         }
942 #endif
943
944         if (fn && fn->fn_flags&RTN_RTINFO)
945                 return fn;
946
947         return NULL;
948 }
949
950
951 /*
952  *      Deletion
953  *
954  */
955
956 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
957 {
958         if (fn->fn_flags&RTN_ROOT)
959                 return &ip6_null_entry;
960
961         while(fn) {
962                 if(fn->left)
963                         return fn->left->leaf;
964
965                 if(fn->right)
966                         return fn->right->leaf;
967
968                 fn = SUBTREE(fn);
969         }
970         return NULL;
971 }
972
973 /*
974  *      Called to trim the tree of intermediate nodes when possible. "fn"
975  *      is the node we want to try and remove.
976  */
977
978 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
979 {
980         int children;
981         int nstate;
982         struct fib6_node *child, *pn;
983         struct fib6_walker_t *w;
984         int iter = 0;
985
986         for (;;) {
987                 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
988                 iter++;
989
990                 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
991                 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
992                 BUG_TRAP(fn->leaf==NULL);
993
994                 children = 0;
995                 child = NULL;
996                 if (fn->right) child = fn->right, children |= 1;
997                 if (fn->left) child = fn->left, children |= 2;
998
999                 if (children == 3 || SUBTREE(fn) 
1000 #ifdef CONFIG_IPV6_SUBTREES
1001                     /* Subtree root (i.e. fn) may have one child */
1002                     || (children && fn->fn_flags&RTN_ROOT)
1003 #endif
1004                     ) {
1005                         fn->leaf = fib6_find_prefix(fn);
1006 #if RT6_DEBUG >= 2
1007                         if (fn->leaf==NULL) {
1008                                 BUG_TRAP(fn->leaf);
1009                                 fn->leaf = &ip6_null_entry;
1010                         }
1011 #endif
1012                         atomic_inc(&fn->leaf->rt6i_ref);
1013                         return fn->parent;
1014                 }
1015
1016                 pn = fn->parent;
1017 #ifdef CONFIG_IPV6_SUBTREES
1018                 if (SUBTREE(pn) == fn) {
1019                         BUG_TRAP(fn->fn_flags&RTN_ROOT);
1020                         SUBTREE(pn) = NULL;
1021                         nstate = FWS_L;
1022                 } else {
1023                         BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
1024 #endif
1025                         if (pn->right == fn) pn->right = child;
1026                         else if (pn->left == fn) pn->left = child;
1027 #if RT6_DEBUG >= 2
1028                         else BUG_TRAP(0);
1029 #endif
1030                         if (child)
1031                                 child->parent = pn;
1032                         nstate = FWS_R;
1033 #ifdef CONFIG_IPV6_SUBTREES
1034                 }
1035 #endif
1036
1037                 read_lock(&fib6_walker_lock);
1038                 FOR_WALKERS(w) {
1039                         if (child == NULL) {
1040                                 if (w->root == fn) {
1041                                         w->root = w->node = NULL;
1042                                         RT6_TRACE("W %p adjusted by delroot 1\n", w);
1043                                 } else if (w->node == fn) {
1044                                         RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
1045                                         w->node = pn;
1046                                         w->state = nstate;
1047                                 }
1048                         } else {
1049                                 if (w->root == fn) {
1050                                         w->root = child;
1051                                         RT6_TRACE("W %p adjusted by delroot 2\n", w);
1052                                 }
1053                                 if (w->node == fn) {
1054                                         w->node = child;
1055                                         if (children&2) {
1056                                                 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1057                                                 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
1058                                         } else {
1059                                                 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
1060                                                 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
1061                                         }
1062                                 }
1063                         }
1064                 }
1065                 read_unlock(&fib6_walker_lock);
1066
1067                 node_free(fn);
1068                 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
1069                         return pn;
1070
1071                 rt6_release(pn->leaf);
1072                 pn->leaf = NULL;
1073                 fn = pn;
1074         }
1075 }
1076
1077 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
1078                            struct nl_info *info)
1079 {
1080         struct fib6_walker_t *w;
1081         struct rt6_info *rt = *rtp;
1082
1083         RT6_TRACE("fib6_del_route\n");
1084
1085         /* Unlink it */
1086         *rtp = rt->u.next;
1087         rt->rt6i_node = NULL;
1088         rt6_stats.fib_rt_entries--;
1089         rt6_stats.fib_discarded_routes++;
1090
1091         /* Adjust walkers */
1092         read_lock(&fib6_walker_lock);
1093         FOR_WALKERS(w) {
1094                 if (w->state == FWS_C && w->leaf == rt) {
1095                         RT6_TRACE("walker %p adjusted by delroute\n", w);
1096                         w->leaf = rt->u.next;
1097                         if (w->leaf == NULL)
1098                                 w->state = FWS_U;
1099                 }
1100         }
1101         read_unlock(&fib6_walker_lock);
1102
1103         rt->u.next = NULL;
1104
1105         if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
1106                 fn->leaf = &ip6_null_entry;
1107
1108         /* If it was last route, expunge its radix tree node */
1109         if (fn->leaf == NULL) {
1110                 fn->fn_flags &= ~RTN_RTINFO;
1111                 rt6_stats.fib_route_nodes--;
1112                 fn = fib6_repair_tree(fn);
1113         }
1114
1115         if (atomic_read(&rt->rt6i_ref) != 1) {
1116                 /* This route is used as dummy address holder in some split
1117                  * nodes. It is not leaked, but it still holds other resources,
1118                  * which must be released in time. So, scan ascendant nodes
1119                  * and replace dummy references to this route with references
1120                  * to still alive ones.
1121                  */
1122                 while (fn) {
1123                         if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
1124                                 fn->leaf = fib6_find_prefix(fn);
1125                                 atomic_inc(&fn->leaf->rt6i_ref);
1126                                 rt6_release(rt);
1127                         }
1128                         fn = fn->parent;
1129                 }
1130                 /* No more references are possible at this point. */
1131                 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
1132         }
1133
1134         inet6_rt_notify(RTM_DELROUTE, rt, info);
1135         rt6_release(rt);
1136 }
1137
1138 int fib6_del(struct rt6_info *rt, struct nl_info *info)
1139 {
1140         struct fib6_node *fn = rt->rt6i_node;
1141         struct rt6_info **rtp;
1142
1143 #if RT6_DEBUG >= 2
1144         if (rt->u.dst.obsolete>0) {
1145                 BUG_TRAP(fn==NULL);
1146                 return -ENOENT;
1147         }
1148 #endif
1149         if (fn == NULL || rt == &ip6_null_entry)
1150                 return -ENOENT;
1151
1152         BUG_TRAP(fn->fn_flags&RTN_RTINFO);
1153
1154         if (!(rt->rt6i_flags&RTF_CACHE))
1155                 fib6_prune_clones(fn, rt);
1156
1157         /*
1158          *      Walk the leaf entries looking for ourself
1159          */
1160
1161         for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
1162                 if (*rtp == rt) {
1163                         fib6_del_route(fn, rtp, info);
1164                         return 0;
1165                 }
1166         }
1167         return -ENOENT;
1168 }
1169
1170 /*
1171  *      Tree traversal function.
1172  *
1173  *      Certainly, it is not interrupt safe.
1174  *      However, it is internally reenterable wrt itself and fib6_add/fib6_del.
1175  *      It means, that we can modify tree during walking
1176  *      and use this function for garbage collection, clone pruning,
1177  *      cleaning tree when a device goes down etc. etc. 
1178  *
1179  *      It guarantees that every node will be traversed,
1180  *      and that it will be traversed only once.
1181  *
1182  *      Callback function w->func may return:
1183  *      0 -> continue walking.
1184  *      positive value -> walking is suspended (used by tree dumps,
1185  *      and probably by gc, if it will be split to several slices)
1186  *      negative value -> terminate walking.
1187  *
1188  *      The function itself returns:
1189  *      0   -> walk is complete.
1190  *      >0  -> walk is incomplete (i.e. suspended)
1191  *      <0  -> walk is terminated by an error.
1192  */
1193
1194 static int fib6_walk_continue(struct fib6_walker_t *w)
1195 {
1196         struct fib6_node *fn, *pn;
1197
1198         for (;;) {
1199                 fn = w->node;
1200                 if (fn == NULL)
1201                         return 0;
1202
1203                 if (w->prune && fn != w->root &&
1204                     fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
1205                         w->state = FWS_C;
1206                         w->leaf = fn->leaf;
1207                 }
1208                 switch (w->state) {
1209 #ifdef CONFIG_IPV6_SUBTREES
1210                 case FWS_S:
1211                         if (SUBTREE(fn)) {
1212                                 w->node = SUBTREE(fn);
1213                                 continue;
1214                         }
1215                         w->state = FWS_L;
1216 #endif  
1217                 case FWS_L:
1218                         if (fn->left) {
1219                                 w->node = fn->left;
1220                                 w->state = FWS_INIT;
1221                                 continue;
1222                         }
1223                         w->state = FWS_R;
1224                 case FWS_R:
1225                         if (fn->right) {
1226                                 w->node = fn->right;
1227                                 w->state = FWS_INIT;
1228                                 continue;
1229                         }
1230                         w->state = FWS_C;
1231                         w->leaf = fn->leaf;
1232                 case FWS_C:
1233                         if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1234                                 int err = w->func(w);
1235                                 if (err)
1236                                         return err;
1237                                 continue;
1238                         }
1239                         w->state = FWS_U;
1240                 case FWS_U:
1241                         if (fn == w->root)
1242                                 return 0;
1243                         pn = fn->parent;
1244                         w->node = pn;
1245 #ifdef CONFIG_IPV6_SUBTREES
1246                         if (SUBTREE(pn) == fn) {
1247                                 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1248                                 w->state = FWS_L;
1249                                 continue;
1250                         }
1251 #endif
1252                         if (pn->left == fn) {
1253                                 w->state = FWS_R;
1254                                 continue;
1255                         }
1256                         if (pn->right == fn) {
1257                                 w->state = FWS_C;
1258                                 w->leaf = w->node->leaf;
1259                                 continue;
1260                         }
1261 #if RT6_DEBUG >= 2
1262                         BUG_TRAP(0);
1263 #endif
1264                 }
1265         }
1266 }
1267
1268 static int fib6_walk(struct fib6_walker_t *w)
1269 {
1270         int res;
1271
1272         w->state = FWS_INIT;
1273         w->node = w->root;
1274
1275         fib6_walker_link(w);
1276         res = fib6_walk_continue(w);
1277         if (res <= 0)
1278                 fib6_walker_unlink(w);
1279         return res;
1280 }
1281
1282 static int fib6_clean_node(struct fib6_walker_t *w)
1283 {
1284         int res;
1285         struct rt6_info *rt;
1286         struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1287
1288         for (rt = w->leaf; rt; rt = rt->u.next) {
1289                 res = c->func(rt, c->arg);
1290                 if (res < 0) {
1291                         w->leaf = rt;
1292                         res = fib6_del(rt, NULL);
1293                         if (res) {
1294 #if RT6_DEBUG >= 2
1295                                 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1296 #endif
1297                                 continue;
1298                         }
1299                         return 0;
1300                 }
1301                 BUG_TRAP(res==0);
1302         }
1303         w->leaf = rt;
1304         return 0;
1305 }
1306
1307 /*
1308  *      Convenient frontend to tree walker.
1309  *      
1310  *      func is called on each route.
1311  *              It may return -1 -> delete this route.
1312  *                            0  -> continue walking
1313  *
1314  *      prune==1 -> only immediate children of node (certainly,
1315  *      ignoring pure split nodes) will be scanned.
1316  */
1317
1318 static void fib6_clean_tree(struct fib6_node *root,
1319                             int (*func)(struct rt6_info *, void *arg),
1320                             int prune, void *arg)
1321 {
1322         struct fib6_cleaner_t c;
1323
1324         c.w.root = root;
1325         c.w.func = fib6_clean_node;
1326         c.w.prune = prune;
1327         c.func = func;
1328         c.arg = arg;
1329
1330         fib6_walk(&c.w);
1331 }
1332
1333 void fib6_clean_all(int (*func)(struct rt6_info *, void *arg),
1334                     int prune, void *arg)
1335 {
1336         struct fib6_table *table;
1337         struct hlist_node *node;
1338         unsigned int h;
1339
1340         rcu_read_lock();
1341         for (h = 0; h < FIB_TABLE_HASHSZ; h++) {
1342                 hlist_for_each_entry_rcu(table, node, &fib_table_hash[h],
1343                                          tb6_hlist) {
1344                         write_lock_bh(&table->tb6_lock);
1345                         fib6_clean_tree(&table->tb6_root, func, prune, arg);
1346                         write_unlock_bh(&table->tb6_lock);
1347                 }
1348         }
1349         rcu_read_unlock();
1350 }
1351
1352 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1353 {
1354         if (rt->rt6i_flags & RTF_CACHE) {
1355                 RT6_TRACE("pruning clone %p\n", rt);
1356                 return -1;
1357         }
1358
1359         return 0;
1360 }
1361
1362 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1363 {
1364         fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1365 }
1366
1367 /*
1368  *      Garbage collection
1369  */
1370
1371 static struct fib6_gc_args
1372 {
1373         int                     timeout;
1374         int                     more;
1375 } gc_args;
1376
1377 static int fib6_age(struct rt6_info *rt, void *arg)
1378 {
1379         unsigned long now = jiffies;
1380
1381         /*
1382          *      check addrconf expiration here.
1383          *      Routes are expired even if they are in use.
1384          *
1385          *      Also age clones. Note, that clones are aged out
1386          *      only if they are not in use now.
1387          */
1388
1389         if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1390                 if (time_after(now, rt->rt6i_expires)) {
1391                         RT6_TRACE("expiring %p\n", rt);
1392                         return -1;
1393                 }
1394                 gc_args.more++;
1395         } else if (rt->rt6i_flags & RTF_CACHE) {
1396                 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1397                     time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1398                         RT6_TRACE("aging clone %p\n", rt);
1399                         return -1;
1400                 } else if ((rt->rt6i_flags & RTF_GATEWAY) &&
1401                            (!(rt->rt6i_nexthop->flags & NTF_ROUTER))) {
1402                         RT6_TRACE("purging route %p via non-router but gateway\n",
1403                                   rt);
1404                         return -1;
1405                 }
1406                 gc_args.more++;
1407         }
1408
1409         return 0;
1410 }
1411
1412 static DEFINE_SPINLOCK(fib6_gc_lock);
1413
1414 void fib6_run_gc(unsigned long dummy)
1415 {
1416         if (dummy != ~0UL) {
1417                 spin_lock_bh(&fib6_gc_lock);
1418                 gc_args.timeout = dummy ? (int)dummy : ip6_rt_gc_interval;
1419         } else {
1420                 local_bh_disable();
1421                 if (!spin_trylock(&fib6_gc_lock)) {
1422                         mod_timer(&ip6_fib_timer, jiffies + HZ);
1423                         local_bh_enable();
1424                         return;
1425                 }
1426                 gc_args.timeout = ip6_rt_gc_interval;
1427         }
1428         gc_args.more = 0;
1429
1430         ndisc_dst_gc(&gc_args.more);
1431         fib6_clean_all(fib6_age, 0, NULL);
1432
1433         if (gc_args.more)
1434                 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1435         else {
1436                 del_timer(&ip6_fib_timer);
1437                 ip6_fib_timer.expires = 0;
1438         }
1439         spin_unlock_bh(&fib6_gc_lock);
1440 }
1441
1442 void __init fib6_init(void)
1443 {
1444         fib6_node_kmem = kmem_cache_create("fib6_nodes",
1445                                            sizeof(struct fib6_node),
1446                                            0, SLAB_HWCACHE_ALIGN,
1447                                            NULL, NULL);
1448         if (!fib6_node_kmem)
1449                 panic("cannot create fib6_nodes cache");
1450
1451         fib6_tables_init();
1452 }
1453
1454 void fib6_gc_cleanup(void)
1455 {
1456         del_timer(&ip6_fib_timer);
1457         kmem_cache_destroy(fib6_node_kmem);
1458 }