Revert "drm/radeon: rework pll selection (v3)"
[linux/fpc-iii.git] / net / ipv4 / inet_diag.c
blobccee270a9b6587efe63e74abcd298bae34fea779
1 /*
2 * inet_diag.c Module for monitoring INET transport protocols sockets.
4 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
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.
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>
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 DEFINE_MUTEX(inet_diag_table_mutex);
55 static const struct inet_diag_handler *inet_diag_lock_handler(int type)
57 if (!inet_diag_table[type])
58 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
59 NETLINK_INET_DIAG, type);
61 mutex_lock(&inet_diag_table_mutex);
62 if (!inet_diag_table[type])
63 return ERR_PTR(-ENOENT);
65 return inet_diag_table[type];
68 static inline void inet_diag_unlock_handler(
69 const struct inet_diag_handler *handler)
71 mutex_unlock(&inet_diag_table_mutex);
74 static int inet_csk_diag_fill(struct sock *sk,
75 struct sk_buff *skb,
76 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
77 const struct nlmsghdr *unlh)
79 const struct inet_sock *inet = inet_sk(sk);
80 const struct inet_connection_sock *icsk = inet_csk(sk);
81 struct inet_diag_msg *r;
82 struct nlmsghdr *nlh;
83 void *info = NULL;
84 struct inet_diag_meminfo *minfo = NULL;
85 unsigned char *b = skb_tail_pointer(skb);
86 const struct inet_diag_handler *handler;
88 handler = inet_diag_table[unlh->nlmsg_type];
89 BUG_ON(handler == NULL);
91 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
92 nlh->nlmsg_flags = nlmsg_flags;
94 r = NLMSG_DATA(nlh);
95 BUG_ON(sk->sk_state == TCP_TIME_WAIT);
97 if (ext & (1 << (INET_DIAG_MEMINFO - 1)))
98 minfo = INET_DIAG_PUT(skb, INET_DIAG_MEMINFO, sizeof(*minfo));
100 if (ext & (1 << (INET_DIAG_INFO - 1)))
101 info = INET_DIAG_PUT(skb, INET_DIAG_INFO,
102 handler->idiag_info_size);
104 if ((ext & (1 << (INET_DIAG_CONG - 1))) && icsk->icsk_ca_ops) {
105 const size_t len = strlen(icsk->icsk_ca_ops->name);
107 strcpy(INET_DIAG_PUT(skb, INET_DIAG_CONG, len + 1),
108 icsk->icsk_ca_ops->name);
111 r->idiag_family = sk->sk_family;
112 r->idiag_state = sk->sk_state;
113 r->idiag_timer = 0;
114 r->idiag_retrans = 0;
116 r->id.idiag_if = sk->sk_bound_dev_if;
117 r->id.idiag_cookie[0] = (u32)(unsigned long)sk;
118 r->id.idiag_cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
120 r->id.idiag_sport = inet->inet_sport;
121 r->id.idiag_dport = inet->inet_dport;
122 r->id.idiag_src[0] = inet->inet_rcv_saddr;
123 r->id.idiag_dst[0] = inet->inet_daddr;
125 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
126 * hence this needs to be included regardless of socket family.
128 if (ext & (1 << (INET_DIAG_TOS - 1)))
129 RTA_PUT_U8(skb, INET_DIAG_TOS, inet->tos);
131 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
132 if (r->idiag_family == AF_INET6) {
133 const struct ipv6_pinfo *np = inet6_sk(sk);
135 if (ext & (1 << (INET_DIAG_TCLASS - 1)))
136 RTA_PUT_U8(skb, INET_DIAG_TCLASS, np->tclass);
138 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
139 &np->rcv_saddr);
140 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
141 &np->daddr);
143 #endif
145 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
147 if (icsk->icsk_pending == ICSK_TIME_RETRANS) {
148 r->idiag_timer = 1;
149 r->idiag_retrans = icsk->icsk_retransmits;
150 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
151 } else if (icsk->icsk_pending == ICSK_TIME_PROBE0) {
152 r->idiag_timer = 4;
153 r->idiag_retrans = icsk->icsk_probes_out;
154 r->idiag_expires = EXPIRES_IN_MS(icsk->icsk_timeout);
155 } else if (timer_pending(&sk->sk_timer)) {
156 r->idiag_timer = 2;
157 r->idiag_retrans = icsk->icsk_probes_out;
158 r->idiag_expires = EXPIRES_IN_MS(sk->sk_timer.expires);
159 } else {
160 r->idiag_timer = 0;
161 r->idiag_expires = 0;
163 #undef EXPIRES_IN_MS
165 r->idiag_uid = sock_i_uid(sk);
166 r->idiag_inode = sock_i_ino(sk);
168 if (minfo) {
169 minfo->idiag_rmem = sk_rmem_alloc_get(sk);
170 minfo->idiag_wmem = sk->sk_wmem_queued;
171 minfo->idiag_fmem = sk->sk_forward_alloc;
172 minfo->idiag_tmem = sk_wmem_alloc_get(sk);
175 handler->idiag_get_info(sk, r, info);
177 if (sk->sk_state < TCP_TIME_WAIT &&
178 icsk->icsk_ca_ops && icsk->icsk_ca_ops->get_info)
179 icsk->icsk_ca_ops->get_info(sk, ext, skb);
181 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
182 return skb->len;
184 rtattr_failure:
185 nlmsg_failure:
186 nlmsg_trim(skb, b);
187 return -EMSGSIZE;
190 static int inet_twsk_diag_fill(struct inet_timewait_sock *tw,
191 struct sk_buff *skb, int ext, u32 pid,
192 u32 seq, u16 nlmsg_flags,
193 const struct nlmsghdr *unlh)
195 long tmo;
196 struct inet_diag_msg *r;
197 const unsigned char *previous_tail = skb_tail_pointer(skb);
198 struct nlmsghdr *nlh = NLMSG_PUT(skb, pid, seq,
199 unlh->nlmsg_type, sizeof(*r));
201 r = NLMSG_DATA(nlh);
202 BUG_ON(tw->tw_state != TCP_TIME_WAIT);
204 nlh->nlmsg_flags = nlmsg_flags;
206 tmo = tw->tw_ttd - jiffies;
207 if (tmo < 0)
208 tmo = 0;
210 r->idiag_family = tw->tw_family;
211 r->idiag_retrans = 0;
212 r->id.idiag_if = tw->tw_bound_dev_if;
213 r->id.idiag_cookie[0] = (u32)(unsigned long)tw;
214 r->id.idiag_cookie[1] = (u32)(((unsigned long)tw >> 31) >> 1);
215 r->id.idiag_sport = tw->tw_sport;
216 r->id.idiag_dport = tw->tw_dport;
217 r->id.idiag_src[0] = tw->tw_rcv_saddr;
218 r->id.idiag_dst[0] = tw->tw_daddr;
219 r->idiag_state = tw->tw_substate;
220 r->idiag_timer = 3;
221 r->idiag_expires = DIV_ROUND_UP(tmo * 1000, HZ);
222 r->idiag_rqueue = 0;
223 r->idiag_wqueue = 0;
224 r->idiag_uid = 0;
225 r->idiag_inode = 0;
226 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
227 if (tw->tw_family == AF_INET6) {
228 const struct inet6_timewait_sock *tw6 =
229 inet6_twsk((struct sock *)tw);
231 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
232 &tw6->tw_v6_rcv_saddr);
233 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
234 &tw6->tw_v6_daddr);
236 #endif
237 nlh->nlmsg_len = skb_tail_pointer(skb) - previous_tail;
238 return skb->len;
239 nlmsg_failure:
240 nlmsg_trim(skb, previous_tail);
241 return -EMSGSIZE;
244 static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
245 int ext, u32 pid, u32 seq, u16 nlmsg_flags,
246 const struct nlmsghdr *unlh)
248 if (sk->sk_state == TCP_TIME_WAIT)
249 return inet_twsk_diag_fill((struct inet_timewait_sock *)sk,
250 skb, ext, pid, seq, nlmsg_flags,
251 unlh);
252 return inet_csk_diag_fill(sk, skb, ext, pid, seq, nlmsg_flags, unlh);
255 static int inet_diag_get_exact(struct sk_buff *in_skb,
256 const struct nlmsghdr *nlh)
258 int err;
259 struct sock *sk;
260 struct inet_diag_req *req = NLMSG_DATA(nlh);
261 struct sk_buff *rep;
262 struct inet_hashinfo *hashinfo;
263 const struct inet_diag_handler *handler;
265 handler = inet_diag_lock_handler(nlh->nlmsg_type);
266 if (IS_ERR(handler)) {
267 err = PTR_ERR(handler);
268 goto unlock;
271 hashinfo = handler->idiag_hashinfo;
272 err = -EINVAL;
274 if (req->idiag_family == AF_INET) {
275 sk = inet_lookup(&init_net, hashinfo, req->id.idiag_dst[0],
276 req->id.idiag_dport, req->id.idiag_src[0],
277 req->id.idiag_sport, req->id.idiag_if);
279 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
280 else if (req->idiag_family == AF_INET6) {
281 sk = inet6_lookup(&init_net, hashinfo,
282 (struct in6_addr *)req->id.idiag_dst,
283 req->id.idiag_dport,
284 (struct in6_addr *)req->id.idiag_src,
285 req->id.idiag_sport,
286 req->id.idiag_if);
288 #endif
289 else {
290 goto unlock;
293 err = -ENOENT;
294 if (sk == NULL)
295 goto unlock;
297 err = -ESTALE;
298 if ((req->id.idiag_cookie[0] != INET_DIAG_NOCOOKIE ||
299 req->id.idiag_cookie[1] != INET_DIAG_NOCOOKIE) &&
300 ((u32)(unsigned long)sk != req->id.idiag_cookie[0] ||
301 (u32)((((unsigned long)sk) >> 31) >> 1) != req->id.idiag_cookie[1]))
302 goto out;
304 err = -ENOMEM;
305 rep = alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg) +
306 sizeof(struct inet_diag_meminfo) +
307 handler->idiag_info_size + 64)),
308 GFP_KERNEL);
309 if (!rep)
310 goto out;
312 err = sk_diag_fill(sk, rep, req->idiag_ext,
313 NETLINK_CB(in_skb).pid,
314 nlh->nlmsg_seq, 0, nlh);
315 if (err < 0) {
316 WARN_ON(err == -EMSGSIZE);
317 kfree_skb(rep);
318 goto out;
320 err = netlink_unicast(idiagnl, rep, NETLINK_CB(in_skb).pid,
321 MSG_DONTWAIT);
322 if (err > 0)
323 err = 0;
325 out:
326 if (sk) {
327 if (sk->sk_state == TCP_TIME_WAIT)
328 inet_twsk_put((struct inet_timewait_sock *)sk);
329 else
330 sock_put(sk);
332 unlock:
333 inet_diag_unlock_handler(handler);
334 return err;
337 static int bitstring_match(const __be32 *a1, const __be32 *a2, int bits)
339 int words = bits >> 5;
341 bits &= 0x1f;
343 if (words) {
344 if (memcmp(a1, a2, words << 2))
345 return 0;
347 if (bits) {
348 __be32 w1, w2;
349 __be32 mask;
351 w1 = a1[words];
352 w2 = a2[words];
354 mask = htonl((0xffffffff) << (32 - bits));
356 if ((w1 ^ w2) & mask)
357 return 0;
360 return 1;
364 static int inet_diag_bc_run(const void *bc, int len,
365 const struct inet_diag_entry *entry)
367 while (len > 0) {
368 int yes = 1;
369 const struct inet_diag_bc_op *op = bc;
371 switch (op->code) {
372 case INET_DIAG_BC_NOP:
373 break;
374 case INET_DIAG_BC_JMP:
375 yes = 0;
376 break;
377 case INET_DIAG_BC_S_GE:
378 yes = entry->sport >= op[1].no;
379 break;
380 case INET_DIAG_BC_S_LE:
381 yes = entry->sport <= op[1].no;
382 break;
383 case INET_DIAG_BC_D_GE:
384 yes = entry->dport >= op[1].no;
385 break;
386 case INET_DIAG_BC_D_LE:
387 yes = entry->dport <= op[1].no;
388 break;
389 case INET_DIAG_BC_AUTO:
390 yes = !(entry->userlocks & SOCK_BINDPORT_LOCK);
391 break;
392 case INET_DIAG_BC_S_COND:
393 case INET_DIAG_BC_D_COND: {
394 struct inet_diag_hostcond *cond;
395 __be32 *addr;
397 cond = (struct inet_diag_hostcond *)(op + 1);
398 if (cond->port != -1 &&
399 cond->port != (op->code == INET_DIAG_BC_S_COND ?
400 entry->sport : entry->dport)) {
401 yes = 0;
402 break;
405 if (cond->prefix_len == 0)
406 break;
408 if (op->code == INET_DIAG_BC_S_COND)
409 addr = entry->saddr;
410 else
411 addr = entry->daddr;
413 if (bitstring_match(addr, cond->addr,
414 cond->prefix_len))
415 break;
416 if (entry->family == AF_INET6 &&
417 cond->family == AF_INET) {
418 if (addr[0] == 0 && addr[1] == 0 &&
419 addr[2] == htonl(0xffff) &&
420 bitstring_match(addr + 3, cond->addr,
421 cond->prefix_len))
422 break;
424 yes = 0;
425 break;
429 if (yes) {
430 len -= op->yes;
431 bc += op->yes;
432 } else {
433 len -= op->no;
434 bc += op->no;
437 return len == 0;
440 static int valid_cc(const void *bc, int len, int cc)
442 while (len >= 0) {
443 const struct inet_diag_bc_op *op = bc;
445 if (cc > len)
446 return 0;
447 if (cc == len)
448 return 1;
449 if (op->yes < 4 || op->yes & 3)
450 return 0;
451 len -= op->yes;
452 bc += op->yes;
454 return 0;
457 static int inet_diag_bc_audit(const void *bytecode, int bytecode_len)
459 const void *bc = bytecode;
460 int len = bytecode_len;
462 while (len > 0) {
463 const struct inet_diag_bc_op *op = bc;
465 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
466 switch (op->code) {
467 case INET_DIAG_BC_AUTO:
468 case INET_DIAG_BC_S_COND:
469 case INET_DIAG_BC_D_COND:
470 case INET_DIAG_BC_S_GE:
471 case INET_DIAG_BC_S_LE:
472 case INET_DIAG_BC_D_GE:
473 case INET_DIAG_BC_D_LE:
474 case INET_DIAG_BC_JMP:
475 if (op->no < 4 || op->no > len + 4 || op->no & 3)
476 return -EINVAL;
477 if (op->no < len &&
478 !valid_cc(bytecode, bytecode_len, len - op->no))
479 return -EINVAL;
480 break;
481 case INET_DIAG_BC_NOP:
482 break;
483 default:
484 return -EINVAL;
486 if (op->yes < 4 || op->yes > len + 4 || op->yes & 3)
487 return -EINVAL;
488 bc += op->yes;
489 len -= op->yes;
491 return len == 0 ? 0 : -EINVAL;
494 static int inet_csk_diag_dump(struct sock *sk,
495 struct sk_buff *skb,
496 struct netlink_callback *cb)
498 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
500 if (nlmsg_attrlen(cb->nlh, sizeof(*r))) {
501 struct inet_diag_entry entry;
502 const struct nlattr *bc = nlmsg_find_attr(cb->nlh,
503 sizeof(*r),
504 INET_DIAG_REQ_BYTECODE);
505 struct inet_sock *inet = inet_sk(sk);
507 entry.family = sk->sk_family;
508 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
509 if (entry.family == AF_INET6) {
510 struct ipv6_pinfo *np = inet6_sk(sk);
512 entry.saddr = np->rcv_saddr.s6_addr32;
513 entry.daddr = np->daddr.s6_addr32;
514 } else
515 #endif
517 entry.saddr = &inet->inet_rcv_saddr;
518 entry.daddr = &inet->inet_daddr;
520 entry.sport = inet->inet_num;
521 entry.dport = ntohs(inet->inet_dport);
522 entry.userlocks = sk->sk_userlocks;
524 if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry))
525 return 0;
528 return inet_csk_diag_fill(sk, skb, r->idiag_ext,
529 NETLINK_CB(cb->skb).pid,
530 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
533 static int inet_twsk_diag_dump(struct inet_timewait_sock *tw,
534 struct sk_buff *skb,
535 struct netlink_callback *cb)
537 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
539 if (nlmsg_attrlen(cb->nlh, sizeof(*r))) {
540 struct inet_diag_entry entry;
541 const struct nlattr *bc = nlmsg_find_attr(cb->nlh,
542 sizeof(*r),
543 INET_DIAG_REQ_BYTECODE);
545 entry.family = tw->tw_family;
546 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
547 if (tw->tw_family == AF_INET6) {
548 struct inet6_timewait_sock *tw6 =
549 inet6_twsk((struct sock *)tw);
550 entry.saddr = tw6->tw_v6_rcv_saddr.s6_addr32;
551 entry.daddr = tw6->tw_v6_daddr.s6_addr32;
552 } else
553 #endif
555 entry.saddr = &tw->tw_rcv_saddr;
556 entry.daddr = &tw->tw_daddr;
558 entry.sport = tw->tw_num;
559 entry.dport = ntohs(tw->tw_dport);
560 entry.userlocks = 0;
562 if (!inet_diag_bc_run(nla_data(bc), nla_len(bc), &entry))
563 return 0;
566 return inet_twsk_diag_fill(tw, skb, r->idiag_ext,
567 NETLINK_CB(cb->skb).pid,
568 cb->nlh->nlmsg_seq, NLM_F_MULTI, cb->nlh);
571 static int inet_diag_fill_req(struct sk_buff *skb, struct sock *sk,
572 struct request_sock *req, u32 pid, u32 seq,
573 const struct nlmsghdr *unlh)
575 const struct inet_request_sock *ireq = inet_rsk(req);
576 struct inet_sock *inet = inet_sk(sk);
577 unsigned char *b = skb_tail_pointer(skb);
578 struct inet_diag_msg *r;
579 struct nlmsghdr *nlh;
580 long tmo;
582 nlh = NLMSG_PUT(skb, pid, seq, unlh->nlmsg_type, sizeof(*r));
583 nlh->nlmsg_flags = NLM_F_MULTI;
584 r = NLMSG_DATA(nlh);
586 r->idiag_family = sk->sk_family;
587 r->idiag_state = TCP_SYN_RECV;
588 r->idiag_timer = 1;
589 r->idiag_retrans = req->retrans;
591 r->id.idiag_if = sk->sk_bound_dev_if;
592 r->id.idiag_cookie[0] = (u32)(unsigned long)req;
593 r->id.idiag_cookie[1] = (u32)(((unsigned long)req >> 31) >> 1);
595 tmo = req->expires - jiffies;
596 if (tmo < 0)
597 tmo = 0;
599 r->id.idiag_sport = inet->inet_sport;
600 r->id.idiag_dport = ireq->rmt_port;
601 r->id.idiag_src[0] = ireq->loc_addr;
602 r->id.idiag_dst[0] = ireq->rmt_addr;
603 r->idiag_expires = jiffies_to_msecs(tmo);
604 r->idiag_rqueue = 0;
605 r->idiag_wqueue = 0;
606 r->idiag_uid = sock_i_uid(sk);
607 r->idiag_inode = 0;
608 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
609 if (r->idiag_family == AF_INET6) {
610 ipv6_addr_copy((struct in6_addr *)r->id.idiag_src,
611 &inet6_rsk(req)->loc_addr);
612 ipv6_addr_copy((struct in6_addr *)r->id.idiag_dst,
613 &inet6_rsk(req)->rmt_addr);
615 #endif
616 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
618 return skb->len;
620 nlmsg_failure:
621 nlmsg_trim(skb, b);
622 return -1;
625 static int inet_diag_dump_reqs(struct sk_buff *skb, struct sock *sk,
626 struct netlink_callback *cb)
628 struct inet_diag_entry entry;
629 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
630 struct inet_connection_sock *icsk = inet_csk(sk);
631 struct listen_sock *lopt;
632 const struct nlattr *bc = NULL;
633 struct inet_sock *inet = inet_sk(sk);
634 int j, s_j;
635 int reqnum, s_reqnum;
636 int err = 0;
638 s_j = cb->args[3];
639 s_reqnum = cb->args[4];
641 if (s_j > 0)
642 s_j--;
644 entry.family = sk->sk_family;
646 read_lock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
648 lopt = icsk->icsk_accept_queue.listen_opt;
649 if (!lopt || !lopt->qlen)
650 goto out;
652 if (nlmsg_attrlen(cb->nlh, sizeof(*r))) {
653 bc = nlmsg_find_attr(cb->nlh, sizeof(*r),
654 INET_DIAG_REQ_BYTECODE);
655 entry.sport = inet->inet_num;
656 entry.userlocks = sk->sk_userlocks;
659 for (j = s_j; j < lopt->nr_table_entries; j++) {
660 struct request_sock *req, *head = lopt->syn_table[j];
662 reqnum = 0;
663 for (req = head; req; reqnum++, req = req->dl_next) {
664 struct inet_request_sock *ireq = inet_rsk(req);
666 if (reqnum < s_reqnum)
667 continue;
668 if (r->id.idiag_dport != ireq->rmt_port &&
669 r->id.idiag_dport)
670 continue;
672 if (bc) {
673 entry.saddr =
674 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
675 (entry.family == AF_INET6) ?
676 inet6_rsk(req)->loc_addr.s6_addr32 :
677 #endif
678 &ireq->loc_addr;
679 entry.daddr =
680 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
681 (entry.family == AF_INET6) ?
682 inet6_rsk(req)->rmt_addr.s6_addr32 :
683 #endif
684 &ireq->rmt_addr;
685 entry.dport = ntohs(ireq->rmt_port);
687 if (!inet_diag_bc_run(nla_data(bc),
688 nla_len(bc), &entry))
689 continue;
692 err = inet_diag_fill_req(skb, sk, req,
693 NETLINK_CB(cb->skb).pid,
694 cb->nlh->nlmsg_seq, cb->nlh);
695 if (err < 0) {
696 cb->args[3] = j + 1;
697 cb->args[4] = reqnum;
698 goto out;
702 s_reqnum = 0;
705 out:
706 read_unlock_bh(&icsk->icsk_accept_queue.syn_wait_lock);
708 return err;
711 static int inet_diag_dump(struct sk_buff *skb, struct netlink_callback *cb)
713 int i, num;
714 int s_i, s_num;
715 struct inet_diag_req *r = NLMSG_DATA(cb->nlh);
716 const struct inet_diag_handler *handler;
717 struct inet_hashinfo *hashinfo;
719 handler = inet_diag_lock_handler(cb->nlh->nlmsg_type);
720 if (IS_ERR(handler))
721 goto unlock;
723 hashinfo = handler->idiag_hashinfo;
725 s_i = cb->args[1];
726 s_num = num = cb->args[2];
728 if (cb->args[0] == 0) {
729 if (!(r->idiag_states & (TCPF_LISTEN | TCPF_SYN_RECV)))
730 goto skip_listen_ht;
732 for (i = s_i; i < INET_LHTABLE_SIZE; i++) {
733 struct sock *sk;
734 struct hlist_nulls_node *node;
735 struct inet_listen_hashbucket *ilb;
737 num = 0;
738 ilb = &hashinfo->listening_hash[i];
739 spin_lock_bh(&ilb->lock);
740 sk_nulls_for_each(sk, node, &ilb->head) {
741 struct inet_sock *inet = inet_sk(sk);
743 if (num < s_num) {
744 num++;
745 continue;
748 if (r->id.idiag_sport != inet->inet_sport &&
749 r->id.idiag_sport)
750 goto next_listen;
752 if (!(r->idiag_states & TCPF_LISTEN) ||
753 r->id.idiag_dport ||
754 cb->args[3] > 0)
755 goto syn_recv;
757 if (inet_csk_diag_dump(sk, skb, cb) < 0) {
758 spin_unlock_bh(&ilb->lock);
759 goto done;
762 syn_recv:
763 if (!(r->idiag_states & TCPF_SYN_RECV))
764 goto next_listen;
766 if (inet_diag_dump_reqs(skb, sk, cb) < 0) {
767 spin_unlock_bh(&ilb->lock);
768 goto done;
771 next_listen:
772 cb->args[3] = 0;
773 cb->args[4] = 0;
774 ++num;
776 spin_unlock_bh(&ilb->lock);
778 s_num = 0;
779 cb->args[3] = 0;
780 cb->args[4] = 0;
782 skip_listen_ht:
783 cb->args[0] = 1;
784 s_i = num = s_num = 0;
787 if (!(r->idiag_states & ~(TCPF_LISTEN | TCPF_SYN_RECV)))
788 goto unlock;
790 for (i = s_i; i <= hashinfo->ehash_mask; i++) {
791 struct inet_ehash_bucket *head = &hashinfo->ehash[i];
792 spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
793 struct sock *sk;
794 struct hlist_nulls_node *node;
796 num = 0;
798 if (hlist_nulls_empty(&head->chain) &&
799 hlist_nulls_empty(&head->twchain))
800 continue;
802 if (i > s_i)
803 s_num = 0;
805 spin_lock_bh(lock);
806 sk_nulls_for_each(sk, node, &head->chain) {
807 struct inet_sock *inet = inet_sk(sk);
809 if (num < s_num)
810 goto next_normal;
811 if (!(r->idiag_states & (1 << sk->sk_state)))
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) < 0) {
820 spin_unlock_bh(lock);
821 goto done;
823 next_normal:
824 ++num;
827 if (r->idiag_states & TCPF_TIME_WAIT) {
828 struct inet_timewait_sock *tw;
830 inet_twsk_for_each(tw, node,
831 &head->twchain) {
833 if (num < s_num)
834 goto next_dying;
835 if (r->id.idiag_sport != tw->tw_sport &&
836 r->id.idiag_sport)
837 goto next_dying;
838 if (r->id.idiag_dport != tw->tw_dport &&
839 r->id.idiag_dport)
840 goto next_dying;
841 if (inet_twsk_diag_dump(tw, skb, cb) < 0) {
842 spin_unlock_bh(lock);
843 goto done;
845 next_dying:
846 ++num;
849 spin_unlock_bh(lock);
852 done:
853 cb->args[1] = i;
854 cb->args[2] = num;
855 unlock:
856 inet_diag_unlock_handler(handler);
857 return skb->len;
860 static int inet_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
862 int hdrlen = sizeof(struct inet_diag_req);
864 if (nlh->nlmsg_type >= INET_DIAG_GETSOCK_MAX ||
865 nlmsg_len(nlh) < hdrlen)
866 return -EINVAL;
868 if (nlh->nlmsg_flags & NLM_F_DUMP) {
869 if (nlmsg_attrlen(nlh, hdrlen)) {
870 struct nlattr *attr;
872 attr = nlmsg_find_attr(nlh, hdrlen,
873 INET_DIAG_REQ_BYTECODE);
874 if (attr == NULL ||
875 nla_len(attr) < sizeof(struct inet_diag_bc_op) ||
876 inet_diag_bc_audit(nla_data(attr), nla_len(attr)))
877 return -EINVAL;
880 return netlink_dump_start(idiagnl, skb, nlh,
881 inet_diag_dump, NULL, 0);
884 return inet_diag_get_exact(skb, nlh);
887 static DEFINE_MUTEX(inet_diag_mutex);
889 static void inet_diag_rcv(struct sk_buff *skb)
891 mutex_lock(&inet_diag_mutex);
892 netlink_rcv_skb(skb, &inet_diag_rcv_msg);
893 mutex_unlock(&inet_diag_mutex);
896 int inet_diag_register(const struct inet_diag_handler *h)
898 const __u16 type = h->idiag_type;
899 int err = -EINVAL;
901 if (type >= INET_DIAG_GETSOCK_MAX)
902 goto out;
904 mutex_lock(&inet_diag_table_mutex);
905 err = -EEXIST;
906 if (inet_diag_table[type] == NULL) {
907 inet_diag_table[type] = h;
908 err = 0;
910 mutex_unlock(&inet_diag_table_mutex);
911 out:
912 return err;
914 EXPORT_SYMBOL_GPL(inet_diag_register);
916 void inet_diag_unregister(const struct inet_diag_handler *h)
918 const __u16 type = h->idiag_type;
920 if (type >= INET_DIAG_GETSOCK_MAX)
921 return;
923 mutex_lock(&inet_diag_table_mutex);
924 inet_diag_table[type] = NULL;
925 mutex_unlock(&inet_diag_table_mutex);
927 EXPORT_SYMBOL_GPL(inet_diag_unregister);
929 static int __init inet_diag_init(void)
931 const int inet_diag_table_size = (INET_DIAG_GETSOCK_MAX *
932 sizeof(struct inet_diag_handler *));
933 int err = -ENOMEM;
935 inet_diag_table = kzalloc(inet_diag_table_size, GFP_KERNEL);
936 if (!inet_diag_table)
937 goto out;
939 idiagnl = netlink_kernel_create(&init_net, NETLINK_INET_DIAG, 0,
940 inet_diag_rcv, NULL, THIS_MODULE);
941 if (idiagnl == NULL)
942 goto out_free_table;
943 err = 0;
944 out:
945 return err;
946 out_free_table:
947 kfree(inet_diag_table);
948 goto out;
951 static void __exit inet_diag_exit(void)
953 netlink_kernel_release(idiagnl);
954 kfree(inet_diag_table);
957 module_init(inet_diag_init);
958 module_exit(inet_diag_exit);
959 MODULE_LICENSE("GPL");
960 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_INET_DIAG);