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;
35 static struct fw3_state
*run_state
= NULL
;
36 static struct fw3_state
*cfg_state
= NULL
;
40 build_state(bool runtime
)
42 struct fw3_state
*state
= NULL
;
43 struct uci_package
*p
= NULL
;
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");
59 sf
= fopen(FW3_STATEFILE
, "r");
63 uci_import(state
->uci
, sf
, "fw3_state", &p
, true);
69 uci_free_context(state
->uci
);
75 state
->statefile
= true;
81 if (!fw3_ubus_connect())
82 error("Failed to connect to ubus");
84 if (uci_load(state
->uci
, "firewall", &p
))
86 uci_perror(state
->uci
, NULL
);
87 error("Failed to load /etc/config/firewall");
90 if (!fw3_find_command("ipset"))
92 warn("Unable to locate ipset utility, disabling ipset support");
93 state
->disable_ipsets
= true;
99 fw3_load_defaults(state
, p
);
100 fw3_load_ipsets(state
, p
);
101 fw3_load_zones(state
, p
);
102 fw3_load_rules(state
, p
);
103 fw3_load_redirects(state
, p
);
104 fw3_load_forwards(state
, p
);
105 fw3_load_includes(state
, p
);
111 free_state(struct fw3_state
*state
)
113 struct list_head
*cur
, *tmp
;
115 list_for_each_safe(cur
, tmp
, &state
->zones
)
116 fw3_free_zone((struct fw3_zone
*)cur
);
118 list_for_each_safe(cur
, tmp
, &state
->rules
)
119 fw3_free_rule((struct fw3_rule
*)cur
);
121 list_for_each_safe(cur
, tmp
, &state
->redirects
)
122 fw3_free_redirect((struct fw3_redirect
*)cur
);
124 list_for_each_safe(cur
, tmp
, &state
->forwards
)
125 fw3_free_forward((struct fw3_forward
*)cur
);
127 list_for_each_safe(cur
, tmp
, &state
->ipsets
)
128 fw3_free_ipset((struct fw3_ipset
*)cur
);
130 list_for_each_safe(cur
, tmp
, &state
->includes
)
131 fw3_free_include((struct fw3_include
*)cur
);
133 uci_free_context(state
->uci
);
137 fw3_ubus_disconnect();
142 restore_pipe(enum fw3_family family
, bool silent
)
146 cmd
= (family
== FW3_FAMILY_V4
) ? "iptables-restore" : "ip6tables-restore";
149 return fw3_stdout_pipe();
151 if (!fw3_command_pipe(silent
, cmd
, "--lenient", "--noflush"))
153 warn("Unable to execute %s", cmd
);
161 family_running(enum fw3_family family
)
163 return (run_state
&& has(run_state
->defaults
.flags
, family
, family
));
167 family_set(struct fw3_state
*state
, enum fw3_family family
, bool set
)
173 set(state
->defaults
.flags
, family
, family
);
175 del(state
->defaults
.flags
, family
, family
);
179 stop(bool complete
, bool reload
)
184 enum fw3_family family
;
185 enum fw3_table table
;
187 if (!complete
&& !run_state
)
190 warn("The firewall appears to be stopped. "
191 "Use the 'flush' command to forcefully purge all rules.");
196 if (!print_rules
&& run_state
)
197 fw3_hotplug_zones(run_state
, false);
199 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
201 if (!complete
&& !family_running(family
))
204 if (!restore_pipe(family
, true))
207 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
209 if (!fw3_has_table(family
== FW3_FAMILY_V6
, fw3_flag_names
[table
]))
212 info(" * %sing %s %s table", complete
? "Flush" : "Clear",
213 fw3_flag_names
[family
], fw3_flag_names
[table
]);
215 fw3_pr("*%s\n", fw3_flag_names
[table
]);
219 fw3_flush_all(table
);
224 fw3_flush_rules(run_state
, family
, table
, reload
, false);
225 fw3_flush_zones(run_state
, family
, table
, reload
, false);
228 fw3_flush_rules(run_state
, family
, table
, reload
, true);
229 fw3_flush_zones(run_state
, family
, table
, reload
, true);
237 if (!reload
&& run_state
)
239 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
241 fw3_destroy_ipsets(run_state
, family
);
245 family_set(run_state
, family
, false);
246 family_set(cfg_state
, family
, false);
252 if (complete
&& (ct
= fopen("/proc/net/nf_conntrack", "w")) != NULL
)
254 info(" * Flushing conntrack table ...");
256 fwrite("f\n", 2, 1, ct
);
260 if (!rv
&& run_state
)
261 fw3_write_statefile(run_state
);
270 enum fw3_family family
;
271 enum fw3_table table
;
273 if (!print_rules
&& !reload
)
275 if (fw3_command_pipe(false, "ipset", "-exist", "-"))
277 fw3_create_ipsets(cfg_state
);
282 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
284 if (family
== FW3_FAMILY_V6
&& cfg_state
->defaults
.disable_ipv6
)
287 if (!print_rules
&& !reload
&& family_running(family
))
289 warn("The %s firewall appears to be started already. "
290 "If it is indeed empty, remove the %s file and retry.",
291 fw3_flag_names
[family
], FW3_STATEFILE
);
296 if (!restore_pipe(family
, false))
299 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
301 if (!fw3_has_table(family
== FW3_FAMILY_V6
, fw3_flag_names
[table
]))
304 info(" * Populating %s %s table",
305 fw3_flag_names
[family
], fw3_flag_names
[table
]);
307 fw3_pr("*%s\n", fw3_flag_names
[table
]);
308 fw3_print_default_chains(cfg_state
, family
, table
, reload
);
309 fw3_print_zone_chains(cfg_state
, family
, table
, reload
);
310 fw3_print_default_head_rules(cfg_state
, family
, table
, reload
);
311 fw3_print_rules(cfg_state
, family
, table
);
312 fw3_print_redirects(cfg_state
, family
, table
);
313 fw3_print_forwards(cfg_state
, family
, table
);
314 fw3_print_zone_rules(cfg_state
, family
, table
, reload
);
315 fw3_print_default_tail_rules(cfg_state
, family
, table
, reload
);
319 fw3_print_includes(cfg_state
, family
, reload
);
322 family_set(run_state
, family
, true);
323 family_set(cfg_state
, family
, true);
330 fw3_set_defaults(cfg_state
);
334 fw3_run_includes(cfg_state
, reload
);
335 fw3_hotplug_zones(cfg_state
, true);
336 fw3_write_statefile(cfg_state
);
344 lookup_network(const char *net
)
347 struct fw3_device
*d
;
349 list_for_each_entry(z
, &cfg_state
->zones
, list
)
351 list_for_each_entry(d
, &z
->networks
, list
)
353 if (!strcmp(d
->name
, net
))
355 printf("%s\n", z
->name
);
365 lookup_device(const char *dev
)
368 struct fw3_device
*d
;
370 list_for_each_entry(z
, &cfg_state
->zones
, list
)
372 list_for_each_entry(d
, &z
->devices
, list
)
374 if (!strcmp(d
->name
, dev
))
376 printf("%s\n", z
->name
);
388 fprintf(stderr
, "fw3 [-4] [-6] [-q] print\n");
389 fprintf(stderr
, "fw3 [-q] {start|stop|flush|reload|restart}\n");
390 fprintf(stderr
, "fw3 [-q] network {net}\n");
391 fprintf(stderr
, "fw3 [-q] device {dev}\n");
397 int main(int argc
, char **argv
)
400 struct fw3_defaults
*defs
= NULL
;
401 enum fw3_family use_family
= FW3_FAMILY_ANY
;
403 while ((ch
= getopt(argc
, argv
, "46dqh")) != -1)
408 use_family
= FW3_FAMILY_V4
;
412 use_family
= FW3_FAMILY_V6
;
420 freopen("/dev/null", "w", stderr
);
431 defs
= &cfg_state
->defaults
;
439 if (!strcmp(argv
[optind
], "print"))
441 if (use_family
== FW3_FAMILY_ANY
)
442 use_family
= FW3_FAMILY_V4
;
443 else if (use_family
== FW3_FAMILY_V6
&& defs
->disable_ipv6
)
444 warn("IPv6 rules globally disabled in configuration");
446 freopen("/dev/null", "w", stderr
);
448 cfg_state
->disable_ipsets
= true;
453 else if (!strcmp(argv
[optind
], "start"))
461 else if (!strcmp(argv
[optind
], "stop"))
465 rv
= stop(false, false);
469 else if (!strcmp(argv
[optind
], "flush"))
473 rv
= stop(true, false);
477 else if (!strcmp(argv
[optind
], "restart"))
487 else if (!strcmp(argv
[optind
], "reload"))
491 rv
= stop(false, true);
497 else if (!strcmp(argv
[optind
], "network") && (optind
+ 1) < argc
)
499 rv
= lookup_network(argv
[optind
+ 1]);
501 else if (!strcmp(argv
[optind
], "device") && (optind
+ 1) < argc
)
503 rv
= lookup_device(argv
[optind
+ 1]);
512 free_state(cfg_state
);
515 free_state(run_state
);