Linux-2.6.12-rc2
[linux-2.6.git] / net / ipv4 / tcp_diag.c
1 /*
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
33 struct tcpdiag_entry
34 {
35         u32 *saddr;
36         u32 *daddr;
37         u16 sport;
38         u16 dport;
39         u16 family;
40         u16 userlocks;
41 };
42
43 static 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
55 static 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
186 nlmsg_failure:
187         skb_trim(skb, b - skb->data);
188         return -1;
189 }
190
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)
201 {
202         return NULL;
203 }
204 #endif
205
206 static 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
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);
261         }
262         return err;
263 }
264
265 static 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
292 static 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
366 static 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
383 static 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
422 static 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
457 static int tcpdiag_fill_req(struct sk_buff *skb, struct sock *sk,
458                             struct open_request *req,
459                             u32 pid, u32 seq)
460 {
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;
466
467         nlh = NLMSG_PUT(skb, pid, seq, TCPDIAG_GETSOCK, sizeof(*r));
468         nlh->nlmsg_flags = NLM_F_MULTI;
469         r = NLMSG_DATA(nlh);
470
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;
475
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);
479
480         tmo = req->expires - jiffies;
481         if (tmo < 0)
482                 tmo = 0;
483
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);
499         }
500 #endif
501         nlh->nlmsg_len = skb->tail - b;
502
503         return skb->len;
504
505 nlmsg_failure:
506         skb_trim(skb, b - skb->data);
507         return -1;
508 }
509
510 static int tcpdiag_dump_reqs(struct sk_buff *skb, struct sock *sk,
511                              struct netlink_callback *cb)
512 {
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;
522
523         s_j = cb->args[3];
524         s_reqnum = cb->args[4];
525
526         if (s_j > 0)
527                 s_j--;
528
529         entry.family = sk->sk_family;
530
531         read_lock_bh(&tp->syn_wait_lock);
532
533         lopt = tp->listen_opt;
534         if (!lopt || !lopt->qlen)
535                 goto out;
536
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;
541         }
542
543         for (j = s_j; j < TCP_SYNQ_HSIZE; j++) {
544                 struct open_request *req, *head = lopt->syn_table[j];
545
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;
553
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);
568
569                                 if (!tcpdiag_bc_run(RTA_DATA(bc),
570                                                     RTA_PAYLOAD(bc), &entry))
571                                         continue;
572                         }
573
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;
581                         }
582                 }
583
584                 s_reqnum = 0;
585         }
586
587 out:
588         read_unlock_bh(&tp->syn_wait_lock);
589
590         return err;
591 }
592
593 static int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb)
594 {
595         int i, num;
596         int s_i, s_num;
597         struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
598
599         s_i = cb->args[1];
600         s_num = num = cb->args[2];
601
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;
609
610                         num = 0;
611                         sk_for_each(sk, node, &tcp_listening_hash[i]) {
612                                 struct inet_sock *inet = inet_sk(sk);
613
614                                 if (num < s_num) {
615                                         num++;
616                                         continue;
617                                 }
618
619                                 if (r->id.tcpdiag_sport != inet->sport &&
620                                     r->id.tcpdiag_sport)
621                                         goto next_listen;
622
623                                 if (!(r->tcpdiag_states&TCPF_LISTEN) ||
624                                     r->id.tcpdiag_dport ||
625                                     cb->args[3] > 0)
626                                         goto syn_recv;
627
628                                 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
629                                         tcp_listen_unlock();
630                                         goto done;
631                                 }
632
633 syn_recv:
634                                 if (!(r->tcpdiag_states&TCPF_SYN_RECV))
635                                         goto next_listen;
636
637                                 if (tcpdiag_dump_reqs(skb, sk, cb) < 0) {
638                                         tcp_listen_unlock();
639                                         goto done;
640                                 }
641
642 next_listen:
643                                 cb->args[3] = 0;
644                                 cb->args[4] = 0;
645                                 ++num;
646                         }
647
648                         s_num = 0;
649                         cb->args[3] = 0;
650                         cb->args[4] = 0;
651                 }
652                 tcp_listen_unlock();
653 skip_listen_ht:
654                 cb->args[0] = 1;
655                 s_i = num = s_num = 0;
656         }
657
658         if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV)))
659                 return skb->len;
660
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;
665
666                 if (i > s_i)
667                         s_num = 0;
668
669                 read_lock_bh(&head->lock);
670
671                 num = 0;
672                 sk_for_each(sk, node, &head->chain) {
673                         struct inet_sock *inet = inet_sk(sk);
674
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;
687                         }
688 next_normal:
689                         ++num;
690                 }
691
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);
696
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;
708                                 }
709 next_dying:
710                                 ++num;
711                         }
712                 }
713                 read_unlock_bh(&head->lock);
714         }
715
716 done:
717         cb->args[1] = i;
718         cb->args[2] = num;
719         return skb->len;
720 }
721
722 static int tcpdiag_dump_done(struct netlink_callback *cb)
723 {
724         return 0;
725 }
726
727
728 static __inline__ int
729 tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
730 {
731         if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
732                 return 0;
733
734         if (nlh->nlmsg_type != TCPDIAG_GETSOCK)
735                 goto err_inval;
736
737         if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len)
738                 goto err_inval;
739
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;
749                 }
750                 return netlink_dump_start(tcpnl, skb, nlh,
751                                           tcpdiag_dump,
752                                           tcpdiag_dump_done);
753         } else {
754                 return tcpdiag_get_exact(skb, nlh);
755         }
756
757 err_inval:
758         return -EINVAL;
759 }
760
761
762 static inline void tcpdiag_rcv_skb(struct sk_buff *skb)
763 {
764         int err;
765         struct nlmsghdr * nlh;
766
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);
774         }
775 }
776
777 static void tcpdiag_rcv(struct sock *sk, int len)
778 {
779         struct sk_buff *skb;
780
781         while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
782                 tcpdiag_rcv_skb(skb);
783                 kfree_skb(skb);
784         }
785 }
786
787 static int __init tcpdiag_init(void)
788 {
789         tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv);
790         if (tcpnl == NULL)
791                 return -ENOMEM;
792         return 0;
793 }
794
795 static void __exit tcpdiag_exit(void)
796 {
797         sock_release(tcpnl->sk_socket);
798 }
799
800 module_init(tcpdiag_init);
801 module_exit(tcpdiag_exit);
802 MODULE_LICENSE("GPL");