1 /* $OpenBSD: tcpdump.c,v 1.79 2016/11/16 13:47:27 reyk Exp $ */ 2 3 /* 4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 5 * The 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: (1) source code distributions 9 * retain the above copyright notice and this paragraph in its entirety, (2) 10 * distributions including binary code include the above copyright notice and 11 * this paragraph in its entirety in the documentation or other materials 12 * provided with the distribution, and (3) all advertising materials mentioning 13 * features or use of this software display the following acknowledgement: 14 * ``This product includes software developed by the University of California, 15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 16 * the University nor the names of its contributors may be used to endorse 17 * or promote products derived from this software without specific prior 18 * written permission. 19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 22 */ 23 24 /* 25 * tcpdump - monitor tcp/ip traffic on an ethernet. 26 * 27 * First written in 1987 by Van Jacobson, Lawrence Berkeley Laboratory. 28 * Mercilessly hacked and occasionally improved since then via the 29 * combined efforts of Van, Steve McCanne and Craig Leres of LBL. 30 */ 31 32 #include <sys/types.h> 33 #include <sys/time.h> 34 #include <sys/ioctl.h> 35 #include <sys/wait.h> 36 37 #include <netinet/in.h> 38 39 #include <pcap.h> 40 #include <signal.h> 41 #include <stdio.h> 42 #include <stdlib.h> 43 #include <string.h> 44 #include <unistd.h> 45 #include <limits.h> 46 #include <ctype.h> 47 #include <err.h> 48 #include <errno.h> 49 50 #include "interface.h" 51 #include "addrtoname.h" 52 #include "setsignal.h" 53 #include "gmt2local.h" 54 55 #include <sys/socket.h> 56 #include <net/if.h> 57 #include <net/pfvar.h> 58 #include "pfctl.h" 59 #include "pfctl_parser.h" 60 #include "privsep.h" 61 62 int Aflag; /* dump ascii */ 63 int aflag; /* translate network and broadcast addresses */ 64 int dflag; /* print filter code */ 65 int eflag; /* print ethernet header */ 66 int fflag; /* don't translate "foreign" IP address */ 67 int Iflag; /* include interface in output */ 68 int Lflag; /* List available link types */ 69 int nflag; /* leave addresses as numbers */ 70 int Nflag; /* remove domains from printed host names */ 71 int Oflag = 1; /* run filter code optimizer */ 72 int oflag; /* print passive OS fingerprints */ 73 int pflag; /* don't go promiscuous */ 74 int qflag; /* quick (shorter) output */ 75 int Sflag; /* print raw TCP sequence numbers */ 76 int tflag = 1; /* print packet arrival time */ 77 int vflag; /* verbose */ 78 int xflag; /* print packet in hex */ 79 int Xflag; /* print packet in emacs-hexl style */ 80 81 int packettype; 82 83 char *program_name; 84 char *device = NULL; 85 86 int32_t thiszone; /* seconds offset from gmt to local time */ 87 88 extern volatile pid_t child_pid; 89 90 /* Externs */ 91 extern void bpf_dump(struct bpf_program *, int); 92 extern int esp_init(char *); 93 94 /* Forwards */ 95 void cleanup(int); 96 void gotchld(int); 97 extern __dead void usage(void); 98 99 /* Length of saved portion of packet. */ 100 int snaplen = 0; 101 102 struct printer { 103 pcap_handler f; 104 int type; 105 }; 106 107 /* XXX needed if using old bpf.h */ 108 #ifndef DLT_ATM_RFC1483 109 #define DLT_ATM_RFC1483 11 110 #endif 111 112 static struct printer printers[] = { 113 { ether_if_print, DLT_EN10MB }, 114 { ether_if_print, DLT_IEEE802 }, 115 { sl_if_print, DLT_SLIP }, 116 { sl_bsdos_if_print, DLT_SLIP_BSDOS }, 117 { ppp_if_print, DLT_PPP }, 118 { fddi_if_print, DLT_FDDI }, 119 { null_if_print, DLT_NULL }, 120 { raw_if_print, DLT_RAW }, 121 { atm_if_print, DLT_ATM_RFC1483 }, 122 { loop_if_print, DLT_LOOP }, 123 { enc_if_print, DLT_ENC }, 124 { pflog_if_print, DLT_PFLOG }, 125 { pfsync_if_print, DLT_PFSYNC }, 126 { ppp_ether_if_print, DLT_PPP_ETHER }, 127 { ieee802_11_if_print, DLT_IEEE802_11 }, 128 { ieee802_11_radio_if_print, DLT_IEEE802_11_RADIO }, 129 { ofp_if_print, DLT_OPENFLOW }, 130 { NULL, 0 }, 131 }; 132 133 static pcap_handler 134 lookup_printer(int type) 135 { 136 struct printer *p; 137 138 for (p = printers; p->f; ++p) { 139 if (type == p->type) 140 return p->f; 141 } 142 143 error("unknown data link type 0x%x", type); 144 /* NOTREACHED */ 145 } 146 147 static int 148 init_pfosfp(void) 149 { 150 pf_osfp_initialize(); 151 if (pfctl_file_fingerprints(-1, 152 PF_OPT_QUIET|PF_OPT_NOACTION, PF_OSFP_FILE) == 0) 153 return 1; 154 return 0; 155 } 156 157 static pcap_t *pd; 158 159 /* Multiple DLT support */ 160 void pcap_list_linktypes(pcap_t *); 161 void pcap_print_linktype(u_int); 162 163 void 164 pcap_print_linktype(u_int dlt) 165 { 166 const char *name; 167 168 if ((name = pcap_datalink_val_to_name(dlt)) != NULL) 169 fprintf(stderr, "%s\n", name); 170 else 171 fprintf(stderr, "<unknown: %u>\n", dlt); 172 } 173 174 void 175 pcap_list_linktypes(pcap_t *p) 176 { 177 int fd = p->fd; 178 u_int n; 179 180 #define MAXDLT 100 181 182 u_int dltlist[MAXDLT]; 183 struct bpf_dltlist dl = {MAXDLT, dltlist}; 184 185 if (fd < 0) 186 error("Invalid bpf descriptor"); 187 188 if (ioctl(fd, BIOCGDLTLIST, &dl) < 0) 189 err(1, "BIOCGDLTLIST"); 190 191 if (dl.bfl_len > MAXDLT) 192 error("Invalid number of linktypes: %u", dl.bfl_len); 193 194 fprintf(stderr, "%d link type%s supported:\n", dl.bfl_len, 195 dl.bfl_len == 1 ? "" : "s"); 196 197 for (n = 0; n < dl.bfl_len; n++) { 198 fprintf(stderr, "\t"); 199 pcap_print_linktype(dltlist[n]); 200 } 201 } 202 203 int 204 main(int argc, char **argv) 205 { 206 int cnt = -1, op, i; 207 bpf_u_int32 localnet, netmask; 208 char *cp, *infile = NULL, *RFileName = NULL; 209 char ebuf[PCAP_ERRBUF_SIZE], *WFileName = NULL; 210 pcap_handler printer; 211 struct bpf_program *fcode; 212 u_char *pcap_userdata; 213 u_int dirfilt = 0, dlt = (u_int) -1; 214 const char *errstr; 215 216 if ((cp = strrchr(argv[0], '/')) != NULL) 217 program_name = cp + 1; 218 else 219 program_name = argv[0]; 220 221 if (priv_init(argc, argv)) 222 error("Failed to setup privsep"); 223 224 /* state: STATE_INIT */ 225 226 opterr = 0; 227 while ((op = getopt(argc, argv, 228 "Aac:D:deE:fF:i:IlLnNOopqr:s:StT:vw:xXy:Y")) != -1) 229 switch (op) { 230 231 case 'A': 232 xflag = 1; 233 Aflag = 1; 234 break; 235 236 case 'a': 237 aflag = 1; 238 break; 239 240 case 'c': 241 cnt = strtonum(optarg, 1, INT_MAX, &errstr); 242 if (errstr) 243 error("invalid packet count %s: %s", 244 optarg, errstr); 245 break; 246 247 case 'D': 248 if (strcasecmp(optarg, "in") == 0) 249 dirfilt = BPF_DIRECTION_OUT; 250 else if (strcasecmp(optarg, "out") == 0) 251 dirfilt = BPF_DIRECTION_IN; 252 else 253 error("invalid traffic direction %s", optarg); 254 break; 255 256 case 'd': 257 ++dflag; 258 break; 259 case 'e': 260 eflag = 1; 261 break; 262 263 case 'f': 264 fflag = 1; 265 break; 266 267 case 'F': 268 infile = optarg; 269 break; 270 271 case 'i': 272 device = optarg; 273 break; 274 275 case 'I': 276 Iflag = 1; 277 break; 278 279 case 'l': 280 setvbuf(stdout, NULL, _IOLBF, 0); 281 break; 282 case 'L': 283 Lflag = 1; 284 break; 285 case 'n': 286 nflag = 1; 287 break; 288 289 case 'N': 290 Nflag = 1; 291 break; 292 293 case 'O': 294 Oflag = 0; 295 break; 296 297 case 'o': 298 oflag = 1; 299 break; 300 301 case 'p': 302 pflag = 1; 303 break; 304 305 case 'q': 306 qflag = 1; 307 break; 308 309 case 'r': 310 RFileName = optarg; 311 break; 312 313 case 's': 314 snaplen = strtonum(optarg, 1, INT_MAX, &errstr); 315 if (errstr) 316 error("invalid snaplen %s: %s", optarg, errstr); 317 break; 318 319 case 'S': 320 Sflag = 1; 321 break; 322 323 case 't': 324 --tflag; 325 break; 326 327 case 'T': 328 if (strcasecmp(optarg, "vat") == 0) 329 packettype = PT_VAT; 330 else if (strcasecmp(optarg, "wb") == 0) 331 packettype = PT_WB; 332 else if (strcasecmp(optarg, "rpc") == 0) 333 packettype = PT_RPC; 334 else if (strcasecmp(optarg, "rtp") == 0) 335 packettype = PT_RTP; 336 else if (strcasecmp(optarg, "rtcp") == 0) 337 packettype = PT_RTCP; 338 else if (strcasecmp(optarg, "cnfp") == 0) 339 packettype = PT_CNFP; 340 else if (strcasecmp(optarg, "vrrp") == 0) 341 packettype = PT_VRRP; 342 else if (strcasecmp(optarg, "tcp") == 0) 343 packettype = PT_TCP; 344 else if (strcasecmp(optarg, "sack") == 0) 345 /* 346 * kept for compatibility; DEFAULT_SNAPLEN 347 * used to be too short to capture SACK. 348 */ 349 ; 350 else 351 error("unknown packet type `%s'", optarg); 352 break; 353 354 case 'v': 355 ++vflag; 356 break; 357 358 case 'w': 359 WFileName = optarg; 360 break; 361 #ifdef YYDEBUG 362 case 'Y': 363 { 364 /* Undocumented flag */ 365 extern int yydebug; 366 yydebug = 1; 367 } 368 break; 369 #endif 370 case 'y': 371 i = pcap_datalink_name_to_val(optarg); 372 if (i < 0) 373 error("invalid data link type: %s", optarg); 374 dlt = (u_int)i; 375 break; 376 377 case 'x': 378 xflag = 1; 379 break; 380 381 case 'X': 382 Xflag = 1; 383 xflag = 1; 384 break; 385 386 case 'E': 387 if (esp_init(optarg) < 0) 388 error("bad esp specification `%s'", optarg); 389 break; 390 391 default: 392 usage(); 393 /* NOTREACHED */ 394 } 395 396 if (snaplen == 0) { 397 switch (dlt) { 398 case DLT_IEEE802_11: 399 snaplen = IEEE802_11_SNAPLEN; 400 break; 401 case DLT_IEEE802_11_RADIO: 402 snaplen = IEEE802_11_RADIO_SNAPLEN; 403 break; 404 default: 405 snaplen = DEFAULT_SNAPLEN; 406 break; 407 } 408 } 409 410 if (aflag && nflag) 411 error("-a and -n options are incompatible"); 412 413 if (RFileName != NULL) { 414 pd = priv_pcap_offline(RFileName, ebuf); 415 if (pd == NULL) 416 error("%s", ebuf); 417 /* state: STATE_BPF */ 418 localnet = 0; 419 netmask = 0; 420 if (fflag != 0) 421 error("-f and -r options are incompatible"); 422 } else { 423 if (device == NULL) { 424 device = pcap_lookupdev(ebuf); 425 if (device == NULL) 426 error("%s", ebuf); 427 } 428 pd = priv_pcap_live(device, snaplen, !pflag, 1000, ebuf, 429 dlt, dirfilt); 430 if (pd == NULL) 431 error("%s", ebuf); 432 433 /* state: STATE_BPF */ 434 if (pcap_lookupnet(device, &localnet, &netmask, ebuf)) { 435 if (fflag) 436 warning("%s", ebuf); 437 localnet = 0; 438 netmask = 0; 439 } 440 } 441 i = pcap_snapshot(pd); 442 if (snaplen < i) { 443 warning("snaplen raised from %d to %d", snaplen, i); 444 snaplen = i; 445 } 446 447 if (Lflag) { 448 pcap_list_linktypes(pd); 449 exit(0); 450 } 451 452 fcode = priv_pcap_setfilter(pd, Oflag, netmask); 453 /* state: STATE_FILTER */ 454 if (fcode == NULL) 455 error("%s", pcap_geterr(pd)); 456 if (dflag) { 457 bpf_dump(fcode, dflag); 458 exit(0); 459 } 460 init_addrtoname(localnet, netmask); 461 462 if (WFileName) { 463 pcap_dumper_t *p; 464 465 p = priv_pcap_dump_open(pd, WFileName); 466 /* state: STATE_RUN */ 467 if (p == NULL) 468 error("%s", pcap_geterr(pd)); 469 { 470 FILE *fp = (FILE *)p; /* XXX touching pcap guts! */ 471 fflush(fp); 472 setvbuf(fp, NULL, _IONBF, 0); 473 } 474 printer = pcap_dump; 475 pcap_userdata = (u_char *)p; 476 } else { 477 printer = lookup_printer(pcap_datalink(pd)); 478 pcap_userdata = NULL; 479 priv_init_done(); 480 /* state: STATE_RUN */ 481 } 482 if (RFileName == NULL) { 483 (void)fprintf(stderr, "%s: listening on %s, link-type ", 484 program_name, device); 485 pcap_print_linktype(pd->linktype); 486 (void)fflush(stderr); 487 } 488 489 if (oflag) 490 oflag = init_pfosfp(); 491 if (tflag > 0) 492 thiszone = gmt2local(0); 493 494 if (pledge("stdio", NULL) == -1) 495 err(1, "pledge"); 496 497 if (pcap_loop(pd, cnt, printer, pcap_userdata) < 0) { 498 (void)fprintf(stderr, "%s: pcap_loop: %s\n", 499 program_name, pcap_geterr(pd)); 500 exit(1); 501 } 502 pcap_close(pd); 503 exit(0); 504 } 505 506 /* make a clean exit on interrupts */ 507 void 508 cleanup(int signo) 509 { 510 struct pcap_stat stat; 511 sigset_t allsigs; 512 char buf[1024]; 513 514 sigfillset(&allsigs); 515 sigprocmask(SIG_BLOCK, &allsigs, NULL); 516 517 /* Can't print the summary if reading from a savefile */ 518 (void)write(STDERR_FILENO, "\n", 1); 519 if (pd != NULL && pcap_file(pd) == NULL) { 520 if (priv_pcap_stats(&stat) < 0) { 521 (void)snprintf(buf, sizeof buf, 522 "pcap_stats: %s\n", pcap_geterr(pd)); 523 write(STDERR_FILENO, buf, strlen(buf)); 524 } else { 525 (void)snprintf(buf, sizeof buf, 526 "%u packets received by filter\n", stat.ps_recv); 527 write(STDERR_FILENO, buf, strlen(buf)); 528 (void)snprintf(buf, sizeof buf, 529 "%u packets dropped by kernel\n", stat.ps_drop); 530 write(STDERR_FILENO, buf, strlen(buf)); 531 } 532 } 533 _exit(0); 534 } 535 536 void 537 gotchld(int signo) 538 { 539 pid_t pid; 540 int status; 541 int save_err = errno; 542 543 do { 544 pid = waitpid(child_pid, &status, WNOHANG); 545 if (pid > 0 && (WIFEXITED(status) || WIFSIGNALED(status))) 546 cleanup(0); 547 } while (pid == -1 && errno == EINTR); 548 549 if (pid == -1) 550 _exit(1); 551 552 errno = save_err; 553 } 554 555 /* dump the buffer in `emacs-hexl' style */ 556 void 557 default_print_hexl(const u_char *cp, unsigned int length) 558 { 559 unsigned int i, j, jm; 560 int c; 561 char ln[128], buf[128]; 562 563 printf("\n"); 564 for (i = 0; i < length; i += 0x10) { 565 snprintf(ln, sizeof(ln), " %04x: ", (unsigned int)i); 566 jm = length - i; 567 jm = jm > 16 ? 16 : jm; 568 569 for (j = 0; j < jm; j++) { 570 if ((j % 2) == 1) 571 snprintf(buf, sizeof(buf), "%02x ", 572 (unsigned int)cp[i+j]); 573 else 574 snprintf(buf, sizeof(buf), "%02x", 575 (unsigned int)cp[i+j]); 576 strlcat(ln, buf, sizeof ln); 577 } 578 for (; j < 16; j++) { 579 if ((j % 2) == 1) 580 snprintf(buf, sizeof buf, " "); 581 else 582 snprintf(buf, sizeof buf, " "); 583 strlcat(ln, buf, sizeof ln); 584 } 585 586 strlcat(ln, " ", sizeof ln); 587 for (j = 0; j < jm; j++) { 588 c = cp[i+j]; 589 c = isprint(c) ? c : '.'; 590 buf[0] = c; 591 buf[1] = '\0'; 592 strlcat(ln, buf, sizeof ln); 593 } 594 printf("%s\n", ln); 595 } 596 } 597 598 /* dump the text from the buffer */ 599 void 600 default_print_ascii(const u_char *cp, unsigned int length) 601 { 602 int c, i; 603 604 printf("\n"); 605 for (i = 0; i < length; i++) { 606 c = cp[i]; 607 if (isprint(c) || c == '\t' || c == '\n' || c == '\r') 608 putchar(c); 609 else 610 putchar('.'); 611 } 612 } 613 614 /* Like default_print() but data need not be aligned */ 615 void 616 default_print_unaligned(const u_char *cp, u_int length) 617 { 618 u_int i, s; 619 int nshorts; 620 621 if (Xflag) { 622 /* dump the buffer in `emacs-hexl' style */ 623 default_print_hexl(cp, length); 624 } else if (Aflag) { 625 /* dump the text in the buffer */ 626 default_print_ascii(cp, length); 627 } else { 628 /* dump the buffer in old tcpdump style */ 629 nshorts = (u_int) length / sizeof(u_short); 630 i = 0; 631 while (--nshorts >= 0) { 632 if ((i++ % 8) == 0) 633 (void)printf("\n\t\t\t"); 634 s = *cp++; 635 (void)printf(" %02x%02x", s, *cp++); 636 } 637 if (length & 1) { 638 if ((i % 8) == 0) 639 (void)printf("\n\t\t\t"); 640 (void)printf(" %02x", *cp); 641 } 642 } 643 } 644 645 void 646 default_print(const u_char *bp, u_int length) 647 { 648 const u_short *sp; 649 u_int i; 650 int nshorts; 651 652 if (Xflag) { 653 /* dump the buffer in `emacs-hexl' style */ 654 default_print_hexl(bp, length); 655 } else if (Aflag) { 656 /* dump the text in the buffer */ 657 default_print_ascii(bp, length); 658 } else { 659 /* dump the buffer in old tcpdump style */ 660 if ((long)bp & 1) { 661 default_print_unaligned(bp, length); 662 return; 663 } 664 sp = (u_short *)bp; 665 nshorts = (u_int) length / sizeof(u_short); 666 i = 0; 667 while (--nshorts >= 0) { 668 if ((i++ % 8) == 0) 669 (void)printf("\n\t\t\t"); 670 (void)printf(" %04x", ntohs(*sp++)); 671 } 672 if (length & 1) { 673 if ((i % 8) == 0) 674 (void)printf("\n\t\t\t"); 675 (void)printf(" %02x", *(u_char *)sp); 676 } 677 } 678 } 679 680 void 681 set_slave_signals(void) 682 { 683 setsignal(SIGTERM, cleanup); 684 setsignal(SIGINT, cleanup); 685 setsignal(SIGCHLD, gotchld); 686 setsignal(SIGHUP, cleanup); 687 } 688 689 __dead void 690 usage(void) 691 { 692 (void)fprintf(stderr, 693 "Usage: %s [-AadefILlNnOopqStvXx] [-c count] [-D direction]\n", 694 program_name); 695 (void)fprintf(stderr, 696 "\t [-E [espalg:]espkey] [-F file] [-i interface] [-r file]\n"); 697 (void)fprintf(stderr, 698 "\t [-s snaplen] [-T type] [-w file] [-y datalinktype] [expression]\n"); 699 exit(1); 700 } 701