xref: /freebsd/sbin/ipf/libipf/load_pool.c (revision 069ac184)
1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  *
7  * $Id$
8  */
9 
10 #include <fcntl.h>
11 #include <sys/ioctl.h>
12 #include "ipf.h"
13 #include "netinet/ip_lookup.h"
14 #include "netinet/ip_pool.h"
15 
16 
17 int
18 load_pool(ip_pool_t *plp, ioctlfunc_t iocfunc)
19 {
20 	iplookupop_t op;
21 	ip_pool_node_t *a;
22 	ip_pool_t pool;
23 
24 	if (pool_open() == -1)
25 		return (-1);
26 
27 	op.iplo_unit = plp->ipo_unit;
28 	op.iplo_type = IPLT_POOL;
29 	op.iplo_arg = 0;
30 	strncpy(op.iplo_name, plp->ipo_name, sizeof(op.iplo_name));
31 	op.iplo_size = sizeof(pool);
32 	op.iplo_struct = &pool;
33 	bzero((char *)&pool, sizeof(pool));
34 	pool.ipo_unit = plp->ipo_unit;
35 	strncpy(pool.ipo_name, plp->ipo_name, sizeof(pool.ipo_name));
36 	if (plp->ipo_name[0] == '\0')
37 		op.iplo_arg |= IPOOL_ANON;
38 
39 	if ((opts & OPT_REMOVE) == 0) {
40 		if (pool_ioctl(iocfunc, SIOCLOOKUPADDTABLE, &op)) {
41 			if ((opts & OPT_DONOTHING) == 0) {
42 				return (ipf_perror_fd(pool_fd(), iocfunc,
43 						     "add lookup table"));
44 			}
45 		}
46 	}
47 
48 	if (op.iplo_arg & IPOOL_ANON)
49 		strncpy(pool.ipo_name, op.iplo_name, sizeof(pool.ipo_name));
50 
51 	if ((opts & OPT_VERBOSE) != 0) {
52 		pool.ipo_list = plp->ipo_list;
53 		(void) printpool(&pool, bcopywrap, pool.ipo_name, opts, NULL);
54 		pool.ipo_list = NULL;
55 	}
56 
57 	for (a = plp->ipo_list; a != NULL; a = a->ipn_next)
58 		load_poolnode(plp->ipo_unit, pool.ipo_name,
59 				     a, 0, iocfunc);
60 
61 	if ((opts & OPT_REMOVE) != 0) {
62 		if (pool_ioctl(iocfunc, SIOCLOOKUPDELTABLE, &op))
63 			if ((opts & OPT_DONOTHING) == 0) {
64 				return (ipf_perror_fd(pool_fd(), iocfunc,
65 						     "delete lookup table"));
66 			}
67 	}
68 	return (0);
69 }
70