blob: 396406feed84523956677e04951c98aeb4230870 [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)
Victor Stinner65334ad2007-10-18 14:27:03 +000043 exit_error(OTHER_PROBLEM,
44 "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')
49 exit_error(OTHER_PROBLEM,
50 "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)
Max Kellermann5b76f682008-01-29 13:42:48 +000070 exit_error(OTHER_PROBLEM, "Can't 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. */
116 exit_error(OTHER_PROBLEM, "Binary NYI\n");
117 }
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
139 program_name = "iptables-save";
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100140 program_version = IPTABLES_VERSION;
Marc Bouchere6869a82000-03-20 06:03:29 +0000141
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100142 xtables_program_name = program_name;
Jan Engelhardt39bf9c82009-01-27 15:59:06 +0100143 xtables_init();
Jan Engelhardt77f48c22009-02-07 19:59:53 +0100144 xtables_set_nfproto(NFPROTO_IPV4);
Jamal Hadi Salim617d3d12009-02-11 16:28:31 -0500145 iptables_globals.program_name = "iptables-save";
Harald Welte3efb6ea2001-08-06 18:50:21 +0000146#ifdef NO_SHARED_LIBS
147 init_extensions();
148#endif
149
Marc Boucher163ad782001-12-06 15:05:48 +0000150 while ((c = getopt_long(argc, argv, "bcdt:", options, NULL)) != -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000151 switch (c) {
152 case 'b':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100153 show_binary = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000154 break;
155
156 case 'c':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100157 show_counters = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000158 break;
159
Rusty Russella8f033e2000-07-30 01:43:01 +0000160 case 't':
161 /* Select specific table. */
162 tablename = optarg;
163 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000164 case 'd':
Harald Welteae1ff9f2000-12-01 14:26:20 +0000165 do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000166 exit(0);
167 }
168 }
169
170 if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000171 fprintf(stderr, "Unknown arguments found on commandline\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000172 exit(1);
173 }
174
Rusty Russella8f033e2000-07-30 01:43:01 +0000175 return !do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000176}