1c793af95Ssangeeta /* 2*bd670b35SErik Nordmark * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 3c793af95Ssangeeta * Use is subject to license terms. 4c793af95Ssangeeta * 5c793af95Ssangeeta * Copyright (c) 1988, 1989, 1993 6c793af95Ssangeeta * The Regents of the University of California. All rights reserved. 7c793af95Ssangeeta * 8c793af95Ssangeeta * Redistribution and use in source and binary forms, with or without 9c793af95Ssangeeta * modification, are permitted provided that the following conditions 10c793af95Ssangeeta * are met: 11c793af95Ssangeeta * 1. Redistributions of source code must retain the above copyright 12c793af95Ssangeeta * notice, this list of conditions and the following disclaimer. 13c793af95Ssangeeta * 2. Redistributions in binary form must reproduce the above copyright 14c793af95Ssangeeta * notice, this list of conditions and the following disclaimer in the 15c793af95Ssangeeta * documentation and/or other materials provided with the distribution. 16c793af95Ssangeeta * 4. Neither the name of the University nor the names of its contributors 17c793af95Ssangeeta * may be used to endorse or promote products derived from this software 18c793af95Ssangeeta * without specific prior written permission. 19c793af95Ssangeeta * 20c793af95Ssangeeta * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21c793af95Ssangeeta * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22c793af95Ssangeeta * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23c793af95Ssangeeta * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24c793af95Ssangeeta * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25c793af95Ssangeeta * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26c793af95Ssangeeta * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27c793af95Ssangeeta * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28c793af95Ssangeeta * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29c793af95Ssangeeta * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30c793af95Ssangeeta * SUCH DAMAGE. 31c793af95Ssangeeta * 32c793af95Ssangeeta * @(#)radix.c 8.5 (Berkeley) 5/19/95 33c793af95Ssangeeta * $FreeBSD: /repoman/r/ncvs/src/sys/net/radix.c,v 1.36.2.1 2005/01/31 23:26:23 34c793af95Ssangeeta * imp Exp $ 35c793af95Ssangeeta */ 36c793af95Ssangeeta 37c793af95Ssangeeta 38c793af95Ssangeeta /* 39c793af95Ssangeeta * Routines to build and maintain radix trees for routing lookups. 40c793af95Ssangeeta */ 41c793af95Ssangeeta #include <sys/types.h> 42c793af95Ssangeeta 43c793af95Ssangeeta #ifndef _RADIX_H_ 44c793af95Ssangeeta #include <sys/param.h> 45c793af95Ssangeeta #ifdef _KERNEL 46c793af95Ssangeeta #include <sys/lock.h> 47c793af95Ssangeeta #include <sys/mutex.h> 48c793af95Ssangeeta #include <sys/systm.h> 49c793af95Ssangeeta #include <sys/cmn_err.h> 50c793af95Ssangeeta #else 51c793af95Ssangeeta #include <assert.h> 52c793af95Ssangeeta #define ASSERT assert 53c793af95Ssangeeta #include <stdio.h> 54c793af95Ssangeeta #include <stdlib.h> 55c793af95Ssangeeta #include <syslog.h> 56c793af95Ssangeeta #include <strings.h> 57c793af95Ssangeeta #endif /* _KERNEL */ 58c793af95Ssangeeta #include <net/radix.h> 59c793af95Ssangeeta #endif 60c793af95Ssangeeta 61c793af95Ssangeeta #ifndef _KERNEL 62c793af95Ssangeeta void 63c793af95Ssangeeta panic(const char *str) 64c793af95Ssangeeta { 65c793af95Ssangeeta fprintf(stderr, "Panic - %s\n", str); 66c793af95Ssangeeta abort(); 67c793af95Ssangeeta } 68c793af95Ssangeeta #endif /* _KERNEL */ 69c793af95Ssangeeta 70c793af95Ssangeeta static int rn_walktree(struct radix_node_head *, walktree_f_t *, void *); 712679e103Ssowmini static int rn_walktree_mt(struct radix_node_head *, walktree_f_t *, 722679e103Ssowmini void *, lockf_t, lockf_t); 73c793af95Ssangeeta static struct radix_node 74c793af95Ssangeeta *rn_insert(void *, struct radix_node_head *, int *, 75c793af95Ssangeeta struct radix_node [2]), 76c793af95Ssangeeta *rn_newpair(void *, int, struct radix_node[2]), 77c793af95Ssangeeta *rn_search(void *, struct radix_node *), 78c793af95Ssangeeta *rn_search_m(void *, struct radix_node *, void *), 79c793af95Ssangeeta *rn_lookup(void *, void *, struct radix_node_head *), 80c793af95Ssangeeta *rn_match(void *, struct radix_node_head *), 81c793af95Ssangeeta *rn_match_args(void *, struct radix_node_head *, match_leaf_t *, 82c793af95Ssangeeta void *), 83c793af95Ssangeeta *rn_addmask(void *, int, int), 84c793af95Ssangeeta *rn_addroute(void *, void *, struct radix_node_head *, 85c793af95Ssangeeta struct radix_node [2]), 86c793af95Ssangeeta *rn_delete(void *, void *, struct radix_node_head *); 87c793af95Ssangeeta static boolean_t rn_refines(void *, void *); 88c793af95Ssangeeta 8940cdc2e8SAlexandr Nedvedicky /* 9040cdc2e8SAlexandr Nedvedicky * IPF also uses PATRICIA tree to manage ippools. IPF stores its own structure 9140cdc2e8SAlexandr Nedvedicky * addrfamily_t. sizeof (addrfamily_t) == 24. 9240cdc2e8SAlexandr Nedvedicky */ 9340cdc2e8SAlexandr Nedvedicky #define MAX_KEYLEN 24 94c793af95Ssangeeta static int max_keylen = MAX_KEYLEN; 95c793af95Ssangeeta 96c793af95Ssangeeta #ifdef _KERNEL 97c793af95Ssangeeta static struct kmem_cache *radix_mask_cache; /* for rn_mkfreelist */ 98c793af95Ssangeeta static struct kmem_cache *radix_node_cache; 99c793af95Ssangeeta #else 100c793af95Ssangeeta static char *radix_mask_cache, *radix_node_cache; /* dummy vars. never inited */ 101c793af95Ssangeeta #endif /* _KERNEL */ 102c793af95Ssangeeta 103c793af95Ssangeeta static struct radix_mask *rn_mkfreelist; 104c793af95Ssangeeta static struct radix_node_head *mask_rnhead; 105c793af95Ssangeeta /* 106c793af95Ssangeeta * Work area -- the following point to 2 buffers of size max_keylen, 107c793af95Ssangeeta * allocated in this order in a block of memory malloc'ed by rn_init. 108c793af95Ssangeeta * A third buffer of size MAX_KEYLEN is allocated from the stack. 109c793af95Ssangeeta */ 110c793af95Ssangeeta static char *rn_zeros, *rn_ones; 111c793af95Ssangeeta 112c793af95Ssangeeta #define MKGet(m) R_Malloc(m, radix_mask_cache, sizeof (struct radix_mask)) 113c793af95Ssangeeta #define MKFree(m) Free(m, radix_mask_cache) 114c793af95Ssangeeta #define rn_masktop (mask_rnhead->rnh_treetop) 115c793af95Ssangeeta 116c793af95Ssangeeta static boolean_t rn_lexobetter(void *m_arg, void *n_arg); 117c793af95Ssangeeta static struct radix_mask * 118c793af95Ssangeeta rn_new_radix_mask(struct radix_node *tt, 119c793af95Ssangeeta struct radix_mask *next); 120c793af95Ssangeeta static boolean_t 121c793af95Ssangeeta rn_satisfies_leaf(char *trial, struct radix_node *leaf, 122c793af95Ssangeeta int skip, match_leaf_t *rn_leaf_fn, void *rn_leaf_arg); 123c793af95Ssangeeta 124c793af95Ssangeeta #define RN_MATCHF(rn, f, arg) (f == NULL || (*f)((rn), arg)) 125c793af95Ssangeeta 126c793af95Ssangeeta /* 127c793af95Ssangeeta * The data structure for the keys is a radix tree with one way 128c793af95Ssangeeta * branching removed. The index rn_bit at an internal node n represents a bit 129c793af95Ssangeeta * position to be tested. The tree is arranged so that all descendants 130c793af95Ssangeeta * of a node n have keys whose bits all agree up to position rn_bit - 1. 131c793af95Ssangeeta * (We say the index of n is rn_bit.) 132c793af95Ssangeeta * 133c793af95Ssangeeta * There is at least one descendant which has a one bit at position rn_bit, 134c793af95Ssangeeta * and at least one with a zero there. 135c793af95Ssangeeta * 136c793af95Ssangeeta * A route is determined by a pair of key and mask. We require that the 137c793af95Ssangeeta * bit-wise logical and of the key and mask to be the key. 138c793af95Ssangeeta * We define the index of a route associated with the mask to be 139c793af95Ssangeeta * the first bit number in the mask where 0 occurs (with bit number 0 140c793af95Ssangeeta * representing the highest order bit). 141c793af95Ssangeeta * 142c793af95Ssangeeta * We say a mask is normal if every bit is 0, past the index of the mask. 143c793af95Ssangeeta * If a node n has a descendant (k, m) with index(m) == index(n) == rn_bit, 144c793af95Ssangeeta * and m is a normal mask, then the route applies to every descendant of n. 145c793af95Ssangeeta * If the index(m) < rn_bit, this implies the trailing last few bits of k 146c793af95Ssangeeta * before bit b are all 0, (and hence consequently true of every descendant 147c793af95Ssangeeta * of n), so the route applies to all descendants of the node as well. 148c793af95Ssangeeta * 149c793af95Ssangeeta * Similar logic shows that a non-normal mask m such that 150c793af95Ssangeeta * index(m) <= index(n) could potentially apply to many children of n. 151c793af95Ssangeeta * Thus, for each non-host route, we attach its mask to a list at an internal 152c793af95Ssangeeta * node as high in the tree as we can go. 153c793af95Ssangeeta * 154c793af95Ssangeeta * The present version of the code makes use of normal routes in short- 155c793af95Ssangeeta * circuiting an explict mask and compare operation when testing whether 156c793af95Ssangeeta * a key satisfies a normal route, and also in remembering the unique leaf 157c793af95Ssangeeta * that governs a subtree. 158c793af95Ssangeeta */ 159c793af95Ssangeeta 160c793af95Ssangeeta /* 161c793af95Ssangeeta * Most of the functions in this code assume that the key/mask arguments 162c793af95Ssangeeta * are sockaddr-like structures, where the first byte is an uchar_t 163c793af95Ssangeeta * indicating the size of the entire structure. 164c793af95Ssangeeta * 165c793af95Ssangeeta * To make the assumption more explicit, we use the LEN() macro to access 166c793af95Ssangeeta * this field. It is safe to pass an expression with side effects 167c793af95Ssangeeta * to LEN() as the argument is evaluated only once. 168c793af95Ssangeeta */ 169c793af95Ssangeeta #define LEN(x) (*(const uchar_t *)(x)) 170c793af95Ssangeeta 171c793af95Ssangeeta 172c793af95Ssangeeta /* 173c793af95Ssangeeta * Search a node in the tree matching the key. 174c793af95Ssangeeta */ 175c793af95Ssangeeta static struct radix_node * 176c793af95Ssangeeta rn_search(v_arg, head) 177c793af95Ssangeeta void *v_arg; 178c793af95Ssangeeta struct radix_node *head; 179c793af95Ssangeeta { 180c793af95Ssangeeta struct radix_node *x; 181c793af95Ssangeeta caddr_t v; 182c793af95Ssangeeta 183c793af95Ssangeeta for (x = head, v = v_arg; x->rn_bit >= 0; ) { 184c793af95Ssangeeta if (x->rn_bmask & v[x->rn_offset]) 185c793af95Ssangeeta x = x->rn_right; 186c793af95Ssangeeta else 187c793af95Ssangeeta x = x->rn_left; 188c793af95Ssangeeta } 189c793af95Ssangeeta return (x); 190c793af95Ssangeeta } 191c793af95Ssangeeta 192c793af95Ssangeeta /* 193c793af95Ssangeeta * Same as above, but with an additional mask. 194c793af95Ssangeeta */ 195c793af95Ssangeeta static struct radix_node * 196c793af95Ssangeeta rn_search_m(v_arg, head, m_arg) 197c793af95Ssangeeta struct radix_node *head; 198c793af95Ssangeeta void *v_arg, *m_arg; 199c793af95Ssangeeta { 200c793af95Ssangeeta struct radix_node *x; 201c793af95Ssangeeta caddr_t v = v_arg, m = m_arg; 202c793af95Ssangeeta 203c793af95Ssangeeta for (x = head; x->rn_bit >= 0; ) { 204c793af95Ssangeeta if ((x->rn_bmask & m[x->rn_offset]) && 205c793af95Ssangeeta (x->rn_bmask & v[x->rn_offset])) 206c793af95Ssangeeta x = x->rn_right; 207c793af95Ssangeeta else 208c793af95Ssangeeta x = x->rn_left; 209c793af95Ssangeeta } 210c793af95Ssangeeta return (x); 211c793af95Ssangeeta } 212c793af95Ssangeeta 213c793af95Ssangeeta /* 214c793af95Ssangeeta * Returns true if there are no bits set in n_arg that are zero in 215c793af95Ssangeeta * m_arg and the masks aren't equal. In other words, it returns true 216c793af95Ssangeeta * when m_arg is a finer-granularity netmask -- it represents a subset 217c793af95Ssangeeta * of the destinations implied by n_arg. 218c793af95Ssangeeta */ 219c793af95Ssangeeta static boolean_t 220c793af95Ssangeeta rn_refines(m_arg, n_arg) 221c793af95Ssangeeta void *m_arg, *n_arg; 222c793af95Ssangeeta { 223c793af95Ssangeeta caddr_t m = m_arg, n = n_arg; 224c793af95Ssangeeta caddr_t lim = n + LEN(n), lim2 = lim; 225c793af95Ssangeeta int longer = LEN(n++) - (int)LEN(m++); 226c793af95Ssangeeta boolean_t masks_are_equal = B_TRUE; 227c793af95Ssangeeta 228c793af95Ssangeeta if (longer > 0) 229c793af95Ssangeeta lim -= longer; 230c793af95Ssangeeta while (n < lim) { 231c793af95Ssangeeta if (*n & ~(*m)) 232c793af95Ssangeeta return (0); 233c793af95Ssangeeta if (*n++ != *m++) 234c793af95Ssangeeta masks_are_equal = B_FALSE; 235c793af95Ssangeeta } 236c793af95Ssangeeta while (n < lim2) 237c793af95Ssangeeta if (*n++) 238c793af95Ssangeeta return (B_FALSE); 239c793af95Ssangeeta if (masks_are_equal && (longer < 0)) 240c793af95Ssangeeta for (lim2 = m - longer; m < lim2; ) 241c793af95Ssangeeta if (*m++) 242c793af95Ssangeeta return (B_TRUE); 243c793af95Ssangeeta return (!masks_are_equal); 244c793af95Ssangeeta } 245c793af95Ssangeeta 246c793af95Ssangeeta static struct radix_node * 247c793af95Ssangeeta rn_lookup(v_arg, m_arg, head) 248c793af95Ssangeeta void *v_arg, *m_arg; 249c793af95Ssangeeta struct radix_node_head *head; 250c793af95Ssangeeta { 251c793af95Ssangeeta struct radix_node *x; 252c793af95Ssangeeta caddr_t netmask = NULL; 253c793af95Ssangeeta 254c793af95Ssangeeta if (m_arg) { 255c793af95Ssangeeta x = rn_addmask(m_arg, 1, head->rnh_treetop->rn_offset); 256c793af95Ssangeeta if (x == NULL) 257c793af95Ssangeeta return (NULL); 258c793af95Ssangeeta netmask = x->rn_key; 259c793af95Ssangeeta } 260c793af95Ssangeeta x = rn_match(v_arg, head); 261c793af95Ssangeeta if (x && netmask) { 262c793af95Ssangeeta while (x && x->rn_mask != netmask) 263c793af95Ssangeeta x = x->rn_dupedkey; 264c793af95Ssangeeta } 265c793af95Ssangeeta return (x); 266c793af95Ssangeeta } 267c793af95Ssangeeta 268c793af95Ssangeeta /* 269c793af95Ssangeeta * Returns true if address 'trial' has no bits differing from the 270c793af95Ssangeeta * leaf's key when compared under the leaf's mask. In other words, 271c793af95Ssangeeta * returns true when 'trial' matches leaf. 272c793af95Ssangeeta * In addition, if a rn_leaf_fn is passed in, that is used to find 273c793af95Ssangeeta * a match on conditions defined by the caller of rn_match. This is 274c793af95Ssangeeta * used by the kernel ftable to match on IRE_MATCH_* conditions. 275c793af95Ssangeeta */ 276c793af95Ssangeeta static boolean_t 277c793af95Ssangeeta rn_satisfies_leaf(trial, leaf, skip, rn_leaf_fn, rn_leaf_arg) 278c793af95Ssangeeta caddr_t trial; 279c793af95Ssangeeta struct radix_node *leaf; 280c793af95Ssangeeta int skip; 281c793af95Ssangeeta match_leaf_t *rn_leaf_fn; 282c793af95Ssangeeta void *rn_leaf_arg; 283c793af95Ssangeeta { 284c793af95Ssangeeta char *cp = trial, *cp2 = leaf->rn_key, *cp3 = leaf->rn_mask; 285c793af95Ssangeeta char *cplim; 286c793af95Ssangeeta int length = min(LEN(cp), LEN(cp2)); 287c793af95Ssangeeta 288c793af95Ssangeeta if (cp3 == 0) 289c793af95Ssangeeta cp3 = rn_ones; 290c793af95Ssangeeta else 291c793af95Ssangeeta length = min(length, LEN(cp3)); 292c793af95Ssangeeta cplim = cp + length; 293c793af95Ssangeeta cp3 += skip; 294c793af95Ssangeeta cp2 += skip; 295c793af95Ssangeeta 296c793af95Ssangeeta for (cp += skip; cp < cplim; cp++, cp2++, cp3++) 297c793af95Ssangeeta if ((*cp ^ *cp2) & *cp3) 298c793af95Ssangeeta return (B_FALSE); 299c793af95Ssangeeta 300c793af95Ssangeeta return (RN_MATCHF(leaf, rn_leaf_fn, rn_leaf_arg)); 301c793af95Ssangeeta } 302c793af95Ssangeeta 303c793af95Ssangeeta static struct radix_node * 304c793af95Ssangeeta rn_match(v_arg, head) 305c793af95Ssangeeta void *v_arg; 306c793af95Ssangeeta struct radix_node_head *head; 307c793af95Ssangeeta { 308c793af95Ssangeeta return (rn_match_args(v_arg, head, NULL, NULL)); 309c793af95Ssangeeta } 310c793af95Ssangeeta 311c793af95Ssangeeta static struct radix_node * 312c793af95Ssangeeta rn_match_args(v_arg, head, rn_leaf_fn, rn_leaf_arg) 313c793af95Ssangeeta void *v_arg; 314c793af95Ssangeeta struct radix_node_head *head; 315c793af95Ssangeeta match_leaf_t *rn_leaf_fn; 316c793af95Ssangeeta void *rn_leaf_arg; 317c793af95Ssangeeta { 318c793af95Ssangeeta caddr_t v = v_arg; 319c793af95Ssangeeta struct radix_node *t = head->rnh_treetop, *x; 320c793af95Ssangeeta caddr_t cp = v, cp2; 321c793af95Ssangeeta caddr_t cplim; 322c793af95Ssangeeta struct radix_node *saved_t, *top = t; 323c793af95Ssangeeta int off = t->rn_offset, vlen = LEN(cp), matched_off; 324c793af95Ssangeeta int test, b, rn_bit; 325c793af95Ssangeeta 326c793af95Ssangeeta /* 327c793af95Ssangeeta * Open code rn_search(v, top) to avoid overhead of extra 328c793af95Ssangeeta * subroutine call. 329c793af95Ssangeeta */ 330c793af95Ssangeeta for (; t->rn_bit >= 0; ) { 331c793af95Ssangeeta if (t->rn_bmask & cp[t->rn_offset]) 332c793af95Ssangeeta t = t->rn_right; 333c793af95Ssangeeta else 334c793af95Ssangeeta t = t->rn_left; 335c793af95Ssangeeta } 336c793af95Ssangeeta /* 337c793af95Ssangeeta * See if we match exactly as a host destination 338c793af95Ssangeeta * or at least learn how many bits match, for normal mask finesse. 339c793af95Ssangeeta * 340c793af95Ssangeeta * It doesn't hurt us to limit how many bytes to check 341c793af95Ssangeeta * to the length of the mask, since if it matches we had a genuine 342c793af95Ssangeeta * match and the leaf we have is the most specific one anyway; 343c793af95Ssangeeta * if it didn't match with a shorter length it would fail 344c793af95Ssangeeta * with a long one. This wins big for class B&C netmasks which 345c793af95Ssangeeta * are probably the most common case... 346c793af95Ssangeeta */ 347c793af95Ssangeeta if (t->rn_mask) 348c793af95Ssangeeta vlen = LEN(t->rn_mask); 349c793af95Ssangeeta cp += off; cp2 = t->rn_key + off; cplim = v + vlen; 350c793af95Ssangeeta for (; cp < cplim; cp++, cp2++) 351c793af95Ssangeeta if (*cp != *cp2) 352c793af95Ssangeeta goto keydiff; 353c793af95Ssangeeta /* 354c793af95Ssangeeta * This extra grot is in case we are explicitly asked 355c793af95Ssangeeta * to look up the default. Ugh! 356c793af95Ssangeeta * 357c793af95Ssangeeta * Never return the root node itself, it seems to cause a 358c793af95Ssangeeta * lot of confusion. 359c793af95Ssangeeta */ 360c793af95Ssangeeta if (t->rn_flags & RNF_ROOT) 361c793af95Ssangeeta t = t->rn_dupedkey; 362c793af95Ssangeeta if (t == NULL || RN_MATCHF(t, rn_leaf_fn, rn_leaf_arg)) { 363c793af95Ssangeeta return (t); 364c793af95Ssangeeta } else { 365c793af95Ssangeeta /* 366c793af95Ssangeeta * Although we found an exact match on the key, rn_leaf_fn 367c793af95Ssangeeta * is looking for some other criteria as well. Continue 368c793af95Ssangeeta * looking as if the exact match failed. 369c793af95Ssangeeta */ 370*bd670b35SErik Nordmark if (t->rn_dupedkey == NULL && 371*bd670b35SErik Nordmark (t->rn_parent->rn_flags & RNF_ROOT)) { 372*bd670b35SErik Nordmark /* no more dupedkeys and hit the top. have to give up */ 373c793af95Ssangeeta return (NULL); 374c793af95Ssangeeta } 375c793af95Ssangeeta b = 0; 376c793af95Ssangeeta goto keeplooking; 377c793af95Ssangeeta 378c793af95Ssangeeta } 379c793af95Ssangeeta keydiff: 380c793af95Ssangeeta test = (*cp ^ *cp2) & 0xff; /* find first bit that differs */ 381c793af95Ssangeeta for (b = 7; (test >>= 1) > 0; ) 382c793af95Ssangeeta b--; 383c793af95Ssangeeta keeplooking: 384c793af95Ssangeeta matched_off = cp - v; 385c793af95Ssangeeta b += matched_off << 3; 386c793af95Ssangeeta rn_bit = -1 - b; 387c793af95Ssangeeta 388c793af95Ssangeeta /* 389c793af95Ssangeeta * If there is a host route in a duped-key chain, it will be first. 390c793af95Ssangeeta */ 391c793af95Ssangeeta if ((saved_t = t)->rn_mask == 0) 392c793af95Ssangeeta t = t->rn_dupedkey; 393c793af95Ssangeeta for (; t != NULL; t = t->rn_dupedkey) { 394c793af95Ssangeeta /* 395c793af95Ssangeeta * Even if we don't match exactly as a host, 396c793af95Ssangeeta * we may match if the leaf we wound up at is 397c793af95Ssangeeta * a route to a net. 398c793af95Ssangeeta */ 399c793af95Ssangeeta 400c793af95Ssangeeta if (t->rn_flags & RNF_NORMAL) { 401c793af95Ssangeeta if ((rn_bit <= t->rn_bit) && 402c793af95Ssangeeta RN_MATCHF(t, rn_leaf_fn, rn_leaf_arg)) { 403c793af95Ssangeeta return (t); 404c793af95Ssangeeta } 405c793af95Ssangeeta } else if (rn_satisfies_leaf(v, t, matched_off, rn_leaf_fn, 406c793af95Ssangeeta rn_leaf_arg)) { 407c793af95Ssangeeta return (t); 408c793af95Ssangeeta } 409c793af95Ssangeeta } 410c793af95Ssangeeta t = saved_t; 411c793af95Ssangeeta /* start searching up the tree */ 412c793af95Ssangeeta do { 413c793af95Ssangeeta struct radix_mask *m; 414c793af95Ssangeeta 415c793af95Ssangeeta t = t->rn_parent; 416c793af95Ssangeeta m = t->rn_mklist; 417c793af95Ssangeeta /* 418c793af95Ssangeeta * If non-contiguous masks ever become important 419c793af95Ssangeeta * we can restore the masking and open coding of 420c793af95Ssangeeta * the search and satisfaction test and put the 421c793af95Ssangeeta * calculation of "off" back before the "do". 422c793af95Ssangeeta */ 423c793af95Ssangeeta while (m) { 424c793af95Ssangeeta if (m->rm_flags & RNF_NORMAL) { 425c793af95Ssangeeta if ((rn_bit <= m->rm_bit) && 426c793af95Ssangeeta RN_MATCHF(m->rm_leaf, rn_leaf_fn, 427c793af95Ssangeeta rn_leaf_arg)) { 428c793af95Ssangeeta return (m->rm_leaf); 429c793af95Ssangeeta } 430c793af95Ssangeeta } else { 431c793af95Ssangeeta off = min(t->rn_offset, matched_off); 432c793af95Ssangeeta x = rn_search_m(v, t, m->rm_mask); 433c793af95Ssangeeta while (x != NULL && x->rn_mask != m->rm_mask) 434c793af95Ssangeeta x = x->rn_dupedkey; 435c793af95Ssangeeta if (x && rn_satisfies_leaf(v, x, off, 436c793af95Ssangeeta rn_leaf_fn, rn_leaf_arg)) { 437c793af95Ssangeeta return (x); 438c793af95Ssangeeta } 439c793af95Ssangeeta } 440c793af95Ssangeeta m = m->rm_mklist; 441c793af95Ssangeeta } 442c793af95Ssangeeta } while (t != top); 443c793af95Ssangeeta return (0); 444c793af95Ssangeeta } 445c793af95Ssangeeta 446c793af95Ssangeeta /* 447c793af95Ssangeeta * Whenever we add a new leaf to the tree, we also add a parent node, 448c793af95Ssangeeta * so we allocate them as an array of two elements: the first one must be 449c793af95Ssangeeta * the leaf (see RNTORT() in route.c), the second one is the parent. 450c793af95Ssangeeta * This routine initializes the relevant fields of the nodes, so that 451c793af95Ssangeeta * the leaf is the left child of the parent node, and both nodes have 452c793af95Ssangeeta * (almost) all all fields filled as appropriate. 453c793af95Ssangeeta * The function returns a pointer to the parent node. 454c793af95Ssangeeta */ 455c793af95Ssangeeta 456c793af95Ssangeeta static struct radix_node * 457c793af95Ssangeeta rn_newpair(v, b, nodes) 458c793af95Ssangeeta void *v; 459c793af95Ssangeeta int b; 460c793af95Ssangeeta struct radix_node nodes[2]; 461c793af95Ssangeeta { 462c793af95Ssangeeta struct radix_node *tt = nodes, *t = tt + 1; 463c793af95Ssangeeta 464c793af95Ssangeeta t->rn_bit = b; 465c793af95Ssangeeta t->rn_bmask = 0x80 >> (b & 7); 466c793af95Ssangeeta t->rn_left = tt; 467c793af95Ssangeeta t->rn_offset = b >> 3; 468c793af95Ssangeeta 469c793af95Ssangeeta /* 470c793af95Ssangeeta * t->rn_parent, r->rn_right, tt->rn_mask, tt->rn_dupedkey 471c793af95Ssangeeta * and tt->rn_bmask must have been zeroed by caller. 472c793af95Ssangeeta */ 473c793af95Ssangeeta tt->rn_bit = -1; 474c793af95Ssangeeta tt->rn_key = v; 475c793af95Ssangeeta tt->rn_parent = t; 476c793af95Ssangeeta tt->rn_flags = t->rn_flags = RNF_ACTIVE; 477c793af95Ssangeeta tt->rn_mklist = t->rn_mklist = 0; 478c793af95Ssangeeta return (t); 479c793af95Ssangeeta } 480c793af95Ssangeeta 481c793af95Ssangeeta static struct radix_node * 482c793af95Ssangeeta rn_insert(v_arg, head, dupentry, nodes) 483c793af95Ssangeeta void *v_arg; 484c793af95Ssangeeta struct radix_node_head *head; 485c793af95Ssangeeta int *dupentry; 486c793af95Ssangeeta struct radix_node nodes[2]; 487c793af95Ssangeeta { 488c793af95Ssangeeta caddr_t v = v_arg; 489c793af95Ssangeeta struct radix_node *top = head->rnh_treetop; 490*bd670b35SErik Nordmark struct radix_node *p, *x; 491c793af95Ssangeeta int head_off = top->rn_offset, vlen = (int)LEN(v); 492c793af95Ssangeeta struct radix_node *t = rn_search(v_arg, top); 493c793af95Ssangeeta caddr_t cp = v + head_off; 494c793af95Ssangeeta int b; 495c793af95Ssangeeta struct radix_node *tt; 496c793af95Ssangeeta caddr_t cp2 = t->rn_key + head_off; 497c793af95Ssangeeta int cmp_res; 498c793af95Ssangeeta caddr_t cplim = v + vlen; 499c793af95Ssangeeta 500*bd670b35SErik Nordmark /* 501*bd670b35SErik Nordmark * Find first bit at which v and t->rn_key differ 502*bd670b35SErik Nordmark */ 503c793af95Ssangeeta while (cp < cplim) 504c793af95Ssangeeta if (*cp2++ != *cp++) 505c793af95Ssangeeta goto on1; 506c793af95Ssangeeta *dupentry = 1; 507c793af95Ssangeeta return (t); 508c793af95Ssangeeta on1: 509c793af95Ssangeeta *dupentry = 0; 510c793af95Ssangeeta cmp_res = (cp[-1] ^ cp2[-1]) & 0xff; 511*bd670b35SErik Nordmark /* 512*bd670b35SErik Nordmark * (cp - v) is the number of bytes where the match is relevant. 513*bd670b35SErik Nordmark * Multiply by 8 to get number of bits. Then reduce this number 514*bd670b35SErik Nordmark * by the trailing bits in the last byte where we have a match 515*bd670b35SErik Nordmark * by looking at (cmp_res >> 1) in each iteration below. 516*bd670b35SErik Nordmark * Note that v starts at the beginning of the key, so, when key 517*bd670b35SErik Nordmark * is a sockaddr structure, the preliminary len/family/port bytes 518*bd670b35SErik Nordmark * are accounted for. 519*bd670b35SErik Nordmark */ 520c793af95Ssangeeta for (b = (cp - v) << 3; cmp_res; b--) 521c793af95Ssangeeta cmp_res >>= 1; 522c793af95Ssangeeta cp = v; 523*bd670b35SErik Nordmark x = top; 524c793af95Ssangeeta do { 525c793af95Ssangeeta p = x; 526c793af95Ssangeeta if (cp[x->rn_offset] & x->rn_bmask) 527c793af95Ssangeeta x = x->rn_right; 528c793af95Ssangeeta else 529c793af95Ssangeeta x = x->rn_left; 530c793af95Ssangeeta } while (b > (unsigned)x->rn_bit); 531c793af95Ssangeeta /* x->rn_bit < b && x->rn_bit >= 0 */ 532*bd670b35SErik Nordmark /* 533*bd670b35SErik Nordmark * now the rightmost bit where v and rn_key differ (b) is < 534*bd670b35SErik Nordmark * x->rn_bit. 535*bd670b35SErik Nordmark * 536*bd670b35SErik Nordmark * We will add a new branch at p. b cannot equal x->rn_bit 537*bd670b35SErik Nordmark * because we know we didn't find a duplicated key. 538*bd670b35SErik Nordmark * The tree will be re-adjusted so that t is inserted between p 539*bd670b35SErik Nordmark * and x. 540*bd670b35SErik Nordmark */ 541c793af95Ssangeeta t = rn_newpair(v_arg, b, nodes); 542c793af95Ssangeeta tt = t->rn_left; 543c793af95Ssangeeta if ((cp[p->rn_offset] & p->rn_bmask) == 0) 544c793af95Ssangeeta p->rn_left = t; 545c793af95Ssangeeta else 546c793af95Ssangeeta p->rn_right = t; 547c793af95Ssangeeta x->rn_parent = t; 548c793af95Ssangeeta t->rn_parent = p; 549c793af95Ssangeeta if ((cp[t->rn_offset] & t->rn_bmask) == 0) { 550c793af95Ssangeeta t->rn_right = x; 551c793af95Ssangeeta } else { 552c793af95Ssangeeta t->rn_right = tt; 553c793af95Ssangeeta t->rn_left = x; 554c793af95Ssangeeta } 555c793af95Ssangeeta return (tt); 556c793af95Ssangeeta } 557c793af95Ssangeeta 558c793af95Ssangeeta static struct radix_node * 559c793af95Ssangeeta rn_addmask(n_arg, search, skip) 560c793af95Ssangeeta int search, skip; 561c793af95Ssangeeta void *n_arg; 562c793af95Ssangeeta { 563c793af95Ssangeeta caddr_t netmask = (caddr_t)n_arg; 564c793af95Ssangeeta struct radix_node *x; 565c793af95Ssangeeta caddr_t cp, cplim; 566c793af95Ssangeeta int b = 0, mlen, j; 567c793af95Ssangeeta int maskduplicated, m0, isnormal; 568c793af95Ssangeeta struct radix_node *saved_x; 569c793af95Ssangeeta int last_zeroed = 0; 570c793af95Ssangeeta char addmask_key[MAX_KEYLEN]; 571c793af95Ssangeeta 572c793af95Ssangeeta if ((mlen = LEN(netmask)) > max_keylen) 573c793af95Ssangeeta mlen = max_keylen; 574c793af95Ssangeeta if (skip == 0) 575c793af95Ssangeeta skip = 1; 576c793af95Ssangeeta if (mlen <= skip) 577c793af95Ssangeeta return (mask_rnhead->rnh_nodes); 578c793af95Ssangeeta if (skip > 1) 579c793af95Ssangeeta bcopy(rn_ones + 1, addmask_key + 1, skip - 1); 580c793af95Ssangeeta if ((m0 = mlen) > skip) 581c793af95Ssangeeta bcopy(netmask + skip, addmask_key + skip, mlen - skip); 582c793af95Ssangeeta /* 583c793af95Ssangeeta * Trim trailing zeroes. 584c793af95Ssangeeta */ 585c793af95Ssangeeta for (cp = addmask_key + mlen; (cp > addmask_key) && cp[-1] == 0; ) 586c793af95Ssangeeta cp--; 587c793af95Ssangeeta mlen = cp - addmask_key; 588c793af95Ssangeeta if (mlen <= skip) { 589c793af95Ssangeeta if (m0 >= last_zeroed) 590c793af95Ssangeeta last_zeroed = mlen; 591c793af95Ssangeeta return (mask_rnhead->rnh_nodes); 592c793af95Ssangeeta } 593c793af95Ssangeeta if (m0 < last_zeroed) 594c793af95Ssangeeta bzero(addmask_key + m0, last_zeroed - m0); 595c793af95Ssangeeta *addmask_key = last_zeroed = mlen; 596c793af95Ssangeeta x = rn_search(addmask_key, rn_masktop); 597c793af95Ssangeeta if (bcmp(addmask_key, x->rn_key, mlen) != 0) 598c793af95Ssangeeta x = 0; 599c793af95Ssangeeta if (x || search) 600c793af95Ssangeeta return (x); 601c793af95Ssangeeta R_Zalloc(x, radix_node_cache, max_keylen + 2 * sizeof (*x)); 602c793af95Ssangeeta 603c793af95Ssangeeta if ((saved_x = x) == 0) 604c793af95Ssangeeta return (0); 605c793af95Ssangeeta netmask = cp = (caddr_t)(x + 2); 606c793af95Ssangeeta bcopy(addmask_key, cp, mlen); 607c793af95Ssangeeta x = rn_insert(cp, mask_rnhead, &maskduplicated, x); 608c793af95Ssangeeta if (maskduplicated) { 609c793af95Ssangeeta #ifdef _KERNEL 610c793af95Ssangeeta cmn_err(CE_WARN, "rn_addmask: mask impossibly already in tree"); 611c793af95Ssangeeta #else 612c793af95Ssangeeta syslog(LOG_ERR, "rn_addmask: mask impossibly already in tree"); 613c793af95Ssangeeta #endif /* _KERNEL */ 614c793af95Ssangeeta Free(saved_x, radix_node_cache); 615c793af95Ssangeeta return (x); 616c793af95Ssangeeta } 617c793af95Ssangeeta /* 618c793af95Ssangeeta * Calculate index of mask, and check for normalcy. 619c793af95Ssangeeta * First find the first byte with a 0 bit, then if there are 620c793af95Ssangeeta * more bits left (remember we already trimmed the trailing 0's), 621c793af95Ssangeeta * the pattern must be one of those in normal_chars[], or we have 622c793af95Ssangeeta * a non-contiguous mask. 623c793af95Ssangeeta */ 624c793af95Ssangeeta cplim = netmask + mlen; 625c793af95Ssangeeta isnormal = 1; 626c793af95Ssangeeta for (cp = netmask + skip; (cp < cplim) && *(uchar_t *)cp == 0xff; ) 627c793af95Ssangeeta cp++; 628c793af95Ssangeeta if (cp != cplim) { 629c793af95Ssangeeta static uint8_t normal_chars[] = { 630c793af95Ssangeeta 0, 0x80, 0xc0, 0xe0, 0xf0, 0xf8, 0xfc, 0xfe, 0xff}; 631c793af95Ssangeeta 632c793af95Ssangeeta for (j = 0x80; (j & *cp) != 0; j >>= 1) 633c793af95Ssangeeta b++; 634c793af95Ssangeeta if (*cp != normal_chars[b] || cp != (cplim - 1)) 635c793af95Ssangeeta isnormal = 0; 636c793af95Ssangeeta } 637c793af95Ssangeeta b += (cp - netmask) << 3; 638c793af95Ssangeeta x->rn_bit = -1 - b; 639c793af95Ssangeeta if (isnormal) 640c793af95Ssangeeta x->rn_flags |= RNF_NORMAL; 641c793af95Ssangeeta return (x); 642c793af95Ssangeeta } 643c793af95Ssangeeta 644c793af95Ssangeeta /* arbitrary ordering for non-contiguous masks */ 645c793af95Ssangeeta static boolean_t 646c793af95Ssangeeta rn_lexobetter(m_arg, n_arg) 647c793af95Ssangeeta void *m_arg, *n_arg; 648c793af95Ssangeeta { 649c793af95Ssangeeta uchar_t *mp = m_arg, *np = n_arg, *lim; 650c793af95Ssangeeta 651c793af95Ssangeeta if (LEN(mp) > LEN(np)) 652c793af95Ssangeeta /* not really, but need to check longer one first */ 653c793af95Ssangeeta return (B_TRUE); 654c793af95Ssangeeta if (LEN(mp) == LEN(np)) 655c793af95Ssangeeta for (lim = mp + LEN(mp); mp < lim; ) 656c793af95Ssangeeta if (*mp++ > *np++) 657c793af95Ssangeeta return (B_TRUE); 658c793af95Ssangeeta return (B_FALSE); 659c793af95Ssangeeta } 660c793af95Ssangeeta 661c793af95Ssangeeta static struct radix_mask * 662c793af95Ssangeeta rn_new_radix_mask(tt, next) 663c793af95Ssangeeta struct radix_node *tt; 664c793af95Ssangeeta struct radix_mask *next; 665c793af95Ssangeeta { 666c793af95Ssangeeta struct radix_mask *m; 667c793af95Ssangeeta 668c793af95Ssangeeta MKGet(m); 669c793af95Ssangeeta if (m == 0) { 670c793af95Ssangeeta #ifndef _KERNEL 671c793af95Ssangeeta syslog(LOG_ERR, "Mask for route not entered\n"); 672c793af95Ssangeeta #endif /* _KERNEL */ 673c793af95Ssangeeta return (0); 674c793af95Ssangeeta } 675c793af95Ssangeeta bzero(m, sizeof (*m)); 676c793af95Ssangeeta m->rm_bit = tt->rn_bit; 677c793af95Ssangeeta m->rm_flags = tt->rn_flags; 678c793af95Ssangeeta if (tt->rn_flags & RNF_NORMAL) 679c793af95Ssangeeta m->rm_leaf = tt; 680c793af95Ssangeeta else 681c793af95Ssangeeta m->rm_mask = tt->rn_mask; 682c793af95Ssangeeta m->rm_mklist = next; 683c793af95Ssangeeta tt->rn_mklist = m; 684c793af95Ssangeeta return (m); 685c793af95Ssangeeta } 686c793af95Ssangeeta 687c793af95Ssangeeta static struct radix_node * 688c793af95Ssangeeta rn_addroute(v_arg, n_arg, head, treenodes) 689c793af95Ssangeeta void *v_arg, *n_arg; 690c793af95Ssangeeta struct radix_node_head *head; 691c793af95Ssangeeta struct radix_node treenodes[2]; 692c793af95Ssangeeta { 693c793af95Ssangeeta caddr_t v = (caddr_t)v_arg, netmask = (caddr_t)n_arg; 694c793af95Ssangeeta struct radix_node *t, *x = 0, *tt; 695c793af95Ssangeeta struct radix_node *saved_tt, *top = head->rnh_treetop; 696c793af95Ssangeeta short b = 0, b_leaf = 0; 697c793af95Ssangeeta int keyduplicated; 698c793af95Ssangeeta caddr_t mmask; 699c793af95Ssangeeta struct radix_mask *m, **mp; 700c793af95Ssangeeta 701c793af95Ssangeeta /* 702c793af95Ssangeeta * In dealing with non-contiguous masks, there may be 703c793af95Ssangeeta * many different routes which have the same mask. 704c793af95Ssangeeta * We will find it useful to have a unique pointer to 705c793af95Ssangeeta * the mask to speed avoiding duplicate references at 706c793af95Ssangeeta * nodes and possibly save time in calculating indices. 707c793af95Ssangeeta */ 708c793af95Ssangeeta if (netmask) { 709c793af95Ssangeeta if ((x = rn_addmask(netmask, 0, top->rn_offset)) == 0) 710c793af95Ssangeeta return (0); 711c793af95Ssangeeta b_leaf = x->rn_bit; 712c793af95Ssangeeta b = -1 - x->rn_bit; 713c793af95Ssangeeta netmask = x->rn_key; 714c793af95Ssangeeta } 715c793af95Ssangeeta /* 716c793af95Ssangeeta * Deal with duplicated keys: attach node to previous instance 717c793af95Ssangeeta */ 718c793af95Ssangeeta saved_tt = tt = rn_insert(v, head, &keyduplicated, treenodes); 719c793af95Ssangeeta if (keyduplicated) { 720c793af95Ssangeeta for (t = tt; tt; t = tt, tt = tt->rn_dupedkey) { 721c793af95Ssangeeta if (tt->rn_mask == netmask) 722c793af95Ssangeeta return (0); 723c793af95Ssangeeta if (netmask == 0 || 724c793af95Ssangeeta (tt->rn_mask && 725c793af95Ssangeeta /* index (netmask) > node */ 726c793af95Ssangeeta ((b_leaf < tt->rn_bit) || 727c793af95Ssangeeta rn_refines(netmask, tt->rn_mask) || 728c793af95Ssangeeta rn_lexobetter(netmask, tt->rn_mask)))) 729c793af95Ssangeeta break; 730c793af95Ssangeeta } 731c793af95Ssangeeta /* 732c793af95Ssangeeta * If the mask is not duplicated, we wouldn't 733c793af95Ssangeeta * find it among possible duplicate key entries 734c793af95Ssangeeta * anyway, so the above test doesn't hurt. 735c793af95Ssangeeta * 736*bd670b35SErik Nordmark * Insert treenodes before tt. 737*bd670b35SErik Nordmark * 738c793af95Ssangeeta * We sort the masks for a duplicated key the same way as 739c793af95Ssangeeta * in a masklist -- most specific to least specific. 740c793af95Ssangeeta * This may require the unfortunate nuisance of relocating 741c793af95Ssangeeta * the head of the list. 742c793af95Ssangeeta * 743c793af95Ssangeeta * We also reverse, or doubly link the list through the 744c793af95Ssangeeta * parent pointer. 745c793af95Ssangeeta */ 746c793af95Ssangeeta if (tt == saved_tt) { 747c793af95Ssangeeta struct radix_node *xx = x; 748c793af95Ssangeeta /* link in at head of list */ 749c793af95Ssangeeta (tt = treenodes)->rn_dupedkey = t; 750c793af95Ssangeeta tt->rn_flags = t->rn_flags; 751c793af95Ssangeeta tt->rn_parent = x = t->rn_parent; 752c793af95Ssangeeta t->rn_parent = tt; /* parent */ 753c793af95Ssangeeta if (x->rn_left == t) 754c793af95Ssangeeta x->rn_left = tt; 755c793af95Ssangeeta else 756c793af95Ssangeeta x->rn_right = tt; 757c793af95Ssangeeta saved_tt = tt; x = xx; 758c793af95Ssangeeta } else { 759c793af95Ssangeeta (tt = treenodes)->rn_dupedkey = t->rn_dupedkey; 760c793af95Ssangeeta t->rn_dupedkey = tt; 761c793af95Ssangeeta /* Set rn_parent value for tt and tt->rn_dupedkey */ 762c793af95Ssangeeta tt->rn_parent = t; 763c793af95Ssangeeta if (tt->rn_dupedkey) 764c793af95Ssangeeta tt->rn_dupedkey->rn_parent = tt; 765c793af95Ssangeeta } 766c793af95Ssangeeta tt->rn_key = v; 767c793af95Ssangeeta tt->rn_bit = -1; 768c793af95Ssangeeta tt->rn_flags = RNF_ACTIVE; 769c793af95Ssangeeta } 770c793af95Ssangeeta /* 771c793af95Ssangeeta * Put mask in tree. 772c793af95Ssangeeta */ 773c793af95Ssangeeta if (netmask) { 774c793af95Ssangeeta tt->rn_mask = netmask; 775c793af95Ssangeeta tt->rn_bit = x->rn_bit; 776c793af95Ssangeeta tt->rn_flags |= x->rn_flags & RNF_NORMAL; 777c793af95Ssangeeta } 778*bd670b35SErik Nordmark /* BEGIN CSTYLED */ 779*bd670b35SErik Nordmark /* 780*bd670b35SErik Nordmark * at this point the parent-child relationship for p, t, x, tt is 781*bd670b35SErik Nordmark * one of the following: 782*bd670b35SErik Nordmark * p p 783*bd670b35SErik Nordmark * : (left/right child) : 784*bd670b35SErik Nordmark * : : 785*bd670b35SErik Nordmark * t t 786*bd670b35SErik Nordmark * / \ / \ 787*bd670b35SErik Nordmark * x tt tt x 788*bd670b35SErik Nordmark * 789*bd670b35SErik Nordmark * tt == saved_tt returned by rn_insert(). 790*bd670b35SErik Nordmark */ 791*bd670b35SErik Nordmark /* END CSTYLED */ 792c793af95Ssangeeta t = saved_tt->rn_parent; 793c793af95Ssangeeta if (keyduplicated) 794c793af95Ssangeeta goto key_exists; 795c793af95Ssangeeta b_leaf = -1 - t->rn_bit; 796*bd670b35SErik Nordmark /* 797*bd670b35SErik Nordmark * b_leaf is now normalized to be in the leaf rn_bit format 798*bd670b35SErik Nordmark * (it is the rn_bit value of a leaf corresponding to netmask 799*bd670b35SErik Nordmark * of t->rn_bit). 800*bd670b35SErik Nordmark */ 801c793af95Ssangeeta if (t->rn_right == saved_tt) 802c793af95Ssangeeta x = t->rn_left; 803c793af95Ssangeeta else 804c793af95Ssangeeta x = t->rn_right; 805*bd670b35SErik Nordmark /* 806*bd670b35SErik Nordmark * Promote general routes from below. 807*bd670b35SErik Nordmark * Identify the less specific netmasks and add them to t->rm_mklist 808*bd670b35SErik Nordmark */ 809c793af95Ssangeeta if (x->rn_bit < 0) { 810*bd670b35SErik Nordmark /* x is the sibling node. it is a leaf node. */ 811c793af95Ssangeeta for (mp = &t->rn_mklist; x; x = x->rn_dupedkey) 812*bd670b35SErik Nordmark if (x->rn_mask && (x->rn_bit >= b_leaf) && 813*bd670b35SErik Nordmark x->rn_mklist == 0) { 814*bd670b35SErik Nordmark /* 815*bd670b35SErik Nordmark * x is the first node in the dupedkey chain 816*bd670b35SErik Nordmark * without a mklist, and with a shorter mask 817*bd670b35SErik Nordmark * than b_leaf. Create a radix_mask 818*bd670b35SErik Nordmark * corresponding to x's mask and add it to 819*bd670b35SErik Nordmark * t's rn_mklist. The mask list gets created 820*bd670b35SErik Nordmark * in decreasing order of mask length. 821*bd670b35SErik Nordmark */ 822c793af95Ssangeeta *mp = m = rn_new_radix_mask(x, 0); 823c793af95Ssangeeta if (m) 824c793af95Ssangeeta mp = &m->rm_mklist; 825c793af95Ssangeeta } 826c793af95Ssangeeta } else if (x->rn_mklist) { 827c793af95Ssangeeta /* 828c793af95Ssangeeta * Skip over masks whose index is > that of new node 829c793af95Ssangeeta */ 830c793af95Ssangeeta for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist) 831c793af95Ssangeeta if (m->rm_bit >= b_leaf) 832c793af95Ssangeeta break; 833c793af95Ssangeeta t->rn_mklist = m; *mp = 0; 834c793af95Ssangeeta } 835c793af95Ssangeeta key_exists: 836c793af95Ssangeeta /* Add new route to highest possible ancestor's list */ 837c793af95Ssangeeta if ((netmask == 0) || (b > t->rn_bit)) 838c793af95Ssangeeta return (tt); /* can't lift at all */ 839c793af95Ssangeeta b_leaf = tt->rn_bit; 840*bd670b35SErik Nordmark /* b is the index of the netmask */ 841c793af95Ssangeeta do { 842c793af95Ssangeeta x = t; 843c793af95Ssangeeta t = t->rn_parent; 844c793af95Ssangeeta } while (b <= t->rn_bit && x != top); 845c793af95Ssangeeta /* 846c793af95Ssangeeta * Search through routes associated with node to 847c793af95Ssangeeta * insert new route according to index. 848c793af95Ssangeeta * Need same criteria as when sorting dupedkeys to avoid 849c793af95Ssangeeta * double loop on deletion. 850c793af95Ssangeeta */ 851c793af95Ssangeeta for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist) { 852c793af95Ssangeeta if (m->rm_bit < b_leaf) 853c793af95Ssangeeta continue; 854c793af95Ssangeeta if (m->rm_bit > b_leaf) 855c793af95Ssangeeta break; 856c793af95Ssangeeta if (m->rm_flags & RNF_NORMAL) { 857c793af95Ssangeeta mmask = m->rm_leaf->rn_mask; 858c793af95Ssangeeta if (tt->rn_flags & RNF_NORMAL) { 859c793af95Ssangeeta #ifdef _KERNEL 860c793af95Ssangeeta cmn_err(CE_WARN, "Non-unique normal route, " 861c793af95Ssangeeta "mask not entered\n"); 862c793af95Ssangeeta #else 863c793af95Ssangeeta syslog(LOG_ERR, "Non-unique normal route, " 864c793af95Ssangeeta "mask not entered\n"); 865c793af95Ssangeeta #endif /* _KERNEL */ 866c793af95Ssangeeta return (tt); 867c793af95Ssangeeta } 868c793af95Ssangeeta } else 869c793af95Ssangeeta mmask = m->rm_mask; 870c793af95Ssangeeta if (mmask == netmask) { 871c793af95Ssangeeta m->rm_refs++; 872c793af95Ssangeeta tt->rn_mklist = m; 873c793af95Ssangeeta return (tt); 874c793af95Ssangeeta } 875c793af95Ssangeeta if (rn_refines(netmask, mmask) || 876c793af95Ssangeeta rn_lexobetter(netmask, mmask)) 877c793af95Ssangeeta break; 878c793af95Ssangeeta } 879c793af95Ssangeeta *mp = rn_new_radix_mask(tt, *mp); 880c793af95Ssangeeta return (tt); 881c793af95Ssangeeta } 882c793af95Ssangeeta 883c793af95Ssangeeta static struct radix_node * 884c793af95Ssangeeta rn_delete(v_arg, netmask_arg, head) 885c793af95Ssangeeta void *v_arg, *netmask_arg; 886c793af95Ssangeeta struct radix_node_head *head; 887c793af95Ssangeeta { 888c793af95Ssangeeta struct radix_node *t, *p, *x, *tt; 889c793af95Ssangeeta struct radix_mask *m, *saved_m, **mp; 890c793af95Ssangeeta struct radix_node *dupedkey, *saved_tt, *top; 891c793af95Ssangeeta caddr_t v, netmask; 892c793af95Ssangeeta int b, head_off, vlen; 893c793af95Ssangeeta 894c793af95Ssangeeta v = v_arg; 895c793af95Ssangeeta netmask = netmask_arg; 896c793af95Ssangeeta x = head->rnh_treetop; 897c793af95Ssangeeta tt = rn_search(v, x); 898c793af95Ssangeeta head_off = x->rn_offset; 899c793af95Ssangeeta vlen = LEN(v); 900c793af95Ssangeeta saved_tt = tt; 901c793af95Ssangeeta top = x; 902c793af95Ssangeeta if (tt == 0 || 903c793af95Ssangeeta bcmp(v + head_off, tt->rn_key + head_off, vlen - head_off)) 904c793af95Ssangeeta return (0); 905c793af95Ssangeeta /* 906c793af95Ssangeeta * Delete our route from mask lists. 907c793af95Ssangeeta */ 908c793af95Ssangeeta if (netmask) { 909c793af95Ssangeeta if ((x = rn_addmask(netmask, 1, head_off)) == 0) 910c793af95Ssangeeta return (0); 911c793af95Ssangeeta netmask = x->rn_key; 912c793af95Ssangeeta while (tt->rn_mask != netmask) 913c793af95Ssangeeta if ((tt = tt->rn_dupedkey) == 0) 914c793af95Ssangeeta return (0); 915c793af95Ssangeeta } 916c793af95Ssangeeta if (tt->rn_mask == 0 || (saved_m = m = tt->rn_mklist) == 0) 917c793af95Ssangeeta goto on1; 918c793af95Ssangeeta if (tt->rn_flags & RNF_NORMAL) { 919c793af95Ssangeeta if (m->rm_leaf != tt || m->rm_refs > 0) { 920c793af95Ssangeeta #ifdef _KERNEL 921c793af95Ssangeeta cmn_err(CE_WARN, 922c793af95Ssangeeta "rn_delete: inconsistent annotation\n"); 923c793af95Ssangeeta #else 924c793af95Ssangeeta syslog(LOG_ERR, "rn_delete: inconsistent annotation\n"); 925c793af95Ssangeeta #endif /* _KERNEL */ 926c793af95Ssangeeta return (0); /* dangling ref could cause disaster */ 927c793af95Ssangeeta } 928c793af95Ssangeeta } else { 929c793af95Ssangeeta if (m->rm_mask != tt->rn_mask) { 930c793af95Ssangeeta #ifdef _KERNEL 931c793af95Ssangeeta cmn_err(CE_WARN, 932c793af95Ssangeeta "rn_delete: inconsistent annotation 2\n"); 933c793af95Ssangeeta #else 934c793af95Ssangeeta syslog(LOG_ERR, 935c793af95Ssangeeta "rn_delete: inconsistent annotation 2\n"); 936c793af95Ssangeeta #endif /* _KERNEL */ 937c793af95Ssangeeta goto on1; 938c793af95Ssangeeta } 939c793af95Ssangeeta if (--m->rm_refs >= 0) 940c793af95Ssangeeta goto on1; 941c793af95Ssangeeta } 942c793af95Ssangeeta b = -1 - tt->rn_bit; 943c793af95Ssangeeta t = saved_tt->rn_parent; 944c793af95Ssangeeta if (b > t->rn_bit) 945c793af95Ssangeeta goto on1; /* Wasn't lifted at all */ 946c793af95Ssangeeta do { 947c793af95Ssangeeta x = t; 948c793af95Ssangeeta t = t->rn_parent; 949c793af95Ssangeeta } while (b <= t->rn_bit && x != top); 950c793af95Ssangeeta for (mp = &x->rn_mklist; (m = *mp) != NULL; mp = &m->rm_mklist) 951c793af95Ssangeeta if (m == saved_m) { 952c793af95Ssangeeta *mp = m->rm_mklist; 953c793af95Ssangeeta MKFree(m); 954c793af95Ssangeeta break; 955c793af95Ssangeeta } 956c793af95Ssangeeta if (m == 0) { 957c793af95Ssangeeta #ifdef _KERNEL 958c793af95Ssangeeta cmn_err(CE_WARN, "rn_delete: couldn't find our annotation\n"); 959c793af95Ssangeeta #else 960c793af95Ssangeeta syslog(LOG_ERR, "rn_delete: couldn't find our annotation\n"); 961c793af95Ssangeeta #endif /* _KERNEL */ 962c793af95Ssangeeta if (tt->rn_flags & RNF_NORMAL) 963c793af95Ssangeeta return (0); /* Dangling ref to us */ 964c793af95Ssangeeta } 965c793af95Ssangeeta on1: 966c793af95Ssangeeta /* 967c793af95Ssangeeta * Eliminate us from tree 968c793af95Ssangeeta */ 969c793af95Ssangeeta if (tt->rn_flags & RNF_ROOT) 970c793af95Ssangeeta return (0); 971c793af95Ssangeeta t = tt->rn_parent; 972c793af95Ssangeeta dupedkey = saved_tt->rn_dupedkey; 973c793af95Ssangeeta if (dupedkey) { 974c793af95Ssangeeta /* 975c793af95Ssangeeta * Here, tt is the deletion target and 976c793af95Ssangeeta * saved_tt is the head of the dupekey chain. 977c793af95Ssangeeta */ 978c793af95Ssangeeta if (tt == saved_tt) { 979c793af95Ssangeeta /* remove from head of chain */ 980c793af95Ssangeeta x = dupedkey; x->rn_parent = t; 981c793af95Ssangeeta if (t->rn_left == tt) 982c793af95Ssangeeta t->rn_left = x; 983c793af95Ssangeeta else 984c793af95Ssangeeta t->rn_right = x; 985c793af95Ssangeeta } else { 986c793af95Ssangeeta /* find node in front of tt on the chain */ 987c793af95Ssangeeta for (x = p = saved_tt; p && p->rn_dupedkey != tt; ) 988c793af95Ssangeeta p = p->rn_dupedkey; 989c793af95Ssangeeta if (p) { 990c793af95Ssangeeta p->rn_dupedkey = tt->rn_dupedkey; 991c793af95Ssangeeta if (tt->rn_dupedkey) /* parent */ 992c793af95Ssangeeta tt->rn_dupedkey->rn_parent = p; 993c793af95Ssangeeta /* parent */ 994c793af95Ssangeeta } else 995c793af95Ssangeeta #ifdef _KERNEL 996c793af95Ssangeeta cmn_err(CE_WARN, 997c793af95Ssangeeta "rn_delete: couldn't find us\n"); 998c793af95Ssangeeta #else 999c793af95Ssangeeta syslog(LOG_ERR, 1000c793af95Ssangeeta "rn_delete: couldn't find us\n"); 1001c793af95Ssangeeta #endif /* _KERNEL */ 1002c793af95Ssangeeta } 1003c793af95Ssangeeta t = tt + 1; 1004c793af95Ssangeeta if (t->rn_flags & RNF_ACTIVE) { 1005c793af95Ssangeeta *++x = *t; 1006c793af95Ssangeeta p = t->rn_parent; 1007c793af95Ssangeeta if (p->rn_left == t) 1008c793af95Ssangeeta p->rn_left = x; 1009c793af95Ssangeeta else 1010c793af95Ssangeeta p->rn_right = x; 1011c793af95Ssangeeta x->rn_left->rn_parent = x; 1012c793af95Ssangeeta x->rn_right->rn_parent = x; 1013c793af95Ssangeeta } 1014c793af95Ssangeeta goto out; 1015c793af95Ssangeeta } 1016c793af95Ssangeeta if (t->rn_left == tt) 1017c793af95Ssangeeta x = t->rn_right; 1018c793af95Ssangeeta else 1019c793af95Ssangeeta x = t->rn_left; 1020c793af95Ssangeeta p = t->rn_parent; 1021c793af95Ssangeeta if (p->rn_right == t) 1022c793af95Ssangeeta p->rn_right = x; 1023c793af95Ssangeeta else 1024c793af95Ssangeeta p->rn_left = x; 1025c793af95Ssangeeta x->rn_parent = p; 1026c793af95Ssangeeta /* 1027c793af95Ssangeeta * Demote routes attached to us. 1028c793af95Ssangeeta */ 1029c793af95Ssangeeta if (t->rn_mklist) { 1030c793af95Ssangeeta if (x->rn_bit >= 0) { 1031c793af95Ssangeeta for (mp = &x->rn_mklist; (m = *mp) != NULL; ) 1032c793af95Ssangeeta mp = &m->rm_mklist; 1033c793af95Ssangeeta *mp = t->rn_mklist; 1034c793af95Ssangeeta } else { 1035c793af95Ssangeeta /* 1036c793af95Ssangeeta * If there are any key,mask pairs in a sibling 1037c793af95Ssangeeta * duped-key chain, some subset will appear sorted 1038c793af95Ssangeeta * in the same order attached to our mklist 1039c793af95Ssangeeta */ 1040c793af95Ssangeeta for (m = t->rn_mklist; m && x; x = x->rn_dupedkey) 1041c793af95Ssangeeta if (m == x->rn_mklist) { 1042c793af95Ssangeeta struct radix_mask *mm = m->rm_mklist; 1043c793af95Ssangeeta x->rn_mklist = 0; 1044c793af95Ssangeeta if (--(m->rm_refs) < 0) 1045c793af95Ssangeeta MKFree(m); 1046c793af95Ssangeeta m = mm; 1047c793af95Ssangeeta } 1048c793af95Ssangeeta if (m) 1049c793af95Ssangeeta #ifdef _KERNEL 1050c793af95Ssangeeta cmn_err(CE_WARN, 1051c793af95Ssangeeta "rn_delete: Orphaned Mask %p at %p\n", 1052c793af95Ssangeeta (void *)m, (void *)x); 1053c793af95Ssangeeta #else 1054c793af95Ssangeeta syslog(LOG_ERR, 1055c793af95Ssangeeta "rn_delete: Orphaned Mask %p at %p\n", 1056c793af95Ssangeeta (void *)m, (void *)x); 1057c793af95Ssangeeta #endif /* _KERNEL */ 1058c793af95Ssangeeta } 1059c793af95Ssangeeta } 1060c793af95Ssangeeta /* 1061c793af95Ssangeeta * We may be holding an active internal node in the tree. 1062c793af95Ssangeeta */ 1063c793af95Ssangeeta x = tt + 1; 1064c793af95Ssangeeta if (t != x) { 1065c793af95Ssangeeta *t = *x; 1066c793af95Ssangeeta t->rn_left->rn_parent = t; 1067c793af95Ssangeeta t->rn_right->rn_parent = t; 1068c793af95Ssangeeta p = x->rn_parent; 1069c793af95Ssangeeta if (p->rn_left == x) 1070c793af95Ssangeeta p->rn_left = t; 1071c793af95Ssangeeta else 1072c793af95Ssangeeta p->rn_right = t; 1073c793af95Ssangeeta } 1074c793af95Ssangeeta out: 1075c793af95Ssangeeta tt->rn_flags &= ~RNF_ACTIVE; 1076c793af95Ssangeeta tt[1].rn_flags &= ~RNF_ACTIVE; 1077c793af95Ssangeeta return (tt); 1078c793af95Ssangeeta } 1079c793af95Ssangeeta 1080c793af95Ssangeeta /* 1081c793af95Ssangeeta * Walk the radix tree; For the kernel routing table, we hold additional 1082c793af95Ssangeeta * refs on the ire_bucket to ensure that the walk function f() does not 1083c793af95Ssangeeta * run into trashed memory. The kernel routing table is identified by 1084c793af95Ssangeeta * a rnh_treetop that has RNF_SUNW_FT set in the rn_flags. 1085c793af95Ssangeeta * Note that all refs takein in rn_walktree are released before it returns, 1086c793af95Ssangeeta * so that f() will need to take any additional references on memory 1087c793af95Ssangeeta * to be passed back to the caller of rn_walktree. 1088c793af95Ssangeeta */ 1089c793af95Ssangeeta static int 1090c793af95Ssangeeta rn_walktree(h, f, w) 1091c793af95Ssangeeta struct radix_node_head *h; 1092c793af95Ssangeeta walktree_f_t *f; 1093c793af95Ssangeeta void *w; 1094c793af95Ssangeeta { 10952679e103Ssowmini return (rn_walktree_mt(h, f, w, NULL, NULL)); 10962679e103Ssowmini } 10972679e103Ssowmini static int 10982679e103Ssowmini rn_walktree_mt(h, f, w, lockf, unlockf) 10992679e103Ssowmini struct radix_node_head *h; 11002679e103Ssowmini walktree_f_t *f; 11012679e103Ssowmini void *w; 11022679e103Ssowmini lockf_t lockf, unlockf; 11032679e103Ssowmini { 1104c793af95Ssangeeta int error; 1105c793af95Ssangeeta struct radix_node *base, *next; 1106c793af95Ssangeeta struct radix_node *rn = h->rnh_treetop; 11072679e103Ssowmini boolean_t is_mt = B_FALSE; 1108c793af95Ssangeeta 11092679e103Ssowmini if (lockf != NULL) { 11102679e103Ssowmini ASSERT(unlockf != NULL); 11112679e103Ssowmini is_mt = B_TRUE; 1112c793af95Ssangeeta } 1113c793af95Ssangeeta /* 1114c793af95Ssangeeta * This gets complicated because we may delete the node 1115c793af95Ssangeeta * while applying the function f to it, so we need to calculate 1116c793af95Ssangeeta * the successor node in advance. 1117c793af95Ssangeeta */ 1118c793af95Ssangeeta RADIX_NODE_HEAD_RLOCK(h); 1119c793af95Ssangeeta /* First time through node, go left */ 1120c793af95Ssangeeta while (rn->rn_bit >= 0) { 1121c793af95Ssangeeta rn = rn->rn_left; 1122c793af95Ssangeeta } 1123c793af95Ssangeeta 11242679e103Ssowmini if (is_mt) 11252679e103Ssowmini (*lockf)(rn); 1126c793af95Ssangeeta 1127c793af95Ssangeeta for (;;) { 1128c793af95Ssangeeta base = rn; 1129c793af95Ssangeeta /* If at right child go back up, otherwise, go right */ 1130c793af95Ssangeeta while (rn->rn_parent->rn_right == rn && 1131c793af95Ssangeeta (rn->rn_flags & RNF_ROOT) == 0) { 1132c793af95Ssangeeta rn = rn->rn_parent; 1133c793af95Ssangeeta } 1134c793af95Ssangeeta /* Find the next *leaf* since next node might vanish, too */ 1135c793af95Ssangeeta for (rn = rn->rn_parent->rn_right; rn->rn_bit >= 0; ) { 1136c793af95Ssangeeta rn = rn->rn_left; 1137c793af95Ssangeeta } 1138c793af95Ssangeeta next = rn; 1139c793af95Ssangeeta 11402679e103Ssowmini if (is_mt && next != NULL) 11412679e103Ssowmini (*lockf)(next); 1142c793af95Ssangeeta 1143c793af95Ssangeeta /* Process leaves */ 1144c793af95Ssangeeta while ((rn = base) != NULL) { 1145c793af95Ssangeeta base = rn->rn_dupedkey; 1146c793af95Ssangeeta 11472679e103Ssowmini if (is_mt && base != NULL) 11482679e103Ssowmini (*lockf)(base); 1149c793af95Ssangeeta 1150c793af95Ssangeeta RADIX_NODE_HEAD_UNLOCK(h); 1151c793af95Ssangeeta if (!(rn->rn_flags & RNF_ROOT) && 1152c793af95Ssangeeta (error = (*f)(rn, w))) { 11532679e103Ssowmini if (is_mt) { 11542679e103Ssowmini (*unlockf)(rn); 1155c793af95Ssangeeta if (base != NULL) 11562679e103Ssowmini (*unlockf)(base); 1157c793af95Ssangeeta if (next != NULL) 11582679e103Ssowmini (*unlockf)(next); 1159c793af95Ssangeeta } 1160c793af95Ssangeeta return (error); 1161c793af95Ssangeeta } 11622679e103Ssowmini if (is_mt) 11632679e103Ssowmini (*unlockf)(rn); 1164c793af95Ssangeeta RADIX_NODE_HEAD_RLOCK(h); 1165c793af95Ssangeeta } 1166c793af95Ssangeeta rn = next; 1167c793af95Ssangeeta if (rn->rn_flags & RNF_ROOT) { 1168c793af95Ssangeeta RADIX_NODE_HEAD_UNLOCK(h); 11692679e103Ssowmini /* 11702679e103Ssowmini * no ref to release, since we never take a ref 11712679e103Ssowmini * on the root node- it can't be deleted. 11722679e103Ssowmini */ 1173c793af95Ssangeeta return (0); 1174c793af95Ssangeeta } 1175c793af95Ssangeeta } 1176c793af95Ssangeeta /* NOTREACHED */ 1177c793af95Ssangeeta } 1178c793af95Ssangeeta 1179c793af95Ssangeeta /* 1180c793af95Ssangeeta * Allocate and initialize an empty tree. This has 3 nodes, which are 1181c793af95Ssangeeta * part of the radix_node_head (in the order <left,root,right>) and are 1182c793af95Ssangeeta * marked RNF_ROOT so they cannot be freed. 1183c793af95Ssangeeta * The leaves have all-zero and all-one keys, with significant 1184c793af95Ssangeeta * bits starting at 'off'. 1185c793af95Ssangeeta * Return 1 on success, 0 on error. 1186c793af95Ssangeeta */ 1187c793af95Ssangeeta int 1188c793af95Ssangeeta rn_inithead(head, off) 1189c793af95Ssangeeta void **head; 1190c793af95Ssangeeta int off; 1191c793af95Ssangeeta { 1192c793af95Ssangeeta struct radix_node_head *rnh; 1193c793af95Ssangeeta struct radix_node *t, *tt, *ttt; 1194c793af95Ssangeeta if (*head) 1195c793af95Ssangeeta return (1); 1196c793af95Ssangeeta R_ZallocSleep(rnh, struct radix_node_head *, sizeof (*rnh)); 1197c793af95Ssangeeta if (rnh == 0) 1198c793af95Ssangeeta return (0); 1199c793af95Ssangeeta #ifdef _KERNEL 1200c793af95Ssangeeta RADIX_NODE_HEAD_LOCK_INIT(rnh); 1201c793af95Ssangeeta #endif 1202c793af95Ssangeeta *head = rnh; 1203c793af95Ssangeeta t = rn_newpair(rn_zeros, off, rnh->rnh_nodes); 1204c793af95Ssangeeta ttt = rnh->rnh_nodes + 2; 1205c793af95Ssangeeta t->rn_right = ttt; 1206c793af95Ssangeeta t->rn_parent = t; 1207c793af95Ssangeeta tt = t->rn_left; /* ... which in turn is rnh->rnh_nodes */ 1208c793af95Ssangeeta tt->rn_flags = t->rn_flags = RNF_ROOT | RNF_ACTIVE; 1209c793af95Ssangeeta tt->rn_bit = -1 - off; 1210c793af95Ssangeeta *ttt = *tt; 1211c793af95Ssangeeta ttt->rn_key = rn_ones; 1212c793af95Ssangeeta rnh->rnh_addaddr = rn_addroute; 1213c793af95Ssangeeta rnh->rnh_deladdr = rn_delete; 1214c793af95Ssangeeta rnh->rnh_matchaddr = rn_match; 1215c793af95Ssangeeta rnh->rnh_matchaddr_args = rn_match_args; 1216c793af95Ssangeeta rnh->rnh_lookup = rn_lookup; 1217c793af95Ssangeeta rnh->rnh_walktree = rn_walktree; 12182679e103Ssowmini rnh->rnh_walktree_mt = rn_walktree_mt; 1219c793af95Ssangeeta rnh->rnh_walktree_from = NULL; /* not implemented */ 1220c793af95Ssangeeta rnh->rnh_treetop = t; 1221c793af95Ssangeeta return (1); 1222c793af95Ssangeeta } 1223c793af95Ssangeeta 1224c793af95Ssangeeta void 1225c793af95Ssangeeta rn_init() 1226c793af95Ssangeeta { 1227c793af95Ssangeeta char *cp, *cplim; 1228c793af95Ssangeeta 1229c793af95Ssangeeta #ifdef _KERNEL 1230c793af95Ssangeeta radix_mask_cache = kmem_cache_create("radix_mask", 1231c793af95Ssangeeta sizeof (struct radix_mask), 0, NULL, NULL, NULL, NULL, NULL, 0); 1232c793af95Ssangeeta radix_node_cache = kmem_cache_create("radix_node", 1233c793af95Ssangeeta max_keylen + 2 * sizeof (struct radix_node), 1234c793af95Ssangeeta 0, NULL, NULL, NULL, NULL, NULL, 0); 1235c793af95Ssangeeta #endif /* _KERNEL */ 1236c793af95Ssangeeta R_ZallocSleep(rn_zeros, char *, 2 * max_keylen); 1237c793af95Ssangeeta 1238c793af95Ssangeeta ASSERT(rn_zeros != NULL); 1239c793af95Ssangeeta bzero(rn_zeros, 2 * max_keylen); 1240c793af95Ssangeeta rn_ones = cp = rn_zeros + max_keylen; 1241c793af95Ssangeeta cplim = rn_ones + max_keylen; 1242c793af95Ssangeeta while (cp < cplim) 1243c793af95Ssangeeta *cp++ = -1; 1244c793af95Ssangeeta if (rn_inithead((void **)(void *)&mask_rnhead, 0) == 0) 1245c793af95Ssangeeta panic("rn_init: could not init mask_rnhead "); 1246c793af95Ssangeeta } 1247c793af95Ssangeeta 1248c793af95Ssangeeta int 1249c793af95Ssangeeta rn_freenode(n, p) 1250c793af95Ssangeeta struct radix_node *n; 1251c793af95Ssangeeta void *p; 1252c793af95Ssangeeta { 1253c793af95Ssangeeta struct radix_node_head *rnh = p; 1254c793af95Ssangeeta struct radix_node *d; 1255c793af95Ssangeeta 1256c793af95Ssangeeta d = rnh->rnh_deladdr(n->rn_key, NULL, rnh); 1257c793af95Ssangeeta if (d != NULL) { 1258c793af95Ssangeeta Free(d, radix_node_cache); 1259c793af95Ssangeeta } 1260c793af95Ssangeeta return (0); 1261c793af95Ssangeeta } 1262c793af95Ssangeeta 1263c793af95Ssangeeta 1264c793af95Ssangeeta void 1265c793af95Ssangeeta rn_freehead(rnh) 1266c793af95Ssangeeta struct radix_node_head *rnh; 1267c793af95Ssangeeta { 1268c793af95Ssangeeta (void) rn_walktree(rnh, rn_freenode, rnh); 1269c793af95Ssangeeta 1270c793af95Ssangeeta rnh->rnh_addaddr = NULL; 1271c793af95Ssangeeta rnh->rnh_deladdr = NULL; 1272c793af95Ssangeeta rnh->rnh_matchaddr = NULL; 1273c793af95Ssangeeta rnh->rnh_lookup = NULL; 1274c793af95Ssangeeta rnh->rnh_walktree = NULL; 1275c793af95Ssangeeta 1276c793af95Ssangeeta #ifdef _KERNEL 1277f4b3ec61Sdh155122 RADIX_NODE_HEAD_DESTROY(rnh); 1278c793af95Ssangeeta FreeHead(rnh, sizeof (*rnh)); 1279c793af95Ssangeeta #else 1280c793af95Ssangeeta Free(rnh, NULL); 1281c793af95Ssangeeta #endif /* _KERNEL */ 1282c793af95Ssangeeta } 1283c793af95Ssangeeta 1284c793af95Ssangeeta void 1285c793af95Ssangeeta rn_fini() 1286c793af95Ssangeeta { 1287c793af95Ssangeeta struct radix_mask *m; 1288c793af95Ssangeeta 1289c793af95Ssangeeta if (rn_zeros != NULL) { 1290c793af95Ssangeeta #ifdef _KERNEL 1291c793af95Ssangeeta FreeHead(rn_zeros, 2 * max_keylen); 1292c793af95Ssangeeta #else 1293c793af95Ssangeeta Free(rn_zeros, NULL); 1294c793af95Ssangeeta #endif 1295c793af95Ssangeeta rn_zeros = NULL; 1296c793af95Ssangeeta } 1297c793af95Ssangeeta 1298c793af95Ssangeeta 1299c793af95Ssangeeta if (mask_rnhead != NULL) { 1300c793af95Ssangeeta rn_freehead(mask_rnhead); 1301c793af95Ssangeeta mask_rnhead = NULL; 1302c793af95Ssangeeta } 1303c793af95Ssangeeta 1304c793af95Ssangeeta while ((m = rn_mkfreelist) != NULL) { 1305c793af95Ssangeeta rn_mkfreelist = m->rm_mklist; 1306c793af95Ssangeeta Free(m, NULL); 1307c793af95Ssangeeta } 1308c793af95Ssangeeta } 1309