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