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