1 /* Code to save the iptables state, in human readable-form. */
2 /* (C) 1999 by Paul 'Rusty' Russell <rusty@rustcorp.com.au> and
3 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
5 * This code is distributed under the terms of GNU GPL v2
16 #include "libiptc/libiptc.h"
18 #include "iptables-multi.h"
20 #ifndef NO_SHARED_LIBS
24 static int show_binary = 0, show_counters = 0;
26 static 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 {.name = "modprobe", .has_arg = true, .val = 'M'},
35 /* Debugging prototype. */
36 static int for_each_table(int (*func)(const char *tablename))
39 FILE *procfile = NULL;
40 char tablename[IPT_TABLE_MAXNAMELEN+1];
42 procfile = fopen("/proc/net/ip_tables_names", "r");
46 while (fgets(tablename, sizeof(tablename), procfile)) {
47 if (tablename[strlen(tablename) - 1] != '\n')
48 xtables_error(OTHER_PROBLEM,
49 "Badly formed tablename `%s'\n",
51 tablename[strlen(tablename) - 1] = '\0';
52 ret &= func(tablename);
59 static int do_output(const char *tablename)
61 struct iptc_handle *h;
62 const char *chain = NULL;
65 return for_each_table(&do_output);
67 h = iptc_init(tablename);
69 xtables_load_ko(xtables_modprobe_program, false);
70 h = iptc_init(tablename);
73 xtables_error(OTHER_PROBLEM, "Cannot initialize: %s\n",
74 iptc_strerror(errno));
77 time_t now = time(NULL);
79 printf("# Generated by iptables-save v%s on %s",
80 IPTABLES_VERSION, ctime(&now));
81 printf("*%s\n", tablename);
83 /* Dump out chain names first,
84 * thereby preventing dependency conflicts */
85 for (chain = iptc_first_chain(h);
87 chain = iptc_next_chain(h)) {
89 printf(":%s ", chain);
90 if (iptc_builtin(chain, h)) {
91 struct ipt_counters count;
93 iptc_get_policy(chain, &count, h));
94 printf("[%llu:%llu]\n", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
101 for (chain = iptc_first_chain(h);
103 chain = iptc_next_chain(h)) {
104 const struct ipt_entry *e;
107 e = iptc_first_rule(chain, h);
109 print_rule(e, h, chain, show_counters);
110 e = iptc_next_rule(e, h);
116 printf("# Completed on %s", ctime(&now));
118 /* Binary, huh? OK. */
119 xtables_error(OTHER_PROBLEM, "Binary NYI\n");
128 * :Chain name POLICY packets bytes
131 #ifdef IPTABLES_MULTI
133 iptables_save_main(int argc, char *argv[])
136 main(int argc, char *argv[])
139 const char *tablename = NULL;
142 iptables_globals.program_name = "iptables-save";
143 c = xtables_init_all(&iptables_globals, NFPROTO_IPV4);
145 fprintf(stderr, "%s/%s Failed to initialize xtables\n",
146 iptables_globals.program_name,
147 iptables_globals.program_version);
150 #ifdef NO_SHARED_LIBS
154 while ((c = getopt_long(argc, argv, "bcdt:", options, NULL)) != -1) {
165 /* Select specific table. */
169 xtables_modprobe_program = optarg;
172 do_output(tablename);
178 fprintf(stderr, "Unknown arguments found on commandline\n");
182 return !do_output(tablename);