2 * Copyright (C) 2012 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
8 #include <sys/socket.h>
10 #include <netinet/in.h>
13 #include <sys/systm.h>
20 #include "netinet/ip_compat.h"
21 #include "netinet/ip_fil.h"
23 # include <arpa/inet.h>
27 #include "netinet/radix_ipf.h"
29 #define ADF_OFF offsetof(addrfamily_t, adf_addr)
30 #define ADF_OFF_BITS (ADF_OFF << 3)
32 static ipf_rdx_node_t *ipf_rx_insert __P((ipf_rdx_head_t *,
33 ipf_rdx_node_t nodes[2], int *));
34 static void ipf_rx_attach_mask __P((ipf_rdx_node_t *, ipf_rdx_mask_t *));
35 static int count_mask_bits __P((addrfamily_t *, u_32_t **));
36 static void buildnodes __P((addrfamily_t *, addrfamily_t *,
37 ipf_rdx_node_t n[2]));
38 static ipf_rdx_node_t *ipf_rx_find_addr __P((ipf_rdx_node_t *, u_32_t *));
39 static ipf_rdx_node_t *ipf_rx_lookup __P((ipf_rdx_head_t *, addrfamily_t *,
41 static ipf_rdx_node_t *ipf_rx_match __P((ipf_rdx_head_t *, addrfamily_t *));
46 * The code in this file has been written to target using the addrfamily_t
47 * data structure to house the address information and no other. Thus there
48 * are certain aspects of thise code (such as offsets to the address itself)
49 * that are hard coded here whilst they might be more variable elsewhere.
50 * Similarly, this code enforces no maximum key length as that's implied by
51 * all keys needing to be stored in addrfamily_t.
54 /* ------------------------------------------------------------------------ */
55 /* Function: count_mask_bits */
56 /* Returns: number of consecutive bits starting at "mask". */
58 /* Count the number of bits set in the address section of addrfamily_t and */
59 /* return both that number and a pointer to the last word with a bit set if */
60 /* lastp is not NULL. The bit count is performed using network byte order */
61 /* as the guide for which bit is the most significant bit. */
62 /* ------------------------------------------------------------------------ */
64 count_mask_bits(mask, lastp)
68 u_32_t *mp = (u_32_t *)&mask->adf_addr;
73 mlen = mask->adf_len - offsetof(addrfamily_t, adf_addr);
74 for (; mlen > 0; mlen -= 4, mp++) {
75 if ((m = ntohl(*mp)) == 0)
79 for (; m & 0x80000000; m <<= 1)
87 /* ------------------------------------------------------------------------ */
88 /* Function: buildnodes */
90 /* Parameters: addr(I) - network address for this radix node */
91 /* mask(I) - netmask associated with the above address */
92 /* nodes(O) - pair of ipf_rdx_node_t's to initialise with data */
93 /* associated with addr and mask. */
95 /* Initialise the fields in a pair of radix tree nodes according to the */
96 /* data supplied in the paramters "addr" and "mask". It is expected that */
97 /* "mask" will contain a consecutive string of bits set. Masks with gaps in */
98 /* the middle are not handled by this implementation. */
99 /* ------------------------------------------------------------------------ */
101 buildnodes(addr, mask, nodes)
102 addrfamily_t *addr, *mask;
103 ipf_rdx_node_t nodes[2];
112 maskbits = count_mask_bits(mask, &last);
117 masklen = last - (u_32_t *)mask;
120 lastbits = maskbits & 0x1f;
122 bzero(&nodes[0], sizeof(ipf_rdx_node_t) * 2);
123 nodes[0].maskbitcount = maskbits;
124 nodes[0].index = -1 - (ADF_OFF_BITS + maskbits);
125 nodes[0].addrkey = (u_32_t *)addr;
126 nodes[0].maskkey = (u_32_t *)mask;
127 nodes[0].addroff = nodes[0].addrkey + masklen;
128 nodes[0].maskoff = nodes[0].maskkey + masklen;
129 nodes[0].parent = &nodes[1];
130 nodes[0].offset = masklen;
131 nodes[0].lastmask = lastmask;
132 nodes[1].offset = masklen;
133 nodes[1].left = &nodes[0];
134 nodes[1].maskbitcount = maskbits;
136 (void) strcpy(nodes[0].name, "_BUILD.0");
137 (void) strcpy(nodes[1].name, "_BUILD.1");
142 /* ------------------------------------------------------------------------ */
143 /* Function: ipf_rx_find_addr */
144 /* Returns: ipf_rdx_node_t * - pointer to a node in the radix tree. */
145 /* Parameters: tree(I) - pointer to first right node in tree to search */
146 /* addr(I) - pointer to address to match */
148 /* Walk the radix tree given by "tree", looking for a leaf node that is a */
149 /* match for the address given by "addr". */
150 /* ------------------------------------------------------------------------ */
151 static ipf_rdx_node_t *
152 ipf_rx_find_addr(tree, addr)
153 ipf_rdx_node_t *tree;
158 for (cur = tree; cur->index >= 0;) {
159 if (cur->bitmask & addr[cur->offset]) {
170 /* ------------------------------------------------------------------------ */
171 /* Function: ipf_rx_match */
172 /* Returns: ipf_rdx_node_t * - NULL on error, else pointer to the node */
173 /* added to the tree. */
174 /* Paramters: head(I) - pointer to tree head to search */
175 /* addr(I) - pointer to address to find */
177 /* Search the radix tree for the best match to the address pointed to by */
178 /* "addr" and return a pointer to that node. This search will not match the */
179 /* address information stored in either of the root leaves as neither of */
180 /* them are considered to be part of the tree of data being stored. */
181 /* ------------------------------------------------------------------------ */
182 static ipf_rdx_node_t *
183 ipf_rx_match(head, addr)
184 ipf_rdx_head_t *head;
187 ipf_rdx_mask_t *masknode;
188 ipf_rdx_node_t *prev;
189 ipf_rdx_node_t *node;
199 end = (u_32_t *)((u_char *)addr + len);
200 node = ipf_rx_find_addr(head->root, (u_32_t *)addr);
203 * Search the dupkey list for a potential match.
205 for (cur = node; (cur != NULL) && (cur->root == 0); cur = cur->dupkey) {
206 i = cur[0].addroff - cur[0].addrkey;
207 data = cur[0].addrkey + i;
208 mask = cur[0].maskkey + i;
209 key = (u_32_t *)addr + i;
210 for (; key < end; data++, key++, mask++)
211 if ((*key & *mask) != *data)
213 if ((end == key) && (cur->root == 0))
214 return (cur); /* Equal keys */
217 key = (u_32_t *)addr;
219 for (node = prev; node->root == 0; node = node->parent) {
221 * We know that the node hasn't matched so therefore only
222 * the entries in the mask list are searched, not the top
223 * node nor the dupkey list.
225 masknode = node->masks;
226 for (; masknode != NULL; masknode = masknode->next) {
227 if (masknode->maskbitcount > node->maskbitcount)
229 cur = masknode->node;
230 for (i = ADF_OFF >> 2; i <= node->offset; i++) {
231 if ((key[i] & masknode->mask[i]) ==
242 /* ------------------------------------------------------------------------ */
243 /* Function: ipf_rx_lookup */
244 /* Returns: ipf_rdx_node_t * - NULL on error, else pointer to the node */
245 /* added to the tree. */
246 /* Paramters: head(I) - pointer to tree head to search */
247 /* addr(I) - address part of the key to match */
248 /* mask(I) - netmask part of the key to match */
250 /* ipf_rx_lookup searches for an exact match on (addr,mask). The intention */
251 /* is to see if a given key is in the tree, not to see if a route exists. */
252 /* ------------------------------------------------------------------------ */
254 ipf_rx_lookup(head, addr, mask)
255 ipf_rdx_head_t *head;
256 addrfamily_t *addr, *mask;
258 ipf_rdx_node_t *found;
259 ipf_rdx_node_t *node;
263 found = ipf_rx_find_addr(head->root, (u_32_t *)addr);
264 if (found->root == 1)
268 * It is possible to find a matching address in the tree but for the
269 * netmask to not match. If the netmask does not match and there is
270 * no list of alternatives present at dupkey, return a failure.
272 count = count_mask_bits(mask, NULL);
273 if (count != found->maskbitcount && found->dupkey == NULL)
276 akey = (u_32_t *)addr;
277 if ((found->addrkey[found->offset] & found->maskkey[found->offset]) !=
281 if (found->dupkey != NULL) {
283 while (node != NULL && node->maskbitcount != count)
293 /* ------------------------------------------------------------------------ */
294 /* Function: ipf_rx_attach_mask */
296 /* Parameters: node(I) - pointer to a radix tree node */
297 /* mask(I) - pointer to mask structure to add */
299 /* Add the netmask to the given node in an ordering where the most specific */
300 /* netmask is at the top of the list. */
301 /* ------------------------------------------------------------------------ */
303 ipf_rx_attach_mask(node, mask)
304 ipf_rdx_node_t *node;
305 ipf_rdx_mask_t *mask;
310 for (pm = &node->masks; (m = *pm) != NULL; pm = &m->next)
311 if (m->maskbitcount < mask->maskbitcount)
318 /* ------------------------------------------------------------------------ */
319 /* Function: ipf_rx_insert */
320 /* Returns: ipf_rdx_node_t * - NULL on error, else pointer to the node */
321 /* added to the tree. */
322 /* Paramters: head(I) - pointer to tree head to add nodes to */
323 /* nodes(I) - pointer to radix nodes to be added */
324 /* dup(O) - set to 1 if node is a duplicate, else 0. */
326 /* Add the new radix tree entry that owns nodes[] to the tree given by head.*/
327 /* If there is already a matching key in the table, "dup" will be set to 1 */
328 /* and the existing node pointer returned if there is a complete key match. */
329 /* A complete key match is a matching of all key data that is presented by */
330 /* by the netmask. */
331 /* ------------------------------------------------------------------------ */
332 static ipf_rdx_node_t *
333 ipf_rx_insert(head, nodes, dup)
334 ipf_rdx_head_t *head;
335 ipf_rdx_node_t nodes[2];
338 ipf_rdx_mask_t **pmask;
339 ipf_rdx_node_t *node;
340 ipf_rdx_node_t *prev;
341 ipf_rdx_mask_t *mask;
355 addr = nodes[0].addrkey;
357 node = ipf_rx_find_addr(head->root, addr);
358 len = ((addrfamily_t *)addr)->adf_len;
359 key = (u_32_t *)&((addrfamily_t *)addr)->adf_addr;
360 data= (u_32_t *)&((addrfamily_t *)node->addrkey)->adf_addr;
361 end = (u_32_t *)((u_char *)addr + len);
362 for (nlen = 0; key < end; data++, key++, nlen += 32)
367 return (node); /* Equal keys */
371 bits = (ntohl(*data) ^ ntohl(*key));
372 for (; bits != 0; nlen++) {
373 if ((bits & 0x80000000) != 0)
377 nlen += ADF_OFF_BITS;
378 nodes[1].index = nlen;
379 nodes[1].bitmask = htonl(0x80000000 >> (nlen & 0x1f));
380 nodes[0].offset = nlen / 32;
381 nodes[1].offset = nlen / 32;
384 * Walk through the tree and look for the correct place to attach
385 * this node. ipf_rx_fin_addr is not used here because the place
386 * to attach this node may be an internal node (same key, different
387 * netmask.) Additionally, the depth of the search is forcibly limited
388 * here to not exceed the netmask, so that a short netmask will be
389 * added higher up the tree even if there are lower branches.
392 key = nodes[0].addrkey;
395 if (key[cur->offset] & cur->bitmask) {
400 } while (nlen > (unsigned)cur->index);
402 if ((key[prev->offset] & prev->bitmask) == 0) {
403 prev->left = &nodes[1];
405 prev->right = &nodes[1];
407 cur->parent = &nodes[1];
408 nodes[1].parent = prev;
409 if ((key[nodes[1].offset] & nodes[1].bitmask) == 0) {
410 nodes[1].right = cur;
412 nodes[1].right = &nodes[0];
416 nodeoff = nodes[0].offset;
417 nodekey = nodes[0].addrkey[nodeoff];
418 nodemask = nodes[0].lastmask;
419 nodebits = nodes[0].maskbitcount;
422 * Find the node up the tree with the largest pattern that still
423 * matches the node being inserted to see if this mask can be
426 for (cur = nodes[1].parent; cur->root == 0; cur = cur->parent) {
427 if (cur->maskbitcount <= nodebits)
429 if (((cur - 1)->addrkey[nodeoff] & nodemask) != nodekey)
434 KMALLOC(mask, ipf_rdx_mask_t *);
437 bzero(mask, sizeof(*mask));
439 mask->node = &nodes[0];
440 mask->maskbitcount = nodebits;
441 mask->mask = nodes[0].maskkey;
442 nodes[0].mymask = mask;
447 for (pmask = &prev->masks; (m = *pmask) != NULL;
449 if (m->maskbitcount < nodebits)
454 * No higher up nodes qualify, so attach mask locally.
456 pmask = &nodes[0].masks;
462 * Search the mask list on each child to see if there are any masks
463 * there that can be moved up to this newly inserted node.
465 cur = nodes[1].right;
466 if (cur->root == 0) {
467 for (pmask = &cur->masks; (mask = *pmask) != NULL; ) {
468 if (mask->maskbitcount < nodebits) {
470 ipf_rx_attach_mask(&nodes[0], mask);
477 if (cur->root == 0 && cur != &nodes[0]) {
478 for (pmask = &cur->masks; (mask = *pmask) != NULL; ) {
479 if (mask->maskbitcount < nodebits) {
481 ipf_rx_attach_mask(&nodes[0], mask);
490 /* ------------------------------------------------------------------------ */
491 /* Function: ipf_rx_addroute */
492 /* Returns: ipf_rdx_node_t * - NULL on error, else pointer to the node */
493 /* added to the tree. */
494 /* Paramters: head(I) - pointer to tree head to search */
495 /* addr(I) - address portion of "route" to add */
496 /* mask(I) - netmask portion of "route" to add */
497 /* nodes(I) - radix tree data nodes inside allocate structure */
499 /* Attempt to add a node to the radix tree. The key for the node is the */
500 /* (addr,mask). No memory allocation for the radix nodes themselves is */
501 /* performed here, the data structure that this radix node is being used to */
502 /* find is expected to house the node data itself however the call to */
503 /* ipf_rx_insert() will attempt to allocate memory in order for netmask to */
504 /* be promoted further up the tree. */
505 /* In this case, the ip_pool_node_t structure from ip_pool.h contains both */
506 /* the key material (addr,mask) and the radix tree nodes[]. */
508 /* The mechanics of inserting the node into the tree is handled by the */
509 /* function ipf_rx_insert() above. Here, the code deals with the case */
510 /* where the data to be inserted is a duplicate. */
511 /* ------------------------------------------------------------------------ */
513 ipf_rx_addroute(head, addr, mask, nodes)
514 ipf_rdx_head_t *head;
515 addrfamily_t *addr, *mask;
516 ipf_rdx_node_t *nodes;
518 ipf_rdx_node_t *node;
519 ipf_rdx_node_t *prev;
523 buildnodes(addr, mask, nodes);
524 x = ipf_rx_insert(head, nodes, &dup);
532 * The duplicate list is kept sorted with the longest
533 * mask at the top, meaning that the most specific entry
534 * in the listis found first. This list thus allows for
535 * duplicates such as 128.128.0.0/32 and 128.128.0.0/16.
537 while ((x != NULL) && (x->maskbitcount > node->maskbitcount)) {
543 * Is it a complete duplicate? If so, return NULL and
544 * fail the insert. Otherwise, insert it into the list
545 * of netmasks active for this key.
547 if ((x != NULL) && (x->maskbitcount == node->maskbitcount))
552 prev->dupkey = &nodes[0];
553 nodes[0].parent = prev;
555 x->parent = &nodes[0];
557 nodes[0].dupkey = x->dupkey;
559 nodes[0].parent = prev;
560 x->parent = &nodes[0];
562 prev->left = &nodes[0];
564 prev->right = &nodes[0];
572 /* ------------------------------------------------------------------------ */
573 /* Function: ipf_rx_delete */
574 /* Returns: ipf_rdx_node_t * - NULL on error, else node removed from */
576 /* Paramters: head(I) - pointer to tree head to search */
577 /* addr(I) - pointer to the address part of the key */
578 /* mask(I) - pointer to the netmask part of the key */
580 /* Search for an entry in the radix tree that is an exact match for (addr, */
581 /* mask) and remove it if it exists. In the case where (addr,mask) is a not */
582 /* a unique key, the tree structure itself is not changed - only the list */
583 /* of duplicate keys. */
584 /* ------------------------------------------------------------------------ */
586 ipf_rx_delete(head, addr, mask)
587 ipf_rdx_head_t *head;
588 addrfamily_t *addr, *mask;
590 ipf_rdx_mask_t **pmask;
591 ipf_rdx_node_t *parent;
592 ipf_rdx_node_t *found;
593 ipf_rdx_node_t *prev;
594 ipf_rdx_node_t *node;
599 found = ipf_rx_find_addr(head->root, (u_32_t *)addr);
602 if (found->root == 1)
604 count = count_mask_bits(mask, NULL);
605 parent = found->parent;
606 if (found->dupkey != NULL) {
608 while (node != NULL && node->maskbitcount != count)
614 * Remove from the dupkey list. Here, "parent" is
615 * the previous node on the list (rather than tree)
616 * and "dupkey" is the next node on the list.
618 parent = node->parent;
619 parent->dupkey = node->dupkey;
620 node->dupkey->parent = parent;
624 * When removing the top node of the dupkey list,
625 * the pointers at the top of the list that point
626 * to other tree nodes need to be preserved and
627 * any children must have their parent updated.
630 node->parent = found->parent;
631 node->right = found->right;
632 node->left = found->left;
633 found->right->parent = node;
634 found->left->parent = node;
635 if (parent->left == found)
641 if (count != found->maskbitcount)
644 * Remove the node from the tree and reconnect the subtree
648 * If there is a tree to the left, look for something to
649 * attach in place of "found".
652 cur = parent->parent;
653 if (parent != found + 1) {
654 if ((found + 1)->parent->right == found + 1)
655 (found + 1)->parent->right = parent;
657 (found + 1)->parent->left = parent;
658 if (cur->right == parent) {
659 if (parent->left == found) {
660 cur->right = parent->right;
661 } else if (parent->left != parent - 1) {
662 cur->right = parent->left;
664 cur->right = parent - 1;
666 cur->right->parent = cur;
668 if (parent->right == found) {
669 cur->left = parent->left;
670 } else if (parent->right != parent - 1) {
671 cur->left = parent->right;
673 cur->left = parent - 1;
675 cur->left->parent = cur;
677 parent->left = (found + 1)->left;
678 if ((found + 1)->right != parent)
679 parent->right = (found + 1)->right;
680 parent->left->parent = parent;
681 parent->right->parent = parent;
682 parent->parent = (found + 1)->parent;
684 parent->bitmask = prev->bitmask;
685 parent->offset = prev->offset;
686 parent->index = prev->index;
689 * We found an edge node.
691 cur = parent->parent;
692 if (cur->left == parent) {
693 if (parent->left == found) {
694 cur->left = parent->right;
695 parent->right->parent = cur;
697 cur->left = parent->left;
698 parent->left->parent = cur;
701 if (parent->right != found) {
702 cur->right = parent->right;
703 parent->right->parent = cur;
705 cur->right = parent->left;
706 prev->left->parent = cur;
713 * Remove mask associated with this node.
715 for (cur = parent; cur->root == 0; cur = cur->parent) {
718 if (cur->maskbitcount <= found->maskbitcount)
720 if (((cur - 1)->addrkey[found->offset] & found->bitmask) !=
721 found->addrkey[found->offset])
723 for (pm = &cur->masks; (m = *pm) != NULL; )
724 if (m->node == cur) {
731 KFREE(found->mymask);
734 * Masks that have been brought up to this node from below need to
737 for (pmask = &parent->masks; (m = *pmask) != NULL; ) {
742 if (found->addrkey[cur->offset] & cur->lastmask) {
743 ipf_rx_attach_mask(parent->right, m);
744 } else if (parent->left != found) {
745 ipf_rx_attach_mask(parent->left, m);
753 /* ------------------------------------------------------------------------ */
754 /* Function: ipf_rx_walktree */
756 /* Paramters: head(I) - pointer to tree head to search */
757 /* walker(I) - function to call for each node in the tree */
758 /* arg(I) - parameter to pass to walker, in addition to the */
761 /* A standard tree walking function except that it is iterative, rather */
762 /* than recursive and tracks the next node in case the "walker" function */
763 /* should happen to delete and free the current node. It thus goes without */
764 /* saying that the "walker" function is not permitted to cause any change */
765 /* in the validity of the data found at either the left or right child. */
766 /* ------------------------------------------------------------------------ */
768 ipf_rx_walktree(head, walker, arg)
769 ipf_rdx_head_t *head;
770 radix_walk_func_t walker;
773 ipf_rdx_node_t *next;
774 ipf_rdx_node_t *node = head->root;
775 ipf_rdx_node_t *base;
777 while (node->index >= 0)
782 while ((node->parent->right == node) && (node->root == 0))
785 for (node = node->parent->right; node->index >= 0; )
789 for (node = base; node != NULL; node = base) {
801 /* ------------------------------------------------------------------------ */
802 /* Function: ipf_rx_inithead */
803 /* Returns: int - 0 = success, else failure */
804 /* Paramters: softr(I) - pointer to radix context */
805 /* headp(O) - location for where to store allocated tree head */
807 /* This function allocates and initialises a radix tree head structure. */
808 /* As a traditional radix tree, node 0 is used as the "0" sentinel and node */
809 /* "2" is used as the all ones sentinel, leaving node "1" as the root from */
810 /* which the tree is hung with node "0" on its left and node "2" to the */
811 /* right. The context, "softr", is used here to provide a common source of */
812 /* the zeroes and ones data rather than have one per head. */
813 /* ------------------------------------------------------------------------ */
815 ipf_rx_inithead(softr, headp)
816 radix_softc_t *softr;
817 ipf_rdx_head_t **headp;
820 ipf_rdx_node_t *node;
822 KMALLOC(ptr, ipf_rdx_head_t *);
826 bzero(ptr, sizeof(*ptr));
828 ptr->root = node + 1;
829 node[0].index = ADF_OFF_BITS;
830 node[0].index = -1 - node[0].index;
831 node[1].index = ADF_OFF_BITS;
832 node[2].index = node[0].index;
833 node[0].parent = node + 1;
834 node[1].parent = node + 1;
835 node[2].parent = node + 1;
836 node[1].bitmask = htonl(0x80000000);
840 node[0].offset = ADF_OFF_BITS >> 5;
841 node[1].offset = ADF_OFF_BITS >> 5;
842 node[2].offset = ADF_OFF_BITS >> 5;
843 node[1].left = &node[0];
844 node[1].right = &node[2];
845 node[0].addrkey = (u_32_t *)softr->zeros;
846 node[2].addrkey = (u_32_t *)softr->ones;
848 (void) strcpy(node[0].name, "0_ROOT");
849 (void) strcpy(node[1].name, "1_ROOT");
850 (void) strcpy(node[2].name, "2_ROOT");
853 ptr->addaddr = ipf_rx_addroute;
854 ptr->deladdr = ipf_rx_delete;
855 ptr->lookup = ipf_rx_lookup;
856 ptr->matchaddr = ipf_rx_match;
857 ptr->walktree = ipf_rx_walktree;
862 /* ------------------------------------------------------------------------ */
863 /* Function: ipf_rx_freehead */
865 /* Paramters: head(I) - pointer to tree head to free */
867 /* This function simply free's up the radix tree head. Prior to calling */
868 /* this function, it is expected that the tree will have been emptied. */
869 /* ------------------------------------------------------------------------ */
871 ipf_rx_freehead(head)
872 ipf_rdx_head_t *head;
878 /* ------------------------------------------------------------------------ */
879 /* Function: ipf_rx_create */
880 /* Parameters: Nil */
882 /* ------------------------------------------------------------------------ */
886 radix_softc_t *softr;
888 KMALLOC(softr, radix_softc_t *);
891 bzero((char *)softr, sizeof(*softr));
893 KMALLOCS(softr->zeros, u_char *, 3 * sizeof(addrfamily_t));
894 if (softr->zeros == NULL) {
898 softr->ones = softr->zeros + sizeof(addrfamily_t);
904 /* ------------------------------------------------------------------------ */
905 /* Function: ipf_rx_init */
906 /* Returns: int - 0 = success (always) */
908 /* ------------------------------------------------------------------------ */
913 radix_softc_t *softr = ctx;
915 memset(softr->zeros, 0, 3 * sizeof(addrfamily_t));
916 memset(softr->ones, 0xff, sizeof(addrfamily_t));
922 /* ------------------------------------------------------------------------ */
923 /* Function: ipf_rx_destroy */
926 /* ------------------------------------------------------------------------ */
931 radix_softc_t *softr = ctx;
933 if (softr->zeros != NULL)
934 KFREES(softr->zeros, 3 * sizeof(addrfamily_t));
938 /* ====================================================================== */
942 * To compile this file as a standalone test unit, use -DRDX_DEBUG=1
944 #define NAME(x) ((x)->index < 0 ? (x)->name : (x)->name)
945 #define GNAME(y) ((y) == NULL ? "NULL" : NAME(y))
947 typedef struct myst {
948 struct ipf_rdx_node nodes[2];
955 typedef struct tabe_s {
963 { "192:168:100::0", "48", "d" },
964 { "192:168:100::2", "128", "d" },
966 { "127.192.0.0", "255.255.255.0", "d" },
967 { "127.128.0.0", "255.255.255.0", "d" },
968 { "127.96.0.0", "255.255.255.0", "d" },
969 { "127.80.0.0", "255.255.255.0", "d" },
970 { "127.72.0.0", "255.255.255.0", "d" },
971 { "127.64.0.0", "255.255.255.0", "d" },
972 { "127.56.0.0", "255.255.255.0", "d" },
973 { "127.48.0.0", "255.255.255.0", "d" },
974 { "127.40.0.0", "255.255.255.0", "d" },
975 { "127.32.0.0", "255.255.255.0", "d" },
976 { "127.24.0.0", "255.255.255.0", "d" },
977 { "127.16.0.0", "255.255.255.0", "d" },
978 { "127.8.0.0", "255.255.255.0", "d" },
979 { "124.0.0.0", "255.0.0.0", "d" },
980 { "125.0.0.0", "255.0.0.0", "d" },
981 { "126.0.0.0", "255.0.0.0", "d" },
982 { "127.0.0.0", "255.0.0.0", "d" },
983 { "10.0.0.0", "255.0.0.0", "d" },
984 { "128.250.0.0", "255.255.0.0", "d" },
985 { "192.168.0.0", "255.255.0.0", "d" },
986 { "192.168.1.0", "255.255.255.0", "d" },
991 char *mtable[][1] = {
993 { "192:168:100::2" },
994 { "192:168:101::2" },
1002 { "255.255.255.0" },
1006 { "128.251.255.255" },
1009 { "192.168.255.255" },
1016 14, 13, 12, 5, 10, 3, 19, 7, 4, 20, 8,
1017 2, 17, 9, 16, 11, 15, 1, 6, 18, 0, 21
1020 static int nodecount = 0;
1021 myst_t *myst_top = NULL;
1022 tabe_t *ttable = NULL;
1024 void add_addr(ipf_rdx_head_t *, int , int);
1025 void checktree(ipf_rdx_head_t *);
1026 void delete_addr(ipf_rdx_head_t *rnh, int item);
1027 void dumptree(ipf_rdx_head_t *rnh);
1028 void nodeprinter(ipf_rdx_node_t *, void *);
1029 void printroots(ipf_rdx_head_t *);
1030 void random_add(ipf_rdx_head_t *);
1031 void random_delete(ipf_rdx_head_t *);
1032 void test_addr(ipf_rdx_head_t *rnh, int pref, addrfamily_t *, int);
1036 ipf_rx_freenode(node, arg)
1037 ipf_rdx_node_t *node;
1040 ipf_rdx_head_t *head = arg;
1044 stp = (myst_t *)node;
1045 rv = ipf_rx_delete(head, &stp->dst, &stp->mask);
1056 static char name[80];
1059 bzero((char *)name, sizeof(name));
1060 txt = inet_ntop(ap->adf_family, &ap->adf_addr, name,
1067 fill6bits(bits, msk)
1079 msk[0] = 0xffffffff;
1080 msk[1] = 0xffffffff;
1081 msk[2] = 0xffffffff;
1082 msk[3] = 0xffffffff;
1087 msk[3] = htonl(msk[3] << (128 - bits));
1088 } else if (bits > 64) {
1090 msk[2] = htonl(msk[2] << (96 - bits));
1091 } else if (bits > 32) {
1094 msk[1] = htonl(msk[1] << (64 - bits));
1099 msk[0] = htonl(msk[0] << (32 - bits));
1110 bzero((char *)afp, sizeof(*afp));
1112 if (strchr(str, ':') == NULL) {
1113 afp->adf_family = AF_INET;
1114 afp->adf_len = offsetof(addrfamily_t, adf_addr) + 4;
1116 afp->adf_family = AF_INET6;
1117 afp->adf_len = offsetof(addrfamily_t, adf_addr) + 16;
1119 inet_pton(afp->adf_family, str, &afp->adf_addr);
1128 if (strchr(str, '.') != NULL) {
1129 afp->adf_addr.in4.s_addr = inet_addr(str);
1130 afp->adf_len = offsetof(addrfamily_t, adf_addr) + 4;
1131 } else if (afp->adf_family == AF_INET) {
1132 afp->adf_addr.i6[0] = htonl(0xffffffff << (32 - atoi(str)));
1133 afp->adf_len = offsetof(addrfamily_t, adf_addr) + 4;
1134 } else if (afp->adf_family == AF_INET6) {
1135 fill6bits(atoi(str), afp->adf_addr.i6);
1136 afp->adf_len = offsetof(addrfamily_t, adf_addr) + 16;
1142 nodeprinter(node, arg)
1143 ipf_rdx_node_t *node;
1146 myst_t *stp = (myst_t *)node;
1148 printf("Node %-9.9s L %-9.9s R %-9.9s P %9.9s/%-9.9s %s/%d\n",
1150 GNAME(node[1].left), GNAME(node[1].right),
1151 GNAME(node[0].parent), GNAME(node[1].parent),
1152 addrname(&stp->dst), node[0].maskbitcount);
1153 if (stp->printed == -1)
1154 printf("!!! %d\n", stp->printed);
1164 ipf_rdx_node_t *node = &stp->nodes[0];
1166 if (stp->nodes[0].index > 0)
1167 stp = (myst_t *)&stp->nodes[-1];
1169 printf("Node %-9.9s ", node[0].name);
1170 printf("L %-9.9s ", GNAME(node[1].left));
1171 printf("R %-9.9s ", GNAME(node[1].right));
1172 printf("P %9.9s", GNAME(node[0].parent));
1173 printf("/%-9.9s ", GNAME(node[1].parent));
1174 printf("%s P%d\n", addrname(&stp->dst), stp->printed);
1187 fp = fopen("hosts", "r");
1189 while (fgets(line, sizeof(line), fp) != NULL) {
1190 s = strchr(line, '\n');
1195 tab = malloc(sizeof(*tab) * 2);
1197 tab = realloc(tab, (lines + 1) * sizeof(*tab));
1198 tab[lines - 1].host = strdup(line);
1199 s = strchr(tab[lines - 1].host, '/');
1201 tab[lines - 1].mask = s;
1202 tab[lines - 1].what = "d";
1206 tab[lines].host = NULL;
1207 tab[lines].mask = NULL;
1208 tab[lines].what = NULL;
1215 ipf_rdx_head_t *rnh;
1217 printf("Root.0.%s b %3d p %-9.9s l %-9.9s r %-9.9s\n",
1218 GNAME(&rnh->nodes[0]),
1219 rnh->nodes[0].index, GNAME(rnh->nodes[0].parent),
1220 GNAME(rnh->nodes[0].left), GNAME(rnh->nodes[0].right));
1221 printf("Root.1.%s b %3d p %-9.9s l %-9.9s r %-9.9s\n",
1222 GNAME(&rnh->nodes[1]),
1223 rnh->nodes[1].index, GNAME(rnh->nodes[1].parent),
1224 GNAME(rnh->nodes[1].left), GNAME(rnh->nodes[1].right));
1225 printf("Root.2.%s b %3d p %-9.9s l %-9.9s r %-9.9s\n",
1226 GNAME(&rnh->nodes[2]),
1227 rnh->nodes[2].index, GNAME(rnh->nodes[2].parent),
1228 GNAME(rnh->nodes[2].left), GNAME(rnh->nodes[2].right));
1233 main(int argc, char *argv[])
1236 ipf_rdx_head_t *rnh;
1244 ctx = ipf_rx_create();
1246 ipf_rx_inithead(ctx, &rnh);
1248 printf("=== ADD-0 ===\n");
1249 for (i = 0; ttable[i].host != NULL; i++) {
1250 add_addr(rnh, i, i);
1254 ipf_rx_walktree(rnh, nodeprinter, NULL);
1255 printf("=== DELETE-0 ===\n");
1256 for (i = 0; ttable[i].host != NULL; i++) {
1257 delete_addr(rnh, i);
1259 ipf_rx_walktree(rnh, nodeprinter, NULL);
1261 printf("=== ADD-1 ===\n");
1262 for (i = 0; ttable[i].host != NULL; i++) {
1263 setaddr(&af, ttable[i].host);
1264 add_addr(rnh, i, i); /*forder[i]); */
1268 ipf_rx_walktree(rnh, nodeprinter, NULL);
1269 printf("=== TEST-1 ===\n");
1270 for (i = 0; ttable[i].host != NULL; i++) {
1271 setaddr(&af, ttable[i].host);
1272 test_addr(rnh, i, &af, -1);
1275 printf("=== TEST-2 ===\n");
1276 for (i = 0; mtable[i][0] != NULL; i++) {
1277 setaddr(&af, mtable[i][0]);
1278 test_addr(rnh, i, &af, -1);
1280 printf("=== DELETE-1 ===\n");
1281 for (i = 0; ttable[i].host != NULL; i++) {
1282 if (ttable[i].what[0] != 'd')
1284 delete_addr(rnh, i);
1285 for (j = 0; ttable[j].host != NULL; j++) {
1286 setaddr(&af, ttable[j].host);
1287 test_addr(rnh, i, &af, 3);
1290 ipf_rx_walktree(rnh, nodeprinter, NULL);
1295 printf("=== ADD-2 ===\n");
1299 ipf_rx_walktree(rnh, nodeprinter, NULL);
1300 printf("=== DELETE-2 ===\n");
1305 ipf_rx_walktree(rnh, ipf_rx_freenode, rnh);
1313 ipf_rdx_head_t *rnh;
1317 printf("VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n");
1319 for (stp = myst_top; stp; stp = stp->next)
1321 printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
1326 test_addr(rnh, pref, addr, limit)
1327 ipf_rdx_head_t *rnh;
1331 static int extras[14] = { 0, -1, 1, 3, 5, 8, 9,
1332 15, 16, 19, 255, 256, 65535, 65536
1340 memset(&af, 0, sizeof(af));
1342 if (limit < 0 || limit > 14)
1345 for (i = 0; i < limit; i++) {
1346 if (ttable[i].host == NULL)
1348 setaddr(&af, ttable[i].host);
1349 printf("%d.%d.LOOKUP(%s)", pref, i, addrname(&af));
1350 rn = ipf_rx_match(rnh, &af);
1352 printf(" = %s (%s/%d)\n", GNAME(rn),
1353 rn ? addrname(&stp->dst) : "NULL",
1354 rn ? rn->maskbitcount : 0);
1357 printf("%d.%d.LOOKUP(%s)", pref, -1, addrname(addr));
1358 rn = ipf_rx_match(rnh, addr);
1360 printf(" = %s (%s/%d)\n", GNAME(rn),
1361 rn ? addrname(&stp->dst) : "NULL", rn ? rn->maskbitcount : 0);
1367 delete_addr(rnh, item)
1368 ipf_rdx_head_t *rnh;
1377 memset(&af, 0, sizeof(af));
1378 memset(&mask, 0, sizeof(mask));
1379 setaddr(&af, ttable[item].host);
1380 mask.adf_family = af.adf_family;
1381 setmask(&mask, ttable[item].mask);
1383 printf("DELETE(%s)\n", addrname(&af));
1384 rn = ipf_rx_delete(rnh, &af, &mask);
1386 printf("FAIL LOOKUP DELETE\n");
1388 for (stp = myst_top; stp != NULL; stp = stp->next)
1389 if (stp->printed != -1)
1391 ipf_rx_walktree(rnh, nodeprinter, NULL);
1395 printf("%d.delete(%s) = %s\n", item, addrname(&af), GNAME(rn));
1397 for (pstp = &myst_top; (stp = *pstp) != NULL; pstp = &stp->next)
1398 if (stp == (myst_t *)rn)
1401 stp->nodes[0].parent = &stp->nodes[0];
1402 stp->nodes[1].parent = &stp->nodes[1];
1411 add_addr(rnh, n, item)
1412 ipf_rdx_head_t *rnh;
1418 stp = calloc(1, sizeof(*stp));
1419 rn = (ipf_rdx_node_t *)stp;
1420 setaddr(&stp->dst, ttable[item].host);
1421 stp->mask.adf_family = stp->dst.adf_family;
1422 setmask(&stp->mask, ttable[item].mask);
1423 stp->next = myst_top;
1425 (void) sprintf(rn[0].name, "_BORN.0");
1426 (void) sprintf(rn[1].name, "_BORN.1");
1427 rn = ipf_rx_addroute(rnh, &stp->dst, &stp->mask, stp->nodes);
1428 (void) sprintf(rn[0].name, "%d_NODE.0", item);
1429 (void) sprintf(rn[1].name, "%d_NODE.1", item);
1430 printf("ADD %d/%d %s/%s\n", n, item, rn[0].name, rn[1].name);
1437 checktree(ipf_rdx_head_t *head)
1445 for (s1 = myst_top; s1 != NULL; s1 = s1->next) {
1447 if (s1->printed == -1)
1450 if (rn->right->parent != rn)
1452 if (rn->left->parent != rn)
1454 if (rn->parent->left != rn && rn->parent->right != rn)
1457 printf("FAULT %#x %s\n", fault, rn->name);
1459 ipf_rx_walktree(head, nodeprinter, NULL);
1470 randomize(int *pnitems)
1478 nitems = sizeof(ttable) / sizeof(ttable[0]);
1480 order = calloc(nitems, sizeof(*order));
1481 srandom(getpid() * time(NULL));
1482 memset(order, 0xff, nitems * sizeof(*order));
1484 for (i = 0; i < nitems - 1; i++) {
1486 choice = rand() % (nitems - 1);
1487 for (j = 0; j < nitems; j++)
1488 if (order[j] == choice)
1490 } while (j != nitems);
1500 ipf_rdx_head_t *rnh;
1506 order = randomize(&nitems);
1508 for (i = 0; i < nitems - 1; i++) {
1509 add_addr(rnh, i, order[i]);
1517 ipf_rdx_head_t *rnh;
1523 order = randomize(&nitems);
1525 for (i = 0; i < nitems - 1; i++) {
1526 delete_addr(rnh, i);
1530 #endif /* RDX_DEBUG */