]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - radix.c
In preparation for the upcoming ipfilter vendor code import,
[FreeBSD/FreeBSD.git] / radix.c
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  *
14  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *      @(#)radix.c     8.6 (Berkeley) 10/17/95
27  */
28
29 /*
30  * Routines to build and maintain radix trees for routing lookups.
31  */
32 #if defined(KERNEL) || defined(_KERNEL)
33 # undef KERNEL
34 # undef _KERNEL
35 # define        KERNEL  1
36 # define        _KERNEL 1
37 #endif
38 #define __SYS_ATOMIC_OPS_H__
39 #if !defined(__svr4__) && !defined(__SVR4) && !defined(__osf__) && \
40     !defined(__hpux) && !defined(__sgi)
41 #include <sys/cdefs.h>
42 #endif
43 #ifndef __P
44 # ifdef __STDC__
45 #  define       __P(x)  x
46 # else
47 #  define       __P(x)  ()
48 # endif
49 #endif
50 #ifdef __osf__
51 # define CONST
52 # define _IPV6_SWTAB_H
53 # define _PROTO_NET_H_
54 # define _PROTO_IPV6_H
55 # include <sys/malloc.h>
56 #endif
57
58 #include <sys/param.h>
59 #ifdef  _KERNEL
60 #include <sys/systm.h>
61 #else
62 void panic __P((char *str));
63 #include <stdlib.h>
64 #include <stdio.h>
65 #include <stdarg.h>
66 #include <string.h>
67 #endif
68 #ifdef __hpux
69 #include <syslog.h>
70 #else
71 #include <sys/syslog.h>
72 #endif
73 #include <sys/time.h>
74 #include <netinet/in.h>
75 #include <sys/socket.h>
76 #include <net/if.h>
77 #ifdef SOLARIS2
78 # define _RADIX_H_
79 #endif
80 #include "netinet/ip_compat.h"
81 #include "netinet/ip_fil.h"
82 #ifdef SOLARIS2
83 # undef _RADIX_H_
84 #endif
85 /* END OF INCLUDES */
86 #include "radix_ipf.h"
87 #ifndef min
88 # define        min     MIN
89 #endif
90 #ifndef max
91 # define        max     MAX
92 #endif
93
94 int     max_keylen = 16;
95 static struct radix_mask *rn_mkfreelist;
96 static struct radix_node_head *mask_rnhead;
97 static char *addmask_key;
98 static u_char normal_chars[] = {0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
99 static char *rn_zeros = NULL, *rn_ones = NULL;
100
101 #define rn_masktop (mask_rnhead->rnh_treetop)
102 #undef Bcmp
103 #define Bcmp(a, b, l) (l == 0 ? 0 : bcmp((caddr_t)(a), (caddr_t)(b), (u_long)l))
104
105 static int rn_satisfies_leaf __P((char *, struct radix_node *, int));
106 static int rn_lexobetter __P((void *, void *));
107 static struct radix_mask *rn_new_radix_mask __P((struct radix_node *,
108     struct radix_mask *));
109 static int rn_freenode __P((struct radix_node *, void *));
110 #if defined(AIX) && !defined(_KERNEL)
111 struct radix_node *rn_match __P((void *, struct radix_node_head *));
112 struct radix_node *rn_addmask __P((int, int, void *));
113 #define FreeS(x, y)     KFREES(x, y)
114 #define Bcopy(x, y, z)  bcopy(x, y, z)
115 #endif
116
117 /*
118  * The data structure for the keys is a radix tree with one way
119  * branching removed.  The index rn_b at an internal node n represents a bit
120  * position to be tested.  The tree is arranged so that all descendants
121  * of a node n have keys whose bits all agree up to position rn_b - 1.
122  * (We say the index of n is rn_b.)
123  *
124  * There is at least one descendant which has a one bit at position rn_b,
125  * and at least one with a zero there.
126  *
127  * A route is determined by a pair of key and mask.  We require that the
128  * bit-wise logical and of the key and mask to be the key.
129  * We define the index of a route to associated with the mask to be
130  * the first bit number in the mask where 0 occurs (with bit number 0
131  * representing the highest order bit).
132  *
133  * We say a mask is normal if every bit is 0, past the index of the mask.
134  * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
135  * and m is a normal mask, then the route applies to every descendant of n.
136  * If the index(m) < rn_b, this implies the trailing last few bits of k
137  * before bit b are all 0, (and hence consequently true of every descendant
138  * of n), so the route applies to all descendants of the node as well.
139  *
140  * Similar logic shows that a non-normal mask m such that
141  * index(m) <= index(n) could potentially apply to many children of n.
142  * Thus, for each non-host route, we attach its mask to a list at an internal
143  * node as high in the tree as we can go.
144  *
145  * The present version of the code makes use of normal routes in short-
146  * circuiting an explicit mask and compare operation when testing whether
147  * a key satisfies a normal route, and also in remembering the unique leaf
148  * that governs a subtree.
149  */
150
151 struct radix_node *
152 rn_search(v_arg, head)
153         void *v_arg;
154         struct radix_node *head;
155 {
156         struct radix_node *x;
157         caddr_t v;
158
159         for (x = head, v = v_arg; x->rn_b >= 0;) {
160                 if (x->rn_bmask & v[x->rn_off])
161                         x = x->rn_r;
162                 else
163                         x = x->rn_l;
164         }
165         return (x);
166 }
167
168 struct radix_node *
169 rn_search_m(v_arg, head, m_arg)
170         struct radix_node *head;
171         void *v_arg, *m_arg;
172 {
173         struct radix_node *x;
174         caddr_t v = v_arg, m = m_arg;
175
176         for (x = head; x->rn_b >= 0;) {
177                 if ((x->rn_bmask & m[x->rn_off]) &&
178                     (x->rn_bmask & v[x->rn_off]))
179                         x = x->rn_r;
180                 else
181                         x = x->rn_l;
182         }
183         return x;
184 }
185
186 int
187 rn_refines(m_arg, n_arg)
188         void *m_arg, *n_arg;
189 {
190         caddr_t m = m_arg, n = n_arg;
191         caddr_t lim, lim2 = lim = n + *(u_char *)n;
192         int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
193         int masks_are_equal = 1;
194
195         if (longer > 0)
196                 lim -= longer;
197         while (n < lim) {
198                 if (*n & ~(*m))
199                         return 0;
200                 if (*n++ != *m++)
201                         masks_are_equal = 0;
202         }
203         while (n < lim2)
204                 if (*n++)
205                         return 0;
206         if (masks_are_equal && (longer < 0))
207                 for (lim2 = m - longer; m < lim2; )
208                         if (*m++)
209                                 return 1;
210         return (!masks_are_equal);
211 }
212
213 struct radix_node *
214 rn_lookup(v_arg, m_arg, head)
215         void *v_arg, *m_arg;
216         struct radix_node_head *head;
217 {
218         struct radix_node *x;
219         caddr_t netmask = 0;
220
221         if (m_arg) {
222                 if ((x = rn_addmask(m_arg, 1, head->rnh_treetop->rn_off)) == 0)
223                         return (0);
224                 netmask = x->rn_key;
225         }
226         x = rn_match(v_arg, head);
227         if (x && netmask) {
228                 while (x && x->rn_mask != netmask)
229                         x = x->rn_dupedkey;
230         }
231         return x;
232 }
233
234 static int
235 rn_satisfies_leaf(trial, leaf, skip)
236         char *trial;
237         struct radix_node *leaf;
238         int skip;
239 {
240         char *cp = trial, *cp2 = leaf->rn_key, *cp3 = leaf->rn_mask;
241         char *cplim;
242         int length = min(*(u_char *)cp, *(u_char *)cp2);
243
244         if (cp3 == 0)
245                 cp3 = rn_ones;
246         else
247                 length = min(length, *(u_char *)cp3);
248         cplim = cp + length;
249         cp3 += skip;
250         cp2 += skip;
251         for (cp += skip; cp < cplim; cp++, cp2++, cp3++)
252                 if ((*cp ^ *cp2) & *cp3)
253                         return 0;
254         return 1;
255 }
256
257 struct radix_node *
258 rn_match(v_arg, head)
259         void *v_arg;
260         struct radix_node_head *head;
261 {
262         caddr_t v = v_arg;
263         struct radix_node *t = head->rnh_treetop, *x;
264         caddr_t cp = v, cp2;
265         caddr_t cplim;
266         struct radix_node *saved_t, *top = t;
267         int off = t->rn_off, vlen = *(u_char *)cp, matched_off;
268         int test, b, rn_b;
269
270         /*
271          * Open code rn_search(v, top) to avoid overhead of extra
272          * subroutine call.
273          */
274         for (; t->rn_b >= 0; ) {
275                 if (t->rn_bmask & cp[t->rn_off])
276                         t = t->rn_r;
277                 else
278                         t = t->rn_l;
279         }
280         /*
281          * See if we match exactly as a host destination
282          * or at least learn how many bits match, for normal mask finesse.
283          *
284          * It doesn't hurt us to limit how many bytes to check
285          * to the length of the mask, since if it matches we had a genuine
286          * match and the leaf we have is the most specific one anyway;
287          * if it didn't match with a shorter length it would fail
288          * with a long one.  This wins big for class B&C netmasks which
289          * are probably the most common case...
290          */
291         if (t->rn_mask)
292                 vlen = *(u_char *)t->rn_mask;
293         cp += off;
294         cp2 = t->rn_key + off;
295         cplim = v + vlen;
296         for (; cp < cplim; cp++, cp2++)
297                 if (*cp != *cp2)
298                         goto on1;
299         /*
300          * This extra grot is in case we are explicitly asked
301          * to look up the default.  Ugh!
302          */
303         if ((t->rn_flags & RNF_ROOT) && t->rn_dupedkey)
304                 t = t->rn_dupedkey;
305         return t;
306 on1:
307         test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */
308         for (b = 7; (test >>= 1) > 0;)
309                 b--;
310         matched_off = cp - v;
311         b += matched_off << 3;
312         rn_b = -1 - b;
313         /*
314          * If there is a host route in a duped-key chain, it will be first.
315          */
316         if ((saved_t = t)->rn_mask == 0)
317                 t = t->rn_dupedkey;
318         for (; t; t = t->rn_dupedkey)
319                 /*
320                  * Even if we don't match exactly as a host,
321                  * we may match if the leaf we wound up at is
322                  * a route to a net.
323                  */
324                 if (t->rn_flags & RNF_NORMAL) {
325                         if (rn_b <= t->rn_b)
326                                 return t;
327                 } else if (rn_satisfies_leaf(v, t, matched_off))
328                                 return t;
329         t = saved_t;
330         /* start searching up the tree */
331         do {
332                 struct radix_mask *m;
333                 t = t->rn_p;
334                 m = t->rn_mklist;
335                 if (m) {
336                         /*
337                          * If non-contiguous masks ever become important
338                          * we can restore the masking and open coding of
339                          * the search and satisfaction test and put the
340                          * calculation of "off" back before the "do".
341                          */
342                         do {
343                                 if (m->rm_flags & RNF_NORMAL) {
344                                         if (rn_b <= m->rm_b)
345                                                 return (m->rm_leaf);
346                                 } else {
347                                         off = min(t->rn_off, matched_off);
348                                         x = rn_search_m(v, t, m->rm_mask);
349                                         while (x && x->rn_mask != m->rm_mask)
350                                                 x = x->rn_dupedkey;
351                                         if (x && rn_satisfies_leaf(v, x, off))
352                                                 return x;
353                                 }
354                                 m = m->rm_mklist;
355                         } while (m);
356                 }
357         } while (t != top);
358         return 0;
359 }
360
361 #ifdef RN_DEBUG
362 int     rn_nodenum;
363 struct  radix_node *rn_clist;
364 int     rn_saveinfo;
365 int     rn_debug =  1;
366 #endif
367
368 struct radix_node *
369 rn_newpair(v, b, nodes)
370         void *v;
371         int b;
372         struct radix_node nodes[2];
373 {
374         struct radix_node *tt = nodes, *t = tt + 1;
375         t->rn_b = b;
376         t->rn_bmask = 0x80 >> (b & 7);
377         t->rn_l = tt;
378         t->rn_off = b >> 3;
379         tt->rn_b = -1;
380         tt->rn_key = (caddr_t)v;
381         tt->rn_p = t;
382         tt->rn_flags = t->rn_flags = RNF_ACTIVE;
383 #ifdef RN_DEBUG
384         tt->rn_info = rn_nodenum++;
385         t->rn_info = rn_nodenum++;
386         tt->rn_twin = t;
387         tt->rn_ybro = rn_clist;
388         rn_clist = tt;
389 #endif
390         return t;
391 }
392
393 struct radix_node *
394 rn_insert(v_arg, head, dupentry, nodes)
395         void *v_arg;
396         struct radix_node_head *head;
397         int *dupentry;
398         struct radix_node nodes[2];
399 {
400         caddr_t v = v_arg;
401         struct radix_node *top = head->rnh_treetop;
402         int head_off = top->rn_off, vlen = (int)*((u_char *)v);
403         struct radix_node *t = rn_search(v_arg, top);
404         caddr_t cp = v + head_off;
405         int b;
406         struct radix_node *tt;
407
408 #ifdef  RN_DEBUG
409         if (rn_debug)
410                 log(LOG_DEBUG, "rn_insert(%p,%p,%p,%p)\n", v_arg, head, dupentry, nodes);
411 #endif
412         /*
413          * Find first bit at which v and t->rn_key differ
414          */
415     {
416         caddr_t cp2 = t->rn_key + head_off;
417         int cmp_res;
418         caddr_t cplim = v + vlen;
419
420         while (cp < cplim)
421                 if (*cp2++ != *cp++)
422                         goto on1;
423         *dupentry = 1;
424         return t;
425 on1:
426         *dupentry = 0;
427         cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
428         for (b = (cp - v) << 3; cmp_res; b--)
429                 cmp_res >>= 1;
430     }
431     {
432         struct radix_node *p, *x = top;
433         cp = v;
434         do {
435                 p = x;
436                 if (cp[x->rn_off] & x->rn_bmask)
437                         x = x->rn_r;
438                 else
439                         x = x->rn_l;
440         } while (b > (unsigned) x->rn_b); /* x->rn_b < b && x->rn_b >= 0 */
441 #ifdef RN_DEBUG
442         if (rn_debug)
443                 log(LOG_DEBUG, "rn_insert: Going In:\n"); // traverse(p);
444 #endif
445         t = rn_newpair(v_arg, b, nodes);
446         tt = t->rn_l;
447         if ((cp[p->rn_off] & p->rn_bmask) == 0)
448                 p->rn_l = t;
449         else
450                 p->rn_r = t;
451         x->rn_p = t;
452         t->rn_p = p; /* frees x, p as temp vars below */
453         if ((cp[t->rn_off] & t->rn_bmask) == 0) {
454                 t->rn_r = x;
455         } else {
456                 t->rn_r = tt;
457                 t->rn_l = x;
458         }
459 #ifdef RN_DEBUG
460         if (rn_debug)
461                 log(LOG_DEBUG, "rn_insert: Coming Out:\n"); // traverse(p);
462 #endif
463     }
464         return (tt);
465 }
466
467 struct radix_node *
468 rn_addmask(n_arg, search, skip)
469         int search, skip;
470         void *n_arg;
471 {
472         caddr_t netmask = (caddr_t)n_arg;
473         struct radix_node *x;
474         caddr_t cp, cplim;
475         int b = 0, mlen, j;
476         int maskduplicated, m0, isnormal;
477         struct radix_node *saved_x;
478         static int last_zeroed = 0;
479
480 #ifdef  RN_DEBUG
481         if (rn_debug)
482                 log(LOG_DEBUG, "rn_addmask(%p,%d,%d)\n", n_arg, search, skip);
483 #endif
484         mlen = *(u_char *)netmask;
485         if ((mlen = *(u_char *)netmask) > max_keylen)
486                 mlen = max_keylen;
487         if (skip == 0)
488                 skip = 1;
489         if (mlen <= skip)
490                 return (mask_rnhead->rnh_nodes);
491         if (skip > 1)
492                 Bcopy(rn_ones + 1, addmask_key + 1, skip - 1);
493         if ((m0 = mlen) > skip)
494                 Bcopy(netmask + skip, addmask_key + skip, mlen - skip);
495         /*
496          * Trim trailing zeroes.
497          */
498         for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0;)
499                 cp--;
500         mlen = cp - addmask_key;
501         if (mlen <= skip) {
502                 if (m0 >= last_zeroed)
503                         last_zeroed = mlen;
504                 return (mask_rnhead->rnh_nodes);
505         }
506         if (m0 < last_zeroed)
507                 Bzero(addmask_key + m0, last_zeroed - m0);
508         *addmask_key = last_zeroed = mlen;
509         x = rn_search(addmask_key, rn_masktop);
510         if (Bcmp(addmask_key, x->rn_key, mlen) != 0)
511                 x = 0;
512         if (x || search)
513                 return (x);
514         R_Malloc(x, struct radix_node *, max_keylen + 2 * sizeof (*x));
515         if ((saved_x = x) == 0)
516                 return (0);
517         Bzero(x, max_keylen + 2 * sizeof (*x));
518         netmask = cp = (caddr_t)(x + 2);
519         Bcopy(addmask_key, cp, mlen);
520         x = rn_insert(cp, mask_rnhead, &maskduplicated, x);
521         if (maskduplicated) {
522 #if 0
523                 log(LOG_ERR, "rn_addmask: mask impossibly already in tree\n");
524 #endif
525                 Free(saved_x);
526                 return (x);
527         }
528         /*
529          * Calculate index of mask, and check for normalcy.
530          */
531         cplim = netmask + mlen;
532         isnormal = 1;
533         for (cp = netmask + skip; (cp < cplim) && *(u_char *)cp == 0xff;)
534                 cp++;
535         if (cp != cplim) {
536                 for (j = 0x80; (j & *cp) != 0; j >>= 1)
537                         b++;
538                 if (*cp != normal_chars[b] || cp != (cplim - 1))
539                         isnormal = 0;
540         }
541         b += (cp - netmask) << 3;
542         x->rn_b = -1 - b;
543         if (isnormal)
544                 x->rn_flags |= RNF_NORMAL;
545         return (x);
546 }
547
548 static int      /* XXX: arbitrary ordering for non-contiguous masks */
549 rn_lexobetter(m_arg, n_arg)
550         void *m_arg, *n_arg;
551 {
552         u_char *mp = m_arg, *np = n_arg, *lim;
553
554         if (*mp > *np)
555                 return 1;  /* not really, but need to check longer one first */
556         if (*mp == *np)
557                 for (lim = mp + *mp; mp < lim;)
558                         if (*mp++ > *np++)
559                                 return 1;
560         return 0;
561 }
562
563 static struct radix_mask *
564 rn_new_radix_mask(tt, next)
565         struct radix_node *tt;
566         struct radix_mask *next;
567 {
568         struct radix_mask *m;
569
570         MKGet(m);
571         if (m == 0) {
572 #if 0
573                 log(LOG_ERR, "Mask for route not entered\n");
574 #endif
575                 return (0);
576         }
577         Bzero(m, sizeof *m);
578         m->rm_b = tt->rn_b;
579         m->rm_flags = tt->rn_flags;
580         if (tt->rn_flags & RNF_NORMAL)
581                 m->rm_leaf = tt;
582         else
583                 m->rm_mask = tt->rn_mask;
584         m->rm_mklist = next;
585         tt->rn_mklist = m;
586         return m;
587 }
588
589 struct radix_node *
590 rn_addroute(v_arg, n_arg, head, treenodes)
591         void *v_arg, *n_arg;
592         struct radix_node_head *head;
593         struct radix_node treenodes[2];
594 {
595         caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
596         struct radix_node *t, *x = NULL, *tt;
597         struct radix_node *saved_tt, *top = head->rnh_treetop;
598         short b = 0, b_leaf = 0;
599         int keyduplicated;
600         caddr_t mmask;
601         struct radix_mask *m, **mp;
602
603 #ifdef  RN_DEBUG
604         if (rn_debug)
605                 log(LOG_DEBUG, "rn_addroute(%p,%p,%p,%p)\n", v_arg, n_arg, head, treenodes);
606 #endif
607         /*
608          * In dealing with non-contiguous masks, there may be
609          * many different routes which have the same mask.
610          * We will find it useful to have a unique pointer to
611          * the mask to speed avoiding duplicate references at
612          * nodes and possibly save time in calculating indices.
613          */
614         if (netmask) {
615                 if ((x = rn_addmask(netmask, 0, top->rn_off)) == 0)
616                         return (0);
617                 b_leaf = x->rn_b;
618                 b = -1 - x->rn_b;
619                 netmask = x->rn_key;
620         }
621         /*
622          * Deal with duplicated keys: attach node to previous instance
623          */
624         saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
625         if (keyduplicated) {
626                 for (t = tt; tt; t = tt, tt = tt->rn_dupedkey) {
627                         if (tt->rn_mask == netmask)
628                                 return (0);
629                         if (netmask == 0 ||
630                             (tt->rn_mask &&
631                              ((b_leaf < tt->rn_b) || /* index(netmask) > node */
632                                rn_refines(netmask, tt->rn_mask) ||
633                                rn_lexobetter(netmask, tt->rn_mask))))
634                                 break;
635                 }
636                 /*
637                  * If the mask is not duplicated, we wouldn't
638                  * find it among possible duplicate key entries
639                  * anyway, so the above test doesn't hurt.
640                  *
641                  * We sort the masks for a duplicated key the same way as
642                  * in a masklist -- most specific to least specific.
643                  * This may require the unfortunate nuisance of relocating
644                  * the head of the list.
645                  *
646                  * We also reverse, or doubly link the list through the
647                  * parent pointer.
648                  */
649                 if (tt == saved_tt) {
650                         struct  radix_node *xx = x;
651                         /* link in at head of list */
652                         (tt = treenodes)->rn_dupedkey = t;
653                         tt->rn_flags = t->rn_flags;
654                         tt->rn_p = x = t->rn_p;
655                         t->rn_p = tt;
656                         if (x->rn_l == t)
657                                 x->rn_l = tt;
658                         else
659                                 x->rn_r = tt;
660                         saved_tt = tt;
661                         x = xx;
662                 } else {
663                         (tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
664                         t->rn_dupedkey = tt;
665                         tt->rn_p = t;
666                         if (tt->rn_dupedkey)
667                                 tt->rn_dupedkey->rn_p = tt;
668                 }
669 #ifdef RN_DEBUG
670                 t=tt+1;
671                 tt->rn_info = rn_nodenum++;
672                 t->rn_info = rn_nodenum++;
673                 tt->rn_twin = t;
674                 tt->rn_ybro = rn_clist;
675                 rn_clist = tt;
676 #endif
677                 tt->rn_key = (caddr_t) v;
678                 tt->rn_b = -1;
679                 tt->rn_flags = RNF_ACTIVE;
680         }
681         /*
682          * Put mask in tree.
683          */
684         if (netmask) {
685                 tt->rn_mask = netmask;
686                 tt->rn_b = x->rn_b;
687                 tt->rn_flags |= x->rn_flags & RNF_NORMAL;
688         }
689         t = saved_tt->rn_p;
690         if (keyduplicated)
691                 goto on2;
692         b_leaf = -1 - t->rn_b;
693         if (t->rn_r == saved_tt)
694                 x = t->rn_l;
695         else
696                 x = t->rn_r;
697         /* Promote general routes from below */
698         if (x->rn_b < 0) {
699             for (mp = &t->rn_mklist; x; x = x->rn_dupedkey)
700                 if (x->rn_mask && (x->rn_b >= b_leaf) && x->rn_mklist == 0) {
701                         *mp = m = rn_new_radix_mask(x, 0);
702                         if (m)
703                                 mp = &m->rm_mklist;
704                 }
705         } else if (x->rn_mklist) {
706                 /*
707                  * Skip over masks whose index is > that of new node
708                  */
709                 for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist)
710                         if (m->rm_b >= b_leaf)
711                                 break;
712                 t->rn_mklist = m;
713                 *mp = 0;
714         }
715 on2:
716         /* Add new route to highest possible ancestor's list */
717         if ((netmask == 0) || (b > t->rn_b ))
718                 return tt; /* can't lift at all */
719         b_leaf = tt->rn_b;
720         do {
721                 x = t;
722                 t = t->rn_p;
723         } while (b <= t->rn_b && x != top);
724         /*
725          * Search through routes associated with node to
726          * insert new route according to index.
727          * Need same criteria as when sorting dupedkeys to avoid
728          * double loop on deletion.
729          */
730         for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist) {
731                 if (m->rm_b < b_leaf)
732                         continue;
733                 if (m->rm_b > b_leaf)
734                         break;
735                 if (m->rm_flags & RNF_NORMAL) {
736                         mmask = m->rm_leaf->rn_mask;
737                         if (tt->rn_flags & RNF_NORMAL) {
738 #if 0
739                                 log(LOG_ERR, "Non-unique normal route,"
740                                     " mask not entered\n");
741 #endif
742                                 return tt;
743                         }
744                 } else
745                         mmask = m->rm_mask;
746                 if (mmask == netmask) {
747                         m->rm_refs++;
748                         tt->rn_mklist = m;
749                         return tt;
750                 }
751                 if (rn_refines(netmask, mmask)
752                     || rn_lexobetter(netmask, mmask))
753                         break;
754         }
755         *mp = rn_new_radix_mask(tt, *mp);
756         return tt;
757 }
758
759 struct radix_node *
760 rn_delete(v_arg, netmask_arg, head)
761         void *v_arg, *netmask_arg;
762         struct radix_node_head *head;
763 {
764         struct radix_node *t, *p, *x, *tt;
765         struct radix_mask *m, *saved_m, **mp;
766         struct radix_node *dupedkey, *saved_tt, *top;
767         caddr_t v, netmask;
768         int b, head_off, vlen;
769
770         v = v_arg;
771         netmask = netmask_arg;
772         x = head->rnh_treetop;
773         tt = rn_search(v, x);
774         head_off = x->rn_off;
775         vlen =  *(u_char *)v;
776         saved_tt = tt;
777         top = x;
778         if (tt == 0 ||
779             Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
780                 return (0);
781         /*
782          * Delete our route from mask lists.
783          */
784         if (netmask) {
785                 if ((x = rn_addmask(netmask, 1, head_off)) == 0)
786                         return (0);
787                 netmask = x->rn_key;
788                 while (tt->rn_mask != netmask)
789                         if ((tt = tt->rn_dupedkey) == 0)
790                                 return (0);
791         }
792         if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0)
793                 goto on1;
794         if (tt->rn_flags & RNF_NORMAL) {
795                 if (m->rm_leaf != tt || m->rm_refs > 0) {
796 #if 0
797                         log(LOG_ERR, "rn_delete: inconsistent annotation\n");
798 #endif
799                         return 0;  /* dangling ref could cause disaster */
800                 }
801         } else {
802                 if (m->rm_mask != tt->rn_mask) {
803 #if 0
804                         log(LOG_ERR, "rn_delete: inconsistent annotation\n");
805 #endif
806                         goto on1;
807                 }
808                 if (--m->rm_refs >= 0)
809                         goto on1;
810         }
811         b = -1 - tt->rn_b;
812         t = saved_tt->rn_p;
813         if (b > t->rn_b)
814                 goto on1; /* Wasn't lifted at all */
815         do {
816                 x = t;
817                 t = t->rn_p;
818         } while (b <= t->rn_b && x != top);
819         for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist)
820                 if (m == saved_m) {
821                         *mp = m->rm_mklist;
822                         MKFree(m);
823                         break;
824                 }
825         if (m == 0) {
826 #if 0
827                 log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
828 #endif
829                 if (tt->rn_flags & RNF_NORMAL)
830                         return (0); /* Dangling ref to us */
831         }
832 on1:
833         /*
834          * Eliminate us from tree
835          */
836         if (tt->rn_flags & RNF_ROOT)
837                 return (0);
838 #ifdef RN_DEBUG
839         /* Get us out of the creation list */
840         for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro)
841                 ;
842         if (t) t->rn_ybro = tt->rn_ybro;
843 #endif
844         t = tt->rn_p;
845         dupedkey = saved_tt->rn_dupedkey;
846         if (dupedkey) {
847                 /*
848                  * Here, tt is the deletion target and
849                  * saved_tt is the head of the dupedkey chain.
850                  */
851                 if (tt == saved_tt) {
852                         x = dupedkey;
853                         x->rn_p = t;
854                         if (t->rn_l == tt)
855                                 t->rn_l = x;
856                         else
857                                 t->rn_r = x;
858                 } else {
859                         /* find node in front of tt on the chain */
860                         for (x = p = saved_tt; p && p->rn_dupedkey != tt;)
861                                 p = p->rn_dupedkey;
862                         if (p) {
863                                 p->rn_dupedkey = tt->rn_dupedkey;
864                                 if (tt->rn_dupedkey)
865                                         tt->rn_dupedkey->rn_p = p;
866                         }
867 #if 0
868                         else
869                                 log(LOG_ERR, "rn_delete: couldn't find us\n");
870 #endif
871                 }
872                 t = tt + 1;
873                 if  (t->rn_flags & RNF_ACTIVE) {
874 #ifndef RN_DEBUG
875                         *++x = *t;
876                         p = t->rn_p;
877 #else
878                         b = t->rn_info;
879                         *++x = *t;
880                         t->rn_info = b;
881                         p = t->rn_p;
882 #endif
883                         if (p->rn_l == t)
884                                 p->rn_l = x;
885                         else
886                                 p->rn_r = x;
887                         x->rn_l->rn_p = x;
888                         x->rn_r->rn_p = x;
889                 }
890                 goto out;
891         }
892         if (t->rn_l == tt)
893                 x = t->rn_r;
894         else
895                 x = t->rn_l;
896         p = t->rn_p;
897         if (p->rn_r == t)
898                 p->rn_r = x;
899         else
900                 p->rn_l = x;
901         x->rn_p = p;
902         /*
903          * Demote routes attached to us.
904          */
905         if (t->rn_mklist) {
906                 if (x->rn_b >= 0) {
907                         for (mp = &x->rn_mklist; (m = *mp) != NULL;)
908                                 mp = &m->rm_mklist;
909                         *mp = t->rn_mklist;
910                 } else {
911                         /* If there are any key,mask pairs in a sibling
912                            duped-key chain, some subset will appear sorted
913                            in the same order attached to our mklist */
914                         for (m = t->rn_mklist; m && x; x = x->rn_dupedkey)
915                                 if (m == x->rn_mklist) {
916                                         struct radix_mask *mm = m->rm_mklist;
917                                         x->rn_mklist = 0;
918                                         if (--(m->rm_refs) < 0)
919                                                 MKFree(m);
920                                         m = mm;
921                                 }
922 #if 0
923                         if (m)
924                                 log(LOG_ERR, "%s %p at %p\n",
925                                     "rn_delete: Orphaned Mask", m, x);
926 #endif
927                 }
928         }
929         /*
930          * We may be holding an active internal node in the tree.
931          */
932         x = tt + 1;
933         if (t != x) {
934 #ifndef RN_DEBUG
935                 *t = *x;
936 #else
937                 b = t->rn_info;
938                 *t = *x;
939                 t->rn_info = b;
940 #endif
941                 t->rn_l->rn_p = t;
942                 t->rn_r->rn_p = t;
943                 p = x->rn_p;
944                 if (p->rn_l == x)
945                         p->rn_l = t;
946                 else
947                         p->rn_r = t;
948         }
949 out:
950         tt->rn_flags &= ~RNF_ACTIVE;
951         tt[1].rn_flags &= ~RNF_ACTIVE;
952         return (tt);
953 }
954
955 int
956 rn_walktree(h, f, w)
957         struct radix_node_head *h;
958         int (*f) __P((struct radix_node *, void *));
959         void *w;
960 {
961         int error;
962         struct radix_node *base, *next;
963         struct radix_node *rn = h->rnh_treetop;
964         /*
965          * This gets complicated because we may delete the node
966          * while applying the function f to it, so we need to calculate
967          * the successor node in advance.
968          */
969         /* First time through node, go left */
970         while (rn->rn_b >= 0)
971                 rn = rn->rn_l;
972         for (;;) {
973                 base = rn;
974                 /* If at right child go back up, otherwise, go right */
975                 while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0)
976                         rn = rn->rn_p;
977                 /* Find the next *leaf* since next node might vanish, too */
978                 for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
979                         rn = rn->rn_l;
980                 next = rn;
981                 /* Process leaves */
982                 while ((rn = base) != NULL) {
983                         base = rn->rn_dupedkey;
984                         if (!(rn->rn_flags & RNF_ROOT)
985                             && (error = (*f)(rn, w)))
986                                 return (error);
987                 }
988                 rn = next;
989                 if (rn->rn_flags & RNF_ROOT)
990                         return (0);
991         }
992         /* NOTREACHED */
993 }
994
995 int
996 rn_inithead(head, off)
997         void **head;
998         int off;
999 {
1000         struct radix_node_head *rnh;
1001
1002         if (*head)
1003                 return (1);
1004         R_Malloc(rnh, struct radix_node_head *, sizeof (*rnh));
1005         if (rnh == 0)
1006                 return (0);
1007         *head = rnh;
1008         return rn_inithead0(rnh, off);
1009 }
1010
1011 int
1012 rn_inithead0(rnh, off)
1013         struct radix_node_head *rnh;
1014         int off;
1015 {
1016         struct radix_node *t, *tt, *ttt;
1017
1018         Bzero(rnh, sizeof (*rnh));
1019         t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
1020         ttt = rnh->rnh_nodes + 2;
1021         t->rn_r = ttt;
1022         t->rn_p = t;
1023         tt = t->rn_l;
1024         tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
1025         tt->rn_b = -1 - off;
1026         *ttt = *tt;
1027         ttt->rn_key = rn_ones;
1028         rnh->rnh_addaddr = rn_addroute;
1029         rnh->rnh_deladdr = rn_delete;
1030         rnh->rnh_matchaddr = rn_match;
1031         rnh->rnh_lookup = rn_lookup;
1032         rnh->rnh_walktree = rn_walktree;
1033         rnh->rnh_treetop = t;
1034         return (1);
1035 }
1036
1037 void
1038 rn_init()
1039 {
1040         char *cp, *cplim;
1041
1042         if (max_keylen == 0) {
1043 #if 0
1044                 log(LOG_ERR,
1045                     "rn_init: radix functions require max_keylen be set\n");
1046 #endif
1047                 return;
1048         }
1049         if (rn_zeros == NULL) {
1050                 R_Malloc(rn_zeros, char *, 3 * max_keylen);
1051         }
1052         if (rn_zeros == NULL)
1053                 panic("rn_init");
1054         Bzero(rn_zeros, 3 * max_keylen);
1055         rn_ones = cp = rn_zeros + max_keylen;
1056         addmask_key = cplim = rn_ones + max_keylen;
1057         while (cp < cplim)
1058                 *cp++ = -1;
1059         if (rn_inithead((void *)&mask_rnhead, 0) == 0)
1060                 panic("rn_init 2");
1061 }
1062
1063
1064 static int
1065 rn_freenode(struct radix_node *n, void *p)
1066 {
1067         struct radix_node_head *rnh = p;
1068         struct radix_node *d;
1069
1070         d = rnh->rnh_deladdr(n->rn_key, NULL, rnh);
1071         if (d != NULL) {
1072                 FreeS(d, max_keylen + 2 * sizeof (*d));
1073         }
1074         return 0;
1075 }
1076
1077
1078 void
1079 rn_freehead(rnh)
1080       struct radix_node_head *rnh;
1081 {
1082
1083         (void)rn_walktree(rnh, rn_freenode, rnh);
1084
1085         rnh->rnh_addaddr = NULL;
1086         rnh->rnh_deladdr = NULL;
1087         rnh->rnh_matchaddr = NULL;
1088         rnh->rnh_lookup = NULL;
1089         rnh->rnh_walktree = NULL;
1090
1091         Free(rnh);
1092 }
1093
1094
1095 void
1096 rn_fini()
1097 {
1098         struct radix_mask *m;
1099
1100         if (rn_zeros != NULL) {
1101                 FreeS(rn_zeros, 3 * max_keylen);
1102                 rn_zeros = NULL;
1103         }
1104
1105         if (mask_rnhead != NULL) {
1106                 rn_freehead(mask_rnhead);
1107                 mask_rnhead = NULL;
1108         }
1109
1110         while ((m = rn_mkfreelist) != NULL) {
1111                 rn_mkfreelist = m->rm_mklist;
1112                 KFREE(m);
1113         }
1114 }
1115
1116
1117 #ifdef  USE_MAIN
1118
1119 typedef struct myst {
1120         addrfamily_t    dst;
1121         addrfamily_t    mask;
1122         struct radix_node nodes[2];
1123 } myst_t;
1124
1125 int
1126 main(int argc, char *argv[])
1127 {
1128         struct radix_node_head *rnh;
1129         struct radix_node *rn;
1130         addrfamily_t af, mf;
1131         myst_t st1, st2, *stp;
1132
1133         memset(&st1, 0, sizeof(st1));
1134         memset(&st2, 0, sizeof(st2));
1135         memset(&af, 0, sizeof(af));
1136
1137         rn_init();
1138
1139         rnh = NULL;
1140         rn_inithead(&rnh, offsetof(addrfamily_t, adf_addr) << 3);
1141
1142         st1.dst.adf_len = sizeof(st1);
1143         st1.mask.adf_len = sizeof(st1);
1144         st1.dst.adf_addr.in4.s_addr = inet_addr("127.0.0.0");
1145         st1.mask.adf_addr.in4.s_addr = inet_addr("255.0.0.0");
1146         rn = rnh->rnh_addaddr(&st1.dst, &st1.mask, rnh, st1.nodes);
1147         printf("add.1 %p\n", rn);
1148
1149         st2.dst.adf_len = sizeof(st2);
1150         st2.mask.adf_len = sizeof(st2);
1151         st2.dst.adf_addr.in4.s_addr = inet_addr("127.0.1.0");
1152         st2.mask.adf_addr.in4.s_addr = inet_addr("255.255.255.0");
1153         rn = rnh->rnh_addaddr(&st2.dst, &st2.mask, rnh, st2.nodes);
1154         printf("add.2 %p\n", rn);
1155
1156         af.adf_len = sizeof(af);
1157         af.adf_addr.in4.s_addr = inet_addr("127.0.1.0");
1158         rn = rnh->rnh_matchaddr(&af, rnh);
1159         if (rn != NULL) {
1160                 printf("1.lookup = %p key %p mask %p\n", rn, rn->rn_key, rn->rn_mask);
1161                 stp = rn->rn_key;
1162                 printf("%s/", inet_ntoa(stp->dst.adf_addr.in4));
1163                 stp = rn->rn_mask;
1164                 printf("%s\n", inet_ntoa(stp->dst.adf_addr.in4));
1165         }
1166
1167         mf.adf_len = sizeof(mf);
1168         mf.adf_addr.in4.s_addr = inet_addr("255.255.255.0");
1169         rn = rnh->rnh_lookup(&af, &mf, rnh);
1170         if (rn != NULL) {
1171                 printf("2.lookup = %p key %p mask %p\n", rn, rn->rn_key, rn->rn_mask);
1172                 stp = rn->rn_key;
1173                 printf("%s/", inet_ntoa(stp->dst.adf_addr.in4));
1174                 stp = rn->rn_mask;
1175                 printf("%s\n", inet_ntoa(stp->dst.adf_addr.in4));
1176         }
1177
1178         af.adf_len = sizeof(af);
1179         af.adf_addr.in4.s_addr = inet_addr("126.0.0.1");
1180         rn = rnh->rnh_matchaddr(&af, rnh);
1181         if (rn != NULL) {
1182                 printf("3.lookup = %p key %p mask %p\n", rn, rn->rn_key, rn->rn_mask);
1183                 stp = rn->rn_key;
1184                 printf("%s/", inet_ntoa(stp->dst.adf_addr.in4));
1185                 stp = rn->rn_mask;
1186                 printf("%s\n", inet_ntoa(stp->dst.adf_addr.in4));
1187         }
1188
1189         return 0;
1190 }
1191
1192
1193 void
1194 log(int level, char *format, ...)
1195 {
1196         va_list ap;
1197
1198         va_start(ap, format);
1199         vfprintf(stderr, format, ap);
1200         va_end(ap);
1201 }
1202 #endif
1203
1204
1205 #ifndef _KERNEL
1206 void
1207 panic(char *str)
1208 {
1209         fputs(str, stderr);
1210         abort();
1211 }
1212 #endif