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