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