The branch main has been updated by rcm:

URL: 
https://cgit.FreeBSD.org/src/commit/?id=f260c85030414b783018bd3999747c59124425b1

commit f260c85030414b783018bd3999747c59124425b1
Author:     R. Christian McDonald <[email protected]>
AuthorDate: 2026-12-04 01:32:59 +0000
Commit:     R. Christian McDonald <[email protected]>
CommitDate: 2026-12-04 01:32:59 +0000

    ipfw_nat: Report a redirect that cannot be set up
    
    nat44_config() ignores the error from add_redir_spool_cfg(), which
    stops at the first redirect it cannot set up: one with an unknown mode,
    a port redirect without ports or with a protocol other than TCP, UDP or
    SCTP, or one for which libalias cannot allocate a link.  That redirect
    and the ones after it are dropped silently, but the instance keeps the
    redirect count of the request, so "ipfw nat N show config" reports more
    redirects than it returns and ipfw(8) misreads the reply ("unknown redir
    mode").  A port range that runs out of memory partway also leaves its
    first links in libalias.
    
    Return the error, count only the redirects that are set up, and delete
    the links of a port range that fails.  As before, the instance stays
    installed with the redirects ahead of the failed one.  Add a
    nat44_config test case for a redirect without ports.
    
    Reviewed by:            maxim
    Fixes:                  d6164b77f8b7 ("Make ipfw_nat module use IP_FW3 
codes.")
    MFC after:              1 week
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D62394
---
 sys/netpfil/ipfw/ip_fw_nat.c          | 17 +++++++----
 tests/sys/netpfil/ipfw/nat44_config.c | 55 +++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 6 deletions(-)

diff --git a/sys/netpfil/ipfw/ip_fw_nat.c b/sys/netpfil/ipfw/ip_fw_nat.c
index 0b69c69c88ba..39f4bbed7581 100644
--- a/sys/netpfil/ipfw/ip_fw_nat.c
+++ b/sys/netpfil/ipfw/ip_fw_nat.c
@@ -239,6 +239,9 @@ add_redir_spool_cfg(char *buf, struct cfg_nat *ptr)
                                    htons(remotePortCopy), r->paddr,
                                    htons(r->pport + i), r->proto);
                                if (r->alink[i] == NULL) {
+                                       while (i-- > 0)
+                                               LibAliasRedirectDelete(ptr->lib,
+                                                   r->alink[i]);
                                        r->alink[0] = NULL;
                                        break;
                                }
@@ -256,6 +259,7 @@ add_redir_spool_cfg(char *buf, struct cfg_nat *ptr)
                        printf("LibAliasRedirect* returned NULL\n");
                        free(r->alink, M_IPFW);
                        free(r, M_IPFW);
+                       ptr->redir_cnt = cnt;   /* the ones in redir_chain */
                        return (EINVAL);
                }
                /* LSNAT handling. */
@@ -495,11 +499,11 @@ lookup_nat_name(struct nat_list *l, char *name)
 
 /* IP_FW3 configuration routines */
 
