]> CyberLeo.Net >> Repos - FreeBSD/stable/8.git/blob - usr.bin/netstat/main.c
MFC: 215434, 215724
[FreeBSD/stable/8.git] / usr.bin / netstat / main.c
1 /*-
2  * Copyright (c) 1983, 1988, 1993
3  *      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 #ifndef lint
35 char const copyright[] =
36 "@(#) Copyright (c) 1983, 1988, 1993\n\
37         Regents of the University of California.  All rights reserved.\n";
38 #endif /* not lint */
39
40 #if 0
41 #ifndef lint
42 static char sccsid[] = "@(#)main.c      8.4 (Berkeley) 3/1/94";
43 #endif /* not lint */
44 #endif
45
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
48
49 #include <sys/param.h>
50 #include <sys/file.h>
51 #include <sys/protosw.h>
52 #include <sys/socket.h>
53 #include <sys/socketvar.h>
54
55 #include <netinet/in.h>
56
57 #ifdef NETGRAPH
58 #include <netgraph/ng_socket.h>
59 #endif
60
61 #include <ctype.h>
62 #include <err.h>
63 #include <errno.h>
64 #include <kvm.h>
65 #include <limits.h>
66 #include <netdb.h>
67 #include <nlist.h>
68 #include <paths.h>
69 #include <stdint.h>
70 #include <stdio.h>
71 #include <stdlib.h>
72 #include <string.h>
73 #include <unistd.h>
74 #include "netstat.h"
75
76 static struct nlist nl[] = {
77 #define N_IFNET         0
78         { .n_name = "_ifnet" },
79 #define N_RTSTAT        1
80         { .n_name = "_rtstat" },
81 #define N_RTREE         2
82         { .n_name = "_rt_tables"},
83 #define N_MRTSTAT       3
84         { .n_name = "_mrtstat" },
85 #define N_MFCHASHTBL    4
86         { .n_name = "_mfchashtbl" },
87 #define N_VIFTABLE      5
88         { .n_name = "_viftable" },
89 #define N_IPX           6
90         { .n_name = "_ipxpcb_list"},
91 #define N_IPXSTAT       7
92         { .n_name = "_ipxstat"},
93 #define N_SPXSTAT       8
94         { .n_name = "_spx_istat"},
95 #define N_DDPSTAT       9
96         { .n_name = "_ddpstat"},
97 #define N_DDPCB         10
98         { .n_name = "_ddpcb"},
99 #define N_NGSOCKS       11
100         { .n_name = "_ngsocklist"},
101 #define N_IP6STAT       12
102         { .n_name = "_ip6stat" },
103 #define N_ICMP6STAT     13
104         { .n_name = "_icmp6stat" },
105 #define N_IPSECSTAT     14
106         { .n_name = "_ipsec4stat" },
107 #define N_IPSEC6STAT    15
108         { .n_name = "_ipsec6stat" },
109 #define N_PIM6STAT      16
110         { .n_name = "_pim6stat" },
111 #define N_MRT6STAT      17
112         { .n_name = "_mrt6stat" },
113 #define N_MF6CTABLE     18
114         { .n_name = "_mf6ctable" },
115 #define N_MIF6TABLE     19
116         { .n_name = "_mif6table" },
117 #define N_PFKEYSTAT     20
118         { .n_name = "_pfkeystat" },
119 #define N_MBSTAT        21
120         { .n_name = "_mbstat" },
121 #define N_MBTYPES       22
122         { .n_name = "_mbtypes" },
123 #define N_NMBCLUSTERS   23
124         { .n_name = "_nmbclusters" },
125 #define N_NMBUFS        24
126         { .n_name = "_nmbufs" },
127 #define N_MBHI          25
128         { .n_name = "_mbuf_hiwm" },
129 #define N_CLHI          26
130         { .n_name = "_clust_hiwm" },
131 #define N_NCPUS         27
132         { .n_name = "_smp_cpus" },
133 #define N_PAGESZ        28
134         { .n_name = "_pagesize" },
135 #define N_MBPSTAT       29
136         { .n_name = "_mb_statpcpu" },
137 #define N_RTTRASH       30
138         { .n_name = "_rttrash" },
139 #define N_MBLO          31
140         { .n_name = "_mbuf_lowm" },
141 #define N_CLLO          32
142         { .n_name = "_clust_lowm" },
143 #define N_CARPSTAT      33
144         { .n_name = "_carpstats" },
145 #define N_PFSYNCSTAT    34
146         { .n_name = "_pfsyncstats" },
147 #define N_AHSTAT        35
148         { .n_name = "_ahstat" },
149 #define N_ESPSTAT       36
150         { .n_name = "_espstat" },
151 #define N_IPCOMPSTAT    37
152         { .n_name = "_ipcompstat" },
153 #define N_TCPSTAT       38
154         { .n_name = "_tcpstat" },
155 #define N_UDPSTAT       39
156         { .n_name = "_udpstat" },
157 #define N_IPSTAT        40
158         { .n_name = "_ipstat" },
159 #define N_ICMPSTAT      41
160         { .n_name = "_icmpstat" },
161 #define N_IGMPSTAT      42
162         { .n_name = "_igmpstat" },
163 #define N_PIMSTAT       43
164         { .n_name = "_pimstat" },
165 #define N_TCBINFO       44
166         { .n_name = "_tcbinfo" },
167 #define N_UDBINFO       45
168         { .n_name = "_udbinfo" },
169 #define N_DIVCBINFO     46
170         { .n_name = "_divcbinfo" },
171 #define N_RIPCBINFO     47
172         { .n_name = "_ripcbinfo" },
173 #define N_UNP_COUNT     48
174         { .n_name = "_unp_count" },
175 #define N_UNP_GENCNT    49
176         { .n_name = "_unp_gencnt" },
177 #define N_UNP_DHEAD     50
178         { .n_name = "_unp_dhead" },
179 #define N_UNP_SHEAD     51
180         { .n_name = "_unp_shead" },
181 #define N_RIP6STAT      52
182         { .n_name = "_rip6stat" },
183 #define N_SCTPSTAT      53
184         { .n_name = "_sctpstat" },
185 #define N_MFCTABLESIZE  54
186         { .n_name = "_mfctablesize" },
187 #define N_ARPSTAT       55
188         { .n_name = "_arpstat" },
189         { .n_name = NULL },
190 };
191
192 struct protox {
193         int     pr_index;               /* index into nlist of cb head */
194         int     pr_sindex;              /* index into nlist of stat block */
195         u_char  pr_wanted;              /* 1 if wanted, 0 otherwise */
196         void    (*pr_cblocks)(u_long, const char *, int, int);
197                                         /* control blocks printing routine */
198         void    (*pr_stats)(u_long, const char *, int, int);
199                                         /* statistics printing routine */
200         void    (*pr_istats)(char *);   /* per/if statistics printing routine */
201         const char      *pr_name;               /* well-known name */
202         int     pr_usesysctl;           /* non-zero if we use sysctl, not kvm */
203         int     pr_protocol;
204 } protox[] = {
205         { N_TCBINFO,    N_TCPSTAT,      1,      protopr,
206           tcp_stats,    NULL,           "tcp",  1,      IPPROTO_TCP },
207         { N_UDBINFO,    N_UDPSTAT,      1,      protopr,
208           udp_stats,    NULL,           "udp",  1,      IPPROTO_UDP },
209 #ifdef SCTP
210         { -1,           N_SCTPSTAT,     1,      sctp_protopr,
211           sctp_stats,   NULL,           "sctp", 1,      IPPROTO_SCTP },
212 #endif
213         { N_DIVCBINFO,  -1,             1,      protopr,
214           NULL,         NULL,           "divert", 1,    IPPROTO_DIVERT },
215         { N_RIPCBINFO,  N_IPSTAT,       1,      protopr,
216           ip_stats,     NULL,           "ip",   1,      IPPROTO_RAW },
217         { N_RIPCBINFO,  N_ICMPSTAT,     1,      protopr,
218           icmp_stats,   NULL,           "icmp", 1,      IPPROTO_ICMP },
219         { N_RIPCBINFO,  N_IGMPSTAT,     1,      protopr,
220           igmp_stats,   NULL,           "igmp", 1,      IPPROTO_IGMP },
221 #ifdef IPSEC
222         { -1,           N_IPSECSTAT,    1,      NULL,   /* keep as compat */
223           ipsec_stats,  NULL,           "ipsec", 0,     0},
224         { -1,           N_AHSTAT,       1,      NULL,
225           ah_stats,     NULL,           "ah",   0,      0},
226         { -1,           N_ESPSTAT,      1,      NULL,
227           esp_stats,    NULL,           "esp",  0,      0},
228         { -1,           N_IPCOMPSTAT,   1,      NULL,
229           ipcomp_stats, NULL,           "ipcomp", 0,    0},
230 #endif
231         { N_RIPCBINFO,  N_PIMSTAT,      1,      protopr,
232           pim_stats,    NULL,           "pim",  1,      IPPROTO_PIM },
233         { -1,           N_CARPSTAT,     1,      NULL,
234           carp_stats,   NULL,           "carp", 1,      0 },
235         { -1,           N_PFSYNCSTAT,   1,      NULL,
236           pfsync_stats, NULL,           "pfsync", 1,    0 },
237         { -1,           N_ARPSTAT,      1,      NULL,
238           arp_stats,    NULL,           "arp", 1,       0 },
239         { -1,           -1,             0,      NULL,
240           NULL,         NULL,           NULL,   0,      0 }
241 };
242
243 #ifdef INET6
244 struct protox ip6protox[] = {
245         { N_TCBINFO,    N_TCPSTAT,      1,      protopr,
246           tcp_stats,    NULL,           "tcp",  1,      IPPROTO_TCP },
247         { N_UDBINFO,    N_UDPSTAT,      1,      protopr,
248           udp_stats,    NULL,           "udp",  1,      IPPROTO_UDP },
249         { N_RIPCBINFO,  N_IP6STAT,      1,      protopr,
250           ip6_stats,    ip6_ifstats,    "ip6",  1,      IPPROTO_RAW },
251         { N_RIPCBINFO,  N_ICMP6STAT,    1,      protopr,
252           icmp6_stats,  icmp6_ifstats,  "icmp6", 1,     IPPROTO_ICMPV6 },
253 #ifdef IPSEC
254         { -1,           N_IPSEC6STAT,   1,      NULL,
255           ipsec_stats,  NULL,           "ipsec6", 0,    0 },
256 #endif
257 #ifdef notyet
258         { -1,           N_PIM6STAT,     1,      NULL,
259           pim6_stats,   NULL,           "pim6", 1,      0 },
260 #endif
261         { -1,           N_RIP6STAT,     1,      NULL,
262           rip6_stats,   NULL,           "rip6", 1,      0 },
263         { -1,           -1,             0,      NULL,
264           NULL,         NULL,           NULL,   0,      0 }
265 };
266 #endif /*INET6*/
267
268 #ifdef IPSEC
269 struct protox pfkeyprotox[] = {
270         { -1,           N_PFKEYSTAT,    1,      NULL,
271           pfkey_stats,  NULL,           "pfkey", 0,     0 },
272         { -1,           -1,             0,      NULL,
273           NULL,         NULL,           NULL,   0,      0 }
274 };
275 #endif
276
277 struct protox atalkprotox[] = {
278         { N_DDPCB,      N_DDPSTAT,      1,      atalkprotopr,
279           ddp_stats,    NULL,           "ddp",  0,      0 },
280         { -1,           -1,             0,      NULL,
281           NULL,         NULL,           NULL,   0,      0 }
282 };
283 #ifdef NETGRAPH
284 struct protox netgraphprotox[] = {
285         { N_NGSOCKS,    -1,             1,      netgraphprotopr,
286           NULL,         NULL,           "ctrl", 0,      0 },
287         { N_NGSOCKS,    -1,             1,      netgraphprotopr,
288           NULL,         NULL,           "data", 0,      0 },
289         { -1,           -1,             0,      NULL,
290           NULL,         NULL,           NULL,   0,      0 }
291 };
292 #endif
293 #ifdef IPX
294 struct protox ipxprotox[] = {
295         { N_IPX,        N_IPXSTAT,      1,      ipxprotopr,
296           ipx_stats,    NULL,           "ipx",  0,      0 },
297         { N_IPX,        N_SPXSTAT,      1,      ipxprotopr,
298           spx_stats,    NULL,           "spx",  0,      0 },
299         { -1,           -1,             0,      NULL,
300           NULL,         NULL,           0,      0,      0 }
301 };
302 #endif
303
304 struct protox *protoprotox[] = {
305                                          protox,
306 #ifdef INET6
307                                          ip6protox,
308 #endif
309 #ifdef IPSEC
310                                          pfkeyprotox,
311 #endif
312 #ifdef IPX
313                                          ipxprotox,
314 #endif
315                                          atalkprotox, NULL };
316
317 static void printproto(struct protox *, const char *);
318 static void usage(void);
319 static struct protox *name2protox(const char *);
320 static struct protox *knownname(const char *);
321
322 static kvm_t *kvmd;
323 static char *nlistf = NULL, *memf = NULL;
324
325 int     Aflag;          /* show addresses of protocol control block */
326 int     aflag;          /* show all sockets (including servers) */
327 int     Bflag;          /* show information about bpf consumers */
328 int     bflag;          /* show i/f total bytes in/out */
329 int     dflag;          /* show i/f dropped packets */
330 int     gflag;          /* show group (multicast) routing or stats */
331 int     hflag;          /* show counters in human readable format */
332 int     iflag;          /* show interfaces */
333 int     Lflag;          /* show size of listen queues */
334 int     mflag;          /* show memory stats */
335 int     noutputs = 0;   /* how much outputs before we exit */
336 int     numeric_addr;   /* show addresses numerically */
337 int     numeric_port;   /* show ports numerically */
338 static int pflag;       /* show given protocol */
339 int     rflag;          /* show routing tables (or routing stats) */
340 int     sflag;          /* show protocol statistics */
341 int     tflag;          /* show i/f watchdog timers */
342 int     Wflag;          /* wide display */
343 int     Tflag;          /* TCP Information */
344 int     xflag;          /* extra information, includes all socket buffer info */
345 int     zflag;          /* zero stats */
346
347 int     interval;       /* repeat interval for i/f stats */
348
349 char    *interface;     /* desired i/f for stats, or NULL for all i/fs */
350 int     unit;           /* unit number for above */
351
352 int     af;             /* address family */
353 int     live;           /* true if we are examining a live system */
354
355 int
356 main(int argc, char *argv[])
357 {
358         struct protox *tp = NULL;  /* for printing cblocks & stats */
359         int ch;
360
361         af = AF_UNSPEC;
362
363         while ((ch = getopt(argc, argv, "AaBbdf:ghI:iLlM:mN:np:Qq:rSTstuWw:xz"))
364             != -1)
365                 switch(ch) {
366                 case 'A':
367                         Aflag = 1;
368                         break;
369                 case 'a':
370                         aflag = 1;
371                         break;
372                 case 'B':
373                         Bflag = 1;
374                         break;
375                 case 'b':
376                         bflag = 1;
377                         break;
378                 case 'd':
379                         dflag = 1;
380                         break;
381                 case 'f':
382                         if (strcmp(optarg, "ipx") == 0)
383                                 af = AF_IPX;
384                         else if (strcmp(optarg, "inet") == 0)
385                                 af = AF_INET;
386 #ifdef INET6
387                         else if (strcmp(optarg, "inet6") == 0)
388                                 af = AF_INET6;
389 #endif
390 #ifdef IPSEC
391                         else if (strcmp(optarg, "pfkey") == 0)
392                                 af = PF_KEY;
393 #endif
394                         else if (strcmp(optarg, "unix") == 0)
395                                 af = AF_UNIX;
396                         else if (strcmp(optarg, "atalk") == 0)
397                                 af = AF_APPLETALK;
398 #ifdef NETGRAPH
399                         else if (strcmp(optarg, "ng") == 0
400                             || strcmp(optarg, "netgraph") == 0)
401                                 af = AF_NETGRAPH;
402 #endif
403                         else if (strcmp(optarg, "link") == 0)
404                                 af = AF_LINK;
405                         else {
406                                 errx(1, "%s: unknown address family", optarg);
407                         }
408                         break;
409                 case 'g':
410                         gflag = 1;
411                         break;
412                 case 'h':
413                         hflag = 1;
414                         break;
415                 case 'I': {
416                         char *cp;
417
418                         iflag = 1;
419                         for (cp = interface = optarg; isalpha(*cp); cp++)
420                                 continue;
421                         unit = atoi(cp);
422                         break;
423                 }
424                 case 'i':
425                         iflag = 1;
426                         break;
427                 case 'L':
428                         Lflag = 1;
429                         break;
430                 case 'M':
431                         memf = optarg;
432                         break;
433                 case 'm':
434                         mflag = 1;
435                         break;
436                 case 'N':
437                         nlistf = optarg;
438                         break;
439                 case 'n':
440                         numeric_addr = numeric_port = 1;
441                         break;
442                 case 'p':
443                         if ((tp = name2protox(optarg)) == NULL) {
444                                 errx(1,
445                                      "%s: unknown or uninstrumented protocol",
446                                      optarg);
447                         }
448                         pflag = 1;
449                         break;
450                 case 'q':
451                         noutputs = atoi(optarg);
452                         if (noutputs != 0)
453                                 noutputs++;
454                         break;
455                 case 'r':
456                         rflag = 1;
457                         break;
458                 case 's':
459                         ++sflag;
460                         break;
461                 case 'S':
462                         numeric_addr = 1;
463                         break;
464                 case 't':
465                         tflag = 1;
466                         break;
467                 case 'u':
468                         af = AF_UNIX;
469                         break;
470                 case 'W':
471                 case 'l':
472                         Wflag = 1;
473                         break;
474                 case 'w':
475                         interval = atoi(optarg);
476                         iflag = 1;
477                         break;
478                 case 'T':
479                         Tflag = 1;
480                         break;
481                 case 'x':
482                         xflag = 1;
483                         break;
484                 case 'z':
485                         zflag = 1;
486                         break;
487                 case '?':
488                 default:
489                         usage();
490                 }
491         argv += optind;
492         argc -= optind;
493
494 #define BACKWARD_COMPATIBILITY
495 #ifdef  BACKWARD_COMPATIBILITY
496         if (*argv) {
497                 if (isdigit(**argv)) {
498                         interval = atoi(*argv);
499                         if (interval <= 0)
500                                 usage();
501                         ++argv;
502                         iflag = 1;
503                 }
504                 if (*argv) {
505                         nlistf = *argv;
506                         if (*++argv)
507                                 memf = *argv;
508                 }
509         }
510 #endif
511
512         /*
513          * Discard setgid privileges if not the running kernel so that bad
514          * guys can't print interesting stuff from kernel memory.
515          */
516         live = (nlistf == NULL && memf == NULL);
517         if (!live)
518                 setgid(getgid());
519
520         if (xflag && Tflag) 
521                 errx(1, "-x and -T are incompatible, pick one.");
522
523         if (Bflag) {
524                 if (!live)
525                         usage();
526                 bpf_stats(interface);
527                 exit(0);
528         }
529         if (mflag) {
530                 if (!live) {
531                         if (kread(0, NULL, 0) == 0)
532                                 mbpr(kvmd, nl[N_MBSTAT].n_value);
533                 } else
534                         mbpr(NULL, 0);
535                 exit(0);
536         }
537 #if 0
538         /*
539          * Keep file descriptors open to avoid overhead
540          * of open/close on each call to get* routines.
541          */
542         sethostent(1);
543         setnetent(1);
544 #else
545         /*
546          * This does not make sense any more with DNS being default over
547          * the files.  Doing a setXXXXent(1) causes a tcp connection to be
548          * used for the queries, which is slower.
549          */
550 #endif
551         kread(0, NULL, 0);
552         if (iflag && !sflag) {
553                 intpr(interval, nl[N_IFNET].n_value, NULL);
554                 exit(0);
555         }
556         if (rflag) {
557                 if (sflag)
558                         rt_stats(nl[N_RTSTAT].n_value, nl[N_RTTRASH].n_value);
559                 else
560                         routepr(nl[N_RTREE].n_value);
561                 exit(0);
562         }
563         if (gflag) {
564                 if (sflag) {
565                         if (af == AF_INET || af == AF_UNSPEC)
566                                 mrt_stats(nl[N_MRTSTAT].n_value);
567 #ifdef INET6
568                         if (af == AF_INET6 || af == AF_UNSPEC)
569                                 mrt6_stats(nl[N_MRT6STAT].n_value);
570 #endif
571                 } else {
572                         if (af == AF_INET || af == AF_UNSPEC)
573                                 mroutepr(nl[N_MFCHASHTBL].n_value,
574                                          nl[N_MFCTABLESIZE].n_value,
575                                          nl[N_VIFTABLE].n_value);
576 #ifdef INET6
577                         if (af == AF_INET6 || af == AF_UNSPEC)
578                                 mroute6pr(nl[N_MF6CTABLE].n_value,
579                                           nl[N_MIF6TABLE].n_value);
580 #endif
581                 }
582                 exit(0);
583         }
584
585         if (tp) {
586                 printproto(tp, tp->pr_name);
587                 exit(0);
588         }
589         if (af == AF_INET || af == AF_UNSPEC)
590                 for (tp = protox; tp->pr_name; tp++)
591                         printproto(tp, tp->pr_name);
592 #ifdef INET6
593         if (af == AF_INET6 || af == AF_UNSPEC)
594                 for (tp = ip6protox; tp->pr_name; tp++)
595                         printproto(tp, tp->pr_name);
596 #endif /*INET6*/
597 #ifdef IPSEC
598         if (af == PF_KEY || af == AF_UNSPEC)
599                 for (tp = pfkeyprotox; tp->pr_name; tp++)
600                         printproto(tp, tp->pr_name);
601 #endif /*IPSEC*/
602 #ifdef IPX
603         if (af == AF_IPX || af == AF_UNSPEC) {
604                 for (tp = ipxprotox; tp->pr_name; tp++)
605                         printproto(tp, tp->pr_name);
606         }
607 #endif /* IPX */
608         if (af == AF_APPLETALK || af == AF_UNSPEC)
609                 for (tp = atalkprotox; tp->pr_name; tp++)
610                         printproto(tp, tp->pr_name);
611 #ifdef NETGRAPH
612         if (af == AF_NETGRAPH || af == AF_UNSPEC)
613                 for (tp = netgraphprotox; tp->pr_name; tp++)
614                         printproto(tp, tp->pr_name);
615 #endif /* NETGRAPH */
616         if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
617                 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
618                     nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value);
619         exit(0);
620 }
621
622 /*
623  * Print out protocol statistics or control blocks (per sflag).
624  * If the interface was not specifically requested, and the symbol
625  * is not in the namelist, ignore this one.
626  */
627 static void
628 printproto(tp, name)
629         struct protox *tp;
630         const char *name;
631 {
632         void (*pr)(u_long, const char *, int, int);
633         u_long off;
634
635         if (sflag) {
636                 if (iflag) {
637                         if (tp->pr_istats)
638                                 intpr(interval, nl[N_IFNET].n_value,
639                                       tp->pr_istats);
640                         else if (pflag)
641                                 printf("%s: no per-interface stats routine\n",
642                                     tp->pr_name);
643                         return;
644                 } else {
645                         pr = tp->pr_stats;
646                         if (!pr) {
647                                 if (pflag)
648                                         printf("%s: no stats routine\n",
649                                             tp->pr_name);
650                                 return;
651                         }
652                         if (tp->pr_usesysctl && live)
653                                 off = 0;
654                         else if (tp->pr_sindex < 0) {
655                                 if (pflag)
656                                         printf(
657                                     "%s: stats routine doesn't work on cores\n",
658                                             tp->pr_name);
659                                 return;
660                         } else
661                                 off = nl[tp->pr_sindex].n_value;
662                 }
663         } else {
664                 pr = tp->pr_cblocks;
665                 if (!pr) {
666                         if (pflag)
667                                 printf("%s: no PCB routine\n", tp->pr_name);
668                         return;
669                 }
670                 if (tp->pr_usesysctl && live)
671                         off = 0;
672                 else if (tp->pr_index < 0) {
673                         if (pflag)
674                                 printf(
675                                     "%s: PCB routine doesn't work on cores\n",
676                                     tp->pr_name);
677                         return;
678                 } else
679                         off = nl[tp->pr_index].n_value;
680         }
681         if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
682             af != AF_UNSPEC))
683                 (*pr)(off, name, af, tp->pr_protocol);
684 }
685
686 /*
687  * Read kernel memory, return 0 on success.
688  */
689 int
690 kread(u_long addr, void *buf, size_t size)
691 {
692         char errbuf[_POSIX2_LINE_MAX];
693
694         if (kvmd == NULL) {
695                 kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
696                 setgid(getgid());
697                 if (kvmd != NULL) {
698                         if (kvm_nlist(kvmd, nl) < 0) {
699                                 if (nlistf)
700                                         errx(1, "%s: kvm_nlist: %s", nlistf,
701                                              kvm_geterr(kvmd));
702                                 else
703                                         errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
704                         }
705
706                         if (nl[0].n_type == 0) {
707                                 if (nlistf)
708                                         errx(1, "%s: no namelist", nlistf);
709                                 else
710                                         errx(1, "no namelist");
711                         }
712                 } else {
713                         warnx("kvm not available: %s", errbuf);
714                         return(-1);
715                 }
716         }
717         if (!buf)
718                 return (0);
719         if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
720                 warnx("%s", kvm_geterr(kvmd));
721                 return (-1);
722         }
723         return (0);
724 }
725
726 const char *
727 plural(uintmax_t n)
728 {
729         return (n != 1 ? "s" : "");
730 }
731
732 const char *
733 plurales(uintmax_t n)
734 {
735         return (n != 1 ? "es" : "");
736 }
737
738 const char *
739 pluralies(uintmax_t n)
740 {
741         return (n != 1 ? "ies" : "y");
742 }
743
744 /*
745  * Find the protox for the given "well-known" name.
746  */
747 static struct protox *
748 knownname(const char *name)
749 {
750         struct protox **tpp, *tp;
751
752         for (tpp = protoprotox; *tpp; tpp++)
753                 for (tp = *tpp; tp->pr_name; tp++)
754                         if (strcmp(tp->pr_name, name) == 0)
755                                 return (tp);
756         return (NULL);
757 }
758
759 /*
760  * Find the protox corresponding to name.
761  */
762 static struct protox *
763 name2protox(const char *name)
764 {
765         struct protox *tp;
766         char **alias;                   /* alias from p->aliases */
767         struct protoent *p;
768
769         /*
770          * Try to find the name in the list of "well-known" names. If that
771          * fails, check if name is an alias for an Internet protocol.
772          */
773         if ((tp = knownname(name)) != NULL)
774                 return (tp);
775
776         setprotoent(1);                 /* make protocol lookup cheaper */
777         while ((p = getprotoent()) != NULL) {
778                 /* assert: name not same as p->name */
779                 for (alias = p->p_aliases; *alias; alias++)
780                         if (strcmp(name, *alias) == 0) {
781                                 endprotoent();
782                                 return (knownname(p->p_name));
783                         }
784         }
785         endprotoent();
786         return (NULL);
787 }
788
789 static void
790 usage(void)
791 {
792         (void)fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
793 "usage: netstat [-AaLnSTWx] [-f protocol_family | -p protocol]\n"
794 "               [-M core] [-N system]",
795 "       netstat -i | -I interface [-abdhntW] [-f address_family]\n"
796 "               [-M core] [-N system]",
797 "       netstat -w wait [-I interface] [-d] [-M core] [-N system] [-q howmany]",
798 "       netstat -s [-s] [-z] [-f protocol_family | -p protocol]\n"
799 "               [-M core] [-N system]",
800 "       netstat -i | -I interface -s [-f protocol_family | -p protocol]\n"
801 "               [-M core] [-N system]",
802 "       netstat -m [-M core] [-N system]",
803 "       netstat -B [-I interface]",
804 "       netstat -r [-AanW] [-f address_family] [-M core] [-N system]",
805 "       netstat -rs [-s] [-M core] [-N system]",
806 "       netstat -g [-W] [-f address_family] [-M core] [-N system]",
807 "       netstat -gs [-s] [-f address_family] [-M core] [-N system]");
808         exit(1);
809 }