~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv6/ip6_fib.c

Version: ~ [ linux-5.1-rc1 ] ~ [ linux-5.0.3 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.30 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.107 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.164 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.176 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.136 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.63 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.39.4 ] ~ [ linux-2.6.38.8 ] ~ [ linux-2.6.37.6 ] ~ [ linux-2.6.36.4 ] ~ [ linux-2.6.35.14 ] ~ [ linux-2.6.34.15 ] ~ [ linux-2.6.33.20 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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/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 
 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;
 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 rwlock_t fib6_walker_lock = RW_LOCK_UNLOCKED;
 73 
 74 
 75 #ifdef CONFIG_IPV6_SUBTREES
 76 #define FWS_INIT FWS_S
 77 #define SUBTREE(fn) ((fn)->subtree)
 78 #else
 79 #define FWS_INIT FWS_L
 80 #define SUBTREE(fn) NULL
 81 #endif
 82 
 83 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt);
 84 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn);
 85 
 86 /*
 87  *      A routing update causes an increase of the serial number on the
 88  *      afected subtree. This allows for cached routes to be asynchronously
 89  *      tested when modifications are made to the destination cache as a
 90  *      result of redirects, path MTU changes, etc.
 91  */
 92 
 93 static __u32 rt_sernum;
 94 
 95 static struct timer_list ip6_fib_timer = TIMER_INITIALIZER(fib6_run_gc, 0, 0);
 96 
 97 static struct fib6_walker_t fib6_walker_list = {
 98         .prev   = &fib6_walker_list,
 99         .next   = &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 appropriate 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)
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                 fn->leaf = rt;
438                 rt->u.next = NULL;
439                 goto out;
440         }
441 
442         for (iter = fn->leaf; iter; iter=iter->u.next) {
443                 /*
444                  *      Search for duplicates
445                  */
446 
447                 if (iter->rt6i_metric == rt->rt6i_metric) {
448                         /*
449                          *      Same priority level
450                          */
451 
452                         if ((iter->rt6i_dev == rt->rt6i_dev) &&
453                             (ipv6_addr_cmp(&iter->rt6i_gateway,
454                                            &rt->rt6i_gateway) == 0)) {
455                                 if (!(iter->rt6i_flags&RTF_EXPIRES))
456                                         return -EEXIST;
457                                 iter->rt6i_expires = rt->rt6i_expires;
458                                 if (!(rt->rt6i_flags&RTF_EXPIRES)) {
459                                         iter->rt6i_flags &= ~RTF_EXPIRES;
460                                         iter->rt6i_expires = 0;
461                                 }
462                                 return -EEXIST;
463                         }
464                 }
465 
466                 if (iter->rt6i_metric > rt->rt6i_metric)
467                         break;
468 
469                 ins = &iter->u.next;
470         }
471 
472         /*
473          *      insert node
474          */
475 
476 out:
477         rt->u.next = iter;
478         *ins = rt;
479         rt->rt6i_node = fn;
480         atomic_inc(&rt->rt6i_ref);
481         inet6_rt_notify(RTM_NEWROUTE, rt, nlh);
482         rt6_stats.fib_rt_entries++;
483 
484         if ((fn->fn_flags & RTN_RTINFO) == 0) {
485                 rt6_stats.fib_route_nodes++;
486                 fn->fn_flags |= RTN_RTINFO;
487         }
488 
489         return 0;
490 }
491 
492 static __inline__ void fib6_start_gc(struct rt6_info *rt)
493 {
494         if (ip6_fib_timer.expires == 0 &&
495             (rt->rt6i_flags & (RTF_EXPIRES|RTF_CACHE)))
496                 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
497 }
498 
499 void fib6_force_start_gc(void)
500 {
501         if (ip6_fib_timer.expires == 0)
502                 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
503 }
504 
505 /*
506  *      Add routing information to the routing tree.
507  *      <destination addr>/<source addr>
508  *      with source addr info in sub-trees
509  */
510 
511 int fib6_add(struct fib6_node *root, struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr)
512 {
513         struct fib6_node *fn;
514         int err = -ENOMEM;
515 
516         fn = fib6_add_1(root, &rt->rt6i_dst.addr, sizeof(struct in6_addr),
517                         rt->rt6i_dst.plen, (u8*) &rt->rt6i_dst - (u8*) rt);
518 
519         if (fn == NULL)
520                 goto out;
521 
522 #ifdef CONFIG_IPV6_SUBTREES
523         if (rt->rt6i_src.plen) {
524                 struct fib6_node *sn;
525 
526                 if (fn->subtree == NULL) {
527                         struct fib6_node *sfn;
528 
529                         /*
530                          * Create subtree.
531                          *
532                          *              fn[main tree]
533                          *              |
534                          *              sfn[subtree root]
535                          *                 \
536                          *                  sn[new leaf node]
537                          */
538 
539                         /* Create subtree root node */
540                         sfn = node_alloc();
541                         if (sfn == NULL)
542                                 goto st_failure;
543 
544                         sfn->leaf = &ip6_null_entry;
545                         atomic_inc(&ip6_null_entry.rt6i_ref);
546                         sfn->fn_flags = RTN_ROOT;
547                         sfn->fn_sernum = fib6_new_sernum();
548 
549                         /* Now add the first leaf node to new subtree */
550 
551                         sn = fib6_add_1(sfn, &rt->rt6i_src.addr,
552                                         sizeof(struct in6_addr), rt->rt6i_src.plen,
553                                         (u8*) &rt->rt6i_src - (u8*) rt);
554 
555                         if (sn == NULL) {
556                                 /* If it is failed, discard just allocated
557                                    root, and then (in st_failure) stale node
558                                    in main tree.
559                                  */
560                                 node_free(sfn);
561                                 goto st_failure;
562                         }
563 
564                         /* Now link new subtree to main tree */
565                         sfn->parent = fn;
566                         fn->subtree = sfn;
567                         if (fn->leaf == NULL) {
568                                 fn->leaf = rt;
569                                 atomic_inc(&rt->rt6i_ref);
570                         }
571                 } else {
572                         sn = fib6_add_1(fn->subtree, &rt->rt6i_src.addr,
573                                         sizeof(struct in6_addr), rt->rt6i_src.plen,
574                                         (u8*) &rt->rt6i_src - (u8*) rt);
575 
576                         if (sn == NULL)
577                                 goto st_failure;
578                 }
579 
580                 fn = sn;
581         }
582 #endif
583 
584         err = fib6_add_rt2node(fn, rt, nlh);
585 
586         if (err == 0) {
587                 fib6_start_gc(rt);
588                 if (!(rt->rt6i_flags&RTF_CACHE))
589                         fib6_prune_clones(fn, rt);
590         }
591 
592 out:
593         if (err)
594                 dst_free(&rt->u.dst);
595         return err;
596 
597 #ifdef CONFIG_IPV6_SUBTREES
598         /* Subtree creation failed, probably main tree node
599            is orphan. If it is, shoot it.
600          */
601 st_failure:
602         if (fn && !(fn->fn_flags & (RTN_RTINFO|RTN_ROOT)))
603                 fib6_repair_tree(fn);
604         dst_free(&rt->u.dst);
605         return err;
606 #endif
607 }
608 
609 /*
610  *      Routing tree lookup
611  *
612  */
613 
614 struct lookup_args {
615         int             offset;         /* key offset on rt6_info       */
616         struct in6_addr *addr;          /* search key                   */
617 };
618 
619 static struct fib6_node * fib6_lookup_1(struct fib6_node *root,
620                                         struct lookup_args *args)
621 {
622         struct fib6_node *fn;
623         int dir;
624 
625         /*
626          *      Descend on a tree
627          */
628 
629         fn = root;
630 
631         for (;;) {
632                 struct fib6_node *next;
633 
634                 dir = addr_bit_set(args->addr, fn->fn_bit);
635 
636                 next = dir ? fn->right : fn->left;
637 
638                 if (next) {
639                         fn = next;
640                         continue;
641                 }
642 
643                 break;
644         }
645 
646         while ((fn->fn_flags & RTN_ROOT) == 0) {
647 #ifdef CONFIG_IPV6_SUBTREES
648                 if (fn->subtree) {
649                         struct fib6_node *st;
650                         struct lookup_args *narg;
651 
652                         narg = args + 1;
653 
654                         if (narg->addr) {
655                                 st = fib6_lookup_1(fn->subtree, narg);
656 
657                                 if (st && !(st->fn_flags & RTN_ROOT))
658                                         return st;
659                         }
660                 }
661 #endif
662 
663                 if (fn->fn_flags & RTN_RTINFO) {
664                         struct rt6key *key;
665 
666                         key = (struct rt6key *) ((u8 *) fn->leaf +
667                                                  args->offset);
668 
669                         if (addr_match(&key->addr, args->addr, key->plen))
670                                 return fn;
671                 }
672 
673                 fn = fn->parent;
674         }
675 
676         return NULL;
677 }
678 
679 struct fib6_node * fib6_lookup(struct fib6_node *root, struct in6_addr *daddr,
680                                struct in6_addr *saddr)
681 {
682         struct lookup_args args[2];
683         struct rt6_info *rt = NULL;
684         struct fib6_node *fn;
685 
686         args[0].offset = (u8*) &rt->rt6i_dst - (u8*) rt;
687         args[0].addr = daddr;
688 
689 #ifdef CONFIG_IPV6_SUBTREES
690         args[1].offset = (u8*) &rt->rt6i_src - (u8*) rt;
691         args[1].addr = saddr;
692 #endif
693 
694         fn = fib6_lookup_1(root, args);
695 
696         if (fn == NULL || fn->fn_flags & RTN_TL_ROOT)
697                 fn = root;
698 
699         return fn;
700 }
701 
702 /*
703  *      Get node with specified destination prefix (and source prefix,
704  *      if subtrees are used)
705  */
706 
707 
708 static struct fib6_node * fib6_locate_1(struct fib6_node *root,
709                                         struct in6_addr *addr,
710                                         int plen, int offset)
711 {
712         struct fib6_node *fn;
713 
714         for (fn = root; fn ; ) {
715                 struct rt6key *key = (struct rt6key *)((u8 *)fn->leaf + offset);
716 
717                 /*
718                  *      Prefix match
719                  */
720                 if (plen < fn->fn_bit ||
721                     !addr_match(&key->addr, addr, fn->fn_bit))
722                         return NULL;
723 
724                 if (plen == fn->fn_bit)
725                         return fn;
726 
727                 /*
728                  *      We have more bits to go
729                  */
730                 if (addr_bit_set(addr, fn->fn_bit))
731                         fn = fn->right;
732                 else
733                         fn = fn->left;
734         }
735         return NULL;
736 }
737 
738 struct fib6_node * fib6_locate(struct fib6_node *root,
739                                struct in6_addr *daddr, int dst_len,
740                                struct in6_addr *saddr, int src_len)
741 {
742         struct rt6_info *rt = NULL;
743         struct fib6_node *fn;
744 
745         fn = fib6_locate_1(root, daddr, dst_len,
746                            (u8*) &rt->rt6i_dst - (u8*) rt);
747 
748 #ifdef CONFIG_IPV6_SUBTREES
749         if (src_len) {
750                 BUG_TRAP(saddr!=NULL);
751                 if (fn == NULL)
752                         fn = fn->subtree;
753                 if (fn)
754                         fn = fib6_locate_1(fn, saddr, src_len,
755                                            (u8*) &rt->rt6i_src - (u8*) rt);
756         }
757 #endif
758 
759         if (fn && fn->fn_flags&RTN_RTINFO)
760                 return fn;
761 
762         return NULL;
763 }
764 
765 
766 /*
767  *      Deletion
768  *
769  */
770 
771 static struct rt6_info * fib6_find_prefix(struct fib6_node *fn)
772 {
773         if (fn->fn_flags&RTN_ROOT)
774                 return &ip6_null_entry;
775 
776         while(fn) {
777                 if(fn->left)
778                         return fn->left->leaf;
779 
780                 if(fn->right)
781                         return fn->right->leaf;
782 
783                 fn = SUBTREE(fn);
784         }
785         return NULL;
786 }
787 
788 /*
789  *      Called to trim the tree of intermediate nodes when possible. "fn"
790  *      is the node we want to try and remove.
791  */
792 
793 static struct fib6_node * fib6_repair_tree(struct fib6_node *fn)
794 {
795         int children;
796         int nstate;
797         struct fib6_node *child, *pn;
798         struct fib6_walker_t *w;
799         int iter = 0;
800 
801         for (;;) {
802                 RT6_TRACE("fixing tree: plen=%d iter=%d\n", fn->fn_bit, iter);
803                 iter++;
804 
805                 BUG_TRAP(!(fn->fn_flags&RTN_RTINFO));
806                 BUG_TRAP(!(fn->fn_flags&RTN_TL_ROOT));
807                 BUG_TRAP(fn->leaf==NULL);
808 
809                 children = 0;
810                 child = NULL;
811                 if (fn->right) child = fn->right, children |= 1;
812                 if (fn->left) child = fn->left, children |= 2;
813 
814                 if (children == 3 || SUBTREE(fn) 
815 #ifdef CONFIG_IPV6_SUBTREES
816                     /* Subtree root (i.e. fn) may have one child */
817                     || (children && fn->fn_flags&RTN_ROOT)
818 #endif
819                     ) {
820                         fn->leaf = fib6_find_prefix(fn);
821 #if RT6_DEBUG >= 2
822                         if (fn->leaf==NULL) {
823                                 BUG_TRAP(fn->leaf);
824                                 fn->leaf = &ip6_null_entry;
825                         }
826 #endif
827                         atomic_inc(&fn->leaf->rt6i_ref);
828                         return fn->parent;
829                 }
830 
831                 pn = fn->parent;
832 #ifdef CONFIG_IPV6_SUBTREES
833                 if (SUBTREE(pn) == fn) {
834                         BUG_TRAP(fn->fn_flags&RTN_ROOT);
835                         SUBTREE(pn) = NULL;
836                         nstate = FWS_L;
837                 } else {
838                         BUG_TRAP(!(fn->fn_flags&RTN_ROOT));
839 #endif
840                         if (pn->right == fn) pn->right = child;
841                         else if (pn->left == fn) pn->left = child;
842 #if RT6_DEBUG >= 2
843                         else BUG_TRAP(0);
844 #endif
845                         if (child)
846                                 child->parent = pn;
847                         nstate = FWS_R;
848 #ifdef CONFIG_IPV6_SUBTREES
849                 }
850 #endif
851 
852                 read_lock(&fib6_walker_lock);
853                 FOR_WALKERS(w) {
854                         if (child == NULL) {
855                                 if (w->root == fn) {
856                                         w->root = w->node = NULL;
857                                         RT6_TRACE("W %p adjusted by delroot 1\n", w);
858                                 } else if (w->node == fn) {
859                                         RT6_TRACE("W %p adjusted by delnode 1, s=%d/%d\n", w, w->state, nstate);
860                                         w->node = pn;
861                                         w->state = nstate;
862                                 }
863                         } else {
864                                 if (w->root == fn) {
865                                         w->root = child;
866                                         RT6_TRACE("W %p adjusted by delroot 2\n", w);
867                                 }
868                                 if (w->node == fn) {
869                                         w->node = child;
870                                         if (children&2) {
871                                                 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
872                                                 w->state = w->state>=FWS_R ? FWS_U : FWS_INIT;
873                                         } else {
874                                                 RT6_TRACE("W %p adjusted by delnode 2, s=%d\n", w, w->state);
875                                                 w->state = w->state>=FWS_C ? FWS_U : FWS_INIT;
876                                         }
877                                 }
878                         }
879                 }
880                 read_unlock(&fib6_walker_lock);
881 
882                 node_free(fn);
883                 if (pn->fn_flags&RTN_RTINFO || SUBTREE(pn))
884                         return pn;
885 
886                 rt6_release(pn->leaf);
887                 pn->leaf = NULL;
888                 fn = pn;
889         }
890 }
891 
892 static void fib6_del_route(struct fib6_node *fn, struct rt6_info **rtp,
893     struct nlmsghdr *nlh, void *_rtattr)
894 {
895         struct fib6_walker_t *w;
896         struct rt6_info *rt = *rtp;
897 
898         RT6_TRACE("fib6_del_route\n");
899 
900         /* Unlink it */
901         *rtp = rt->u.next;
902         rt->rt6i_node = NULL;
903         rt6_stats.fib_rt_entries--;
904         rt6_stats.fib_discarded_routes++;
905 
906         /* Adjust walkers */
907         read_lock(&fib6_walker_lock);
908         FOR_WALKERS(w) {
909                 if (w->state == FWS_C && w->leaf == rt) {
910                         RT6_TRACE("walker %p adjusted by delroute\n", w);
911                         w->leaf = rt->u.next;
912                         if (w->leaf == NULL)
913                                 w->state = FWS_U;
914                 }
915         }
916         read_unlock(&fib6_walker_lock);
917 
918         rt->u.next = NULL;
919 
920         if (fn->leaf == NULL && fn->fn_flags&RTN_TL_ROOT)
921                 fn->leaf = &ip6_null_entry;
922 
923         /* If it was last route, expunge its radix tree node */
924         if (fn->leaf == NULL) {
925                 fn->fn_flags &= ~RTN_RTINFO;
926                 rt6_stats.fib_route_nodes--;
927                 fn = fib6_repair_tree(fn);
928         }
929 
930         if (atomic_read(&rt->rt6i_ref) != 1) {
931                 /* This route is used as dummy address holder in some split
932                  * nodes. It is not leaked, but it still holds other resources,
933                  * which must be released in time. So, scan ascendant nodes
934                  * and replace dummy references to this route with references
935                  * to still alive ones.
936                  */
937                 while (fn) {
938                         if (!(fn->fn_flags&RTN_RTINFO) && fn->leaf == rt) {
939                                 fn->leaf = fib6_find_prefix(fn);
940                                 atomic_inc(&fn->leaf->rt6i_ref);
941                                 rt6_release(rt);
942                         }
943                         fn = fn->parent;
944                 }
945                 /* No more references are possiible at this point. */
946                 if (atomic_read(&rt->rt6i_ref) != 1) BUG();
947         }
948 
949         inet6_rt_notify(RTM_DELROUTE, rt, nlh);
950         rt6_release(rt);
951 }
952 
953 int fib6_del(struct rt6_info *rt, struct nlmsghdr *nlh, void *_rtattr)
954 {
955         struct fib6_node *fn = rt->rt6i_node;
956         struct rt6_info **rtp;
957 
958 #if RT6_DEBUG >= 2
959         if (rt->u.dst.obsolete>0) {
960                 BUG_TRAP(fn==NULL);
961                 return -ENOENT;
962         }
963 #endif
964         if (fn == NULL || rt == &ip6_null_entry)
965                 return -ENOENT;
966 
967         BUG_TRAP(fn->fn_flags&RTN_RTINFO);
968 
969         if (!(rt->rt6i_flags&RTF_CACHE))
970                 fib6_prune_clones(fn, rt);
971 
972         /*
973          *      Walk the leaf entries looking for ourself
974          */
975 
976         for (rtp = &fn->leaf; *rtp; rtp = &(*rtp)->u.next) {
977                 if (*rtp == rt) {
978                         fib6_del_route(fn, rtp, nlh, _rtattr);
979                         return 0;
980                 }
981         }
982         return -ENOENT;
983 }
984 
985 /*
986  *      Tree traversal function.
987  *
988  *      Certainly, it is not interrupt safe.
989  *      However, it is internally reenterable wrt itself and fib6_add/fib6_del.
990  *      It means, that we can modify tree during walking
991  *      and use this function for garbage collection, clone pruning,
992  *      cleaning tree when a device goes down etc. etc. 
993  *
994  *      It guarantees that every node will be traversed,
995  *      and that it will be traversed only once.
996  *
997  *      Callback function w->func may return:
998  *      0 -> continue walking.
999  *      positive value -> walking is suspended (used by tree dumps,
1000  *      and probably by gc, if it will be split to several slices)
1001  *      negative value -> terminate walking.
1002  *
1003  *      The function itself returns:
1004  *      0   -> walk is complete.
1005  *      >0  -> walk is incomplete (i.e. suspended)
1006  *      <0  -> walk is terminated by an error.
1007  */
1008 
1009 int fib6_walk_continue(struct fib6_walker_t *w)
1010 {
1011         struct fib6_node *fn, *pn;
1012 
1013         for (;;) {
1014                 fn = w->node;
1015                 if (fn == NULL)
1016                         return 0;
1017 
1018                 if (w->prune && fn != w->root &&
1019                     fn->fn_flags&RTN_RTINFO && w->state < FWS_C) {
1020                         w->state = FWS_C;
1021                         w->leaf = fn->leaf;
1022                 }
1023                 switch (w->state) {
1024 #ifdef CONFIG_IPV6_SUBTREES
1025                 case FWS_S:
1026                         if (SUBTREE(fn)) {
1027                                 w->node = SUBTREE(fn);
1028                                 continue;
1029                         }
1030                         w->state = FWS_L;
1031 #endif  
1032                 case FWS_L:
1033                         if (fn->left) {
1034                                 w->node = fn->left;
1035                                 w->state = FWS_INIT;
1036                                 continue;
1037                         }
1038                         w->state = FWS_R;
1039                 case FWS_R:
1040                         if (fn->right) {
1041                                 w->node = fn->right;
1042                                 w->state = FWS_INIT;
1043                                 continue;
1044                         }
1045                         w->state = FWS_C;
1046                         w->leaf = fn->leaf;
1047                 case FWS_C:
1048                         if (w->leaf && fn->fn_flags&RTN_RTINFO) {
1049                                 int err = w->func(w);
1050                                 if (err)
1051                                         return err;
1052                                 continue;
1053                         }
1054                         w->state = FWS_U;
1055                 case FWS_U:
1056                         if (fn == w->root)
1057                                 return 0;
1058                         pn = fn->parent;
1059                         w->node = pn;
1060 #ifdef CONFIG_IPV6_SUBTREES
1061                         if (SUBTREE(pn) == fn) {
1062                                 BUG_TRAP(fn->fn_flags&RTN_ROOT);
1063                                 w->state = FWS_L;
1064                                 continue;
1065                         }
1066 #endif
1067                         if (pn->left == fn) {
1068                                 w->state = FWS_R;
1069                                 continue;
1070                         }
1071                         if (pn->right == fn) {
1072                                 w->state = FWS_C;
1073                                 w->leaf = w->node->leaf;
1074                                 continue;
1075                         }
1076 #if RT6_DEBUG >= 2
1077                         BUG_TRAP(0);
1078 #endif
1079                 }
1080         }
1081 }
1082 
1083 int fib6_walk(struct fib6_walker_t *w)
1084 {
1085         int res;
1086 
1087         w->state = FWS_INIT;
1088         w->node = w->root;
1089 
1090         fib6_walker_link(w);
1091         res = fib6_walk_continue(w);
1092         if (res <= 0)
1093                 fib6_walker_unlink(w);
1094         return res;
1095 }
1096 
1097 static int fib6_clean_node(struct fib6_walker_t *w)
1098 {
1099         int res;
1100         struct rt6_info *rt;
1101         struct fib6_cleaner_t *c = (struct fib6_cleaner_t*)w;
1102 
1103         for (rt = w->leaf; rt; rt = rt->u.next) {
1104                 res = c->func(rt, c->arg);
1105                 if (res < 0) {
1106                         w->leaf = rt;
1107                         res = fib6_del(rt, NULL, NULL);
1108                         if (res) {
1109 #if RT6_DEBUG >= 2
1110                                 printk(KERN_DEBUG "fib6_clean_node: del failed: rt=%p@%p err=%d\n", rt, rt->rt6i_node, res);
1111 #endif
1112                                 continue;
1113                         }
1114                         return 0;
1115                 }
1116                 BUG_TRAP(res==0);
1117         }
1118         w->leaf = rt;
1119         return 0;
1120 }
1121 
1122 /*
1123  *      Convenient frontend to tree walker.
1124  *      
1125  *      func is called on each route.
1126  *              It may return -1 -> delete this route.
1127  *                            0  -> continue walking
1128  *
1129  *      prune==1 -> only immediate children of node (certainly,
1130  *      ignoring pure split nodes) will be scanned.
1131  */
1132 
1133 void fib6_clean_tree(struct fib6_node *root,
1134                      int (*func)(struct rt6_info *, void *arg),
1135                      int prune, void *arg)
1136 {
1137         struct fib6_cleaner_t c;
1138 
1139         c.w.root = root;
1140         c.w.func = fib6_clean_node;
1141         c.w.prune = prune;
1142         c.func = func;
1143         c.arg = arg;
1144 
1145         fib6_walk(&c.w);
1146 }
1147 
1148 static int fib6_prune_clone(struct rt6_info *rt, void *arg)
1149 {
1150         if (rt->rt6i_flags & RTF_CACHE) {
1151                 RT6_TRACE("pruning clone %p\n", rt);
1152                 return -1;
1153         }
1154 
1155         return 0;
1156 }
1157 
1158 static void fib6_prune_clones(struct fib6_node *fn, struct rt6_info *rt)
1159 {
1160         fib6_clean_tree(fn, fib6_prune_clone, 1, rt);
1161 }
1162 
1163 /*
1164  *      Garbage collection
1165  */
1166 
1167 static struct fib6_gc_args
1168 {
1169         int                     timeout;
1170         int                     more;
1171 } gc_args;
1172 
1173 static int fib6_age(struct rt6_info *rt, void *arg)
1174 {
1175         unsigned long now = jiffies;
1176 
1177         /*
1178          *      check addrconf expiration here.
1179          *      Routes are expired even if they are in use.
1180          *
1181          *      Also age clones. Note, that clones are aged out
1182          *      only if they are not in use now.
1183          */
1184 
1185         if (rt->rt6i_flags&RTF_EXPIRES && rt->rt6i_expires) {
1186                 if (time_after(now, rt->rt6i_expires)) {
1187                         RT6_TRACE("expiring %p\n", rt);
1188                         return -1;
1189                 }
1190                 gc_args.more++;
1191         } else if (rt->rt6i_flags & RTF_CACHE) {
1192                 if (atomic_read(&rt->u.dst.__refcnt) == 0 &&
1193                     time_after_eq(now, rt->u.dst.lastuse + gc_args.timeout)) {
1194                         RT6_TRACE("aging clone %p\n", rt);
1195                         return -1;
1196                 }
1197                 gc_args.more++;
1198         }
1199 
1200         return 0;
1201 }
1202 
1203 static spinlock_t fib6_gc_lock = SPIN_LOCK_UNLOCKED;
1204 
1205 void fib6_run_gc(unsigned long dummy)
1206 {
1207         if (dummy != ~0UL) {
1208                 spin_lock_bh(&fib6_gc_lock);
1209                 gc_args.timeout = (int)dummy;
1210         } else {
1211                 local_bh_disable();
1212                 if (!spin_trylock(&fib6_gc_lock)) {
1213                         mod_timer(&ip6_fib_timer, jiffies + HZ);
1214                         local_bh_enable();
1215                         return;
1216                 }
1217                 gc_args.timeout = ip6_rt_gc_interval;
1218         }
1219         gc_args.more = 0;
1220 
1221 
1222         write_lock_bh(&rt6_lock);
1223         ndisc_dst_gc(&gc_args.more);
1224         fib6_clean_tree(&ip6_routing_table, fib6_age, 0, NULL);
1225         write_unlock_bh(&rt6_lock);
1226 
1227         if (gc_args.more)
1228                 mod_timer(&ip6_fib_timer, jiffies + ip6_rt_gc_interval);
1229         else {
1230                 del_timer(&ip6_fib_timer);
1231                 ip6_fib_timer.expires = 0;
1232         }
1233         spin_unlock_bh(&fib6_gc_lock);
1234 }
1235 
1236 void __init fib6_init(void)
1237 {
1238         fib6_node_kmem = kmem_cache_create("fib6_nodes",
1239                                            sizeof(struct fib6_node),
1240                                            0, SLAB_HWCACHE_ALIGN,
1241                                            NULL, NULL);
1242 }
1243 
1244 #ifdef MODULE
1245 void fib6_gc_cleanup(void)
1246 {
1247         del_timer(&ip6_fib_timer);
1248         kmem_cache_destroy(fib6_node_kmem);
1249 }
1250 #endif
1251 
1252 
1253 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp