[TG3]: Add tagged status support.
[linux-2.6/verdex.git] / net / ipv4 / tcp_diag.c
blob8faa8948f75c2d67db9643dc07b24af7abc609a5
1 /*
2 * tcp_diag.c Module for monitoring TCP sockets.
4 * Version: $Id: tcp_diag.c,v 1.3 2002/02/01 22:01:04 davem Exp $
6 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
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>
23 #include <net/icmp.h>
24 #include <net/tcp.h>
25 #include <net/ipv6.h>
26 #include <net/inet_common.h>
28 #include <linux/inet.h>
29 #include <linux/stddef.h>
31 #include <linux/tcp_diag.h>
33 struct tcpdiag_entry
35 u32 *saddr;
36 u32 *daddr;
37 u16 sport;
38 u16 dport;
39 u16 family;
40 u16 userlocks;
43 static struct sock *tcpnl;
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); })
55 static int tcpdiag_fill(struct sk_buff *skb, struct sock *sk,
56 int ext, u32 pid, u32 seq, u16 nlmsg_flags)
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;
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));
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));
80 r->tcpdiag_family = sk->sk_family;
81 r->tcpdiag_state = sk->sk_state;
82 r->tcpdiag_timer = 0;
83 r->tcpdiag_retrans = 0;
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);
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;
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);
113 #endif
114 nlh->nlmsg_len = skb->tail - b;
115 return skb->len;
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;
123 #ifdef CONFIG_IP_TCPDIAG_IPV6
124 if (r->tcpdiag_family == AF_INET6) {
125 struct ipv6_pinfo *np = inet6_sk(sk);
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);
132 #endif
134 #define EXPIRES_IN_MS(tmo) ((tmo-jiffies)*1000+HZ-1)/HZ
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;
152 #undef EXPIRES_IN_MS
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);
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);
166 if (info)
167 tcp_get_info(sk, info);
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);
183 nlh->nlmsg_len = skb->tail - b;
184 return skb->len;
186 nlmsg_failure:
187 skb_trim(skb, b - skb->data);
188 return -1;
191 extern struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport,
192 int dif);
193 #ifdef CONFIG_IP_TCPDIAG_IPV6
194 extern struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
195 struct in6_addr *daddr, u16 dport,
196 int dif);
197 #else
198 static inline struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
199 struct in6_addr *daddr, u16 dport,
200 int dif)
202 return NULL;
204 #endif
206 static int tcpdiag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh)
208 int err;
209 struct sock *sk;
210 struct tcpdiagreq *req = NLMSG_DATA(nlh);
211 struct sk_buff *rep;
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);
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);
224 #endif
225 else {
226 return -EINVAL;
229 if (sk == NULL)
230 return -ENOENT;
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;
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;
246 if (tcpdiag_fill(rep, sk, req->tcpdiag_ext,
247 NETLINK_CB(in_skb).pid,
248 nlh->nlmsg_seq, 0) <= 0)
249 BUG();
251 err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
252 if (err > 0)
253 err = 0;
255 out:
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);
262 return err;
265 static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
267 int words = bits >> 5;
269 bits &= 0x1f;
271 if (words) {
272 if (memcmp(a1, a2, words << 2))
273 return 0;
275 if (bits) {
276 __u32 w1, w2;
277 __u32 mask;
279 w1 = a1[words];
280 w2 = a2[words];
282 mask = htonl((0xffffffff) << (32 - bits));
284 if ((w1 ^ w2) & mask)
285 return 0;
288 return 1;
292 static int tcpdiag_bc_run(const void *bc, int len,
293 const struct tcpdiag_entry *entry)
295 while (len > 0) {
296 int yes = 1;
297 const struct tcpdiag_bc_op *op = bc;
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:
323 struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1);
324 u32 *addr;
326 if (cond->port != -1 &&
327 cond->port != (op->code == TCPDIAG_BC_S_COND ?
328 entry->sport : entry->dport)) {
329 yes = 0;
330 break;
333 if (cond->prefix_len == 0)
334 break;
336 if (op->code == TCPDIAG_BC_S_COND)
337 addr = entry->saddr;
338 else
339 addr = entry->daddr;
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;
350 yes = 0;
351 break;
355 if (yes) {
356 len -= op->yes;
357 bc += op->yes;
358 } else {
359 len -= op->no;
360 bc += op->no;
363 return (len == 0);
366 static int valid_cc(const void *bc, int len, int cc)
368 while (len >= 0) {
369 const struct tcpdiag_bc_op *op = bc;
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;
380 return 0;
383 static int tcpdiag_bc_audit(const void *bytecode, int bytecode_len)
385 const unsigned char *bc = bytecode;
386 int len = bytecode_len;
388 while (len > 0) {
389 struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc;
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;
416 bc += op->yes;
417 len -= op->yes;
419 return len == 0 ? 0 : -EINVAL;
422 static int tcpdiag_dump_sock(struct sk_buff *skb, struct sock *sk,
423 struct netlink_callback *cb)
425 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
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);
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);
437 entry.saddr = np->rcv_saddr.s6_addr32;
438 entry.daddr = np->daddr.s6_addr32;
439 } else
440 #endif
442 entry.saddr = &inet->rcv_saddr;
443 entry.daddr = &inet->daddr;
445 entry.sport = inet->num;
446 entry.dport = ntohs(inet->dport);
447 entry.userlocks = sk->sk_userlocks;
449 if (!tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
450 return 0;
453 return tcpdiag_fill(skb, sk, r->tcpdiag_ext, NETLINK_CB(cb->skb).pid,
454 cb->nlh->nlmsg_seq, NLM_F_MULTI);
457 static int tcpdiag_fill_req(struct sk_buff *skb, struct sock *sk,
458 struct open_request *req,
459 u32 pid, u32 seq)
461 struct inet_sock *inet = inet_sk(sk);
462 unsigned char *b = skb->tail;
463 struct tcpdiagmsg *r;
464 struct nlmsghdr *nlh;
465 long tmo;
467 nlh = NLMSG_PUT(skb, pid, seq, TCPDIAG_GETSOCK, sizeof(*r));
468 nlh->nlmsg_flags = NLM_F_MULTI;
469 r = NLMSG_DATA(nlh);
471 r->tcpdiag_family = sk->sk_family;
472 r->tcpdiag_state = TCP_SYN_RECV;
473 r->tcpdiag_timer = 1;
474 r->tcpdiag_retrans = req->retrans;
476 r->id.tcpdiag_if = sk->sk_bound_dev_if;
477 r->id.tcpdiag_cookie[0] = (u32)(unsigned long)req;
478 r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
480 tmo = req->expires - jiffies;
481 if (tmo < 0)
482 tmo = 0;
484 r->id.tcpdiag_sport = inet->sport;
485 r->id.tcpdiag_dport = req->rmt_port;
486 r->id.tcpdiag_src[0] = req->af.v4_req.loc_addr;
487 r->id.tcpdiag_dst[0] = req->af.v4_req.rmt_addr;
488 r->tcpdiag_expires = jiffies_to_msecs(tmo),
489 r->tcpdiag_rqueue = 0;
490 r->tcpdiag_wqueue = 0;
491 r->tcpdiag_uid = sock_i_uid(sk);
492 r->tcpdiag_inode = 0;
493 #ifdef CONFIG_IP_TCPDIAG_IPV6
494 if (r->tcpdiag_family == AF_INET6) {
495 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
496 &req->af.v6_req.loc_addr);
497 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
498 &req->af.v6_req.rmt_addr);
500 #endif
501 nlh->nlmsg_len = skb->tail - b;
503 return skb->len;
505 nlmsg_failure:
506 skb_trim(skb, b - skb->data);
507 return -1;
510 static int tcpdiag_dump_reqs(struct sk_buff *skb, struct sock *sk,
511 struct netlink_callback *cb)
513 struct tcpdiag_entry entry;
514 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
515 struct tcp_sock *tp = tcp_sk(sk);
516 struct tcp_listen_opt *lopt;
517 struct rtattr *bc = NULL;
518 struct inet_sock *inet = inet_sk(sk);
519 int j, s_j;
520 int reqnum, s_reqnum;
521 int err = 0;
523 s_j = cb->args[3];
524 s_reqnum = cb->args[4];
526 if (s_j > 0)
527 s_j--;
529 entry.family = sk->sk_family;
531 read_lock_bh(&tp->syn_wait_lock);
533 lopt = tp->listen_opt;
534 if (!lopt || !lopt->qlen)
535 goto out;
537 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
538 bc = (struct rtattr *)(r + 1);
539 entry.sport = inet->num;
540 entry.userlocks = sk->sk_userlocks;
543 for (j = s_j; j < TCP_SYNQ_HSIZE; j++) {
544 struct open_request *req, *head = lopt->syn_table[j];
546 reqnum = 0;
547 for (req = head; req; reqnum++, req = req->dl_next) {
548 if (reqnum < s_reqnum)
549 continue;
550 if (r->id.tcpdiag_dport != req->rmt_port &&
551 r->id.tcpdiag_dport)
552 continue;
554 if (bc) {
555 entry.saddr =
556 #ifdef CONFIG_IP_TCPDIAG_IPV6
557 (entry.family == AF_INET6) ?
558 req->af.v6_req.loc_addr.s6_addr32 :
559 #endif
560 &req->af.v4_req.loc_addr;
561 entry.daddr =
562 #ifdef CONFIG_IP_TCPDIAG_IPV6
563 (entry.family == AF_INET6) ?
564 req->af.v6_req.rmt_addr.s6_addr32 :
565 #endif
566 &req->af.v4_req.rmt_addr;
567 entry.dport = ntohs(req->rmt_port);
569 if (!tcpdiag_bc_run(RTA_DATA(bc),
570 RTA_PAYLOAD(bc), &entry))
571 continue;
574 err = tcpdiag_fill_req(skb, sk, req,
575 NETLINK_CB(cb->skb).pid,
576 cb->nlh->nlmsg_seq);
577 if (err < 0) {
578 cb->args[3] = j + 1;
579 cb->args[4] = reqnum;
580 goto out;
584 s_reqnum = 0;
587 out:
588 read_unlock_bh(&tp->syn_wait_lock);
590 return err;
593 static int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb)
595 int i, num;
596 int s_i, s_num;
597 struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
599 s_i = cb->args[1];
600 s_num = num = cb->args[2];
602 if (cb->args[0] == 0) {
603 if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV)))
604 goto skip_listen_ht;
605 tcp_listen_lock();
606 for (i = s_i; i < TCP_LHTABLE_SIZE; i++) {
607 struct sock *sk;
608 struct hlist_node *node;
610 num = 0;
611 sk_for_each(sk, node, &tcp_listening_hash[i]) {
612 struct inet_sock *inet = inet_sk(sk);
614 if (num < s_num) {
615 num++;
616 continue;
619 if (r->id.tcpdiag_sport != inet->sport &&
620 r->id.tcpdiag_sport)
621 goto next_listen;
623 if (!(r->tcpdiag_states&TCPF_LISTEN) ||
624 r->id.tcpdiag_dport ||
625 cb->args[3] > 0)
626 goto syn_recv;
628 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
629 tcp_listen_unlock();
630 goto done;
633 syn_recv:
634 if (!(r->tcpdiag_states&TCPF_SYN_RECV))
635 goto next_listen;
637 if (tcpdiag_dump_reqs(skb, sk, cb) < 0) {
638 tcp_listen_unlock();
639 goto done;
642 next_listen:
643 cb->args[3] = 0;
644 cb->args[4] = 0;
645 ++num;
648 s_num = 0;
649 cb->args[3] = 0;
650 cb->args[4] = 0;
652 tcp_listen_unlock();
653 skip_listen_ht:
654 cb->args[0] = 1;
655 s_i = num = s_num = 0;
658 if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV)))
659 return skb->len;
661 for (i = s_i; i < tcp_ehash_size; i++) {
662 struct tcp_ehash_bucket *head = &tcp_ehash[i];
663 struct sock *sk;
664 struct hlist_node *node;
666 if (i > s_i)
667 s_num = 0;
669 read_lock_bh(&head->lock);
671 num = 0;
672 sk_for_each(sk, node, &head->chain) {
673 struct inet_sock *inet = inet_sk(sk);
675 if (num < s_num)
676 goto next_normal;
677 if (!(r->tcpdiag_states & (1 << sk->sk_state)))
678 goto next_normal;
679 if (r->id.tcpdiag_sport != inet->sport &&
680 r->id.tcpdiag_sport)
681 goto next_normal;
682 if (r->id.tcpdiag_dport != inet->dport && r->id.tcpdiag_dport)
683 goto next_normal;
684 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
685 read_unlock_bh(&head->lock);
686 goto done;
688 next_normal:
689 ++num;
692 if (r->tcpdiag_states&TCPF_TIME_WAIT) {
693 sk_for_each(sk, node,
694 &tcp_ehash[i + tcp_ehash_size].chain) {
695 struct inet_sock *inet = inet_sk(sk);
697 if (num < s_num)
698 goto next_dying;
699 if (r->id.tcpdiag_sport != inet->sport &&
700 r->id.tcpdiag_sport)
701 goto next_dying;
702 if (r->id.tcpdiag_dport != inet->dport &&
703 r->id.tcpdiag_dport)
704 goto next_dying;
705 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
706 read_unlock_bh(&head->lock);
707 goto done;
709 next_dying:
710 ++num;
713 read_unlock_bh(&head->lock);
716 done:
717 cb->args[1] = i;
718 cb->args[2] = num;
719 return skb->len;
722 static int tcpdiag_dump_done(struct netlink_callback *cb)
724 return 0;
728 static __inline__ int
729 tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
731 if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
732 return 0;
734 if (nlh->nlmsg_type != TCPDIAG_GETSOCK)
735 goto err_inval;
737 if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len)
738 goto err_inval;
740 if (nlh->nlmsg_flags&NLM_F_DUMP) {
741 if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) {
742 struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq));
743 if (rta->rta_type != TCPDIAG_REQ_BYTECODE ||
744 rta->rta_len < 8 ||
745 rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq)))
746 goto err_inval;
747 if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
748 goto err_inval;
750 return netlink_dump_start(tcpnl, skb, nlh,
751 tcpdiag_dump,
752 tcpdiag_dump_done);
753 } else {
754 return tcpdiag_get_exact(skb, nlh);
757 err_inval:
758 return -EINVAL;
762 static inline void tcpdiag_rcv_skb(struct sk_buff *skb)
764 int err;
765 struct nlmsghdr * nlh;
767 if (skb->len >= NLMSG_SPACE(0)) {
768 nlh = (struct nlmsghdr *)skb->data;
769 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
770 return;
771 err = tcpdiag_rcv_msg(skb, nlh);
772 if (err || nlh->nlmsg_flags & NLM_F_ACK)
773 netlink_ack(skb, nlh, err);
777 static void tcpdiag_rcv(struct sock *sk, int len)
779 struct sk_buff *skb;
780 unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
782 while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
783 tcpdiag_rcv_skb(skb);
784 kfree_skb(skb);
788 static int __init tcpdiag_init(void)
790 tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv);
791 if (tcpnl == NULL)
792 return -ENOMEM;
793 return 0;
796 static void __exit tcpdiag_exit(void)
798 sock_release(tcpnl->sk_socket);
801 module_init(tcpdiag_init);
802 module_exit(tcpdiag_exit);
803 MODULE_LICENSE("GPL");