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 <uapi/linux/mptcp.h> 14 #include "protocol.h" 15 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) 20 { 21 if (!inet_diag_bc_sk(bc, sk)) 22 return 0; 23 24 return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, req, NLM_F_MULTI, 25 net_admin); 26 } 27 28 static int mptcp_diag_dump_one(struct netlink_callback *cb, 29 const struct inet_diag_req_v2 *req) 30 { 31 struct sk_buff *in_skb = cb->skb; 32 struct mptcp_sock *msk = NULL; 33 struct sk_buff *rep; 34 int err = -ENOENT; 35 struct net *net; 36 struct sock *sk; 37 38 net = sock_net(in_skb->sk); 39 msk = mptcp_token_get_sock(net, req->id.idiag_cookie[0]); 40 if (!msk) 41 goto out_nosk; 42 43 err = -ENOMEM; 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, 49 GFP_KERNEL); 50 if (!rep) 51 goto out; 52 53 err = inet_sk_diag_fill(sk, inet_csk(sk), rep, cb, req, 0, 54 netlink_net_capable(in_skb, CAP_NET_ADMIN)); 55 if (err < 0) { 56 WARN_ON(err == -EMSGSIZE); 57 kfree_skb(rep); 58 goto out; 59 } 60 err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid); 61 62 out: 63 sock_put(sk); 64 65 out_nosk: 66 return err; 67 } 68 69 struct mptcp_diag_ctx { 70 long s_slot; 71 long s_num; 72 unsigned int l_slot; 73 unsigned int l_num; 74 }; 75 76 static void mptcp_diag_dump_listeners(struct sk_buff *skb, struct netlink_callback *cb, 77 const struct inet_diag_req_v2 *r, 78 bool net_admin) 79 { 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; 85 int i; 86 87 hinfo = net->ipv4.tcp_death_row.hashinfo; 88 89 for (i = diag_ctx->l_slot; i <= hinfo->lhash2_mask; i++) { 90 struct inet_listen_hashbucket *ilb; 91 struct hlist_nulls_node *node; 92 struct sock *sk; 93 int num = 0; 94 95 ilb = &hinfo->lhash2[i]; 96 97 rcu_read_lock(); 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); 102 int ret; 103 104 if (num < diag_ctx->l_num) 105 goto next_listen; 106 107 if (!ctx || strcmp(inet_csk(sk)->icsk_ulp_ops->name, "mptcp")) 108 goto next_listen; 109 110 sk = ctx->conn; 111 if (!sk || !net_eq(sock_net(sk), net)) 112 goto next_listen; 113 114 if (r->sdiag_family != AF_UNSPEC && 115 sk->sk_family != r->sdiag_family) 116 goto next_listen; 117 118 if (r->id.idiag_sport != inet->inet_sport && 119 r->id.idiag_sport) 120 goto next_listen; 121 122 if (!refcount_inc_not_zero(&sk->sk_refcnt)) 123 goto next_listen; 124 125 ret = sk_diag_dump(sk, skb, cb, r, bc, net_admin); 126 127 sock_put(sk); 128 129 if (ret < 0) { 130 spin_unlock(&ilb->lock); 131 rcu_read_unlock(); 132 diag_ctx->l_slot = i; 133 diag_ctx->l_num = num; 134 return; 135 } 136 diag_ctx->l_num = num + 1; 137 num = 0; 138 next_listen: 139 ++num; 140 } 141 spin_unlock(&ilb->lock); 142 rcu_read_unlock(); 143 144 cond_resched(); 145 diag_ctx->l_num = 0; 146 } 147 148 diag_ctx->l_num = 0; 149 diag_ctx->l_slot = i; 150 } 151 152 static void mptcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, 153 const struct inet_diag_req_v2 *r) 154 { 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; 160 struct nlattr *bc; 161 162 BUILD_BUG_ON(sizeof(cb->ctx) < sizeof(*diag_ctx)); 163 164 cb_data = cb->data; 165 bc = cb_data->inet_diag_nla_bc; 166 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; 171 int ret = 0; 172 173 if (!(r->idiag_states & (1 << sk->sk_state))) 174 goto next; 175 if (r->sdiag_family != AF_UNSPEC && 176 sk->sk_family != r->sdiag_family) 177 goto next; 178 if (r->id.idiag_sport != inet->inet_sport && 179 r->id.idiag_sport) 180 goto next; 181 if (r->id.idiag_dport != inet->inet_dport && 182 r->id.idiag_dport) 183 goto next; 184 185 ret = sk_diag_dump(sk, skb, cb, r, bc, net_admin); 186 next: 187 sock_put(sk); 188 if (ret < 0) { 189 /* will retry on the same position */ 190 diag_ctx->s_num--; 191 break; 192 } 193 cond_resched(); 194 } 195 196 if ((r->idiag_states & TCPF_LISTEN) && r->id.idiag_dport == 0) 197 mptcp_diag_dump_listeners(skb, cb, r, net_admin); 198 } 199 200 static void mptcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r, 201 void *_info) 202 { 203 struct mptcp_sock *msk = mptcp_sk(sk); 204 struct mptcp_info *info = _info; 205 206 r->idiag_rqueue = sk_rmem_alloc_get(sk); 207 r->idiag_wqueue = sk_wmem_alloc_get(sk); 208 209 if (inet_sk_state_load(sk) == TCP_LISTEN) { 210 struct sock *lsk = READ_ONCE(msk->first); 211 212 if (lsk) { 213 /* override with settings from tcp listener, 214 * so Send-Q will show accept queue. 215 */ 216 r->idiag_rqueue = READ_ONCE(lsk->sk_ack_backlog); 217 r->idiag_wqueue = READ_ONCE(lsk->sk_max_ack_backlog); 218 } 219 } 220 221 if (!info) 222 return; 223 224 mptcp_diag_fill_info(msk, info); 225 } 226 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), 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