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 /* \summary: TCP printer */ 27 28 #include <sys/cdefs.h> 29 #ifndef lint 30 __RCSID("$NetBSD: print-tcp.c,v 1.10 2020/02/24 18:39:47 kamil Exp $"); 31 #endif 32 33 #ifdef HAVE_CONFIG_H 34 #include "config.h" 35 #endif 36 37 #include <netdissect-stdinc.h> 38 39 #include <stdlib.h> 40 #include <string.h> 41 42 #include "netdissect.h" 43 #include "addrtoname.h" 44 #include "extract.h" 45 46 #include "tcp.h" 47 48 #include "ip.h" 49 #include "ip6.h" 50 #include "ipproto.h" 51 #include "rpc_auth.h" 52 #include "rpc_msg.h" 53 54 #ifdef HAVE_LIBCRYPTO 55 #include <openssl/md5.h> 56 #include "signature.h" 57 58 static int tcp_verify_signature(netdissect_options *ndo, 59 const struct ip *ip, const struct tcphdr *tp, 60 const u_char *data, int length, const u_char *rcvsig); 61 #endif 62 63 static void print_tcp_rst_data(netdissect_options *, register const u_char *sp, u_int length); 64 static void print_tcp_fastopen_option(netdissect_options *ndo, register const u_char *cp, 65 u_int datalen, int exp); 66 67 #define MAX_RST_DATA_LEN 30 68 69 70 struct tha { 71 struct in_addr src; 72 struct in_addr dst; 73 u_int port; 74 }; 75 76 struct tcp_seq_hash { 77 struct tcp_seq_hash *nxt; 78 struct tha addr; 79 tcp_seq seq; 80 tcp_seq ack; 81 }; 82 83 struct tha6 { 84 struct in6_addr src; 85 struct in6_addr dst; 86 u_int port; 87 }; 88 89 struct tcp_seq_hash6 { 90 struct tcp_seq_hash6 *nxt; 91 struct tha6 addr; 92 tcp_seq seq; 93 tcp_seq ack; 94 }; 95 96 #define TSEQ_HASHSIZE 919 97 98 /* These tcp optinos do not have the size octet */ 99 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP) 100 101 static struct tcp_seq_hash tcp_seq_hash4[TSEQ_HASHSIZE]; 102 static struct tcp_seq_hash6 tcp_seq_hash6[TSEQ_HASHSIZE]; 103 104 static const struct tok tcp_flag_values[] = { 105 { TH_FIN, "F" }, 106 { TH_SYN, "S" }, 107 { TH_RST, "R" }, 108 { TH_PUSH, "P" }, 109 { TH_ACK, "." }, 110 { TH_URG, "U" }, 111 { TH_ECNECHO, "E" }, 112 { TH_CWR, "W" }, 113 { 0, NULL } 114 }; 115 116 static const struct tok tcp_option_values[] = { 117 { TCPOPT_EOL, "eol" }, 118 { TCPOPT_NOP, "nop" }, 119 { TCPOPT_MAXSEG, "mss" }, 120 { TCPOPT_WSCALE, "wscale" }, 121 { TCPOPT_SACKOK, "sackOK" }, 122 { TCPOPT_SACK, "sack" }, 123 { TCPOPT_ECHO, "echo" }, 124 { TCPOPT_ECHOREPLY, "echoreply" }, 125 { TCPOPT_TIMESTAMP, "TS" }, 126 { TCPOPT_CC, "cc" }, 127 { TCPOPT_CCNEW, "ccnew" }, 128 { TCPOPT_CCECHO, "" }, 129 { TCPOPT_SIGNATURE, "md5" }, 130 { TCPOPT_SCPS, "scps" }, 131 { TCPOPT_UTO, "uto" }, 132 { TCPOPT_TCPAO, "tcp-ao" }, 133 { TCPOPT_MPTCP, "mptcp" }, 134 { TCPOPT_FASTOPEN, "tfo" }, 135 { TCPOPT_EXPERIMENT2, "exp" }, 136 { 0, NULL } 137 }; 138 139 static int 140 tcp_cksum(netdissect_options *ndo, 141 register const struct ip *ip, 142 register const struct tcphdr *tp, 143 register u_int len) 144 { 145 return nextproto4_cksum(ndo, ip, (const uint8_t *)tp, len, len, 146 IPPROTO_TCP); 147 } 148 149 static int 150 tcp6_cksum(netdissect_options *ndo, 151 register const struct ip6_hdr *ip6, 152 register const struct tcphdr *tp, 153 register u_int len) 154 { 155 return nextproto6_cksum(ndo, ip6, (const uint8_t *)tp, len, len, 156 IPPROTO_TCP); 157 } 158 159 UNALIGNED_OK 160 void 161 tcp_print(netdissect_options *ndo, 162 register const u_char *bp, register u_int length, 163 register const u_char *bp2, int fragmented) 164 { 165 register const struct tcphdr *tp; 166 register const struct ip *ip; 167 register u_char flags; 168 register u_int hlen; 169 register char ch; 170 uint16_t sport, dport, win, urp; 171 uint32_t seq, ack, thseq, thack; 172 u_int utoval; 173 uint16_t magic; 174 register int rev; 175 register const struct ip6_hdr *ip6; 176 177 tp = (const struct tcphdr *)bp; 178 ip = (const struct ip *)bp2; 179 if (IP_V(ip) == 6) 180 ip6 = (const struct ip6_hdr *)bp2; 181 else 182 ip6 = NULL; 183 ch = '\0'; 184 if (!ND_TTEST(tp->th_dport)) { 185 ND_PRINT((ndo, "%s > %s: [|tcp]", 186 ipaddr_string(ndo, &ip->ip_src), 187 ipaddr_string(ndo, &ip->ip_dst))); 188 return; 189 } 190 191 sport = EXTRACT_16BITS(&tp->th_sport); 192 dport = EXTRACT_16BITS(&tp->th_dport); 193 194 if (ip6) { 195 if (ip6->ip6_nxt == IPPROTO_TCP) { 196 ND_PRINT((ndo, "%s.%s > %s.%s: ", 197 ip6addr_string(ndo, &ip6->ip6_src), 198 tcpport_string(ndo, sport), 199 ip6addr_string(ndo, &ip6->ip6_dst), 200 tcpport_string(ndo, dport))); 201 } else { 202 ND_PRINT((ndo, "%s > %s: ", 203 tcpport_string(ndo, sport), tcpport_string(ndo, dport))); 204 } 205 } else { 206 if (ip->ip_p == IPPROTO_TCP) { 207 ND_PRINT((ndo, "%s.%s > %s.%s: ", 208 ipaddr_string(ndo, &ip->ip_src), 209 tcpport_string(ndo, sport), 210 ipaddr_string(ndo, &ip->ip_dst), 211 tcpport_string(ndo, dport))); 212 } else { 213 ND_PRINT((ndo, "%s > %s: ", 214 tcpport_string(ndo, sport), tcpport_string(ndo, dport))); 215 } 216 } 217 218 ND_TCHECK(*tp); 219 220 hlen = TH_OFF(tp) * 4; 221 222 if (hlen < sizeof(*tp)) { 223 ND_PRINT((ndo, " tcp %d [bad hdr length %u - too short, < %lu]", 224 length - hlen, hlen, (unsigned long)sizeof(*tp))); 225 return; 226 } 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 (ndo->ndo_qflag) { 234 ND_PRINT((ndo, "tcp %d", length - hlen)); 235 if (hlen > length) { 236 ND_PRINT((ndo, " [bad hdr length %u - too long, > %u]", 237 hlen, length)); 238 } 239 return; 240 } 241 242 flags = tp->th_flags; 243 ND_PRINT((ndo, "Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags))); 244 245 if (!ndo->ndo_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 if (ip6) { 254 register struct tcp_seq_hash6 *th; 255 struct tcp_seq_hash6 *tcp_seq_hash; 256 const struct in6_addr *src, *dst; 257 struct tha6 tha; 258 259 tcp_seq_hash = tcp_seq_hash6; 260 src = &ip6->ip6_src; 261 dst = &ip6->ip6_dst; 262 if (sport > dport) 263 rev = 1; 264 else if (sport == dport) { 265 if (UNALIGNED_MEMCMP(src, dst, sizeof ip6->ip6_dst) > 0) 266 rev = 1; 267 } 268 if (rev) { 269 UNALIGNED_MEMCPY(&tha.src, dst, sizeof ip6->ip6_dst); 270 UNALIGNED_MEMCPY(&tha.dst, src, sizeof ip6->ip6_src); 271 tha.port = ((u_int)dport) << 16 | sport; 272 } else { 273 UNALIGNED_MEMCPY(&tha.dst, dst, sizeof ip6->ip6_dst); 274 UNALIGNED_MEMCPY(&tha.src, src, sizeof ip6->ip6_src); 275 tha.port = ((u_int)sport) << 16 | dport; 276 } 277 278 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE]; 279 th->nxt; th = th->nxt) 280 if (memcmp((char *)&tha, (char *)&th->addr, 281 sizeof(th->addr)) == 0) 282 break; 283 284 if (!th->nxt || (flags & TH_SYN)) { 285 /* didn't find it or new conversation */ 286 if (th->nxt == NULL) { 287 th->nxt = (struct tcp_seq_hash6 *) 288 calloc(1, sizeof(*th)); 289 if (th->nxt == NULL) 290 (*ndo->ndo_error)(ndo, 291 "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 register struct tcp_seq_hash *th; 309 struct tcp_seq_hash *tcp_seq_hash; 310 struct tha tha; 311 312 tcp_seq_hash = tcp_seq_hash4; 313 if (sport > dport) 314 rev = 1; 315 else if (sport == dport) { 316 if (UNALIGNED_MEMCMP(&ip->ip_src, &ip->ip_dst, sizeof ip->ip_dst) > 0) 317 rev = 1; 318 } 319 if (rev) { 320 UNALIGNED_MEMCPY(&tha.src, &ip->ip_dst, sizeof ip->ip_dst); 321 UNALIGNED_MEMCPY(&tha.dst, &ip->ip_src, sizeof ip->ip_src); 322 tha.port = ((u_int)dport) << 16 | sport; 323 } else { 324 UNALIGNED_MEMCPY(&tha.dst, &ip->ip_dst, sizeof ip->ip_dst); 325 UNALIGNED_MEMCPY(&tha.src, &ip->ip_src, sizeof ip->ip_src); 326 tha.port = ((u_int)sport) << 16 | dport; 327 } 328 329 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE]; 330 th->nxt; th = th->nxt) 331 if (memcmp((char *)&tha, (char *)&th->addr, 332 sizeof(th->addr)) == 0) 333 break; 334 335 if (!th->nxt || (flags & TH_SYN)) { 336 /* didn't find it or new conversation */ 337 if (th->nxt == NULL) { 338 th->nxt = (struct tcp_seq_hash *) 339 calloc(1, sizeof(*th)); 340 if (th->nxt == NULL) 341 (*ndo->ndo_error)(ndo, 342 "tcp_print: calloc"); 343 } 344 th->addr = tha; 345 if (rev) 346 th->ack = seq, th->seq = ack - 1; 347 else 348 th->seq = seq, th->ack = ack - 1; 349 } else { 350 if (rev) 351 seq -= th->ack, ack -= th->seq; 352 else 353 seq -= th->seq, ack -= th->ack; 354 } 355 356 thseq = th->seq; 357 thack = th->ack; 358 } 359 } else { 360 /*fool gcc*/ 361 thseq = thack = rev = 0; 362 } 363 if (hlen > length) { 364 ND_PRINT((ndo, " [bad hdr length %u - too long, > %u]", 365 hlen, length)); 366 return; 367 } 368 369 if (ndo->ndo_vflag && !ndo->ndo_Kflag && !fragmented) { 370 /* Check the checksum, if possible. */ 371 uint16_t sum, tcp_sum; 372 373 if (IP_V(ip) == 4) { 374 if (ND_TTEST2(tp->th_sport, length)) { 375 sum = tcp_cksum(ndo, ip, tp, length); 376 tcp_sum = EXTRACT_16BITS(&tp->th_sum); 377 378 ND_PRINT((ndo, ", cksum 0x%04x", tcp_sum)); 379 if (sum != 0) 380 ND_PRINT((ndo, " (incorrect -> 0x%04x)", 381 in_cksum_shouldbe(tcp_sum, sum))); 382 else 383 ND_PRINT((ndo, " (correct)")); 384 } 385 } else if (IP_V(ip) == 6 && ip6->ip6_plen) { 386 if (ND_TTEST2(tp->th_sport, length)) { 387 sum = tcp6_cksum(ndo, ip6, tp, length); 388 tcp_sum = EXTRACT_16BITS(&tp->th_sum); 389 390 ND_PRINT((ndo, ", cksum 0x%04x", tcp_sum)); 391 if (sum != 0) 392 ND_PRINT((ndo, " (incorrect -> 0x%04x)", 393 in_cksum_shouldbe(tcp_sum, sum))); 394 else 395 ND_PRINT((ndo, " (correct)")); 396 397 } 398 } 399 } 400 401 length -= hlen; 402 if (ndo->ndo_vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST)) { 403 ND_PRINT((ndo, ", seq %u", seq)); 404 405 if (length > 0) { 406 ND_PRINT((ndo, ":%u", seq + length)); 407 } 408 } 409 410 if (flags & TH_ACK) { 411 ND_PRINT((ndo, ", ack %u", ack)); 412 } 413 414 ND_PRINT((ndo, ", win %d", win)); 415 416 if (flags & TH_URG) 417 ND_PRINT((ndo, ", urg %d", urp)); 418 /* 419 * Handle any options. 420 */ 421 if (hlen > sizeof(*tp)) { 422 register const u_char *cp; 423 register u_int i, opt, datalen; 424 register u_int len; 425 426 hlen -= sizeof(*tp); 427 cp = (const u_char *)tp + sizeof(*tp); 428 ND_PRINT((ndo, ", options [")); 429 while (hlen > 0) { 430 if (ch != '\0') 431 ND_PRINT((ndo, "%c", ch)); 432 ND_TCHECK(*cp); 433 opt = *cp++; 434 if (ZEROLENOPT(opt)) 435 len = 1; 436 else { 437 ND_TCHECK(*cp); 438 len = *cp++; /* total including type, len */ 439 if (len < 2 || len > hlen) 440 goto bad; 441 --hlen; /* account for length byte */ 442 } 443 --hlen; /* account for type byte */ 444 datalen = 0; 445 446 /* Bail if "l" bytes of data are not left or were not captured */ 447 #define LENCHECK(l) { if ((l) > hlen) goto bad; ND_TCHECK2(*cp, l); } 448 449 450 ND_PRINT((ndo, "%s", tok2str(tcp_option_values, "unknown-%u", opt))); 451 452 switch (opt) { 453 454 case TCPOPT_MAXSEG: 455 datalen = 2; 456 LENCHECK(datalen); 457 ND_PRINT((ndo, " %u", EXTRACT_16BITS(cp))); 458 break; 459 460 case TCPOPT_WSCALE: 461 datalen = 1; 462 LENCHECK(datalen); 463 ND_PRINT((ndo, " %u", *cp)); 464 break; 465 466 case TCPOPT_SACK: 467 datalen = len - 2; 468 if (datalen % 8 != 0) { 469 ND_PRINT((ndo, " invalid sack")); 470 } else { 471 uint32_t s, e; 472 473 ND_PRINT((ndo, " %d ", datalen / 8)); 474 for (i = 0; i < datalen; i += 8) { 475 LENCHECK(i + 4); 476 s = EXTRACT_32BITS(cp + i); 477 LENCHECK(i + 8); 478 e = EXTRACT_32BITS(cp + i + 4); 479 if (rev) { 480 s -= thseq; 481 e -= thseq; 482 } else { 483 s -= thack; 484 e -= thack; 485 } 486 ND_PRINT((ndo, "{%u:%u}", s, e)); 487 } 488 } 489 break; 490 491 case TCPOPT_CC: 492 case TCPOPT_CCNEW: 493 case TCPOPT_CCECHO: 494 case TCPOPT_ECHO: 495 case TCPOPT_ECHOREPLY: 496 497 /* 498 * those options share their semantics. 499 * fall through 500 */ 501 datalen = 4; 502 LENCHECK(datalen); 503 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp))); 504 break; 505 506 case TCPOPT_TIMESTAMP: 507 datalen = 8; 508 LENCHECK(datalen); 509 ND_PRINT((ndo, " val %u ecr %u", 510 EXTRACT_32BITS(cp), 511 EXTRACT_32BITS(cp + 4))); 512 break; 513 514 case TCPOPT_SIGNATURE: 515 datalen = TCP_SIGLEN; 516 LENCHECK(datalen); 517 ND_PRINT((ndo, " ")); 518 #ifdef HAVE_LIBCRYPTO 519 switch (tcp_verify_signature(ndo, ip, tp, 520 bp + TH_OFF(tp) * 4, length, cp)) { 521 522 case SIGNATURE_VALID: 523 ND_PRINT((ndo, "valid")); 524 break; 525 526 case SIGNATURE_INVALID: 527 ND_PRINT((ndo, "invalid")); 528 break; 529 530 case CANT_CHECK_SIGNATURE: 531 ND_PRINT((ndo, "can't check - ")); 532 for (i = 0; i < TCP_SIGLEN; ++i) 533 ND_PRINT((ndo, "%02x", cp[i])); 534 break; 535 } 536 #else 537 for (i = 0; i < TCP_SIGLEN; ++i) 538 ND_PRINT((ndo, "%02x", cp[i])); 539 #endif 540 break; 541 542 case TCPOPT_SCPS: 543 datalen = 2; 544 LENCHECK(datalen); 545 ND_PRINT((ndo, " cap %02x id %u", cp[0], cp[1])); 546 break; 547 548 case TCPOPT_TCPAO: 549 datalen = len - 2; 550 /* RFC 5925 Section 2.2: 551 * "The Length value MUST be greater than or equal to 4." 552 * (This includes the Kind and Length fields already processed 553 * at this point.) 554 */ 555 if (datalen < 2) { 556 ND_PRINT((ndo, " invalid")); 557 } else { 558 LENCHECK(1); 559 ND_PRINT((ndo, " keyid %u", cp[0])); 560 LENCHECK(2); 561 ND_PRINT((ndo, " rnextkeyid %u", cp[1])); 562 if (datalen > 2) { 563 ND_PRINT((ndo, " mac 0x")); 564 for (i = 2; i < datalen; i++) { 565 LENCHECK(i + 1); 566 ND_PRINT((ndo, "%02x", cp[i])); 567 } 568 } 569 } 570 break; 571 572 case TCPOPT_EOL: 573 case TCPOPT_NOP: 574 case TCPOPT_SACKOK: 575 /* 576 * Nothing interesting. 577 * fall through 578 */ 579 break; 580 581 case TCPOPT_UTO: 582 datalen = 2; 583 LENCHECK(datalen); 584 utoval = EXTRACT_16BITS(cp); 585 ND_PRINT((ndo, " 0x%x", utoval)); 586 if (utoval & 0x0001) 587 utoval = (utoval >> 1) * 60; 588 else 589 utoval >>= 1; 590 ND_PRINT((ndo, " %u", utoval)); 591 break; 592 593 case TCPOPT_MPTCP: 594 datalen = len - 2; 595 LENCHECK(datalen); 596 if (!mptcp_print(ndo, cp-2, len, flags)) 597 goto bad; 598 break; 599 600 case TCPOPT_FASTOPEN: 601 datalen = len - 2; 602 LENCHECK(datalen); 603 ND_PRINT((ndo, " ")); 604 print_tcp_fastopen_option(ndo, cp, datalen, FALSE); 605 break; 606 607 case TCPOPT_EXPERIMENT2: 608 datalen = len - 2; 609 LENCHECK(datalen); 610 if (datalen < 2) 611 goto bad; 612 /* RFC6994 */ 613 magic = EXTRACT_16BITS(cp); 614 ND_PRINT((ndo, "-")); 615 616 switch(magic) { 617 618 case 0xf989: /* TCP Fast Open RFC 7413 */ 619 print_tcp_fastopen_option(ndo, cp + 2, datalen - 2, TRUE); 620 break; 621 622 default: 623 /* Unknown magic number */ 624 ND_PRINT((ndo, "%04x", magic)); 625 break; 626 } 627 break; 628 629 default: 630 datalen = len - 2; 631 if (datalen) 632 ND_PRINT((ndo, " 0x")); 633 for (i = 0; i < datalen; ++i) { 634 LENCHECK(i + 1); 635 ND_PRINT((ndo, "%02x", cp[i])); 636 } 637 break; 638 } 639 640 /* Account for data printed */ 641 cp += datalen; 642 hlen -= datalen; 643 644 /* Check specification against observed length */ 645 ++datalen; /* option octet */ 646 if (!ZEROLENOPT(opt)) 647 ++datalen; /* size octet */ 648 if (datalen != len) 649 ND_PRINT((ndo, "[len %d]", len)); 650 ch = ','; 651 if (opt == TCPOPT_EOL) 652 break; 653 } 654 ND_PRINT((ndo, "]")); 655 } 656 657 /* 658 * Print length field before crawling down the stack. 659 */ 660 ND_PRINT((ndo, ", length %u", length)); 661 662 if (length <= 0) 663 return; 664 665 /* 666 * Decode payload if necessary. 667 */ 668 bp += TH_OFF(tp) * 4; 669 if ((flags & TH_RST) && ndo->ndo_vflag) { 670 print_tcp_rst_data(ndo, bp, length); 671 return; 672 } 673 674 if (ndo->ndo_packettype) { 675 switch (ndo->ndo_packettype) { 676 case PT_ZMTP1: 677 zmtp1_print(ndo, bp, length); 678 break; 679 case PT_RESP: 680 resp_print(ndo, bp, length); 681 break; 682 } 683 return; 684 } 685 686 if (IS_SRC_OR_DST_PORT(TELNET_PORT)) { 687 telnet_print(ndo, bp, length); 688 } else if (IS_SRC_OR_DST_PORT(SMTP_PORT)) { 689 ND_PRINT((ndo, ": ")); 690 smtp_print(ndo, bp, length); 691 } else if (IS_SRC_OR_DST_PORT(BGP_PORT)) 692 bgp_print(ndo, bp, length); 693 else if (IS_SRC_OR_DST_PORT(PPTP_PORT)) 694 pptp_print(ndo, bp); 695 else if (IS_SRC_OR_DST_PORT(REDIS_PORT)) 696 resp_print(ndo, bp, length); 697 #ifdef ENABLE_SMB 698 else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT)) 699 nbt_tcp_print(ndo, bp, length); 700 else if (IS_SRC_OR_DST_PORT(SMB_PORT)) 701 smb_tcp_print(ndo, bp, length); 702 #endif 703 else if (IS_SRC_OR_DST_PORT(BEEP_PORT)) 704 beep_print(ndo, bp, length); 705 else if (IS_SRC_OR_DST_PORT(OPENFLOW_PORT_OLD) || IS_SRC_OR_DST_PORT(OPENFLOW_PORT_IANA)) 706 openflow_print(ndo, bp, length); 707 else if (IS_SRC_OR_DST_PORT(FTP_PORT)) { 708 ND_PRINT((ndo, ": ")); 709 ftp_print(ndo, bp, length); 710 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT) || IS_SRC_OR_DST_PORT(HTTP_PORT_ALT)) { 711 ND_PRINT((ndo, ": ")); 712 http_print(ndo, bp, length); 713 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT) || IS_SRC_OR_DST_PORT(RTSP_PORT_ALT)) { 714 ND_PRINT((ndo, ": ")); 715 rtsp_print(ndo, bp, length); 716 } else if (length > 2 && 717 (IS_SRC_OR_DST_PORT(NAMESERVER_PORT))) { 718 /* domain_print() assumes it does not have to prepend a space before its 719 * own output to separate it from the output of the calling function. This 720 * works well with udp_print(), but requires a small prop here. 721 */ 722 ND_PRINT((ndo, " ")); 723 724 /* 725 * TCP DNS query has 2byte length at the head. 726 * XXX packet could be unaligned, it can go strange 727 */ 728 ns_print(ndo, bp + 2, length - 2, 0); 729 } else if (IS_SRC_OR_DST_PORT(MSDP_PORT)) { 730 msdp_print(ndo, bp, length); 731 } else if (IS_SRC_OR_DST_PORT(RPKI_RTR_PORT)) { 732 rpki_rtr_print(ndo, bp, length); 733 } 734 else if (length > 0 && (IS_SRC_OR_DST_PORT(LDP_PORT))) { 735 ldp_print(ndo, bp, length); 736 } 737 else if ((IS_SRC_OR_DST_PORT(NFS_PORT)) && 738 length >= 4 && ND_TTEST2(*bp, 4)) { 739 /* 740 * If data present, header length valid, and NFS port used, 741 * assume NFS. 742 * Pass offset of data plus 4 bytes for RPC TCP msg length 743 * to NFS print routines. 744 */ 745 uint32_t fraglen; 746 register const struct sunrpc_msg *rp; 747 enum sunrpc_msg_type direction; 748 749 fraglen = EXTRACT_32BITS(bp) & 0x7FFFFFFF; 750 if (fraglen > (length) - 4) 751 fraglen = (length) - 4; 752 rp = (const struct sunrpc_msg *)(bp + 4); 753 if (ND_TTEST(rp->rm_direction)) { 754 direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction); 755 if (dport == NFS_PORT && direction == SUNRPC_CALL) { 756 ND_PRINT((ndo, ": NFS request xid %u ", EXTRACT_32BITS(&rp->rm_xid))); 757 nfsreq_print_noaddr(ndo, (const u_char *)rp, fraglen, (const u_char *)ip); 758 return; 759 } 760 if (sport == NFS_PORT && direction == SUNRPC_REPLY) { 761 ND_PRINT((ndo, ": NFS reply xid %u ", EXTRACT_32BITS(&rp->rm_xid))); 762 nfsreply_print_noaddr(ndo, (const u_char *)rp, fraglen, (const u_char *)ip); 763 return; 764 } 765 } 766 } 767 768 return; 769 bad: 770 ND_PRINT((ndo, "[bad opt]")); 771 if (ch != '\0') 772 ND_PRINT((ndo, ">")); 773 return; 774 trunc: 775 ND_PRINT((ndo, "[|tcp]")); 776 if (ch != '\0') 777 ND_PRINT((ndo, ">")); 778 } 779 780 /* 781 * RFC1122 says the following on data in RST segments: 782 * 783 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 784 * 785 * A TCP SHOULD allow a received RST segment to include data. 786 * 787 * DISCUSSION 788 * It has been suggested that a RST segment could contain 789 * ASCII text that encoded and explained the cause of the 790 * RST. No standard has yet been established for such 791 * data. 792 * 793 */ 794 795 static void 796 print_tcp_rst_data(netdissect_options *ndo, 797 register const u_char *sp, u_int length) 798 { 799 int c; 800 801 ND_PRINT((ndo, ND_TTEST2(*sp, length) ? " [RST" : " [!RST")); 802 if (length > MAX_RST_DATA_LEN) { 803 length = MAX_RST_DATA_LEN; /* can use -X for longer */ 804 ND_PRINT((ndo, "+")); /* indicate we truncate */ 805 } 806 ND_PRINT((ndo, " ")); 807 while (length-- && sp < ndo->ndo_snapend) { 808 c = *sp++; 809 safeputchar(ndo, c); 810 } 811 ND_PRINT((ndo, "]")); 812 } 813 814 static void 815 print_tcp_fastopen_option(netdissect_options *ndo, register const u_char *cp, 816 u_int datalen, int exp) 817 { 818 u_int i; 819 820 if (exp) 821 ND_PRINT((ndo, "tfo")); 822 823 if (datalen == 0) { 824 /* Fast Open Cookie Request */ 825 ND_PRINT((ndo, " cookiereq")); 826 } else { 827 /* Fast Open Cookie */ 828 if (datalen % 2 != 0 || datalen < 4 || datalen > 16) { 829 ND_PRINT((ndo, " invalid")); 830 } else { 831 ND_PRINT((ndo, " cookie ")); 832 for (i = 0; i < datalen; ++i) 833 ND_PRINT((ndo, "%02x", cp[i])); 834 } 835 } 836 } 837 838 #ifdef HAVE_LIBCRYPTO 839 USES_APPLE_DEPRECATED_API 840 static int 841 tcp_verify_signature(netdissect_options *ndo, 842 const struct ip *ip, const struct tcphdr *tp, 843 const u_char *data, int length, const u_char *rcvsig) 844 { 845 struct tcphdr tp1; 846 u_char sig[TCP_SIGLEN]; 847 char zero_proto = 0; 848 MD5_CTX ctx; 849 uint16_t savecsum, tlen; 850 const struct ip6_hdr *ip6; 851 uint32_t len32; 852 uint8_t nxt; 853 854 if (data + length > ndo->ndo_snapend) { 855 ND_PRINT((ndo, "snaplen too short, ")); 856 return (CANT_CHECK_SIGNATURE); 857 } 858 859 tp1 = *tp; 860 861 if (ndo->ndo_sigsecret == NULL) { 862 ND_PRINT((ndo, "shared secret not supplied with -M, ")); 863 return (CANT_CHECK_SIGNATURE); 864 } 865 866 MD5_Init(&ctx); 867 /* 868 * Step 1: Update MD5 hash with IP pseudo-header. 869 */ 870 if (IP_V(ip) == 4) { 871 MD5_Update(&ctx, (const char *)&ip->ip_src, sizeof(ip->ip_src)); 872 MD5_Update(&ctx, (const char *)&ip->ip_dst, sizeof(ip->ip_dst)); 873 MD5_Update(&ctx, (const char *)&zero_proto, sizeof(zero_proto)); 874 MD5_Update(&ctx, (const char *)&ip->ip_p, sizeof(ip->ip_p)); 875 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4; 876 tlen = htons(tlen); 877 MD5_Update(&ctx, (const char *)&tlen, sizeof(tlen)); 878 } else if (IP_V(ip) == 6) { 879 ip6 = (const struct ip6_hdr *)ip; 880 MD5_Update(&ctx, (const char *)&ip6->ip6_src, sizeof(ip6->ip6_src)); 881 MD5_Update(&ctx, (const char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst)); 882 len32 = htonl(EXTRACT_16BITS(&ip6->ip6_plen)); 883 MD5_Update(&ctx, (const char *)&len32, sizeof(len32)); 884 nxt = 0; 885 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt)); 886 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt)); 887 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt)); 888 nxt = IPPROTO_TCP; 889 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt)); 890 } else { 891 ND_PRINT((ndo, "IP version not 4 or 6, ")); 892 return (CANT_CHECK_SIGNATURE); 893 } 894 895 /* 896 * Step 2: Update MD5 hash with TCP header, excluding options. 897 * The TCP checksum must be set to zero. 898 */ 899 savecsum = tp1.th_sum; 900 tp1.th_sum = 0; 901 MD5_Update(&ctx, (const char *)&tp1, sizeof(struct tcphdr)); 902 tp1.th_sum = savecsum; 903 /* 904 * Step 3: Update MD5 hash with TCP segment data, if present. 905 */ 906 if (length > 0) 907 MD5_Update(&ctx, data, length); 908 /* 909 * Step 4: Update MD5 hash with shared secret. 910 */ 911 MD5_Update(&ctx, ndo->ndo_sigsecret, strlen(ndo->ndo_sigsecret)); 912 MD5_Final(sig, &ctx); 913 914 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0) 915 return (SIGNATURE_VALID); 916 else 917 return (SIGNATURE_INVALID); 918 } 919 USES_APPLE_RST 920 #endif /* HAVE_LIBCRYPTO */ 921 922 /* 923 * Local Variables: 924 * c-style: whitesmith 925 * c-basic-offset: 8 926 * End: 927 */ 928