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