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