]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
- enable build outside of the tree
[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.57 1999-10-30 05:11:21 itojun Exp $ (LBL)";
25 #endif
26
27 #include <sys/param.h>
28 #include <sys/time.h>
29
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <netinet/ip_var.h>
34 #include <netinet/tcp.h>
35
36 #ifdef HAVE_MEMORY_H
37 #include <memory.h>
38 #endif
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <unistd.h>
43
44 #ifdef INET6
45 #include <netinet/ip6.h>
46 #endif
47
48 #include "interface.h"
49 #include "addrtoname.h"
50 #include "extract.h"
51
52 /* Compatibility */
53 #ifndef TCPOPT_WSCALE
54 #define TCPOPT_WSCALE 3 /* window scale factor (rfc1072) */
55 #endif
56 #ifndef TCPOPT_SACKOK
57 #define TCPOPT_SACKOK 4 /* selective ack ok (rfc1072) */
58 #endif
59 #ifndef TCPOPT_SACK
60 #define TCPOPT_SACK 5 /* selective ack (rfc1072) */
61 #endif
62 #ifndef TCPOPT_ECHO
63 #define TCPOPT_ECHO 6 /* echo (rfc1072) */
64 #endif
65 #ifndef TCPOPT_ECHOREPLY
66 #define TCPOPT_ECHOREPLY 7 /* echo (rfc1072) */
67 #endif
68 #ifndef TCPOPT_TIMESTAMP
69 #define TCPOPT_TIMESTAMP 8 /* timestamps (rfc1323) */
70 #endif
71 #ifndef TCPOPT_CC
72 #define TCPOPT_CC 11 /* T/TCP CC options (rfc1644) */
73 #endif
74 #ifndef TCPOPT_CCNEW
75 #define TCPOPT_CCNEW 12 /* T/TCP CC options (rfc1644) */
76 #endif
77 #ifndef TCPOPT_CCECHO
78 #define TCPOPT_CCECHO 13 /* T/TCP CC options (rfc1644) */
79 #endif
80
81 struct tha {
82 #ifndef INET6
83 struct in_addr src;
84 struct in_addr dst;
85 #else
86 struct in6_addr src;
87 struct in6_addr dst;
88 #endif /*INET6*/
89 u_int port;
90 };
91
92 struct tcp_seq_hash {
93 struct tcp_seq_hash *nxt;
94 struct tha addr;
95 tcp_seq seq;
96 tcp_seq ack;
97 };
98
99 #define TSEQ_HASHSIZE 919
100
101 /* These tcp optinos do not have the size octet */
102 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
103
104 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
105
106
107 void
108 tcp_print(register const u_char *bp, register u_int length,
109 register const u_char *bp2)
110 {
111 register const struct tcphdr *tp;
112 register const struct ip *ip;
113 register u_char flags;
114 register int hlen;
115 register char ch;
116 u_short sport, dport, win, urp;
117 u_int32_t seq, ack;
118 #ifdef INET6
119 register const struct ip6_hdr *ip6;
120 #endif
121
122 tp = (struct tcphdr *)bp;
123 ip = (struct ip *)bp2;
124 #ifdef INET6
125 if (ip->ip_v == 6)
126 ip6 = (struct ip6_hdr *)bp2;
127 else
128 ip6 = NULL;
129 #endif /*INET6*/
130 ch = '\0';
131 TCHECK(*tp);
132 if (length < sizeof(*tp)) {
133 (void)printf("truncated-tcp %d", length);
134 return;
135 }
136
137 sport = ntohs(tp->th_sport);
138 dport = ntohs(tp->th_dport);
139 seq = ntohl(tp->th_seq);
140 ack = ntohl(tp->th_ack);
141 win = ntohs(tp->th_win);
142 urp = ntohs(tp->th_urp);
143
144 #ifdef INET6
145 if (ip6) {
146 if (ip6->ip6_nxt == IPPROTO_TCP) {
147 (void)printf("%s.%s > %s.%s: ",
148 ip6addr_string(&ip6->ip6_src),
149 tcpport_string(sport),
150 ip6addr_string(&ip6->ip6_dst),
151 tcpport_string(dport));
152 } else {
153 (void)printf("%s > %s: ",
154 tcpport_string(sport), tcpport_string(dport));
155 }
156 } else
157 #endif /*INET6*/
158 {
159 if (ip->ip_p == IPPROTO_TCP) {
160 (void)printf("%s.%s > %s.%s: ",
161 ipaddr_string(&ip->ip_src),
162 tcpport_string(sport),
163 ipaddr_string(&ip->ip_dst),
164 tcpport_string(dport));
165 } else {
166 (void)printf("%s > %s: ",
167 tcpport_string(sport), tcpport_string(dport));
168 }
169 }
170
171 if (qflag) {
172 (void)printf("tcp %d", length - tp->th_off * 4);
173 return;
174 }
175 #ifdef TH_ECN
176 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|TH_ECN)) {
177 #else
178 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH)) {
179 #endif
180 if (flags & TH_SYN)
181 putchar('S');
182 if (flags & TH_FIN)
183 putchar('F');
184 if (flags & TH_RST)
185 putchar('R');
186 if (flags & TH_PUSH)
187 putchar('P');
188 #ifdef TH_ECN
189 if (flags & TH_ECN)
190 putchar('C');
191 #endif
192 } else
193 putchar('.');
194
195 if (!Sflag && (flags & TH_ACK)) {
196 register struct tcp_seq_hash *th;
197 register int rev;
198 struct tha tha;
199 /*
200 * Find (or record) the initial sequence numbers for
201 * this conversation. (we pick an arbitrary
202 * collating order so there's only one entry for
203 * both directions).
204 */
205 #ifdef INET6
206 bzero(&tha, sizeof(tha));
207 rev = 0;
208 if (ip6) {
209 if (sport > dport) {
210 rev = 1;
211 } else if (sport == dport) {
212 int i;
213
214 for (i = 0; i < 4; i++) {
215 if (((u_int32_t *)(&ip6->ip6_src))[i] >
216 ((u_int32_t *)(&ip6->ip6_dst))[i]) {
217 rev = 1;
218 break;
219 }
220 }
221 }
222 if (rev) {
223 tha.src = ip6->ip6_dst;
224 tha.dst = ip6->ip6_src;
225 tha.port = dport << 16 | sport;
226 } else {
227 tha.dst = ip6->ip6_dst;
228 tha.src = ip6->ip6_src;
229 tha.port = sport << 16 | dport;
230 }
231 } else {
232 if (sport > dport ||
233 (sport == dport &&
234 ip->ip_src.s_addr > ip->ip_dst.s_addr)) {
235 rev = 1;
236 }
237 if (rev) {
238 *(struct in_addr *)&tha.src = ip->ip_dst;
239 *(struct in_addr *)&tha.dst = ip->ip_src;
240 tha.port = dport << 16 | sport;
241 } else {
242 *(struct in_addr *)&tha.dst = ip->ip_dst;
243 *(struct in_addr *)&tha.src = ip->ip_src;
244 tha.port = sport << 16 | dport;
245 }
246 }
247 #else
248 if (sport < dport ||
249 (sport == dport &&
250 ip->ip_src.s_addr < ip->ip_dst.s_addr)) {
251 tha.src = ip->ip_src, tha.dst = ip->ip_dst;
252 tha.port = sport << 16 | dport;
253 rev = 0;
254 } else {
255 tha.src = ip->ip_dst, tha.dst = ip->ip_src;
256 tha.port = dport << 16 | sport;
257 rev = 1;
258 }
259 #endif
260
261 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
262 th->nxt; th = th->nxt)
263 if (!memcmp((char *)&tha, (char *)&th->addr,
264 sizeof(th->addr)))
265 break;
266
267 if (!th->nxt || flags & TH_SYN) {
268 /* didn't find it or new conversation */
269 if (th->nxt == NULL) {
270 th->nxt = (struct tcp_seq_hash *)
271 calloc(1, sizeof(*th));
272 if (th->nxt == NULL)
273 error("tcp_print: calloc");
274 }
275 th->addr = tha;
276 if (rev)
277 th->ack = seq, th->seq = ack - 1;
278 else
279 th->seq = seq, th->ack = ack - 1;
280 } else {
281 if (rev)
282 seq -= th->ack, ack -= th->seq;
283 else
284 seq -= th->seq, ack -= th->ack;
285 }
286 }
287 hlen = tp->th_off * 4;
288 if (hlen > length) {
289 (void)printf(" [bad hdr length]");
290 return;
291 }
292 length -= hlen;
293 if (length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
294 (void)printf(" %u:%u(%d)", seq, seq + length, length);
295 if (flags & TH_ACK)
296 (void)printf(" ack %u", ack);
297
298 (void)printf(" win %d", win);
299
300 if (flags & TH_URG)
301 (void)printf(" urg %d", urp);
302 /*
303 * Handle any options.
304 */
305 if ((hlen -= sizeof(*tp)) > 0) {
306 register const u_char *cp;
307 register int i, opt, len, datalen;
308
309 cp = (const u_char *)tp + sizeof(*tp);
310 putchar(' ');
311 ch = '<';
312 while (hlen > 0) {
313 putchar(ch);
314 TCHECK(*cp);
315 opt = *cp++;
316 if (ZEROLENOPT(opt))
317 len = 1;
318 else {
319 TCHECK(*cp);
320 len = *cp++; /* total including type, len */
321 if (len < 2 || len > hlen)
322 goto bad;
323 --hlen; /* account for length byte */
324 }
325 --hlen; /* account for type byte */
326 datalen = 0;
327
328 /* Bail if "l" bytes of data are not left or were not captured */
329 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
330
331 switch (opt) {
332
333 case TCPOPT_MAXSEG:
334 (void)printf("mss");
335 datalen = 2;
336 LENCHECK(datalen);
337 (void)printf(" %u", EXTRACT_16BITS(cp));
338
339 break;
340
341 case TCPOPT_EOL:
342 (void)printf("eol");
343 break;
344
345 case TCPOPT_NOP:
346 (void)printf("nop");
347 break;
348
349 case TCPOPT_WSCALE:
350 (void)printf("wscale");
351 datalen = 1;
352 LENCHECK(datalen);
353 (void)printf(" %u", *cp);
354 break;
355
356 case TCPOPT_SACKOK:
357 (void)printf("sackOK");
358 break;
359
360 case TCPOPT_SACK:
361 (void)printf("sack");
362 datalen = len - 2;
363 for (i = 0; i < datalen; i += 4) {
364 LENCHECK(i + 4);
365 /* block-size@relative-origin */
366 (void)printf(" %u@%u",
367 EXTRACT_16BITS(cp + i + 2),
368 EXTRACT_16BITS(cp + i));
369 }
370 if (datalen % 4)
371 (void)printf("[len %d]", len);
372 break;
373
374 case TCPOPT_ECHO:
375 (void)printf("echo");
376 datalen = 4;
377 LENCHECK(datalen);
378 (void)printf(" %u", EXTRACT_32BITS(cp));
379 break;
380
381 case TCPOPT_ECHOREPLY:
382 (void)printf("echoreply");
383 datalen = 4;
384 LENCHECK(datalen);
385 (void)printf(" %u", EXTRACT_32BITS(cp));
386 break;
387
388 case TCPOPT_TIMESTAMP:
389 (void)printf("timestamp");
390 datalen = 8;
391 LENCHECK(4);
392 (void)printf(" %u", EXTRACT_32BITS(cp));
393 LENCHECK(datalen);
394 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
395 break;
396
397 case TCPOPT_CC:
398 (void)printf("cc");
399 datalen = 4;
400 LENCHECK(datalen);
401 (void)printf(" %u", EXTRACT_32BITS(cp));
402 break;
403
404 case TCPOPT_CCNEW:
405 (void)printf("ccnew");
406 datalen = 4;
407 LENCHECK(datalen);
408 (void)printf(" %u", EXTRACT_32BITS(cp));
409 break;
410
411 case TCPOPT_CCECHO:
412 (void)printf("ccecho");
413 datalen = 4;
414 LENCHECK(datalen);
415 (void)printf(" %u", EXTRACT_32BITS(cp));
416 break;
417
418 default:
419 (void)printf("opt-%d:", opt);
420 datalen = len - 2;
421 for (i = 0; i < datalen; ++i) {
422 LENCHECK(i);
423 (void)printf("%02x", cp[i]);
424 }
425 break;
426 }
427
428 /* Account for data printed */
429 cp += datalen;
430 hlen -= datalen;
431
432 /* Check specification against observed length */
433 ++datalen; /* option octet */
434 if (!ZEROLENOPT(opt))
435 ++datalen; /* size octet */
436 if (datalen != len)
437 (void)printf("[len %d]", len);
438 ch = ',';
439 if (opt == TCPOPT_EOL)
440 break;
441 }
442 putchar('>');
443 }
444
445 if (length <= 0)
446 return;
447
448 /*
449 * Decode payload if necessary.
450 */
451 bp += (tp->th_off * 4);
452 if (sport == 179 || dport == 179)
453 bgp_print(bp, length);
454 return;
455 bad:
456 fputs("[bad opt]", stdout);
457 if (ch != '\0')
458 putchar('>');
459 return;
460 trunc:
461 fputs("[|tcp]", stdout);
462 if (ch != '\0')
463 putchar('>');
464 }
465