1 /* Code to restore the iptables state, from file by ip6tables-save.
2 * Author: Andras Kis-Szabo <kisza@sch.bme.hu>
4 * based on iptables-restore
6 * Harald Welte <laforge@gnumonks.org>
7 * Rusty Russell <rusty@linuxcare.com.au>
8 * This code is distributed under the terms of GNU GPL v2
10 * $Id: ip6tables-restore.c 6828 2007-05-10 15:00:39Z /C=EU/ST=EU/CN=Patrick McHardy/emailAddress=kaber@trash.net $
14 #include <sys/errno.h>
18 #include "ip6tables.h"
19 #include "libiptc/libip6tc.h"
22 #define DEBUGP(x, args...) fprintf(stderr, x, ## args)
24 #define DEBUGP(x, args...)
27 static int binary
= 0, counters
= 0, verbose
= 0, noflush
= 0;
29 /* Keeping track of external matches and targets. */
30 static struct option options
[] = {
31 { "binary", 0, 0, 'b' },
32 { "counters", 0, 0, 'c' },
33 { "verbose", 0, 0, 'v' },
34 { "test", 0, 0, 't' },
35 { "help", 0, 0, 'h' },
36 { "noflush", 0, 0, 'n'},
37 { "modprobe", 1, 0, 'M'},
41 static void print_usage(const char *name
, const char *version
) __attribute__((noreturn
));
43 static void print_usage(const char *name
, const char *version
)
45 fprintf(stderr
, "Usage: %s [-b] [-c] [-v] [-t] [-h]\n"
52 " [ --modprobe=<command>]\n", name
);
57 ip6tc_handle_t
create_handle(const char *tablename
, const char* modprobe
)
59 ip6tc_handle_t handle
;
61 handle
= ip6tc_init(tablename
);
64 /* try to insmod the module if iptc_init failed */
65 ip6tables_insmod("ip6_tables", modprobe
, 0);
66 handle
= ip6tc_init(tablename
);
70 exit_error(PARAMETER_PROBLEM
, "%s: unable to initialize "
71 "table '%s'\n", program_name
, tablename
);
77 static int parse_counters(char *string
, struct ip6t_counters
*ctr
)
79 unsigned long long pcnt
, bcnt
;
82 ret
= sscanf(string
, "[%llu:%llu]",
83 (unsigned long long *)&pcnt
,
84 (unsigned long long *)&bcnt
);
90 /* global new argv and argc */
91 static char *newargv
[255];
94 /* function adding one argument to newargv, updating newargc
95 * returns true if argument added, false otherwise */
96 static int add_argv(char *what
) {
97 DEBUGP("add_argv: %s\n", what
);
98 if (what
&& ((newargc
+ 1) < sizeof(newargv
)/sizeof(char *))) {
99 newargv
[newargc
] = strdup(what
);
106 static void free_argv(void) {
109 for (i
= 0; i
< newargc
; i
++)
113 #ifdef IPTABLES_MULTI
114 int ip6tables_restore_main(int argc
, char *argv
[])
116 int main(int argc
, char *argv
[])
119 ip6tc_handle_t handle
= NULL
;
122 char curtable
[IP6T_TABLE_MAXNAMELEN
+ 1];
124 const char *modprobe
= 0;
125 int in_table
= 0, testing
= 0;
127 program_name
= "ip6tables-restore";
128 program_version
= IPTABLES_VERSION
;
131 lib_dir
= getenv("IP6TABLES_LIB_DIR");
133 lib_dir
= IP6T_LIB_DIR
;
135 #ifdef NO_SHARED_LIBS
139 while ((c
= getopt_long(argc
, argv
, "bcvthnM:", options
, NULL
)) != -1) {
154 print_usage("ip6tables-restore",
166 if (optind
== argc
- 1) {
167 in
= fopen(argv
[optind
], "r");
169 fprintf(stderr
, "Can't open %s: %s\n", argv
[optind
],
174 else if (optind
< argc
) {
175 fprintf(stderr
, "Unknown arguments found on commandline\n");
180 /* Grab standard input. */
181 while (fgets(buffer
, sizeof(buffer
), in
)) {
185 if (buffer
[0] == '\n')
187 else if (buffer
[0] == '#') {
189 fputs(buffer
, stdout
);
191 } else if ((strcmp(buffer
, "COMMIT\n") == 0) && (in_table
)) {
193 DEBUGP("Calling commit\n");
194 ret
= ip6tc_commit(&handle
);
196 DEBUGP("Not calling commit, testing\n");
200 } else if ((buffer
[0] == '*') && (!in_table
)) {
204 table
= strtok(buffer
+1, " \t\n");
205 DEBUGP("line %u, table '%s'\n", line
, table
);
207 exit_error(PARAMETER_PROBLEM
,
208 "%s: line %u table name invalid\n",
212 strncpy(curtable
, table
, IP6T_TABLE_MAXNAMELEN
);
213 curtable
[IP6T_TABLE_MAXNAMELEN
] = '\0';
218 handle
= create_handle(table
, modprobe
);
220 DEBUGP("Cleaning all chains of table '%s'\n",
222 for_each_chain(flush_entries
, verbose
, 1,
225 DEBUGP("Deleting all user-defined chains "
226 "of table '%s'\n", table
);
227 for_each_chain(delete_chain
, verbose
, 0,
234 } else if ((buffer
[0] == ':') && (in_table
)) {
236 char *policy
, *chain
;
238 chain
= strtok(buffer
+1, " \t\n");
239 DEBUGP("line %u, chain '%s'\n", line
, chain
);
241 exit_error(PARAMETER_PROBLEM
,
242 "%s: line %u chain name invalid\n",
247 if (ip6tc_builtin(chain
, handle
) <= 0) {
248 if (noflush
&& ip6tc_is_chain(chain
, handle
)) {
249 DEBUGP("Flushing existing user defined chain '%s'\n", chain
);
250 if (!ip6tc_flush_entries(chain
, &handle
))
251 exit_error(PARAMETER_PROBLEM
,
252 "error flushing chain "
256 DEBUGP("Creating new chain '%s'\n", chain
);
257 if (!ip6tc_create_chain(chain
, &handle
))
258 exit_error(PARAMETER_PROBLEM
,
259 "error creating chain "
265 policy
= strtok(NULL
, " \t\n");
266 DEBUGP("line %u, policy '%s'\n", line
, policy
);
268 exit_error(PARAMETER_PROBLEM
,
269 "%s: line %u policy invalid\n",
274 if (strcmp(policy
, "-") != 0) {
275 struct ip6t_counters count
;
279 ctrs
= strtok(NULL
, " \t\n");
281 if (!ctrs
|| !parse_counters(ctrs
, &count
))
282 exit_error(PARAMETER_PROBLEM
,
283 "invalid policy counters "
284 "for chain '%s'\n", chain
);
288 sizeof(struct ip6t_counters
));
291 DEBUGP("Setting policy of chain %s to %s\n",
294 if (!ip6tc_set_policy(chain
, policy
, &count
,
296 exit_error(OTHER_PROBLEM
,
297 "Can't set policy `%s'"
298 " on `%s' line %u: %s\n",
300 ip6tc_strerror(errno
));
305 } else if (in_table
) {
314 int quote_open
, escaped
;
317 /* reset the newargv */
320 if (buffer
[0] == '[') {
321 /* we have counters in our input */
322 ptr
= strchr(buffer
, ']');
324 exit_error(PARAMETER_PROBLEM
,
325 "Bad line %u: need ]\n",
328 pcnt
= strtok(buffer
+1, ":");
330 exit_error(PARAMETER_PROBLEM
,
331 "Bad line %u: need :\n",
334 bcnt
= strtok(NULL
, "]");
336 exit_error(PARAMETER_PROBLEM
,
337 "Bad line %u: need ]\n",
340 /* start command parsing after counter */
341 parsestart
= ptr
+ 1;
343 /* start command parsing at start of line */
349 add_argv((char *) &curtable
);
351 if (counters
&& pcnt
&& bcnt
) {
352 add_argv("--set-counters");
353 add_argv((char *) pcnt
);
354 add_argv((char *) bcnt
);
357 /* After fighting with strtok enough, here's now
358 * a 'real' parser. According to Rusty I'm now no
359 * longer a real hacker, but I can live with that */
365 for (curchar
= parsestart
; *curchar
; curchar
++) {
366 char param_buffer
[1024];
370 param_buffer
[param_len
++] = *curchar
;
373 } else if (*curchar
== '\\') {
376 } else if (*curchar
== '"') {
380 param_buffer
[param_len
++] = *curchar
;
384 if (*curchar
== '"') {
392 || * curchar
== '\n') {
398 param_buffer
[param_len
] = '\0';
400 /* check if table name specified */
401 if (!strncmp(param_buffer
, "-t", 2)
402 || !strncmp(param_buffer
, "--table", 8)) {
403 exit_error(PARAMETER_PROBLEM
,
404 "Line %u seems to have a "
405 "-t table option.\n", line
);
409 add_argv(param_buffer
);
412 /* regular character, copy to buffer */
413 param_buffer
[param_len
++] = *curchar
;
415 if (param_len
>= sizeof(param_buffer
))
416 exit_error(PARAMETER_PROBLEM
,
417 "Parameter too long!");
421 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
424 for (a
= 0; a
< newargc
; a
++)
425 DEBUGP("argv[%u]: %s\n", a
, newargv
[a
]);
427 ret
= do_command6(newargc
, newargv
,
428 &newargv
[2], &handle
);
434 fprintf(stderr
, "%s: line %u failed\n",
440 fprintf(stderr
, "%s: COMMIT expected at line %u\n",
441 program_name
, line
+ 1);