]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
TCP: Add support for the AE (AccECN) flag.
[tcpdump] / print-tcp.c
1 /* $NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky Exp $ */
2
3 /*
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Copyright (c) 1999-2004 The tcpdump.org project
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that: (1) source code distributions
11 * retain the above copyright notice and this paragraph in its entirety, (2)
12 * distributions including binary code include the above copyright notice and
13 * this paragraph in its entirety in the documentation or other materials
14 * provided with the distribution, and (3) all advertising materials mentioning
15 * features or use of this software display the following acknowledgement:
16 * ``This product includes software developed by the University of California,
17 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
18 * the University nor the names of its contributors may be used to endorse
19 * or promote products derived from this software without specific prior
20 * written permission.
21 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
24 */
25
26 /* \summary: TCP printer */
27
28 #ifndef lint
29 #else
30 __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
31 #endif
32
33 #ifdef HAVE_CONFIG_H
34 #include <config.h>
35 #endif
36
37 #include "netdissect-stdinc.h"
38
39 #include <stdlib.h>
40 #include <string.h>
41
42 #define ND_LONGJMP_FROM_TCHECK
43 #include "netdissect.h"
44 #include "addrtoname.h"
45 #include "extract.h"
46
47 #include "diag-control.h"
48
49 #include "tcp.h"
50
51 #include "ip.h"
52 #include "ip6.h"
53 #include "ipproto.h"
54 #include "rpc_auth.h"
55 #include "rpc_msg.h"
56
57 #ifdef HAVE_LIBCRYPTO
58 #include <openssl/md5.h>
59 #include "signature.h"
60
61 static int tcp_verify_signature(netdissect_options *ndo,
62 const struct ip *ip, const struct tcphdr *tp,
63 const u_char *data, u_int length, const u_char *rcvsig);
64 #endif
65
66 static void print_tcp_rst_data(netdissect_options *, const u_char *sp, u_int length);
67 static void print_tcp_fastopen_option(netdissect_options *ndo, const u_char *cp,
68 u_int datalen);
69
70 #define MAX_RST_DATA_LEN 30
71
72
73 struct tha {
74 nd_ipv4 src;
75 nd_ipv4 dst;
76 u_int port;
77 };
78
79 struct tcp_seq_hash {
80 struct tcp_seq_hash *nxt;
81 struct tha addr;
82 uint32_t seq;
83 uint32_t ack;
84 };
85
86 struct tha6 {
87 nd_ipv6 src;
88 nd_ipv6 dst;
89 u_int port;
90 };
91
92 struct tcp_seq_hash6 {
93 struct tcp_seq_hash6 *nxt;
94 struct tha6 addr;
95 uint32_t seq;
96 uint32_t ack;
97 };
98
99 #define TSEQ_HASHSIZE 919
100
101 /* These tcp options 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_hash4[TSEQ_HASHSIZE];
105 static struct tcp_seq_hash6 tcp_seq_hash6[TSEQ_HASHSIZE];
106
107 const struct tok tcp_flag_values[] = {
108 { TH_FIN, "F" },
109 { TH_SYN, "S" },
110 { TH_RST, "R" },
111 { TH_PUSH, "P" },
112 { TH_ACK, "." },
113 { TH_URG, "U" },
114 { TH_ECNECHO, "E" },
115 { TH_CWR, "W" },
116 { TH_AE, "e" },
117 { 0, NULL }
118 };
119
120 static const struct tok tcp_option_values[] = {
121 { TCPOPT_EOL, "eol" },
122 { TCPOPT_NOP, "nop" },
123 { TCPOPT_MAXSEG, "mss" },
124 { TCPOPT_WSCALE, "wscale" },
125 { TCPOPT_SACKOK, "sackOK" },
126 { TCPOPT_SACK, "sack" },
127 { TCPOPT_ECHO, "echo" },
128 { TCPOPT_ECHOREPLY, "echoreply" },
129 { TCPOPT_TIMESTAMP, "TS" },
130 { TCPOPT_CC, "cc" },
131 { TCPOPT_CCNEW, "ccnew" },
132 { TCPOPT_CCECHO, "ccecho" },
133 { TCPOPT_SIGNATURE, "md5" },
134 { TCPOPT_SCPS, "scps" },
135 { TCPOPT_UTO, "uto" },
136 { TCPOPT_TCPAO, "tcp-ao" },
137 { TCPOPT_MPTCP, "mptcp" },
138 { TCPOPT_FASTOPEN, "tfo" },
139 { TCPOPT_EXPERIMENT2, "exp" },
140 { 0, NULL }
141 };
142
143 static uint16_t
144 tcp_cksum(netdissect_options *ndo,
145 const struct ip *ip,
146 const struct tcphdr *tp,
147 u_int len)
148 {
149 return nextproto4_cksum(ndo, ip, (const uint8_t *)tp, len, len,
150 IPPROTO_TCP);
151 }
152
153 static uint16_t
154 tcp6_cksum(netdissect_options *ndo,
155 const struct ip6_hdr *ip6,
156 const struct tcphdr *tp,
157 u_int len)
158 {
159 return nextproto6_cksum(ndo, ip6, (const uint8_t *)tp, len, len,
160 IPPROTO_TCP);
161 }
162
163 void
164 tcp_print(netdissect_options *ndo,
165 const u_char *bp, u_int length,
166 const u_char *bp2, int fragmented)
167 {
168 const struct tcphdr *tp;
169 const struct ip *ip;
170 uint16_t flags;
171 u_int hlen;
172 char ch;
173 uint16_t sport, dport, win, urp;
174 uint32_t seq, ack, thseq, thack;
175 u_int utoval;
176 uint16_t magic;
177 int rev;
178 const struct ip6_hdr *ip6;
179 u_int header_len; /* Header length in bytes */
180
181 ndo->ndo_protocol = "tcp";
182 tp = (const struct tcphdr *)bp;
183 ip = (const struct ip *)bp2;
184 if (IP_V(ip) == 6)
185 ip6 = (const struct ip6_hdr *)bp2;
186 else
187 ip6 = NULL;
188 ch = '\0';
189 if (!ND_TTEST_2(tp->th_dport)) {
190 if (ip6) {
191 ND_PRINT("%s > %s:",
192 GET_IP6ADDR_STRING(ip6->ip6_src),
193 GET_IP6ADDR_STRING(ip6->ip6_dst));
194 } else {
195 ND_PRINT("%s > %s:",
196 GET_IPADDR_STRING(ip->ip_src),
197 GET_IPADDR_STRING(ip->ip_dst));
198 }
199 nd_trunc_longjmp(ndo);
200 }
201
202 sport = GET_BE_U_2(tp->th_sport);
203 dport = GET_BE_U_2(tp->th_dport);
204
205 if (ip6) {
206 if (GET_U_1(ip6->ip6_nxt) == IPPROTO_TCP) {
207 ND_PRINT("%s.%s > %s.%s: ",
208 GET_IP6ADDR_STRING(ip6->ip6_src),
209 tcpport_string(ndo, sport),
210 GET_IP6ADDR_STRING(ip6->ip6_dst),
211 tcpport_string(ndo, dport));
212 } else {
213 ND_PRINT("%s > %s: ",
214 tcpport_string(ndo, sport), tcpport_string(ndo, dport));
215 }
216 } else {
217 if (GET_U_1(ip->ip_p) == IPPROTO_TCP) {
218 ND_PRINT("%s.%s > %s.%s: ",
219 GET_IPADDR_STRING(ip->ip_src),
220 tcpport_string(ndo, sport),
221 GET_IPADDR_STRING(ip->ip_dst),
222 tcpport_string(ndo, dport));
223 } else {
224 ND_PRINT("%s > %s: ",
225 tcpport_string(ndo, sport), tcpport_string(ndo, dport));
226 }
227 }
228
229 hlen = TH_OFF(tp) * 4;
230
231 if (hlen < sizeof(*tp)) {
232 ND_PRINT(" tcp %u [bad hdr length %u - too short, < %zu]",
233 length - hlen, hlen, sizeof(*tp));
234 goto invalid;
235 }
236
237 seq = GET_BE_U_4(tp->th_seq);
238 ack = GET_BE_U_4(tp->th_ack);
239 win = GET_BE_U_2(tp->th_win);
240 urp = GET_BE_U_2(tp->th_urp);
241
242 if (ndo->ndo_qflag) {
243 ND_PRINT("tcp %u", length - hlen);
244 if (hlen > length) {
245 ND_PRINT(" [bad hdr length %u - too long, > %u]",
246 hlen, length);
247 goto invalid;
248 }
249 return;
250 }
251
252 flags = tcp_get_flags(tp);
253 ND_PRINT("Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags));
254
255 if (!ndo->ndo_Sflag && (flags & TH_ACK)) {
256 /*
257 * Find (or record) the initial sequence numbers for
258 * this conversation. (we pick an arbitrary
259 * collating order so there's only one entry for
260 * both directions).
261 */
262 rev = 0;
263 if (ip6) {
264 struct tcp_seq_hash6 *th;
265 struct tcp_seq_hash6 *tcp_seq_hash;
266 const void *src, *dst;
267 struct tha6 tha;
268
269 tcp_seq_hash = tcp_seq_hash6;
270 src = (const void *)ip6->ip6_src;
271 dst = (const void *)ip6->ip6_dst;
272 if (sport > dport)
273 rev = 1;
274 else if (sport == dport) {
275 if (UNALIGNED_MEMCMP(src, dst, sizeof(ip6->ip6_dst)) > 0)
276 rev = 1;
277 }
278 if (rev) {
279 UNALIGNED_MEMCPY(&tha.src, dst, sizeof(ip6->ip6_dst));
280 UNALIGNED_MEMCPY(&tha.dst, src, sizeof(ip6->ip6_src));
281 tha.port = ((u_int)dport) << 16 | sport;
282 } else {
283 UNALIGNED_MEMCPY(&tha.dst, dst, sizeof(ip6->ip6_dst));
284 UNALIGNED_MEMCPY(&tha.src, src, sizeof(ip6->ip6_src));
285 tha.port = ((u_int)sport) << 16 | dport;
286 }
287
288 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
289 th->nxt; th = th->nxt)
290 if (memcmp((char *)&tha, (char *)&th->addr,
291 sizeof(th->addr)) == 0)
292 break;
293
294 if (!th->nxt || (flags & TH_SYN)) {
295 /* didn't find it or new conversation */
296 /* calloc() return used by the 'tcp_seq_hash6'
297 hash table: do not free() */
298 if (th->nxt == NULL) {
299 th->nxt = (struct tcp_seq_hash6 *)
300 calloc(1, sizeof(*th));
301 if (th->nxt == NULL)
302 (*ndo->ndo_error)(ndo,
303 S_ERR_ND_MEM_ALLOC,
304 "%s: calloc", __func__);
305 }
306 th->addr = tha;
307 if (rev)
308 th->ack = seq, th->seq = ack - 1;
309 else
310 th->seq = seq, th->ack = ack - 1;
311 } else {
312 if (rev)
313 seq -= th->ack, ack -= th->seq;
314 else
315 seq -= th->seq, ack -= th->ack;
316 }
317
318 thseq = th->seq;
319 thack = th->ack;
320 } else {
321 struct tcp_seq_hash *th;
322 struct tcp_seq_hash *tcp_seq_hash;
323 struct tha tha;
324
325 tcp_seq_hash = tcp_seq_hash4;
326 if (sport > dport)
327 rev = 1;
328 else if (sport == dport) {
329 if (UNALIGNED_MEMCMP(ip->ip_src, ip->ip_dst, sizeof(ip->ip_dst)) > 0)
330 rev = 1;
331 }
332 if (rev) {
333 UNALIGNED_MEMCPY(&tha.src, ip->ip_dst,
334 sizeof(ip->ip_dst));
335 UNALIGNED_MEMCPY(&tha.dst, ip->ip_src,
336 sizeof(ip->ip_src));
337 tha.port = ((u_int)dport) << 16 | sport;
338 } else {
339 UNALIGNED_MEMCPY(&tha.dst, ip->ip_dst,
340 sizeof(ip->ip_dst));
341 UNALIGNED_MEMCPY(&tha.src, ip->ip_src,
342 sizeof(ip->ip_src));
343 tha.port = ((u_int)sport) << 16 | dport;
344 }
345
346 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
347 th->nxt; th = th->nxt)
348 if (memcmp((char *)&tha, (char *)&th->addr,
349 sizeof(th->addr)) == 0)
350 break;
351
352 if (!th->nxt || (flags & TH_SYN)) {
353 /* didn't find it or new conversation */
354 /* calloc() return used by the 'tcp_seq_hash4'
355 hash table: do not free() */
356 if (th->nxt == NULL) {
357 th->nxt = (struct tcp_seq_hash *)
358 calloc(1, sizeof(*th));
359 if (th->nxt == NULL)
360 (*ndo->ndo_error)(ndo,
361 S_ERR_ND_MEM_ALLOC,
362 "%s: calloc", __func__);
363 }
364 th->addr = tha;
365 if (rev)
366 th->ack = seq, th->seq = ack - 1;
367 else
368 th->seq = seq, th->ack = ack - 1;
369 } else {
370 if (rev)
371 seq -= th->ack, ack -= th->seq;
372 else
373 seq -= th->seq, ack -= th->ack;
374 }
375
376 thseq = th->seq;
377 thack = th->ack;
378 }
379 } else {
380 /*fool gcc*/
381 thseq = thack = rev = 0;
382 }
383 if (hlen > length) {
384 ND_PRINT(" [bad hdr length %u - too long, > %u]",
385 hlen, length);
386 goto invalid;
387 }
388
389 if (ndo->ndo_vflag && !ndo->ndo_Kflag && !fragmented) {
390 /* Check the checksum, if possible. */
391 uint16_t sum, tcp_sum;
392
393 if (IP_V(ip) == 4) {
394 if (ND_TTEST_LEN(tp->th_sport, length)) {
395 sum = tcp_cksum(ndo, ip, tp, length);
396 tcp_sum = GET_BE_U_2(tp->th_sum);
397
398 ND_PRINT(", cksum 0x%04x", tcp_sum);
399 if (sum != 0)
400 ND_PRINT(" (incorrect -> 0x%04x)",
401 in_cksum_shouldbe(tcp_sum, sum));
402 else
403 ND_PRINT(" (correct)");
404 }
405 } else if (IP_V(ip) == 6) {
406 if (ND_TTEST_LEN(tp->th_sport, length)) {
407 sum = tcp6_cksum(ndo, ip6, tp, length);
408 tcp_sum = GET_BE_U_2(tp->th_sum);
409
410 ND_PRINT(", cksum 0x%04x", tcp_sum);
411 if (sum != 0)
412 ND_PRINT(" (incorrect -> 0x%04x)",
413 in_cksum_shouldbe(tcp_sum, sum));
414 else
415 ND_PRINT(" (correct)");
416
417 }
418 }
419 }
420
421 length -= hlen;
422 if (ndo->ndo_vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST)) {
423 ND_PRINT(", seq %u", seq);
424
425 if (length > 0) {
426 ND_PRINT(":%u", seq + length);
427 }
428 }
429
430 if (flags & TH_ACK) {
431 ND_PRINT(", ack %u", ack);
432 }
433
434 ND_PRINT(", win %u", win);
435
436 if (flags & TH_URG)
437 ND_PRINT(", urg %u", urp);
438 /*
439 * Handle any options.
440 */
441 if (hlen > sizeof(*tp)) {
442 const u_char *cp;
443 u_int i, opt, datalen;
444 u_int len;
445
446 hlen -= sizeof(*tp);
447 cp = (const u_char *)tp + sizeof(*tp);
448 ND_PRINT(", options [");
449 while (hlen != 0) {
450 if (ch != '\0')
451 ND_PRINT("%c", ch);
452 opt = GET_U_1(cp);
453 cp++;
454 if (ZEROLENOPT(opt))
455 len = 1;
456 else {
457 len = GET_U_1(cp);
458 cp++; /* total including type, len */
459 if (len < 2 || len > hlen)
460 goto bad;
461 --hlen; /* account for length byte */
462 }
463 --hlen; /* account for type byte */
464 datalen = 0;
465
466 /* Bail if "l" bytes of data are not left or were not captured */
467 #define LENCHECK(l) { if ((l) > hlen) goto bad; }
468
469
470 ND_PRINT("%s", tok2str(tcp_option_values, "unknown-%u", opt));
471
472 switch (opt) {
473
474 case TCPOPT_MAXSEG:
475 datalen = 2;
476 LENCHECK(datalen);
477 ND_PRINT(" %u", GET_BE_U_2(cp));
478 break;
479
480 case TCPOPT_WSCALE:
481 datalen = 1;
482 LENCHECK(datalen);
483 ND_PRINT(" %u", GET_U_1(cp));
484 break;
485
486 case TCPOPT_SACK:
487 datalen = len - 2;
488 if (datalen % 8 != 0) {
489 ND_PRINT(" invalid sack");
490 } else {
491 uint32_t s, e;
492
493 ND_PRINT(" %u ", datalen / 8);
494 for (i = 0; i < datalen; i += 8) {
495 LENCHECK(i + 4);
496 s = GET_BE_U_4(cp + i);
497 LENCHECK(i + 8);
498 e = GET_BE_U_4(cp + i + 4);
499 if (rev) {
500 s -= thseq;
501 e -= thseq;
502 } else {
503 s -= thack;
504 e -= thack;
505 }
506 ND_PRINT("{%u:%u}", s, e);
507 }
508 }
509 break;
510
511 case TCPOPT_CC:
512 case TCPOPT_CCNEW:
513 case TCPOPT_CCECHO:
514 case TCPOPT_ECHO:
515 case TCPOPT_ECHOREPLY:
516
517 /*
518 * those options share their semantics.
519 * fall through
520 */
521 datalen = 4;
522 LENCHECK(datalen);
523 ND_PRINT(" %u", GET_BE_U_4(cp));
524 break;
525
526 case TCPOPT_TIMESTAMP:
527 datalen = 8;
528 LENCHECK(datalen);
529 ND_PRINT(" val %u ecr %u",
530 GET_BE_U_4(cp),
531 GET_BE_U_4(cp + 4));
532 break;
533
534 case TCPOPT_SIGNATURE:
535 datalen = TCP_SIGLEN;
536 LENCHECK(datalen);
537 ND_TCHECK_LEN(cp, datalen);
538 ND_PRINT(" ");
539 #ifdef HAVE_LIBCRYPTO
540 switch (tcp_verify_signature(ndo, ip, tp,
541 bp + TH_OFF(tp) * 4, length, cp)) {
542
543 case SIGNATURE_VALID:
544 ND_PRINT("valid");
545 break;
546
547 case SIGNATURE_INVALID:
548 nd_print_invalid(ndo);
549 break;
550
551 case CANT_CHECK_SIGNATURE:
552 ND_PRINT("can't check - ");
553 for (i = 0; i < TCP_SIGLEN; ++i)
554 ND_PRINT("%02x",
555 GET_U_1(cp + i));
556 break;
557 }
558 #else
559 for (i = 0; i < TCP_SIGLEN; ++i)
560 ND_PRINT("%02x", GET_U_1(cp + i));
561 #endif
562 break;
563
564 case TCPOPT_SCPS:
565 datalen = 2;
566 LENCHECK(datalen);
567 ND_PRINT(" cap %02x id %u", GET_U_1(cp),
568 GET_U_1(cp + 1));
569 break;
570
571 case TCPOPT_TCPAO:
572 datalen = len - 2;
573 /* RFC 5925 Section 2.2:
574 * "The Length value MUST be greater than or equal to 4."
575 * (This includes the Kind and Length fields already processed
576 * at this point.)
577 */
578 if (datalen < 2) {
579 nd_print_invalid(ndo);
580 } else {
581 LENCHECK(1);
582 ND_PRINT(" keyid %u", GET_U_1(cp));
583 LENCHECK(2);
584 ND_PRINT(" rnextkeyid %u",
585 GET_U_1(cp + 1));
586 if (datalen > 2) {
587 ND_PRINT(" mac 0x");
588 for (i = 2; i < datalen; i++) {
589 LENCHECK(i + 1);
590 ND_PRINT("%02x",
591 GET_U_1(cp + i));
592 }
593 }
594 }
595 break;
596
597 case TCPOPT_EOL:
598 case TCPOPT_NOP:
599 case TCPOPT_SACKOK:
600 /*
601 * Nothing interesting.
602 * fall through
603 */
604 break;
605
606 case TCPOPT_UTO:
607 datalen = 2;
608 LENCHECK(datalen);
609 utoval = GET_BE_U_2(cp);
610 ND_PRINT(" 0x%x", utoval);
611 if (utoval & 0x0001)
612 utoval = (utoval >> 1) * 60;
613 else
614 utoval >>= 1;
615 ND_PRINT(" %u", utoval);
616 break;
617
618 case TCPOPT_MPTCP:
619 {
620 const u_char *snapend_save;
621 int ret;
622
623 datalen = len - 2;
624 LENCHECK(datalen);
625 /* FIXME: Proof-read mptcp_print() and if it
626 * always covers all bytes when it returns 1,
627 * only do ND_TCHECK_LEN() if it returned 0.
628 */
629 ND_TCHECK_LEN(cp, datalen);
630 /* Update the snapend to the end of the option
631 * before calling mptcp_print(). Some options
632 * (MPTCP or others) may be present after a
633 * MPTCP option. This prevents that, in
634 * mptcp_print(), the remaining length < the
635 * remaining caplen.
636 */
637 snapend_save = ndo->ndo_snapend;
638 ndo->ndo_snapend = ND_MIN(cp - 2 + len,
639 ndo->ndo_snapend);
640 ret = mptcp_print(ndo, cp - 2, len, flags);
641 ndo->ndo_snapend = snapend_save;
642 if (!ret)
643 goto bad;
644 break;
645 }
646
647 case TCPOPT_FASTOPEN:
648 datalen = len - 2;
649 LENCHECK(datalen);
650 ND_TCHECK_LEN(cp, datalen);
651 ND_PRINT(" ");
652 print_tcp_fastopen_option(ndo, cp, datalen);
653 break;
654
655 case TCPOPT_EXPERIMENT2:
656 datalen = len - 2;
657 LENCHECK(datalen);
658 ND_TCHECK_LEN(cp, datalen);
659 if (datalen < 2)
660 goto bad;
661 /* RFC6994 */
662 magic = GET_BE_U_2(cp);
663 ND_PRINT("-");
664
665 switch(magic) {
666
667 case 0xf989: /* TCP Fast Open RFC 7413 */
668 ND_PRINT("tfo");
669 print_tcp_fastopen_option(ndo, cp + 2, datalen - 2);
670 break;
671
672 default:
673 /* Unknown magic number */
674 ND_PRINT("%04x", magic);
675 break;
676 }
677 break;
678
679 default:
680 datalen = len - 2;
681 if (datalen)
682 ND_PRINT(" 0x");
683 for (i = 0; i < datalen; ++i) {
684 LENCHECK(i + 1);
685 ND_PRINT("%02x", GET_U_1(cp + i));
686 }
687 break;
688 }
689
690 /* Account for data printed */
691 cp += datalen;
692 hlen -= datalen;
693
694 /* Check specification against observed length */
695 ++datalen; /* option octet */
696 if (!ZEROLENOPT(opt))
697 ++datalen; /* size octet */
698 if (datalen != len)
699 ND_PRINT("[len %u]", len);
700 ch = ',';
701 if (opt == TCPOPT_EOL)
702 break;
703 }
704 ND_PRINT("]");
705 }
706
707 /*
708 * Print length field before crawling down the stack.
709 */
710 ND_PRINT(", length %u", length);
711
712 if (length == 0)
713 return;
714
715 /*
716 * Decode payload if necessary.
717 */
718 header_len = TH_OFF(tp) * 4;
719 /*
720 * Do a bounds check before decoding the payload.
721 * At least the header data is required.
722 */
723 if (!ND_TTEST_LEN(bp, header_len)) {
724 ND_PRINT(" [remaining caplen(%u) < header length(%u)]",
725 ND_BYTES_AVAILABLE_AFTER(bp), header_len);
726 nd_trunc_longjmp(ndo);
727 }
728 bp += header_len;
729 if ((flags & TH_RST) && ndo->ndo_vflag) {
730 print_tcp_rst_data(ndo, bp, length);
731 return;
732 }
733
734 if (ndo->ndo_packettype) {
735 switch (ndo->ndo_packettype) {
736 case PT_ZMTP1:
737 zmtp1_print(ndo, bp, length);
738 break;
739 case PT_RESP:
740 resp_print(ndo, bp, length);
741 break;
742 case PT_DOMAIN:
743 /* over_tcp: TRUE, is_mdns: FALSE */
744 domain_print(ndo, bp, length, TRUE, FALSE);
745 break;
746 }
747 return;
748 }
749
750 if (IS_SRC_OR_DST_PORT(FTP_PORT)) {
751 ND_PRINT(": ");
752 ftp_print(ndo, bp, length);
753 } else if (IS_SRC_OR_DST_PORT(SSH_PORT)) {
754 ssh_print(ndo, bp, length);
755 } else if (IS_SRC_OR_DST_PORT(TELNET_PORT)) {
756 telnet_print(ndo, bp, length);
757 } else if (IS_SRC_OR_DST_PORT(SMTP_PORT)) {
758 ND_PRINT(": ");
759 smtp_print(ndo, bp, length);
760 } else if (IS_SRC_OR_DST_PORT(WHOIS_PORT)) {
761 ND_PRINT(": ");
762 whois_print(ndo, bp, length);
763 } else if (IS_SRC_OR_DST_PORT(NAMESERVER_PORT)) {
764 /* over_tcp: TRUE, is_mdns: FALSE */
765 domain_print(ndo, bp, length, TRUE, FALSE);
766 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT)) {
767 ND_PRINT(": ");
768 http_print(ndo, bp, length);
769 #ifdef ENABLE_SMB
770 } else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT)) {
771 nbt_tcp_print(ndo, bp, length);
772 #endif
773 } else if (IS_SRC_OR_DST_PORT(BGP_PORT)) {
774 bgp_print(ndo, bp, length);
775 } else if (IS_SRC_OR_DST_PORT(RPKI_RTR_PORT)) {
776 rpki_rtr_print(ndo, bp, length);
777 #ifdef ENABLE_SMB
778 } else if (IS_SRC_OR_DST_PORT(SMB_PORT)) {
779 smb_tcp_print(ndo, bp, length);
780 #endif
781 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT)) {
782 ND_PRINT(": ");
783 rtsp_print(ndo, bp, length);
784 } else if (IS_SRC_OR_DST_PORT(MSDP_PORT)) {
785 msdp_print(ndo, bp, length);
786 } else if (IS_SRC_OR_DST_PORT(LDP_PORT)) {
787 ldp_print(ndo, bp, length);
788 } else if (IS_SRC_OR_DST_PORT(PPTP_PORT))
789 pptp_print(ndo, bp);
790 else if (IS_SRC_OR_DST_PORT(REDIS_PORT))
791 resp_print(ndo, bp, length);
792 else if (IS_SRC_OR_DST_PORT(BEEP_PORT))
793 beep_print(ndo, bp, length);
794 else if (IS_SRC_OR_DST_PORT(OPENFLOW_PORT_OLD) || IS_SRC_OR_DST_PORT(OPENFLOW_PORT_IANA)) {
795 openflow_print(ndo, bp, length);
796 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT_ALT)) {
797 ND_PRINT(": ");
798 http_print(ndo, bp, length);
799 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT_ALT)) {
800 ND_PRINT(": ");
801 rtsp_print(ndo, bp, length);
802 } else if ((IS_SRC_OR_DST_PORT(NFS_PORT)) &&
803 length >= 4) {
804 /*
805 * If data present, header length valid, and NFS port used,
806 * assume NFS.
807 * Pass offset of data plus 4 bytes for RPC TCP msg length
808 * to NFS print routines.
809 */
810 uint32_t fraglen;
811 const struct sunrpc_msg *rp;
812 enum sunrpc_msg_type direction;
813
814 fraglen = GET_BE_U_4(bp) & 0x7FFFFFFF;
815 if (fraglen > (length) - 4)
816 fraglen = (length) - 4;
817 rp = (const struct sunrpc_msg *)(bp + 4);
818 if (ND_TTEST_4(rp->rm_direction)) {
819 direction = (enum sunrpc_msg_type) GET_BE_U_4(rp->rm_direction);
820 if (dport == NFS_PORT && direction == SUNRPC_CALL) {
821 ND_PRINT(": NFS request xid %u ",
822 GET_BE_U_4(rp->rm_xid));
823 nfsreq_noaddr_print(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
824 return;
825 }
826 if (sport == NFS_PORT && direction == SUNRPC_REPLY) {
827 ND_PRINT(": NFS reply xid %u ",
828 GET_BE_U_4(rp->rm_xid));
829 nfsreply_noaddr_print(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
830 return;
831 }
832 }
833 }
834
835 return;
836 bad:
837 ND_PRINT("[bad opt]");
838 if (ch != '\0')
839 ND_PRINT("]");
840 return;
841 invalid:
842 nd_print_invalid(ndo);
843 }
844
845 /*
846 * RFC1122 says the following on data in RST segments:
847 *
848 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
849 *
850 * A TCP SHOULD allow a received RST segment to include data.
851 *
852 * DISCUSSION
853 * It has been suggested that a RST segment could contain
854 * ASCII text that encoded and explained the cause of the
855 * RST. No standard has yet been established for such
856 * data.
857 *
858 */
859
860 static void
861 print_tcp_rst_data(netdissect_options *ndo,
862 const u_char *sp, u_int length)
863 {
864 ND_PRINT(ND_TTEST_LEN(sp, length) ? " [RST" : " [!RST");
865 if (length > MAX_RST_DATA_LEN) {
866 length = MAX_RST_DATA_LEN; /* can use -X for longer */
867 ND_PRINT("+"); /* indicate we truncate */
868 }
869 ND_PRINT(" ");
870 (void)nd_printn(ndo, sp, length, ndo->ndo_snapend);
871 ND_PRINT("]");
872 }
873
874 static void
875 print_tcp_fastopen_option(netdissect_options *ndo, const u_char *cp,
876 u_int datalen)
877 {
878 u_int i;
879
880 if (datalen == 0) {
881 /* Fast Open Cookie Request */
882 ND_PRINT(" cookiereq");
883 } else {
884 /* Fast Open Cookie */
885 if (datalen % 2 != 0 || datalen < 4 || datalen > 16) {
886 nd_print_invalid(ndo);
887 } else {
888 ND_PRINT(" cookie ");
889 for (i = 0; i < datalen; ++i)
890 ND_PRINT("%02x", GET_U_1(cp + i));
891 }
892 }
893 }
894
895 #ifdef HAVE_LIBCRYPTO
896 DIAG_OFF_DEPRECATION
897 static int
898 tcp_verify_signature(netdissect_options *ndo,
899 const struct ip *ip, const struct tcphdr *tp,
900 const u_char *data, u_int length, const u_char *rcvsig)
901 {
902 struct tcphdr tp1;
903 u_char sig[TCP_SIGLEN];
904 char zero_proto = 0;
905 MD5_CTX ctx;
906 uint16_t savecsum, tlen;
907 const struct ip6_hdr *ip6;
908 uint32_t len32;
909 uint8_t nxt;
910
911 if (!ND_TTEST_LEN(data, length)) {
912 ND_PRINT("snaplen too short, ");
913 return (CANT_CHECK_SIGNATURE);
914 }
915
916 tp1 = *tp;
917
918 if (ndo->ndo_sigsecret == NULL) {
919 ND_PRINT("shared secret not supplied with -M, ");
920 return (CANT_CHECK_SIGNATURE);
921 }
922
923 MD5_Init(&ctx);
924 /*
925 * Step 1: Update MD5 hash with IP pseudo-header.
926 */
927 if (IP_V(ip) == 4) {
928 MD5_Update(&ctx, (const char *)&ip->ip_src, sizeof(ip->ip_src));
929 MD5_Update(&ctx, (const char *)&ip->ip_dst, sizeof(ip->ip_dst));
930 MD5_Update(&ctx, (const char *)&zero_proto, sizeof(zero_proto));
931 MD5_Update(&ctx, (const char *)&ip->ip_p, sizeof(ip->ip_p));
932 tlen = GET_BE_U_2(ip->ip_len) - IP_HL(ip) * 4;
933 tlen = htons(tlen);
934 MD5_Update(&ctx, (const char *)&tlen, sizeof(tlen));
935 } else if (IP_V(ip) == 6) {
936 ip6 = (const struct ip6_hdr *)ip;
937 MD5_Update(&ctx, (const char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
938 MD5_Update(&ctx, (const char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
939 len32 = htonl(GET_BE_U_2(ip6->ip6_plen));
940 MD5_Update(&ctx, (const char *)&len32, sizeof(len32));
941 nxt = 0;
942 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
943 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
944 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
945 nxt = IPPROTO_TCP;
946 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
947 } else {
948 ND_PRINT("IP version not 4 or 6, ");
949 return (CANT_CHECK_SIGNATURE);
950 }
951
952 /*
953 * Step 2: Update MD5 hash with TCP header, excluding options.
954 * The TCP checksum must be set to zero.
955 */
956 memcpy(&savecsum, tp1.th_sum, sizeof(savecsum));
957 memset(tp1.th_sum, 0, sizeof(tp1.th_sum));
958 MD5_Update(&ctx, (const char *)&tp1, sizeof(struct tcphdr));
959 memcpy(tp1.th_sum, &savecsum, sizeof(tp1.th_sum));
960 /*
961 * Step 3: Update MD5 hash with TCP segment data, if present.
962 */
963 if (length > 0)
964 MD5_Update(&ctx, data, length);
965 /*
966 * Step 4: Update MD5 hash with shared secret.
967 */
968 MD5_Update(&ctx, ndo->ndo_sigsecret, strlen(ndo->ndo_sigsecret));
969 MD5_Final(sig, &ctx);
970
971 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
972 return (SIGNATURE_VALID);
973 else
974 return (SIGNATURE_INVALID);
975 }
976 DIAG_ON_DEPRECATION
977 #endif /* HAVE_LIBCRYPTO */