Skip to content

Conversation

@jukkar
Copy link
Member

@jukkar jukkar commented Oct 23, 2025

If the network interface goes down, close all TCP connections that are bound to that interface. The reasoning here is that we need a way to remove IP address from the interface and it might not be possible if there is a connection that is bound to that address. If the interface comes back on, we might get the same IP address in which case the socket closure was not really needed but it is not possible to know in advance.

@jukkar jukkar added In progress For PRs: is work in progress and should not be merged yet. For issues: Is being worked on area: Networking labels Oct 23, 2025
Comment on lines 782 to 783
/* Decrement refcount on user app's behalf */
net_context_unref(&contexts[i]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I'm not sure if we should dereference net context as well? On the application side there's still an open socket that needs to be closed, when we close the socket the context would be unreffed by the socket code again I think?

Probably better just to have the net stack to report an error on the socket, and let the application close it. The question is, whether we should do it only for the sockets with active connections (i.e. TCP only, which should be handled by net_tcp_put() call here already), or really all sockets (we'd need some extra handling for UDP sockets then). IMHO, TCP only is fine, I don't think there's a need to close connection-less sockets?

@jukkar jukkar force-pushed the fix/close-sockets-on-iface-down branch from 693c666 to 25da33c Compare October 23, 2025 09:59
@jukkar
Copy link
Member Author

jukkar commented Oct 23, 2025

  • Updated according to comments
Comment on lines 1062 to 1066
* @brief Close and unref all network context bound to an network interface.
*
* @details This releases all the contexts that are bound to a specific
* network interface. It is not possible to send or receive data via those
* contexts after this call.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This description seems no longer valid. Maybe the function needs a rename as well given it's now just shutting down TCP connections on given interface.

Comment on lines 780 to 782
contexts[i].connect_cb = NULL;
contexts[i].recv_cb = NULL;
contexts[i].send_cb = NULL;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't clear the callbacks I think, TCP still needs connect_cb/recv_cb (depending on the internal state it's in) to notify the socket layer about connection being closed? If we clear the callback now, the application won't get an error from the socket.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, good point, I will remove those lines.

If the network interface goes down, then we do not have any time to tear down TCP connection gracefully because there is no more a connection to send data to. In this case the TCP connection is forcefully closed without going into FIN_WAIT_1 state. Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
@jukkar jukkar force-pushed the fix/close-sockets-on-iface-down branch from 25da33c to 360a716 Compare October 23, 2025 10:54
@jukkar
Copy link
Member Author

jukkar commented Oct 23, 2025

  • Updated according to comments
  • Moved the functionality to tcp.c because we are only dealing with TCP here
Copy link
Contributor

@rlubos rlubos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One final nit, looks good otherwise.

Comment on lines 4770 to 4775
if (IS_ENABLED(CONFIG_NET_OFFLOAD) && net_if_is_ip_offloaded(iface)) {
context->flags &= ~NET_CONTEXT_IN_USE;
(void)net_offload_put(iface, context);
k_mutex_unlock(&context->lock);
return;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we need this anymore, native TCP stack would never be used by offloaded interfaces.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, very true, will remove.

@jukkar jukkar force-pushed the fix/close-sockets-on-iface-down branch from 360a716 to ada0276 Compare October 23, 2025 11:08
rlubos
rlubos previously approved these changes Oct 23, 2025
maass-hamburg
maass-hamburg previously approved these changes Oct 23, 2025
If the network interface goes down, close all TCP connections that are bound to that interface. The reasoning here is that we need a way to remove IP address from the interface and it might not be possible if there is a TCP socket that is bound to that address. If the interface comes back on, we might get the same IP address in which case the socket closure was not really needed but it is not possible to know in advance. Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
@jukkar jukkar dismissed stale reviews from maass-hamburg and rlubos via 23f49eb October 23, 2025 11:41
@jukkar jukkar force-pushed the fix/close-sockets-on-iface-down branch from ada0276 to 23f49eb Compare October 23, 2025 11:41
@jukkar
Copy link
Member Author

jukkar commented Oct 23, 2025

  • CI fixes
@jukkar jukkar removed the In progress For PRs: is work in progress and should not be merged yet. For issues: Is being worked on label Oct 23, 2025
@cfriedt cfriedt merged commit ec3bcd3 into zephyrproject-rtos:main Oct 27, 2025
28 checks passed
@jukkar jukkar deleted the fix/close-sockets-on-iface-down branch October 28, 2025 07:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

4 participants