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