2 * Copyright (C) 2002 by Darren Reed.
4 * See the IPFILTER.LICENCE file for details on licencing.
6 * $Id: load_hash.c,v 1.11.2.2 2005/02/01 02:44:05 darrenr Exp $
8 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
9 * Use is subject to license terms.
11 * Copyright (c) 2014, Joyent, Inc. All rights reserved.
15 #include <sys/ioctl.h>
17 #include "netinet/ip_lookup.h"
18 #include "netinet/ip_htable.h"
21 static int hashfd
= -1;
24 int load_hash(iphp
, list
, iocfunc
)
35 if ((hashfd
== -1) && ((opts
& OPT_DONOTHING
) == 0))
36 hashfd
= open(IPLOOKUP_NAME
, O_RDWR
);
37 if ((hashfd
== -1) && ((opts
& OPT_DONOTHING
) == 0))
39 if (setzone(hashfd
) != 0) {
44 for (n
= 0, a
= list
; a
!= NULL
; a
= a
->ipe_next
)
48 op
.iplo_type
= IPLT_HASH
;
49 op
.iplo_unit
= iphp
->iph_unit
;
50 strncpy(op
.iplo_name
, iphp
->iph_name
, sizeof(op
.iplo_name
));
51 if (*op
.iplo_name
== '\0')
52 op
.iplo_arg
= IPHASH_ANON
;
53 op
.iplo_size
= sizeof(iph
);
54 op
.iplo_struct
= &iph
;
55 iph
.iph_unit
= iphp
->iph_unit
;
56 iph
.iph_type
= iphp
->iph_type
;
57 strncpy(iph
.iph_name
, iphp
->iph_name
, sizeof(iph
.iph_name
));
58 iph
.iph_flags
= iphp
->iph_flags
;
61 if (iphp
->iph_size
== 0)
64 size
= iphp
->iph_size
;
65 if ((list
== NULL
) && (size
== 1)) {
67 "WARNING: empty hash table %s, recommend setting %s\n",
68 iphp
->iph_name
, "size to match expected use");
71 iph
.iph_seed
= iphp
->iph_seed
;
75 if ((opts
& OPT_REMOVE
) == 0) {
76 if ((*iocfunc
)(hashfd
, SIOCLOOKUPADDTABLE
, &op
))
77 if ((opts
& OPT_DONOTHING
) == 0) {
78 perror("load_hash:SIOCLOOKUPADDTABLE");
83 strncpy(op
.iplo_name
, iph
.iph_name
, sizeof(op
.iplo_name
));
84 strncpy(iphp
->iph_name
, iph
.iph_name
, sizeof(op
.iplo_name
));
86 if (opts
& OPT_VERBOSE
) {
87 for (a
= list
; a
!= NULL
; a
= a
->ipe_next
) {
88 if (a
->ipe_family
== AF_INET
) {
89 a
->ipe_addr
.in4_addr
= ntohl(a
->ipe_addr
.in4_addr
);
90 a
->ipe_mask
.in4_addr
= ntohl(a
->ipe_mask
.in4_addr
);
93 iph
.iph_table
= calloc(size
, sizeof(*iph
.iph_table
));
94 if (iph
.iph_table
== NULL
) {
95 perror("calloc(size, sizeof(*iph.iph_table))");
98 iph
.iph_table
[0] = list
;
99 printhash(&iph
, bcopywrap
, iph
.iph_name
, opts
);
102 for (a
= list
; a
!= NULL
; a
= a
->ipe_next
) {
103 if (a
->ipe_family
== AF_INET
) {
104 a
->ipe_addr
.in4_addr
= htonl(a
->ipe_addr
.in4_addr
);
105 a
->ipe_mask
.in4_addr
= htonl(a
->ipe_mask
.in4_addr
);
110 if (opts
& OPT_DEBUG
)
111 printf("Hash %s:\n", iph
.iph_name
);
113 for (a
= list
; a
!= NULL
; a
= a
->ipe_next
)
114 load_hashnode(iphp
->iph_unit
, iph
.iph_name
, a
, iocfunc
);
116 if ((opts
& OPT_REMOVE
) != 0) {
117 if ((*iocfunc
)(hashfd
, SIOCLOOKUPDELTABLE
, &op
))
118 if ((opts
& OPT_DONOTHING
) == 0) {
119 perror("load_hash:SIOCLOOKUPDELTABLE");