[TCPDIAG]: Introduce inet_diag_{register,unregister}
[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 #include <net/inet_connection_sock.h>
28 #include <net/inet_hashtables.h>
29 #include <net/inet_timewait_sock.h>
30 #include <net/inet6_hashtables.h>
31
32 #include <linux/inet.h>
33 #include <linux/stddef.h>
34
35 #include <linux/tcp_diag.h>
36
37 static const struct inet_diag_handler **inet_diag_table;
38
39 struct tcpdiag_entry
40 {
41         u32 *saddr;
42         u32 *daddr;
43         u16 sport;
44         u16 dport;
45         u16 family;
46         u16 userlocks;
47 };
48
49 static struct sock *tcpnl;
50
51 #define TCPDIAG_PUT(skb, attrtype, attrlen) \
52         RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
53
54 #ifdef CONFIG_IP_TCPDIAG_DCCP
55 extern struct inet_hashinfo dccp_hashinfo;
56 #endif
57
58 static int tcpdiag_fill(struct sk_buff *skb, struct sock *sk,
59                         int ext, u32 pid, u32 seq, u16 nlmsg_flags,
60                         const struct nlmsghdr *unlh)
61 {
62         const struct inet_sock *inet = inet_sk(sk);
63         const struct inet_connection_sock *icsk = inet_csk(sk);
64         struct tcpdiagmsg *r;
65         struct nlmsghdr  *nlh;
66         void *info = NULL;
67         struct tcpdiag_meminfo  *minfo = NULL;
68         unsigned char    *b = skb->tail;
69         const struct inet_diag_handler *handler;
70
71         handler = inet_diag_table[unlh->nlmsg_type];
72         BUG_ON(handler == NULL);
73
74         nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
75         nlh->nlmsg_flags = nlmsg_flags;
76
77         r = NLMSG_DATA(nlh);
78         if (sk->sk_state != TCP_TIME_WAIT) {
79                 if (ext & (1<<(TCPDIAG_MEMINFO-1)))
80                         minfo = TCPDIAG_PUT(skb, TCPDIAG_MEMINFO, sizeof(*minfo));
81                 if (ext & (1<<(TCPDIAG_INFO-1)))
82                         info = TCPDIAG_PUT(skb, TCPDIAG_INFO,
83                                            handler->idiag_info_size);
84                 
85                 if ((ext & (1 << (TCPDIAG_CONG - 1))) && icsk->icsk_ca_ops) {
86                         size_t len = strlen(icsk->icsk_ca_ops->name);
87                         strcpy(TCPDIAG_PUT(skb, TCPDIAG_CONG, len+1),
88                                icsk->icsk_ca_ops->name);
89                 }
90         }
91         r->tcpdiag_family = sk->sk_family;
92         r->tcpdiag_state = sk->sk_state;
93         r->tcpdiag_timer = 0;
94         r->tcpdiag_retrans = 0;
95
96         r->id.tcpdiag_if = sk->sk_bound_dev_if;
97         r->id.tcpdiag_cookie[0] = (u32)(unsigned long)sk;
98         r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
99
100         if (r->tcpdiag_state == TCP_TIME_WAIT) {
101                 const struct inet_timewait_sock *tw = inet_twsk(sk);
102                 long tmo = tw->tw_ttd - jiffies;
103                 if (tmo < 0)
104                         tmo = 0;
105
106                 r->id.tcpdiag_sport = tw->tw_sport;
107                 r->id.tcpdiag_dport = tw->tw_dport;
108                 r->id.tcpdiag_src[0] = tw->tw_rcv_saddr;
109                 r->id.tcpdiag_dst[0] = tw->tw_daddr;
110                 r->tcpdiag_state = tw->tw_substate;
111                 r->tcpdiag_timer = 3;
112                 r->tcpdiag_expires = (tmo*1000+HZ-1)/HZ;
113                 r->tcpdiag_rqueue = 0;
114                 r->tcpdiag_wqueue = 0;
115                 r->tcpdiag_uid = 0;
116                 r->tcpdiag_inode = 0;
117 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
118                 if (r->tcpdiag_family == AF_INET6) {
119                         const struct tcp6_timewait_sock *tcp6tw = tcp6_twsk(sk);
120
121                         ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
122                                        &tcp6tw->tw_v6_rcv_saddr);
123                         ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
124                                        &tcp6tw->tw_v6_daddr);
125                 }
126 #endif
127                 nlh->nlmsg_len = skb->tail - b;
128                 return skb->len;
129         }
130
131         r->id.tcpdiag_sport = inet->sport;
132         r->id.tcpdiag_dport = inet->dport;
133         r->id.tcpdiag_src[0] = inet->rcv_saddr;
134         r->id.tcpdiag_dst[0] = inet->daddr;
135
136 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
137         if (r->tcpdiag_family == AF_INET6) {
138                 struct ipv6_pinfo *np = inet6_sk(sk);
139
140                 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
141                                &np->rcv_saddr);
142                 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
143                                &np->daddr);
144         }
145 #endif
146
147 #define EXPIRES_IN_MS(tmo)  ((tmo-jiffies)*1000+HZ-1)/HZ
148
149         if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
150                 r->tcpdiag_timer = 1;
151                 r->tcpdiag_retrans = icsk->icsk_retransmits;
152                 r->tcpdiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
153         } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
154                 r->tcpdiag_timer = 4;
155                 r->tcpdiag_retrans = icsk->icsk_probes_out;
156                 r->tcpdiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
157         } else if (timer_pending(&sk->sk_timer)) {
158                 r->tcpdiag_timer = 2;
159                 r->tcpdiag_retrans = icsk->icsk_probes_out;
160                 r->tcpdiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
161         } else {
162                 r->tcpdiag_timer = 0;
163                 r->tcpdiag_expires = 0;
164         }
165 #undef EXPIRES_IN_MS
166
167         r->tcpdiag_uid = sock_i_uid(sk);
168         r->tcpdiag_inode = sock_i_ino(sk);
169
170         if (minfo) {
171                 minfo->tcpdiag_rmem = atomic_read(&sk->sk_rmem_alloc);
172                 minfo->tcpdiag_wmem = sk->sk_wmem_queued;
173                 minfo->tcpdiag_fmem = sk->sk_forward_alloc;
174                 minfo->tcpdiag_tmem = atomic_read(&sk->sk_wmem_alloc);
175         }
176
177         handler->idiag_get_info(sk, r, info);
178
179         if (sk->sk_state < TCP_TIME_WAIT &&
180             icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
181                 icsk->icsk_ca_ops->get_info(sk, ext, skb);
182
183         nlh->nlmsg_len = skb->tail - b;
184         return skb->len;
185
186 rtattr_failure:
187 nlmsg_failure:
188         skb_trim(skb, b - skb->data);
189         return -1;
190 }
191
192 static 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         struct inet_hashinfo *hashinfo;
199         const struct inet_diag_handler *handler;
200
201         handler = inet_diag_table[nlh->nlmsg_type];
202         BUG_ON(handler == NULL);
203         hashinfo = handler->idiag_hashinfo;
204
205         if (req->tcpdiag_family == AF_INET) {
206                 sk = inet_lookup(hashinfo, req->id.tcpdiag_dst[0],
207                                  req->id.tcpdiag_dport, req->id.tcpdiag_src[0],
208                                  req->id.tcpdiag_sport, req->id.tcpdiag_if);
209         }
210 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
211         else if (req->tcpdiag_family == AF_INET6) {
212                 sk = inet6_lookup(hashinfo,
213                                   (struct in6_addr*)req->id.tcpdiag_dst,
214                                   req->id.tcpdiag_dport,
215                                   (struct in6_addr*)req->id.tcpdiag_src,
216                                   req->id.tcpdiag_sport,
217                                   req->id.tcpdiag_if);
218         }
219 #endif
220         else {
221                 return -EINVAL;
222         }
223
224         if (sk == NULL)
225                 return -ENOENT;
226
227         err = -ESTALE;
228         if ((req->id.tcpdiag_cookie[0] != TCPDIAG_NOCOOKIE ||
229              req->id.tcpdiag_cookie[1] != TCPDIAG_NOCOOKIE) &&
230             ((u32)(unsigned long)sk != req->id.tcpdiag_cookie[0] ||
231              (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.tcpdiag_cookie[1]))
232                 goto out;
233
234         err = -ENOMEM;
235         rep = alloc_skb(NLMSG_SPACE((sizeof(struct tcpdiagmsg) +
236                                      sizeof(struct tcpdiag_meminfo) +
237                                      handler->idiag_info_size + 64)),
238                         GFP_KERNEL);
239         if (!rep)
240                 goto out;
241
242         if (tcpdiag_fill(rep, sk, req->tcpdiag_ext,
243                          NETLINK_CB(in_skb).pid,
244                          nlh->nlmsg_seq, 0, nlh) <= 0)
245                 BUG();
246
247         err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
248         if (err > 0)
249                 err = 0;
250
251 out:
252         if (sk) {
253                 if (sk->sk_state == TCP_TIME_WAIT)
254                         inet_twsk_put((struct inet_timewait_sock *)sk);
255                 else
256                         sock_put(sk);
257         }
258         return err;
259 }
260
261 static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
262 {
263         int words = bits >> 5;
264
265         bits &= 0x1f;
266
267         if (words) {
268                 if (memcmp(a1, a2, words << 2))
269                         return 0;
270         }
271         if (bits) {
272                 __u32 w1, w2;
273                 __u32 mask;
274
275                 w1 = a1[words];
276                 w2 = a2[words];
277
278                 mask = htonl((0xffffffff) << (32 - bits));
279
280                 if ((w1 ^ w2) & mask)
281                         return 0;
282         }
283
284         return 1;
285 }
286
287
288 static int tcpdiag_bc_run(const void *bc, int len,
289                           const struct tcpdiag_entry *entry)
290 {
291         while (len > 0) {
292                 int yes = 1;
293                 const struct tcpdiag_bc_op *op = bc;
294
295                 switch (op->code) {
296                 case TCPDIAG_BC_NOP:
297                         break;
298                 case TCPDIAG_BC_JMP:
299                         yes = 0;
300                         break;
301                 case TCPDIAG_BC_S_GE:
302                         yes = entry->sport >= op[1].no;
303                         break;
304                 case TCPDIAG_BC_S_LE:
305                         yes = entry->dport <= op[1].no;
306                         break;
307                 case TCPDIAG_BC_D_GE:
308                         yes = entry->dport >= op[1].no;
309                         break;
310                 case TCPDIAG_BC_D_LE:
311                         yes = entry->dport <= op[1].no;
312                         break;
313                 case TCPDIAG_BC_AUTO:
314                         yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
315                         break;
316                 case TCPDIAG_BC_S_COND:
317                 case TCPDIAG_BC_D_COND:
318                 {
319                         struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1);
320                         u32 *addr;
321
322                         if (cond->port != -1 &&
323                             cond->port != (op->code == TCPDIAG_BC_S_COND ?
324                                              entry->sport : entry->dport)) {
325                                 yes = 0;
326                                 break;
327                         }
328                         
329                         if (cond->prefix_len == 0)
330                                 break;
331
332                         if (op->code == TCPDIAG_BC_S_COND)
333                                 addr = entry->saddr;
334                         else
335                                 addr = entry->daddr;
336
337                         if (bitstring_match(addr, cond->addr, cond->prefix_len))
338                                 break;
339                         if (entry->family == AF_INET6 &&
340                             cond->family == AF_INET) {
341                                 if (addr[0] == 0 && addr[1] == 0 &&
342                                     addr[2] == htonl(0xffff) &&
343                                     bitstring_match(addr+3, cond->addr, cond->prefix_len))
344                                         break;
345                         }
346                         yes = 0;
347                         break;
348                 }
349                 }
350
351                 if (yes) { 
352                         len -= op->yes;
353                         bc += op->yes;
354                 } else {
355                         len -= op->no;
356                         bc += op->no;
357                 }
358         }
359         return (len == 0);
360 }
361
362 static int valid_cc(const void *bc, int len, int cc)
363 {
364         while (len >= 0) {
365                 const struct tcpdiag_bc_op *op = bc;
366
367                 if (cc > len)
368                         return 0;
369                 if (cc == len)
370                         return 1;
371                 if (op->yes < 4)
372                         return 0;
373                 len -= op->yes;
374                 bc  += op->yes;
375         }
376         return 0;
377 }
378
379 static int tcpdiag_bc_audit(const void *bytecode, int bytecode_len)
380 {
381         const unsigned char *bc = bytecode;
382         int  len = bytecode_len;
383
384         while (len > 0) {
385                 struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc;
386
387 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
388                 switch (op->code) {
389                 case TCPDIAG_BC_AUTO:
390                 case TCPDIAG_BC_S_COND:
391                 case TCPDIAG_BC_D_COND:
392                 case TCPDIAG_BC_S_GE:
393                 case TCPDIAG_BC_S_LE:
394                 case TCPDIAG_BC_D_GE:
395                 case TCPDIAG_BC_D_LE:
396                         if (op->yes < 4 || op->yes > len+4)
397                                 return -EINVAL;
398                 case TCPDIAG_BC_JMP:
399                         if (op->no < 4 || op->no > len+4)
400                                 return -EINVAL;
401                         if (op->no < len &&
402                             !valid_cc(bytecode, bytecode_len, len-op->no))
403                                 return -EINVAL;
404                         break;
405                 case TCPDIAG_BC_NOP:
406                         if (op->yes < 4 || op->yes > len+4)
407                                 return -EINVAL;
408                         break;
409                 default:
410                         return -EINVAL;
411                 }
412                 bc += op->yes;
413                 len -= op->yes;
414         }
415         return len == 0 ? 0 : -EINVAL;
416 }
417
418 static int tcpdiag_dump_sock(struct sk_buff *skb, struct sock *sk,
419                              struct netlink_callback *cb)
420 {
421         struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
422
423         if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
424                 struct tcpdiag_entry entry;
425                 struct rtattr *bc = (struct rtattr *)(r + 1);
426                 struct inet_sock *inet = inet_sk(sk);
427
428                 entry.family = sk->sk_family;
429 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
430                 if (entry.family == AF_INET6) {
431                         struct ipv6_pinfo *np = inet6_sk(sk);
432
433                         entry.saddr = np->rcv_saddr.s6_addr32;
434                         entry.daddr = np->daddr.s6_addr32;
435                 } else
436 #endif
437                 {
438                         entry.saddr = &inet->rcv_saddr;
439                         entry.daddr = &inet->daddr;
440                 }
441                 entry.sport = inet->num;
442                 entry.dport = ntohs(inet->dport);
443                 entry.userlocks = sk->sk_userlocks;
444
445                 if (!tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
446                         return 0;
447         }
448
449         return tcpdiag_fill(skb, sk, r->tcpdiag_ext, NETLINK_CB(cb->skb).pid,
450                             cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
451 }
452
453 static int tcpdiag_fill_req(struct sk_buff *skb, struct sock *sk,
454                             struct request_sock *req,
455                             u32 pid, u32 seq,
456                             const struct nlmsghdr *unlh)
457 {
458         const struct inet_request_sock *ireq = inet_rsk(req);
459         struct inet_sock *inet = inet_sk(sk);
460         unsigned char *b = skb->tail;
461         struct tcpdiagmsg *r;
462         struct nlmsghdr *nlh;
463         long tmo;
464
465         nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
466         nlh->nlmsg_flags = NLM_F_MULTI;
467         r = NLMSG_DATA(nlh);
468
469         r->tcpdiag_family = sk->sk_family;
470         r->tcpdiag_state = TCP_SYN_RECV;
471         r->tcpdiag_timer = 1;
472         r->tcpdiag_retrans = req->retrans;
473
474         r->id.tcpdiag_if = sk->sk_bound_dev_if;
475         r->id.tcpdiag_cookie[0] = (u32)(unsigned long)req;
476         r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
477
478         tmo = req->expires - jiffies;
479         if (tmo < 0)
480                 tmo = 0;
481
482         r->id.tcpdiag_sport = inet->sport;
483         r->id.tcpdiag_dport = ireq->rmt_port;
484         r->id.tcpdiag_src[0] = ireq->loc_addr;
485         r->id.tcpdiag_dst[0] = ireq->rmt_addr;
486         r->tcpdiag_expires = jiffies_to_msecs(tmo),
487         r->tcpdiag_rqueue = 0;
488         r->tcpdiag_wqueue = 0;
489         r->tcpdiag_uid = sock_i_uid(sk);
490         r->tcpdiag_inode = 0;
491 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
492         if (r->tcpdiag_family == AF_INET6) {
493                 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
494                                &tcp6_rsk(req)->loc_addr);
495                 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
496                                &tcp6_rsk(req)->rmt_addr);
497         }
498 #endif
499         nlh->nlmsg_len = skb->tail - b;
500
501         return skb->len;
502
503 nlmsg_failure:
504         skb_trim(skb, b - skb->data);
505         return -1;
506 }
507
508 static int tcpdiag_dump_reqs(struct sk_buff *skb, struct sock *sk,
509                              struct netlink_callback *cb)
510 {
511         struct tcpdiag_entry entry;
512         struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
513         struct inet_connection_sock *icsk = inet_csk(sk);
514         struct listen_sock *lopt;
515         struct rtattr *bc = NULL;
516         struct inet_sock *inet = inet_sk(sk);
517         int j, s_j;
518         int reqnum, s_reqnum;
519         int err = 0;
520
521         s_j = cb->args[3];
522         s_reqnum = cb->args[4];
523
524         if (s_j > 0)
525                 s_j--;
526
527         entry.family = sk->sk_family;
528
529         read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
530
531         lopt = icsk->icsk_accept_queue.listen_opt;
532         if (!lopt || !lopt->qlen)
533                 goto out;
534
535         if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
536                 bc = (struct rtattr *)(r + 1);
537                 entry.sport = inet->num;
538                 entry.userlocks = sk->sk_userlocks;
539         }
540
541         for (j = s_j; j < lopt->nr_table_entries; j++) {
542                 struct request_sock *req, *head = lopt->syn_table[j];
543
544                 reqnum = 0;
545                 for (req = head; req; reqnum++, req = req->dl_next) {
546                         struct inet_request_sock *ireq = inet_rsk(req);
547
548                         if (reqnum < s_reqnum)
549                                 continue;
550                         if (r->id.tcpdiag_dport != ireq->rmt_port &&
551                             r->id.tcpdiag_dport)
552                                 continue;
553
554                         if (bc) {
555                                 entry.saddr =
556 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
557                                         (entry.family == AF_INET6) ?
558                                         tcp6_rsk(req)->loc_addr.s6_addr32 :
559 #endif
560                                         &ireq->loc_addr;
561                                 entry.daddr = 
562 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
563                                         (entry.family == AF_INET6) ?
564                                         tcp6_rsk(req)->rmt_addr.s6_addr32 :
565 #endif
566                                         &ireq->rmt_addr;
567                                 entry.dport = ntohs(ireq->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, cb->nlh);
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(&icsk->icsk_accept_queue.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         const struct inet_diag_handler *handler;
599         struct inet_hashinfo *hashinfo;
600
601         handler = inet_diag_table[cb->nlh->nlmsg_type];
602         BUG_ON(handler == NULL);
603         hashinfo = handler->idiag_hashinfo;
604                 
605         s_i = cb->args[1];
606         s_num = num = cb->args[2];
607
608         if (cb->args[0] == 0) {
609                 if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV)))
610                         goto skip_listen_ht;
611
612                 inet_listen_lock(hashinfo);
613                 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
614                         struct sock *sk;
615                         struct hlist_node *node;
616
617                         num = 0;
618                         sk_for_each(sk, node, &hashinfo->listening_hash[i]) {
619                                 struct inet_sock *inet = inet_sk(sk);
620
621                                 if (num < s_num) {
622                                         num++;
623                                         continue;
624                                 }
625
626                                 if (r->id.tcpdiag_sport != inet->sport &&
627                                     r->id.tcpdiag_sport)
628                                         goto next_listen;
629
630                                 if (!(r->tcpdiag_states&TCPF_LISTEN) ||
631                                     r->id.tcpdiag_dport ||
632                                     cb->args[3] > 0)
633                                         goto syn_recv;
634
635                                 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
636                                         inet_listen_unlock(hashinfo);
637                                         goto done;
638                                 }
639
640 syn_recv:
641                                 if (!(r->tcpdiag_states&TCPF_SYN_RECV))
642                                         goto next_listen;
643
644                                 if (tcpdiag_dump_reqs(skb, sk, cb) < 0) {
645                                         inet_listen_unlock(hashinfo);
646                                         goto done;
647                                 }
648
649 next_listen:
650                                 cb->args[3] = 0;
651                                 cb->args[4] = 0;
652                                 ++num;
653                         }
654
655                         s_num = 0;
656                         cb->args[3] = 0;
657                         cb->args[4] = 0;
658                 }
659                 inet_listen_unlock(hashinfo);
660 skip_listen_ht:
661                 cb->args[0] = 1;
662                 s_i = num = s_num = 0;
663         }
664
665         if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV)))
666                 return skb->len;
667
668         for (i = s_i; i < hashinfo->ehash_size; i++) {
669                 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
670                 struct sock *sk;
671                 struct hlist_node *node;
672
673                 if (i > s_i)
674                         s_num = 0;
675
676                 read_lock_bh(&head->lock);
677
678                 num = 0;
679                 sk_for_each(sk, node, &head->chain) {
680                         struct inet_sock *inet = inet_sk(sk);
681
682                         if (num < s_num)
683                                 goto next_normal;
684                         if (!(r->tcpdiag_states & (1 << sk->sk_state)))
685                                 goto next_normal;
686                         if (r->id.tcpdiag_sport != inet->sport &&
687                             r->id.tcpdiag_sport)
688                                 goto next_normal;
689                         if (r->id.tcpdiag_dport != inet->dport && r->id.tcpdiag_dport)
690                                 goto next_normal;
691                         if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
692                                 read_unlock_bh(&head->lock);
693                                 goto done;
694                         }
695 next_normal:
696                         ++num;
697                 }
698
699                 if (r->tcpdiag_states&TCPF_TIME_WAIT) {
700                         sk_for_each(sk, node,
701                                     &hashinfo->ehash[i + hashinfo->ehash_size].chain) {
702                                 struct inet_sock *inet = inet_sk(sk);
703
704                                 if (num < s_num)
705                                         goto next_dying;
706                                 if (r->id.tcpdiag_sport != inet->sport &&
707                                     r->id.tcpdiag_sport)
708                                         goto next_dying;
709                                 if (r->id.tcpdiag_dport != inet->dport &&
710                                     r->id.tcpdiag_dport)
711                                         goto next_dying;
712                                 if (tcpdiag_dump_sock(skb, sk, cb) < 0) {
713                                         read_unlock_bh(&head->lock);
714                                         goto done;
715                                 }
716 next_dying:
717                                 ++num;
718                         }
719                 }
720                 read_unlock_bh(&head->lock);
721         }
722
723 done:
724         cb->args[1] = i;
725         cb->args[2] = num;
726         return skb->len;
727 }
728
729 static int tcpdiag_dump_done(struct netlink_callback *cb)
730 {
731         return 0;
732 }
733
734
735 static __inline__ int
736 tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
737 {
738         if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
739                 return 0;
740
741         if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX)
742                 goto err_inval;
743
744         if (inet_diag_table[nlh->nlmsg_type] == NULL)
745                 return -ENOENT;
746
747         if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len)
748                 goto err_inval;
749
750         if (nlh->nlmsg_flags&NLM_F_DUMP) {
751                 if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) {
752                         struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq));
753                         if (rta->rta_type != TCPDIAG_REQ_BYTECODE ||
754                             rta->rta_len < 8 ||
755                             rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq)))
756                                 goto err_inval;
757                         if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
758                                 goto err_inval;
759                 }
760                 return netlink_dump_start(tcpnl, skb, nlh,
761                                           tcpdiag_dump,
762                                           tcpdiag_dump_done);
763         } else {
764                 return tcpdiag_get_exact(skb, nlh);
765         }
766
767 err_inval:
768         return -EINVAL;
769 }
770
771
772 static inline void tcpdiag_rcv_skb(struct sk_buff *skb)
773 {
774         int err;
775         struct nlmsghdr * nlh;
776
777         if (skb->len >= NLMSG_SPACE(0)) {
778                 nlh = (struct nlmsghdr *)skb->data;
779                 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
780                         return;
781                 err = tcpdiag_rcv_msg(skb, nlh);
782                 if (err || nlh->nlmsg_flags & NLM_F_ACK) 
783                         netlink_ack(skb, nlh, err);
784         }
785 }
786
787 static void tcpdiag_rcv(struct sock *sk, int len)
788 {
789         struct sk_buff *skb;
790         unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
791
792         while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
793                 tcpdiag_rcv_skb(skb);
794                 kfree_skb(skb);
795         }
796 }
797
798 static void tcp_diag_get_info(struct sock *sk, struct tcpdiagmsg *r,
799                               void *_info)
800 {
801         const struct tcp_sock *tp = tcp_sk(sk);
802         struct tcp_info *info = _info;
803
804         r->tcpdiag_rqueue = tp->rcv_nxt - tp->copied_seq;
805         r->tcpdiag_wqueue = tp->write_seq - tp->snd_una;
806         if (info != NULL)
807                 tcp_get_info(sk, info);
808 }
809
810 static struct inet_diag_handler tcp_diag_handler = {
811         .idiag_hashinfo  = &tcp_hashinfo,
812         .idiag_get_info  = tcp_diag_get_info,
813         .idiag_type      = TCPDIAG_GETSOCK,
814         .idiag_info_size = sizeof(struct tcp_info),
815 };
816
817 static DEFINE_SPINLOCK(inet_diag_register_lock);
818
819 int inet_diag_register(const struct inet_diag_handler *h)
820 {
821         const __u16 type = h->idiag_type;
822         int err = -EINVAL;
823
824         if (type >= INET_DIAG_GETSOCK_MAX)
825                 goto out;
826
827         spin_lock(&inet_diag_register_lock);
828         err = -EEXIST;
829         if (inet_diag_table[type] == NULL) {
830                 inet_diag_table[type] = h;
831                 err = 0;
832         }
833         spin_unlock(&inet_diag_register_lock);
834 out:
835         return err;
836 }
837 EXPORT_SYMBOL_GPL(inet_diag_register);
838
839 void inet_diag_unregister(const struct inet_diag_handler *h)
840 {
841         const __u16 type = h->idiag_type;
842
843         if (type >= INET_DIAG_GETSOCK_MAX)
844                 return;
845
846         spin_lock(&inet_diag_register_lock);
847         inet_diag_table[type] = NULL;
848         spin_unlock(&inet_diag_register_lock);
849
850         synchronize_rcu();
851 }
852 EXPORT_SYMBOL_GPL(inet_diag_unregister);
853
854 static int __init tcpdiag_init(void)
855 {
856         const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
857                                           sizeof(struct inet_diag_handler *));
858         int err = -ENOMEM;
859
860         inet_diag_table = kmalloc(inet_diag_table_size, GFP_KERNEL);
861         if (!inet_diag_table)
862                 goto out;
863
864         memset(inet_diag_table, 0, inet_diag_table_size);
865
866         tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv,
867                                       THIS_MODULE);
868         if (tcpnl == NULL)
869                 goto out_free_table;
870
871         err = inet_diag_register(&tcp_diag_handler);
872         if (err)
873                 goto out_sock_release;
874 out:
875         return err;
876 out_sock_release:
877         sock_release(tcpnl->sk_socket);
878 out_free_table:
879         kfree(inet_diag_table);
880         goto out;
881 }
882
883 static void __exit tcpdiag_exit(void)
884 {
885         sock_release(tcpnl->sk_socket);
886         kfree(inet_diag_table);
887 }
888
889 module_init(tcpdiag_init);
890 module_exit(tcpdiag_exit);
891 MODULE_LICENSE("GPL");