-static void
+static int
 nat44_config(struct ip_fw_chain *chain, struct nat44_cfg_nat *ucfg)
 {
        struct cfg_nat *ptr, *tcfg;
-       int gencnt;
+       int error, gencnt;
 
        /*
         * Find/create nat rule.
@@ -545,7 +549,7 @@ nat44_config(struct ip_fw_chain *chain, struct 
nat44_cfg_nat *ucfg)
        /* Delete old cfgs. */
        del_redir_spool_cfg(ptr, &ptr->redir_chain);
        /* Add new entries. */
-       add_redir_spool_cfg((char *)(ucfg + 1), ptr);
+       error = add_redir_spool_cfg((char *)(ucfg + 1), ptr);
 
        /* Extra check to avoid race with another ipfw_nat_cfg() */
        tcfg = NULL;
@@ -562,6 +566,8 @@ nat44_config(struct ip_fw_chain *chain, struct 
nat44_cfg_nat *ucfg)
 
        if (tcfg != NULL)
                free_nat_instance(ptr);
+
+       return (error);
 }
 
 /*
@@ -634,8 +640,7 @@ nat44_cfg(struct ip_fw_chain *chain, ip_fw3_opheader *op3,
            ucfg->redir_cnt) != 0)
                return (EINVAL);
 
-       nat44_config(chain, ucfg);
-       return (0);
+       return (nat44_config(chain, ucfg));
 }
 
 /*
@@ -1033,7 +1038,7 @@ ipfw_nat_cfg(struct sockopt *sopt)
                goto out;
        }
 
-       nat44_config(&V_layer3_chain, ucfg);
+       error = nat44_config(&V_layer3_chain, ucfg);
 
 out:
        free(buf, M_TEMP);
diff --git a/tests/sys/netpfil/ipfw/nat44_config.c 
b/tests/sys/netpfil/ipfw/nat44_config.c
index 3eb5dff7bea7..5c87ade394e1 100644
--- a/tests/sys/netpfil/ipfw/nat44_config.c
+++ b/tests/sys/netpfil/ipfw/nat44_config.c
@@ -324,6 +324,60 @@ ATF_TC_BODY(reject_keeps_config, tc)
        nat_destroy(s, "105");
 }
 
+/* Read the instance back; check its size and return its redir_cnt. */
+static uint32_t
+nat_redir_cnt(int s, const char *name)
+{
+       struct nat44_cfg_nat *ucfg;
+       ipfw_obj_header *oh;
+       socklen_t sz;
+       char *buf;
+       uint32_t cnt;
+
+       sz = sizeof(*oh) + sizeof(*ucfg) + 4 * sizeof(struct nat44_cfg_redir);
+       buf = calloc(1, sz);
+       ATF_REQUIRE(buf != NULL);
+       oh = (ipfw_obj_header *)(void *)buf;
+       fill_oh(oh, IP_FW_NAT44_XGETCONFIG, name);
+       ucfg = (struct nat44_cfg_nat *)(oh + 1);
+       strlcpy(ucfg->name, name, sizeof(ucfg->name));
+       ATF_REQUIRE_MSG(getsockopt(s, IPPROTO_IP, IP_FW3, buf, &sz) == 0,
+           "XGETCONFIG: %s", strerror(errno));
+       cnt = ucfg->redir_cnt;
+       ATF_CHECK_EQ_MSG(sizeof(*oh) + sizeof(*ucfg) +
+           cnt * sizeof(struct nat44_cfg_redir), ucfg->size,
+           "%u redirs in a reply of %u bytes", cnt, ucfg->size);
+       free(buf);
+       return (cnt);
+}
+
+ATF_TC(bad_redir_reported);
+ATF_TC_HEAD(bad_redir_reported, tc)
+{
+       nat_head(tc, "A redir that cannot be set up is reported, and the "
+           "instance counts only the redirs ahead of it");
+}
+ATF_TC_BODY(bad_redir_reported, tc)
+{
+       struct nat44_cfg_redir *r;
+       struct req rq;
+       int error, s;
+
+       s = fw_socket();
+       /* A redirect_addr, then a redirect_port without ports. */
+       req_init(&rq, "106", 2, 2 * sizeof(struct nat44_cfg_redir));
+       req_add_redir(&rq, 0);
+       r = (struct nat44_cfg_redir *)(void *)(rq.buf + rq.len);
+       req_add_redir(&rq, 0);
+       r->mode = NAT44_REDIR_PORT;
+       r->pport_cnt = 0;
+       error = req_send(s, &rq);
+       ATF_CHECK_EQ_MSG(EINVAL, error, "XCONFIG returned %d (%s)", error,
+           strerror(error));
+       ATF_CHECK_EQ(1, nat_redir_cnt(s, "106"));
+       nat_destroy(s, "106");
+}
+
 ATF_TP_ADD_TCS(tp)
 {
        ATF_TP_ADD_TC(tp, spool_missing);
@@ -331,6 +385,7 @@ ATF_TP_ADD_TCS(tp)
        ATF_TP_ADD_TC(tp, redir_cnt_overflow);
        ATF_TP_ADD_TC(tp, spool_roundtrip);
        ATF_TP_ADD_TC(tp, reject_keeps_config);
+       ATF_TP_ADD_TC(tp, bad_redir_reported);
 
        return (atf_no_error());
 }

Reply via email to