New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
net: net_context: zsock_connect
can error out due to duplicate ports
#72035
Comments
I'm not sure whether the bug is in the implementation of |
@JordanYates I am trying to replicate this but have so far failed. Do you have a setup / test app I could use? What is baffling is that here in zephyr/subsys/net/ip/net_context.c Lines 173 to 175 in a495fd9
we check whether there is a local port (that is set by net_context_get() ) already set. So I just wonder why we would get the duplicate port selected.
|
Are you using IPv4-to-IPv6 mapping |
I can reliably trigger this in my local code by changing this line: zephyr/subsys/net/ip/net_context.c Line 315 in c059405
to this:
Which in theory should just make the function loop for ~2 msec.
This option is not enabled ( |
NCS version
Diff:
Console:
|
There is no need to check our own context when going through the used ports in the system. This prevents error when binding in some corner cases. Fixes zephyrproject-rtos#72035 Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
There is no need to check our own context when going through the used ports in the system. This prevents error when binding in some corner cases. Fixes #72035 Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
There is no need to check our own context when going through the used ports in the system. This prevents error when binding in some corner cases. Fixes zephyrproject-rtos#72035 Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
Describe the bug
check_used_port
can returnfalse
when the port is actually in use, leading tofind_available_port
returning an unavailable port.This can be reliably triggered when
sys_rand32_get
is derived from the system clock, but can occur with a 1/2**16 chance in normal operation.The initial
net_context_get
fromzsock_socket
assigns a random port to the net context:zephyr/subsys/net/ip/net_context.c
Lines 502 to 503 in c059405
net_context_connect
fromzsock_connect
then generates another random port insidebind_default
:zephyr/subsys/net/ip/net_context.c
Lines 656 to 658 in c059405
A duplicate port number at this stage is not detected because the context
sin_addr
pointer is currently NULL, which skips further checks here:zephyr/subsys/net/ip/net_context.c
Line 247 in c059405
Inside
net_context_bind
(called frombind_default
), thesin_addr
pointer is finally set (using the first generated port):zephyr/subsys/net/ip/net_context.c
Line 884 in c059405
And the second generated port is then checked again:
zephyr/subsys/net/ip/net_context.c
Lines 893 to 898 in c059405
At this point
check_used_port
returns an error, andsock_connect
errors out.Expected behavior
It should not be possible for duplicate port numbers to be generated inside
zsock_connect
that cause an error without the user being able to intervene.Environment (please complete the following information):
The text was updated successfully, but these errors were encountered: