blob: 700ff2413588d41c71836026ae3298b06cb36d73 [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
45
46#define TCPDIAG_PUT(skb, attrtype, attrlen) \
47({ int rtalen = RTA_LENGTH(attrlen); \
48 struct rtattr *rta; \
49 if (skb_tailroom(skb) < RTA_ALIGN(rtalen)) goto nlmsg_failure; \
50 rta = (void*)__skb_put(skb, RTA_ALIGN(rtalen)); \
51 rta->rta_type = attrtype; \
52 rta->rta_len = rtalen; \
53 RTA_DATA(rta); })
54
55static int tcpdiag_fill(struct sk_buff *skb, struct sock *sk,
56 int ext, u32 pid, u32 seq, u16 nlmsg_flags)
57{
58 struct inet_sock *inet = inet_sk(sk);
59 struct tcp_sock *tp = tcp_sk(sk);
60 struct tcpdiagmsg *r;
61 struct nlmsghdr *nlh;
62 struct tcp_info *info = NULL;
63 struct tcpdiag_meminfo *minfo = NULL;
64 struct tcpvegas_info *vinfo = NULL;
65 unsigned char *b = skb->tail;
66
67 nlh = NLMSG_PUT(skb, pid, seq, TCPDIAG_GETSOCK, sizeof(*r));
68 nlh->nlmsg_flags = nlmsg_flags;
69 r = NLMSG_DATA(nlh);
70 if (sk->sk_state != TCP_TIME_WAIT) {
71 if (ext & (1<<(TCPDIAG_MEMINFO-1)))
72 minfo = TCPDIAG_PUT(skb, TCPDIAG_MEMINFO, sizeof(*minfo));
73 if (ext & (1<<(TCPDIAG_INFO-1)))
74 info = TCPDIAG_PUT(skb, TCPDIAG_INFO, sizeof(*info));
75
76 if ((tcp_is_westwood(tp) || tcp_is_vegas(tp))
77 && (ext & (1<<(TCPDIAG_VEGASINFO-1))))
78 vinfo = TCPDIAG_PUT(skb, TCPDIAG_VEGASINFO, sizeof(*vinfo));
79 }
80 r->tcpdiag_family = sk->sk_family;
81 r->tcpdiag_state = sk->sk_state;
82 r->tcpdiag_timer = 0;
83 r->tcpdiag_retrans = 0;
84
85 r->id.tcpdiag_if = sk->sk_bound_dev_if;
86 r->id.tcpdiag_cookie[0] = (u32)(unsigned long)sk;
87 r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
88
89 if (r->tcpdiag_state == TCP_TIME_WAIT) {
90 struct tcp_tw_bucket *tw = (struct tcp_tw_bucket*)sk;
91 long tmo = tw->tw_ttd - jiffies;
92 if (tmo < 0)
93 tmo = 0;
94
95 r->id.tcpdiag_sport = tw->tw_sport;
96 r->id.tcpdiag_dport = tw->tw_dport;
97 r->id.tcpdiag_src[0] = tw->tw_rcv_saddr;
98 r->id.tcpdiag_dst[0] = tw->tw_daddr;
99 r->tcpdiag_state = tw->tw_substate;
100 r->tcpdiag_timer = 3;
101 r->tcpdiag_expires = (tmo*1000+HZ-1)/HZ;
102 r->tcpdiag_rqueue = 0;
103 r->tcpdiag_wqueue = 0;
104 r->tcpdiag_uid = 0;
105 r->tcpdiag_inode = 0;
106#ifdef CONFIG_IP_TCPDIAG_IPV6
107 if (r->tcpdiag_family == AF_INET6) {
108 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
109 &tw->tw_v6_rcv_saddr);
110 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
111 &tw->tw_v6_daddr);
112 }
113#endif
114 nlh->nlmsg_len = skb->tail - b;
115 return skb->len;
116 }
117
118 r->id.tcpdiag_sport = inet->sport;
119 r->id.tcpdiag_dport = inet->dport;
120 r->id.tcpdiag_src[0] = inet->rcv_saddr;
121 r->id.tcpdiag_dst[0] = inet->daddr;
122
123#ifdef CONFIG_IP_TCPDIAG_IPV6
124 if (r->tcpdiag_family == AF_INET6) {
125 struct ipv6_pinfo *np = inet6_sk(sk);
126
127 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
128 &np->rcv_saddr);
129 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
130 &np->daddr);
131 }
132#endif
133
134#define EXPIRES_IN_MS(tmo) ((tmo-jiffies)*1000+HZ-1)/HZ
135
136 if (tp->pending == TCP_TIME_RETRANS) {
137 r->tcpdiag_timer = 1;
138 r->tcpdiag_retrans = tp->retransmits;
139 r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
140 } else if (tp->pending == TCP_TIME_PROBE0) {
141 r->tcpdiag_timer = 4;
142 r->tcpdiag_retrans = tp->probes_out;
143 r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
144 } else if (timer_pending(&sk->sk_timer)) {
145 r->tcpdiag_timer = 2;
146 r->tcpdiag_retrans = tp->probes_out;
147 r->tcpdiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
148 } else {
149 r->tcpdiag_timer = 0;
150 r->tcpdiag_expires = 0;
151 }
152#undef EXPIRES_IN_MS
153
154 r->tcpdiag_rqueue = tp->rcv_nxt - tp->copied_seq;
155 r->tcpdiag_wqueue = tp->write_seq - tp->snd_una;
156 r->tcpdiag_uid = sock_i_uid(sk);
157 r->tcpdiag_inode = sock_i_ino(sk);
158
159 if (minfo) {
160 minfo->tcpdiag_rmem = atomic_read(&sk->sk_rmem_alloc);
161 minfo->tcpdiag_wmem = sk->sk_wmem_queued;
162 minfo->tcpdiag_fmem = sk->sk_forward_alloc;
163 minfo->tcpdiag_tmem = atomic_read(&sk->sk_wmem_alloc);
164 }
165
166 if (info)
167 tcp_get_info(sk, info);
168
169 if (vinfo) {
170 if (tcp_is_vegas(tp)) {
171 vinfo->tcpv_enabled = tp->vegas.doing_vegas_now;
172 vinfo->tcpv_rttcnt = tp->vegas.cntRTT;
173 vinfo->tcpv_rtt = jiffies_to_usecs(tp->vegas.baseRTT);
174 vinfo->tcpv_minrtt = jiffies_to_usecs(tp->vegas.minRTT);
175 } else {
176 vinfo->tcpv_enabled = 0;
177 vinfo->tcpv_rttcnt = 0;
178 vinfo->tcpv_rtt = jiffies_to_usecs(tp->westwood.rtt);
179 vinfo->tcpv_minrtt = jiffies_to_usecs(tp->westwood.rtt_min);
180 }
181 }
182
183 nlh->nlmsg_len = skb->tail - b;
184 return skb->len;
185
186nlmsg_failure:
187 skb_trim(skb, b - skb->data);
188 return -1;
189}
190
191extern struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport,
192 int dif);
193#ifdef CONFIG_IP_TCPDIAG_IPV6
194extern struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
195 struct in6_addr *daddr, u16 dport,
196 int dif);
197#else
198static inline struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
199 struct in6_addr *daddr, u16 dport,
200 int dif)
201{
202 return NULL;
203}
204#endif
205
206static int tcpdiag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh)
207{
208 int err;
209 struct sock *sk;
210 struct tcpdiagreq *req = NLMSG_DATA(nlh);
211 struct sk_buff *rep;
212
213 if (req->tcpdiag_family == AF_INET) {
214 sk = tcp_v4_lookup(req->id.tcpdiag_dst[0], req->id.tcpdiag_dport,
215 req->id.tcpdiag_src[0], req->id.tcpdiag_sport,
216 req->id.tcpdiag_if);
217 }
218#ifdef CONFIG_IP_TCPDIAG_IPV6
219 else if (req->tcpdiag_family == AF_INET6) {
220 sk = tcp_v6_lookup((struct in6_addr*)req->id.tcpdiag_dst, req->id.tcpdiag_dport,
221 (struct in6_addr*)req->id.tcpdiag_src, req->id.tcpdiag_sport,
222 req->id.tcpdiag_if);
223 }
224#endif
225 else {
226 return -EINVAL;
227 }
228
229 if (sk == NULL)
230 return -ENOENT;
231
232 err = -ESTALE;
233 if ((req->id.tcpdiag_cookie[0] != TCPDIAG_NOCOOKIE ||
234 req->id.tcpdiag_cookie[1] != TCPDIAG_NOCOOKIE) &&
235 ((u32)(unsigned long)sk != req->id.tcpdiag_cookie[0] ||
236 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.tcpdiag_cookie[1]))
237 goto out;
238
239 err = -ENOMEM;
240 rep = alloc_skb(NLMSG_SPACE(sizeof(struct tcpdiagmsg)+
241 sizeof(struct tcpdiag_meminfo)+
242 sizeof(struct tcp_info)+64), GFP_KERNEL);
243 if (!rep)
244 goto out;
245
246 if (tcpdiag_fill(rep, sk, req->tcpdiag_ext,
247 NETLINK_CB(in_skb).pid,
248 nlh->nlmsg_seq, 0) <= 0)
249 BUG();
250
251 err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
252 if (err > 0)
253 err = 0;
254
255out:
256 if (sk) {
257 if (sk->sk_state == TCP_TIME_WAIT)
258 tcp_tw_put((struct tcp_tw_bucket*)sk);
259 else
260 sock_put(sk);
261 }
262 return err;
263}
264
265static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
266{
267 int words = bits >> 5;
268
269 bits &= 0x1f;
270
271 if (words) {
272 if (memcmp(a1, a2, words << 2))
273 return 0;
274 }
275 if (bits) {
276 __u32 w1, w2;
277 __u32 mask;
278
279 w1 = a1[words];
280 w2 = a2[words];
281
282 mask = htonl((0xffffffff) << (32 - bits));
283
284 if ((w1 ^ w2) & mask)
285 return 0;
286 }
287
288 return 1;
289}
290
291
292static int tcpdiag_bc_run(const void *bc, int len,
293 const struct tcpdiag_entry *entry)
294{
295 while (len > 0) {
296 int yes = 1;
297 const struct tcpdiag_bc_op *op = bc;
298
299 switch (op->code) {
300 case TCPDIAG_BC_NOP:
301 break;
302 case TCPDIAG_BC_JMP:
303 yes = 0;
304 break;
305 case TCPDIAG_BC_S_GE:
306 yes = entry->sport >= op[1].no;
307 break;
308 case TCPDIAG_BC_S_LE:
309 yes = entry->dport <= op[1].no;
310 break;
311 case TCPDIAG_BC_D_GE:
312 yes = entry->dport >= op[1].no;
313 break;
314 case TCPDIAG_BC_D_LE:
315 yes = entry->dport <= op[1].no;
316 break;
317 case TCPDIAG_BC_AUTO:
318 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
319 break;
320 case TCPDIAG_BC_S_COND:
321 case TCPDIAG_BC_D_COND:
322 {
323 struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1);
324 u32 *addr;
325
326 if (cond->port != -1 &&
327 cond->port != (op->code == TCPDIAG_BC_S_COND ?
328 entry->sport : entry->dport)) {
329 yes = 0;
330 break;
331 }
332
333 if (cond->prefix_len == 0)
334 break;
335
336 if (op->code == TCPDIAG_BC_S_COND)
337 addr = entry->saddr;
338 else
339 addr = entry->daddr;
340
341 if (bitstring_match(addr, cond->addr, cond->prefix_len))
342 break;
343 if (entry->family == AF_INET6 &&
344 cond->family == AF_INET) {
345 if (addr[0] == 0 && addr[1] == 0 &&
346 addr[2] == htonl(0xffff) &&
347 bitstring_match(addr+3, cond->addr, cond->prefix_len))
348 break;
349 }
350 yes = 0;
351 break;
352 }
353 }
354
355 if (yes) {
356 len -= op->yes;
357 bc += op->yes;
358 } else {
359 len -= op->no;
360 bc += op->no;
361 }
362 }
363 return (len == 0);
364}
365
366static int valid_cc(const void *bc, int len, int cc)
367{
368 while (len >= 0) {
369 const struct tcpdiag_bc_op *op = bc;
370
371 if (cc > len)
372 return 0;
373 if (cc == len)
374 return 1;
375 if (op->yes < 4)
376 return 0;
377 len -= op->yes;
378 bc += op->yes;
379 }
380 return 0;
381}
382
383static int tcpdiag_bc_audit(const void *bytecode, int bytecode_len)
384{
385 const unsigned char *bc = bytecode;
386 int len = bytecode_len;
387
388 while (len > 0) {
389 struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc;
390
391//printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
392 switch (op->code) {
393 case TCPDIAG_BC_AUTO:
394 case TCPDIAG_BC_S_COND:
395 case TCPDIAG_BC_D_COND:
396 case TCPDIAG_BC_S_GE:
397 case TCPDIAG_BC_S_LE:
398 case TCPDIAG_BC_D_GE:
399 case TCPDIAG_BC_D_LE:
400 if (op->yes < 4 || op->yes > len+4)
401 return -EINVAL;
402 case TCPDIAG_BC_JMP:
403 if (op->no < 4 || op->no > len+4)
404 return -EINVAL;
405 if (op->no < len &&
406 !valid_cc(bytecode, bytecode_len, len-op->no))
407 return -EINVAL;
408 break;
409 case TCPDIAG_BC_NOP:
410 if (op->yes < 4 || op->yes > len+4)
411 return -EINVAL;
412 break;
413 default:
414 return -EINVAL;
415 }
416 bc += op->yes;
417 len -= op->yes;
418 }
419 return len == 0 ? 0 : -EINVAL;
420}
421
422static int tcpdiag_dump_sock(struct sk_buff *skb, struct sock *sk,
423 struct netlink_callback *cb)
424{
425 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
426
427 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
428 struct tcpdiag_entry entry;
429 struct rtattr *bc = (struct rtattr *)(r + 1);
430 struct inet_sock *inet = inet_sk(sk);
431
432 entry.family = sk->sk_family;
433#ifdef CONFIG_IP_TCPDIAG_IPV6
434 if (entry.family == AF_INET6) {
435 struct ipv6_pinfo *np = inet6_sk(sk);
436
437 entry.saddr = np->rcv_saddr.s6_addr32;
438 entry.daddr = np->daddr.s6_addr32;
439 } else
440#endif
441 {
442 entry.saddr = &inet->rcv_saddr;
443 entry.daddr = &inet->daddr;
444 }
445 entry.sport = inet->num;
446 entry.dport = ntohs(inet->dport);
447 entry.userlocks = sk->sk_userlocks;
448
449 if (!tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
450 return 0;
451 }
452
453 return tcpdiag_fill(skb, sk, r->tcpdiag_ext, NETLINK_CB(cb->skb).pid,
454 cb->nlh->nlmsg_seq, NLM_F_MULTI);
455}
456
457static int tcpdiag_fill_req(struct sk_buff *skb, struct sock *sk,
458 struct open_request *req,
459 u32 pid, u32 seq)
460{
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700461 const struct inet_request_sock *ireq = inet_rsk(req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 struct inet_sock *inet = inet_sk(sk);
463 unsigned char *b = skb->tail;
464 struct tcpdiagmsg *r;
465 struct nlmsghdr *nlh;
466 long tmo;
467
468 nlh = NLMSG_PUT(skb, pid, seq, TCPDIAG_GETSOCK, sizeof(*r));
469 nlh->nlmsg_flags = NLM_F_MULTI;
470 r = NLMSG_DATA(nlh);
471
472 r->tcpdiag_family = sk->sk_family;
473 r->tcpdiag_state = TCP_SYN_RECV;
474 r->tcpdiag_timer = 1;
475 r->tcpdiag_retrans = req->retrans;
476
477 r->id.tcpdiag_if = sk->sk_bound_dev_if;
478 r->id.tcpdiag_cookie[0] = (u32)(unsigned long)req;
479 r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
480
481 tmo = req->expires - jiffies;
482 if (tmo < 0)
483 tmo = 0;
484
485 r->id.tcpdiag_sport = inet->sport;
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700486 r->id.tcpdiag_dport = ireq->rmt_port;
487 r->id.tcpdiag_src[0] = ireq->loc_addr;
488 r->id.tcpdiag_dst[0] = ireq->rmt_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 r->tcpdiag_expires = jiffies_to_msecs(tmo),
490 r->tcpdiag_rqueue = 0;
491 r->tcpdiag_wqueue = 0;
492 r->tcpdiag_uid = sock_i_uid(sk);
493 r->tcpdiag_inode = 0;
494#ifdef CONFIG_IP_TCPDIAG_IPV6
495 if (r->tcpdiag_family == AF_INET6) {
496 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700497 &tcp6_rsk(req)->loc_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700499 &tcp6_rsk(req)->rmt_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 }
501#endif
502 nlh->nlmsg_len = skb->tail - b;
503
504 return skb->len;
505
506nlmsg_failure:
507 skb_trim(skb, b - skb->data);
508 return -1;
509}
510
511static int tcpdiag_dump_reqs(struct sk_buff *skb, struct sock *sk,
512 struct netlink_callback *cb)
513{
514 struct tcpdiag_entry entry;
515 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
516 struct tcp_sock *tp = tcp_sk(sk);
517 struct tcp_listen_opt *lopt;
518 struct rtattr *bc = NULL;
519 struct inet_sock *inet = inet_sk(sk);
520 int j, s_j;
521 int reqnum, s_reqnum;
522 int err = 0;
523
524 s_j = cb->args[3];
525 s_reqnum = cb->args[4];
526
527 if (s_j > 0)
528 s_j--;
529
530 entry.family = sk->sk_family;
531
532 read_lock_bh(&tp->syn_wait_lock);
533
534 lopt = tp->listen_opt;
535 if (!lopt || !lopt->qlen)
536 goto out;
537
538 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
539 bc = (struct rtattr *)(r + 1);
540 entry.sport = inet->num;
541 entry.userlocks = sk->sk_userlocks;
542 }
543
544 for (j = s_j; j < TCP_SYNQ_HSIZE; j++) {
545 struct open_request *req, *head = lopt->syn_table[j];
546
547 reqnum = 0;
548 for (req = head; req; reqnum++, req = req->dl_next) {
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700549 struct inet_request_sock *ireq = inet_rsk(req);
550
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 if (reqnum < s_reqnum)
552 continue;
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700553 if (r->id.tcpdiag_dport != ireq->rmt_port &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 r->id.tcpdiag_dport)
555 continue;
556
557 if (bc) {
558 entry.saddr =
559#ifdef CONFIG_IP_TCPDIAG_IPV6
560 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700561 tcp6_rsk(req)->loc_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700563 &ireq->loc_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 entry.daddr =
565#ifdef CONFIG_IP_TCPDIAG_IPV6
566 (entry.family == AF_INET6) ?
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700567 tcp6_rsk(req)->rmt_addr.s6_addr32 :
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568#endif
Arnaldo Carvalho de Melo2e6599c2005-06-18 22:46:52 -0700569 &ireq->rmt_addr;
570 entry.dport = ntohs(ireq->rmt_port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 if (!tcpdiag_bc_run(RTA_DATA(bc),
573 RTA_PAYLOAD(bc), &entry))
574 continue;
575 }
576
577 err = tcpdiag_fill_req(skb, sk, req,
578 NETLINK_CB(cb->skb).pid,
579 cb->nlh->nlmsg_seq);
580 if (err < 0) {
581 cb->args[3] = j + 1;
582 cb->args[4] = reqnum;
583 goto out;
584 }
585 }
586
587 s_reqnum = 0;
588 }
589
590out:
591 read_unlock_bh(&tp->syn_wait_lock);
592
593 return err;
594}
595
596static int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb)
597{
598 int i, num;
599 int s_i, s_num;
600 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
601
602 s_i = cb->args[1];
603 s_num = num = cb->args[2];
604
605 if (cb->args[0] == 0) {
606 if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV)))
607 goto skip_listen_ht;
608 tcp_listen_lock();
609 for (i = s_i; i < TCP_LHTABLE_SIZE; i++) {
610 struct sock *sk;
611 struct hlist_node *node;
612
613 num = 0;
614 sk_for_each(sk, node, &tcp_listening_hash[i]) {
615 struct inet_sock *inet = inet_sk(sk);
616
617 if (num < s_num) {
618 num++;
619 continue;
620 }
621
622 if (r->id.tcpdiag_sport != inet->sport &&
623 r->id.tcpdiag_sport)
624 goto next_listen;
625
626 if (!(r->tcpdiag_states&TCPF_LISTEN) ||
627 r->id.tcpdiag_dport ||
628 cb->args[3] > 0)
629 goto syn_recv;
630
631 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
632 tcp_listen_unlock();
633 goto done;
634 }
635
636syn_recv:
637 if (!(r->tcpdiag_states&TCPF_SYN_RECV))
638 goto next_listen;
639
640 if (tcpdiag_dump_reqs(skb, sk, cb) < 0) {
641 tcp_listen_unlock();
642 goto done;
643 }
644
645next_listen:
646 cb->args[3] = 0;
647 cb->args[4] = 0;
648 ++num;
649 }
650
651 s_num = 0;
652 cb->args[3] = 0;
653 cb->args[4] = 0;
654 }
655 tcp_listen_unlock();
656skip_listen_ht:
657 cb->args[0] = 1;
658 s_i = num = s_num = 0;
659 }
660
661 if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV)))
662 return skb->len;
663
664 for (i = s_i; i < tcp_ehash_size; i++) {
665 struct tcp_ehash_bucket *head = &tcp_ehash[i];
666 struct sock *sk;
667 struct hlist_node *node;
668
669 if (i > s_i)
670 s_num = 0;
671
672 read_lock_bh(&head->lock);
673
674 num = 0;
675 sk_for_each(sk, node, &head->chain) {
676 struct inet_sock *inet = inet_sk(sk);
677
678 if (num < s_num)
679 goto next_normal;
680 if (!(r->tcpdiag_states & (1 << sk->sk_state)))
681 goto next_normal;
682 if (r->id.tcpdiag_sport != inet->sport &&
683 r->id.tcpdiag_sport)
684 goto next_normal;
685 if (r->id.tcpdiag_dport != inet->dport && r->id.tcpdiag_dport)
686 goto next_normal;
687 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
688 read_unlock_bh(&head->lock);
689 goto done;
690 }
691next_normal:
692 ++num;
693 }
694
695 if (r->tcpdiag_states&TCPF_TIME_WAIT) {
696 sk_for_each(sk, node,
697 &tcp_ehash[i + tcp_ehash_size].chain) {
698 struct inet_sock *inet = inet_sk(sk);
699
700 if (num < s_num)
701 goto next_dying;
702 if (r->id.tcpdiag_sport != inet->sport &&
703 r->id.tcpdiag_sport)
704 goto next_dying;
705 if (r->id.tcpdiag_dport != inet->dport &&
706 r->id.tcpdiag_dport)
707 goto next_dying;
708 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
709 read_unlock_bh(&head->lock);
710 goto done;
711 }
712next_dying:
713 ++num;
714 }
715 }
716 read_unlock_bh(&head->lock);
717 }
718
719done:
720 cb->args[1] = i;
721 cb->args[2] = num;
722 return skb->len;
723}
724
725static int tcpdiag_dump_done(struct netlink_callback *cb)
726{
727 return 0;
728}
729
730
731static __inline__ int
732tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
733{
734 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
735 return 0;
736
737 if (nlh->nlmsg_type != TCPDIAG_GETSOCK)
738 goto err_inval;
739
740 if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len)
741 goto err_inval;
742
743 if (nlh->nlmsg_flags&NLM_F_DUMP) {
744 if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) {
745 struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq));
746 if (rta->rta_type != TCPDIAG_REQ_BYTECODE ||
747 rta->rta_len < 8 ||
748 rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq)))
749 goto err_inval;
750 if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
751 goto err_inval;
752 }
753 return netlink_dump_start(tcpnl, skb, nlh,
754 tcpdiag_dump,
755 tcpdiag_dump_done);
756 } else {
757 return tcpdiag_get_exact(skb, nlh);
758 }
759
760err_inval:
761 return -EINVAL;
762}
763
764
765static inline void tcpdiag_rcv_skb(struct sk_buff *skb)
766{
767 int err;
768 struct nlmsghdr * nlh;
769
770 if (skb->len >= NLMSG_SPACE(0)) {
771 nlh = (struct nlmsghdr *)skb->data;
772 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
773 return;
774 err = tcpdiag_rcv_msg(skb, nlh);
775 if (err || nlh->nlmsg_flags & NLM_F_ACK)
776 netlink_ack(skb, nlh, err);
777 }
778}
779
780static void tcpdiag_rcv(struct sock *sk, int len)
781{
782 struct sk_buff *skb;
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700783 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Herbert Xu2a0a6eb2005-05-03 14:55:09 -0700785 while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 tcpdiag_rcv_skb(skb);
787 kfree_skb(skb);
788 }
789}
790
791static int __init tcpdiag_init(void)
792{
793 tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv);
794 if (tcpnl == NULL)
795 return -ENOMEM;
796 return 0;
797}
798
799static void __exit tcpdiag_exit(void)
800{
801 sock_release(tcpnl->sk_socket);
802}
803
804module_init(tcpdiag_init);
805module_exit(tcpdiag_exit);
806MODULE_LICENSE("GPL");