blob: 570e61f9611fe9f62bf3513afc9a5abb365544b7 [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 {
Al Viro9f855292006-09-27 18:44:30 -070041 __be32 *saddr;
42 __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
64static inline void inet_diag_unlock_handler(
65 const struct inet_diag_handler *handler)
66{
67 mutex_unlock(&inet_diag_table_mutex);
68}
69
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +000070int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
Pavel Emelyanovc8991362012-01-10 22:36:35 +000071 struct sk_buff *skb, struct inet_diag_req_v2 *req,
Pavel Emelyanova029fe22011-12-06 07:59:32 +000072 u32 pid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -080073 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070075 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030076 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 struct nlmsghdr *nlh;
Thomas Graf6e277ed2012-06-26 23:36:12 +000078 struct nlattr *attr;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030079 void *info = NULL;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030080 const struct inet_diag_handler *handler;
Pavel Emelyanova029fe22011-12-06 07:59:32 +000081 int ext = req->idiag_ext;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030082
Pavel Emelyanova029fe22011-12-06 07:59:32 +000083 handler = inet_diag_table[req->sdiag_protocol];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030084 BUG_ON(handler == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
Thomas Graf6e277ed2012-06-26 23:36:12 +000086 nlh = nlmsg_put(skb, pid, seq, unlh->nlmsg_type, sizeof(*r),
87 nlmsg_flags);
88 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -070089 return -EMSGSIZE;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030090
David S. Millerd1063522012-06-26 21:28:54 -070091 r = nlmsg_data(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -080092 BUG_ON(sk->sk_state == TCP_TIME_WAIT);
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -080093
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030094 r->idiag_family = sk->sk_family;
95 r->idiag_state = sk->sk_state;
96 r->idiag_timer = 0;
97 r->idiag_retrans = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030099 r->id.idiag_if = sk->sk_bound_dev_if;
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +0000100 sock_diag_save_cookie(sk, r->id.idiag_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000102 r->id.idiag_sport = inet->inet_sport;
103 r->id.idiag_dport = inet->inet_dport;
104 r->id.idiag_src[0] = inet->inet_rcv_saddr;
105 r->id.idiag_dst[0] = inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500107 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
108 * hence this needs to be included regardless of socket family.
109 */
110 if (ext & (1 << (INET_DIAG_TOS - 1)))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000111 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
112 goto errout;
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500113
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000114#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300115 if (r->idiag_family == AF_INET6) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000116 const struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000118 *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr;
119 *(struct in6_addr *)r->id.idiag_dst = np->daddr;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000120
Maciej Żenczykowski06236ac2011-11-07 14:23:11 +0000121 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000122 if (nla_put_u8(skb, INET_DIAG_TCLASS, np->tclass) < 0)
123 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 }
125#endif
126
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000127 r->idiag_uid = sock_i_uid(sk);
128 r->idiag_inode = sock_i_ino(sk);
129
Thomas Graf6e277ed2012-06-26 23:36:12 +0000130 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
131 struct inet_diag_meminfo minfo = {
132 .idiag_rmem = sk_rmem_alloc_get(sk),
133 .idiag_wmem = sk->sk_wmem_queued,
134 .idiag_fmem = sk->sk_forward_alloc,
135 .idiag_tmem = sk_wmem_alloc_get(sk),
136 };
137
138 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
139 goto errout;
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000140 }
141
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000142 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
143 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000144 goto errout;
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000145
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000146 if (icsk == NULL) {
Shan Wei62ad6fc2012-04-24 18:15:41 +0000147 handler->idiag_get_info(sk, r, NULL);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000148 goto out;
149 }
150
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700151#define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700153 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300154 r->idiag_timer = 1;
155 r->idiag_retrans = icsk->icsk_retransmits;
156 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700157 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300158 r->idiag_timer = 4;
159 r->idiag_retrans = icsk->icsk_probes_out;
160 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300162 r->idiag_timer = 2;
163 r->idiag_retrans = icsk->icsk_probes_out;
164 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300166 r->idiag_timer = 0;
167 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169#undef EXPIRES_IN_MS
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300170
Thomas Graf6e277ed2012-06-26 23:36:12 +0000171 if (ext & (1 << (INET_DIAG_INFO - 1))) {
172 attr = nla_reserve(skb, INET_DIAG_INFO,
173 sizeof(struct tcp_info));
174 if (!attr)
175 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Thomas Graf6e277ed2012-06-26 23:36:12 +0000177 info = nla_data(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
Thomas Graf6e277ed2012-06-26 23:36:12 +0000180 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops)
181 if (nla_put_string(skb, INET_DIAG_CONG,
182 icsk->icsk_ca_ops->name) < 0)
183 goto errout;
184
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300185 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300187 if (sk->sk_state < TCP_TIME_WAIT &&
188 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
189 icsk->icsk_ca_ops->get_info(sk, ext, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000191out:
Thomas Graf6e277ed2012-06-26 23:36:12 +0000192 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193
Thomas Graf6e277ed2012-06-26 23:36:12 +0000194errout:
195 nlmsg_cancel(skb, nlh);
Patrick McHardy26932562007-01-31 23:16:40 -0800196 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000198EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
199
200static int inet_csk_diag_fill(struct sock *sk,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000201 struct sk_buff *skb, struct inet_diag_req_v2 *req,
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000202 u32 pid, u32 seq, u16 nlmsg_flags,
203 const struct nlmsghdr *unlh)
204{
205 return inet_sk_diag_fill(sk, inet_csk(sk),
206 skb, req, pid, seq, nlmsg_flags, unlh);
207}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800209static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000210 struct sk_buff *skb, struct inet_diag_req_v2 *req,
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000211 u32 pid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800212 const struct nlmsghdr *unlh)
213{
214 long tmo;
215 struct inet_diag_msg *r;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000216 struct nlmsghdr *nlh;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800217
Thomas Graf6e277ed2012-06-26 23:36:12 +0000218 nlh = nlmsg_put(skb, pid, seq, unlh->nlmsg_type, sizeof(*r),
219 nlmsg_flags);
220 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700221 return -EMSGSIZE;
David S. Millerd1063522012-06-26 21:28:54 -0700222
223 r = nlmsg_data(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800224 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
225
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800226 tmo = tw->tw_ttd - jiffies;
227 if (tmo < 0)
228 tmo = 0;
229
230 r->idiag_family = tw->tw_family;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800231 r->idiag_retrans = 0;
232 r->id.idiag_if = tw->tw_bound_dev_if;
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +0000233 sock_diag_save_cookie(tw, r->id.idiag_cookie);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800234 r->id.idiag_sport = tw->tw_sport;
235 r->id.idiag_dport = tw->tw_dport;
236 r->id.idiag_src[0] = tw->tw_rcv_saddr;
237 r->id.idiag_dst[0] = tw->tw_daddr;
238 r->idiag_state = tw->tw_substate;
239 r->idiag_timer = 3;
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700240 r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800241 r->idiag_rqueue = 0;
242 r->idiag_wqueue = 0;
243 r->idiag_uid = 0;
244 r->idiag_inode = 0;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000245#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800246 if (tw->tw_family == AF_INET6) {
247 const struct inet6_timewait_sock *tw6 =
248 inet6_twsk((struct sock *)tw);
249
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000250 *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr;
251 *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800252 }
253#endif
Thomas Graf6e277ed2012-06-26 23:36:12 +0000254
255 return nlmsg_end(skb, nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800256}
257
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800258static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000259 struct inet_diag_req_v2 *r, u32 pid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800260 const struct nlmsghdr *unlh)
261{
262 if (sk->sk_state == TCP_TIME_WAIT)
263 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000264 skb, r, pid, seq, nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800265 unlh);
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000266 return inet_csk_diag_fill(sk, skb, r, pid, seq, nlmsg_flags, unlh);
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800267}
268
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000269int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *in_skb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000270 const struct nlmsghdr *nlh, struct inet_diag_req_v2 *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271{
272 int err;
273 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 struct sk_buff *rep;
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000275 struct net *net = sock_net(in_skb->sk);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300276
Herbert Xud523a322007-12-03 15:51:25 +1100277 err = -EINVAL;
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000278 if (req->sdiag_family == AF_INET) {
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000279 sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300280 req->id.idiag_dport, req->id.idiag_src[0],
281 req->id.idiag_sport, req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000283#if IS_ENABLED(CONFIG_IPV6)
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000284 else if (req->sdiag_family == AF_INET6) {
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000285 sk = inet6_lookup(net, hashinfo,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300286 (struct in6_addr *)req->id.idiag_dst,
287 req->id.idiag_dport,
288 (struct in6_addr *)req->id.idiag_src,
289 req->id.idiag_sport,
290 req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 }
292#endif
293 else {
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000294 goto out_nosk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
Herbert Xud523a322007-12-03 15:51:25 +1100297 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (sk == NULL)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000299 goto out_nosk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +0000301 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
Pavel Emelyanovb005ab42011-12-09 06:21:53 +0000302 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 goto out;
304
Thomas Graf6e277ed2012-06-26 23:36:12 +0000305 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
306 sizeof(struct inet_diag_meminfo) +
307 sizeof(struct tcp_info) + 64, GFP_KERNEL);
308 if (!rep) {
309 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto out;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000313 err = sk_diag_fill(sk, rep, req,
Patrick McHardy26932562007-01-31 23:16:40 -0800314 NETLINK_CB(in_skb).pid,
315 nlh->nlmsg_seq, 0, nlh);
316 if (err < 0) {
317 WARN_ON(err == -EMSGSIZE);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000318 nlmsg_free(rep);
Patrick McHardy26932562007-01-31 23:16:40 -0800319 goto out;
320 }
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000321 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).pid,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300322 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 if (err > 0)
324 err = 0;
325
326out:
327 if (sk) {
328 if (sk->sk_state == TCP_TIME_WAIT)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700329 inet_twsk_put((struct inet_timewait_sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 else
331 sock_put(sk);
332 }
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000333out_nosk:
334 return err;
335}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000336EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000337
338static int inet_diag_get_exact(struct sk_buff *in_skb,
339 const struct nlmsghdr *nlh,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000340 struct inet_diag_req_v2 *req)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000341{
342 const struct inet_diag_handler *handler;
343 int err;
344
345 handler = inet_diag_lock_handler(req->sdiag_protocol);
346 if (IS_ERR(handler))
347 err = PTR_ERR(handler);
348 else
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000349 err = handler->dump_one(in_skb, nlh, req);
Herbert Xud523a322007-12-03 15:51:25 +1100350 inet_diag_unlock_handler(handler);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 return err;
353}
354
Al Viro9f855292006-09-27 18:44:30 -0700355static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 int words = bits >> 5;
358
359 bits &= 0x1f;
360
361 if (words) {
362 if (memcmp(a1, a2, words << 2))
363 return 0;
364 }
365 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700366 __be32 w1, w2;
367 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
369 w1 = a1[words];
370 w2 = a2[words];
371
372 mask = htonl((0xffffffff) << (32 - bits));
373
374 if ((w1 ^ w2) & mask)
375 return 0;
376 }
377
378 return 1;
379}
380
381
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000382static int inet_diag_bc_run(const struct nlattr *_bc,
383 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384{
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000385 const void *bc = nla_data(_bc);
386 int len = nla_len(_bc);
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 while (len > 0) {
389 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300390 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
392 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300393 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300395 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 yes = 0;
397 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300398 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 yes = entry->sport >= op[1].no;
400 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300401 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800402 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300404 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 yes = entry->dport >= op[1].no;
406 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300407 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 yes = entry->dport <= op[1].no;
409 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300410 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
412 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300413 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300414 case INET_DIAG_BC_D_COND: {
415 struct inet_diag_hostcond *cond;
Al Viro9f855292006-09-27 18:44:30 -0700416 __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300418 cond = (struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300420 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 entry->sport : entry->dport)) {
422 yes = 0;
423 break;
424 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 if (cond->prefix_len == 0)
427 break;
428
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300429 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 addr = entry->saddr;
431 else
432 addr = entry->daddr;
433
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800434 if (bitstring_match(addr, cond->addr,
435 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 break;
437 if (entry->family == AF_INET6 &&
438 cond->family == AF_INET) {
439 if (addr[0] == 0 && addr[1] == 0 &&
440 addr[2] == htonl(0xffff) &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300441 bitstring_match(addr + 3, cond->addr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900442 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 break;
444 }
445 yes = 0;
446 break;
447 }
448 }
449
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800450 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 len -= op->yes;
452 bc += op->yes;
453 } else {
454 len -= op->no;
455 bc += op->no;
456 }
457 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000458 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000461int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
462{
463 struct inet_diag_entry entry;
464 struct inet_sock *inet = inet_sk(sk);
465
466 if (bc == NULL)
467 return 1;
468
469 entry.family = sk->sk_family;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000470#if IS_ENABLED(CONFIG_IPV6)
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000471 if (entry.family == AF_INET6) {
472 struct ipv6_pinfo *np = inet6_sk(sk);
473
474 entry.saddr = np->rcv_saddr.s6_addr32;
475 entry.daddr = np->daddr.s6_addr32;
476 } else
477#endif
478 {
479 entry.saddr = &inet->inet_rcv_saddr;
480 entry.daddr = &inet->inet_daddr;
481 }
482 entry.sport = inet->inet_num;
483 entry.dport = ntohs(inet->inet_dport);
484 entry.userlocks = sk->sk_userlocks;
485
486 return inet_diag_bc_run(bc, &entry);
487}
488EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
489
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490static int valid_cc(const void *bc, int len, int cc)
491{
492 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300493 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
495 if (cc > len)
496 return 0;
497 if (cc == len)
498 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400499 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return 0;
501 len -= op->yes;
502 bc += op->yes;
503 }
504 return 0;
505}
506
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300507static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Eric Dumazeteeb14972011-06-17 16:25:39 -0400509 const void *bc = bytecode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 int len = bytecode_len;
511
512 while (len > 0) {
Eric Dumazeteeb14972011-06-17 16:25:39 -0400513 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
516 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300517 case INET_DIAG_BC_AUTO:
518 case INET_DIAG_BC_S_COND:
519 case INET_DIAG_BC_D_COND:
520 case INET_DIAG_BC_S_GE:
521 case INET_DIAG_BC_S_LE:
522 case INET_DIAG_BC_D_GE:
523 case INET_DIAG_BC_D_LE:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300524 case INET_DIAG_BC_JMP:
Eric Dumazeteeb14972011-06-17 16:25:39 -0400525 if (op->no < 4 || op->no > len + 4 || op->no & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 return -EINVAL;
527 if (op->no < len &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300528 !valid_cc(bytecode, bytecode_len, len - op->no))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 return -EINVAL;
530 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300531 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 break;
533 default:
534 return -EINVAL;
535 }
Eric Dumazeteeb14972011-06-17 16:25:39 -0400536 if (op->yes < 4 || op->yes > len + 4 || op->yes & 3)
537 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800538 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 len -= op->yes;
540 }
541 return len == 0 ? 0 : -EINVAL;
542}
543
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800544static int inet_csk_diag_dump(struct sock *sk,
545 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000546 struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000547 struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000548 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000550 if (!inet_diag_bc_sk(bc, sk))
551 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000553 return inet_csk_diag_fill(sk, skb, r,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800554 NETLINK_CB(cb->skb).pid,
555 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556}
557
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800558static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
559 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000560 struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000561 struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000562 const struct nlattr *bc)
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800563{
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000564 if (bc != NULL) {
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800565 struct inet_diag_entry entry;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800566
567 entry.family = tw->tw_family;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000568#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800569 if (tw->tw_family == AF_INET6) {
570 struct inet6_timewait_sock *tw6 =
571 inet6_twsk((struct sock *)tw);
572 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
573 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
574 } else
575#endif
576 {
577 entry.saddr = &tw->tw_rcv_saddr;
578 entry.daddr = &tw->tw_daddr;
579 }
580 entry.sport = tw->tw_num;
581 entry.dport = ntohs(tw->tw_dport);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900582 entry.userlocks = 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800583
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000584 if (!inet_diag_bc_run(bc, &entry))
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800585 return 0;
586 }
587
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000588 return inet_twsk_diag_fill(tw, skb, r,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800589 NETLINK_CB(cb->skb).pid,
590 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
591}
592
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300593static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800594 struct request_sock *req, u32 pid, u32 seq,
595 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700597 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300599 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 struct nlmsghdr *nlh;
601 long tmo;
602
Thomas Graf6e277ed2012-06-26 23:36:12 +0000603 nlh = nlmsg_put(skb, pid, seq, unlh->nlmsg_type, sizeof(*r),
604 NLM_F_MULTI);
605 if (!nlh)
606 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607
Thomas Graf6e277ed2012-06-26 23:36:12 +0000608 r = nlmsg_data(nlh);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300609 r->idiag_family = sk->sk_family;
610 r->idiag_state = TCP_SYN_RECV;
611 r->idiag_timer = 1;
612 r->idiag_retrans = req->retrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300614 r->id.idiag_if = sk->sk_bound_dev_if;
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +0000615 sock_diag_save_cookie(req, r->id.idiag_cookie);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616
617 tmo = req->expires - jiffies;
618 if (tmo < 0)
619 tmo = 0;
620
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000621 r->id.idiag_sport = inet->inet_sport;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300622 r->id.idiag_dport = ireq->rmt_port;
623 r->id.idiag_src[0] = ireq->loc_addr;
624 r->id.idiag_dst[0] = ireq->rmt_addr;
625 r->idiag_expires = jiffies_to_msecs(tmo);
626 r->idiag_rqueue = 0;
627 r->idiag_wqueue = 0;
628 r->idiag_uid = sock_i_uid(sk);
629 r->idiag_inode = 0;
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000630#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300631 if (r->idiag_family == AF_INET6) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000632 *(struct in6_addr *)r->id.idiag_src = inet6_rsk(req)->loc_addr;
633 *(struct in6_addr *)r->id.idiag_dst = inet6_rsk(req)->rmt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 }
635#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636
Thomas Graf6e277ed2012-06-26 23:36:12 +0000637 return nlmsg_end(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300640static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000641 struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000642 struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000643 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300645 struct inet_diag_entry entry;
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700646 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo2ad69c52005-06-18 22:48:55 -0700647 struct listen_sock *lopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct inet_sock *inet = inet_sk(sk);
649 int j, s_j;
650 int reqnum, s_reqnum;
651 int err = 0;
652
653 s_j = cb->args[3];
654 s_reqnum = cb->args[4];
655
656 if (s_j > 0)
657 s_j--;
658
659 entry.family = sk->sk_family;
660
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700661 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700663 lopt = icsk->icsk_accept_queue.listen_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (!lopt || !lopt->qlen)
665 goto out;
666
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000667 if (bc != NULL) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000668 entry.sport = inet->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 entry.userlocks = sk->sk_userlocks;
670 }
671
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300672 for (j = s_j; j < lopt->nr_table_entries; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700673 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674
675 reqnum = 0;
676 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700677 struct inet_request_sock *ireq = inet_rsk(req);
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (reqnum < s_reqnum)
680 continue;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300681 if (r->id.idiag_dport != ireq->rmt_port &&
682 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 continue;
684
685 if (bc) {
686 entry.saddr =
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000687#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800689 inet6_rsk(req)->loc_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700691 &ireq->loc_addr;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800692 entry.daddr =
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000693#if IS_ENABLED(CONFIG_IPV6)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800695 inet6_rsk(req)->rmt_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700697 &ireq->rmt_addr;
698 entry.dport = ntohs(ireq->rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000700 if (!inet_diag_bc_run(bc, &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 continue;
702 }
703
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300704 err = inet_diag_fill_req(skb, sk, req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 NETLINK_CB(cb->skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300706 cb->nlh->nlmsg_seq, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (err < 0) {
708 cb->args[3] = j + 1;
709 cb->args[4] = reqnum;
710 goto out;
711 }
712 }
713
714 s_reqnum = 0;
715 }
716
717out:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700718 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 return err;
721}
722
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000723void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000724 struct netlink_callback *cb, struct inet_diag_req_v2 *r, struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 int i, num;
727 int s_i, s_num;
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000728 struct net *net = sock_net(skb->sk);
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 s_i = cb->args[1];
731 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (cb->args[0] == 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300734 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300736
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700737 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct sock *sk;
Eric Dumazetc25eb3b2008-11-23 17:22:55 -0800739 struct hlist_nulls_node *node;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800740 struct inet_listen_hashbucket *ilb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800743 ilb = &hashinfo->listening_hash[i];
744 spin_lock_bh(&ilb->lock);
Eric Dumazetc25eb3b2008-11-23 17:22:55 -0800745 sk_nulls_for_each(sk, node, &ilb->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct inet_sock *inet = inet_sk(sk);
747
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000748 if (!net_eq(sock_net(sk), net))
749 continue;
750
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 if (num < s_num) {
752 num++;
753 continue;
754 }
755
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000756 if (r->sdiag_family != AF_UNSPEC &&
757 sk->sk_family != r->sdiag_family)
758 goto next_listen;
759
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000760 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300761 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 goto next_listen;
763
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300764 if (!(r->idiag_states & TCPF_LISTEN) ||
765 r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 cb->args[3] > 0)
767 goto syn_recv;
768
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000769 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800770 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 goto done;
772 }
773
774syn_recv:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300775 if (!(r->idiag_states & TCPF_SYN_RECV))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 goto next_listen;
777
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000778 if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800779 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 goto done;
781 }
782
783next_listen:
784 cb->args[3] = 0;
785 cb->args[4] = 0;
786 ++num;
787 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800788 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 s_num = 0;
791 cb->args[3] = 0;
792 cb->args[4] = 0;
793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794skip_listen_ht:
795 cb->args[0] = 1;
796 s_i = num = s_num = 0;
797 }
798
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300799 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000800 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801
Eric Dumazetf373b532009-10-09 00:16:19 +0000802 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300803 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800804 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct sock *sk;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800806 struct hlist_nulls_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Andi Kleen6be547a2008-08-28 01:09:54 -0700808 num = 0;
809
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800810 if (hlist_nulls_empty(&head->chain) &&
811 hlist_nulls_empty(&head->twchain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700812 continue;
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 if (i > s_i)
815 s_num = 0;
816
David S. Miller7e3aab42008-11-21 16:39:19 -0800817 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800818 sk_nulls_for_each(sk, node, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 struct inet_sock *inet = inet_sk(sk);
820
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000821 if (!net_eq(sock_net(sk), net))
822 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 if (num < s_num)
824 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300825 if (!(r->idiag_states & (1 << sk->sk_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 goto next_normal;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000827 if (r->sdiag_family != AF_UNSPEC &&
828 sk->sk_family != r->sdiag_family)
829 goto next_normal;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000830 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300831 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 goto next_normal;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000833 if (r->id.idiag_dport != inet->inet_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800834 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 goto next_normal;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000836 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800837 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 goto done;
839 }
840next_normal:
841 ++num;
842 }
843
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300844 if (r->idiag_states & TCPF_TIME_WAIT) {
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800845 struct inet_timewait_sock *tw;
846
847 inet_twsk_for_each(tw, node,
Eric Dumazetdbca9b2752007-02-08 14:16:46 -0800848 &head->twchain) {
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000849 if (!net_eq(twsk_net(tw), net))
850 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 if (num < s_num)
853 goto next_dying;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000854 if (r->sdiag_family != AF_UNSPEC &&
855 tw->tw_family != r->sdiag_family)
856 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800857 if (r->id.idiag_sport != tw->tw_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300858 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800860 if (r->id.idiag_dport != tw->tw_dport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300861 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 goto next_dying;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000863 if (inet_twsk_diag_dump(tw, skb, cb, r, bc) < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800864 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 goto done;
866 }
867next_dying:
868 ++num;
869 }
870 }
David S. Miller7e3aab42008-11-21 16:39:19 -0800871 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 }
873
874done:
875 cb->args[1] = i;
876 cb->args[2] = num;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000877out:
878 ;
879}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000880EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000881
882static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000883 struct inet_diag_req_v2 *r, struct nlattr *bc)
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000884{
885 const struct inet_diag_handler *handler;
886
887 handler = inet_diag_lock_handler(r->sdiag_protocol);
888 if (!IS_ERR(handler))
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000889 handler->dump(skb, cb, r, bc);
Herbert Xud523a322007-12-03 15:51:25 +1100890 inet_diag_unlock_handler(handler);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000891
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return skb->len;
893}
894
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000895static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
896{
897 struct nlattr *bc = NULL;
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000898 int hdrlen = sizeof(struct inet_diag_req_v2);
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000899
900 if (nlmsg_attrlen(cb->nlh, hdrlen))
901 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
902
David S. Millerd1063522012-06-26 21:28:54 -0700903 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000904}
905
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000906static inline int inet_diag_type2proto(int type)
907{
908 switch (type) {
909 case TCPDIAG_GETSOCK:
910 return IPPROTO_TCP;
911 case DCCPDIAG_GETSOCK:
912 return IPPROTO_DCCP;
913 default:
914 return 0;
915 }
916}
917
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000918static int inet_diag_dump_compat(struct sk_buff *skb, struct netlink_callback *cb)
919{
David S. Millerd1063522012-06-26 21:28:54 -0700920 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000921 struct inet_diag_req_v2 req;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000922 struct nlattr *bc = NULL;
Pavel Emelyanov3b09c842012-01-10 22:37:26 +0000923 int hdrlen = sizeof(struct inet_diag_req);
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000924
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000925 req.sdiag_family = AF_UNSPEC; /* compatibility */
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000926 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
927 req.idiag_ext = rc->idiag_ext;
928 req.idiag_states = rc->idiag_states;
929 req.id = rc->id;
930
931 if (nlmsg_attrlen(cb->nlh, hdrlen))
932 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
933
934 return __inet_diag_dump(skb, cb, &req, bc);
935}
936
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000937static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
938 const struct nlmsghdr *nlh)
939{
David S. Millerd1063522012-06-26 21:28:54 -0700940 struct inet_diag_req *rc = nlmsg_data(nlh);
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000941 struct inet_diag_req_v2 req;
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000942
943 req.sdiag_family = rc->idiag_family;
944 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
945 req.idiag_ext = rc->idiag_ext;
946 req.idiag_states = rc->idiag_states;
947 req.id = rc->id;
948
949 return inet_diag_get_exact(in_skb, nlh, &req);
950}
951
Pavel Emelyanov8d341722011-12-06 07:57:06 +0000952static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Pavel Emelyanov3b09c842012-01-10 22:37:26 +0000954 int hdrlen = sizeof(struct inet_diag_req);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000955 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956
Thomas Grafead592b2007-03-22 23:30:35 -0700957 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
958 nlmsg_len(nlh) < hdrlen)
959 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
David S. Millerb8f3ab42011-01-18 12:40:38 -0800961 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -0700962 if (nlmsg_attrlen(nlh, hdrlen)) {
963 struct nlattr *attr;
964
965 attr = nlmsg_find_attr(nlh, hdrlen,
966 INET_DIAG_REQ_BYTECODE);
967 if (attr == NULL ||
968 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
969 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
970 return -EINVAL;
971 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000972 {
973 struct netlink_dump_control c = {
974 .dump = inet_diag_dump_compat,
975 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000976 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +0000977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 }
Thomas Grafead592b2007-03-22 23:30:35 -0700979
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000980 return inet_diag_get_exact_compat(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981}
982
Pavel Emelyanovd3664772011-12-06 07:58:03 +0000983static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
984{
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000985 int hdrlen = sizeof(struct inet_diag_req_v2);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000986 struct net *net = sock_net(skb->sk);
Pavel Emelyanovd3664772011-12-06 07:58:03 +0000987
988 if (nlmsg_len(h) < hdrlen)
989 return -EINVAL;
990
991 if (h->nlmsg_flags & NLM_F_DUMP) {
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000992 if (nlmsg_attrlen(h, hdrlen)) {
993 struct nlattr *attr;
994 attr = nlmsg_find_attr(h, hdrlen,
995 INET_DIAG_REQ_BYTECODE);
996 if (attr == NULL ||
997 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
998 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
999 return -EINVAL;
1000 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001001 {
1002 struct netlink_dump_control c = {
1003 .dump = inet_diag_dump,
1004 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001005 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001006 }
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001007 }
1008
David S. Millerd1063522012-06-26 21:28:54 -07001009 return inet_diag_get_exact(skb, h, nlmsg_data(h));
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001010}
1011
Shan Wei8dcf01f2012-04-24 18:21:07 +00001012static const struct sock_diag_handler inet_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001013 .family = AF_INET,
1014 .dump = inet_diag_handler_dump,
1015};
1016
Shan Wei8dcf01f2012-04-24 18:21:07 +00001017static const struct sock_diag_handler inet6_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001018 .family = AF_INET6,
1019 .dump = inet_diag_handler_dump,
1020};
1021
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001022int inet_diag_register(const struct inet_diag_handler *h)
1023{
1024 const __u16 type = h->idiag_type;
1025 int err = -EINVAL;
1026
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001027 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001028 goto out;
1029
Herbert Xud523a322007-12-03 15:51:25 +11001030 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001031 err = -EEXIST;
1032 if (inet_diag_table[type] == NULL) {
1033 inet_diag_table[type] = h;
1034 err = 0;
1035 }
Herbert Xud523a322007-12-03 15:51:25 +11001036 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001037out:
1038 return err;
1039}
1040EXPORT_SYMBOL_GPL(inet_diag_register);
1041
1042void inet_diag_unregister(const struct inet_diag_handler *h)
1043{
1044 const __u16 type = h->idiag_type;
1045
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001046 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001047 return;
1048
Herbert Xud523a322007-12-03 15:51:25 +11001049 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001050 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +11001051 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001052}
1053EXPORT_SYMBOL_GPL(inet_diag_unregister);
1054
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001055static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001057 const int inet_diag_table_size = (IPPROTO_MAX *
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001058 sizeof(struct inet_diag_handler *));
1059 int err = -ENOMEM;
1060
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001061 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001062 if (!inet_diag_table)
1063 goto out;
1064
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001065 err = sock_diag_register(&inet_diag_handler);
1066 if (err)
1067 goto out_free_nl;
1068
1069 err = sock_diag_register(&inet6_diag_handler);
1070 if (err)
1071 goto out_free_inet;
1072
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001073 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001074out:
1075 return err;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001076
1077out_free_inet:
1078 sock_diag_unregister(&inet_diag_handler);
1079out_free_nl:
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001080 kfree(inet_diag_table);
1081 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001084static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001086 sock_diag_unregister(&inet6_diag_handler);
1087 sock_diag_unregister(&inet_diag_handler);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001088 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001089 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090}
1091
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001092module_init(inet_diag_init);
1093module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +00001095MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1096MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);