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