1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * Copyright (C) 2002 by Darren Reed.
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
5*0Sstevel@tonic-gate  *
6*0Sstevel@tonic-gate  * $Id: load_hash.c,v 1.10 2003/04/26 04:55:11 darrenr Exp $
7*0Sstevel@tonic-gate  */
8*0Sstevel@tonic-gate 
9*0Sstevel@tonic-gate #include <fcntl.h>
10*0Sstevel@tonic-gate #include <sys/ioctl.h>
11*0Sstevel@tonic-gate #include "ipf.h"
12*0Sstevel@tonic-gate #if SOLARIS2 >= 10
13*0Sstevel@tonic-gate #include "ip_lookup.h"
14*0Sstevel@tonic-gate #include "ip_htable.h"
15*0Sstevel@tonic-gate #else
16*0Sstevel@tonic-gate #include "netinet/ip_lookup.h"
17*0Sstevel@tonic-gate #include "netinet/ip_htable.h"
18*0Sstevel@tonic-gate #endif
19*0Sstevel@tonic-gate 
20*0Sstevel@tonic-gate static int hashfd = -1;
21*0Sstevel@tonic-gate 
22*0Sstevel@tonic-gate 
23*0Sstevel@tonic-gate int load_hash(iphp, list, iocfunc)
24*0Sstevel@tonic-gate iphtable_t *iphp;
25*0Sstevel@tonic-gate iphtent_t *list;
26*0Sstevel@tonic-gate ioctlfunc_t iocfunc;
27*0Sstevel@tonic-gate {
28*0Sstevel@tonic-gate 	iplookupop_t op;
29*0Sstevel@tonic-gate 	iphtable_t iph;
30*0Sstevel@tonic-gate 	iphtent_t *a;
31*0Sstevel@tonic-gate 	size_t size;
32*0Sstevel@tonic-gate 	int n;
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate 	if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
35*0Sstevel@tonic-gate 		hashfd = open(IPLOOKUP_NAME, O_RDWR);
36*0Sstevel@tonic-gate 	if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
37*0Sstevel@tonic-gate 		return -1;
38*0Sstevel@tonic-gate 	if (list == NULL)
39*0Sstevel@tonic-gate 		return 0;
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate 	for (n = 0, a = list; a != NULL; a = a->ipe_next)
42*0Sstevel@tonic-gate 		n++;
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate 	op.iplo_arg = 0;
45*0Sstevel@tonic-gate 	op.iplo_type = IPLT_HASH;
46*0Sstevel@tonic-gate 	op.iplo_unit = iphp->iph_unit;
47*0Sstevel@tonic-gate 	strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name));
48*0Sstevel@tonic-gate 	if (*op.iplo_name == '\0')
49*0Sstevel@tonic-gate 		op.iplo_arg = IPHASH_ANON;
50*0Sstevel@tonic-gate 	op.iplo_size = sizeof(iph);
51*0Sstevel@tonic-gate 	op.iplo_struct = &iph;
52*0Sstevel@tonic-gate 	iph.iph_unit = iphp->iph_unit;
53*0Sstevel@tonic-gate 	iph.iph_type = iphp->iph_type;
54*0Sstevel@tonic-gate 	strncpy(iph.iph_name, iphp->iph_name, sizeof(iph.iph_name));
55*0Sstevel@tonic-gate 	iph.iph_flags = iphp->iph_flags;
56*0Sstevel@tonic-gate 	if (iphp->iph_size == 0)
57*0Sstevel@tonic-gate 		size = n * 2 - 1;
58*0Sstevel@tonic-gate 	else
59*0Sstevel@tonic-gate 		size = iphp->iph_size;
60*0Sstevel@tonic-gate 	iph.iph_size = size;
61*0Sstevel@tonic-gate 	iph.iph_seed = iphp->iph_seed;
62*0Sstevel@tonic-gate 	iph.iph_table = NULL;
63*0Sstevel@tonic-gate 	iph.iph_ref = 0;
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate 	if ((*iocfunc)(hashfd, SIOCLOOKUPADDTABLE, &op))
66*0Sstevel@tonic-gate 		if ((opts & OPT_DONOTHING) == 0) {
67*0Sstevel@tonic-gate 			perror("load_hash:SIOCLOOKUPADDTABLE");
68*0Sstevel@tonic-gate 			return -1;
69*0Sstevel@tonic-gate 		}
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 	strncpy(op.iplo_name, iph.iph_name, sizeof(op.iplo_name));
72*0Sstevel@tonic-gate 	strncpy(iphp->iph_name, iph.iph_name, sizeof(op.iplo_name));
73*0Sstevel@tonic-gate 
74*0Sstevel@tonic-gate 	if (opts & OPT_VERBOSE) {
75*0Sstevel@tonic-gate 		for (a = list; a != NULL; a = a->ipe_next) {
76*0Sstevel@tonic-gate 			a->ipe_addr.in4_addr = ntohl(a->ipe_addr.in4_addr);
77*0Sstevel@tonic-gate 			a->ipe_mask.in4_addr = ntohl(a->ipe_mask.in4_addr);
78*0Sstevel@tonic-gate 		}
79*0Sstevel@tonic-gate 		iph.iph_table = calloc(size, sizeof(*iph.iph_table));
80*0Sstevel@tonic-gate 		if (iph.iph_table == NULL) {
81*0Sstevel@tonic-gate 			perror("calloc(size, sizeof(*iph.iph_table))");
82*0Sstevel@tonic-gate 			return -1;
83*0Sstevel@tonic-gate 		}
84*0Sstevel@tonic-gate 		iph.iph_table[0] = list;
85*0Sstevel@tonic-gate 		printhash(&iph, bcopywrap, opts);
86*0Sstevel@tonic-gate 		free(iph.iph_table);
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate 		for (a = list; a != NULL; a = a->ipe_next) {
89*0Sstevel@tonic-gate 			a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr);
90*0Sstevel@tonic-gate 			a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr);
91*0Sstevel@tonic-gate 		}
92*0Sstevel@tonic-gate 	}
93*0Sstevel@tonic-gate 
94*0Sstevel@tonic-gate 	if (opts & OPT_DEBUG)
95*0Sstevel@tonic-gate 		printf("Hash %s:\n", iph.iph_name);
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	for (a = list; a != NULL; a = a->ipe_next)
98*0Sstevel@tonic-gate 		load_hashnode(iphp->iph_unit, iph.iph_name, a, iocfunc);
99*0Sstevel@tonic-gate 
100*0Sstevel@tonic-gate 	return 0;
101*0Sstevel@tonic-gate }
102