blob: 55cfe6a1472d96657104a0ae1b52cc22efed137c [file] [log] [blame]
Marc Bouchere6869a82000-03-20 06:03:29 +00001/* Code to save the iptables state, in human readable-form. */
Harald Welte10a907f2002-08-07 09:07:41 +00002/* (C) 1999 by Paul 'Rusty' Russell <rusty@rustcorp.com.au> and
3 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
4 *
5 * This code is distributed under the terms of GNU GPL v2
6 *
Harald Welteae1ff9f2000-12-01 14:26:20 +00007 */
Marc Bouchere6869a82000-03-20 06:03:29 +00008#include <getopt.h>
9#include <sys/errno.h>
10#include <stdio.h>
Harald Welteae1ff9f2000-12-01 14:26:20 +000011#include <fcntl.h>
12#include <stdlib.h>
13#include <string.h>
Marc Bouchere6869a82000-03-20 06:03:29 +000014#include <time.h>
Harald Welted8ac9672004-04-15 10:10:19 +000015#include <netdb.h>
Rusty Russellb1f69be2000-08-27 07:42:54 +000016#include "libiptc/libiptc.h"
17#include "iptables.h"
Jan Engelhardt33690a12008-02-11 00:54:00 +010018#include "iptables-multi.h"
Marc Bouchere6869a82000-03-20 06:03:29 +000019
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000020#ifndef NO_SHARED_LIBS
21#include <dlfcn.h>
22#endif
23
Jan Engelhardtdbb77542008-02-11 00:33:30 +010024static int show_binary = 0, show_counters = 0;
Rusty Russella8f033e2000-07-30 01:43:01 +000025
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010026static const struct option options[] = {
27 {.name = "binary", .has_arg = false, .val = 'b'},
28 {.name = "counters", .has_arg = false, .val = 'c'},
29 {.name = "dump", .has_arg = false, .val = 'd'},
30 {.name = "table", .has_arg = true, .val = 't'},
31 {NULL},
Marc Bouchere6869a82000-03-20 06:03:29 +000032};
33
Marc Bouchere6869a82000-03-20 06:03:29 +000034/* Debugging prototype. */
Rusty Russella8f033e2000-07-30 01:43:01 +000035static int for_each_table(int (*func)(const char *tablename))
36{
Max Kellermann5b76f682008-01-29 13:42:48 +000037 int ret = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +000038 FILE *procfile = NULL;
Rusty Russella8f033e2000-07-30 01:43:01 +000039 char tablename[IPT_TABLE_MAXNAMELEN+1];
40
Harald Welteae1ff9f2000-12-01 14:26:20 +000041 procfile = fopen("/proc/net/ip_tables_names", "r");
Rusty Russella8f033e2000-07-30 01:43:01 +000042 if (!procfile)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010043 xtables_error(OTHER_PROBLEM,
Victor Stinner65334ad2007-10-18 14:27:03 +000044 "Unable to open /proc/net/ip_tables_names: %s\n",
45 strerror(errno));
Rusty Russella8f033e2000-07-30 01:43:01 +000046
47 while (fgets(tablename, sizeof(tablename), procfile)) {
48 if (tablename[strlen(tablename) - 1] != '\n')
Jan Engelhardt1829ed42009-02-21 03:29:44 +010049 xtables_error(OTHER_PROBLEM,
Rusty Russella8f033e2000-07-30 01:43:01 +000050 "Badly formed tablename `%s'\n",
51 tablename);
52 tablename[strlen(tablename) - 1] = '\0';
53 ret &= func(tablename);
54 }
55
56 return ret;
57}
Max Kellermann5b76f682008-01-29 13:42:48 +000058
Rusty Russella8f033e2000-07-30 01:43:01 +000059
Rusty Russella8f033e2000-07-30 01:43:01 +000060static int do_output(const char *tablename)
61{
Jan Engelhardtfd187312008-11-10 16:59:27 +010062 struct iptc_handle *h;
Rusty Russella8f033e2000-07-30 01:43:01 +000063 const char *chain = NULL;
64
65 if (!tablename)
66 return for_each_table(&do_output);
67
68 h = iptc_init(tablename);
69 if (!h)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010070 xtables_error(OTHER_PROBLEM, "Cannot initialize: %s\n",
Rusty Russella8f033e2000-07-30 01:43:01 +000071 iptc_strerror(errno));
72
Jan Engelhardtdbb77542008-02-11 00:33:30 +010073 if (!show_binary) {
Rusty Russella8f033e2000-07-30 01:43:01 +000074 time_t now = time(NULL);
75
76 printf("# Generated by iptables-save v%s on %s",
Jan Engelhardtdacafa52009-01-27 20:56:23 +010077 IPTABLES_VERSION, ctime(&now));
Harald Welteae1ff9f2000-12-01 14:26:20 +000078 printf("*%s\n", tablename);
Rusty Russella8f033e2000-07-30 01:43:01 +000079
Max Kellermann5b76f682008-01-29 13:42:48 +000080 /* Dump out chain names first,
Harald Welte9f7fa492001-03-15 15:12:02 +000081 * thereby preventing dependency conflicts */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010082 for (chain = iptc_first_chain(h);
Rusty Russella8f033e2000-07-30 01:43:01 +000083 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010084 chain = iptc_next_chain(h)) {
Max Kellermann5b76f682008-01-29 13:42:48 +000085
Rusty Russella8f033e2000-07-30 01:43:01 +000086 printf(":%s ", chain);
Harald Welteae1ff9f2000-12-01 14:26:20 +000087 if (iptc_builtin(chain, h)) {
Rusty Russella8f033e2000-07-30 01:43:01 +000088 struct ipt_counters count;
89 printf("%s ",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010090 iptc_get_policy(chain, &count, h));
Martin Josefssona28d4952004-05-26 16:04:48 +000091 printf("[%llu:%llu]\n", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
Rusty Russella8f033e2000-07-30 01:43:01 +000092 } else {
Harald Welted8e65632001-01-05 15:20:07 +000093 printf("- [0:0]\n");
Rusty Russella8f033e2000-07-30 01:43:01 +000094 }
Harald Welte9f7fa492001-03-15 15:12:02 +000095 }
Max Kellermann5b76f682008-01-29 13:42:48 +000096
Harald Welte9f7fa492001-03-15 15:12:02 +000097
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010098 for (chain = iptc_first_chain(h);
Harald Welte9f7fa492001-03-15 15:12:02 +000099 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100100 chain = iptc_next_chain(h)) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000101 const struct ipt_entry *e;
Rusty Russella8f033e2000-07-30 01:43:01 +0000102
Harald Welteae1ff9f2000-12-01 14:26:20 +0000103 /* Dump out rules */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100104 e = iptc_first_rule(chain, h);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000105 while(e) {
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100106 print_rule(e, h, chain, show_counters);
107 e = iptc_next_rule(e, h);
Rusty Russella8f033e2000-07-30 01:43:01 +0000108 }
109 }
110
111 now = time(NULL);
112 printf("COMMIT\n");
113 printf("# Completed on %s", ctime(&now));
114 } else {
115 /* Binary, huh? OK. */
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100116 xtables_error(OTHER_PROBLEM, "Binary NYI\n");
Rusty Russella8f033e2000-07-30 01:43:01 +0000117 }
118
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100119 iptc_free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000120
Rusty Russella8f033e2000-07-30 01:43:01 +0000121 return 1;
122}
123
Marc Bouchere6869a82000-03-20 06:03:29 +0000124/* Format:
125 * :Chain name POLICY packets bytes
126 * rule
127 */
Bastiaan Bakker4e3771f2004-06-25 11:18:57 +0000128#ifdef IPTABLES_MULTI
129int
130iptables_save_main(int argc, char *argv[])
131#else
132int
133main(int argc, char *argv[])
134#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000135{
Rusty Russella8f033e2000-07-30 01:43:01 +0000136 const char *tablename = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000137 int c;
Marc Bouchere6869a82000-03-20 06:03:29 +0000138
Jamal Hadi Salim617d3d12009-02-11 16:28:31 -0500139 iptables_globals.program_name = "iptables-save";
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500140 c = xtables_init_all(&iptables_globals, NFPROTO_IPV4);
141 if (c < 0) {
142 fprintf(stderr, "%s/%s Failed to initialize xtables\n",
143 iptables_globals.program_name,
144 iptables_globals.program_version);
145 exit(1);
146 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000147#ifdef NO_SHARED_LIBS
148 init_extensions();
149#endif
150
Marc Boucher163ad782001-12-06 15:05:48 +0000151 while ((c = getopt_long(argc, argv, "bcdt:", options, NULL)) != -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000152 switch (c) {
153 case 'b':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100154 show_binary = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000155 break;
156
157 case 'c':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100158 show_counters = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000159 break;
160
Rusty Russella8f033e2000-07-30 01:43:01 +0000161 case 't':
162 /* Select specific table. */
163 tablename = optarg;
164 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000165 case 'd':
Harald Welteae1ff9f2000-12-01 14:26:20 +0000166 do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000167 exit(0);
168 }
169 }
170
171 if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000172 fprintf(stderr, "Unknown arguments found on commandline\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000173 exit(1);
174 }
175
Rusty Russella8f033e2000-07-30 01:43:01 +0000176 return !do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000177}