git: 948598a0d8e9 - main - syslogd: Create one forwarding socket per address pair

From: R. Christian McDonald <rcm_at_FreeBSD.org>
Date: Fri, 27 Nov 2026 01:49:15 UTC
The branch main has been updated by rcm:

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

commit 948598a0d8e9aa992409c4d763ed235117e92020
Author:     R. Christian McDonald <rcm@FreeBSD.org>
AuthorDate: 2026-11-27 01:48:10 +0000
Commit:     R. Christian McDonald <rcm@FreeBSD.org>
CommitDate: 2026-11-27 01:48:10 +0000

    syslogd: Create one forwarding socket per address pair
    
    For each address a forwarding destination resolves to,
    make_forw_socket_array() creates a socket bound to the address of each
    listening socket and connects it to the destination.  The same
    <laddr, raddr> pair can come up more than once for one rule: the
    resolver may return an address twice, e.g. when a host name appears on
    several /etc/hosts lines for one IP, and several listening sockets may
    be bound to one address.  connect() then fails with EADDRINUSE.
    maybe_dup_forw_socket() only searches the rules parsed earlier, so the
    casper process exits and syslogd dies with "Failed to xfer
    configuration nvlist" on every start and reload.
    
    Skip address pairs the rule already has a socket for.  A second socket
    would add nothing, and with -A it would send each message twice.
    
    Reported by:            Marcos Mendoza
    Reviewed by:            markj
    Fixes:                  ae4f708f0b38 ("syslogd: Ensure that forwarded...")
    See also:               https://redmine.pfsense.org/issues/16966
    MFC after:              1 week
    Sponsored by:           Rubicon Communications, LLC ("Netgate")
    Differential Revision:  https://reviews.freebsd.org/D61493
---
 usr.sbin/syslogd/syslogd.c             | 55 +++++++++++++++++++++----------
 usr.sbin/syslogd/tests/syslogd_test.sh | 60 ++++++++++++++++++++++++++++++++++
 2 files changed, 98 insertions(+), 17 deletions(-)

diff --git a/usr.sbin/syslogd/syslogd.c b/usr.sbin/syslogd/syslogd.c
index 48b92f2ea03e..86c81f9d104f 100644
--- a/usr.sbin/syslogd/syslogd.c
+++ b/usr.sbin/syslogd/syslogd.c
@@ -3116,6 +3116,28 @@ make_forw_socket(const nvlist_t *nvl, struct addrinfo *ai, struct addrinfo *lai)
 	return (s);
 }
 
+/*
+ * Record the <laddr, raddr> pair for slot "n" of the filed's forwarding array.
+ * Returns false if an earlier slot already holds the same pair.
+ */
+static bool
+set_forw_addr(struct filed *f, size_t n, const struct addrinfo *ai,
+    const struct addrinfo *lai)
+{
+	struct forw_addr *fa;
+
+	fa = &f->f_addrs[n];
+	memset(fa, 0, sizeof(*fa));
+	memcpy(&fa->raddr, ai->ai_addr, ai->ai_addrlen);
+	if (lai != NULL)
+		memcpy(&fa->laddr, lai->ai_addr, lai->ai_addrlen);
+	for (size_t i = 0; i < n; i++) {
+		if (memcmp(&f->f_addrs[i], fa, sizeof(*fa)) == 0)
+			return (false);
+	}
+	return (true);
+}
+
 static void
 make_forw_socket_array(const nvlist_t *nvl, struct filed *f,
     struct addrinfo *res)
