a58f582332af00683241f2ab2fc9cae3800df766
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
;
35 static const char *families
[] = {
41 static const char *tables
[] = {
49 static struct fw3_state
*
52 struct fw3_state
*state
= NULL
;
53 struct uci_package
*p
= NULL
;
55 state
= malloc(sizeof(*state
));
58 error("Out of memory");
60 memset(state
, 0, sizeof(*state
));
61 state
->uci
= uci_alloc_context();
64 error("Out of memory");
66 if (uci_load(state
->uci
, "firewall", &p
))
68 uci_perror(state
->uci
, NULL
);
69 error("Failed to load /etc/config/firewall");
72 if (!fw3_find_command("ipset"))
74 warn("Unable to locate ipset utility, disabling ipset support");
75 state
->disable_ipsets
= true;
78 fw3_load_defaults(state
, p
);
79 fw3_load_ipsets(state
, p
);
80 fw3_load_zones(state
, p
);
81 fw3_load_rules(state
, p
);
82 fw3_load_redirects(state
, p
);
83 fw3_load_forwards(state
, p
);
89 free_state(struct fw3_state
*state
)
91 struct list_head
*cur
, *tmp
;
93 list_for_each_safe(cur
, tmp
, &state
->zones
)
94 fw3_free_zone((struct fw3_zone
*)cur
);
96 list_for_each_safe(cur
, tmp
, &state
->rules
)
97 fw3_free_rule((struct fw3_rule
*)cur
);
99 list_for_each_safe(cur
, tmp
, &state
->redirects
)
100 fw3_free_redirect((struct fw3_redirect
*)cur
);
102 list_for_each_safe(cur
, tmp
, &state
->forwards
)
103 fw3_free_forward((struct fw3_forward
*)cur
);
105 uci_free_context(state
->uci
);
109 fw3_ubus_disconnect();
114 restore_pipe(enum fw3_family family
, bool silent
)
116 const char *cmd
[] = {
123 return fw3_stdout_pipe();
125 if (!fw3_command_pipe(silent
, cmd
[family
], "--lenient", "--noflush"))
127 warn("Unable to execute %s", cmd
[family
]);
134 #define family_flag(f) \
135 (f == FW3_FAMILY_V4 ? FW3_DEFAULT_IPV4_LOADED : FW3_DEFAULT_IPV6_LOADED)
138 family_running(struct list_head
*statefile
, enum fw3_family family
)
140 struct fw3_statefile_entry
*e
;
144 list_for_each_entry(e
, statefile
, list
)
146 if (e
->type
!= FW3_TYPE_DEFAULTS
)
149 return hasbit(e
->flags
[0], family_flag(family
));
157 family_used(enum fw3_family family
)
159 return (use_family
== FW3_FAMILY_ANY
) || (use_family
== family
);
163 family_loaded(struct fw3_state
*state
, enum fw3_family family
)
165 return hasbit(state
->defaults
.has_flag
, family_flag(family
));
169 family_set(struct fw3_state
*state
, enum fw3_family family
, bool set
)
172 setbit(state
->defaults
.has_flag
, family_flag(family
));
174 delbit(state
->defaults
.has_flag
, family_flag(family
));
178 stop(struct fw3_state
*state
, bool complete
, bool restart
)
181 enum fw3_family family
;
182 enum fw3_table table
;
184 struct list_head
*statefile
= fw3_read_statefile();
186 if (!complete
&& !statefile
)
189 warn("The firewall appears to be stopped. "
190 "Use the 'flush' command to forcefully purge all rules.");
195 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
197 if (!complete
&& !family_running(statefile
, family
))
200 if (!family_used(family
) || !restore_pipe(family
, true))
203 info("Removing %s rules ...", families
[family
]);
205 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
207 if (!fw3_has_table(family
== FW3_FAMILY_V6
, tables
[table
]))
210 info(" * %sing %s table",
211 complete
? "Flush" : "Clear", tables
[table
]);
213 fw3_pr("*%s\n", tables
[table
]);
217 fw3_flush_all(table
);
222 fw3_flush_rules(table
, family
, false, statefile
);
223 fw3_flush_zones(table
, family
, false, statefile
);
226 fw3_flush_rules(table
, family
, true, statefile
);
227 fw3_flush_zones(table
, family
, true, statefile
);
236 family_set(state
, family
, false);
242 !family_loaded(state
, FW3_FAMILY_V4
) &&
243 !family_loaded(state
, FW3_FAMILY_V6
) &&
244 fw3_command_pipe(false, "ipset", "-exist", "-"))
246 fw3_destroy_ipsets(statefile
);
250 fw3_free_statefile(statefile
);
253 fw3_write_statefile(state
);
259 start(struct fw3_state
*state
, bool restart
)
262 enum fw3_family family
;
263 enum fw3_table table
;
265 struct list_head
*statefile
= fw3_read_statefile();
267 if (!print_rules
&& !restart
&&
268 fw3_command_pipe(false, "ipset", "-exist", "-"))
270 fw3_create_ipsets(state
);
274 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
276 if (!family_used(family
))
279 if (!family_loaded(state
, family
) || !restore_pipe(family
, false))
282 if (!restart
&& family_running(statefile
, family
))
284 warn("The %s firewall appears to be started already. "
285 "If it is indeed empty, remove the %s file and retry.",
286 families
[family
], FW3_STATEFILE
);
291 info("Constructing %s rules ...", families
[family
]);
293 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
295 if (!fw3_has_table(family
== FW3_FAMILY_V6
, tables
[table
]))
298 info(" * Populating %s table", tables
[table
]);
300 fw3_pr("*%s\n", tables
[table
]);
301 fw3_print_default_chains(table
, family
, state
);
302 fw3_print_zone_chains(table
, family
, state
);
303 fw3_print_default_head_rules(table
, family
, state
);
304 fw3_print_rules(table
, family
, state
);
305 fw3_print_redirects(table
, family
, state
);
306 fw3_print_forwards(table
, family
, state
);
307 fw3_print_zone_rules(table
, family
, state
);
308 fw3_print_default_tail_rules(table
, family
, state
);
313 family_set(state
, family
, true);
318 fw3_free_statefile(statefile
);
321 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|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
, "46qh")) != -1)
390 use_family
= FW3_FAMILY_V4
;
394 use_family
= FW3_FAMILY_V6
;
398 freopen("/dev/null", "w", stderr
);
407 if (!fw3_ubus_connect())
408 error("Failed to connect to ubus");
410 state
= build_state();
411 defs
= &state
->defaults
;
422 if (use_family
== FW3_FAMILY_V6
&& defs
->disable_ipv6
)
423 warn("IPv6 rules globally disabled in configuration");
425 if (!strcmp(argv
[optind
], "print"))
427 freopen("/dev/null", "w", stderr
);
429 state
->disable_ipsets
= true;
432 rv
= start(state
, false);
434 else if (!strcmp(argv
[optind
], "start"))
436 rv
= start(state
, false);
438 else if (!strcmp(argv
[optind
], "stop"))
440 rv
= stop(state
, false, false);
442 else if (!strcmp(argv
[optind
], "flush"))
444 rv
= stop(state
, true, false);
446 else if (!strcmp(argv
[optind
], "restart"))
448 rv
= stop(state
, false, true);
449 rv
= start(state
, !rv
);
451 else if (!strcmp(argv
[optind
], "network") && (optind
+ 1) < argc
)
453 rv
= lookup_network(state
, argv
[optind
+ 1]);
455 else if (!strcmp(argv
[optind
], "device") && (optind
+ 1) < argc
)
457 rv
= lookup_device(state
, argv
[optind
+ 1]);