X-Git-Url: http://git.archive.openwrt.org/?p=project%2Ffirewall3.git;a=blobdiff_plain;f=rules.c;h=f45de6eb6e864f7d881ec95c19bbe36bbde18580;hp=4750af7ef2d9dee6050d567c7c15c633a030ae54;hb=a4dec0d88c144d0e973c1884531e27c7499c27a2;hpb=aeba5741d7ed3b53fb4f1bb3679feb0fa526f4c6 diff --git a/rules.c b/rules.c index 4750af7..f45de6e 100644 --- a/rules.c +++ b/rules.c @@ -28,6 +28,9 @@ const struct fw3_option fw3_rule_opts[] = { 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), @@ -69,44 +72,82 @@ 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; - fw3_parse_options(rule, fw3_rule_opts, s); + if (!fw3_parse_options(rule, fw3_rule_opts, s)) + { + warn_elem(e, "skipped due to invalid options"); + fw3_free_rule(rule); + continue; + } + } + list_for_each_entry_safe(rule, n, &state->rules, list) + { if (!rule->enabled) { fw3_free_rule(rule); @@ -211,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; } } @@ -286,7 +324,8 @@ static void set_target(struct fw3_ipt_rule *r, struct fw3_rule *rule) return; case FW3_FLAG_NOTRACK: - fw3_ipt_rule_target(r, fw3_flag_names[rule->target]); + fw3_ipt_rule_target(r, "CT"); + fw3_ipt_rule_addarg(r, false, "--notrack", NULL); return; case FW3_FLAG_ACCEPT: @@ -295,7 +334,7 @@ static void set_target(struct fw3_ipt_rule *r, struct fw3_rule *rule) break; default: - name = "reject"; + name = fw3_flag_names[FW3_FLAG_REJECT]; break; } @@ -303,8 +342,10 @@ static void set_target(struct fw3_ipt_rule *r, struct fw3_rule *rule) fw3_ipt_rule_target(r, "zone_%s_dest_%s", rule->dest.name, name); else if (need_src_action_chain(rule)) fw3_ipt_rule_target(r, "zone_%s_src_%s", rule->src.name, name); - else + else if (strcmp(name, "REJECT")) fw3_ipt_rule_target(r, name); + else + fw3_ipt_rule_target(r, "reject"); } static void @@ -342,6 +383,7 @@ print_rule(struct fw3_ipt_handle *handle, struct fw3_state *state, 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);