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 e6af62e..f45de6e 100644 (file)
--- a/rules.c
+++ b/rules.c
@@ -28,15 +28,18 @@ 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),
 
-       FW3_LIST("src_ip",             address,   rule,     ip_src),
+       FW3_LIST("src_ip",             network,   rule,     ip_src),
        FW3_LIST("src_mac",            mac,       rule,     mac_src),
        FW3_LIST("src_port",           port,      rule,     port_src),
 
-       FW3_LIST("dest_ip",            address,   rule,     ip_dest),
+       FW3_LIST("dest_ip",            network,   rule,     ip_dest),
        FW3_LIST("dest_port",          port,      rule,     port_dest),
 
        FW3_LIST("icmp_type",          icmptype,  rule,     icmp_type),
@@ -53,35 +56,28 @@ const struct fw3_option fw3_rule_opts[] = {
        FW3_OPT("weekdays",            weekdays,  rule,     time.weekdays),
        FW3_OPT("monthdays",           monthdays, rule,     time.monthdays),
 
+       FW3_OPT("mark",                mark,      rule,     mark),
+       FW3_OPT("set_mark",            mark,      rule,     set_mark),
+       FW3_OPT("set_xmark",           mark,      rule,     set_xmark),
+
        FW3_OPT("target",              target,    rule,     target),
 
        { }
 };
 
 
-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);
@@ -93,10 +89,65 @@ 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;
+               blobmsg_for_each_attr(opt, entry, orem)
+                       if (!strcmp(blobmsg_name(opt), "type"))
+                               type = blobmsg_get_string(opt);
+
+               if (!type || strcmp(type, "rule"))
+                       continue;
+
+               if (!(rule = alloc_rule(state)))
+                       continue;
+
+               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;
+               }
+       }
+
+       uci_foreach_element(&p->sections, e)
+       {
+               s = uci_to_section(e);
+
+               if (strcmp(s->type, "rule"))
+                       continue;
+
+               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);
@@ -110,14 +161,14 @@ fw3_load_rules(struct fw3_state *state, struct uci_package *p)
                        continue;
                }
                else if (rule->src.set && !rule->src.any &&
-                        !(rule->_src = fw3_lookup_zone(state, rule->src.name, false)))
+                        !(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, false)))
+                        !(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);
@@ -129,8 +180,8 @@ fw3_load_rules(struct fw3_state *state, struct uci_package *p)
                        fw3_free_rule(rule);
                        continue;
                }
-               else if (rule->ipset.set && !rule->ipset.any &&
-                        !(rule->_ipset = fw3_lookup_ipset(state, rule->ipset.name, false)))
+               else if (rule->ipset.set &&
+                        !(rule->ipset.ptr = fw3_lookup_ipset(state, rule->ipset.name)))
                {
                        warn_elem(e, "refers to unknown ipset '%s'", rule->ipset.name);
                        fw3_free_rule(rule);
@@ -144,18 +195,47 @@ fw3_load_rules(struct fw3_state *state, struct uci_package *p)
                        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);
+                       continue;
+               }
+
+               if (rule->_dest && rule->target == FW3_FLAG_MARK)
+               {
+                       warn_elem(e, "must not specify 'dest' for MARK target");
+                       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'");
+                       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");
                }
 
+               if (list_empty(&rule->proto))
+               {
+                       warn_elem(e, "does not specify a protocol, assuming TCP+UDP");
+                       fw3_parse_protocol(&rule->proto, "tcpudp", true);
+               }
+
                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_NOTRACK)
+               else if (rule->target > FW3_FLAG_MARK)
                {
                        warn_elem(e, "has invalid target specified, defaulting to REJECT");
                        rule->target = FW3_FLAG_REJECT;
@@ -167,23 +247,29 @@ fw3_load_rules(struct fw3_state *state, struct uci_package *p)
                        setbit(rule->_dest->flags[0], rule->target);
                        setbit(rule->_dest->flags[1], rule->target);
                }
-
-               list_add_tail(&rule->list, &state->rules);
-               continue;
+               else if (need_src_action_chain(rule))
+               {
+                       setbit(rule->_src->flags[0], fw3_to_src_target(rule->target));
+                       setbit(rule->_src->flags[1], fw3_to_src_target(rule->target));
+               }
        }
 }
 
 
 static void
