]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/netstat/sctp.c
MFH: r278968-r280640
[FreeBSD/FreeBSD.git] / usr.bin / netstat / sctp.c
1 /*-
2  * Copyright (c) 2001-2007, by Weongyo Jeong. All rights reserved.
3  * Copyright (c) 2011, by Michael Tuexen. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * a) Redistributions of source code must retain the above copyright notice,
9  *   this list of conditions and the following disclaimer.
10  *
11  * b) Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in
13  *   the documentation and/or other materials provided with the distribution.
14  *
15  * c) Neither the name of Cisco Systems, Inc. nor the names of its
16  *    contributors may be used to endorse or promote products derived
17  *    from this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #if 0
33 #ifndef lint
34 static char sccsid[] = "@(#)sctp.c      0.1 (Berkeley) 4/18/2007";
35 #endif /* not lint */
36 #endif
37
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40
41 #include <sys/param.h>
42 #include <sys/queue.h>
43 #include <sys/types.h>
44 #include <sys/socket.h>
45 #include <sys/socketvar.h>
46 #include <sys/sysctl.h>
47 #include <sys/protosw.h>
48
49 #include <netinet/in.h>
50 #include <netinet/sctp.h>
51 #include <netinet/sctp_constants.h>
52 #include <arpa/inet.h>
53
54 #include <err.h>
55 #include <errno.h>
56 #include <libutil.h>
57 #include <netdb.h>
58 #include <stdint.h>
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <stdbool.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include "netstat.h"
65 #include <libxo/xo.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 const 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 = strchr(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 = strchr(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(const char *container, union sctp_sockstore *address,
198     int port, int num_port)
199 {
200         struct servent *sp = 0;
201         char line[80], *cp;
202         int width;
203
204         if (container)
205                 xo_open_container(container);
206
207         switch (address->sa.sa_family) {
208 #ifdef INET
209         case AF_INET:
210                 sprintf(line, "%.*s.", Wflag ? 39 : 16, inetname(&address->sin.sin_addr));
211                 break;
212 #endif
213 #ifdef INET6
214         case AF_INET6:
215                 sprintf(line, "%.*s.", Wflag ? 39 : 16, inet6name(&address->sin6.sin6_addr));
216                 break;
217 #endif
218         default:
219                 sprintf(line, "%.*s.", Wflag ? 39 : 16, "");
220                 break;
221         }
222         cp = strchr(line, '\0');
223         if (!num_port && port)
224                 sp = getservbyport((int)port, "sctp");
225         if (sp || port == 0)
226                 sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
227         else
228                 sprintf(cp, "%d ", ntohs((u_short)port));
229         width = Wflag ? 45 : 22;
230         xo_emit("{d:target/%-*.*s} ", width, width, line);
231
232         int alen = cp - line - 1, plen = strlen(cp) - 1;
233         xo_emit("{e:address/%*.*s}{e:port/%*.*s}", alen, alen, line, plen,
234             plen, cp);
235
236         if (container)
237                 xo_close_container(container);
238 }
239
240 static int
241 sctp_skip_xinpcb_ifneed(char *buf, const size_t buflen, size_t *offset)
242 {
243         int exist_tcb = 0;
244         struct xsctp_tcb *xstcb;
245         struct xsctp_raddr *xraddr;
246         struct xsctp_laddr *xladdr;
247
248         while (*offset < buflen) {
249                 xladdr = (struct xsctp_laddr *)(buf + *offset);
250                 *offset += sizeof(struct xsctp_laddr);
251                 if (xladdr->last == 1)
252                         break;
253         }
254
255         while (*offset < buflen) {
256                 xstcb = (struct xsctp_tcb *)(buf + *offset);
257                 *offset += sizeof(struct xsctp_tcb);
258                 if (xstcb->last == 1)
259                         break;
260
261                 exist_tcb = 1;
262
263                 while (*offset < buflen) {
264                         xladdr = (struct xsctp_laddr *)(buf + *offset);
265                         *offset += sizeof(struct xsctp_laddr);
266                         if (xladdr->last == 1)
267                                 break;
268                 }
269
270                 while (*offset < buflen) {
271                         xraddr = (struct xsctp_raddr *)(buf + *offset);
272                         *offset += sizeof(struct xsctp_raddr);
273                         if (xraddr->last == 1)
274                                 break;
275                 }
276         }
277
278         /*
279          * If Lflag is set, we don't care about the return value.
280          */
281         if (Lflag)
282                 return 0;
283
284         return exist_tcb;
285 }
286
287 static void
288 sctp_process_tcb(struct xsctp_tcb *xstcb,
289     char *buf, const size_t buflen, size_t *offset, int *indent)
290 {
291         int i, xl_total = 0, xr_total = 0, x_max;
292         struct xsctp_raddr *xraddr;
293         struct xsctp_laddr *xladdr;
294         struct xladdr_entry *prev_xl = NULL, *xl = NULL, *xl_tmp;
295         struct xraddr_entry *prev_xr = NULL, *xr = NULL, *xr_tmp;
296
297         LIST_INIT(&xladdr_head);
298         LIST_INIT(&xraddr_head);
299
300         /*
301          * Make `struct xladdr_list' list and `struct xraddr_list' list
302          * to handle the address flexibly.
303          */
304         while (*offset < buflen) {
305                 xladdr = (struct xsctp_laddr *)(buf + *offset);
306                 *offset += sizeof(struct xsctp_laddr);
307                 if (xladdr->last == 1)
308                         break;
309
310                 prev_xl = xl;
311                 xl = malloc(sizeof(struct xladdr_entry));
312                 if (xl == NULL) {
313                         xo_warnx("malloc %lu bytes",
314                             (u_long)sizeof(struct xladdr_entry));
315                         goto out;
316                 }
317                 xl->xladdr = xladdr;
318                 if (prev_xl == NULL)
319                         LIST_INSERT_HEAD(&xladdr_head, xl, xladdr_entries);
320                 else
321                         LIST_INSERT_AFTER(prev_xl, xl, xladdr_entries);
322                 xl_total++;
323         }
324
325         while (*offset < buflen) {
326                 xraddr = (struct xsctp_raddr *)(buf + *offset);
327                 *offset += sizeof(struct xsctp_raddr);
328                 if (xraddr->last == 1)
329                         break;
330
331                 prev_xr = xr;
332                 xr = malloc(sizeof(struct xraddr_entry));
333                 if (xr == NULL) {
334                         xo_warnx("malloc %lu bytes",
335                             (u_long)sizeof(struct xraddr_entry));
336                         goto out;
337                 }
338                 xr->xraddr = xraddr;
339                 if (prev_xr == NULL)
340                         LIST_INSERT_HEAD(&xraddr_head, xr, xraddr_entries);
341                 else
342                         LIST_INSERT_AFTER(prev_xr, xr, xraddr_entries);
343                 xr_total++;
344         }
345
346         /*
347          * Let's print the address infos.
348          */
349         xo_open_list("address");
350         xl = LIST_FIRST(&xladdr_head);
351         xr = LIST_FIRST(&xraddr_head);
352         x_max = (xl_total > xr_total) ? xl_total : xr_total;
353         for (i = 0; i < x_max; i++) {
354                 xo_open_instance("address");
355
356                 if (((*indent == 0) && i > 0) || *indent > 0)
357                         xo_emit("{P:/%-12s} ", " ");
358
359                 if (xl != NULL) {
360                         sctp_print_address("local", &(xl->xladdr->address),
361                             htons(xstcb->local_port), numeric_port);
362                 } else {
363                         if (Wflag) {
364                                 xo_emit("{P:/%-45s} ", " ");
365                         } else {
366                                 xo_emit("{P:/%-22s} ", " ");
367                         }
368                 }
369
370                 if (xr != NULL && !Lflag) {
371                         sctp_print_address("remote", &(xr->xraddr->address),
372                             htons(xstcb->remote_port), numeric_port);
373                 }
374
375                 if (xl != NULL)
376                         xl = LIST_NEXT(xl, xladdr_entries);
377                 if (xr != NULL)
378                         xr = LIST_NEXT(xr, xraddr_entries);
379
380                 if (i == 0 && !Lflag)
381                         sctp_statesprint(xstcb->state);
382
383                 if (i < x_max)
384                         xo_emit("\n");
385                 xo_close_instance("address");
386         }
387
388 out:
389         /*
390          * Free the list which be used to handle the address.
391          */
392         xl = LIST_FIRST(&xladdr_head);
393         while (xl != NULL) {
394                 xl_tmp = LIST_NEXT(xl, xladdr_entries);
395                 free(xl);
396                 xl = xl_tmp;
397         }
398
399         xr = LIST_FIRST(&xraddr_head);
400         while (xr != NULL) {
401                 xr_tmp = LIST_NEXT(xr, xraddr_entries);
402                 free(xr);
403                 xr = xr_tmp;
404         }
405 }
406
407 static void
408 sctp_process_inpcb(struct xsctp_inpcb *xinpcb,
409     char *buf, const size_t buflen, size_t *offset)
410 {
411         int indent = 0, xladdr_total = 0, is_listening = 0;
412         static int first = 1;
413         const char *tname, *pname;
414         struct xsctp_tcb *xstcb;
415         struct xsctp_laddr *xladdr;
416         size_t offset_laddr;
417         int process_closed;
418
419         if (xinpcb->maxqlen > 0)
420                 is_listening = 1;
421
422         if (first) {
423                 if (!Lflag) {
424                         xo_emit("Active SCTP associations");
425                         if (aflag)
426                                 xo_emit(" (including servers)");
427                 } else
428                         xo_emit("Current listen queue sizes (qlen/maxqlen)");
429                 xo_emit("\n");
430                 if (Lflag)
431                         xo_emit("{T:/%-6.6s} {T:/%-5.5s} {T:/%-8.8s} "
432                             "{T:/%-22.22s}\n",
433                             "Proto", "Type", "Listen", "Local Address");
434                 else
435                         if (Wflag)
436                                 xo_emit("{T:/%-6.6s} {T:/%-5.5s} {T:/%-45.45s} "
437                                     "{T:/%-45.45s} {T:/%s}\n",
438                                     "Proto", "Type",
439                                     "Local Address", "Foreign Address",
440                                     "(state)");
441                         else
442                                 xo_emit("{T:/%-6.6s} {T:/%-5.5s} {T:/%-22.22s} "
443                                     "{T:/%-22.22s} {T:/%s}\n",
444                                     "Proto", "Type",
445                                     "Local Address", "Foreign Address",
446                                     "(state)");
447                 first = 0;
448         }
449         xladdr = (struct xsctp_laddr *)(buf + *offset);
450         if (Lflag && !is_listening) {
451                 sctp_skip_xinpcb_ifneed(buf, buflen, offset);
452                 return;
453         }
454
455         if (xinpcb->flags & SCTP_PCB_FLAGS_BOUND_V6) {
456                 /* Can't distinguish between sctp46 and sctp6 */
457                 pname = "sctp46";
458         } else {
459                 pname = "sctp4";
460         }
461
462         if (xinpcb->flags & SCTP_PCB_FLAGS_TCPTYPE)
463                 tname = "1to1";
464         else if (xinpcb->flags & SCTP_PCB_FLAGS_UDPTYPE)
465                 tname = "1toN";
466         else
467                 tname = "????";
468
469         if (Lflag) {
470                 char buf1[9];
471
472                 snprintf(buf1, 9, "%hu/%hu", xinpcb->qlen, xinpcb->maxqlen);
473                 xo_emit("{:protocol/%-6.6s/%s} {:type/%-5.5s/%s} ",
474                     pname, tname);
475                 xo_emit("{d:queues/%-8.8s}{e:queue-len/%hu}"
476                     "{e:max-queue-len/%hu} ",
477                     buf1, xinpcb->qlen, xinpcb->maxqlen);
478         }
479
480         offset_laddr = *offset;
481         process_closed = 0;
482
483         xo_open_list("local-address");
484 retry:
485         while (*offset < buflen) {
486                 xladdr = (struct xsctp_laddr *)(buf + *offset);
487                 *offset += sizeof(struct xsctp_laddr);
488                 if (xladdr->last) {
489                         if (aflag && !Lflag && (xladdr_total == 0) && process_closed) {
490                                 xo_open_instance("local-address");
491
492                                 xo_emit("{:protocol/%-6.6s/%s} "
493                                     "{:type/%-5.5s/%s} ", pname, tname);
494                                 if (Wflag) {
495                                         xo_emit("{P:/%-91.91s/%s} "
496                                             "{:state/CLOSED}", " ");
497                                 } else {
498                                         xo_emit("{P:/%-45.45s/%s} "
499                                             "{:state/CLOSED}", " ");
500                                 }
501                                 xo_close_instance("local-address");
502                         }
503                         if (process_closed || is_listening) {
504                                 xo_emit("\n");
505                         }
506                         break;
507                 }
508
509                 if (!Lflag && !is_listening && !process_closed)
510                         continue;
511
512                 xo_open_instance("local-address");
513
514                 if (xladdr_total == 0) {
515                         xo_emit("{:protocol/%-6.6s/%s} {:type/%-5.5s/%s} ",
516                             pname, tname);
517                 } else {
518                         xo_emit("\n");
519                         xo_emit(Lflag ? "{P:/%-21.21s} " : "{P:/%-12.12s} ",
520                             " ");
521                 }
522                 sctp_print_address("local", &(xladdr->address),
523                     htons(xinpcb->local_port), numeric_port);
524                 if (aflag && !Lflag && xladdr_total == 0) {
525                         if (Wflag) {
526                                 if (process_closed) {
527                                         xo_emit("{P:/%-45.45s} "
528                                             "{:state/CLOSED}", " ");
529                                 } else {
530                                         xo_emit("{P:/%-45.45s} "
531                                             "{:state:LISTEN}", " ");
532                                 }
533                         } else {
534                                 if (process_closed) {
535                                         xo_emit("{P:/%-22.22s} "
536                                             "{:state/CLOSED}", " ");
537                                 } else {
538                                         xo_emit("{P:/%-22.22s} "
539                                             "{:state/LISTEN}", " ");
540                                 }
541                         }
542                 }
543                 xladdr_total++;
544                 xo_close_instance("local-address");
545         }
546
547         xstcb = (struct xsctp_tcb *)(buf + *offset);
548         *offset += sizeof(struct xsctp_tcb);
549         if (aflag && (xladdr_total == 0) && xstcb->last && !process_closed) {
550                 process_closed = 1;
551                 *offset = offset_laddr;
552                 goto retry;
553         }
554         while (xstcb->last == 0 && *offset < buflen) {
555                 xo_emit("{:protocol/%-6.6s/%s} {:type/%-5.5s/%s} ",
556                     pname, tname);
557                 sctp_process_tcb(xstcb, buf, buflen, offset, &indent);
558                 indent++;
559                 xstcb = (struct xsctp_tcb *)(buf + *offset);
560                 *offset += sizeof(struct xsctp_tcb);
561         }
562
563         xo_close_list("local-address");
564 }
565
566 /*
567  * Print a summary of SCTP connections related to an Internet
568  * protocol.
569  */
570 void
571 sctp_protopr(u_long off __unused,
572     const char *name __unused, int af1 __unused, int proto)
573 {
574         char *buf;
575         const char *mibvar = "net.inet.sctp.assoclist";
576         size_t offset = 0;
577         size_t len = 0;
578         struct xsctp_inpcb *xinpcb;
579
580         if (proto != IPPROTO_SCTP)
581                 return;
582
583         if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
584                 if (errno != ENOENT)
585                         xo_warn("sysctl: %s", mibvar);
586                 return;
587         }
588         if ((buf = malloc(len)) == 0) {
589                 xo_warnx("malloc %lu bytes", (u_long)len);
590                 return;
591         }
592         if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
593                 xo_warn("sysctl: %s", mibvar);
594                 free(buf);
595                 return;
596         }
597
598         xinpcb = (struct xsctp_inpcb *)(buf + offset);
599         offset += sizeof(struct xsctp_inpcb);
600         while (xinpcb->last == 0 && offset < len) {
601                 sctp_process_inpcb(xinpcb, buf, (const size_t)len,
602                     &offset);
603
604                 xinpcb = (struct xsctp_inpcb *)(buf + offset);
605                 offset += sizeof(struct xsctp_inpcb);
606         }
607
608         free(buf);
609 }
610
611 static void
612 sctp_statesprint(uint32_t state)
613 {
614         int idx;
615
616         switch (state) {
617         case SCTP_STATE_COOKIE_WAIT:
618                 idx = NETSTAT_SCTP_STATES_COOKIE_WAIT;
619                 break;
620         case SCTP_STATE_COOKIE_ECHOED:
621                 idx = NETSTAT_SCTP_STATES_COOKIE_ECHOED;
622                 break;
623         case SCTP_STATE_OPEN:
624                 idx = NETSTAT_SCTP_STATES_ESTABLISHED;
625                 break;
626         case SCTP_STATE_SHUTDOWN_SENT:
627                 idx = NETSTAT_SCTP_STATES_SHUTDOWN_SENT;
628                 break;
629         case SCTP_STATE_SHUTDOWN_RECEIVED:
630                 idx = NETSTAT_SCTP_STATES_SHUTDOWN_RECEIVED;
631                 break;
632         case SCTP_STATE_SHUTDOWN_ACK_SENT:
633                 idx = NETSTAT_SCTP_STATES_SHUTDOWN_ACK_SENT;
634                 break;
635         case SCTP_STATE_SHUTDOWN_PENDING:
636                 idx = NETSTAT_SCTP_STATES_SHUTDOWN_PENDING;
637                 break;
638         default:
639                 xo_emit("UNKNOWN {:state/0x%08x}", state);
640                 return;
641         }
642
643         xo_emit("{:state/%s}", sctpstates[idx]);
644 }
645
646 /*
647  * Dump SCTP statistics structure.
648  */
649 void
650 sctp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
651 {
652         struct sctpstat sctpstat, zerostat;
653         size_t len = sizeof(sctpstat);
654
655         if (live) {
656                 if (zflag)
657                         memset(&zerostat, 0, len);
658                 if (sysctlbyname("net.inet.sctp.stats", &sctpstat, &len,
659                     zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
660                         if (errno != ENOENT)
661                                 xo_warn("sysctl: net.inet.sctp.stats");
662                         return;
663                 }
664         } else
665                 kread(off, &sctpstat, len);
666
667         xo_open_container(name);
668         xo_emit("{T:/%s}:\n", name);
669
670 #define p(f, m) if (sctpstat.f || sflag <= 1) \
671         xo_emit(m, (uintmax_t)sctpstat.f, plural(sctpstat.f))
672 #define p1a(f, m) if (sctpstat.f || sflag <= 1) \
673         xo_emit(m, (uintmax_t)sctpstat.f)
674
675         /*
676          * input statistics
677          */
678         p(sctps_recvpackets, "\t{:received-packets/%ju} "
679             "{N:/input packet%s}\n");
680         p(sctps_recvdatagrams, "\t\t{:received-datagrams/%ju} "
681             "{N:/datagram%s}\n");
682         p(sctps_recvpktwithdata, "\t\t{:received-with-data/%ju} "
683             "{N:/packet%s that had data}\n");
684         p(sctps_recvsacks, "\t\t{:received-sack-chunks/%ju} "
685             "{N:/input SACK chunk%s}\n");
686         p(sctps_recvdata, "\t\t{:received-data-chunks/%ju} "
687             "{N:/input DATA chunk%s}\n");
688         p(sctps_recvdupdata, "\t\t{:received-duplicate-data-chunks/%ju} "
689             "{N:/duplicate DATA chunk%s}\n");
690         p(sctps_recvheartbeat, "\t\t{:received-hb-chunks/%ju} "
691             "{N:/input HB chunk%s}\n");
692         p(sctps_recvheartbeatack, "\t\t{:received-hb-ack-chunks/%ju} "
693             "{N:/HB-ACK chunk%s}\n");
694         p(sctps_recvecne, "\t\t{:received-ecne-chunks/%ju} "
695             "{N:/input ECNE chunk%s}\n");
696         p(sctps_recvauth, "\t\t{:received-auth-chunks/%ju} "
697             "{N:/input AUTH chunk%s}\n");
698         p(sctps_recvauthmissing, "\t\t{:dropped-missing-auth/%ju} "
699             "{N:/chunk%s missing AUTH}\n");
700         p(sctps_recvivalhmacid, "\t\t{:dropped-invalid-hmac/%ju} "
701             "{N:/invalid HMAC id%s received}\n");
702         p(sctps_recvivalkeyid, "\t\t{:dropped-invalid-secret/%ju} "
703             "{N:/invalid secret id%s received}\n");
704         p1a(sctps_recvauthfailed, "\t\t{:dropped-auth-failed/%ju} "
705             "{N:/auth failed}\n");
706         p1a(sctps_recvexpress, "\t\t{:received-fast-path/%ju} "
707             "{N:/fast path receives all one chunk}\n");
708         p1a(sctps_recvexpressm, "\t\t{:receives-fast-path-multipart/%ju} "
709             "{N:/fast path multi-part data}\n");
710
711         /*
712          * output statistics
713          */
714         p(sctps_sendpackets, "\t{:sent-packets/%ju} "
715             "{N:/output packet%s}\n");
716         p(sctps_sendsacks, "\t\t{:sent-sacks/%ju} "
717             "{N:/output SACK%s}\n");
718         p(sctps_senddata, "\t\t{:sent-data-chunks/%ju} "
719             "{N:/output DATA chunk%s}\n");
720         p(sctps_sendretransdata, "\t\t{:sent-retransmitted-data-chunks/%ju} "
721             "{N:/retransmitted DATA chunk%s}\n");
722         p(sctps_sendfastretrans, "\t\t"
723             "{:sent-fast-retransmitted-data-chunks/%ju} "
724             "{N:/fast retransmitted DATA chunk%s}\n");
725         p(sctps_sendmultfastretrans, "\t\t"
726             "{:sent-fast-retransmitted-data-chunk-multiple-times/%ju} "
727             "{N:/FR'%s that happened more than once to same chunk}\n");
728         p(sctps_sendheartbeat, "\t\t{:sent-hb-chunks/%ju} "
729             "{N:/output HB chunk%s}\n");
730         p(sctps_sendecne, "\t\t{:sent-ecne-chunks/%ju} "
731             "{N:/output ECNE chunk%s}\n");
732         p(sctps_sendauth, "\t\t{:sent-auth-chunks/%ju} "
733             "{N:/output AUTH chunk%s}\n");
734         p1a(sctps_senderrors, "\t\t{:send-errors/%ju} "
735             "{N:/ip_output error counter}\n");
736
737         /*
738          * PCKDROPREP statistics
739          */
740         xo_emit("\t{T:Packet drop statistics}:\n");
741         xo_open_container("drop-statistics");
742         p1a(sctps_pdrpfmbox, "\t\t{:middle-box/%ju} "
743             "{N:/from middle box}\n");
744         p1a(sctps_pdrpfehos, "\t\t{:end-host/%ju} "
745             "{N:/from end host}\n");
746         p1a(sctps_pdrpmbda, "\t\t{:with-data/%ju} "
747             "{N:/with data}\n");
748         p1a(sctps_pdrpmbct, "\t\t{:non-data/%ju} "
749             "{N:/non-data, non-endhost}\n");
750         p1a(sctps_pdrpbwrpt, "\t\t{:non-endhost/%ju} "
751             "{N:/non-endhost, bandwidth rep only}\n");
752         p1a(sctps_pdrpcrupt, "\t\t{:short-header/%ju} "
753             "{N:/not enough for chunk header}\n");
754         p1a(sctps_pdrpnedat, "\t\t{:short-data/%ju} "
755             "{N:/not enough data to confirm}\n");
756         p1a(sctps_pdrppdbrk, "\t\t{:chunk-break/%ju} "
757             "{N:/where process_chunk_drop said break}\n");
758         p1a(sctps_pdrptsnnf, "\t\t{:tsn-not-found/%ju} "
759             "{N:/failed to find TSN}\n");
760         p1a(sctps_pdrpdnfnd, "\t\t{:reverse-tsn/%ju} "
761             "{N:/attempt reverse TSN lookup}\n");
762         p1a(sctps_pdrpdiwnp, "\t\t{:confirmed-zero-window/%ju} "
763             "{N:/e-host confirms zero-rwnd}\n");
764         p1a(sctps_pdrpdizrw, "\t\t{:middle-box-no-space/%ju} "
765             "{N:/midbox confirms no space}\n");
766         p1a(sctps_pdrpbadd, "\t\t{:bad-data/%ju} "
767             "{N:/data did not match TSN}\n");
768         p(sctps_pdrpmark, "\t\t{:tsn-marked-fast-retransmission/%ju} "
769             "{N:/TSN'%s marked for Fast Retran}\n");
770         xo_close_container("drop-statistics");
771
772         /*
773          * Timeouts
774          */
775         xo_emit("\t{T:Timeouts}:\n");
776         xo_open_container("timeouts");
777         p(sctps_timoiterator, "\t\t{:iterator/%ju} "
778             "{N:/iterator timer%s fired}\n");
779         p(sctps_timodata, "\t\t{:t3-data/%ju} "
780             "{N:/T3 data time out%s}\n");
781         p(sctps_timowindowprobe, "\t\t{:window-probe/%ju} "
782             "{N:/window probe (T3) timer%s fired}\n");
783         p(sctps_timoinit, "\t\t{:init-timer/%ju} "
784             "{N:/INIT timer%s fired}\n");
785         p(sctps_timosack, "\t\t{:sack-timer/%ju} "
786             "{N:/sack timer%s fired}\n");
787         p(sctps_timoshutdown, "\t\t{:shutdown-timer/%ju} "
788             "{N:/shutdown timer%s fired}\n");
789         p(sctps_timoheartbeat, "\t\t{:heartbeat-timer/%ju} "
790             "{N:/heartbeat timer%s fired}\n");
791         p1a(sctps_timocookie, "\t\t{:cookie-timer/%ju} "
792             "{N:/a cookie timeout fired}\n");
793         p1a(sctps_timosecret, "\t\t{:endpoint-changed-cookie/%ju} "
794             "{N:/an endpoint changed its cook}ie"
795             "secret\n");
796         p(sctps_timopathmtu, "\t\t{:pmtu-timer/%ju} "
797             "{N:/PMTU timer%s fired}\n");
798         p(sctps_timoshutdownack, "\t\t{:shutdown-timer/%ju} "
799             "{N:/shutdown ack timer%s fired}\n");
800         p(sctps_timoshutdownguard, "\t\t{:shutdown-guard-timer/%ju} "
801             "{N:/shutdown guard timer%s fired}\n");
802         p(sctps_timostrmrst, "\t\t{:stream-reset-timer/%ju} "
803             "{N:/stream reset timer%s fired}\n");
804         p(sctps_timoearlyfr, "\t\t{:early-fast-retransmission-timer/%ju} "
805             "{N:/early FR timer%s fired}\n");
806         p1a(sctps_timoasconf, "\t\t{:asconf-timer/%ju} "
807             "{N:/an asconf timer fired}\n");
808         p1a(sctps_timoautoclose, "\t\t{:auto-close-timer/%ju} "
809             "{N:/auto close timer fired}\n");
810         p(sctps_timoassockill, "\t\t{:asoc-free-timer/%ju} "
811             "{N:/asoc free timer%s expired}\n");
812         p(sctps_timoinpkill, "\t\t{:input-free-timer/%ju} "
813             "{N:/inp free timer%s expired}\n");
814         xo_close_container("timeouts");
815
816 #if 0
817         /*
818          * Early fast retransmission counters
819          */
820         p(sctps_earlyfrstart, "\t%ju TODO:sctps_earlyfrstart\n");
821         p(sctps_earlyfrstop, "\t%ju TODO:sctps_earlyfrstop\n");
822         p(sctps_earlyfrmrkretrans, "\t%ju TODO:sctps_earlyfrmrkretrans\n");
823         p(sctps_earlyfrstpout, "\t%ju TODO:sctps_earlyfrstpout\n");
824         p(sctps_earlyfrstpidsck1, "\t%ju TODO:sctps_earlyfrstpidsck1\n");
825         p(sctps_earlyfrstpidsck2, "\t%ju TODO:sctps_earlyfrstpidsck2\n");
826         p(sctps_earlyfrstpidsck3, "\t%ju TODO:sctps_earlyfrstpidsck3\n");
827         p(sctps_earlyfrstpidsck4, "\t%ju TODO:sctps_earlyfrstpidsck4\n");
828         p(sctps_earlyfrstrid, "\t%ju TODO:sctps_earlyfrstrid\n");
829         p(sctps_earlyfrstrout, "\t%ju TODO:sctps_earlyfrstrout\n");
830         p(sctps_earlyfrstrtmr, "\t%ju TODO:sctps_earlyfrstrtmr\n");
831 #endif
832
833         /*
834          * Others
835          */
836         p1a(sctps_hdrops, "\t{:dropped-too-short/%ju} "
837             "{N:/packet shorter than header}\n");
838         p1a(sctps_badsum, "\t{:dropped-bad-checksum/%ju} "
839             "{N:/checksum error}\n");
840         p1a(sctps_noport, "\t{:dropped-no-endpoint/%ju} "
841             "{N:/no endpoint for port}\n");
842         p1a(sctps_badvtag, "\t{:dropped-bad-v-tag/%ju} "
843             "{N:/bad v-tag}\n");
844         p1a(sctps_badsid, "\t{:dropped-bad-sid/%ju} "
845             "{N:/bad SID}\n");
846         p1a(sctps_nomem, "\t{:dropped-no-memory/%ju} "
847             "{N:/no memory}\n");
848         p1a(sctps_fastretransinrtt, "\t{:multiple-fast-retransmits-in-rtt/%ju} "
849             "{N:/number of multiple FR in a RT}T window\n");
850 #if 0
851         p(sctps_markedretrans, "\t%ju TODO:sctps_markedretrans\n");
852 #endif
853         p1a(sctps_naglesent, "\t{:rfc813-sent/%ju} "
854             "{N:/RFC813 allowed sending}\n");
855         p1a(sctps_naglequeued, "\t{:rfc813-queued/%ju} "
856             "{N:/RFC813 does not allow sending}\n");
857         p1a(sctps_maxburstqueued, "\t{:max-burst-queued/%ju} "
858             "{N:/times max burst prohibited sending}\n");
859         p1a(sctps_ifnomemqueued, "\t{:no-memory-in-interface/%ju} "
860             "{N:/look ahead tells us no memory in interface}\n");
861         p(sctps_windowprobed, "\t{:sent-window-probes/%ju} "
862             "{N:/number%s of window probes sent}\n");
863         p(sctps_lowlevelerr, "\t{:low-level-err/%ju} "
864             "{N:/time%s an output error to clamp down on next user send}\n");
865         p(sctps_lowlevelerrusr, "\t{:low-level-user-error/%ju} "
866             "{N:/time%s sctp_senderrors were caused from a user}\n");
867         p(sctps_datadropchklmt, "\t{:dropped-chunk-limit/%ju} "
868             "{N:/number of in data drop%s due to chunk limit reached}\n");
869         p(sctps_datadroprwnd, "\t{:dropped-rwnd-limit/%ju} "
870             "{N:/number of in data drop%s due to rwnd limit reached}\n");
871         p(sctps_ecnereducedcwnd, "\t{:ecn-reduced-cwnd/%ju} "
872             "{N:/time%s a ECN reduced the cwnd}\n");
873         p1a(sctps_vtagexpress, "\t{:v-tag-express-lookup/%ju} "
874             "{N:/used express lookup via vtag}\n");
875         p1a(sctps_vtagbogus, "\t{:v-tag-collision/%ju} "
876             "{N:/collision in express lookup}\n");
877         p(sctps_primary_randry, "\t{:sender-ran-dry/%ju} "
878             "{N:/time%s the sender ran dry of user data on primary}\n");
879         p1a(sctps_cmt_randry, "\t{:cmt-ran-dry/%ju} "
880             "{N:/same for above}\n");
881         p(sctps_slowpath_sack, "\t{:slow-path-sack/%ju} "
882             "{N:/sack%s the slow way}\n");
883         p(sctps_wu_sacks_sent, "\t{:sent-window-update-only-sack/%ju} "
884             "{N:/window update only sack%s sent}\n");
885         p(sctps_sends_with_flags, "\t{:sent-with-sinfo/%ju} "
886             "{N:/send%s with sinfo_flags !=0}\n");
887         p(sctps_sends_with_unord, "\t{:sent-with-unordered/%ju} "
888             "{N:/unordered send%s}\n");
889         p(sctps_sends_with_eof, "\t{:sent-with-eof/%ju} "
890             "{N:/send%s with EOF flag set}\n");
891         p(sctps_sends_with_abort, "\t{:sent-with-abort/%ju} "
892             "{N:/send%s with ABORT flag set}\n");
893         p(sctps_protocol_drain_calls, "\t{:protocol-drain-called/%ju} "
894             "{N:/time%s protocol drain called}\n");
895         p(sctps_protocol_drains_done, "\t{:protocol-drain/%ju} "
896             "{N:/time%s we did a protocol drain}\n");
897         p(sctps_read_peeks, "\t{:read-with-peek/%ju} "
898             "{N:/time%s recv was called with peek}\n");
899         p(sctps_cached_chk, "\t{:cached-chunks/%ju} "
900             "{N:/cached chunk%s used}\n");
901         p1a(sctps_cached_strmoq, "\t{:cached-output-queue-used/%ju} "
902             "{N:/cached stream oq's used}\n");
903         p(sctps_left_abandon, "\t{:messages-abandoned/%ju} "
904             "{N:/unread message%s abandonded by close}\n");
905         p1a(sctps_send_burst_avoid, "\t{:send-burst-avoidance/%ju} "
906             "{N:/send burst avoidance, already max burst inflight to net}\n");
907         p1a(sctps_send_cwnd_avoid, "\t{:send-cwnd-avoidance/%ju} "
908             "{N:/send cwnd full avoidance, already max burst inflight "
909             "to net}\n");
910         p(sctps_fwdtsn_map_over, "\t{:tsn-map-overruns/%ju} "
911            "{N:/number of map array over-run%s via fwd-tsn's}\n");
912
913 #undef p
914 #undef p1a
915         xo_close_container(name);
916 }
917
918 #endif /* SCTP */