Make sure that NOTRACK is linked into firewall3 if it is part of libext*.a
[project/firewall3.git] / redirects.c
index 6d33f90..bb5e1ff 100644 (file)
@@ -44,6 +44,9 @@ const struct fw3_option fw3_redirect_opts[] = {
 
        FW3_OPT("extra",               string,    redirect,     extra),
 
+       FW3_OPT("limit",               limit,     redirect,     limit),
+       FW3_OPT("limit_burst",         int,       redirect,     limit.burst),
+
        FW3_OPT("utc_time",            bool,      redirect,     time.utc),
        FW3_OPT("start_date",          date,      redirect,     time.datestart),
        FW3_OPT("stop_date",           date,      redirect,     time.datestop),
@@ -115,10 +118,10 @@ compare_addr(struct fw3_address *a, struct fw3_address *b)
 {
        uint32_t mask;
 
-       if (a->family != FW3_FAMILY_V4)
+       if (a->family != FW3_FAMILY_V4 || b->family != FW3_FAMILY_V4)
                return false;
 
-       mask = ~((1 << (32 - a->mask)) - 1);
+       mask = htonl(~((1 << (32 - a->mask)) - 1));
 
        return ((a->address.v4.s_addr & mask) == (b->address.v4.s_addr & mask));
 }
@@ -162,6 +165,55 @@ resolve_dest(struct uci_element *e, struct fw3_redirect *redir,
        return false;
 }
 
+static bool
+check_local(struct uci_element *e, struct fw3_redirect *redir,
+            struct fw3_state *state)
+{
+       struct fw3_zone *zone;
+       struct fw3_device *net;
+       struct fw3_address *addr;
+       struct list_head *addrs;
+
+       if (redir->target != FW3_FLAG_DNAT)
+               return false;
+
+       if (!redir->ip_redir.set)
+               redir->local = true;
+
+       if (redir->local)
+               return true;
+
+       list_for_each_entry(zone, &state->zones, list)
+       {
+               list_for_each_entry(net, &zone->networks, list)
+               {
+                       addrs = fw3_ubus_address(net->name);
+
+                       if (!addrs)
+                               continue;
+
+                       list_for_each_entry(addr, addrs, list)
+                       {
+                               if (!compare_addr(&redir->ip_redir, addr))
+                                       continue;
+
+                               warn_elem(e, "refers to a destination address on this router, "
+                                            "assuming port redirection");
+
+                               redir->local = true;
+                               break;
+                       }
+
+                       fw3_free_list(addrs);
+
+                       if (redir->local)
+                               return true;
+               }
+       }
+
+       return false;
+}
+
 void
 fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
 {
@@ -267,7 +319,8 @@ fw3_load_redirects(struct fw3_state *state, struct uci_package *p)
                                valid = true;
                        }
 
-                       if (!redir->dest.set && resolve_dest(e, redir, state))
+                       if (!check_local(e, redir, state) && !redir->dest.set &&
+                           resolve_dest(e, redir, state))
                        {
                                warn_elem(e, "does not specify a destination, assuming '%s'",
                                          redir->dest.name);
@@ -374,8 +427,7 @@ append_chain_filter(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
 {
        if (redir->target == FW3_FLAG_DNAT)
        {
-               /* XXX: check for local ip */
-               if (!redir->ip_redir.set)
+               if (redir->local)
                        fw3_ipt_rule_append(r, "zone_%s_input", redir->src.name);
                else
                        fw3_ipt_rule_append(r, "zone_%s_forward", redir->src.name);
@@ -392,8 +444,7 @@ append_chain_filter(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
 static void
 set_target_filter(struct fw3_ipt_rule *r, struct fw3_redirect *redir)
 {
-       /* XXX: check for local ip */
-       if (redir->target == FW3_FLAG_DNAT && !redir->ip_redir.set)
+       if (redir->local)
                fw3_ipt_rule_extra(r, "-m conntrack --ctstate DNAT");
 
        fw3_ipt_rule_target(r, "ACCEPT");
@@ -445,6 +496,7 @@ print_redirect(struct fw3_ipt_handle *h, struct fw3_state *state,
                fw3_ipt_rule_sport_dport(r, spt, dpt);
                fw3_ipt_rule_mac(r, mac);
                fw3_ipt_rule_ipset(r, &redir->ipset);
+               fw3_ipt_rule_limit(r, &redir->limit);
                fw3_ipt_rule_time(r, &redir->time);
                fw3_ipt_rule_mark(r, &redir->mark);
                set_target_nat(r, redir);
@@ -463,6 +515,7 @@ print_redirect(struct fw3_ipt_handle *h, struct fw3_state *state,
                fw3_ipt_rule_sport_dport(r, spt, dpt);
                fw3_ipt_rule_mac(r, mac);
                fw3_ipt_rule_ipset(r, &redir->ipset);
+               fw3_ipt_rule_limit(r, &redir->limit);
                fw3_ipt_rule_time(r, &redir->time);
                fw3_ipt_rule_mark(r, &redir->mark);
                set_target_filter(r, redir);
@@ -489,6 +542,7 @@ print_reflection(struct fw3_ipt_handle *h, struct fw3_state *state,
        case FW3_TABLE_NAT:
                r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, ea);
                fw3_ipt_rule_sport_dport(r, NULL, &redir->port_dest);
+               fw3_ipt_rule_limit(r, &redir->limit);
                fw3_ipt_rule_time(r, &redir->time);
                set_comment(r, redir->name, num, true);
                set_snat_dnat(r, FW3_FLAG_DNAT, &redir->ip_redir, &redir->port_redir);
@@ -496,6 +550,7 @@ print_reflection(struct fw3_ipt_handle *h, struct fw3_state *state,
 
                r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, &redir->ip_redir);
                fw3_ipt_rule_sport_dport(r, NULL, &redir->port_redir);
+               fw3_ipt_rule_limit(r, &redir->limit);
                fw3_ipt_rule_time(r, &redir->time);
                set_comment(r, redir->name, num, true);
                set_snat_dnat(r, FW3_FLAG_SNAT, ra, NULL);
@@ -505,6 +560,7 @@ print_reflection(struct fw3_ipt_handle *h, struct fw3_state *state,
        case FW3_TABLE_FILTER:
                r = fw3_ipt_rule_create(h, proto, NULL, NULL, ia, &redir->ip_redir);
                fw3_ipt_rule_sport_dport(r, NULL, &redir->port_redir);
+               fw3_ipt_rule_limit(r, &redir->limit);
                fw3_ipt_rule_time(r, &redir->time);
                set_comment(r, redir->name, num, true);
                fw3_ipt_rule_target(r, "zone_%s_dest_ACCEPT", redir->dest.name);