Add common fw3_address_to_string() helper function
[project/firewall3.git] / redirects.c
index 7cd26c8..6ea91b8 100644 (file)
@@ -52,7 +52,11 @@ const struct fw3_option fw3_redirect_opts[] = {
        FW3_OPT("weekdays",            weekdays,  redirect,     time.weekdays),
        FW3_OPT("monthdays",           monthdays, redirect,     time.monthdays),
 
+       FW3_OPT("mark",                mark,      redirect,     mark),
+
        FW3_OPT("reflection",          bool,      redirect,     reflection),
+       FW3_OPT("reflection_src",      reflection_source,
+                                                 redirect,     reflection_src),
 
        FW3_OPT("target",              target,    redirect,     target),
 
@@ -112,7 +116,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);
 
@@ -136,6 +140,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)
@@ -151,14 +157,14 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                        continue;
                }
                else if (redir->src.set && !redir->src.any &&
-                        !(redir->_src = fw3_lookup_zone(state, redir->src.name, false)))
+                        !(redir->_src = fw3_lookup_zone(state, redir->src.name)))
                {
                        warn_elem(e, "refers to not existing zone '%s'", redir->src.name);
                        fw3_free_redirect(redir);
                        continue;
                }
                else if (redir->dest.set && !redir->dest.any &&
-                        !(redir->_dest = fw3_lookup_zone(state, redir->dest.name, false)))
+                        !(redir->_dest = fw3_lookup_zone(state, redir->dest.name)))
                {
                        warn_elem(e, "refers to not existing zone '%s'", redir->dest.name);
                        fw3_free_redirect(redir);
@@ -171,7 +177,7 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                        continue;
                }
                else if (redir->ipset.set && !redir->ipset.any &&
-                        !(redir->_ipset = fw3_lookup_ipset(state, redir->ipset.name, false)))
+                        !(redir->_ipset = fw3_lookup_ipset(state, redir->ipset.name)))
                {
                        warn_elem(e, "refers to unknown ipset '%s'", redir->ipset.name);
                        fw3_free_redirect(redir);
@@ -184,18 +190,18 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                        continue;
                }
 
-               if (redir->target == FW3_TARGET_UNSPEC)
+               if (redir->target == FW3_FLAG_UNSPEC)
                {
                        warn_elem(e, "has no target specified, defaulting to DNAT");
-                       redir->target = FW3_TARGET_DNAT;
+                       redir->target = FW3_FLAG_DNAT;
                }
-               else if (redir->target < FW3_TARGET_DNAT)
+               else if (redir->target < FW3_FLAG_DNAT)
                {
                        warn_elem(e, "has invalid target specified, defaulting to DNAT");
-                       redir->target = FW3_TARGET_DNAT;
+                       redir->target = FW3_FLAG_DNAT;
                }
 
-               if (redir->target == FW3_TARGET_DNAT)
+               if (redir->target == FW3_FLAG_DNAT)
                {
                        if (redir->src.any)
                                warn_elem(e, "must not have source '*' for DNAT target");
@@ -210,9 +216,9 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
 
                        if (redir->reflection && redir->_dest && redir->_src->masq)
                        {
-                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_TARGET_ACCEPT);
-                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_TARGET_DNAT);
-                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_TARGET_SNAT);
+                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_ACCEPT);
+                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_DNAT);
+                               set(redir->_dest->flags, FW3_FAMILY_V4, FW3_FLAG_SNAT);
                        }
                }
                else
@@ -223,6 +229,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);
@@ -231,6 +239,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);
@@ -247,27 +261,28 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
 static void
 print_chain_nat(struct fw3_redirect *redir)
 {
-       if (redir->target == FW3_TARGET_DNAT)
+       if (redir->target == FW3_FLAG_DNAT)
                fw3_pr("-A zone_%s_prerouting", redir->src.name);
        else
                fw3_pr("-A zone_%s_postrouting", redir->dest.name);
 }
 
 static void
