- if (rule->src.invert || rule->dest.invert)
- {
- warn_elem(e, "must not have inverted 'src' or 'dest' options");
- fw3_free_rule(rule);
- continue;
- }
- else if (rule->src.set && !rule->src.any &&
- !(rule->_src = fw3_lookup_zone(state, rule->src.name, false)))
- {
- warn_elem(e, "refers to not existing zone '%s'", rule->src.name);
- fw3_free_rule(rule);
- continue;
- }
- else if (rule->dest.set && !rule->dest.any &&
- !(rule->_dest = fw3_lookup_zone(state, rule->dest.name, false)))
- {
- warn_elem(e, "refers to not existing zone '%s'", rule->dest.name);
- fw3_free_rule(rule);
- continue;
- }
- else if (rule->ipset.set && state->disable_ipsets)
- {
- warn_elem(e, "skipped due to disabled ipset support");
- fw3_free_rule(rule);
- continue;
- }
- else if (rule->ipset.set && !rule->ipset.any &&
- !(rule->_ipset = fw3_lookup_ipset(state, rule->ipset.name, false)))
- {
- warn_elem(e, "refers to unknown ipset '%s'", rule->ipset.name);
- fw3_free_rule(rule);
+ if (r->src.invert || r->dest.invert)
+ {
+ warn_section("rule", r, e, "must not have inverted 'src' or 'dest' options");
+ return false;
+ }
+ else if (r->src.set && !r->src.any &&
+ !(r->_src = fw3_lookup_zone(state, r->src.name)))
+ {
+ warn_section("rule", r, e, "refers to not existing zone '%s'", r->src.name);
+ return false;
+ }
+ else if (r->dest.set && !r->dest.any &&
+ !(r->_dest = fw3_lookup_zone(state, r->dest.name)))
+ {
+ warn_section("rule", r, e, "refers to not existing zone '%s'", r->dest.name);
+ return false;
+ }
+ else if (r->ipset.set && state->disable_ipsets)
+ {
+ warn_section("rule", r, e, "skipped due to disabled ipset support");
+ return false;
+ }
+ else if (r->ipset.set &&
+ !(r->ipset.ptr = fw3_lookup_ipset(state, r->ipset.name)))
+ {
+ warn_section("rule", r, e, "refers to unknown ipset '%s'", r->ipset.name);
+ return false;
+ }
+ else if (r->helper.set &&
+ !(r->helper.ptr = fw3_lookup_cthelper(state, r->helper.name)))
+ {
+ warn_section("rule", r, e, "refers to unknown CT helper '%s'", r->helper.name);
+ return false;
+ }
+ else if (r->set_helper.set &&
+ !(r->set_helper.ptr = fw3_lookup_cthelper(state, r->set_helper.name)))
+ {
+ warn_section("rule", r, e, "refers to unknown CT helper '%s'", r->set_helper.name);
+ return false;
+ }
+
+ if (!r->_src && (r->target == FW3_FLAG_NOTRACK || r->target == FW3_FLAG_HELPER))
+ {
+ warn_section("rule", r, e, "is set to target %s but has no source assigned",
+ fw3_flag_names[r->target]);
+ return false;
+ }
+
+ if (!r->set_mark.set && !r->set_xmark.set &&
+ r->target == FW3_FLAG_MARK)
+ {
+ warn_section("rule", r, e, "is set to target MARK but specifies neither "
+ "'set_mark' nor 'set_xmark' option");
+ return false;
+ }
+
+ if (!r->set_dscp.set && r->target == FW3_FLAG_DSCP)
+ {
+ warn_section("rule", r, e, "is set to target DSCP but specifies no "
+ "'set_dscp' option");
+ return false;
+ }
+
+ if (r->set_mark.invert || r->set_xmark.invert || r->set_dscp.invert)
+ {
+ warn_section("rule", r, e, "must not have inverted 'set_mark', "
+ "'set_xmark' or 'set_dscp'");
+ return false;
+ }
+
+ if (!r->set_helper.set && r->target == FW3_FLAG_HELPER)
+ {
+ warn_section("rule", r, e, "is set to target HELPER but specifies "
+ "no 'set_helper' option");
+ return false;
+ }
+
+ if (r->set_helper.invert && r->target == FW3_FLAG_HELPER)
+ {
+ warn_section("rule", r, e, "must not have inverted 'set_helper' option");
+ return false;
+ }
+
+ if (!r->_src && !r->_dest && !r->src.any && !r->dest.any)
+ {
+ warn_section("rule", r, e, "has neither a source nor a destination zone assigned "
+ "- assuming an output rule");
+ }
+
+ if (list_empty(&r->proto))
+ {
+ warn_section("rule", r, e, "does not specify a protocol, assuming TCP+UDP");
+ fw3_parse_protocol(&r->proto, "tcpudp", true);
+ }
+
+ if (r->target == FW3_FLAG_UNSPEC)
+ {
+ warn_section("rule", r, e, "has no target specified, defaulting to REJECT");
+ r->target = FW3_FLAG_REJECT;
+ }
+ else if (r->target > FW3_FLAG_DSCP)
+ {
+ warn_section("rule", r, e, "has invalid target specified, defaulting to REJECT");
+ r->target = FW3_FLAG_REJECT;
+ }
+
+ /* NB: r family... */
+ if (r->_dest)
+ {
+ fw3_setbit(r->_dest->flags[0], r->target);
+ fw3_setbit(r->_dest->flags[1], r->target);
+ }
+ else if (need_src_action_chain(r))
+ {
+ fw3_setbit(r->_src->flags[0], fw3_to_src_target(r->target));
+ fw3_setbit(r->_src->flags[1], fw3_to_src_target(r->target));
+ }
+
+ return true;
+}
+
+void
+fw3_load_rules(struct fw3_state *state, struct uci_package *p,
+ struct blob_attr *a)
+{
+ struct uci_section *s;
+ struct uci_element *e;
+ struct fw3_rule *rule;
+ struct blob_attr *entry;
+ unsigned rem;
+
+ INIT_LIST_HEAD(&state->rules);
+
+ blob_for_each_attr(entry, a, rem) {
+ const char *type;
+ const char *name = "ubus rule";
+
+ if (!fw3_attr_parse_name_type(entry, &name, &type))