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
15static 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 bool net_admin)
19{
20 if (!inet_diag_bc_sk(cb_data: cb->data, 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
27static 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(sk: in_skb->sk);
38 msk = mptcp_token_get_sock(net, token: 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(payload: nla_total_size(payload: sizeof(struct inet_diag_msg)) +
45 inet_diag_msg_attrs_size() +
46 nla_total_size(payload: sizeof(struct mptcp_info)) +
47 nla_total_size(payload: 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), skb: rep, cb, req, nlmsg_flags: 0,
53 net_admin: netlink_net_capable(skb: in_skb, CAP_NET_ADMIN));
54 if (err < 0) {
55 WARN_ON(err == -EMSGSIZE);
56 kfree_skb(skb: rep);
57 goto out;
58 }
59 err = nlmsg_unicast(sk: net->diag_nlsk, skb: rep, NETLINK_CB(in_skb).portid);
60
61out:
62 sock_put(sk);
63
64out_nosk:
65 return err;
66}
67
68struct mptcp_diag_ctx {
69 long s_slot;
70 long s_num;
71 unsigned int l_slot;
72 unsigned int l_num;
73};
74
75static 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 mptcp_diag_ctx *diag_ctx = (void *)cb->ctx;
80 struct net *net = sock_net(sk: skb->sk);
81 struct inet_hashinfo *hinfo;
82 int i;
83
84 hinfo = net->ipv4.tcp_death_row.hashinfo;
85
86 for (i = diag_ctx->l_slot; i <= hinfo->lhash2_mask; i++) {
87 struct inet_listen_hashbucket *ilb;
88 struct hlist_nulls_node *node;
89 struct sock *sk;
90 int num = 0;
91
92 ilb = &hinfo->lhash2[i];
93
94 rcu_read_lock();
95 spin_lock(lock: &ilb->lock);
96 sk_nulls_for_each(sk, node, &ilb->nulls_head) {
97 const struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(sk);
98 struct inet_sock *inet = inet_sk(sk);
99 int ret;
100
101 if (num < diag_ctx->l_num)
102 goto next_listen;
103
104 if (!ctx || strcmp(inet_csk(sk)->icsk_ulp_ops->name, "mptcp"))
105 goto next_listen;
106
107 sk = ctx->conn;
108 if (!sk || !net_eq(net1: sock_net(sk), net2: net))
109 goto next_listen;
110
111 if (r->sdiag_family != AF_UNSPEC &&
112 sk->sk_family != r->sdiag_family)
113 goto next_listen;
114
115 if (r->id.idiag_sport != inet->inet_sport &&
116 r->id.idiag_sport)
117 goto next_listen;
118
119 if (!refcount_inc_not_zero(r: &sk->sk_refcnt))
120 goto next_listen;
121
122 ret = sk_diag_dump(sk, skb, cb, req: r, net_admin);
123
124 sock_put(sk);
125
126 if (ret < 0) {
127 spin_unlock(lock: &ilb->lock);
128 rcu_read_unlock();
129 diag_ctx->l_slot = i;
130 diag_ctx->l_num = num;
131 return;
132 }
133 diag_ctx->l_num = num + 1;
134 num = 0;
135next_listen:
136 ++num;
137 }
138 spin_unlock(lock: &ilb->lock);
139 rcu_read_unlock();
140
141 cond_resched();
142 diag_ctx->l_num = 0;
143 }
144
145 diag_ctx->l_num = 0;
146 diag_ctx->l_slot = i;
147}
148
149static void mptcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
150 const struct inet_diag_req_v2 *r)
151{
152 bool net_admin = netlink_net_capable(skb: cb->skb, CAP_NET_ADMIN);
153 struct mptcp_diag_ctx *diag_ctx = (void *)cb->ctx;
154 struct net *net = sock_net(sk: skb->sk);
155 struct mptcp_sock *msk;
156
157 BUILD_BUG_ON(sizeof(cb->ctx) < sizeof(*diag_ctx));
158
159 while ((msk = mptcp_token_iter_next(net, s_slot: &diag_ctx->s_slot,
160 s_num: &diag_ctx->s_num)) != NULL) {
161 struct inet_sock *inet = (struct inet_sock *)msk;
162 struct sock *sk = (struct sock *)msk;
163 int ret = 0;
164
165 if (!(r->idiag_states & (1 << sk->sk_state)))
166 goto next;
167 if (r->sdiag_family != AF_UNSPEC &&
168 sk->sk_family != r->sdiag_family)
169 goto next;
170 if (r->id.idiag_sport != inet->inet_sport &&
171 r->id.idiag_sport)
172 goto next;
173 if (r->id.idiag_dport != inet->inet_dport &&
174 r->id.idiag_dport)
175 goto next;
176
177 ret = sk_diag_dump(sk, skb, cb, req: r, net_admin);
178next:
179 sock_put(sk);
180 if (ret < 0) {
181 /* will retry on the same position */
182 diag_ctx->s_num--;
183 break;
184 }
185 cond_resched();
186 }
187
188 if ((r->idiag_states & TCPF_LISTEN) && r->id.idiag_dport == 0)
189 mptcp_diag_dump_listeners(skb, cb, r, net_admin);
190}
191
192static void mptcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
193 void *_info)
194{
195 struct mptcp_sock *msk = mptcp_sk(sk);
196 struct mptcp_info *info = _info;
197
198 r->idiag_rqueue = sk_rmem_alloc_get(sk) +
199 READ_ONCE(mptcp_sk(sk)->backlog_len);
200 r->idiag_wqueue = sk_wmem_alloc_get(sk);
201
202 if (inet_sk_state_load(sk) == TCP_LISTEN) {
203 struct sock *lsk = READ_ONCE(msk->first);
204
205 if (lsk) {
206 /* override with settings from tcp listener,
207 * so Send-Q will show accept queue.
208 */
209 r->idiag_rqueue = READ_ONCE(lsk->sk_ack_backlog);
210 r->idiag_wqueue = READ_ONCE(lsk->sk_max_ack_backlog);
211 }
212 }
213
214 if (!info)
215 return;
216
217 mptcp_diag_fill_info(msk, info);
218}
219
220static const struct inet_diag_handler mptcp_diag_handler = {
221 .owner = THIS_MODULE,
222 .dump = mptcp_diag_dump,
223 .dump_one = mptcp_diag_dump_one,
224 .idiag_get_info = mptcp_diag_get_info,
225 .idiag_type = IPPROTO_MPTCP,
226 .idiag_info_size = sizeof(struct mptcp_info),
227};
228
229static int __init mptcp_diag_init(void)
230{
231 return inet_diag_register(handler: &mptcp_diag_handler);
232}
233
234static void __exit mptcp_diag_exit(void)
235{
236 inet_diag_unregister(handler: &mptcp_diag_handler);
237}
238
239module_init(mptcp_diag_init);
240module_exit(mptcp_diag_exit);
241MODULE_LICENSE("GPL");
242MODULE_DESCRIPTION("MPTCP socket monitoring via SOCK_DIAG");
243MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-262 /* AF_INET - IPPROTO_MPTCP */);
244

source code of linux/net/mptcp/mptcp_diag.c