1*91d03f50Smrg /* $NetBSD: load_hash.c,v 1.3 2019/10/05 23:32:20 mrg Exp $ */
2bc4097aaSchristos
3bc4097aaSchristos /*
4bc4097aaSchristos * Copyright (C) 2012 by Darren Reed.
5bc4097aaSchristos *
6bc4097aaSchristos * See the IPFILTER.LICENCE file for details on licencing.
7bc4097aaSchristos *
813885a66Sdarrenr * Id: load_hash.c,v 1.1.1.2 2012/07/22 13:44:39 darrenr Exp $
9bc4097aaSchristos */
10bc4097aaSchristos
11bc4097aaSchristos #include <fcntl.h>
12bc4097aaSchristos #include <sys/ioctl.h>
13bc4097aaSchristos #include "ipf.h"
14bc4097aaSchristos #include "netinet/ip_lookup.h"
15bc4097aaSchristos #include "netinet/ip_htable.h"
16bc4097aaSchristos
17bc4097aaSchristos
18bc4097aaSchristos int
load_hash(iphp,list,iocfunc)19bc4097aaSchristos load_hash(iphp, list, iocfunc)
20bc4097aaSchristos iphtable_t *iphp;
21bc4097aaSchristos iphtent_t *list;
22bc4097aaSchristos ioctlfunc_t iocfunc;
23bc4097aaSchristos {
24bc4097aaSchristos iplookupop_t op;
25bc4097aaSchristos iphtable_t iph;
26bc4097aaSchristos iphtent_t *a;
27bc4097aaSchristos size_t size;
28bc4097aaSchristos int n;
29bc4097aaSchristos
30bc4097aaSchristos if (pool_open() == -1)
31bc4097aaSchristos return -1;
32bc4097aaSchristos
33bc4097aaSchristos for (n = 0, a = list; a != NULL; a = a->ipe_next)
34bc4097aaSchristos n++;
35bc4097aaSchristos
36bc4097aaSchristos bzero((char *)&iph, sizeof(iph));
37bc4097aaSchristos op.iplo_arg = 0;
38bc4097aaSchristos op.iplo_type = IPLT_HASH;
39bc4097aaSchristos op.iplo_unit = iphp->iph_unit;
40bc4097aaSchristos strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name));
41bc4097aaSchristos if (*op.iplo_name == '\0')
42bc4097aaSchristos op.iplo_arg = IPHASH_ANON;
43bc4097aaSchristos op.iplo_size = sizeof(iph);
44bc4097aaSchristos op.iplo_struct = &iph;
45c9d5dc6cSdarrenr iph = *iphp;
46bc4097aaSchristos if (n <= 0)
47bc4097aaSchristos n = 1;
48bc4097aaSchristos if (iphp->iph_size == 0)
49bc4097aaSchristos size = n * 2 - 1;
50bc4097aaSchristos else
51bc4097aaSchristos size = iphp->iph_size;
52bc4097aaSchristos if ((list == NULL) && (size == 1)) {
53bc4097aaSchristos fprintf(stderr,
54bc4097aaSchristos "WARNING: empty hash table %s, recommend setting %s\n",
55bc4097aaSchristos iphp->iph_name, "size to match expected use");
56bc4097aaSchristos }
57bc4097aaSchristos iph.iph_size = size;
58bc4097aaSchristos iph.iph_table = NULL;
59bc4097aaSchristos iph.iph_list = NULL;
60bc4097aaSchristos iph.iph_ref = 0;
61bc4097aaSchristos
62bc4097aaSchristos if ((opts & OPT_REMOVE) == 0) {
63bc4097aaSchristos if (pool_ioctl(iocfunc, SIOCLOOKUPADDTABLE, &op))
64bc4097aaSchristos if ((opts & OPT_DONOTHING) == 0) {
65c9d5dc6cSdarrenr return ipf_perror_fd(pool_fd(), iocfunc,
66c9d5dc6cSdarrenr "add lookup hash table");
67bc4097aaSchristos }
68bc4097aaSchristos }
69bc4097aaSchristos
70*91d03f50Smrg strncpy(iph.iph_name, op.iplo_name, sizeof(iph.iph_name) - 1);
71*91d03f50Smrg strncpy(iphp->iph_name, op.iplo_name, sizeof(iphp->iph_name) - 1);
72bc4097aaSchristos
73bc4097aaSchristos if (opts & OPT_VERBOSE) {
74bc4097aaSchristos iph.iph_table = calloc(size, sizeof(*iph.iph_table));
75bc4097aaSchristos if (iph.iph_table == NULL) {
76bc4097aaSchristos perror("calloc(size, sizeof(*iph.iph_table))");
77bc4097aaSchristos return -1;
78bc4097aaSchristos }
79bc4097aaSchristos iph.iph_list = list;
80bc4097aaSchristos printhash(&iph, bcopywrap, iph.iph_name, opts, NULL);
81bc4097aaSchristos free(iph.iph_table);
82bc4097aaSchristos
83bc4097aaSchristos for (a = list; a != NULL; a = a->ipe_next) {
84bc4097aaSchristos a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr);
85bc4097aaSchristos a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr);
86bc4097aaSchristos }
87bc4097aaSchristos }
88bc4097aaSchristos
89bc4097aaSchristos if (opts & OPT_DEBUG)
90bc4097aaSchristos printf("Hash %s:\n", iph.iph_name);
91bc4097aaSchristos
92bc4097aaSchristos for (a = list; a != NULL; a = a->ipe_next)
93bc4097aaSchristos load_hashnode(iphp->iph_unit, iph.iph_name, a, 0, iocfunc);
94bc4097aaSchristos
95bc4097aaSchristos if ((opts & OPT_REMOVE) != 0) {
96bc4097aaSchristos if (pool_ioctl(iocfunc, SIOCLOOKUPDELTABLE, &op))
97bc4097aaSchristos if ((opts & OPT_DONOTHING) == 0) {
98c9d5dc6cSdarrenr return ipf_perror_fd(pool_fd(), iocfunc,
99c9d5dc6cSdarrenr "delete lookup hash table");
100bc4097aaSchristos }
101bc4097aaSchristos }
102bc4097aaSchristos return 0;
103bc4097aaSchristos }
104