blob: 3e3ec43cfbeee15aef931ddaf49e647a75562885 [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
Maciej Zenczykowskia2397282011-04-04 15:30:32 +020042 procfile = fopen("/proc/net/ip_tables_names", "re");
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
Jan Engelhardtf1afcc82009-06-10 13:52:58 +020055 fclose(procfile);
Rusty Russella8f033e2000-07-30 01:43:01 +000056 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);
Jan Engelhardtfbb56392009-03-19 16:57:35 +010069 if (h == NULL) {
70 xtables_load_ko(xtables_modprobe_program, false);
71 h = iptc_init(tablename);
72 }
Rusty Russella8f033e2000-07-30 01:43:01 +000073 if (!h)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010074 xtables_error(OTHER_PROBLEM, "Cannot initialize: %s\n",
Rusty Russella8f033e2000-07-30 01:43:01 +000075 iptc_strerror(errno));
76
Jan Engelhardtdbb77542008-02-11 00:33:30 +010077 if (!show_binary) {
Rusty Russella8f033e2000-07-30 01:43:01 +000078 time_t now = time(NULL);
79
80 printf("# Generated by iptables-save v%s on %s",
Jan Engelhardtdacafa52009-01-27 20:56:23 +010081 IPTABLES_VERSION, ctime(&now));
Harald Welteae1ff9f2000-12-01 14:26:20 +000082 printf("*%s\n", tablename);
Rusty Russella8f033e2000-07-30 01:43:01 +000083
Max Kellermann5b76f682008-01-29 13:42:48 +000084 /* Dump out chain names first,
Harald Welte9f7fa492001-03-15 15:12:02 +000085 * thereby preventing dependency conflicts */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010086 for (chain = iptc_first_chain(h);
Rusty Russella8f033e2000-07-30 01:43:01 +000087 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010088 chain = iptc_next_chain(h)) {
Max Kellermann5b76f682008-01-29 13:42:48 +000089
Rusty Russella8f033e2000-07-30 01:43:01 +000090 printf(":%s ", chain);
Harald Welteae1ff9f2000-12-01 14:26:20 +000091 if (iptc_builtin(chain, h)) {
Rusty Russella8f033e2000-07-30 01:43:01 +000092 struct ipt_counters count;
93 printf("%s ",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010094 iptc_get_policy(chain, &count, h));
Martin Josefssona28d4952004-05-26 16:04:48 +000095 printf("[%llu:%llu]\n", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
Rusty Russella8f033e2000-07-30 01:43:01 +000096 } else {
Harald Welted8e65632001-01-05 15:20:07 +000097 printf("- [0:0]\n");
Rusty Russella8f033e2000-07-30 01:43:01 +000098 }
Harald Welte9f7fa492001-03-15 15:12:02 +000099 }
Max Kellermann5b76f682008-01-29 13:42:48 +0000100
Harald Welte9f7fa492001-03-15 15:12:02 +0000101
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100102 for (chain = iptc_first_chain(h);
Harald Welte9f7fa492001-03-15 15:12:02 +0000103 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100104 chain = iptc_next_chain(h)) {
Harald Welte9f7fa492001-03-15 15:12:02 +0000105 const struct ipt_entry *e;
Rusty Russella8f033e2000-07-30 01:43:01 +0000106
Harald Welteae1ff9f2000-12-01 14:26:20 +0000107 /* Dump out rules */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100108 e = iptc_first_rule(chain, h);
Harald Welteae1ff9f2000-12-01 14:26:20 +0000109 while(e) {
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100110 print_rule(e, h, chain, show_counters);
111 e = iptc_next_rule(e, h);
Rusty Russella8f033e2000-07-30 01:43:01 +0000112 }
113 }
114
115 now = time(NULL);
116 printf("COMMIT\n");
117 printf("# Completed on %s", ctime(&now));
118 } else {
119 /* Binary, huh? OK. */
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100120 xtables_error(OTHER_PROBLEM, "Binary NYI\n");
Rusty Russella8f033e2000-07-30 01:43:01 +0000121 }
122
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100123 iptc_free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000124
Rusty Russella8f033e2000-07-30 01:43:01 +0000125 return 1;
126}
127
Marc Bouchere6869a82000-03-20 06:03:29 +0000128/* Format:
129 * :Chain name POLICY packets bytes
130 * rule
131 */
Bastiaan Bakker4e3771f2004-06-25 11:18:57 +0000132#ifdef IPTABLES_MULTI
133int
134iptables_save_main(int argc, char *argv[])
135#else
136int
137main(int argc, char *argv[])
138#endif
Marc Bouchere6869a82000-03-20 06:03:29 +0000139{
Rusty Russella8f033e2000-07-30 01:43:01 +0000140 const char *tablename = NULL;
Marc Bouchere6869a82000-03-20 06:03:29 +0000141 int c;
Marc Bouchere6869a82000-03-20 06:03:29 +0000142
Jamal Hadi Salim617d3d12009-02-11 16:28:31 -0500143 iptables_globals.program_name = "iptables-save";
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500144 c = xtables_init_all(&iptables_globals, NFPROTO_IPV4);
145 if (c < 0) {
146 fprintf(stderr, "%s/%s Failed to initialize xtables\n",
147 iptables_globals.program_name,
148 iptables_globals.program_version);
149 exit(1);
150 }
Jan Engelhardtb79ec692009-07-23 17:41:21 +0200151#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
Harald Welte3efb6ea2001-08-06 18:50:21 +0000152 init_extensions();
153#endif
154
Marc Boucher163ad782001-12-06 15:05:48 +0000155 while ((c = getopt_long(argc, argv, "bcdt:", options, NULL)) != -1) {
Marc Bouchere6869a82000-03-20 06:03:29 +0000156 switch (c) {
157 case 'b':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100158 show_binary = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000159 break;
160
161 case 'c':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100162 show_counters = 1;
Marc Bouchere6869a82000-03-20 06:03:29 +0000163 break;
164
Rusty Russella8f033e2000-07-30 01:43:01 +0000165 case 't':
166 /* Select specific table. */
167 tablename = optarg;
168 break;
Jan Engelhardtfbb56392009-03-19 16:57:35 +0100169 case 'M':
170 xtables_modprobe_program = optarg;
171 break;
Marc Bouchere6869a82000-03-20 06:03:29 +0000172 case 'd':
Harald Welteae1ff9f2000-12-01 14:26:20 +0000173 do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000174 exit(0);
175 }
176 }
177
178 if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000179 fprintf(stderr, "Unknown arguments found on commandline\n");
Marc Bouchere6869a82000-03-20 06:03:29 +0000180 exit(1);
181 }
182
Rusty Russella8f033e2000-07-30 01:43:01 +0000183 return !do_output(tablename);
Marc Bouchere6869a82000-03-20 06:03:29 +0000184}