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
, 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("target", target
, rule
, target
),
72 need_src_action_chain(struct fw3_rule
*r
)
74 return (r
->_src
&& r
->_src
->log
&& (r
->target
> FW3_FLAG_ACCEPT
));
77 static struct fw3_rule
*
78 alloc_rule(struct fw3_state
*state
)
80 struct fw3_rule
*rule
= calloc(1, sizeof(*rule
));
83 INIT_LIST_HEAD(&rule
->proto
);
85 INIT_LIST_HEAD(&rule
->ip_src
);
86 INIT_LIST_HEAD(&rule
->mac_src
);
87 INIT_LIST_HEAD(&rule
->port_src
);
89 INIT_LIST_HEAD(&rule
->ip_dest
);
90 INIT_LIST_HEAD(&rule
->port_dest
);
92 INIT_LIST_HEAD(&rule
->icmp_type
);
94 list_add_tail(&rule
->list
, &state
->rules
);
102 check_rule(struct fw3_state
*state
, struct fw3_rule
*r
, struct uci_element
*e
)
107 if (r
->src
.invert
|| r
->dest
.invert
)
109 warn_section("rule", r
, e
, "must not have inverted 'src' or 'dest' options");
112 else if (r
->src
.set
&& !r
->src
.any
&&
113 !(r
->_src
= fw3_lookup_zone(state
, r
->src
.name
)))
115 warn_section("rule", r
, e
, "refers to not existing zone '%s'", r
->src
.name
);
118 else if (r
->dest
.set
&& !r
->dest
.any
&&
119 !(r
->_dest
= fw3_lookup_zone(state
, r
->dest
.name
)))
121 warn_section("rule", r
, e
, "refers to not existing zone '%s'", r
->dest
.name
);
124 else if (r
->ipset
.set
&& state
->disable_ipsets
)
126 warn_section("rule", r
, e
, "skipped due to disabled ipset support");
129 else if (r
->ipset
.set
&&
130 !(r
->ipset
.ptr
= fw3_lookup_ipset(state
, r
->ipset
.name
)))
132 warn_section("rule", r
, e
, "refers to unknown ipset '%s'", r
->ipset
.name
);
135 else if (r
->helper
.set
&&
136 !(r
->helper
.ptr
= fw3_lookup_cthelper(state
, r
->helper
.name
)))
138 warn_section("rule", r
, e
, "refers to unknown CT helper '%s'", r
->helper
.name
);
141 else if (r
->set_helper
.set
&&
142 !(r
->set_helper
.ptr
= fw3_lookup_cthelper(state
, r
->set_helper
.name
)))
144 warn_section("rule", r
, e
, "refers to unknown CT helper '%s'", r
->set_helper
.name
);
148 if (!r
->_src
&& (r
->target
== FW3_FLAG_NOTRACK
|| r
->target
== FW3_FLAG_HELPER
))
150 warn_section("rule", r
, e
, "is set to target %s but has no source assigned",
151 fw3_flag_names
[r
->target
]);
155 if (!r
->set_mark
.set
&& !r
->set_xmark
.set
&&
156 r
->target
== FW3_FLAG_MARK
)
158 warn_section("rule", r
, e
, "is set to target MARK but specifies neither "
159 "'set_mark' nor 'set_xmark' option");
163 if (r
->_dest
&& r
->target
== FW3_FLAG_MARK
)
165 warn_section("rule", r
, e
, "must not specify 'dest' for MARK target");
169 if (r
->set_mark
.invert
|| r
->set_xmark
.invert
)
171 warn_section("rule", r
, e
, "must not have inverted 'set_mark' or 'set_xmark'");
175 if (!r
->set_helper
.set
&& r
->target
== FW3_FLAG_HELPER
)
177 warn_section("rule", r
, e
, "is set to target HELPER but specifies "
178 "no 'set_helper' option");
182 if (r
->set_helper
.invert
&& r
->target
== FW3_FLAG_HELPER
)
184 warn_section("rule", r
, e
, "must not have inverted 'set_helper' option");
188 if (!r
->_src
&& !r
->_dest
&& !r
->src
.any
&& !r
->dest
.any
)
190 warn_section("rule", r
, e
, "has neither a source nor a destination zone assigned "
191 "- assuming an output r");
194 if (list_empty(&r
->proto
))
196 warn_section("rule", r
, e
, "does not specify a protocol, assuming TCP+UDP");
197 fw3_parse_protocol(&r
->proto
, "tcpudp", true);
200 if (r
->target
== FW3_FLAG_UNSPEC
)
202 warn_section("rule", r
, e
, "has no target specified, defaulting to REJECT");
203 r
->target
= FW3_FLAG_REJECT
;
205 else if (r
->target
> FW3_FLAG_MARK
)
207 warn_section("rule", r
, e
, "has invalid target specified, defaulting to REJECT");
208 r
->target
= FW3_FLAG_REJECT
;
211 /* NB: r family... */
214 fw3_setbit(r
->_dest
->flags
[0], r
->target
);
215 fw3_setbit(r
->_dest
->flags
[1], r
->target
);
217 else if (need_src_action_chain(r
))
219 fw3_setbit(r
->_src
->flags
[0], fw3_to_src_target(r
->target
));
220 fw3_setbit(r
->_src
->flags
[1], fw3_to_src_target(r
->target
));
227 fw3_load_rules(struct fw3_state
*state
, struct uci_package
*p
,
230 struct uci_section
*s
;
231 struct uci_element
*e
;
232 struct fw3_rule
*rule
;
233 struct blob_attr
*entry
;
236 INIT_LIST_HEAD(&state
->rules
);
238 blob_for_each_attr(entry
, a
, rem
) {
240 const char *name
= "ubus rule";
242 if (!fw3_attr_parse_name_type(entry
, &name
, &type
))
245 if (strcmp(type
, "rule"))
248 if (!(rule
= alloc_rule(state
)))
251 if (!fw3_parse_blob_options(rule
, fw3_rule_opts
, entry
, name
))
253 warn_section("rule", rule
, NULL
, "skipped due to invalid options");
258 if (!check_rule(state
, rule
, NULL
))
262 uci_foreach_element(&p
->sections
, e
)
264 s
= uci_to_section(e
);
266 if (strcmp(s
->type
, "rule"))
269 if (!(rule
= alloc_rule(state
)))
272 if (!fw3_parse_options(rule
, fw3_rule_opts
, s
))
274 warn_elem(e
, "skipped due to invalid options");
279 if (!check_rule(state
, rule
, e
))
286 append_chain(struct fw3_ipt_rule
*r
, struct fw3_rule
*rule
)
290 snprintf(chain
, sizeof(chain
), "OUTPUT");
292 if (rule
->target
== FW3_FLAG_NOTRACK
)
294 snprintf(chain
, sizeof(chain
), "zone_%s_notrack", rule
->src
.name
);
296 else if (rule
->target
== FW3_FLAG_HELPER
)
298 snprintf(chain
, sizeof(chain
), "zone_%s_helper", rule
->src
.name
);
300 else if (rule
->target
== FW3_FLAG_MARK
&& (rule
->_src
|| rule
->src
.any
))
302 snprintf(chain
, sizeof(chain
), "PREROUTING");
311 snprintf(chain
, sizeof(chain
), "zone_%s_forward",
314 snprintf(chain
, sizeof(chain
), "zone_%s_input",
320 snprintf(chain
, sizeof(chain
), "FORWARD");
322 snprintf(chain
, sizeof(chain
), "INPUT");
326 if (rule
->dest
.set
&& !rule
->src
.set
)
329 snprintf(chain
, sizeof(chain
), "OUTPUT");
331 snprintf(chain
, sizeof(chain
), "zone_%s_output",
336 fw3_ipt_rule_append(r
, chain
);
339 static void set_target(struct fw3_ipt_rule
*r
, struct fw3_rule
*rule
)
342 struct fw3_mark
*mark
;
343 char buf
[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
348 name
= rule
->set_mark
.set
? "--set-mark" : "--set-xmark";
349 mark
= rule
->set_mark
.set
? &rule
->set_mark
: &rule
->set_xmark
;
350 sprintf(buf
, "0x%x/0x%x", mark
->mark
, mark
->mask
);
352 fw3_ipt_rule_target(r
, "MARK");
353 fw3_ipt_rule_addarg(r
, false, name
, buf
);
356 case FW3_FLAG_NOTRACK
:
357 fw3_ipt_rule_target(r
, "CT");
358 fw3_ipt_rule_addarg(r
, false, "--notrack", NULL
);
361 case FW3_FLAG_HELPER
:
362 fw3_ipt_rule_target(r
, "CT");
363 fw3_ipt_rule_addarg(r
, false, "--helper", rule
->set_helper
.ptr
->name
);
366 case FW3_FLAG_ACCEPT
:
368 name
= fw3_flag_names
[rule
->target
];
372 name
= fw3_flag_names
[FW3_FLAG_REJECT
];
376 if (rule
->dest
.set
&& !rule
->dest
.any
)
377 fw3_ipt_rule_target(r
, "zone_%s_dest_%s", rule
->dest
.name
, name
);
378 else if (need_src_action_chain(rule
))
379 fw3_ipt_rule_target(r
, "zone_%s_src_%s", rule
->src
.name
, name
);
380 else if (strcmp(name
, "REJECT"))
381 fw3_ipt_rule_target(r
, name
);
383 fw3_ipt_rule_target(r
, "reject");
387 set_comment(struct fw3_ipt_rule
*r
, const char *name
, int num
)
390 fw3_ipt_rule_comment(r
, name
);
392 fw3_ipt_rule_comment(r
, "@rule[%u]", num
);
396 print_rule(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
397 struct fw3_rule
*rule
, int num
, struct fw3_protocol
*proto
,
398 struct fw3_address
*sip
, struct fw3_address
*dip
,
399 struct fw3_port
*sport
, struct fw3_port
*dport
,
400 struct fw3_mac
*mac
, struct fw3_icmptype
*icmptype
)
402 struct fw3_ipt_rule
*r
;
404 if (!fw3_is_family(sip
, handle
->family
) ||
405 !fw3_is_family(dip
, handle
->family
))
407 if ((sip
&& !sip
->resolved
) || (dip
&& !dip
->resolved
))
408 info(" ! Skipping due to different family of ip address");
413 if (!fw3_is_family(sip
, handle
->family
) ||
414 !fw3_is_family(dip
, handle
->family
))
416 if ((sip
&& !sip
->resolved
) || (dip
&& !dip
->resolved
))
417 info(" ! Skipping due to different family of ip address");
422 if (!fw3_is_family(sip
, handle
->family
) ||
423 !fw3_is_family(dip
, handle
->family
))
425 if ((sip
&& !sip
->resolved
) || (dip
&& !dip
->resolved
))
426 info(" ! Skipping due to different family of ip address");
431 if (proto
->protocol
== 58 && handle
->family
== FW3_FAMILY_V4
)
433 info(" ! Skipping protocol %s due to different family",
434 fw3_protoname(proto
));
438 if (rule
->helper
.ptr
&&
439 rule
->helper
.ptr
->proto
.protocol
!= proto
->protocol
)
441 info(" ! Skipping protocol %s since helper '%s' does not support it",
442 fw3_protoname(proto
), rule
->helper
.ptr
->name
);
446 if (rule
->set_helper
.ptr
&&
447 rule
->set_helper
.ptr
->proto
.protocol
!= proto
->protocol
)
449 info(" ! Skipping protocol %s since helper '%s' does not support it",
450 fw3_protoname(proto
), rule
->helper
.ptr
->name
);
454 r
= fw3_ipt_rule_create(handle
, proto
, NULL
, NULL
, sip
, dip
);
455 fw3_ipt_rule_sport_dport(r
, sport
, dport
);
456 fw3_ipt_rule_device(r
, rule
->device
, rule
->direction_out
);
457 fw3_ipt_rule_icmptype(r
, icmptype
);
458 fw3_ipt_rule_mac(r
, mac
);
459 fw3_ipt_rule_ipset(r
, &rule
->ipset
);
460 fw3_ipt_rule_helper(r
, &rule
->helper
);
461 fw3_ipt_rule_limit(r
, &rule
->limit
);
462 fw3_ipt_rule_time(r
, &rule
->time
);
463 fw3_ipt_rule_mark(r
, &rule
->mark
);
465 fw3_ipt_rule_extra(r
, rule
->extra
);
466 set_comment(r
, rule
->name
, num
);
467 append_chain(r
, rule
);
471 expand_rule(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
,
472 struct fw3_rule
*rule
, int num
)
474 struct fw3_protocol
*proto
;
475 struct fw3_address
*sip
;
476 struct fw3_address
*dip
;
477 struct fw3_port
*sport
;
478 struct fw3_port
*dport
;
480 struct fw3_icmptype
*icmptype
;
482 struct list_head
*sports
= NULL
;
483 struct list_head
*dports
= NULL
;
484 struct list_head
*icmptypes
= NULL
;
486 struct list_head empty
;
487 INIT_LIST_HEAD(&empty
);
489 if (!fw3_is_family(rule
, handle
->family
))
492 if ((rule
->target
== FW3_FLAG_NOTRACK
&& handle
->table
!= FW3_TABLE_RAW
) ||
493 (rule
->target
== FW3_FLAG_HELPER
&& handle
->table
!= FW3_TABLE_RAW
) ||
494 (rule
->target
== FW3_FLAG_MARK
&& handle
->table
!= FW3_TABLE_MANGLE
) ||
495 (rule
->target
< FW3_FLAG_NOTRACK
&& handle
->table
!= FW3_TABLE_FILTER
))
499 info(" * Rule '%s'", rule
->name
);
501 info(" * Rule #%u", num
);
503 if (!fw3_is_family(rule
->_src
, handle
->family
) ||
504 !fw3_is_family(rule
->_dest
, handle
->family
))
506 info(" ! Skipping due to different family of zone");
512 if (!fw3_is_family(rule
->ipset
.ptr
, handle
->family
))
514 info(" ! Skipping due to different family in ipset");
518 if (!fw3_check_ipset(rule
->ipset
.ptr
))
520 info(" ! Skipping due to missing ipset '%s'",
521 rule
->ipset
.ptr
->external
522 ? rule
->ipset
.ptr
->external
: rule
->ipset
.ptr
->name
);
526 set(rule
->ipset
.ptr
->flags
, handle
->family
, handle
->family
);
529 if (rule
->helper
.ptr
&& !fw3_is_family(rule
->helper
.ptr
, handle
->family
))
531 info(" ! Skipping due to unsupported family of CT helper");
535 if (rule
->set_helper
.ptr
&& !fw3_is_family(rule
->set_helper
.ptr
, handle
->family
))
537 info(" ! Skipping due to unsupported family of CT helper");
541 list_for_each_entry(proto
, &rule
->proto
, list
)
543 /* icmp / ipv6-icmp */
544 if (proto
->protocol
== 1 || proto
->protocol
== 58)
548 icmptypes
= &rule
->icmp_type
;
552 sports
= &rule
->port_src
;
553 dports
= &rule
->port_dest
;
557 fw3_foreach(sip
, &rule
->ip_src
)
558 fw3_foreach(dip
, &rule
->ip_dest
)
559 fw3_foreach(sport
, sports
)
560 fw3_foreach(dport
, dports
)
561 fw3_foreach(mac
, &rule
->mac_src
)
562 fw3_foreach(icmptype
, icmptypes
)
563 print_rule(handle
, state
, rule
, num
, proto
, sip
, dip
,
564 sport
, dport
, mac
, icmptype
);
569 fw3_print_rules(struct fw3_ipt_handle
*handle
, struct fw3_state
*state
)
572 struct fw3_rule
*rule
;
574 list_for_each_entry(rule
, &state
->rules
, list
)
575 expand_rule(handle
, state
, rule
, num
++);