* [PATCH net-next] net: fix off-by-one in udp_flow_src_port() / psp_write_headers()
@ 2026-03-02 16:39 Eric Dumazet
2026-03-02 19:58 ` Willem de Bruijn
2026-03-05 1:01 ` patchwork-bot+netdevbpf
0 siblings, 2 replies; 3+ messages in thread
From: Eric Dumazet @ 2026-03-02 16:39 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Kuniyuki Iwashima, netdev, eric.dumazet,
Eric Dumazet
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.
Signed-off-by: Eric Dumazet <edumazet@google.com>
---
include/net/udp.h | 3 ++-
net/psp/psp_main.c | 2 +-
2 files changed, 3 insertions(+), 2 deletions(-)
diff --git a/include/net/udp.h b/include/net/udp.h
index da68702ddf6edc6a28440490be07be7eef2d1a4e..b648003e5792a167822f28aeb728c3d64a2141f4 100644
--- a/include/net/udp.h
+++ b/include/net/udp.h
@@ -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)
diff --git a/net/psp/psp_main.c b/net/psp/psp_main.c
index d4c04c923c5ac6aead8b5bac41c54ae1143f4110..9508b6c380038b311a9b778933789b5c4cb61007 100644
--- a/net/psp/psp_main.c
+++ b/net/psp/psp_main.c
@@ -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);
}
--
2.53.0.473.g4a7958ca14-goog
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH net-next] net: fix off-by-one in udp_flow_src_port() / psp_write_headers()
2026-03-02 16:39 [PATCH net-next] net: fix off-by-one in udp_flow_src_port() / psp_write_headers() Eric Dumazet
@ 2026-03-02 19:58 ` Willem de Bruijn
2026-03-05 1:01 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: Willem de Bruijn @ 2026-03-02 19:58 UTC (permalink / raw)
To: Eric Dumazet, David S . Miller, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Kuniyuki Iwashima, netdev, eric.dumazet,
Eric Dumazet
Eric Dumazet wrote:
> 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.
>
> Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: Willem de Bruijn <willemb@google.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH net-next] net: fix off-by-one in udp_flow_src_port() / psp_write_headers()
2026-03-02 16:39 [PATCH net-next] net: fix off-by-one in udp_flow_src_port() / psp_write_headers() Eric Dumazet
2026-03-02 19:58 ` Willem de Bruijn
@ 2026-03-05 1:01 ` patchwork-bot+netdevbpf
1 sibling, 0 replies; 3+ messages in thread
From: patchwork-bot+netdevbpf @ 2026-03-05 1:01 UTC (permalink / raw)
To: Eric Dumazet; +Cc: davem, kuba, pabeni, horms, kuniyu, netdev, eric.dumazet
Hello:
This patch was applied to netdev/net-next.git (main)
by Jakub Kicinski <kuba@kernel.org>:
On Mon, 2 Mar 2026 16:39:33 +0000 you wrote:
> 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.
>
> [...]
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!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-03-05 1:01 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-02 16:39 [PATCH net-next] net: fix off-by-one in udp_flow_src_port() / psp_write_headers() Eric Dumazet
2026-03-02 19:58 ` Willem de Bruijn
2026-03-05 1:01 ` patchwork-bot+netdevbpf
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox