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