blob: 5d50aad3cdbf420b4fd8baa8429f213d1846fa55 [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03003 * inet_diag.c Module for monitoring INET transport protocols sockets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 */
7
Ilpo Järvinen172589c2007-08-28 15:50:33 -07008#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/fcntl.h>
12#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/cache.h>
15#include <linux/init.h>
16#include <linux/time.h>
17
18#include <net/icmp.h>
19#include <net/tcp.h>
20#include <net/ipv6.h>
21#include <net/inet_common.h>
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -030022#include <net/inet_connection_sock.h>
23#include <net/inet_hashtables.h>
24#include <net/inet_timewait_sock.h>
25#include <net/inet6_hashtables.h>
Martin KaFai Lau085c20c2020-02-25 15:04:27 -080026#include <net/bpf_sk_storage.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070027#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
29#include <linux/inet.h>
30#include <linux/stddef.h>
31
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -030032#include <linux/inet_diag.h>
Pavel Emelyanovd3664772011-12-06 07:58:03 +000033#include <linux/sock_diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030035static const struct inet_diag_handler **inet_diag_table;
36
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030037struct inet_diag_entry {
Eric Dumazete31c5e02015-03-10 07:15:53 -070038 const __be32 *saddr;
39 const __be32 *daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 u16 sport;
41 u16 dport;
42 u16 family;
43 u16 userlocks;
David Ahern637c8412016-06-23 18:42:51 -070044 u32 ifindex;
Lorenzo Colittia52e95a2016-08-24 15:46:26 +090045 u32 mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
Herbert Xud523a322007-12-03 15:51:25 +110048static DEFINE_MUTEX(inet_diag_table_mutex);
49
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000050static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
Herbert Xud523a322007-12-03 15:51:25 +110051{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000052 if (!inet_diag_table[proto])
Xin Longbf2ae2e2018-03-10 18:57:50 +080053 sock_load_diag_module(AF_INET, proto);
Herbert Xud523a322007-12-03 15:51:25 +110054
55 mutex_lock(&inet_diag_table_mutex);
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000056 if (!inet_diag_table[proto])
Herbert Xud523a322007-12-03 15:51:25 +110057 return ERR_PTR(-ENOENT);
58
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000059 return inet_diag_table[proto];
Herbert Xud523a322007-12-03 15:51:25 +110060}
61
Eric Dumazete31c5e02015-03-10 07:15:53 -070062static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
Herbert Xud523a322007-12-03 15:51:25 +110063{
64 mutex_unlock(&inet_diag_table_mutex);
65}
66
Xin Longcb2050a2016-04-14 15:35:32 +080067void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
Eric Dumazeta4458342015-03-13 15:51:12 -070068{
69 r->idiag_family = sk->sk_family;
70
71 r->id.idiag_sport = htons(sk->sk_num);
72 r->id.idiag_dport = sk->sk_dport;
73 r->id.idiag_if = sk->sk_bound_dev_if;
74 sock_diag_save_cookie(sk, r->id.idiag_cookie);
75
76#if IS_ENABLED(CONFIG_IPV6)
77 if (sk->sk_family == AF_INET6) {
78 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
79 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
80 } else
81#endif
82 {
83 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
84 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
85
86 r->id.idiag_src[0] = sk->sk_rcv_saddr;
87 r->id.idiag_dst[0] = sk->sk_daddr;
88 }
89}
Xin Longcb2050a2016-04-14 15:35:32 +080090EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill);
Eric Dumazeta4458342015-03-13 15:51:12 -070091
Ivan Delalandeb37e8842017-08-31 09:59:38 -070092static size_t inet_sk_attr_size(struct sock *sk,
93 const struct inet_diag_req_v2 *req,
94 bool net_admin)
Eric Dumazetc8e2c802015-03-13 09:49:59 -070095{
Ivan Delalandeb37e8842017-08-31 09:59:38 -070096 const struct inet_diag_handler *handler;
97 size_t aux = 0;
98
99 handler = inet_diag_table[req->sdiag_protocol];
100 if (handler && handler->idiag_get_aux_size)
101 aux = handler->idiag_get_aux_size(sk, net_admin);
102
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700103 return nla_total_size(sizeof(struct tcp_info))
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700104 + nla_total_size(sizeof(struct inet_diag_msg))
Dmitry Yakunin83f73c5b2020-03-05 15:33:12 +0300105 + inet_diag_msg_attrs_size()
106 + nla_total_size(sizeof(struct inet_diag_meminfo))
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700107 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
108 + nla_total_size(TCP_CA_NAME_MAX)
109 + nla_total_size(sizeof(struct tcpvegas_info))
Ivan Delalandeb37e8842017-08-31 09:59:38 -0700110 + aux
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700111 + 64;
112}
113
Xin Longcb2050a2016-04-14 15:35:32 +0800114int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
115 struct inet_diag_msg *r, int ext,
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900116 struct user_namespace *user_ns,
117 bool net_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700119 const struct inet_sock *inet = inet_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Pavel Emelyanove4e541a2012-10-23 22:29:56 +0400121 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
122 goto errout;
123
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500124 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
125 * hence this needs to be included regardless of socket family.
126 */
127 if (ext & (1 << (INET_DIAG_TOS - 1)))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000128 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
129 goto errout;
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500130
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000131#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300132 if (r->idiag_family == AF_INET6) {
Maciej Żenczykowski06236ac2011-11-07 14:23:11 +0000133 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
Eric Dumazetefe42082013-10-03 15:42:29 -0700134 if (nla_put_u8(skb, INET_DIAG_TCLASS,
135 inet6_sk(sk)->tclass) < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000136 goto errout;
Phil Sutter20462152015-06-24 11:02:51 +0200137
Phil Sutter8220ea22015-07-10 11:39:57 +0200138 if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
139 nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
Phil Sutter20462152015-06-24 11:02:51 +0200140 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142#endif
143
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900144 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark))
145 goto errout;
146
Dmitry Yakunin83f73c5b2020-03-05 15:33:12 +0300147 if (ext & (1 << (INET_DIAG_CLASS_ID - 1)) ||
148 ext & (1 << (INET_DIAG_TCLASS - 1))) {
149 u32 classid = 0;
150
151#ifdef CONFIG_SOCK_CGROUP_DATA
152 classid = sock_cgroup_classid(&sk->sk_cgrp_data);
153#endif
154 /* Fallback to socket priority if class id isn't set.
155 * Classful qdiscs use it as direct reference to class.
156 * For cgroup2 classid is always zero.
157 */
158 if (!classid)
159 classid = sk->sk_priority;
160
161 if (nla_put_u32(skb, INET_DIAG_CLASS_ID, classid))
162 goto errout;
163 }
164
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600165 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000166 r->idiag_inode = sock_i_ino(sk);
167
Xin Longcb2050a2016-04-14 15:35:32 +0800168 return 0;
169errout:
170 return 1;
171}
172EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
173
Martin KaFai Lau085c20c2020-02-25 15:04:27 -0800174#define MAX_DUMP_ALLOC_SIZE (KMALLOC_MAX_SIZE - SKB_DATA_ALIGN(sizeof(struct skb_shared_info)))
175
Xin Longcb2050a2016-04-14 15:35:32 +0800176int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800177 struct sk_buff *skb, struct netlink_callback *cb,
178 const struct inet_diag_req_v2 *req,
179 u16 nlmsg_flags, bool net_admin)
Xin Longcb2050a2016-04-14 15:35:32 +0800180{
181 const struct tcp_congestion_ops *ca_ops;
182 const struct inet_diag_handler *handler;
Martin KaFai Lau085c20c2020-02-25 15:04:27 -0800183 struct inet_diag_dump_data *cb_data;
Xin Longcb2050a2016-04-14 15:35:32 +0800184 int ext = req->idiag_ext;
185 struct inet_diag_msg *r;
186 struct nlmsghdr *nlh;
187 struct nlattr *attr;
188 void *info = NULL;
189
Martin KaFai Lau085c20c2020-02-25 15:04:27 -0800190 cb_data = cb->data;
Xin Longcb2050a2016-04-14 15:35:32 +0800191 handler = inet_diag_table[req->sdiag_protocol];
192 BUG_ON(!handler);
193
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800194 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
195 cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags);
Xin Longcb2050a2016-04-14 15:35:32 +0800196 if (!nlh)
197 return -EMSGSIZE;
198
199 r = nlmsg_data(nlh);
200 BUG_ON(!sk_fullsock(sk));
201
202 inet_diag_msg_common_fill(r, sk);
203 r->idiag_state = sk->sk_state;
204 r->idiag_timer = 0;
205 r->idiag_retrans = 0;
206
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800207 if (inet_diag_msg_attrs_fill(sk, skb, r, ext,
208 sk_user_ns(NETLINK_CB(cb->skb).sk),
209 net_admin))
Xin Longcb2050a2016-04-14 15:35:32 +0800210 goto errout;
211
Thomas Graf6e277ed2012-06-26 23:36:12 +0000212 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
213 struct inet_diag_meminfo minfo = {
214 .idiag_rmem = sk_rmem_alloc_get(sk),
Eric Dumazetab4e8462019-10-10 20:17:46 -0700215 .idiag_wmem = READ_ONCE(sk->sk_wmem_queued),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000216 .idiag_fmem = sk->sk_forward_alloc,
217 .idiag_tmem = sk_wmem_alloc_get(sk),
218 };
219
220 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
221 goto errout;
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000222 }
223
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000224 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
225 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000226 goto errout;
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000227
Cyrill Gorcunov432490f2016-10-21 13:03:44 +0300228 /*
229 * RAW sockets might have user-defined protocols assigned,
230 * so report the one supplied on socket creation.
231 */
232 if (sk->sk_type == SOCK_RAW) {
233 if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))
234 goto errout;
235 }
236
Eric Dumazete31c5e02015-03-10 07:15:53 -0700237 if (!icsk) {
Shan Wei62ad6fc2012-04-24 18:15:41 +0000238 handler->idiag_get_info(sk, r, NULL);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000239 goto out;
240 }
241
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000242 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
Yuchung Cheng57dde7f2017-01-12 22:11:33 -0800243 icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000244 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300245 r->idiag_timer = 1;
246 r->idiag_retrans = icsk->icsk_retransmits;
Xin Longb7de5292016-04-19 15:10:01 +0800247 r->idiag_expires =
Eric Dumazet3828a932019-11-05 14:11:50 -0800248 jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700249 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300250 r->idiag_timer = 4;
251 r->idiag_retrans = icsk->icsk_probes_out;
Xin Longb7de5292016-04-19 15:10:01 +0800252 r->idiag_expires =
Eric Dumazet3828a932019-11-05 14:11:50 -0800253 jiffies_delta_to_msecs(icsk->icsk_timeout - jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300255 r->idiag_timer = 2;
256 r->idiag_retrans = icsk->icsk_probes_out;
Xin Longb7de5292016-04-19 15:10:01 +0800257 r->idiag_expires =
Eric Dumazet3828a932019-11-05 14:11:50 -0800258 jiffies_delta_to_msecs(sk->sk_timer.expires - jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300260 r->idiag_timer = 0;
261 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300263
Craig Gallek3fd22af2015-06-15 11:26:19 -0400264 if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
Nicolas Dichtel6ed46d12016-04-26 10:06:14 +0200265 attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
266 handler->idiag_info_size,
267 INET_DIAG_PAD);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000268 if (!attr)
269 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Thomas Graf6e277ed2012-06-26 23:36:12 +0000271 info = nla_data(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
273
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700274 if (ext & (1 << (INET_DIAG_CONG - 1))) {
275 int err = 0;
276
277 rcu_read_lock();
278 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
279 if (ca_ops)
280 err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
281 rcu_read_unlock();
282 if (err < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000283 goto errout;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700284 }
Thomas Graf6e277ed2012-06-26 23:36:12 +0000285
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300286 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287
Ivan Delalandeb37e8842017-08-31 09:59:38 -0700288 if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux)
289 if (handler->idiag_get_aux(sk, net_admin, skb) < 0)
290 goto errout;
291
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700292 if (sk->sk_state < TCP_TIME_WAIT) {
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700293 union tcp_cc_info info;
294 size_t sz = 0;
295 int attr;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700296
297 rcu_read_lock();
298 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
299 if (ca_ops && ca_ops->get_info)
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700300 sz = ca_ops->get_info(sk, ext, &attr, &info);
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700301 rcu_read_unlock();
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700302 if (sz && nla_put(skb, attr, sz, &info) < 0)
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700303 goto errout;
304 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305
Martin KaFai Lau085c20c2020-02-25 15:04:27 -0800306 /* Keep it at the end for potential retry with a larger skb,
307 * or else do best-effort fitting, which is only done for the
308 * first_nlmsg.
309 */
310 if (cb_data->bpf_stg_diag) {
311 bool first_nlmsg = ((unsigned char *)nlh == skb->data);
312 unsigned int prev_min_dump_alloc;
313 unsigned int total_nla_size = 0;
314 unsigned int msg_len;
315 int err;
316
317 msg_len = skb_tail_pointer(skb) - (unsigned char *)nlh;
318 err = bpf_sk_storage_diag_put(cb_data->bpf_stg_diag, sk, skb,
319 INET_DIAG_SK_BPF_STORAGES,
320 &total_nla_size);
321
322 if (!err)
323 goto out;
324
325 total_nla_size += msg_len;
326 prev_min_dump_alloc = cb->min_dump_alloc;
327 if (total_nla_size > prev_min_dump_alloc)
328 cb->min_dump_alloc = min_t(u32, total_nla_size,
329 MAX_DUMP_ALLOC_SIZE);
330
331 if (!first_nlmsg)
332 goto errout;
333
334 if (cb->min_dump_alloc > prev_min_dump_alloc)
335 /* Retry with pskb_expand_head() with
336 * __GFP_DIRECT_RECLAIM
337 */
338 goto errout;
339
340 WARN_ON_ONCE(total_nla_size <= prev_min_dump_alloc);
341
342 /* Send what we have for this sk
343 * and move on to the next sk in the following
344 * dump()
345 */
346 }
347
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000348out:
Johannes Berg053c0952015-01-16 22:09:00 +0100349 nlmsg_end(skb, nlh);
350 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Thomas Graf6e277ed2012-06-26 23:36:12 +0000352errout:
353 nlmsg_cancel(skb, nlh);
Patrick McHardy26932562007-01-31 23:16:40 -0800354 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355}
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000356EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
357
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700358static int inet_twsk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700359 struct sk_buff *skb,
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800360 struct netlink_callback *cb,
361 u16 nlmsg_flags)
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800362{
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700363 struct inet_timewait_sock *tw = inet_twsk(sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800364 struct inet_diag_msg *r;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000365 struct nlmsghdr *nlh;
Eric Dumazet789f5582015-04-12 18:51:09 -0700366 long tmo;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800367
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800368 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
369 cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type,
370 sizeof(*r), nlmsg_flags);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000371 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700372 return -EMSGSIZE;
David S. Millerd1063522012-06-26 21:28:54 -0700373
374 r = nlmsg_data(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800375 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
376
Eric Dumazeta4458342015-03-13 15:51:12 -0700377 inet_diag_msg_common_fill(r, sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800378 r->idiag_retrans = 0;
Daniel Borkmannb1aac812013-12-17 00:38:39 +0100379
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800380 r->idiag_state = tw->tw_substate;
381 r->idiag_timer = 3;
Eric Dumazet3828a932019-11-05 14:11:50 -0800382 tmo = tw->tw_timer.expires - jiffies;
383 r->idiag_expires = jiffies_delta_to_msecs(tmo);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800384 r->idiag_rqueue = 0;
385 r->idiag_wqueue = 0;
386 r->idiag_uid = 0;
387 r->idiag_inode = 0;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000388
Johannes Berg053c0952015-01-16 22:09:00 +0100389 nlmsg_end(skb, nlh);
390 return 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800391}
392
Eric Dumazeta58917f2015-03-15 21:12:14 -0700393static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800394 struct netlink_callback *cb,
395 u16 nlmsg_flags, bool net_admin)
Eric Dumazeta58917f2015-03-15 21:12:14 -0700396{
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900397 struct request_sock *reqsk = inet_reqsk(sk);
Eric Dumazeta58917f2015-03-15 21:12:14 -0700398 struct inet_diag_msg *r;
399 struct nlmsghdr *nlh;
400 long tmo;
401
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800402 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
403 cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags);
Eric Dumazeta58917f2015-03-15 21:12:14 -0700404 if (!nlh)
405 return -EMSGSIZE;
406
407 r = nlmsg_data(nlh);
408 inet_diag_msg_common_fill(r, sk);
409 r->idiag_state = TCP_SYN_RECV;
410 r->idiag_timer = 1;
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900411 r->idiag_retrans = reqsk->num_retrans;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700412
413 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
414 offsetof(struct sock, sk_cookie));
415
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700416 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
Eric Dumazet3828a932019-11-05 14:11:50 -0800417 r->idiag_expires = jiffies_delta_to_msecs(tmo);
Eric Dumazeta58917f2015-03-15 21:12:14 -0700418 r->idiag_rqueue = 0;
419 r->idiag_wqueue = 0;
420 r->idiag_uid = 0;
421 r->idiag_inode = 0;
422
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900423 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
424 inet_rsk(reqsk)->ir_mark))
425 return -EMSGSIZE;
426
Eric Dumazeta58917f2015-03-15 21:12:14 -0700427 nlmsg_end(skb, nlh);
428 return 0;
429}
430
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800431static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800432 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700433 const struct inet_diag_req_v2 *r,
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800434 u16 nlmsg_flags, bool net_admin)
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800435{
436 if (sk->sk_state == TCP_TIME_WAIT)
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800437 return inet_twsk_diag_fill(sk, skb, cb, nlmsg_flags);
Eric Dumazetefe42082013-10-03 15:42:29 -0700438
Eric Dumazeta58917f2015-03-15 21:12:14 -0700439 if (sk->sk_state == TCP_NEW_SYN_RECV)
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800440 return inet_req_diag_fill(sk, skb, cb, nlmsg_flags, net_admin);
Eric Dumazeta58917f2015-03-15 21:12:14 -0700441
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800442 return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, r, nlmsg_flags,
443 net_admin);
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800444}
445
Lorenzo Colittib613f562015-12-16 12:30:02 +0900446struct sock *inet_diag_find_one_icsk(struct net *net,
447 struct inet_hashinfo *hashinfo,
448 const struct inet_diag_req_v2 *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700450 struct sock *sk;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300451
Eric Dumazet2d331912016-04-01 08:52:15 -0700452 rcu_read_lock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700453 if (req->sdiag_family == AF_INET)
Craig Galleka5836362016-02-10 11:50:38 -0500454 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300455 req->id.idiag_dport, req->id.idiag_src[0],
456 req->id.idiag_sport, req->id.idiag_if);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000457#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet7c130672016-01-20 16:25:01 -0800458 else if (req->sdiag_family == AF_INET6) {
459 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
460 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
Craig Galleka5836362016-02-10 11:50:38 -0500461 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
Eric Dumazet7c130672016-01-20 16:25:01 -0800462 req->id.idiag_dport, req->id.idiag_src[3],
463 req->id.idiag_sport, req->id.idiag_if);
464 else
Craig Galleka5836362016-02-10 11:50:38 -0500465 sk = inet6_lookup(net, hashinfo, NULL, 0,
Eric Dumazet7c130672016-01-20 16:25:01 -0800466 (struct in6_addr *)req->id.idiag_dst,
467 req->id.idiag_dport,
468 (struct in6_addr *)req->id.idiag_src,
469 req->id.idiag_sport,
470 req->id.idiag_if);
471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472#endif
Eric Dumazet2d331912016-04-01 08:52:15 -0700473 else {
474 rcu_read_unlock();
Lorenzo Colittib613f562015-12-16 12:30:02 +0900475 return ERR_PTR(-EINVAL);
Eric Dumazet2d331912016-04-01 08:52:15 -0700476 }
477 rcu_read_unlock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700478 if (!sk)
Lorenzo Colittib613f562015-12-16 12:30:02 +0900479 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Lorenzo Colittib613f562015-12-16 12:30:02 +0900481 if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
482 sock_gen_put(sk);
483 return ERR_PTR(-ENOENT);
484 }
485
486 return sk;
487}
488EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
489
490int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800491 struct netlink_callback *cb,
Lorenzo Colittib613f562015-12-16 12:30:02 +0900492 const struct inet_diag_req_v2 *req)
493{
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800494 struct sk_buff *in_skb = cb->skb;
Ivan Delalandeb37e8842017-08-31 09:59:38 -0700495 bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN);
Lorenzo Colittib613f562015-12-16 12:30:02 +0900496 struct net *net = sock_net(in_skb->sk);
497 struct sk_buff *rep;
498 struct sock *sk;
499 int err;
500
501 sk = inet_diag_find_one_icsk(net, hashinfo, req);
502 if (IS_ERR(sk))
503 return PTR_ERR(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Ivan Delalandeb37e8842017-08-31 09:59:38 -0700505 rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000506 if (!rep) {
507 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto out;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800511 err = sk_diag_fill(sk, rep, cb, req, 0, net_admin);
Patrick McHardy26932562007-01-31 23:16:40 -0800512 if (err < 0) {
513 WARN_ON(err == -EMSGSIZE);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000514 nlmsg_free(rep);
Patrick McHardy26932562007-01-31 23:16:40 -0800515 goto out;
516 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000517 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300518 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (err > 0)
520 err = 0;
521
522out:
Eric Dumazetc1d607c2013-10-11 08:54:49 -0700523 if (sk)
524 sock_gen_put(sk);
525
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000526 return err;
527}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000528EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000529
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900530static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000531 const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700532 const struct inet_diag_req_v2 *req)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000533{
534 const struct inet_diag_handler *handler;
535 int err;
536
537 handler = inet_diag_lock_handler(req->sdiag_protocol);
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800538 if (IS_ERR(handler)) {
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000539 err = PTR_ERR(handler);
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800540 } else if (cmd == SOCK_DIAG_BY_FAMILY) {
Martin KaFai Lau0df6d322020-02-25 15:04:15 -0800541 struct inet_diag_dump_data empty_dump_data = {};
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800542 struct netlink_callback cb = {
543 .nlh = nlh,
544 .skb = in_skb,
Martin KaFai Lau0df6d322020-02-25 15:04:15 -0800545 .data = &empty_dump_data,
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800546 };
547 err = handler->dump_one(&cb, req);
548 } else if (cmd == SOCK_DESTROY && handler->destroy) {
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900549 err = handler->destroy(in_skb, req);
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800550 } else {
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900551 err = -EOPNOTSUPP;
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800552 }
Herbert Xud523a322007-12-03 15:51:25 +1100553 inet_diag_unlock_handler(handler);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return err;
556}
557
Al Viro9f855292006-09-27 18:44:30 -0700558static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
560 int words = bits >> 5;
561
562 bits &= 0x1f;
563
564 if (words) {
565 if (memcmp(a1, a2, words << 2))
566 return 0;
567 }
568 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700569 __be32 w1, w2;
570 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 w1 = a1[words];
573 w2 = a2[words];
574
575 mask = htonl((0xffffffff) << (32 - bits));
576
577 if ((w1 ^ w2) & mask)
578 return 0;
579 }
580
581 return 1;
582}
583
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000584static int inet_diag_bc_run(const struct nlattr *_bc,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700585 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000587 const void *bc = nla_data(_bc);
588 int len = nla_len(_bc);
589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 while (len > 0) {
591 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300592 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593
594 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300595 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300597 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 yes = 0;
599 break;
Kristian Evensenbbb61892017-12-27 18:27:58 +0100600 case INET_DIAG_BC_S_EQ:
601 yes = entry->sport == op[1].no;
602 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300603 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 yes = entry->sport >= op[1].no;
605 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300606 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800607 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 break;
Kristian Evensenbbb61892017-12-27 18:27:58 +0100609 case INET_DIAG_BC_D_EQ:
610 yes = entry->dport == op[1].no;
611 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300612 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 yes = entry->dport >= op[1].no;
614 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300615 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 yes = entry->dport <= op[1].no;
617 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300618 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
620 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300621 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300622 case INET_DIAG_BC_D_COND: {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700623 const struct inet_diag_hostcond *cond;
624 const __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625
Eric Dumazete31c5e02015-03-10 07:15:53 -0700626 cond = (const struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300628 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 entry->sport : entry->dport)) {
630 yes = 0;
631 break;
632 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800633
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300634 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 addr = entry->saddr;
636 else
637 addr = entry->daddr;
638
Neal Cardwellf67caec2012-12-08 19:43:23 +0000639 if (cond->family != AF_UNSPEC &&
640 cond->family != entry->family) {
641 if (entry->family == AF_INET6 &&
642 cond->family == AF_INET) {
643 if (addr[0] == 0 && addr[1] == 0 &&
644 addr[2] == htonl(0xffff) &&
645 bitstring_match(addr + 3,
646 cond->addr,
647 cond->prefix_len))
648 break;
649 }
650 yes = 0;
651 break;
652 }
653
654 if (cond->prefix_len == 0)
655 break;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800656 if (bitstring_match(addr, cond->addr,
657 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 yes = 0;
660 break;
661 }
David Ahern637c8412016-06-23 18:42:51 -0700662 case INET_DIAG_BC_DEV_COND: {
663 u32 ifindex;
664
665 ifindex = *((const u32 *)(op + 1));
666 if (ifindex != entry->ifindex)
667 yes = 0;
668 break;
669 }
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900670 case INET_DIAG_BC_MARK_COND: {
671 struct inet_diag_markcond *cond;
672
673 cond = (struct inet_diag_markcond *)(op + 1);
674 if ((entry->mark & cond->mask) != cond->mark)
675 yes = 0;
676 break;
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800680 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 len -= op->yes;
682 bc += op->yes;
683 } else {
684 len -= op->no;
685 bc += op->no;
686 }
687 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000688 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Eric Dumazeta4458342015-03-13 15:51:12 -0700691/* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
692 */
693static void entry_fill_addrs(struct inet_diag_entry *entry,
694 const struct sock *sk)
695{
696#if IS_ENABLED(CONFIG_IPV6)
697 if (sk->sk_family == AF_INET6) {
698 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
699 entry->daddr = sk->sk_v6_daddr.s6_addr32;
700 } else
701#endif
702 {
703 entry->saddr = &sk->sk_rcv_saddr;
704 entry->daddr = &sk->sk_daddr;
705 }
706}
707
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000708int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
709{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000710 struct inet_sock *inet = inet_sk(sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700711 struct inet_diag_entry entry;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000712
Eric Dumazete31c5e02015-03-10 07:15:53 -0700713 if (!bc)
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000714 return 1;
715
716 entry.family = sk->sk_family;
Eric Dumazeta4458342015-03-13 15:51:12 -0700717 entry_fill_addrs(&entry, sk);
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000718 entry.sport = inet->inet_num;
719 entry.dport = ntohs(inet->inet_dport);
David Ahern637c8412016-06-23 18:42:51 -0700720 entry.ifindex = sk->sk_bound_dev_if;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700721 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900722 if (sk_fullsock(sk))
723 entry.mark = sk->sk_mark;
724 else if (sk->sk_state == TCP_NEW_SYN_RECV)
725 entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
726 else
727 entry.mark = 0;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000728
729 return inet_diag_bc_run(bc, &entry);
730}
731EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733static int valid_cc(const void *bc, int len, int cc)
734{
735 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300736 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (cc > len)
739 return 0;
740 if (cc == len)
741 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400742 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 return 0;
744 len -= op->yes;
745 bc += op->yes;
746 }
747 return 0;
748}
749
David Ahern637c8412016-06-23 18:42:51 -0700750/* data is u32 ifindex */
751static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
752 int *min_len)
753{
754 /* Check ifindex space. */
755 *min_len += sizeof(u32);
756 if (len < *min_len)
757 return false;
758
759 return true;
760}
Neal Cardwell405c0052012-12-08 19:43:22 +0000761/* Validate an inet_diag_hostcond. */
762static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
763 int *min_len)
764{
Neal Cardwell405c0052012-12-08 19:43:22 +0000765 struct inet_diag_hostcond *cond;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700766 int addr_len;
Neal Cardwell405c0052012-12-08 19:43:22 +0000767
768 /* Check hostcond space. */
769 *min_len += sizeof(struct inet_diag_hostcond);
770 if (len < *min_len)
771 return false;
772 cond = (struct inet_diag_hostcond *)(op + 1);
773
774 /* Check address family and address length. */
775 switch (cond->family) {
776 case AF_UNSPEC:
777 addr_len = 0;
778 break;
779 case AF_INET:
780 addr_len = sizeof(struct in_addr);
781 break;
782 case AF_INET6:
783 addr_len = sizeof(struct in6_addr);
784 break;
785 default:
786 return false;
787 }
788 *min_len += addr_len;
789 if (len < *min_len)
790 return false;
791
792 /* Check prefix length (in bits) vs address length (in bytes). */
793 if (cond->prefix_len > 8 * addr_len)
794 return false;
795
796 return true;
797}
798
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000799/* Validate a port comparison operator. */
Eric Dumazete31c5e02015-03-10 07:15:53 -0700800static bool valid_port_comparison(const struct inet_diag_bc_op *op,
801 int len, int *min_len)
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000802{
803 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
804 *min_len += sizeof(struct inet_diag_bc_op);
805 if (len < *min_len)
806 return false;
807 return true;
808}
809
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900810static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
811 int *min_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900813 *min_len += sizeof(struct inet_diag_markcond);
814 return len >= *min_len;
815}
816
817static int inet_diag_bc_audit(const struct nlattr *attr,
818 const struct sk_buff *skb)
819{
820 bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN);
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +0900821 const void *bytecode, *bc;
822 int bytecode_len, len;
823
824 if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
825 return -EINVAL;
826
827 bytecode = bc = nla_data(attr);
828 len = bytecode_len = nla_len(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829
830 while (len > 0) {
Neal Cardwell405c0052012-12-08 19:43:22 +0000831 int min_len = sizeof(struct inet_diag_bc_op);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700832 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300835 case INET_DIAG_BC_S_COND:
836 case INET_DIAG_BC_D_COND:
Neal Cardwell405c0052012-12-08 19:43:22 +0000837 if (!valid_hostcond(bc, len, &min_len))
838 return -EINVAL;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000839 break;
David Ahern637c8412016-06-23 18:42:51 -0700840 case INET_DIAG_BC_DEV_COND:
841 if (!valid_devcond(bc, len, &min_len))
842 return -EINVAL;
843 break;
Kristian Evensenbbb61892017-12-27 18:27:58 +0100844 case INET_DIAG_BC_S_EQ:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300845 case INET_DIAG_BC_S_GE:
846 case INET_DIAG_BC_S_LE:
Kristian Evensenbbb61892017-12-27 18:27:58 +0100847 case INET_DIAG_BC_D_EQ:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300848 case INET_DIAG_BC_D_GE:
849 case INET_DIAG_BC_D_LE:
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000850 if (!valid_port_comparison(bc, len, &min_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return -EINVAL;
852 break;
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900853 case INET_DIAG_BC_MARK_COND:
854 if (!net_admin)
855 return -EPERM;
856 if (!valid_markcond(bc, len, &min_len))
857 return -EINVAL;
858 break;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000859 case INET_DIAG_BC_AUTO:
860 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300861 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 break;
863 default:
864 return -EINVAL;
865 }
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000866
867 if (op->code != INET_DIAG_BC_NOP) {
868 if (op->no < min_len || op->no > len + 4 || op->no & 3)
869 return -EINVAL;
870 if (op->no < len &&
871 !valid_cc(bytecode, bytecode_len, len - op->no))
872 return -EINVAL;
873 }
874
Neal Cardwell405c0052012-12-08 19:43:22 +0000875 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
Eric Dumazeteeb14972011-06-17 16:25:39 -0400876 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800877 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 len -= op->yes;
879 }
880 return len == 0 ? 0 : -EINVAL;
881}
882
Eric Dumazet49612722015-03-05 10:18:14 -0800883static void twsk_build_assert(void)
884{
885 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
886 offsetof(struct sock, sk_family));
887
888 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
889 offsetof(struct inet_sock, inet_num));
890
891 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
892 offsetof(struct inet_sock, inet_dport));
893
894 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
895 offsetof(struct inet_sock, inet_rcv_saddr));
896
897 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
898 offsetof(struct inet_sock, inet_daddr));
899
900#if IS_ENABLED(CONFIG_IPV6)
901 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
902 offsetof(struct sock, sk_v6_rcv_saddr));
903
904 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
905 offsetof(struct sock, sk_v6_daddr));
906#endif
907}
908
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000909void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700910 struct netlink_callback *cb,
Martin KaFai Lau0df6d322020-02-25 15:04:15 -0800911 const struct inet_diag_req_v2 *r)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900913 bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
Martin KaFai Lau0df6d322020-02-25 15:04:15 -0800914 struct inet_diag_dump_data *cb_data = cb->data;
Eric Dumazet67db3e42016-11-04 11:54:32 -0700915 struct net *net = sock_net(skb->sk);
916 u32 idiag_states = r->idiag_states;
917 int i, num, s_i, s_num;
Martin KaFai Lau0df6d322020-02-25 15:04:15 -0800918 struct nlattr *bc;
Eric Dumazet67db3e42016-11-04 11:54:32 -0700919 struct sock *sk;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800920
Martin KaFai Lau0df6d322020-02-25 15:04:15 -0800921 bc = cb_data->inet_diag_nla_bc;
Eric Dumazet079096f2015-10-02 11:43:32 -0700922 if (idiag_states & TCPF_SYN_RECV)
923 idiag_states |= TCPF_NEW_SYN_RECV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 s_i = cb->args[1];
925 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (cb->args[0] == 0) {
Eric Dumazet9652dc22016-10-19 21:24:58 -0700928 if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300930
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700931 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800932 struct inet_listen_hashbucket *ilb;
Eric Dumazet8dbd76e2019-12-13 18:20:41 -0800933 struct hlist_nulls_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934
935 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800936 ilb = &hashinfo->listening_hash[i];
Eric Dumazet9652dc22016-10-19 21:24:58 -0700937 spin_lock(&ilb->lock);
Eric Dumazet8dbd76e2019-12-13 18:20:41 -0800938 sk_nulls_for_each(sk, node, &ilb->nulls_head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 struct inet_sock *inet = inet_sk(sk);
940
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000941 if (!net_eq(sock_net(sk), net))
942 continue;
943
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 if (num < s_num) {
945 num++;
946 continue;
947 }
948
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000949 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazete31c5e02015-03-10 07:15:53 -0700950 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000951 goto next_listen;
952
Eric Dumazetc720c7e82009-10-15 06:30:45 +0000953 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300954 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 goto next_listen;
956
Martin KaFai Lau5682d392020-02-25 15:04:09 -0800957 if (!inet_diag_bc_sk(bc, sk))
958 goto next_listen;
959
960 if (inet_sk_diag_fill(sk, inet_csk(sk), skb,
961 cb, r, NLM_F_MULTI,
962 net_admin) < 0) {
Eric Dumazet9652dc22016-10-19 21:24:58 -0700963 spin_unlock(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 goto done;
965 }
966
967next_listen:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 ++num;
969 }
Eric Dumazet9652dc22016-10-19 21:24:58 -0700970 spin_unlock(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
972 s_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974skip_listen_ht:
975 cb->args[0] = 1;
976 s_i = num = s_num = 0;
977 }
978
Eric Dumazet079096f2015-10-02 11:43:32 -0700979 if (!(idiag_states & ~TCPF_LISTEN))
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Eric Dumazet67db3e42016-11-04 11:54:32 -0700982#define SKARR_SZ 16
Eric Dumazetf373b532009-10-09 00:16:19 +0000983 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300984 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800985 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800986 struct hlist_nulls_node *node;
Eric Dumazet67db3e42016-11-04 11:54:32 -0700987 struct sock *sk_arr[SKARR_SZ];
988 int num_arr[SKARR_SZ];
989 int idx, accum, res;
Andi Kleen6be547a2008-08-28 01:09:54 -0700990
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700991 if (hlist_nulls_empty(&head->chain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700992 continue;
993
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (i > s_i)
995 s_num = 0;
996
Eric Dumazet67db3e42016-11-04 11:54:32 -0700997next_chunk:
998 num = 0;
999 accum = 0;
David S. Miller7e3aab42008-11-21 16:39:19 -08001000 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -08001001 sk_nulls_for_each(sk, node, &head->chain) {
Eric Dumazet67db3e42016-11-04 11:54:32 -07001002 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001004 if (!net_eq(sock_net(sk), net))
1005 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 if (num < s_num)
1007 goto next_normal;
Neal Cardwell70315d22014-01-10 15:34:45 -05001008 state = (sk->sk_state == TCP_TIME_WAIT) ?
1009 inet_twsk(sk)->tw_substate : sk->sk_state;
Eric Dumazet079096f2015-10-02 11:43:32 -07001010 if (!(idiag_states & (1 << state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 goto next_normal;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +00001012 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07001013 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +00001014 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07001015 if (r->id.idiag_sport != htons(sk->sk_num) &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001016 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -07001018 if (r->id.idiag_dport != sk->sk_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -08001019 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 goto next_normal;
Eric Dumazeta58917f2015-03-15 21:12:14 -07001021 twsk_build_assert();
1022
1023 if (!inet_diag_bc_sk(bc, sk))
1024 goto next_normal;
1025
Eric Dumazetf0c928d2018-12-20 15:28:56 -08001026 if (!refcount_inc_not_zero(&sk->sk_refcnt))
1027 goto next_normal;
1028
Eric Dumazet67db3e42016-11-04 11:54:32 -07001029 num_arr[accum] = num;
1030 sk_arr[accum] = sk;
1031 if (++accum == SKARR_SZ)
1032 break;
1033next_normal:
1034 ++num;
1035 }
1036 spin_unlock_bh(lock);
1037 res = 0;
1038 for (idx = 0; idx < accum; idx++) {
1039 if (res >= 0) {
Martin KaFai Lau5682d392020-02-25 15:04:09 -08001040 res = sk_diag_fill(sk_arr[idx], skb, cb, r,
1041 NLM_F_MULTI, net_admin);
Eric Dumazet67db3e42016-11-04 11:54:32 -07001042 if (res < 0)
1043 num = num_arr[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
Eric Dumazet67db3e42016-11-04 11:54:32 -07001045 sock_gen_put(sk_arr[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
Eric Dumazet67db3e42016-11-04 11:54:32 -07001047 if (res < 0)
1048 break;
Eric Dumazetacffb582016-03-14 15:40:00 -07001049 cond_resched();
Eric Dumazet67db3e42016-11-04 11:54:32 -07001050 if (accum == SKARR_SZ) {
1051 s_num = num + 1;
1052 goto next_chunk;
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 }
1055
1056done:
1057 cb->args[1] = i;
1058 cb->args[2] = num;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +00001059out:
1060 ;
1061}
Pavel Emelyanov1942c512011-12-09 06:23:18 +00001062EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +00001063
1064static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Martin KaFai Lau0df6d322020-02-25 15:04:15 -08001065 const struct inet_diag_req_v2 *r)
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +00001066{
1067 const struct inet_diag_handler *handler;
Martin KaFai Lau085c20c2020-02-25 15:04:27 -08001068 u32 prev_min_dump_alloc;
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +00001069 int err = 0;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +00001070
Martin KaFai Lau085c20c2020-02-25 15:04:27 -08001071again:
1072 prev_min_dump_alloc = cb->min_dump_alloc;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +00001073 handler = inet_diag_lock_handler(r->sdiag_protocol);
1074 if (!IS_ERR(handler))
Martin KaFai Lau0df6d322020-02-25 15:04:15 -08001075 handler->dump(skb, cb, r);
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +00001076 else
1077 err = PTR_ERR(handler);
Herbert Xud523a322007-12-03 15:51:25 +11001078 inet_diag_unlock_handler(handler);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +00001079
Martin KaFai Lau085c20c2020-02-25 15:04:27 -08001080 /* The skb is not large enough to fit one sk info and
1081 * inet_sk_diag_fill() has requested for a larger skb.
1082 */
1083 if (!skb->len && cb->min_dump_alloc > prev_min_dump_alloc) {
1084 err = pskb_expand_head(skb, 0, cb->min_dump_alloc, GFP_KERNEL);
1085 if (!err)
1086 goto again;
1087 }
1088
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +00001089 return err ? : skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001092static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
1093{
Martin KaFai Lau0df6d322020-02-25 15:04:15 -08001094 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh));
1095}
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001096
Martin KaFai Lau0df6d322020-02-25 15:04:15 -08001097static int __inet_diag_dump_start(struct netlink_callback *cb, int hdrlen)
1098{
1099 const struct nlmsghdr *nlh = cb->nlh;
1100 struct inet_diag_dump_data *cb_data;
1101 struct sk_buff *skb = cb->skb;
1102 struct nlattr *nla;
1103 int rem, err;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001104
Martin KaFai Lau0df6d322020-02-25 15:04:15 -08001105 cb_data = kzalloc(sizeof(*cb_data), GFP_KERNEL);
1106 if (!cb_data)
1107 return -ENOMEM;
1108
1109 nla_for_each_attr(nla, nlmsg_attrdata(nlh, hdrlen),
1110 nlmsg_attrlen(nlh, hdrlen), rem) {
1111 int type = nla_type(nla);
1112
1113 if (type < __INET_DIAG_REQ_MAX)
1114 cb_data->req_nlas[type] = nla;
1115 }
1116
1117 nla = cb_data->inet_diag_nla_bc;
1118 if (nla) {
1119 err = inet_diag_bc_audit(nla, skb);
1120 if (err) {
1121 kfree(cb_data);
1122 return err;
1123 }
1124 }
1125
Martin KaFai Lau085c20c2020-02-25 15:04:27 -08001126 nla = cb_data->inet_diag_nla_bpf_stgs;
1127 if (nla) {
1128 struct bpf_sk_storage_diag *bpf_stg_diag;
1129
1130 bpf_stg_diag = bpf_sk_storage_diag_alloc(nla);
1131 if (IS_ERR(bpf_stg_diag)) {
1132 kfree(cb_data);
1133 return PTR_ERR(bpf_stg_diag);
1134 }
1135 cb_data->bpf_stg_diag = bpf_stg_diag;
1136 }
1137
Martin KaFai Lau0df6d322020-02-25 15:04:15 -08001138 cb->data = cb_data;
1139 return 0;
1140}
1141
1142static int inet_diag_dump_start(struct netlink_callback *cb)
1143{
1144 return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req_v2));
1145}
1146
1147static int inet_diag_dump_start_compat(struct netlink_callback *cb)
1148{
1149 return __inet_diag_dump_start(cb, sizeof(struct inet_diag_req));
1150}
1151
1152static int inet_diag_dump_done(struct netlink_callback *cb)
1153{
Martin KaFai Lau085c20c2020-02-25 15:04:27 -08001154 struct inet_diag_dump_data *cb_data = cb->data;
1155
1156 bpf_sk_storage_diag_free(cb_data->bpf_stg_diag);
Martin KaFai Lau0df6d322020-02-25 15:04:15 -08001157 kfree(cb->data);
1158
1159 return 0;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001160}
1161
Eric Dumazete31c5e02015-03-10 07:15:53 -07001162static int inet_diag_type2proto(int type)
Pavel Emelyanova029fe22011-12-06 07:59:32 +00001163{
1164 switch (type) {
1165 case TCPDIAG_GETSOCK:
1166 return IPPROTO_TCP;
1167 case DCCPDIAG_GETSOCK:
1168 return IPPROTO_DCCP;
1169 default:
1170 return 0;
1171 }
1172}
1173
Eric Dumazete31c5e02015-03-10 07:15:53 -07001174static int inet_diag_dump_compat(struct sk_buff *skb,
1175 struct netlink_callback *cb)
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001176{
David S. Millerd1063522012-06-26 21:28:54 -07001177 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001178 struct inet_diag_req_v2 req;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001179
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +00001180 req.sdiag_family = AF_UNSPEC; /* compatibility */
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001181 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
1182 req.idiag_ext = rc->idiag_ext;
1183 req.idiag_states = rc->idiag_states;
1184 req.id = rc->id;
1185
Martin KaFai Lau0df6d322020-02-25 15:04:15 -08001186 return __inet_diag_dump(skb, cb, &req);
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001187}
1188
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001189static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -07001190 const struct nlmsghdr *nlh)
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001191{
David S. Millerd1063522012-06-26 21:28:54 -07001192 struct inet_diag_req *rc = nlmsg_data(nlh);
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001193 struct inet_diag_req_v2 req;
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001194
1195 req.sdiag_family = rc->idiag_family;
1196 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1197 req.idiag_ext = rc->idiag_ext;
1198 req.idiag_states = rc->idiag_states;
1199 req.id = rc->id;
1200
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001201 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001202}
1203
Pavel Emelyanov8d341722011-12-06 07:57:06 +00001204static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Pavel Emelyanov3b09c842012-01-10 22:37:26 +00001206 int hdrlen = sizeof(struct inet_diag_req);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001207 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Thomas Grafead592b2007-03-22 23:30:35 -07001209 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1210 nlmsg_len(nlh) < hdrlen)
1211 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
David S. Millerb8f3ab42011-01-18 12:40:38 -08001213 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Martin KaFai Lau0df6d322020-02-25 15:04:15 -08001214 struct netlink_dump_control c = {
1215 .start = inet_diag_dump_start_compat,
1216 .done = inet_diag_dump_done,
1217 .dump = inet_diag_dump_compat,
1218 };
1219 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Thomas Grafead592b2007-03-22 23:30:35 -07001221
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001222 return inet_diag_get_exact_compat(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223}
1224
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001225static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001226{
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001227 int hdrlen = sizeof(struct inet_diag_req_v2);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001228 struct net *net = sock_net(skb->sk);
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001229
1230 if (nlmsg_len(h) < hdrlen)
1231 return -EINVAL;
1232
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001233 if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
1234 h->nlmsg_flags & NLM_F_DUMP) {
Martin KaFai Lau0df6d322020-02-25 15:04:15 -08001235 struct netlink_dump_control c = {
1236 .start = inet_diag_dump_start,
1237 .done = inet_diag_dump_done,
1238 .dump = inet_diag_dump,
1239 };
1240 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001241 }
1242
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001243 return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001244}
1245
Craig Gallek35ac8382015-06-15 11:26:20 -04001246static
1247int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
1248{
1249 const struct inet_diag_handler *handler;
1250 struct nlmsghdr *nlh;
1251 struct nlattr *attr;
1252 struct inet_diag_msg *r;
1253 void *info = NULL;
1254 int err = 0;
1255
1256 nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
1257 if (!nlh)
1258 return -ENOMEM;
1259
1260 r = nlmsg_data(nlh);
1261 memset(r, 0, sizeof(*r));
1262 inet_diag_msg_common_fill(r, sk);
Craig Galleke0df02e2015-06-17 10:59:10 -04001263 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1264 r->id.idiag_sport = inet_sk(sk)->inet_sport;
Craig Gallek35ac8382015-06-15 11:26:20 -04001265 r->idiag_state = sk->sk_state;
1266
1267 if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
1268 nlmsg_cancel(skb, nlh);
1269 return err;
1270 }
1271
1272 handler = inet_diag_lock_handler(sk->sk_protocol);
1273 if (IS_ERR(handler)) {
1274 inet_diag_unlock_handler(handler);
1275 nlmsg_cancel(skb, nlh);
1276 return PTR_ERR(handler);
1277 }
1278
1279 attr = handler->idiag_info_size
Nicolas Dichtel6ed46d12016-04-26 10:06:14 +02001280 ? nla_reserve_64bit(skb, INET_DIAG_INFO,
1281 handler->idiag_info_size,
1282 INET_DIAG_PAD)
Craig Gallek35ac8382015-06-15 11:26:20 -04001283 : NULL;
1284 if (attr)
1285 info = nla_data(attr);
1286
1287 handler->idiag_get_info(sk, r, info);
1288 inet_diag_unlock_handler(handler);
1289
1290 nlmsg_end(skb, nlh);
1291 return 0;
1292}
1293
Shan Wei8dcf01f2012-04-24 18:21:07 +00001294static const struct sock_diag_handler inet_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001295 .family = AF_INET,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001296 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001297 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001298 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001299};
1300
Shan Wei8dcf01f2012-04-24 18:21:07 +00001301static const struct sock_diag_handler inet6_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001302 .family = AF_INET6,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001303 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001304 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001305 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001306};
1307
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001308int inet_diag_register(const struct inet_diag_handler *h)
1309{
1310 const __u16 type = h->idiag_type;
1311 int err = -EINVAL;
1312
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001313 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001314 goto out;
1315
Herbert Xud523a322007-12-03 15:51:25 +11001316 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001317 err = -EEXIST;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001318 if (!inet_diag_table[type]) {
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001319 inet_diag_table[type] = h;
1320 err = 0;
1321 }
Herbert Xud523a322007-12-03 15:51:25 +11001322 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001323out:
1324 return err;
1325}
1326EXPORT_SYMBOL_GPL(inet_diag_register);
1327
1328void inet_diag_unregister(const struct inet_diag_handler *h)
1329{
1330 const __u16 type = h->idiag_type;
1331
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001332 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001333 return;
1334
Herbert Xud523a322007-12-03 15:51:25 +11001335 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001336 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +11001337 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001338}
1339EXPORT_SYMBOL_GPL(inet_diag_unregister);
1340
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001341static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001343 const int inet_diag_table_size = (IPPROTO_MAX *
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001344 sizeof(struct inet_diag_handler *));
1345 int err = -ENOMEM;
1346
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001347 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001348 if (!inet_diag_table)
1349 goto out;
1350
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001351 err = sock_diag_register(&inet_diag_handler);
1352 if (err)
1353 goto out_free_nl;
1354
1355 err = sock_diag_register(&inet6_diag_handler);
1356 if (err)
1357 goto out_free_inet;
1358
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001359 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001360out:
1361 return err;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001362
1363out_free_inet:
1364 sock_diag_unregister(&inet_diag_handler);
1365out_free_nl:
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001366 kfree(inet_diag_table);
1367 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001370static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001372 sock_diag_unregister(&inet6_diag_handler);
1373 sock_diag_unregister(&inet_diag_handler);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001374 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001375 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001378module_init(inet_diag_init);
1379module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc622011-12-15 02:43:27 +00001381MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1382MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);