FW3_OPT("src", device, rule, src),
FW3_OPT("dest", device, rule, dest),
+ FW3_OPT("device", string, rule, device),
+ FW3_OPT("direction", direction, rule, direction_out),
+
FW3_OPT("ipset", setmatch, rule, ipset),
FW3_LIST("proto", protocol, rule, proto),
return (r->_src && r->_src->log && (r->target > FW3_FLAG_ACCEPT));
}
+static struct fw3_rule*
+alloc_rule(struct fw3_state *state)
+{
+ struct fw3_rule *rule = calloc(1, sizeof(*rule));
+
+ if (rule) {
+ INIT_LIST_HEAD(&rule->proto);
+
+ INIT_LIST_HEAD(&rule->ip_src);
+ INIT_LIST_HEAD(&rule->mac_src);
+ INIT_LIST_HEAD(&rule->port_src);
+
+ INIT_LIST_HEAD(&rule->ip_dest);
+ INIT_LIST_HEAD(&rule->port_dest);
+
+ INIT_LIST_HEAD(&rule->icmp_type);
+
+ list_add_tail(&rule->list, &state->rules);
+ rule->enabled = true;
+ }
+
+ return rule;
+}
+
void
-fw3_load_rules(struct fw3_state *state, struct uci_package *p)
+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 fw3_rule *rule, *n;
+ struct blob_attr *entry, *opt;
+ unsigned rem, orem;
INIT_LIST_HEAD(&state->rules);
- uci_foreach_element(&p->sections, e)
- {
- s = uci_to_section(e);
+ blob_for_each_attr(entry, a, rem) {
+ const char *type = NULL;
+ blobmsg_for_each_attr(opt, entry, orem)
+ if (!strcmp(blobmsg_name(opt), "type"))
+ type = blobmsg_get_string(opt);
- if (strcmp(s->type, "rule"))
+ if (!type || strcmp(type, "rule"))
continue;
- rule = malloc(sizeof(*rule));
-
- if (!rule)
+ if (!(rule = alloc_rule(state)))
continue;
- memset(rule, 0, sizeof(*rule));
-
- INIT_LIST_HEAD(&rule->proto);
-
- INIT_LIST_HEAD(&rule->ip_src);
- INIT_LIST_HEAD(&rule->mac_src);
- INIT_LIST_HEAD(&rule->port_src);
+ if (!fw3_parse_blob_options(rule, fw3_rule_opts, entry))
+ {
+ fprintf(stderr, "ubus section skipped due to invalid options\n");
+ fw3_free_rule(rule);
+ continue;
+ }
+ }
- INIT_LIST_HEAD(&rule->ip_dest);
- INIT_LIST_HEAD(&rule->port_dest);
+ uci_foreach_element(&p->sections, e)
+ {
+ s = uci_to_section(e);
- INIT_LIST_HEAD(&rule->icmp_type);
+ if (strcmp(s->type, "rule"))
+ continue;
- rule->enabled = true;
+ if (!(rule = alloc_rule(state)))
+ continue;
if (!fw3_parse_options(rule, fw3_rule_opts, s))
{
fw3_free_rule(rule);
continue;
}
+ }
+ list_for_each_entry_safe(rule, n, &state->rules, list)
+ {
if (!rule->enabled)
{
fw3_free_rule(rule);
setbit(rule->_src->flags[0], fw3_to_src_target(rule->target));
setbit(rule->_src->flags[1], fw3_to_src_target(rule->target));
}
-
- list_add_tail(&rule->list, &state->rules);
- continue;
}
}
r = fw3_ipt_rule_create(handle, proto, NULL, NULL, sip, dip);
fw3_ipt_rule_sport_dport(r, sport, dport);
+ fw3_ipt_rule_device(r, rule->device, rule->direction_out);
fw3_ipt_rule_icmptype(r, icmptype);
fw3_ipt_rule_mac(r, mac);
fw3_ipt_rule_ipset(r, &rule->ipset);