For ingress rules, only jump into zone_name_src_ACTION chains if the target is not...
[project/firewall3.git] / rules.c
diff --git a/rules.c b/rules.c
index 53268e2..4750af7 100644 (file)
--- a/rules.c
+++ b/rules.c
@@ -28,15 +28,15 @@ 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("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),
@@ -63,6 +63,12 @@ const struct fw3_option fw3_rule_opts[] = {
 };
 
 
+static bool
+need_src_action_chain(struct fw3_rule *r)
+{
+       return (r->_src && r->_src->log && (r->target > FW3_FLAG_ACCEPT));
+}
+
 void
 fw3_load_rules(struct fw3_state *state, struct uci_package *p)
 {
@@ -133,8 +139,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)))
+               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);
@@ -200,6 +206,11 @@ 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);
                }
+               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));
+               }
 
                list_add_tail(&rule->list, &state->rules);
                continue;
@@ -245,7 +256,13 @@ append_chain(struct fw3_ipt_rule *r, struct fw3_rule *rule)
                }
 
                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), "delegate_output");
+                       else
+                               snprintf(chain, sizeof(chain), "zone_%s_output",
+                                        rule->dest.name);
+               }
        }
 
        fw3_ipt_rule_append(r, chain);
@@ -268,28 +285,40 @@ 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, fw3_flag_names[rule->target]);
+               return;
+
        case FW3_FLAG_ACCEPT:
        case FW3_FLAG_DROP:
-       case FW3_FLAG_NOTRACK:
                name = fw3_flag_names[rule->target];
                break;
 
        default:
-               name = fw3_flag_names[FW3_FLAG_REJECT];
+               name = "reject";
                break;
        }
 
        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 if (need_src_action_chain(rule))
+               fw3_ipt_rule_target(r, "zone_%s_src_%s", rule->src.name, name);
        else
                fw3_ipt_rule_target(r, name);
 }
 
 static void
+set_comment(struct fw3_ipt_rule *r, const char *name, int num)
+{
+       if (name)
+               fw3_ipt_rule_comment(r, name);
+       else
+               fw3_ipt_rule_comment(r, "@rule[%u]", num);
+}
+
+static void
 print_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
-           struct fw3_rule *rule, struct fw3_protocol *proto,
+           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)
@@ -299,7 +328,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;
        }
 
@@ -313,13 +344,13 @@ print_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
        fw3_ipt_rule_sport_dport(r, sport, dport);
        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);
        set_target(r, rule);
        fw3_ipt_rule_extra(r, rule->extra);
-       fw3_ipt_rule_comment(r, rule->name);
+       set_comment(r, rule->name, num);
        append_chain(r, rule);
 }
 
@@ -362,23 +393,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)
@@ -403,7 +434,7 @@ expand_rule(struct fw3_ipt_handle *handle, struct fw3_state *state,
                fw3_foreach(dport, dports)
                fw3_foreach(mac, &rule->mac_src)
                fw3_foreach(icmptype, icmptypes)
-                       print_rule(handle, state, rule, proto, sip, dip,
+                       print_rule(handle, state, rule, num, proto, sip, dip,
                                   sport, dport, mac, icmptype);
        }
 }