ip6tables-save.c 4.73 KB
Newer Older
1
2
3
4
5
6
7
8
/* Code to save the ip6tables state, in human readable-form. */
/* Author:  Andras Kis-Szabo <kisza@sch.bme.hu>
 * Original code: iptables-save
 * Authors: Paul 'Rusty' Russel <rusty@linuxcare.com.au> and
 *          Harald Welte <laforge@gnumonks.org>
 * This code is distributed under the terms of GNU GPL v2
 */
#include <getopt.h>
9
#include <errno.h>
10
11
12
13
14
15
16
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <netdb.h>
#include <arpa/inet.h>
17
#include <unistd.h>
18
19
20
21
#include "libiptc/libip6tc.h"
#include "ip6tables.h"
#include "ip6tables-multi.h"

22
23
24
#define prog_name ip6tables_globals.program_name
#define prog_vers ip6tables_globals.program_version

25
static int show_counters;
26
27
28
29
30
31

static const struct option options[] = {
	{.name = "counters", .has_arg = false, .val = 'c'},
	{.name = "dump",     .has_arg = false, .val = 'd'},
	{.name = "table",    .has_arg = true,  .val = 't'},
	{.name = "modprobe", .has_arg = true,  .val = 'M'},
32
	{.name = "file",     .has_arg = true,  .val = 'f'},
33
	{.name = "version",  .has_arg = false, .val = 'V'},
34
35
36
37
38
39
40
41
42
43
	{NULL},
};


/* Debugging prototype. */
static int for_each_table(int (*func)(const char *tablename))
{
	int ret = 1;
	FILE *procfile = NULL;
	char tablename[XT_TABLE_MAXNAMELEN+1];
44
	static const char filename[] = "/proc/net/ip6_tables_names";
45

46
47
48
49
50
51
52
53
	procfile = fopen(filename, "re");
	if (!procfile) {
		if (errno == ENOENT)
			return ret;
		fprintf(stderr, "Failed to list table names in %s: %s\n",
		        filename, strerror(errno));
		exit(1);
	}
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136

	while (fgets(tablename, sizeof(tablename), procfile)) {
		if (tablename[strlen(tablename) - 1] != '\n')
			xtables_error(OTHER_PROBLEM,
				   "Badly formed tablename `%s'\n",
				   tablename);
		tablename[strlen(tablename) - 1] = '\0';
		ret &= func(tablename);
	}

	fclose(procfile);
	return ret;
}


static int do_output(const char *tablename)
{
	struct xtc_handle *h;
	const char *chain = NULL;

	if (!tablename)
		return for_each_table(&do_output);

	h = ip6tc_init(tablename);
	if (h == NULL) {
		xtables_load_ko(xtables_modprobe_program, false);
		h = ip6tc_init(tablename);
	}
	if (!h)
		xtables_error(OTHER_PROBLEM, "Cannot initialize: %s\n",
			   ip6tc_strerror(errno));

	time_t now = time(NULL);

	printf("# Generated by ip6tables-save v%s on %s",
	       IPTABLES_VERSION, ctime(&now));
	printf("*%s\n", tablename);

	/* Dump out chain names first,
	 * thereby preventing dependency conflicts */
	for (chain = ip6tc_first_chain(h);
	     chain;
	     chain = ip6tc_next_chain(h)) {

		printf(":%s ", chain);
		if (ip6tc_builtin(chain, h)) {
			struct xt_counters count;
			printf("%s ",
			       ip6tc_get_policy(chain, &count, h));
			printf("[%llu:%llu]\n", (unsigned long long)count.pcnt, (unsigned long long)count.bcnt);
		} else {
			printf("- [0:0]\n");
		}
	}

	for (chain = ip6tc_first_chain(h);
	     chain;
	     chain = ip6tc_next_chain(h)) {
		const struct ip6t_entry *e;

		/* Dump out rules */
		e = ip6tc_first_rule(chain, h);
		while(e) {
			print_rule6(e, h, chain, show_counters);
			e = ip6tc_next_rule(e, h);
		}
	}

	now = time(NULL);
	printf("COMMIT\n");
	printf("# Completed on %s", ctime(&now));
	ip6tc_free(h);

	return 1;
}

/* Format:
 * :Chain name POLICY packets bytes
 * rule
 */
int ip6tables_save_main(int argc, char *argv[])
{
	const char *tablename = NULL;
137
138
	FILE *file = NULL;
	int ret, c;
139
140
141
142
143
144
145
146
147
148
149
150
151
152

	ip6tables_globals.program_name = "ip6tables-save";
	c = xtables_init_all(&ip6tables_globals, NFPROTO_IPV6);
	if (c < 0) {
		fprintf(stderr, "%s/%s Failed to initialize xtables\n",
				ip6tables_globals.program_name,
				ip6tables_globals.program_version);
		exit(1);
	}
#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
	init_extensions();
	init_extensions6();
#endif

153
	while ((c = getopt_long(argc, argv, "bcdt:M:f:V", options, NULL)) != -1) {
154
		switch (c) {
155
156
157
		case 'b':
			fprintf(stderr, "-b/--binary option is not implemented\n");
			break;
158
159
160
161
162
163
164
165
166
167
168
		case 'c':
			show_counters = 1;
			break;

		case 't':
			/* Select specific table. */
			tablename = optarg;
			break;
		case 'M':
			xtables_modprobe_program = optarg;
			break;
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
		case 'f':
			file = fopen(optarg, "w");
			if (file == NULL) {
				fprintf(stderr, "Failed to open file, error: %s\n",
					strerror(errno));
				exit(1);
			}
			ret = dup2(fileno(file), STDOUT_FILENO);
			if (ret == -1) {
				fprintf(stderr, "Failed to redirect stdout, error: %s\n",
					strerror(errno));
				exit(1);
			}
			fclose(file);
			break;
184
185
186
		case 'd':
			do_output(tablename);
			exit(0);
187
188
189
		case 'V':
			printf("%s v%s (legacy)\n", prog_name, prog_vers);
			exit(0);
190
191
192
193
		default:
			fprintf(stderr,
				"Look at manual page `ip6tables-save.8' for more information.\n");
			exit(1);
194
195
196
197
198
199
200
201
202
203
		}
	}

	if (optind < argc) {
		fprintf(stderr, "Unknown arguments found on commandline\n");
		exit(1);
	}

	return !do_output(tablename);
}