blob: def007ec1d6f85b3f1f9830554c8a4c0be70a905 [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 *
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -03004 * Version: $Id: inet_diag.c,v 1.3 2002/02/01 22:01:04 davem Exp $
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/fcntl.h>
17#include <linux/random.h>
18#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
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -080053static int inet_csk_diag_fill(struct sock *sk,
54 struct sk_buff *skb,
55 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
56 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -070057{
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070058 const struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -070059 const struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030060 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 struct nlmsghdr *nlh;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030062 void *info = NULL;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030063 struct inet_diag_meminfo *minfo = NULL;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -070064 unsigned char *b = skb_tail_pointer(skb);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030065 const struct inet_diag_handler *handler;
66
67 handler = inet_diag_table[unlh->nlmsg_type];
68 BUG_ON(handler == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -030070 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 nlh->nlmsg_flags = nlmsg_flags;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -030072
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 r = NLMSG_DATA(nlh);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -080074 BUG_ON(sk->sk_state == TCP_TIME_WAIT);
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -080075
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -080076 if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
77 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo));
78
79 if (ext & (1 << (INET_DIAG_INFO - 1)))
80 info = INET_DIAG_PUT(skb, INET_DIAG_INFO,
81 handler->idiag_info_size);
82
83 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
84 const size_t len = strlen(icsk->icsk_ca_ops->name);
85
86 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
87 icsk->icsk_ca_ops->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 }
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -080089
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030090 r->idiag_family = sk->sk_family;
91 r->idiag_state = sk->sk_state;
92 r->idiag_timer = 0;
93 r->idiag_retrans = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030095 r->id.idiag_if = sk->sk_bound_dev_if;
96 r->id.idiag_cookie[0] = (u32)(unsigned long)sk;
97 r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -030099 r->id.idiag_sport = inet->sport;
100 r->id.idiag_dport = inet->dport;
101 r->id.idiag_src[0] = inet->rcv_saddr;
102 r->id.idiag_dst[0] = inet->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300104#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300105 if (r->idiag_family == AF_INET6) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 struct ipv6_pinfo *np = inet6_sk(sk);
107
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300108 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 &np->rcv_saddr);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300110 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 &np->daddr);
112 }
113#endif
114
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300115#define EXPIRES_IN_MS(tmo) ((tmo - jiffies) * 1000 + HZ - 1) / HZ
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700117 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300118 r->idiag_timer = 1;
119 r->idiag_retrans = icsk->icsk_retransmits;
120 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700121 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300122 r->idiag_timer = 4;
123 r->idiag_retrans = icsk->icsk_probes_out;
124 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 } else if (timer_pending(&sk->sk_timer)) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300126 r->idiag_timer = 2;
127 r->idiag_retrans = icsk->icsk_probes_out;
128 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 } else {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300130 r->idiag_timer = 0;
131 r->idiag_expires = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133#undef EXPIRES_IN_MS
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300134
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300135 r->idiag_uid = sock_i_uid(sk);
136 r->idiag_inode = sock_i_ino(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137
138 if (minfo) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300139 minfo->idiag_rmem = atomic_read(&sk->sk_rmem_alloc);
140 minfo->idiag_wmem = sk->sk_wmem_queued;
141 minfo->idiag_fmem = sk->sk_forward_alloc;
142 minfo->idiag_tmem = atomic_read(&sk->sk_wmem_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300145 handler->idiag_get_info(sk, r, info);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Arnaldo Carvalho de Melo6687e982005-08-10 04:03:31 -0300147 if (sk->sk_state < TCP_TIME_WAIT &&
148 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
149 icsk->icsk_ca_ops->get_info(sk, ext, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700151 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 return skb->len;
153
Stephen Hemminger7c99c902005-06-23 12:20:36 -0700154rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700156 nlmsg_trim(skb, b);
Patrick McHardy26932562007-01-31 23:16:40 -0800157 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800160static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
161 struct sk_buff *skb, int ext, u32 pid,
162 u32 seq, u16 nlmsg_flags,
163 const struct nlmsghdr *unlh)
164{
165 long tmo;
166 struct inet_diag_msg *r;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700167 const unsigned char *previous_tail = skb_tail_pointer(skb);
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800168 struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq,
169 unlh->nlmsg_type, sizeof(*r));
170
171 r = NLMSG_DATA(nlh);
172 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
173
174 nlh->nlmsg_flags = nlmsg_flags;
175
176 tmo = tw->tw_ttd - jiffies;
177 if (tmo < 0)
178 tmo = 0;
179
180 r->idiag_family = tw->tw_family;
181 r->idiag_state = tw->tw_state;
182 r->idiag_timer = 0;
183 r->idiag_retrans = 0;
184 r->id.idiag_if = tw->tw_bound_dev_if;
185 r->id.idiag_cookie[0] = (u32)(unsigned long)tw;
186 r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1);
187 r->id.idiag_sport = tw->tw_sport;
188 r->id.idiag_dport = tw->tw_dport;
189 r->id.idiag_src[0] = tw->tw_rcv_saddr;
190 r->id.idiag_dst[0] = tw->tw_daddr;
191 r->idiag_state = tw->tw_substate;
192 r->idiag_timer = 3;
193 r->idiag_expires = (tmo * 1000 + HZ - 1) / HZ;
194 r->idiag_rqueue = 0;
195 r->idiag_wqueue = 0;
196 r->idiag_uid = 0;
197 r->idiag_inode = 0;
198#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
199 if (tw->tw_family == AF_INET6) {
200 const struct inet6_timewait_sock *tw6 =
201 inet6_twsk((struct sock *)tw);
202
203 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
204 &tw6->tw_v6_rcv_saddr);
205 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
206 &tw6->tw_v6_daddr);
207 }
208#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700209 nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800210 return skb->len;
211nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700212 nlmsg_trim(skb, previous_tail);
Patrick McHardy26932562007-01-31 23:16:40 -0800213 return -EMSGSIZE;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800214}
215
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800216static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
217 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
218 const struct nlmsghdr *unlh)
219{
220 if (sk->sk_state == TCP_TIME_WAIT)
221 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
222 skb, ext, pid, seq, nlmsg_flags,
223 unlh);
224 return inet_csk_diag_fill(sk, skb, ext, pid, seq, nlmsg_flags, unlh);
225}
226
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800227static int inet_diag_get_exact(struct sk_buff *in_skb,
228 const struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 int err;
231 struct sock *sk;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300232 struct inet_diag_req *req = NLMSG_DATA(nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct sk_buff *rep;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300234 struct inet_hashinfo *hashinfo;
235 const struct inet_diag_handler *handler;
236
237 handler = inet_diag_table[nlh->nlmsg_type];
238 BUG_ON(handler == NULL);
239 hashinfo = handler->idiag_hashinfo;
240
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300241 if (req->idiag_family == AF_INET) {
242 sk = inet_lookup(hashinfo, req->id.idiag_dst[0],
243 req->id.idiag_dport, req->id.idiag_src[0],
244 req->id.idiag_sport, req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300246#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300247 else if (req->idiag_family == AF_INET6) {
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300248 sk = inet6_lookup(hashinfo,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300249 (struct in6_addr *)req->id.idiag_dst,
250 req->id.idiag_dport,
251 (struct in6_addr *)req->id.idiag_src,
252 req->id.idiag_sport,
253 req->id.idiag_if);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255#endif
256 else {
257 return -EINVAL;
258 }
259
260 if (sk == NULL)
261 return -ENOENT;
262
263 err = -ESTALE;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300264 if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
265 req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
266 ((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
267 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 goto out;
269
270 err = -ENOMEM;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300271 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
272 sizeof(struct inet_diag_meminfo) +
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300273 handler->idiag_info_size + 64)),
274 GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!rep)
276 goto out;
277
Patrick McHardy26932562007-01-31 23:16:40 -0800278 err = sk_diag_fill(sk, rep, req->idiag_ext,
279 NETLINK_CB(in_skb).pid,
280 nlh->nlmsg_seq, 0, nlh);
281 if (err < 0) {
282 WARN_ON(err == -EMSGSIZE);
283 kfree_skb(rep);
284 goto out;
285 }
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300286 err = netlink_unicast(idiagnl, rep, NETLINK_CB(in_skb).pid,
287 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 if (err > 0)
289 err = 0;
290
291out:
292 if (sk) {
293 if (sk->sk_state == TCP_TIME_WAIT)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700294 inet_twsk_put((struct inet_timewait_sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 else
296 sock_put(sk);
297 }
298 return err;
299}
300
Al Viro9f855292006-09-27 18:44:30 -0700301static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
303 int words = bits >> 5;
304
305 bits &= 0x1f;
306
307 if (words) {
308 if (memcmp(a1, a2, words << 2))
309 return 0;
310 }
311 if (bits) {
Al Viro9f855292006-09-27 18:44:30 -0700312 __be32 w1, w2;
313 __be32 mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 w1 = a1[words];
316 w2 = a2[words];
317
318 mask = htonl((0xffffffff) << (32 - bits));
319
320 if ((w1 ^ w2) & mask)
321 return 0;
322 }
323
324 return 1;
325}
326
327
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300328static int inet_diag_bc_run(const void *bc, int len,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800329 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330{
331 while (len > 0) {
332 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300333 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
335 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300336 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300338 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 yes = 0;
340 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300341 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 yes = entry->sport >= op[1].no;
343 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300344 case INET_DIAG_BC_S_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 yes = entry->dport <= op[1].no;
346 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300347 case INET_DIAG_BC_D_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 yes = entry->dport >= op[1].no;
349 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300350 case INET_DIAG_BC_D_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 yes = entry->dport <= op[1].no;
352 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300353 case INET_DIAG_BC_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
355 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300356 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300357 case INET_DIAG_BC_D_COND: {
358 struct inet_diag_hostcond *cond;
Al Viro9f855292006-09-27 18:44:30 -0700359 __be32 *addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300361 cond = (struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300363 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 entry->sport : entry->dport)) {
365 yes = 0;
366 break;
367 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (cond->prefix_len == 0)
370 break;
371
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300372 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 addr = entry->saddr;
374 else
375 addr = entry->daddr;
376
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800377 if (bitstring_match(addr, cond->addr,
378 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 break;
380 if (entry->family == AF_INET6 &&
381 cond->family == AF_INET) {
382 if (addr[0] == 0 && addr[1] == 0 &&
383 addr[2] == htonl(0xffff) &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300384 bitstring_match(addr + 3, cond->addr,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900385 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 break;
387 }
388 yes = 0;
389 break;
390 }
391 }
392
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800393 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 len -= op->yes;
395 bc += op->yes;
396 } else {
397 len -= op->no;
398 bc += op->no;
399 }
400 }
401 return (len == 0);
402}
403
404static int valid_cc(const void *bc, int len, int cc)
405{
406 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300407 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 if (cc > len)
410 return 0;
411 if (cc == len)
412 return 1;
413 if (op->yes < 4)
414 return 0;
415 len -= op->yes;
416 bc += op->yes;
417 }
418 return 0;
419}
420
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300421static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
423 const unsigned char *bc = bytecode;
424 int len = bytecode_len;
425
426 while (len > 0) {
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300427 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428
429//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
430 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300431 case INET_DIAG_BC_AUTO:
432 case INET_DIAG_BC_S_COND:
433 case INET_DIAG_BC_D_COND:
434 case INET_DIAG_BC_S_GE:
435 case INET_DIAG_BC_S_LE:
436 case INET_DIAG_BC_D_GE:
437 case INET_DIAG_BC_D_LE:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300438 if (op->yes < 4 || op->yes > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return -EINVAL;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300440 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300441 if (op->no < 4 || op->no > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -EINVAL;
443 if (op->no < len &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300444 !valid_cc(bytecode, bytecode_len, len - op->no))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 return -EINVAL;
446 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300447 case INET_DIAG_BC_NOP:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300448 if (op->yes < 4 || op->yes > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 return -EINVAL;
450 break;
451 default:
452 return -EINVAL;
453 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800454 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 len -= op->yes;
456 }
457 return len == 0 ? 0 : -EINVAL;
458}
459
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800460static int inet_csk_diag_dump(struct sock *sk,
461 struct sk_buff *skb,
462 struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300464 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
466 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300467 struct inet_diag_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 struct rtattr *bc = (struct rtattr *)(r + 1);
469 struct inet_sock *inet = inet_sk(sk);
470
471 entry.family = sk->sk_family;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300472#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 if (entry.family == AF_INET6) {
474 struct ipv6_pinfo *np = inet6_sk(sk);
475
476 entry.saddr = np->rcv_saddr.s6_addr32;
477 entry.daddr = np->daddr.s6_addr32;
478 } else
479#endif
480 {
481 entry.saddr = &inet->rcv_saddr;
482 entry.daddr = &inet->daddr;
483 }
484 entry.sport = inet->num;
485 entry.dport = ntohs(inet->dport);
486 entry.userlocks = sk->sk_userlocks;
487
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300488 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 return 0;
490 }
491
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800492 return inet_csk_diag_fill(sk, skb, r->idiag_ext,
493 NETLINK_CB(cb->skb).pid,
494 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800497static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
498 struct sk_buff *skb,
499 struct netlink_callback *cb)
500{
501 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
502
503 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
504 struct inet_diag_entry entry;
505 struct rtattr *bc = (struct rtattr *)(r + 1);
506
507 entry.family = tw->tw_family;
508#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
509 if (tw->tw_family == AF_INET6) {
510 struct inet6_timewait_sock *tw6 =
511 inet6_twsk((struct sock *)tw);
512 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
513 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
514 } else
515#endif
516 {
517 entry.saddr = &tw->tw_rcv_saddr;
518 entry.daddr = &tw->tw_daddr;
519 }
520 entry.sport = tw->tw_num;
521 entry.dport = ntohs(tw->tw_dport);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900522 entry.userlocks = 0;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800523
524 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
525 return 0;
526 }
527
528 return inet_twsk_diag_fill(tw, skb, r->idiag_ext,
529 NETLINK_CB(cb->skb).pid,
530 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
531}
532
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300533static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800534 struct request_sock *req, u32 pid, u32 seq,
535 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700537 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct inet_sock *inet = inet_sk(sk);
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700539 unsigned char *b = skb_tail_pointer(skb);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300540 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 struct nlmsghdr *nlh;
542 long tmo;
543
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300544 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 nlh->nlmsg_flags = NLM_F_MULTI;
546 r = NLMSG_DATA(nlh);
547
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300548 r->idiag_family = sk->sk_family;
549 r->idiag_state = TCP_SYN_RECV;
550 r->idiag_timer = 1;
551 r->idiag_retrans = req->retrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300553 r->id.idiag_if = sk->sk_bound_dev_if;
554 r->id.idiag_cookie[0] = (u32)(unsigned long)req;
555 r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
557 tmo = req->expires - jiffies;
558 if (tmo < 0)
559 tmo = 0;
560
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300561 r->id.idiag_sport = inet->sport;
562 r->id.idiag_dport = ireq->rmt_port;
563 r->id.idiag_src[0] = ireq->loc_addr;
564 r->id.idiag_dst[0] = ireq->rmt_addr;
565 r->idiag_expires = jiffies_to_msecs(tmo);
566 r->idiag_rqueue = 0;
567 r->idiag_wqueue = 0;
568 r->idiag_uid = sock_i_uid(sk);
569 r->idiag_inode = 0;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300570#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300571 if (r->idiag_family == AF_INET6) {
572 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800573 &inet6_rsk(req)->loc_addr);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300574 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800575 &inet6_rsk(req)->rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577#endif
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700578 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
580 return skb->len;
581
582nlmsg_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700583 nlmsg_trim(skb, b);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return -1;
585}
586
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300587static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800588 struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300590 struct inet_diag_entry entry;
591 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700592 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo2ad69c52005-06-18 22:48:55 -0700593 struct listen_sock *lopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 struct rtattr *bc = NULL;
595 struct inet_sock *inet = inet_sk(sk);
596 int j, s_j;
597 int reqnum, s_reqnum;
598 int err = 0;
599
600 s_j = cb->args[3];
601 s_reqnum = cb->args[4];
602
603 if (s_j > 0)
604 s_j--;
605
606 entry.family = sk->sk_family;
607
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700608 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700610 lopt = icsk->icsk_accept_queue.listen_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 if (!lopt || !lopt->qlen)
612 goto out;
613
614 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
615 bc = (struct rtattr *)(r + 1);
616 entry.sport = inet->num;
617 entry.userlocks = sk->sk_userlocks;
618 }
619
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300620 for (j = s_j; j < lopt->nr_table_entries; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700621 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
623 reqnum = 0;
624 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700625 struct inet_request_sock *ireq = inet_rsk(req);
626
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 if (reqnum < s_reqnum)
628 continue;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300629 if (r->id.idiag_dport != ireq->rmt_port &&
630 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 continue;
632
633 if (bc) {
634 entry.saddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300635#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800637 inet6_rsk(req)->loc_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700639 &ireq->loc_addr;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800640 entry.daddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300641#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800643 inet6_rsk(req)->rmt_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700645 &ireq->rmt_addr;
646 entry.dport = ntohs(ireq->rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300648 if (!inet_diag_bc_run(RTA_DATA(bc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 RTA_PAYLOAD(bc), &entry))
650 continue;
651 }
652
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300653 err = inet_diag_fill_req(skb, sk, req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 NETLINK_CB(cb->skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300655 cb->nlh->nlmsg_seq, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (err < 0) {
657 cb->args[3] = j + 1;
658 cb->args[4] = reqnum;
659 goto out;
660 }
661 }
662
663 s_reqnum = 0;
664 }
665
666out:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700667 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
669 return err;
670}
671
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300672static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
674 int i, num;
675 int s_i, s_num;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300676 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300677 const struct inet_diag_handler *handler;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300678 struct inet_hashinfo *hashinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300680 handler = inet_diag_table[cb->nlh->nlmsg_type];
681 BUG_ON(handler == NULL);
682 hashinfo = handler->idiag_hashinfo;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 s_i = cb->args[1];
685 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300686
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 if (cb->args[0] == 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300688 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300690
691 inet_listen_lock(hashinfo);
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700692 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 struct sock *sk;
694 struct hlist_node *node;
695
696 num = 0;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300697 sk_for_each(sk, node, &hashinfo->listening_hash[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct inet_sock *inet = inet_sk(sk);
699
700 if (num < s_num) {
701 num++;
702 continue;
703 }
704
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300705 if (r->id.idiag_sport != inet->sport &&
706 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 goto next_listen;
708
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300709 if (!(r->idiag_states & TCPF_LISTEN) ||
710 r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 cb->args[3] > 0)
712 goto syn_recv;
713
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800714 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300715 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 goto done;
717 }
718
719syn_recv:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300720 if (!(r->idiag_states & TCPF_SYN_RECV))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 goto next_listen;
722
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300723 if (inet_diag_dump_reqs(skb, sk, cb) < 0) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300724 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 goto done;
726 }
727
728next_listen:
729 cb->args[3] = 0;
730 cb->args[4] = 0;
731 ++num;
732 }
733
734 s_num = 0;
735 cb->args[3] = 0;
736 cb->args[4] = 0;
737 }
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300738 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739skip_listen_ht:
740 cb->args[0] = 1;
741 s_i = num = s_num = 0;
742 }
743
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300744 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 return skb->len;
746
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300747 for (i = s_i; i < hashinfo->ehash_size; i++) {
748 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 struct sock *sk;
750 struct hlist_node *node;
751
752 if (i > s_i)
753 s_num = 0;
754
755 read_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 num = 0;
757 sk_for_each(sk, node, &head->chain) {
758 struct inet_sock *inet = inet_sk(sk);
759
760 if (num < s_num)
761 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300762 if (!(r->idiag_states & (1 << sk->sk_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300764 if (r->id.idiag_sport != inet->sport &&
765 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 goto next_normal;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800767 if (r->id.idiag_dport != inet->dport &&
768 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 goto next_normal;
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800770 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 read_unlock_bh(&head->lock);
772 goto done;
773 }
774next_normal:
775 ++num;
776 }
777
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300778 if (r->idiag_states & TCPF_TIME_WAIT) {
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800779 struct inet_timewait_sock *tw;
780
781 inet_twsk_for_each(tw, node,
Eric Dumazetdbca9b2752007-02-08 14:16:46 -0800782 &head->twchain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
784 if (num < s_num)
785 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800786 if (r->id.idiag_sport != tw->tw_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300787 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800789 if (r->id.idiag_dport != tw->tw_dport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300790 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 goto next_dying;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800792 if (inet_twsk_diag_dump(tw, skb, cb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 read_unlock_bh(&head->lock);
794 goto done;
795 }
796next_dying:
797 ++num;
798 }
799 }
800 read_unlock_bh(&head->lock);
801 }
802
803done:
804 cb->args[1] = i;
805 cb->args[2] = num;
806 return skb->len;
807}
808
Thomas Grafead592b2007-03-22 23:30:35 -0700809static int inet_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Thomas Grafead592b2007-03-22 23:30:35 -0700811 int hdrlen = sizeof(struct inet_diag_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
Thomas Grafead592b2007-03-22 23:30:35 -0700813 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
814 nlmsg_len(nlh) < hdrlen)
815 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300817 if (inet_diag_table[nlh->nlmsg_type] == NULL)
818 return -ENOENT;
819
Thomas Grafead592b2007-03-22 23:30:35 -0700820 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafead592b2007-03-22 23:30:35 -0700821 if (nlmsg_attrlen(nlh, hdrlen)) {
822 struct nlattr *attr;
823
824 attr = nlmsg_find_attr(nlh, hdrlen,
825 INET_DIAG_REQ_BYTECODE);
826 if (attr == NULL ||
827 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
828 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
829 return -EINVAL;
830 }
831
Thomas Grafc702e802007-03-22 23:30:55 -0700832 return netlink_dump_start(idiagnl, skb, nlh,
833 inet_diag_dump, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 }
Thomas Grafead592b2007-03-22 23:30:35 -0700835
836 return inet_diag_get_exact(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837}
838
Patrick McHardy0a9c7302007-09-11 11:33:28 +0200839static DEFINE_MUTEX(inet_diag_mutex);
840
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300841static void inet_diag_rcv(struct sock *sk, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Thomas Grafead592b2007-03-22 23:30:35 -0700843 unsigned int qlen = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
Thomas Grafead592b2007-03-22 23:30:35 -0700845 do {
Patrick McHardy0a9c7302007-09-11 11:33:28 +0200846 mutex_lock(&inet_diag_mutex);
Thomas Grafead592b2007-03-22 23:30:35 -0700847 netlink_run_queue(sk, &qlen, &inet_diag_rcv_msg);
Patrick McHardy0a9c7302007-09-11 11:33:28 +0200848 mutex_unlock(&inet_diag_mutex);
Thomas Grafead592b2007-03-22 23:30:35 -0700849 } while (qlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300852static DEFINE_SPINLOCK(inet_diag_register_lock);
853
854int inet_diag_register(const struct inet_diag_handler *h)
855{
856 const __u16 type = h->idiag_type;
857 int err = -EINVAL;
858
859 if (type >= INET_DIAG_GETSOCK_MAX)
860 goto out;
861
862 spin_lock(&inet_diag_register_lock);
863 err = -EEXIST;
864 if (inet_diag_table[type] == NULL) {
865 inet_diag_table[type] = h;
866 err = 0;
867 }
868 spin_unlock(&inet_diag_register_lock);
869out:
870 return err;
871}
872EXPORT_SYMBOL_GPL(inet_diag_register);
873
874void inet_diag_unregister(const struct inet_diag_handler *h)
875{
876 const __u16 type = h->idiag_type;
877
878 if (type >= INET_DIAG_GETSOCK_MAX)
879 return;
880
881 spin_lock(&inet_diag_register_lock);
882 inet_diag_table[type] = NULL;
883 spin_unlock(&inet_diag_register_lock);
884
885 synchronize_rcu();
886}
887EXPORT_SYMBOL_GPL(inet_diag_unregister);
888
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300889static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300891 const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
892 sizeof(struct inet_diag_handler *));
893 int err = -ENOMEM;
894
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700895 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300896 if (!inet_diag_table)
897 goto out;
898
Patrick McHardy06628602005-08-15 12:33:26 -0700899 idiagnl = netlink_kernel_create(NETLINK_INET_DIAG, 0, inet_diag_rcv,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700900 NULL, THIS_MODULE);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300901 if (idiagnl == NULL)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300902 goto out_free_table;
Arnaldo Carvalho de Melo17b085e2005-08-12 12:59:17 -0300903 err = 0;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300904out:
905 return err;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300906out_free_table:
907 kfree(inet_diag_table);
908 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909}
910
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300911static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300913 sock_release(idiagnl->sk_socket);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300914 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915}
916
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300917module_init(inet_diag_init);
918module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919MODULE_LICENSE("GPL");