17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * Copyright (C) 1999-2001, 2003 by Darren Reed. 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * See the IPFILTER.LICENCE file for details on licencing. 57c478bd9Sstevel@tonic-gate * 6*33f2fefdSDarren Reed * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 77c478bd9Sstevel@tonic-gate * Use is subject to license terms. 87c478bd9Sstevel@tonic-gate */ 97c478bd9Sstevel@tonic-gate 107c478bd9Sstevel@tonic-gate #ifdef __FreeBSD__ 117c478bd9Sstevel@tonic-gate # ifndef __FreeBSD_cc_version 127c478bd9Sstevel@tonic-gate # include <osreldate.h> 137c478bd9Sstevel@tonic-gate # else 147c478bd9Sstevel@tonic-gate # if __FreeBSD_cc_version < 430000 157c478bd9Sstevel@tonic-gate # include <osreldate.h> 167c478bd9Sstevel@tonic-gate # endif 177c478bd9Sstevel@tonic-gate # endif 187c478bd9Sstevel@tonic-gate #endif 197c478bd9Sstevel@tonic-gate #include <stdio.h> 207c478bd9Sstevel@tonic-gate #include <unistd.h> 217c478bd9Sstevel@tonic-gate #include <string.h> 227c478bd9Sstevel@tonic-gate #include <fcntl.h> 237c478bd9Sstevel@tonic-gate #include <errno.h> 247c478bd9Sstevel@tonic-gate #if !defined(__SVR4) && !defined(__GNUC__) 257c478bd9Sstevel@tonic-gate #include <strings.h> 267c478bd9Sstevel@tonic-gate #endif 277c478bd9Sstevel@tonic-gate #include <sys/types.h> 287c478bd9Sstevel@tonic-gate #include <sys/param.h> 297c478bd9Sstevel@tonic-gate #include <sys/file.h> 307c478bd9Sstevel@tonic-gate #include <stdlib.h> 317c478bd9Sstevel@tonic-gate #include <stddef.h> 327c478bd9Sstevel@tonic-gate #include <sys/socket.h> 337c478bd9Sstevel@tonic-gate #include <sys/ioctl.h> 347c478bd9Sstevel@tonic-gate #include <netinet/in.h> 357c478bd9Sstevel@tonic-gate #include <netinet/in_systm.h> 367c478bd9Sstevel@tonic-gate #include <sys/time.h> 377c478bd9Sstevel@tonic-gate #include <net/if.h> 387c478bd9Sstevel@tonic-gate #if __FreeBSD_version >= 300000 397c478bd9Sstevel@tonic-gate # include <net/if_var.h> 407c478bd9Sstevel@tonic-gate #endif 417c478bd9Sstevel@tonic-gate #include <netinet/ip.h> 427c478bd9Sstevel@tonic-gate #include <netdb.h> 437c478bd9Sstevel@tonic-gate #include <arpa/nameser.h> 447c478bd9Sstevel@tonic-gate #include <resolv.h> 457c478bd9Sstevel@tonic-gate #include "ipf.h" 46ab25eeb5Syz155240 #include "netinet/ipl.h" 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate #if !defined(lint) 49ab25eeb5Syz155240 static const char rcsid[] = "@(#)Id: ipfs.c,v 1.12 2003/12/01 01:56:53 darrenr Exp"; 507c478bd9Sstevel@tonic-gate #endif 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate #ifndef IPF_SAVEDIR 537c478bd9Sstevel@tonic-gate # define IPF_SAVEDIR "/var/db/ipf" 547c478bd9Sstevel@tonic-gate #endif 557c478bd9Sstevel@tonic-gate #ifndef IPF_NATFILE 567c478bd9Sstevel@tonic-gate # define IPF_NATFILE "ipnat.ipf" 577c478bd9Sstevel@tonic-gate #endif 587c478bd9Sstevel@tonic-gate #ifndef IPF_STATEFILE 597c478bd9Sstevel@tonic-gate # define IPF_STATEFILE "ipstate.ipf" 607c478bd9Sstevel@tonic-gate #endif 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate #if !defined(__SVR4) && defined(__GNUC__) 637c478bd9Sstevel@tonic-gate extern char *index __P((const char *, int)); 647c478bd9Sstevel@tonic-gate #endif 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate extern char *optarg; 677c478bd9Sstevel@tonic-gate extern int optind; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate int main __P((int, char *[])); 707c478bd9Sstevel@tonic-gate void usage __P((void)); 717c478bd9Sstevel@tonic-gate int changestateif __P((char *, char *)); 727c478bd9Sstevel@tonic-gate int changenatif __P((char *, char *)); 737c478bd9Sstevel@tonic-gate int readstate __P((int, char *)); 747c478bd9Sstevel@tonic-gate int readnat __P((int, char *)); 757c478bd9Sstevel@tonic-gate int writestate __P((int, char *)); 767c478bd9Sstevel@tonic-gate int opendevice __P((char *)); 777c478bd9Sstevel@tonic-gate void closedevice __P((int)); 787c478bd9Sstevel@tonic-gate int setlock __P((int, int)); 797c478bd9Sstevel@tonic-gate int writeall __P((char *)); 807c478bd9Sstevel@tonic-gate int readall __P((char *)); 817c478bd9Sstevel@tonic-gate int writenat __P((int, char *)); 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate int opts = 0; 847c478bd9Sstevel@tonic-gate char *progname; 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate void usage() 887c478bd9Sstevel@tonic-gate { 897c478bd9Sstevel@tonic-gate fprintf(stderr, "usage: %s [-nv] -l\n", progname); 907c478bd9Sstevel@tonic-gate fprintf(stderr, "usage: %s [-nv] -u\n", progname); 917c478bd9Sstevel@tonic-gate fprintf(stderr, "usage: %s [-nv] [-d <dir>] -R\n", progname); 927c478bd9Sstevel@tonic-gate fprintf(stderr, "usage: %s [-nv] [-d <dir>] -W\n", progname); 93*33f2fefdSDarren Reed fprintf(stderr, "usage: %s [-nv] [-N|-S] [-f <file>] -r\n", progname); 94*33f2fefdSDarren Reed fprintf(stderr, "usage: %s [-nv] [-N|-S] [-f <file>] -w\n", progname); 95*33f2fefdSDarren Reed fprintf(stderr, "usage: %s [-nv] [-N|-S] -f <file> -i <if1>,<if2>\n", 967c478bd9Sstevel@tonic-gate progname); 977c478bd9Sstevel@tonic-gate exit(1); 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * Change interface names in state information saved out to disk. 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate int changestateif(ifs, fname) 1057c478bd9Sstevel@tonic-gate char *ifs, *fname; 1067c478bd9Sstevel@tonic-gate { 1077c478bd9Sstevel@tonic-gate int fd, olen, nlen, rw; 1087c478bd9Sstevel@tonic-gate ipstate_save_t ips; 1097c478bd9Sstevel@tonic-gate off_t pos; 1107c478bd9Sstevel@tonic-gate char *s; 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate s = strchr(ifs, ','); 1137c478bd9Sstevel@tonic-gate if (!s) 1147c478bd9Sstevel@tonic-gate usage(); 1157c478bd9Sstevel@tonic-gate *s++ = '\0'; 1167c478bd9Sstevel@tonic-gate nlen = strlen(s); 1177c478bd9Sstevel@tonic-gate olen = strlen(ifs); 1187c478bd9Sstevel@tonic-gate if (nlen >= sizeof(ips.ips_is.is_ifname) || 1197c478bd9Sstevel@tonic-gate olen >= sizeof(ips.ips_is.is_ifname)) 1207c478bd9Sstevel@tonic-gate usage(); 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate fd = open(fname, O_RDWR); 1237c478bd9Sstevel@tonic-gate if (fd == -1) { 1247c478bd9Sstevel@tonic-gate perror("open"); 1257c478bd9Sstevel@tonic-gate exit(1); 1267c478bd9Sstevel@tonic-gate } 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate for (pos = 0; read(fd, &ips, sizeof(ips)) == sizeof(ips); ) { 1297c478bd9Sstevel@tonic-gate rw = 0; 1307c478bd9Sstevel@tonic-gate if (!strncmp(ips.ips_is.is_ifname[0], ifs, olen + 1)) { 1317c478bd9Sstevel@tonic-gate strcpy(ips.ips_is.is_ifname[0], s); 1327c478bd9Sstevel@tonic-gate rw = 1; 1337c478bd9Sstevel@tonic-gate } 1347c478bd9Sstevel@tonic-gate if (!strncmp(ips.ips_is.is_ifname[1], ifs, olen + 1)) { 1357c478bd9Sstevel@tonic-gate strcpy(ips.ips_is.is_ifname[1], s); 1367c478bd9Sstevel@tonic-gate rw = 1; 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate if (rw == 1) { 1397c478bd9Sstevel@tonic-gate if (lseek(fd, pos, SEEK_SET) != pos) { 1407c478bd9Sstevel@tonic-gate perror("lseek"); 1417c478bd9Sstevel@tonic-gate exit(1); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate if (write(fd, &ips, sizeof(ips)) != sizeof(ips)) { 1447c478bd9Sstevel@tonic-gate perror("write"); 1457c478bd9Sstevel@tonic-gate exit(1); 1467c478bd9Sstevel@tonic-gate } 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate pos = lseek(fd, 0, SEEK_CUR); 1497c478bd9Sstevel@tonic-gate } 1507c478bd9Sstevel@tonic-gate close(fd); 1517c478bd9Sstevel@tonic-gate 1527c478bd9Sstevel@tonic-gate return 0; 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate 1567c478bd9Sstevel@tonic-gate /* 1577c478bd9Sstevel@tonic-gate * Change interface names in NAT information saved out to disk. 1587c478bd9Sstevel@tonic-gate */ 1597c478bd9Sstevel@tonic-gate int changenatif(ifs, fname) 1607c478bd9Sstevel@tonic-gate char *ifs, *fname; 1617c478bd9Sstevel@tonic-gate { 1627c478bd9Sstevel@tonic-gate int fd, olen, nlen, rw; 1637c478bd9Sstevel@tonic-gate nat_save_t ipn; 1647c478bd9Sstevel@tonic-gate nat_t *nat; 1657c478bd9Sstevel@tonic-gate off_t pos; 1667c478bd9Sstevel@tonic-gate char *s; 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate s = strchr(ifs, ','); 1697c478bd9Sstevel@tonic-gate if (!s) 1707c478bd9Sstevel@tonic-gate usage(); 1717c478bd9Sstevel@tonic-gate *s++ = '\0'; 1727c478bd9Sstevel@tonic-gate nlen = strlen(s); 1737c478bd9Sstevel@tonic-gate olen = strlen(ifs); 1747c478bd9Sstevel@tonic-gate nat = &ipn.ipn_nat; 1757c478bd9Sstevel@tonic-gate if (nlen >= sizeof(nat->nat_ifnames[0]) || 1767c478bd9Sstevel@tonic-gate olen >= sizeof(nat->nat_ifnames[0])) 1777c478bd9Sstevel@tonic-gate usage(); 1787c478bd9Sstevel@tonic-gate 1797c478bd9Sstevel@tonic-gate fd = open(fname, O_RDWR); 1807c478bd9Sstevel@tonic-gate if (fd == -1) { 1817c478bd9Sstevel@tonic-gate perror("open"); 1827c478bd9Sstevel@tonic-gate exit(1); 1837c478bd9Sstevel@tonic-gate } 1847c478bd9Sstevel@tonic-gate 1857c478bd9Sstevel@tonic-gate for (pos = 0; read(fd, &ipn, sizeof(ipn)) == sizeof(ipn); ) { 1867c478bd9Sstevel@tonic-gate rw = 0; 1877c478bd9Sstevel@tonic-gate if (!strncmp(nat->nat_ifnames[0], ifs, olen + 1)) { 1887c478bd9Sstevel@tonic-gate strcpy(nat->nat_ifnames[0], s); 1897c478bd9Sstevel@tonic-gate rw = 1; 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate if (!strncmp(nat->nat_ifnames[1], ifs, olen + 1)) { 1927c478bd9Sstevel@tonic-gate strcpy(nat->nat_ifnames[1], s); 1937c478bd9Sstevel@tonic-gate rw = 1; 1947c478bd9Sstevel@tonic-gate } 1957c478bd9Sstevel@tonic-gate if (rw == 1) { 1967c478bd9Sstevel@tonic-gate if (lseek(fd, pos, SEEK_SET) != pos) { 1977c478bd9Sstevel@tonic-gate perror("lseek"); 1987c478bd9Sstevel@tonic-gate exit(1); 1997c478bd9Sstevel@tonic-gate } 2007c478bd9Sstevel@tonic-gate if (write(fd, &ipn, sizeof(ipn)) != sizeof(ipn)) { 2017c478bd9Sstevel@tonic-gate perror("write"); 2027c478bd9Sstevel@tonic-gate exit(1); 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate } 2057c478bd9Sstevel@tonic-gate pos = lseek(fd, 0, SEEK_CUR); 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate close(fd); 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate return 0; 2107c478bd9Sstevel@tonic-gate } 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate 2137c478bd9Sstevel@tonic-gate int main(argc,argv) 2147c478bd9Sstevel@tonic-gate int argc; 2157c478bd9Sstevel@tonic-gate char *argv[]; 2167c478bd9Sstevel@tonic-gate { 2177c478bd9Sstevel@tonic-gate int c, lock = -1, devfd = -1, err = 0, rw = -1, ns = -1, set = 0; 2187c478bd9Sstevel@tonic-gate char *dirname = NULL, *filename = NULL, *ifs = NULL; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate progname = argv[0]; 2217c478bd9Sstevel@tonic-gate while ((c = getopt(argc, argv, "d:f:lNnSRruvWw")) != -1) 2227c478bd9Sstevel@tonic-gate switch (c) 2237c478bd9Sstevel@tonic-gate { 2247c478bd9Sstevel@tonic-gate case 'd' : 2257c478bd9Sstevel@tonic-gate if ((set == 0) && !dirname && !filename) 2267c478bd9Sstevel@tonic-gate dirname = optarg; 2277c478bd9Sstevel@tonic-gate else 2287c478bd9Sstevel@tonic-gate usage(); 2297c478bd9Sstevel@tonic-gate break; 2307c478bd9Sstevel@tonic-gate case 'f' : 2317c478bd9Sstevel@tonic-gate if ((set == 0) && !dirname && !filename) 2327c478bd9Sstevel@tonic-gate filename = optarg; 2337c478bd9Sstevel@tonic-gate else 2347c478bd9Sstevel@tonic-gate usage(); 2357c478bd9Sstevel@tonic-gate break; 2367c478bd9Sstevel@tonic-gate case 'i' : 2377c478bd9Sstevel@tonic-gate ifs = optarg; 2387c478bd9Sstevel@tonic-gate set = 1; 2397c478bd9Sstevel@tonic-gate break; 2407c478bd9Sstevel@tonic-gate case 'l' : 2417c478bd9Sstevel@tonic-gate if (filename || dirname || set) 2427c478bd9Sstevel@tonic-gate usage(); 2437c478bd9Sstevel@tonic-gate lock = 1; 2447c478bd9Sstevel@tonic-gate set = 1; 2457c478bd9Sstevel@tonic-gate break; 2467c478bd9Sstevel@tonic-gate case 'n' : 2477c478bd9Sstevel@tonic-gate opts |= OPT_DONOTHING; 2487c478bd9Sstevel@tonic-gate break; 2497c478bd9Sstevel@tonic-gate case 'N' : 2507c478bd9Sstevel@tonic-gate if ((ns >= 0) || dirname || (rw != -1) || set) 2517c478bd9Sstevel@tonic-gate usage(); 2527c478bd9Sstevel@tonic-gate ns = 0; 2537c478bd9Sstevel@tonic-gate set = 1; 2547c478bd9Sstevel@tonic-gate break; 2557c478bd9Sstevel@tonic-gate case 'r' : 256ab25eeb5Syz155240 if (dirname || (rw != -1) || (ns == -1)) 2577c478bd9Sstevel@tonic-gate usage(); 2587c478bd9Sstevel@tonic-gate rw = 0; 2597c478bd9Sstevel@tonic-gate set = 1; 2607c478bd9Sstevel@tonic-gate break; 2617c478bd9Sstevel@tonic-gate case 'R' : 2627c478bd9Sstevel@tonic-gate rw = 2; 2637c478bd9Sstevel@tonic-gate set = 1; 2647c478bd9Sstevel@tonic-gate break; 2657c478bd9Sstevel@tonic-gate case 'S' : 2667c478bd9Sstevel@tonic-gate if ((ns >= 0) || dirname || (rw != -1) || set) 2677c478bd9Sstevel@tonic-gate usage(); 2687c478bd9Sstevel@tonic-gate ns = 1; 2697c478bd9Sstevel@tonic-gate set = 1; 2707c478bd9Sstevel@tonic-gate break; 2717c478bd9Sstevel@tonic-gate case 'u' : 2727c478bd9Sstevel@tonic-gate if (filename || dirname || set) 2737c478bd9Sstevel@tonic-gate usage(); 2747c478bd9Sstevel@tonic-gate lock = 0; 2757c478bd9Sstevel@tonic-gate set = 1; 2767c478bd9Sstevel@tonic-gate break; 2777c478bd9Sstevel@tonic-gate case 'v' : 2787c478bd9Sstevel@tonic-gate opts |= OPT_VERBOSE; 2797c478bd9Sstevel@tonic-gate break; 2807c478bd9Sstevel@tonic-gate case 'w' : 2817c478bd9Sstevel@tonic-gate if (dirname || (rw != -1) || (ns == -1)) 2827c478bd9Sstevel@tonic-gate usage(); 2837c478bd9Sstevel@tonic-gate rw = 1; 2847c478bd9Sstevel@tonic-gate set = 1; 2857c478bd9Sstevel@tonic-gate break; 2867c478bd9Sstevel@tonic-gate case 'W' : 2877c478bd9Sstevel@tonic-gate rw = 3; 2887c478bd9Sstevel@tonic-gate set = 1; 2897c478bd9Sstevel@tonic-gate break; 2907c478bd9Sstevel@tonic-gate case '?' : 2917c478bd9Sstevel@tonic-gate default : 2927c478bd9Sstevel@tonic-gate usage(); 2937c478bd9Sstevel@tonic-gate } 2947c478bd9Sstevel@tonic-gate 2957c478bd9Sstevel@tonic-gate if (ifs) { 2967c478bd9Sstevel@tonic-gate if (!filename || ns < 0) 2977c478bd9Sstevel@tonic-gate usage(); 2987c478bd9Sstevel@tonic-gate if (ns == 0) 2997c478bd9Sstevel@tonic-gate return changenatif(ifs, filename); 3007c478bd9Sstevel@tonic-gate else 3017c478bd9Sstevel@tonic-gate return changestateif(ifs, filename); 3027c478bd9Sstevel@tonic-gate } 3037c478bd9Sstevel@tonic-gate 3047c478bd9Sstevel@tonic-gate if ((ns >= 0) || (lock >= 0)) { 3057c478bd9Sstevel@tonic-gate if (lock >= 0) 3067c478bd9Sstevel@tonic-gate devfd = opendevice(NULL); 3077c478bd9Sstevel@tonic-gate else if (ns >= 0) { 3087c478bd9Sstevel@tonic-gate if (ns == 1) 3097c478bd9Sstevel@tonic-gate devfd = opendevice(IPSTATE_NAME); 3107c478bd9Sstevel@tonic-gate else if (ns == 0) 3117c478bd9Sstevel@tonic-gate devfd = opendevice(IPNAT_NAME); 3127c478bd9Sstevel@tonic-gate } 3137c478bd9Sstevel@tonic-gate if (devfd == -1) 3147c478bd9Sstevel@tonic-gate exit(1); 3157c478bd9Sstevel@tonic-gate } 3167c478bd9Sstevel@tonic-gate 3177c478bd9Sstevel@tonic-gate if (lock >= 0) 3187c478bd9Sstevel@tonic-gate err = setlock(devfd, lock); 3197c478bd9Sstevel@tonic-gate else if (rw >= 0) { 3207c478bd9Sstevel@tonic-gate if (rw & 1) { /* WRITE */ 3217c478bd9Sstevel@tonic-gate if (rw & 2) 3227c478bd9Sstevel@tonic-gate err = writeall(dirname); 3237c478bd9Sstevel@tonic-gate else { 3247c478bd9Sstevel@tonic-gate if (ns == 0) 3257c478bd9Sstevel@tonic-gate err = writenat(devfd, filename); 3267c478bd9Sstevel@tonic-gate else if (ns == 1) 3277c478bd9Sstevel@tonic-gate err = writestate(devfd, filename); 3287c478bd9Sstevel@tonic-gate } 3297c478bd9Sstevel@tonic-gate } else { 3307c478bd9Sstevel@tonic-gate if (rw & 2) 3317c478bd9Sstevel@tonic-gate err = readall(dirname); 3327c478bd9Sstevel@tonic-gate else { 3337c478bd9Sstevel@tonic-gate if (ns == 0) 3347c478bd9Sstevel@tonic-gate err = readnat(devfd, filename); 3357c478bd9Sstevel@tonic-gate else if (ns == 1) 3367c478bd9Sstevel@tonic-gate err = readstate(devfd, filename); 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate } 3397c478bd9Sstevel@tonic-gate } 3407c478bd9Sstevel@tonic-gate return err; 3417c478bd9Sstevel@tonic-gate } 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate int opendevice(ipfdev) 3457c478bd9Sstevel@tonic-gate char *ipfdev; 3467c478bd9Sstevel@tonic-gate { 3477c478bd9Sstevel@tonic-gate int fd = -1; 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate if (opts & OPT_DONOTHING) 3507c478bd9Sstevel@tonic-gate return -2; 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate if (!ipfdev) 3537c478bd9Sstevel@tonic-gate ipfdev = IPL_NAME; 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate if ((fd = open(ipfdev, O_RDWR)) == -1) 3567c478bd9Sstevel@tonic-gate if ((fd = open(ipfdev, O_RDONLY)) == -1) 3577c478bd9Sstevel@tonic-gate perror("open device"); 3587c478bd9Sstevel@tonic-gate return fd; 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate void closedevice(fd) 3637c478bd9Sstevel@tonic-gate int fd; 3647c478bd9Sstevel@tonic-gate { 3657c478bd9Sstevel@tonic-gate close(fd); 3667c478bd9Sstevel@tonic-gate } 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate int setlock(fd, lock) 3707c478bd9Sstevel@tonic-gate int fd, lock; 3717c478bd9Sstevel@tonic-gate { 3727c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 3737c478bd9Sstevel@tonic-gate printf("Turn lock %s\n", lock ? "on" : "off"); 3747c478bd9Sstevel@tonic-gate if (!(opts & OPT_DONOTHING)) { 3757c478bd9Sstevel@tonic-gate if (ioctl(fd, SIOCSTLCK, &lock) == -1) { 3767c478bd9Sstevel@tonic-gate perror("SIOCSTLCK"); 3777c478bd9Sstevel@tonic-gate return 1; 3787c478bd9Sstevel@tonic-gate } 3797c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 3807c478bd9Sstevel@tonic-gate printf("Lock now %s\n", lock ? "on" : "off"); 3817c478bd9Sstevel@tonic-gate } 3827c478bd9Sstevel@tonic-gate return 0; 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate 3867c478bd9Sstevel@tonic-gate int writestate(fd, file) 3877c478bd9Sstevel@tonic-gate int fd; 3887c478bd9Sstevel@tonic-gate char *file; 3897c478bd9Sstevel@tonic-gate { 3907c478bd9Sstevel@tonic-gate ipstate_save_t ips, *ipsp; 391ab25eeb5Syz155240 ipfobj_t obj; 3927c478bd9Sstevel@tonic-gate int wfd = -1; 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate if (!file) 3957c478bd9Sstevel@tonic-gate file = IPF_STATEFILE; 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate wfd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0600); 3987c478bd9Sstevel@tonic-gate if (wfd == -1) { 3997c478bd9Sstevel@tonic-gate fprintf(stderr, "%s ", file); 4007c478bd9Sstevel@tonic-gate perror("state:open"); 4017c478bd9Sstevel@tonic-gate return 1; 4027c478bd9Sstevel@tonic-gate } 4037c478bd9Sstevel@tonic-gate 4047c478bd9Sstevel@tonic-gate ipsp = &ips; 405ab25eeb5Syz155240 bzero((char *)&obj, sizeof(obj)); 4067c478bd9Sstevel@tonic-gate bzero((char *)ipsp, sizeof(ips)); 4077c478bd9Sstevel@tonic-gate 408ab25eeb5Syz155240 obj.ipfo_rev = IPFILTER_VERSION; 409ab25eeb5Syz155240 obj.ipfo_size = sizeof(*ipsp); 410ab25eeb5Syz155240 obj.ipfo_type = IPFOBJ_STATESAVE; 411ab25eeb5Syz155240 obj.ipfo_ptr = ipsp; 412ab25eeb5Syz155240 4137c478bd9Sstevel@tonic-gate do { 414ab25eeb5Syz155240 4157c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 4167c478bd9Sstevel@tonic-gate printf("Getting state from addr %p\n", ips.ips_next); 417ab25eeb5Syz155240 if (ioctl(fd, SIOCSTGET, &obj)) { 4187c478bd9Sstevel@tonic-gate if (errno == ENOENT) 4197c478bd9Sstevel@tonic-gate break; 4207c478bd9Sstevel@tonic-gate perror("state:SIOCSTGET"); 4217c478bd9Sstevel@tonic-gate close(wfd); 4227c478bd9Sstevel@tonic-gate return 1; 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 4257c478bd9Sstevel@tonic-gate printf("Got state next %p\n", ips.ips_next); 4267c478bd9Sstevel@tonic-gate if (write(wfd, ipsp, sizeof(ips)) != sizeof(ips)) { 4277c478bd9Sstevel@tonic-gate perror("state:write"); 4287c478bd9Sstevel@tonic-gate close(wfd); 4297c478bd9Sstevel@tonic-gate return 1; 4307c478bd9Sstevel@tonic-gate } 4317c478bd9Sstevel@tonic-gate } while (ips.ips_next != NULL); 4327c478bd9Sstevel@tonic-gate close(wfd); 4337c478bd9Sstevel@tonic-gate 4347c478bd9Sstevel@tonic-gate return 0; 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate 4387c478bd9Sstevel@tonic-gate int readstate(fd, file) 4397c478bd9Sstevel@tonic-gate int fd; 4407c478bd9Sstevel@tonic-gate char *file; 4417c478bd9Sstevel@tonic-gate { 4427c478bd9Sstevel@tonic-gate ipstate_save_t ips, *is, *ipshead = NULL, *is1, *ipstail = NULL; 4437c478bd9Sstevel@tonic-gate int sfd = -1, i; 444ab25eeb5Syz155240 ipfobj_t obj; 4457c478bd9Sstevel@tonic-gate 4467c478bd9Sstevel@tonic-gate if (!file) 4477c478bd9Sstevel@tonic-gate file = IPF_STATEFILE; 4487c478bd9Sstevel@tonic-gate 4497c478bd9Sstevel@tonic-gate sfd = open(file, O_RDONLY, 0600); 4507c478bd9Sstevel@tonic-gate if (sfd == -1) { 4517c478bd9Sstevel@tonic-gate fprintf(stderr, "%s ", file); 4527c478bd9Sstevel@tonic-gate perror("open"); 4537c478bd9Sstevel@tonic-gate return 1; 4547c478bd9Sstevel@tonic-gate } 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate bzero((char *)&ips, sizeof(ips)); 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate /* 4597c478bd9Sstevel@tonic-gate * 1. Read all state information in. 4607c478bd9Sstevel@tonic-gate */ 4617c478bd9Sstevel@tonic-gate do { 4627c478bd9Sstevel@tonic-gate i = read(sfd, &ips, sizeof(ips)); 4637c478bd9Sstevel@tonic-gate if (i == -1) { 4647c478bd9Sstevel@tonic-gate perror("read"); 4657c478bd9Sstevel@tonic-gate close(sfd); 4667c478bd9Sstevel@tonic-gate return 1; 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate if (i == 0) 4697c478bd9Sstevel@tonic-gate break; 4707c478bd9Sstevel@tonic-gate if (i != sizeof(ips)) { 471ab25eeb5Syz155240 fprintf(stderr, "state:incomplete read: %d != %d\n", 472ab25eeb5Syz155240 i, (int)sizeof(ips)); 4737c478bd9Sstevel@tonic-gate close(sfd); 4747c478bd9Sstevel@tonic-gate return 1; 4757c478bd9Sstevel@tonic-gate } 4767c478bd9Sstevel@tonic-gate is = (ipstate_save_t *)malloc(sizeof(*is)); 4777c478bd9Sstevel@tonic-gate if(!is) { 4787c478bd9Sstevel@tonic-gate fprintf(stderr, "malloc failed\n"); 4797c478bd9Sstevel@tonic-gate return 1; 4807c478bd9Sstevel@tonic-gate } 4817c478bd9Sstevel@tonic-gate 4827c478bd9Sstevel@tonic-gate bcopy((char *)&ips, (char *)is, sizeof(ips)); 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate /* 4857c478bd9Sstevel@tonic-gate * Check to see if this is the first state entry that will 4867c478bd9Sstevel@tonic-gate * reference a particular rule and if so, flag it as such 4877c478bd9Sstevel@tonic-gate * else just adjust the rule pointer to become a pointer to 4887c478bd9Sstevel@tonic-gate * the other. We do this so we have a means later for tracking 4897c478bd9Sstevel@tonic-gate * who is referencing us when we get back the real pointer 4907c478bd9Sstevel@tonic-gate * in is_rule after doing the ioctl. 4917c478bd9Sstevel@tonic-gate */ 4927c478bd9Sstevel@tonic-gate for (is1 = ipshead; is1 != NULL; is1 = is1->ips_next) 4937c478bd9Sstevel@tonic-gate if (is1->ips_rule == is->ips_rule) 4947c478bd9Sstevel@tonic-gate break; 4957c478bd9Sstevel@tonic-gate if (is1 == NULL) 4967c478bd9Sstevel@tonic-gate is->ips_is.is_flags |= SI_NEWFR; 4977c478bd9Sstevel@tonic-gate else 4987c478bd9Sstevel@tonic-gate is->ips_rule = (void *)&is1->ips_rule; 4997c478bd9Sstevel@tonic-gate 5007c478bd9Sstevel@tonic-gate /* 5017c478bd9Sstevel@tonic-gate * Use a tail-queue type list (add things to the end).. 5027c478bd9Sstevel@tonic-gate */ 5037c478bd9Sstevel@tonic-gate is->ips_next = NULL; 5047c478bd9Sstevel@tonic-gate if (!ipshead) 5057c478bd9Sstevel@tonic-gate ipshead = is; 5067c478bd9Sstevel@tonic-gate if (ipstail) 5077c478bd9Sstevel@tonic-gate ipstail->ips_next = is; 5087c478bd9Sstevel@tonic-gate ipstail = is; 5097c478bd9Sstevel@tonic-gate } while (1); 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate close(sfd); 5127c478bd9Sstevel@tonic-gate 513ab25eeb5Syz155240 obj.ipfo_rev = IPFILTER_VERSION; 514ab25eeb5Syz155240 obj.ipfo_size = sizeof(*is); 515ab25eeb5Syz155240 obj.ipfo_type = IPFOBJ_STATESAVE; 516ab25eeb5Syz155240 5177c478bd9Sstevel@tonic-gate for (is = ipshead; is; is = is->ips_next) { 5187c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 5197c478bd9Sstevel@tonic-gate printf("Loading new state table entry\n"); 5207c478bd9Sstevel@tonic-gate if (is->ips_is.is_flags & SI_NEWFR) { 5217c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 5227c478bd9Sstevel@tonic-gate printf("Loading new filter rule\n"); 5237c478bd9Sstevel@tonic-gate } 524ab25eeb5Syz155240 525ab25eeb5Syz155240 obj.ipfo_ptr = is; 5267c478bd9Sstevel@tonic-gate if (!(opts & OPT_DONOTHING)) 527ab25eeb5Syz155240 if (ioctl(fd, SIOCSTPUT, &obj)) { 5287c478bd9Sstevel@tonic-gate perror("SIOCSTPUT"); 5297c478bd9Sstevel@tonic-gate return 1; 5307c478bd9Sstevel@tonic-gate } 5317c478bd9Sstevel@tonic-gate 5327c478bd9Sstevel@tonic-gate if (is->ips_is.is_flags & SI_NEWFR) { 5337c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 5347c478bd9Sstevel@tonic-gate printf("Real rule addr %p\n", is->ips_rule); 5357c478bd9Sstevel@tonic-gate for (is1 = is->ips_next; is1; is1 = is1->ips_next) 5367c478bd9Sstevel@tonic-gate if (is1->ips_rule == (frentry_t *)&is->ips_rule) 5377c478bd9Sstevel@tonic-gate is1->ips_rule = is->ips_rule; 5387c478bd9Sstevel@tonic-gate } 5397c478bd9Sstevel@tonic-gate } 5407c478bd9Sstevel@tonic-gate 5417c478bd9Sstevel@tonic-gate return 0; 5427c478bd9Sstevel@tonic-gate } 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate 5457c478bd9Sstevel@tonic-gate int readnat(fd, file) 5467c478bd9Sstevel@tonic-gate int fd; 5477c478bd9Sstevel@tonic-gate char *file; 5487c478bd9Sstevel@tonic-gate { 549ab25eeb5Syz155240 nat_save_t ipn, *in, *ipnhead = NULL, *in1, *ipntail = NULL; 550ab25eeb5Syz155240 ipfobj_t obj; 5517c478bd9Sstevel@tonic-gate int nfd, i; 5527c478bd9Sstevel@tonic-gate nat_t *nat; 553ab25eeb5Syz155240 char *s; 554ab25eeb5Syz155240 int n; 5557c478bd9Sstevel@tonic-gate 5567c478bd9Sstevel@tonic-gate nfd = -1; 5577c478bd9Sstevel@tonic-gate in = NULL; 5587c478bd9Sstevel@tonic-gate ipnhead = NULL; 5597c478bd9Sstevel@tonic-gate ipntail = NULL; 5607c478bd9Sstevel@tonic-gate 5617c478bd9Sstevel@tonic-gate if (!file) 5627c478bd9Sstevel@tonic-gate file = IPF_NATFILE; 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate nfd = open(file, O_RDONLY); 5657c478bd9Sstevel@tonic-gate if (nfd == -1) { 5667c478bd9Sstevel@tonic-gate fprintf(stderr, "%s ", file); 5677c478bd9Sstevel@tonic-gate perror("nat:open"); 5687c478bd9Sstevel@tonic-gate return 1; 5697c478bd9Sstevel@tonic-gate } 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate bzero((char *)&ipn, sizeof(ipn)); 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate /* 5747c478bd9Sstevel@tonic-gate * 1. Read all state information in. 5757c478bd9Sstevel@tonic-gate */ 5767c478bd9Sstevel@tonic-gate do { 5777c478bd9Sstevel@tonic-gate i = read(nfd, &ipn, sizeof(ipn)); 5787c478bd9Sstevel@tonic-gate if (i == -1) { 5797c478bd9Sstevel@tonic-gate perror("read"); 5807c478bd9Sstevel@tonic-gate close(nfd); 5817c478bd9Sstevel@tonic-gate return 1; 5827c478bd9Sstevel@tonic-gate } 5837c478bd9Sstevel@tonic-gate if (i == 0) 5847c478bd9Sstevel@tonic-gate break; 5857c478bd9Sstevel@tonic-gate if (i != sizeof(ipn)) { 586ab25eeb5Syz155240 fprintf(stderr, "nat:incomplete read: %d != %d\n", 587ab25eeb5Syz155240 i, (int)sizeof(ipn)); 5887c478bd9Sstevel@tonic-gate close(nfd); 5897c478bd9Sstevel@tonic-gate return 1; 5907c478bd9Sstevel@tonic-gate } 5917c478bd9Sstevel@tonic-gate 592ab25eeb5Syz155240 in = (nat_save_t *)malloc(ipn.ipn_dsize); 5937c478bd9Sstevel@tonic-gate if (!in) 5947c478bd9Sstevel@tonic-gate break; 5957c478bd9Sstevel@tonic-gate 596ab25eeb5Syz155240 if (ipn.ipn_dsize > sizeof(ipn)) { 597ab25eeb5Syz155240 n = ipn.ipn_dsize - sizeof(ipn); 598ab25eeb5Syz155240 if (n > 0) { 599ab25eeb5Syz155240 s = in->ipn_data + sizeof(in->ipn_data); 6007c478bd9Sstevel@tonic-gate i = read(nfd, s, n); 6017c478bd9Sstevel@tonic-gate if (i == 0) 6027c478bd9Sstevel@tonic-gate break; 6037c478bd9Sstevel@tonic-gate if (i != n) { 604ab25eeb5Syz155240 fprintf(stderr, 605ab25eeb5Syz155240 "nat:incomplete read: %d != %d\n", 6067c478bd9Sstevel@tonic-gate i, n); 6077c478bd9Sstevel@tonic-gate close(nfd); 6087c478bd9Sstevel@tonic-gate return 1; 6097c478bd9Sstevel@tonic-gate } 6107c478bd9Sstevel@tonic-gate } 611ab25eeb5Syz155240 } 612ab25eeb5Syz155240 bcopy((char *)&ipn, (char *)in, sizeof(ipn)); 6137c478bd9Sstevel@tonic-gate 6147c478bd9Sstevel@tonic-gate /* 615ab25eeb5Syz155240 * Check to see if this is the first NAT entry that will 6167c478bd9Sstevel@tonic-gate * reference a particular rule and if so, flag it as such 6177c478bd9Sstevel@tonic-gate * else just adjust the rule pointer to become a pointer to 6187c478bd9Sstevel@tonic-gate * the other. We do this so we have a means later for tracking 6197c478bd9Sstevel@tonic-gate * who is referencing us when we get back the real pointer 6207c478bd9Sstevel@tonic-gate * in is_rule after doing the ioctl. 6217c478bd9Sstevel@tonic-gate */ 6227c478bd9Sstevel@tonic-gate nat = &in->ipn_nat; 6237c478bd9Sstevel@tonic-gate if (nat->nat_fr != NULL) { 6247c478bd9Sstevel@tonic-gate for (in1 = ipnhead; in1 != NULL; in1 = in1->ipn_next) 6257c478bd9Sstevel@tonic-gate if (in1->ipn_rule == nat->nat_fr) 6267c478bd9Sstevel@tonic-gate break; 6277c478bd9Sstevel@tonic-gate if (in1 == NULL) 6287c478bd9Sstevel@tonic-gate nat->nat_flags |= SI_NEWFR; 6297c478bd9Sstevel@tonic-gate else 6307c478bd9Sstevel@tonic-gate nat->nat_fr = &in1->ipn_fr; 6317c478bd9Sstevel@tonic-gate } 6327c478bd9Sstevel@tonic-gate 6337c478bd9Sstevel@tonic-gate /* 6347c478bd9Sstevel@tonic-gate * Use a tail-queue type list (add things to the end).. 6357c478bd9Sstevel@tonic-gate */ 6367c478bd9Sstevel@tonic-gate in->ipn_next = NULL; 6377c478bd9Sstevel@tonic-gate if (!ipnhead) 6387c478bd9Sstevel@tonic-gate ipnhead = in; 6397c478bd9Sstevel@tonic-gate if (ipntail) 6407c478bd9Sstevel@tonic-gate ipntail->ipn_next = in; 6417c478bd9Sstevel@tonic-gate ipntail = in; 6427c478bd9Sstevel@tonic-gate } while (1); 6437c478bd9Sstevel@tonic-gate 6447c478bd9Sstevel@tonic-gate close(nfd); 645ab25eeb5Syz155240 nfd = -1; 646ab25eeb5Syz155240 647ab25eeb5Syz155240 obj.ipfo_rev = IPFILTER_VERSION; 648ab25eeb5Syz155240 obj.ipfo_type = IPFOBJ_NATSAVE; 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate for (in = ipnhead; in; in = in->ipn_next) { 6517c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 6527c478bd9Sstevel@tonic-gate printf("Loading new NAT table entry\n"); 6537c478bd9Sstevel@tonic-gate nat = &in->ipn_nat; 6547c478bd9Sstevel@tonic-gate if (nat->nat_flags & SI_NEWFR) { 6557c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 6567c478bd9Sstevel@tonic-gate printf("Loading new filter rule\n"); 6577c478bd9Sstevel@tonic-gate } 658ab25eeb5Syz155240 659ab25eeb5Syz155240 obj.ipfo_ptr = in; 660ab25eeb5Syz155240 obj.ipfo_size = in->ipn_dsize; 6617c478bd9Sstevel@tonic-gate if (!(opts & OPT_DONOTHING)) 662ab25eeb5Syz155240 if (ioctl(fd, SIOCSTPUT, &obj)) { 663ab25eeb5Syz155240 fprintf(stderr, "in=%p:", in); 6647c478bd9Sstevel@tonic-gate perror("SIOCSTPUT"); 6657c478bd9Sstevel@tonic-gate return 1; 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate 6687c478bd9Sstevel@tonic-gate if (nat->nat_flags & SI_NEWFR) { 6697c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 6707c478bd9Sstevel@tonic-gate printf("Real rule addr %p\n", nat->nat_fr); 6717c478bd9Sstevel@tonic-gate for (in1 = in->ipn_next; in1; in1 = in1->ipn_next) 6727c478bd9Sstevel@tonic-gate if (in1->ipn_rule == &in->ipn_fr) 6737c478bd9Sstevel@tonic-gate in1->ipn_rule = nat->nat_fr; 6747c478bd9Sstevel@tonic-gate } 6757c478bd9Sstevel@tonic-gate } 6767c478bd9Sstevel@tonic-gate 6777c478bd9Sstevel@tonic-gate return 0; 6787c478bd9Sstevel@tonic-gate } 6797c478bd9Sstevel@tonic-gate 6807c478bd9Sstevel@tonic-gate 6817c478bd9Sstevel@tonic-gate int writenat(fd, file) 6827c478bd9Sstevel@tonic-gate int fd; 6837c478bd9Sstevel@tonic-gate char *file; 6847c478bd9Sstevel@tonic-gate { 6857c478bd9Sstevel@tonic-gate nat_save_t *ipnp = NULL, *next = NULL; 686ab25eeb5Syz155240 ipfobj_t obj; 6877c478bd9Sstevel@tonic-gate int nfd = -1; 6887c478bd9Sstevel@tonic-gate natget_t ng; 6897c478bd9Sstevel@tonic-gate 6907c478bd9Sstevel@tonic-gate if (!file) 6917c478bd9Sstevel@tonic-gate file = IPF_NATFILE; 6927c478bd9Sstevel@tonic-gate 6937c478bd9Sstevel@tonic-gate nfd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0600); 6947c478bd9Sstevel@tonic-gate if (nfd == -1) { 6957c478bd9Sstevel@tonic-gate fprintf(stderr, "%s ", file); 6967c478bd9Sstevel@tonic-gate perror("nat:open"); 6977c478bd9Sstevel@tonic-gate return 1; 6987c478bd9Sstevel@tonic-gate } 6997c478bd9Sstevel@tonic-gate 700ab25eeb5Syz155240 obj.ipfo_rev = IPFILTER_VERSION; 701ab25eeb5Syz155240 obj.ipfo_type = IPFOBJ_NATSAVE; 7027c478bd9Sstevel@tonic-gate 7037c478bd9Sstevel@tonic-gate do { 7047c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 7057c478bd9Sstevel@tonic-gate printf("Getting nat from addr %p\n", ipnp); 7067c478bd9Sstevel@tonic-gate ng.ng_ptr = next; 7077c478bd9Sstevel@tonic-gate ng.ng_sz = 0; 7087c478bd9Sstevel@tonic-gate if (ioctl(fd, SIOCSTGSZ, &ng)) { 7097c478bd9Sstevel@tonic-gate perror("nat:SIOCSTGSZ"); 7107c478bd9Sstevel@tonic-gate close(nfd); 7117c478bd9Sstevel@tonic-gate if (ipnp != NULL) 7127c478bd9Sstevel@tonic-gate free(ipnp); 7137c478bd9Sstevel@tonic-gate return 1; 7147c478bd9Sstevel@tonic-gate } 7157c478bd9Sstevel@tonic-gate 7167c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 7177c478bd9Sstevel@tonic-gate printf("NAT size %d from %p\n", ng.ng_sz, ng.ng_ptr); 7187c478bd9Sstevel@tonic-gate 7197c478bd9Sstevel@tonic-gate if (ng.ng_sz == 0) 7207c478bd9Sstevel@tonic-gate break; 7217c478bd9Sstevel@tonic-gate 7227c478bd9Sstevel@tonic-gate if (!ipnp) 7237c478bd9Sstevel@tonic-gate ipnp = malloc(ng.ng_sz); 7247c478bd9Sstevel@tonic-gate else 7257c478bd9Sstevel@tonic-gate ipnp = realloc((char *)ipnp, ng.ng_sz); 7267c478bd9Sstevel@tonic-gate if (!ipnp) { 7277c478bd9Sstevel@tonic-gate fprintf(stderr, 7287c478bd9Sstevel@tonic-gate "malloc for %d bytes failed\n", ng.ng_sz); 7297c478bd9Sstevel@tonic-gate break; 7307c478bd9Sstevel@tonic-gate } 7317c478bd9Sstevel@tonic-gate 7327c478bd9Sstevel@tonic-gate bzero((char *)ipnp, ng.ng_sz); 733ab25eeb5Syz155240 obj.ipfo_size = ng.ng_sz; 734ab25eeb5Syz155240 obj.ipfo_ptr = ipnp; 735ab25eeb5Syz155240 ipnp->ipn_dsize = ng.ng_sz; 7367c478bd9Sstevel@tonic-gate ipnp->ipn_next = next; 737ab25eeb5Syz155240 if (ioctl(fd, SIOCSTGET, &obj)) { 7387c478bd9Sstevel@tonic-gate if (errno == ENOENT) 7397c478bd9Sstevel@tonic-gate break; 7407c478bd9Sstevel@tonic-gate perror("nat:SIOCSTGET"); 7417c478bd9Sstevel@tonic-gate close(nfd); 7427c478bd9Sstevel@tonic-gate free(ipnp); 7437c478bd9Sstevel@tonic-gate return 1; 7447c478bd9Sstevel@tonic-gate } 7457c478bd9Sstevel@tonic-gate 7467c478bd9Sstevel@tonic-gate if (opts & OPT_VERBOSE) 747ab25eeb5Syz155240 printf("Got nat next %p ipn_dsize %d ng_sz %d\n", 748ab25eeb5Syz155240 ipnp->ipn_next, ipnp->ipn_dsize, ng.ng_sz); 749ab25eeb5Syz155240 if (write(nfd, ipnp, ipnp->ipn_dsize) != ipnp->ipn_dsize) { 7507c478bd9Sstevel@tonic-gate perror("nat:write"); 7517c478bd9Sstevel@tonic-gate close(nfd); 7527c478bd9Sstevel@tonic-gate free(ipnp); 7537c478bd9Sstevel@tonic-gate return 1; 7547c478bd9Sstevel@tonic-gate } 7557c478bd9Sstevel@tonic-gate next = ipnp->ipn_next; 7567c478bd9Sstevel@tonic-gate } while (ipnp && next); 7577c478bd9Sstevel@tonic-gate if (ipnp != NULL) 7587c478bd9Sstevel@tonic-gate free(ipnp); 7597c478bd9Sstevel@tonic-gate close(nfd); 7607c478bd9Sstevel@tonic-gate 7617c478bd9Sstevel@tonic-gate return 0; 7627c478bd9Sstevel@tonic-gate } 7637c478bd9Sstevel@tonic-gate 7647c478bd9Sstevel@tonic-gate 7657c478bd9Sstevel@tonic-gate int writeall(dirname) 7667c478bd9Sstevel@tonic-gate char *dirname; 7677c478bd9Sstevel@tonic-gate { 7687c478bd9Sstevel@tonic-gate int fd, devfd; 7697c478bd9Sstevel@tonic-gate 7707c478bd9Sstevel@tonic-gate if (!dirname) 7717c478bd9Sstevel@tonic-gate dirname = IPF_SAVEDIR; 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate if (chdir(dirname)) { 774ab25eeb5Syz155240 fprintf(stderr, "IPF_SAVEDIR=%s: ", dirname); 7757c478bd9Sstevel@tonic-gate perror("chdir(IPF_SAVEDIR)"); 7767c478bd9Sstevel@tonic-gate return 1; 7777c478bd9Sstevel@tonic-gate } 7787c478bd9Sstevel@tonic-gate 7797c478bd9Sstevel@tonic-gate fd = opendevice(NULL); 7807c478bd9Sstevel@tonic-gate if (fd == -1) 7817c478bd9Sstevel@tonic-gate return 1; 7827c478bd9Sstevel@tonic-gate if (setlock(fd, 1)) { 7837c478bd9Sstevel@tonic-gate close(fd); 7847c478bd9Sstevel@tonic-gate return 1; 7857c478bd9Sstevel@tonic-gate } 7867c478bd9Sstevel@tonic-gate 7877c478bd9Sstevel@tonic-gate devfd = opendevice(IPSTATE_NAME); 7887c478bd9Sstevel@tonic-gate if (devfd == -1) 7897c478bd9Sstevel@tonic-gate goto bad; 7907c478bd9Sstevel@tonic-gate if (writestate(devfd, NULL)) 7917c478bd9Sstevel@tonic-gate goto bad; 7927c478bd9Sstevel@tonic-gate close(devfd); 7937c478bd9Sstevel@tonic-gate 7947c478bd9Sstevel@tonic-gate devfd = opendevice(IPNAT_NAME); 7957c478bd9Sstevel@tonic-gate if (devfd == -1) 7967c478bd9Sstevel@tonic-gate goto bad; 7977c478bd9Sstevel@tonic-gate if (writenat(devfd, NULL)) 7987c478bd9Sstevel@tonic-gate goto bad; 7997c478bd9Sstevel@tonic-gate close(devfd); 8007c478bd9Sstevel@tonic-gate 8017c478bd9Sstevel@tonic-gate if (setlock(fd, 0)) { 8027c478bd9Sstevel@tonic-gate close(fd); 8037c478bd9Sstevel@tonic-gate return 1; 8047c478bd9Sstevel@tonic-gate } 8057c478bd9Sstevel@tonic-gate 8067c478bd9Sstevel@tonic-gate close(fd); 8077c478bd9Sstevel@tonic-gate return 0; 8087c478bd9Sstevel@tonic-gate 8097c478bd9Sstevel@tonic-gate bad: 8107c478bd9Sstevel@tonic-gate setlock(fd, 0); 8117c478bd9Sstevel@tonic-gate close(fd); 8127c478bd9Sstevel@tonic-gate return 1; 8137c478bd9Sstevel@tonic-gate } 8147c478bd9Sstevel@tonic-gate 8157c478bd9Sstevel@tonic-gate 8167c478bd9Sstevel@tonic-gate int readall(dirname) 8177c478bd9Sstevel@tonic-gate char *dirname; 8187c478bd9Sstevel@tonic-gate { 8197c478bd9Sstevel@tonic-gate int fd, devfd; 8207c478bd9Sstevel@tonic-gate 8217c478bd9Sstevel@tonic-gate if (!dirname) 8227c478bd9Sstevel@tonic-gate dirname = IPF_SAVEDIR; 8237c478bd9Sstevel@tonic-gate 8247c478bd9Sstevel@tonic-gate if (chdir(dirname)) { 8257c478bd9Sstevel@tonic-gate perror("chdir(IPF_SAVEDIR)"); 8267c478bd9Sstevel@tonic-gate return 1; 8277c478bd9Sstevel@tonic-gate } 8287c478bd9Sstevel@tonic-gate 8297c478bd9Sstevel@tonic-gate fd = opendevice(NULL); 8307c478bd9Sstevel@tonic-gate if (fd == -1) 8317c478bd9Sstevel@tonic-gate return 1; 8327c478bd9Sstevel@tonic-gate if (setlock(fd, 1)) { 8337c478bd9Sstevel@tonic-gate close(fd); 8347c478bd9Sstevel@tonic-gate return 1; 8357c478bd9Sstevel@tonic-gate } 8367c478bd9Sstevel@tonic-gate 8377c478bd9Sstevel@tonic-gate devfd = opendevice(IPSTATE_NAME); 8387c478bd9Sstevel@tonic-gate if (devfd == -1) 8397c478bd9Sstevel@tonic-gate return 1; 8407c478bd9Sstevel@tonic-gate if (readstate(devfd, NULL)) 8417c478bd9Sstevel@tonic-gate return 1; 8427c478bd9Sstevel@tonic-gate close(devfd); 8437c478bd9Sstevel@tonic-gate 8447c478bd9Sstevel@tonic-gate devfd = opendevice(IPNAT_NAME); 8457c478bd9Sstevel@tonic-gate if (devfd == -1) 8467c478bd9Sstevel@tonic-gate return 1; 8477c478bd9Sstevel@tonic-gate if (readnat(devfd, NULL)) 8487c478bd9Sstevel@tonic-gate return 1; 8497c478bd9Sstevel@tonic-gate close(devfd); 8507c478bd9Sstevel@tonic-gate 8517c478bd9Sstevel@tonic-gate if (setlock(fd, 0)) { 8527c478bd9Sstevel@tonic-gate close(fd); 8537c478bd9Sstevel@tonic-gate return 1; 8547c478bd9Sstevel@tonic-gate } 8557c478bd9Sstevel@tonic-gate 8567c478bd9Sstevel@tonic-gate return 0; 8577c478bd9Sstevel@tonic-gate } 858