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"
32 static bool print_rules = false;
33 static enum fw3_family use_family = FW3_FAMILY_ANY;
36 static struct fw3_state *
39 struct fw3_state *state = NULL;
40 struct uci_package *p = NULL;
42 state = malloc(sizeof(*state));
45 error("Out of memory");
47 memset(state, 0, sizeof(*state));
48 state->uci = uci_alloc_context();
51 error("Out of memory");
53 if (uci_load(state->uci, "firewall", &p))
55 uci_perror(state->uci, NULL);
56 error("Failed to load /etc/config/firewall");
59 if (!fw3_find_command("ipset"))
61 warn("Unable to locate ipset utility, disabling ipset support");
62 state->disable_ipsets = true;
65 INIT_LIST_HEAD(&state->running_zones);
66 INIT_LIST_HEAD(&state->running_ipsets);
68 fw3_load_defaults(state, p);
69 fw3_load_ipsets(state, p);
70 fw3_load_zones(state, p);
71 fw3_load_rules(state, p);
72 fw3_load_redirects(state, p);
73 fw3_load_forwards(state, p);
75 state->statefile = fw3_read_statefile(state);
81 free_state(struct fw3_state *state)
83 struct list_head *cur, *tmp;
85 list_for_each_safe(cur, tmp, &state->zones)
86 fw3_free_zone((struct fw3_zone *)cur);
88 list_for_each_safe(cur, tmp, &state->rules)
89 fw3_free_rule((struct fw3_rule *)cur);
91 list_for_each_safe(cur, tmp, &state->redirects)
92 fw3_free_redirect((struct fw3_redirect *)cur);
94 list_for_each_safe(cur, tmp, &state->forwards)
95 fw3_free_forward((struct fw3_forward *)cur);
97 list_for_each_safe(cur, tmp, &state->ipsets)
98 fw3_free_ipset((struct fw3_ipset *)cur);
100 uci_free_context(state->uci);
104 fw3_ubus_disconnect();
109 restore_pipe(enum fw3_family family, bool silent)
113 cmd = (family == FW3_FAMILY_V4) ? "iptables-restore" : "ip6tables-restore";
116 return fw3_stdout_pipe();
118 if (!fw3_command_pipe(silent, cmd, "--lenient", "--noflush"))
120 warn("Unable to execute %s", cmd);
128 family_running(struct fw3_state *state, enum fw3_family family)
130 return hasbit(state->running_defaults.flags, family);
134 family_used(enum fw3_family family)
136 return (use_family == FW3_FAMILY_ANY) || (use_family == family);
140 family_loaded(struct fw3_state *state, enum fw3_family family)
142 return hasbit(state->defaults.flags, family);
146 family_set(struct fw3_state *state, enum fw3_family family, bool set)
149 setbit(state->defaults.flags, family);
151 delbit(state->defaults.flags, family);
155 stop(struct fw3_state *state, bool complete, bool restart)
158 enum fw3_family family;
159 enum fw3_table table;
161 if (!complete && !state->statefile)
164 warn("The firewall appears to be stopped. "
165 "Use the 'flush' command to forcefully purge all rules.");
170 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
172 if (!complete && !family_running(state, family))
175 if (!family_used(family) || !restore_pipe(family, true))
178 info("Removing %s rules ...", fw3_flag_names[family]);
180 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
182 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
185 info(" * %sing %s table",
186 complete ? "Flush" : "Clear", fw3_flag_names[table]);
188 fw3_pr("*%s\n", fw3_flag_names[table]);
192 fw3_flush_all(table);
197 fw3_flush_rules(table, family, false, state);
198 fw3_flush_zones(table, family, false, state);
201 fw3_flush_rules(table, family, true, state);
202 fw3_flush_zones(table, family, true, state);
211 family_set(state, family, false);
216 if (!restart && fw3_command_pipe(false, "ipset", "-exist", "-"))
218 fw3_destroy_ipsets(state);
223 fw3_write_statefile(state);
229 start(struct fw3_state *state, bool restart)
232 enum fw3_family family;
233 enum fw3_table table;
235 if (!print_rules && !restart &&
236 fw3_command_pipe(false, "ipset", "-exist", "-"))
238 fw3_create_ipsets(state);
242 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
244 if (!family_used(family))
247 if (!print_rules && !restart && family_running(state, family))
249 warn("The %s firewall appears to be started already. "
250 "If it is indeed empty, remove the %s file and retry.",
251 fw3_flag_names[family], FW3_STATEFILE);
256 if (!family_loaded(state, family) || !restore_pipe(family, false))
259 info("Constructing %s rules ...", fw3_flag_names[family]);
261 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
263 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
266 info(" * Populating %s table", fw3_flag_names[table]);
268 fw3_pr("*%s\n", fw3_flag_names[table]);
269 fw3_print_default_chains(table, family, state);
270 fw3_print_zone_chains(table, family, state);
271 fw3_print_default_head_rules(table, family, state);
272 fw3_print_rules(table, family, state);
273 fw3_print_redirects(table, family, state);
274 fw3_print_forwards(table, family, state);
275 fw3_print_zone_rules(table, family, state);
276 fw3_print_default_tail_rules(table, family, state);
281 family_set(state, family, true);
286 if (!rv && !print_rules)
287 fw3_write_statefile(state);
293 lookup_network(struct fw3_state *state, const char *net)
296 struct fw3_device *d;
298 list_for_each_entry(z, &state->zones, list)
300 list_for_each_entry(d, &z->networks, list)
302 if (!strcmp(d->name, net))
304 printf("%s\n", z->name);
314 lookup_device(struct fw3_state *state, const char *dev)
317 struct fw3_device *d;
319 list_for_each_entry(z, &state->zones, list)
321 list_for_each_entry(d, &z->devices, list)
323 if (!strcmp(d->name, dev))
325 printf("%s\n", z->name);
337 fprintf(stderr, "fw3 [-4] [-6] [-q] {start|stop|flush|restart|print}\n");
338 fprintf(stderr, "fw3 [-q] network {net}\n");
339 fprintf(stderr, "fw3 [-q] device {dev}\n");
345 int main(int argc, char **argv)
348 struct fw3_state *state = NULL;
349 struct fw3_defaults *defs = NULL;
351 while ((ch = getopt(argc, argv, "46qh")) != -1)
356 use_family = FW3_FAMILY_V4;
360 use_family = FW3_FAMILY_V6;
364 freopen("/dev/null", "w", stderr);
373 if (!fw3_ubus_connect())
374 error("Failed to connect to ubus");
376 state = build_state();
377 defs = &state->defaults;
388 if (use_family == FW3_FAMILY_V6 && defs->disable_ipv6)
389 warn("IPv6 rules globally disabled in configuration");
391 if (!strcmp(argv[optind], "print"))
393 if (use_family == FW3_FAMILY_ANY)
394 use_family = FW3_FAMILY_V4;
396 freopen("/dev/null", "w", stderr);
398 state->disable_ipsets = true;
401 rv = start(state, false);
403 else if (!strcmp(argv[optind], "start"))
405 rv = start(state, false);
407 else if (!strcmp(argv[optind], "stop"))
409 rv = stop(state, false, false);
411 else if (!strcmp(argv[optind], "flush"))
413 rv = stop(state, true, false);
415 else if (!strcmp(argv[optind], "restart"))
417 rv = stop(state, false, true);
418 rv = start(state, !rv);
420 else if (!strcmp(argv[optind], "network") && (optind + 1) < argc)
422 rv = lookup_network(state, argv[optind + 1]);
424 else if (!strcmp(argv[optind], "device") && (optind + 1) < argc)
426 rv = lookup_device(state, argv[optind + 1]);