The branch stable/15 has been updated by kp:

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

commit 9de06f27b140660933f95ba50db452202cbea7fe
Author:     Kristof Provost <[email protected]>
AuthorDate: 2026-06-11 14:58:20 +0000
Commit:     Kristof Provost <[email protected]>
CommitDate: 2026-06-26 23:02:05 +0000

    libpfctl: fix memory leak
    
    When we snl_init_writer() we allocate memory in the struct snl_state in the 
struct pfctl_handle.
    This memory was never released again, leading to a memory leak. We still
    had a reference to the memory and would release it on pfctl_close()
    (so valgrind did not detect it as a leak), but long-lived users (e.g.
    bsnmpd) would eventually run out of memory.
    
    Explicitly reset the snl_state when we're done to prevent this.
    
    MFC after:      2 weeks
    Sponsored by:   Rubicon Communications, LLC ("Netgate")
    
    (cherry picked from commit fcb31b57112425a4eb64241651a0206108105298)
---
 lib/libpfctl/libpfctl.c | 434 ++++++++++++++++++++++++++++++++++--------------
 1 file changed, 308 insertions(+), 126 deletions(-)

diff --git a/lib/libpfctl/libpfctl.c b/lib/libpfctl/libpfctl.c
index a97ca92e5144..35d2a31906bf 100644
--- a/lib/libpfctl/libpfctl.c
+++ b/lib/libpfctl/libpfctl.c
@@ -125,15 +125,22 @@ pfctl_do_netlink_cmd(struct pfctl_handle *h, uint cmd)
        hdr = snl_create_genl_msg_request(&nw, h->family_id, cmd);
 
        hdr = snl_finalize_msg(&nw);
-       if (hdr == NULL)
-               return (ENOMEM);
+       if (hdr == NULL) {
+               e.error = ENOMEM;
+               goto out;
+       }
        seq_id = hdr->nlmsg_seq;
 
