blob: a34e2260eff306fc276a86f464c58108f34262e0 [file] [log] [blame]
András Kis-Szabó2f523792001-02-27 09:59:48 +00001/* Code to restore the iptables state, from file by ip6tables-save.
2 * Author: Andras Kis-Szabo <kisza@sch.bme.hu>
3 *
4 * based on iptables-restore
5 * Authors:
6 * Harald Welte <laforge@gnumonks.org>
7 * Rusty Russell <rusty@linuxcare.com.au>
András Kis-Szabó0c4188f2002-08-14 11:40:41 +00008 * This code is distributed under the terms of GNU GPL v2
András Kis-Szabó2f523792001-02-27 09:59:48 +00009 *
Martin Josefsson357d59d2004-12-27 19:49:28 +000010 * $Id$
András Kis-Szabó2f523792001-02-27 09:59:48 +000011 */
12
13#include <getopt.h>
14#include <sys/errno.h>
15#include <string.h>
16#include <stdio.h>
17#include <stdlib.h>
18#include "ip6tables.h"
Yasuyuki KOZAKAI0b82e8e2007-07-24 05:47:40 +000019#include "xtables.h"
András Kis-Szabó2f523792001-02-27 09:59:48 +000020#include "libiptc/libip6tc.h"
21
22#ifdef DEBUG
23#define DEBUGP(x, args...) fprintf(stderr, x, ## args)
24#else
25#define DEBUGP(x, args...)
26#endif
27
András Kis-Szabó2f523792001-02-27 09:59:48 +000028static int binary = 0, counters = 0, verbose = 0, noflush = 0;
29
30/* Keeping track of external matches and targets. */
31static struct option options[] = {
32 { "binary", 0, 0, 'b' },
33 { "counters", 0, 0, 'c' },
Martin Josefssonec789712004-01-31 19:28:13 +000034 { "verbose", 0, 0, 'v' },
Martin Josefsson4e5e29a2004-02-02 19:59:17 +000035 { "test", 0, 0, 't' },
András Kis-Szabó2f523792001-02-27 09:59:48 +000036 { "help", 0, 0, 'h' },
37 { "noflush", 0, 0, 'n'},
Harald Welte58918652001-06-16 18:25:25 +000038 { "modprobe", 1, 0, 'M'},
András Kis-Szabó2f523792001-02-27 09:59:48 +000039 { 0 }
40};
41
42static void print_usage(const char *name, const char *version) __attribute__((noreturn));
43
44static void print_usage(const char *name, const char *version)
45{
Martin Josefsson60044392004-02-02 20:12:33 +000046 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
András Kis-Szabó2f523792001-02-27 09:59:48 +000047 " [ --binary ]\n"
48 " [ --counters ]\n"
49 " [ --verbose ]\n"
Martin Josefsson4e5e29a2004-02-02 19:59:17 +000050 " [ --test ]\n"
András Kis-Szabó2f523792001-02-27 09:59:48 +000051 " [ --help ]\n"
Harald Welte58918652001-06-16 18:25:25 +000052 " [ --noflush ]\n"
53 " [ --modprobe=<command>]\n", name);
András Kis-Szabó2f523792001-02-27 09:59:48 +000054
55 exit(1);
56}
57
Harald Welte58918652001-06-16 18:25:25 +000058ip6tc_handle_t create_handle(const char *tablename, const char* modprobe)
András Kis-Szabó2f523792001-02-27 09:59:48 +000059{
60 ip6tc_handle_t handle;
61
62 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000063
64 if (!handle) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000065 /* try to insmod the module if iptc_init failed */
Yasuyuki KOZAKAI0d502bc2007-07-24 05:52:07 +000066 load_xtables_ko(modprobe, 0);
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000067 handle = ip6tc_init(tablename);
Harald Welte58918652001-06-16 18:25:25 +000068 }
69
András Kis-Szabó2f523792001-02-27 09:59:48 +000070 if (!handle) {
Patrick McHardy5b098ac2007-02-14 13:59:12 +000071 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize "
András Kis-Szabó2f523792001-02-27 09:59:48 +000072 "table '%s'\n", program_name, tablename);
73 exit(1);
74 }
75 return handle;
76}
77
Lutz Jaenickee78c69c2006-12-09 13:06:04 +000078static int parse_counters(char *string, struct ip6t_counters *ctr)
András Kis-Szabó2f523792001-02-27 09:59:48 +000079{
Patrick McHardy31f51c62007-10-16 08:49:31 +000080 u_int64_t *pcnt, *bcnt;
81
82 pcnt = &ctr->pcnt;
83 bcnt = &ctr->bcnt;
84 return (sscanf(string, "[%llu:%llu]", (unsigned long long *)pcnt, (unsigned long long *)bcnt) == 2);
András Kis-Szabó2f523792001-02-27 09:59:48 +000085}
86
Harald Welte885c6eb2001-10-04 08:30:46 +000087/* global new argv and argc */
88static char *newargv[255];
89static int newargc;
90
91/* function adding one argument to newargv, updating newargc
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000092 * returns true if argument added, false otherwise */
Harald Welte885c6eb2001-10-04 08:30:46 +000093static int add_argv(char *what) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +000094 DEBUGP("add_argv: %s\n", what);
95 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
96 newargv[newargc] = strdup(what);
97 newargc++;
98 return 1;
99 } else
100 return 0;
Harald Welte885c6eb2001-10-04 08:30:46 +0000101}
102
103static void free_argv(void) {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000104 int i;
Harald Welte885c6eb2001-10-04 08:30:46 +0000105
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000106 for (i = 0; i < newargc; i++)
107 free(newargv[i]);
Harald Welte885c6eb2001-10-04 08:30:46 +0000108}
109
András Kis-Szabó2f523792001-02-27 09:59:48 +0000110int main(int argc, char *argv[])
111{
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000112 ip6tc_handle_t handle = NULL;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000113 char buffer[10240];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000114 int c;
115 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
András Kis-Szabó2f523792001-02-27 09:59:48 +0000116 FILE *in;
Harald Welte58918652001-06-16 18:25:25 +0000117 const char *modprobe = 0;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000118 int in_table = 0, testing = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000119
120 program_name = "ip6tables-restore";
Harald Welte80fe35d2002-05-29 13:08:15 +0000121 program_version = IPTABLES_VERSION;
Harald Weltea8658ca2003-03-05 07:46:15 +0000122 line = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000123
Martin Josefsson357d59d2004-12-27 19:49:28 +0000124 lib_dir = getenv("IP6TABLES_LIB_DIR");
125 if (!lib_dir)
126 lib_dir = IP6T_LIB_DIR;
127
Harald Welte3efb6ea2001-08-06 18:50:21 +0000128#ifdef NO_SHARED_LIBS
129 init_extensions();
130#endif
131
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000132 while ((c = getopt_long(argc, argv, "bcvthnM:", options, NULL)) != -1) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000133 switch (c) {
134 case 'b':
135 binary = 1;
136 break;
137 case 'c':
138 counters = 1;
139 break;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000140 case 'v':
141 verbose = 1;
142 break;
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000143 case 't':
144 testing = 1;
145 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000146 case 'h':
147 print_usage("ip6tables-restore",
Harald Welte80fe35d2002-05-29 13:08:15 +0000148 IPTABLES_VERSION);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000149 break;
150 case 'n':
151 noflush = 1;
152 break;
Harald Welte58918652001-06-16 18:25:25 +0000153 case 'M':
154 modprobe = optarg;
155 break;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000156 }
157 }
158
159 if (optind == argc - 1) {
160 in = fopen(argv[optind], "r");
161 if (!in) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000162 fprintf(stderr, "Can't open %s: %s\n", argv[optind],
András Kis-Szabó2f523792001-02-27 09:59:48 +0000163 strerror(errno));
164 exit(1);
165 }
166 }
167 else if (optind < argc) {
Pavel Rusnak972af092007-05-10 15:00:39 +0000168 fprintf(stderr, "Unknown arguments found on commandline\n");
András Kis-Szabó2f523792001-02-27 09:59:48 +0000169 exit(1);
170 }
171 else in = stdin;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000172
András Kis-Szabó2f523792001-02-27 09:59:48 +0000173 /* Grab standard input. */
174 while (fgets(buffer, sizeof(buffer), in)) {
Martin Josefssoncc536282004-02-02 20:14:56 +0000175 int ret = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000176
177 line++;
Martin Josefsson5065afa2004-01-31 19:33:47 +0000178 if (buffer[0] == '\n')
179 continue;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000180 else if (buffer[0] == '#') {
Martin Josefsson3673dcb2004-01-31 19:41:49 +0000181 if (verbose)
182 fputs(buffer, stdout);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000183 continue;
Harald Weltea8658ca2003-03-05 07:46:15 +0000184 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
Martin Josefsson4e5e29a2004-02-02 19:59:17 +0000185 if (!testing) {
186 DEBUGP("Calling commit\n");
187 ret = ip6tc_commit(&handle);
188 } else {
189 DEBUGP("Not calling commit, testing\n");
190 ret = 1;
191 }
Harald Weltea8658ca2003-03-05 07:46:15 +0000192 in_table = 0;
193 } else if ((buffer[0] == '*') && (!in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000194 /* New table */
195 char *table;
196
197 table = strtok(buffer+1, " \t\n");
198 DEBUGP("line %u, table '%s'\n", line, table);
199 if (!table) {
200 exit_error(PARAMETER_PROBLEM,
201 "%s: line %u table name invalid\n",
202 program_name, line);
203 exit(1);
204 }
205 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
Karsten Desler073df8f2004-01-31 15:33:55 +0000206 curtable[IP6T_TABLE_MAXNAMELEN] = '\0';
András Kis-Szabó2f523792001-02-27 09:59:48 +0000207
Martin Josefsson841e4ae2003-05-02 15:30:11 +0000208 if (handle)
209 ip6tc_free(&handle);
210
Harald Welte58918652001-06-16 18:25:25 +0000211 handle = create_handle(table, modprobe);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000212 if (noflush == 0) {
213 DEBUGP("Cleaning all chains of table '%s'\n",
214 table);
215 for_each_chain(flush_entries, verbose, 1,
216 &handle);
217
218 DEBUGP("Deleting all user-defined chains "
219 "of table '%s'\n", table);
220 for_each_chain(delete_chain, verbose, 0,
221 &handle) ;
222 }
223
224 ret = 1;
Harald Weltea8658ca2003-03-05 07:46:15 +0000225 in_table = 1;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000226
Harald Weltea8658ca2003-03-05 07:46:15 +0000227 } else if ((buffer[0] == ':') && (in_table)) {
András Kis-Szabó2f523792001-02-27 09:59:48 +0000228 /* New chain. */
229 char *policy, *chain;
230
231 chain = strtok(buffer+1, " \t\n");
232 DEBUGP("line %u, chain '%s'\n", line, chain);
233 if (!chain) {
234 exit_error(PARAMETER_PROBLEM,
235 "%s: line %u chain name invalid\n",
236 program_name, line);
237 exit(1);
238 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000239
Harald Weltedf1c71c2004-08-30 16:00:32 +0000240 if (ip6tc_builtin(chain, handle) <= 0) {
Charlie Brady595e4932005-06-12 15:54:15 +0000241 if (noflush && ip6tc_is_chain(chain, handle)) {
242 DEBUGP("Flushing existing user defined chain '%s'\n", chain);
243 if (!ip6tc_flush_entries(chain, &handle))
244 exit_error(PARAMETER_PROBLEM,
245 "error flushing chain "
246 "'%s':%s\n", chain,
247 strerror(errno));
248 } else {
249 DEBUGP("Creating new chain '%s'\n", chain);
250 if (!ip6tc_create_chain(chain, &handle))
251 exit_error(PARAMETER_PROBLEM,
252 "error creating chain "
253 "'%s':%s\n", chain,
254 strerror(errno));
255 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000256 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000257
258 policy = strtok(NULL, " \t\n");
259 DEBUGP("line %u, policy '%s'\n", line, policy);
260 if (!policy) {
261 exit_error(PARAMETER_PROBLEM,
262 "%s: line %u policy invalid\n",
263 program_name, line);
264 exit(1);
265 }
266
267 if (strcmp(policy, "-") != 0) {
268 struct ip6t_counters count;
269
270 if (counters) {
271 char *ctrs;
272 ctrs = strtok(NULL, " \t\n");
273
Harald Welte6f38a302006-02-09 14:35:38 +0000274 if (!ctrs || !parse_counters(ctrs, &count))
275 exit_error(PARAMETER_PROBLEM,
276 "invalid policy counters "
277 "for chain '%s'\n", chain);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000278
279 } else {
280 memset(&count, 0,
281 sizeof(struct ip6t_counters));
282 }
283
284 DEBUGP("Setting policy of chain %s to %s\n",
285 chain, policy);
286
287 if (!ip6tc_set_policy(chain, policy, &count,
288 &handle))
289 exit_error(OTHER_PROBLEM,
290 "Can't set policy `%s'"
291 " on `%s' line %u: %s\n",
292 chain, policy, line,
293 ip6tc_strerror(errno));
294 }
295
296 ret = 1;
297
Harald Weltea8658ca2003-03-05 07:46:15 +0000298 } else if (in_table) {
Harald Welte885c6eb2001-10-04 08:30:46 +0000299 int a;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000300 char *ptr = buffer;
301 char *pcnt = NULL;
302 char *bcnt = NULL;
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000303 char *parsestart;
Harald Welte885c6eb2001-10-04 08:30:46 +0000304
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000305 /* the parser */
306 char *param_start, *curchar;
307 int quote_open;
Harald Welte885c6eb2001-10-04 08:30:46 +0000308
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000309 /* reset the newargv */
310 newargc = 0;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000311
312 if (buffer[0] == '[') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000313 /* we have counters in our input */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000314 ptr = strchr(buffer, ']');
315 if (!ptr)
316 exit_error(PARAMETER_PROBLEM,
317 "Bad line %u: need ]\n",
318 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000319
András Kis-Szabó2f523792001-02-27 09:59:48 +0000320 pcnt = strtok(buffer+1, ":");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000321 if (!pcnt)
322 exit_error(PARAMETER_PROBLEM,
323 "Bad line %u: need :\n",
324 line);
Harald Welte885c6eb2001-10-04 08:30:46 +0000325
András Kis-Szabó2f523792001-02-27 09:59:48 +0000326 bcnt = strtok(NULL, "]");
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000327 if (!bcnt)
328 exit_error(PARAMETER_PROBLEM,
329 "Bad line %u: need ]\n",
330 line);
András Kis-Szabó2f523792001-02-27 09:59:48 +0000331
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000332 /* start command parsing after counter */
333 parsestart = ptr + 1;
334 } else {
335 /* start command parsing at start of line */
336 parsestart = buffer;
András Kis-Szabó2f523792001-02-27 09:59:48 +0000337 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000338
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000339 add_argv(argv[0]);
340 add_argv("-t");
341 add_argv((char *) &curtable);
342
343 if (counters && pcnt && bcnt) {
344 add_argv("--set-counters");
345 add_argv((char *) pcnt);
346 add_argv((char *) bcnt);
347 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000348
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000349 /* After fighting with strtok enough, here's now
350 * a 'real' parser. According to Rusty I'm now no
351 * longer a real hacker, but I can live with that */
András Kis-Szabó2f523792001-02-27 09:59:48 +0000352
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000353 quote_open = 0;
354 param_start = parsestart;
355
356 for (curchar = parsestart; *curchar; curchar++) {
357 if (*curchar == '"') {
Michael Rash29cdbd02004-01-05 09:41:50 +0000358 /* quote_open cannot be true if there
359 * was no previous character. Thus,
360 * curchar-1 has to be within bounds */
361 if (quote_open &&
362 *(curchar-1) != '\\') {
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000363 quote_open = 0;
364 *curchar = ' ';
365 } else {
366 quote_open = 1;
367 param_start++;
368 }
369 }
370 if (*curchar == ' '
371 || *curchar == '\t'
372 || * curchar == '\n') {
373 char param_buffer[1024];
374 int param_len = curchar-param_start;
Harald Welte885c6eb2001-10-04 08:30:46 +0000375
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000376 if (quote_open)
377 continue;
Harald Welte885c6eb2001-10-04 08:30:46 +0000378
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000379 if (!param_len) {
380 /* two spaces? */
381 param_start++;
382 continue;
383 }
384
385 /* end of one parameter */
386 strncpy(param_buffer, param_start,
387 param_len);
388 *(param_buffer+param_len) = '\0';
Harald Weltebb41f882001-10-21 14:11:54 +0000389
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000390 /* check if table name specified */
Harald Welte48a6f902001-10-22 15:16:21 +0000391 if (!strncmp(param_buffer, "-t", 3)
392 || !strncmp(param_buffer, "--table", 8)) {
Harald Weltebb41f882001-10-21 14:11:54 +0000393 exit_error(PARAMETER_PROBLEM,
394 "Line %u seems to have a "
395 "-t table option.\n", line);
396 exit(1);
397 }
398
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000399 add_argv(param_buffer);
400 param_start += param_len + 1;
401 } else {
402 /* regular character, skip */
403 }
404 }
Harald Welte885c6eb2001-10-04 08:30:46 +0000405
406 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000407 newargc, curtable);
Harald Welte885c6eb2001-10-04 08:30:46 +0000408
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000409 for (a = 0; a < newargc; a++)
András Kis-Szabó2f523792001-02-27 09:59:48 +0000410 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
411
Harald Welte885c6eb2001-10-04 08:30:46 +0000412 ret = do_command6(newargc, newargv,
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000413 &newargv[2], &handle);
Harald Welte885c6eb2001-10-04 08:30:46 +0000414
András Kis-Szabó97fd91a2002-03-03 09:44:31 +0000415 free_argv();
András Kis-Szabó2f523792001-02-27 09:59:48 +0000416 }
417 if (!ret) {
418 fprintf(stderr, "%s: line %u failed\n",
419 program_name, line);
420 exit(1);
421 }
422 }
Martin Josefssone0dc5812004-02-02 19:58:36 +0000423 if (in_table) {
424 fprintf(stderr, "%s: COMMIT expected at line %u\n",
425 program_name, line + 1);
426 exit(1);
427 }
András Kis-Szabó2f523792001-02-27 09:59:48 +0000428
429 return 0;
430}