xref: /freebsd/sbin/ipf/libipf/load_dstlistnode.c (revision 44bc30192139b0b3c95510ab3b35802bcc6d63e4)
1 /*
2  * Copyright (C) 2012 by Darren Reed.
3  *
4  * See the IPFILTER.LICENCE file for details on licencing.
5  *
6  * $Id: load_dstlistnode.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_pool.h"
14 
15 
16 int
load_dstlistnode(int role,char * name,ipf_dstnode_t * node,ioctlfunc_t iocfunc)17 load_dstlistnode(int role, char *name, ipf_dstnode_t *node,
18 	ioctlfunc_t iocfunc)
19 {
20 	iplookupop_t op;
21 	frdest_t *dst;
22 	char *what;
23 	int err;
24 
25 	if (pool_open() == -1)
26 		return (-1);
27 
28 	dst = calloc(1, sizeof(*dst) + node->ipfd_dest.fd_name);
29 	if (dst == NULL)
30 		return (-1);
31 
32 	op.iplo_unit = role;
33 	op.iplo_type = IPLT_DSTLIST;
34 	op.iplo_arg = 0;
35 	op.iplo_struct = dst;
36 	op.iplo_size = sizeof(*dst);
37 	if (node->ipfd_dest.fd_name >= 0)
38 		op.iplo_size += node->ipfd_dest.fd_name;
39 	(void) strncpy(op.iplo_name, name, sizeof(op.iplo_name));
40 
41 	dst->fd_addr = node->ipfd_dest.fd_addr;
42 	dst->fd_type = node->ipfd_dest.fd_type;
43 	dst->fd_name = node->ipfd_dest.fd_name;
44 	if (node->ipfd_dest.fd_name >= 0)
45 		bcopy(node->ipfd_names, (char *)dst + sizeof(*dst),
46 		      node->ipfd_dest.fd_name);
47 
48 	if ((opts & OPT_REMOVE) == 0) {
49 		what = "add";
50 		err = pool_ioctl(iocfunc, SIOCLOOKUPADDNODE, &op);
51 	} else {
52 		what = "delete";
53 		err = pool_ioctl(iocfunc, SIOCLOOKUPDELNODE, &op);
54 	}
55 	free(dst);
56 
57 	if (err != 0) {
58 		if ((opts & OPT_DONOTHING) == 0) {
59 			char msg[80];
60 
61 			(void) snprintf(msg, sizeof(msg), "%s lookup node", what);
62 			return (ipf_perror_fd(pool_fd(), iocfunc, msg));
63 		}
64 	}
65 
66 	return (0);
67 }
68