2a46f9f81ba09278195e4fe310cc06c1b3c772f0
[releases.git] / tcp_diag.c
1 /*
2  * tcp_diag.c   Module for monitoring TCP transport protocols sockets.
3  *
4  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
5  *
6  *      This program is free software; you can redistribute it and/or
7  *      modify it under the terms of the GNU General Public License
8  *      as published by the Free Software Foundation; either version
9  *      2 of the License, or (at your option) any later version.
10  */
11
12 #include <linux/module.h>
13 #include <linux/net.h>
14 #include <linux/sock_diag.h>
15 #include <linux/inet_diag.h>
16
17 #include <linux/tcp.h>
18
19 #include <net/netlink.h>
20 #include <net/tcp.h>
21
22 static void tcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
23                               void *_info)
24 {
25         struct tcp_info *info = _info;
26
27         if (inet_sk_state_load(sk) == TCP_LISTEN) {
28                 r->idiag_rqueue = sk->sk_ack_backlog;
29                 r->idiag_wqueue = sk->sk_max_ack_backlog;
30         } else if (sk->sk_type == SOCK_STREAM) {
31                 const struct tcp_sock *tp = tcp_sk(sk);
32
33                 r->idiag_rqueue = max_t(int, READ_ONCE(tp->rcv_nxt) -
34                                              READ_ONCE(tp->copied_seq), 0);
35                 r->idiag_wqueue = READ_ONCE(tp->write_seq) - tp->snd_una;
36         }
37         if (info)
38                 tcp_get_info(sk, info);
39 }
40
41 #ifdef CONFIG_TCP_MD5SIG
42 static void tcp_diag_md5sig_fill(struct tcp_diag_md5sig *info,
43                                  const struct tcp_md5sig_key *key)
44 {
45         info->tcpm_family = key->family;
46         info->tcpm_prefixlen = key->prefixlen;
47         info->tcpm_keylen = key->keylen;
48         memcpy(info->tcpm_key, key->key, key->keylen);
49
50         if (key->family == AF_INET)
51                 info->tcpm_addr[0] = key->addr.a4.s_addr;
52         #if IS_ENABLED(CONFIG_IPV6)
53         else if (key->family == AF_INET6)
54                 memcpy(&info->tcpm_addr, &key->addr.a6,
55                        sizeof(info->tcpm_addr));
56         #endif
57 }
58
59 static int tcp_diag_put_md5sig(struct sk_buff *skb,
60                                const struct tcp_md5sig_info *md5sig)
61 {
62         const struct tcp_md5sig_key *key;
63         struct tcp_diag_md5sig *info;
64         struct nlattr *attr;
65         int md5sig_count = 0;
66
67         hlist_for_each_entry_rcu(key, &md5sig->head, node)
68                 md5sig_count++;
69         if (md5sig_count == 0)
70                 return 0;
71
72         attr = nla_reserve(skb, INET_DIAG_MD5SIG,
73                            md5sig_count * sizeof(struct tcp_diag_md5sig));
74         if (!attr)
75                 return -EMSGSIZE;
76
77         info = nla_data(attr);
78         memset(info, 0, md5sig_count * sizeof(struct tcp_diag_md5sig));
79         hlist_for_each_entry_rcu(key, &md5sig->head, node) {
80                 tcp_diag_md5sig_fill(info++, key);
81                 if (--md5sig_count == 0)
82                         break;
83         }
84
85         return 0;
86 }
87 #endif
88
89 static int tcp_diag_get_aux(struct sock *sk, bool net_admin,
90                             struct sk_buff *skb)
91 {
92 #ifdef CONFIG_TCP_MD5SIG
93         if (net_admin) {
94                 struct tcp_md5sig_info *md5sig;
95                 int err = 0;
96
97                 rcu_read_lock();
98                 md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
99                 if (md5sig)
100                         err = tcp_diag_put_md5sig(skb, md5sig);
101                 rcu_read_unlock();
102                 if (err < 0)
103                         return err;
104         }
105 #endif
106
107         return 0;
108 }
109
110 static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin)
111 {
112         size_t size = 0;
113
114 #ifdef CONFIG_TCP_MD5SIG
115         if (net_admin && sk_fullsock(sk)) {
116                 const struct tcp_md5sig_info *md5sig;
117                 const struct tcp_md5sig_key *key;
118                 size_t md5sig_count = 0;
119
120                 rcu_read_lock();
121                 md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
122                 if (md5sig) {
123                         hlist_for_each_entry_rcu(key, &md5sig->head, node)
124                                 md5sig_count++;
125                 }
126                 rcu_read_unlock();
127                 size += nla_total_size(md5sig_count *
128                                        sizeof(struct tcp_diag_md5sig));
129         }
130 #endif
131
132         return size;
133 }
134
135 static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
136                           const struct inet_diag_req_v2 *r, struct nlattr *bc)
137 {
138         inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r, bc);
139 }
140
141 static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
142                              const struct inet_diag_req_v2 *req)
143 {
144         return inet_diag_dump_one_icsk(&tcp_hashinfo, in_skb, nlh, req);
145 }
146
147 #ifdef CONFIG_INET_DIAG_DESTROY
148 static int tcp_diag_destroy(struct sk_buff *in_skb,
149                             const struct inet_diag_req_v2 *req)
150 {
151         struct net *net = sock_net(in_skb->sk);
152         struct sock *sk = inet_diag_find_one_icsk(net, &tcp_hashinfo, req);
153         int err;
154
155         if (IS_ERR(sk))
156                 return PTR_ERR(sk);
157
158         err = sock_diag_destroy(sk, ECONNABORTED);
159
160         sock_gen_put(sk);
161
162         return err;
163 }
164 #endif
165
166 static const struct inet_diag_handler tcp_diag_handler = {
167         .dump                   = tcp_diag_dump,
168         .dump_one               = tcp_diag_dump_one,
169         .idiag_get_info         = tcp_diag_get_info,
170         .idiag_get_aux          = tcp_diag_get_aux,
171         .idiag_get_aux_size     = tcp_diag_get_aux_size,
172         .idiag_type             = IPPROTO_TCP,
173         .idiag_info_size        = sizeof(struct tcp_info),
174 #ifdef CONFIG_INET_DIAG_DESTROY
175         .destroy                = tcp_diag_destroy,
176 #endif
177 };
178
179 static int __init tcp_diag_init(void)
180 {
181         return inet_diag_register(&tcp_diag_handler);
182 }
183
184 static void __exit tcp_diag_exit(void)
185 {
186         inet_diag_unregister(&tcp_diag_handler);
187 }
188
189 module_init(tcp_diag_init);
190 module_exit(tcp_diag_exit);
191 MODULE_LICENSE("GPL");
192 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-6 /* AF_INET - IPPROTO_TCP */);