Skip to content

Commit 41db762

Browse files
edumazetkuba-moo
authored andcommitted
inet: returns a bool from inet_sk_get_local_port_range()
Change inet_sk_get_local_port_range() to return a boolean, telling the callers if the port range was provided by IP_LOCAL_PORT_RANGE socket option. Adds documentation while we are at it. Signed-off-by: Eric Dumazet <[email protected]> Reviewed-by: Kuniyuki Iwashima <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
1 parent 758a8d5 commit 41db762

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

include/net/ip.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ static inline void inet_get_local_port_range(const struct net *net, int *low, in
356356
*low = range & 0xffff;
357357
*high = range >> 16;
358358
}
359-
void inet_sk_get_local_port_range(const struct sock *sk, int *low, int *high);
359+
bool inet_sk_get_local_port_range(const struct sock *sk, int *low, int *high);
360360

361361
#ifdef CONFIG_SYSCTL
362362
static inline bool inet_is_local_reserved_port(struct net *net, unsigned short port)

net/ipv4/inet_connection_sock.c

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,16 +117,25 @@ bool inet_rcv_saddr_any(const struct sock *sk)
117117
return !sk->sk_rcv_saddr;
118118
}
119119

120-
void inet_sk_get_local_port_range(const struct sock *sk, int *low, int *high)
120+
/**
121+
* inet_sk_get_local_port_range - fetch ephemeral ports range
122+
* @sk: socket
123+
* @low: pointer to low port
124+
* @high: pointer to high port
125+
*
126+
* Fetch netns port range (/proc/sys/net/ipv4/ip_local_port_range)
127+
* Range can be overridden if socket got IP_LOCAL_PORT_RANGE option.
128+
* Returns true if IP_LOCAL_PORT_RANGE was set on this socket.
129+
*/
130+
bool inet_sk_get_local_port_range(const struct sock *sk, int *low, int *high)
121131
{
122-
const struct inet_sock *inet = inet_sk(sk);
123-
const struct net *net = sock_net(sk);
124132
int lo, hi, sk_lo, sk_hi;
133+
bool local_range = false;
125134
u32 sk_range;
126135

127-
inet_get_local_port_range(net, &lo, &hi);
136+
inet_get_local_port_range(sock_net(sk), &lo, &hi);
128137

129-
sk_range = READ_ONCE(inet->local_port_range);
138+
sk_range = READ_ONCE(inet_sk(sk)->local_port_range);
130139
if (unlikely(sk_range)) {
131140
sk_lo = sk_range & 0xffff;
132141
sk_hi = sk_range >> 16;
@@ -135,10 +144,12 @@ void inet_sk_get_local_port_range(const struct sock *sk, int *low, int *high)
135144
lo = sk_lo;
136145
if (lo <= sk_hi && sk_hi <= hi)
137146
hi = sk_hi;
147+
local_range = true;
138148
}
139149

140150
*low = lo;
141151
*high = hi;
152+
return local_range;
142153
}
143154
EXPORT_SYMBOL(inet_sk_get_local_port_range);
144155

0 commit comments

Comments
 (0)