xref: /freebsd-src/sbin/ipf/libipf/load_pool.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
141edb306SCy Schubert 
241edb306SCy Schubert /*
341edb306SCy Schubert  * Copyright (C) 2012 by Darren Reed.
441edb306SCy Schubert  *
541edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
641edb306SCy Schubert  *
741edb306SCy Schubert  * $Id$
841edb306SCy Schubert  */
941edb306SCy Schubert 
1041edb306SCy Schubert #include <fcntl.h>
1141edb306SCy Schubert #include <sys/ioctl.h>
1241edb306SCy Schubert #include "ipf.h"
1341edb306SCy Schubert #include "netinet/ip_lookup.h"
1441edb306SCy Schubert #include "netinet/ip_pool.h"
1541edb306SCy Schubert 
1641edb306SCy Schubert 
1741edb306SCy Schubert int
load_pool(ip_pool_t * plp,ioctlfunc_t iocfunc)18efeb8bffSCy Schubert load_pool(ip_pool_t *plp, ioctlfunc_t iocfunc)
1941edb306SCy Schubert {
2041edb306SCy Schubert 	iplookupop_t op;
2141edb306SCy Schubert 	ip_pool_node_t *a;
2241edb306SCy Schubert 	ip_pool_t pool;
2341edb306SCy Schubert 
2441edb306SCy Schubert 	if (pool_open() == -1)
25*2582ae57SCy Schubert 		return (-1);
2641edb306SCy Schubert 
2741edb306SCy Schubert 	op.iplo_unit = plp->ipo_unit;
2841edb306SCy Schubert 	op.iplo_type = IPLT_POOL;
2941edb306SCy Schubert 	op.iplo_arg = 0;
3041edb306SCy Schubert 	strncpy(op.iplo_name, plp->ipo_name, sizeof(op.iplo_name));
3141edb306SCy Schubert 	op.iplo_size = sizeof(pool);
3241edb306SCy Schubert 	op.iplo_struct = &pool;
3341edb306SCy Schubert 	bzero((char *)&pool, sizeof(pool));
3441edb306SCy Schubert 	pool.ipo_unit = plp->ipo_unit;
3541edb306SCy Schubert 	strncpy(pool.ipo_name, plp->ipo_name, sizeof(pool.ipo_name));
3641edb306SCy Schubert 	if (plp->ipo_name[0] == '\0')
3741edb306SCy Schubert 		op.iplo_arg |= IPOOL_ANON;
3841edb306SCy Schubert 
3941edb306SCy Schubert 	if ((opts & OPT_REMOVE) == 0) {
4041edb306SCy Schubert 		if (pool_ioctl(iocfunc, SIOCLOOKUPADDTABLE, &op)) {
4141edb306SCy Schubert 			if ((opts & OPT_DONOTHING) == 0) {
42*2582ae57SCy Schubert 				return (ipf_perror_fd(pool_fd(), iocfunc,
43*2582ae57SCy Schubert 						     "add lookup table"));
4441edb306SCy Schubert 			}
4541edb306SCy Schubert 		}
4641edb306SCy Schubert 	}
4741edb306SCy Schubert 
4841edb306SCy Schubert 	if (op.iplo_arg & IPOOL_ANON)
4941edb306SCy Schubert 		strncpy(pool.ipo_name, op.iplo_name, sizeof(pool.ipo_name));
5041edb306SCy Schubert 
5141edb306SCy Schubert 	if ((opts & OPT_VERBOSE) != 0) {
5241edb306SCy Schubert 		pool.ipo_list = plp->ipo_list;
5341edb306SCy Schubert 		(void) printpool(&pool, bcopywrap, pool.ipo_name, opts, NULL);
5441edb306SCy Schubert 		pool.ipo_list = NULL;
5541edb306SCy Schubert 	}
5641edb306SCy Schubert 
5741edb306SCy Schubert 	for (a = plp->ipo_list; a != NULL; a = a->ipn_next)
5841edb306SCy Schubert 		load_poolnode(plp->ipo_unit, pool.ipo_name,
5941edb306SCy Schubert 				     a, 0, iocfunc);
6041edb306SCy Schubert 
6141edb306SCy Schubert 	if ((opts & OPT_REMOVE) != 0) {
6241edb306SCy Schubert 		if (pool_ioctl(iocfunc, SIOCLOOKUPDELTABLE, &op))
6341edb306SCy Schubert 			if ((opts & OPT_DONOTHING) == 0) {
64*2582ae57SCy Schubert 				return (ipf_perror_fd(pool_fd(), iocfunc,
65*2582ae57SCy Schubert 						     "delete lookup table"));
6641edb306SCy Schubert 			}
6741edb306SCy Schubert 	}
68*2582ae57SCy Schubert 	return (0);
6941edb306SCy Schubert }
70