2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 #include "redirects.h"
33 static bool print_rules = false;
35 static struct fw3_state *run_state = NULL;
36 static struct fw3_state *cfg_state = NULL;
40 build_state(bool runtime)
42 struct fw3_state *state = NULL;
43 struct uci_package *p = NULL;
46 state = malloc(sizeof(*state));
49 error("Out of memory");
51 memset(state, 0, sizeof(*state));
52 state->uci = uci_alloc_context();
55 error("Out of memory");
59 sf = fopen(FW3_STATEFILE, "r");
63 uci_import(state->uci, sf, "fw3_state", &p, true);
69 uci_free_context(state->uci);
75 state->statefile = true;
81 if (!fw3_ubus_connect())
82 error("Failed to connect to ubus");
84 if (uci_load(state->uci, "firewall", &p))
86 uci_perror(state->uci, NULL);
87 error("Failed to load /etc/config/firewall");
90 if (!fw3_find_command("ipset"))
92 warn("Unable to locate ipset utility, disabling ipset support");
93 state->disable_ipsets = true;
99 fw3_load_defaults(state, p);
100 fw3_load_ipsets(state, p);
101 fw3_load_zones(state, p);
102 fw3_load_rules(state, p);
103 fw3_load_redirects(state, p);
104 fw3_load_forwards(state, p);
105 fw3_load_includes(state, p);
111 free_state(struct fw3_state *state)
113 struct list_head *cur, *tmp;
115 list_for_each_safe(cur, tmp, &state->zones)
116 fw3_free_zone((struct fw3_zone *)cur);
118 list_for_each_safe(cur, tmp, &state->rules)
119 fw3_free_rule((struct fw3_rule *)cur);
121 list_for_each_safe(cur, tmp, &state->redirects)
122 fw3_free_redirect((struct fw3_redirect *)cur);
124 list_for_each_safe(cur, tmp, &state->forwards)
125 fw3_free_forward((struct fw3_forward *)cur);
127 list_for_each_safe(cur, tmp, &state->ipsets)
128 fw3_free_ipset((struct fw3_ipset *)cur);
130 list_for_each_safe(cur, tmp, &state->includes)
131 fw3_free_include((struct fw3_include *)cur);
133 uci_free_context(state->uci);
137 fw3_ubus_disconnect();
142 restore_pipe(enum fw3_family family, bool silent)
146 cmd = (family == FW3_FAMILY_V4) ? "iptables-restore" : "ip6tables-restore";
149 return fw3_stdout_pipe();
151 if (!fw3_command_pipe(silent, cmd, "--lenient", "--noflush"))
153 warn("Unable to execute %s", cmd);
161 family_running(enum fw3_family family)
163 return (run_state && has(run_state->defaults.flags, family, family));
167 family_set(struct fw3_state *state, enum fw3_family family, bool set)
173 set(state->defaults.flags, family, family);
175 del(state->defaults.flags, family, family);
184 enum fw3_family family;
185 enum fw3_table table;
187 if (!complete && !run_state)
189 warn("The firewall appears to be stopped. "
190 "Use the 'flush' command to forcefully purge all rules.");
195 if (!print_rules && run_state)
196 fw3_hotplug_zones(run_state, false);
198 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
200 if (!complete && !family_running(family))
203 if (!restore_pipe(family, true))
206 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
208 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
211 info(" * %sing %s %s table", complete ? "Flush" : "Clear",
212 fw3_flag_names[family], fw3_flag_names[table]);
214 fw3_pr("*%s\n", fw3_flag_names[table]);
218 fw3_flush_all(table);
223 fw3_flush_rules(run_state, family, table, false, false);
224 fw3_flush_zones(run_state, family, table, false, false);
227 fw3_flush_rules(run_state, family, table, false, true);
228 fw3_flush_zones(run_state, family, table, false, true);
235 family_set(run_state, family, false);
236 family_set(cfg_state, family, false);
243 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
245 fw3_destroy_ipsets(run_state);
250 if (complete && (ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
252 info(" * Flushing conntrack table ...");
254 fwrite("f\n", 2, 1, ct);
258 if (!rv && run_state)
259 fw3_write_statefile(run_state);
268 enum fw3_family family;
269 enum fw3_table table;
273 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
275 fw3_create_ipsets(cfg_state);
280 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
282 if (family == FW3_FAMILY_V6 && cfg_state->defaults.disable_ipv6)
285 if (!print_rules && family_running(family))
287 warn("The %s firewall appears to be started already. "
288 "If it is indeed empty, remove the %s file and retry.",
289 fw3_flag_names[family], FW3_STATEFILE);
294 if (!restore_pipe(family, false))
297 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
299 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
302 info(" * Populating %s %s table",
303 fw3_flag_names[family], fw3_flag_names[table]);
305 fw3_pr("*%s\n", fw3_flag_names[table]);
306 fw3_print_default_chains(cfg_state, family, table, false);
307 fw3_print_zone_chains(cfg_state, family, table, false);
308 fw3_print_default_head_rules(cfg_state, family, table, false);
309 fw3_print_rules(cfg_state, family, table);
310 fw3_print_redirects(cfg_state, family, table);
311 fw3_print_forwards(cfg_state, family, table);
312 fw3_print_zone_rules(cfg_state, family, table, false);
313 fw3_print_default_tail_rules(cfg_state, family, table, false);
317 fw3_print_includes(cfg_state, family, false);
320 family_set(run_state, family, true);
321 family_set(cfg_state, family, true);
328 fw3_set_defaults(cfg_state);
332 fw3_run_includes(cfg_state, false);
333 fw3_hotplug_zones(cfg_state, true);
334 fw3_write_statefile(cfg_state);
346 enum fw3_family family;
347 enum fw3_table table;
349 if (!print_rules && run_state)
350 fw3_hotplug_zones(run_state, false);
352 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
354 if (!restore_pipe(family, true))
357 if (!family_running(family))
360 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
362 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
365 info(" * Clearing %s %s table",
366 fw3_flag_names[family], fw3_flag_names[table]);
368 fw3_pr("*%s\n", fw3_flag_names[table]);
373 fw3_flush_rules(run_state, family, table, true, false);
374 fw3_flush_zones(run_state, family, table, true, false);
377 fw3_flush_rules(run_state, family, table, true, true);
378 fw3_flush_zones(run_state, family, table, true, true);
384 family_set(run_state, family, false);
385 family_set(cfg_state, family, false);
388 if (family == FW3_FAMILY_V6 && cfg_state->defaults.disable_ipv6)
391 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
393 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
396 info(" * Populating %s %s table",
397 fw3_flag_names[family], fw3_flag_names[table]);
399 fw3_pr("*%s\n", fw3_flag_names[table]);
400 fw3_print_default_chains(cfg_state, family, table, true);
401 fw3_print_zone_chains(cfg_state, family, table, true);
402 fw3_print_default_head_rules(cfg_state, family, table, true);
403 fw3_print_rules(cfg_state, family, table);
404 fw3_print_redirects(cfg_state, family, table);
405 fw3_print_forwards(cfg_state, family, table);
406 fw3_print_zone_rules(cfg_state, family, table, true);
407 fw3_print_default_tail_rules(cfg_state, family, table, true);
411 fw3_print_includes(cfg_state, family, true);
413 family_set(run_state, family, true);
414 family_set(cfg_state, family, true);
424 fw3_set_defaults(cfg_state);
428 fw3_run_includes(cfg_state, true);
429 fw3_hotplug_zones(cfg_state, true);
430 fw3_write_statefile(cfg_state);
438 lookup_network(const char *net)
441 struct fw3_device *d;
443 list_for_each_entry(z, &cfg_state->zones, list)
445 list_for_each_entry(d, &z->networks, list)
447 if (!strcmp(d->name, net))
449 printf("%s\n", z->name);
459 lookup_device(const char *dev)
462 struct fw3_device *d;
464 list_for_each_entry(z, &cfg_state->zones, list)
466 list_for_each_entry(d, &z->devices, list)
468 if (!strcmp(d->name, dev))
470 printf("%s\n", z->name);
482 fprintf(stderr, "fw3 [-4] [-6] [-q] print\n");
483 fprintf(stderr, "fw3 [-q] {start|stop|flush|reload|restart}\n");
484 fprintf(stderr, "fw3 [-q] network {net}\n");
485 fprintf(stderr, "fw3 [-q] device {dev}\n");
491 int main(int argc, char **argv)
494 struct fw3_defaults *defs = NULL;
495 enum fw3_family use_family = FW3_FAMILY_ANY;
497 while ((ch = getopt(argc, argv, "46dqh")) != -1)
502 use_family = FW3_FAMILY_V4;
506 use_family = FW3_FAMILY_V6;
514 freopen("/dev/null", "w", stderr);
525 defs = &cfg_state->defaults;
533 if (!strcmp(argv[optind], "print"))
535 if (use_family == FW3_FAMILY_ANY)
536 use_family = FW3_FAMILY_V4;
537 else if (use_family == FW3_FAMILY_V6 && defs->disable_ipv6)
538 warn("IPv6 rules globally disabled in configuration");
540 freopen("/dev/null", "w", stderr);
542 cfg_state->disable_ipsets = true;
547 else if (!strcmp(argv[optind], "start"))
555 else if (!strcmp(argv[optind], "stop"))
563 else if (!strcmp(argv[optind], "flush"))
571 else if (!strcmp(argv[optind], "restart"))
580 else if (!strcmp(argv[optind], "reload"))
588 else if (!strcmp(argv[optind], "network") && (optind + 1) < argc)
590 rv = lookup_network(argv[optind + 1]);
592 else if (!strcmp(argv[optind], "device") && (optind + 1) < argc)
594 rv = lookup_device(argv[optind + 1]);
603 free_state(cfg_state);
606 free_state(run_state);