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