Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* MPTCP socket monitoring support
0003  *
0004  * Copyright (c) 2020 Red Hat
0005  *
0006  * Author: Paolo Abeni <pabeni@redhat.com>
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/net.h>
0011 #include <linux/inet_diag.h>
0012 #include <net/netlink.h>
0013 #include <uapi/linux/mptcp.h>
0014 #include "protocol.h"
0015 
0016 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
0017             struct netlink_callback *cb,
0018             const struct inet_diag_req_v2 *req,
0019             struct nlattr *bc, bool net_admin)
0020 {
0021     if (!inet_diag_bc_sk(bc, sk))
0022         return 0;
0023 
0024     return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, req, NLM_F_MULTI,
0025                  net_admin);
0026 }
0027 
0028 static int mptcp_diag_dump_one(struct netlink_callback *cb,
0029                    const struct inet_diag_req_v2 *req)
0030 {
0031     struct sk_buff *in_skb = cb->skb;
0032     struct mptcp_sock *msk = NULL;
0033     struct sk_buff *rep;
0034     int err = -ENOENT;
0035     struct net *net;
0036     struct sock *sk;
0037 
0038     net = sock_net(in_skb->sk);
0039     msk = mptcp_token_get_sock(net, req->id.idiag_cookie[0]);
0040     if (!msk)
0041         goto out_nosk;
0042 
0043     err = -ENOMEM;
0044     sk = (struct sock *)msk;
0045     rep = nlmsg_new(nla_total_size(sizeof(struct inet_diag_msg)) +
0046             inet_diag_msg_attrs_size() +
0047             nla_total_size(sizeof(struct mptcp_info)) +
0048             nla_total_size(sizeof(struct inet_diag_meminfo)) + 64,
0049             GFP_KERNEL);
0050     if (!rep)
0051         goto out;
0052 
0053     err = inet_sk_diag_fill(sk, inet_csk(sk), rep, cb, req, 0,
0054                 netlink_net_capable(in_skb, CAP_NET_ADMIN));
0055     if (err < 0) {
0056         WARN_ON(err == -EMSGSIZE);
0057         kfree_skb(rep);
0058         goto out;
0059     }
0060     err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
0061 
0062 out:
0063     sock_put(sk);
0064 
0065 out_nosk:
0066     return err;
0067 }
0068 
0069 struct mptcp_diag_ctx {
0070     long s_slot;
0071     long s_num;
0072     unsigned int l_slot;
0073     unsigned int l_num;
0074 };
0075 
0076 static void mptcp_diag_dump_listeners(struct sk_buff *skb, struct netlink_callback *cb,
0077                       const struct inet_diag_req_v2 *r,
0078                       bool net_admin)
0079 {
0080     struct inet_diag_dump_data *cb_data = cb->data;
0081     struct mptcp_diag_ctx *diag_ctx = (void *)cb->ctx;
0082     struct nlattr *bc = cb_data->inet_diag_nla_bc;
0083     struct net *net = sock_net(skb->sk);
0084     int i;
0085 
0086     for (i = diag_ctx->l_slot; i <= tcp_hashinfo.lhash2_mask; i++) {
0087         struct inet_listen_hashbucket *ilb;
0088         struct hlist_nulls_node *node;
0089         struct sock *sk;
0090         int num = 0;
0091 
0092         ilb = &tcp_hashinfo.lhash2[i];
0093 
0094         rcu_read_lock();
0095         spin_lock(&ilb->lock);
0096         sk_nulls_for_each(sk, node, &ilb->nulls_head) {
0097             const struct mptcp_subflow_context *ctx = mptcp_subflow_ctx(sk);
0098             struct inet_sock *inet = inet_sk(sk);
0099             int ret;
0100 
0101             if (num < diag_ctx->l_num)
0102                 goto next_listen;
0103 
0104             if (!ctx || strcmp(inet_csk(sk)->icsk_ulp_ops->name, "mptcp"))
0105                 goto next_listen;
0106 
0107             sk = ctx->conn;
0108             if (!sk || !net_eq(sock_net(sk), net))
0109                 goto next_listen;
0110 
0111             if (r->sdiag_family != AF_UNSPEC &&
0112                 sk->sk_family != r->sdiag_family)
0113                 goto next_listen;
0114 
0115             if (r->id.idiag_sport != inet->inet_sport &&
0116                 r->id.idiag_sport)
0117                 goto next_listen;
0118 
0119             if (!refcount_inc_not_zero(&sk->sk_refcnt))
0120                 goto next_listen;
0121 
0122             ret = sk_diag_dump(sk, skb, cb, r, bc, net_admin);
0123 
0124             sock_put(sk);
0125 
0126             if (ret < 0) {
0127                 spin_unlock(&ilb->lock);
0128                 rcu_read_unlock();
0129                 diag_ctx->l_slot = i;
0130                 diag_ctx->l_num = num;
0131                 return;
0132             }
0133             diag_ctx->l_num = num + 1;
0134             num = 0;
0135 next_listen:
0136             ++num;
0137         }
0138         spin_unlock(&ilb->lock);
0139         rcu_read_unlock();
0140 
0141         cond_resched();
0142         diag_ctx->l_num = 0;
0143     }
0144 
0145     diag_ctx->l_num = 0;
0146     diag_ctx->l_slot = i;
0147 }
0148 
0149 static void mptcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
0150                 const struct inet_diag_req_v2 *r)
0151 {
0152     bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
0153     struct mptcp_diag_ctx *diag_ctx = (void *)cb->ctx;
0154     struct net *net = sock_net(skb->sk);
0155     struct inet_diag_dump_data *cb_data;
0156     struct mptcp_sock *msk;
0157     struct nlattr *bc;
0158 
0159     BUILD_BUG_ON(sizeof(cb->ctx) < sizeof(*diag_ctx));
0160 
0161     cb_data = cb->data;
0162     bc = cb_data->inet_diag_nla_bc;
0163 
0164     while ((msk = mptcp_token_iter_next(net, &diag_ctx->s_slot,
0165                         &diag_ctx->s_num)) != NULL) {
0166         struct inet_sock *inet = (struct inet_sock *)msk;
0167         struct sock *sk = (struct sock *)msk;
0168         int ret = 0;
0169 
0170         if (!(r->idiag_states & (1 << sk->sk_state)))
0171             goto next;
0172         if (r->sdiag_family != AF_UNSPEC &&
0173             sk->sk_family != r->sdiag_family)
0174             goto next;
0175         if (r->id.idiag_sport != inet->inet_sport &&
0176             r->id.idiag_sport)
0177             goto next;
0178         if (r->id.idiag_dport != inet->inet_dport &&
0179             r->id.idiag_dport)
0180             goto next;
0181 
0182         ret = sk_diag_dump(sk, skb, cb, r, bc, net_admin);
0183 next:
0184         sock_put(sk);
0185         if (ret < 0) {
0186             /* will retry on the same position */
0187             diag_ctx->s_num--;
0188             break;
0189         }
0190         cond_resched();
0191     }
0192 
0193     if ((r->idiag_states & TCPF_LISTEN) && r->id.idiag_dport == 0)
0194         mptcp_diag_dump_listeners(skb, cb, r, net_admin);
0195 }
0196 
0197 static void mptcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
0198                 void *_info)
0199 {
0200     struct mptcp_sock *msk = mptcp_sk(sk);
0201     struct mptcp_info *info = _info;
0202 
0203     r->idiag_rqueue = sk_rmem_alloc_get(sk);
0204     r->idiag_wqueue = sk_wmem_alloc_get(sk);
0205 
0206     if (inet_sk_state_load(sk) == TCP_LISTEN) {
0207         struct sock *lsk = READ_ONCE(msk->first);
0208 
0209         if (lsk) {
0210             /* override with settings from tcp listener,
0211              * so Send-Q will show accept queue.
0212              */
0213             r->idiag_rqueue = READ_ONCE(lsk->sk_ack_backlog);
0214             r->idiag_wqueue = READ_ONCE(lsk->sk_max_ack_backlog);
0215         }
0216     }
0217 
0218     if (!info)
0219         return;
0220 
0221     mptcp_diag_fill_info(msk, info);
0222 }
0223 
0224 static const struct inet_diag_handler mptcp_diag_handler = {
0225     .dump        = mptcp_diag_dump,
0226     .dump_one    = mptcp_diag_dump_one,
0227     .idiag_get_info  = mptcp_diag_get_info,
0228     .idiag_type  = IPPROTO_MPTCP,
0229     .idiag_info_size = sizeof(struct mptcp_info),
0230 };
0231 
0232 static int __init mptcp_diag_init(void)
0233 {
0234     return inet_diag_register(&mptcp_diag_handler);
0235 }
0236 
0237 static void __exit mptcp_diag_exit(void)
0238 {
0239     inet_diag_unregister(&mptcp_diag_handler);
0240 }
0241 
0242 module_init(mptcp_diag_init);
0243 module_exit(mptcp_diag_exit);
0244 MODULE_LICENSE("GPL");
0245 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-262 /* AF_INET - IPPROTO_MPTCP */);