blob: abfbe492ebfe30f2e32f0e1c5dceba399bfbdbd9 [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])
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +000056 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
57 NETLINK_SOCK_DIAG, AF_INET, proto);
Herbert Xud523a322007-12-03 15:51:25 +110058
59 mutex_lock(&inet_diag_table_mutex);
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000060 if (!inet_diag_table[proto])
Herbert Xud523a322007-12-03 15:51:25 +110061 return ERR_PTR(-ENOENT);
62
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000063 return inet_diag_table[proto];
Herbert Xud523a322007-12-03 15:51:25 +110064}
65
Eric Dumazete31c5e02015-03-10 07:15:53 -070066static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
Herbert Xud523a322007-12-03 15:51:25 +110067{
68 mutex_unlock(&inet_diag_table_mutex);
69}
70
Xin Longcb2050a2016-04-14 15:35:32 +080071void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
Eric Dumazeta4458342015-03-13 15:51:12 -070072{
73 r->idiag_family = sk->sk_family;
74
75 r->id.idiag_sport = htons(sk->sk_num);
76 r->id.idiag_dport = sk->sk_dport;
77 r->id.idiag_if = sk->sk_bound_dev_if;
78 sock_diag_save_cookie(sk, r->id.idiag_cookie);
79
80#if IS_ENABLED(CONFIG_IPV6)
81 if (sk->sk_family == AF_INET6) {
82 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
83 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
84 } else
85#endif
86 {
87 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
88 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
89
90 r->id.idiag_src[0] = sk->sk_rcv_saddr;
91 r->id.idiag_dst[0] = sk->sk_daddr;
92 }
93}
Xin Longcb2050a2016-04-14 15:35:32 +080094EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill);
Eric Dumazeta4458342015-03-13 15:51:12 -070095
Eric Dumazetc8e2c802015-03-13 09:49:59 -070096static size_t inet_sk_attr_size(void)
97{
98 return nla_total_size(sizeof(struct tcp_info))
99 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
100 + nla_total_size(1) /* INET_DIAG_TOS */
101 + nla_total_size(1) /* INET_DIAG_TCLASS */
102 + nla_total_size(sizeof(struct inet_diag_meminfo))
103 + nla_total_size(sizeof(struct inet_diag_msg))
104 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
105 + nla_total_size(TCP_CA_NAME_MAX)
106 + nla_total_size(sizeof(struct tcpvegas_info))
107 + 64;
108}
109
Xin Longcb2050a2016-04-14 15:35:32 +0800110int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
111 struct inet_diag_msg *r, int ext,
112 struct user_namespace *user_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700114 const struct inet_sock *inet = inet_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Pavel Emelyanove4e541a2012-10-23 22:29:56 +0400116 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
117 goto errout;
118
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500119 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
120 * hence this needs to be included regardless of socket family.
121 */
122 if (ext & (1 << (INET_DIAG_TOS - 1)))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000123 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
124 goto errout;
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500125
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000126#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300127 if (r->idiag_family == AF_INET6) {
Maciej Żenczykowski06236ac2011-11-07 14:23:11 +0000128 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
Eric Dumazetefe42082013-10-03 15:42:29 -0700129 if (nla_put_u8(skb, INET_DIAG_TCLASS,
130 inet6_sk(sk)->tclass) < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000131 goto errout;
Phil Sutter20462152015-06-24 11:02:51 +0200132
Phil Sutter8220ea22015-07-10 11:39:57 +0200133 if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
134 nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
Phil Sutter20462152015-06-24 11:02:51 +0200135 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 }
137#endif
138
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600139 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000140 r->idiag_inode = sock_i_ino(sk);
141
Xin Longcb2050a2016-04-14 15:35:32 +0800142 return 0;
143errout:
144 return 1;
145}
146EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
147
148int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
149 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
150 struct user_namespace *user_ns,
151 u32 portid, u32 seq, u16 nlmsg_flags,
152 const struct nlmsghdr *unlh)
153{
154 const struct tcp_congestion_ops *ca_ops;
155 const struct inet_diag_handler *handler;
156 int ext = req->idiag_ext;
157 struct inet_diag_msg *r;
158 struct nlmsghdr *nlh;
159 struct nlattr *attr;
160 void *info = NULL;
161
162 handler = inet_diag_table[req->sdiag_protocol];
163 BUG_ON(!handler);
164
165 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
166 nlmsg_flags);
167 if (!nlh)
168 return -EMSGSIZE;
169
170 r = nlmsg_data(nlh);
171 BUG_ON(!sk_fullsock(sk));
172
173 inet_diag_msg_common_fill(r, sk);
174 r->idiag_state = sk->sk_state;
175 r->idiag_timer = 0;
176 r->idiag_retrans = 0;
177
178 if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns))
179 goto errout;
180
Thomas Graf6e277ed2012-06-26 23:36:12 +0000181 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
182 struct inet_diag_meminfo minfo = {
183 .idiag_rmem = sk_rmem_alloc_get(sk),
184 .idiag_wmem = sk->sk_wmem_queued,
185 .idiag_fmem = sk->sk_forward_alloc,
186 .idiag_tmem = sk_wmem_alloc_get(sk),
187 };
188
189 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
190 goto errout;
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000191 }
192
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000193 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
194 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000195 goto errout;
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000196
Eric Dumazete31c5e02015-03-10 07:15:53 -0700197 if (!icsk) {
Shan Wei62ad6fc2012-04-24 18:15:41 +0000198 handler->idiag_get_info(sk, r, NULL);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000199 goto out;
200 }
201
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000202 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
203 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
204 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300205 r->idiag_timer = 1;
206 r->idiag_retrans = icsk->icsk_retransmits;
Xin Longb7de5292016-04-19 15:10:01 +0800207 r->idiag_expires =
208 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700209 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300210 r->idiag_timer = 4;
211 r->idiag_retrans = icsk->icsk_probes_out;
Xin Longb7de5292016-04-19 15:10:01 +0800212 r->idiag_expires =
213 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300215 r->idiag_timer = 2;
216 r->idiag_retrans = icsk->icsk_probes_out;
Xin Longb7de5292016-04-19 15:10:01 +0800217 r->idiag_expires =
218 jiffies_to_msecs(sk->sk_timer.expires - jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300220 r->idiag_timer = 0;
221 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 }
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300223
Craig Gallek3fd22af2015-06-15 11:26:19 -0400224 if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
Nicolas Dichtel6ed46d12016-04-26 10:06:14 +0200225 attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
226 handler->idiag_info_size,
227 INET_DIAG_PAD);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000228 if (!attr)
229 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Thomas Graf6e277ed2012-06-26 23:36:12 +0000231 info = nla_data(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700234 if (ext & (1 << (INET_DIAG_CONG - 1))) {
235 int err = 0;
236
237 rcu_read_lock();
238 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
239 if (ca_ops)
240 err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
241 rcu_read_unlock();
242 if (err < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000243 goto errout;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700244 }
Thomas Graf6e277ed2012-06-26 23:36:12 +0000245
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300246 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700248 if (sk->sk_state < TCP_TIME_WAIT) {
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700249 union tcp_cc_info info;
250 size_t sz = 0;
251 int attr;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700252
253 rcu_read_lock();
254 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
255 if (ca_ops && ca_ops->get_info)
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700256 sz = ca_ops->get_info(sk, ext, &attr, &info);
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700257 rcu_read_unlock();
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700258 if (sz && nla_put(skb, attr, sz, &info) < 0)
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700259 goto errout;
260 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000262out:
Johannes Berg053c0952015-01-16 22:09:00 +0100263 nlmsg_end(skb, nlh);
264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Thomas Graf6e277ed2012-06-26 23:36:12 +0000266errout:
267 nlmsg_cancel(skb, nlh);
Patrick McHardy26932562007-01-31 23:16:40 -0800268 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269}
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000270EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
271
272static int inet_csk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700273 struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700274 const struct inet_diag_req_v2 *req,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600275 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000276 u32 portid, u32 seq, u16 nlmsg_flags,
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000277 const struct nlmsghdr *unlh)
278{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700279 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req,
280 user_ns, portid, seq, nlmsg_flags, unlh);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000281}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700283static int inet_twsk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700284 struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000285 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800286 const struct nlmsghdr *unlh)
287{
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700288 struct inet_timewait_sock *tw = inet_twsk(sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800289 struct inet_diag_msg *r;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000290 struct nlmsghdr *nlh;
Eric Dumazet789f5582015-04-12 18:51:09 -0700291 long tmo;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800292
Eric W. Biederman15e47302012-09-07 20:12:54 +0000293 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000294 nlmsg_flags);
295 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700296 return -EMSGSIZE;
David S. Millerd1063522012-06-26 21:28:54 -0700297
298 r = nlmsg_data(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800299 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
300
Eric Dumazet789f5582015-04-12 18:51:09 -0700301 tmo = tw->tw_timer.expires - jiffies;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800302 if (tmo < 0)
303 tmo = 0;
304
Eric Dumazeta4458342015-03-13 15:51:12 -0700305 inet_diag_msg_common_fill(r, sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800306 r->idiag_retrans = 0;
Daniel Borkmannb1aac812013-12-17 00:38:39 +0100307
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800308 r->idiag_state = tw->tw_substate;
309 r->idiag_timer = 3;
Eric Dumazet96f817f2013-10-03 14:27:25 -0700310 r->idiag_expires = jiffies_to_msecs(tmo);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800311 r->idiag_rqueue = 0;
312 r->idiag_wqueue = 0;
313 r->idiag_uid = 0;
314 r->idiag_inode = 0;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000315
Johannes Berg053c0952015-01-16 22:09:00 +0100316 nlmsg_end(skb, nlh);
317 return 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800318}
319
Eric Dumazeta58917f2015-03-15 21:12:14 -0700320static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
321 u32 portid, u32 seq, u16 nlmsg_flags,
322 const struct nlmsghdr *unlh)
323{
324 struct inet_diag_msg *r;
325 struct nlmsghdr *nlh;
326 long tmo;
327
328 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
329 nlmsg_flags);
330 if (!nlh)
331 return -EMSGSIZE;
332
333 r = nlmsg_data(nlh);
334 inet_diag_msg_common_fill(r, sk);
335 r->idiag_state = TCP_SYN_RECV;
336 r->idiag_timer = 1;
337 r->idiag_retrans = inet_reqsk(sk)->num_retrans;
338
339 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
340 offsetof(struct sock, sk_cookie));
341
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700342 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700343 r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
344 r->idiag_rqueue = 0;
345 r->idiag_wqueue = 0;
346 r->idiag_uid = 0;
347 r->idiag_inode = 0;
348
349 nlmsg_end(skb, nlh);
350 return 0;
351}
352
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800353static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700354 const struct inet_diag_req_v2 *r,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600355 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000356 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800357 const struct nlmsghdr *unlh)
358{
359 if (sk->sk_state == TCP_TIME_WAIT)
Eric Dumazeta58917f2015-03-15 21:12:14 -0700360 return inet_twsk_diag_fill(sk, skb, portid, seq,
Eric Dumazetefe42082013-10-03 15:42:29 -0700361 nlmsg_flags, unlh);
362
Eric Dumazeta58917f2015-03-15 21:12:14 -0700363 if (sk->sk_state == TCP_NEW_SYN_RECV)
364 return inet_req_diag_fill(sk, skb, portid, seq,
365 nlmsg_flags, unlh);
366
Eric Dumazetefe42082013-10-03 15:42:29 -0700367 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
368 nlmsg_flags, unlh);
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800369}
370
Lorenzo Colittib613f562015-12-16 12:30:02 +0900371struct sock *inet_diag_find_one_icsk(struct net *net,
372 struct inet_hashinfo *hashinfo,
373 const struct inet_diag_req_v2 *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700375 struct sock *sk;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300376
Eric Dumazet2d331912016-04-01 08:52:15 -0700377 rcu_read_lock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700378 if (req->sdiag_family == AF_INET)
Craig Galleka5836362016-02-10 11:50:38 -0500379 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300380 req->id.idiag_dport, req->id.idiag_src[0],
381 req->id.idiag_sport, req->id.idiag_if);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000382#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet7c130672016-01-20 16:25:01 -0800383 else if (req->sdiag_family == AF_INET6) {
384 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
385 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
Craig Galleka5836362016-02-10 11:50:38 -0500386 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
Eric Dumazet7c130672016-01-20 16:25:01 -0800387 req->id.idiag_dport, req->id.idiag_src[3],
388 req->id.idiag_sport, req->id.idiag_if);
389 else
Craig Galleka5836362016-02-10 11:50:38 -0500390 sk = inet6_lookup(net, hashinfo, NULL, 0,
Eric Dumazet7c130672016-01-20 16:25:01 -0800391 (struct in6_addr *)req->id.idiag_dst,
392 req->id.idiag_dport,
393 (struct in6_addr *)req->id.idiag_src,
394 req->id.idiag_sport,
395 req->id.idiag_if);
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397#endif
Eric Dumazet2d331912016-04-01 08:52:15 -0700398 else {
399 rcu_read_unlock();
Lorenzo Colittib613f562015-12-16 12:30:02 +0900400 return ERR_PTR(-EINVAL);
Eric Dumazet2d331912016-04-01 08:52:15 -0700401 }
402 rcu_read_unlock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700403 if (!sk)
Lorenzo Colittib613f562015-12-16 12:30:02 +0900404 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Lorenzo Colittib613f562015-12-16 12:30:02 +0900406 if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
407 sock_gen_put(sk);
408 return ERR_PTR(-ENOENT);
409 }
410
411 return sk;
412}
413EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
414
415int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
416 struct sk_buff *in_skb,
417 const struct nlmsghdr *nlh,
418 const struct inet_diag_req_v2 *req)
419{
420 struct net *net = sock_net(in_skb->sk);
421 struct sk_buff *rep;
422 struct sock *sk;
423 int err;
424
425 sk = inet_diag_find_one_icsk(net, hashinfo, req);
426 if (IS_ERR(sk))
427 return PTR_ERR(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700429 rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000430 if (!rep) {
431 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 goto out;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000435 err = sk_diag_fill(sk, rep, req,
Patrick McHardye32123e2013-04-17 06:46:57 +0000436 sk_user_ns(NETLINK_CB(in_skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000437 NETLINK_CB(in_skb).portid,
Patrick McHardy26932562007-01-31 23:16:40 -0800438 nlh->nlmsg_seq, 0, nlh);
439 if (err < 0) {
440 WARN_ON(err == -EMSGSIZE);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000441 nlmsg_free(rep);
Patrick McHardy26932562007-01-31 23:16:40 -0800442 goto out;
443 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000444 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300445 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 if (err > 0)
447 err = 0;
448
449out:
Eric Dumazetc1d607c2013-10-11 08:54:49 -0700450 if (sk)
451 sock_gen_put(sk);
452
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000453 return err;
454}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000455EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000456
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900457static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000458 const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700459 const struct inet_diag_req_v2 *req)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000460{
461 const struct inet_diag_handler *handler;
462 int err;
463
464 handler = inet_diag_lock_handler(req->sdiag_protocol);
465 if (IS_ERR(handler))
466 err = PTR_ERR(handler);
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900467 else if (cmd == SOCK_DIAG_BY_FAMILY)
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000468 err = handler->dump_one(in_skb, nlh, req);
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900469 else if (cmd == SOCK_DESTROY && handler->destroy)
470 err = handler->destroy(in_skb, req);
471 else
472 err = -EOPNOTSUPP;
Herbert Xud523a322007-12-03 15:51:25 +1100473 inet_diag_unlock_handler(handler);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return err;
476}
477
Al Viro9f855292006-09-27 18:44:30 -0700478static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479{
480 int words = bits >> 5;
481
482 bits &= 0x1f;
483
484 if (words) {
485 if (memcmp(a1, a2, words << 2))
486 return 0;
487 }
488 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700489 __be32 w1, w2;
490 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
492 w1 = a1[words];
493 w2 = a2[words];
494
495 mask = htonl((0xffffffff) << (32 - bits));
496
497 if ((w1 ^ w2) & mask)
498 return 0;
499 }
500
501 return 1;
502}
503
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000504static int inet_diag_bc_run(const struct nlattr *_bc,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700505 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000507 const void *bc = nla_data(_bc);
508 int len = nla_len(_bc);
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 while (len > 0) {
511 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300512 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300515 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300517 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 yes = 0;
519 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300520 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 yes = entry->sport >= op[1].no;
522 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300523 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800524 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300526 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 yes = entry->dport >= op[1].no;
528 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300529 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 yes = entry->dport <= op[1].no;
531 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300532 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
534 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300535 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300536 case INET_DIAG_BC_D_COND: {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700537 const struct inet_diag_hostcond *cond;
538 const __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Eric Dumazete31c5e02015-03-10 07:15:53 -0700540 cond = (const struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300542 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 entry->sport : entry->dport)) {
544 yes = 0;
545 break;
546 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800547
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300548 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 addr = entry->saddr;
550 else
551 addr = entry->daddr;
552
Neal Cardwellf67caec2012-12-08 19:43:23 +0000553 if (cond->family != AF_UNSPEC &&
554 cond->family != entry->family) {
555 if (entry->family == AF_INET6 &&
556 cond->family == AF_INET) {
557 if (addr[0] == 0 && addr[1] == 0 &&
558 addr[2] == htonl(0xffff) &&
559 bitstring_match(addr + 3,
560 cond->addr,
561 cond->prefix_len))
562 break;
563 }
564 yes = 0;
565 break;
566 }
567
568 if (cond->prefix_len == 0)
569 break;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800570 if (bitstring_match(addr, cond->addr,
571 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 yes = 0;
574 break;
575 }
David Ahern637c8412016-06-23 18:42:51 -0700576 case INET_DIAG_BC_DEV_COND: {
577 u32 ifindex;
578
579 ifindex = *((const u32 *)(op + 1));
580 if (ifindex != entry->ifindex)
581 yes = 0;
582 break;
583 }
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900584 case INET_DIAG_BC_MARK_COND: {
585 struct inet_diag_markcond *cond;
586
587 cond = (struct inet_diag_markcond *)(op + 1);
588 if ((entry->mark & cond->mask) != cond->mark)
589 yes = 0;
590 break;
591 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800594 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 len -= op->yes;
596 bc += op->yes;
597 } else {
598 len -= op->no;
599 bc += op->no;
600 }
601 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000602 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603}
604
Eric Dumazeta4458342015-03-13 15:51:12 -0700605/* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
606 */
607static void entry_fill_addrs(struct inet_diag_entry *entry,
608 const struct sock *sk)
609{
610#if IS_ENABLED(CONFIG_IPV6)
611 if (sk->sk_family == AF_INET6) {
612 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
613 entry->daddr = sk->sk_v6_daddr.s6_addr32;
614 } else
615#endif
616 {
617 entry->saddr = &sk->sk_rcv_saddr;
618 entry->daddr = &sk->sk_daddr;
619 }
620}
621
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000622int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
623{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000624 struct inet_sock *inet = inet_sk(sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700625 struct inet_diag_entry entry;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000626
Eric Dumazete31c5e02015-03-10 07:15:53 -0700627 if (!bc)
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000628 return 1;
629
630 entry.family = sk->sk_family;
Eric Dumazeta4458342015-03-13 15:51:12 -0700631 entry_fill_addrs(&entry, sk);
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000632 entry.sport = inet->inet_num;
633 entry.dport = ntohs(inet->inet_dport);
David Ahern637c8412016-06-23 18:42:51 -0700634 entry.ifindex = sk->sk_bound_dev_if;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700635 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900636 if (sk_fullsock(sk))
637 entry.mark = sk->sk_mark;
638 else if (sk->sk_state == TCP_NEW_SYN_RECV)
639 entry.mark = inet_rsk(inet_reqsk(sk))->ir_mark;
640 else
641 entry.mark = 0;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000642
643 return inet_diag_bc_run(bc, &entry);
644}
645EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647static int valid_cc(const void *bc, int len, int cc)
648{
649 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300650 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 if (cc > len)
653 return 0;
654 if (cc == len)
655 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400656 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 return 0;
658 len -= op->yes;
659 bc += op->yes;
660 }
661 return 0;
662}
663
David Ahern637c8412016-06-23 18:42:51 -0700664/* data is u32 ifindex */
665static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
666 int *min_len)
667{
668 /* Check ifindex space. */
669 *min_len += sizeof(u32);
670 if (len < *min_len)
671 return false;
672
673 return true;
674}
Neal Cardwell405c0052012-12-08 19:43:22 +0000675/* Validate an inet_diag_hostcond. */
676static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
677 int *min_len)
678{
Neal Cardwell405c0052012-12-08 19:43:22 +0000679 struct inet_diag_hostcond *cond;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700680 int addr_len;
Neal Cardwell405c0052012-12-08 19:43:22 +0000681
682 /* Check hostcond space. */
683 *min_len += sizeof(struct inet_diag_hostcond);
684 if (len < *min_len)
685 return false;
686 cond = (struct inet_diag_hostcond *)(op + 1);
687
688 /* Check address family and address length. */
689 switch (cond->family) {
690 case AF_UNSPEC:
691 addr_len = 0;
692 break;
693 case AF_INET:
694 addr_len = sizeof(struct in_addr);
695 break;
696 case AF_INET6:
697 addr_len = sizeof(struct in6_addr);
698 break;
699 default:
700 return false;
701 }
702 *min_len += addr_len;
703 if (len < *min_len)
704 return false;
705
706 /* Check prefix length (in bits) vs address length (in bytes). */
707 if (cond->prefix_len > 8 * addr_len)
708 return false;
709
710 return true;
711}
712
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000713/* Validate a port comparison operator. */
Eric Dumazete31c5e02015-03-10 07:15:53 -0700714static bool valid_port_comparison(const struct inet_diag_bc_op *op,
715 int len, int *min_len)
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000716{
717 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
718 *min_len += sizeof(struct inet_diag_bc_op);
719 if (len < *min_len)
720 return false;
721 return true;
722}
723
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900724static bool valid_markcond(const struct inet_diag_bc_op *op, int len,
725 int *min_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726{
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900727 *min_len += sizeof(struct inet_diag_markcond);
728 return len >= *min_len;
729}
730
731static int inet_diag_bc_audit(const struct nlattr *attr,
732 const struct sk_buff *skb)
733{
734 bool net_admin = netlink_net_capable(skb, CAP_NET_ADMIN);
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +0900735 const void *bytecode, *bc;
736 int bytecode_len, len;
737
738 if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
739 return -EINVAL;
740
741 bytecode = bc = nla_data(attr);
742 len = bytecode_len = nla_len(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 while (len > 0) {
Neal Cardwell405c0052012-12-08 19:43:22 +0000745 int min_len = sizeof(struct inet_diag_bc_op);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700746 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300749 case INET_DIAG_BC_S_COND:
750 case INET_DIAG_BC_D_COND:
Neal Cardwell405c0052012-12-08 19:43:22 +0000751 if (!valid_hostcond(bc, len, &min_len))
752 return -EINVAL;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000753 break;
David Ahern637c8412016-06-23 18:42:51 -0700754 case INET_DIAG_BC_DEV_COND:
755 if (!valid_devcond(bc, len, &min_len))
756 return -EINVAL;
757 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300758 case INET_DIAG_BC_S_GE:
759 case INET_DIAG_BC_S_LE:
760 case INET_DIAG_BC_D_GE:
761 case INET_DIAG_BC_D_LE:
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000762 if (!valid_port_comparison(bc, len, &min_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 return -EINVAL;
764 break;
Lorenzo Colittia52e95a2016-08-24 15:46:26 +0900765 case INET_DIAG_BC_MARK_COND:
766 if (!net_admin)
767 return -EPERM;
768 if (!valid_markcond(bc, len, &min_len))
769 return -EINVAL;
770 break;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000771 case INET_DIAG_BC_AUTO:
772 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300773 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 break;
775 default:
776 return -EINVAL;
777 }
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000778
779 if (op->code != INET_DIAG_BC_NOP) {
780 if (op->no < min_len || op->no > len + 4 || op->no & 3)
781 return -EINVAL;
782 if (op->no < len &&
783 !valid_cc(bytecode, bytecode_len, len - op->no))
784 return -EINVAL;
785 }
786
Neal Cardwell405c0052012-12-08 19:43:22 +0000787 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
Eric Dumazeteeb14972011-06-17 16:25:39 -0400788 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800789 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 len -= op->yes;
791 }
792 return len == 0 ? 0 : -EINVAL;
793}
794
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800795static int inet_csk_diag_dump(struct sock *sk,
796 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000797 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700798 const struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000799 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000801 if (!inet_diag_bc_sk(bc, sk))
802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000804 return inet_csk_diag_fill(sk, skb, r,
Patrick McHardye32123e2013-04-17 06:46:57 +0000805 sk_user_ns(NETLINK_CB(cb->skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000806 NETLINK_CB(cb->skb).portid,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800807 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
809
Eric Dumazet49612722015-03-05 10:18:14 -0800810static void twsk_build_assert(void)
811{
812 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
813 offsetof(struct sock, sk_family));
814
815 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
816 offsetof(struct inet_sock, inet_num));
817
818 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
819 offsetof(struct inet_sock, inet_dport));
820
821 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
822 offsetof(struct inet_sock, inet_rcv_saddr));
823
824 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
825 offsetof(struct inet_sock, inet_daddr));
826
827#if IS_ENABLED(CONFIG_IPV6)
828 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
829 offsetof(struct sock, sk_v6_rcv_saddr));
830
831 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
832 offsetof(struct sock, sk_v6_daddr));
833#endif
834}
835
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000836void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700837 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700838 const struct inet_diag_req_v2 *r, struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000840 struct net *net = sock_net(skb->sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700841 int i, num, s_i, s_num;
Eric Dumazet079096f2015-10-02 11:43:32 -0700842 u32 idiag_states = r->idiag_states;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800843
Eric Dumazet079096f2015-10-02 11:43:32 -0700844 if (idiag_states & TCPF_SYN_RECV)
845 idiag_states |= TCPF_NEW_SYN_RECV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 s_i = cb->args[1];
847 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 if (cb->args[0] == 0) {
Eric Dumazet079096f2015-10-02 11:43:32 -0700850 if (!(idiag_states & TCPF_LISTEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300852
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700853 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800854 struct inet_listen_hashbucket *ilb;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700855 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
857 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800858 ilb = &hashinfo->listening_hash[i];
859 spin_lock_bh(&ilb->lock);
Eric Dumazet3b24d852016-04-01 08:52:17 -0700860 sk_for_each(sk, &ilb->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct inet_sock *inet = inet_sk(sk);
862
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000863 if (!net_eq(sock_net(sk), net))
864 continue;
865
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (num < s_num) {
867 num++;
868 continue;
869 }
870
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000871 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazete31c5e02015-03-10 07:15:53 -0700872 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000873 goto next_listen;
874
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000875 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300876 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 goto next_listen;
878
Eric Dumazet079096f2015-10-02 11:43:32 -0700879 if (r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 cb->args[3] > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 goto next_listen;
882
Eric Dumazet079096f2015-10-02 11:43:32 -0700883 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800884 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 goto done;
886 }
887
888next_listen:
889 cb->args[3] = 0;
890 cb->args[4] = 0;
891 ++num;
892 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800893 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
895 s_num = 0;
896 cb->args[3] = 0;
897 cb->args[4] = 0;
898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899skip_listen_ht:
900 cb->args[0] = 1;
901 s_i = num = s_num = 0;
902 }
903
Eric Dumazet079096f2015-10-02 11:43:32 -0700904 if (!(idiag_states & ~TCPF_LISTEN))
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000905 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906
Eric Dumazetf373b532009-10-09 00:16:19 +0000907 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300908 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800909 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800910 struct hlist_nulls_node *node;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700911 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Andi Kleen6be547a2008-08-28 01:09:54 -0700913 num = 0;
914
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700915 if (hlist_nulls_empty(&head->chain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700916 continue;
917
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (i > s_i)
919 s_num = 0;
920
David S. Miller7e3aab42008-11-21 16:39:19 -0800921 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800922 sk_nulls_for_each(sk, node, &head->chain) {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700923 int state, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000925 if (!net_eq(sock_net(sk), net))
926 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 if (num < s_num)
928 goto next_normal;
Neal Cardwell70315d22014-01-10 15:34:45 -0500929 state = (sk->sk_state == TCP_TIME_WAIT) ?
930 inet_twsk(sk)->tw_substate : sk->sk_state;
Eric Dumazet079096f2015-10-02 11:43:32 -0700931 if (!(idiag_states & (1 << state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 goto next_normal;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000933 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700934 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000935 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700936 if (r->id.idiag_sport != htons(sk->sk_num) &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300937 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700939 if (r->id.idiag_dport != sk->sk_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800940 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 goto next_normal;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700942 twsk_build_assert();
943
944 if (!inet_diag_bc_sk(bc, sk))
945 goto next_normal;
946
947 res = sk_diag_fill(sk, skb, r,
948 sk_user_ns(NETLINK_CB(cb->skb).sk),
949 NETLINK_CB(cb->skb).portid,
950 cb->nlh->nlmsg_seq, NLM_F_MULTI,
951 cb->nlh);
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700952 if (res < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800953 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 goto done;
955 }
956next_normal:
957 ++num;
958 }
959
David S. Miller7e3aab42008-11-21 16:39:19 -0800960 spin_unlock_bh(lock);
Eric Dumazetacffb582016-03-14 15:40:00 -0700961 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 }
963
964done:
965 cb->args[1] = i;
966 cb->args[2] = num;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000967out:
968 ;
969}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000970EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000971
972static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700973 const struct inet_diag_req_v2 *r,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700974 struct nlattr *bc)
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000975{
976 const struct inet_diag_handler *handler;
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000977 int err = 0;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000978
979 handler = inet_diag_lock_handler(r->sdiag_protocol);
980 if (!IS_ERR(handler))
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000981 handler->dump(skb, cb, r, bc);
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000982 else
983 err = PTR_ERR(handler);
Herbert Xud523a322007-12-03 15:51:25 +1100984 inet_diag_unlock_handler(handler);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000985
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000986 return err ? : skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000989static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
990{
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000991 int hdrlen = sizeof(struct inet_diag_req_v2);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700992 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000993
994 if (nlmsg_attrlen(cb->nlh, hdrlen))
995 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
996
David S. Millerd1063522012-06-26 21:28:54 -0700997 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000998}
999
Eric Dumazete31c5e02015-03-10 07:15:53 -07001000static int inet_diag_type2proto(int type)
Pavel Emelyanova029fe22011-12-06 07:59:32 +00001001{
1002 switch (type) {
1003 case TCPDIAG_GETSOCK:
1004 return IPPROTO_TCP;
1005 case DCCPDIAG_GETSOCK:
1006 return IPPROTO_DCCP;
1007 default:
1008 return 0;
1009 }
1010}
1011
Eric Dumazete31c5e02015-03-10 07:15:53 -07001012static int inet_diag_dump_compat(struct sk_buff *skb,
1013 struct netlink_callback *cb)
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001014{
David S. Millerd1063522012-06-26 21:28:54 -07001015 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
Eric Dumazete31c5e02015-03-10 07:15:53 -07001016 int hdrlen = sizeof(struct inet_diag_req);
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001017 struct inet_diag_req_v2 req;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001018 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001019
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +00001020 req.sdiag_family = AF_UNSPEC; /* compatibility */
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001021 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
1022 req.idiag_ext = rc->idiag_ext;
1023 req.idiag_states = rc->idiag_states;
1024 req.id = rc->id;
1025
1026 if (nlmsg_attrlen(cb->nlh, hdrlen))
1027 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
1028
1029 return __inet_diag_dump(skb, cb, &req, bc);
1030}
1031
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001032static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -07001033 const struct nlmsghdr *nlh)
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001034{
David S. Millerd1063522012-06-26 21:28:54 -07001035 struct inet_diag_req *rc = nlmsg_data(nlh);
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001036 struct inet_diag_req_v2 req;
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001037
1038 req.sdiag_family = rc->idiag_family;
1039 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1040 req.idiag_ext = rc->idiag_ext;
1041 req.idiag_states = rc->idiag_states;
1042 req.id = rc->id;
1043
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001044 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001045}
1046
Pavel Emelyanov8d341722011-12-06 07:57:06 +00001047static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048{
Pavel Emelyanov3b09c842012-01-10 22:37:26 +00001049 int hdrlen = sizeof(struct inet_diag_req);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001050 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Thomas Grafead592b2007-03-22 23:30:35 -07001052 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1053 nlmsg_len(nlh) < hdrlen)
1054 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
David S. Millerb8f3ab42011-01-18 12:40:38 -08001056 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -07001057 if (nlmsg_attrlen(nlh, hdrlen)) {
1058 struct nlattr *attr;
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001059 int err;
Thomas Grafead592b2007-03-22 23:30:35 -07001060
1061 attr = nlmsg_find_attr(nlh, hdrlen,
1062 INET_DIAG_REQ_BYTECODE);
Lorenzo Colittia52e95a2016-08-24 15:46:26 +09001063 err = inet_diag_bc_audit(attr, skb);
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001064 if (err)
1065 return err;
Thomas Grafead592b2007-03-22 23:30:35 -07001066 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001067 {
1068 struct netlink_dump_control c = {
1069 .dump = inet_diag_dump_compat,
1070 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001071 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 }
Thomas Grafead592b2007-03-22 23:30:35 -07001074
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001075 return inet_diag_get_exact_compat(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076}
1077
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001078static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001079{
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001080 int hdrlen = sizeof(struct inet_diag_req_v2);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001081 struct net *net = sock_net(skb->sk);
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001082
1083 if (nlmsg_len(h) < hdrlen)
1084 return -EINVAL;
1085
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001086 if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
1087 h->nlmsg_flags & NLM_F_DUMP) {
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001088 if (nlmsg_attrlen(h, hdrlen)) {
1089 struct nlattr *attr;
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001090 int err;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001091
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001092 attr = nlmsg_find_attr(h, hdrlen,
1093 INET_DIAG_REQ_BYTECODE);
Lorenzo Colittia52e95a2016-08-24 15:46:26 +09001094 err = inet_diag_bc_audit(attr, skb);
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001095 if (err)
1096 return err;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001097 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001098 {
1099 struct netlink_dump_control c = {
1100 .dump = inet_diag_dump,
1101 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001102 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001103 }
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001104 }
1105
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001106 return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001107}
1108
Craig Gallek35ac8382015-06-15 11:26:20 -04001109static
1110int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
1111{
1112 const struct inet_diag_handler *handler;
1113 struct nlmsghdr *nlh;
1114 struct nlattr *attr;
1115 struct inet_diag_msg *r;
1116 void *info = NULL;
1117 int err = 0;
1118
1119 nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
1120 if (!nlh)
1121 return -ENOMEM;
1122
1123 r = nlmsg_data(nlh);
1124 memset(r, 0, sizeof(*r));
1125 inet_diag_msg_common_fill(r, sk);
Craig Galleke0df02e2015-06-17 10:59:10 -04001126 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1127 r->id.idiag_sport = inet_sk(sk)->inet_sport;
Craig Gallek35ac8382015-06-15 11:26:20 -04001128 r->idiag_state = sk->sk_state;
1129
1130 if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
1131 nlmsg_cancel(skb, nlh);
1132 return err;
1133 }
1134
1135 handler = inet_diag_lock_handler(sk->sk_protocol);
1136 if (IS_ERR(handler)) {
1137 inet_diag_unlock_handler(handler);
1138 nlmsg_cancel(skb, nlh);
1139 return PTR_ERR(handler);
1140 }
1141
1142 attr = handler->idiag_info_size
Nicolas Dichtel6ed46d12016-04-26 10:06:14 +02001143 ? nla_reserve_64bit(skb, INET_DIAG_INFO,
1144 handler->idiag_info_size,
1145 INET_DIAG_PAD)
Craig Gallek35ac8382015-06-15 11:26:20 -04001146 : NULL;
1147 if (attr)
1148 info = nla_data(attr);
1149
1150 handler->idiag_get_info(sk, r, info);
1151 inet_diag_unlock_handler(handler);
1152
1153 nlmsg_end(skb, nlh);
1154 return 0;
1155}
1156
Shan Wei8dcf01f2012-04-24 18:21:07 +00001157static const struct sock_diag_handler inet_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001158 .family = AF_INET,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001159 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001160 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001161 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001162};
1163
Shan Wei8dcf01f2012-04-24 18:21:07 +00001164static const struct sock_diag_handler inet6_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001165 .family = AF_INET6,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001166 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001167 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001168 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001169};
1170
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001171int inet_diag_register(const struct inet_diag_handler *h)
1172{
1173 const __u16 type = h->idiag_type;
1174 int err = -EINVAL;
1175
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001176 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001177 goto out;
1178
Herbert Xud523a322007-12-03 15:51:25 +11001179 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001180 err = -EEXIST;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001181 if (!inet_diag_table[type]) {
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001182 inet_diag_table[type] = h;
1183 err = 0;
1184 }
Herbert Xud523a322007-12-03 15:51:25 +11001185 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001186out:
1187 return err;
1188}
1189EXPORT_SYMBOL_GPL(inet_diag_register);
1190
1191void inet_diag_unregister(const struct inet_diag_handler *h)
1192{
1193 const __u16 type = h->idiag_type;
1194
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001195 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001196 return;
1197
Herbert Xud523a322007-12-03 15:51:25 +11001198 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001199 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +11001200 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001201}
1202EXPORT_SYMBOL_GPL(inet_diag_unregister);
1203
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001204static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001206 const int inet_diag_table_size = (IPPROTO_MAX *
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001207 sizeof(struct inet_diag_handler *));
1208 int err = -ENOMEM;
1209
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001210 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001211 if (!inet_diag_table)
1212 goto out;
1213
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001214 err = sock_diag_register(&inet_diag_handler);
1215 if (err)
1216 goto out_free_nl;
1217
1218 err = sock_diag_register(&inet6_diag_handler);
1219 if (err)
1220 goto out_free_inet;
1221
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001222 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001223out:
1224 return err;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001225
1226out_free_inet:
1227 sock_diag_unregister(&inet_diag_handler);
1228out_free_nl:
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001229 kfree(inet_diag_table);
1230 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001233static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001235 sock_diag_unregister(&inet6_diag_handler);
1236 sock_diag_unregister(&inet_diag_handler);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001237 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001238 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239}
1240
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001241module_init(inet_diag_init);
1242module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +00001244MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1245MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);