xref: /onnv-gate/usr/src/cmd/ipf/lib/common/load_hash.c (revision 2393:76e0289ce525)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * Copyright (C) 2002 by Darren Reed.
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * See the IPFILTER.LICENCE file for details on licencing.
50Sstevel@tonic-gate  *
6*2393Syz155240  * $Id: load_hash.c,v 1.11.2.2 2005/02/01 02:44:05 darrenr Exp $
7637Sml37995  *
8*2393Syz155240  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
9637Sml37995  * Use is subject to license terms.
100Sstevel@tonic-gate  */
110Sstevel@tonic-gate 
12637Sml37995 #pragma ident	"%Z%%M%	%I%	%E% SMI"
13637Sml37995 
140Sstevel@tonic-gate #include <fcntl.h>
150Sstevel@tonic-gate #include <sys/ioctl.h>
160Sstevel@tonic-gate #include "ipf.h"
170Sstevel@tonic-gate #include "netinet/ip_lookup.h"
180Sstevel@tonic-gate #include "netinet/ip_htable.h"
190Sstevel@tonic-gate 
200Sstevel@tonic-gate static int hashfd = -1;
210Sstevel@tonic-gate 
220Sstevel@tonic-gate 
load_hash(iphp,list,iocfunc)230Sstevel@tonic-gate int load_hash(iphp, list, iocfunc)
240Sstevel@tonic-gate iphtable_t *iphp;
250Sstevel@tonic-gate iphtent_t *list;
260Sstevel@tonic-gate ioctlfunc_t iocfunc;
270Sstevel@tonic-gate {
280Sstevel@tonic-gate 	iplookupop_t op;
290Sstevel@tonic-gate 	iphtable_t iph;
300Sstevel@tonic-gate 	iphtent_t *a;
310Sstevel@tonic-gate 	size_t size;
320Sstevel@tonic-gate 	int n;
330Sstevel@tonic-gate 
340Sstevel@tonic-gate 	if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
350Sstevel@tonic-gate 		hashfd = open(IPLOOKUP_NAME, O_RDWR);
360Sstevel@tonic-gate 	if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
370Sstevel@tonic-gate 		return -1;
380Sstevel@tonic-gate 
390Sstevel@tonic-gate 	for (n = 0, a = list; a != NULL; a = a->ipe_next)
400Sstevel@tonic-gate 		n++;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 	op.iplo_arg = 0;
430Sstevel@tonic-gate 	op.iplo_type = IPLT_HASH;
440Sstevel@tonic-gate 	op.iplo_unit = iphp->iph_unit;
450Sstevel@tonic-gate 	strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name));
460Sstevel@tonic-gate 	if (*op.iplo_name == '\0')
470Sstevel@tonic-gate 		op.iplo_arg = IPHASH_ANON;
480Sstevel@tonic-gate 	op.iplo_size = sizeof(iph);
490Sstevel@tonic-gate 	op.iplo_struct = &iph;
500Sstevel@tonic-gate 	iph.iph_unit = iphp->iph_unit;
510Sstevel@tonic-gate 	iph.iph_type = iphp->iph_type;
520Sstevel@tonic-gate 	strncpy(iph.iph_name, iphp->iph_name, sizeof(iph.iph_name));
530Sstevel@tonic-gate 	iph.iph_flags = iphp->iph_flags;
54*2393Syz155240 	if (n <= 0)
55*2393Syz155240 		n = 1;
560Sstevel@tonic-gate 	if (iphp->iph_size == 0)
570Sstevel@tonic-gate 		size = n * 2 - 1;
580Sstevel@tonic-gate 	else
590Sstevel@tonic-gate 		size = iphp->iph_size;
60*2393Syz155240 	if ((list == NULL) && (size == 1)) {
61*2393Syz155240 		fprintf(stderr,
62*2393Syz155240 			"WARNING: empty hash table %s, recommend setting %s\n",
63*2393Syz155240 			iphp->iph_name, "size to match expected use");
64*2393Syz155240 	}
650Sstevel@tonic-gate 	iph.iph_size = size;
660Sstevel@tonic-gate 	iph.iph_seed = iphp->iph_seed;
670Sstevel@tonic-gate 	iph.iph_table = NULL;
680Sstevel@tonic-gate 	iph.iph_ref = 0;
690Sstevel@tonic-gate 
70*2393Syz155240 	if ((opts & OPT_REMOVE) == 0) {
71*2393Syz155240 		if ((*iocfunc)(hashfd, SIOCLOOKUPADDTABLE, &op))
72*2393Syz155240 			if ((opts & OPT_DONOTHING) == 0) {
73*2393Syz155240 				perror("load_hash:SIOCLOOKUPADDTABLE");
74*2393Syz155240 				return -1;
75*2393Syz155240 			}
76*2393Syz155240 	}
770Sstevel@tonic-gate 
780Sstevel@tonic-gate 	strncpy(op.iplo_name, iph.iph_name, sizeof(op.iplo_name));
790Sstevel@tonic-gate 	strncpy(iphp->iph_name, iph.iph_name, sizeof(op.iplo_name));
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (opts & OPT_VERBOSE) {
820Sstevel@tonic-gate 		for (a = list; a != NULL; a = a->ipe_next) {
83637Sml37995 			if (a->ipe_family == AF_INET) {
84637Sml37995 				a->ipe_addr.in4_addr = ntohl(a->ipe_addr.in4_addr);
85637Sml37995 				a->ipe_mask.in4_addr = ntohl(a->ipe_mask.in4_addr);
86637Sml37995 			}
870Sstevel@tonic-gate 		}
880Sstevel@tonic-gate 		iph.iph_table = calloc(size, sizeof(*iph.iph_table));
890Sstevel@tonic-gate 		if (iph.iph_table == NULL) {
900Sstevel@tonic-gate 			perror("calloc(size, sizeof(*iph.iph_table))");
910Sstevel@tonic-gate 			return -1;
920Sstevel@tonic-gate 		}
930Sstevel@tonic-gate 		iph.iph_table[0] = list;
94*2393Syz155240 		printhash(&iph, bcopywrap, iph.iph_name, opts);
950Sstevel@tonic-gate 		free(iph.iph_table);
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 		for (a = list; a != NULL; a = a->ipe_next) {
98637Sml37995 			if (a->ipe_family == AF_INET) {
99637Sml37995 				a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr);
100637Sml37995 				a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr);
101637Sml37995 			}
1020Sstevel@tonic-gate 		}
1030Sstevel@tonic-gate 	}
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	if (opts & OPT_DEBUG)
1060Sstevel@tonic-gate 		printf("Hash %s:\n", iph.iph_name);
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	for (a = list; a != NULL; a = a->ipe_next)
1090Sstevel@tonic-gate 		load_hashnode(iphp->iph_unit, iph.iph_name, a, iocfunc);
1100Sstevel@tonic-gate 
111*2393Syz155240 	if ((opts & OPT_REMOVE) != 0) {
112*2393Syz155240 		if ((*iocfunc)(hashfd, SIOCLOOKUPDELTABLE, &op))
113*2393Syz155240 			if ((opts & OPT_DONOTHING) == 0) {
114*2393Syz155240 				perror("load_hash:SIOCLOOKUPDELTABLE");
115*2393Syz155240 				return -1;
116*2393Syz155240 			}
117*2393Syz155240 	}
1180Sstevel@tonic-gate 	return 0;
1190Sstevel@tonic-gate }
120