2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013-2014 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"
35 static enum fw3_family print_family
= FW3_FAMILY_ANY
;
37 static struct fw3_state
*run_state
= NULL
;
38 static struct fw3_state
*cfg_state
= NULL
;
42 build_state(bool runtime
)
44 struct fw3_state
*state
= NULL
;
45 struct uci_package
*p
= NULL
;
48 state
= calloc(1, sizeof(*state
));
50 error("Out of memory");
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;
100 struct blob_buf b
= {NULL
, NULL
, 0, NULL
};
103 fw3_load_defaults(state
, p
);
104 fw3_load_ipsets(state
, p
);
105 fw3_load_zones(state
, p
);
106 fw3_load_rules(state
, p
, b
.head
);
107 fw3_load_redirects(state
, p
);
108 fw3_load_snats(state
, p
, b
.head
);
109 fw3_load_forwards(state
, p
);
110 fw3_load_includes(state
, p
);
116 free_state(struct fw3_state
*state
)
118 struct list_head
*cur
, *tmp
;
120 list_for_each_safe(cur
, tmp
, &state
->zones
)
121 fw3_free_zone((struct fw3_zone
*)cur
);
123 list_for_each_safe(cur
, tmp
, &state
->rules
)
124 fw3_free_rule((struct fw3_rule
*)cur
);
126 list_for_each_safe(cur
, tmp
, &state
->redirects
)
127 fw3_free_redirect((struct fw3_redirect
*)cur
);
129 list_for_each_safe(cur
, tmp
, &state
->snats
)
130 fw3_free_snat((struct fw3_snat
*)cur
);
132 list_for_each_safe(cur
, tmp
, &state
->forwards
)
133 fw3_free_forward((struct fw3_forward
*)cur
);
135 list_for_each_safe(cur
, tmp
, &state
->ipsets
)
136 fw3_free_ipset((struct fw3_ipset
*)cur
);
138 list_for_each_safe(cur
, tmp
, &state
->includes
)
139 fw3_free_include((struct fw3_include
*)cur
);
141 uci_free_context(state
->uci
);
145 fw3_ubus_disconnect();
150 family_running(enum fw3_family family
)
152 return (run_state
&& has(run_state
->defaults
.flags
, family
, family
));
156 family_set(struct fw3_state
*state
, enum fw3_family family
, bool set
)
162 set(state
->defaults
.flags
, family
, family
);
164 del(state
->defaults
.flags
, family
, family
);
173 enum fw3_family family
;
174 enum fw3_table table
;
175 struct fw3_ipt_handle
*handle
;
177 if (!complete
&& !run_state
)
179 warn("The firewall appears to be stopped. "
180 "Use the 'flush' command to forcefully purge all rules.");
185 if (!print_family
&& run_state
)
186 fw3_hotplug_zones(run_state
, false);
188 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
190 if (!complete
&& !family_running(family
))
193 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
195 if (!fw3_has_table(family
== FW3_FAMILY_V6
, fw3_flag_names
[table
]))
198 if (!(handle
= fw3_ipt_open(family
, table
)))
201 info(" * %sing %s %s table", complete
? "Flush" : "Clear",
202 fw3_flag_names
[family
], fw3_flag_names
[table
]);
206 fw3_flush_all(handle
);
210 fw3_flush_rules(handle
, run_state
, false);
211 fw3_flush_zones(handle
, run_state
, false);
214 fw3_ipt_commit(handle
);
215 fw3_ipt_close(handle
);
218 family_set(run_state
, family
, false);
219 family_set(cfg_state
, family
, false);
225 fw3_destroy_ipsets(run_state
);
227 if (complete
&& (ct
= fopen("/proc/net/nf_conntrack", "w")) != NULL
)
229 info(" * Flushing conntrack table ...");
231 fwrite("f\n", 2, 1, ct
);
235 if (!rv
&& run_state
)
236 fw3_write_statefile(run_state
);
245 enum fw3_family family
;
246 enum fw3_table table
;
247 struct fw3_ipt_handle
*handle
;
250 fw3_create_ipsets(cfg_state
);
252 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
254 if (family
== FW3_FAMILY_V6
&& cfg_state
->defaults
.disable_ipv6
)
257 if (print_family
&& family
!= print_family
)
260 if (!print_family
&& family_running(family
))
262 warn("The %s firewall appears to be started already. "
263 "If it is indeed empty, remove the %s file and retry.",
264 fw3_flag_names
[family
], FW3_STATEFILE
);
269 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
271 if (!fw3_has_table(family
== FW3_FAMILY_V6
, fw3_flag_names
[table
]))
274 if (!(handle
= fw3_ipt_open(family
, table
)))
277 info(" * Populating %s %s table",
278 fw3_flag_names
[family
], fw3_flag_names
[table
]);
280 fw3_print_default_chains(handle
, cfg_state
, false);
281 fw3_print_zone_chains(handle
, cfg_state
, false);
282 fw3_print_default_head_rules(handle
, cfg_state
, false);
283 fw3_print_rules(handle
, cfg_state
);
284 fw3_print_redirects(handle
, cfg_state
);
285 fw3_print_snats(handle
, cfg_state
);
286 fw3_print_forwards(handle
, cfg_state
);
287 fw3_print_zone_rules(handle
, cfg_state
, false);
288 fw3_print_default_tail_rules(handle
, cfg_state
, false);
291 fw3_ipt_commit(handle
);
293 fw3_ipt_close(handle
);
297 fw3_print_includes(cfg_state
, family
, false);
299 family_set(run_state
, family
, true);
300 family_set(cfg_state
, family
, true);
307 fw3_set_defaults(cfg_state
);
311 fw3_run_includes(cfg_state
, false);
312 fw3_hotplug_zones(cfg_state
, true);
313 fw3_write_statefile(cfg_state
);
325 enum fw3_family family
;
326 enum fw3_table table
;
327 struct fw3_ipt_handle
*handle
;
332 fw3_hotplug_zones(run_state
, false);
334 for (family
= FW3_FAMILY_V4
; family
<= FW3_FAMILY_V6
; family
++)
336 if (!family_running(family
))
339 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
341 if (!fw3_has_table(family
== FW3_FAMILY_V6
, fw3_flag_names
[table
]))
344 if (!(handle
= fw3_ipt_open(family
, table
)))
347 info(" * Clearing %s %s table",
348 fw3_flag_names
[family
], fw3_flag_names
[table
]);
350 fw3_flush_rules(handle
, run_state
, true);
351 fw3_flush_zones(handle
, run_state
, true);
352 fw3_ipt_commit(handle
);
353 fw3_ipt_close(handle
);
356 family_set(run_state
, family
, false);
357 family_set(cfg_state
, family
, false);
360 if (family
== FW3_FAMILY_V6
&& cfg_state
->defaults
.disable_ipv6
)
363 for (table
= FW3_TABLE_FILTER
; table
<= FW3_TABLE_RAW
; table
++)
365 if (!fw3_has_table(family
== FW3_FAMILY_V6
, fw3_flag_names
[table
]))
368 if (!(handle
= fw3_ipt_open(family
, table
)))
371 info(" * Populating %s %s table",
372 fw3_flag_names
[family
], fw3_flag_names
[table
]);
374 fw3_print_default_chains(handle
, cfg_state
, true);
375 fw3_print_zone_chains(handle
, cfg_state
, true);
376 fw3_print_default_head_rules(handle
, cfg_state
, true);
377 fw3_print_rules(handle
, cfg_state
);
378 fw3_print_redirects(handle
, cfg_state
);
379 fw3_print_snats(handle
, cfg_state
);
380 fw3_print_forwards(handle
, cfg_state
);
381 fw3_print_zone_rules(handle
, cfg_state
, true);
382 fw3_print_default_tail_rules(handle
, cfg_state
, true);
384 fw3_ipt_commit(handle
);
385 fw3_ipt_close(handle
);
388 fw3_print_includes(cfg_state
, family
, true);
390 family_set(run_state
, family
, true);
391 family_set(cfg_state
, family
, true);
398 fw3_set_defaults(cfg_state
);
399 fw3_run_includes(cfg_state
, true);
400 fw3_hotplug_zones(cfg_state
, true);
401 fw3_write_statefile(cfg_state
);
408 lookup_network(const char *net
)
411 struct fw3_device
*d
;
413 list_for_each_entry(z
, &cfg_state
->zones
, list
)
415 list_for_each_entry(d
, &z
->networks
, list
)
417 if (!strcmp(d
->name
, net
))
419 printf("%s\n", z
->name
);
429 lookup_device(const char *dev
)
432 struct fw3_device
*d
;
434 list_for_each_entry(z
, &cfg_state
->zones
, list
)
436 list_for_each_entry(d
, &z
->devices
, list
)
438 if (!strcmp(d
->name
, dev
))
440 printf("%s\n", z
->name
);
450 lookup_zone(const char *zone
, const char *device
)
453 struct fw3_device
*d
;
455 list_for_each_entry(z
, &cfg_state
->zones
, list
)
457 if (strcmp(z
->name
, zone
))
460 list_for_each_entry(d
, &z
->devices
, list
)
462 if (device
&& strcmp(device
, d
->name
))
465 printf("%s\n", d
->name
);
481 fprintf(stderr
, "fw3 [-4] [-6] [-q] print\n");
482 fprintf(stderr
, "fw3 [-q] {start|stop|flush|reload|restart}\n");
483 fprintf(stderr
, "fw3 [-q] network {net}\n");
484 fprintf(stderr
, "fw3 [-q] device {dev}\n");
485 fprintf(stderr
, "fw3 [-q] zone {zone} [dev]\n");
491 int main(int argc
, char **argv
)
494 enum fw3_family family
= FW3_FAMILY_ANY
;
495 struct fw3_defaults
*defs
= NULL
;
497 while ((ch
= getopt(argc
, argv
, "46dqh")) != -1)
502 family
= FW3_FAMILY_V4
;
506 family
= FW3_FAMILY_V6
;
514 if (freopen("/dev/null", "w", stderr
)) {}
525 defs
= &cfg_state
->defaults
;
533 if (!strcmp(argv
[optind
], "print"))
535 if (family
== FW3_FAMILY_ANY
)
537 family
= FW3_FAMILY_V4
;
539 else if (family
== FW3_FAMILY_V6
)
541 if (defs
->disable_ipv6
)
542 warn("IPv6 rules globally disabled in configuration");
545 warn("IPv6 support is not compiled in");
549 if (freopen("/dev/null", "w", stderr
)) {};
551 cfg_state
->disable_ipsets
= true;
552 print_family
= family
;
557 else if (!strcmp(argv
[optind
], "start"))
565 else if (!strcmp(argv
[optind
], "stop"))
573 else if (!strcmp(argv
[optind
], "flush"))
581 else if (!strcmp(argv
[optind
], "restart"))
590 else if (!strcmp(argv
[optind
], "reload"))
598 else if (!strcmp(argv
[optind
], "network") && (optind
+ 1) < argc
)
600 rv
= lookup_network(argv
[optind
+ 1]);
602 else if (!strcmp(argv
[optind
], "device") && (optind
+ 1) < argc
)
604 rv
= lookup_device(argv
[optind
+ 1]);
606 else if (!strcmp(argv
[optind
], "zone") && (optind
+ 1) < argc
)
608 rv
= lookup_zone(argv
[optind
+ 1], argv
[optind
+ 2]);
617 free_state(cfg_state
);
620 free_state(run_state
);