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_%s_input"),
29 C(ANY
, FILTER
, UNSPEC
, "zone_%s_output"),
30 C(ANY
, FILTER
, UNSPEC
, "zone_%s_forward"),
32 C(ANY
, FILTER
, SRC_ACCEPT
, "zone_%s_src_ACCEPT"),
33 C(ANY
, FILTER
, SRC_REJECT
, "zone_%s_src_REJECT"),
34 C(ANY
, FILTER
, SRC_DROP
, "zone_%s_src_DROP"),
36 C(ANY
, FILTER
, ACCEPT
, "zone_%s_dest_ACCEPT"),
37 C(ANY
, FILTER
, REJECT
, "zone_%s_dest_REJECT"),
38 C(ANY
, FILTER
, DROP
, "zone_%s_dest_DROP"),
40 C(V4
, NAT
, SNAT
, "zone_%s_postrouting"),
41 C(V4
, NAT
, DNAT
, "zone_%s_prerouting"),
43 C(ANY
, RAW
, HELPER
, "zone_%s_helper"),
44 C(ANY
, RAW
, NOTRACK
, "zone_%s_notrack"),
46 C(ANY
, FILTER
, CUSTOM_CHAINS
, "input_%s_rule"),
47 C(ANY
, FILTER
, CUSTOM_CHAINS
, "output_%s_rule"),
48 C(ANY
, FILTER
, CUSTOM_CHAINS
, "forwarding_%s_rule"),
50 C(V4
, NAT
, CUSTOM_CHAINS
, "prerouting_%s_rule"),
51 C(V4
, NAT
, CUSTOM_CHAINS
, "postrouting_%s_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 print_zone_chain(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
332 bool reload
, struct fw3_zone
*zone
)
335 struct fw3_ipt_rule
*r
;
336 const struct fw3_chain_spec
*c
;
338 const char *flt_chains
[] = {
341 "forward", "forwarding",
344 const char *nat_chains
[] = {
345 "prerouting", "prerouting",
346 "postrouting", "postrouting",
349 if (!fw3_is_family(zone
, handle
->family
))
352 set(zone
->flags
, handle
->family
, handle
->table
);
354 if (zone
->custom_chains
)
355 set(zone
->flags
, handle
->family
, FW3_FLAG_CUSTOM_CHAINS
);
357 for (c
= zone_chains
; c
->format
; c
++)
359 if (!fw3_is_family(c
, handle
->family
))
362 if (c
->table
!= handle
->table
)
366 !fw3_hasbit(zone
->flags
[handle
->family
== FW3_FAMILY_V6
], c
->flag
))
369 fw3_ipt_create_chain(handle
, reload
, c
->format
, zone
->name
);
372 if (zone
->custom_chains
)
374 if (handle
->table
== FW3_TABLE_FILTER
)
376 for (i
= 0; i
< sizeof(flt_chains
)/sizeof(flt_chains
[0]); i
+= 2)
378 r
= fw3_ipt_rule_new(handle
);
379 fw3_ipt_rule_comment(r
, "Custom %s %s rule chain", zone
->name
, flt_chains
[i
+1]);
380 fw3_ipt_rule_target(r
, "%s_%s_rule", flt_chains
[i
+1], zone
->name
);
381 fw3_ipt_rule_append(r
, "zone_%s_%s", zone
->name
, flt_chains
[i
]);
384 else if (handle
->table
== FW3_TABLE_NAT
)
386 for (i
= 0; i
< sizeof(nat_chains
)/sizeof(nat_chains
[0]); i
+= 2)
388 r
= fw3_ipt_rule_new(handle
);
389 fw3_ipt_rule_comment(r
, "Custom %s %s rule chain", zone
->name
, nat_chains
[i
+1]);
390 fw3_ipt_rule_target(r
, "%s_%s_rule", nat_chains
[i
+1], zone
->name
);
391 fw3_ipt_rule_append(r
, "zone_%s_%s", zone
->name
, nat_chains
[i
]);
396 set(zone
->flags
, handle
->family
, handle
->table
);
400 print_interface_rule(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
401 bool reload
, struct fw3_zone
*zone
,
402 struct fw3_device
*dev
, struct fw3_address
*sub
)
404 struct fw3_protocol tcp
= { .protocol
= 6 };
405 struct fw3_ipt_rule
*r
;
412 const char *chains
[] = {
415 "forward", "FORWARD",
418 #define jump_target(t) \
419 ((t == FW3_FLAG_REJECT) ? "reject" : fw3_flag_names[t])
421 if (handle
->table
== FW3_TABLE_FILTER
)
423 for (t
= FW3_FLAG_ACCEPT
; t
<= FW3_FLAG_DROP
; t
++)
425 if (t
> FW3_FLAG_ACCEPT
&& zone
->log
& FW3_ZONE_LOG_FILTER
)
427 if (has(zone
->flags
, handle
->family
, fw3_to_src_target(t
)))
429 r
= fw3_ipt_rule_create(handle
, NULL
, dev
, NULL
, sub
, NULL
);
431 snprintf(buf
, sizeof(buf
) - 1, "%s %s in: ",
432 fw3_flag_names
[t
], zone
->name
);
434 fw3_ipt_rule_limit(r
, &zone
->log_limit
);
435 fw3_ipt_rule_target(r
, "LOG");
436 fw3_ipt_rule_addarg(r
, false, "--log-prefix", buf
);
437 fw3_ipt_rule_replace(r
, "zone_%s_src_%s",
438 zone
->name
, fw3_flag_names
[t
]);
441 if (has(zone
->flags
, handle
->family
, t
))
443 r
= fw3_ipt_rule_create(handle
, NULL
, NULL
, dev
, NULL
, sub
);
445 snprintf(buf
, sizeof(buf
) - 1, "%s %s out: ",
446 fw3_flag_names
[t
], zone
->name
);
448 fw3_ipt_rule_limit(r
, &zone
->log_limit
);
449 fw3_ipt_rule_target(r
, "LOG");
450 fw3_ipt_rule_addarg(r
, false, "--log-prefix", buf
);
451 fw3_ipt_rule_replace(r
, "zone_%s_dest_%s",
452 zone
->name
, fw3_flag_names
[t
]);
456 if (has(zone
->flags
, handle
->family
, fw3_to_src_target(t
)))
458 r
= fw3_ipt_rule_create(handle
, NULL
, dev
, NULL
, sub
, NULL
);
459 fw3_ipt_rule_target(r
, jump_target(t
));
460 fw3_ipt_rule_extra(r
, zone
->extra_src
);
462 if (t
== FW3_FLAG_ACCEPT
&& !state
->defaults
.drop_invalid
)
463 fw3_ipt_rule_extra(r
,
464 "-m conntrack --ctstate NEW,UNTRACKED");
466 fw3_ipt_rule_replace(r
, "zone_%s_src_%s", zone
->name
,
470 if (has(zone
->flags
, handle
->family
, t
))
472 if (t
== FW3_FLAG_ACCEPT
&&
473 zone
->masq
&& !zone
->masq_allow_invalid
)
475 r
= fw3_ipt_rule_create(handle
, NULL
, NULL
, dev
, NULL
, sub
);
476 fw3_ipt_rule_extra(r
, "-m conntrack --ctstate INVALID");
477 fw3_ipt_rule_comment(r
, "Prevent NAT leakage");
478 fw3_ipt_rule_target(r
, fw3_flag_names
[FW3_FLAG_DROP
]);
479 fw3_ipt_rule_replace(r
, "zone_%s_dest_%s", zone
->name
,
483 r
= fw3_ipt_rule_create(handle
, NULL
, NULL
, dev
, NULL
, sub
);
484 fw3_ipt_rule_target(r
, jump_target(t
));
485 fw3_ipt_rule_extra(r
, zone
->extra_dest
);
486 fw3_ipt_rule_replace(r
, "zone_%s_dest_%s", zone
->name
,
491 for (i
= 0; i
< sizeof(chains
)/sizeof(chains
[0]); i
+= 2)
493 if (*chains
[i
] == 'o')
494 r
= fw3_ipt_rule_create(handle
, NULL
, NULL
, dev
, NULL
, sub
);
496 r
= fw3_ipt_rule_create(handle
, NULL
, dev
, NULL
, sub
, NULL
);
498 fw3_ipt_rule_target(r
, "zone_%s_%s", zone
->name
, chains
[i
]);
500 if (*chains
[i
] == 'o')
501 fw3_ipt_rule_extra(r
, zone
->extra_dest
);
503 fw3_ipt_rule_extra(r
, zone
->extra_src
);
505 fw3_ipt_rule_replace(r
, chains
[i
+ 1]);
508 else if (handle
->table
== FW3_TABLE_NAT
)
510 if (has(zone
->flags
, handle
->family
, FW3_FLAG_DNAT
))
512 r
= fw3_ipt_rule_create(handle
, NULL
, dev
, NULL
, sub
, NULL
);
513 fw3_ipt_rule_target(r
, "zone_%s_prerouting", zone
->name
);
514 fw3_ipt_rule_extra(r
, zone
->extra_src
);
515 fw3_ipt_rule_replace(r
, "PREROUTING");
518 if (has(zone
->flags
, handle
->family
, FW3_FLAG_SNAT
))
520 r
= fw3_ipt_rule_create(handle
, NULL
, NULL
, dev
, NULL
, sub
);
521 fw3_ipt_rule_target(r
, "zone_%s_postrouting", zone
->name
);
522 fw3_ipt_rule_extra(r
, zone
->extra_dest
);
523 fw3_ipt_rule_replace(r
, "POSTROUTING");
526 else if (handle
->table
== FW3_TABLE_MANGLE
)
530 if (zone
->log
& FW3_ZONE_LOG_MANGLE
)
532 snprintf(buf
, sizeof(buf
) - 1, "MSSFIX %s out: ", zone
->name
);
534 r
= fw3_ipt_rule_create(handle
, &tcp
, NULL
, dev
, NULL
, sub
);
535 fw3_ipt_rule_addarg(r
, false, "--tcp-flags", "SYN,RST");
536 fw3_ipt_rule_addarg(r
, false, "SYN", NULL
);
537 fw3_ipt_rule_limit(r
, &zone
->log_limit
);
538 fw3_ipt_rule_comment(r
, "Zone %s MTU fix logging", zone
->name
);
539 fw3_ipt_rule_target(r
, "LOG");
540 fw3_ipt_rule_addarg(r
, false, "--log-prefix", buf
);
541 fw3_ipt_rule_replace(r
, "FORWARD");
544 r
= fw3_ipt_rule_create(handle
, &tcp
, NULL
, dev
, NULL
, sub
);
545 fw3_ipt_rule_addarg(r
, false, "--tcp-flags", "SYN,RST");
546 fw3_ipt_rule_addarg(r
, false, "SYN", NULL
);
547 fw3_ipt_rule_comment(r
, "Zone %s MTU fixing", zone
->name
);
548 fw3_ipt_rule_target(r
, "TCPMSS");
549 fw3_ipt_rule_addarg(r
, false, "--clamp-mss-to-pmtu", NULL
);
550 fw3_ipt_rule_replace(r
, "FORWARD");
553 else if (handle
->table
== FW3_TABLE_RAW
)
555 bool loopback_dev
= (dev
!= NULL
&& !dev
->any
&&
556 !dev
->invert
&& fw3_check_loopback_dev(dev
->name
));
557 char *chain
= loopback_dev
|| (sub
!= NULL
&& !sub
->invert
&& fw3_check_loopback_addr(sub
)) ?
558 "OUTPUT" : "PREROUTING";
560 if (has(zone
->flags
, handle
->family
, FW3_FLAG_HELPER
))
562 r
= fw3_ipt_rule_create(handle
, NULL
, loopback_dev
? NULL
: dev
, NULL
, sub
, NULL
);
563 fw3_ipt_rule_comment(r
, "%s CT helper assignment", zone
->name
);
564 fw3_ipt_rule_target(r
, "zone_%s_helper", zone
->name
);
565 fw3_ipt_rule_extra(r
, zone
->extra_src
);
566 fw3_ipt_rule_replace(r
, chain
);
569 if (has(zone
->flags
, handle
->family
, FW3_FLAG_NOTRACK
))
571 r
= fw3_ipt_rule_create(handle
, NULL
, loopback_dev
? NULL
: dev
, NULL
, sub
, NULL
);
572 fw3_ipt_rule_comment(r
, "%s CT bypass", zone
->name
);
573 fw3_ipt_rule_target(r
, "zone_%s_notrack", zone
->name
);
574 fw3_ipt_rule_extra(r
, zone
->extra_src
);
575 fw3_ipt_rule_replace(r
, chain
);
581 print_interface_rules(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
582 bool reload
, struct fw3_zone
*zone
)
584 struct fw3_device
*dev
;
585 struct fw3_address
*sub
;
587 fw3_foreach(dev
, &zone
->devices
)
588 fw3_foreach(sub
, &zone
->subnets
)
590 if (!fw3_is_family(sub
, handle
->family
))
593 if (!dev
&& !sub
&& !zone
->extra_src
&& !zone
->extra_dest
)
596 print_interface_rule(handle
, state
, reload
, zone
, dev
, sub
);
600 static struct fw3_address
*
601 next_addr(struct fw3_address
*addr
, struct list_head
*list
,
602 enum fw3_family family
, bool invert
)
605 struct fw3_address
*rv
;
607 for (p
= addr
? addr
->list
.next
: list
->next
; p
!= list
; p
= p
->next
)
609 rv
= list_entry(p
, struct fw3_address
, list
);
611 if (fw3_is_family(rv
, family
) && rv
->set
&& rv
->invert
== invert
)
619 print_zone_rule(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
620 bool reload
, struct fw3_zone
*zone
)
622 bool first_src
, first_dest
;
623 struct fw3_address
*msrc
;
624 struct fw3_address
*mdest
;
625 struct fw3_ipt_rule
*r
;
627 if (!fw3_is_family(zone
, handle
->family
))
630 info(" * Zone '%s'", zone
->name
);
632 switch (handle
->table
)
634 case FW3_TABLE_FILTER
:
635 if (has(zone
->flags
, handle
->family
, FW3_FLAG_DNAT
))
637 r
= fw3_ipt_rule_new(handle
);
638 fw3_ipt_rule_extra(r
, "-m conntrack --ctstate DNAT");
639 fw3_ipt_rule_comment(r
, "Accept port redirections");
640 fw3_ipt_rule_target(r
, fw3_flag_names
[FW3_FLAG_ACCEPT
]);
641 fw3_ipt_rule_append(r
, "zone_%s_input", zone
->name
);
643 r
= fw3_ipt_rule_new(handle
);
644 fw3_ipt_rule_extra(r
, "-m conntrack --ctstate DNAT");
645 fw3_ipt_rule_comment(r
, "Accept port forwards");
646 fw3_ipt_rule_target(r
, fw3_flag_names
[FW3_FLAG_ACCEPT
]);
647 fw3_ipt_rule_append(r
, "zone_%s_forward", zone
->name
);
650 r
= fw3_ipt_rule_new(handle
);
651 fw3_ipt_rule_target(r
, "zone_%s_src_%s", zone
->name
,
652 fw3_flag_names
[zone
->policy_input
]);
653 fw3_ipt_rule_append(r
, "zone_%s_input", zone
->name
);
655 r
= fw3_ipt_rule_new(handle
);
656 fw3_ipt_rule_target(r
, "zone_%s_dest_%s", zone
->name
,
657 fw3_flag_names
[zone
->policy_forward
]);
658 fw3_ipt_rule_append(r
, "zone_%s_forward", zone
->name
);
660 r
= fw3_ipt_rule_new(handle
);
661 fw3_ipt_rule_target(r
, "zone_%s_dest_%s", zone
->name
,
662 fw3_flag_names
[zone
->policy_output
]);
663 fw3_ipt_rule_append(r
, "zone_%s_output", zone
->name
);
668 if (zone
->masq
&& handle
->family
== FW3_FAMILY_V4
)
670 /* for any negated masq_src ip, emit -s addr -j RETURN rules */
672 (msrc
= next_addr(msrc
, &zone
->masq_src
,
673 handle
->family
, true)) != NULL
; )
675 msrc
->invert
= false;
676 r
= fw3_ipt_rule_new(handle
);
677 fw3_ipt_rule_src_dest(r
, msrc
, NULL
);
678 fw3_ipt_rule_target(r
, "RETURN");
679 fw3_ipt_rule_append(r
, "zone_%s_postrouting", zone
->name
);
683 /* for any negated masq_dest ip, emit -d addr -j RETURN rules */
685 (mdest
= next_addr(mdest
, &zone
->masq_dest
,
686 handle
->family
, true)) != NULL
; )
688 mdest
->invert
= false;
689 r
= fw3_ipt_rule_new(handle
);
690 fw3_ipt_rule_src_dest(r
, NULL
, mdest
);
691 fw3_ipt_rule_target(r
, "RETURN");
692 fw3_ipt_rule_append(r
, "zone_%s_postrouting", zone
->name
);
693 mdest
->invert
= true;
696 /* emit masquerading entries for non-negated addresses
697 and ensure that both src and dest loops run at least once,
698 even if there are no relevant addresses */
699 for (first_src
= true, msrc
= NULL
;
700 (msrc
= next_addr(msrc
, &zone
->masq_src
,
701 handle
->family
, false)) || first_src
;
704 for (first_dest
= true, mdest
= NULL
;
705 (mdest
= next_addr(mdest
, &zone
->masq_dest
,
706 handle
->family
, false)) || first_dest
;
709 r
= fw3_ipt_rule_new(handle
);
710 fw3_ipt_rule_src_dest(r
, msrc
, mdest
);
711 fw3_ipt_rule_target(r
, "MASQUERADE");
712 fw3_ipt_rule_append(r
, "zone_%s_postrouting", zone
->name
);
719 fw3_print_cthelpers(handle
, state
, zone
);
722 case FW3_TABLE_MANGLE
:
726 print_interface_rules(handle
, state
, reload
, zone
);
730 fw3_print_zone_chains(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
733 struct fw3_zone
*zone
;
735 list_for_each_entry(zone
, &state
->zones
, list
)
736 print_zone_chain(handle
, state
, reload
, zone
);
740 fw3_print_zone_rules(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
743 struct fw3_zone
*zone
;
745 list_for_each_entry(zone
, &state
->zones
, list
)
746 print_zone_rule(handle
, state
, reload
, zone
);
750 fw3_flush_zones(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
753 struct fw3_zone
*z
, *tmp
;
754 const struct fw3_chain_spec
*c
;
757 list_for_each_entry_safe(z
, tmp
, &state
->zones
, list
)
759 if (!has(z
->flags
, handle
->family
, handle
->table
))
762 /* first flush all rules ... */
763 for (c
= zone_chains
; c
->format
; c
++)
765 /* don't touch user chains on selective stop */
766 if (reload
&& c
->flag
== FW3_FLAG_CUSTOM_CHAINS
)
769 if (!fw3_is_family(c
, handle
->family
))
772 if (c
->table
!= handle
->table
)
775 if (c
->flag
&& !has(z
->flags
, handle
->family
, c
->flag
))
778 snprintf(chain
, sizeof(chain
), c
->format
, z
->name
);
779 fw3_ipt_flush_chain(handle
, chain
);
782 /* ... then remove the chains */
783 for (c
= zone_chains
; c
->format
; c
++)
785 if (!fw3_is_family(c
, handle
->family
))
788 if (c
->table
!= handle
->table
)
791 if (c
->flag
&& !has(z
->flags
, handle
->family
, c
->flag
))
794 snprintf(chain
, sizeof(chain
), c
->format
, z
->name
);
796 fw3_ipt_delete_chain(handle
, reload
, chain
);
799 del(z
->flags
, handle
->family
, handle
->table
);
804 fw3_hotplug_zones(struct fw3_state
*state
, bool add
)
807 struct fw3_device
*d
;
809 list_for_each_entry(z
, &state
->zones
, list
)
811 if (add
!= fw3_hasbit(z
->flags
[0], FW3_FLAG_HOTPLUG
))
813 list_for_each_entry(d
, &z
->devices
, list
)
814 fw3_hotplug(add
, z
, d
);
817 fw3_setbit(z
->flags
[0], FW3_FLAG_HOTPLUG
);
819 fw3_delbit(z
->flags
[0], FW3_FLAG_HOTPLUG
);
825 fw3_lookup_zone(struct fw3_state
*state
, const char *name
)
829 if (list_empty(&state
->zones
))
832 list_for_each_entry(z
, &state
->zones
, list
)
834 if (strcmp(z
->name
, name
))
844 fw3_resolve_zone_addresses(struct fw3_zone
*zone
, struct fw3_address
*addr
)
846 struct fw3_device
*net
;
847 struct fw3_address
*cur
, *tmp
;
848 struct list_head
*all
;
850 all
= calloc(1, sizeof(*all
));
856 if (addr
&& addr
->set
)
858 tmp
= malloc(sizeof(*tmp
));
863 list_add_tail(&tmp
->list
, all
);
868 list_for_each_entry(net
, &zone
->networks
, list
)
869 fw3_ubus_address(all
, net
->name
);
871 list_for_each_entry(cur
, &zone
->subnets
, list
)
873 tmp
= malloc(sizeof(*tmp
));
879 list_add_tail(&tmp
->list
, all
);