Skip to content

Commit 7a7160e

Browse files
q2vendavem330
authored andcommitted
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 <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 1cb5072 commit 7a7160e

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
@@ -522,9 +522,9 @@ int __inet_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len,
522522
/* Make sure we are allowed to bind here. */
523523
if (snum || !(inet->bind_address_no_port ||
524524
(flags & BIND_FORCE_ADDRESS_NO_PORT))) {
525-
if (sk->sk_prot->get_port(sk, snum)) {
525+
err = sk->sk_prot->get_port(sk, snum);
526+
if (err) {
526527
inet->inet_saddr = inet->inet_rcv_saddr = 0;
527-
err = -EADDRINUSE;
528528
goto out_release_sock;
529529
}
530530
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
@@ -471,11 +471,11 @@ int inet_csk_get_port(struct sock *sk, unsigned short snum)
471471
bool reuse = sk->sk_reuse && sk->sk_state != TCP_LISTEN;
472472
bool found_port = false, check_bind_conflict = true;
473473
bool bhash_created = false, bhash2_created = false;
474+
int ret = -EADDRINUSE, port = snum, l3mdev;
474475
struct inet_bind_hashbucket *head, *head2;
475476
struct inet_bind2_bucket *tb2 = NULL;
476477
struct inet_bind_bucket *tb = NULL;
477478
bool head2_lock_acquired = false;
478-
int ret = 1, port = snum, l3mdev;
479479
struct net *net = sock_net(sk);
480480

481481
l3mdev = inet_sk_bound_l3mdev(sk);
@@ -1186,7 +1186,7 @@ int inet_csk_listen_start(struct sock *sk)
11861186
{
11871187
struct inet_connection_sock *icsk = inet_csk(sk);
11881188
struct inet_sock *inet = inet_sk(sk);
1189-
int err = -EADDRINUSE;
1189+
int err;
11901190

11911191
reqsk_queue_alloc(&icsk->icsk_accept_queue);
11921192

@@ -1202,7 +1202,8 @@ int inet_csk_listen_start(struct sock *sk)
12021202
* after validation is complete.
12031203
*/
12041204
inet_sk_state_store(sk, TCP_LISTEN);
1205-
if (!sk->sk_prot->get_port(sk, inet->inet_num)) {
1205+
err = sk->sk_prot->get_port(sk, inet->inet_num);
1206+
if (!err) {
12061207
inet->inet_sport = htons(inet->inet_num);
12071208

12081209
sk_dst_reset(sk);

net/ipv4/ping.c

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

139139
fail:
140140
spin_unlock(&ping_table.lock);
141-
return 1;
141+
return -EADDRINUSE;
142142
}
143143
EXPORT_SYMBOL_GPL(ping_get_port);
144144

net/ipv4/udp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
240240
struct udp_table *udptable = udp_get_table_prot(sk);
241241
struct udp_hslot *hslot, *hslot2;
242242
struct net *net = sock_net(sk);
243-
int error = 1;
243+
int error = -EADDRINUSE;
244244

245245
if (!snum) {
246246
DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);

net/ipv6/af_inet6.c

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

0 commit comments

Comments
 (0)