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"
34 static bool print_rules = false;
36 static struct fw3_state *run_state = NULL;
37 static struct fw3_state *cfg_state = NULL;
41 build_state(bool runtime)
43 struct fw3_state *state = NULL;
44 struct uci_package *p = NULL;
47 state = malloc(sizeof(*state));
50 error("Out of memory");
52 memset(state, 0, sizeof(*state));
53 state->uci = uci_alloc_context();
56 error("Out of memory");
60 sf = fopen(FW3_STATEFILE, "r");
64 uci_import(state->uci, sf, "fw3_state", &p, true);
70 uci_free_context(state->uci);
76 state->statefile = true;
82 if (!fw3_ubus_connect())
83 error("Failed to connect to ubus");
85 if (uci_load(state->uci, "firewall", &p))
87 uci_perror(state->uci, NULL);
88 error("Failed to load /etc/config/firewall");
91 if (!fw3_find_command("ipset"))
93 warn("Unable to locate ipset utility, disabling ipset support");
94 state->disable_ipsets = true;
100 fw3_load_defaults(state, p);
101 fw3_load_ipsets(state, p);
102 fw3_load_zones(state, p);
103 fw3_load_rules(state, p);
104 fw3_load_redirects(state, p);
105 fw3_load_forwards(state, p);
106 fw3_load_includes(state, p);
112 free_state(struct fw3_state *state)
114 struct list_head *cur, *tmp;
116 list_for_each_safe(cur, tmp, &state->zones)
117 fw3_free_zone((struct fw3_zone *)cur);
119 list_for_each_safe(cur, tmp, &state->rules)
120 fw3_free_rule((struct fw3_rule *)cur);
122 list_for_each_safe(cur, tmp, &state->redirects)
123 fw3_free_redirect((struct fw3_redirect *)cur);
125 list_for_each_safe(cur, tmp, &state->forwards)
126 fw3_free_forward((struct fw3_forward *)cur);
128 list_for_each_safe(cur, tmp, &state->ipsets)
129 fw3_free_ipset((struct fw3_ipset *)cur);
131 list_for_each_safe(cur, tmp, &state->includes)
132 fw3_free_include((struct fw3_include *)cur);
134 uci_free_context(state->uci);
138 fw3_ubus_disconnect();
143 family_running(enum fw3_family family)
145 return (run_state && has(run_state->defaults.flags, family, family));
149 family_set(struct fw3_state *state, enum fw3_family family, bool set)
155 set(state->defaults.flags, family, family);
157 del(state->defaults.flags, family, family);
166 enum fw3_family family;
167 enum fw3_table table;
168 struct fw3_ipt_handle *handle;
170 if (!complete && !run_state)
172 warn("The firewall appears to be stopped. "
173 "Use the 'flush' command to forcefully purge all rules.");
178 if (!print_rules && run_state)
179 fw3_hotplug_zones(run_state, false);
181 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
183 if (!complete && !family_running(family))
186 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
188 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
191 if (!(handle = fw3_ipt_open(family, table)))
194 info(" * %sing %s %s table", complete ? "Flush" : "Clear",
195 fw3_flag_names[family], fw3_flag_names[table]);
199 fw3_flush_all(handle);
203 fw3_flush_rules(handle, run_state, false);
204 fw3_flush_zones(handle, run_state, false);
207 fw3_ipt_commit(handle);
210 family_set(run_state, family, false);
211 family_set(cfg_state, family, false);
218 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
220 fw3_destroy_ipsets(run_state);
225 if (complete && (ct = fopen("/proc/net/nf_conntrack", "w")) != NULL)
227 info(" * Flushing conntrack table ...");
229 fwrite("f\n", 2, 1, ct);
233 if (!rv && run_state)
234 fw3_write_statefile(run_state);
243 enum fw3_family family;
244 enum fw3_table table;
245 struct fw3_ipt_handle *handle;
249 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
251 fw3_create_ipsets(cfg_state);
256 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
258 if (family == FW3_FAMILY_V6 && cfg_state->defaults.disable_ipv6)
261 if (!print_rules && family_running(family))
263 warn("The %s firewall appears to be started already. "
264 "If it is indeed empty, remove the %s file and retry.",
265 fw3_flag_names[family], FW3_STATEFILE);
270 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
272 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
275 if (!(handle = fw3_ipt_open(family, table)))
278 info(" * Populating %s %s table",
279 fw3_flag_names[family], fw3_flag_names[table]);
281 fw3_print_default_chains(handle, cfg_state, false);
282 fw3_print_zone_chains(handle, cfg_state, false);
283 fw3_print_default_head_rules(handle, cfg_state, false);
284 fw3_print_rules(handle, cfg_state);
285 fw3_print_redirects(handle, cfg_state);
286 fw3_print_forwards(handle, cfg_state);
287 fw3_print_zone_rules(handle, cfg_state, false);
288 fw3_print_default_tail_rules(handle, cfg_state, false);
290 fw3_ipt_commit(handle);
293 //fw3_print_includes(cfg_state, family, false);
295 family_set(run_state, family, true);
296 family_set(cfg_state, family, true);
303 fw3_set_defaults(cfg_state);
307 fw3_run_includes(cfg_state, false);
308 fw3_hotplug_zones(cfg_state, true);
309 fw3_write_statefile(cfg_state);
321 enum fw3_family family;
322 enum fw3_table table;
323 struct fw3_ipt_handle *handle;
325 if (!print_rules && run_state)
326 fw3_hotplug_zones(run_state, false);
328 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
330 if (!family_running(family))
333 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
335 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
338 if (!(handle = fw3_ipt_open(family, table)))
341 info(" * Clearing %s %s table",
342 fw3_flag_names[family], fw3_flag_names[table]);
346 fw3_flush_rules(handle, run_state, true);
347 fw3_flush_zones(handle, run_state, true);
350 fw3_ipt_commit(handle);
353 family_set(run_state, family, false);
354 family_set(cfg_state, family, false);
357 if (family == FW3_FAMILY_V6 && cfg_state->defaults.disable_ipv6)
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 if (!(handle = fw3_ipt_open(family, table)))
368 info(" * Populating %s %s table",
369 fw3_flag_names[family], fw3_flag_names[table]);
371 fw3_print_default_chains(handle, cfg_state, true);
372 fw3_print_zone_chains(handle, cfg_state, true);
373 fw3_print_default_head_rules(handle, cfg_state, true);
374 fw3_print_rules(handle, cfg_state);
375 fw3_print_redirects(handle, cfg_state);
376 fw3_print_forwards(handle, cfg_state);
377 fw3_print_zone_rules(handle, cfg_state, true);
378 fw3_print_default_tail_rules(handle, cfg_state, true);
380 fw3_ipt_commit(handle);
383 //fw3_print_includes(cfg_state, family, true);
385 family_set(run_state, family, true);
386 family_set(cfg_state, family, true);
393 fw3_set_defaults(cfg_state);
397 fw3_run_includes(cfg_state, true);
398 fw3_hotplug_zones(cfg_state, true);
399 fw3_write_statefile(cfg_state);
407 lookup_network(const char *net)
410 struct fw3_device *d;
412 list_for_each_entry(z, &cfg_state->zones, list)
414 list_for_each_entry(d, &z->networks, list)
416 if (!strcmp(d->name, net))
418 printf("%s\n", z->name);
428 lookup_device(const char *dev)
431 struct fw3_device *d;
433 list_for_each_entry(z, &cfg_state->zones, list)
435 list_for_each_entry(d, &z->devices, list)
437 if (!strcmp(d->name, dev))
439 printf("%s\n", z->name);
451 fprintf(stderr, "fw3 [-4] [-6] [-q] print\n");
452 fprintf(stderr, "fw3 [-q] {start|stop|flush|reload|restart}\n");
453 fprintf(stderr, "fw3 [-q] network {net}\n");
454 fprintf(stderr, "fw3 [-q] device {dev}\n");
460 int main(int argc, char **argv)
463 struct fw3_defaults *defs = NULL;
464 enum fw3_family use_family = FW3_FAMILY_ANY;
466 while ((ch = getopt(argc, argv, "46dqh")) != -1)
471 use_family = FW3_FAMILY_V4;
475 use_family = FW3_FAMILY_V6;
483 freopen("/dev/null", "w", stderr);
494 defs = &cfg_state->defaults;
502 if (!strcmp(argv[optind], "print"))
504 if (use_family == FW3_FAMILY_ANY)
505 use_family = FW3_FAMILY_V4;
506 else if (use_family == FW3_FAMILY_V6 && defs->disable_ipv6)
507 warn("IPv6 rules globally disabled in configuration");
509 freopen("/dev/null", "w", stderr);
511 cfg_state->disable_ipsets = true;
516 else if (!strcmp(argv[optind], "start"))
524 else if (!strcmp(argv[optind], "stop"))
532 else if (!strcmp(argv[optind], "flush"))
540 else if (!strcmp(argv[optind], "restart"))
549 else if (!strcmp(argv[optind], "reload"))
557 else if (!strcmp(argv[optind], "network") && (optind + 1) < argc)
559 rv = lookup_network(argv[optind + 1]);
561 else if (!strcmp(argv[optind], "device") && (optind + 1) < argc)
563 rv = lookup_device(argv[optind + 1]);
572 free_state(cfg_state);
575 free_state(run_state);