blob: 457db99c76df79ca2724af4891f05940fbec46b7 [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
14#include <linux/config.h>
15#include <linux/module.h>
16#include <linux/types.h>
17#include <linux/fcntl.h>
18#include <linux/random.h>
19#include <linux/cache.h>
20#include <linux/init.h>
21#include <linux/time.h>
22
23#include <net/icmp.h>
24#include <net/tcp.h>
25#include <net/ipv6.h>
26#include <net/inet_common.h>
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -030027#include <net/inet_connection_sock.h>
28#include <net/inet_hashtables.h>
29#include <net/inet_timewait_sock.h>
30#include <net/inet6_hashtables.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 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 u32 *saddr;
41 u32 *daddr;
42 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 unsigned char *b = skb->tail;
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
151 nlh->nlmsg_len = skb->tail - b;
152 return skb->len;
153
Stephen Hemminger7c99c902005-06-23 12:20:36 -0700154rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155nlmsg_failure:
156 skb_trim(skb, b - skb->data);
157 return -1;
158}
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;
167 const unsigned char *previous_tail = skb->tail;
168 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
209 nlh->nlmsg_len = skb->tail - previous_tail;
210 return skb->len;
211nlmsg_failure:
212 skb_trim(skb, previous_tail - skb->data);
213 return -1;
214}
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
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800278 if (sk_diag_fill(sk, rep, req->idiag_ext,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 NETLINK_CB(in_skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300280 nlh->nlmsg_seq, 0, nlh) <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 BUG();
282
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300283 err = netlink_unicast(idiagnl, rep, NETLINK_CB(in_skb).pid,
284 MSG_DONTWAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (err > 0)
286 err = 0;
287
288out:
289 if (sk) {
290 if (sk->sk_state == TCP_TIME_WAIT)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700291 inet_twsk_put((struct inet_timewait_sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 else
293 sock_put(sk);
294 }
295 return err;
296}
297
298static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
299{
300 int words = bits >> 5;
301
302 bits &= 0x1f;
303
304 if (words) {
305 if (memcmp(a1, a2, words << 2))
306 return 0;
307 }
308 if (bits) {
309 __u32 w1, w2;
310 __u32 mask;
311
312 w1 = a1[words];
313 w2 = a2[words];
314
315 mask = htonl((0xffffffff) << (32 - bits));
316
317 if ((w1 ^ w2) & mask)
318 return 0;
319 }
320
321 return 1;
322}
323
324
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300325static int inet_diag_bc_run(const void *bc, int len,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800326 const struct inet_diag_entry *entry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
328 while (len > 0) {
329 int yes = 1;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300330 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300333 case INET_DIAG_BC_NOP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300335 case INET_DIAG_BC_JMP:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 yes = 0;
337 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300338 case INET_DIAG_BC_S_GE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 yes = entry->sport >= op[1].no;
340 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300341 case INET_DIAG_BC_S_LE:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 yes = entry->dport <= op[1].no;
343 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300344 case INET_DIAG_BC_D_GE:
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_LE:
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_AUTO:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
352 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300353 case INET_DIAG_BC_S_COND:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300354 case INET_DIAG_BC_D_COND: {
355 struct inet_diag_hostcond *cond;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 u32 *addr;
357
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300358 cond = (struct inet_diag_hostcond *)(op + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (cond->port != -1 &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300360 cond->port != (op->code == INET_DIAG_BC_S_COND ?
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 entry->sport : entry->dport)) {
362 yes = 0;
363 break;
364 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (cond->prefix_len == 0)
367 break;
368
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300369 if (op->code == INET_DIAG_BC_S_COND)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 addr = entry->saddr;
371 else
372 addr = entry->daddr;
373
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800374 if (bitstring_match(addr, cond->addr,
375 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 break;
377 if (entry->family == AF_INET6 &&
378 cond->family == AF_INET) {
379 if (addr[0] == 0 && addr[1] == 0 &&
380 addr[2] == htonl(0xffff) &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300381 bitstring_match(addr + 3, cond->addr,
382 cond->prefix_len))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 break;
384 }
385 yes = 0;
386 break;
387 }
388 }
389
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800390 if (yes) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 len -= op->yes;
392 bc += op->yes;
393 } else {
394 len -= op->no;
395 bc += op->no;
396 }
397 }
398 return (len == 0);
399}
400
401static int valid_cc(const void *bc, int len, int cc)
402{
403 while (len >= 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300404 const struct inet_diag_bc_op *op = bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 if (cc > len)
407 return 0;
408 if (cc == len)
409 return 1;
410 if (op->yes < 4)
411 return 0;
412 len -= op->yes;
413 bc += op->yes;
414 }
415 return 0;
416}
417
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300418static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419{
420 const unsigned char *bc = bytecode;
421 int len = bytecode_len;
422
423 while (len > 0) {
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300424 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)bc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425
426//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
427 switch (op->code) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300428 case INET_DIAG_BC_AUTO:
429 case INET_DIAG_BC_S_COND:
430 case INET_DIAG_BC_D_COND:
431 case INET_DIAG_BC_S_GE:
432 case INET_DIAG_BC_S_LE:
433 case INET_DIAG_BC_D_GE:
434 case INET_DIAG_BC_D_LE:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300435 if (op->yes < 4 || op->yes > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 return -EINVAL;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300437 case INET_DIAG_BC_JMP:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300438 if (op->no < 4 || op->no > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return -EINVAL;
440 if (op->no < len &&
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300441 !valid_cc(bytecode, bytecode_len, len - op->no))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 return -EINVAL;
443 break;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300444 case INET_DIAG_BC_NOP:
Arnaldo Carvalho de Meloa8c21902005-08-12 12:56:38 -0300445 if (op->yes < 4 || op->yes > len + 4)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 return -EINVAL;
447 break;
448 default:
449 return -EINVAL;
450 }
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800451 bc += op->yes;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 len -= op->yes;
453 }
454 return len == 0 ? 0 : -EINVAL;
455}
456
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800457static int inet_csk_diag_dump(struct sock *sk,
458 struct sk_buff *skb,
459 struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300461 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
463 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300464 struct inet_diag_entry entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 struct rtattr *bc = (struct rtattr *)(r + 1);
466 struct inet_sock *inet = inet_sk(sk);
467
468 entry.family = sk->sk_family;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300469#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (entry.family == AF_INET6) {
471 struct ipv6_pinfo *np = inet6_sk(sk);
472
473 entry.saddr = np->rcv_saddr.s6_addr32;
474 entry.daddr = np->daddr.s6_addr32;
475 } else
476#endif
477 {
478 entry.saddr = &inet->rcv_saddr;
479 entry.daddr = &inet->daddr;
480 }
481 entry.sport = inet->num;
482 entry.dport = ntohs(inet->dport);
483 entry.userlocks = sk->sk_userlocks;
484
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300485 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 return 0;
487 }
488
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800489 return inet_csk_diag_fill(sk, skb, r->idiag_ext,
490 NETLINK_CB(cb->skb).pid,
491 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492}
493
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800494static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
495 struct sk_buff *skb,
496 struct netlink_callback *cb)
497{
498 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
499
500 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
501 struct inet_diag_entry entry;
502 struct rtattr *bc = (struct rtattr *)(r + 1);
503
504 entry.family = tw->tw_family;
505#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
506 if (tw->tw_family == AF_INET6) {
507 struct inet6_timewait_sock *tw6 =
508 inet6_twsk((struct sock *)tw);
509 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
510 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
511 } else
512#endif
513 {
514 entry.saddr = &tw->tw_rcv_saddr;
515 entry.daddr = &tw->tw_daddr;
516 }
517 entry.sport = tw->tw_num;
518 entry.dport = ntohs(tw->tw_dport);
519 entry.userlocks = 0;
520
521 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
522 return 0;
523 }
524
525 return inet_twsk_diag_fill(tw, skb, r->idiag_ext,
526 NETLINK_CB(cb->skb).pid,
527 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
528}
529
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300530static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800531 struct request_sock *req, u32 pid, u32 seq,
532 const struct nlmsghdr *unlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700534 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 struct inet_sock *inet = inet_sk(sk);
536 unsigned char *b = skb->tail;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300537 struct inet_diag_msg *r;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 struct nlmsghdr *nlh;
539 long tmo;
540
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300541 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 nlh->nlmsg_flags = NLM_F_MULTI;
543 r = NLMSG_DATA(nlh);
544
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300545 r->idiag_family = sk->sk_family;
546 r->idiag_state = TCP_SYN_RECV;
547 r->idiag_timer = 1;
548 r->idiag_retrans = req->retrans;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300550 r->id.idiag_if = sk->sk_bound_dev_if;
551 r->id.idiag_cookie[0] = (u32)(unsigned long)req;
552 r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 tmo = req->expires - jiffies;
555 if (tmo < 0)
556 tmo = 0;
557
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300558 r->id.idiag_sport = inet->sport;
559 r->id.idiag_dport = ireq->rmt_port;
560 r->id.idiag_src[0] = ireq->loc_addr;
561 r->id.idiag_dst[0] = ireq->rmt_addr;
562 r->idiag_expires = jiffies_to_msecs(tmo);
563 r->idiag_rqueue = 0;
564 r->idiag_wqueue = 0;
565 r->idiag_uid = sock_i_uid(sk);
566 r->idiag_inode = 0;
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300567#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300568 if (r->idiag_family == AF_INET6) {
569 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800570 &inet6_rsk(req)->loc_addr);
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300571 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800572 &inet6_rsk(req)->rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
574#endif
575 nlh->nlmsg_len = skb->tail - b;
576
577 return skb->len;
578
579nlmsg_failure:
580 skb_trim(skb, b - skb->data);
581 return -1;
582}
583
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300584static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800585 struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300587 struct inet_diag_entry entry;
588 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700589 struct inet_connection_sock *icsk = inet_csk(sk);
Arnaldo Carvalho de Melo2ad69c52005-06-18 22:48:55 -0700590 struct listen_sock *lopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 struct rtattr *bc = NULL;
592 struct inet_sock *inet = inet_sk(sk);
593 int j, s_j;
594 int reqnum, s_reqnum;
595 int err = 0;
596
597 s_j = cb->args[3];
598 s_reqnum = cb->args[4];
599
600 if (s_j > 0)
601 s_j--;
602
603 entry.family = sk->sk_family;
604
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700605 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700607 lopt = icsk->icsk_accept_queue.listen_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 if (!lopt || !lopt->qlen)
609 goto out;
610
611 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
612 bc = (struct rtattr *)(r + 1);
613 entry.sport = inet->num;
614 entry.userlocks = sk->sk_userlocks;
615 }
616
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300617 for (j = s_j; j < lopt->nr_table_entries; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700618 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 reqnum = 0;
621 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700622 struct inet_request_sock *ireq = inet_rsk(req);
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (reqnum < s_reqnum)
625 continue;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300626 if (r->id.idiag_dport != ireq->rmt_port &&
627 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 continue;
629
630 if (bc) {
631 entry.saddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300632#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800634 inet6_rsk(req)->loc_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700636 &ireq->loc_addr;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800637 entry.daddr =
Arnaldo Carvalho de Melo505cbfc2005-08-12 09:19:38 -0300638#if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Meloca304b62005-12-13 23:15:40 -0800640 inet6_rsk(req)->rmt_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700642 &ireq->rmt_addr;
643 entry.dport = ntohs(ireq->rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300645 if (!inet_diag_bc_run(RTA_DATA(bc),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 RTA_PAYLOAD(bc), &entry))
647 continue;
648 }
649
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300650 err = inet_diag_fill_req(skb, sk, req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 NETLINK_CB(cb->skb).pid,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300652 cb->nlh->nlmsg_seq, cb->nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 if (err < 0) {
654 cb->args[3] = j + 1;
655 cb->args[4] = reqnum;
656 goto out;
657 }
658 }
659
660 s_reqnum = 0;
661 }
662
663out:
Arnaldo Carvalho de Melo463c84b2005-08-09 20:10:42 -0700664 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 return err;
667}
668
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300669static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
671 int i, num;
672 int s_i, s_num;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300673 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300674 const struct inet_diag_handler *handler;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300675 struct inet_hashinfo *hashinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300677 handler = inet_diag_table[cb->nlh->nlmsg_type];
678 BUG_ON(handler == NULL);
679 hashinfo = handler->idiag_hashinfo;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 s_i = cb->args[1];
682 s_num = num = cb->args[2];
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 if (cb->args[0] == 0) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300685 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto skip_listen_ht;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300687
688 inet_listen_lock(hashinfo);
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700689 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct sock *sk;
691 struct hlist_node *node;
692
693 num = 0;
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300694 sk_for_each(sk, node, &hashinfo->listening_hash[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 struct inet_sock *inet = inet_sk(sk);
696
697 if (num < s_num) {
698 num++;
699 continue;
700 }
701
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300702 if (r->id.idiag_sport != inet->sport &&
703 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 goto next_listen;
705
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300706 if (!(r->idiag_states & TCPF_LISTEN) ||
707 r->id.idiag_dport ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 cb->args[3] > 0)
709 goto syn_recv;
710
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800711 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300712 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 goto done;
714 }
715
716syn_recv:
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300717 if (!(r->idiag_states & TCPF_SYN_RECV))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 goto next_listen;
719
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300720 if (inet_diag_dump_reqs(skb, sk, cb) < 0) {
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300721 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 goto done;
723 }
724
725next_listen:
726 cb->args[3] = 0;
727 cb->args[4] = 0;
728 ++num;
729 }
730
731 s_num = 0;
732 cb->args[3] = 0;
733 cb->args[4] = 0;
734 }
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300735 inet_listen_unlock(hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736skip_listen_ht:
737 cb->args[0] = 1;
738 s_i = num = s_num = 0;
739 }
740
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300741 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return skb->len;
743
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300744 for (i = s_i; i < hashinfo->ehash_size; i++) {
745 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 struct sock *sk;
747 struct hlist_node *node;
748
749 if (i > s_i)
750 s_num = 0;
751
752 read_lock_bh(&head->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 num = 0;
754 sk_for_each(sk, node, &head->chain) {
755 struct inet_sock *inet = inet_sk(sk);
756
757 if (num < s_num)
758 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300759 if (!(r->idiag_states & (1 << sk->sk_state)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto next_normal;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300761 if (r->id.idiag_sport != inet->sport &&
762 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 goto next_normal;
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800764 if (r->id.idiag_dport != inet->dport &&
765 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 goto next_normal;
Arnaldo Carvalho de Melodff2c032006-01-09 14:56:56 -0800767 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 read_unlock_bh(&head->lock);
769 goto done;
770 }
771next_normal:
772 ++num;
773 }
774
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300775 if (r->idiag_states & TCPF_TIME_WAIT) {
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800776 struct inet_timewait_sock *tw;
777
778 inet_twsk_for_each(tw, node,
Arnaldo Carvalho de Melo540722f2005-08-10 05:54:28 -0300779 &hashinfo->ehash[i + hashinfo->ehash_size].chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (num < s_num)
782 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800783 if (r->id.idiag_sport != tw->tw_sport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300784 r->id.idiag_sport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 goto next_dying;
Arnaldo Carvalho de Melo7dbf0752006-01-09 14:56:03 -0800786 if (r->id.idiag_dport != tw->tw_dport &&
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300787 r->id.idiag_dport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 goto next_dying;
Arnaldo Carvalho de Meloc7d58aa2006-01-09 14:56:38 -0800789 if (inet_twsk_diag_dump(tw, skb, cb) < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 read_unlock_bh(&head->lock);
791 goto done;
792 }
793next_dying:
794 ++num;
795 }
796 }
797 read_unlock_bh(&head->lock);
798 }
799
800done:
801 cb->args[1] = i;
802 cb->args[2] = num;
803 return skb->len;
804}
805
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800806static inline int inet_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
809 return 0;
810
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300811 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 goto err_inval;
813
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300814 if (inet_diag_table[nlh->nlmsg_type] == NULL)
815 return -ENOENT;
816
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300817 if (NLMSG_LENGTH(sizeof(struct inet_diag_req)) > skb->len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 goto err_inval;
819
820 if (nlh->nlmsg_flags&NLM_F_DUMP) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300821 if (nlh->nlmsg_len >
822 (4 + NLMSG_SPACE(sizeof(struct inet_diag_req)))) {
823 struct rtattr *rta = (void *)(NLMSG_DATA(nlh) +
824 sizeof(struct inet_diag_req));
825 if (rta->rta_type != INET_DIAG_REQ_BYTECODE ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 rta->rta_len < 8 ||
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300827 rta->rta_len >
828 (nlh->nlmsg_len -
829 NLMSG_SPACE(sizeof(struct inet_diag_req))))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 goto err_inval;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300831 if (inet_diag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 goto err_inval;
833 }
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300834 return netlink_dump_start(idiagnl, skb, nlh,
Thomas Grafa8f74b22005-11-10 02:25:52 +0100835 inet_diag_dump, NULL);
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800836 } else
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300837 return inet_diag_get_exact(skb, nlh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839err_inval:
840 return -EINVAL;
841}
842
843
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300844static inline void inet_diag_rcv_skb(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 if (skb->len >= NLMSG_SPACE(0)) {
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800847 int err;
848 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
849
850 if (nlh->nlmsg_len < sizeof(*nlh) ||
851 skb->len < nlh->nlmsg_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 return;
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300853 err = inet_diag_rcv_msg(skb, nlh);
Arnaldo Carvalho de Melo4e852c02006-01-09 14:56:19 -0800854 if (err || nlh->nlmsg_flags & NLM_F_ACK)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 netlink_ack(skb, nlh, err);
856 }
857}
858
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300859static void inet_diag_rcv(struct sock *sk, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
861 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700862 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700864 while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300865 inet_diag_rcv_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 kfree_skb(skb);
867 }
868}
869
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300870static DEFINE_SPINLOCK(inet_diag_register_lock);
871
872int inet_diag_register(const struct inet_diag_handler *h)
873{
874 const __u16 type = h->idiag_type;
875 int err = -EINVAL;
876
877 if (type >= INET_DIAG_GETSOCK_MAX)
878 goto out;
879
880 spin_lock(&inet_diag_register_lock);
881 err = -EEXIST;
882 if (inet_diag_table[type] == NULL) {
883 inet_diag_table[type] = h;
884 err = 0;
885 }
886 spin_unlock(&inet_diag_register_lock);
887out:
888 return err;
889}
890EXPORT_SYMBOL_GPL(inet_diag_register);
891
892void inet_diag_unregister(const struct inet_diag_handler *h)
893{
894 const __u16 type = h->idiag_type;
895
896 if (type >= INET_DIAG_GETSOCK_MAX)
897 return;
898
899 spin_lock(&inet_diag_register_lock);
900 inet_diag_table[type] = NULL;
901 spin_unlock(&inet_diag_register_lock);
902
903 synchronize_rcu();
904}
905EXPORT_SYMBOL_GPL(inet_diag_unregister);
906
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300907static int __init inet_diag_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300909 const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
910 sizeof(struct inet_diag_handler *));
911 int err = -ENOMEM;
912
913 inet_diag_table = kmalloc(inet_diag_table_size, GFP_KERNEL);
914 if (!inet_diag_table)
915 goto out;
916
917 memset(inet_diag_table, 0, inet_diag_table_size);
Patrick McHardy06628602005-08-15 12:33:26 -0700918 idiagnl = netlink_kernel_create(NETLINK_INET_DIAG, 0, inet_diag_rcv,
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300919 THIS_MODULE);
920 if (idiagnl == NULL)
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300921 goto out_free_table;
Arnaldo Carvalho de Melo17b085e2005-08-12 12:59:17 -0300922 err = 0;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300923out:
924 return err;
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300925out_free_table:
926 kfree(inet_diag_table);
927 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928}
929
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300930static void __exit inet_diag_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931{
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300932 sock_release(idiagnl->sk_socket);
Arnaldo Carvalho de Melo4f5736c2005-08-12 09:27:49 -0300933 kfree(inet_diag_table);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934}
935
Arnaldo Carvalho de Melo73c1f4a2005-08-12 12:51:49 -0300936module_init(inet_diag_init);
937module_exit(inet_diag_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938MODULE_LICENSE("GPL");