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
{
47 #if IS_ENABLED(CONFIG_IPV6)
48 struct in6_addr saddr_storage
; /* for IPv4-mapped-IPv6 addresses */
49 struct in6_addr daddr_storage
; /* for IPv4-mapped-IPv6 addresses */
53 static DEFINE_MUTEX(inet_diag_table_mutex
);
55 static const struct inet_diag_handler
*inet_diag_lock_handler(int proto
)
57 if (!inet_diag_table
[proto
])
58 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK
,
59 NETLINK_SOCK_DIAG
, AF_INET
, proto
);
61 mutex_lock(&inet_diag_table_mutex
);
62 if (!inet_diag_table
[proto
])
63 return ERR_PTR(-ENOENT
);
65 return inet_diag_table
[proto
];
68 static inline void inet_diag_unlock_handler(
69 const struct inet_diag_handler
*handler
)
71 mutex_unlock(&inet_diag_table_mutex
);
74 static size_t inet_sk_attr_size(void)
76 return nla_total_size(sizeof(struct tcp_info
))
77 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
78 + nla_total_size(1) /* INET_DIAG_TOS */
79 + nla_total_size(1) /* INET_DIAG_TCLASS */
80 + nla_total_size(sizeof(struct inet_diag_meminfo
))
81 + nla_total_size(sizeof(struct inet_diag_msg
))
82 + nla_total_size(SK_MEMINFO_VARS
* sizeof(u32
))
83 + nla_total_size(TCP_CA_NAME_MAX
)
84 + nla_total_size(sizeof(struct tcpvegas_info
))
88 int inet_sk_diag_fill(struct sock
*sk
, struct inet_connection_sock
*icsk
,
89 struct sk_buff
*skb
, struct inet_diag_req_v2
*req
,
90 struct user_namespace
*user_ns
,
91 u32 portid
, u32 seq
, u16 nlmsg_flags
,
92 const struct nlmsghdr
*unlh
)
94 const struct inet_sock
*inet
= inet_sk(sk
);
95 struct inet_diag_msg
*r
;
99 const struct inet_diag_handler
*handler
;
100 int ext
= req
->idiag_ext
;
102 handler
= inet_diag_table
[req
->sdiag_protocol
];
103 BUG_ON(handler
== NULL
);
105 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
111 BUG_ON(sk
->sk_state
== TCP_TIME_WAIT
);
113 r
->idiag_family
= sk
->sk_family
;
114 r
->idiag_state
= sk
->sk_state
;
116 r
->idiag_retrans
= 0;
118 r
->id
.idiag_if
= sk
->sk_bound_dev_if
;
119 sock_diag_save_cookie(sk
, r
->id
.idiag_cookie
);
121 r
->id
.idiag_sport
= inet
->inet_sport
;
122 r
->id
.idiag_dport
= inet
->inet_dport
;
124 memset(&r
->id
.idiag_src
, 0, sizeof(r
->id
.idiag_src
));
125 memset(&r
->id
.idiag_dst
, 0, sizeof(r
->id
.idiag_dst
));
127 r
->id
.idiag_src
[0] = inet
->inet_rcv_saddr
;
128 r
->id
.idiag_dst
[0] = inet
->inet_daddr
;
130 if (nla_put_u8(skb
, INET_DIAG_SHUTDOWN
, sk
->sk_shutdown
))
133 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
134 * hence this needs to be included regardless of socket family.
136 if (ext
& (1 << (INET_DIAG_TOS
- 1)))
137 if (nla_put_u8(skb
, INET_DIAG_TOS
, inet
->tos
) < 0)
140 #if IS_ENABLED(CONFIG_IPV6)
141 if (r
->idiag_family
== AF_INET6
) {
143 *(struct in6_addr
*)r
->id
.idiag_src
= sk
->sk_v6_rcv_saddr
;
144 *(struct in6_addr
*)r
->id
.idiag_dst
= sk
->sk_v6_daddr
;
146 if (ext
& (1 << (INET_DIAG_TCLASS
- 1)))
147 if (nla_put_u8(skb
, INET_DIAG_TCLASS
,
148 inet6_sk(sk
)->tclass
) < 0)
153 r
->idiag_uid
= from_kuid_munged(user_ns
, sock_i_uid(sk
));
154 r
->idiag_inode
= sock_i_ino(sk
);
156 if (ext
& (1 << (INET_DIAG_MEMINFO
- 1))) {
157 struct inet_diag_meminfo minfo
= {
158 .idiag_rmem
= sk_rmem_alloc_get(sk
),
159 .idiag_wmem
= sk
->sk_wmem_queued
,
160 .idiag_fmem
= sk
->sk_forward_alloc
,
161 .idiag_tmem
= sk_wmem_alloc_get(sk
),
164 if (nla_put(skb
, INET_DIAG_MEMINFO
, sizeof(minfo
), &minfo
) < 0)
168 if (ext
& (1 << (INET_DIAG_SKMEMINFO
- 1)))
169 if (sock_diag_put_meminfo(sk
, skb
, INET_DIAG_SKMEMINFO
))
173 handler
->idiag_get_info(sk
, r
, NULL
);
177 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
179 if (icsk
->icsk_pending
== ICSK_TIME_RETRANS
||
180 icsk
->icsk_pending
== ICSK_TIME_EARLY_RETRANS
||
181 icsk
->icsk_pending
== ICSK_TIME_LOSS_PROBE
) {
183 r
->idiag_retrans
= icsk
->icsk_retransmits
;
184 r
->idiag_expires
= EXPIRES_IN_MS(icsk
->icsk_timeout
);
185 } else if (icsk
->icsk_pending
== ICSK_TIME_PROBE0
) {
187 r
->idiag_retrans
= icsk
->icsk_probes_out
;
188 r
->idiag_expires
= EXPIRES_IN_MS(icsk
->icsk_timeout
);
189 } else if (timer_pending(&sk
->sk_timer
)) {
191 r
->idiag_retrans
= icsk
->icsk_probes_out
;
192 r
->idiag_expires
= EXPIRES_IN_MS(sk
->sk_timer
.expires
);
195 r
->idiag_expires
= 0;
199 if (ext
& (1 << (INET_DIAG_INFO
- 1))) {
200 attr
= nla_reserve(skb
, INET_DIAG_INFO
,
201 sizeof(struct tcp_info
));
205 info
= nla_data(attr
);
208 if ((ext
& (1 << (INET_DIAG_CONG
- 1))) && icsk
->icsk_ca_ops
)
209 if (nla_put_string(skb
, INET_DIAG_CONG
,
210 icsk
->icsk_ca_ops
->name
) < 0)
213 handler
->idiag_get_info(sk
, r
, info
);
215 if (sk
->sk_state
< TCP_TIME_WAIT
&&
216 icsk
->icsk_ca_ops
&& icsk
->icsk_ca_ops
->get_info
)
217 icsk
->icsk_ca_ops
->get_info(sk
, ext
, skb
);
224 nlmsg_cancel(skb
, nlh
);
227 EXPORT_SYMBOL_GPL(inet_sk_diag_fill
);
229 static int inet_csk_diag_fill(struct sock
*sk
,
230 struct sk_buff
*skb
, struct inet_diag_req_v2
*req
,
231 struct user_namespace
*user_ns
,
232 u32 portid
, u32 seq
, u16 nlmsg_flags
,
233 const struct nlmsghdr
*unlh
)
235 return inet_sk_diag_fill(sk
, inet_csk(sk
),
236 skb
, req
, user_ns
, portid
, seq
, nlmsg_flags
, unlh
);
239 static int inet_twsk_diag_fill(struct inet_timewait_sock
*tw
,
240 struct sk_buff
*skb
, struct inet_diag_req_v2
*req
,
241 u32 portid
, u32 seq
, u16 nlmsg_flags
,
242 const struct nlmsghdr
*unlh
)
245 struct inet_diag_msg
*r
;
246 struct nlmsghdr
*nlh
;
248 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
254 BUG_ON(tw
->tw_state
!= TCP_TIME_WAIT
);
256 tmo
= tw
->tw_ttd
- inet_tw_time_stamp();
260 r
->idiag_family
= tw
->tw_family
;
261 r
->idiag_retrans
= 0;
263 r
->id
.idiag_if
= tw
->tw_bound_dev_if
;
264 sock_diag_save_cookie(tw
, r
->id
.idiag_cookie
);
266 r
->id
.idiag_sport
= tw
->tw_sport
;
267 r
->id
.idiag_dport
= tw
->tw_dport
;
269 memset(&r
->id
.idiag_src
, 0, sizeof(r
->id
.idiag_src
));
270 memset(&r
->id
.idiag_dst
, 0, sizeof(r
->id
.idiag_dst
));
272 r
->id
.idiag_src
[0] = tw
->tw_rcv_saddr
;
273 r
->id
.idiag_dst
[0] = tw
->tw_daddr
;
275 r
->idiag_state
= tw
->tw_substate
;
277 r
->idiag_expires
= jiffies_to_msecs(tmo
);
282 #if IS_ENABLED(CONFIG_IPV6)
283 if (tw
->tw_family
== AF_INET6
) {
284 *(struct in6_addr
*)r
->id
.idiag_src
= tw
->tw_v6_rcv_saddr
;
285 *(struct in6_addr
*)r
->id
.idiag_dst
= tw
->tw_v6_daddr
;
293 static int sk_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
294 struct inet_diag_req_v2
*r
,
295 struct user_namespace
*user_ns
,
296 u32 portid
, u32 seq
, u16 nlmsg_flags
,
297 const struct nlmsghdr
*unlh
)
299 if (sk
->sk_state
== TCP_TIME_WAIT
)
300 return inet_twsk_diag_fill(inet_twsk(sk
), skb
, r
, portid
, seq
,
303 return inet_csk_diag_fill(sk
, skb
, r
, user_ns
, portid
, seq
,
307 int inet_diag_dump_one_icsk(struct inet_hashinfo
*hashinfo
, struct sk_buff
*in_skb
,
308 const struct nlmsghdr
*nlh
, struct inet_diag_req_v2
*req
)
313 struct net
*net
= sock_net(in_skb
->sk
);
316 if (req
->sdiag_family
== AF_INET
) {
317 sk
= inet_lookup(net
, hashinfo
, req
->id
.idiag_dst
[0],
318 req
->id
.idiag_dport
, req
->id
.idiag_src
[0],
319 req
->id
.idiag_sport
, req
->id
.idiag_if
);
321 #if IS_ENABLED(CONFIG_IPV6)
322 else if (req
->sdiag_family
== AF_INET6
) {
323 sk
= inet6_lookup(net
, hashinfo
,
324 (struct in6_addr
*)req
->id
.idiag_dst
,
326 (struct in6_addr
*)req
->id
.idiag_src
,
339 err
= sock_diag_check_cookie(sk
, req
->id
.idiag_cookie
);
343 rep
= nlmsg_new(inet_sk_attr_size(), GFP_KERNEL
);
349 err
= sk_diag_fill(sk
, rep
, req
,
350 sk_user_ns(NETLINK_CB(in_skb
).sk
),
351 NETLINK_CB(in_skb
).portid
,
352 nlh
->nlmsg_seq
, 0, nlh
);
354 WARN_ON(err
== -EMSGSIZE
);
358 err
= netlink_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
,
370 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk
);
372 static int inet_diag_get_exact(struct sk_buff
*in_skb
,
373 const struct nlmsghdr
*nlh
,
374 struct inet_diag_req_v2
*req
)
376 const struct inet_diag_handler
*handler
;
379 handler
= inet_diag_lock_handler(req
->sdiag_protocol
);
381 err
= PTR_ERR(handler
);
383 err
= handler
->dump_one(in_skb
, nlh
, req
);
384 inet_diag_unlock_handler(handler
);
389 static int bitstring_match(const __be32
*a1
, const __be32
*a2
, int bits
)
391 int words
= bits
>> 5;
396 if (memcmp(a1
, a2
, words
<< 2))
406 mask
= htonl((0xffffffff) << (32 - bits
));
408 if ((w1
^ w2
) & mask
)
416 static int inet_diag_bc_run(const struct nlattr
*_bc
,
417 const struct inet_diag_entry
*entry
)
419 const void *bc
= nla_data(_bc
);
420 int len
= nla_len(_bc
);
424 const struct inet_diag_bc_op
*op
= bc
;
427 case INET_DIAG_BC_NOP
:
429 case INET_DIAG_BC_JMP
:
432 case INET_DIAG_BC_S_GE
:
433 yes
= entry
->sport
>= op
[1].no
;
435 case INET_DIAG_BC_S_LE
:
436 yes
= entry
->sport
<= op
[1].no
;
438 case INET_DIAG_BC_D_GE
:
439 yes
= entry
->dport
>= op
[1].no
;
441 case INET_DIAG_BC_D_LE
:
442 yes
= entry
->dport
<= op
[1].no
;
444 case INET_DIAG_BC_AUTO
:
445 yes
= !(entry
->userlocks
& SOCK_BINDPORT_LOCK
);
447 case INET_DIAG_BC_S_COND
:
448 case INET_DIAG_BC_D_COND
: {
449 struct inet_diag_hostcond
*cond
;
452 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
453 if (cond
->port
!= -1 &&
454 cond
->port
!= (op
->code
== INET_DIAG_BC_S_COND
?
455 entry
->sport
: entry
->dport
)) {
460 if (op
->code
== INET_DIAG_BC_S_COND
)
465 if (cond
->family
!= AF_UNSPEC
&&
466 cond
->family
!= entry
->family
) {
467 if (entry
->family
== AF_INET6
&&
468 cond
->family
== AF_INET
) {
469 if (addr
[0] == 0 && addr
[1] == 0 &&
470 addr
[2] == htonl(0xffff) &&
471 bitstring_match(addr
+ 3,
480 if (cond
->prefix_len
== 0)
482 if (bitstring_match(addr
, cond
->addr
,
501 int inet_diag_bc_sk(const struct nlattr
*bc
, struct sock
*sk
)
503 struct inet_diag_entry entry
;
504 struct inet_sock
*inet
= inet_sk(sk
);
509 entry
.family
= sk
->sk_family
;
510 #if IS_ENABLED(CONFIG_IPV6)
511 if (entry
.family
== AF_INET6
) {
513 entry
.saddr
= sk
->sk_v6_rcv_saddr
.s6_addr32
;
514 entry
.daddr
= sk
->sk_v6_daddr
.s6_addr32
;
518 entry
.saddr
= &inet
->inet_rcv_saddr
;
519 entry
.daddr
= &inet
->inet_daddr
;
521 entry
.sport
= inet
->inet_num
;
522 entry
.dport
= ntohs(inet
->inet_dport
);
523 entry
.userlocks
= sk
->sk_userlocks
;
525 return inet_diag_bc_run(bc
, &entry
);
527 EXPORT_SYMBOL_GPL(inet_diag_bc_sk
);
529 static int valid_cc(const void *bc
, int len
, int cc
)
532 const struct inet_diag_bc_op
*op
= bc
;
538 if (op
->yes
< 4 || op
->yes
& 3)
546 /* Validate an inet_diag_hostcond. */
547 static bool valid_hostcond(const struct inet_diag_bc_op
*op
, int len
,
551 struct inet_diag_hostcond
*cond
;
553 /* Check hostcond space. */
554 *min_len
+= sizeof(struct inet_diag_hostcond
);
557 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
559 /* Check address family and address length. */
560 switch (cond
->family
) {
565 addr_len
= sizeof(struct in_addr
);
568 addr_len
= sizeof(struct in6_addr
);
573 *min_len
+= addr_len
;
577 /* Check prefix length (in bits) vs address length (in bytes). */
578 if (cond
->prefix_len
> 8 * addr_len
)
584 /* Validate a port comparison operator. */
585 static inline bool valid_port_comparison(const struct inet_diag_bc_op
*op
,
586 int len
, int *min_len
)
588 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
589 *min_len
+= sizeof(struct inet_diag_bc_op
);
595 static int inet_diag_bc_audit(const void *bytecode
, int bytecode_len
)
597 const void *bc
= bytecode
;
598 int len
= bytecode_len
;
601 const struct inet_diag_bc_op
*op
= bc
;
602 int min_len
= sizeof(struct inet_diag_bc_op
);
604 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
606 case INET_DIAG_BC_S_COND
:
607 case INET_DIAG_BC_D_COND
:
608 if (!valid_hostcond(bc
, len
, &min_len
))
611 case INET_DIAG_BC_S_GE
:
612 case INET_DIAG_BC_S_LE
:
613 case INET_DIAG_BC_D_GE
:
614 case INET_DIAG_BC_D_LE
:
615 if (!valid_port_comparison(bc
, len
, &min_len
))
618 case INET_DIAG_BC_AUTO
:
619 case INET_DIAG_BC_JMP
:
620 case INET_DIAG_BC_NOP
:
626 if (op
->code
!= INET_DIAG_BC_NOP
) {
627 if (op
->no
< min_len
|| op
->no
> len
+ 4 || op
->no
& 3)
630 !valid_cc(bytecode
, bytecode_len
, len
- op
->no
))
634 if (op
->yes
< min_len
|| op
->yes
> len
+ 4 || op
->yes
& 3)
639 return len
== 0 ? 0 : -EINVAL
;
642 static int inet_csk_diag_dump(struct sock
*sk
,
644 struct netlink_callback
*cb
,
645 struct inet_diag_req_v2
*r
,
646 const struct nlattr
*bc
)
648 if (!inet_diag_bc_sk(bc
, sk
))
651 return inet_csk_diag_fill(sk
, skb
, r
,
652 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
653 NETLINK_CB(cb
->skb
).portid
,
654 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
657 static int inet_twsk_diag_dump(struct sock
*sk
,
659 struct netlink_callback
*cb
,
660 struct inet_diag_req_v2
*r
,
661 const struct nlattr
*bc
)
663 struct inet_timewait_sock
*tw
= inet_twsk(sk
);
666 struct inet_diag_entry entry
;
668 entry
.family
= tw
->tw_family
;
669 #if IS_ENABLED(CONFIG_IPV6)
670 if (tw
->tw_family
== AF_INET6
) {
671 entry
.saddr
= tw
->tw_v6_rcv_saddr
.s6_addr32
;
672 entry
.daddr
= tw
->tw_v6_daddr
.s6_addr32
;
676 entry
.saddr
= &tw
->tw_rcv_saddr
;
677 entry
.daddr
= &tw
->tw_daddr
;
679 entry
.sport
= tw
->tw_num
;
680 entry
.dport
= ntohs(tw
->tw_dport
);
683 if (!inet_diag_bc_run(bc
, &entry
))
687 return inet_twsk_diag_fill(tw
, skb
, r
,
688 NETLINK_CB(cb
->skb
).portid
,
689 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
692 /* Get the IPv4, IPv6, or IPv4-mapped-IPv6 local and remote addresses
693 * from a request_sock. For IPv4-mapped-IPv6 we must map IPv4 to IPv6.
695 static inline void inet_diag_req_addrs(const struct sock
*sk
,
696 const struct request_sock
*req
,
697 struct inet_diag_entry
*entry
)
699 struct inet_request_sock
*ireq
= inet_rsk(req
);
701 #if IS_ENABLED(CONFIG_IPV6)
702 if (sk
->sk_family
== AF_INET6
) {
703 if (req
->rsk_ops
->family
== AF_INET6
) {
704 entry
->saddr
= ireq
->ir_v6_loc_addr
.s6_addr32
;
705 entry
->daddr
= ireq
->ir_v6_rmt_addr
.s6_addr32
;
706 } else if (req
->rsk_ops
->family
== AF_INET
) {
707 ipv6_addr_set_v4mapped(ireq
->ir_loc_addr
,
708 &entry
->saddr_storage
);
709 ipv6_addr_set_v4mapped(ireq
->ir_rmt_addr
,
710 &entry
->daddr_storage
);
711 entry
->saddr
= entry
->saddr_storage
.s6_addr32
;
712 entry
->daddr
= entry
->daddr_storage
.s6_addr32
;
717 entry
->saddr
= &ireq
->ir_loc_addr
;
718 entry
->daddr
= &ireq
->ir_rmt_addr
;
722 static int inet_diag_fill_req(struct sk_buff
*skb
, struct sock
*sk
,
723 struct request_sock
*req
,
724 struct user_namespace
*user_ns
,
726 const struct nlmsghdr
*unlh
)
728 const struct inet_request_sock
*ireq
= inet_rsk(req
);
729 struct inet_sock
*inet
= inet_sk(sk
);
730 struct inet_diag_msg
*r
;
731 struct nlmsghdr
*nlh
;
734 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
740 r
->idiag_family
= sk
->sk_family
;
741 r
->idiag_state
= TCP_SYN_RECV
;
743 r
->idiag_retrans
= req
->num_retrans
;
745 r
->id
.idiag_if
= sk
->sk_bound_dev_if
;
746 sock_diag_save_cookie(req
, r
->id
.idiag_cookie
);
748 tmo
= req
->expires
- jiffies
;
752 r
->id
.idiag_sport
= inet
->inet_sport
;
753 r
->id
.idiag_dport
= ireq
->ir_rmt_port
;
755 memset(&r
->id
.idiag_src
, 0, sizeof(r
->id
.idiag_src
));
756 memset(&r
->id
.idiag_dst
, 0, sizeof(r
->id
.idiag_dst
));
758 r
->id
.idiag_src
[0] = ireq
->ir_loc_addr
;
759 r
->id
.idiag_dst
[0] = ireq
->ir_rmt_addr
;
761 r
->idiag_expires
= jiffies_to_msecs(tmo
);
764 r
->idiag_uid
= from_kuid_munged(user_ns
, sock_i_uid(sk
));
766 #if IS_ENABLED(CONFIG_IPV6)
767 if (r
->idiag_family
== AF_INET6
) {
768 struct inet_diag_entry entry
;
769 inet_diag_req_addrs(sk
, req
, &entry
);
770 memcpy(r
->id
.idiag_src
, entry
.saddr
, sizeof(struct in6_addr
));
771 memcpy(r
->id
.idiag_dst
, entry
.daddr
, sizeof(struct in6_addr
));
779 static int inet_diag_dump_reqs(struct sk_buff
*skb
, struct sock
*sk
,
780 struct netlink_callback
*cb
,
781 struct inet_diag_req_v2
*r
,
782 const struct nlattr
*bc
)
784 struct inet_diag_entry entry
;
785 struct inet_connection_sock
*icsk
= inet_csk(sk
);
786 struct listen_sock
*lopt
;
787 struct inet_sock
*inet
= inet_sk(sk
);
789 int reqnum
, s_reqnum
;
793 s_reqnum
= cb
->args
[4];
798 entry
.family
= sk
->sk_family
;
800 read_lock_bh(&icsk
->icsk_accept_queue
.syn_wait_lock
);
802 lopt
= icsk
->icsk_accept_queue
.listen_opt
;
803 if (!lopt
|| !lopt
->qlen
)
807 entry
.sport
= inet
->inet_num
;
808 entry
.userlocks
= sk
->sk_userlocks
;
811 for (j
= s_j
; j
< lopt
->nr_table_entries
; j
++) {
812 struct request_sock
*req
, *head
= lopt
->syn_table
[j
];
815 for (req
= head
; req
; reqnum
++, req
= req
->dl_next
) {
816 struct inet_request_sock
*ireq
= inet_rsk(req
);
818 if (reqnum
< s_reqnum
)
820 if (r
->id
.idiag_dport
!= ireq
->ir_rmt_port
&&
825 inet_diag_req_addrs(sk
, req
, &entry
);
826 entry
.dport
= ntohs(ireq
->ir_rmt_port
);
828 if (!inet_diag_bc_run(bc
, &entry
))
832 err
= inet_diag_fill_req(skb
, sk
, req
,
833 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
834 NETLINK_CB(cb
->skb
).portid
,
835 cb
->nlh
->nlmsg_seq
, cb
->nlh
);
838 cb
->args
[4] = reqnum
;
847 read_unlock_bh(&icsk
->icsk_accept_queue
.syn_wait_lock
);
852 void inet_diag_dump_icsk(struct inet_hashinfo
*hashinfo
, struct sk_buff
*skb
,
853 struct netlink_callback
*cb
, struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
857 struct net
*net
= sock_net(skb
->sk
);
860 s_num
= num
= cb
->args
[2];
862 if (cb
->args
[0] == 0) {
863 if (!(r
->idiag_states
& (TCPF_LISTEN
| TCPF_SYN_RECV
)))
866 for (i
= s_i
; i
< INET_LHTABLE_SIZE
; i
++) {
868 struct hlist_nulls_node
*node
;
869 struct inet_listen_hashbucket
*ilb
;
872 ilb
= &hashinfo
->listening_hash
[i
];
873 spin_lock_bh(&ilb
->lock
);
874 sk_nulls_for_each(sk
, node
, &ilb
->head
) {
875 struct inet_sock
*inet
= inet_sk(sk
);
877 if (!net_eq(sock_net(sk
), net
))
885 if (r
->sdiag_family
!= AF_UNSPEC
&&
886 sk
->sk_family
!= r
->sdiag_family
)
889 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
893 if (!(r
->idiag_states
& TCPF_LISTEN
) ||
898 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
899 spin_unlock_bh(&ilb
->lock
);
904 if (!(r
->idiag_states
& TCPF_SYN_RECV
))
907 if (inet_diag_dump_reqs(skb
, sk
, cb
, r
, bc
) < 0) {
908 spin_unlock_bh(&ilb
->lock
);
917 spin_unlock_bh(&ilb
->lock
);
925 s_i
= num
= s_num
= 0;
928 if (!(r
->idiag_states
& ~(TCPF_LISTEN
| TCPF_SYN_RECV
)))
931 for (i
= s_i
; i
<= hashinfo
->ehash_mask
; i
++) {
932 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[i
];
933 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, i
);
935 struct hlist_nulls_node
*node
;
939 if (hlist_nulls_empty(&head
->chain
))
946 sk_nulls_for_each(sk
, node
, &head
->chain
) {
950 if (!net_eq(sock_net(sk
), net
))
954 state
= (sk
->sk_state
== TCP_TIME_WAIT
) ?
955 inet_twsk(sk
)->tw_substate
: sk
->sk_state
;
956 if (!(r
->idiag_states
& (1 << state
)))
958 if (r
->sdiag_family
!= AF_UNSPEC
&&
959 sk
->sk_family
!= r
->sdiag_family
)
961 if (r
->id
.idiag_sport
!= htons(sk
->sk_num
) &&
964 if (r
->id
.idiag_dport
!= sk
->sk_dport
&&
967 if (sk
->sk_state
== TCP_TIME_WAIT
)
968 res
= inet_twsk_diag_dump(sk
, skb
, cb
, r
, bc
);
970 res
= inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
);
972 spin_unlock_bh(lock
);
979 spin_unlock_bh(lock
);
988 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk
);
990 static int __inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
991 struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
993 const struct inet_diag_handler
*handler
;
996 handler
= inet_diag_lock_handler(r
->sdiag_protocol
);
997 if (!IS_ERR(handler
))
998 handler
->dump(skb
, cb
, r
, bc
);
1000 err
= PTR_ERR(handler
);
1001 inet_diag_unlock_handler(handler
);
1003 return err
? : skb
->len
;
1006 static int inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1008 struct nlattr
*bc
= NULL
;
1009 int hdrlen
= sizeof(struct inet_diag_req_v2
);
1011 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
1012 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
1014 return __inet_diag_dump(skb
, cb
, nlmsg_data(cb
->nlh
), bc
);
1017 static inline int inet_diag_type2proto(int type
)
1020 case TCPDIAG_GETSOCK
:
1022 case DCCPDIAG_GETSOCK
:
1023 return IPPROTO_DCCP
;
1029 static int inet_diag_dump_compat(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1031 struct inet_diag_req
*rc
= nlmsg_data(cb
->nlh
);
1032 struct inet_diag_req_v2 req
;
1033 struct nlattr
*bc
= NULL
;
1034 int hdrlen
= sizeof(struct inet_diag_req
);
1036 req
.sdiag_family
= AF_UNSPEC
; /* compatibility */
1037 req
.sdiag_protocol
= inet_diag_type2proto(cb
->nlh
->nlmsg_type
);
1038 req
.idiag_ext
= rc
->idiag_ext
;
1039 req
.idiag_states
= rc
->idiag_states
;
1042 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
1043 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
1045 return __inet_diag_dump(skb
, cb
, &req
, bc
);
1048 static int inet_diag_get_exact_compat(struct sk_buff
*in_skb
,
1049 const struct nlmsghdr
*nlh
)
1051 struct inet_diag_req
*rc
= nlmsg_data(nlh
);
1052 struct inet_diag_req_v2 req
;
1054 req
.sdiag_family
= rc
->idiag_family
;
1055 req
.sdiag_protocol
= inet_diag_type2proto(nlh
->nlmsg_type
);
1056 req
.idiag_ext
= rc
->idiag_ext
;
1057 req
.idiag_states
= rc
->idiag_states
;
1060 return inet_diag_get_exact(in_skb
, nlh
, &req
);
1063 static int inet_diag_rcv_msg_compat(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1065 int hdrlen
= sizeof(struct inet_diag_req
);
1066 struct net
*net
= sock_net(skb
->sk
);
1068 if (nlh
->nlmsg_type
>= INET_DIAG_GETSOCK_MAX
||
1069 nlmsg_len(nlh
) < hdrlen
)
1072 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1073 if (nlmsg_attrlen(nlh
, hdrlen
)) {
1074 struct nlattr
*attr
;
1076 attr
= nlmsg_find_attr(nlh
, hdrlen
,
1077 INET_DIAG_REQ_BYTECODE
);
1079 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
1080 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
1084 struct netlink_dump_control c
= {
1085 .dump
= inet_diag_dump_compat
,
1087 return netlink_dump_start(net
->diag_nlsk
, skb
, nlh
, &c
);
1091 return inet_diag_get_exact_compat(skb
, nlh
);
1094 static int inet_diag_handler_dump(struct sk_buff
*skb
, struct nlmsghdr
*h
)
1096 int hdrlen
= sizeof(struct inet_diag_req_v2
);
1097 struct net
*net
= sock_net(skb
->sk
);
1099 if (nlmsg_len(h
) < hdrlen
)
1102 if (h
->nlmsg_flags
& NLM_F_DUMP
) {
1103 if (nlmsg_attrlen(h
, hdrlen
)) {
1104 struct nlattr
*attr
;
1105 attr
= nlmsg_find_attr(h
, hdrlen
,
1106 INET_DIAG_REQ_BYTECODE
);
1108 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
1109 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
1113 struct netlink_dump_control c
= {
1114 .dump
= inet_diag_dump
,
1116 return netlink_dump_start(net
->diag_nlsk
, skb
, h
, &c
);
1120 return inet_diag_get_exact(skb
, h
, nlmsg_data(h
));
1123 static const struct sock_diag_handler inet_diag_handler
= {
1125 .dump
= inet_diag_handler_dump
,
1128 static const struct sock_diag_handler inet6_diag_handler
= {
1130 .dump
= inet_diag_handler_dump
,
1133 int inet_diag_register(const struct inet_diag_handler
*h
)
1135 const __u16 type
= h
->idiag_type
;
1138 if (type
>= IPPROTO_MAX
)
1141 mutex_lock(&inet_diag_table_mutex
);
1143 if (inet_diag_table
[type
] == NULL
) {
1144 inet_diag_table
[type
] = h
;
1147 mutex_unlock(&inet_diag_table_mutex
);
1151 EXPORT_SYMBOL_GPL(inet_diag_register
);
1153 void inet_diag_unregister(const struct inet_diag_handler
*h
)
1155 const __u16 type
= h
->idiag_type
;
1157 if (type
>= IPPROTO_MAX
)
1160 mutex_lock(&inet_diag_table_mutex
);
1161 inet_diag_table
[type
] = NULL
;
1162 mutex_unlock(&inet_diag_table_mutex
);
1164 EXPORT_SYMBOL_GPL(inet_diag_unregister
);
1166 static int __init
inet_diag_init(void)
1168 const int inet_diag_table_size
= (IPPROTO_MAX
*
1169 sizeof(struct inet_diag_handler
*));
1172 inet_diag_table
= kzalloc(inet_diag_table_size
, GFP_KERNEL
);
1173 if (!inet_diag_table
)
1176 err
= sock_diag_register(&inet_diag_handler
);
1180 err
= sock_diag_register(&inet6_diag_handler
);
1184 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat
);
1189 sock_diag_unregister(&inet_diag_handler
);
1191 kfree(inet_diag_table
);
1195 static void __exit
inet_diag_exit(void)
1197 sock_diag_unregister(&inet6_diag_handler
);
1198 sock_diag_unregister(&inet_diag_handler
);
1199 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat
);
1200 kfree(inet_diag_table
);
1203 module_init(inet_diag_init
);
1204 module_exit(inet_diag_exit
);
1205 MODULE_LICENSE("GPL");
1206 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2 /* AF_INET */);
1207 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 10 /* AF_INET6 */);