-       snl_send_message(&h->ss, hdr);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -402,7 +409,7 @@ static const struct snl_hdr_parser *stat_parser[] = {
 struct pfctl_status *
 pfctl_get_status_h(struct pfctl_handle *h)
 {
-       struct pfctl_status     *status;
+       struct pfctl_status     *status = NULL;
        struct snl_errmsg_data e = {};
        struct nlmsghdr *hdr;
        struct snl_writer nw;
@@ -416,17 +423,17 @@ pfctl_get_status_h(struct pfctl_handle *h)
        hdr->nlmsg_flags |= NLM_F_DUMP;
 
        hdr = snl_finalize_msg(&nw);
-       if (hdr == NULL) {
-               return (NULL);
-       }
+       if (hdr == NULL)
+               goto out;
 
        seq_id = hdr->nlmsg_seq;
        if (! snl_send_message(&h->ss, hdr))
-               return (NULL);
+               goto out;
 
        status = calloc(1, sizeof(*status));
        if (status == NULL)
-               return (NULL);
+               goto out;
+
        TAILQ_INIT(&status->counters);
        TAILQ_INIT(&status->lcounters);
        TAILQ_INIT(&status->fcounters);
@@ -437,6 +444,8 @@ pfctl_get_status_h(struct pfctl_handle *h)
                        continue;
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (status);
 }
 
@@ -1350,17 +1359,23 @@ pfctl_add_rule_h(struct pfctl_handle *h, const struct 
pfctl_rule *r,
 
        snl_add_msg_attr_pf_rule(&nw, PF_ART_RULE, r);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -1392,18 +1407,24 @@ pfctl_get_rules_info_h(struct pfctl_handle *h, struct 
pfctl_rules_info *rules, u
        snl_add_msg_attr_u8(&nw, PF_GR_ACTION, ruleset);
 
        hdr = snl_finalize_msg(&nw);
-       if (hdr == NULL)
-               return (ENOMEM);
+       if (hdr == NULL) {
+               e.error = ENOMEM;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &getrules_parser, rules))
                        continue;
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -1717,12 +1738,16 @@ pfctl_get_clear_rule_h(struct pfctl_handle *h, uint32_t 
nr, uint32_t ticket,
        snl_add_msg_attr_u8(&nw, PF_GR_CLEAR, clear);
 
        hdr = snl_finalize_msg(&nw);
-       if (hdr == NULL)
-               return (ENOMEM);
+       if (hdr == NULL) {
+               e.error = ENOMEM;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &getrule_parser, &attrs))
@@ -1732,6 +1757,8 @@ pfctl_get_clear_rule_h(struct pfctl_handle *h, uint32_t 
nr, uint32_t ticket,
        memcpy(rule, &attrs.r, sizeof(attrs.r));
        strlcpy(anchor_call, attrs.anchor_call, MAXPATHLEN);
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -1806,6 +1833,7 @@ SNL_DECLARE_PARSER(creator_parser, struct genlmsghdr, 
snl_f_p_empty, ap_creators
 int
 pfctl_get_creatorids(struct pfctl_handle *h, uint32_t *creators, size_t *len)
 {
+       struct snl_errmsg_data e = {};
        struct nlmsghdr *hdr;
        struct snl_writer nw;
        size_t i = 0;
@@ -1815,13 +1843,18 @@ pfctl_get_creatorids(struct pfctl_handle *h, uint32_t 
*creators, size_t *len)
            PFNL_CMD_GETCREATORS);
        hdr->nlmsg_flags |= NLM_F_DUMP;
        hdr = snl_finalize_msg(&nw);
-       if (hdr == NULL)
-               return (ENOMEM);
+       if (hdr == NULL) {
+               e.error = ENOMEM;
+               goto out;
+       }
+
        uint32_t seq_id = hdr->nlmsg_seq;
 
-       snl_send_message(&h->ss, hdr);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
-       struct snl_errmsg_data e = {};
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                struct pfctl_creator c;
                bzero(&c, sizeof(c));
@@ -1831,13 +1864,17 @@ pfctl_get_creatorids(struct pfctl_handle *h, uint32_t 
*creators, size_t *len)
 
                creators[i] = c.id;
                i++;
-               if (i > *len)
-                       return (E2BIG);
+               if (i > *len) {
+                       e.error = E2BIG;
+                       goto out;
+               }
        }
 
        *len = i;
 
-       return (0);
+out:
+       snl_clear_lb(&h->ss);
+       return (e.error);
 }
 
 static inline bool
@@ -1951,12 +1988,17 @@ pfctl_get_states_nl(struct pfctl_state_filter *filter, 
struct snl_state *ss, pfc
        snl_add_msg_attr_ip6(&nw, PF_ST_FILTER_MASK, &filter->mask.v6);
 
        hdr = snl_finalize_msg(&nw);
-       if (hdr == NULL)
-               return (ENOMEM);
+       if (hdr == NULL) {
+               ret = ENOMEM;
+               goto out;
+       }
 
        uint32_t seq_id = hdr->nlmsg_seq;
 
-       snl_send_message(ss, hdr);
+       if (! snl_send_message(ss, hdr)) {
+               ret = ENXIO;
+               goto out;
+       }
 
        struct snl_errmsg_data e = {};
        while ((hdr = snl_read_reply_multi(ss, seq_id, &e)) != NULL) {
@@ -1966,11 +2008,15 @@ pfctl_get_states_nl(struct pfctl_state_filter *filter, 
struct snl_state *ss, pfc
                        continue;
 
                ret = f(&s, arg);
-               if (ret != 0)
-                       return (ret);
+               if (ret != 0) {
+                       goto out;
+               }
        }
 
-       return (0);
+       ret = e.error;
+out:
+       snl_clear_lb(ss);
+       return (ret);
 }
 
 int
@@ -2076,13 +2122,17 @@ _pfctl_clear_states_h(struct pfctl_handle *h, const 
struct pfctl_kill *kill,
        snl_add_msg_attr_bool(&nw, PF_CS_KILL_MATCH, kill->kill_match);
        snl_add_msg_attr_bool(&nw, PF_CS_NAT, kill->nat);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &clear_states_parser, 
&attrs))
@@ -2092,6 +2142,8 @@ _pfctl_clear_states_h(struct pfctl_handle *h, const 
struct pfctl_kill *kill,
        if (killed)
                *killed = attrs.killed;
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2424,12 +2476,16 @@ _pfctl_table_add_addrs_h(struct pfctl_handle *h, struct 
pfr_table *tbl, struct p
        for (int i = 0; i < size; i++)
                snl_add_msg_attr_pfr_addr(&nw, PF_TA_ADDR, &addrs[i]);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &table_add_addr_parser, 
&added))
@@ -2439,6 +2495,8 @@ _pfctl_table_add_addrs_h(struct pfctl_handle *h, struct 
pfr_table *tbl, struct p
        if (nadd)
                *nadd = added;
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2487,12 +2545,16 @@ _pfctl_table_del_addrs_h(struct pfctl_handle *h, struct 
pfr_table *tbl, struct p
        for (int i = 0; i < size; i++)
                snl_add_msg_attr_pfr_addr(&nw, PF_TA_ADDR, &addrs[i]);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &table_del_addr_parser, 
&deleted))
@@ -2502,6 +2564,8 @@ _pfctl_table_del_addrs_h(struct pfctl_handle *h, struct 
pfr_table *tbl, struct p
        if (ndel)
                *ndel = deleted;
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2615,17 +2679,23 @@ pfctl_set_statusif(struct pfctl_handle *h, const char 
*ifname)
 
        snl_add_msg_attr_string(&nw, PF_SS_IFNAME, ifname);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2663,19 +2733,25 @@ pfctl_natlook(struct pfctl_handle *h, const struct 
pfctl_natlook_key *k,
        snl_add_msg_attr_u16(&nw, PF_NL_SRC_PORT, k->sport);
        snl_add_msg_attr_u16(&nw, PF_NL_DST_PORT, k->dport);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &natlook_parser, r))
                        continue;
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2693,17 +2769,23 @@ pfctl_set_debug(struct pfctl_handle *h, uint32_t level)
 
        snl_add_msg_attr_u32(&nw, PF_SD_LEVEL, level);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2722,17 +2804,23 @@ pfctl_set_timeout(struct pfctl_handle *h, uint32_t 
timeout, uint32_t seconds)
        snl_add_msg_attr_u32(&nw, PF_TO_TIMEOUT, timeout);
        snl_add_msg_attr_u32(&nw, PF_TO_SECONDS, seconds);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2762,13 +2850,17 @@ pfctl_get_timeout(struct pfctl_handle *h, uint32_t 
timeout, uint32_t *seconds)
 
        snl_add_msg_attr_u32(&nw, PF_TO_TIMEOUT, timeout);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &get_timeout_parser, &to))
