blob: f5e2bdaef9495eb52524089f6d9b341cd5503137 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030037static const struct inet_diag_handler **inet_diag_table;
38
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030039struct inet_diag_entry {
Al Viro9f855292006-09-27 18:44:30 -070040 __be32 *saddr;
41 __be32 *daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 u16 sport;
43 u16 dport;
44 u16 family;
45 u16 userlocks;
46};
47
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030048static struct sock *idiagnl;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030050#define INET_DIAG_PUT(skb, attrtype, attrlen) \
Stephen Hemminger7c99c902005-06-23 12:20:36 -070051 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Herbert Xud523a322007-12-03 15:51:25 +110053static DEFINE_MUTEX(inet_diag_table_mutex);
54
55static const struct inet_diag_handler *inet_diag_lock_handler(int type)
56{
Herbert Xud523a322007-12-03 15:51:25 +110057 if (!inet_diag_table[type])
58 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
59 NETLINK_INET_DIAG, type);
Herbert Xud523a322007-12-03 15:51:25 +110060
61 mutex_lock(&inet_diag_table_mutex);
62 if (!inet_diag_table[type])
63 return ERR_PTR(-ENOENT);
64
65 return inet_diag_table[type];
66}
67
68static inline void inet_diag_unlock_handler(
69 const struct inet_diag_handler *handler)
70{
71 mutex_unlock(&inet_diag_table_mutex);
72}
73
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -080074static int inet_csk_diag_fill(struct sock *sk,
75 struct sk_buff *skb,
76 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
77 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070079 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070080 const struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030081 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030083 void *info = NULL;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030084 struct inet_diag_meminfo *minfo = NULL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070085 unsigned char *b = skb_tail_pointer(skb);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030086 const struct inet_diag_handler *handler;
87
88 handler = inet_diag_table[unlh->nlmsg_type];
89 BUG_ON(handler == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -030091 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -070092 nlh->nlmsg_flags = nlmsg_flags;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 r = NLMSG_DATA(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -080095 BUG_ON(sk->sk_state == TCP_TIME_WAIT);
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -080096
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -080097 if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
98 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo));
99
100 if (ext & (1 << (INET_DIAG_INFO - 1)))
101 info = INET_DIAG_PUT(skb, INET_DIAG_INFO,
102 handler->idiag_info_size);
103
104 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
105 const size_t len = strlen(icsk->icsk_ca_ops->name);
106
107 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
108 icsk->icsk_ca_ops->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 }
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800110
Murali Raja3ceca742011-10-12 09:00:35 +0000111 if ((ext & (1 << (INET_DIAG_TOS - 1))) && (sk->sk_family != AF_INET6))
112 RTA_PUT_U8(skb, INET_DIAG_TOS, inet->tos);
113
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300114 r->idiag_family = sk->sk_family;
115 r->idiag_state = sk->sk_state;
116 r->idiag_timer = 0;
117 r->idiag_retrans = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300119 r->id.idiag_if = sk->sk_bound_dev_if;
120 r->id.idiag_cookie[0] = (u32)(unsigned long)sk;
121 r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000123 r->id.idiag_sport = inet->inet_sport;
124 r->id.idiag_dport = inet->inet_dport;
125 r->id.idiag_src[0] = inet->inet_rcv_saddr;
126 r->id.idiag_dst[0] = inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300128#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300129 if (r->idiag_family == AF_INET6) {
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000130 const struct ipv6_pinfo *np = inet6_sk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300132 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 &np->rcv_saddr);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300134 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 &np->daddr);
Murali Raja3ceca742011-10-12 09:00:35 +0000136 if (ext & (1 << (INET_DIAG_TOS - 1)))
137 RTA_PUT_U8(skb, INET_DIAG_TOS, np->tclass);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 }
139#endif
140
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700141#define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700143 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300144 r->idiag_timer = 1;
145 r->idiag_retrans = icsk->icsk_retransmits;
146 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700147 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300148 r->idiag_timer = 4;
149 r->idiag_retrans = icsk->icsk_probes_out;
150 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300152 r->idiag_timer = 2;
153 r->idiag_retrans = icsk->icsk_probes_out;
154 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300156 r->idiag_timer = 0;
157 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 }
159#undef EXPIRES_IN_MS
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300160
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300161 r->idiag_uid = sock_i_uid(sk);
162 r->idiag_inode = sock_i_ino(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
164 if (minfo) {
Eric Dumazet31e6d362009-06-17 19:05:41 -0700165 minfo->idiag_rmem = sk_rmem_alloc_get(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300166 minfo->idiag_wmem = sk->sk_wmem_queued;
167 minfo->idiag_fmem = sk->sk_forward_alloc;
Eric Dumazet31e6d362009-06-17 19:05:41 -0700168 minfo->idiag_tmem = sk_wmem_alloc_get(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 }
170
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300171 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300173 if (sk->sk_state < TCP_TIME_WAIT &&
174 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
175 icsk->icsk_ca_ops->get_info(sk, ext, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700177 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 return skb->len;
179
Stephen Hemminger7c99c902005-06-23 12:20:36 -0700180rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700182 nlmsg_trim(skb, b);
Patrick McHardy26932562007-01-31 23:16:40 -0800183 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800186static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
187 struct sk_buff *skb, int ext, u32 pid,
188 u32 seq, u16 nlmsg_flags,
189 const struct nlmsghdr *unlh)
190{
191 long tmo;
192 struct inet_diag_msg *r;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700193 const unsigned char *previous_tail = skb_tail_pointer(skb);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800194 struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq,
195 unlh->nlmsg_type, sizeof(*r));
196
197 r = NLMSG_DATA(nlh);
198 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
199
200 nlh->nlmsg_flags = nlmsg_flags;
201
202 tmo = tw->tw_ttd - jiffies;
203 if (tmo < 0)
204 tmo = 0;
205
206 r->idiag_family = tw->tw_family;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800207 r->idiag_retrans = 0;
208 r->id.idiag_if = tw->tw_bound_dev_if;
209 r->id.idiag_cookie[0] = (u32)(unsigned long)tw;
210 r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1);
211 r->id.idiag_sport = tw->tw_sport;
212 r->id.idiag_dport = tw->tw_dport;
213 r->id.idiag_src[0] = tw->tw_rcv_saddr;
214 r->id.idiag_dst[0] = tw->tw_daddr;
215 r->idiag_state = tw->tw_substate;
216 r->idiag_timer = 3;
Ilpo Järvinen172589c2007-08-28 15:50:33 -0700217 r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800218 r->idiag_rqueue = 0;
219 r->idiag_wqueue = 0;
220 r->idiag_uid = 0;
221 r->idiag_inode = 0;
222#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
223 if (tw->tw_family == AF_INET6) {
224 const struct inet6_timewait_sock *tw6 =
225 inet6_twsk((struct sock *)tw);
226
227 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
228 &tw6->tw_v6_rcv_saddr);
229 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
230 &tw6->tw_v6_daddr);
231 }
232#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700233 nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800234 return skb->len;
235nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700236 nlmsg_trim(skb, previous_tail);
Patrick McHardy26932562007-01-31 23:16:40 -0800237 return -EMSGSIZE;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800238}
239
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800240static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
241 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
242 const struct nlmsghdr *unlh)
243{
244 if (sk->sk_state == TCP_TIME_WAIT)
245 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
246 skb, ext, pid, seq, nlmsg_flags,
247 unlh);
248 return inet_csk_diag_fill(sk, skb, ext, pid, seq, nlmsg_flags, unlh);
249}
250
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800251static int inet_diag_get_exact(struct sk_buff *in_skb,
252 const struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253{
254 int err;
255 struct sock *sk;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300256 struct inet_diag_req *req = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 struct sk_buff *rep;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300258 struct inet_hashinfo *hashinfo;
259 const struct inet_diag_handler *handler;
260
Herbert Xud523a322007-12-03 15:51:25 +1100261 handler = inet_diag_lock_handler(nlh->nlmsg_type);
Arnaldo Carvalho de Melo8cf8e5a2008-01-28 20:52:12 -0800262 if (IS_ERR(handler)) {
263 err = PTR_ERR(handler);
264 goto unlock;
265 }
Herbert Xud523a322007-12-03 15:51:25 +1100266
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300267 hashinfo = handler->idiag_hashinfo;
Herbert Xud523a322007-12-03 15:51:25 +1100268 err = -EINVAL;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300269
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300270 if (req->idiag_family == AF_INET) {
Pavel Emelyanovc67499c2008-01-31 05:06:40 -0800271 sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0],
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300272 req->id.idiag_dport, req->id.idiag_src[0],
273 req->id.idiag_sport, req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300275#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300276 else if (req->idiag_family == AF_INET6) {
Pavel Emelyanovd86e0da2008-01-31 05:07:21 -0800277 sk = inet6_lookup(&init_net, hashinfo,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300278 (struct in6_addr *)req->id.idiag_dst,
279 req->id.idiag_dport,
280 (struct in6_addr *)req->id.idiag_src,
281 req->id.idiag_sport,
282 req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284#endif
285 else {
Herbert Xud523a322007-12-03 15:51:25 +1100286 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
288
Herbert Xud523a322007-12-03 15:51:25 +1100289 err = -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 if (sk == NULL)
Herbert Xud523a322007-12-03 15:51:25 +1100291 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292
293 err = -ESTALE;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300294 if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
295 req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
296 ((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
297 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 goto out;
299
300 err = -ENOMEM;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300301 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
302 sizeof(struct inet_diag_meminfo) +
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300303 handler->idiag_info_size + 64)),
304 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 if (!rep)
306 goto out;
307
Patrick McHardy26932562007-01-31 23:16:40 -0800308 err = sk_diag_fill(sk, rep, req->idiag_ext,
309 NETLINK_CB(in_skb).pid,
310 nlh->nlmsg_seq, 0, nlh);
311 if (err < 0) {
312 WARN_ON(err == -EMSGSIZE);
313 kfree_skb(rep);
314 goto out;
315 }
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300316 err = netlink_unicast(idiagnl, rep, NETLINK_CB(in_skb).pid,
317 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 if (err > 0)
319 err = 0;
320
321out:
322 if (sk) {
323 if (sk->sk_state == TCP_TIME_WAIT)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700324 inet_twsk_put((struct inet_timewait_sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 else
326 sock_put(sk);
327 }
Herbert Xud523a322007-12-03 15:51:25 +1100328unlock:
329 inet_diag_unlock_handler(handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 return err;
331}
332
Al Viro9f855292006-09-27 18:44:30 -0700333static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334{
335 int words = bits >> 5;
336
337 bits &= 0x1f;
338
339 if (words) {
340 if (memcmp(a1, a2, words << 2))
341 return 0;
342 }
343 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700344 __be32 w1, w2;
345 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 w1 = a1[words];
348 w2 = a2[words];
349
350 mask = htonl((0xffffffff) << (32 - bits));
351
352 if ((w1 ^ w2) & mask)
353 return 0;
354 }
355
356 return 1;
357}
358
359
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300360static int inet_diag_bc_run(const void *bc, int len,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800361 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 while (len > 0) {
364 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300365 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300368 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300370 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 yes = 0;
372 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300373 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 yes = entry->sport >= op[1].no;
375 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300376 case INET_DIAG_BC_S_LE:
Roel Kluinb4ced2b2010-01-19 14:12:20 -0800377 yes = entry->sport <= op[1].no;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300379 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 yes = entry->dport >= op[1].no;
381 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300382 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 yes = entry->dport <= op[1].no;
384 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300385 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
387 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300388 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300389 case INET_DIAG_BC_D_COND: {
390 struct inet_diag_hostcond *cond;
Al Viro9f855292006-09-27 18:44:30 -0700391 __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300393 cond = (struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300395 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 entry->sport : entry->dport)) {
397 yes = 0;
398 break;
399 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 if (cond->prefix_len == 0)
402 break;
403
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300404 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 addr = entry->saddr;
406 else
407 addr = entry->daddr;
408
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800409 if (bitstring_match(addr, cond->addr,
410 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 break;
412 if (entry->family == AF_INET6 &&
413 cond->family == AF_INET) {
414 if (addr[0] == 0 && addr[1] == 0 &&
415 addr[2] == htonl(0xffff) &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300416 bitstring_match(addr + 3, cond->addr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900417 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 break;
419 }
420 yes = 0;
421 break;
422 }
423 }
424
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800425 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 len -= op->yes;
427 bc += op->yes;
428 } else {
429 len -= op->no;
430 bc += op->no;
431 }
432 }
Eric Dumazeta02cec22010-09-22 20:43:57 +0000433 return len == 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434}
435
436static int valid_cc(const void *bc, int len, int cc)
437{
438 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300439 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441 if (cc > len)
442 return 0;
443 if (cc == len)
444 return 1;
Eric Dumazeteeb14972011-06-17 16:25:39 -0400445 if (op->yes < 4 || op->yes & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return 0;
447 len -= op->yes;
448 bc += op->yes;
449 }
450 return 0;
451}
452
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300453static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Eric Dumazeteeb14972011-06-17 16:25:39 -0400455 const void *bc = bytecode;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 int len = bytecode_len;
457
458 while (len > 0) {
Eric Dumazeteeb14972011-06-17 16:25:39 -0400459 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
461//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
462 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300463 case INET_DIAG_BC_AUTO:
464 case INET_DIAG_BC_S_COND:
465 case INET_DIAG_BC_D_COND:
466 case INET_DIAG_BC_S_GE:
467 case INET_DIAG_BC_S_LE:
468 case INET_DIAG_BC_D_GE:
469 case INET_DIAG_BC_D_LE:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300470 case INET_DIAG_BC_JMP:
Eric Dumazeteeb14972011-06-17 16:25:39 -0400471 if (op->no < 4 || op->no > len + 4 || op->no & 3)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 return -EINVAL;
473 if (op->no < len &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300474 !valid_cc(bytecode, bytecode_len, len - op->no))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 return -EINVAL;
476 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300477 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 break;
479 default:
480 return -EINVAL;
481 }
Eric Dumazeteeb14972011-06-17 16:25:39 -0400482 if (op->yes < 4 || op->yes > len + 4 || op->yes & 3)
483 return -EINVAL;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800484 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 len -= op->yes;
486 }
487 return len == 0 ? 0 : -EINVAL;
488}
489
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800490static int inet_csk_diag_dump(struct sock *sk,
491 struct sk_buff *skb,
492 struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300494 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495
Nelson Elhage22e76c82010-11-03 16:35:41 +0000496 if (nlmsg_attrlen(cb->nlh, sizeof(*r))) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300497 struct inet_diag_entry entry;
Nelson Elhage22e76c82010-11-03 16:35:41 +0000498 const struct nlattr *bc = nlmsg_find_attr(cb->nlh,
499 sizeof(*r),
500 INET_DIAG_REQ_BYTECODE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 struct inet_sock *inet = inet_sk(sk);
502
503 entry.family = sk->sk_family;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300504#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (entry.family == AF_INET6) {
506 struct ipv6_pinfo *np = inet6_sk(sk);
507
508 entry.saddr = np->rcv_saddr.s6_addr32;
509 entry.daddr = np->daddr.s6_addr32;
510 } else
511#endif
512 {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000513 entry.saddr = &inet->inet_rcv_saddr;
514 entry.daddr = &inet->inet_daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000516 entry.sport = inet->inet_num;
517 entry.dport = ntohs(inet->inet_dport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 entry.userlocks = sk->sk_userlocks;
519
Nelson Elhage22e76c82010-11-03 16:35:41 +0000520 if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 return 0;
522 }
523
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800524 return inet_csk_diag_fill(sk, skb, r->idiag_ext,
525 NETLINK_CB(cb->skb).pid,
526 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800529static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
530 struct sk_buff *skb,
531 struct netlink_callback *cb)
532{
533 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
534
Nelson Elhage22e76c82010-11-03 16:35:41 +0000535 if (nlmsg_attrlen(cb->nlh, sizeof(*r))) {
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800536 struct inet_diag_entry entry;
Nelson Elhage22e76c82010-11-03 16:35:41 +0000537 const struct nlattr *bc = nlmsg_find_attr(cb->nlh,
538 sizeof(*r),
539 INET_DIAG_REQ_BYTECODE);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800540
541 entry.family = tw->tw_family;
542#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
543 if (tw->tw_family == AF_INET6) {
544 struct inet6_timewait_sock *tw6 =
545 inet6_twsk((struct sock *)tw);
546 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
547 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
548 } else
549#endif
550 {
551 entry.saddr = &tw->tw_rcv_saddr;
552 entry.daddr = &tw->tw_daddr;
553 }
554 entry.sport = tw->tw_num;
555 entry.dport = ntohs(tw->tw_dport);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900556 entry.userlocks = 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800557
Nelson Elhage22e76c82010-11-03 16:35:41 +0000558 if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry))
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800559 return 0;
560 }
561
562 return inet_twsk_diag_fill(tw, skb, r->idiag_ext,
563 NETLINK_CB(cb->skb).pid,
564 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
565}
566
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300567static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800568 struct request_sock *req, u32 pid, u32 seq,
569 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700571 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700573 unsigned char *b = skb_tail_pointer(skb);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300574 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct nlmsghdr *nlh;
576 long tmo;
577
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300578 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 nlh->nlmsg_flags = NLM_F_MULTI;
580 r = NLMSG_DATA(nlh);
581
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300582 r->idiag_family = sk->sk_family;
583 r->idiag_state = TCP_SYN_RECV;
584 r->idiag_timer = 1;
585 r->idiag_retrans = req->retrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300587 r->id.idiag_if = sk->sk_bound_dev_if;
588 r->id.idiag_cookie[0] = (u32)(unsigned long)req;
589 r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
591 tmo = req->expires - jiffies;
592 if (tmo < 0)
593 tmo = 0;
594
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000595 r->id.idiag_sport = inet->inet_sport;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300596 r->id.idiag_dport = ireq->rmt_port;
597 r->id.idiag_src[0] = ireq->loc_addr;
598 r->id.idiag_dst[0] = ireq->rmt_addr;
599 r->idiag_expires = jiffies_to_msecs(tmo);
600 r->idiag_rqueue = 0;
601 r->idiag_wqueue = 0;
602 r->idiag_uid = sock_i_uid(sk);
603 r->idiag_inode = 0;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300604#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300605 if (r->idiag_family == AF_INET6) {
606 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800607 &inet6_rsk(req)->loc_addr);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300608 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800609 &inet6_rsk(req)->rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 }
611#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700612 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 return skb->len;
615
616nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700617 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return -1;
619}
620
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300621static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800622 struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300624 struct inet_diag_entry entry;
625 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700626 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo2ad69c52005-06-18 22:48:55 -0700627 struct listen_sock *lopt;
Nelson Elhage22e76c82010-11-03 16:35:41 +0000628 const struct nlattr *bc = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 struct inet_sock *inet = inet_sk(sk);
630 int j, s_j;
631 int reqnum, s_reqnum;
632 int err = 0;
633
634 s_j = cb->args[3];
635 s_reqnum = cb->args[4];
636
637 if (s_j > 0)
638 s_j--;
639
640 entry.family = sk->sk_family;
641
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700642 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700644 lopt = icsk->icsk_accept_queue.listen_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 if (!lopt || !lopt->qlen)
646 goto out;
647
Nelson Elhage22e76c82010-11-03 16:35:41 +0000648 if (nlmsg_attrlen(cb->nlh, sizeof(*r))) {
649 bc = nlmsg_find_attr(cb->nlh, sizeof(*r),
650 INET_DIAG_REQ_BYTECODE);
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000651 entry.sport = inet->inet_num;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 entry.userlocks = sk->sk_userlocks;
653 }
654
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300655 for (j = s_j; j < lopt->nr_table_entries; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700656 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 reqnum = 0;
659 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700660 struct inet_request_sock *ireq = inet_rsk(req);
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 if (reqnum < s_reqnum)
663 continue;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300664 if (r->id.idiag_dport != ireq->rmt_port &&
665 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 continue;
667
668 if (bc) {
669 entry.saddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300670#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800672 inet6_rsk(req)->loc_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700674 &ireq->loc_addr;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800675 entry.daddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300676#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800678 inet6_rsk(req)->rmt_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700680 &ireq->rmt_addr;
681 entry.dport = ntohs(ireq->rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
Nelson Elhage22e76c82010-11-03 16:35:41 +0000683 if (!inet_diag_bc_run(nla_data(bc),
684 nla_len(bc), &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 continue;
686 }
687
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300688 err = inet_diag_fill_req(skb, sk, req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 NETLINK_CB(cb->skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300690 cb->nlh->nlmsg_seq, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (err < 0) {
692 cb->args[3] = j + 1;
693 cb->args[4] = reqnum;
694 goto out;
695 }
696 }
697
698 s_reqnum = 0;
699 }
700
701out:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700702 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 return err;
705}
706
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300707static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 int i, num;
710 int s_i, s_num;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300711 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300712 const struct inet_diag_handler *handler;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300713 struct inet_hashinfo *hashinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Herbert Xud523a322007-12-03 15:51:25 +1100715 handler = inet_diag_lock_handler(cb->nlh->nlmsg_type);
Arnaldo Carvalho de Melo8cf8e5a2008-01-28 20:52:12 -0800716 if (IS_ERR(handler))
717 goto unlock;
Herbert Xud523a322007-12-03 15:51:25 +1100718
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300719 hashinfo = handler->idiag_hashinfo;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 s_i = cb->args[1];
722 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300723
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (cb->args[0] == 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300725 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300727
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700728 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 struct sock *sk;
Eric Dumazetc25eb3b2008-11-23 17:22:55 -0800730 struct hlist_nulls_node *node;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800731 struct inet_listen_hashbucket *ilb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 num = 0;
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800734 ilb = &hashinfo->listening_hash[i];
735 spin_lock_bh(&ilb->lock);
Eric Dumazetc25eb3b2008-11-23 17:22:55 -0800736 sk_nulls_for_each(sk, node, &ilb->head) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 struct inet_sock *inet = inet_sk(sk);
738
739 if (num < s_num) {
740 num++;
741 continue;
742 }
743
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000744 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300745 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 goto next_listen;
747
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300748 if (!(r->idiag_states & TCPF_LISTEN) ||
749 r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 cb->args[3] > 0)
751 goto syn_recv;
752
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800753 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800754 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 goto done;
756 }
757
758syn_recv:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300759 if (!(r->idiag_states & TCPF_SYN_RECV))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto next_listen;
761
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300762 if (inet_diag_dump_reqs(skb, sk, cb) < 0) {
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800763 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 goto done;
765 }
766
767next_listen:
768 cb->args[3] = 0;
769 cb->args[4] = 0;
770 ++num;
771 }
Eric Dumazet5caea4e2008-11-20 00:40:07 -0800772 spin_unlock_bh(&ilb->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
774 s_num = 0;
775 cb->args[3] = 0;
776 cb->args[4] = 0;
777 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778skip_listen_ht:
779 cb->args[0] = 1;
780 s_i = num = s_num = 0;
781 }
782
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300783 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
Herbert Xud523a322007-12-03 15:51:25 +1100784 goto unlock;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Eric Dumazetf373b532009-10-09 00:16:19 +0000786 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300787 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
David S. Miller7e3aab42008-11-21 16:39:19 -0800788 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 struct sock *sk;
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800790 struct hlist_nulls_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Andi Kleen6be547a2008-08-28 01:09:54 -0700792 num = 0;
793
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800794 if (hlist_nulls_empty(&head->chain) &&
795 hlist_nulls_empty(&head->twchain))
Andi Kleen6be547a2008-08-28 01:09:54 -0700796 continue;
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 if (i > s_i)
799 s_num = 0;
800
David S. Miller7e3aab42008-11-21 16:39:19 -0800801 spin_lock_bh(lock);
Eric Dumazet3ab5aee2008-11-16 19:40:17 -0800802 sk_nulls_for_each(sk, node, &head->chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 struct inet_sock *inet = inet_sk(sk);
804
805 if (num < s_num)
806 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300807 if (!(r->idiag_states & (1 << sk->sk_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 goto next_normal;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000809 if (r->id.idiag_sport != inet->inet_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300810 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto next_normal;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000812 if (r->id.idiag_dport != inet->inet_dport &&
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800813 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 goto next_normal;
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800815 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800816 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 goto done;
818 }
819next_normal:
820 ++num;
821 }
822
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300823 if (r->idiag_states & TCPF_TIME_WAIT) {
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800824 struct inet_timewait_sock *tw;
825
826 inet_twsk_for_each(tw, node,
Eric Dumazetdbca9b2752007-02-08 14:16:46 -0800827 &head->twchain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 if (num < s_num)
830 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800831 if (r->id.idiag_sport != tw->tw_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300832 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800834 if (r->id.idiag_dport != tw->tw_dport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300835 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 goto next_dying;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800837 if (inet_twsk_diag_dump(tw, skb, cb) < 0) {
David S. Miller7e3aab42008-11-21 16:39:19 -0800838 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 goto done;
840 }
841next_dying:
842 ++num;
843 }
844 }
David S. Miller7e3aab42008-11-21 16:39:19 -0800845 spin_unlock_bh(lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 }
847
848done:
849 cb->args[1] = i;
850 cb->args[2] = num;
Herbert Xud523a322007-12-03 15:51:25 +1100851unlock:
852 inet_diag_unlock_handler(handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return skb->len;
854}
855
Thomas Grafead592b2007-03-22 23:30:35 -0700856static int inet_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Thomas Grafead592b2007-03-22 23:30:35 -0700858 int hdrlen = sizeof(struct inet_diag_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Thomas Grafead592b2007-03-22 23:30:35 -0700860 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
861 nlmsg_len(nlh) < hdrlen)
862 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
David S. Millerb8f3ab42011-01-18 12:40:38 -0800864 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -0700865 if (nlmsg_attrlen(nlh, hdrlen)) {
866 struct nlattr *attr;
867
868 attr = nlmsg_find_attr(nlh, hdrlen,
869 INET_DIAG_REQ_BYTECODE);
870 if (attr == NULL ||
871 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
872 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
873 return -EINVAL;
874 }
875
Thomas Grafc702e802007-03-22 23:30:55 -0700876 return netlink_dump_start(idiagnl, skb, nlh,
Greg Rosec7ac8672011-06-10 01:27:09 +0000877 inet_diag_dump, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 }
Thomas Grafead592b2007-03-22 23:30:35 -0700879
880 return inet_diag_get_exact(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881}
882
Patrick McHardy0a9c7302007-09-11 11:33:28 +0200883static DEFINE_MUTEX(inet_diag_mutex);
884
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700885static void inet_diag_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700887 mutex_lock(&inet_diag_mutex);
888 netlink_rcv_skb(skb, &inet_diag_rcv_msg);
889 mutex_unlock(&inet_diag_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300892int inet_diag_register(const struct inet_diag_handler *h)
893{
894 const __u16 type = h->idiag_type;
895 int err = -EINVAL;
896
897 if (type >= INET_DIAG_GETSOCK_MAX)
898 goto out;
899
Herbert Xud523a322007-12-03 15:51:25 +1100900 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300901 err = -EEXIST;
902 if (inet_diag_table[type] == NULL) {
903 inet_diag_table[type] = h;
904 err = 0;
905 }
Herbert Xud523a322007-12-03 15:51:25 +1100906 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300907out:
908 return err;
909}
910EXPORT_SYMBOL_GPL(inet_diag_register);
911
912void inet_diag_unregister(const struct inet_diag_handler *h)
913{
914 const __u16 type = h->idiag_type;
915
916 if (type >= INET_DIAG_GETSOCK_MAX)
917 return;
918
Herbert Xud523a322007-12-03 15:51:25 +1100919 mutex_lock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300920 inet_diag_table[type] = NULL;
Herbert Xud523a322007-12-03 15:51:25 +1100921 mutex_unlock(&inet_diag_table_mutex);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300922}
923EXPORT_SYMBOL_GPL(inet_diag_unregister);
924
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300925static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926{
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300927 const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
928 sizeof(struct inet_diag_handler *));
929 int err = -ENOMEM;
930
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700931 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300932 if (!inet_diag_table)
933 goto out;
934
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200935 idiagnl = netlink_kernel_create(&init_net, NETLINK_INET_DIAG, 0,
Herbert Xud523a322007-12-03 15:51:25 +1100936 inet_diag_rcv, NULL, THIS_MODULE);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300937 if (idiagnl == NULL)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300938 goto out_free_table;
Arnaldo Carvalho de Melo17b085e2005-08-12 12:59:17 -0300939 err = 0;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300940out:
941 return err;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300942out_free_table:
943 kfree(inet_diag_table);
944 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945}
946
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300947static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Denis V. Lunevb7c6ba62008-01-28 14:41:19 -0800949 netlink_kernel_release(idiagnl);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300950 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951}
952
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300953module_init(inet_diag_init);
954module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955MODULE_LICENSE("GPL");
Jean Delvare305e1e92007-10-21 16:44:04 -0700956MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_INET_DIAG);