1 #include <linux/module.h>
2 #include <linux/inet_diag.h>
3 #include <linux/sock_diag.h>
4 #include <net/sctp/sctp.h>
6 extern void inet_diag_msg_common_fill(struct inet_diag_msg
*r
,
8 extern int inet_diag_msg_attrs_fill(struct sock
*sk
, struct sk_buff
*skb
,
9 struct inet_diag_msg
*r
, int ext
,
10 struct user_namespace
*user_ns
);
12 static void sctp_diag_get_info(struct sock
*sk
, struct inet_diag_msg
*r
,
15 /* define some functions to make asoc/ep fill look clean */
16 static void inet_diag_msg_sctpasoc_fill(struct inet_diag_msg
*r
,
18 struct sctp_association
*asoc
)
20 union sctp_addr laddr
, paddr
;
21 struct dst_entry
*dst
;
23 laddr
= list_entry(asoc
->base
.bind_addr
.address_list
.next
,
24 struct sctp_sockaddr_entry
, list
)->a
;
25 paddr
= asoc
->peer
.primary_path
->ipaddr
;
26 dst
= asoc
->peer
.primary_path
->dst
;
28 r
->idiag_family
= sk
->sk_family
;
29 r
->id
.idiag_sport
= htons(asoc
->base
.bind_addr
.port
);
30 r
->id
.idiag_dport
= htons(asoc
->peer
.port
);
31 r
->id
.idiag_if
= dst
? dst
->dev
->ifindex
: 0;
32 sock_diag_save_cookie(sk
, r
->id
.idiag_cookie
);
34 #if IS_ENABLED(CONFIG_IPV6)
35 if (sk
->sk_family
== AF_INET6
) {
36 *(struct in6_addr
*)r
->id
.idiag_src
= laddr
.v6
.sin6_addr
;
37 *(struct in6_addr
*)r
->id
.idiag_dst
= paddr
.v6
.sin6_addr
;
41 memset(&r
->id
.idiag_src
, 0, sizeof(r
->id
.idiag_src
));
42 memset(&r
->id
.idiag_dst
, 0, sizeof(r
->id
.idiag_dst
));
44 r
->id
.idiag_src
[0] = laddr
.v4
.sin_addr
.s_addr
;
45 r
->id
.idiag_dst
[0] = paddr
.v4
.sin_addr
.s_addr
;
48 r
->idiag_state
= asoc
->state
;
49 r
->idiag_timer
= SCTP_EVENT_TIMEOUT_T3_RTX
;
50 r
->idiag_retrans
= asoc
->rtx_data_chunks
;
51 r
->idiag_expires
= jiffies_to_msecs(
52 asoc
->timeouts
[SCTP_EVENT_TIMEOUT_T3_RTX
] - 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
, addrlen
);
80 static int inet_diag_msg_sctpaddrs_fill(struct sk_buff
*skb
,
81 struct sctp_association
*asoc
)
83 int addrlen
= sizeof(struct sockaddr_storage
);
84 struct sctp_transport
*from
;
88 attr
= nla_reserve(skb
, INET_DIAG_PEERS
,
89 addrlen
* asoc
->peer
.transport_count
);
93 info
= nla_data(attr
);
94 list_for_each_entry(from
, &asoc
->peer
.transport_addr_list
,
96 memcpy(info
, &from
->ipaddr
, addrlen
);
103 /* sctp asoc/ep fill*/
104 static int inet_sctp_diag_fill(struct sock
*sk
, struct sctp_association
*asoc
,
106 const struct inet_diag_req_v2
*req
,
107 struct user_namespace
*user_ns
,
108 int portid
, u32 seq
, u16 nlmsg_flags
,
109 const struct nlmsghdr
*unlh
)
111 struct sctp_endpoint
*ep
= sctp_sk(sk
)->ep
;
112 struct list_head
*addr_list
;
113 struct inet_diag_msg
*r
;
114 struct nlmsghdr
*nlh
;
115 int ext
= req
->idiag_ext
;
116 struct sctp_infox infox
;
119 nlh
= nlmsg_put(skb
, portid
, seq
, unlh
->nlmsg_type
, sizeof(*r
),
125 BUG_ON(!sk_fullsock(sk
));
128 inet_diag_msg_sctpasoc_fill(r
, sk
, asoc
);
130 inet_diag_msg_common_fill(r
, sk
);
131 r
->idiag_state
= sk
->sk_state
;
133 r
->idiag_retrans
= 0;
136 if (inet_diag_msg_attrs_fill(sk
, skb
, r
, ext
, user_ns
))
139 if (ext
& (1 << (INET_DIAG_SKMEMINFO
- 1))) {
140 u32 mem
[SK_MEMINFO_VARS
];
143 if (asoc
&& asoc
->ep
->sndbuf_policy
)
144 amt
= asoc
->sndbuf_used
;
146 amt
= sk_wmem_alloc_get(sk
);
147 mem
[SK_MEMINFO_WMEM_ALLOC
] = amt
;
148 if (asoc
&& asoc
->ep
->rcvbuf_policy
)
149 amt
= atomic_read(&asoc
->rmem_alloc
);
151 amt
= sk_rmem_alloc_get(sk
);
152 mem
[SK_MEMINFO_RMEM_ALLOC
] = amt
;
153 mem
[SK_MEMINFO_RCVBUF
] = sk
->sk_rcvbuf
;
154 mem
[SK_MEMINFO_SNDBUF
] = sk
->sk_sndbuf
;
155 mem
[SK_MEMINFO_FWD_ALLOC
] = sk
->sk_forward_alloc
;
156 mem
[SK_MEMINFO_WMEM_QUEUED
] = sk
->sk_wmem_queued
;
157 mem
[SK_MEMINFO_OPTMEM
] = atomic_read(&sk
->sk_omem_alloc
);
158 mem
[SK_MEMINFO_BACKLOG
] = sk
->sk_backlog
.len
;
159 mem
[SK_MEMINFO_DROPS
] = atomic_read(&sk
->sk_drops
);
161 if (nla_put(skb
, INET_DIAG_SKMEMINFO
, sizeof(mem
), &mem
) < 0)
165 if (ext
& (1 << (INET_DIAG_INFO
- 1))) {
168 attr
= nla_reserve_64bit(skb
, INET_DIAG_INFO
,
169 sizeof(struct sctp_info
),
174 info
= nla_data(attr
);
176 infox
.sctpinfo
= (struct sctp_info
*)info
;
178 sctp_diag_get_info(sk
, r
, &infox
);
180 addr_list
= asoc
? &asoc
->base
.bind_addr
.address_list
181 : &ep
->base
.bind_addr
.address_list
;
182 if (inet_diag_msg_sctpladdrs_fill(skb
, addr_list
))
185 if (asoc
&& (ext
& (1 << (INET_DIAG_CONG
- 1))))
186 if (nla_put_string(skb
, INET_DIAG_CONG
, "reno") < 0)
189 if (asoc
&& inet_diag_msg_sctpaddrs_fill(skb
, asoc
))
196 nlmsg_cancel(skb
, nlh
);
200 /* callback and param */
201 struct sctp_comm_param
{
203 struct netlink_callback
*cb
;
204 const struct inet_diag_req_v2
*r
;
205 const struct nlmsghdr
*nlh
;
208 static size_t inet_assoc_attr_size(struct sctp_association
*asoc
)
210 int addrlen
= sizeof(struct sockaddr_storage
);
212 struct sctp_sockaddr_entry
*laddr
;
214 list_for_each_entry_rcu(laddr
, &asoc
->base
.bind_addr
.address_list
,
218 return nla_total_size(sizeof(struct sctp_info
))
219 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
220 + nla_total_size(1) /* INET_DIAG_TOS */
221 + nla_total_size(1) /* INET_DIAG_TCLASS */
222 + nla_total_size(addrlen
* asoc
->peer
.transport_count
)
223 + nla_total_size(addrlen
* addrcnt
)
224 + nla_total_size(sizeof(struct inet_diag_meminfo
))
225 + nla_total_size(sizeof(struct inet_diag_msg
))
229 static int sctp_tsp_dump_one(struct sctp_transport
*tsp
, void *p
)
231 struct sctp_association
*assoc
= tsp
->asoc
;
232 struct sock
*sk
= tsp
->asoc
->base
.sk
;
233 struct sctp_comm_param
*commp
= p
;
234 struct sk_buff
*in_skb
= commp
->skb
;
235 const struct inet_diag_req_v2
*req
= commp
->r
;
236 const struct nlmsghdr
*nlh
= commp
->nlh
;
237 struct net
*net
= sock_net(in_skb
->sk
);
241 err
= sock_diag_check_cookie(sk
, req
->id
.idiag_cookie
);
246 rep
= nlmsg_new(inet_assoc_attr_size(assoc
), GFP_KERNEL
);
251 if (sk
!= assoc
->base
.sk
) {
256 err
= inet_sctp_diag_fill(sk
, assoc
, rep
, req
,
257 sk_user_ns(NETLINK_CB(in_skb
).sk
),
258 NETLINK_CB(in_skb
).portid
,
259 nlh
->nlmsg_seq
, 0, nlh
);
262 WARN_ON(err
== -EMSGSIZE
);
267 err
= netlink_unicast(net
->diag_nlsk
, rep
, NETLINK_CB(in_skb
).portid
,
275 static int sctp_tsp_dump(struct sctp_transport
*tsp
, void *p
)
277 struct sctp_endpoint
*ep
= tsp
->asoc
->ep
;
278 struct sctp_comm_param
*commp
= p
;
279 struct sock
*sk
= ep
->base
.sk
;
280 struct sk_buff
*skb
= commp
->skb
;
281 struct netlink_callback
*cb
= commp
->cb
;
282 const struct inet_diag_req_v2
*r
= commp
->r
;
283 struct sctp_association
*assoc
=
284 list_entry(ep
->asocs
.next
, struct sctp_association
, asocs
);
287 /* find the ep only once through the transports by this condition */
288 if (tsp
->asoc
!= assoc
)
291 if (r
->sdiag_family
!= AF_UNSPEC
&& sk
->sk_family
!= r
->sdiag_family
)
295 if (sk
!= assoc
->base
.sk
)
297 list_for_each_entry(assoc
, &ep
->asocs
, asocs
) {
298 if (cb
->args
[4] < cb
->args
[1])
301 if (r
->id
.idiag_sport
!= htons(assoc
->base
.bind_addr
.port
) &&
304 if (r
->id
.idiag_dport
!= htons(assoc
->peer
.port
) &&
309 inet_sctp_diag_fill(sk
, NULL
, skb
, r
,
310 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
311 NETLINK_CB(cb
->skb
).portid
,
313 NLM_F_MULTI
, cb
->nlh
) < 0) {
320 if (inet_sctp_diag_fill(sk
, assoc
, skb
, r
,
321 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
322 NETLINK_CB(cb
->skb
).portid
,
323 cb
->nlh
->nlmsg_seq
, 0, cb
->nlh
) < 0) {
342 static int sctp_ep_dump(struct sctp_endpoint
*ep
, void *p
)
344 struct sctp_comm_param
*commp
= p
;
345 struct sock
*sk
= ep
->base
.sk
;
346 struct sk_buff
*skb
= commp
->skb
;
347 struct netlink_callback
*cb
= commp
->cb
;
348 const struct inet_diag_req_v2
*r
= commp
->r
;
349 struct net
*net
= sock_net(skb
->sk
);
350 struct inet_sock
*inet
= inet_sk(sk
);
353 if (!net_eq(sock_net(sk
), net
))
356 if (cb
->args
[4] < cb
->args
[1])
359 if (r
->sdiag_family
!= AF_UNSPEC
&&
360 sk
->sk_family
!= r
->sdiag_family
)
363 if (r
->id
.idiag_sport
!= inet
->inet_sport
&&
367 if (r
->id
.idiag_dport
!= inet
->inet_dport
&&
371 if (inet_sctp_diag_fill(sk
, NULL
, skb
, r
,
372 sk_user_ns(NETLINK_CB(cb
->skb
).sk
),
373 NETLINK_CB(cb
->skb
).portid
,
374 cb
->nlh
->nlmsg_seq
, NLM_F_MULTI
,
385 /* define the functions for sctp_diag_handler*/
386 static void sctp_diag_get_info(struct sock
*sk
, struct inet_diag_msg
*r
,
389 struct sctp_infox
*infox
= (struct sctp_infox
*)info
;
392 r
->idiag_rqueue
= atomic_read(&infox
->asoc
->rmem_alloc
);
393 r
->idiag_wqueue
= infox
->asoc
->sndbuf_used
;
395 r
->idiag_rqueue
= sk
->sk_ack_backlog
;
396 r
->idiag_wqueue
= sk
->sk_max_ack_backlog
;
399 sctp_get_sctp_info(sk
, infox
->asoc
, infox
->sctpinfo
);
402 static int sctp_diag_dump_one(struct sk_buff
*in_skb
,
403 const struct nlmsghdr
*nlh
,
404 const struct inet_diag_req_v2
*req
)
406 struct net
*net
= sock_net(in_skb
->sk
);
407 union sctp_addr laddr
, paddr
;
408 struct sctp_comm_param commp
= {
414 if (req
->sdiag_family
== AF_INET
) {
415 laddr
.v4
.sin_port
= req
->id
.idiag_sport
;
416 laddr
.v4
.sin_addr
.s_addr
= req
->id
.idiag_src
[0];
417 laddr
.v4
.sin_family
= AF_INET
;
419 paddr
.v4
.sin_port
= req
->id
.idiag_dport
;
420 paddr
.v4
.sin_addr
.s_addr
= req
->id
.idiag_dst
[0];
421 paddr
.v4
.sin_family
= AF_INET
;
423 laddr
.v6
.sin6_port
= req
->id
.idiag_sport
;
424 memcpy(&laddr
.v6
.sin6_addr
, req
->id
.idiag_src
, 64);
425 laddr
.v6
.sin6_family
= AF_INET6
;
427 paddr
.v6
.sin6_port
= req
->id
.idiag_dport
;
428 memcpy(&paddr
.v6
.sin6_addr
, req
->id
.idiag_dst
, 64);
429 paddr
.v6
.sin6_family
= AF_INET6
;
432 return sctp_transport_lookup_process(sctp_tsp_dump_one
,
433 net
, &laddr
, &paddr
, &commp
);
436 static void sctp_diag_dump(struct sk_buff
*skb
, struct netlink_callback
*cb
,
437 const struct inet_diag_req_v2
*r
, struct nlattr
*bc
)
439 u32 idiag_states
= r
->idiag_states
;
440 struct net
*net
= sock_net(skb
->sk
);
441 struct sctp_comm_param commp
= {
447 /* eps hashtable dumps
449 * 0 : if it will traversal listen sock
450 * 1 : to record the sock pos of this time's traversal
451 * 4 : to work as a temporary variable to traversal list
453 if (cb
->args
[0] == 0) {
454 if (!(idiag_states
& TCPF_LISTEN
))
456 if (sctp_for_each_endpoint(sctp_ep_dump
, &commp
))
464 /* asocs by transport hashtable dump
466 * 1 : to record the assoc pos of this time's traversal
467 * 2 : to record the transport pos of this time's traversal
468 * 3 : to mark if we have dumped the ep info of the current asoc
469 * 4 : to work as a temporary variable to traversal list
471 if (!(idiag_states
& ~TCPF_LISTEN
))
473 sctp_for_each_transport(sctp_tsp_dump
, net
, cb
->args
[2], &commp
);
475 cb
->args
[1] = cb
->args
[4];
479 static const struct inet_diag_handler sctp_diag_handler
= {
480 .dump
= sctp_diag_dump
,
481 .dump_one
= sctp_diag_dump_one
,
482 .idiag_get_info
= sctp_diag_get_info
,
483 .idiag_type
= IPPROTO_SCTP
,
484 .idiag_info_size
= sizeof(struct sctp_info
),
487 static int __init
sctp_diag_init(void)
489 return inet_diag_register(&sctp_diag_handler
);
492 static void __exit
sctp_diag_exit(void)
494 inet_diag_unregister(&sctp_diag_handler
);
497 module_init(sctp_diag_init
);
498 module_exit(sctp_diag_exit
);
499 MODULE_LICENSE("GPL");
500 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK
, NETLINK_SOCK_DIAG
, 2-132);