Cache xtime every call to update_wall_time
[usb.git] / net / ipv4 / inet_diag.c
blobdbeacd8b0f9059c1b738c820e2bcd10aa32bd9e2
1 /*
2 * inet_diag.c Module for monitoring INET transport protocols sockets.
4 * Version: $Id: inet_diag.c,v 1.3 2002/02/01 22:01:04 davem Exp $
6 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/fcntl.h>
17 #include <linux/random.h>
18 #include <linux/cache.h>
19 #include <linux/init.h>
20 #include <linux/time.h>
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>
32 #include <linux/inet.h>
33 #include <linux/stddef.h>
35 #include <linux/inet_diag.h>
37 static const struct inet_diag_handler **inet_diag_table;
39 struct inet_diag_entry {
40 __be32 *saddr;
41 __be32 *daddr;
42 u16 sport;
43 u16 dport;
44 u16 family;
45 u16 userlocks;
48 static struct sock *idiagnl;
50 #define INET_DIAG_PUT(skb, attrtype, attrlen) \
51 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
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)
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_pointer(skb);
65 const struct inet_diag_handler *handler;
67 handler = inet_diag_table[unlh->nlmsg_type];
68 BUG_ON(handler == NULL);
70 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
71 nlh->nlmsg_flags = nlmsg_flags;
73 r = NLMSG_DATA(nlh);
74 BUG_ON(sk->sk_state == TCP_TIME_WAIT);
76 if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
77 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo));
79 if (ext & (1 << (INET_DIAG_INFO - 1)))
80 info = INET_DIAG_PUT(skb, INET_DIAG_INFO,
81 handler->idiag_info_size);
83 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
84 const size_t len = strlen(icsk->icsk_ca_ops->name);
86 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
87 icsk->icsk_ca_ops->name);
90 r->idiag_family = sk->sk_family;
91 r->idiag_state = sk->sk_state;
92 r->idiag_timer = 0;
93 r->idiag_retrans = 0;
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);
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;
104 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
105 if (r->idiag_family == AF_INET6) {
106 struct ipv6_pinfo *np = inet6_sk(sk);
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);
113 #endif
115 #define EXPIRES_IN_MS(tmo) ((tmo - jiffies) * 1000 + HZ - 1) / HZ
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;
133 #undef EXPIRES_IN_MS
135 r->idiag_uid = sock_i_uid(sk);
136 r->idiag_inode = sock_i_ino(sk);
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);
145 handler->idiag_get_info(sk, r, info);
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);
151 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
152 return skb->len;
154 rtattr_failure:
155 nlmsg_failure:
156 nlmsg_trim(skb, b);
157 return -EMSGSIZE;
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)
165 long tmo;
166 struct inet_diag_msg *r;
167 const unsigned char *previous_tail = skb_tail_pointer(skb);
168 struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq,
169 unlh->nlmsg_type, sizeof(*r));
171 r = NLMSG_DATA(nlh);
172 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
174 nlh->nlmsg_flags = nlmsg_flags;
176 tmo = tw->tw_ttd - jiffies;
177 if (tmo < 0)
178 tmo = 0;
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);
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);
208 #endif
209 nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail;
210 return skb->len;
211 nlmsg_failure:
212 nlmsg_trim(skb, previous_tail);
213 return -EMSGSIZE;
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)
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);
227 static int inet_diag_get_exact(struct sk_buff *in_skb,
228 const struct nlmsghdr *nlh)
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;
237 handler = inet_diag_table[nlh->nlmsg_type];
238 BUG_ON(handler == NULL);
239 hashinfo = handler->idiag_hashinfo;
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);
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);
255 #endif
256 else {
257 return -EINVAL;
260 if (sk == NULL)
261 return -ENOENT;
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;
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;
278 err = sk_diag_fill(sk, rep, req->idiag_ext,
279 NETLINK_CB(in_skb).pid,
280 nlh->nlmsg_seq, 0, nlh);
281 if (err < 0) {
282 WARN_ON(err == -EMSGSIZE);
283 kfree_skb(rep);
284 goto out;
286 err = netlink_unicast(idiagnl, rep, NETLINK_CB(in_skb).pid,
287 MSG_DONTWAIT);
288 if (err > 0)
289 err = 0;
291 out:
292 if (sk) {
293 if (sk->sk_state == TCP_TIME_WAIT)
294 inet_twsk_put((struct inet_timewait_sock *)sk);
295 else
296 sock_put(sk);
298 return err;
301 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
303 int words = bits >> 5;
305 bits &= 0x1f;
307 if (words) {
308 if (memcmp(a1, a2, words << 2))
309 return 0;
311 if (bits) {
312 __be32 w1, w2;
313 __be32 mask;
315 w1 = a1[words];
316 w2 = a2[words];
318 mask = htonl((0xffffffff) << (32 - bits));
320 if ((w1 ^ w2) & mask)
321 return 0;
324 return 1;
328 static int inet_diag_bc_run(const void *bc, int len,
329 const struct inet_diag_entry *entry)
331 while (len > 0) {
332 int yes = 1;
333 const struct inet_diag_bc_op *op = bc;
335 switch (op->code) {
336 case INET_DIAG_BC_NOP:
337 break;
338 case INET_DIAG_BC_JMP:
339 yes = 0;
340 break;
341 case INET_DIAG_BC_S_GE:
342 yes = entry->sport >= op[1].no;
343 break;
344 case INET_DIAG_BC_S_LE:
345 yes = entry->dport <= op[1].no;
346 break;
347 case INET_DIAG_BC_D_GE:
348 yes = entry->dport >= op[1].no;
349 break;
350 case INET_DIAG_BC_D_LE:
351 yes = entry->dport <= op[1].no;
352 break;
353 case INET_DIAG_BC_AUTO:
354 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
355 break;
356 case INET_DIAG_BC_S_COND:
357 case INET_DIAG_BC_D_COND: {
358 struct inet_diag_hostcond *cond;
359 __be32 *addr;
361 cond = (struct inet_diag_hostcond *)(op + 1);
362 if (cond->port != -1 &&
363 cond->port != (op->code == INET_DIAG_BC_S_COND ?
364 entry->sport : entry->dport)) {
365 yes = 0;
366 break;
369 if (cond->prefix_len == 0)
370 break;
372 if (op->code == INET_DIAG_BC_S_COND)
373 addr = entry->saddr;
374 else
375 addr = entry->daddr;
377 if (bitstring_match(addr, cond->addr,
378 cond->prefix_len))
379 break;
380 if (entry->family == AF_INET6 &&
381 cond->family == AF_INET) {
382 if (addr[0] == 0 && addr[1] == 0 &&
383 addr[2] == htonl(0xffff) &&
384 bitstring_match(addr + 3, cond->addr,
385 cond->prefix_len))
386 break;
388 yes = 0;
389 break;
393 if (yes) {
394 len -= op->yes;
395 bc += op->yes;
396 } else {
397 len -= op->no;
398 bc += op->no;
401 return (len == 0);
404 static int valid_cc(const void *bc, int len, int cc)
406 while (len >= 0) {
407 const struct inet_diag_bc_op *op = bc;
409 if (cc > len)
410 return 0;
411 if (cc == len)
412 return 1;
413 if (op->yes < 4)
414 return 0;
415 len -= op->yes;
416 bc += op->yes;
418 return 0;
421 static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
423 const unsigned char *bc = bytecode;
424 int len = bytecode_len;
426 while (len > 0) {
427 struct inet_diag_bc_op *op = (struct inet_diag_bc_op *)bc;
429 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
430 switch (op->code) {
431 case INET_DIAG_BC_AUTO:
432 case INET_DIAG_BC_S_COND:
433 case INET_DIAG_BC_D_COND:
434 case INET_DIAG_BC_S_GE:
435 case INET_DIAG_BC_S_LE:
436 case INET_DIAG_BC_D_GE:
437 case INET_DIAG_BC_D_LE:
438 if (op->yes < 4 || op->yes > len + 4)
439 return -EINVAL;
440 case INET_DIAG_BC_JMP:
441 if (op->no < 4 || op->no > len + 4)
442 return -EINVAL;
443 if (op->no < len &&
444 !valid_cc(bytecode, bytecode_len, len - op->no))
445 return -EINVAL;
446 break;
447 case INET_DIAG_BC_NOP:
448 if (op->yes < 4 || op->yes > len + 4)
449 return -EINVAL;
450 break;
451 default:
452 return -EINVAL;
454 bc += op->yes;
455 len -= op->yes;
457 return len == 0 ? 0 : -EINVAL;
460 static int inet_csk_diag_dump(struct sock *sk,
461 struct sk_buff *skb,
462 struct netlink_callback *cb)
464 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
466 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
467 struct inet_diag_entry entry;
468 struct rtattr *bc = (struct rtattr *)(r + 1);
469 struct inet_sock *inet = inet_sk(sk);
471 entry.family = sk->sk_family;
472 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
473 if (entry.family == AF_INET6) {
474 struct ipv6_pinfo *np = inet6_sk(sk);
476 entry.saddr = np->rcv_saddr.s6_addr32;
477 entry.daddr = np->daddr.s6_addr32;
478 } else
479 #endif
481 entry.saddr = &inet->rcv_saddr;
482 entry.daddr = &inet->daddr;
484 entry.sport = inet->num;
485 entry.dport = ntohs(inet->dport);
486 entry.userlocks = sk->sk_userlocks;
488 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
489 return 0;
492 return inet_csk_diag_fill(sk, skb, r->idiag_ext,
493 NETLINK_CB(cb->skb).pid,
494 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
497 static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
498 struct sk_buff *skb,
499 struct netlink_callback *cb)
501 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
503 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
504 struct inet_diag_entry entry;
505 struct rtattr *bc = (struct rtattr *)(r + 1);
507 entry.family = tw->tw_family;
508 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
509 if (tw->tw_family == AF_INET6) {
510 struct inet6_timewait_sock *tw6 =
511 inet6_twsk((struct sock *)tw);
512 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
513 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
514 } else
515 #endif
517 entry.saddr = &tw->tw_rcv_saddr;
518 entry.daddr = &tw->tw_daddr;
520 entry.sport = tw->tw_num;
521 entry.dport = ntohs(tw->tw_dport);
522 entry.userlocks = 0;
524 if (!inet_diag_bc_run(RTA_DATA(bc), RTA_PAYLOAD(bc), &entry))
525 return 0;
528 return inet_twsk_diag_fill(tw, skb, r->idiag_ext,
529 NETLINK_CB(cb->skb).pid,
530 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
533 static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
534 struct request_sock *req, u32 pid, u32 seq,
535 const struct nlmsghdr *unlh)
537 const struct inet_request_sock *ireq = inet_rsk(req);
538 struct inet_sock *inet = inet_sk(sk);
539 unsigned char *b = skb_tail_pointer(skb);
540 struct inet_diag_msg *r;
541 struct nlmsghdr *nlh;
542 long tmo;
544 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
545 nlh->nlmsg_flags = NLM_F_MULTI;
546 r = NLMSG_DATA(nlh);
548 r->idiag_family = sk->sk_family;
549 r->idiag_state = TCP_SYN_RECV;
550 r->idiag_timer = 1;
551 r->idiag_retrans = req->retrans;
553 r->id.idiag_if = sk->sk_bound_dev_if;
554 r->id.idiag_cookie[0] = (u32)(unsigned long)req;
555 r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
557 tmo = req->expires - jiffies;
558 if (tmo < 0)
559 tmo = 0;
561 r->id.idiag_sport = inet->sport;
562 r->id.idiag_dport = ireq->rmt_port;
563 r->id.idiag_src[0] = ireq->loc_addr;
564 r->id.idiag_dst[0] = ireq->rmt_addr;
565 r->idiag_expires = jiffies_to_msecs(tmo);
566 r->idiag_rqueue = 0;
567 r->idiag_wqueue = 0;
568 r->idiag_uid = sock_i_uid(sk);
569 r->idiag_inode = 0;
570 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
571 if (r->idiag_family == AF_INET6) {
572 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
573 &inet6_rsk(req)->loc_addr);
574 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
575 &inet6_rsk(req)->rmt_addr);
577 #endif
578 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
580 return skb->len;
582 nlmsg_failure:
583 nlmsg_trim(skb, b);
584 return -1;
587 static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
588 struct netlink_callback *cb)
590 struct inet_diag_entry entry;
591 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
592 struct inet_connection_sock *icsk = inet_csk(sk);
593 struct listen_sock *lopt;
594 struct rtattr *bc = NULL;
595 struct inet_sock *inet = inet_sk(sk);
596 int j, s_j;
597 int reqnum, s_reqnum;
598 int err = 0;
600 s_j = cb->args[3];
601 s_reqnum = cb->args[4];
603 if (s_j > 0)
604 s_j--;
606 entry.family = sk->sk_family;
608 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
610 lopt = icsk->icsk_accept_queue.listen_opt;
611 if (!lopt || !lopt->qlen)
612 goto out;
614 if (cb->nlh->nlmsg_len > 4 + NLMSG_SPACE(sizeof(*r))) {
615 bc = (struct rtattr *)(r + 1);
616 entry.sport = inet->num;
617 entry.userlocks = sk->sk_userlocks;
620 for (j = s_j; j < lopt->nr_table_entries; j++) {
621 struct request_sock *req, *head = lopt->syn_table[j];
623 reqnum = 0;
624 for (req = head; req; reqnum++, req = req->dl_next) {
625 struct inet_request_sock *ireq = inet_rsk(req);
627 if (reqnum < s_reqnum)
628 continue;
629 if (r->id.idiag_dport != ireq->rmt_port &&
630 r->id.idiag_dport)
631 continue;
633 if (bc) {
634 entry.saddr =
635 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
636 (entry.family == AF_INET6) ?
637 inet6_rsk(req)->loc_addr.s6_addr32 :
638 #endif
639 &ireq->loc_addr;
640 entry.daddr =
641 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
642 (entry.family == AF_INET6) ?
643 inet6_rsk(req)->rmt_addr.s6_addr32 :
644 #endif
645 &ireq->rmt_addr;
646 entry.dport = ntohs(ireq->rmt_port);
648 if (!inet_diag_bc_run(RTA_DATA(bc),
649 RTA_PAYLOAD(bc), &entry))
650 continue;
653 err = inet_diag_fill_req(skb, sk, req,
654 NETLINK_CB(cb->skb).pid,
655 cb->nlh->nlmsg_seq, cb->nlh);
656 if (err < 0) {
657 cb->args[3] = j + 1;
658 cb->args[4] = reqnum;
659 goto out;
663 s_reqnum = 0;
666 out:
667 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
669 return err;
672 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
674 int i, num;
675 int s_i, s_num;
676 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
677 const struct inet_diag_handler *handler;
678 struct inet_hashinfo *hashinfo;
680 handler = inet_diag_table[cb->nlh->nlmsg_type];
681 BUG_ON(handler == NULL);
682 hashinfo = handler->idiag_hashinfo;
684 s_i = cb->args[1];
685 s_num = num = cb->args[2];
687 if (cb->args[0] == 0) {
688 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
689 goto skip_listen_ht;
691 inet_listen_lock(hashinfo);
692 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
693 struct sock *sk;
694 struct hlist_node *node;
696 num = 0;
697 sk_for_each(sk, node, &hashinfo->listening_hash[i]) {
698 struct inet_sock *inet = inet_sk(sk);
700 if (num < s_num) {
701 num++;
702 continue;
705 if (r->id.idiag_sport != inet->sport &&
706 r->id.idiag_sport)
707 goto next_listen;
709 if (!(r->idiag_states & TCPF_LISTEN) ||
710 r->id.idiag_dport ||
711 cb->args[3] > 0)
712 goto syn_recv;
714 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
715 inet_listen_unlock(hashinfo);
716 goto done;
719 syn_recv:
720 if (!(r->idiag_states & TCPF_SYN_RECV))
721 goto next_listen;
723 if (inet_diag_dump_reqs(skb, sk, cb) < 0) {
724 inet_listen_unlock(hashinfo);
725 goto done;
728 next_listen:
729 cb->args[3] = 0;
730 cb->args[4] = 0;
731 ++num;
734 s_num = 0;
735 cb->args[3] = 0;
736 cb->args[4] = 0;
738 inet_listen_unlock(hashinfo);
739 skip_listen_ht:
740 cb->args[0] = 1;
741 s_i = num = s_num = 0;
744 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
745 return skb->len;
747 for (i = s_i; i < hashinfo->ehash_size; i++) {
748 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
749 struct sock *sk;
750 struct hlist_node *node;
752 if (i > s_i)
753 s_num = 0;
755 read_lock_bh(&head->lock);
756 num = 0;
757 sk_for_each(sk, node, &head->chain) {
758 struct inet_sock *inet = inet_sk(sk);
760 if (num < s_num)
761 goto next_normal;
762 if (!(r->idiag_states & (1 << sk->sk_state)))
763 goto next_normal;
764 if (r->id.idiag_sport != inet->sport &&
765 r->id.idiag_sport)
766 goto next_normal;
767 if (r->id.idiag_dport != inet->dport &&
768 r->id.idiag_dport)
769 goto next_normal;
770 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
771 read_unlock_bh(&head->lock);
772 goto done;
774 next_normal:
775 ++num;
778 if (r->idiag_states & TCPF_TIME_WAIT) {
779 struct inet_timewait_sock *tw;
781 inet_twsk_for_each(tw, node,
782 &head->twchain) {
784 if (num < s_num)
785 goto next_dying;
786 if (r->id.idiag_sport != tw->tw_sport &&
787 r->id.idiag_sport)
788 goto next_dying;
789 if (r->id.idiag_dport != tw->tw_dport &&
790 r->id.idiag_dport)
791 goto next_dying;
792 if (inet_twsk_diag_dump(tw, skb, cb) < 0) {
793 read_unlock_bh(&head->lock);
794 goto done;
796 next_dying:
797 ++num;
800 read_unlock_bh(&head->lock);
803 done:
804 cb->args[1] = i;
805 cb->args[2] = num;
806 return skb->len;
809 static int inet_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
811 int hdrlen = sizeof(struct inet_diag_req);
813 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
814 nlmsg_len(nlh) < hdrlen)
815 return -EINVAL;
817 if (inet_diag_table[nlh->nlmsg_type] == NULL)
818 return -ENOENT;
820 if (nlh->nlmsg_flags & NLM_F_DUMP) {
821 if (nlmsg_attrlen(nlh, hdrlen)) {
822 struct nlattr *attr;
824 attr = nlmsg_find_attr(nlh, hdrlen,
825 INET_DIAG_REQ_BYTECODE);
826 if (attr == NULL ||
827 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
828 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
829 return -EINVAL;
832 return netlink_dump_start(idiagnl, skb, nlh,
833 inet_diag_dump, NULL);
836 return inet_diag_get_exact(skb, nlh);
839 static void inet_diag_rcv(struct sock *sk, int len)
841 unsigned int qlen = 0;
843 do {
844 netlink_run_queue(sk, &qlen, &inet_diag_rcv_msg);
845 } while (qlen);
848 static DEFINE_SPINLOCK(inet_diag_register_lock);
850 int inet_diag_register(const struct inet_diag_handler *h)
852 const __u16 type = h->idiag_type;
853 int err = -EINVAL;
855 if (type >= INET_DIAG_GETSOCK_MAX)
856 goto out;
858 spin_lock(&inet_diag_register_lock);
859 err = -EEXIST;
860 if (inet_diag_table[type] == NULL) {
861 inet_diag_table[type] = h;
862 err = 0;
864 spin_unlock(&inet_diag_register_lock);
865 out:
866 return err;
868 EXPORT_SYMBOL_GPL(inet_diag_register);
870 void inet_diag_unregister(const struct inet_diag_handler *h)
872 const __u16 type = h->idiag_type;
874 if (type >= INET_DIAG_GETSOCK_MAX)
875 return;
877 spin_lock(&inet_diag_register_lock);
878 inet_diag_table[type] = NULL;
879 spin_unlock(&inet_diag_register_lock);
881 synchronize_rcu();
883 EXPORT_SYMBOL_GPL(inet_diag_unregister);
885 static int __init inet_diag_init(void)
887 const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
888 sizeof(struct inet_diag_handler *));
889 int err = -ENOMEM;
891 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
892 if (!inet_diag_table)
893 goto out;
895 idiagnl = netlink_kernel_create(NETLINK_INET_DIAG, 0, inet_diag_rcv,
896 NULL, THIS_MODULE);
897 if (idiagnl == NULL)
898 goto out_free_table;
899 err = 0;
900 out:
901 return err;
902 out_free_table:
903 kfree(inet_diag_table);
904 goto out;
907 static void __exit inet_diag_exit(void)
909 sock_release(idiagnl->sk_socket);
910 kfree(inet_diag_table);
913 module_init(inet_diag_init);
914 module_exit(inet_diag_exit);
915 MODULE_LICENSE("GPL");