1*8a16b7a1SPedro F. Giffuni /*-
2*8a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
3*8a16b7a1SPedro F. Giffuni *
47b6ab19dSGarrett Wollman * Copyright (c) 1988, 1989, 1993
57b6ab19dSGarrett Wollman * The Regents of the University of California. All rights reserved.
67b6ab19dSGarrett Wollman *
77b6ab19dSGarrett Wollman * Redistribution and use in source and binary forms, with or without
87b6ab19dSGarrett Wollman * modification, are permitted provided that the following conditions
97b6ab19dSGarrett Wollman * are met:
107b6ab19dSGarrett Wollman * 1. Redistributions of source code must retain the above copyright
117b6ab19dSGarrett Wollman * notice, this list of conditions and the following disclaimer.
127b6ab19dSGarrett Wollman * 2. Redistributions in binary form must reproduce the above copyright
137b6ab19dSGarrett Wollman * notice, this list of conditions and the following disclaimer in the
147b6ab19dSGarrett Wollman * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
167b6ab19dSGarrett Wollman * may be used to endorse or promote products derived from this software
177b6ab19dSGarrett Wollman * without specific prior written permission.
187b6ab19dSGarrett Wollman *
197b6ab19dSGarrett Wollman * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
207b6ab19dSGarrett Wollman * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
217b6ab19dSGarrett Wollman * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
227b6ab19dSGarrett Wollman * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
237b6ab19dSGarrett Wollman * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
247b6ab19dSGarrett Wollman * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
257b6ab19dSGarrett Wollman * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
267b6ab19dSGarrett Wollman * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
277b6ab19dSGarrett Wollman * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
287b6ab19dSGarrett Wollman * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
297b6ab19dSGarrett Wollman * SUCH DAMAGE.
307b6ab19dSGarrett Wollman */
317b6ab19dSGarrett Wollman
327b6ab19dSGarrett Wollman /*
337b6ab19dSGarrett Wollman * Routines to build and maintain radix trees for routing lookups.
347b6ab19dSGarrett Wollman */
357b6ab19dSGarrett Wollman
367b6ab19dSGarrett Wollman #include "defs.h"
377b6ab19dSGarrett Wollman #define log(x, msg) syslog(x, msg)
387b6ab19dSGarrett Wollman #define panic(s) {log(LOG_ERR,s); exit(1);}
397b6ab19dSGarrett Wollman #define min(a,b) (((a)<(b))?(a):(b))
407b6ab19dSGarrett Wollman
417b6ab19dSGarrett Wollman int max_keylen;
42f2ca6d8aSPoul-Henning Kamp static struct radix_mask *rn_mkfreelist;
43f2ca6d8aSPoul-Henning Kamp static struct radix_node_head *mask_rnhead;
447b6ab19dSGarrett Wollman static char *addmask_key;
45a3e04ddcSPoul-Henning Kamp static const uint8_t normal_chars[] =
46f2ca6d8aSPoul-Henning Kamp { 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff};
477b6ab19dSGarrett Wollman static char *rn_zeros, *rn_ones;
487b6ab19dSGarrett Wollman
497b6ab19dSGarrett Wollman #define rn_masktop (mask_rnhead->rnh_treetop)
502c7a9781SMark Murray #define Bcmp(a, b, l) (l == 0 ? 0 \
512c7a9781SMark Murray : memcmp((caddr_t)(a), (caddr_t)(b), (size_t)l))
527b6ab19dSGarrett Wollman
532c7a9781SMark Murray static int rn_satisfies_leaf(char *, struct radix_node *, int);
54f2ca6d8aSPoul-Henning Kamp static struct radix_node *rn_addmask(void *n_arg, int search, int skip);
55f2ca6d8aSPoul-Henning Kamp static struct radix_node *rn_addroute(void *v_arg, void *n_arg,
56f2ca6d8aSPoul-Henning Kamp struct radix_node_head *head, struct radix_node treenodes[2]);
57f2ca6d8aSPoul-Henning Kamp static struct radix_node *rn_match(void *v_arg, struct radix_node_head *head);
587b6ab19dSGarrett Wollman
597b6ab19dSGarrett Wollman /*
607b6ab19dSGarrett Wollman * The data structure for the keys is a radix tree with one way
617b6ab19dSGarrett Wollman * branching removed. The index rn_b at an internal node n represents a bit
627b6ab19dSGarrett Wollman * position to be tested. The tree is arranged so that all descendants
637b6ab19dSGarrett Wollman * of a node n have keys whose bits all agree up to position rn_b - 1.
647b6ab19dSGarrett Wollman * (We say the index of n is rn_b.)
657b6ab19dSGarrett Wollman *
667b6ab19dSGarrett Wollman * There is at least one descendant which has a one bit at position rn_b,
677b6ab19dSGarrett Wollman * and at least one with a zero there.
687b6ab19dSGarrett Wollman *
697b6ab19dSGarrett Wollman * A route is determined by a pair of key and mask. We require that the
707b6ab19dSGarrett Wollman * bit-wise logical and of the key and mask to be the key.
717b6ab19dSGarrett Wollman * We define the index of a route to associated with the mask to be
727b6ab19dSGarrett Wollman * the first bit number in the mask where 0 occurs (with bit number 0
737b6ab19dSGarrett Wollman * representing the highest order bit).
747b6ab19dSGarrett Wollman *
757b6ab19dSGarrett Wollman * We say a mask is normal if every bit is 0, past the index of the mask.
767b6ab19dSGarrett Wollman * If a node n has a descendant (k, m) with index(m) == index(n) == rn_b,
777b6ab19dSGarrett Wollman * and m is a normal mask, then the route applies to every descendant of n.
787b6ab19dSGarrett Wollman * If the index(m) < rn_b, this implies the trailing last few bits of k
797b6ab19dSGarrett Wollman * before bit b are all 0, (and hence consequently true of every descendant
807b6ab19dSGarrett Wollman * of n), so the route applies to all descendants of the node as well.
817b6ab19dSGarrett Wollman *
827b6ab19dSGarrett Wollman * Similar logic shows that a non-normal mask m such that
837b6ab19dSGarrett Wollman * index(m) <= index(n) could potentially apply to many children of n.
847b6ab19dSGarrett Wollman * Thus, for each non-host route, we attach its mask to a list at an internal
857b6ab19dSGarrett Wollman * node as high in the tree as we can go.
867b6ab19dSGarrett Wollman *
877b6ab19dSGarrett Wollman * The present version of the code makes use of normal routes in short-
884b85a12fSUlrich Spörlein * circuiting an explicit mask and compare operation when testing whether
897b6ab19dSGarrett Wollman * a key satisfies a normal route, and also in remembering the unique leaf
907b6ab19dSGarrett Wollman * that governs a subtree.
917b6ab19dSGarrett Wollman */
927b6ab19dSGarrett Wollman
93f2ca6d8aSPoul-Henning Kamp static struct radix_node *
rn_search(void * v_arg,struct radix_node * head)947b6ab19dSGarrett Wollman rn_search(void *v_arg,
957b6ab19dSGarrett Wollman struct radix_node *head)
967b6ab19dSGarrett Wollman {
972c7a9781SMark Murray struct radix_node *x;
982c7a9781SMark Murray caddr_t v;
997b6ab19dSGarrett Wollman
1007b6ab19dSGarrett Wollman for (x = head, v = v_arg; x->rn_b >= 0;) {
1017b6ab19dSGarrett Wollman if (x->rn_bmask & v[x->rn_off])
1027b6ab19dSGarrett Wollman x = x->rn_r;
1037b6ab19dSGarrett Wollman else
1047b6ab19dSGarrett Wollman x = x->rn_l;
1057b6ab19dSGarrett Wollman }
1067b6ab19dSGarrett Wollman return (x);
1077b6ab19dSGarrett Wollman }
1087b6ab19dSGarrett Wollman
109f2ca6d8aSPoul-Henning Kamp static struct radix_node *
rn_search_m(void * v_arg,struct radix_node * head,void * m_arg)1107b6ab19dSGarrett Wollman rn_search_m(void *v_arg,
1117b6ab19dSGarrett Wollman struct radix_node *head,
1127b6ab19dSGarrett Wollman void *m_arg)
1137b6ab19dSGarrett Wollman {
1142c7a9781SMark Murray struct radix_node *x;
1152c7a9781SMark Murray caddr_t v = v_arg, m = m_arg;
1167b6ab19dSGarrett Wollman
1177b6ab19dSGarrett Wollman for (x = head; x->rn_b >= 0;) {
1187b6ab19dSGarrett Wollman if ((x->rn_bmask & m[x->rn_off]) &&
1197b6ab19dSGarrett Wollman (x->rn_bmask & v[x->rn_off]))
1207b6ab19dSGarrett Wollman x = x->rn_r;
1217b6ab19dSGarrett Wollman else
1227b6ab19dSGarrett Wollman x = x->rn_l;
1237b6ab19dSGarrett Wollman }
1247b6ab19dSGarrett Wollman return x;
1257b6ab19dSGarrett Wollman }
1267b6ab19dSGarrett Wollman
127f2ca6d8aSPoul-Henning Kamp static int
rn_refines(void * m_arg,void * n_arg)1287b6ab19dSGarrett Wollman rn_refines(void* m_arg, void *n_arg)
1297b6ab19dSGarrett Wollman {
1302c7a9781SMark Murray caddr_t m = m_arg, n = n_arg;
1312c7a9781SMark Murray caddr_t lim, lim2 = lim = n + *(u_char *)n;
1327b6ab19dSGarrett Wollman int longer = (*(u_char *)n++) - (int)(*(u_char *)m++);
1337b6ab19dSGarrett Wollman int masks_are_equal = 1;
1347b6ab19dSGarrett Wollman
1357b6ab19dSGarrett Wollman if (longer > 0)
1367b6ab19dSGarrett Wollman lim -= longer;
1377b6ab19dSGarrett Wollman while (n < lim) {
1387b6ab19dSGarrett Wollman if (*n & ~(*m))
1397b6ab19dSGarrett Wollman return 0;
1407b6ab19dSGarrett Wollman if (*n++ != *m++)
1417b6ab19dSGarrett Wollman masks_are_equal = 0;
1427b6ab19dSGarrett Wollman }
1437b6ab19dSGarrett Wollman while (n < lim2)
1447b6ab19dSGarrett Wollman if (*n++)
1457b6ab19dSGarrett Wollman return 0;
1467b6ab19dSGarrett Wollman if (masks_are_equal && (longer < 0))
1477b6ab19dSGarrett Wollman for (lim2 = m - longer; m < lim2; )
1487b6ab19dSGarrett Wollman if (*m++)
1497b6ab19dSGarrett Wollman return 1;
1507b6ab19dSGarrett Wollman return (!masks_are_equal);
1517b6ab19dSGarrett Wollman }
1527b6ab19dSGarrett Wollman
153f2ca6d8aSPoul-Henning Kamp static struct radix_node *
rn_lookup(void * v_arg,void * m_arg,struct radix_node_head * head)1542c7a9781SMark Murray rn_lookup(void *v_arg, void *m_arg, struct radix_node_head *head)
1557b6ab19dSGarrett Wollman {
1562c7a9781SMark Murray struct radix_node *x;
1577b6ab19dSGarrett Wollman caddr_t netmask = 0;
1587b6ab19dSGarrett Wollman
1597b6ab19dSGarrett Wollman if (m_arg) {
160562c5a82SPedro F. Giffuni if ((x = rn_addmask(m_arg, 1,
161562c5a82SPedro F. Giffuni head->rnh_treetop->rn_off)) == NULL)
1627b6ab19dSGarrett Wollman return (0);
1637b6ab19dSGarrett Wollman netmask = x->rn_key;
1647b6ab19dSGarrett Wollman }
1657b6ab19dSGarrett Wollman x = rn_match(v_arg, head);
1667b6ab19dSGarrett Wollman if (x && netmask) {
1677b6ab19dSGarrett Wollman while (x && x->rn_mask != netmask)
1687b6ab19dSGarrett Wollman x = x->rn_dupedkey;
1697b6ab19dSGarrett Wollman }
1707b6ab19dSGarrett Wollman return x;
1717b6ab19dSGarrett Wollman }
1727b6ab19dSGarrett Wollman
1737b6ab19dSGarrett Wollman static int
rn_satisfies_leaf(char * trial,struct radix_node * leaf,int skip)1742c7a9781SMark Murray rn_satisfies_leaf(char *trial,
1752c7a9781SMark Murray struct radix_node *leaf,
1767b6ab19dSGarrett Wollman int skip)
1777b6ab19dSGarrett Wollman {
1782c7a9781SMark Murray char *cp = trial, *cp2 = leaf->rn_key, *cp3 = leaf->rn_mask;
1797b6ab19dSGarrett Wollman char *cplim;
1807b6ab19dSGarrett Wollman int length = min(*(u_char *)cp, *(u_char *)cp2);
1817b6ab19dSGarrett Wollman
182562c5a82SPedro F. Giffuni if (cp3 == NULL)
1837b6ab19dSGarrett Wollman cp3 = rn_ones;
1847b6ab19dSGarrett Wollman else
1857b6ab19dSGarrett Wollman length = min(length, *(u_char *)cp3);
1867b6ab19dSGarrett Wollman cplim = cp + length; cp3 += skip; cp2 += skip;
1877b6ab19dSGarrett Wollman for (cp += skip; cp < cplim; cp++, cp2++, cp3++)
1887b6ab19dSGarrett Wollman if ((*cp ^ *cp2) & *cp3)
1897b6ab19dSGarrett Wollman return 0;
1907b6ab19dSGarrett Wollman return 1;
1917b6ab19dSGarrett Wollman }
1927b6ab19dSGarrett Wollman
193f2ca6d8aSPoul-Henning Kamp static struct radix_node *
rn_match(void * v_arg,struct radix_node_head * head)1947b6ab19dSGarrett Wollman rn_match(void *v_arg,
1957b6ab19dSGarrett Wollman struct radix_node_head *head)
1967b6ab19dSGarrett Wollman {
1977b6ab19dSGarrett Wollman caddr_t v = v_arg;
1982c7a9781SMark Murray struct radix_node *t = head->rnh_treetop, *x;
1992c7a9781SMark Murray caddr_t cp = v, cp2;
2007b6ab19dSGarrett Wollman caddr_t cplim;
2017b6ab19dSGarrett Wollman struct radix_node *saved_t, *top = t;
2027b6ab19dSGarrett Wollman int off = t->rn_off, vlen = *(u_char *)cp, matched_off;
2032c7a9781SMark Murray int test, b, rn_b;
2047b6ab19dSGarrett Wollman
2057b6ab19dSGarrett Wollman /*
2067b6ab19dSGarrett Wollman * Open code rn_search(v, top) to avoid overhead of extra
2077b6ab19dSGarrett Wollman * subroutine call.
2087b6ab19dSGarrett Wollman */
2097b6ab19dSGarrett Wollman for (; t->rn_b >= 0; ) {
2107b6ab19dSGarrett Wollman if (t->rn_bmask & cp[t->rn_off])
2117b6ab19dSGarrett Wollman t = t->rn_r;
2127b6ab19dSGarrett Wollman else
2137b6ab19dSGarrett Wollman t = t->rn_l;
2147b6ab19dSGarrett Wollman }
2157b6ab19dSGarrett Wollman /*
2167b6ab19dSGarrett Wollman * See if we match exactly as a host destination
2177b6ab19dSGarrett Wollman * or at least learn how many bits match, for normal mask finesse.
2187b6ab19dSGarrett Wollman *
2197b6ab19dSGarrett Wollman * It doesn't hurt us to limit how many bytes to check
2207b6ab19dSGarrett Wollman * to the length of the mask, since if it matches we had a genuine
2217b6ab19dSGarrett Wollman * match and the leaf we have is the most specific one anyway;
2227b6ab19dSGarrett Wollman * if it didn't match with a shorter length it would fail
2237b6ab19dSGarrett Wollman * with a long one. This wins big for class B&C netmasks which
2247b6ab19dSGarrett Wollman * are probably the most common case...
2257b6ab19dSGarrett Wollman */
2267b6ab19dSGarrett Wollman if (t->rn_mask)
2277b6ab19dSGarrett Wollman vlen = *(u_char *)t->rn_mask;
2287b6ab19dSGarrett Wollman cp += off; cp2 = t->rn_key + off; cplim = v + vlen;
2297b6ab19dSGarrett Wollman for (; cp < cplim; cp++, cp2++)
2307b6ab19dSGarrett Wollman if (*cp != *cp2)
2317b6ab19dSGarrett Wollman goto on1;
2327b6ab19dSGarrett Wollman /*
2337b6ab19dSGarrett Wollman * This extra grot is in case we are explicitly asked
2347b6ab19dSGarrett Wollman * to look up the default. Ugh!
2357b6ab19dSGarrett Wollman * Or 255.255.255.255
2367b6ab19dSGarrett Wollman *
2377b6ab19dSGarrett Wollman * In this case, we have a complete match of the key. Unless
2387b6ab19dSGarrett Wollman * the node is one of the roots, we are finished.
2394b85a12fSUlrich Spörlein * If it is the zeros root, then take what we have, preferring
2407b6ab19dSGarrett Wollman * any real data.
2417b6ab19dSGarrett Wollman * If it is the ones root, then pretend the target key was followed
2427b6ab19dSGarrett Wollman * by a byte of zeros.
2437b6ab19dSGarrett Wollman */
2447b6ab19dSGarrett Wollman if (!(t->rn_flags & RNF_ROOT))
2457b6ab19dSGarrett Wollman return t; /* not a root */
2467b6ab19dSGarrett Wollman if (t->rn_dupedkey) {
2477b6ab19dSGarrett Wollman t = t->rn_dupedkey;
2487b6ab19dSGarrett Wollman return t; /* have some real data */
2497b6ab19dSGarrett Wollman }
2507b6ab19dSGarrett Wollman if (*(cp-1) == 0)
2517b6ab19dSGarrett Wollman return t; /* not the ones root */
2527b6ab19dSGarrett Wollman b = 0; /* fake a zero after 255.255.255.255 */
2537b6ab19dSGarrett Wollman goto on2;
2547b6ab19dSGarrett Wollman on1:
2557b6ab19dSGarrett Wollman test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */
2567b6ab19dSGarrett Wollman for (b = 7; (test >>= 1) > 0;)
2577b6ab19dSGarrett Wollman b--;
2587b6ab19dSGarrett Wollman on2:
2597b6ab19dSGarrett Wollman matched_off = cp - v;
2607b6ab19dSGarrett Wollman b += matched_off << 3;
2617b6ab19dSGarrett Wollman rn_b = -1 - b;
2627b6ab19dSGarrett Wollman /*
2637b6ab19dSGarrett Wollman * If there is a host route in a duped-key chain, it will be first.
2647b6ab19dSGarrett Wollman */
2657b6ab19dSGarrett Wollman if ((saved_t = t)->rn_mask == 0)
2667b6ab19dSGarrett Wollman t = t->rn_dupedkey;
2672c7a9781SMark Murray for (; t; t = t->rn_dupedkey) {
2687b6ab19dSGarrett Wollman /*
2697b6ab19dSGarrett Wollman * Even if we don't match exactly as a host,
2707b6ab19dSGarrett Wollman * we may match if the leaf we wound up at is
2717b6ab19dSGarrett Wollman * a route to a net.
2727b6ab19dSGarrett Wollman */
2737b6ab19dSGarrett Wollman if (t->rn_flags & RNF_NORMAL) {
2747b6ab19dSGarrett Wollman if (rn_b <= t->rn_b)
2757b6ab19dSGarrett Wollman return t;
2762c7a9781SMark Murray } else if (rn_satisfies_leaf(v, t, matched_off)) {
2777b6ab19dSGarrett Wollman return t;
2782c7a9781SMark Murray }
2792c7a9781SMark Murray }
2807b6ab19dSGarrett Wollman t = saved_t;
2817b6ab19dSGarrett Wollman /* start searching up the tree */
2827b6ab19dSGarrett Wollman do {
2832c7a9781SMark Murray struct radix_mask *m;
2847b6ab19dSGarrett Wollman t = t->rn_p;
2857b6ab19dSGarrett Wollman if ((m = t->rn_mklist)) {
2867b6ab19dSGarrett Wollman /*
2877b6ab19dSGarrett Wollman * If non-contiguous masks ever become important
2887b6ab19dSGarrett Wollman * we can restore the masking and open coding of
2897b6ab19dSGarrett Wollman * the search and satisfaction test and put the
2907b6ab19dSGarrett Wollman * calculation of "off" back before the "do".
2917b6ab19dSGarrett Wollman */
2927b6ab19dSGarrett Wollman do {
2937b6ab19dSGarrett Wollman if (m->rm_flags & RNF_NORMAL) {
2947b6ab19dSGarrett Wollman if (rn_b <= m->rm_b)
2957b6ab19dSGarrett Wollman return (m->rm_leaf);
2967b6ab19dSGarrett Wollman } else {
2977b6ab19dSGarrett Wollman off = min(t->rn_off, matched_off);
2987b6ab19dSGarrett Wollman x = rn_search_m(v, t, m->rm_mask);
2997b6ab19dSGarrett Wollman while (x && x->rn_mask != m->rm_mask)
3007b6ab19dSGarrett Wollman x = x->rn_dupedkey;
3012c7a9781SMark Murray if (x && rn_satisfies_leaf(v, x, off))
3027b6ab19dSGarrett Wollman return x;
3037b6ab19dSGarrett Wollman }
3047b6ab19dSGarrett Wollman } while ((m = m->rm_mklist));
3057b6ab19dSGarrett Wollman }
3067b6ab19dSGarrett Wollman } while (t != top);
3077b6ab19dSGarrett Wollman return 0;
3087b6ab19dSGarrett Wollman }
3097b6ab19dSGarrett Wollman
3107b6ab19dSGarrett Wollman #ifdef RN_DEBUG
3117b6ab19dSGarrett Wollman int rn_nodenum;
3127b6ab19dSGarrett Wollman struct radix_node *rn_clist;
3137b6ab19dSGarrett Wollman int rn_saveinfo;
3147b6ab19dSGarrett Wollman int rn_debug = 1;
3157b6ab19dSGarrett Wollman #endif
3167b6ab19dSGarrett Wollman
317f2ca6d8aSPoul-Henning Kamp static struct radix_node *
rn_newpair(void * v,int b,struct radix_node nodes[2])3187b6ab19dSGarrett Wollman rn_newpair(void *v, int b, struct radix_node nodes[2])
3197b6ab19dSGarrett Wollman {
3202c7a9781SMark Murray struct radix_node *tt = nodes, *t = tt + 1;
3217b6ab19dSGarrett Wollman t->rn_b = b; t->rn_bmask = 0x80 >> (b & 7);
3227b6ab19dSGarrett Wollman t->rn_l = tt; t->rn_off = b >> 3;
3237b6ab19dSGarrett Wollman tt->rn_b = -1; tt->rn_key = (caddr_t)v; tt->rn_p = t;
3247b6ab19dSGarrett Wollman tt->rn_flags = t->rn_flags = RNF_ACTIVE;
3257b6ab19dSGarrett Wollman #ifdef RN_DEBUG
3267b6ab19dSGarrett Wollman tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
3277b6ab19dSGarrett Wollman tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
3287b6ab19dSGarrett Wollman #endif
3297b6ab19dSGarrett Wollman return t;
3307b6ab19dSGarrett Wollman }
3317b6ab19dSGarrett Wollman
332f2ca6d8aSPoul-Henning Kamp static struct radix_node *
rn_insert(void * v_arg,struct radix_node_head * head,int * dupentry,struct radix_node nodes[2])3337b6ab19dSGarrett Wollman rn_insert(void* v_arg,
3347b6ab19dSGarrett Wollman struct radix_node_head *head,
3357b6ab19dSGarrett Wollman int *dupentry,
3367b6ab19dSGarrett Wollman struct radix_node nodes[2])
3377b6ab19dSGarrett Wollman {
3387b6ab19dSGarrett Wollman caddr_t v = v_arg;
3397b6ab19dSGarrett Wollman struct radix_node *top = head->rnh_treetop;
3407b6ab19dSGarrett Wollman int head_off = top->rn_off, vlen = (int)*((u_char *)v);
3412c7a9781SMark Murray struct radix_node *t = rn_search(v_arg, top);
3422c7a9781SMark Murray caddr_t cp = v + head_off;
3432c7a9781SMark Murray int b;
3447b6ab19dSGarrett Wollman struct radix_node *tt;
3457b6ab19dSGarrett Wollman
3467b6ab19dSGarrett Wollman /*
3477b6ab19dSGarrett Wollman * Find first bit at which v and t->rn_key differ
3487b6ab19dSGarrett Wollman */
3497b6ab19dSGarrett Wollman {
3502c7a9781SMark Murray caddr_t cp2 = t->rn_key + head_off;
3512c7a9781SMark Murray int cmp_res;
3527b6ab19dSGarrett Wollman caddr_t cplim = v + vlen;
3537b6ab19dSGarrett Wollman
3547b6ab19dSGarrett Wollman while (cp < cplim)
3557b6ab19dSGarrett Wollman if (*cp2++ != *cp++)
3567b6ab19dSGarrett Wollman goto on1;
3577b6ab19dSGarrett Wollman /* handle adding 255.255.255.255 */
3587b6ab19dSGarrett Wollman if (!(t->rn_flags & RNF_ROOT) || *(cp2-1) == 0) {
3597b6ab19dSGarrett Wollman *dupentry = 1;
3607b6ab19dSGarrett Wollman return t;
3617b6ab19dSGarrett Wollman }
3627b6ab19dSGarrett Wollman on1:
3637b6ab19dSGarrett Wollman *dupentry = 0;
3647b6ab19dSGarrett Wollman cmp_res = (cp[-1] ^ cp2[-1]) & 0xff;
3657b6ab19dSGarrett Wollman for (b = (cp - v) << 3; cmp_res; b--)
3667b6ab19dSGarrett Wollman cmp_res >>= 1;
3677b6ab19dSGarrett Wollman }
3687b6ab19dSGarrett Wollman {
3692c7a9781SMark Murray struct radix_node *p, *x = top;
3707b6ab19dSGarrett Wollman cp = v;
3717b6ab19dSGarrett Wollman do {
3727b6ab19dSGarrett Wollman p = x;
3737b6ab19dSGarrett Wollman if (cp[x->rn_off] & x->rn_bmask)
3747b6ab19dSGarrett Wollman x = x->rn_r;
3757b6ab19dSGarrett Wollman else x = x->rn_l;
3762c7a9781SMark Murray } while ((unsigned)b > (unsigned)x->rn_b);
3777b6ab19dSGarrett Wollman #ifdef RN_DEBUG
3787b6ab19dSGarrett Wollman if (rn_debug)
3797b6ab19dSGarrett Wollman log(LOG_DEBUG, "rn_insert: Going In:\n"), traverse(p);
3807b6ab19dSGarrett Wollman #endif
3817b6ab19dSGarrett Wollman t = rn_newpair(v_arg, b, nodes); tt = t->rn_l;
3827b6ab19dSGarrett Wollman if ((cp[p->rn_off] & p->rn_bmask) == 0)
3837b6ab19dSGarrett Wollman p->rn_l = t;
3847b6ab19dSGarrett Wollman else
3857b6ab19dSGarrett Wollman p->rn_r = t;
3867b6ab19dSGarrett Wollman x->rn_p = t; t->rn_p = p; /* frees x, p as temp vars below */
3877b6ab19dSGarrett Wollman if ((cp[t->rn_off] & t->rn_bmask) == 0) {
3887b6ab19dSGarrett Wollman t->rn_r = x;
3897b6ab19dSGarrett Wollman } else {
3907b6ab19dSGarrett Wollman t->rn_r = tt; t->rn_l = x;
3917b6ab19dSGarrett Wollman }
3927b6ab19dSGarrett Wollman #ifdef RN_DEBUG
3937b6ab19dSGarrett Wollman if (rn_debug)
3947b6ab19dSGarrett Wollman log(LOG_DEBUG, "rn_insert: Coming Out:\n"), traverse(p);
3957b6ab19dSGarrett Wollman #endif
3967b6ab19dSGarrett Wollman }
3977b6ab19dSGarrett Wollman return (tt);
3987b6ab19dSGarrett Wollman }
3997b6ab19dSGarrett Wollman
400f2ca6d8aSPoul-Henning Kamp static struct radix_node *
rn_addmask(void * n_arg,int search,int skip)4017b6ab19dSGarrett Wollman rn_addmask(void *n_arg, int search, int skip)
4027b6ab19dSGarrett Wollman {
4037b6ab19dSGarrett Wollman caddr_t netmask = (caddr_t)n_arg;
4042c7a9781SMark Murray struct radix_node *x;
4052c7a9781SMark Murray caddr_t cp, cplim;
4062c7a9781SMark Murray int b = 0, mlen, j;
4077b6ab19dSGarrett Wollman int maskduplicated, m0, isnormal;
4087b6ab19dSGarrett Wollman struct radix_node *saved_x;
4097b6ab19dSGarrett Wollman static int last_zeroed = 0;
4107b6ab19dSGarrett Wollman
4117b6ab19dSGarrett Wollman if ((mlen = *(u_char *)netmask) > max_keylen)
4127b6ab19dSGarrett Wollman mlen = max_keylen;
4137b6ab19dSGarrett Wollman if (skip == 0)
4147b6ab19dSGarrett Wollman skip = 1;
4157b6ab19dSGarrett Wollman if (mlen <= skip)
4167b6ab19dSGarrett Wollman return (mask_rnhead->rnh_nodes);
4177b6ab19dSGarrett Wollman if (skip > 1)
4187b6ab19dSGarrett Wollman Bcopy(rn_ones + 1, addmask_key + 1, skip - 1);
4197b6ab19dSGarrett Wollman if ((m0 = mlen) > skip)
4207b6ab19dSGarrett Wollman Bcopy(netmask + skip, addmask_key + skip, mlen - skip);
4217b6ab19dSGarrett Wollman /*
4227b6ab19dSGarrett Wollman * Trim trailing zeroes.
4237b6ab19dSGarrett Wollman */
4247b6ab19dSGarrett Wollman for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0;)
4257b6ab19dSGarrett Wollman cp--;
4267b6ab19dSGarrett Wollman mlen = cp - addmask_key;
4277b6ab19dSGarrett Wollman if (mlen <= skip) {
4287b6ab19dSGarrett Wollman if (m0 >= last_zeroed)
4297b6ab19dSGarrett Wollman last_zeroed = mlen;
4307b6ab19dSGarrett Wollman return (mask_rnhead->rnh_nodes);
4317b6ab19dSGarrett Wollman }
4327b6ab19dSGarrett Wollman if (m0 < last_zeroed)
4337b6ab19dSGarrett Wollman Bzero(addmask_key + m0, last_zeroed - m0);
4347b6ab19dSGarrett Wollman *addmask_key = last_zeroed = mlen;
4357b6ab19dSGarrett Wollman x = rn_search(addmask_key, rn_masktop);
4367b6ab19dSGarrett Wollman if (Bcmp(addmask_key, x->rn_key, mlen) != 0)
437562c5a82SPedro F. Giffuni x = NULL;
4387b6ab19dSGarrett Wollman if (x || search)
4397b6ab19dSGarrett Wollman return (x);
4402c7a9781SMark Murray x = (struct radix_node *)rtmalloc(max_keylen + 2*sizeof(*x),
4412c7a9781SMark Murray "rn_addmask");
4422c7a9781SMark Murray saved_x = x;
4437b6ab19dSGarrett Wollman Bzero(x, max_keylen + 2 * sizeof (*x));
4447b6ab19dSGarrett Wollman netmask = cp = (caddr_t)(x + 2);
4457b6ab19dSGarrett Wollman Bcopy(addmask_key, cp, mlen);
4467b6ab19dSGarrett Wollman x = rn_insert(cp, mask_rnhead, &maskduplicated, x);
4477b6ab19dSGarrett Wollman if (maskduplicated) {
4487b6ab19dSGarrett Wollman log(LOG_ERR, "rn_addmask: mask impossibly already in tree");
4497b6ab19dSGarrett Wollman Free(saved_x);
4507b6ab19dSGarrett Wollman return (x);
4517b6ab19dSGarrett Wollman }
4527b6ab19dSGarrett Wollman /*
4537b6ab19dSGarrett Wollman * Calculate index of mask, and check for normalcy.
4547b6ab19dSGarrett Wollman */
4557b6ab19dSGarrett Wollman cplim = netmask + mlen; isnormal = 1;
4567b6ab19dSGarrett Wollman for (cp = netmask + skip; (cp < cplim) && *(u_char *)cp == 0xff;)
4577b6ab19dSGarrett Wollman cp++;
4587b6ab19dSGarrett Wollman if (cp != cplim) {
4597b6ab19dSGarrett Wollman for (j = 0x80; (j & *cp) != 0; j >>= 1)
4607b6ab19dSGarrett Wollman b++;
4617b6ab19dSGarrett Wollman if (*cp != normal_chars[b] || cp != (cplim - 1))
4627b6ab19dSGarrett Wollman isnormal = 0;
4637b6ab19dSGarrett Wollman }
4647b6ab19dSGarrett Wollman b += (cp - netmask) << 3;
4657b6ab19dSGarrett Wollman x->rn_b = -1 - b;
4667b6ab19dSGarrett Wollman if (isnormal)
4677b6ab19dSGarrett Wollman x->rn_flags |= RNF_NORMAL;
4687b6ab19dSGarrett Wollman return (x);
4697b6ab19dSGarrett Wollman }
4707b6ab19dSGarrett Wollman
4717b6ab19dSGarrett Wollman static int /* XXX: arbitrary ordering for non-contiguous masks */
rn_lexobetter(void * m_arg,void * n_arg)4727b6ab19dSGarrett Wollman rn_lexobetter(void *m_arg, void *n_arg)
4737b6ab19dSGarrett Wollman {
4742c7a9781SMark Murray u_char *mp = m_arg, *np = n_arg, *lim;
4757b6ab19dSGarrett Wollman
4767b6ab19dSGarrett Wollman if (*mp > *np)
4777b6ab19dSGarrett Wollman return 1; /* not really, but need to check longer one first */
4787b6ab19dSGarrett Wollman if (*mp == *np)
4797b6ab19dSGarrett Wollman for (lim = mp + *mp; mp < lim;)
4807b6ab19dSGarrett Wollman if (*mp++ > *np++)
4817b6ab19dSGarrett Wollman return 1;
4827b6ab19dSGarrett Wollman return 0;
4837b6ab19dSGarrett Wollman }
4847b6ab19dSGarrett Wollman
4857b6ab19dSGarrett Wollman static struct radix_mask *
rn_new_radix_mask(struct radix_node * tt,struct radix_mask * next)4862c7a9781SMark Murray rn_new_radix_mask(struct radix_node *tt,
4872c7a9781SMark Murray struct radix_mask *next)
4887b6ab19dSGarrett Wollman {
4892c7a9781SMark Murray struct radix_mask *m;
4907b6ab19dSGarrett Wollman
4917b6ab19dSGarrett Wollman MKGet(m);
492562c5a82SPedro F. Giffuni if (m == NULL) {
4937b6ab19dSGarrett Wollman log(LOG_ERR, "Mask for route not entered\n");
4947b6ab19dSGarrett Wollman return (0);
4957b6ab19dSGarrett Wollman }
4967b6ab19dSGarrett Wollman Bzero(m, sizeof *m);
4977b6ab19dSGarrett Wollman m->rm_b = tt->rn_b;
4987b6ab19dSGarrett Wollman m->rm_flags = tt->rn_flags;
4997b6ab19dSGarrett Wollman if (tt->rn_flags & RNF_NORMAL)
5007b6ab19dSGarrett Wollman m->rm_leaf = tt;
5017b6ab19dSGarrett Wollman else
5027b6ab19dSGarrett Wollman m->rm_mask = tt->rn_mask;
5037b6ab19dSGarrett Wollman m->rm_mklist = next;
5047b6ab19dSGarrett Wollman tt->rn_mklist = m;
5057b6ab19dSGarrett Wollman return m;
5067b6ab19dSGarrett Wollman }
5077b6ab19dSGarrett Wollman
508f2ca6d8aSPoul-Henning Kamp static struct radix_node *
rn_addroute(void * v_arg,void * n_arg,struct radix_node_head * head,struct radix_node treenodes[2])5097b6ab19dSGarrett Wollman rn_addroute(void *v_arg,
5107b6ab19dSGarrett Wollman void *n_arg,
5117b6ab19dSGarrett Wollman struct radix_node_head *head,
5127b6ab19dSGarrett Wollman struct radix_node treenodes[2])
5137b6ab19dSGarrett Wollman {
5147b6ab19dSGarrett Wollman caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg;
515562c5a82SPedro F. Giffuni struct radix_node *t, *x = NULL, *tt;
5167b6ab19dSGarrett Wollman struct radix_node *saved_tt, *top = head->rnh_treetop;
5177b6ab19dSGarrett Wollman short b = 0, b_leaf = 0;
5187b6ab19dSGarrett Wollman int keyduplicated;
5197b6ab19dSGarrett Wollman caddr_t mmask;
5207b6ab19dSGarrett Wollman struct radix_mask *m, **mp;
5217b6ab19dSGarrett Wollman
5227b6ab19dSGarrett Wollman /*
5237b6ab19dSGarrett Wollman * In dealing with non-contiguous masks, there may be
5247b6ab19dSGarrett Wollman * many different routes which have the same mask.
5257b6ab19dSGarrett Wollman * We will find it useful to have a unique pointer to
5267b6ab19dSGarrett Wollman * the mask to speed avoiding duplicate references at
5277b6ab19dSGarrett Wollman * nodes and possibly save time in calculating indices.
5287b6ab19dSGarrett Wollman */
5297b6ab19dSGarrett Wollman if (netmask) {
530562c5a82SPedro F. Giffuni if ((x = rn_addmask(netmask, 0, top->rn_off)) == NULL)
5317b6ab19dSGarrett Wollman return (0);
5327b6ab19dSGarrett Wollman b_leaf = x->rn_b;
5337b6ab19dSGarrett Wollman b = -1 - x->rn_b;
5347b6ab19dSGarrett Wollman netmask = x->rn_key;
5357b6ab19dSGarrett Wollman }
5367b6ab19dSGarrett Wollman /*
5377b6ab19dSGarrett Wollman * Deal with duplicated keys: attach node to previous instance
5387b6ab19dSGarrett Wollman */
5397b6ab19dSGarrett Wollman saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes);
5407b6ab19dSGarrett Wollman if (keyduplicated) {
5417b6ab19dSGarrett Wollman for (t = tt; tt; t = tt, tt = tt->rn_dupedkey) {
5427b6ab19dSGarrett Wollman if (tt->rn_mask == netmask)
5437b6ab19dSGarrett Wollman return (0);
5447b6ab19dSGarrett Wollman if (netmask == 0 ||
5457b6ab19dSGarrett Wollman (tt->rn_mask &&
5467b6ab19dSGarrett Wollman ((b_leaf < tt->rn_b) || /* index(netmask) > node */
5477b6ab19dSGarrett Wollman rn_refines(netmask, tt->rn_mask) ||
5487b6ab19dSGarrett Wollman rn_lexobetter(netmask, tt->rn_mask))))
5497b6ab19dSGarrett Wollman break;
5507b6ab19dSGarrett Wollman }
5517b6ab19dSGarrett Wollman /*
5527b6ab19dSGarrett Wollman * If the mask is not duplicated, we wouldn't
5537b6ab19dSGarrett Wollman * find it among possible duplicate key entries
5547b6ab19dSGarrett Wollman * anyway, so the above test doesn't hurt.
5557b6ab19dSGarrett Wollman *
5567b6ab19dSGarrett Wollman * We sort the masks for a duplicated key the same way as
5577b6ab19dSGarrett Wollman * in a masklist -- most specific to least specific.
5587b6ab19dSGarrett Wollman * This may require the unfortunate nuisance of relocating
5597b6ab19dSGarrett Wollman * the head of the list.
5607b6ab19dSGarrett Wollman */
5617b6ab19dSGarrett Wollman if (tt == saved_tt) {
5627b6ab19dSGarrett Wollman struct radix_node *xx = x;
5637b6ab19dSGarrett Wollman /* link in at head of list */
5647b6ab19dSGarrett Wollman (tt = treenodes)->rn_dupedkey = t;
5657b6ab19dSGarrett Wollman tt->rn_flags = t->rn_flags;
5667b6ab19dSGarrett Wollman tt->rn_p = x = t->rn_p;
5677b6ab19dSGarrett Wollman if (x->rn_l == t) x->rn_l = tt; else x->rn_r = tt;
5687b6ab19dSGarrett Wollman saved_tt = tt; x = xx;
5697b6ab19dSGarrett Wollman } else {
5707b6ab19dSGarrett Wollman (tt = treenodes)->rn_dupedkey = t->rn_dupedkey;
5717b6ab19dSGarrett Wollman t->rn_dupedkey = tt;
5727b6ab19dSGarrett Wollman }
5737b6ab19dSGarrett Wollman #ifdef RN_DEBUG
5747b6ab19dSGarrett Wollman t=tt+1; tt->rn_info = rn_nodenum++; t->rn_info = rn_nodenum++;
5757b6ab19dSGarrett Wollman tt->rn_twin = t; tt->rn_ybro = rn_clist; rn_clist = tt;
5767b6ab19dSGarrett Wollman #endif
5777b6ab19dSGarrett Wollman tt->rn_key = (caddr_t) v;
5787b6ab19dSGarrett Wollman tt->rn_b = -1;
5797b6ab19dSGarrett Wollman tt->rn_flags = RNF_ACTIVE;
5807b6ab19dSGarrett Wollman }
5817b6ab19dSGarrett Wollman /*
5827b6ab19dSGarrett Wollman * Put mask in tree.
5837b6ab19dSGarrett Wollman */
5847b6ab19dSGarrett Wollman if (netmask) {
5857b6ab19dSGarrett Wollman tt->rn_mask = netmask;
5867b6ab19dSGarrett Wollman tt->rn_b = x->rn_b;
5877b6ab19dSGarrett Wollman tt->rn_flags |= x->rn_flags & RNF_NORMAL;
5887b6ab19dSGarrett Wollman }
5897b6ab19dSGarrett Wollman t = saved_tt->rn_p;
5907b6ab19dSGarrett Wollman if (keyduplicated)
5917b6ab19dSGarrett Wollman goto on2;
5927b6ab19dSGarrett Wollman b_leaf = -1 - t->rn_b;
5937b6ab19dSGarrett Wollman if (t->rn_r == saved_tt) x = t->rn_l; else x = t->rn_r;
5947b6ab19dSGarrett Wollman /* Promote general routes from below */
5957b6ab19dSGarrett Wollman if (x->rn_b < 0) {
5967b6ab19dSGarrett Wollman for (mp = &t->rn_mklist; x; x = x->rn_dupedkey)
5977b6ab19dSGarrett Wollman if (x->rn_mask && (x->rn_b >= b_leaf) && x->rn_mklist == 0) {
5987b6ab19dSGarrett Wollman if ((*mp = m = rn_new_radix_mask(x, 0)))
5997b6ab19dSGarrett Wollman mp = &m->rm_mklist;
6007b6ab19dSGarrett Wollman }
6017b6ab19dSGarrett Wollman } else if (x->rn_mklist) {
6027b6ab19dSGarrett Wollman /*
6037b6ab19dSGarrett Wollman * Skip over masks whose index is > that of new node
6047b6ab19dSGarrett Wollman */
6057b6ab19dSGarrett Wollman for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
6067b6ab19dSGarrett Wollman if (m->rm_b >= b_leaf)
6077b6ab19dSGarrett Wollman break;
608562c5a82SPedro F. Giffuni t->rn_mklist = m; *mp = NULL;
6097b6ab19dSGarrett Wollman }
6107b6ab19dSGarrett Wollman on2:
6117b6ab19dSGarrett Wollman /* Add new route to highest possible ancestor's list */
6127b6ab19dSGarrett Wollman if ((netmask == 0) || (b > t->rn_b ))
6137b6ab19dSGarrett Wollman return tt; /* can't lift at all */
6147b6ab19dSGarrett Wollman b_leaf = tt->rn_b;
6157b6ab19dSGarrett Wollman do {
6167b6ab19dSGarrett Wollman x = t;
6177b6ab19dSGarrett Wollman t = t->rn_p;
6187b6ab19dSGarrett Wollman } while (b <= t->rn_b && x != top);
6197b6ab19dSGarrett Wollman /*
6207b6ab19dSGarrett Wollman * Search through routes associated with node to
6217b6ab19dSGarrett Wollman * insert new route according to index.
6227b6ab19dSGarrett Wollman * Need same criteria as when sorting dupedkeys to avoid
6237b6ab19dSGarrett Wollman * double loop on deletion.
6247b6ab19dSGarrett Wollman */
6257b6ab19dSGarrett Wollman for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist) {
6267b6ab19dSGarrett Wollman if (m->rm_b < b_leaf)
6277b6ab19dSGarrett Wollman continue;
6287b6ab19dSGarrett Wollman if (m->rm_b > b_leaf)
6297b6ab19dSGarrett Wollman break;
6307b6ab19dSGarrett Wollman if (m->rm_flags & RNF_NORMAL) {
6317b6ab19dSGarrett Wollman mmask = m->rm_leaf->rn_mask;
6327b6ab19dSGarrett Wollman if (tt->rn_flags & RNF_NORMAL) {
6337b6ab19dSGarrett Wollman log(LOG_ERR,
6347b6ab19dSGarrett Wollman "Non-unique normal route, mask not entered");
6357b6ab19dSGarrett Wollman return tt;
6367b6ab19dSGarrett Wollman }
6377b6ab19dSGarrett Wollman } else
6387b6ab19dSGarrett Wollman mmask = m->rm_mask;
6397b6ab19dSGarrett Wollman if (mmask == netmask) {
6407b6ab19dSGarrett Wollman m->rm_refs++;
6417b6ab19dSGarrett Wollman tt->rn_mklist = m;
6427b6ab19dSGarrett Wollman return tt;
6437b6ab19dSGarrett Wollman }
6447b6ab19dSGarrett Wollman if (rn_refines(netmask, mmask) || rn_lexobetter(netmask, mmask))
6457b6ab19dSGarrett Wollman break;
6467b6ab19dSGarrett Wollman }
6477b6ab19dSGarrett Wollman *mp = rn_new_radix_mask(tt, *mp);
6487b6ab19dSGarrett Wollman return tt;
6497b6ab19dSGarrett Wollman }
6507b6ab19dSGarrett Wollman
651f2ca6d8aSPoul-Henning Kamp static struct radix_node *
rn_delete(void * v_arg,void * netmask_arg,struct radix_node_head * head)6527b6ab19dSGarrett Wollman rn_delete(void *v_arg,
6537b6ab19dSGarrett Wollman void *netmask_arg,
6547b6ab19dSGarrett Wollman struct radix_node_head *head)
6557b6ab19dSGarrett Wollman {
6562c7a9781SMark Murray struct radix_node *t, *p, *x, *tt;
6577b6ab19dSGarrett Wollman struct radix_mask *m, *saved_m, **mp;
6587b6ab19dSGarrett Wollman struct radix_node *dupedkey, *saved_tt, *top;
6597b6ab19dSGarrett Wollman caddr_t v, netmask;
6607b6ab19dSGarrett Wollman int b, head_off, vlen;
6617b6ab19dSGarrett Wollman
6627b6ab19dSGarrett Wollman v = v_arg;
6637b6ab19dSGarrett Wollman netmask = netmask_arg;
6647b6ab19dSGarrett Wollman x = head->rnh_treetop;
6657b6ab19dSGarrett Wollman tt = rn_search(v, x);
6667b6ab19dSGarrett Wollman head_off = x->rn_off;
6677b6ab19dSGarrett Wollman vlen = *(u_char *)v;
6687b6ab19dSGarrett Wollman saved_tt = tt;
6697b6ab19dSGarrett Wollman top = x;
670562c5a82SPedro F. Giffuni if (tt == NULL ||
6717b6ab19dSGarrett Wollman Bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off))
6727b6ab19dSGarrett Wollman return (0);
6737b6ab19dSGarrett Wollman /*
6747b6ab19dSGarrett Wollman * Delete our route from mask lists.
6757b6ab19dSGarrett Wollman */
6767b6ab19dSGarrett Wollman if (netmask) {
677562c5a82SPedro F. Giffuni if ((x = rn_addmask(netmask, 1, head_off)) == NULL)
6787b6ab19dSGarrett Wollman return (0);
6797b6ab19dSGarrett Wollman netmask = x->rn_key;
6807b6ab19dSGarrett Wollman while (tt->rn_mask != netmask)
681562c5a82SPedro F. Giffuni if ((tt = tt->rn_dupedkey) == NULL)
6827b6ab19dSGarrett Wollman return (0);
6837b6ab19dSGarrett Wollman }
684562c5a82SPedro F. Giffuni if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == NULL)
6857b6ab19dSGarrett Wollman goto on1;
6867b6ab19dSGarrett Wollman if (tt->rn_flags & RNF_NORMAL) {
6877b6ab19dSGarrett Wollman if (m->rm_leaf != tt || m->rm_refs > 0) {
6887b6ab19dSGarrett Wollman log(LOG_ERR, "rn_delete: inconsistent annotation\n");
6897b6ab19dSGarrett Wollman return 0; /* dangling ref could cause disaster */
6907b6ab19dSGarrett Wollman }
6917b6ab19dSGarrett Wollman } else {
6927b6ab19dSGarrett Wollman if (m->rm_mask != tt->rn_mask) {
6937b6ab19dSGarrett Wollman log(LOG_ERR, "rn_delete: inconsistent annotation\n");
6947b6ab19dSGarrett Wollman goto on1;
6957b6ab19dSGarrett Wollman }
6967b6ab19dSGarrett Wollman if (--m->rm_refs >= 0)
6977b6ab19dSGarrett Wollman goto on1;
6987b6ab19dSGarrett Wollman }
6997b6ab19dSGarrett Wollman b = -1 - tt->rn_b;
7007b6ab19dSGarrett Wollman t = saved_tt->rn_p;
7017b6ab19dSGarrett Wollman if (b > t->rn_b)
7027b6ab19dSGarrett Wollman goto on1; /* Wasn't lifted at all */
7037b6ab19dSGarrett Wollman do {
7047b6ab19dSGarrett Wollman x = t;
7057b6ab19dSGarrett Wollman t = t->rn_p;
7067b6ab19dSGarrett Wollman } while (b <= t->rn_b && x != top);
7077b6ab19dSGarrett Wollman for (mp = &x->rn_mklist; (m = *mp); mp = &m->rm_mklist)
7087b6ab19dSGarrett Wollman if (m == saved_m) {
7097b6ab19dSGarrett Wollman *mp = m->rm_mklist;
7107b6ab19dSGarrett Wollman MKFree(m);
7117b6ab19dSGarrett Wollman break;
7127b6ab19dSGarrett Wollman }
713562c5a82SPedro F. Giffuni if (m == NULL) {
7147b6ab19dSGarrett Wollman log(LOG_ERR, "rn_delete: couldn't find our annotation\n");
7157b6ab19dSGarrett Wollman if (tt->rn_flags & RNF_NORMAL)
7167b6ab19dSGarrett Wollman return (0); /* Dangling ref to us */
7177b6ab19dSGarrett Wollman }
7187b6ab19dSGarrett Wollman on1:
7197b6ab19dSGarrett Wollman /*
7207b6ab19dSGarrett Wollman * Eliminate us from tree
7217b6ab19dSGarrett Wollman */
7227b6ab19dSGarrett Wollman if (tt->rn_flags & RNF_ROOT)
7237b6ab19dSGarrett Wollman return (0);
7247b6ab19dSGarrett Wollman #ifdef RN_DEBUG
7257b6ab19dSGarrett Wollman /* Get us out of the creation list */
7267b6ab19dSGarrett Wollman for (t = rn_clist; t && t->rn_ybro != tt; t = t->rn_ybro) {}
7277b6ab19dSGarrett Wollman if (t) t->rn_ybro = tt->rn_ybro;
7287b6ab19dSGarrett Wollman #endif
7297b6ab19dSGarrett Wollman t = tt->rn_p;
7307b6ab19dSGarrett Wollman if ((dupedkey = saved_tt->rn_dupedkey)) {
7317b6ab19dSGarrett Wollman if (tt == saved_tt) {
7327b6ab19dSGarrett Wollman x = dupedkey; x->rn_p = t;
7337b6ab19dSGarrett Wollman if (t->rn_l == tt) t->rn_l = x; else t->rn_r = x;
7347b6ab19dSGarrett Wollman } else {
7357b6ab19dSGarrett Wollman for (x = p = saved_tt; p && p->rn_dupedkey != tt;)
7367b6ab19dSGarrett Wollman p = p->rn_dupedkey;
7377b6ab19dSGarrett Wollman if (p) p->rn_dupedkey = tt->rn_dupedkey;
7387b6ab19dSGarrett Wollman else log(LOG_ERR, "rn_delete: couldn't find us\n");
7397b6ab19dSGarrett Wollman }
7407b6ab19dSGarrett Wollman t = tt + 1;
7417b6ab19dSGarrett Wollman if (t->rn_flags & RNF_ACTIVE) {
7427b6ab19dSGarrett Wollman #ifndef RN_DEBUG
7437b6ab19dSGarrett Wollman *++x = *t; p = t->rn_p;
7447b6ab19dSGarrett Wollman #else
7457b6ab19dSGarrett Wollman b = t->rn_info; *++x = *t; t->rn_info = b; p = t->rn_p;
7467b6ab19dSGarrett Wollman #endif
7477b6ab19dSGarrett Wollman if (p->rn_l == t) p->rn_l = x; else p->rn_r = x;
7487b6ab19dSGarrett Wollman x->rn_l->rn_p = x; x->rn_r->rn_p = x;
7497b6ab19dSGarrett Wollman }
7507b6ab19dSGarrett Wollman goto out;
7517b6ab19dSGarrett Wollman }
7527b6ab19dSGarrett Wollman if (t->rn_l == tt) x = t->rn_r; else x = t->rn_l;
7537b6ab19dSGarrett Wollman p = t->rn_p;
7547b6ab19dSGarrett Wollman if (p->rn_r == t) p->rn_r = x; else p->rn_l = x;
7557b6ab19dSGarrett Wollman x->rn_p = p;
7567b6ab19dSGarrett Wollman /*
7577b6ab19dSGarrett Wollman * Demote routes attached to us.
7587b6ab19dSGarrett Wollman */
7597b6ab19dSGarrett Wollman if (t->rn_mklist) {
7607b6ab19dSGarrett Wollman if (x->rn_b >= 0) {
7617b6ab19dSGarrett Wollman for (mp = &x->rn_mklist; (m = *mp);)
7627b6ab19dSGarrett Wollman mp = &m->rm_mklist;
7637b6ab19dSGarrett Wollman *mp = t->rn_mklist;
7647b6ab19dSGarrett Wollman } else {
7657b6ab19dSGarrett Wollman /* If there are any key,mask pairs in a sibling
7667b6ab19dSGarrett Wollman duped-key chain, some subset will appear sorted
7677b6ab19dSGarrett Wollman in the same order attached to our mklist */
7687b6ab19dSGarrett Wollman for (m = t->rn_mklist; m && x; x = x->rn_dupedkey)
7697b6ab19dSGarrett Wollman if (m == x->rn_mklist) {
7707b6ab19dSGarrett Wollman struct radix_mask *mm = m->rm_mklist;
7717b6ab19dSGarrett Wollman x->rn_mklist = 0;
7727b6ab19dSGarrett Wollman if (--(m->rm_refs) < 0)
7737b6ab19dSGarrett Wollman MKFree(m);
7747b6ab19dSGarrett Wollman m = mm;
7757b6ab19dSGarrett Wollman }
7767b6ab19dSGarrett Wollman if (m)
7772c7a9781SMark Murray syslog(LOG_ERR, "%s 0x%lx at 0x%lx\n",
7787b6ab19dSGarrett Wollman "rn_delete: Orphaned Mask",
7797b6ab19dSGarrett Wollman (unsigned long)m,
7807b6ab19dSGarrett Wollman (unsigned long)x);
7817b6ab19dSGarrett Wollman }
7827b6ab19dSGarrett Wollman }
7837b6ab19dSGarrett Wollman /*
7847b6ab19dSGarrett Wollman * We may be holding an active internal node in the tree.
7857b6ab19dSGarrett Wollman */
7867b6ab19dSGarrett Wollman x = tt + 1;
7877b6ab19dSGarrett Wollman if (t != x) {
7887b6ab19dSGarrett Wollman #ifndef RN_DEBUG
7897b6ab19dSGarrett Wollman *t = *x;
7907b6ab19dSGarrett Wollman #else
7917b6ab19dSGarrett Wollman b = t->rn_info; *t = *x; t->rn_info = b;
7927b6ab19dSGarrett Wollman #endif
7937b6ab19dSGarrett Wollman t->rn_l->rn_p = t; t->rn_r->rn_p = t;
7947b6ab19dSGarrett Wollman p = x->rn_p;
7957b6ab19dSGarrett Wollman if (p->rn_l == x) p->rn_l = t; else p->rn_r = t;
7967b6ab19dSGarrett Wollman }
7977b6ab19dSGarrett Wollman out:
7987b6ab19dSGarrett Wollman tt->rn_flags &= ~RNF_ACTIVE;
7997b6ab19dSGarrett Wollman tt[1].rn_flags &= ~RNF_ACTIVE;
8007b6ab19dSGarrett Wollman return (tt);
8017b6ab19dSGarrett Wollman }
8027b6ab19dSGarrett Wollman
8037b6ab19dSGarrett Wollman int
rn_walktree(struct radix_node_head * h,int (* f)(struct radix_node *,struct walkarg *),struct walkarg * w)8047b6ab19dSGarrett Wollman rn_walktree(struct radix_node_head *h,
8052c7a9781SMark Murray int (*f)(struct radix_node *, struct walkarg *),
8067b6ab19dSGarrett Wollman struct walkarg *w)
8077b6ab19dSGarrett Wollman {
8087b6ab19dSGarrett Wollman int error;
8097b6ab19dSGarrett Wollman struct radix_node *base, *next;
8102c7a9781SMark Murray struct radix_node *rn = h->rnh_treetop;
8117b6ab19dSGarrett Wollman /*
8127b6ab19dSGarrett Wollman * This gets complicated because we may delete the node
8137b6ab19dSGarrett Wollman * while applying the function f to it, so we need to calculate
8147b6ab19dSGarrett Wollman * the successor node in advance.
8157b6ab19dSGarrett Wollman */
8167b6ab19dSGarrett Wollman /* First time through node, go left */
8177b6ab19dSGarrett Wollman while (rn->rn_b >= 0)
8187b6ab19dSGarrett Wollman rn = rn->rn_l;
8197b6ab19dSGarrett Wollman for (;;) {
8207b6ab19dSGarrett Wollman base = rn;
8217b6ab19dSGarrett Wollman /* If at right child go back up, otherwise, go right */
8227b6ab19dSGarrett Wollman while (rn->rn_p->rn_r == rn && (rn->rn_flags & RNF_ROOT) == 0)
8237b6ab19dSGarrett Wollman rn = rn->rn_p;
8247b6ab19dSGarrett Wollman /* Find the next *leaf* since next node might vanish, too */
8257b6ab19dSGarrett Wollman for (rn = rn->rn_p->rn_r; rn->rn_b >= 0;)
8267b6ab19dSGarrett Wollman rn = rn->rn_l;
8277b6ab19dSGarrett Wollman next = rn;
8287b6ab19dSGarrett Wollman /* Process leaves */
8297b6ab19dSGarrett Wollman while ((rn = base)) {
8307b6ab19dSGarrett Wollman base = rn->rn_dupedkey;
8317b6ab19dSGarrett Wollman if (!(rn->rn_flags & RNF_ROOT) && (error = (*f)(rn, w)))
8327b6ab19dSGarrett Wollman return (error);
8337b6ab19dSGarrett Wollman }
8347b6ab19dSGarrett Wollman rn = next;
8357b6ab19dSGarrett Wollman if (rn->rn_flags & RNF_ROOT)
8367b6ab19dSGarrett Wollman return (0);
8377b6ab19dSGarrett Wollman }
8387b6ab19dSGarrett Wollman /* NOTREACHED */
8397b6ab19dSGarrett Wollman }
8407b6ab19dSGarrett Wollman
8417b6ab19dSGarrett Wollman int
rn_inithead(struct radix_node_head ** head,int off)8429940236eSPoul-Henning Kamp rn_inithead(struct radix_node_head **head, int off)
8437b6ab19dSGarrett Wollman {
8442c7a9781SMark Murray struct radix_node_head *rnh;
8452c7a9781SMark Murray struct radix_node *t, *tt, *ttt;
8467b6ab19dSGarrett Wollman if (*head)
8477b6ab19dSGarrett Wollman return (1);
84890100551SPhilippe Charnier rnh = (struct radix_node_head *)rtmalloc(sizeof(*rnh), "rn_inithead");
8497b6ab19dSGarrett Wollman Bzero(rnh, sizeof (*rnh));
8507b6ab19dSGarrett Wollman *head = rnh;
8517b6ab19dSGarrett Wollman t = rn_newpair(rn_zeros, off, rnh->rnh_nodes);
8527b6ab19dSGarrett Wollman ttt = rnh->rnh_nodes + 2;
8537b6ab19dSGarrett Wollman t->rn_r = ttt;
8547b6ab19dSGarrett Wollman t->rn_p = t;
8557b6ab19dSGarrett Wollman tt = t->rn_l;
8567b6ab19dSGarrett Wollman tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE;
8577b6ab19dSGarrett Wollman tt->rn_b = -1 - off;
8587b6ab19dSGarrett Wollman *ttt = *tt;
8597b6ab19dSGarrett Wollman ttt->rn_key = rn_ones;
8607b6ab19dSGarrett Wollman rnh->rnh_addaddr = rn_addroute;
8617b6ab19dSGarrett Wollman rnh->rnh_deladdr = rn_delete;
8627b6ab19dSGarrett Wollman rnh->rnh_matchaddr = rn_match;
8637b6ab19dSGarrett Wollman rnh->rnh_lookup = rn_lookup;
8647b6ab19dSGarrett Wollman rnh->rnh_walktree = rn_walktree;
8657b6ab19dSGarrett Wollman rnh->rnh_treetop = t;
8667b6ab19dSGarrett Wollman return (1);
8677b6ab19dSGarrett Wollman }
8687b6ab19dSGarrett Wollman
8697b6ab19dSGarrett Wollman void
rn_init(void)8707b6ab19dSGarrett Wollman rn_init(void)
8717b6ab19dSGarrett Wollman {
8727b6ab19dSGarrett Wollman char *cp, *cplim;
8737b6ab19dSGarrett Wollman if (max_keylen == 0) {
8747b6ab19dSGarrett Wollman printf("rn_init: radix functions require max_keylen be set\n");
8757b6ab19dSGarrett Wollman return;
8767b6ab19dSGarrett Wollman }
87790100551SPhilippe Charnier rn_zeros = (char *)rtmalloc(3 * max_keylen, "rn_init");
8787b6ab19dSGarrett Wollman Bzero(rn_zeros, 3 * max_keylen);
8797b6ab19dSGarrett Wollman rn_ones = cp = rn_zeros + max_keylen;
8807b6ab19dSGarrett Wollman addmask_key = cplim = rn_ones + max_keylen;
8817b6ab19dSGarrett Wollman while (cp < cplim)
8827b6ab19dSGarrett Wollman *cp++ = -1;
8839940236eSPoul-Henning Kamp if (rn_inithead(&mask_rnhead, 0) == 0)
8847b6ab19dSGarrett Wollman panic("rn_init 2");
8857b6ab19dSGarrett Wollman }
8867b6ab19dSGarrett Wollman
887