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 bool skip_family
[FW3_FAMILY_V6
+ 1] = { false };
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
);
72 if (state
->defaults
.disable_ipv6
&& !skip_family
[FW3_FAMILY_V6
])
74 warn("IPv6 rules globally disabled in configuration");
75 skip_family
[FW3_FAMILY_V6
] = true;
82 free_state(struct fw3_state
*state
)
84 struct list_head
*cur
, *tmp
;
86 list_for_each_safe(cur
, tmp
, &state
->zones
)
87 fw3_free_zone((struct fw3_zone
*)cur
);
89 list_for_each_safe(cur
, tmp
, &state
->rules
)
90 fw3_free_rule((struct fw3_rule
*)cur
);
92 list_for_each_safe(cur
, tmp
, &state
->redirects
)
93 fw3_free_redirect((struct fw3_redirect
*)cur
);
95 list_for_each_safe(cur
, tmp
, &state
->forwards
)
96 fw3_free_forward((struct fw3_forward
*)cur
);
98 uci_free_context(state
->uci
);
102 fw3_ubus_disconnect();
107 restore_pipe(enum fw3_family family
, bool silent
)
109 const char *cmd
[] = {
116 return fw3_stdout_pipe();
118 if (!fw3_command_pipe(silent
, cmd
[family
], "--lenient", "--noflush"))
120 warn("Unable to execute %s", cmd
[family
]);
128 stop(struct fw3_state
*state
, bool complete
, bool ipsets
)
130 enum fw3_family family
;
131 enum fw3_table table
;
133 struct list_head
*statefile
= fw3_read_state();
135 const char *tables
[] = {
142 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
144 if (skip_family
[family
] || !restore_pipe(family
, true))
147 info("Removing IPv%d rules ...", family
== FW3_FAMILY_V4
? 4 : 6);
149 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
151 if (!fw3_has_table(family
== FW3_FAMILY_V6
, tables
[table
]))
154 info(" * %sing %s table",
155 complete
? "Flush" : "Clear", tables
[table
]);
157 fw3_pr("*%s\n", tables
[table
]);
161 fw3_flush_all(table
);
166 fw3_flush_rules(table
, family
, false, statefile
);
167 fw3_flush_zones(table
, family
, false, statefile
);
170 fw3_flush_rules(table
, family
, true, statefile
);
171 fw3_flush_zones(table
, family
, true, statefile
);
180 if (ipsets
&& fw3_command_pipe(false, "ipset", "-exist", "-"))
182 fw3_destroy_ipsets(statefile
);
186 fw3_free_state(statefile
);
192 start(struct fw3_state
*state
)
194 enum fw3_family family
;
195 enum fw3_table table
;
197 const char *tables
[] = {
204 if (!print_rules
&& fw3_command_pipe(false, "ipset", "-exist", "-"))
206 fw3_create_ipsets(state
);
210 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
212 if (skip_family
[family
] || !restore_pipe(family
, false))
215 info("Constructing IPv%d rules ...", family
== FW3_FAMILY_V4
? 4 : 6);
217 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
219 if (!fw3_has_table(family
== FW3_FAMILY_V6
, tables
[table
]))
222 info(" * Populating %s table", tables
[table
]);
224 fw3_pr("*%s\n", tables
[table
]);
225 fw3_print_default_chains(table
, family
, state
);
226 fw3_print_zone_chains(table
, family
, state
);
227 fw3_print_default_head_rules(table
, family
, state
);
228 fw3_print_rules(table
, family
, state
);
229 fw3_print_redirects(table
, family
, state
);
230 fw3_print_forwards(table
, family
, state
);
231 fw3_print_zone_rules(table
, family
, state
);
232 fw3_print_default_tail_rules(table
, family
, state
);
243 lookup_network(struct fw3_state
*state
, const char *net
)
246 struct fw3_device
*d
;
248 list_for_each_entry(z
, &state
->zones
, list
)
250 list_for_each_entry(d
, &z
->networks
, list
)
252 if (!strcmp(d
->name
, net
))
254 printf("%s\n", z
->name
);
264 lookup_device(struct fw3_state
*state
, const char *dev
)
267 struct fw3_device
*d
;
269 list_for_each_entry(z
, &state
->zones
, list
)
271 list_for_each_entry(d
, &z
->devices
, list
)
273 if (!strcmp(d
->name
, dev
))
275 printf("%s\n", z
->name
);
287 fprintf(stderr
, "fw3 [-4] [-6] [-q] {start|stop|flush|restart|print}\n");
288 fprintf(stderr
, "fw3 [-q] network {net}\n");
289 fprintf(stderr
, "fw3 [-q] device {dev}\n");
295 int main(int argc
, char **argv
)
298 struct fw3_state
*state
= NULL
;
300 while ((ch
= getopt(argc
, argv
, "46qh")) != -1)
305 skip_family
[FW3_FAMILY_V4
] = false;
306 skip_family
[FW3_FAMILY_V6
] = true;
310 skip_family
[FW3_FAMILY_V4
] = true;
311 skip_family
[FW3_FAMILY_V6
] = false;
315 freopen("/dev/null", "w", stderr
);
324 if (!fw3_ubus_connect())
325 error("Failed to connect to ubus");
327 state
= build_state();
338 if (!strcmp(argv
[optind
], "print"))
340 freopen("/dev/null", "w", stderr
);
342 state
->disable_ipsets
= true;
345 if (!skip_family
[FW3_FAMILY_V4
] && !skip_family
[FW3_FAMILY_V6
])
346 skip_family
[FW3_FAMILY_V6
] = true;
350 else if (!strcmp(argv
[optind
], "start"))
354 warn("The firewall appears to be started already. "
355 "If it is indeed empty, remove the %s file and retry.",
362 fw3_write_state(state
);
364 else if (!strcmp(argv
[optind
], "stop"))
366 if (!fw3_has_state())
368 warn("The firewall appears to be stopped. "
369 "Use the 'flush' command to forcefully purge all rules.");
374 rv
= stop(state
, false, true);
378 else if (!strcmp(argv
[optind
], "flush"))
380 rv
= stop(state
, true, true);
385 else if (!strcmp(argv
[optind
], "restart"))
389 stop(state
, false, false);
394 fw3_write_state(state
);
396 else if (!strcmp(argv
[optind
], "network") && (optind
+ 1) < argc
)
398 rv
= lookup_network(state
, argv
[optind
+ 1]);
400 else if (!strcmp(argv
[optind
], "device") && (optind
+ 1) < argc
)
402 rv
= lookup_device(state
, argv
[optind
+ 1]);