firewall3: fix left shift on 64 bit systems in fw3_bitlen2netmask
[project/firewall3.git] / rules.c
diff --git a/rules.c b/rules.c
index 1a4473b..f45de6e 100644 (file)
--- 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:
@@ -344,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);