Commit cc24c1a828 for frr

commit cc24c1a82889b2cf683770adff31bdd319c61e15
Author: Lincoln Thurlow <lincoln@isi.edu>
Date:   Wed Sep 2 19:09:57 2026 -0700

    pceplib: allow parsing PCEP split across TCP reads

    This error occurs when messages sent across multiple
    TCP reads are not parsed correctly.  Instead pceplib causes each
    partial message to close the socket and tear down the session.

    PCEP runs over a TCP byte stream, but pcep_msg_read() decodes only
    the complete messages returned by a single read() and drops any
    trailing partial message.  A large burst of messages that get split
    by the kernel across reads ends up killing the session.

    The solution presented is to create a buffer for each session.  The
    buffer reads messages across calls and is responsible for
    reassembling the partial messages into a complete PCEP message.

    Signed-off-by: Lincoln Thurlow <lincoln@isi.edu>

diff --git a/pceplib/pcep_msg_tools.c b/pceplib/pcep_msg_tools.c
index b7a9545774..530531544b 100644
--- a/pceplib/pcep_msg_tools.c
+++ b/pceplib/pcep_msg_tools.c
@@ -166,6 +166,95 @@ double_linked_list *pcep_msg_read(int sock_fd)
 	return msg_list;
 }

+double_linked_list *pcep_msg_read_buffered(int sock_fd, uint8_t *buffer, uint32_t buffer_size,
+					   uint32_t *buffer_len, int *invalid_msgs)
+{
+	int ret;
+	uint32_t offset = 0;
+
+	/* Defensive: the loop below never leaves a whole message buffered */
+	if (*buffer_len >= buffer_size) {
+		pcep_log(LOG_ERR, "%s: buffer full (%u bytes) fd [%d], discarding", __func__,
+			 *buffer_len, sock_fd);
+		*buffer_len = 0;
+	}
+
+	ret = read(sock_fd, buffer + *buffer_len, buffer_size - *buffer_len);
+
+	if (ret < 0) {
+		/*
+		 * Non-blocking socket with nothing to read, or an interrupted
+		 * read: not an error and not a close. Return an empty list so
+		 * the caller keeps the session and the buffer as they are.
+		 */
+		if (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
+			return dll_initialize();
+		/* A real read error: report it as a closed socket (NULL). */
+		pcep_log(LOG_INFO, "%s: Failed to read from socket fd [%d] errno [%d %s]",
+			 __func__, sock_fd, errno, strerror(errno));
+		return NULL;
+	} else if (ret == 0) {
+		/*
+		 * The peer closed the connection: NULL tells the caller the
+		 * socket is gone so it can tear the session down.
+		 */
+		pcep_log(LOG_INFO, "%s: Remote shutdown fd [%d]", __func__, sock_fd);
+		return NULL;
+	}
+
+	*buffer_len += ret;
+
+	double_linked_list *msg_list = dll_initialize();
+
+	/*
+	 * Decode every complete message the buffer now holds.
+	 * Stop once fewer than a header's worth of bytes are left,
+	 * the length field is not yet in.
+	 */
+	while (*buffer_len - offset >= MESSAGE_HEADER_LENGTH) {
+		int32_t msg_length = pcep_decode_validate_msg_header(buffer + offset);
+
+		if (msg_length < 0 || (uint32_t)msg_length > buffer_size) {
+			pcep_log(LOG_INFO,
+				 "%s: Received an invalid message fd [%d], discarding %u bytes",
+				 __func__, sock_fd, *buffer_len - offset);
+			(*invalid_msgs)++;
+			*buffer_len = 0;
+			return msg_list;
+		}
+
+		/*
+		 * The header is valid but the whole message has not arrived
+		 * yet. A later read() completes it.
+		 */
+		if (*buffer_len - offset < (uint32_t)msg_length)
+			break;
+
+		struct pcep_message *msg = pcep_decode_message(buffer + offset);
+
+		if (msg == NULL) {
+			pcep_log(LOG_INFO, "%s: Could not decode a message of length [%d] fd [%d]",
+				 __func__, msg_length, sock_fd);
+			(*invalid_msgs)++;
+		} else {
+			dll_append(msg_list, msg);
+		}
+		offset += msg_length;
+	}
+
+	/*
+	 * Drop the consumed bytes, shifting any trailing partial message to
+	 * the front so the next call reads in right after it.
+	 */
+	if (offset > 0) {
+		*buffer_len -= offset;
+		if (*buffer_len > 0)
+			memmove(buffer, buffer + offset, *buffer_len);
+	}
+
+	return msg_list;
+}
+
 struct pcep_message *pcep_msg_get(double_linked_list *msg_list, uint8_t type)
 {
 	if (msg_list == NULL) {
diff --git a/pceplib/pcep_msg_tools.h b/pceplib/pcep_msg_tools.h
index e756b461b0..4b17549bb9 100644
--- a/pceplib/pcep_msg_tools.h
+++ b/pceplib/pcep_msg_tools.h
@@ -25,6 +25,18 @@ extern "C" {

 /* Returns a double linked list of PCEP messages */
 double_linked_list *pcep_msg_read(int sock_fd);
+/*
+ * Read from socket into the caller's reassembly buffer, which holds
+ * buffer_len bytes of an earlier partial message, and
+ * decode every complete message in it. The bytes of a trailing partial
+ * message are kept in the buffer for the next call. Every message that
+ * fails to decode is counted in invalid_msgs; an invalid message header
+ * discards the buffer, since the length of what follows is unknown.
+ * Returns NULL if the socket was closed or the read failed, otherwise
+ * the list of decoded messages.
+ */
+double_linked_list *pcep_msg_read_buffered(int sock_fd, uint8_t *buffer, uint32_t buffer_size,
+					   uint32_t *buffer_len, int *invalid_msgs);
 /* Given a double linked list of PCEP messages, return the first node that has
  * the same message type */
 struct pcep_message *pcep_msg_get(double_linked_list *msg_list, uint8_t type);
diff --git a/pceplib/pcep_session_logic.h b/pceplib/pcep_session_logic.h
index d7ef7c355f..812be137e3 100644
--- a/pceplib/pcep_session_logic.h
+++ b/pceplib/pcep_session_logic.h
@@ -150,6 +150,12 @@ typedef struct pcep_session_ {
 	/* Configuration received from the PCE, to be used in the PCC */
 	pcep_configuration pce_config;
 	struct counters_group *pcep_session_counters;
+	/*
+	 * Bytes read from the socket that do not yet form a complete
+	 * message. A message split across reads is completed here.
+	 */
+	uint8_t rx_buffer[PCEP_MESSAGE_LENGTH];
+	uint32_t rx_buffer_len;

 } pcep_session;

diff --git a/pceplib/pcep_session_logic_loop.c b/pceplib/pcep_session_logic_loop.c
index 6717f4676d..08489ae471 100644
--- a/pceplib/pcep_session_logic_loop.c
+++ b/pceplib/pcep_session_logic_loop.c
@@ -43,9 +43,9 @@ static pcep_session_event *create_session_event(pcep_session *session)
  * for each pcep_session creation, so it will be called whenever
  * messages are ready to be read. This function will be called
  * by the socket_comm thread.
- * This function will decode the read PCEP message and give it
- * to the session_logic_loop so it can be handled by the session_logic
- * state machine. */
+ * This function will reassemble and decode the read PCEP messages and
+ * give them to the session_logic_loop so they can be handled by the
+ * session_logic state machine. */
 int session_logic_msg_ready_handler(void *data, int socket_fd)
 {
 	if (data == NULL) {
@@ -72,25 +72,45 @@ int session_logic_msg_ready_handler(void *data, int socket_fd)
 	 * in pcep_session_logic_states.c */
 	pcep_session_event *rcvd_msg_event = create_session_event(session);

-	int msg_length = 0;
-	double_linked_list *msg_list = pcep_msg_read(socket_fd);
+	/*
+	 * The socket comm loop stops reading from the socket when 0 is
+	 * returned, which must only happen when the socket was closed. A
+	 * read that completes no message (a message split across reads,
+	 * or an undecodable one) keeps the socket open. When messages were
+	 * received, the length of the first one is returned, as before.
+	 */
+	int result = 1;
+	int invalid_msgs = 0;
+	double_linked_list *msg_list =
+		pcep_msg_read_buffered(socket_fd, session->rx_buffer, sizeof(session->rx_buffer),
+				       &session->rx_buffer_len, &invalid_msgs);
+
+	while (invalid_msgs > 0) {
+		increment_unknown_message(session);
+		invalid_msgs--;
+	}
+
+	/* No complete message yet: keep the session and wait for more data */
+	if (msg_list != NULL && msg_list->num_entries == 0) {
+		dll_destroy(msg_list);
+		pceplib_free(PCEPLIB_INFRA, rcvd_msg_event);
+		pthread_mutex_unlock(&(session_logic_handle_->session_logic_mutex));
+		return result;
+	}

 	if (msg_list == NULL) {
 		/* The socket was closed, or there was a socket read error */
 		pcep_log(LOG_INFO,
 			 "%s: PCEP connection closed for session [%d]",
 			 __func__, session->session_id);
-		dll_destroy(msg_list);
+		result = 0;
+		session->rx_buffer_len = 0;
 		rcvd_msg_event->socket_closed = true;
 		socket_comm_session_teardown(session->socket_comm_session);
 		pcep_session_cancel_timers(session);
 		session->socket_comm_session = NULL;
 		session->session_state = SESSION_STATE_INITIALIZED;
 		enqueue_event(session, PCE_CLOSED_SOCKET, NULL);
-	} else if (msg_list->num_entries == 0) {
-		/* Invalid message received */
-		increment_unknown_message(session);
-		dll_destroy_with_data(msg_list);
 	} else {
 		/* Just logging the first of potentially several messages
 		 * received */
@@ -104,7 +124,7 @@ int session_logic_msg_ready_handler(void *data, int socket_fd)
 			session->session_id);

 		rcvd_msg_event->received_msg_list = msg_list;
-		msg_length = msg->encoded_message_length;
+		result = msg->encoded_message_length;
 	}

 	queue_enqueue(session_logic_handle_->session_event_queue,
@@ -112,7 +132,7 @@ int session_logic_msg_ready_handler(void *data, int socket_fd)
 	pthread_cond_signal(&(session_logic_handle_->session_logic_cond_var));
 	pthread_mutex_unlock(&(session_logic_handle_->session_logic_mutex));

-	return msg_length;
+	return result;
 }


diff --git a/pceplib/test/pcep_msg_messages_tests.c b/pceplib/test/pcep_msg_messages_tests.c
index 26bc02914c..48dbac9566 100644
--- a/pceplib/test/pcep_msg_messages_tests.c
+++ b/pceplib/test/pcep_msg_messages_tests.c
@@ -196,6 +196,8 @@ int main(int argc, char **argv)
 		    test_pcep_msg_read_pcep_initiate_vendor_info);
 	CU_add_test(tools_suite, "test_pcep_msg_read_pcep_initiate_unknown_tlv",
 		    test_pcep_msg_read_pcep_initiate_unknown_tlv);
+	CU_add_test(tools_suite, "test_pcep_msg_read_buffered_split",
+		    test_pcep_msg_read_buffered_split);
 	CU_add_test(tools_suite, "test_pcep_msg_read_pcep_update",
 		    test_pcep_msg_read_pcep_update);
 	CU_add_test(tools_suite, "test_pcep_msg_read_pcep_open",
diff --git a/pceplib/test/pcep_msg_tools_test.c b/pceplib/test/pcep_msg_tools_test.c
index 982d55d756..3512cae015 100644
--- a/pceplib/test/pcep_msg_tools_test.c
+++ b/pceplib/test/pcep_msg_tools_test.c
@@ -18,6 +18,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <sys/socket.h>
 #include <sys/stat.h>


@@ -331,6 +332,108 @@ void test_pcep_msg_read_pcep_initiate(void)
 }


+/*
+ * Messages arriving split across reads must be reassembled from the
+ * caller's buffer instead of being dropped.
+ */
+void test_pcep_msg_read_buffered_split(void)
+{
+	char filename[BASE_TMPFILE_SIZE];
+	uint8_t msg_bytes[pcep_initiate_hexbyte_strs_length];
+	uint8_t buffer[PCEP_MESSAGE_LENGTH];
+	uint32_t buffer_len = 0;
+	int invalid_msgs = 0;
+	int sv[2];
+
+	int fd = convert_hexstrs_to_binary(filename, pcep_initiate_hexbyte_strs,
+					   pcep_initiate_hexbyte_strs_length);
+	if (fd == -1) {
+		CU_ASSERT_TRUE(fd >= 0);
+		return;
+	}
+	ssize_t msg_len = read(fd, msg_bytes, sizeof(msg_bytes));
+
+	close(fd);
+	unlink(filename);
+	CU_ASSERT_EQUAL(msg_len, pcep_initiate_hexbyte_strs_length);
+	if (msg_len != pcep_initiate_hexbyte_strs_length)
+		return;
+
+	CU_ASSERT_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
+
+	/*
+	 * The first 30 bytes of a message: nothing to decode yet, and the
+	 * bytes are kept for the next read.
+	 */
+	CU_ASSERT_EQUAL(write(sv[1], msg_bytes, 30), 30);
+	double_linked_list *msg_list = pcep_msg_read_buffered(sv[0], buffer, sizeof(buffer),
+							      &buffer_len, &invalid_msgs);
+	CU_ASSERT_PTR_NOT_NULL(msg_list);
+	assert(msg_list != NULL);
+	CU_ASSERT_EQUAL(msg_list->num_entries, 0);
+	CU_ASSERT_EQUAL(buffer_len, 30);
+	CU_ASSERT_EQUAL(invalid_msgs, 0);
+	dll_destroy(msg_list);
+
+	/*
+	 * The rest of it, a whole second message and the start of a third:
+	 * two messages come out, the start of the third stays buffered.
+	 */
+	CU_ASSERT_EQUAL(write(sv[1], msg_bytes + 30, msg_len - 30), msg_len - 30);
+	CU_ASSERT_EQUAL(write(sv[1], msg_bytes, msg_len), msg_len);
+	CU_ASSERT_EQUAL(write(sv[1], msg_bytes, 10), 10);
+	msg_list = pcep_msg_read_buffered(sv[0], buffer, sizeof(buffer), &buffer_len,
+					  &invalid_msgs);
+	CU_ASSERT_PTR_NOT_NULL(msg_list);
+	assert(msg_list != NULL);
+	CU_ASSERT_EQUAL(msg_list->num_entries, 2);
+	CU_ASSERT_EQUAL(buffer_len, 10);
+	CU_ASSERT_EQUAL(invalid_msgs, 0);
+	double_linked_list_node *node;
+
+	for (node = msg_list->head; node != NULL; node = node->next_node) {
+		struct pcep_message *msg = (struct pcep_message *)node->data;
+
+		CU_ASSERT_EQUAL(msg->msg_header->type, PCEP_TYPE_INITIATE);
+		CU_ASSERT_EQUAL(msg->encoded_message_length, msg_len);
+	}
+	pcep_msg_free_message_list(msg_list);
+
+	/* The rest of the third message completes it */
+	CU_ASSERT_EQUAL(write(sv[1], msg_bytes + 10, msg_len - 10), msg_len - 10);
+	msg_list = pcep_msg_read_buffered(sv[0], buffer, sizeof(buffer), &buffer_len,
+					  &invalid_msgs);
+	CU_ASSERT_PTR_NOT_NULL(msg_list);
+	assert(msg_list != NULL);
+	CU_ASSERT_EQUAL(msg_list->num_entries, 1);
+	CU_ASSERT_EQUAL(buffer_len, 0);
+	CU_ASSERT_EQUAL(invalid_msgs, 0);
+	pcep_msg_free_message_list(msg_list);
+
+	/*
+	 * An invalid header (wrong version) is counted and discards the
+	 * buffer, since the length of what follows is unknown.
+	 */
+	uint8_t garbage[8] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
+
+	CU_ASSERT_EQUAL(write(sv[1], garbage, sizeof(garbage)), (ssize_t)sizeof(garbage));
+	msg_list = pcep_msg_read_buffered(sv[0], buffer, sizeof(buffer), &buffer_len,
+					  &invalid_msgs);
+	CU_ASSERT_PTR_NOT_NULL(msg_list);
+	assert(msg_list != NULL);
+	CU_ASSERT_EQUAL(msg_list->num_entries, 0);
+	CU_ASSERT_EQUAL(buffer_len, 0);
+	CU_ASSERT_EQUAL(invalid_msgs, 1);
+	dll_destroy(msg_list);
+
+	/* The peer closing the socket is reported as NULL */
+	close(sv[1]);
+	msg_list = pcep_msg_read_buffered(sv[0], buffer, sizeof(buffer), &buffer_len,
+					  &invalid_msgs);
+	CU_ASSERT_PTR_NULL(msg_list);
+	close(sv[0]);
+}
+
 void test_pcep_msg_read_pcep_initiate2(void)
 {
 	char filename[BASE_TMPFILE_SIZE];
diff --git a/pceplib/test/pcep_msg_tools_test.h b/pceplib/test/pcep_msg_tools_test.h
index 82a4b6164b..64153c75e8 100644
--- a/pceplib/test/pcep_msg_tools_test.h
+++ b/pceplib/test/pcep_msg_tools_test.h
@@ -24,6 +24,7 @@ void test_pcep_msg_read_pcep_initiate(void);
 void test_pcep_msg_read_pcep_initiate2(void);
 void test_pcep_msg_read_pcep_initiate_vendor_info(void);
 void test_pcep_msg_read_pcep_initiate_unknown_tlv(void);
+void test_pcep_msg_read_buffered_split(void);
 void test_pcep_msg_read_pcep_update(void);
 void test_pcep_msg_read_pcep_open(void);
 void test_pcep_msg_read_pcep_open_initiate(void);
diff --git a/pceplib/test/pcep_session_logic_loop_test.c b/pceplib/test/pcep_session_logic_loop_test.c
index 0f74cff31e..b36853f4e6 100644
--- a/pceplib/test/pcep_session_logic_loop_test.c
+++ b/pceplib/test/pcep_session_logic_loop_test.c
@@ -19,6 +19,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <sys/types.h>
+#include <sys/socket.h>
 #include <sys/stat.h>

 #include <CUnit/CUnit.h>
@@ -183,6 +184,61 @@ void test_session_logic_msg_ready_handler()
 }


+/* A message split across reads must not close the session: the handler
+ * returns a positive value and queues nothing until the message is
+ * complete, then delivers it.
+ */
+void test_session_logic_msg_ready_handler_partial(void)
+{
+	int sv[2];
+	pcep_session session;
+	struct pcep_versioning *versioning;
+	struct pcep_message *keep_alive_msg;
+	pcep_session_event *socket_event;
+	uint16_t len, first;
+
+	CU_ASSERT_EQUAL_FATAL(socketpair(AF_UNIX, SOCK_STREAM, 0, sv), 0);
+
+	memset(&session, 0, sizeof(pcep_session));
+	session.session_id = 100;
+
+	versioning = create_default_pcep_versioning();
+	keep_alive_msg = pcep_msg_create_keepalive();
+	pcep_encode_message(keep_alive_msg, versioning);
+	len = keep_alive_msg->encoded_message_length;
+	first = len / 2;
+
+	/* First part only: the socket stays open and nothing is queued */
+	CU_ASSERT_EQUAL(write(sv[1], keep_alive_msg->encoded_message, first), first);
+	CU_ASSERT_TRUE(session_logic_msg_ready_handler(&session, sv[0]) > 0);
+	CU_ASSERT_EQUAL(session_logic_handle_->session_event_queue->num_entries, 0);
+	CU_ASSERT_EQUAL(session_logic_event_queue_->event_queue->num_entries, 0);
+	CU_ASSERT_EQUAL(session.rx_buffer_len, first);
+
+	/* The remainder completes the message, which is delivered */
+	CU_ASSERT_EQUAL(write(sv[1], keep_alive_msg->encoded_message + first, len - first),
+			len - first);
+	CU_ASSERT_EQUAL(session_logic_msg_ready_handler(&session, sv[0]), len);
+	CU_ASSERT_EQUAL(session_logic_event_queue_->event_queue->num_entries, 0);
+	CU_ASSERT_EQUAL(session_logic_handle_->session_event_queue->num_entries, 1);
+	socket_event =
+		(pcep_session_event *)queue_dequeue(session_logic_handle_->session_event_queue);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(socket_event);
+	assert(socket_event != NULL);
+	CU_ASSERT_FALSE(socket_event->socket_closed);
+	CU_ASSERT_PTR_NOT_NULL_FATAL(socket_event->received_msg_list);
+	CU_ASSERT_EQUAL(socket_event->received_msg_list->num_entries, 1);
+	CU_ASSERT_EQUAL(session.rx_buffer_len, 0);
+
+	pcep_msg_free_message_list(socket_event->received_msg_list);
+	pceplib_free(PCEPLIB_INFRA, socket_event);
+	pcep_msg_free_message(keep_alive_msg);
+	destroy_pcep_versioning(versioning);
+	close(sv[0]);
+	close(sv[1]);
+}
+
+
 void test_session_logic_conn_except_notifier()
 {
 	/* Just testing that it does not core dump */
diff --git a/pceplib/test/pcep_session_logic_loop_test.h b/pceplib/test/pcep_session_logic_loop_test.h
index f2ad822251..7ea2bb76ac 100644
--- a/pceplib/test/pcep_session_logic_loop_test.h
+++ b/pceplib/test/pcep_session_logic_loop_test.h
@@ -22,6 +22,7 @@ void pcep_session_logic_loop_test_teardown(void);
 void test_session_logic_loop_null_data(void);
 void test_session_logic_loop_inactive(void);
 void test_session_logic_msg_ready_handler(void);
+void test_session_logic_msg_ready_handler_partial(void);
 void test_session_logic_conn_except_notifier(void);
 void test_session_logic_timer_expire_handler(void);

diff --git a/pceplib/test/pcep_session_logic_tests.c b/pceplib/test/pcep_session_logic_tests.c
index 2b09a16469..7834f55e98 100644
--- a/pceplib/test/pcep_session_logic_tests.c
+++ b/pceplib/test/pcep_session_logic_tests.c
@@ -91,6 +91,8 @@ int main(int argc, char **argv)
 	CU_add_test(test_session_logic_loop_suite,
 		    "test_session_logic_msg_ready_handler",
 		    test_session_logic_msg_ready_handler);
+	CU_add_test(test_session_logic_loop_suite, "test_session_logic_msg_ready_handler_partial",
+		    test_session_logic_msg_ready_handler_partial);
 	CU_add_test(test_session_logic_loop_suite,
 		    "test_session_logic_conn_except_notifier",
 		    test_session_logic_conn_except_notifier);