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>
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
))
23 return inet_sk_diag_fill(sk
, inet_csk(sk
), skb
, cb
, req
, NLM_F_MULTI
,
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
;
37 net
= sock_net(in_skb
->sk
);
38 msk
= mptcp_token_get_sock(net
, req
->id
.idiag_cookie
[0]);
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,
52 err
= inet_sk_diag_fill(sk
, inet_csk(sk
), rep
, cb
, req
, 0,
53 netlink_net_capable(in_skb
, CAP_NET_ADMIN
));
55 WARN_ON(err
== -EMSGSIZE
);
59 err
= nlmsg_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
);
68 struct mptcp_diag_ctx
{
75 static void mptcp_diag_dump_listeners(struct sk_buff
*skb
, struct netlink_callback
*cb
,
76 const struct inet_diag_req_v2
*r
,
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
;
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
;
94 ilb
= &hinfo
->lhash2
[i
];
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
);
103 if (num
< diag_ctx
->l_num
)
106 if (!ctx
|| strcmp(inet_csk(sk
)->icsk_ulp_ops
->name
, "mptcp"))
110 if (!sk
|| !net_eq(sock_net(sk
), net
))
113 if (r
->sdiag_family
!= AF_UNSPEC
&&
114 sk
->sk_family
!= r
->sdiag_family
)
117 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
121 if (!refcount_inc_not_zero(&sk
->sk_refcnt
))
124 ret
= sk_diag_dump(sk
, skb
, cb
, r
, bc
, net_admin
);
129 spin_unlock(&ilb
->lock
);
131 diag_ctx
->l_slot
= i
;
132 diag_ctx
->l_num
= num
;
135 diag_ctx
->l_num
= num
+ 1;
140 spin_unlock(&ilb
->lock
);
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
;
161 BUILD_BUG_ON(sizeof(cb
->ctx
) < sizeof(*diag_ctx
));
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
;
172 if (!(r
->idiag_states
& (1 << sk
->sk_state
)))
174 if (r
->sdiag_family
!= AF_UNSPEC
&&
175 sk
->sk_family
!= r
->sdiag_family
)
177 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
180 if (r
->id
.idiag_dport
!= inet
->inet_dport
&&
184 ret
= sk_diag_dump(sk
, skb
, cb
, r
, bc
, net_admin
);
188 /* will retry on the same position */
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
,
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
);
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
);
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 */);