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