@@ -2778,6 +2870,8 @@ pfctl_get_timeout(struct pfctl_handle *h, uint32_t 
timeout, uint32_t *seconds)
        if (seconds != NULL)
                *seconds = to.seconds;
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2796,17 +2890,23 @@ pfctl_set_limit(struct pfctl_handle *h, const int 
index, const uint limit)
        snl_add_msg_attr_u32(&nw, PF_LI_INDEX, index);
        snl_add_msg_attr_u32(&nw, PF_LI_LIMIT, limit);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2836,13 +2936,17 @@ pfctl_get_limit(struct pfctl_handle *h, const int 
index, uint *limit)
 
        snl_add_msg_attr_u32(&nw, PF_LI_INDEX, index);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &get_limit_parser, &li))
@@ -2852,6 +2956,8 @@ pfctl_get_limit(struct pfctl_handle *h, const int index, 
uint *limit)
        if (limit != NULL)
                *limit = li.limit;
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2879,13 +2985,17 @@ pfctl_begin_addrs(struct pfctl_handle *h, uint32_t 
*ticket)
            PFNL_CMD_BEGIN_ADDRS);
        hdr->nlmsg_flags |= NLM_F_DUMP;
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &begin_addrs_parser, &attrs))
@@ -2895,6 +3005,8 @@ pfctl_begin_addrs(struct pfctl_handle *h, uint32_t 
*ticket)
        if (ticket != NULL)
                *ticket = attrs.ticket;
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2921,17 +3033,23 @@ pfctl_add_addr(struct pfctl_handle *h, const struct 
pfioc_pooladdr *pa, int whic
        snl_add_msg_attr_pool_addr(&nw, PF_AA_ADDR, &pa->addr);
        snl_add_msg_attr_u32(&nw, PF_AA_WHICH, which);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -2959,19 +3077,24 @@ pfctl_get_addrs(struct pfctl_handle *h, uint32_t 
ticket, uint32_t r_num,
        snl_add_msg_attr_string(&nw, PF_AA_ANCHOR, anchor);
        snl_add_msg_attr_u32(&nw, PF_AA_WHICH, which);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &get_addrs_parser, nr))
                        continue;
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -3019,19 +3142,24 @@ pfctl_get_addr(struct pfctl_handle *h, uint32_t ticket, 
uint32_t r_num,
        snl_add_msg_attr_u32(&nw, PF_AA_NR, nr);
        snl_add_msg_attr_u32(&nw, PF_AA_WHICH, which);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &get_addr_parser, pa))
                        continue;
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -3058,13 +3186,17 @@ pfctl_get_rulesets(struct pfctl_handle *h, const char 
*path, uint32_t *nr)
 
        snl_add_msg_attr_string(&nw, PF_RS_PATH, path);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &ruleset_parser, &rs))
