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>
36 #include <linux/sock_diag.h>
38 static const struct inet_diag_handler
**inet_diag_table
;
40 struct inet_diag_entry
{
49 #define INET_DIAG_PUT(skb, attrtype, attrlen) \
50 RTA_DATA(__RTA_PUT(skb, attrtype, attrlen))
52 static DEFINE_MUTEX(inet_diag_table_mutex
);
54 static const struct inet_diag_handler
*inet_diag_lock_handler(int proto
)
56 if (!inet_diag_table
[proto
])
57 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK
,
58 NETLINK_SOCK_DIAG
, proto
);
60 mutex_lock(&inet_diag_table_mutex
);
61 if (!inet_diag_table
[proto
])
62 return ERR_PTR(-ENOENT
);
64 return inet_diag_table
[proto
];
67 static inline void inet_diag_unlock_handler(
68 const struct inet_diag_handler
*handler
)
70 mutex_unlock(&inet_diag_table_mutex
);
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
)
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
;
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
;
88 handler
= inet_diag_table
[req
->sdiag_protocol
];
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
, sizeof(struct tcp_info
));
103 if ((ext
& (1 << (INET_DIAG_CONG
- 1))) && icsk
->icsk_ca_ops
) {
104 const size_t len
= strlen(icsk
->icsk_ca_ops
->name
);
106 strcpy(INET_DIAG_PUT(skb
, INET_DIAG_CONG
, len
+ 1),
107 icsk
->icsk_ca_ops
->name
);
110 r
->idiag_family
= sk
->sk_family
;
111 r
->idiag_state
= sk
->sk_state
;
113 r
->idiag_retrans
= 0;
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);
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
;
124 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
125 * hence this needs to be included regardless of socket family.
127 if (ext
& (1 << (INET_DIAG_TOS
- 1)))
128 RTA_PUT_U8(skb
, INET_DIAG_TOS
, inet
->tos
);
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
);
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
);
141 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
143 if (icsk
->icsk_pending
== ICSK_TIME_RETRANS
) {
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
) {
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
)) {
153 r
->idiag_retrans
= icsk
->icsk_probes_out
;
154 r
->idiag_expires
= EXPIRES_IN_MS(sk
->sk_timer
.expires
);
157 r
->idiag_expires
= 0;
161 r
->idiag_uid
= sock_i_uid(sk
);
162 r
->idiag_inode
= sock_i_ino(sk
);
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
);
171 handler
->idiag_get_info(sk
, r
, info
);
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
);
177 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
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
)
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
));
198 BUG_ON(tw
->tw_state
!= TCP_TIME_WAIT
);
200 nlh
->nlmsg_flags
= nlmsg_flags
;
202 tmo
= tw
->tw_ttd
- jiffies
;
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
;
217 r
->idiag_expires
= DIV_ROUND_UP(tmo
* 1000, HZ
);
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
);
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
;
231 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - previous_tail
;
234 nlmsg_trim(skb
, previous_tail
);
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
)
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
,
246 return inet_csk_diag_fill(sk
, skb
, r
, pid
, seq
, nlmsg_flags
, unlh
);
249 static int inet_diag_get_exact(struct sk_buff
*in_skb
,
250 const struct nlmsghdr
*nlh
,
251 struct inet_diag_req
*req
)
256 struct inet_hashinfo
*hashinfo
;
257 const struct inet_diag_handler
*handler
;
259 handler
= inet_diag_lock_handler(req
->sdiag_protocol
);
260 if (IS_ERR(handler
)) {
261 err
= PTR_ERR(handler
);
265 hashinfo
= handler
->idiag_hashinfo
;
268 if (req
->sdiag_family
== AF_INET
) {
269 sk
= inet_lookup(&init_net
, hashinfo
, req
->id
.idiag_dst
[0],
270 req
->id
.idiag_dport
, req
->id
.idiag_src
[0],
271 req
->id
.idiag_sport
, req
->id
.idiag_if
);
273 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
274 else if (req
->sdiag_family
== AF_INET6
) {
275 sk
= inet6_lookup(&init_net
, hashinfo
,
276 (struct in6_addr
*)req
->id
.idiag_dst
,
278 (struct in6_addr
*)req
->id
.idiag_src
,
292 if ((req
->id
.idiag_cookie
[0] != INET_DIAG_NOCOOKIE
||
293 req
->id
.idiag_cookie
[1] != INET_DIAG_NOCOOKIE
) &&
294 ((u32
)(unsigned long)sk
!= req
->id
.idiag_cookie
[0] ||
295 (u32
)((((unsigned long)sk
) >> 31) >> 1) != req
->id
.idiag_cookie
[1]))
299 rep
= alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg
) +
300 sizeof(struct inet_diag_meminfo
) +
301 sizeof(struct tcp_info
) + 64)),
306 err
= sk_diag_fill(sk
, rep
, req
,
307 NETLINK_CB(in_skb
).pid
,
308 nlh
->nlmsg_seq
, 0, nlh
);
310 WARN_ON(err
== -EMSGSIZE
);
314 err
= netlink_unicast(sock_diag_nlsk
, rep
, NETLINK_CB(in_skb
).pid
,
321 if (sk
->sk_state
== TCP_TIME_WAIT
)
322 inet_twsk_put((struct inet_timewait_sock
*)sk
);
327 inet_diag_unlock_handler(handler
);
331 static int bitstring_match(const __be32
*a1
, const __be32
*a2
, int bits
)
333 int words
= bits
>> 5;
338 if (memcmp(a1
, a2
, words
<< 2))
348 mask
= htonl((0xffffffff) << (32 - bits
));
350 if ((w1
^ w2
) & mask
)
358 static int inet_diag_bc_run(const struct nlattr
*_bc
,
359 const struct inet_diag_entry
*entry
)
361 const void *bc
= nla_data(_bc
);
362 int len
= nla_len(_bc
);
366 const struct inet_diag_bc_op
*op
= bc
;
369 case INET_DIAG_BC_NOP
:
371 case INET_DIAG_BC_JMP
:
374 case INET_DIAG_BC_S_GE
:
375 yes
= entry
->sport
>= op
[1].no
;
377 case INET_DIAG_BC_S_LE
:
378 yes
= entry
->sport
<= op
[1].no
;
380 case INET_DIAG_BC_D_GE
:
381 yes
= entry
->dport
>= op
[1].no
;
383 case INET_DIAG_BC_D_LE
:
384 yes
= entry
->dport
<= op
[1].no
;
386 case INET_DIAG_BC_AUTO
:
387 yes
= !(entry
->userlocks
& SOCK_BINDPORT_LOCK
);
389 case INET_DIAG_BC_S_COND
:
390 case INET_DIAG_BC_D_COND
: {
391 struct inet_diag_hostcond
*cond
;
394 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
395 if (cond
->port
!= -1 &&
396 cond
->port
!= (op
->code
== INET_DIAG_BC_S_COND
?
397 entry
->sport
: entry
->dport
)) {
402 if (cond
->prefix_len
== 0)
405 if (op
->code
== INET_DIAG_BC_S_COND
)
410 if (bitstring_match(addr
, cond
->addr
,
413 if (entry
->family
== AF_INET6
&&
414 cond
->family
== AF_INET
) {
415 if (addr
[0] == 0 && addr
[1] == 0 &&
416 addr
[2] == htonl(0xffff) &&
417 bitstring_match(addr
+ 3, cond
->addr
,
437 static int valid_cc(const void *bc
, int len
, int cc
)
440 const struct inet_diag_bc_op
*op
= bc
;
446 if (op
->yes
< 4 || op
->yes
& 3)
454 static int inet_diag_bc_audit(const void *bytecode
, int bytecode_len
)
456 const void *bc
= bytecode
;
457 int len
= bytecode_len
;
460 const struct inet_diag_bc_op
*op
= bc
;
462 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
464 case INET_DIAG_BC_AUTO
:
465 case INET_DIAG_BC_S_COND
:
466 case INET_DIAG_BC_D_COND
:
467 case INET_DIAG_BC_S_GE
:
468 case INET_DIAG_BC_S_LE
:
469 case INET_DIAG_BC_D_GE
:
470 case INET_DIAG_BC_D_LE
:
471 case INET_DIAG_BC_JMP
:
472 if (op
->no
< 4 || op
->no
> len
+ 4 || op
->no
& 3)
475 !valid_cc(bytecode
, bytecode_len
, len
- op
->no
))
478 case INET_DIAG_BC_NOP
:
483 if (op
->yes
< 4 || op
->yes
> len
+ 4 || op
->yes
& 3)
488 return len
== 0 ? 0 : -EINVAL
;
491 static int inet_csk_diag_dump(struct sock
*sk
,
493 struct netlink_callback
*cb
,
494 struct inet_diag_req
*r
,
495 const struct nlattr
*bc
)
498 struct inet_diag_entry entry
;
499 struct inet_sock
*inet
= inet_sk(sk
);
501 entry
.family
= sk
->sk_family
;
502 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
503 if (entry
.family
== AF_INET6
) {
504 struct ipv6_pinfo
*np
= inet6_sk(sk
);
506 entry
.saddr
= np
->rcv_saddr
.s6_addr32
;
507 entry
.daddr
= np
->daddr
.s6_addr32
;
511 entry
.saddr
= &inet
->inet_rcv_saddr
;
512 entry
.daddr
= &inet
->inet_daddr
;
514 entry
.sport
= inet
->inet_num
;
515 entry
.dport
= ntohs(inet
->inet_dport
);
516 entry
.userlocks
= sk
->sk_userlocks
;
518 if (!inet_diag_bc_run(bc
, &entry
))
522 return inet_csk_diag_fill(sk
, skb
, r
,
523 NETLINK_CB(cb
->skb
).pid
,
524 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
527 static int inet_twsk_diag_dump(struct inet_timewait_sock
*tw
,
529 struct netlink_callback
*cb
,
530 struct inet_diag_req
*r
,
531 const struct nlattr
*bc
)
534 struct inet_diag_entry entry
;
536 entry
.family
= tw
->tw_family
;
537 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
538 if (tw
->tw_family
== AF_INET6
) {
539 struct inet6_timewait_sock
*tw6
=
540 inet6_twsk((struct sock
*)tw
);
541 entry
.saddr
= tw6
->tw_v6_rcv_saddr
.s6_addr32
;
542 entry
.daddr
= tw6
->tw_v6_daddr
.s6_addr32
;
546 entry
.saddr
= &tw
->tw_rcv_saddr
;
547 entry
.daddr
= &tw
->tw_daddr
;
549 entry
.sport
= tw
->tw_num
;
550 entry
.dport
= ntohs(tw
->tw_dport
);
553 if (!inet_diag_bc_run(bc
, &entry
))
557 return inet_twsk_diag_fill(tw
, skb
, r
,
558 NETLINK_CB(cb
->skb
).pid
,
559 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
562 static int inet_diag_fill_req(struct sk_buff
*skb
, struct sock
*sk
,
563 struct request_sock
*req
, u32 pid
, u32 seq
,
564 const struct nlmsghdr
*unlh
)
566 const struct inet_request_sock
*ireq
= inet_rsk(req
);
567 struct inet_sock
*inet
= inet_sk(sk
);
568 unsigned char *b
= skb_tail_pointer(skb
);
569 struct inet_diag_msg
*r
;
570 struct nlmsghdr
*nlh
;
573 nlh
= NLMSG_PUT(skb
, pid
, seq
, unlh
->nlmsg_type
, sizeof(*r
));
574 nlh
->nlmsg_flags
= NLM_F_MULTI
;
577 r
->idiag_family
= sk
->sk_family
;
578 r
->idiag_state
= TCP_SYN_RECV
;
580 r
->idiag_retrans
= req
->retrans
;
582 r
->id
.idiag_if
= sk
->sk_bound_dev_if
;
583 r
->id
.idiag_cookie
[0] = (u32
)(unsigned long)req
;
584 r
->id
.idiag_cookie
[1] = (u32
)(((unsigned long)req
>> 31) >> 1);
586 tmo
= req
->expires
- jiffies
;
590 r
->id
.idiag_sport
= inet
->inet_sport
;
591 r
->id
.idiag_dport
= ireq
->rmt_port
;
592 r
->id
.idiag_src
[0] = ireq
->loc_addr
;
593 r
->id
.idiag_dst
[0] = ireq
->rmt_addr
;
594 r
->idiag_expires
= jiffies_to_msecs(tmo
);
597 r
->idiag_uid
= sock_i_uid(sk
);
599 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
600 if (r
->idiag_family
== AF_INET6
) {
601 *(struct in6_addr
*)r
->id
.idiag_src
= inet6_rsk(req
)->loc_addr
;
602 *(struct in6_addr
*)r
->id
.idiag_dst
= inet6_rsk(req
)->rmt_addr
;
605 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
614 static int inet_diag_dump_reqs(struct sk_buff
*skb
, struct sock
*sk
,
615 struct netlink_callback
*cb
,
616 struct inet_diag_req
*r
,
617 const struct nlattr
*bc
)
619 struct inet_diag_entry entry
;
620 struct inet_connection_sock
*icsk
= inet_csk(sk
);
621 struct listen_sock
*lopt
;
622 struct inet_sock
*inet
= inet_sk(sk
);
624 int reqnum
, s_reqnum
;
628 s_reqnum
= cb
->args
[4];
633 entry
.family
= sk
->sk_family
;
635 read_lock_bh(&icsk
->icsk_accept_queue
.syn_wait_lock
);
637 lopt
= icsk
->icsk_accept_queue
.listen_opt
;
638 if (!lopt
|| !lopt
->qlen
)
642 entry
.sport
= inet
->inet_num
;
643 entry
.userlocks
= sk
->sk_userlocks
;
646 for (j
= s_j
; j
< lopt
->nr_table_entries
; j
++) {
647 struct request_sock
*req
, *head
= lopt
->syn_table
[j
];
650 for (req
= head
; req
; reqnum
++, req
= req
->dl_next
) {
651 struct inet_request_sock
*ireq
= inet_rsk(req
);
653 if (reqnum
< s_reqnum
)
655 if (r
->id
.idiag_dport
!= ireq
->rmt_port
&&
661 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
662 (entry
.family
== AF_INET6
) ?
663 inet6_rsk(req
)->loc_addr
.s6_addr32
:
667 #if defined(CONFIG_IPV6) || defined (CONFIG_IPV6_MODULE)
668 (entry
.family
== AF_INET6
) ?
669 inet6_rsk(req
)->rmt_addr
.s6_addr32
:
672 entry
.dport
= ntohs(ireq
->rmt_port
);
674 if (!inet_diag_bc_run(bc
, &entry
))
678 err
= inet_diag_fill_req(skb
, sk
, req
,
679 NETLINK_CB(cb
->skb
).pid
,
680 cb
->nlh
->nlmsg_seq
, cb
->nlh
);
683 cb
->args
[4] = reqnum
;
692 read_unlock_bh(&icsk
->icsk_accept_queue
.syn_wait_lock
);
697 static int __inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
698 struct inet_diag_req
*r
, struct nlattr
*bc
)
702 const struct inet_diag_handler
*handler
;
703 struct inet_hashinfo
*hashinfo
;
705 handler
= inet_diag_lock_handler(r
->sdiag_protocol
);
709 hashinfo
= handler
->idiag_hashinfo
;
712 s_num
= num
= cb
->args
[2];
714 if (cb
->args
[0] == 0) {
715 if (!(r
->idiag_states
& (TCPF_LISTEN
| TCPF_SYN_RECV
)))
718 for (i
= s_i
; i
< INET_LHTABLE_SIZE
; i
++) {
720 struct hlist_nulls_node
*node
;
721 struct inet_listen_hashbucket
*ilb
;
724 ilb
= &hashinfo
->listening_hash
[i
];
725 spin_lock_bh(&ilb
->lock
);
726 sk_nulls_for_each(sk
, node
, &ilb
->head
) {
727 struct inet_sock
*inet
= inet_sk(sk
);
734 if (r
->sdiag_family
!= AF_UNSPEC
&&
735 sk
->sk_family
!= r
->sdiag_family
)
738 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
742 if (!(r
->idiag_states
& TCPF_LISTEN
) ||
747 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
748 spin_unlock_bh(&ilb
->lock
);
753 if (!(r
->idiag_states
& TCPF_SYN_RECV
))
756 if (inet_diag_dump_reqs(skb
, sk
, cb
, r
, bc
) < 0) {
757 spin_unlock_bh(&ilb
->lock
);
766 spin_unlock_bh(&ilb
->lock
);
774 s_i
= num
= s_num
= 0;
777 if (!(r
->idiag_states
& ~(TCPF_LISTEN
| TCPF_SYN_RECV
)))
780 for (i
= s_i
; i
<= hashinfo
->ehash_mask
; i
++) {
781 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[i
];
782 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, i
);
784 struct hlist_nulls_node
*node
;
788 if (hlist_nulls_empty(&head
->chain
) &&
789 hlist_nulls_empty(&head
->twchain
))
796 sk_nulls_for_each(sk
, node
, &head
->chain
) {
797 struct inet_sock
*inet
= inet_sk(sk
);
801 if (!(r
->idiag_states
& (1 << sk
->sk_state
)))
803 if (r
->sdiag_family
!= AF_UNSPEC
&&
804 sk
->sk_family
!= r
->sdiag_family
)
806 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
809 if (r
->id
.idiag_dport
!= inet
->inet_dport
&&
812 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
813 spin_unlock_bh(lock
);
820 if (r
->idiag_states
& TCPF_TIME_WAIT
) {
821 struct inet_timewait_sock
*tw
;
823 inet_twsk_for_each(tw
, node
,
828 if (r
->sdiag_family
!= AF_UNSPEC
&&
829 tw
->tw_family
!= r
->sdiag_family
)
831 if (r
->id
.idiag_sport
!= tw
->tw_sport
&&
834 if (r
->id
.idiag_dport
!= tw
->tw_dport
&&
837 if (inet_twsk_diag_dump(tw
, skb
, cb
, r
, bc
) < 0) {
838 spin_unlock_bh(lock
);
845 spin_unlock_bh(lock
);
852 inet_diag_unlock_handler(handler
);
856 static int inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
858 struct nlattr
*bc
= NULL
;
859 int hdrlen
= sizeof(struct inet_diag_req
);
861 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
862 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
864 return __inet_diag_dump(skb
, cb
, (struct inet_diag_req
*)NLMSG_DATA(cb
->nlh
), bc
);
867 static inline int inet_diag_type2proto(int type
)
870 case TCPDIAG_GETSOCK
:
872 case DCCPDIAG_GETSOCK
:
879 static int inet_diag_dump_compat(struct sk_buff
*skb
, struct netlink_callback
*cb
)
881 struct inet_diag_req_compat
*rc
= NLMSG_DATA(cb
->nlh
);
882 struct inet_diag_req req
;
883 struct nlattr
*bc
= NULL
;
884 int hdrlen
= sizeof(struct inet_diag_req_compat
);
886 req
.sdiag_family
= AF_UNSPEC
; /* compatibility */
887 req
.sdiag_protocol
= inet_diag_type2proto(cb
->nlh
->nlmsg_type
);
888 req
.idiag_ext
= rc
->idiag_ext
;
889 req
.idiag_states
= rc
->idiag_states
;
892 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
893 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
895 return __inet_diag_dump(skb
, cb
, &req
, bc
);
898 static int inet_diag_get_exact_compat(struct sk_buff
*in_skb
,
899 const struct nlmsghdr
*nlh
)
901 struct inet_diag_req_compat
*rc
= NLMSG_DATA(nlh
);
902 struct inet_diag_req req
;
904 req
.sdiag_family
= rc
->idiag_family
;
905 req
.sdiag_protocol
= inet_diag_type2proto(nlh
->nlmsg_type
);
906 req
.idiag_ext
= rc
->idiag_ext
;
907 req
.idiag_states
= rc
->idiag_states
;
910 return inet_diag_get_exact(in_skb
, nlh
, &req
);
913 static int inet_diag_rcv_msg_compat(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
915 int hdrlen
= sizeof(struct inet_diag_req_compat
);
917 if (nlh
->nlmsg_type
>= INET_DIAG_GETSOCK_MAX
||
918 nlmsg_len(nlh
) < hdrlen
)
921 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
922 if (nlmsg_attrlen(nlh
, hdrlen
)) {
925 attr
= nlmsg_find_attr(nlh
, hdrlen
,
926 INET_DIAG_REQ_BYTECODE
);
928 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
929 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
933 return netlink_dump_start(sock_diag_nlsk
, skb
, nlh
,
934 inet_diag_dump_compat
, NULL
, 0);
937 return inet_diag_get_exact_compat(skb
, nlh
);
940 static int inet_diag_handler_dump(struct sk_buff
*skb
, struct nlmsghdr
*h
)
942 int hdrlen
= sizeof(struct inet_diag_req
);
944 if (nlmsg_len(h
) < hdrlen
)
947 if (h
->nlmsg_flags
& NLM_F_DUMP
) {
948 if (nlmsg_attrlen(h
, hdrlen
)) {
950 attr
= nlmsg_find_attr(h
, hdrlen
,
951 INET_DIAG_REQ_BYTECODE
);
953 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
954 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
958 return netlink_dump_start(sock_diag_nlsk
, skb
, h
,
959 inet_diag_dump
, NULL
, 0);
962 return inet_diag_get_exact(skb
, h
, (struct inet_diag_req
*)NLMSG_DATA(h
));
965 static struct sock_diag_handler inet_diag_handler
= {
967 .dump
= inet_diag_handler_dump
,
970 static struct sock_diag_handler inet6_diag_handler
= {
972 .dump
= inet_diag_handler_dump
,
975 int inet_diag_register(const struct inet_diag_handler
*h
)
977 const __u16 type
= h
->idiag_type
;
980 if (type
>= IPPROTO_MAX
)
983 mutex_lock(&inet_diag_table_mutex
);
985 if (inet_diag_table
[type
] == NULL
) {
986 inet_diag_table
[type
] = h
;
989 mutex_unlock(&inet_diag_table_mutex
);
993 EXPORT_SYMBOL_GPL(inet_diag_register
);
995 void inet_diag_unregister(const struct inet_diag_handler
*h
)
997 const __u16 type
= h
->idiag_type
;
999 if (type
>= IPPROTO_MAX
)
1002 mutex_lock(&inet_diag_table_mutex
);
1003 inet_diag_table
[type
] = NULL
;
1004 mutex_unlock(&inet_diag_table_mutex
);
1006 EXPORT_SYMBOL_GPL(inet_diag_unregister
);
1008 static int __init
inet_diag_init(void)
1010 const int inet_diag_table_size
= (IPPROTO_MAX
*
1011 sizeof(struct inet_diag_handler
*));
1014 inet_diag_table
= kzalloc(inet_diag_table_size
, GFP_KERNEL
);
1015 if (!inet_diag_table
)
1018 err
= sock_diag_register(&inet_diag_handler
);
1022 err
= sock_diag_register(&inet6_diag_handler
);
1026 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat
);
1031 sock_diag_unregister(&inet_diag_handler
);
1033 kfree(inet_diag_table
);
1037 static void __exit
inet_diag_exit(void)
1039 sock_diag_unregister(&inet6_diag_handler
);
1040 sock_diag_unregister(&inet_diag_handler
);
1041 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat
);
1042 kfree(inet_diag_table
);
1045 module_init(inet_diag_init
);
1046 module_exit(inet_diag_exit
);
1047 MODULE_LICENSE("GPL");
1048 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 0);