1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * tcp_diag.c Module for monitoring TCP transport protocols sockets.
5 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 #include <linux/module.h>
10 #include <linux/sock_diag.h>
11 #include <linux/inet_diag.h>
13 #include <linux/tcp.h>
15 #include <net/netlink.h>
18 static void tcp_diag_get_info(struct sock
*sk
, struct inet_diag_msg
*r
,
21 struct tcp_info
*info
= _info
;
23 if (inet_sk_state_load(sk
) == TCP_LISTEN
) {
24 r
->idiag_rqueue
= READ_ONCE(sk
->sk_ack_backlog
);
25 r
->idiag_wqueue
= READ_ONCE(sk
->sk_max_ack_backlog
);
26 } else if (sk
->sk_type
== SOCK_STREAM
) {
27 const struct tcp_sock
*tp
= tcp_sk(sk
);
29 r
->idiag_rqueue
= max_t(int, READ_ONCE(tp
->rcv_nxt
) -
30 READ_ONCE(tp
->copied_seq
), 0);
31 r
->idiag_wqueue
= READ_ONCE(tp
->write_seq
) - tp
->snd_una
;
34 tcp_get_info(sk
, info
);
37 #ifdef CONFIG_TCP_MD5SIG
38 static void tcp_diag_md5sig_fill(struct tcp_diag_md5sig
*info
,
39 const struct tcp_md5sig_key
*key
)
41 info
->tcpm_family
= key
->family
;
42 info
->tcpm_prefixlen
= key
->prefixlen
;
43 info
->tcpm_keylen
= key
->keylen
;
44 memcpy(info
->tcpm_key
, key
->key
, key
->keylen
);
46 if (key
->family
== AF_INET
)
47 info
->tcpm_addr
[0] = key
->addr
.a4
.s_addr
;
48 #if IS_ENABLED(CONFIG_IPV6)
49 else if (key
->family
== AF_INET6
)
50 memcpy(&info
->tcpm_addr
, &key
->addr
.a6
,
51 sizeof(info
->tcpm_addr
));
55 static int tcp_diag_put_md5sig(struct sk_buff
*skb
,
56 const struct tcp_md5sig_info
*md5sig
)
58 const struct tcp_md5sig_key
*key
;
59 struct tcp_diag_md5sig
*info
;
63 hlist_for_each_entry_rcu(key
, &md5sig
->head
, node
)
65 if (md5sig_count
== 0)
68 attr
= nla_reserve(skb
, INET_DIAG_MD5SIG
,
69 md5sig_count
* sizeof(struct tcp_diag_md5sig
));
73 info
= nla_data(attr
);
74 memset(info
, 0, md5sig_count
* sizeof(struct tcp_diag_md5sig
));
75 hlist_for_each_entry_rcu(key
, &md5sig
->head
, node
) {
76 tcp_diag_md5sig_fill(info
++, key
);
77 if (--md5sig_count
== 0)
85 static int tcp_diag_put_ulp(struct sk_buff
*skb
, struct sock
*sk
,
86 const struct tcp_ulp_ops
*ulp_ops
)
91 nest
= nla_nest_start_noflag(skb
, INET_DIAG_ULP_INFO
);
95 err
= nla_put_string(skb
, INET_ULP_INFO_NAME
, ulp_ops
->name
);
99 if (ulp_ops
->get_info
)
100 err
= ulp_ops
->get_info(sk
, skb
);
104 nla_nest_end(skb
, nest
);
108 nla_nest_cancel(skb
, nest
);
112 static int tcp_diag_get_aux(struct sock
*sk
, bool net_admin
,
115 struct inet_connection_sock
*icsk
= inet_csk(sk
);
118 #ifdef CONFIG_TCP_MD5SIG
120 struct tcp_md5sig_info
*md5sig
;
123 md5sig
= rcu_dereference(tcp_sk(sk
)->md5sig_info
);
125 err
= tcp_diag_put_md5sig(skb
, md5sig
);
133 const struct tcp_ulp_ops
*ulp_ops
;
135 ulp_ops
= icsk
->icsk_ulp_ops
;
137 err
= tcp_diag_put_ulp(skb
, sk
, ulp_ops
);
144 static size_t tcp_diag_get_aux_size(struct sock
*sk
, bool net_admin
)
146 struct inet_connection_sock
*icsk
= inet_csk(sk
);
149 #ifdef CONFIG_TCP_MD5SIG
150 if (net_admin
&& sk_fullsock(sk
)) {
151 const struct tcp_md5sig_info
*md5sig
;
152 const struct tcp_md5sig_key
*key
;
153 size_t md5sig_count
= 0;
156 md5sig
= rcu_dereference(tcp_sk(sk
)->md5sig_info
);
158 hlist_for_each_entry_rcu(key
, &md5sig
->head
, node
)
162 size
+= nla_total_size(md5sig_count
*
163 sizeof(struct tcp_diag_md5sig
));
167 if (net_admin
&& sk_fullsock(sk
)) {
168 const struct tcp_ulp_ops
*ulp_ops
;
170 ulp_ops
= icsk
->icsk_ulp_ops
;
172 size
+= nla_total_size(0) +
173 nla_total_size(TCP_ULP_NAME_MAX
);
174 if (ulp_ops
->get_info_size
)
175 size
+= ulp_ops
->get_info_size(sk
);
181 static void tcp_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
182 const struct inet_diag_req_v2
*r
)
184 inet_diag_dump_icsk(&tcp_hashinfo
, skb
, cb
, r
);
187 static int tcp_diag_dump_one(struct netlink_callback
*cb
,
188 const struct inet_diag_req_v2
*req
)
190 return inet_diag_dump_one_icsk(&tcp_hashinfo
, cb
, req
);
193 #ifdef CONFIG_INET_DIAG_DESTROY
194 static int tcp_diag_destroy(struct sk_buff
*in_skb
,
195 const struct inet_diag_req_v2
*req
)
197 struct net
*net
= sock_net(in_skb
->sk
);
198 struct sock
*sk
= inet_diag_find_one_icsk(net
, &tcp_hashinfo
, req
);
204 err
= sock_diag_destroy(sk
, ECONNABORTED
);
212 static const struct inet_diag_handler tcp_diag_handler
= {
213 .dump
= tcp_diag_dump
,
214 .dump_one
= tcp_diag_dump_one
,
215 .idiag_get_info
= tcp_diag_get_info
,
216 .idiag_get_aux
= tcp_diag_get_aux
,
217 .idiag_get_aux_size
= tcp_diag_get_aux_size
,
218 .idiag_type
= IPPROTO_TCP
,
219 .idiag_info_size
= sizeof(struct tcp_info
),
220 #ifdef CONFIG_INET_DIAG_DESTROY
221 .destroy
= tcp_diag_destroy
,
225 static int __init
tcp_diag_init(void)
227 return inet_diag_register(&tcp_diag_handler
);
230 static void __exit
tcp_diag_exit(void)
232 inet_diag_unregister(&tcp_diag_handler
);
235 module_init(tcp_diag_init
);
236 module_exit(tcp_diag_exit
);
237 MODULE_LICENSE("GPL");
238 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2-6 /* AF_INET - IPPROTO_TCP */);