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