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