2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
23 static const char rcsid
[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.86 2001-05-09 01:16:57 fenner Exp $ (LBL)";
31 #include <sys/param.h>
36 #include <netinet/in.h>
44 #include "interface.h"
45 #include "addrtoname.h"
57 static void print_tcp_rst_data(register const u_char
*sp
, u_int length
);
59 #define MAX_RST_DATA_LEN 30
74 struct tcp_seq_hash
*nxt
;
80 #define TSEQ_HASHSIZE 919
82 /* These tcp optinos do not have the size octet */
83 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
85 static struct tcp_seq_hash tcp_seq_hash
[TSEQ_HASHSIZE
];
89 #define TELNET_PORT 23
94 #define NETBIOS_SSN_PORT 139
96 #define PPTP_PORT 1723
98 #define BXXP_PORT 10288
100 #define NFS_PORT 2049
103 static int tcp_cksum(register const struct ip
*ip
,
104 register const struct tcphdr
*tp
,
118 register const u_int16_t
*sp
;
120 tlen
= ntohs(ip
->ip_len
) - ((const char *)tp
-(const char*)ip
);
122 /* pseudo-header.. */
123 phu
.ph
.len
= htons(tlen
);
125 phu
.ph
.proto
= IPPROTO_TCP
;
126 memcpy(&phu
.ph
.src
, &ip
->ip_src
.s_addr
, sizeof(u_int32_t
));
127 memcpy(&phu
.ph
.dst
, &ip
->ip_dst
.s_addr
, sizeof(u_int32_t
));
130 sum
= sp
[0]+sp
[1]+sp
[2]+sp
[3]+sp
[4]+sp
[5];
132 sp
= (const u_int16_t
*)tp
;
134 for (i
=0; i
<(tlen
&~1); i
+= 2)
138 sum
+= htons( (*(const u_int8_t
*)sp
) << 8);
142 sum
= (sum
& 0xffff) + (sum
>> 16);
149 static int tcp6_cksum(const struct ip6_hdr
*ip6
, const struct tcphdr
*tp
,
153 register const u_int16_t
*sp
;
157 struct in6_addr ph_src
;
158 struct in6_addr ph_dst
;
166 tlen
= ntohs(ip6
->ip6_plen
) + sizeof(struct ip6_hdr
) -
167 ((const char *)tp
- (const char*)ip6
);
170 memset(&phu
, 0, sizeof(phu
));
171 phu
.ph
.ph_src
= ip6
->ip6_src
;
172 phu
.ph
.ph_dst
= ip6
->ip6_dst
;
173 phu
.ph
.ph_len
= htonl(tlen
);
174 phu
.ph
.ph_nxt
= IPPROTO_TCP
;
177 for (i
= 0; i
< sizeof(phu
.pa
) / sizeof(phu
.pa
[0]); i
++)
180 sp
= (const u_int16_t
*)tp
;
182 for (i
= 0; i
< (tlen
& ~1); i
+= 2)
186 sum
+= htons((*(const u_int8_t
*)sp
) << 8);
189 sum
= (sum
& 0xffff) + (sum
>> 16);
197 tcp_print(register const u_char
*bp
, register u_int length
,
198 register const u_char
*bp2
, int fragmented
)
200 register const struct tcphdr
*tp
;
201 register const struct ip
*ip
;
202 register u_char flags
;
205 u_int16_t sport
, dport
, win
, urp
;
206 u_int32_t seq
, ack
, thseq
, thack
;
209 register const struct ip6_hdr
*ip6
;
212 tp
= (struct tcphdr
*)bp
;
213 ip
= (struct ip
*)bp2
;
216 ip6
= (struct ip6_hdr
*)bp2
;
221 if (!TTEST(tp
->th_dport
)) {
222 (void)printf("%s > %s: [|tcp]",
223 ipaddr_string(&ip
->ip_src
),
224 ipaddr_string(&ip
->ip_dst
));
228 sport
= ntohs(tp
->th_sport
);
229 dport
= ntohs(tp
->th_dport
);
232 hlen
= TH_OFF(tp
) * 4;
235 * If data present and NFS port used, assume NFS.
236 * Pass offset of data plus 4 bytes for RPC TCP msg length
237 * to NFS print routines.
240 if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
) <= snapend
&&
242 nfsreq_print((u_char
*)tp
+ hlen
+ 4, length
- hlen
,
245 } else if ((u_char
*)tp
+ 4 + sizeof(struct rpc_msg
)
248 nfsreply_print((u_char
*)tp
+ hlen
+ 4,length
-hlen
,
255 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
256 (void)printf("%s.%s > %s.%s: ",
257 ip6addr_string(&ip6
->ip6_src
),
258 tcpport_string(sport
),
259 ip6addr_string(&ip6
->ip6_dst
),
260 tcpport_string(dport
));
262 (void)printf("%s > %s: ",
263 tcpport_string(sport
), tcpport_string(dport
));
268 if (ip
->ip_p
== IPPROTO_TCP
) {
269 (void)printf("%s.%s > %s.%s: ",
270 ipaddr_string(&ip
->ip_src
),
271 tcpport_string(sport
),
272 ipaddr_string(&ip
->ip_dst
),
273 tcpport_string(dport
));
275 (void)printf("%s > %s: ",
276 tcpport_string(sport
), tcpport_string(dport
));
282 seq
= (u_int32_t
)ntohl(tp
->th_seq
);
283 ack
= (u_int32_t
)ntohl(tp
->th_ack
);
284 win
= ntohs(tp
->th_win
);
285 urp
= ntohs(tp
->th_urp
);
288 (void)printf("tcp %d", length
- TH_OFF(tp
) * 4);
291 if ((flags
= tp
->th_flags
) & (TH_SYN
|TH_FIN
|TH_RST
|TH_PUSH
|
292 TH_ECNECHO
|TH_CWR
)) {
302 putchar('W'); /* congestion _W_indow reduced (ECN) */
303 if (flags
& TH_ECNECHO
)
304 putchar('E'); /* ecn _E_cho sent (ECN) */
308 if (!Sflag
&& (flags
& TH_ACK
)) {
309 register struct tcp_seq_hash
*th
;
313 * Find (or record) the initial sequence numbers for
314 * this conversation. (we pick an arbitrary
315 * collating order so there's only one entry for
319 memset(&tha
, 0, sizeof(tha
));
324 else if (sport
== dport
) {
327 for (i
= 0; i
< 4; i
++) {
328 if (((u_int32_t
*)(&ip6
->ip6_src
))[i
] >
329 ((u_int32_t
*)(&ip6
->ip6_dst
))[i
]) {
336 tha
.src
= ip6
->ip6_dst
;
337 tha
.dst
= ip6
->ip6_src
;
338 tha
.port
= dport
<< 16 | sport
;
340 tha
.dst
= ip6
->ip6_dst
;
341 tha
.src
= ip6
->ip6_src
;
342 tha
.port
= sport
<< 16 | dport
;
347 ip
->ip_src
.s_addr
> ip
->ip_dst
.s_addr
)) {
351 *(struct in_addr
*)&tha
.src
= ip
->ip_dst
;
352 *(struct in_addr
*)&tha
.dst
= ip
->ip_src
;
353 tha
.port
= dport
<< 16 | sport
;
355 *(struct in_addr
*)&tha
.dst
= ip
->ip_dst
;
356 *(struct in_addr
*)&tha
.src
= ip
->ip_src
;
357 tha
.port
= sport
<< 16 | dport
;
363 ip
->ip_src
.s_addr
< ip
->ip_dst
.s_addr
)) {
364 tha
.src
= ip
->ip_src
, tha
.dst
= ip
->ip_dst
;
365 tha
.port
= sport
<< 16 | dport
;
368 tha
.src
= ip
->ip_dst
, tha
.dst
= ip
->ip_src
;
369 tha
.port
= dport
<< 16 | sport
;
375 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
376 th
->nxt
; th
= th
->nxt
)
377 if (!memcmp((char *)&tha
, (char *)&th
->addr
,
381 if (!th
->nxt
|| (flags
& TH_SYN
)) {
382 /* didn't find it or new conversation */
383 if (th
->nxt
== NULL
) {
384 th
->nxt
= (struct tcp_seq_hash
*)
385 calloc(1, sizeof(*th
));
387 error("tcp_print: calloc");
391 th
->ack
= seq
, th
->seq
= ack
- 1;
393 th
->seq
= seq
, th
->ack
= ack
- 1;
396 seq
-= th
->ack
, ack
-= th
->seq
;
398 seq
-= th
->seq
, ack
-= th
->ack
;
405 thseq
= thack
= threv
= 0;
408 (void)printf(" [bad hdr length]");
412 if (IP_V(ip
) == 4 && vflag
&& !fragmented
) {
414 if (TTEST2(tp
->th_sport
, length
)) {
415 sum
= tcp_cksum(ip
, tp
, length
);
417 (void)printf(" [bad tcp cksum %x!]", sum
);
419 (void)printf(" [tcp sum ok]");
423 if (IP_V(ip
) == 6 && ip6
->ip6_plen
&& vflag
&& !fragmented
) {
425 if (TTEST2(tp
->th_sport
, length
)) {
426 sum
= tcp6_cksum(ip6
, tp
, length
);
428 (void)printf(" [bad tcp cksum %x!]", sum
);
430 (void)printf(" [tcp sum ok]");
436 if (vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
))
437 (void)printf(" %u:%u(%d)", seq
, seq
+ length
, length
);
439 (void)printf(" ack %u", ack
);
441 (void)printf(" win %d", win
);
444 (void)printf(" urg %d", urp
);
446 * Handle any options.
448 if ((hlen
-= sizeof(*tp
)) > 0) {
449 register const u_char
*cp
;
450 register int i
, opt
, len
, datalen
;
452 cp
= (const u_char
*)tp
+ sizeof(*tp
);
463 len
= *cp
++; /* total including type, len */
464 if (len
< 2 || len
> hlen
)
466 --hlen
; /* account for length byte */
468 --hlen
; /* account for type byte */
471 /* Bail if "l" bytes of data are not left or were not captured */
472 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
480 (void)printf(" %u", EXTRACT_16BITS(cp
));
493 (void)printf("wscale");
496 (void)printf(" %u", *cp
);
500 (void)printf("sackOK");
504 (void)printf("sack");
506 if (datalen
% 8 != 0) {
507 (void)printf(" malformed sack ");
511 (void)printf(" sack %d ", datalen
/ 8);
512 for (i
= 0; i
< datalen
; i
+= 8) {
514 s
= EXTRACT_32BITS(cp
+ i
);
516 e
= EXTRACT_32BITS(cp
+ i
+ 4);
524 (void)printf("{%u:%u}", s
, e
);
531 (void)printf("echo");
534 (void)printf(" %u", EXTRACT_32BITS(cp
));
537 case TCPOPT_ECHOREPLY
:
538 (void)printf("echoreply");
541 (void)printf(" %u", EXTRACT_32BITS(cp
));
544 case TCPOPT_TIMESTAMP
:
545 (void)printf("timestamp");
548 (void)printf(" %u", EXTRACT_32BITS(cp
));
550 (void)printf(" %u", EXTRACT_32BITS(cp
+ 4));
557 (void)printf(" %u", EXTRACT_32BITS(cp
));
561 (void)printf("ccnew");
564 (void)printf(" %u", EXTRACT_32BITS(cp
));
568 (void)printf("ccecho");
571 (void)printf(" %u", EXTRACT_32BITS(cp
));
575 (void)printf("opt-%d:", opt
);
577 for (i
= 0; i
< datalen
; ++i
) {
579 (void)printf("%02x", cp
[i
]);
584 /* Account for data printed */
588 /* Check specification against observed length */
589 ++datalen
; /* option octet */
590 if (!ZEROLENOPT(opt
))
591 ++datalen
; /* size octet */
593 (void)printf("[len %d]", len
);
595 if (opt
== TCPOPT_EOL
)
605 * Decode payload if necessary.
607 bp
+= TH_OFF(tp
) * 4;
608 if (flags
& TH_RST
) {
610 print_tcp_rst_data(bp
, length
);
612 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
614 telnet_print(bp
, length
);
615 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
616 bgp_print(bp
, length
);
617 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
618 pptp_print(bp
, length
);
619 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
620 nbt_tcp_print(bp
, length
);
621 else if (sport
== BXXP_PORT
|| dport
== BXXP_PORT
)
622 bxxp_print(bp
, length
);
623 else if (length
> 2 &&
624 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
)) {
625 /* TCP DNS query has 2byte length at the head */
626 ns_print(bp
+ 2, length
- 2);
631 fputs("[bad opt]", stdout
);
636 fputs("[|tcp]", stdout
);
642 * RFC1122 says the following on data in RST segments:
644 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
646 * A TCP SHOULD allow a received RST segment to include data.
649 * It has been suggested that a RST segment could contain
650 * ASCII text that encoded and explained the cause of the
651 * RST. No standard has yet been established for such
657 print_tcp_rst_data(register const u_char
*sp
, u_int length
)
661 if (TTEST2(*sp
, length
))
665 if (length
> MAX_RST_DATA_LEN
) {
666 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
667 putchar('+'); /* indicate we truncate */
670 while (length
-- && sp
<= snapend
) {