-print_chain(struct fw3_rule *rule)
+append_chain(struct fw3_ipt_rule *r, struct fw3_rule *rule)
 {
-       char chain[256];
+       char chain[32];
 
-       sprintf(chain, "delegate_output");
+       snprintf(chain, sizeof(chain), "delegate_output");
 
        if (rule->target == FW3_FLAG_NOTRACK)
        {
-               sprintf(chain, "zone_%s_notrack", rule->src.name);
+               snprintf(chain, sizeof(chain), "zone_%s_notrack", rule->src.name);
+       }
+       else if (rule->target == FW3_FLAG_MARK)
+       {
+               snprintf(chain, sizeof(chain), "fwmark");
        }
        else
        {
@@ -192,88 +278,127 @@ print_chain(struct fw3_rule *rule)
                        if (!rule->src.any)
                        {
                                if (rule->dest.set)
-                                       sprintf(chain, "zone_%s_forward", rule->src.name);
+                                       snprintf(chain, sizeof(chain), "zone_%s_forward",
+                                                rule->src.name);
                                else
-                                       sprintf(chain, "zone_%s_input", rule->src.name);
+                                       snprintf(chain, sizeof(chain), "zone_%s_input",
+                                                rule->src.name);
                        }
                        else
                        {
                                if (rule->dest.set)
-                                       sprintf(chain, "delegate_forward");
+                                       snprintf(chain, sizeof(chain), "delegate_forward");
                                else
-                                       sprintf(chain, "delegate_input");
+                                       snprintf(chain, sizeof(chain), "delegate_input");
                        }
                }
 
                if (rule->dest.set && !rule->src.set)
-                       sprintf(chain, "zone_%s_output", rule->dest.name);
+               {
+                       if (rule->dest.any)
+                               snprintf(chain, sizeof(chain), "delegate_output");
+                       else
+                               snprintf(chain, sizeof(chain), "zone_%s_output",
+                                        rule->dest.name);
+               }
        }
 
-       fw3_pr("-A %s", chain);
+       fw3_ipt_rule_append(r, chain);
 }
 
-static void print_target(struct fw3_rule *rule)
+static void set_target(struct fw3_ipt_rule *r, struct fw3_rule *rule)
 {
-       const char *target;
+       const char *name;
+       struct fw3_mark *mark;
+       char buf[sizeof("0xFFFFFFFF/0xFFFFFFFF\0")];
 
        switch(rule->target)
        {
+       case FW3_FLAG_MARK:
+               name = rule->set_mark.set ? "--set-mark" : "--set-xmark";
+               mark = rule->set_mark.set ? &rule->set_mark : &rule->set_xmark;
+               sprintf(buf, "0x%x/0x%x", mark->mark, mark->mask);
+
+               fw3_ipt_rule_target(r, "MARK");
+               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:
-               target = fw3_flag_names[rule->target];
+               name = fw3_flag_names[rule->target];
                break;
 
        default:
-               target = fw3_flag_names[FW3_FLAG_REJECT];
+               name = fw3_flag_names[FW3_FLAG_REJECT];
                break;
        }
 
        if (rule->dest.set && !rule->dest.any)
-               fw3_pr(" -j zone_%s_dest_%s\n", rule->dest.name, target);
-       else if (rule->target == FW3_FLAG_REJECT)
-               fw3_pr(" -j reject\n");
+               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 if (strcmp(name, "REJECT"))
+               fw3_ipt_rule_target(r, name);
+       else
+               fw3_ipt_rule_target(r, "reject");
+}
+
+static void
+set_comment(struct fw3_ipt_rule *r, const char *name, int num)
+{
+       if (name)
+               fw3_ipt_rule_comment(r, name);
        else
-               fw3_pr(" -j %s\n", target);
+               fw3_ipt_rule_comment(r, "@rule[%u]", num);
 }
 
 static void
-print_rule(struct fw3_state *state, enum fw3_family family,
-           enum fw3_table table, struct fw3_rule *rule,
-           struct fw3_protocol *proto,
+print_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
+           struct fw3_rule *rule, int num, struct fw3_protocol *proto,
            struct fw3_address *sip, struct fw3_address *dip,
            struct fw3_port *sport, struct fw3_port *dport,
            struct fw3_mac *mac, struct fw3_icmptype *icmptype)
 {
-       if (!fw3_is_family(sip, family) || !fw3_is_family(dip, family))
+       struct fw3_ipt_rule *r;
+
+       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;
        }
 
