xref: /freebsd/sbin/ipf/libipf/load_poolnode.c (revision 7fdf597e96a02165cfe22ff357b857d5fa15ed8a)
1 
2 /*
3  * Copyright (C) 2012 by Darren Reed.
4  *
5  * See the IPFILTER.LICENCE file for details on licencing.
6  *
7  * $Id$
8  */
9 
10 #include <fcntl.h>
11 #include <sys/ioctl.h>
12 #include "ipf.h"
13 #include "netinet/ip_lookup.h"
14 #include "netinet/ip_pool.h"
15 
16 
17 int
18 load_poolnode(int role, char *name, ip_pool_node_t *node, int ttl,
19 	ioctlfunc_t iocfunc)
20 {
21 	ip_pool_node_t pn;
22 	iplookupop_t op;
23 	char *what;
24 	int err;
25 
26 	if (pool_open() == -1)
27 		return (-1);
28 
29 	op.iplo_unit = role;
30 	op.iplo_type = IPLT_POOL;
31 	op.iplo_arg = 0;
32 	op.iplo_struct = &pn;
33 	op.iplo_size = sizeof(pn);
34 	strncpy(op.iplo_name, name, sizeof(op.iplo_name));
35 
36 	bzero((char *)&pn, sizeof(pn));
37 	bcopy((char *)&node->ipn_addr, (char *)&pn.ipn_addr,
38 	      sizeof(pn.ipn_addr));
39 	bcopy((char *)&node->ipn_mask, (char *)&pn.ipn_mask,
40 	      sizeof(pn.ipn_mask));
41 	pn.ipn_info = node->ipn_info;
42 	pn.ipn_die = ttl;
43 	strncpy(pn.ipn_name, node->ipn_name, sizeof(pn.ipn_name));
44 
45 	if ((opts & OPT_REMOVE) == 0) {
46 		what = "add";
47 		err = pool_ioctl(iocfunc, SIOCLOOKUPADDNODE, &op);
48 	} else {
49 		what = "delete";
50 		err = pool_ioctl(iocfunc, SIOCLOOKUPDELNODE, &op);
51 	}
52 
53 	if (err != 0) {
54 		if ((opts & OPT_DONOTHING) == 0) {
55 			char msg[255];
56 			char ipaddr[80], mask_msg[10], mask[8];
57 
58 			inet_ntop(pn.ipn_addr.adf_family,
59 				pn.ipn_addr.adf_addr.vptr, ipaddr,
60 				sizeof(ipaddr));
61 
62 #ifdef USE_INET6
63 			if (pn.ipn_mask.adf_family == AF_INET) {
64 #endif
65 				inet_ntop(pn.ipn_mask.adf_family,
66 					pn.ipn_mask.adf_addr.vptr, mask,
67 					sizeof(mask));
68 				mask_msg[0]='/';
69 				mask_msg[1]='\0';
70 				strlcat(mask_msg, mask, sizeof(mask_msg));
71 #ifdef USE_INET6
72 			} else {
73 				mask_msg[0]='\0';
74 			}
75 #endif
76 
77 			snprintf(msg, sizeof(msg), "%s pool(%s) node(%s%s)",
78 				what, name, ipaddr, mask_msg);
79 			return (ipf_perror_fd(pool_fd(), iocfunc, msg));
80 		}
81 	}
82 
83 	return (0);
84 }
85