17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * Copyright (C) 2002 by Darren Reed. 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing. 57c478bd9Sstevel@tonic-gate * 6ab25eeb5Syz155240 * $Id: load_hash.c,v 1.11.2.2 2005/02/01 02:44:05 darrenr Exp $ 77663b816Sml37995 * 8ab25eeb5Syz155240 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 97663b816Sml37995 * Use is subject to license terms. 10*94bdecd9SRob Gulewich * 11*94bdecd9SRob Gulewich * Copyright (c) 2014, Joyent, Inc. All rights reserved. 127c478bd9Sstevel@tonic-gate */ 137c478bd9Sstevel@tonic-gate 147c478bd9Sstevel@tonic-gate #include <fcntl.h> 157c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 167c478bd9Sstevel@tonic-gate #include "ipf.h" 177c478bd9Sstevel@tonic-gate #include "netinet/ip_lookup.h" 187c478bd9Sstevel@tonic-gate #include "netinet/ip_htable.h" 19*94bdecd9SRob Gulewich #include "ipfzone.h" 207c478bd9Sstevel@tonic-gate 217c478bd9Sstevel@tonic-gate static int hashfd = -1; 227c478bd9Sstevel@tonic-gate 237c478bd9Sstevel@tonic-gate 247c478bd9Sstevel@tonic-gate int load_hash(iphp, list, iocfunc) 257c478bd9Sstevel@tonic-gate iphtable_t *iphp; 267c478bd9Sstevel@tonic-gate iphtent_t *list; 277c478bd9Sstevel@tonic-gate ioctlfunc_t iocfunc; 287c478bd9Sstevel@tonic-gate { 297c478bd9Sstevel@tonic-gate iplookupop_t op; 307c478bd9Sstevel@tonic-gate iphtable_t iph; 317c478bd9Sstevel@tonic-gate iphtent_t *a; 327c478bd9Sstevel@tonic-gate size_t size; 337c478bd9Sstevel@tonic-gate int n; 347c478bd9Sstevel@tonic-gate 357c478bd9Sstevel@tonic-gate if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0)) 367c478bd9Sstevel@tonic-gate hashfd = open(IPLOOKUP_NAME, O_RDWR); 377c478bd9Sstevel@tonic-gate if ((hashfd == -1) && ((opts & OPT_DONOTHING) == 0)) 387c478bd9Sstevel@tonic-gate return -1; 39*94bdecd9SRob Gulewich if (setzone(hashfd) != 0) { 40*94bdecd9SRob Gulewich close(hashfd); 41*94bdecd9SRob Gulewich return -1; 42*94bdecd9SRob Gulewich } 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate for (n = 0, a = list; a != NULL; a = a->ipe_next) 457c478bd9Sstevel@tonic-gate n++; 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate op.iplo_arg = 0; 487c478bd9Sstevel@tonic-gate op.iplo_type = IPLT_HASH; 497c478bd9Sstevel@tonic-gate op.iplo_unit = iphp->iph_unit; 507c478bd9Sstevel@tonic-gate strncpy(op.iplo_name, iphp->iph_name, sizeof(op.iplo_name)); 517c478bd9Sstevel@tonic-gate if (*op.iplo_name == '\0') 527c478bd9Sstevel@tonic-gate op.iplo_arg = IPHASH_ANON; 537c478bd9Sstevel@tonic-gate op.iplo_size = sizeof(iph); 547c478bd9Sstevel@tonic-gate op.iplo_struct = &iph; 557c478bd9Sstevel@tonic-gate iph.iph_unit = iphp->iph_unit; 567c478bd9Sstevel@tonic-gate iph.iph_type = iphp->iph_type; 577c478bd9Sstevel@tonic-gate strncpy(iph.iph_name, iphp->iph_name, sizeof(iph.iph_name)); 587c478bd9Sstevel@tonic-gate iph.iph_flags = iphp->iph_flags; 59ab25eeb5Syz155240 if (n <= 0) 60ab25eeb5Syz155240 n = 1; 617c478bd9Sstevel@tonic-gate if (iphp->iph_size == 0) 627c478bd9Sstevel@tonic-gate size = n * 2 - 1; 637c478bd9Sstevel@tonic-gate else 647c478bd9Sstevel@tonic-gate size = iphp->iph_size; 65ab25eeb5Syz155240 if ((list == NULL) && (size == 1)) { 66ab25eeb5Syz155240 fprintf(stderr, 67ab25eeb5Syz155240 "WARNING: empty hash table %s, recommend setting %s\n", 68ab25eeb5Syz155240 iphp->iph_name, "size to match expected use"); 69ab25eeb5Syz155240 } 707c478bd9Sstevel@tonic-gate iph.iph_size = size; 717c478bd9Sstevel@tonic-gate iph.iph_seed = iphp->iph_seed; 727c478bd9Sstevel@tonic-gate iph.iph_table = NULL; 737c478bd9Sstevel@tonic-gate iph.iph_ref = 0; 747c478bd9Sstevel@tonic-gate 75ab25eeb5Syz155240 if ((opts & OPT_REMOVE) == 0) { 767c478bd9Sstevel@tonic-gate if ((*iocfunc)(hashfd, SIOCLOOKUPADDTABLE, &op)) 777c478bd9Sstevel@tonic-gate if ((opts & OPT_DONOTHING) == 0) { 787c478bd9Sstevel@tonic-gate perror("load_hash:SIOCLOOKUPADDTABLE"); 797c478bd9Sstevel@tonic-gate return -1; 807c478bd9Sstevel@tonic-gate } 81ab25eeb5Syz155240 } 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate strncpy(op.iplo_name, iph.iph_name, sizeof(op.iplo_name)); 847c478bd9Sstevel@tonic-gate strncpy(iphp->iph_name, iph.iph_name, sizeof(op.iplo_name)); 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) { 877c478bd9Sstevel@tonic-gate for (a = list; a != NULL; a = a->ipe_next) { 887663b816Sml37995 if (a->ipe_family == AF_INET) { 897c478bd9Sstevel@tonic-gate a->ipe_addr.in4_addr = ntohl(a->ipe_addr.in4_addr); 907c478bd9Sstevel@tonic-gate a->ipe_mask.in4_addr = ntohl(a->ipe_mask.in4_addr); 917c478bd9Sstevel@tonic-gate } 927663b816Sml37995 } 937c478bd9Sstevel@tonic-gate iph.iph_table = calloc(size, sizeof(*iph.iph_table)); 947c478bd9Sstevel@tonic-gate if (iph.iph_table == NULL) { 957c478bd9Sstevel@tonic-gate perror("calloc(size, sizeof(*iph.iph_table))"); 967c478bd9Sstevel@tonic-gate return -1; 977c478bd9Sstevel@tonic-gate } 987c478bd9Sstevel@tonic-gate iph.iph_table[0] = list; 99ab25eeb5Syz155240 printhash(&iph, bcopywrap, iph.iph_name, opts); 1007c478bd9Sstevel@tonic-gate free(iph.iph_table); 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate for (a = list; a != NULL; a = a->ipe_next) { 1037663b816Sml37995 if (a->ipe_family == AF_INET) { 1047c478bd9Sstevel@tonic-gate a->ipe_addr.in4_addr = htonl(a->ipe_addr.in4_addr); 1057c478bd9Sstevel@tonic-gate a->ipe_mask.in4_addr = htonl(a->ipe_mask.in4_addr); 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate } 1087663b816Sml37995 } 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate if (opts & OPT_DEBUG) 1117c478bd9Sstevel@tonic-gate printf("Hash %s:\n", iph.iph_name); 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate for (a = list; a != NULL; a = a->ipe_next) 1147c478bd9Sstevel@tonic-gate load_hashnode(iphp->iph_unit, iph.iph_name, a, iocfunc); 1157c478bd9Sstevel@tonic-gate 116ab25eeb5Syz155240 if ((opts & OPT_REMOVE) != 0) { 117ab25eeb5Syz155240 if ((*iocfunc)(hashfd, SIOCLOOKUPDELTABLE, &op)) 118ab25eeb5Syz155240 if ((opts & OPT_DONOTHING) == 0) { 119ab25eeb5Syz155240 perror("load_hash:SIOCLOOKUPDELTABLE"); 120ab25eeb5Syz155240 return -1; 121ab25eeb5Syz155240 } 122ab25eeb5Syz155240 } 1237c478bd9Sstevel@tonic-gate return 0; 1247c478bd9Sstevel@tonic-gate } 125