]> The Tcpdump Group git mirrors - tcpdump/blob - print-tcp.c
clean up the LCP Auth option printer, remove printf() chains and make use of tok2str...
[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 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that: (1) source code distributions
7 * retain the above copyright notice and this paragraph in its entirety, (2)
8 * distributions including binary code include the above copyright notice and
9 * this paragraph in its entirety in the documentation or other materials
10 * provided with the distribution, and (3) all advertising materials mentioning
11 * features or use of this software display the following acknowledgement:
12 * ``This product includes software developed by the University of California,
13 * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
14 * the University nor the names of its contributors may be used to endorse
15 * or promote products derived from this software without specific prior
16 * written permission.
17 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
20 */
21
22 #ifndef lint
23 static const char rcsid[] _U_ =
24 "@(#) $Header: /tcpdump/master/tcpdump/print-tcp.c,v 1.119 2004-12-27 00:41:31 guy Exp $ (LBL)";
25 #endif
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <tcpdump-stdinc.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36
37 #include "interface.h"
38 #include "addrtoname.h"
39 #include "extract.h"
40
41 #include "tcp.h"
42
43 #include "ip.h"
44 #ifdef INET6
45 #include "ip6.h"
46 #endif
47 #include "ipproto.h"
48 #include "rpc_auth.h"
49 #include "rpc_msg.h"
50
51 #include "nameser.h"
52
53 #ifdef HAVE_LIBCRYPTO
54 #include <openssl/md5.h>
55
56 #define SIGNATURE_VALID 0
57 #define SIGNATURE_INVALID 1
58 #define CANT_CHECK_SIGNATURE 2
59
60 static int tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
61 const u_char *data, int length, const u_char *rcvsig);
62 #endif
63
64 static void print_tcp_rst_data(register const u_char *sp, u_int length);
65
66 #define MAX_RST_DATA_LEN 30
67
68
69 struct tha {
70 #ifndef INET6
71 struct in_addr src;
72 struct in_addr dst;
73 #else
74 struct in6_addr src;
75 struct in6_addr dst;
76 #endif /*INET6*/
77 u_int port;
78 };
79
80 struct tcp_seq_hash {
81 struct tcp_seq_hash *nxt;
82 struct tha addr;
83 tcp_seq seq;
84 tcp_seq ack;
85 };
86
87 #define TSEQ_HASHSIZE 919
88
89 /* These tcp optinos do not have the size octet */
90 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
91
92 static struct tcp_seq_hash tcp_seq_hash[TSEQ_HASHSIZE];
93
94
95 #ifndef TELNET_PORT
96 #define TELNET_PORT 23
97 #endif
98 #ifndef BGP_PORT
99 #define BGP_PORT 179
100 #endif
101 #define NETBIOS_SSN_PORT 139
102 #ifndef PPTP_PORT
103 #define PPTP_PORT 1723
104 #endif
105 #define BEEP_PORT 10288
106 #ifndef NFS_PORT
107 #define NFS_PORT 2049
108 #endif
109 #define MSDP_PORT 639
110 #define LDP_PORT 646
111
112 static int tcp_cksum(register const struct ip *ip,
113 register const struct tcphdr *tp,
114 register u_int len)
115 {
116 union phu {
117 struct phdr {
118 u_int32_t src;
119 u_int32_t dst;
120 u_char mbz;
121 u_char proto;
122 u_int16_t len;
123 } ph;
124 u_int16_t pa[6];
125 } phu;
126 const u_int16_t *sp;
127
128 /* pseudo-header.. */
129 phu.ph.len = htons((u_int16_t)len);
130 phu.ph.mbz = 0;
131 phu.ph.proto = IPPROTO_TCP;
132 memcpy(&phu.ph.src, &ip->ip_src.s_addr, sizeof(u_int32_t));
133 if (IP_HL(ip) == 5)
134 memcpy(&phu.ph.dst, &ip->ip_dst.s_addr, sizeof(u_int32_t));
135 else
136 phu.ph.dst = ip_finddst(ip);
137
138 sp = &phu.pa[0];
139 return in_cksum((u_short *)tp, len,
140 sp[0]+sp[1]+sp[2]+sp[3]+sp[4]+sp[5]);
141 }
142
143 #ifdef INET6
144 static int tcp6_cksum(const struct ip6_hdr *ip6, const struct tcphdr *tp,
145 u_int len)
146 {
147 size_t i;
148 register const u_int16_t *sp;
149 u_int32_t sum;
150 union {
151 struct {
152 struct in6_addr ph_src;
153 struct in6_addr ph_dst;
154 u_int32_t ph_len;
155 u_int8_t ph_zero[3];
156 u_int8_t ph_nxt;
157 } ph;
158 u_int16_t pa[20];
159 } phu;
160
161 /* pseudo-header */
162 memset(&phu, 0, sizeof(phu));
163 phu.ph.ph_src = ip6->ip6_src;
164 phu.ph.ph_dst = ip6->ip6_dst;
165 phu.ph.ph_len = htonl(len);
166 phu.ph.ph_nxt = IPPROTO_TCP;
167
168 sum = 0;
169 for (i = 0; i < sizeof(phu.pa) / sizeof(phu.pa[0]); i++)
170 sum += phu.pa[i];
171
172 sp = (const u_int16_t *)tp;
173
174 for (i = 0; i < (len & ~1); i += 2)
175 sum += *sp++;
176
177 if (len & 1)
178 sum += htons((*(const u_int8_t *)sp) << 8);
179
180 while (sum > 0xffff)
181 sum = (sum & 0xffff) + (sum >> 16);
182 sum = ~sum & 0xffff;
183
184 return (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 if ((flags = tp->th_flags) & (TH_SYN|TH_FIN|TH_RST|TH_PUSH|
294 TH_ECNECHO|TH_CWR)) {
295 if (flags & TH_SYN)
296 putchar('S');
297 if (flags & TH_FIN)
298 putchar('F');
299 if (flags & TH_RST)
300 putchar('R');
301 if (flags & TH_PUSH)
302 putchar('P');
303 if (flags & TH_CWR)
304 putchar('W'); /* congestion _W_indow reduced (ECN) */
305 if (flags & TH_ECNECHO)
306 putchar('E'); /* ecn _E_cho sent (ECN) */
307 } else
308 putchar('.');
309
310 if (!Sflag && (flags & TH_ACK)) {
311 register struct tcp_seq_hash *th;
312 const void *src, *dst;
313 register int rev;
314 struct tha tha;
315 /*
316 * Find (or record) the initial sequence numbers for
317 * this conversation. (we pick an arbitrary
318 * collating order so there's only one entry for
319 * both directions).
320 */
321 #ifdef INET6
322 memset(&tha, 0, sizeof(tha));
323 rev = 0;
324 if (ip6) {
325 src = &ip6->ip6_src;
326 dst = &ip6->ip6_dst;
327 if (sport > dport)
328 rev = 1;
329 else if (sport == dport) {
330 if (memcmp(src, dst, sizeof ip6->ip6_dst) > 0)
331 rev = 1;
332 }
333 if (rev) {
334 memcpy(&tha.src, dst, sizeof ip6->ip6_dst);
335 memcpy(&tha.dst, src, sizeof ip6->ip6_src);
336 tha.port = dport << 16 | sport;
337 } else {
338 memcpy(&tha.dst, dst, sizeof ip6->ip6_dst);
339 memcpy(&tha.src, src, sizeof ip6->ip6_src);
340 tha.port = sport << 16 | dport;
341 }
342 } else {
343 src = &ip->ip_src;
344 dst = &ip->ip_dst;
345 if (sport > dport)
346 rev = 1;
347 else if (sport == dport) {
348 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
349 rev = 1;
350 }
351 if (rev) {
352 memcpy(&tha.src, dst, sizeof ip->ip_dst);
353 memcpy(&tha.dst, src, sizeof ip->ip_src);
354 tha.port = dport << 16 | sport;
355 } else {
356 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
357 memcpy(&tha.src, src, sizeof ip->ip_src);
358 tha.port = sport << 16 | dport;
359 }
360 }
361 #else
362 rev = 0;
363 src = &ip->ip_src;
364 dst = &ip->ip_dst;
365 if (sport > dport)
366 rev = 1;
367 else if (sport == dport) {
368 if (memcmp(src, dst, sizeof ip->ip_dst) > 0)
369 rev = 1;
370 }
371 if (rev) {
372 memcpy(&tha.src, dst, sizeof ip->ip_dst);
373 memcpy(&tha.dst, src, sizeof ip->ip_src);
374 tha.port = dport << 16 | sport;
375 } else {
376 memcpy(&tha.dst, dst, sizeof ip->ip_dst);
377 memcpy(&tha.src, src, sizeof ip->ip_src);
378 tha.port = sport << 16 | dport;
379 }
380 #endif
381
382 threv = rev;
383 for (th = &tcp_seq_hash[tha.port % TSEQ_HASHSIZE];
384 th->nxt; th = th->nxt)
385 if (memcmp((char *)&tha, (char *)&th->addr,
386 sizeof(th->addr)) == 0)
387 break;
388
389 if (!th->nxt || (flags & TH_SYN)) {
390 /* didn't find it or new conversation */
391 if (th->nxt == NULL) {
392 th->nxt = (struct tcp_seq_hash *)
393 calloc(1, sizeof(*th));
394 if (th->nxt == NULL)
395 error("tcp_print: calloc");
396 }
397 th->addr = tha;
398 if (rev)
399 th->ack = seq, th->seq = ack - 1;
400 else
401 th->seq = seq, th->ack = ack - 1;
402 } else {
403 if (rev)
404 seq -= th->ack, ack -= th->seq;
405 else
406 seq -= th->seq, ack -= th->ack;
407 }
408
409 thseq = th->seq;
410 thack = th->ack;
411 } else {
412 /*fool gcc*/
413 thseq = thack = threv = 0;
414 }
415 if (hlen > length) {
416 (void)printf(" [bad hdr length %u - too long, > %u]",
417 hlen, length);
418 return;
419 }
420
421 if (IP_V(ip) == 4 && vflag && !fragmented) {
422 u_int16_t sum, tcp_sum;
423 if (TTEST2(tp->th_sport, length)) {
424 sum = tcp_cksum(ip, tp, length);
425
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 #ifdef INET6
435 if (IP_V(ip) == 6 && ip6->ip6_plen && vflag && !fragmented) {
436 u_int16_t sum,tcp_sum;
437 if (TTEST2(tp->th_sport, length)) {
438 sum = tcp6_cksum(ip6, tp, length);
439 (void)printf(", cksum 0x%04x",EXTRACT_16BITS(&tp->th_sum));
440 if (sum != 0) {
441 tcp_sum = EXTRACT_16BITS(&tp->th_sum);
442 (void)printf(" (incorrect (-> 0x%04x),",in_cksum_shouldbe(tcp_sum, sum));
443 } else
444 (void)printf(" (correct),");
445
446 }
447 }
448 #endif
449
450 length -= hlen;
451 if (vflag > 1 || length > 0 || flags & (TH_SYN | TH_FIN | TH_RST))
452 (void)printf(" %u:%u(%u)", seq, seq + length, length);
453 if (flags & TH_ACK)
454 (void)printf(" ack %u", ack);
455
456 (void)printf(" win %d", win);
457
458 if (flags & TH_URG)
459 (void)printf(" urg %d", urp);
460 /*
461 * Handle any options.
462 */
463 if (hlen > sizeof(*tp)) {
464 register const u_char *cp;
465 register u_int i, opt, datalen;
466 register u_int len;
467
468 hlen -= sizeof(*tp);
469 cp = (const u_char *)tp + sizeof(*tp);
470 putchar(' ');
471 ch = '<';
472 while (hlen > 0) {
473 putchar(ch);
474 TCHECK(*cp);
475 opt = *cp++;
476 if (ZEROLENOPT(opt))
477 len = 1;
478 else {
479 TCHECK(*cp);
480 len = *cp++; /* total including type, len */
481 if (len < 2 || len > hlen)
482 goto bad;
483 --hlen; /* account for length byte */
484 }
485 --hlen; /* account for type byte */
486 datalen = 0;
487
488 /* Bail if "l" bytes of data are not left or were not captured */
489 #define LENCHECK(l) { if ((l) > hlen) goto bad; TCHECK2(*cp, l); }
490
491 switch (opt) {
492
493 case TCPOPT_MAXSEG:
494 (void)printf("mss");
495 datalen = 2;
496 LENCHECK(datalen);
497 (void)printf(" %u", EXTRACT_16BITS(cp));
498
499 break;
500
501 case TCPOPT_EOL:
502 (void)printf("eol");
503 break;
504
505 case TCPOPT_NOP:
506 (void)printf("nop");
507 break;
508
509 case TCPOPT_WSCALE:
510 (void)printf("wscale");
511 datalen = 1;
512 LENCHECK(datalen);
513 (void)printf(" %u", *cp);
514 break;
515
516 case TCPOPT_SACKOK:
517 (void)printf("sackOK");
518 break;
519
520 case TCPOPT_SACK:
521 (void)printf("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(" sack %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 (void)printf(" ");
544 }
545 break;
546
547 case TCPOPT_ECHO:
548 (void)printf("echo");
549 datalen = 4;
550 LENCHECK(datalen);
551 (void)printf(" %u", EXTRACT_32BITS(cp));
552 break;
553
554 case TCPOPT_ECHOREPLY:
555 (void)printf("echoreply");
556 datalen = 4;
557 LENCHECK(datalen);
558 (void)printf(" %u", EXTRACT_32BITS(cp));
559 break;
560
561 case TCPOPT_TIMESTAMP:
562 (void)printf("timestamp");
563 datalen = 8;
564 LENCHECK(4);
565 (void)printf(" %u", EXTRACT_32BITS(cp));
566 LENCHECK(datalen);
567 (void)printf(" %u", EXTRACT_32BITS(cp + 4));
568 break;
569
570 case TCPOPT_CC:
571 (void)printf("cc");
572 datalen = 4;
573 LENCHECK(datalen);
574 (void)printf(" %u", EXTRACT_32BITS(cp));
575 break;
576
577 case TCPOPT_CCNEW:
578 (void)printf("ccnew");
579 datalen = 4;
580 LENCHECK(datalen);
581 (void)printf(" %u", EXTRACT_32BITS(cp));
582 break;
583
584 case TCPOPT_CCECHO:
585 (void)printf("ccecho");
586 datalen = 4;
587 LENCHECK(datalen);
588 (void)printf(" %u", EXTRACT_32BITS(cp));
589 break;
590
591 case TCPOPT_SIGNATURE:
592 (void)printf("md5:");
593 datalen = TCP_SIGLEN;
594 LENCHECK(datalen);
595 #ifdef HAVE_LIBCRYPTO
596 switch (tcp_verify_signature(ip, tp,
597 bp + TH_OFF(tp) * 4, length, cp)) {
598
599 case SIGNATURE_VALID:
600 (void)printf("valid");
601 break;
602
603 case SIGNATURE_INVALID:
604 (void)printf("invalid");
605 break;
606
607 case CANT_CHECK_SIGNATURE:
608 (void)printf("can't check - ");
609 for (i = 0; i < TCP_SIGLEN; ++i)
610 (void)printf("%02x", cp[i]);
611 break;
612 }
613 #else
614 for (i = 0; i < TCP_SIGLEN; ++i)
615 (void)printf("%02x", cp[i]);
616 #endif
617 break;
618
619 default:
620 (void)printf("opt-%u:", opt);
621 datalen = len - 2;
622 for (i = 0; i < datalen; ++i) {
623 LENCHECK(i);
624 (void)printf("%02x", cp[i]);
625 }
626 break;
627 }
628
629 /* Account for data printed */
630 cp += datalen;
631 hlen -= datalen;
632
633 /* Check specification against observed length */
634 ++datalen; /* option octet */
635 if (!ZEROLENOPT(opt))
636 ++datalen; /* size octet */
637 if (datalen != len)
638 (void)printf("[len %d]", len);
639 ch = ',';
640 if (opt == TCPOPT_EOL)
641 break;
642 }
643 putchar('>');
644 }
645
646 if (length <= 0)
647 return;
648
649 /*
650 * Decode payload if necessary.
651 */
652 bp += TH_OFF(tp) * 4;
653 if (flags & TH_RST) {
654 if (vflag)
655 print_tcp_rst_data(bp, length);
656 } else {
657 if (sport == TELNET_PORT || dport == TELNET_PORT) {
658 if (!qflag && vflag)
659 telnet_print(bp, length);
660 } else if (sport == BGP_PORT || dport == BGP_PORT)
661 bgp_print(bp, length);
662 else if (sport == PPTP_PORT || dport == PPTP_PORT)
663 pptp_print(bp);
664 #ifdef TCPDUMP_DO_SMB
665 else if (sport == NETBIOS_SSN_PORT || dport == NETBIOS_SSN_PORT)
666 nbt_tcp_print(bp, length);
667 #endif
668 else if (sport == BEEP_PORT || dport == BEEP_PORT)
669 beep_print(bp, length);
670 else if (length > 2 &&
671 (sport == NAMESERVER_PORT || dport == NAMESERVER_PORT ||
672 sport == MULTICASTDNS_PORT || dport == MULTICASTDNS_PORT)) {
673 /*
674 * TCP DNS query has 2byte length at the head.
675 * XXX packet could be unaligned, it can go strange
676 */
677 ns_print(bp + 2, length - 2, 0);
678 } else if (sport == MSDP_PORT || dport == MSDP_PORT) {
679 msdp_print(bp, length);
680 }
681 else if (length > 0 && (sport == LDP_PORT || dport == LDP_PORT)) {
682 ldp_print(bp, length);
683 }
684 }
685 return;
686 bad:
687 fputs("[bad opt]", stdout);
688 if (ch != '\0')
689 putchar('>');
690 return;
691 trunc:
692 fputs("[|tcp]", stdout);
693 if (ch != '\0')
694 putchar('>');
695 }
696
697 /*
698 * RFC1122 says the following on data in RST segments:
699 *
700 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
701 *
702 * A TCP SHOULD allow a received RST segment to include data.
703 *
704 * DISCUSSION
705 * It has been suggested that a RST segment could contain
706 * ASCII text that encoded and explained the cause of the
707 * RST. No standard has yet been established for such
708 * data.
709 *
710 */
711
712 static void
713 print_tcp_rst_data(register const u_char *sp, u_int length)
714 {
715 int c;
716
717 if (TTEST2(*sp, length))
718 printf(" [RST");
719 else
720 printf(" [!RST");
721 if (length > MAX_RST_DATA_LEN) {
722 length = MAX_RST_DATA_LEN; /* can use -X for longer */
723 putchar('+'); /* indicate we truncate */
724 }
725 putchar(' ');
726 while (length-- && sp <= snapend) {
727 c = *sp++;
728 safeputchar(c);
729 }
730 putchar(']');
731 }
732
733 #ifdef HAVE_LIBCRYPTO
734 static int
735 tcp_verify_signature(const struct ip *ip, const struct tcphdr *tp,
736 const u_char *data, int length, const u_char *rcvsig)
737 {
738 struct tcphdr tp1;
739 char sig[TCP_SIGLEN];
740 char zero_proto = 0;
741 MD5_CTX ctx;
742 u_int16_t savecsum, tlen;
743 #ifdef INET6
744 struct ip6_hdr *ip6;
745 #endif
746 u_int32_t len32;
747 u_int8_t nxt;
748
749 tp1 = *tp;
750
751 if (tcpmd5secret == NULL)
752 return (CANT_CHECK_SIGNATURE);
753
754 MD5_Init(&ctx);
755 /*
756 * Step 1: Update MD5 hash with IP pseudo-header.
757 */
758 if (IP_V(ip) == 4) {
759 MD5_Update(&ctx, (char *)&ip->ip_src, sizeof(ip->ip_src));
760 MD5_Update(&ctx, (char *)&ip->ip_dst, sizeof(ip->ip_dst));
761 MD5_Update(&ctx, (char *)&zero_proto, sizeof(zero_proto));
762 MD5_Update(&ctx, (char *)&ip->ip_p, sizeof(ip->ip_p));
763 tlen = EXTRACT_16BITS(&ip->ip_len) - IP_HL(ip) * 4;
764 tlen = htons(tlen);
765 MD5_Update(&ctx, (char *)&tlen, sizeof(tlen));
766 #ifdef INET6
767 } else if (IP_V(ip) == 6) {
768 ip6 = (struct ip6_hdr *)ip;
769 MD5_Update(&ctx, (char *)&ip6->ip6_src, sizeof(ip6->ip6_src));
770 MD5_Update(&ctx, (char *)&ip6->ip6_dst, sizeof(ip6->ip6_dst));
771 len32 = htonl(ntohs(ip6->ip6_plen));
772 MD5_Update(&ctx, (char *)&len32, sizeof(len32));
773 nxt = 0;
774 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
775 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
776 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
777 nxt = IPPROTO_TCP;
778 MD5_Update(&ctx, (char *)&nxt, sizeof(nxt));
779 #endif
780 } else
781 return (CANT_CHECK_SIGNATURE);
782
783 /*
784 * Step 2: Update MD5 hash with TCP header, excluding options.
785 * The TCP checksum must be set to zero.
786 */
787 savecsum = tp1.th_sum;
788 tp1.th_sum = 0;
789 MD5_Update(&ctx, (char *)&tp1, sizeof(struct tcphdr));
790 tp1.th_sum = savecsum;
791 /*
792 * Step 3: Update MD5 hash with TCP segment data, if present.
793 */
794 if (length > 0)
795 MD5_Update(&ctx, data, length);
796 /*
797 * Step 4: Update MD5 hash with shared secret.
798 */
799 MD5_Update(&ctx, tcpmd5secret, strlen(tcpmd5secret));
800 MD5_Final(sig, &ctx);
801
802 if (memcmp(rcvsig, sig, 16))
803 return (SIGNATURE_VALID);
804 else
805 return (SIGNATURE_INVALID);
806 }
807 #endif /* HAVE_LIBCRYPTO */