4 * Copyright (C) 1993-2001, 2003 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
8 #if defined(KERNEL) || defined(_KERNEL)
14 #include <sys/param.h>
15 #include <sys/types.h>
16 #include <sys/errno.h>
29 #include <sys/socket.h>
30 #if defined(__FreeBSD_version) && (__FreeBSD_version >= 300000)
31 # include <sys/malloc.h>
33 #if defined(__FreeBSD__)
34 # include <sys/cdefs.h>
35 # include <sys/proc.h>
37 #if !defined(__svr4__) && !defined(__SVR4) && !defined(__hpux) && \
39 # include <sys/mbuf.h>
42 # include <sys/systm.h>
46 #include <netinet/in.h>
49 #include "netinet/ip_compat.h"
50 #include "netinet/ip_fil.h"
51 #include "netinet/ip_lookup.h"
52 #include "netinet/ip_htable.h"
56 static const char rcsid[] = "@(#)$Id: ip_htable.c,v 2.34.2.11 2007/09/20 12:51:51 darrenr Exp $";
59 #ifdef IPFILTER_LOOKUP
60 static iphtent_t *fr_iphmfind __P((iphtable_t *, struct in_addr *));
61 static u_long ipht_nomem[IPL_LOGSIZE] = { 0, 0, 0, 0, 0, 0, 0, 0 };
62 static u_long ipf_nhtables[IPL_LOGSIZE] = { 0, 0, 0, 0, 0, 0, 0, 0 };
63 static u_long ipf_nhtnodes[IPL_LOGSIZE] = { 0, 0, 0, 0, 0, 0, 0, 0 };
65 iphtable_t *ipf_htables[IPL_LOGSIZE] = { NULL, NULL, NULL, NULL,
66 NULL, NULL, NULL, NULL };
69 void fr_htable_unload()
73 fop.iplf_unit = IPL_LOGALL;
74 (void)fr_flushhtable(&fop);
78 int fr_gethtablestat(op)
83 if (op->iplo_size != sizeof(stats))
86 stats.iphs_tables = ipf_htables[op->iplo_unit];
87 stats.iphs_numtables = ipf_nhtables[op->iplo_unit];
88 stats.iphs_numnodes = ipf_nhtnodes[op->iplo_unit];
89 stats.iphs_nomem = ipht_nomem[op->iplo_unit];
91 return COPYOUT(&stats, op->iplo_struct, sizeof(stats));
97 * Create a new hash table using the template passed.
102 iphtable_t *iph, *oiph;
103 char name[FR_GROUPLEN];
106 unit = op->iplo_unit;
107 if ((op->iplo_arg & IPHASH_ANON) == 0) {
108 iph = fr_existshtable(unit, op->iplo_name);
110 if ((iph->iph_flags & IPHASH_DELETE) == 0)
112 iph->iph_flags &= ~IPHASH_DELETE;
117 KMALLOC(iph, iphtable_t *);
119 ipht_nomem[op->iplo_unit]++;
122 err = COPYIN(op->iplo_struct, iph, sizeof(*iph));
128 if (iph->iph_unit != unit) {
133 if ((op->iplo_arg & IPHASH_ANON) != 0) {
137 #if defined(SNPRINTF) && defined(_KERNEL)
138 SNPRINTF(name, sizeof(name), "%u", i);
140 (void)sprintf(name, "%u", i);
142 for (oiph = ipf_htables[unit]; oiph != NULL;
143 oiph = oiph->iph_next)
144 if (strncmp(oiph->iph_name, name,
145 sizeof(oiph->iph_name)) == 0)
147 } while (oiph != NULL);
149 (void)strncpy(iph->iph_name, name, sizeof(iph->iph_name));
150 (void)strncpy(op->iplo_name, name, sizeof(op->iplo_name));
151 iph->iph_type |= IPHASH_ANON;
154 KMALLOCS(iph->iph_table, iphtent_t **,
155 iph->iph_size * sizeof(*iph->iph_table));
156 if (iph->iph_table == NULL) {
162 bzero((char *)iph->iph_table, iph->iph_size * sizeof(*iph->iph_table));
164 iph->iph_list = NULL;
167 iph->iph_next = ipf_htables[unit];
168 iph->iph_pnext = &ipf_htables[unit];
169 if (ipf_htables[unit] != NULL)
170 ipf_htables[unit]->iph_pnext = &iph->iph_next;
171 ipf_htables[unit] = iph;
172 ipf_nhtables[unit]++;
180 int fr_removehtable(unit, name)
186 iph = fr_findhtable(unit, name);
190 if (iph->iph_unit != unit) {
194 if (iph->iph_ref != 0) {
195 (void) fr_clearhtable(iph);
196 iph->iph_flags |= IPHASH_DELETE;
206 int fr_clearhtable(iph)
211 while ((ipe = iph->iph_list) != NULL)
212 if (fr_delhtent(iph, ipe) != 0)
218 int fr_delhtable(iph)
222 if (fr_clearhtable(iph) != 0)
225 if (iph->iph_pnext != NULL)
226 *iph->iph_pnext = iph->iph_next;
227 if (iph->iph_next != NULL)
228 iph->iph_next->iph_pnext = iph->iph_pnext;
230 ipf_nhtables[iph->iph_unit]--;
232 return fr_derefhtable(iph);
237 * Delete an entry from a hash table.
239 int fr_delhtent(iph, ipe)
244 if (ipe->ipe_phnext != NULL)
245 *ipe->ipe_phnext = ipe->ipe_hnext;
246 if (ipe->ipe_hnext != NULL)
247 ipe->ipe_hnext->ipe_phnext = ipe->ipe_phnext;
249 if (ipe->ipe_pnext != NULL)
250 *ipe->ipe_pnext = ipe->ipe_next;
251 if (ipe->ipe_next != NULL)
252 ipe->ipe_next->ipe_pnext = ipe->ipe_pnext;
254 switch (iph->iph_type & ~IPHASH_ANON)
256 case IPHASH_GROUPMAP :
257 if (ipe->ipe_group != NULL)
258 fr_delgroup(ipe->ipe_group, IPL_LOGIPF, fr_active);
267 return fr_derefhtent(ipe);
271 int fr_derefhtable(iph)
279 if (iph->iph_ref == 0) {
280 KFREES(iph->iph_table, iph->iph_size * sizeof(*iph->iph_table));
288 int fr_derefhtent(ipe)
293 if (ipe->ipe_ref == 0) {
294 ipf_nhtnodes[ipe->ipe_unit]--;
305 iphtable_t *fr_existshtable(unit, name)
311 for (iph = ipf_htables[unit]; iph != NULL; iph = iph->iph_next)
312 if (strncmp(iph->iph_name, name, sizeof(iph->iph_name)) == 0)
318 iphtable_t *fr_findhtable(unit, name)
324 iph = fr_existshtable(unit, name);
325 if ((iph != NULL) && (iph->iph_flags & IPHASH_DELETE) == 0)
332 size_t fr_flushhtable(op)
341 for (i = 0; i <= IPL_LOGMAX; i++) {
342 if (op->iplf_unit == i || op->iplf_unit == IPL_LOGALL) {
343 while ((iph = ipf_htables[i]) != NULL) {
344 if (fr_delhtable(iph) == 0) {
347 iph->iph_flags |= IPHASH_DELETE;
358 * Add an entry to a hash table.
360 int fr_addhtent(iph, ipeo)
368 KMALLOC(ipe, iphtent_t *);
372 bcopy((char *)ipeo, (char *)ipe, sizeof(*ipe));
373 ipe->ipe_addr.in4_addr &= ipe->ipe_mask.in4_addr;
374 ipe->ipe_addr.in4_addr = ntohl(ipe->ipe_addr.in4_addr);
375 bits = count4bits(ipe->ipe_mask.in4_addr);
376 ipe->ipe_mask.in4_addr = ntohl(ipe->ipe_mask.in4_addr);
378 hv = IPE_HASH_FN(ipe->ipe_addr.in4_addr, ipe->ipe_mask.in4_addr,
381 ipe->ipe_hnext = iph->iph_table[hv];
382 ipe->ipe_phnext = iph->iph_table + hv;
384 if (iph->iph_table[hv] != NULL)
385 iph->iph_table[hv]->ipe_phnext = &ipe->ipe_hnext;
386 iph->iph_table[hv] = ipe;
388 ipe->ipe_next = iph->iph_list;
389 ipe->ipe_pnext = &iph->iph_list;
390 if (ipe->ipe_next != NULL)
391 ipe->ipe_next->ipe_pnext = &ipe->ipe_next;
394 if ((bits >= 0) && (bits != 32))
395 iph->iph_masks |= 1 << bits;
397 switch (iph->iph_type & ~IPHASH_ANON)
399 case IPHASH_GROUPMAP :
400 ipe->ipe_ptr = fr_addgroup(ipe->ipe_group, NULL,
401 iph->iph_flags, IPL_LOGIPF,
411 ipe->ipe_unit = iph->iph_unit;
412 ipf_nhtnodes[ipe->ipe_unit]++;
418 void *fr_iphmfindgroup(tptr, aptr)
421 struct in_addr *addr;
426 READ_ENTER(&ip_poolrw);
430 ipe = fr_iphmfind(iph, addr);
435 RWLOCK_EXIT(&ip_poolrw);
440 /* ------------------------------------------------------------------------ */
441 /* Function: fr_iphmfindip */
442 /* Returns: int - 0 == +ve match, -1 == error, 1 == -ve/no match */
443 /* Parameters: tptr(I) - pointer to the pool to search */
444 /* ipversion(I) - IP protocol version (4 or 6) */
445 /* aptr(I) - pointer to address information */
447 /* Search the hash table for a given address and return a search result. */
448 /* ------------------------------------------------------------------------ */
449 int fr_iphmfindip(tptr, ipversion, aptr)
453 struct in_addr *addr;
461 if (tptr == NULL || aptr == NULL)
467 READ_ENTER(&ip_poolrw);
468 ipe = fr_iphmfind(iph, addr);
473 RWLOCK_EXIT(&ip_poolrw);
478 /* Locks: ip_poolrw */
479 static iphtent_t *fr_iphmfind(iph, addr)
481 struct in_addr *addr;
483 u_32_t hmsk, msk, ips;
487 hmsk = iph->iph_masks;
490 ips = ntohl(addr->s_addr) & msk;
491 hv = IPE_HASH_FN(ips, msk, iph->iph_size);
492 for (ipe = iph->iph_table[hv]; (ipe != NULL); ipe = ipe->ipe_hnext) {
493 if (ipe->ipe_mask.in4_addr != msk ||
494 ipe->ipe_addr.in4_addr != ips) {
500 if ((ipe == NULL) && (hmsk != 0)) {
503 if (hmsk & 0x80000000)
516 int fr_htable_getnext(token, ilp)
518 ipflookupiter_t *ilp;
520 iphtent_t *node, zn, *nextnode;
521 iphtable_t *iph, zp, *nextiph;
530 READ_ENTER(&ip_poolrw);
532 switch (ilp->ili_otype)
534 case IPFLOOKUPITER_LIST :
535 iph = token->ipt_data;
537 nextiph = ipf_htables[(int)ilp->ili_unit];
539 nextiph = iph->iph_next;
542 if (nextiph != NULL) {
543 ATOMIC_INC(nextiph->iph_ref);
544 token->ipt_data = nextiph;
546 bzero((char *)&zp, sizeof(zp));
548 token->ipt_data = NULL;
552 case IPFLOOKUPITER_NODE :
553 node = token->ipt_data;
555 iph = fr_findhtable(ilp->ili_unit, ilp->ili_name);
559 nextnode = iph->iph_list;
562 nextnode = node->ipe_next;
565 if (nextnode != NULL) {
566 ATOMIC_INC(nextnode->ipe_ref);
567 token->ipt_data = nextnode;
569 bzero((char *)&zn, sizeof(zn));
571 token->ipt_data = NULL;
579 RWLOCK_EXIT(&ip_poolrw);
583 switch (ilp->ili_otype)
585 case IPFLOOKUPITER_LIST :
587 WRITE_ENTER(&ip_poolrw);
589 RWLOCK_EXIT(&ip_poolrw);
591 err = COPYOUT(nextiph, ilp->ili_data, sizeof(*nextiph));
596 case IPFLOOKUPITER_NODE :
598 WRITE_ENTER(&ip_poolrw);
600 RWLOCK_EXIT(&ip_poolrw);
602 err = COPYOUT(nextnode, ilp->ili_data, sizeof(*nextnode));
612 void fr_htable_iterderef(otype, unit, data)
621 if (unit < 0 || unit > IPL_LOGMAX)
626 case IPFLOOKUPITER_LIST :
627 WRITE_ENTER(&ip_poolrw);
628 fr_derefhtable((iphtable_t *)data);
629 RWLOCK_EXIT(&ip_poolrw);
632 case IPFLOOKUPITER_NODE :
633 WRITE_ENTER(&ip_poolrw);
634 fr_derefhtent((iphtent_t *)data);
635 RWLOCK_EXIT(&ip_poolrw);
642 #endif /* IPFILTER_LOOKUP */