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