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.11.2.2 2005/02/01 02:44:05 darrenr Exp $
7 *
8 * Copyright 2006 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#include "netinet/ip_lookup.h"
18#include "netinet/ip_htable.h"
19
20static int hashfd = -1;
21
22
23int load_hash(iphp, list, iocfunc)
24iphtable_t *iphp;
25iphtent_t *list;
26ioctlfunc_t iocfunc;
27{
28	iplookupop_t op;
29	iphtable_t iph;
30	iphtent_t *a;
31	size_t size;
32	int n;
33
34	if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
35		hashfd = open(IPLOOKUP_NAME, O_RDWR);
36	if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0))
37		return -1;
38
39	for (n = 0, a = list; a != NULL; a = a->ipe_next)
40		n++;
41
42	op.iplo_arg = 0;
43	op.iplo_type = IPLT_HASH;
44	op.iplo_unit = iphp->iph_unit;
45	strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name));
46	if (*op.iplo_name == '\0')
47		op.iplo_arg = IPHASH_ANON;
48	op.iplo_size = sizeof(iph);
49	op.iplo_struct = &iph;
50	iph.iph_unit = iphp->iph_unit;
51	iph.iph_type = iphp->iph_type;
52	strncpy(iph.iph_name, iphp->iph_name, sizeof(iph.iph_name));
53	iph.iph_flags = iphp->iph_flags;
54	if (n <= 0)
55		n = 1;
56	if (iphp->iph_size == 0)
57		size = n * 2 - 1;
58	else
59		size = iphp->iph_size;
60	if ((list == NULL) && (size == 1)) {
61		fprintf(stderr,
62			"WARNING: empty hash table %s, recommend setting %s\n",
63			iphp->iph_name, "size to match expected use");
64	}
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 ((opts & OPT_REMOVE) == 0) {
71		if ((*iocfunc)(hashfd, SIOCLOOKUPADDTABLE, &op))
72			if ((opts & OPT_DONOTHING) == 0) {
73				perror("load_hash:SIOCLOOKUPADDTABLE");
74				return -1;
75			}
76	}
77
78	strncpy(op.iplo_name, iph.iph_name, sizeof(op.iplo_name));
79	strncpy(iphp->iph_name, iph.iph_name, sizeof(op.iplo_name));
80
81	if (opts & OPT_VERBOSE) {
82		for (a = list; a != NULL; a = a->ipe_next) {
83			if (a->ipe_family == AF_INET) {
84				a->ipe_addr.in4_addr = ntohl(a->ipe_addr.in4_addr);
85				a->ipe_mask.in4_addr = ntohl(a->ipe_mask.in4_addr);
86			}
87		}
88		iph.iph_table = calloc(size, sizeof(*iph.iph_table));
89		if (iph.iph_table == NULL) {
90			perror("calloc(size, sizeof(*iph.iph_table))");
91			return -1;
92		}
93		iph.iph_table[0] = list;
94		printhash(&iph, bcopywrap, iph.iph_name, opts);
95		free(iph.iph_table);
96
97		for (a = list; a != NULL; a = a->ipe_next) {
98			if (a->ipe_family == AF_INET) {
99				a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr);
100				a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr);
101			}
102		}
103	}
104
105	if (opts & OPT_DEBUG)
106		printf("Hash %s:\n", iph.iph_name);
107
108	for (a = list; a != NULL; a = a->ipe_next)
109		load_hashnode(iphp->iph_unit, iph.iph_name, a, iocfunc);
110
111	if ((opts & OPT_REMOVE) != 0) {
112		if ((*iocfunc)(hashfd, SIOCLOOKUPDELTABLE, &op))
113			if ((opts & OPT_DONOTHING) == 0) {
114				perror("load_hash:SIOCLOOKUPDELTABLE");
115				return -1;
116			}
117	}
118	return 0;
119}
120