xref: /netbsd/external/bsd/ipf/dist/lib/load_hash.c (revision ec8120b1)
1*ec8120b1Smrg /*	$NetBSD: load_hash.c,v 1.3 2019/10/05 23:32:20 mrg Exp $	*/
226945a25Schristos 
326945a25Schristos /*
426945a25Schristos  * Copyright (C) 2012 by Darren Reed.
526945a25Schristos  *
626945a25Schristos  * See the IPFILTER.LICENCE file for details on licencing.
726945a25Schristos  *
8f52ace7aSdarrenr  * Id: load_hash.c,v 1.1.1.2 2012/07/22 13:44:39 darrenr Exp $
926945a25Schristos  */
1026945a25Schristos 
1126945a25Schristos #include <fcntl.h>
1226945a25Schristos #include <sys/ioctl.h>
1326945a25Schristos #include "ipf.h"
1426945a25Schristos #include "netinet/ip_lookup.h"
1526945a25Schristos #include "netinet/ip_htable.h"
1626945a25Schristos 
1726945a25Schristos 
1826945a25Schristos int
load_hash(iphp,list,iocfunc)1926945a25Schristos load_hash(iphp, list, iocfunc)
2026945a25Schristos 	iphtable_t *iphp;
2126945a25Schristos 	iphtent_t *list;
2226945a25Schristos 	ioctlfunc_t iocfunc;
2326945a25Schristos {
2426945a25Schristos 	iplookupop_t op;
2526945a25Schristos 	iphtable_t iph;
2626945a25Schristos 	iphtent_t *a;
2726945a25Schristos 	size_t size;
2826945a25Schristos 	int n;
2926945a25Schristos 
3026945a25Schristos 	if (pool_open() == -1)
3126945a25Schristos 		return -1;
3226945a25Schristos 
3326945a25Schristos 	for (n = 0, a = list; a != NULL; a = a->ipe_next)
3426945a25Schristos 		n++;
3526945a25Schristos 
3626945a25Schristos 	bzero((char *)&iph, sizeof(iph));
3726945a25Schristos 	op.iplo_arg = 0;
3826945a25Schristos 	op.iplo_type = IPLT_HASH;
3926945a25Schristos 	op.iplo_unit = iphp->iph_unit;
4026945a25Schristos 	strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name));
4126945a25Schristos 	if (*op.iplo_name == '\0')
4226945a25Schristos 		op.iplo_arg = IPHASH_ANON;
4326945a25Schristos 	op.iplo_size = sizeof(iph);
4426945a25Schristos 	op.iplo_struct = &iph;
45c50c2f6fSdarrenr 	iph = *iphp;
4626945a25Schristos 	if (n <= 0)
4726945a25Schristos 		n = 1;
4826945a25Schristos 	if (iphp->iph_size == 0)
4926945a25Schristos 		size = n * 2 - 1;
5026945a25Schristos 	else
5126945a25Schristos 		size = iphp->iph_size;
5226945a25Schristos 	if ((list == NULL) && (size == 1)) {
5326945a25Schristos 		fprintf(stderr,
5426945a25Schristos 			"WARNING: empty hash table %s, recommend setting %s\n",
5526945a25Schristos 			iphp->iph_name, "size to match expected use");
5626945a25Schristos 	}
5726945a25Schristos 	iph.iph_size = size;
5826945a25Schristos 	iph.iph_table = NULL;
5926945a25Schristos 	iph.iph_list = NULL;
6026945a25Schristos 	iph.iph_ref = 0;
6126945a25Schristos 
6226945a25Schristos 	if ((opts & OPT_REMOVE) == 0) {
6326945a25Schristos 		if (pool_ioctl(iocfunc, SIOCLOOKUPADDTABLE, &op))
6426945a25Schristos 			if ((opts & OPT_DONOTHING) == 0) {
65c50c2f6fSdarrenr 				return ipf_perror_fd(pool_fd(), iocfunc,
66c50c2f6fSdarrenr 					"add lookup hash table");
6726945a25Schristos 			}
6826945a25Schristos 	}
6926945a25Schristos 
70*ec8120b1Smrg 	strncpy(iph.iph_name, op.iplo_name, sizeof(iph.iph_name) - 1);
71*ec8120b1Smrg 	strncpy(iphp->iph_name, op.iplo_name, sizeof(iphp->iph_name) - 1);
7226945a25Schristos 
7326945a25Schristos 	if (opts & OPT_VERBOSE) {
7426945a25Schristos 		iph.iph_table = calloc(size, sizeof(*iph.iph_table));
7526945a25Schristos 		if (iph.iph_table == NULL) {
7626945a25Schristos 			perror("calloc(size, sizeof(*iph.iph_table))");
7726945a25Schristos 			return -1;
7826945a25Schristos 		}
7926945a25Schristos 		iph.iph_list = list;
8026945a25Schristos 		printhash(&iph, bcopywrap, iph.iph_name, opts, NULL);
8126945a25Schristos 		free(iph.iph_table);
8226945a25Schristos 
8326945a25Schristos 		for (a = list; a != NULL; a = a->ipe_next) {
8426945a25Schristos 			a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr);
8526945a25Schristos 			a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr);
8626945a25Schristos 		}
8726945a25Schristos 	}
8826945a25Schristos 
8926945a25Schristos 	if (opts & OPT_DEBUG)
9026945a25Schristos 		printf("Hash %s:\n", iph.iph_name);
9126945a25Schristos 
9226945a25Schristos 	for (a = list; a != NULL; a = a->ipe_next)
9326945a25Schristos 		load_hashnode(iphp->iph_unit, iph.iph_name, a, 0, iocfunc);
9426945a25Schristos 
9526945a25Schristos 	if ((opts & OPT_REMOVE) != 0) {
9626945a25Schristos 		if (pool_ioctl(iocfunc, SIOCLOOKUPDELTABLE, &op))
9726945a25Schristos 			if ((opts & OPT_DONOTHING) == 0) {
98c50c2f6fSdarrenr 				return ipf_perror_fd(pool_fd(), iocfunc,
99c50c2f6fSdarrenr 					"delete lookup hash table");
10026945a25Schristos 			}
10126945a25Schristos 	}
10226945a25Schristos 	return 0;
10326945a25Schristos }
104