source: rtems-libbsd/freebsd-userspace/commands/usr.bin/netstat/inet.c @ f1710b6

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since f1710b6 was f1710b6, checked in by Joel Sherrill <joel.sherrill@…>, on 09/07/12 at 17:50:18

netstat: New command - Almost completely compiles

Currently disable in Makefile until all builds. Adding it so
others can assist in debugging issues.

  • Property mode set to 100644
File size: 37.9 KB
Line 
1/*-
2 * Copyright (c) 1983, 1988, 1993, 1995
3 *      The Regents of the University of California.  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
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 *    must display the following acknowledgement:
15 *      This product includes software developed by the University of
16 *      California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 *    may be used to endorse or promote products derived from this software
19 *    without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34#if 0
35#ifndef lint
36static char sccsid[] = "@(#)inet.c      8.5 (Berkeley) 5/24/95";
37#endif /* not lint */
38#endif
39
40#include <sys/cdefs.h>
41__FBSDID("$FreeBSD$");
42
43#include <sys/param.h>
44#include <sys/queue.h>
45#ifdef __rtems__
46#include <freebsd/sys/domain.h>
47#include <freebsd/sys/protosw.h>
48#else
49#include <sys/domain.h>
50#include <sys/protosw.h>
51#endif
52#include <sys/socket.h>
53#include <sys/socketvar.h>
54#include <sys/sysctl.h>
55
56#include <net/route.h>
57#include <net/if_arp.h>
58#include <netinet/in.h>
59#include <netinet/in_systm.h>
60#include <netinet/ip.h>
61#ifdef __rtems__
62#include <freebsd/netinet/ip_carp.h>
63#else
64#include <netinet/ip_carp.h>
65#endif
66#ifdef INET6
67#include <netinet/ip6.h>
68#endif /* INET6 */
69#include <netinet/in_pcb.h>
70#include <netinet/ip_icmp.h>
71#include <netinet/icmp_var.h>
72#include <netinet/igmp_var.h>
73#include <netinet/ip_var.h>
74#include <netinet/pim_var.h>
75#include <netinet/tcp.h>
76#include <netinet/tcpip.h>
77#include <netinet/tcp_seq.h>
78#define TCPSTATES
79#include <netinet/tcp_fsm.h>
80#include <netinet/tcp_timer.h>
81#include <netinet/tcp_var.h>
82#include <netinet/tcp_debug.h>
83#include <netinet/udp.h>
84#include <netinet/udp_var.h>
85
86#include <arpa/inet.h>
87#include <err.h>
88#include <errno.h>
89#include <libutil.h>
90#include <netdb.h>
91#include <stdint.h>
92#include <stdio.h>
93#include <stdlib.h>
94#include <string.h>
95#include <unistd.h>
96#include "netstat.h"
97
98char    *inetname(struct in_addr *);
99void    inetprint(struct in_addr *, int, const char *, int);
100#ifdef INET6
101static int udp_done, tcp_done;
102#endif /* INET6 */
103
104static int
105pcblist_sysctl(int proto, char **bufp, int istcp)
106{
107        const char *mibvar;
108        char *buf;
109        size_t len;
110
111        switch (proto) {
112        case IPPROTO_TCP:
113                mibvar = "net.inet.tcp.pcblist";
114                break;
115        case IPPROTO_UDP:
116                mibvar = "net.inet.udp.pcblist";
117                break;
118        case IPPROTO_DIVERT:
119                mibvar = "net.inet.divert.pcblist";
120                break;
121        default:
122                mibvar = "net.inet.raw.pcblist";
123                break;
124        }
125
126        len = 0;
127        if (sysctlbyname(mibvar, 0, &len, 0, 0) < 0) {
128                if (errno != ENOENT)
129                        warn("sysctl: %s", mibvar);
130                return (0);
131        }
132        if ((buf = malloc(len)) == 0) {
133                warnx("malloc %lu bytes", (u_long)len);
134                return (0);
135        }
136        if (sysctlbyname(mibvar, buf, &len, 0, 0) < 0) {
137                warn("sysctl: %s", mibvar);
138                free(buf);
139                return (0);
140        }
141        *bufp = buf;
142        return (1);
143}
144
145/*
146 * Copied directly from uipc_socket2.c.  We leave out some fields that are in
147 * nested structures that aren't used to avoid extra work.
148 */
149static void
150sbtoxsockbuf(struct sockbuf *sb, struct xsockbuf *xsb)
151{
152        xsb->sb_cc = sb->sb_cc;
153        xsb->sb_hiwat = sb->sb_hiwat;
154        xsb->sb_mbcnt = sb->sb_mbcnt;
155        xsb->sb_mcnt = sb->sb_mcnt;
156        xsb->sb_ccnt = sb->sb_ccnt;
157        xsb->sb_mbmax = sb->sb_mbmax;
158        xsb->sb_lowat = sb->sb_lowat;
159        xsb->sb_flags = sb->sb_flags;
160        xsb->sb_timeo = sb->sb_timeo;
161}
162
163int
164sotoxsocket(struct socket *so, struct xsocket *xso)
165{
166        struct protosw proto;
167        struct domain domain;
168
169        bzero(xso, sizeof *xso);
170        xso->xso_len = sizeof *xso;
171        xso->xso_so = so;
172        xso->so_type = so->so_type;
173        xso->so_options = so->so_options;
174        xso->so_linger = so->so_linger;
175        xso->so_state = so->so_state;
176        xso->so_pcb = so->so_pcb;
177        if (kread((uintptr_t)so->so_proto, &proto, sizeof(proto)) != 0)
178                return (-1);
179        xso->xso_protocol = proto.pr_protocol;
180        if (kread((uintptr_t)proto.pr_domain, &domain, sizeof(domain)) != 0)
181                return (-1);
182        xso->xso_family = domain.dom_family;
183        xso->so_qlen = so->so_qlen;
184        xso->so_incqlen = so->so_incqlen;
185        xso->so_qlimit = so->so_qlimit;
186        xso->so_timeo = so->so_timeo;
187        xso->so_error = so->so_error;
188        xso->so_oobmark = so->so_oobmark;
189        sbtoxsockbuf(&so->so_snd, &xso->so_snd);
190        sbtoxsockbuf(&so->so_rcv, &xso->so_rcv);
191        return (0);
192}
193
194static int
195pcblist_kvm(u_long off, char **bufp, int istcp)
196{
197        struct inpcbinfo pcbinfo;
198        struct inpcbhead listhead;
199        struct inpcb *inp;
200        struct xinpcb xi;
201        struct xinpgen xig;
202        struct xtcpcb xt;
203        struct socket so;
204        struct xsocket *xso;
205        char *buf, *p;
206        size_t len;
207
208        if (off == 0)
209                return (0);
210        kread(off, &pcbinfo, sizeof(pcbinfo));
211        if (istcp)
212                len = 2 * sizeof(xig) +
213                    (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
214                    sizeof(struct xtcpcb);
215        else
216                len = 2 * sizeof(xig) +
217                    (pcbinfo.ipi_count + pcbinfo.ipi_count / 8) *
218                    sizeof(struct xinpcb);
219        if ((buf = malloc(len)) == 0) {
220                warnx("malloc %lu bytes", (u_long)len);
221                return (0);
222        }
223        p = buf;
224
225#define COPYOUT(obj, size) do {                                         \
226        if (len < (size)) {                                             \
227                warnx("buffer size exceeded");                          \
228                goto fail;                                              \
229        }                                                               \
230        bcopy((obj), p, (size));                                        \
231        len -= (size);                                                  \
232        p += (size);                                                    \
233} while (0)
234
235#define KREAD(off, buf, len) do {                                       \
236        if (kread((uintptr_t)(off), (buf), (len)) != 0)                 \
237                goto fail;                                              \
238} while (0)
239
240        /* Write out header. */
241        xig.xig_len = sizeof xig;
242        xig.xig_count = pcbinfo.ipi_count;
243        xig.xig_gen = pcbinfo.ipi_gencnt;
244        xig.xig_sogen = 0;
245        COPYOUT(&xig, sizeof xig);
246
247        /* Walk the PCB list. */
248        xt.xt_len = sizeof xt;
249        xi.xi_len = sizeof xi;
250        if (istcp)
251                xso = &xt.xt_socket;
252        else
253                xso = &xi.xi_socket;
254        KREAD(pcbinfo.ipi_listhead, &listhead, sizeof(listhead));
255        LIST_FOREACH(inp, &listhead, inp_list) {
256                if (istcp) {
257                        KREAD(inp, &xt.xt_inp, sizeof(*inp));
258                        inp = &xt.xt_inp;
259                } else {
260                        KREAD(inp, &xi.xi_inp, sizeof(*inp));
261                        inp = &xi.xi_inp;
262                }
263
264                if (inp->inp_gencnt > pcbinfo.ipi_gencnt)
265                        continue;
266
267                if (istcp) {
268                        if (inp->inp_ppcb == NULL)
269                                bzero(&xt.xt_tp, sizeof xt.xt_tp);
270                        else if (inp->inp_flags & INP_TIMEWAIT) {
271                                bzero(&xt.xt_tp, sizeof xt.xt_tp);
272                                xt.xt_tp.t_state = TCPS_TIME_WAIT;
273                        } else
274                                KREAD(inp->inp_ppcb, &xt.xt_tp,
275                                    sizeof xt.xt_tp);
276                }
277                if (inp->inp_socket) {
278                        KREAD(inp->inp_socket, &so, sizeof(so));
279                        if (sotoxsocket(&so, xso) != 0)
280                                goto fail;
281                } else {
282                        bzero(xso, sizeof(*xso));
283                        if (istcp)
284                                xso->xso_protocol = IPPROTO_TCP;
285                }
286                if (istcp)
287                        COPYOUT(&xt, sizeof xt);
288                else
289                        COPYOUT(&xi, sizeof xi);
290        }
291
292        /* Reread the pcbinfo and write out the footer. */
293        kread(off, &pcbinfo, sizeof(pcbinfo));
294        xig.xig_count = pcbinfo.ipi_count;
295        xig.xig_gen = pcbinfo.ipi_gencnt;
296        COPYOUT(&xig, sizeof xig);
297
298        *bufp = buf;
299        return (1);
300
301fail:
302        free(buf);
303        return (0);
304#undef COPYOUT
305#undef KREAD
306}
307
308/*
309 * Print a summary of connections related to an Internet
310 * protocol.  For TCP, also give state of connection.
311 * Listening processes (aflag) are suppressed unless the
312 * -a (all) flag is specified.
313 */
314void
315protopr(u_long off, const char *name, int af1, int proto)
316{
317        int istcp;
318        static int first = 1;
319        char *buf;
320        const char *vchar;
321        struct tcpcb *tp = NULL;
322        struct inpcb *inp;
323        struct xinpgen *xig, *oxig;
324        struct xsocket *so;
325
326        istcp = 0;
327        switch (proto) {
328        case IPPROTO_TCP:
329#ifdef INET6
330                if (tcp_done != 0)
331                        return;
332                else
333                        tcp_done = 1;
334#endif
335                istcp = 1;
336                break;
337        case IPPROTO_UDP:
338#ifdef INET6
339                if (udp_done != 0)
340                        return;
341                else
342                        udp_done = 1;
343#endif
344                break;
345        }
346        if (live) {
347                if (!pcblist_sysctl(proto, &buf, istcp))
348                        return;
349        } else {
350                if (!pcblist_kvm(off, &buf, istcp))
351                        return;
352        }
353
354        oxig = xig = (struct xinpgen *)buf;
355        for (xig = (struct xinpgen *)((char *)xig + xig->xig_len);
356             xig->xig_len > sizeof(struct xinpgen);
357             xig = (struct xinpgen *)((char *)xig + xig->xig_len)) {
358                if (istcp) {
359                        tp = &((struct xtcpcb *)xig)->xt_tp;
360                        inp = &((struct xtcpcb *)xig)->xt_inp;
361                        so = &((struct xtcpcb *)xig)->xt_socket;
362                } else {
363                        inp = &((struct xinpcb *)xig)->xi_inp;
364                        so = &((struct xinpcb *)xig)->xi_socket;
365                }
366
367                /* Ignore sockets for protocols other than the desired one. */
368                if (so->xso_protocol != proto)
369                        continue;
370
371                /* Ignore PCBs which were freed during copyout. */
372                if (inp->inp_gencnt > oxig->xig_gen)
373                        continue;
374
375                if ((af1 == AF_INET && (inp->inp_vflag & INP_IPV4) == 0)
376#ifdef INET6
377                    || (af1 == AF_INET6 && (inp->inp_vflag & INP_IPV6) == 0)
378#endif /* INET6 */
379                    || (af1 == AF_UNSPEC && ((inp->inp_vflag & INP_IPV4) == 0
380#ifdef INET6
381                                          && (inp->inp_vflag & INP_IPV6) == 0
382#endif /* INET6 */
383                        ))
384                    )
385                        continue;
386                if (!aflag &&
387                    (
388                     (istcp && tp->t_state == TCPS_LISTEN)
389                     || (af1 == AF_INET &&
390                      inet_lnaof(inp->inp_laddr) == INADDR_ANY)
391#ifdef INET6
392                     || (af1 == AF_INET6 &&
393                         IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
394#endif /* INET6 */
395                     || (af1 == AF_UNSPEC &&
396                         (((inp->inp_vflag & INP_IPV4) != 0 &&
397                           inet_lnaof(inp->inp_laddr) == INADDR_ANY)
398#ifdef INET6
399                          || ((inp->inp_vflag & INP_IPV6) != 0 &&
400                              IN6_IS_ADDR_UNSPECIFIED(&inp->in6p_laddr))
401#endif
402                          ))
403                     ))
404                        continue;
405
406                if (first) {
407                        if (!Lflag) {
408                                printf("Active Internet connections");
409                                if (aflag)
410                                        printf(" (including servers)");
411                        } else
412                                printf(
413        "Current listen queue sizes (qlen/incqlen/maxqlen)");
414                        putchar('\n');
415                        if (Aflag)
416                                printf("%-8.8s ", "Tcpcb");
417                        if (Lflag)
418                                printf("%-5.5s %-14.14s %-22.22s\n",
419                                    "Proto", "Listen", "Local Address");
420                        else {
421                                printf((Aflag && !Wflag) ?
422                                       "%-5.5s %-6.6s %-6.6s  %-18.18s %-18.18s" :
423                                       "%-5.5s %-6.6s %-6.6s  %-22.22s %-22.22s",
424                                       "Proto", "Recv-Q", "Send-Q",
425                                       "Local Address", "Foreign Address");
426                                if (xflag)
427                                        printf("%-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %-6.6s %s\n",
428                                                "R-MBUF", "S-MBUF", "R-CLUS",
429                                                "S-CLUS", "R-HIWA", "S-HIWA",
430                                                "R-LOWA", "S-LOWA", "R-BCNT",
431                                                "S-BCNT", "R-BMAX", "S-BMAX",
432                                               "(state)");
433                                else
434                                        printf("(state)\n");
435                        }
436                        first = 0;
437                }
438                if (Lflag && so->so_qlimit == 0)
439                        continue;
440                if (Aflag) {
441                        if (istcp)
442                                printf("%8lx ", (u_long)inp->inp_ppcb);
443                        else
444                                printf("%8lx ", (u_long)so->so_pcb);
445                }
446#ifdef INET6
447                if ((inp->inp_vflag & INP_IPV6) != 0)
448                        vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
449                            "46" : "6 ";
450                else
451#endif
452                vchar = ((inp->inp_vflag & INP_IPV4) != 0) ?
453                    "4 " : "  ";
454                printf("%-3.3s%-2.2s ", name, vchar);
455                if (Lflag) {
456                        char buf1[15];
457
458                        snprintf(buf1, 15, "%d/%d/%d", so->so_qlen,
459                            so->so_incqlen, so->so_qlimit);
460                        printf("%-14.14s ", buf1);
461                } else {
462                        printf("%6u %6u ", so->so_rcv.sb_cc, so->so_snd.sb_cc);
463                }
464                if (numeric_port) {
465                        if (inp->inp_vflag & INP_IPV4) {
466                                inetprint(&inp->inp_laddr, (int)inp->inp_lport,
467                                    name, 1);
468                                if (!Lflag)
469                                        inetprint(&inp->inp_faddr,
470                                            (int)inp->inp_fport, name, 1);
471                        }
472#ifdef INET6
473                        else if (inp->inp_vflag & INP_IPV6) {
474                                inet6print(&inp->in6p_laddr,
475                                    (int)inp->inp_lport, name, 1);
476                                if (!Lflag)
477                                        inet6print(&inp->in6p_faddr,
478                                            (int)inp->inp_fport, name, 1);
479                        } /* else nothing printed now */
480#endif /* INET6 */
481                } else if (inp->inp_flags & INP_ANONPORT) {
482                        if (inp->inp_vflag & INP_IPV4) {
483                                inetprint(&inp->inp_laddr, (int)inp->inp_lport,
484                                    name, 1);
485                                if (!Lflag)
486                                        inetprint(&inp->inp_faddr,
487                                            (int)inp->inp_fport, name, 0);
488                        }
489#ifdef INET6
490                        else if (inp->inp_vflag & INP_IPV6) {
491                                inet6print(&inp->in6p_laddr,
492                                    (int)inp->inp_lport, name, 1);
493                                if (!Lflag)
494                                        inet6print(&inp->in6p_faddr,
495                                            (int)inp->inp_fport, name, 0);
496                        } /* else nothing printed now */
497#endif /* INET6 */
498                } else {
499                        if (inp->inp_vflag & INP_IPV4) {
500                                inetprint(&inp->inp_laddr, (int)inp->inp_lport,
501                                    name, 0);
502                                if (!Lflag)
503                                        inetprint(&inp->inp_faddr,
504                                            (int)inp->inp_fport, name,
505                                            inp->inp_lport != inp->inp_fport);
506                        }
507#ifdef INET6
508                        else if (inp->inp_vflag & INP_IPV6) {
509                                inet6print(&inp->in6p_laddr,
510                                    (int)inp->inp_lport, name, 0);
511                                if (!Lflag)
512                                        inet6print(&inp->in6p_faddr,
513                                            (int)inp->inp_fport, name,
514                                            inp->inp_lport != inp->inp_fport);
515                        } /* else nothing printed now */
516#endif /* INET6 */
517                }
518                if (xflag) {
519                        if (Lflag)
520                                printf("%21s %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ",
521                                       " ",
522                                       so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
523                                       so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
524                                       so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
525                                       so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
526                                       so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
527                                       so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
528                        else
529                                printf("%6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u %6u ",
530                                       so->so_rcv.sb_mcnt, so->so_snd.sb_mcnt,
531                                       so->so_rcv.sb_ccnt, so->so_snd.sb_ccnt,
532                                       so->so_rcv.sb_hiwat, so->so_snd.sb_hiwat,
533                                       so->so_rcv.sb_lowat, so->so_snd.sb_lowat,
534                                       so->so_rcv.sb_mbcnt, so->so_snd.sb_mbcnt,
535                                       so->so_rcv.sb_mbmax, so->so_snd.sb_mbmax);
536                }
537                if (istcp && !Lflag) {
538                        if (tp->t_state < 0 || tp->t_state >= TCP_NSTATES)
539                                printf("%d", tp->t_state);
540                        else {
541                                printf("%s", tcpstates[tp->t_state]);
542#if defined(TF_NEEDSYN) && defined(TF_NEEDFIN)
543                                /* Show T/TCP `hidden state' */
544                                if (tp->t_flags & (TF_NEEDSYN|TF_NEEDFIN))
545                                        putchar('*');
546#endif /* defined(TF_NEEDSYN) && defined(TF_NEEDFIN) */
547                        }
548                }               
549                putchar('\n');
550        }
551        if (xig != oxig && xig->xig_gen != oxig->xig_gen) {
552                if (oxig->xig_count > xig->xig_count) {
553                        printf("Some %s sockets may have been deleted.\n",
554                            name);
555                } else if (oxig->xig_count < xig->xig_count) {
556                        printf("Some %s sockets may have been created.\n",
557                            name);
558                } else {
559                        printf(
560        "Some %s sockets may have been created or deleted.\n",
561                            name);
562                }
563        }
564        free(buf);
565}
566
567/*
568 * Dump TCP statistics structure.
569 */
570void
571tcp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
572{
573        struct tcpstat tcpstat, zerostat;
574        size_t len = sizeof tcpstat;
575
576#ifdef INET6
577        if (tcp_done != 0)
578                return;
579        else
580                tcp_done = 1;
581#endif
582
583        if (live) {
584                if (zflag)
585                        memset(&zerostat, 0, len);
586                if (sysctlbyname("net.inet.tcp.stats", &tcpstat, &len,
587                    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
588                        warn("sysctl: net.inet.tcp.stats");
589                        return;
590                }
591        } else
592                kread(off, &tcpstat, len);
593
594        printf ("%s:\n", name);
595
596#define p(f, m) if (tcpstat.f || sflag <= 1) \
597    printf(m, tcpstat.f, plural(tcpstat.f))
598#define p1a(f, m) if (tcpstat.f || sflag <= 1) \
599    printf(m, tcpstat.f)
600#define p2(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
601    printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2, plural(tcpstat.f2))
602#define p2a(f1, f2, m) if (tcpstat.f1 || tcpstat.f2 || sflag <= 1) \
603    printf(m, tcpstat.f1, plural(tcpstat.f1), tcpstat.f2)
604#define p3(f, m) if (tcpstat.f || sflag <= 1) \
605    printf(m, tcpstat.f, pluralies(tcpstat.f))
606
607        p(tcps_sndtotal, "\t%lu packet%s sent\n");
608        p2(tcps_sndpack,tcps_sndbyte, "\t\t%lu data packet%s (%lu byte%s)\n");
609        p2(tcps_sndrexmitpack, tcps_sndrexmitbyte,
610            "\t\t%lu data packet%s (%lu byte%s) retransmitted\n");
611        p(tcps_sndrexmitbad,
612            "\t\t%lu data packet%s unnecessarily retransmitted\n");
613        p(tcps_mturesent, "\t\t%lu resend%s initiated by MTU discovery\n");
614        p2a(tcps_sndacks, tcps_delack,
615            "\t\t%lu ack-only packet%s (%lu delayed)\n");
616        p(tcps_sndurg, "\t\t%lu URG only packet%s\n");
617        p(tcps_sndprobe, "\t\t%lu window probe packet%s\n");
618        p(tcps_sndwinup, "\t\t%lu window update packet%s\n");
619        p(tcps_sndctrl, "\t\t%lu control packet%s\n");
620        p(tcps_rcvtotal, "\t%lu packet%s received\n");
621        p2(tcps_rcvackpack, tcps_rcvackbyte,
622            "\t\t%lu ack%s (for %lu byte%s)\n");
623        p(tcps_rcvdupack, "\t\t%lu duplicate ack%s\n");
624        p(tcps_rcvacktoomuch, "\t\t%lu ack%s for unsent data\n");
625        p2(tcps_rcvpack, tcps_rcvbyte,
626            "\t\t%lu packet%s (%lu byte%s) received in-sequence\n");
627        p2(tcps_rcvduppack, tcps_rcvdupbyte,
628            "\t\t%lu completely duplicate packet%s (%lu byte%s)\n");
629        p(tcps_pawsdrop, "\t\t%lu old duplicate packet%s\n");
630        p2(tcps_rcvpartduppack, tcps_rcvpartdupbyte,
631            "\t\t%lu packet%s with some dup. data (%lu byte%s duped)\n");
632        p2(tcps_rcvoopack, tcps_rcvoobyte,
633            "\t\t%lu out-of-order packet%s (%lu byte%s)\n");
634        p2(tcps_rcvpackafterwin, tcps_rcvbyteafterwin,
635            "\t\t%lu packet%s (%lu byte%s) of data after window\n");
636        p(tcps_rcvwinprobe, "\t\t%lu window probe%s\n");
637        p(tcps_rcvwinupd, "\t\t%lu window update packet%s\n");
638        p(tcps_rcvafterclose, "\t\t%lu packet%s received after close\n");
639        p(tcps_rcvbadsum, "\t\t%lu discarded for bad checksum%s\n");
640        p(tcps_rcvbadoff, "\t\t%lu discarded for bad header offset field%s\n");
641        p1a(tcps_rcvshort, "\t\t%lu discarded because packet too short\n");
642        p1a(tcps_rcvmemdrop, "\t\t%lu discarded due to memory problems\n");
643        p(tcps_connattempt, "\t%lu connection request%s\n");
644        p(tcps_accepts, "\t%lu connection accept%s\n");
645        p(tcps_badsyn, "\t%lu bad connection attempt%s\n");
646        p(tcps_listendrop, "\t%lu listen queue overflow%s\n");
647        p(tcps_badrst, "\t%lu ignored RSTs in the window%s\n");
648        p(tcps_connects, "\t%lu connection%s established (including accepts)\n");
649        p2(tcps_closed, tcps_drops,
650            "\t%lu connection%s closed (including %lu drop%s)\n");
651        p(tcps_cachedrtt, "\t\t%lu connection%s updated cached RTT on close\n");
652        p(tcps_cachedrttvar,
653            "\t\t%lu connection%s updated cached RTT variance on close\n");
654        p(tcps_cachedssthresh,
655            "\t\t%lu connection%s updated cached ssthresh on close\n");
656        p(tcps_conndrops, "\t%lu embryonic connection%s dropped\n");
657        p2(tcps_rttupdated, tcps_segstimed,
658            "\t%lu segment%s updated rtt (of %lu attempt%s)\n");
659        p(tcps_rexmttimeo, "\t%lu retransmit timeout%s\n");
660        p(tcps_timeoutdrop, "\t\t%lu connection%s dropped by rexmit timeout\n");
661        p(tcps_persisttimeo, "\t%lu persist timeout%s\n");
662        p(tcps_persistdrop, "\t\t%lu connection%s dropped by persist timeout\n");
663        p(tcps_finwait2_drops,
664            "\t%lu Connection%s (fin_wait_2) dropped because of timeout\n");
665        p(tcps_keeptimeo, "\t%lu keepalive timeout%s\n");
666        p(tcps_keepprobe, "\t\t%lu keepalive probe%s sent\n");
667        p(tcps_keepdrops, "\t\t%lu connection%s dropped by keepalive\n");
668        p(tcps_predack, "\t%lu correct ACK header prediction%s\n");
669        p(tcps_preddat, "\t%lu correct data packet header prediction%s\n");
670
671        p3(tcps_sc_added, "\t%lu syncache entr%s added\n");
672        p1a(tcps_sc_retransmitted, "\t\t%lu retransmitted\n");
673        p1a(tcps_sc_dupsyn, "\t\t%lu dupsyn\n");
674        p1a(tcps_sc_dropped, "\t\t%lu dropped\n");
675        p1a(tcps_sc_completed, "\t\t%lu completed\n");
676        p1a(tcps_sc_bucketoverflow, "\t\t%lu bucket overflow\n");
677        p1a(tcps_sc_cacheoverflow, "\t\t%lu cache overflow\n");
678        p1a(tcps_sc_reset, "\t\t%lu reset\n");
679        p1a(tcps_sc_stale, "\t\t%lu stale\n");
680        p1a(tcps_sc_aborted, "\t\t%lu aborted\n");
681        p1a(tcps_sc_badack, "\t\t%lu badack\n");
682        p1a(tcps_sc_unreach, "\t\t%lu unreach\n");
683        p(tcps_sc_zonefail, "\t\t%lu zone failure%s\n");
684        p(tcps_sc_sendcookie, "\t%lu cookie%s sent\n");
685        p(tcps_sc_recvcookie, "\t%lu cookie%s received\n");
686
687        p(tcps_sack_recovery_episode, "\t%lu SACK recovery episode%s\n");
688        p(tcps_sack_rexmits,
689            "\t%lu segment rexmit%s in SACK recovery episodes\n");
690        p(tcps_sack_rexmit_bytes,
691            "\t%lu byte rexmit%s in SACK recovery episodes\n");
692        p(tcps_sack_rcv_blocks,
693            "\t%lu SACK option%s (SACK blocks) received\n");
694        p(tcps_sack_send_blocks, "\t%lu SACK option%s (SACK blocks) sent\n");
695        p1a(tcps_sack_sboverflow, "\t%lu SACK scoreboard overflow\n");
696
697        p(tcps_ecn_ce, "\t%lu packet%s with ECN CE bit set\n");
698        p(tcps_ecn_ect0, "\t%lu packet%s with ECN ECT(0) bit set\n");
699        p(tcps_ecn_ect1, "\t%lu packet%s with ECN ECT(1) bit set\n");
700        p(tcps_ecn_shs, "\t%lu successful ECN handshake%s\n");
701        p(tcps_ecn_rcwnd, "\t%lu time%s ECN reduced the congestion window\n");
702#undef p
703#undef p1a
704#undef p2
705#undef p2a
706#undef p3
707}
708
709/*
710 * Dump UDP statistics structure.
711 */
712void
713udp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
714{
715        struct udpstat udpstat, zerostat;
716        size_t len = sizeof udpstat;
717        u_long delivered;
718
719#ifdef INET6
720        if (udp_done != 0)
721                return;
722        else
723                udp_done = 1;
724#endif
725
726        if (live) {
727                if (zflag)
728                        memset(&zerostat, 0, len);
729                if (sysctlbyname("net.inet.udp.stats", &udpstat, &len,
730                    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
731                        warn("sysctl: net.inet.udp.stats");
732                        return;
733                }
734        } else
735                kread(off, &udpstat, len);
736
737        printf("%s:\n", name);
738#define p(f, m) if (udpstat.f || sflag <= 1) \
739    printf(m, udpstat.f, plural(udpstat.f))
740#define p1a(f, m) if (udpstat.f || sflag <= 1) \
741    printf(m, udpstat.f)
742        p(udps_ipackets, "\t%lu datagram%s received\n");
743        p1a(udps_hdrops, "\t%lu with incomplete header\n");
744        p1a(udps_badlen, "\t%lu with bad data length field\n");
745        p1a(udps_badsum, "\t%lu with bad checksum\n");
746        p1a(udps_nosum, "\t%lu with no checksum\n");
747        p1a(udps_noport, "\t%lu dropped due to no socket\n");
748        p(udps_noportbcast,
749            "\t%lu broadcast/multicast datagram%s undelivered\n");
750        p1a(udps_fullsock, "\t%lu dropped due to full socket buffers\n");
751        p1a(udpps_pcbhashmiss, "\t%lu not for hashed pcb\n");
752        delivered = udpstat.udps_ipackets -
753                    udpstat.udps_hdrops -
754                    udpstat.udps_badlen -
755                    udpstat.udps_badsum -
756                    udpstat.udps_noport -
757                    udpstat.udps_noportbcast -
758                    udpstat.udps_fullsock;
759        if (delivered || sflag <= 1)
760                printf("\t%lu delivered\n", delivered);
761        p(udps_opackets, "\t%lu datagram%s output\n");
762        /* the next statistic is cumulative in udps_noportbcast */
763        p(udps_filtermcast,
764            "\t%lu time%s multicast source filter matched\n");
765#undef p
766#undef p1a
767}
768
769/*
770 * Dump CARP statistics structure.
771 */
772void
773carp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
774{
775        struct carpstats carpstat, zerostat;
776        size_t len = sizeof(struct carpstats);
777
778        if (live) {
779                if (zflag)
780                        memset(&zerostat, 0, len);
781                if (sysctlbyname("net.inet.carp.stats", &carpstat, &len,
782                    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
783                        if (errno != ENOENT)
784                                warn("sysctl: net.inet.carp.stats");
785                        return;
786                }
787        } else {
788                if (off == 0)
789                        return;
790                kread(off, &carpstat, len);
791        }
792
793        printf("%s:\n", name);
794
795#define p(f, m) if (carpstat.f || sflag <= 1) \
796        printf(m, (uintmax_t)carpstat.f, plural(carpstat.f))
797#define p2(f, m) if (carpstat.f || sflag <= 1) \
798        printf(m, (uintmax_t)carpstat.f)
799
800        p(carps_ipackets, "\t%ju packet%s received (IPv4)\n");
801        p(carps_ipackets6, "\t%ju packet%s received (IPv6)\n");
802        p(carps_badttl, "\t\t%ju packet%s discarded for wrong TTL\n");
803        p(carps_hdrops, "\t\t%ju packet%s shorter than header\n");
804        p(carps_badsum, "\t\t%ju discarded for bad checksum%s\n");
805        p(carps_badver, "\t\t%ju discarded packet%s with a bad version\n");
806        p2(carps_badlen, "\t\t%ju discarded because packet too short\n");
807        p2(carps_badauth, "\t\t%ju discarded for bad authentication\n");
808        p2(carps_badvhid, "\t\t%ju discarded for bad vhid\n");
809        p2(carps_badaddrs, "\t\t%ju discarded because of a bad address list\n");
810        p(carps_opackets, "\t%ju packet%s sent (IPv4)\n");
811        p(carps_opackets6, "\t%ju packet%s sent (IPv6)\n");
812        p2(carps_onomem, "\t\t%ju send failed due to mbuf memory error\n");
813#if notyet
814        p(carps_ostates, "\t\t%s state update%s sent\n");
815#endif
816#undef p
817#undef p2
818}
819
820/*
821 * Dump IP statistics structure.
822 */
823void
824ip_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
825{
826        struct ipstat ipstat, zerostat;
827        size_t len = sizeof ipstat;
828
829        if (live) {
830                if (zflag)
831                        memset(&zerostat, 0, len);
832                if (sysctlbyname("net.inet.ip.stats", &ipstat, &len,
833                    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
834                        warn("sysctl: net.inet.ip.stats");
835                        return;
836                }
837        } else
838                kread(off, &ipstat, len);
839
840        printf("%s:\n", name);
841
842#define p(f, m) if (ipstat.f || sflag <= 1) \
843    printf(m, ipstat.f, plural(ipstat.f))
844#define p1a(f, m) if (ipstat.f || sflag <= 1) \
845    printf(m, ipstat.f)
846
847        p(ips_total, "\t%lu total packet%s received\n");
848        p(ips_badsum, "\t%lu bad header checksum%s\n");
849        p1a(ips_toosmall, "\t%lu with size smaller than minimum\n");
850        p1a(ips_tooshort, "\t%lu with data size < data length\n");
851        p1a(ips_toolong, "\t%lu with ip length > max ip packet size\n");
852        p1a(ips_badhlen, "\t%lu with header length < data size\n");
853        p1a(ips_badlen, "\t%lu with data length < header length\n");
854        p1a(ips_badoptions, "\t%lu with bad options\n");
855        p1a(ips_badvers, "\t%lu with incorrect version number\n");
856        p(ips_fragments, "\t%lu fragment%s received\n");
857        p(ips_fragdropped, "\t%lu fragment%s dropped (dup or out of space)\n");
858        p(ips_fragtimeout, "\t%lu fragment%s dropped after timeout\n");
859        p(ips_reassembled, "\t%lu packet%s reassembled ok\n");
860        p(ips_delivered, "\t%lu packet%s for this host\n");
861        p(ips_noproto, "\t%lu packet%s for unknown/unsupported protocol\n");
862        p(ips_forward, "\t%lu packet%s forwarded");
863        p(ips_fastforward, " (%lu packet%s fast forwarded)");
864        if (ipstat.ips_forward || sflag <= 1)
865                putchar('\n');
866        p(ips_cantforward, "\t%lu packet%s not forwardable\n");
867        p(ips_notmember,
868            "\t%lu packet%s received for unknown multicast group\n");
869        p(ips_redirectsent, "\t%lu redirect%s sent\n");
870        p(ips_localout, "\t%lu packet%s sent from this host\n");
871        p(ips_rawout, "\t%lu packet%s sent with fabricated ip header\n");
872        p(ips_odropped,
873            "\t%lu output packet%s dropped due to no bufs, etc.\n");
874        p(ips_noroute, "\t%lu output packet%s discarded due to no route\n");
875        p(ips_fragmented, "\t%lu output datagram%s fragmented\n");
876        p(ips_ofragments, "\t%lu fragment%s created\n");
877        p(ips_cantfrag, "\t%lu datagram%s that can't be fragmented\n");
878        p(ips_nogif, "\t%lu tunneling packet%s that can't find gif\n");
879        p(ips_badaddr, "\t%lu datagram%s with bad address in header\n");
880#undef p
881#undef p1a
882}
883
884/*
885 * Dump ARP statistics structure.
886 */
887void
888arp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
889{
890        struct arpstat arpstat, zerostat;
891        size_t len = sizeof(arpstat);
892
893        if (live) {
894                if (zflag)
895                        memset(&zerostat, 0, len);
896                if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,
897                    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
898                        warn("sysctl: net.link.ether.arp.stats");
899                        return;
900                }
901        } else
902                kread(off, &arpstat, len);
903
904        printf("%s:\n", name);
905
906#define p(f, m) if (arpstat.f || sflag <= 1) \
907    printf(m, arpstat.f, plural(arpstat.f))
908#define p2(f, m) if (arpstat.f || sflag <= 1) \
909    printf(m, arpstat.f, pluralies(arpstat.f))
910
911        p(txrequests, "\t%lu ARP request%s sent\n");
912        p2(txreplies, "\t%lu ARP repl%s sent\n");
913        p(rxrequests, "\t%lu ARP request%s received\n");
914        p2(rxreplies, "\t%lu ARP repl%s received\n");
915        p(received, "\t%lu ARP packet%s received\n");
916        p(dropped, "\t%lu total packet%s dropped due to no ARP entry\n");
917        p(timeouts, "\t%lu ARP entry%s timed out\n");
918        p(dupips, "\t%lu Duplicate IP%s seen\n");
919#undef p
920#undef p2
921}
922
923
924
925static  const char *icmpnames[ICMP_MAXTYPE + 1] = {
926        "echo reply",                   /* RFC 792 */
927        "#1",
928        "#2",
929        "destination unreachable",      /* RFC 792 */
930        "source quench",                /* RFC 792 */
931        "routing redirect",             /* RFC 792 */
932        "#6",
933        "#7",
934        "echo",                         /* RFC 792 */
935        "router advertisement",         /* RFC 1256 */
936        "router solicitation",          /* RFC 1256 */
937        "time exceeded",                /* RFC 792 */
938        "parameter problem",            /* RFC 792 */
939        "time stamp",                   /* RFC 792 */
940        "time stamp reply",             /* RFC 792 */
941        "information request",          /* RFC 792 */
942        "information request reply",    /* RFC 792 */
943        "address mask request",         /* RFC 950 */
944        "address mask reply",           /* RFC 950 */
945        "#19",
946        "#20",
947        "#21",
948        "#22",
949        "#23",
950        "#24",
951        "#25",
952        "#26",
953        "#27",
954        "#28",
955        "#29",
956        "icmp traceroute",              /* RFC 1393 */
957        "datagram conversion error",    /* RFC 1475 */
958        "mobile host redirect",
959        "IPv6 where-are-you",
960        "IPv6 i-am-here",
961        "mobile registration req",
962        "mobile registration reply",
963        "domain name request",          /* RFC 1788 */
964        "domain name reply",            /* RFC 1788 */
965        "icmp SKIP",
966        "icmp photuris",                /* RFC 2521 */
967};
968
969/*
970 * Dump ICMP statistics.
971 */
972void
973icmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
974{
975        struct icmpstat icmpstat, zerostat;
976        int i, first;
977        size_t len;
978
979        len = sizeof icmpstat;
980        if (live) {
981                if (zflag)
982                        memset(&zerostat, 0, len);
983                if (sysctlbyname("net.inet.icmp.stats", &icmpstat, &len,
984                    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
985                        warn("sysctl: net.inet.icmp.stats");
986                        return;
987                }
988        } else
989                kread(off, &icmpstat, len);
990
991        printf("%s:\n", name);
992
993#define p(f, m) if (icmpstat.f || sflag <= 1) \
994    printf(m, icmpstat.f, plural(icmpstat.f))
995#define p1a(f, m) if (icmpstat.f || sflag <= 1) \
996    printf(m, icmpstat.f)
997#define p2(f, m) if (icmpstat.f || sflag <= 1) \
998    printf(m, icmpstat.f, plurales(icmpstat.f))
999
1000        p(icps_error, "\t%lu call%s to icmp_error\n");
1001        p(icps_oldicmp,
1002            "\t%lu error%s not generated in response to an icmp message\n");
1003        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
1004                if (icmpstat.icps_outhist[i] != 0) {
1005                        if (first) {
1006                                printf("\tOutput histogram:\n");
1007                                first = 0;
1008                        }
1009                        if (icmpnames[i] != NULL)
1010                                printf("\t\t%s: %lu\n", icmpnames[i],
1011                                        icmpstat.icps_outhist[i]);
1012                        else
1013                                printf("\t\tunknown ICMP #%d: %lu\n", i,
1014                                        icmpstat.icps_outhist[i]);
1015                }
1016        p(icps_badcode, "\t%lu message%s with bad code fields\n");
1017        p(icps_tooshort, "\t%lu message%s less than the minimum length\n");
1018        p(icps_checksum, "\t%lu message%s with bad checksum\n");
1019        p(icps_badlen, "\t%lu message%s with bad length\n");
1020        p1a(icps_bmcastecho, "\t%lu multicast echo requests ignored\n");
1021        p1a(icps_bmcasttstamp, "\t%lu multicast timestamp requests ignored\n");
1022        for (first = 1, i = 0; i < ICMP_MAXTYPE + 1; i++)
1023                if (icmpstat.icps_inhist[i] != 0) {
1024                        if (first) {
1025                                printf("\tInput histogram:\n");
1026                                first = 0;
1027                        }
1028                        if (icmpnames[i] != NULL)
1029                                printf("\t\t%s: %lu\n", icmpnames[i],
1030                                    icmpstat.icps_inhist[i]);
1031                        else
1032                                printf("\t\tunknown ICMP #%d: %lu\n", i,
1033                                    icmpstat.icps_inhist[i]);
1034                }
1035        p(icps_reflect, "\t%lu message response%s generated\n");
1036        p2(icps_badaddr, "\t%lu invalid return address%s\n");
1037        p(icps_noroute, "\t%lu no return route%s\n");
1038#undef p
1039#undef p1a
1040#undef p2
1041        if (live) {
1042                len = sizeof i;
1043                if (sysctlbyname("net.inet.icmp.maskrepl", &i, &len, NULL, 0) <
1044                    0)
1045                        return;
1046                printf("\tICMP address mask responses are %sabled\n",
1047                    i ? "en" : "dis");
1048        }
1049}
1050
1051#ifndef BURN_BRIDGES
1052/*
1053 * Dump IGMP statistics structure (pre 8.x kernel).
1054 */
1055static void
1056igmp_stats_live_old(u_long off, const char *name)
1057{
1058        struct oigmpstat oigmpstat, zerostat;
1059        size_t len = sizeof(oigmpstat);
1060
1061        if (zflag)
1062                memset(&zerostat, 0, len);
1063        if (sysctlbyname("net.inet.igmp.stats", &oigmpstat, &len,
1064            zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1065                warn("sysctl: net.inet.igmp.stats");
1066                return;
1067        }
1068
1069        printf("%s:\n", name);
1070
1071#define p(f, m) if (oigmpstat.f || sflag <= 1) \
1072    printf(m, oigmpstat.f, plural(oigmpstat.f))
1073#define py(f, m) if (oigmpstat.f || sflag <= 1) \
1074    printf(m, oigmpstat.f, oigmpstat.f != 1 ? "ies" : "y")
1075        p(igps_rcv_total, "\t%u message%s received\n");
1076        p(igps_rcv_tooshort, "\t%u message%s received with too few bytes\n");
1077        p(igps_rcv_badsum, "\t%u message%s received with bad checksum\n");
1078        py(igps_rcv_queries, "\t%u membership quer%s received\n");
1079        py(igps_rcv_badqueries,
1080            "\t%u membership quer%s received with invalid field(s)\n");
1081        p(igps_rcv_reports, "\t%u membership report%s received\n");
1082        p(igps_rcv_badreports,
1083            "\t%u membership report%s received with invalid field(s)\n");
1084        p(igps_rcv_ourreports,
1085"\t%u membership report%s received for groups to which we belong\n");
1086        p(igps_snd_reports, "\t%u membership report%s sent\n");
1087#undef p
1088#undef py
1089}
1090#endif /* !BURN_BRIDGES */
1091
1092/*
1093 * Dump IGMP statistics structure.
1094 */
1095void
1096igmp_stats(u_long off, const char *name, int af1 __unused, int proto __unused)
1097{
1098        struct igmpstat igmpstat, zerostat;
1099        size_t len;
1100
1101#ifndef BURN_BRIDGES
1102        if (live) {
1103                /*
1104                 * Detect if we are being run against a pre-IGMPv3 kernel.
1105                 * We cannot do this for a core file as the legacy
1106                 * struct igmpstat has no size field, nor does it
1107                 * export it in any readily-available symbols.
1108                 */
1109                len = 0;
1110                if (sysctlbyname("net.inet.igmp.stats", NULL, &len, NULL,
1111                    0) < 0) {
1112                        warn("sysctl: net.inet.igmp.stats");
1113                        return;
1114                }
1115                if (len < sizeof(igmpstat)) {
1116                        igmp_stats_live_old(off, name);
1117                        return;
1118                }
1119        }
1120#endif /* !BURN_BRIDGES */
1121
1122        len = sizeof(igmpstat);
1123        if (live) {
1124                if (zflag)
1125                        memset(&zerostat, 0, len);
1126                if (sysctlbyname("net.inet.igmp.stats", &igmpstat, &len,
1127                    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1128                        warn("sysctl: net.inet.igmp.stats");
1129                        return;
1130                }
1131        } else {
1132                len = sizeof(igmpstat);
1133                kread(off, &igmpstat, len);
1134        }
1135
1136        if (igmpstat.igps_version != IGPS_VERSION_3) {
1137                warnx("%s: version mismatch (%d != %d)", __func__,
1138                    igmpstat.igps_version, IGPS_VERSION_3);
1139        }
1140        if (igmpstat.igps_len != IGPS_VERSION3_LEN) {
1141                warnx("%s: size mismatch (%d != %d)", __func__,
1142                    igmpstat.igps_len, IGPS_VERSION3_LEN);
1143        }
1144
1145        printf("%s:\n", name);
1146
1147#define p64(f, m) if (igmpstat.f || sflag <= 1) \
1148    printf(m, (uintmax_t) igmpstat.f, plural(igmpstat.f))
1149#define py64(f, m) if (igmpstat.f || sflag <= 1) \
1150    printf(m, (uintmax_t) igmpstat.f, pluralies(igmpstat.f))
1151        p64(igps_rcv_total, "\t%ju message%s received\n");
1152        p64(igps_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1153        p64(igps_rcv_badttl, "\t%ju message%s received with wrong TTL\n");
1154        p64(igps_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1155        py64(igps_rcv_v1v2_queries, "\t%ju V1/V2 membership quer%s received\n");
1156        py64(igps_rcv_v3_queries, "\t%ju V3 membership quer%s received\n");
1157        py64(igps_rcv_badqueries,
1158            "\t%ju membership quer%s received with invalid field(s)\n");
1159        py64(igps_rcv_gen_queries, "\t%ju general quer%s received\n");
1160        py64(igps_rcv_group_queries, "\t%ju group quer%s received\n");
1161        py64(igps_rcv_gsr_queries, "\t%ju group-source quer%s received\n");
1162        py64(igps_drop_gsr_queries, "\t%ju group-source quer%s dropped\n");
1163        p64(igps_rcv_reports, "\t%ju membership report%s received\n");
1164        p64(igps_rcv_badreports,
1165            "\t%ju membership report%s received with invalid field(s)\n");
1166        p64(igps_rcv_ourreports,
1167"\t%ju membership report%s received for groups to which we belong\n");
1168        p64(igps_rcv_nora, "\t%ju V3 report%s received without Router Alert\n");
1169        p64(igps_snd_reports, "\t%ju membership report%s sent\n");
1170#undef p64
1171#undef py64
1172}
1173
1174/*
1175 * Dump PIM statistics structure.
1176 */
1177void
1178pim_stats(u_long off __unused, const char *name, int af1 __unused,
1179    int proto __unused)
1180{
1181        struct pimstat pimstat, zerostat;
1182        size_t len = sizeof pimstat;
1183
1184        if (live) {
1185                if (zflag)
1186                        memset(&zerostat, 0, len);
1187                if (sysctlbyname("net.inet.pim.stats", &pimstat, &len,
1188                    zflag ? &zerostat : NULL, zflag ? len : 0) < 0) {
1189                        if (errno != ENOENT)
1190                                warn("sysctl: net.inet.pim.stats");
1191                        return;
1192                }
1193        } else {
1194                if (off == 0)
1195                        return;
1196                kread(off, &pimstat, len);
1197        }
1198
1199        printf("%s:\n", name);
1200
1201#define p(f, m) if (pimstat.f || sflag <= 1) \
1202    printf(m, (uintmax_t)pimstat.f, plural(pimstat.f))
1203#define py(f, m) if (pimstat.f || sflag <= 1) \
1204    printf(m, (uintmax_t)pimstat.f, pimstat.f != 1 ? "ies" : "y")
1205        p(pims_rcv_total_msgs, "\t%ju message%s received\n");
1206        p(pims_rcv_total_bytes, "\t%ju byte%s received\n");
1207        p(pims_rcv_tooshort, "\t%ju message%s received with too few bytes\n");
1208        p(pims_rcv_badsum, "\t%ju message%s received with bad checksum\n");
1209        p(pims_rcv_badversion, "\t%ju message%s received with bad version\n");
1210        p(pims_rcv_registers_msgs, "\t%ju data register message%s received\n");
1211        p(pims_rcv_registers_bytes, "\t%ju data register byte%s received\n");
1212        p(pims_rcv_registers_wrongiif,
1213            "\t%ju data register message%s received on wrong iif\n");
1214        p(pims_rcv_badregisters, "\t%ju bad register%s received\n");
1215        p(pims_snd_registers_msgs, "\t%ju data register message%s sent\n");
1216        p(pims_snd_registers_bytes, "\t%ju data register byte%s sent\n");
1217#undef p
1218#undef py
1219}
1220
1221/*
1222 * Pretty print an Internet address (net address + port).
1223 */
1224void
1225inetprint(struct in_addr *in, int port, const char *proto, int num_port)
1226{
1227        struct servent *sp = 0;
1228        char line[80], *cp;
1229        int width;
1230
1231        if (Wflag)
1232            sprintf(line, "%s.", inetname(in));
1233        else
1234            sprintf(line, "%.*s.", (Aflag && !num_port) ? 12 : 16, inetname(in));
1235        cp = index(line, '\0');
1236        if (!num_port && port)
1237                sp = getservbyport((int)port, proto);
1238        if (sp || port == 0)
1239                sprintf(cp, "%.15s ", sp ? sp->s_name : "*");
1240        else
1241                sprintf(cp, "%d ", ntohs((u_short)port));
1242        width = (Aflag && !Wflag) ? 18 : 22;
1243        if (Wflag)
1244            printf("%-*s ", width, line);
1245        else
1246            printf("%-*.*s ", width, width, line);
1247}
1248
1249/*
1250 * Construct an Internet address representation.
1251 * If numeric_addr has been supplied, give
1252 * numeric value, otherwise try for symbolic name.
1253 */
1254char *
1255inetname(struct in_addr *inp)
1256{
1257        char *cp;
1258        static char line[MAXHOSTNAMELEN];
1259        struct hostent *hp;
1260        struct netent *np;
1261
1262        cp = 0;
1263        if (!numeric_addr && inp->s_addr != INADDR_ANY) {
1264                int net = inet_netof(*inp);
1265                int lna = inet_lnaof(*inp);
1266
1267                if (lna == INADDR_ANY) {
1268                        np = getnetbyaddr(net, AF_INET);
1269                        if (np)
1270                                cp = np->n_name;
1271                }
1272                if (cp == 0) {
1273                        hp = gethostbyaddr((char *)inp, sizeof (*inp), AF_INET);
1274                        if (hp) {
1275                                cp = hp->h_name;
1276                                trimdomain(cp, strlen(cp));
1277                        }
1278                }
1279        }
1280        if (inp->s_addr == INADDR_ANY)
1281                strcpy(line, "*");
1282        else if (cp) {
1283                strlcpy(line, cp, sizeof(line));
1284        } else {
1285                inp->s_addr = ntohl(inp->s_addr);
1286#define C(x)    ((u_int)((x) & 0xff))
1287                sprintf(line, "%u.%u.%u.%u", C(inp->s_addr >> 24),
1288                    C(inp->s_addr >> 16), C(inp->s_addr >> 8), C(inp->s_addr));
1289        }
1290        return (line);
1291}
Note: See TracBrowser for help on using the repository browser.