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