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