Home | History | Annotate | Line # | Download | only in netinet
ip_pool.c revision 1.4
      1  1.4  christos /*	$NetBSD: ip_pool.c,v 1.4 2014/03/20 20:43:12 christos Exp $	*/
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Copyright (C) 2012 by Darren Reed.
      5  1.1  christos  *
      6  1.1  christos  * See the IPFILTER.LICENCE file for details on licencing.
      7  1.1  christos  */
      8  1.1  christos #if defined(KERNEL) || defined(_KERNEL)
      9  1.1  christos # undef KERNEL
     10  1.1  christos # undef _KERNEL
     11  1.1  christos # define        KERNEL	1
     12  1.1  christos # define        _KERNEL	1
     13  1.1  christos #endif
     14  1.1  christos #if defined(__osf__)
     15  1.1  christos # define _PROTO_NET_H_
     16  1.1  christos #endif
     17  1.1  christos #include <sys/errno.h>
     18  1.1  christos #include <sys/types.h>
     19  1.1  christos #include <sys/param.h>
     20  1.2  christos #if defined(__NetBSD__)
     21  1.2  christos # if (NetBSD >= 199905) && !defined(IPFILTER_LKM) && defined(_KERNEL)
     22  1.2  christos #  include "opt_ipfilter.h"
     23  1.2  christos # endif
     24  1.2  christos #endif
     25  1.1  christos #include <sys/file.h>
     26  1.1  christos #if !defined(_KERNEL) && !defined(__KERNEL__)
     27  1.1  christos # include <stdio.h>
     28  1.1  christos # include <stdlib.h>
     29  1.1  christos # include <string.h>
     30  1.1  christos # define _KERNEL
     31  1.1  christos # ifdef __OpenBSD__
     32  1.1  christos struct file;
     33  1.1  christos # endif
     34  1.1  christos # include <sys/uio.h>
     35  1.1  christos # undef _KERNEL
     36  1.1  christos #else
     37  1.1  christos # include <sys/systm.h>
     38  1.1  christos # if defined(NetBSD) && (__NetBSD_Version__ >= 104000000)
     39  1.1  christos #  include <sys/proc.h>
     40  1.1  christos # endif
     41  1.1  christos #endif
     42  1.1  christos #include <sys/time.h>
     43  1.1  christos #if defined(_KERNEL) && !defined(SOLARIS2)
     44  1.1  christos # include <sys/mbuf.h>
     45  1.1  christos #endif
     46  1.1  christos #if defined(__SVR4) || defined(__svr4__)
     47  1.1  christos # include <sys/byteorder.h>
     48  1.1  christos # ifdef _KERNEL
     49  1.1  christos #  include <sys/dditypes.h>
     50  1.1  christos # endif
     51  1.1  christos # include <sys/stream.h>
     52  1.1  christos # include <sys/kmem.h>
     53  1.1  christos #endif
     54  1.1  christos #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
     55  1.1  christos # include <sys/malloc.h>
     56  1.1  christos #endif
     57  1.1  christos 
     58  1.1  christos #include <sys/socket.h>
     59  1.1  christos #include <net/if.h>
     60  1.1  christos #include <netinet/in.h>
     61  1.1  christos #if !defined(_KERNEL)
     62  1.1  christos # include "ipf.h"
     63  1.1  christos #endif
     64  1.1  christos 
     65  1.1  christos #include "netinet/ip_compat.h"
     66  1.1  christos #include "netinet/ip_fil.h"
     67  1.1  christos #include "netinet/ip_pool.h"
     68  1.1  christos #include "netinet/radix_ipf.h"
     69  1.1  christos 
     70  1.1  christos /* END OF INCLUDES */
     71  1.1  christos 
     72  1.1  christos #if !defined(lint)
     73  1.2  christos #if defined(__NetBSD__)
     74  1.2  christos #include <sys/cdefs.h>
     75  1.4  christos __KERNEL_RCSID(0, "$NetBSD: ip_pool.c,v 1.4 2014/03/20 20:43:12 christos Exp $");
     76  1.2  christos #else
     77  1.1  christos static const char sccsid[] = "@(#)ip_fil.c	2.41 6/5/96 (C) 1993-2000 Darren Reed";
     78  1.3   darrenr static const char rcsid[] = "@(#)Id: ip_pool.c,v 1.1.1.2 2012/07/22 13:45:31 darrenr Exp";
     79  1.2  christos #endif
     80  1.1  christos #endif
     81  1.1  christos 
     82  1.1  christos typedef struct ipf_pool_softc_s {
     83  1.1  christos 	void		*ipf_radix;
     84  1.1  christos 	ip_pool_t	*ipf_pool_list[LOOKUP_POOL_SZ];
     85  1.1  christos 	ipf_pool_stat_t	ipf_pool_stats;
     86  1.1  christos 	ip_pool_node_t	*ipf_node_explist;
     87  1.1  christos } ipf_pool_softc_t;
     88  1.1  christos 
     89  1.1  christos 
     90  1.3   darrenr static void ipf_pool_clearnodes(ipf_main_softc_t *, ipf_pool_softc_t *,
     91  1.3   darrenr 				     ip_pool_t *);
     92  1.2  christos static int ipf_pool_create(ipf_main_softc_t *, ipf_pool_softc_t *, iplookupop_t *);
     93  1.2  christos static int ipf_pool_deref(ipf_main_softc_t *, void *, void *);
     94  1.2  christos static int ipf_pool_destroy(ipf_main_softc_t *, ipf_pool_softc_t *, int, char *);
     95  1.2  christos static void *ipf_pool_exists(ipf_pool_softc_t *, int, char *);
     96  1.2  christos static void *ipf_pool_find(void *, int, char *);
     97  1.2  christos static ip_pool_node_t *ipf_pool_findeq(ipf_pool_softc_t *, ip_pool_t *,
     98  1.2  christos 					    addrfamily_t *, addrfamily_t *);
     99  1.3   darrenr static void ipf_pool_free(ipf_main_softc_t *, ipf_pool_softc_t *, ip_pool_t *);
    100  1.2  christos static int ipf_pool_insert_node(ipf_main_softc_t *, ipf_pool_softc_t *,
    101  1.2  christos 				     ip_pool_t *, struct ip_pool_node *);
    102  1.2  christos static int ipf_pool_iter_deref(ipf_main_softc_t *, void *, int, int, void *);
    103  1.2  christos static int ipf_pool_iter_next(ipf_main_softc_t *,  void *, ipftoken_t *,
    104  1.2  christos 				   ipflookupiter_t *);
    105  1.2  christos static size_t ipf_pool_flush(ipf_main_softc_t *, void *, iplookupflush_t *);
    106  1.2  christos static int ipf_pool_node_add(ipf_main_softc_t *, void *, iplookupop_t *,
    107  1.2  christos 				  int);
    108  1.2  christos static int ipf_pool_node_del(ipf_main_softc_t *, void *, iplookupop_t *,
    109  1.2  christos 				  int);
    110  1.2  christos static void ipf_pool_node_deref(ipf_pool_softc_t *, ip_pool_node_t *);
    111  1.3   darrenr static int ipf_pool_remove_node(ipf_main_softc_t *, ipf_pool_softc_t *,
    112  1.3   darrenr 				ip_pool_t *, ip_pool_node_t *);
    113  1.2  christos static int ipf_pool_search(ipf_main_softc_t *, void *, int,
    114  1.2  christos 				void *, u_int);
    115  1.2  christos static void *ipf_pool_soft_create(ipf_main_softc_t *);
    116  1.2  christos static void ipf_pool_soft_destroy(ipf_main_softc_t *, void *);
    117  1.2  christos static void ipf_pool_soft_fini(ipf_main_softc_t *, void *);
    118  1.2  christos static int ipf_pool_soft_init(ipf_main_softc_t *, void *);
    119  1.2  christos static int ipf_pool_stats_get(ipf_main_softc_t *, void *, iplookupop_t *);
    120  1.2  christos static int ipf_pool_table_add(ipf_main_softc_t *, void *, iplookupop_t *);
    121  1.2  christos static int ipf_pool_table_del(ipf_main_softc_t *, void *, iplookupop_t *);
    122  1.2  christos static void *ipf_pool_select_add_ref(void *, int, char *);
    123  1.2  christos static void ipf_pool_expire(ipf_main_softc_t *, void *);
    124  1.1  christos 
    125  1.1  christos ipf_lookup_t ipf_pool_backend = {
    126  1.1  christos 	IPLT_POOL,
    127  1.1  christos 	ipf_pool_soft_create,
    128  1.1  christos 	ipf_pool_soft_destroy,
    129  1.1  christos 	ipf_pool_soft_init,
    130  1.1  christos 	ipf_pool_soft_fini,
    131  1.1  christos 	ipf_pool_search,
    132  1.1  christos 	ipf_pool_flush,
    133  1.1  christos 	ipf_pool_iter_deref,
    134  1.1  christos 	ipf_pool_iter_next,
    135  1.1  christos 	ipf_pool_node_add,
    136  1.1  christos 	ipf_pool_node_del,
    137  1.1  christos 	ipf_pool_stats_get,
    138  1.1  christos 	ipf_pool_table_add,
    139  1.1  christos 	ipf_pool_table_del,
    140  1.1  christos 	ipf_pool_deref,
    141  1.1  christos 	ipf_pool_find,
    142  1.1  christos 	ipf_pool_select_add_ref,
    143  1.1  christos 	NULL,
    144  1.1  christos 	ipf_pool_expire,
    145  1.1  christos 	NULL
    146  1.1  christos };
    147  1.1  christos 
    148  1.1  christos 
    149  1.1  christos #ifdef TEST_POOL
    150  1.2  christos void treeprint(ip_pool_t *);
    151  1.1  christos 
    152  1.1  christos int
    153  1.1  christos main(argc, argv)
    154  1.1  christos 	int argc;
    155  1.1  christos 	char *argv[];
    156  1.1  christos {
    157  1.1  christos 	ip_pool_node_t node;
    158  1.1  christos 	addrfamily_t a, b;
    159  1.1  christos 	iplookupop_t op;
    160  1.1  christos 	ip_pool_t *ipo;
    161  1.1  christos 	i6addr_t ip;
    162  1.1  christos 
    163  1.1  christos 	RWLOCK_INIT(softc->ipf_poolrw, "poolrw");
    164  1.1  christos 	ipf_pool_init();
    165  1.1  christos 
    166  1.1  christos 	bzero((char *)&ip, sizeof(ip));
    167  1.1  christos 	bzero((char *)&op, sizeof(op));
    168  1.1  christos 	bzero((char *)&node, sizeof(node));
    169  1.2  christos 	strlcpy(op.iplo_name, "0", sizeof(op.iplo_name));
    170  1.1  christos 
    171  1.1  christos 	if (ipf_pool_create(&op) == 0)
    172  1.1  christos 		ipo = ipf_pool_exists(0, "0");
    173  1.1  christos 
    174  1.1  christos 	node.ipn_addr.adf_family = AF_INET;
    175  1.1  christos 
    176  1.1  christos 	node.ipn_addr.adf_addr.in4.s_addr = 0x0a010203;
    177  1.1  christos 	node.ipn_mask.adf_addr.in4.s_addr = 0xffffffff;
    178  1.1  christos 	node.ipn_info = 1;
    179  1.1  christos 	ipf_pool_insert_node(ipo, &node);
    180  1.1  christos 
    181  1.1  christos 	node.ipn_addr.adf_addr.in4.s_addr = 0x0a000000;
    182  1.1  christos 	node.ipn_mask.adf_addr.in4.s_addr = 0xff000000;
    183  1.1  christos 	node.ipn_info = 0;
    184  1.1  christos 	ipf_pool_insert_node(ipo, &node);
    185  1.1  christos 
    186  1.1  christos 	node.ipn_addr.adf_addr.in4.s_addr = 0x0a010100;
    187  1.1  christos 	node.ipn_mask.adf_addr.in4.s_addr = 0xffffff00;
    188  1.1  christos 	node.ipn_info = 1;
    189  1.1  christos 	ipf_pool_insert_node(ipo, &node);
    190  1.1  christos 
    191  1.1  christos 	node.ipn_addr.adf_addr.in4.s_addr = 0x0a010200;
    192  1.1  christos 	node.ipn_mask.adf_addr.in4.s_addr = 0xffffff00;
    193  1.1  christos 	node.ipn_info = 0;
    194  1.1  christos 	ipf_pool_insert_node(ipo, &node);
    195  1.1  christos 
    196  1.1  christos 	node.ipn_addr.adf_addr.in4.s_addr = 0x0a010000;
    197  1.1  christos 	node.ipn_mask.adf_addr.in4.s_addr = 0xffff0000;
    198  1.1  christos 	node.ipn_info = 1;
    199  1.1  christos 	ipf_pool_insert_node(ipo, &node);
    200  1.1  christos 
    201  1.1  christos 	node.ipn_addr.adf_addr.in4.s_addr = 0x0a01020f;
    202  1.1  christos 	node.ipn_mask.adf_addr.in4.s_addr = 0xffffffff;
    203  1.1  christos 	node.ipn_info = 1;
    204  1.1  christos 	ipf_pool_insert_node(ipo, &node);
    205  1.1  christos #ifdef	DEBUG_POOL
    206  1.1  christos 	treeprint(ipo);
    207  1.1  christos #endif
    208  1.1  christos 	ip.in4.s_addr = 0x0a00aabb;
    209  1.1  christos 	printf("search(%#x) = %d (0)\n", ip.in4.s_addr,
    210  1.1  christos 		ipf_pool_search(ipo, 4, &ip, 1));
    211  1.1  christos 
    212  1.1  christos 	ip.in4.s_addr = 0x0a000001;
    213  1.1  christos 	printf("search(%#x) = %d (0)\n", ip.in4.s_addr,
    214  1.1  christos 		ipf_pool_search(ipo, 4, &ip, 1));
    215  1.1  christos 
    216  1.1  christos 	ip.in4.s_addr = 0x0a000101;
    217  1.1  christos 	printf("search(%#x) = %d (0)\n", ip.in4.s_addr,
    218  1.1  christos 		ipf_pool_search(ipo, 4, &ip, 1));
    219  1.1  christos 
    220  1.1  christos 	ip.in4.s_addr = 0x0a010001;
    221  1.1  christos 	printf("search(%#x) = %d (1)\n", ip.in4.s_addr,
    222  1.1  christos 		ipf_pool_search(ipo, 4, &ip, 1));
    223  1.1  christos 
    224  1.1  christos 	ip.in4.s_addr = 0x0a010101;
    225  1.1  christos 	printf("search(%#x) = %d (1)\n", ip.in4.s_addr,
    226  1.1  christos 		ipf_pool_search(ipo, 4, &ip, 1));
    227  1.1  christos 
    228  1.1  christos 	ip.in4.s_addr = 0x0a010201;
    229  1.1  christos 	printf("search(%#x) = %d (0)\n", ip.in4.s_addr,
    230  1.1  christos 		ipf_pool_search(ipo, 4, &ip, 1));
    231  1.1  christos 
    232  1.1  christos 	ip.in4.s_addr = 0x0a010203;
    233  1.1  christos 	printf("search(%#x) = %d (1)\n", ip.in4.s_addr,
    234  1.1  christos 		ipf_pool_search(ipo, 4, &ip, 1));
    235  1.1  christos 
    236  1.1  christos 	ip.in4.s_addr = 0x0a01020f;
    237  1.1  christos 	printf("search(%#x) = %d (1)\n", ip.in4.s_addr,
    238  1.1  christos 		ipf_pool_search(ipo, 4, &ip, 1));
    239  1.1  christos 
    240  1.1  christos 	ip.in4.s_addr = 0x0b00aabb;
    241  1.1  christos 	printf("search(%#x) = %d (-1)\n", ip.in4.s_addr,
    242  1.1  christos 		ipf_pool_search(ipo, 4, &ip, 1));
    243  1.1  christos 
    244  1.1  christos #ifdef	DEBUG_POOL
    245  1.1  christos 	treeprint(ipo);
    246  1.1  christos #endif
    247  1.1  christos 
    248  1.1  christos 	ipf_pool_fini();
    249  1.1  christos 
    250  1.1  christos 	return 0;
    251  1.1  christos }
    252  1.1  christos 
    253  1.1  christos 
    254  1.1  christos void
    255  1.1  christos treeprint(ipo)
    256  1.1  christos 	ip_pool_t *ipo;
    257  1.1  christos {
    258  1.1  christos 	ip_pool_node_t *c;
    259  1.1  christos 
    260  1.1  christos 	for (c = ipo->ipo_list; c != NULL; c = c->ipn_next)
    261  1.1  christos 		printf("Node %p(%s) (%#x/%#x) = %d hits %lu\n",
    262  1.1  christos 			c, c->ipn_name, c->ipn_addr.adf_addr.in4.s_addr,
    263  1.1  christos 			c->ipn_mask.adf_addr.in4.s_addr,
    264  1.1  christos 			c->ipn_info, c->ipn_hits);
    265  1.1  christos }
    266  1.1  christos #endif /* TEST_POOL */
    267  1.1  christos 
    268  1.1  christos 
    269  1.1  christos /* ------------------------------------------------------------------------ */
    270  1.1  christos /* Function:    ipf_pool_soft_create                                        */
    271  1.1  christos /* Returns:     void *   - NULL = failure, else pointer to local context    */
    272  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    273  1.1  christos /*                                                                          */
    274  1.1  christos /* Initialise the routing table data structures where required.             */
    275  1.1  christos /* ------------------------------------------------------------------------ */
    276  1.1  christos static void *
    277  1.2  christos ipf_pool_soft_create(ipf_main_softc_t *softc)
    278  1.1  christos {
    279  1.1  christos 	ipf_pool_softc_t *softp;
    280  1.1  christos 
    281  1.1  christos 	KMALLOC(softp, ipf_pool_softc_t *);
    282  1.3   darrenr 	if (softp == NULL) {
    283  1.3   darrenr 		IPFERROR(70032);
    284  1.1  christos 		return NULL;
    285  1.3   darrenr 	}
    286  1.1  christos 
    287  1.1  christos 	bzero((char *)softp, sizeof(*softp));
    288  1.1  christos 
    289  1.1  christos 	softp->ipf_radix = ipf_rx_create();
    290  1.1  christos 	if (softp->ipf_radix == NULL) {
    291  1.3   darrenr 		IPFERROR(70033);
    292  1.1  christos 		KFREE(softp);
    293  1.1  christos 		return NULL;
    294  1.1  christos 	}
    295  1.1  christos 
    296  1.1  christos 	return softp;
    297  1.1  christos }
    298  1.1  christos 
    299  1.1  christos 
    300  1.1  christos /* ------------------------------------------------------------------------ */
    301  1.1  christos /* Function:    ipf_pool_soft_init                                          */
    302  1.1  christos /* Returns:     int     - 0 = success, else error                           */
    303  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    304  1.1  christos /*              arg(I)   - pointer to local context to use                  */
    305  1.1  christos /*                                                                          */
    306  1.1  christos /* Initialise the routing table data structures where required.             */
    307  1.1  christos /* ------------------------------------------------------------------------ */
    308  1.1  christos static int
    309  1.2  christos ipf_pool_soft_init(ipf_main_softc_t *softc, void *arg)
    310  1.1  christos {
    311  1.1  christos 	ipf_pool_softc_t *softp = arg;
    312  1.1  christos 
    313  1.1  christos 	ipf_rx_init(softp->ipf_radix);
    314  1.1  christos 
    315  1.1  christos 	return 0;
    316  1.1  christos }
    317  1.1  christos 
    318  1.1  christos 
    319  1.1  christos /* ------------------------------------------------------------------------ */
    320  1.1  christos /* Function:    ipf_pool_soft_fini                                          */
    321  1.1  christos /* Returns:     Nil                                                         */
    322  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    323  1.1  christos /*              arg(I)   - pointer to local context to use                  */
    324  1.1  christos /* Locks:       WRITE(ipf_global)                                           */
    325  1.1  christos /*                                                                          */
    326  1.1  christos /* Clean up all the pool data structures allocated and call the cleanup     */
    327  1.1  christos /* function for the radix tree that supports the pools. ipf_pool_destroy is */
    328  1.1  christos /* used to delete the pools one by one to ensure they're properly freed up. */
    329  1.1  christos /* ------------------------------------------------------------------------ */
    330  1.1  christos static void
    331  1.2  christos ipf_pool_soft_fini(ipf_main_softc_t *softc, void *arg)
    332  1.1  christos {
    333  1.1  christos 	ipf_pool_softc_t *softp = arg;
    334  1.1  christos 	ip_pool_t *p, *q;
    335  1.1  christos 	int i;
    336  1.1  christos 
    337  1.1  christos 	softc = arg;
    338  1.1  christos 
    339  1.1  christos 	for (i = -1; i <= IPL_LOGMAX; i++) {
    340  1.1  christos 		for (q = softp->ipf_pool_list[i + 1]; (p = q) != NULL; ) {
    341  1.1  christos 			q = p->ipo_next;
    342  1.1  christos 			(void) ipf_pool_destroy(softc, arg, i, p->ipo_name);
    343  1.1  christos 		}
    344  1.1  christos 	}
    345  1.1  christos }
    346  1.1  christos 
    347  1.1  christos 
    348  1.1  christos /* ------------------------------------------------------------------------ */
    349  1.1  christos /* Function:    ipf_pool_soft_destroy                                       */
    350  1.1  christos /* Returns:     Nil                                                         */
    351  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    352  1.1  christos /*              arg(I)   - pointer to local context to use                  */
    353  1.1  christos /*                                                                          */
    354  1.1  christos /* Clean up the pool by free'ing the radix tree associated with it and free */
    355  1.1  christos /* up the pool context too.                                                 */
    356  1.1  christos /* ------------------------------------------------------------------------ */
    357  1.1  christos static void
    358  1.2  christos ipf_pool_soft_destroy(ipf_main_softc_t *softc, void *arg)
    359  1.1  christos {
    360  1.1  christos 	ipf_pool_softc_t *softp = arg;
    361  1.1  christos 
    362  1.1  christos 	ipf_rx_destroy(softp->ipf_radix);
    363  1.1  christos 
    364  1.1  christos 	KFREE(softp);
    365  1.1  christos }
    366  1.1  christos 
    367  1.1  christos 
    368  1.1  christos /* ------------------------------------------------------------------------ */
    369  1.1  christos /* Function:   ipf_pool_node_add                                            */
    370  1.1  christos /* Returns:    int - 0 = success, else error                                */
    371  1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
    372  1.1  christos /*             arg(I)   - pointer to local context to use                   */
    373  1.1  christos /*             op(I) - pointer to lookup operatin data                      */
    374  1.1  christos /*                                                                          */
    375  1.3   darrenr /* When adding a new node, a check is made to ensure that the address/mask  */
    376  1.3   darrenr /* pair supplied has been appropriately prepared by applying the mask to    */
    377  1.3   darrenr /* the address prior to calling for the pair to be added.                   */
    378  1.1  christos /* ------------------------------------------------------------------------ */
    379  1.1  christos static int
    380  1.2  christos ipf_pool_node_add(ipf_main_softc_t *softc, void *arg, iplookupop_t *op, int uid)
    381  1.1  christos {
    382  1.1  christos 	ip_pool_node_t node, *m;
    383  1.1  christos 	ip_pool_t *p;
    384  1.1  christos 	int err;
    385  1.1  christos 
    386  1.1  christos 	if (op->iplo_size != sizeof(node)) {
    387  1.1  christos 		IPFERROR(70014);
    388  1.1  christos 		return EINVAL;
    389  1.1  christos 	}
    390  1.1  christos 
    391  1.1  christos 	err = COPYIN(op->iplo_struct, &node, sizeof(node));
    392  1.1  christos 	if (err != 0) {
    393  1.1  christos 		IPFERROR(70015);
    394  1.1  christos 		return EFAULT;
    395  1.1  christos 	}
    396  1.1  christos 
    397  1.1  christos 	p = ipf_pool_find(arg, op->iplo_unit, op->iplo_name);
    398  1.1  christos 	if (p == NULL) {
    399  1.1  christos 		IPFERROR(70017);
    400  1.1  christos 		return ESRCH;
    401  1.1  christos 	}
    402  1.1  christos 
    403  1.3   darrenr 	if (node.ipn_addr.adf_family == AF_INET) {
    404  1.3   darrenr 		if (node.ipn_addr.adf_len != offsetof(addrfamily_t, adf_addr) +
    405  1.3   darrenr 					     sizeof(struct in_addr)) {
    406  1.3   darrenr 			IPFERROR(70028);
    407  1.3   darrenr 			return EINVAL;
    408  1.3   darrenr 		}
    409  1.3   darrenr 	}
    410  1.3   darrenr #ifdef USE_INET6
    411  1.3   darrenr 	else if (node.ipn_addr.adf_family == AF_INET6) {
    412  1.3   darrenr 		if (node.ipn_addr.adf_len != offsetof(addrfamily_t, adf_addr) +
    413  1.3   darrenr 					     sizeof(struct in6_addr)) {
    414  1.3   darrenr 			IPFERROR(70034);
    415  1.3   darrenr 			return EINVAL;
    416  1.3   darrenr 		}
    417  1.3   darrenr 	}
    418  1.3   darrenr #endif
    419  1.3   darrenr 	if (node.ipn_mask.adf_len != node.ipn_addr.adf_len) {
    420  1.3   darrenr 		IPFERROR(70029);
    421  1.3   darrenr 		return EINVAL;
    422  1.3   darrenr 	}
    423  1.3   darrenr 
    424  1.3   darrenr 	/*
    425  1.3   darrenr 	 * Check that the address/mask pair works.
    426  1.3   darrenr 	 */
    427  1.3   darrenr 	if (node.ipn_addr.adf_family == AF_INET) {
    428  1.3   darrenr 		if ((node.ipn_addr.adf_addr.in4.s_addr &
    429  1.3   darrenr 		     node.ipn_mask.adf_addr.in4.s_addr) !=
    430  1.3   darrenr 		    node.ipn_addr.adf_addr.in4.s_addr) {
    431  1.3   darrenr 			IPFERROR(70035);
    432  1.3   darrenr 			return EINVAL;
    433  1.3   darrenr 		}
    434  1.3   darrenr 	}
    435  1.3   darrenr #ifdef USE_INET6
    436  1.3   darrenr 	else if (node.ipn_addr.adf_family == AF_INET6) {
    437  1.3   darrenr 		if (IP6_MASKNEQ(&node.ipn_addr.adf_addr.in6,
    438  1.3   darrenr 				&node.ipn_mask.adf_addr.in6,
    439  1.3   darrenr 				&node.ipn_addr.adf_addr.in6)) {
    440  1.3   darrenr 			IPFERROR(70036);
    441  1.3   darrenr 			return EINVAL;
    442  1.3   darrenr 		}
    443  1.3   darrenr 	}
    444  1.3   darrenr #endif
    445  1.3   darrenr 
    446  1.1  christos 	/*
    447  1.1  christos 	 * add an entry to a pool - return an error if it already
    448  1.1  christos 	 * exists remove an entry from a pool - if it exists
    449  1.1  christos 	 * - in both cases, the pool *must* exist!
    450  1.1  christos 	 */
    451  1.1  christos 	m = ipf_pool_findeq(arg, p, &node.ipn_addr, &node.ipn_mask);
    452  1.1  christos 	if (m != NULL) {
    453  1.1  christos 		IPFERROR(70018);
    454  1.1  christos 		return EEXIST;
    455  1.1  christos 	}
    456  1.1  christos 	err = ipf_pool_insert_node(softc, arg, p, &node);
    457  1.1  christos 
    458  1.1  christos 	return err;
    459  1.1  christos }
    460  1.1  christos 
    461  1.1  christos 
    462  1.1  christos /* ------------------------------------------------------------------------ */
    463  1.1  christos /* Function:   ipf_pool_node_del                                            */
    464  1.1  christos /* Returns:    int - 0 = success, else error                                */
    465  1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
    466  1.1  christos /*             arg(I)   - pointer to local context to use                   */
    467  1.1  christos /*             op(I)    - pointer to lookup operatin data                   */
    468  1.1  christos /*                                                                          */
    469  1.1  christos /* ------------------------------------------------------------------------ */
    470  1.1  christos static int
    471  1.2  christos ipf_pool_node_del(ipf_main_softc_t *softc, void *arg, iplookupop_t *op, int uid)
    472  1.1  christos {
    473  1.1  christos 	ip_pool_node_t node, *m;
    474  1.1  christos 	ip_pool_t *p;
    475  1.1  christos 	int err;
    476  1.1  christos 
    477  1.1  christos 
    478  1.1  christos 	if (op->iplo_size != sizeof(node)) {
    479  1.1  christos 		IPFERROR(70019);
    480  1.1  christos 		return EINVAL;
    481  1.1  christos 	}
    482  1.1  christos 	node.ipn_uid = uid;
    483  1.1  christos 
    484  1.1  christos 	err = COPYIN(op->iplo_struct, &node, sizeof(node));
    485  1.1  christos 	if (err != 0) {
    486  1.1  christos 		IPFERROR(70020);
    487  1.1  christos 		return EFAULT;
    488  1.1  christos 	}
    489  1.1  christos 
    490  1.3   darrenr 	if (node.ipn_addr.adf_family == AF_INET) {
    491  1.3   darrenr 		if (node.ipn_addr.adf_len != offsetof(addrfamily_t, adf_addr) +
    492  1.3   darrenr 					     sizeof(struct in_addr)) {
    493  1.3   darrenr 			IPFERROR(70030);
    494  1.3   darrenr 			return EINVAL;
    495  1.3   darrenr 		}
    496  1.3   darrenr 	}
    497  1.3   darrenr #ifdef USE_INET6
    498  1.3   darrenr 	else if (node.ipn_addr.adf_family == AF_INET6) {
    499  1.3   darrenr 		if (node.ipn_addr.adf_len != offsetof(addrfamily_t, adf_addr) +
    500  1.3   darrenr 					     sizeof(struct in6_addr)) {
    501  1.3   darrenr 			IPFERROR(70037);
    502  1.3   darrenr 			return EINVAL;
    503  1.3   darrenr 		}
    504  1.3   darrenr 	}
    505  1.3   darrenr #endif
    506  1.3   darrenr 	if (node.ipn_mask.adf_len != node.ipn_addr.adf_len) {
    507  1.3   darrenr 		IPFERROR(70031);
    508  1.3   darrenr 		return EINVAL;
    509  1.3   darrenr 	}
    510  1.3   darrenr 
    511  1.1  christos 	p = ipf_pool_find(arg, op->iplo_unit, op->iplo_name);
    512  1.1  christos 	if (p == NULL) {
    513  1.1  christos 		IPFERROR(70021);
    514  1.1  christos 		return ESRCH;
    515  1.1  christos 	}
    516  1.1  christos 
    517  1.1  christos 	m = ipf_pool_findeq(arg, p, &node.ipn_addr, &node.ipn_mask);
    518  1.1  christos 	if (m == NULL) {
    519  1.1  christos 		IPFERROR(70022);
    520  1.1  christos 		return ENOENT;
    521  1.1  christos 	}
    522  1.1  christos 
    523  1.1  christos 	if ((uid != 0) && (uid != m->ipn_uid)) {
    524  1.1  christos 		IPFERROR(70024);
    525  1.1  christos 		return EACCES;
    526  1.1  christos 	}
    527  1.1  christos 
    528  1.3   darrenr 	err = ipf_pool_remove_node(softc, arg, p, m);
    529  1.1  christos 
    530  1.1  christos 	return err;
    531  1.1  christos }
    532  1.1  christos 
    533  1.1  christos 
    534  1.1  christos /* ------------------------------------------------------------------------ */
    535  1.1  christos /* Function:   ipf_pool_table_add                                           */
    536  1.1  christos /* Returns:    int - 0 = success, else error                                */
    537  1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
    538  1.1  christos /*             arg(I)   - pointer to local context to use                   */
    539  1.1  christos /*             op(I)    - pointer to lookup operatin data                   */
    540  1.1  christos /*                                                                          */
    541  1.1  christos /* ------------------------------------------------------------------------ */
    542  1.1  christos static int
    543  1.2  christos ipf_pool_table_add(ipf_main_softc_t *softc, void *arg, iplookupop_t *op)
    544  1.1  christos {
    545  1.1  christos 	int err;
    546  1.1  christos 
    547  1.1  christos 	if (((op->iplo_arg & LOOKUP_ANON) == 0) &&
    548  1.1  christos 	    (ipf_pool_find(arg, op->iplo_unit, op->iplo_name) != NULL)) {
    549  1.1  christos 		IPFERROR(70023);
    550  1.1  christos 		err = EEXIST;
    551  1.1  christos 	} else {
    552  1.1  christos 		err = ipf_pool_create(softc, arg, op);
    553  1.1  christos 	}
    554  1.1  christos 
    555  1.1  christos 	return err;
    556  1.1  christos }
    557  1.1  christos 
    558  1.1  christos 
    559  1.1  christos /* ------------------------------------------------------------------------ */
    560  1.1  christos /* Function:   ipf_pool_table_del                                           */
    561  1.1  christos /* Returns:    int - 0 = success, else error                                */
    562  1.1  christos /* Parameters: softc(I) - pointer to soft context main structure            */
    563  1.1  christos /*             arg(I)   - pointer to local context to use                   */
    564  1.1  christos /*             op(I)    - pointer to lookup operatin data                   */
    565  1.1  christos /*                                                                          */
    566  1.1  christos /* ------------------------------------------------------------------------ */
    567  1.1  christos static int
    568  1.2  christos ipf_pool_table_del(ipf_main_softc_t *softc, void *arg, iplookupop_t *op)
    569  1.1  christos {
    570  1.1  christos 	return ipf_pool_destroy(softc, arg, op->iplo_unit, op->iplo_name);
    571  1.1  christos }
    572  1.1  christos 
    573  1.1  christos 
    574  1.1  christos /* ------------------------------------------------------------------------ */
    575  1.1  christos /* Function:    ipf_pool_statistics                                         */
    576  1.1  christos /* Returns:     int      - 0 = success, else error                          */
    577  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    578  1.1  christos /*              arg(I)   - pointer to local context to use                  */
    579  1.1  christos /*              op(I)    - pointer to lookup operatin data                  */
    580  1.1  christos /*                                                                          */
    581  1.1  christos /* Copy the current statistics out into user space, collecting pool list    */
    582  1.1  christos /* pointers as appropriate for later use.                                   */
    583  1.1  christos /* ------------------------------------------------------------------------ */
    584  1.1  christos static int
    585  1.2  christos ipf_pool_stats_get(ipf_main_softc_t *softc, void *arg, iplookupop_t *op)
    586  1.1  christos {
    587  1.1  christos 	ipf_pool_softc_t *softp = arg;
    588  1.1  christos 	ipf_pool_stat_t stats;
    589  1.1  christos 	int unit, i, err = 0;
    590  1.1  christos 
    591  1.1  christos 	if (op->iplo_size != sizeof(ipf_pool_stat_t)) {
    592  1.1  christos 		IPFERROR(70001);
    593  1.1  christos 		return EINVAL;
    594  1.1  christos 	}
    595  1.1  christos 
    596  1.1  christos 	bcopy((char *)&softp->ipf_pool_stats, (char *)&stats, sizeof(stats));
    597  1.1  christos 	unit = op->iplo_unit;
    598  1.1  christos 	if (unit == IPL_LOGALL) {
    599  1.1  christos 		for (i = 0; i <= LOOKUP_POOL_MAX; i++)
    600  1.1  christos 			stats.ipls_list[i] = softp->ipf_pool_list[i];
    601  1.1  christos 	} else if (unit >= 0 && unit <= IPL_LOGMAX) {
    602  1.1  christos 		unit++;						/* -1 => 0 */
    603  1.1  christos 		if (op->iplo_name[0] != '\0')
    604  1.1  christos 			stats.ipls_list[unit] = ipf_pool_exists(softp, unit - 1,
    605  1.1  christos 								op->iplo_name);
    606  1.1  christos 		else
    607  1.1  christos 			stats.ipls_list[unit] = softp->ipf_pool_list[unit];
    608  1.1  christos 	} else {
    609  1.1  christos 		IPFERROR(70025);
    610  1.1  christos 		err = EINVAL;
    611  1.1  christos 	}
    612  1.1  christos 	if (err == 0) {
    613  1.1  christos 		err = COPYOUT(&stats, op->iplo_struct, sizeof(stats));
    614  1.1  christos 		if (err != 0) {
    615  1.1  christos 			IPFERROR(70026);
    616  1.1  christos 			return EFAULT;
    617  1.1  christos 		}
    618  1.1  christos 	}
    619  1.1  christos 	return 0;
    620  1.1  christos }
    621  1.1  christos 
    622  1.1  christos 
    623  1.1  christos /* ------------------------------------------------------------------------ */
    624  1.1  christos /* Function:    ipf_pool_exists                                             */
    625  1.1  christos /* Returns:     int      - 0 = success, else error                          */
    626  1.1  christos /* Parameters:  softp(I) - pointer to soft context pool information         */
    627  1.1  christos /*              unit(I)  - ipfilter device to which we are working on       */
    628  1.1  christos /*              name(I)  - name of the pool                                 */
    629  1.1  christos /*                                                                          */
    630  1.1  christos /* Find a matching pool inside the collection of pools for a particular     */
    631  1.1  christos /* device, indicated by the unit number.                                    */
    632  1.1  christos /* ------------------------------------------------------------------------ */
    633  1.1  christos static void *
    634  1.2  christos ipf_pool_exists(ipf_pool_softc_t *softp, int unit, char *name)
    635  1.1  christos {
    636  1.1  christos 	ip_pool_t *p;
    637  1.1  christos 	int i;
    638  1.1  christos 
    639  1.1  christos 	if (unit == IPL_LOGALL) {
    640  1.1  christos 		for (i = 0; i <= LOOKUP_POOL_MAX; i++) {
    641  1.1  christos 			for (p = softp->ipf_pool_list[i]; p != NULL;
    642  1.1  christos 			     p = p->ipo_next) {
    643  1.1  christos 				if (strncmp(p->ipo_name, name,
    644  1.1  christos 					    sizeof(p->ipo_name)) == 0)
    645  1.1  christos 					break;
    646  1.1  christos 			}
    647  1.1  christos 			if (p != NULL)
    648  1.1  christos 				break;
    649  1.1  christos 		}
    650  1.1  christos 	} else {
    651  1.1  christos 		for (p = softp->ipf_pool_list[unit + 1]; p != NULL;
    652  1.1  christos 		     p = p->ipo_next)
    653  1.1  christos 			if (strncmp(p->ipo_name, name,
    654  1.1  christos 				    sizeof(p->ipo_name)) == 0)
    655  1.1  christos 				break;
    656  1.1  christos 	}
    657  1.1  christos 	return p;
    658  1.1  christos }
    659  1.1  christos 
    660  1.1  christos 
    661  1.1  christos /* ------------------------------------------------------------------------ */
    662  1.1  christos /* Function:    ipf_pool_find                                               */
    663  1.1  christos /* Returns:     int    - 0 = success, else error                            */
    664  1.1  christos /* Parameters:  arg(I)  - pointer to local context to use                   */
    665  1.1  christos /*              unit(I) - ipfilter device to which we are working on        */
    666  1.1  christos /*              name(I)  - name of the pool                                 */
    667  1.1  christos /*                                                                          */
    668  1.1  christos /* Find a matching pool inside the collection of pools for a particular     */
    669  1.1  christos /* device, indicated by the unit number.  If it is marked for deletion then */
    670  1.1  christos /* pretend it does not exist.                                               */
    671  1.1  christos /* ------------------------------------------------------------------------ */
    672  1.1  christos static void *
    673  1.2  christos ipf_pool_find(void *arg, int unit, char *name)
    674  1.1  christos {
    675  1.1  christos 	ipf_pool_softc_t *softp = arg;
    676  1.1  christos 	ip_pool_t *p;
    677  1.1  christos 
    678  1.1  christos 	p = ipf_pool_exists(softp, unit, name);
    679  1.1  christos 	if ((p != NULL) && (p->ipo_flags & IPOOL_DELETE))
    680  1.1  christos 		return NULL;
    681  1.1  christos 
    682  1.1  christos 	return p;
    683  1.1  christos }
    684  1.1  christos 
    685  1.1  christos 
    686  1.1  christos /* ------------------------------------------------------------------------ */
    687  1.1  christos /* Function:    ipf_pool_select_add_ref                                     */
    688  1.1  christos /* Returns:     int - 0 = success, else error                               */
    689  1.1  christos /* Parameters:  arg(I)  - pointer to local context to use                   */
    690  1.1  christos /*              unit(I) - ipfilter device to which we are working on        */
    691  1.1  christos /*              name(I)  - name of the pool                                 */
    692  1.1  christos /*                                                                          */
    693  1.1  christos /* ------------------------------------------------------------------------ */
    694  1.1  christos static void *
    695  1.2  christos ipf_pool_select_add_ref(void *arg, int unit, char *name)
    696  1.1  christos {
    697  1.1  christos 	ip_pool_t *p;
    698  1.1  christos 
    699  1.1  christos 	p = ipf_pool_find(arg, -1, name);
    700  1.1  christos 	if (p == NULL)
    701  1.1  christos 		p = ipf_pool_find(arg, unit, name);
    702  1.1  christos 	if (p != NULL) {
    703  1.1  christos 		ATOMIC_INC32(p->ipo_ref);
    704  1.1  christos 	}
    705  1.1  christos 	return p;
    706  1.1  christos }
    707  1.1  christos 
    708  1.1  christos 
    709  1.1  christos /* ------------------------------------------------------------------------ */
    710  1.1  christos /* Function:    ipf_pool_findeq                                             */
    711  1.1  christos /* Returns:     int     - 0 = success, else error                           */
    712  1.1  christos /* Parameters:  softp(I) - pointer to soft context pool information         */
    713  1.1  christos /*              ipo(I)  - pointer to the pool getting the new node.         */
    714  1.1  christos /*              addr(I) - pointer to address information to match on        */
    715  1.1  christos /*              mask(I) - pointer to the address mask to match              */
    716  1.1  christos /*                                                                          */
    717  1.1  christos /* Searches for an exact match of an entry in the pool.                     */
    718  1.1  christos /* ------------------------------------------------------------------------ */
    719  1.2  christos extern void printhostmask(int, u_32_t *, u_32_t *);
    720  1.1  christos static ip_pool_node_t *
    721  1.2  christos ipf_pool_findeq(ipf_pool_softc_t *softp, ip_pool_t *ipo, addrfamily_t *addr,
    722  1.2  christos     addrfamily_t *mask)
    723  1.1  christos {
    724  1.1  christos 	ipf_rdx_node_t *n;
    725  1.1  christos 
    726  1.1  christos 	n = ipo->ipo_head->lookup(ipo->ipo_head, addr, mask);
    727  1.1  christos 	return (ip_pool_node_t *)n;
    728  1.1  christos }
    729  1.1  christos 
    730  1.1  christos 
    731  1.1  christos /* ------------------------------------------------------------------------ */
    732  1.1  christos /* Function:    ipf_pool_search                                             */
    733  1.1  christos /* Returns:     int     - 0 == +ve match, -1 == error, 1 == -ve/no match    */
    734  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    735  1.1  christos /*              tptr(I)    - pointer to the pool to search                  */
    736  1.1  christos /*              version(I) - IP protocol version (4 or 6)                   */
    737  1.1  christos /*              dptr(I)    - pointer to address information                 */
    738  1.1  christos /*              bytes(I)   - length of packet                               */
    739  1.1  christos /*                                                                          */
    740  1.1  christos /* Search the pool for a given address and return a search result.          */
    741  1.1  christos /* ------------------------------------------------------------------------ */
    742  1.1  christos static int
    743  1.2  christos ipf_pool_search(ipf_main_softc_t *softc, void *tptr, int ipversion, void *dptr,
    744  1.2  christos     u_int bytes)
    745  1.1  christos {
    746  1.1  christos 	ipf_rdx_node_t *rn;
    747  1.1  christos 	ip_pool_node_t *m;
    748  1.1  christos 	i6addr_t *addr;
    749  1.1  christos 	addrfamily_t v;
    750  1.1  christos 	ip_pool_t *ipo;
    751  1.1  christos 	int rv;
    752  1.1  christos 
    753  1.1  christos 	ipo = tptr;
    754  1.1  christos 	if (ipo == NULL)
    755  1.1  christos 		return -1;
    756  1.1  christos 
    757  1.1  christos 	rv = 1;
    758  1.1  christos 	m = NULL;
    759  1.1  christos 	addr = (i6addr_t *)dptr;
    760  1.1  christos 	bzero(&v, sizeof(v));
    761  1.1  christos 
    762  1.1  christos 	if (ipversion == 4) {
    763  1.1  christos 		v.adf_family = AF_INET;
    764  1.3   darrenr 		v.adf_len = offsetof(addrfamily_t, adf_addr) +
    765  1.3   darrenr 			    sizeof(struct in_addr);
    766  1.1  christos 		v.adf_addr.in4 = addr->in4;
    767  1.1  christos #ifdef USE_INET6
    768  1.1  christos 	} else if (ipversion == 6) {
    769  1.1  christos 		v.adf_family = AF_INET6;
    770  1.3   darrenr 		v.adf_len = offsetof(addrfamily_t, adf_addr) +
    771  1.3   darrenr 			    sizeof(struct in6_addr);
    772  1.1  christos 		v.adf_addr.in6 = addr->in6;
    773  1.1  christos #endif
    774  1.1  christos 	} else
    775  1.1  christos 		return -1;
    776  1.1  christos 
    777  1.1  christos 	READ_ENTER(&softc->ipf_poolrw);
    778  1.1  christos 
    779  1.1  christos 	rn = ipo->ipo_head->matchaddr(ipo->ipo_head, &v);
    780  1.1  christos 
    781  1.1  christos 	if ((rn != NULL) && (rn->root == 0)) {
    782  1.1  christos 		m = (ip_pool_node_t *)rn;
    783  1.1  christos 		ipo->ipo_hits++;
    784  1.1  christos 		m->ipn_bytes += bytes;
    785  1.1  christos 		m->ipn_hits++;
    786  1.1  christos 		rv = m->ipn_info;
    787  1.1  christos 	}
    788  1.1  christos 	RWLOCK_EXIT(&softc->ipf_poolrw);
    789  1.1  christos 	return rv;
    790  1.1  christos }
    791  1.1  christos 
    792  1.1  christos 
    793  1.1  christos /* ------------------------------------------------------------------------ */
    794  1.1  christos /* Function:    ipf_pool_insert_node                                        */
    795  1.1  christos /* Returns:     int      - 0 = success, else error                          */
    796  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    797  1.1  christos /*              softp(I) - pointer to soft context pool information         */
    798  1.1  christos /*              ipo(I)   - pointer to the pool getting the new node.        */
    799  1.1  christos /*              node(I)  - structure with address/mask to add               */
    800  1.1  christos /* Locks:       WRITE(ipf_poolrw)                                           */
    801  1.1  christos /*                                                                          */
    802  1.1  christos /* Add another node to the pool given by ipo.  The three parameters passed  */
    803  1.1  christos /* in (addr, mask, info) shold all be stored in the node.                   */
    804  1.1  christos /* ------------------------------------------------------------------------ */
    805  1.1  christos static int
    806  1.2  christos ipf_pool_insert_node(ipf_main_softc_t *softc, ipf_pool_softc_t *softp,
    807  1.2  christos     ip_pool_t *ipo, struct ip_pool_node *node)
    808  1.1  christos {
    809  1.1  christos 	ipf_rdx_node_t *rn;
    810  1.1  christos 	ip_pool_node_t *x;
    811  1.1  christos 
    812  1.1  christos 	if ((node->ipn_addr.adf_len > sizeof(*rn)) ||
    813  1.1  christos 	    (node->ipn_addr.adf_len < 4)) {
    814  1.1  christos 		IPFERROR(70003);
    815  1.1  christos 		return EINVAL;
    816  1.1  christos 	}
    817  1.1  christos 
    818  1.1  christos 	if ((node->ipn_mask.adf_len > sizeof(*rn)) ||
    819  1.1  christos 	    (node->ipn_mask.adf_len < 4)) {
    820  1.1  christos 		IPFERROR(70004);
    821  1.1  christos 		return EINVAL;
    822  1.1  christos 	}
    823  1.1  christos 
    824  1.1  christos 	KMALLOC(x, ip_pool_node_t *);
    825  1.1  christos 	if (x == NULL) {
    826  1.1  christos 		IPFERROR(70002);
    827  1.1  christos 		return ENOMEM;
    828  1.1  christos 	}
    829  1.1  christos 
    830  1.1  christos 	*x = *node;
    831  1.1  christos 	bzero((char *)x->ipn_nodes, sizeof(x->ipn_nodes));
    832  1.1  christos 	x->ipn_owner = ipo;
    833  1.1  christos 	x->ipn_hits = 0;
    834  1.1  christos 	x->ipn_next = NULL;
    835  1.1  christos 	x->ipn_pnext = NULL;
    836  1.1  christos 	x->ipn_dnext = NULL;
    837  1.1  christos 	x->ipn_pdnext = NULL;
    838  1.1  christos 
    839  1.1  christos 	if (x->ipn_die != 0) {
    840  1.1  christos 		/*
    841  1.1  christos 		 * If the new node has a given expiration time, insert it
    842  1.1  christos 		 * into the list of expiring nodes with the ones to be
    843  1.1  christos 		 * removed first added to the front of the list. The
    844  1.1  christos 		 * insertion is O(n) but it is kept sorted for quick scans
    845  1.1  christos 		 * at expiration interval checks.
    846  1.1  christos 		 */
    847  1.1  christos 		ip_pool_node_t *n;
    848  1.1  christos 
    849  1.1  christos 		x->ipn_die = softc->ipf_ticks + IPF_TTLVAL(x->ipn_die);
    850  1.1  christos 		for (n = softp->ipf_node_explist; n != NULL; n = n->ipn_dnext) {
    851  1.1  christos 			if (x->ipn_die < n->ipn_die)
    852  1.1  christos 				break;
    853  1.1  christos 			if (n->ipn_dnext == NULL) {
    854  1.1  christos 				/*
    855  1.1  christos 				 * We've got to the last node and everything
    856  1.1  christos 				 * wanted to be expired before this new node,
    857  1.1  christos 				 * so we have to tack it on the end...
    858  1.1  christos 				 */
    859  1.1  christos 				n->ipn_dnext = x;
    860  1.1  christos 				x->ipn_pdnext = &n->ipn_dnext;
    861  1.1  christos 				n = NULL;
    862  1.1  christos 				break;
    863  1.1  christos 			}
    864  1.1  christos 		}
    865  1.1  christos 
    866  1.1  christos 		if (softp->ipf_node_explist == NULL) {
    867  1.1  christos 			softp->ipf_node_explist = x;
    868  1.1  christos 			x->ipn_pdnext = &softp->ipf_node_explist;
    869  1.1  christos 		} else if (n != NULL) {
    870  1.1  christos 			x->ipn_dnext = n;
    871  1.1  christos 			x->ipn_pdnext = n->ipn_pdnext;
    872  1.1  christos 			n->ipn_pdnext = &x->ipn_dnext;
    873  1.1  christos 		}
    874  1.1  christos 	}
    875  1.1  christos 
    876  1.1  christos 	rn = ipo->ipo_head->addaddr(ipo->ipo_head, &x->ipn_addr, &x->ipn_mask,
    877  1.1  christos 				    x->ipn_nodes);
    878  1.1  christos #ifdef	DEBUG_POOL
    879  1.1  christos 	printf("Added %p at %p\n", x, rn);
    880  1.1  christos #endif
    881  1.1  christos 
    882  1.1  christos 	if (rn == NULL) {
    883  1.1  christos 		KFREE(x);
    884  1.1  christos 		IPFERROR(70005);
    885  1.1  christos 		return ENOMEM;
    886  1.1  christos 	}
    887  1.1  christos 
    888  1.1  christos 	x->ipn_ref = 1;
    889  1.3   darrenr 	x->ipn_pnext = ipo->ipo_tail;
    890  1.3   darrenr 	*ipo->ipo_tail = x;
    891  1.3   darrenr 	ipo->ipo_tail = &x->ipn_next;
    892  1.1  christos 
    893  1.1  christos 	softp->ipf_pool_stats.ipls_nodes++;
    894  1.1  christos 
    895  1.1  christos 	return 0;
    896  1.1  christos }
    897  1.1  christos 
    898  1.1  christos 
    899  1.1  christos /* ------------------------------------------------------------------------ */
    900  1.1  christos /* Function:    ipf_pool_create                                             */
    901  1.1  christos /* Returns:     int      - 0 = success, else error                          */
    902  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    903  1.1  christos /*              softp(I) - pointer to soft context pool information         */
    904  1.1  christos /*              op(I)    - pointer to iplookup struct with call details     */
    905  1.1  christos /* Locks:       WRITE(ipf_poolrw)                                           */
    906  1.1  christos /*                                                                          */
    907  1.1  christos /* Creates a new group according to the paramters passed in via the         */
    908  1.1  christos /* iplookupop structure.  Does not check to see if the group already exists */
    909  1.1  christos /* when being inserted - assume this has already been done.  If the pool is */
    910  1.1  christos /* marked as being anonymous, give it a new, unique, identifier.  Call any  */
    911  1.1  christos /* other functions required to initialise the structure.                    */
    912  1.1  christos /*                                                                          */
    913  1.1  christos /* If the structure is flagged for deletion then reset the flag and return, */
    914  1.1  christos /* as this likely means we've tried to free a pool that is in use (flush)   */
    915  1.1  christos /* and now want to repopulate it with "new" data.                           */
    916  1.1  christos /* ------------------------------------------------------------------------ */
    917  1.1  christos static int
    918  1.2  christos ipf_pool_create(ipf_main_softc_t *softc, ipf_pool_softc_t *softp,
    919  1.2  christos     iplookupop_t *op)
    920  1.1  christos {
    921  1.1  christos 	char name[FR_GROUPLEN];
    922  1.1  christos 	int poolnum, unit;
    923  1.1  christos 	ip_pool_t *h;
    924  1.1  christos 
    925  1.1  christos 	unit = op->iplo_unit;
    926  1.1  christos 
    927  1.1  christos 	if ((op->iplo_arg & LOOKUP_ANON) == 0) {
    928  1.1  christos 		h = ipf_pool_exists(softp, unit, op->iplo_name);
    929  1.1  christos 		if (h != NULL) {
    930  1.1  christos 			if ((h->ipo_flags & IPOOL_DELETE) == 0) {
    931  1.1  christos 				IPFERROR(70006);
    932  1.1  christos 				return EEXIST;
    933  1.1  christos 			}
    934  1.1  christos 			h->ipo_flags &= ~IPOOL_DELETE;
    935  1.1  christos 			return 0;
    936  1.1  christos 		}
    937  1.1  christos 	}
    938  1.1  christos 
    939  1.1  christos 	KMALLOC(h, ip_pool_t *);
    940  1.1  christos 	if (h == NULL) {
    941  1.1  christos 		IPFERROR(70007);
    942  1.1  christos 		return ENOMEM;
    943  1.1  christos 	}
    944  1.1  christos 	bzero(h, sizeof(*h));
    945  1.1  christos 
    946  1.1  christos 	if (ipf_rx_inithead(softp->ipf_radix, &h->ipo_head) != 0) {
    947  1.1  christos 		KFREE(h);
    948  1.1  christos 		IPFERROR(70008);
    949  1.1  christos 		return ENOMEM;
    950  1.1  christos 	}
    951  1.1  christos 
    952  1.1  christos 	if ((op->iplo_arg & LOOKUP_ANON) != 0) {
    953  1.1  christos 		ip_pool_t *p;
    954  1.1  christos 
    955  1.1  christos 		h->ipo_flags |= IPOOL_ANON;
    956  1.1  christos 		poolnum = LOOKUP_ANON;
    957  1.1  christos 
    958  1.4  christos 		snprintf(name, sizeof(name), "%x", poolnum);
    959  1.1  christos 
    960  1.1  christos 		for (p = softp->ipf_pool_list[unit + 1]; p != NULL; ) {
    961  1.1  christos 			if (strncmp(name, p->ipo_name,
    962  1.1  christos 				    sizeof(p->ipo_name)) == 0) {
    963  1.1  christos 				poolnum++;
    964  1.4  christos 				snprintf(name, sizeof(name), "%x", poolnum);
    965  1.1  christos 				p = softp->ipf_pool_list[unit + 1];
    966  1.1  christos 			} else
    967  1.1  christos 				p = p->ipo_next;
    968  1.1  christos 		}
    969  1.1  christos 
    970  1.1  christos 		(void)strncpy(h->ipo_name, name, sizeof(h->ipo_name));
    971  1.1  christos 		(void)strncpy(op->iplo_name, name, sizeof(op->iplo_name));
    972  1.1  christos 	} else {
    973  1.1  christos 		(void)strncpy(h->ipo_name, op->iplo_name, sizeof(h->ipo_name));
    974  1.1  christos 	}
    975  1.1  christos 
    976  1.1  christos 	h->ipo_radix = softp->ipf_radix;
    977  1.1  christos 	h->ipo_ref = 1;
    978  1.1  christos 	h->ipo_list = NULL;
    979  1.3   darrenr 	h->ipo_tail = &h->ipo_list;
    980  1.1  christos 	h->ipo_unit = unit;
    981  1.1  christos 	h->ipo_next = softp->ipf_pool_list[unit + 1];
    982  1.1  christos 	if (softp->ipf_pool_list[unit + 1] != NULL)
    983  1.1  christos 		softp->ipf_pool_list[unit + 1]->ipo_pnext = &h->ipo_next;
    984  1.1  christos 	h->ipo_pnext = &softp->ipf_pool_list[unit + 1];
    985  1.1  christos 	softp->ipf_pool_list[unit + 1] = h;
    986  1.1  christos 
    987  1.1  christos 	softp->ipf_pool_stats.ipls_pools++;
    988  1.1  christos 
    989  1.1  christos 	return 0;
    990  1.1  christos }
    991  1.1  christos 
    992  1.1  christos 
    993  1.1  christos /* ------------------------------------------------------------------------ */
    994  1.1  christos /* Function:    ipf_pool_remove_node                                        */
    995  1.1  christos /* Returns:     int      - 0 = success, else error                          */
    996  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
    997  1.1  christos /*              ipo(I)   - pointer to the pool to remove the node from.     */
    998  1.1  christos /*              ipe(I)   - address being deleted as a node                  */
    999  1.1  christos /* Locks:       WRITE(ipf_poolrw)                                           */
   1000  1.1  christos /*                                                                          */
   1001  1.1  christos /* Remove a node from the pool given by ipo.                                */
   1002  1.1  christos /* ------------------------------------------------------------------------ */
   1003  1.1  christos static int
   1004  1.3   darrenr ipf_pool_remove_node(ipf_main_softc_t *softc, ipf_pool_softc_t *softp,
   1005  1.3   darrenr 	ip_pool_t *ipo, ip_pool_node_t *ipe)
   1006  1.1  christos {
   1007  1.3   darrenr 	void *ptr;
   1008  1.3   darrenr 
   1009  1.3   darrenr 	if (ipo->ipo_tail == &ipe->ipn_next)
   1010  1.3   darrenr 		ipo->ipo_tail = ipe->ipn_pnext;
   1011  1.1  christos 
   1012  1.1  christos 	if (ipe->ipn_pnext != NULL)
   1013  1.1  christos 		*ipe->ipn_pnext = ipe->ipn_next;
   1014  1.1  christos 	if (ipe->ipn_next != NULL)
   1015  1.1  christos 		ipe->ipn_next->ipn_pnext = ipe->ipn_pnext;
   1016  1.1  christos 
   1017  1.1  christos 	if (ipe->ipn_pdnext != NULL)
   1018  1.1  christos 		*ipe->ipn_pdnext = ipe->ipn_dnext;
   1019  1.1  christos 	if (ipe->ipn_dnext != NULL)
   1020  1.1  christos 		ipe->ipn_dnext->ipn_pdnext = ipe->ipn_pdnext;
   1021  1.1  christos 
   1022  1.3   darrenr 	ptr = ipo->ipo_head->deladdr(ipo->ipo_head, &ipe->ipn_addr,
   1023  1.3   darrenr 				     &ipe->ipn_mask);
   1024  1.1  christos 
   1025  1.3   darrenr 	if (ptr != NULL) {
   1026  1.3   darrenr 		ipf_pool_node_deref(softp, ipe);
   1027  1.3   darrenr 		return 0;
   1028  1.3   darrenr 	}
   1029  1.3   darrenr 	IPFERROR(70027);
   1030  1.3   darrenr 	return ESRCH;
   1031  1.1  christos }
   1032  1.1  christos 
   1033  1.1  christos 
   1034  1.1  christos /* ------------------------------------------------------------------------ */
   1035  1.1  christos /* Function:    ipf_pool_destroy                                            */
   1036  1.1  christos /* Returns:     int    - 0 = success, else error                            */
   1037  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   1038  1.1  christos /*              softp(I) - pointer to soft context pool information         */
   1039  1.1  christos /*              unit(I)  - ipfilter device to which we are working on      */
   1040  1.1  christos /*              name(I)  - name of the pool                                 */
   1041  1.1  christos /* Locks:       WRITE(ipf_poolrw) or WRITE(ipf_global)                      */
   1042  1.1  christos /*                                                                          */
   1043  1.1  christos /* Search for a pool using paramters passed in and if it's not otherwise    */
   1044  1.1  christos /* busy, free it.  If it is busy, clear all of its nodes, mark it for being */
   1045  1.1  christos /* deleted and return an error saying it is busy.                           */
   1046  1.1  christos /*                                                                          */
   1047  1.1  christos /* NOTE: Because this function is called out of ipfdetach() where ipf_poolrw*/
   1048  1.1  christos /* may not be initialised, we can't use an ASSERT to enforce the locking    */
   1049  1.1  christos /* assertion that one of the two (ipf_poolrw,ipf_global) is held.           */
   1050  1.1  christos /* ------------------------------------------------------------------------ */
   1051  1.1  christos static int
   1052  1.2  christos ipf_pool_destroy(ipf_main_softc_t *softc, ipf_pool_softc_t *softp, int unit,
   1053  1.2  christos     char *name)
   1054  1.1  christos {
   1055  1.1  christos 	ip_pool_t *ipo;
   1056  1.1  christos 
   1057  1.1  christos 	ipo = ipf_pool_exists(softp, unit, name);
   1058  1.1  christos 	if (ipo == NULL) {
   1059  1.1  christos 		IPFERROR(70009);
   1060  1.1  christos 		return ESRCH;
   1061  1.1  christos 	}
   1062  1.1  christos 
   1063  1.1  christos 	if (ipo->ipo_ref != 1) {
   1064  1.3   darrenr 		ipf_pool_clearnodes(softc, softp, ipo);
   1065  1.1  christos 		ipo->ipo_flags |= IPOOL_DELETE;
   1066  1.1  christos 		return 0;
   1067  1.1  christos 	}
   1068  1.1  christos 
   1069  1.3   darrenr 	ipf_pool_free(softc, softp, ipo);
   1070  1.1  christos 	return 0;
   1071  1.1  christos }
   1072  1.1  christos 
   1073  1.1  christos 
   1074  1.1  christos /* ------------------------------------------------------------------------ */
   1075  1.1  christos /* Function:    ipf_pool_flush                                              */
   1076  1.1  christos /* Returns:     int    - number of pools deleted                            */
   1077  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   1078  1.1  christos /*              arg(I)   - pointer to local context to use                  */
   1079  1.1  christos /*              fp(I)    - which pool(s) to flush                           */
   1080  1.1  christos /* Locks:       WRITE(ipf_poolrw) or WRITE(ipf_global)                      */
   1081  1.1  christos /*                                                                          */
   1082  1.1  christos /* Free all pools associated with the device that matches the unit number   */
   1083  1.1  christos /* passed in with operation.                                                */
   1084  1.1  christos /*                                                                          */
   1085  1.1  christos /* NOTE: Because this function is called out of ipfdetach() where ipf_poolrw*/
   1086  1.1  christos /* may not be initialised, we can't use an ASSERT to enforce the locking    */
   1087  1.1  christos /* assertion that one of the two (ipf_poolrw,ipf_global) is held.           */
   1088  1.1  christos /* ------------------------------------------------------------------------ */
   1089  1.1  christos static size_t
   1090  1.2  christos ipf_pool_flush(ipf_main_softc_t *softc, void *arg, iplookupflush_t *fp)
   1091  1.1  christos {
   1092  1.1  christos 	ipf_pool_softc_t *softp = arg;
   1093  1.1  christos 	int i, num = 0, unit, err;
   1094  1.1  christos 	ip_pool_t *p, *q;
   1095  1.1  christos 
   1096  1.1  christos 	unit = fp->iplf_unit;
   1097  1.1  christos 	for (i = -1; i <= IPL_LOGMAX; i++) {
   1098  1.1  christos 		if (unit != IPLT_ALL && i != unit)
   1099  1.1  christos 			continue;
   1100  1.1  christos 		for (q = softp->ipf_pool_list[i + 1]; (p = q) != NULL; ) {
   1101  1.1  christos 			q = p->ipo_next;
   1102  1.1  christos 			err = ipf_pool_destroy(softc, softp, i, p->ipo_name);
   1103  1.1  christos 			if (err == 0)
   1104  1.1  christos 				num++;
   1105  1.1  christos 		}
   1106  1.1  christos 	}
   1107  1.1  christos 	return num;
   1108  1.1  christos }
   1109  1.1  christos 
   1110  1.1  christos 
   1111  1.1  christos /* ------------------------------------------------------------------------ */
   1112  1.1  christos /* Function:    ipf_pool_free                                               */
   1113  1.1  christos /* Returns:     void                                                        */
   1114  1.3   darrenr /* Parameters:  softc(I) - pointer to soft context main structure           */
   1115  1.3   darrenr /*              softp(I) - pointer to soft context pool information         */
   1116  1.1  christos /*              ipo(I) - pointer to pool structure                          */
   1117  1.1  christos /* Locks:       WRITE(ipf_poolrw) or WRITE(ipf_global)                      */
   1118  1.1  christos /*                                                                          */
   1119  1.1  christos /* Deletes the pool strucutre passed in from the list of pools and deletes  */
   1120  1.1  christos /* all of the address information stored in it, including any tree data     */
   1121  1.1  christos /* structures also allocated.                                               */
   1122  1.1  christos /*                                                                          */
   1123  1.1  christos /* NOTE: Because this function is called out of ipfdetach() where ipf_poolrw*/
   1124  1.1  christos /* may not be initialised, we can't use an ASSERT to enforce the locking    */
   1125  1.1  christos /* assertion that one of the two (ipf_poolrw,ipf_global) is held.           */
   1126  1.1  christos /* ------------------------------------------------------------------------ */
   1127  1.1  christos static void
   1128  1.3   darrenr ipf_pool_free(ipf_main_softc_t *softc, ipf_pool_softc_t *softp, ip_pool_t *ipo)
   1129  1.1  christos {
   1130  1.1  christos 
   1131  1.3   darrenr 	ipf_pool_clearnodes(softc, softp, ipo);
   1132  1.1  christos 
   1133  1.1  christos 	if (ipo->ipo_next != NULL)
   1134  1.1  christos 		ipo->ipo_next->ipo_pnext = ipo->ipo_pnext;
   1135  1.1  christos 	*ipo->ipo_pnext = ipo->ipo_next;
   1136  1.1  christos 	ipf_rx_freehead(ipo->ipo_head);
   1137  1.1  christos 	KFREE(ipo);
   1138  1.1  christos 
   1139  1.1  christos 	softp->ipf_pool_stats.ipls_pools--;
   1140  1.1  christos }
   1141  1.1  christos 
   1142  1.1  christos 
   1143  1.1  christos /* ------------------------------------------------------------------------ */
   1144  1.1  christos /* Function:    ipf_pool_clearnodes                                         */
   1145  1.1  christos /* Returns:     void                                                        */
   1146  1.3   darrenr /* Parameters:  softc(I) - pointer to soft context main structure           */
   1147  1.3   darrenr /*              softp(I) - pointer to soft context pool information         */
   1148  1.1  christos /*              ipo(I)   - pointer to pool structure                        */
   1149  1.1  christos /* Locks:       WRITE(ipf_poolrw) or WRITE(ipf_global)                      */
   1150  1.1  christos /*                                                                          */
   1151  1.1  christos /* Deletes all nodes stored in a pool structure.                            */
   1152  1.1  christos /* ------------------------------------------------------------------------ */
   1153  1.1  christos static void
   1154  1.3   darrenr ipf_pool_clearnodes(ipf_main_softc_t *softc, ipf_pool_softc_t *softp,
   1155  1.3   darrenr 	ip_pool_t *ipo)
   1156  1.1  christos {
   1157  1.1  christos 	ip_pool_node_t *n, **next;
   1158  1.1  christos 
   1159  1.3   darrenr 	for (next = &ipo->ipo_list; (n = *next) != NULL; )
   1160  1.3   darrenr 		ipf_pool_remove_node(softc, softp, ipo, n);
   1161  1.1  christos 
   1162  1.1  christos 	ipo->ipo_list = NULL;
   1163  1.1  christos }
   1164  1.1  christos 
   1165  1.1  christos 
   1166  1.1  christos /* ------------------------------------------------------------------------ */
   1167  1.1  christos /* Function:    ipf_pool_deref                                              */
   1168  1.1  christos /* Returns:     void                                                        */
   1169  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   1170  1.1  christos /*              arg(I)   - pointer to local context to use                  */
   1171  1.1  christos /*              pool(I)  - pointer to pool structure                        */
   1172  1.1  christos /* Locks:       WRITE(ipf_poolrw)                                           */
   1173  1.1  christos /*                                                                          */
   1174  1.1  christos /* Drop the number of known references to this pool structure by one and if */
   1175  1.1  christos /* we arrive at zero known references, free it.                             */
   1176  1.1  christos /* ------------------------------------------------------------------------ */
   1177  1.1  christos static int
   1178  1.2  christos ipf_pool_deref(ipf_main_softc_t *softc, void *arg, void *pool)
   1179  1.1  christos {
   1180  1.1  christos 	ip_pool_t *ipo = pool;
   1181  1.1  christos 
   1182  1.1  christos 	ipo->ipo_ref--;
   1183  1.1  christos 
   1184  1.1  christos 	if (ipo->ipo_ref == 0)
   1185  1.3   darrenr 		ipf_pool_free(softc, arg, ipo);
   1186  1.1  christos 
   1187  1.1  christos 	else if ((ipo->ipo_ref == 1) && (ipo->ipo_flags & IPOOL_DELETE))
   1188  1.1  christos 		ipf_pool_destroy(softc, arg, ipo->ipo_unit, ipo->ipo_name);
   1189  1.1  christos 
   1190  1.1  christos 	return 0;
   1191  1.1  christos }
   1192  1.1  christos 
   1193  1.1  christos 
   1194  1.1  christos /* ------------------------------------------------------------------------ */
   1195  1.1  christos /* Function:    ipf_pool_node_deref                                         */
   1196  1.1  christos /* Returns:     void                                                        */
   1197  1.1  christos /* Parameters:  softp(I) - pointer to soft context pool information         */
   1198  1.1  christos /*              ipn(I)   - pointer to pool structure                        */
   1199  1.1  christos /* Locks:       WRITE(ipf_poolrw)                                           */
   1200  1.1  christos /*                                                                          */
   1201  1.1  christos /* Drop a reference to the pool node passed in and if we're the last, free  */
   1202  1.1  christos /* it all up and adjust the stats accordingly.                              */
   1203  1.1  christos /* ------------------------------------------------------------------------ */
   1204  1.1  christos static void
   1205  1.2  christos ipf_pool_node_deref(ipf_pool_softc_t *softp, ip_pool_node_t *ipn)
   1206  1.1  christos {
   1207  1.1  christos 
   1208  1.1  christos 	ipn->ipn_ref--;
   1209  1.1  christos 
   1210  1.1  christos 	if (ipn->ipn_ref == 0) {
   1211  1.1  christos 		KFREE(ipn);
   1212  1.1  christos 		softp->ipf_pool_stats.ipls_nodes--;
   1213  1.1  christos 	}
   1214  1.1  christos }
   1215  1.1  christos 
   1216  1.1  christos 
   1217  1.1  christos /* ------------------------------------------------------------------------ */
   1218  1.1  christos /* Function:    ipf_pool_iter_next                                          */
   1219  1.1  christos /* Returns:     void                                                        */
   1220  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   1221  1.1  christos /*              arg(I)   - pointer to local context to use                  */
   1222  1.1  christos /*              token(I) - pointer to pool structure                        */
   1223  1.1  christos /*              ilp(IO)  - pointer to pool iterating structure              */
   1224  1.1  christos /*                                                                          */
   1225  1.1  christos /* ------------------------------------------------------------------------ */
   1226  1.1  christos static int
   1227  1.2  christos ipf_pool_iter_next(ipf_main_softc_t *softc, void *arg, ipftoken_t *token,
   1228  1.2  christos     ipflookupiter_t *ilp)
   1229  1.1  christos {
   1230  1.1  christos 	ipf_pool_softc_t *softp = arg;
   1231  1.1  christos 	ip_pool_node_t *node, zn, *nextnode;
   1232  1.1  christos 	ip_pool_t *ipo, zp, *nextipo;
   1233  1.1  christos 	void *pnext;
   1234  1.1  christos 	int err;
   1235  1.1  christos 
   1236  1.1  christos 	err = 0;
   1237  1.1  christos 	node = NULL;
   1238  1.1  christos 	nextnode = NULL;
   1239  1.1  christos 	ipo = NULL;
   1240  1.1  christos 	nextipo = NULL;
   1241  1.1  christos 
   1242  1.1  christos 	READ_ENTER(&softc->ipf_poolrw);
   1243  1.1  christos 
   1244  1.1  christos 	switch (ilp->ili_otype)
   1245  1.1  christos 	{
   1246  1.1  christos 	case IPFLOOKUPITER_LIST :
   1247  1.1  christos 		ipo = token->ipt_data;
   1248  1.1  christos 		if (ipo == NULL) {
   1249  1.1  christos 			nextipo = softp->ipf_pool_list[(int)ilp->ili_unit + 1];
   1250  1.1  christos 		} else {
   1251  1.1  christos 			nextipo = ipo->ipo_next;
   1252  1.1  christos 		}
   1253  1.1  christos 
   1254  1.1  christos 		if (nextipo != NULL) {
   1255  1.1  christos 			ATOMIC_INC32(nextipo->ipo_ref);
   1256  1.1  christos 			token->ipt_data = nextipo;
   1257  1.1  christos 		} else {
   1258  1.1  christos 			bzero((char *)&zp, sizeof(zp));
   1259  1.1  christos 			nextipo = &zp;
   1260  1.1  christos 			token->ipt_data = NULL;
   1261  1.1  christos 		}
   1262  1.1  christos 		pnext = nextipo->ipo_next;
   1263  1.1  christos 		break;
   1264  1.1  christos 
   1265  1.1  christos 	case IPFLOOKUPITER_NODE :
   1266  1.1  christos 		node = token->ipt_data;
   1267  1.1  christos 		if (node == NULL) {
   1268  1.1  christos 			ipo = ipf_pool_exists(arg, ilp->ili_unit,
   1269  1.1  christos 					      ilp->ili_name);
   1270  1.1  christos 			if (ipo == NULL) {
   1271  1.1  christos 				IPFERROR(70010);
   1272  1.1  christos 				err = ESRCH;
   1273  1.1  christos 			} else {
   1274  1.1  christos 				nextnode = ipo->ipo_list;
   1275  1.1  christos 				ipo = NULL;
   1276  1.1  christos 			}
   1277  1.1  christos 		} else {
   1278  1.1  christos 			nextnode = node->ipn_next;
   1279  1.1  christos 		}
   1280  1.1  christos 
   1281  1.1  christos 		if (nextnode != NULL) {
   1282  1.1  christos 			ATOMIC_INC32(nextnode->ipn_ref);
   1283  1.1  christos 			token->ipt_data = nextnode;
   1284  1.1  christos 		} else {
   1285  1.1  christos 			bzero((char *)&zn, sizeof(zn));
   1286  1.1  christos 			nextnode = &zn;
   1287  1.1  christos 			token->ipt_data = NULL;
   1288  1.1  christos 		}
   1289  1.1  christos 		pnext = nextnode->ipn_next;
   1290  1.1  christos 		break;
   1291  1.1  christos 
   1292  1.1  christos 	default :
   1293  1.1  christos 		IPFERROR(70011);
   1294  1.1  christos 		pnext = NULL;
   1295  1.1  christos 		err = EINVAL;
   1296  1.1  christos 		break;
   1297  1.1  christos 	}
   1298  1.1  christos 
   1299  1.1  christos 	RWLOCK_EXIT(&softc->ipf_poolrw);
   1300  1.1  christos 	if (err != 0)
   1301  1.1  christos 		return err;
   1302  1.1  christos 
   1303  1.1  christos 	switch (ilp->ili_otype)
   1304  1.1  christos 	{
   1305  1.1  christos 	case IPFLOOKUPITER_LIST :
   1306  1.1  christos 		err = COPYOUT(nextipo, ilp->ili_data, sizeof(*nextipo));
   1307  1.1  christos 		if (err != 0)  {
   1308  1.1  christos 			IPFERROR(70012);
   1309  1.1  christos 			err = EFAULT;
   1310  1.1  christos 		}
   1311  1.1  christos 		if (ipo != NULL) {
   1312  1.1  christos 			WRITE_ENTER(&softc->ipf_poolrw);
   1313  1.1  christos 			ipf_pool_deref(softc, softp, ipo);
   1314  1.1  christos 			RWLOCK_EXIT(&softc->ipf_poolrw);
   1315  1.1  christos 		}
   1316  1.1  christos 		break;
   1317  1.1  christos 
   1318  1.1  christos 	case IPFLOOKUPITER_NODE :
   1319  1.1  christos 		err = COPYOUT(nextnode, ilp->ili_data, sizeof(*nextnode));
   1320  1.1  christos 		if (err != 0) {
   1321  1.1  christos 			IPFERROR(70013);
   1322  1.1  christos 			err = EFAULT;
   1323  1.1  christos 		}
   1324  1.1  christos 		if (node != NULL) {
   1325  1.1  christos 			WRITE_ENTER(&softc->ipf_poolrw);
   1326  1.1  christos 			ipf_pool_node_deref(softp, node);
   1327  1.1  christos 			RWLOCK_EXIT(&softc->ipf_poolrw);
   1328  1.1  christos 		}
   1329  1.1  christos 		break;
   1330  1.1  christos 	}
   1331  1.1  christos 	if (pnext == NULL)
   1332  1.1  christos 		ipf_token_mark_complete(token);
   1333  1.1  christos 
   1334  1.1  christos 	return err;
   1335  1.1  christos }
   1336  1.1  christos 
   1337  1.1  christos 
   1338  1.1  christos /* ------------------------------------------------------------------------ */
   1339  1.1  christos /* Function:    ipf_pool_iterderef                                          */
   1340  1.1  christos /* Returns:     void                                                        */
   1341  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   1342  1.1  christos /*              arg(I)   - pointer to local context to use                  */
   1343  1.1  christos /*              unit(I)  - ipfilter device to which we are working on       */
   1344  1.1  christos /* Locks:       WRITE(ipf_poolrw)                                           */
   1345  1.1  christos /*                                                                          */
   1346  1.1  christos /* ------------------------------------------------------------------------ */
   1347  1.1  christos static int
   1348  1.2  christos ipf_pool_iter_deref(ipf_main_softc_t *softc, void *arg, int otype, int unit,
   1349  1.2  christos     void *data)
   1350  1.1  christos {
   1351  1.1  christos 	ipf_pool_softc_t *softp = arg;
   1352  1.1  christos 
   1353  1.1  christos 	if (data == NULL)
   1354  1.1  christos 		return EINVAL;
   1355  1.1  christos 
   1356  1.1  christos 	if (unit < 0 || unit > IPL_LOGMAX)
   1357  1.1  christos 		return EINVAL;
   1358  1.1  christos 
   1359  1.1  christos 	switch (otype)
   1360  1.1  christos 	{
   1361  1.1  christos 	case IPFLOOKUPITER_LIST :
   1362  1.1  christos 		ipf_pool_deref(softc, softp, (ip_pool_t *)data);
   1363  1.1  christos 		break;
   1364  1.1  christos 
   1365  1.1  christos 	case IPFLOOKUPITER_NODE :
   1366  1.1  christos 		ipf_pool_node_deref(softp, (ip_pool_node_t *)data);
   1367  1.1  christos 		break;
   1368  1.1  christos 	default :
   1369  1.1  christos 		break;
   1370  1.1  christos 	}
   1371  1.1  christos 
   1372  1.1  christos 	return 0;
   1373  1.1  christos }
   1374  1.1  christos 
   1375  1.1  christos 
   1376  1.1  christos /* ------------------------------------------------------------------------ */
   1377  1.1  christos /* Function:    ipf_pool_expire                                             */
   1378  1.1  christos /* Returns:     Nil                                                         */
   1379  1.1  christos /* Parameters:  softc(I) - pointer to soft context main structure           */
   1380  1.1  christos /*              arg(I)   - pointer to local context to use                  */
   1381  1.1  christos /*                                                                          */
   1382  1.1  christos /* At present this function exists just to support temporary addition of    */
   1383  1.1  christos /* nodes to the address pool.                                               */
   1384  1.1  christos /* ------------------------------------------------------------------------ */
   1385  1.1  christos static void
   1386  1.2  christos ipf_pool_expire(ipf_main_softc_t *softc, void *arg)
   1387  1.1  christos {
   1388  1.1  christos 	ipf_pool_softc_t *softp = arg;
   1389  1.1  christos 	ip_pool_node_t *n;
   1390  1.1  christos 
   1391  1.1  christos 	while ((n = softp->ipf_node_explist) != NULL) {
   1392  1.1  christos 		/*
   1393  1.1  christos 		 * Because the list is kept sorted on insertion, the fist
   1394  1.1  christos 		 * one that dies in the future means no more work to do.
   1395  1.1  christos 		 */
   1396  1.1  christos 		if (n->ipn_die > softc->ipf_ticks)
   1397  1.1  christos 			break;
   1398  1.3   darrenr 		ipf_pool_remove_node(softc, softp, n->ipn_owner, n);
   1399  1.1  christos 	}
   1400  1.1  christos }
   1401  1.1  christos 
   1402  1.1  christos 
   1403  1.1  christos 
   1404  1.1  christos 
   1405  1.1  christos #ifndef _KERNEL
   1406  1.1  christos void
   1407  1.1  christos ipf_pool_dump(softc, arg)
   1408  1.1  christos 	ipf_main_softc_t *softc;
   1409  1.1  christos 	void *arg;
   1410  1.1  christos {
   1411  1.1  christos 	ipf_pool_softc_t *softp = arg;
   1412  1.1  christos 	ip_pool_t *ipl;
   1413  1.1  christos 	int i;
   1414  1.1  christos 
   1415  1.1  christos 	printf("List of configured pools\n");
   1416  1.1  christos 	for (i = 0; i <= LOOKUP_POOL_MAX; i++)
   1417  1.1  christos 		for (ipl = softp->ipf_pool_list[i]; ipl != NULL;
   1418  1.1  christos 		     ipl = ipl->ipo_next)
   1419  1.1  christos 			printpool(ipl, bcopywrap, NULL, opts, NULL);
   1420  1.1  christos }
   1421  1.1  christos #endif
   1422