xref: /freebsd-src/sbin/ipf/libipf/load_hash.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
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_hash(iphtable_t * iphp,iphtent_t * list,ioctlfunc_t iocfunc)18efeb8bffSCy Schubert load_hash(iphtable_t *iphp, iphtent_t *list, ioctlfunc_t iocfunc)
1941edb306SCy Schubert {
2041edb306SCy Schubert 	iplookupop_t op;
2141edb306SCy Schubert 	iphtable_t iph;
2241edb306SCy Schubert 	iphtent_t *a;
2341edb306SCy Schubert 	size_t size;
2441edb306SCy Schubert 	int n;
2541edb306SCy Schubert 
2641edb306SCy Schubert 	if (pool_open() == -1)
27*2582ae57SCy Schubert 		return (-1);
2841edb306SCy Schubert 
2941edb306SCy Schubert 	for (n = 0, a = list; a != NULL; a = a->ipe_next)
3041edb306SCy Schubert 		n++;
3141edb306SCy Schubert 
3241edb306SCy Schubert 	bzero((char *)&iph, sizeof(iph));
3341edb306SCy Schubert 	op.iplo_arg = 0;
3441edb306SCy Schubert 	op.iplo_type = IPLT_HASH;
3541edb306SCy Schubert 	op.iplo_unit = iphp->iph_unit;
3641edb306SCy Schubert 	strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name));
3741edb306SCy Schubert 	if (*op.iplo_name == '\0')
3841edb306SCy Schubert 		op.iplo_arg = IPHASH_ANON;
3941edb306SCy Schubert 	op.iplo_size = sizeof(iph);
4041edb306SCy Schubert 	op.iplo_struct = &iph;
4141edb306SCy Schubert 	iph = *iphp;
4241edb306SCy Schubert 	if (n <= 0)
4341edb306SCy Schubert 		n = 1;
4441edb306SCy Schubert 	if (iphp->iph_size == 0)
4541edb306SCy Schubert 		size = n * 2 - 1;
4641edb306SCy Schubert 	else
4741edb306SCy Schubert 		size = iphp->iph_size;
4841edb306SCy Schubert 	if ((list == NULL) && (size == 1)) {
4941edb306SCy Schubert 		fprintf(stderr,
5041edb306SCy Schubert 			"WARNING: empty hash table %s, recommend setting %s\n",
5141edb306SCy Schubert 			iphp->iph_name, "size to match expected use");
5241edb306SCy Schubert 	}
5341edb306SCy Schubert 	iph.iph_size = size;
5441edb306SCy Schubert 	iph.iph_table = NULL;
5541edb306SCy Schubert 	iph.iph_list = NULL;
5641edb306SCy Schubert 	iph.iph_ref = 0;
5741edb306SCy Schubert 
5841edb306SCy Schubert 	if ((opts & OPT_REMOVE) == 0) {
5941edb306SCy Schubert 		if (pool_ioctl(iocfunc, SIOCLOOKUPADDTABLE, &op))
6041edb306SCy Schubert 			if ((opts & OPT_DONOTHING) == 0) {
61*2582ae57SCy Schubert 				return (ipf_perror_fd(pool_fd(), iocfunc,
62*2582ae57SCy Schubert 					"add lookup hash table"));
6341edb306SCy Schubert 			}
6441edb306SCy Schubert 	}
6541edb306SCy Schubert 
6641edb306SCy Schubert 	strncpy(iph.iph_name, op.iplo_name, sizeof(op.iplo_name));
6741edb306SCy Schubert 	strncpy(iphp->iph_name, op.iplo_name, sizeof(op.iplo_name));
6841edb306SCy Schubert 
6941edb306SCy Schubert 	if (opts & OPT_VERBOSE) {
7041edb306SCy Schubert 		iph.iph_table = calloc(size, sizeof(*iph.iph_table));
7141edb306SCy Schubert 		if (iph.iph_table == NULL) {
7241edb306SCy Schubert 			perror("calloc(size, sizeof(*iph.iph_table))");
73*2582ae57SCy Schubert 			return (-1);
7441edb306SCy Schubert 		}
7541edb306SCy Schubert 		iph.iph_list = list;
7641edb306SCy Schubert 		printhash(&iph, bcopywrap, iph.iph_name, opts, NULL);
7741edb306SCy Schubert 		free(iph.iph_table);
7841edb306SCy Schubert 
7941edb306SCy Schubert 		for (a = list; a != NULL; a = a->ipe_next) {
8041edb306SCy Schubert 			a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr);
8141edb306SCy Schubert 			a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr);
8241edb306SCy Schubert 		}
8341edb306SCy Schubert 	}
8441edb306SCy Schubert 
8541edb306SCy Schubert 	if (opts & OPT_DEBUG)
8641edb306SCy Schubert 		printf("Hash %s:\n", iph.iph_name);
8741edb306SCy Schubert 
8841edb306SCy Schubert 	for (a = list; a != NULL; a = a->ipe_next)
8941edb306SCy Schubert 		load_hashnode(iphp->iph_unit, iph.iph_name, a, 0, iocfunc);
9041edb306SCy Schubert 
9141edb306SCy Schubert 	if ((opts & OPT_REMOVE) != 0) {
9241edb306SCy Schubert 		if (pool_ioctl(iocfunc, SIOCLOOKUPDELTABLE, &op))
9341edb306SCy Schubert 			if ((opts & OPT_DONOTHING) == 0) {
94*2582ae57SCy Schubert 				return (ipf_perror_fd(pool_fd(), iocfunc,
95*2582ae57SCy Schubert 					"delete lookup hash table"));
9641edb306SCy Schubert 			}
9741edb306SCy Schubert 	}
98*2582ae57SCy Schubert 	return (0);
9941edb306SCy Schubert }
100