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>
9 #include <linux/kernel.h>
10 #include <linux/net.h>
11 #include <linux/inet_diag.h>
12 #include <net/netlink.h>
13 #include <uapi/linux/mptcp.h>
16 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
17 struct netlink_callback *cb,
18 const struct inet_diag_req_v2 *req,
19 struct nlattr *bc, bool net_admin)
21 if (!inet_diag_bc_sk(bc, sk))
24 return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, req, NLM_F_MULTI,
28 static int mptcp_diag_dump_one(struct netlink_callback *cb,
29 const struct inet_diag_req_v2 *req)
31 struct sk_buff *in_skb = cb->skb;
32 struct mptcp_sock *msk = NULL;
38 net = sock_net(in_skb->sk);
39 msk = mptcp_token_get_sock(net, req->id.idiag_cookie[0]);
44 sk = (struct sock *)msk;
45 rep = nlmsg_new(nla_total_size(sizeof(struct inet_diag_msg)) +
46 inet_diag_msg_attrs_size() +
47 nla_total_size(sizeof(struct mptcp_info)) +
48 nla_total_size(sizeof(struct inet_diag_meminfo)) + 64,
53 err = inet_sk_diag_fill(sk, inet_csk(sk), rep, cb, req, 0,
54 netlink_net_capable(in_skb, CAP_NET_ADMIN));
56 WARN_ON(err == -EMSGSIZE);
60 err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
69 struct mptcp_diag_ctx {
76 static void mptcp_diag_dump_listeners(struct sk_buff *skb, struct netlink_callback *cb,
77 const struct inet_diag_req_v2 *r,
80 struct inet_diag_dump_data *cb_data = cb->data;
81 struct mptcp_diag_ctx *diag_ctx = (void *)cb->ctx;
82 struct nlattr *bc = cb_data->inet_diag_nla_bc;
83 struct net *net = sock_net(skb->sk);
84 struct inet_hashinfo *hinfo;
87 hinfo = net->ipv4.tcp_death_row.hashinfo;
89 for (i = diag_ctx->l_slot; i <= hinfo->lhash2_mask; i++) {
90 struct inet_listen_hashbucket *ilb;
91 struct hlist_nulls_node *node;
95 ilb = &hinfo->lhash2[i];
98 spin_lock(&ilb->lock);
99 sk_nulls_for_each(sk, node, &ilb->nulls_head) {
100 const struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(sk);
101 struct inet_sock *inet = inet_sk(sk);
104 if (num < diag_ctx->l_num)
107 if (!ctx || strcmp(inet_csk(sk)->icsk_ulp_ops->name, "mptcp"))
111 if (!sk || !net_eq(sock_net(sk), net))
114 if (r->sdiag_family != AF_UNSPEC &&
115 sk->sk_family != r->sdiag_family)
118 if (r->id.idiag_sport != inet->inet_sport &&
122 if (!refcount_inc_not_zero(&sk->sk_refcnt))
125 ret = sk_diag_dump(sk, skb, cb, r, bc, net_admin);
130 spin_unlock(&ilb->lock);
132 diag_ctx->l_slot = i;
133 diag_ctx->l_num = num;
136 diag_ctx->l_num = num + 1;
141 spin_unlock(&ilb->lock);
149 diag_ctx->l_slot = i;
152 static void mptcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
153 const struct inet_diag_req_v2 *r)
155 bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
156 struct mptcp_diag_ctx *diag_ctx = (void *)cb->ctx;
157 struct net *net = sock_net(skb->sk);
158 struct inet_diag_dump_data *cb_data;
159 struct mptcp_sock *msk;
162 BUILD_BUG_ON(sizeof(cb->ctx) < sizeof(*diag_ctx));
165 bc = cb_data->inet_diag_nla_bc;
167 while ((msk = mptcp_token_iter_next(net, &diag_ctx->s_slot,
168 &diag_ctx->s_num)) != NULL) {
169 struct inet_sock *inet = (struct inet_sock *)msk;
170 struct sock *sk = (struct sock *)msk;
173 if (!(r->idiag_states & (1 << sk->sk_state)))
175 if (r->sdiag_family != AF_UNSPEC &&
176 sk->sk_family != r->sdiag_family)
178 if (r->id.idiag_sport != inet->inet_sport &&
181 if (r->id.idiag_dport != inet->inet_dport &&
185 ret = sk_diag_dump(sk, skb, cb, r, bc, net_admin);
189 /* will retry on the same position */
196 if ((r->idiag_states & TCPF_LISTEN) && r->id.idiag_dport == 0)
197 mptcp_diag_dump_listeners(skb, cb, r, net_admin);
200 static void mptcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
203 struct mptcp_sock *msk = mptcp_sk(sk);
204 struct mptcp_info *info = _info;
206 r->idiag_rqueue = sk_rmem_alloc_get(sk);
207 r->idiag_wqueue = sk_wmem_alloc_get(sk);
209 if (inet_sk_state_load(sk) == TCP_LISTEN) {
210 struct sock *lsk = READ_ONCE(msk->first);
213 /* override with settings from tcp listener,
214 * so Send-Q will show accept queue.
216 r->idiag_rqueue = READ_ONCE(lsk->sk_ack_backlog);
217 r->idiag_wqueue = READ_ONCE(lsk->sk_max_ack_backlog);
224 mptcp_diag_fill_info(msk, info);
227 static const struct inet_diag_handler mptcp_diag_handler = {
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_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-262 /* AF_INET - IPPROTO_MPTCP */);