blob: 9de34b51a76202075fe76b45e2b1ecc020278c7c [file] [log] [blame]
András Kis-Szabó2f523792001-02-27 09:59:48 +00001/* Code to save the ip6tables state, in human readable-form. */
2/* Author: Andras Kis-Szabo <kisza@sch.bme.hu>
3 * Original code: iptables-save
4 * Authors: Paul 'Rusty' Russel <rusty@linuxcare.com.au> and
Max Kellermann5b76f682008-01-29 13:42:48 +00005 * Harald Welte <laforge@gnumonks.org>
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00006 * This code is distributed under the terms of GNU GPL v2
András Kis-Szabó2f523792001-02-27 09:59:48 +00007 */
8#include <getopt.h>
9#include <sys/errno.h>
10#include <stdio.h>
11#include <fcntl.h>
12#include <stdlib.h>
13#include <string.h>
András Kis-Szabó2f523792001-02-27 09:59:48 +000014#include <time.h>
15#include <netdb.h>
16#include <arpa/inet.h>
17#include "libiptc/libip6tc.h"
18#include "ip6tables.h"
Jan Engelhardt33690a12008-02-11 00:54:00 +010019#include "ip6tables-multi.h"
András Kis-Szabó2f523792001-02-27 09:59:48 +000020
Mike Frysinger5a26b5f2007-12-19 14:51:17 +000021#ifndef NO_SHARED_LIBS
22#include <dlfcn.h>
23#endif
24
Jan Engelhardtdbb77542008-02-11 00:33:30 +010025static int show_binary = 0, show_counters = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +000026
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010027static const struct option options[] = {
28 {.name = "binary", .has_arg = false, .val = 'b'},
29 {.name = "counters", .has_arg = false, .val = 'c'},
30 {.name = "dump", .has_arg = false, .val = 'd'},
31 {.name = "table", .has_arg = true, .val = 't'},
32 {NULL},
András Kis-Szabó2f523792001-02-27 09:59:48 +000033};
34
András Kis-Szabó2f523792001-02-27 09:59:48 +000035
András Kis-Szabó2f523792001-02-27 09:59:48 +000036/* Debugging prototype. */
37static int for_each_table(int (*func)(const char *tablename))
38{
Max Kellermann5b76f682008-01-29 13:42:48 +000039 int ret = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +000040 FILE *procfile = NULL;
41 char tablename[IP6T_TABLE_MAXNAMELEN+1];
42
43 procfile = fopen("/proc/net/ip6_tables_names", "r");
44 if (!procfile)
Victor Stinner65334ad2007-10-18 14:27:03 +000045 exit_error(OTHER_PROBLEM,
46 "Unable to open /proc/net/ip6_tables_names: %s\n",
47 strerror(errno));
András Kis-Szabó2f523792001-02-27 09:59:48 +000048
49 while (fgets(tablename, sizeof(tablename), procfile)) {
50 if (tablename[strlen(tablename) - 1] != '\n')
51 exit_error(OTHER_PROBLEM,
52 "Badly formed tablename `%s'\n",
53 tablename);
54 tablename[strlen(tablename) - 1] = '\0';
55 ret &= func(tablename);
56 }
57
58 return ret;
59}
Max Kellermann5b76f682008-01-29 13:42:48 +000060
András Kis-Szabó2f523792001-02-27 09:59:48 +000061
62static int do_output(const char *tablename)
63{
Jan Engelhardtfd187312008-11-10 16:59:27 +010064 struct ip6tc_handle *h;
András Kis-Szabó2f523792001-02-27 09:59:48 +000065 const char *chain = NULL;
66
67 if (!tablename)
68 return for_each_table(&do_output);
69
70 h = ip6tc_init(tablename);
71 if (!h)
Max Kellermann5b76f682008-01-29 13:42:48 +000072 exit_error(OTHER_PROBLEM, "Can't initialize: %s\n",
András Kis-Szabó2f523792001-02-27 09:59:48 +000073 ip6tc_strerror(errno));
74
Jan Engelhardtdbb77542008-02-11 00:33:30 +010075 if (!show_binary) {
András Kis-Szabó2f523792001-02-27 09:59:48 +000076 time_t now = time(NULL);
77
78 printf("# Generated by ip6tables-save v%s on %s",
Jan Engelhardtdacafa52009-01-27 20:56:23 +010079 IPTABLES_VERSION, ctime(&now));
András Kis-Szabó2f523792001-02-27 09:59:48 +000080 printf("*%s\n", tablename);
81
Max Kellermann5b76f682008-01-29 13:42:48 +000082 /* Dump out chain names first,
Harald Welte885c6eb2001-10-04 08:30:46 +000083 * thereby preventing dependency conflicts */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010084 for (chain = ip6tc_first_chain(h);
András Kis-Szabó2f523792001-02-27 09:59:48 +000085 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010086 chain = ip6tc_next_chain(h)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +000087
88 printf(":%s ", chain);
89 if (ip6tc_builtin(chain, h)) {
90 struct ip6t_counters count;
91 printf("%s ",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010092 ip6tc_get_policy(chain, &count, h));
Martin Josefssona28d4952004-05-26 16:04:48 +000093 printf("[%llu:%llu]\n", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
András Kis-Szabó2f523792001-02-27 09:59:48 +000094 } else {
95 printf("- [0:0]\n");
96 }
Harald Welte885c6eb2001-10-04 08:30:46 +000097 }
98
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000099
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100100 for (chain = ip6tc_first_chain(h);
Harald Welte885c6eb2001-10-04 08:30:46 +0000101 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100102 chain = ip6tc_next_chain(h)) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000103 const struct ip6t_entry *e;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000104
105 /* Dump out rules */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100106 e = ip6tc_first_rule(chain, h);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000107 while(e) {
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100108 print_rule(e, h, chain, show_counters);
109 e = ip6tc_next_rule(e, h);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000110 }
111 }
112
113 now = time(NULL);
114 printf("COMMIT\n");
115 printf("# Completed on %s", ctime(&now));
116 } else {
117 /* Binary, huh? OK. */
118 exit_error(OTHER_PROBLEM, "Binary NYI\n");
119 }
120
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100121 ip6tc_free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000122
András Kis-Szabó2f523792001-02-27 09:59:48 +0000123 return 1;
124}
125
126/* Format:
127 * :Chain name POLICY packets bytes
128 * rule
129 */
Hann-Huei Chiou4bc48332007-10-23 14:22:34 +0000130#ifdef IPTABLES_MULTI
131int ip6tables_save_main(int argc, char *argv[])
132#else
András Kis-Szabó2f523792001-02-27 09:59:48 +0000133int main(int argc, char *argv[])
Hann-Huei Chiou4bc48332007-10-23 14:22:34 +0000134#endif
András Kis-Szabó2f523792001-02-27 09:59:48 +0000135{
136 const char *tablename = NULL;
137 int c;
138
139 program_name = "ip6tables-save";
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100140 program_version = IPTABLES_VERSION;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000141
Jan Engelhardtdacafa52009-01-27 20:56:23 +0100142 xtables_program_name = program_name;
Jamal Hadi Salim617d3d12009-02-11 16:28:31 -0500143 ip6tables_globals.program_name = "ip6tables-save";
Jamal Hadi Salim7e4db2f2009-02-13 09:14:17 -0500144 c = xtables_init_all(&ip6tables_globals, NFPROTO_IPV6);
145 if (c < 0) {
146 fprintf(stderr, "%s/%s Failed to initialize xtables\n",
147 ip6tables_globals.program_name,
148 ip6tables_globals.program_version);
149 exit(1);
150 }
Harald Welte3efb6ea2001-08-06 18:50:21 +0000151#ifdef NO_SHARED_LIBS
152 init_extensions();
153#endif
154
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000155 while ((c = getopt_long(argc, argv, "bcdt:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000156 switch (c) {
157 case 'b':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100158 show_binary = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000159 break;
160
161 case 'c':
Jan Engelhardtdbb77542008-02-11 00:33:30 +0100162 show_counters = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000163 break;
164
165 case 't':
166 /* Select specific table. */
167 tablename = optarg;
168 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000169 case 'd':
170 do_output(tablename);
171 exit(0);
172 }
173 }
174
175 if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000176 fprintf(stderr, "Unknown arguments found on commandline\n");
András Kis-Szabó2f523792001-02-27 09:59:48 +0000177 exit(1);
178 }
179
180 return !do_output(tablename);
181}