Increase compatibility to old firewall by initializing protocol of rules and redirect...
[project/firewall3.git] / rules.c
diff --git a/rules.c b/rules.c
index 77fe998..1037d2f 100644 (file)
--- a/rules.c
+++ b/rules.c
@@ -137,7 +137,7 @@ fw3_load_rules(struct fw3_state *state, struct uci_package *p)
                        continue;
                }
 
-               if (!rule->_src && rule->target == FW3_TARGET_NOTRACK)
+               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);
@@ -150,19 +150,29 @@ fw3_load_rules(struct fw3_state *state, struct uci_package *p)
                                     "- assuming an output rule");
                }
 
-               if (rule->target == FW3_TARGET_UNSPEC)
+               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_TARGET_REJECT;
+                       rule->target = FW3_FLAG_REJECT;
                }
-               else if (rule->target > FW3_TARGET_NOTRACK)
+               else if (rule->target > FW3_FLAG_NOTRACK)
                {
                        warn_elem(e, "has invalid target specified, defaulting to REJECT");
-                       rule->target = FW3_TARGET_REJECT;
+                       rule->target = FW3_FLAG_REJECT;
                }
 
+               /* NB: rule family... */
                if (rule->_dest)
-                       setbit(rule->_dest->flags, rule->target);
+               {
+                       setbit(rule->_dest->flags[0], rule->target);
+                       setbit(rule->_dest->flags[1], rule->target);
+               }
 
                list_add_tail(&rule->list, &state->rules);
                continue;
@@ -177,7 +187,7 @@ print_chain(struct fw3_rule *rule)
 
        sprintf(chain, "delegate_output");
 
-       if (rule->target == FW3_TARGET_NOTRACK)
+       if (rule->target == FW3_FLAG_NOTRACK)
        {
                sprintf(chain, "zone_%s_notrack", rule->src.name);
        }
@@ -214,28 +224,29 @@ static void print_target(struct fw3_rule *rule)
 
        switch(rule->target)
        {
-       case FW3_TARGET_ACCEPT:
-       case FW3_TARGET_DROP:
-       case FW3_TARGET_NOTRACK:
+       case FW3_FLAG_ACCEPT:
+       case FW3_FLAG_DROP:
+       case FW3_FLAG_NOTRACK:
                target = fw3_flag_names[rule->target];
                break;
 
        default:
-               target = fw3_flag_names[FW3_TARGET_REJECT];
+               target = 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_TARGET_REJECT)
+       else if (rule->target == FW3_FLAG_REJECT)
                fw3_pr(" -j reject\n");
        else
                fw3_pr(" -j %s\n", target);
 }
 
 static void
-print_rule(enum fw3_table table, enum fw3_family family,
-           struct fw3_rule *rule, struct fw3_protocol *proto,
+print_rule(struct fw3_state *state, enum fw3_family family,
+           enum fw3_table table, struct fw3_rule *rule,
+           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)
@@ -267,8 +278,8 @@ print_rule(enum fw3_table table, enum fw3_family family,
 }
 
 static void
-expand_rule(enum fw3_table table, enum fw3_family family,
-            struct fw3_rule *rule, int num)
+expand_rule(struct fw3_state *state, enum fw3_family family,
+            enum fw3_table table, struct fw3_rule *rule, int num)
 {
        struct fw3_protocol *proto;
        struct fw3_address *sip;
@@ -288,7 +299,7 @@ expand_rule(enum fw3_table table, enum fw3_family family,
        if (!fw3_is_family(rule, family))
                return;
 
-       if ((table == FW3_TABLE_RAW && rule->target != FW3_TARGET_NOTRACK) ||
+       if ((table == FW3_TABLE_RAW && rule->target != FW3_FLAG_NOTRACK) ||
            (table != FW3_TABLE_FILTER))
                return;
 
@@ -312,7 +323,7 @@ expand_rule(enum fw3_table table, enum fw3_family family,
                        return;
                }
 
-               setbit(rule->_ipset->flags, family);
+               set(rule->_ipset->flags, family, family);
        }
 
        list_for_each_entry(proto, &rule->proto, list)
@@ -337,18 +348,18 @@ expand_rule(enum fw3_table table, enum fw3_family family,
                fw3_foreach(dport, dports)
                fw3_foreach(mac, &rule->mac_src)
                fw3_foreach(icmptype, icmptypes)
-                       print_rule(table, family, rule, proto, sip, dip, sport, dport,
-                                  mac, icmptype);
+                       print_rule(state, family, table, rule, proto, sip, dip,
+                                  sport, dport, mac, icmptype);
        }
 }
 
 void
-fw3_print_rules(enum fw3_table table, enum fw3_family family,
-                struct fw3_state *state)
+fw3_print_rules(struct fw3_state *state, enum fw3_family family,
+                enum fw3_table table)
 {
        int num = 0;
        struct fw3_rule *rule;
 
        list_for_each_entry(rule, &state->rules, list)
-               expand_rule(table, family, rule, num++);
+               expand_rule(state, family, table, rule, num++);
 }