-print_snat_dnat(enum fw3_target target,
+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_TARGET_DNAT)
-               t = "DNAT --to-destination";
+       if (target == FW3_FLAG_DNAT)
+               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)
        {
@@ -283,7 +298,7 @@ print_snat_dnat(enum fw3_target target,
 static void
 print_target_nat(struct fw3_redirect *redir)
 {
-       if (redir->target == FW3_TARGET_DNAT)
+       if (redir->target == FW3_FLAG_DNAT)
                print_snat_dnat(redir->target, &redir->ip_redir, &redir->port_redir);
        else
                print_snat_dnat(redir->target, &redir->ip_dest, &redir->port_dest);
@@ -292,7 +307,7 @@ print_target_nat(struct fw3_redirect *redir)
 static void
 print_chain_filter(struct fw3_redirect *redir)
 {
-       if (redir->target == FW3_TARGET_DNAT)
+       if (redir->target == FW3_FLAG_DNAT)
        {
                /* XXX: check for local ip */
                if (!redir->ip_redir.set)
@@ -313,18 +328,18 @@ static void
 print_target_filter(struct fw3_redirect *redir)
 {
        /* XXX: check for local ip */
-       if (redir->target == FW3_TARGET_DNAT && !redir->ip_redir.set)
+       if (redir->target == FW3_FLAG_DNAT && !redir->ip_redir.set)
                fw3_pr(" -m conntrack --ctstate DNAT -j ACCEPT\n");
        else
                fw3_pr(" -j ACCEPT\n");
 }
 
 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;
+       struct fw3_address *ext_addr, *int_addr, ref_addr;
        struct fw3_device *ext_net, *int_net;
        struct fw3_protocol *proto;
        struct fw3_mac *mac;
@@ -369,7 +384,7 @@ print_redirect(enum fw3_table table, enum fw3_family family,
                        fw3_format_ipset(redir->_ipset, redir->ipset.invert);
                        fw3_format_protocol(proto, family);
 
-                       if (redir->target == FW3_TARGET_DNAT)
+                       if (redir->target == FW3_FLAG_DNAT)
                        {
                                fw3_format_src_dest(&redir->ip_src, &redir->ip_dest);
                                fw3_format_sport_dport(&redir->port_src, &redir->port_dest);
@@ -382,6 +397,7 @@ print_redirect(enum fw3_table table, enum fw3_family family,
 
                        fw3_format_mac(mac);
                        fw3_format_time(&redir->time);
+                       fw3_format_mark(&redir->mark);
                        fw3_format_extra(redir->extra);
                        fw3_format_comment(redir->name);
                        print_target_nat(redir);
@@ -395,6 +411,7 @@ print_redirect(enum fw3_table table, enum fw3_family family,
                        fw3_format_sport_dport(&redir->port_src, &redir->port_redir);
                        fw3_format_mac(mac);
                        fw3_format_time(&redir->time);
+                       fw3_format_mark(&redir->mark);
                        fw3_format_extra(redir->extra);
                        fw3_format_comment(redir->name);
                        print_target_filter(redir);
@@ -402,7 +419,7 @@ print_redirect(enum fw3_table table, enum fw3_family family,
        }
 
        /* reflection rules */
-       if (redir->target != FW3_TARGET_DNAT || !redir->reflection)
+       if (redir->target != FW3_FLAG_DNAT || !redir->reflection)
                return;
 
        if (!redir->_dest || !redir->_src->masq)
@@ -433,6 +450,12 @@ print_redirect(enum fw3_table table, enum fw3_family family,
                                if (!proto || (proto->protocol != 6 && proto->protocol != 17))
                                        continue;
 
+                               if (redir->reflection_src == FW3_REFLECTION_INTERNAL)
+                                       ref_addr = *int_addr;
+                               else
+                                       ref_addr = *ext_addr;
+
+                               ref_addr.mask = 32;
                                ext_addr->mask = 32;
 
                                if (table == FW3_TABLE_NAT)
@@ -443,7 +466,7 @@ print_redirect(enum fw3_table table, enum fw3_family family,
                                        fw3_format_sport_dport(NULL, &redir->port_dest);
                                        fw3_format_time(&redir->time);
                                        fw3_format_comment(redir->name, " (reflection)");
-                                       print_snat_dnat(FW3_TARGET_DNAT,
+                                       print_snat_dnat(FW3_FLAG_DNAT,
                                                        &redir->ip_redir, &redir->port_redir);
 
                                        fw3_pr("-A zone_%s_postrouting", redir->dest.name);
@@ -452,7 +475,7 @@ print_redirect(enum fw3_table table, enum fw3_family family,
                                        fw3_format_sport_dport(NULL, &redir->port_redir);
                                        fw3_format_time(&redir->time);
                                        fw3_format_comment(redir->name, " (reflection)");
-                                       print_snat_dnat(FW3_TARGET_SNAT, ext_addr, NULL);
+                                       print_snat_dnat(FW3_FLAG_SNAT, &ref_addr, NULL);
                                }
                                else if (table == FW3_TABLE_FILTER)
                                {
@@ -474,8 +497,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;
@@ -487,5 +510,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++);
 }