Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / net / ipv4 / inet_diag.c
1 /*
2  * inet_diag.c  Module for monitoring INET transport protocols sockets.
3  *
4  * Version:     $Id: inet_diag.c,v 1.3 2002/02/01 22:01:04 davem Exp $
5  *
6  * Authors:     Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
7  *
8  *      This program is free software; you can redistribute it and/or
9  *      modify it under the terms of the GNU General Public License
10  *      as published by the Free Software Foundation; either version
11  *      2 of the License, or (at your option) any later version.
12  */
13
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/types.h>
17 #include <linux/fcntl.h>
18 #include <linux/random.h>
19 #include <linux/cache.h>
20 #include <linux/init.h>
21 #include <linux/time.h>
22
23 #include <net/icmp.h>
24 #include <net/tcp.h>
25 #include <net/ipv6.h>
26 #include <net/inet_common.h>
27 #include <net/inet_connection_sock.h>
28 #include <net/inet_hashtables.h>
29 #include <net/inet_timewait_sock.h>
30 #include <net/inet6_hashtables.h>
31
32 #include <linux/inet.h>
33 #include <linux/stddef.h>
34
35 #include <linux/inet_diag.h>
36
37 static const struct inet_diag_handler **inet_diag_table;
38
39 struct inet_diag_entry {
40         u32 *saddr;
41         u32 *daddr;
42         u16 sport;
43         u16 dport;
44         u16 family;
45         u16 userlocks;
46 };
47
48 static struct sock *idiagnl;
49
50 #define INET_DIAG_PUT(skb, attrtype, attrlen) \
51         RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
52
53 static int inet_csk_diag_fill(struct sock *sk,
54                               struct sk_buff *skb,
55                               int ext, u32 pid, u32 seq, u16 nlmsg_flags,
56                               const struct nlmsghdr *unlh)
57 {
58         const struct inet_sock *inet = inet_sk(sk);
59         const struct inet_connection_sock *icsk = inet_csk(sk);
60         struct inet_diag_msg *r;
61         struct nlmsghdr  *nlh;
62         void *info = NULL;
63         struct inet_diag_meminfo  *minfo = NULL;
64         unsigned char    *b = skb->tail;
65         const struct inet_diag_handler *handler;
66
67         handler = inet_diag_table[unlh->nlmsg_type];
68         BUG_ON(handler == NULL);
69
70         nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
71         nlh->nlmsg_flags = nlmsg_flags;
72
73         r = NLMSG_DATA(nlh);
74         BUG_ON(sk->sk_state == TCP_TIME_WAIT);
75
76         if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
77                 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo));
78
79         if (ext & (1 << (INET_DIAG_INFO - 1)))
80                 info = INET_DIAG_PUT(skb, INET_DIAG_INFO,
81                                      handler->idiag_info_size);
82
83         if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
84                 const size_t len = strlen(icsk->icsk_ca_ops->name);
85
86                 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
87                        icsk->icsk_ca_ops->name);
88         }
89
90         r->idiag_family = sk->sk_family;
91         r->idiag_state = sk->sk_state;
92         r->idiag_timer = 0;
93         r->idiag_retrans = 0;
94
95         r->id.idiag_if = sk->sk_bound_dev_if;
96         r->id.idiag_cookie[0] = (u32)(unsigned long)sk;
97         r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
98
99         r->id.idiag_sport = inet->sport;
100         r->id.idiag_dport = inet->dport;
101         r->id.idiag_src[0] = inet->rcv_saddr;
102         r->id.idiag_dst[0] = inet->daddr;
103
104 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
105         if (r->idiag_family == AF_INET6) {
106                 struct ipv6_pinfo *np = inet6_sk(sk);
107
108                 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
109                                &np->rcv_saddr);
110                 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
111                                &np->daddr);
112         }
113 #endif
114
115 #define EXPIRES_IN_MS(tmo)  ((tmo - jiffies) * 1000 + HZ - 1) / HZ
116
117         if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
118                 r->idiag_timer = 1;
119                 r->idiag_retrans = icsk->icsk_retransmits;
120                 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
121         } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
122                 r->idiag_timer = 4;
123                 r->idiag_retrans = icsk->icsk_probes_out;
124                 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
125         } else if (timer_pending(&sk->sk_timer)) {
126                 r->idiag_timer = 2;
127                 r->idiag_retrans = icsk->icsk_probes_out;
128                 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
129         } else {
130                 r->idiag_timer = 0;
131                 r->idiag_expires = 0;
132         }
133 #undef EXPIRES_IN_MS
134
135         r->idiag_uid = sock_i_uid(sk);
136         r->idiag_inode = sock_i_ino(sk);
137
138         if (minfo) {
139                 minfo->idiag_rmem = atomic_read(&sk->sk_rmem_alloc);
140                 minfo->idiag_wmem = sk->sk_wmem_queued;
141                 minfo->idiag_fmem = sk->sk_forward_alloc;
142                 minfo->idiag_tmem = atomic_read(&sk->sk_wmem_alloc);
143         }
144
145         handler->idiag_get_info(sk, r, info);
146
147         if (sk->sk_state < TCP_TIME_WAIT &&
148             icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
149                 icsk->icsk_ca_ops->get_info(sk, ext, skb);
150
151         nlh->nlmsg_len = skb->tail - b;
152         return skb->len;
153
154 rtattr_failure:
155 nlmsg_failure:
156         skb_trim(skb, b - skb->data);
157         return -1;
158 }
159
160 static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
161                                struct sk_buff *skb, int ext, u32 pid,
162                                u32 seq, u16 nlmsg_flags,
163                                const struct nlmsghdr *unlh)
164 {
165         long tmo;
166         struct inet_diag_msg *r;
167         const unsigned char *previous_tail = skb->tail;
168         struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq,
169                                          unlh->nlmsg_type, sizeof(*r));
170
171         r = NLMSG_DATA(nlh);
172         BUG_ON(tw->tw_state != TCP_TIME_WAIT);
173
174         nlh->nlmsg_flags = nlmsg_flags;
175
176         tmo = tw->tw_ttd - jiffies;
177         if (tmo < 0)
178                 tmo = 0;
179
180         r->idiag_family       = tw->tw_family;
181         r->idiag_state        = tw->tw_state;
182         r->idiag_timer        = 0;
183         r->idiag_retrans      = 0;
184         r->id.idiag_if        = tw->tw_bound_dev_if;
185         r->id.idiag_cookie[0] = (u32)(unsigned long)tw;
186         r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1);
187         r->id.idiag_sport     = tw->tw_sport;
188         r->id.idiag_dport     = tw->tw_dport;
189         r->id.idiag_src[0]    = tw->tw_rcv_saddr;
190         r->id.idiag_dst[0]    = tw->tw_daddr;
191         r->idiag_state        = tw->tw_substate;
192         r->idiag_timer        = 3;
193         r->idiag_expires      = (tmo * 1000 + HZ - 1) / HZ;
194         r->idiag_rqueue       = 0;
195         r->idiag_wqueue       = 0;
196         r->idiag_uid          = 0;
197         r->idiag_inode        = 0;
198 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
199         if (tw->tw_family == AF_INET6) {
200                 const struct inet6_timewait_sock *tw6 =
201                                                 inet6_twsk((struct sock *)tw);
202
203                 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
204                                &tw6->tw_v6_rcv_saddr);
205                 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
206                                &tw6->tw_v6_daddr);
207         }
208 #endif
209         nlh->nlmsg_len = skb->tail - previous_tail;
210         return skb->len;
211 nlmsg_failure:
212         skb_trim(skb, previous_tail - skb->data);
213         return -1;
214 }
215
216 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
217                         int ext, u32 pid, u32 seq, u16 nlmsg_flags,
218                         const struct nlmsghdr *unlh)
219 {
220         if (sk->sk_state == TCP_TIME_WAIT)
221                 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
222                                            skb, ext, pid, seq, nlmsg_flags,
223                                            unlh);
224         return inet_csk_diag_fill(sk, skb, ext, pid, seq, nlmsg_flags, unlh);
225 }
226
227 static int inet_diag_get_exact(struct sk_buff *in_skb,
228                                const struct nlmsghdr *nlh)
229 {
230         int err;
231         struct sock *sk;
232         struct inet_diag_req *req = NLMSG_DATA(nlh);
233         struct sk_buff *rep;
234         struct inet_hashinfo *hashinfo;
235         const struct inet_diag_handler *handler;
236
237         handler = inet_diag_table[nlh->nlmsg_type];
238         BUG_ON(handler == NULL);
239         hashinfo = handler->idiag_hashinfo;
240
241         if (req->idiag_family == AF_INET) {
242                 sk = inet_lookup(hashinfo, req->id.idiag_dst[0],
243                                  req->id.idiag_dport, req->id.idiag_src[0],
244                                  req->id.idiag_sport, req->id.idiag_if);
245         }
246 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
247         else if (req->idiag_family == AF_INET6) {
248                 sk = inet6_lookup(hashinfo,
249                                   (struct in6_addr *)req->id.idiag_dst,
250                                   req->id.idiag_dport,
251                                   (struct in6_addr *)req->id.idiag_src,
252                                   req->id.idiag_sport,
253                                   req->id.idiag_if);
254         }
255 #endif
256         else {
257                 return -EINVAL;
258         }
259
260         if (sk == NULL)
261                 return -ENOENT;
262
263         err = -ESTALE;
264         if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
265              req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
266             ((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
267              (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
268                 goto out;
269
270         err = -ENOMEM;
271         rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
272                                      sizeof(struct inet_diag_meminfo) +
273                                      handler->idiag_info_size + 64)),
274                         GFP_KERNEL);
275         if (!rep)
276                 goto out;
277
278         if (sk_diag_fill(sk, rep, req->idiag_ext,
279                          NETLINK_CB(in_skb).pid,
280                          nlh->nlmsg_seq, 0, nlh) <= 0)
281                 BUG();
282
283         err = netlink_unicast(idiagnl, rep, NETLINK_CB(in_skb).pid,
284                               MSG_DONTWAIT);
285         if (err > 0)
286                 err = 0;
287
288 out:
289         if (sk) {
290                 if (sk->sk_state == TCP_TIME_WAIT)
291                         inet_twsk_put((struct inet_timewait_sock *)sk);
292                 else
293                         sock_put(sk);
294         }
295         return err;
296 }
297
298 static int bitstring_match(const u32 *a1, const u32 *a2, int bits)
299 {
300         int words = bits >> 5;
301
302         bits &= 0x1f;
303
304         if (words) {
305                 if (memcmp(a1, a2, words << 2))
306                         return 0;
307         }
308         if (bits) {
309                 __u32 w1, w2;
310                 __u32 mask;
311
312                 w1 = a1[words];
313                 w2 = a2[words];
314
315                 mask = htonl((0xffffffff) << (32 - bits));
316
317                 if ((w1 ^ w2) & mask)
318                         return 0;
319         }
320
321         return 1;
322 }
323
324
325 static int inet_diag_bc_run(const void *bc, int len,
326                             const struct inet_diag_entry *entry)
327 {
328         while (len > 0) {
329                 int yes = 1;
330                 const struct inet_diag_bc_op *op = bc;
331
332                 switch (op->code) {
333                 case INET_DIAG_BC_NOP:
334                         break;
335                 case INET_DIAG_BC_JMP:
336                         yes = 0;
337                         break;
338                 case INET_DIAG_BC_S_GE:
339                         yes = entry->sport >= op[1].no;
340                         break;
341                 case INET_DIAG_BC_S_LE:
342                         yes = entry->dport <= op[1].no;
343                         break;
344                 case INET_DIAG_BC_D_GE:
345                         yes = entry->dport >= op[1].no;
346                         break;
347                 case INET_DIAG_BC_D_LE:
348                         yes = entry->dport <= op[1].no;
349                         break;
350                 case INET_DIAG_BC_AUTO:
351                         yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
352                         break;
353                 case INET_DIAG_BC_S_COND:
354                 case INET_DIAG_BC_D_COND: {
355                         struct inet_diag_hostcond *cond;
356                         u32 *addr;
357
358                         cond = (struct inet_diag_hostcond *)(op + 1);
359                         if (cond->port != -1 &&
360                             cond->port != (op->code == INET_DIAG_BC_S_COND ?
361                                              entry->sport : entry->dport)) {
362                                 yes = 0;
363                                 break;
364                         }
365
366                         if (cond->prefix_len == 0)
367                                 break;
368
369                         if (op->code == INET_DIAG_BC_S_COND)
370                                 addr = entry->saddr;
371                         else
372                                 addr = entry->daddr;
373
374                         if (bitstring_match(addr, cond->addr,
375                                             cond->prefix_len))
376                                 break;
377                         if (entry->family == AF_INET6 &&
378                             cond->family == AF_INET) {
379                                 if (addr[0] == 0 && addr[1] == 0 &&
380                                     addr[2] == htonl(0xffff) &&
381                                     bitstring_match(addr + 3, cond->addr,
382                                                     cond->prefix_len))
383                                         break;
384                         }
385                         yes = 0;
386                         break;
387                 }
388                 }
389
390                 if (yes) {
391                         len -= op->yes;
392                         bc += op->yes;
393                 } else {
394                         len -= op->no;
395                         bc += op->no;
396                 }
397         }
398         return (len == 0);
399 }
400
401 static int valid_cc(const void *bc, int len, int cc)
402 {
403         while (len >= 0) {
404                 const struct inet_diag_bc_op *op = bc;
405
406                 if (cc > len)
407                         return 0;
408                 if (cc == len)
409                         return 1;
410                 if (op->yes < 4)
411                         return 0;
412                 len -= op->yes;
413                 bc  += op->yes;
414         }
415         return 0;
416 }
417
418 static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
419 {
420         const unsigned char *bc = bytecode;
421         int  len = bytecode_len;
422
423         while (len > 0) {
424                 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)bc;
425
426 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
427                 switch (op->code) {
428                 case INET_DIAG_BC_AUTO:
429                 case INET_DIAG_BC_S_COND:
430                 case INET_DIAG_BC_D_COND:
431                 case INET_DIAG_BC_S_GE:
432                 case INET_DIAG_BC_S_LE:
433                 case INET_DIAG_BC_D_GE:
434                 case INET_DIAG_BC_D_LE:
435                         if (op->yes < 4 || op->yes > len + 4)
436                                 return -EINVAL;
437                 case INET_DIAG_BC_JMP:
438                         if (op->no < 4 || op->no > len + 4)
439                                 return -EINVAL;
440                         if (op->no < len &&
441                             !valid_cc(bytecode, bytecode_len, len - op->no))
442                                 return -EINVAL;
443                         break;
444                 case INET_DIAG_BC_NOP:
445                         if (op->yes < 4 || op->yes > len + 4)
446                                 return -EINVAL;
447                         break;
448                 default:
449                         return -EINVAL;
450                 }
451                 bc  += op->yes;
452                 len -= op->yes;
453         }
454         return len == 0 ? 0 : -EINVAL;
455 }
456
457 static int inet_csk_diag_dump(struct sock *sk,
458                               struct sk_buff *skb,
459                               struct netlink_callback *cb)
460 {
461         struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
462
463         if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
464                 struct inet_diag_entry entry;
465                 struct rtattr *bc = (struct rtattr *)(r + 1);
466                 struct inet_sock *inet = inet_sk(sk);
467
468                 entry.family = sk->sk_family;
469 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
470                 if (entry.family == AF_INET6) {
471                         struct ipv6_pinfo *np = inet6_sk(sk);
472
473                         entry.saddr = np->rcv_saddr.s6_addr32;
474                         entry.daddr = np->daddr.s6_addr32;
475                 } else
476 #endif
477                 {
478                         entry.saddr = &inet->rcv_saddr;
479                         entry.daddr = &inet->daddr;
480                 }
481                 entry.sport = inet->num;
482                 entry.dport = ntohs(inet->dport);
483                 entry.userlocks = sk->sk_userlocks;
484
485                 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
486                         return 0;
487         }
488
489         return inet_csk_diag_fill(sk, skb, r->idiag_ext,
490                                   NETLINK_CB(cb->skb).pid,
491                                   cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
492 }
493
494 static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
495                                struct sk_buff *skb,
496                                struct netlink_callback *cb)
497 {
498         struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
499
500         if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
501                 struct inet_diag_entry entry;
502                 struct rtattr *bc = (struct rtattr *)(r + 1);
503
504                 entry.family = tw->tw_family;
505 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
506                 if (tw->tw_family == AF_INET6) {
507                         struct inet6_timewait_sock *tw6 =
508                                                 inet6_twsk((struct sock *)tw);
509                         entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
510                         entry.daddr = tw6->tw_v6_daddr.s6_addr32;
511                 } else
512 #endif
513                 {
514                         entry.saddr = &tw->tw_rcv_saddr;
515                         entry.daddr = &tw->tw_daddr;
516                 }
517                 entry.sport = tw->tw_num;
518                 entry.dport = ntohs(tw->tw_dport);
519                 entry.userlocks = 0; 
520
521                 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
522                         return 0;
523         }
524
525         return inet_twsk_diag_fill(tw, skb, r->idiag_ext,
526                                    NETLINK_CB(cb->skb).pid,
527                                    cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
528 }
529
530 static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
531                               struct request_sock *req, u32 pid, u32 seq,
532                               const struct nlmsghdr *unlh)
533 {
534         const struct inet_request_sock *ireq = inet_rsk(req);
535         struct inet_sock *inet = inet_sk(sk);
536         unsigned char *b = skb->tail;
537         struct inet_diag_msg *r;
538         struct nlmsghdr *nlh;
539         long tmo;
540
541         nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
542         nlh->nlmsg_flags = NLM_F_MULTI;
543         r = NLMSG_DATA(nlh);
544
545         r->idiag_family = sk->sk_family;
546         r->idiag_state = TCP_SYN_RECV;
547         r->idiag_timer = 1;
548         r->idiag_retrans = req->retrans;
549
550         r->id.idiag_if = sk->sk_bound_dev_if;
551         r->id.idiag_cookie[0] = (u32)(unsigned long)req;
552         r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
553
554         tmo = req->expires - jiffies;
555         if (tmo < 0)
556                 tmo = 0;
557
558         r->id.idiag_sport = inet->sport;
559         r->id.idiag_dport = ireq->rmt_port;
560         r->id.idiag_src[0] = ireq->loc_addr;
561         r->id.idiag_dst[0] = ireq->rmt_addr;
562         r->idiag_expires = jiffies_to_msecs(tmo);
563         r->idiag_rqueue = 0;
564         r->idiag_wqueue = 0;
565         r->idiag_uid = sock_i_uid(sk);
566         r->idiag_inode = 0;
567 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
568         if (r->idiag_family == AF_INET6) {
569                 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
570                                &inet6_rsk(req)->loc_addr);
571                 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
572                                &inet6_rsk(req)->rmt_addr);
573         }
574 #endif
575         nlh->nlmsg_len = skb->tail - b;
576
577         return skb->len;
578
579 nlmsg_failure:
580         skb_trim(skb, b - skb->data);
581         return -1;
582 }
583
584 static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
585                                struct netlink_callback *cb)
586 {
587         struct inet_diag_entry entry;
588         struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
589         struct inet_connection_sock *icsk = inet_csk(sk);
590         struct listen_sock *lopt;
591         struct rtattr *bc = NULL;
592         struct inet_sock *inet = inet_sk(sk);
593         int j, s_j;
594         int reqnum, s_reqnum;
595         int err = 0;
596
597         s_j = cb->args[3];
598         s_reqnum = cb->args[4];
599
600         if (s_j > 0)
601                 s_j--;
602
603         entry.family = sk->sk_family;
604
605         read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
606
607         lopt = icsk->icsk_accept_queue.listen_opt;
608         if (!lopt || !lopt->qlen)
609                 goto out;
610
611         if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
612                 bc = (struct rtattr *)(r + 1);
613                 entry.sport = inet->num;
614                 entry.userlocks = sk->sk_userlocks;
615         }
616
617         for (j = s_j; j < lopt->nr_table_entries; j++) {
618                 struct request_sock *req, *head = lopt->syn_table[j];
619
620                 reqnum = 0;
621                 for (req = head; req; reqnum++, req = req->dl_next) {
622                         struct inet_request_sock *ireq = inet_rsk(req);
623
624                         if (reqnum < s_reqnum)
625                                 continue;
626                         if (r->id.idiag_dport != ireq->rmt_port &&
627                             r->id.idiag_dport)
628                                 continue;
629
630                         if (bc) {
631                                 entry.saddr =
632 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
633                                         (entry.family == AF_INET6) ?
634                                         inet6_rsk(req)->loc_addr.s6_addr32 :
635 #endif
636                                         &ireq->loc_addr;
637                                 entry.daddr =
638 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
639                                         (entry.family == AF_INET6) ?
640                                         inet6_rsk(req)->rmt_addr.s6_addr32 :
641 #endif
642                                         &ireq->rmt_addr;
643                                 entry.dport = ntohs(ireq->rmt_port);
644
645                                 if (!inet_diag_bc_run(RTA_DATA(bc),
646                                                     RTA_PAYLOAD(bc), &entry))
647                                         continue;
648                         }
649
650                         err = inet_diag_fill_req(skb, sk, req,
651                                                NETLINK_CB(cb->skb).pid,
652                                                cb->nlh->nlmsg_seq, cb->nlh);
653                         if (err < 0) {
654                                 cb->args[3] = j + 1;
655                                 cb->args[4] = reqnum;
656                                 goto out;
657                         }
658                 }
659
660                 s_reqnum = 0;
661         }
662
663 out:
664         read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
665
666         return err;
667 }
668
669 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
670 {
671         int i, num;
672         int s_i, s_num;
673         struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
674         const struct inet_diag_handler *handler;
675         struct inet_hashinfo *hashinfo;
676
677         handler = inet_diag_table[cb->nlh->nlmsg_type];
678         BUG_ON(handler == NULL);
679         hashinfo = handler->idiag_hashinfo;
680
681         s_i = cb->args[1];
682         s_num = num = cb->args[2];
683
684         if (cb->args[0] == 0) {
685                 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
686                         goto skip_listen_ht;
687
688                 inet_listen_lock(hashinfo);
689                 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
690                         struct sock *sk;
691                         struct hlist_node *node;
692
693                         num = 0;
694                         sk_for_each(sk, node, &hashinfo->listening_hash[i]) {
695                                 struct inet_sock *inet = inet_sk(sk);
696
697                                 if (!vx_check(sk->sk_xid, VX_IDENT|VX_WATCH))
698                                         continue;
699                                 if (num < s_num) {
700                                         num++;
701                                         continue;
702                                 }
703
704                                 if (r->id.idiag_sport != inet->sport &&
705                                     r->id.idiag_sport)
706                                         goto next_listen;
707
708                                 if (!(r->idiag_states & TCPF_LISTEN) ||
709                                     r->id.idiag_dport ||
710                                     cb->args[3] > 0)
711                                         goto syn_recv;
712
713                                 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
714                                         inet_listen_unlock(hashinfo);
715                                         goto done;
716                                 }
717
718 syn_recv:
719                                 if (!(r->idiag_states & TCPF_SYN_RECV))
720                                         goto next_listen;
721
722                                 if (inet_diag_dump_reqs(skb, sk, cb) < 0) {
723                                         inet_listen_unlock(hashinfo);
724                                         goto done;
725                                 }
726
727 next_listen:
728                                 cb->args[3] = 0;
729                                 cb->args[4] = 0;
730                                 ++num;
731                         }
732
733                         s_num = 0;
734                         cb->args[3] = 0;
735                         cb->args[4] = 0;
736                 }
737                 inet_listen_unlock(hashinfo);
738 skip_listen_ht:
739                 cb->args[0] = 1;
740                 s_i = num = s_num = 0;
741         }
742
743         if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
744                 return skb->len;
745
746         for (i = s_i; i < hashinfo->ehash_size; i++) {
747                 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
748                 struct sock *sk;
749                 struct hlist_node *node;
750
751                 if (i > s_i)
752                         s_num = 0;
753
754                 read_lock_bh(&head->lock);
755                 num = 0;
756                 sk_for_each(sk, node, &head->chain) {
757                         struct inet_sock *inet = inet_sk(sk);
758
759                         if (!vx_check(sk->sk_xid, VX_IDENT|VX_WATCH))
760                                 continue;
761                         if (num < s_num)
762                                 goto next_normal;
763                         if (!(r->idiag_states & (1 << sk->sk_state)))
764                                 goto next_normal;
765                         if (r->id.idiag_sport != inet->sport &&
766                             r->id.idiag_sport)
767                                 goto next_normal;
768                         if (r->id.idiag_dport != inet->dport &&
769                             r->id.idiag_dport)
770                                 goto next_normal;
771                         if (inet_csk_diag_dump(sk, skb, cb) < 0) {
772                                 read_unlock_bh(&head->lock);
773                                 goto done;
774                         }
775 next_normal:
776                         ++num;
777                 }
778
779                 if (r->idiag_states & TCPF_TIME_WAIT) {
780                         struct inet_timewait_sock *tw;
781
782                         inet_twsk_for_each(tw, node,
783                                     &hashinfo->ehash[i + hashinfo->ehash_size].chain) {
784
785                                 if (!vx_check(tw->tw_xid, VX_IDENT|VX_WATCH))
786                                         continue;
787                                 if (num < s_num)
788                                         goto next_dying;
789                                 if (r->id.idiag_sport != tw->tw_sport &&
790                                     r->id.idiag_sport)
791                                         goto next_dying;
792                                 if (r->id.idiag_dport != tw->tw_dport &&
793                                     r->id.idiag_dport)
794                                         goto next_dying;
795                                 if (inet_twsk_diag_dump(tw, skb, cb) < 0) {
796                                         read_unlock_bh(&head->lock);
797                                         goto done;
798                                 }
799 next_dying:
800                                 ++num;
801                         }
802                 }
803                 read_unlock_bh(&head->lock);
804         }
805
806 done:
807         cb->args[1] = i;
808         cb->args[2] = num;
809         return skb->len;
810 }
811
812 static inline int inet_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
813 {
814         if (!(nlh->nlmsg_flags&NLM_F_REQUEST))
815                 return 0;
816
817         if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX)
818                 goto err_inval;
819
820         if (inet_diag_table[nlh->nlmsg_type] == NULL)
821                 return -ENOENT;
822
823         if (NLMSG_LENGTH(sizeof(struct inet_diag_req)) > skb->len)
824                 goto err_inval;
825
826         if (nlh->nlmsg_flags&NLM_F_DUMP) {
827                 if (nlh->nlmsg_len >
828                     (4 + NLMSG_SPACE(sizeof(struct inet_diag_req)))) {
829                         struct rtattr *rta = (void *)(NLMSG_DATA(nlh) +
830                                                  sizeof(struct inet_diag_req));
831                         if (rta->rta_type != INET_DIAG_REQ_BYTECODE ||
832                             rta->rta_len < 8 ||
833                             rta->rta_len >
834                             (nlh->nlmsg_len -
835                              NLMSG_SPACE(sizeof(struct inet_diag_req))))
836                                 goto err_inval;
837                         if (inet_diag_bc_audit(RTA_DATA(rta), RTA_PAYLOAD(rta)))
838                                 goto err_inval;
839                 }
840                 return netlink_dump_start(idiagnl, skb, nlh,
841                                           inet_diag_dump, NULL);
842         } else
843                 return inet_diag_get_exact(skb, nlh);
844
845 err_inval:
846         return -EINVAL;
847 }
848
849
850 static inline void inet_diag_rcv_skb(struct sk_buff *skb)
851 {
852         if (skb->len >= NLMSG_SPACE(0)) {
853                 int err;
854                 struct nlmsghdr *nlh = (struct nlmsghdr *)skb->data;
855
856                 if (nlh->nlmsg_len < sizeof(*nlh) ||
857                     skb->len < nlh->nlmsg_len)
858                         return;
859                 err = inet_diag_rcv_msg(skb, nlh);
860                 if (err || nlh->nlmsg_flags & NLM_F_ACK)
861                         netlink_ack(skb, nlh, err);
862         }
863 }
864
865 static void inet_diag_rcv(struct sock *sk, int len)
866 {
867         struct sk_buff *skb;
868         unsigned int qlen = skb_queue_len(&sk->sk_receive_queue);
869
870         while (qlen-- && (skb = skb_dequeue(&sk->sk_receive_queue))) {
871                 inet_diag_rcv_skb(skb);
872                 kfree_skb(skb);
873         }
874 }
875
876 static DEFINE_SPINLOCK(inet_diag_register_lock);
877
878 int inet_diag_register(const struct inet_diag_handler *h)
879 {
880         const __u16 type = h->idiag_type;
881         int err = -EINVAL;
882
883         if (type >= INET_DIAG_GETSOCK_MAX)
884                 goto out;
885
886         spin_lock(&inet_diag_register_lock);
887         err = -EEXIST;
888         if (inet_diag_table[type] == NULL) {
889                 inet_diag_table[type] = h;
890                 err = 0;
891         }
892         spin_unlock(&inet_diag_register_lock);
893 out:
894         return err;
895 }
896 EXPORT_SYMBOL_GPL(inet_diag_register);
897
898 void inet_diag_unregister(const struct inet_diag_handler *h)
899 {
900         const __u16 type = h->idiag_type;
901
902         if (type >= INET_DIAG_GETSOCK_MAX)
903                 return;
904
905         spin_lock(&inet_diag_register_lock);
906         inet_diag_table[type] = NULL;
907         spin_unlock(&inet_diag_register_lock);
908
909         synchronize_rcu();
910 }
911 EXPORT_SYMBOL_GPL(inet_diag_unregister);
912
913 static int __init inet_diag_init(void)
914 {
915         const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
916                                           sizeof(struct inet_diag_handler *));
917         int err = -ENOMEM;
918
919         inet_diag_table = kmalloc(inet_diag_table_size, GFP_KERNEL);
920         if (!inet_diag_table)
921                 goto out;
922
923         memset(inet_diag_table, 0, inet_diag_table_size);
924         idiagnl = netlink_kernel_create(NETLINK_INET_DIAG, 0, inet_diag_rcv,
925                                         THIS_MODULE);
926         if (idiagnl == NULL)
927                 goto out_free_table;
928         err = 0;
929 out:
930         return err;
931 out_free_table:
932         kfree(inet_diag_table);
933         goto out;
934 }
935
936 static void __exit inet_diag_exit(void)
937 {
938         sock_release(idiagnl->sk_socket);
939         kfree(inet_diag_table);
940 }
941
942 module_init(inet_diag_init);
943 module_exit(inet_diag_exit);
944 MODULE_LICENSE("GPL");