Skip to content

Commit 7fe33a5

Browse files
rluboscfriedt
authored andcommitted
net: tcp: Fix error reporting on listening socket on iface down
A listening TCP context has no active connection therefore it has no a second ref from the TCP stack. Thereby, when shutting down connections when interface goes down, net_tcp_put() should not be called on a listening socket to release the ref on the TCP stack behalf. Instead, report the error via the registered accept_cb callback no notify the application about the error, which should then close the socket and release the associated TCP context. Signed-off-by: Robert Lubos <[email protected]>
1 parent c9eba65 commit 7fe33a5

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

subsys/net/ip/tcp.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4765,7 +4765,18 @@ static void close_tcp_conn(struct tcp *conn, void *user_data)
47654765
}
47664766

47674767
/* net_tcp_put() will handle decrementing refcount on stack's behalf */
4768-
net_tcp_put(context, true);
4768+
if (net_context_get_state(context) != NET_CONTEXT_LISTENING) {
4769+
net_tcp_put(context, true);
4770+
} else {
4771+
if (context->conn_handler) {
4772+
net_conn_unregister(context->conn_handler);
4773+
context->conn_handler = NULL;
4774+
}
4775+
4776+
if (conn->accept_cb != NULL) {
4777+
conn->accept_cb(conn->context, NULL, 0, -ENETDOWN, context->user_data);
4778+
}
4779+
}
47694780
}
47704781

47714782
void net_tcp_close_all_for_iface(struct net_if *iface)

subsys/net/lib/sockets/sockets_inet.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,13 @@ static void zsock_accepted_cb(struct net_context *new_ctx,
217217
net_context_ref(new_ctx);
218218

219219
(void)k_condvar_signal(&parent->cond.recv);
220-
}
220+
} else if (status < 0) {
221+
parent->user_data = INT_TO_POINTER(-status);
222+
sock_set_error(parent);
221223

224+
k_fifo_cancel_wait(&parent->recv_q);
225+
(void)k_condvar_signal(&parent->cond.recv);
226+
}
222227
}
223228

224229
static void zsock_received_cb(struct net_context *ctx,

0 commit comments

Comments
 (0)