-
Notifications
You must be signed in to change notification settings - Fork 8.3k
net: sockets: add support for SO_REUSEADDR and SO_REUSEPORT #61885
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -158,7 +158,8 @@ static struct net_conn *conn_find_handler(uint16_t proto, uint8_t family, | |
| const struct sockaddr *remote_addr, | ||
| const struct sockaddr *local_addr, | ||
| uint16_t remote_port, | ||
| uint16_t local_port) | ||
| uint16_t local_port, | ||
| bool reuseport_set) | ||
| { | ||
| struct net_conn *conn; | ||
| struct net_conn *tmp; | ||
|
|
@@ -174,67 +175,75 @@ static struct net_conn *conn_find_handler(uint16_t proto, uint8_t family, | |
| continue; | ||
| } | ||
|
|
||
| if (remote_addr) { | ||
|
||
| if (!(conn->flags & NET_CONN_REMOTE_ADDR_SET)) { | ||
| if (local_addr) { | ||
| if (!(conn->flags & NET_CONN_LOCAL_ADDR_SET)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (IS_ENABLED(CONFIG_NET_IPV6) && | ||
| remote_addr->sa_family == AF_INET6 && | ||
| remote_addr->sa_family == | ||
| conn->remote_addr.sa_family) { | ||
| local_addr->sa_family == AF_INET6 && | ||
| local_addr->sa_family == | ||
| conn->local_addr.sa_family) { | ||
| if (!net_ipv6_addr_cmp( | ||
| &net_sin6(remote_addr)->sin6_addr, | ||
| &net_sin6(&conn->remote_addr)-> | ||
| &net_sin6(local_addr)->sin6_addr, | ||
| &net_sin6(&conn->local_addr)-> | ||
| sin6_addr)) { | ||
| continue; | ||
| } | ||
| } else if (IS_ENABLED(CONFIG_NET_IPV4) && | ||
| remote_addr->sa_family == AF_INET && | ||
| remote_addr->sa_family == | ||
| conn->remote_addr.sa_family) { | ||
| local_addr->sa_family == AF_INET && | ||
| local_addr->sa_family == | ||
| conn->local_addr.sa_family) { | ||
| if (!net_ipv4_addr_cmp( | ||
| &net_sin(remote_addr)->sin_addr, | ||
| &net_sin(&conn->remote_addr)-> | ||
| &net_sin(local_addr)->sin_addr, | ||
| &net_sin(&conn->local_addr)-> | ||
| sin_addr)) { | ||
| continue; | ||
| } | ||
| } else { | ||
| continue; | ||
| } | ||
| } else if (conn->flags & NET_CONN_REMOTE_ADDR_SET) { | ||
| } else if (conn->flags & NET_CONN_LOCAL_ADDR_SET) { | ||
| continue; | ||
| } | ||
|
|
||
| if (local_addr) { | ||
| if (!(conn->flags & NET_CONN_LOCAL_ADDR_SET)) { | ||
| if (net_sin(&conn->local_addr)->sin_port != | ||
| htons(local_port)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (remote_addr) { | ||
| if (!(conn->flags & NET_CONN_REMOTE_ADDR_SET)) { | ||
| continue; | ||
| } | ||
|
|
||
| if (IS_ENABLED(CONFIG_NET_IPV6) && | ||
| local_addr->sa_family == AF_INET6 && | ||
| local_addr->sa_family == | ||
| conn->local_addr.sa_family) { | ||
| remote_addr->sa_family == AF_INET6 && | ||
| remote_addr->sa_family == | ||
| conn->remote_addr.sa_family) { | ||
| if (!net_ipv6_addr_cmp( | ||
| &net_sin6(local_addr)->sin6_addr, | ||
| &net_sin6(&conn->local_addr)-> | ||
| &net_sin6(remote_addr)->sin6_addr, | ||
| &net_sin6(&conn->remote_addr)-> | ||
| sin6_addr)) { | ||
| continue; | ||
| } | ||
| } else if (IS_ENABLED(CONFIG_NET_IPV4) && | ||
| local_addr->sa_family == AF_INET && | ||
| local_addr->sa_family == | ||
| conn->local_addr.sa_family) { | ||
| remote_addr->sa_family == AF_INET && | ||
| remote_addr->sa_family == | ||
| conn->remote_addr.sa_family) { | ||
| if (!net_ipv4_addr_cmp( | ||
| &net_sin(local_addr)->sin_addr, | ||
| &net_sin(&conn->local_addr)-> | ||
| &net_sin(remote_addr)->sin_addr, | ||
| &net_sin(&conn->remote_addr)-> | ||
| sin_addr)) { | ||
| continue; | ||
| } | ||
| } else { | ||
| continue; | ||
| } | ||
| } else if (conn->flags & NET_CONN_LOCAL_ADDR_SET) { | ||
| } else if (conn->flags & NET_CONN_REMOTE_ADDR_SET) { | ||
| continue; | ||
| } else if (reuseport_set && conn->context != NULL && | ||
| net_context_is_reuseport_set(conn->context)) { | ||
| continue; | ||
| } | ||
|
|
||
|
|
@@ -243,11 +252,6 @@ static struct net_conn *conn_find_handler(uint16_t proto, uint8_t family, | |
| continue; | ||
| } | ||
|
|
||
| if (net_sin(&conn->local_addr)->sin_port != | ||
| htons(local_port)) { | ||
| continue; | ||
| } | ||
|
|
||
| k_mutex_unlock(&conn_lock); | ||
| return conn; | ||
| } | ||
|
|
@@ -270,10 +274,13 @@ int net_conn_register(uint16_t proto, uint8_t family, | |
| uint8_t flags = 0U; | ||
|
|
||
| conn = conn_find_handler(proto, family, remote_addr, local_addr, | ||
| remote_port, local_port); | ||
| remote_port, local_port, | ||
| context != NULL ? | ||
| net_context_is_reuseport_set(context) : | ||
| false); | ||
| if (conn) { | ||
| NET_ERR("Identical connection handler %p already found.", conn); | ||
| return -EALREADY; | ||
| return -EADDRINUSE; | ||
| } | ||
|
|
||
| conn = conn_get_unused(); | ||
|
|
@@ -736,14 +743,6 @@ enum net_verdict net_conn_input(struct net_pkt *pkt, | |
| continue; /* wrong local address */ | ||
| } | ||
|
|
||
| /* If we have an existing best_match, and that one | ||
| * specifies a remote port, then we've matched to a | ||
| * LISTENING connection that we should not override. | ||
| */ | ||
| if (best_match != NULL && best_match->flags & NET_CONN_REMOTE_PORT_SPEC) { | ||
rlubos marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| continue; /* do not override listening connection */ | ||
| } | ||
|
|
||
| if (best_rank < NET_CONN_RANK(conn->flags)) { | ||
| struct net_pkt *mcast_pkt; | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.