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 static DEFINE_MUTEX(inet_diag_table_mutex
);
51 static const struct inet_diag_handler
*inet_diag_lock_handler(int proto
)
53 if (!inet_diag_table
[proto
])
54 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK
,
55 NETLINK_SOCK_DIAG
, AF_INET
, proto
);
57 mutex_lock(&inet_diag_table_mutex
);
58 if (!inet_diag_table
[proto
])
59 return ERR_PTR(-ENOENT
);
61 return inet_diag_table
[proto
];
64 static void inet_diag_unlock_handler(const struct inet_diag_handler
*handler
)
66 mutex_unlock(&inet_diag_table_mutex
);
69 static void inet_diag_msg_common_fill(struct inet_diag_msg
*r
, struct sock
*sk
)
71 r
->idiag_family
= sk
->sk_family
;
73 r
->id
.idiag_sport
= htons(sk
->sk_num
);
74 r
->id
.idiag_dport
= sk
->sk_dport
;
75 r
->id
.idiag_if
= sk
->sk_bound_dev_if
;
76 sock_diag_save_cookie(sk
, r
->id
.idiag_cookie
);
78 #if IS_ENABLED(CONFIG_IPV6)
79 if (sk
->sk_family
== AF_INET6
) {
80 *(struct in6_addr
*)r
->id
.idiag_src
= sk
->sk_v6_rcv_saddr
;
81 *(struct in6_addr
*)r
->id
.idiag_dst
= sk
->sk_v6_daddr
;
85 memset(&r
->id
.idiag_src
, 0, sizeof(r
->id
.idiag_src
));
86 memset(&r
->id
.idiag_dst
, 0, sizeof(r
->id
.idiag_dst
));
88 r
->id
.idiag_src
[0] = sk
->sk_rcv_saddr
;
89 r
->id
.idiag_dst
[0] = sk
->sk_daddr
;
93 static size_t inet_sk_attr_size(void)
95 return nla_total_size(sizeof(struct tcp_info
))
96 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
97 + nla_total_size(1) /* INET_DIAG_TOS */
98 + nla_total_size(1) /* INET_DIAG_TCLASS */
99 + nla_total_size(sizeof(struct inet_diag_meminfo
))
100 + nla_total_size(sizeof(struct inet_diag_msg
))
101 + nla_total_size(SK_MEMINFO_VARS
* sizeof(u32
))
102 + nla_total_size(TCP_CA_NAME_MAX
)
103 + nla_total_size(sizeof(struct tcpvegas_info
))
107 int inet_sk_diag_fill(struct sock
*sk
, struct inet_connection_sock
*icsk
,
108 struct sk_buff
*skb
, const struct inet_diag_req_v2
*req
,
109 struct user_namespace
*user_ns
,
110 u32 portid
, u32 seq
, u16 nlmsg_flags
,
111 const struct nlmsghdr
*unlh
)
113 const struct inet_sock
*inet
= inet_sk(sk
);
114 const struct tcp_congestion_ops
*ca_ops
;
115 const struct inet_diag_handler
*handler
;
116 int ext
= req
->idiag_ext
;
117 struct inet_diag_msg
*r
;
118 struct nlmsghdr
*nlh
;
122 handler
= inet_diag_table
[req
->sdiag_protocol
];
125 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
131 BUG_ON(!sk_fullsock(sk
));
133 inet_diag_msg_common_fill(r
, sk
);
134 r
->idiag_state
= sk
->sk_state
;
136 r
->idiag_retrans
= 0;
138 if (nla_put_u8(skb
, INET_DIAG_SHUTDOWN
, sk
->sk_shutdown
))
141 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
142 * hence this needs to be included regardless of socket family.
144 if (ext
& (1 << (INET_DIAG_TOS
- 1)))
145 if (nla_put_u8(skb
, INET_DIAG_TOS
, inet
->tos
) < 0)
148 #if IS_ENABLED(CONFIG_IPV6)
149 if (r
->idiag_family
== AF_INET6
) {
150 if (ext
& (1 << (INET_DIAG_TCLASS
- 1)))
151 if (nla_put_u8(skb
, INET_DIAG_TCLASS
,
152 inet6_sk(sk
)->tclass
) < 0)
157 r
->idiag_uid
= from_kuid_munged(user_ns
, sock_i_uid(sk
));
158 r
->idiag_inode
= sock_i_ino(sk
);
160 if (ext
& (1 << (INET_DIAG_MEMINFO
- 1))) {
161 struct inet_diag_meminfo minfo
= {
162 .idiag_rmem
= sk_rmem_alloc_get(sk
),
163 .idiag_wmem
= sk
->sk_wmem_queued
,
164 .idiag_fmem
= sk
->sk_forward_alloc
,
165 .idiag_tmem
= sk_wmem_alloc_get(sk
),
168 if (nla_put(skb
, INET_DIAG_MEMINFO
, sizeof(minfo
), &minfo
) < 0)
172 if (ext
& (1 << (INET_DIAG_SKMEMINFO
- 1)))
173 if (sock_diag_put_meminfo(sk
, skb
, INET_DIAG_SKMEMINFO
))
177 handler
->idiag_get_info(sk
, r
, NULL
);
181 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
183 if (icsk
->icsk_pending
== ICSK_TIME_RETRANS
||
184 icsk
->icsk_pending
== ICSK_TIME_EARLY_RETRANS
||
185 icsk
->icsk_pending
== ICSK_TIME_LOSS_PROBE
) {
187 r
->idiag_retrans
= icsk
->icsk_retransmits
;
188 r
->idiag_expires
= EXPIRES_IN_MS(icsk
->icsk_timeout
);
189 } else if (icsk
->icsk_pending
== ICSK_TIME_PROBE0
) {
191 r
->idiag_retrans
= icsk
->icsk_probes_out
;
192 r
->idiag_expires
= EXPIRES_IN_MS(icsk
->icsk_timeout
);
193 } else if (timer_pending(&sk
->sk_timer
)) {
195 r
->idiag_retrans
= icsk
->icsk_probes_out
;
196 r
->idiag_expires
= EXPIRES_IN_MS(sk
->sk_timer
.expires
);
199 r
->idiag_expires
= 0;
203 if (ext
& (1 << (INET_DIAG_INFO
- 1))) {
204 attr
= nla_reserve(skb
, INET_DIAG_INFO
,
205 sizeof(struct tcp_info
));
209 info
= nla_data(attr
);
212 if (ext
& (1 << (INET_DIAG_CONG
- 1))) {
216 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
218 err
= nla_put_string(skb
, INET_DIAG_CONG
, ca_ops
->name
);
224 handler
->idiag_get_info(sk
, r
, info
);
226 if (sk
->sk_state
< TCP_TIME_WAIT
) {
230 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
231 if (ca_ops
&& ca_ops
->get_info
)
232 err
= ca_ops
->get_info(sk
, ext
, skb
);
243 nlmsg_cancel(skb
, nlh
);
246 EXPORT_SYMBOL_GPL(inet_sk_diag_fill
);
248 static int inet_csk_diag_fill(struct sock
*sk
,
250 const struct inet_diag_req_v2
*req
,
251 struct user_namespace
*user_ns
,
252 u32 portid
, u32 seq
, u16 nlmsg_flags
,
253 const struct nlmsghdr
*unlh
)
255 return inet_sk_diag_fill(sk
, inet_csk(sk
), skb
, req
,
256 user_ns
, portid
, seq
, nlmsg_flags
, unlh
);
259 static int inet_twsk_diag_fill(struct sock
*sk
,
261 u32 portid
, u32 seq
, u16 nlmsg_flags
,
262 const struct nlmsghdr
*unlh
)
264 struct inet_timewait_sock
*tw
= inet_twsk(sk
);
265 struct inet_diag_msg
*r
;
266 struct nlmsghdr
*nlh
;
269 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
275 BUG_ON(tw
->tw_state
!= TCP_TIME_WAIT
);
277 tmo
= tw
->tw_timer
.expires
- jiffies
;
281 inet_diag_msg_common_fill(r
, sk
);
282 r
->idiag_retrans
= 0;
284 r
->idiag_state
= tw
->tw_substate
;
286 r
->idiag_expires
= jiffies_to_msecs(tmo
);
296 static int inet_req_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
297 u32 portid
, u32 seq
, u16 nlmsg_flags
,
298 const struct nlmsghdr
*unlh
)
300 struct inet_diag_msg
*r
;
301 struct nlmsghdr
*nlh
;
304 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
310 inet_diag_msg_common_fill(r
, sk
);
311 r
->idiag_state
= TCP_SYN_RECV
;
313 r
->idiag_retrans
= inet_reqsk(sk
)->num_retrans
;
315 BUILD_BUG_ON(offsetof(struct inet_request_sock
, ir_cookie
) !=
316 offsetof(struct sock
, sk_cookie
));
318 tmo
= inet_reqsk(sk
)->rsk_timer
.expires
- jiffies
;
319 r
->idiag_expires
= (tmo
>= 0) ? jiffies_to_msecs(tmo
) : 0;
329 static int sk_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
330 const struct inet_diag_req_v2
*r
,
331 struct user_namespace
*user_ns
,
332 u32 portid
, u32 seq
, u16 nlmsg_flags
,
333 const struct nlmsghdr
*unlh
)
335 if (sk
->sk_state
== TCP_TIME_WAIT
)
336 return inet_twsk_diag_fill(sk
, skb
, portid
, seq
,
339 if (sk
->sk_state
== TCP_NEW_SYN_RECV
)
340 return inet_req_diag_fill(sk
, skb
, portid
, seq
,
343 return inet_csk_diag_fill(sk
, skb
, r
, user_ns
, portid
, seq
,
347 int inet_diag_dump_one_icsk(struct inet_hashinfo
*hashinfo
,
348 struct sk_buff
*in_skb
,
349 const struct nlmsghdr
*nlh
,
350 const struct inet_diag_req_v2
*req
)
352 struct net
*net
= sock_net(in_skb
->sk
);
358 if (req
->sdiag_family
== AF_INET
)
359 sk
= inet_lookup(net
, hashinfo
, req
->id
.idiag_dst
[0],
360 req
->id
.idiag_dport
, req
->id
.idiag_src
[0],
361 req
->id
.idiag_sport
, req
->id
.idiag_if
);
362 #if IS_ENABLED(CONFIG_IPV6)
363 else if (req
->sdiag_family
== AF_INET6
)
364 sk
= inet6_lookup(net
, hashinfo
,
365 (struct in6_addr
*)req
->id
.idiag_dst
,
367 (struct in6_addr
*)req
->id
.idiag_src
,
378 err
= sock_diag_check_cookie(sk
, req
->id
.idiag_cookie
);
382 rep
= nlmsg_new(inet_sk_attr_size(), GFP_KERNEL
);
388 err
= sk_diag_fill(sk
, rep
, req
,
389 sk_user_ns(NETLINK_CB(in_skb
).sk
),
390 NETLINK_CB(in_skb
).portid
,
391 nlh
->nlmsg_seq
, 0, nlh
);
393 WARN_ON(err
== -EMSGSIZE
);
397 err
= netlink_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
,
409 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk
);
411 static int inet_diag_get_exact(struct sk_buff
*in_skb
,
412 const struct nlmsghdr
*nlh
,
413 const struct inet_diag_req_v2
*req
)
415 const struct inet_diag_handler
*handler
;
418 handler
= inet_diag_lock_handler(req
->sdiag_protocol
);
420 err
= PTR_ERR(handler
);
422 err
= handler
->dump_one(in_skb
, nlh
, req
);
423 inet_diag_unlock_handler(handler
);
428 static int bitstring_match(const __be32
*a1
, const __be32
*a2
, int bits
)
430 int words
= bits
>> 5;
435 if (memcmp(a1
, a2
, words
<< 2))
445 mask
= htonl((0xffffffff) << (32 - bits
));
447 if ((w1
^ w2
) & mask
)
454 static int inet_diag_bc_run(const struct nlattr
*_bc
,
455 const struct inet_diag_entry
*entry
)
457 const void *bc
= nla_data(_bc
);
458 int len
= nla_len(_bc
);
462 const struct inet_diag_bc_op
*op
= bc
;
465 case INET_DIAG_BC_NOP
:
467 case INET_DIAG_BC_JMP
:
470 case INET_DIAG_BC_S_GE
:
471 yes
= entry
->sport
>= op
[1].no
;
473 case INET_DIAG_BC_S_LE
:
474 yes
= entry
->sport
<= op
[1].no
;
476 case INET_DIAG_BC_D_GE
:
477 yes
= entry
->dport
>= op
[1].no
;
479 case INET_DIAG_BC_D_LE
:
480 yes
= entry
->dport
<= op
[1].no
;
482 case INET_DIAG_BC_AUTO
:
483 yes
= !(entry
->userlocks
& SOCK_BINDPORT_LOCK
);
485 case INET_DIAG_BC_S_COND
:
486 case INET_DIAG_BC_D_COND
: {
487 const struct inet_diag_hostcond
*cond
;
490 cond
= (const struct inet_diag_hostcond
*)(op
+ 1);
491 if (cond
->port
!= -1 &&
492 cond
->port
!= (op
->code
== INET_DIAG_BC_S_COND
?
493 entry
->sport
: entry
->dport
)) {
498 if (op
->code
== INET_DIAG_BC_S_COND
)
503 if (cond
->family
!= AF_UNSPEC
&&
504 cond
->family
!= entry
->family
) {
505 if (entry
->family
== AF_INET6
&&
506 cond
->family
== AF_INET
) {
507 if (addr
[0] == 0 && addr
[1] == 0 &&
508 addr
[2] == htonl(0xffff) &&
509 bitstring_match(addr
+ 3,
518 if (cond
->prefix_len
== 0)
520 if (bitstring_match(addr
, cond
->addr
,
539 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
541 static void entry_fill_addrs(struct inet_diag_entry
*entry
,
542 const struct sock
*sk
)
544 #if IS_ENABLED(CONFIG_IPV6)
545 if (sk
->sk_family
== AF_INET6
) {
546 entry
->saddr
= sk
->sk_v6_rcv_saddr
.s6_addr32
;
547 entry
->daddr
= sk
->sk_v6_daddr
.s6_addr32
;
551 entry
->saddr
= &sk
->sk_rcv_saddr
;
552 entry
->daddr
= &sk
->sk_daddr
;
556 int inet_diag_bc_sk(const struct nlattr
*bc
, struct sock
*sk
)
558 struct inet_sock
*inet
= inet_sk(sk
);
559 struct inet_diag_entry entry
;
564 entry
.family
= sk
->sk_family
;
565 entry_fill_addrs(&entry
, sk
);
566 entry
.sport
= inet
->inet_num
;
567 entry
.dport
= ntohs(inet
->inet_dport
);
568 entry
.userlocks
= sk_fullsock(sk
) ? sk
->sk_userlocks
: 0;
570 return inet_diag_bc_run(bc
, &entry
);
572 EXPORT_SYMBOL_GPL(inet_diag_bc_sk
);
574 static int valid_cc(const void *bc
, int len
, int cc
)
577 const struct inet_diag_bc_op
*op
= bc
;
583 if (op
->yes
< 4 || op
->yes
& 3)
591 /* Validate an inet_diag_hostcond. */
592 static bool valid_hostcond(const struct inet_diag_bc_op
*op
, int len
,
595 struct inet_diag_hostcond
*cond
;
598 /* Check hostcond space. */
599 *min_len
+= sizeof(struct inet_diag_hostcond
);
602 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
604 /* Check address family and address length. */
605 switch (cond
->family
) {
610 addr_len
= sizeof(struct in_addr
);
613 addr_len
= sizeof(struct in6_addr
);
618 *min_len
+= addr_len
;
622 /* Check prefix length (in bits) vs address length (in bytes). */
623 if (cond
->prefix_len
> 8 * addr_len
)
629 /* Validate a port comparison operator. */
630 static bool valid_port_comparison(const struct inet_diag_bc_op
*op
,
631 int len
, int *min_len
)
633 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
634 *min_len
+= sizeof(struct inet_diag_bc_op
);
640 static int inet_diag_bc_audit(const void *bytecode
, int bytecode_len
)
642 const void *bc
= bytecode
;
643 int len
= bytecode_len
;
646 int min_len
= sizeof(struct inet_diag_bc_op
);
647 const struct inet_diag_bc_op
*op
= bc
;
650 case INET_DIAG_BC_S_COND
:
651 case INET_DIAG_BC_D_COND
:
652 if (!valid_hostcond(bc
, len
, &min_len
))
655 case INET_DIAG_BC_S_GE
:
656 case INET_DIAG_BC_S_LE
:
657 case INET_DIAG_BC_D_GE
:
658 case INET_DIAG_BC_D_LE
:
659 if (!valid_port_comparison(bc
, len
, &min_len
))
662 case INET_DIAG_BC_AUTO
:
663 case INET_DIAG_BC_JMP
:
664 case INET_DIAG_BC_NOP
:
670 if (op
->code
!= INET_DIAG_BC_NOP
) {
671 if (op
->no
< min_len
|| op
->no
> len
+ 4 || op
->no
& 3)
674 !valid_cc(bytecode
, bytecode_len
, len
- op
->no
))
678 if (op
->yes
< min_len
|| op
->yes
> len
+ 4 || op
->yes
& 3)
683 return len
== 0 ? 0 : -EINVAL
;
686 static int inet_csk_diag_dump(struct sock
*sk
,
688 struct netlink_callback
*cb
,
689 const struct inet_diag_req_v2
*r
,
690 const struct nlattr
*bc
)
692 if (!inet_diag_bc_sk(bc
, sk
))
695 return inet_csk_diag_fill(sk
, skb
, r
,
696 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
697 NETLINK_CB(cb
->skb
).portid
,
698 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
701 static void twsk_build_assert(void)
703 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_family
) !=
704 offsetof(struct sock
, sk_family
));
706 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_num
) !=
707 offsetof(struct inet_sock
, inet_num
));
709 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_dport
) !=
710 offsetof(struct inet_sock
, inet_dport
));
712 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_rcv_saddr
) !=
713 offsetof(struct inet_sock
, inet_rcv_saddr
));
715 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_daddr
) !=
716 offsetof(struct inet_sock
, inet_daddr
));
718 #if IS_ENABLED(CONFIG_IPV6)
719 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_rcv_saddr
) !=
720 offsetof(struct sock
, sk_v6_rcv_saddr
));
722 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_daddr
) !=
723 offsetof(struct sock
, sk_v6_daddr
));
727 static int inet_diag_dump_reqs(struct sk_buff
*skb
, struct sock
*sk
,
728 struct netlink_callback
*cb
,
729 const struct inet_diag_req_v2
*r
,
730 const struct nlattr
*bc
)
732 struct inet_connection_sock
*icsk
= inet_csk(sk
);
733 struct inet_sock
*inet
= inet_sk(sk
);
734 struct inet_diag_entry entry
;
735 int j
, s_j
, reqnum
, s_reqnum
;
736 struct listen_sock
*lopt
;
740 s_reqnum
= cb
->args
[4];
745 entry
.family
= sk
->sk_family
;
747 spin_lock_bh(&icsk
->icsk_accept_queue
.syn_wait_lock
);
749 lopt
= icsk
->icsk_accept_queue
.listen_opt
;
750 if (!lopt
|| !listen_sock_qlen(lopt
))
754 entry
.sport
= inet
->inet_num
;
755 entry
.userlocks
= sk
->sk_userlocks
;
758 for (j
= s_j
; j
< lopt
->nr_table_entries
; j
++) {
759 struct request_sock
*req
, *head
= lopt
->syn_table
[j
];
762 for (req
= head
; req
; reqnum
++, req
= req
->dl_next
) {
763 struct inet_request_sock
*ireq
= inet_rsk(req
);
765 if (reqnum
< s_reqnum
)
767 if (r
->id
.idiag_dport
!= ireq
->ir_rmt_port
&&
772 /* Note: entry.sport and entry.userlocks are already set */
773 entry_fill_addrs(&entry
, req_to_sk(req
));
774 entry
.dport
= ntohs(ireq
->ir_rmt_port
);
776 if (!inet_diag_bc_run(bc
, &entry
))
780 err
= inet_req_diag_fill(req_to_sk(req
), skb
,
781 NETLINK_CB(cb
->skb
).portid
,
783 NLM_F_MULTI
, cb
->nlh
);
786 cb
->args
[4] = reqnum
;
795 spin_unlock_bh(&icsk
->icsk_accept_queue
.syn_wait_lock
);
800 void inet_diag_dump_icsk(struct inet_hashinfo
*hashinfo
, struct sk_buff
*skb
,
801 struct netlink_callback
*cb
,
802 const struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
804 struct net
*net
= sock_net(skb
->sk
);
805 int i
, num
, s_i
, s_num
;
808 s_num
= num
= cb
->args
[2];
810 if (cb
->args
[0] == 0) {
811 if (!(r
->idiag_states
& (TCPF_LISTEN
| TCPF_SYN_RECV
)))
814 for (i
= s_i
; i
< INET_LHTABLE_SIZE
; i
++) {
815 struct inet_listen_hashbucket
*ilb
;
816 struct hlist_nulls_node
*node
;
820 ilb
= &hashinfo
->listening_hash
[i
];
821 spin_lock_bh(&ilb
->lock
);
822 sk_nulls_for_each(sk
, node
, &ilb
->head
) {
823 struct inet_sock
*inet
= inet_sk(sk
);
825 if (!net_eq(sock_net(sk
), net
))
833 if (r
->sdiag_family
!= AF_UNSPEC
&&
834 sk
->sk_family
!= r
->sdiag_family
)
837 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
841 if (!(r
->idiag_states
& TCPF_LISTEN
) ||
846 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
847 spin_unlock_bh(&ilb
->lock
);
852 if (!(r
->idiag_states
& TCPF_SYN_RECV
))
855 if (inet_diag_dump_reqs(skb
, sk
, cb
, r
, bc
) < 0) {
856 spin_unlock_bh(&ilb
->lock
);
865 spin_unlock_bh(&ilb
->lock
);
873 s_i
= num
= s_num
= 0;
876 if (!(r
->idiag_states
& ~(TCPF_LISTEN
| TCPF_SYN_RECV
)))
879 for (i
= s_i
; i
<= hashinfo
->ehash_mask
; i
++) {
880 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[i
];
881 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, i
);
882 struct hlist_nulls_node
*node
;
887 if (hlist_nulls_empty(&head
->chain
))
894 sk_nulls_for_each(sk
, node
, &head
->chain
) {
897 if (!net_eq(sock_net(sk
), net
))
901 state
= (sk
->sk_state
== TCP_TIME_WAIT
) ?
902 inet_twsk(sk
)->tw_substate
: sk
->sk_state
;
903 if (!(r
->idiag_states
& (1 << state
)))
905 if (r
->sdiag_family
!= AF_UNSPEC
&&
906 sk
->sk_family
!= r
->sdiag_family
)
908 if (r
->id
.idiag_sport
!= htons(sk
->sk_num
) &&
911 if (r
->id
.idiag_dport
!= sk
->sk_dport
&&
916 if (!inet_diag_bc_sk(bc
, sk
))
919 res
= sk_diag_fill(sk
, skb
, r
,
920 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
921 NETLINK_CB(cb
->skb
).portid
,
922 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
925 spin_unlock_bh(lock
);
932 spin_unlock_bh(lock
);
941 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk
);
943 static int __inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
944 const struct inet_diag_req_v2
*r
,
947 const struct inet_diag_handler
*handler
;
950 handler
= inet_diag_lock_handler(r
->sdiag_protocol
);
951 if (!IS_ERR(handler
))
952 handler
->dump(skb
, cb
, r
, bc
);
954 err
= PTR_ERR(handler
);
955 inet_diag_unlock_handler(handler
);
957 return err
? : skb
->len
;
960 static int inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
962 int hdrlen
= sizeof(struct inet_diag_req_v2
);
963 struct nlattr
*bc
= NULL
;
965 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
966 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
968 return __inet_diag_dump(skb
, cb
, nlmsg_data(cb
->nlh
), bc
);
971 static int inet_diag_type2proto(int type
)
974 case TCPDIAG_GETSOCK
:
976 case DCCPDIAG_GETSOCK
:
983 static int inet_diag_dump_compat(struct sk_buff
*skb
,
984 struct netlink_callback
*cb
)
986 struct inet_diag_req
*rc
= nlmsg_data(cb
->nlh
);
987 int hdrlen
= sizeof(struct inet_diag_req
);
988 struct inet_diag_req_v2 req
;
989 struct nlattr
*bc
= NULL
;
991 req
.sdiag_family
= AF_UNSPEC
; /* compatibility */
992 req
.sdiag_protocol
= inet_diag_type2proto(cb
->nlh
->nlmsg_type
);
993 req
.idiag_ext
= rc
->idiag_ext
;
994 req
.idiag_states
= rc
->idiag_states
;
997 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
998 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
1000 return __inet_diag_dump(skb
, cb
, &req
, bc
);
1003 static int inet_diag_get_exact_compat(struct sk_buff
*in_skb
,
1004 const struct nlmsghdr
*nlh
)
1006 struct inet_diag_req
*rc
= nlmsg_data(nlh
);
1007 struct inet_diag_req_v2 req
;
1009 req
.sdiag_family
= rc
->idiag_family
;
1010 req
.sdiag_protocol
= inet_diag_type2proto(nlh
->nlmsg_type
);
1011 req
.idiag_ext
= rc
->idiag_ext
;
1012 req
.idiag_states
= rc
->idiag_states
;
1015 return inet_diag_get_exact(in_skb
, nlh
, &req
);
1018 static int inet_diag_rcv_msg_compat(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1020 int hdrlen
= sizeof(struct inet_diag_req
);
1021 struct net
*net
= sock_net(skb
->sk
);
1023 if (nlh
->nlmsg_type
>= INET_DIAG_GETSOCK_MAX
||
1024 nlmsg_len(nlh
) < hdrlen
)
1027 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1028 if (nlmsg_attrlen(nlh
, hdrlen
)) {
1029 struct nlattr
*attr
;
1031 attr
= nlmsg_find_attr(nlh
, hdrlen
,
1032 INET_DIAG_REQ_BYTECODE
);
1034 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
1035 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
1039 struct netlink_dump_control c
= {
1040 .dump
= inet_diag_dump_compat
,
1042 return netlink_dump_start(net
->diag_nlsk
, skb
, nlh
, &c
);
1046 return inet_diag_get_exact_compat(skb
, nlh
);
1049 static int inet_diag_handler_dump(struct sk_buff
*skb
, struct nlmsghdr
*h
)
1051 int hdrlen
= sizeof(struct inet_diag_req_v2
);
1052 struct net
*net
= sock_net(skb
->sk
);
1054 if (nlmsg_len(h
) < hdrlen
)
1057 if (h
->nlmsg_flags
& NLM_F_DUMP
) {
1058 if (nlmsg_attrlen(h
, hdrlen
)) {
1059 struct nlattr
*attr
;
1061 attr
= nlmsg_find_attr(h
, hdrlen
,
1062 INET_DIAG_REQ_BYTECODE
);
1064 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
1065 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
1069 struct netlink_dump_control c
= {
1070 .dump
= inet_diag_dump
,
1072 return netlink_dump_start(net
->diag_nlsk
, skb
, h
, &c
);
1076 return inet_diag_get_exact(skb
, h
, nlmsg_data(h
));
1079 static const struct sock_diag_handler inet_diag_handler
= {
1081 .dump
= inet_diag_handler_dump
,
1084 static const struct sock_diag_handler inet6_diag_handler
= {
1086 .dump
= inet_diag_handler_dump
,
1089 int inet_diag_register(const struct inet_diag_handler
*h
)
1091 const __u16 type
= h
->idiag_type
;
1094 if (type
>= IPPROTO_MAX
)
1097 mutex_lock(&inet_diag_table_mutex
);
1099 if (!inet_diag_table
[type
]) {
1100 inet_diag_table
[type
] = h
;
1103 mutex_unlock(&inet_diag_table_mutex
);
1107 EXPORT_SYMBOL_GPL(inet_diag_register
);
1109 void inet_diag_unregister(const struct inet_diag_handler
*h
)
1111 const __u16 type
= h
->idiag_type
;
1113 if (type
>= IPPROTO_MAX
)
1116 mutex_lock(&inet_diag_table_mutex
);
1117 inet_diag_table
[type
] = NULL
;
1118 mutex_unlock(&inet_diag_table_mutex
);
1120 EXPORT_SYMBOL_GPL(inet_diag_unregister
);
1122 static int __init
inet_diag_init(void)
1124 const int inet_diag_table_size
= (IPPROTO_MAX
*
1125 sizeof(struct inet_diag_handler
*));
1128 inet_diag_table
= kzalloc(inet_diag_table_size
, GFP_KERNEL
);
1129 if (!inet_diag_table
)
1132 err
= sock_diag_register(&inet_diag_handler
);
1136 err
= sock_diag_register(&inet6_diag_handler
);
1140 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat
);
1145 sock_diag_unregister(&inet_diag_handler
);
1147 kfree(inet_diag_table
);
1151 static void __exit
inet_diag_exit(void)
1153 sock_diag_unregister(&inet6_diag_handler
);
1154 sock_diag_unregister(&inet_diag_handler
);
1155 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat
);
1156 kfree(inet_diag_table
);
1159 module_init(inet_diag_init
);
1160 module_exit(inet_diag_exit
);
1161 MODULE_LICENSE("GPL");
1162 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2 /* AF_INET */);
1163 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 10 /* AF_INET6 */);