4 * Copyright (C) 1999-2001, 2003 by Darren Reed.
6 * See the IPFILTER.LICENCE file for details on licencing.
9 # ifndef __FreeBSD_cc_version
10 # include <osreldate.h>
12 # if __FreeBSD_cc_version < 430000
13 # include <osreldate.h>
22 #if !defined(__SVR4) && !defined(__GNUC__)
25 #include <sys/types.h>
26 #include <sys/param.h>
30 #include <sys/socket.h>
31 #include <sys/ioctl.h>
32 #include <netinet/in.h>
33 #include <netinet/in_systm.h>
36 #if __FreeBSD_version >= 300000
37 # include <net/if_var.h>
39 #include <netinet/ip.h>
41 #include <arpa/nameser.h>
44 #include "netinet/ipl.h"
47 static const char rcsid[] = "@(#)Id: ipfs.c,v 1.12 2003/12/01 01:56:53 darrenr Exp";
51 # define IPF_SAVEDIR "/var/db/ipf"
54 # define IPF_NATFILE "ipnat.ipf"
57 # define IPF_STATEFILE "ipstate.ipf"
60 #if !defined(__SVR4) && defined(__GNUC__)
61 extern char *index __P((const char *, int));
67 int main __P((int, char *[]));
68 void usage __P((void));
69 int changestateif __P((char *, char *));
70 int changenatif __P((char *, char *));
71 int readstate __P((int, char *));
72 int readnat __P((int, char *));
73 int writestate __P((int, char *));
74 int opendevice __P((char *));
75 void closedevice __P((int));
76 int setlock __P((int, int));
77 int writeall __P((char *));
78 int readall __P((char *));
79 int writenat __P((int, char *));
87 fprintf(stderr, "usage: %s [-nv] -l\n", progname);
88 fprintf(stderr, "usage: %s [-nv] -u\n", progname);
89 fprintf(stderr, "usage: %s [-nv] [-d <dir>] -R\n", progname);
90 fprintf(stderr, "usage: %s [-nv] [-d <dir>] -W\n", progname);
91 fprintf(stderr, "usage: %s [-nNSv] [-f <file>] -r\n", progname);
92 fprintf(stderr, "usage: %s [-nNSv] [-f <file>] -w\n", progname);
93 fprintf(stderr, "usage: %s [-nNSv] -f <filename> -i <if1>,<if2>\n",
100 * Change interface names in state information saved out to disk.
102 int changestateif(ifs, fname)
105 int fd, olen, nlen, rw;
110 s = strchr(ifs, ',');
116 if (nlen >= sizeof(ips.ips_is.is_ifname) ||
117 olen >= sizeof(ips.ips_is.is_ifname))
120 fd = open(fname, O_RDWR);
126 for (pos = 0; read(fd, &ips, sizeof(ips)) == sizeof(ips); ) {
128 if (!strncmp(ips.ips_is.is_ifname[0], ifs, olen + 1)) {
129 strcpy(ips.ips_is.is_ifname[0], s);
132 if (!strncmp(ips.ips_is.is_ifname[1], ifs, olen + 1)) {
133 strcpy(ips.ips_is.is_ifname[1], s);
137 if (lseek(fd, pos, SEEK_SET) != pos) {
141 if (write(fd, &ips, sizeof(ips)) != sizeof(ips)) {
146 pos = lseek(fd, 0, SEEK_CUR);
155 * Change interface names in NAT information saved out to disk.
157 int changenatif(ifs, fname)
160 int fd, olen, nlen, rw;
166 s = strchr(ifs, ',');
173 if (nlen >= sizeof(nat->nat_ifnames[0]) ||
174 olen >= sizeof(nat->nat_ifnames[0]))
177 fd = open(fname, O_RDWR);
183 for (pos = 0; read(fd, &ipn, sizeof(ipn)) == sizeof(ipn); ) {
185 if (!strncmp(nat->nat_ifnames[0], ifs, olen + 1)) {
186 strcpy(nat->nat_ifnames[0], s);
189 if (!strncmp(nat->nat_ifnames[1], ifs, olen + 1)) {
190 strcpy(nat->nat_ifnames[1], s);
194 if (lseek(fd, pos, SEEK_SET) != pos) {
198 if (write(fd, &ipn, sizeof(ipn)) != sizeof(ipn)) {
203 pos = lseek(fd, 0, SEEK_CUR);
215 int c, lock = -1, devfd = -1, err = 0, rw = -1, ns = -1, set = 0;
216 char *dirname = NULL, *filename = NULL, *ifs = NULL;
219 while ((c = getopt(argc, argv, "d:f:lNnSRruvWw")) != -1)
223 if ((set == 0) && !dirname && !filename)
229 if ((set != 0) && !dirname && !filename)
239 if (filename || dirname || set)
245 opts |= OPT_DONOTHING;
248 if ((ns >= 0) || dirname || (rw != -1) || set)
254 if (dirname || (rw != -1) || (ns == -1))
264 if ((ns >= 0) || dirname || (rw != -1) || set)
270 if (filename || dirname || set)
279 if (dirname || (rw != -1) || (ns == -1))
294 if (!filename || ns < 0)
297 return changenatif(ifs, filename);
299 return changestateif(ifs, filename);
302 if ((ns >= 0) || (lock >= 0)) {
304 devfd = opendevice(NULL);
307 devfd = opendevice(IPSTATE_NAME);
309 devfd = opendevice(IPNAT_NAME);
316 err = setlock(devfd, lock);
318 if (rw & 1) { /* WRITE */
320 err = writeall(dirname);
323 err = writenat(devfd, filename);
325 err = writestate(devfd, filename);
329 err = readall(dirname);
332 err = readnat(devfd, filename);
334 err = readstate(devfd, filename);
342 int opendevice(ipfdev)
347 if (opts & OPT_DONOTHING)
353 if ((fd = open(ipfdev, O_RDWR)) == -1)
354 if ((fd = open(ipfdev, O_RDONLY)) == -1)
355 perror("open device");
367 int setlock(fd, lock)
370 if (opts & OPT_VERBOSE)
371 printf("Turn lock %s\n", lock ? "on" : "off");
372 if (!(opts & OPT_DONOTHING)) {
373 if (ioctl(fd, SIOCSTLCK, &lock) == -1) {
377 if (opts & OPT_VERBOSE)
378 printf("Lock now %s\n", lock ? "on" : "off");
384 int writestate(fd, file)
388 ipstate_save_t ips, *ipsp;
393 file = IPF_STATEFILE;
395 wfd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0600);
397 fprintf(stderr, "%s ", file);
398 perror("state:open");
403 bzero((char *)&obj, sizeof(obj));
404 bzero((char *)ipsp, sizeof(ips));
406 obj.ipfo_rev = IPFILTER_VERSION;
407 obj.ipfo_size = sizeof(*ipsp);
408 obj.ipfo_type = IPFOBJ_STATESAVE;
413 if (opts & OPT_VERBOSE)
414 printf("Getting state from addr %p\n", ips.ips_next);
415 if (ioctl(fd, SIOCSTGET, &obj)) {
418 perror("state:SIOCSTGET");
422 if (opts & OPT_VERBOSE)
423 printf("Got state next %p\n", ips.ips_next);
424 if (write(wfd, ipsp, sizeof(ips)) != sizeof(ips)) {
425 perror("state:write");
429 } while (ips.ips_next != NULL);
436 int readstate(fd, file)
440 ipstate_save_t ips, *is, *ipshead = NULL, *is1, *ipstail = NULL;
445 file = IPF_STATEFILE;
447 sfd = open(file, O_RDONLY, 0600);
449 fprintf(stderr, "%s ", file);
454 bzero((char *)&ips, sizeof(ips));
457 * 1. Read all state information in.
460 i = read(sfd, &ips, sizeof(ips));
467 if (i != sizeof(ips)) {
468 fprintf(stderr, "state:incomplete read: %d != %d\n",
469 i, (int)sizeof(ips));
472 is = (ipstate_save_t *)malloc(sizeof(*is));
474 fprintf(stderr, "malloc failed\n");
478 bcopy((char *)&ips, (char *)is, sizeof(ips));
481 * Check to see if this is the first state entry that will
482 * reference a particular rule and if so, flag it as such
483 * else just adjust the rule pointer to become a pointer to
484 * the other. We do this so we have a means later for tracking
485 * who is referencing us when we get back the real pointer
486 * in is_rule after doing the ioctl.
488 for (is1 = ipshead; is1 != NULL; is1 = is1->ips_next)
489 if (is1->ips_rule == is->ips_rule)
492 is->ips_is.is_flags |= SI_NEWFR;
494 is->ips_rule = (void *)&is1->ips_rule;
497 * Use a tail-queue type list (add things to the end)..
503 ipstail->ips_next = is;
509 obj.ipfo_rev = IPFILTER_VERSION;
510 obj.ipfo_size = sizeof(*is);
511 obj.ipfo_type = IPFOBJ_STATESAVE;
513 while ((is = ipshead) != NULL) {
514 if (opts & OPT_VERBOSE)
515 printf("Loading new state table entry\n");
516 if (is->ips_is.is_flags & SI_NEWFR) {
517 if (opts & OPT_VERBOSE)
518 printf("Loading new filter rule\n");
522 if (!(opts & OPT_DONOTHING))
523 if (ioctl(fd, SIOCSTPUT, &obj)) {
528 if (is->ips_is.is_flags & SI_NEWFR) {
529 if (opts & OPT_VERBOSE)
530 printf("Real rule addr %p\n", is->ips_rule);
531 for (is1 = is->ips_next; is1; is1 = is1->ips_next)
532 if (is1->ips_rule == (frentry_t *)&is->ips_rule)
533 is1->ips_rule = is->ips_rule;
536 ipshead = is->ips_next;
543 while ((is = ipshead) != NULL) {
544 ipshead = is->ips_next;
553 int readnat(fd, file)
557 nat_save_t ipn, *in, *ipnhead = NULL, *in1, *ipntail = NULL;
572 nfd = open(file, O_RDONLY);
574 fprintf(stderr, "%s ", file);
579 bzero((char *)&ipn, sizeof(ipn));
582 * 1. Read all state information in.
585 i = read(nfd, &ipn, sizeof(ipn));
592 if (i != sizeof(ipn)) {
593 fprintf(stderr, "nat:incomplete read: %d != %d\n",
594 i, (int)sizeof(ipn));
598 in = (nat_save_t *)malloc(ipn.ipn_dsize);
600 fprintf(stderr, "nat:cannot malloc nat save atruct\n");
604 if (ipn.ipn_dsize > sizeof(ipn)) {
605 n = ipn.ipn_dsize - sizeof(ipn);
607 s = in->ipn_data + sizeof(in->ipn_data);
613 "nat:incomplete read: %d != %d\n",
619 bcopy((char *)&ipn, (char *)in, sizeof(ipn));
622 * Check to see if this is the first NAT entry that will
623 * reference a particular rule and if so, flag it as such
624 * else just adjust the rule pointer to become a pointer to
625 * the other. We do this so we have a means later for tracking
626 * who is referencing us when we get back the real pointer
627 * in is_rule after doing the ioctl.
630 if (nat->nat_fr != NULL) {
631 for (in1 = ipnhead; in1 != NULL; in1 = in1->ipn_next)
632 if (in1->ipn_rule == nat->nat_fr)
635 nat->nat_flags |= SI_NEWFR;
637 nat->nat_fr = &in1->ipn_fr;
641 * Use a tail-queue type list (add things to the end)..
647 ipntail->ipn_next = in;
654 obj.ipfo_rev = IPFILTER_VERSION;
655 obj.ipfo_type = IPFOBJ_NATSAVE;
657 while ((in = ipnhead) != NULL) {
658 if (opts & OPT_VERBOSE)
659 printf("Loading new NAT table entry\n");
661 if (nat->nat_flags & SI_NEWFR) {
662 if (opts & OPT_VERBOSE)
663 printf("Loading new filter rule\n");
667 obj.ipfo_size = in->ipn_dsize;
668 if (!(opts & OPT_DONOTHING))
669 if (ioctl(fd, SIOCSTPUT, &obj)) {
670 fprintf(stderr, "in=%p:", in);
675 if (nat->nat_flags & SI_NEWFR) {
676 if (opts & OPT_VERBOSE)
677 printf("Real rule addr %p\n", nat->nat_fr);
678 for (in1 = in->ipn_next; in1; in1 = in1->ipn_next)
679 if (in1->ipn_rule == &in->ipn_fr)
680 in1->ipn_rule = nat->nat_fr;
683 ipnhead = in->ipn_next;
690 while ((in = ipnhead) != NULL) {
691 ipnhead = in->ipn_next;
700 int writenat(fd, file)
704 nat_save_t *ipnp = NULL, *next = NULL;
712 nfd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0600);
714 fprintf(stderr, "%s ", file);
719 obj.ipfo_rev = IPFILTER_VERSION;
720 obj.ipfo_type = IPFOBJ_NATSAVE;
723 if (opts & OPT_VERBOSE)
724 printf("Getting nat from addr %p\n", ipnp);
727 if (ioctl(fd, SIOCSTGSZ, &ng)) {
728 perror("nat:SIOCSTGSZ");
735 if (opts & OPT_VERBOSE)
736 printf("NAT size %d from %p\n", ng.ng_sz, ng.ng_ptr);
742 ipnp = malloc(ng.ng_sz);
744 ipnp = realloc((char *)ipnp, ng.ng_sz);
747 "malloc for %d bytes failed\n", ng.ng_sz);
751 bzero((char *)ipnp, ng.ng_sz);
752 obj.ipfo_size = ng.ng_sz;
754 ipnp->ipn_dsize = ng.ng_sz;
755 ipnp->ipn_next = next;
756 if (ioctl(fd, SIOCSTGET, &obj)) {
759 perror("nat:SIOCSTGET");
765 if (opts & OPT_VERBOSE)
766 printf("Got nat next %p ipn_dsize %d ng_sz %d\n",
767 ipnp->ipn_next, ipnp->ipn_dsize, ng.ng_sz);
768 if (write(nfd, ipnp, ipnp->ipn_dsize) != ipnp->ipn_dsize) {
774 next = ipnp->ipn_next;
775 } while (ipnp && next);
784 int writeall(dirname)
790 dirname = IPF_SAVEDIR;
792 if (chdir(dirname)) {
793 fprintf(stderr, "IPF_SAVEDIR=%s: ", dirname);
794 perror("chdir(IPF_SAVEDIR)");
798 fd = opendevice(NULL);
801 if (setlock(fd, 1)) {
806 devfd = opendevice(IPSTATE_NAME);
809 if (writestate(devfd, NULL))
813 devfd = opendevice(IPNAT_NAME);
816 if (writenat(devfd, NULL))
820 if (setlock(fd, 0)) {
841 dirname = IPF_SAVEDIR;
843 if (chdir(dirname)) {
844 perror("chdir(IPF_SAVEDIR)");
848 fd = opendevice(NULL);
851 if (setlock(fd, 1)) {
856 devfd = opendevice(IPSTATE_NAME);
859 if (readstate(devfd, NULL))
863 devfd = opendevice(IPNAT_NAME);
866 if (readnat(devfd, NULL))
870 if (setlock(fd, 0)) {