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 static 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
;
93 static size_t inet_sk_attr_size(void)
95 return nla_total_size(sizeof(struct tcp_info
))
96 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
97 + nla_total_size(1) /* INET_DIAG_TOS */
98 + nla_total_size(1) /* INET_DIAG_TCLASS */
99 + nla_total_size(sizeof(struct inet_diag_meminfo
))
100 + nla_total_size(sizeof(struct inet_diag_msg
))
101 + nla_total_size(SK_MEMINFO_VARS
* sizeof(u32
))
102 + nla_total_size(TCP_CA_NAME_MAX
)
103 + nla_total_size(sizeof(struct tcpvegas_info
))
107 int inet_sk_diag_fill(struct sock
*sk
, struct inet_connection_sock
*icsk
,
108 struct sk_buff
*skb
, const struct inet_diag_req_v2
*req
,
109 struct user_namespace
*user_ns
,
110 u32 portid
, u32 seq
, u16 nlmsg_flags
,
111 const struct nlmsghdr
*unlh
)
113 const struct inet_sock
*inet
= inet_sk(sk
);
114 const struct tcp_congestion_ops
*ca_ops
;
115 const struct inet_diag_handler
*handler
;
116 int ext
= req
->idiag_ext
;
117 struct inet_diag_msg
*r
;
118 struct nlmsghdr
*nlh
;
122 handler
= inet_diag_table
[req
->sdiag_protocol
];
125 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
131 BUG_ON(!sk_fullsock(sk
));
133 inet_diag_msg_common_fill(r
, sk
);
134 r
->idiag_state
= sk
->sk_state
;
136 r
->idiag_retrans
= 0;
138 if (nla_put_u8(skb
, INET_DIAG_SHUTDOWN
, sk
->sk_shutdown
))
141 /* IPv6 dual-stack sockets use inet->tos for IPv4 connections,
142 * hence this needs to be included regardless of socket family.
144 if (ext
& (1 << (INET_DIAG_TOS
- 1)))
145 if (nla_put_u8(skb
, INET_DIAG_TOS
, inet
->tos
) < 0)
148 #if IS_ENABLED(CONFIG_IPV6)
149 if (r
->idiag_family
== AF_INET6
) {
150 if (ext
& (1 << (INET_DIAG_TCLASS
- 1)))
151 if (nla_put_u8(skb
, INET_DIAG_TCLASS
,
152 inet6_sk(sk
)->tclass
) < 0)
155 if (((1 << sk
->sk_state
) & (TCPF_LISTEN
| TCPF_CLOSE
)) &&
156 nla_put_u8(skb
, INET_DIAG_SKV6ONLY
, ipv6_only_sock(sk
)))
161 r
->idiag_uid
= from_kuid_munged(user_ns
, sock_i_uid(sk
));
162 r
->idiag_inode
= sock_i_ino(sk
);
164 if (ext
& (1 << (INET_DIAG_MEMINFO
- 1))) {
165 struct inet_diag_meminfo minfo
= {
166 .idiag_rmem
= sk_rmem_alloc_get(sk
),
167 .idiag_wmem
= sk
->sk_wmem_queued
,
168 .idiag_fmem
= sk
->sk_forward_alloc
,
169 .idiag_tmem
= sk_wmem_alloc_get(sk
),
172 if (nla_put(skb
, INET_DIAG_MEMINFO
, sizeof(minfo
), &minfo
) < 0)
176 if (ext
& (1 << (INET_DIAG_SKMEMINFO
- 1)))
177 if (sock_diag_put_meminfo(sk
, skb
, INET_DIAG_SKMEMINFO
))
181 handler
->idiag_get_info(sk
, r
, NULL
);
185 #define EXPIRES_IN_MS(tmo) DIV_ROUND_UP((tmo - jiffies) * 1000, HZ)
187 if (icsk
->icsk_pending
== ICSK_TIME_RETRANS
||
188 icsk
->icsk_pending
== ICSK_TIME_EARLY_RETRANS
||
189 icsk
->icsk_pending
== ICSK_TIME_LOSS_PROBE
) {
191 r
->idiag_retrans
= icsk
->icsk_retransmits
;
192 r
->idiag_expires
= EXPIRES_IN_MS(icsk
->icsk_timeout
);
193 } else if (icsk
->icsk_pending
== ICSK_TIME_PROBE0
) {
195 r
->idiag_retrans
= icsk
->icsk_probes_out
;
196 r
->idiag_expires
= EXPIRES_IN_MS(icsk
->icsk_timeout
);
197 } else if (timer_pending(&sk
->sk_timer
)) {
199 r
->idiag_retrans
= icsk
->icsk_probes_out
;
200 r
->idiag_expires
= EXPIRES_IN_MS(sk
->sk_timer
.expires
);
203 r
->idiag_expires
= 0;
207 if ((ext
& (1 << (INET_DIAG_INFO
- 1))) && handler
->idiag_info_size
) {
208 attr
= nla_reserve(skb
, INET_DIAG_INFO
,
209 handler
->idiag_info_size
);
213 info
= nla_data(attr
);
216 if (ext
& (1 << (INET_DIAG_CONG
- 1))) {
220 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
222 err
= nla_put_string(skb
, INET_DIAG_CONG
, ca_ops
->name
);
228 handler
->idiag_get_info(sk
, r
, info
);
230 if (sk
->sk_state
< TCP_TIME_WAIT
) {
231 union tcp_cc_info info
;
236 ca_ops
= READ_ONCE(icsk
->icsk_ca_ops
);
237 if (ca_ops
&& ca_ops
->get_info
)
238 sz
= ca_ops
->get_info(sk
, ext
, &attr
, &info
);
240 if (sz
&& nla_put(skb
, attr
, sz
, &info
) < 0)
249 nlmsg_cancel(skb
, nlh
);
252 EXPORT_SYMBOL_GPL(inet_sk_diag_fill
);
254 static int inet_csk_diag_fill(struct sock
*sk
,
256 const struct inet_diag_req_v2
*req
,
257 struct user_namespace
*user_ns
,
258 u32 portid
, u32 seq
, u16 nlmsg_flags
,
259 const struct nlmsghdr
*unlh
)
261 return inet_sk_diag_fill(sk
, inet_csk(sk
), skb
, req
,
262 user_ns
, portid
, seq
, nlmsg_flags
, unlh
);
265 static int inet_twsk_diag_fill(struct sock
*sk
,
267 u32 portid
, u32 seq
, u16 nlmsg_flags
,
268 const struct nlmsghdr
*unlh
)
270 struct inet_timewait_sock
*tw
= inet_twsk(sk
);
271 struct inet_diag_msg
*r
;
272 struct nlmsghdr
*nlh
;
275 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
281 BUG_ON(tw
->tw_state
!= TCP_TIME_WAIT
);
283 tmo
= tw
->tw_timer
.expires
- jiffies
;
287 inet_diag_msg_common_fill(r
, sk
);
288 r
->idiag_retrans
= 0;
290 r
->idiag_state
= tw
->tw_substate
;
292 r
->idiag_expires
= jiffies_to_msecs(tmo
);
302 static int inet_req_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
303 u32 portid
, u32 seq
, u16 nlmsg_flags
,
304 const struct nlmsghdr
*unlh
)
306 struct inet_diag_msg
*r
;
307 struct nlmsghdr
*nlh
;
310 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
316 inet_diag_msg_common_fill(r
, sk
);
317 r
->idiag_state
= TCP_SYN_RECV
;
319 r
->idiag_retrans
= inet_reqsk(sk
)->num_retrans
;
321 BUILD_BUG_ON(offsetof(struct inet_request_sock
, ir_cookie
) !=
322 offsetof(struct sock
, sk_cookie
));
324 tmo
= inet_reqsk(sk
)->rsk_timer
.expires
- jiffies
;
325 r
->idiag_expires
= (tmo
>= 0) ? jiffies_to_msecs(tmo
) : 0;
335 static int sk_diag_fill(struct sock
*sk
, struct sk_buff
*skb
,
336 const struct inet_diag_req_v2
*r
,
337 struct user_namespace
*user_ns
,
338 u32 portid
, u32 seq
, u16 nlmsg_flags
,
339 const struct nlmsghdr
*unlh
)
341 if (sk
->sk_state
== TCP_TIME_WAIT
)
342 return inet_twsk_diag_fill(sk
, skb
, portid
, seq
,
345 if (sk
->sk_state
== TCP_NEW_SYN_RECV
)
346 return inet_req_diag_fill(sk
, skb
, portid
, seq
,
349 return inet_csk_diag_fill(sk
, skb
, r
, user_ns
, portid
, seq
,
353 int inet_diag_dump_one_icsk(struct inet_hashinfo
*hashinfo
,
354 struct sk_buff
*in_skb
,
355 const struct nlmsghdr
*nlh
,
356 const struct inet_diag_req_v2
*req
)
358 struct net
*net
= sock_net(in_skb
->sk
);
364 if (req
->sdiag_family
== AF_INET
)
365 sk
= inet_lookup(net
, hashinfo
, req
->id
.idiag_dst
[0],
366 req
->id
.idiag_dport
, req
->id
.idiag_src
[0],
367 req
->id
.idiag_sport
, req
->id
.idiag_if
);
368 #if IS_ENABLED(CONFIG_IPV6)
369 else if (req
->sdiag_family
== AF_INET6
)
370 sk
= inet6_lookup(net
, hashinfo
,
371 (struct in6_addr
*)req
->id
.idiag_dst
,
373 (struct in6_addr
*)req
->id
.idiag_src
,
384 err
= sock_diag_check_cookie(sk
, req
->id
.idiag_cookie
);
388 rep
= nlmsg_new(inet_sk_attr_size(), GFP_KERNEL
);
394 err
= sk_diag_fill(sk
, rep
, req
,
395 sk_user_ns(NETLINK_CB(in_skb
).sk
),
396 NETLINK_CB(in_skb
).portid
,
397 nlh
->nlmsg_seq
, 0, nlh
);
399 WARN_ON(err
== -EMSGSIZE
);
403 err
= netlink_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
,
415 EXPORT_SYMBOL_GPL(inet_diag_dump_one_icsk
);
417 static int inet_diag_get_exact(struct sk_buff
*in_skb
,
418 const struct nlmsghdr
*nlh
,
419 const struct inet_diag_req_v2
*req
)
421 const struct inet_diag_handler
*handler
;
424 handler
= inet_diag_lock_handler(req
->sdiag_protocol
);
426 err
= PTR_ERR(handler
);
428 err
= handler
->dump_one(in_skb
, nlh
, req
);
429 inet_diag_unlock_handler(handler
);
434 static int bitstring_match(const __be32
*a1
, const __be32
*a2
, int bits
)
436 int words
= bits
>> 5;
441 if (memcmp(a1
, a2
, words
<< 2))
451 mask
= htonl((0xffffffff) << (32 - bits
));
453 if ((w1
^ w2
) & mask
)
460 static int inet_diag_bc_run(const struct nlattr
*_bc
,
461 const struct inet_diag_entry
*entry
)
463 const void *bc
= nla_data(_bc
);
464 int len
= nla_len(_bc
);
468 const struct inet_diag_bc_op
*op
= bc
;
471 case INET_DIAG_BC_NOP
:
473 case INET_DIAG_BC_JMP
:
476 case INET_DIAG_BC_S_GE
:
477 yes
= entry
->sport
>= op
[1].no
;
479 case INET_DIAG_BC_S_LE
:
480 yes
= entry
->sport
<= op
[1].no
;
482 case INET_DIAG_BC_D_GE
:
483 yes
= entry
->dport
>= op
[1].no
;
485 case INET_DIAG_BC_D_LE
:
486 yes
= entry
->dport
<= op
[1].no
;
488 case INET_DIAG_BC_AUTO
:
489 yes
= !(entry
->userlocks
& SOCK_BINDPORT_LOCK
);
491 case INET_DIAG_BC_S_COND
:
492 case INET_DIAG_BC_D_COND
: {
493 const struct inet_diag_hostcond
*cond
;
496 cond
= (const struct inet_diag_hostcond
*)(op
+ 1);
497 if (cond
->port
!= -1 &&
498 cond
->port
!= (op
->code
== INET_DIAG_BC_S_COND
?
499 entry
->sport
: entry
->dport
)) {
504 if (op
->code
== INET_DIAG_BC_S_COND
)
509 if (cond
->family
!= AF_UNSPEC
&&
510 cond
->family
!= entry
->family
) {
511 if (entry
->family
== AF_INET6
&&
512 cond
->family
== AF_INET
) {
513 if (addr
[0] == 0 && addr
[1] == 0 &&
514 addr
[2] == htonl(0xffff) &&
515 bitstring_match(addr
+ 3,
524 if (cond
->prefix_len
== 0)
526 if (bitstring_match(addr
, cond
->addr
,
545 /* This helper is available for all sockets (ESTABLISH, TIMEWAIT, SYN_RECV)
547 static void entry_fill_addrs(struct inet_diag_entry
*entry
,
548 const struct sock
*sk
)
550 #if IS_ENABLED(CONFIG_IPV6)
551 if (sk
->sk_family
== AF_INET6
) {
552 entry
->saddr
= sk
->sk_v6_rcv_saddr
.s6_addr32
;
553 entry
->daddr
= sk
->sk_v6_daddr
.s6_addr32
;
557 entry
->saddr
= &sk
->sk_rcv_saddr
;
558 entry
->daddr
= &sk
->sk_daddr
;
562 int inet_diag_bc_sk(const struct nlattr
*bc
, struct sock
*sk
)
564 struct inet_sock
*inet
= inet_sk(sk
);
565 struct inet_diag_entry entry
;
570 entry
.family
= sk
->sk_family
;
571 entry_fill_addrs(&entry
, sk
);
572 entry
.sport
= inet
->inet_num
;
573 entry
.dport
= ntohs(inet
->inet_dport
);
574 entry
.userlocks
= sk_fullsock(sk
) ? sk
->sk_userlocks
: 0;
576 return inet_diag_bc_run(bc
, &entry
);
578 EXPORT_SYMBOL_GPL(inet_diag_bc_sk
);
580 static int valid_cc(const void *bc
, int len
, int cc
)
583 const struct inet_diag_bc_op
*op
= bc
;
589 if (op
->yes
< 4 || op
->yes
& 3)
597 /* Validate an inet_diag_hostcond. */
598 static bool valid_hostcond(const struct inet_diag_bc_op
*op
, int len
,
601 struct inet_diag_hostcond
*cond
;
604 /* Check hostcond space. */
605 *min_len
+= sizeof(struct inet_diag_hostcond
);
608 cond
= (struct inet_diag_hostcond
*)(op
+ 1);
610 /* Check address family and address length. */
611 switch (cond
->family
) {
616 addr_len
= sizeof(struct in_addr
);
619 addr_len
= sizeof(struct in6_addr
);
624 *min_len
+= addr_len
;
628 /* Check prefix length (in bits) vs address length (in bytes). */
629 if (cond
->prefix_len
> 8 * addr_len
)
635 /* Validate a port comparison operator. */
636 static bool valid_port_comparison(const struct inet_diag_bc_op
*op
,
637 int len
, int *min_len
)
639 /* Port comparisons put the port in a follow-on inet_diag_bc_op. */
640 *min_len
+= sizeof(struct inet_diag_bc_op
);
646 static int inet_diag_bc_audit(const void *bytecode
, int bytecode_len
)
648 const void *bc
= bytecode
;
649 int len
= bytecode_len
;
652 int min_len
= sizeof(struct inet_diag_bc_op
);
653 const struct inet_diag_bc_op
*op
= bc
;
656 case INET_DIAG_BC_S_COND
:
657 case INET_DIAG_BC_D_COND
:
658 if (!valid_hostcond(bc
, len
, &min_len
))
661 case INET_DIAG_BC_S_GE
:
662 case INET_DIAG_BC_S_LE
:
663 case INET_DIAG_BC_D_GE
:
664 case INET_DIAG_BC_D_LE
:
665 if (!valid_port_comparison(bc
, len
, &min_len
))
668 case INET_DIAG_BC_AUTO
:
669 case INET_DIAG_BC_JMP
:
670 case INET_DIAG_BC_NOP
:
676 if (op
->code
!= INET_DIAG_BC_NOP
) {
677 if (op
->no
< min_len
|| op
->no
> len
+ 4 || op
->no
& 3)
680 !valid_cc(bytecode
, bytecode_len
, len
- op
->no
))
684 if (op
->yes
< min_len
|| op
->yes
> len
+ 4 || op
->yes
& 3)
689 return len
== 0 ? 0 : -EINVAL
;
692 static int inet_csk_diag_dump(struct sock
*sk
,
694 struct netlink_callback
*cb
,
695 const struct inet_diag_req_v2
*r
,
696 const struct nlattr
*bc
)
698 if (!inet_diag_bc_sk(bc
, sk
))
701 return inet_csk_diag_fill(sk
, skb
, r
,
702 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
703 NETLINK_CB(cb
->skb
).portid
,
704 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
, cb
->nlh
);
707 static void twsk_build_assert(void)
709 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_family
) !=
710 offsetof(struct sock
, sk_family
));
712 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_num
) !=
713 offsetof(struct inet_sock
, inet_num
));
715 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_dport
) !=
716 offsetof(struct inet_sock
, inet_dport
));
718 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_rcv_saddr
) !=
719 offsetof(struct inet_sock
, inet_rcv_saddr
));
721 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_daddr
) !=
722 offsetof(struct inet_sock
, inet_daddr
));
724 #if IS_ENABLED(CONFIG_IPV6)
725 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_rcv_saddr
) !=
726 offsetof(struct sock
, sk_v6_rcv_saddr
));
728 BUILD_BUG_ON(offsetof(struct inet_timewait_sock
, tw_v6_daddr
) !=
729 offsetof(struct sock
, sk_v6_daddr
));
733 void inet_diag_dump_icsk(struct inet_hashinfo
*hashinfo
, struct sk_buff
*skb
,
734 struct netlink_callback
*cb
,
735 const struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
737 struct net
*net
= sock_net(skb
->sk
);
738 int i
, num
, s_i
, s_num
;
739 u32 idiag_states
= r
->idiag_states
;
741 if (idiag_states
& TCPF_SYN_RECV
)
742 idiag_states
|= TCPF_NEW_SYN_RECV
;
744 s_num
= num
= cb
->args
[2];
746 if (cb
->args
[0] == 0) {
747 if (!(idiag_states
& TCPF_LISTEN
))
750 for (i
= s_i
; i
< INET_LHTABLE_SIZE
; i
++) {
751 struct inet_listen_hashbucket
*ilb
;
752 struct hlist_nulls_node
*node
;
756 ilb
= &hashinfo
->listening_hash
[i
];
757 spin_lock_bh(&ilb
->lock
);
758 sk_nulls_for_each(sk
, node
, &ilb
->head
) {
759 struct inet_sock
*inet
= inet_sk(sk
);
761 if (!net_eq(sock_net(sk
), net
))
769 if (r
->sdiag_family
!= AF_UNSPEC
&&
770 sk
->sk_family
!= r
->sdiag_family
)
773 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
777 if (r
->id
.idiag_dport
||
781 if (inet_csk_diag_dump(sk
, skb
, cb
, r
, bc
) < 0) {
782 spin_unlock_bh(&ilb
->lock
);
791 spin_unlock_bh(&ilb
->lock
);
799 s_i
= num
= s_num
= 0;
802 if (!(idiag_states
& ~TCPF_LISTEN
))
805 for (i
= s_i
; i
<= hashinfo
->ehash_mask
; i
++) {
806 struct inet_ehash_bucket
*head
= &hashinfo
->ehash
[i
];
807 spinlock_t
*lock
= inet_ehash_lockp(hashinfo
, i
);
808 struct hlist_nulls_node
*node
;
813 if (hlist_nulls_empty(&head
->chain
))
820 sk_nulls_for_each(sk
, node
, &head
->chain
) {
823 if (!net_eq(sock_net(sk
), net
))
827 state
= (sk
->sk_state
== TCP_TIME_WAIT
) ?
828 inet_twsk(sk
)->tw_substate
: sk
->sk_state
;
829 if (!(idiag_states
& (1 << state
)))
831 if (r
->sdiag_family
!= AF_UNSPEC
&&
832 sk
->sk_family
!= r
->sdiag_family
)
834 if (r
->id
.idiag_sport
!= htons(sk
->sk_num
) &&
837 if (r
->id
.idiag_dport
!= sk
->sk_dport
&&
842 if (!inet_diag_bc_sk(bc
, sk
))
845 res
= sk_diag_fill(sk
, skb
, r
,
846 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
847 NETLINK_CB(cb
->skb
).portid
,
848 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
851 spin_unlock_bh(lock
);
858 spin_unlock_bh(lock
);
867 EXPORT_SYMBOL_GPL(inet_diag_dump_icsk
);
869 static int __inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
870 const struct inet_diag_req_v2
*r
,
873 const struct inet_diag_handler
*handler
;
876 handler
= inet_diag_lock_handler(r
->sdiag_protocol
);
877 if (!IS_ERR(handler
))
878 handler
->dump(skb
, cb
, r
, bc
);
880 err
= PTR_ERR(handler
);
881 inet_diag_unlock_handler(handler
);
883 return err
? : skb
->len
;
886 static int inet_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
)
888 int hdrlen
= sizeof(struct inet_diag_req_v2
);
889 struct nlattr
*bc
= NULL
;
891 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
892 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
894 return __inet_diag_dump(skb
, cb
, nlmsg_data(cb
->nlh
), bc
);
897 static int inet_diag_type2proto(int type
)
900 case TCPDIAG_GETSOCK
:
902 case DCCPDIAG_GETSOCK
:
909 static int inet_diag_dump_compat(struct sk_buff
*skb
,
910 struct netlink_callback
*cb
)
912 struct inet_diag_req
*rc
= nlmsg_data(cb
->nlh
);
913 int hdrlen
= sizeof(struct inet_diag_req
);
914 struct inet_diag_req_v2 req
;
915 struct nlattr
*bc
= NULL
;
917 req
.sdiag_family
= AF_UNSPEC
; /* compatibility */
918 req
.sdiag_protocol
= inet_diag_type2proto(cb
->nlh
->nlmsg_type
);
919 req
.idiag_ext
= rc
->idiag_ext
;
920 req
.idiag_states
= rc
->idiag_states
;
923 if (nlmsg_attrlen(cb
->nlh
, hdrlen
))
924 bc
= nlmsg_find_attr(cb
->nlh
, hdrlen
, INET_DIAG_REQ_BYTECODE
);
926 return __inet_diag_dump(skb
, cb
, &req
, bc
);
929 static int inet_diag_get_exact_compat(struct sk_buff
*in_skb
,
930 const struct nlmsghdr
*nlh
)
932 struct inet_diag_req
*rc
= nlmsg_data(nlh
);
933 struct inet_diag_req_v2 req
;
935 req
.sdiag_family
= rc
->idiag_family
;
936 req
.sdiag_protocol
= inet_diag_type2proto(nlh
->nlmsg_type
);
937 req
.idiag_ext
= rc
->idiag_ext
;
938 req
.idiag_states
= rc
->idiag_states
;
941 return inet_diag_get_exact(in_skb
, nlh
, &req
);
944 static int inet_diag_rcv_msg_compat(struct sk_buff
*skb
, struct nlmsghdr
*nlh
)
946 int hdrlen
= sizeof(struct inet_diag_req
);
947 struct net
*net
= sock_net(skb
->sk
);
949 if (nlh
->nlmsg_type
>= INET_DIAG_GETSOCK_MAX
||
950 nlmsg_len(nlh
) < hdrlen
)
953 if (nlh
->nlmsg_flags
& NLM_F_DUMP
) {
954 if (nlmsg_attrlen(nlh
, hdrlen
)) {
957 attr
= nlmsg_find_attr(nlh
, hdrlen
,
958 INET_DIAG_REQ_BYTECODE
);
960 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
961 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
965 struct netlink_dump_control c
= {
966 .dump
= inet_diag_dump_compat
,
968 return netlink_dump_start(net
->diag_nlsk
, skb
, nlh
, &c
);
972 return inet_diag_get_exact_compat(skb
, nlh
);
975 static int inet_diag_handler_dump(struct sk_buff
*skb
, struct nlmsghdr
*h
)
977 int hdrlen
= sizeof(struct inet_diag_req_v2
);
978 struct net
*net
= sock_net(skb
->sk
);
980 if (nlmsg_len(h
) < hdrlen
)
983 if (h
->nlmsg_flags
& NLM_F_DUMP
) {
984 if (nlmsg_attrlen(h
, hdrlen
)) {
987 attr
= nlmsg_find_attr(h
, hdrlen
,
988 INET_DIAG_REQ_BYTECODE
);
990 nla_len(attr
) < sizeof(struct inet_diag_bc_op
) ||
991 inet_diag_bc_audit(nla_data(attr
), nla_len(attr
)))
995 struct netlink_dump_control c
= {
996 .dump
= inet_diag_dump
,
998 return netlink_dump_start(net
->diag_nlsk
, skb
, h
, &c
);
1002 return inet_diag_get_exact(skb
, h
, nlmsg_data(h
));
1006 int inet_diag_handler_get_info(struct sk_buff
*skb
, struct sock
*sk
)
1008 const struct inet_diag_handler
*handler
;
1009 struct nlmsghdr
*nlh
;
1010 struct nlattr
*attr
;
1011 struct inet_diag_msg
*r
;
1015 nlh
= nlmsg_put(skb
, 0, 0, SOCK_DIAG_BY_FAMILY
, sizeof(*r
), 0);
1019 r
= nlmsg_data(nlh
);
1020 memset(r
, 0, sizeof(*r
));
1021 inet_diag_msg_common_fill(r
, sk
);
1022 if (sk
->sk_type
== SOCK_DGRAM
|| sk
->sk_type
== SOCK_STREAM
)
1023 r
->id
.idiag_sport
= inet_sk(sk
)->inet_sport
;
1024 r
->idiag_state
= sk
->sk_state
;
1026 if ((err
= nla_put_u8(skb
, INET_DIAG_PROTOCOL
, sk
->sk_protocol
))) {
1027 nlmsg_cancel(skb
, nlh
);
1031 handler
= inet_diag_lock_handler(sk
->sk_protocol
);
1032 if (IS_ERR(handler
)) {
1033 inet_diag_unlock_handler(handler
);
1034 nlmsg_cancel(skb
, nlh
);
1035 return PTR_ERR(handler
);
1038 attr
= handler
->idiag_info_size
1039 ? nla_reserve(skb
, INET_DIAG_INFO
, handler
->idiag_info_size
)
1042 info
= nla_data(attr
);
1044 handler
->idiag_get_info(sk
, r
, info
);
1045 inet_diag_unlock_handler(handler
);
1047 nlmsg_end(skb
, nlh
);
1051 static const struct sock_diag_handler inet_diag_handler
= {
1053 .dump
= inet_diag_handler_dump
,
1054 .get_info
= inet_diag_handler_get_info
,
1057 static const struct sock_diag_handler inet6_diag_handler
= {
1059 .dump
= inet_diag_handler_dump
,
1060 .get_info
= inet_diag_handler_get_info
,
1063 int inet_diag_register(const struct inet_diag_handler
*h
)
1065 const __u16 type
= h
->idiag_type
;
1068 if (type
>= IPPROTO_MAX
)
1071 mutex_lock(&inet_diag_table_mutex
);
1073 if (!inet_diag_table
[type
]) {
1074 inet_diag_table
[type
] = h
;
1077 mutex_unlock(&inet_diag_table_mutex
);
1081 EXPORT_SYMBOL_GPL(inet_diag_register
);
1083 void inet_diag_unregister(const struct inet_diag_handler
*h
)
1085 const __u16 type
= h
->idiag_type
;
1087 if (type
>= IPPROTO_MAX
)
1090 mutex_lock(&inet_diag_table_mutex
);
1091 inet_diag_table
[type
] = NULL
;
1092 mutex_unlock(&inet_diag_table_mutex
);
1094 EXPORT_SYMBOL_GPL(inet_diag_unregister
);
1096 static int __init
inet_diag_init(void)
1098 const int inet_diag_table_size
= (IPPROTO_MAX
*
1099 sizeof(struct inet_diag_handler
*));
1102 inet_diag_table
= kzalloc(inet_diag_table_size
, GFP_KERNEL
);
1103 if (!inet_diag_table
)
1106 err
= sock_diag_register(&inet_diag_handler
);
1110 err
= sock_diag_register(&inet6_diag_handler
);
1114 sock_diag_register_inet_compat(inet_diag_rcv_msg_compat
);
1119 sock_diag_unregister(&inet_diag_handler
);
1121 kfree(inet_diag_table
);
1125 static void __exit
inet_diag_exit(void)
1127 sock_diag_unregister(&inet6_diag_handler
);
1128 sock_diag_unregister(&inet_diag_handler
);
1129 sock_diag_unregister_inet_compat(inet_diag_rcv_msg_compat
);
1130 kfree(inet_diag_table
);
1133 module_init(inet_diag_init
);
1134 module_exit(inet_diag_exit
);
1135 MODULE_LICENSE("GPL");
1136 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2 /* AF_INET */);
1137 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 10 /* AF_INET6 */);