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 * 60Sstevel@tonic-gate * $Id: load_hash.c,v 1.10 2003/04/26 04:55:11 darrenr Exp $ 7*637Sml37995 * 8*637Sml37995 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 9*637Sml37995 * Use is subject to license terms. 100Sstevel@tonic-gate */ 110Sstevel@tonic-gate 12*637Sml37995 #pragma ident "%Z%%M% %I% %E% SMI" 13*637Sml37995 140Sstevel@tonic-gate #include <fcntl.h> 150Sstevel@tonic-gate #include <sys/ioctl.h> 160Sstevel@tonic-gate #include "ipf.h" 170Sstevel@tonic-gate #if SOLARIS2 >= 10 180Sstevel@tonic-gate #include "ip_lookup.h" 190Sstevel@tonic-gate #include "ip_htable.h" 200Sstevel@tonic-gate #else 210Sstevel@tonic-gate #include "netinet/ip_lookup.h" 220Sstevel@tonic-gate #include "netinet/ip_htable.h" 230Sstevel@tonic-gate #endif 240Sstevel@tonic-gate 250Sstevel@tonic-gate static int hashfd = -1; 260Sstevel@tonic-gate 270Sstevel@tonic-gate 280Sstevel@tonic-gate int load_hash(iphp, list, iocfunc) 290Sstevel@tonic-gate iphtable_t *iphp; 300Sstevel@tonic-gate iphtent_t *list; 310Sstevel@tonic-gate ioctlfunc_t iocfunc; 320Sstevel@tonic-gate { 330Sstevel@tonic-gate iplookupop_t op; 340Sstevel@tonic-gate iphtable_t iph; 350Sstevel@tonic-gate iphtent_t *a; 360Sstevel@tonic-gate size_t size; 370Sstevel@tonic-gate int n; 380Sstevel@tonic-gate 390Sstevel@tonic-gate if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0)) 400Sstevel@tonic-gate hashfd = open(IPLOOKUP_NAME, O_RDWR); 410Sstevel@tonic-gate if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0)) 420Sstevel@tonic-gate return -1; 430Sstevel@tonic-gate if (list == NULL) 440Sstevel@tonic-gate return 0; 450Sstevel@tonic-gate 460Sstevel@tonic-gate for (n = 0, a = list; a != NULL; a = a->ipe_next) 470Sstevel@tonic-gate n++; 480Sstevel@tonic-gate 490Sstevel@tonic-gate op.iplo_arg = 0; 500Sstevel@tonic-gate op.iplo_type = IPLT_HASH; 510Sstevel@tonic-gate op.iplo_unit = iphp->iph_unit; 520Sstevel@tonic-gate strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name)); 530Sstevel@tonic-gate if (*op.iplo_name == '\0') 540Sstevel@tonic-gate op.iplo_arg = IPHASH_ANON; 550Sstevel@tonic-gate op.iplo_size = sizeof(iph); 560Sstevel@tonic-gate op.iplo_struct = &iph; 570Sstevel@tonic-gate iph.iph_unit = iphp->iph_unit; 580Sstevel@tonic-gate iph.iph_type = iphp->iph_type; 590Sstevel@tonic-gate strncpy(iph.iph_name, iphp->iph_name, sizeof(iph.iph_name)); 600Sstevel@tonic-gate iph.iph_flags = iphp->iph_flags; 610Sstevel@tonic-gate if (iphp->iph_size == 0) 620Sstevel@tonic-gate size = n * 2 - 1; 630Sstevel@tonic-gate else 640Sstevel@tonic-gate size = iphp->iph_size; 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 700Sstevel@tonic-gate if ((*iocfunc)(hashfd, SIOCLOOKUPADDTABLE, &op)) 710Sstevel@tonic-gate if ((opts & OPT_DONOTHING) == 0) { 720Sstevel@tonic-gate perror("load_hash:SIOCLOOKUPADDTABLE"); 730Sstevel@tonic-gate return -1; 740Sstevel@tonic-gate } 750Sstevel@tonic-gate 760Sstevel@tonic-gate strncpy(op.iplo_name, iph.iph_name, sizeof(op.iplo_name)); 770Sstevel@tonic-gate strncpy(iphp->iph_name, iph.iph_name, sizeof(op.iplo_name)); 780Sstevel@tonic-gate 790Sstevel@tonic-gate if (opts & OPT_VERBOSE) { 800Sstevel@tonic-gate for (a = list; a != NULL; a = a->ipe_next) { 81*637Sml37995 if (a->ipe_family == AF_INET) { 82*637Sml37995 a->ipe_addr.in4_addr = ntohl(a->ipe_addr.in4_addr); 83*637Sml37995 a->ipe_mask.in4_addr = ntohl(a->ipe_mask.in4_addr); 84*637Sml37995 } 850Sstevel@tonic-gate } 860Sstevel@tonic-gate iph.iph_table = calloc(size, sizeof(*iph.iph_table)); 870Sstevel@tonic-gate if (iph.iph_table == NULL) { 880Sstevel@tonic-gate perror("calloc(size, sizeof(*iph.iph_table))"); 890Sstevel@tonic-gate return -1; 900Sstevel@tonic-gate } 910Sstevel@tonic-gate iph.iph_table[0] = list; 920Sstevel@tonic-gate printhash(&iph, bcopywrap, opts); 930Sstevel@tonic-gate free(iph.iph_table); 940Sstevel@tonic-gate 950Sstevel@tonic-gate for (a = list; a != NULL; a = a->ipe_next) { 96*637Sml37995 if (a->ipe_family == AF_INET) { 97*637Sml37995 a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr); 98*637Sml37995 a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr); 99*637Sml37995 } 1000Sstevel@tonic-gate } 1010Sstevel@tonic-gate } 1020Sstevel@tonic-gate 1030Sstevel@tonic-gate if (opts & OPT_DEBUG) 1040Sstevel@tonic-gate printf("Hash %s:\n", iph.iph_name); 1050Sstevel@tonic-gate 1060Sstevel@tonic-gate for (a = list; a != NULL; a = a->ipe_next) 1070Sstevel@tonic-gate load_hashnode(iphp->iph_unit, iph.iph_name, a, iocfunc); 1080Sstevel@tonic-gate 1090Sstevel@tonic-gate return 0; 1100Sstevel@tonic-gate } 111