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