source: rtems/c/src/exec/libnetworking/net/radix.c @ c52f1c7

4.104.114.84.95
Last change on this file since c52f1c7 was c52f1c7, checked in by Joel Sherrill <joel.sherrill@…>, on 02/27/02 at 22:39:18

2002-02-27 Eric Norum <eric.norum@…>

  • net/radix.c: Properly handle fetching the default route when there is no route. This was a bug in the original FreeBSD code and this fix is from an updated version of their code.
  • Property mode set to 100644
File size: 26.8 KB
Line 
1/*
2 * Copyright (c) 1988, 1989, 1993
3 *      The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by the University of
16 *      California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 *      @(#)radix.c     8.4 (Berkeley) 11/2/94
34 *      $Id$
35 */
36
37/*
38 * Routines to build and maintain radix trees for routing lookups.
39 */
40#ifndef _RADIX_H_
41#include <sys/param.h>
42#ifdef  KERNEL
43#include <sys/systm.h>
44#include <sys/malloc.h>
45#define M_DONTWAIT M_NOWAIT
46#include <sys/domain.h>
47#else
48#include <stdlib.h>
49#endif
50#include <sys/syslog.h>
51#include <net/radix.h>
52#endif
53
54static struct radix_node *
55                rn_lookup __P((void *v_arg, void *m_arg,
56                               struct radix_node_head *head));
57static int      rn_walktree_from __P((struct radix_node_head *h, void *a,
58                                      void *m, walktree_f_t *f, void *w));
59static int rn_walktree __P((struct radix_node_head *, walktree_f_t *, void *));
60static struct radix_node
61         *rn_delete __P((void *, void *, struct radix_node_head *)),
62         *rn_insert __P((void *, struct radix_node_head *, int *,
63                        struct radix_node [2])),
64         *rn_newpair __P((void *, int, struct radix_node[2])),
65         *rn_search __P((void *, struct radix_node *)),
66         *rn_search_m __P((void *, struct radix_node *, void *));
67
68static int      max_keylen;
69static struct radix_mask *rn_mkfreelist;
70static struct radix_node_head *mask_rnhead;
71static char *addmask_key;
72static char normal_chars[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, -1};
73static char *rn_zeros, *rn_ones;
74
75#define rn_masktop (mask_rnhead->rnh_treetop)
76#undef Bcmp
77#define Bcmp(a, b, l) (l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
78
79static int      rn_lexobetter __P((void *m_arg, void *n_arg));
80static struct radix_mask *
81                rn_new_radix_mask __P((struct radix_node *tt,
82                                       struct radix_mask *next));
83static int      rn_satsifies_leaf __P((char *trial, struct radix_node *leaf,
84                                       int skip));
85
86/*
87 * The data structure for the keys is a radix tree with one way
88 * branching removed.  The index rn_b at an internal node n represents a bit
89 * position to be tested.  The tree is arranged so that all descendants
90 * of a node n have keys whose bits all agree up to position rn_b - 1.
91 * (We say the index of n is rn_b.)
92 *
93 * There is at least one descendant which has a one bit at position rn_b,
94 * and at least one with a zero there.
95 *
96 * A route is determined by a pair of key and mask.  We require that the
97 * bit-wise logical and of the key and mask to be the key.
98 * We define the index of a route to associated with the mask to be
99 * the first bit number in the mask where 0 occurs (with bit number 0
100 * representing the highest order bit).
101 *
102 * We say a mask is normal if every bit is 0, past the index of the mask.
103 * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
104 * and m is a normal mask, then the route applies to every descendant of n.
105 * If the index(m) < rn_b, this implies the trailing last few bits of k
106 * before bit b are all 0, (and hence consequently true of every descendant
107 * of n), so the route applies to all descendants of the node as well.
108 *
109 * Similar logic shows that a non-normal mask m such that
110 * index(m) <= index(n) could potentially apply to many children of n.
111 * Thus, for each non-host route, we attach its mask to a list at an internal
112 * node as high in the tree as we can go.
113 *
114 * The present version of the code makes use of normal routes in short-
115 * circuiting an explict mask and compare operation when testing whether
116 * a key satisfies a normal route, and also in remembering the unique leaf
117 * that governs a subtree.
118 */
119
120static struct radix_node *
121rn_search(v_arg, head)
122        void *v_arg;
123        struct radix_node *head;
124{
125        register struct radix_node *x;
126        register caddr_t v;
127
128        for (x = head, v = v_arg; x->rn_b >= 0;) {
129                if (x->rn_bmask & v[x->rn_off])
130                        x = x->rn_r;
131                else
132                        x = x->rn_l;
133        }
134        return (x);
135};
136
137static struct radix_node *
138rn_search_m(v_arg, head, m_arg)
139        struct radix_node *head;
140        void *v_arg, *m_arg;
141{
142        register struct radix_node *x;
143        register caddr_t v = v_arg, m = m_arg;
144
145        for (x = head; x->rn_b >= 0;) {
146                if ((x->rn_bmask & m[x->rn_off]) &&
147                    (x->rn_bmask & v[x->rn_off]))
148                        x = x->rn_r;
149                else
150                        x = x->rn_l;
151        }
152        return x;
153};
154
155int
156rn_refines(m_arg, n_arg)
157        void *m_arg, *n_arg;
158{
159        register caddr_t m = m_arg, n = n_arg;
160        register caddr_t lim, lim2 = lim = n + *(u_char *)n;
161        int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
162        int masks_are_equal = 1;
163
164        if (longer > 0)
165                lim -= longer;
166        while (n < lim) {
167                if (*n & ~(*m))
168                        return 0;
169                if (*n++ != *m++)
170                        masks_are_equal = 0;
171        }
172        while (n < lim2)
173                if (*n++)
174                        return 0;
175        if (masks_are_equal && (longer < 0))
176                for (lim2 = m - longer; m < lim2; )
177                        if (*m++)
178                                return 1;
179        return (!masks_are_equal);
180}
181
182struct radix_node *
183rn_lookup(v_arg, m_arg, head)
184        void *v_arg, *m_arg;
185        struct radix_node_head *head;
186{
187        register struct radix_node *x;
188        caddr_t netmask = 0;
189
190        if (m_arg) {
191                if ((x = rn_addmask(m_arg, 1, head->rnh_treetop->rn_off)) == 0)
192                        return (0);
193                netmask = x->rn_key;
194        }
195        x = rn_match(v_arg, head);
196        if (x && netmask) {
197                while (x && x->rn_mask != netmask)
198                        x = x->rn_dupedkey;
199        }
200        return x;
201}
202
203static int
204rn_satsifies_leaf(trial, leaf, skip)
205        char *trial;
206        register struct radix_node *leaf;
207        int skip;
208{
209        register char *cp = trial, *cp2 = leaf->rn_key, *cp3 = leaf->rn_mask;
210        char *cplim;
211        int length = min(*(u_char *)cp, *(u_char *)cp2);
212
213        if (cp3 == 0)
214                cp3 = rn_ones;
215        else
216                length = min(length, *(u_char *)cp3);
217        cplim = cp + length; cp3 += skip; cp2 += skip;
218        for (cp += skip; cp < cplim; cp++, cp2++, cp3++)
219                if ((*cp ^ *cp2) & *cp3)
220                        return 0;
221        return 1;
222}
223
224struct radix_node *
225rn_match(v_arg, head)
226        void *v_arg;
227        struct radix_node_head *head;
228{
229        caddr_t v = v_arg;
230        register struct radix_node *t = head->rnh_treetop, *x;
231        register caddr_t cp = v, cp2;
232        caddr_t cplim;
233        struct radix_node *saved_t, *top = t;
234        int off = t->rn_off, vlen = *(u_char *)cp, matched_off;
235        register int test, b, rn_b;
236
237        /*
238         * Open code rn_search(v, top) to avoid overhead of extra
239         * subroutine call.
240         */
241        for (; t->rn_b >= 0; ) {
242                if (t->rn_bmask & cp[t->rn_off])
243                        t = t->rn_r;
244                else
245                        t = t->rn_l;
246        }
247        /*
248         * See if we match exactly as a host destination
249         * or at least learn how many bits match, for normal mask finesse.
250         *
251         * It doesn't hurt us to limit how many bytes to check
252         * to the length of the mask, since if it matches we had a genuine
253         * match and the leaf we have is the most specific one anyway;
254         * if it didn't match with a shorter length it would fail
255         * with a long one.  This wins big for class B&C netmasks which
256         * are probably the most common case...
257         */
258        if (t->rn_mask)
259                vlen = *(u_char *)t->rn_mask;
260        cp += off; cp2 = t->rn_key + off; cplim = v + vlen;
261        for (; cp < cplim; cp++, cp2++)
262                if (*cp != *cp2)
263                        goto on1;
264        /*
265         * This extra grot is in case we are explicitly asked
266         * to look up the default.  Ugh!
267         *
268         * Never return the root node itself, it seems to cause a
269         * lot of confusion.
270         */
271        if (t->rn_flags & RNF_ROOT)
272                t = t->rn_dupedkey;
273        return t;
274on1:
275        test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */
276        for (b = 7; (test >>= 1) > 0;)
277                b--;
278        matched_off = cp - v;
279        b += matched_off << 3;
280        rn_b = -1 - b;
281        /*
282         * If there is a host route in a duped-key chain, it will be first.
283         */
284        if ((saved_t = t)->rn_mask == 0)
285                t = t->rn_dupedkey;
286        for (; t; t = t->rn_dupedkey)
287                /*
288                 * Even if we don't match exactly as a host,
289                 * we may match if the leaf we wound up at is
290                 * a route to a net.
291                 */
292                if (t->rn_flags & RNF_NORMAL) {
293                        if (rn_b <= t->rn_b)
294                                return t;
295                } else if (rn_satsifies_leaf(v, t, matched_off))
296                                return t;
297        t = saved_t;
298        /* start searching up the tree */
299        do {
300                register struct radix_mask *m;
301                t = t->rn_p;
302                m = t->rn_mklist;
303                if (m) {
304                        /*
305                         * If non-contiguous masks ever become important
306                         * we can restore the masking and open coding of
307                         * the search and satisfaction test and put the
308                         * calculation of "off" back before the "do".
309                         */
310                        do {
311                                if (m->rm_flags & RNF_NORMAL) {
312                                        if (rn_b <= m->rm_b)
313                                                return (m->rm_leaf);
314                                } else {
315                                        off = min(t->rn_off, matched_off);
316                                        x = rn_search_m(v, t, m->rm_mask);
317                                        while (x && x->rn_mask != m->rm_mask)
318                                                x = x->rn_dupedkey;
319                                        if (x && rn_satsifies_leaf(v, x, off))
320                                                    return x;
321                                }
322                                m = m->rm_mklist;
323                        } while (m);
324                }
325        } while (t != top);
326        return 0;
327};
328
329#ifdef RN_DEBUG
330int     rn_nodenum;
331struct  radix_node *rn_clist;
332int     rn_saveinfo;
333int     rn_debug =  1;
334#endif
335
336static struct radix_node *
337rn_newpair(v, b, nodes)
338        void *v;
339        int b;
340        struct radix_node nodes[2];
341{
342        register struct radix_node *tt = nodes, *t = tt + 1;
343        t->rn_b = b; t->rn_bmask = 0x80 >> (b & 7);
344        t->rn_l = tt; t->rn_off = b >> 3;
345        tt->rn_b = -1; tt->rn_key = (caddr_t)v; tt->rn_p = t;
346        tt->rn_flags = t->rn_flags = RNF_ACTIVE;
347#ifdef RN_DEBUG
348        tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
349        tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
350#endif
351        return t;
352}
353
354static struct radix_node *
355rn_insert(v_arg, head, dupentry, nodes)
356        void *v_arg;
357        struct radix_node_head *head;
358        int *dupentry;
359        struct radix_node nodes[2];
360{
361        caddr_t v = v_arg;
362        struct radix_node *top = head->rnh_treetop;
363        int head_off = top->rn_off, vlen = (int)*((u_char *)v);
364        register struct radix_node *t = rn_search(v_arg, top);
365        register caddr_t cp = v + head_off;
366        register int b;
367        struct radix_node *tt;
368        /*
369         * Find first bit at which v and t->rn_key differ
370         */
371    {
372        register caddr_t cp2 = t->rn_key + head_off;
373        register int cmp_res;
374        caddr_t cplim = v + vlen;
375
376        while (cp < cplim)
377                if (*cp2++ != *cp++)
378                        goto on1;
379        *dupentry = 1;
380        return t;
381on1:
382        *dupentry = 0;
383        cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
384        for (b = (cp - v) << 3; cmp_res; b--)
385                cmp_res >>= 1;
386    }
387    {
388        register struct radix_node *p, *x = top;
389        cp = v;
390        do {
391                p = x;
392                if (cp[x->rn_off] & x->rn_bmask)
393                        x = x->rn_r;
394                else x = x->rn_l;
395        } while (b > (unsigned) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */
396#ifdef RN_DEBUG
397        if (rn_debug)
398                log(LOG_DEBUG, "rn_insert: Going In:\n"), traverse(p);
399#endif
400        t = rn_newpair(v_arg, b, nodes); tt = t->rn_l;
401        if ((cp[p->rn_off] & p->rn_bmask) == 0)
402                p->rn_l = t;
403        else
404                p->rn_r = t;
405        x->rn_p = t; t->rn_p = p; /* frees x, p as temp vars below */
406        if ((cp[t->rn_off] & t->rn_bmask) == 0) {
407                t->rn_r = x;
408        } else {
409                t->rn_r = tt; t->rn_l = x;
410        }
411#ifdef RN_DEBUG
412        if (rn_debug)
413                log(LOG_DEBUG, "rn_insert: Coming Out:\n"), traverse(p);
414#endif
415    }
416        return (tt);
417}
418
419struct radix_node *
420rn_addmask(n_arg, search, skip)
421        int search, skip;
422        void *n_arg;
423{
424        caddr_t netmask = (caddr_t)n_arg;
425        register struct radix_node *x;
426        register caddr_t cp, cplim;
427        register int b = 0, mlen, j;
428        int maskduplicated, m0, isnormal;
429        struct radix_node *saved_x;
430        static int last_zeroed = 0;
431
432        if ((mlen = *(u_char *)netmask) > max_keylen)
433                mlen = max_keylen;
434        if (skip == 0)
435                skip = 1;
436        if (mlen <= skip)
437                return (mask_rnhead->rnh_nodes);
438        if (skip > 1)
439                Bcopy(rn_ones + 1, addmask_key + 1, skip - 1);
440        if ((m0 = mlen) > skip)
441                Bcopy(netmask + skip, addmask_key + skip, mlen - skip);
442        /*
443         * Trim trailing zeroes.
444         */
445        for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0;)
446                cp--;
447        mlen = cp - addmask_key;
448        if (mlen <= skip) {
449                if (m0 >= last_zeroed)
450                        last_zeroed = mlen;
451                return (mask_rnhead->rnh_nodes);
452        }
453        if (m0 < last_zeroed)
454                Bzero(addmask_key + m0, last_zeroed - m0);
455        *addmask_key = last_zeroed = mlen;
456        x = rn_search(addmask_key, rn_masktop);
457        if (Bcmp(addmask_key, x->rn_key, mlen) != 0)
458                x = 0;
459        if (x || search)
460                return (x);
461        R_Malloc(x, struct radix_node *, max_keylen + 2 * sizeof (*x));
462        if ((saved_x = x) == 0)
463                return (0);
464        Bzero(x, max_keylen + 2 * sizeof (*x));
465        netmask = cp = (caddr_t)(x + 2);
466        Bcopy(addmask_key, cp, mlen);
467        x = rn_insert(cp, mask_rnhead, &maskduplicated, x);
468        if (maskduplicated) {
469                log(LOG_ERR, "rn_addmask: mask impossibly already in tree");
470                Free(saved_x);
471                return (x);
472        }
473        /*
474         * Calculate index of mask, and check for normalcy.
475         */
476        cplim = netmask + mlen; isnormal = 1;
477        for (cp = netmask + skip; (cp < cplim) && *(u_char *)cp == 0xff;)
478                cp++;
479        if (cp != cplim) {
480                for (j = 0x80; (j & *cp) != 0; j >>= 1)
481                        b++;
482                if (*cp != normal_chars[b] || cp != (cplim - 1))
483                        isnormal = 0;
484        }
485        b += (cp - netmask) << 3;
486        x->rn_b = -1 - b;
487        if (isnormal)
488                x->rn_flags |= RNF_NORMAL;
489        return (x);
490}
491
492static int      /* XXX: arbitrary ordering for non-contiguous masks */
493rn_lexobetter(m_arg, n_arg)
494        void *m_arg, *n_arg;
495{
496        register u_char *mp = m_arg, *np = n_arg, *lim;
497
498        if (*mp > *np)
499                return 1;  /* not really, but need to check longer one first */
500        if (*mp == *np)
501                for (lim = mp + *mp; mp < lim;)
502                        if (*mp++ > *np++)
503                                return 1;
504        return 0;
505}
506
507static struct radix_mask *
508rn_new_radix_mask(tt, next)
509        register struct radix_node *tt;
510        register struct radix_mask *next;
511{
512        register struct radix_mask *m;
513
514        MKGet(m);
515        if (m == 0) {
516                log(LOG_ERR, "Mask for route not entered\n");
517                return (0);
518        }
519        Bzero(m, sizeof *m);
520        m->rm_b = tt->rn_b;
521        m->rm_flags = tt->rn_flags;
522        if (tt->rn_flags & RNF_NORMAL)
523                m->rm_leaf = tt;
524        else
525                m->rm_mask = tt->rn_mask;
526        m->rm_mklist = next;
527        tt->rn_mklist = m;
528        return m;
529}
530
531struct radix_node *
532rn_addroute(v_arg, n_arg, head, treenodes)
533        void *v_arg, *n_arg;
534        struct radix_node_head *head;
535        struct radix_node treenodes[2];
536{
537        caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
538        register struct radix_node *t, *x = 0, *tt;
539        struct radix_node *saved_tt, *top = head->rnh_treetop;
540        short b = 0, b_leaf = 0;
541        int keyduplicated;
542        caddr_t mmask;
543        struct radix_mask *m, **mp;
544
545        /*
546         * In dealing with non-contiguous masks, there may be
547         * many different routes which have the same mask.
548         * We will find it useful to have a unique pointer to
549         * the mask to speed avoiding duplicate references at
550         * nodes and possibly save time in calculating indices.
551         */
552        if (netmask)  {
553                if ((x = rn_addmask(netmask, 0, top->rn_off)) == 0)
554                        return (0);
555                b_leaf = x->rn_b;
556                b = -1 - x->rn_b;
557                netmask = x->rn_key;
558        }
559        /*
560         * Deal with duplicated keys: attach node to previous instance
561         */
562        saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
563        if (keyduplicated) {
564                for (t = tt; tt; t = tt, tt = tt->rn_dupedkey) {
565                        if (tt->rn_mask == netmask)
566                                return (0);
567                        if (netmask == 0 ||
568                            (tt->rn_mask &&
569                             ((b_leaf < tt->rn_b) || /* index(netmask) > node */
570                               rn_refines(netmask, tt->rn_mask) ||
571                               rn_lexobetter(netmask, tt->rn_mask))))
572                                break;
573                }
574                /*
575                 * If the mask is not duplicated, we wouldn't
576                 * find it among possible duplicate key entries
577                 * anyway, so the above test doesn't hurt.
578                 *
579                 * We sort the masks for a duplicated key the same way as
580                 * in a masklist -- most specific to least specific.
581                 * This may require the unfortunate nuisance of relocating
582                 * the head of the list.
583                 */
584                if (tt == saved_tt) {
585                        struct  radix_node *xx = x;
586                        /* link in at head of list */
587                        (tt = treenodes)->rn_dupedkey = t;
588                        tt->rn_flags = t->rn_flags;
589                        tt->rn_p = x = t->rn_p;
590                        t->rn_p = tt;                           /* parent */
591                        if (x->rn_l == t) x->rn_l = tt; else x->rn_r = tt;
592                        saved_tt = tt; x = xx;
593                } else {
594                        (tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
595                        t->rn_dupedkey = tt;
596                        tt->rn_p = t;                           /* parent */
597                        if (tt->rn_dupedkey)                    /* parent */
598                                tt->rn_dupedkey->rn_p = tt;     /* parent */
599                }
600#ifdef RN_DEBUG
601                t=tt+1; tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
602                tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
603#endif
604                tt->rn_key = (caddr_t) v;
605                tt->rn_b = -1;
606                tt->rn_flags = RNF_ACTIVE;
607        }
608        /*
609         * Put mask in tree.
610         */
611        if (netmask) {
612                tt->rn_mask = netmask;
613                tt->rn_b = x->rn_b;
614                tt->rn_flags |= x->rn_flags & RNF_NORMAL;
615        }
616        t = saved_tt->rn_p;
617        if (keyduplicated)
618                goto on2;
619        b_leaf = -1 - t->rn_b;
620        if (t->rn_r == saved_tt) x = t->rn_l; else x = t->rn_r;
621        /* Promote general routes from below */
622        if (x->rn_b < 0) {
623            for (mp = &t->rn_mklist; x; x = x->rn_dupedkey)
624                if (x->rn_mask && (x->rn_b >= b_leaf) && x->rn_mklist == 0) {
625                        *mp = m = rn_new_radix_mask(x, 0);
626                        if (m)
627                                mp = &m->rm_mklist;
628                }
629        } else if (x->rn_mklist) {
630                /*
631                 * Skip over masks whose index is > that of new node
632                 */
633                for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
634                        if (m->rm_b >= b_leaf)
635                                break;
636                t->rn_mklist = m; *mp = 0;
637        }
638on2:
639        /* Add new route to highest possible ancestor's list */
640        if ((netmask == 0) || (b > t->rn_b ))
641                return tt; /* can't lift at all */
642        b_leaf = tt->rn_b;
643        do {
644                x = t;
645                t = t->rn_p;
646        } while (b <= t->rn_b && x != top);
647        /*
648         * Search through routes associated with node to
649         * insert new route according to index.
650         * Need same criteria as when sorting dupedkeys to avoid
651         * double loop on deletion.
652         */
653        for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist) {
654                if (m->rm_b < b_leaf)
655                        continue;
656                if (m->rm_b > b_leaf)
657                        break;
658                if (m->rm_flags & RNF_NORMAL) {
659                        mmask = m->rm_leaf->rn_mask;
660                        if (tt->rn_flags & RNF_NORMAL) {
661                                log(LOG_ERR,
662                                   "Non-unique normal route, mask not entered");
663                                return tt;
664                        }
665                } else
666                        mmask = m->rm_mask;
667                if (mmask == netmask) {
668                        m->rm_refs++;
669                        tt->rn_mklist = m;
670                        return tt;
671                }
672                if (rn_refines(netmask, mmask) || rn_lexobetter(netmask, mmask))
673                        break;
674        }
675        *mp = rn_new_radix_mask(tt, *mp);
676        return tt;
677}
678
679static struct radix_node *
680rn_delete(v_arg, netmask_arg, head)
681        void *v_arg, *netmask_arg;
682        struct radix_node_head *head;
683{
684        register struct radix_node *t, *p, *x, *tt;
685        struct radix_mask *m, *saved_m, **mp;
686        struct radix_node *dupedkey, *saved_tt, *top;
687        caddr_t v, netmask;
688        int b, head_off, vlen;
689
690        v = v_arg;
691        netmask = netmask_arg;
692        x = head->rnh_treetop;
693        tt = rn_search(v, x);
694        head_off = x->rn_off;
695        vlen =  *(u_char *)v;
696        saved_tt = tt;
697        top = x;
698        if (tt == 0 ||
699            Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
700                return (0);
701        /*
702         * Delete our route from mask lists.
703         */
704        if (netmask) {
705                if ((x = rn_addmask(netmask, 1, head_off)) == 0)
706                        return (0);
707                netmask = x->rn_key;
708                while (tt->rn_mask != netmask)
709                        if ((tt = tt->rn_dupedkey) == 0)
710                                return (0);
711        }
712        if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
713                goto on1;
714        if (tt->rn_flags & RNF_NORMAL) {
715                if (m->rm_leaf != tt || m->rm_refs > 0) {
716                        log(LOG_ERR, "rn_delete: inconsistent annotation\n");
717                        return 0;  /* dangling ref could cause disaster */
718                }
719        } else {
720                if (m->rm_mask != tt->rn_mask) {
721                        log(LOG_ERR, "rn_delete: inconsistent annotation\n");
722                        goto on1;
723                }
724                if (--m->rm_refs >= 0)
725                        goto on1;
726        }
727        b = -1 - tt->rn_b;
728        t = saved_tt->rn_p;
729        if (b > t->rn_b)
730                goto on1; /* Wasn't lifted at all */
731        do {
732                x = t;
733                t = t->rn_p;
734        } while (b <= t->rn_b && x != top);
735        for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
736                if (m == saved_m) {
737                        *mp = m->rm_mklist;
738                        MKFree(m);
739                        break;
740                }
741        if (m == 0) {
742                log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
743                if (tt->rn_flags & RNF_NORMAL)
744                        return (0); /* Dangling ref to us */
745        }
746on1:
747        /*
748         * Eliminate us from tree
749         */
750        if (tt->rn_flags & RNF_ROOT)
751                return (0);
752#ifdef RN_DEBUG
753        /* Get us out of the creation list */
754        for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro) {}
755        if (t) t->rn_ybro = tt->rn_ybro;
756#endif
757        t = tt->rn_p;
758        dupedkey = saved_tt->rn_dupedkey;
759        if (dupedkey) {
760                /*
761                 * at this point, tt is the deletion target and saved_tt
762                 * is the head of the dupekey chain
763                 */
764                if (tt == saved_tt) {
765                        /* remove from head of chain */
766                        x = dupedkey; x->rn_p = t;
767                        if (t->rn_l == tt) t->rn_l = x; else t->rn_r = x;
768                } else {
769                        /* find node in front of tt on the chain */
770                        for (x = p = saved_tt; p && p->rn_dupedkey != tt;)
771                                p = p->rn_dupedkey;
772                        if (p) {
773                                p->rn_dupedkey = tt->rn_dupedkey;
774                                if (tt->rn_dupedkey)               /* parent */
775                                        tt->rn_dupedkey->rn_p = p; /* parent */
776                        } else log(LOG_ERR, "rn_delete: couldn't find us\n");
777                }
778                t = tt + 1;
779                if  (t->rn_flags & RNF_ACTIVE) {
780#ifndef RN_DEBUG
781                        *++x = *t; p = t->rn_p;
782#else
783                        b = t->rn_info; *++x = *t; t->rn_info = b; p = t->rn_p;
784#endif
785                        if (p->rn_l == t) p->rn_l = x; else p->rn_r = x;
786                        x->rn_l->rn_p = x; x->rn_r->rn_p = x;
787                }
788                goto out;
789        }
790        if (t->rn_l == tt) x = t->rn_r; else x = t->rn_l;
791        p = t->rn_p;
792        if (p->rn_r == t) p->rn_r = x; else p->rn_l = x;
793        x->rn_p = p;
794        /*
795         * Demote routes attached to us.
796         */
797        if (t->rn_mklist) {
798                if (x->rn_b >= 0) {
799                        for (mp = &x->rn_mklist; (m = *mp);)
800                                mp = &m->rm_mklist;
801                        *mp = t->rn_mklist;
802                } else {
803                        /* If there are any key,mask pairs in a sibling
804                           duped-key chain, some subset will appear sorted
805                           in the same order attached to our mklist */
806                        for (m = t->rn_mklist; m && x; x = x->rn_dupedkey)
807                                if (m == x->rn_mklist) {
808                                        struct radix_mask *mm = m->rm_mklist;
809                                        x->rn_mklist = 0;
810                                        if (--(m->rm_refs) < 0)
811                                                MKFree(m);
812                                        m = mm;
813                                }
814                        if (m)
815                                log(LOG_ERR, "%s %p at %x\n",
816                                            "rn_delete: Orphaned Mask", m, x);
817                }
818        }
819        /*
820         * We may be holding an active internal node in the tree.
821         */
822        x = tt + 1;
823        if (t != x) {
824#ifndef RN_DEBUG
825                *t = *x;
826#else
827                b = t->rn_info; *t = *x; t->rn_info = b;
828#endif
829                t->rn_l->rn_p = t; t->rn_r->rn_p = t;
830                p = x->rn_p;
831                if (p->rn_l == x) p->rn_l = t; else p->rn_r = t;
832        }
833out:
834        tt->rn_flags &= ~RNF_ACTIVE;
835        tt[1].rn_flags &= ~RNF_ACTIVE;
836        return (tt);
837}
838
839/*
840 * This is the same as rn_walktree() except for the parameters and the
841 * exit.
842 */
843static int
844rn_walktree_from(h, a, m, f, w)
845        struct radix_node_head *h;
846        void *a, *m;
847        walktree_f_t *f;
848        void *w;
849{
850        int error;
851        struct radix_node *base, *next;
852        u_char *xa = (u_char *)a;
853        u_char *xm = (u_char *)m;
854        register struct radix_node *rn, *last = 0 /* shut up gcc */;
855        int stopping = 0;
856        int lastb;
857
858        /*
859         * rn_search_m is sort-of-open-coded here.
860         */
861        /* printf("about to search\n"); */
862        for (rn = h->rnh_treetop; rn->rn_b >= 0; ) {
863                last = rn;
864                /* printf("rn_b %d, rn_bmask %x, xm[rn_off] %x\n",
865                       rn->rn_b, rn->rn_bmask, xm[rn->rn_off]); */
866                if (!(rn->rn_bmask & xm[rn->rn_off])) {
867                        break;
868                }
869                if (rn->rn_bmask & xa[rn->rn_off]) {
870                        rn = rn->rn_r;
871                } else {
872                        rn = rn->rn_l;
873                }
874        }
875        /* printf("done searching\n"); */
876
877        /*
878         * Two cases: either we stepped off the end of our mask,
879         * in which case last == rn, or we reached a leaf, in which
880         * case we want to start from the last node we looked at.
881         * Either way, last is the node we want to start from.
882         */
883        rn = last;
884        lastb = rn->rn_b;
885
886        /* printf("rn %p, lastb %d\n", rn, lastb);*/
887
888        /*
889         * This gets complicated because we may delete the node
890         * while applying the function f to it, so we need to calculate
891         * the successor node in advance.
892         */
893        while (rn->rn_b >= 0)
894                rn = rn->rn_l;
895
896        while (!stopping) {
897                /* printf("node %p (%d)\n", rn, rn->rn_b); */
898                base = rn;
899                /* If at right child go back up, otherwise, go right */
900                while (rn->rn_p->rn_r == rn && !(rn->rn_flags & RNF_ROOT)) {
901                        rn = rn->rn_p;
902
903                        /* if went up beyond last, stop */
904                        if (rn->rn_b < lastb) {
905                                stopping = 1;
906                                /* printf("up too far\n"); */
907                        }
908                }
909
910                /* Find the next *leaf* since next node might vanish, too */
911                for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
912                        rn = rn->rn_l;
913                next = rn;
914                /* Process leaves */
915                while ((rn = base) != 0) {
916                        base = rn->rn_dupedkey;
917                        /* printf("leaf %p\n", rn); */
918                        if (!(rn->rn_flags & RNF_ROOT)
919                            && (error = (*f)(rn, w)))
920                                return (error);
921                }
922                rn = next;
923
924                if (rn->rn_flags & RNF_ROOT) {
925                        /* printf("root, stopping"); */
926                        stopping = 1;
927                }
928
929        }
930        return 0;
931}
932
933static int
934rn_walktree(h, f, w)
935        struct radix_node_head *h;
936        walktree_f_t *f;
937        void *w;
938{
939        int error;
940        struct radix_node *base, *next;
941        register struct radix_node *rn = h->rnh_treetop;
942        /*
943         * This gets complicated because we may delete the node
944         * while applying the function f to it, so we need to calculate
945         * the successor node in advance.
946         */
947        /* First time through node, go left */
948        while (rn->rn_b >= 0)
949                rn = rn->rn_l;
950        for (;;) {
951                base = rn;
952                /* If at right child go back up, otherwise, go right */
953                while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0)
954                        rn = rn->rn_p;
955                /* Find the next *leaf* since next node might vanish, too */
956                for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
957                        rn = rn->rn_l;
958                next = rn;
959                /* Process leaves */
960                while ((rn = base)) {
961                        base = rn->rn_dupedkey;
962                        if (!(rn->rn_flags & RNF_ROOT) && (error = (*f)(rn, w)))
963                                return (error);
964                }
965                rn = next;
966                if (rn->rn_flags & RNF_ROOT)
967                        return (0);
968        }
969        /* NOTREACHED */
970}
971
972int
973rn_inithead(head, off)
974        void **head;
975        int off;
976{
977        register struct radix_node_head *rnh;
978        register struct radix_node *t, *tt, *ttt;
979        if (*head)
980                return (1);
981        R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
982        if (rnh == 0)
983                return (0);
984        Bzero(rnh, sizeof (*rnh));
985        *head = rnh;
986        t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
987        ttt = rnh->rnh_nodes + 2;
988        t->rn_r = ttt;
989        t->rn_p = t;
990        tt = t->rn_l;
991        tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
992        tt->rn_b = -1 - off;
993        *ttt = *tt;
994        ttt->rn_key = rn_ones;
995        rnh->rnh_addaddr = rn_addroute;
996        rnh->rnh_deladdr = rn_delete;
997        rnh->rnh_matchaddr = rn_match;
998        rnh->rnh_lookup = rn_lookup;
999        rnh->rnh_walktree = rn_walktree;
1000        rnh->rnh_walktree_from = rn_walktree_from;
1001        rnh->rnh_treetop = t;
1002        return (1);
1003}
1004
1005void
1006rn_init()
1007{
1008        char *cp, *cplim;
1009#ifdef KERNEL
1010        struct domain *dom;
1011
1012        for (dom = domains; dom; dom = dom->dom_next)
1013                if (dom->dom_maxrtkey > max_keylen)
1014                        max_keylen = dom->dom_maxrtkey;
1015#endif
1016        if (max_keylen == 0) {
1017                log(LOG_ERR,
1018                    "rn_init: radix functions require max_keylen be set\n");
1019                return;
1020        }
1021        R_Malloc(rn_zeros, char *, 3 * max_keylen);
1022        if (rn_zeros == NULL)
1023                panic("rn_init");
1024        Bzero(rn_zeros, 3 * max_keylen);
1025        rn_ones = cp = rn_zeros + max_keylen;
1026        addmask_key = cplim = rn_ones + max_keylen;
1027        while (cp < cplim)
1028                *cp++ = -1;
1029        if (rn_inithead((void **)&mask_rnhead, 0) == 0)
1030                panic("rn_init 2");
1031}
Note: See TracBrowser for help on using the repository browser.