4 * Copyright (C) 2012 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$";
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);
136 if (!strncmp(ips.ips_is.is_ifname[2], ifs, olen + 1)) {
137 strcpy(ips.ips_is.is_ifname[2], s);
140 if (!strncmp(ips.ips_is.is_ifname[3], ifs, olen + 1)) {
141 strcpy(ips.ips_is.is_ifname[3], s);
145 if (lseek(fd, pos, SEEK_SET) != pos) {
149 if (write(fd, &ips, sizeof(ips)) != sizeof(ips)) {
154 pos = lseek(fd, 0, SEEK_CUR);
163 * Change interface names in NAT information saved out to disk.
165 int changenatif(ifs, fname)
168 int fd, olen, nlen, rw;
174 s = strchr(ifs, ',');
181 if (nlen >= sizeof(nat->nat_ifnames[0]) ||
182 olen >= sizeof(nat->nat_ifnames[0]))
185 fd = open(fname, O_RDWR);
191 for (pos = 0; read(fd, &ipn, sizeof(ipn)) == sizeof(ipn); ) {
193 if (!strncmp(nat->nat_ifnames[0], ifs, olen + 1)) {
194 strcpy(nat->nat_ifnames[0], s);
197 if (!strncmp(nat->nat_ifnames[1], ifs, olen + 1)) {
198 strcpy(nat->nat_ifnames[1], s);
202 if (lseek(fd, pos, SEEK_SET) != pos) {
206 if (write(fd, &ipn, sizeof(ipn)) != sizeof(ipn)) {
211 pos = lseek(fd, 0, SEEK_CUR);
223 int c, lock = -1, devfd = -1, err = 0, rw = -1, ns = -1, set = 0;
224 char *dirname = NULL, *filename = NULL, *ifs = NULL;
227 while ((c = getopt(argc, argv, "d:f:i:lNnSRruvWw")) != -1)
231 if ((set == 0) && !dirname && !filename)
237 if ((set != 0) && !dirname && !filename)
247 if (filename || dirname || set)
253 opts |= OPT_DONOTHING;
256 if ((ns >= 0) || dirname || (rw != -1) || set)
262 if (dirname || (rw != -1) || (ns == -1))
272 if ((ns >= 0) || dirname || (rw != -1) || set)
278 if (filename || dirname || set)
287 if (dirname || (rw != -1) || (ns == -1))
302 if (!filename || ns < 0)
305 return changenatif(ifs, filename);
307 return changestateif(ifs, filename);
310 if ((ns >= 0) || (lock >= 0)) {
312 devfd = opendevice(NULL);
315 devfd = opendevice(IPSTATE_NAME);
317 devfd = opendevice(IPNAT_NAME);
324 err = setlock(devfd, lock);
326 if (rw & 1) { /* WRITE */
328 err = writeall(dirname);
331 err = writenat(devfd, filename);
333 err = writestate(devfd, filename);
337 err = readall(dirname);
340 err = readnat(devfd, filename);
342 err = readstate(devfd, filename);
350 int opendevice(ipfdev)
355 if (opts & OPT_DONOTHING)
361 if ((fd = open(ipfdev, O_RDWR)) == -1)
362 if ((fd = open(ipfdev, O_RDONLY)) == -1)
363 perror("open device");
375 int setlock(fd, lock)
378 if (opts & OPT_VERBOSE)
379 printf("Turn lock %s\n", lock ? "on" : "off");
380 if (!(opts & OPT_DONOTHING)) {
381 if (ioctl(fd, SIOCSTLCK, &lock) == -1) {
385 if (opts & OPT_VERBOSE)
386 printf("Lock now %s\n", lock ? "on" : "off");
392 int writestate(fd, file)
396 ipstate_save_t ips, *ipsp;
401 file = IPF_STATEFILE;
403 wfd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0600);
405 fprintf(stderr, "%s ", file);
406 perror("state:open");
411 bzero((char *)&obj, sizeof(obj));
412 bzero((char *)ipsp, sizeof(ips));
414 obj.ipfo_rev = IPFILTER_VERSION;
415 obj.ipfo_size = sizeof(*ipsp);
416 obj.ipfo_type = IPFOBJ_STATESAVE;
421 if (opts & OPT_VERBOSE)
422 printf("Getting state from addr %p\n", ips.ips_next);
423 if (ioctl(fd, SIOCSTGET, &obj)) {
426 perror("state:SIOCSTGET");
430 if (opts & OPT_VERBOSE)
431 printf("Got state next %p\n", ips.ips_next);
432 if (write(wfd, ipsp, sizeof(ips)) != sizeof(ips)) {
433 perror("state:write");
437 } while (ips.ips_next != NULL);
444 int readstate(fd, file)
448 ipstate_save_t ips, *is, *ipshead = NULL, *is1, *ipstail = NULL;
453 file = IPF_STATEFILE;
455 sfd = open(file, O_RDONLY, 0600);
457 fprintf(stderr, "%s ", file);
462 bzero((char *)&ips, sizeof(ips));
465 * 1. Read all state information in.
468 i = read(sfd, &ips, sizeof(ips));
475 if (i != sizeof(ips)) {
476 fprintf(stderr, "state:incomplete read: %d != %d\n",
477 i, (int)sizeof(ips));
480 is = (ipstate_save_t *)malloc(sizeof(*is));
482 fprintf(stderr, "malloc failed\n");
486 bcopy((char *)&ips, (char *)is, sizeof(ips));
489 * Check to see if this is the first state entry that will
490 * reference a particular rule and if so, flag it as such
491 * else just adjust the rule pointer to become a pointer to
492 * the other. We do this so we have a means later for tracking
493 * who is referencing us when we get back the real pointer
494 * in is_rule after doing the ioctl.
496 for (is1 = ipshead; is1 != NULL; is1 = is1->ips_next)
497 if (is1->ips_rule == is->ips_rule)
500 is->ips_is.is_flags |= SI_NEWFR;
502 is->ips_rule = (void *)&is1->ips_rule;
505 * Use a tail-queue type list (add things to the end)..
511 ipstail->ips_next = is;
517 obj.ipfo_rev = IPFILTER_VERSION;
518 obj.ipfo_size = sizeof(*is);
519 obj.ipfo_type = IPFOBJ_STATESAVE;
521 while ((is = ipshead) != NULL) {
522 if (opts & OPT_VERBOSE)
523 printf("Loading new state table entry\n");
524 if (is->ips_is.is_flags & SI_NEWFR) {
525 if (opts & OPT_VERBOSE)
526 printf("Loading new filter rule\n");
530 if (!(opts & OPT_DONOTHING))
531 if (ioctl(fd, SIOCSTPUT, &obj)) {
536 if (is->ips_is.is_flags & SI_NEWFR) {
537 if (opts & OPT_VERBOSE)
538 printf("Real rule addr %p\n", is->ips_rule);
539 for (is1 = is->ips_next; is1; is1 = is1->ips_next)
540 if (is1->ips_rule == (frentry_t *)&is->ips_rule)
541 is1->ips_rule = is->ips_rule;
544 ipshead = is->ips_next;
551 while ((is = ipshead) != NULL) {
552 ipshead = is->ips_next;
561 int readnat(fd, file)
565 nat_save_t ipn, *in, *ipnhead = NULL, *in1, *ipntail = NULL;
580 nfd = open(file, O_RDONLY);
582 fprintf(stderr, "%s ", file);
587 bzero((char *)&ipn, sizeof(ipn));
590 * 1. Read all state information in.
593 i = read(nfd, &ipn, sizeof(ipn));
600 if (i != sizeof(ipn)) {
601 fprintf(stderr, "nat:incomplete read: %d != %d\n",
602 i, (int)sizeof(ipn));
606 in = (nat_save_t *)malloc(ipn.ipn_dsize);
608 fprintf(stderr, "nat:cannot malloc nat save atruct\n");
612 if (ipn.ipn_dsize > sizeof(ipn)) {
613 n = ipn.ipn_dsize - sizeof(ipn);
615 s = in->ipn_data + sizeof(in->ipn_data);
621 "nat:incomplete read: %d != %d\n",
627 bcopy((char *)&ipn, (char *)in, sizeof(ipn));
630 * Check to see if this is the first NAT entry that will
631 * reference a particular rule and if so, flag it as such
632 * else just adjust the rule pointer to become a pointer to
633 * the other. We do this so we have a means later for tracking
634 * who is referencing us when we get back the real pointer
635 * in is_rule after doing the ioctl.
638 if (nat->nat_fr != NULL) {
639 for (in1 = ipnhead; in1 != NULL; in1 = in1->ipn_next)
640 if (in1->ipn_rule == nat->nat_fr)
643 nat->nat_flags |= SI_NEWFR;
645 nat->nat_fr = &in1->ipn_fr;
649 * Use a tail-queue type list (add things to the end)..
655 ipntail->ipn_next = in;
662 obj.ipfo_rev = IPFILTER_VERSION;
663 obj.ipfo_type = IPFOBJ_NATSAVE;
665 while ((in = ipnhead) != NULL) {
666 if (opts & OPT_VERBOSE)
667 printf("Loading new NAT table entry\n");
669 if (nat->nat_flags & SI_NEWFR) {
670 if (opts & OPT_VERBOSE)
671 printf("Loading new filter rule\n");
675 obj.ipfo_size = in->ipn_dsize;
676 if (!(opts & OPT_DONOTHING))
677 if (ioctl(fd, SIOCSTPUT, &obj)) {
678 fprintf(stderr, "in=%p:", in);
683 if (nat->nat_flags & SI_NEWFR) {
684 if (opts & OPT_VERBOSE)
685 printf("Real rule addr %p\n", nat->nat_fr);
686 for (in1 = in->ipn_next; in1; in1 = in1->ipn_next)
687 if (in1->ipn_rule == &in->ipn_fr)
688 in1->ipn_rule = nat->nat_fr;
691 ipnhead = in->ipn_next;
698 while ((in = ipnhead) != NULL) {
699 ipnhead = in->ipn_next;
708 int writenat(fd, file)
712 nat_save_t *ipnp = NULL, *next = NULL;
720 nfd = open(file, O_WRONLY|O_TRUNC|O_CREAT, 0600);
722 fprintf(stderr, "%s ", file);
727 obj.ipfo_rev = IPFILTER_VERSION;
728 obj.ipfo_type = IPFOBJ_NATSAVE;
731 if (opts & OPT_VERBOSE)
732 printf("Getting nat from addr %p\n", ipnp);
735 if (ioctl(fd, SIOCSTGSZ, &ng)) {
736 perror("nat:SIOCSTGSZ");
743 if (opts & OPT_VERBOSE)
744 printf("NAT size %d from %p\n", ng.ng_sz, ng.ng_ptr);
750 ipnp = malloc(ng.ng_sz);
752 ipnp = realloc((char *)ipnp, ng.ng_sz);
755 "malloc for %d bytes failed\n", ng.ng_sz);
759 bzero((char *)ipnp, ng.ng_sz);
760 obj.ipfo_size = ng.ng_sz;
762 ipnp->ipn_dsize = ng.ng_sz;
763 ipnp->ipn_next = next;
764 if (ioctl(fd, SIOCSTGET, &obj)) {
767 perror("nat:SIOCSTGET");
773 if (opts & OPT_VERBOSE)
774 printf("Got nat next %p ipn_dsize %d ng_sz %d\n",
775 ipnp->ipn_next, ipnp->ipn_dsize, ng.ng_sz);
776 if (write(nfd, ipnp, ipnp->ipn_dsize) != ipnp->ipn_dsize) {
782 next = ipnp->ipn_next;
783 } while (ipnp && next);
792 int writeall(dirname)
798 dirname = IPF_SAVEDIR;
800 if (chdir(dirname)) {
801 fprintf(stderr, "IPF_SAVEDIR=%s: ", dirname);
802 perror("chdir(IPF_SAVEDIR)");
806 fd = opendevice(NULL);
809 if (setlock(fd, 1)) {
814 devfd = opendevice(IPSTATE_NAME);
817 if (writestate(devfd, NULL))
821 devfd = opendevice(IPNAT_NAME);
824 if (writenat(devfd, NULL))
828 if (setlock(fd, 0)) {
849 dirname = IPF_SAVEDIR;
851 if (chdir(dirname)) {
852 perror("chdir(IPF_SAVEDIR)");
856 fd = opendevice(NULL);
859 if (setlock(fd, 1)) {
864 devfd = opendevice(IPSTATE_NAME);
867 if (readstate(devfd, NULL))
871 devfd = opendevice(IPNAT_NAME);
874 if (readnat(devfd, NULL))
878 if (setlock(fd, 0)) {