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