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 6460 2006-02-09 14:35:38Z /C=DE/ST=Berlin/L=Berlin/O=Netfilter Project/OU=Development/CN=laforge/emailAddress=laforge@netfilter.org $
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
);
66 handle
= ip6tc_init(tablename
);
70 exit_error(PARAMETER_PROBLEM
, "%s: unable to initialize"
71 "table '%s'\n", program_name
, tablename
);
77 int parse_counters(char *string
, struct ip6t_counters
*ctr
)
79 return (sscanf(string
, "[%llu:%llu]", (unsigned long long *)&ctr
->pcnt
, (unsigned long long *)&ctr
->bcnt
) == 2);
82 /* global new argv and argc */
83 static char *newargv
[255];
86 /* function adding one argument to newargv, updating newargc
87 * returns true if argument added, false otherwise */
88 static int add_argv(char *what
) {
89 DEBUGP("add_argv: %s\n", what
);
90 if (what
&& ((newargc
+ 1) < sizeof(newargv
)/sizeof(char *))) {
91 newargv
[newargc
] = strdup(what
);
98 static void free_argv(void) {
101 for (i
= 0; i
< newargc
; i
++)
105 int main(int argc
, char *argv
[])
107 ip6tc_handle_t handle
= NULL
;
110 char curtable
[IP6T_TABLE_MAXNAMELEN
+ 1];
112 const char *modprobe
= 0;
113 int in_table
= 0, testing
= 0;
115 program_name
= "ip6tables-restore";
116 program_version
= IPTABLES_VERSION
;
119 lib_dir
= getenv("IP6TABLES_LIB_DIR");
121 lib_dir
= IP6T_LIB_DIR
;
123 #ifdef NO_SHARED_LIBS
127 while ((c
= getopt_long(argc
, argv
, "bcvthnM:", options
, NULL
)) != -1) {
142 print_usage("ip6tables-restore",
154 if (optind
== argc
- 1) {
155 in
= fopen(argv
[optind
], "r");
157 fprintf(stderr
, "Can't open %s: %s", argv
[optind
],
162 else if (optind
< argc
) {
163 fprintf(stderr
, "Unknown arguments found on commandline");
168 /* Grab standard input. */
169 while (fgets(buffer
, sizeof(buffer
), in
)) {
173 if (buffer
[0] == '\n')
175 else if (buffer
[0] == '#') {
177 fputs(buffer
, stdout
);
179 } else if ((strcmp(buffer
, "COMMIT\n") == 0) && (in_table
)) {
181 DEBUGP("Calling commit\n");
182 ret
= ip6tc_commit(&handle
);
184 DEBUGP("Not calling commit, testing\n");
188 } else if ((buffer
[0] == '*') && (!in_table
)) {
192 table
= strtok(buffer
+1, " \t\n");
193 DEBUGP("line %u, table '%s'\n", line
, table
);
195 exit_error(PARAMETER_PROBLEM
,
196 "%s: line %u table name invalid\n",
200 strncpy(curtable
, table
, IP6T_TABLE_MAXNAMELEN
);
201 curtable
[IP6T_TABLE_MAXNAMELEN
] = '\0';
206 handle
= create_handle(table
, modprobe
);
208 DEBUGP("Cleaning all chains of table '%s'\n",
210 for_each_chain(flush_entries
, verbose
, 1,
213 DEBUGP("Deleting all user-defined chains "
214 "of table '%s'\n", table
);
215 for_each_chain(delete_chain
, verbose
, 0,
222 } else if ((buffer
[0] == ':') && (in_table
)) {
224 char *policy
, *chain
;
226 chain
= strtok(buffer
+1, " \t\n");
227 DEBUGP("line %u, chain '%s'\n", line
, chain
);
229 exit_error(PARAMETER_PROBLEM
,
230 "%s: line %u chain name invalid\n",
235 if (ip6tc_builtin(chain
, handle
) <= 0) {
236 if (noflush
&& ip6tc_is_chain(chain
, handle
)) {
237 DEBUGP("Flushing existing user defined chain '%s'\n", chain
);
238 if (!ip6tc_flush_entries(chain
, &handle
))
239 exit_error(PARAMETER_PROBLEM
,
240 "error flushing chain "
244 DEBUGP("Creating new chain '%s'\n", chain
);
245 if (!ip6tc_create_chain(chain
, &handle
))
246 exit_error(PARAMETER_PROBLEM
,
247 "error creating chain "
253 policy
= strtok(NULL
, " \t\n");
254 DEBUGP("line %u, policy '%s'\n", line
, policy
);
256 exit_error(PARAMETER_PROBLEM
,
257 "%s: line %u policy invalid\n",
262 if (strcmp(policy
, "-") != 0) {
263 struct ip6t_counters count
;
267 ctrs
= strtok(NULL
, " \t\n");
269 if (!ctrs
|| !parse_counters(ctrs
, &count
))
270 exit_error(PARAMETER_PROBLEM
,
271 "invalid policy counters "
272 "for chain '%s'\n", chain
);
276 sizeof(struct ip6t_counters
));
279 DEBUGP("Setting policy of chain %s to %s\n",
282 if (!ip6tc_set_policy(chain
, policy
, &count
,
284 exit_error(OTHER_PROBLEM
,
285 "Can't set policy `%s'"
286 " on `%s' line %u: %s\n",
288 ip6tc_strerror(errno
));
293 } else if (in_table
) {
301 char *param_start
, *curchar
;
304 /* reset the newargv */
307 if (buffer
[0] == '[') {
308 /* we have counters in our input */
309 ptr
= strchr(buffer
, ']');
311 exit_error(PARAMETER_PROBLEM
,
312 "Bad line %u: need ]\n",
315 pcnt
= strtok(buffer
+1, ":");
317 exit_error(PARAMETER_PROBLEM
,
318 "Bad line %u: need :\n",
321 bcnt
= strtok(NULL
, "]");
323 exit_error(PARAMETER_PROBLEM
,
324 "Bad line %u: need ]\n",
327 /* start command parsing after counter */
328 parsestart
= ptr
+ 1;
330 /* start command parsing at start of line */
336 add_argv((char *) &curtable
);
338 if (counters
&& pcnt
&& bcnt
) {
339 add_argv("--set-counters");
340 add_argv((char *) pcnt
);
341 add_argv((char *) bcnt
);
344 /* After fighting with strtok enough, here's now
345 * a 'real' parser. According to Rusty I'm now no
346 * longer a real hacker, but I can live with that */
349 param_start
= parsestart
;
351 for (curchar
= parsestart
; *curchar
; curchar
++) {
352 if (*curchar
== '"') {
353 /* quote_open cannot be true if there
354 * was no previous character. Thus,
355 * curchar-1 has to be within bounds */
357 *(curchar
-1) != '\\') {
367 || * curchar
== '\n') {
368 char param_buffer
[1024];
369 int param_len
= curchar
-param_start
;
380 /* end of one parameter */
381 strncpy(param_buffer
, param_start
,
383 *(param_buffer
+param_len
) = '\0';
385 /* check if table name specified */
386 if (!strncmp(param_buffer
, "-t", 3)
387 || !strncmp(param_buffer
, "--table", 8)) {
388 exit_error(PARAMETER_PROBLEM
,
389 "Line %u seems to have a "
390 "-t table option.\n", line
);
394 add_argv(param_buffer
);
395 param_start
+= param_len
+ 1;
397 /* regular character, skip */
401 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
404 for (a
= 0; a
< newargc
; a
++)
405 DEBUGP("argv[%u]: %s\n", a
, newargv
[a
]);
407 ret
= do_command6(newargc
, newargv
,
408 &newargv
[2], &handle
);
413 fprintf(stderr
, "%s: line %u failed\n",
419 fprintf(stderr
, "%s: COMMIT expected at line %u\n",
420 program_name
, line
+ 1);