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