xref: /freebsd/sbin/ipf/libipf/load_hashnode.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_htable.h"
15 
16 
17 int
18 load_hashnode(int unit, char *name, iphtent_t *node, int ttl,
19 	ioctlfunc_t iocfunc)
20 {
21 	iplookupop_t op;
22 	iphtent_t ipe;
23 	char *what;
24 	int err;
25 
26 	if (pool_open() == -1)
27 		return (-1);
28 
29 	op.iplo_type = IPLT_HASH;
30 	op.iplo_unit = unit;
31 	op.iplo_arg = 0;
32 	op.iplo_size = sizeof(ipe);
33 	op.iplo_struct = &ipe;
34 	strncpy(op.iplo_name, name, sizeof(op.iplo_name));
35 
36 	bzero((char *)&ipe, sizeof(ipe));
37 	ipe.ipe_family = node->ipe_family;
38 	ipe.ipe_die = ttl;
39 	bcopy((char *)&node->ipe_addr, (char *)&ipe.ipe_addr,
40 	      sizeof(ipe.ipe_addr));
41 	bcopy((char *)&node->ipe_mask, (char *)&ipe.ipe_mask,
42 	      sizeof(ipe.ipe_mask));
43 	bcopy((char *)&node->ipe_group, (char *)&ipe.ipe_group,
44 	      sizeof(ipe.ipe_group));
45 
46 	if ((opts & OPT_REMOVE) == 0) {
47 		what = "add";
48 		err = pool_ioctl(iocfunc, SIOCLOOKUPADDNODE, &op);
49 	} else {
50 		what = "delete";
51 		err = pool_ioctl(iocfunc, SIOCLOOKUPDELNODE, &op);
52 	}
53 
54 	if (err != 0)
55 		if (!(opts & OPT_DONOTHING)) {
56 			char msg[255];
57 			char ipaddr[80], mask_msg[10], mask[8];
58 
59 			inet_ntop(ipe.ipe_family,
60 				ipe.ipe_addr.vptr, ipaddr,
61 				sizeof(ipaddr));
62 #ifdef USE_INET6
63 			if (ipe.ipe_family == AF_INET) {
64 #endif
65 				inet_ntop(ipe.ipe_family,
66 					ipe.ipe_mask.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 node from lookup hash table(%s) node(%s%s)", what, name, ipaddr, mask_msg);
78 			return (ipf_perror_fd(pool_fd(), iocfunc, msg));
79 		}
80 	return (0);
81 }
82