79324a4431340ca333b2ba88c38eb51263df8e02
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
)
130 enum fw3_family family
;
131 enum fw3_table table
;
133 const char *tables
[] = {
140 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
142 if (skip_family
[family
] || !restore_pipe(family
, true))
145 info("Removing IPv%d rules ...", family
== FW3_FAMILY_V4
? 4 : 6);
147 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
149 if (!fw3_has_table(family
== FW3_FAMILY_V6
, tables
[table
]))
152 info(" * %sing %s table",
153 complete
? "Flush" : "Clear", tables
[table
]);
155 fw3_pr("*%s\n", tables
[table
]);
156 fw3_print_flush_rules(table
, family
, state
, complete
);
167 destroy_ipsets(struct fw3_state
*state
)
169 if (!fw3_command_pipe(false, "ipset", "-exist", "-"))
172 fw3_destroy_ipsets(state
);
177 start(struct fw3_state
*state
)
179 enum fw3_family family
;
180 enum fw3_table table
;
182 const char *tables
[] = {
189 if (!print_rules
&& fw3_command_pipe(false, "ipset", "-exist", "-"))
191 fw3_create_ipsets(state
);
195 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
197 if (skip_family
[family
] || !restore_pipe(family
, false))
200 info("Constructing IPv%d rules ...", family
== FW3_FAMILY_V4
? 4 : 6);
202 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
204 if (!fw3_has_table(family
== FW3_FAMILY_V6
, tables
[table
]))
207 info(" * Populating %s table", tables
[table
]);
209 fw3_pr("*%s\n", tables
[table
]);
210 fw3_print_default_chains(table
, family
, state
);
211 fw3_print_zone_chains(table
, family
, state
);
212 fw3_print_default_rules(table
, family
, state
);
213 fw3_print_rules(table
, family
, state
);
214 fw3_print_redirects(table
, family
, state
);
215 fw3_print_forwards(table
, family
, state
);
216 fw3_print_zone_rules(table
, family
, state
);
227 lookup_network(struct fw3_state
*state
, const char *net
)
230 struct fw3_device
*d
;
232 list_for_each_entry(z
, &state
->zones
, list
)
234 list_for_each_entry(d
, &z
->networks
, list
)
236 if (!strcmp(d
->name
, net
))
238 printf("%s\n", z
->name
);
248 lookup_device(struct fw3_state
*state
, const char *dev
)
251 struct fw3_device
*d
;
253 list_for_each_entry(z
, &state
->zones
, list
)
255 list_for_each_entry(d
, &z
->devices
, list
)
257 if (!strcmp(d
->name
, dev
))
259 printf("%s\n", z
->name
);
271 fprintf(stderr
, "fw3 [-4] [-6] [-q] {start|stop|flush|restart|print}\n");
272 fprintf(stderr
, "fw3 [-q] network {net}\n");
273 fprintf(stderr
, "fw3 [-q] device {dev}\n");
279 int main(int argc
, char **argv
)
282 struct fw3_state
*state
= NULL
;
284 while ((ch
= getopt(argc
, argv
, "46qh")) != -1)
289 skip_family
[FW3_FAMILY_V4
] = false;
290 skip_family
[FW3_FAMILY_V6
] = true;
294 skip_family
[FW3_FAMILY_V4
] = true;
295 skip_family
[FW3_FAMILY_V6
] = false;
299 freopen("/dev/null", "w", stderr
);
308 if (!fw3_ubus_connect())
309 error("Failed to connect to ubus");
311 state
= build_state();
322 if (!strcmp(argv
[optind
], "print"))
324 freopen("/dev/null", "w", stderr
);
326 state
->disable_ipsets
= true;
329 if (!skip_family
[FW3_FAMILY_V4
] && !skip_family
[FW3_FAMILY_V6
])
330 skip_family
[FW3_FAMILY_V6
] = true;
334 else if (!strcmp(argv
[optind
], "start"))
338 warn("The firewall appears to be started already. "
339 "If it is indeed empty, remove the %s file and retry.",
346 fw3_write_state(state
);
348 else if (!strcmp(argv
[optind
], "stop"))
350 if (!fw3_has_state())
352 warn("The firewall appears to be stopped. "
353 "Use the 'flush' command to forcefully purge all rules.");
358 rv
= stop(state
, false);
360 destroy_ipsets(state
);
364 else if (!strcmp(argv
[optind
], "flush"))
366 rv
= stop(state
, true);
368 destroy_ipsets(state
);
373 else if (!strcmp(argv
[optind
], "restart"))
382 fw3_write_state(state
);
384 else if (!strcmp(argv
[optind
], "network") && (optind
+ 1) < argc
)
386 rv
= lookup_network(state
, argv
[optind
+ 1]);
388 else if (!strcmp(argv
[optind
], "device") && (optind
+ 1) < argc
)
390 rv
= lookup_device(state
, argv
[optind
+ 1]);