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);
224 !family_loaded(state, FW3_FAMILY_V4) &&
225 !family_loaded(state, FW3_FAMILY_V6) &&
226 fw3_command_pipe(false, "ipset", "-exist", "-"))
228 fw3_destroy_ipsets(statefile);
232 fw3_free_statefile(statefile);
235 fw3_write_statefile(state);
241 start(struct fw3_state *state, bool restart)
244 enum fw3_family family;
245 enum fw3_table table;
247 struct list_head *statefile = fw3_read_statefile();
249 if (!print_rules && !restart &&
250 fw3_command_pipe(false, "ipset", "-exist", "-"))
252 fw3_create_ipsets(state);
256 for (family = FW3_FAMILY_V4; family <= FW3_FAMILY_V6; family++)
258 if (!family_used(family))
261 if (!family_loaded(state, family) || !restore_pipe(family, false))
264 if (!print_rules && !restart && family_running(statefile, family))
266 warn("The %s firewall appears to be started already. "
267 "If it is indeed empty, remove the %s file and retry.",
268 fw3_flag_names[family], FW3_STATEFILE);
273 info("Constructing %s rules ...", fw3_flag_names[family]);
275 for (table = FW3_TABLE_FILTER; table <= FW3_TABLE_RAW; table++)
277 if (!fw3_has_table(family == FW3_FAMILY_V6, fw3_flag_names[table]))
280 info(" * Populating %s table", fw3_flag_names[table]);
282 fw3_pr("*%s\n", fw3_flag_names[table]);
283 fw3_print_default_chains(table, family, state);
284 fw3_print_zone_chains(table, family, state);
285 fw3_print_default_head_rules(table, family, state);
286 fw3_print_rules(table, family, state);
287 fw3_print_redirects(table, family, state);
288 fw3_print_forwards(table, family, state);
289 fw3_print_zone_rules(table, family, state);
290 fw3_print_default_tail_rules(table, family, state);
295 family_set(state, family, true);
300 fw3_free_statefile(statefile);
302 if (!rv && !print_rules)
303 fw3_write_statefile(state);
309 lookup_network(struct fw3_state *state, const char *net)
312 struct fw3_device *d;
314 list_for_each_entry(z, &state->zones, list)
316 list_for_each_entry(d, &z->networks, list)
318 if (!strcmp(d->name, net))
320 printf("%s\n", z->name);
330 lookup_device(struct fw3_state *state, const char *dev)
333 struct fw3_device *d;
335 list_for_each_entry(z, &state->zones, list)
337 list_for_each_entry(d, &z->devices, list)
339 if (!strcmp(d->name, dev))
341 printf("%s\n", z->name);
353 fprintf(stderr, "fw3 [-4] [-6] [-q] {start|stop|flush|restart|print}\n");
354 fprintf(stderr, "fw3 [-q] network {net}\n");
355 fprintf(stderr, "fw3 [-q] device {dev}\n");
361 int main(int argc, char **argv)
364 struct fw3_state *state = NULL;
365 struct fw3_defaults *defs = NULL;
367 while ((ch = getopt(argc, argv, "46qh")) != -1)
372 use_family = FW3_FAMILY_V4;
376 use_family = FW3_FAMILY_V6;
380 freopen("/dev/null", "w", stderr);
389 if (!fw3_ubus_connect())
390 error("Failed to connect to ubus");
392 state = build_state();
393 defs = &state->defaults;
404 if (use_family == FW3_FAMILY_V6 && defs->disable_ipv6)
405 warn("IPv6 rules globally disabled in configuration");
407 if (!strcmp(argv[optind], "print"))
409 if (use_family == FW3_FAMILY_ANY)
410 use_family = FW3_FAMILY_V4;
412 freopen("/dev/null", "w", stderr);
414 state->disable_ipsets = true;
417 rv = start(state, false);
419 else if (!strcmp(argv[optind], "start"))
421 rv = start(state, false);
423 else if (!strcmp(argv[optind], "stop"))
425 rv = stop(state, false, false);
427 else if (!strcmp(argv[optind], "flush"))
429 rv = stop(state, true, false);
431 else if (!strcmp(argv[optind], "restart"))
433 rv = stop(state, false, true);
434 rv = start(state, !rv);
436 else if (!strcmp(argv[optind], "network") && (optind + 1) < argc)
438 rv = lookup_network(state, argv[optind + 1]);
440 else if (!strcmp(argv[optind], "device") && (optind + 1) < argc)
442 rv = lookup_device(state, argv[optind + 1]);