2 * firewall3 - 3rd OpenWrt UCI firewall implementation
4 * Copyright (C) 2013-2018 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.
22 const struct fw3_option fw3_rule_opts
[] = {
23 FW3_OPT("enabled", bool, rule
, enabled
),
25 FW3_OPT("name", string
, rule
, name
),
26 FW3_OPT("family", family
, rule
, family
),
28 FW3_OPT("src", device
, rule
, src
),
29 FW3_OPT("dest", device
, rule
, dest
),
31 FW3_OPT("device", string
, rule
, device
),
32 FW3_OPT("direction", direction
, rule
, direction_out
),
34 FW3_OPT("ipset", setmatch
, rule
, ipset
),
35 FW3_OPT("helper", cthelper
, rule
, helper
),
36 FW3_OPT("set_helper", cthelper
, rule
, set_helper
),
38 FW3_LIST("proto", protocol
, rule
, proto
),
40 FW3_LIST("src_ip", network
, rule
, ip_src
),
41 FW3_LIST("src_mac", mac
, rule
, mac_src
),
42 FW3_LIST("src_port", port
, rule
, port_src
),
44 FW3_LIST("dest_ip", network
, rule
, ip_dest
),
45 FW3_LIST("dest_port", port
, rule
, port_dest
),
47 FW3_LIST("icmp_type", icmptype
, rule
, icmp_type
),
48 FW3_OPT("extra", string
, rule
, extra
),
50 FW3_OPT("limit", limit
, rule
, limit
),
51 FW3_OPT("limit_burst", int, rule
, limit
.burst
),
53 FW3_OPT("utc_time", bool, rule
, time
.utc
),
54 FW3_OPT("start_date", date
, rule
, time
.datestart
),
55 FW3_OPT("stop_date", date
, rule
, time
.datestop
),
56 FW3_OPT("start_time", time
, rule
, time
.timestart
),
57 FW3_OPT("stop_time", time
, rule
, time
.timestop
),
58 FW3_OPT("weekdays", weekdays
, rule
, time
.weekdays
),
59 FW3_OPT("monthdays", monthdays
, rule
, time
.monthdays
),
61 FW3_OPT("mark", mark
, rule
, mark
),
62 FW3_OPT("set_mark", mark
, rule
, set_mark
),
63 FW3_OPT("set_xmark", mark
, rule
, set_xmark
),
65 FW3_OPT("dscp", dscp
, rule
, dscp
),
66 FW3_OPT("set_dscp", dscp
, rule
, set_dscp
),
68 FW3_OPT("target", target
, rule
, target
),
75 need_src_action_chain(struct fw3_rule
*r
)
77 return (r
->_src
&& r
->_src
->log
&& (r
->target
> FW3_FLAG_ACCEPT
));
80 static struct fw3_rule
*
81 alloc_rule(struct fw3_state
*state
)
83 struct fw3_rule
*rule
= calloc(1, sizeof(*rule
));
86 INIT_LIST_HEAD(&rule
->proto
);
88 INIT_LIST_HEAD(&rule
->ip_src
);
89 INIT_LIST_HEAD(&rule
->mac_src
);
90 INIT_LIST_HEAD(&rule
->port_src
);
92 INIT_LIST_HEAD(&rule
->ip_dest
);
93 INIT_LIST_HEAD(&rule
->port_dest
);
95 INIT_LIST_HEAD(&rule
->icmp_type
);
97 list_add_tail(&rule
->list
, &state
->rules
);
105 check_rule(struct fw3_state
*state
, struct fw3_rule
*r
, struct uci_element
*e
)
110 if (r
->src
.invert
|| r
->dest
.invert
)
112 warn_section("rule", r
, e
, "must not have inverted 'src' or 'dest' options");
115 else if (r
->src
.set
&& !r
->src
.any
&&
116 !(r
->_src
= fw3_lookup_zone(state
, r
->src
.name
)))
118 warn_section("rule", r
, e
, "refers to not existing zone '%s'", r
->src
.name
);
121 else if (r
->dest
.set
&& !r
->dest
.any
&&
122 !(r
->_dest
= fw3_lookup_zone(state
, r
->dest
.name
)))
124 warn_section("rule", r
, e
, "refers to not existing zone '%s'", r
->dest
.name
);
127 else if (r
->ipset
.set
&& state
->disable_ipsets
)
129 warn_section("rule", r
, e
, "skipped due to disabled ipset support");
132 else if (r
->ipset
.set
&&
133 !(r
->ipset
.ptr
= fw3_lookup_ipset(state
, r
->ipset
.name
)))
135 warn_section("rule", r
, e
, "refers to unknown ipset '%s'", r
->ipset
.name
);
138 else if (r
->helper
.set
&&
139 !(r
->helper
.ptr
= fw3_lookup_cthelper(state
, r
->helper
.name
)))
141 warn_section("rule", r
, e
, "refers to unknown CT helper '%s'", r
->helper
.name
);
144 else if (r
->set_helper
.set
&&
145 !(r
->set_helper
.ptr
= fw3_lookup_cthelper(state
, r
->set_helper
.name
)))
147 warn_section("rule", r
, e
, "refers to unknown CT helper '%s'", r
->set_helper
.name
);
151 if (!r
->_src
&& (r
->target
== FW3_FLAG_NOTRACK
|| r
->target
== FW3_FLAG_HELPER
))
153 warn_section("rule", r
, e
, "is set to target %s but has no source assigned",
154 fw3_flag_names
[r
->target
]);
158 if (!r
->set_mark
.set
&& !r
->set_xmark
.set
&&
159 r
->target
== FW3_FLAG_MARK
)
161 warn_section("rule", r
, e
, "is set to target MARK but specifies neither "
162 "'set_mark' nor 'set_xmark' option");
166 if (!r
->set_dscp
.set
&& r
->target
== FW3_FLAG_DSCP
)
168 warn_section("rule", r
, e
, "is set to target DSCP but specifies no "
169 "'set_dscp' option");
173 if (r
->set_mark
.invert
|| r
->set_xmark
.invert
|| r
->set_dscp
.invert
)
175 warn_section("rule", r
, e
, "must not have inverted 'set_mark', "
176 "'set_xmark' or 'set_dscp'");
180 if (!r
->set_helper
.set
&& r
->target
== FW3_FLAG_HELPER
)
182 warn_section("rule", r
, e
, "is set to target HELPER but specifies "
183 "no 'set_helper' option");
187 if (r
->set_helper
.invert
&& r
->target
== FW3_FLAG_HELPER
)
189 warn_section("rule", r
, e
, "must not have inverted 'set_helper' option");
193 if (!r
->_src
&& !r
->_dest
&& !r
->src
.any
&& !r
->dest
.any
)
195 warn_section("rule", r
, e
, "has neither a source nor a destination zone assigned "
196 "- assuming an output rule");
199 if (list_empty(&r
->proto
))
201 warn_section("rule", r
, e
, "does not specify a protocol, assuming TCP+UDP");
202 fw3_parse_protocol(&r
->proto
, "tcpudp", true);
205 if (r
->target
== FW3_FLAG_UNSPEC
)
207 warn_section("rule", r
, e
, "has no target specified, defaulting to REJECT");
208 r
->target
= FW3_FLAG_REJECT
;
210 else if (r
->target
> FW3_FLAG_DSCP
)
212 warn_section("rule", r
, e
, "has invalid target specified, defaulting to REJECT");
213 r
->target
= FW3_FLAG_REJECT
;
216 /* NB: r family... */
219 fw3_setbit(r
->_dest
->flags
[0], r
->target
);
220 fw3_setbit(r
->_dest
->flags
[1], r
->target
);
222 else if (need_src_action_chain(r
))
224 fw3_setbit(r
->_src
->flags
[0], fw3_to_src_target(r
->target
));
225 fw3_setbit(r
->_src
->flags
[1], fw3_to_src_target(r
->target
));
232 fw3_load_rules(struct fw3_state
*state
, struct uci_package
*p
,
235 struct uci_section
*s
;
236 struct uci_element
*e
;
237 struct fw3_rule
*rule
;
238 struct blob_attr
*entry
;
241 INIT_LIST_HEAD(&state
->rules
);
243 blob_for_each_attr(entry
, a
, rem
) {
245 const char *name
= "ubus rule";
247 if (!fw3_attr_parse_name_type(entry
, &name
, &type
))
250 if (strcmp(type
, "rule"))
253 if (!(rule
= alloc_rule(state
)))
256 if (!fw3_parse_blob_options(rule
, fw3_rule_opts
, entry
, name
))
258 warn_section("rule", rule
, NULL
, "skipped due to invalid options");
263 if (!check_rule(state
, rule
, NULL
))
267 uci_foreach_element(&p
->sections
, e
)
269 s
= uci_to_section(e
);
271 if (strcmp(s
->type
, "rule"))
274 if (!(rule
= alloc_rule(state
)))
277 if (!fw3_parse_options(rule
, fw3_rule_opts
, s
))
279 warn_elem(e
, "skipped due to invalid options");
284 if (!check_rule(state
, rule
, e
))
291 append_chain(struct fw3_ipt_rule
*r
, struct fw3_rule
*rule
)
295 snprintf(chain
, sizeof(chain
), "OUTPUT");
297 if (rule
->target
== FW3_FLAG_NOTRACK
)
299 snprintf(chain
, sizeof(chain
), "zone_%s_notrack", rule
->src
.name
);
301 else if (rule
->target
== FW3_FLAG_HELPER
)
303 snprintf(chain
, sizeof(chain
), "zone_%s_helper", rule
->src
.name
);
305 else if (rule
->target
== FW3_FLAG_MARK
|| rule
->target
== FW3_FLAG_DSCP
)
307 if ((rule
->_dest
&& rule
->_src
) ||
308 (rule
->dest
.any
&& rule
->src
.any
))
309 snprintf(chain
, sizeof(chain
), "FORWARD");
310 else if (rule
->src
.any
&& rule
->_dest
)
311 snprintf(chain
, sizeof(chain
), "POSTROUTING");
312 else if (rule
->dest
.any
&& rule
->_src
)
313 snprintf(chain
, sizeof(chain
), "PREROUTING");
314 else if (!rule
->dest
.set
&& rule
->src
.set
)
315 snprintf(chain
, sizeof(chain
), "INPUT");
316 else /* if (!rule->src.set) */
317 snprintf(chain
, sizeof(chain
), "OUTPUT");
326 snprintf(chain
, sizeof(chain
), "zone_%s_forward",
329 snprintf(chain
, sizeof(chain
), "zone_%s_input",
335 snprintf(chain
, sizeof(chain
), "FORWARD");
337 snprintf(chain
, sizeof(chain
), "INPUT");
341 if (rule
->dest
.set
&& !rule
->src
.set
)
344 snprintf(chain
, sizeof(chain
), "OUTPUT");
346 snprintf(chain
, sizeof(chain
), "zone_%s_output",
351 fw3_ipt_rule_append(r
, chain
);
354 static void set_target(struct fw3_ipt_rule
*r
, struct fw3_rule
*rule
)
357 struct fw3_mark
*mark
;
358 char buf
[sizeof("0xFFFFFFFF/0xFFFFFFFF")];
363 name
= rule
->set_mark
.set
? "--set-mark" : "--set-xmark";
364 mark
= rule
->set_mark
.set
? &rule
->set_mark
: &rule
->set_xmark
;
365 snprintf(buf
, sizeof(buf
), "0x%x/0x%x", mark
->mark
, mark
->mask
);
367 fw3_ipt_rule_target(r
, "MARK");
368 fw3_ipt_rule_addarg(r
, false, name
, buf
);
372 snprintf(buf
, sizeof(buf
), "0x%x", rule
->set_dscp
.dscp
);
374 fw3_ipt_rule_target(r
, "DSCP");
375 fw3_ipt_rule_addarg(r
, false, "--set-dscp", buf
);
378 case FW3_FLAG_NOTRACK
:
379 fw3_ipt_rule_target(r
, "CT");
380 fw3_ipt_rule_addarg(r
, false, "--notrack", NULL
);
383 case FW3_FLAG_HELPER
:
384 fw3_ipt_rule_target(r
, "CT");
385 fw3_ipt_rule_addarg(r
, false, "--helper", rule
->set_helper
.ptr
->name
);
388 case FW3_FLAG_ACCEPT
:
390 name
= fw3_flag_names
[rule
->target
];
394 name
= fw3_flag_names
[FW3_FLAG_REJECT
];
398 if (rule
->dest
.set
&& !rule
->dest
.any
)
399 fw3_ipt_rule_target(r
, "zone_%s_dest_%s", rule
->dest
.name
, name
);
400 else if (need_src_action_chain(rule
))
401 fw3_ipt_rule_target(r
, "zone_%s_src_%s", rule
->src
.name
, name
);
402 else if (strcmp(name
, "REJECT"))
403 fw3_ipt_rule_target(r
, name
);
405 fw3_ipt_rule_target(r
, "reject");
409 set_comment(struct fw3_ipt_rule
*r
, const char *name
, int num
)
412 fw3_ipt_rule_comment(r
, name
);
414 fw3_ipt_rule_comment(r
, "@rule[%u]", num
);
418 print_rule(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
419 struct fw3_rule
*rule
, int num
, struct fw3_protocol
*proto
,
420 struct fw3_address
*sip
, struct fw3_address
*dip
,
421 struct fw3_port
*sport
, struct fw3_port
*dport
,
422 struct fw3_mac
*mac
, struct fw3_icmptype
*icmptype
)
424 struct fw3_ipt_rule
*r
;
425 struct fw3_device
*idev
, *odev
;
426 struct list_head empty
, *idevices
, *odevices
;
427 INIT_LIST_HEAD(&empty
);
428 idevices
= odevices
= &empty
;
430 if (!fw3_is_family(sip
, handle
->family
) ||
431 !fw3_is_family(dip
, handle
->family
))
433 if ((sip
&& !sip
->resolved
) || (dip
&& !dip
->resolved
))
434 info(" ! Skipping due to different family of ip address");
439 if (!fw3_is_family(sip
, handle
->family
) ||
440 !fw3_is_family(dip
, handle
->family
))
442 if ((sip
&& !sip
->resolved
) || (dip
&& !dip
->resolved
))
443 info(" ! Skipping due to different family of ip address");
448 if (!fw3_is_family(sip
, handle
->family
) ||
449 !fw3_is_family(dip
, handle
->family
))
451 if ((sip
&& !sip
->resolved
) || (dip
&& !dip
->resolved
))
452 info(" ! Skipping due to different family of ip address");
457 if (proto
->protocol
== 58 && handle
->family
== FW3_FAMILY_V4
)
459 info(" ! Skipping protocol %s due to different family",
460 fw3_protoname(proto
));
464 if (rule
->helper
.ptr
&&
465 !fw3_cthelper_check_proto(rule
->helper
.ptr
, proto
))
467 info(" ! Skipping protocol %s since helper '%s' does not support it",
468 fw3_protoname(proto
), rule
->helper
.ptr
->name
);
472 if (rule
->set_helper
.ptr
&&
473 !fw3_cthelper_check_proto(rule
->set_helper
.ptr
, proto
))
475 info(" ! Skipping protocol %s since helper '%s' does not support it",
476 fw3_protoname(proto
), rule
->helper
.ptr
->name
);
480 if (rule
->target
== FW3_FLAG_DSCP
|| rule
->target
== FW3_FLAG_MARK
)
483 idevices
= &rule
->_src
->devices
;
485 odevices
= &rule
->_dest
->devices
;
488 fw3_foreach(idev
, idevices
)
489 fw3_foreach(odev
, odevices
)
491 r
= fw3_ipt_rule_create(handle
, proto
, idev
, odev
, sip
, dip
);
492 fw3_ipt_rule_sport_dport(r
, sport
, dport
);
493 fw3_ipt_rule_device(r
, rule
->device
, rule
->direction_out
);
494 fw3_ipt_rule_icmptype(r
, icmptype
);
495 fw3_ipt_rule_mac(r
, mac
);
496 fw3_ipt_rule_ipset(r
, &rule
->ipset
);
497 fw3_ipt_rule_helper(r
, &rule
->helper
);
498 fw3_ipt_rule_limit(r
, &rule
->limit
);
499 fw3_ipt_rule_time(r
, &rule
->time
);
500 fw3_ipt_rule_mark(r
, &rule
->mark
);
501 fw3_ipt_rule_dscp(r
, &rule
->dscp
);
503 fw3_ipt_rule_extra(r
, rule
->extra
);
504 set_comment(r
, rule
->name
, num
);
505 append_chain(r
, rule
);
510 expand_rule(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
511 struct fw3_rule
*rule
, int num
)
513 struct fw3_protocol
*proto
;
514 struct fw3_address
*sip
;
515 struct fw3_address
*dip
;
516 struct fw3_port
*sport
;
517 struct fw3_port
*dport
;
519 struct fw3_icmptype
*icmptype
;
521 struct list_head
*sports
= NULL
;
522 struct list_head
*dports
= NULL
;
523 struct list_head
*icmptypes
= NULL
;
525 struct list_head empty
;
526 INIT_LIST_HEAD(&empty
);
528 if (!fw3_is_family(rule
, handle
->family
))
531 if ((rule
->target
== FW3_FLAG_NOTRACK
&& handle
->table
!= FW3_TABLE_RAW
) ||
532 (rule
->target
== FW3_FLAG_HELPER
&& handle
->table
!= FW3_TABLE_RAW
) ||
533 (rule
->target
== FW3_FLAG_MARK
&& handle
->table
!= FW3_TABLE_MANGLE
) ||
534 (rule
->target
== FW3_FLAG_DSCP
&& handle
->table
!= FW3_TABLE_MANGLE
) ||
535 (rule
->target
< FW3_FLAG_NOTRACK
&& handle
->table
!= FW3_TABLE_FILTER
))
539 info(" * Rule '%s'", rule
->name
);
541 info(" * Rule #%u", num
);
543 if (!fw3_is_family(rule
->_src
, handle
->family
) ||
544 !fw3_is_family(rule
->_dest
, handle
->family
))
546 info(" ! Skipping due to different family of zone");
552 if (!fw3_is_family(rule
->ipset
.ptr
, handle
->family
))
554 info(" ! Skipping due to different family in ipset");
558 if (!fw3_check_ipset(rule
->ipset
.ptr
))
560 info(" ! Skipping due to missing ipset '%s'",
561 rule
->ipset
.ptr
->external
562 ? rule
->ipset
.ptr
->external
: rule
->ipset
.ptr
->name
);
566 set(rule
->ipset
.ptr
->flags
, handle
->family
, handle
->family
);
569 if (rule
->helper
.ptr
&& !fw3_is_family(rule
->helper
.ptr
, handle
->family
))
571 info(" ! Skipping due to unsupported family of CT helper");
575 if (rule
->set_helper
.ptr
&& !fw3_is_family(rule
->set_helper
.ptr
, handle
->family
))
577 info(" ! Skipping due to unsupported family of CT helper");
581 list_for_each_entry(proto
, &rule
->proto
, list
)
583 /* icmp / ipv6-icmp */
584 if (proto
->protocol
== 1 || proto
->protocol
== 58)
588 icmptypes
= &rule
->icmp_type
;
592 sports
= &rule
->port_src
;
593 dports
= &rule
->port_dest
;
597 fw3_foreach(sip
, &rule
->ip_src
)
598 fw3_foreach(dip
, &rule
->ip_dest
)
599 fw3_foreach(sport
, sports
)
600 fw3_foreach(dport
, dports
)
601 fw3_foreach(mac
, &rule
->mac_src
)
602 fw3_foreach(icmptype
, icmptypes
)
603 print_rule(handle
, state
, rule
, num
, proto
, sip
, dip
,
604 sport
, dport
, mac
, icmptype
);
609 fw3_print_rules(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
)
612 struct fw3_rule
*rule
;
614 list_for_each_entry(rule
, &state
->rules
, list
)
615 expand_rule(handle
, state
, rule
, num
++);