import of upstream 2.4.34.4 from kernel.org
[linux-2.4.git] / net / ipv6 / ip6_fib.c
1 /*
2  *      Linux INET6 implementation 
3  *      Forwarding Information Database
4  *
5  *      Authors:
6  *      Pedro Roque             <pedro_m@yahoo.com>     
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/config.h>
23 #include <linux/errno.h>
24 #include <linux/types.h>
25 #include <linux/net.h>
26 #include <linux/route.h>
27 #include <linux/netdevice.h>
28 #include <linux/in6.h>
29 #include <linux/init.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 #undef CONFIG_IPV6_SUBTREES
44
45 #if RT6_DEBUG >= 3
46 #define RT6_TRACE(x...) printk(KERN_DEBUG x)
47 #else
48 #define RT6_TRACE(x...) do { ; } while (0)
49 #endif
50
51 struct rt6_statistics   rt6_stats;
52
53 static kmem_cache_t * fib6_node_kmem;
54
55 enum fib_walk_state_t
56 {
57 #ifdef CONFIG_IPV6_SUBTREES
58         FWS_S,
59 #endif
60         FWS_L,
61         FWS_R,
62         FWS_C,
63         FWS_U
64 };
65
66 struct fib6_cleaner_t
67 {
68         struct fib6_walker_t w;
69         int (*func)(struct rt6_info *, void *arg);
70         void *arg;
71 };
72
73 rwlock_t fib6_walker_lock = RW_LOCK_UNLOCKED;
74
75
76 #ifdef CONFIG_IPV6_SUBTREES
77 #define FWS_INIT FWS_S
78 #define SUBTREE(fn) ((fn)->subtree)
79 #else
80 #define FWS_INIT FWS_L
81 #define SUBTREE(fn) NULL
82 #endif
83
84 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
85 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
86
87 /*
88  *      A routing update causes an increase of the serial number on the
89  *      afected 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       = 0;
95
96 static struct timer_list ip6_fib_timer = { function: fib6_run_gc };
97
98 struct fib6_walker_t fib6_walker_list = {
99         &fib6_walker_list, &fib6_walker_list, 
100 };
101
102 #define FOR_WALKERS(w) for ((w)=fib6_walker_list.next; (w) != &fib6_walker_list; (w)=(w)->next)
103
104 static __inline__ u32 fib6_new_sernum(void)
105 {
106         u32 n = ++rt_sernum;
107         if ((__s32)n <= 0)
108                 rt_sernum = n = 1;
109         return n;
110 }
111
112 /*
113  *      Auxiliary address test functions for the radix tree.
114  *
115  *      These assume a 32bit processor (although it will work on 
116  *      64bit processors)
117  */
118
119 /*
120  *      compare "prefix length" bits of an address
121  */
122
123 static __inline__ int addr_match(void *token1, void *token2, int prefixlen)
124 {
125         __u32 *a1 = token1;
126         __u32 *a2 = token2;
127         int pdw;
128         int pbi;
129
130         pdw = prefixlen >> 5;     /* num of whole __u32 in prefix */
131         pbi = prefixlen &  0x1f;  /* num of bits in incomplete u32 in prefix */
132
133         if (pdw)
134                 if (memcmp(a1, a2, pdw << 2))
135                         return 0;
136
137         if (pbi) {
138                 __u32 mask;
139
140                 mask = htonl((0xffffffff) << (32 - pbi));
141
142                 if ((a1[pdw] ^ a2[pdw]) & mask)
143                         return 0;
144         }
145
146         return 1;
147 }
148
149 /*
150  *      test bit
151  */
152
153 static __inline__ int addr_bit_set(void *token, int fn_bit)
154 {
155         __u32 *addr = token;
156
157         return htonl(1 << ((~fn_bit)&0x1F)) & addr[fn_bit>>5];
158 }
159
160 /*
161  *      find the first different bit between two addresses
162  *      length of address must be a multiple of 32bits
163  */
164
165 static __inline__ int addr_diff(void *token1, void *token2, int addrlen)
166 {
167         __u32 *a1 = token1;
168         __u32 *a2 = token2;
169         int i;
170
171         addrlen >>= 2;
172
173         for (i = 0; i < addrlen; i++) {
174                 __u32 xb;
175
176                 xb = a1[i] ^ a2[i];
177
178                 if (xb) {
179                         int j = 31;
180
181                         xb = ntohl(xb);
182
183                         while ((xb & (1 << j)) == 0)
184                                 j--;
185
186                         return (i * 32 + 31 - j);
187                 }
188         }
189
190         /*
191          *      we should *never* get to this point since that 
192          *      would mean the addrs are equal
193          *
194          *      However, we do get to it 8) And exacly, when
195          *      addresses are equal 8)
196          *
197          *      ip route add 1111::/128 via ...
198          *      ip route add 1111::/64 via ...
199          *      and we are here.
200          *
201          *      Ideally, this function should stop comparison
202          *      at prefix length. It does not, but it is still OK,
203          *      if returned value is greater than prefix length.
204          *                                      --ANK (980803)
205          */
206
207         return addrlen<<5;
208 }
209
210 static __inline__ struct fib6_node * node_alloc(void)
211 {
212         struct fib6_node *fn;
213
214         if ((fn = kmem_cache_alloc(fib6_node_kmem, SLAB_ATOMIC)) != NULL)
215                 memset(fn, 0, sizeof(struct fib6_node));
216
217         return fn;
218 }
219
220 static __inline__ void node_free(struct fib6_node * fn)
221 {
222         kmem_cache_free(fib6_node_kmem, fn);
223 }
224
225 static __inline__ void rt6_release(struct rt6_info *rt)
226 {
227         if (atomic_dec_and_test(&rt->rt6i_ref))
228                 dst_free(&rt->u.dst);
229 }
230
231
232 /*
233  *      Routing Table
234  *
235  *      return the apropriate node for a routing tree "add" operation
236  *      by either creating and inserting or by returning an existing
237  *      node.
238  */
239
240 static struct fib6_node * fib6_add_1(struct fib6_node *root, void *addr,
241                                      int addrlen, int plen,
242                                      int offset)
243 {
244         struct fib6_node *fn, *in, *ln;
245         struct fib6_node *pn = NULL;
246         struct rt6key *key;
247         int     bit;
248         int     dir = 0;
249         __u32   sernum = fib6_new_sernum();
250
251         RT6_TRACE("fib6_add_1\n");
252
253         /* insert node in tree */
254
255         fn = root;
256
257         do {
258                 key = (struct rt6key *)((u8 *)fn->leaf + offset);
259
260                 /*
261                  *      Prefix match
262                  */
263                 if (plen < fn->fn_bit ||
264                     !addr_match(&key->addr, addr, fn->fn_bit))
265                         goto insert_above;
266                 
267                 /*
268                  *      Exact match ?
269                  */
270                          
271                 if (plen == fn->fn_bit) {
272                         /* clean up an intermediate node */
273                         if ((fn->fn_flags & RTN_RTINFO) == 0) {
274                                 rt6_release(fn->leaf);
275                                 fn->leaf = NULL;
276                         }
277                         
278                         fn->fn_sernum = sernum;
279                                 
280                         return fn;
281                 }
282
283                 /*
284                  *      We have more bits to go
285                  */
286                          
287                 /* Try to walk down on tree. */
288                 fn->fn_sernum = sernum;
289                 dir = addr_bit_set(addr, fn->fn_bit);
290                 pn = fn;
291                 fn = dir ? fn->right: fn->left;
292         } while (fn);
293
294         /*
295          *      We walked to the bottom of tree.
296          *      Create new leaf node without children.
297          */
298
299         ln = node_alloc();
300
301         if (ln == NULL)
302                 return NULL;
303         ln->fn_bit = plen;
304                         
305         ln->parent = pn;
306         ln->fn_sernum = sernum;
307
308         if (dir)
309                 pn->right = ln;
310         else
311                 pn->left  = ln;
312
313         return ln;
314
315
316 insert_above:
317         /*
318          * split since we don't have a common prefix anymore or 
319          * we have a less significant route.
320          * we've to insert an intermediate node on the list
321          * this new node will point to the one we need to create
322          * and the current
323          */
324
325         pn = fn->parent;
326
327         /* find 1st bit in difference between the 2 addrs.
328
329            See comment in addr_diff: bit may be an invalid value,
330            but if it is >= plen, the value is ignored in any case.
331          */
332         
333         bit = addr_diff(addr, &key->addr, addrlen);
334
335         /* 
336          *              (intermediate)[in]      
337          *                /        \
338          *      (new leaf node)[ln] (old node)[fn]
339          */
340         if (plen > bit) {
341                 in = node_alloc();
342                 ln = node_alloc();
343                 
344                 if (in == NULL || ln == NULL) {
345                         if (in)
346                                 node_free(in);
347                         if (ln)
348                                 node_free(ln);
349                         return NULL;
350                 }
351
352                 /* 
353                  * new intermediate node. 
354                  * RTN_RTINFO will
355                  * be off since that an address that chooses one of
356                  * the branches would not match less specific routes
357                  * in the other branch
358                  */
359
360                 in->fn_bit = bit;
361
362                 in->parent = pn;
363                 in->leaf = fn->leaf;
364                 atomic_inc(&in->leaf->rt6i_ref);
365
366                 in->fn_sernum = sernum;
367
368                 /* update parent pointer */
369                 if (dir)
370                         pn->right = in;
371                 else
372                         pn->left  = in;
373
374                 ln->fn_bit = plen;
375
376                 ln->parent = in;
377                 fn->parent = in;
378
379                 ln->fn_sernum = sernum;
380
381                 if (addr_bit_set(addr, bit)) {
382                         in->right = ln;
383                         in->left  = fn;
384                 } else {
385                         in->left  = ln;
386                         in->right = fn;
387                 }
388         } else { /* plen <= bit */
389
390                 /* 
391                  *              (new leaf node)[ln]
392                  *                /        \
393                  *           (old node)[fn] NULL
394                  */
395
396                 ln = node_alloc();
397
398                 if (ln == NULL)
399                         return NULL;
400
401                 ln->fn_bit = plen;
402
403                 ln->parent = pn;
404
405                 ln->fn_sernum = sernum;
406                 
407                 if (dir)
408                         pn->right = ln;
409                 else
410                         pn->left  = ln;
411
412                 if (addr_bit_set(&key->addr, plen))
413                         ln->right = fn;
414                 else
415                         ln->left  = fn;
416
417                 fn->parent = ln;
418         }
419         return ln;
420 }
421
422 /*
423  *      Insert routing information in a node.
424  */
425
426 static int fib6_add_rt2node(struct fib6_node *fn, struct rt6_info *rt,
427                 struct nlmsghdr *nlh,  struct netlink_skb_parms *req)
428 {
429         struct rt6_info *iter = NULL;
430         struct rt6_info **ins;
431
432         ins = &fn->leaf;
433
434         if (fn->fn_flags&RTN_TL_ROOT &&
435             fn->leaf == &ip6_null_entry &&
436             !(rt->rt6i_flags & (RTF_DEFAULT | RTF_ADDRCONF | RTF_ALLONLINK)) ){
437                 /*
438                  * The top fib of ip6 routing table includes ip6_null_entry.
439                  */
440                 fn->leaf = rt;
441                 rt->u.next = NULL;
442                 goto out;
443         }
444
445         for (iter = fn->leaf; iter; iter=iter->u.next) {
446                 /*
447                  *      Search for duplicates
448                  */
449
450                 if (iter->rt6i_metric == rt->rt6i_metric) {
451                         /*
452                          *      Same priority level
453                          */
454
455                         if ((iter->rt6i_dev == rt->rt6i_dev) &&
456                             (iter->rt6i_flowr == rt->rt6i_flowr) &&
457                             (ipv6_addr_cmp(&iter->rt6i_gateway,
458                                            &rt->rt6i_gateway) == 0)) {
459                                 if (!(iter->rt6i_flags&RTF_EXPIRES))
460                                         return -EEXIST;
461                                 iter->rt6i_expires = rt->rt6i_expires;
462                                 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
463                                         iter->rt6i_flags &= ~RTF_EXPIRES;
464                                         iter->rt6i_expires = 0;
465                                 }
466                                 return -EEXIST;
467                         }
468                 }
469
470                 if (iter->rt6i_metric > rt->rt6i_metric)
471                         break;
472
473                 ins = &iter->u.next;
474         }
475
476         /*
477          *      insert node
478          */
479
480 out:
481         rt->u.next = iter;
482         *ins = rt;
483         rt->rt6i_node = fn;
484         atomic_inc(&rt->rt6i_ref);
485         inet6_rt_notify(RTM_NEWROUTE, rt, nlh, req);
486         rt6_stats.fib_rt_entries++;
487
488         if ((fn->fn_flags & RTN_RTINFO) == 0) {
489                 rt6_stats.fib_route_nodes++;
490                 fn->fn_flags |= RTN_RTINFO;
491         }
492
493         return 0;
494 }
495
496 static __inline__ void fib6_start_gc(struct rt6_info *rt)
497 {
498         if (ip6_fib_timer.expires == 0 &&
499             (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
500                 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
501 }
502
503 /*
504  *      Add routing information to the routing tree.
505  *      <destination addr>/<source addr>
506  *      with source addr info in sub-trees
507  */
508
509 int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nlmsghdr *nlh,
510                 struct netlink_skb_parms *req)
511 {
512         struct fib6_node *fn;
513         int err = -ENOMEM;
514
515         fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
516                         rt->rt6i_dst.plen, (u8*) &rt->rt6i_dst - (u8*) rt);
517
518         if (fn == NULL)
519                 goto out;
520
521 #ifdef CONFIG_IPV6_SUBTREES
522         if (rt->rt6i_src.plen) {
523                 struct fib6_node *sn;
524
525                 if (fn->subtree == NULL) {
526                         struct fib6_node *sfn;
527
528                         /*
529                          * Create subtree.
530                          *
531                          *              fn[main tree]
532                          *              |
533                          *              sfn[subtree root]
534                          *                 \
535                          *                  sn[new leaf node]
536                          */
537
538                         /* Create subtree root node */
539                         sfn = node_alloc();
540                         if (sfn == NULL)
541                                 goto st_failure;
542
543                         sfn->leaf = &ip6_null_entry;
544                         atomic_inc(&ip6_null_entry.rt6i_ref);
545                         sfn->fn_flags = RTN_ROOT;
546                         sfn->fn_sernum = fib6_new_sernum();
547
548                         /* Now add the first leaf node to new subtree */
549
550                         sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
551                                         sizeof(struct in6_addr), rt->rt6i_src.plen,
552                                         (u8*) &rt->rt6i_src - (u8*) rt);
553
554                         if (sn == NULL) {
555                                 /* If it is failed, discard just allocated
556                                    root, and then (in st_failure) stale node
557                                    in main tree.
558                                  */
559                                 node_free(sfn);
560                                 goto st_failure;
561                         }
562
563                         /* Now link new subtree to main tree */
564                         sfn->parent = fn;
565                         fn->subtree = sfn;
566                         if (fn->leaf == NULL) {
567                                 fn->leaf = rt;
568                                 atomic_inc(&rt->rt6i_ref);
569                         }
570                 } else {
571                         sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
572                                         sizeof(struct in6_addr), rt->rt6i_src.plen,
573                                         (u8*) &rt->rt6i_src - (u8*) rt);
574
575                         if (sn == NULL)
576                                 goto st_failure;
577                 }
578
579                 fn = sn;
580         }
581 #endif
582
583         err = fib6_add_rt2node(fn, rt, nlh, req);
584
585         if (err == 0) {
586                 fib6_start_gc(rt);
587                 if (!(rt->rt6i_flags&RTF_CACHE))
588                         fib6_prune_clones(fn, rt);
589         }
590
591 out:
592         if (err)
593                 dst_free(&rt->u.dst);
594         return err;
595
596 #ifdef CONFIG_IPV6_SUBTREES
597         /* Subtree creation failed, probably main tree node
598            is orphan. If it is, shoot it.
599          */
600 st_failure:
601         if (fn && !(fn->fn_flags&RTN_RTINFO|RTN_ROOT))
602                 fib_repair_tree(fn);
603         dst_free(&rt->u.dst);
604         return err;
605 #endif
606 }
607
608 /*
609  *      Routing tree lookup
610  *
611  */
612
613 struct lookup_args {
614         int             offset;         /* key offset on rt6_info       */
615         struct in6_addr *addr;          /* search key                   */
616 };
617
618 static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
619                                         struct lookup_args *args)
620 {
621         struct fib6_node *fn;
622         int dir;
623
624         /*
625          *      Descend on a tree
626          */
627
628         fn = root;
629
630         for (;;) {
631                 struct fib6_node *next;
632
633                 dir = addr_bit_set(args->addr, fn->fn_bit);
634
635                 next = dir ? fn->right : fn->left;
636
637                 if (next) {
638                         fn = next;
639                         continue;
640                 }
641
642                 break;
643         }
644
645         while ((fn->fn_flags & RTN_ROOT) == 0) {
646 #ifdef CONFIG_IPV6_SUBTREES
647                 if (fn->subtree) {
648                         struct fib6_node *st;
649                         struct lookup_args *narg;
650
651                         narg = args + 1;
652
653                         if (narg->addr) {
654                                 st = fib6_lookup_1(fn->subtree, narg);
655
656                                 if (st && !(st->fn_flags & RTN_ROOT))
657                                         return st;
658                         }
659                 }
660 #endif
661
662                 if (fn->fn_flags & RTN_RTINFO) {
663                         struct rt6key *key;
664
665                         key = (struct rt6key *) ((u8 *) fn->leaf +
666                                                  args->offset);
667
668                         if (addr_match(&key->addr, args->addr, key->plen))
669                                 return fn;
670                 }
671
672                 fn = fn->parent;
673         }
674
675         return NULL;
676 }
677
678 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
679                                struct in6_addr *saddr)
680 {
681         struct lookup_args args[2];
682         struct rt6_info *rt = NULL;
683         struct fib6_node *fn;
684
685         args[0].offset = (u8*) &rt->rt6i_dst - (u8*) rt;
686         args[0].addr = daddr;
687
688 #ifdef CONFIG_IPV6_SUBTREES
689         args[1].offset = (u8*) &rt->rt6i_src - (u8*) rt;
690         args[1].addr = saddr;
691 #endif
692
693         fn = fib6_lookup_1(root, args);
694
695         if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
696                 fn = root;
697
698         return fn;
699 }
700
701 /*
702  *      Get node with sepciafied destination prefix (and source prefix,
703  *      if subtrees are used)
704  */
705
706
707 static struct fib6_node * fib6_locate_1(struct fib6_node *root,
708                                         struct in6_addr *addr,
709                                         int plen, int offset)
710 {
711         struct fib6_node *fn;
712
713         for (fn = root; fn ; ) {
714                 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
715
716                 /*
717                  *      Prefix match
718                  */
719                 if (plen < fn->fn_bit ||
720                     !addr_match(&key->addr, addr, fn->fn_bit))
721                         return NULL;
722
723                 if (plen == fn->fn_bit)
724                         return fn;
725
726                 /*
727                  *      We have more bits to go
728                  */
729                 if (addr_bit_set(addr, fn->fn_bit))
730                         fn = fn->right;
731                 else
732                         fn = fn->left;
733         }
734         return NULL;
735 }
736
737 struct fib6_node * fib6_locate(struct fib6_node *root,
738                                struct in6_addr *daddr, int dst_len,
739                                struct in6_addr *saddr, int src_len)
740 {
741         struct rt6_info *rt = NULL;
742         struct fib6_node *fn;
743
744         fn = fib6_locate_1(root, daddr, dst_len,
745                            (u8*) &rt->rt6i_dst - (u8*) rt);
746
747 #ifdef CONFIG_IPV6_SUBTREES
748         if (src_len) {
749                 BUG_TRAP(saddr!=NULL);
750                 if (fn == NULL)
751                         fn = fn->subtree;
752                 if (fn)
753                         fn = fib6_locate_1(fn, saddr, src_len,
754                                            (u8*) &rt->rt6i_src - (u8*) rt);
755         }
756 #endif
757
758         if (fn && fn->fn_flags&RTN_RTINFO)
759                 return fn;
760
761         return NULL;
762 }
763
764
765 /*
766  *      Deletion
767  *
768  */
769
770 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
771 {
772         if (fn->fn_flags&RTN_ROOT)
773                 return &ip6_null_entry;
774
775         while(fn) {
776                 if(fn->left)
777                         return fn->left->leaf;
778
779                 if(fn->right)
780                         return fn->right->leaf;
781
782                 fn = SUBTREE(fn);
783         }
784         return NULL;
785 }
786
787 /*
788  *      Called to trim the tree of intermediate nodes when possible. "fn"
789  *      is the node we want to try and remove.
790  */
791
792 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
793 {
794         int children;
795         int nstate;
796         struct fib6_node *child, *pn;
797         struct fib6_walker_t *w;
798         int iter = 0;
799
800         for (;;) {
801                 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
802                 iter++;
803
804                 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
805                 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
806                 BUG_TRAP(fn->leaf==NULL);
807
808                 children = 0;
809                 child = NULL;
810                 if (fn->right) child = fn->right, children |= 1;
811                 if (fn->left) child = fn->left, children |= 2;
812
813                 if (children == 3 || SUBTREE(fn) 
814 #ifdef CONFIG_IPV6_SUBTREES
815                     /* Subtree root (i.e. fn) may have one child */
816                     || (children && fn->fn_flags&RTN_ROOT)
817 #endif
818                     ) {
819                         fn->leaf = fib6_find_prefix(fn);
820 #if RT6_DEBUG >= 2
821                         if (fn->leaf==NULL) {
822                                 BUG_TRAP(fn->leaf);
823                                 fn->leaf = &ip6_null_entry;
824                         }
825 #endif
826                         atomic_inc(&fn->leaf->rt6i_ref);
827                         return fn->parent;
828                 }
829
830                 pn = fn->parent;
831 #ifdef CONFIG_IPV6_SUBTREES
832                 if (SUBTREE(pn) == fn) {
833                         BUG_TRAP(fn->fn_flags&RTN_ROOT);
834                         SUBTREE(pn) = NULL;
835                         nstate = FWS_L;
836                 } else {
837                         BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
838 #endif
839                         if (pn->right == fn) pn->right = child;
840                         else if (pn->left == fn) pn->left = child;
841 #if RT6_DEBUG >= 2
842                         else BUG_TRAP(0);
843 #endif
844                         if (child)
845                                 child->parent = pn;
846                         nstate = FWS_R;
847 #ifdef CONFIG_IPV6_SUBTREES
848                 }
849 #endif
850
851                 read_lock(&fib6_walker_lock);
852                 FOR_WALKERS(w) {
853                         if (child == NULL) {
854                                 if (w->root == fn) {
855                                         w->root = w->node = NULL;
856                                         RT6_TRACE("W %p adjusted by delroot 1\n", w);
857                                 } else if (w->node == fn) {
858                                         RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
859                                         w->node = pn;
860                                         w->state = nstate;
861                                 }
862                         } else {
863                                 if (w->root == fn) {
864                                         w->root = child;
865                                         RT6_TRACE("W %p adjusted by delroot 2\n", w);
866                                 }
867                                 if (w->node == fn) {
868                                         w->node = child;
869                                         if (children&2) {
870                                                 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
871                                                 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
872                                         } else {
873                                                 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
874                                                 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
875                                         }
876                                 }
877                         }
878                 }
879                 read_unlock(&fib6_walker_lock);
880
881                 node_free(fn);
882                 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
883                         return pn;
884
885                 rt6_release(pn->leaf);
886                 pn->leaf = NULL;
887                 fn = pn;
888         }
889 }
890
891 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
892     struct nlmsghdr *nlh, struct netlink_skb_parms *req)
893 {
894         struct fib6_walker_t *w;
895         struct rt6_info *rt = *rtp;
896
897         RT6_TRACE("fib6_del_route\n");
898
899         /* Unlink it */
900         *rtp = rt->u.next;
901         rt->rt6i_node = NULL;
902         rt6_stats.fib_rt_entries--;
903
904         /* Adjust walkers */
905         read_lock(&fib6_walker_lock);
906         FOR_WALKERS(w) {
907                 if (w->state == FWS_C && w->leaf == rt) {
908                         RT6_TRACE("walker %p adjusted by delroute\n", w);
909                         w->leaf = rt->u.next;
910                         if (w->leaf == NULL)
911                                 w->state = FWS_U;
912                 }
913         }
914         read_unlock(&fib6_walker_lock);
915
916         rt->u.next = NULL;
917
918         if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
919                 fn->leaf = &ip6_null_entry;
920
921         /* If it was last route, expunge its radix tree node */
922         if (fn->leaf == NULL) {
923                 fn->fn_flags &= ~RTN_RTINFO;
924                 rt6_stats.fib_route_nodes--;
925                 fn = fib6_repair_tree(fn);
926         }
927
928         if (atomic_read(&rt->rt6i_ref) != 1) {
929                 /* This route is used as dummy address holder in some split
930                  * nodes. It is not leaked, but it still holds other resources,
931                  * which must be released in time. So, scan ascendant nodes
932                  * and replace dummy references to this route with references
933                  * to still alive ones.
934                  */
935                 while (fn) {
936                         if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
937                                 fn->leaf = fib6_find_prefix(fn);
938                                 atomic_inc(&fn->leaf->rt6i_ref);
939                                 rt6_release(rt);
940                         }
941                         fn = fn->parent;
942                 }
943                 /* No more references are possiible at this point. */
944                 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
945         }
946
947         inet6_rt_notify(RTM_DELROUTE, rt, nlh, req);
948         rt6_release(rt);
949 }
950
951 int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, struct netlink_skb_parms *req)
952 {
953         struct fib6_node *fn = rt->rt6i_node;
954         struct rt6_info **rtp;
955
956 #if RT6_DEBUG >= 2
957         if (rt->u.dst.obsolete>0) {
958                 BUG_TRAP(fn==NULL);
959                 return -ENOENT;
960         }
961 #endif
962         if (fn == NULL || rt == &ip6_null_entry)
963                 return -ENOENT;
964
965         BUG_TRAP(fn->fn_flags&RTN_RTINFO);
966
967         if (!(rt->rt6i_flags&RTF_CACHE))
968                 fib6_prune_clones(fn, rt);
969
970         /*
971          *      Walk the leaf entries looking for ourself
972          */
973
974         for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
975                 if (*rtp == rt) {
976                         fib6_del_route(fn, rtp, nlh, req);
977                         return 0;
978                 }
979         }
980         return -ENOENT;
981 }
982
983 /*
984  *      Tree traversal function.
985  *
986  *      Certainly, it is not interrupt safe.
987  *      However, it is internally reenterable wrt itself and fib6_add/fib6_del.
988  *      It means, that we can modify tree during walking
989  *      and use this function for garbage collection, clone pruning,
990  *      cleaning tree when a device goes down etc. etc. 
991  *
992  *      It guarantees that every node will be traversed,
993  *      and that it will be traversed only once.
994  *
995  *      Callback function w->func may return:
996  *      0 -> continue walking.
997  *      positive value -> walking is suspended (used by tree dumps,
998  *      and probably by gc, if it will be split to several slices)
999  *      negative value -> terminate walking.
1000  *
1001  *      The function itself returns:
1002  *      0   -> walk is complete.
1003  *      >0  -> walk is incomplete (i.e. suspended)
1004  *      <0  -> walk is terminated by an error.
1005  */
1006
1007 int fib6_walk_continue(struct fib6_walker_t *w)
1008 {
1009         struct fib6_node *fn, *pn;
1010
1011         for (;;) {
1012                 fn = w->node;
1013                 if (fn == NULL)
1014                         return 0;
1015
1016                 if (w->prune && fn != w->root &&
1017                     fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
1018                         w->state = FWS_C;
1019                         w->leaf = fn->leaf;
1020                 }
1021                 switch (w->state) {
1022 #ifdef CONFIG_IPV6_SUBTREES
1023                 case FWS_S:
1024                         if (SUBTREE(fn)) {
1025                                 w->node = SUBTREE(fn);
1026                                 continue;
1027                         }
1028                         w->state = FWS_L;
1029 #endif  
1030                 case FWS_L:
1031                         if (fn->left) {
1032                                 w->node = fn->left;
1033                                 w->state = FWS_INIT;
1034                                 continue;
1035                         }
1036                         w->state = FWS_R;
1037                 case FWS_R:
1038                         if (fn->right) {
1039                                 w->node = fn->right;
1040                                 w->state = FWS_INIT;
1041                                 continue;
1042                         }
1043                         w->state = FWS_C;
1044                         w->leaf = fn->leaf;
1045                 case FWS_C:
1046                         if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1047                                 int err = w->func(w);
1048                                 if (err)
1049                                         return err;
1050                                 continue;
1051                         }
1052                         w->state = FWS_U;
1053                 case FWS_U:
1054                         if (fn == w->root)
1055                                 return 0;
1056                         pn = fn->parent;
1057                         w->node = pn;
1058 #ifdef CONFIG_IPV6_SUBTREES
1059                         if (SUBTREE(pn) == fn) {
1060                                 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1061                                 w->state = FWS_L;
1062                                 continue;
1063                         }
1064 #endif
1065                         if (pn->left == fn) {
1066                                 w->state = FWS_R;
1067                                 continue;
1068                         }
1069                         if (pn->right == fn) {
1070                                 w->state = FWS_C;
1071                                 w->leaf = w->node->leaf;
1072                                 continue;
1073                         }
1074 #if RT6_DEBUG >= 2
1075                         BUG_TRAP(0);
1076 #endif
1077                 }
1078         }
1079 }
1080
1081 int fib6_walk(struct fib6_walker_t *w)
1082 {
1083         int res;
1084
1085         w->state = FWS_INIT;
1086         w->node = w->root;
1087
1088         fib6_walker_link(w);
1089         res = fib6_walk_continue(w);
1090         if (res <= 0)
1091                 fib6_walker_unlink(w);
1092         return res;
1093 }
1094
1095 static int fib6_clean_node(struct fib6_walker_t *w)
1096 {
1097         int res;
1098         struct rt6_info *rt;
1099         struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1100
1101         for (rt = w->leaf; rt; rt = rt->u.next) {
1102                 res = c->func(rt, c->arg);
1103                 if (res < 0) {
1104                         w->leaf = rt;
1105                         res = fib6_del(rt, NULL, NULL);
1106                         if (res) {
1107 #if RT6_DEBUG >= 2
1108                                 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1109 #endif
1110                                 continue;
1111                         }
1112                         return 0;
1113                 }
1114                 BUG_TRAP(res==0);
1115         }
1116         w->leaf = rt;
1117         return 0;
1118 }
1119
1120 /*
1121  *      Convenient frontend to tree walker.
1122  *      
1123  *      func is called on each route.
1124  *              It may return -1 -> delete this route.
1125  *                            0  -> continue walking
1126  *
1127  *      prune==1 -> only immediate children of node (certainly,
1128  *      ignoring pure split nodes) will be scanned.
1129  */
1130
1131 void fib6_clean_tree(struct fib6_node *root,
1132                      int (*func)(struct rt6_info *, void *arg),
1133                      int prune, void *arg)
1134 {
1135         struct fib6_cleaner_t c;
1136
1137         c.w.root = root;
1138         c.w.func = fib6_clean_node;
1139         c.w.prune = prune;
1140         c.func = func;
1141         c.arg = arg;
1142
1143         fib6_walk(&c.w);
1144 }
1145
1146 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1147 {
1148         if (rt->rt6i_flags & RTF_CACHE) {
1149                 RT6_TRACE("pruning clone %p\n", rt);
1150                 return -1;
1151         }
1152
1153         return 0;
1154 }
1155
1156 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1157 {
1158         fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1159 }
1160
1161 /*
1162  *      Garbage collection
1163  */
1164
1165 static struct fib6_gc_args
1166 {
1167         int                     timeout;
1168         int                     more;
1169 } gc_args;
1170
1171 static int fib6_age(struct rt6_info *rt, void *arg)
1172 {
1173         unsigned long now = jiffies;
1174
1175         /* Age clones. Note, that clones are aged out
1176            only if they are not in use now.
1177          */
1178
1179         /*
1180          *      check addrconf expiration here.
1181          *      They are expired even if they are in use.
1182          */
1183
1184         if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1185                 if (time_after(now, rt->rt6i_expires)) {
1186                         RT6_TRACE("expiring %p\n", rt);
1187                         return -1;
1188                 }
1189                 gc_args.more++;
1190         } else if (rt->rt6i_flags & RTF_CACHE) {
1191                 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1192                     time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1193                         RT6_TRACE("aging clone %p\n", rt);
1194                         return -1;
1195                 }
1196                 gc_args.more++;
1197         }
1198
1199         return 0;
1200 }
1201
1202 static spinlock_t fib6_gc_lock = SPIN_LOCK_UNLOCKED;
1203
1204 void fib6_run_gc(unsigned long dummy)
1205 {
1206         if (dummy != ~0UL) {
1207                 spin_lock_bh(&fib6_gc_lock);
1208                 gc_args.timeout = (int)dummy;
1209         } else {
1210                 local_bh_disable();
1211                 if (!spin_trylock(&fib6_gc_lock)) {
1212                         mod_timer(&ip6_fib_timer, jiffies + HZ);
1213                         local_bh_enable();
1214                         return;
1215                 }
1216                 gc_args.timeout = ip6_rt_gc_interval;
1217         }
1218         gc_args.more = 0;
1219
1220
1221         write_lock_bh(&rt6_lock);
1222         fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
1223         write_unlock_bh(&rt6_lock);
1224
1225         if (gc_args.more)
1226                 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1227         else {
1228                 del_timer(&ip6_fib_timer);
1229                 ip6_fib_timer.expires = 0;
1230         }
1231         spin_unlock_bh(&fib6_gc_lock);
1232 }
1233
1234 void __init fib6_init(void)
1235 {
1236         if (!fib6_node_kmem)
1237                 fib6_node_kmem = kmem_cache_create("fib6_nodes",
1238                                                    sizeof(struct fib6_node),
1239                                                    0, SLAB_HWCACHE_ALIGN,
1240                                                    NULL, NULL);
1241 }
1242
1243 #ifdef MODULE
1244 void fib6_gc_cleanup(void)
1245 {
1246         del_timer(&ip6_fib_timer);
1247 }
1248 #endif
1249
1250