1*13885a66Sdarrenr /* $NetBSD: load_dstlist.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_dstlist.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_dstlist.h"
16bc4097aaSchristos
17bc4097aaSchristos
18bc4097aaSchristos int
load_dstlist(dst,iocfunc,nodes)19bc4097aaSchristos load_dstlist(dst, iocfunc, nodes)
20bc4097aaSchristos ippool_dst_t *dst;
21bc4097aaSchristos ioctlfunc_t iocfunc;
22bc4097aaSchristos ipf_dstnode_t *nodes;
23bc4097aaSchristos {
24bc4097aaSchristos iplookupop_t op;
25bc4097aaSchristos ipf_dstnode_t *a;
26bc4097aaSchristos ippool_dst_t dest;
27bc4097aaSchristos
28bc4097aaSchristos if (dst->ipld_name[0] == '\0')
29bc4097aaSchristos return -1;
30bc4097aaSchristos
31bc4097aaSchristos if (pool_open() == -1)
32bc4097aaSchristos return -1;
33bc4097aaSchristos
34bc4097aaSchristos op.iplo_unit = dst->ipld_unit;
35bc4097aaSchristos op.iplo_type = IPLT_DSTLIST;
36bc4097aaSchristos op.iplo_arg = 0;
37bc4097aaSchristos strncpy(op.iplo_name, dst->ipld_name, sizeof(op.iplo_name));
38bc4097aaSchristos op.iplo_size = sizeof(dest);
39bc4097aaSchristos op.iplo_struct = &dest;
40bc4097aaSchristos bzero((char *)&dest, sizeof(dest));
41bc4097aaSchristos dest.ipld_unit = dst->ipld_unit;
42bc4097aaSchristos dest.ipld_policy = dst->ipld_policy;
43bc4097aaSchristos dest.ipld_flags = dst->ipld_flags;
44bc4097aaSchristos strncpy(dest.ipld_name, dst->ipld_name, sizeof(dest.ipld_name));
45bc4097aaSchristos
46bc4097aaSchristos if ((opts & OPT_REMOVE) == 0) {
47bc4097aaSchristos if (pool_ioctl(iocfunc, SIOCLOOKUPADDTABLE, &op))
48bc4097aaSchristos if ((opts & OPT_DONOTHING) == 0) {
49c9d5dc6cSdarrenr return ipf_perror_fd(pool_fd(), iocfunc,
50c9d5dc6cSdarrenr "add destination list table");
51bc4097aaSchristos }
52bc4097aaSchristos }
53bc4097aaSchristos
54bc4097aaSchristos if ((opts & OPT_VERBOSE) != 0) {
55bc4097aaSchristos dest.ipld_dests = dst->ipld_dests;
56bc4097aaSchristos printdstlist(&dest, bcopywrap, dest.ipld_name, opts, nodes, NULL);
57bc4097aaSchristos dest.ipld_dests = NULL;
58bc4097aaSchristos }
59bc4097aaSchristos
60bc4097aaSchristos for (a = nodes; a != NULL; a = a->ipfd_next)
61c9d5dc6cSdarrenr load_dstlistnode(dst->ipld_unit, dest.ipld_name, a, iocfunc);
62bc4097aaSchristos
63bc4097aaSchristos if ((opts & OPT_REMOVE) != 0) {
64bc4097aaSchristos if (pool_ioctl(iocfunc, SIOCLOOKUPDELTABLE, &op))
65bc4097aaSchristos if ((opts & OPT_DONOTHING) == 0) {
66c9d5dc6cSdarrenr return ipf_perror_fd(pool_fd(), iocfunc,
67c9d5dc6cSdarrenr "delete destination list table");
68bc4097aaSchristos }
69bc4097aaSchristos }
70bc4097aaSchristos return 0;
71bc4097aaSchristos }
72