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