blob: 4e5bc4b2f14e6786ceb7d63e5902f8fc17819dfa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03002 * inet_diag.c Module for monitoring INET transport protocols sockets.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 * 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
Ilpo Järvinen172589c2007-08-28 15:50:33 -070012#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/fcntl.h>
16#include <linux/random.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090017#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/cache.h>
19#include <linux/init.h>
20#include <linux/time.h>
21
22#include <net/icmp.h>
23#include <net/tcp.h>
24#include <net/ipv6.h>
25#include <net/inet_common.h>
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -030026#include <net/inet_connection_sock.h>
27#include <net/inet_hashtables.h>
28#include <net/inet_timewait_sock.h>
29#include <net/inet6_hashtables.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070030#include <net/netlink.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
32#include <linux/inet.h>
33#include <linux/stddef.h>
34
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -030035#include <linux/inet_diag.h>
Pavel Emelyanovd3664772011-12-06 07:58:03 +000036#include <linux/sock_diag.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030038static const struct inet_diag_handler **inet_diag_table;
39
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030040struct inet_diag_entry {
Eric Dumazete31c5e02015-03-10 07:15:53 -070041 const __be32 *saddr;
42 const __be32 *daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 u16 sport;
44 u16 dport;
45 u16 family;
46 u16 userlocks;
David Ahern637c8412016-06-23 18:42:51 -070047 u32 ifindex;
Lorenzo Colittia52e95a2016-08-24 15:46:26 +090048 u32 mark;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049};
50
Herbert Xud523a322007-12-03 15:51:25 +110051static DEFINE_MUTEX(inet_diag_table_mutex);
52
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000053static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
Herbert Xud523a322007-12-03 15:51:25 +110054{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000055 if (!inet_diag_table[proto])
Xin Longbf2ae2e2018-03-10 18:57:50 +080056 sock_load_diag_module(AF_INET, proto);
Herbert Xud523a322007-12-03 15:51:25 +110057
58 mutex_lock(&inet_diag_table_mutex);
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000059 if (!inet_diag_table[proto])
Herbert Xud523a322007-12-03 15:51:25 +110060 return ERR_PTR(-ENOENT);
61
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000062 return inet_diag_table[proto];
Herbert Xud523a322007-12-03 15:51:25 +110063}
64
Eric Dumazete31c5e02015-03-10 07:15:53 -070065static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
Herbert Xud523a322007-12-03 15:51:25 +110066{
67 mutex_unlock(&inet_diag_table_mutex);
68}
69
Xin Longcb2050a2016-04-14 15:35:32 +080070void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
Eric Dumazeta4458342015-03-13 15:51:12 -070071{
72 r->idiag_family = sk->sk_family;
73
74 r->id.idiag_sport = htons(sk->sk_num);
75 r->id.idiag_dport = sk->sk_dport;
76 r->id.idiag_if = sk->sk_bound_dev_if;
77 sock_diag_save_cookie(sk, r->id.idiag_cookie);
78
79#if IS_ENABLED(CONFIG_IPV6)
80 if (sk->sk_family == AF_INET6) {
81 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
82 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
83 } else
84#endif
85 {
86 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
87 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
88
89 r->id.idiag_src[0] = sk->sk_rcv_saddr;
90 r->id.idiag_dst[0] = sk->sk_daddr;
91 }
92}
Xin Longcb2050a2016-04-14 15:35:32 +080093EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill);
Eric Dumazeta4458342015-03-13 15:51:12 -070094
Ivan Delalandeb37e8842017-08-31 09:59:38 -070095static size_t inet_sk_attr_size(struct sock *sk,
96 const struct inet_diag_req_v2 *req,
97 bool net_admin)
Eric Dumazetc8e2c802015-03-13 09:49:59 -070098{
Ivan Delalandeb37e8842017-08-31 09:59:38 -070099 const struct inet_diag_handler *handler;
100 size_t aux = 0;
101
102 handler = inet_diag_table[req->sdiag_protocol];
103 if (handler && handler->idiag_get_aux_size)
104 aux = handler->idiag_get_aux_size(sk, net_admin);
105
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700106 return nla_total_size(sizeof(struct tcp_info))
107 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
108 + nla_total_size(1) /* INET_DIAG_TOS */
109 + nla_total_size(1) /* INET_DIAG_TCLASS */
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900110 + nla_total_size(4) /* INET_DIAG_MARK */
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700111 + nla_total_size(sizeof(struct inet_diag_meminfo))
112 + nla_total_size(sizeof(struct inet_diag_msg))
113 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
114 + nla_total_size(TCP_CA_NAME_MAX)
115 + nla_total_size(sizeof(struct tcpvegas_info))
Ivan Delalandeb37e8842017-08-31 09:59:38 -0700116 + aux
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700117 + 64;
118}
119
Xin Longcb2050a2016-04-14 15:35:32 +0800120int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
121 struct inet_diag_msg *r, int ext,
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900122 struct user_namespace *user_ns,
123 bool net_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700125 const struct inet_sock *inet = inet_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126
Pavel Emelyanove4e541a2012-10-23 22:29:56 +0400127 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
128 goto errout;
129
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500130 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
131 * hence this needs to be included regardless of socket family.
132 */
133 if (ext & (1 << (INET_DIAG_TOS - 1)))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000134 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
135 goto errout;
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500136
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000137#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300138 if (r->idiag_family == AF_INET6) {
Maciej Żenczykowski06236ac2011-11-07 14:23:11 +0000139 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
Eric Dumazetefe42082013-10-03 15:42:29 -0700140 if (nla_put_u8(skb, INET_DIAG_TCLASS,
141 inet6_sk(sk)->tclass) < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000142 goto errout;
Phil Sutter20462152015-06-24 11:02:51 +0200143
Phil Sutter8220ea22015-07-10 11:39:57 +0200144 if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
145 nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
Phil Sutter20462152015-06-24 11:02:51 +0200146 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148#endif
149
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900150 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK, sk->sk_mark))
151 goto errout;
152
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600153 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000154 r->idiag_inode = sock_i_ino(sk);
155
Xin Longcb2050a2016-04-14 15:35:32 +0800156 return 0;
157errout:
158 return 1;
159}
160EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
161
162int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
163 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
164 struct user_namespace *user_ns,
165 u32 portid, u32 seq, u16 nlmsg_flags,
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900166 const struct nlmsghdr *unlh,
167 bool net_admin)
Xin Longcb2050a2016-04-14 15:35:32 +0800168{
169 const struct tcp_congestion_ops *ca_ops;
170 const struct inet_diag_handler *handler;
171 int ext = req->idiag_ext;
172 struct inet_diag_msg *r;
173 struct nlmsghdr *nlh;
174 struct nlattr *attr;
175 void *info = NULL;
176
177 handler = inet_diag_table[req->sdiag_protocol];
178 BUG_ON(!handler);
179
180 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
181 nlmsg_flags);
182 if (!nlh)
183 return -EMSGSIZE;
184
185 r = nlmsg_data(nlh);
186 BUG_ON(!sk_fullsock(sk));
187
188 inet_diag_msg_common_fill(r, sk);
189 r->idiag_state = sk->sk_state;
190 r->idiag_timer = 0;
191 r->idiag_retrans = 0;
192
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900193 if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin))
Xin Longcb2050a2016-04-14 15:35:32 +0800194 goto errout;
195
Thomas Graf6e277ed2012-06-26 23:36:12 +0000196 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
197 struct inet_diag_meminfo minfo = {
198 .idiag_rmem = sk_rmem_alloc_get(sk),
199 .idiag_wmem = sk->sk_wmem_queued,
200 .idiag_fmem = sk->sk_forward_alloc,
201 .idiag_tmem = sk_wmem_alloc_get(sk),
202 };
203
204 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
205 goto errout;
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000206 }
207
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000208 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
209 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000210 goto errout;
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000211
Cyrill Gorcunov432490f2016-10-21 13:03:44 +0300212 /*
213 * RAW sockets might have user-defined protocols assigned,
214 * so report the one supplied on socket creation.
215 */
216 if (sk->sk_type == SOCK_RAW) {
217 if (nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))
218 goto errout;
219 }
220
Eric Dumazete31c5e02015-03-10 07:15:53 -0700221 if (!icsk) {
Shan Wei62ad6fc2012-04-24 18:15:41 +0000222 handler->idiag_get_info(sk, r, NULL);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000223 goto out;
224 }
225
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000226 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
Yuchung Cheng57dde7f2017-01-12 22:11:33 -0800227 icsk->icsk_pending == ICSK_TIME_REO_TIMEOUT ||
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000228 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300229 r->idiag_timer = 1;
230 r->idiag_retrans = icsk->icsk_retransmits;
Xin Longb7de5292016-04-19 15:10:01 +0800231 r->idiag_expires =
232 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700233 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300234 r->idiag_timer = 4;
235 r->idiag_retrans = icsk->icsk_probes_out;
Xin Longb7de5292016-04-19 15:10:01 +0800236 r->idiag_expires =
237 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300239 r->idiag_timer = 2;
240 r->idiag_retrans = icsk->icsk_probes_out;
Xin Longb7de5292016-04-19 15:10:01 +0800241 r->idiag_expires =
242 jiffies_to_msecs(sk->sk_timer.expires - jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300244 r->idiag_timer = 0;
245 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300247
Craig Gallek3fd22af2015-06-15 11:26:19 -0400248 if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
Nicolas Dichtel6ed46d12016-04-26 10:06:14 +0200249 attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
250 handler->idiag_info_size,
251 INET_DIAG_PAD);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000252 if (!attr)
253 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Thomas Graf6e277ed2012-06-26 23:36:12 +0000255 info = nla_data(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 }
257
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700258 if (ext & (1 << (INET_DIAG_CONG - 1))) {
259 int err = 0;
260
261 rcu_read_lock();
262 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
263 if (ca_ops)
264 err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
265 rcu_read_unlock();
266 if (err < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000267 goto errout;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700268 }
Thomas Graf6e277ed2012-06-26 23:36:12 +0000269
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300270 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Ivan Delalandeb37e8842017-08-31 09:59:38 -0700272 if (ext & (1 << (INET_DIAG_INFO - 1)) && handler->idiag_get_aux)
273 if (handler->idiag_get_aux(sk, net_admin, skb) < 0)
274 goto errout;
275
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700276 if (sk->sk_state < TCP_TIME_WAIT) {
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700277 union tcp_cc_info info;
278 size_t sz = 0;
279 int attr;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700280
281 rcu_read_lock();
282 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
283 if (ca_ops && ca_ops->get_info)
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700284 sz = ca_ops->get_info(sk, ext, &attr, &info);
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700285 rcu_read_unlock();
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700286 if (sz && nla_put(skb, attr, sz, &info) < 0)
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700287 goto errout;
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
Levin, Alexander (Sasha Levin)0888e372017-08-17 00:35:11 +0000290 if (ext & (1 << (INET_DIAG_CLASS_ID - 1))) {
291 u32 classid = 0;
292
293#ifdef CONFIG_SOCK_CGROUP_DATA
294 classid = sock_cgroup_classid(&sk->sk_cgrp_data);
295#endif
296
297 if (nla_put_u32(skb, INET_DIAG_CLASS_ID, classid))
298 goto errout;
299 }
300
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000301out:
Johannes Berg053c0952015-01-16 22:09:00 +0100302 nlmsg_end(skb, nlh);
303 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Thomas Graf6e277ed2012-06-26 23:36:12 +0000305errout:
306 nlmsg_cancel(skb, nlh);
Patrick McHardy26932562007-01-31 23:16:40 -0800307 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000309EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
310
311static int inet_csk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700312 struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700313 const struct inet_diag_req_v2 *req,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600314 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000315 u32 portid, u32 seq, u16 nlmsg_flags,
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900316 const struct nlmsghdr *unlh,
317 bool net_admin)
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000318{
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900319 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req, user_ns,
320 portid, seq, nlmsg_flags, unlh, net_admin);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000321}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700323static int inet_twsk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700324 struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000325 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800326 const struct nlmsghdr *unlh)
327{
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700328 struct inet_timewait_sock *tw = inet_twsk(sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800329 struct inet_diag_msg *r;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000330 struct nlmsghdr *nlh;
Eric Dumazet789f5582015-04-12 18:51:09 -0700331 long tmo;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800332
Eric W. Biederman15e47302012-09-07 20:12:54 +0000333 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000334 nlmsg_flags);
335 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700336 return -EMSGSIZE;
David S. Millerd1063522012-06-26 21:28:54 -0700337
338 r = nlmsg_data(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800339 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
340
Eric Dumazet789f5582015-04-12 18:51:09 -0700341 tmo = tw->tw_timer.expires - jiffies;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800342 if (tmo < 0)
343 tmo = 0;
344
Eric Dumazeta4458342015-03-13 15:51:12 -0700345 inet_diag_msg_common_fill(r, sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800346 r->idiag_retrans = 0;
Daniel Borkmannb1aac812013-12-17 00:38:39 +0100347
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800348 r->idiag_state = tw->tw_substate;
349 r->idiag_timer = 3;
Eric Dumazet96f817f2013-10-03 14:27:25 -0700350 r->idiag_expires = jiffies_to_msecs(tmo);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800351 r->idiag_rqueue = 0;
352 r->idiag_wqueue = 0;
353 r->idiag_uid = 0;
354 r->idiag_inode = 0;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000355
Johannes Berg053c0952015-01-16 22:09:00 +0100356 nlmsg_end(skb, nlh);
357 return 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800358}
359
Eric Dumazeta58917f2015-03-15 21:12:14 -0700360static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
361 u32 portid, u32 seq, u16 nlmsg_flags,
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900362 const struct nlmsghdr *unlh, bool net_admin)
Eric Dumazeta58917f2015-03-15 21:12:14 -0700363{
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900364 struct request_sock *reqsk = inet_reqsk(sk);
Eric Dumazeta58917f2015-03-15 21:12:14 -0700365 struct inet_diag_msg *r;
366 struct nlmsghdr *nlh;
367 long tmo;
368
369 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
370 nlmsg_flags);
371 if (!nlh)
372 return -EMSGSIZE;
373
374 r = nlmsg_data(nlh);
375 inet_diag_msg_common_fill(r, sk);
376 r->idiag_state = TCP_SYN_RECV;
377 r->idiag_timer = 1;
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900378 r->idiag_retrans = reqsk->num_retrans;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700379
380 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
381 offsetof(struct sock, sk_cookie));
382
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700383 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700384 r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
385 r->idiag_rqueue = 0;
386 r->idiag_wqueue = 0;
387 r->idiag_uid = 0;
388 r->idiag_inode = 0;
389
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900390 if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
391 inet_rsk(reqsk)->ir_mark))
392 return -EMSGSIZE;
393
Eric Dumazeta58917f2015-03-15 21:12:14 -0700394 nlmsg_end(skb, nlh);
395 return 0;
396}
397
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800398static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700399 const struct inet_diag_req_v2 *r,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600400 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000401 u32 portid, u32 seq, u16 nlmsg_flags,
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900402 const struct nlmsghdr *unlh, bool net_admin)
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800403{
404 if (sk->sk_state == TCP_TIME_WAIT)
Eric Dumazeta58917f2015-03-15 21:12:14 -0700405 return inet_twsk_diag_fill(sk, skb, portid, seq,
Eric Dumazetefe42082013-10-03 15:42:29 -0700406 nlmsg_flags, unlh);
407
Eric Dumazeta58917f2015-03-15 21:12:14 -0700408 if (sk->sk_state == TCP_NEW_SYN_RECV)
409 return inet_req_diag_fill(sk, skb, portid, seq,
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900410 nlmsg_flags, unlh, net_admin);
Eric Dumazeta58917f2015-03-15 21:12:14 -0700411
Eric Dumazetefe42082013-10-03 15:42:29 -0700412 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900413 nlmsg_flags, unlh, net_admin);
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800414}
415
Lorenzo Colittib613f562015-12-16 12:30:02 +0900416struct sock *inet_diag_find_one_icsk(struct net *net,
417 struct inet_hashinfo *hashinfo,
418 const struct inet_diag_req_v2 *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700420 struct sock *sk;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300421
Eric Dumazet2d331912016-04-01 08:52:15 -0700422 rcu_read_lock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700423 if (req->sdiag_family == AF_INET)
Craig Galleka5836362016-02-10 11:50:38 -0500424 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300425 req->id.idiag_dport, req->id.idiag_src[0],
426 req->id.idiag_sport, req->id.idiag_if);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000427#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet7c130672016-01-20 16:25:01 -0800428 else if (req->sdiag_family == AF_INET6) {
429 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
430 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
Craig Galleka5836362016-02-10 11:50:38 -0500431 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
Eric Dumazet7c130672016-01-20 16:25:01 -0800432 req->id.idiag_dport, req->id.idiag_src[3],
433 req->id.idiag_sport, req->id.idiag_if);
434 else
Craig Galleka5836362016-02-10 11:50:38 -0500435 sk = inet6_lookup(net, hashinfo, NULL, 0,
Eric Dumazet7c130672016-01-20 16:25:01 -0800436 (struct in6_addr *)req->id.idiag_dst,
437 req->id.idiag_dport,
438 (struct in6_addr *)req->id.idiag_src,
439 req->id.idiag_sport,
440 req->id.idiag_if);
441 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442#endif
Eric Dumazet2d331912016-04-01 08:52:15 -0700443 else {
444 rcu_read_unlock();
Lorenzo Colittib613f562015-12-16 12:30:02 +0900445 return ERR_PTR(-EINVAL);
Eric Dumazet2d331912016-04-01 08:52:15 -0700446 }
447 rcu_read_unlock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700448 if (!sk)
Lorenzo Colittib613f562015-12-16 12:30:02 +0900449 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Lorenzo Colittib613f562015-12-16 12:30:02 +0900451 if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
452 sock_gen_put(sk);
453 return ERR_PTR(-ENOENT);
454 }
455
456 return sk;
457}
458EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
459
460int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
461 struct sk_buff *in_skb,
462 const struct nlmsghdr *nlh,
463 const struct inet_diag_req_v2 *req)
464{
Ivan Delalandeb37e8842017-08-31 09:59:38 -0700465 bool net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN);
Lorenzo Colittib613f562015-12-16 12:30:02 +0900466 struct net *net = sock_net(in_skb->sk);
467 struct sk_buff *rep;
468 struct sock *sk;
469 int err;
470
471 sk = inet_diag_find_one_icsk(net, hashinfo, req);
472 if (IS_ERR(sk))
473 return PTR_ERR(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Ivan Delalandeb37e8842017-08-31 09:59:38 -0700475 rep = nlmsg_new(inet_sk_attr_size(sk, req, net_admin), GFP_KERNEL);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000476 if (!rep) {
477 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 goto out;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000479 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000481 err = sk_diag_fill(sk, rep, req,
Patrick McHardye32123e2013-04-17 06:46:57 +0000482 sk_user_ns(NETLINK_CB(in_skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000483 NETLINK_CB(in_skb).portid,
Ivan Delalandeb37e8842017-08-31 09:59:38 -0700484 nlh->nlmsg_seq, 0, nlh, net_admin);
Patrick McHardy26932562007-01-31 23:16:40 -0800485 if (err < 0) {
486 WARN_ON(err == -EMSGSIZE);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000487 nlmsg_free(rep);
Patrick McHardy26932562007-01-31 23:16:40 -0800488 goto out;
489 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000490 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300491 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (err > 0)
493 err = 0;
494
495out:
Eric Dumazetc1d607c2013-10-11 08:54:49 -0700496 if (sk)
497 sock_gen_put(sk);
498
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000499 return err;
500}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000501EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000502
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900503static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000504 const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700505 const struct inet_diag_req_v2 *req)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000506{
507 const struct inet_diag_handler *handler;
508 int err;
509
510 handler = inet_diag_lock_handler(req->sdiag_protocol);
511 if (IS_ERR(handler))
512 err = PTR_ERR(handler);
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900513 else if (cmd == SOCK_DIAG_BY_FAMILY)
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000514 err = handler->dump_one(in_skb, nlh, req);
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900515 else if (cmd == SOCK_DESTROY && handler->destroy)
516 err = handler->destroy(in_skb, req);
517 else
518 err = -EOPNOTSUPP;
Herbert Xud523a322007-12-03 15:51:25 +1100519 inet_diag_unlock_handler(handler);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return err;
522}
523
Al Viro9f855292006-09-27 18:44:30 -0700524static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 int words = bits >> 5;
527
528 bits &= 0x1f;
529
530 if (words) {
531 if (memcmp(a1, a2, words << 2))
532 return 0;
533 }
534 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700535 __be32 w1, w2;
536 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 w1 = a1[words];
539 w2 = a2[words];
540
541 mask = htonl((0xffffffff) << (32 - bits));
542
543 if ((w1 ^ w2) & mask)
544 return 0;
545 }
546
547 return 1;
548}
549
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000550static int inet_diag_bc_run(const struct nlattr *_bc,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700551 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552{
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000553 const void *bc = nla_data(_bc);
554 int len = nla_len(_bc);
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 while (len > 0) {
557 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300558 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
560 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300561 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300563 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 yes = 0;
565 break;
Kristian Evensenbbb61892017-12-27 18:27:58 +0100566 case INET_DIAG_BC_S_EQ:
567 yes = entry->sport == op[1].no;
568 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300569 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 yes = entry->sport >= op[1].no;
571 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300572 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800573 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 break;
Kristian Evensenbbb61892017-12-27 18:27:58 +0100575 case INET_DIAG_BC_D_EQ:
576 yes = entry->dport == op[1].no;
577 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300578 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 yes = entry->dport >= op[1].no;
580 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300581 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 yes = entry->dport <= op[1].no;
583 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300584 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
586 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300587 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300588 case INET_DIAG_BC_D_COND: {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700589 const struct inet_diag_hostcond *cond;
590 const __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Eric Dumazete31c5e02015-03-10 07:15:53 -0700592 cond = (const struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300594 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 entry->sport : entry->dport)) {
596 yes = 0;
597 break;
598 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800599
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300600 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 addr = entry->saddr;
602 else
603 addr = entry->daddr;
604
Neal Cardwellf67caec2012-12-08 19:43:23 +0000605 if (cond->family != AF_UNSPEC &&
606 cond->family != entry->family) {
607 if (entry->family == AF_INET6 &&
608 cond->family == AF_INET) {
609 if (addr[0] == 0 && addr[1] == 0 &&
610 addr[2] == htonl(0xffff) &&
611 bitstring_match(addr + 3,
612 cond->addr,
613 cond->prefix_len))
614 break;
615 }
616 yes = 0;
617 break;
618 }
619
620 if (cond->prefix_len == 0)
621 break;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800622 if (bitstring_match(addr, cond->addr,
623 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 yes = 0;
626 break;
627 }
David Ahern637c8412016-06-23 18:42:51 -0700628 case INET_DIAG_BC_DEV_COND: {
629 u32 ifindex;
630
631 ifindex = *((const u32 *)(op + 1));
632 if (ifindex != entry->ifindex)
633 yes = 0;
634 break;
635 }
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900636 case INET_DIAG_BC_MARK_COND: {
637 struct inet_diag_markcond *cond;
638
639 cond = (struct inet_diag_markcond *)(op + 1);
640 if ((entry->mark & cond->mask) != cond->mark)
641 yes = 0;
642 break;
643 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800646 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 len -= op->yes;
648 bc += op->yes;
649 } else {
650 len -= op->no;
651 bc += op->no;
652 }
653 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000654 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
Eric Dumazeta4458342015-03-13 15:51:12 -0700657/* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
658 */
659static void entry_fill_addrs(struct inet_diag_entry *entry,
660 const struct sock *sk)
661{
662#if IS_ENABLED(CONFIG_IPV6)
663 if (sk->sk_family == AF_INET6) {
664 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
665 entry->daddr = sk->sk_v6_daddr.s6_addr32;
666 } else
667#endif
668 {
669 entry->saddr = &sk->sk_rcv_saddr;
670 entry->daddr = &sk->sk_daddr;
671 }
672}
673
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000674int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
675{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000676 struct inet_sock *inet = inet_sk(sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700677 struct inet_diag_entry entry;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000678
Eric Dumazete31c5e02015-03-10 07:15:53 -0700679 if (!bc)
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000680 return 1;
681
682 entry.family = sk->sk_family;
Eric Dumazeta4458342015-03-13 15:51:12 -0700683 entry_fill_addrs(&entry, sk);
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000684 entry.sport = inet->inet_num;
685 entry.dport = ntohs(inet->inet_dport);
David Ahern637c8412016-06-23 18:42:51 -0700686 entry.ifindex = sk->sk_bound_dev_if;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700687 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900688 if (sk_fullsock(sk))
689 entry.mark = sk->sk_mark;
690 else if (sk->sk_state == TCP_NEW_SYN_RECV)
691 entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
692 else
693 entry.mark = 0;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000694
695 return inet_diag_bc_run(bc, &entry);
696}
697EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699static int valid_cc(const void *bc, int len, int cc)
700{
701 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300702 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 if (cc > len)
705 return 0;
706 if (cc == len)
707 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400708 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 return 0;
710 len -= op->yes;
711 bc += op->yes;
712 }
713 return 0;
714}
715
David Ahern637c8412016-06-23 18:42:51 -0700716/* data is u32 ifindex */
717static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
718 int *min_len)
719{
720 /* Check ifindex space. */
721 *min_len += sizeof(u32);
722 if (len < *min_len)
723 return false;
724
725 return true;
726}
Neal Cardwell405c0052012-12-08 19:43:22 +0000727/* Validate an inet_diag_hostcond. */
728static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
729 int *min_len)
730{
Neal Cardwell405c0052012-12-08 19:43:22 +0000731 struct inet_diag_hostcond *cond;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700732 int addr_len;
Neal Cardwell405c0052012-12-08 19:43:22 +0000733
734 /* Check hostcond space. */
735 *min_len += sizeof(struct inet_diag_hostcond);
736 if (len < *min_len)
737 return false;
738 cond = (struct inet_diag_hostcond *)(op + 1);
739
740 /* Check address family and address length. */
741 switch (cond->family) {
742 case AF_UNSPEC:
743 addr_len = 0;
744 break;
745 case AF_INET:
746 addr_len = sizeof(struct in_addr);
747 break;
748 case AF_INET6:
749 addr_len = sizeof(struct in6_addr);
750 break;
751 default:
752 return false;
753 }
754 *min_len += addr_len;
755 if (len < *min_len)
756 return false;
757
758 /* Check prefix length (in bits) vs address length (in bytes). */
759 if (cond->prefix_len > 8 * addr_len)
760 return false;
761
762 return true;
763}
764
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000765/* Validate a port comparison operator. */
Eric Dumazete31c5e02015-03-10 07:15:53 -0700766static bool valid_port_comparison(const struct inet_diag_bc_op *op,
767 int len, int *min_len)
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000768{
769 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
770 *min_len += sizeof(struct inet_diag_bc_op);
771 if (len < *min_len)
772 return false;
773 return true;
774}
775
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900776static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
777 int *min_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900779 *min_len += sizeof(struct inet_diag_markcond);
780 return len >= *min_len;
781}
782
783static int inet_diag_bc_audit(const struct nlattr *attr,
784 const struct sk_buff *skb)
785{
786 bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN);
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +0900787 const void *bytecode, *bc;
788 int bytecode_len, len;
789
790 if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
791 return -EINVAL;
792
793 bytecode = bc = nla_data(attr);
794 len = bytecode_len = nla_len(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 while (len > 0) {
Neal Cardwell405c0052012-12-08 19:43:22 +0000797 int min_len = sizeof(struct inet_diag_bc_op);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700798 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300801 case INET_DIAG_BC_S_COND:
802 case INET_DIAG_BC_D_COND:
Neal Cardwell405c0052012-12-08 19:43:22 +0000803 if (!valid_hostcond(bc, len, &min_len))
804 return -EINVAL;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000805 break;
David Ahern637c8412016-06-23 18:42:51 -0700806 case INET_DIAG_BC_DEV_COND:
807 if (!valid_devcond(bc, len, &min_len))
808 return -EINVAL;
809 break;
Kristian Evensenbbb61892017-12-27 18:27:58 +0100810 case INET_DIAG_BC_S_EQ:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300811 case INET_DIAG_BC_S_GE:
812 case INET_DIAG_BC_S_LE:
Kristian Evensenbbb61892017-12-27 18:27:58 +0100813 case INET_DIAG_BC_D_EQ:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300814 case INET_DIAG_BC_D_GE:
815 case INET_DIAG_BC_D_LE:
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000816 if (!valid_port_comparison(bc, len, &min_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return -EINVAL;
818 break;
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900819 case INET_DIAG_BC_MARK_COND:
820 if (!net_admin)
821 return -EPERM;
822 if (!valid_markcond(bc, len, &min_len))
823 return -EINVAL;
824 break;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000825 case INET_DIAG_BC_AUTO:
826 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300827 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 break;
829 default:
830 return -EINVAL;
831 }
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000832
833 if (op->code != INET_DIAG_BC_NOP) {
834 if (op->no < min_len || op->no > len + 4 || op->no & 3)
835 return -EINVAL;
836 if (op->no < len &&
837 !valid_cc(bytecode, bytecode_len, len - op->no))
838 return -EINVAL;
839 }
840
Neal Cardwell405c0052012-12-08 19:43:22 +0000841 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
Eric Dumazeteeb14972011-06-17 16:25:39 -0400842 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800843 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 len -= op->yes;
845 }
846 return len == 0 ? 0 : -EINVAL;
847}
848
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800849static int inet_csk_diag_dump(struct sock *sk,
850 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000851 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700852 const struct inet_diag_req_v2 *r,
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900853 const struct nlattr *bc,
854 bool net_admin)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000856 if (!inet_diag_bc_sk(bc, sk))
857 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000859 return inet_csk_diag_fill(sk, skb, r,
Patrick McHardye32123e2013-04-17 06:46:57 +0000860 sk_user_ns(NETLINK_CB(cb->skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000861 NETLINK_CB(cb->skb).portid,
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900862 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh,
863 net_admin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864}
865
Eric Dumazet49612722015-03-05 10:18:14 -0800866static void twsk_build_assert(void)
867{
868 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
869 offsetof(struct sock, sk_family));
870
871 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
872 offsetof(struct inet_sock, inet_num));
873
874 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
875 offsetof(struct inet_sock, inet_dport));
876
877 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
878 offsetof(struct inet_sock, inet_rcv_saddr));
879
880 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
881 offsetof(struct inet_sock, inet_daddr));
882
883#if IS_ENABLED(CONFIG_IPV6)
884 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
885 offsetof(struct sock, sk_v6_rcv_saddr));
886
887 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
888 offsetof(struct sock, sk_v6_daddr));
889#endif
890}
891
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000892void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700893 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700894 const struct inet_diag_req_v2 *r, struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895{
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900896 bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
Eric Dumazet67db3e42016-11-04 11:54:32 -0700897 struct net *net = sock_net(skb->sk);
898 u32 idiag_states = r->idiag_states;
899 int i, num, s_i, s_num;
900 struct sock *sk;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800901
Eric Dumazet079096f2015-10-02 11:43:32 -0700902 if (idiag_states & TCPF_SYN_RECV)
903 idiag_states |= TCPF_NEW_SYN_RECV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 s_i = cb->args[1];
905 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300906
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 if (cb->args[0] == 0) {
Eric Dumazet9652dc22016-10-19 21:24:58 -0700908 if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300910
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700911 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800912 struct inet_listen_hashbucket *ilb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913
914 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800915 ilb = &hashinfo->listening_hash[i];
Eric Dumazet9652dc22016-10-19 21:24:58 -0700916 spin_lock(&ilb->lock);
Eric Dumazet3b24d852016-04-01 08:52:17 -0700917 sk_for_each(sk, &ilb->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 struct inet_sock *inet = inet_sk(sk);
919
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000920 if (!net_eq(sock_net(sk), net))
921 continue;
922
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (num < s_num) {
924 num++;
925 continue;
926 }
927
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000928 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazete31c5e02015-03-10 07:15:53 -0700929 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000930 goto next_listen;
931
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000932 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300933 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 goto next_listen;
935
Lorenzo Colittid545cac2016-09-08 00:42:25 +0900936 if (inet_csk_diag_dump(sk, skb, cb, r,
937 bc, net_admin) < 0) {
Eric Dumazet9652dc22016-10-19 21:24:58 -0700938 spin_unlock(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 goto done;
940 }
941
942next_listen:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 ++num;
944 }
Eric Dumazet9652dc22016-10-19 21:24:58 -0700945 spin_unlock(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
947 s_num = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949skip_listen_ht:
950 cb->args[0] = 1;
951 s_i = num = s_num = 0;
952 }
953
Eric Dumazet079096f2015-10-02 11:43:32 -0700954 if (!(idiag_states & ~TCPF_LISTEN))
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000955 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Eric Dumazet67db3e42016-11-04 11:54:32 -0700957#define SKARR_SZ 16
Eric Dumazetf373b532009-10-09 00:16:19 +0000958 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300959 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800960 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800961 struct hlist_nulls_node *node;
Eric Dumazet67db3e42016-11-04 11:54:32 -0700962 struct sock *sk_arr[SKARR_SZ];
963 int num_arr[SKARR_SZ];
964 int idx, accum, res;
Andi Kleen6be547a2008-08-28 01:09:54 -0700965
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700966 if (hlist_nulls_empty(&head->chain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700967 continue;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (i > s_i)
970 s_num = 0;
971
Eric Dumazet67db3e42016-11-04 11:54:32 -0700972next_chunk:
973 num = 0;
974 accum = 0;
David S. Miller7e3aab42008-11-21 16:39:19 -0800975 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800976 sk_nulls_for_each(sk, node, &head->chain) {
Eric Dumazet67db3e42016-11-04 11:54:32 -0700977 int state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000979 if (!net_eq(sock_net(sk), net))
980 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (num < s_num)
982 goto next_normal;
Neal Cardwell70315d22014-01-10 15:34:45 -0500983 state = (sk->sk_state == TCP_TIME_WAIT) ?
984 inet_twsk(sk)->tw_substate : sk->sk_state;
Eric Dumazet079096f2015-10-02 11:43:32 -0700985 if (!(idiag_states & (1 << state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 goto next_normal;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000987 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700988 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000989 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700990 if (r->id.idiag_sport != htons(sk->sk_num) &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300991 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700993 if (r->id.idiag_dport != sk->sk_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800994 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 goto next_normal;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700996 twsk_build_assert();
997
998 if (!inet_diag_bc_sk(bc, sk))
999 goto next_normal;
1000
Eric Dumazet67db3e42016-11-04 11:54:32 -07001001 sock_hold(sk);
1002 num_arr[accum] = num;
1003 sk_arr[accum] = sk;
1004 if (++accum == SKARR_SZ)
1005 break;
1006next_normal:
1007 ++num;
1008 }
1009 spin_unlock_bh(lock);
1010 res = 0;
1011 for (idx = 0; idx < accum; idx++) {
1012 if (res >= 0) {
1013 res = sk_diag_fill(sk_arr[idx], skb, r,
Eric Dumazeta58917f2015-03-15 21:12:14 -07001014 sk_user_ns(NETLINK_CB(cb->skb).sk),
1015 NETLINK_CB(cb->skb).portid,
1016 cb->nlh->nlmsg_seq, NLM_F_MULTI,
Lorenzo Colittid545cac2016-09-08 00:42:25 +09001017 cb->nlh, net_admin);
Eric Dumazet67db3e42016-11-04 11:54:32 -07001018 if (res < 0)
1019 num = num_arr[idx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
Eric Dumazet67db3e42016-11-04 11:54:32 -07001021 sock_gen_put(sk_arr[idx]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
Eric Dumazet67db3e42016-11-04 11:54:32 -07001023 if (res < 0)
1024 break;
Eric Dumazetacffb582016-03-14 15:40:00 -07001025 cond_resched();
Eric Dumazet67db3e42016-11-04 11:54:32 -07001026 if (accum == SKARR_SZ) {
1027 s_num = num + 1;
1028 goto next_chunk;
1029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
1031
1032done:
1033 cb->args[1] = i;
1034 cb->args[2] = num;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +00001035out:
1036 ;
1037}
Pavel Emelyanov1942c512011-12-09 06:23:18 +00001038EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +00001039
1040static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -07001041 const struct inet_diag_req_v2 *r,
Eric Dumazete31c5e02015-03-10 07:15:53 -07001042 struct nlattr *bc)
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +00001043{
1044 const struct inet_diag_handler *handler;
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +00001045 int err = 0;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +00001046
1047 handler = inet_diag_lock_handler(r->sdiag_protocol);
1048 if (!IS_ERR(handler))
Pavel Emelyanov1942c512011-12-09 06:23:18 +00001049 handler->dump(skb, cb, r, bc);
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +00001050 else
1051 err = PTR_ERR(handler);
Herbert Xud523a322007-12-03 15:51:25 +11001052 inet_diag_unlock_handler(handler);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +00001053
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +00001054 return err ? : skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055}
1056
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001057static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
1058{
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001059 int hdrlen = sizeof(struct inet_diag_req_v2);
Eric Dumazete31c5e02015-03-10 07:15:53 -07001060 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001061
1062 if (nlmsg_attrlen(cb->nlh, hdrlen))
1063 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1064
David S. Millerd1063522012-06-26 21:28:54 -07001065 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001066}
1067
Eric Dumazete31c5e02015-03-10 07:15:53 -07001068static int inet_diag_type2proto(int type)
Pavel Emelyanova029fe22011-12-06 07:59:32 +00001069{
1070 switch (type) {
1071 case TCPDIAG_GETSOCK:
1072 return IPPROTO_TCP;
1073 case DCCPDIAG_GETSOCK:
1074 return IPPROTO_DCCP;
1075 default:
1076 return 0;
1077 }
1078}
1079
Eric Dumazete31c5e02015-03-10 07:15:53 -07001080static int inet_diag_dump_compat(struct sk_buff *skb,
1081 struct netlink_callback *cb)
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001082{
David S. Millerd1063522012-06-26 21:28:54 -07001083 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
Eric Dumazete31c5e02015-03-10 07:15:53 -07001084 int hdrlen = sizeof(struct inet_diag_req);
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001085 struct inet_diag_req_v2 req;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001086 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001087
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +00001088 req.sdiag_family = AF_UNSPEC; /* compatibility */
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001089 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
1090 req.idiag_ext = rc->idiag_ext;
1091 req.idiag_states = rc->idiag_states;
1092 req.id = rc->id;
1093
1094 if (nlmsg_attrlen(cb->nlh, hdrlen))
1095 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1096
1097 return __inet_diag_dump(skb, cb, &req, bc);
1098}
1099
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001100static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -07001101 const struct nlmsghdr *nlh)
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001102{
David S. Millerd1063522012-06-26 21:28:54 -07001103 struct inet_diag_req *rc = nlmsg_data(nlh);
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001104 struct inet_diag_req_v2 req;
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001105
1106 req.sdiag_family = rc->idiag_family;
1107 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1108 req.idiag_ext = rc->idiag_ext;
1109 req.idiag_states = rc->idiag_states;
1110 req.id = rc->id;
1111
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001112 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001113}
1114
Pavel Emelyanov8d341722011-12-06 07:57:06 +00001115static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116{
Pavel Emelyanov3b09c842012-01-10 22:37:26 +00001117 int hdrlen = sizeof(struct inet_diag_req);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001118 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Thomas Grafead592b2007-03-22 23:30:35 -07001120 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1121 nlmsg_len(nlh) < hdrlen)
1122 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
David S. Millerb8f3ab42011-01-18 12:40:38 -08001124 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -07001125 if (nlmsg_attrlen(nlh, hdrlen)) {
1126 struct nlattr *attr;
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001127 int err;
Thomas Grafead592b2007-03-22 23:30:35 -07001128
1129 attr = nlmsg_find_attr(nlh, hdrlen,
1130 INET_DIAG_REQ_BYTECODE);
Lorenzo Colittia52e95a2016-08-24 15:46:26 +09001131 err = inet_diag_bc_audit(attr, skb);
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001132 if (err)
1133 return err;
Thomas Grafead592b2007-03-22 23:30:35 -07001134 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001135 {
1136 struct netlink_dump_control c = {
1137 .dump = inet_diag_dump_compat,
1138 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001139 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
Thomas Grafead592b2007-03-22 23:30:35 -07001142
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001143 return inet_diag_get_exact_compat(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144}
1145
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001146static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001147{
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001148 int hdrlen = sizeof(struct inet_diag_req_v2);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001149 struct net *net = sock_net(skb->sk);
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001150
1151 if (nlmsg_len(h) < hdrlen)
1152 return -EINVAL;
1153
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001154 if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
1155 h->nlmsg_flags & NLM_F_DUMP) {
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001156 if (nlmsg_attrlen(h, hdrlen)) {
1157 struct nlattr *attr;
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001158 int err;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001159
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001160 attr = nlmsg_find_attr(h, hdrlen,
1161 INET_DIAG_REQ_BYTECODE);
Lorenzo Colittia52e95a2016-08-24 15:46:26 +09001162 err = inet_diag_bc_audit(attr, skb);
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001163 if (err)
1164 return err;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001165 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001166 {
1167 struct netlink_dump_control c = {
1168 .dump = inet_diag_dump,
1169 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001170 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001171 }
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001172 }
1173
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001174 return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001175}
1176
Craig Gallek35ac8382015-06-15 11:26:20 -04001177static
1178int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
1179{
1180 const struct inet_diag_handler *handler;
1181 struct nlmsghdr *nlh;
1182 struct nlattr *attr;
1183 struct inet_diag_msg *r;
1184 void *info = NULL;
1185 int err = 0;
1186
1187 nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
1188 if (!nlh)
1189 return -ENOMEM;
1190
1191 r = nlmsg_data(nlh);
1192 memset(r, 0, sizeof(*r));
1193 inet_diag_msg_common_fill(r, sk);
Craig Galleke0df02e2015-06-17 10:59:10 -04001194 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1195 r->id.idiag_sport = inet_sk(sk)->inet_sport;
Craig Gallek35ac8382015-06-15 11:26:20 -04001196 r->idiag_state = sk->sk_state;
1197
1198 if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
1199 nlmsg_cancel(skb, nlh);
1200 return err;
1201 }
1202
1203 handler = inet_diag_lock_handler(sk->sk_protocol);
1204 if (IS_ERR(handler)) {
1205 inet_diag_unlock_handler(handler);
1206 nlmsg_cancel(skb, nlh);
1207 return PTR_ERR(handler);
1208 }
1209
1210 attr = handler->idiag_info_size
Nicolas Dichtel6ed46d12016-04-26 10:06:14 +02001211 ? nla_reserve_64bit(skb, INET_DIAG_INFO,
1212 handler->idiag_info_size,
1213 INET_DIAG_PAD)
Craig Gallek35ac8382015-06-15 11:26:20 -04001214 : NULL;
1215 if (attr)
1216 info = nla_data(attr);
1217
1218 handler->idiag_get_info(sk, r, info);
1219 inet_diag_unlock_handler(handler);
1220
1221 nlmsg_end(skb, nlh);
1222 return 0;
1223}
1224
Shan Wei8dcf01f2012-04-24 18:21:07 +00001225static const struct sock_diag_handler inet_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001226 .family = AF_INET,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001227 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001228 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001229 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001230};
1231
Shan Wei8dcf01f2012-04-24 18:21:07 +00001232static const struct sock_diag_handler inet6_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001233 .family = AF_INET6,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001234 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001235 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001236 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001237};
1238
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001239int inet_diag_register(const struct inet_diag_handler *h)
1240{
1241 const __u16 type = h->idiag_type;
1242 int err = -EINVAL;
1243
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001244 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001245 goto out;
1246
Herbert Xud523a322007-12-03 15:51:25 +11001247 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001248 err = -EEXIST;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001249 if (!inet_diag_table[type]) {
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001250 inet_diag_table[type] = h;
1251 err = 0;
1252 }
Herbert Xud523a322007-12-03 15:51:25 +11001253 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001254out:
1255 return err;
1256}
1257EXPORT_SYMBOL_GPL(inet_diag_register);
1258
1259void inet_diag_unregister(const struct inet_diag_handler *h)
1260{
1261 const __u16 type = h->idiag_type;
1262
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001263 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001264 return;
1265
Herbert Xud523a322007-12-03 15:51:25 +11001266 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001267 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +11001268 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001269}
1270EXPORT_SYMBOL_GPL(inet_diag_unregister);
1271
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001272static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001274 const int inet_diag_table_size = (IPPROTO_MAX *
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001275 sizeof(struct inet_diag_handler *));
1276 int err = -ENOMEM;
1277
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001278 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001279 if (!inet_diag_table)
1280 goto out;
1281
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001282 err = sock_diag_register(&inet_diag_handler);
1283 if (err)
1284 goto out_free_nl;
1285
1286 err = sock_diag_register(&inet6_diag_handler);
1287 if (err)
1288 goto out_free_inet;
1289
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001290 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001291out:
1292 return err;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001293
1294out_free_inet:
1295 sock_diag_unregister(&inet_diag_handler);
1296out_free_nl:
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001297 kfree(inet_diag_table);
1298 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001301static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001303 sock_diag_unregister(&inet6_diag_handler);
1304 sock_diag_unregister(&inet_diag_handler);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001305 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001306 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307}
1308
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001309module_init(inet_diag_init);
1310module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +00001312MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1313MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);