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 fw3_load_defaults(state, p);
66 fw3_load_ipsets(state, p);
67 fw3_load_zones(state, p);
68 fw3_load_rules(state, p);
69 fw3_load_redirects(state, p);
70 fw3_load_forwards(state, p);
76 free_state(struct fw3_state *state)
78 struct list_head *cur, *tmp;
80 list_for_each_safe(cur, tmp, &state->zones)
81 fw3_free_zone((struct fw3_zone *)cur);
83 list_for_each_safe(cur, tmp, &state->rules)
84 fw3_free_rule((struct fw3_rule *)cur);
86 list_for_each_safe(cur, tmp, &state->redirects)
87 fw3_free_redirect((struct fw3_redirect *)cur);
89 list_for_each_safe(cur, tmp, &state->forwards)
90 fw3_free_forward((struct fw3_forward *)cur);
92 uci_free_context(state->uci);
96 fw3_ubus_disconnect();
101 restore_pipe(enum fw3_family family, bool silent)
105 cmd = (family == FW3_FAMILY_V4) ? "iptables-restore" : "ip6tables-restore";
108 return fw3_stdout_pipe();
110 if (!fw3_command_pipe(silent, cmd, "--lenient", "--noflush"))
112 warn("Unable to execute %s", cmd);
120 family_running(struct list_head *statefile, enum fw3_family family)
122 struct fw3_statefile_entry *e;
126 list_for_each_entry(e, statefile, list)
128 if (e->type != FW3_TYPE_DEFAULTS)
131 return hasbit(e->flags[0], family);
139 family_used(enum fw3_family family)
141 return (use_family == FW3_FAMILY_ANY) || (use_family == family);
145 family_loaded(struct fw3_state *state, enum fw3_family family)
147 return hasbit(state->defaults.flags, family);
151 family_set(struct fw3_state *state, enum fw3_family family, bool set)
154 setbit(state->defaults.flags, family);
156 delbit(state->defaults.flags, family);
160 stop(struct fw3_state *state, bool complete, bool restart)
163 enum fw3_family family;
164 enum fw3_table table;
166 struct list_head *statefile = fw3_read_statefile();
168 if (!complete && !statefile)
171 warn("The firewall appears to be stopped. "
172 "Use the 'flush' command to forcefully purge all rules.");
177 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
179 if (!complete && !family_running(statefile, family))
182 if (!family_used(family) || !restore_pipe(family, true))
185 info("Removing %s rules ...", fw3_flag_names[family]);
187 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
189 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
192 info(" * %sing %s table",
193 complete ? "Flush" : "Clear", fw3_flag_names[table]);
195 fw3_pr("*%s\n", fw3_flag_names[table]);
199 fw3_flush_all(table);
204 fw3_flush_rules(table, family, false, statefile);
205 fw3_flush_zones(table, family, false, statefile);
208 fw3_flush_rules(table, family, true, statefile);
209 fw3_flush_zones(table, family, true, statefile);
218 family_set(state, family, false);
223 if (!restart && fw3_command_pipe(false, "ipset", "-exist", "-"))
225 fw3_destroy_ipsets(state, statefile);
229 fw3_free_statefile(statefile);
232 fw3_write_statefile(state);
238 start(struct fw3_state *state, bool restart)
241 enum fw3_family family;
242 enum fw3_table table;
244 struct list_head *statefile = fw3_read_statefile();
246 if (!print_rules && !restart &&
247 fw3_command_pipe(false, "ipset", "-exist", "-"))
249 fw3_create_ipsets(state, statefile);
253 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
255 if (!family_used(family))
258 if (!family_loaded(state, family) || !restore_pipe(family, false))
261 if (!print_rules && !restart && family_running(statefile, 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 info("Constructing %s rules ...", fw3_flag_names[family]);
272 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
274 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
277 info(" * Populating %s table", fw3_flag_names[table]);
279 fw3_pr("*%s\n", fw3_flag_names[table]);
280 fw3_print_default_chains(table, family, state);
281 fw3_print_zone_chains(table, family, state);
282 fw3_print_default_head_rules(table, family, state);
283 fw3_print_rules(table, family, state);
284 fw3_print_redirects(table, family, state);
285 fw3_print_forwards(table, family, state);
286 fw3_print_zone_rules(table, family, state);
287 fw3_print_default_tail_rules(table, family, state);
292 family_set(state, family, true);
297 fw3_free_statefile(statefile);
299 if (!rv && !print_rules)
300 fw3_write_statefile(state);
306 lookup_network(struct fw3_state *state, const char *net)
309 struct fw3_device *d;
311 list_for_each_entry(z, &state->zones, list)
313 list_for_each_entry(d, &z->networks, list)
315 if (!strcmp(d->name, net))
317 printf("%s\n", z->name);
327 lookup_device(struct fw3_state *state, const char *dev)
330 struct fw3_device *d;
332 list_for_each_entry(z, &state->zones, list)
334 list_for_each_entry(d, &z->devices, list)
336 if (!strcmp(d->name, dev))
338 printf("%s\n", z->name);
350 fprintf(stderr, "fw3 [-4] [-6] [-q] {start|stop|flush|restart|print}\n");
351 fprintf(stderr, "fw3 [-q] network {net}\n");
352 fprintf(stderr, "fw3 [-q] device {dev}\n");
358 int main(int argc, char **argv)
361 struct fw3_state *state = NULL;
362 struct fw3_defaults *defs = NULL;
364 while ((ch = getopt(argc, argv, "46qh")) != -1)
369 use_family = FW3_FAMILY_V4;
373 use_family = FW3_FAMILY_V6;
377 freopen("/dev/null", "w", stderr);
386 if (!fw3_ubus_connect())
387 error("Failed to connect to ubus");
389 state = build_state();
390 defs = &state->defaults;
401 if (use_family == FW3_FAMILY_V6 && defs->disable_ipv6)
402 warn("IPv6 rules globally disabled in configuration");
404 if (!strcmp(argv[optind], "print"))
406 if (use_family == FW3_FAMILY_ANY)
407 use_family = FW3_FAMILY_V4;
409 freopen("/dev/null", "w", stderr);
411 state->disable_ipsets = true;
414 rv = start(state, false);
416 else if (!strcmp(argv[optind], "start"))
418 rv = start(state, false);
420 else if (!strcmp(argv[optind], "stop"))
422 rv = stop(state, false, false);
424 else if (!strcmp(argv[optind], "flush"))
426 rv = stop(state, true, false);
428 else if (!strcmp(argv[optind], "restart"))
430 rv = stop(state, false, true);
431 rv = start(state, !rv);
433 else if (!strcmp(argv[optind], "network") && (optind + 1) < argc)
435 rv = lookup_network(state, argv[optind + 1]);
437 else if (!strcmp(argv[optind], "device") && (optind + 1) < argc)
439 rv = lookup_device(state, argv[optind + 1]);