firewall3: check the return value of fw3_parse_options()
[project/firewall3.git] / rules.c
diff --git a/rules.c b/rules.c
index 0f4e925..5fb9998 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,7 +28,10 @@ const struct fw3_option fw3_rule_opts[] = {
        FW3_OPT("src",                 device,    rule,     src),
        FW3_OPT("dest",                device,    rule,     dest),
 
-       FW3_OPT("ipset",               device,    rule,     ipset),
+       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,112 +89,178 @@ 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;
+       }
 
-               fw3_parse_options(rule, fw3_rule_opts, s);
+       return rule;
+}
 
-               if (!rule->enabled)
-               {
-                       fw3_free_rule(rule);
-                       continue;
-               }
+#define warn_rule(r, e, fmt, ...)                                                                      \
+       do {                                                                                                                    \
+               if (e)                                                                                                          \
+                       warn_elem(e, fmt, ##__VA_ARGS__);                                               \
+               else                                                                                                            \
+                       warn("Warning: ubus rule (%s) " fmt,                                    \
+                            (r && r->name) ? r->name : "?", ##__VA_ARGS__);    \
+       } while(0)
+
+static bool
+check_rule(struct fw3_state *state, struct fw3_rule *r, struct uci_element *e)
+{
+       if (!r->enabled)
+               return false;
 
-               if (rule->src.invert || rule->dest.invert)
-               {
-                       warn_elem(e, "must not have inverted 'src' or 'dest' options");
-                       fw3_free_rule(rule);
-                       continue;
-               }
-               else if (rule->src.set && !rule->src.any &&
-                        !(rule->_src = fw3_lookup_zone(state, rule->src.name)))
-               {
-                       warn_elem(e, "refers to not existing zone '%s'", rule->src.name);
-                       fw3_free_rule(rule);
-                       continue;
-               }
-               else if (rule->dest.set && !rule->dest.any &&
-                        !(rule->_dest = fw3_lookup_zone(state, rule->dest.name)))
-               {
-                       warn_elem(e, "refers to not existing zone '%s'", rule->dest.name);
-                       fw3_free_rule(rule);
-                       continue;
-               }
-               else if (rule->ipset.set && state->disable_ipsets)
-               {
-                       warn_elem(e, "skipped due to disabled ipset support");
-                       fw3_free_rule(rule);
-                       continue;
-               }
-               else if (rule->ipset.set && !rule->ipset.any &&
-                        !(rule->_ipset = fw3_lookup_ipset(state, rule->ipset.name)))
-               {
-                       warn_elem(e, "refers to unknown ipset '%s'", rule->ipset.name);
-                       fw3_free_rule(rule);
-                       continue;
-               }
+       if (r->src.invert || r->dest.invert)
+       {
+               warn_rule(r, e, "must not have inverted 'src' or 'dest' options");
+               return false;
+       }
+       else if (r->src.set && !r->src.any &&
+                !(r->_src = fw3_lookup_zone(state, r->src.name)))
+       {
+               warn_rule(r, e, "refers to not existing zone '%s'", r->src.name);
+               return false;
+       }
+       else if (r->dest.set && !r->dest.any &&
+                !(r->_dest = fw3_lookup_zone(state, r->dest.name)))
+       {
+               warn_rule(r, e, "refers to not existing zone '%s'", r->dest.name);
+               return false;
+       }
+       else if (r->ipset.set && state->disable_ipsets)
+       {
+               warn_rule(r, e, "skipped due to disabled ipset support");
+               return false;
+       }
+       else if (r->ipset.set &&
+                !(r->ipset.ptr = fw3_lookup_ipset(state, r->ipset.name)))
+       {
+               warn_rule(r, e, "refers to unknown ipset '%s'", r->ipset.name);
+               return false;
+       }
 
-               if (!rule->_src && rule->target == FW3_FLAG_NOTRACK)
-               {
-                       warn_elem(e, "is set to target NOTRACK but has no source assigned");
-                       fw3_free_rule(rule);
+       if (!r->_src && r->target == FW3_FLAG_NOTRACK)
+       {
+               warn_rule(r, e, "is set to target NOTRACK but has no source assigned");
+               return false;
+       }
+
+       if (!r->set_mark.set && !r->set_xmark.set &&
+           r->target == FW3_FLAG_MARK)
+       {
+               warn_rule(r, e, "is set to target MARK but specifies neither "
+                               "'set_mark' nor 'set_xmark' option");
+               return false;
+       }
+
+       if (r->_dest && r->target == FW3_FLAG_MARK)
+       {
+               warn_rule(r, e, "must not specify 'dest' for MARK target");
+               return false;
+       }
+
+       if (r->set_mark.invert || r->set_xmark.invert)
+       {
+               warn_rule(r, e, "must not have inverted 'set_mark' or 'set_xmark'");
+               return false;
+       }
+
+       if (!r->_src && !r->_dest && !r->src.any && !r->dest.any)
+       {
+               warn_rule(r, e, "has neither a source nor a destination zone assigned "
+                               "- assuming an output r");
+       }
+
+       if (list_empty(&r->proto))
+       {
+               warn_rule(r, e, "does not specify a protocol, assuming TCP+UDP");
+               fw3_parse_protocol(&r->proto, "tcpudp", true);
+       }
+
+       if (r->target == FW3_FLAG_UNSPEC)
+       {
+               warn_rule(r, e, "has no target specified, defaulting to REJECT");
+               r->target = FW3_FLAG_REJECT;
+       }
+       else if (r->target > FW3_FLAG_MARK)
+       {
+               warn_rule(r, e, "has invalid target specified, defaulting to REJECT");
+               r->target = FW3_FLAG_REJECT;
+       }
+
+       /* NB: r family... */
+       if (r->_dest)
+       {
+               fw3_setbit(r->_dest->flags[0], r->target);
+               fw3_setbit(r->_dest->flags[1], r->target);
+       }
+       else if (need_src_action_chain(r))
+       {
+               fw3_setbit(r->_src->flags[0], fw3_to_src_target(r->target));
+               fw3_setbit(r->_src->flags[1], fw3_to_src_target(r->target));
+       }
+
+       return true;
+}
+
+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;
+       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->set_mark.set && !rule->set_xmark.set &&
-                   rule->target == FW3_FLAG_MARK)
-               {
-                       warn_elem(e, "is set to target MARK but specifies neither "
-                                    "'set_mark' nor 'set_xmark' option");
-                       fw3_free_rule(rule);
+               if (!(rule = alloc_rule(state)))
                        continue;
-               }
 
-               if (rule->_dest && rule->target == FW3_FLAG_MARK)
+               if (!fw3_parse_blob_options(rule, fw3_rule_opts, entry, name))
                {
-                       warn_elem(e, "must not specify 'dest' for MARK target");
+                       warn_rule(rule, NULL, "skipped due to invalid options\n");
                        fw3_free_rule(rule);
                        continue;
                }
 
-               if (rule->set_mark.invert || rule->set_xmark.invert)
-               {
-                       warn_elem(e, "must not have inverted 'set_mark' or 'set_xmark'");
+               if (!check_rule(state, rule, NULL))
                        fw3_free_rule(rule);
-                       continue;
-               }
+       }
 
-               if (!rule->_src && !rule->_dest && !rule->src.any && !rule->dest.any)
-               {
-                       warn_elem(e, "has neither a source nor a destination zone assigned "
-                                    "- assuming an output rule");
-               }
+       uci_foreach_element(&p->sections, e)
+       {
+               s = uci_to_section(e);
 
-               if (list_empty(&rule->proto))
-               {
-                       warn_elem(e, "does not specify a protocol, assuming TCP+UDP");
-                       fw3_parse_protocol(&rule->proto, "tcpudp", true);
-               }
+               if (strcmp(s->type, "rule"))
+                       continue;
 
-               if (rule->target == FW3_FLAG_UNSPEC)
-               {
-                       warn_elem(e, "has no target specified, defaulting to REJECT");
-                       rule->target = FW3_FLAG_REJECT;
-               }
-               else if (rule->target > FW3_FLAG_MARK)
-               {
-                       warn_elem(e, "has invalid target specified, defaulting to REJECT");
-                       rule->target = FW3_FLAG_REJECT;
-               }
+               if (!(rule = alloc_rule(state)))
+                       continue;
 
-               /* NB: rule family... */
-               if (rule->_dest)
+               if (!fw3_parse_options(rule, fw3_rule_opts, s))
                {
-                       setbit(rule->_dest->flags[0], rule->target);
-                       setbit(rule->_dest->flags[1], rule->target);
+                       warn_elem(e, "skipped due to invalid options");
+                       fw3_free_rule(rule);
+                       continue;
                }
 
-               list_add_tail(&rule->list, &state->rules);
-               continue;
+               if (!check_rule(state, rule, e))
+                       fw3_free_rule(rule);
        }
 }
 
