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