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 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
;
92 EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill
);
94 static size_t inet_sk_attr_size(void)
96 return nla_total_size(sizeof(struct tcp_info
))
97 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
98 + nla_total_size(1) /* INET_DIAG_TOS */
99 + nla_total_size(1) /* INET_DIAG_TCLASS */
100 + nla_total_size(sizeof(struct inet_diag_meminfo
))
101 + nla_total_size(sizeof(struct inet_diag_msg
))
102 + nla_total_size(SK_MEMINFO_VARS
* sizeof(u32
))
103 + nla_total_size(TCP_CA_NAME_MAX
)
104 + nla_total_size(sizeof(struct tcpvegas_info
))
108 int inet_diag_msg_attrs_fill(struct sock
*sk
, struct sk_buff
*skb
,
109 struct inet_diag_msg
*r
, int ext
,
110 struct user_namespace
*user_ns
)
112 const struct inet_sock
*inet
= inet_sk(sk
);
114 if (nla_put_u8(skb
, INET_DIAG_SHUTDOWN
, sk
->sk_shutdown
))
117 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
118 * hence this needs to be included regardless of socket family.
120 if (ext
& (1 << (INET_DIAG_TOS
- 1)))
121 if (nla_put_u8(skb
, INET_DIAG_TOS
, inet
->tos
) < 0)
124 #if IS_ENABLED(CONFIG_IPV6)
125 if (r
->idiag_family
== AF_INET6
) {
126 if (ext
& (1 << (INET_DIAG_TCLASS
- 1)))
127 if (nla_put_u8(skb
, INET_DIAG_TCLASS
,
128 inet6_sk(sk
)->tclass
) < 0)
131 if (((1 << sk
->sk_state
) & (TCPF_LISTEN
| TCPF_CLOSE
)) &&
132 nla_put_u8(skb
, INET_DIAG_SKV6ONLY
, ipv6_only_sock(sk
)))
137 r
->idiag_uid
= from_kuid_munged(user_ns
, sock_i_uid(sk
));
138 r
->idiag_inode
= sock_i_ino(sk
);
144 EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill
);
146 int inet_sk_diag_fill(struct sock
*sk
, struct inet_connection_sock
*icsk
,
147 struct sk_buff
*skb
, const struct inet_diag_req_v2
*req
,
148 struct user_namespace
*user_ns
,
149 u32 portid
, u32 seq
, u16 nlmsg_flags
,
150 const struct nlmsghdr
*unlh
)
152 const struct tcp_congestion_ops
*ca_ops
;
153 const struct inet_diag_handler
*handler
;
154 int ext
= req
->idiag_ext
;
155 struct inet_diag_msg
*r
;
156 struct nlmsghdr
*nlh
;
160 handler
= inet_diag_table
[req
->sdiag_protocol
];
163 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
169 BUG_ON(!sk_fullsock(sk
));
171 inet_diag_msg_common_fill(r
, sk
);
172 r
->idiag_state
= sk
->sk_state
;
174 r
->idiag_retrans
= 0;
176 if (inet_diag_msg_attrs_fill(sk
, skb
, r
, ext
, user_ns
))
179 if (ext
& (1 << (INET_DIAG_MEMINFO
- 1))) {
180 struct inet_diag_meminfo minfo
= {
181 .idiag_rmem
= sk_rmem_alloc_get(sk
),
182 .idiag_wmem
= sk
->sk_wmem_queued
,
183 .idiag_fmem
= sk
->sk_forward_alloc
,
184 .idiag_tmem
= sk_wmem_alloc_get(sk
),
187 if (nla_put(skb
, INET_DIAG_MEMINFO
, sizeof(minfo
), &minfo
) < 0)
191 if (ext
& (1 << (INET_DIAG_SKMEMINFO
- 1)))
192 if (sock_diag_put_meminfo(sk
, skb
, INET_DIAG_SKMEMINFO
))
196 handler
->idiag_get_info(sk
, r
, NULL
);
200 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
202 if (icsk
->icsk_pending
== ICSK_TIME_RETRANS
||
203 icsk
->icsk_pending
== ICSK_TIME_EARLY_RETRANS
||
204 icsk
->icsk_pending
== ICSK_TIME_LOSS_PROBE
) {
206 r
->idiag_retrans
= icsk
->icsk_retransmits
;
207 r
->idiag_expires
= EXPIRES_IN_MS(icsk
->icsk_timeout
);
208 } else if (icsk
->icsk_pending
== ICSK_TIME_PROBE0
) {
210 r
->idiag_retrans
= icsk
->icsk_probes_out
;
211 r
->idiag_expires
= EXPIRES_IN_MS(icsk
->icsk_timeout
);
212 } else if (timer_pending(&sk
->sk_timer
)) {
214 r
->idiag_retrans
= icsk
->icsk_probes_out
;
215 r
->idiag_expires
= EXPIRES_IN_MS(sk
->sk_timer
.expires
);
218 r
->idiag_expires
= 0;
222 if ((ext
& (1 << (INET_DIAG_INFO
- 1))) && handler
->idiag_info_size
) {
223 attr
= nla_reserve(skb
, INET_DIAG_INFO
,
224 handler
->idiag_info_size
);
228 info
= nla_data(attr
);
231 if (ext
& (1 << (INET_DIAG_CONG
- 1))) {
235 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
237 err
= nla_put_string(skb
, INET_DIAG_CONG
, ca_ops
->name
);
243 handler
->idiag_get_info(sk
, r
, info
);
245 if (sk
->sk_state
< TCP_TIME_WAIT
) {
246 union tcp_cc_info info
;
251 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
252 if (ca_ops
&& ca_ops
->get_info
)
253 sz
= ca_ops
->get_info(sk
, ext
, &attr
, &info
);
255 if (sz
&& nla_put(skb
, attr
, sz
, &info
) < 0)
264 nlmsg_cancel(skb
, nlh
);
267 EXPORT_SYMBOL_GPL(inet_sk_diag_fill
);
269 static int inet_csk_diag_fill(struct sock
*sk
,
271 const struct inet_diag_req_v2
*req
,
272 struct user_namespace
*user_ns
,
273 u32 portid
, u32 seq
, u16 nlmsg_flags
,
274 const struct nlmsghdr
*unlh
)
276 return inet_sk_diag_fill(sk
, inet_csk(sk
), skb
, req
,
277 user_ns
, portid
, seq
, nlmsg_flags
, unlh
);
280 static int inet_twsk_diag_fill(struct sock
*sk
,
282 u32 portid
, u32 seq
, u16 nlmsg_flags
,
283 const struct nlmsghdr
*unlh
)
285 struct inet_timewait_sock
*tw
= inet_twsk(sk
);
286 struct inet_diag_msg
*r
;
287 struct nlmsghdr
*nlh
;
290 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
296 BUG_ON(tw
->tw_state
!= TCP_TIME_WAIT
);
298 tmo
= tw
->tw_timer
.expires
- jiffies
;
302 inet_diag_msg_common_fill(r
, sk
);
303 r
->idiag_retrans
= 0;
305 r
->idiag_state
= tw
->tw_substate
;
307 r
->idiag_expires
= jiffies_to_msecs(tmo
);
317 static int inet_req_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
318 u32 portid
, u32 seq
, u16 nlmsg_flags
,
319 const struct nlmsghdr
*unlh
)
321 struct inet_diag_msg
*r
;
322 struct nlmsghdr
*nlh
;
325 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
331 inet_diag_msg_common_fill(r
, sk
);
332 r
->idiag_state
= TCP_SYN_RECV
;
334 r
->idiag_retrans
= inet_reqsk(sk
)->num_retrans
;
336 BUILD_BUG_ON(offsetof(struct inet_request_sock
, ir_cookie
) !=
337 offsetof(struct sock
, sk_cookie
));
339 tmo
= inet_reqsk(sk
)->rsk_timer
.expires
- jiffies
;
340 r
->idiag_expires
= (tmo
>= 0) ? jiffies_to_msecs(tmo
) : 0;
350 static int sk_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
351 const struct inet_diag_req_v2
*r
,
352 struct user_namespace
*user_ns
,
353 u32 portid
, u32 seq
, u16 nlmsg_flags
,
354 const struct nlmsghdr
*unlh
)
356 if (sk
->sk_state
== TCP_TIME_WAIT
)
357 return inet_twsk_diag_fill(sk
, skb
, portid
, seq
,
360 if (sk
->sk_state
== TCP_NEW_SYN_RECV
)
361 return inet_req_diag_fill(sk
, skb
, portid
, seq
,
364 return inet_csk_diag_fill(sk
, skb
, r
, user_ns
, portid
, seq
,
368 struct sock
*inet_diag_find_one_icsk(struct net
*net
,
369 struct inet_hashinfo
*hashinfo
,
370 const struct inet_diag_req_v2
*req
)
375 if (req
->sdiag_family
== AF_INET
)
376 sk
= inet_lookup(net
, hashinfo
, NULL
, 0, req
->id
.idiag_dst
[0],
377 req
->id
.idiag_dport
, req
->id
.idiag_src
[0],
378 req
->id
.idiag_sport
, req
->id
.idiag_if
);
379 #if IS_ENABLED(CONFIG_IPV6)
380 else if (req
->sdiag_family
== AF_INET6
) {
381 if (ipv6_addr_v4mapped((struct in6_addr
*)req
->id
.idiag_dst
) &&
382 ipv6_addr_v4mapped((struct in6_addr
*)req
->id
.idiag_src
))
383 sk
= inet_lookup(net
, hashinfo
, NULL
, 0, req
->id
.idiag_dst
[3],
384 req
->id
.idiag_dport
, req
->id
.idiag_src
[3],
385 req
->id
.idiag_sport
, req
->id
.idiag_if
);
387 sk
= inet6_lookup(net
, hashinfo
, NULL
, 0,
388 (struct in6_addr
*)req
->id
.idiag_dst
,
390 (struct in6_addr
*)req
->id
.idiag_src
,
397 return ERR_PTR(-EINVAL
);
401 return ERR_PTR(-ENOENT
);
403 if (sock_diag_check_cookie(sk
, req
->id
.idiag_cookie
)) {
405 return ERR_PTR(-ENOENT
);
410 EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk
);
412 int inet_diag_dump_one_icsk(struct inet_hashinfo
*hashinfo
,
413 struct sk_buff
*in_skb
,
414 const struct nlmsghdr
*nlh
,
415 const struct inet_diag_req_v2
*req
)
417 struct net
*net
= sock_net(in_skb
->sk
);
422 sk
= inet_diag_find_one_icsk(net
, hashinfo
, req
);
426 rep
= nlmsg_new(inet_sk_attr_size(), GFP_KERNEL
);
432 err
= sk_diag_fill(sk
, rep
, req
,
433 sk_user_ns(NETLINK_CB(in_skb
).sk
),
434 NETLINK_CB(in_skb
).portid
,
435 nlh
->nlmsg_seq
, 0, nlh
);
437 WARN_ON(err
== -EMSGSIZE
);
441 err
= netlink_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
,
452 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk
);
454 static int inet_diag_cmd_exact(int cmd
, struct sk_buff
*in_skb
,
455 const struct nlmsghdr
*nlh
,
456 const struct inet_diag_req_v2
*req
)
458 const struct inet_diag_handler
*handler
;
461 handler
= inet_diag_lock_handler(req
->sdiag_protocol
);
463 err
= PTR_ERR(handler
);
464 else if (cmd
== SOCK_DIAG_BY_FAMILY
)
465 err
= handler
->dump_one(in_skb
, nlh
, req
);
466 else if (cmd
== SOCK_DESTROY
&& handler
->destroy
)
467 err
= handler
->destroy(in_skb
, req
);
470 inet_diag_unlock_handler(handler
);
475 static int bitstring_match(const __be32
*a1
, const __be32
*a2
, int bits
)
477 int words
= bits
>> 5;
482 if (memcmp(a1
, a2
, words
<< 2))
492 mask
= htonl((0xffffffff) << (32 - bits
));
494 if ((w1
^ w2
) & mask
)
501 static int inet_diag_bc_run(const struct nlattr
*_bc
,
502 const struct inet_diag_entry
*entry
)
504 const void *bc
= nla_data(_bc
);
505 int len
= nla_len(_bc
);
509 const struct inet_diag_bc_op
*op
= bc
;
512 case INET_DIAG_BC_NOP
:
514 case INET_DIAG_BC_JMP
:
517 case INET_DIAG_BC_S_GE
:
518 yes
= entry
->sport
>= op
[1].no
;
520 case INET_DIAG_BC_S_LE
:
521 yes
= entry
->sport
<= op
[1].no
;
523 case INET_DIAG_BC_D_GE
:
524 yes
= entry
->dport
>= op
[1].no
;
526 case INET_DIAG_BC_D_LE
:
527 yes
= entry
->dport
<= op
[1].no
;
529 case INET_DIAG_BC_AUTO
:
530 yes
= !(entry
->userlocks
& SOCK_BINDPORT_LOCK
);
532 case INET_DIAG_BC_S_COND
:
533 case INET_DIAG_BC_D_COND
: {
534 const struct inet_diag_hostcond
*cond
;
537 cond
= (const struct inet_diag_hostcond
*)(op
+ 1);
538 if (cond
->port
!= -1 &&
539 cond
->port
!= (op
->code
== INET_DIAG_BC_S_COND
?
540 entry
->sport
: entry
->dport
)) {
545 if (op
->code
== INET_DIAG_BC_S_COND
)
550 if (cond
->family
!= AF_UNSPEC
&&
551 cond
->family
!= entry
->family
) {
552 if (entry
->family
== AF_INET6
&&
553 cond
->family
== AF_INET
) {
554 if (addr
[0] == 0 && addr
[1] == 0 &&
555 addr
[2] == htonl(0xffff) &&
556 bitstring_match(addr
+ 3,
565 if (cond
->prefix_len
== 0)
567 if (bitstring_match(addr
, cond
->addr
,
586 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
588 static void entry_fill_addrs(struct inet_diag_entry
*entry
,
589 const struct sock
*sk
)
591 #if IS_ENABLED(CONFIG_IPV6)
592 if (sk
->sk_family
== AF_INET6
) {
593 entry
->saddr
= sk
->sk_v6_rcv_saddr
.s6_addr32
;
594 entry
->daddr
= sk
->sk_v6_daddr
.s6_addr32
;
598 entry
->saddr
= &sk
->sk_rcv_saddr
;
599 entry
->daddr
= &sk
->sk_daddr
;
603 int inet_diag_bc_sk(const struct nlattr
*bc
, struct sock
*sk
)
605 struct inet_sock
*inet
= inet_sk(sk
);
606 struct inet_diag_entry entry
;
611 entry
.family
= sk
->sk_family
;
612 entry_fill_addrs(&entry
, sk
);
613 entry
.sport
= inet
->inet_num
;
614 entry
.dport
= ntohs(inet
->inet_dport
);
615 entry
.userlocks
= sk_fullsock(sk
) ? sk
->sk_userlocks
: 0;
617 return inet_diag_bc_run(bc
, &entry
);
619 EXPORT_SYMBOL_GPL(inet_diag_bc_sk
);
621 static int valid_cc(const void *bc
, int len
, int cc
)
624 const struct inet_diag_bc_op
*op
= bc
;
630 if (op
->yes
< 4 || op
->yes
& 3)
638 /* Validate an inet_diag_hostcond. */
639 static bool valid_hostcond(const struct inet_diag_bc_op
*op
, int len
,
642 struct inet_diag_hostcond
*cond
;
645 /* Check hostcond space. */
646 *min_len
+= sizeof(struct inet_diag_hostcond
);
649 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
651 /* Check address family and address length. */
652 switch (cond
->family
) {
657 addr_len
= sizeof(struct in_addr
);
660 addr_len
= sizeof(struct in6_addr
);
665 *min_len
+= addr_len
;
669 /* Check prefix length (in bits) vs address length (in bytes). */
670 if (cond
->prefix_len
> 8 * addr_len
)
676 /* Validate a port comparison operator. */
677 static bool valid_port_comparison(const struct inet_diag_bc_op
*op
,
678 int len
, int *min_len
)
680 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
681 *min_len
+= sizeof(struct inet_diag_bc_op
);
687 static int inet_diag_bc_audit(const void *bytecode
, int bytecode_len
)
689 const void *bc
= bytecode
;
690 int len
= bytecode_len
;
693 int min_len
= sizeof(struct inet_diag_bc_op
);
694 const struct inet_diag_bc_op
*op
= bc
;
697 case INET_DIAG_BC_S_COND
:
698 case INET_DIAG_BC_D_COND
:
699 if (!valid_hostcond(bc
, len
, &min_len
))
702 case INET_DIAG_BC_S_GE
:
703 case INET_DIAG_BC_S_LE
:
704 case INET_DIAG_BC_D_GE
:
705 case INET_DIAG_BC_D_LE
:
706 if (!valid_port_comparison(bc
, len
, &min_len
))
709 case INET_DIAG_BC_AUTO
:
710 case INET_DIAG_BC_JMP
:
711 case INET_DIAG_BC_NOP
:
717 if (op
->code
!= INET_DIAG_BC_NOP
) {
718 if (op
->no
< min_len
|| op
->no
> len
+ 4 || op
->no
& 3)
721 !valid_cc(bytecode
, bytecode_len
, len
- op
->no
))
725 if (op
->yes
< min_len
|| op
->yes
> len
+ 4 || op
->yes
& 3)
730 return len
== 0 ? 0 : -EINVAL
;
733 static int inet_csk_diag_dump(struct sock
*sk
,
735 struct netlink_callback
*cb
,
736 const struct inet_diag_req_v2
*r
,
737 const struct nlattr
*bc
)
739 if (!inet_diag_bc_sk(bc
, sk
))
742 return inet_csk_diag_fill(sk
, skb
, r
,
743 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
744 NETLINK_CB(cb
->skb
).portid
,
745 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
748 static void twsk_build_assert(void)
750 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_family
) !=
751 offsetof(struct sock
, sk_family
));
753 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_num
) !=
754 offsetof(struct inet_sock
, inet_num
));
756 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_dport
) !=
757 offsetof(struct inet_sock
, inet_dport
));
759 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_rcv_saddr
) !=
760 offsetof(struct inet_sock
, inet_rcv_saddr
));
762 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_daddr
) !=
763 offsetof(struct inet_sock
, inet_daddr
));
765 #if IS_ENABLED(CONFIG_IPV6)
766 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_rcv_saddr
) !=
767 offsetof(struct sock
, sk_v6_rcv_saddr
));
769 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_daddr
) !=
770 offsetof(struct sock
, sk_v6_daddr
));
774 void inet_diag_dump_icsk(struct inet_hashinfo
*hashinfo
, struct sk_buff
*skb
,
775 struct netlink_callback
*cb
,
776 const struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
778 struct net
*net
= sock_net(skb
->sk
);
779 int i
, num
, s_i
, s_num
;
780 u32 idiag_states
= r
->idiag_states
;
782 if (idiag_states
& TCPF_SYN_RECV
)
783 idiag_states
|= TCPF_NEW_SYN_RECV
;
785 s_num
= num
= cb
->args
[2];
787 if (cb
->args
[0] == 0) {
788 if (!(idiag_states
& TCPF_LISTEN
))
791 for (i
= s_i
; i
< INET_LHTABLE_SIZE
; i
++) {
792 struct inet_listen_hashbucket
*ilb
;
796 ilb
= &hashinfo
->listening_hash
[i
];
797 spin_lock_bh(&ilb
->lock
);
798 sk_for_each(sk
, &ilb
->head
) {
799 struct inet_sock
*inet
= inet_sk(sk
);
801 if (!net_eq(sock_net(sk
), net
))
809 if (r
->sdiag_family
!= AF_UNSPEC
&&
810 sk
->sk_family
!= r
->sdiag_family
)
813 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
817 if (r
->id
.idiag_dport
||
821 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
822 spin_unlock_bh(&ilb
->lock
);
831 spin_unlock_bh(&ilb
->lock
);
839 s_i
= num
= s_num
= 0;
842 if (!(idiag_states
& ~TCPF_LISTEN
))
845 for (i
= s_i
; i
<= hashinfo
->ehash_mask
; i
++) {
846 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[i
];
847 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, i
);
848 struct hlist_nulls_node
*node
;
853 if (hlist_nulls_empty(&head
->chain
))
860 sk_nulls_for_each(sk
, node
, &head
->chain
) {
863 if (!net_eq(sock_net(sk
), net
))
867 state
= (sk
->sk_state
== TCP_TIME_WAIT
) ?
868 inet_twsk(sk
)->tw_substate
: sk
->sk_state
;
869 if (!(idiag_states
& (1 << state
)))
871 if (r
->sdiag_family
!= AF_UNSPEC
&&
872 sk
->sk_family
!= r
->sdiag_family
)
874 if (r
->id
.idiag_sport
!= htons(sk
->sk_num
) &&
877 if (r
->id
.idiag_dport
!= sk
->sk_dport
&&
882 if (!inet_diag_bc_sk(bc
, sk
))
885 res
= sk_diag_fill(sk
, skb
, r
,
886 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
887 NETLINK_CB(cb
->skb
).portid
,
888 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
891 spin_unlock_bh(lock
);
898 spin_unlock_bh(lock
);
908 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk
);
910 static int __inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
911 const struct inet_diag_req_v2
*r
,
914 const struct inet_diag_handler
*handler
;
917 handler
= inet_diag_lock_handler(r
->sdiag_protocol
);
918 if (!IS_ERR(handler
))
919 handler
->dump(skb
, cb
, r
, bc
);
921 err
= PTR_ERR(handler
);
922 inet_diag_unlock_handler(handler
);
924 return err
? : skb
->len
;
927 static int inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
929 int hdrlen
= sizeof(struct inet_diag_req_v2
);
930 struct nlattr
*bc
= NULL
;
932 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
933 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
935 return __inet_diag_dump(skb
, cb
, nlmsg_data(cb
->nlh
), bc
);
938 static int inet_diag_type2proto(int type
)
941 case TCPDIAG_GETSOCK
:
943 case DCCPDIAG_GETSOCK
:
950 static int inet_diag_dump_compat(struct sk_buff
*skb
,
951 struct netlink_callback
*cb
)
953 struct inet_diag_req
*rc
= nlmsg_data(cb
->nlh
);
954 int hdrlen
= sizeof(struct inet_diag_req
);
955 struct inet_diag_req_v2 req
;
956 struct nlattr
*bc
= NULL
;
958 req
.sdiag_family
= AF_UNSPEC
; /* compatibility */
959 req
.sdiag_protocol
= inet_diag_type2proto(cb
->nlh
->nlmsg_type
);
960 req
.idiag_ext
= rc
->idiag_ext
;
961 req
.idiag_states
= rc
->idiag_states
;
964 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
965 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
967 return __inet_diag_dump(skb
, cb
, &req
, bc
);
970 static int inet_diag_get_exact_compat(struct sk_buff
*in_skb
,
971 const struct nlmsghdr
*nlh
)
973 struct inet_diag_req
*rc
= nlmsg_data(nlh
);
974 struct inet_diag_req_v2 req
;
976 req
.sdiag_family
= rc
->idiag_family
;
977 req
.sdiag_protocol
= inet_diag_type2proto(nlh
->nlmsg_type
);
978 req
.idiag_ext
= rc
->idiag_ext
;
979 req
.idiag_states
= rc
->idiag_states
;
982 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY
, in_skb
, nlh
, &req
);
985 static int inet_diag_rcv_msg_compat(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
987 int hdrlen
= sizeof(struct inet_diag_req
);
988 struct net
*net
= sock_net(skb
->sk
);
990 if (nlh
->nlmsg_type
>= INET_DIAG_GETSOCK_MAX
||
991 nlmsg_len(nlh
) < hdrlen
)
994 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
995 if (nlmsg_attrlen(nlh
, hdrlen
)) {
998 attr
= nlmsg_find_attr(nlh
, hdrlen
,
999 INET_DIAG_REQ_BYTECODE
);
1001 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
1002 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
1006 struct netlink_dump_control c
= {
1007 .dump
= inet_diag_dump_compat
,
1009 return netlink_dump_start(net
->diag_nlsk
, skb
, nlh
, &c
);
1013 return inet_diag_get_exact_compat(skb
, nlh
);
1016 static int inet_diag_handler_cmd(struct sk_buff
*skb
, struct nlmsghdr
*h
)
1018 int hdrlen
= sizeof(struct inet_diag_req_v2
);
1019 struct net
*net
= sock_net(skb
->sk
);
1021 if (nlmsg_len(h
) < hdrlen
)
1024 if (h
->nlmsg_type
== SOCK_DIAG_BY_FAMILY
&&
1025 h
->nlmsg_flags
& NLM_F_DUMP
) {
1026 if (nlmsg_attrlen(h
, hdrlen
)) {
1027 struct nlattr
*attr
;
1029 attr
= nlmsg_find_attr(h
, hdrlen
,
1030 INET_DIAG_REQ_BYTECODE
);
1032 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
1033 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
1037 struct netlink_dump_control c
= {
1038 .dump
= inet_diag_dump
,
1040 return netlink_dump_start(net
->diag_nlsk
, skb
, h
, &c
);
1044 return inet_diag_cmd_exact(h
->nlmsg_type
, skb
, h
, nlmsg_data(h
));
1048 int inet_diag_handler_get_info(struct sk_buff
*skb
, struct sock
*sk
)
1050 const struct inet_diag_handler
*handler
;
1051 struct nlmsghdr
*nlh
;
1052 struct nlattr
*attr
;
1053 struct inet_diag_msg
*r
;
1057 nlh
= nlmsg_put(skb
, 0, 0, SOCK_DIAG_BY_FAMILY
, sizeof(*r
), 0);
1061 r
= nlmsg_data(nlh
);
1062 memset(r
, 0, sizeof(*r
));
1063 inet_diag_msg_common_fill(r
, sk
);
1064 if (sk
->sk_type
== SOCK_DGRAM
|| sk
->sk_type
== SOCK_STREAM
)
1065 r
->id
.idiag_sport
= inet_sk(sk
)->inet_sport
;
1066 r
->idiag_state
= sk
->sk_state
;
1068 if ((err
= nla_put_u8(skb
, INET_DIAG_PROTOCOL
, sk
->sk_protocol
))) {
1069 nlmsg_cancel(skb
, nlh
);
1073 handler
= inet_diag_lock_handler(sk
->sk_protocol
);
1074 if (IS_ERR(handler
)) {
1075 inet_diag_unlock_handler(handler
);
1076 nlmsg_cancel(skb
, nlh
);
1077 return PTR_ERR(handler
);
1080 attr
= handler
->idiag_info_size
1081 ? nla_reserve(skb
, INET_DIAG_INFO
, handler
->idiag_info_size
)
1084 info
= nla_data(attr
);
1086 handler
->idiag_get_info(sk
, r
, info
);
1087 inet_diag_unlock_handler(handler
);
1089 nlmsg_end(skb
, nlh
);
1093 static const struct sock_diag_handler inet_diag_handler
= {
1095 .dump
= inet_diag_handler_cmd
,
1096 .get_info
= inet_diag_handler_get_info
,
1097 .destroy
= inet_diag_handler_cmd
,
1100 static const struct sock_diag_handler inet6_diag_handler
= {
1102 .dump
= inet_diag_handler_cmd
,
1103 .get_info
= inet_diag_handler_get_info
,
1104 .destroy
= inet_diag_handler_cmd
,
1107 int inet_diag_register(const struct inet_diag_handler
*h
)
1109 const __u16 type
= h
->idiag_type
;
1112 if (type
>= IPPROTO_MAX
)
1115 mutex_lock(&inet_diag_table_mutex
);
1117 if (!inet_diag_table
[type
]) {
1118 inet_diag_table
[type
] = h
;
1121 mutex_unlock(&inet_diag_table_mutex
);
1125 EXPORT_SYMBOL_GPL(inet_diag_register
);
1127 void inet_diag_unregister(const struct inet_diag_handler
*h
)
1129 const __u16 type
= h
->idiag_type
;
1131 if (type
>= IPPROTO_MAX
)
1134 mutex_lock(&inet_diag_table_mutex
);
1135 inet_diag_table
[type
] = NULL
;
1136 mutex_unlock(&inet_diag_table_mutex
);
1138 EXPORT_SYMBOL_GPL(inet_diag_unregister
);
1140 static int __init
inet_diag_init(void)
1142 const int inet_diag_table_size
= (IPPROTO_MAX
*
1143 sizeof(struct inet_diag_handler
*));
1146 inet_diag_table
= kzalloc(inet_diag_table_size
, GFP_KERNEL
);
1147 if (!inet_diag_table
)
1150 err
= sock_diag_register(&inet_diag_handler
);
1154 err
= sock_diag_register(&inet6_diag_handler
);
1158 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat
);
1163 sock_diag_unregister(&inet_diag_handler
);
1165 kfree(inet_diag_table
);
1169 static void __exit
inet_diag_exit(void)
1171 sock_diag_unregister(&inet6_diag_handler
);
1172 sock_diag_unregister(&inet_diag_handler
);
1173 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat
);
1174 kfree(inet_diag_table
);
1177 module_init(inet_diag_init
);
1178 module_exit(inet_diag_exit
);
1179 MODULE_LICENSE("GPL");
1180 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2 /* AF_INET */);
1181 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 10 /* AF_INET6 */);