1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright Red Hat Inc. 2017
4 *
5 * This file is part of the SCTP kernel implementation
6 *
7 * These functions implement sctp diag support.
8 *
9 * Please send any bug reports or fixes you make to the
10 * email addresched(es):
11 * lksctp developers <linux-sctp@vger.kernel.org>
12 *
13 * Written or modified by:
14 * Xin Long <lucien.xin@gmail.com>
15 */
16
17 #include <linux/module.h>
18 #include <linux/inet_diag.h>
19 #include <linux/sock_diag.h>
20 #include <net/sctp/sctp.h>
21
22 static void sctp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
23 void *info);
24
25 /* define some functions to make asoc/ep fill look clean */
inet_diag_msg_sctpasoc_fill(struct inet_diag_msg * r,struct sock * sk,struct sctp_association * asoc)26 static void inet_diag_msg_sctpasoc_fill(struct inet_diag_msg *r,
27 struct sock *sk,
28 struct sctp_association *asoc)
29 {
30 union sctp_addr laddr, paddr;
31 struct dst_entry *dst;
32 struct timer_list *t3_rtx = &asoc->peer.primary_path->T3_rtx_timer;
33
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;
38
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);
44
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;
49 } else
50 #endif
51 {
52 memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
53 memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
54
55 r->id.idiag_src[0] = laddr.v4.sin_addr.s_addr;
56 r->id.idiag_dst[0] = paddr.v4.sin_addr.s_addr;
57 }
58
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);
64 }
65 }
66
inet_diag_msg_sctpladdrs_fill(struct sk_buff * skb,struct list_head * address_list)67 static int inet_diag_msg_sctpladdrs_fill(struct sk_buff *skb,
68 struct list_head *address_list)
69 {
70 struct sctp_sockaddr_entry *laddr;
71 int addrlen = sizeof(struct sockaddr_storage);
72 int addrcnt = 0;
73 struct nlattr *attr;
74 void *info = NULL;
75
76 list_for_each_entry_rcu(laddr, address_list, list)
77 addrcnt++;
78
79 attr = nla_reserve(skb, INET_DIAG_LOCALS, addrlen * addrcnt);
80 if (!attr)
81 return -EMSGSIZE;
82
83 info = nla_data(attr);
84 list_for_each_entry_rcu(laddr, address_list, list) {
85 memcpy(info, &laddr->a, sizeof(laddr->a));
86 memset(info + sizeof(laddr->a), 0, addrlen - sizeof(laddr->a));
87 info += addrlen;
88 }
89
90 return 0;
91 }
92
inet_diag_msg_sctpaddrs_fill(struct sk_buff * skb,struct sctp_association * asoc)93 static int inet_diag_msg_sctpaddrs_fill(struct sk_buff *skb,
94 struct sctp_association *asoc)
95 {
96 int addrlen = sizeof(struct sockaddr_storage);
97 struct sctp_transport *from;
98 struct nlattr *attr;
99 void *info = NULL;
100
101 attr = nla_reserve(skb, INET_DIAG_PEERS,
102 addrlen * asoc->peer.transport_count);
103 if (!attr)
104 return -EMSGSIZE;
105
106 info = nla_data(attr);
107 list_for_each_entry(from, &asoc->peer.transport_addr_list,
108 transports) {
109 memcpy(info, &from->ipaddr, sizeof(from->ipaddr));
110 memset(info + sizeof(from->ipaddr), 0,
111 addrlen - sizeof(from->ipaddr));
112 info += addrlen;
113 }
114
115 return 0;
116 }
117
118 /* sctp asoc/ep fill*/
inet_sctp_diag_fill(struct sock * sk,struct sctp_association * asoc,struct sk_buff * skb,const struct inet_diag_req_v2 * req,struct user_namespace * user_ns,int portid,u32 seq,u16 nlmsg_flags,const struct nlmsghdr * unlh,bool net_admin)119 static int inet_sctp_diag_fill(struct sock *sk, struct sctp_association *asoc,
120 struct sk_buff *skb,
121 const struct inet_diag_req_v2 *req,
122 struct user_namespace *user_ns,
123 int portid, u32 seq, u16 nlmsg_flags,
124 const struct nlmsghdr *unlh,
125 bool net_admin)
126 {
127 struct sctp_endpoint *ep = sctp_sk(sk)->ep;
128 struct list_head *addr_list;
129 struct inet_diag_msg *r;
130 struct nlmsghdr *nlh;
131 int ext = req->idiag_ext;
132 struct sctp_infox infox;
133 void *info = NULL;
134
135 nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
136 nlmsg_flags);
137 if (!nlh)
138 return -EMSGSIZE;
139
140 r = nlmsg_data(nlh);
141 BUG_ON(!sk_fullsock(sk));
142
143 r->idiag_timer = 0;
144 r->idiag_retrans = 0;
145 r->idiag_expires = 0;
146 if (asoc) {
147 inet_diag_msg_sctpasoc_fill(r, sk, asoc);
148 } else {
149 inet_diag_msg_common_fill(r, sk);
150 r->idiag_state = sk->sk_state;
151 }
152
153 if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin))
154 goto errout;
155
156 if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) {
157 u32 mem[SK_MEMINFO_VARS];
158 int amt;
159
160 if (asoc && asoc->ep->sndbuf_policy)
161 amt = asoc->sndbuf_used;
162 else
163 amt = sk_wmem_alloc_get(sk);
164 mem[SK_MEMINFO_WMEM_ALLOC] = amt;
165 if (asoc && asoc->ep->rcvbuf_policy)
166 amt = atomic_read(&asoc->rmem_alloc);
167 else
168 amt = sk_rmem_alloc_get(sk);
169 mem[SK_MEMINFO_RMEM_ALLOC] = amt;
170 mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
171 mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
172 mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
173 mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
174 mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
175 mem[SK_MEMINFO_BACKLOG] = READ_ONCE(sk->sk_backlog.len);
176 mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops);
177
178 if (nla_put(skb, INET_DIAG_SKMEMINFO, sizeof(mem), &mem) < 0)
179 goto errout;
180 }
181
182 if (ext & (1 << (INET_DIAG_INFO - 1))) {
183 struct nlattr *attr;
184
185 attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
186 sizeof(struct sctp_info),
187 INET_DIAG_PAD);
188 if (!attr)
189 goto errout;
190
191 info = nla_data(attr);
192 }
193 infox.sctpinfo = (struct sctp_info *)info;
194 infox.asoc = asoc;
195 sctp_diag_get_info(sk, r, &infox);
196
197 addr_list = asoc ? &asoc->base.bind_addr.address_list
198 : &ep->base.bind_addr.address_list;
199 if (inet_diag_msg_sctpladdrs_fill(skb, addr_list))
200 goto errout;
201
202 if (asoc && (ext & (1 << (INET_DIAG_CONG - 1))))
203 if (nla_put_string(skb, INET_DIAG_CONG, "reno") < 0)
204 goto errout;
205
206 if (asoc && inet_diag_msg_sctpaddrs_fill(skb, asoc))
207 goto errout;
208
209 nlmsg_end(skb, nlh);
210 return 0;
211
212 errout:
213 nlmsg_cancel(skb, nlh);
214 return -EMSGSIZE;
215 }
216
217 /* callback and param */
218 struct sctp_comm_param {
219 struct sk_buff *skb;
220 struct netlink_callback *cb;
221 const struct inet_diag_req_v2 *r;
222 const struct nlmsghdr *nlh;
223 bool net_admin;
224 };
225
inet_assoc_attr_size(struct sctp_association * asoc)226 static size_t inet_assoc_attr_size(struct sctp_association *asoc)
227 {
228 int addrlen = sizeof(struct sockaddr_storage);
229 int addrcnt = 0;
230 struct sctp_sockaddr_entry *laddr;
231
232 list_for_each_entry_rcu(laddr, &asoc->base.bind_addr.address_list,
233 list)
234 addrcnt++;
235
236 return nla_total_size(sizeof(struct sctp_info))
237 + nla_total_size(addrlen * asoc->peer.transport_count)
238 + nla_total_size(addrlen * addrcnt)
239 + nla_total_size(sizeof(struct inet_diag_msg))
240 + inet_diag_msg_attrs_size()
241 + nla_total_size(sizeof(struct inet_diag_meminfo))
242 + 64;
243 }
244
sctp_tsp_dump_one(struct sctp_transport * tsp,void * p)245 static int sctp_tsp_dump_one(struct sctp_transport *tsp, void *p)
246 {
247 struct sctp_association *assoc = tsp->asoc;
248 struct sock *sk = tsp->asoc->base.sk;
249 struct sctp_comm_param *commp = p;
250 struct sk_buff *in_skb = commp->skb;
251 const struct inet_diag_req_v2 *req = commp->r;
252 const struct nlmsghdr *nlh = commp->nlh;
253 struct net *net = sock_net(in_skb->sk);
254 struct sk_buff *rep;
255 int err;
256
257 err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
258 if (err)
259 goto out;
260
261 err = -ENOMEM;
262 rep = nlmsg_new(inet_assoc_attr_size(assoc), GFP_KERNEL);
263 if (!rep)
264 goto out;
265
266 lock_sock(sk);
267 if (sk != assoc->base.sk) {
268 release_sock(sk);
269 sk = assoc->base.sk;
270 lock_sock(sk);
271 }
272 err = inet_sctp_diag_fill(sk, assoc, rep, req,
273 sk_user_ns(NETLINK_CB(in_skb).sk),
274 NETLINK_CB(in_skb).portid,
275 nlh->nlmsg_seq, 0, nlh,
276 commp->net_admin);
277 release_sock(sk);
278 if (err < 0) {
279 WARN_ON(err == -EMSGSIZE);
280 kfree_skb(rep);
281 goto out;
282 }
283
284 err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
285 MSG_DONTWAIT);
286 if (err > 0)
287 err = 0;
288 out:
289 return err;
290 }
291
sctp_sock_dump(struct sctp_endpoint * ep,struct sctp_transport * tsp,void * p)292 static int sctp_sock_dump(struct sctp_endpoint *ep, struct sctp_transport *tsp, void *p)
293 {
294 struct sctp_comm_param *commp = p;
295 struct sock *sk = ep->base.sk;
296 struct sk_buff *skb = commp->skb;
297 struct netlink_callback *cb = commp->cb;
298 const struct inet_diag_req_v2 *r = commp->r;
299 struct sctp_association *assoc;
300 int err = 0;
301
302 lock_sock(sk);
303 if (ep != tsp->asoc->ep)
304 goto release;
305 list_for_each_entry(assoc, &ep->asocs, asocs) {
306 if (cb->args[4] < cb->args[1])
307 goto next;
308
309 if (r->id.idiag_sport != htons(assoc->base.bind_addr.port) &&
310 r->id.idiag_sport)
311 goto next;
312 if (r->id.idiag_dport != htons(assoc->peer.port) &&
313 r->id.idiag_dport)
314 goto next;
315
316 if (!cb->args[3] &&
317 inet_sctp_diag_fill(sk, NULL, skb, r,
318 sk_user_ns(NETLINK_CB(cb->skb).sk),
319 NETLINK_CB(cb->skb).portid,
320 cb->nlh->nlmsg_seq,
321 NLM_F_MULTI, cb->nlh,
322 commp->net_admin) < 0) {
323 err = 1;
324 goto release;
325 }
326 cb->args[3] = 1;
327
328 if (inet_sctp_diag_fill(sk, assoc, skb, r,
329 sk_user_ns(NETLINK_CB(cb->skb).sk),
330 NETLINK_CB(cb->skb).portid,
331 cb->nlh->nlmsg_seq, 0, cb->nlh,
332 commp->net_admin) < 0) {
333 err = 1;
334 goto release;
335 }
336 next:
337 cb->args[4]++;
338 }
339 cb->args[1] = 0;
340 cb->args[3] = 0;
341 cb->args[4] = 0;
342 release:
343 release_sock(sk);
344 return err;
345 }
346
sctp_sock_filter(struct sctp_endpoint * ep,struct sctp_transport * tsp,void * p)347 static int sctp_sock_filter(struct sctp_endpoint *ep, struct sctp_transport *tsp, void *p)
348 {
349 struct sctp_comm_param *commp = p;
350 struct sock *sk = ep->base.sk;
351 const struct inet_diag_req_v2 *r = commp->r;
352
353 /* find the ep only once through the transports by this condition */
354 if (!list_is_first(&tsp->asoc->asocs, &ep->asocs))
355 return 0;
356
357 if (r->sdiag_family != AF_UNSPEC && sk->sk_family != r->sdiag_family)
358 return 0;
359
360 return 1;
361 }
362
sctp_ep_dump(struct sctp_endpoint * ep,void * p)363 static int sctp_ep_dump(struct sctp_endpoint *ep, void *p)
364 {
365 struct sctp_comm_param *commp = p;
366 struct sock *sk = ep->base.sk;
367 struct sk_buff *skb = commp->skb;
368 struct netlink_callback *cb = commp->cb;
369 const struct inet_diag_req_v2 *r = commp->r;
370 struct net *net = sock_net(skb->sk);
371 struct inet_sock *inet = inet_sk(sk);
372 int err = 0;
373
374 if (!net_eq(sock_net(sk), net))
375 goto out;
376
377 if (cb->args[4] < cb->args[1])
378 goto next;
379
380 if (!(r->idiag_states & TCPF_LISTEN) && !list_empty(&ep->asocs))
381 goto next;
382
383 if (r->sdiag_family != AF_UNSPEC &&
384 sk->sk_family != r->sdiag_family)
385 goto next;
386
387 if (r->id.idiag_sport != inet->inet_sport &&
388 r->id.idiag_sport)
389 goto next;
390
391 if (r->id.idiag_dport != inet->inet_dport &&
392 r->id.idiag_dport)
393 goto next;
394
395 if (inet_sctp_diag_fill(sk, NULL, skb, r,
396 sk_user_ns(NETLINK_CB(cb->skb).sk),
397 NETLINK_CB(cb->skb).portid,
398 cb->nlh->nlmsg_seq, NLM_F_MULTI,
399 cb->nlh, commp->net_admin) < 0) {
400 err = 2;
401 goto out;
402 }
403 next:
404 cb->args[4]++;
405 out:
406 return err;
407 }
408
409 /* define the functions for sctp_diag_handler*/
sctp_diag_get_info(struct sock * sk,struct inet_diag_msg * r,void * info)410 static void sctp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
411 void *info)
412 {
413 struct sctp_infox *infox = (struct sctp_infox *)info;
414
415 if (infox->asoc) {
416 r->idiag_rqueue = atomic_read(&infox->asoc->rmem_alloc);
417 r->idiag_wqueue = infox->asoc->sndbuf_used;
418 } else {
419 r->idiag_rqueue = sk->sk_ack_backlog;
420 r->idiag_wqueue = sk->sk_max_ack_backlog;
421 }
422 if (infox->sctpinfo)
423 sctp_get_sctp_info(sk, infox->asoc, infox->sctpinfo);
424 }
425
sctp_diag_dump_one(struct sk_buff * in_skb,const struct nlmsghdr * nlh,const struct inet_diag_req_v2 * req)426 static int sctp_diag_dump_one(struct sk_buff *in_skb,
427 const struct nlmsghdr *nlh,
428 const struct inet_diag_req_v2 *req)
429 {
430 struct net *net = sock_net(in_skb->sk);
431 union sctp_addr laddr, paddr;
432 struct sctp_comm_param commp = {
433 .skb = in_skb,
434 .r = req,
435 .nlh = nlh,
436 .net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN),
437 };
438
439 if (req->sdiag_family == AF_INET) {
440 laddr.v4.sin_port = req->id.idiag_sport;
441 laddr.v4.sin_addr.s_addr = req->id.idiag_src[0];
442 laddr.v4.sin_family = AF_INET;
443
444 paddr.v4.sin_port = req->id.idiag_dport;
445 paddr.v4.sin_addr.s_addr = req->id.idiag_dst[0];
446 paddr.v4.sin_family = AF_INET;
447 } else {
448 laddr.v6.sin6_port = req->id.idiag_sport;
449 memcpy(&laddr.v6.sin6_addr, req->id.idiag_src,
450 sizeof(laddr.v6.sin6_addr));
451 laddr.v6.sin6_family = AF_INET6;
452
453 paddr.v6.sin6_port = req->id.idiag_dport;
454 memcpy(&paddr.v6.sin6_addr, req->id.idiag_dst,
455 sizeof(paddr.v6.sin6_addr));
456 paddr.v6.sin6_family = AF_INET6;
457 }
458
459 return sctp_transport_lookup_process(sctp_tsp_dump_one,
460 net, &laddr, &paddr, &commp);
461 }
462
sctp_diag_dump(struct sk_buff * skb,struct netlink_callback * cb,const struct inet_diag_req_v2 * r,struct nlattr * bc)463 static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
464 const struct inet_diag_req_v2 *r, struct nlattr *bc)
465 {
466 u32 idiag_states = r->idiag_states;
467 struct net *net = sock_net(skb->sk);
468 struct sctp_comm_param commp = {
469 .skb = skb,
470 .cb = cb,
471 .r = r,
472 .net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN),
473 };
474 int pos = cb->args[2];
475
476 /* eps hashtable dumps
477 * args:
478 * 0 : if it will traversal listen sock
479 * 1 : to record the sock pos of this time's traversal
480 * 4 : to work as a temporary variable to traversal list
481 */
482 if (cb->args[0] == 0) {
483 if (!(idiag_states & TCPF_LISTEN))
484 goto skip;
485 if (sctp_for_each_endpoint(sctp_ep_dump, &commp))
486 goto done;
487 skip:
488 cb->args[0] = 1;
489 cb->args[1] = 0;
490 cb->args[4] = 0;
491 }
492
493 /* asocs by transport hashtable dump
494 * args:
495 * 1 : to record the assoc pos of this time's traversal
496 * 2 : to record the transport pos of this time's traversal
497 * 3 : to mark if we have dumped the ep info of the current asoc
498 * 4 : to work as a temporary variable to traversal list
499 * 5 : to save the sk we get from travelsing the tsp list.
500 */
501 if (!(idiag_states & ~(TCPF_LISTEN | TCPF_CLOSE)))
502 goto done;
503
504 sctp_transport_traverse_process(sctp_sock_filter, sctp_sock_dump,
505 net, &pos, &commp);
506 cb->args[2] = pos;
507
508 done:
509 cb->args[1] = cb->args[4];
510 cb->args[4] = 0;
511 }
512
513 static const struct inet_diag_handler sctp_diag_handler = {
514 .dump = sctp_diag_dump,
515 .dump_one = sctp_diag_dump_one,
516 .idiag_get_info = sctp_diag_get_info,
517 .idiag_type = IPPROTO_SCTP,
518 .idiag_info_size = sizeof(struct sctp_info),
519 };
520
sctp_diag_init(void)521 static int __init sctp_diag_init(void)
522 {
523 return inet_diag_register(&sctp_diag_handler);
524 }
525
sctp_diag_exit(void)526 static void __exit sctp_diag_exit(void)
527 {
528 inet_diag_unregister(&sctp_diag_handler);
529 }
530
531 module_init(sctp_diag_init);
532 module_exit(sctp_diag_exit);
533 MODULE_LICENSE("GPL");
534 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-132);
535