]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
Reorganize netdissect.h
[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 const struct in_addr *src, *dst;
297 struct tha tha;
298
299 tcp_seq_hash = tcp_seq_hash4;
300 src = &ip->ip_src;
301 dst = &ip->ip_dst;
302 if (sport > dport)
303 rev = 1;
304 else if (sport == dport) {
305 if (UNALIGNED_MEMCMP(src, dst, sizeof ip->ip_dst) > 0)
306 rev = 1;
307 }
308 if (rev) {
309 UNALIGNED_MEMCPY(&tha.src, dst, sizeof ip->ip_dst);
310 UNALIGNED_MEMCPY(&tha.dst, src, sizeof ip->ip_src);
311 tha.port = dport << 16 | sport;
312 } else {
313 UNALIGNED_MEMCPY(&tha.dst, dst, sizeof ip->ip_dst);
314 UNALIGNED_MEMCPY(&tha.src, src, sizeof ip->ip_src);
315 tha.port = sport << 16 | dport;
316 }
317
318 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
319 th->nxt; th = th->nxt)
320 if (memcmp((char *)&tha, (char *)&th->addr,
321 sizeof(th->addr)) == 0)
322 break;
323
324 if (!th->nxt || (flags & TH_SYN)) {
325 /* didn't find it or new conversation */
326 if (th->nxt == NULL) {
327 th->nxt = (struct tcp_seq_hash *)
328 calloc(1, sizeof(*th));
329 if (th->nxt == NULL)
330 (*ndo->ndo_error)(ndo,
331 "tcp_print: calloc");
332 }
333 th->addr = tha;
334 if (rev)
335 th->ack = seq, th->seq = ack - 1;
336 else
337 th->seq = seq, th->ack = ack - 1;
338 } else {
339 if (rev)
340 seq -= th->ack, ack -= th->seq;
341 else
342 seq -= th->seq, ack -= th->ack;
343 }
344
345 thseq = th->seq;
346 thack = th->ack;
347 }
348 } else {
349 /*fool gcc*/
350 thseq = thack = rev = 0;
351 }
352 if (hlen > length) {
353 ND_PRINT((ndo, " [bad hdr length %u - too long, > %u]",
354 hlen, length));
355 return;
356 }
357
358 if (ndo->ndo_vflag && !ndo->ndo_Kflag && !fragmented) {
359 /* Check the checksum, if possible. */
360 uint16_t sum, tcp_sum;
361
362 if (IP_V(ip) == 4) {
363 if (ND_TTEST2(tp->th_sport, length)) {
364 sum = tcp_cksum(ndo, ip, tp, length);
365 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
366
367 ND_PRINT((ndo, ", cksum 0x%04x", tcp_sum));
368 if (sum != 0)
369 ND_PRINT((ndo, " (incorrect -> 0x%04x)",
370 in_cksum_shouldbe(tcp_sum, sum)));
371 else
372 ND_PRINT((ndo, " (correct)"));
373 }
374 } else if (IP_V(ip) == 6 && ip6->ip6_plen) {
375 if (ND_TTEST2(tp->th_sport, length)) {
376 sum = nextproto6_cksum(ip6, (const uint8_t *)tp,
377 length, length, IPPROTO_TCP);
378 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
379
380 ND_PRINT((ndo, ", cksum 0x%04x", tcp_sum));
381 if (sum != 0)
382 ND_PRINT((ndo, " (incorrect -> 0x%04x)",
383 in_cksum_shouldbe(tcp_sum, sum)));
384 else
385 ND_PRINT((ndo, " (correct)"));
386
387 }
388 }
389 }
390
391 length -= hlen;
392 if (ndo->ndo_vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST)) {
393 ND_PRINT((ndo, ", seq %u", seq));
394
395 if (length > 0) {
396 ND_PRINT((ndo, ":%u", seq + length));
397 }
398 }
399
400 if (flags & TH_ACK) {
401 ND_PRINT((ndo, ", ack %u", ack));
402 }
403
404 ND_PRINT((ndo, ", win %d", win));
405
406 if (flags & TH_URG)
407 ND_PRINT((ndo, ", urg %d", urp));
408 /*
409 * Handle any options.
410 */
411 if (hlen > sizeof(*tp)) {
412 register const u_char *cp;
413 register u_int i, opt, datalen;
414 register u_int len;
415
416 hlen -= sizeof(*tp);
417 cp = (const u_char *)tp + sizeof(*tp);
418 ND_PRINT((ndo, ", options ["));
419 while (hlen > 0) {
420 if (ch != '\0')
421 ND_PRINT((ndo, "%c", ch));
422 ND_TCHECK(*cp);
423 opt = *cp++;
424 if (ZEROLENOPT(opt))
425 len = 1;
426 else {
427 ND_TCHECK(*cp);
428 len = *cp++; /* total including type, len */
429 if (len < 2 || len > hlen)
430 goto bad;
431 --hlen; /* account for length byte */
432 }
433 --hlen; /* account for type byte */
434 datalen = 0;
435
436 /* Bail if "l" bytes of data are not left or were not captured */
437 #define LENCHECK(l) { if ((l) > hlen) goto bad; ND_TCHECK2(*cp, l); }
438
439
440 ND_PRINT((ndo, "%s", tok2str(tcp_option_values, "unknown-%u", opt)));
441
442 switch (opt) {
443
444 case TCPOPT_MAXSEG:
445 datalen = 2;
446 LENCHECK(datalen);
447 ND_PRINT((ndo, " %u", EXTRACT_16BITS(cp)));
448 break;
449
450 case TCPOPT_WSCALE:
451 datalen = 1;
452 LENCHECK(datalen);
453 ND_PRINT((ndo, " %u", *cp));
454 break;
455
456 case TCPOPT_SACK:
457 datalen = len - 2;
458 if (datalen % 8 != 0) {
459 ND_PRINT((ndo, "invalid sack"));
460 } else {
461 uint32_t s, e;
462
463 ND_PRINT((ndo, " %d ", datalen / 8));
464 for (i = 0; i < datalen; i += 8) {
465 LENCHECK(i + 4);
466 s = EXTRACT_32BITS(cp + i);
467 LENCHECK(i + 8);
468 e = EXTRACT_32BITS(cp + i + 4);
469 if (rev) {
470 s -= thseq;
471 e -= thseq;
472 } else {
473 s -= thack;
474 e -= thack;
475 }
476 ND_PRINT((ndo, "{%u:%u}", s, e));
477 }
478 }
479 break;
480
481 case TCPOPT_CC:
482 case TCPOPT_CCNEW:
483 case TCPOPT_CCECHO:
484 case TCPOPT_ECHO:
485 case TCPOPT_ECHOREPLY:
486
487 /*
488 * those options share their semantics.
489 * fall through
490 */
491 datalen = 4;
492 LENCHECK(datalen);
493 ND_PRINT((ndo, " %u", EXTRACT_32BITS(cp)));
494 break;
495
496 case TCPOPT_TIMESTAMP:
497 datalen = 8;
498 LENCHECK(datalen);
499 ND_PRINT((ndo, " val %u ecr %u",
500 EXTRACT_32BITS(cp),
501 EXTRACT_32BITS(cp + 4)));
502 break;
503
504 case TCPOPT_SIGNATURE:
505 datalen = TCP_SIGLEN;
506 LENCHECK(datalen);
507 #ifdef HAVE_LIBCRYPTO
508 switch (tcp_verify_signature(ndo, ip, tp,
509 bp + TH_OFF(tp) * 4, length, cp)) {
510
511 case SIGNATURE_VALID:
512 ND_PRINT((ndo, "valid"));
513 break;
514
515 case SIGNATURE_INVALID:
516 ND_PRINT((ndo, "invalid"));
517 break;
518
519 case CANT_CHECK_SIGNATURE:
520 ND_PRINT((ndo, "can't check - "));
521 for (i = 0; i < TCP_SIGLEN; ++i)
522 ND_PRINT((ndo, "%02x", cp[i]));
523 break;
524 }
525 #else
526 for (i = 0; i < TCP_SIGLEN; ++i)
527 ND_PRINT((ndo, "%02x", cp[i]));
528 #endif
529 break;
530
531 case TCPOPT_AUTH:
532 ND_PRINT((ndo, "keyid %d", *cp++));
533 datalen = len - 3;
534 for (i = 0; i < datalen; ++i) {
535 LENCHECK(i);
536 ND_PRINT((ndo, "%02x", cp[i]));
537 }
538 break;
539
540
541 case TCPOPT_EOL:
542 case TCPOPT_NOP:
543 case TCPOPT_SACKOK:
544 /*
545 * Nothing interesting.
546 * fall through
547 */
548 break;
549
550 case TCPOPT_UTO:
551 datalen = 2;
552 LENCHECK(datalen);
553 utoval = EXTRACT_16BITS(cp);
554 ND_PRINT((ndo, "0x%x", utoval));
555 if (utoval & 0x0001)
556 utoval = (utoval >> 1) * 60;
557 else
558 utoval >>= 1;
559 ND_PRINT((ndo, " %u", utoval));
560 break;
561
562 case TCPOPT_MPTCP:
563 datalen = len - 2;
564 LENCHECK(datalen);
565 if (!mptcp_print(ndo, cp-2, len, flags))
566 goto bad;
567 break;
568
569 case TCPOPT_FASTOPEN:
570 datalen = len - 2;
571 LENCHECK(datalen);
572 print_tcp_fastopen_option(ndo, cp, datalen, FALSE);
573 break;
574
575 case TCPOPT_EXPERIMENT2:
576 datalen = len - 2;
577 LENCHECK(datalen);
578 if (datalen < 2)
579 goto bad;
580 /* RFC6994 */
581 magic = EXTRACT_16BITS(cp);
582 ND_PRINT((ndo, "-"));
583
584 switch(magic) {
585
586 case 0xf989: /* TCP Fast Open RFC 7413 */
587 print_tcp_fastopen_option(ndo, cp + 2, datalen - 2, TRUE);
588 break;
589
590 default:
591 /* Unknown magic number */
592 ND_PRINT((ndo, "%04x", magic));
593 break;
594 }
595 break;
596
597 default:
598 datalen = len - 2;
599 if (datalen)
600 ND_PRINT((ndo, " 0x"));
601 for (i = 0; i < datalen; ++i) {
602 LENCHECK(i);
603 ND_PRINT((ndo, "%02x", cp[i]));
604 }
605 break;
606 }
607
608 /* Account for data printed */
609 cp += datalen;
610 hlen -= datalen;
611
612 /* Check specification against observed length */
613 ++datalen; /* option octet */
614 if (!ZEROLENOPT(opt))
615 ++datalen; /* size octet */
616 if (datalen != len)
617 ND_PRINT((ndo, "[len %d]", len));
618 ch = ',';
619 if (opt == TCPOPT_EOL)
620 break;
621 }
622 ND_PRINT((ndo, "]"));
623 }
624
625 /*
626 * Print length field before crawling down the stack.
627 */
628 ND_PRINT((ndo, ", length %u", length));
629
630 if (length <= 0)
631 return;
632
633 /*
634 * Decode payload if necessary.
635 */
636 bp += TH_OFF(tp) * 4;
637 if ((flags & TH_RST) && ndo->ndo_vflag) {
638 print_tcp_rst_data(ndo, bp, length);
639 return;
640 }
641
642 if (ndo->ndo_packettype) {
643 switch (ndo->ndo_packettype) {
644 case PT_ZMTP1:
645 zmtp1_print(ndo, bp, length);
646 break;
647 }
648 return;
649 }
650
651 if (sport == TELNET_PORT || dport == TELNET_PORT) {
652 telnet_print(ndo, bp, length);
653 } else if (sport == SMTP_PORT || dport == SMTP_PORT) {
654 ND_PRINT((ndo, ": "));
655 smtp_print(ndo, bp, length);
656 } else if (sport == BGP_PORT || dport == BGP_PORT)
657 bgp_print(ndo, bp, length);
658 else if (sport == PPTP_PORT || dport == PPTP_PORT)
659 pptp_print(ndo, bp);
660 #ifdef TCPDUMP_DO_SMB
661 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
662 nbt_tcp_print(ndo, bp, length);
663 else if (sport == SMB_PORT || dport == SMB_PORT)
664 smb_tcp_print(ndo, bp, length);
665 #endif
666 else if (sport == BEEP_PORT || dport == BEEP_PORT)
667 beep_print(ndo, bp, length);
668 else if (sport == OPENFLOW_PORT_OLD || dport == OPENFLOW_PORT_OLD ||
669 sport == OPENFLOW_PORT_IANA || dport == OPENFLOW_PORT_IANA)
670 openflow_print(ndo, bp, length);
671 else if (sport == FTP_PORT || dport == FTP_PORT) {
672 ND_PRINT((ndo, ": "));
673 ftp_print(ndo, bp, length);
674 } else if (sport == HTTP_PORT || dport == HTTP_PORT ||
675 sport == HTTP_PORT_ALT || dport == HTTP_PORT_ALT) {
676 ND_PRINT((ndo, ": "));
677 http_print(ndo, bp, length);
678 } else if (sport == RTSP_PORT || dport == RTSP_PORT ||
679 sport == RTSP_PORT_ALT || dport == RTSP_PORT_ALT) {
680 ND_PRINT((ndo, ": "));
681 rtsp_print(ndo, bp, length);
682 } else if (length > 2 &&
683 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT)) {
684 /*
685 * TCP DNS query has 2byte length at the head.
686 * XXX packet could be unaligned, it can go strange
687 */
688 ns_print(ndo, bp + 2, length - 2, 0);
689 } else if (sport == MSDP_PORT || dport == MSDP_PORT) {
690 msdp_print(ndo, bp, length);
691 } else if (sport == RPKI_RTR_PORT || dport == RPKI_RTR_PORT) {
692 rpki_rtr_print(ndo, bp, length);
693 }
694 else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
695 ldp_print(ndo, bp, length);
696 }
697 else if ((sport == NFS_PORT || dport == NFS_PORT) &&
698 length >= 4 && ND_TTEST2(*bp, 4)) {
699 /*
700 * If data present, header length valid, and NFS port used,
701 * assume NFS.
702 * Pass offset of data plus 4 bytes for RPC TCP msg length
703 * to NFS print routines.
704 */
705 uint32_t fraglen;
706 register const struct sunrpc_msg *rp;
707 enum sunrpc_msg_type direction;
708
709 fraglen = EXTRACT_32BITS(bp) & 0x7FFFFFFF;
710 if (fraglen > (length) - 4)
711 fraglen = (length) - 4;
712 rp = (const struct sunrpc_msg *)(bp + 4);
713 if (ND_TTEST(rp->rm_direction)) {
714 direction = (enum sunrpc_msg_type)EXTRACT_32BITS(&rp->rm_direction);
715 if (dport == NFS_PORT && direction == SUNRPC_CALL) {
716 ND_PRINT((ndo, ": NFS request xid %u ", EXTRACT_32BITS(&rp->rm_xid)));
717 nfsreq_print_noaddr(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
718 return;
719 }
720 if (sport == NFS_PORT && direction == SUNRPC_REPLY) {
721 ND_PRINT((ndo, ": NFS reply xid %u ", EXTRACT_32BITS(&rp->rm_xid)));
722 nfsreply_print_noaddr(ndo, (const u_char *)rp, fraglen, (const u_char *)ip);
723 return;
724 }
725 }
726 }
727
728 return;
729 bad:
730 ND_PRINT((ndo, "[bad opt]"));
731 if (ch != '\0')
732 ND_PRINT((ndo, ">"));
733 return;
734 trunc:
735 ND_PRINT((ndo, "[|tcp]"));
736 if (ch != '\0')
737 ND_PRINT((ndo, ">"));
738 }
739
740 /*
741 * RFC1122 says the following on data in RST segments:
742 *
743 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
744 *
745 * A TCP SHOULD allow a received RST segment to include data.
746 *
747 * DISCUSSION
748 * It has been suggested that a RST segment could contain
749 * ASCII text that encoded and explained the cause of the
750 * RST. No standard has yet been established for such
751 * data.
752 *
753 */
754
755 static void
756 print_tcp_rst_data(netdissect_options *ndo,
757 register const u_char *sp, u_int length)
758 {
759 int c;
760
761 ND_PRINT((ndo, ND_TTEST2(*sp, length) ? " [RST" : " [!RST"));
762 if (length > MAX_RST_DATA_LEN) {
763 length = MAX_RST_DATA_LEN; /* can use -X for longer */
764 ND_PRINT((ndo, "+")); /* indicate we truncate */
765 }
766 ND_PRINT((ndo, " "));
767 while (length-- && sp <= ndo->ndo_snapend) {
768 c = *sp++;
769 safeputchar(ndo, c);
770 }
771 ND_PRINT((ndo, "]"));
772 }
773
774 static void
775 print_tcp_fastopen_option(netdissect_options *ndo, register const u_char *cp,
776 u_int datalen, int exp)
777 {
778 u_int i;
779
780 if (exp)
781 ND_PRINT((ndo, "tfo"));
782
783 if (datalen == 0) {
784 /* Fast Open Cookie Request */
785 ND_PRINT((ndo, " cookiereq"));
786 } else {
787 /* Fast Open Cookie */
788 if (datalen % 2 != 0 || datalen < 4 || datalen > 16) {
789 ND_PRINT((ndo, " invalid"));
790 } else {
791 ND_PRINT((ndo, " cookie "));
792 for (i = 0; i < datalen; ++i)
793 ND_PRINT((ndo, "%02x", cp[i]));
794 }
795 }
796 }
797
798 #ifdef HAVE_LIBCRYPTO
799 USES_APPLE_DEPRECATED_API
800 static int
801 tcp_verify_signature(netdissect_options *ndo,
802 const struct ip *ip, const struct tcphdr *tp,
803 const u_char *data, int length, const u_char *rcvsig)
804 {
805 struct tcphdr tp1;
806 u_char sig[TCP_SIGLEN];
807 char zero_proto = 0;
808 MD5_CTX ctx;
809 uint16_t savecsum, tlen;
810 const struct ip6_hdr *ip6;
811 uint32_t len32;
812 uint8_t nxt;
813
814 if (data + length > ndo->ndo_snapend) {
815 ND_PRINT((ndo, "snaplen too short, "));
816 return (CANT_CHECK_SIGNATURE);
817 }
818
819 tp1 = *tp;
820
821 if (ndo->ndo_sigsecret == NULL) {
822 ND_PRINT((ndo, "shared secret not supplied with -M, "));
823 return (CANT_CHECK_SIGNATURE);
824 }
825
826 MD5_Init(&ctx);
827 /*
828 * Step 1: Update MD5 hash with IP pseudo-header.
829 */
830 if (IP_V(ip) == 4) {
831 MD5_Update(&ctx, (const char *)&ip->ip_src, sizeof(ip->ip_src));
832 MD5_Update(&ctx, (const char *)&ip->ip_dst, sizeof(ip->ip_dst));
833 MD5_Update(&ctx, (const char *)&zero_proto, sizeof(zero_proto));
834 MD5_Update(&ctx, (const char *)&ip->ip_p, sizeof(ip->ip_p));
835 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
836 tlen = htons(tlen);
837 MD5_Update(&ctx, (const char *)&tlen, sizeof(tlen));
838 } else if (IP_V(ip) == 6) {
839 ip6 = (const struct ip6_hdr *)ip;
840 MD5_Update(&ctx, (const char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
841 MD5_Update(&ctx, (const char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
842 len32 = htonl(EXTRACT_16BITS(&ip6->ip6_plen));
843 MD5_Update(&ctx, (const char *)&len32, sizeof(len32));
844 nxt = 0;
845 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
846 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
847 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
848 nxt = IPPROTO_TCP;
849 MD5_Update(&ctx, (const char *)&nxt, sizeof(nxt));
850 } else {
851 ND_PRINT((ndo, "IP version not 4 or 6, "));
852 return (CANT_CHECK_SIGNATURE);
853 }
854
855 /*
856 * Step 2: Update MD5 hash with TCP header, excluding options.
857 * The TCP checksum must be set to zero.
858 */
859 savecsum = tp1.th_sum;
860 tp1.th_sum = 0;
861 MD5_Update(&ctx, (const char *)&tp1, sizeof(struct tcphdr));
862 tp1.th_sum = savecsum;
863 /*
864 * Step 3: Update MD5 hash with TCP segment data, if present.
865 */
866 if (length > 0)
867 MD5_Update(&ctx, data, length);
868 /*
869 * Step 4: Update MD5 hash with shared secret.
870 */
871 MD5_Update(&ctx, ndo->ndo_sigsecret, strlen(ndo->ndo_sigsecret));
872 MD5_Final(sig, &ctx);
873
874 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
875 return (SIGNATURE_VALID);
876 else
877 return (SIGNATURE_INVALID);
878 }
879 USES_APPLE_RST
880 #endif /* HAVE_LIBCRYPTO */
881
882 /*
883 * Local Variables:
884 * c-style: whitesmith
885 * c-basic-offset: 8
886 * End:
887 */