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.
23 #define C(f, tbl, tgt, fmt) \
24 { FW3_FAMILY_##f, FW3_TABLE_##tbl, FW3_FLAG_##tgt, fmt }
26 static const struct fw3_chain_spec zone_chains
[] = {
27 C(ANY
, FILTER
, UNSPEC
, "zone_%s_input"),
28 C(ANY
, FILTER
, UNSPEC
, "zone_%s_output"),
29 C(ANY
, FILTER
, UNSPEC
, "zone_%s_forward"),
31 C(ANY
, FILTER
, SRC_ACCEPT
, "zone_%s_src_ACCEPT"),
32 C(ANY
, FILTER
, SRC_REJECT
, "zone_%s_src_REJECT"),
33 C(ANY
, FILTER
, SRC_DROP
, "zone_%s_src_DROP"),
35 C(ANY
, FILTER
, ACCEPT
, "zone_%s_dest_ACCEPT"),
36 C(ANY
, FILTER
, REJECT
, "zone_%s_dest_REJECT"),
37 C(ANY
, FILTER
, DROP
, "zone_%s_dest_DROP"),
39 C(V4
, NAT
, SNAT
, "zone_%s_postrouting"),
40 C(V4
, NAT
, DNAT
, "zone_%s_prerouting"),
42 C(ANY
, RAW
, NOTRACK
, "zone_%s_notrack"),
44 C(ANY
, FILTER
, CUSTOM_CHAINS
, "input_%s_rule"),
45 C(ANY
, FILTER
, CUSTOM_CHAINS
, "output_%s_rule"),
46 C(ANY
, FILTER
, CUSTOM_CHAINS
, "forwarding_%s_rule"),
48 C(V4
, NAT
, CUSTOM_CHAINS
, "prerouting_%s_rule"),
49 C(V4
, NAT
, CUSTOM_CHAINS
, "postrouting_%s_rule"),
54 const struct fw3_option fw3_zone_opts
[] = {
55 FW3_OPT("enabled", bool, zone
, enabled
),
57 FW3_OPT("name", string
, zone
, name
),
58 FW3_OPT("family", family
, zone
, family
),
60 FW3_LIST("network", device
, zone
, networks
),
61 FW3_LIST("device", device
, zone
, devices
),
62 FW3_LIST("subnet", network
, zone
, subnets
),
64 FW3_OPT("input", target
, zone
, policy_input
),
65 FW3_OPT("forward", target
, zone
, policy_forward
),
66 FW3_OPT("output", target
, zone
, policy_output
),
68 FW3_OPT("masq", bool, zone
, masq
),
69 FW3_LIST("masq_src", network
, zone
, masq_src
),
70 FW3_LIST("masq_dest", network
, zone
, masq_dest
),
72 FW3_OPT("extra", string
, zone
, extra_src
),
73 FW3_OPT("extra_src", string
, zone
, extra_src
),
74 FW3_OPT("extra_dest", string
, zone
, extra_dest
),
76 FW3_OPT("conntrack", bool, zone
, conntrack
),
77 FW3_OPT("mtu_fix", bool, zone
, mtu_fix
),
78 FW3_OPT("custom_chains", bool, zone
, custom_chains
),
80 FW3_OPT("log", bool, zone
, log
),
81 FW3_OPT("log_limit", limit
, zone
, log_limit
),
83 FW3_OPT("__flags_v4", int, zone
, flags
[0]),
84 FW3_OPT("__flags_v6", int, zone
, flags
[1]),
86 FW3_LIST("__addrs", address
, zone
, old_addrs
),
93 check_policy(struct uci_element
*e
, enum fw3_flag
*pol
, enum fw3_flag def
,
96 if (*pol
== FW3_FLAG_UNSPEC
)
98 warn_elem(e
, "has no %s policy specified, using default", name
);
101 else if (*pol
> FW3_FLAG_DROP
)
103 warn_elem(e
, "has invalid %s policy, using default", name
);
109 resolve_networks(struct uci_element
*e
, struct fw3_zone
*zone
)
111 struct fw3_device
*net
, *tmp
;
113 list_for_each_entry(net
, &zone
->networks
, list
)
115 tmp
= fw3_ubus_device(net
->name
);
119 warn_elem(e
, "cannot resolve device of network '%s'", net
->name
);
123 snprintf(tmp
->network
, sizeof(tmp
->network
), "%s", net
->name
);
124 list_add_tail(&tmp
->list
, &zone
->devices
);
131 struct fw3_zone
*zone
;
133 zone
= calloc(1, sizeof(*zone
));
137 INIT_LIST_HEAD(&zone
->networks
);
138 INIT_LIST_HEAD(&zone
->devices
);
139 INIT_LIST_HEAD(&zone
->subnets
);
140 INIT_LIST_HEAD(&zone
->masq_src
);
141 INIT_LIST_HEAD(&zone
->masq_dest
);
143 INIT_LIST_HEAD(&zone
->old_addrs
);
145 zone
->enabled
= true;
146 zone
->custom_chains
= true;
147 zone
->log_limit
.rate
= 10;
153 fw3_load_zones(struct fw3_state
*state
, struct uci_package
*p
)
155 struct uci_section
*s
;
156 struct uci_element
*e
;
157 struct fw3_zone
*zone
;
158 struct fw3_defaults
*defs
= &state
->defaults
;
160 INIT_LIST_HEAD(&state
->zones
);
162 uci_foreach_element(&p
->sections
, e
)
164 s
= uci_to_section(e
);
166 if (strcmp(s
->type
, "zone"))
169 zone
= fw3_alloc_zone();
174 fw3_parse_options(zone
, fw3_zone_opts
, s
);
182 if (!zone
->extra_dest
)
183 zone
->extra_dest
= zone
->extra_src
;
185 if (!defs
->custom_chains
&& zone
->custom_chains
)
186 zone
->custom_chains
= false;
188 if (!zone
->name
|| !*zone
->name
)
190 warn_elem(e
, "has no name - ignoring");
195 if (strlen(zone
->name
) > FW3_ZONE_MAXNAMELEN
)
197 warn_elem(e
, "must not have a name longer than %u characters",
198 FW3_ZONE_MAXNAMELEN
);
203 fw3_ubus_zone_devices(zone
);
205 if (list_empty(&zone
->networks
) && list_empty(&zone
->devices
) &&
206 list_empty(&zone
->subnets
) && !zone
->extra_src
)
208 warn_elem(e
, "has no device, network, subnet or extra options");
211 check_policy(e
, &zone
->policy_input
, defs
->policy_input
, "input");
212 check_policy(e
, &zone
->policy_output
, defs
->policy_output
, "output");
213 check_policy(e
, &zone
->policy_forward
, defs
->policy_forward
, "forward");
215 resolve_networks(e
, zone
);
219 fw3_setbit(zone
->flags
[0], FW3_FLAG_SNAT
);
220 zone
->conntrack
= true;
223 if (zone
->custom_chains
)
225 fw3_setbit(zone
->flags
[0], FW3_FLAG_SNAT
);
226 fw3_setbit(zone
->flags
[0], FW3_FLAG_DNAT
);
229 fw3_setbit(zone
->flags
[0], fw3_to_src_target(zone
->policy_input
));
230 fw3_setbit(zone
->flags
[0], zone
->policy_forward
);
231 fw3_setbit(zone
->flags
[0], zone
->policy_output
);
233 fw3_setbit(zone
->flags
[1], fw3_to_src_target(zone
->policy_input
));
234 fw3_setbit(zone
->flags
[1], zone
->policy_forward
);
235 fw3_setbit(zone
->flags
[1], zone
->policy_output
);
237 list_add_tail(&zone
->list
, &state
->zones
);
243 print_zone_chain(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
244 bool reload
, struct fw3_zone
*zone
)
247 struct fw3_ipt_rule
*r
;
248 const struct fw3_chain_spec
*c
;
250 const char *flt_chains
[] = {
253 "forward", "forwarding",
256 const char *nat_chains
[] = {
257 "prerouting", "prerouting",
258 "postrouting", "postrouting",
261 if (!fw3_is_family(zone
, handle
->family
))
264 info(" * Zone '%s'", zone
->name
);
266 set(zone
->flags
, handle
->family
, handle
->table
);
268 if (zone
->custom_chains
)
269 set(zone
->flags
, handle
->family
, FW3_FLAG_CUSTOM_CHAINS
);
271 if (!zone
->conntrack
&& !state
->defaults
.drop_invalid
)
272 set(zone
->flags
, handle
->family
, FW3_FLAG_NOTRACK
);
274 for (c
= zone_chains
; c
->format
; c
++)
276 /* don't touch user chains on selective stop */
277 if (reload
&& c
->flag
== FW3_FLAG_CUSTOM_CHAINS
)
280 if (!fw3_is_family(c
, handle
->family
))
283 if (c
->table
!= handle
->table
)
287 !fw3_hasbit(zone
->flags
[handle
->family
== FW3_FAMILY_V6
], c
->flag
))
290 fw3_ipt_create_chain(handle
, c
->format
, zone
->name
);
293 if (zone
->custom_chains
)
295 if (handle
->table
== FW3_TABLE_FILTER
)
297 for (i
= 0; i
< sizeof(flt_chains
)/sizeof(flt_chains
[0]); i
+= 2)
299 r
= fw3_ipt_rule_new(handle
);
300 fw3_ipt_rule_comment(r
, "user chain for %s", flt_chains
[i
+1]);
301 fw3_ipt_rule_target(r
, "%s_%s_rule", flt_chains
[i
+1], zone
->name
);
302 fw3_ipt_rule_append(r
, "zone_%s_%s", zone
->name
, flt_chains
[i
]);
305 else if (handle
->table
== FW3_TABLE_NAT
)
307 for (i
= 0; i
< sizeof(nat_chains
)/sizeof(nat_chains
[0]); i
+= 2)
309 r
= fw3_ipt_rule_new(handle
);
310 fw3_ipt_rule_comment(r
, "user chain for %s", nat_chains
[i
+1]);
311 fw3_ipt_rule_target(r
, "%s_%s_rule", nat_chains
[i
+1], zone
->name
);
312 fw3_ipt_rule_append(r
, "zone_%s_%s", zone
->name
, nat_chains
[i
]);
317 set(zone
->flags
, handle
->family
, handle
->table
);
321 print_interface_rule(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
322 bool reload
, struct fw3_zone
*zone
,
323 struct fw3_device
*dev
, struct fw3_address
*sub
)
325 struct fw3_protocol tcp
= { .protocol
= 6 };
326 struct fw3_ipt_rule
*r
;
333 const char *chains
[] = {
336 "forward", "FORWARD",
339 #define jump_target(t) \
340 ((t == FW3_FLAG_REJECT) ? "reject" : fw3_flag_names[t])
342 if (handle
->table
== FW3_TABLE_FILTER
)
344 for (t
= FW3_FLAG_ACCEPT
; t
<= FW3_FLAG_DROP
; t
++)
346 if (has(zone
->flags
, handle
->family
, fw3_to_src_target(t
)))
348 r
= fw3_ipt_rule_create(handle
, NULL
, dev
, NULL
, sub
, NULL
);
349 fw3_ipt_rule_target(r
, jump_target(t
));
350 fw3_ipt_rule_extra(r
, zone
->extra_src
);
352 if (t
== FW3_FLAG_ACCEPT
&& !state
->defaults
.drop_invalid
)
353 fw3_ipt_rule_extra(r
,
354 "-m conntrack --ctstate NEW,UNTRACKED");
356 fw3_ipt_rule_replace(r
, "zone_%s_src_%s", zone
->name
,
360 if (has(zone
->flags
, handle
->family
, t
))
362 r
= fw3_ipt_rule_create(handle
, NULL
, NULL
, dev
, NULL
, sub
);
363 fw3_ipt_rule_target(r
, jump_target(t
));
364 fw3_ipt_rule_extra(r
, zone
->extra_dest
);
366 if (t
== FW3_FLAG_ACCEPT
&& !state
->defaults
.drop_invalid
)
367 fw3_ipt_rule_extra(r
,
368 "-m conntrack --ctstate NEW,UNTRACKED");
370 fw3_ipt_rule_replace(r
, "zone_%s_dest_%s", zone
->name
,
375 for (i
= 0; i
< sizeof(chains
)/sizeof(chains
[0]); i
+= 2)
377 if (*chains
[i
] == 'o')
378 r
= fw3_ipt_rule_create(handle
, NULL
, NULL
, dev
, NULL
, sub
);
380 r
= fw3_ipt_rule_create(handle
, NULL
, dev
, NULL
, sub
, NULL
);
382 fw3_ipt_rule_target(r
, "zone_%s_%s", zone
->name
, chains
[i
]);
384 if (*chains
[i
] == 'o')
385 fw3_ipt_rule_extra(r
, zone
->extra_dest
);
387 fw3_ipt_rule_extra(r
, zone
->extra_src
);
389 fw3_ipt_rule_replace(r
, chains
[i
+ 1]);
392 else if (handle
->table
== FW3_TABLE_NAT
)
394 if (has(zone
->flags
, handle
->family
, FW3_FLAG_DNAT
))
396 r
= fw3_ipt_rule_create(handle
, NULL
, dev
, NULL
, sub
, NULL
);
397 fw3_ipt_rule_target(r
, "zone_%s_prerouting", zone
->name
);
398 fw3_ipt_rule_extra(r
, zone
->extra_src
);
399 fw3_ipt_rule_replace(r
, "PREROUTING");
402 if (has(zone
->flags
, handle
->family
, FW3_FLAG_SNAT
))
404 r
= fw3_ipt_rule_create(handle
, NULL
, NULL
, dev
, NULL
, sub
);
405 fw3_ipt_rule_target(r
, "zone_%s_postrouting", zone
->name
);
406 fw3_ipt_rule_extra(r
, zone
->extra_dest
);
407 fw3_ipt_rule_replace(r
, "POSTROUTING");
410 else if (handle
->table
== FW3_TABLE_MANGLE
)
416 snprintf(buf
, sizeof(buf
) - 1, "MSSFIX(%s): ", zone
->name
);
418 r
= fw3_ipt_rule_create(handle
, &tcp
, NULL
, dev
, NULL
, sub
);
419 fw3_ipt_rule_addarg(r
, false, "--tcp-flags", "SYN,RST");
420 fw3_ipt_rule_addarg(r
, false, "SYN", NULL
);
421 fw3_ipt_rule_limit(r
, &zone
->log_limit
);
422 fw3_ipt_rule_comment(r
, "%s (mtu_fix logging)", zone
->name
);
423 fw3_ipt_rule_target(r
, "LOG");
424 fw3_ipt_rule_addarg(r
, false, "--log-prefix", buf
);
425 fw3_ipt_rule_replace(r
, "FORWARD");
428 r
= fw3_ipt_rule_create(handle
, &tcp
, NULL
, dev
, NULL
, sub
);
429 fw3_ipt_rule_addarg(r
, false, "--tcp-flags", "SYN,RST");
430 fw3_ipt_rule_addarg(r
, false, "SYN", NULL
);
431 fw3_ipt_rule_comment(r
, "%s (mtu_fix)", zone
->name
);
432 fw3_ipt_rule_target(r
, "TCPMSS");
433 fw3_ipt_rule_addarg(r
, false, "--clamp-mss-to-pmtu", NULL
);
434 fw3_ipt_rule_replace(r
, "FORWARD");
437 else if (handle
->table
== FW3_TABLE_RAW
)
439 if (has(zone
->flags
, handle
->family
, FW3_FLAG_NOTRACK
))
441 r
= fw3_ipt_rule_create(handle
, NULL
, dev
, NULL
, sub
, NULL
);
442 fw3_ipt_rule_target(r
, "zone_%s_notrack", zone
->name
);
443 fw3_ipt_rule_extra(r
, zone
->extra_src
);
444 fw3_ipt_rule_replace(r
, "PREROUTING");
450 print_interface_rules(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
451 bool reload
, struct fw3_zone
*zone
)
453 struct fw3_device
*dev
;
454 struct fw3_address
*sub
;
456 fw3_foreach(dev
, &zone
->devices
)
457 fw3_foreach(sub
, &zone
->subnets
)
459 if (!fw3_is_family(sub
, handle
->family
))
465 print_interface_rule(handle
, state
, reload
, zone
, dev
, sub
);
469 static struct fw3_address
*
470 next_addr(struct fw3_address
*addr
, struct list_head
*list
,
471 enum fw3_family family
, bool invert
)
474 struct fw3_address
*rv
;
476 for (p
= addr
? addr
->list
.next
: list
->next
; p
!= list
; p
= p
->next
)
478 rv
= list_entry(p
, struct fw3_address
, list
);
480 if (fw3_is_family(rv
, family
) && rv
->invert
== invert
)
488 print_zone_rule(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
489 bool reload
, struct fw3_zone
*zone
)
491 bool disable_notrack
= state
->defaults
.drop_invalid
;
492 bool first_src
, first_dest
;
493 struct fw3_address
*msrc
;
494 struct fw3_address
*mdest
;
495 struct fw3_ipt_rule
*r
;
500 if (!fw3_is_family(zone
, handle
->family
))
503 switch (handle
->table
)
505 case FW3_TABLE_FILTER
:
506 if (has(zone
->flags
, handle
->family
, FW3_FLAG_DNAT
))
508 r
= fw3_ipt_rule_new(handle
);
509 fw3_ipt_rule_extra(r
, "-m conntrack --ctstate DNAT");
510 fw3_ipt_rule_comment(r
, "Accept port redirections");
511 fw3_ipt_rule_target(r
, fw3_flag_names
[FW3_FLAG_ACCEPT
]);
512 fw3_ipt_rule_append(r
, "zone_%s_input", zone
->name
);
514 r
= fw3_ipt_rule_new(handle
);
515 fw3_ipt_rule_extra(r
, "-m conntrack --ctstate DNAT");
516 fw3_ipt_rule_comment(r
, "Accept port forwards");
517 fw3_ipt_rule_target(r
, fw3_flag_names
[FW3_FLAG_ACCEPT
]);
518 fw3_ipt_rule_append(r
, "zone_%s_forward", zone
->name
);
521 r
= fw3_ipt_rule_new(handle
);
522 fw3_ipt_rule_target(r
, "zone_%s_src_%s", zone
->name
,
523 fw3_flag_names
[zone
->policy_input
]);
524 fw3_ipt_rule_append(r
, "zone_%s_input", zone
->name
);
526 r
= fw3_ipt_rule_new(handle
);
527 fw3_ipt_rule_target(r
, "zone_%s_dest_%s", zone
->name
,
528 fw3_flag_names
[zone
->policy_forward
]);
529 fw3_ipt_rule_append(r
, "zone_%s_forward", zone
->name
);
531 r
= fw3_ipt_rule_new(handle
);
532 fw3_ipt_rule_target(r
, "zone_%s_dest_%s", zone
->name
,
533 fw3_flag_names
[zone
->policy_output
]);
534 fw3_ipt_rule_append(r
, "zone_%s_output", zone
->name
);
538 for (t
= FW3_FLAG_REJECT
; t
<= FW3_FLAG_DROP
; t
++)
540 if (has(zone
->flags
, handle
->family
, fw3_to_src_target(t
)))
542 r
= fw3_ipt_rule_new(handle
);
544 snprintf(buf
, sizeof(buf
) - 1, "%s(src %s)",
545 fw3_flag_names
[t
], zone
->name
);
547 fw3_ipt_rule_limit(r
, &zone
->log_limit
);
548 fw3_ipt_rule_target(r
, "LOG");
549 fw3_ipt_rule_addarg(r
, false, "--log-prefix", buf
);
550 fw3_ipt_rule_append(r
, "zone_%s_src_%s",
551 zone
->name
, fw3_flag_names
[t
]);
554 if (has(zone
->flags
, handle
->family
, t
))
556 r
= fw3_ipt_rule_new(handle
);
558 snprintf(buf
, sizeof(buf
) - 1, "%s(dest %s)",
559 fw3_flag_names
[t
], zone
->name
);
561 fw3_ipt_rule_limit(r
, &zone
->log_limit
);
562 fw3_ipt_rule_target(r
, "LOG");
563 fw3_ipt_rule_addarg(r
, false, "--log-prefix", buf
);
564 fw3_ipt_rule_append(r
, "zone_%s_dest_%s",
565 zone
->name
, fw3_flag_names
[t
]);
572 if (zone
->masq
&& handle
->family
== FW3_FAMILY_V4
)
574 /* for any negated masq_src ip, emit -s addr -j RETURN rules */
576 (msrc
= next_addr(msrc
, &zone
->masq_src
,
577 handle
->family
, true)) != NULL
; )
579 msrc
->invert
= false;
580 r
= fw3_ipt_rule_new(handle
);
581 fw3_ipt_rule_src_dest(r
, msrc
, NULL
);
582 fw3_ipt_rule_target(r
, "RETURN");
583 fw3_ipt_rule_append(r
, "zone_%s_postrouting", zone
->name
);
587 /* for any negated masq_dest ip, emit -d addr -j RETURN rules */
589 (mdest
= next_addr(mdest
, &zone
->masq_dest
,
590 handle
->family
, true)) != NULL
; )
592 mdest
->invert
= false;
593 r
= fw3_ipt_rule_new(handle
);
594 fw3_ipt_rule_src_dest(r
, NULL
, mdest
);
595 fw3_ipt_rule_target(r
, "RETURN");
596 fw3_ipt_rule_append(r
, "zone_%s_postrouting", zone
->name
);
597 mdest
->invert
= true;
600 /* emit masquerading entries for non-negated addresses
601 and ensure that both src and dest loops run at least once,
602 even if there are no relevant addresses */
603 for (first_src
= true, msrc
= NULL
;
604 (msrc
= next_addr(msrc
, &zone
->masq_src
,
605 handle
->family
, false)) || first_src
;
608 for (first_dest
= true, mdest
= NULL
;
609 (mdest
= next_addr(mdest
, &zone
->masq_dest
,
610 handle
->family
, false)) || first_dest
;
613 r
= fw3_ipt_rule_new(handle
);
614 fw3_ipt_rule_src_dest(r
, msrc
, mdest
);
615 fw3_ipt_rule_target(r
, "MASQUERADE");
616 fw3_ipt_rule_append(r
, "zone_%s_postrouting", zone
->name
);
623 if (!zone
->conntrack
&& !disable_notrack
)
625 r
= fw3_ipt_rule_new(handle
);
626 fw3_ipt_rule_target(r
, "CT");
627 fw3_ipt_rule_addarg(r
, false, "--notrack", NULL
);
628 fw3_ipt_rule_append(r
, "zone_%s_notrack", zone
->name
);
632 case FW3_TABLE_MANGLE
:
636 print_interface_rules(handle
, state
, reload
, zone
);
640 fw3_print_zone_chains(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
643 struct fw3_zone
*zone
;
645 list_for_each_entry(zone
, &state
->zones
, list
)
646 print_zone_chain(handle
, state
, reload
, zone
);
650 fw3_print_zone_rules(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
653 struct fw3_zone
*zone
;
655 list_for_each_entry(zone
, &state
->zones
, list
)
656 print_zone_rule(handle
, state
, reload
, zone
);
660 fw3_flush_zones(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
663 struct fw3_zone
*z
, *tmp
;
664 const struct fw3_chain_spec
*c
;
667 list_for_each_entry_safe(z
, tmp
, &state
->zones
, list
)
669 if (!has(z
->flags
, handle
->family
, handle
->table
))
672 for (c
= zone_chains
; c
->format
; c
++)
674 /* don't touch user chains on selective stop */
675 if (reload
&& c
->flag
== FW3_FLAG_CUSTOM_CHAINS
)
678 if (!fw3_is_family(c
, handle
->family
))
681 if (c
->table
!= handle
->table
)
684 if (c
->flag
&& !has(z
->flags
, handle
->family
, c
->flag
))
687 snprintf(chain
, sizeof(chain
), c
->format
, z
->name
);
688 fw3_ipt_flush_chain(handle
, chain
);
690 /* keep certain basic chains that do not depend on any settings to
691 avoid purging unrelated user rules pointing to them */
692 if (reload
&& !c
->flag
)
695 fw3_ipt_delete_chain(handle
, chain
);
698 del(z
->flags
, handle
->family
, handle
->table
);
703 fw3_hotplug_zones(struct fw3_state
*state
, bool add
)
706 struct fw3_device
*d
;
708 list_for_each_entry(z
, &state
->zones
, list
)
710 if (add
!= fw3_hasbit(z
->flags
[0], FW3_FLAG_HOTPLUG
))
712 list_for_each_entry(d
, &z
->devices
, list
)
713 fw3_hotplug(add
, z
, d
);
716 fw3_setbit(z
->flags
[0], FW3_FLAG_HOTPLUG
);
718 fw3_delbit(z
->flags
[0], FW3_FLAG_HOTPLUG
);
724 fw3_lookup_zone(struct fw3_state
*state
, const char *name
)
728 if (list_empty(&state
->zones
))
731 list_for_each_entry(z
, &state
->zones
, list
)
733 if (strcmp(z
->name
, name
))
743 fw3_resolve_zone_addresses(struct fw3_zone
*zone
, struct fw3_address
*addr
)
745 struct fw3_device
*net
;
746 struct fw3_address
*cur
, *tmp
;
747 struct list_head
*all
;
749 all
= calloc(1, sizeof(*all
));
755 if (addr
&& addr
->set
)
757 tmp
= malloc(sizeof(*tmp
));
762 list_add_tail(&tmp
->list
, all
);
767 list_for_each_entry(net
, &zone
->networks
, list
)
768 fw3_ubus_address(all
, net
->name
);
770 list_for_each_entry(cur
, &zone
->subnets
, list
)
772 tmp
= malloc(sizeof(*tmp
));
778 list_add_tail(&tmp
->list
, all
);