Skip to content

Commit 3f7e753

Browse files
edumazetdavem330
authored andcommitted
inet: move inet->freebind to inet->inet_flags
IP_FREEBIND socket option can now be set/read without locking the socket. Signed-off-by: Eric Dumazet <edumazet@google.com> Acked-by: Soheil Hassas Yeganeh <soheil@google.com> Reviewed-by: Simon Horman <horms@kernel.org> Reviewed-by: Matthieu Baerts <matthieu.baerts@tessares.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8e8cfb1 commit 3f7e753

File tree

7 files changed

+22
-22
lines changed

7 files changed

+22
-22
lines changed

include/net/inet_sock.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,6 @@ struct inet_sock {
231231
__u8 mc_ttl;
232232
__u8 pmtudisc;
233233
__u8 is_icsk:1,
234-
freebind:1,
235234
hdrincl:1,
236235
mc_loop:1,
237236
transparent:1,
@@ -271,6 +270,7 @@ enum {
271270

272271
INET_FLAGS_RECVERR = 9,
273272
INET_FLAGS_RECVERR_RFC4884 = 10,
273+
INET_FLAGS_FREEBIND = 11,
274274
};
275275

276276
/* cmsg flags for inet */
@@ -423,7 +423,8 @@ static inline bool inet_can_nonlocal_bind(struct net *net,
423423
struct inet_sock *inet)
424424
{
425425
return READ_ONCE(net->ipv4.sysctl_ip_nonlocal_bind) ||
426-
inet->freebind || inet->transparent;
426+
test_bit(INET_FLAGS_FREEBIND, &inet->inet_flags) ||
427+
inet->transparent;
427428
}
428429

429430
static inline bool inet_addr_valid_or_nonlocal(struct net *net,

include/net/ipv6.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,8 @@ static inline bool ipv6_can_nonlocal_bind(struct net *net,
937937
struct inet_sock *inet)
938938
{
939939
return net->ipv6.sysctl.ip_nonlocal_bind ||
940-
inet->freebind || inet->transparent;
940+
test_bit(INET_FLAGS_FREEBIND, &inet->inet_flags) ||
941+
inet->transparent;
941942
}
942943

943944
/* Sysctl settings for net ipv6.auto_flowlabels */

net/ipv4/inet_diag.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
184184
memset(&inet_sockopt, 0, sizeof(inet_sockopt));
185185
inet_sockopt.recverr = inet_test_bit(RECVERR, sk);
186186
inet_sockopt.is_icsk = inet->is_icsk;
187-
inet_sockopt.freebind = inet->freebind;
187+
inet_sockopt.freebind = inet_test_bit(FREEBIND, sk);
188188
inet_sockopt.hdrincl = inet->hdrincl;
189189
inet_sockopt.mc_loop = inet->mc_loop;
190190
inet_sockopt.transparent = inet->transparent;

net/ipv4/ip_sockglue.c

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,7 @@ EXPORT_SYMBOL(ip_sock_set_tos);
608608

609609
void ip_sock_set_freebind(struct sock *sk)
610610
{
611-
lock_sock(sk);
612-
inet_sk(sk)->freebind = true;
613-
release_sock(sk);
611+
inet_set_bit(FREEBIND, sk);
614612
}
615613
EXPORT_SYMBOL(ip_sock_set_freebind);
616614

@@ -985,6 +983,11 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname,
985983
return -EINVAL;
986984
inet_assign_bit(RECVERR_RFC4884, sk, val);
987985
return 0;
986+
case IP_FREEBIND:
987+
if (optlen < 1)
988+
return -EINVAL;
989+
inet_assign_bit(FREEBIND, sk, val);
990+
return 0;
988991
}
989992

990993
err = 0;
@@ -1310,12 +1313,6 @@ int do_ip_setsockopt(struct sock *sk, int level, int optname,
13101313
inet->mc_all = val;
13111314
break;
13121315

1313-
case IP_FREEBIND:
1314-
if (optlen < 1)
1315-
goto e_inval;
1316-
inet->freebind = !!val;
1317-
break;
1318-
13191316
case IP_IPSEC_POLICY:
13201317
case IP_XFRM_POLICY:
13211318
err = -EPERM;
@@ -1578,6 +1575,9 @@ int do_ip_getsockopt(struct sock *sk, int level, int optname,
15781575
case IP_RECVERR_RFC4884:
15791576
val = inet_test_bit(RECVERR_RFC4884, sk);
15801577
goto copyval;
1578+
case IP_FREEBIND:
1579+
val = inet_test_bit(FREEBIND, sk);
1580+
goto copyval;
15811581
}
15821582

15831583
if (needs_rtnl)
@@ -1737,9 +1737,6 @@ int do_ip_getsockopt(struct sock *sk, int level, int optname,
17371737
len -= msg.msg_controllen;
17381738
return copy_to_sockptr(optlen, &len, sizeof(int));
17391739
}
1740-
case IP_FREEBIND:
1741-
val = inet->freebind;
1742-
break;
17431740
case IP_TRANSPARENT:
17441741
val = inet->transparent;
17451742
break;

net/ipv6/ipv6_sockglue.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ int do_ipv6_setsockopt(struct sock *sk, int level, int optname,
641641
if (optlen < sizeof(int))
642642
goto e_inval;
643643
/* we also don't have a separate freebind bit for IPV6 */
644-
inet_sk(sk)->freebind = valbool;
644+
inet_assign_bit(FREEBIND, sk, valbool);
645645
retv = 0;
646646
break;
647647

@@ -1334,7 +1334,7 @@ int do_ipv6_getsockopt(struct sock *sk, int level, int optname,
13341334
break;
13351335

13361336
case IPV6_FREEBIND:
1337-
val = inet_sk(sk)->freebind;
1337+
val = inet_test_bit(FREEBIND, sk);
13381338
break;
13391339

13401340
case IPV6_RECVORIGDSTADDR:

net/mptcp/sockopt.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,8 @@ static int mptcp_setsockopt_v6(struct mptcp_sock *msk, int optname,
419419
inet_sk(sk)->transparent = inet_sk(ssk)->transparent;
420420
break;
421421
case IPV6_FREEBIND:
422-
inet_sk(sk)->freebind = inet_sk(ssk)->freebind;
422+
inet_assign_bit(FREEBIND, sk,
423+
inet_test_bit(FREEBIND, ssk));
423424
break;
424425
}
425426

@@ -704,7 +705,7 @@ static int mptcp_setsockopt_sol_ip_set_transparent(struct mptcp_sock *msk, int o
704705

705706
switch (optname) {
706707
case IP_FREEBIND:
707-
issk->freebind = inet_sk(sk)->freebind;
708+
inet_assign_bit(FREEBIND, ssk, inet_test_bit(FREEBIND, sk));
708709
break;
709710
case IP_TRANSPARENT:
710711
issk->transparent = inet_sk(sk)->transparent;
@@ -1441,7 +1442,7 @@ static void sync_socket_options(struct mptcp_sock *msk, struct sock *ssk)
14411442
__tcp_sock_set_nodelay(ssk, !!msk->nodelay);
14421443

14431444
inet_sk(ssk)->transparent = inet_sk(sk)->transparent;
1444-
inet_sk(ssk)->freebind = inet_sk(sk)->freebind;
1445+
inet_assign_bit(FREEBIND, ssk, inet_test_bit(FREEBIND, sk));
14451446
}
14461447

14471448
static void __mptcp_sockopt_sync(struct mptcp_sock *msk, struct sock *ssk)

net/sctp/protocol.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
360360
ret = inet_addr_type_table(net, addr->v4.sin_addr.s_addr, tb_id);
361361
if (addr->v4.sin_addr.s_addr != htonl(INADDR_ANY) &&
362362
ret != RTN_LOCAL &&
363-
!sp->inet.freebind &&
363+
!inet_test_bit(FREEBIND, sk) &&
364364
!READ_ONCE(net->ipv4.sysctl_ip_nonlocal_bind))
365365
return 0;
366366

0 commit comments

Comments
 (0)