xref: /linux/net/ipv4/tcp_diag.c (revision 00db41243e8d5032c2e0f5bf6063bb19324bfdb3)
117b085eaSArnaldo Carvalho de Melo /*
217b085eaSArnaldo Carvalho de Melo  * tcp_diag.c	Module for monitoring TCP transport protocols sockets.
317b085eaSArnaldo Carvalho de Melo  *
417b085eaSArnaldo Carvalho de Melo  * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
517b085eaSArnaldo Carvalho de Melo  *
617b085eaSArnaldo Carvalho de Melo  *	This program is free software; you can redistribute it and/or
717b085eaSArnaldo Carvalho de Melo  *      modify it under the terms of the GNU General Public License
817b085eaSArnaldo Carvalho de Melo  *      as published by the Free Software Foundation; either version
917b085eaSArnaldo Carvalho de Melo  *      2 of the License, or (at your option) any later version.
1017b085eaSArnaldo Carvalho de Melo  */
1117b085eaSArnaldo Carvalho de Melo 
1217b085eaSArnaldo Carvalho de Melo #include <linux/module.h>
1317b085eaSArnaldo Carvalho de Melo #include <linux/inet_diag.h>
1417b085eaSArnaldo Carvalho de Melo 
1517b085eaSArnaldo Carvalho de Melo #include <linux/tcp.h>
1617b085eaSArnaldo Carvalho de Melo 
1717b085eaSArnaldo Carvalho de Melo #include <net/tcp.h>
1817b085eaSArnaldo Carvalho de Melo 
1917b085eaSArnaldo Carvalho de Melo static void tcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
2017b085eaSArnaldo Carvalho de Melo 			      void *_info)
2117b085eaSArnaldo Carvalho de Melo {
2217b085eaSArnaldo Carvalho de Melo 	const struct tcp_sock *tp = tcp_sk(sk);
2317b085eaSArnaldo Carvalho de Melo 	struct tcp_info *info = _info;
2417b085eaSArnaldo Carvalho de Melo 
255ee3afbaSRick Jones 	if (sk->sk_state == TCP_LISTEN) {
2647da8ee6SSridhar Samudrala 		r->idiag_rqueue = sk->sk_ack_backlog;
275ee3afbaSRick Jones 		r->idiag_wqueue = sk->sk_max_ack_backlog;
285ee3afbaSRick Jones 	} else {
2949d09007SEric Dumazet 		r->idiag_rqueue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
3017b085eaSArnaldo Carvalho de Melo 		r->idiag_wqueue = tp->write_seq - tp->snd_una;
315ee3afbaSRick Jones 	}
32*00db4124SIan Morris 	if (info)
3317b085eaSArnaldo Carvalho de Melo 		tcp_get_info(sk, info);
3417b085eaSArnaldo Carvalho de Melo }
3517b085eaSArnaldo Carvalho de Melo 
361942c518SPavel Emelyanov static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
3734160ea3SEric Dumazet 			  const struct inet_diag_req_v2 *r, struct nlattr *bc)
381942c518SPavel Emelyanov {
391942c518SPavel Emelyanov 	inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r, bc);
401942c518SPavel Emelyanov }
411942c518SPavel Emelyanov 
421942c518SPavel Emelyanov static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
4334160ea3SEric Dumazet 			     const struct inet_diag_req_v2 *req)
441942c518SPavel Emelyanov {
451942c518SPavel Emelyanov 	return inet_diag_dump_one_icsk(&tcp_hashinfo, in_skb, nlh, req);
461942c518SPavel Emelyanov }
471942c518SPavel Emelyanov 
48a7a0d6a8SEric Dumazet static const struct inet_diag_handler tcp_diag_handler = {
491942c518SPavel Emelyanov 	.dump		 = tcp_diag_dump,
501942c518SPavel Emelyanov 	.dump_one	 = tcp_diag_dump_one,
5117b085eaSArnaldo Carvalho de Melo 	.idiag_get_info	 = tcp_diag_get_info,
52f13c95f0SPavel Emelyanov 	.idiag_type	 = IPPROTO_TCP,
5317b085eaSArnaldo Carvalho de Melo };
5417b085eaSArnaldo Carvalho de Melo 
5517b085eaSArnaldo Carvalho de Melo static int __init tcp_diag_init(void)
5617b085eaSArnaldo Carvalho de Melo {
5717b085eaSArnaldo Carvalho de Melo 	return inet_diag_register(&tcp_diag_handler);
5817b085eaSArnaldo Carvalho de Melo }
5917b085eaSArnaldo Carvalho de Melo 
6017b085eaSArnaldo Carvalho de Melo static void __exit tcp_diag_exit(void)
6117b085eaSArnaldo Carvalho de Melo {
6217b085eaSArnaldo Carvalho de Melo 	inet_diag_unregister(&tcp_diag_handler);
6317b085eaSArnaldo Carvalho de Melo }
6417b085eaSArnaldo Carvalho de Melo 
6517b085eaSArnaldo Carvalho de Melo module_init(tcp_diag_init);
6617b085eaSArnaldo Carvalho de Melo module_exit(tcp_diag_exit);
6717b085eaSArnaldo Carvalho de Melo MODULE_LICENSE("GPL");
68aec8dc62SPavel Emelyanov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-6 /* AF_INET - IPPROTO_TCP */);
69