utils.h: Avoid name clashes for setbit/delbit/hasbit
[project/firewall3.git] / rules.c
diff --git a/rules.c b/rules.c
index a251f7c..8f232d3 100644 (file)
--- a/rules.c
+++ b/rules.c
@@ -1,7 +1,7 @@
 /*
  * firewall3 - 3rd OpenWrt UCI firewall implementation
  *
- *   Copyright (C) 2013 Jo-Philipp Wich <jow@openwrt.org>
+ *   Copyright (C) 2013 Jo-Philipp Wich <jo@mein.io>
  *
  * Permission to use, copy, modify, and/or distribute this software for any
  * purpose with or without fee is hereby granted, provided that the above
@@ -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),
@@ -63,29 +66,18 @@ const struct fw3_option fw3_rule_opts[] = {
 };
 
 
-void
-fw3_load_rules(struct fw3_state *state, struct uci_package *p)
+static bool
+need_src_action_chain(struct fw3_rule *r)
 {
-       struct uci_section *s;
-       struct uci_element *e;
-       struct fw3_rule *rule;
-
-       INIT_LIST_HEAD(&state->rules);
-
-       uci_foreach_element(&p->sections, e)
-       {
-               s = uci_to_section(e);
-
-               if (strcmp(s->type, "rule"))
-                       continue;
-
-               rule = malloc(sizeof(*rule));
-
-               if (!rule)
-                       continue;
+       return (r->_src && r->_src->log && (r->target > FW3_FLAG_ACCEPT));
+}
 
-               memset(rule, 0, sizeof(*rule));
+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);
@@ -97,10 +89,68 @@ fw3_load_rules(struct fw3_state *state, struct uci_package *p)
 
                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,
+               struct blob_attr *a)
+{
+       struct uci_section *s;
+       struct uci_element *e;
+       struct fw3_rule *rule, *n;
+       struct blob_attr *entry, *opt;
+       unsigned rem, orem;
+
+       INIT_LIST_HEAD(&state->rules);
+
+       blob_for_each_attr(entry, a, rem) {
+               const char *type = NULL;
+               const char *name = "ubus rule";
+               blobmsg_for_each_attr(opt, entry, orem)
+                       if (!strcmp(blobmsg_name(opt), "type"))
+                               type = blobmsg_get_string(opt);
+                       else if (!strcmp(blobmsg_name(opt), "name"))
+                               name = blobmsg_get_string(opt);
+
+               if (!type || strcmp(type, "rule"))
+                       continue;
+
+               if (!(rule = alloc_rule(state)))
+                       continue;
 
-               fw3_parse_options(rule, fw3_rule_opts, s);
+               if (!fw3_parse_blob_options(rule, fw3_rule_opts, entry, name))
+               {
+                       fprintf(stderr, "%s skipped due to invalid options\n", name);
+                       fw3_free_rule(rule);
+                       continue;
+               }
+       }
+
+       uci_foreach_element(&p->sections, e)
+       {
+               s = uci_to_section(e);
+
+               if (strcmp(s->type, "rule"))
+                       continue;
+
+               if (!(rule = alloc_rule(state)))
+                       continue;
+
+               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);
@@ -197,12 +247,14 @@ fw3_load_rules(struct fw3_state *state, struct uci_package *p)
                /* NB: rule family... */
                if (rule->_dest)
                {
-                       setbit(rule->_dest->flags[0], rule->target);
-                       setbit(rule->_dest->flags[1], rule->target);
+                       fw3_setbit(rule->_dest->flags[0], rule->target);
+                       fw3_setbit(rule->_dest->flags[1], rule->target);
+               }
+               else if (need_src_action_chain(rule))
+               {
+                       fw3_setbit(rule->_src->flags[0], fw3_to_src_target(rule->target));
+                       fw3_setbit(rule->_src->flags[1], fw3_to_src_target(rule->target));
                }
-
-               list_add_tail(&rule->list, &state->rules);
-               continue;
        }
 }
 
@@ -212,15 +264,15 @@ append_chain(struct fw3_ipt_rule *r, struct fw3_rule *rule)
 {
        char chain[32];
 
-       snprintf(chain, sizeof(chain), "delegate_output");
+       snprintf(chain, sizeof(chain), "OUTPUT");
 
        if (rule->target == FW3_FLAG_NOTRACK)
        {
                snprintf(chain, sizeof(chain), "zone_%s_notrack", rule->src.name);
        }
-       else if (rule->target == FW3_FLAG_MARK)
+       else if (rule->target == FW3_FLAG_MARK && (rule->_src || rule->src.any))
        {
-               snprintf(chain, sizeof(chain), "fwmark");
+               snprintf(chain, sizeof(chain), "PREROUTING");
        }
        else
        {
@@ -238,16 +290,16 @@ append_chain(struct fw3_ipt_rule *r, struct fw3_rule *rule)
                        else
                        {
                                if (rule->dest.set)
-                                       snprintf(chain, sizeof(chain), "delegate_forward");
+                                       snprintf(chain, sizeof(chain), "FORWARD");
                                else
-                                       snprintf(chain, sizeof(chain), "delegate_input");
+                                       snprintf(chain, sizeof(chain), "INPUT");
                        }
                }
 
                if (rule->dest.set && !rule->src.set)
                {
                        if (rule->dest.any)
-                               snprintf(chain, sizeof(chain), "delegate_output");
+                               snprintf(chain, sizeof(chain), "OUTPUT");
                        else
                                snprintf(chain, sizeof(chain), "zone_%s_output",
                                         rule->dest.name);
@@ -274,9 +326,13 @@ static void set_target(struct fw3_ipt_rule *r, struct fw3_rule *rule)
                fw3_ipt_rule_addarg(r, false, name, buf);
                return;
 
+       case FW3_FLAG_NOTRACK:
+               fw3_ipt_rule_target(r, "CT");
+               fw3_ipt_rule_addarg(r, false, "--notrack", NULL);
+               return;
+
        case FW3_FLAG_ACCEPT:
        case FW3_FLAG_DROP:
-       case FW3_FLAG_NOTRACK:
                name = fw3_flag_names[rule->target];
                break;
 
@@ -287,10 +343,12 @@ static void set_target(struct fw3_ipt_rule *r, struct fw3_rule *rule)
 
        if (rule->dest.set && !rule->dest.any)
                fw3_ipt_rule_target(r, "zone_%s_dest_%s", rule->dest.name, name);
-       else if (rule->target == FW3_FLAG_REJECT)
-               fw3_ipt_rule_target(r, "reject");
-       else
+       else if (need_src_action_chain(rule))
+               fw3_ipt_rule_target(r, "zone_%s_src_%s", rule->src.name, name);
+       else if (strcmp(name, "REJECT"))
                fw3_ipt_rule_target(r, name);
+       else
+               fw3_ipt_rule_target(r, "reject");
 }
 
 static void
@@ -328,6 +386,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);