source: rtems-libbsd/freebsd/usr.bin/netstat/inet.c @ 6da9b23

4.1155-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 6da9b23 was 6da9b23, checked in by Sebastian Huber <sebastian.huber@…>, on 10/30/13 at 14:42:53

NETSTAT(1): Initialize global variables

Use BSD program lock.

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