xtables-save.c 6.75 KB
Newer Older
1
2
3
4
5
6
7
8
/* Code to save the xtables state, in human readable-form. */
/* (C) 1999 by Paul 'Rusty' Russell <rusty@rustcorp.com.au> and
 * (C) 2000-2002 by Harald Welte <laforge@gnumonks.org>
 * (C) 2012 by Pablo Neira Ayuso <pablo@netfilter.org>
 *
 * This code is distributed under the terms of GNU GPL v2
 *
 */
9
#include "config.h"
10
11
#include <getopt.h>
#include <errno.h>
12
#include <libgen.h>
13
14
15
16
17
18
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <netdb.h>
19
#include <unistd.h>
20
21
22
23
#include "libiptc/libiptc.h"
#include "iptables.h"
#include "xtables-multi.h"
#include "nft.h"
24
#include "nft-cache.h"
25
26
27
28
29
30
31

#include <libnftnl/chain.h>

#ifndef NO_SHARED_LIBS
#include <dlfcn.h>
#endif

32
33
34
#define prog_name xtables_globals.program_name
#define prog_vers xtables_globals.program_version

35
36
static const char *ipt_save_optstring = "bcdt:M:f:46V";
static const struct option ipt_save_options[] = {
37
	{.name = "counters", .has_arg = false, .val = 'c'},
38
	{.name = "version",  .has_arg = false, .val = 'V'},
39
40
41
	{.name = "dump",     .has_arg = false, .val = 'd'},
	{.name = "table",    .has_arg = true,  .val = 't'},
	{.name = "modprobe", .has_arg = true,  .val = 'M'},
42
	{.name = "file",     .has_arg = true,  .val = 'f'},
43
44
45
46
47
	{.name = "ipv4",     .has_arg = false, .val = '4'},
	{.name = "ipv6",     .has_arg = false, .val = '6'},
	{NULL},
};

48
static const char *arp_save_optstring = "cM:V";
49
50
51
52
53
54
55
static const struct option arp_save_options[] = {
	{.name = "counters", .has_arg = false, .val = 'c'},
	{.name = "version",  .has_arg = false, .val = 'V'},
	{.name = "modprobe", .has_arg = true,  .val = 'M'},
	{NULL},
};

56
static const char *ebt_save_optstring = "ct:M:V";
57
58
59
60
61
62
63
64
static const struct option ebt_save_options[] = {
	{.name = "counters", .has_arg = false, .val = 'c'},
	{.name = "version",  .has_arg = false, .val = 'V'},
	{.name = "table",    .has_arg = true,  .val = 't'},
	{.name = "modprobe", .has_arg = true,  .val = 'M'},
	{NULL},
};

65
66
67
68
struct do_output_data {
	unsigned int format;
	bool commit;
};
69

70
static int
71
__do_output(struct nft_handle *h, const char *tablename, void *data)
72
73
{
	struct nftnl_chain_list *chain_list;
74
75
	struct do_output_data *d = data;
	time_t now;
76

77
78
	if (!nft_table_builtin_find(h, tablename))
		return 0;
79

80
81
82
	if (!nft_is_table_compatible(h, tablename, NULL)) {
		printf("# Table `%s' is incompatible, use 'nft' tool.\n",
		       tablename);
83
84
85
		return 0;
	}

86
	chain_list = nft_chain_list_get(h, tablename, NULL);
87
88
	if (!chain_list)
		return 0;
89

90
91
92
	now = time(NULL);
	printf("# Generated by %s v%s on %s", prog_name,
	       prog_vers, ctime(&now));
93
94
95
96

	printf("*%s\n", tablename);
	/* Dump out chain names first,
	 * thereby preventing dependency conflicts */
97
	nft_chain_save(h, chain_list);
98
99
100
	nft_rule_save(h, tablename, d->format);
	if (d->commit)
		printf("COMMIT\n");
101
102
103

	now = time(NULL);
	printf("# Completed on %s", ctime(&now));
104
105
106
107
	return 0;
}

static int
108
do_output(struct nft_handle *h, const char *tablename, struct do_output_data *d)
109
110
111
112
{
	int ret;

	if (!tablename) {
113
		ret = nft_for_each_table(h, __do_output, d);
114
115
116
		nft_check_xt_legacy(h->family, true);
		return !!ret;
	}
117

118
119
120
	if (!nft_table_find(h, tablename) &&
	    !nft_table_builtin_find(h, tablename)) {
		fprintf(stderr, "Table `%s' does not exist\n", tablename);
121
122
123
		return 1;
	}

124
	ret = __do_output(h, tablename, d);
125
126
	nft_check_xt_legacy(h->family, true);
	return ret;
127
128
129
130
131
132
133
}

