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