@@ -212,15 +270,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,14 +296,20 @@ 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)
-                       snprintf(chain, sizeof(chain), "zone_%s_output", rule->dest.name);
+               {
+                       if (rule->dest.any)
+                               snprintf(chain, sizeof(chain), "OUTPUT");
+                       else
+                               snprintf(chain, sizeof(chain), "zone_%s_output",
+                                        rule->dest.name);
+               }
        }
 
        fw3_ipt_rule_append(r, chain);
@@ -268,9 +332,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;
 
@@ -281,10 +349,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
@@ -308,7 +378,9 @@ print_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
        if (!fw3_is_family(sip, handle->family) ||
            !fw3_is_family(dip, handle->family))
        {
-               info("     ! Skipping due to different family of ip address");
+               if ((sip && !sip->resolved) || (dip && !dip->resolved))
+                       info("     ! Skipping due to different family of ip address");
+
                return;
        }
 
@@ -320,9 +392,10 @@ 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, rule->ipset.invert);
+       fw3_ipt_rule_ipset(r, &rule->ipset);
        fw3_ipt_rule_limit(r, &rule->limit);
        fw3_ipt_rule_time(r, &rule->time);
        fw3_ipt_rule_mark(r, &rule->mark);
@@ -371,23 +444,23 @@ expand_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                return;
        }
 
-       if (rule->_ipset)
+       if (rule->ipset.ptr)
        {
-               if (!fw3_is_family(rule->_ipset, handle->family))
+               if (!fw3_is_family(rule->ipset.ptr, handle->family))
                {
                        info("     ! Skipping due to different family in ipset");
                        return;
                }
 
-               if (!fw3_check_ipset(rule->_ipset))
+               if (!fw3_check_ipset(rule->ipset.ptr))
                {
                        info("     ! Skipping due to missing ipset '%s'",
-                            rule->_ipset->external
-                                       ? rule->_ipset->external : rule->_ipset->name);
+                            rule->ipset.ptr->external
+                                       ? rule->ipset.ptr->external : rule->ipset.ptr->name);
                        return;
                }
 
-               set(rule->_ipset->flags, handle->family, handle->family);
+               set(rule->ipset.ptr->flags, handle->family, handle->family);
        }
 
        list_for_each_entry(proto, &rule->proto, list)