/* Format:
 * :Chain name POLICY packets bytes
 * rule
 */
static int
134
135
xtables_save_main(int family, int argc, char *argv[],
		  const char *optstring, const struct option *longopts)
136
{
137
	const struct builtin_table *tables;
138
	const char *tablename = NULL;
139
140
141
	struct do_output_data d = {
		.format = FMT_NOCOUNTS,
	};
142
143
144
145
	bool dump = false;
	struct nft_handle h = {
		.family	= family,
	};
146
147
	FILE *file = NULL;
	int ret, c;
148

149
	xtables_globals.program_name = basename(*argv);;
150
151
152
153
154
155
156
157
	c = xtables_init_all(&xtables_globals, family);
	if (c < 0) {
		fprintf(stderr, "%s/%s Failed to initialize xtables\n",
				xtables_globals.program_name,
				xtables_globals.program_version);
		exit(1);
	}

158
	while ((c = getopt_long(argc, argv, optstring, longopts, NULL)) != -1) {
159
160
161
162
163
		switch (c) {
		case 'b':
			fprintf(stderr, "-b/--binary option is not implemented\n");
			break;
		case 'c':
164
			d.format &= ~FMT_NOCOUNTS;
165
166
167
168
169
170
171
172
173
			break;

		case 't':
			/* Select specific table. */
			tablename = optarg;
			break;
		case 'M':
			xtables_modprobe_program = optarg;
			break;
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
		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;
189
190
191
192
193
194
195
196
197
198
		case 'd':
			dump = true;
			break;
		case '4':
			h.family = AF_INET;
			break;
		case '6':
			h.family = AF_INET6;
			xtables_set_nfproto(AF_INET6);
			break;
199
200
201
		case 'V':
			printf("%s v%s (nf_tables)\n", prog_name, prog_vers);
			exit(0);
202
203
		default:
			fprintf(stderr,
204
205
				"Look at manual page `%s.8' for more information.\n",
				prog_name);
206
			exit(1);
207
208
209
210
211
212
213
214
		}
	}

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

215
216
217
218
219
220
221
222
	switch (family) {
	case NFPROTO_IPV4:
	case NFPROTO_IPV6: /* fallthough, same table */
#if defined(ALL_INCLUSIVE) || defined(NO_SHARED_LIBS)
		init_extensions();
		init_extensions4();
#endif
		tables = xtables_ipv4;
223
		d.commit = true;
224
225
226
227
		break;
	case NFPROTO_ARP:
		tables = xtables_arp;
		break;
228
229
230
231
232
233
234
235
236
	case NFPROTO_BRIDGE: {
		const char *ctr = getenv("EBTABLES_SAVE_COUNTER");

		if (!(d.format & FMT_NOCOUNTS)) {
			d.format |= FMT_EBT_SAVE;
		} else if (ctr && !strcmp(ctr, "yes")) {
			d.format &= ~FMT_NOCOUNTS;
			d.format |= FMT_C_COUNTS | FMT_EBT_SAVE;
		}
237
238
		tables = xtables_bridge;
		break;
239
	}
240
241
242
243
244
245
246
247
248
249
	default:
		fprintf(stderr, "Unknown family %d\n", family);
		return 1;
	}

	if (nft_init(&h, tables) < 0) {
		fprintf(stderr, "%s/%s Failed to initialize nft: %s\n",
				xtables_globals.program_name,
				xtables_globals.program_version,
				strerror(errno));
250
251
		exit(EXIT_FAILURE);
	}
252
253
254
	h.ops = nft_family_ops_lookup(h.family);
	if (!h.ops)
		xtables_error(PARAMETER_PROBLEM, "Unknown family");
255

256
	ret = do_output(&h, tablename, &d);
257
258
	nft_fini(&h);
	if (dump)
259
260
		exit(0);

261
	return ret;
262
263
264
265
}

int xtables_ip4_save_main(int argc, char *argv[])
{
266
267
	return xtables_save_main(NFPROTO_IPV4, argc, argv,
				 ipt_save_optstring, ipt_save_options);
268
269
270
271
}

int xtables_ip6_save_main(int argc, char *argv[])
{
272
273
	return xtables_save_main(NFPROTO_IPV6, argc, argv,
				 ipt_save_optstring, ipt_save_options);
274
275
}

276
int xtables_eb_save_main(int argc, char *argv[])
277
{
278
279
	return xtables_save_main(NFPROTO_BRIDGE, argc, argv,
				 ebt_save_optstring, ebt_save_options);
280
281
}

282
int xtables_arp_save_main(int argc, char *argv[])
283
{
284
285
	return xtables_save_main(NFPROTO_ARP, argc, argv,
				 arp_save_optstring, arp_save_options);
286
}