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