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