blob: 6f2d6f2276b981a6bd30b97867112b72d14cd4ce [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * tcp_diag.c Module for monitoring TCP sockets.
3 *
4 * Version: $Id: tcp_diag.c,v 1.3 2002/02/01 22:01:04 davem Exp $
5 *
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>
27
28#include <linux/inet.h>
29#include <linux/stddef.h>
30
31#include <linux/tcp_diag.h>
32
33struct tcpdiag_entry
34{
35 u32 *saddr;
36 u32 *daddr;
37 u16 sport;
38 u16 dport;
39 u16 family;
40 u16 userlocks;
41};
42
43static struct sock *tcpnl;
44
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#define TCPDIAG_PUT(skb, attrtype, attrlen) \
Stephen Hemminger7c99c902005-06-23 12:20:36 -070046 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48static int tcpdiag_fill(struct sk_buff *skb, struct sock *sk,
49 int ext, u32 pid, u32 seq, u16 nlmsg_flags)
50{
51 struct inet_sock *inet = inet_sk(sk);
52 struct tcp_sock *tp = tcp_sk(sk);
53 struct tcpdiagmsg *r;
54 struct nlmsghdr *nlh;
55 struct tcp_info *info = NULL;
56 struct tcpdiag_meminfo *minfo = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 unsigned char *b = skb->tail;
58
59 nlh = NLMSG_PUT(skb, pid, seq, TCPDIAG_GETSOCK, sizeof(*r));
60 nlh->nlmsg_flags = nlmsg_flags;
61 r = NLMSG_DATA(nlh);
62 if (sk->sk_state != TCP_TIME_WAIT) {
63 if (ext & (1<<(TCPDIAG_MEMINFO-1)))
64 minfo = TCPDIAG_PUT(skb, TCPDIAG_MEMINFO, sizeof(*minfo));
65 if (ext & (1<<(TCPDIAG_INFO-1)))
66 info = TCPDIAG_PUT(skb, TCPDIAG_INFO, sizeof(*info));
67
Stephen Hemminger056ede62005-06-23 12:21:28 -070068 if (ext & (1<<(TCPDIAG_CONG-1))) {
69 size_t len = strlen(tp->ca_ops->name);
70 strcpy(TCPDIAG_PUT(skb, TCPDIAG_CONG, len+1),
71 tp->ca_ops->name);
72 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 }
74 r->tcpdiag_family = sk->sk_family;
75 r->tcpdiag_state = sk->sk_state;
76 r->tcpdiag_timer = 0;
77 r->tcpdiag_retrans = 0;
78
79 r->id.tcpdiag_if = sk->sk_bound_dev_if;
80 r->id.tcpdiag_cookie[0] = (u32)(unsigned long)sk;
81 r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
82
83 if (r->tcpdiag_state == TCP_TIME_WAIT) {
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -070084 const struct inet_timewait_sock *tw = inet_twsk(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 long tmo = tw->tw_ttd - jiffies;
86 if (tmo < 0)
87 tmo = 0;
88
89 r->id.tcpdiag_sport = tw->tw_sport;
90 r->id.tcpdiag_dport = tw->tw_dport;
91 r->id.tcpdiag_src[0] = tw->tw_rcv_saddr;
92 r->id.tcpdiag_dst[0] = tw->tw_daddr;
93 r->tcpdiag_state = tw->tw_substate;
94 r->tcpdiag_timer = 3;
95 r->tcpdiag_expires = (tmo*1000+HZ-1)/HZ;
96 r->tcpdiag_rqueue = 0;
97 r->tcpdiag_wqueue = 0;
98 r->tcpdiag_uid = 0;
99 r->tcpdiag_inode = 0;
100#ifdef CONFIG_IP_TCPDIAG_IPV6
101 if (r->tcpdiag_family == AF_INET6) {
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700102 const struct tcp6_timewait_sock *tcp6tw = tcp6_twsk(sk);
103
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700105 &tcp6tw->tw_v6_rcv_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700107 &tcp6tw->tw_v6_daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 }
109#endif
110 nlh->nlmsg_len = skb->tail - b;
111 return skb->len;
112 }
113
114 r->id.tcpdiag_sport = inet->sport;
115 r->id.tcpdiag_dport = inet->dport;
116 r->id.tcpdiag_src[0] = inet->rcv_saddr;
117 r->id.tcpdiag_dst[0] = inet->daddr;
118
119#ifdef CONFIG_IP_TCPDIAG_IPV6
120 if (r->tcpdiag_family == AF_INET6) {
121 struct ipv6_pinfo *np = inet6_sk(sk);
122
123 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
124 &np->rcv_saddr);
125 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
126 &np->daddr);
127 }
128#endif
129
130#define EXPIRES_IN_MS(tmo) ((tmo-jiffies)*1000+HZ-1)/HZ
131
132 if (tp->pending == TCP_TIME_RETRANS) {
133 r->tcpdiag_timer = 1;
134 r->tcpdiag_retrans = tp->retransmits;
135 r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
136 } else if (tp->pending == TCP_TIME_PROBE0) {
137 r->tcpdiag_timer = 4;
138 r->tcpdiag_retrans = tp->probes_out;
139 r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
140 } else if (timer_pending(&sk->sk_timer)) {
141 r->tcpdiag_timer = 2;
142 r->tcpdiag_retrans = tp->probes_out;
143 r->tcpdiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
144 } else {
145 r->tcpdiag_timer = 0;
146 r->tcpdiag_expires = 0;
147 }
148#undef EXPIRES_IN_MS
149
150 r->tcpdiag_rqueue = tp->rcv_nxt - tp->copied_seq;
151 r->tcpdiag_wqueue = tp->write_seq - tp->snd_una;
152 r->tcpdiag_uid = sock_i_uid(sk);
153 r->tcpdiag_inode = sock_i_ino(sk);
154
155 if (minfo) {
156 minfo->tcpdiag_rmem = atomic_read(&sk->sk_rmem_alloc);
157 minfo->tcpdiag_wmem = sk->sk_wmem_queued;
158 minfo->tcpdiag_fmem = sk->sk_forward_alloc;
159 minfo->tcpdiag_tmem = atomic_read(&sk->sk_wmem_alloc);
160 }
161
162 if (info)
163 tcp_get_info(sk, info);
164
Stephen Hemminger317a76f2005-06-23 12:19:55 -0700165 if (sk->sk_state < TCP_TIME_WAIT && tp->ca_ops->get_info)
166 tp->ca_ops->get_info(tp, ext, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
168 nlh->nlmsg_len = skb->tail - b;
169 return skb->len;
170
Stephen Hemminger7c99c902005-06-23 12:20:36 -0700171rtattr_failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172nlmsg_failure:
173 skb_trim(skb, b - skb->data);
174 return -1;
175}
176
177extern struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport,
178 int dif);
179#ifdef CONFIG_IP_TCPDIAG_IPV6
180extern struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
181 struct in6_addr *daddr, u16 dport,
182 int dif);
183#else
184static inline struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
185 struct in6_addr *daddr, u16 dport,
186 int dif)
187{
188 return NULL;
189}
190#endif
191
192static int tcpdiag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh)
193{
194 int err;
195 struct sock *sk;
196 struct tcpdiagreq *req = NLMSG_DATA(nlh);
197 struct sk_buff *rep;
198
199 if (req->tcpdiag_family == AF_INET) {
200 sk = tcp_v4_lookup(req->id.tcpdiag_dst[0], req->id.tcpdiag_dport,
201 req->id.tcpdiag_src[0], req->id.tcpdiag_sport,
202 req->id.tcpdiag_if);
203 }
204#ifdef CONFIG_IP_TCPDIAG_IPV6
205 else if (req->tcpdiag_family == AF_INET6) {
206 sk = tcp_v6_lookup((struct in6_addr*)req->id.tcpdiag_dst, req->id.tcpdiag_dport,
207 (struct in6_addr*)req->id.tcpdiag_src, req->id.tcpdiag_sport,
208 req->id.tcpdiag_if);
209 }
210#endif
211 else {
212 return -EINVAL;
213 }
214
215 if (sk == NULL)
216 return -ENOENT;
217
218 err = -ESTALE;
219 if ((req->id.tcpdiag_cookie[0] != TCPDIAG_NOCOOKIE ||
220 req->id.tcpdiag_cookie[1] != TCPDIAG_NOCOOKIE) &&
221 ((u32)(unsigned long)sk != req->id.tcpdiag_cookie[0] ||
222 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.tcpdiag_cookie[1]))
223 goto out;
224
225 err = -ENOMEM;
226 rep = alloc_skb(NLMSG_SPACE(sizeof(struct tcpdiagmsg)+
227 sizeof(struct tcpdiag_meminfo)+
228 sizeof(struct tcp_info)+64), GFP_KERNEL);
229 if (!rep)
230 goto out;
231
232 if (tcpdiag_fill(rep, sk, req->tcpdiag_ext,
233 NETLINK_CB(in_skb).pid,
234 nlh->nlmsg_seq, 0) <= 0)
235 BUG();
236
237 err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
238 if (err > 0)
239 err = 0;
240
241out:
242 if (sk) {
243 if (sk->sk_state == TCP_TIME_WAIT)
Arnaldo Carvalho de Melo8feaf0c02005-08-09 20:09:30 -0700244 inet_twsk_put((struct inet_timewait_sock *)sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 else
246 sock_put(sk);
247 }
248 return err;
249}
250
251static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
252{
253 int words = bits >> 5;
254
255 bits &= 0x1f;
256
257 if (words) {
258 if (memcmp(a1, a2, words << 2))
259 return 0;
260 }
261 if (bits) {
262 __u32 w1, w2;
263 __u32 mask;
264
265 w1 = a1[words];
266 w2 = a2[words];
267
268 mask = htonl((0xffffffff) << (32 - bits));
269
270 if ((w1 ^ w2) & mask)
271 return 0;
272 }
273
274 return 1;
275}
276
277
278static int tcpdiag_bc_run(const void *bc, int len,
279 const struct tcpdiag_entry *entry)
280{
281 while (len > 0) {
282 int yes = 1;
283 const struct tcpdiag_bc_op *op = bc;
284
285 switch (op->code) {
286 case TCPDIAG_BC_NOP:
287 break;
288 case TCPDIAG_BC_JMP:
289 yes = 0;
290 break;
291 case TCPDIAG_BC_S_GE:
292 yes = entry->sport >= op[1].no;
293 break;
294 case TCPDIAG_BC_S_LE:
295 yes = entry->dport <= op[1].no;
296 break;
297 case TCPDIAG_BC_D_GE:
298 yes = entry->dport >= op[1].no;
299 break;
300 case TCPDIAG_BC_D_LE:
301 yes = entry->dport <= op[1].no;
302 break;
303 case TCPDIAG_BC_AUTO:
304 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
305 break;
306 case TCPDIAG_BC_S_COND:
307 case TCPDIAG_BC_D_COND:
308 {
309 struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1);
310 u32 *addr;
311
312 if (cond->port != -1 &&
313 cond->port != (op->code == TCPDIAG_BC_S_COND ?
314 entry->sport : entry->dport)) {
315 yes = 0;
316 break;
317 }
318
319 if (cond->prefix_len == 0)
320 break;
321
322 if (op->code == TCPDIAG_BC_S_COND)
323 addr = entry->saddr;
324 else
325 addr = entry->daddr;
326
327 if (bitstring_match(addr, cond->addr, cond->prefix_len))
328 break;
329 if (entry->family == AF_INET6 &&
330 cond->family == AF_INET) {
331 if (addr[0] == 0 && addr[1] == 0 &&
332 addr[2] == htonl(0xffff) &&
333 bitstring_match(addr+3, cond->addr, cond->prefix_len))
334 break;
335 }
336 yes = 0;
337 break;
338 }
339 }
340
341 if (yes) {
342 len -= op->yes;
343 bc += op->yes;
344 } else {
345 len -= op->no;
346 bc += op->no;
347 }
348 }
349 return (len == 0);
350}
351
352static int valid_cc(const void *bc, int len, int cc)
353{
354 while (len >= 0) {
355 const struct tcpdiag_bc_op *op = bc;
356
357 if (cc > len)
358 return 0;
359 if (cc == len)
360 return 1;
361 if (op->yes < 4)
362 return 0;
363 len -= op->yes;
364 bc += op->yes;
365 }
366 return 0;
367}
368
369static int tcpdiag_bc_audit(const void *bytecode, int bytecode_len)
370{
371 const unsigned char *bc = bytecode;
372 int len = bytecode_len;
373
374 while (len > 0) {
375 struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc;
376
377//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
378 switch (op->code) {
379 case TCPDIAG_BC_AUTO:
380 case TCPDIAG_BC_S_COND:
381 case TCPDIAG_BC_D_COND:
382 case TCPDIAG_BC_S_GE:
383 case TCPDIAG_BC_S_LE:
384 case TCPDIAG_BC_D_GE:
385 case TCPDIAG_BC_D_LE:
386 if (op->yes < 4 || op->yes > len+4)
387 return -EINVAL;
388 case TCPDIAG_BC_JMP:
389 if (op->no < 4 || op->no > len+4)
390 return -EINVAL;
391 if (op->no < len &&
392 !valid_cc(bytecode, bytecode_len, len-op->no))
393 return -EINVAL;
394 break;
395 case TCPDIAG_BC_NOP:
396 if (op->yes < 4 || op->yes > len+4)
397 return -EINVAL;
398 break;
399 default:
400 return -EINVAL;
401 }
402 bc += op->yes;
403 len -= op->yes;
404 }
405 return len == 0 ? 0 : -EINVAL;
406}
407
408static int tcpdiag_dump_sock(struct sk_buff *skb, struct sock *sk,
409 struct netlink_callback *cb)
410{
411 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
412
413 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
414 struct tcpdiag_entry entry;
415 struct rtattr *bc = (struct rtattr *)(r + 1);
416 struct inet_sock *inet = inet_sk(sk);
417
418 entry.family = sk->sk_family;
419#ifdef CONFIG_IP_TCPDIAG_IPV6
420 if (entry.family == AF_INET6) {
421 struct ipv6_pinfo *np = inet6_sk(sk);
422
423 entry.saddr = np->rcv_saddr.s6_addr32;
424 entry.daddr = np->daddr.s6_addr32;
425 } else
426#endif
427 {
428 entry.saddr = &inet->rcv_saddr;
429 entry.daddr = &inet->daddr;
430 }
431 entry.sport = inet->num;
432 entry.dport = ntohs(inet->dport);
433 entry.userlocks = sk->sk_userlocks;
434
435 if (!tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
436 return 0;
437 }
438
439 return tcpdiag_fill(skb, sk, r->tcpdiag_ext, NETLINK_CB(cb->skb).pid,
440 cb->nlh->nlmsg_seq, NLM_F_MULTI);
441}
442
443static int tcpdiag_fill_req(struct sk_buff *skb, struct sock *sk,
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700444 struct request_sock *req,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 u32 pid, u32 seq)
446{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700447 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 struct inet_sock *inet = inet_sk(sk);
449 unsigned char *b = skb->tail;
450 struct tcpdiagmsg *r;
451 struct nlmsghdr *nlh;
452 long tmo;
453
454 nlh = NLMSG_PUT(skb, pid, seq, TCPDIAG_GETSOCK, sizeof(*r));
455 nlh->nlmsg_flags = NLM_F_MULTI;
456 r = NLMSG_DATA(nlh);
457
458 r->tcpdiag_family = sk->sk_family;
459 r->tcpdiag_state = TCP_SYN_RECV;
460 r->tcpdiag_timer = 1;
461 r->tcpdiag_retrans = req->retrans;
462
463 r->id.tcpdiag_if = sk->sk_bound_dev_if;
464 r->id.tcpdiag_cookie[0] = (u32)(unsigned long)req;
465 r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
466
467 tmo = req->expires - jiffies;
468 if (tmo < 0)
469 tmo = 0;
470
471 r->id.tcpdiag_sport = inet->sport;
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700472 r->id.tcpdiag_dport = ireq->rmt_port;
473 r->id.tcpdiag_src[0] = ireq->loc_addr;
474 r->id.tcpdiag_dst[0] = ireq->rmt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 r->tcpdiag_expires = jiffies_to_msecs(tmo),
476 r->tcpdiag_rqueue = 0;
477 r->tcpdiag_wqueue = 0;
478 r->tcpdiag_uid = sock_i_uid(sk);
479 r->tcpdiag_inode = 0;
480#ifdef CONFIG_IP_TCPDIAG_IPV6
481 if (r->tcpdiag_family == AF_INET6) {
482 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700483 &tcp6_rsk(req)->loc_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700485 &tcp6_rsk(req)->rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487#endif
488 nlh->nlmsg_len = skb->tail - b;
489
490 return skb->len;
491
492nlmsg_failure:
493 skb_trim(skb, b - skb->data);
494 return -1;
495}
496
497static int tcpdiag_dump_reqs(struct sk_buff *skb, struct sock *sk,
498 struct netlink_callback *cb)
499{
500 struct tcpdiag_entry entry;
501 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
502 struct tcp_sock *tp = tcp_sk(sk);
Arnaldo Carvalho de Melo2ad69c52005-06-18 22:48:55 -0700503 struct listen_sock *lopt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 struct rtattr *bc = NULL;
505 struct inet_sock *inet = inet_sk(sk);
506 int j, s_j;
507 int reqnum, s_reqnum;
508 int err = 0;
509
510 s_j = cb->args[3];
511 s_reqnum = cb->args[4];
512
513 if (s_j > 0)
514 s_j--;
515
516 entry.family = sk->sk_family;
517
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -0700518 read_lock_bh(&tp->accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -0700520 lopt = tp->accept_queue.listen_opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (!lopt || !lopt->qlen)
522 goto out;
523
524 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
525 bc = (struct rtattr *)(r + 1);
526 entry.sport = inet->num;
527 entry.userlocks = sk->sk_userlocks;
528 }
529
530 for (j = s_j; j < TCP_SYNQ_HSIZE; j++) {
Arnaldo Carvalho de Melo60236fd2005-06-18 22:47:21 -0700531 struct request_sock *req, *head = lopt->syn_table[j];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 reqnum = 0;
534 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700535 struct inet_request_sock *ireq = inet_rsk(req);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 if (reqnum < s_reqnum)
538 continue;
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700539 if (r->id.tcpdiag_dport != ireq->rmt_port &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 r->id.tcpdiag_dport)
541 continue;
542
543 if (bc) {
544 entry.saddr =
545#ifdef CONFIG_IP_TCPDIAG_IPV6
546 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700547 tcp6_rsk(req)->loc_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700549 &ireq->loc_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 entry.daddr =
551#ifdef CONFIG_IP_TCPDIAG_IPV6
552 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700553 tcp6_rsk(req)->rmt_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700555 &ireq->rmt_addr;
556 entry.dport = ntohs(ireq->rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
558 if (!tcpdiag_bc_run(RTA_DATA(bc),
559 RTA_PAYLOAD(bc), &entry))
560 continue;
561 }
562
563 err = tcpdiag_fill_req(skb, sk, req,
564 NETLINK_CB(cb->skb).pid,
565 cb->nlh->nlmsg_seq);
566 if (err < 0) {
567 cb->args[3] = j + 1;
568 cb->args[4] = reqnum;
569 goto out;
570 }
571 }
572
573 s_reqnum = 0;
574 }
575
576out:
Arnaldo Carvalho de Melo0e875062005-06-18 22:47:59 -0700577 read_unlock_bh(&tp->accept_queue.syn_wait_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 return err;
580}
581
582static int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb)
583{
584 int i, num;
585 int s_i, s_num;
586 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
587
588 s_i = cb->args[1];
589 s_num = num = cb->args[2];
590
591 if (cb->args[0] == 0) {
592 if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV)))
593 goto skip_listen_ht;
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -0700594 inet_listen_lock(&tcp_hashinfo);
Arnaldo Carvalho de Melo0f7ff922005-08-09 19:59:44 -0700595 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 struct sock *sk;
597 struct hlist_node *node;
598
599 num = 0;
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -0700600 sk_for_each(sk, node, &tcp_hashinfo.listening_hash[i]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 struct inet_sock *inet = inet_sk(sk);
602
603 if (num < s_num) {
604 num++;
605 continue;
606 }
607
608 if (r->id.tcpdiag_sport != inet->sport &&
609 r->id.tcpdiag_sport)
610 goto next_listen;
611
612 if (!(r->tcpdiag_states&TCPF_LISTEN) ||
613 r->id.tcpdiag_dport ||
614 cb->args[3] > 0)
615 goto syn_recv;
616
617 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -0700618 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 goto done;
620 }
621
622syn_recv:
623 if (!(r->tcpdiag_states&TCPF_SYN_RECV))
624 goto next_listen;
625
626 if (tcpdiag_dump_reqs(skb, sk, cb) < 0) {
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -0700627 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 goto done;
629 }
630
631next_listen:
632 cb->args[3] = 0;
633 cb->args[4] = 0;
634 ++num;
635 }
636
637 s_num = 0;
638 cb->args[3] = 0;
639 cb->args[4] = 0;
640 }
Arnaldo Carvalho de Melof3f05f72005-08-09 20:08:09 -0700641 inet_listen_unlock(&tcp_hashinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642skip_listen_ht:
643 cb->args[0] = 1;
644 s_i = num = s_num = 0;
645 }
646
647 if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV)))
648 return skb->len;
649
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -0700650 for (i = s_i; i < tcp_hashinfo.ehash_size; i++) {
651 struct inet_ehash_bucket *head = &tcp_hashinfo.ehash[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 struct sock *sk;
653 struct hlist_node *node;
654
655 if (i > s_i)
656 s_num = 0;
657
658 read_lock_bh(&head->lock);
659
660 num = 0;
661 sk_for_each(sk, node, &head->chain) {
662 struct inet_sock *inet = inet_sk(sk);
663
664 if (num < s_num)
665 goto next_normal;
666 if (!(r->tcpdiag_states & (1 << sk->sk_state)))
667 goto next_normal;
668 if (r->id.tcpdiag_sport != inet->sport &&
669 r->id.tcpdiag_sport)
670 goto next_normal;
671 if (r->id.tcpdiag_dport != inet->dport && r->id.tcpdiag_dport)
672 goto next_normal;
673 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
674 read_unlock_bh(&head->lock);
675 goto done;
676 }
677next_normal:
678 ++num;
679 }
680
681 if (r->tcpdiag_states&TCPF_TIME_WAIT) {
682 sk_for_each(sk, node,
Arnaldo Carvalho de Melo6e04e022005-08-09 20:07:35 -0700683 &tcp_hashinfo.ehash[i + tcp_hashinfo.ehash_size].chain) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 struct inet_sock *inet = inet_sk(sk);
685
686 if (num < s_num)
687 goto next_dying;
688 if (r->id.tcpdiag_sport != inet->sport &&
689 r->id.tcpdiag_sport)
690 goto next_dying;
691 if (r->id.tcpdiag_dport != inet->dport &&
692 r->id.tcpdiag_dport)
693 goto next_dying;
694 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
695 read_unlock_bh(&head->lock);
696 goto done;
697 }
698next_dying:
699 ++num;
700 }
701 }
702 read_unlock_bh(&head->lock);
703 }
704
705done:
706 cb->args[1] = i;
707 cb->args[2] = num;
708 return skb->len;
709}
710
711static int tcpdiag_dump_done(struct netlink_callback *cb)
712{
713 return 0;
714}
715
716
717static __inline__ int
718tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
719{
720 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
721 return 0;
722
723 if (nlh->nlmsg_type != TCPDIAG_GETSOCK)
724 goto err_inval;
725
726 if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len)
727 goto err_inval;
728
729 if (nlh->nlmsg_flags&NLM_F_DUMP) {
730 if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) {
731 struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq));
732 if (rta->rta_type != TCPDIAG_REQ_BYTECODE ||
733 rta->rta_len < 8 ||
734 rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq)))
735 goto err_inval;
736 if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
737 goto err_inval;
738 }
739 return netlink_dump_start(tcpnl, skb, nlh,
740 tcpdiag_dump,
741 tcpdiag_dump_done);
742 } else {
743 return tcpdiag_get_exact(skb, nlh);
744 }
745
746err_inval:
747 return -EINVAL;
748}
749
750
751static inline void tcpdiag_rcv_skb(struct sk_buff *skb)
752{
753 int err;
754 struct nlmsghdr * nlh;
755
756 if (skb->len >= NLMSG_SPACE(0)) {
757 nlh = (struct nlmsghdr *)skb->data;
758 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
759 return;
760 err = tcpdiag_rcv_msg(skb, nlh);
761 if (err || nlh->nlmsg_flags & NLM_F_ACK)
762 netlink_ack(skb, nlh, err);
763 }
764}
765
766static void tcpdiag_rcv(struct sock *sk, int len)
767{
768 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700769 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700771 while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 tcpdiag_rcv_skb(skb);
773 kfree_skb(skb);
774 }
775}
776
777static int __init tcpdiag_init(void)
778{
Harald Welte4fdb3bb2005-08-09 19:40:55 -0700779 tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv,
780 THIS_MODULE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 if (tcpnl == NULL)
782 return -ENOMEM;
783 return 0;
784}
785
786static void __exit tcpdiag_exit(void)
787{
788 sock_release(tcpnl->sk_socket);
789}
790
791module_init(tcpdiag_init);
792module_exit(tcpdiag_exit);
793MODULE_LICENSE("GPL");