1*0Sstevel@tonic-gate /* 2*0Sstevel@tonic-gate * Copyright (C) 1999-2001, 2003 by Darren Reed. 3*0Sstevel@tonic-gate * 4*0Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing. 5*0Sstevel@tonic-gate * 6*0Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 7*0Sstevel@tonic-gate * Use is subject to license terms. 8*0Sstevel@tonic-gate */ 9*0Sstevel@tonic-gate 10*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gate #ifdef __FreeBSD__ 13*0Sstevel@tonic-gate # ifndef __FreeBSD_cc_version 14*0Sstevel@tonic-gate # include <osreldate.h> 15*0Sstevel@tonic-gate # else 16*0Sstevel@tonic-gate # if __FreeBSD_cc_version < 430000 17*0Sstevel@tonic-gate # include <osreldate.h> 18*0Sstevel@tonic-gate # endif 19*0Sstevel@tonic-gate # endif 20*0Sstevel@tonic-gate #endif 21*0Sstevel@tonic-gate #include <stdio.h> 22*0Sstevel@tonic-gate #include <unistd.h> 23*0Sstevel@tonic-gate #include <string.h> 24*0Sstevel@tonic-gate #include <fcntl.h> 25*0Sstevel@tonic-gate #include <errno.h> 26*0Sstevel@tonic-gate #if !defined(__SVR4) && !defined(__GNUC__) 27*0Sstevel@tonic-gate #include <strings.h> 28*0Sstevel@tonic-gate #endif 29*0Sstevel@tonic-gate #include <sys/types.h> 30*0Sstevel@tonic-gate #include <sys/param.h> 31*0Sstevel@tonic-gate #include <sys/file.h> 32*0Sstevel@tonic-gate #include <stdlib.h> 33*0Sstevel@tonic-gate #include <stddef.h> 34*0Sstevel@tonic-gate #include <sys/socket.h> 35*0Sstevel@tonic-gate #include <sys/ioctl.h> 36*0Sstevel@tonic-gate #include <netinet/in.h> 37*0Sstevel@tonic-gate #include <netinet/in_systm.h> 38*0Sstevel@tonic-gate #include <sys/time.h> 39*0Sstevel@tonic-gate #include <net/if.h> 40*0Sstevel@tonic-gate #if __FreeBSD_version >= 300000 41*0Sstevel@tonic-gate # include <net/if_var.h> 42*0Sstevel@tonic-gate #endif 43*0Sstevel@tonic-gate #include <netinet/ip.h> 44*0Sstevel@tonic-gate #include <netdb.h> 45*0Sstevel@tonic-gate #include <arpa/nameser.h> 46*0Sstevel@tonic-gate #include <resolv.h> 47*0Sstevel@tonic-gate #include "ipf.h" 48*0Sstevel@tonic-gate 49*0Sstevel@tonic-gate #if !defined(lint) 50*0Sstevel@tonic-gate static const char rcsid[] = "@(#)$Id: ipfs.c,v 1.9 2003/05/17 09:47:35 darrenr Exp $"; 51*0Sstevel@tonic-gate #endif 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gate #ifndef IPF_SAVEDIR 54*0Sstevel@tonic-gate # define IPF_SAVEDIR "/var/db/ipf" 55*0Sstevel@tonic-gate #endif 56*0Sstevel@tonic-gate #ifndef IPF_NATFILE 57*0Sstevel@tonic-gate # define IPF_NATFILE "ipnat.ipf" 58*0Sstevel@tonic-gate #endif 59*0Sstevel@tonic-gate #ifndef IPF_STATEFILE 60*0Sstevel@tonic-gate # define IPF_STATEFILE "ipstate.ipf" 61*0Sstevel@tonic-gate #endif 62*0Sstevel@tonic-gate 63*0Sstevel@tonic-gate #if !defined(__SVR4) && defined(__GNUC__) 64*0Sstevel@tonic-gate extern char *index __P((const char *, int)); 65*0Sstevel@tonic-gate #endif 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gate extern char *optarg; 68*0Sstevel@tonic-gate extern int optind; 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate int main __P((int, char *[])); 71*0Sstevel@tonic-gate void usage __P((void)); 72*0Sstevel@tonic-gate int changestateif __P((char *, char *)); 73*0Sstevel@tonic-gate int changenatif __P((char *, char *)); 74*0Sstevel@tonic-gate int readstate __P((int, char *)); 75*0Sstevel@tonic-gate int readnat __P((int, char *)); 76*0Sstevel@tonic-gate int writestate __P((int, char *)); 77*0Sstevel@tonic-gate int opendevice __P((char *)); 78*0Sstevel@tonic-gate void closedevice __P((int)); 79*0Sstevel@tonic-gate int setlock __P((int, int)); 80*0Sstevel@tonic-gate int writeall __P((char *)); 81*0Sstevel@tonic-gate int readall __P((char *)); 82*0Sstevel@tonic-gate int writenat __P((int, char *)); 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gate int opts = 0; 85*0Sstevel@tonic-gate char *progname; 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gate void usage() 89*0Sstevel@tonic-gate { 90*0Sstevel@tonic-gate fprintf(stderr, "usage: %s [-nv] -l\n", progname); 91*0Sstevel@tonic-gate fprintf(stderr, "usage: %s [-nv] -u\n", progname); 92*0Sstevel@tonic-gate fprintf(stderr, "usage: %s [-nv] [-d <dir>] -R\n", progname); 93*0Sstevel@tonic-gate fprintf(stderr, "usage: %s [-nv] [-d <dir>] -W\n", progname); 94*0Sstevel@tonic-gate fprintf(stderr, "usage: %s [-nNSv] [-f <file>] -r\n", progname); 95*0Sstevel@tonic-gate fprintf(stderr, "usage: %s [-nNSv] [-f <file>] -w\n", progname); 96*0Sstevel@tonic-gate fprintf(stderr, "usage: %s [-nNSv] -f <filename> -i <if1>,<if2>\n", 97*0Sstevel@tonic-gate progname); 98*0Sstevel@tonic-gate exit(1); 99*0Sstevel@tonic-gate } 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate /* 103*0Sstevel@tonic-gate * Change interface names in state information saved out to disk. 104*0Sstevel@tonic-gate */ 105*0Sstevel@tonic-gate int changestateif(ifs, fname) 106*0Sstevel@tonic-gate char *ifs, *fname; 107*0Sstevel@tonic-gate { 108*0Sstevel@tonic-gate int fd, olen, nlen, rw; 109*0Sstevel@tonic-gate ipstate_save_t ips; 110*0Sstevel@tonic-gate off_t pos; 111*0Sstevel@tonic-gate char *s; 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gate s = strchr(ifs, ','); 114*0Sstevel@tonic-gate if (!s) 115*0Sstevel@tonic-gate usage(); 116*0Sstevel@tonic-gate *s++ = '\0'; 117*0Sstevel@tonic-gate nlen = strlen(s); 118*0Sstevel@tonic-gate olen = strlen(ifs); 119*0Sstevel@tonic-gate if (nlen >= sizeof(ips.ips_is.is_ifname) || 120*0Sstevel@tonic-gate olen >= sizeof(ips.ips_is.is_ifname)) 121*0Sstevel@tonic-gate usage(); 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate fd = open(fname, O_RDWR); 124*0Sstevel@tonic-gate if (fd == -1) { 125*0Sstevel@tonic-gate perror("open"); 126*0Sstevel@tonic-gate exit(1); 127*0Sstevel@tonic-gate } 128*0Sstevel@tonic-gate 129*0Sstevel@tonic-gate for (pos = 0; read(fd, &ips, sizeof(ips)) == sizeof(ips); ) { 130*0Sstevel@tonic-gate rw = 0; 131*0Sstevel@tonic-gate if (!strncmp(ips.ips_is.is_ifname[0], ifs, olen + 1)) { 132*0Sstevel@tonic-gate strcpy(ips.ips_is.is_ifname[0], s); 133*0Sstevel@tonic-gate rw = 1; 134*0Sstevel@tonic-gate } 135*0Sstevel@tonic-gate if (!strncmp(ips.ips_is.is_ifname[1], ifs, olen + 1)) { 136*0Sstevel@tonic-gate strcpy(ips.ips_is.is_ifname[1], s); 137*0Sstevel@tonic-gate rw = 1; 138*0Sstevel@tonic-gate } 139*0Sstevel@tonic-gate if (rw == 1) { 140*0Sstevel@tonic-gate if (lseek(fd, pos, SEEK_SET) != pos) { 141*0Sstevel@tonic-gate perror("lseek"); 142*0Sstevel@tonic-gate exit(1); 143*0Sstevel@tonic-gate } 144*0Sstevel@tonic-gate if (write(fd, &ips, sizeof(ips)) != sizeof(ips)) { 145*0Sstevel@tonic-gate perror("write"); 146*0Sstevel@tonic-gate exit(1); 147*0Sstevel@tonic-gate } 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate pos = lseek(fd, 0, SEEK_CUR); 150*0Sstevel@tonic-gate } 151*0Sstevel@tonic-gate close(fd); 152*0Sstevel@tonic-gate 153*0Sstevel@tonic-gate return 0; 154*0Sstevel@tonic-gate } 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gate 157*0Sstevel@tonic-gate /* 158*0Sstevel@tonic-gate * Change interface names in NAT information saved out to disk. 159*0Sstevel@tonic-gate */ 160*0Sstevel@tonic-gate int changenatif(ifs, fname) 161*0Sstevel@tonic-gate char *ifs, *fname; 162*0Sstevel@tonic-gate { 163*0Sstevel@tonic-gate int fd, olen, nlen, rw; 164*0Sstevel@tonic-gate nat_save_t ipn; 165*0Sstevel@tonic-gate nat_t *nat; 166*0Sstevel@tonic-gate off_t pos; 167*0Sstevel@tonic-gate char *s; 168*0Sstevel@tonic-gate 169*0Sstevel@tonic-gate s = strchr(ifs, ','); 170*0Sstevel@tonic-gate if (!s) 171*0Sstevel@tonic-gate usage(); 172*0Sstevel@tonic-gate *s++ = '\0'; 173*0Sstevel@tonic-gate nlen = strlen(s); 174*0Sstevel@tonic-gate olen = strlen(ifs); 175*0Sstevel@tonic-gate nat = &ipn.ipn_nat; 176*0Sstevel@tonic-gate if (nlen >= sizeof(nat->nat_ifnames[0]) || 177*0Sstevel@tonic-gate olen >= sizeof(nat->nat_ifnames[0])) 178*0Sstevel@tonic-gate usage(); 179*0Sstevel@tonic-gate 180*0Sstevel@tonic-gate fd = open(fname, O_RDWR); 181*0Sstevel@tonic-gate if (fd == -1) { 182*0Sstevel@tonic-gate perror("open"); 183*0Sstevel@tonic-gate exit(1); 184*0Sstevel@tonic-gate } 185*0Sstevel@tonic-gate 186*0Sstevel@tonic-gate for (pos = 0; read(fd, &ipn, sizeof(ipn)) == sizeof(ipn); ) { 187*0Sstevel@tonic-gate rw = 0; 188*0Sstevel@tonic-gate if (!strncmp(nat->nat_ifnames[0], ifs, olen + 1)) { 189*0Sstevel@tonic-gate strcpy(nat->nat_ifnames[0], s); 190*0Sstevel@tonic-gate rw = 1; 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate if (!strncmp(nat->nat_ifnames[1], ifs, olen + 1)) { 193*0Sstevel@tonic-gate strcpy(nat->nat_ifnames[1], s); 194*0Sstevel@tonic-gate rw = 1; 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate if (rw == 1) { 197*0Sstevel@tonic-gate if (lseek(fd, pos, SEEK_SET) != pos) { 198*0Sstevel@tonic-gate perror("lseek"); 199*0Sstevel@tonic-gate exit(1); 200*0Sstevel@tonic-gate } 201*0Sstevel@tonic-gate if (write(fd, &ipn, sizeof(ipn)) != sizeof(ipn)) { 202*0Sstevel@tonic-gate perror("write"); 203*0Sstevel@tonic-gate exit(1); 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate pos = lseek(fd, 0, SEEK_CUR); 207*0Sstevel@tonic-gate } 208*0Sstevel@tonic-gate close(fd); 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate return 0; 211*0Sstevel@tonic-gate } 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate 214*0Sstevel@tonic-gate int main(argc,argv) 215*0Sstevel@tonic-gate int argc; 216*0Sstevel@tonic-gate char *argv[]; 217*0Sstevel@tonic-gate { 218*0Sstevel@tonic-gate int c, lock = -1, devfd = -1, err = 0, rw = -1, ns = -1, set = 0; 219*0Sstevel@tonic-gate char *dirname = NULL, *filename = NULL, *ifs = NULL; 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate progname = argv[0]; 222*0Sstevel@tonic-gate while ((c = getopt(argc, argv, "d:f:lNnSRruvWw")) != -1) 223*0Sstevel@tonic-gate switch (c) 224*0Sstevel@tonic-gate { 225*0Sstevel@tonic-gate case 'd' : 226*0Sstevel@tonic-gate if ((set == 0) && !dirname && !filename) 227*0Sstevel@tonic-gate dirname = optarg; 228*0Sstevel@tonic-gate else 229*0Sstevel@tonic-gate usage(); 230*0Sstevel@tonic-gate break; 231*0Sstevel@tonic-gate case 'f' : 232*0Sstevel@tonic-gate if ((set == 0) && !dirname && !filename) 233*0Sstevel@tonic-gate filename = optarg; 234*0Sstevel@tonic-gate else 235*0Sstevel@tonic-gate usage(); 236*0Sstevel@tonic-gate break; 237*0Sstevel@tonic-gate case 'i' : 238*0Sstevel@tonic-gate ifs = optarg; 239*0Sstevel@tonic-gate set = 1; 240*0Sstevel@tonic-gate break; 241*0Sstevel@tonic-gate case 'l' : 242*0Sstevel@tonic-gate if (filename || dirname || set) 243*0Sstevel@tonic-gate usage(); 244*0Sstevel@tonic-gate lock = 1; 245*0Sstevel@tonic-gate set = 1; 246*0Sstevel@tonic-gate break; 247*0Sstevel@tonic-gate case 'n' : 248*0Sstevel@tonic-gate opts |= OPT_DONOTHING; 249*0Sstevel@tonic-gate break; 250*0Sstevel@tonic-gate case 'N' : 251*0Sstevel@tonic-gate if ((ns >= 0) || dirname || (rw != -1) || set) 252*0Sstevel@tonic-gate usage(); 253*0Sstevel@tonic-gate ns = 0; 254*0Sstevel@tonic-gate set = 1; 255*0Sstevel@tonic-gate break; 256*0Sstevel@tonic-gate case 'r' : 257*0Sstevel@tonic-gate if ((ns >= 0) || dirname || (rw != -1)) 258*0Sstevel@tonic-gate usage(); 259*0Sstevel@tonic-gate rw = 0; 260*0Sstevel@tonic-gate set = 1; 261*0Sstevel@tonic-gate break; 262*0Sstevel@tonic-gate case 'R' : 263*0Sstevel@tonic-gate rw = 2; 264*0Sstevel@tonic-gate set = 1; 265*0Sstevel@tonic-gate break; 266*0Sstevel@tonic-gate case 'S' : 267*0Sstevel@tonic-gate if ((ns >= 0) || dirname || (rw != -1) || set) 268*0Sstevel@tonic-gate usage(); 269*0Sstevel@tonic-gate ns = 1; 270*0Sstevel@tonic-gate set = 1; 271*0Sstevel@tonic-gate break; 272*0Sstevel@tonic-gate case 'u' : 273*0Sstevel@tonic-gate if (filename || dirname || set) 274*0Sstevel@tonic-gate usage(); 275*0Sstevel@tonic-gate lock = 0; 276*0Sstevel@tonic-gate set = 1; 277*0Sstevel@tonic-gate break; 278*0Sstevel@tonic-gate case 'v' : 279*0Sstevel@tonic-gate opts |= OPT_VERBOSE; 280*0Sstevel@tonic-gate break; 281*0Sstevel@tonic-gate case 'w' : 282*0Sstevel@tonic-gate if (dirname || (rw != -1) || (ns == -1)) 283*0Sstevel@tonic-gate usage(); 284*0Sstevel@tonic-gate rw = 1; 285*0Sstevel@tonic-gate set = 1; 286*0Sstevel@tonic-gate break; 287*0Sstevel@tonic-gate case 'W' : 288*0Sstevel@tonic-gate rw = 3; 289*0Sstevel@tonic-gate set = 1; 290*0Sstevel@tonic-gate break; 291*0Sstevel@tonic-gate case '?' : 292*0Sstevel@tonic-gate default : 293*0Sstevel@tonic-gate usage(); 294*0Sstevel@tonic-gate } 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate if (ifs) { 297*0Sstevel@tonic-gate if (!filename || ns < 0) 298*0Sstevel@tonic-gate usage(); 299*0Sstevel@tonic-gate if (ns == 0) 300*0Sstevel@tonic-gate return changenatif(ifs, filename); 301*0Sstevel@tonic-gate else 302*0Sstevel@tonic-gate return changestateif(ifs, filename); 303*0Sstevel@tonic-gate } 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gate if ((ns >= 0) || (lock >= 0)) { 306*0Sstevel@tonic-gate if (lock >= 0) 307*0Sstevel@tonic-gate devfd = opendevice(NULL); 308*0Sstevel@tonic-gate else if (ns >= 0) { 309*0Sstevel@tonic-gate if (ns == 1) 310*0Sstevel@tonic-gate devfd = opendevice(IPSTATE_NAME); 311*0Sstevel@tonic-gate else if (ns == 0) 312*0Sstevel@tonic-gate devfd = opendevice(IPNAT_NAME); 313*0Sstevel@tonic-gate } 314*0Sstevel@tonic-gate if (devfd == -1) 315*0Sstevel@tonic-gate exit(1); 316*0Sstevel@tonic-gate } 317*0Sstevel@tonic-gate 318*0Sstevel@tonic-gate if (lock >= 0) 319*0Sstevel@tonic-gate err = setlock(devfd, lock); 320*0Sstevel@tonic-gate else if (rw >= 0) { 321*0Sstevel@tonic-gate if (rw & 1) { /* WRITE */ 322*0Sstevel@tonic-gate if (rw & 2) 323*0Sstevel@tonic-gate err = writeall(dirname); 324*0Sstevel@tonic-gate else { 325*0Sstevel@tonic-gate if (ns == 0) 326*0Sstevel@tonic-gate err = writenat(devfd, filename); 327*0Sstevel@tonic-gate else if (ns == 1) 328*0Sstevel@tonic-gate err = writestate(devfd, filename); 329*0Sstevel@tonic-gate } 330*0Sstevel@tonic-gate } else { 331*0Sstevel@tonic-gate if (rw & 2) 332*0Sstevel@tonic-gate err = readall(dirname); 333*0Sstevel@tonic-gate else { 334*0Sstevel@tonic-gate if (ns == 0) 335*0Sstevel@tonic-gate err = readnat(devfd, filename); 336*0Sstevel@tonic-gate else if (ns == 1) 337*0Sstevel@tonic-gate err = readstate(devfd, filename); 338*0Sstevel@tonic-gate } 339*0Sstevel@tonic-gate } 340*0Sstevel@tonic-gate } 341*0Sstevel@tonic-gate return err; 342*0Sstevel@tonic-gate } 343*0Sstevel@tonic-gate 344*0Sstevel@tonic-gate 345*0Sstevel@tonic-gate int opendevice(ipfdev) 346*0Sstevel@tonic-gate char *ipfdev; 347*0Sstevel@tonic-gate { 348*0Sstevel@tonic-gate int fd = -1; 349*0Sstevel@tonic-gate 350*0Sstevel@tonic-gate if (opts & OPT_DONOTHING) 351*0Sstevel@tonic-gate return -2; 352*0Sstevel@tonic-gate 353*0Sstevel@tonic-gate if (!ipfdev) 354*0Sstevel@tonic-gate ipfdev = IPL_NAME; 355*0Sstevel@tonic-gate 356*0Sstevel@tonic-gate if ((fd = open(ipfdev, O_RDWR)) == -1) 357*0Sstevel@tonic-gate if ((fd = open(ipfdev, O_RDONLY)) == -1) 358*0Sstevel@tonic-gate perror("open device"); 359*0Sstevel@tonic-gate return fd; 360*0Sstevel@tonic-gate } 361*0Sstevel@tonic-gate 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gate void closedevice(fd) 364*0Sstevel@tonic-gate int fd; 365*0Sstevel@tonic-gate { 366*0Sstevel@tonic-gate close(fd); 367*0Sstevel@tonic-gate } 368*0Sstevel@tonic-gate 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gate int setlock(fd, lock) 371*0Sstevel@tonic-gate int fd, lock; 372*0Sstevel@tonic-gate { 373*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 374*0Sstevel@tonic-gate printf("Turn lock %s\n", lock ? "on" : "off"); 375*0Sstevel@tonic-gate if (!(opts & OPT_DONOTHING)) { 376*0Sstevel@tonic-gate if (ioctl(fd, SIOCSTLCK, &lock) == -1) { 377*0Sstevel@tonic-gate perror("SIOCSTLCK"); 378*0Sstevel@tonic-gate return 1; 379*0Sstevel@tonic-gate } 380*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 381*0Sstevel@tonic-gate printf("Lock now %s\n", lock ? "on" : "off"); 382*0Sstevel@tonic-gate } 383*0Sstevel@tonic-gate return 0; 384*0Sstevel@tonic-gate } 385*0Sstevel@tonic-gate 386*0Sstevel@tonic-gate 387*0Sstevel@tonic-gate int writestate(fd, file) 388*0Sstevel@tonic-gate int fd; 389*0Sstevel@tonic-gate char *file; 390*0Sstevel@tonic-gate { 391*0Sstevel@tonic-gate ipstate_save_t ips, *ipsp; 392*0Sstevel@tonic-gate int wfd = -1; 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gate if (!file) 395*0Sstevel@tonic-gate file = IPF_STATEFILE; 396*0Sstevel@tonic-gate 397*0Sstevel@tonic-gate wfd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0600); 398*0Sstevel@tonic-gate if (wfd == -1) { 399*0Sstevel@tonic-gate fprintf(stderr, "%s ", file); 400*0Sstevel@tonic-gate perror("state:open"); 401*0Sstevel@tonic-gate return 1; 402*0Sstevel@tonic-gate } 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gate ipsp = &ips; 405*0Sstevel@tonic-gate bzero((char *)ipsp, sizeof(ips)); 406*0Sstevel@tonic-gate 407*0Sstevel@tonic-gate do { 408*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 409*0Sstevel@tonic-gate printf("Getting state from addr %p\n", ips.ips_next); 410*0Sstevel@tonic-gate if (ioctl(fd, SIOCSTGET, &ipsp)) { 411*0Sstevel@tonic-gate if (errno == ENOENT) 412*0Sstevel@tonic-gate break; 413*0Sstevel@tonic-gate perror("state:SIOCSTGET"); 414*0Sstevel@tonic-gate close(wfd); 415*0Sstevel@tonic-gate return 1; 416*0Sstevel@tonic-gate } 417*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 418*0Sstevel@tonic-gate printf("Got state next %p\n", ips.ips_next); 419*0Sstevel@tonic-gate if (write(wfd, ipsp, sizeof(ips)) != sizeof(ips)) { 420*0Sstevel@tonic-gate perror("state:write"); 421*0Sstevel@tonic-gate close(wfd); 422*0Sstevel@tonic-gate return 1; 423*0Sstevel@tonic-gate } 424*0Sstevel@tonic-gate } while (ips.ips_next != NULL); 425*0Sstevel@tonic-gate close(wfd); 426*0Sstevel@tonic-gate 427*0Sstevel@tonic-gate return 0; 428*0Sstevel@tonic-gate } 429*0Sstevel@tonic-gate 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate int readstate(fd, file) 432*0Sstevel@tonic-gate int fd; 433*0Sstevel@tonic-gate char *file; 434*0Sstevel@tonic-gate { 435*0Sstevel@tonic-gate ipstate_save_t ips, *is, *ipshead = NULL, *is1, *ipstail = NULL; 436*0Sstevel@tonic-gate int sfd = -1, i; 437*0Sstevel@tonic-gate 438*0Sstevel@tonic-gate if (!file) 439*0Sstevel@tonic-gate file = IPF_STATEFILE; 440*0Sstevel@tonic-gate 441*0Sstevel@tonic-gate sfd = open(file, O_RDONLY, 0600); 442*0Sstevel@tonic-gate if (sfd == -1) { 443*0Sstevel@tonic-gate fprintf(stderr, "%s ", file); 444*0Sstevel@tonic-gate perror("open"); 445*0Sstevel@tonic-gate return 1; 446*0Sstevel@tonic-gate } 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gate bzero((char *)&ips, sizeof(ips)); 449*0Sstevel@tonic-gate 450*0Sstevel@tonic-gate /* 451*0Sstevel@tonic-gate * 1. Read all state information in. 452*0Sstevel@tonic-gate */ 453*0Sstevel@tonic-gate do { 454*0Sstevel@tonic-gate i = read(sfd, &ips, sizeof(ips)); 455*0Sstevel@tonic-gate if (i == -1) { 456*0Sstevel@tonic-gate perror("read"); 457*0Sstevel@tonic-gate close(sfd); 458*0Sstevel@tonic-gate return 1; 459*0Sstevel@tonic-gate } 460*0Sstevel@tonic-gate if (i == 0) 461*0Sstevel@tonic-gate break; 462*0Sstevel@tonic-gate if (i != sizeof(ips)) { 463*0Sstevel@tonic-gate fprintf(stderr, "incomplete read: %d != %d\n", i, 464*0Sstevel@tonic-gate (int)sizeof(ips)); 465*0Sstevel@tonic-gate close(sfd); 466*0Sstevel@tonic-gate return 1; 467*0Sstevel@tonic-gate } 468*0Sstevel@tonic-gate is = (ipstate_save_t *)malloc(sizeof(*is)); 469*0Sstevel@tonic-gate if(!is) { 470*0Sstevel@tonic-gate fprintf(stderr, "malloc failed\n"); 471*0Sstevel@tonic-gate return 1; 472*0Sstevel@tonic-gate } 473*0Sstevel@tonic-gate 474*0Sstevel@tonic-gate bcopy((char *)&ips, (char *)is, sizeof(ips)); 475*0Sstevel@tonic-gate 476*0Sstevel@tonic-gate /* 477*0Sstevel@tonic-gate * Check to see if this is the first state entry that will 478*0Sstevel@tonic-gate * reference a particular rule and if so, flag it as such 479*0Sstevel@tonic-gate * else just adjust the rule pointer to become a pointer to 480*0Sstevel@tonic-gate * the other. We do this so we have a means later for tracking 481*0Sstevel@tonic-gate * who is referencing us when we get back the real pointer 482*0Sstevel@tonic-gate * in is_rule after doing the ioctl. 483*0Sstevel@tonic-gate */ 484*0Sstevel@tonic-gate for (is1 = ipshead; is1 != NULL; is1 = is1->ips_next) 485*0Sstevel@tonic-gate if (is1->ips_rule == is->ips_rule) 486*0Sstevel@tonic-gate break; 487*0Sstevel@tonic-gate if (is1 == NULL) 488*0Sstevel@tonic-gate is->ips_is.is_flags |= SI_NEWFR; 489*0Sstevel@tonic-gate else 490*0Sstevel@tonic-gate is->ips_rule = (void *)&is1->ips_rule; 491*0Sstevel@tonic-gate 492*0Sstevel@tonic-gate /* 493*0Sstevel@tonic-gate * Use a tail-queue type list (add things to the end).. 494*0Sstevel@tonic-gate */ 495*0Sstevel@tonic-gate is->ips_next = NULL; 496*0Sstevel@tonic-gate if (!ipshead) 497*0Sstevel@tonic-gate ipshead = is; 498*0Sstevel@tonic-gate if (ipstail) 499*0Sstevel@tonic-gate ipstail->ips_next = is; 500*0Sstevel@tonic-gate ipstail = is; 501*0Sstevel@tonic-gate } while (1); 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate close(sfd); 504*0Sstevel@tonic-gate 505*0Sstevel@tonic-gate for (is = ipshead; is; is = is->ips_next) { 506*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 507*0Sstevel@tonic-gate printf("Loading new state table entry\n"); 508*0Sstevel@tonic-gate if (is->ips_is.is_flags & SI_NEWFR) { 509*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 510*0Sstevel@tonic-gate printf("Loading new filter rule\n"); 511*0Sstevel@tonic-gate } 512*0Sstevel@tonic-gate if (!(opts & OPT_DONOTHING)) 513*0Sstevel@tonic-gate if (ioctl(fd, SIOCSTPUT, &is)) { 514*0Sstevel@tonic-gate perror("SIOCSTPUT"); 515*0Sstevel@tonic-gate return 1; 516*0Sstevel@tonic-gate } 517*0Sstevel@tonic-gate 518*0Sstevel@tonic-gate if (is->ips_is.is_flags & SI_NEWFR) { 519*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 520*0Sstevel@tonic-gate printf("Real rule addr %p\n", is->ips_rule); 521*0Sstevel@tonic-gate for (is1 = is->ips_next; is1; is1 = is1->ips_next) 522*0Sstevel@tonic-gate if (is1->ips_rule == (frentry_t *)&is->ips_rule) 523*0Sstevel@tonic-gate is1->ips_rule = is->ips_rule; 524*0Sstevel@tonic-gate } 525*0Sstevel@tonic-gate } 526*0Sstevel@tonic-gate 527*0Sstevel@tonic-gate return 0; 528*0Sstevel@tonic-gate } 529*0Sstevel@tonic-gate 530*0Sstevel@tonic-gate 531*0Sstevel@tonic-gate int readnat(fd, file) 532*0Sstevel@tonic-gate int fd; 533*0Sstevel@tonic-gate char *file; 534*0Sstevel@tonic-gate { 535*0Sstevel@tonic-gate nat_save_t ipn, *in, *ipnhead, *in1, *ipntail, *ipnp; 536*0Sstevel@tonic-gate int nfd, i; 537*0Sstevel@tonic-gate nat_t *nat; 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gate nfd = -1; 540*0Sstevel@tonic-gate in = NULL; 541*0Sstevel@tonic-gate ipnhead = NULL; 542*0Sstevel@tonic-gate ipntail = NULL; 543*0Sstevel@tonic-gate 544*0Sstevel@tonic-gate if (!file) 545*0Sstevel@tonic-gate file = IPF_NATFILE; 546*0Sstevel@tonic-gate 547*0Sstevel@tonic-gate nfd = open(file, O_RDONLY); 548*0Sstevel@tonic-gate if (nfd == -1) { 549*0Sstevel@tonic-gate fprintf(stderr, "%s ", file); 550*0Sstevel@tonic-gate perror("nat:open"); 551*0Sstevel@tonic-gate return 1; 552*0Sstevel@tonic-gate } 553*0Sstevel@tonic-gate 554*0Sstevel@tonic-gate bzero((char *)&ipn, sizeof(ipn)); 555*0Sstevel@tonic-gate ipnp = &ipn; 556*0Sstevel@tonic-gate 557*0Sstevel@tonic-gate /* 558*0Sstevel@tonic-gate * 1. Read all state information in. 559*0Sstevel@tonic-gate */ 560*0Sstevel@tonic-gate do { 561*0Sstevel@tonic-gate i = read(nfd, &ipn, sizeof(ipn)); 562*0Sstevel@tonic-gate if (i == -1) { 563*0Sstevel@tonic-gate perror("read"); 564*0Sstevel@tonic-gate close(nfd); 565*0Sstevel@tonic-gate return 1; 566*0Sstevel@tonic-gate } 567*0Sstevel@tonic-gate if (i == 0) 568*0Sstevel@tonic-gate break; 569*0Sstevel@tonic-gate if (i != sizeof(ipn)) { 570*0Sstevel@tonic-gate fprintf(stderr, "incomplete read: %d != %d\n", i, 571*0Sstevel@tonic-gate (int)sizeof(ipn)); 572*0Sstevel@tonic-gate close(nfd); 573*0Sstevel@tonic-gate return 1; 574*0Sstevel@tonic-gate } 575*0Sstevel@tonic-gate 576*0Sstevel@tonic-gate if (ipn.ipn_dsize > 0) { 577*0Sstevel@tonic-gate char *s = ipnp->ipn_data; 578*0Sstevel@tonic-gate int n = ipnp->ipn_dsize; 579*0Sstevel@tonic-gate 580*0Sstevel@tonic-gate n -= sizeof(ipnp->ipn_data); 581*0Sstevel@tonic-gate in = malloc(sizeof(*in) + n); 582*0Sstevel@tonic-gate if (!in) 583*0Sstevel@tonic-gate break; 584*0Sstevel@tonic-gate 585*0Sstevel@tonic-gate s += sizeof(ipnp->ipn_data); 586*0Sstevel@tonic-gate i = read(nfd, s, n); 587*0Sstevel@tonic-gate if (i == 0) 588*0Sstevel@tonic-gate break; 589*0Sstevel@tonic-gate if (i != n) { 590*0Sstevel@tonic-gate fprintf(stderr, "incomplete read: %d != %d\n", 591*0Sstevel@tonic-gate i, n); 592*0Sstevel@tonic-gate close(nfd); 593*0Sstevel@tonic-gate free(in); 594*0Sstevel@tonic-gate return 1; 595*0Sstevel@tonic-gate } 596*0Sstevel@tonic-gate } else { 597*0Sstevel@tonic-gate ipn.ipn_dsize = 0; 598*0Sstevel@tonic-gate in = (nat_save_t *)malloc(sizeof(*in)); 599*0Sstevel@tonic-gate } 600*0Sstevel@tonic-gate bcopy((char *)ipnp, (char *)in, sizeof(ipn)); 601*0Sstevel@tonic-gate 602*0Sstevel@tonic-gate /* 603*0Sstevel@tonic-gate * Check to see if this is the first state entry that will 604*0Sstevel@tonic-gate * reference a particular rule and if so, flag it as such 605*0Sstevel@tonic-gate * else just adjust the rule pointer to become a pointer to 606*0Sstevel@tonic-gate * the other. We do this so we have a means later for tracking 607*0Sstevel@tonic-gate * who is referencing us when we get back the real pointer 608*0Sstevel@tonic-gate * in is_rule after doing the ioctl. 609*0Sstevel@tonic-gate */ 610*0Sstevel@tonic-gate nat = &in->ipn_nat; 611*0Sstevel@tonic-gate if (nat->nat_fr != NULL) { 612*0Sstevel@tonic-gate for (in1 = ipnhead; in1 != NULL; in1 = in1->ipn_next) 613*0Sstevel@tonic-gate if (in1->ipn_rule == nat->nat_fr) 614*0Sstevel@tonic-gate break; 615*0Sstevel@tonic-gate if (in1 == NULL) 616*0Sstevel@tonic-gate nat->nat_flags |= SI_NEWFR; 617*0Sstevel@tonic-gate else 618*0Sstevel@tonic-gate nat->nat_fr = &in1->ipn_fr; 619*0Sstevel@tonic-gate } 620*0Sstevel@tonic-gate 621*0Sstevel@tonic-gate /* 622*0Sstevel@tonic-gate * Use a tail-queue type list (add things to the end).. 623*0Sstevel@tonic-gate */ 624*0Sstevel@tonic-gate in->ipn_next = NULL; 625*0Sstevel@tonic-gate if (!ipnhead) 626*0Sstevel@tonic-gate ipnhead = in; 627*0Sstevel@tonic-gate if (ipntail) 628*0Sstevel@tonic-gate ipntail->ipn_next = in; 629*0Sstevel@tonic-gate ipntail = in; 630*0Sstevel@tonic-gate } while (1); 631*0Sstevel@tonic-gate 632*0Sstevel@tonic-gate close(nfd); 633*0Sstevel@tonic-gate 634*0Sstevel@tonic-gate for (in = ipnhead; in; in = in->ipn_next) { 635*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 636*0Sstevel@tonic-gate printf("Loading new NAT table entry\n"); 637*0Sstevel@tonic-gate nat = &in->ipn_nat; 638*0Sstevel@tonic-gate if (nat->nat_flags & SI_NEWFR) { 639*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 640*0Sstevel@tonic-gate printf("Loading new filter rule\n"); 641*0Sstevel@tonic-gate } 642*0Sstevel@tonic-gate if (!(opts & OPT_DONOTHING)) 643*0Sstevel@tonic-gate if (ioctl(fd, SIOCSTPUT, &in)) { 644*0Sstevel@tonic-gate perror("SIOCSTPUT"); 645*0Sstevel@tonic-gate return 1; 646*0Sstevel@tonic-gate } 647*0Sstevel@tonic-gate 648*0Sstevel@tonic-gate if (nat->nat_flags & SI_NEWFR) { 649*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 650*0Sstevel@tonic-gate printf("Real rule addr %p\n", nat->nat_fr); 651*0Sstevel@tonic-gate for (in1 = in->ipn_next; in1; in1 = in1->ipn_next) 652*0Sstevel@tonic-gate if (in1->ipn_rule == &in->ipn_fr) 653*0Sstevel@tonic-gate in1->ipn_rule = nat->nat_fr; 654*0Sstevel@tonic-gate } 655*0Sstevel@tonic-gate } 656*0Sstevel@tonic-gate 657*0Sstevel@tonic-gate return 0; 658*0Sstevel@tonic-gate } 659*0Sstevel@tonic-gate 660*0Sstevel@tonic-gate 661*0Sstevel@tonic-gate int writenat(fd, file) 662*0Sstevel@tonic-gate int fd; 663*0Sstevel@tonic-gate char *file; 664*0Sstevel@tonic-gate { 665*0Sstevel@tonic-gate nat_save_t *ipnp = NULL, *next = NULL; 666*0Sstevel@tonic-gate int nfd = -1; 667*0Sstevel@tonic-gate natget_t ng; 668*0Sstevel@tonic-gate 669*0Sstevel@tonic-gate if (!file) 670*0Sstevel@tonic-gate file = IPF_NATFILE; 671*0Sstevel@tonic-gate 672*0Sstevel@tonic-gate nfd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0600); 673*0Sstevel@tonic-gate if (nfd == -1) { 674*0Sstevel@tonic-gate fprintf(stderr, "%s ", file); 675*0Sstevel@tonic-gate perror("nat:open"); 676*0Sstevel@tonic-gate return 1; 677*0Sstevel@tonic-gate } 678*0Sstevel@tonic-gate 679*0Sstevel@tonic-gate 680*0Sstevel@tonic-gate do { 681*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 682*0Sstevel@tonic-gate printf("Getting nat from addr %p\n", ipnp); 683*0Sstevel@tonic-gate ng.ng_ptr = next; 684*0Sstevel@tonic-gate ng.ng_sz = 0; 685*0Sstevel@tonic-gate if (ioctl(fd, SIOCSTGSZ, &ng)) { 686*0Sstevel@tonic-gate perror("nat:SIOCSTGSZ"); 687*0Sstevel@tonic-gate close(nfd); 688*0Sstevel@tonic-gate if (ipnp != NULL) 689*0Sstevel@tonic-gate free(ipnp); 690*0Sstevel@tonic-gate return 1; 691*0Sstevel@tonic-gate } 692*0Sstevel@tonic-gate 693*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 694*0Sstevel@tonic-gate printf("NAT size %d from %p\n", ng.ng_sz, ng.ng_ptr); 695*0Sstevel@tonic-gate 696*0Sstevel@tonic-gate if (ng.ng_sz == 0) 697*0Sstevel@tonic-gate break; 698*0Sstevel@tonic-gate 699*0Sstevel@tonic-gate if (!ipnp) 700*0Sstevel@tonic-gate ipnp = malloc(ng.ng_sz); 701*0Sstevel@tonic-gate else 702*0Sstevel@tonic-gate ipnp = realloc((char *)ipnp, ng.ng_sz); 703*0Sstevel@tonic-gate if (!ipnp) { 704*0Sstevel@tonic-gate fprintf(stderr, 705*0Sstevel@tonic-gate "malloc for %d bytes failed\n", ng.ng_sz); 706*0Sstevel@tonic-gate break; 707*0Sstevel@tonic-gate } 708*0Sstevel@tonic-gate 709*0Sstevel@tonic-gate bzero((char *)ipnp, ng.ng_sz); 710*0Sstevel@tonic-gate ipnp->ipn_next = next; 711*0Sstevel@tonic-gate if (ioctl(fd, SIOCSTGET, &ipnp)) { 712*0Sstevel@tonic-gate if (errno == ENOENT) 713*0Sstevel@tonic-gate break; 714*0Sstevel@tonic-gate perror("nat:SIOCSTGET"); 715*0Sstevel@tonic-gate close(nfd); 716*0Sstevel@tonic-gate free(ipnp); 717*0Sstevel@tonic-gate return 1; 718*0Sstevel@tonic-gate } 719*0Sstevel@tonic-gate 720*0Sstevel@tonic-gate if (opts & OPT_VERBOSE) 721*0Sstevel@tonic-gate printf("Got nat next %p\n", ipnp->ipn_next); 722*0Sstevel@tonic-gate if (write(nfd, ipnp, ng.ng_sz) != ng.ng_sz) { 723*0Sstevel@tonic-gate perror("nat:write"); 724*0Sstevel@tonic-gate close(nfd); 725*0Sstevel@tonic-gate free(ipnp); 726*0Sstevel@tonic-gate return 1; 727*0Sstevel@tonic-gate } 728*0Sstevel@tonic-gate next = ipnp->ipn_next; 729*0Sstevel@tonic-gate } while (ipnp && next); 730*0Sstevel@tonic-gate if (ipnp != NULL) 731*0Sstevel@tonic-gate free(ipnp); 732*0Sstevel@tonic-gate close(nfd); 733*0Sstevel@tonic-gate 734*0Sstevel@tonic-gate return 0; 735*0Sstevel@tonic-gate } 736*0Sstevel@tonic-gate 737*0Sstevel@tonic-gate 738*0Sstevel@tonic-gate int writeall(dirname) 739*0Sstevel@tonic-gate char *dirname; 740*0Sstevel@tonic-gate { 741*0Sstevel@tonic-gate int fd, devfd; 742*0Sstevel@tonic-gate 743*0Sstevel@tonic-gate if (!dirname) 744*0Sstevel@tonic-gate dirname = IPF_SAVEDIR; 745*0Sstevel@tonic-gate 746*0Sstevel@tonic-gate if (chdir(dirname)) { 747*0Sstevel@tonic-gate perror("chdir(IPF_SAVEDIR)"); 748*0Sstevel@tonic-gate return 1; 749*0Sstevel@tonic-gate } 750*0Sstevel@tonic-gate 751*0Sstevel@tonic-gate fd = opendevice(NULL); 752*0Sstevel@tonic-gate if (fd == -1) 753*0Sstevel@tonic-gate return 1; 754*0Sstevel@tonic-gate if (setlock(fd, 1)) { 755*0Sstevel@tonic-gate close(fd); 756*0Sstevel@tonic-gate return 1; 757*0Sstevel@tonic-gate } 758*0Sstevel@tonic-gate 759*0Sstevel@tonic-gate devfd = opendevice(IPSTATE_NAME); 760*0Sstevel@tonic-gate if (devfd == -1) 761*0Sstevel@tonic-gate goto bad; 762*0Sstevel@tonic-gate if (writestate(devfd, NULL)) 763*0Sstevel@tonic-gate goto bad; 764*0Sstevel@tonic-gate close(devfd); 765*0Sstevel@tonic-gate 766*0Sstevel@tonic-gate devfd = opendevice(IPNAT_NAME); 767*0Sstevel@tonic-gate if (devfd == -1) 768*0Sstevel@tonic-gate goto bad; 769*0Sstevel@tonic-gate if (writenat(devfd, NULL)) 770*0Sstevel@tonic-gate goto bad; 771*0Sstevel@tonic-gate close(devfd); 772*0Sstevel@tonic-gate 773*0Sstevel@tonic-gate if (setlock(fd, 0)) { 774*0Sstevel@tonic-gate close(fd); 775*0Sstevel@tonic-gate return 1; 776*0Sstevel@tonic-gate } 777*0Sstevel@tonic-gate 778*0Sstevel@tonic-gate close(fd); 779*0Sstevel@tonic-gate return 0; 780*0Sstevel@tonic-gate 781*0Sstevel@tonic-gate bad: 782*0Sstevel@tonic-gate setlock(fd, 0); 783*0Sstevel@tonic-gate close(fd); 784*0Sstevel@tonic-gate return 1; 785*0Sstevel@tonic-gate } 786*0Sstevel@tonic-gate 787*0Sstevel@tonic-gate 788*0Sstevel@tonic-gate int readall(dirname) 789*0Sstevel@tonic-gate char *dirname; 790*0Sstevel@tonic-gate { 791*0Sstevel@tonic-gate int fd, devfd; 792*0Sstevel@tonic-gate 793*0Sstevel@tonic-gate if (!dirname) 794*0Sstevel@tonic-gate dirname = IPF_SAVEDIR; 795*0Sstevel@tonic-gate 796*0Sstevel@tonic-gate if (chdir(dirname)) { 797*0Sstevel@tonic-gate perror("chdir(IPF_SAVEDIR)"); 798*0Sstevel@tonic-gate return 1; 799*0Sstevel@tonic-gate } 800*0Sstevel@tonic-gate 801*0Sstevel@tonic-gate fd = opendevice(NULL); 802*0Sstevel@tonic-gate if (fd == -1) 803*0Sstevel@tonic-gate return 1; 804*0Sstevel@tonic-gate if (setlock(fd, 1)) { 805*0Sstevel@tonic-gate close(fd); 806*0Sstevel@tonic-gate return 1; 807*0Sstevel@tonic-gate } 808*0Sstevel@tonic-gate 809*0Sstevel@tonic-gate devfd = opendevice(IPSTATE_NAME); 810*0Sstevel@tonic-gate if (devfd == -1) 811*0Sstevel@tonic-gate return 1; 812*0Sstevel@tonic-gate if (readstate(devfd, NULL)) 813*0Sstevel@tonic-gate return 1; 814*0Sstevel@tonic-gate close(devfd); 815*0Sstevel@tonic-gate 816*0Sstevel@tonic-gate devfd = opendevice(IPNAT_NAME); 817*0Sstevel@tonic-gate if (devfd == -1) 818*0Sstevel@tonic-gate return 1; 819*0Sstevel@tonic-gate if (readnat(devfd, NULL)) 820*0Sstevel@tonic-gate return 1; 821*0Sstevel@tonic-gate close(devfd); 822*0Sstevel@tonic-gate 823*0Sstevel@tonic-gate if (setlock(fd, 0)) { 824*0Sstevel@tonic-gate close(fd); 825*0Sstevel@tonic-gate return 1; 826*0Sstevel@tonic-gate } 827*0Sstevel@tonic-gate 828*0Sstevel@tonic-gate return 0; 829*0Sstevel@tonic-gate } 830