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;
17 laddr = list_entry(asoc->base.bind_addr.address_list.next,
18 struct sctp_sockaddr_entry, list)->a;
19 paddr = asoc->peer.primary_path->ipaddr;
20 dst = asoc->peer.primary_path->dst;
22 r->idiag_family = sk->sk_family;
23 r->id.idiag_sport = htons(asoc->base.bind_addr.port);
24 r->id.idiag_dport = htons(asoc->peer.port);
25 r->id.idiag_if = dst ? dst->dev->ifindex : 0;
26 sock_diag_save_cookie(sk, r->id.idiag_cookie);
28 #if IS_ENABLED(CONFIG_IPV6)
29 if (sk->sk_family == AF_INET6) {
30 *(struct in6_addr *)r->id.idiag_src = laddr.v6.sin6_addr;
31 *(struct in6_addr *)r->id.idiag_dst = paddr.v6.sin6_addr;
35 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
36 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
38 r->id.idiag_src[0] = laddr.v4.sin_addr.s_addr;
39 r->id.idiag_dst[0] = paddr.v4.sin_addr.s_addr;
42 r->idiag_state = asoc->state;
43 r->idiag_timer = SCTP_EVENT_TIMEOUT_T3_RTX;
44 r->idiag_retrans = asoc->rtx_data_chunks;
45 r->idiag_expires = jiffies_to_msecs(
46 asoc->timeouts[SCTP_EVENT_TIMEOUT_T3_RTX] - jiffies);
49 static int inet_diag_msg_sctpladdrs_fill(struct sk_buff *skb,
50 struct list_head *address_list)
52 struct sctp_sockaddr_entry *laddr;
53 int addrlen = sizeof(struct sockaddr_storage);
58 list_for_each_entry_rcu(laddr, address_list, list)
61 attr = nla_reserve(skb, INET_DIAG_LOCALS, addrlen * addrcnt);
65 info = nla_data(attr);
66 list_for_each_entry_rcu(laddr, address_list, list) {
67 memcpy(info, &laddr->a, addrlen);
74 static int inet_diag_msg_sctpaddrs_fill(struct sk_buff *skb,
75 struct sctp_association *asoc)
77 int addrlen = sizeof(struct sockaddr_storage);
78 struct sctp_transport *from;
82 attr = nla_reserve(skb, INET_DIAG_PEERS,
83 addrlen * asoc->peer.transport_count);
87 info = nla_data(attr);
88 list_for_each_entry(from, &asoc->peer.transport_addr_list,
90 memcpy(info, &from->ipaddr, addrlen);
97 /* sctp asoc/ep fill*/
98 static int inet_sctp_diag_fill(struct sock *sk, struct sctp_association *asoc,
100 const struct inet_diag_req_v2 *req,
101 struct user_namespace *user_ns,
102 int portid, u32 seq, u16 nlmsg_flags,
103 const struct nlmsghdr *unlh)
105 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
106 struct list_head *addr_list;
107 struct inet_diag_msg *r;
108 struct nlmsghdr *nlh;
109 int ext = req->idiag_ext;
110 struct sctp_infox infox;
113 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
119 BUG_ON(!sk_fullsock(sk));
122 inet_diag_msg_sctpasoc_fill(r, sk, asoc);
124 inet_diag_msg_common_fill(r, sk);
125 r->idiag_state = sk->sk_state;
127 r->idiag_retrans = 0;
130 if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns))
133 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) {
134 u32 mem[SK_MEMINFO_VARS];
137 if (asoc && asoc->ep->sndbuf_policy)
138 amt = asoc->sndbuf_used;
140 amt = sk_wmem_alloc_get(sk);
141 mem[SK_MEMINFO_WMEM_ALLOC] = amt;
142 if (asoc && asoc->ep->rcvbuf_policy)
143 amt = atomic_read(&asoc->rmem_alloc);
145 amt = sk_rmem_alloc_get(sk);
146 mem[SK_MEMINFO_RMEM_ALLOC] = amt;
147 mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
148 mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
149 mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
150 mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
151 mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
152 mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
153 mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops);
155 if (nla_put(skb, INET_DIAG_SKMEMINFO, sizeof(mem), &mem) < 0)
159 if (ext & (1 << (INET_DIAG_INFO - 1))) {
162 attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
163 sizeof(struct sctp_info),
168 info = nla_data(attr);
170 infox.sctpinfo = (struct sctp_info *)info;
172 sctp_diag_get_info(sk, r, &infox);
174 addr_list = asoc ? &asoc->base.bind_addr.address_list
175 : &ep->base.bind_addr.address_list;
176 if (inet_diag_msg_sctpladdrs_fill(skb, addr_list))
179 if (asoc && (ext & (1 << (INET_DIAG_CONG - 1))))
180 if (nla_put_string(skb, INET_DIAG_CONG, "reno") < 0)
183 if (asoc && inet_diag_msg_sctpaddrs_fill(skb, asoc))
190 nlmsg_cancel(skb, nlh);
194 /* callback and param */
195 struct sctp_comm_param {
197 struct netlink_callback *cb;
198 const struct inet_diag_req_v2 *r;
199 const struct nlmsghdr *nlh;
202 static size_t inet_assoc_attr_size(struct sctp_association *asoc)
204 int addrlen = sizeof(struct sockaddr_storage);
206 struct sctp_sockaddr_entry *laddr;
208 list_for_each_entry_rcu(laddr, &asoc->base.bind_addr.address_list,
212 return nla_total_size(sizeof(struct sctp_info))
213 + nla_total_size(1) /* INET_DIAG_SHUTDOWN */
214 + nla_total_size(1) /* INET_DIAG_TOS */
215 + nla_total_size(1) /* INET_DIAG_TCLASS */
216 + nla_total_size(addrlen * asoc->peer.transport_count)
217 + nla_total_size(addrlen * addrcnt)
218 + nla_total_size(sizeof(struct inet_diag_meminfo))
219 + nla_total_size(sizeof(struct inet_diag_msg))
223 static int sctp_tsp_dump_one(struct sctp_transport *tsp, void *p)
225 struct sctp_association *assoc = tsp->asoc;
226 struct sock *sk = tsp->asoc->base.sk;
227 struct sctp_comm_param *commp = p;
228 struct sk_buff *in_skb = commp->skb;
229 const struct inet_diag_req_v2 *req = commp->r;
230 const struct nlmsghdr *nlh = commp->nlh;
231 struct net *net = sock_net(in_skb->sk);
235 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
240 rep = nlmsg_new(inet_assoc_attr_size(assoc), GFP_KERNEL);
245 if (sk != assoc->base.sk) {
250 err = inet_sctp_diag_fill(sk, assoc, rep, req,
251 sk_user_ns(NETLINK_CB(in_skb).sk),
252 NETLINK_CB(in_skb).portid,
253 nlh->nlmsg_seq, 0, nlh);
256 WARN_ON(err == -EMSGSIZE);
261 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
269 static int sctp_tsp_dump(struct sctp_transport *tsp, void *p)
271 struct sctp_endpoint *ep = tsp->asoc->ep;
272 struct sctp_comm_param *commp = p;
273 struct sock *sk = ep->base.sk;
274 struct sk_buff *skb = commp->skb;
275 struct netlink_callback *cb = commp->cb;
276 const struct inet_diag_req_v2 *r = commp->r;
277 struct sctp_association *assoc =
278 list_entry(ep->asocs.next, struct sctp_association, asocs);
281 /* find the ep only once through the transports by this condition */
282 if (tsp->asoc != assoc)
285 if (r->sdiag_family != AF_UNSPEC && sk->sk_family != r->sdiag_family)
289 if (sk != assoc->base.sk)
291 list_for_each_entry(assoc, &ep->asocs, asocs) {
292 if (cb->args[4] < cb->args[1])
295 if (r->id.idiag_sport != htons(assoc->base.bind_addr.port) &&
298 if (r->id.idiag_dport != htons(assoc->peer.port) &&
303 inet_sctp_diag_fill(sk, NULL, skb, r,
304 sk_user_ns(NETLINK_CB(cb->skb).sk),
305 NETLINK_CB(cb->skb).portid,
307 NLM_F_MULTI, cb->nlh) < 0) {
314 if (inet_sctp_diag_fill(sk, assoc, skb, r,
315 sk_user_ns(NETLINK_CB(cb->skb).sk),
316 NETLINK_CB(cb->skb).portid,
317 cb->nlh->nlmsg_seq, 0, cb->nlh) < 0) {
336 static int sctp_ep_dump(struct sctp_endpoint *ep, void *p)
338 struct sctp_comm_param *commp = p;
339 struct sock *sk = ep->base.sk;
340 struct sk_buff *skb = commp->skb;
341 struct netlink_callback *cb = commp->cb;
342 const struct inet_diag_req_v2 *r = commp->r;
343 struct net *net = sock_net(skb->sk);
344 struct inet_sock *inet = inet_sk(sk);
347 if (!net_eq(sock_net(sk), net))
350 if (cb->args[4] < cb->args[1])
353 if ((r->idiag_states & ~TCPF_LISTEN) && !list_empty(&ep->asocs))
356 if (r->sdiag_family != AF_UNSPEC &&
357 sk->sk_family != r->sdiag_family)
360 if (r->id.idiag_sport != inet->inet_sport &&
364 if (r->id.idiag_dport != inet->inet_dport &&
368 if (inet_sctp_diag_fill(sk, NULL, skb, r,
369 sk_user_ns(NETLINK_CB(cb->skb).sk),
370 NETLINK_CB(cb->skb).portid,
371 cb->nlh->nlmsg_seq, NLM_F_MULTI,
382 /* define the functions for sctp_diag_handler*/
383 static void sctp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
386 struct sctp_infox *infox = (struct sctp_infox *)info;
389 r->idiag_rqueue = atomic_read(&infox->asoc->rmem_alloc);
390 r->idiag_wqueue = infox->asoc->sndbuf_used;
392 r->idiag_rqueue = sk->sk_ack_backlog;
393 r->idiag_wqueue = sk->sk_max_ack_backlog;
396 sctp_get_sctp_info(sk, infox->asoc, infox->sctpinfo);
399 static int sctp_diag_dump_one(struct sk_buff *in_skb,
400 const struct nlmsghdr *nlh,
401 const struct inet_diag_req_v2 *req)
403 struct net *net = sock_net(in_skb->sk);
404 union sctp_addr laddr, paddr;
405 struct sctp_comm_param commp = {
411 if (req->sdiag_family == AF_INET) {
412 laddr.v4.sin_port = req->id.idiag_sport;
413 laddr.v4.sin_addr.s_addr = req->id.idiag_src[0];
414 laddr.v4.sin_family = AF_INET;
416 paddr.v4.sin_port = req->id.idiag_dport;
417 paddr.v4.sin_addr.s_addr = req->id.idiag_dst[0];
418 paddr.v4.sin_family = AF_INET;
420 laddr.v6.sin6_port = req->id.idiag_sport;
421 memcpy(&laddr.v6.sin6_addr, req->id.idiag_src, 64);
422 laddr.v6.sin6_family = AF_INET6;
424 paddr.v6.sin6_port = req->id.idiag_dport;
425 memcpy(&paddr.v6.sin6_addr, req->id.idiag_dst, 64);
426 paddr.v6.sin6_family = AF_INET6;
429 return sctp_transport_lookup_process(sctp_tsp_dump_one,
430 net, &laddr, &paddr, &commp);
433 static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
434 const struct inet_diag_req_v2 *r, struct nlattr *bc)
436 u32 idiag_states = r->idiag_states;
437 struct net *net = sock_net(skb->sk);
438 struct sctp_comm_param commp = {
444 /* eps hashtable dumps
446 * 0 : if it will traversal listen sock
447 * 1 : to record the sock pos of this time's traversal
448 * 4 : to work as a temporary variable to traversal list
450 if (cb->args[0] == 0) {
451 if (!(idiag_states & TCPF_LISTEN))
453 if (sctp_for_each_endpoint(sctp_ep_dump, &commp))
461 /* asocs by transport hashtable dump
463 * 1 : to record the assoc pos of this time's traversal
464 * 2 : to record the transport pos of this time's traversal
465 * 3 : to mark if we have dumped the ep info of the current asoc
466 * 4 : to work as a temporary variable to traversal list
468 if (!(idiag_states & ~TCPF_LISTEN))
470 sctp_for_each_transport(sctp_tsp_dump, net, cb->args[2], &commp);
472 cb->args[1] = cb->args[4];
476 static const struct inet_diag_handler sctp_diag_handler = {
477 .dump = sctp_diag_dump,
478 .dump_one = sctp_diag_dump_one,
479 .idiag_get_info = sctp_diag_get_info,
480 .idiag_type = IPPROTO_SCTP,
481 .idiag_info_size = sizeof(struct sctp_info),
484 static int __init sctp_diag_init(void)
486 return inet_diag_register(&sctp_diag_handler);
489 static void __exit sctp_diag_exit(void)
491 inet_diag_unregister(&sctp_diag_handler);
494 module_init(sctp_diag_init);
495 module_exit(sctp_diag_exit);
496 MODULE_LICENSE("GPL");
497 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-132);