1 #include <linux/module.h>
2 #include <linux/inet_diag.h>
3 #include <linux/sock_diag.h>
4 #include <net/sctp/sctp.h>
6 static void sctp_diag_get_info(struct sock
*sk
, struct inet_diag_msg
*r
,
9 /* define some functions to make asoc/ep fill look clean */
10 static void inet_diag_msg_sctpasoc_fill(struct inet_diag_msg
*r
,
12 struct sctp_association
*asoc
)
14 union sctp_addr laddr
, paddr
;
15 struct dst_entry
*dst
;
16 struct timer_list
*t3_rtx
= &asoc
->peer
.primary_path
->T3_rtx_timer
;
18 laddr
= list_entry(asoc
->base
.bind_addr
.address_list
.next
,
19 struct sctp_sockaddr_entry
, list
)->a
;
20 paddr
= asoc
->peer
.primary_path
->ipaddr
;
21 dst
= asoc
->peer
.primary_path
->dst
;
23 r
->idiag_family
= sk
->sk_family
;
24 r
->id
.idiag_sport
= htons(asoc
->base
.bind_addr
.port
);
25 r
->id
.idiag_dport
= htons(asoc
->peer
.port
);
26 r
->id
.idiag_if
= dst
? dst
->dev
->ifindex
: 0;
27 sock_diag_save_cookie(sk
, r
->id
.idiag_cookie
);
29 #if IS_ENABLED(CONFIG_IPV6)
30 if (sk
->sk_family
== AF_INET6
) {
31 *(struct in6_addr
*)r
->id
.idiag_src
= laddr
.v6
.sin6_addr
;
32 *(struct in6_addr
*)r
->id
.idiag_dst
= paddr
.v6
.sin6_addr
;
36 memset(&r
->id
.idiag_src
, 0, sizeof(r
->id
.idiag_src
));
37 memset(&r
->id
.idiag_dst
, 0, sizeof(r
->id
.idiag_dst
));
39 r
->id
.idiag_src
[0] = laddr
.v4
.sin_addr
.s_addr
;
40 r
->id
.idiag_dst
[0] = paddr
.v4
.sin_addr
.s_addr
;
43 r
->idiag_state
= asoc
->state
;
44 if (timer_pending(t3_rtx
)) {
45 r
->idiag_timer
= SCTP_EVENT_TIMEOUT_T3_RTX
;
46 r
->idiag_retrans
= asoc
->rtx_data_chunks
;
47 r
->idiag_expires
= jiffies_to_msecs(t3_rtx
->expires
- jiffies
);
55 static int inet_diag_msg_sctpladdrs_fill(struct sk_buff
*skb
,
56 struct list_head
*address_list
)
58 struct sctp_sockaddr_entry
*laddr
;
59 int addrlen
= sizeof(struct sockaddr_storage
);
64 list_for_each_entry_rcu(laddr
, address_list
, list
)
67 attr
= nla_reserve(skb
, INET_DIAG_LOCALS
, addrlen
* addrcnt
);
71 info
= nla_data(attr
);
72 list_for_each_entry_rcu(laddr
, address_list
, list
) {
73 memcpy(info
, &laddr
->a
, sizeof(laddr
->a
));
74 memset(info
+ sizeof(laddr
->a
), 0, addrlen
- sizeof(laddr
->a
));
81 static int inet_diag_msg_sctpaddrs_fill(struct sk_buff
*skb
,
82 struct sctp_association
*asoc
)
84 int addrlen
= sizeof(struct sockaddr_storage
);
85 struct sctp_transport
*from
;
89 attr
= nla_reserve(skb
, INET_DIAG_PEERS
,
90 addrlen
* asoc
->peer
.transport_count
);
94 info
= nla_data(attr
);
95 list_for_each_entry(from
, &asoc
->peer
.transport_addr_list
,
97 memcpy(info
, &from
->ipaddr
, sizeof(from
->ipaddr
));
98 memset(info
+ sizeof(from
->ipaddr
), 0,
99 addrlen
- sizeof(from
->ipaddr
));
106 /* sctp asoc/ep fill*/
107 static int inet_sctp_diag_fill(struct sock
*sk
, struct sctp_association
*asoc
,
109 const struct inet_diag_req_v2
*req
,
110 struct user_namespace
*user_ns
,
111 int portid
, u32 seq
, u16 nlmsg_flags
,
112 const struct nlmsghdr
*unlh
,
115 struct sctp_endpoint
*ep
= sctp_sk(sk
)->ep
;
116 struct list_head
*addr_list
;
117 struct inet_diag_msg
*r
;
118 struct nlmsghdr
*nlh
;
119 int ext
= req
->idiag_ext
;
120 struct sctp_infox infox
;
123 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
129 BUG_ON(!sk_fullsock(sk
));
132 inet_diag_msg_sctpasoc_fill(r
, sk
, asoc
);
134 inet_diag_msg_common_fill(r
, sk
);
135 r
->idiag_state
= sk
->sk_state
;
137 r
->idiag_retrans
= 0;
140 if (inet_diag_msg_attrs_fill(sk
, skb
, r
, ext
, user_ns
, net_admin
))
143 if (ext
& (1 << (INET_DIAG_SKMEMINFO
- 1))) {
144 u32 mem
[SK_MEMINFO_VARS
];
147 if (asoc
&& asoc
->ep
->sndbuf_policy
)
148 amt
= asoc
->sndbuf_used
;
150 amt
= sk_wmem_alloc_get(sk
);
151 mem
[SK_MEMINFO_WMEM_ALLOC
] = amt
;
152 if (asoc
&& asoc
->ep
->rcvbuf_policy
)
153 amt
= atomic_read(&asoc
->rmem_alloc
);
155 amt
= sk_rmem_alloc_get(sk
);
156 mem
[SK_MEMINFO_RMEM_ALLOC
] = amt
;
157 mem
[SK_MEMINFO_RCVBUF
] = sk
->sk_rcvbuf
;
158 mem
[SK_MEMINFO_SNDBUF
] = sk
->sk_sndbuf
;
159 mem
[SK_MEMINFO_FWD_ALLOC
] = sk
->sk_forward_alloc
;
160 mem
[SK_MEMINFO_WMEM_QUEUED
] = sk
->sk_wmem_queued
;
161 mem
[SK_MEMINFO_OPTMEM
] = atomic_read(&sk
->sk_omem_alloc
);
162 mem
[SK_MEMINFO_BACKLOG
] = sk
->sk_backlog
.len
;
163 mem
[SK_MEMINFO_DROPS
] = atomic_read(&sk
->sk_drops
);
165 if (nla_put(skb
, INET_DIAG_SKMEMINFO
, sizeof(mem
), &mem
) < 0)
169 if (ext
& (1 << (INET_DIAG_INFO
- 1))) {
172 attr
= nla_reserve_64bit(skb
, INET_DIAG_INFO
,
173 sizeof(struct sctp_info
),
178 info
= nla_data(attr
);
180 infox
.sctpinfo
= (struct sctp_info
*)info
;
182 sctp_diag_get_info(sk
, r
, &infox
);
184 addr_list
= asoc
? &asoc
->base
.bind_addr
.address_list
185 : &ep
->base
.bind_addr
.address_list
;
186 if (inet_diag_msg_sctpladdrs_fill(skb
, addr_list
))
189 if (asoc
&& (ext
& (1 << (INET_DIAG_CONG
- 1))))
190 if (nla_put_string(skb
, INET_DIAG_CONG
, "reno") < 0)
193 if (asoc
&& inet_diag_msg_sctpaddrs_fill(skb
, asoc
))
200 nlmsg_cancel(skb
, nlh
);
204 /* callback and param */
205 struct sctp_comm_param
{
207 struct netlink_callback
*cb
;
208 const struct inet_diag_req_v2
*r
;
209 const struct nlmsghdr
*nlh
;
213 static size_t inet_assoc_attr_size(struct sctp_association
*asoc
)
215 int addrlen
= sizeof(struct sockaddr_storage
);
217 struct sctp_sockaddr_entry
*laddr
;
219 list_for_each_entry_rcu(laddr
, &asoc
->base
.bind_addr
.address_list
,
223 return nla_total_size(sizeof(struct sctp_info
))
224 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
225 + nla_total_size(1) /* INET_DIAG_TOS */
226 + nla_total_size(1) /* INET_DIAG_TCLASS */
227 + nla_total_size(4) /* INET_DIAG_MARK */
228 + nla_total_size(addrlen
* asoc
->peer
.transport_count
)
229 + nla_total_size(addrlen
* addrcnt
)
230 + nla_total_size(sizeof(struct inet_diag_meminfo
))
231 + nla_total_size(sizeof(struct inet_diag_msg
))
235 static int sctp_tsp_dump_one(struct sctp_transport
*tsp
, void *p
)
237 struct sctp_association
*assoc
= tsp
->asoc
;
238 struct sock
*sk
= tsp
->asoc
->base
.sk
;
239 struct sctp_comm_param
*commp
= p
;
240 struct sk_buff
*in_skb
= commp
->skb
;
241 const struct inet_diag_req_v2
*req
= commp
->r
;
242 const struct nlmsghdr
*nlh
= commp
->nlh
;
243 struct net
*net
= sock_net(in_skb
->sk
);
247 err
= sock_diag_check_cookie(sk
, req
->id
.idiag_cookie
);
252 rep
= nlmsg_new(inet_assoc_attr_size(assoc
), GFP_KERNEL
);
257 if (sk
!= assoc
->base
.sk
) {
262 err
= inet_sctp_diag_fill(sk
, assoc
, rep
, req
,
263 sk_user_ns(NETLINK_CB(in_skb
).sk
),
264 NETLINK_CB(in_skb
).portid
,
265 nlh
->nlmsg_seq
, 0, nlh
,
269 WARN_ON(err
== -EMSGSIZE
);
274 err
= netlink_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
,
282 static int sctp_sock_dump(struct sctp_transport
*tsp
, void *p
)
284 struct sctp_endpoint
*ep
= tsp
->asoc
->ep
;
285 struct sctp_comm_param
*commp
= p
;
286 struct sock
*sk
= ep
->base
.sk
;
287 struct sk_buff
*skb
= commp
->skb
;
288 struct netlink_callback
*cb
= commp
->cb
;
289 const struct inet_diag_req_v2
*r
= commp
->r
;
290 struct sctp_association
*assoc
;
294 list_for_each_entry(assoc
, &ep
->asocs
, asocs
) {
295 if (cb
->args
[4] < cb
->args
[1])
298 if (r
->id
.idiag_sport
!= htons(assoc
->base
.bind_addr
.port
) &&
301 if (r
->id
.idiag_dport
!= htons(assoc
->peer
.port
) &&
306 inet_sctp_diag_fill(sk
, NULL
, skb
, r
,
307 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
308 NETLINK_CB(cb
->skb
).portid
,
310 NLM_F_MULTI
, cb
->nlh
,
311 commp
->net_admin
) < 0) {
317 if (inet_sctp_diag_fill(sk
, assoc
, skb
, r
,
318 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
319 NETLINK_CB(cb
->skb
).portid
,
320 cb
->nlh
->nlmsg_seq
, 0, cb
->nlh
,
321 commp
->net_admin
) < 0) {
336 static int sctp_sock_filter(struct sctp_transport
*tsp
, void *p
)
338 struct sctp_endpoint
*ep
= tsp
->asoc
->ep
;
339 struct sctp_comm_param
*commp
= p
;
340 struct sock
*sk
= ep
->base
.sk
;
341 const struct inet_diag_req_v2
*r
= commp
->r
;
342 struct sctp_association
*assoc
=
343 list_entry(ep
->asocs
.next
, struct sctp_association
, asocs
);
345 /* find the ep only once through the transports by this condition */
346 if (tsp
->asoc
!= assoc
)
349 if (r
->sdiag_family
!= AF_UNSPEC
&& sk
->sk_family
!= r
->sdiag_family
)
355 static int sctp_ep_dump(struct sctp_endpoint
*ep
, void *p
)
357 struct sctp_comm_param
*commp
= p
;
358 struct sock
*sk
= ep
->base
.sk
;
359 struct sk_buff
*skb
= commp
->skb
;
360 struct netlink_callback
*cb
= commp
->cb
;
361 const struct inet_diag_req_v2
*r
= commp
->r
;
362 struct net
*net
= sock_net(skb
->sk
);
363 struct inet_sock
*inet
= inet_sk(sk
);
366 if (!net_eq(sock_net(sk
), net
))
369 if (cb
->args
[4] < cb
->args
[1])
372 if (!(r
->idiag_states
& TCPF_LISTEN
) && !list_empty(&ep
->asocs
))
375 if (r
->sdiag_family
!= AF_UNSPEC
&&
376 sk
->sk_family
!= r
->sdiag_family
)
379 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
383 if (r
->id
.idiag_dport
!= inet
->inet_dport
&&
387 if (inet_sctp_diag_fill(sk
, NULL
, skb
, r
,
388 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
389 NETLINK_CB(cb
->skb
).portid
,
390 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
391 cb
->nlh
, commp
->net_admin
) < 0) {
401 /* define the functions for sctp_diag_handler*/
402 static void sctp_diag_get_info(struct sock
*sk
, struct inet_diag_msg
*r
,
405 struct sctp_infox
*infox
= (struct sctp_infox
*)info
;
408 r
->idiag_rqueue
= atomic_read(&infox
->asoc
->rmem_alloc
);
409 r
->idiag_wqueue
= infox
->asoc
->sndbuf_used
;
411 r
->idiag_rqueue
= sk
->sk_ack_backlog
;
412 r
->idiag_wqueue
= sk
->sk_max_ack_backlog
;
415 sctp_get_sctp_info(sk
, infox
->asoc
, infox
->sctpinfo
);
418 static int sctp_diag_dump_one(struct sk_buff
*in_skb
,
419 const struct nlmsghdr
*nlh
,
420 const struct inet_diag_req_v2
*req
)
422 struct net
*net
= sock_net(in_skb
->sk
);
423 union sctp_addr laddr
, paddr
;
424 struct sctp_comm_param commp
= {
428 .net_admin
= netlink_net_capable(in_skb
, CAP_NET_ADMIN
),
431 if (req
->sdiag_family
== AF_INET
) {
432 laddr
.v4
.sin_port
= req
->id
.idiag_sport
;
433 laddr
.v4
.sin_addr
.s_addr
= req
->id
.idiag_src
[0];
434 laddr
.v4
.sin_family
= AF_INET
;
436 paddr
.v4
.sin_port
= req
->id
.idiag_dport
;
437 paddr
.v4
.sin_addr
.s_addr
= req
->id
.idiag_dst
[0];
438 paddr
.v4
.sin_family
= AF_INET
;
440 laddr
.v6
.sin6_port
= req
->id
.idiag_sport
;
441 memcpy(&laddr
.v6
.sin6_addr
, req
->id
.idiag_src
,
442 sizeof(laddr
.v6
.sin6_addr
));
443 laddr
.v6
.sin6_family
= AF_INET6
;
445 paddr
.v6
.sin6_port
= req
->id
.idiag_dport
;
446 memcpy(&paddr
.v6
.sin6_addr
, req
->id
.idiag_dst
,
447 sizeof(paddr
.v6
.sin6_addr
));
448 paddr
.v6
.sin6_family
= AF_INET6
;
451 return sctp_transport_lookup_process(sctp_tsp_dump_one
,
452 net
, &laddr
, &paddr
, &commp
);
455 static void sctp_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
456 const struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
458 u32 idiag_states
= r
->idiag_states
;
459 struct net
*net
= sock_net(skb
->sk
);
460 struct sctp_comm_param commp
= {
464 .net_admin
= netlink_net_capable(cb
->skb
, CAP_NET_ADMIN
),
466 int pos
= cb
->args
[2];
468 /* eps hashtable dumps
470 * 0 : if it will traversal listen sock
471 * 1 : to record the sock pos of this time's traversal
472 * 4 : to work as a temporary variable to traversal list
474 if (cb
->args
[0] == 0) {
475 if (!(idiag_states
& TCPF_LISTEN
))
477 if (sctp_for_each_endpoint(sctp_ep_dump
, &commp
))
485 /* asocs by transport hashtable dump
487 * 1 : to record the assoc pos of this time's traversal
488 * 2 : to record the transport pos of this time's traversal
489 * 3 : to mark if we have dumped the ep info of the current asoc
490 * 4 : to work as a temporary variable to traversal list
491 * 5 : to save the sk we get from travelsing the tsp list.
493 if (!(idiag_states
& ~(TCPF_LISTEN
| TCPF_CLOSE
)))
496 sctp_for_each_transport(sctp_sock_filter
, sctp_sock_dump
,
501 cb
->args
[1] = cb
->args
[4];
505 static const struct inet_diag_handler sctp_diag_handler
= {
506 .dump
= sctp_diag_dump
,
507 .dump_one
= sctp_diag_dump_one
,
508 .idiag_get_info
= sctp_diag_get_info
,
509 .idiag_type
= IPPROTO_SCTP
,
510 .idiag_info_size
= sizeof(struct sctp_info
),
513 static int __init
sctp_diag_init(void)
515 return inet_diag_register(&sctp_diag_handler
);
518 static void __exit
sctp_diag_exit(void)
520 inet_diag_unregister(&sctp_diag_handler
);
523 module_init(sctp_diag_init
);
524 module_exit(sctp_diag_exit
);
525 MODULE_LICENSE("GPL");
526 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2-132);