Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * tcp_diag.c   Module for monitoring TCP transport protocols sockets.
0004  *
0005  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
0006  */
0007 
0008 #include <linux/module.h>
0009 #include <linux/net.h>
0010 #include <linux/sock_diag.h>
0011 #include <linux/inet_diag.h>
0012 
0013 #include <linux/tcp.h>
0014 
0015 #include <net/netlink.h>
0016 #include <net/tcp.h>
0017 
0018 static void tcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
0019                   void *_info)
0020 {
0021     struct tcp_info *info = _info;
0022 
0023     if (inet_sk_state_load(sk) == TCP_LISTEN) {
0024         r->idiag_rqueue = READ_ONCE(sk->sk_ack_backlog);
0025         r->idiag_wqueue = READ_ONCE(sk->sk_max_ack_backlog);
0026     } else if (sk->sk_type == SOCK_STREAM) {
0027         const struct tcp_sock *tp = tcp_sk(sk);
0028 
0029         r->idiag_rqueue = max_t(int, READ_ONCE(tp->rcv_nxt) -
0030                          READ_ONCE(tp->copied_seq), 0);
0031         r->idiag_wqueue = READ_ONCE(tp->write_seq) - tp->snd_una;
0032     }
0033     if (info)
0034         tcp_get_info(sk, info);
0035 }
0036 
0037 #ifdef CONFIG_TCP_MD5SIG
0038 static void tcp_diag_md5sig_fill(struct tcp_diag_md5sig *info,
0039                  const struct tcp_md5sig_key *key)
0040 {
0041     info->tcpm_family = key->family;
0042     info->tcpm_prefixlen = key->prefixlen;
0043     info->tcpm_keylen = key->keylen;
0044     memcpy(info->tcpm_key, key->key, key->keylen);
0045 
0046     if (key->family == AF_INET)
0047         info->tcpm_addr[0] = key->addr.a4.s_addr;
0048     #if IS_ENABLED(CONFIG_IPV6)
0049     else if (key->family == AF_INET6)
0050         memcpy(&info->tcpm_addr, &key->addr.a6,
0051                sizeof(info->tcpm_addr));
0052     #endif
0053 }
0054 
0055 static int tcp_diag_put_md5sig(struct sk_buff *skb,
0056                    const struct tcp_md5sig_info *md5sig)
0057 {
0058     const struct tcp_md5sig_key *key;
0059     struct tcp_diag_md5sig *info;
0060     struct nlattr *attr;
0061     int md5sig_count = 0;
0062 
0063     hlist_for_each_entry_rcu(key, &md5sig->head, node)
0064         md5sig_count++;
0065     if (md5sig_count == 0)
0066         return 0;
0067 
0068     attr = nla_reserve(skb, INET_DIAG_MD5SIG,
0069                md5sig_count * sizeof(struct tcp_diag_md5sig));
0070     if (!attr)
0071         return -EMSGSIZE;
0072 
0073     info = nla_data(attr);
0074     memset(info, 0, md5sig_count * sizeof(struct tcp_diag_md5sig));
0075     hlist_for_each_entry_rcu(key, &md5sig->head, node) {
0076         tcp_diag_md5sig_fill(info++, key);
0077         if (--md5sig_count == 0)
0078             break;
0079     }
0080 
0081     return 0;
0082 }
0083 #endif
0084 
0085 static int tcp_diag_put_ulp(struct sk_buff *skb, struct sock *sk,
0086                 const struct tcp_ulp_ops *ulp_ops)
0087 {
0088     struct nlattr *nest;
0089     int err;
0090 
0091     nest = nla_nest_start_noflag(skb, INET_DIAG_ULP_INFO);
0092     if (!nest)
0093         return -EMSGSIZE;
0094 
0095     err = nla_put_string(skb, INET_ULP_INFO_NAME, ulp_ops->name);
0096     if (err)
0097         goto nla_failure;
0098 
0099     if (ulp_ops->get_info)
0100         err = ulp_ops->get_info(sk, skb);
0101     if (err)
0102         goto nla_failure;
0103 
0104     nla_nest_end(skb, nest);
0105     return 0;
0106 
0107 nla_failure:
0108     nla_nest_cancel(skb, nest);
0109     return err;
0110 }
0111 
0112 static int tcp_diag_get_aux(struct sock *sk, bool net_admin,
0113                 struct sk_buff *skb)
0114 {
0115     struct inet_connection_sock *icsk = inet_csk(sk);
0116     int err = 0;
0117 
0118 #ifdef CONFIG_TCP_MD5SIG
0119     if (net_admin) {
0120         struct tcp_md5sig_info *md5sig;
0121 
0122         rcu_read_lock();
0123         md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
0124         if (md5sig)
0125             err = tcp_diag_put_md5sig(skb, md5sig);
0126         rcu_read_unlock();
0127         if (err < 0)
0128             return err;
0129     }
0130 #endif
0131 
0132     if (net_admin) {
0133         const struct tcp_ulp_ops *ulp_ops;
0134 
0135         ulp_ops = icsk->icsk_ulp_ops;
0136         if (ulp_ops)
0137             err = tcp_diag_put_ulp(skb, sk, ulp_ops);
0138         if (err)
0139             return err;
0140     }
0141     return 0;
0142 }
0143 
0144 static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin)
0145 {
0146     struct inet_connection_sock *icsk = inet_csk(sk);
0147     size_t size = 0;
0148 
0149 #ifdef CONFIG_TCP_MD5SIG
0150     if (net_admin && sk_fullsock(sk)) {
0151         const struct tcp_md5sig_info *md5sig;
0152         const struct tcp_md5sig_key *key;
0153         size_t md5sig_count = 0;
0154 
0155         rcu_read_lock();
0156         md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
0157         if (md5sig) {
0158             hlist_for_each_entry_rcu(key, &md5sig->head, node)
0159                 md5sig_count++;
0160         }
0161         rcu_read_unlock();
0162         size += nla_total_size(md5sig_count *
0163                        sizeof(struct tcp_diag_md5sig));
0164     }
0165 #endif
0166 
0167     if (net_admin && sk_fullsock(sk)) {
0168         const struct tcp_ulp_ops *ulp_ops;
0169 
0170         ulp_ops = icsk->icsk_ulp_ops;
0171         if (ulp_ops) {
0172             size += nla_total_size(0) +
0173                 nla_total_size(TCP_ULP_NAME_MAX);
0174             if (ulp_ops->get_info_size)
0175                 size += ulp_ops->get_info_size(sk);
0176         }
0177     }
0178     return size;
0179 }
0180 
0181 static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
0182               const struct inet_diag_req_v2 *r)
0183 {
0184     inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r);
0185 }
0186 
0187 static int tcp_diag_dump_one(struct netlink_callback *cb,
0188                  const struct inet_diag_req_v2 *req)
0189 {
0190     return inet_diag_dump_one_icsk(&tcp_hashinfo, cb, req);
0191 }
0192 
0193 #ifdef CONFIG_INET_DIAG_DESTROY
0194 static int tcp_diag_destroy(struct sk_buff *in_skb,
0195                 const struct inet_diag_req_v2 *req)
0196 {
0197     struct net *net = sock_net(in_skb->sk);
0198     struct sock *sk = inet_diag_find_one_icsk(net, &tcp_hashinfo, req);
0199     int err;
0200 
0201     if (IS_ERR(sk))
0202         return PTR_ERR(sk);
0203 
0204     err = sock_diag_destroy(sk, ECONNABORTED);
0205 
0206     sock_gen_put(sk);
0207 
0208     return err;
0209 }
0210 #endif
0211 
0212 static const struct inet_diag_handler tcp_diag_handler = {
0213     .dump           = tcp_diag_dump,
0214     .dump_one       = tcp_diag_dump_one,
0215     .idiag_get_info     = tcp_diag_get_info,
0216     .idiag_get_aux      = tcp_diag_get_aux,
0217     .idiag_get_aux_size = tcp_diag_get_aux_size,
0218     .idiag_type     = IPPROTO_TCP,
0219     .idiag_info_size    = sizeof(struct tcp_info),
0220 #ifdef CONFIG_INET_DIAG_DESTROY
0221     .destroy        = tcp_diag_destroy,
0222 #endif
0223 };
0224 
0225 static int __init tcp_diag_init(void)
0226 {
0227     return inet_diag_register(&tcp_diag_handler);
0228 }
0229 
0230 static void __exit tcp_diag_exit(void)
0231 {
0232     inet_diag_unregister(&tcp_diag_handler);
0233 }
0234 
0235 module_init(tcp_diag_init);
0236 module_exit(tcp_diag_exit);
0237 MODULE_LICENSE("GPL");
0238 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-6 /* AF_INET - IPPROTO_TCP */);