tcp_diag.c 4.7 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) -
  28. READ_ONCE(tp->copied_seq), 0);
  29. r->idiag_wqueue = READ_ONCE(tp->write_seq) - tp->snd_una;
  30. }
  31. if (info)
  32. tcp_get_info(sk, info);
  33. }
  34. #ifdef CONFIG_TCP_MD5SIG
  35. static void tcp_diag_md5sig_fill(struct tcp_diag_md5sig *info,
  36. const struct tcp_md5sig_key *key)
  37. {
  38. info->tcpm_family = key->family;
  39. info->tcpm_prefixlen = key->prefixlen;
  40. info->tcpm_keylen = key->keylen;
  41. memcpy(info->tcpm_key, key->key, key->keylen);
  42. if (key->family == AF_INET)
  43. info->tcpm_addr[0] = key->addr.a4.s_addr;
  44. #if IS_ENABLED(CONFIG_IPV6)
  45. else if (key->family == AF_INET6)
  46. memcpy(&info->tcpm_addr, &key->addr.a6,
  47. sizeof(info->tcpm_addr));
  48. #endif
  49. }
  50. static int tcp_diag_put_md5sig(struct sk_buff *skb,
  51. const struct tcp_md5sig_info *md5sig)
  52. {
  53. const struct tcp_md5sig_key *key;
  54. struct tcp_diag_md5sig *info;
  55. struct nlattr *attr;
  56. int md5sig_count = 0;
  57. hlist_for_each_entry_rcu(key, &md5sig->head, node)
  58. md5sig_count++;
  59. if (md5sig_count == 0)
  60. return 0;
  61. attr = nla_reserve(skb, INET_DIAG_MD5SIG,
  62. md5sig_count * sizeof(struct tcp_diag_md5sig));
  63. if (!attr)
  64. return -EMSGSIZE;
  65. info = nla_data(attr);
  66. memset(info, 0, md5sig_count * sizeof(struct tcp_diag_md5sig));
  67. hlist_for_each_entry_rcu(key, &md5sig->head, node) {
  68. tcp_diag_md5sig_fill(info++, key);
  69. if (--md5sig_count == 0)
  70. break;
  71. }
  72. return 0;
  73. }
  74. #endif
  75. static int tcp_diag_get_aux(struct sock *sk, bool net_admin,
  76. struct sk_buff *skb)
  77. {
  78. #ifdef CONFIG_TCP_MD5SIG
  79. if (net_admin) {
  80. struct tcp_md5sig_info *md5sig;
  81. int err = 0;
  82. rcu_read_lock();
  83. md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
  84. if (md5sig)
  85. err = tcp_diag_put_md5sig(skb, md5sig);
  86. rcu_read_unlock();
  87. if (err < 0)
  88. return err;
  89. }
  90. #endif
  91. return 0;
  92. }
  93. static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin)
  94. {
  95. size_t size = 0;
  96. #ifdef CONFIG_TCP_MD5SIG
  97. if (net_admin && sk_fullsock(sk)) {
  98. const struct tcp_md5sig_info *md5sig;
  99. const struct tcp_md5sig_key *key;
  100. size_t md5sig_count = 0;
  101. rcu_read_lock();
  102. md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
  103. if (md5sig) {
  104. hlist_for_each_entry_rcu(key, &md5sig->head, node)
  105. md5sig_count++;
  106. }
  107. rcu_read_unlock();
  108. size += nla_total_size(md5sig_count *
  109. sizeof(struct tcp_diag_md5sig));
  110. }
  111. #endif
  112. return size;
  113. }
  114. static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  115. const struct inet_diag_req_v2 *r, struct nlattr *bc)
  116. {
  117. inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r, bc);
  118. }
  119. static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
  120. const struct inet_diag_req_v2 *req)
  121. {
  122. return inet_diag_dump_one_icsk(&tcp_hashinfo, in_skb, nlh, req);
  123. }
  124. #ifdef CONFIG_INET_DIAG_DESTROY
  125. static int tcp_diag_destroy(struct sk_buff *in_skb,
  126. const struct inet_diag_req_v2 *req)
  127. {
  128. struct net *net = sock_net(in_skb->sk);
  129. struct sock *sk = inet_diag_find_one_icsk(net, &tcp_hashinfo, req);
  130. int err;
  131. if (IS_ERR(sk))
  132. return PTR_ERR(sk);
  133. err = sock_diag_destroy(sk, ECONNABORTED);
  134. sock_gen_put(sk);
  135. return err;
  136. }
  137. #endif
  138. static const struct inet_diag_handler tcp_diag_handler = {
  139. .dump = tcp_diag_dump,
  140. .dump_one = tcp_diag_dump_one,
  141. .idiag_get_info = tcp_diag_get_info,
  142. .idiag_get_aux = tcp_diag_get_aux,
  143. .idiag_get_aux_size = tcp_diag_get_aux_size,
  144. .idiag_type = IPPROTO_TCP,
  145. .idiag_info_size = sizeof(struct tcp_info),
  146. #ifdef CONFIG_INET_DIAG_DESTROY
  147. .destroy = tcp_diag_destroy,
  148. #endif
  149. };
  150. static int __init tcp_diag_init(void)
  151. {
  152. return inet_diag_register(&tcp_diag_handler);
  153. }
  154. static void __exit tcp_diag_exit(void)
  155. {
  156. inet_diag_unregister(&tcp_diag_handler);
  157. }
  158. module_init(tcp_diag_init);
  159. module_exit(tcp_diag_exit);
  160. MODULE_LICENSE("GPL");
  161. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-6 /* AF_INET - IPPROTO_TCP */);