]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
From Alexander Dupuy: if the separator character is null, it means "no
[tcpdump] / print-tcp.c
1 /*
2 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Copyright (c) 1999-2004 The tcpdump.org project
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that: (1) source code distributions
9 * retain the above copyright notice and this paragraph in its entirety, (2)
10 * distributions including binary code include the above copyright notice and
11 * this paragraph in its entirety in the documentation or other materials
12 * provided with the distribution, and (3) all advertising materials mentioning
13 * features or use of this software display the following acknowledgement:
14 * ``This product includes software developed by the University of California,
15 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
16 * the University nor the names of its contributors may be used to endorse
17 * or promote products derived from this software without specific prior
18 * written permission.
19 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
22 */
23
24 #ifndef lint
25 static const char rcsid[] _U_ =
26 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.129 2007-04-03 20:02:56 guy Exp $ (LBL)";
27 #endif
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <tcpdump-stdinc.h>
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38
39 #include "interface.h"
40 #include "addrtoname.h"
41 #include "extract.h"
42
43 #include "tcp.h"
44
45 #include "ip.h"
46 #ifdef INET6
47 #include "ip6.h"
48 #endif
49 #include "ipproto.h"
50 #include "rpc_auth.h"
51 #include "rpc_msg.h"
52
53 #include "nameser.h"
54
55 #ifdef HAVE_LIBCRYPTO
56 #include <openssl/md5.h>
57
58 #define SIGNATURE_VALID 0
59 #define SIGNATURE_INVALID 1
60 #define CANT_CHECK_SIGNATURE 2
61
62 static int tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
63 const u_char *data, int length, const u_char *rcvsig);
64 #endif
65
66 static void print_tcp_rst_data(register const u_char *sp, u_int length);
67
68 #define MAX_RST_DATA_LEN 30
69
70
71 struct tha {
72 #ifndef INET6
73 struct in_addr src;
74 struct in_addr dst;
75 #else
76 struct in6_addr src;
77 struct in6_addr dst;
78 #endif /*INET6*/
79 u_int port;
80 };
81
82 struct tcp_seq_hash {
83 struct tcp_seq_hash *nxt;
84 struct tha addr;
85 tcp_seq seq;
86 tcp_seq ack;
87 };
88
89 #define TSEQ_HASHSIZE 919
90
91 /* These tcp optinos do not have the size octet */
92 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
93
94 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
95
96 struct tok tcp_flag_values[] = {
97 { TH_FIN, "F" },
98 { TH_SYN, "S" },
99 { TH_RST, "R" },
100 { TH_PUSH, "P" },
101 { TH_ACK, "." },
102 { TH_URG, "U" },
103 { TH_ECNECHO, "E" },
104 { TH_CWR, "W" },
105 { 0, NULL }
106 };
107
108 struct tok tcp_option_values[] = {
109 { TCPOPT_EOL, "eol" },
110 { TCPOPT_NOP, "nop" },
111 { TCPOPT_MAXSEG, "mss" },
112 { TCPOPT_WSCALE, "wscale" },
113 { TCPOPT_SACKOK, "sackOK" },
114 { TCPOPT_SACK, "sack" },
115 { TCPOPT_ECHO, "echo" },
116 { TCPOPT_ECHOREPLY, "echoreply" },
117 { TCPOPT_TIMESTAMP, "TS" },
118 { TCPOPT_CC, "cc" },
119 { TCPOPT_CCNEW, "ccnew" },
120 { TCPOPT_CCECHO, "" },
121 { TCPOPT_SIGNATURE, "md5" },
122 { TCPOPT_AUTH, "enhanced auth" },
123 { 0, NULL }
124 };
125
126 static int tcp_cksum(register const struct ip *ip,
127 register const struct tcphdr *tp,
128 register u_int len)
129 {
130 union phu {
131 struct phdr {
132 u_int32_t src;
133 u_int32_t dst;
134 u_char mbz;
135 u_char proto;
136 u_int16_t len;
137 } ph;
138 u_int16_t pa[6];
139 } phu;
140 const u_int16_t *sp;
141
142 /* pseudo-header.. */
143 phu.ph.len = htons((u_int16_t)len);
144 phu.ph.mbz = 0;
145 phu.ph.proto = IPPROTO_TCP;
146 memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
147 if (IP_HL(ip) == 5)
148 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
149 else
150 phu.ph.dst = ip_finddst(ip);
151
152 sp = &phu.pa[0];
153 return in_cksum((u_short *)tp, len,
154 sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5]);
155 }
156
157 #ifdef INET6
158 static int tcp6_cksum(const struct ip6_hdr *ip6, const struct tcphdr *tp,
159 u_int len)
160 {
161 size_t i;
162 u_int32_t sum = 0;
163 union {
164 struct {
165 struct in6_addr ph_src;
166 struct in6_addr ph_dst;
167 u_int32_t ph_len;
168 u_int8_t ph_zero[3];
169 u_int8_t ph_nxt;
170 } ph;
171 u_int16_t pa[20];
172 } phu;
173
174 /* pseudo-header */
175 memset(&phu, 0, sizeof(phu));
176 phu.ph.ph_src = ip6->ip6_src;
177 phu.ph.ph_dst = ip6->ip6_dst;
178 phu.ph.ph_len = htonl(len);
179 phu.ph.ph_nxt = IPPROTO_TCP;
180
181 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
182 sum += phu.pa[i];
183
184 return in_cksum((u_short *)tp, len, sum);
185 }
186 #endif
187
188 void
189 tcp_print(register const u_char *bp, register u_int length,
190 register const u_char *bp2, int fragmented)
191 {
192 register const struct tcphdr *tp;
193 register const struct ip *ip;
194 register u_char flags;
195 register u_int hlen;
196 register char ch;
197 u_int16_t sport, dport, win, urp;
198 u_int32_t seq, ack, thseq, thack;
199 int threv;
200 #ifdef INET6
201 register const struct ip6_hdr *ip6;
202 #endif
203
204 tp = (struct tcphdr *)bp;
205 ip = (struct ip *)bp2;
206 #ifdef INET6
207 if (IP_V(ip) == 6)
208 ip6 = (struct ip6_hdr *)bp2;
209 else
210 ip6 = NULL;
211 #endif /*INET6*/
212 ch = '\0';
213 if (!TTEST(tp->th_dport)) {
214 (void)printf("%s > %s: [|tcp]",
215 ipaddr_string(&ip->ip_src),
216 ipaddr_string(&ip->ip_dst));
217 return;
218 }
219
220 sport = EXTRACT_16BITS(&tp->th_sport);
221 dport = EXTRACT_16BITS(&tp->th_dport);
222
223 hlen = TH_OFF(tp) * 4;
224
225 /*
226 * If data present, header length valid, and NFS port used,
227 * assume NFS.
228 * Pass offset of data plus 4 bytes for RPC TCP msg length
229 * to NFS print routines.
230 */
231 if (!qflag && hlen >= sizeof(*tp) && hlen <= length) {
232 if ((u_char *)tp + 4 + sizeof(struct sunrpc_msg) <= snapend &&
233 dport == NFS_PORT) {
234 nfsreq_print((u_char *)tp + hlen + 4, length - hlen,
235 (u_char *)ip);
236 return;
237 } else if ((u_char *)tp + 4 + sizeof(struct sunrpc_msg)
238 <= snapend &&
239 sport == NFS_PORT) {
240 nfsreply_print((u_char *)tp + hlen + 4, length - hlen,
241 (u_char *)ip);
242 return;
243 }
244 }
245 #ifdef INET6
246 if (ip6) {
247 if (ip6->ip6_nxt == IPPROTO_TCP) {
248 (void)printf("%s.%s > %s.%s: ",
249 ip6addr_string(&ip6->ip6_src),
250 tcpport_string(sport),
251 ip6addr_string(&ip6->ip6_dst),
252 tcpport_string(dport));
253 } else {
254 (void)printf("%s > %s: ",
255 tcpport_string(sport), tcpport_string(dport));
256 }
257 } else
258 #endif /*INET6*/
259 {
260 if (ip->ip_p == IPPROTO_TCP) {
261 (void)printf("%s.%s > %s.%s: ",
262 ipaddr_string(&ip->ip_src),
263 tcpport_string(sport),
264 ipaddr_string(&ip->ip_dst),
265 tcpport_string(dport));
266 } else {
267 (void)printf("%s > %s: ",
268 tcpport_string(sport), tcpport_string(dport));
269 }
270 }
271
272 if (hlen < sizeof(*tp)) {
273 (void)printf(" tcp %d [bad hdr length %u - too short, < %lu]",
274 length - hlen, hlen, (unsigned long)sizeof(*tp));
275 return;
276 }
277
278 TCHECK(*tp);
279
280 seq = EXTRACT_32BITS(&tp->th_seq);
281 ack = EXTRACT_32BITS(&tp->th_ack);
282 win = EXTRACT_16BITS(&tp->th_win);
283 urp = EXTRACT_16BITS(&tp->th_urp);
284
285 if (qflag) {
286 (void)printf("tcp %d", length - hlen);
287 if (hlen > length) {
288 (void)printf(" [bad hdr length %u - too long, > %u]",
289 hlen, length);
290 }
291 return;
292 }
293
294 flags = tp->th_flags;
295 printf("Flags [%s]", bittok2str_nosep(tcp_flag_values, "none", flags));
296
297 if (!Sflag && (flags & TH_ACK)) {
298 register struct tcp_seq_hash *th;
299 const void *src, *dst;
300 register int rev;
301 struct tha tha;
302 /*
303 * Find (or record) the initial sequence numbers for
304 * this conversation. (we pick an arbitrary
305 * collating order so there's only one entry for
306 * both directions).
307 */
308 #ifdef INET6
309 memset(&tha, 0, sizeof(tha));
310 rev = 0;
311 if (ip6) {
312 src = &ip6->ip6_src;
313 dst = &ip6->ip6_dst;
314 if (sport > dport)
315 rev = 1;
316 else if (sport == dport) {
317 if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0)
318 rev = 1;
319 }
320 if (rev) {
321 memcpy(&tha.src, dst, sizeof ip6->ip6_dst);
322 memcpy(&tha.dst, src, sizeof ip6->ip6_src);
323 tha.port = dport << 16 | sport;
324 } else {
325 memcpy(&tha.dst, dst, sizeof ip6->ip6_dst);
326 memcpy(&tha.src, src, sizeof ip6->ip6_src);
327 tha.port = sport << 16 | dport;
328 }
329 } else {
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 = tcp6_cksum(ip6, tp, length);
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 || 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 default:
597 datalen = len - 2;
598 for (i = 0; i < datalen; ++i) {
599 LENCHECK(i);
600 (void)printf("%02x", cp[i]);
601 }
602 break;
603 }
604
605 /* Account for data printed */
606 cp += datalen;
607 hlen -= datalen;
608
609 /* Check specification against observed length */
610 ++datalen; /* option octet */
611 if (!ZEROLENOPT(opt))
612 ++datalen; /* size octet */
613 if (datalen != len)
614 (void)printf("[len %d]", len);
615 ch = ',';
616 if (opt == TCPOPT_EOL)
617 break;
618 }
619 putchar(']');
620 }
621
622 /*
623 * Print length field before crawling down the stack.
624 */
625 printf(", length %u", length);
626
627 if (length <= 0)
628 return;
629
630 /*
631 * Decode payload if necessary.
632 */
633 bp += TH_OFF(tp) * 4;
634 if ((flags & TH_RST) && vflag) {
635 print_tcp_rst_data(bp, length);
636 return;
637 }
638
639 if (sport == TELNET_PORT || dport == TELNET_PORT) {
640 if (!qflag && vflag)
641 telnet_print(bp, length);
642 } else if (sport == BGP_PORT || dport == BGP_PORT)
643 bgp_print(bp, length);
644 else if (sport == PPTP_PORT || dport == PPTP_PORT)
645 pptp_print(bp);
646 #ifdef TCPDUMP_DO_SMB
647 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
648 nbt_tcp_print(bp, length);
649 #endif
650 else if (sport == BEEP_PORT || dport == BEEP_PORT)
651 beep_print(bp, length);
652 else if (length > 2 &&
653 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT ||
654 sport == MULTICASTDNS_PORT || dport == MULTICASTDNS_PORT)) {
655 /*
656 * TCP DNS query has 2byte length at the head.
657 * XXX packet could be unaligned, it can go strange
658 */
659 ns_print(bp + 2, length - 2, 0);
660 } else if (sport == MSDP_PORT || dport == MSDP_PORT) {
661 msdp_print(bp, length);
662 }
663 else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
664 ldp_print(bp, length);
665 }
666
667 return;
668 bad:
669 fputs("[bad opt]", stdout);
670 if (ch != '\0')
671 putchar('>');
672 return;
673 trunc:
674 fputs("[|tcp]", stdout);
675 if (ch != '\0')
676 putchar('>');
677 }
678
679 /*
680 * RFC1122 says the following on data in RST segments:
681 *
682 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
683 *
684 * A TCP SHOULD allow a received RST segment to include data.
685 *
686 * DISCUSSION
687 * It has been suggested that a RST segment could contain
688 * ASCII text that encoded and explained the cause of the
689 * RST. No standard has yet been established for such
690 * data.
691 *
692 */
693
694 static void
695 print_tcp_rst_data(register const u_char *sp, u_int length)
696 {
697 int c;
698
699 if (TTEST2(*sp, length))
700 printf(" [RST");
701 else
702 printf(" [!RST");
703 if (length > MAX_RST_DATA_LEN) {
704 length = MAX_RST_DATA_LEN; /* can use -X for longer */
705 putchar('+'); /* indicate we truncate */
706 }
707 putchar(' ');
708 while (length-- && sp <= snapend) {
709 c = *sp++;
710 safeputchar(c);
711 }
712 putchar(']');
713 }
714
715 #ifdef HAVE_LIBCRYPTO
716 static int
717 tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
718 const u_char *data, int length, const u_char *rcvsig)
719 {
720 struct tcphdr tp1;
721 u_char sig[TCP_SIGLEN];
722 char zero_proto = 0;
723 MD5_CTX ctx;
724 u_int16_t savecsum, tlen;
725 #ifdef INET6
726 struct ip6_hdr *ip6;
727 #endif
728 u_int32_t len32;
729 u_int8_t nxt;
730
731 tp1 = *tp;
732
733 if (tcpmd5secret == NULL)
734 return (CANT_CHECK_SIGNATURE);
735
736 MD5_Init(&ctx);
737 /*
738 * Step 1: Update MD5 hash with IP pseudo-header.
739 */
740 if (IP_V(ip) == 4) {
741 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src));
742 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst));
743 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto));
744 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p));
745 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
746 tlen = htons(tlen);
747 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen));
748 #ifdef INET6
749 } else if (IP_V(ip) == 6) {
750 ip6 = (struct ip6_hdr *)ip;
751 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
752 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
753 len32 = htonl(ntohs(ip6->ip6_plen));
754 MD5_Update(&ctx, (char *)&len32, sizeof(len32));
755 nxt = 0;
756 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
757 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
758 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
759 nxt = IPPROTO_TCP;
760 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
761 #endif
762 } else
763 return (CANT_CHECK_SIGNATURE);
764
765 /*
766 * Step 2: Update MD5 hash with TCP header, excluding options.
767 * The TCP checksum must be set to zero.
768 */
769 savecsum = tp1.th_sum;
770 tp1.th_sum = 0;
771 MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr));
772 tp1.th_sum = savecsum;
773 /*
774 * Step 3: Update MD5 hash with TCP segment data, if present.
775 */
776 if (length > 0)
777 MD5_Update(&ctx, data, length);
778 /*
779 * Step 4: Update MD5 hash with shared secret.
780 */
781 MD5_Update(&ctx, tcpmd5secret, strlen(tcpmd5secret));
782 MD5_Final(sig, &ctx);
783
784 if (memcmp(rcvsig, sig, TCP_SIGLEN) == 0)
785 return (SIGNATURE_VALID);
786 else
787 return (SIGNATURE_INVALID);
788 }
789 #endif /* HAVE_LIBCRYPTO */
790
791 /*
792 * Local Variables:
793 * c-style: whitesmith
794 * c-basic-offset: 8
795 * End:
796 */