Skip to content

Commit 8912bbc

Browse files
author
Paolo Abeni
committed
net: Return errno in sk->sk_prot->get_port().
Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=2166482 Tested: vs bz reproducer Conflicts: different context in inet_csk_get_port and udp_lib_get_port,\ as rhel-9 lacks the upstream commit 08eaef9 ("tcp: Clean up \ some functions.") and upstream commit 919dfa0 ("udp: Clean up \ some functions.") Upstream commit: commit 7a7160e Author: Kuniyuki Iwashima <kuniyu@amazon.com> Date: Fri Nov 18 10:25:06 2022 -0800 net: Return errno in sk->sk_prot->get_port(). We assume the correct errno is -EADDRINUSE when sk->sk_prot->get_port() fails, so some ->get_port() functions return just 1 on failure and the callers return -EADDRINUSE instead. However, mptcp_get_port() can return -EINVAL. Let's not ignore the error. Note the only exception is inet_autobind(), all of whose callers return -EAGAIN instead. Fixes: cec37a6 ("mptcp: Handle MP_CAPABLE options for outgoing connections") Signed-off-by: Kuniyuki Iwashima <kuniyu@amazon.com> Signed-off-by: David S. Miller <davem@davemloft.net> Signed-off-by: Paolo Abeni <pabeni@redhat.com>
1 parent cceb43f commit 8912bbc

File tree

5 files changed

+10
-9
lines changed

5 files changed

+10
-9
lines changed

net/ipv4/af_inet.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,9 +525,9 @@ int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
525525
/* Make sure we are allowed to bind here. */
526526
if (snum || !(inet->bind_address_no_port ||
527527
(flags & BIND_FORCE_ADDRESS_NO_PORT))) {
528-
if (sk->sk_prot->get_port(sk, snum)) {
528+
err = sk->sk_prot->get_port(sk, snum);
529+
if (err) {
529530
inet->inet_saddr = inet->inet_rcv_saddr = 0;
530-
err = -EADDRINUSE;
531531
goto out_release_sock;
532532
}
533533
if (!(flags & BIND_FROM_BPF)) {

net/ipv4/inet_connection_sock.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
367367
{
368368
bool reuse = sk->sk_reuse && sk->sk_state != TCP_LISTEN;
369369
struct inet_hashinfo *hinfo = sk->sk_prot->h.hashinfo;
370-
int ret = 1, port = snum;
370+
int ret = -EADDRINUSE, port = snum;
371371
struct inet_bind_hashbucket *head;
372372
struct net *net = sock_net(sk);
373373
struct inet_bind_bucket *tb = NULL;
@@ -1054,7 +1054,7 @@ int inet_csk_listen_start(struct sock *sk, int backlog)
10541054
{
10551055
struct inet_connection_sock *icsk = inet_csk(sk);
10561056
struct inet_sock *inet = inet_sk(sk);
1057-
int err = -EADDRINUSE;
1057+
int err;
10581058

10591059
err = inet_ulp_can_listen(sk);
10601060
if (unlikely(err))
@@ -1074,7 +1074,8 @@ int inet_csk_listen_start(struct sock *sk, int backlog)
10741074
* after validation is complete.
10751075
*/
10761076
inet_sk_state_store(sk, TCP_LISTEN);
1077-
if (!sk->sk_prot->get_port(sk, inet->inet_num)) {
1077+
err = sk->sk_prot->get_port(sk, inet->inet_num);
1078+
if (!err) {
10781079
inet->inet_sport = htons(inet->inet_num);
10791080

10801081
sk_dst_reset(sk);

net/ipv4/ping.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ int ping_get_port(struct sock *sk, unsigned short ident)
142142

143143
fail:
144144
spin_unlock(&ping_table.lock);
145-
return 1;
145+
return -EADDRINUSE;
146146
}
147147
EXPORT_SYMBOL_GPL(ping_get_port);
148148

net/ipv4/udp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
234234
{
235235
struct udp_hslot *hslot, *hslot2;
236236
struct udp_table *udptable = sk->sk_prot->h.udp_table;
237-
int error = 1;
238237
struct net *net = sock_net(sk);
238+
int error = -EADDRINUSE;
239239

240240
if (!snum) {
241241
int low, high, remaining;

net/ipv6/af_inet6.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,10 @@ static int __inet6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
411411
/* Make sure we are allowed to bind here. */
412412
if (snum || !(inet->bind_address_no_port ||
413413
(flags & BIND_FORCE_ADDRESS_NO_PORT))) {
414-
if (sk->sk_prot->get_port(sk, snum)) {
414+
err = sk->sk_prot->get_port(sk, snum);
415+
if (err) {
415416
sk->sk_ipv6only = saved_ipv6only;
416417
inet_reset_saddr(sk);
417-
err = -EADDRINUSE;
418418
goto out;
419419
}
420420
if (!(flags & BIND_FROM_BPF)) {

0 commit comments

Comments
 (0)