Commit b58f0e8f authored by Christoph Hellwig's avatar Christoph Hellwig Committed by David S. Miller

net: add sock_set_reuseaddr

Add a helper to directly set the SO_REUSEADDR sockopt from kernel space
without going through a fake uaccess.

For this the iscsi target now has to formally depend on inet to avoid
a mostly theoretical compile failure.  For actual operation it already
did depend on having ipv4 or ipv6 support.
Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Acked-by: default avatarSagi Grimberg <sagi@grimberg.me>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 1eba1110
...@@ -1312,17 +1312,14 @@ static void siw_cm_llp_state_change(struct sock *sk) ...@@ -1312,17 +1312,14 @@ static void siw_cm_llp_state_change(struct sock *sk)
static int kernel_bindconnect(struct socket *s, struct sockaddr *laddr, static int kernel_bindconnect(struct socket *s, struct sockaddr *laddr,
struct sockaddr *raddr) struct sockaddr *raddr)
{ {
int rv, flags = 0, s_val = 1; int rv, flags = 0;
size_t size = laddr->sa_family == AF_INET ? size_t size = laddr->sa_family == AF_INET ?
sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
/* /*
* Make address available again asap. * Make address available again asap.
*/ */
rv = kernel_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&s_val, sock_set_reuseaddr(s->sk);
sizeof(s_val));
if (rv < 0)
return rv;
rv = s->ops->bind(s, laddr, size); rv = s->ops->bind(s, laddr, size);
if (rv < 0) if (rv < 0)
...@@ -1781,7 +1778,7 @@ int siw_create_listen(struct iw_cm_id *id, int backlog) ...@@ -1781,7 +1778,7 @@ int siw_create_listen(struct iw_cm_id *id, int backlog)
struct siw_cep *cep = NULL; struct siw_cep *cep = NULL;
struct siw_device *sdev = to_siw_dev(id->device); struct siw_device *sdev = to_siw_dev(id->device);
int addr_family = id->local_addr.ss_family; int addr_family = id->local_addr.ss_family;
int rv = 0, s_val; int rv = 0;
if (addr_family != AF_INET && addr_family != AF_INET6) if (addr_family != AF_INET && addr_family != AF_INET6)
return -EAFNOSUPPORT; return -EAFNOSUPPORT;
...@@ -1793,13 +1790,8 @@ int siw_create_listen(struct iw_cm_id *id, int backlog) ...@@ -1793,13 +1790,8 @@ int siw_create_listen(struct iw_cm_id *id, int backlog)
/* /*
* Allow binding local port when still in TIME_WAIT from last close. * Allow binding local port when still in TIME_WAIT from last close.
*/ */
s_val = 1; sock_set_reuseaddr(s->sk);
rv = kernel_setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (char *)&s_val,
sizeof(s_val));
if (rv) {
siw_dbg(id->device, "setsockopt error: %d\n", rv);
goto error;
}
if (addr_family == AF_INET) { if (addr_family == AF_INET) {
struct sockaddr_in *laddr = &to_sockaddr_in(id->local_addr); struct sockaddr_in *laddr = &to_sockaddr_in(id->local_addr);
......
...@@ -1632,6 +1632,7 @@ static int nvmet_tcp_add_port(struct nvmet_port *nport) ...@@ -1632,6 +1632,7 @@ static int nvmet_tcp_add_port(struct nvmet_port *nport)
port->sock->sk->sk_user_data = port; port->sock->sk->sk_user_data = port;
port->data_ready = port->sock->sk->sk_data_ready; port->data_ready = port->sock->sk->sk_data_ready;
port->sock->sk->sk_data_ready = nvmet_tcp_listen_data_ready; port->sock->sk->sk_data_ready = nvmet_tcp_listen_data_ready;
sock_set_reuseaddr(port->sock->sk);
opt = 1; opt = 1;
ret = kernel_setsockopt(port->sock, IPPROTO_TCP, ret = kernel_setsockopt(port->sock, IPPROTO_TCP,
...@@ -1641,13 +1642,6 @@ static int nvmet_tcp_add_port(struct nvmet_port *nport) ...@@ -1641,13 +1642,6 @@ static int nvmet_tcp_add_port(struct nvmet_port *nport)
goto err_sock; goto err_sock;
} }
ret = kernel_setsockopt(port->sock, SOL_SOCKET, SO_REUSEADDR,
(char *)&opt, sizeof(opt));
if (ret) {
pr_err("failed to set SO_REUSEADDR sock opt %d\n", ret);
goto err_sock;
}
if (so_priority > 0) { if (so_priority > 0) {
ret = kernel_setsockopt(port->sock, SOL_SOCKET, SO_PRIORITY, ret = kernel_setsockopt(port->sock, SOL_SOCKET, SO_PRIORITY,
(char *)&so_priority, sizeof(so_priority)); (char *)&so_priority, sizeof(so_priority));
......
# SPDX-License-Identifier: GPL-2.0-only # SPDX-License-Identifier: GPL-2.0-only
config ISCSI_TARGET config ISCSI_TARGET
tristate "Linux-iSCSI.org iSCSI Target Mode Stack" tristate "Linux-iSCSI.org iSCSI Target Mode Stack"
depends on NET depends on INET
select CRYPTO select CRYPTO
select CRYPTO_CRC32C select CRYPTO_CRC32C
select CRYPTO_CRC32C_INTEL if X86 select CRYPTO_CRC32C_INTEL if X86
......
...@@ -909,14 +909,7 @@ int iscsit_setup_np( ...@@ -909,14 +909,7 @@ int iscsit_setup_np(
} }
} }
/* FIXME: Someone please explain why this is endian-safe */ sock_set_reuseaddr(sock->sk);
ret = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR,
(char *)&opt, sizeof(opt));
if (ret < 0) {
pr_err("kernel_setsockopt() for SO_REUSEADDR"
" failed\n");
goto fail;
}
ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND, ret = kernel_setsockopt(sock, IPPROTO_IP, IP_FREEBIND,
(char *)&opt, sizeof(opt)); (char *)&opt, sizeof(opt));
......
...@@ -1127,12 +1127,8 @@ static struct socket *tcp_create_listen_sock(struct connection *con, ...@@ -1127,12 +1127,8 @@ static struct socket *tcp_create_listen_sock(struct connection *con,
kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one, kernel_setsockopt(sock, SOL_TCP, TCP_NODELAY, (char *)&one,
sizeof(one)); sizeof(one));
result = kernel_setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, sock_set_reuseaddr(sock->sk);
(char *)&one, sizeof(one));
if (result < 0) {
log_print("Failed to set SO_REUSEADDR on socket: %d", result);
}
write_lock_bh(&sock->sk->sk_callback_lock); write_lock_bh(&sock->sk->sk_callback_lock);
sock->sk->sk_user_data = con; sock->sk->sk_user_data = con;
save_listen_callbacks(sock); save_listen_callbacks(sock);
......
...@@ -2688,4 +2688,6 @@ static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif) ...@@ -2688,4 +2688,6 @@ static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif)
void sock_def_readable(struct sock *sk); void sock_def_readable(struct sock *sk);
void sock_set_reuseaddr(struct sock *sk);
#endif /* _SOCK_H */ #endif /* _SOCK_H */
...@@ -712,6 +712,14 @@ bool sk_mc_loop(struct sock *sk) ...@@ -712,6 +712,14 @@ bool sk_mc_loop(struct sock *sk)
} }
EXPORT_SYMBOL(sk_mc_loop); EXPORT_SYMBOL(sk_mc_loop);
void sock_set_reuseaddr(struct sock *sk)
{
lock_sock(sk);
sk->sk_reuse = SK_CAN_REUSE;
release_sock(sk);
}
EXPORT_SYMBOL(sock_set_reuseaddr);
/* /*
* This is meant for all protocols to use and covers goings on * This is meant for all protocols to use and covers goings on
* at the socket level. Everything here is generic. * at the socket level. Everything here is generic.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment