X-Git-Url: https://git.tcpdump.org/tcpdump/blobdiff_plain/447060b9fbb3aacdeba6fca7f86061118ead7ffa..299fc72fea7a32b18a77457c70028e9679fd5c0d:/print-tcp.c diff --git a/print-tcp.c b/print-tcp.c index 4baaee3a..1886044b 100644 --- a/print-tcp.c +++ b/print-tcp.c @@ -31,10 +31,10 @@ __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $"); #endif #ifdef HAVE_CONFIG_H -#include "config.h" +#include #endif -#include +#include "netdissect-stdinc.h" #include #include @@ -57,7 +57,7 @@ __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $"); static int tcp_verify_signature(netdissect_options *ndo, const struct ip *ip, const struct tcphdr *tp, - const u_char *data, int length, const u_char *rcvsig); + const u_char *data, u_int length, const u_char *rcvsig); #endif static void print_tcp_rst_data(netdissect_options *, const u_char *sp, u_int length); @@ -142,8 +142,8 @@ tcp_cksum(netdissect_options *ndo, const struct tcphdr *tp, u_int len) { - return nextproto4_cksum(ndo, ip, (const uint8_t *)tp, len, len, - IPPROTO_TCP); + return nextproto4_cksum(ndo, ip, (const uint8_t *)tp, len, len, + IPPROTO_TCP); } static int @@ -152,8 +152,8 @@ tcp6_cksum(netdissect_options *ndo, const struct tcphdr *tp, u_int len) { - return nextproto6_cksum(ndo, ip6, (const uint8_t *)tp, len, len, - IPPROTO_TCP); + return nextproto6_cksum(ndo, ip6, (const uint8_t *)tp, len, len, + IPPROTO_TCP); } void @@ -173,6 +173,7 @@ tcp_print(netdissect_options *ndo, int rev; const struct ip6_hdr *ip6; + ndo->ndo_protocol = "tcp"; tp = (const struct tcphdr *)bp; ip = (const struct ip *)bp2; if (IP_V(ip) == 6) @@ -181,9 +182,16 @@ tcp_print(netdissect_options *ndo, ip6 = NULL; ch = '\0'; if (!ND_TTEST_2(tp->th_dport)) { - ND_PRINT("%s > %s: [|tcp]", - ipaddr_string(ndo, &ip->ip_src), - ipaddr_string(ndo, &ip->ip_dst)); + if (ip6) { + ND_PRINT("%s > %s:", + ip6addr_string(ndo, ip6->ip6_src), + ip6addr_string(ndo, ip6->ip6_dst)); + } else { + ND_PRINT("%s > %s:", + ipaddr_string(ndo, ip->ip_src), + ipaddr_string(ndo, ip->ip_dst)); + } + nd_print_trunc(ndo); return; } @@ -193,24 +201,24 @@ tcp_print(netdissect_options *ndo, if (ip6) { if (EXTRACT_U_1(ip6->ip6_nxt) == IPPROTO_TCP) { ND_PRINT("%s.%s > %s.%s: ", - ip6addr_string(ndo, &ip6->ip6_src), - tcpport_string(ndo, sport), - ip6addr_string(ndo, &ip6->ip6_dst), - tcpport_string(ndo, dport)); + ip6addr_string(ndo, ip6->ip6_src), + tcpport_string(ndo, sport), + ip6addr_string(ndo, ip6->ip6_dst), + tcpport_string(ndo, dport)); } else { ND_PRINT("%s > %s: ", - tcpport_string(ndo, sport), tcpport_string(ndo, dport)); + tcpport_string(ndo, sport), tcpport_string(ndo, dport)); } } else { if (EXTRACT_U_1(ip->ip_p) == IPPROTO_TCP) { ND_PRINT("%s.%s > %s.%s: ", - ipaddr_string(ndo, &ip->ip_src), - tcpport_string(ndo, sport), - ipaddr_string(ndo, &ip->ip_dst), - tcpport_string(ndo, dport)); + ipaddr_string(ndo, ip->ip_src), + tcpport_string(ndo, sport), + ipaddr_string(ndo, ip->ip_dst), + tcpport_string(ndo, dport)); } else { ND_PRINT("%s > %s: ", - tcpport_string(ndo, sport), tcpport_string(ndo, dport)); + tcpport_string(ndo, sport), tcpport_string(ndo, dport)); } } @@ -219,8 +227,8 @@ tcp_print(netdissect_options *ndo, hlen = TH_OFF(tp) * 4; if (hlen < sizeof(*tp)) { - ND_PRINT(" tcp %d [bad hdr length %u - too short, < %lu]", - length - hlen, hlen, (unsigned long)sizeof(*tp)); + ND_PRINT(" tcp %u [bad hdr length %u - too short, < %lu]", + length - hlen, hlen, (unsigned long)sizeof(*tp)); return; } @@ -230,10 +238,10 @@ tcp_print(netdissect_options *ndo, urp = EXTRACT_BE_U_2(tp->th_urp); if (ndo->ndo_qflag) { - ND_PRINT("tcp %d", length - hlen); + ND_PRINT("tcp %u", length - hlen); if (hlen > length) { ND_PRINT(" [bad hdr length %u - too long, > %u]", - hlen, length); + hlen, length); } return; } @@ -256,22 +264,22 @@ tcp_print(netdissect_options *ndo, struct tha6 tha; tcp_seq_hash = tcp_seq_hash6; - src = (const void *)&ip6->ip6_src; - dst = (const void *)&ip6->ip6_dst; + src = (const void *)ip6->ip6_src; + dst = (const void *)ip6->ip6_dst; if (sport > dport) rev = 1; else if (sport == dport) { - if (UNALIGNED_MEMCMP(src, dst, sizeof ip6->ip6_dst) > 0) + if (UNALIGNED_MEMCMP(src, dst, sizeof(ip6->ip6_dst)) > 0) rev = 1; } if (rev) { - UNALIGNED_MEMCPY(&tha.src, dst, sizeof ip6->ip6_dst); - UNALIGNED_MEMCPY(&tha.dst, src, sizeof ip6->ip6_src); - tha.port = dport << 16 | sport; + UNALIGNED_MEMCPY(&tha.src, dst, sizeof(ip6->ip6_dst)); + UNALIGNED_MEMCPY(&tha.dst, src, sizeof(ip6->ip6_src)); + tha.port = ((u_int)dport) << 16 | sport; } else { - UNALIGNED_MEMCPY(&tha.dst, dst, sizeof ip6->ip6_dst); - UNALIGNED_MEMCPY(&tha.src, src, sizeof ip6->ip6_src); - tha.port = sport << 16 | dport; + UNALIGNED_MEMCPY(&tha.dst, dst, sizeof(ip6->ip6_dst)); + UNALIGNED_MEMCPY(&tha.src, src, sizeof(ip6->ip6_src)); + tha.port = ((u_int)sport) << 16 | dport; } for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE]; @@ -282,12 +290,15 @@ tcp_print(netdissect_options *ndo, if (!th->nxt || (flags & TH_SYN)) { /* didn't find it or new conversation */ + /* calloc() return used by the 'tcp_seq_hash6' + hash table: do not free() */ if (th->nxt == NULL) { th->nxt = (struct tcp_seq_hash6 *) calloc(1, sizeof(*th)); if (th->nxt == NULL) (*ndo->ndo_error)(ndo, - "tcp_print: calloc"); + S_ERR_ND_MEM_ALLOC, + "tcp_print: calloc"); } th->addr = tha; if (rev) @@ -312,17 +323,21 @@ tcp_print(netdissect_options *ndo, if (sport > dport) rev = 1; else if (sport == dport) { - if (UNALIGNED_MEMCMP(&ip->ip_src, &ip->ip_dst, sizeof ip->ip_dst) > 0) + if (UNALIGNED_MEMCMP(ip->ip_src, ip->ip_dst, sizeof(ip->ip_dst)) > 0) rev = 1; } if (rev) { - UNALIGNED_MEMCPY(&tha.src, &ip->ip_dst, sizeof ip->ip_dst); - UNALIGNED_MEMCPY(&tha.dst, &ip->ip_src, sizeof ip->ip_src); - tha.port = dport << 16 | sport; + UNALIGNED_MEMCPY(&tha.src, ip->ip_dst, + sizeof(ip->ip_dst)); + UNALIGNED_MEMCPY(&tha.dst, ip->ip_src, + sizeof(ip->ip_src)); + tha.port = ((u_int)dport) << 16 | sport; } else { - UNALIGNED_MEMCPY(&tha.dst, &ip->ip_dst, sizeof ip->ip_dst); - UNALIGNED_MEMCPY(&tha.src, &ip->ip_src, sizeof ip->ip_src); - tha.port = sport << 16 | dport; + UNALIGNED_MEMCPY(&tha.dst, ip->ip_dst, + sizeof(ip->ip_dst)); + UNALIGNED_MEMCPY(&tha.src, ip->ip_src, + sizeof(ip->ip_src)); + tha.port = ((u_int)sport) << 16 | dport; } for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE]; @@ -333,12 +348,15 @@ tcp_print(netdissect_options *ndo, if (!th->nxt || (flags & TH_SYN)) { /* didn't find it or new conversation */ + /* calloc() return used by the 'tcp_seq_hash4' + hash table: do not free() */ if (th->nxt == NULL) { th->nxt = (struct tcp_seq_hash *) calloc(1, sizeof(*th)); if (th->nxt == NULL) (*ndo->ndo_error)(ndo, - "tcp_print: calloc"); + S_ERR_ND_MEM_ALLOC, + "tcp_print: calloc"); } th->addr = tha; if (rev) @@ -361,7 +379,7 @@ tcp_print(netdissect_options *ndo, } if (hlen > length) { ND_PRINT(" [bad hdr length %u - too long, > %u]", - hlen, length); + hlen, length); return; } @@ -410,10 +428,10 @@ tcp_print(netdissect_options *ndo, ND_PRINT(", ack %u", ack); } - ND_PRINT(", win %d", win); + ND_PRINT(", win %u", win); if (flags & TH_URG) - ND_PRINT(", urg %d", urp); + ND_PRINT(", urg %u", urp); /* * Handle any options. */ @@ -471,7 +489,7 @@ tcp_print(netdissect_options *ndo, } else { uint32_t s, e; - ND_PRINT(" %d ", datalen / 8); + ND_PRINT(" %u ", datalen / 8); for (i = 0; i < datalen; i += 8) { LENCHECK(i + 4); s = EXTRACT_BE_U_4(cp + i); @@ -525,7 +543,7 @@ tcp_print(netdissect_options *ndo, break; case SIGNATURE_INVALID: - ND_PRINT("invalid"); + nd_print_invalid(ndo); break; case CANT_CHECK_SIGNATURE: @@ -554,7 +572,7 @@ tcp_print(netdissect_options *ndo, * at this point.) */ if (datalen < 2) { - ND_PRINT(" invalid"); + nd_print_invalid(ndo); } else { LENCHECK(1); ND_PRINT(" keyid %u", EXTRACT_U_1(cp)); @@ -643,11 +661,11 @@ tcp_print(netdissect_options *ndo, hlen -= datalen; /* Check specification against observed length */ - ++datalen; /* option octet */ + ++datalen; /* option octet */ if (!ZEROLENOPT(opt)) - ++datalen; /* size octet */ + ++datalen; /* size octet */ if (datalen != len) - ND_PRINT("[len %d]", len); + ND_PRINT("[len %u]", len); ch = ','; if (opt == TCPOPT_EOL) break; @@ -701,8 +719,8 @@ tcp_print(netdissect_options *ndo, #ifdef ENABLE_SMB else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT)) nbt_tcp_print(ndo, bp, length); - else if (IS_SRC_OR_DST_PORT(SMB_PORT)) - smb_tcp_print(ndo, bp, length); + else if (IS_SRC_OR_DST_PORT(SMB_PORT)) + smb_tcp_print(ndo, bp, length); #endif else if (IS_SRC_OR_DST_PORT(BEEP_PORT)) beep_print(ndo, bp, length); @@ -719,6 +737,12 @@ tcp_print(netdissect_options *ndo, rtsp_print(ndo, bp, length); } else if (length > 2 && (IS_SRC_OR_DST_PORT(NAMESERVER_PORT))) { + /* domain_print() assumes it does not have to prepend a space before its + * own output to separate it from the output of the calling function. This + * works well with udp_print(), but requires a small prop here. + */ + ND_PRINT(" "); + /* * TCP DNS query has 2byte length at the head. * XXX packet could be unaligned, it can go strange @@ -769,10 +793,10 @@ tcp_print(netdissect_options *ndo, bad: ND_PRINT("[bad opt]"); if (ch != '\0') - ND_PRINT(">"); + ND_PRINT("]"); return; trunc: - ND_PRINT("[|tcp]"); + nd_print_trunc(ndo); if (ch != '\0') ND_PRINT(">"); } @@ -807,7 +831,7 @@ print_tcp_rst_data(netdissect_options *ndo, while (length-- && sp < ndo->ndo_snapend) { c = EXTRACT_U_1(sp); sp++; - safeputchar(ndo, c); + fn_print_char(ndo, c); } ND_PRINT("]"); } @@ -827,7 +851,7 @@ print_tcp_fastopen_option(netdissect_options *ndo, const u_char *cp, } else { /* Fast Open Cookie */ if (datalen % 2 != 0 || datalen < 4 || datalen > 16) { - ND_PRINT(" invalid"); + nd_print_invalid(ndo); } else { ND_PRINT(" cookie "); for (i = 0; i < datalen; ++i) @@ -841,7 +865,7 @@ USES_APPLE_DEPRECATED_API static int tcp_verify_signature(netdissect_options *ndo, const struct ip *ip, const struct tcphdr *tp, - const u_char *data, int length, const u_char *rcvsig) + const u_char *data, u_int length, const u_char *rcvsig) { struct tcphdr tp1; u_char sig[TCP_SIGLEN]; @@ -852,15 +876,15 @@ tcp_verify_signature(netdissect_options *ndo, uint32_t len32; uint8_t nxt; - if (data + length > ndo->ndo_snapend) { - ND_PRINT("snaplen too short, "); - return (CANT_CHECK_SIGNATURE); - } + if (data + length > ndo->ndo_snapend) { + ND_PRINT("snaplen too short, "); + return (CANT_CHECK_SIGNATURE); + } tp1 = *tp; if (ndo->ndo_sigsecret == NULL) { - ND_PRINT("shared secret not supplied with -M, "); + ND_PRINT("shared secret not supplied with -M, "); return (CANT_CHECK_SIGNATURE); } @@ -889,7 +913,7 @@ tcp_verify_signature(netdissect_options *ndo, nxt = IPPROTO_TCP; MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt)); } else { - ND_PRINT("IP version not 4 or 6, "); + ND_PRINT("IP version not 4 or 6, "); return (CANT_CHECK_SIGNATURE); } @@ -919,10 +943,3 @@ tcp_verify_signature(netdissect_options *ndo, } USES_APPLE_RST #endif /* HAVE_LIBCRYPTO */ - -/* - * Local Variables: - * c-style: whitesmith - * c-basic-offset: 8 - * End: - */