1 /* $OpenBSD: main.c,v 1.116 2019/04/28 17:59:51 mpi Exp $ */ 2 /* $NetBSD: main.c,v 1.9 1996/05/07 02:55:02 thorpej Exp $ */ 3 4 /* 5 * Copyright (c) 1983, 1988, 1993 6 * Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. Neither the name of the University nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 */ 32 33 #include <sys/types.h> 34 #include <sys/protosw.h> 35 #include <sys/socket.h> 36 #include <sys/sysctl.h> 37 38 #include <net/route.h> 39 #include <netinet/in.h> 40 41 #include <ctype.h> 42 #include <err.h> 43 #include <errno.h> 44 #include <fcntl.h> 45 #include <kvm.h> 46 #include <limits.h> 47 #include <netdb.h> 48 #include <nlist.h> 49 #include <paths.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <string.h> 53 #include <unistd.h> 54 #include "netstat.h" 55 56 struct nlist nl[] = { 57 #define N_AFMAP 0 58 { "_afmap"}, 59 #define N_AF2IDX 1 60 { "_af2idx" }, 61 #define N_AF2IDXMAX 2 62 { "_af2idx_max" }, 63 64 { "" } 65 }; 66 67 struct protox { 68 void (*pr_stats)(char *); /* statistics printing routine */ 69 char *pr_name; /* well-known name */ 70 int pr_proto; /* protocol number */ 71 } protox[] = { 72 { ip_stats, "ip", IPPROTO_IPV4 }, 73 { icmp_stats, "icmp", 0 }, 74 { igmp_stats, "igmp", 0 }, 75 { ipip_stats, "ipencap", 0 }, 76 { tcp_stats, "tcp", IPPROTO_TCP }, 77 { udp_stats, "udp", IPPROTO_UDP }, 78 { ipsec_stats, "ipsec", 0 }, 79 { esp_stats, "esp", 0 }, 80 { ah_stats, "ah", 0 }, 81 { etherip_stats,"etherip", 0 }, 82 { ipcomp_stats, "ipcomp", 0 }, 83 { carp_stats, "carp", 0 }, 84 { pfsync_stats, "pfsync", 0 }, 85 { div_stats, "divert", IPPROTO_DIVERT }, 86 { pflow_stats, "pflow", 0 }, 87 { NULL, NULL, 0 } 88 }; 89 90 struct protox ip6protox[] = { 91 { ip6_stats, "ip6", IPPROTO_IPV6 }, 92 { div6_stats, "divert6", IPPROTO_DIVERT }, 93 { icmp6_stats, "icmp6", 0 }, 94 { rip6_stats, "rip6", 0 }, 95 { NULL, NULL, 0 } 96 }; 97 98 struct protox *protoprotox[] = { 99 protox, ip6protox, NULL 100 }; 101 102 static void usage(void); 103 static struct protox *name2protox(char *); 104 static struct protox *knownname(char *); 105 void gettable(u_int); 106 107 kvm_t *kvmd; 108 109 int 110 main(int argc, char *argv[]) 111 { 112 extern char *optarg; 113 extern int optind; 114 const char *errstr; 115 struct protox *tp = NULL; /* for printing cblocks & stats */ 116 int ch; 117 char *nlistf = NULL, *memf = NULL, *ep; 118 char buf[_POSIX2_LINE_MAX]; 119 u_long pcbaddr = 0; 120 u_int tableid; 121 int Tflag = 0; 122 int repeatcount = 0; 123 int proto = 0; 124 int need_nlist, kvm_flags = O_RDONLY; 125 126 af = AF_UNSPEC; 127 tableid = getrtable(); 128 129 while ((ch = getopt(argc, argv, 130 "AaBbc:deFf:ghI:iLlM:mN:np:P:qrsT:tuvW:w:")) != -1) 131 switch (ch) { 132 case 'A': 133 Aflag = 1; 134 break; 135 case 'a': 136 aflag = 1; 137 break; 138 case 'B': 139 Bflag = 1; 140 break; 141 case 'b': 142 bflag = 1; 143 break; 144 case 'c': 145 repeatcount = strtonum(optarg, 1, INT_MAX, &errstr); 146 if (errstr) 147 errx(1, "count is %s", errstr); 148 break; 149 case 'd': 150 dflag = IF_SHOW_DROP; 151 break; 152 case 'e': 153 dflag = IF_SHOW_ERRS; 154 break; 155 case 'F': 156 Fflag = 1; 157 break; 158 case 'f': 159 if (strcmp(optarg, "inet") == 0) 160 af = AF_INET; 161 else if (strcmp(optarg, "inet6") == 0) 162 af = AF_INET6; 163 else if (strcmp(optarg, "local") == 0) 164 af = AF_LOCAL; 165 else if (strcmp(optarg, "unix") == 0) 166 af = AF_UNIX; 167 else if (strcmp(optarg, "mpls") == 0) 168 af = AF_MPLS; 169 else { 170 (void)fprintf(stderr, 171 "%s: %s: unknown address family\n", 172 __progname, optarg); 173 exit(1); 174 } 175 break; 176 case 'g': 177 gflag = 1; 178 break; 179 case 'h': 180 hflag = 1; 181 break; 182 case 'I': 183 iflag = 1; 184 interface = optarg; 185 break; 186 case 'i': 187 iflag = 1; 188 break; 189 case 'l': 190 lflag = 1; 191 break; 192 case 'M': 193 memf = optarg; 194 break; 195 case 'm': 196 mflag = 1; 197 break; 198 case 'N': 199 nlistf = optarg; 200 break; 201 case 'n': 202 nflag = 1; 203 break; 204 case 'p': 205 if ((tp = name2protox(optarg)) == NULL) { 206 (void)fprintf(stderr, 207 "%s: %s: unknown protocol\n", 208 __progname, optarg); 209 exit(1); 210 } 211 pflag = 1; 212 break; 213 case 'P': 214 errno = 0; 215 pcbaddr = strtoul(optarg, &ep, 16); 216 if (optarg[0] == '\0' || *ep != '\0' || 217 errno == ERANGE) { 218 (void)fprintf(stderr, 219 "%s: %s: invalid PCB address\n", 220 __progname, optarg); 221 exit(1); 222 } 223 Pflag = 1; 224 break; 225 case 'q': 226 qflag = 1; 227 break; 228 case 'r': 229 rflag = 1; 230 break; 231 case 's': 232 ++sflag; 233 break; 234 case 'T': 235 tableid = strtonum(optarg, 0, RT_TABLEID_MAX, &errstr); 236 if (errstr) 237 errx(1, "invalid table id: %s", errstr); 238 Tflag = 1; 239 break; 240 case 't': 241 tflag = 1; 242 break; 243 case 'u': 244 af = AF_UNIX; 245 break; 246 case 'v': 247 vflag = 1; 248 break; 249 case 'W': 250 Wflag = 1; 251 interface = optarg; 252 break; 253 case 'w': 254 interval = strtonum(optarg, 1, INT_MAX, &errstr); 255 if (errstr) 256 errx(1, "interval is %s", errstr); 257 iflag = 1; 258 break; 259 case '?': 260 default: 261 usage(); 262 } 263 argv += optind; 264 argc -= optind; 265 266 if (argc) { 267 interval = strtonum(*argv, 1, INT_MAX, &errstr); 268 if (errstr) 269 errx(1, "interval is %s", errstr); 270 ++argv; 271 --argc; 272 iflag = 1; 273 } 274 if (argc) 275 usage(); 276 277 /* 278 * Show per-interface statistics which don't need access to 279 * kernel memory (they're using IOCTLs) 280 */ 281 if (Wflag) { 282 if (interface == NULL) 283 usage(); 284 net80211_ifstats(interface); 285 exit(0); 286 } 287 288 if (mflag) { 289 mbpr(); 290 exit(0); 291 } 292 if (iflag) { 293 intpr(interval, repeatcount); 294 exit(0); 295 } 296 if (sflag) { 297 if (rflag) { 298 rt_stats(); 299 } else if (gflag) { 300 if (af == AF_INET || af == AF_UNSPEC) 301 mrt_stats(); 302 if (af == AF_INET6 || af == AF_UNSPEC) 303 mrt6_stats(); 304 } else if (pflag && tp->pr_name) { 305 (*tp->pr_stats)(tp->pr_name); 306 } else { 307 if (af == AF_INET || af == AF_UNSPEC) 308 for (tp = protox; tp->pr_name; tp++) 309 (*tp->pr_stats)(tp->pr_name); 310 if (af == AF_INET6 || af == AF_UNSPEC) 311 for (tp = ip6protox; tp->pr_name; tp++) 312 (*tp->pr_stats)(tp->pr_name); 313 } 314 exit(0); 315 } 316 if (gflag) { 317 if (af == AF_INET || af == AF_UNSPEC) 318 mroutepr(); 319 if (af == AF_INET6 || af == AF_UNSPEC) 320 mroute6pr(); 321 exit(0); 322 } 323 324 /* 325 * The remaining code may need kvm so lets try to open it. 326 * -r and -P are the only bits left that actually can use this. 327 */ 328 need_nlist = (nlistf != NULL) || (memf != NULL) || (Aflag && rflag); 329 if (!need_nlist && !Pflag) 330 kvm_flags |= KVM_NO_FILES; 331 332 if ((kvmd = kvm_openfiles(nlistf, memf, NULL, kvm_flags, buf)) == NULL) 333 errx(1, "kvm_openfiles: %s", buf); 334 335 if (need_nlist && (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0)) { 336 if (nlistf) 337 errx(1, "%s: no namelist", nlistf); 338 else 339 errx(1, "no namelist"); 340 } 341 342 if (!need_nlist && Tflag) 343 gettable(tableid); 344 345 if (rflag) { 346 if (Aflag || nlistf != NULL || memf != NULL) 347 routepr(nl[N_AFMAP].n_value, nl[N_AF2IDX].n_value, 348 nl[N_AF2IDXMAX].n_value, tableid); 349 else 350 p_rttables(af, tableid); 351 exit(0); 352 } 353 354 if (pflag) { 355 if (tp->pr_proto == 0) 356 errx(1, "no protocol handler for protocol %s", 357 tp->pr_name); 358 else 359 proto = tp->pr_proto; 360 } 361 362 protopr(kvmd, pcbaddr, tableid, proto); 363 exit(0); 364 } 365 366 /* 367 * Read kernel memory, return 0 on success. 368 */ 369 int 370 kread(u_long addr, void *buf, int size) 371 { 372 373 if (kvm_read(kvmd, addr, buf, size) != size) { 374 (void)fprintf(stderr, "%s: %s\n", __progname, 375 kvm_geterr(kvmd)); 376 return (-1); 377 } 378 return (0); 379 } 380 381 char * 382 plural(u_int64_t n) 383 { 384 return (n != 1 ? "s" : ""); 385 } 386 387 char * 388 plurales(u_int64_t n) 389 { 390 return (n != 1 ? "es" : ""); 391 } 392 393 char * 394 pluralys(u_int64_t n) 395 { 396 return (n != 1 ? "ies" : "y"); 397 } 398 399 /* 400 * Find the protox for the given "well-known" name. 401 */ 402 static struct protox * 403 knownname(char *name) 404 { 405 struct protox **tpp, *tp; 406 407 for (tpp = protoprotox; *tpp; tpp++) 408 for (tp = *tpp; tp->pr_name; tp++) 409 if (strcmp(tp->pr_name, name) == 0) 410 return (tp); 411 return (NULL); 412 } 413 414 /* 415 * Find the protox corresponding to name. 416 */ 417 static struct protox * 418 name2protox(char *name) 419 { 420 struct protox *tp; 421 char **alias; /* alias from p->aliases */ 422 struct protoent *p; 423 424 /* 425 * Try to find the name in the list of "well-known" names. If that 426 * fails, check if name is an alias for an Internet protocol. 427 */ 428 if ((tp = knownname(name))) 429 return (tp); 430 431 setprotoent(1); /* make protocol lookup cheaper */ 432 while ((p = getprotoent())) { 433 /* assert: name not same as p->name */ 434 for (alias = p->p_aliases; *alias; alias++) 435 if (strcmp(name, *alias) == 0) { 436 endprotoent(); 437 return (knownname(p->p_name)); 438 } 439 } 440 endprotoent(); 441 return (NULL); 442 } 443 444 static void 445 usage(void) 446 { 447 (void)fprintf(stderr, 448 "usage: %s [-AaBln] [-f address_family] [-M core] [-N system]\n" 449 " %s [-bdeFgilmnqrstu] [-f address_family] [-M core] [-N system]\n" 450 " [-T rtable]\n" 451 " %s [-bdehn] [-c count] [-I interface] [-M core] [-N system] [-w wait]\n" 452 " %s [-v] [-M core] [-N system] -P pcbaddr\n" 453 " %s [-s] [-M core] [-N system] [-p protocol]\n" 454 " %s [-a] [-f address_family] [-i | -I interface]\n" 455 " %s [-W interface]\n", 456 __progname, __progname, __progname, __progname, 457 __progname, __progname, __progname); 458 exit(1); 459 } 460 461 void 462 gettable(u_int tableid) 463 { 464 struct rt_tableinfo info; 465 int mib[6]; 466 size_t len; 467 468 mib[0] = CTL_NET; 469 mib[1] = PF_ROUTE; 470 mib[2] = 0; 471 mib[3] = 0; 472 mib[4] = NET_RT_TABLE; 473 mib[5] = tableid; 474 475 len = sizeof(info); 476 if (sysctl(mib, 6, &info, &len, NULL, 0) == -1) 477 err(1, "routing table %d", tableid); 478 } 479