1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright Red Hat Inc. 2017
5 * This file is part of the SCTP kernel implementation
7 * These functions implement sctp diag support.
9 * Please send any bug reports or fixes you make to the
10 * email addresched(es):
11 * lksctp developers <linux-sctp@vger.kernel.org>
13 * Written or modified by:
14 * Xin Long <lucien.xin@gmail.com>
17 #include <linux/module.h>
18 #include <linux/inet_diag.h>
19 #include <linux/sock_diag.h>
20 #include <net/sctp/sctp.h>
22 static void sctp_diag_get_info(struct sock
*sk
, struct inet_diag_msg
*r
,
25 /* define some functions to make asoc/ep fill look clean */
26 static void inet_diag_msg_sctpasoc_fill(struct inet_diag_msg
*r
,
28 struct sctp_association
*asoc
)
30 union sctp_addr laddr
, paddr
;
31 struct dst_entry
*dst
;
32 struct timer_list
*t3_rtx
= &asoc
->peer
.primary_path
->T3_rtx_timer
;
34 laddr
= list_entry(asoc
->base
.bind_addr
.address_list
.next
,
35 struct sctp_sockaddr_entry
, list
)->a
;
36 paddr
= asoc
->peer
.primary_path
->ipaddr
;
37 dst
= asoc
->peer
.primary_path
->dst
;
39 r
->idiag_family
= sk
->sk_family
;
40 r
->id
.idiag_sport
= htons(asoc
->base
.bind_addr
.port
);
41 r
->id
.idiag_dport
= htons(asoc
->peer
.port
);
42 r
->id
.idiag_if
= dst
? dst
->dev
->ifindex
: 0;
43 sock_diag_save_cookie(sk
, r
->id
.idiag_cookie
);
45 #if IS_ENABLED(CONFIG_IPV6)
46 if (sk
->sk_family
== AF_INET6
) {
47 *(struct in6_addr
*)r
->id
.idiag_src
= laddr
.v6
.sin6_addr
;
48 *(struct in6_addr
*)r
->id
.idiag_dst
= paddr
.v6
.sin6_addr
;
52 memset(&r
->id
.idiag_src
, 0, sizeof(r
->id
.idiag_src
));
53 memset(&r
->id
.idiag_dst
, 0, sizeof(r
->id
.idiag_dst
));
55 r
->id
.idiag_src
[0] = laddr
.v4
.sin_addr
.s_addr
;
56 r
->id
.idiag_dst
[0] = paddr
.v4
.sin_addr
.s_addr
;
59 r
->idiag_state
= asoc
->state
;
60 if (timer_pending(t3_rtx
)) {
61 r
->idiag_timer
= SCTP_EVENT_TIMEOUT_T3_RTX
;
62 r
->idiag_retrans
= asoc
->rtx_data_chunks
;
63 r
->idiag_expires
= jiffies_to_msecs(t3_rtx
->expires
- jiffies
);
71 static int inet_diag_msg_sctpladdrs_fill(struct sk_buff
*skb
,
72 struct list_head
*address_list
)
74 struct sctp_sockaddr_entry
*laddr
;
75 int addrlen
= sizeof(struct sockaddr_storage
);
80 list_for_each_entry_rcu(laddr
, address_list
, list
)
83 attr
= nla_reserve(skb
, INET_DIAG_LOCALS
, addrlen
* addrcnt
);
87 info
= nla_data(attr
);
88 list_for_each_entry_rcu(laddr
, address_list
, list
) {
89 memcpy(info
, &laddr
->a
, sizeof(laddr
->a
));
90 memset(info
+ sizeof(laddr
->a
), 0, addrlen
- sizeof(laddr
->a
));
97 static int inet_diag_msg_sctpaddrs_fill(struct sk_buff
*skb
,
98 struct sctp_association
*asoc
)
100 int addrlen
= sizeof(struct sockaddr_storage
);
101 struct sctp_transport
*from
;
105 attr
= nla_reserve(skb
, INET_DIAG_PEERS
,
106 addrlen
* asoc
->peer
.transport_count
);
110 info
= nla_data(attr
);
111 list_for_each_entry(from
, &asoc
->peer
.transport_addr_list
,
113 memcpy(info
, &from
->ipaddr
, sizeof(from
->ipaddr
));
114 memset(info
+ sizeof(from
->ipaddr
), 0,
115 addrlen
- sizeof(from
->ipaddr
));
122 /* sctp asoc/ep fill*/
123 static int inet_sctp_diag_fill(struct sock
*sk
, struct sctp_association
*asoc
,
125 const struct inet_diag_req_v2
*req
,
126 struct user_namespace
*user_ns
,
127 int portid
, u32 seq
, u16 nlmsg_flags
,
128 const struct nlmsghdr
*unlh
,
131 struct sctp_endpoint
*ep
= sctp_sk(sk
)->ep
;
132 struct list_head
*addr_list
;
133 struct inet_diag_msg
*r
;
134 struct nlmsghdr
*nlh
;
135 int ext
= req
->idiag_ext
;
136 struct sctp_infox infox
;
139 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
145 BUG_ON(!sk_fullsock(sk
));
148 inet_diag_msg_sctpasoc_fill(r
, sk
, asoc
);
150 inet_diag_msg_common_fill(r
, sk
);
151 r
->idiag_state
= sk
->sk_state
;
153 r
->idiag_retrans
= 0;
156 if (inet_diag_msg_attrs_fill(sk
, skb
, r
, ext
, user_ns
, net_admin
))
159 if (ext
& (1 << (INET_DIAG_SKMEMINFO
- 1))) {
160 u32 mem
[SK_MEMINFO_VARS
];
163 if (asoc
&& asoc
->ep
->sndbuf_policy
)
164 amt
= asoc
->sndbuf_used
;
166 amt
= sk_wmem_alloc_get(sk
);
167 mem
[SK_MEMINFO_WMEM_ALLOC
] = amt
;
168 if (asoc
&& asoc
->ep
->rcvbuf_policy
)
169 amt
= atomic_read(&asoc
->rmem_alloc
);
171 amt
= sk_rmem_alloc_get(sk
);
172 mem
[SK_MEMINFO_RMEM_ALLOC
] = amt
;
173 mem
[SK_MEMINFO_RCVBUF
] = sk
->sk_rcvbuf
;
174 mem
[SK_MEMINFO_SNDBUF
] = sk
->sk_sndbuf
;
175 mem
[SK_MEMINFO_FWD_ALLOC
] = sk
->sk_forward_alloc
;
176 mem
[SK_MEMINFO_WMEM_QUEUED
] = sk
->sk_wmem_queued
;
177 mem
[SK_MEMINFO_OPTMEM
] = atomic_read(&sk
->sk_omem_alloc
);
178 mem
[SK_MEMINFO_BACKLOG
] = READ_ONCE(sk
->sk_backlog
.len
);
179 mem
[SK_MEMINFO_DROPS
] = atomic_read(&sk
->sk_drops
);
181 if (nla_put(skb
, INET_DIAG_SKMEMINFO
, sizeof(mem
), &mem
) < 0)
185 if (ext
& (1 << (INET_DIAG_INFO
- 1))) {
188 attr
= nla_reserve_64bit(skb
, INET_DIAG_INFO
,
189 sizeof(struct sctp_info
),
194 info
= nla_data(attr
);
196 infox
.sctpinfo
= (struct sctp_info
*)info
;
198 sctp_diag_get_info(sk
, r
, &infox
);
200 addr_list
= asoc
? &asoc
->base
.bind_addr
.address_list
201 : &ep
->base
.bind_addr
.address_list
;
202 if (inet_diag_msg_sctpladdrs_fill(skb
, addr_list
))
205 if (asoc
&& (ext
& (1 << (INET_DIAG_CONG
- 1))))
206 if (nla_put_string(skb
, INET_DIAG_CONG
, "reno") < 0)
209 if (asoc
&& inet_diag_msg_sctpaddrs_fill(skb
, asoc
))
216 nlmsg_cancel(skb
, nlh
);
220 /* callback and param */
221 struct sctp_comm_param
{
223 struct netlink_callback
*cb
;
224 const struct inet_diag_req_v2
*r
;
225 const struct nlmsghdr
*nlh
;
229 static size_t inet_assoc_attr_size(struct sctp_association
*asoc
)
231 int addrlen
= sizeof(struct sockaddr_storage
);
233 struct sctp_sockaddr_entry
*laddr
;
235 list_for_each_entry_rcu(laddr
, &asoc
->base
.bind_addr
.address_list
,
239 return nla_total_size(sizeof(struct sctp_info
))
240 + nla_total_size(addrlen
* asoc
->peer
.transport_count
)
241 + nla_total_size(addrlen
* addrcnt
)
242 + nla_total_size(sizeof(struct inet_diag_msg
))
243 + inet_diag_msg_attrs_size()
244 + nla_total_size(sizeof(struct inet_diag_meminfo
))
248 static int sctp_tsp_dump_one(struct sctp_transport
*tsp
, void *p
)
250 struct sctp_association
*assoc
= tsp
->asoc
;
251 struct sock
*sk
= tsp
->asoc
->base
.sk
;
252 struct sctp_comm_param
*commp
= p
;
253 struct sk_buff
*in_skb
= commp
->skb
;
254 const struct inet_diag_req_v2
*req
= commp
->r
;
255 const struct nlmsghdr
*nlh
= commp
->nlh
;
256 struct net
*net
= sock_net(in_skb
->sk
);
260 err
= sock_diag_check_cookie(sk
, req
->id
.idiag_cookie
);
265 rep
= nlmsg_new(inet_assoc_attr_size(assoc
), GFP_KERNEL
);
270 if (sk
!= assoc
->base
.sk
) {
275 err
= inet_sctp_diag_fill(sk
, assoc
, rep
, req
,
276 sk_user_ns(NETLINK_CB(in_skb
).sk
),
277 NETLINK_CB(in_skb
).portid
,
278 nlh
->nlmsg_seq
, 0, nlh
,
282 WARN_ON(err
== -EMSGSIZE
);
287 err
= netlink_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
,
295 static int sctp_sock_dump(struct sctp_transport
*tsp
, void *p
)
297 struct sctp_endpoint
*ep
= tsp
->asoc
->ep
;
298 struct sctp_comm_param
*commp
= p
;
299 struct sock
*sk
= ep
->base
.sk
;
300 struct sk_buff
*skb
= commp
->skb
;
301 struct netlink_callback
*cb
= commp
->cb
;
302 const struct inet_diag_req_v2
*r
= commp
->r
;
303 struct sctp_association
*assoc
;
307 list_for_each_entry(assoc
, &ep
->asocs
, asocs
) {
308 if (cb
->args
[4] < cb
->args
[1])
311 if (r
->id
.idiag_sport
!= htons(assoc
->base
.bind_addr
.port
) &&
314 if (r
->id
.idiag_dport
!= htons(assoc
->peer
.port
) &&
319 inet_sctp_diag_fill(sk
, NULL
, skb
, r
,
320 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
321 NETLINK_CB(cb
->skb
).portid
,
323 NLM_F_MULTI
, cb
->nlh
,
324 commp
->net_admin
) < 0) {
330 if (inet_sctp_diag_fill(sk
, assoc
, skb
, r
,
331 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
332 NETLINK_CB(cb
->skb
).portid
,
333 cb
->nlh
->nlmsg_seq
, 0, cb
->nlh
,
334 commp
->net_admin
) < 0) {
349 static int sctp_sock_filter(struct sctp_transport
*tsp
, void *p
)
351 struct sctp_endpoint
*ep
= tsp
->asoc
->ep
;
352 struct sctp_comm_param
*commp
= p
;
353 struct sock
*sk
= ep
->base
.sk
;
354 const struct inet_diag_req_v2
*r
= commp
->r
;
355 struct sctp_association
*assoc
=
356 list_entry(ep
->asocs
.next
, struct sctp_association
, asocs
);
358 /* find the ep only once through the transports by this condition */
359 if (tsp
->asoc
!= assoc
)
362 if (r
->sdiag_family
!= AF_UNSPEC
&& sk
->sk_family
!= r
->sdiag_family
)
368 static int sctp_ep_dump(struct sctp_endpoint
*ep
, void *p
)
370 struct sctp_comm_param
*commp
= p
;
371 struct sock
*sk
= ep
->base
.sk
;
372 struct sk_buff
*skb
= commp
->skb
;
373 struct netlink_callback
*cb
= commp
->cb
;
374 const struct inet_diag_req_v2
*r
= commp
->r
;
375 struct net
*net
= sock_net(skb
->sk
);
376 struct inet_sock
*inet
= inet_sk(sk
);
379 if (!net_eq(sock_net(sk
), net
))
382 if (cb
->args
[4] < cb
->args
[1])
385 if (!(r
->idiag_states
& TCPF_LISTEN
) && !list_empty(&ep
->asocs
))
388 if (r
->sdiag_family
!= AF_UNSPEC
&&
389 sk
->sk_family
!= r
->sdiag_family
)
392 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
396 if (r
->id
.idiag_dport
!= inet
->inet_dport
&&
400 if (inet_sctp_diag_fill(sk
, NULL
, skb
, r
,
401 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
402 NETLINK_CB(cb
->skb
).portid
,
403 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
404 cb
->nlh
, commp
->net_admin
) < 0) {
414 /* define the functions for sctp_diag_handler*/
415 static void sctp_diag_get_info(struct sock
*sk
, struct inet_diag_msg
*r
,
418 struct sctp_infox
*infox
= (struct sctp_infox
*)info
;
421 r
->idiag_rqueue
= atomic_read(&infox
->asoc
->rmem_alloc
);
422 r
->idiag_wqueue
= infox
->asoc
->sndbuf_used
;
424 r
->idiag_rqueue
= READ_ONCE(sk
->sk_ack_backlog
);
425 r
->idiag_wqueue
= READ_ONCE(sk
->sk_max_ack_backlog
);
428 sctp_get_sctp_info(sk
, infox
->asoc
, infox
->sctpinfo
);
431 static int sctp_diag_dump_one(struct netlink_callback
*cb
,
432 const struct inet_diag_req_v2
*req
)
434 struct sk_buff
*in_skb
= cb
->skb
;
435 struct net
*net
= sock_net(in_skb
->sk
);
436 const struct nlmsghdr
*nlh
= cb
->nlh
;
437 union sctp_addr laddr
, paddr
;
438 struct sctp_comm_param commp
= {
442 .net_admin
= netlink_net_capable(in_skb
, CAP_NET_ADMIN
),
445 if (req
->sdiag_family
== AF_INET
) {
446 laddr
.v4
.sin_port
= req
->id
.idiag_sport
;
447 laddr
.v4
.sin_addr
.s_addr
= req
->id
.idiag_src
[0];
448 laddr
.v4
.sin_family
= AF_INET
;
450 paddr
.v4
.sin_port
= req
->id
.idiag_dport
;
451 paddr
.v4
.sin_addr
.s_addr
= req
->id
.idiag_dst
[0];
452 paddr
.v4
.sin_family
= AF_INET
;
454 laddr
.v6
.sin6_port
= req
->id
.idiag_sport
;
455 memcpy(&laddr
.v6
.sin6_addr
, req
->id
.idiag_src
,
456 sizeof(laddr
.v6
.sin6_addr
));
457 laddr
.v6
.sin6_family
= AF_INET6
;
459 paddr
.v6
.sin6_port
= req
->id
.idiag_dport
;
460 memcpy(&paddr
.v6
.sin6_addr
, req
->id
.idiag_dst
,
461 sizeof(paddr
.v6
.sin6_addr
));
462 paddr
.v6
.sin6_family
= AF_INET6
;
465 return sctp_transport_lookup_process(sctp_tsp_dump_one
,
466 net
, &laddr
, &paddr
, &commp
);
469 static void sctp_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
470 const struct inet_diag_req_v2
*r
)
472 u32 idiag_states
= r
->idiag_states
;
473 struct net
*net
= sock_net(skb
->sk
);
474 struct sctp_comm_param commp
= {
478 .net_admin
= netlink_net_capable(cb
->skb
, CAP_NET_ADMIN
),
480 int pos
= cb
->args
[2];
482 /* eps hashtable dumps
484 * 0 : if it will traversal listen sock
485 * 1 : to record the sock pos of this time's traversal
486 * 4 : to work as a temporary variable to traversal list
488 if (cb
->args
[0] == 0) {
489 if (!(idiag_states
& TCPF_LISTEN
))
491 if (sctp_for_each_endpoint(sctp_ep_dump
, &commp
))
499 /* asocs by transport hashtable dump
501 * 1 : to record the assoc pos of this time's traversal
502 * 2 : to record the transport pos of this time's traversal
503 * 3 : to mark if we have dumped the ep info of the current asoc
504 * 4 : to work as a temporary variable to traversal list
505 * 5 : to save the sk we get from travelsing the tsp list.
507 if (!(idiag_states
& ~(TCPF_LISTEN
| TCPF_CLOSE
)))
510 sctp_for_each_transport(sctp_sock_filter
, sctp_sock_dump
,
515 cb
->args
[1] = cb
->args
[4];
519 static const struct inet_diag_handler sctp_diag_handler
= {
520 .dump
= sctp_diag_dump
,
521 .dump_one
= sctp_diag_dump_one
,
522 .idiag_get_info
= sctp_diag_get_info
,
523 .idiag_type
= IPPROTO_SCTP
,
524 .idiag_info_size
= sizeof(struct sctp_info
),
527 static int __init
sctp_diag_init(void)
529 return inet_diag_register(&sctp_diag_handler
);
532 static void __exit
sctp_diag_exit(void)
534 inet_diag_unregister(&sctp_diag_handler
);
537 module_init(sctp_diag_init
);
538 module_exit(sctp_diag_exit
);
539 MODULE_LICENSE("GPL");
540 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2-132);