-       if (proto->protocol == 58 && family == FW3_FAMILY_V4)
+       if (proto->protocol == 58 && handle->family == FW3_FAMILY_V4)
        {
                info("     ! Skipping due to different family of protocol");
                return;
        }
 
-       print_chain(rule);
-       fw3_format_ipset(rule->_ipset, rule->ipset.invert);
-       fw3_format_protocol(proto, family);
-       fw3_format_src_dest(sip, dip);
-       fw3_format_sport_dport(sport, dport);
-       fw3_format_icmptype(icmptype, family);
-       fw3_format_mac(mac);
-       fw3_format_limit(&rule->limit);
-       fw3_format_time(&rule->time);
-       fw3_format_extra(rule->extra);
-       fw3_format_comment(rule->name);
-       print_target(rule);
+       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);
+       fw3_ipt_rule_limit(r, &rule->limit);
+       fw3_ipt_rule_time(r, &rule->time);
+       fw3_ipt_rule_mark(r, &rule->mark);
+       set_target(r, rule);
+       fw3_ipt_rule_extra(r, rule->extra);
+       set_comment(r, rule->name, num);
+       append_chain(r, rule);
 }
 
 static void
-expand_rule(struct fw3_state *state, enum fw3_family family,
-            enum fw3_table table, struct fw3_rule *rule, int num)
+expand_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
+            struct fw3_rule *rule, int num)
 {
        struct fw3_protocol *proto;
        struct fw3_address *sip;
@@ -290,11 +415,12 @@ expand_rule(struct fw3_state *state, enum fw3_family family,
        struct list_head empty;
        INIT_LIST_HEAD(&empty);
 
-       if (!fw3_is_family(rule, family))
+       if (!fw3_is_family(rule, handle->family))
                return;
 
-       if ((table == FW3_TABLE_RAW && rule->target != FW3_FLAG_NOTRACK) ||
-           (table != FW3_TABLE_FILTER))
+       if ((rule->target == FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_RAW) ||
+           (rule->target == FW3_FLAG_MARK && handle->table != FW3_TABLE_MANGLE) ||
+               (rule->target < FW3_FLAG_NOTRACK && handle->table != FW3_TABLE_FILTER))
                return;
 
        if (rule->name)
@@ -302,22 +428,30 @@ expand_rule(struct fw3_state *state, enum fw3_family family,
        else
                info("   * Rule #%u", num);
 
-       if (!fw3_is_family(rule->_src, family) ||
-           !fw3_is_family(rule->_dest, family))
+       if (!fw3_is_family(rule->_src, handle->family) ||
+           !fw3_is_family(rule->_dest, handle->family))
        {
                info("     ! Skipping due to different family of zone");
                return;
        }
 
-       if (rule->_ipset)
+       if (rule->ipset.ptr)
        {
-               if (!fw3_is_family(rule->_ipset, family))
+               if (!fw3_is_family(rule->ipset.ptr, handle->family))
                {
                        info("     ! Skipping due to different family in ipset");
                        return;
                }
 
-               set(rule->_ipset->flags, family, family);
+               if (!fw3_check_ipset(rule->ipset.ptr))
+               {
+                       info("     ! Skipping due to missing ipset '%s'",
+                            rule->ipset.ptr->external
+                                       ? rule->ipset.ptr->external : rule->ipset.ptr->name);
+                       return;
+               }
+
+               set(rule->ipset.ptr->flags, handle->family, handle->family);
        }
 
        list_for_each_entry(proto, &rule->proto, list)
@@ -342,18 +476,17 @@ expand_rule(struct fw3_state *state, enum fw3_family family,
                fw3_foreach(dport, dports)
                fw3_foreach(mac, &rule->mac_src)
                fw3_foreach(icmptype, icmptypes)
-                       print_rule(state, family, table, rule, proto, sip, dip,
+                       print_rule(handle, state, rule, num, proto, sip, dip,
                                   sport, dport, mac, icmptype);
        }
 }
 
 void
-fw3_print_rules(struct fw3_state *state, enum fw3_family family,
-                enum fw3_table table)
+fw3_print_rules(struct fw3_ipt_handle *handle, struct fw3_state *state)
 {
        int num = 0;
        struct fw3_rule *rule;
 
        list_for_each_entry(rule, &state->rules, list)
-               expand_rule(state, family, table, rule, num++);
+               expand_rule(handle, state, rule, num++);
 }