print-tcp.c revision 197829
11541Srgrimes/* $NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky Exp $ */ 21541Srgrimes 31541Srgrimes/* 41541Srgrimes * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997 51541Srgrimes * The Regents of the University of California. All rights reserved. 61541Srgrimes * 71541Srgrimes * Copyright (c) 1999-2004 The tcpdump.org project 81541Srgrimes * 91541Srgrimes * Redistribution and use in source and binary forms, with or without 101541Srgrimes * modification, are permitted provided that: (1) source code distributions 111541Srgrimes * retain the above copyright notice and this paragraph in its entirety, (2) 121541Srgrimes * distributions including binary code include the above copyright notice and 131541Srgrimes * this paragraph in its entirety in the documentation or other materials 141541Srgrimes * provided with the distribution, and (3) all advertising materials mentioning 151541Srgrimes * features or use of this software display the following acknowledgement: 161541Srgrimes * ``This product includes software developed by the University of California, 171541Srgrimes * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of 181541Srgrimes * the University nor the names of its contributors may be used to endorse 191541Srgrimes * or promote products derived from this software without specific prior 201541Srgrimes * written permission. 211541Srgrimes * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED 221541Srgrimes * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF 231541Srgrimes * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 241541Srgrimes */ 251541Srgrimes 261541Srgrimes#ifndef lint 271541Srgrimesstatic const char rcsid[] _U_ = 281541Srgrimes"@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.130.2.3 2007-12-22 03:08:45 guy Exp $ (LBL)"; 291541Srgrimes #else 301541Srgrimes__RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $"); 311541Srgrimes#endif 321541Srgrimes 331541Srgrimes#ifdef HAVE_CONFIG_H 343308Sphk#include "config.h" 351541Srgrimes#endif 361541Srgrimes 371541Srgrimes#include <tcpdump-stdinc.h> 381541Srgrimes 391541Srgrimes#include <stdio.h> 401541Srgrimes#include <stdlib.h> 411541Srgrimes#include <string.h> 421541Srgrimes 431541Srgrimes#include "interface.h" 441541Srgrimes#include "addrtoname.h" 451541Srgrimes#include "extract.h" 461541Srgrimes 471541Srgrimes#include "tcp.h" 481541Srgrimes 491541Srgrimes#include "ip.h" 501541Srgrimes#ifdef INET6 511541Srgrimes#include "ip6.h" 521541Srgrimes#endif 531541Srgrimes#include "ipproto.h" 541541Srgrimes#include "rpc_auth.h" 551541Srgrimes#include "rpc_msg.h" 561541Srgrimes 571541Srgrimes#include "nameser.h" 581541Srgrimes 591541Srgrimes#ifdef HAVE_LIBCRYPTO 601541Srgrimes#include <openssl/md5.h> 611541Srgrimes 621541Srgrimes#define SIGNATURE_VALID 0 631541Srgrimes#define SIGNATURE_INVALID 1 641541Srgrimes#define CANT_CHECK_SIGNATURE 2 651541Srgrimes 661541Srgrimesstatic int tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp, 671541Srgrimes const u_char *data, int length, const u_char *rcvsig); 681541Srgrimes#endif 691541Srgrimes 701541Srgrimesstatic void print_tcp_rst_data(register const u_char *sp, u_int length); 711541Srgrimes 721541Srgrimes#define MAX_RST_DATA_LEN 30 731541Srgrimes 741541Srgrimes 751541Srgrimesstruct tha { 761541Srgrimes#ifndef INET6 771541Srgrimes struct in_addr src; 781541Srgrimes struct in_addr dst; 791541Srgrimes#else 801541Srgrimes struct in6_addr src; 811541Srgrimes struct in6_addr dst; 821541Srgrimes#endif /*INET6*/ 831541Srgrimes u_int port; 841541Srgrimes}; 851541Srgrimes 861541Srgrimesstruct tcp_seq_hash { 871541Srgrimes struct tcp_seq_hash *nxt; 881541Srgrimes struct tha addr; 891541Srgrimes tcp_seq seq; 901541Srgrimes tcp_seq ack; 911541Srgrimes}; 921541Srgrimes 931541Srgrimes#define TSEQ_HASHSIZE 919 941541Srgrimes 951541Srgrimes/* These tcp optinos do not have the size octet */ 961541Srgrimes#define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP) 971541Srgrimes 981541Srgrimesstatic struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE]; 991541Srgrimes 1001541Srgrimesstruct tok tcp_flag_values[] = { 1011541Srgrimes { TH_FIN, "F" }, 1021541Srgrimes { TH_SYN, "S" }, 1031541Srgrimes { TH_RST, "R" }, 1041541Srgrimes { TH_PUSH, "P" }, 1051541Srgrimes { TH_ACK, "." }, 1061541Srgrimes { TH_URG, "U" }, 1071541Srgrimes { TH_ECNECHO, "E" }, 1081541Srgrimes { TH_CWR, "W" }, 1091541Srgrimes { 0, NULL } 1101541Srgrimes}; 1111541Srgrimes 1121541Srgrimesstruct tok tcp_option_values[] = { 1131541Srgrimes { TCPOPT_EOL, "eol" }, 1141541Srgrimes { TCPOPT_NOP, "nop" }, 1151541Srgrimes { TCPOPT_MAXSEG, "mss" }, 1161541Srgrimes { TCPOPT_WSCALE, "wscale" }, 1171541Srgrimes { TCPOPT_SACKOK, "sackOK" }, 1181541Srgrimes { TCPOPT_SACK, "sack" }, 1191541Srgrimes { TCPOPT_ECHO, "echo" }, 1201541Srgrimes { TCPOPT_ECHOREPLY, "echoreply" }, 1211541Srgrimes { TCPOPT_TIMESTAMP, "TS" }, 1221541Srgrimes { TCPOPT_CC, "cc" }, 1231541Srgrimes { TCPOPT_CCNEW, "ccnew" }, 1241541Srgrimes { TCPOPT_CCECHO, "" }, 1251541Srgrimes { TCPOPT_SIGNATURE, "md5" }, 1261541Srgrimes { TCPOPT_AUTH, "enhanced auth" }, 1271541Srgrimes { TCPOPT_UTO, "uto" }, 1281541Srgrimes { 0, NULL } 1291541Srgrimes}; 1301541Srgrimes 1311541Srgrimesstatic int tcp_cksum(register const struct ip *ip, 1321541Srgrimes register const struct tcphdr *tp, 1331541Srgrimes register u_int len) 1341541Srgrimes{ 1351541Srgrimes union phu { 1361541Srgrimes struct phdr { 1371541Srgrimes u_int32_t src; 1381541Srgrimes u_int32_t dst; 1391541Srgrimes u_char mbz; 1401541Srgrimes u_char proto; 1411541Srgrimes u_int16_t len; 1421541Srgrimes } ph; 1431541Srgrimes u_int16_t pa[6]; 1441541Srgrimes } phu; 1451541Srgrimes const u_int16_t *sp; 1461541Srgrimes 1471541Srgrimes /* pseudo-header.. */ 1481541Srgrimes phu.ph.len = htons((u_int16_t)len); 1491541Srgrimes phu.ph.mbz = 0; 1501541Srgrimes phu.ph.proto = IPPROTO_TCP; 1511541Srgrimes memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t)); 1521541Srgrimes if (IP_HL(ip) == 5) 1531541Srgrimes memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t)); 1541541Srgrimes else 1551541Srgrimes phu.ph.dst = ip_finddst(ip); 1561541Srgrimes 1571541Srgrimes sp = &phu.pa[0]; 1581541Srgrimes return in_cksum((u_short *)tp, len, 1591541Srgrimes sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5]); 1601541Srgrimes} 1611541Srgrimes 1621541Srgrimes#ifdef INET6 1631541Srgrimesstatic int tcp6_cksum(const struct ip6_hdr *ip6, const struct tcphdr *tp, 1643308Sphk u_int len) 1653308Sphk{ 1661541Srgrimes size_t i; 1671541Srgrimes u_int32_t sum = 0; 1681541Srgrimes union { 1691541Srgrimes struct { 1701541Srgrimes struct in6_addr ph_src; 1713308Sphk struct in6_addr ph_dst; 1723308Sphk u_int32_t ph_len; 1731541Srgrimes u_int8_t ph_zero[3]; 1741541Srgrimes u_int8_t ph_nxt; 1751541Srgrimes } ph; 1761541Srgrimes u_int16_t pa[20]; 1771541Srgrimes } phu; 1781541Srgrimes 1793308Sphk /* pseudo-header */ 1803308Sphk memset(&phu, 0, sizeof(phu)); 1811541Srgrimes phu.ph.ph_src = ip6->ip6_src; 1821541Srgrimes phu.ph.ph_dst = ip6->ip6_dst; 1831541Srgrimes phu.ph.ph_len = htonl(len); 1841541Srgrimes phu.ph.ph_nxt = IPPROTO_TCP; 1851541Srgrimes 1861541Srgrimes for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++) 1871541Srgrimes sum += phu.pa[i]; 1881541Srgrimes 1891541Srgrimes return in_cksum((u_short *)tp, len, sum); 1901541Srgrimes} 1911541Srgrimes#endif 1921541Srgrimes 1931549Srgrimesvoid 1941541Srgrimestcp_print(register const u_char *bp, register u_int length, 1951541Srgrimes register const u_char *bp2, int fragmented) 1961541Srgrimes{ 1971541Srgrimes register const struct tcphdr *tp; 1981541Srgrimes register const struct ip *ip; 1991541Srgrimes register u_char flags; 2001541Srgrimes register u_int hlen; 2011541Srgrimes register char ch; 2021541Srgrimes u_int16_t sport, dport, win, urp; 2031541Srgrimes u_int32_t seq, ack, thseq, thack; 2041541Srgrimes int threv; 2051541Srgrimes#ifdef INET6 2061541Srgrimes register const struct ip6_hdr *ip6; 2071541Srgrimes#endif 2081541Srgrimes 2091541Srgrimes tp = (struct tcphdr *)bp; 2101541Srgrimes ip = (struct ip *)bp2; 2111541Srgrimes#ifdef INET6 2121541Srgrimes if (IP_V(ip) == 6) 2131541Srgrimes ip6 = (struct ip6_hdr *)bp2; 2141541Srgrimes else 2153308Sphk ip6 = NULL; 2163308Sphk#endif /*INET6*/ 2173308Sphk ch = '\0'; 2183308Sphk if (!TTEST(tp->th_dport)) { 2191541Srgrimes (void)printf("%s > %s: [|tcp]", 2203308Sphk ipaddr_string(&ip->ip_src), 2213308Sphk ipaddr_string(&ip->ip_dst)); 2221541Srgrimes return; 2231541Srgrimes } 2241541Srgrimes 2251541Srgrimes sport = EXTRACT_16BITS(&tp->th_sport); 2261541Srgrimes dport = EXTRACT_16BITS(&tp->th_dport); 2271541Srgrimes 2283308Sphk hlen = TH_OFF(tp) * 4; 2293308Sphk 2301541Srgrimes /* 2311541Srgrimes * If data present, header length valid, and NFS port used, 2321541Srgrimes * assume NFS. 2331541Srgrimes * Pass offset of data plus 4 bytes for RPC TCP msg length 2341541Srgrimes * to NFS print routines. 2353308Sphk */ 2361541Srgrimes if (!qflag && hlen >= sizeof(*tp) && hlen <= length && 2371541Srgrimes (length - hlen) >= 4) { 2381541Srgrimes u_char *fraglenp; 2391541Srgrimes u_int32_t fraglen; 2401541Srgrimes register struct sunrpc_msg *rp; 2411541Srgrimes enum sunrpc_msg_type direction; 2421541Srgrimes 2431541Srgrimes fraglenp = (u_char *)tp + hlen; 2441541Srgrimes if (TTEST2(*fraglenp, 4)) { 2451541Srgrimes fraglen = EXTRACT_32BITS(fraglenp) & 0x7FFFFFFF; 2461541Srgrimes if (fraglen > (length - hlen) - 4) 2471541Srgrimes fraglen = (length - hlen) - 4; 2481541Srgrimes rp = (struct sunrpc_msg *)(fraglenp + 4); 2491541Srgrimes if (TTEST(rp->rm_direction)) { 2501541Srgrimes direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction); 2511541Srgrimes if (dport == NFS_PORT && 2521541Srgrimes direction == SUNRPC_CALL) { 2531541Srgrimes nfsreq_print((u_char *)rp, fraglen, 2543308Sphk (u_char *)ip); 2553308Sphk return; 2561541Srgrimes } 2571541Srgrimes if (sport == NFS_PORT && 2581541Srgrimes direction == SUNRPC_REPLY) { 2591541Srgrimes nfsreply_print((u_char *)rp, fraglen, 2601541Srgrimes (u_char *)ip); 2611541Srgrimes return; 2621541Srgrimes } 2631541Srgrimes } 2641541Srgrimes } 2651549Srgrimes } 2661541Srgrimes#ifdef INET6 2671541Srgrimes if (ip6) { 2681541Srgrimes if (ip6->ip6_nxt == IPPROTO_TCP) { 2691541Srgrimes (void)printf("%s.%s > %s.%s: ", 2701541Srgrimes ip6addr_string(&ip6->ip6_src), 2711541Srgrimes tcpport_string(sport), 2721541Srgrimes ip6addr_string(&ip6->ip6_dst), 2731541Srgrimes tcpport_string(dport)); 2741541Srgrimes } else { 2751541Srgrimes (void)printf("%s > %s: ", 2761541Srgrimes tcpport_string(sport), tcpport_string(dport)); 2771549Srgrimes } 2781541Srgrimes } else 2791541Srgrimes#endif /*INET6*/ 2801541Srgrimes { 2811541Srgrimes if (ip->ip_p == IPPROTO_TCP) { 2821541Srgrimes (void)printf("%s.%s > %s.%s: ", 2831541Srgrimes ipaddr_string(&ip->ip_src), 2841541Srgrimes tcpport_string(sport), 2851541Srgrimes ipaddr_string(&ip->ip_dst), 2861541Srgrimes tcpport_string(dport)); 2871541Srgrimes } else { 2881541Srgrimes (void)printf("%s > %s: ", 2891541Srgrimes tcpport_string(sport), tcpport_string(dport)); 2901541Srgrimes } 2911541Srgrimes } 2921541Srgrimes 2931541Srgrimes if (hlen < sizeof(*tp)) { 2941541Srgrimes (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]", 2951541Srgrimes length - hlen, hlen, (unsigned long)sizeof(*tp)); 2961541Srgrimes return; 2971541Srgrimes } 2981541Srgrimes 2991541Srgrimes TCHECK(*tp); 3001541Srgrimes 3011541Srgrimes seq = EXTRACT_32BITS(&tp->th_seq); 3021541Srgrimes ack = EXTRACT_32BITS(&tp->th_ack); 3031549Srgrimes win = EXTRACT_16BITS(&tp->th_win); 3041541Srgrimes urp = EXTRACT_16BITS(&tp->th_urp); 3051541Srgrimes 3061541Srgrimes if (qflag) { 3071541Srgrimes (void)printf("tcp %d", length - hlen); 3081541Srgrimes if (hlen > length) { 3091541Srgrimes (void)printf(" [bad hdr length %u - too long, > %u]", 3101541Srgrimes hlen, length); 3111541Srgrimes } 3121541Srgrimes return; 3131541Srgrimes } 3141541Srgrimes 3151541Srgrimes flags = tp->th_flags; 3161541Srgrimes printf("Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags)); 3171541Srgrimes 3181541Srgrimes if (!Sflag && (flags & TH_ACK)) { 3193308Sphk register struct tcp_seq_hash *th; 3203308Sphk const void *src, *dst; 3211541Srgrimes register int rev; 3221541Srgrimes struct tha tha; 3231541Srgrimes /* 3241541Srgrimes * Find (or record) the initial sequence numbers for 3251541Srgrimes * this conversation. (we pick an arbitrary 3261541Srgrimes * collating order so there's only one entry for 3273308Sphk * both directions). 3283308Sphk */ 3291541Srgrimes#ifdef INET6 3301541Srgrimes memset(&tha, 0, sizeof(tha)); 3311541Srgrimes rev = 0; 3321541Srgrimes if (ip6) { 3331541Srgrimes src = &ip6->ip6_src; 3341541Srgrimes dst = &ip6->ip6_dst; 3353308Sphk if (sport > dport) 3363308Sphk rev = 1; 3371541Srgrimes else if (sport == dport) { 3381541Srgrimes if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0) 3391541Srgrimes rev = 1; 3401541Srgrimes } 3411541Srgrimes if (rev) { 3421541Srgrimes memcpy(&tha.src, dst, sizeof ip6->ip6_dst); 3431541Srgrimes memcpy(&tha.dst, src, sizeof ip6->ip6_src); 3441541Srgrimes tha.port = dport << 16 | sport; 345 } else { 346 memcpy(&tha.dst, dst, sizeof ip6->ip6_dst); 347 memcpy(&tha.src, src, sizeof ip6->ip6_src); 348 tha.port = sport << 16 | dport; 349 } 350 } else { 351 src = &ip->ip_src; 352 dst = &ip->ip_dst; 353 if (sport > dport) 354 rev = 1; 355 else if (sport == dport) { 356 if (memcmp(src, dst, sizeof ip->ip_dst) > 0) 357 rev = 1; 358 } 359 if (rev) { 360 memcpy(&tha.src, dst, sizeof ip->ip_dst); 361 memcpy(&tha.dst, src, sizeof ip->ip_src); 362 tha.port = dport << 16 | sport; 363 } else { 364 memcpy(&tha.dst, dst, sizeof ip->ip_dst); 365 memcpy(&tha.src, src, sizeof ip->ip_src); 366 tha.port = sport << 16 | dport; 367 } 368 } 369#else 370 rev = 0; 371 src = &ip->ip_src; 372 dst = &ip->ip_dst; 373 if (sport > dport) 374 rev = 1; 375 else if (sport == dport) { 376 if (memcmp(src, dst, sizeof ip->ip_dst) > 0) 377 rev = 1; 378 } 379 if (rev) { 380 memcpy(&tha.src, dst, sizeof ip->ip_dst); 381 memcpy(&tha.dst, src, sizeof ip->ip_src); 382 tha.port = dport << 16 | sport; 383 } else { 384 memcpy(&tha.dst, dst, sizeof ip->ip_dst); 385 memcpy(&tha.src, src, sizeof ip->ip_src); 386 tha.port = sport << 16 | dport; 387 } 388#endif 389 390 threv = rev; 391 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE]; 392 th->nxt; th = th->nxt) 393 if (memcmp((char *)&tha, (char *)&th->addr, 394 sizeof(th->addr)) == 0) 395 break; 396 397 if (!th->nxt || (flags & TH_SYN)) { 398 /* didn't find it or new conversation */ 399 if (th->nxt == NULL) { 400 th->nxt = (struct tcp_seq_hash *) 401 calloc(1, sizeof(*th)); 402 if (th->nxt == NULL) 403 error("tcp_print: calloc"); 404 } 405 th->addr = tha; 406 if (rev) 407 th->ack = seq, th->seq = ack - 1; 408 else 409 th->seq = seq, th->ack = ack - 1; 410 } else { 411 if (rev) 412 seq -= th->ack, ack -= th->seq; 413 else 414 seq -= th->seq, ack -= th->ack; 415 } 416 417 thseq = th->seq; 418 thack = th->ack; 419 } else { 420 /*fool gcc*/ 421 thseq = thack = threv = 0; 422 } 423 if (hlen > length) { 424 (void)printf(" [bad hdr length %u - too long, > %u]", 425 hlen, length); 426 return; 427 } 428 429 if (IP_V(ip) == 4 && vflag && !Kflag && !fragmented) { 430 u_int16_t sum, tcp_sum; 431 if (TTEST2(tp->th_sport, length)) { 432 sum = tcp_cksum(ip, tp, length); 433 434 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum)); 435 if (sum != 0) { 436 tcp_sum = EXTRACT_16BITS(&tp->th_sum); 437 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum, sum)); 438 } else 439 (void)printf(" (correct)"); 440 } 441 } 442#ifdef INET6 443 if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !Kflag && !fragmented) { 444 u_int16_t sum,tcp_sum; 445 if (TTEST2(tp->th_sport, length)) { 446 sum = tcp6_cksum(ip6, tp, length); 447 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum)); 448 if (sum != 0) { 449 tcp_sum = EXTRACT_16BITS(&tp->th_sum); 450 (void)printf(" (incorrect -> 0x%04x)",in_cksum_shouldbe(tcp_sum, sum)); 451 } else 452 (void)printf(" (correct)"); 453 454 } 455 } 456#endif 457 458 length -= hlen; 459 if (vflag > 1 || flags & (TH_SYN | TH_FIN | TH_RST)) { 460 (void)printf(", seq %u", seq); 461 462 if (length > 0) { 463 (void)printf(":%u", seq + length); 464 } 465 } 466 467 if (flags & TH_ACK) { 468 (void)printf(", ack %u", ack); 469 } 470 471 (void)printf(", win %d", win); 472 473 if (flags & TH_URG) 474 (void)printf(", urg %d", urp); 475 /* 476 * Handle any options. 477 */ 478 if (hlen > sizeof(*tp)) { 479 register const u_char *cp; 480 register u_int i, opt, datalen; 481 register u_int len; 482 483 hlen -= sizeof(*tp); 484 cp = (const u_char *)tp + sizeof(*tp); 485 printf(", options ["); 486 while (hlen > 0) { 487 if (ch != '\0') 488 putchar(ch); 489 TCHECK(*cp); 490 opt = *cp++; 491 if (ZEROLENOPT(opt)) 492 len = 1; 493 else { 494 TCHECK(*cp); 495 len = *cp++; /* total including type, len */ 496 if (len < 2 || len > hlen) 497 goto bad; 498 --hlen; /* account for length byte */ 499 } 500 --hlen; /* account for type byte */ 501 datalen = 0; 502 503/* Bail if "l" bytes of data are not left or were not captured */ 504#define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); } 505 506 507 printf("%s", tok2str(tcp_option_values, "Unknown Option %u", opt)); 508 509 switch (opt) { 510 511 case TCPOPT_MAXSEG: 512 datalen = 2; 513 LENCHECK(datalen); 514 (void)printf(" %u", EXTRACT_16BITS(cp)); 515 break; 516 517 case TCPOPT_WSCALE: 518 datalen = 1; 519 LENCHECK(datalen); 520 (void)printf(" %u", *cp); 521 break; 522 523 case TCPOPT_SACK: 524 datalen = len - 2; 525 if (datalen % 8 != 0) { 526 (void)printf("malformed sack"); 527 } else { 528 u_int32_t s, e; 529 530 (void)printf(" %d ", datalen / 8); 531 for (i = 0; i < datalen; i += 8) { 532 LENCHECK(i + 4); 533 s = EXTRACT_32BITS(cp + i); 534 LENCHECK(i + 8); 535 e = EXTRACT_32BITS(cp + i + 4); 536 if (threv) { 537 s -= thseq; 538 e -= thseq; 539 } else { 540 s -= thack; 541 e -= thack; 542 } 543 (void)printf("{%u:%u}", s, e); 544 } 545 } 546 break; 547 548 case TCPOPT_CC: 549 case TCPOPT_CCNEW: 550 case TCPOPT_CCECHO: 551 case TCPOPT_ECHO: 552 case TCPOPT_ECHOREPLY: 553 554 /* 555 * those options share their semantics. 556 * fall through 557 */ 558 datalen = 4; 559 LENCHECK(datalen); 560 (void)printf(" %u", EXTRACT_32BITS(cp)); 561 break; 562 563 case TCPOPT_TIMESTAMP: 564 datalen = 8; 565 LENCHECK(datalen); 566 (void)printf(" val %u ecr %u", 567 EXTRACT_32BITS(cp), 568 EXTRACT_32BITS(cp + 4)); 569 break; 570 571 case TCPOPT_SIGNATURE: 572 datalen = TCP_SIGLEN; 573 LENCHECK(datalen); 574#ifdef HAVE_LIBCRYPTO 575 switch (tcp_verify_signature(ip, tp, 576 bp + TH_OFF(tp) * 4, length, cp)) { 577 578 case SIGNATURE_VALID: 579 (void)printf("valid"); 580 break; 581 582 case SIGNATURE_INVALID: 583 (void)printf("invalid"); 584 break; 585 586 case CANT_CHECK_SIGNATURE: 587 (void)printf("can't check - "); 588 for (i = 0; i < TCP_SIGLEN; ++i) 589 (void)printf("%02x", cp[i]); 590 break; 591 } 592#else 593 for (i = 0; i < TCP_SIGLEN; ++i) 594 (void)printf("%02x", cp[i]); 595#endif 596 break; 597 598 case TCPOPT_AUTH: 599 (void)printf("keyid %d", *cp++); 600 datalen = len - 3; 601 for (i = 0; i < datalen; ++i) { 602 LENCHECK(i); 603 (void)printf("%02x", cp[i]); 604 } 605 break; 606 607 608 case TCPOPT_EOL: 609 case TCPOPT_NOP: 610 case TCPOPT_SACKOK: 611 /* 612 * Nothing interesting. 613 * fall through 614 */ 615 break; 616 617 case TCPOPT_UTO: 618 datalen = 2; 619 LENCHECK(datalen); 620 uint utoval = EXTRACT_16BITS(cp); 621 if (utoval & 0x0001) 622 utoval = (utoval >> 1) * 60; 623 else 624 utoval >>= 1; 625 (void)printf(" %u", utoval); 626 break; 627 628 default: 629 datalen = len - 2; 630 for (i = 0; i < datalen; ++i) { 631 LENCHECK(i); 632 (void)printf("%02x", cp[i]); 633 } 634 break; 635 } 636 637 /* Account for data printed */ 638 cp += datalen; 639 hlen -= datalen; 640 641 /* Check specification against observed length */ 642 ++datalen; /* option octet */ 643 if (!ZEROLENOPT(opt)) 644 ++datalen; /* size octet */ 645 if (datalen != len) 646 (void)printf("[len %d]", len); 647 ch = ','; 648 if (opt == TCPOPT_EOL) 649 break; 650 } 651 putchar(']'); 652 } 653 654 /* 655 * Print length field before crawling down the stack. 656 */ 657 printf(", length %u", length); 658 659 if (length <= 0) 660 return; 661 662 /* 663 * Decode payload if necessary. 664 */ 665 bp += TH_OFF(tp) * 4; 666 if ((flags & TH_RST) && vflag) { 667 print_tcp_rst_data(bp, length); 668 return; 669 } 670 671 if (sport == TELNET_PORT || dport == TELNET_PORT) { 672 if (!qflag && vflag) 673 telnet_print(bp, length); 674 } else if (sport == BGP_PORT || dport == BGP_PORT) 675 bgp_print(bp, length); 676 else if (sport == PPTP_PORT || dport == PPTP_PORT) 677 pptp_print(bp); 678#ifdef TCPDUMP_DO_SMB 679 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT) 680 nbt_tcp_print(bp, length); 681 else if (sport == SMB_PORT || dport == SMB_PORT) 682 smb_tcp_print(bp, length); 683#endif 684 else if (sport == BEEP_PORT || dport == BEEP_PORT) 685 beep_print(bp, length); 686 else if (length > 2 && 687 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT || 688 sport == MULTICASTDNS_PORT || dport == MULTICASTDNS_PORT)) { 689 /* 690 * TCP DNS query has 2byte length at the head. 691 * XXX packet could be unaligned, it can go strange 692 */ 693 ns_print(bp + 2, length - 2, 0); 694 } else if (sport == MSDP_PORT || dport == MSDP_PORT) { 695 msdp_print(bp, length); 696 } 697 else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) { 698 ldp_print(bp, length); 699 } 700 701 return; 702 bad: 703 fputs("[bad opt]", stdout); 704 if (ch != '\0') 705 putchar('>'); 706 return; 707 trunc: 708 fputs("[|tcp]", stdout); 709 if (ch != '\0') 710 putchar('>'); 711} 712 713/* 714 * RFC1122 says the following on data in RST segments: 715 * 716 * 4.2.2.12 RST Segment: RFC-793 Section 3.4 717 * 718 * A TCP SHOULD allow a received RST segment to include data. 719 * 720 * DISCUSSION 721 * It has been suggested that a RST segment could contain 722 * ASCII text that encoded and explained the cause of the 723 * RST. No standard has yet been established for such 724 * data. 725 * 726 */ 727 728static void 729print_tcp_rst_data(register const u_char *sp, u_int length) 730{ 731 int c; 732 733 if (TTEST2(*sp, length)) 734 printf(" [RST"); 735 else 736 printf(" [!RST"); 737 if (length > MAX_RST_DATA_LEN) { 738 length = MAX_RST_DATA_LEN; /* can use -X for longer */ 739 putchar('+'); /* indicate we truncate */ 740 } 741 putchar(' '); 742 while (length-- && sp <= snapend) { 743 c = *sp++; 744 safeputchar(c); 745 } 746 putchar(']'); 747} 748 749#ifdef HAVE_LIBCRYPTO 750static int 751tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp, 752 const u_char *data, int length, const u_char *rcvsig) 753{ 754 struct tcphdr tp1; 755 u_char sig[TCP_SIGLEN]; 756 char zero_proto = 0; 757 MD5_CTX ctx; 758 u_int16_t savecsum, tlen; 759#ifdef INET6 760 struct ip6_hdr *ip6; 761 u_int32_t len32; 762 u_int8_t nxt; 763#endif 764 765 tp1 = *tp; 766 767 if (tcpmd5secret == NULL) 768 return (CANT_CHECK_SIGNATURE); 769 770 MD5_Init(&ctx); 771 /* 772 * Step 1: Update MD5 hash with IP pseudo-header. 773 */ 774 if (IP_V(ip) == 4) { 775 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src)); 776 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst)); 777 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto)); 778 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p)); 779 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4; 780 tlen = htons(tlen); 781 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen)); 782#ifdef INET6 783 } else if (IP_V(ip) == 6) { 784 ip6 = (struct ip6_hdr *)ip; 785 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src)); 786 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst)); 787 len32 = htonl(ntohs(ip6->ip6_plen)); 788 MD5_Update(&ctx, (char *)&len32, sizeof(len32)); 789 nxt = 0; 790 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt)); 791 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt)); 792 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt)); 793 nxt = IPPROTO_TCP; 794 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt)); 795#endif 796 } else 797 return (CANT_CHECK_SIGNATURE); 798 799 /* 800 * Step 2: Update MD5 hash with TCP header, excluding options. 801 * The TCP checksum must be set to zero. 802 */ 803 savecsum = tp1.th_sum; 804 tp1.th_sum = 0; 805 MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr)); 806 tp1.th_sum = savecsum; 807 /* 808 * Step 3: Update MD5 hash with TCP segment data, if present. 809 */ 810 if (length > 0) 811 MD5_Update(&ctx, data, length); 812 /* 813 * Step 4: Update MD5 hash with shared secret. 814 */ 815 MD5_Update(&ctx, tcpmd5secret, strlen(tcpmd5secret)); 816 MD5_Final(sig, &ctx); 817 818 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0) 819 return (SIGNATURE_VALID); 820 else 821 return (SIGNATURE_INVALID); 822} 823#endif /* HAVE_LIBCRYPTO */ 824 825/* 826 * Local Variables: 827 * c-style: whitesmith 828 * c-basic-offset: 8 829 * End: 830 */ 831