xref: /linux/net/mptcp/mptcp_diag.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
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 
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"
14 
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)
19 {
20 	if (!inet_diag_bc_sk(bc, sk))
21 		return 0;
22 
23 	return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, req, NLM_F_MULTI,
24 				 net_admin);
25 }
26 
27 static int mptcp_diag_dump_one(struct netlink_callback *cb,
28 			       const struct inet_diag_req_v2 *req)
29 {
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;
36 
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;
41 
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;
51 
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;
58 	}
59 	err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
60 
61 out:
62 	sock_put(sk);
63 
64 out_nosk:
65 	return err;
66 }
67 
68 struct mptcp_diag_ctx {
69 	long s_slot;
70 	long s_num;
71 	unsigned int l_slot;
72 	unsigned int l_num;
73 };
74 
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)
78 {
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;
85 
86 	hinfo = net->ipv4.tcp_death_row.hashinfo;
87 
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;
93 
94 		ilb = &hinfo->lhash2[i];
95 
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;
102 
103 			if (num < diag_ctx->l_num)
104 				goto next_listen;
105 
106 			if (!ctx || strcmp(inet_csk(sk)->icsk_ulp_ops->name, "mptcp"))
107 				goto next_listen;
108 
109 			sk = ctx->conn;
110 			if (!sk || !net_eq(sock_net(sk), net))
111 				goto next_listen;
112 
113 			if (r->sdiag_family != AF_UNSPEC &&
114 			    sk->sk_family != r->sdiag_family)
115 				goto next_listen;
116 
117 			if (r->id.idiag_sport != inet->inet_sport &&
118 			    r->id.idiag_sport)
119 				goto next_listen;
120 
121 			if (!refcount_inc_not_zero(&sk->sk_refcnt))
122 				goto next_listen;
123 
124 			ret = sk_diag_dump(sk, skb, cb, r, bc, net_admin);
125 
126 			sock_put(sk);
127 
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;
134 			}
135 			diag_ctx->l_num = num + 1;
136 			num = 0;
137 next_listen:
138 			++num;
139 		}
140 		spin_unlock(&ilb->lock);
141 		rcu_read_unlock();
142 
143 		cond_resched();
144 		diag_ctx->l_num = 0;
145 	}
146 
147 	diag_ctx->l_num = 0;
148 	diag_ctx->l_slot = i;
149 }
150 
151 static void mptcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
152 			    const struct inet_diag_req_v2 *r)
153 {
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;
160 
161 	BUILD_BUG_ON(sizeof(cb->ctx) < sizeof(*diag_ctx));
162 
163 	cb_data = cb->data;
164 	bc = cb_data->inet_diag_nla_bc;
165 
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;
171 
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;
183 
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;
191 		}
192 		cond_resched();
193 	}
194 
195 	if ((r->idiag_states & TCPF_LISTEN) && r->id.idiag_dport == 0)
196 		mptcp_diag_dump_listeners(skb, cb, r, net_admin);
197 }
198 
199 static void mptcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
200 				void *_info)
201 {
202 	struct mptcp_sock *msk = mptcp_sk(sk);
203 	struct mptcp_info *info = _info;
204 
205 	r->idiag_rqueue = sk_rmem_alloc_get(sk);
206 	r->idiag_wqueue = sk_wmem_alloc_get(sk);
207 
208 	if (inet_sk_state_load(sk) == TCP_LISTEN) {
209 		struct sock *lsk = READ_ONCE(msk->first);
210 
211 		if (lsk) {
212 			/* override with settings from tcp listener,
213 			 * so Send-Q will show accept queue.
214 			 */
215 			r->idiag_rqueue = READ_ONCE(lsk->sk_ack_backlog);
216 			r->idiag_wqueue = READ_ONCE(lsk->sk_max_ack_backlog);
217 		}
218 	}
219 
220 	if (!info)
221 		return;
222 
223 	mptcp_diag_fill_info(msk, info);
224 }
225 
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),
233 };
234 
235 static int __init mptcp_diag_init(void)
236 {
237 	return inet_diag_register(&mptcp_diag_handler);
238 }
239 
240 static void __exit mptcp_diag_exit(void)
241 {
242 	inet_diag_unregister(&mptcp_diag_handler);
243 }
244 
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 */);
250