blob: ac7b5c909fe7b5d293a09120b0df024815c032eb [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
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +000093int inet_sk_diag_fill(struct sock *sk, struct inet_connection_sock *icsk,
Eric Dumazet34160ea2015-03-10 07:15:54 -070094 struct sk_buff *skb, const struct inet_diag_req_v2 *req,
Eric Dumazete31c5e02015-03-10 07:15:53 -070095 struct user_namespace *user_ns,
96 u32 portid, u32 seq, u16 nlmsg_flags,
97 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070099 const struct inet_sock *inet = inet_sk(sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700100 const struct inet_diag_handler *handler;
101 int ext = req->idiag_ext;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300102 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 struct nlmsghdr *nlh;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000104 struct nlattr *attr;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300105 void *info = NULL;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300106
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000107 handler = inet_diag_table[req->sdiag_protocol];
Eric Dumazete31c5e02015-03-10 07:15:53 -0700108 BUG_ON(!handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Eric W. Biederman15e47302012-09-07 20:12:54 +0000110 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000111 nlmsg_flags);
112 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700113 return -EMSGSIZE;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300114
David S. Millerd1063522012-06-26 21:28:54 -0700115 r = nlmsg_data(nlh);
Eric Dumazeta07c9202015-03-13 15:51:11 -0700116 BUG_ON((1 << sk->sk_state) & (TCPF_TIME_WAIT | TCPF_NEW_SYN_RECV));
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800117
Eric Dumazeta4458342015-03-13 15:51:12 -0700118 inet_diag_msg_common_fill(r, sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300119 r->idiag_state = sk->sk_state;
120 r->idiag_timer = 0;
121 r->idiag_retrans = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Pavel Emelyanove4e541a2012-10-23 22:29:56 +0400124 if (nla_put_u8(skb, INET_DIAG_SHUTDOWN, sk->sk_shutdown))
125 goto errout;
126
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500127 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
128 * hence this needs to be included regardless of socket family.
129 */
130 if (ext & (1 << (INET_DIAG_TOS - 1)))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000131 if (nla_put_u8(skb, INET_DIAG_TOS, inet->tos) < 0)
132 goto errout;
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500133
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000134#if IS_ENABLED(CONFIG_IPV6)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300135 if (r->idiag_family == AF_INET6) {
Maciej Żenczykowski06236ac2011-11-07 14:23:11 +0000136 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
Eric Dumazetefe42082013-10-03 15:42:29 -0700137 if (nla_put_u8(skb, INET_DIAG_TCLASS,
138 inet6_sk(sk)->tclass) < 0)
Thomas Graf6e277ed2012-06-26 23:36:12 +0000139 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 }
141#endif
142
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600143 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000144 r->idiag_inode = sock_i_ino(sk);
145
Thomas Graf6e277ed2012-06-26 23:36:12 +0000146 if (ext & (1 << (INET_DIAG_MEMINFO - 1))) {
147 struct inet_diag_meminfo minfo = {
148 .idiag_rmem = sk_rmem_alloc_get(sk),
149 .idiag_wmem = sk->sk_wmem_queued,
150 .idiag_fmem = sk->sk_forward_alloc,
151 .idiag_tmem = sk_wmem_alloc_get(sk),
152 };
153
154 if (nla_put(skb, INET_DIAG_MEMINFO, sizeof(minfo), &minfo) < 0)
155 goto errout;
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000156 }
157
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000158 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1)))
159 if (sock_diag_put_meminfo(sk, skb, INET_DIAG_SKMEMINFO))
Thomas Graf6e277ed2012-06-26 23:36:12 +0000160 goto errout;
Pavel Emelyanovc0636fa2011-12-30 00:53:32 +0000161
Eric Dumazete31c5e02015-03-10 07:15:53 -0700162 if (!icsk) {
Shan Wei62ad6fc2012-04-24 18:15:41 +0000163 handler->idiag_get_info(sk, r, NULL);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000164 goto out;
165 }
166
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700167#define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168
Nandita Dukkipati6ba8a3b2013-03-11 10:00:43 +0000169 if (icsk->icsk_pending == ICSK_TIME_RETRANS ||
170 icsk->icsk_pending == ICSK_TIME_EARLY_RETRANS ||
171 icsk->icsk_pending == ICSK_TIME_LOSS_PROBE) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300172 r->idiag_timer = 1;
173 r->idiag_retrans = icsk->icsk_retransmits;
174 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700175 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300176 r->idiag_timer = 4;
177 r->idiag_retrans = icsk->icsk_probes_out;
178 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300180 r->idiag_timer = 2;
181 r->idiag_retrans = icsk->icsk_probes_out;
182 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300184 r->idiag_timer = 0;
185 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187#undef EXPIRES_IN_MS
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300188
Thomas Graf6e277ed2012-06-26 23:36:12 +0000189 if (ext & (1 << (INET_DIAG_INFO - 1))) {
190 attr = nla_reserve(skb, INET_DIAG_INFO,
191 sizeof(struct tcp_info));
192 if (!attr)
193 goto errout;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194
Thomas Graf6e277ed2012-06-26 23:36:12 +0000195 info = nla_data(attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 }
197
Thomas Graf6e277ed2012-06-26 23:36:12 +0000198 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops)
199 if (nla_put_string(skb, INET_DIAG_CONG,
200 icsk->icsk_ca_ops->name) < 0)
201 goto errout;
202
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300203 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300205 if (sk->sk_state < TCP_TIME_WAIT &&
206 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
207 icsk->icsk_ca_ops->get_info(sk, ext, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000209out:
Johannes Berg053c0952015-01-16 22:09:00 +0100210 nlmsg_end(skb, nlh);
211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
Thomas Graf6e277ed2012-06-26 23:36:12 +0000213errout:
214 nlmsg_cancel(skb, nlh);
Patrick McHardy26932562007-01-31 23:16:40 -0800215 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000217EXPORT_SYMBOL_GPL(inet_sk_diag_fill);
218
219static int inet_csk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700220 struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700221 const struct inet_diag_req_v2 *req,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600222 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000223 u32 portid, u32 seq, u16 nlmsg_flags,
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000224 const struct nlmsghdr *unlh)
225{
Eric Dumazete31c5e02015-03-10 07:15:53 -0700226 return inet_sk_diag_fill(sk, inet_csk(sk), skb, req,
227 user_ns, portid, seq, nlmsg_flags, unlh);
Pavel Emelyanov3c4d05c2011-12-09 06:23:00 +0000228}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700230static int inet_twsk_diag_fill(struct sock *sk,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700231 struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700232 const struct inet_diag_req_v2 *req,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000233 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800234 const struct nlmsghdr *unlh)
235{
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700236 struct inet_timewait_sock *tw = inet_twsk(sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800237 struct inet_diag_msg *r;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000238 struct nlmsghdr *nlh;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700239 s32 tmo;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800240
Eric W. Biederman15e47302012-09-07 20:12:54 +0000241 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000242 nlmsg_flags);
243 if (!nlh)
David S. Millerd1063522012-06-26 21:28:54 -0700244 return -EMSGSIZE;
David S. Millerd1063522012-06-26 21:28:54 -0700245
246 r = nlmsg_data(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800247 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
248
Eric Dumazet96f817f2013-10-03 14:27:25 -0700249 tmo = tw->tw_ttd - inet_tw_time_stamp();
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800250 if (tmo < 0)
251 tmo = 0;
252
Eric Dumazeta4458342015-03-13 15:51:12 -0700253 inet_diag_msg_common_fill(r, sk);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800254 r->idiag_retrans = 0;
Daniel Borkmannb1aac812013-12-17 00:38:39 +0100255
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800256 r->idiag_state = tw->tw_substate;
257 r->idiag_timer = 3;
Eric Dumazet96f817f2013-10-03 14:27:25 -0700258 r->idiag_expires = jiffies_to_msecs(tmo);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800259 r->idiag_rqueue = 0;
260 r->idiag_wqueue = 0;
261 r->idiag_uid = 0;
262 r->idiag_inode = 0;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000263
Johannes Berg053c0952015-01-16 22:09:00 +0100264 nlmsg_end(skb, nlh);
265 return 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800266}
267
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800268static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700269 const struct inet_diag_req_v2 *r,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600270 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000271 u32 portid, u32 seq, u16 nlmsg_flags,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800272 const struct nlmsghdr *unlh)
273{
274 if (sk->sk_state == TCP_TIME_WAIT)
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700275 return inet_twsk_diag_fill(sk, skb, r, portid, seq,
Eric Dumazetefe42082013-10-03 15:42:29 -0700276 nlmsg_flags, unlh);
277
278 return inet_csk_diag_fill(sk, skb, r, user_ns, portid, seq,
279 nlmsg_flags, unlh);
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800280}
281
Eric Dumazete31c5e02015-03-10 07:15:53 -0700282int inet_diag_dump_one_icsk(struct inet_hashinfo *hashinfo,
283 struct sk_buff *in_skb,
284 const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700285 const struct inet_diag_req_v2 *req)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000287 struct net *net = sock_net(in_skb->sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700288 struct sk_buff *rep;
289 struct sock *sk;
290 int err;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300291
Herbert Xud523a322007-12-03 15:51:25 +1100292 err = -EINVAL;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700293 if (req->sdiag_family == AF_INET)
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000294 sk = inet_lookup(net, hashinfo, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300295 req->id.idiag_dport, req->id.idiag_src[0],
296 req->id.idiag_sport, req->id.idiag_if);
Eric Dumazetdfd56b82011-12-10 09:48:31 +0000297#if IS_ENABLED(CONFIG_IPV6)
Eric Dumazete31c5e02015-03-10 07:15:53 -0700298 else if (req->sdiag_family == AF_INET6)
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000299 sk = inet6_lookup(net, hashinfo,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300300 (struct in6_addr *)req->id.idiag_dst,
301 req->id.idiag_dport,
302 (struct in6_addr *)req->id.idiag_src,
303 req->id.idiag_sport,
304 req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305#endif
Eric Dumazete31c5e02015-03-10 07:15:53 -0700306 else
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000307 goto out_nosk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
Herbert Xud523a322007-12-03 15:51:25 +1100309 err = -ENOENT;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700310 if (!sk)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000311 goto out_nosk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +0000313 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
Pavel Emelyanovb005ab42011-12-09 06:21:53 +0000314 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 goto out;
316
Thomas Graf6e277ed2012-06-26 23:36:12 +0000317 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
318 sizeof(struct inet_diag_meminfo) +
319 sizeof(struct tcp_info) + 64, GFP_KERNEL);
320 if (!rep) {
321 err = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 goto out;
Thomas Graf6e277ed2012-06-26 23:36:12 +0000323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000325 err = sk_diag_fill(sk, rep, req,
Patrick McHardye32123e2013-04-17 06:46:57 +0000326 sk_user_ns(NETLINK_CB(in_skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000327 NETLINK_CB(in_skb).portid,
Patrick McHardy26932562007-01-31 23:16:40 -0800328 nlh->nlmsg_seq, 0, nlh);
329 if (err < 0) {
330 WARN_ON(err == -EMSGSIZE);
Thomas Graf6e277ed2012-06-26 23:36:12 +0000331 nlmsg_free(rep);
Patrick McHardy26932562007-01-31 23:16:40 -0800332 goto out;
333 }
Eric W. Biederman15e47302012-09-07 20:12:54 +0000334 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300335 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 if (err > 0)
337 err = 0;
338
339out:
Eric Dumazetc1d607c2013-10-11 08:54:49 -0700340 if (sk)
341 sock_gen_put(sk);
342
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000343out_nosk:
344 return err;
345}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000346EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000347
348static int inet_diag_get_exact(struct sk_buff *in_skb,
349 const struct nlmsghdr *nlh,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700350 const struct inet_diag_req_v2 *req)
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000351{
352 const struct inet_diag_handler *handler;
353 int err;
354
355 handler = inet_diag_lock_handler(req->sdiag_protocol);
356 if (IS_ERR(handler))
357 err = PTR_ERR(handler);
358 else
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000359 err = handler->dump_one(in_skb, nlh, req);
Herbert Xud523a322007-12-03 15:51:25 +1100360 inet_diag_unlock_handler(handler);
Pavel Emelyanov476f7db2011-12-09 06:22:10 +0000361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 return err;
363}
364
Al Viro9f855292006-09-27 18:44:30 -0700365static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366{
367 int words = bits >> 5;
368
369 bits &= 0x1f;
370
371 if (words) {
372 if (memcmp(a1, a2, words << 2))
373 return 0;
374 }
375 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700376 __be32 w1, w2;
377 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 w1 = a1[words];
380 w2 = a2[words];
381
382 mask = htonl((0xffffffff) << (32 - bits));
383
384 if ((w1 ^ w2) & mask)
385 return 0;
386 }
387
388 return 1;
389}
390
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000391static int inet_diag_bc_run(const struct nlattr *_bc,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700392 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000394 const void *bc = nla_data(_bc);
395 int len = nla_len(_bc);
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 while (len > 0) {
398 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300399 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300402 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300404 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 yes = 0;
406 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300407 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 yes = entry->sport >= op[1].no;
409 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300410 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800411 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300413 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 yes = entry->dport >= op[1].no;
415 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300416 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 yes = entry->dport <= op[1].no;
418 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300419 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
421 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300422 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300423 case INET_DIAG_BC_D_COND: {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700424 const struct inet_diag_hostcond *cond;
425 const __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Eric Dumazete31c5e02015-03-10 07:15:53 -0700427 cond = (const struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300429 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 entry->sport : entry->dport)) {
431 yes = 0;
432 break;
433 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800434
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300435 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 addr = entry->saddr;
437 else
438 addr = entry->daddr;
439
Neal Cardwellf67caec2012-12-08 19:43:23 +0000440 if (cond->family != AF_UNSPEC &&
441 cond->family != entry->family) {
442 if (entry->family == AF_INET6 &&
443 cond->family == AF_INET) {
444 if (addr[0] == 0 && addr[1] == 0 &&
445 addr[2] == htonl(0xffff) &&
446 bitstring_match(addr + 3,
447 cond->addr,
448 cond->prefix_len))
449 break;
450 }
451 yes = 0;
452 break;
453 }
454
455 if (cond->prefix_len == 0)
456 break;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800457 if (bitstring_match(addr, cond->addr,
458 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 yes = 0;
461 break;
462 }
463 }
464
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800465 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 len -= op->yes;
467 bc += op->yes;
468 } else {
469 len -= op->no;
470 bc += op->no;
471 }
472 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000473 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Eric Dumazeta4458342015-03-13 15:51:12 -0700476/* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
477 */
478static void entry_fill_addrs(struct inet_diag_entry *entry,
479 const struct sock *sk)
480{
481#if IS_ENABLED(CONFIG_IPV6)
482 if (sk->sk_family == AF_INET6) {
483 entry->saddr = sk->sk_v6_rcv_saddr.s6_addr32;
484 entry->daddr = sk->sk_v6_daddr.s6_addr32;
485 } else
486#endif
487 {
488 entry->saddr = &sk->sk_rcv_saddr;
489 entry->daddr = &sk->sk_daddr;
490 }
491}
492
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000493int inet_diag_bc_sk(const struct nlattr *bc, struct sock *sk)
494{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000495 struct inet_sock *inet = inet_sk(sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700496 struct inet_diag_entry entry;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000497
Eric Dumazete31c5e02015-03-10 07:15:53 -0700498 if (!bc)
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000499 return 1;
500
501 entry.family = sk->sk_family;
Eric Dumazeta4458342015-03-13 15:51:12 -0700502 entry_fill_addrs(&entry, sk);
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000503 entry.sport = inet->inet_num;
504 entry.dport = ntohs(inet->inet_dport);
Eric Dumazet49612722015-03-05 10:18:14 -0800505 entry.userlocks = (sk->sk_state != TCP_TIME_WAIT) ? sk->sk_userlocks : 0;
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000506
507 return inet_diag_bc_run(bc, &entry);
508}
509EXPORT_SYMBOL_GPL(inet_diag_bc_sk);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static int valid_cc(const void *bc, int len, int cc)
512{
513 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300514 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
516 if (cc > len)
517 return 0;
518 if (cc == len)
519 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400520 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 0;
522 len -= op->yes;
523 bc += op->yes;
524 }
525 return 0;
526}
527
Neal Cardwell405c0052012-12-08 19:43:22 +0000528/* Validate an inet_diag_hostcond. */
529static bool valid_hostcond(const struct inet_diag_bc_op *op, int len,
530 int *min_len)
531{
Neal Cardwell405c0052012-12-08 19:43:22 +0000532 struct inet_diag_hostcond *cond;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700533 int addr_len;
Neal Cardwell405c0052012-12-08 19:43:22 +0000534
535 /* Check hostcond space. */
536 *min_len += sizeof(struct inet_diag_hostcond);
537 if (len < *min_len)
538 return false;
539 cond = (struct inet_diag_hostcond *)(op + 1);
540
541 /* Check address family and address length. */
542 switch (cond->family) {
543 case AF_UNSPEC:
544 addr_len = 0;
545 break;
546 case AF_INET:
547 addr_len = sizeof(struct in_addr);
548 break;
549 case AF_INET6:
550 addr_len = sizeof(struct in6_addr);
551 break;
552 default:
553 return false;
554 }
555 *min_len += addr_len;
556 if (len < *min_len)
557 return false;
558
559 /* Check prefix length (in bits) vs address length (in bytes). */
560 if (cond->prefix_len > 8 * addr_len)
561 return false;
562
563 return true;
564}
565
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000566/* Validate a port comparison operator. */
Eric Dumazete31c5e02015-03-10 07:15:53 -0700567static bool valid_port_comparison(const struct inet_diag_bc_op *op,
568 int len, int *min_len)
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000569{
570 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
571 *min_len += sizeof(struct inet_diag_bc_op);
572 if (len < *min_len)
573 return false;
574 return true;
575}
576
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300577static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Eric Dumazeteeb14972011-06-17 16:25:39 -0400579 const void *bc = bytecode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 int len = bytecode_len;
581
582 while (len > 0) {
Neal Cardwell405c0052012-12-08 19:43:22 +0000583 int min_len = sizeof(struct inet_diag_bc_op);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700584 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300587 case INET_DIAG_BC_S_COND:
588 case INET_DIAG_BC_D_COND:
Neal Cardwell405c0052012-12-08 19:43:22 +0000589 if (!valid_hostcond(bc, len, &min_len))
590 return -EINVAL;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000591 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300592 case INET_DIAG_BC_S_GE:
593 case INET_DIAG_BC_S_LE:
594 case INET_DIAG_BC_D_GE:
595 case INET_DIAG_BC_D_LE:
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000596 if (!valid_port_comparison(bc, len, &min_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return -EINVAL;
598 break;
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000599 case INET_DIAG_BC_AUTO:
600 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300601 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 break;
603 default:
604 return -EINVAL;
605 }
Neal Cardwell5e1f5422012-12-09 11:09:54 +0000606
607 if (op->code != INET_DIAG_BC_NOP) {
608 if (op->no < min_len || op->no > len + 4 || op->no & 3)
609 return -EINVAL;
610 if (op->no < len &&
611 !valid_cc(bytecode, bytecode_len, len - op->no))
612 return -EINVAL;
613 }
614
Neal Cardwell405c0052012-12-08 19:43:22 +0000615 if (op->yes < min_len || op->yes > len + 4 || op->yes & 3)
Eric Dumazeteeb14972011-06-17 16:25:39 -0400616 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800617 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 len -= op->yes;
619 }
620 return len == 0 ? 0 : -EINVAL;
621}
622
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800623static int inet_csk_diag_dump(struct sock *sk,
624 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000625 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700626 const struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000627 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628{
Pavel Emelyanov8d07d152011-12-09 06:22:44 +0000629 if (!inet_diag_bc_sk(bc, sk))
630 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000632 return inet_csk_diag_fill(sk, skb, r,
Patrick McHardye32123e2013-04-17 06:46:57 +0000633 sk_user_ns(NETLINK_CB(cb->skb).sk),
Eric W. Biederman15e47302012-09-07 20:12:54 +0000634 NETLINK_CB(cb->skb).portid,
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800635 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636}
637
Eric Dumazet49612722015-03-05 10:18:14 -0800638static void twsk_build_assert(void)
639{
640 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
641 offsetof(struct sock, sk_family));
642
643 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
644 offsetof(struct inet_sock, inet_num));
645
646 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
647 offsetof(struct inet_sock, inet_dport));
648
649 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
650 offsetof(struct inet_sock, inet_rcv_saddr));
651
652 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
653 offsetof(struct inet_sock, inet_daddr));
654
655#if IS_ENABLED(CONFIG_IPV6)
656 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
657 offsetof(struct sock, sk_v6_rcv_saddr));
658
659 BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
660 offsetof(struct sock, sk_v6_daddr));
661#endif
662}
663
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700664static int inet_twsk_diag_dump(struct sock *sk,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800665 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000666 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700667 const struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000668 const struct nlattr *bc)
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800669{
Eric Dumazet49612722015-03-05 10:18:14 -0800670 twsk_build_assert();
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700671
Eric Dumazet49612722015-03-05 10:18:14 -0800672 if (!inet_diag_bc_sk(bc, sk))
673 return 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800674
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700675 return inet_twsk_diag_fill(sk, skb, r,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000676 NETLINK_CB(cb->skb).portid,
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800677 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
678}
679
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300680static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600681 struct request_sock *req,
682 struct user_namespace *user_ns,
Eric W. Biederman15e47302012-09-07 20:12:54 +0000683 u32 portid, u32 seq,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800684 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700686 const struct inet_request_sock *ireq = inet_rsk(req);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300687 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 struct nlmsghdr *nlh;
689 long tmo;
690
Eric W. Biederman15e47302012-09-07 20:12:54 +0000691 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
Thomas Graf6e277ed2012-06-26 23:36:12 +0000692 NLM_F_MULTI);
693 if (!nlh)
694 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Thomas Graf6e277ed2012-06-26 23:36:12 +0000696 r = nlmsg_data(nlh);
Eric Dumazeta4458342015-03-13 15:51:12 -0700697 inet_diag_msg_common_fill(r, (struct sock *)ireq);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300698 r->idiag_state = TCP_SYN_RECV;
699 r->idiag_timer = 1;
Eric Dumazete6c022a2012-10-27 23:16:46 +0000700 r->idiag_retrans = req->num_retrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Eric Dumazet33cf7c92015-03-11 18:53:14 -0700702 BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
703 offsetof(struct sock, sk_cookie));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 tmo = req->expires - jiffies;
706 if (tmo < 0)
707 tmo = 0;
708
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300709 r->idiag_expires = jiffies_to_msecs(tmo);
710 r->idiag_rqueue = 0;
711 r->idiag_wqueue = 0;
Eric W. Biedermand06ca952012-05-24 17:58:08 -0600712 r->idiag_uid = from_kuid_munged(user_ns, sock_i_uid(sk));
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300713 r->idiag_inode = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Johannes Berg053c0952015-01-16 22:09:00 +0100715 nlmsg_end(skb, nlh);
716 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717}
718
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300719static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000720 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700721 const struct inet_diag_req_v2 *r,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000722 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700724 struct inet_connection_sock *icsk = inet_csk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct inet_sock *inet = inet_sk(sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700726 struct inet_diag_entry entry;
727 int j, s_j, reqnum, s_reqnum;
728 struct listen_sock *lopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 int err = 0;
730
731 s_j = cb->args[3];
732 s_reqnum = cb->args[4];
733
734 if (s_j > 0)
735 s_j--;
736
737 entry.family = sk->sk_family;
738
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700739 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700741 lopt = icsk->icsk_accept_queue.listen_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 if (!lopt || !lopt->qlen)
743 goto out;
744
Eric Dumazete31c5e02015-03-10 07:15:53 -0700745 if (bc) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000746 entry.sport = inet->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 entry.userlocks = sk->sk_userlocks;
748 }
749
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300750 for (j = s_j; j < lopt->nr_table_entries; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700751 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
753 reqnum = 0;
754 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700755 struct inet_request_sock *ireq = inet_rsk(req);
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (reqnum < s_reqnum)
758 continue;
Eric Dumazet634fb9792013-10-09 15:21:29 -0700759 if (r->id.idiag_dport != ireq->ir_rmt_port &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300760 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 continue;
762
763 if (bc) {
Eric Dumazeta4458342015-03-13 15:51:12 -0700764 /* Note: entry.sport and entry.userlocks are already set */
765 entry_fill_addrs(&entry, (struct sock *)req);
Eric Dumazet634fb9792013-10-09 15:21:29 -0700766 entry.dport = ntohs(ireq->ir_rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Pavel Emelyanov87c22ea2011-12-09 06:21:34 +0000768 if (!inet_diag_bc_run(bc, &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 continue;
770 }
771
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300772 err = inet_diag_fill_req(skb, sk, req,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700773 sk_user_ns(NETLINK_CB(cb->skb).sk),
774 NETLINK_CB(cb->skb).portid,
775 cb->nlh->nlmsg_seq, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (err < 0) {
777 cb->args[3] = j + 1;
778 cb->args[4] = reqnum;
779 goto out;
780 }
781 }
782
783 s_reqnum = 0;
784 }
785
786out:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700787 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 return err;
790}
791
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000792void inet_diag_dump_icsk(struct inet_hashinfo *hashinfo, struct sk_buff *skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700793 struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700794 const struct inet_diag_req_v2 *r, struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000796 struct net *net = sock_net(skb->sk);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700797 int i, num, s_i, s_num;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 s_i = cb->args[1];
800 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 if (cb->args[0] == 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300803 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300805
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700806 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800807 struct inet_listen_hashbucket *ilb;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700808 struct hlist_nulls_node *node;
809 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800812 ilb = &hashinfo->listening_hash[i];
813 spin_lock_bh(&ilb->lock);
Eric Dumazetc25eb3b2008-11-23 17:22:55 -0800814 sk_nulls_for_each(sk, node, &ilb->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 struct inet_sock *inet = inet_sk(sk);
816
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000817 if (!net_eq(sock_net(sk), net))
818 continue;
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 if (num < s_num) {
821 num++;
822 continue;
823 }
824
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000825 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazete31c5e02015-03-10 07:15:53 -0700826 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000827 goto next_listen;
828
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000829 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300830 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 goto next_listen;
832
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300833 if (!(r->idiag_states & TCPF_LISTEN) ||
834 r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 cb->args[3] > 0)
836 goto syn_recv;
837
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000838 if (inet_csk_diag_dump(sk, skb, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800839 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 goto done;
841 }
842
843syn_recv:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300844 if (!(r->idiag_states & TCPF_SYN_RECV))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 goto next_listen;
846
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000847 if (inet_diag_dump_reqs(skb, sk, cb, r, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800848 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 goto done;
850 }
851
852next_listen:
853 cb->args[3] = 0;
854 cb->args[4] = 0;
855 ++num;
856 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800857 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858
859 s_num = 0;
860 cb->args[3] = 0;
861 cb->args[4] = 0;
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863skip_listen_ht:
864 cb->args[0] = 1;
865 s_i = num = s_num = 0;
866 }
867
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300868 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000869 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Eric Dumazetf373b532009-10-09 00:16:19 +0000871 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300872 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800873 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800874 struct hlist_nulls_node *node;
Eric Dumazete31c5e02015-03-10 07:15:53 -0700875 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Andi Kleen6be547a2008-08-28 01:09:54 -0700877 num = 0;
878
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700879 if (hlist_nulls_empty(&head->chain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700880 continue;
881
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 if (i > s_i)
883 s_num = 0;
884
David S. Miller7e3aab42008-11-21 16:39:19 -0800885 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800886 sk_nulls_for_each(sk, node, &head->chain) {
Eric Dumazete31c5e02015-03-10 07:15:53 -0700887 int state, res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000889 if (!net_eq(sock_net(sk), net))
890 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if (num < s_num)
892 goto next_normal;
Neal Cardwell70315d22014-01-10 15:34:45 -0500893 state = (sk->sk_state == TCP_TIME_WAIT) ?
894 inet_twsk(sk)->tw_substate : sk->sk_state;
895 if (!(r->idiag_states & (1 << state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 goto next_normal;
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000897 if (r->sdiag_family != AF_UNSPEC &&
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700898 sk->sk_family != r->sdiag_family)
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000899 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700900 if (r->id.idiag_sport != htons(sk->sk_num) &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300901 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700903 if (r->id.idiag_dport != sk->sk_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800904 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 goto next_normal;
Eric Dumazet05dbc7b2013-10-03 00:22:02 -0700906 if (sk->sk_state == TCP_TIME_WAIT)
907 res = inet_twsk_diag_dump(sk, skb, cb, r, bc);
908 else
909 res = inet_csk_diag_dump(sk, skb, cb, r, bc);
910 if (res < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800911 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 goto done;
913 }
914next_normal:
915 ++num;
916 }
917
David S. Miller7e3aab42008-11-21 16:39:19 -0800918 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 }
920
921done:
922 cb->args[1] = i;
923 cb->args[2] = num;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000924out:
925 ;
926}
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000927EXPORT_SYMBOL_GPL(inet_diag_dump_icsk);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000928
929static int __inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
Eric Dumazet34160ea2015-03-10 07:15:54 -0700930 const struct inet_diag_req_v2 *r,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700931 struct nlattr *bc)
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000932{
933 const struct inet_diag_handler *handler;
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000934 int err = 0;
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000935
936 handler = inet_diag_lock_handler(r->sdiag_protocol);
937 if (!IS_ERR(handler))
Pavel Emelyanov1942c512011-12-09 06:23:18 +0000938 handler->dump(skb, cb, r, bc);
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000939 else
940 err = PTR_ERR(handler);
Herbert Xud523a322007-12-03 15:51:25 +1100941 inet_diag_unlock_handler(handler);
Pavel Emelyanovefb3cb42011-12-09 06:22:26 +0000942
Cyrill Gorcunovcacb6ba2012-11-03 09:30:34 +0000943 return err ? : skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944}
945
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000946static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
947{
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000948 int hdrlen = sizeof(struct inet_diag_req_v2);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700949 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000950
951 if (nlmsg_attrlen(cb->nlh, hdrlen))
952 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
953
David S. Millerd1063522012-06-26 21:28:54 -0700954 return __inet_diag_dump(skb, cb, nlmsg_data(cb->nlh), bc);
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000955}
956
Eric Dumazete31c5e02015-03-10 07:15:53 -0700957static int inet_diag_type2proto(int type)
Pavel Emelyanova029fe22011-12-06 07:59:32 +0000958{
959 switch (type) {
960 case TCPDIAG_GETSOCK:
961 return IPPROTO_TCP;
962 case DCCPDIAG_GETSOCK:
963 return IPPROTO_DCCP;
964 default:
965 return 0;
966 }
967}
968
Eric Dumazete31c5e02015-03-10 07:15:53 -0700969static int inet_diag_dump_compat(struct sk_buff *skb,
970 struct netlink_callback *cb)
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000971{
David S. Millerd1063522012-06-26 21:28:54 -0700972 struct inet_diag_req *rc = nlmsg_data(cb->nlh);
Eric Dumazete31c5e02015-03-10 07:15:53 -0700973 int hdrlen = sizeof(struct inet_diag_req);
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000974 struct inet_diag_req_v2 req;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000975 struct nlattr *bc = NULL;
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000976
Pavel Emelyanovd23deaa2011-12-06 07:59:15 +0000977 req.sdiag_family = AF_UNSPEC; /* compatibility */
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +0000978 req.sdiag_protocol = inet_diag_type2proto(cb->nlh->nlmsg_type);
979 req.idiag_ext = rc->idiag_ext;
980 req.idiag_states = rc->idiag_states;
981 req.id = rc->id;
982
983 if (nlmsg_attrlen(cb->nlh, hdrlen))
984 bc = nlmsg_find_attr(cb->nlh, hdrlen, INET_DIAG_REQ_BYTECODE);
985
986 return __inet_diag_dump(skb, cb, &req, bc);
987}
988
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000989static int inet_diag_get_exact_compat(struct sk_buff *in_skb,
Eric Dumazete31c5e02015-03-10 07:15:53 -0700990 const struct nlmsghdr *nlh)
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000991{
David S. Millerd1063522012-06-26 21:28:54 -0700992 struct inet_diag_req *rc = nlmsg_data(nlh);
Pavel Emelyanovc8991362012-01-10 22:36:35 +0000993 struct inet_diag_req_v2 req;
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +0000994
995 req.sdiag_family = rc->idiag_family;
996 req.sdiag_protocol = inet_diag_type2proto(nlh->nlmsg_type);
997 req.idiag_ext = rc->idiag_ext;
998 req.idiag_states = rc->idiag_states;
999 req.id = rc->id;
1000
1001 return inet_diag_get_exact(in_skb, nlh, &req);
1002}
1003
Pavel Emelyanov8d341722011-12-06 07:57:06 +00001004static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
Pavel Emelyanov3b09c842012-01-10 22:37:26 +00001006 int hdrlen = sizeof(struct inet_diag_req);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001007 struct net *net = sock_net(skb->sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Thomas Grafead592b2007-03-22 23:30:35 -07001009 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
1010 nlmsg_len(nlh) < hdrlen)
1011 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
David S. Millerb8f3ab42011-01-18 12:40:38 -08001013 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -07001014 if (nlmsg_attrlen(nlh, hdrlen)) {
1015 struct nlattr *attr;
1016
1017 attr = nlmsg_find_attr(nlh, hdrlen,
1018 INET_DIAG_REQ_BYTECODE);
Eric Dumazete31c5e02015-03-10 07:15:53 -07001019 if (!attr ||
Thomas Grafead592b2007-03-22 23:30:35 -07001020 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1021 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1022 return -EINVAL;
1023 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001024 {
1025 struct netlink_dump_control c = {
1026 .dump = inet_diag_dump_compat,
1027 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001028 return netlink_dump_start(net->diag_nlsk, skb, nlh, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001029 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
Thomas Grafead592b2007-03-22 23:30:35 -07001031
Pavel Emelyanovfe50ce22011-12-06 07:58:39 +00001032 return inet_diag_get_exact_compat(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033}
1034
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001035static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
1036{
Pavel Emelyanovc8991362012-01-10 22:36:35 +00001037 int hdrlen = sizeof(struct inet_diag_req_v2);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001038 struct net *net = sock_net(skb->sk);
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001039
1040 if (nlmsg_len(h) < hdrlen)
1041 return -EINVAL;
1042
1043 if (h->nlmsg_flags & NLM_F_DUMP) {
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001044 if (nlmsg_attrlen(h, hdrlen)) {
1045 struct nlattr *attr;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001046
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001047 attr = nlmsg_find_attr(h, hdrlen,
1048 INET_DIAG_REQ_BYTECODE);
Eric Dumazete31c5e02015-03-10 07:15:53 -07001049 if (!attr ||
Pavel Emelyanov25c4cd22011-12-06 07:58:58 +00001050 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
1051 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
1052 return -EINVAL;
1053 }
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001054 {
1055 struct netlink_dump_control c = {
1056 .dump = inet_diag_dump,
1057 };
Andrey Vagin51d7ccc2012-07-16 04:28:49 +00001058 return netlink_dump_start(net->diag_nlsk, skb, h, &c);
Pablo Neira Ayuso80d326f2012-02-24 14:30:15 +00001059 }
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001060 }
1061
David S. Millerd1063522012-06-26 21:28:54 -07001062 return inet_diag_get_exact(skb, h, nlmsg_data(h));
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001063}
1064
Shan Wei8dcf01f2012-04-24 18:21:07 +00001065static const struct sock_diag_handler inet_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001066 .family = AF_INET,
1067 .dump = inet_diag_handler_dump,
1068};
1069
Shan Wei8dcf01f2012-04-24 18:21:07 +00001070static const struct sock_diag_handler inet6_diag_handler = {
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001071 .family = AF_INET6,
1072 .dump = inet_diag_handler_dump,
1073};
1074
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001075int inet_diag_register(const struct inet_diag_handler *h)
1076{
1077 const __u16 type = h->idiag_type;
1078 int err = -EINVAL;
1079
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001080 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001081 goto out;
1082
Herbert Xud523a322007-12-03 15:51:25 +11001083 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001084 err = -EEXIST;
Eric Dumazete31c5e02015-03-10 07:15:53 -07001085 if (!inet_diag_table[type]) {
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001086 inet_diag_table[type] = h;
1087 err = 0;
1088 }
Herbert Xud523a322007-12-03 15:51:25 +11001089 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001090out:
1091 return err;
1092}
1093EXPORT_SYMBOL_GPL(inet_diag_register);
1094
1095void inet_diag_unregister(const struct inet_diag_handler *h)
1096{
1097 const __u16 type = h->idiag_type;
1098
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001099 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001100 return;
1101
Herbert Xud523a322007-12-03 15:51:25 +11001102 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001103 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +11001104 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001105}
1106EXPORT_SYMBOL_GPL(inet_diag_unregister);
1107
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001108static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001110 const int inet_diag_table_size = (IPPROTO_MAX *
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001111 sizeof(struct inet_diag_handler *));
1112 int err = -ENOMEM;
1113
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001114 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001115 if (!inet_diag_table)
1116 goto out;
1117
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001118 err = sock_diag_register(&inet_diag_handler);
1119 if (err)
1120 goto out_free_nl;
1121
1122 err = sock_diag_register(&inet6_diag_handler);
1123 if (err)
1124 goto out_free_inet;
1125
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001126 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001127out:
1128 return err;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001129
1130out_free_inet:
1131 sock_diag_unregister(&inet_diag_handler);
1132out_free_nl:
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001133 kfree(inet_diag_table);
1134 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001137static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001139 sock_diag_unregister(&inet6_diag_handler);
1140 sock_diag_unregister(&inet_diag_handler);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001141 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001142 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001145module_init(inet_diag_init);
1146module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147MODULE_LICENSE("GPL");
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +00001148MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2 /* AF_INET */);
1149MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10 /* AF_INET6 */);