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
) {
142 const struct ipv6_pinfo
*np
= inet6_sk(sk
);
144 *(struct in6_addr
*)r
->id
.idiag_src
= np
->rcv_saddr
;
145 *(struct in6_addr
*)r
->id
.idiag_dst
= np
->daddr
;
147 if (ext
& (1 << (INET_DIAG_TCLASS
- 1)))
148 if (nla_put_u8(skb
, INET_DIAG_TCLASS
, np
->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
);
220 return nlmsg_end(skb
, nlh
);
223 nlmsg_cancel(skb
, nlh
);
226 EXPORT_SYMBOL_GPL(inet_sk_diag_fill
);
228 static int inet_csk_diag_fill(struct sock
*sk
,
229 struct sk_buff
*skb
, struct inet_diag_req_v2
*req
,
230 struct user_namespace
*user_ns
,
231 u32 portid
, u32 seq
, u16 nlmsg_flags
,
232 const struct nlmsghdr
*unlh
)
234 return inet_sk_diag_fill(sk
, inet_csk(sk
),
235 skb
, req
, user_ns
, portid
, seq
, nlmsg_flags
, unlh
);
238 static int inet_twsk_diag_fill(struct inet_timewait_sock
*tw
,
239 struct sk_buff
*skb
, struct inet_diag_req_v2
*req
,
240 u32 portid
, u32 seq
, u16 nlmsg_flags
,
241 const struct nlmsghdr
*unlh
)
244 struct inet_diag_msg
*r
;
245 struct nlmsghdr
*nlh
;
247 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
253 BUG_ON(tw
->tw_state
!= TCP_TIME_WAIT
);
255 tmo
= tw
->tw_ttd
- jiffies
;
259 r
->idiag_family
= tw
->tw_family
;
260 r
->idiag_retrans
= 0;
262 r
->id
.idiag_if
= tw
->tw_bound_dev_if
;
263 sock_diag_save_cookie(tw
, r
->id
.idiag_cookie
);
265 r
->id
.idiag_sport
= tw
->tw_sport
;
266 r
->id
.idiag_dport
= tw
->tw_dport
;
268 memset(&r
->id
.idiag_src
, 0, sizeof(r
->id
.idiag_src
));
269 memset(&r
->id
.idiag_dst
, 0, sizeof(r
->id
.idiag_dst
));
271 r
->id
.idiag_src
[0] = tw
->tw_rcv_saddr
;
272 r
->id
.idiag_dst
[0] = tw
->tw_daddr
;
274 r
->idiag_state
= tw
->tw_substate
;
276 r
->idiag_expires
= DIV_ROUND_UP(tmo
* 1000, HZ
);
281 #if IS_ENABLED(CONFIG_IPV6)
282 if (tw
->tw_family
== AF_INET6
) {
283 const struct inet6_timewait_sock
*tw6
=
284 inet6_twsk((struct sock
*)tw
);
286 *(struct in6_addr
*)r
->id
.idiag_src
= tw6
->tw_v6_rcv_saddr
;
287 *(struct in6_addr
*)r
->id
.idiag_dst
= tw6
->tw_v6_daddr
;
291 return nlmsg_end(skb
, nlh
);
294 static int sk_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
295 struct inet_diag_req_v2
*r
,
296 struct user_namespace
*user_ns
,
297 u32 portid
, u32 seq
, u16 nlmsg_flags
,
298 const struct nlmsghdr
*unlh
)
300 if (sk
->sk_state
== TCP_TIME_WAIT
)
301 return inet_twsk_diag_fill((struct inet_timewait_sock
*)sk
,
302 skb
, r
, portid
, seq
, nlmsg_flags
,
304 return inet_csk_diag_fill(sk
, skb
, r
, user_ns
, portid
, seq
, nlmsg_flags
, unlh
);
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
,
365 if (sk
->sk_state
== TCP_TIME_WAIT
)
366 inet_twsk_put((struct inet_timewait_sock
*)sk
);
373 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk
);
375 static int inet_diag_get_exact(struct sk_buff
*in_skb
,
376 const struct nlmsghdr
*nlh
,
377 struct inet_diag_req_v2
*req
)
379 const struct inet_diag_handler
*handler
;
382 handler
= inet_diag_lock_handler(req
->sdiag_protocol
);
384 err
= PTR_ERR(handler
);
386 err
= handler
->dump_one(in_skb
, nlh
, req
);
387 inet_diag_unlock_handler(handler
);
392 static int bitstring_match(const __be32
*a1
, const __be32
*a2
, int bits
)
394 int words
= bits
>> 5;
399 if (memcmp(a1
, a2
, words
<< 2))
409 mask
= htonl((0xffffffff) << (32 - bits
));
411 if ((w1
^ w2
) & mask
)
419 static int inet_diag_bc_run(const struct nlattr
*_bc
,
420 const struct inet_diag_entry
*entry
)
422 const void *bc
= nla_data(_bc
);
423 int len
= nla_len(_bc
);
427 const struct inet_diag_bc_op
*op
= bc
;
430 case INET_DIAG_BC_NOP
:
432 case INET_DIAG_BC_JMP
:
435 case INET_DIAG_BC_S_GE
:
436 yes
= entry
->sport
>= op
[1].no
;
438 case INET_DIAG_BC_S_LE
:
439 yes
= entry
->sport
<= op
[1].no
;
441 case INET_DIAG_BC_D_GE
:
442 yes
= entry
->dport
>= op
[1].no
;
444 case INET_DIAG_BC_D_LE
:
445 yes
= entry
->dport
<= op
[1].no
;
447 case INET_DIAG_BC_AUTO
:
448 yes
= !(entry
->userlocks
& SOCK_BINDPORT_LOCK
);
450 case INET_DIAG_BC_S_COND
:
451 case INET_DIAG_BC_D_COND
: {
452 struct inet_diag_hostcond
*cond
;
455 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
456 if (cond
->port
!= -1 &&
457 cond
->port
!= (op
->code
== INET_DIAG_BC_S_COND
?
458 entry
->sport
: entry
->dport
)) {
463 if (op
->code
== INET_DIAG_BC_S_COND
)
468 if (cond
->family
!= AF_UNSPEC
&&
469 cond
->family
!= entry
->family
) {
470 if (entry
->family
== AF_INET6
&&
471 cond
->family
== AF_INET
) {
472 if (addr
[0] == 0 && addr
[1] == 0 &&
473 addr
[2] == htonl(0xffff) &&
474 bitstring_match(addr
+ 3,
483 if (cond
->prefix_len
== 0)
485 if (bitstring_match(addr
, cond
->addr
,
504 int inet_diag_bc_sk(const struct nlattr
*bc
, struct sock
*sk
)
506 struct inet_diag_entry entry
;
507 struct inet_sock
*inet
= inet_sk(sk
);
512 entry
.family
= sk
->sk_family
;
513 #if IS_ENABLED(CONFIG_IPV6)
514 if (entry
.family
== AF_INET6
) {
515 struct ipv6_pinfo
*np
= inet6_sk(sk
);
517 entry
.saddr
= np
->rcv_saddr
.s6_addr32
;
518 entry
.daddr
= np
->daddr
.s6_addr32
;
522 entry
.saddr
= &inet
->inet_rcv_saddr
;
523 entry
.daddr
= &inet
->inet_daddr
;
525 entry
.sport
= inet
->inet_num
;
526 entry
.dport
= ntohs(inet
->inet_dport
);
527 entry
.userlocks
= sk
->sk_userlocks
;
529 return inet_diag_bc_run(bc
, &entry
);
531 EXPORT_SYMBOL_GPL(inet_diag_bc_sk
);
533 static int valid_cc(const void *bc
, int len
, int cc
)
536 const struct inet_diag_bc_op
*op
= bc
;
542 if (op
->yes
< 4 || op
->yes
& 3)
550 /* Validate an inet_diag_hostcond. */
551 static bool valid_hostcond(const struct inet_diag_bc_op
*op
, int len
,
555 struct inet_diag_hostcond
*cond
;
557 /* Check hostcond space. */
558 *min_len
+= sizeof(struct inet_diag_hostcond
);
561 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
563 /* Check address family and address length. */
564 switch (cond
->family
) {
569 addr_len
= sizeof(struct in_addr
);
572 addr_len
= sizeof(struct in6_addr
);
577 *min_len
+= addr_len
;
581 /* Check prefix length (in bits) vs address length (in bytes). */
582 if (cond
->prefix_len
> 8 * addr_len
)
588 /* Validate a port comparison operator. */
589 static inline bool valid_port_comparison(const struct inet_diag_bc_op
*op
,
590 int len
, int *min_len
)
592 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
593 *min_len
+= sizeof(struct inet_diag_bc_op
);
599 static int inet_diag_bc_audit(const void *bytecode
, int bytecode_len
)
601 const void *bc
= bytecode
;
602 int len
= bytecode_len
;
605 const struct inet_diag_bc_op
*op
= bc
;
606 int min_len
= sizeof(struct inet_diag_bc_op
);
608 //printk("BC: %d %d %d {%d} / %d\n", op->code, op->yes, op->no, op[1].no, len);
610 case INET_DIAG_BC_S_COND
:
611 case INET_DIAG_BC_D_COND
:
612 if (!valid_hostcond(bc
, len
, &min_len
))
615 case INET_DIAG_BC_S_GE
:
616 case INET_DIAG_BC_S_LE
:
617 case INET_DIAG_BC_D_GE
:
618 case INET_DIAG_BC_D_LE
:
619 if (!valid_port_comparison(bc
, len
, &min_len
))
622 case INET_DIAG_BC_AUTO
:
623 case INET_DIAG_BC_JMP
:
624 case INET_DIAG_BC_NOP
:
630 if (op
->code
!= INET_DIAG_BC_NOP
) {
631 if (op
->no
< min_len
|| op
->no
> len
+ 4 || op
->no
& 3)
634 !valid_cc(bytecode
, bytecode_len
, len
- op
->no
))
638 if (op
->yes
< min_len
|| op
->yes
> len
+ 4 || op
->yes
& 3)
643 return len
== 0 ? 0 : -EINVAL
;
646 static int inet_csk_diag_dump(struct sock
*sk
,
648 struct netlink_callback
*cb
,
649 struct inet_diag_req_v2
*r
,
650 const struct nlattr
*bc
)
652 if (!inet_diag_bc_sk(bc
, sk
))
655 return inet_csk_diag_fill(sk
, skb
, r
,
656 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
657 NETLINK_CB(cb
->skb
).portid
,
658 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
661 static int inet_twsk_diag_dump(struct inet_timewait_sock
*tw
,
663 struct netlink_callback
*cb
,
664 struct inet_diag_req_v2
*r
,
665 const struct nlattr
*bc
)
668 struct inet_diag_entry entry
;
670 entry
.family
= tw
->tw_family
;
671 #if IS_ENABLED(CONFIG_IPV6)
672 if (tw
->tw_family
== AF_INET6
) {
673 struct inet6_timewait_sock
*tw6
=
674 inet6_twsk((struct sock
*)tw
);
675 entry
.saddr
= tw6
->tw_v6_rcv_saddr
.s6_addr32
;
676 entry
.daddr
= tw6
->tw_v6_daddr
.s6_addr32
;
680 entry
.saddr
= &tw
->tw_rcv_saddr
;
681 entry
.daddr
= &tw
->tw_daddr
;
683 entry
.sport
= tw
->tw_num
;
684 entry
.dport
= ntohs(tw
->tw_dport
);
687 if (!inet_diag_bc_run(bc
, &entry
))
691 return inet_twsk_diag_fill(tw
, skb
, r
,
692 NETLINK_CB(cb
->skb
).portid
,
693 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
696 /* Get the IPv4, IPv6, or IPv4-mapped-IPv6 local and remote addresses
697 * from a request_sock. For IPv4-mapped-IPv6 we must map IPv4 to IPv6.
699 static inline void inet_diag_req_addrs(const struct sock
*sk
,
700 const struct request_sock
*req
,
701 struct inet_diag_entry
*entry
)
703 struct inet_request_sock
*ireq
= inet_rsk(req
);
705 #if IS_ENABLED(CONFIG_IPV6)
706 if (sk
->sk_family
== AF_INET6
) {
707 if (req
->rsk_ops
->family
== AF_INET6
) {
708 entry
->saddr
= inet6_rsk(req
)->loc_addr
.s6_addr32
;
709 entry
->daddr
= inet6_rsk(req
)->rmt_addr
.s6_addr32
;
710 } else if (req
->rsk_ops
->family
== AF_INET
) {
711 ipv6_addr_set_v4mapped(ireq
->loc_addr
,
712 &entry
->saddr_storage
);
713 ipv6_addr_set_v4mapped(ireq
->rmt_addr
,
714 &entry
->daddr_storage
);
715 entry
->saddr
= entry
->saddr_storage
.s6_addr32
;
716 entry
->daddr
= entry
->daddr_storage
.s6_addr32
;
721 entry
->saddr
= &ireq
->loc_addr
;
722 entry
->daddr
= &ireq
->rmt_addr
;
726 static int inet_diag_fill_req(struct sk_buff
*skb
, struct sock
*sk
,
727 struct request_sock
*req
,
728 struct user_namespace
*user_ns
,
730 const struct nlmsghdr
*unlh
)
732 const struct inet_request_sock
*ireq
= inet_rsk(req
);
733 struct inet_sock
*inet
= inet_sk(sk
);
734 struct inet_diag_msg
*r
;
735 struct nlmsghdr
*nlh
;
738 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
744 r
->idiag_family
= sk
->sk_family
;
745 r
->idiag_state
= TCP_SYN_RECV
;
747 r
->idiag_retrans
= req
->num_retrans
;
749 r
->id
.idiag_if
= sk
->sk_bound_dev_if
;
750 sock_diag_save_cookie(req
, r
->id
.idiag_cookie
);
752 tmo
= req
->expires
- jiffies
;
756 r
->id
.idiag_sport
= inet
->inet_sport
;
757 r
->id
.idiag_dport
= ireq
->rmt_port
;
759 memset(&r
->id
.idiag_src
, 0, sizeof(r
->id
.idiag_src
));
760 memset(&r
->id
.idiag_dst
, 0, sizeof(r
->id
.idiag_dst
));
762 r
->id
.idiag_src
[0] = ireq
->loc_addr
;
763 r
->id
.idiag_dst
[0] = ireq
->rmt_addr
;
765 r
->idiag_expires
= jiffies_to_msecs(tmo
);
768 r
->idiag_uid
= from_kuid_munged(user_ns
, sock_i_uid(sk
));
770 #if IS_ENABLED(CONFIG_IPV6)
771 if (r
->idiag_family
== AF_INET6
) {
772 struct inet_diag_entry entry
;
773 inet_diag_req_addrs(sk
, req
, &entry
);
774 memcpy(r
->id
.idiag_src
, entry
.saddr
, sizeof(struct in6_addr
));
775 memcpy(r
->id
.idiag_dst
, entry
.daddr
, sizeof(struct in6_addr
));
779 return nlmsg_end(skb
, nlh
);
782 static int inet_diag_dump_reqs(struct sk_buff
*skb
, struct sock
*sk
,
783 struct netlink_callback
*cb
,
784 struct inet_diag_req_v2
*r
,
785 const struct nlattr
*bc
)
787 struct inet_diag_entry entry
;
788 struct inet_connection_sock
*icsk
= inet_csk(sk
);
789 struct listen_sock
*lopt
;
790 struct inet_sock
*inet
= inet_sk(sk
);
792 int reqnum
, s_reqnum
;
796 s_reqnum
= cb
->args
[4];
801 entry
.family
= sk
->sk_family
;
803 read_lock_bh(&icsk
->icsk_accept_queue
.syn_wait_lock
);
805 lopt
= icsk
->icsk_accept_queue
.listen_opt
;
806 if (!lopt
|| !lopt
->qlen
)
810 entry
.sport
= inet
->inet_num
;
811 entry
.userlocks
= sk
->sk_userlocks
;
814 for (j
= s_j
; j
< lopt
->nr_table_entries
; j
++) {
815 struct request_sock
*req
, *head
= lopt
->syn_table
[j
];
818 for (req
= head
; req
; reqnum
++, req
= req
->dl_next
) {
819 struct inet_request_sock
*ireq
= inet_rsk(req
);
821 if (reqnum
< s_reqnum
)
823 if (r
->id
.idiag_dport
!= ireq
->rmt_port
&&
828 inet_diag_req_addrs(sk
, req
, &entry
);
829 entry
.dport
= ntohs(ireq
->rmt_port
);
831 if (!inet_diag_bc_run(bc
, &entry
))
835 err
= inet_diag_fill_req(skb
, sk
, req
,
836 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
837 NETLINK_CB(cb
->skb
).portid
,
838 cb
->nlh
->nlmsg_seq
, cb
->nlh
);
841 cb
->args
[4] = reqnum
;
850 read_unlock_bh(&icsk
->icsk_accept_queue
.syn_wait_lock
);
855 void inet_diag_dump_icsk(struct inet_hashinfo
*hashinfo
, struct sk_buff
*skb
,
856 struct netlink_callback
*cb
, struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
860 struct net
*net
= sock_net(skb
->sk
);
863 s_num
= num
= cb
->args
[2];
865 if (cb
->args
[0] == 0) {
866 if (!(r
->idiag_states
& (TCPF_LISTEN
| TCPF_SYN_RECV
)))
869 for (i
= s_i
; i
< INET_LHTABLE_SIZE
; i
++) {
871 struct hlist_nulls_node
*node
;
872 struct inet_listen_hashbucket
*ilb
;
875 ilb
= &hashinfo
->listening_hash
[i
];
876 spin_lock_bh(&ilb
->lock
);
877 sk_nulls_for_each(sk
, node
, &ilb
->head
) {
878 struct inet_sock
*inet
= inet_sk(sk
);
880 if (!net_eq(sock_net(sk
), net
))
888 if (r
->sdiag_family
!= AF_UNSPEC
&&
889 sk
->sk_family
!= r
->sdiag_family
)
892 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
896 if (!(r
->idiag_states
& TCPF_LISTEN
) ||
901 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
902 spin_unlock_bh(&ilb
->lock
);
907 if (!(r
->idiag_states
& TCPF_SYN_RECV
))
910 if (inet_diag_dump_reqs(skb
, sk
, cb
, r
, bc
) < 0) {
911 spin_unlock_bh(&ilb
->lock
);
920 spin_unlock_bh(&ilb
->lock
);
928 s_i
= num
= s_num
= 0;
931 if (!(r
->idiag_states
& ~(TCPF_LISTEN
| TCPF_SYN_RECV
)))
934 for (i
= s_i
; i
<= hashinfo
->ehash_mask
; i
++) {
935 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[i
];
936 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, i
);
938 struct hlist_nulls_node
*node
;
942 if (hlist_nulls_empty(&head
->chain
) &&
943 hlist_nulls_empty(&head
->twchain
))
950 sk_nulls_for_each(sk
, node
, &head
->chain
) {
951 struct inet_sock
*inet
= inet_sk(sk
);
953 if (!net_eq(sock_net(sk
), net
))
957 if (!(r
->idiag_states
& (1 << sk
->sk_state
)))
959 if (r
->sdiag_family
!= AF_UNSPEC
&&
960 sk
->sk_family
!= r
->sdiag_family
)
962 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
965 if (r
->id
.idiag_dport
!= inet
->inet_dport
&&
968 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
969 spin_unlock_bh(lock
);
976 if (r
->idiag_states
& (TCPF_TIME_WAIT
| TCPF_FIN_WAIT2
)) {
977 struct inet_timewait_sock
*tw
;
979 inet_twsk_for_each(tw
, node
,
981 if (!net_eq(twsk_net(tw
), net
))
986 if (!(r
->idiag_states
& (1 << tw
->tw_substate
)))
988 if (r
->sdiag_family
!= AF_UNSPEC
&&
989 tw
->tw_family
!= r
->sdiag_family
)
991 if (r
->id
.idiag_sport
!= tw
->tw_sport
&&
994 if (r
->id
.idiag_dport
!= tw
->tw_dport
&&
997 if (inet_twsk_diag_dump(tw
, skb
, cb
, r
, bc
) < 0) {
998 spin_unlock_bh(lock
);
1005 spin_unlock_bh(lock
);
1014 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk
);
1016 static int __inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
1017 struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
1019 const struct inet_diag_handler
*handler
;
1022 handler
= inet_diag_lock_handler(r
->sdiag_protocol
);
1023 if (!IS_ERR(handler
))
1024 handler
->dump(skb
, cb
, r
, bc
);
1026 err
= PTR_ERR(handler
);
1027 inet_diag_unlock_handler(handler
);
1029 return err
? : skb
->len
;
1032 static int inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1034 struct nlattr
*bc
= NULL
;
1035 int hdrlen
= sizeof(struct inet_diag_req_v2
);
1037 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
1038 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
1040 return __inet_diag_dump(skb
, cb
, nlmsg_data(cb
->nlh
), bc
);
1043 static inline int inet_diag_type2proto(int type
)
1046 case TCPDIAG_GETSOCK
:
1048 case DCCPDIAG_GETSOCK
:
1049 return IPPROTO_DCCP
;
1055 static int inet_diag_dump_compat(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1057 struct inet_diag_req
*rc
= nlmsg_data(cb
->nlh
);
1058 struct inet_diag_req_v2 req
;
1059 struct nlattr
*bc
= NULL
;
1060 int hdrlen
= sizeof(struct inet_diag_req
);
1062 req
.sdiag_family
= AF_UNSPEC
; /* compatibility */
1063 req
.sdiag_protocol
= inet_diag_type2proto(cb
->nlh
->nlmsg_type
);
1064 req
.idiag_ext
= rc
->idiag_ext
;
1065 req
.idiag_states
= rc
->idiag_states
;
1068 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
1069 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
1071 return __inet_diag_dump(skb
, cb
, &req
, bc
);
1074 static int inet_diag_get_exact_compat(struct sk_buff
*in_skb
,
1075 const struct nlmsghdr
*nlh
)
1077 struct inet_diag_req
*rc
= nlmsg_data(nlh
);
1078 struct inet_diag_req_v2 req
;
1080 req
.sdiag_family
= rc
->idiag_family
;
1081 req
.sdiag_protocol
= inet_diag_type2proto(nlh
->nlmsg_type
);
1082 req
.idiag_ext
= rc
->idiag_ext
;
1083 req
.idiag_states
= rc
->idiag_states
;
1086 return inet_diag_get_exact(in_skb
, nlh
, &req
);
1089 static int inet_diag_rcv_msg_compat(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1091 int hdrlen
= sizeof(struct inet_diag_req
);
1092 struct net
*net
= sock_net(skb
->sk
);
1094 if (nlh
->nlmsg_type
>= INET_DIAG_GETSOCK_MAX
||
1095 nlmsg_len(nlh
) < hdrlen
)
1098 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1099 if (nlmsg_attrlen(nlh
, hdrlen
)) {
1100 struct nlattr
*attr
;
1102 attr
= nlmsg_find_attr(nlh
, hdrlen
,
1103 INET_DIAG_REQ_BYTECODE
);
1105 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
1106 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
1110 struct netlink_dump_control c
= {
1111 .dump
= inet_diag_dump_compat
,
1113 return netlink_dump_start(net
->diag_nlsk
, skb
, nlh
, &c
);
1117 return inet_diag_get_exact_compat(skb
, nlh
);
1120 static int inet_diag_handler_dump(struct sk_buff
*skb
, struct nlmsghdr
*h
)
1122 int hdrlen
= sizeof(struct inet_diag_req_v2
);
1123 struct net
*net
= sock_net(skb
->sk
);
1125 if (nlmsg_len(h
) < hdrlen
)
1128 if (h
->nlmsg_flags
& NLM_F_DUMP
) {
1129 if (nlmsg_attrlen(h
, hdrlen
)) {
1130 struct nlattr
*attr
;
1131 attr
= nlmsg_find_attr(h
, hdrlen
,
1132 INET_DIAG_REQ_BYTECODE
);
1134 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
1135 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
1139 struct netlink_dump_control c
= {
1140 .dump
= inet_diag_dump
,
1142 return netlink_dump_start(net
->diag_nlsk
, skb
, h
, &c
);
1146 return inet_diag_get_exact(skb
, h
, nlmsg_data(h
));
1149 static const struct sock_diag_handler inet_diag_handler
= {
1151 .dump
= inet_diag_handler_dump
,
1154 static const struct sock_diag_handler inet6_diag_handler
= {
1156 .dump
= inet_diag_handler_dump
,
1159 int inet_diag_register(const struct inet_diag_handler
*h
)
1161 const __u16 type
= h
->idiag_type
;
1164 if (type
>= IPPROTO_MAX
)
1167 mutex_lock(&inet_diag_table_mutex
);
1169 if (inet_diag_table
[type
] == NULL
) {
1170 inet_diag_table
[type
] = h
;
1173 mutex_unlock(&inet_diag_table_mutex
);
1177 EXPORT_SYMBOL_GPL(inet_diag_register
);
1179 void inet_diag_unregister(const struct inet_diag_handler
*h
)
1181 const __u16 type
= h
->idiag_type
;
1183 if (type
>= IPPROTO_MAX
)
1186 mutex_lock(&inet_diag_table_mutex
);
1187 inet_diag_table
[type
] = NULL
;
1188 mutex_unlock(&inet_diag_table_mutex
);
1190 EXPORT_SYMBOL_GPL(inet_diag_unregister
);
1192 static int __init
inet_diag_init(void)
1194 const int inet_diag_table_size
= (IPPROTO_MAX
*
1195 sizeof(struct inet_diag_handler
*));
1198 inet_diag_table
= kzalloc(inet_diag_table_size
, GFP_KERNEL
);
1199 if (!inet_diag_table
)
1202 err
= sock_diag_register(&inet_diag_handler
);
1206 err
= sock_diag_register(&inet6_diag_handler
);
1210 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat
);
1215 sock_diag_unregister(&inet_diag_handler
);
1217 kfree(inet_diag_table
);
1221 static void __exit
inet_diag_exit(void)
1223 sock_diag_unregister(&inet6_diag_handler
);
1224 sock_diag_unregister(&inet_diag_handler
);
1225 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat
);
1226 kfree(inet_diag_table
);
1229 module_init(inet_diag_init
);
1230 module_exit(inet_diag_exit
);
1231 MODULE_LICENSE("GPL");
1232 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2 /* AF_INET */);
1233 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 10 /* AF_INET6 */);