Skip to contentSkip to conversation

sctp: stream: rebind out_curr when updating streams

PATCH In review Ren Wei 2 participants 2 messages May 27 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Ren Wei

author
#1 lore raw

PATCH 1/1 sctp: stream: rebind out_curr when updating streams

In reply to a message not in the archive

From: Yuqi Xu <[email protected]>

sctp_stream_update() replaces the outbound stream table while out_curr may still point to the current stream selected for an unfinished fragmented message.

Remember the current stream id before freeing the old table and rebind out_curr after the new table is installed. If that stream no longer exists, clear the cached pointer instead.

This keeps the scheduler cursor valid across stream table replacement without losing the current stream when it still survives the update.

Fixes 5bbbbe32a431 ("sctp: introduce stream scheduler foundations")
Assisted-by Codex:GPT-5.4
---
 net/sctp/stream.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/sctp/stream.c +11 -0
index c2247793c88b..fcb6c688f61d 100644
@@ -208,6 +208,16 @@ void sctp_stream_clear(struct sctp_stream *stream)
 void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
 {
 	const struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
+	__u16 sid = SCTP_MAX_STREAM;
+
+	/* Preserve the current stream if its sid survives the table swap. */
+	if (stream->out_curr) {
+		for (sid = 0; sid < stream->outcnt; sid++)
+			if (SCTP_SO(stream, sid) == stream->out_curr)
+				break;
+		if (sid == stream->outcnt)
+			sid = SCTP_MAX_STREAM;
+	}
 
 	sched->unsched_all(stream);
 	sctp_stream_outq_migrate(stream, new, new->outcnt);
@@ -217,6 +227,7 @@ void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
 	stream->in  = new->in;
 	stream->outcnt = new->outcnt;
 	stream->incnt  = new->incnt;
+	stream->out_curr = sid < stream->outcnt ? SCTP_SO(stream, sid) : NULL;
 
 	sched->sched_all(stream);
 
Signature · 1 line
2.54.0
maintainer
#2 lore raw
Quoted 51 lines · Ren Wei

From: Yuqi Xu <[email protected]>

sctp_stream_update() replaces the outbound stream table while out_curr may still point to the current stream selected for an unfinished fragmented message.

Remember the current stream id before freeing the old table and rebind out_curr after the new table is installed. If that stream no longer exists, clear the cached pointer instead.

This keeps the scheduler cursor valid across stream table replacement without losing the current stream when it still survives the update.

Fixes 5bbbbe32a431 ("sctp: introduce stream scheduler foundations")
Assisted-by Codex:GPT-5.4
---
 net/sctp/stream.c | 11 +++++++++++
 1 file changed, 11 insertions(+)
net/sctp/stream.c +10 -0
index c2247793c88b..fcb6c688f61d 100644
@@ -208,6 +208,16 @@ void sctp_stream_clear(struct sctp_stream *stream)
 void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
 {
        const struct sctp_sched_ops *sched = sctp_sched_ops_from_stream(stream);
+       __u16 sid = SCTP_MAX_STREAM;
+
+       /* Preserve the current stream if its sid survives the table swap. */
+       if (stream->out_curr) {
+               for (sid = 0; sid < stream->outcnt; sid++)
+                       if (SCTP_SO(stream, sid) == stream->out_curr)
+                               break;
+               if (sid == stream->outcnt)
+                       sid = SCTP_MAX_STREAM;
+       }
 
        sched->unsched_all(stream);
        sctp_stream_outq_migrate(stream, new, new->outcnt);

net/sctp/stream.c void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new) · line 233

Quoted 7 lines
net/sctp/stream.c +1 -0
@@ -217,6 +227,7 @@ void sctp_stream_update(struct sctp_stream *stream, struct sctp_stream *new)
        stream->in  = new->in;
        stream->outcnt = new->outcnt;
        stream->incnt  = new->incnt;
+       stream->out_curr = sid < stream->outcnt ? SCTP_SO(stream, sid) : NULL;
 
        sched->sched_all(stream);
 

Thanks for the report and patch.

sctp_stream_update() is only called in COOKIE_WAIT state, where stream->out_curr is expected to be NULL, since no user data should have been sent at or before this state unless the state transitioned backward.

However, there is a corner case in sctp_sf_do_5_2_6_stale(): when a Stale Cookie ERROR is received, the association may move from COOKIE_ECHOED back to COOKIE_WAIT. If data was already bundled with COOKIE-ECHO to send out, this issue can be triggered.

Updating stream->out_curr here is not a proper fix here, as any transmitted data would also remain in the retransmit queue.

Since this restarts the handshake, negotiated parameters may change, so previously queued/sent data should be dropped.

So you can fix it by purging the outqueue in sctp_sf_do_5_2_6_stale() like:

net/sctp/sm_statefuns.c +1 -5
index 8e89a870780c..9b23c11cbb9e 100644
@@ -2598,11 +2598,7 @@ static enum sctp_disposition sctp_sf_do_5_2_6_stale(
         */
        sctp_add_cmd_sf(commands, SCTP_CMD_DEL_NON_PRIMARY, SCTP_NULL());
 
-       /* If we've sent any data bundled with COOKIE-ECHO we will need to
-        * resend
-        */
-       sctp_add_cmd_sf(commands, SCTP_CMD_T1_RETRAN,
-                       SCTP_TRANSPORT(asoc->peer.primary_path));
+       sctp_add_cmd_sf(commands, SCTP_CMD_PURGE_OUTQUEUE, SCTP_NULL());
 
        /* Cast away the const modifier, as we want to just
         * rerun it through as a sideffect.

The userspace would rely on SCTP_SEND_FAILED_EVENT to get notified.

Thanks.