net: fix off-by-one in udp_flow_src_port() / psp_write_headers()
Subsystems (4)
PATCH 1/1 net: fix off-by-one in udp_flow_src_port() / psp_write_headers()
udp_flow_src_port() and psp_write_headers() use ip_local_port_range.
ip_local_port_range is inclusive : all ports between min and max can be used.
Before this patch, if ip_local_port_range was set to 40000-40001 40001 would not be used as a source port.
Use reciprocal_scale() to help code readability.
Not tagged for stable trees, as this change could break user expectations.
--- include/net/udp.h | 3 ++- net/psp/psp_main.c | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-)
include/net/udp.h +2 -1
@@ -29,6 +29,7 @@#include <linux/seq_file.h> #include <linux/poll.h> #include <linux/indirect_call_wrapper.h> +#include <linux/math.h> /** * struct udp_skb_cb - UDP(-Lite) private variables
@@ -376,7 +377,7 @@ static inline __be16 udp_flow_src_port(struct net *net, struct sk_buff *skb,*/ hash ^= hash <<16 ; -return htons ((((u64 ) hash * (max - min)) >>32 ) + min); +return htons (reciprocal_scale (hash, max - min +1 ) + min); }static inline int udp_rqueue_get (struct sock *sk)
net/psp/psp_main.c +1 -1
@@ -202,7 +202,7 @@ static void psp_write_headers(struct net *net, struct sk_buff *skb, __be32 spi,* reciprocal divide. */ hash ^= hash <<16 ; - uh->source =htons ((((u64 )hash * (max - min)) >>32 ) + min); + uh->source =htons (reciprocal_scale (hash, max - min +1 ) + min); }else { uh->source =udp_flow_src_port (net, skb,0 ,0 , false); }
Signature · 1 line
2.53.0.473.g4a7958ca14-goog
Quoted 8 lines · Eric Dumazet
udp_flow_src_port()andpsp_write_headers()useip_local_port_range.
ip_local_port_rangeis inclusive : all ports between min and max can be used.Before this patch, if
ip_local_port_rangewas set to 40000-40001 40001 would not be used as a source port.
Quoted 6 lines
Use
reciprocal_scale()to help code readability.Not tagged for stable trees, as this change could break user expectations.
Signed-off-by Eric Dumazet <[email protected]>
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Hello:
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:
Quoted 9 lines · On Mon, 2 Mar 2026 16:39:33 +0000 you wrote:
udp_flow_src_port()andpsp_write_headers()useip_local_port_range.
ip_local_port_rangeis inclusive : all ports between min and max can be used.Before this patch, if
ip_local_port_rangewas set to 40000-40001 40001 would not be used as a source port.[...]
Here is the summary with links:
- [net-next] net: fix off-by-one in udp_flow_src_port() / psp_write_headers()
https://git.kernel.org/netdev/net-next/c/c26b8c4e291c
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html