X-Git-Url: http://git.archive.openwrt.org/?p=project%2Ffirewall3.git;a=blobdiff_plain;f=rules.c;h=f45de6eb6e864f7d881ec95c19bbe36bbde18580;hp=b41dfac5e6d981e4d032de4cfa0239a09f174526;hb=e678dcbf0336c3ca10f9fe2fae8b19347b6c1d4d;hpb=2f392a3b91c25c94abfc9a7862d908c923f7bf2b diff --git a/rules.c b/rules.c index b41dfac..f45de6e 100644 --- a/rules.c +++ b/rules.c @@ -72,41 +72,71 @@ need_src_action_chain(struct fw3_rule *r) 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)) { @@ -114,7 +144,10 @@ fw3_load_rules(struct fw3_state *state, struct uci_package *p) fw3_free_rule(rule); continue; } + } + list_for_each_entry_safe(rule, n, &state->rules, list) + { if (!rule->enabled) { fw3_free_rule(rule); @@ -219,9 +252,6 @@ fw3_load_rules(struct fw3_state *state, struct uci_package *p) 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; } }