mptcp_diag.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* MPTCP socket monitoring support
  3. *
  4. * Copyright (c) 2020 Red Hat
  5. *
  6. * Author: Paolo Abeni <pabeni@redhat.com>
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/net.h>
  10. #include <linux/inet_diag.h>
  11. #include <net/netlink.h>
  12. #include "protocol.h"
  13. static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
  14. struct netlink_callback *cb,
  15. const struct inet_diag_req_v2 *req,
  16. struct nlattr *bc, bool net_admin)
  17. {
  18. if (!inet_diag_bc_sk(bc, sk))
  19. return 0;
  20. return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, req, NLM_F_MULTI,
  21. net_admin);
  22. }
  23. static int mptcp_diag_dump_one(struct netlink_callback *cb,
  24. const struct inet_diag_req_v2 *req)
  25. {
  26. struct sk_buff *in_skb = cb->skb;
  27. struct mptcp_sock *msk = NULL;
  28. struct sk_buff *rep;
  29. int err = -ENOENT;
  30. struct net *net;
  31. struct sock *sk;
  32. net = sock_net(in_skb->sk);
  33. msk = mptcp_token_get_sock(net, req->id.idiag_cookie[0]);
  34. if (!msk)
  35. goto out_nosk;
  36. err = -ENOMEM;
  37. sk = (struct sock *)msk;
  38. rep = nlmsg_new(nla_total_size(sizeof(struct inet_diag_msg)) +
  39. inet_diag_msg_attrs_size() +
  40. nla_total_size(sizeof(struct mptcp_info)) +
  41. nla_total_size(sizeof(struct inet_diag_meminfo)) + 64,
  42. GFP_KERNEL);
  43. if (!rep)
  44. goto out;
  45. err = inet_sk_diag_fill(sk, inet_csk(sk), rep, cb, req, 0,
  46. netlink_net_capable(in_skb, CAP_NET_ADMIN));
  47. if (err < 0) {
  48. WARN_ON(err == -EMSGSIZE);
  49. kfree_skb(rep);
  50. goto out;
  51. }
  52. err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
  53. out:
  54. sock_put(sk);
  55. out_nosk:
  56. return err;
  57. }
  58. struct mptcp_diag_ctx {
  59. long s_slot;
  60. long s_num;
  61. unsigned int l_slot;
  62. unsigned int l_num;
  63. };
  64. static void mptcp_diag_dump_listeners(struct sk_buff *skb, struct netlink_callback *cb,
  65. const struct inet_diag_req_v2 *r,
  66. bool net_admin)
  67. {
  68. struct inet_diag_dump_data *cb_data = cb->data;
  69. struct mptcp_diag_ctx *diag_ctx = (void *)cb->ctx;
  70. struct nlattr *bc = cb_data->inet_diag_nla_bc;
  71. struct net *net = sock_net(skb->sk);
  72. struct inet_hashinfo *hinfo;
  73. int i;
  74. hinfo = net->ipv4.tcp_death_row.hashinfo;
  75. for (i = diag_ctx->l_slot; i <= hinfo->lhash2_mask; i++) {
  76. struct inet_listen_hashbucket *ilb;
  77. struct hlist_nulls_node *node;
  78. struct sock *sk;
  79. int num = 0;
  80. ilb = &hinfo->lhash2[i];
  81. rcu_read_lock();
  82. spin_lock(&ilb->lock);
  83. sk_nulls_for_each(sk, node, &ilb->nulls_head) {
  84. const struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(sk);
  85. struct inet_sock *inet = inet_sk(sk);
  86. int ret;
  87. if (num < diag_ctx->l_num)
  88. goto next_listen;
  89. if (!ctx || strcmp(inet_csk(sk)->icsk_ulp_ops->name, "mptcp"))
  90. goto next_listen;
  91. sk = ctx->conn;
  92. if (!sk || !net_eq(sock_net(sk), net))
  93. goto next_listen;
  94. if (r->sdiag_family != AF_UNSPEC &&
  95. sk->sk_family != r->sdiag_family)
  96. goto next_listen;
  97. if (r->id.idiag_sport != inet->inet_sport &&
  98. r->id.idiag_sport)
  99. goto next_listen;
  100. if (!refcount_inc_not_zero(&sk->sk_refcnt))
  101. goto next_listen;
  102. ret = sk_diag_dump(sk, skb, cb, r, bc, net_admin);
  103. sock_put(sk);
  104. if (ret < 0) {
  105. spin_unlock(&ilb->lock);
  106. rcu_read_unlock();
  107. diag_ctx->l_slot = i;
  108. diag_ctx->l_num = num;
  109. return;
  110. }
  111. diag_ctx->l_num = num + 1;
  112. num = 0;
  113. next_listen:
  114. ++num;
  115. }
  116. spin_unlock(&ilb->lock);
  117. rcu_read_unlock();
  118. cond_resched();
  119. diag_ctx->l_num = 0;
  120. }
  121. diag_ctx->l_num = 0;
  122. diag_ctx->l_slot = i;
  123. }
  124. static void mptcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  125. const struct inet_diag_req_v2 *r)
  126. {
  127. bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
  128. struct mptcp_diag_ctx *diag_ctx = (void *)cb->ctx;
  129. struct net *net = sock_net(skb->sk);
  130. struct inet_diag_dump_data *cb_data;
  131. struct mptcp_sock *msk;
  132. struct nlattr *bc;
  133. BUILD_BUG_ON(sizeof(cb->ctx) < sizeof(*diag_ctx));
  134. cb_data = cb->data;
  135. bc = cb_data->inet_diag_nla_bc;
  136. while ((msk = mptcp_token_iter_next(net, &diag_ctx->s_slot,
  137. &diag_ctx->s_num)) != NULL) {
  138. struct inet_sock *inet = (struct inet_sock *)msk;
  139. struct sock *sk = (struct sock *)msk;
  140. int ret = 0;
  141. if (!(r->idiag_states & (1 << sk->sk_state)))
  142. goto next;
  143. if (r->sdiag_family != AF_UNSPEC &&
  144. sk->sk_family != r->sdiag_family)
  145. goto next;
  146. if (r->id.idiag_sport != inet->inet_sport &&
  147. r->id.idiag_sport)
  148. goto next;
  149. if (r->id.idiag_dport != inet->inet_dport &&
  150. r->id.idiag_dport)
  151. goto next;
  152. ret = sk_diag_dump(sk, skb, cb, r, bc, net_admin);
  153. next:
  154. sock_put(sk);
  155. if (ret < 0) {
  156. /* will retry on the same position */
  157. diag_ctx->s_num--;
  158. break;
  159. }
  160. cond_resched();
  161. }
  162. if ((r->idiag_states & TCPF_LISTEN) && r->id.idiag_dport == 0)
  163. mptcp_diag_dump_listeners(skb, cb, r, net_admin);
  164. }
  165. static void mptcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
  166. void *_info)
  167. {
  168. struct mptcp_sock *msk = mptcp_sk(sk);
  169. struct mptcp_info *info = _info;
  170. r->idiag_rqueue = sk_rmem_alloc_get(sk);
  171. r->idiag_wqueue = sk_wmem_alloc_get(sk);
  172. if (inet_sk_state_load(sk) == TCP_LISTEN) {
  173. struct sock *lsk = READ_ONCE(msk->first);
  174. if (lsk) {
  175. /* override with settings from tcp listener,
  176. * so Send-Q will show accept queue.
  177. */
  178. r->idiag_rqueue = READ_ONCE(lsk->sk_ack_backlog);
  179. r->idiag_wqueue = READ_ONCE(lsk->sk_max_ack_backlog);
  180. }
  181. }
  182. if (!info)
  183. return;
  184. mptcp_diag_fill_info(msk, info);
  185. }
  186. static const struct inet_diag_handler mptcp_diag_handler = {
  187. .owner = THIS_MODULE,
  188. .dump = mptcp_diag_dump,
  189. .dump_one = mptcp_diag_dump_one,
  190. .idiag_get_info = mptcp_diag_get_info,
  191. .idiag_type = IPPROTO_MPTCP,
  192. .idiag_info_size = sizeof(struct mptcp_info),
  193. };
  194. static int __init mptcp_diag_init(void)
  195. {
  196. return inet_diag_register(&mptcp_diag_handler);
  197. }
  198. static void __exit mptcp_diag_exit(void)
  199. {
  200. inet_diag_unregister(&mptcp_diag_handler);
  201. }
  202. module_init(mptcp_diag_init);
  203. module_exit(mptcp_diag_exit);
  204. MODULE_LICENSE("GPL");
  205. MODULE_DESCRIPTION("MPTCP socket monitoring via SOCK_DIAG");
  206. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-262 /* AF_INET - IPPROTO_MPTCP */);