blob: a89b68c6a9348d75df56d76e499cf3510ce4a7fb [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070048};
49
Herbert Xud523a322007-12-03 15:51:25 +110050static DEFINE_MUTEX(inet_diag_table_mutex);
51
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000052static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
Herbert Xud523a322007-12-03 15:51:25 +110053{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000054 if (!inet_diag_table[proto])
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +000055 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
56 NETLINK_SOCK_DIAG, 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
Eric Dumazetc8e2c802015-03-13 09:49:59 -070095static size_t inet_sk_attr_size(void)
96{
97 return nla_total_size(sizeof(struct tcp_info))
98 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
99 + nla_total_size(1) /* INET_DIAG_TOS */
100 + nla_total_size(1) /* INET_DIAG_TCLASS */
101 + nla_total_size(sizeof(struct inet_diag_meminfo))
102 + nla_total_size(sizeof(struct inet_diag_msg))
103 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
104 + nla_total_size(TCP_CA_NAME_MAX)
105 + nla_total_size(sizeof(struct tcpvegas_info))
106 + 64;
107}
108
Xin Longcb2050a2016-04-14 15:35:32 +0800109int inet_diag_msg_attrs_fill(struct sock *sk, struct sk_buff *skb,
110 struct inet_diag_msg *r, int ext,
111 struct user_namespace *user_ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700113 const struct inet_sock *inet = inet_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Pavel Emelyanove4e541a2012-10-23 22:29:56 +0400115 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
116 goto errout;
117
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500118 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
119 * hence this needs to be included regardless of socket family.
120 */
121 if (ext & (1 << (INET_DIAG_TOS - 1)))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000122 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
123 goto errout;
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500124
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000125#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300126 if (r->idiag_family == AF_INET6) {
Maciej Żenczykowski06236ac2011-11-07 14:23:11 +0000127 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
Eric Dumazetefe42082013-10-03 15:42:29 -0700128 if (nla_put_u8(skb, INET_DIAG_TCLASS,
129 inet6_sk(sk)->tclass) < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000130 goto errout;
Phil Sutter20462152015-06-24 11:02:51 +0200131
Phil Sutter8220ea22015-07-10 11:39:57 +0200132 if (((1 << sk->sk_state) & (TCPF_LISTEN | TCPF_CLOSE)) &&
133 nla_put_u8(skb, INET_DIAG_SKV6ONLY, ipv6_only_sock(sk)))
Phil Sutter20462152015-06-24 11:02:51 +0200134 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136#endif
137
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600138 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000139 r->idiag_inode = sock_i_ino(sk);
140
Xin Longcb2050a2016-04-14 15:35:32 +0800141 return 0;
142errout:
143 return 1;
144}
145EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill);
146
147int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
148 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
149 struct user_namespace *user_ns,
150 u32 portid, u32 seq, u16 nlmsg_flags,
151 const struct nlmsghdr *unlh)
152{
153 const struct tcp_congestion_ops *ca_ops;
154 const struct inet_diag_handler *handler;
155 int ext = req->idiag_ext;
156 struct inet_diag_msg *r;
157 struct nlmsghdr *nlh;
158 struct nlattr *attr;
159 void *info = NULL;
160
161 handler = inet_diag_table[req->sdiag_protocol];
162 BUG_ON(!handler);
163
164 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
165 nlmsg_flags);
166 if (!nlh)
167 return -EMSGSIZE;
168
169 r = nlmsg_data(nlh);
170 BUG_ON(!sk_fullsock(sk));
171
172 inet_diag_msg_common_fill(r, sk);
173 r->idiag_state = sk->sk_state;
174 r->idiag_timer = 0;
175 r->idiag_retrans = 0;
176
177 if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns))
178 goto errout;
179
Thomas Graf6e277ed2012-06-26 23:36:12 +0000180 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
181 struct inet_diag_meminfo minfo = {
182 .idiag_rmem = sk_rmem_alloc_get(sk),
183 .idiag_wmem = sk->sk_wmem_queued,
184 .idiag_fmem = sk->sk_forward_alloc,
185 .idiag_tmem = sk_wmem_alloc_get(sk),
186 };
187
188 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
189 goto errout;
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000190 }
191
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000192 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
193 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000194 goto errout;
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000195
Eric Dumazete31c5e02015-03-10 07:15:53 -0700196 if (!icsk) {
Shan Wei62ad6fc2012-04-24 18:15:41 +0000197 handler->idiag_get_info(sk, r, NULL);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000198 goto out;
199 }
200
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000201 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
202 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
203 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300204 r->idiag_timer = 1;
205 r->idiag_retrans = icsk->icsk_retransmits;
Xin Longb7de5292016-04-19 15:10:01 +0800206 r->idiag_expires =
207 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700208 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300209 r->idiag_timer = 4;
210 r->idiag_retrans = icsk->icsk_probes_out;
Xin Longb7de5292016-04-19 15:10:01 +0800211 r->idiag_expires =
212 jiffies_to_msecs(icsk->icsk_timeout - jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300214 r->idiag_timer = 2;
215 r->idiag_retrans = icsk->icsk_probes_out;
Xin Longb7de5292016-04-19 15:10:01 +0800216 r->idiag_expires =
217 jiffies_to_msecs(sk->sk_timer.expires - jiffies);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300219 r->idiag_timer = 0;
220 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300222
Craig Gallek3fd22af2015-06-15 11:26:19 -0400223 if ((ext & (1 << (INET_DIAG_INFO - 1))) && handler->idiag_info_size) {
Nicolas Dichtel6ed46d12016-04-26 10:06:14 +0200224 attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
225 handler->idiag_info_size,
226 INET_DIAG_PAD);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000227 if (!attr)
228 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Thomas Graf6e277ed2012-06-26 23:36:12 +0000230 info = nla_data(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 }
232
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700233 if (ext & (1 << (INET_DIAG_CONG - 1))) {
234 int err = 0;
235
236 rcu_read_lock();
237 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
238 if (ca_ops)
239 err = nla_put_string(skb, INET_DIAG_CONG, ca_ops->name);
240 rcu_read_unlock();
241 if (err < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000242 goto errout;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700243 }
Thomas Graf6e277ed2012-06-26 23:36:12 +0000244
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300245 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700247 if (sk->sk_state < TCP_TIME_WAIT) {
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700248 union tcp_cc_info info;
249 size_t sz = 0;
250 int attr;
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700251
252 rcu_read_lock();
253 ca_ops = READ_ONCE(icsk->icsk_ca_ops);
254 if (ca_ops && ca_ops->get_info)
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700255 sz = ca_ops->get_info(sk, ext, &attr, &info);
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700256 rcu_read_unlock();
Eric Dumazet64f40ff2015-04-28 16:23:48 -0700257 if (sz && nla_put(skb, attr, sz, &info) < 0)
Eric Dumazet521f1cf2015-04-16 18:10:35 -0700258 goto errout;
259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000261out:
Johannes Berg053c0952015-01-16 22:09:00 +0100262 nlmsg_end(skb, nlh);
263 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
Thomas Graf6e277ed2012-06-26 23:36:12 +0000265errout:
266 nlmsg_cancel(skb, nlh);
Patrick McHardy26932562007-01-31 23:16:40 -0800267 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000269EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
270
271static int inet_csk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700272 struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700273 const struct inet_diag_req_v2 *req,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600274 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000275 u32 portid, u32 seq, u16 nlmsg_flags,
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000276 const struct nlmsghdr *unlh)
277{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700278 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req,
279 user_ns, portid, seq, nlmsg_flags, unlh);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000280}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700282static int inet_twsk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700283 struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000284 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800285 const struct nlmsghdr *unlh)
286{
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700287 struct inet_timewait_sock *tw = inet_twsk(sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800288 struct inet_diag_msg *r;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000289 struct nlmsghdr *nlh;
Eric Dumazet789f5582015-04-12 18:51:09 -0700290 long tmo;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800291
Eric W. Biederman15e47302012-09-07 20:12:54 +0000292 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000293 nlmsg_flags);
294 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700295 return -EMSGSIZE;
David S. Millerd1063522012-06-26 21:28:54 -0700296
297 r = nlmsg_data(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800298 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
299
Eric Dumazet789f5582015-04-12 18:51:09 -0700300 tmo = tw->tw_timer.expires - jiffies;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800301 if (tmo < 0)
302 tmo = 0;
303
Eric Dumazeta4458342015-03-13 15:51:12 -0700304 inet_diag_msg_common_fill(r, sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800305 r->idiag_retrans = 0;
Daniel Borkmannb1aac812013-12-17 00:38:39 +0100306
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800307 r->idiag_state = tw->tw_substate;
308 r->idiag_timer = 3;
Eric Dumazet96f817f2013-10-03 14:27:25 -0700309 r->idiag_expires = jiffies_to_msecs(tmo);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800310 r->idiag_rqueue = 0;
311 r->idiag_wqueue = 0;
312 r->idiag_uid = 0;
313 r->idiag_inode = 0;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000314
Johannes Berg053c0952015-01-16 22:09:00 +0100315 nlmsg_end(skb, nlh);
316 return 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800317}
318
Eric Dumazeta58917f2015-03-15 21:12:14 -0700319static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
320 u32 portid, u32 seq, u16 nlmsg_flags,
321 const struct nlmsghdr *unlh)
322{
323 struct inet_diag_msg *r;
324 struct nlmsghdr *nlh;
325 long tmo;
326
327 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
328 nlmsg_flags);
329 if (!nlh)
330 return -EMSGSIZE;
331
332 r = nlmsg_data(nlh);
333 inet_diag_msg_common_fill(r, sk);
334 r->idiag_state = TCP_SYN_RECV;
335 r->idiag_timer = 1;
336 r->idiag_retrans = inet_reqsk(sk)->num_retrans;
337
338 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
339 offsetof(struct sock, sk_cookie));
340
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700341 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700342 r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
343 r->idiag_rqueue = 0;
344 r->idiag_wqueue = 0;
345 r->idiag_uid = 0;
346 r->idiag_inode = 0;
347
348 nlmsg_end(skb, nlh);
349 return 0;
350}
351
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800352static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700353 const struct inet_diag_req_v2 *r,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600354 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000355 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800356 const struct nlmsghdr *unlh)
357{
358 if (sk->sk_state == TCP_TIME_WAIT)
Eric Dumazeta58917f2015-03-15 21:12:14 -0700359 return inet_twsk_diag_fill(sk, skb, portid, seq,
Eric Dumazetefe42082013-10-03 15:42:29 -0700360 nlmsg_flags, unlh);
361
Eric Dumazeta58917f2015-03-15 21:12:14 -0700362 if (sk->sk_state == TCP_NEW_SYN_RECV)
363 return inet_req_diag_fill(sk, skb, portid, seq,
364 nlmsg_flags, unlh);
365
Eric Dumazetefe42082013-10-03 15:42:29 -0700366 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
367 nlmsg_flags, unlh);
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800368}
369
Lorenzo Colittib613f562015-12-16 12:30:02 +0900370struct sock *inet_diag_find_one_icsk(struct net *net,
371 struct inet_hashinfo *hashinfo,
372 const struct inet_diag_req_v2 *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700374 struct sock *sk;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300375
Eric Dumazet2d331912016-04-01 08:52:15 -0700376 rcu_read_lock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700377 if (req->sdiag_family == AF_INET)
Craig Galleka5836362016-02-10 11:50:38 -0500378 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300379 req->id.idiag_dport, req->id.idiag_src[0],
380 req->id.idiag_sport, req->id.idiag_if);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000381#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazet7c130672016-01-20 16:25:01 -0800382 else if (req->sdiag_family == AF_INET6) {
383 if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
384 ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
Craig Galleka5836362016-02-10 11:50:38 -0500385 sk = inet_lookup(net, hashinfo, NULL, 0, req->id.idiag_dst[3],
Eric Dumazet7c130672016-01-20 16:25:01 -0800386 req->id.idiag_dport, req->id.idiag_src[3],
387 req->id.idiag_sport, req->id.idiag_if);
388 else
Craig Galleka5836362016-02-10 11:50:38 -0500389 sk = inet6_lookup(net, hashinfo, NULL, 0,
Eric Dumazet7c130672016-01-20 16:25:01 -0800390 (struct in6_addr *)req->id.idiag_dst,
391 req->id.idiag_dport,
392 (struct in6_addr *)req->id.idiag_src,
393 req->id.idiag_sport,
394 req->id.idiag_if);
395 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396#endif
Eric Dumazet2d331912016-04-01 08:52:15 -0700397 else {
398 rcu_read_unlock();
Lorenzo Colittib613f562015-12-16 12:30:02 +0900399 return ERR_PTR(-EINVAL);
Eric Dumazet2d331912016-04-01 08:52:15 -0700400 }
401 rcu_read_unlock();
Eric Dumazete31c5e02015-03-10 07:15:53 -0700402 if (!sk)
Lorenzo Colittib613f562015-12-16 12:30:02 +0900403 return ERR_PTR(-ENOENT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Lorenzo Colittib613f562015-12-16 12:30:02 +0900405 if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
406 sock_gen_put(sk);
407 return ERR_PTR(-ENOENT);
408 }
409
410 return sk;
411}
412EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk);
413
414int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
415 struct sk_buff *in_skb,
416 const struct nlmsghdr *nlh,
417 const struct inet_diag_req_v2 *req)
418{
419 struct net *net = sock_net(in_skb->sk);
420 struct sk_buff *rep;
421 struct sock *sk;
422 int err;
423
424 sk = inet_diag_find_one_icsk(net, hashinfo, req);
425 if (IS_ERR(sk))
426 return PTR_ERR(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700428 rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000429 if (!rep) {
430 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 goto out;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000432 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000434 err = sk_diag_fill(sk, rep, req,
Patrick McHardye32123e2013-04-17 06:46:57 +0000435 sk_user_ns(NETLINK_CB(in_skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000436 NETLINK_CB(in_skb).portid,
Patrick McHardy26932562007-01-31 23:16:40 -0800437 nlh->nlmsg_seq, 0, nlh);
438 if (err < 0) {
439 WARN_ON(err == -EMSGSIZE);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000440 nlmsg_free(rep);
Patrick McHardy26932562007-01-31 23:16:40 -0800441 goto out;
442 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000443 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300444 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 if (err > 0)
446 err = 0;
447
448out:
Eric Dumazetc1d607c2013-10-11 08:54:49 -0700449 if (sk)
450 sock_gen_put(sk);
451
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000452 return err;
453}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000454EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000455
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900456static int inet_diag_cmd_exact(int cmd, struct sk_buff *in_skb,
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000457 const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700458 const struct inet_diag_req_v2 *req)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000459{
460 const struct inet_diag_handler *handler;
461 int err;
462
463 handler = inet_diag_lock_handler(req->sdiag_protocol);
464 if (IS_ERR(handler))
465 err = PTR_ERR(handler);
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900466 else if (cmd == SOCK_DIAG_BY_FAMILY)
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000467 err = handler->dump_one(in_skb, nlh, req);
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +0900468 else if (cmd == SOCK_DESTROY && handler->destroy)
469 err = handler->destroy(in_skb, req);
470 else
471 err = -EOPNOTSUPP;
Herbert Xud523a322007-12-03 15:51:25 +1100472 inet_diag_unlock_handler(handler);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000473
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 return err;
475}
476
Al Viro9f855292006-09-27 18:44:30 -0700477static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478{
479 int words = bits >> 5;
480
481 bits &= 0x1f;
482
483 if (words) {
484 if (memcmp(a1, a2, words << 2))
485 return 0;
486 }
487 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700488 __be32 w1, w2;
489 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
491 w1 = a1[words];
492 w2 = a2[words];
493
494 mask = htonl((0xffffffff) << (32 - bits));
495
496 if ((w1 ^ w2) & mask)
497 return 0;
498 }
499
500 return 1;
501}
502
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000503static int inet_diag_bc_run(const struct nlattr *_bc,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700504 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000506 const void *bc = nla_data(_bc);
507 int len = nla_len(_bc);
508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 while (len > 0) {
510 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300511 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512
513 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300514 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300516 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 yes = 0;
518 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300519 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 yes = entry->sport >= op[1].no;
521 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300522 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800523 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300525 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 yes = entry->dport >= op[1].no;
527 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300528 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 yes = entry->dport <= op[1].no;
530 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300531 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
533 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300534 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300535 case INET_DIAG_BC_D_COND: {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700536 const struct inet_diag_hostcond *cond;
537 const __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538
Eric Dumazete31c5e02015-03-10 07:15:53 -0700539 cond = (const struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300541 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 entry->sport : entry->dport)) {
543 yes = 0;
544 break;
545 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800546
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300547 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 addr = entry->saddr;
549 else
550 addr = entry->daddr;
551
Neal Cardwellf67caec2012-12-08 19:43:23 +0000552 if (cond->family != AF_UNSPEC &&
553 cond->family != entry->family) {
554 if (entry->family == AF_INET6 &&
555 cond->family == AF_INET) {
556 if (addr[0] == 0 && addr[1] == 0 &&
557 addr[2] == htonl(0xffff) &&
558 bitstring_match(addr + 3,
559 cond->addr,
560 cond->prefix_len))
561 break;
562 }
563 yes = 0;
564 break;
565 }
566
567 if (cond->prefix_len == 0)
568 break;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800569 if (bitstring_match(addr, cond->addr,
570 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 yes = 0;
573 break;
574 }
David Ahern637c8412016-06-23 18:42:51 -0700575 case INET_DIAG_BC_DEV_COND: {
576 u32 ifindex;
577
578 ifindex = *((const u32 *)(op + 1));
579 if (ifindex != entry->ifindex)
580 yes = 0;
581 break;
582 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800585 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 len -= op->yes;
587 bc += op->yes;
588 } else {
589 len -= op->no;
590 bc += op->no;
591 }
592 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000593 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594}
595
Eric Dumazeta4458342015-03-13 15:51:12 -0700596/* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
597 */
598static void entry_fill_addrs(struct inet_diag_entry *entry,
599 const struct sock *sk)
600{
601#if IS_ENABLED(CONFIG_IPV6)
602 if (sk->sk_family == AF_INET6) {
603 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
604 entry->daddr = sk->sk_v6_daddr.s6_addr32;
605 } else
606#endif
607 {
608 entry->saddr = &sk->sk_rcv_saddr;
609 entry->daddr = &sk->sk_daddr;
610 }
611}
612
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000613int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
614{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000615 struct inet_sock *inet = inet_sk(sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700616 struct inet_diag_entry entry;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000617
Eric Dumazete31c5e02015-03-10 07:15:53 -0700618 if (!bc)
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000619 return 1;
620
621 entry.family = sk->sk_family;
Eric Dumazeta4458342015-03-13 15:51:12 -0700622 entry_fill_addrs(&entry, sk);
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000623 entry.sport = inet->inet_num;
624 entry.dport = ntohs(inet->inet_dport);
David Ahern637c8412016-06-23 18:42:51 -0700625 entry.ifindex = sk->sk_bound_dev_if;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700626 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000627
628 return inet_diag_bc_run(bc, &entry);
629}
630EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632static int valid_cc(const void *bc, int len, int cc)
633{
634 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300635 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
637 if (cc > len)
638 return 0;
639 if (cc == len)
640 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400641 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 0;
643 len -= op->yes;
644 bc += op->yes;
645 }
646 return 0;
647}
648
David Ahern637c8412016-06-23 18:42:51 -0700649/* data is u32 ifindex */
650static bool valid_devcond(const struct inet_diag_bc_op *op, int len,
651 int *min_len)
652{
653 /* Check ifindex space. */
654 *min_len += sizeof(u32);
655 if (len < *min_len)
656 return false;
657
658 return true;
659}
Neal Cardwell405c0052012-12-08 19:43:22 +0000660/* Validate an inet_diag_hostcond. */
661static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
662 int *min_len)
663{
Neal Cardwell405c0052012-12-08 19:43:22 +0000664 struct inet_diag_hostcond *cond;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700665 int addr_len;
Neal Cardwell405c0052012-12-08 19:43:22 +0000666
667 /* Check hostcond space. */
668 *min_len += sizeof(struct inet_diag_hostcond);
669 if (len < *min_len)
670 return false;
671 cond = (struct inet_diag_hostcond *)(op + 1);
672
673 /* Check address family and address length. */
674 switch (cond->family) {
675 case AF_UNSPEC:
676 addr_len = 0;
677 break;
678 case AF_INET:
679 addr_len = sizeof(struct in_addr);
680 break;
681 case AF_INET6:
682 addr_len = sizeof(struct in6_addr);
683 break;
684 default:
685 return false;
686 }
687 *min_len += addr_len;
688 if (len < *min_len)
689 return false;
690
691 /* Check prefix length (in bits) vs address length (in bytes). */
692 if (cond->prefix_len > 8 * addr_len)
693 return false;
694
695 return true;
696}
697
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000698/* Validate a port comparison operator. */
Eric Dumazete31c5e02015-03-10 07:15:53 -0700699static bool valid_port_comparison(const struct inet_diag_bc_op *op,
700 int len, int *min_len)
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000701{
702 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
703 *min_len += sizeof(struct inet_diag_bc_op);
704 if (len < *min_len)
705 return false;
706 return true;
707}
708
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +0900709static int inet_diag_bc_audit(const struct nlattr *attr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +0900711 const void *bytecode, *bc;
712 int bytecode_len, len;
713
714 if (!attr || nla_len(attr) < sizeof(struct inet_diag_bc_op))
715 return -EINVAL;
716
717 bytecode = bc = nla_data(attr);
718 len = bytecode_len = nla_len(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 while (len > 0) {
Neal Cardwell405c0052012-12-08 19:43:22 +0000721 int min_len = sizeof(struct inet_diag_bc_op);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700722 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300725 case INET_DIAG_BC_S_COND:
726 case INET_DIAG_BC_D_COND:
Neal Cardwell405c0052012-12-08 19:43:22 +0000727 if (!valid_hostcond(bc, len, &min_len))
728 return -EINVAL;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000729 break;
David Ahern637c8412016-06-23 18:42:51 -0700730 case INET_DIAG_BC_DEV_COND:
731 if (!valid_devcond(bc, len, &min_len))
732 return -EINVAL;
733 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300734 case INET_DIAG_BC_S_GE:
735 case INET_DIAG_BC_S_LE:
736 case INET_DIAG_BC_D_GE:
737 case INET_DIAG_BC_D_LE:
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000738 if (!valid_port_comparison(bc, len, &min_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 return -EINVAL;
740 break;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000741 case INET_DIAG_BC_AUTO:
742 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300743 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 break;
745 default:
746 return -EINVAL;
747 }
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000748
749 if (op->code != INET_DIAG_BC_NOP) {
750 if (op->no < min_len || op->no > len + 4 || op->no & 3)
751 return -EINVAL;
752 if (op->no < len &&
753 !valid_cc(bytecode, bytecode_len, len - op->no))
754 return -EINVAL;
755 }
756
Neal Cardwell405c0052012-12-08 19:43:22 +0000757 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
Eric Dumazeteeb14972011-06-17 16:25:39 -0400758 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800759 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 len -= op->yes;
761 }
762 return len == 0 ? 0 : -EINVAL;
763}
764
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800765static int inet_csk_diag_dump(struct sock *sk,
766 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000767 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700768 const struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000769 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000771 if (!inet_diag_bc_sk(bc, sk))
772 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000774 return inet_csk_diag_fill(sk, skb, r,
Patrick McHardye32123e2013-04-17 06:46:57 +0000775 sk_user_ns(NETLINK_CB(cb->skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000776 NETLINK_CB(cb->skb).portid,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800777 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
779
Eric Dumazet49612722015-03-05 10:18:14 -0800780static void twsk_build_assert(void)
781{
782 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
783 offsetof(struct sock, sk_family));
784
785 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
786 offsetof(struct inet_sock, inet_num));
787
788 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
789 offsetof(struct inet_sock, inet_dport));
790
791 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
792 offsetof(struct inet_sock, inet_rcv_saddr));
793
794 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
795 offsetof(struct inet_sock, inet_daddr));
796
797#if IS_ENABLED(CONFIG_IPV6)
798 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
799 offsetof(struct sock, sk_v6_rcv_saddr));
800
801 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
802 offsetof(struct sock, sk_v6_daddr));
803#endif
804}
805
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000806void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700807 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700808 const struct inet_diag_req_v2 *r, struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000810 struct net *net = sock_net(skb->sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700811 int i, num, s_i, s_num;
Eric Dumazet079096f2015-10-02 11:43:32 -0700812 u32 idiag_states = r->idiag_states;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800813
Eric Dumazet079096f2015-10-02 11:43:32 -0700814 if (idiag_states & TCPF_SYN_RECV)
815 idiag_states |= TCPF_NEW_SYN_RECV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 s_i = cb->args[1];
817 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300818
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 if (cb->args[0] == 0) {
Eric Dumazet079096f2015-10-02 11:43:32 -0700820 if (!(idiag_states & TCPF_LISTEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300822
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700823 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800824 struct inet_listen_hashbucket *ilb;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700825 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826
827 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800828 ilb = &hashinfo->listening_hash[i];
829 spin_lock_bh(&ilb->lock);
Eric Dumazet3b24d852016-04-01 08:52:17 -0700830 sk_for_each(sk, &ilb->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 struct inet_sock *inet = inet_sk(sk);
832
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000833 if (!net_eq(sock_net(sk), net))
834 continue;
835
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 if (num < s_num) {
837 num++;
838 continue;
839 }
840
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000841 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazete31c5e02015-03-10 07:15:53 -0700842 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000843 goto next_listen;
844
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000845 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300846 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto next_listen;
848
Eric Dumazet079096f2015-10-02 11:43:32 -0700849 if (r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 cb->args[3] > 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 goto next_listen;
852
Eric Dumazet079096f2015-10-02 11:43:32 -0700853 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800854 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 goto done;
856 }
857
858next_listen:
859 cb->args[3] = 0;
860 cb->args[4] = 0;
861 ++num;
862 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800863 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 s_num = 0;
866 cb->args[3] = 0;
867 cb->args[4] = 0;
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869skip_listen_ht:
870 cb->args[0] = 1;
871 s_i = num = s_num = 0;
872 }
873
Eric Dumazet079096f2015-10-02 11:43:32 -0700874 if (!(idiag_states & ~TCPF_LISTEN))
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000875 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Eric Dumazetf373b532009-10-09 00:16:19 +0000877 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300878 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800879 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800880 struct hlist_nulls_node *node;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700881 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Andi Kleen6be547a2008-08-28 01:09:54 -0700883 num = 0;
884
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700885 if (hlist_nulls_empty(&head->chain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700886 continue;
887
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 if (i > s_i)
889 s_num = 0;
890
David S. Miller7e3aab42008-11-21 16:39:19 -0800891 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800892 sk_nulls_for_each(sk, node, &head->chain) {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700893 int state, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000895 if (!net_eq(sock_net(sk), net))
896 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 if (num < s_num)
898 goto next_normal;
Neal Cardwell70315d22014-01-10 15:34:45 -0500899 state = (sk->sk_state == TCP_TIME_WAIT) ?
900 inet_twsk(sk)->tw_substate : sk->sk_state;
Eric Dumazet079096f2015-10-02 11:43:32 -0700901 if (!(idiag_states & (1 << state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto next_normal;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000903 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700904 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000905 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700906 if (r->id.idiag_sport != htons(sk->sk_num) &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300907 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700909 if (r->id.idiag_dport != sk->sk_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800910 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 goto next_normal;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700912 twsk_build_assert();
913
914 if (!inet_diag_bc_sk(bc, sk))
915 goto next_normal;
916
917 res = sk_diag_fill(sk, skb, r,
918 sk_user_ns(NETLINK_CB(cb->skb).sk),
919 NETLINK_CB(cb->skb).portid,
920 cb->nlh->nlmsg_seq, NLM_F_MULTI,
921 cb->nlh);
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700922 if (res < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800923 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 goto done;
925 }
926next_normal:
927 ++num;
928 }
929
David S. Miller7e3aab42008-11-21 16:39:19 -0800930 spin_unlock_bh(lock);
Eric Dumazetacffb582016-03-14 15:40:00 -0700931 cond_resched();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 }
933
934done:
935 cb->args[1] = i;
936 cb->args[2] = num;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000937out:
938 ;
939}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000940EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000941
942static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700943 const struct inet_diag_req_v2 *r,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700944 struct nlattr *bc)
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000945{
946 const struct inet_diag_handler *handler;
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000947 int err = 0;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000948
949 handler = inet_diag_lock_handler(r->sdiag_protocol);
950 if (!IS_ERR(handler))
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000951 handler->dump(skb, cb, r, bc);
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000952 else
953 err = PTR_ERR(handler);
Herbert Xud523a322007-12-03 15:51:25 +1100954 inet_diag_unlock_handler(handler);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000955
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000956 return err ? : skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957}
958
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000959static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
960{
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000961 int hdrlen = sizeof(struct inet_diag_req_v2);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700962 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000963
964 if (nlmsg_attrlen(cb->nlh, hdrlen))
965 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
966
David S. Millerd1063522012-06-26 21:28:54 -0700967 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000968}
969
Eric Dumazete31c5e02015-03-10 07:15:53 -0700970static int inet_diag_type2proto(int type)
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000971{
972 switch (type) {
973 case TCPDIAG_GETSOCK:
974 return IPPROTO_TCP;
975 case DCCPDIAG_GETSOCK:
976 return IPPROTO_DCCP;
977 default:
978 return 0;
979 }
980}
981
Eric Dumazete31c5e02015-03-10 07:15:53 -0700982static int inet_diag_dump_compat(struct sk_buff *skb,
983 struct netlink_callback *cb)
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000984{
David S. Millerd1063522012-06-26 21:28:54 -0700985 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700986 int hdrlen = sizeof(struct inet_diag_req);
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000987 struct inet_diag_req_v2 req;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000988 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000989
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000990 req.sdiag_family = AF_UNSPEC; /* compatibility */
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000991 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
992 req.idiag_ext = rc->idiag_ext;
993 req.idiag_states = rc->idiag_states;
994 req.id = rc->id;
995
996 if (nlmsg_attrlen(cb->nlh, hdrlen))
997 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
998
999 return __inet_diag_dump(skb, cb, &req, bc);
1000}
1001
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001002static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -07001003 const struct nlmsghdr *nlh)
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001004{
David S. Millerd1063522012-06-26 21:28:54 -07001005 struct inet_diag_req *rc = nlmsg_data(nlh);
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001006 struct inet_diag_req_v2 req;
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001007
1008 req.sdiag_family = rc->idiag_family;
1009 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
1010 req.idiag_ext = rc->idiag_ext;
1011 req.idiag_states = rc->idiag_states;
1012 req.id = rc->id;
1013
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001014 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY, in_skb, nlh, &req);
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001015}
1016
Pavel Emelyanov8d341722011-12-06 07:57:06 +00001017static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
Pavel Emelyanov3b09c842012-01-10 22:37:26 +00001019 int hdrlen = sizeof(struct inet_diag_req);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001020 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Thomas Grafead592b2007-03-22 23:30:35 -07001022 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1023 nlmsg_len(nlh) < hdrlen)
1024 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
David S. Millerb8f3ab42011-01-18 12:40:38 -08001026 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -07001027 if (nlmsg_attrlen(nlh, hdrlen)) {
1028 struct nlattr *attr;
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001029 int err;
Thomas Grafead592b2007-03-22 23:30:35 -07001030
1031 attr = nlmsg_find_attr(nlh, hdrlen,
1032 INET_DIAG_REQ_BYTECODE);
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001033 err = inet_diag_bc_audit(attr);
1034 if (err)
1035 return err;
Thomas Grafead592b2007-03-22 23:30:35 -07001036 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001037 {
1038 struct netlink_dump_control c = {
1039 .dump = inet_diag_dump_compat,
1040 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001041 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
Thomas Grafead592b2007-03-22 23:30:35 -07001044
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001045 return inet_diag_get_exact_compat(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001048static int inet_diag_handler_cmd(struct sk_buff *skb, struct nlmsghdr *h)
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001049{
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001050 int hdrlen = sizeof(struct inet_diag_req_v2);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001051 struct net *net = sock_net(skb->sk);
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001052
1053 if (nlmsg_len(h) < hdrlen)
1054 return -EINVAL;
1055
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001056 if (h->nlmsg_type == SOCK_DIAG_BY_FAMILY &&
1057 h->nlmsg_flags & NLM_F_DUMP) {
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001058 if (nlmsg_attrlen(h, hdrlen)) {
1059 struct nlattr *attr;
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001060 int err;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001061
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001062 attr = nlmsg_find_attr(h, hdrlen,
1063 INET_DIAG_REQ_BYTECODE);
Lorenzo Colitti627cc4a2016-08-24 15:46:25 +09001064 err = inet_diag_bc_audit(attr);
1065 if (err)
1066 return err;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001067 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001068 {
1069 struct netlink_dump_control c = {
1070 .dump = inet_diag_dump,
1071 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001072 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001073 }
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001074 }
1075
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001076 return inet_diag_cmd_exact(h->nlmsg_type, skb, h, nlmsg_data(h));
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001077}
1078
Craig Gallek35ac8382015-06-15 11:26:20 -04001079static
1080int inet_diag_handler_get_info(struct sk_buff *skb, struct sock *sk)
1081{
1082 const struct inet_diag_handler *handler;
1083 struct nlmsghdr *nlh;
1084 struct nlattr *attr;
1085 struct inet_diag_msg *r;
1086 void *info = NULL;
1087 int err = 0;
1088
1089 nlh = nlmsg_put(skb, 0, 0, SOCK_DIAG_BY_FAMILY, sizeof(*r), 0);
1090 if (!nlh)
1091 return -ENOMEM;
1092
1093 r = nlmsg_data(nlh);
1094 memset(r, 0, sizeof(*r));
1095 inet_diag_msg_common_fill(r, sk);
Craig Galleke0df02e2015-06-17 10:59:10 -04001096 if (sk->sk_type == SOCK_DGRAM || sk->sk_type == SOCK_STREAM)
1097 r->id.idiag_sport = inet_sk(sk)->inet_sport;
Craig Gallek35ac8382015-06-15 11:26:20 -04001098 r->idiag_state = sk->sk_state;
1099
1100 if ((err = nla_put_u8(skb, INET_DIAG_PROTOCOL, sk->sk_protocol))) {
1101 nlmsg_cancel(skb, nlh);
1102 return err;
1103 }
1104
1105 handler = inet_diag_lock_handler(sk->sk_protocol);
1106 if (IS_ERR(handler)) {
1107 inet_diag_unlock_handler(handler);
1108 nlmsg_cancel(skb, nlh);
1109 return PTR_ERR(handler);
1110 }
1111
1112 attr = handler->idiag_info_size
Nicolas Dichtel6ed46d12016-04-26 10:06:14 +02001113 ? nla_reserve_64bit(skb, INET_DIAG_INFO,
1114 handler->idiag_info_size,
1115 INET_DIAG_PAD)
Craig Gallek35ac8382015-06-15 11:26:20 -04001116 : NULL;
1117 if (attr)
1118 info = nla_data(attr);
1119
1120 handler->idiag_get_info(sk, r, info);
1121 inet_diag_unlock_handler(handler);
1122
1123 nlmsg_end(skb, nlh);
1124 return 0;
1125}
1126
Shan Wei8dcf01f2012-04-24 18:21:07 +00001127static const struct sock_diag_handler inet_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001128 .family = AF_INET,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001129 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001130 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001131 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001132};
1133
Shan Wei8dcf01f2012-04-24 18:21:07 +00001134static const struct sock_diag_handler inet6_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001135 .family = AF_INET6,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001136 .dump = inet_diag_handler_cmd,
Craig Gallek35ac8382015-06-15 11:26:20 -04001137 .get_info = inet_diag_handler_get_info,
Lorenzo Colitti6eb5d2e2015-12-16 12:30:04 +09001138 .destroy = inet_diag_handler_cmd,
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001139};
1140
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001141int inet_diag_register(const struct inet_diag_handler *h)
1142{
1143 const __u16 type = h->idiag_type;
1144 int err = -EINVAL;
1145
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001146 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001147 goto out;
1148
Herbert Xud523a322007-12-03 15:51:25 +11001149 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001150 err = -EEXIST;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001151 if (!inet_diag_table[type]) {
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001152 inet_diag_table[type] = h;
1153 err = 0;
1154 }
Herbert Xud523a322007-12-03 15:51:25 +11001155 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001156out:
1157 return err;
1158}
1159EXPORT_SYMBOL_GPL(inet_diag_register);
1160
1161void inet_diag_unregister(const struct inet_diag_handler *h)
1162{
1163 const __u16 type = h->idiag_type;
1164
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001165 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001166 return;
1167
Herbert Xud523a322007-12-03 15:51:25 +11001168 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001169 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +11001170 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001171}
1172EXPORT_SYMBOL_GPL(inet_diag_unregister);
1173
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001174static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001176 const int inet_diag_table_size = (IPPROTO_MAX *
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001177 sizeof(struct inet_diag_handler *));
1178 int err = -ENOMEM;
1179
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001180 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001181 if (!inet_diag_table)
1182 goto out;
1183
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001184 err = sock_diag_register(&inet_diag_handler);
1185 if (err)
1186 goto out_free_nl;
1187
1188 err = sock_diag_register(&inet6_diag_handler);
1189 if (err)
1190 goto out_free_inet;
1191
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001192 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001193out:
1194 return err;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001195
1196out_free_inet:
1197 sock_diag_unregister(&inet_diag_handler);
1198out_free_nl:
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001199 kfree(inet_diag_table);
1200 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201}
1202
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001203static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001205 sock_diag_unregister(&inet6_diag_handler);
1206 sock_diag_unregister(&inet_diag_handler);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001207 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001208 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001211module_init(inet_diag_init);
1212module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +00001214MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1215MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);