xref: /freebsd/sbin/ipf/libipf/load_hashnode.c (revision 9da30a23a5ecae151ae1db045354fab105c69e12)
141edb306SCy Schubert 
241edb306SCy Schubert /*
341edb306SCy Schubert  * Copyright (C) 2012 by Darren Reed.
441edb306SCy Schubert  *
541edb306SCy Schubert  * See the IPFILTER.LICENCE file for details on licencing.
641edb306SCy Schubert  *
741edb306SCy Schubert  * $Id$
841edb306SCy Schubert  */
941edb306SCy Schubert 
1041edb306SCy Schubert #include <fcntl.h>
1141edb306SCy Schubert #include <sys/ioctl.h>
1241edb306SCy Schubert #include "ipf.h"
1341edb306SCy Schubert #include "netinet/ip_lookup.h"
1441edb306SCy Schubert #include "netinet/ip_htable.h"
1541edb306SCy Schubert 
1641edb306SCy Schubert 
1741edb306SCy Schubert int
load_hashnode(int unit,char * name,iphtent_t * node,int ttl,ioctlfunc_t iocfunc)18efeb8bffSCy Schubert load_hashnode(int unit, char *name, iphtent_t *node, int ttl,
19efeb8bffSCy Schubert 	ioctlfunc_t iocfunc)
2041edb306SCy Schubert {
2141edb306SCy Schubert 	iplookupop_t op;
2241edb306SCy Schubert 	iphtent_t ipe;
2341edb306SCy Schubert 	char *what;
2441edb306SCy Schubert 	int err;
2541edb306SCy Schubert 
2641edb306SCy Schubert 	if (pool_open() == -1)
272582ae57SCy Schubert 		return (-1);
2841edb306SCy Schubert 
2941edb306SCy Schubert 	op.iplo_type = IPLT_HASH;
3041edb306SCy Schubert 	op.iplo_unit = unit;
3141edb306SCy Schubert 	op.iplo_arg = 0;
3241edb306SCy Schubert 	op.iplo_size = sizeof(ipe);
3341edb306SCy Schubert 	op.iplo_struct = &ipe;
3441edb306SCy Schubert 	strncpy(op.iplo_name, name, sizeof(op.iplo_name));
3541edb306SCy Schubert 
3641edb306SCy Schubert 	bzero((char *)&ipe, sizeof(ipe));
3741edb306SCy Schubert 	ipe.ipe_family = node->ipe_family;
3841edb306SCy Schubert 	ipe.ipe_die = ttl;
3941edb306SCy Schubert 	bcopy((char *)&node->ipe_addr, (char *)&ipe.ipe_addr,
4041edb306SCy Schubert 	      sizeof(ipe.ipe_addr));
4141edb306SCy Schubert 	bcopy((char *)&node->ipe_mask, (char *)&ipe.ipe_mask,
4241edb306SCy Schubert 	      sizeof(ipe.ipe_mask));
4341edb306SCy Schubert 	bcopy((char *)&node->ipe_group, (char *)&ipe.ipe_group,
4441edb306SCy Schubert 	      sizeof(ipe.ipe_group));
4541edb306SCy Schubert 
4641edb306SCy Schubert 	if ((opts & OPT_REMOVE) == 0) {
4741edb306SCy Schubert 		what = "add";
4841edb306SCy Schubert 		err = pool_ioctl(iocfunc, SIOCLOOKUPADDNODE, &op);
4941edb306SCy Schubert 	} else {
5041edb306SCy Schubert 		what = "delete";
5141edb306SCy Schubert 		err = pool_ioctl(iocfunc, SIOCLOOKUPDELNODE, &op);
5241edb306SCy Schubert 	}
5341edb306SCy Schubert 
5441edb306SCy Schubert 	if (err != 0)
5541edb306SCy Schubert 		if (!(opts & OPT_DONOTHING)) {
56*9da30a23SCy Schubert 			char msg[255];
57*9da30a23SCy Schubert 			char ipaddr[80], mask_msg[10], mask[8];
5841edb306SCy Schubert 
59*9da30a23SCy Schubert 			inet_ntop(ipe.ipe_family,
60*9da30a23SCy Schubert 				ipe.ipe_addr.vptr, ipaddr,
61*9da30a23SCy Schubert 				sizeof(ipaddr));
62*9da30a23SCy Schubert #ifdef USE_INET6
63*9da30a23SCy Schubert 			if (ipe.ipe_family == AF_INET) {
64*9da30a23SCy Schubert #endif
65*9da30a23SCy Schubert 				inet_ntop(ipe.ipe_family,
66*9da30a23SCy Schubert 					ipe.ipe_mask.vptr, mask,
67*9da30a23SCy Schubert 					sizeof(mask));
68*9da30a23SCy Schubert 				mask_msg[0]='/';
69*9da30a23SCy Schubert 				mask_msg[1]='\0';
70*9da30a23SCy Schubert 				strlcat(mask_msg, mask, sizeof(mask_msg));
71*9da30a23SCy Schubert #ifdef USE_INET6
72*9da30a23SCy Schubert 			} else {
73*9da30a23SCy Schubert 				mask_msg[0]='\0';
74*9da30a23SCy Schubert 			}
75*9da30a23SCy Schubert #endif
76*9da30a23SCy Schubert 
77*9da30a23SCy Schubert 			snprintf(msg, sizeof(msg), "%s node from lookup hash table(%s) node(%s%s)", what, name, ipaddr, mask_msg);
782582ae57SCy Schubert 			return (ipf_perror_fd(pool_fd(), iocfunc, msg));
7941edb306SCy Schubert 		}
802582ae57SCy Schubert 	return (0);
8141edb306SCy Schubert }
82