net: diag: split inet_diag_dump_one_icsk into two
Subsystems (4)
PATCH v4 1/4 net: diag: split inet_diag_dump_one_icsk into two
Currently, inet_diag_dump_one_icsk finds a socket and then dumps its information to userspace. Split it into a part that finds the socket and a part that dumps the information.
--- include/linux/inet_diag.h | 5 +++++ net/ipv4/inet_diag.c | 42 +++++++++++++++++++++++++++--------------- 2 files changed, 32 insertions(+), 15 deletions(-)
include/linux/inet_diag.h +5 -0
@@ -3,6 +3,7 @@#include <uapi/linux/inet_diag.h> +struct net ;struct sock ;struct inet_hashinfo ;struct nlattr ;
@@ -41,6 +42,10 @@ int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb, const struct nlmsghdr *nlh, const struct inet_diag_req_v2 *req); +struct sock *inet_diag_find_one_icsk (struct net *net, +struct inet_hashinfo *hashinfo, +const struct inet_diag_req_v2 *req); +int inet_diag_bc_sk (const struct nlattr *_bc,struct sock *sk);extern int inet_diag_register (const struct inet_diag_handler *handler);
net/ipv4/inet_diag.c +27 -15
66 lines not on this page · raw message
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH v4 2/4 net: diag: Add the ability to destroy a socket from userspace.
This allows a privileged userspace process, such as a connection manager or system administration tool, to close sockets belonging to other apps when the network they were established on has disconnected. It is needed on laptops and mobile hosts to ensure that network switches / disconnects do not result in applications being blocked for long periods of time (minutes) in read or connect calls on TCP sockets that will never succeed because the IP address they are bound to is no longer on the system. Closing the sockets causes these calls to fail fast and allows the apps to reconnect on another network.
For many years Android kernels have supported this via an out-of-tree SIOCKILLADDR ioctl that is called on every RTM_DELADDR event, but this solution is cleaner, more robust and more flexible: the connection manager can iterate over all connections on the deleted IP address and close all of them. It can also be used to close all sockets opened by a given app process, for example if the user has restricted that app from using the network.
This patch adds a SOCK_DESTROY operation and a destroy function pointer to sock_diag_handler. It does not include any implementation code.
--- include/linux/sock_diag.h | 1 + include/uapi/linux/sock_diag.h | 1 + net/core/sock_diag.c | 11 ++++++++--- 3 files changed, 10 insertions(+), 3 deletions(-)
include/linux/sock_diag.h +1 -0
@@ -15,6 +15,7 @@ struct sock_diag_handler {__u8 family;int (*dump)(struct sk_buff *skb,struct nlmsghdr *nlh);int (*get_info)(struct sk_buff *skb,struct sock *sk); +int (*destroy)(struct sk_buff *skb,struct nlmsghdr *nlh); };int sock_diag_register (const struct sock_diag_handler *h);
include/uapi/linux/sock_diag.h +1 -0
@@ -4,6 +4,7 @@#include <linux/types.h> #define SOCK_DIAG_BY_FAMILY 20 +#define SOCK_DESTROY 21struct sock_diag_req {__u8 sdiag_family;
net/core/sock_diag.c +8 -3
@@ -214,7 +214,7 @@ void sock_diag_unregister(const struct sock_diag_handler *hnld) }EXPORT_SYMBOL_GPL (sock_diag_unregister); -static int __sock_diag_rcv_msg (struct sk_buff *skb,struct nlmsghdr *nlh) +static int __sock_diag_cmd (struct sk_buff *skb,struct nlmsghdr *nlh) {int err;struct sock_diag_req *req =nlmsg_data (nlh);
@@ -234,8 +234,12 @@ static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) hndl = sock_diag_handlers[req->sdiag_family];if (hndl ==NULL ) err = -ENOENT ; -else +else if (nlh->nlmsg_type ==SOCK_DIAG_BY_FAMILY ) err = hndl->dump (skb, nlh); +else if (nlh->nlmsg_type ==SOCK_DESTROY && hndl->destroy) + err = hndl->destroy (skb, nlh); +else + err = -EOPNOTSUPP ;mutex_unlock (&sock_diag_table_mutex);return err;
@@ -261,7 +265,8 @@ static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)return ret;case SOCK_DIAG_BY_FAMILY : -return __sock_diag_rcv_msg (skb, nlh); +case SOCK_DESTROY : +return __sock_diag_cmd (skb, nlh);default :return -EINVAL ; }
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH v4 3/4 net: diag: Support SOCK_DESTROY for inet sockets.
In reply to Lorenzo ColittiThis passes the SOCK_DESTROY operation to the underlying protocol diag handler, or returns -EINVAL if that handler does not define a destroy operation.
Most of this patch is just renaming functions. This is not strictly necessary, but it would be fairly counterintuitive to have the code to destroy inet sockets be in a function whose name starts with inet_diag_get.
--- include/linux/inet_diag.h | 4 ++++ net/ipv4/inet_diag.c | 23 +++++++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-)
include/linux/inet_diag.h +4 -0
@@ -24,6 +24,10 @@ struct inet_diag_handler {void (*idiag_get_info)(struct sock *sk,struct inet_diag_msg *r,void *info); + +int (*destroy)(struct sk_buff *in_skb, +const struct inet_diag_req_v2 *req); +__u16 idiag_type;__u16 idiag_info_size; };
net/ipv4/inet_diag.c +15 -8
@@ -426,7 +426,7 @@ out: }EXPORT_SYMBOL_GPL (inet_diag_dump_one_icsk); -static int inet_diag_get_exact (struct sk_buff *in_skb, +static int inet_diag_cmd_exact (int cmd,struct sk_buff *in_skb,const struct nlmsghdr *nlh,const struct inet_diag_req_v2 *req) {
@@ -436,8 +436,12 @@ static int inet_diag_get_exact(struct sk_buff *in_skb, handler =inet_diag_lock_handler (req->sdiag_protocol);if (IS_ERR (handler)) err =PTR_ERR (handler); -else +else if (cmd ==SOCK_DIAG_BY_FAMILY ) err = handler->dump_one (in_skb, nlh, req); +else if (cmd ==SOCK_DESTROY && handler->destroy) + err = handler->destroy (in_skb, req); +else + err = -EOPNOTSUPP ;inet_diag_unlock_handler (handler);return err;
@@ -950,7 +954,7 @@ static int inet_diag_get_exact_compat(struct sk_buff *in_skb, req.idiag_states = rc->idiag_states; req.id = rc->id; -return inet_diag_get_exact (in_skb, nlh, &req); +return inet_diag_cmd_exact (SOCK_DIAG_BY_FAMILY , in_skb, nlh, &req); }static int inet_diag_rcv_msg_compat (struct sk_buff *skb,struct nlmsghdr *nlh)
@@ -984,7 +988,7 @@ static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)return inet_diag_get_exact_compat (skb, nlh); } -static int inet_diag_handler_dump (struct sk_buff *skb,struct nlmsghdr *h) +static int inet_diag_handler_cmd (struct sk_buff *skb,struct nlmsghdr *h) {int hdrlen =sizeof (struct inet_diag_req_v2 );struct net *net =sock_net (skb->sk);
@@ -992,7 +996,8 @@ static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)if (nlmsg_len (h) < hdrlen)return -EINVAL ; -if (h->nlmsg_flags &NLM_F_DUMP ) { +if (h->nlmsg_type ==SOCK_DIAG_BY_FAMILY && + h->nlmsg_flags &NLM_F_DUMP ) {if (nlmsg_attrlen (h, hdrlen)) {struct nlattr *attr;
@@ -1011,7 +1016,7 @@ static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h) } } -return inet_diag_get_exact (skb, h,nlmsg_data (h)); +return inet_diag_cmd_exact (h->nlmsg_type, skb, h,nlmsg_data (h)); } static
@@ -1062,14 +1067,16 @@ int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)static const struct sock_diag_handler inet_diag_handler = { .family =AF_INET , - .dump = inet_diag_handler_dump, + .dump = inet_diag_handler_cmd, .get_info = inet_diag_handler_get_info, + .destroy = inet_diag_handler_cmd, };static const struct sock_diag_handler inet6_diag_handler = { .family =AF_INET6 , - .dump = inet_diag_handler_dump, + .dump = inet_diag_handler_cmd, .get_info = inet_diag_handler_get_info, + .destroy = inet_diag_handler_cmd, };int inet_diag_register (const struct inet_diag_handler *h)
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
PATCH v4 4/4 net: diag: Support destroying TCP sockets.
In reply to Lorenzo ColittiThis implements SOCK_DESTROY for TCP sockets. It causes all blocking calls on the socket to fail fast with ECONNABORTED and causes a protocol close of the socket. It informs the other end of the connection by sending a RST, i.e., initiating a TCP ABORT as per RFC 793. ECONNABORTED was chosen for consistency with FreeBSD.
--- net/ipv4/Kconfig | 13 +++++++++++++ net/ipv4/tcp_diag.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ net/ipv4/tcp_output.c | 1 + 3 files changed, 60 insertions(+)
net/ipv4/Kconfig +13 -0
@@ -436,6 +436,19 @@ config INET_UDP_DIAG Support for UDP socket monitoring interface used by the ss tool. If unsure, say Y. +config INET_DIAG_DESTROY + bool "INET: allow privileged process to administratively close sockets" + depends on INET_DIAG && (IPV6 || IPV6=n) + default n + ---help--- + Provides a SOCK_DESTROY operation that allows privileged processes + (e.g., a connection manager or a network administration tool such as + ss) to close sockets opened by other processes. Closing a socket in + this way interrupts any blocking read/writes/connect operations on + the socket and causes future socket calls to behave as if the socket + had been disconnected. + If unsure, say N. + menuconfig TCP_CONG_ADVANCED bool "TCP: advanced congestion control" ---help---
net/ipv4/tcp_diag.c +46 -0
64 lines not on this page · raw message
net/ipv4/tcp_output.c +1 -0
@@ -2894,6 +2894,7 @@ void tcp_send_active_reset(struct sock *sk, gfp_t priority)TCP_INC_STATS (sock_net (sk),TCP_MIB_OUTRSTS ); } +EXPORT_SYMBOL_GPL (tcp_send_active_reset);/* Send a crossed SYN-ACK during socket establishment. * WARNING: This routine must only be called when we have already sent
Signature · 1 line
2.6.0.rc2.230.g3dd15c0
Re: [PATCH v4 4/4] net: diag: Support destroying TCP sockets.
net/ipv4/tcp_diag.c static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh, · line 77
Quoted 6 lines · Lorenzo Colitti
net/ipv4/tcp_diag.c +6
-0+/* Don't race with BH socket closes such as inet_csk_listen_stop. */ +local_bh_disable (); +bh_lock_sock (sk); + +if (!sock_flag (sk,SOCK_DEAD )) { +smp_wmb ();/* Be consistent with tcp_reset */
This barrier is absolutely useless here, it must be moved _after_ sk->sk_err = ECONNABORTED.
Since you copied tcp_reset() code, please do not reorder barriers.
Also, comment in tcp_reset() is better IMO
/* This barrier is coupled with smp_rmb() in tcp_poll() */
net/ipv4/tcp_diag.c static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh, · line 79
Quoted 2 lines
net/ipv4/tcp_diag.c +2
-0+ sk->sk_err =ECONNABORTED ; + sk->sk_error_report (sk);
Why not using tcp_need_reset() here, otherwise we might send spurious RST for some tcp states.
Ideally, packetdrill tests would be nice.
net/ipv4/tcp_diag.c static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh, · line 82
Quoted 3 lines
net/ipv4/tcp_diag.c +3
-0+tcp_send_active_reset (sk,GFP_ATOMIC ); +tcp_done (sk); + }
Please do not send a v5 before letting other people comment on v3 & v4, no need to flood netdev with many versions sent the same day.
Thanks.