2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013-2014 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"
35 static enum fw3_family print_family = FW3_FAMILY_ANY;
37 static struct fw3_state *run_state = NULL;
38 static struct fw3_state *cfg_state = NULL;
42 build_state(bool runtime)
44 struct fw3_state *state = NULL;
45 struct uci_package *p = NULL;
48 state = malloc(sizeof(*state));
51 error("Out of memory");
53 memset(state, 0, sizeof(*state));
54 state->uci = uci_alloc_context();
57 error("Out of memory");
61 sf = fopen(FW3_STATEFILE, "r");
65 uci_import(state->uci, sf, "fw3_state", &p, true);
71 uci_free_context(state->uci);
77 state->statefile = true;
83 if (!fw3_ubus_connect())
84 error("Failed to connect to ubus");
86 if (uci_load(state->uci, "firewall", &p))
88 uci_perror(state->uci, NULL);
89 error("Failed to load /etc/config/firewall");
92 if (!fw3_find_command("ipset"))
94 warn("Unable to locate ipset utility, disabling ipset support");
95 state->disable_ipsets = true;
102 struct blob_buf b = {NULL, NULL, 0, NULL};
105 fw3_load_defaults(state, p);
106 fw3_load_ipsets(state, p);
107 fw3_load_zones(state, p);
108 fw3_load_rules(state, p, b.head);
109 fw3_load_redirects(state, p);
110 fw3_load_snats(state, p, b.head);
111 fw3_load_forwards(state, p);
112 fw3_load_includes(state, p);
118 free_state(struct fw3_state *state)
120 struct list_head *cur, *tmp;
122 list_for_each_safe(cur, tmp, &state->zones)
123 fw3_free_zone((struct fw3_zone *)cur);
125 list_for_each_safe(cur, tmp, &state->rules)
126 fw3_free_rule((struct fw3_rule *)cur);
128 list_for_each_safe(cur, tmp, &state->redirects)
129 fw3_free_redirect((struct fw3_redirect *)cur);
131 list_for_each_safe(cur, tmp, &state->snats)
132 fw3_free_snat((struct fw3_snat *)cur);
134 list_for_each_safe(cur, tmp, &state->forwards)
135 fw3_free_forward((struct fw3_forward *)cur);
137 list_for_each_safe(cur, tmp, &state->ipsets)
138 fw3_free_ipset((struct fw3_ipset *)cur);
140 list_for_each_safe(cur, tmp, &state->includes)
141 fw3_free_include((struct fw3_include *)cur);
143 uci_free_context(state->uci);
147 fw3_ubus_disconnect();
152 family_running(enum fw3_family family)
154 return (run_state && has(run_state->defaults.flags, family, family));
158 family_set(struct fw3_state *state, enum fw3_family family, bool set)
164 set(state->defaults.flags, family, family);
166 del(state->defaults.flags, family, family);
175 enum fw3_family family;
176 enum fw3_table table;
177 struct fw3_ipt_handle *handle;
179 if (!complete && !run_state)
181 warn("The firewall appears to be stopped. "
182 "Use the 'flush' command to forcefully purge all rules.");
187 if (!print_family && run_state)
188 fw3_hotplug_zones(run_state, false);
190 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
192 if (!complete && !family_running(family))
195 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
197 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
200 if (!(handle = fw3_ipt_open(family, table)))
203 info(" * %sing %s %s table", complete ? "Flush" : "Clear",
204 fw3_flag_names[family], fw3_flag_names[table]);
208 fw3_flush_all(handle);
212 fw3_flush_rules(handle, run_state, false);
213 fw3_flush_zones(handle, run_state, false);
216 fw3_ipt_commit(handle);
217 fw3_ipt_close(handle);
220 family_set(run_state, family, false);
221 family_set(cfg_state, family, false);
227 fw3_destroy_ipsets(run_state);
229 if (complete && (ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
231 info(" * Flushing conntrack table ...");
233 fwrite("f\n", 2, 1, ct);
237 if (!rv && run_state)
238 fw3_write_statefile(run_state);
247 enum fw3_family family;
248 enum fw3_table table;
249 struct fw3_ipt_handle *handle;
252 fw3_create_ipsets(cfg_state);
254 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
256 if (family == FW3_FAMILY_V6 && cfg_state->defaults.disable_ipv6)
259 if (print_family && family != print_family)
262 if (!print_family && family_running(family))
264 warn("The %s firewall appears to be started already. "
265 "If it is indeed empty, remove the %s file and retry.",
266 fw3_flag_names[family], FW3_STATEFILE);
271 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
273 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
276 if (!(handle = fw3_ipt_open(family, table)))
279 info(" * Populating %s %s table",
280 fw3_flag_names[family], fw3_flag_names[table]);
282 fw3_print_default_chains(handle, cfg_state, false);
283 fw3_print_zone_chains(handle, cfg_state, false);
284 fw3_print_default_head_rules(handle, cfg_state, false);
285 fw3_print_rules(handle, cfg_state);
286 fw3_print_redirects(handle, cfg_state);
287 fw3_print_snats(handle, cfg_state);
288 fw3_print_forwards(handle, cfg_state);
289 fw3_print_zone_rules(handle, cfg_state, false);
290 fw3_print_default_tail_rules(handle, cfg_state, false);
293 fw3_ipt_commit(handle);
295 fw3_ipt_close(handle);
299 fw3_print_includes(cfg_state, family, false);
301 family_set(run_state, family, true);
302 family_set(cfg_state, family, true);
309 fw3_set_defaults(cfg_state);
313 fw3_run_includes(cfg_state, false);
314 fw3_hotplug_zones(cfg_state, true);
315 fw3_write_statefile(cfg_state);
327 enum fw3_family family;
328 enum fw3_table table;
329 struct fw3_ipt_handle *handle;
334 fw3_hotplug_zones(run_state, false);
336 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
338 if (!family_running(family))
341 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
343 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
346 if (!(handle = fw3_ipt_open(family, table)))
349 info(" * Clearing %s %s table",
350 fw3_flag_names[family], fw3_flag_names[table]);
352 fw3_flush_rules(handle, run_state, true);
353 fw3_flush_zones(handle, run_state, true);
354 fw3_ipt_commit(handle);
355 fw3_ipt_close(handle);
358 family_set(run_state, family, false);
359 family_set(cfg_state, family, false);
362 if (family == FW3_FAMILY_V6 && cfg_state->defaults.disable_ipv6)
365 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
367 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
370 if (!(handle = fw3_ipt_open(family, table)))
373 info(" * Populating %s %s table",
374 fw3_flag_names[family], fw3_flag_names[table]);
376 fw3_print_default_chains(handle, cfg_state, true);
377 fw3_print_zone_chains(handle, cfg_state, true);
378 fw3_print_default_head_rules(handle, cfg_state, true);
379 fw3_print_rules(handle, cfg_state);
380 fw3_print_redirects(handle, cfg_state);
381 fw3_print_snats(handle, cfg_state);
382 fw3_print_forwards(handle, cfg_state);
383 fw3_print_zone_rules(handle, cfg_state, true);
384 fw3_print_default_tail_rules(handle, cfg_state, true);
386 fw3_ipt_commit(handle);
387 fw3_ipt_close(handle);
390 fw3_print_includes(cfg_state, family, true);
392 family_set(run_state, family, true);
393 family_set(cfg_state, family, true);
400 fw3_set_defaults(cfg_state);
401 fw3_run_includes(cfg_state, true);
402 fw3_hotplug_zones(cfg_state, true);
403 fw3_write_statefile(cfg_state);
410 lookup_network(const char *net)
413 struct fw3_device *d;
415 list_for_each_entry(z, &cfg_state->zones, list)
417 list_for_each_entry(d, &z->networks, list)
419 if (!strcmp(d->name, net))
421 printf("%s\n", z->name);
431 lookup_device(const char *dev)
434 struct fw3_device *d;
436 list_for_each_entry(z, &cfg_state->zones, list)
438 list_for_each_entry(d, &z->devices, list)
440 if (!strcmp(d->name, dev))
442 printf("%s\n", z->name);
454 fprintf(stderr, "fw3 [-4] [-6] [-q] print\n");
455 fprintf(stderr, "fw3 [-q] {start|stop|flush|reload|restart}\n");
456 fprintf(stderr, "fw3 [-q] network {net}\n");
457 fprintf(stderr, "fw3 [-q] device {dev}\n");
463 int main(int argc, char **argv)
466 enum fw3_family family = FW3_FAMILY_ANY;
467 struct fw3_defaults *defs = NULL;
469 while ((ch = getopt(argc, argv, "46dqh")) != -1)
474 family = FW3_FAMILY_V4;
478 family = FW3_FAMILY_V6;
486 if (freopen("/dev/null", "w", stderr)) {}
497 defs = &cfg_state->defaults;
505 if (!strcmp(argv[optind], "print"))
507 if (family == FW3_FAMILY_ANY)
509 family = FW3_FAMILY_V4;
511 else if (family == FW3_FAMILY_V6)
513 if (defs->disable_ipv6)
514 warn("IPv6 rules globally disabled in configuration");
517 warn("IPv6 support is not compiled in");
521 if (freopen("/dev/null", "w", stderr)) {};
523 cfg_state->disable_ipsets = true;
524 print_family = family;
529 else if (!strcmp(argv[optind], "start"))
537 else if (!strcmp(argv[optind], "stop"))
545 else if (!strcmp(argv[optind], "flush"))
553 else if (!strcmp(argv[optind], "restart"))
562 else if (!strcmp(argv[optind], "reload"))
570 else if (!strcmp(argv[optind], "network") && (optind + 1) < argc)
572 rv = lookup_network(argv[optind + 1]);
574 else if (!strcmp(argv[optind], "device") && (optind + 1) < argc)
576 rv = lookup_device(argv[optind + 1]);
585 free_state(cfg_state);
588 free_state(run_state);