xtables-save.c 5.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/* 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
 *
 */
#include <getopt.h>
#include <errno.h>
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <netdb.h>
17
#include <unistd.h>
18
19
20
21
22
23
24
25
26
27
28
#include "libiptc/libiptc.h"
#include "iptables.h"
#include "xtables-multi.h"
#include "nft.h"

#include <libnftnl/chain.h>

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

29
30
31
#define prog_name xtables_globals.program_name
#define prog_vers xtables_globals.program_version

32
33
34
35
static bool show_counters = false;

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

static int
47
__do_output(struct nft_handle *h, const char *tablename, bool counters)
48
49
50
51
52
53
{
	struct nftnl_chain_list *chain_list;


	if (!nft_table_find(h, tablename)) {
		printf("Table `%s' does not exist\n", tablename);
54
55
56
57
58
		return 1;
	}

	if (!nft_is_table_compatible(h, tablename)) {
		printf("# Table `%s' is incompatible, use 'nft' tool.\n", tablename);
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
		return 0;
	}

	chain_list = nft_chain_dump(h);

	time_t now = time(NULL);

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

	/* Dump out chain names first,
	 * thereby preventing dependency conflicts */
	nft_chain_save(h, chain_list, tablename);
	nft_rule_save(h, tablename, counters);

	now = time(NULL);
	printf("COMMIT\n");
	printf("# Completed on %s", ctime(&now));
78
79
80
81
82
83
84
85
86
87
88
89
90
	return 0;
}

static int
do_output(struct nft_handle *h, const char *tablename, bool counters)
{
	int ret;

	if (!tablename) {
		ret = nft_for_each_table(h, __do_output, counters);
		nft_check_xt_legacy(h->family, true);
		return !!ret;
	}
91

92
93
94
	ret = __do_output(h, tablename, counters);
	nft_check_xt_legacy(h->family, true);
	return ret;
95
96
97
98
99
100
101
102
103
}

/* Format:
 * :Chain name POLICY packets bytes
 * rule
 */
static int
xtables_save_main(int family, const char *progname, int argc, char *argv[])
{
104
	struct builtin_table *tables;
105
106
107
108
109
	const char *tablename = NULL;
	bool dump = false;
	struct nft_handle h = {
		.family	= family,
	};
110
111
	FILE *file = NULL;
	int ret, c;
112
113
114
115
116
117
118
119
120
121

	xtables_globals.program_name = progname;
	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);
	}

122
	while ((c = getopt_long(argc, argv, "bcdt:M:f:46V", options, NULL)) != -1) {
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
		switch (c) {
		case 'b':
			fprintf(stderr, "-b/--binary option is not implemented\n");
			break;
		case 'c':
			show_counters = true;
			break;

		case 't':
			/* Select specific table. */
			tablename = optarg;
			break;
		case 'M':
			xtables_modprobe_program = optarg;
			break;
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
		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;
153
154
155
156
157
158
159
160
161
162
		case 'd':
			dump = true;
			break;
		case '4':
			h.family = AF_INET;
			break;
		case '6':
			h.family = AF_INET6;
			xtables_set_nfproto(AF_INET6);
			break;
163
164
165
		case 'V':
			printf("%s v%s (nf_tables)\n", prog_name, prog_vers);
			exit(0);
166
167
168
169
		default:
			fprintf(stderr,
				"Look at manual page `xtables-save.8' for more information.\n");
			exit(1);
170
171
172
173
174
175
176
177
		}
	}

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

178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
	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;
		break;
	case NFPROTO_ARP:
		tables = xtables_arp;
		break;
	case NFPROTO_BRIDGE:
		tables = xtables_bridge;
		break;
	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));
203
204
205
		exit(EXIT_FAILURE);
	}

206
207
208
209
210
	if (dump) {
		do_output(&h, tablename, show_counters);
		exit(0);
	}

211
	return do_output(&h, tablename, show_counters);
212
213
214
215
216
217
218
219
220
221
222
}

int xtables_ip4_save_main(int argc, char *argv[])
{
	return xtables_save_main(NFPROTO_IPV4, "iptables-save", argc, argv);
}

int xtables_ip6_save_main(int argc, char *argv[])
{
	return xtables_save_main(NFPROTO_IPV6, "ip6tables-save", argc, argv);
}