2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
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.
24 #define C(f, tbl, tgt, fmt) \
25 { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_FLAG_##tgt, fmt }
27 static const struct fw3_chain_spec zone_chains[] = {
28 C(ANY, FILTER, UNSPEC, "zone_?_input"),
29 C(ANY, FILTER, UNSPEC, "zone_?_output"),
30 C(ANY, FILTER, UNSPEC, "zone_?_forward"),
32 C(ANY, FILTER, SRC_ACCEPT, "zone_?_src_ACCEPT"),
33 C(ANY, FILTER, SRC_REJECT, "zone_?_src_REJECT"),
34 C(ANY, FILTER, SRC_DROP, "zone_?_src_DROP"),
36 C(ANY, FILTER, ACCEPT, "zone_?_dest_ACCEPT"),
37 C(ANY, FILTER, REJECT, "zone_?_dest_REJECT"),
38 C(ANY, FILTER, DROP, "zone_?_dest_DROP"),
40 C(V4, NAT, SNAT, "zone_?_postrouting"),
41 C(V4, NAT, DNAT, "zone_?_prerouting"),
43 C(ANY, RAW, HELPER, "zone_?_helper"),
44 C(ANY, RAW, NOTRACK, "zone_?_notrack"),
46 C(ANY, FILTER, CUSTOM_CHAINS, "input_?_rule"),
47 C(ANY, FILTER, CUSTOM_CHAINS, "output_?_rule"),
48 C(ANY, FILTER, CUSTOM_CHAINS, "forwarding_?_rule"),
50 C(V4, NAT, CUSTOM_CHAINS, "prerouting_?_rule"),
51 C(V4, NAT, CUSTOM_CHAINS, "postrouting_?_rule"),
56 enum fw3_zone_logmask {
57 FW3_ZONE_LOG_FILTER = (1 << 0),
58 FW3_ZONE_LOG_MANGLE = (1 << 1),
61 const struct fw3_option fw3_zone_opts[] = {
62 FW3_OPT("enabled", bool, zone, enabled),
64 FW3_OPT("name", string, zone, name),
65 FW3_OPT("family", family, zone, family),
67 FW3_LIST("network", device, zone, networks),
68 FW3_LIST("device", device, zone, devices),
69 FW3_LIST("subnet", network, zone, subnets),
71 FW3_OPT("input", target, zone, policy_input),
72 FW3_OPT("forward", target, zone, policy_forward),
73 FW3_OPT("output", target, zone, policy_output),
75 FW3_OPT("masq", bool, zone, masq),
76 FW3_OPT("masq_allow_invalid", bool, zone, masq_allow_invalid),
77 FW3_LIST("masq_src", network, zone, masq_src),
78 FW3_LIST("masq_dest", network, zone, masq_dest),
80 FW3_OPT("extra", string, zone, extra_src),
81 FW3_OPT("extra_src", string, zone, extra_src),
82 FW3_OPT("extra_dest", string, zone, extra_dest),
84 FW3_OPT("mtu_fix", bool, zone, mtu_fix),
85 FW3_OPT("custom_chains", bool, zone, custom_chains),
87 FW3_OPT("log", int, zone, log),
88 FW3_OPT("log_limit", limit, zone, log_limit),
90 FW3_OPT("auto_helper", bool, zone, auto_helper),
91 FW3_LIST("helper", cthelper, zone, cthelpers),
93 FW3_OPT("__flags_v4", int, zone, flags[0]),
94 FW3_OPT("__flags_v6", int, zone, flags[1]),
96 FW3_LIST("__addrs", address, zone, old_addrs),
102 check_policy(struct uci_element *e, enum fw3_flag *pol, enum fw3_flag def,
105 if (*pol == FW3_FLAG_UNSPEC)
107 warn_elem(e, "has no %s policy specified, using default", name);
110 else if (*pol > FW3_FLAG_DROP)
112 warn_elem(e, "has invalid %s policy, using default", name);
118 check_masq_addrs(struct list_head *head)
120 struct fw3_address *addr;
121 int n_addr = 0, n_failed = 0;
123 list_for_each_entry(addr, head, list)
130 if (!addr->set && addr->resolved)
134 return (n_addr == 0 || n_failed < n_addr);
138 resolve_networks(struct uci_element *e, struct fw3_zone *zone)
140 struct fw3_device *net, *tmp;
142 list_for_each_entry(net, &zone->networks, list)
144 tmp = fw3_ubus_device(net->name);
148 warn_elem(e, "cannot resolve device of network '%s'", net->name);
152 snprintf(tmp->network, sizeof(tmp->network), "%s", net->name);
153 list_add_tail(&tmp->list, &zone->devices);
158 resolve_cthelpers(struct fw3_state *s, struct uci_element *e, struct fw3_zone *zone)
160 struct fw3_cthelpermatch *match;
162 if (list_empty(&zone->cthelpers))
164 if (!zone->masq && zone->auto_helper)
166 fw3_setbit(zone->flags[0], FW3_FLAG_HELPER);
167 fw3_setbit(zone->flags[1], FW3_FLAG_HELPER);
173 list_for_each_entry(match, &zone->cthelpers, list)
177 warn_elem(e, "must not use a negated helper match");
181 match->ptr = fw3_lookup_cthelper(s, match->name);
185 warn_elem(e, "refers to not existing helper '%s'", match->name);
189 if (fw3_is_family(match->ptr, FW3_FAMILY_V4))
190 fw3_setbit(zone->flags[0], FW3_FLAG_HELPER);
192 if (fw3_is_family(match->ptr, FW3_FAMILY_V6))
193 fw3_setbit(zone->flags[1], FW3_FLAG_HELPER);
200 struct fw3_zone *zone;
202 zone = calloc(1, sizeof(*zone));
206 INIT_LIST_HEAD(&zone->networks);
207 INIT_LIST_HEAD(&zone->devices);
208 INIT_LIST_HEAD(&zone->subnets);
209 INIT_LIST_HEAD(&zone->masq_src);
210 INIT_LIST_HEAD(&zone->masq_dest);
211 INIT_LIST_HEAD(&zone->cthelpers);
213 INIT_LIST_HEAD(&zone->old_addrs);
215 zone->enabled = true;
216 zone->auto_helper = true;
217 zone->custom_chains = true;
218 zone->log_limit.rate = 10;
224 fw3_load_zones(struct fw3_state *state, struct uci_package *p)
226 struct uci_section *s;
227 struct uci_element *e;
228 struct fw3_zone *zone;
229 struct fw3_defaults *defs = &state->defaults;
231 INIT_LIST_HEAD(&state->zones);
233 uci_foreach_element(&p->sections, e)
235 s = uci_to_section(e);
237 if (strcmp(s->type, "zone"))
240 zone = fw3_alloc_zone();
245 if (!fw3_parse_options(zone, fw3_zone_opts, s))
246 warn_elem(e, "has invalid options");
254 if (!zone->extra_dest)
255 zone->extra_dest = zone->extra_src;
257 if (!defs->custom_chains && zone->custom_chains)
258 zone->custom_chains = false;
260 if (!defs->auto_helper && zone->auto_helper)
261 zone->auto_helper = false;
263 if (!zone->name || !*zone->name)
265 warn_elem(e, "has no name - ignoring");
270 if (strlen(zone->name) > FW3_ZONE_MAXNAMELEN)
272 warn_elem(e, "must not have a name longer than %u characters",
273 FW3_ZONE_MAXNAMELEN);
278 fw3_ubus_zone_devices(zone);
280 if (list_empty(&zone->networks) && list_empty(&zone->devices) &&
281 list_empty(&zone->subnets) && !zone->extra_src)
283 warn_elem(e, "has no device, network, subnet or extra options");
286 if (!check_masq_addrs(&zone->masq_src))
288 warn_elem(e, "has unresolved masq_src, disabling masq");
292 if (!check_masq_addrs(&zone->masq_dest))
294 warn_elem(e, "has unresolved masq_dest, disabling masq");
298 check_policy(e, &zone->policy_input, defs->policy_input, "input");
299 check_policy(e, &zone->policy_output, defs->policy_output, "output");
300 check_policy(e, &zone->policy_forward, defs->policy_forward, "forward");
302 resolve_networks(e, zone);
306 fw3_setbit(zone->flags[0], FW3_FLAG_SNAT);
309 if (zone->custom_chains)
311 fw3_setbit(zone->flags[0], FW3_FLAG_SNAT);
312 fw3_setbit(zone->flags[0], FW3_FLAG_DNAT);
315 resolve_cthelpers(state, e, zone);
317 fw3_setbit(zone->flags[0], fw3_to_src_target(zone->policy_input));
318 fw3_setbit(zone->flags[0], zone->policy_forward);
319 fw3_setbit(zone->flags[0], zone->policy_output);
321 fw3_setbit(zone->flags[1], fw3_to_src_target(zone->policy_input));
322 fw3_setbit(zone->flags[1], zone->policy_forward);
323 fw3_setbit(zone->flags[1], zone->policy_output);
325 list_add_tail(&zone->list, &state->zones);
331 format_chain(const char *fmt, const char *zonename)
333 static char chain[32];
338 for (p = chain, rem = sizeof(chain); *fmt; fmt++) {
340 len = snprintf(p, rem, "%s", zonename);
342 if (len < 0 || len >= rem)
363 print_zone_chain(struct fw3_ipt_handle *handle, struct fw3_state *state,
364 bool reload, struct fw3_zone *zone)
367 struct fw3_ipt_rule *r;
368 const struct fw3_chain_spec *c;
370 const char *flt_chains[] = {
373 "forward", "forwarding",
376 const char *nat_chains[] = {
377 "prerouting", "prerouting",
378 "postrouting", "postrouting",
381 if (!fw3_is_family(zone, handle->family))
384 set(zone->flags, handle->family, handle->table);
386 if (zone->custom_chains)
387 set(zone->flags, handle->family, FW3_FLAG_CUSTOM_CHAINS);
389 for (c = zone_chains; c->format; c++)
391 if (!fw3_is_family(c, handle->family))
394 if (c->table != handle->table)
398 !fw3_hasbit(zone->flags[handle->family == FW3_FAMILY_V6], c->flag))
401 fw3_ipt_create_chain(handle, reload, format_chain(c->format, zone->name));
404 if (zone->custom_chains)
406 if (handle->table == FW3_TABLE_FILTER)
408 for (i = 0; i < sizeof(flt_chains)/sizeof(flt_chains[0]); i += 2)
410 r = fw3_ipt_rule_new(handle);
411 fw3_ipt_rule_comment(r, "Custom %s %s rule chain", zone->name, flt_chains[i+1]);
412 fw3_ipt_rule_target(r, "%s_%s_rule", flt_chains[i+1], zone->name);
413 fw3_ipt_rule_append(r, "zone_%s_%s", zone->name, flt_chains[i]);
416 else if (handle->table == FW3_TABLE_NAT)
418 for (i = 0; i < sizeof(nat_chains)/sizeof(nat_chains[0]); i += 2)
420 r = fw3_ipt_rule_new(handle);
421 fw3_ipt_rule_comment(r, "Custom %s %s rule chain", zone->name, nat_chains[i+1]);
422 fw3_ipt_rule_target(r, "%s_%s_rule", nat_chains[i+1], zone->name);
423 fw3_ipt_rule_append(r, "zone_%s_%s", zone->name, nat_chains[i]);
428 set(zone->flags, handle->family, handle->table);
432 print_interface_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
433 bool reload, struct fw3_zone *zone,
434 struct fw3_device *dev, struct fw3_address *sub)
436 struct fw3_protocol tcp = { .protocol = 6 };
437 struct fw3_ipt_rule *r;
444 const char *chains[] = {
447 "forward", "FORWARD",
450 #define jump_target(t) \
451 ((t == FW3_FLAG_REJECT) ? "reject" : fw3_flag_names[t])
453 if (handle->table == FW3_TABLE_FILTER)
455 for (t = FW3_FLAG_ACCEPT; t <= FW3_FLAG_DROP; t++)
457 if (t > FW3_FLAG_ACCEPT && zone->log & FW3_ZONE_LOG_FILTER)
459 if (has(zone->flags, handle->family, fw3_to_src_target(t)))
461 r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
463 snprintf(buf, sizeof(buf) - 1, "%s %s in: ",
464 fw3_flag_names[t], zone->name);
466 fw3_ipt_rule_limit(r, &zone->log_limit);
467 fw3_ipt_rule_target(r, "LOG");
468 fw3_ipt_rule_addarg(r, false, "--log-prefix", buf);
469 fw3_ipt_rule_replace(r, "zone_%s_src_%s",
470 zone->name, fw3_flag_names[t]);
473 if (has(zone->flags, handle->family, t))
475 r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
477 snprintf(buf, sizeof(buf) - 1, "%s %s out: ",
478 fw3_flag_names[t], zone->name);
480 fw3_ipt_rule_limit(r, &zone->log_limit);
481 fw3_ipt_rule_target(r, "LOG");
482 fw3_ipt_rule_addarg(r, false, "--log-prefix", buf);
483 fw3_ipt_rule_replace(r, "zone_%s_dest_%s",
484 zone->name, fw3_flag_names[t]);
488 if (has(zone->flags, handle->family, fw3_to_src_target(t)))
490 r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
491 fw3_ipt_rule_target(r, jump_target(t));
492 fw3_ipt_rule_extra(r, zone->extra_src);
494 if (t == FW3_FLAG_ACCEPT && !state->defaults.drop_invalid)
495 fw3_ipt_rule_extra(r,
496 "-m conntrack --ctstate NEW,UNTRACKED");
498 fw3_ipt_rule_replace(r, "zone_%s_src_%s", zone->name,
502 if (has(zone->flags, handle->family, t))
504 if (t == FW3_FLAG_ACCEPT &&
505 zone->masq && !zone->masq_allow_invalid)
507 r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
508 fw3_ipt_rule_extra(r, "-m conntrack --ctstate INVALID");
509 fw3_ipt_rule_comment(r, "Prevent NAT leakage");
510 fw3_ipt_rule_target(r, fw3_flag_names[FW3_FLAG_DROP]);
511 fw3_ipt_rule_replace(r, "zone_%s_dest_%s", zone->name,
515 r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
516 fw3_ipt_rule_target(r, jump_target(t));
517 fw3_ipt_rule_extra(r, zone->extra_dest);
518 fw3_ipt_rule_replace(r, "zone_%s_dest_%s", zone->name,
523 for (i = 0; i < sizeof(chains)/sizeof(chains[0]); i += 2)
525 if (*chains[i] == 'o')
526 r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
528 r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
530 fw3_ipt_rule_target(r, "zone_%s_%s", zone->name, chains[i]);
532 if (*chains[i] == 'o')
533 fw3_ipt_rule_extra(r, zone->extra_dest);
535 fw3_ipt_rule_extra(r, zone->extra_src);
537 fw3_ipt_rule_replace(r, chains[i + 1]);
540 else if (handle->table == FW3_TABLE_NAT)
542 if (has(zone->flags, handle->family, FW3_FLAG_DNAT))
544 r = fw3_ipt_rule_create(handle, NULL, dev, NULL, sub, NULL);
545 fw3_ipt_rule_target(r, "zone_%s_prerouting", zone->name);
546 fw3_ipt_rule_extra(r, zone->extra_src);
547 fw3_ipt_rule_replace(r, "PREROUTING");
550 if (has(zone->flags, handle->family, FW3_FLAG_SNAT))
552 r = fw3_ipt_rule_create(handle, NULL, NULL, dev, NULL, sub);
553 fw3_ipt_rule_target(r, "zone_%s_postrouting", zone->name);
554 fw3_ipt_rule_extra(r, zone->extra_dest);
555 fw3_ipt_rule_replace(r, "POSTROUTING");
558 else if (handle->table == FW3_TABLE_MANGLE)
562 if (zone->log & FW3_ZONE_LOG_MANGLE)
564 snprintf(buf, sizeof(buf) - 1, "MSSFIX %s out: ", zone->name);
566 r = fw3_ipt_rule_create(handle, &tcp, NULL, dev, NULL, sub);
567 fw3_ipt_rule_addarg(r, false, "--tcp-flags", "SYN,RST");
568 fw3_ipt_rule_addarg(r, false, "SYN", NULL);
569 fw3_ipt_rule_limit(r, &zone->log_limit);
570 fw3_ipt_rule_comment(r, "Zone %s MTU fix logging", zone->name);
571 fw3_ipt_rule_target(r, "LOG");
572 fw3_ipt_rule_addarg(r, false, "--log-prefix", buf);
573 fw3_ipt_rule_replace(r, "FORWARD");
576 r = fw3_ipt_rule_create(handle, &tcp, NULL, dev, NULL, sub);
577 fw3_ipt_rule_addarg(r, false, "--tcp-flags", "SYN,RST");
578 fw3_ipt_rule_addarg(r, false, "SYN", NULL);
579 fw3_ipt_rule_comment(r, "Zone %s MTU fixing", zone->name);
580 fw3_ipt_rule_target(r, "TCPMSS");
581 fw3_ipt_rule_addarg(r, false, "--clamp-mss-to-pmtu", NULL);
582 fw3_ipt_rule_replace(r, "FORWARD");
585 else if (handle->table == FW3_TABLE_RAW)
587 bool loopback_dev = (dev != NULL && !dev->any &&
588 !dev->invert && fw3_check_loopback_dev(dev->name));
589 char *chain = loopback_dev || (sub != NULL && !sub->invert && fw3_check_loopback_addr(sub)) ?
590 "OUTPUT" : "PREROUTING";
592 if (has(zone->flags, handle->family, FW3_FLAG_HELPER))
594 r = fw3_ipt_rule_create(handle, NULL, loopback_dev ? NULL : dev, NULL, sub, NULL);
595 fw3_ipt_rule_comment(r, "%s CT helper assignment", zone->name);
596 fw3_ipt_rule_target(r, "zone_%s_helper", zone->name);
597 fw3_ipt_rule_extra(r, zone->extra_src);
598 fw3_ipt_rule_replace(r, chain);
601 if (has(zone->flags, handle->family, FW3_FLAG_NOTRACK))
603 r = fw3_ipt_rule_create(handle, NULL, loopback_dev ? NULL : dev, NULL, sub, NULL);
604 fw3_ipt_rule_comment(r, "%s CT bypass", zone->name);
605 fw3_ipt_rule_target(r, "zone_%s_notrack", zone->name);
606 fw3_ipt_rule_extra(r, zone->extra_src);
607 fw3_ipt_rule_replace(r, chain);
613 print_interface_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
614 bool reload, struct fw3_zone *zone)
616 struct fw3_device *dev;
617 struct fw3_address *sub;
619 fw3_foreach(dev, &zone->devices)
620 fw3_foreach(sub, &zone->subnets)
622 if (!fw3_is_family(sub, handle->family))
625 if (!dev && !sub && !zone->extra_src && !zone->extra_dest)
628 print_interface_rule(handle, state, reload, zone, dev, sub);
632 static struct fw3_address *
633 next_addr(struct fw3_address *addr, struct list_head *list,
634 enum fw3_family family, bool invert)
637 struct fw3_address *rv;
639 for (p = addr ? addr->list.next : list->next; p != list; p = p->next)
641 rv = list_entry(p, struct fw3_address, list);
643 if (fw3_is_family(rv, family) && rv->set && rv->invert == invert)
651 print_zone_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
652 bool reload, struct fw3_zone *zone)
654 bool first_src, first_dest;
655 struct fw3_address *msrc;
656 struct fw3_address *mdest;
657 struct fw3_ipt_rule *r;
659 if (!fw3_is_family(zone, handle->family))
662 info(" * Zone '%s'", zone->name);
664 switch (handle->table)
666 case FW3_TABLE_FILTER:
667 if (has(zone->flags, handle->family, FW3_FLAG_DNAT))
669 r = fw3_ipt_rule_new(handle);
670 fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
671 fw3_ipt_rule_comment(r, "Accept port redirections");
672 fw3_ipt_rule_target(r, fw3_flag_names[FW3_FLAG_ACCEPT]);
673 fw3_ipt_rule_append(r, "zone_%s_input", zone->name);
675 r = fw3_ipt_rule_new(handle);
676 fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
677 fw3_ipt_rule_comment(r, "Accept port forwards");
678 fw3_ipt_rule_target(r, fw3_flag_names[FW3_FLAG_ACCEPT]);
679 fw3_ipt_rule_append(r, "zone_%s_forward", zone->name);
682 r = fw3_ipt_rule_new(handle);
683 fw3_ipt_rule_target(r, "zone_%s_src_%s", zone->name,
684 fw3_flag_names[zone->policy_input]);
685 fw3_ipt_rule_append(r, "zone_%s_input", zone->name);
687 r = fw3_ipt_rule_new(handle);
688 fw3_ipt_rule_target(r, "zone_%s_dest_%s", zone->name,
689 fw3_flag_names[zone->policy_forward]);
690 fw3_ipt_rule_append(r, "zone_%s_forward", zone->name);
692 r = fw3_ipt_rule_new(handle);
693 fw3_ipt_rule_target(r, "zone_%s_dest_%s", zone->name,
694 fw3_flag_names[zone->policy_output]);
695 fw3_ipt_rule_append(r, "zone_%s_output", zone->name);
700 if (zone->masq && handle->family == FW3_FAMILY_V4)
702 /* for any negated masq_src ip, emit -s addr -j RETURN rules */
704 (msrc = next_addr(msrc, &zone->masq_src,
705 handle->family, true)) != NULL; )
707 msrc->invert = false;
708 r = fw3_ipt_rule_new(handle);
709 fw3_ipt_rule_src_dest(r, msrc, NULL);
710 fw3_ipt_rule_target(r, "RETURN");
711 fw3_ipt_rule_append(r, "zone_%s_postrouting", zone->name);
715 /* for any negated masq_dest ip, emit -d addr -j RETURN rules */
717 (mdest = next_addr(mdest, &zone->masq_dest,
718 handle->family, true)) != NULL; )
720 mdest->invert = false;
721 r = fw3_ipt_rule_new(handle);
722 fw3_ipt_rule_src_dest(r, NULL, mdest);
723 fw3_ipt_rule_target(r, "RETURN");
724 fw3_ipt_rule_append(r, "zone_%s_postrouting", zone->name);
725 mdest->invert = true;
728 /* emit masquerading entries for non-negated addresses
729 and ensure that both src and dest loops run at least once,
730 even if there are no relevant addresses */
731 for (first_src = true, msrc = NULL;
732 (msrc = next_addr(msrc, &zone->masq_src,
733 handle->family, false)) || first_src;
736 for (first_dest = true, mdest = NULL;
737 (mdest = next_addr(mdest, &zone->masq_dest,
738 handle->family, false)) || first_dest;
741 r = fw3_ipt_rule_new(handle);
742 fw3_ipt_rule_src_dest(r, msrc, mdest);
743 fw3_ipt_rule_target(r, "MASQUERADE");
744 fw3_ipt_rule_append(r, "zone_%s_postrouting", zone->name);
751 fw3_print_cthelpers(handle, state, zone);
754 case FW3_TABLE_MANGLE:
758 print_interface_rules(handle, state, reload, zone);
762 fw3_print_zone_chains(struct fw3_ipt_handle *handle, struct fw3_state *state,
765 struct fw3_zone *zone;
767 list_for_each_entry(zone, &state->zones, list)
768 print_zone_chain(handle, state, reload, zone);
772 fw3_print_zone_rules(struct fw3_ipt_handle *handle, struct fw3_state *state,
775 struct fw3_zone *zone;
777 list_for_each_entry(zone, &state->zones, list)
778 print_zone_rule(handle, state, reload, zone);
782 fw3_flush_zones(struct fw3_ipt_handle *handle, struct fw3_state *state,
785 struct fw3_zone *z, *tmp;
786 const struct fw3_chain_spec *c;
788 list_for_each_entry_safe(z, tmp, &state->zones, list)
790 if (!has(z->flags, handle->family, handle->table))
793 /* first flush all rules ... */
794 for (c = zone_chains; c->format; c++)
796 /* don't touch user chains on selective stop */
797 if (reload && c->flag == FW3_FLAG_CUSTOM_CHAINS)
800 if (!fw3_is_family(c, handle->family))
803 if (c->table != handle->table)
806 if (c->flag && !has(z->flags, handle->family, c->flag))
809 fw3_ipt_flush_chain(handle, format_chain(c->format, z->name));
812 /* ... then remove the chains */
813 for (c = zone_chains; c->format; c++)
815 if (!fw3_is_family(c, handle->family))
818 if (c->table != handle->table)
821 if (c->flag && !has(z->flags, handle->family, c->flag))
824 fw3_ipt_delete_chain(handle, reload,
825 format_chain(c->format, z->name));
828 del(z->flags, handle->family, handle->table);
833 fw3_hotplug_zones(struct fw3_state *state, bool add)
836 struct fw3_device *d;
838 list_for_each_entry(z, &state->zones, list)
840 if (add != fw3_hasbit(z->flags[0], FW3_FLAG_HOTPLUG))
842 list_for_each_entry(d, &z->devices, list)
843 fw3_hotplug(add, z, d);
846 fw3_setbit(z->flags[0], FW3_FLAG_HOTPLUG);
848 fw3_delbit(z->flags[0], FW3_FLAG_HOTPLUG);
854 fw3_lookup_zone(struct fw3_state *state, const char *name)
858 if (list_empty(&state->zones))
861 list_for_each_entry(z, &state->zones, list)
863 if (strcmp(z->name, name))
873 fw3_resolve_zone_addresses(struct fw3_zone *zone, struct fw3_address *addr)
875 struct fw3_device *net;
876 struct fw3_address *cur, *tmp;
877 struct list_head *all;
879 all = calloc(1, sizeof(*all));
885 if (addr && addr->set)
887 tmp = malloc(sizeof(*tmp));
892 list_add_tail(&tmp->list, all);
897 list_for_each_entry(net, &zone->networks, list)
898 fw3_ubus_address(all, net->name);
900 list_for_each_entry(cur, &zone->subnets, list)
902 tmp = malloc(sizeof(*tmp));
908 list_add_tail(&tmp->list, all);