blob: 70e8b3c308ec3bd5c3069d87558b8b24d644ed52 [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;
47};
48
Herbert Xud523a322007-12-03 15:51:25 +110049static DEFINE_MUTEX(inet_diag_table_mutex);
50
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000051static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
Herbert Xud523a322007-12-03 15:51:25 +110052{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000053 if (!inet_diag_table[proto])
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +000054 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK,
55 NETLINK_SOCK_DIAG, AF_INET, proto);
Herbert Xud523a322007-12-03 15:51:25 +110056
57 mutex_lock(&inet_diag_table_mutex);
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000058 if (!inet_diag_table[proto])
Herbert Xud523a322007-12-03 15:51:25 +110059 return ERR_PTR(-ENOENT);
60
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000061 return inet_diag_table[proto];
Herbert Xud523a322007-12-03 15:51:25 +110062}
63
Eric Dumazete31c5e02015-03-10 07:15:53 -070064static void inet_diag_unlock_handler(const struct inet_diag_handler *handler)
Herbert Xud523a322007-12-03 15:51:25 +110065{
66 mutex_unlock(&inet_diag_table_mutex);
67}
68
Eric Dumazeta4458342015-03-13 15:51:12 -070069static void inet_diag_msg_common_fill(struct inet_diag_msg *r, struct sock *sk)
70{
71 r->idiag_family = sk->sk_family;
72
73 r->id.idiag_sport = htons(sk->sk_num);
74 r->id.idiag_dport = sk->sk_dport;
75 r->id.idiag_if = sk->sk_bound_dev_if;
76 sock_diag_save_cookie(sk, r->id.idiag_cookie);
77
78#if IS_ENABLED(CONFIG_IPV6)
79 if (sk->sk_family == AF_INET6) {
80 *(struct in6_addr *)r->id.idiag_src = sk->sk_v6_rcv_saddr;
81 *(struct in6_addr *)r->id.idiag_dst = sk->sk_v6_daddr;
82 } else
83#endif
84 {
85 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
86 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
87
88 r->id.idiag_src[0] = sk->sk_rcv_saddr;
89 r->id.idiag_dst[0] = sk->sk_daddr;
90 }
91}
92
Eric Dumazetc8e2c802015-03-13 09:49:59 -070093static size_t inet_sk_attr_size(void)
94{
95 return nla_total_size(sizeof(struct tcp_info))
96 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
97 + nla_total_size(1) /* INET_DIAG_TOS */
98 + nla_total_size(1) /* INET_DIAG_TCLASS */
99 + nla_total_size(sizeof(struct inet_diag_meminfo))
100 + nla_total_size(sizeof(struct inet_diag_msg))
101 + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
102 + nla_total_size(TCP_CA_NAME_MAX)
103 + nla_total_size(sizeof(struct tcpvegas_info))
104 + 64;
105}
106
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000107int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700108 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700109 struct user_namespace *user_ns,
110 u32 portid, u32 seq, u16 nlmsg_flags,
111 const struct nlmsghdr *unlh)
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);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700114 const struct inet_diag_handler *handler;
115 int ext = req->idiag_ext;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300116 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 struct nlmsghdr *nlh;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000118 struct nlattr *attr;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300119 void *info = NULL;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300120
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000121 handler = inet_diag_table[req->sdiag_protocol];
Eric Dumazete31c5e02015-03-10 07:15:53 -0700122 BUG_ON(!handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Eric W. Biederman15e47302012-09-07 20:12:54 +0000124 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000125 nlmsg_flags);
126 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700127 return -EMSGSIZE;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300128
David S. Millerd1063522012-06-26 21:28:54 -0700129 r = nlmsg_data(nlh);
Eric Dumazeta58917f2015-03-15 21:12:14 -0700130 BUG_ON(!sk_fullsock(sk));
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800131
Eric Dumazeta4458342015-03-13 15:51:12 -0700132 inet_diag_msg_common_fill(r, sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300133 r->idiag_state = sk->sk_state;
134 r->idiag_timer = 0;
135 r->idiag_retrans = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Pavel Emelyanove4e541a2012-10-23 22:29:56 +0400137 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
138 goto errout;
139
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500140 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
141 * hence this needs to be included regardless of socket family.
142 */
143 if (ext & (1 << (INET_DIAG_TOS - 1)))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000144 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
145 goto errout;
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500146
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000147#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300148 if (r->idiag_family == AF_INET6) {
Maciej Żenczykowski06236ac2011-11-07 14:23:11 +0000149 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
Eric Dumazetefe42082013-10-03 15:42:29 -0700150 if (nla_put_u8(skb, INET_DIAG_TCLASS,
151 inet6_sk(sk)->tclass) < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000152 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 }
154#endif
155
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600156 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000157 r->idiag_inode = sock_i_ino(sk);
158
Thomas Graf6e277ed2012-06-26 23:36:12 +0000159 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
160 struct inet_diag_meminfo minfo = {
161 .idiag_rmem = sk_rmem_alloc_get(sk),
162 .idiag_wmem = sk->sk_wmem_queued,
163 .idiag_fmem = sk->sk_forward_alloc,
164 .idiag_tmem = sk_wmem_alloc_get(sk),
165 };
166
167 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
168 goto errout;
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000169 }
170
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000171 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
172 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000173 goto errout;
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000174
Eric Dumazete31c5e02015-03-10 07:15:53 -0700175 if (!icsk) {
Shan Wei62ad6fc2012-04-24 18:15:41 +0000176 handler->idiag_get_info(sk, r, NULL);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000177 goto out;
178 }
179
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700180#define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000182 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
183 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
184 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300185 r->idiag_timer = 1;
186 r->idiag_retrans = icsk->icsk_retransmits;
187 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700188 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300189 r->idiag_timer = 4;
190 r->idiag_retrans = icsk->icsk_probes_out;
191 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300193 r->idiag_timer = 2;
194 r->idiag_retrans = icsk->icsk_probes_out;
195 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300197 r->idiag_timer = 0;
198 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 }
200#undef EXPIRES_IN_MS
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300201
Thomas Graf6e277ed2012-06-26 23:36:12 +0000202 if (ext & (1 << (INET_DIAG_INFO - 1))) {
203 attr = nla_reserve(skb, INET_DIAG_INFO,
204 sizeof(struct tcp_info));
205 if (!attr)
206 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Thomas Graf6e277ed2012-06-26 23:36:12 +0000208 info = nla_data(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 }
210
Thomas Graf6e277ed2012-06-26 23:36:12 +0000211 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops)
212 if (nla_put_string(skb, INET_DIAG_CONG,
213 icsk->icsk_ca_ops->name) < 0)
214 goto errout;
215
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300216 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300218 if (sk->sk_state < TCP_TIME_WAIT &&
219 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
220 icsk->icsk_ca_ops->get_info(sk, ext, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000222out:
Johannes Berg053c0952015-01-16 22:09:00 +0100223 nlmsg_end(skb, nlh);
224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Thomas Graf6e277ed2012-06-26 23:36:12 +0000226errout:
227 nlmsg_cancel(skb, nlh);
Patrick McHardy26932562007-01-31 23:16:40 -0800228 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000230EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
231
232static int inet_csk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700233 struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700234 const struct inet_diag_req_v2 *req,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600235 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000236 u32 portid, u32 seq, u16 nlmsg_flags,
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000237 const struct nlmsghdr *unlh)
238{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700239 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req,
240 user_ns, portid, seq, nlmsg_flags, unlh);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000241}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700243static int inet_twsk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700244 struct sk_buff *skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000245 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800246 const struct nlmsghdr *unlh)
247{
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700248 struct inet_timewait_sock *tw = inet_twsk(sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800249 struct inet_diag_msg *r;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000250 struct nlmsghdr *nlh;
Eric Dumazet789f5582015-04-12 18:51:09 -0700251 long tmo;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800252
Eric W. Biederman15e47302012-09-07 20:12:54 +0000253 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000254 nlmsg_flags);
255 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700256 return -EMSGSIZE;
David S. Millerd1063522012-06-26 21:28:54 -0700257
258 r = nlmsg_data(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800259 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
260
Eric Dumazet789f5582015-04-12 18:51:09 -0700261 tmo = tw->tw_timer.expires - jiffies;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800262 if (tmo < 0)
263 tmo = 0;
264
Eric Dumazeta4458342015-03-13 15:51:12 -0700265 inet_diag_msg_common_fill(r, sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800266 r->idiag_retrans = 0;
Daniel Borkmannb1aac812013-12-17 00:38:39 +0100267
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800268 r->idiag_state = tw->tw_substate;
269 r->idiag_timer = 3;
Eric Dumazet96f817f2013-10-03 14:27:25 -0700270 r->idiag_expires = jiffies_to_msecs(tmo);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800271 r->idiag_rqueue = 0;
272 r->idiag_wqueue = 0;
273 r->idiag_uid = 0;
274 r->idiag_inode = 0;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000275
Johannes Berg053c0952015-01-16 22:09:00 +0100276 nlmsg_end(skb, nlh);
277 return 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800278}
279
Eric Dumazeta58917f2015-03-15 21:12:14 -0700280static int inet_req_diag_fill(struct sock *sk, struct sk_buff *skb,
281 u32 portid, u32 seq, u16 nlmsg_flags,
282 const struct nlmsghdr *unlh)
283{
284 struct inet_diag_msg *r;
285 struct nlmsghdr *nlh;
286 long tmo;
287
288 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
289 nlmsg_flags);
290 if (!nlh)
291 return -EMSGSIZE;
292
293 r = nlmsg_data(nlh);
294 inet_diag_msg_common_fill(r, sk);
295 r->idiag_state = TCP_SYN_RECV;
296 r->idiag_timer = 1;
297 r->idiag_retrans = inet_reqsk(sk)->num_retrans;
298
299 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
300 offsetof(struct sock, sk_cookie));
301
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700302 tmo = inet_reqsk(sk)->rsk_timer.expires - jiffies;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700303 r->idiag_expires = (tmo >= 0) ? jiffies_to_msecs(tmo) : 0;
304 r->idiag_rqueue = 0;
305 r->idiag_wqueue = 0;
306 r->idiag_uid = 0;
307 r->idiag_inode = 0;
308
309 nlmsg_end(skb, nlh);
310 return 0;
311}
312
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800313static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700314 const struct inet_diag_req_v2 *r,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600315 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000316 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800317 const struct nlmsghdr *unlh)
318{
319 if (sk->sk_state == TCP_TIME_WAIT)
Eric Dumazeta58917f2015-03-15 21:12:14 -0700320 return inet_twsk_diag_fill(sk, skb, portid, seq,
Eric Dumazetefe42082013-10-03 15:42:29 -0700321 nlmsg_flags, unlh);
322
Eric Dumazeta58917f2015-03-15 21:12:14 -0700323 if (sk->sk_state == TCP_NEW_SYN_RECV)
324 return inet_req_diag_fill(sk, skb, portid, seq,
325 nlmsg_flags, unlh);
326
Eric Dumazetefe42082013-10-03 15:42:29 -0700327 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
328 nlmsg_flags, unlh);
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800329}
330
Eric Dumazete31c5e02015-03-10 07:15:53 -0700331int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
332 struct sk_buff *in_skb,
333 const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700334 const struct inet_diag_req_v2 *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000336 struct net *net = sock_net(in_skb->sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700337 struct sk_buff *rep;
338 struct sock *sk;
339 int err;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300340
Herbert Xud523a322007-12-03 15:51:25 +1100341 err = -EINVAL;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700342 if (req->sdiag_family == AF_INET)
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000343 sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300344 req->id.idiag_dport, req->id.idiag_src[0],
345 req->id.idiag_sport, req->id.idiag_if);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000346#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazete31c5e02015-03-10 07:15:53 -0700347 else if (req->sdiag_family == AF_INET6)
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000348 sk = inet6_lookup(net, hashinfo,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300349 (struct in6_addr *)req->id.idiag_dst,
350 req->id.idiag_dport,
351 (struct in6_addr *)req->id.idiag_src,
352 req->id.idiag_sport,
353 req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354#endif
Eric Dumazete31c5e02015-03-10 07:15:53 -0700355 else
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000356 goto out_nosk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
Herbert Xud523a322007-12-03 15:51:25 +1100358 err = -ENOENT;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700359 if (!sk)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000360 goto out_nosk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +0000362 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
Pavel Emelyanovb005ab42011-12-09 06:21:53 +0000363 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 goto out;
365
Eric Dumazetc8e2c802015-03-13 09:49:59 -0700366 rep = nlmsg_new(inet_sk_attr_size(), GFP_KERNEL);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000367 if (!rep) {
368 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 goto out;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000370 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000372 err = sk_diag_fill(sk, rep, req,
Patrick McHardye32123e2013-04-17 06:46:57 +0000373 sk_user_ns(NETLINK_CB(in_skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000374 NETLINK_CB(in_skb).portid,
Patrick McHardy26932562007-01-31 23:16:40 -0800375 nlh->nlmsg_seq, 0, nlh);
376 if (err < 0) {
377 WARN_ON(err == -EMSGSIZE);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000378 nlmsg_free(rep);
Patrick McHardy26932562007-01-31 23:16:40 -0800379 goto out;
380 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000381 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300382 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (err > 0)
384 err = 0;
385
386out:
Eric Dumazetc1d607c2013-10-11 08:54:49 -0700387 if (sk)
388 sock_gen_put(sk);
389
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000390out_nosk:
391 return err;
392}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000393EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000394
395static int inet_diag_get_exact(struct sk_buff *in_skb,
396 const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700397 const struct inet_diag_req_v2 *req)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000398{
399 const struct inet_diag_handler *handler;
400 int err;
401
402 handler = inet_diag_lock_handler(req->sdiag_protocol);
403 if (IS_ERR(handler))
404 err = PTR_ERR(handler);
405 else
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000406 err = handler->dump_one(in_skb, nlh, req);
Herbert Xud523a322007-12-03 15:51:25 +1100407 inet_diag_unlock_handler(handler);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000408
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 return err;
410}
411
Al Viro9f855292006-09-27 18:44:30 -0700412static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
414 int words = bits >> 5;
415
416 bits &= 0x1f;
417
418 if (words) {
419 if (memcmp(a1, a2, words << 2))
420 return 0;
421 }
422 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700423 __be32 w1, w2;
424 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426 w1 = a1[words];
427 w2 = a2[words];
428
429 mask = htonl((0xffffffff) << (32 - bits));
430
431 if ((w1 ^ w2) & mask)
432 return 0;
433 }
434
435 return 1;
436}
437
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000438static int inet_diag_bc_run(const struct nlattr *_bc,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700439 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440{
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000441 const void *bc = nla_data(_bc);
442 int len = nla_len(_bc);
443
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 while (len > 0) {
445 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300446 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300449 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300451 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 yes = 0;
453 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300454 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 yes = entry->sport >= op[1].no;
456 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300457 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800458 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300460 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 yes = entry->dport >= op[1].no;
462 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300463 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 yes = entry->dport <= op[1].no;
465 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300466 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
468 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300469 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300470 case INET_DIAG_BC_D_COND: {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700471 const struct inet_diag_hostcond *cond;
472 const __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
Eric Dumazete31c5e02015-03-10 07:15:53 -0700474 cond = (const struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300476 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 entry->sport : entry->dport)) {
478 yes = 0;
479 break;
480 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800481
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300482 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 addr = entry->saddr;
484 else
485 addr = entry->daddr;
486
Neal Cardwellf67caec2012-12-08 19:43:23 +0000487 if (cond->family != AF_UNSPEC &&
488 cond->family != entry->family) {
489 if (entry->family == AF_INET6 &&
490 cond->family == AF_INET) {
491 if (addr[0] == 0 && addr[1] == 0 &&
492 addr[2] == htonl(0xffff) &&
493 bitstring_match(addr + 3,
494 cond->addr,
495 cond->prefix_len))
496 break;
497 }
498 yes = 0;
499 break;
500 }
501
502 if (cond->prefix_len == 0)
503 break;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800504 if (bitstring_match(addr, cond->addr,
505 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 yes = 0;
508 break;
509 }
510 }
511
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800512 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 len -= op->yes;
514 bc += op->yes;
515 } else {
516 len -= op->no;
517 bc += op->no;
518 }
519 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000520 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Eric Dumazeta4458342015-03-13 15:51:12 -0700523/* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
524 */
525static void entry_fill_addrs(struct inet_diag_entry *entry,
526 const struct sock *sk)
527{
528#if IS_ENABLED(CONFIG_IPV6)
529 if (sk->sk_family == AF_INET6) {
530 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
531 entry->daddr = sk->sk_v6_daddr.s6_addr32;
532 } else
533#endif
534 {
535 entry->saddr = &sk->sk_rcv_saddr;
536 entry->daddr = &sk->sk_daddr;
537 }
538}
539
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000540int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
541{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000542 struct inet_sock *inet = inet_sk(sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700543 struct inet_diag_entry entry;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000544
Eric Dumazete31c5e02015-03-10 07:15:53 -0700545 if (!bc)
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000546 return 1;
547
548 entry.family = sk->sk_family;
Eric Dumazeta4458342015-03-13 15:51:12 -0700549 entry_fill_addrs(&entry, sk);
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000550 entry.sport = inet->inet_num;
551 entry.dport = ntohs(inet->inet_dport);
Eric Dumazeta58917f2015-03-15 21:12:14 -0700552 entry.userlocks = sk_fullsock(sk) ? sk->sk_userlocks : 0;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000553
554 return inet_diag_bc_run(bc, &entry);
555}
556EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558static int valid_cc(const void *bc, int len, int cc)
559{
560 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300561 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
563 if (cc > len)
564 return 0;
565 if (cc == len)
566 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400567 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 return 0;
569 len -= op->yes;
570 bc += op->yes;
571 }
572 return 0;
573}
574
Neal Cardwell405c0052012-12-08 19:43:22 +0000575/* Validate an inet_diag_hostcond. */
576static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
577 int *min_len)
578{
Neal Cardwell405c0052012-12-08 19:43:22 +0000579 struct inet_diag_hostcond *cond;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700580 int addr_len;
Neal Cardwell405c0052012-12-08 19:43:22 +0000581
582 /* Check hostcond space. */
583 *min_len += sizeof(struct inet_diag_hostcond);
584 if (len < *min_len)
585 return false;
586 cond = (struct inet_diag_hostcond *)(op + 1);
587
588 /* Check address family and address length. */
589 switch (cond->family) {
590 case AF_UNSPEC:
591 addr_len = 0;
592 break;
593 case AF_INET:
594 addr_len = sizeof(struct in_addr);
595 break;
596 case AF_INET6:
597 addr_len = sizeof(struct in6_addr);
598 break;
599 default:
600 return false;
601 }
602 *min_len += addr_len;
603 if (len < *min_len)
604 return false;
605
606 /* Check prefix length (in bits) vs address length (in bytes). */
607 if (cond->prefix_len > 8 * addr_len)
608 return false;
609
610 return true;
611}
612
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000613/* Validate a port comparison operator. */
Eric Dumazete31c5e02015-03-10 07:15:53 -0700614static bool valid_port_comparison(const struct inet_diag_bc_op *op,
615 int len, int *min_len)
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000616{
617 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
618 *min_len += sizeof(struct inet_diag_bc_op);
619 if (len < *min_len)
620 return false;
621 return true;
622}
623
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300624static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625{
Eric Dumazeteeb14972011-06-17 16:25:39 -0400626 const void *bc = bytecode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 int len = bytecode_len;
628
629 while (len > 0) {
Neal Cardwell405c0052012-12-08 19:43:22 +0000630 int min_len = sizeof(struct inet_diag_bc_op);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700631 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300634 case INET_DIAG_BC_S_COND:
635 case INET_DIAG_BC_D_COND:
Neal Cardwell405c0052012-12-08 19:43:22 +0000636 if (!valid_hostcond(bc, len, &min_len))
637 return -EINVAL;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000638 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300639 case INET_DIAG_BC_S_GE:
640 case INET_DIAG_BC_S_LE:
641 case INET_DIAG_BC_D_GE:
642 case INET_DIAG_BC_D_LE:
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000643 if (!valid_port_comparison(bc, len, &min_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -EINVAL;
645 break;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000646 case INET_DIAG_BC_AUTO:
647 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300648 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 break;
650 default:
651 return -EINVAL;
652 }
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000653
654 if (op->code != INET_DIAG_BC_NOP) {
655 if (op->no < min_len || op->no > len + 4 || op->no & 3)
656 return -EINVAL;
657 if (op->no < len &&
658 !valid_cc(bytecode, bytecode_len, len - op->no))
659 return -EINVAL;
660 }
661
Neal Cardwell405c0052012-12-08 19:43:22 +0000662 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
Eric Dumazeteeb14972011-06-17 16:25:39 -0400663 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800664 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 len -= op->yes;
666 }
667 return len == 0 ? 0 : -EINVAL;
668}
669
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800670static int inet_csk_diag_dump(struct sock *sk,
671 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000672 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700673 const struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000674 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000676 if (!inet_diag_bc_sk(bc, sk))
677 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000679 return inet_csk_diag_fill(sk, skb, r,
Patrick McHardye32123e2013-04-17 06:46:57 +0000680 sk_user_ns(NETLINK_CB(cb->skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000681 NETLINK_CB(cb->skb).portid,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800682 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683}
684
Eric Dumazet49612722015-03-05 10:18:14 -0800685static void twsk_build_assert(void)
686{
687 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
688 offsetof(struct sock, sk_family));
689
690 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
691 offsetof(struct inet_sock, inet_num));
692
693 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
694 offsetof(struct inet_sock, inet_dport));
695
696 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
697 offsetof(struct inet_sock, inet_rcv_saddr));
698
699 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
700 offsetof(struct inet_sock, inet_daddr));
701
702#if IS_ENABLED(CONFIG_IPV6)
703 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
704 offsetof(struct sock, sk_v6_rcv_saddr));
705
706 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
707 offsetof(struct sock, sk_v6_daddr));
708#endif
709}
710
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300711static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000712 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700713 const struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000714 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700716 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct inet_sock *inet = inet_sk(sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700718 struct inet_diag_entry entry;
719 int j, s_j, reqnum, s_reqnum;
720 struct listen_sock *lopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 int err = 0;
722
723 s_j = cb->args[3];
724 s_reqnum = cb->args[4];
725
726 if (s_j > 0)
727 s_j--;
728
729 entry.family = sk->sk_family;
730
Eric Dumazetb2827052015-03-22 10:22:21 -0700731 spin_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700733 lopt = icsk->icsk_accept_queue.listen_opt;
Eric Dumazetfa76ce732015-03-19 19:04:20 -0700734 if (!lopt || !listen_sock_qlen(lopt))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 goto out;
736
Eric Dumazete31c5e02015-03-10 07:15:53 -0700737 if (bc) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000738 entry.sport = inet->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 entry.userlocks = sk->sk_userlocks;
740 }
741
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300742 for (j = s_j; j < lopt->nr_table_entries; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700743 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 reqnum = 0;
746 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700747 struct inet_request_sock *ireq = inet_rsk(req);
748
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 if (reqnum < s_reqnum)
750 continue;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700751 if (r->id.idiag_dport != ireq->ir_rmt_port &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300752 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 continue;
754
755 if (bc) {
Eric Dumazeta4458342015-03-13 15:51:12 -0700756 /* Note: entry.sport and entry.userlocks are already set */
Eric Dumazet08d2cc3b2015-03-18 14:05:38 -0700757 entry_fill_addrs(&entry, req_to_sk(req));
Eric Dumazet634fb9792013-10-09 15:21:29 -0700758 entry.dport = ntohs(ireq->ir_rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000760 if (!inet_diag_bc_run(bc, &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 continue;
762 }
763
Eric Dumazet08d2cc3b2015-03-18 14:05:38 -0700764 err = inet_req_diag_fill(req_to_sk(req), skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700765 NETLINK_CB(cb->skb).portid,
Eric Dumazeta58917f2015-03-15 21:12:14 -0700766 cb->nlh->nlmsg_seq,
767 NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (err < 0) {
769 cb->args[3] = j + 1;
770 cb->args[4] = reqnum;
771 goto out;
772 }
773 }
774
775 s_reqnum = 0;
776 }
777
778out:
Eric Dumazetb2827052015-03-22 10:22:21 -0700779 spin_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 return err;
782}
783
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000784void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700785 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700786 const struct inet_diag_req_v2 *r, struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000788 struct net *net = sock_net(skb->sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700789 int i, num, s_i, s_num;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 s_i = cb->args[1];
792 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300793
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 if (cb->args[0] == 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300795 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300797
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700798 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800799 struct inet_listen_hashbucket *ilb;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700800 struct hlist_nulls_node *node;
801 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802
803 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800804 ilb = &hashinfo->listening_hash[i];
805 spin_lock_bh(&ilb->lock);
Eric Dumazetc25eb3b2008-11-23 17:22:55 -0800806 sk_nulls_for_each(sk, node, &ilb->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 struct inet_sock *inet = inet_sk(sk);
808
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000809 if (!net_eq(sock_net(sk), net))
810 continue;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (num < s_num) {
813 num++;
814 continue;
815 }
816
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000817 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazete31c5e02015-03-10 07:15:53 -0700818 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000819 goto next_listen;
820
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000821 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300822 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 goto next_listen;
824
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300825 if (!(r->idiag_states & TCPF_LISTEN) ||
826 r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 cb->args[3] > 0)
828 goto syn_recv;
829
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000830 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800831 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 goto done;
833 }
834
835syn_recv:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300836 if (!(r->idiag_states & TCPF_SYN_RECV))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 goto next_listen;
838
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000839 if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800840 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto done;
842 }
843
844next_listen:
845 cb->args[3] = 0;
846 cb->args[4] = 0;
847 ++num;
848 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800849 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851 s_num = 0;
852 cb->args[3] = 0;
853 cb->args[4] = 0;
854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855skip_listen_ht:
856 cb->args[0] = 1;
857 s_i = num = s_num = 0;
858 }
859
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300860 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000861 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Eric Dumazetf373b532009-10-09 00:16:19 +0000863 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300864 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800865 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800866 struct hlist_nulls_node *node;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700867 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Andi Kleen6be547a2008-08-28 01:09:54 -0700869 num = 0;
870
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700871 if (hlist_nulls_empty(&head->chain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700872 continue;
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (i > s_i)
875 s_num = 0;
876
David S. Miller7e3aab42008-11-21 16:39:19 -0800877 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800878 sk_nulls_for_each(sk, node, &head->chain) {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700879 int state, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000881 if (!net_eq(sock_net(sk), net))
882 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 if (num < s_num)
884 goto next_normal;
Neal Cardwell70315d22014-01-10 15:34:45 -0500885 state = (sk->sk_state == TCP_TIME_WAIT) ?
886 inet_twsk(sk)->tw_substate : sk->sk_state;
887 if (!(r->idiag_states & (1 << state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 goto next_normal;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000889 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700890 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000891 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700892 if (r->id.idiag_sport != htons(sk->sk_num) &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300893 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700895 if (r->id.idiag_dport != sk->sk_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800896 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 goto next_normal;
Eric Dumazeta58917f2015-03-15 21:12:14 -0700898 twsk_build_assert();
899
900 if (!inet_diag_bc_sk(bc, sk))
901 goto next_normal;
902
903 res = sk_diag_fill(sk, skb, r,
904 sk_user_ns(NETLINK_CB(cb->skb).sk),
905 NETLINK_CB(cb->skb).portid,
906 cb->nlh->nlmsg_seq, NLM_F_MULTI,
907 cb->nlh);
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700908 if (res < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800909 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 goto done;
911 }
912next_normal:
913 ++num;
914 }
915
David S. Miller7e3aab42008-11-21 16:39:19 -0800916 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 }
918
919done:
920 cb->args[1] = i;
921 cb->args[2] = num;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000922out:
923 ;
924}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000925EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000926
927static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700928 const struct inet_diag_req_v2 *r,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700929 struct nlattr *bc)
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000930{
931 const struct inet_diag_handler *handler;
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000932 int err = 0;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000933
934 handler = inet_diag_lock_handler(r->sdiag_protocol);
935 if (!IS_ERR(handler))
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000936 handler->dump(skb, cb, r, bc);
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000937 else
938 err = PTR_ERR(handler);
Herbert Xud523a322007-12-03 15:51:25 +1100939 inet_diag_unlock_handler(handler);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000940
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000941 return err ? : skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000944static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
945{
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000946 int hdrlen = sizeof(struct inet_diag_req_v2);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700947 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000948
949 if (nlmsg_attrlen(cb->nlh, hdrlen))
950 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
951
David S. Millerd1063522012-06-26 21:28:54 -0700952 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000953}
954
Eric Dumazete31c5e02015-03-10 07:15:53 -0700955static int inet_diag_type2proto(int type)
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000956{
957 switch (type) {
958 case TCPDIAG_GETSOCK:
959 return IPPROTO_TCP;
960 case DCCPDIAG_GETSOCK:
961 return IPPROTO_DCCP;
962 default:
963 return 0;
964 }
965}
966
Eric Dumazete31c5e02015-03-10 07:15:53 -0700967static int inet_diag_dump_compat(struct sk_buff *skb,
968 struct netlink_callback *cb)
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000969{
David S. Millerd1063522012-06-26 21:28:54 -0700970 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700971 int hdrlen = sizeof(struct inet_diag_req);
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000972 struct inet_diag_req_v2 req;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000973 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000974
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000975 req.sdiag_family = AF_UNSPEC; /* compatibility */
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000976 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
977 req.idiag_ext = rc->idiag_ext;
978 req.idiag_states = rc->idiag_states;
979 req.id = rc->id;
980
981 if (nlmsg_attrlen(cb->nlh, hdrlen))
982 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
983
984 return __inet_diag_dump(skb, cb, &req, bc);
985}
986
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000987static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700988 const struct nlmsghdr *nlh)
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000989{
David S. Millerd1063522012-06-26 21:28:54 -0700990 struct inet_diag_req *rc = nlmsg_data(nlh);
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000991 struct inet_diag_req_v2 req;
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000992
993 req.sdiag_family = rc->idiag_family;
994 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
995 req.idiag_ext = rc->idiag_ext;
996 req.idiag_states = rc->idiag_states;
997 req.id = rc->id;
998
999 return inet_diag_get_exact(in_skb, nlh, &req);
1000}
1001
Pavel Emelyanov8d341722011-12-06 07:57:06 +00001002static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Pavel Emelyanov3b09c842012-01-10 22:37:26 +00001004 int hdrlen = sizeof(struct inet_diag_req);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001005 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006
Thomas Grafead592b2007-03-22 23:30:35 -07001007 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1008 nlmsg_len(nlh) < hdrlen)
1009 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
David S. Millerb8f3ab42011-01-18 12:40:38 -08001011 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -07001012 if (nlmsg_attrlen(nlh, hdrlen)) {
1013 struct nlattr *attr;
1014
1015 attr = nlmsg_find_attr(nlh, hdrlen,
1016 INET_DIAG_REQ_BYTECODE);
Eric Dumazete31c5e02015-03-10 07:15:53 -07001017 if (!attr ||
Thomas Grafead592b2007-03-22 23:30:35 -07001018 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1019 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1020 return -EINVAL;
1021 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001022 {
1023 struct netlink_dump_control c = {
1024 .dump = inet_diag_dump_compat,
1025 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001026 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001027 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 }
Thomas Grafead592b2007-03-22 23:30:35 -07001029
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001030 return inet_diag_get_exact_compat(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031}
1032
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001033static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1034{
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001035 int hdrlen = sizeof(struct inet_diag_req_v2);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001036 struct net *net = sock_net(skb->sk);
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001037
1038 if (nlmsg_len(h) < hdrlen)
1039 return -EINVAL;
1040
1041 if (h->nlmsg_flags & NLM_F_DUMP) {
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001042 if (nlmsg_attrlen(h, hdrlen)) {
1043 struct nlattr *attr;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001044
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001045 attr = nlmsg_find_attr(h, hdrlen,
1046 INET_DIAG_REQ_BYTECODE);
Eric Dumazete31c5e02015-03-10 07:15:53 -07001047 if (!attr ||
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001048 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1049 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1050 return -EINVAL;
1051 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001052 {
1053 struct netlink_dump_control c = {
1054 .dump = inet_diag_dump,
1055 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001056 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001057 }
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001058 }
1059
David S. Millerd1063522012-06-26 21:28:54 -07001060 return inet_diag_get_exact(skb, h, nlmsg_data(h));
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001061}
1062
Shan Wei8dcf01f2012-04-24 18:21:07 +00001063static const struct sock_diag_handler inet_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001064 .family = AF_INET,
1065 .dump = inet_diag_handler_dump,
1066};
1067
Shan Wei8dcf01f2012-04-24 18:21:07 +00001068static const struct sock_diag_handler inet6_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001069 .family = AF_INET6,
1070 .dump = inet_diag_handler_dump,
1071};
1072
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001073int inet_diag_register(const struct inet_diag_handler *h)
1074{
1075 const __u16 type = h->idiag_type;
1076 int err = -EINVAL;
1077
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001078 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001079 goto out;
1080
Herbert Xud523a322007-12-03 15:51:25 +11001081 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001082 err = -EEXIST;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001083 if (!inet_diag_table[type]) {
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001084 inet_diag_table[type] = h;
1085 err = 0;
1086 }
Herbert Xud523a322007-12-03 15:51:25 +11001087 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001088out:
1089 return err;
1090}
1091EXPORT_SYMBOL_GPL(inet_diag_register);
1092
1093void inet_diag_unregister(const struct inet_diag_handler *h)
1094{
1095 const __u16 type = h->idiag_type;
1096
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001097 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001098 return;
1099
Herbert Xud523a322007-12-03 15:51:25 +11001100 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001101 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +11001102 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001103}
1104EXPORT_SYMBOL_GPL(inet_diag_unregister);
1105
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001106static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001108 const int inet_diag_table_size = (IPPROTO_MAX *
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001109 sizeof(struct inet_diag_handler *));
1110 int err = -ENOMEM;
1111
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001112 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001113 if (!inet_diag_table)
1114 goto out;
1115
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001116 err = sock_diag_register(&inet_diag_handler);
1117 if (err)
1118 goto out_free_nl;
1119
1120 err = sock_diag_register(&inet6_diag_handler);
1121 if (err)
1122 goto out_free_inet;
1123
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001124 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001125out:
1126 return err;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001127
1128out_free_inet:
1129 sock_diag_unregister(&inet_diag_handler);
1130out_free_nl:
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001131 kfree(inet_diag_table);
1132 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133}
1134
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001135static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001137 sock_diag_unregister(&inet6_diag_handler);
1138 sock_diag_unregister(&inet_diag_handler);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001139 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001140 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141}
1142
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001143module_init(inet_diag_init);
1144module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +00001146MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1147MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);