Merge tag 'mips_5.4' of git://git.kernel.org/pub/scm/linux/kernel/git/mips/linux
[linux-2.6-block.git] / net / ipv4 / tcp_diag.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * tcp_diag.c   Module for monitoring TCP transport protocols sockets.
4  *
5  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
6  */
7
8 #include <linux/module.h>
9 #include <linux/net.h>
10 #include <linux/sock_diag.h>
11 #include <linux/inet_diag.h>
12
13 #include <linux/tcp.h>
14
15 #include <net/netlink.h>
16 #include <net/tcp.h>
17
18 static void tcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
19                               void *_info)
20 {
21         struct tcp_info *info = _info;
22
23         if (inet_sk_state_load(sk) == TCP_LISTEN) {
24                 r->idiag_rqueue = sk->sk_ack_backlog;
25                 r->idiag_wqueue = sk->sk_max_ack_backlog;
26         } else if (sk->sk_type == SOCK_STREAM) {
27                 const struct tcp_sock *tp = tcp_sk(sk);
28
29                 r->idiag_rqueue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0);
30                 r->idiag_wqueue = tp->write_seq - tp->snd_una;
31         }
32         if (info)
33                 tcp_get_info(sk, info);
34 }
35
36 #ifdef CONFIG_TCP_MD5SIG
37 static void tcp_diag_md5sig_fill(struct tcp_diag_md5sig *info,
38                                  const struct tcp_md5sig_key *key)
39 {
40         info->tcpm_family = key->family;
41         info->tcpm_prefixlen = key->prefixlen;
42         info->tcpm_keylen = key->keylen;
43         memcpy(info->tcpm_key, key->key, key->keylen);
44
45         if (key->family == AF_INET)
46                 info->tcpm_addr[0] = key->addr.a4.s_addr;
47         #if IS_ENABLED(CONFIG_IPV6)
48         else if (key->family == AF_INET6)
49                 memcpy(&info->tcpm_addr, &key->addr.a6,
50                        sizeof(info->tcpm_addr));
51         #endif
52 }
53
54 static int tcp_diag_put_md5sig(struct sk_buff *skb,
55                                const struct tcp_md5sig_info *md5sig)
56 {
57         const struct tcp_md5sig_key *key;
58         struct tcp_diag_md5sig *info;
59         struct nlattr *attr;
60         int md5sig_count = 0;
61
62         hlist_for_each_entry_rcu(key, &md5sig->head, node)
63                 md5sig_count++;
64         if (md5sig_count == 0)
65                 return 0;
66
67         attr = nla_reserve(skb, INET_DIAG_MD5SIG,
68                            md5sig_count * sizeof(struct tcp_diag_md5sig));
69         if (!attr)
70                 return -EMSGSIZE;
71
72         info = nla_data(attr);
73         memset(info, 0, md5sig_count * sizeof(struct tcp_diag_md5sig));
74         hlist_for_each_entry_rcu(key, &md5sig->head, node) {
75                 tcp_diag_md5sig_fill(info++, key);
76                 if (--md5sig_count == 0)
77                         break;
78         }
79
80         return 0;
81 }
82 #endif
83
84 static int tcp_diag_put_ulp(struct sk_buff *skb, struct sock *sk,
85                             const struct tcp_ulp_ops *ulp_ops)
86 {
87         struct nlattr *nest;
88         int err;
89
90         nest = nla_nest_start_noflag(skb, INET_DIAG_ULP_INFO);
91         if (!nest)
92                 return -EMSGSIZE;
93
94         err = nla_put_string(skb, INET_ULP_INFO_NAME, ulp_ops->name);
95         if (err)
96                 goto nla_failure;
97
98         if (ulp_ops->get_info)
99                 err = ulp_ops->get_info(sk, skb);
100         if (err)
101                 goto nla_failure;
102
103         nla_nest_end(skb, nest);
104         return 0;
105
106 nla_failure:
107         nla_nest_cancel(skb, nest);
108         return err;
109 }
110
111 static int tcp_diag_get_aux(struct sock *sk, bool net_admin,
112                             struct sk_buff *skb)
113 {
114         struct inet_connection_sock *icsk = inet_csk(sk);
115         int err = 0;
116
117 #ifdef CONFIG_TCP_MD5SIG
118         if (net_admin) {
119                 struct tcp_md5sig_info *md5sig;
120
121                 rcu_read_lock();
122                 md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
123                 if (md5sig)
124                         err = tcp_diag_put_md5sig(skb, md5sig);
125                 rcu_read_unlock();
126                 if (err < 0)
127                         return err;
128         }
129 #endif
130
131         if (net_admin) {
132                 const struct tcp_ulp_ops *ulp_ops;
133
134                 ulp_ops = icsk->icsk_ulp_ops;
135                 if (ulp_ops)
136                         err = tcp_diag_put_ulp(skb, sk, ulp_ops);
137                 if (err)
138                         return err;
139         }
140         return 0;
141 }
142
143 static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin)
144 {
145         struct inet_connection_sock *icsk = inet_csk(sk);
146         size_t size = 0;
147
148 #ifdef CONFIG_TCP_MD5SIG
149         if (net_admin && sk_fullsock(sk)) {
150                 const struct tcp_md5sig_info *md5sig;
151                 const struct tcp_md5sig_key *key;
152                 size_t md5sig_count = 0;
153
154                 rcu_read_lock();
155                 md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
156                 if (md5sig) {
157                         hlist_for_each_entry_rcu(key, &md5sig->head, node)
158                                 md5sig_count++;
159                 }
160                 rcu_read_unlock();
161                 size += nla_total_size(md5sig_count *
162                                        sizeof(struct tcp_diag_md5sig));
163         }
164 #endif
165
166         if (net_admin && sk_fullsock(sk)) {
167                 const struct tcp_ulp_ops *ulp_ops;
168
169                 ulp_ops = icsk->icsk_ulp_ops;
170                 if (ulp_ops) {
171                         size += nla_total_size(0) +
172                                 nla_total_size(TCP_ULP_NAME_MAX);
173                         if (ulp_ops->get_info_size)
174                                 size += ulp_ops->get_info_size(sk);
175                 }
176         }
177         return size;
178 }
179
180 static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
181                           const struct inet_diag_req_v2 *r, struct nlattr *bc)
182 {
183         inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r, bc);
184 }
185
186 static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh,
187                              const struct inet_diag_req_v2 *req)
188 {
189         return inet_diag_dump_one_icsk(&tcp_hashinfo, in_skb, nlh, req);
190 }
191
192 #ifdef CONFIG_INET_DIAG_DESTROY
193 static int tcp_diag_destroy(struct sk_buff *in_skb,
194                             const struct inet_diag_req_v2 *req)
195 {
196         struct net *net = sock_net(in_skb->sk);
197         struct sock *sk = inet_diag_find_one_icsk(net, &tcp_hashinfo, req);
198         int err;
199
200         if (IS_ERR(sk))
201                 return PTR_ERR(sk);
202
203         err = sock_diag_destroy(sk, ECONNABORTED);
204
205         sock_gen_put(sk);
206
207         return err;
208 }
209 #endif
210
211 static const struct inet_diag_handler tcp_diag_handler = {
212         .dump                   = tcp_diag_dump,
213         .dump_one               = tcp_diag_dump_one,
214         .idiag_get_info         = tcp_diag_get_info,
215         .idiag_get_aux          = tcp_diag_get_aux,
216         .idiag_get_aux_size     = tcp_diag_get_aux_size,
217         .idiag_type             = IPPROTO_TCP,
218         .idiag_info_size        = sizeof(struct tcp_info),
219 #ifdef CONFIG_INET_DIAG_DESTROY
220         .destroy                = tcp_diag_destroy,
221 #endif
222 };
223
224 static int __init tcp_diag_init(void)
225 {
226         return inet_diag_register(&tcp_diag_handler);
227 }
228
229 static void __exit tcp_diag_exit(void)
230 {
231         inet_diag_unregister(&tcp_diag_handler);
232 }
233
234 module_init(tcp_diag_init);
235 module_exit(tcp_diag_exit);
236 MODULE_LICENSE("GPL");
237 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-6 /* AF_INET - IPPROTO_TCP */);