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