tcp_diag.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * tcp_diag.c Module for monitoring TCP transport protocols sockets.
  3. *
  4. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/net.h>
  13. #include <linux/sock_diag.h>
  14. #include <linux/inet_diag.h>
  15. #include <linux/tcp.h>
  16. #include <net/netlink.h>
  17. #include <net/tcp.h>
  18. static void tcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
  19. void *_info)
  20. {
  21. struct tcp_info *info = _info;
  22. if (inet_sk_state_load(sk) == TCP_LISTEN) {
  23. r->idiag_rqueue = sk->sk_ack_backlog;
  24. r->idiag_wqueue = sk->sk_max_ack_backlog;
  25. } else if (sk->sk_type == SOCK_STREAM) {
  26. const struct tcp_sock *tp = tcp_sk(sk);
  27. r->idiag_rqueue = max_t(int, READ_ONCE(tp->rcv_nxt) - tp->copied_seq, 0);
  28. r->idiag_wqueue = tp->write_seq - tp->snd_una;
  29. }
  30. if (info)
  31. tcp_get_info(sk, info);
  32. }
  33. #ifdef CONFIG_TCP_MD5SIG
  34. static void tcp_diag_md5sig_fill(struct tcp_diag_md5sig *info,
  35. const struct tcp_md5sig_key *key)
  36. {
  37. info->tcpm_family = key->family;
  38. info->tcpm_prefixlen = key->prefixlen;
  39. info->tcpm_keylen = key->keylen;
  40. memcpy(info->tcpm_key, key->key, key->keylen);
  41. if (key->family == AF_INET)
  42. info->tcpm_addr[0] = key->addr.a4.s_addr;
  43. #if IS_ENABLED(CONFIG_IPV6)
  44. else if (key->family == AF_INET6)
  45. memcpy(&info->tcpm_addr, &key->addr.a6,
  46. sizeof(info->tcpm_addr));
  47. #endif
  48. }
  49. static int tcp_diag_put_md5sig(struct sk_buff *skb,
  50. const struct tcp_md5sig_info *md5sig)
  51. {
  52. const struct tcp_md5sig_key *key;
  53. struct tcp_diag_md5sig *info;
  54. struct nlattr *attr;
  55. int md5sig_count = 0;
  56. hlist_for_each_entry_rcu(key, &md5sig->head, node)
  57. md5sig_count++;
  58. if (md5sig_count == 0)
  59. return 0;
  60. attr = nla_reserve(skb, INET_DIAG_MD5SIG,
  61. md5sig_count * sizeof(struct tcp_diag_md5sig));
  62. if (!attr)
  63. return -EMSGSIZE;
  64. info = nla_data(attr);
  65. memset(info, 0, md5sig_count * sizeof(struct tcp_diag_md5sig));
  66. hlist_for_each_entry_rcu(key, &md5sig->head, node) {
  67. tcp_diag_md5sig_fill(info++, key);
  68. if (--md5sig_count == 0)
  69. break;
  70. }
  71. return 0;
  72. }
  73. #endif
  74. static int tcp_diag_get_aux(struct sock *sk, bool net_admin,
  75. struct sk_buff *skb)
  76. {
  77. #ifdef CONFIG_TCP_MD5SIG
  78. if (net_admin) {
  79. struct tcp_md5sig_info *md5sig;
  80. int err = 0;
  81. rcu_read_lock();
  82. md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
  83. if (md5sig)
  84. err = tcp_diag_put_md5sig(skb, md5sig);
  85. rcu_read_unlock();
  86. if (err < 0)
  87. return err;
  88. }
  89. #endif
  90. return 0;
  91. }
  92. static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin)
  93. {
  94. size_t size = 0;
  95. #ifdef CONFIG_TCP_MD5SIG
  96. if (net_admin && sk_fullsock(sk)) {
  97. const struct tcp_md5sig_info *md5sig;
  98. const struct tcp_md5sig_key *key;
  99. size_t md5sig_count = 0;
  100. rcu_read_lock();
  101. md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
  102. if (md5sig) {
  103. hlist_for_each_entry_rcu(key, &md5sig->head, node)
  104. md5sig_count++;
  105. }
  106. rcu_read_unlock();
  107. size += nla_total_size(md5sig_count *
  108. sizeof(struct tcp_diag_md5sig));
  109. }
  110. #endif
  111. return size;
  112. }
  113. static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  114. const struct inet_diag_req_v2 *r, struct nlattr *bc)
  115. {
  116. inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r, bc);
  117. }
  118. static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
  119. const struct inet_diag_req_v2 *req)
  120. {
  121. return inet_diag_dump_one_icsk(&tcp_hashinfo, in_skb, nlh, req);
  122. }
  123. #ifdef CONFIG_INET_DIAG_DESTROY
  124. static int tcp_diag_destroy(struct sk_buff *in_skb,
  125. const struct inet_diag_req_v2 *req)
  126. {
  127. struct net *net = sock_net(in_skb->sk);
  128. struct sock *sk = inet_diag_find_one_icsk(net, &tcp_hashinfo, req);
  129. int err;
  130. if (IS_ERR(sk))
  131. return PTR_ERR(sk);
  132. err = sock_diag_destroy(sk, ECONNABORTED);
  133. sock_gen_put(sk);
  134. return err;
  135. }
  136. #endif
  137. static const struct inet_diag_handler tcp_diag_handler = {
  138. .dump = tcp_diag_dump,
  139. .dump_one = tcp_diag_dump_one,
  140. .idiag_get_info = tcp_diag_get_info,
  141. .idiag_get_aux = tcp_diag_get_aux,
  142. .idiag_get_aux_size = tcp_diag_get_aux_size,
  143. .idiag_type = IPPROTO_TCP,
  144. .idiag_info_size = sizeof(struct tcp_info),
  145. #ifdef CONFIG_INET_DIAG_DESTROY
  146. .destroy = tcp_diag_destroy,
  147. #endif
  148. };
  149. static int __init tcp_diag_init(void)
  150. {
  151. return inet_diag_register(&tcp_diag_handler);
  152. }
  153. static void __exit tcp_diag_exit(void)
  154. {
  155. inet_diag_unregister(&tcp_diag_handler);
  156. }
  157. module_init(tcp_diag_init);
  158. module_exit(tcp_diag_exit);
  159. MODULE_LICENSE("GPL");
  160. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-6 /* AF_INET - IPPROTO_TCP */);