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