]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
d45a295779f61c3d63930e163397ca03e3d04c50
[tcpdump] / print-tcp.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
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
16 * written permission.
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.
20 */
21
22 #ifndef lint
23 static const char rcsid[] =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.85 2001-03-09 05:38:21 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <sys/param.h>
32 #include <sys/time.h>
33
34 #include <rpc/rpc.h>
35
36 #include <netinet/in.h>
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <ctype.h>
42 #include <unistd.h>
43
44 #include "interface.h"
45 #include "addrtoname.h"
46 #include "extract.h"
47
48 #include "tcp.h"
49
50 #include "ip.h"
51 #ifdef INET6
52 #include "ip6.h"
53 #endif
54
55 #include "nameser.h"
56
57 static void print_tcp_rst_data(register const u_char *sp, u_int length);
58
59 #define MAX_RST_DATA_LEN 30
60
61 /* Compatibility */
62 #ifndef TCPOPT_WSCALE
63 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
64 #endif
65 #ifndef TCPOPT_SACKOK
66 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
67 #endif
68 #ifndef TCPOPT_SACK
69 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
70 #endif
71 #ifndef TCPOPT_ECHO
72 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
73 #endif
74 #ifndef TCPOPT_ECHOREPLY
75 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
76 #endif
77 #ifndef TCPOPT_TIMESTAMP
78 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
79 #endif
80 #ifndef TCPOPT_CC
81 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
82 #endif
83 #ifndef TCPOPT_CCNEW
84 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
85 #endif
86 #ifndef TCPOPT_CCECHO
87 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
88 #endif
89
90 /*
91 * Definitions required for ECN
92 * for use if the OS running tcpdump does not have ECN
93 */
94 #ifndef TH_ECNECHO
95 #define TH_ECNECHO 0x40 /* ECN Echo in tcp header */
96 #endif
97 #ifndef TH_CWR
98 #define TH_CWR 0x80 /* ECN Cwnd Reduced in tcp header*/
99 #endif
100
101 struct tha {
102 #ifndef INET6
103 struct in_addr src;
104 struct in_addr dst;
105 #else
106 struct in6_addr src;
107 struct in6_addr dst;
108 #endif /*INET6*/
109 u_int port;
110 };
111
112 struct tcp_seq_hash {
113 struct tcp_seq_hash *nxt;
114 struct tha addr;
115 tcp_seq seq;
116 tcp_seq ack;
117 };
118
119 #define TSEQ_HASHSIZE 919
120
121 /* These tcp optinos do not have the size octet */
122 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
123
124 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
125
126
127 #ifndef TELNET_PORT
128 #define TELNET_PORT 23
129 #endif
130 #ifndef BGP_PORT
131 #define BGP_PORT 179
132 #endif
133 #define NETBIOS_SSN_PORT 139
134 #ifndef PPTP_PORT
135 #define PPTP_PORT 1723
136 #endif
137 #define BXXP_PORT 10288
138 #ifndef NFS_PORT
139 #define NFS_PORT 2049
140 #endif
141
142 static int tcp_cksum(register const struct ip *ip,
143 register const struct tcphdr *tp,
144 register int len)
145 {
146 int i, tlen;
147 union phu {
148 struct phdr {
149 u_int32_t src;
150 u_int32_t dst;
151 u_char mbz;
152 u_char proto;
153 u_int16_t len;
154 } ph;
155 u_int16_t pa[6];
156 } phu;
157 register const u_int16_t *sp;
158 u_int32_t sum;
159 tlen = ntohs(ip->ip_len) - ((const char *)tp-(const char*)ip);
160
161 /* pseudo-header.. */
162 phu.ph.len = htons(tlen);
163 phu.ph.mbz = 0;
164 phu.ph.proto = IPPROTO_TCP;
165 memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
166 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
167
168 sp = &phu.pa[0];
169 sum = sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5];
170
171 sp = (const u_int16_t *)tp;
172
173 for (i=0; i<(tlen&~1); i+= 2)
174 sum += *sp++;
175
176 if (tlen & 1) {
177 sum += htons( (*(const u_int8_t *)sp) << 8);
178 }
179
180 while (sum > 0xffff)
181 sum = (sum & 0xffff) + (sum >> 16);
182 sum = ~sum & 0xffff;
183
184 return (sum);
185 }
186
187 #ifdef INET6
188 static int tcp6_cksum(const struct ip6_hdr *ip6, const struct tcphdr *tp,
189 int len)
190 {
191 int i, tlen;
192 register const u_int16_t *sp;
193 u_int32_t sum;
194 union {
195 struct {
196 struct in6_addr ph_src;
197 struct in6_addr ph_dst;
198 u_int32_t ph_len;
199 u_int8_t ph_zero[3];
200 u_int8_t ph_nxt;
201 } ph;
202 u_int16_t pa[20];
203 } phu;
204
205 tlen = ntohs(ip6->ip6_plen) + sizeof(struct ip6_hdr) -
206 ((const char *)tp - (const char*)ip6);
207
208 /* pseudo-header */
209 memset(&phu, 0, sizeof(phu));
210 phu.ph.ph_src = ip6->ip6_src;
211 phu.ph.ph_dst = ip6->ip6_dst;
212 phu.ph.ph_len = htonl(tlen);
213 phu.ph.ph_nxt = IPPROTO_TCP;
214
215 sum = 0;
216 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
217 sum += phu.pa[i];
218
219 sp = (const u_int16_t *)tp;
220
221 for (i = 0; i < (tlen & ~1); i += 2)
222 sum += *sp++;
223
224 if (tlen & 1)
225 sum += htons((*(const u_int8_t *)sp) << 8);
226
227 while (sum > 0xffff)
228 sum = (sum & 0xffff) + (sum >> 16);
229 sum = ~sum & 0xffff;
230
231 return (sum);
232 }
233 #endif
234
235 void
236 tcp_print(register const u_char *bp, register u_int length,
237 register const u_char *bp2, int fragmented)
238 {
239 register const struct tcphdr *tp;
240 register const struct ip *ip;
241 register u_char flags;
242 register int hlen;
243 register char ch;
244 u_int16_t sport, dport, win, urp;
245 u_int32_t seq, ack, thseq, thack;
246 int threv;
247 #ifdef INET6
248 register const struct ip6_hdr *ip6;
249 #endif
250
251 tp = (struct tcphdr *)bp;
252 ip = (struct ip *)bp2;
253 #ifdef INET6
254 if (IP_V(ip) == 6)
255 ip6 = (struct ip6_hdr *)bp2;
256 else
257 ip6 = NULL;
258 #endif /*INET6*/
259 ch = '\0';
260 if (!TTEST(tp->th_dport)) {
261 (void)printf("%s > %s: [|tcp]",
262 ipaddr_string(&ip->ip_src),
263 ipaddr_string(&ip->ip_dst));
264 return;
265 }
266
267 sport = ntohs(tp->th_sport);
268 dport = ntohs(tp->th_dport);
269
270
271 hlen = TH_OFF(tp) * 4;
272
273 /*
274 * If data present and NFS port used, assume NFS.
275 * Pass offset of data plus 4 bytes for RPC TCP msg length
276 * to NFS print routines.
277 */
278 if (!qflag) {
279 if ((u_char *)tp + 4 + sizeof(struct rpc_msg) <= snapend &&
280 dport == NFS_PORT) {
281 nfsreq_print((u_char *)tp + hlen + 4, length - hlen,
282 (u_char *)ip);
283 return;
284 } else if ((u_char *)tp + 4 + sizeof(struct rpc_msg)
285 <= snapend &&
286 sport == NFS_PORT) {
287 nfsreply_print((u_char *)tp + hlen + 4,length-hlen,
288 (u_char *)ip);
289 return;
290 }
291 }
292 #ifdef INET6
293 if (ip6) {
294 if (ip6->ip6_nxt == IPPROTO_TCP) {
295 (void)printf("%s.%s > %s.%s: ",
296 ip6addr_string(&ip6->ip6_src),
297 tcpport_string(sport),
298 ip6addr_string(&ip6->ip6_dst),
299 tcpport_string(dport));
300 } else {
301 (void)printf("%s > %s: ",
302 tcpport_string(sport), tcpport_string(dport));
303 }
304 } else
305 #endif /*INET6*/
306 {
307 if (ip->ip_p == IPPROTO_TCP) {
308 (void)printf("%s.%s > %s.%s: ",
309 ipaddr_string(&ip->ip_src),
310 tcpport_string(sport),
311 ipaddr_string(&ip->ip_dst),
312 tcpport_string(dport));
313 } else {
314 (void)printf("%s > %s: ",
315 tcpport_string(sport), tcpport_string(dport));
316 }
317 }
318
319 TCHECK(*tp);
320
321 seq = (u_int32_t)ntohl(tp->th_seq);
322 ack = (u_int32_t)ntohl(tp->th_ack);
323 win = ntohs(tp->th_win);
324 urp = ntohs(tp->th_urp);
325
326 if (qflag) {
327 (void)printf("tcp %d", length - TH_OFF(tp) * 4);
328 return;
329 }
330 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|
331 TH_ECNECHO|TH_CWR)) {
332 if (flags & TH_SYN)
333 putchar('S');
334 if (flags & TH_FIN)
335 putchar('F');
336 if (flags & TH_RST)
337 putchar('R');
338 if (flags & TH_PUSH)
339 putchar('P');
340 if (flags & TH_CWR)
341 putchar('W'); /* congestion _W_indow reduced (ECN) */
342 if (flags & TH_ECNECHO)
343 putchar('E'); /* ecn _E_cho sent (ECN) */
344 } else
345 putchar('.');
346
347 if (!Sflag && (flags & TH_ACK)) {
348 register struct tcp_seq_hash *th;
349 register int rev;
350 struct tha tha;
351 /*
352 * Find (or record) the initial sequence numbers for
353 * this conversation. (we pick an arbitrary
354 * collating order so there's only one entry for
355 * both directions).
356 */
357 #ifdef INET6
358 memset(&tha, 0, sizeof(tha));
359 rev = 0;
360 if (ip6) {
361 if (sport > dport)
362 rev = 1;
363 else if (sport == dport) {
364 int i;
365
366 for (i = 0; i < 4; i++) {
367 if (((u_int32_t *)(&ip6->ip6_src))[i] >
368 ((u_int32_t *)(&ip6->ip6_dst))[i]) {
369 rev = 1;
370 break;
371 }
372 }
373 }
374 if (rev) {
375 tha.src = ip6->ip6_dst;
376 tha.dst = ip6->ip6_src;
377 tha.port = dport << 16 | sport;
378 } else {
379 tha.dst = ip6->ip6_dst;
380 tha.src = ip6->ip6_src;
381 tha.port = sport << 16 | dport;
382 }
383 } else {
384 if (sport > dport ||
385 (sport == dport &&
386 ip->ip_src.s_addr > ip->ip_dst.s_addr)) {
387 rev = 1;
388 }
389 if (rev) {
390 *(struct in_addr *)&tha.src = ip->ip_dst;
391 *(struct in_addr *)&tha.dst = ip->ip_src;
392 tha.port = dport << 16 | sport;
393 } else {
394 *(struct in_addr *)&tha.dst = ip->ip_dst;
395 *(struct in_addr *)&tha.src = ip->ip_src;
396 tha.port = sport << 16 | dport;
397 }
398 }
399 #else
400 if (sport < dport ||
401 (sport == dport &&
402 ip->ip_src.s_addr < ip->ip_dst.s_addr)) {
403 tha.src = ip->ip_src, tha.dst = ip->ip_dst;
404 tha.port = sport << 16 | dport;
405 rev = 0;
406 } else {
407 tha.src = ip->ip_dst, tha.dst = ip->ip_src;
408 tha.port = dport << 16 | sport;
409 rev = 1;
410 }
411 #endif
412
413 threv = rev;
414 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
415 th->nxt; th = th->nxt)
416 if (!memcmp((char *)&tha, (char *)&th->addr,
417 sizeof(th->addr)))
418 break;
419
420 if (!th->nxt || (flags & TH_SYN)) {
421 /* didn't find it or new conversation */
422 if (th->nxt == NULL) {
423 th->nxt = (struct tcp_seq_hash *)
424 calloc(1, sizeof(*th));
425 if (th->nxt == NULL)
426 error("tcp_print: calloc");
427 }
428 th->addr = tha;
429 if (rev)
430 th->ack = seq, th->seq = ack - 1;
431 else
432 th->seq = seq, th->ack = ack - 1;
433 } else {
434 if (rev)
435 seq -= th->ack, ack -= th->seq;
436 else
437 seq -= th->seq, ack -= th->ack;
438 }
439
440 thseq = th->seq;
441 thack = th->ack;
442 } else {
443 /*fool gcc*/
444 thseq = thack = threv = 0;
445 }
446 if (hlen > length) {
447 (void)printf(" [bad hdr length]");
448 return;
449 }
450
451 if (IP_V(ip) == 4 && vflag && !fragmented) {
452 int sum;
453 if (TTEST2(tp->th_sport, length)) {
454 sum = tcp_cksum(ip, tp, length);
455 if (sum != 0)
456 (void)printf(" [bad tcp cksum %x!]", sum);
457 else
458 (void)printf(" [tcp sum ok]");
459 }
460 }
461 #ifdef INET6
462 if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !fragmented) {
463 int sum;
464 if (TTEST2(tp->th_sport, length)) {
465 sum = tcp6_cksum(ip6, tp, length);
466 if (sum != 0)
467 (void)printf(" [bad tcp cksum %x!]", sum);
468 else
469 (void)printf(" [tcp sum ok]");
470 }
471 }
472 #endif
473
474 length -= hlen;
475 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
476 (void)printf(" %u:%u(%d)", seq, seq + length, length);
477 if (flags & TH_ACK)
478 (void)printf(" ack %u", ack);
479
480 (void)printf(" win %d", win);
481
482 if (flags & TH_URG)
483 (void)printf(" urg %d", urp);
484 /*
485 * Handle any options.
486 */
487 if ((hlen -= sizeof(*tp)) > 0) {
488 register const u_char *cp;
489 register int i, opt, len, datalen;
490
491 cp = (const u_char *)tp + sizeof(*tp);
492 putchar(' ');
493 ch = '<';
494 while (hlen > 0) {
495 putchar(ch);
496 TCHECK(*cp);
497 opt = *cp++;
498 if (ZEROLENOPT(opt))
499 len = 1;
500 else {
501 TCHECK(*cp);
502 len = *cp++; /* total including type, len */
503 if (len < 2 || len > hlen)
504 goto bad;
505 --hlen; /* account for length byte */
506 }
507 --hlen; /* account for type byte */
508 datalen = 0;
509
510 /* Bail if "l" bytes of data are not left or were not captured */
511 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
512
513 switch (opt) {
514
515 case TCPOPT_MAXSEG:
516 (void)printf("mss");
517 datalen = 2;
518 LENCHECK(datalen);
519 (void)printf(" %u", EXTRACT_16BITS(cp));
520
521 break;
522
523 case TCPOPT_EOL:
524 (void)printf("eol");
525 break;
526
527 case TCPOPT_NOP:
528 (void)printf("nop");
529 break;
530
531 case TCPOPT_WSCALE:
532 (void)printf("wscale");
533 datalen = 1;
534 LENCHECK(datalen);
535 (void)printf(" %u", *cp);
536 break;
537
538 case TCPOPT_SACKOK:
539 (void)printf("sackOK");
540 break;
541
542 case TCPOPT_SACK:
543 (void)printf("sack");
544 datalen = len - 2;
545 if (datalen % 8 != 0) {
546 (void)printf(" malformed sack ");
547 } else {
548 u_int32_t s, e;
549
550 (void)printf(" sack %d ", datalen / 8);
551 for (i = 0; i < datalen; i += 8) {
552 LENCHECK(i + 4);
553 s = EXTRACT_32BITS(cp + i);
554 LENCHECK(i + 8);
555 e = EXTRACT_32BITS(cp + i + 4);
556 if (threv) {
557 s -= thseq;
558 e -= thseq;
559 } else {
560 s -= thack;
561 e -= thack;
562 }
563 (void)printf("{%u:%u}", s, e);
564 }
565 (void)printf(" ");
566 }
567 break;
568
569 case TCPOPT_ECHO:
570 (void)printf("echo");
571 datalen = 4;
572 LENCHECK(datalen);
573 (void)printf(" %u", EXTRACT_32BITS(cp));
574 break;
575
576 case TCPOPT_ECHOREPLY:
577 (void)printf("echoreply");
578 datalen = 4;
579 LENCHECK(datalen);
580 (void)printf(" %u", EXTRACT_32BITS(cp));
581 break;
582
583 case TCPOPT_TIMESTAMP:
584 (void)printf("timestamp");
585 datalen = 8;
586 LENCHECK(4);
587 (void)printf(" %u", EXTRACT_32BITS(cp));
588 LENCHECK(datalen);
589 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
590 break;
591
592 case TCPOPT_CC:
593 (void)printf("cc");
594 datalen = 4;
595 LENCHECK(datalen);
596 (void)printf(" %u", EXTRACT_32BITS(cp));
597 break;
598
599 case TCPOPT_CCNEW:
600 (void)printf("ccnew");
601 datalen = 4;
602 LENCHECK(datalen);
603 (void)printf(" %u", EXTRACT_32BITS(cp));
604 break;
605
606 case TCPOPT_CCECHO:
607 (void)printf("ccecho");
608 datalen = 4;
609 LENCHECK(datalen);
610 (void)printf(" %u", EXTRACT_32BITS(cp));
611 break;
612
613 default:
614 (void)printf("opt-%d:", opt);
615 datalen = len - 2;
616 for (i = 0; i < datalen; ++i) {
617 LENCHECK(i);
618 (void)printf("%02x", cp[i]);
619 }
620 break;
621 }
622
623 /* Account for data printed */
624 cp += datalen;
625 hlen -= datalen;
626
627 /* Check specification against observed length */
628 ++datalen; /* option octet */
629 if (!ZEROLENOPT(opt))
630 ++datalen; /* size octet */
631 if (datalen != len)
632 (void)printf("[len %d]", len);
633 ch = ',';
634 if (opt == TCPOPT_EOL)
635 break;
636 }
637 putchar('>');
638 }
639
640 if (length <= 0)
641 return;
642
643 /*
644 * Decode payload if necessary.
645 */
646 bp += TH_OFF(tp) * 4;
647 if (flags & TH_RST) {
648 if (vflag)
649 print_tcp_rst_data(bp, length);
650 } else {
651 if (sport == TELNET_PORT || dport == TELNET_PORT) {
652 if (!qflag && vflag)
653 telnet_print(bp, length);
654 } else if (sport == BGP_PORT || dport == BGP_PORT)
655 bgp_print(bp, length);
656 else if (sport == PPTP_PORT || dport == PPTP_PORT)
657 pptp_print(bp, length);
658 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
659 nbt_tcp_print(bp, length);
660 else if (sport == BXXP_PORT || dport == BXXP_PORT)
661 bxxp_print(bp, length);
662 else if (length > 2 &&
663 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT)) {
664 /* TCP DNS query has 2byte length at the head */
665 ns_print(bp + 2, length - 2);
666 }
667 }
668 return;
669 bad:
670 fputs("[bad opt]", stdout);
671 if (ch != '\0')
672 putchar('>');
673 return;
674 trunc:
675 fputs("[|tcp]", stdout);
676 if (ch != '\0')
677 putchar('>');
678 }
679
680 /*
681 * RFC1122 says the following on data in RST segments:
682 *
683 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
684 *
685 * A TCP SHOULD allow a received RST segment to include data.
686 *
687 * DISCUSSION
688 * It has been suggested that a RST segment could contain
689 * ASCII text that encoded and explained the cause of the
690 * RST. No standard has yet been established for such
691 * data.
692 *
693 */
694
695 static void
696 print_tcp_rst_data(register const u_char *sp, u_int length)
697 {
698 int c;
699
700 if (TTEST2(*sp, length))
701 printf(" [RST");
702 else
703 printf(" [!RST");
704 if (length > MAX_RST_DATA_LEN) {
705 length = MAX_RST_DATA_LEN; /* can use -X for longer */
706 putchar('+'); /* indicate we truncate */
707 }
708 putchar(' ');
709 while (length-- && sp <= snapend) {
710 c = *sp++;
711 safeputchar(c);
712 }
713 putchar(']');
714 }