1 /* $OpenBSD: main.c,v 1.113 2018/08/13 14:36:54 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_RTREE 0 58 { "_rt_tables"}, 59 #define N_RTMASK 1 60 { "_mask_rnhead" }, 61 #define N_AF2RTAFIDX 2 62 { "_af2rtafidx" }, 63 #define N_RTBLIDMAX 3 64 { "_rtbl_id_max" }, 65 66 { "" } 67 }; 68 69 struct protox { 70 void (*pr_stats)(char *); /* statistics printing routine */ 71 char *pr_name; /* well-known name */ 72 int pr_proto; /* protocol number */ 73 } protox[] = { 74 { ip_stats, "ip", IPPROTO_IPV4 }, 75 { icmp_stats, "icmp", 0 }, 76 { igmp_stats, "igmp", 0 }, 77 { ipip_stats, "ipencap", 0 }, 78 { tcp_stats, "tcp", IPPROTO_TCP }, 79 { udp_stats, "udp", IPPROTO_UDP }, 80 { ipsec_stats, "ipsec", 0 }, 81 { esp_stats, "esp", 0 }, 82 { ah_stats, "ah", 0 }, 83 { etherip_stats,"etherip", 0 }, 84 { ipcomp_stats, "ipcomp", 0 }, 85 { carp_stats, "carp", 0 }, 86 { pfsync_stats, "pfsync", 0 }, 87 { div_stats, "divert", IPPROTO_DIVERT }, 88 { pflow_stats, "pflow", 0 }, 89 { NULL, NULL, 0 } 90 }; 91 92 struct protox ip6protox[] = { 93 { ip6_stats, "ip6", IPPROTO_IPV6 }, 94 { div6_stats, "divert6", IPPROTO_DIVERT }, 95 { icmp6_stats, "icmp6", 0 }, 96 { rip6_stats, "rip6", 0 }, 97 { NULL, NULL, 0 } 98 }; 99 100 struct protox *protoprotox[] = { 101 protox, ip6protox, NULL 102 }; 103 104 static void usage(void); 105 static struct protox *name2protox(char *); 106 static struct protox *knownname(char *); 107 u_int gettable(const char *); 108 109 kvm_t *kvmd; 110 111 int 112 main(int argc, char *argv[]) 113 { 114 extern char *optarg; 115 extern int optind; 116 const char *errstr; 117 struct protox *tp = NULL; /* for printing cblocks & stats */ 118 int ch; 119 char *nlistf = NULL, *memf = NULL, *ep; 120 char buf[_POSIX2_LINE_MAX]; 121 u_long pcbaddr = 0; 122 u_int tableid; 123 int Tflag = 0; 124 int repeatcount = 0; 125 int proto = 0; 126 int need_nlist, kvm_flags = O_RDONLY; 127 128 af = AF_UNSPEC; 129 tableid = getrtable(); 130 131 while ((ch = getopt(argc, argv, 132 "AaBbc:dFf:ghI:iLlM:mN:np:P:qrsT:tuvW:w:")) != -1) 133 switch (ch) { 134 case 'A': 135 Aflag = 1; 136 break; 137 case 'a': 138 aflag = 1; 139 break; 140 case 'B': 141 Bflag = 1; 142 break; 143 case 'b': 144 bflag = 1; 145 break; 146 case 'c': 147 repeatcount = strtonum(optarg, 1, INT_MAX, &errstr); 148 if (errstr) 149 errx(1, "count is %s", errstr); 150 break; 151 case 'd': 152 dflag = 1; 153 break; 154 case 'F': 155 Fflag = 1; 156 break; 157 case 'f': 158 if (strcmp(optarg, "inet") == 0) 159 af = AF_INET; 160 else if (strcmp(optarg, "inet6") == 0) 161 af = AF_INET6; 162 else if (strcmp(optarg, "local") == 0) 163 af = AF_LOCAL; 164 else if (strcmp(optarg, "unix") == 0) 165 af = AF_UNIX; 166 else if (strcmp(optarg, "mpls") == 0) 167 af = AF_MPLS; 168 else if (strcmp(optarg, "mask") == 0) 169 af = 0xff; 170 else { 171 (void)fprintf(stderr, 172 "%s: %s: unknown address family\n", 173 __progname, optarg); 174 exit(1); 175 } 176 break; 177 case 'g': 178 gflag = 1; 179 break; 180 case 'h': 181 hflag = 1; 182 break; 183 case 'I': 184 iflag = 1; 185 interface = optarg; 186 break; 187 case 'i': 188 iflag = 1; 189 break; 190 case 'l': 191 lflag = 1; 192 break; 193 case 'M': 194 memf = optarg; 195 break; 196 case 'm': 197 mflag = 1; 198 break; 199 case 'N': 200 nlistf = optarg; 201 break; 202 case 'n': 203 nflag = 1; 204 break; 205 case 'p': 206 if ((tp = name2protox(optarg)) == NULL) { 207 (void)fprintf(stderr, 208 "%s: %s: unknown protocol\n", 209 __progname, optarg); 210 exit(1); 211 } 212 pflag = 1; 213 break; 214 case 'P': 215 errno = 0; 216 pcbaddr = strtoul(optarg, &ep, 16); 217 if (optarg[0] == '\0' || *ep != '\0' || 218 errno == ERANGE) { 219 (void)fprintf(stderr, 220 "%s: %s: invalid PCB address\n", 221 __progname, optarg); 222 exit(1); 223 } 224 Pflag = 1; 225 break; 226 case 'q': 227 qflag = 1; 228 break; 229 case 'r': 230 rflag = 1; 231 break; 232 case 's': 233 ++sflag; 234 break; 235 case 'T': 236 Tflag = 1; 237 tableid = gettable(optarg); 238 break; 239 case 't': 240 tflag = 1; 241 break; 242 case 'u': 243 af = AF_UNIX; 244 break; 245 case 'v': 246 vflag = 1; 247 break; 248 case 'W': 249 Wflag = 1; 250 interface = optarg; 251 break; 252 case 'w': 253 interval = strtonum(optarg, 1, INT_MAX, &errstr); 254 if (errstr) 255 errx(1, "interval is %s", errstr); 256 iflag = 1; 257 break; 258 case '?': 259 default: 260 usage(); 261 } 262 argv += optind; 263 argc -= optind; 264 265 if (argc) { 266 interval = strtonum(*argv, 1, INT_MAX, &errstr); 267 if (errstr) 268 errx(1, "interval is %s", errstr); 269 ++argv; 270 --argc; 271 iflag = 1; 272 } 273 if (argc) 274 usage(); 275 276 /* 277 * Show per-interface statistics which don't need access to 278 * kernel memory (they're using IOCTLs) 279 */ 280 if (Wflag) { 281 if (interface == NULL) 282 usage(); 283 net80211_ifstats(interface); 284 exit(0); 285 } 286 287 if (mflag) { 288 mbpr(); 289 exit(0); 290 } 291 if (iflag) { 292 intpr(interval, repeatcount); 293 exit(0); 294 } 295 if (sflag) { 296 if (rflag) { 297 rt_stats(); 298 } else if (gflag) { 299 if (af == AF_INET || af == AF_UNSPEC) 300 mrt_stats(); 301 if (af == AF_INET6 || af == AF_UNSPEC) 302 mrt6_stats(); 303 } else if (pflag && tp->pr_name) { 304 (*tp->pr_stats)(tp->pr_name); 305 } else { 306 if (af == AF_INET || af == AF_UNSPEC) 307 for (tp = protox; tp->pr_name; tp++) 308 (*tp->pr_stats)(tp->pr_name); 309 if (af == AF_INET6 || af == AF_UNSPEC) 310 for (tp = ip6protox; tp->pr_name; tp++) 311 (*tp->pr_stats)(tp->pr_name); 312 } 313 exit(0); 314 } 315 if (gflag) { 316 if (af == AF_INET || af == AF_UNSPEC) 317 mroutepr(); 318 if (af == AF_INET6 || af == AF_UNSPEC) 319 mroute6pr(); 320 exit(0); 321 } 322 323 /* 324 * The remaining code may need kvm so lets try to open it. 325 * -r and -P are the only bits left that actually can use this. 326 */ 327 need_nlist = (nlistf != NULL) || (memf != NULL) || (Aflag && rflag); 328 if (!need_nlist && !Pflag) 329 kvm_flags |= KVM_NO_FILES; 330 331 if ((kvmd = kvm_openfiles(nlistf, memf, NULL, kvm_flags, buf)) == NULL) 332 errx(1, "kvm_openfiles: %s", buf); 333 334 if (need_nlist && (kvm_nlist(kvmd, nl) < 0 || nl[0].n_type == 0)) { 335 if (nlistf) 336 errx(1, "%s: no namelist", nlistf); 337 else 338 errx(1, "no namelist"); 339 } 340 341 if (rflag) { 342 if (Aflag || nlistf != NULL || memf != NULL) 343 routepr(nl[N_RTREE].n_value, nl[N_RTMASK].n_value, 344 nl[N_AF2RTAFIDX].n_value, nl[N_RTBLIDMAX].n_value, 345 tableid); 346 else 347 p_rttables(af, tableid); 348 exit(0); 349 } 350 351 if (pflag) { 352 if (tp->pr_proto == 0) 353 errx(1, "no protocol handler for protocol %s", 354 tp->pr_name); 355 else 356 proto = tp->pr_proto; 357 } 358 359 protopr(kvmd, pcbaddr, tableid, proto); 360 exit(0); 361 } 362 363 /* 364 * Read kernel memory, return 0 on success. 365 */ 366 int 367 kread(u_long addr, void *buf, int size) 368 { 369 370 if (kvm_read(kvmd, addr, buf, size) != size) { 371 (void)fprintf(stderr, "%s: %s\n", __progname, 372 kvm_geterr(kvmd)); 373 return (-1); 374 } 375 return (0); 376 } 377 378 char * 379 plural(u_int64_t n) 380 { 381 return (n != 1 ? "s" : ""); 382 } 383 384 char * 385 plurales(u_int64_t n) 386 { 387 return (n != 1 ? "es" : ""); 388 } 389 390 char * 391 pluralys(u_int64_t n) 392 { 393 return (n != 1 ? "ies" : "y"); 394 } 395 396 /* 397 * Find the protox for the given "well-known" name. 398 */ 399 static struct protox * 400 knownname(char *name) 401 { 402 struct protox **tpp, *tp; 403 404 for (tpp = protoprotox; *tpp; tpp++) 405 for (tp = *tpp; tp->pr_name; tp++) 406 if (strcmp(tp->pr_name, name) == 0) 407 return (tp); 408 return (NULL); 409 } 410 411 /* 412 * Find the protox corresponding to name. 413 */ 414 static struct protox * 415 name2protox(char *name) 416 { 417 struct protox *tp; 418 char **alias; /* alias from p->aliases */ 419 struct protoent *p; 420 421 /* 422 * Try to find the name in the list of "well-known" names. If that 423 * fails, check if name is an alias for an Internet protocol. 424 */ 425 if ((tp = knownname(name))) 426 return (tp); 427 428 setprotoent(1); /* make protocol lookup cheaper */ 429 while ((p = getprotoent())) { 430 /* assert: name not same as p->name */ 431 for (alias = p->p_aliases; *alias; alias++) 432 if (strcmp(name, *alias) == 0) { 433 endprotoent(); 434 return (knownname(p->p_name)); 435 } 436 } 437 endprotoent(); 438 return (NULL); 439 } 440 441 static void 442 usage(void) 443 { 444 (void)fprintf(stderr, 445 "usage: %s [-AaBln] [-f address_family] [-M core] [-N system]\n" 446 " %s [-bdFgilmnqrstu] [-f address_family] [-M core] [-N system]\n" 447 " [-T tableid]\n" 448 " %s [-bdhn] [-c count] [-I interface] [-M core] [-N system] [-w wait]\n" 449 " %s [-v] [-M core] [-N system] -P pcbaddr\n" 450 " %s [-s] [-M core] [-N system] [-p protocol]\n" 451 " %s [-a] [-f address_family] [-i | -I interface]\n" 452 " %s [-W interface]\n", 453 __progname, __progname, __progname, __progname, 454 __progname, __progname, __progname); 455 exit(1); 456 } 457 458 u_int 459 gettable(const char *s) 460 { 461 const char *errstr; 462 struct rt_tableinfo info; 463 int mib[6]; 464 size_t len; 465 u_int tableid; 466 467 tableid = strtonum(s, 0, RT_TABLEID_MAX, &errstr); 468 if (errstr) 469 errx(1, "invalid table id: %s", errstr); 470 471 mib[0] = CTL_NET; 472 mib[1] = PF_ROUTE; 473 mib[2] = 0; 474 mib[3] = 0; 475 mib[4] = NET_RT_TABLE; 476 mib[5] = tableid; 477 478 len = sizeof(info); 479 if (sysctl(mib, 6, &info, &len, NULL, 0) == -1) 480 err(1, "routing table %d", tableid); 481 482 return (tableid); 483 } 484