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>
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
{
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
,
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
;
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
;
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
;
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
,
140 ipv6_addr_copy((struct in6_addr
*)r
->id
.idiag_dst
,
145 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
147 if (icsk
->icsk_pending
== ICSK_TIME_RETRANS
) {
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
) {
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
)) {
157 r
->idiag_retrans
= icsk
->icsk_probes_out
;
158 r
->idiag_expires
= EXPIRES_IN_MS(sk
->sk_timer
.expires
);
161 r
->idiag_expires
= 0;
165 r
->idiag_uid
= sock_i_uid(sk
);
166 r
->idiag_inode
= sock_i_ino(sk
);
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
;
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
)
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
));
202 BUG_ON(tw
->tw_state
!= TCP_TIME_WAIT
);
204 nlh
->nlmsg_flags
= nlmsg_flags
;
206 tmo
= tw
->tw_ttd
- jiffies
;
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
;
221 r
->idiag_expires
= DIV_ROUND_UP(tmo
* 1000, HZ
);
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
,
237 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - previous_tail
;
240 nlmsg_trim(skb
, previous_tail
);
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
,
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
)
260 struct inet_diag_req
*req
= NLMSG_DATA(nlh
);
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
);
271 hashinfo
= handler
->idiag_hashinfo
;
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
,
284 (struct in6_addr
*)req
->id
.idiag_src
,
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]))
305 rep
= alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg
) +
306 sizeof(struct inet_diag_meminfo
) +
307 handler
->idiag_info_size
+ 64)),
312 err
= sk_diag_fill(sk
, rep
, req
->idiag_ext
,
313 NETLINK_CB(in_skb
).pid
,
314 nlh
->nlmsg_seq
, 0, nlh
);
316 WARN_ON(err
== -EMSGSIZE
);
320 err
= netlink_unicast(idiagnl
, rep
, NETLINK_CB(in_skb
).pid
,
327 if (sk
->sk_state
== TCP_TIME_WAIT
)
328 inet_twsk_put((struct inet_timewait_sock
*)sk
);
333 inet_diag_unlock_handler(handler
);
337 static int bitstring_match(const __be32
*a1
, const __be32
*a2
, int bits
)
339 int words
= bits
>> 5;
344 if (memcmp(a1
, a2
, words
<< 2))
354 mask
= htonl((0xffffffff) << (32 - bits
));
356 if ((w1
^ w2
) & mask
)
364 static int inet_diag_bc_run(const void *bc
, int len
,
365 const struct inet_diag_entry
*entry
)
369 const struct inet_diag_bc_op
*op
= bc
;
372 case INET_DIAG_BC_NOP
:
374 case INET_DIAG_BC_JMP
:
377 case INET_DIAG_BC_S_GE
:
378 yes
= entry
->sport
>= op
[1].no
;
380 case INET_DIAG_BC_S_LE
:
381 yes
= entry
->sport
<= op
[1].no
;
383 case INET_DIAG_BC_D_GE
:
384 yes
= entry
->dport
>= op
[1].no
;
386 case INET_DIAG_BC_D_LE
:
387 yes
= entry
->dport
<= op
[1].no
;
389 case INET_DIAG_BC_AUTO
:
390 yes
= !(entry
->userlocks
& SOCK_BINDPORT_LOCK
);
392 case INET_DIAG_BC_S_COND
:
393 case INET_DIAG_BC_D_COND
: {
394 struct inet_diag_hostcond
*cond
;
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
)) {
405 if (cond
->prefix_len
== 0)
408 if (op
->code
== INET_DIAG_BC_S_COND
)
413 if (bitstring_match(addr
, cond
->addr
,
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
,
440 static int valid_cc(const void *bc
, int len
, int cc
)
443 const struct inet_diag_bc_op
*op
= bc
;
449 if (op
->yes
< 4 || op
->yes
& 3)
457 static int inet_diag_bc_audit(const void *bytecode
, int bytecode_len
)
459 const void *bc
= bytecode
;
460 int len
= bytecode_len
;
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);
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)
478 !valid_cc(bytecode
, bytecode_len
, len
- op
->no
))
481 case INET_DIAG_BC_NOP
:
486 if (op
->yes
< 4 || op
->yes
> len
+ 4 || op
->yes
& 3)
491 return len
== 0 ? 0 : -EINVAL
;
494 static int inet_csk_diag_dump(struct sock
*sk
,
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
,
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
;
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
))
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
,
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
,
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
;
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
);
562 if (!inet_diag_bc_run(nla_data(bc
), nla_len(bc
), &entry
))
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
;
582 nlh
= NLMSG_PUT(skb
, pid
, seq
, unlh
->nlmsg_type
, sizeof(*r
));
583 nlh
->nlmsg_flags
= NLM_F_MULTI
;
586 r
->idiag_family
= sk
->sk_family
;
587 r
->idiag_state
= TCP_SYN_RECV
;
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
;
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
);
606 r
->idiag_uid
= sock_i_uid(sk
);
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
);
616 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
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
);
635 int reqnum
, s_reqnum
;
639 s_reqnum
= cb
->args
[4];
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
)
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
];
663 for (req
= head
; req
; reqnum
++, req
= req
->dl_next
) {
664 struct inet_request_sock
*ireq
= inet_rsk(req
);
666 if (reqnum
< s_reqnum
)
668 if (r
->id
.idiag_dport
!= ireq
->rmt_port
&&
674 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
675 (entry
.family
== AF_INET6
) ?
676 inet6_rsk(req
)->loc_addr
.s6_addr32
:
680 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
681 (entry
.family
== AF_INET6
) ?
682 inet6_rsk(req
)->rmt_addr
.s6_addr32
:
685 entry
.dport
= ntohs(ireq
->rmt_port
);
687 if (!inet_diag_bc_run(nla_data(bc
),
688 nla_len(bc
), &entry
))
692 err
= inet_diag_fill_req(skb
, sk
, req
,
693 NETLINK_CB(cb
->skb
).pid
,
694 cb
->nlh
->nlmsg_seq
, cb
->nlh
);
697 cb
->args
[4] = reqnum
;
706 read_unlock_bh(&icsk
->icsk_accept_queue
.syn_wait_lock
);
711 static int inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
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
);
723 hashinfo
= handler
->idiag_hashinfo
;
726 s_num
= num
= cb
->args
[2];
728 if (cb
->args
[0] == 0) {
729 if (!(r
->idiag_states
& (TCPF_LISTEN
| TCPF_SYN_RECV
)))
732 for (i
= s_i
; i
< INET_LHTABLE_SIZE
; i
++) {
734 struct hlist_nulls_node
*node
;
735 struct inet_listen_hashbucket
*ilb
;
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
);
748 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
752 if (!(r
->idiag_states
& TCPF_LISTEN
) ||
757 if (inet_csk_diag_dump(sk
, skb
, cb
) < 0) {
758 spin_unlock_bh(&ilb
->lock
);
763 if (!(r
->idiag_states
& TCPF_SYN_RECV
))
766 if (inet_diag_dump_reqs(skb
, sk
, cb
) < 0) {
767 spin_unlock_bh(&ilb
->lock
);
776 spin_unlock_bh(&ilb
->lock
);
784 s_i
= num
= s_num
= 0;
787 if (!(r
->idiag_states
& ~(TCPF_LISTEN
| TCPF_SYN_RECV
)))
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
);
794 struct hlist_nulls_node
*node
;
798 if (hlist_nulls_empty(&head
->chain
) &&
799 hlist_nulls_empty(&head
->twchain
))
806 sk_nulls_for_each(sk
, node
, &head
->chain
) {
807 struct inet_sock
*inet
= inet_sk(sk
);
811 if (!(r
->idiag_states
& (1 << sk
->sk_state
)))
813 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
816 if (r
->id
.idiag_dport
!= inet
->inet_dport
&&
819 if (inet_csk_diag_dump(sk
, skb
, cb
) < 0) {
820 spin_unlock_bh(lock
);
827 if (r
->idiag_states
& TCPF_TIME_WAIT
) {
828 struct inet_timewait_sock
*tw
;
830 inet_twsk_for_each(tw
, node
,
835 if (r
->id
.idiag_sport
!= tw
->tw_sport
&&
838 if (r
->id
.idiag_dport
!= tw
->tw_dport
&&
841 if (inet_twsk_diag_dump(tw
, skb
, cb
) < 0) {
842 spin_unlock_bh(lock
);
849 spin_unlock_bh(lock
);
856 inet_diag_unlock_handler(handler
);
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
)
868 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
869 if (nlmsg_attrlen(nlh
, hdrlen
)) {
872 attr
= nlmsg_find_attr(nlh
, hdrlen
,
873 INET_DIAG_REQ_BYTECODE
);
875 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
876 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
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
;
901 if (type
>= INET_DIAG_GETSOCK_MAX
)
904 mutex_lock(&inet_diag_table_mutex
);
906 if (inet_diag_table
[type
] == NULL
) {
907 inet_diag_table
[type
] = h
;
910 mutex_unlock(&inet_diag_table_mutex
);
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
)
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
*));
935 inet_diag_table
= kzalloc(inet_diag_table_size
, GFP_KERNEL
);
936 if (!inet_diag_table
)
939 idiagnl
= netlink_kernel_create(&init_net
, NETLINK_INET_DIAG
, 0,
940 inet_diag_rcv
, NULL
, THIS_MODULE
);
947 kfree(inet_diag_table
);
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
);