blob: f37b1284b46bcdb18c09c15c1250f63009821db6 [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
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +000049static struct sock *sdiagnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030051#define INET_DIAG_PUT(skb, attrtype, attrlen) \
Stephen Hemminger7c99c902005-06-23 12:20:36 -070052 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000054static inline int inet_diag_type2proto(int type)
55{
56 switch (type) {
57 case TCPDIAG_GETSOCK:
58 return IPPROTO_TCP;
59 case DCCPDIAG_GETSOCK:
60 return IPPROTO_DCCP;
61 default:
62 return 0;
63 }
64}
65
Herbert Xud523a322007-12-03 15:51:25 +110066static DEFINE_MUTEX(inet_diag_table_mutex);
67
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000068static const struct inet_diag_handler *inet_diag_lock_handler(int proto)
Herbert Xud523a322007-12-03 15:51:25 +110069{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000070 if (!inet_diag_table[proto])
Herbert Xud523a322007-12-03 15:51:25 +110071 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000072 NETLINK_SOCK_DIAG, proto);
Herbert Xud523a322007-12-03 15:51:25 +110073
74 mutex_lock(&inet_diag_table_mutex);
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000075 if (!inet_diag_table[proto])
Herbert Xud523a322007-12-03 15:51:25 +110076 return ERR_PTR(-ENOENT);
77
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +000078 return inet_diag_table[proto];
Herbert Xud523a322007-12-03 15:51:25 +110079}
80
81static inline void inet_diag_unlock_handler(
82 const struct inet_diag_handler *handler)
83{
84 mutex_unlock(&inet_diag_table_mutex);
85}
86
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -080087static int inet_csk_diag_fill(struct sock *sk,
88 struct sk_buff *skb,
89 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
90 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070092 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070093 const struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030094 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030096 void *info = NULL;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030097 struct inet_diag_meminfo *minfo = NULL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070098 unsigned char *b = skb_tail_pointer(skb);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030099 const struct inet_diag_handler *handler;
100
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +0000101 handler = inet_diag_table[inet_diag_type2proto(unlh->nlmsg_type)];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300102 BUG_ON(handler == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300104 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 nlh->nlmsg_flags = nlmsg_flags;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300106
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 r = NLMSG_DATA(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800108 BUG_ON(sk->sk_state == TCP_TIME_WAIT);
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800109
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800110 if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
111 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo));
112
113 if (ext & (1 << (INET_DIAG_INFO - 1)))
114 info = INET_DIAG_PUT(skb, INET_DIAG_INFO,
115 handler->idiag_info_size);
116
117 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
118 const size_t len = strlen(icsk->icsk_ca_ops->name);
119
120 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
121 icsk->icsk_ca_ops->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 }
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800123
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300124 r->idiag_family = sk->sk_family;
125 r->idiag_state = sk->sk_state;
126 r->idiag_timer = 0;
127 r->idiag_retrans = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300129 r->id.idiag_if = sk->sk_bound_dev_if;
130 r->id.idiag_cookie[0] = (u32)(unsigned long)sk;
131 r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000133 r->id.idiag_sport = inet->inet_sport;
134 r->id.idiag_dport = inet->inet_dport;
135 r->id.idiag_src[0] = inet->inet_rcv_saddr;
136 r->id.idiag_dst[0] = inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
Maciej Żenczykowski717b6d82011-11-22 16:03:10 -0500138 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
139 * hence this needs to be included regardless of socket family.
140 */
141 if (ext & (1 << (INET_DIAG_TOS - 1)))
142 RTA_PUT_U8(skb, INET_DIAG_TOS, inet->tos);
143
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300144#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300145 if (r->idiag_family == AF_INET6) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000146 const struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000148 *(struct in6_addr *)r->id.idiag_src = np->rcv_saddr;
149 *(struct in6_addr *)r->id.idiag_dst = np->daddr;
Maciej Żenczykowski06236ac2011-11-07 14:23:11 +0000150 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
151 RTA_PUT_U8(skb, INET_DIAG_TCLASS, np->tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153#endif
154
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700155#define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700157 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300158 r->idiag_timer = 1;
159 r->idiag_retrans = icsk->icsk_retransmits;
160 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700161 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300162 r->idiag_timer = 4;
163 r->idiag_retrans = icsk->icsk_probes_out;
164 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300166 r->idiag_timer = 2;
167 r->idiag_retrans = icsk->icsk_probes_out;
168 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300170 r->idiag_timer = 0;
171 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173#undef EXPIRES_IN_MS
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300174
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300175 r->idiag_uid = sock_i_uid(sk);
176 r->idiag_inode = sock_i_ino(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 if (minfo) {
Eric Dumazet31e6d362009-06-17 19:05:41 -0700179 minfo->idiag_rmem = sk_rmem_alloc_get(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300180 minfo->idiag_wmem = sk->sk_wmem_queued;
181 minfo->idiag_fmem = sk->sk_forward_alloc;
Eric Dumazet31e6d362009-06-17 19:05:41 -0700182 minfo->idiag_tmem = sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
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
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700191 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return skb->len;
193
Stephen Hemminger7c99c902005-06-23 12:20:36 -0700194rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700196 nlmsg_trim(skb, b);
Patrick McHardy26932562007-01-31 23:16:40 -0800197 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198}
199
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800200static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
201 struct sk_buff *skb, int ext, u32 pid,
202 u32 seq, u16 nlmsg_flags,
203 const struct nlmsghdr *unlh)
204{
205 long tmo;
206 struct inet_diag_msg *r;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700207 const unsigned char *previous_tail = skb_tail_pointer(skb);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800208 struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq,
209 unlh->nlmsg_type, sizeof(*r));
210
211 r = NLMSG_DATA(nlh);
212 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
213
214 nlh->nlmsg_flags = nlmsg_flags;
215
216 tmo = tw->tw_ttd - jiffies;
217 if (tmo < 0)
218 tmo = 0;
219
220 r->idiag_family = tw->tw_family;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800221 r->idiag_retrans = 0;
222 r->id.idiag_if = tw->tw_bound_dev_if;
223 r->id.idiag_cookie[0] = (u32)(unsigned long)tw;
224 r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1);
225 r->id.idiag_sport = tw->tw_sport;
226 r->id.idiag_dport = tw->tw_dport;
227 r->id.idiag_src[0] = tw->tw_rcv_saddr;
228 r->id.idiag_dst[0] = tw->tw_daddr;
229 r->idiag_state = tw->tw_substate;
230 r->idiag_timer = 3;
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700231 r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800232 r->idiag_rqueue = 0;
233 r->idiag_wqueue = 0;
234 r->idiag_uid = 0;
235 r->idiag_inode = 0;
236#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
237 if (tw->tw_family == AF_INET6) {
238 const struct inet6_timewait_sock *tw6 =
239 inet6_twsk((struct sock *)tw);
240
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000241 *(struct in6_addr *)r->id.idiag_src = tw6->tw_v6_rcv_saddr;
242 *(struct in6_addr *)r->id.idiag_dst = tw6->tw_v6_daddr;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800243 }
244#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700245 nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800246 return skb->len;
247nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700248 nlmsg_trim(skb, previous_tail);
Patrick McHardy26932562007-01-31 23:16:40 -0800249 return -EMSGSIZE;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800250}
251
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800252static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
253 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
254 const struct nlmsghdr *unlh)
255{
256 if (sk->sk_state == TCP_TIME_WAIT)
257 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
258 skb, ext, pid, seq, nlmsg_flags,
259 unlh);
260 return inet_csk_diag_fill(sk, skb, ext, pid, seq, nlmsg_flags, unlh);
261}
262
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800263static int inet_diag_get_exact(struct sk_buff *in_skb,
264 const struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 int err;
267 struct sock *sk;
Pavel Emelyanov126fdc32011-12-06 07:58:21 +0000268 struct inet_diag_req_compat *req = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct sk_buff *rep;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300270 struct inet_hashinfo *hashinfo;
271 const struct inet_diag_handler *handler;
272
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +0000273 handler = inet_diag_lock_handler(inet_diag_type2proto(nlh->nlmsg_type));
Arnaldo Carvalho de Melo8cf8e5a2008-01-28 20:52:12 -0800274 if (IS_ERR(handler)) {
275 err = PTR_ERR(handler);
276 goto unlock;
277 }
Herbert Xud523a322007-12-03 15:51:25 +1100278
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300279 hashinfo = handler->idiag_hashinfo;
Herbert Xud523a322007-12-03 15:51:25 +1100280 err = -EINVAL;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300281
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300282 if (req->idiag_family == AF_INET) {
Pavel Emelyanovc67499c2008-01-31 05:06:40 -0800283 sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300284 req->id.idiag_dport, req->id.idiag_src[0],
285 req->id.idiag_sport, req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286 }
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300287#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300288 else if (req->idiag_family == AF_INET6) {
Pavel Emelyanovd86e0da2008-01-31 05:07:21 -0800289 sk = inet6_lookup(&init_net, hashinfo,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300290 (struct in6_addr *)req->id.idiag_dst,
291 req->id.idiag_dport,
292 (struct in6_addr *)req->id.idiag_src,
293 req->id.idiag_sport,
294 req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296#endif
297 else {
Herbert Xud523a322007-12-03 15:51:25 +1100298 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
Herbert Xud523a322007-12-03 15:51:25 +1100301 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 if (sk == NULL)
Herbert Xud523a322007-12-03 15:51:25 +1100303 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 err = -ESTALE;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300306 if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
307 req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
308 ((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
309 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 goto out;
311
312 err = -ENOMEM;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300313 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
314 sizeof(struct inet_diag_meminfo) +
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300315 handler->idiag_info_size + 64)),
316 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (!rep)
318 goto out;
319
Patrick McHardy26932562007-01-31 23:16:40 -0800320 err = sk_diag_fill(sk, rep, req->idiag_ext,
321 NETLINK_CB(in_skb).pid,
322 nlh->nlmsg_seq, 0, nlh);
323 if (err < 0) {
324 WARN_ON(err == -EMSGSIZE);
325 kfree_skb(rep);
326 goto out;
327 }
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +0000328 err = netlink_unicast(sdiagnl, rep, NETLINK_CB(in_skb).pid,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300329 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (err > 0)
331 err = 0;
332
333out:
334 if (sk) {
335 if (sk->sk_state == TCP_TIME_WAIT)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700336 inet_twsk_put((struct inet_timewait_sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 else
338 sock_put(sk);
339 }
Herbert Xud523a322007-12-03 15:51:25 +1100340unlock:
341 inet_diag_unlock_handler(handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 return err;
343}
344
Al Viro9f855292006-09-27 18:44:30 -0700345static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346{
347 int words = bits >> 5;
348
349 bits &= 0x1f;
350
351 if (words) {
352 if (memcmp(a1, a2, words << 2))
353 return 0;
354 }
355 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700356 __be32 w1, w2;
357 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
359 w1 = a1[words];
360 w2 = a2[words];
361
362 mask = htonl((0xffffffff) << (32 - bits));
363
364 if ((w1 ^ w2) & mask)
365 return 0;
366 }
367
368 return 1;
369}
370
371
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300372static int inet_diag_bc_run(const void *bc, int len,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800373 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374{
375 while (len > 0) {
376 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300377 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
379 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300380 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300382 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 yes = 0;
384 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300385 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 yes = entry->sport >= op[1].no;
387 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300388 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800389 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300391 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 yes = entry->dport >= op[1].no;
393 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300394 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 yes = entry->dport <= op[1].no;
396 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300397 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
399 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300400 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300401 case INET_DIAG_BC_D_COND: {
402 struct inet_diag_hostcond *cond;
Al Viro9f855292006-09-27 18:44:30 -0700403 __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300405 cond = (struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300407 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 entry->sport : entry->dport)) {
409 yes = 0;
410 break;
411 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800412
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 if (cond->prefix_len == 0)
414 break;
415
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300416 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 addr = entry->saddr;
418 else
419 addr = entry->daddr;
420
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800421 if (bitstring_match(addr, cond->addr,
422 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 break;
424 if (entry->family == AF_INET6 &&
425 cond->family == AF_INET) {
426 if (addr[0] == 0 && addr[1] == 0 &&
427 addr[2] == htonl(0xffff) &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300428 bitstring_match(addr + 3, cond->addr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900429 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 break;
431 }
432 yes = 0;
433 break;
434 }
435 }
436
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800437 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 len -= op->yes;
439 bc += op->yes;
440 } else {
441 len -= op->no;
442 bc += op->no;
443 }
444 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000445 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
448static int valid_cc(const void *bc, int len, int cc)
449{
450 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300451 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (cc > len)
454 return 0;
455 if (cc == len)
456 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400457 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 return 0;
459 len -= op->yes;
460 bc += op->yes;
461 }
462 return 0;
463}
464
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300465static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466{
Eric Dumazeteeb14972011-06-17 16:25:39 -0400467 const void *bc = bytecode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 int len = bytecode_len;
469
470 while (len > 0) {
Eric Dumazeteeb14972011-06-17 16:25:39 -0400471 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
474 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300475 case INET_DIAG_BC_AUTO:
476 case INET_DIAG_BC_S_COND:
477 case INET_DIAG_BC_D_COND:
478 case INET_DIAG_BC_S_GE:
479 case INET_DIAG_BC_S_LE:
480 case INET_DIAG_BC_D_GE:
481 case INET_DIAG_BC_D_LE:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300482 case INET_DIAG_BC_JMP:
Eric Dumazeteeb14972011-06-17 16:25:39 -0400483 if (op->no < 4 || op->no > len + 4 || op->no & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 return -EINVAL;
485 if (op->no < len &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300486 !valid_cc(bytecode, bytecode_len, len - op->no))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 return -EINVAL;
488 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300489 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 break;
491 default:
492 return -EINVAL;
493 }
Eric Dumazeteeb14972011-06-17 16:25:39 -0400494 if (op->yes < 4 || op->yes > len + 4 || op->yes & 3)
495 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800496 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 len -= op->yes;
498 }
499 return len == 0 ? 0 : -EINVAL;
500}
501
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800502static int inet_csk_diag_dump(struct sock *sk,
503 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000504 struct netlink_callback *cb,
505 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Pavel Emelyanov126fdc32011-12-06 07:58:21 +0000507 struct inet_diag_req_compat *r = NLMSG_DATA(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000509 if (bc != NULL) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300510 struct inet_diag_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 struct inet_sock *inet = inet_sk(sk);
512
513 entry.family = sk->sk_family;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300514#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 if (entry.family == AF_INET6) {
516 struct ipv6_pinfo *np = inet6_sk(sk);
517
518 entry.saddr = np->rcv_saddr.s6_addr32;
519 entry.daddr = np->daddr.s6_addr32;
520 } else
521#endif
522 {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000523 entry.saddr = &inet->inet_rcv_saddr;
524 entry.daddr = &inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000526 entry.sport = inet->inet_num;
527 entry.dport = ntohs(inet->inet_dport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 entry.userlocks = sk->sk_userlocks;
529
Nelson Elhage22e76c82010-11-03 16:35:41 +0000530 if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 return 0;
532 }
533
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800534 return inet_csk_diag_fill(sk, skb, r->idiag_ext,
535 NETLINK_CB(cb->skb).pid,
536 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800539static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
540 struct sk_buff *skb,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000541 struct netlink_callback *cb,
542 const struct nlattr *bc)
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800543{
Pavel Emelyanov126fdc32011-12-06 07:58:21 +0000544 struct inet_diag_req_compat *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800545
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000546 if (bc != NULL) {
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800547 struct inet_diag_entry entry;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800548
549 entry.family = tw->tw_family;
550#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
551 if (tw->tw_family == AF_INET6) {
552 struct inet6_timewait_sock *tw6 =
553 inet6_twsk((struct sock *)tw);
554 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
555 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
556 } else
557#endif
558 {
559 entry.saddr = &tw->tw_rcv_saddr;
560 entry.daddr = &tw->tw_daddr;
561 }
562 entry.sport = tw->tw_num;
563 entry.dport = ntohs(tw->tw_dport);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900564 entry.userlocks = 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800565
Nelson Elhage22e76c82010-11-03 16:35:41 +0000566 if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry))
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800567 return 0;
568 }
569
570 return inet_twsk_diag_fill(tw, skb, r->idiag_ext,
571 NETLINK_CB(cb->skb).pid,
572 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
573}
574
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300575static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800576 struct request_sock *req, u32 pid, u32 seq,
577 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700579 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700581 unsigned char *b = skb_tail_pointer(skb);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300582 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 struct nlmsghdr *nlh;
584 long tmo;
585
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300586 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 nlh->nlmsg_flags = NLM_F_MULTI;
588 r = NLMSG_DATA(nlh);
589
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300590 r->idiag_family = sk->sk_family;
591 r->idiag_state = TCP_SYN_RECV;
592 r->idiag_timer = 1;
593 r->idiag_retrans = req->retrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300595 r->id.idiag_if = sk->sk_bound_dev_if;
596 r->id.idiag_cookie[0] = (u32)(unsigned long)req;
597 r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 tmo = req->expires - jiffies;
600 if (tmo < 0)
601 tmo = 0;
602
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000603 r->id.idiag_sport = inet->inet_sport;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300604 r->id.idiag_dport = ireq->rmt_port;
605 r->id.idiag_src[0] = ireq->loc_addr;
606 r->id.idiag_dst[0] = ireq->rmt_addr;
607 r->idiag_expires = jiffies_to_msecs(tmo);
608 r->idiag_rqueue = 0;
609 r->idiag_wqueue = 0;
610 r->idiag_uid = sock_i_uid(sk);
611 r->idiag_inode = 0;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300612#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300613 if (r->idiag_family == AF_INET6) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000614 *(struct in6_addr *)r->id.idiag_src = inet6_rsk(req)->loc_addr;
615 *(struct in6_addr *)r->id.idiag_dst = inet6_rsk(req)->rmt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700618 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 return skb->len;
621
622nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700623 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 return -1;
625}
626
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300627static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000628 struct netlink_callback *cb,
629 const struct nlattr *bc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300631 struct inet_diag_entry entry;
Pavel Emelyanov126fdc32011-12-06 07:58:21 +0000632 struct inet_diag_req_compat *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700633 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo2ad69c52005-06-18 22:48:55 -0700634 struct listen_sock *lopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 struct inet_sock *inet = inet_sk(sk);
636 int j, s_j;
637 int reqnum, s_reqnum;
638 int err = 0;
639
640 s_j = cb->args[3];
641 s_reqnum = cb->args[4];
642
643 if (s_j > 0)
644 s_j--;
645
646 entry.family = sk->sk_family;
647
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700648 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700650 lopt = icsk->icsk_accept_queue.listen_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (!lopt || !lopt->qlen)
652 goto out;
653
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000654 if (bc != NULL) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000655 entry.sport = inet->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 entry.userlocks = sk->sk_userlocks;
657 }
658
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300659 for (j = s_j; j < lopt->nr_table_entries; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700660 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
662 reqnum = 0;
663 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700664 struct inet_request_sock *ireq = inet_rsk(req);
665
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 if (reqnum < s_reqnum)
667 continue;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300668 if (r->id.idiag_dport != ireq->rmt_port &&
669 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 continue;
671
672 if (bc) {
673 entry.saddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300674#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800676 inet6_rsk(req)->loc_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700678 &ireq->loc_addr;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800679 entry.daddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300680#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800682 inet6_rsk(req)->rmt_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700684 &ireq->rmt_addr;
685 entry.dport = ntohs(ireq->rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Nelson Elhage22e76c82010-11-03 16:35:41 +0000687 if (!inet_diag_bc_run(nla_data(bc),
688 nla_len(bc), &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 continue;
690 }
691
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300692 err = inet_diag_fill_req(skb, sk, req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 NETLINK_CB(cb->skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300694 cb->nlh->nlmsg_seq, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (err < 0) {
696 cb->args[3] = j + 1;
697 cb->args[4] = reqnum;
698 goto out;
699 }
700 }
701
702 s_reqnum = 0;
703 }
704
705out:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700706 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707
708 return err;
709}
710
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300711static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 int i, num;
714 int s_i, s_num;
Pavel Emelyanov126fdc32011-12-06 07:58:21 +0000715 struct inet_diag_req_compat *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300716 const struct inet_diag_handler *handler;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300717 struct inet_hashinfo *hashinfo;
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000718 const struct nlattr *bc = NULL;
719
Pavel Emelyanov126fdc32011-12-06 07:58:21 +0000720 if (nlmsg_attrlen(cb->nlh, sizeof(struct inet_diag_req_compat)))
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000721 bc = nlmsg_find_attr(cb->nlh, sizeof(*r), INET_DIAG_REQ_BYTECODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +0000723 handler = inet_diag_lock_handler(inet_diag_type2proto(cb->nlh->nlmsg_type));
Arnaldo Carvalho de Melo8cf8e5a2008-01-28 20:52:12 -0800724 if (IS_ERR(handler))
725 goto unlock;
Herbert Xud523a322007-12-03 15:51:25 +1100726
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300727 hashinfo = handler->idiag_hashinfo;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 s_i = cb->args[1];
730 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300731
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (cb->args[0] == 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300733 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300735
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700736 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct sock *sk;
Eric Dumazetc25eb3b2008-11-23 17:22:55 -0800738 struct hlist_nulls_node *node;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800739 struct inet_listen_hashbucket *ilb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800742 ilb = &hashinfo->listening_hash[i];
743 spin_lock_bh(&ilb->lock);
Eric Dumazetc25eb3b2008-11-23 17:22:55 -0800744 sk_nulls_for_each(sk, node, &ilb->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct inet_sock *inet = inet_sk(sk);
746
747 if (num < s_num) {
748 num++;
749 continue;
750 }
751
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000752 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300753 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 goto next_listen;
755
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300756 if (!(r->idiag_states & TCPF_LISTEN) ||
757 r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 cb->args[3] > 0)
759 goto syn_recv;
760
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000761 if (inet_csk_diag_dump(sk, skb, cb, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800762 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto done;
764 }
765
766syn_recv:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300767 if (!(r->idiag_states & TCPF_SYN_RECV))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 goto next_listen;
769
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000770 if (inet_diag_dump_reqs(skb, sk, cb, bc) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800771 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 goto done;
773 }
774
775next_listen:
776 cb->args[3] = 0;
777 cb->args[4] = 0;
778 ++num;
779 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800780 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 s_num = 0;
783 cb->args[3] = 0;
784 cb->args[4] = 0;
785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786skip_listen_ht:
787 cb->args[0] = 1;
788 s_i = num = s_num = 0;
789 }
790
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300791 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
Herbert Xud523a322007-12-03 15:51:25 +1100792 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Eric Dumazetf373b532009-10-09 00:16:19 +0000794 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300795 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800796 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 struct sock *sk;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800798 struct hlist_nulls_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
Andi Kleen6be547a2008-08-28 01:09:54 -0700800 num = 0;
801
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800802 if (hlist_nulls_empty(&head->chain) &&
803 hlist_nulls_empty(&head->twchain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700804 continue;
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 if (i > s_i)
807 s_num = 0;
808
David S. Miller7e3aab42008-11-21 16:39:19 -0800809 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800810 sk_nulls_for_each(sk, node, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct inet_sock *inet = inet_sk(sk);
812
813 if (num < s_num)
814 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300815 if (!(r->idiag_states & (1 << sk->sk_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 goto next_normal;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000817 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300818 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 goto next_normal;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000820 if (r->id.idiag_dport != inet->inet_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800821 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 goto next_normal;
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000823 if (inet_csk_diag_dump(sk, skb, cb, bc) < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800824 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 goto done;
826 }
827next_normal:
828 ++num;
829 }
830
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300831 if (r->idiag_states & TCPF_TIME_WAIT) {
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800832 struct inet_timewait_sock *tw;
833
834 inet_twsk_for_each(tw, node,
Eric Dumazetdbca9b2752007-02-08 14:16:46 -0800835 &head->twchain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 if (num < s_num)
838 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800839 if (r->id.idiag_sport != tw->tw_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300840 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800842 if (r->id.idiag_dport != tw->tw_dport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300843 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto next_dying;
Pavel Emelyanov37f352b2011-12-06 07:57:26 +0000845 if (inet_twsk_diag_dump(tw, skb, cb, bc) < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800846 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto done;
848 }
849next_dying:
850 ++num;
851 }
852 }
David S. Miller7e3aab42008-11-21 16:39:19 -0800853 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 }
855
856done:
857 cb->args[1] = i;
858 cb->args[2] = num;
Herbert Xud523a322007-12-03 15:51:25 +1100859unlock:
860 inet_diag_unlock_handler(handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return skb->len;
862}
863
Pavel Emelyanov8d341722011-12-06 07:57:06 +0000864static int inet_diag_rcv_msg_compat(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Pavel Emelyanov126fdc32011-12-06 07:58:21 +0000866 int hdrlen = sizeof(struct inet_diag_req_compat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Thomas Grafead592b2007-03-22 23:30:35 -0700868 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
869 nlmsg_len(nlh) < hdrlen)
870 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
David S. Millerb8f3ab42011-01-18 12:40:38 -0800872 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -0700873 if (nlmsg_attrlen(nlh, hdrlen)) {
874 struct nlattr *attr;
875
876 attr = nlmsg_find_attr(nlh, hdrlen,
877 INET_DIAG_REQ_BYTECODE);
878 if (attr == NULL ||
879 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
880 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
881 return -EINVAL;
882 }
883
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +0000884 return netlink_dump_start(sdiagnl, skb, nlh,
Greg Rosec7ac8672011-06-10 01:27:09 +0000885 inet_diag_dump, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 }
Thomas Grafead592b2007-03-22 23:30:35 -0700887
888 return inet_diag_get_exact(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
Pavel Emelyanovd3664772011-12-06 07:58:03 +0000891static int inet_diag_handler_dump(struct sk_buff *skb, struct nlmsghdr *h)
892{
893 int hdrlen = sizeof(struct inet_diag_req);
894
895 if (nlmsg_len(h) < hdrlen)
896 return -EINVAL;
897
898 if (h->nlmsg_flags & NLM_F_DUMP) {
899 return -EAFNOSUPPORT;
900 }
901
902 return -EAFNOSUPPORT;
903}
904
905static struct sock_diag_handler inet_diag_handler = {
906 .family = AF_INET,
907 .dump = inet_diag_handler_dump,
908};
909
910static struct sock_diag_handler inet6_diag_handler = {
911 .family = AF_INET6,
912 .dump = inet_diag_handler_dump,
913};
914
915static struct sock_diag_handler *sock_diag_handlers[AF_MAX];
916static DEFINE_MUTEX(sock_diag_table_mutex);
917
918int sock_diag_register(struct sock_diag_handler *hndl)
919{
920 int err = 0;
921
922 if (hndl->family > AF_MAX)
923 return -EINVAL;
924
925 mutex_lock(&sock_diag_table_mutex);
926 if (sock_diag_handlers[hndl->family])
927 err = -EBUSY;
928 else
929 sock_diag_handlers[hndl->family] = hndl;
930 mutex_unlock(&sock_diag_table_mutex);
931
932 return err;
933}
934
935void sock_diag_unregister(struct sock_diag_handler *hnld)
936{
937 int family = hnld->family;
938
939 if (family > AF_MAX)
940 return;
941
942 mutex_lock(&sock_diag_table_mutex);
943 BUG_ON(sock_diag_handlers[family] != hnld);
944 sock_diag_handlers[family] = NULL;
945 mutex_unlock(&sock_diag_table_mutex);
946}
947
948static inline struct sock_diag_handler *sock_diag_lock_handler(int family)
949{
950 mutex_lock(&sock_diag_table_mutex);
951 return sock_diag_handlers[family];
952}
953
954static inline void sock_diag_unlock_handler(struct sock_diag_handler *h)
955{
956 mutex_unlock(&sock_diag_table_mutex);
957}
958
Pavel Emelyanov8d341722011-12-06 07:57:06 +0000959static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
960{
Pavel Emelyanovd3664772011-12-06 07:58:03 +0000961 int err;
962 struct sock_diag_req *req = NLMSG_DATA(nlh);
963 struct sock_diag_handler *hndl;
964
965 if (nlmsg_len(nlh) < sizeof(*req))
966 return -EINVAL;
967
968 hndl = sock_diag_lock_handler(req->sdiag_family);
969 if (hndl == NULL)
970 err = -ENOENT;
971 else
972 err = hndl->dump(skb, nlh);
973 sock_diag_unlock_handler(hndl);
974
975 return err;
Pavel Emelyanov8d341722011-12-06 07:57:06 +0000976}
977
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +0000978static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Pavel Emelyanov8d341722011-12-06 07:57:06 +0000980 switch (nlh->nlmsg_type) {
981 case TCPDIAG_GETSOCK:
982 case DCCPDIAG_GETSOCK:
983 return inet_diag_rcv_msg_compat(skb, nlh);
984 case SOCK_DIAG_BY_FAMILY:
985 return __sock_diag_rcv_msg(skb, nlh);
986 default:
987 return -EINVAL;
988 }
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +0000989}
990
991static DEFINE_MUTEX(sock_diag_mutex);
992
993static void sock_diag_rcv(struct sk_buff *skb)
994{
995 mutex_lock(&sock_diag_mutex);
996 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
997 mutex_unlock(&sock_diag_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998}
999
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001000int inet_diag_register(const struct inet_diag_handler *h)
1001{
1002 const __u16 type = h->idiag_type;
1003 int err = -EINVAL;
1004
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001005 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001006 goto out;
1007
Herbert Xud523a322007-12-03 15:51:25 +11001008 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001009 err = -EEXIST;
1010 if (inet_diag_table[type] == NULL) {
1011 inet_diag_table[type] = h;
1012 err = 0;
1013 }
Herbert Xud523a322007-12-03 15:51:25 +11001014 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001015out:
1016 return err;
1017}
1018EXPORT_SYMBOL_GPL(inet_diag_register);
1019
1020void inet_diag_unregister(const struct inet_diag_handler *h)
1021{
1022 const __u16 type = h->idiag_type;
1023
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001024 if (type >= IPPROTO_MAX)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001025 return;
1026
Herbert Xud523a322007-12-03 15:51:25 +11001027 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001028 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +11001029 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001030}
1031EXPORT_SYMBOL_GPL(inet_diag_unregister);
1032
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001033static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034{
Pavel Emelyanovf13c95f2011-12-06 08:05:24 +00001035 const int inet_diag_table_size = (IPPROTO_MAX *
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001036 sizeof(struct inet_diag_handler *));
1037 int err = -ENOMEM;
1038
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001039 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001040 if (!inet_diag_table)
1041 goto out;
1042
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001043 sdiagnl = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0,
1044 sock_diag_rcv, NULL, THIS_MODULE);
1045 if (sdiagnl == NULL)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001046 goto out_free_table;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001047
1048 err = sock_diag_register(&inet_diag_handler);
1049 if (err)
1050 goto out_free_nl;
1051
1052 err = sock_diag_register(&inet6_diag_handler);
1053 if (err)
1054 goto out_free_inet;
1055
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001056out:
1057 return err;
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001058
1059out_free_inet:
1060 sock_diag_unregister(&inet_diag_handler);
1061out_free_nl:
1062 netlink_kernel_release(sdiagnl);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001063out_free_table:
1064 kfree(inet_diag_table);
1065 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001068static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069{
Pavel Emelyanovd3664772011-12-06 07:58:03 +00001070 sock_diag_unregister(&inet6_diag_handler);
1071 sock_diag_unregister(&inet_diag_handler);
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001072 netlink_kernel_release(sdiagnl);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -03001073 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074}
1075
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03001076module_init(inet_diag_init);
1077module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078MODULE_LICENSE("GPL");
Pavel Emelyanov7f1fb602011-12-06 07:56:43 +00001079MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);