]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.bin/netstat/main.c
Update to bmake-201802222
[FreeBSD/FreeBSD.git] / usr.bin / netstat / main.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1983, 1988, 1993
5  *      Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. Neither the name of the University nor the names of its contributors
16  *    may be used to endorse or promote products derived from this software
17  *    without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29  * SUCH DAMAGE.
30  */
31
32 #ifndef lint
33 static char const copyright[] =
34 "@(#) Copyright (c) 1983, 1988, 1993\n\
35         Regents of the University of California.  All rights reserved.\n";
36 #endif /* not lint */
37
38 #if 0
39 #ifndef lint
40 static char sccsid[] = "@(#)main.c      8.4 (Berkeley) 3/1/94";
41 #endif /* not lint */
42 #endif
43
44 #include <sys/cdefs.h>
45 __FBSDID("$FreeBSD$");
46
47 #include <sys/param.h>
48 #include <sys/file.h>
49 #include <sys/protosw.h>
50 #include <sys/socket.h>
51 #include <sys/socketvar.h>
52 #include <sys/sysctl.h>
53
54 #include <netinet/in.h>
55
56 #ifdef NETGRAPH
57 #include <netgraph/ng_socket.h>
58 #endif
59
60 #include <ctype.h>
61 #include <err.h>
62 #include <errno.h>
63 #include <kvm.h>
64 #include <limits.h>
65 #include <netdb.h>
66 #include <nlist.h>
67 #include <paths.h>
68 #include <stdint.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <stdbool.h>
72 #include <string.h>
73 #include <unistd.h>
74 #include "netstat.h"
75 #include "nl_defs.h"
76 #include <libxo/xo.h>
77
78 static struct protox {
79         int     pr_index;               /* index into nlist of cb head */
80         int     pr_sindex;              /* index into nlist of stat block */
81         u_char  pr_wanted;              /* 1 if wanted, 0 otherwise */
82         void    (*pr_cblocks)(u_long, const char *, int, int);
83                                         /* control blocks printing routine */
84         void    (*pr_stats)(u_long, const char *, int, int);
85                                         /* statistics printing routine */
86         void    (*pr_istats)(char *);   /* per/if statistics printing routine */
87         const char      *pr_name;               /* well-known name */
88         int     pr_usesysctl;           /* non-zero if we use sysctl, not kvm */
89         int     pr_protocol;
90 } protox[] = {
91         { N_TCBINFO,    N_TCPSTAT,      1,      protopr,
92           tcp_stats,    NULL,           "tcp",  1,      IPPROTO_TCP },
93         { N_UDBINFO,    N_UDPSTAT,      1,      protopr,
94           udp_stats,    NULL,           "udp",  1,      IPPROTO_UDP },
95 #ifdef SCTP
96         { -1,           N_SCTPSTAT,     1,      sctp_protopr,
97           sctp_stats,   NULL,           "sctp", 1,      IPPROTO_SCTP },
98 #endif
99 #ifdef SDP
100         { -1,           -1,             1,      protopr,
101          NULL,          NULL,           "sdp",  1,      IPPROTO_TCP },
102 #endif
103         { N_DIVCBINFO,  -1,             1,      protopr,
104           NULL,         NULL,           "divert", 1,    IPPROTO_DIVERT },
105         { N_RIPCBINFO,  N_IPSTAT,       1,      protopr,
106           ip_stats,     NULL,           "ip",   1,      IPPROTO_RAW },
107         { N_RIPCBINFO,  N_ICMPSTAT,     1,      protopr,
108           icmp_stats,   NULL,           "icmp", 1,      IPPROTO_ICMP },
109         { N_RIPCBINFO,  N_IGMPSTAT,     1,      protopr,
110           igmp_stats,   NULL,           "igmp", 1,      IPPROTO_IGMP },
111 #ifdef IPSEC
112         { -1,           N_IPSEC4STAT,   1,      NULL,   /* keep as compat */
113           ipsec_stats,  NULL,           "ipsec", 1,     0},
114         { -1,           N_AHSTAT,       1,      NULL,
115           ah_stats,     NULL,           "ah",   1,      0},
116         { -1,           N_ESPSTAT,      1,      NULL,
117           esp_stats,    NULL,           "esp",  1,      0},
118         { -1,           N_IPCOMPSTAT,   1,      NULL,
119           ipcomp_stats, NULL,           "ipcomp", 1,    0},
120 #endif
121         { N_RIPCBINFO,  N_PIMSTAT,      1,      protopr,
122           pim_stats,    NULL,           "pim",  1,      IPPROTO_PIM },
123         { -1,           N_CARPSTATS,    1,      NULL,
124           carp_stats,   NULL,           "carp", 1,      0 },
125 #ifdef PF
126         { -1,           N_PFSYNCSTATS,  1,      NULL,
127           pfsync_stats, NULL,           "pfsync", 1,    0 },
128 #endif
129         { -1,           N_ARPSTAT,      1,      NULL,
130           arp_stats,    NULL,           "arp", 1,       0 },
131         { -1,           -1,             0,      NULL,
132           NULL,         NULL,           NULL,   0,      0 }
133 };
134
135 #ifdef INET6
136 static struct protox ip6protox[] = {
137         { N_TCBINFO,    N_TCPSTAT,      1,      protopr,
138           tcp_stats,    NULL,           "tcp",  1,      IPPROTO_TCP },
139         { N_UDBINFO,    N_UDPSTAT,      1,      protopr,
140           udp_stats,    NULL,           "udp",  1,      IPPROTO_UDP },
141         { N_RIPCBINFO,  N_IP6STAT,      1,      protopr,
142           ip6_stats,    ip6_ifstats,    "ip6",  1,      IPPROTO_RAW },
143         { N_RIPCBINFO,  N_ICMP6STAT,    1,      protopr,
144           icmp6_stats,  icmp6_ifstats,  "icmp6", 1,     IPPROTO_ICMPV6 },
145 #ifdef SDP
146         { -1,           -1,             1,      protopr,
147          NULL,          NULL,           "sdp",  1,      IPPROTO_TCP },
148 #endif
149 #ifdef IPSEC
150         { -1,           N_IPSEC6STAT,   1,      NULL,
151           ipsec_stats,  NULL,           "ipsec6", 1,    0 },
152 #endif
153 #ifdef notyet
154         { -1,           N_PIM6STAT,     1,      NULL,
155           pim6_stats,   NULL,           "pim6", 1,      0 },
156 #endif
157         { -1,           N_RIP6STAT,     1,      NULL,
158           rip6_stats,   NULL,           "rip6", 1,      0 },
159         { -1,           -1,             0,      NULL,
160           NULL,         NULL,           NULL,   0,      0 }
161 };
162 #endif /*INET6*/
163
164 #ifdef IPSEC
165 static struct protox pfkeyprotox[] = {
166         { -1,           N_PFKEYSTAT,    1,      NULL,
167           pfkey_stats,  NULL,           "pfkey", 0,     0 },
168         { -1,           -1,             0,      NULL,
169           NULL,         NULL,           NULL,   0,      0 }
170 };
171 #endif
172
173 #ifdef NETGRAPH
174 static struct protox netgraphprotox[] = {
175         { N_NGSOCKLIST, -1,             1,      netgraphprotopr,
176           NULL,         NULL,           "ctrl", 0,      0 },
177         { N_NGSOCKLIST, -1,             1,      netgraphprotopr,
178           NULL,         NULL,           "data", 0,      0 },
179         { -1,           -1,             0,      NULL,
180           NULL,         NULL,           NULL,   0,      0 }
181 };
182 #endif
183
184 static struct protox *protoprotox[] = {
185                                          protox,
186 #ifdef INET6
187                                          ip6protox,
188 #endif
189 #ifdef IPSEC
190                                          pfkeyprotox,
191 #endif
192                                          NULL };
193
194 static void printproto(struct protox *, const char *, bool *);
195 static void usage(void);
196 static struct protox *name2protox(const char *);
197 static struct protox *knownname(const char *);
198
199 static int kresolve_list(struct nlist *_nl);
200
201 static kvm_t *kvmd;
202 static char *nlistf = NULL, *memf = NULL;
203
204 int     Aflag;          /* show addresses of protocol control block */
205 int     aflag;          /* show all sockets (including servers) */
206 static int      Bflag;          /* show information about bpf consumers */
207 int     bflag;          /* show i/f total bytes in/out */
208 int     dflag;          /* show i/f dropped packets */
209 int     gflag;          /* show group (multicast) routing or stats */
210 int     hflag;          /* show counters in human readable format */
211 int     iflag;          /* show interfaces */
212 int     Lflag;          /* show size of listen queues */
213 int     mflag;          /* show memory stats */
214 int     noutputs = 0;   /* how much outputs before we exit */
215 int     numeric_addr;   /* show addresses numerically */
216 int     numeric_port;   /* show ports numerically */
217 static int pflag;       /* show given protocol */
218 static int      Qflag;          /* show netisr information */
219 int     rflag;          /* show routing tables (or routing stats) */
220 int     Rflag;          /* show flow / RSS statistics */
221 int     sflag;          /* show protocol statistics */
222 int     Wflag;          /* wide display */
223 int     Tflag;          /* TCP Information */
224 int     xflag;          /* extra information, includes all socket buffer info */
225 int     zflag;          /* zero stats */
226
227 int     interval;       /* repeat interval for i/f stats */
228
229 char    *interface;     /* desired i/f for stats, or NULL for all i/fs */
230 int     unit;           /* unit number for above */
231
232 static int      af;             /* address family */
233 int     live;           /* true if we are examining a live system */
234
235 int
236 main(int argc, char *argv[])
237 {
238         struct protox *tp = NULL;  /* for printing cblocks & stats */
239         int ch;
240         int fib = -1;
241         char *endptr;
242         bool first = true;
243
244         af = AF_UNSPEC;
245
246         argc = xo_parse_args(argc, argv);
247         if (argc < 0)
248                 exit(EXIT_FAILURE);
249
250         while ((ch = getopt(argc, argv, "46AaBbdF:f:ghI:iLlM:mN:np:Qq:RrSTsuWw:xz"))
251             != -1)
252                 switch(ch) {
253                 case '4':
254 #ifdef INET
255                         af = AF_INET;
256 #else
257                         errx(1, "IPv4 support is not compiled in");
258 #endif
259                         break;
260                 case '6':
261 #ifdef INET6
262                         af = AF_INET6;
263 #else
264                         errx(1, "IPv6 support is not compiled in");
265 #endif
266                         break;
267                 case 'A':
268                         Aflag = 1;
269                         break;
270                 case 'a':
271                         aflag = 1;
272                         break;
273                 case 'B':
274                         Bflag = 1;
275                         break;
276                 case 'b':
277                         bflag = 1;
278                         break;
279                 case 'd':
280                         dflag = 1;
281                         break;
282                 case 'F':
283                         fib = strtol(optarg, &endptr, 0);
284                         if (*endptr != '\0' ||
285                             (fib == 0 && (errno == EINVAL || errno == ERANGE)))
286                                 xo_errx(1, "%s: invalid fib", optarg);
287                         break;
288                 case 'f':
289                         if (strcmp(optarg, "inet") == 0)
290                                 af = AF_INET;
291 #ifdef INET6
292                         else if (strcmp(optarg, "inet6") == 0)
293                                 af = AF_INET6;
294 #endif
295 #ifdef IPSEC
296                         else if (strcmp(optarg, "pfkey") == 0)
297                                 af = PF_KEY;
298 #endif
299                         else if (strcmp(optarg, "unix") == 0 ||
300                                  strcmp(optarg, "local") == 0)
301                                 af = AF_UNIX;
302 #ifdef NETGRAPH
303                         else if (strcmp(optarg, "ng") == 0
304                             || strcmp(optarg, "netgraph") == 0)
305                                 af = AF_NETGRAPH;
306 #endif
307                         else if (strcmp(optarg, "link") == 0)
308                                 af = AF_LINK;
309                         else {
310                                 xo_errx(1, "%s: unknown address family",
311                                     optarg);
312                         }
313                         break;
314                 case 'g':
315                         gflag = 1;
316                         break;
317                 case 'h':
318                         hflag = 1;
319                         break;
320                 case 'I': {
321                         char *cp;
322
323                         iflag = 1;
324                         for (cp = interface = optarg; isalpha(*cp); cp++)
325                                 continue;
326                         unit = atoi(cp);
327                         break;
328                 }
329                 case 'i':
330                         iflag = 1;
331                         break;
332                 case 'L':
333                         Lflag = 1;
334                         break;
335                 case 'M':
336                         memf = optarg;
337                         break;
338                 case 'm':
339                         mflag = 1;
340                         break;
341                 case 'N':
342                         nlistf = optarg;
343                         break;
344                 case 'n':
345                         numeric_addr = numeric_port = 1;
346                         break;
347                 case 'p':
348                         if ((tp = name2protox(optarg)) == NULL) {
349                                 xo_errx(1, "%s: unknown or uninstrumented "
350                                     "protocol", optarg);
351                         }
352                         pflag = 1;
353                         break;
354                 case 'Q':
355                         Qflag = 1;
356                         break;
357                 case 'q':
358                         noutputs = atoi(optarg);
359                         if (noutputs != 0)
360                                 noutputs++;
361                         break;
362                 case 'r':
363                         rflag = 1;
364                         break;
365                 case 'R':
366                         Rflag = 1;
367                         break;
368                 case 's':
369                         ++sflag;
370                         break;
371                 case 'S':
372                         numeric_addr = 1;
373                         break;
374                 case 'u':
375                         af = AF_UNIX;
376                         break;
377                 case 'W':
378                 case 'l':
379                         Wflag = 1;
380                         break;
381                 case 'w':
382                         interval = atoi(optarg);
383                         iflag = 1;
384                         break;
385                 case 'T':
386                         Tflag = 1;
387                         break;
388                 case 'x':
389                         xflag = 1;
390                         break;
391                 case 'z':
392                         zflag = 1;
393                         break;
394                 case '?':
395                 default:
396                         usage();
397                 }
398         argv += optind;
399         argc -= optind;
400
401 #define BACKWARD_COMPATIBILITY
402 #ifdef  BACKWARD_COMPATIBILITY
403         if (*argv) {
404                 if (isdigit(**argv)) {
405                         interval = atoi(*argv);
406                         if (interval <= 0)
407                                 usage();
408                         ++argv;
409                         iflag = 1;
410                 }
411                 if (*argv) {
412                         nlistf = *argv;
413                         if (*++argv)
414                                 memf = *argv;
415                 }
416         }
417 #endif
418
419         /*
420          * Discard setgid privileges if not the running kernel so that bad
421          * guys can't print interesting stuff from kernel memory.
422          */
423         live = (nlistf == NULL && memf == NULL);
424         if (!live) {
425                 if (setgid(getgid()) != 0)
426                         xo_err(-1, "setgid");
427                 /* Load all necessary kvm symbols */
428                 kresolve_list(nl);
429         }
430
431         if (xflag && Tflag)
432                 xo_errx(1, "-x and -T are incompatible, pick one.");
433
434         if (Bflag) {
435                 if (!live)
436                         usage();
437                 bpf_stats(interface);
438                 xo_finish();
439                 exit(0);
440         }
441         if (mflag) {
442                 if (!live) {
443                         if (kread(0, NULL, 0) == 0)
444                                 mbpr(kvmd, nl[N_SFSTAT].n_value);
445                 } else
446                         mbpr(NULL, 0);
447                 xo_finish();
448                 exit(0);
449         }
450         if (Qflag) {
451                 if (!live) {
452                         if (kread(0, NULL, 0) == 0)
453                                 netisr_stats();
454                 } else
455                         netisr_stats();
456                 xo_finish();
457                 exit(0);
458         }
459 #if 0
460         /*
461          * Keep file descriptors open to avoid overhead
462          * of open/close on each call to get* routines.
463          */
464         sethostent(1);
465         setnetent(1);
466 #else
467         /*
468          * This does not make sense any more with DNS being default over
469          * the files.  Doing a setXXXXent(1) causes a tcp connection to be
470          * used for the queries, which is slower.
471          */
472 #endif
473         if (iflag && !sflag) {
474                 xo_open_container("statistics");
475                 intpr(NULL, af);
476                 xo_close_container("statistics");
477                 xo_finish();
478                 exit(0);
479         }
480         if (rflag) {
481                 xo_open_container("statistics");
482                 if (sflag) {
483                         rt_stats();
484                 } else
485                         routepr(fib, af);
486                 xo_close_container("statistics");
487                 xo_finish();
488                 exit(0);
489         }
490
491         if (gflag) {
492                 xo_open_container("statistics");
493                 if (sflag) {
494                         if (af == AF_INET || af == AF_UNSPEC)
495                                 mrt_stats();
496 #ifdef INET6
497                         if (af == AF_INET6 || af == AF_UNSPEC)
498                                 mrt6_stats();
499 #endif
500                 } else {
501                         if (af == AF_INET || af == AF_UNSPEC)
502                                 mroutepr();
503 #ifdef INET6
504                         if (af == AF_INET6 || af == AF_UNSPEC)
505                                 mroute6pr();
506 #endif
507                 }
508                 xo_close_container("statistics");
509                 xo_finish();
510                 exit(0);
511         }
512
513         if (tp) {
514                 xo_open_container("statistics");
515                 printproto(tp, tp->pr_name, &first);
516                 if (!first)
517                         xo_close_list("socket");
518                 xo_close_container("statistics");
519                 xo_finish();
520                 exit(0);
521         }
522
523         xo_open_container("statistics");
524         if (af == AF_INET || af == AF_UNSPEC)
525                 for (tp = protox; tp->pr_name; tp++)
526                         printproto(tp, tp->pr_name, &first);
527 #ifdef INET6
528         if (af == AF_INET6 || af == AF_UNSPEC)
529                 for (tp = ip6protox; tp->pr_name; tp++)
530                         printproto(tp, tp->pr_name, &first);
531 #endif /*INET6*/
532 #ifdef IPSEC
533         if (af == PF_KEY || af == AF_UNSPEC)
534                 for (tp = pfkeyprotox; tp->pr_name; tp++)
535                         printproto(tp, tp->pr_name, &first);
536 #endif /*IPSEC*/
537 #ifdef NETGRAPH
538         if (af == AF_NETGRAPH || af == AF_UNSPEC)
539                 for (tp = netgraphprotox; tp->pr_name; tp++)
540                         printproto(tp, tp->pr_name, &first);
541 #endif /* NETGRAPH */
542         if ((af == AF_UNIX || af == AF_UNSPEC) && !sflag)
543                 unixpr(nl[N_UNP_COUNT].n_value, nl[N_UNP_GENCNT].n_value,
544                     nl[N_UNP_DHEAD].n_value, nl[N_UNP_SHEAD].n_value,
545                     nl[N_UNP_SPHEAD].n_value, &first);
546
547         if (!first)
548                 xo_close_list("socket");
549         xo_close_container("statistics");
550         xo_finish();
551         exit(0);
552 }
553
554 static int
555 fetch_stats_internal(const char *sysctlname, u_long off, void *stats,
556     size_t len, kreadfn_t kreadfn, int zero)
557 {
558         int error;
559
560         if (live) {
561                 memset(stats, 0, len);
562                 if (zero)
563                         error = sysctlbyname(sysctlname, NULL, NULL, stats,
564                             len);
565                 else
566                         error = sysctlbyname(sysctlname, stats, &len, NULL, 0);
567                 if (error == -1 && errno != ENOENT)
568                         xo_warn("sysctl %s", sysctlname);
569         } else {
570                 if (off == 0)
571                         return (1);
572                 error = kreadfn(off, stats, len);
573         }
574         return (error);
575 }
576
577 int
578 fetch_stats(const char *sysctlname, u_long off, void *stats,
579     size_t len, kreadfn_t kreadfn)
580 {
581
582         return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn,
583     zflag));
584 }
585
586 int
587 fetch_stats_ro(const char *sysctlname, u_long off, void *stats,
588     size_t len, kreadfn_t kreadfn)
589 {
590
591         return (fetch_stats_internal(sysctlname, off, stats, len, kreadfn, 0));
592 }
593
594 /*
595  * Print out protocol statistics or control blocks (per sflag).
596  * If the interface was not specifically requested, and the symbol
597  * is not in the namelist, ignore this one.
598  */
599 static void
600 printproto(struct protox *tp, const char *name, bool *first)
601 {
602         void (*pr)(u_long, const char *, int, int);
603         u_long off;
604         bool doingdblocks = false;
605
606         if (sflag) {
607                 if (iflag) {
608                         if (tp->pr_istats)
609                                 intpr(tp->pr_istats, af);
610                         else if (pflag)
611                                 xo_message("%s: no per-interface stats routine",
612                                     tp->pr_name);
613                         return;
614                 } else {
615                         pr = tp->pr_stats;
616                         if (!pr) {
617                                 if (pflag)
618                                         xo_message("%s: no stats routine",
619                                             tp->pr_name);
620                                 return;
621                         }
622                         if (tp->pr_usesysctl && live)
623                                 off = 0;
624                         else if (tp->pr_sindex < 0) {
625                                 if (pflag)
626                                         xo_message("%s: stats routine doesn't "
627                                             "work on cores", tp->pr_name);
628                                 return;
629                         } else
630                                 off = nl[tp->pr_sindex].n_value;
631                 }
632         } else {
633                 doingdblocks = true;
634                 pr = tp->pr_cblocks;
635                 if (!pr) {
636                         if (pflag)
637                                 xo_message("%s: no PCB routine", tp->pr_name);
638                         return;
639                 }
640                 if (tp->pr_usesysctl && live)
641                         off = 0;
642                 else if (tp->pr_index < 0) {
643                         if (pflag)
644                                 xo_message("%s: PCB routine doesn't work on "
645                                     "cores", tp->pr_name);
646                         return;
647                 } else
648                         off = nl[tp->pr_index].n_value;
649         }
650         if (pr != NULL && (off || (live && tp->pr_usesysctl) ||
651             af != AF_UNSPEC)) {
652                 if (doingdblocks && *first) {
653                         xo_open_list("socket");
654                         *first = false;
655                 }
656
657                 (*pr)(off, name, af, tp->pr_protocol);
658         }
659 }
660
661 static int
662 kvmd_init(void)
663 {
664         char errbuf[_POSIX2_LINE_MAX];
665
666         if (kvmd != NULL)
667                 return (0);
668
669         kvmd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY, errbuf);
670         if (setgid(getgid()) != 0)
671                 xo_err(-1, "setgid");
672
673         if (kvmd == NULL) {
674                 xo_warnx("kvm not available: %s", errbuf);
675                 return (-1);
676         }
677
678         return (0);
679 }
680
681 /*
682  * Resolve symbol list, return 0 on success.
683  */
684 static int
685 kresolve_list(struct nlist *_nl)
686 {
687
688         if ((kvmd == NULL) && (kvmd_init() != 0))
689                 return (-1);
690
691         if (_nl[0].n_type != 0)
692                 return (0);
693
694         if (kvm_nlist(kvmd, _nl) < 0) {
695                 if (nlistf)
696                         xo_errx(1, "%s: kvm_nlist: %s", nlistf,
697                             kvm_geterr(kvmd));
698                 else
699                         xo_errx(1, "kvm_nlist: %s", kvm_geterr(kvmd));
700         }
701
702         return (0);
703 }
704
705 /*
706  * Wrapper of kvm_dpcpu_setcpu().
707  */
708 void
709 kset_dpcpu(u_int cpuid)
710 {
711
712         if ((kvmd == NULL) && (kvmd_init() != 0))
713                 xo_errx(-1, "%s: kvm is not available", __func__);
714
715         if (kvm_dpcpu_setcpu(kvmd, cpuid) < 0)
716                 xo_errx(-1, "%s: kvm_dpcpu_setcpu(%u): %s", __func__,
717                     cpuid, kvm_geterr(kvmd)); 
718         return;
719 }
720
721 /*
722  * Read kernel memory, return 0 on success.
723  */
724 int
725 kread(u_long addr, void *buf, size_t size)
726 {
727
728         if (kvmd_init() < 0)
729                 return (-1);
730
731         if (!buf)
732                 return (0);
733         if (kvm_read(kvmd, addr, buf, size) != (ssize_t)size) {
734                 xo_warnx("%s", kvm_geterr(kvmd));
735                 return (-1);
736         }
737         return (0);
738 }
739
740 /*
741  * Read single counter(9).
742  */
743 uint64_t
744 kread_counter(u_long addr)
745 {
746
747         if (kvmd_init() < 0)
748                 return (-1);
749
750         return (kvm_counter_u64_fetch(kvmd, addr));
751 }
752
753 /*
754  * Read an array of N counters in kernel memory into array of N uint64_t's.
755  */
756 int
757 kread_counters(u_long addr, void *buf, size_t size)
758 {
759         uint64_t *c;
760         u_long *counters;
761         size_t i, n;
762
763         if (kvmd_init() < 0)
764                 return (-1);
765
766         if (size % sizeof(uint64_t) != 0) {
767                 xo_warnx("kread_counters: invalid counter set size");
768                 return (-1);
769         }
770
771         n = size / sizeof(uint64_t);
772         if ((counters = malloc(n * sizeof(u_long))) == NULL)
773                 xo_err(-1, "malloc");
774         if (kread(addr, counters, n * sizeof(u_long)) < 0) {
775                 free(counters);
776                 return (-1);
777         }
778
779         c = buf;
780         for (i = 0; i < n; i++)
781                 c[i] = kvm_counter_u64_fetch(kvmd, counters[i]);
782
783         free(counters);
784         return (0);
785 }
786
787 const char *
788 plural(uintmax_t n)
789 {
790         return (n != 1 ? "s" : "");
791 }
792
793 const char *
794 plurales(uintmax_t n)
795 {
796         return (n != 1 ? "es" : "");
797 }
798
799 const char *
800 pluralies(uintmax_t n)
801 {
802         return (n != 1 ? "ies" : "y");
803 }
804
805 /*
806  * Find the protox for the given "well-known" name.
807  */
808 static struct protox *
809 knownname(const char *name)
810 {
811         struct protox **tpp, *tp;
812
813         for (tpp = protoprotox; *tpp; tpp++)
814                 for (tp = *tpp; tp->pr_name; tp++)
815                         if (strcmp(tp->pr_name, name) == 0)
816                                 return (tp);
817         return (NULL);
818 }
819
820 /*
821  * Find the protox corresponding to name.
822  */
823 static struct protox *
824 name2protox(const char *name)
825 {
826         struct protox *tp;
827         char **alias;                   /* alias from p->aliases */
828         struct protoent *p;
829
830         /*
831          * Try to find the name in the list of "well-known" names. If that
832          * fails, check if name is an alias for an Internet protocol.
833          */
834         if ((tp = knownname(name)) != NULL)
835                 return (tp);
836
837         setprotoent(1);                 /* make protocol lookup cheaper */
838         while ((p = getprotoent()) != NULL) {
839                 /* assert: name not same as p->name */
840                 for (alias = p->p_aliases; *alias; alias++)
841                         if (strcmp(name, *alias) == 0) {
842                                 endprotoent();
843                                 return (knownname(p->p_name));
844                         }
845         }
846         endprotoent();
847         return (NULL);
848 }
849
850 static void
851 usage(void)
852 {
853         (void)xo_error("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
854 "usage: netstat [-46AaLnRSTWx] [-f protocol_family | -p protocol]\n"
855 "               [-M core] [-N system]",
856 "       netstat -i | -I interface [-46abdhnW] [-f address_family]\n"
857 "               [-M core] [-N system]",
858 "       netstat -w wait [-I interface] [-46d] [-M core] [-N system]\n"
859 "               [-q howmany]",
860 "       netstat -s [-46sz] [-f protocol_family | -p protocol]\n"
861 "               [-M core] [-N system]",
862 "       netstat -i | -I interface -s [-46s]\n"
863 "               [-f protocol_family | -p protocol] [-M core] [-N system]",
864 "       netstat -m [-M core] [-N system]",
865 "       netstat -B [-z] [-I interface]",
866 "       netstat -r [-46AnW] [-F fibnum] [-f address_family]\n"
867 "               [-M core] [-N system]",
868 "       netstat -rs [-s] [-M core] [-N system]",
869 "       netstat -g [-46W] [-f address_family] [-M core] [-N system]",
870 "       netstat -gs [-46s] [-f address_family] [-M core] [-N system]",
871 "       netstat -Q");
872         xo_finish();
873         exit(1);
874 }