1 | #include <machine/rtems-bsd-user-space.h> |
---|
2 | |
---|
3 | /*- |
---|
4 | * Copyright (c) 2001-2007, by Weongyo Jeong. All rights reserved. |
---|
5 | * Copyright (c) 2011, by Michael Tuexen. All rights reserved. |
---|
6 | * |
---|
7 | * Redistribution and use in source and binary forms, with or without |
---|
8 | * modification, are permitted provided that the following conditions are met: |
---|
9 | * |
---|
10 | * a) Redistributions of source code must retain the above copyright notice, |
---|
11 | * this list of conditions and the following disclaimer. |
---|
12 | * |
---|
13 | * b) Redistributions in binary form must reproduce the above copyright |
---|
14 | * notice, this list of conditions and the following disclaimer in |
---|
15 | * the documentation and/or other materials provided with the distribution. |
---|
16 | * |
---|
17 | * c) Neither the name of Cisco Systems, Inc. nor the names of its |
---|
18 | * contributors may be used to endorse or promote products derived |
---|
19 | * from this software without specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
---|
23 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
24 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
25 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
29 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
30 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
---|
31 | * THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | */ |
---|
33 | |
---|
34 | #if 0 |
---|
35 | #ifndef lint |
---|
36 | static char sccsid[] = "@(#)sctp.c 0.1 (Berkeley) 4/18/2007"; |
---|
37 | #endif /* not lint */ |
---|
38 | #endif |
---|
39 | |
---|
40 | #include <sys/cdefs.h> |
---|
41 | __FBSDID("$FreeBSD$"); |
---|
42 | |
---|
43 | #include <rtems/bsd/sys/param.h> |
---|
44 | #include <sys/queue.h> |
---|
45 | #include <rtems/bsd/sys/types.h> |
---|
46 | #include <sys/socket.h> |
---|
47 | #include <sys/socketvar.h> |
---|
48 | #include <sys/sysctl.h> |
---|
49 | #include <sys/protosw.h> |
---|
50 | |
---|
51 | #include <netinet/in.h> |
---|
52 | #include <netinet/sctp.h> |
---|
53 | #include <netinet/sctp_constants.h> |
---|
54 | #include <arpa/inet.h> |
---|
55 | |
---|
56 | #include <err.h> |
---|
57 | #include <errno.h> |
---|
58 | #include <libutil.h> |
---|
59 | #include <netdb.h> |
---|
60 | #include <stdint.h> |
---|
61 | #include <stdio.h> |
---|
62 | #include <stdlib.h> |
---|
63 | #include <string.h> |
---|
64 | #include <unistd.h> |
---|
65 | #include "netstat.h" |
---|
66 | |
---|
67 | #ifdef SCTP |
---|
68 | |
---|
69 | static void sctp_statesprint(uint32_t state); |
---|
70 | |
---|
71 | #define NETSTAT_SCTP_STATES_CLOSED 0x0 |
---|
72 | #define NETSTAT_SCTP_STATES_BOUND 0x1 |
---|
73 | #define NETSTAT_SCTP_STATES_LISTEN 0x2 |
---|
74 | #define NETSTAT_SCTP_STATES_COOKIE_WAIT 0x3 |
---|
75 | #define NETSTAT_SCTP_STATES_COOKIE_ECHOED 0x4 |
---|
76 | #define NETSTAT_SCTP_STATES_ESTABLISHED 0x5 |
---|
77 | #define NETSTAT_SCTP_STATES_SHUTDOWN_SENT 0x6 |
---|
78 | #define NETSTAT_SCTP_STATES_SHUTDOWN_RECEIVED 0x7 |
---|
79 | #define NETSTAT_SCTP_STATES_SHUTDOWN_ACK_SENT 0x8 |
---|
80 | #define NETSTAT_SCTP_STATES_SHUTDOWN_PENDING 0x9 |
---|
81 | |
---|
82 | char *sctpstates[] = { |
---|
83 | "CLOSED", |
---|
84 | "BOUND", |
---|
85 | "LISTEN", |
---|
86 | "COOKIE_WAIT", |
---|
87 | "COOKIE_ECHOED", |
---|
88 | "ESTABLISHED", |
---|
89 | "SHUTDOWN_SENT", |
---|
90 | "SHUTDOWN_RECEIVED", |
---|
91 | "SHUTDOWN_ACK_SENT", |
---|
92 | "SHUTDOWN_PENDING" |
---|
93 | }; |
---|
94 | |
---|
95 | LIST_HEAD(xladdr_list, xladdr_entry) xladdr_head; |
---|
96 | struct xladdr_entry { |
---|
97 | struct xsctp_laddr *xladdr; |
---|
98 | LIST_ENTRY(xladdr_entry) xladdr_entries; |
---|
99 | }; |
---|
100 | |
---|
101 | LIST_HEAD(xraddr_list, xraddr_entry) xraddr_head; |
---|
102 | struct xraddr_entry { |
---|
103 | struct xsctp_raddr *xraddr; |
---|
104 | LIST_ENTRY(xraddr_entry) xraddr_entries; |
---|
105 | }; |
---|
106 | |
---|
107 | /* |
---|
108 | * Construct an Internet address representation. |
---|
109 | * If numeric_addr has been supplied, give |
---|
110 | * numeric value, otherwise try for symbolic name. |
---|
111 | */ |
---|
112 | #ifdef INET |
---|
113 | static char * |
---|
114 | inetname(struct in_addr *inp) |
---|
115 | { |
---|
116 | char *cp; |
---|
117 | static char line[MAXHOSTNAMELEN]; |
---|
118 | struct hostent *hp; |
---|
119 | struct netent *np; |
---|
120 | |
---|
121 | cp = 0; |
---|
122 | if (!numeric_addr && inp->s_addr != INADDR_ANY) { |
---|
123 | int net = inet_netof(*inp); |
---|
124 | int lna = inet_lnaof(*inp); |
---|
125 | |
---|
126 | if (lna == INADDR_ANY) { |
---|
127 | np = getnetbyaddr(net, AF_INET); |
---|
128 | if (np) |
---|
129 | cp = np->n_name; |
---|
130 | } |
---|
131 | if (cp == 0) { |
---|
132 | hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET); |
---|
133 | if (hp) { |
---|
134 | cp = hp->h_name; |
---|
135 | trimdomain(cp, strlen(cp)); |
---|
136 | } |
---|
137 | } |
---|
138 | } |
---|
139 | if (inp->s_addr == INADDR_ANY) |
---|
140 | strcpy(line, "*"); |
---|
141 | else if (cp) { |
---|
142 | strlcpy(line, cp, sizeof(line)); |
---|
143 | } else { |
---|
144 | inp->s_addr = ntohl(inp->s_addr); |
---|
145 | #define C(x) ((u_int)((x) & 0xff)) |
---|
146 | sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24), |
---|
147 | C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr)); |
---|
148 | inp->s_addr = htonl(inp->s_addr); |
---|
149 | } |
---|
150 | return (line); |
---|
151 | } |
---|
152 | #endif |
---|
153 | |
---|
154 | #ifdef INET6 |
---|
155 | static char ntop_buf[INET6_ADDRSTRLEN]; |
---|
156 | |
---|
157 | static char * |
---|
158 | inet6name(struct in6_addr *in6p) |
---|
159 | { |
---|
160 | char *cp; |
---|
161 | static char line[50]; |
---|
162 | struct hostent *hp; |
---|
163 | static char domain[MAXHOSTNAMELEN]; |
---|
164 | static int first = 1; |
---|
165 | |
---|
166 | if (first && !numeric_addr) { |
---|
167 | first = 0; |
---|
168 | if (gethostname(domain, MAXHOSTNAMELEN) == 0 && |
---|
169 | (cp = index(domain, '.'))) |
---|
170 | (void) strcpy(domain, cp + 1); |
---|
171 | else |
---|
172 | domain[0] = 0; |
---|
173 | } |
---|
174 | cp = 0; |
---|
175 | if (!numeric_addr && !IN6_IS_ADDR_UNSPECIFIED(in6p)) { |
---|
176 | hp = gethostbyaddr((char *)in6p, sizeof(*in6p), AF_INET6); |
---|
177 | if (hp) { |
---|
178 | if ((cp = index(hp->h_name, '.')) && |
---|
179 | !strcmp(cp + 1, domain)) |
---|
180 | *cp = 0; |
---|
181 | cp = hp->h_name; |
---|
182 | } |
---|
183 | } |
---|
184 | if (IN6_IS_ADDR_UNSPECIFIED(in6p)) |
---|
185 | strcpy(line, "*"); |
---|
186 | else if (cp) |
---|
187 | strcpy(line, cp); |
---|
188 | else |
---|
189 | sprintf(line, "%s", |
---|
190 | inet_ntop(AF_INET6, (void *)in6p, ntop_buf, |
---|
191 | sizeof(ntop_buf))); |
---|
192 | return (line); |
---|
193 | } |
---|
194 | #endif |
---|
195 | |
---|
196 | static void |
---|
197 | sctp_print_address(union sctp_sockstore *address, int port, int num_port) |
---|
198 | { |
---|
199 | struct servent *sp = 0; |
---|
200 | char line[80], *cp; |
---|
201 | int width; |
---|
202 | |
---|
203 | switch (address->sa.sa_family) { |
---|
204 | #ifdef INET |
---|
205 | case AF_INET: |
---|
206 | sprintf(line, "%.*s.", Wflag ? 39 : 16, inetname(&address->sin.sin_addr)); |
---|
207 | break; |
---|
208 | #endif |
---|
209 | #ifdef INET6 |
---|
210 | case AF_INET6: |
---|
211 | sprintf(line, "%.*s.", Wflag ? 39 : 16, inet6name(&address->sin6.sin6_addr)); |
---|
212 | break; |
---|
213 | #endif |
---|
214 | default: |
---|
215 | sprintf(line, "%.*s.", Wflag ? 39 : 16, ""); |
---|
216 | break; |
---|
217 | } |
---|
218 | cp = index(line, '\0'); |
---|
219 | if (!num_port && port) |
---|
220 | sp = getservbyport((int)port, "sctp"); |
---|
221 | if (sp || port == 0) |
---|
222 | sprintf(cp, "%.15s ", sp ? sp->s_name : "*"); |
---|
223 | else |
---|
224 | sprintf(cp, "%d ", ntohs((u_short)port)); |
---|
225 | width = Wflag ? 45 : 22; |
---|
226 | printf("%-*.*s ", width, width, line); |
---|
227 | } |
---|
228 | |
---|
229 | static int |
---|
230 | sctp_skip_xinpcb_ifneed(char *buf, const size_t buflen, size_t *offset) |
---|
231 | { |
---|
232 | int exist_tcb = 0; |
---|
233 | struct xsctp_tcb *xstcb; |
---|
234 | struct xsctp_raddr *xraddr; |
---|
235 | struct xsctp_laddr *xladdr; |
---|
236 | |
---|
237 | while (*offset < buflen) { |
---|
238 | xladdr = (struct xsctp_laddr *)(buf + *offset); |
---|
239 | *offset += sizeof(struct xsctp_laddr); |
---|
240 | if (xladdr->last == 1) |
---|
241 | break; |
---|
242 | } |
---|
243 | |
---|
244 | while (*offset < buflen) { |
---|
245 | xstcb = (struct xsctp_tcb *)(buf + *offset); |
---|
246 | *offset += sizeof(struct xsctp_tcb); |
---|
247 | if (xstcb->last == 1) |
---|
248 | break; |
---|
249 | |
---|
250 | exist_tcb = 1; |
---|
251 | |
---|
252 | while (*offset < buflen) { |
---|
253 | xladdr = (struct xsctp_laddr *)(buf + *offset); |
---|
254 | *offset += sizeof(struct xsctp_laddr); |
---|
255 | if (xladdr->last == 1) |
---|
256 | break; |
---|
257 | } |
---|
258 | |
---|
259 | while (*offset < buflen) { |
---|
260 | xraddr = (struct xsctp_raddr *)(buf + *offset); |
---|
261 | *offset += sizeof(struct xsctp_raddr); |
---|
262 | if (xraddr->last == 1) |
---|
263 | break; |
---|
264 | } |
---|
265 | } |
---|
266 | |
---|
267 | /* |
---|
268 | * If Lflag is set, we don't care about the return value. |
---|
269 | */ |
---|
270 | if (Lflag) |
---|
271 | return 0; |
---|
272 | |
---|
273 | return exist_tcb; |
---|
274 | } |
---|
275 | |
---|
276 | static void |
---|
277 | sctp_process_tcb(struct xsctp_tcb *xstcb, |
---|
278 | char *buf, const size_t buflen, size_t *offset, int *indent) |
---|
279 | { |
---|
280 | int i, xl_total = 0, xr_total = 0, x_max; |
---|
281 | struct xsctp_raddr *xraddr; |
---|
282 | struct xsctp_laddr *xladdr; |
---|
283 | struct xladdr_entry *prev_xl = NULL, *xl = NULL, *xl_tmp; |
---|
284 | struct xraddr_entry *prev_xr = NULL, *xr = NULL, *xr_tmp; |
---|
285 | |
---|
286 | LIST_INIT(&xladdr_head); |
---|
287 | LIST_INIT(&xraddr_head); |
---|
288 | |
---|
289 | /* |
---|
290 | * Make `struct xladdr_list' list and `struct xraddr_list' list |
---|
291 | * to handle the address flexibly. |
---|
292 | */ |
---|
293 | while (*offset < buflen) { |
---|
294 | xladdr = (struct xsctp_laddr *)(buf + *offset); |
---|
295 | *offset += sizeof(struct xsctp_laddr); |
---|
296 | if (xladdr->last == 1) |
---|
297 | break; |
---|
298 | |
---|
299 | prev_xl = xl; |
---|
300 | xl = malloc(sizeof(struct xladdr_entry)); |
---|
301 | if (xl == NULL) { |
---|
302 | warnx("malloc %lu bytes", |
---|
303 | (u_long)sizeof(struct xladdr_entry)); |
---|
304 | goto out; |
---|
305 | } |
---|
306 | xl->xladdr = xladdr; |
---|
307 | if (prev_xl == NULL) |
---|
308 | LIST_INSERT_HEAD(&xladdr_head, xl, xladdr_entries); |
---|
309 | else |
---|
310 | LIST_INSERT_AFTER(prev_xl, xl, xladdr_entries); |
---|
311 | xl_total++; |
---|
312 | } |
---|
313 | |
---|
314 | while (*offset < buflen) { |
---|
315 | xraddr = (struct xsctp_raddr *)(buf + *offset); |
---|
316 | *offset += sizeof(struct xsctp_raddr); |
---|
317 | if (xraddr->last == 1) |
---|
318 | break; |
---|
319 | |
---|
320 | prev_xr = xr; |
---|
321 | xr = malloc(sizeof(struct xraddr_entry)); |
---|
322 | if (xr == NULL) { |
---|
323 | warnx("malloc %lu bytes", |
---|
324 | (u_long)sizeof(struct xraddr_entry)); |
---|
325 | goto out; |
---|
326 | } |
---|
327 | xr->xraddr = xraddr; |
---|
328 | if (prev_xr == NULL) |
---|
329 | LIST_INSERT_HEAD(&xraddr_head, xr, xraddr_entries); |
---|
330 | else |
---|
331 | LIST_INSERT_AFTER(prev_xr, xr, xraddr_entries); |
---|
332 | xr_total++; |
---|
333 | } |
---|
334 | |
---|
335 | /* |
---|
336 | * Let's print the address infos. |
---|
337 | */ |
---|
338 | xl = LIST_FIRST(&xladdr_head); |
---|
339 | xr = LIST_FIRST(&xraddr_head); |
---|
340 | x_max = (xl_total > xr_total) ? xl_total : xr_total; |
---|
341 | for (i = 0; i < x_max; i++) { |
---|
342 | if (((*indent == 0) && i > 0) || *indent > 0) |
---|
343 | printf("%-12s ", " "); |
---|
344 | |
---|
345 | if (xl != NULL) { |
---|
346 | sctp_print_address(&(xl->xladdr->address), |
---|
347 | htons(xstcb->local_port), numeric_port); |
---|
348 | } else { |
---|
349 | if (Wflag) { |
---|
350 | printf("%-45s ", " "); |
---|
351 | } else { |
---|
352 | printf("%-22s ", " "); |
---|
353 | } |
---|
354 | } |
---|
355 | |
---|
356 | if (xr != NULL && !Lflag) { |
---|
357 | sctp_print_address(&(xr->xraddr->address), |
---|
358 | htons(xstcb->remote_port), numeric_port); |
---|
359 | } |
---|
360 | |
---|
361 | if (xl != NULL) |
---|
362 | xl = LIST_NEXT(xl, xladdr_entries); |
---|
363 | if (xr != NULL) |
---|
364 | xr = LIST_NEXT(xr, xraddr_entries); |
---|
365 | |
---|
366 | if (i == 0 && !Lflag) |
---|
367 | sctp_statesprint(xstcb->state); |
---|
368 | |
---|
369 | if (i < x_max) |
---|
370 | putchar('\n'); |
---|
371 | } |
---|
372 | |
---|
373 | out: |
---|
374 | /* |
---|
375 | * Free the list which be used to handle the address. |
---|
376 | */ |
---|
377 | xl = LIST_FIRST(&xladdr_head); |
---|
378 | while (xl != NULL) { |
---|
379 | xl_tmp = LIST_NEXT(xl, xladdr_entries); |
---|
380 | free(xl); |
---|
381 | xl = xl_tmp; |
---|
382 | } |
---|
383 | |
---|
384 | xr = LIST_FIRST(&xraddr_head); |
---|
385 | while (xr != NULL) { |
---|
386 | xr_tmp = LIST_NEXT(xr, xraddr_entries); |
---|
387 | free(xr); |
---|
388 | xr = xr_tmp; |
---|
389 | } |
---|
390 | } |
---|
391 | |
---|
392 | static void |
---|
393 | sctp_process_inpcb(struct xsctp_inpcb *xinpcb, |
---|
394 | char *buf, const size_t buflen, size_t *offset) |
---|
395 | { |
---|
396 | int indent = 0, xladdr_total = 0, is_listening = 0; |
---|
397 | static int first = 1; |
---|
398 | char *tname, *pname; |
---|
399 | struct xsctp_tcb *xstcb; |
---|
400 | struct xsctp_laddr *xladdr; |
---|
401 | size_t offset_laddr; |
---|
402 | int process_closed; |
---|
403 | |
---|
404 | if (xinpcb->maxqlen > 0) |
---|
405 | is_listening = 1; |
---|
406 | |
---|
407 | if (first) { |
---|
408 | if (!Lflag) { |
---|
409 | printf("Active SCTP associations"); |
---|
410 | if (aflag) |
---|
411 | printf(" (including servers)"); |
---|
412 | } else |
---|
413 | printf("Current listen queue sizes (qlen/maxqlen)"); |
---|
414 | putchar('\n'); |
---|
415 | if (Lflag) |
---|
416 | printf("%-6.6s %-5.5s %-8.8s %-22.22s\n", |
---|
417 | "Proto", "Type", "Listen", "Local Address"); |
---|
418 | else |
---|
419 | if (Wflag) |
---|
420 | printf("%-6.6s %-5.5s %-45.45s %-45.45s %s\n", |
---|
421 | "Proto", "Type", |
---|
422 | "Local Address", "Foreign Address", |
---|
423 | "(state)"); |
---|
424 | else |
---|
425 | printf("%-6.6s %-5.5s %-22.22s %-22.22s %s\n", |
---|
426 | "Proto", "Type", |
---|
427 | "Local Address", "Foreign Address", |
---|
428 | "(state)"); |
---|
429 | first = 0; |
---|
430 | } |
---|
431 | xladdr = (struct xsctp_laddr *)(buf + *offset); |
---|
432 | if (Lflag && !is_listening) { |
---|
433 | sctp_skip_xinpcb_ifneed(buf, buflen, offset); |
---|
434 | return; |
---|
435 | } |
---|
436 | |
---|
437 | if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) { |
---|
438 | /* Can't distinguish between sctp46 and sctp6 */ |
---|
439 | pname = "sctp46"; |
---|
440 | } else { |
---|
441 | pname = "sctp4"; |
---|
442 | } |
---|
443 | |
---|
444 | if (xinpcb->flags & SCTP_PCB_FLAGS_TCPTYPE) |
---|
445 | tname = "1to1"; |
---|
446 | else if (xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE) |
---|
447 | tname = "1toN"; |
---|
448 | else |
---|
449 | tname = "????"; |
---|
450 | |
---|
451 | if (Lflag) { |
---|
452 | char buf1[9]; |
---|
453 | |
---|
454 | snprintf(buf1, 9, "%hu/%hu", xinpcb->qlen, xinpcb->maxqlen); |
---|
455 | printf("%-6.6s %-5.5s ", pname, tname); |
---|
456 | printf("%-8.8s ", buf1); |
---|
457 | } |
---|
458 | |
---|
459 | offset_laddr = *offset; |
---|
460 | process_closed = 0; |
---|
461 | retry: |
---|
462 | while (*offset < buflen) { |
---|
463 | xladdr = (struct xsctp_laddr *)(buf + *offset); |
---|
464 | *offset += sizeof(struct xsctp_laddr); |
---|
465 | if (xladdr->last) { |
---|
466 | if (aflag && !Lflag && (xladdr_total == 0) && process_closed) { |
---|
467 | printf("%-6.6s %-5.5s ", pname, tname); |
---|
468 | if (Wflag) { |
---|
469 | printf("%-91.91s CLOSED", " "); |
---|
470 | } else { |
---|
471 | printf("%-45.45s CLOSED", " "); |
---|
472 | } |
---|
473 | } |
---|
474 | if (process_closed || is_listening) { |
---|
475 | putchar('\n'); |
---|
476 | } |
---|
477 | break; |
---|
478 | } |
---|
479 | |
---|
480 | if (!Lflag && !is_listening && !process_closed) |
---|
481 | continue; |
---|
482 | |
---|
483 | if (xladdr_total == 0) { |
---|
484 | printf("%-6.6s %-5.5s ", pname, tname); |
---|
485 | } else { |
---|
486 | putchar('\n'); |
---|
487 | printf((Lflag) ? |
---|
488 | "%-21.21s " : "%-12.12s ", " "); |
---|
489 | } |
---|
490 | sctp_print_address(&(xladdr->address), |
---|
491 | htons(xinpcb->local_port), numeric_port); |
---|
492 | if (aflag && !Lflag && xladdr_total == 0) { |
---|
493 | if (Wflag) { |
---|
494 | if (process_closed) { |
---|
495 | printf("%-45.45s CLOSED", " "); |
---|
496 | } else { |
---|
497 | printf("%-45.45s LISTEN", " "); |
---|
498 | } |
---|
499 | } else { |
---|
500 | if (process_closed) { |
---|
501 | printf("%-22.22s CLOSED", " "); |
---|
502 | } else { |
---|
503 | printf("%-22.22s LISTEN", " "); |
---|
504 | } |
---|
505 | } |
---|
506 | } |
---|
507 | xladdr_total++; |
---|
508 | } |
---|
509 | |
---|
510 | xstcb = (struct xsctp_tcb *)(buf + *offset); |
---|
511 | *offset += sizeof(struct xsctp_tcb); |
---|
512 | if (aflag && (xladdr_total == 0) && xstcb->last && !process_closed) { |
---|
513 | process_closed = 1; |
---|
514 | *offset = offset_laddr; |
---|
515 | goto retry; |
---|
516 | } |
---|
517 | while (xstcb->last == 0 && *offset < buflen) { |
---|
518 | printf("%-6.6s %-5.5s ", pname, tname); |
---|
519 | sctp_process_tcb(xstcb, buf, buflen, offset, &indent); |
---|
520 | indent++; |
---|
521 | xstcb = (struct xsctp_tcb *)(buf + *offset); |
---|
522 | *offset += sizeof(struct xsctp_tcb); |
---|
523 | } |
---|
524 | } |
---|
525 | |
---|
526 | /* |
---|
527 | * Print a summary of SCTP connections related to an Internet |
---|
528 | * protocol. |
---|
529 | */ |
---|
530 | void |
---|
531 | sctp_protopr(u_long off __unused, |
---|
532 | const char *name, int af1, int proto) |
---|
533 | { |
---|
534 | char *buf; |
---|
535 | const char *mibvar = "net.inet.sctp.assoclist"; |
---|
536 | size_t offset = 0; |
---|
537 | size_t len = 0; |
---|
538 | struct xsctp_inpcb *xinpcb; |
---|
539 | |
---|
540 | if (proto != IPPROTO_SCTP) |
---|
541 | return; |
---|
542 | |
---|
543 | if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) { |
---|
544 | if (errno != ENOENT) |
---|
545 | warn("sysctl: %s", mibvar); |
---|
546 | return; |
---|
547 | } |
---|
548 | if ((buf = malloc(len)) == 0) { |
---|
549 | warnx("malloc %lu bytes", (u_long)len); |
---|
550 | return; |
---|
551 | } |
---|
552 | if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) { |
---|
553 | warn("sysctl: %s", mibvar); |
---|
554 | free(buf); |
---|
555 | return; |
---|
556 | } |
---|
557 | |
---|
558 | xinpcb = (struct xsctp_inpcb *)(buf + offset); |
---|
559 | offset += sizeof(struct xsctp_inpcb); |
---|
560 | while (xinpcb->last == 0 && offset < len) { |
---|
561 | sctp_process_inpcb(xinpcb, buf, (const size_t)len, |
---|
562 | &offset); |
---|
563 | |
---|
564 | xinpcb = (struct xsctp_inpcb *)(buf + offset); |
---|
565 | offset += sizeof(struct xsctp_inpcb); |
---|
566 | } |
---|
567 | |
---|
568 | free(buf); |
---|
569 | } |
---|
570 | |
---|
571 | static void |
---|
572 | sctp_statesprint(uint32_t state) |
---|
573 | { |
---|
574 | int idx; |
---|
575 | |
---|
576 | switch (state) { |
---|
577 | case SCTP_STATE_COOKIE_WAIT: |
---|
578 | idx = NETSTAT_SCTP_STATES_COOKIE_WAIT; |
---|
579 | break; |
---|
580 | case SCTP_STATE_COOKIE_ECHOED: |
---|
581 | idx = NETSTAT_SCTP_STATES_COOKIE_ECHOED; |
---|
582 | break; |
---|
583 | case SCTP_STATE_OPEN: |
---|
584 | idx = NETSTAT_SCTP_STATES_ESTABLISHED; |
---|
585 | break; |
---|
586 | case SCTP_STATE_SHUTDOWN_SENT: |
---|
587 | idx = NETSTAT_SCTP_STATES_SHUTDOWN_SENT; |
---|
588 | break; |
---|
589 | case SCTP_STATE_SHUTDOWN_RECEIVED: |
---|
590 | idx = NETSTAT_SCTP_STATES_SHUTDOWN_RECEIVED; |
---|
591 | break; |
---|
592 | case SCTP_STATE_SHUTDOWN_ACK_SENT: |
---|
593 | idx = NETSTAT_SCTP_STATES_SHUTDOWN_ACK_SENT; |
---|
594 | break; |
---|
595 | case SCTP_STATE_SHUTDOWN_PENDING: |
---|
596 | idx = NETSTAT_SCTP_STATES_SHUTDOWN_PENDING; |
---|
597 | break; |
---|
598 | default: |
---|
599 | printf("UNKNOWN 0x%08x", state); |
---|
600 | return; |
---|
601 | } |
---|
602 | |
---|
603 | printf("%s", sctpstates[idx]); |
---|
604 | } |
---|
605 | |
---|
606 | /* |
---|
607 | * Dump SCTP statistics structure. |
---|
608 | */ |
---|
609 | void |
---|
610 | sctp_stats(u_long off, const char *name, int af1 __unused, int proto __unused) |
---|
611 | { |
---|
612 | struct sctpstat sctpstat, zerostat; |
---|
613 | size_t len = sizeof(sctpstat); |
---|
614 | |
---|
615 | if (live) { |
---|
616 | if (zflag) |
---|
617 | memset(&zerostat, 0, len); |
---|
618 | if (sysctlbyname("net.inet.sctp.stats", &sctpstat, &len, |
---|
619 | zflag ? &zerostat : NULL, zflag ? len : 0) < 0) { |
---|
620 | if (errno != ENOENT) |
---|
621 | warn("sysctl: net.inet.sctp.stats"); |
---|
622 | return; |
---|
623 | } |
---|
624 | } else |
---|
625 | kread(off, &sctpstat, len); |
---|
626 | |
---|
627 | printf ("%s:\n", name); |
---|
628 | |
---|
629 | #define p(f, m) if (sctpstat.f || sflag <= 1) \ |
---|
630 | printf(m, (uintmax_t)sctpstat.f, plural(sctpstat.f)) |
---|
631 | #define p1a(f, m) if (sctpstat.f || sflag <= 1) \ |
---|
632 | printf(m, (uintmax_t)sctpstat.f) |
---|
633 | |
---|
634 | /* |
---|
635 | * input statistics |
---|
636 | */ |
---|
637 | p(sctps_recvpackets, "\t%ju input packet%s\n"); |
---|
638 | p(sctps_recvdatagrams, "\t\t%ju datagram%s\n"); |
---|
639 | p(sctps_recvpktwithdata, "\t\t%ju packet%s that had data\n"); |
---|
640 | p(sctps_recvsacks, "\t\t%ju input SACK chunk%s\n"); |
---|
641 | p(sctps_recvdata, "\t\t%ju input DATA chunk%s\n"); |
---|
642 | p(sctps_recvdupdata, "\t\t%ju duplicate DATA chunk%s\n"); |
---|
643 | p(sctps_recvheartbeat, "\t\t%ju input HB chunk%s\n"); |
---|
644 | p(sctps_recvheartbeatack, "\t\t%ju HB-ACK chunk%s\n"); |
---|
645 | p(sctps_recvecne, "\t\t%ju input ECNE chunk%s\n"); |
---|
646 | p(sctps_recvauth, "\t\t%ju input AUTH chunk%s\n"); |
---|
647 | p(sctps_recvauthmissing, "\t\t%ju chunk%s missing AUTH\n"); |
---|
648 | p(sctps_recvivalhmacid, "\t\t%ju invalid HMAC id%s received\n"); |
---|
649 | p(sctps_recvivalkeyid, "\t\t%ju invalid secret id%s received\n"); |
---|
650 | p1a(sctps_recvauthfailed, "\t\t%ju auth failed\n"); |
---|
651 | p1a(sctps_recvexpress, "\t\t%ju fast path receives all one chunk\n"); |
---|
652 | p1a(sctps_recvexpressm, "\t\t%ju fast path multi-part data\n"); |
---|
653 | |
---|
654 | /* |
---|
655 | * output statistics |
---|
656 | */ |
---|
657 | p(sctps_sendpackets, "\t%ju output packet%s\n"); |
---|
658 | p(sctps_sendsacks, "\t\t%ju output SACK%s\n"); |
---|
659 | p(sctps_senddata, "\t\t%ju output DATA chunk%s\n"); |
---|
660 | p(sctps_sendretransdata, "\t\t%ju retransmitted DATA chunk%s\n"); |
---|
661 | p(sctps_sendfastretrans, "\t\t%ju fast retransmitted DATA chunk%s\n"); |
---|
662 | p(sctps_sendmultfastretrans, "\t\t%ju FR'%s that happened more " |
---|
663 | "than once to same chunk\n"); |
---|
664 | p(sctps_sendheartbeat, "\t\t%ju output HB chunk%s\n"); |
---|
665 | p(sctps_sendecne, "\t\t%ju output ECNE chunk%s\n"); |
---|
666 | p(sctps_sendauth, "\t\t%ju output AUTH chunk%s\n"); |
---|
667 | p1a(sctps_senderrors, "\t\t%ju ip_output error counter\n"); |
---|
668 | |
---|
669 | /* |
---|
670 | * PCKDROPREP statistics |
---|
671 | */ |
---|
672 | printf("\tPacket drop statistics:\n"); |
---|
673 | p1a(sctps_pdrpfmbox, "\t\t%ju from middle box\n"); |
---|
674 | p1a(sctps_pdrpfehos, "\t\t%ju from end host\n"); |
---|
675 | p1a(sctps_pdrpmbda, "\t\t%ju with data\n"); |
---|
676 | p1a(sctps_pdrpmbct, "\t\t%ju non-data, non-endhost\n"); |
---|
677 | p1a(sctps_pdrpbwrpt, "\t\t%ju non-endhost, bandwidth rep only\n"); |
---|
678 | p1a(sctps_pdrpcrupt, "\t\t%ju not enough for chunk header\n"); |
---|
679 | p1a(sctps_pdrpnedat, "\t\t%ju not enough data to confirm\n"); |
---|
680 | p1a(sctps_pdrppdbrk, "\t\t%ju where process_chunk_drop said break\n"); |
---|
681 | p1a(sctps_pdrptsnnf, "\t\t%ju failed to find TSN\n"); |
---|
682 | p1a(sctps_pdrpdnfnd, "\t\t%ju attempt reverse TSN lookup\n"); |
---|
683 | p1a(sctps_pdrpdiwnp, "\t\t%ju e-host confirms zero-rwnd\n"); |
---|
684 | p1a(sctps_pdrpdizrw, "\t\t%ju midbox confirms no space\n"); |
---|
685 | p1a(sctps_pdrpbadd, "\t\t%ju data did not match TSN\n"); |
---|
686 | p(sctps_pdrpmark, "\t\t%ju TSN'%s marked for Fast Retran\n"); |
---|
687 | |
---|
688 | /* |
---|
689 | * Timeouts |
---|
690 | */ |
---|
691 | printf("\tTimeouts:\n"); |
---|
692 | p(sctps_timoiterator, "\t\t%ju iterator timer%s fired\n"); |
---|
693 | p(sctps_timodata, "\t\t%ju T3 data time out%s\n"); |
---|
694 | p(sctps_timowindowprobe, "\t\t%ju window probe (T3) timer%s fired\n"); |
---|
695 | p(sctps_timoinit, "\t\t%ju INIT timer%s fired\n"); |
---|
696 | p(sctps_timosack, "\t\t%ju sack timer%s fired\n"); |
---|
697 | p(sctps_timoshutdown, "\t\t%ju shutdown timer%s fired\n"); |
---|
698 | p(sctps_timoheartbeat, "\t\t%ju heartbeat timer%s fired\n"); |
---|
699 | p1a(sctps_timocookie, "\t\t%ju a cookie timeout fired\n"); |
---|
700 | p1a(sctps_timosecret, "\t\t%ju an endpoint changed its cookie" |
---|
701 | "secret\n"); |
---|
702 | p(sctps_timopathmtu, "\t\t%ju PMTU timer%s fired\n"); |
---|
703 | p(sctps_timoshutdownack, "\t\t%ju shutdown ack timer%s fired\n"); |
---|
704 | p(sctps_timoshutdownguard, "\t\t%ju shutdown guard timer%s fired\n"); |
---|
705 | p(sctps_timostrmrst, "\t\t%ju stream reset timer%s fired\n"); |
---|
706 | p(sctps_timoearlyfr, "\t\t%ju early FR timer%s fired\n"); |
---|
707 | p1a(sctps_timoasconf, "\t\t%ju an asconf timer fired\n"); |
---|
708 | p1a(sctps_timoautoclose, "\t\t%ju auto close timer fired\n"); |
---|
709 | p(sctps_timoassockill, "\t\t%ju asoc free timer%s expired\n"); |
---|
710 | p(sctps_timoinpkill, "\t\t%ju inp free timer%s expired\n"); |
---|
711 | |
---|
712 | #if 0 |
---|
713 | /* |
---|
714 | * Early fast retransmission counters |
---|
715 | */ |
---|
716 | p(sctps_earlyfrstart, "\t%ju TODO:sctps_earlyfrstart\n"); |
---|
717 | p(sctps_earlyfrstop, "\t%ju TODO:sctps_earlyfrstop\n"); |
---|
718 | p(sctps_earlyfrmrkretrans, "\t%ju TODO:sctps_earlyfrmrkretrans\n"); |
---|
719 | p(sctps_earlyfrstpout, "\t%ju TODO:sctps_earlyfrstpout\n"); |
---|
720 | p(sctps_earlyfrstpidsck1, "\t%ju TODO:sctps_earlyfrstpidsck1\n"); |
---|
721 | p(sctps_earlyfrstpidsck2, "\t%ju TODO:sctps_earlyfrstpidsck2\n"); |
---|
722 | p(sctps_earlyfrstpidsck3, "\t%ju TODO:sctps_earlyfrstpidsck3\n"); |
---|
723 | p(sctps_earlyfrstpidsck4, "\t%ju TODO:sctps_earlyfrstpidsck4\n"); |
---|
724 | p(sctps_earlyfrstrid, "\t%ju TODO:sctps_earlyfrstrid\n"); |
---|
725 | p(sctps_earlyfrstrout, "\t%ju TODO:sctps_earlyfrstrout\n"); |
---|
726 | p(sctps_earlyfrstrtmr, "\t%ju TODO:sctps_earlyfrstrtmr\n"); |
---|
727 | #endif |
---|
728 | |
---|
729 | /* |
---|
730 | * Others |
---|
731 | */ |
---|
732 | p1a(sctps_hdrops, "\t%ju packet shorter than header\n"); |
---|
733 | p1a(sctps_badsum, "\t%ju checksum error\n"); |
---|
734 | p1a(sctps_noport, "\t%ju no endpoint for port\n"); |
---|
735 | p1a(sctps_badvtag, "\t%ju bad v-tag\n"); |
---|
736 | p1a(sctps_badsid, "\t%ju bad SID\n"); |
---|
737 | p1a(sctps_nomem, "\t%ju no memory\n"); |
---|
738 | p1a(sctps_fastretransinrtt, "\t%ju number of multiple FR in a RTT " |
---|
739 | "window\n"); |
---|
740 | #if 0 |
---|
741 | p(sctps_markedretrans, "\t%ju TODO:sctps_markedretrans\n"); |
---|
742 | #endif |
---|
743 | p1a(sctps_naglesent, "\t%ju RFC813 allowed sending\n"); |
---|
744 | p1a(sctps_naglequeued, "\t%ju RFC813 does not allow sending\n"); |
---|
745 | p1a(sctps_maxburstqueued, "\t%ju times max burst prohibited sending\n"); |
---|
746 | p1a(sctps_ifnomemqueued, "\t%ju look ahead tells us no memory in " |
---|
747 | "interface\n"); |
---|
748 | p(sctps_windowprobed, "\t%ju number%s of window probes sent\n"); |
---|
749 | p(sctps_lowlevelerr, "\t%ju time%s an output error to clamp " |
---|
750 | "down on next user send\n"); |
---|
751 | p(sctps_lowlevelerrusr, "\t%ju time%s sctp_senderrors were " |
---|
752 | "caused from a user\n"); |
---|
753 | p(sctps_datadropchklmt, "\t%ju number of in data drop%s due to " |
---|
754 | "chunk limit reached\n"); |
---|
755 | p(sctps_datadroprwnd, "\t%ju number of in data drop%s due to rwnd " |
---|
756 | "limit reached\n"); |
---|
757 | p(sctps_ecnereducedcwnd, "\t%ju time%s a ECN reduced " |
---|
758 | "the cwnd\n"); |
---|
759 | p1a(sctps_vtagexpress, "\t%ju used express lookup via vtag\n"); |
---|
760 | p1a(sctps_vtagbogus, "\t%ju collision in express lookup\n"); |
---|
761 | p(sctps_primary_randry, "\t%ju time%s the sender ran dry " |
---|
762 | "of user data on primary\n"); |
---|
763 | p1a(sctps_cmt_randry, "\t%ju same for above\n"); |
---|
764 | p(sctps_slowpath_sack, "\t%ju sack%s the slow way\n"); |
---|
765 | p(sctps_wu_sacks_sent, "\t%ju window update only sack%s sent\n"); |
---|
766 | p(sctps_sends_with_flags, "\t%ju send%s with sinfo_flags !=0\n"); |
---|
767 | p(sctps_sends_with_unord, "\t%ju unordered send%s\n"); |
---|
768 | p(sctps_sends_with_eof, "\t%ju send%s with EOF flag set\n"); |
---|
769 | p(sctps_sends_with_abort, "\t%ju send%s with ABORT flag set\n"); |
---|
770 | p(sctps_protocol_drain_calls, "\t%ju time%s protocol drain called\n"); |
---|
771 | p(sctps_protocol_drains_done, "\t%ju time%s we did a protocol " |
---|
772 | "drain\n"); |
---|
773 | p(sctps_read_peeks, "\t%ju time%s recv was called with peek\n"); |
---|
774 | p(sctps_cached_chk, "\t%ju cached chunk%s used\n"); |
---|
775 | p1a(sctps_cached_strmoq, "\t%ju cached stream oq's used\n"); |
---|
776 | p(sctps_left_abandon, "\t%ju unread message%s abandonded by close\n"); |
---|
777 | p1a(sctps_send_burst_avoid, "\t%ju send burst avoidance, already " |
---|
778 | "max burst inflight to net\n"); |
---|
779 | p1a(sctps_send_cwnd_avoid, "\t%ju send cwnd full avoidance, already " |
---|
780 | "max burst inflight to net\n"); |
---|
781 | p(sctps_fwdtsn_map_over, "\t%ju number of map array over-run%s via " |
---|
782 | "fwd-tsn's\n"); |
---|
783 | |
---|
784 | #undef p |
---|
785 | #undef p1a |
---|
786 | } |
---|
787 | |
---|
788 | #endif /* SCTP */ |
---|