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