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 if (icsk
->icsk_pending
== ICSK_TIME_RETRANS
||
201 icsk
->icsk_pending
== ICSK_TIME_EARLY_RETRANS
||
202 icsk
->icsk_pending
== ICSK_TIME_LOSS_PROBE
) {
204 r
->idiag_retrans
= icsk
->icsk_retransmits
;
206 jiffies_to_msecs(icsk
->icsk_timeout
- jiffies
);
207 } else if (icsk
->icsk_pending
== ICSK_TIME_PROBE0
) {
209 r
->idiag_retrans
= icsk
->icsk_probes_out
;
211 jiffies_to_msecs(icsk
->icsk_timeout
- jiffies
);
212 } else if (timer_pending(&sk
->sk_timer
)) {
214 r
->idiag_retrans
= icsk
->icsk_probes_out
;
216 jiffies_to_msecs(sk
->sk_timer
.expires
- jiffies
);
219 r
->idiag_expires
= 0;
222 if ((ext
& (1 << (INET_DIAG_INFO
- 1))) && handler
->idiag_info_size
) {
223 attr
= nla_reserve_64bit(skb
, INET_DIAG_INFO
,
224 handler
->idiag_info_size
,
229 info
= nla_data(attr
);
232 if (ext
& (1 << (INET_DIAG_CONG
- 1))) {
236 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
238 err
= nla_put_string(skb
, INET_DIAG_CONG
, ca_ops
->name
);
244 handler
->idiag_get_info(sk
, r
, info
);
246 if (sk
->sk_state
< TCP_TIME_WAIT
) {
247 union tcp_cc_info info
;
252 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
253 if (ca_ops
&& ca_ops
->get_info
)
254 sz
= ca_ops
->get_info(sk
, ext
, &attr
, &info
);
256 if (sz
&& nla_put(skb
, attr
, sz
, &info
) < 0)
265 nlmsg_cancel(skb
, nlh
);
268 EXPORT_SYMBOL_GPL(inet_sk_diag_fill
);
270 static int inet_csk_diag_fill(struct sock
*sk
,
272 const struct inet_diag_req_v2
*req
,
273 struct user_namespace
*user_ns
,
274 u32 portid
, u32 seq
, u16 nlmsg_flags
,
275 const struct nlmsghdr
*unlh
)
277 return inet_sk_diag_fill(sk
, inet_csk(sk
), skb
, req
,
278 user_ns
, portid
, seq
, nlmsg_flags
, unlh
);
281 static int inet_twsk_diag_fill(struct sock
*sk
,
283 u32 portid
, u32 seq
, u16 nlmsg_flags
,
284 const struct nlmsghdr
*unlh
)
286 struct inet_timewait_sock
*tw
= inet_twsk(sk
);
287 struct inet_diag_msg
*r
;
288 struct nlmsghdr
*nlh
;
291 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
297 BUG_ON(tw
->tw_state
!= TCP_TIME_WAIT
);
299 tmo
= tw
->tw_timer
.expires
- jiffies
;
303 inet_diag_msg_common_fill(r
, sk
);
304 r
->idiag_retrans
= 0;
306 r
->idiag_state
= tw
->tw_substate
;
308 r
->idiag_expires
= jiffies_to_msecs(tmo
);
318 static int inet_req_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
319 u32 portid
, u32 seq
, u16 nlmsg_flags
,
320 const struct nlmsghdr
*unlh
)
322 struct inet_diag_msg
*r
;
323 struct nlmsghdr
*nlh
;
326 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
332 inet_diag_msg_common_fill(r
, sk
);
333 r
->idiag_state
= TCP_SYN_RECV
;
335 r
->idiag_retrans
= inet_reqsk(sk
)->num_retrans
;
337 BUILD_BUG_ON(offsetof(struct inet_request_sock
, ir_cookie
) !=
338 offsetof(struct sock
, sk_cookie
));
340 tmo
= inet_reqsk(sk
)->rsk_timer
.expires
- jiffies
;
341 r
->idiag_expires
= (tmo
>= 0) ? jiffies_to_msecs(tmo
) : 0;
351 static int sk_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
352 const struct inet_diag_req_v2
*r
,
353 struct user_namespace
*user_ns
,
354 u32 portid
, u32 seq
, u16 nlmsg_flags
,
355 const struct nlmsghdr
*unlh
)
357 if (sk
->sk_state
== TCP_TIME_WAIT
)
358 return inet_twsk_diag_fill(sk
, skb
, portid
, seq
,
361 if (sk
->sk_state
== TCP_NEW_SYN_RECV
)
362 return inet_req_diag_fill(sk
, skb
, portid
, seq
,
365 return inet_csk_diag_fill(sk
, skb
, r
, user_ns
, portid
, seq
,
369 struct sock
*inet_diag_find_one_icsk(struct net
*net
,
370 struct inet_hashinfo
*hashinfo
,
371 const struct inet_diag_req_v2
*req
)
376 if (req
->sdiag_family
== AF_INET
)
377 sk
= inet_lookup(net
, hashinfo
, NULL
, 0, req
->id
.idiag_dst
[0],
378 req
->id
.idiag_dport
, req
->id
.idiag_src
[0],
379 req
->id
.idiag_sport
, req
->id
.idiag_if
);
380 #if IS_ENABLED(CONFIG_IPV6)
381 else if (req
->sdiag_family
== AF_INET6
) {
382 if (ipv6_addr_v4mapped((struct in6_addr
*)req
->id
.idiag_dst
) &&
383 ipv6_addr_v4mapped((struct in6_addr
*)req
->id
.idiag_src
))
384 sk
= inet_lookup(net
, hashinfo
, NULL
, 0, req
->id
.idiag_dst
[3],
385 req
->id
.idiag_dport
, req
->id
.idiag_src
[3],
386 req
->id
.idiag_sport
, req
->id
.idiag_if
);
388 sk
= inet6_lookup(net
, hashinfo
, NULL
, 0,
389 (struct in6_addr
*)req
->id
.idiag_dst
,
391 (struct in6_addr
*)req
->id
.idiag_src
,
398 return ERR_PTR(-EINVAL
);
402 return ERR_PTR(-ENOENT
);
404 if (sock_diag_check_cookie(sk
, req
->id
.idiag_cookie
)) {
406 return ERR_PTR(-ENOENT
);
411 EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk
);
413 int inet_diag_dump_one_icsk(struct inet_hashinfo
*hashinfo
,
414 struct sk_buff
*in_skb
,
415 const struct nlmsghdr
*nlh
,
416 const struct inet_diag_req_v2
*req
)
418 struct net
*net
= sock_net(in_skb
->sk
);
423 sk
= inet_diag_find_one_icsk(net
, hashinfo
, req
);
427 rep
= nlmsg_new(inet_sk_attr_size(), GFP_KERNEL
);
433 err
= sk_diag_fill(sk
, rep
, req
,
434 sk_user_ns(NETLINK_CB(in_skb
).sk
),
435 NETLINK_CB(in_skb
).portid
,
436 nlh
->nlmsg_seq
, 0, nlh
);
438 WARN_ON(err
== -EMSGSIZE
);
442 err
= netlink_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
,
453 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk
);
455 static int inet_diag_cmd_exact(int cmd
, struct sk_buff
*in_skb
,
456 const struct nlmsghdr
*nlh
,
457 const struct inet_diag_req_v2
*req
)
459 const struct inet_diag_handler
*handler
;
462 handler
= inet_diag_lock_handler(req
->sdiag_protocol
);
464 err
= PTR_ERR(handler
);
465 else if (cmd
== SOCK_DIAG_BY_FAMILY
)
466 err
= handler
->dump_one(in_skb
, nlh
, req
);
467 else if (cmd
== SOCK_DESTROY
&& handler
->destroy
)
468 err
= handler
->destroy(in_skb
, req
);
471 inet_diag_unlock_handler(handler
);
476 static int bitstring_match(const __be32
*a1
, const __be32
*a2
, int bits
)
478 int words
= bits
>> 5;
483 if (memcmp(a1
, a2
, words
<< 2))
493 mask
= htonl((0xffffffff) << (32 - bits
));
495 if ((w1
^ w2
) & mask
)
502 static int inet_diag_bc_run(const struct nlattr
*_bc
,
503 const struct inet_diag_entry
*entry
)
505 const void *bc
= nla_data(_bc
);
506 int len
= nla_len(_bc
);
510 const struct inet_diag_bc_op
*op
= bc
;
513 case INET_DIAG_BC_NOP
:
515 case INET_DIAG_BC_JMP
:
518 case INET_DIAG_BC_S_GE
:
519 yes
= entry
->sport
>= op
[1].no
;
521 case INET_DIAG_BC_S_LE
:
522 yes
= entry
->sport
<= op
[1].no
;
524 case INET_DIAG_BC_D_GE
:
525 yes
= entry
->dport
>= op
[1].no
;
527 case INET_DIAG_BC_D_LE
:
528 yes
= entry
->dport
<= op
[1].no
;
530 case INET_DIAG_BC_AUTO
:
531 yes
= !(entry
->userlocks
& SOCK_BINDPORT_LOCK
);
533 case INET_DIAG_BC_S_COND
:
534 case INET_DIAG_BC_D_COND
: {
535 const struct inet_diag_hostcond
*cond
;
538 cond
= (const struct inet_diag_hostcond
*)(op
+ 1);
539 if (cond
->port
!= -1 &&
540 cond
->port
!= (op
->code
== INET_DIAG_BC_S_COND
?
541 entry
->sport
: entry
->dport
)) {
546 if (op
->code
== INET_DIAG_BC_S_COND
)
551 if (cond
->family
!= AF_UNSPEC
&&
552 cond
->family
!= entry
->family
) {
553 if (entry
->family
== AF_INET6
&&
554 cond
->family
== AF_INET
) {
555 if (addr
[0] == 0 && addr
[1] == 0 &&
556 addr
[2] == htonl(0xffff) &&
557 bitstring_match(addr
+ 3,
566 if (cond
->prefix_len
== 0)
568 if (bitstring_match(addr
, cond
->addr
,
587 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
589 static void entry_fill_addrs(struct inet_diag_entry
*entry
,
590 const struct sock
*sk
)
592 #if IS_ENABLED(CONFIG_IPV6)
593 if (sk
->sk_family
== AF_INET6
) {
594 entry
->saddr
= sk
->sk_v6_rcv_saddr
.s6_addr32
;
595 entry
->daddr
= sk
->sk_v6_daddr
.s6_addr32
;
599 entry
->saddr
= &sk
->sk_rcv_saddr
;
600 entry
->daddr
= &sk
->sk_daddr
;
604 int inet_diag_bc_sk(const struct nlattr
*bc
, struct sock
*sk
)
606 struct inet_sock
*inet
= inet_sk(sk
);
607 struct inet_diag_entry entry
;
612 entry
.family
= sk
->sk_family
;
613 entry_fill_addrs(&entry
, sk
);
614 entry
.sport
= inet
->inet_num
;
615 entry
.dport
= ntohs(inet
->inet_dport
);
616 entry
.userlocks
= sk_fullsock(sk
) ? sk
->sk_userlocks
: 0;
618 return inet_diag_bc_run(bc
, &entry
);
620 EXPORT_SYMBOL_GPL(inet_diag_bc_sk
);
622 static int valid_cc(const void *bc
, int len
, int cc
)
625 const struct inet_diag_bc_op
*op
= bc
;
631 if (op
->yes
< 4 || op
->yes
& 3)
639 /* Validate an inet_diag_hostcond. */
640 static bool valid_hostcond(const struct inet_diag_bc_op
*op
, int len
,
643 struct inet_diag_hostcond
*cond
;
646 /* Check hostcond space. */
647 *min_len
+= sizeof(struct inet_diag_hostcond
);
650 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
652 /* Check address family and address length. */
653 switch (cond
->family
) {
658 addr_len
= sizeof(struct in_addr
);
661 addr_len
= sizeof(struct in6_addr
);
666 *min_len
+= addr_len
;
670 /* Check prefix length (in bits) vs address length (in bytes). */
671 if (cond
->prefix_len
> 8 * addr_len
)
677 /* Validate a port comparison operator. */
678 static bool valid_port_comparison(const struct inet_diag_bc_op
*op
,
679 int len
, int *min_len
)
681 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
682 *min_len
+= sizeof(struct inet_diag_bc_op
);
688 static int inet_diag_bc_audit(const void *bytecode
, int bytecode_len
)
690 const void *bc
= bytecode
;
691 int len
= bytecode_len
;
694 int min_len
= sizeof(struct inet_diag_bc_op
);
695 const struct inet_diag_bc_op
*op
= bc
;
698 case INET_DIAG_BC_S_COND
:
699 case INET_DIAG_BC_D_COND
:
700 if (!valid_hostcond(bc
, len
, &min_len
))
703 case INET_DIAG_BC_S_GE
:
704 case INET_DIAG_BC_S_LE
:
705 case INET_DIAG_BC_D_GE
:
706 case INET_DIAG_BC_D_LE
:
707 if (!valid_port_comparison(bc
, len
, &min_len
))
710 case INET_DIAG_BC_AUTO
:
711 case INET_DIAG_BC_JMP
:
712 case INET_DIAG_BC_NOP
:
718 if (op
->code
!= INET_DIAG_BC_NOP
) {
719 if (op
->no
< min_len
|| op
->no
> len
+ 4 || op
->no
& 3)
722 !valid_cc(bytecode
, bytecode_len
, len
- op
->no
))
726 if (op
->yes
< min_len
|| op
->yes
> len
+ 4 || op
->yes
& 3)
731 return len
== 0 ? 0 : -EINVAL
;
734 static int inet_csk_diag_dump(struct sock
*sk
,
736 struct netlink_callback
*cb
,
737 const struct inet_diag_req_v2
*r
,
738 const struct nlattr
*bc
)
740 if (!inet_diag_bc_sk(bc
, sk
))
743 return inet_csk_diag_fill(sk
, skb
, r
,
744 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
745 NETLINK_CB(cb
->skb
).portid
,
746 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
749 static void twsk_build_assert(void)
751 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_family
) !=
752 offsetof(struct sock
, sk_family
));
754 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_num
) !=
755 offsetof(struct inet_sock
, inet_num
));
757 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_dport
) !=
758 offsetof(struct inet_sock
, inet_dport
));
760 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_rcv_saddr
) !=
761 offsetof(struct inet_sock
, inet_rcv_saddr
));
763 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_daddr
) !=
764 offsetof(struct inet_sock
, inet_daddr
));
766 #if IS_ENABLED(CONFIG_IPV6)
767 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_rcv_saddr
) !=
768 offsetof(struct sock
, sk_v6_rcv_saddr
));
770 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_daddr
) !=
771 offsetof(struct sock
, sk_v6_daddr
));
775 void inet_diag_dump_icsk(struct inet_hashinfo
*hashinfo
, struct sk_buff
*skb
,
776 struct netlink_callback
*cb
,
777 const struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
779 struct net
*net
= sock_net(skb
->sk
);
780 int i
, num
, s_i
, s_num
;
781 u32 idiag_states
= r
->idiag_states
;
783 if (idiag_states
& TCPF_SYN_RECV
)
784 idiag_states
|= TCPF_NEW_SYN_RECV
;
786 s_num
= num
= cb
->args
[2];
788 if (cb
->args
[0] == 0) {
789 if (!(idiag_states
& TCPF_LISTEN
))
792 for (i
= s_i
; i
< INET_LHTABLE_SIZE
; i
++) {
793 struct inet_listen_hashbucket
*ilb
;
797 ilb
= &hashinfo
->listening_hash
[i
];
798 spin_lock_bh(&ilb
->lock
);
799 sk_for_each(sk
, &ilb
->head
) {
800 struct inet_sock
*inet
= inet_sk(sk
);
802 if (!net_eq(sock_net(sk
), net
))
810 if (r
->sdiag_family
!= AF_UNSPEC
&&
811 sk
->sk_family
!= r
->sdiag_family
)
814 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
818 if (r
->id
.idiag_dport
||
822 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
823 spin_unlock_bh(&ilb
->lock
);
832 spin_unlock_bh(&ilb
->lock
);
840 s_i
= num
= s_num
= 0;
843 if (!(idiag_states
& ~TCPF_LISTEN
))
846 for (i
= s_i
; i
<= hashinfo
->ehash_mask
; i
++) {
847 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[i
];
848 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, i
);
849 struct hlist_nulls_node
*node
;
854 if (hlist_nulls_empty(&head
->chain
))
861 sk_nulls_for_each(sk
, node
, &head
->chain
) {
864 if (!net_eq(sock_net(sk
), net
))
868 state
= (sk
->sk_state
== TCP_TIME_WAIT
) ?
869 inet_twsk(sk
)->tw_substate
: sk
->sk_state
;
870 if (!(idiag_states
& (1 << state
)))
872 if (r
->sdiag_family
!= AF_UNSPEC
&&
873 sk
->sk_family
!= r
->sdiag_family
)
875 if (r
->id
.idiag_sport
!= htons(sk
->sk_num
) &&
878 if (r
->id
.idiag_dport
!= sk
->sk_dport
&&
883 if (!inet_diag_bc_sk(bc
, sk
))
886 res
= sk_diag_fill(sk
, skb
, r
,
887 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
888 NETLINK_CB(cb
->skb
).portid
,
889 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
892 spin_unlock_bh(lock
);
899 spin_unlock_bh(lock
);
909 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk
);
911 static int __inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
912 const struct inet_diag_req_v2
*r
,
915 const struct inet_diag_handler
*handler
;
918 handler
= inet_diag_lock_handler(r
->sdiag_protocol
);
919 if (!IS_ERR(handler
))
920 handler
->dump(skb
, cb
, r
, bc
);
922 err
= PTR_ERR(handler
);
923 inet_diag_unlock_handler(handler
);
925 return err
? : skb
->len
;
928 static int inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
930 int hdrlen
= sizeof(struct inet_diag_req_v2
);
931 struct nlattr
*bc
= NULL
;
933 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
934 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
936 return __inet_diag_dump(skb
, cb
, nlmsg_data(cb
->nlh
), bc
);
939 static int inet_diag_type2proto(int type
)
942 case TCPDIAG_GETSOCK
:
944 case DCCPDIAG_GETSOCK
:
951 static int inet_diag_dump_compat(struct sk_buff
*skb
,
952 struct netlink_callback
*cb
)
954 struct inet_diag_req
*rc
= nlmsg_data(cb
->nlh
);
955 int hdrlen
= sizeof(struct inet_diag_req
);
956 struct inet_diag_req_v2 req
;
957 struct nlattr
*bc
= NULL
;
959 req
.sdiag_family
= AF_UNSPEC
; /* compatibility */
960 req
.sdiag_protocol
= inet_diag_type2proto(cb
->nlh
->nlmsg_type
);
961 req
.idiag_ext
= rc
->idiag_ext
;
962 req
.idiag_states
= rc
->idiag_states
;
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
, &req
, bc
);
971 static int inet_diag_get_exact_compat(struct sk_buff
*in_skb
,
972 const struct nlmsghdr
*nlh
)
974 struct inet_diag_req
*rc
= nlmsg_data(nlh
);
975 struct inet_diag_req_v2 req
;
977 req
.sdiag_family
= rc
->idiag_family
;
978 req
.sdiag_protocol
= inet_diag_type2proto(nlh
->nlmsg_type
);
979 req
.idiag_ext
= rc
->idiag_ext
;
980 req
.idiag_states
= rc
->idiag_states
;
983 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY
, in_skb
, nlh
, &req
);
986 static int inet_diag_rcv_msg_compat(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
988 int hdrlen
= sizeof(struct inet_diag_req
);
989 struct net
*net
= sock_net(skb
->sk
);
991 if (nlh
->nlmsg_type
>= INET_DIAG_GETSOCK_MAX
||
992 nlmsg_len(nlh
) < hdrlen
)
995 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
996 if (nlmsg_attrlen(nlh
, hdrlen
)) {
999 attr
= nlmsg_find_attr(nlh
, hdrlen
,
1000 INET_DIAG_REQ_BYTECODE
);
1002 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
1003 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
1007 struct netlink_dump_control c
= {
1008 .dump
= inet_diag_dump_compat
,
1010 return netlink_dump_start(net
->diag_nlsk
, skb
, nlh
, &c
);
1014 return inet_diag_get_exact_compat(skb
, nlh
);
1017 static int inet_diag_handler_cmd(struct sk_buff
*skb
, struct nlmsghdr
*h
)
1019 int hdrlen
= sizeof(struct inet_diag_req_v2
);
1020 struct net
*net
= sock_net(skb
->sk
);
1022 if (nlmsg_len(h
) < hdrlen
)
1025 if (h
->nlmsg_type
== SOCK_DIAG_BY_FAMILY
&&
1026 h
->nlmsg_flags
& NLM_F_DUMP
) {
1027 if (nlmsg_attrlen(h
, hdrlen
)) {
1028 struct nlattr
*attr
;
1030 attr
= nlmsg_find_attr(h
, hdrlen
,
1031 INET_DIAG_REQ_BYTECODE
);
1033 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
1034 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
1038 struct netlink_dump_control c
= {
1039 .dump
= inet_diag_dump
,
1041 return netlink_dump_start(net
->diag_nlsk
, skb
, h
, &c
);
1045 return inet_diag_cmd_exact(h
->nlmsg_type
, skb
, h
, nlmsg_data(h
));
1049 int inet_diag_handler_get_info(struct sk_buff
*skb
, struct sock
*sk
)
1051 const struct inet_diag_handler
*handler
;
1052 struct nlmsghdr
*nlh
;
1053 struct nlattr
*attr
;
1054 struct inet_diag_msg
*r
;
1058 nlh
= nlmsg_put(skb
, 0, 0, SOCK_DIAG_BY_FAMILY
, sizeof(*r
), 0);
1062 r
= nlmsg_data(nlh
);
1063 memset(r
, 0, sizeof(*r
));
1064 inet_diag_msg_common_fill(r
, sk
);
1065 if (sk
->sk_type
== SOCK_DGRAM
|| sk
->sk_type
== SOCK_STREAM
)
1066 r
->id
.idiag_sport
= inet_sk(sk
)->inet_sport
;
1067 r
->idiag_state
= sk
->sk_state
;
1069 if ((err
= nla_put_u8(skb
, INET_DIAG_PROTOCOL
, sk
->sk_protocol
))) {
1070 nlmsg_cancel(skb
, nlh
);
1074 handler
= inet_diag_lock_handler(sk
->sk_protocol
);
1075 if (IS_ERR(handler
)) {
1076 inet_diag_unlock_handler(handler
);
1077 nlmsg_cancel(skb
, nlh
);
1078 return PTR_ERR(handler
);
1081 attr
= handler
->idiag_info_size
1082 ? nla_reserve_64bit(skb
, INET_DIAG_INFO
,
1083 handler
->idiag_info_size
,
1087 info
= nla_data(attr
);
1089 handler
->idiag_get_info(sk
, r
, info
);
1090 inet_diag_unlock_handler(handler
);
1092 nlmsg_end(skb
, nlh
);
1096 static const struct sock_diag_handler inet_diag_handler
= {
1098 .dump
= inet_diag_handler_cmd
,
1099 .get_info
= inet_diag_handler_get_info
,
1100 .destroy
= inet_diag_handler_cmd
,
1103 static const struct sock_diag_handler inet6_diag_handler
= {
1105 .dump
= inet_diag_handler_cmd
,
1106 .get_info
= inet_diag_handler_get_info
,
1107 .destroy
= inet_diag_handler_cmd
,
1110 int inet_diag_register(const struct inet_diag_handler
*h
)
1112 const __u16 type
= h
->idiag_type
;
1115 if (type
>= IPPROTO_MAX
)
1118 mutex_lock(&inet_diag_table_mutex
);
1120 if (!inet_diag_table
[type
]) {
1121 inet_diag_table
[type
] = h
;
1124 mutex_unlock(&inet_diag_table_mutex
);
1128 EXPORT_SYMBOL_GPL(inet_diag_register
);
1130 void inet_diag_unregister(const struct inet_diag_handler
*h
)
1132 const __u16 type
= h
->idiag_type
;
1134 if (type
>= IPPROTO_MAX
)
1137 mutex_lock(&inet_diag_table_mutex
);
1138 inet_diag_table
[type
] = NULL
;
1139 mutex_unlock(&inet_diag_table_mutex
);
1141 EXPORT_SYMBOL_GPL(inet_diag_unregister
);
1143 static int __init
inet_diag_init(void)
1145 const int inet_diag_table_size
= (IPPROTO_MAX
*
1146 sizeof(struct inet_diag_handler
*));
1149 inet_diag_table
= kzalloc(inet_diag_table_size
, GFP_KERNEL
);
1150 if (!inet_diag_table
)
1153 err
= sock_diag_register(&inet_diag_handler
);
1157 err
= sock_diag_register(&inet6_diag_handler
);
1161 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat
);
1166 sock_diag_unregister(&inet_diag_handler
);
1168 kfree(inet_diag_table
);
1172 static void __exit
inet_diag_exit(void)
1174 sock_diag_unregister(&inet6_diag_handler
);
1175 sock_diag_unregister(&inet_diag_handler
);
1176 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat
);
1177 kfree(inet_diag_table
);
1180 module_init(inet_diag_init
);
1181 module_exit(inet_diag_exit
);
1182 MODULE_LICENSE("GPL");
1183 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2 /* AF_INET */);
1184 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 10 /* AF_INET6 */);