xref: /freebsd/sbin/ipf/libipf/load_pool.c (revision b3d14eaccc5f606690d99b1998bfdf32a22404f6)
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(plp, iocfunc)
20 	ip_pool_t *plp;
21 	ioctlfunc_t iocfunc;
22 {
23 	iplookupop_t op;
24 	ip_pool_node_t *a;
25 	ip_pool_t pool;
26 
27 	if (pool_open() == -1)
28 		return -1;
29 
30 	op.iplo_unit = plp->ipo_unit;
31 	op.iplo_type = IPLT_POOL;
32 	op.iplo_arg = 0;
33 	strncpy(op.iplo_name, plp->ipo_name, sizeof(op.iplo_name));
34 	op.iplo_size = sizeof(pool);
35 	op.iplo_struct = &pool;
36 	bzero((char *)&pool, sizeof(pool));
37 	pool.ipo_unit = plp->ipo_unit;
38 	strncpy(pool.ipo_name, plp->ipo_name, sizeof(pool.ipo_name));
39 	if (plp->ipo_name[0] == '\0')
40 		op.iplo_arg |= IPOOL_ANON;
41 
42 	if ((opts & OPT_REMOVE) == 0) {
43 		if (pool_ioctl(iocfunc, SIOCLOOKUPADDTABLE, &op)) {
44 			if ((opts & OPT_DONOTHING) == 0) {
45 				return ipf_perror_fd(pool_fd(), iocfunc,
46 						     "add lookup table");
47 			}
48 		}
49 	}
50 
51 	if (op.iplo_arg & IPOOL_ANON)
52 		strncpy(pool.ipo_name, op.iplo_name, sizeof(pool.ipo_name));
53 
54 	if ((opts & OPT_VERBOSE) != 0) {
55 		pool.ipo_list = plp->ipo_list;
56 		(void) printpool(&pool, bcopywrap, pool.ipo_name, opts, NULL);
57 		pool.ipo_list = NULL;
58 	}
59 
60 	for (a = plp->ipo_list; a != NULL; a = a->ipn_next)
61 		load_poolnode(plp->ipo_unit, pool.ipo_name,
62 				     a, 0, iocfunc);
63 
64 	if ((opts & OPT_REMOVE) != 0) {
65 		if (pool_ioctl(iocfunc, SIOCLOOKUPDELTABLE, &op))
66 			if ((opts & OPT_DONOTHING) == 0) {
67 				return ipf_perror_fd(pool_fd(), iocfunc,
68 						     "delete lookup table");
69 			}
70 	}
71 	return 0;
72 }
73