Increase compatibility to old firewall by initializing protocol of rules and redirect...
[project/firewall3.git] / redirects.c
index b42201f..7817e9b 100644 (file)
@@ -114,7 +114,7 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
        struct uci_element *e;
        struct fw3_redirect *redir;
 
-       bool valid = false;
+       bool valid;
 
        INIT_LIST_HEAD(&state->redirects);
 
@@ -138,6 +138,8 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                redir->enabled = true;
                redir->reflection = true;
 
+               valid = false;
+
                fw3_parse_options(redir, fw3_redirect_opts, s);
 
                if (!redir->enabled)
@@ -225,6 +227,8 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                                warn_elem(e, "has no destination specified");
                        else if (!redir->ip_dest.set)
                                warn_elem(e, "has no src_dip option specified");
+                       else if (!list_empty(&redir->mac_src))
+                               warn_elem(e, "must not use 'src_mac' option for SNAT target");
                        else
                        {
                                set(redir->_dest->flags, FW3_FAMILY_V4, redir->target);
@@ -233,6 +237,12 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                        }
                }
 
+               if (list_empty(&redir->proto))
+               {
+                       warn_elem(e, "does not specify a protocol, assuming TCP+UDP");
+                       fw3_parse_protocol(&redir->proto, "tcpudp", true);
+               }
+
                if (!valid)
                {
                        fw3_free_redirect(redir);
@@ -259,17 +269,18 @@ static void
 print_snat_dnat(enum fw3_flag target,
                 struct fw3_address *addr, struct fw3_port *port)
 {
-       const char *t;
        char s[sizeof("255.255.255.255 ")];
 
        if (target == FW3_FLAG_DNAT)
-               t = "DNAT --to-destination";
+               fw3_pr(" -j DNAT --to-destination ");
        else
-               t = "SNAT --to-source";
+               fw3_pr(" -j SNAT --to-source ");
 
-       inet_ntop(AF_INET, &addr->address.v4, s, sizeof(s));
-
-       fw3_pr(" -j %s %s", t, s);
+       if (addr && addr->set)
+       {
+               inet_ntop(AF_INET, &addr->address.v4, s, sizeof(s));
+               fw3_pr(s);
+       }
 
        if (port && port->set)
        {
@@ -322,8 +333,8 @@ print_target_filter(struct fw3_redirect *redir)
 }
 
 static void
-print_redirect(enum fw3_table table, enum fw3_family family,
-               struct fw3_redirect *redir, int num)
+print_redirect(struct fw3_state *state, enum fw3_family family,
+               enum fw3_table table, struct fw3_redirect *redir, int num)
 {
        struct list_head *ext_addrs, *int_addrs;
        struct fw3_address *ext_addr, *int_addr, ref_addr;
@@ -482,8 +493,8 @@ print_redirect(enum fw3_table table, enum fw3_family family,
 }
 
 void
-fw3_print_redirects(enum fw3_table table, enum fw3_family family,
-                    struct fw3_state *state)
+fw3_print_redirects(struct fw3_state *state, enum fw3_family family,
+                    enum fw3_table table)
 {
        int num = 0;
        struct fw3_redirect *redir;
@@ -495,5 +506,5 @@ fw3_print_redirects(enum fw3_table table, enum fw3_family family,
                return;
 
        list_for_each_entry(redir, &state->redirects, list)
-               print_redirect(table, family, redir, num++);
+               print_redirect(state, family, table, redir, num++);
 }