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