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
{
51 static DEFINE_MUTEX(inet_diag_table_mutex
);
53 static const struct inet_diag_handler
*inet_diag_lock_handler(int proto
)
55 if (!inet_diag_table
[proto
])
56 request_module("net-pf-%d-proto-%d-type-%d-%d", PF_NETLINK
,
57 NETLINK_SOCK_DIAG
, AF_INET
, proto
);
59 mutex_lock(&inet_diag_table_mutex
);
60 if (!inet_diag_table
[proto
])
61 return ERR_PTR(-ENOENT
);
63 return inet_diag_table
[proto
];
66 static void inet_diag_unlock_handler(const struct inet_diag_handler
*handler
)
68 mutex_unlock(&inet_diag_table_mutex
);
71 void inet_diag_msg_common_fill(struct inet_diag_msg
*r
, struct sock
*sk
)
73 r
->idiag_family
= sk
->sk_family
;
75 r
->id
.idiag_sport
= htons(sk
->sk_num
);
76 r
->id
.idiag_dport
= sk
->sk_dport
;
77 r
->id
.idiag_if
= sk
->sk_bound_dev_if
;
78 sock_diag_save_cookie(sk
, r
->id
.idiag_cookie
);
80 #if IS_ENABLED(CONFIG_IPV6)
81 if (sk
->sk_family
== AF_INET6
) {
82 *(struct in6_addr
*)r
->id
.idiag_src
= sk
->sk_v6_rcv_saddr
;
83 *(struct in6_addr
*)r
->id
.idiag_dst
= sk
->sk_v6_daddr
;
87 memset(&r
->id
.idiag_src
, 0, sizeof(r
->id
.idiag_src
));
88 memset(&r
->id
.idiag_dst
, 0, sizeof(r
->id
.idiag_dst
));
90 r
->id
.idiag_src
[0] = sk
->sk_rcv_saddr
;
91 r
->id
.idiag_dst
[0] = sk
->sk_daddr
;
94 EXPORT_SYMBOL_GPL(inet_diag_msg_common_fill
);
96 static size_t inet_sk_attr_size(void)
98 return nla_total_size(sizeof(struct tcp_info
))
99 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
100 + nla_total_size(1) /* INET_DIAG_TOS */
101 + nla_total_size(1) /* INET_DIAG_TCLASS */
102 + nla_total_size(4) /* INET_DIAG_MARK */
103 + nla_total_size(sizeof(struct inet_diag_meminfo
))
104 + nla_total_size(sizeof(struct inet_diag_msg
))
105 + nla_total_size(SK_MEMINFO_VARS
* sizeof(u32
))
106 + nla_total_size(TCP_CA_NAME_MAX
)
107 + nla_total_size(sizeof(struct tcpvegas_info
))
111 int inet_diag_msg_attrs_fill(struct sock
*sk
, struct sk_buff
*skb
,
112 struct inet_diag_msg
*r
, int ext
,
113 struct user_namespace
*user_ns
,
116 const struct inet_sock
*inet
= inet_sk(sk
);
118 if (nla_put_u8(skb
, INET_DIAG_SHUTDOWN
, sk
->sk_shutdown
))
121 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
122 * hence this needs to be included regardless of socket family.
124 if (ext
& (1 << (INET_DIAG_TOS
- 1)))
125 if (nla_put_u8(skb
, INET_DIAG_TOS
, inet
->tos
) < 0)
128 #if IS_ENABLED(CONFIG_IPV6)
129 if (r
->idiag_family
== AF_INET6
) {
130 if (ext
& (1 << (INET_DIAG_TCLASS
- 1)))
131 if (nla_put_u8(skb
, INET_DIAG_TCLASS
,
132 inet6_sk(sk
)->tclass
) < 0)
135 if (((1 << sk
->sk_state
) & (TCPF_LISTEN
| TCPF_CLOSE
)) &&
136 nla_put_u8(skb
, INET_DIAG_SKV6ONLY
, ipv6_only_sock(sk
)))
141 if (net_admin
&& nla_put_u32(skb
, INET_DIAG_MARK
, sk
->sk_mark
))
144 r
->idiag_uid
= from_kuid_munged(user_ns
, sock_i_uid(sk
));
145 r
->idiag_inode
= sock_i_ino(sk
);
151 EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill
);
153 int inet_sk_diag_fill(struct sock
*sk
, struct inet_connection_sock
*icsk
,
154 struct sk_buff
*skb
, const struct inet_diag_req_v2
*req
,
155 struct user_namespace
*user_ns
,
156 u32 portid
, u32 seq
, u16 nlmsg_flags
,
157 const struct nlmsghdr
*unlh
,
160 const struct tcp_congestion_ops
*ca_ops
;
161 const struct inet_diag_handler
*handler
;
162 int ext
= req
->idiag_ext
;
163 struct inet_diag_msg
*r
;
164 struct nlmsghdr
*nlh
;
168 handler
= inet_diag_table
[req
->sdiag_protocol
];
171 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
177 BUG_ON(!sk_fullsock(sk
));
179 inet_diag_msg_common_fill(r
, sk
);
180 r
->idiag_state
= sk
->sk_state
;
182 r
->idiag_retrans
= 0;
184 if (inet_diag_msg_attrs_fill(sk
, skb
, r
, ext
, user_ns
, net_admin
))
187 if (ext
& (1 << (INET_DIAG_MEMINFO
- 1))) {
188 struct inet_diag_meminfo minfo
= {
189 .idiag_rmem
= sk_rmem_alloc_get(sk
),
190 .idiag_wmem
= sk
->sk_wmem_queued
,
191 .idiag_fmem
= sk
->sk_forward_alloc
,
192 .idiag_tmem
= sk_wmem_alloc_get(sk
),
195 if (nla_put(skb
, INET_DIAG_MEMINFO
, sizeof(minfo
), &minfo
) < 0)
199 if (ext
& (1 << (INET_DIAG_SKMEMINFO
- 1)))
200 if (sock_diag_put_meminfo(sk
, skb
, INET_DIAG_SKMEMINFO
))
204 handler
->idiag_get_info(sk
, r
, NULL
);
208 if (icsk
->icsk_pending
== ICSK_TIME_RETRANS
||
209 icsk
->icsk_pending
== ICSK_TIME_EARLY_RETRANS
||
210 icsk
->icsk_pending
== ICSK_TIME_LOSS_PROBE
) {
212 r
->idiag_retrans
= icsk
->icsk_retransmits
;
214 jiffies_to_msecs(icsk
->icsk_timeout
- jiffies
);
215 } else if (icsk
->icsk_pending
== ICSK_TIME_PROBE0
) {
217 r
->idiag_retrans
= icsk
->icsk_probes_out
;
219 jiffies_to_msecs(icsk
->icsk_timeout
- jiffies
);
220 } else if (timer_pending(&sk
->sk_timer
)) {
222 r
->idiag_retrans
= icsk
->icsk_probes_out
;
224 jiffies_to_msecs(sk
->sk_timer
.expires
- jiffies
);
227 r
->idiag_expires
= 0;
230 if ((ext
& (1 << (INET_DIAG_INFO
- 1))) && handler
->idiag_info_size
) {
231 attr
= nla_reserve_64bit(skb
, INET_DIAG_INFO
,
232 handler
->idiag_info_size
,
237 info
= nla_data(attr
);
240 if (ext
& (1 << (INET_DIAG_CONG
- 1))) {
244 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
246 err
= nla_put_string(skb
, INET_DIAG_CONG
, ca_ops
->name
);
252 handler
->idiag_get_info(sk
, r
, info
);
254 if (sk
->sk_state
< TCP_TIME_WAIT
) {
255 union tcp_cc_info info
;
260 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
261 if (ca_ops
&& ca_ops
->get_info
)
262 sz
= ca_ops
->get_info(sk
, ext
, &attr
, &info
);
264 if (sz
&& nla_put(skb
, attr
, sz
, &info
) < 0)
273 nlmsg_cancel(skb
, nlh
);
276 EXPORT_SYMBOL_GPL(inet_sk_diag_fill
);
278 static int inet_csk_diag_fill(struct sock
*sk
,
280 const struct inet_diag_req_v2
*req
,
281 struct user_namespace
*user_ns
,
282 u32 portid
, u32 seq
, u16 nlmsg_flags
,
283 const struct nlmsghdr
*unlh
,
286 return inet_sk_diag_fill(sk
, inet_csk(sk
), skb
, req
, user_ns
,
287 portid
, seq
, nlmsg_flags
, unlh
, net_admin
);
290 static int inet_twsk_diag_fill(struct sock
*sk
,
292 u32 portid
, u32 seq
, u16 nlmsg_flags
,
293 const struct nlmsghdr
*unlh
)
295 struct inet_timewait_sock
*tw
= inet_twsk(sk
);
296 struct inet_diag_msg
*r
;
297 struct nlmsghdr
*nlh
;
300 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
306 BUG_ON(tw
->tw_state
!= TCP_TIME_WAIT
);
308 tmo
= tw
->tw_timer
.expires
- jiffies
;
312 inet_diag_msg_common_fill(r
, sk
);
313 r
->idiag_retrans
= 0;
315 r
->idiag_state
= tw
->tw_substate
;
317 r
->idiag_expires
= jiffies_to_msecs(tmo
);
327 static int inet_req_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
328 u32 portid
, u32 seq
, u16 nlmsg_flags
,
329 const struct nlmsghdr
*unlh
, bool net_admin
)
331 struct request_sock
*reqsk
= inet_reqsk(sk
);
332 struct inet_diag_msg
*r
;
333 struct nlmsghdr
*nlh
;
336 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
342 inet_diag_msg_common_fill(r
, sk
);
343 r
->idiag_state
= TCP_SYN_RECV
;
345 r
->idiag_retrans
= reqsk
->num_retrans
;
347 BUILD_BUG_ON(offsetof(struct inet_request_sock
, ir_cookie
) !=
348 offsetof(struct sock
, sk_cookie
));
350 tmo
= inet_reqsk(sk
)->rsk_timer
.expires
- jiffies
;
351 r
->idiag_expires
= (tmo
>= 0) ? jiffies_to_msecs(tmo
) : 0;
357 if (net_admin
&& nla_put_u32(skb
, INET_DIAG_MARK
,
358 inet_rsk(reqsk
)->ir_mark
))
365 static int sk_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
366 const struct inet_diag_req_v2
*r
,
367 struct user_namespace
*user_ns
,
368 u32 portid
, u32 seq
, u16 nlmsg_flags
,
369 const struct nlmsghdr
*unlh
, bool net_admin
)
371 if (sk
->sk_state
== TCP_TIME_WAIT
)
372 return inet_twsk_diag_fill(sk
, skb
, portid
, seq
,
375 if (sk
->sk_state
== TCP_NEW_SYN_RECV
)
376 return inet_req_diag_fill(sk
, skb
, portid
, seq
,
377 nlmsg_flags
, unlh
, net_admin
);
379 return inet_csk_diag_fill(sk
, skb
, r
, user_ns
, portid
, seq
,
380 nlmsg_flags
, unlh
, net_admin
);
383 struct sock
*inet_diag_find_one_icsk(struct net
*net
,
384 struct inet_hashinfo
*hashinfo
,
385 const struct inet_diag_req_v2
*req
)
390 if (req
->sdiag_family
== AF_INET
)
391 sk
= inet_lookup(net
, hashinfo
, NULL
, 0, req
->id
.idiag_dst
[0],
392 req
->id
.idiag_dport
, req
->id
.idiag_src
[0],
393 req
->id
.idiag_sport
, req
->id
.idiag_if
);
394 #if IS_ENABLED(CONFIG_IPV6)
395 else if (req
->sdiag_family
== AF_INET6
) {
396 if (ipv6_addr_v4mapped((struct in6_addr
*)req
->id
.idiag_dst
) &&
397 ipv6_addr_v4mapped((struct in6_addr
*)req
->id
.idiag_src
))
398 sk
= inet_lookup(net
, hashinfo
, NULL
, 0, req
->id
.idiag_dst
[3],
399 req
->id
.idiag_dport
, req
->id
.idiag_src
[3],
400 req
->id
.idiag_sport
, req
->id
.idiag_if
);
402 sk
= inet6_lookup(net
, hashinfo
, NULL
, 0,
403 (struct in6_addr
*)req
->id
.idiag_dst
,
405 (struct in6_addr
*)req
->id
.idiag_src
,
412 return ERR_PTR(-EINVAL
);
416 return ERR_PTR(-ENOENT
);
418 if (sock_diag_check_cookie(sk
, req
->id
.idiag_cookie
)) {
420 return ERR_PTR(-ENOENT
);
425 EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk
);
427 int inet_diag_dump_one_icsk(struct inet_hashinfo
*hashinfo
,
428 struct sk_buff
*in_skb
,
429 const struct nlmsghdr
*nlh
,
430 const struct inet_diag_req_v2
*req
)
432 struct net
*net
= sock_net(in_skb
->sk
);
437 sk
= inet_diag_find_one_icsk(net
, hashinfo
, req
);
441 rep
= nlmsg_new(inet_sk_attr_size(), GFP_KERNEL
);
447 err
= sk_diag_fill(sk
, rep
, req
,
448 sk_user_ns(NETLINK_CB(in_skb
).sk
),
449 NETLINK_CB(in_skb
).portid
,
450 nlh
->nlmsg_seq
, 0, nlh
,
451 netlink_net_capable(in_skb
, CAP_NET_ADMIN
));
453 WARN_ON(err
== -EMSGSIZE
);
457 err
= netlink_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
,
468 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk
);
470 static int inet_diag_cmd_exact(int cmd
, struct sk_buff
*in_skb
,
471 const struct nlmsghdr
*nlh
,
472 const struct inet_diag_req_v2
*req
)
474 const struct inet_diag_handler
*handler
;
477 handler
= inet_diag_lock_handler(req
->sdiag_protocol
);
479 err
= PTR_ERR(handler
);
480 else if (cmd
== SOCK_DIAG_BY_FAMILY
)
481 err
= handler
->dump_one(in_skb
, nlh
, req
);
482 else if (cmd
== SOCK_DESTROY
&& handler
->destroy
)
483 err
= handler
->destroy(in_skb
, req
);
486 inet_diag_unlock_handler(handler
);
491 static int bitstring_match(const __be32
*a1
, const __be32
*a2
, int bits
)
493 int words
= bits
>> 5;
498 if (memcmp(a1
, a2
, words
<< 2))
508 mask
= htonl((0xffffffff) << (32 - bits
));
510 if ((w1
^ w2
) & mask
)
517 static int inet_diag_bc_run(const struct nlattr
*_bc
,
518 const struct inet_diag_entry
*entry
)
520 const void *bc
= nla_data(_bc
);
521 int len
= nla_len(_bc
);
525 const struct inet_diag_bc_op
*op
= bc
;
528 case INET_DIAG_BC_NOP
:
530 case INET_DIAG_BC_JMP
:
533 case INET_DIAG_BC_S_GE
:
534 yes
= entry
->sport
>= op
[1].no
;
536 case INET_DIAG_BC_S_LE
:
537 yes
= entry
->sport
<= op
[1].no
;
539 case INET_DIAG_BC_D_GE
:
540 yes
= entry
->dport
>= op
[1].no
;
542 case INET_DIAG_BC_D_LE
:
543 yes
= entry
->dport
<= op
[1].no
;
545 case INET_DIAG_BC_AUTO
:
546 yes
= !(entry
->userlocks
& SOCK_BINDPORT_LOCK
);
548 case INET_DIAG_BC_S_COND
:
549 case INET_DIAG_BC_D_COND
: {
550 const struct inet_diag_hostcond
*cond
;
553 cond
= (const struct inet_diag_hostcond
*)(op
+ 1);
554 if (cond
->port
!= -1 &&
555 cond
->port
!= (op
->code
== INET_DIAG_BC_S_COND
?
556 entry
->sport
: entry
->dport
)) {
561 if (op
->code
== INET_DIAG_BC_S_COND
)
566 if (cond
->family
!= AF_UNSPEC
&&
567 cond
->family
!= entry
->family
) {
568 if (entry
->family
== AF_INET6
&&
569 cond
->family
== AF_INET
) {
570 if (addr
[0] == 0 && addr
[1] == 0 &&
571 addr
[2] == htonl(0xffff) &&
572 bitstring_match(addr
+ 3,
581 if (cond
->prefix_len
== 0)
583 if (bitstring_match(addr
, cond
->addr
,
589 case INET_DIAG_BC_DEV_COND
: {
592 ifindex
= *((const u32
*)(op
+ 1));
593 if (ifindex
!= entry
->ifindex
)
597 case INET_DIAG_BC_MARK_COND
: {
598 struct inet_diag_markcond
*cond
;
600 cond
= (struct inet_diag_markcond
*)(op
+ 1);
601 if ((entry
->mark
& cond
->mask
) != cond
->mark
)
618 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
620 static void entry_fill_addrs(struct inet_diag_entry
*entry
,
621 const struct sock
*sk
)
623 #if IS_ENABLED(CONFIG_IPV6)
624 if (sk
->sk_family
== AF_INET6
) {
625 entry
->saddr
= sk
->sk_v6_rcv_saddr
.s6_addr32
;
626 entry
->daddr
= sk
->sk_v6_daddr
.s6_addr32
;
630 entry
->saddr
= &sk
->sk_rcv_saddr
;
631 entry
->daddr
= &sk
->sk_daddr
;
635 int inet_diag_bc_sk(const struct nlattr
*bc
, struct sock
*sk
)
637 struct inet_sock
*inet
= inet_sk(sk
);
638 struct inet_diag_entry entry
;
643 entry
.family
= sk
->sk_family
;
644 entry_fill_addrs(&entry
, sk
);
645 entry
.sport
= inet
->inet_num
;
646 entry
.dport
= ntohs(inet
->inet_dport
);
647 entry
.ifindex
= sk
->sk_bound_dev_if
;
648 entry
.userlocks
= sk_fullsock(sk
) ? sk
->sk_userlocks
: 0;
650 entry
.mark
= sk
->sk_mark
;
651 else if (sk
->sk_state
== TCP_NEW_SYN_RECV
)
652 entry
.mark
= inet_rsk(inet_reqsk(sk
))->ir_mark
;
656 return inet_diag_bc_run(bc
, &entry
);
658 EXPORT_SYMBOL_GPL(inet_diag_bc_sk
);
660 static int valid_cc(const void *bc
, int len
, int cc
)
663 const struct inet_diag_bc_op
*op
= bc
;
669 if (op
->yes
< 4 || op
->yes
& 3)
677 /* data is u32 ifindex */
678 static bool valid_devcond(const struct inet_diag_bc_op
*op
, int len
,
681 /* Check ifindex space. */
682 *min_len
+= sizeof(u32
);
688 /* Validate an inet_diag_hostcond. */
689 static bool valid_hostcond(const struct inet_diag_bc_op
*op
, int len
,
692 struct inet_diag_hostcond
*cond
;
695 /* Check hostcond space. */
696 *min_len
+= sizeof(struct inet_diag_hostcond
);
699 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
701 /* Check address family and address length. */
702 switch (cond
->family
) {
707 addr_len
= sizeof(struct in_addr
);
710 addr_len
= sizeof(struct in6_addr
);
715 *min_len
+= addr_len
;
719 /* Check prefix length (in bits) vs address length (in bytes). */
720 if (cond
->prefix_len
> 8 * addr_len
)
726 /* Validate a port comparison operator. */
727 static bool valid_port_comparison(const struct inet_diag_bc_op
*op
,
728 int len
, int *min_len
)
730 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
731 *min_len
+= sizeof(struct inet_diag_bc_op
);
737 static bool valid_markcond(const struct inet_diag_bc_op
*op
, int len
,
740 *min_len
+= sizeof(struct inet_diag_markcond
);
741 return len
>= *min_len
;
744 static int inet_diag_bc_audit(const struct nlattr
*attr
,
745 const struct sk_buff
*skb
)
747 bool net_admin
= netlink_net_capable(skb
, CAP_NET_ADMIN
);
748 const void *bytecode
, *bc
;
749 int bytecode_len
, len
;
751 if (!attr
|| nla_len(attr
) < sizeof(struct inet_diag_bc_op
))
754 bytecode
= bc
= nla_data(attr
);
755 len
= bytecode_len
= nla_len(attr
);
758 int min_len
= sizeof(struct inet_diag_bc_op
);
759 const struct inet_diag_bc_op
*op
= bc
;
762 case INET_DIAG_BC_S_COND
:
763 case INET_DIAG_BC_D_COND
:
764 if (!valid_hostcond(bc
, len
, &min_len
))
767 case INET_DIAG_BC_DEV_COND
:
768 if (!valid_devcond(bc
, len
, &min_len
))
771 case INET_DIAG_BC_S_GE
:
772 case INET_DIAG_BC_S_LE
:
773 case INET_DIAG_BC_D_GE
:
774 case INET_DIAG_BC_D_LE
:
775 if (!valid_port_comparison(bc
, len
, &min_len
))
778 case INET_DIAG_BC_MARK_COND
:
781 if (!valid_markcond(bc
, len
, &min_len
))
784 case INET_DIAG_BC_AUTO
:
785 case INET_DIAG_BC_JMP
:
786 case INET_DIAG_BC_NOP
:
792 if (op
->code
!= INET_DIAG_BC_NOP
) {
793 if (op
->no
< min_len
|| op
->no
> len
+ 4 || op
->no
& 3)
796 !valid_cc(bytecode
, bytecode_len
, len
- op
->no
))
800 if (op
->yes
< min_len
|| op
->yes
> len
+ 4 || op
->yes
& 3)
805 return len
== 0 ? 0 : -EINVAL
;
808 static int inet_csk_diag_dump(struct sock
*sk
,
810 struct netlink_callback
*cb
,
811 const struct inet_diag_req_v2
*r
,
812 const struct nlattr
*bc
,
815 if (!inet_diag_bc_sk(bc
, sk
))
818 return inet_csk_diag_fill(sk
, skb
, r
,
819 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
820 NETLINK_CB(cb
->skb
).portid
,
821 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
,
825 static void twsk_build_assert(void)
827 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_family
) !=
828 offsetof(struct sock
, sk_family
));
830 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_num
) !=
831 offsetof(struct inet_sock
, inet_num
));
833 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_dport
) !=
834 offsetof(struct inet_sock
, inet_dport
));
836 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_rcv_saddr
) !=
837 offsetof(struct inet_sock
, inet_rcv_saddr
));
839 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_daddr
) !=
840 offsetof(struct inet_sock
, inet_daddr
));
842 #if IS_ENABLED(CONFIG_IPV6)
843 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_rcv_saddr
) !=
844 offsetof(struct sock
, sk_v6_rcv_saddr
));
846 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_daddr
) !=
847 offsetof(struct sock
, sk_v6_daddr
));
851 void inet_diag_dump_icsk(struct inet_hashinfo
*hashinfo
, struct sk_buff
*skb
,
852 struct netlink_callback
*cb
,
853 const struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
855 struct net
*net
= sock_net(skb
->sk
);
856 int i
, num
, s_i
, s_num
;
857 u32 idiag_states
= r
->idiag_states
;
858 bool net_admin
= netlink_net_capable(cb
->skb
, CAP_NET_ADMIN
);
860 if (idiag_states
& TCPF_SYN_RECV
)
861 idiag_states
|= TCPF_NEW_SYN_RECV
;
863 s_num
= num
= cb
->args
[2];
865 if (cb
->args
[0] == 0) {
866 if (!(idiag_states
& TCPF_LISTEN
))
869 for (i
= s_i
; i
< INET_LHTABLE_SIZE
; i
++) {
870 struct inet_listen_hashbucket
*ilb
;
874 ilb
= &hashinfo
->listening_hash
[i
];
875 spin_lock_bh(&ilb
->lock
);
876 sk_for_each(sk
, &ilb
->head
) {
877 struct inet_sock
*inet
= inet_sk(sk
);
879 if (!net_eq(sock_net(sk
), net
))
887 if (r
->sdiag_family
!= AF_UNSPEC
&&
888 sk
->sk_family
!= r
->sdiag_family
)
891 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
895 if (r
->id
.idiag_dport
||
899 if (inet_csk_diag_dump(sk
, skb
, cb
, r
,
900 bc
, net_admin
) < 0) {
901 spin_unlock_bh(&ilb
->lock
);
910 spin_unlock_bh(&ilb
->lock
);
918 s_i
= num
= s_num
= 0;
921 if (!(idiag_states
& ~TCPF_LISTEN
))
924 for (i
= s_i
; i
<= hashinfo
->ehash_mask
; i
++) {
925 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[i
];
926 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, i
);
927 struct hlist_nulls_node
*node
;
932 if (hlist_nulls_empty(&head
->chain
))
939 sk_nulls_for_each(sk
, node
, &head
->chain
) {
942 if (!net_eq(sock_net(sk
), net
))
946 state
= (sk
->sk_state
== TCP_TIME_WAIT
) ?
947 inet_twsk(sk
)->tw_substate
: sk
->sk_state
;
948 if (!(idiag_states
& (1 << state
)))
950 if (r
->sdiag_family
!= AF_UNSPEC
&&
951 sk
->sk_family
!= r
->sdiag_family
)
953 if (r
->id
.idiag_sport
!= htons(sk
->sk_num
) &&
956 if (r
->id
.idiag_dport
!= sk
->sk_dport
&&
961 if (!inet_diag_bc_sk(bc
, sk
))
964 res
= sk_diag_fill(sk
, skb
, r
,
965 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
966 NETLINK_CB(cb
->skb
).portid
,
967 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
970 spin_unlock_bh(lock
);
977 spin_unlock_bh(lock
);
987 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk
);
989 static int __inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
990 const struct inet_diag_req_v2
*r
,
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 int hdrlen
= sizeof(struct inet_diag_req_v2
);
1009 struct nlattr
*bc
= NULL
;
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 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
,
1030 struct netlink_callback
*cb
)
1032 struct inet_diag_req
*rc
= nlmsg_data(cb
->nlh
);
1033 int hdrlen
= sizeof(struct inet_diag_req
);
1034 struct inet_diag_req_v2 req
;
1035 struct nlattr
*bc
= NULL
;
1037 req
.sdiag_family
= AF_UNSPEC
; /* compatibility */
1038 req
.sdiag_protocol
= inet_diag_type2proto(cb
->nlh
->nlmsg_type
);
1039 req
.idiag_ext
= rc
->idiag_ext
;
1040 req
.idiag_states
= rc
->idiag_states
;
1043 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
1044 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
1046 return __inet_diag_dump(skb
, cb
, &req
, bc
);
1049 static int inet_diag_get_exact_compat(struct sk_buff
*in_skb
,
1050 const struct nlmsghdr
*nlh
)
1052 struct inet_diag_req
*rc
= nlmsg_data(nlh
);
1053 struct inet_diag_req_v2 req
;
1055 req
.sdiag_family
= rc
->idiag_family
;
1056 req
.sdiag_protocol
= inet_diag_type2proto(nlh
->nlmsg_type
);
1057 req
.idiag_ext
= rc
->idiag_ext
;
1058 req
.idiag_states
= rc
->idiag_states
;
1061 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY
, in_skb
, nlh
, &req
);
1064 static int inet_diag_rcv_msg_compat(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1066 int hdrlen
= sizeof(struct inet_diag_req
);
1067 struct net
*net
= sock_net(skb
->sk
);
1069 if (nlh
->nlmsg_type
>= INET_DIAG_GETSOCK_MAX
||
1070 nlmsg_len(nlh
) < hdrlen
)
1073 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1074 if (nlmsg_attrlen(nlh
, hdrlen
)) {
1075 struct nlattr
*attr
;
1078 attr
= nlmsg_find_attr(nlh
, hdrlen
,
1079 INET_DIAG_REQ_BYTECODE
);
1080 err
= inet_diag_bc_audit(attr
, skb
);
1085 struct netlink_dump_control c
= {
1086 .dump
= inet_diag_dump_compat
,
1088 return netlink_dump_start(net
->diag_nlsk
, skb
, nlh
, &c
);
1092 return inet_diag_get_exact_compat(skb
, nlh
);
1095 static int inet_diag_handler_cmd(struct sk_buff
*skb
, struct nlmsghdr
*h
)
1097 int hdrlen
= sizeof(struct inet_diag_req_v2
);
1098 struct net
*net
= sock_net(skb
->sk
);
1100 if (nlmsg_len(h
) < hdrlen
)
1103 if (h
->nlmsg_type
== SOCK_DIAG_BY_FAMILY
&&
1104 h
->nlmsg_flags
& NLM_F_DUMP
) {
1105 if (nlmsg_attrlen(h
, hdrlen
)) {
1106 struct nlattr
*attr
;
1109 attr
= nlmsg_find_attr(h
, hdrlen
,
1110 INET_DIAG_REQ_BYTECODE
);
1111 err
= inet_diag_bc_audit(attr
, skb
);
1116 struct netlink_dump_control c
= {
1117 .dump
= inet_diag_dump
,
1119 return netlink_dump_start(net
->diag_nlsk
, skb
, h
, &c
);
1123 return inet_diag_cmd_exact(h
->nlmsg_type
, skb
, h
, nlmsg_data(h
));
1127 int inet_diag_handler_get_info(struct sk_buff
*skb
, struct sock
*sk
)
1129 const struct inet_diag_handler
*handler
;
1130 struct nlmsghdr
*nlh
;
1131 struct nlattr
*attr
;
1132 struct inet_diag_msg
*r
;
1136 nlh
= nlmsg_put(skb
, 0, 0, SOCK_DIAG_BY_FAMILY
, sizeof(*r
), 0);
1140 r
= nlmsg_data(nlh
);
1141 memset(r
, 0, sizeof(*r
));
1142 inet_diag_msg_common_fill(r
, sk
);
1143 if (sk
->sk_type
== SOCK_DGRAM
|| sk
->sk_type
== SOCK_STREAM
)
1144 r
->id
.idiag_sport
= inet_sk(sk
)->inet_sport
;
1145 r
->idiag_state
= sk
->sk_state
;
1147 if ((err
= nla_put_u8(skb
, INET_DIAG_PROTOCOL
, sk
->sk_protocol
))) {
1148 nlmsg_cancel(skb
, nlh
);
1152 handler
= inet_diag_lock_handler(sk
->sk_protocol
);
1153 if (IS_ERR(handler
)) {
1154 inet_diag_unlock_handler(handler
);
1155 nlmsg_cancel(skb
, nlh
);
1156 return PTR_ERR(handler
);
1159 attr
= handler
->idiag_info_size
1160 ? nla_reserve_64bit(skb
, INET_DIAG_INFO
,
1161 handler
->idiag_info_size
,
1165 info
= nla_data(attr
);
1167 handler
->idiag_get_info(sk
, r
, info
);
1168 inet_diag_unlock_handler(handler
);
1170 nlmsg_end(skb
, nlh
);
1174 static const struct sock_diag_handler inet_diag_handler
= {
1176 .dump
= inet_diag_handler_cmd
,
1177 .get_info
= inet_diag_handler_get_info
,
1178 .destroy
= inet_diag_handler_cmd
,
1181 static const struct sock_diag_handler inet6_diag_handler
= {
1183 .dump
= inet_diag_handler_cmd
,
1184 .get_info
= inet_diag_handler_get_info
,
1185 .destroy
= inet_diag_handler_cmd
,
1188 int inet_diag_register(const struct inet_diag_handler
*h
)
1190 const __u16 type
= h
->idiag_type
;
1193 if (type
>= IPPROTO_MAX
)
1196 mutex_lock(&inet_diag_table_mutex
);
1198 if (!inet_diag_table
[type
]) {
1199 inet_diag_table
[type
] = h
;
1202 mutex_unlock(&inet_diag_table_mutex
);
1206 EXPORT_SYMBOL_GPL(inet_diag_register
);
1208 void inet_diag_unregister(const struct inet_diag_handler
*h
)
1210 const __u16 type
= h
->idiag_type
;
1212 if (type
>= IPPROTO_MAX
)
1215 mutex_lock(&inet_diag_table_mutex
);
1216 inet_diag_table
[type
] = NULL
;
1217 mutex_unlock(&inet_diag_table_mutex
);
1219 EXPORT_SYMBOL_GPL(inet_diag_unregister
);
1221 static int __init
inet_diag_init(void)
1223 const int inet_diag_table_size
= (IPPROTO_MAX
*
1224 sizeof(struct inet_diag_handler
*));
1227 inet_diag_table
= kzalloc(inet_diag_table_size
, GFP_KERNEL
);
1228 if (!inet_diag_table
)
1231 err
= sock_diag_register(&inet_diag_handler
);
1235 err
= sock_diag_register(&inet6_diag_handler
);
1239 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat
);
1244 sock_diag_unregister(&inet_diag_handler
);
1246 kfree(inet_diag_table
);
1250 static void __exit
inet_diag_exit(void)
1252 sock_diag_unregister(&inet6_diag_handler
);
1253 sock_diag_unregister(&inet_diag_handler
);
1254 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat
);
1255 kfree(inet_diag_table
);
1258 module_init(inet_diag_init
);
1259 module_exit(inet_diag_exit
);
1260 MODULE_LICENSE("GPL");
1261 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2 /* AF_INET */);
1262 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 10 /* AF_INET6 */);