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));
468 if (i != sizeof(ips)) {
469 fprintf(stderr, "state:incomplete read: %d != %d\n",
470 i, (int)sizeof(ips));
474 is = (ipstate_save_t *)malloc(sizeof(*is));
476 fprintf(stderr, "malloc failed\n");
480 bcopy((char *)&ips, (char *)is, sizeof(ips));
483 * Check to see if this is the first state entry that will
484 * reference a particular rule and if so, flag it as such
485 * else just adjust the rule pointer to become a pointer to
486 * the other. We do this so we have a means later for tracking
487 * who is referencing us when we get back the real pointer
488 * in is_rule after doing the ioctl.
490 for (is1 = ipshead; is1 != NULL; is1 = is1->ips_next)
491 if (is1->ips_rule == is->ips_rule)
494 is->ips_is.is_flags |= SI_NEWFR;
496 is->ips_rule = (void *)&is1->ips_rule;
499 * Use a tail-queue type list (add things to the end)..
505 ipstail->ips_next = is;
511 obj.ipfo_rev = IPFILTER_VERSION;
512 obj.ipfo_size = sizeof(*is);
513 obj.ipfo_type = IPFOBJ_STATESAVE;
515 for (is = ipshead; is; is = is->ips_next) {
516 if (opts & OPT_VERBOSE)
517 printf("Loading new state table entry\n");
518 if (is->ips_is.is_flags & SI_NEWFR) {
519 if (opts & OPT_VERBOSE)
520 printf("Loading new filter rule\n");
524 if (!(opts & OPT_DONOTHING))
525 if (ioctl(fd, SIOCSTPUT, &obj)) {
530 if (is->ips_is.is_flags & SI_NEWFR) {
531 if (opts & OPT_VERBOSE)
532 printf("Real rule addr %p\n", is->ips_rule);
533 for (is1 = is->ips_next; is1; is1 = is1->ips_next)
534 if (is1->ips_rule == (frentry_t *)&is->ips_rule)
535 is1->ips_rule = is->ips_rule;
543 int readnat(fd, file)
547 nat_save_t ipn, *in, *ipnhead = NULL, *in1, *ipntail = NULL;
562 nfd = open(file, O_RDONLY);
564 fprintf(stderr, "%s ", file);
569 bzero((char *)&ipn, sizeof(ipn));
572 * 1. Read all state information in.
575 i = read(nfd, &ipn, sizeof(ipn));
583 if (i != sizeof(ipn)) {
584 fprintf(stderr, "nat:incomplete read: %d != %d\n",
585 i, (int)sizeof(ipn));
590 in = (nat_save_t *)malloc(ipn.ipn_dsize);
594 if (ipn.ipn_dsize > sizeof(ipn)) {
595 n = ipn.ipn_dsize - sizeof(ipn);
597 s = in->ipn_data + sizeof(in->ipn_data);
603 "nat:incomplete read: %d != %d\n",
610 bcopy((char *)&ipn, (char *)in, sizeof(ipn));
613 * Check to see if this is the first NAT entry that will
614 * reference a particular rule and if so, flag it as such
615 * else just adjust the rule pointer to become a pointer to
616 * the other. We do this so we have a means later for tracking
617 * who is referencing us when we get back the real pointer
618 * in is_rule after doing the ioctl.
621 if (nat->nat_fr != NULL) {
622 for (in1 = ipnhead; in1 != NULL; in1 = in1->ipn_next)
623 if (in1->ipn_rule == nat->nat_fr)
626 nat->nat_flags |= SI_NEWFR;
628 nat->nat_fr = &in1->ipn_fr;
632 * Use a tail-queue type list (add things to the end)..
638 ipntail->ipn_next = in;
645 obj.ipfo_rev = IPFILTER_VERSION;
646 obj.ipfo_type = IPFOBJ_NATSAVE;
648 for (in = ipnhead; in; in = in->ipn_next) {
649 if (opts & OPT_VERBOSE)
650 printf("Loading new NAT table entry\n");
652 if (nat->nat_flags & SI_NEWFR) {
653 if (opts & OPT_VERBOSE)
654 printf("Loading new filter rule\n");
658 obj.ipfo_size = in->ipn_dsize;
659 if (!(opts & OPT_DONOTHING))
660 if (ioctl(fd, SIOCSTPUT, &obj)) {
661 fprintf(stderr, "in=%p:", in);
666 if (nat->nat_flags & SI_NEWFR) {
667 if (opts & OPT_VERBOSE)
668 printf("Real rule addr %p\n", nat->nat_fr);
669 for (in1 = in->ipn_next; in1; in1 = in1->ipn_next)
670 if (in1->ipn_rule == &in->ipn_fr)
671 in1->ipn_rule = nat->nat_fr;
679 int writenat(fd, file)
683 nat_save_t *ipnp = NULL, *next = NULL;
691 nfd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0600);
693 fprintf(stderr, "%s ", file);
698 obj.ipfo_rev = IPFILTER_VERSION;
699 obj.ipfo_type = IPFOBJ_NATSAVE;
702 if (opts & OPT_VERBOSE)
703 printf("Getting nat from addr %p\n", ipnp);
706 if (ioctl(fd, SIOCSTGSZ, &ng)) {
707 perror("nat:SIOCSTGSZ");
714 if (opts & OPT_VERBOSE)
715 printf("NAT size %d from %p\n", ng.ng_sz, ng.ng_ptr);
721 ipnp = malloc(ng.ng_sz);
723 ipnp = realloc((char *)ipnp, ng.ng_sz);
726 "malloc for %d bytes failed\n", ng.ng_sz);
730 bzero((char *)ipnp, ng.ng_sz);
731 obj.ipfo_size = ng.ng_sz;
733 ipnp->ipn_dsize = ng.ng_sz;
734 ipnp->ipn_next = next;
735 if (ioctl(fd, SIOCSTGET, &obj)) {
738 perror("nat:SIOCSTGET");
744 if (opts & OPT_VERBOSE)
745 printf("Got nat next %p ipn_dsize %d ng_sz %d\n",
746 ipnp->ipn_next, ipnp->ipn_dsize, ng.ng_sz);
747 if (write(nfd, ipnp, ipnp->ipn_dsize) != ipnp->ipn_dsize) {
753 next = ipnp->ipn_next;
754 } while (ipnp && next);
763 int writeall(dirname)
769 dirname = IPF_SAVEDIR;
771 if (chdir(dirname)) {
772 fprintf(stderr, "IPF_SAVEDIR=%s: ", dirname);
773 perror("chdir(IPF_SAVEDIR)");
777 fd = opendevice(NULL);
780 if (setlock(fd, 1)) {
785 devfd = opendevice(IPSTATE_NAME);
788 if (writestate(devfd, NULL))
792 devfd = opendevice(IPNAT_NAME);
795 if (writenat(devfd, NULL))
799 if (setlock(fd, 0)) {
820 dirname = IPF_SAVEDIR;
822 if (chdir(dirname)) {
823 perror("chdir(IPF_SAVEDIR)");
827 fd = opendevice(NULL);
830 if (setlock(fd, 1)) {
835 devfd = opendevice(IPSTATE_NAME);
838 if (readstate(devfd, NULL))
842 devfd = opendevice(IPNAT_NAME);
845 if (readnat(devfd, NULL))
849 if (setlock(fd, 0)) {