Skip to content

Commit

Permalink
net: context: Do not check our own ports
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
jukkar authored and nashif committed May 14, 2024
1 parent 97b0ad6 commit 1d7970b
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions subsys/net/ip/net_context.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,8 @@ static inline bool is_in_tcp_time_wait_state(struct net_context *context)
#endif
}

static int check_used_port(struct net_if *iface,
static int check_used_port(struct net_context *context,
struct net_if *iface,
enum net_ip_protocol proto,
uint16_t local_port,
const struct sockaddr *local_addr,
Expand All @@ -170,6 +171,10 @@ static int check_used_port(struct net_if *iface,
continue;
}

if (context != NULL && context == &contexts[i]) {
continue;
}

if (!(net_context_get_proto(&contexts[i]) == proto &&
net_sin((struct sockaddr *)&
contexts[i].local)->sin_port == local_port)) {
Expand Down Expand Up @@ -313,7 +318,7 @@ static uint16_t find_available_port(struct net_context *context,

do {
local_port = sys_rand16_get() | 0x8000;
} while (check_used_port(NULL, net_context_get_proto(context),
} while (check_used_port(context, NULL, net_context_get_proto(context),
htons(local_port), addr, false, false) == -EEXIST);

return htons(local_port);
Expand All @@ -327,7 +332,8 @@ bool net_context_port_in_use(enum net_ip_protocol proto,
uint16_t local_port,
const struct sockaddr *local_addr)
{
return check_used_port(NULL, proto, htons(local_port), local_addr, false, false) != 0;
return check_used_port(NULL, NULL, proto, htons(local_port),
local_addr, false, false) != 0;
}

#if defined(CONFIG_NET_CONTEXT_CHECK)
Expand Down Expand Up @@ -794,7 +800,7 @@ int net_context_bind(struct net_context *context, const struct sockaddr *addr,

ret = 0;
if (addr6->sin6_port) {
ret = check_used_port(iface,
ret = check_used_port(context, iface,
context->proto,
addr6->sin6_port,
addr,
Expand Down Expand Up @@ -894,7 +900,7 @@ int net_context_bind(struct net_context *context, const struct sockaddr *addr,

ret = 0;
if (addr4->sin_port) {
ret = check_used_port(iface,
ret = check_used_port(context, iface,
context->proto,
addr4->sin_port,
addr,
Expand Down

0 comments on commit 1d7970b

Please sign in to comment.