load_hash.c revision 170268
1145510Sdarrenr/* $FreeBSD: head/contrib/ipfilter/lib/load_hash.c 170268 2007-06-04 02:54:36Z darrenr $ */ 2145510Sdarrenr 3145510Sdarrenr/* 4145510Sdarrenr * Copyright (C) 2002-2005 by Darren Reed. 5145510Sdarrenr * 6145510Sdarrenr * See the IPFILTER.LICENCE file for details on licencing. 7145510Sdarrenr * 8145510Sdarrenr * $Id: load_hash.c,v 1.11.2.5 2006/07/14 06:12:25 darrenr Exp $ 9145510Sdarrenr */ 10145510Sdarrenr 11145510Sdarrenr#include <fcntl.h> 12145510Sdarrenr#include <sys/ioctl.h> 13145510Sdarrenr#include "ipf.h" 14145510Sdarrenr#include "netinet/ip_lookup.h" 15145510Sdarrenr#include "netinet/ip_htable.h" 16145510Sdarrenr 17145510Sdarrenrstatic int hashfd = -1; 18145510Sdarrenr 19145510Sdarrenr 20145510Sdarrenrint load_hash(iphp, list, iocfunc) 21145510Sdarrenriphtable_t *iphp; 22145510Sdarrenriphtent_t *list; 23145510Sdarrenrioctlfunc_t iocfunc; 24145510Sdarrenr{ 25145510Sdarrenr iplookupop_t op; 26145510Sdarrenr iphtable_t iph; 27145510Sdarrenr iphtent_t *a; 28145510Sdarrenr size_t size; 29145510Sdarrenr int n; 30145510Sdarrenr 31145510Sdarrenr if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0)) 32145510Sdarrenr hashfd = open(IPLOOKUP_NAME, O_RDWR); 33145510Sdarrenr if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0)) 34145510Sdarrenr return -1; 35145510Sdarrenr 36145510Sdarrenr for (n = 0, a = list; a != NULL; a = a->ipe_next) 37145510Sdarrenr n++; 38145510Sdarrenr 39145510Sdarrenr op.iplo_arg = 0; 40145510Sdarrenr op.iplo_type = IPLT_HASH; 41145510Sdarrenr op.iplo_unit = iphp->iph_unit; 42145510Sdarrenr strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name)); 43145510Sdarrenr if (*op.iplo_name == '\0') 44145510Sdarrenr op.iplo_arg = IPHASH_ANON; 45145510Sdarrenr op.iplo_size = sizeof(iph); 46145510Sdarrenr op.iplo_struct = &iph; 47145510Sdarrenr iph.iph_unit = iphp->iph_unit; 48145510Sdarrenr iph.iph_type = iphp->iph_type; 49145510Sdarrenr strncpy(iph.iph_name, iphp->iph_name, sizeof(iph.iph_name)); 50145510Sdarrenr iph.iph_flags = iphp->iph_flags; 51145510Sdarrenr if (n <= 0) 52145510Sdarrenr n = 1; 53145510Sdarrenr if (iphp->iph_size == 0) 54145510Sdarrenr size = n * 2 - 1; 55145510Sdarrenr else 56145510Sdarrenr size = iphp->iph_size; 57145510Sdarrenr if ((list == NULL) && (size == 1)) { 58145510Sdarrenr fprintf(stderr, 59 "WARNING: empty hash table %s, recommend setting %s\n", 60 iphp->iph_name, "size to match expected use"); 61 } 62 iph.iph_size = size; 63 iph.iph_seed = iphp->iph_seed; 64 iph.iph_table = NULL; 65 iph.iph_list = NULL; 66 iph.iph_ref = 0; 67 68 if ((opts & OPT_REMOVE) == 0) { 69 if ((*iocfunc)(hashfd, SIOCLOOKUPADDTABLE, &op)) 70 if ((opts & OPT_DONOTHING) == 0) { 71 perror("load_hash:SIOCLOOKUPADDTABLE"); 72 return -1; 73 } 74 } 75 76 strncpy(iph.iph_name, op.iplo_name, sizeof(op.iplo_name)); 77 strncpy(iphp->iph_name, op.iplo_name, sizeof(op.iplo_name)); 78 79 if (opts & OPT_VERBOSE) { 80 for (a = list; a != NULL; a = a->ipe_next) { 81 a->ipe_addr.in4_addr = ntohl(a->ipe_addr.in4_addr); 82 a->ipe_mask.in4_addr = ntohl(a->ipe_mask.in4_addr); 83 } 84 iph.iph_table = calloc(size, sizeof(*iph.iph_table)); 85 if (iph.iph_table == NULL) { 86 perror("calloc(size, sizeof(*iph.iph_table))"); 87 return -1; 88 } 89 iph.iph_list = list; 90 printhash(&iph, bcopywrap, iph.iph_name, opts); 91 free(iph.iph_table); 92 iph.iph_list = NULL; 93 94 for (a = list; a != NULL; a = a->ipe_next) { 95 a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr); 96 a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr); 97 } 98 } 99 100 if (opts & OPT_DEBUG) 101 printf("Hash %s:\n", iph.iph_name); 102 103 for (a = list; a != NULL; a = a->ipe_next) 104 load_hashnode(iphp->iph_unit, iph.iph_name, a, iocfunc); 105 106 if ((opts & OPT_REMOVE) != 0) { 107 if ((*iocfunc)(hashfd, SIOCLOOKUPDELTABLE, &op)) 108 if ((opts & OPT_DONOTHING) == 0) { 109 perror("load_hash:SIOCLOOKUPDELTABLE"); 110 return -1; 111 } 112 } 113 return 0; 114} 115