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