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