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 int inet_sk_diag_fill(struct sock
*sk
, struct inet_connection_sock
*icsk
,
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 struct inet_diag_msg
*r
;
82 struct inet_diag_meminfo
*minfo
= NULL
;
83 unsigned char *b
= skb_tail_pointer(skb
);
84 const struct inet_diag_handler
*handler
;
85 int ext
= req
->idiag_ext
;
87 handler
= inet_diag_table
[req
->sdiag_protocol
];
88 BUG_ON(handler
== NULL
);
90 nlh
= NLMSG_PUT(skb
, pid
, seq
, unlh
->nlmsg_type
, sizeof(*r
));
91 nlh
->nlmsg_flags
= nlmsg_flags
;
94 BUG_ON(sk
->sk_state
== TCP_TIME_WAIT
);
96 if (ext
& (1 << (INET_DIAG_MEMINFO
- 1)))
97 minfo
= INET_DIAG_PUT(skb
, INET_DIAG_MEMINFO
, sizeof(*minfo
));
99 r
->idiag_family
= sk
->sk_family
;
100 r
->idiag_state
= sk
->sk_state
;
102 r
->idiag_retrans
= 0;
104 r
->id
.idiag_if
= sk
->sk_bound_dev_if
;
105 r
->id
.idiag_cookie
[0] = (u32
)(unsigned long)sk
;
106 r
->id
.idiag_cookie
[1] = (u32
)(((unsigned long)sk
>> 31) >> 1);
108 r
->id
.idiag_sport
= inet
->inet_sport
;
109 r
->id
.idiag_dport
= inet
->inet_dport
;
110 r
->id
.idiag_src
[0] = inet
->inet_rcv_saddr
;
111 r
->id
.idiag_dst
[0] = inet
->inet_daddr
;
113 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
114 * hence this needs to be included regardless of socket family.
116 if (ext
& (1 << (INET_DIAG_TOS
- 1)))
117 RTA_PUT_U8(skb
, INET_DIAG_TOS
, inet
->tos
);
119 #if IS_ENABLED(CONFIG_IPV6)
120 if (r
->idiag_family
== AF_INET6
) {
121 const struct ipv6_pinfo
*np
= inet6_sk(sk
);
123 *(struct in6_addr
*)r
->id
.idiag_src
= np
->rcv_saddr
;
124 *(struct in6_addr
*)r
->id
.idiag_dst
= np
->daddr
;
125 if (ext
& (1 << (INET_DIAG_TCLASS
- 1)))
126 RTA_PUT_U8(skb
, INET_DIAG_TCLASS
, np
->tclass
);
130 r
->idiag_uid
= sock_i_uid(sk
);
131 r
->idiag_inode
= sock_i_ino(sk
);
134 minfo
->idiag_rmem
= sk_rmem_alloc_get(sk
);
135 minfo
->idiag_wmem
= sk
->sk_wmem_queued
;
136 minfo
->idiag_fmem
= sk
->sk_forward_alloc
;
137 minfo
->idiag_tmem
= sk_wmem_alloc_get(sk
);
141 r
->idiag_rqueue
= r
->idiag_wqueue
= 0;
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 if (ext
& (1 << (INET_DIAG_INFO
- 1)))
166 info
= INET_DIAG_PUT(skb
, INET_DIAG_INFO
, sizeof(struct tcp_info
));
168 if ((ext
& (1 << (INET_DIAG_CONG
- 1))) && icsk
->icsk_ca_ops
) {
169 const size_t len
= strlen(icsk
->icsk_ca_ops
->name
);
171 strcpy(INET_DIAG_PUT(skb
, INET_DIAG_CONG
, len
+ 1),
172 icsk
->icsk_ca_ops
->name
);
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
);
182 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
190 EXPORT_SYMBOL_GPL(inet_sk_diag_fill
);
192 static int inet_csk_diag_fill(struct sock
*sk
,
193 struct sk_buff
*skb
, struct inet_diag_req
*req
,
194 u32 pid
, u32 seq
, u16 nlmsg_flags
,
195 const struct nlmsghdr
*unlh
)
197 return inet_sk_diag_fill(sk
, inet_csk(sk
),
198 skb
, req
, pid
, seq
, nlmsg_flags
, unlh
);
201 static int inet_twsk_diag_fill(struct inet_timewait_sock
*tw
,
202 struct sk_buff
*skb
, struct inet_diag_req
*req
,
203 u32 pid
, u32 seq
, u16 nlmsg_flags
,
204 const struct nlmsghdr
*unlh
)
207 struct inet_diag_msg
*r
;
208 const unsigned char *previous_tail
= skb_tail_pointer(skb
);
209 struct nlmsghdr
*nlh
= NLMSG_PUT(skb
, pid
, seq
,
210 unlh
->nlmsg_type
, sizeof(*r
));
213 BUG_ON(tw
->tw_state
!= TCP_TIME_WAIT
);
215 nlh
->nlmsg_flags
= nlmsg_flags
;
217 tmo
= tw
->tw_ttd
- jiffies
;
221 r
->idiag_family
= tw
->tw_family
;
222 r
->idiag_retrans
= 0;
223 r
->id
.idiag_if
= tw
->tw_bound_dev_if
;
224 r
->id
.idiag_cookie
[0] = (u32
)(unsigned long)tw
;
225 r
->id
.idiag_cookie
[1] = (u32
)(((unsigned long)tw
>> 31) >> 1);
226 r
->id
.idiag_sport
= tw
->tw_sport
;
227 r
->id
.idiag_dport
= tw
->tw_dport
;
228 r
->id
.idiag_src
[0] = tw
->tw_rcv_saddr
;
229 r
->id
.idiag_dst
[0] = tw
->tw_daddr
;
230 r
->idiag_state
= tw
->tw_substate
;
232 r
->idiag_expires
= DIV_ROUND_UP(tmo
* 1000, HZ
);
237 #if IS_ENABLED(CONFIG_IPV6)
238 if (tw
->tw_family
== AF_INET6
) {
239 const struct inet6_timewait_sock
*tw6
=
240 inet6_twsk((struct sock
*)tw
);
242 *(struct in6_addr
*)r
->id
.idiag_src
= tw6
->tw_v6_rcv_saddr
;
243 *(struct in6_addr
*)r
->id
.idiag_dst
= tw6
->tw_v6_daddr
;
246 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - previous_tail
;
249 nlmsg_trim(skb
, previous_tail
);
253 static int sk_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
254 struct inet_diag_req
*r
, u32 pid
, u32 seq
, u16 nlmsg_flags
,
255 const struct nlmsghdr
*unlh
)
257 if (sk
->sk_state
== TCP_TIME_WAIT
)
258 return inet_twsk_diag_fill((struct inet_timewait_sock
*)sk
,
259 skb
, r
, pid
, seq
, nlmsg_flags
,
261 return inet_csk_diag_fill(sk
, skb
, r
, pid
, seq
, nlmsg_flags
, unlh
);
264 int inet_diag_check_cookie(struct sock
*sk
, struct inet_diag_req
*req
)
266 if ((req
->id
.idiag_cookie
[0] != INET_DIAG_NOCOOKIE
||
267 req
->id
.idiag_cookie
[1] != INET_DIAG_NOCOOKIE
) &&
268 ((u32
)(unsigned long)sk
!= req
->id
.idiag_cookie
[0] ||
269 (u32
)((((unsigned long)sk
) >> 31) >> 1) != req
->id
.idiag_cookie
[1]))
274 EXPORT_SYMBOL_GPL(inet_diag_check_cookie
);
276 int inet_diag_dump_one_icsk(struct inet_hashinfo
*hashinfo
, struct sk_buff
*in_skb
,
277 const struct nlmsghdr
*nlh
, struct inet_diag_req
*req
)
284 if (req
->sdiag_family
== AF_INET
) {
285 sk
= inet_lookup(&init_net
, hashinfo
, req
->id
.idiag_dst
[0],
286 req
->id
.idiag_dport
, req
->id
.idiag_src
[0],
287 req
->id
.idiag_sport
, req
->id
.idiag_if
);
289 #if IS_ENABLED(CONFIG_IPV6)
290 else if (req
->sdiag_family
== AF_INET6
) {
291 sk
= inet6_lookup(&init_net
, hashinfo
,
292 (struct in6_addr
*)req
->id
.idiag_dst
,
294 (struct in6_addr
*)req
->id
.idiag_src
,
307 err
= inet_diag_check_cookie(sk
, req
);
312 rep
= alloc_skb(NLMSG_SPACE((sizeof(struct inet_diag_msg
) +
313 sizeof(struct inet_diag_meminfo
) +
314 sizeof(struct tcp_info
) + 64)),
319 err
= sk_diag_fill(sk
, rep
, req
,
320 NETLINK_CB(in_skb
).pid
,
321 nlh
->nlmsg_seq
, 0, nlh
);
323 WARN_ON(err
== -EMSGSIZE
);
327 err
= netlink_unicast(sock_diag_nlsk
, rep
, NETLINK_CB(in_skb
).pid
,
334 if (sk
->sk_state
== TCP_TIME_WAIT
)
335 inet_twsk_put((struct inet_timewait_sock
*)sk
);
342 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk
);
344 static int inet_diag_get_exact(struct sk_buff
*in_skb
,
345 const struct nlmsghdr
*nlh
,
346 struct inet_diag_req
*req
)
348 const struct inet_diag_handler
*handler
;
351 handler
= inet_diag_lock_handler(req
->sdiag_protocol
);
353 err
= PTR_ERR(handler
);
355 err
= handler
->dump_one(in_skb
, nlh
, req
);
356 inet_diag_unlock_handler(handler
);
361 static int bitstring_match(const __be32
*a1
, const __be32
*a2
, int bits
)
363 int words
= bits
>> 5;
368 if (memcmp(a1
, a2
, words
<< 2))
378 mask
= htonl((0xffffffff) << (32 - bits
));
380 if ((w1
^ w2
) & mask
)
388 static int inet_diag_bc_run(const struct nlattr
*_bc
,
389 const struct inet_diag_entry
*entry
)
391 const void *bc
= nla_data(_bc
);
392 int len
= nla_len(_bc
);
396 const struct inet_diag_bc_op
*op
= bc
;
399 case INET_DIAG_BC_NOP
:
401 case INET_DIAG_BC_JMP
:
404 case INET_DIAG_BC_S_GE
:
405 yes
= entry
->sport
>= op
[1].no
;
407 case INET_DIAG_BC_S_LE
:
408 yes
= entry
->sport
<= op
[1].no
;
410 case INET_DIAG_BC_D_GE
:
411 yes
= entry
->dport
>= op
[1].no
;
413 case INET_DIAG_BC_D_LE
:
414 yes
= entry
->dport
<= op
[1].no
;
416 case INET_DIAG_BC_AUTO
:
417 yes
= !(entry
->userlocks
& SOCK_BINDPORT_LOCK
);
419 case INET_DIAG_BC_S_COND
:
420 case INET_DIAG_BC_D_COND
: {
421 struct inet_diag_hostcond
*cond
;
424 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
425 if (cond
->port
!= -1 &&
426 cond
->port
!= (op
->code
== INET_DIAG_BC_S_COND
?
427 entry
->sport
: entry
->dport
)) {
432 if (cond
->prefix_len
== 0)
435 if (op
->code
== INET_DIAG_BC_S_COND
)
440 if (bitstring_match(addr
, cond
->addr
,
443 if (entry
->family
== AF_INET6
&&
444 cond
->family
== AF_INET
) {
445 if (addr
[0] == 0 && addr
[1] == 0 &&
446 addr
[2] == htonl(0xffff) &&
447 bitstring_match(addr
+ 3, cond
->addr
,
467 int inet_diag_bc_sk(const struct nlattr
*bc
, struct sock
*sk
)
469 struct inet_diag_entry entry
;
470 struct inet_sock
*inet
= inet_sk(sk
);
475 entry
.family
= sk
->sk_family
;
476 #if IS_ENABLED(CONFIG_IPV6)
477 if (entry
.family
== AF_INET6
) {
478 struct ipv6_pinfo
*np
= inet6_sk(sk
);
480 entry
.saddr
= np
->rcv_saddr
.s6_addr32
;
481 entry
.daddr
= np
->daddr
.s6_addr32
;
485 entry
.saddr
= &inet
->inet_rcv_saddr
;
486 entry
.daddr
= &inet
->inet_daddr
;
488 entry
.sport
= inet
->inet_num
;
489 entry
.dport
= ntohs(inet
->inet_dport
);
490 entry
.userlocks
= sk
->sk_userlocks
;
492 return inet_diag_bc_run(bc
, &entry
);
494 EXPORT_SYMBOL_GPL(inet_diag_bc_sk
);
496 static int valid_cc(const void *bc
, int len
, int cc
)
499 const struct inet_diag_bc_op
*op
= bc
;
505 if (op
->yes
< 4 || op
->yes
& 3)
513 static int inet_diag_bc_audit(const void *bytecode
, int bytecode_len
)
515 const void *bc
= bytecode
;
516 int len
= bytecode_len
;
519 const struct inet_diag_bc_op
*op
= bc
;
521 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
523 case INET_DIAG_BC_AUTO
:
524 case INET_DIAG_BC_S_COND
:
525 case INET_DIAG_BC_D_COND
:
526 case INET_DIAG_BC_S_GE
:
527 case INET_DIAG_BC_S_LE
:
528 case INET_DIAG_BC_D_GE
:
529 case INET_DIAG_BC_D_LE
:
530 case INET_DIAG_BC_JMP
:
531 if (op
->no
< 4 || op
->no
> len
+ 4 || op
->no
& 3)
534 !valid_cc(bytecode
, bytecode_len
, len
- op
->no
))
537 case INET_DIAG_BC_NOP
:
542 if (op
->yes
< 4 || op
->yes
> len
+ 4 || op
->yes
& 3)
547 return len
== 0 ? 0 : -EINVAL
;
550 static int inet_csk_diag_dump(struct sock
*sk
,
552 struct netlink_callback
*cb
,
553 struct inet_diag_req
*r
,
554 const struct nlattr
*bc
)
556 if (!inet_diag_bc_sk(bc
, sk
))
559 return inet_csk_diag_fill(sk
, skb
, r
,
560 NETLINK_CB(cb
->skb
).pid
,
561 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
564 static int inet_twsk_diag_dump(struct inet_timewait_sock
*tw
,
566 struct netlink_callback
*cb
,
567 struct inet_diag_req
*r
,
568 const struct nlattr
*bc
)
571 struct inet_diag_entry entry
;
573 entry
.family
= tw
->tw_family
;
574 #if IS_ENABLED(CONFIG_IPV6)
575 if (tw
->tw_family
== AF_INET6
) {
576 struct inet6_timewait_sock
*tw6
=
577 inet6_twsk((struct sock
*)tw
);
578 entry
.saddr
= tw6
->tw_v6_rcv_saddr
.s6_addr32
;
579 entry
.daddr
= tw6
->tw_v6_daddr
.s6_addr32
;
583 entry
.saddr
= &tw
->tw_rcv_saddr
;
584 entry
.daddr
= &tw
->tw_daddr
;
586 entry
.sport
= tw
->tw_num
;
587 entry
.dport
= ntohs(tw
->tw_dport
);
590 if (!inet_diag_bc_run(bc
, &entry
))
594 return inet_twsk_diag_fill(tw
, skb
, r
,
595 NETLINK_CB(cb
->skb
).pid
,
596 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
599 static int inet_diag_fill_req(struct sk_buff
*skb
, struct sock
*sk
,
600 struct request_sock
*req
, u32 pid
, u32 seq
,
601 const struct nlmsghdr
*unlh
)
603 const struct inet_request_sock
*ireq
= inet_rsk(req
);
604 struct inet_sock
*inet
= inet_sk(sk
);
605 unsigned char *b
= skb_tail_pointer(skb
);
606 struct inet_diag_msg
*r
;
607 struct nlmsghdr
*nlh
;
610 nlh
= NLMSG_PUT(skb
, pid
, seq
, unlh
->nlmsg_type
, sizeof(*r
));
611 nlh
->nlmsg_flags
= NLM_F_MULTI
;
614 r
->idiag_family
= sk
->sk_family
;
615 r
->idiag_state
= TCP_SYN_RECV
;
617 r
->idiag_retrans
= req
->retrans
;
619 r
->id
.idiag_if
= sk
->sk_bound_dev_if
;
620 r
->id
.idiag_cookie
[0] = (u32
)(unsigned long)req
;
621 r
->id
.idiag_cookie
[1] = (u32
)(((unsigned long)req
>> 31) >> 1);
623 tmo
= req
->expires
- jiffies
;
627 r
->id
.idiag_sport
= inet
->inet_sport
;
628 r
->id
.idiag_dport
= ireq
->rmt_port
;
629 r
->id
.idiag_src
[0] = ireq
->loc_addr
;
630 r
->id
.idiag_dst
[0] = ireq
->rmt_addr
;
631 r
->idiag_expires
= jiffies_to_msecs(tmo
);
634 r
->idiag_uid
= sock_i_uid(sk
);
636 #if IS_ENABLED(CONFIG_IPV6)
637 if (r
->idiag_family
== AF_INET6
) {
638 *(struct in6_addr
*)r
->id
.idiag_src
= inet6_rsk(req
)->loc_addr
;
639 *(struct in6_addr
*)r
->id
.idiag_dst
= inet6_rsk(req
)->rmt_addr
;
642 nlh
->nlmsg_len
= skb_tail_pointer(skb
) - b
;
651 static int inet_diag_dump_reqs(struct sk_buff
*skb
, struct sock
*sk
,
652 struct netlink_callback
*cb
,
653 struct inet_diag_req
*r
,
654 const struct nlattr
*bc
)
656 struct inet_diag_entry entry
;
657 struct inet_connection_sock
*icsk
= inet_csk(sk
);
658 struct listen_sock
*lopt
;
659 struct inet_sock
*inet
= inet_sk(sk
);
661 int reqnum
, s_reqnum
;
665 s_reqnum
= cb
->args
[4];
670 entry
.family
= sk
->sk_family
;
672 read_lock_bh(&icsk
->icsk_accept_queue
.syn_wait_lock
);
674 lopt
= icsk
->icsk_accept_queue
.listen_opt
;
675 if (!lopt
|| !lopt
->qlen
)
679 entry
.sport
= inet
->inet_num
;
680 entry
.userlocks
= sk
->sk_userlocks
;
683 for (j
= s_j
; j
< lopt
->nr_table_entries
; j
++) {
684 struct request_sock
*req
, *head
= lopt
->syn_table
[j
];
687 for (req
= head
; req
; reqnum
++, req
= req
->dl_next
) {
688 struct inet_request_sock
*ireq
= inet_rsk(req
);
690 if (reqnum
< s_reqnum
)
692 if (r
->id
.idiag_dport
!= ireq
->rmt_port
&&
698 #if IS_ENABLED(CONFIG_IPV6)
699 (entry
.family
== AF_INET6
) ?
700 inet6_rsk(req
)->loc_addr
.s6_addr32
:
704 #if IS_ENABLED(CONFIG_IPV6)
705 (entry
.family
== AF_INET6
) ?
706 inet6_rsk(req
)->rmt_addr
.s6_addr32
:
709 entry
.dport
= ntohs(ireq
->rmt_port
);
711 if (!inet_diag_bc_run(bc
, &entry
))
715 err
= inet_diag_fill_req(skb
, sk
, req
,
716 NETLINK_CB(cb
->skb
).pid
,
717 cb
->nlh
->nlmsg_seq
, cb
->nlh
);
720 cb
->args
[4] = reqnum
;
729 read_unlock_bh(&icsk
->icsk_accept_queue
.syn_wait_lock
);
734 void inet_diag_dump_icsk(struct inet_hashinfo
*hashinfo
, struct sk_buff
*skb
,
735 struct netlink_callback
*cb
, struct inet_diag_req
*r
, struct nlattr
*bc
)
741 s_num
= num
= cb
->args
[2];
743 if (cb
->args
[0] == 0) {
744 if (!(r
->idiag_states
& (TCPF_LISTEN
| TCPF_SYN_RECV
)))
747 for (i
= s_i
; i
< INET_LHTABLE_SIZE
; i
++) {
749 struct hlist_nulls_node
*node
;
750 struct inet_listen_hashbucket
*ilb
;
753 ilb
= &hashinfo
->listening_hash
[i
];
754 spin_lock_bh(&ilb
->lock
);
755 sk_nulls_for_each(sk
, node
, &ilb
->head
) {
756 struct inet_sock
*inet
= inet_sk(sk
);
763 if (r
->sdiag_family
!= AF_UNSPEC
&&
764 sk
->sk_family
!= r
->sdiag_family
)
767 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
771 if (!(r
->idiag_states
& TCPF_LISTEN
) ||
776 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
777 spin_unlock_bh(&ilb
->lock
);
782 if (!(r
->idiag_states
& TCPF_SYN_RECV
))
785 if (inet_diag_dump_reqs(skb
, sk
, cb
, r
, bc
) < 0) {
786 spin_unlock_bh(&ilb
->lock
);
795 spin_unlock_bh(&ilb
->lock
);
803 s_i
= num
= s_num
= 0;
806 if (!(r
->idiag_states
& ~(TCPF_LISTEN
| TCPF_SYN_RECV
)))
809 for (i
= s_i
; i
<= hashinfo
->ehash_mask
; i
++) {
810 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[i
];
811 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, i
);
813 struct hlist_nulls_node
*node
;
817 if (hlist_nulls_empty(&head
->chain
) &&
818 hlist_nulls_empty(&head
->twchain
))
825 sk_nulls_for_each(sk
, node
, &head
->chain
) {
826 struct inet_sock
*inet
= inet_sk(sk
);
830 if (!(r
->idiag_states
& (1 << sk
->sk_state
)))
832 if (r
->sdiag_family
!= AF_UNSPEC
&&
833 sk
->sk_family
!= r
->sdiag_family
)
835 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
838 if (r
->id
.idiag_dport
!= inet
->inet_dport
&&
841 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
842 spin_unlock_bh(lock
);
849 if (r
->idiag_states
& TCPF_TIME_WAIT
) {
850 struct inet_timewait_sock
*tw
;
852 inet_twsk_for_each(tw
, node
,
857 if (r
->sdiag_family
!= AF_UNSPEC
&&
858 tw
->tw_family
!= r
->sdiag_family
)
860 if (r
->id
.idiag_sport
!= tw
->tw_sport
&&
863 if (r
->id
.idiag_dport
!= tw
->tw_dport
&&
866 if (inet_twsk_diag_dump(tw
, skb
, cb
, r
, bc
) < 0) {
867 spin_unlock_bh(lock
);
874 spin_unlock_bh(lock
);
883 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk
);
885 static int __inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
886 struct inet_diag_req
*r
, struct nlattr
*bc
)
888 const struct inet_diag_handler
*handler
;
890 handler
= inet_diag_lock_handler(r
->sdiag_protocol
);
891 if (!IS_ERR(handler
))
892 handler
->dump(skb
, cb
, r
, bc
);
893 inet_diag_unlock_handler(handler
);
898 static int inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
900 struct nlattr
*bc
= NULL
;
901 int hdrlen
= sizeof(struct inet_diag_req
);
903 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
904 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
906 return __inet_diag_dump(skb
, cb
, (struct inet_diag_req
*)NLMSG_DATA(cb
->nlh
), bc
);
909 static inline int inet_diag_type2proto(int type
)
912 case TCPDIAG_GETSOCK
:
914 case DCCPDIAG_GETSOCK
:
921 static int inet_diag_dump_compat(struct sk_buff
*skb
, struct netlink_callback
*cb
)
923 struct inet_diag_req_compat
*rc
= NLMSG_DATA(cb
->nlh
);
924 struct inet_diag_req req
;
925 struct nlattr
*bc
= NULL
;
926 int hdrlen
= sizeof(struct inet_diag_req_compat
);
928 req
.sdiag_family
= AF_UNSPEC
; /* compatibility */
929 req
.sdiag_protocol
= inet_diag_type2proto(cb
->nlh
->nlmsg_type
);
930 req
.idiag_ext
= rc
->idiag_ext
;
931 req
.idiag_states
= rc
->idiag_states
;
934 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
935 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
937 return __inet_diag_dump(skb
, cb
, &req
, bc
);
940 static int inet_diag_get_exact_compat(struct sk_buff
*in_skb
,
941 const struct nlmsghdr
*nlh
)
943 struct inet_diag_req_compat
*rc
= NLMSG_DATA(nlh
);
944 struct inet_diag_req req
;
946 req
.sdiag_family
= rc
->idiag_family
;
947 req
.sdiag_protocol
= inet_diag_type2proto(nlh
->nlmsg_type
);
948 req
.idiag_ext
= rc
->idiag_ext
;
949 req
.idiag_states
= rc
->idiag_states
;
952 return inet_diag_get_exact(in_skb
, nlh
, &req
);
955 static int inet_diag_rcv_msg_compat(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
957 int hdrlen
= sizeof(struct inet_diag_req_compat
);
959 if (nlh
->nlmsg_type
>= INET_DIAG_GETSOCK_MAX
||
960 nlmsg_len(nlh
) < hdrlen
)
963 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
964 if (nlmsg_attrlen(nlh
, hdrlen
)) {
967 attr
= nlmsg_find_attr(nlh
, hdrlen
,
968 INET_DIAG_REQ_BYTECODE
);
970 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
971 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
975 return netlink_dump_start(sock_diag_nlsk
, skb
, nlh
,
976 inet_diag_dump_compat
, NULL
, 0);
979 return inet_diag_get_exact_compat(skb
, nlh
);
982 static int inet_diag_handler_dump(struct sk_buff
*skb
, struct nlmsghdr
*h
)
984 int hdrlen
= sizeof(struct inet_diag_req
);
986 if (nlmsg_len(h
) < hdrlen
)
989 if (h
->nlmsg_flags
& NLM_F_DUMP
) {
990 if (nlmsg_attrlen(h
, hdrlen
)) {
992 attr
= nlmsg_find_attr(h
, hdrlen
,
993 INET_DIAG_REQ_BYTECODE
);
995 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
996 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
1000 return netlink_dump_start(sock_diag_nlsk
, skb
, h
,
1001 inet_diag_dump
, NULL
, 0);
1004 return inet_diag_get_exact(skb
, h
, (struct inet_diag_req
*)NLMSG_DATA(h
));
1007 static struct sock_diag_handler inet_diag_handler
= {
1009 .dump
= inet_diag_handler_dump
,
1012 static struct sock_diag_handler inet6_diag_handler
= {
1014 .dump
= inet_diag_handler_dump
,
1017 int inet_diag_register(const struct inet_diag_handler
*h
)
1019 const __u16 type
= h
->idiag_type
;
1022 if (type
>= IPPROTO_MAX
)
1025 mutex_lock(&inet_diag_table_mutex
);
1027 if (inet_diag_table
[type
] == NULL
) {
1028 inet_diag_table
[type
] = h
;
1031 mutex_unlock(&inet_diag_table_mutex
);
1035 EXPORT_SYMBOL_GPL(inet_diag_register
);
1037 void inet_diag_unregister(const struct inet_diag_handler
*h
)
1039 const __u16 type
= h
->idiag_type
;
1041 if (type
>= IPPROTO_MAX
)
1044 mutex_lock(&inet_diag_table_mutex
);
1045 inet_diag_table
[type
] = NULL
;
1046 mutex_unlock(&inet_diag_table_mutex
);
1048 EXPORT_SYMBOL_GPL(inet_diag_unregister
);
1050 static int __init
inet_diag_init(void)
1052 const int inet_diag_table_size
= (IPPROTO_MAX
*
1053 sizeof(struct inet_diag_handler
*));
1056 inet_diag_table
= kzalloc(inet_diag_table_size
, GFP_KERNEL
);
1057 if (!inet_diag_table
)
1060 err
= sock_diag_register(&inet_diag_handler
);
1064 err
= sock_diag_register(&inet6_diag_handler
);
1068 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat
);
1073 sock_diag_unregister(&inet_diag_handler
);
1075 kfree(inet_diag_table
);
1079 static void __exit
inet_diag_exit(void)
1081 sock_diag_unregister(&inet6_diag_handler
);
1082 sock_diag_unregister(&inet_diag_handler
);
1083 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat
);
1084 kfree(inet_diag_table
);
1087 module_init(inet_diag_init
);
1088 module_exit(inet_diag_exit
);
1089 MODULE_LICENSE("GPL");
1090 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 0);