patch-2_6_7-vs1_9_1_12
[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
22 #include <net/icmp.h>
23 #include <net/tcp.h>
24 #include <net/ipv6.h>
25 #include <net/inet_common.h>
26
27 #include <linux/inet.h>
28 #include <linux/stddef.h>
29
30 #include <linux/tcp_diag.h>
31
32 static struct sock *tcpnl;
33
34
35 #define TCPDIAG_PUT(skb, attrtype, attrlen) \
36 ({ int rtalen = RTA_LENGTH(attrlen);        \
37    struct rtattr *rta;                      \
38    if (skb_tailroom(skb) < RTA_ALIGN(rtalen)) goto nlmsg_failure; \
39    rta = (void*)__skb_put(skb, RTA_ALIGN(rtalen)); \
40    rta->rta_type = attrtype;                \
41    rta->rta_len = rtalen;                   \
42    RTA_DATA(rta); })
43
44 /* Return information about state of tcp endpoint in API format. */
45 void tcp_get_info(struct sock *sk, struct tcp_info *info)
46 {
47         struct tcp_opt *tp = tcp_sk(sk);
48         u32 now = tcp_time_stamp;
49
50         memset(info, 0, sizeof(*info));
51
52         info->tcpi_state = sk->sk_state;
53         info->tcpi_ca_state = tp->ca_state;
54         info->tcpi_retransmits = tp->retransmits;
55         info->tcpi_probes = tp->probes_out;
56         info->tcpi_backoff = tp->backoff;
57
58         if (tp->tstamp_ok)
59                 info->tcpi_options |= TCPI_OPT_TIMESTAMPS;
60         if (tp->sack_ok)
61                 info->tcpi_options |= TCPI_OPT_SACK;
62         if (tp->wscale_ok) {
63                 info->tcpi_options |= TCPI_OPT_WSCALE;
64                 info->tcpi_snd_wscale = tp->snd_wscale;
65                 info->tcpi_rcv_wscale = tp->rcv_wscale;
66         } 
67
68         if (tp->ecn_flags&TCP_ECN_OK)
69                 info->tcpi_options |= TCPI_OPT_ECN;
70
71         info->tcpi_rto = (1000000*tp->rto)/HZ;
72         info->tcpi_ato = (1000000*tp->ack.ato)/HZ;
73         info->tcpi_snd_mss = tp->mss_cache;
74         info->tcpi_rcv_mss = tp->ack.rcv_mss;
75
76         info->tcpi_unacked = tp->packets_out;
77         info->tcpi_sacked = tp->sacked_out;
78         info->tcpi_lost = tp->lost_out;
79         info->tcpi_retrans = tp->retrans_out;
80         info->tcpi_fackets = tp->fackets_out;
81
82         info->tcpi_last_data_sent = ((now - tp->lsndtime)*1000)/HZ;
83         info->tcpi_last_data_recv = ((now - tp->ack.lrcvtime)*1000)/HZ;
84         info->tcpi_last_ack_recv = ((now - tp->rcv_tstamp)*1000)/HZ;
85
86         info->tcpi_pmtu = tp->pmtu_cookie;
87         info->tcpi_rcv_ssthresh = tp->rcv_ssthresh;
88         info->tcpi_rtt = ((1000000*tp->srtt)/HZ)>>3;
89         info->tcpi_rttvar = ((1000000*tp->mdev)/HZ)>>2;
90         info->tcpi_snd_ssthresh = tp->snd_ssthresh;
91         info->tcpi_snd_cwnd = tp->snd_cwnd;
92         info->tcpi_advmss = tp->advmss;
93         info->tcpi_reordering = tp->reordering;
94
95         info->tcpi_rcv_rtt = ((1000000*tp->rcv_rtt_est.rtt)/HZ)>>3;
96         info->tcpi_rcv_space = tp->rcvq_space.space;
97 }
98
99 static int tcpdiag_fill(struct sk_buff *skb, struct sock *sk,
100                         int ext, u32 pid, u32 seq)
101 {
102         struct inet_opt *inet = inet_sk(sk);
103         struct tcp_opt *tp = tcp_sk(sk);
104         struct tcpdiagmsg *r;
105         struct nlmsghdr  *nlh;
106         struct tcp_info  *info = NULL;
107         struct tcpdiag_meminfo  *minfo = NULL;
108         struct tcpvegas_info *vinfo = NULL;
109         unsigned char    *b = skb->tail;
110
111         nlh = NLMSG_PUT(skb, pid, seq, TCPDIAG_GETSOCK, sizeof(*r));
112         r = NLMSG_DATA(nlh);
113         if (sk->sk_state != TCP_TIME_WAIT) {
114                 if (ext & (1<<(TCPDIAG_MEMINFO-1)))
115                         minfo = TCPDIAG_PUT(skb, TCPDIAG_MEMINFO, sizeof(*minfo));
116                 if (ext & (1<<(TCPDIAG_INFO-1)))
117                         info = TCPDIAG_PUT(skb, TCPDIAG_INFO, sizeof(*info));
118                 
119                 if (tcp_is_vegas(tp) && (ext & (1<<(TCPDIAG_VEGASINFO-1))))
120                         vinfo = TCPDIAG_PUT(skb, TCPDIAG_VEGASINFO, sizeof(*vinfo));
121         }
122         r->tcpdiag_family = sk->sk_family;
123         r->tcpdiag_state = sk->sk_state;
124         r->tcpdiag_timer = 0;
125         r->tcpdiag_retrans = 0;
126
127         r->id.tcpdiag_if = sk->sk_bound_dev_if;
128         r->id.tcpdiag_cookie[0] = (u32)(unsigned long)sk;
129         r->id.tcpdiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
130
131         if (r->tcpdiag_state == TCP_TIME_WAIT) {
132                 struct tcp_tw_bucket *tw = (struct tcp_tw_bucket*)sk;
133                 long tmo = tw->tw_ttd - jiffies;
134                 if (tmo < 0)
135                         tmo = 0;
136
137                 r->id.tcpdiag_sport = tw->tw_sport;
138                 r->id.tcpdiag_dport = tw->tw_dport;
139                 r->id.tcpdiag_src[0] = tw->tw_rcv_saddr;
140                 r->id.tcpdiag_dst[0] = tw->tw_daddr;
141                 r->tcpdiag_state = tw->tw_substate;
142                 r->tcpdiag_timer = 3;
143                 r->tcpdiag_expires = (tmo*1000+HZ-1)/HZ;
144                 r->tcpdiag_rqueue = 0;
145                 r->tcpdiag_wqueue = 0;
146                 r->tcpdiag_uid = 0;
147                 r->tcpdiag_inode = 0;
148 #ifdef CONFIG_IPV6
149                 if (r->tcpdiag_family == AF_INET6) {
150                         ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
151                                        &tw->tw_v6_rcv_saddr);
152                         ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
153                                        &tw->tw_v6_daddr);
154                 }
155 #endif
156                 nlh->nlmsg_len = skb->tail - b;
157                 return skb->len;
158         }
159
160         r->id.tcpdiag_sport = inet->sport;
161         r->id.tcpdiag_dport = inet->dport;
162         r->id.tcpdiag_src[0] = inet->rcv_saddr;
163         r->id.tcpdiag_dst[0] = inet->daddr;
164
165 #ifdef CONFIG_IPV6
166         if (r->tcpdiag_family == AF_INET6) {
167                 struct ipv6_pinfo *np = inet6_sk(sk);
168
169                 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_src,
170                                &np->rcv_saddr);
171                 ipv6_addr_copy((struct in6_addr *)r->id.tcpdiag_dst,
172                                &np->daddr);
173         }
174 #endif
175
176 #define EXPIRES_IN_MS(tmo)  ((tmo-jiffies)*1000+HZ-1)/HZ
177
178         if (tp->pending == TCP_TIME_RETRANS) {
179                 r->tcpdiag_timer = 1;
180                 r->tcpdiag_retrans = tp->retransmits;
181                 r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
182         } else if (tp->pending == TCP_TIME_PROBE0) {
183                 r->tcpdiag_timer = 4;
184                 r->tcpdiag_retrans = tp->probes_out;
185                 r->tcpdiag_expires = EXPIRES_IN_MS(tp->timeout);
186         } else if (timer_pending(&sk->sk_timer)) {
187                 r->tcpdiag_timer = 2;
188                 r->tcpdiag_retrans = tp->probes_out;
189                 r->tcpdiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
190         } else {
191                 r->tcpdiag_timer = 0;
192                 r->tcpdiag_expires = 0;
193         }
194 #undef EXPIRES_IN_MS
195
196         r->tcpdiag_rqueue = tp->rcv_nxt - tp->copied_seq;
197         r->tcpdiag_wqueue = tp->write_seq - tp->snd_una;
198         r->tcpdiag_uid = sock_i_uid(sk);
199         r->tcpdiag_inode = sock_i_ino(sk);
200
201         if (minfo) {
202                 minfo->tcpdiag_rmem = atomic_read(&sk->sk_rmem_alloc);
203                 minfo->tcpdiag_wmem = sk->sk_wmem_queued;
204                 minfo->tcpdiag_fmem = sk->sk_forward_alloc;
205                 minfo->tcpdiag_tmem = atomic_read(&sk->sk_wmem_alloc);
206         }
207
208         if (info) 
209                 tcp_get_info(sk, info);
210
211         if (vinfo) {
212                 vinfo->tcpv_enabled = tp->vegas.doing_vegas_now;
213                 vinfo->tcpv_rttcnt = tp->vegas.cntRTT;
214                 vinfo->tcpv_rtt = tp->vegas.baseRTT;
215                 vinfo->tcpv_minrtt = tp->vegas.minRTT;
216         }
217
218         nlh->nlmsg_len = skb->tail - b;
219         return skb->len;
220
221 nlmsg_failure:
222         skb_trim(skb, b - skb->data);
223         return -1;
224 }
225
226 extern struct sock *tcp_v4_lookup(u32 saddr, u16 sport, u32 daddr, u16 dport, int dif);
227 #ifdef CONFIG_IPV6
228 extern struct sock *tcp_v6_lookup(struct in6_addr *saddr, u16 sport,
229                                   struct in6_addr *daddr, u16 dport,
230                                   int dif);
231 #endif
232
233 static int tcpdiag_get_exact(struct sk_buff *in_skb, const struct nlmsghdr *nlh)
234 {
235         int err;
236         struct sock *sk;
237         struct tcpdiagreq *req = NLMSG_DATA(nlh);
238         struct sk_buff *rep;
239
240         if (req->tcpdiag_family == AF_INET) {
241                 sk = tcp_v4_lookup(req->id.tcpdiag_dst[0], req->id.tcpdiag_dport,
242                                    req->id.tcpdiag_src[0], req->id.tcpdiag_sport,
243                                    req->id.tcpdiag_if);
244         }
245 #ifdef CONFIG_IPV6
246         else if (req->tcpdiag_family == AF_INET6) {
247                 sk = tcp_v6_lookup((struct in6_addr*)req->id.tcpdiag_dst, req->id.tcpdiag_dport,
248                                    (struct in6_addr*)req->id.tcpdiag_src, req->id.tcpdiag_sport,
249                                    req->id.tcpdiag_if);
250         }
251 #endif
252         else {
253                 return -EINVAL;
254         }
255
256         if (sk == NULL)
257                 return -ENOENT;
258
259         err = -ESTALE;
260         if ((req->id.tcpdiag_cookie[0] != TCPDIAG_NOCOOKIE ||
261              req->id.tcpdiag_cookie[1] != TCPDIAG_NOCOOKIE) &&
262             ((u32)(unsigned long)sk != req->id.tcpdiag_cookie[0] ||
263              (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.tcpdiag_cookie[1]))
264                 goto out;
265
266         err = -ENOMEM;
267         rep = alloc_skb(NLMSG_SPACE(sizeof(struct tcpdiagmsg)+
268                                     sizeof(struct tcpdiag_meminfo)+
269                                     sizeof(struct tcp_info)+64), GFP_KERNEL);
270         if (!rep)
271                 goto out;
272
273         if (tcpdiag_fill(rep, sk, req->tcpdiag_ext,
274                          NETLINK_CB(in_skb).pid,
275                          nlh->nlmsg_seq) <= 0)
276                 BUG();
277
278         err = netlink_unicast(tcpnl, rep, NETLINK_CB(in_skb).pid, MSG_DONTWAIT);
279         if (err > 0)
280                 err = 0;
281
282 out:
283         if (sk) {
284                 if (sk->sk_state == TCP_TIME_WAIT)
285                         tcp_tw_put((struct tcp_tw_bucket*)sk);
286                 else
287                         sock_put(sk);
288         }
289         return err;
290 }
291
292 static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
293 {
294         int words = bits >> 5;
295
296         bits &= 0x1f;
297
298         if (words) {
299                 if (memcmp(a1, a2, words << 2))
300                         return 0;
301         }
302         if (bits) {
303                 __u32 w1, w2;
304                 __u32 mask;
305
306                 w1 = a1[words];
307                 w2 = a2[words];
308
309                 mask = htonl((0xffffffff) << (32 - bits));
310
311                 if ((w1 ^ w2) & mask)
312                         return 0;
313         }
314
315         return 1;
316 }
317
318
319 static int tcpdiag_bc_run(const void *bc, int len, struct sock *sk)
320 {
321         while (len > 0) {
322                 int yes = 1;
323                 struct inet_opt *inet = inet_sk(sk);
324                 const struct tcpdiag_bc_op *op = bc;
325
326                 switch (op->code) {
327                 case TCPDIAG_BC_NOP:
328                         break;
329                 case TCPDIAG_BC_JMP:
330                         yes = 0;
331                         break;
332                 case TCPDIAG_BC_S_GE:
333                         yes = inet->num >= op[1].no;
334                         break;
335                 case TCPDIAG_BC_S_LE:
336                         yes = inet->num <= op[1].no;
337                         break;
338                 case TCPDIAG_BC_D_GE:
339                         yes = ntohs(inet->dport) >= op[1].no;
340                         break;
341                 case TCPDIAG_BC_D_LE:
342                         yes = ntohs(inet->dport) <= op[1].no;
343                         break;
344                 case TCPDIAG_BC_AUTO:
345                         yes = !(sk->sk_userlocks & SOCK_BINDPORT_LOCK);
346                         break;
347                 case TCPDIAG_BC_S_COND:
348                 case TCPDIAG_BC_D_COND:
349                 {
350                         struct tcpdiag_hostcond *cond = (struct tcpdiag_hostcond*)(op+1);
351                         u32 *addr;
352
353                         if (cond->port != -1 &&
354                             cond->port != (op->code == TCPDIAG_BC_S_COND ?
355                                              inet->num : ntohs(inet->dport))) {
356                                 yes = 0;
357                                 break;
358                         }
359                         
360                         if (cond->prefix_len == 0)
361                                 break;
362
363 #ifdef CONFIG_IPV6
364                         if (sk->sk_family == AF_INET6) {
365                                 struct ipv6_pinfo *np = inet6_sk(sk);
366
367                                 if (op->code == TCPDIAG_BC_S_COND)
368                                         addr = (u32*)&np->rcv_saddr;
369                                 else
370                                         addr = (u32*)&np->daddr;
371                         } else
372 #endif
373                         {
374                                 if (op->code == TCPDIAG_BC_S_COND)
375                                         addr = &inet->rcv_saddr;
376                                 else
377                                         addr = &inet->daddr;
378                         }
379
380                         if (bitstring_match(addr, cond->addr, cond->prefix_len))
381                                 break;
382                         if (sk->sk_family == AF_INET6 &&
383                             cond->family == AF_INET) {
384                                 if (addr[0] == 0 && addr[1] == 0 &&
385                                     addr[2] == htonl(0xffff) &&
386                                     bitstring_match(addr+3, cond->addr, cond->prefix_len))
387                                         break;
388                         }
389                         yes = 0;
390                         break;
391                 }
392                 }
393
394                 if (yes) { 
395                         len -= op->yes;
396                         bc += op->yes;
397                 } else {
398                         len -= op->no;
399                         bc += op->no;
400                 }
401         }
402         return (len == 0);
403 }
404
405 static int valid_cc(const void *bc, int len, int cc)
406 {
407         while (len >= 0) {
408                 const struct tcpdiag_bc_op *op = bc;
409
410                 if (cc > len)
411                         return 0;
412                 if (cc == len)
413                         return 1;
414                 if (op->yes < 4)
415                         return 0;
416                 len -= op->yes;
417                 bc  += op->yes;
418         }
419         return 0;
420 }
421
422 static int tcpdiag_bc_audit(const void *bytecode, int bytecode_len)
423 {
424         const unsigned char *bc = bytecode;
425         int  len = bytecode_len;
426
427         while (len > 0) {
428                 struct tcpdiag_bc_op *op = (struct tcpdiag_bc_op*)bc;
429
430 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
431                 switch (op->code) {
432                 case TCPDIAG_BC_AUTO:
433                 case TCPDIAG_BC_S_COND:
434                 case TCPDIAG_BC_D_COND:
435                 case TCPDIAG_BC_S_GE:
436                 case TCPDIAG_BC_S_LE:
437                 case TCPDIAG_BC_D_GE:
438                 case TCPDIAG_BC_D_LE:
439                         if (op->yes < 4 || op->yes > len+4)
440                                 return -EINVAL;
441                 case TCPDIAG_BC_JMP:
442                         if (op->no < 4 || op->no > len+4)
443                                 return -EINVAL;
444                         if (op->no < len &&
445                             !valid_cc(bytecode, bytecode_len, len-op->no))
446                                 return -EINVAL;
447                         break;
448                 case TCPDIAG_BC_NOP:
449                         if (op->yes < 4 || op->yes > len+4)
450                                 return -EINVAL;
451                         break;
452                 default:
453                         return -EINVAL;
454                 }
455                 bc += op->yes;
456                 len -= op->yes;
457         }
458         return len == 0 ? 0 : -EINVAL;
459 }
460
461
462 static int tcpdiag_dump(struct sk_buff *skb, struct netlink_callback *cb)
463 {
464         int i, num;
465         int s_i, s_num;
466         struct tcpdiagreq *r = NLMSG_DATA(cb->nlh);
467         struct rtattr *bc = NULL;
468
469         if (cb->nlh->nlmsg_len > 4+NLMSG_SPACE(sizeof(struct tcpdiagreq)))
470                 bc = (struct rtattr*)(r+1);
471
472         s_i = cb->args[1];
473         s_num = num = cb->args[2];
474
475         if (cb->args[0] == 0) {
476                 if (!(r->tcpdiag_states&(TCPF_LISTEN|TCPF_SYN_RECV)))
477                         goto skip_listen_ht;
478                 tcp_listen_lock();
479                 for (i = s_i; i < TCP_LHTABLE_SIZE; i++) {
480                         struct sock *sk;
481                         struct hlist_node *node;
482
483                         if (i > s_i)
484                                 s_num = 0;
485
486                         num = 0;
487                         sk_for_each(sk, node, &tcp_listening_hash[i]) {
488                                 struct inet_opt *inet = inet_sk(sk);
489                                 if (num < s_num)
490                                         continue;
491                                 if (!(r->tcpdiag_states&TCPF_LISTEN) ||
492                                     r->id.tcpdiag_dport)
493                                         continue;
494                                 if (r->id.tcpdiag_sport != inet->sport &&
495                                     r->id.tcpdiag_sport)
496                                         continue;
497                                 if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
498                                         continue;
499                                 if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
500                                                  NETLINK_CB(cb->skb).pid,
501                                                  cb->nlh->nlmsg_seq) <= 0) {
502                                         tcp_listen_unlock();
503                                         goto done;
504                                 }
505                                 ++num;
506                         }
507                 }
508                 tcp_listen_unlock();
509 skip_listen_ht:
510                 cb->args[0] = 1;
511                 s_i = num = s_num = 0;
512         }
513
514         if (!(r->tcpdiag_states&~(TCPF_LISTEN|TCPF_SYN_RECV)))
515                 return skb->len;
516
517         for (i = s_i; i < tcp_ehash_size; i++) {
518                 struct tcp_ehash_bucket *head = &tcp_ehash[i];
519                 struct sock *sk;
520                 struct hlist_node *node;
521
522                 if (i > s_i)
523                         s_num = 0;
524
525                 read_lock_bh(&head->lock);
526
527                 num = 0;
528                 sk_for_each(sk, node, &head->chain) {
529                         struct inet_opt *inet = inet_sk(sk);
530
531                         if (num < s_num)
532                                 continue;
533                         if (!(r->tcpdiag_states & (1 << sk->sk_state)))
534                                 continue;
535                         if (r->id.tcpdiag_sport != inet->sport &&
536                             r->id.tcpdiag_sport)
537                                 continue;
538                         if (r->id.tcpdiag_dport != inet->dport && r->id.tcpdiag_dport)
539                                 continue;
540                         if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
541                                 continue;
542                         if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
543                                          NETLINK_CB(cb->skb).pid,
544                                          cb->nlh->nlmsg_seq) <= 0) {
545                                 read_unlock_bh(&head->lock);
546                                 goto done;
547                         }
548                         ++num;
549                 }
550
551                 if (r->tcpdiag_states&TCPF_TIME_WAIT) {
552                         sk_for_each(sk, node,
553                                     &tcp_ehash[i + tcp_ehash_size].chain) {
554                                 struct inet_opt *inet = inet_sk(sk);
555
556                                 if (num < s_num)
557                                         continue;
558                                 if (!(r->tcpdiag_states & (1 << sk->sk_zapped)))
559                                         continue;
560                                 if (r->id.tcpdiag_sport != inet->sport &&
561                                     r->id.tcpdiag_sport)
562                                         continue;
563                                 if (r->id.tcpdiag_dport != inet->dport &&
564                                     r->id.tcpdiag_dport)
565                                         continue;
566                                 if (bc && !tcpdiag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), sk))
567                                         continue;
568                                 if (tcpdiag_fill(skb, sk, r->tcpdiag_ext,
569                                                  NETLINK_CB(cb->skb).pid,
570                                                  cb->nlh->nlmsg_seq) <= 0) {
571                                         read_unlock_bh(&head->lock);
572                                         goto done;
573                                 }
574                                 ++num;
575                         }
576                 }
577                 read_unlock_bh(&head->lock);
578         }
579
580 done:
581         cb->args[1] = i;
582         cb->args[2] = num;
583         return skb->len;
584 }
585
586 static int tcpdiag_dump_done(struct netlink_callback *cb)
587 {
588         return 0;
589 }
590
591
592 static __inline__ int
593 tcpdiag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
594 {
595         if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
596                 return 0;
597
598         if (nlh->nlmsg_type != TCPDIAG_GETSOCK)
599                 goto err_inval;
600
601         if (NLMSG_LENGTH(sizeof(struct tcpdiagreq)) > skb->len)
602                 goto err_inval;
603
604         if (nlh->nlmsg_flags&NLM_F_DUMP) {
605                 if (nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(struct tcpdiagreq))) {
606                         struct rtattr *rta = (struct rtattr*)(NLMSG_DATA(nlh) + sizeof(struct tcpdiagreq));
607                         if (rta->rta_type != TCPDIAG_REQ_BYTECODE ||
608                             rta->rta_len < 8 ||
609                             rta->rta_len > nlh->nlmsg_len - NLMSG_SPACE(sizeof(struct tcpdiagreq)))
610                                 goto err_inval;
611                         if (tcpdiag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
612                                 goto err_inval;
613                 }
614                 return netlink_dump_start(tcpnl, skb, nlh,
615                                           tcpdiag_dump,
616                                           tcpdiag_dump_done);
617         } else {
618                 return tcpdiag_get_exact(skb, nlh);
619         }
620
621 err_inval:
622         return -EINVAL;
623 }
624
625
626 static inline void tcpdiag_rcv_skb(struct sk_buff *skb)
627 {
628         int err;
629         struct nlmsghdr * nlh;
630
631         if (skb->len >= NLMSG_SPACE(0)) {
632                 nlh = (struct nlmsghdr *)skb->data;
633                 if (nlh->nlmsg_len < sizeof(*nlh) || skb->len < nlh->nlmsg_len)
634                         return;
635                 err = tcpdiag_rcv_msg(skb, nlh);
636                 if (err || nlh->nlmsg_flags & NLM_F_ACK) 
637                         netlink_ack(skb, nlh, err);
638         }
639 }
640
641 static void tcpdiag_rcv(struct sock *sk, int len)
642 {
643         struct sk_buff *skb;
644
645         while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
646                 tcpdiag_rcv_skb(skb);
647                 kfree_skb(skb);
648         }
649 }
650
651 void __init tcpdiag_init(void)
652 {
653         tcpnl = netlink_kernel_create(NETLINK_TCPDIAG, tcpdiag_rcv);
654         if (tcpnl == NULL)
655                 panic("tcpdiag_init: Cannot create netlink socket.");
656 }