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(sizeof(struct inet_diag_meminfo
))
103 + nla_total_size(sizeof(struct inet_diag_msg
))
104 + nla_total_size(SK_MEMINFO_VARS
* sizeof(u32
))
105 + nla_total_size(TCP_CA_NAME_MAX
)
106 + nla_total_size(sizeof(struct tcpvegas_info
))
110 int inet_diag_msg_attrs_fill(struct sock
*sk
, struct sk_buff
*skb
,
111 struct inet_diag_msg
*r
, int ext
,
112 struct user_namespace
*user_ns
)
114 const struct inet_sock
*inet
= inet_sk(sk
);
116 if (nla_put_u8(skb
, INET_DIAG_SHUTDOWN
, sk
->sk_shutdown
))
119 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
120 * hence this needs to be included regardless of socket family.
122 if (ext
& (1 << (INET_DIAG_TOS
- 1)))
123 if (nla_put_u8(skb
, INET_DIAG_TOS
, inet
->tos
) < 0)
126 #if IS_ENABLED(CONFIG_IPV6)
127 if (r
->idiag_family
== AF_INET6
) {
128 if (ext
& (1 << (INET_DIAG_TCLASS
- 1)))
129 if (nla_put_u8(skb
, INET_DIAG_TCLASS
,
130 inet6_sk(sk
)->tclass
) < 0)
133 if (((1 << sk
->sk_state
) & (TCPF_LISTEN
| TCPF_CLOSE
)) &&
134 nla_put_u8(skb
, INET_DIAG_SKV6ONLY
, ipv6_only_sock(sk
)))
139 r
->idiag_uid
= from_kuid_munged(user_ns
, sock_i_uid(sk
));
140 r
->idiag_inode
= sock_i_ino(sk
);
146 EXPORT_SYMBOL_GPL(inet_diag_msg_attrs_fill
);
148 int inet_sk_diag_fill(struct sock
*sk
, struct inet_connection_sock
*icsk
,
149 struct sk_buff
*skb
, const struct inet_diag_req_v2
*req
,
150 struct user_namespace
*user_ns
,
151 u32 portid
, u32 seq
, u16 nlmsg_flags
,
152 const struct nlmsghdr
*unlh
)
154 const struct tcp_congestion_ops
*ca_ops
;
155 const struct inet_diag_handler
*handler
;
156 int ext
= req
->idiag_ext
;
157 struct inet_diag_msg
*r
;
158 struct nlmsghdr
*nlh
;
162 handler
= inet_diag_table
[req
->sdiag_protocol
];
165 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
171 BUG_ON(!sk_fullsock(sk
));
173 inet_diag_msg_common_fill(r
, sk
);
174 r
->idiag_state
= sk
->sk_state
;
176 r
->idiag_retrans
= 0;
178 if (inet_diag_msg_attrs_fill(sk
, skb
, r
, ext
, user_ns
))
181 if (ext
& (1 << (INET_DIAG_MEMINFO
- 1))) {
182 struct inet_diag_meminfo minfo
= {
183 .idiag_rmem
= sk_rmem_alloc_get(sk
),
184 .idiag_wmem
= sk
->sk_wmem_queued
,
185 .idiag_fmem
= sk
->sk_forward_alloc
,
186 .idiag_tmem
= sk_wmem_alloc_get(sk
),
189 if (nla_put(skb
, INET_DIAG_MEMINFO
, sizeof(minfo
), &minfo
) < 0)
193 if (ext
& (1 << (INET_DIAG_SKMEMINFO
- 1)))
194 if (sock_diag_put_meminfo(sk
, skb
, INET_DIAG_SKMEMINFO
))
198 handler
->idiag_get_info(sk
, r
, NULL
);
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
;
208 jiffies_to_msecs(icsk
->icsk_timeout
- jiffies
);
209 } else if (icsk
->icsk_pending
== ICSK_TIME_PROBE0
) {
211 r
->idiag_retrans
= icsk
->icsk_probes_out
;
213 jiffies_to_msecs(icsk
->icsk_timeout
- jiffies
);
214 } else if (timer_pending(&sk
->sk_timer
)) {
216 r
->idiag_retrans
= icsk
->icsk_probes_out
;
218 jiffies_to_msecs(sk
->sk_timer
.expires
- jiffies
);
221 r
->idiag_expires
= 0;
224 if ((ext
& (1 << (INET_DIAG_INFO
- 1))) && handler
->idiag_info_size
) {
225 attr
= nla_reserve_64bit(skb
, INET_DIAG_INFO
,
226 handler
->idiag_info_size
,
231 info
= nla_data(attr
);
234 if (ext
& (1 << (INET_DIAG_CONG
- 1))) {
238 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
240 err
= nla_put_string(skb
, INET_DIAG_CONG
, ca_ops
->name
);
246 handler
->idiag_get_info(sk
, r
, info
);
248 if (sk
->sk_state
< TCP_TIME_WAIT
) {
249 union tcp_cc_info info
;
254 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
255 if (ca_ops
&& ca_ops
->get_info
)
256 sz
= ca_ops
->get_info(sk
, ext
, &attr
, &info
);
258 if (sz
&& nla_put(skb
, attr
, sz
, &info
) < 0)
267 nlmsg_cancel(skb
, nlh
);
270 EXPORT_SYMBOL_GPL(inet_sk_diag_fill
);
272 static int inet_csk_diag_fill(struct sock
*sk
,
274 const struct inet_diag_req_v2
*req
,
275 struct user_namespace
*user_ns
,
276 u32 portid
, u32 seq
, u16 nlmsg_flags
,
277 const struct nlmsghdr
*unlh
)
279 return inet_sk_diag_fill(sk
, inet_csk(sk
), skb
, req
,
280 user_ns
, portid
, seq
, nlmsg_flags
, unlh
);
283 static int inet_twsk_diag_fill(struct sock
*sk
,
285 u32 portid
, u32 seq
, u16 nlmsg_flags
,
286 const struct nlmsghdr
*unlh
)
288 struct inet_timewait_sock
*tw
= inet_twsk(sk
);
289 struct inet_diag_msg
*r
;
290 struct nlmsghdr
*nlh
;
293 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
299 BUG_ON(tw
->tw_state
!= TCP_TIME_WAIT
);
301 tmo
= tw
->tw_timer
.expires
- jiffies
;
305 inet_diag_msg_common_fill(r
, sk
);
306 r
->idiag_retrans
= 0;
308 r
->idiag_state
= tw
->tw_substate
;
310 r
->idiag_expires
= jiffies_to_msecs(tmo
);
320 static int inet_req_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
321 u32 portid
, u32 seq
, u16 nlmsg_flags
,
322 const struct nlmsghdr
*unlh
)
324 struct inet_diag_msg
*r
;
325 struct nlmsghdr
*nlh
;
328 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
334 inet_diag_msg_common_fill(r
, sk
);
335 r
->idiag_state
= TCP_SYN_RECV
;
337 r
->idiag_retrans
= inet_reqsk(sk
)->num_retrans
;
339 BUILD_BUG_ON(offsetof(struct inet_request_sock
, ir_cookie
) !=
340 offsetof(struct sock
, sk_cookie
));
342 tmo
= inet_reqsk(sk
)->rsk_timer
.expires
- jiffies
;
343 r
->idiag_expires
= (tmo
>= 0) ? jiffies_to_msecs(tmo
) : 0;
353 static int sk_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
354 const struct inet_diag_req_v2
*r
,
355 struct user_namespace
*user_ns
,
356 u32 portid
, u32 seq
, u16 nlmsg_flags
,
357 const struct nlmsghdr
*unlh
)
359 if (sk
->sk_state
== TCP_TIME_WAIT
)
360 return inet_twsk_diag_fill(sk
, skb
, portid
, seq
,
363 if (sk
->sk_state
== TCP_NEW_SYN_RECV
)
364 return inet_req_diag_fill(sk
, skb
, portid
, seq
,
367 return inet_csk_diag_fill(sk
, skb
, r
, user_ns
, portid
, seq
,
371 struct sock
*inet_diag_find_one_icsk(struct net
*net
,
372 struct inet_hashinfo
*hashinfo
,
373 const struct inet_diag_req_v2
*req
)
378 if (req
->sdiag_family
== AF_INET
)
379 sk
= inet_lookup(net
, hashinfo
, NULL
, 0, req
->id
.idiag_dst
[0],
380 req
->id
.idiag_dport
, req
->id
.idiag_src
[0],
381 req
->id
.idiag_sport
, req
->id
.idiag_if
);
382 #if IS_ENABLED(CONFIG_IPV6)
383 else if (req
->sdiag_family
== AF_INET6
) {
384 if (ipv6_addr_v4mapped((struct in6_addr
*)req
->id
.idiag_dst
) &&
385 ipv6_addr_v4mapped((struct in6_addr
*)req
->id
.idiag_src
))
386 sk
= inet_lookup(net
, hashinfo
, NULL
, 0, req
->id
.idiag_dst
[3],
387 req
->id
.idiag_dport
, req
->id
.idiag_src
[3],
388 req
->id
.idiag_sport
, req
->id
.idiag_if
);
390 sk
= inet6_lookup(net
, hashinfo
, NULL
, 0,
391 (struct in6_addr
*)req
->id
.idiag_dst
,
393 (struct in6_addr
*)req
->id
.idiag_src
,
400 return ERR_PTR(-EINVAL
);
404 return ERR_PTR(-ENOENT
);
406 if (sock_diag_check_cookie(sk
, req
->id
.idiag_cookie
)) {
408 return ERR_PTR(-ENOENT
);
413 EXPORT_SYMBOL_GPL(inet_diag_find_one_icsk
);
415 int inet_diag_dump_one_icsk(struct inet_hashinfo
*hashinfo
,
416 struct sk_buff
*in_skb
,
417 const struct nlmsghdr
*nlh
,
418 const struct inet_diag_req_v2
*req
)
420 struct net
*net
= sock_net(in_skb
->sk
);
425 sk
= inet_diag_find_one_icsk(net
, hashinfo
, req
);
429 rep
= nlmsg_new(inet_sk_attr_size(), GFP_KERNEL
);
435 err
= sk_diag_fill(sk
, rep
, req
,
436 sk_user_ns(NETLINK_CB(in_skb
).sk
),
437 NETLINK_CB(in_skb
).portid
,
438 nlh
->nlmsg_seq
, 0, nlh
);
440 WARN_ON(err
== -EMSGSIZE
);
444 err
= netlink_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
,
455 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk
);
457 static int inet_diag_cmd_exact(int cmd
, struct sk_buff
*in_skb
,
458 const struct nlmsghdr
*nlh
,
459 const struct inet_diag_req_v2
*req
)
461 const struct inet_diag_handler
*handler
;
464 handler
= inet_diag_lock_handler(req
->sdiag_protocol
);
466 err
= PTR_ERR(handler
);
467 else if (cmd
== SOCK_DIAG_BY_FAMILY
)
468 err
= handler
->dump_one(in_skb
, nlh
, req
);
469 else if (cmd
== SOCK_DESTROY
&& handler
->destroy
)
470 err
= handler
->destroy(in_skb
, req
);
473 inet_diag_unlock_handler(handler
);
478 static int bitstring_match(const __be32
*a1
, const __be32
*a2
, int bits
)
480 int words
= bits
>> 5;
485 if (memcmp(a1
, a2
, words
<< 2))
495 mask
= htonl((0xffffffff) << (32 - bits
));
497 if ((w1
^ w2
) & mask
)
504 static int inet_diag_bc_run(const struct nlattr
*_bc
,
505 const struct inet_diag_entry
*entry
)
507 const void *bc
= nla_data(_bc
);
508 int len
= nla_len(_bc
);
512 const struct inet_diag_bc_op
*op
= bc
;
515 case INET_DIAG_BC_NOP
:
517 case INET_DIAG_BC_JMP
:
520 case INET_DIAG_BC_S_GE
:
521 yes
= entry
->sport
>= op
[1].no
;
523 case INET_DIAG_BC_S_LE
:
524 yes
= entry
->sport
<= op
[1].no
;
526 case INET_DIAG_BC_D_GE
:
527 yes
= entry
->dport
>= op
[1].no
;
529 case INET_DIAG_BC_D_LE
:
530 yes
= entry
->dport
<= op
[1].no
;
532 case INET_DIAG_BC_AUTO
:
533 yes
= !(entry
->userlocks
& SOCK_BINDPORT_LOCK
);
535 case INET_DIAG_BC_S_COND
:
536 case INET_DIAG_BC_D_COND
: {
537 const struct inet_diag_hostcond
*cond
;
540 cond
= (const struct inet_diag_hostcond
*)(op
+ 1);
541 if (cond
->port
!= -1 &&
542 cond
->port
!= (op
->code
== INET_DIAG_BC_S_COND
?
543 entry
->sport
: entry
->dport
)) {
548 if (op
->code
== INET_DIAG_BC_S_COND
)
553 if (cond
->family
!= AF_UNSPEC
&&
554 cond
->family
!= entry
->family
) {
555 if (entry
->family
== AF_INET6
&&
556 cond
->family
== AF_INET
) {
557 if (addr
[0] == 0 && addr
[1] == 0 &&
558 addr
[2] == htonl(0xffff) &&
559 bitstring_match(addr
+ 3,
568 if (cond
->prefix_len
== 0)
570 if (bitstring_match(addr
, cond
->addr
,
576 case INET_DIAG_BC_DEV_COND
: {
579 ifindex
= *((const u32
*)(op
+ 1));
580 if (ifindex
!= entry
->ifindex
)
584 case INET_DIAG_BC_MARK_COND
: {
585 struct inet_diag_markcond
*cond
;
587 cond
= (struct inet_diag_markcond
*)(op
+ 1);
588 if ((entry
->mark
& cond
->mask
) != cond
->mark
)
605 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
607 static void entry_fill_addrs(struct inet_diag_entry
*entry
,
608 const struct sock
*sk
)
610 #if IS_ENABLED(CONFIG_IPV6)
611 if (sk
->sk_family
== AF_INET6
) {
612 entry
->saddr
= sk
->sk_v6_rcv_saddr
.s6_addr32
;
613 entry
->daddr
= sk
->sk_v6_daddr
.s6_addr32
;
617 entry
->saddr
= &sk
->sk_rcv_saddr
;
618 entry
->daddr
= &sk
->sk_daddr
;
622 int inet_diag_bc_sk(const struct nlattr
*bc
, struct sock
*sk
)
624 struct inet_sock
*inet
= inet_sk(sk
);
625 struct inet_diag_entry entry
;
630 entry
.family
= sk
->sk_family
;
631 entry_fill_addrs(&entry
, sk
);
632 entry
.sport
= inet
->inet_num
;
633 entry
.dport
= ntohs(inet
->inet_dport
);
634 entry
.ifindex
= sk
->sk_bound_dev_if
;
635 entry
.userlocks
= sk_fullsock(sk
) ? sk
->sk_userlocks
: 0;
637 entry
.mark
= sk
->sk_mark
;
638 else if (sk
->sk_state
== TCP_NEW_SYN_RECV
)
639 entry
.mark
= inet_rsk(inet_reqsk(sk
))->ir_mark
;
643 return inet_diag_bc_run(bc
, &entry
);
645 EXPORT_SYMBOL_GPL(inet_diag_bc_sk
);
647 static int valid_cc(const void *bc
, int len
, int cc
)
650 const struct inet_diag_bc_op
*op
= bc
;
656 if (op
->yes
< 4 || op
->yes
& 3)
664 /* data is u32 ifindex */
665 static bool valid_devcond(const struct inet_diag_bc_op
*op
, int len
,
668 /* Check ifindex space. */
669 *min_len
+= sizeof(u32
);
675 /* Validate an inet_diag_hostcond. */
676 static bool valid_hostcond(const struct inet_diag_bc_op
*op
, int len
,
679 struct inet_diag_hostcond
*cond
;
682 /* Check hostcond space. */
683 *min_len
+= sizeof(struct inet_diag_hostcond
);
686 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
688 /* Check address family and address length. */
689 switch (cond
->family
) {
694 addr_len
= sizeof(struct in_addr
);
697 addr_len
= sizeof(struct in6_addr
);
702 *min_len
+= addr_len
;
706 /* Check prefix length (in bits) vs address length (in bytes). */
707 if (cond
->prefix_len
> 8 * addr_len
)
713 /* Validate a port comparison operator. */
714 static bool valid_port_comparison(const struct inet_diag_bc_op
*op
,
715 int len
, int *min_len
)
717 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
718 *min_len
+= sizeof(struct inet_diag_bc_op
);
724 static bool valid_markcond(const struct inet_diag_bc_op
*op
, int len
,
727 *min_len
+= sizeof(struct inet_diag_markcond
);
728 return len
>= *min_len
;
731 static int inet_diag_bc_audit(const struct nlattr
*attr
,
732 const struct sk_buff
*skb
)
734 bool net_admin
= netlink_net_capable(skb
, CAP_NET_ADMIN
);
735 const void *bytecode
, *bc
;
736 int bytecode_len
, len
;
738 if (!attr
|| nla_len(attr
) < sizeof(struct inet_diag_bc_op
))
741 bytecode
= bc
= nla_data(attr
);
742 len
= bytecode_len
= nla_len(attr
);
745 int min_len
= sizeof(struct inet_diag_bc_op
);
746 const struct inet_diag_bc_op
*op
= bc
;
749 case INET_DIAG_BC_S_COND
:
750 case INET_DIAG_BC_D_COND
:
751 if (!valid_hostcond(bc
, len
, &min_len
))
754 case INET_DIAG_BC_DEV_COND
:
755 if (!valid_devcond(bc
, len
, &min_len
))
758 case INET_DIAG_BC_S_GE
:
759 case INET_DIAG_BC_S_LE
:
760 case INET_DIAG_BC_D_GE
:
761 case INET_DIAG_BC_D_LE
:
762 if (!valid_port_comparison(bc
, len
, &min_len
))
765 case INET_DIAG_BC_MARK_COND
:
768 if (!valid_markcond(bc
, len
, &min_len
))
771 case INET_DIAG_BC_AUTO
:
772 case INET_DIAG_BC_JMP
:
773 case INET_DIAG_BC_NOP
:
779 if (op
->code
!= INET_DIAG_BC_NOP
) {
780 if (op
->no
< min_len
|| op
->no
> len
+ 4 || op
->no
& 3)
783 !valid_cc(bytecode
, bytecode_len
, len
- op
->no
))
787 if (op
->yes
< min_len
|| op
->yes
> len
+ 4 || op
->yes
& 3)
792 return len
== 0 ? 0 : -EINVAL
;
795 static int inet_csk_diag_dump(struct sock
*sk
,
797 struct netlink_callback
*cb
,
798 const struct inet_diag_req_v2
*r
,
799 const struct nlattr
*bc
)
801 if (!inet_diag_bc_sk(bc
, sk
))
804 return inet_csk_diag_fill(sk
, skb
, r
,
805 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
806 NETLINK_CB(cb
->skb
).portid
,
807 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
810 static void twsk_build_assert(void)
812 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_family
) !=
813 offsetof(struct sock
, sk_family
));
815 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_num
) !=
816 offsetof(struct inet_sock
, inet_num
));
818 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_dport
) !=
819 offsetof(struct inet_sock
, inet_dport
));
821 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_rcv_saddr
) !=
822 offsetof(struct inet_sock
, inet_rcv_saddr
));
824 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_daddr
) !=
825 offsetof(struct inet_sock
, inet_daddr
));
827 #if IS_ENABLED(CONFIG_IPV6)
828 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_rcv_saddr
) !=
829 offsetof(struct sock
, sk_v6_rcv_saddr
));
831 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_daddr
) !=
832 offsetof(struct sock
, sk_v6_daddr
));
836 void inet_diag_dump_icsk(struct inet_hashinfo
*hashinfo
, struct sk_buff
*skb
,
837 struct netlink_callback
*cb
,
838 const struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
840 struct net
*net
= sock_net(skb
->sk
);
841 int i
, num
, s_i
, s_num
;
842 u32 idiag_states
= r
->idiag_states
;
844 if (idiag_states
& TCPF_SYN_RECV
)
845 idiag_states
|= TCPF_NEW_SYN_RECV
;
847 s_num
= num
= cb
->args
[2];
849 if (cb
->args
[0] == 0) {
850 if (!(idiag_states
& TCPF_LISTEN
))
853 for (i
= s_i
; i
< INET_LHTABLE_SIZE
; i
++) {
854 struct inet_listen_hashbucket
*ilb
;
858 ilb
= &hashinfo
->listening_hash
[i
];
859 spin_lock_bh(&ilb
->lock
);
860 sk_for_each(sk
, &ilb
->head
) {
861 struct inet_sock
*inet
= inet_sk(sk
);
863 if (!net_eq(sock_net(sk
), net
))
871 if (r
->sdiag_family
!= AF_UNSPEC
&&
872 sk
->sk_family
!= r
->sdiag_family
)
875 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
879 if (r
->id
.idiag_dport
||
883 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
884 spin_unlock_bh(&ilb
->lock
);
893 spin_unlock_bh(&ilb
->lock
);
901 s_i
= num
= s_num
= 0;
904 if (!(idiag_states
& ~TCPF_LISTEN
))
907 for (i
= s_i
; i
<= hashinfo
->ehash_mask
; i
++) {
908 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[i
];
909 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, i
);
910 struct hlist_nulls_node
*node
;
915 if (hlist_nulls_empty(&head
->chain
))
922 sk_nulls_for_each(sk
, node
, &head
->chain
) {
925 if (!net_eq(sock_net(sk
), net
))
929 state
= (sk
->sk_state
== TCP_TIME_WAIT
) ?
930 inet_twsk(sk
)->tw_substate
: sk
->sk_state
;
931 if (!(idiag_states
& (1 << state
)))
933 if (r
->sdiag_family
!= AF_UNSPEC
&&
934 sk
->sk_family
!= r
->sdiag_family
)
936 if (r
->id
.idiag_sport
!= htons(sk
->sk_num
) &&
939 if (r
->id
.idiag_dport
!= sk
->sk_dport
&&
944 if (!inet_diag_bc_sk(bc
, sk
))
947 res
= sk_diag_fill(sk
, skb
, r
,
948 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
949 NETLINK_CB(cb
->skb
).portid
,
950 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
953 spin_unlock_bh(lock
);
960 spin_unlock_bh(lock
);
970 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk
);
972 static int __inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
973 const struct inet_diag_req_v2
*r
,
976 const struct inet_diag_handler
*handler
;
979 handler
= inet_diag_lock_handler(r
->sdiag_protocol
);
980 if (!IS_ERR(handler
))
981 handler
->dump(skb
, cb
, r
, bc
);
983 err
= PTR_ERR(handler
);
984 inet_diag_unlock_handler(handler
);
986 return err
? : skb
->len
;
989 static int inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
991 int hdrlen
= sizeof(struct inet_diag_req_v2
);
992 struct nlattr
*bc
= NULL
;
994 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
995 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
997 return __inet_diag_dump(skb
, cb
, nlmsg_data(cb
->nlh
), bc
);
1000 static int inet_diag_type2proto(int type
)
1003 case TCPDIAG_GETSOCK
:
1005 case DCCPDIAG_GETSOCK
:
1006 return IPPROTO_DCCP
;
1012 static int inet_diag_dump_compat(struct sk_buff
*skb
,
1013 struct netlink_callback
*cb
)
1015 struct inet_diag_req
*rc
= nlmsg_data(cb
->nlh
);
1016 int hdrlen
= sizeof(struct inet_diag_req
);
1017 struct inet_diag_req_v2 req
;
1018 struct nlattr
*bc
= NULL
;
1020 req
.sdiag_family
= AF_UNSPEC
; /* compatibility */
1021 req
.sdiag_protocol
= inet_diag_type2proto(cb
->nlh
->nlmsg_type
);
1022 req
.idiag_ext
= rc
->idiag_ext
;
1023 req
.idiag_states
= rc
->idiag_states
;
1026 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
1027 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
1029 return __inet_diag_dump(skb
, cb
, &req
, bc
);
1032 static int inet_diag_get_exact_compat(struct sk_buff
*in_skb
,
1033 const struct nlmsghdr
*nlh
)
1035 struct inet_diag_req
*rc
= nlmsg_data(nlh
);
1036 struct inet_diag_req_v2 req
;
1038 req
.sdiag_family
= rc
->idiag_family
;
1039 req
.sdiag_protocol
= inet_diag_type2proto(nlh
->nlmsg_type
);
1040 req
.idiag_ext
= rc
->idiag_ext
;
1041 req
.idiag_states
= rc
->idiag_states
;
1044 return inet_diag_cmd_exact(SOCK_DIAG_BY_FAMILY
, in_skb
, nlh
, &req
);
1047 static int inet_diag_rcv_msg_compat(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
1049 int hdrlen
= sizeof(struct inet_diag_req
);
1050 struct net
*net
= sock_net(skb
->sk
);
1052 if (nlh
->nlmsg_type
>= INET_DIAG_GETSOCK_MAX
||
1053 nlmsg_len(nlh
) < hdrlen
)
1056 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
1057 if (nlmsg_attrlen(nlh
, hdrlen
)) {
1058 struct nlattr
*attr
;
1061 attr
= nlmsg_find_attr(nlh
, hdrlen
,
1062 INET_DIAG_REQ_BYTECODE
);
1063 err
= inet_diag_bc_audit(attr
, skb
);
1068 struct netlink_dump_control c
= {
1069 .dump
= inet_diag_dump_compat
,
1071 return netlink_dump_start(net
->diag_nlsk
, skb
, nlh
, &c
);
1075 return inet_diag_get_exact_compat(skb
, nlh
);
1078 static int inet_diag_handler_cmd(struct sk_buff
*skb
, struct nlmsghdr
*h
)
1080 int hdrlen
= sizeof(struct inet_diag_req_v2
);
1081 struct net
*net
= sock_net(skb
->sk
);
1083 if (nlmsg_len(h
) < hdrlen
)
1086 if (h
->nlmsg_type
== SOCK_DIAG_BY_FAMILY
&&
1087 h
->nlmsg_flags
& NLM_F_DUMP
) {
1088 if (nlmsg_attrlen(h
, hdrlen
)) {
1089 struct nlattr
*attr
;
1092 attr
= nlmsg_find_attr(h
, hdrlen
,
1093 INET_DIAG_REQ_BYTECODE
);
1094 err
= inet_diag_bc_audit(attr
, skb
);
1099 struct netlink_dump_control c
= {
1100 .dump
= inet_diag_dump
,
1102 return netlink_dump_start(net
->diag_nlsk
, skb
, h
, &c
);
1106 return inet_diag_cmd_exact(h
->nlmsg_type
, skb
, h
, nlmsg_data(h
));
1110 int inet_diag_handler_get_info(struct sk_buff
*skb
, struct sock
*sk
)
1112 const struct inet_diag_handler
*handler
;
1113 struct nlmsghdr
*nlh
;
1114 struct nlattr
*attr
;
1115 struct inet_diag_msg
*r
;
1119 nlh
= nlmsg_put(skb
, 0, 0, SOCK_DIAG_BY_FAMILY
, sizeof(*r
), 0);
1123 r
= nlmsg_data(nlh
);
1124 memset(r
, 0, sizeof(*r
));
1125 inet_diag_msg_common_fill(r
, sk
);
1126 if (sk
->sk_type
== SOCK_DGRAM
|| sk
->sk_type
== SOCK_STREAM
)
1127 r
->id
.idiag_sport
= inet_sk(sk
)->inet_sport
;
1128 r
->idiag_state
= sk
->sk_state
;
1130 if ((err
= nla_put_u8(skb
, INET_DIAG_PROTOCOL
, sk
->sk_protocol
))) {
1131 nlmsg_cancel(skb
, nlh
);
1135 handler
= inet_diag_lock_handler(sk
->sk_protocol
);
1136 if (IS_ERR(handler
)) {
1137 inet_diag_unlock_handler(handler
);
1138 nlmsg_cancel(skb
, nlh
);
1139 return PTR_ERR(handler
);
1142 attr
= handler
->idiag_info_size
1143 ? nla_reserve_64bit(skb
, INET_DIAG_INFO
,
1144 handler
->idiag_info_size
,
1148 info
= nla_data(attr
);
1150 handler
->idiag_get_info(sk
, r
, info
);
1151 inet_diag_unlock_handler(handler
);
1153 nlmsg_end(skb
, nlh
);
1157 static const struct sock_diag_handler inet_diag_handler
= {
1159 .dump
= inet_diag_handler_cmd
,
1160 .get_info
= inet_diag_handler_get_info
,
1161 .destroy
= inet_diag_handler_cmd
,
1164 static const struct sock_diag_handler inet6_diag_handler
= {
1166 .dump
= inet_diag_handler_cmd
,
1167 .get_info
= inet_diag_handler_get_info
,
1168 .destroy
= inet_diag_handler_cmd
,
1171 int inet_diag_register(const struct inet_diag_handler
*h
)
1173 const __u16 type
= h
->idiag_type
;
1176 if (type
>= IPPROTO_MAX
)
1179 mutex_lock(&inet_diag_table_mutex
);
1181 if (!inet_diag_table
[type
]) {
1182 inet_diag_table
[type
] = h
;
1185 mutex_unlock(&inet_diag_table_mutex
);
1189 EXPORT_SYMBOL_GPL(inet_diag_register
);
1191 void inet_diag_unregister(const struct inet_diag_handler
*h
)
1193 const __u16 type
= h
->idiag_type
;
1195 if (type
>= IPPROTO_MAX
)
1198 mutex_lock(&inet_diag_table_mutex
);
1199 inet_diag_table
[type
] = NULL
;
1200 mutex_unlock(&inet_diag_table_mutex
);
1202 EXPORT_SYMBOL_GPL(inet_diag_unregister
);
1204 static int __init
inet_diag_init(void)
1206 const int inet_diag_table_size
= (IPPROTO_MAX
*
1207 sizeof(struct inet_diag_handler
*));
1210 inet_diag_table
= kzalloc(inet_diag_table_size
, GFP_KERNEL
);
1211 if (!inet_diag_table
)
1214 err
= sock_diag_register(&inet_diag_handler
);
1218 err
= sock_diag_register(&inet6_diag_handler
);
1222 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat
);
1227 sock_diag_unregister(&inet_diag_handler
);
1229 kfree(inet_diag_table
);
1233 static void __exit
inet_diag_exit(void)
1235 sock_diag_unregister(&inet6_diag_handler
);
1236 sock_diag_unregister(&inet_diag_handler
);
1237 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat
);
1238 kfree(inet_diag_table
);
1241 module_init(inet_diag_init
);
1242 module_exit(inet_diag_exit
);
1243 MODULE_LICENSE("GPL");
1244 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2 /* AF_INET */);
1245 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 10 /* AF_INET6 */);