blob: 6000b494c5b770062afd1d9730bb78bbea049ad7 [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'},
Jan Engelhardtfbb56392009-03-19 16:57:35 +010031 {.name = "modprobe", .has_arg = true, .val = 'M'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010032 {NULL},
Marc Bouchere6869a82000-03-20 06:03:29 +000033};
34
Marc Bouchere6869a82000-03-20 06:03:29 +000035/* Debugging prototype. */
Rusty Russella8f033e2000-07-30 01:43:01 +000036static int for_each_table(int (*func)(const char *tablename))
37{
Max Kellermann5b76f682008-01-29 13:42:48 +000038 int ret = 1;
Harald Welteae1ff9f2000-12-01 14:26:20 +000039 FILE *procfile = NULL;
Rusty Russella8f033e2000-07-30 01:43:01 +000040 char tablename[IPT_TABLE_MAXNAMELEN+1];
41
Harald Welteae1ff9f2000-12-01 14:26:20 +000042 procfile = fopen("/proc/net/ip_tables_names", "r");
Rusty Russella8f033e2000-07-30 01:43:01 +000043 if (!procfile)
Jan Engelhardtfbb56392009-03-19 16:57:35 +010044 return ret;
Rusty Russella8f033e2000-07-30 01:43:01 +000045
46 while (fgets(tablename, sizeof(tablename), procfile)) {
47 if (tablename[strlen(tablename) - 1] != '\n')
Jan Engelhardt1829ed42009-02-21 03:29:44 +010048 xtables_error(OTHER_PROBLEM,
Rusty Russella8f033e2000-07-30 01:43:01 +000049 "Badly formed tablename `%s'\n",
50 tablename);
51 tablename[strlen(tablename) - 1] = '\0';
52 ret &= func(tablename);
53 }
54
55 return ret;
56}
Max Kellermann5b76f682008-01-29 13:42:48 +000057
Rusty Russella8f033e2000-07-30 01:43:01 +000058
Rusty Russella8f033e2000-07-30 01:43:01 +000059static int do_output(const char *tablename)
60{
Jan Engelhardtfd187312008-11-10 16:59:27 +010061 struct iptc_handle *h;
Rusty Russella8f033e2000-07-30 01:43:01 +000062 const char *chain = NULL;
63
64 if (!tablename)
65 return for_each_table(&do_output);
66
67 h = iptc_init(tablename);
Jan Engelhardtfbb56392009-03-19 16:57:35 +010068 if (h == NULL) {
69 xtables_load_ko(xtables_modprobe_program, false);
70 h = iptc_init(tablename);
71 }
Rusty Russella8f033e2000-07-30 01:43:01 +000072 if (!h)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010073 xtables_error(OTHER_PROBLEM, "Cannot initialize: %s\n",
Rusty Russella8f033e2000-07-30 01:43:01 +000074 iptc_strerror(errno));
75
Jan Engelhardtdbb77542008-02-11 00:33:30 +010076 if (!show_binary) {
Rusty Russella8f033e2000-07-30 01:43:01 +000077 time_t now = time(NULL);
78
79 printf("# Generated by iptables-save v%s on %s",
Jan Engelhardtdacafa52009-01-27 20:56:23 +010080 IPTABLES_VERSION, ctime(&now));
Harald Welteae1ff9f2000-12-01 14:26:20 +000081 printf("*%s\n", tablename);
Rusty Russella8f033e2000-07-30 01:43:01 +000082
Max Kellermann5b76f682008-01-29 13:42:48 +000083 /* Dump out chain names first,
Harald Welte9f7fa492001-03-15 15:12:02 +000084 * thereby preventing dependency conflicts */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010085 for (chain = iptc_first_chain(h);
Rusty Russella8f033e2000-07-30 01:43:01 +000086 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010087 chain = iptc_next_chain(h)) {
Max Kellermann5b76f682008-01-29 13:42:48 +000088
Rusty Russella8f033e2000-07-30 01:43:01 +000089 printf(":%s ", chain);
Harald Welteae1ff9f2000-12-01 14:26:20 +000090 if (iptc_builtin(chain, h)) {
Rusty Russella8f033e2000-07-30 01:43:01 +000091 struct ipt_counters count;
92 printf("%s ",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010093 iptc_get_policy(chain, &count, h));
Martin Josefssona28d4952004-05-26 16:04:48 +000094 printf("[%llu:%llu]\n", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
Rusty Russella8f033e2000-07-30 01:43:01 +000095 } else {
Harald Welted8e65632001-01-05 15:20:07 +000096 printf("- [0:0]\n");
Rusty Russella8f033e2000-07-30 01:43:01 +000097 }
Harald Welte9f7fa492001-03-15 15:12:02 +000098 }
Max Kellermann5b76f682008-01-29 13:42:48 +000099
Harald Welte9f7fa492001-03-15 15:12:02 +0000100
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100101 for (chain = iptc_first_chain(h);
Harald Welte9f7fa492001-03-15 15:12:02 +0000102 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100103 chain = iptc_next_chain(h)) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000104 const struct ipt_entry *e;
Rusty Russella8f033e2000-07-30 01:43:01 +0000105
Harald Welteae1ff9f2000-12-01 14:26:20 +0000106 /* Dump out rules */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100107 e = iptc_first_rule(chain, h);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000108 while(e) {
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100109 print_rule(e, h, chain, show_counters);
110 e = iptc_next_rule(e, h);
Rusty Russella8f033e2000-07-30 01:43:01 +0000111 }
112 }
113
114 now = time(NULL);
115 printf("COMMIT\n");
116 printf("# Completed on %s", ctime(&now));
117 } else {
118 /* Binary, huh? OK. */
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100119 xtables_error(OTHER_PROBLEM, "Binary NYI\n");
Rusty Russella8f033e2000-07-30 01:43:01 +0000120 }
121
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100122 iptc_free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000123
Rusty Russella8f033e2000-07-30 01:43:01 +0000124 return 1;
125}
126
Marc Bouchere6869a82000-03-20 06:03:29 +0000127/* Format:
128 * :Chain name POLICY packets bytes
129 * rule
130 */
Bastiaan Bakker4e3771f2004-06-25 11:18:57 +0000131#ifdef IPTABLES_MULTI
132int
133iptables_save_main(int argc, char *argv[])
134#else
135int
136main(int argc, char *argv[])
137#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000138{
Rusty Russella8f033e2000-07-30 01:43:01 +0000139 const char *tablename = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000140 int c;
Marc Bouchere6869a82000-03-20 06:03:29 +0000141
Jamal Hadi Salim617d3d12009-02-11 16:28:31 -0500142 iptables_globals.program_name = "iptables-save";
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500143 c = xtables_init_all(&iptables_globals, NFPROTO_IPV4);
144 if (c < 0) {
145 fprintf(stderr, "%s/%s Failed to initialize xtables\n",
146 iptables_globals.program_name,
147 iptables_globals.program_version);
148 exit(1);
149 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000150#ifdef NO_SHARED_LIBS
151 init_extensions();
152#endif
153
Marc Boucher163ad782001-12-06 15:05:48 +0000154 while ((c = getopt_long(argc, argv, "bcdt:", options, NULL)) != -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000155 switch (c) {
156 case 'b':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100157 show_binary = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000158 break;
159
160 case 'c':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100161 show_counters = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000162 break;
163
Rusty Russella8f033e2000-07-30 01:43:01 +0000164 case 't':
165 /* Select specific table. */
166 tablename = optarg;
167 break;
Jan Engelhardtfbb56392009-03-19 16:57:35 +0100168 case 'M':
169 xtables_modprobe_program = optarg;
170 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000171 case 'd':
Harald Welteae1ff9f2000-12-01 14:26:20 +0000172 do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000173 exit(0);
174 }
175 }
176
177 if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000178 fprintf(stderr, "Unknown arguments found on commandline\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000179 exit(1);
180 }
181
Rusty Russella8f033e2000-07-30 01:43:01 +0000182 return !do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000183}