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