@@ -3073,6 +3205,8 @@ pfctl_get_rulesets(struct pfctl_handle *h, const char 
*path, uint32_t *nr)
 
        *nr = rs.nr;
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -3091,13 +3225,16 @@ pfctl_get_ruleset(struct pfctl_handle *h, const char 
*path, uint32_t nr, struct
        snl_add_msg_attr_string(&nw, PF_RS_PATH, path);
        snl_add_msg_attr_u32(&nw, PF_RS_NR, nr);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (! snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (! snl_parse_nlmsg(&h->ss, hdr, &ruleset_parser, rs))
@@ -3107,6 +3244,8 @@ pfctl_get_ruleset(struct pfctl_handle *h, const char 
*path, uint32_t nr, struct
        rs->nr = nr;
        strlcpy(rs->path, path, sizeof(rs->path));
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -3146,13 +3285,17 @@ pfctl_get_srcnodes(struct pfctl_handle *h, 
pfctl_get_srcnode_fn fn, void *arg)
        hdr = snl_create_genl_msg_request(&nw, h->family_id,
            PFNL_CMD_GET_SRCNODES);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (!snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (!snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                bzero(&sn, sizeof(sn));
@@ -3164,6 +3307,8 @@ pfctl_get_srcnodes(struct pfctl_handle *h, 
pfctl_get_srcnode_fn fn, void *arg)
                        return (ret);
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -3190,19 +3335,25 @@ pfctl_clear_tables(struct pfctl_handle *h, struct 
pfr_table *filter,
        snl_add_msg_attr_u32(&nw, PF_T_TABLE_FLAGS, filter->pfrt_flags);
        snl_add_msg_attr_u32(&nw, PF_T_FLAGS, flags);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (!snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (!snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (!snl_parse_nlmsg(&h->ss, hdr, &ndel_parser, ndel))
                        continue;
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -3228,19 +3379,25 @@ pfctl_add_table(struct pfctl_handle *h, struct 
pfr_table *table,
        snl_add_msg_attr_u32(&nw, PF_T_TABLE_FLAGS, table->pfrt_flags);
        snl_add_msg_attr_u32(&nw, PF_T_FLAGS, flags);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (!snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (!snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (!snl_parse_nlmsg(&h->ss, hdr, &nadd_parser, nadd))
                        continue;
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -3262,19 +3419,26 @@ pfctl_del_table(struct pfctl_handle *h, struct 
pfr_table *table,
        snl_add_msg_attr_u32(&nw, PF_T_TABLE_FLAGS, table->pfrt_flags);
        snl_add_msg_attr_u32(&nw, PF_T_FLAGS, flags);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       hdr = snl_finalize_msg(&nw);
+       if (hdr == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (!snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (!snl_parse_nlmsg(&h->ss, hdr, &ndel_parser, ndel))
                        continue;
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -3343,13 +3507,17 @@ pfctl_get_tstats(struct pfctl_handle *h, const struct 
pfr_table *filter,
        snl_add_msg_attr_string(&nw, PF_T_NAME, filter->pfrt_name);
        snl_add_msg_attr_u32(&nw, PF_T_TABLE_FLAGS, filter->pfrt_flags);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (!snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                struct pfr_tstats tstats = {};
@@ -3362,6 +3530,8 @@ pfctl_get_tstats(struct pfctl_handle *h, const struct 
pfr_table *filter,
                        break;
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -3389,13 +3559,17 @@ pfctl_clear_tstats(struct pfctl_handle *h, const struct 
pfr_table *filter,
        snl_add_msg_attr_u32(&nw, PF_T_TABLE_FLAGS, filter->pfrt_flags);
        snl_add_msg_attr_u32(&nw, PF_T_FLAGS, flags);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (!snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (!snl_parse_nlmsg(&h->ss, hdr, &tstats_clr_parser, &zero))
@@ -3404,6 +3578,8 @@ pfctl_clear_tstats(struct pfctl_handle *h, const struct 
pfr_table *filter,
                        *nzero = (uint32_t)zero;
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 
@@ -3431,13 +3607,17 @@ pfctl_clear_addrs(struct pfctl_handle *h, const struct 
pfr_table *filter,
        snl_add_msg_attr_u32(&nw, PF_T_TABLE_FLAGS, filter->pfrt_flags);
        snl_add_msg_attr_u32(&nw, PF_T_FLAGS, flags);
 
-       if ((hdr = snl_finalize_msg(&nw)) == NULL)
-               return (ENXIO);
+       if ((hdr = snl_finalize_msg(&nw)) == NULL) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        seq_id = hdr->nlmsg_seq;
 
-       if (!snl_send_message(&h->ss, hdr))
-               return (ENXIO);
+       if (! snl_send_message(&h->ss, hdr)) {
+               e.error = ENXIO;
+               goto out;
+       }
 
        while ((hdr = snl_read_reply_multi(&h->ss, seq_id, &e)) != NULL) {
                if (!snl_parse_nlmsg(&h->ss, hdr, &clr_addrs_parser, &del))
@@ -3446,6 +3626,8 @@ pfctl_clear_addrs(struct pfctl_handle *h, const struct 
pfr_table *filter,
                        *ndel = (uint32_t)del;
        }
 
+out:
+       snl_clear_lb(&h->ss);
        return (e.error);
 }
 

Reply via email to