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"
33 static bool print_rules
= false;
34 static enum fw3_family use_family
= FW3_FAMILY_ANY
;
37 static struct fw3_state
*
40 struct fw3_state
*state
= NULL
;
41 struct uci_package
*p
= NULL
;
43 if (!fw3_ubus_connect())
44 error("Failed to connect to ubus");
46 state
= malloc(sizeof(*state
));
49 error("Out of memory");
51 memset(state
, 0, sizeof(*state
));
52 state
->uci
= uci_alloc_context();
55 error("Out of memory");
57 if (uci_load(state
->uci
, "firewall", &p
))
59 uci_perror(state
->uci
, NULL
);
60 error("Failed to load /etc/config/firewall");
63 if (!fw3_find_command("ipset"))
65 warn("Unable to locate ipset utility, disabling ipset support");
66 state
->disable_ipsets
= true;
69 INIT_LIST_HEAD(&state
->running_zones
);
70 INIT_LIST_HEAD(&state
->running_ipsets
);
72 fw3_load_defaults(state
, p
);
73 fw3_load_ipsets(state
, p
);
74 fw3_load_zones(state
, p
);
75 fw3_load_rules(state
, p
);
76 fw3_load_redirects(state
, p
);
77 fw3_load_forwards(state
, p
);
78 fw3_load_includes(state
, p
);
80 state
->statefile
= fw3_read_statefile(state
);
86 free_state(struct fw3_state
*state
)
88 struct list_head
*cur
, *tmp
;
90 list_for_each_safe(cur
, tmp
, &state
->zones
)
91 fw3_free_zone((struct fw3_zone
*)cur
);
93 list_for_each_safe(cur
, tmp
, &state
->rules
)
94 fw3_free_rule((struct fw3_rule
*)cur
);
96 list_for_each_safe(cur
, tmp
, &state
->redirects
)
97 fw3_free_redirect((struct fw3_redirect
*)cur
);
99 list_for_each_safe(cur
, tmp
, &state
->forwards
)
100 fw3_free_forward((struct fw3_forward
*)cur
);
102 list_for_each_safe(cur
, tmp
, &state
->ipsets
)
103 fw3_free_ipset((struct fw3_ipset
*)cur
);
105 list_for_each_safe(cur
, tmp
, &state
->includes
)
106 fw3_free_include((struct fw3_include
*)cur
);
108 uci_free_context(state
->uci
);
112 fw3_ubus_disconnect();
117 restore_pipe(enum fw3_family family
, bool silent
)
121 cmd
= (family
== FW3_FAMILY_V4
) ? "iptables-restore" : "ip6tables-restore";
124 return fw3_stdout_pipe();
126 if (!fw3_command_pipe(silent
, cmd
, "--lenient", "--noflush"))
128 warn("Unable to execute %s", cmd
);
136 family_running(struct fw3_state
*state
, enum fw3_family family
)
138 return hasbit(state
->defaults
.running_flags
, family
);
142 family_used(enum fw3_family family
)
144 return (use_family
== FW3_FAMILY_ANY
) || (use_family
== family
);
148 family_loaded(struct fw3_state
*state
, enum fw3_family family
)
150 return hasbit(state
->defaults
.flags
, family
);
154 family_set(struct fw3_state
*state
, enum fw3_family family
, bool set
)
157 setbit(state
->defaults
.flags
, family
);
159 delbit(state
->defaults
.flags
, family
);
163 stop(struct fw3_state
*state
, bool complete
, bool reload
)
168 enum fw3_family family
;
169 enum fw3_table table
;
170 enum fw3_target policy
= reload
? FW3_TARGET_DROP
: FW3_TARGET_ACCEPT
;
172 if (!complete
&& !state
->statefile
)
175 warn("The firewall appears to be stopped. "
176 "Use the 'flush' command to forcefully purge all rules.");
181 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
183 if (!complete
&& !family_running(state
, family
))
186 if (!family_used(family
) || !restore_pipe(family
, true))
189 info("Removing %s rules ...", fw3_flag_names
[family
]);
191 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
193 if (!fw3_has_table(family
== FW3_FAMILY_V6
, fw3_flag_names
[table
]))
196 info(" * %sing %s table",
197 complete
? "Flush" : "Clear", fw3_flag_names
[table
]);
199 fw3_pr("*%s\n", fw3_flag_names
[table
]);
203 fw3_flush_all(table
);
208 fw3_flush_rules(table
, family
, false, state
, policy
);
209 fw3_flush_zones(table
, family
, false, reload
, state
);
212 fw3_flush_rules(table
, family
, true, state
, policy
);
213 fw3_flush_zones(table
, family
, true, reload
, state
);
223 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
225 fw3_destroy_ipsets(state
, family
);
229 family_set(state
, family
, false);
235 if (complete
&& (ct
= fopen("/proc/net/nf_conntrack", "w")) != NULL
)
237 info("Flushing conntrack table ...");
239 fwrite("f\n", 2, 1, ct
);
244 fw3_write_statefile(state
);
250 start(struct fw3_state
*state
, bool reload
)
253 enum fw3_family family
;
254 enum fw3_table table
;
256 if (!print_rules
&& !reload
)
258 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
260 fw3_create_ipsets(state
);
265 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
267 if (!family_used(family
))
270 if (!print_rules
&& !reload
&& family_running(state
, family
))
272 warn("The %s firewall appears to be started already. "
273 "If it is indeed empty, remove the %s file and retry.",
274 fw3_flag_names
[family
], FW3_STATEFILE
);
279 if (!family_loaded(state
, family
) || !restore_pipe(family
, false))
282 info("Constructing %s rules ...", fw3_flag_names
[family
]);
284 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
286 if (!fw3_has_table(family
== FW3_FAMILY_V6
, fw3_flag_names
[table
]))
289 info(" * Populating %s table", fw3_flag_names
[table
]);
291 fw3_pr("*%s\n", fw3_flag_names
[table
]);
292 fw3_print_default_chains(table
, family
, state
);
293 fw3_print_zone_chains(table
, family
, state
);
294 fw3_print_default_head_rules(table
, family
, state
);
295 fw3_print_rules(table
, family
, state
);
296 fw3_print_redirects(table
, family
, state
);
297 fw3_print_forwards(table
, family
, state
);
298 fw3_print_zone_rules(table
, family
, state
);
299 fw3_print_default_tail_rules(table
, family
, state
);
304 fw3_print_includes(family
, state
);
307 family_set(state
, family
, true);
314 fw3_set_defaults(state
);
316 if (!reload
&& !print_rules
)
317 fw3_run_includes(state
);
320 fw3_write_statefile(state
);
327 lookup_network(struct fw3_state
*state
, const char *net
)
330 struct fw3_device
*d
;
332 list_for_each_entry(z
, &state
->zones
, list
)
334 list_for_each_entry(d
, &z
->networks
, list
)
336 if (!strcmp(d
->name
, net
))
338 printf("%s\n", z
->name
);
348 lookup_device(struct fw3_state
*state
, const char *dev
)
351 struct fw3_device
*d
;
353 list_for_each_entry(z
, &state
->zones
, list
)
355 list_for_each_entry(d
, &z
->devices
, list
)
357 if (!strcmp(d
->name
, dev
))
359 printf("%s\n", z
->name
);
371 fprintf(stderr
, "fw3 [-4] [-6] [-q] {start|stop|flush|reload|restart|print}\n");
372 fprintf(stderr
, "fw3 [-q] network {net}\n");
373 fprintf(stderr
, "fw3 [-q] device {dev}\n");
379 int main(int argc
, char **argv
)
382 struct fw3_state
*state
= NULL
;
383 struct fw3_defaults
*defs
= NULL
;
385 while ((ch
= getopt(argc
, argv
, "46dqh")) != -1)
390 use_family
= FW3_FAMILY_V4
;
394 use_family
= FW3_FAMILY_V6
;
402 freopen("/dev/null", "w", stderr
);
411 state
= build_state();
412 defs
= &state
->defaults
;
423 if (use_family
== FW3_FAMILY_V6
&& defs
->disable_ipv6
)
424 warn("IPv6 rules globally disabled in configuration");
426 if (!strcmp(argv
[optind
], "print"))
428 if (use_family
== FW3_FAMILY_ANY
)
429 use_family
= FW3_FAMILY_V4
;
431 freopen("/dev/null", "w", stderr
);
433 state
->disable_ipsets
= true;
436 rv
= start(state
, false);
438 else if (!strcmp(argv
[optind
], "start"))
440 rv
= start(state
, false);
442 else if (!strcmp(argv
[optind
], "stop"))
444 rv
= stop(state
, false, false);
446 else if (!strcmp(argv
[optind
], "flush"))
448 rv
= stop(state
, true, false);
450 else if (!strcmp(argv
[optind
], "restart"))
452 stop(state
, true, false);
455 state
= build_state();
456 rv
= start(state
, false);
458 else if (!strcmp(argv
[optind
], "reload"))
460 rv
= stop(state
, false, true);
461 rv
= start(state
, !rv
);
463 else if (!strcmp(argv
[optind
], "network") && (optind
+ 1) < argc
)
465 rv
= lookup_network(state
, argv
[optind
+ 1]);
467 else if (!strcmp(argv
[optind
], "device") && (optind
+ 1) < argc
)
469 rv
= lookup_device(state
, argv
[optind
+ 1]);