1 /* NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky 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 * Copyright (c) 1999-2004 The tcpdump.org project 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that: (1) source code distributions 11 * retain the above copyright notice and this paragraph in its entirety, (2) 12 * distributions including binary code include the above copyright notice and 13 * this paragraph in its entirety in the documentation or other materials 14 * provided with the distribution, and (3) all advertising materials mentioning 15 * features or use of this software display the following acknowledgement: 16 * ``This product includes software developed by the University of California, 17 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 18 * the University nor the names of its contributors may be used to endorse 19 * or promote products derived from this software without specific prior 20 * written permission. 21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 22 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 24 */ 25 26 #include <sys/cdefs.h> 27 #ifndef lint 28 #if 0 29 static const char rcsid[] _U_ = 30 "@(#) Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.135 2008-11-09 23:35:03 mcr Exp (LBL)"; 31 #else 32 __RCSID("$NetBSD: print-tcp.c,v 1.4 2013/12/31 17:33:31 christos Exp $"); 33 #endif 34 #endif 35 36 #ifdef HAVE_CONFIG_H 37 #include "config.h" 38 #endif 39 40 #include <tcpdump-stdinc.h> 41 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 46 #include "interface.h" 47 #include "addrtoname.h" 48 #include "extract.h" 49 50 #include "tcp.h" 51 52 #include "ip.h" 53 #ifdef INET6 54 #include "ip6.h" 55 #endif 56 #include "ipproto.h" 57 #include "rpc_auth.h" 58 #include "rpc_msg.h" 59 60 #include "nameser.h" 61 62 #ifdef HAVE_LIBCRYPTO 63 #include <openssl/md5.h> 64 #include <signature.h> 65 66 static int tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp, 67 const u_char *data, int length, const u_char *rcvsig); 68 #endif 69 70 static void print_tcp_rst_data(register const u_char *sp, u_int length); 71 72 #define MAX_RST_DATA_LEN 30 73 74 75 struct tha { 76 struct in_addr src; 77 struct in_addr dst; 78 u_int port; 79 }; 80 81 struct tcp_seq_hash { 82 struct tcp_seq_hash *nxt; 83 struct tha addr; 84 tcp_seq seq; 85 tcp_seq ack; 86 }; 87 88 #ifdef INET6 89 struct tha6 { 90 struct in6_addr src; 91 struct in6_addr dst; 92 u_int port; 93 }; 94 95 struct tcp_seq_hash6 { 96 struct tcp_seq_hash6 *nxt; 97 struct tha6 addr; 98 tcp_seq seq; 99 tcp_seq ack; 100 }; 101 #endif 102 103 #define TSEQ_HASHSIZE 919 104 105 /* These tcp optinos do not have the size octet */ 106 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP) 107 108 static struct tcp_seq_hash tcp_seq_hash4[TSEQ_HASHSIZE]; 109 #ifdef INET6 110 static struct tcp_seq_hash6 tcp_seq_hash6[TSEQ_HASHSIZE]; 111 #endif 112 113 static const struct tok tcp_flag_values[] = { 114 { TH_FIN, "F" }, 115 { TH_SYN, "S" }, 116 { TH_RST, "R" }, 117 { TH_PUSH, "P" }, 118 { TH_ACK, "." }, 119 { TH_URG, "U" }, 120 { TH_ECNECHO, "E" }, 121 { TH_CWR, "W" }, 122 { 0, NULL } 123 }; 124 125 static const struct tok tcp_option_values[] = { 126 { TCPOPT_EOL, "eol" }, 127 { TCPOPT_NOP, "nop" }, 128 { TCPOPT_MAXSEG, "mss" }, 129 { TCPOPT_WSCALE, "wscale" }, 130 { TCPOPT_SACKOK, "sackOK" }, 131 { TCPOPT_SACK, "sack" }, 132 { TCPOPT_ECHO, "echo" }, 133 { TCPOPT_ECHOREPLY, "echoreply" }, 134 { TCPOPT_TIMESTAMP, "TS" }, 135 { TCPOPT_CC, "cc" }, 136 { TCPOPT_CCNEW, "ccnew" }, 137 { TCPOPT_CCECHO, "" }, 138 { TCPOPT_SIGNATURE, "md5" }, 139 { TCPOPT_AUTH, "enhanced auth" }, 140 { TCPOPT_UTO, "uto" }, 141 { TCPOPT_MPTCP, "mptcp" }, 142 { TCPOPT_EXPERIMENT2, "exp" }, 143 { 0, NULL } 144 }; 145 146 static int tcp_cksum(register const struct ip *ip, 147 register const struct tcphdr *tp, 148 register u_int len) 149 { 150 return (nextproto4_cksum(ip, (const u_int8_t *)tp, len, 151 IPPROTO_TCP)); 152 } 153 154 void 155 tcp_print(register const u_char *bp, register u_int length, 156 register const u_char *bp2, int fragmented) 157 { 158 register const struct tcphdr *tp; 159 register const struct ip *ip; 160 register u_char flags; 161 register u_int hlen; 162 register char ch; 163 u_int16_t sport, dport, win, urp; 164 u_int32_t seq, ack, thseq, thack; 165 u_int utoval; 166 u_int16_t magic; 167 register int rev; 168 #ifdef INET6 169 register const struct ip6_hdr *ip6; 170 #endif 171 172 tp = (struct tcphdr *)bp; 173 ip = (struct ip *)bp2; 174 #ifdef INET6 175 if (IP_V(ip) == 6) 176 ip6 = (struct ip6_hdr *)bp2; 177 else 178 ip6 = NULL; 179 #endif /*INET6*/ 180 ch = '\0'; 181 if (!TTEST(tp->th_dport)) { 182 (void)printf("%s > %s: [|tcp]", 183 ipaddr_string(&ip->ip_src), 184 ipaddr_string(&ip->ip_dst)); 185 return; 186 } 187 188 sport = EXTRACT_16BITS(&tp->th_sport); 189 dport = EXTRACT_16BITS(&tp->th_dport); 190 191 hlen = TH_OFF(tp) * 4; 192 193 #ifdef INET6 194 if (ip6) { 195 if (ip6->ip6_nxt == IPPROTO_TCP) { 196 (void)printf("%s.%s > %s.%s: ", 197 ip6addr_string(&ip6->ip6_src), 198 tcpport_string(sport), 199 ip6addr_string(&ip6->ip6_dst), 200 tcpport_string(dport)); 201 } else { 202 (void)printf("%s > %s: ", 203 tcpport_string(sport), tcpport_string(dport)); 204 } 205 } else 206 #endif /*INET6*/ 207 { 208 if (ip->ip_p == IPPROTO_TCP) { 209 (void)printf("%s.%s > %s.%s: ", 210 ipaddr_string(&ip->ip_src), 211 tcpport_string(sport), 212 ipaddr_string(&ip->ip_dst), 213 tcpport_string(dport)); 214 } else { 215 (void)printf("%s > %s: ", 216 tcpport_string(sport), tcpport_string(dport)); 217 } 218 } 219 220 if (hlen < sizeof(*tp)) { 221 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]", 222 length - hlen, hlen, (unsigned long)sizeof(*tp)); 223 return; 224 } 225 226 TCHECK(*tp); 227 228 seq = EXTRACT_32BITS(&tp->th_seq); 229 ack = EXTRACT_32BITS(&tp->th_ack); 230 win = EXTRACT_16BITS(&tp->th_win); 231 urp = EXTRACT_16BITS(&tp->th_urp); 232 233 if (qflag) { 234 (void)printf("tcp %d", length - hlen); 235 if (hlen > length) { 236 (void)printf(" [bad hdr length %u - too long, > %u]", 237 hlen, length); 238 } 239 return; 240 } 241 242 flags = tp->th_flags; 243 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags)); 244 245 if (!Sflag && (flags & TH_ACK)) { 246 /* 247 * Find (or record) the initial sequence numbers for 248 * this conversation. (we pick an arbitrary 249 * collating order so there's only one entry for 250 * both directions). 251 */ 252 rev = 0; 253 #ifdef INET6 254 if (ip6) { 255 register struct tcp_seq_hash6 *th; 256 struct tcp_seq_hash6 *tcp_seq_hash; 257 const struct in6_addr *src, *dst; 258 struct tha6 tha; 259 260 tcp_seq_hash = tcp_seq_hash6; 261 src = &ip6->ip6_src; 262 dst = &ip6->ip6_dst; 263 if (sport > dport) 264 rev = 1; 265 else if (sport == dport) { 266 if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0) 267 rev = 1; 268 } 269 if (rev) { 270 memcpy(&tha.src, dst, sizeof ip6->ip6_dst); 271 memcpy(&tha.dst, src, sizeof ip6->ip6_src); 272 tha.port = dport << 16 | sport; 273 } else { 274 memcpy(&tha.dst, dst, sizeof ip6->ip6_dst); 275 memcpy(&tha.src, src, sizeof ip6->ip6_src); 276 tha.port = sport << 16 | dport; 277 } 278 279 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE]; 280 th->nxt; th = th->nxt) 281 if (memcmp((char *)&tha, (char *)&th->addr, 282 sizeof(th->addr)) == 0) 283 break; 284 285 if (!th->nxt || (flags & TH_SYN)) { 286 /* didn't find it or new conversation */ 287 if (th->nxt == NULL) { 288 th->nxt = (struct tcp_seq_hash6 *) 289 calloc(1, sizeof(*th)); 290 if (th->nxt == NULL) 291 error("tcp_print: calloc"); 292 } 293 th->addr = tha; 294 if (rev) 295 th->ack = seq, th->seq = ack - 1; 296 else 297 th->seq = seq, th->ack = ack - 1; 298 } else { 299 if (rev) 300 seq -= th->ack, ack -= th->seq; 301 else 302 seq -= th->seq, ack -= th->ack; 303 } 304 305 thseq = th->seq; 306 thack = th->ack; 307 } else { 308 #else /*INET6*/ 309 { 310 #endif /*INET6*/ 311 register struct tcp_seq_hash *th; 312 struct tcp_seq_hash *tcp_seq_hash; 313 const struct in_addr *src, *dst; 314 struct tha tha; 315 316 tcp_seq_hash = tcp_seq_hash4; 317 src = &ip->ip_src; 318 dst = &ip->ip_dst; 319 if (sport > dport) 320 rev = 1; 321 else if (sport == dport) { 322 if (memcmp(src, dst, sizeof ip->ip_dst) > 0) 323 rev = 1; 324 } 325 if (rev) { 326 memcpy(&tha.src, dst, sizeof ip->ip_dst); 327 memcpy(&tha.dst, src, sizeof ip->ip_src); 328 tha.port = dport << 16 | sport; 329 } else { 330 memcpy(&tha.dst, dst, sizeof ip->ip_dst); 331 memcpy(&tha.src, src, sizeof ip->ip_src); 332 tha.port = sport << 16 | dport; 333 } 334 335 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE]; 336 th->nxt; th = th->nxt) 337 if (memcmp((char *)&tha, (char *)&th->addr, 338 sizeof(th->addr)) == 0) 339 break; 340 341 if (!th->nxt || (flags & TH_SYN)) { 342 /* didn't find it or new conversation */ 343 if (th->nxt == NULL) { 344 th->nxt = (struct tcp_seq_hash *) 345 calloc(1, sizeof(*th)); 346 if (th->nxt == NULL) 347 error("tcp_print: calloc"); 348 } 349 th->addr = tha; 350 if (rev) 351 th->ack = seq, th->seq = ack - 1; 352 else 353 th->seq = seq, th->ack = ack - 1; 354 } else { 355 if (rev) 356 seq -= th->ack, ack -= th->seq; 357 else 358 seq -= th->seq, ack -= th->ack; 359 } 360 361 thseq = th->seq; 362 thack = th->ack; 363 } 364 } else { 365 /*fool gcc*/ 366 thseq = thack = rev = 0; 367 } 368 if (hlen > length) { 369 (void)printf(" [bad hdr length %u - too long, > %u]", 370 hlen, length); 371 return; 372 } 373 374 if (vflag && !Kflag && !fragmented) { 375 /* Check the checksum, if possible. */ 376 u_int16_t sum, tcp_sum; 377 378 if (IP_V(ip) == 4) { 379 if (TTEST2(tp->th_sport, length)) { 380 sum = tcp_cksum(ip, tp, length); 381 tcp_sum = EXTRACT_16BITS(&tp->th_sum); 382 383 (void)printf(", cksum 0x%04x", tcp_sum); 384 if (sum != 0) 385 (void)printf(" (incorrect -> 0x%04x)", 386 in_cksum_shouldbe(tcp_sum, sum)); 387 else 388 (void)printf(" (correct)"); 389 } 390 } 391 #ifdef INET6 392 else if (IP_V(ip) == 6 && ip6->ip6_plen) { 393 if (TTEST2(tp->th_sport, length)) { 394 sum = nextproto6_cksum(ip6, (const u_int8_t *)tp, length, IPPROTO_TCP); 395 tcp_sum = EXTRACT_16BITS(&tp->th_sum); 396 397 (void)printf(", cksum 0x%04x", tcp_sum); 398 if (sum != 0) 399 (void)printf(" (incorrect -> 0x%04x)", 400 in_cksum_shouldbe(tcp_sum, sum)); 401 else 402 (void)printf(" (correct)"); 403 404 } 405 } 406 #endif 407 } 408 409 length -= hlen; 410 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST)) { 411 (void)printf(", seq %u", seq); 412 413 if (length > 0) { 414 (void)printf(":%u", seq + length); 415 } 416 } 417 418 if (flags & TH_ACK) { 419 (void)printf(", ack %u", ack); 420 } 421 422 (void)printf(", win %d", win); 423 424 if (flags & TH_URG) 425 (void)printf(", urg %d", urp); 426 /* 427 * Handle any options. 428 */ 429 if (hlen > sizeof(*tp)) { 430 register const u_char *cp; 431 register u_int i, opt, datalen; 432 register u_int len; 433 434 hlen -= sizeof(*tp); 435 cp = (const u_char *)tp + sizeof(*tp); 436 printf(", options ["); 437 while (hlen > 0) { 438 if (ch != '\0') 439 putchar(ch); 440 TCHECK(*cp); 441 opt = *cp++; 442 if (ZEROLENOPT(opt)) 443 len = 1; 444 else { 445 TCHECK(*cp); 446 len = *cp++; /* total including type, len */ 447 if (len < 2 || len > hlen) 448 goto bad; 449 --hlen; /* account for length byte */ 450 } 451 --hlen; /* account for type byte */ 452 datalen = 0; 453 454 /* Bail if "l" bytes of data are not left or were not captured */ 455 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); } 456 457 458 printf("%s", tok2str(tcp_option_values, "unknown-%u", opt)); 459 460 switch (opt) { 461 462 case TCPOPT_MAXSEG: 463 datalen = 2; 464 LENCHECK(datalen); 465 (void)printf(" %u", EXTRACT_16BITS(cp)); 466 break; 467 468 case TCPOPT_WSCALE: 469 datalen = 1; 470 LENCHECK(datalen); 471 (void)printf(" %u", *cp); 472 break; 473 474 case TCPOPT_SACK: 475 datalen = len - 2; 476 if (datalen % 8 != 0) { 477 (void)printf("malformed sack"); 478 } else { 479 u_int32_t s, e; 480 481 (void)printf(" %d ", datalen / 8); 482 for (i = 0; i < datalen; i += 8) { 483 LENCHECK(i + 4); 484 s = EXTRACT_32BITS(cp + i); 485 LENCHECK(i + 8); 486 e = EXTRACT_32BITS(cp + i + 4); 487 if (rev) { 488 s -= thseq; 489 e -= thseq; 490 } else { 491 s -= thack; 492 e -= thack; 493 } 494 (void)printf("{%u:%u}", s, e); 495 } 496 } 497 break; 498 499 case TCPOPT_CC: 500 case TCPOPT_CCNEW: 501 case TCPOPT_CCECHO: 502 case TCPOPT_ECHO: 503 case TCPOPT_ECHOREPLY: 504 505 /* 506 * those options share their semantics. 507 * fall through 508 */ 509 datalen = 4; 510 LENCHECK(datalen); 511 (void)printf(" %u", EXTRACT_32BITS(cp)); 512 break; 513 514 case TCPOPT_TIMESTAMP: 515 datalen = 8; 516 LENCHECK(datalen); 517 (void)printf(" val %u ecr %u", 518 EXTRACT_32BITS(cp), 519 EXTRACT_32BITS(cp + 4)); 520 break; 521 522 case TCPOPT_SIGNATURE: 523 datalen = TCP_SIGLEN; 524 LENCHECK(datalen); 525 #ifdef HAVE_LIBCRYPTO 526 switch (tcp_verify_signature(ip, tp, 527 bp + TH_OFF(tp) * 4, length, cp)) { 528 529 case SIGNATURE_VALID: 530 (void)printf("valid"); 531 break; 532 533 case SIGNATURE_INVALID: 534 (void)printf("invalid"); 535 break; 536 537 case CANT_CHECK_SIGNATURE: 538 (void)printf("can't check - "); 539 for (i = 0; i < TCP_SIGLEN; ++i) 540 (void)printf("%02x", cp[i]); 541 break; 542 } 543 #else 544 for (i = 0; i < TCP_SIGLEN; ++i) 545 (void)printf("%02x", cp[i]); 546 #endif 547 break; 548 549 case TCPOPT_AUTH: 550 (void)printf("keyid %d", *cp++); 551 datalen = len - 3; 552 for (i = 0; i < datalen; ++i) { 553 LENCHECK(i); 554 (void)printf("%02x", cp[i]); 555 } 556 break; 557 558 559 case TCPOPT_EOL: 560 case TCPOPT_NOP: 561 case TCPOPT_SACKOK: 562 /* 563 * Nothing interesting. 564 * fall through 565 */ 566 break; 567 568 case TCPOPT_UTO: 569 datalen = 2; 570 LENCHECK(datalen); 571 utoval = EXTRACT_16BITS(cp); 572 (void)printf("0x%x", utoval); 573 if (utoval & 0x0001) 574 utoval = (utoval >> 1) * 60; 575 else 576 utoval >>= 1; 577 (void)printf(" %u", utoval); 578 break; 579 580 case TCPOPT_MPTCP: 581 datalen = len - 2; 582 LENCHECK(datalen); 583 if (!mptcp_print(cp-2, len, flags)) 584 goto bad; 585 break; 586 587 case TCPOPT_EXPERIMENT2: 588 datalen = len - 2; 589 LENCHECK(datalen); 590 if (datalen < 2) 591 goto bad; 592 /* RFC6994 */ 593 magic = EXTRACT_16BITS(cp); 594 (void)printf("-"); 595 596 switch(magic) { 597 598 case 0xf989: 599 /* TCP Fast Open: draft-ietf-tcpm-fastopen-04 */ 600 if (datalen == 2) { 601 /* Fast Open Cookie Request */ 602 (void)printf("tfo cookiereq"); 603 } else { 604 /* Fast Open Cookie */ 605 if (datalen % 2 != 0 || datalen < 6 || datalen > 18) { 606 (void)printf("tfo malformed"); 607 } else { 608 (void)printf("tfo cookie "); 609 for (i = 2; i < datalen; ++i) 610 (void)printf("%02x", cp[i]); 611 } 612 } 613 break; 614 615 default: 616 /* Unknown magic number */ 617 (void)printf("%04x", magic); 618 break; 619 } 620 break; 621 622 default: 623 datalen = len - 2; 624 if (datalen) 625 printf(" 0x"); 626 for (i = 0; i < datalen; ++i) { 627 LENCHECK(i); 628 (void)printf("%02x", cp[i]); 629 } 630 break; 631 } 632 633 /* Account for data printed */ 634 cp += datalen; 635 hlen -= datalen; 636 637 /* Check specification against observed length */ 638 ++datalen; /* option octet */ 639 if (!ZEROLENOPT(opt)) 640 ++datalen; /* size octet */ 641 if (datalen != len) 642 (void)printf("[len %d]", len); 643 ch = ','; 644 if (opt == TCPOPT_EOL) 645 break; 646 } 647 putchar(']'); 648 } 649 650 /* 651 * Print length field before crawling down the stack. 652 */ 653 printf(", length %u", length); 654 655 if (length <= 0) 656 return; 657 658 /* 659 * Decode payload if necessary. 660 */ 661 bp += TH_OFF(tp) * 4; 662 if ((flags & TH_RST) && vflag) { 663 print_tcp_rst_data(bp, length); 664 return; 665 } 666 667 if (packettype) { 668 switch (packettype) { 669 case PT_ZMTP1: 670 zmtp1_print(bp, length); 671 break; 672 } 673 return; 674 } 675 676 if (sport == TELNET_PORT || dport == TELNET_PORT) { 677 if (!qflag && vflag) 678 telnet_print(bp, length); 679 } else if (sport == BGP_PORT || dport == BGP_PORT) 680 bgp_print(bp, length); 681 else if (sport == PPTP_PORT || dport == PPTP_PORT) 682 pptp_print(bp); 683 #ifdef TCPDUMP_DO_SMB 684 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT) 685 nbt_tcp_print(bp, length); 686 else if (sport == SMB_PORT || dport == SMB_PORT) 687 smb_tcp_print(bp, length); 688 #endif 689 else if (sport == BEEP_PORT || dport == BEEP_PORT) 690 beep_print(bp, length); 691 else if (sport == OPENFLOW_PORT || dport == OPENFLOW_PORT) 692 openflow_print(bp, length); 693 else if (length > 2 && 694 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT || 695 sport == MULTICASTDNS_PORT || dport == MULTICASTDNS_PORT)) { 696 /* 697 * TCP DNS query has 2byte length at the head. 698 * XXX packet could be unaligned, it can go strange 699 */ 700 ns_print(bp + 2, length - 2, 0); 701 } else if (sport == MSDP_PORT || dport == MSDP_PORT) { 702 msdp_print(bp, length); 703 } else if (sport == RPKI_RTR_PORT || dport == RPKI_RTR_PORT) { 704 rpki_rtr_print(bp, length); 705 } 706 else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) { 707 ldp_print(bp, length); 708 } 709 else if ((sport == NFS_PORT || dport == NFS_PORT) && 710 length >= 4 && TTEST2(*bp, 4)) { 711 /* 712 * If data present, header length valid, and NFS port used, 713 * assume NFS. 714 * Pass offset of data plus 4 bytes for RPC TCP msg length 715 * to NFS print routines. 716 */ 717 u_int32_t fraglen; 718 register struct sunrpc_msg *rp; 719 enum sunrpc_msg_type direction; 720 721 fraglen = EXTRACT_32BITS(bp) & 0x7FFFFFFF; 722 if (fraglen > (length) - 4) 723 fraglen = (length) - 4; 724 rp = (struct sunrpc_msg *)(bp + 4); 725 if (TTEST(rp->rm_direction)) { 726 direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction); 727 if (dport == NFS_PORT && direction == SUNRPC_CALL) { 728 (void)printf(": NFS request xid %u ", EXTRACT_32BITS(&rp->rm_xid)); 729 nfsreq_print_noaddr((u_char *)rp, fraglen, (u_char *)ip); 730 return; 731 } 732 if (sport == NFS_PORT && direction == SUNRPC_REPLY) { 733 (void)printf(": NFS reply xid %u ", EXTRACT_32BITS(&rp->rm_xid)); 734 nfsreply_print_noaddr((u_char *)rp, fraglen, (u_char *)ip); 735 return; 736 } 737 } 738 } 739 740 return; 741 bad: 742 fputs("[bad opt]", stdout); 743 if (ch != '\0') 744 putchar('>'); 745 return; 746 trunc: 747 fputs("[|tcp]", stdout); 748 if (ch != '\0') 749 putchar('>'); 750 } 751 752 /* 753 * RFC1122 says the following on data in RST segments: 754 * 755 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 756 * 757 * A TCP SHOULD allow a received RST segment to include data. 758 * 759 * DISCUSSION 760 * It has been suggested that a RST segment could contain 761 * ASCII text that encoded and explained the cause of the 762 * RST. No standard has yet been established for such 763 * data. 764 * 765 */ 766 767 static void 768 print_tcp_rst_data(register const u_char *sp, u_int length) 769 { 770 int c; 771 772 if (TTEST2(*sp, length)) 773 printf(" [RST"); 774 else 775 printf(" [!RST"); 776 if (length > MAX_RST_DATA_LEN) { 777 length = MAX_RST_DATA_LEN; /* can use -X for longer */ 778 putchar('+'); /* indicate we truncate */ 779 } 780 putchar(' '); 781 while (length-- && sp <= snapend) { 782 c = *sp++; 783 safeputchar(c); 784 } 785 putchar(']'); 786 } 787 788 #ifdef HAVE_LIBCRYPTO 789 USES_APPLE_DEPRECATED_API 790 static int 791 tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp, 792 const u_char *data, int length, const u_char *rcvsig) 793 { 794 struct tcphdr tp1; 795 u_char sig[TCP_SIGLEN]; 796 char zero_proto = 0; 797 MD5_CTX ctx; 798 u_int16_t savecsum, tlen; 799 #ifdef INET6 800 struct ip6_hdr *ip6; 801 u_int32_t len32; 802 u_int8_t nxt; 803 #endif 804 805 if (data + length > snapend) { 806 printf("snaplen too short, "); 807 return (CANT_CHECK_SIGNATURE); 808 } 809 810 tp1 = *tp; 811 812 if (sigsecret == NULL) { 813 printf("shared secret not supplied with -M, "); 814 return (CANT_CHECK_SIGNATURE); 815 } 816 817 MD5_Init(&ctx); 818 /* 819 * Step 1: Update MD5 hash with IP pseudo-header. 820 */ 821 if (IP_V(ip) == 4) { 822 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src)); 823 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst)); 824 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto)); 825 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p)); 826 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4; 827 tlen = htons(tlen); 828 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen)); 829 #ifdef INET6 830 } else if (IP_V(ip) == 6) { 831 ip6 = (struct ip6_hdr *)ip; 832 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src)); 833 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst)); 834 len32 = htonl(EXTRACT_16BITS(&ip6->ip6_plen)); 835 MD5_Update(&ctx, (char *)&len32, sizeof(len32)); 836 nxt = 0; 837 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt)); 838 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt)); 839 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt)); 840 nxt = IPPROTO_TCP; 841 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt)); 842 #endif 843 } else { 844 #ifdef INET6 845 printf("IP version not 4 or 6, "); 846 #else 847 printf("IP version not 4, "); 848 #endif 849 return (CANT_CHECK_SIGNATURE); 850 } 851 852 /* 853 * Step 2: Update MD5 hash with TCP header, excluding options. 854 * The TCP checksum must be set to zero. 855 */ 856 savecsum = tp1.th_sum; 857 tp1.th_sum = 0; 858 MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr)); 859 tp1.th_sum = savecsum; 860 /* 861 * Step 3: Update MD5 hash with TCP segment data, if present. 862 */ 863 if (length > 0) 864 MD5_Update(&ctx, data, length); 865 /* 866 * Step 4: Update MD5 hash with shared secret. 867 */ 868 MD5_Update(&ctx, sigsecret, strlen(sigsecret)); 869 MD5_Final(sig, &ctx); 870 871 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0) 872 return (SIGNATURE_VALID); 873 else 874 return (SIGNATURE_INVALID); 875 } 876 USES_APPLE_RST 877 #endif /* HAVE_LIBCRYPTO */ 878 879 /* 880 * Local Variables: 881 * c-style: whitesmith 882 * c-basic-offset: 8 883 * End: 884 */ 885