blob: c3b8ec0d31af2cb1bbc2de1174fc2ca771cc7013 [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'},
Jan Engelhardtfbb56392009-03-19 16:57:35 +010032 {.name = "modprobe", .has_arg = true, .val = 'M'},
Gáspár Lajos7bc3cb72008-03-27 08:20:39 +010033 {NULL},
András Kis-Szabó2f523792001-02-27 09:59:48 +000034};
35
András Kis-Szabó2f523792001-02-27 09:59:48 +000036
András Kis-Szabó2f523792001-02-27 09:59:48 +000037/* Debugging prototype. */
38static int for_each_table(int (*func)(const char *tablename))
39{
Max Kellermann5b76f682008-01-29 13:42:48 +000040 int ret = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +000041 FILE *procfile = NULL;
42 char tablename[IP6T_TABLE_MAXNAMELEN+1];
43
Maciej Zenczykowskia2397282011-04-04 15:30:32 +020044 procfile = fopen("/proc/net/ip6_tables_names", "re");
András Kis-Szabó2f523792001-02-27 09:59:48 +000045 if (!procfile)
Jan Engelhardtfbb56392009-03-19 16:57:35 +010046 return ret;
András Kis-Szabó2f523792001-02-27 09:59:48 +000047
48 while (fgets(tablename, sizeof(tablename), procfile)) {
49 if (tablename[strlen(tablename) - 1] != '\n')
Jan Engelhardt1829ed42009-02-21 03:29:44 +010050 xtables_error(OTHER_PROBLEM,
András Kis-Szabó2f523792001-02-27 09:59:48 +000051 "Badly formed tablename `%s'\n",
52 tablename);
53 tablename[strlen(tablename) - 1] = '\0';
54 ret &= func(tablename);
55 }
56
Jan Engelhardtf1afcc82009-06-10 13:52:58 +020057 fclose(procfile);
András Kis-Szabó2f523792001-02-27 09:59:48 +000058 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);
Jan Engelhardtfbb56392009-03-19 16:57:35 +010071 if (h == NULL) {
72 xtables_load_ko(xtables_modprobe_program, false);
73 h = ip6tc_init(tablename);
74 }
András Kis-Szabó2f523792001-02-27 09:59:48 +000075 if (!h)
Jan Engelhardt1829ed42009-02-21 03:29:44 +010076 xtables_error(OTHER_PROBLEM, "Cannot initialize: %s\n",
András Kis-Szabó2f523792001-02-27 09:59:48 +000077 ip6tc_strerror(errno));
78
Jan Engelhardtdbb77542008-02-11 00:33:30 +010079 if (!show_binary) {
András Kis-Szabó2f523792001-02-27 09:59:48 +000080 time_t now = time(NULL);
81
82 printf("# Generated by ip6tables-save v%s on %s",
Jan Engelhardtdacafa52009-01-27 20:56:23 +010083 IPTABLES_VERSION, ctime(&now));
András Kis-Szabó2f523792001-02-27 09:59:48 +000084 printf("*%s\n", tablename);
85
Max Kellermann5b76f682008-01-29 13:42:48 +000086 /* Dump out chain names first,
Harald Welte885c6eb2001-10-04 08:30:46 +000087 * thereby preventing dependency conflicts */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010088 for (chain = ip6tc_first_chain(h);
András Kis-Szabó2f523792001-02-27 09:59:48 +000089 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010090 chain = ip6tc_next_chain(h)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +000091
92 printf(":%s ", chain);
93 if (ip6tc_builtin(chain, h)) {
94 struct ip6t_counters count;
95 printf("%s ",
Jan Engelhardt1c9015b2008-11-10 17:00:41 +010096 ip6tc_get_policy(chain, &count, h));
Martin Josefssona28d4952004-05-26 16:04:48 +000097 printf("[%llu:%llu]\n", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
András Kis-Szabó2f523792001-02-27 09:59:48 +000098 } else {
99 printf("- [0:0]\n");
100 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000101 }
102
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000103
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100104 for (chain = ip6tc_first_chain(h);
Harald Welte885c6eb2001-10-04 08:30:46 +0000105 chain;
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100106 chain = ip6tc_next_chain(h)) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000107 const struct ip6t_entry *e;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000108
109 /* Dump out rules */
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100110 e = ip6tc_first_rule(chain, h);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000111 while(e) {
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100112 print_rule(e, h, chain, show_counters);
113 e = ip6tc_next_rule(e, h);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000114 }
115 }
116
117 now = time(NULL);
118 printf("COMMIT\n");
119 printf("# Completed on %s", ctime(&now));
120 } else {
121 /* Binary, huh? OK. */
Jan Engelhardt1829ed42009-02-21 03:29:44 +0100122 xtables_error(OTHER_PROBLEM, "Binary NYI\n");
András Kis-Szabó2f523792001-02-27 09:59:48 +0000123 }
124
Jan Engelhardt1c9015b2008-11-10 17:00:41 +0100125 ip6tc_free(h);
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000126
András Kis-Szabó2f523792001-02-27 09:59:48 +0000127 return 1;
128}
129
130/* Format:
131 * :Chain name POLICY packets bytes
132 * rule
133 */
Hann-Huei Chiou4bc48332007-10-23 14:22:34 +0000134#ifdef IPTABLES_MULTI
135int ip6tables_save_main(int argc, char *argv[])
136#else
András Kis-Szabó2f523792001-02-27 09:59:48 +0000137int main(int argc, char *argv[])
Hann-Huei Chiou4bc48332007-10-23 14:22:34 +0000138#endif
András Kis-Szabó2f523792001-02-27 09:59:48 +0000139{
140 const char *tablename = NULL;
141 int c;
142
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 }
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
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;
Jan Engelhardtfbb56392009-03-19 16:57:35 +0100169 case 'M':
170 xtables_modprobe_program = optarg;
171 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000172 case 'd':
173 do_output(tablename);
174 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");
András Kis-Szabó2f523792001-02-27 09:59:48 +0000180 exit(1);
181 }
182
183 return !do_output(tablename);
184}