@@ -3125,7 +3147,7 @@ make_forw_socket_array(const nvlist_t *nvl, struct filed *f,
 
 	f->f_num_addr_fds = 0;
 
-	/* How many sockets do we need? */
+	/* How many sockets do we need at most? */
 	for (ai = res; ai != NULL; ai = ai->ai_next) {
 		struct socklist *boundsock;
 		int count;
@@ -3150,6 +3172,11 @@ make_forw_socket_array(const nvlist_t *nvl, struct filed *f,
 	 * belonging to the destination address, create one socket
 	 * connected to the destination and bound to the address of the
 	 * listening socket.
+	 *
+	 * The resolver may return an address more than once, and several
+	 * listening sockets may be bound to one address.  A second socket for
+	 * the same <laddr, raddr> pair could not be connected and would only
+	 * send a second copy of each message, so each pair gets one socket.
 	 */
 	i = 0;
 	for (ai = res; ai != NULL; ai = ai->ai_next) {
@@ -3158,25 +3185,19 @@ make_forw_socket_array(const nvlist_t *nvl, struct filed *f,
 
 		count = 0;
 		STAILQ_FOREACH(boundsock, &shead, next) {
-			if (boundsock->sl_ai.ai_family ==
-			    ai->ai_family) {
-				memcpy(&f->f_addrs[i].raddr, ai->ai_addr,
-				    ai->ai_addrlen);
-				memcpy(&f->f_addrs[i].laddr,
-				    boundsock->sl_ai.ai_addr,
-				    boundsock->sl_ai.ai_addrlen);
-				f->f_addr_fds[i++] = make_forw_socket(nvl, ai,
-				    &boundsock->sl_ai);
-				count++;
-			}
+			if (boundsock->sl_ai.ai_family != ai->ai_family)
+				continue;
+			count++;
+			if (!set_forw_addr(f, i, ai, &boundsock->sl_ai))
+				continue;
+			f->f_addr_fds[i++] = make_forw_socket(nvl, ai,
+			    &boundsock->sl_ai);
 		}
-		if (count == 0) {
-			memcpy(&f->f_addrs[i].raddr, ai->ai_addr,
-			    ai->ai_addrlen);
+		if (count == 0 && set_forw_addr(f, i, ai, NULL))
 			f->f_addr_fds[i++] = make_forw_socket(nvl, ai, NULL);
-		}
 	}
-	assert(i == f->f_num_addr_fds);
+	assert(i > 0 && i <= f->f_num_addr_fds);
+	f->f_num_addr_fds = i;
 }
 
 static void
diff --git a/usr.sbin/syslogd/tests/syslogd_test.sh b/usr.sbin/syslogd/tests/syslogd_test.sh
index fe97f60c2684..050587760c9d 100644
--- a/usr.sbin/syslogd/tests/syslogd_test.sh
+++ b/usr.sbin/syslogd/tests/syslogd_test.sh
@@ -610,6 +610,65 @@ forward_cleanup()
     syslogd_cleanup
 }
 
+atf_test_case "forward_dup_addr" "cleanup"
+forward_dup_addr_head()
+{
+    atf_set descr "syslogd forwards once per <local, remote> address pair"
+    atf_set require.user root
+    atf_set "require.progs" syslogd
+}
+forward_dup_addr_body()
+{
+    local epair
+
+    syslogd_check_req epair
+
+    atf_check -o save:epair ifconfig epair create
+    epair=$(cat epair)
+    epair=${epair%%a}
+
+    syslogd_mkjail syslogd_server vnet
+    atf_check ifconfig ${epair}a vnet syslogd_server
+    atf_check jexec syslogd_server ifconfig ${epair}a inet 169.254.0.1/16
+
+    syslogd_mkjail syslogd_client vnet
+    atf_check ifconfig ${epair}b vnet syslogd_client
+    atf_check jexec syslogd_client ifconfig ${epair}b inet 169.254.0.3/16
+
+    cat <<__EOF__ > ./client_config
+user.debug @169.254.0.1
+__EOF__
+
+    cat <<__EOF__ > ./server_config
+user.debug ${SYSLOGD_LOGFILE}
+__EOF__
+
+    # Without -c -c, the server would log a second copy of the message
+    # only as a repeat, which the count below cannot see.
+    syslogd_start -j syslogd_server -f ${PWD}/server_config -b 169.254.0.1 \
+        -c -c
+
+    # Two listening sockets bound to one address give the forwarding rule
+    # the same <local, remote> address pair twice.  With -A, syslogd sends
+    # to every forwarding socket it has.
+    syslogd_start -j syslogd_client -f ${PWD}/client_config \
+        -b 169.254.0.3 -b 169.254.0.3 \
+        -p ${PWD}/client -P ${SYSLOGD_PIDFILE}.2 -A
+
+    syslogd_log_jail syslogd_client \
+        -p user.debug -t test1 -h ${PWD}/client "hello, world"
+    syslogd_check_log "test1: hello, world"
+
+    # The client must still be running, and must have sent one copy.
+    atf_check pkill -0 -F ${SYSLOGD_PIDFILE}.2
+    sleep 0.5
+    atf_check -o inline:"1\n" grep -c "test1: hello, world" ${SYSLOGD_LOGFILE}
+}
+forward_dup_addr_cleanup()
+{
+    syslogd_cleanup
+}
+
 atf_test_case "forward_reload" "cleanup"
 forward_reload_head()
 {
@@ -696,5 +755,6 @@ atf_init_test_cases()
     atf_add_test_case "allowed_peer_forwarding"
     atf_add_test_case "allowed_peer_wildcard"
     atf_add_test_case "forward"
+    atf_add_test_case "forward_dup_addr"
     atf_add_test_case "forward_reload"
 }