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