xref: /freebsd/sbin/ipf/libipf/load_dstlist.c (revision 2e4311906d8c8dc7a7c726345268253bca6d4acc)
1 /*
2  * Copyright (C) 2012 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: load_dstlist.c,v 1.1.2.5 2012/07/22 08:04:24 darren_r Exp $
7  */
8 
9 #include <fcntl.h>
10 #include <sys/ioctl.h>
11 #include "ipf.h"
12 #include "netinet/ip_lookup.h"
13 #include "netinet/ip_dstlist.h"
14 
15 
16 int
17 load_dstlist(ippool_dst_t *dst, ioctlfunc_t iocfunc, ipf_dstnode_t *nodes)
18 {
19 	iplookupop_t op;
20 	ipf_dstnode_t *a;
21 	ippool_dst_t dest;
22 
23 	if (dst->ipld_name[0] == '\0')
24 		return (-1);
25 
26 	if (pool_open() == -1)
27 		return (-1);
28 
29 	op.iplo_unit = dst->ipld_unit;
30 	op.iplo_type = IPLT_DSTLIST;
31 	op.iplo_arg = 0;
32 	strncpy(op.iplo_name, dst->ipld_name, sizeof(op.iplo_name));
33 	op.iplo_size = sizeof(dest);
34 	op.iplo_struct = &dest;
35 	bzero((char *)&dest, sizeof(dest));
36 	dest.ipld_unit = dst->ipld_unit;
37 	dest.ipld_policy = dst->ipld_policy;
38 	dest.ipld_flags = dst->ipld_flags;
39 	strncpy(dest.ipld_name, dst->ipld_name, sizeof(dest.ipld_name));
40 
41 	if ((opts & OPT_REMOVE) == 0) {
42 		if (pool_ioctl(iocfunc, SIOCLOOKUPADDTABLE, &op))
43 			if ((opts & OPT_DONOTHING) == 0) {
44 				return (ipf_perror_fd(pool_fd(), iocfunc,
45 						  "add destination list table"));
46 			}
47 	}
48 
49 	if ((opts & OPT_VERBOSE) != 0) {
50 		dest.ipld_dests = dst->ipld_dests;
51 		printdstlist(&dest, bcopywrap, dest.ipld_name, opts, nodes, NULL);
52 		dest.ipld_dests = NULL;
53 	}
54 
55 	for (a = nodes; a != NULL; a = a->ipfd_next)
56 		load_dstlistnode(dst->ipld_unit, dest.ipld_name, a, iocfunc);
57 
58 	if ((opts & OPT_REMOVE) != 0) {
59 		if (pool_ioctl(iocfunc, SIOCLOOKUPDELTABLE, &op))
60 			if ((opts & OPT_DONOTHING) == 0) {
61 				return (ipf_perror_fd(pool_fd(), iocfunc,
62 					      "delete destination list table"));
63 			}
64 	}
65 	return (0);
66 }
67