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>
18 #include "netinet/ip_compat.h"
19 #include "netinet/ip_fil.h"
21 # include <arpa/inet.h>
25 #include "netinet/radix_ipf.h"
27 #define ADF_OFF offsetof(addrfamily_t, adf_addr)
28 #define ADF_OFF_BITS (ADF_OFF << 3)
30 static ipf_rdx_node_t *ipf_rx_insert __P((ipf_rdx_head_t *,
31 ipf_rdx_node_t nodes[2], int *));
32 static void ipf_rx_attach_mask __P((ipf_rdx_node_t *, ipf_rdx_mask_t *));
33 static int count_mask_bits __P((addrfamily_t *, u_32_t **));
34 static void buildnodes __P((addrfamily_t *, addrfamily_t *,
35 ipf_rdx_node_t n[2]));
36 static ipf_rdx_node_t *ipf_rx_find_addr __P((ipf_rdx_node_t *, u_32_t *));
37 static ipf_rdx_node_t *ipf_rx_lookup __P((ipf_rdx_head_t *, addrfamily_t *,
39 static ipf_rdx_node_t *ipf_rx_match __P((ipf_rdx_head_t *, addrfamily_t *));
44 * The code in this file has been written to target using the addrfamily_t
45 * data structure to house the address information and no other. Thus there
46 * are certain aspects of thise code (such as offsets to the address itself)
47 * that are hard coded here whilst they might be more variable elsewhere.
48 * Similarly, this code enforces no maximum key length as that's implied by
49 * all keys needing to be stored in addrfamily_t.
52 /* ------------------------------------------------------------------------ */
53 /* Function: count_mask_bits */
54 /* Returns: number of consecutive bits starting at "mask". */
56 /* Count the number of bits set in the address section of addrfamily_t and */
57 /* return both that number and a pointer to the last word with a bit set if */
58 /* lastp is not NULL. The bit count is performed using network byte order */
59 /* as the guide for which bit is the most significant bit. */
60 /* ------------------------------------------------------------------------ */
62 count_mask_bits(mask, lastp)
66 u_32_t *mp = (u_32_t *)&mask->adf_addr;
71 mlen = mask->adf_len - offsetof(addrfamily_t, adf_addr);
72 for (; mlen > 0; mlen -= 4, mp++) {
73 if ((m = ntohl(*mp)) == 0)
77 for (; m & 0x80000000; m <<= 1)
85 /* ------------------------------------------------------------------------ */
86 /* Function: buildnodes */
88 /* Parameters: addr(I) - network address for this radix node */
89 /* mask(I) - netmask associated with the above address */
90 /* nodes(O) - pair of ipf_rdx_node_t's to initialise with data */
91 /* associated with addr and mask. */
93 /* Initialise the fields in a pair of radix tree nodes according to the */
94 /* data supplied in the paramters "addr" and "mask". It is expected that */
95 /* "mask" will contain a consecutive string of bits set. Masks with gaps in */
96 /* the middle are not handled by this implementation. */
97 /* ------------------------------------------------------------------------ */
99 buildnodes(addr, mask, nodes)
100 addrfamily_t *addr, *mask;
101 ipf_rdx_node_t nodes[2];
110 maskbits = count_mask_bits(mask, &last);
115 masklen = last - (u_32_t *)mask;
118 lastbits = maskbits & 0x1f;
120 bzero(&nodes[0], sizeof(ipf_rdx_node_t) * 2);
121 nodes[0].maskbitcount = maskbits;
122 nodes[0].index = -1 - (ADF_OFF_BITS + maskbits);
123 nodes[0].addrkey = (u_32_t *)addr;
124 nodes[0].maskkey = (u_32_t *)mask;
125 nodes[0].addroff = nodes[0].addrkey + masklen;
126 nodes[0].maskoff = nodes[0].maskkey + masklen;
127 nodes[0].parent = &nodes[1];
128 nodes[0].offset = masklen;
129 nodes[0].lastmask = lastmask;
130 nodes[1].offset = masklen;
131 nodes[1].left = &nodes[0];
132 nodes[1].maskbitcount = maskbits;
134 (void) strcpy(nodes[0].name, "_BUILD.0");
135 (void) strcpy(nodes[1].name, "_BUILD.1");
140 /* ------------------------------------------------------------------------ */
141 /* Function: ipf_rx_find_addr */
142 /* Returns: ipf_rdx_node_t * - pointer to a node in the radix tree. */
143 /* Parameters: tree(I) - pointer to first right node in tree to search */
144 /* addr(I) - pointer to address to match */
146 /* Walk the radix tree given by "tree", looking for a leaf node that is a */
147 /* match for the address given by "addr". */
148 /* ------------------------------------------------------------------------ */
149 static ipf_rdx_node_t *
150 ipf_rx_find_addr(tree, addr)
151 ipf_rdx_node_t *tree;
156 for (cur = tree; cur->index >= 0;) {
157 if (cur->bitmask & addr[cur->offset]) {
168 /* ------------------------------------------------------------------------ */
169 /* Function: ipf_rx_match */
170 /* Returns: ipf_rdx_node_t * - NULL on error, else pointer to the node */
171 /* added to the tree. */
172 /* Paramters: head(I) - pointer to tree head to search */
173 /* addr(I) - pointer to address to find */
175 /* Search the radix tree for the best match to the address pointed to by */
176 /* "addr" and return a pointer to that node. This search will not match the */
177 /* address information stored in either of the root leaves as neither of */
178 /* them are considered to be part of the tree of data being stored. */
179 /* ------------------------------------------------------------------------ */
180 static ipf_rdx_node_t *
181 ipf_rx_match(head, addr)
182 ipf_rdx_head_t *head;
185 ipf_rdx_mask_t *masknode;
186 ipf_rdx_node_t *prev;
187 ipf_rdx_node_t *node;
197 end = (u_32_t *)((u_char *)addr + len);
198 node = ipf_rx_find_addr(head->root, (u_32_t *)addr);
201 * Search the dupkey list for a potential match.
203 for (cur = node; (cur != NULL) && (cur->root == 0); cur = cur->dupkey) {
204 i = cur[0].addroff - cur[0].addrkey;
205 data = cur[0].addrkey + i;
206 mask = cur[0].maskkey + i;
207 key = (u_32_t *)addr + i;
208 for (; key < end; data++, key++, mask++)
209 if ((*key & *mask) != *data)
211 if ((end == key) && (cur->root == 0))
212 return (cur); /* Equal keys */
215 key = (u_32_t *)addr;
217 for (node = prev; node->root == 0; node = node->parent) {
219 * We know that the node hasn't matched so therefore only
220 * the entries in the mask list are searched, not the top
221 * node nor the dupkey list.
223 masknode = node->masks;
224 for (; masknode != NULL; masknode = masknode->next) {
225 if (masknode->maskbitcount > node->maskbitcount)
227 cur = masknode->node;
228 for (i = ADF_OFF >> 2; i <= node->offset; i++) {
229 if ((key[i] & masknode->mask[i]) ==
240 /* ------------------------------------------------------------------------ */
241 /* Function: ipf_rx_lookup */
242 /* Returns: ipf_rdx_node_t * - NULL on error, else pointer to the node */
243 /* added to the tree. */
244 /* Paramters: head(I) - pointer to tree head to search */
245 /* addr(I) - address part of the key to match */
246 /* mask(I) - netmask part of the key to match */
248 /* ipf_rx_lookup searches for an exact match on (addr,mask). The intention */
249 /* is to see if a given key is in the tree, not to see if a route exists. */
250 /* ------------------------------------------------------------------------ */
252 ipf_rx_lookup(head, addr, mask)
253 ipf_rdx_head_t *head;
254 addrfamily_t *addr, *mask;
256 ipf_rdx_node_t *found;
257 ipf_rdx_node_t *node;
261 found = ipf_rx_find_addr(head->root, (u_32_t *)addr);
262 if (found->root == 1)
266 * It is possible to find a matching address in the tree but for the
267 * netmask to not match. If the netmask does not match and there is
268 * no list of alternatives present at dupkey, return a failure.
270 count = count_mask_bits(mask, NULL);
271 if (count != found->maskbitcount && found->dupkey == NULL)
274 akey = (u_32_t *)addr;
275 if ((found->addrkey[found->offset] & found->maskkey[found->offset]) !=
279 if (found->dupkey != NULL) {
281 while (node != NULL && node->maskbitcount != count)
291 /* ------------------------------------------------------------------------ */
292 /* Function: ipf_rx_attach_mask */
294 /* Parameters: node(I) - pointer to a radix tree node */
295 /* mask(I) - pointer to mask structure to add */
297 /* Add the netmask to the given node in an ordering where the most specific */
298 /* netmask is at the top of the list. */
299 /* ------------------------------------------------------------------------ */
301 ipf_rx_attach_mask(node, mask)
302 ipf_rdx_node_t *node;
303 ipf_rdx_mask_t *mask;
308 for (pm = &node->masks; (m = *pm) != NULL; pm = &m->next)
309 if (m->maskbitcount < mask->maskbitcount)
316 /* ------------------------------------------------------------------------ */
317 /* Function: ipf_rx_insert */
318 /* Returns: ipf_rdx_node_t * - NULL on error, else pointer to the node */
319 /* added to the tree. */
320 /* Paramters: head(I) - pointer to tree head to add nodes to */
321 /* nodes(I) - pointer to radix nodes to be added */
322 /* dup(O) - set to 1 if node is a duplicate, else 0. */
324 /* Add the new radix tree entry that owns nodes[] to the tree given by head.*/
325 /* If there is already a matching key in the table, "dup" will be set to 1 */
326 /* and the existing node pointer returned if there is a complete key match. */
327 /* A complete key match is a matching of all key data that is presented by */
328 /* by the netmask. */
329 /* ------------------------------------------------------------------------ */
330 static ipf_rdx_node_t *
331 ipf_rx_insert(head, nodes, dup)
332 ipf_rdx_head_t *head;
333 ipf_rdx_node_t nodes[2];
336 ipf_rdx_mask_t **pmask;
337 ipf_rdx_node_t *node;
338 ipf_rdx_node_t *prev;
339 ipf_rdx_mask_t *mask;
353 addr = nodes[0].addrkey;
355 node = ipf_rx_find_addr(head->root, addr);
356 len = ((addrfamily_t *)addr)->adf_len;
357 key = (u_32_t *)&((addrfamily_t *)addr)->adf_addr;
358 data= (u_32_t *)&((addrfamily_t *)node->addrkey)->adf_addr;
359 end = (u_32_t *)((u_char *)addr + len);
360 for (nlen = 0; key < end; data++, key++, nlen += 32)
365 return (node); /* Equal keys */
369 bits = (ntohl(*data) ^ ntohl(*key));
370 for (; bits != 0; nlen++) {
371 if ((bits & 0x80000000) != 0)
375 nlen += ADF_OFF_BITS;
376 nodes[1].index = nlen;
377 nodes[1].bitmask = htonl(0x80000000 >> (nlen & 0x1f));
378 nodes[0].offset = nlen / 32;
379 nodes[1].offset = nlen / 32;
382 * Walk through the tree and look for the correct place to attach
383 * this node. ipf_rx_fin_addr is not used here because the place
384 * to attach this node may be an internal node (same key, different
385 * netmask.) Additionally, the depth of the search is forcibly limited
386 * here to not exceed the netmask, so that a short netmask will be
387 * added higher up the tree even if there are lower branches.
390 key = nodes[0].addrkey;
393 if (key[cur->offset] & cur->bitmask) {
398 } while (nlen > (unsigned)cur->index);
400 if ((key[prev->offset] & prev->bitmask) == 0) {
401 prev->left = &nodes[1];
403 prev->right = &nodes[1];
405 cur->parent = &nodes[1];
406 nodes[1].parent = prev;
407 if ((key[nodes[1].offset] & nodes[1].bitmask) == 0) {
408 nodes[1].right = cur;
410 nodes[1].right = &nodes[0];
414 nodeoff = nodes[0].offset;
415 nodekey = nodes[0].addrkey[nodeoff];
416 nodemask = nodes[0].lastmask;
417 nodebits = nodes[0].maskbitcount;
420 * Find the node up the tree with the largest pattern that still
421 * matches the node being inserted to see if this mask can be
424 for (cur = nodes[1].parent; cur->root == 0; cur = cur->parent) {
425 if (cur->maskbitcount <= nodebits)
427 if (((cur - 1)->addrkey[nodeoff] & nodemask) != nodekey)
432 KMALLOC(mask, ipf_rdx_mask_t *);
435 bzero(mask, sizeof(*mask));
437 mask->node = &nodes[0];
438 mask->maskbitcount = nodebits;
439 mask->mask = nodes[0].maskkey;
440 nodes[0].mymask = mask;
445 for (pmask = &prev->masks; (m = *pmask) != NULL;
447 if (m->maskbitcount < nodebits)
452 * No higher up nodes qualify, so attach mask locally.
454 pmask = &nodes[0].masks;
460 * Search the mask list on each child to see if there are any masks
461 * there that can be moved up to this newly inserted node.
463 cur = nodes[1].right;
464 if (cur->root == 0) {
465 for (pmask = &cur->masks; (mask = *pmask) != NULL; ) {
466 if (mask->maskbitcount < nodebits) {
468 ipf_rx_attach_mask(&nodes[0], mask);
475 if (cur->root == 0 && cur != &nodes[0]) {
476 for (pmask = &cur->masks; (mask = *pmask) != NULL; ) {
477 if (mask->maskbitcount < nodebits) {
479 ipf_rx_attach_mask(&nodes[0], mask);
488 /* ------------------------------------------------------------------------ */
489 /* Function: ipf_rx_addroute */
490 /* Returns: ipf_rdx_node_t * - NULL on error, else pointer to the node */
491 /* added to the tree. */
492 /* Paramters: head(I) - pointer to tree head to search */
493 /* addr(I) - address portion of "route" to add */
494 /* mask(I) - netmask portion of "route" to add */
495 /* nodes(I) - radix tree data nodes inside allocate structure */
497 /* Attempt to add a node to the radix tree. The key for the node is the */
498 /* (addr,mask). No memory allocation for the radix nodes themselves is */
499 /* performed here, the data structure that this radix node is being used to */
500 /* find is expected to house the node data itself however the call to */
501 /* ipf_rx_insert() will attempt to allocate memory in order for netmask to */
502 /* be promoted further up the tree. */
503 /* In this case, the ip_pool_node_t structure from ip_pool.h contains both */
504 /* the key material (addr,mask) and the radix tree nodes[]. */
506 /* The mechanics of inserting the node into the tree is handled by the */
507 /* function ipf_rx_insert() above. Here, the code deals with the case */
508 /* where the data to be inserted is a duplicate. */
509 /* ------------------------------------------------------------------------ */
511 ipf_rx_addroute(head, addr, mask, nodes)
512 ipf_rdx_head_t *head;
513 addrfamily_t *addr, *mask;
514 ipf_rdx_node_t *nodes;
516 ipf_rdx_node_t *node;
517 ipf_rdx_node_t *prev;
521 buildnodes(addr, mask, nodes);
522 x = ipf_rx_insert(head, nodes, &dup);
530 * The duplicate list is kept sorted with the longest
531 * mask at the top, meaning that the most specific entry
532 * in the listis found first. This list thus allows for
533 * duplicates such as 128.128.0.0/32 and 128.128.0.0/16.
535 while ((x != NULL) && (x->maskbitcount > node->maskbitcount)) {
541 * Is it a complete duplicate? If so, return NULL and
542 * fail the insert. Otherwise, insert it into the list
543 * of netmasks active for this key.
545 if ((x != NULL) && (x->maskbitcount == node->maskbitcount))
550 prev->dupkey = &nodes[0];
551 nodes[0].parent = prev;
553 x->parent = &nodes[0];
555 nodes[0].dupkey = x->dupkey;
557 nodes[0].parent = prev;
558 x->parent = &nodes[0];
560 prev->left = &nodes[0];
562 prev->right = &nodes[0];
570 /* ------------------------------------------------------------------------ */
571 /* Function: ipf_rx_delete */
572 /* Returns: ipf_rdx_node_t * - NULL on error, else node removed from */
574 /* Paramters: head(I) - pointer to tree head to search */
575 /* addr(I) - pointer to the address part of the key */
576 /* mask(I) - pointer to the netmask part of the key */
578 /* Search for an entry in the radix tree that is an exact match for (addr, */
579 /* mask) and remove it if it exists. In the case where (addr,mask) is a not */
580 /* a unique key, the tree structure itself is not changed - only the list */
581 /* of duplicate keys. */
582 /* ------------------------------------------------------------------------ */
584 ipf_rx_delete(head, addr, mask)
585 ipf_rdx_head_t *head;
586 addrfamily_t *addr, *mask;
588 ipf_rdx_mask_t **pmask;
589 ipf_rdx_node_t *parent;
590 ipf_rdx_node_t *found;
591 ipf_rdx_node_t *prev;
592 ipf_rdx_node_t *node;
597 found = ipf_rx_find_addr(head->root, (u_32_t *)addr);
600 if (found->root == 1)
602 count = count_mask_bits(mask, NULL);
603 parent = found->parent;
604 if (found->dupkey != NULL) {
606 while (node != NULL && node->maskbitcount != count)
612 * Remove from the dupkey list. Here, "parent" is
613 * the previous node on the list (rather than tree)
614 * and "dupkey" is the next node on the list.
616 parent = node->parent;
617 parent->dupkey = node->dupkey;
618 node->dupkey->parent = parent;
622 * When removing the top node of the dupkey list,
623 * the pointers at the top of the list that point
624 * to other tree nodes need to be preserved and
625 * any children must have their parent updated.
628 node->parent = found->parent;
629 node->right = found->right;
630 node->left = found->left;
631 found->right->parent = node;
632 found->left->parent = node;
633 if (parent->left == found)
639 if (count != found->maskbitcount)
642 * Remove the node from the tree and reconnect the subtree
646 * If there is a tree to the left, look for something to
647 * attach in place of "found".
650 cur = parent->parent;
651 if (parent != found + 1) {
652 if ((found + 1)->parent->right == found + 1)
653 (found + 1)->parent->right = parent;
655 (found + 1)->parent->left = parent;
656 if (cur->right == parent) {
657 if (parent->left == found) {
658 cur->right = parent->right;
659 } else if (parent->left != parent - 1) {
660 cur->right = parent->left;
662 cur->right = parent - 1;
664 cur->right->parent = cur;
666 if (parent->right == found) {
667 cur->left = parent->left;
668 } else if (parent->right != parent - 1) {
669 cur->left = parent->right;
671 cur->left = parent - 1;
673 cur->left->parent = cur;
675 parent->left = (found + 1)->left;
676 if ((found + 1)->right != parent)
677 parent->right = (found + 1)->right;
678 parent->left->parent = parent;
679 parent->right->parent = parent;
680 parent->parent = (found + 1)->parent;
682 parent->bitmask = prev->bitmask;
683 parent->offset = prev->offset;
684 parent->index = prev->index;
687 * We found an edge node.
689 cur = parent->parent;
690 if (cur->left == parent) {
691 if (parent->left == found) {
692 cur->left = parent->right;
693 parent->right->parent = cur;
695 cur->left = parent->left;
696 parent->left->parent = cur;
699 if (parent->right != found) {
700 cur->right = parent->right;
701 parent->right->parent = cur;
703 cur->right = parent->left;
704 prev->left->parent = cur;
711 * Remove mask associated with this node.
713 for (cur = parent; cur->root == 0; cur = cur->parent) {
716 if (cur->maskbitcount <= found->maskbitcount)
718 if (((cur - 1)->addrkey[found->offset] & found->bitmask) !=
719 found->addrkey[found->offset])
721 for (pm = &cur->masks; (m = *pm) != NULL; )
722 if (m->node == cur) {
729 KFREE(found->mymask);
732 * Masks that have been brought up to this node from below need to
735 for (pmask = &parent->masks; (m = *pmask) != NULL; ) {
740 if (found->addrkey[cur->offset] & cur->lastmask) {
741 ipf_rx_attach_mask(parent->right, m);
742 } else if (parent->left != found) {
743 ipf_rx_attach_mask(parent->left, m);
751 /* ------------------------------------------------------------------------ */
752 /* Function: ipf_rx_walktree */
754 /* Paramters: head(I) - pointer to tree head to search */
755 /* walker(I) - function to call for each node in the tree */
756 /* arg(I) - parameter to pass to walker, in addition to the */
759 /* A standard tree walking function except that it is iterative, rather */
760 /* than recursive and tracks the next node in case the "walker" function */
761 /* should happen to delete and free the current node. It thus goes without */
762 /* saying that the "walker" function is not permitted to cause any change */
763 /* in the validity of the data found at either the left or right child. */
764 /* ------------------------------------------------------------------------ */
766 ipf_rx_walktree(head, walker, arg)
767 ipf_rdx_head_t *head;
768 radix_walk_func_t walker;
771 ipf_rdx_node_t *next;
772 ipf_rdx_node_t *node = head->root;
773 ipf_rdx_node_t *base;
775 while (node->index >= 0)
780 while ((node->parent->right == node) && (node->root == 0))
783 for (node = node->parent->right; node->index >= 0; )
787 for (node = base; node != NULL; node = base) {
799 /* ------------------------------------------------------------------------ */
800 /* Function: ipf_rx_inithead */
801 /* Returns: int - 0 = success, else failure */
802 /* Paramters: softr(I) - pointer to radix context */
803 /* headp(O) - location for where to store allocated tree head */
805 /* This function allocates and initialises a radix tree head structure. */
806 /* As a traditional radix tree, node 0 is used as the "0" sentinel and node */
807 /* "2" is used as the all ones sentinel, leaving node "1" as the root from */
808 /* which the tree is hung with node "0" on its left and node "2" to the */
809 /* right. The context, "softr", is used here to provide a common source of */
810 /* the zeroes and ones data rather than have one per head. */
811 /* ------------------------------------------------------------------------ */
813 ipf_rx_inithead(softr, headp)
814 radix_softc_t *softr;
815 ipf_rdx_head_t **headp;
818 ipf_rdx_node_t *node;
820 KMALLOC(ptr, ipf_rdx_head_t *);
824 bzero(ptr, sizeof(*ptr));
826 ptr->root = node + 1;
827 node[0].index = ADF_OFF_BITS;
828 node[0].index = -1 - node[0].index;
829 node[1].index = ADF_OFF_BITS;
830 node[2].index = node[0].index;
831 node[0].parent = node + 1;
832 node[1].parent = node + 1;
833 node[2].parent = node + 1;
834 node[1].bitmask = htonl(0x80000000);
838 node[0].offset = ADF_OFF_BITS >> 5;
839 node[1].offset = ADF_OFF_BITS >> 5;
840 node[2].offset = ADF_OFF_BITS >> 5;
841 node[1].left = &node[0];
842 node[1].right = &node[2];
843 node[0].addrkey = (u_32_t *)softr->zeros;
844 node[2].addrkey = (u_32_t *)softr->ones;
846 (void) strcpy(node[0].name, "0_ROOT");
847 (void) strcpy(node[1].name, "1_ROOT");
848 (void) strcpy(node[2].name, "2_ROOT");
851 ptr->addaddr = ipf_rx_addroute;
852 ptr->deladdr = ipf_rx_delete;
853 ptr->lookup = ipf_rx_lookup;
854 ptr->matchaddr = ipf_rx_match;
855 ptr->walktree = ipf_rx_walktree;
860 /* ------------------------------------------------------------------------ */
861 /* Function: ipf_rx_freehead */
863 /* Paramters: head(I) - pointer to tree head to free */
865 /* This function simply free's up the radix tree head. Prior to calling */
866 /* this function, it is expected that the tree will have been emptied. */
867 /* ------------------------------------------------------------------------ */
869 ipf_rx_freehead(head)
870 ipf_rdx_head_t *head;
876 /* ------------------------------------------------------------------------ */
877 /* Function: ipf_rx_create */
878 /* Parameters: Nil */
880 /* ------------------------------------------------------------------------ */
884 radix_softc_t *softr;
886 KMALLOC(softr, radix_softc_t *);
889 bzero((char *)softr, sizeof(*softr));
891 KMALLOCS(softr->zeros, u_char *, 3 * sizeof(addrfamily_t));
892 if (softr->zeros == NULL) {
896 softr->ones = softr->zeros + sizeof(addrfamily_t);
902 /* ------------------------------------------------------------------------ */
903 /* Function: ipf_rx_init */
904 /* Returns: int - 0 = success (always) */
906 /* ------------------------------------------------------------------------ */
911 radix_softc_t *softr = ctx;
913 memset(softr->zeros, 0, 3 * sizeof(addrfamily_t));
914 memset(softr->ones, 0xff, sizeof(addrfamily_t));
920 /* ------------------------------------------------------------------------ */
921 /* Function: ipf_rx_destroy */
924 /* ------------------------------------------------------------------------ */
929 radix_softc_t *softr = ctx;
931 if (softr->zeros != NULL)
932 KFREES(softr->zeros, 3 * sizeof(addrfamily_t));
936 /* ====================================================================== */
940 * To compile this file as a standalone test unit, use -DRDX_DEBUG=1
942 #define NAME(x) ((x)->index < 0 ? (x)->name : (x)->name)
943 #define GNAME(y) ((y) == NULL ? "NULL" : NAME(y))
945 typedef struct myst {
946 struct ipf_rdx_node nodes[2];
953 typedef struct tabe_s {
961 { "192:168:100::0", "48", "d" },
962 { "192:168:100::2", "128", "d" },
964 { "127.192.0.0", "255.255.255.0", "d" },
965 { "127.128.0.0", "255.255.255.0", "d" },
966 { "127.96.0.0", "255.255.255.0", "d" },
967 { "127.80.0.0", "255.255.255.0", "d" },
968 { "127.72.0.0", "255.255.255.0", "d" },
969 { "127.64.0.0", "255.255.255.0", "d" },
970 { "127.56.0.0", "255.255.255.0", "d" },
971 { "127.48.0.0", "255.255.255.0", "d" },
972 { "127.40.0.0", "255.255.255.0", "d" },
973 { "127.32.0.0", "255.255.255.0", "d" },
974 { "127.24.0.0", "255.255.255.0", "d" },
975 { "127.16.0.0", "255.255.255.0", "d" },
976 { "127.8.0.0", "255.255.255.0", "d" },
977 { "124.0.0.0", "255.0.0.0", "d" },
978 { "125.0.0.0", "255.0.0.0", "d" },
979 { "126.0.0.0", "255.0.0.0", "d" },
980 { "127.0.0.0", "255.0.0.0", "d" },
981 { "10.0.0.0", "255.0.0.0", "d" },
982 { "128.250.0.0", "255.255.0.0", "d" },
983 { "192.168.0.0", "255.255.0.0", "d" },
984 { "192.168.1.0", "255.255.255.0", "d" },
989 char *mtable[][1] = {
991 { "192:168:100::2" },
992 { "192:168:101::2" },
1000 { "255.255.255.0" },
1004 { "128.251.255.255" },
1007 { "192.168.255.255" },
1014 14, 13, 12, 5, 10, 3, 19, 7, 4, 20, 8,
1015 2, 17, 9, 16, 11, 15, 1, 6, 18, 0, 21
1018 static int nodecount = 0;
1019 myst_t *myst_top = NULL;
1020 tabe_t *ttable = NULL;
1022 void add_addr(ipf_rdx_head_t *, int , int);
1023 void checktree(ipf_rdx_head_t *);
1024 void delete_addr(ipf_rdx_head_t *rnh, int item);
1025 void dumptree(ipf_rdx_head_t *rnh);
1026 void nodeprinter(ipf_rdx_node_t *, void *);
1027 void printroots(ipf_rdx_head_t *);
1028 void random_add(ipf_rdx_head_t *);
1029 void random_delete(ipf_rdx_head_t *);
1030 void test_addr(ipf_rdx_head_t *rnh, int pref, addrfamily_t *, int);
1034 ipf_rx_freenode(node, arg)
1035 ipf_rdx_node_t *node;
1038 ipf_rdx_head_t *head = arg;
1042 stp = (myst_t *)node;
1043 rv = ipf_rx_delete(head, &stp->dst, &stp->mask);
1054 static char name[80];
1057 bzero((char *)name, sizeof(name));
1058 txt = inet_ntop(ap->adf_family, &ap->adf_addr, name,
1065 fill6bits(bits, msk)
1077 msk[0] = 0xffffffff;
1078 msk[1] = 0xffffffff;
1079 msk[2] = 0xffffffff;
1080 msk[3] = 0xffffffff;
1085 msk[3] = htonl(msk[3] << (128 - bits));
1086 } else if (bits > 64) {
1088 msk[2] = htonl(msk[2] << (96 - bits));
1089 } else if (bits > 32) {
1092 msk[1] = htonl(msk[1] << (64 - bits));
1097 msk[0] = htonl(msk[0] << (32 - bits));
1108 bzero((char *)afp, sizeof(*afp));
1110 if (strchr(str, ':') == NULL) {
1111 afp->adf_family = AF_INET;
1112 afp->adf_len = offsetof(addrfamily_t, adf_addr) + 4;
1114 afp->adf_family = AF_INET6;
1115 afp->adf_len = offsetof(addrfamily_t, adf_addr) + 16;
1117 inet_pton(afp->adf_family, str, &afp->adf_addr);
1126 if (strchr(str, '.') != NULL) {
1127 afp->adf_addr.in4.s_addr = inet_addr(str);
1128 afp->adf_len = offsetof(addrfamily_t, adf_addr) + 4;
1129 } else if (afp->adf_family == AF_INET) {
1130 afp->adf_addr.i6[0] = htonl(0xffffffff << (32 - atoi(str)));
1131 afp->adf_len = offsetof(addrfamily_t, adf_addr) + 4;
1132 } else if (afp->adf_family == AF_INET6) {
1133 fill6bits(atoi(str), afp->adf_addr.i6);
1134 afp->adf_len = offsetof(addrfamily_t, adf_addr) + 16;
1140 nodeprinter(node, arg)
1141 ipf_rdx_node_t *node;
1144 myst_t *stp = (myst_t *)node;
1146 printf("Node %-9.9s L %-9.9s R %-9.9s P %9.9s/%-9.9s %s/%d\n",
1148 GNAME(node[1].left), GNAME(node[1].right),
1149 GNAME(node[0].parent), GNAME(node[1].parent),
1150 addrname(&stp->dst), node[0].maskbitcount);
1151 if (stp->printed == -1)
1152 printf("!!! %d\n", stp->printed);
1162 ipf_rdx_node_t *node = &stp->nodes[0];
1164 if (stp->nodes[0].index > 0)
1165 stp = (myst_t *)&stp->nodes[-1];
1167 printf("Node %-9.9s ", node[0].name);
1168 printf("L %-9.9s ", GNAME(node[1].left));
1169 printf("R %-9.9s ", GNAME(node[1].right));
1170 printf("P %9.9s", GNAME(node[0].parent));
1171 printf("/%-9.9s ", GNAME(node[1].parent));
1172 printf("%s P%d\n", addrname(&stp->dst), stp->printed);
1185 fp = fopen("hosts", "r");
1187 while (fgets(line, sizeof(line), fp) != NULL) {
1188 s = strchr(line, '\n');
1193 tab = malloc(sizeof(*tab) * 2);
1195 tab = reallocarray(tab, lines + 1, sizeof(*tab));
1196 tab[lines - 1].host = strdup(line);
1197 s = strchr(tab[lines - 1].host, '/');
1199 tab[lines - 1].mask = s;
1200 tab[lines - 1].what = "d";
1204 tab[lines].host = NULL;
1205 tab[lines].mask = NULL;
1206 tab[lines].what = NULL;
1213 ipf_rdx_head_t *rnh;
1215 printf("Root.0.%s b %3d p %-9.9s l %-9.9s r %-9.9s\n",
1216 GNAME(&rnh->nodes[0]),
1217 rnh->nodes[0].index, GNAME(rnh->nodes[0].parent),
1218 GNAME(rnh->nodes[0].left), GNAME(rnh->nodes[0].right));
1219 printf("Root.1.%s b %3d p %-9.9s l %-9.9s r %-9.9s\n",
1220 GNAME(&rnh->nodes[1]),
1221 rnh->nodes[1].index, GNAME(rnh->nodes[1].parent),
1222 GNAME(rnh->nodes[1].left), GNAME(rnh->nodes[1].right));
1223 printf("Root.2.%s b %3d p %-9.9s l %-9.9s r %-9.9s\n",
1224 GNAME(&rnh->nodes[2]),
1225 rnh->nodes[2].index, GNAME(rnh->nodes[2].parent),
1226 GNAME(rnh->nodes[2].left), GNAME(rnh->nodes[2].right));
1231 main(int argc, char *argv[])
1234 ipf_rdx_head_t *rnh;
1242 ctx = ipf_rx_create();
1244 ipf_rx_inithead(ctx, &rnh);
1246 printf("=== ADD-0 ===\n");
1247 for (i = 0; ttable[i].host != NULL; i++) {
1248 add_addr(rnh, i, i);
1252 ipf_rx_walktree(rnh, nodeprinter, NULL);
1253 printf("=== DELETE-0 ===\n");
1254 for (i = 0; ttable[i].host != NULL; i++) {
1255 delete_addr(rnh, i);
1257 ipf_rx_walktree(rnh, nodeprinter, NULL);
1259 printf("=== ADD-1 ===\n");
1260 for (i = 0; ttable[i].host != NULL; i++) {
1261 setaddr(&af, ttable[i].host);
1262 add_addr(rnh, i, i); /*forder[i]); */
1266 ipf_rx_walktree(rnh, nodeprinter, NULL);
1267 printf("=== TEST-1 ===\n");
1268 for (i = 0; ttable[i].host != NULL; i++) {
1269 setaddr(&af, ttable[i].host);
1270 test_addr(rnh, i, &af, -1);
1273 printf("=== TEST-2 ===\n");
1274 for (i = 0; mtable[i][0] != NULL; i++) {
1275 setaddr(&af, mtable[i][0]);
1276 test_addr(rnh, i, &af, -1);
1278 printf("=== DELETE-1 ===\n");
1279 for (i = 0; ttable[i].host != NULL; i++) {
1280 if (ttable[i].what[0] != 'd')
1282 delete_addr(rnh, i);
1283 for (j = 0; ttable[j].host != NULL; j++) {
1284 setaddr(&af, ttable[j].host);
1285 test_addr(rnh, i, &af, 3);
1288 ipf_rx_walktree(rnh, nodeprinter, NULL);
1293 printf("=== ADD-2 ===\n");
1297 ipf_rx_walktree(rnh, nodeprinter, NULL);
1298 printf("=== DELETE-2 ===\n");
1303 ipf_rx_walktree(rnh, ipf_rx_freenode, rnh);
1311 ipf_rdx_head_t *rnh;
1315 printf("VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV\n");
1317 for (stp = myst_top; stp; stp = stp->next)
1319 printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
1324 test_addr(rnh, pref, addr, limit)
1325 ipf_rdx_head_t *rnh;
1329 static int extras[14] = { 0, -1, 1, 3, 5, 8, 9,
1330 15, 16, 19, 255, 256, 65535, 65536
1338 memset(&af, 0, sizeof(af));
1340 if (limit < 0 || limit > 14)
1343 for (i = 0; i < limit; i++) {
1344 if (ttable[i].host == NULL)
1346 setaddr(&af, ttable[i].host);
1347 printf("%d.%d.LOOKUP(%s)", pref, i, addrname(&af));
1348 rn = ipf_rx_match(rnh, &af);
1350 printf(" = %s (%s/%d)\n", GNAME(rn),
1351 rn ? addrname(&stp->dst) : "NULL",
1352 rn ? rn->maskbitcount : 0);
1355 printf("%d.%d.LOOKUP(%s)", pref, -1, addrname(addr));
1356 rn = ipf_rx_match(rnh, addr);
1358 printf(" = %s (%s/%d)\n", GNAME(rn),
1359 rn ? addrname(&stp->dst) : "NULL", rn ? rn->maskbitcount : 0);
1365 delete_addr(rnh, item)
1366 ipf_rdx_head_t *rnh;
1375 memset(&af, 0, sizeof(af));
1376 memset(&mask, 0, sizeof(mask));
1377 setaddr(&af, ttable[item].host);
1378 mask.adf_family = af.adf_family;
1379 setmask(&mask, ttable[item].mask);
1381 printf("DELETE(%s)\n", addrname(&af));
1382 rn = ipf_rx_delete(rnh, &af, &mask);
1384 printf("FAIL LOOKUP DELETE\n");
1386 for (stp = myst_top; stp != NULL; stp = stp->next)
1387 if (stp->printed != -1)
1389 ipf_rx_walktree(rnh, nodeprinter, NULL);
1393 printf("%d.delete(%s) = %s\n", item, addrname(&af), GNAME(rn));
1395 for (pstp = &myst_top; (stp = *pstp) != NULL; pstp = &stp->next)
1396 if (stp == (myst_t *)rn)
1399 stp->nodes[0].parent = &stp->nodes[0];
1400 stp->nodes[1].parent = &stp->nodes[1];
1409 add_addr(rnh, n, item)
1410 ipf_rdx_head_t *rnh;
1416 stp = calloc(1, sizeof(*stp));
1417 rn = (ipf_rdx_node_t *)stp;
1418 setaddr(&stp->dst, ttable[item].host);
1419 stp->mask.adf_family = stp->dst.adf_family;
1420 setmask(&stp->mask, ttable[item].mask);
1421 stp->next = myst_top;
1423 (void) sprintf(rn[0].name, "_BORN.0");
1424 (void) sprintf(rn[1].name, "_BORN.1");
1425 rn = ipf_rx_addroute(rnh, &stp->dst, &stp->mask, stp->nodes);
1426 (void) sprintf(rn[0].name, "%d_NODE.0", item);
1427 (void) sprintf(rn[1].name, "%d_NODE.1", item);
1428 printf("ADD %d/%d %s/%s\n", n, item, rn[0].name, rn[1].name);
1435 checktree(ipf_rdx_head_t *head)
1443 for (s1 = myst_top; s1 != NULL; s1 = s1->next) {
1445 if (s1->printed == -1)
1448 if (rn->right->parent != rn)
1450 if (rn->left->parent != rn)
1452 if (rn->parent->left != rn && rn->parent->right != rn)
1455 printf("FAULT %#x %s\n", fault, rn->name);
1457 ipf_rx_walktree(head, nodeprinter, NULL);
1468 randomize(int *pnitems)
1476 nitems = sizeof(ttable) / sizeof(ttable[0]);
1478 order = calloc(nitems, sizeof(*order));
1479 srandom(getpid() * time(NULL));
1480 memset(order, 0xff, nitems * sizeof(*order));
1482 for (i = 0; i < nitems - 1; i++) {
1484 choice = rand() % (nitems - 1);
1485 for (j = 0; j < nitems; j++)
1486 if (order[j] == choice)
1488 } while (j != nitems);
1498 ipf_rdx_head_t *rnh;
1504 order = randomize(&nitems);
1506 for (i = 0; i < nitems - 1; i++) {
1507 add_addr(rnh, i, order[i]);
1515 ipf_rdx_head_t *rnh;
1521 order = randomize(&nitems);
1523 for (i = 0; i < nitems - 1; i++) {
1524 delete_addr(rnh, i);
1528 #endif /* RDX_DEBUG */