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,v 1.12 2003/05/02 15:30:11 laforge Exp $
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", 1, 0, 'v' },
34 { "help", 0, 0, 'h' },
35 { "noflush", 0, 0, 'n'},
36 { "modprobe", 1, 0, 'M'},
40 static void print_usage(const char *name, const char *version) __attribute__((noreturn));
42 static void print_usage(const char *name, const char *version)
44 fprintf(stderr, "Usage: %s [-b] [-c] [-v] [-h]\n"
50 " [ --modprobe=<command>]\n", name);
55 ip6tc_handle_t create_handle(const char *tablename, const char* modprobe)
57 ip6tc_handle_t handle;
59 handle = ip6tc_init(tablename);
62 /* try to insmod the module if iptc_init failed */
63 ip6tables_insmod("ip6_tables", modprobe);
64 handle = ip6tc_init(tablename);
68 exit_error(PARAMETER_PROBLEM, "%s: unable to initialize"
69 "table '%s'\n", program_name, tablename);
75 int parse_counters(char *string, struct ip6t_counters *ctr)
77 return (sscanf(string, "[%llu:%llu]", &ctr->pcnt, &ctr->bcnt) == 2);
80 /* global new argv and argc */
81 static char *newargv[255];
84 /* function adding one argument to newargv, updating newargc
85 * returns true if argument added, false otherwise */
86 static int add_argv(char *what) {
87 DEBUGP("add_argv: %s\n", what);
88 if (what && ((newargc + 1) < sizeof(newargv)/sizeof(char *))) {
89 newargv[newargc] = strdup(what);
96 static void free_argv(void) {
99 for (i = 0; i < newargc; i++)
103 int main(int argc, char *argv[])
105 ip6tc_handle_t handle = NULL;
108 char curtable[IP6T_TABLE_MAXNAMELEN + 1];
110 const char *modprobe = 0;
113 program_name = "ip6tables-restore";
114 program_version = IPTABLES_VERSION;
117 #ifdef NO_SHARED_LIBS
121 while ((c = getopt_long(argc, argv, "bcvhnM:", options, NULL)) != -1) {
133 print_usage("ip6tables-restore",
145 if (optind == argc - 1) {
146 in = fopen(argv[optind], "r");
148 fprintf(stderr, "Can't open %s: %s", argv[optind],
153 else if (optind < argc) {
154 fprintf(stderr, "Unknown arguments found on commandline");
159 /* Grab standard input. */
160 while (fgets(buffer, sizeof(buffer), in)) {
164 if (buffer[0] == '\n') continue;
165 else if (buffer[0] == '#') {
166 if (verbose) fputs(buffer, stdout);
168 } else if ((strcmp(buffer, "COMMIT\n") == 0) && (in_table)) {
169 DEBUGP("Calling commit\n");
170 ret = ip6tc_commit(&handle);
172 } else if ((buffer[0] == '*') && (!in_table)) {
176 table = strtok(buffer+1, " \t\n");
177 DEBUGP("line %u, table '%s'\n", line, table);
179 exit_error(PARAMETER_PROBLEM,
180 "%s: line %u table name invalid\n",
184 strncpy(curtable, table, IP6T_TABLE_MAXNAMELEN);
189 handle = create_handle(table, modprobe);
191 DEBUGP("Cleaning all chains of table '%s'\n",
193 for_each_chain(flush_entries, verbose, 1,
196 DEBUGP("Deleting all user-defined chains "
197 "of table '%s'\n", table);
198 for_each_chain(delete_chain, verbose, 0,
205 } else if ((buffer[0] == ':') && (in_table)) {
207 char *policy, *chain;
209 chain = strtok(buffer+1, " \t\n");
210 DEBUGP("line %u, chain '%s'\n", line, chain);
212 exit_error(PARAMETER_PROBLEM,
213 "%s: line %u chain name invalid\n",
218 if (!ip6tc_builtin(chain, handle)) {
219 DEBUGP("Creating new chain '%s'\n", chain);
220 if (!ip6tc_create_chain(chain, &handle))
221 exit_error(PARAMETER_PROBLEM,
222 "error creating chain "
227 policy = strtok(NULL, " \t\n");
228 DEBUGP("line %u, policy '%s'\n", line, policy);
230 exit_error(PARAMETER_PROBLEM,
231 "%s: line %u policy invalid\n",
236 if (strcmp(policy, "-") != 0) {
237 struct ip6t_counters count;
241 ctrs = strtok(NULL, " \t\n");
243 parse_counters(ctrs, &count);
247 sizeof(struct ip6t_counters));
250 DEBUGP("Setting policy of chain %s to %s\n",
253 if (!ip6tc_set_policy(chain, policy, &count,
255 exit_error(OTHER_PROBLEM,
256 "Can't set policy `%s'"
257 " on `%s' line %u: %s\n",
259 ip6tc_strerror(errno));
264 } else if (in_table) {
272 char *param_start, *curchar;
275 /* reset the newargv */
278 if (buffer[0] == '[') {
279 /* we have counters in our input */
280 ptr = strchr(buffer, ']');
282 exit_error(PARAMETER_PROBLEM,
283 "Bad line %u: need ]\n",
286 pcnt = strtok(buffer+1, ":");
288 exit_error(PARAMETER_PROBLEM,
289 "Bad line %u: need :\n",
292 bcnt = strtok(NULL, "]");
294 exit_error(PARAMETER_PROBLEM,
295 "Bad line %u: need ]\n",
298 /* start command parsing after counter */
299 parsestart = ptr + 1;
301 /* start command parsing at start of line */
307 add_argv((char *) &curtable);
309 if (counters && pcnt && bcnt) {
310 add_argv("--set-counters");
311 add_argv((char *) pcnt);
312 add_argv((char *) bcnt);
315 /* After fighting with strtok enough, here's now
316 * a 'real' parser. According to Rusty I'm now no
317 * longer a real hacker, but I can live with that */
320 param_start = parsestart;
322 for (curchar = parsestart; *curchar; curchar++) {
323 if (*curchar == '"') {
334 || * curchar == '\n') {
335 char param_buffer[1024];
336 int param_len = curchar-param_start;
347 /* end of one parameter */
348 strncpy(param_buffer, param_start,
350 *(param_buffer+param_len) = '\0';
352 /* check if table name specified */
353 if (!strncmp(param_buffer, "-t", 3)
354 || !strncmp(param_buffer, "--table", 8)) {
355 exit_error(PARAMETER_PROBLEM,
356 "Line %u seems to have a "
357 "-t table option.\n", line);
361 add_argv(param_buffer);
362 param_start += param_len + 1;
364 /* regular character, skip */
368 DEBUGP("calling do_command6(%u, argv, &%s, handle):\n",
371 for (a = 0; a < newargc; a++)
372 DEBUGP("argv[%u]: %s\n", a, newargv[a]);
374 ret = do_command6(newargc, newargv,
375 &newargv[2], &handle);
380 fprintf(stderr, "%s: line %u failed\n",