radix.c 30 KB

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