1 /* $NetBSD: print-tcp.c,v 1.9 2007/07/26 18:15:12 plunky Exp $ */
4 * Copyright (c) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997
5 * The Regents of the University of California. All rights reserved.
7 * Copyright (c) 1999-2004 The tcpdump.org project
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
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.
28 __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
35 #include <tcpdump-stdinc.h>
40 #include "interface.h"
41 #include "addrtoname.h"
55 #include <openssl/md5.h>
56 #include "signature.h"
58 static int tcp_verify_signature(netdissect_options
*ndo
,
59 const struct ip
*ip
, const struct tcphdr
*tp
,
60 const u_char
*data
, int length
, const u_char
*rcvsig
);
63 static void print_tcp_rst_data(netdissect_options
*, register const u_char
*sp
, u_int length
);
64 static void print_tcp_fastopen_option(netdissect_options
*ndo
, register const u_char
*cp
,
65 u_int datalen
, int exp
);
67 #define MAX_RST_DATA_LEN 30
77 struct tcp_seq_hash
*nxt
;
90 struct tcp_seq_hash6
{
91 struct tcp_seq_hash6
*nxt
;
98 #define TSEQ_HASHSIZE 919
100 /* These tcp optinos do not have the size octet */
101 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
103 static struct tcp_seq_hash tcp_seq_hash4
[TSEQ_HASHSIZE
];
105 static struct tcp_seq_hash6 tcp_seq_hash6
[TSEQ_HASHSIZE
];
108 static const struct tok tcp_flag_values
[] = {
120 static const struct tok tcp_option_values
[] = {
121 { TCPOPT_EOL
, "eol" },
122 { TCPOPT_NOP
, "nop" },
123 { TCPOPT_MAXSEG
, "mss" },
124 { TCPOPT_WSCALE
, "wscale" },
125 { TCPOPT_SACKOK
, "sackOK" },
126 { TCPOPT_SACK
, "sack" },
127 { TCPOPT_ECHO
, "echo" },
128 { TCPOPT_ECHOREPLY
, "echoreply" },
129 { TCPOPT_TIMESTAMP
, "TS" },
131 { TCPOPT_CCNEW
, "ccnew" },
132 { TCPOPT_CCECHO
, "" },
133 { TCPOPT_SIGNATURE
, "md5" },
134 { TCPOPT_AUTH
, "enhanced auth" },
135 { TCPOPT_UTO
, "uto" },
136 { TCPOPT_MPTCP
, "mptcp" },
137 { TCPOPT_FASTOPEN
, "tfo" },
138 { TCPOPT_EXPERIMENT2
, "exp" },
143 tcp_cksum(netdissect_options
*ndo
,
144 register const struct ip
*ip
,
145 register const struct tcphdr
*tp
,
148 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)tp
, len
, len
,
153 tcp_print(netdissect_options
*ndo
,
154 register const u_char
*bp
, register u_int length
,
155 register const u_char
*bp2
, int fragmented
)
157 register const struct tcphdr
*tp
;
158 register const struct ip
*ip
;
159 register u_char flags
;
162 uint16_t sport
, dport
, win
, urp
;
163 uint32_t seq
, ack
, thseq
, thack
;
168 register const struct ip6_hdr
*ip6
;
171 tp
= (const struct tcphdr
*)bp
;
172 ip
= (const struct ip
*)bp2
;
175 ip6
= (const struct ip6_hdr
*)bp2
;
180 if (!ND_TTEST(tp
->th_dport
)) {
181 ND_PRINT((ndo
, "%s > %s: [|tcp]",
182 ipaddr_string(ndo
, &ip
->ip_src
),
183 ipaddr_string(ndo
, &ip
->ip_dst
)));
187 sport
= EXTRACT_16BITS(&tp
->th_sport
);
188 dport
= EXTRACT_16BITS(&tp
->th_dport
);
190 hlen
= TH_OFF(tp
) * 4;
194 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
195 ND_PRINT((ndo
, "%s.%s > %s.%s: ",
196 ip6addr_string(ndo
, &ip6
->ip6_src
),
197 tcpport_string(sport
),
198 ip6addr_string(ndo
, &ip6
->ip6_dst
),
199 tcpport_string(dport
)));
201 ND_PRINT((ndo
, "%s > %s: ",
202 tcpport_string(sport
), tcpport_string(dport
)));
207 if (ip
->ip_p
== IPPROTO_TCP
) {
208 ND_PRINT((ndo
, "%s.%s > %s.%s: ",
209 ipaddr_string(ndo
, &ip
->ip_src
),
210 tcpport_string(sport
),
211 ipaddr_string(ndo
, &ip
->ip_dst
),
212 tcpport_string(dport
)));
214 ND_PRINT((ndo
, "%s > %s: ",
215 tcpport_string(sport
), tcpport_string(dport
)));
219 if (hlen
< sizeof(*tp
)) {
220 ND_PRINT((ndo
, " tcp %d [bad hdr length %u - too short, < %lu]",
221 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
)));
227 seq
= EXTRACT_32BITS(&tp
->th_seq
);
228 ack
= EXTRACT_32BITS(&tp
->th_ack
);
229 win
= EXTRACT_16BITS(&tp
->th_win
);
230 urp
= EXTRACT_16BITS(&tp
->th_urp
);
232 if (ndo
->ndo_qflag
) {
233 ND_PRINT((ndo
, "tcp %d", length
- hlen
));
235 ND_PRINT((ndo
, " [bad hdr length %u - too long, > %u]",
241 flags
= tp
->th_flags
;
242 ND_PRINT((ndo
, "Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
)));
244 if (!ndo
->ndo_Sflag
&& (flags
& TH_ACK
)) {
246 * Find (or record) the initial sequence numbers for
247 * this conversation. (we pick an arbitrary
248 * collating order so there's only one entry for
254 register struct tcp_seq_hash6
*th
;
255 struct tcp_seq_hash6
*tcp_seq_hash
;
256 const struct in6_addr
*src
, *dst
;
259 tcp_seq_hash
= tcp_seq_hash6
;
264 else if (sport
== dport
) {
265 if (UNALIGNED_MEMCMP(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
269 UNALIGNED_MEMCPY(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
270 UNALIGNED_MEMCPY(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
271 tha
.port
= dport
<< 16 | sport
;
273 UNALIGNED_MEMCPY(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
274 UNALIGNED_MEMCPY(&tha
.src
, src
, sizeof ip6
->ip6_src
);
275 tha
.port
= sport
<< 16 | dport
;
278 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
279 th
->nxt
; th
= th
->nxt
)
280 if (memcmp((char *)&tha
, (char *)&th
->addr
,
281 sizeof(th
->addr
)) == 0)
284 if (!th
->nxt
|| (flags
& TH_SYN
)) {
285 /* didn't find it or new conversation */
286 if (th
->nxt
== NULL
) {
287 th
->nxt
= (struct tcp_seq_hash6
*)
288 calloc(1, sizeof(*th
));
290 error("tcp_print: calloc");
294 th
->ack
= seq
, th
->seq
= ack
- 1;
296 th
->seq
= seq
, th
->ack
= ack
- 1;
299 seq
-= th
->ack
, ack
-= th
->seq
;
301 seq
-= th
->seq
, ack
-= th
->ack
;
310 register struct tcp_seq_hash
*th
;
311 struct tcp_seq_hash
*tcp_seq_hash
;
312 const struct in_addr
*src
, *dst
;
315 tcp_seq_hash
= tcp_seq_hash4
;
320 else if (sport
== dport
) {
321 if (UNALIGNED_MEMCMP(src
, dst
, sizeof ip
->ip_dst
) > 0)
325 UNALIGNED_MEMCPY(&tha
.src
, dst
, sizeof ip
->ip_dst
);
326 UNALIGNED_MEMCPY(&tha
.dst
, src
, sizeof ip
->ip_src
);
327 tha
.port
= dport
<< 16 | sport
;
329 UNALIGNED_MEMCPY(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
330 UNALIGNED_MEMCPY(&tha
.src
, src
, sizeof ip
->ip_src
);
331 tha
.port
= sport
<< 16 | dport
;
334 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
335 th
->nxt
; th
= th
->nxt
)
336 if (memcmp((char *)&tha
, (char *)&th
->addr
,
337 sizeof(th
->addr
)) == 0)
340 if (!th
->nxt
|| (flags
& TH_SYN
)) {
341 /* didn't find it or new conversation */
342 if (th
->nxt
== NULL
) {
343 th
->nxt
= (struct tcp_seq_hash
*)
344 calloc(1, sizeof(*th
));
346 error("tcp_print: calloc");
350 th
->ack
= seq
, th
->seq
= ack
- 1;
352 th
->seq
= seq
, th
->ack
= ack
- 1;
355 seq
-= th
->ack
, ack
-= th
->seq
;
357 seq
-= th
->seq
, ack
-= th
->ack
;
365 thseq
= thack
= rev
= 0;
368 ND_PRINT((ndo
, " [bad hdr length %u - too long, > %u]",
373 if (ndo
->ndo_vflag
&& !ndo
->ndo_Kflag
&& !fragmented
) {
374 /* Check the checksum, if possible. */
375 uint16_t sum
, tcp_sum
;
378 if (ND_TTEST2(tp
->th_sport
, length
)) {
379 sum
= tcp_cksum(ndo
, ip
, tp
, length
);
380 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
382 ND_PRINT((ndo
, ", cksum 0x%04x", tcp_sum
));
384 ND_PRINT((ndo
, " (incorrect -> 0x%04x)",
385 in_cksum_shouldbe(tcp_sum
, sum
)));
387 ND_PRINT((ndo
, " (correct)"));
391 else if (IP_V(ip
) == 6 && ip6
->ip6_plen
) {
392 if (ND_TTEST2(tp
->th_sport
, length
)) {
393 sum
= nextproto6_cksum(ip6
, (const uint8_t *)tp
,
394 length
, length
, IPPROTO_TCP
);
395 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
397 ND_PRINT((ndo
, ", cksum 0x%04x", tcp_sum
));
399 ND_PRINT((ndo
, " (incorrect -> 0x%04x)",
400 in_cksum_shouldbe(tcp_sum
, sum
)));
402 ND_PRINT((ndo
, " (correct)"));
410 if (ndo
->ndo_vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
411 ND_PRINT((ndo
, ", seq %u", seq
));
414 ND_PRINT((ndo
, ":%u", seq
+ length
));
418 if (flags
& TH_ACK
) {
419 ND_PRINT((ndo
, ", ack %u", ack
));
422 ND_PRINT((ndo
, ", win %d", win
));
425 ND_PRINT((ndo
, ", urg %d", urp
));
427 * Handle any options.
429 if (hlen
> sizeof(*tp
)) {
430 register const u_char
*cp
;
431 register u_int i
, opt
, datalen
;
435 cp
= (const u_char
*)tp
+ sizeof(*tp
);
436 ND_PRINT((ndo
, ", options ["));
439 ND_PRINT((ndo
, "%c", ch
));
446 len
= *cp
++; /* total including type, len */
447 if (len
< 2 || len
> hlen
)
449 --hlen
; /* account for length byte */
451 --hlen
; /* account for type byte */
454 /* Bail if "l" bytes of data are not left or were not captured */
455 #define LENCHECK(l) { if ((l) > hlen) goto bad; ND_TCHECK2(*cp, l); }
458 ND_PRINT((ndo
, "%s", tok2str(tcp_option_values
, "unknown-%u", opt
)));
465 ND_PRINT((ndo
, " %u", EXTRACT_16BITS(cp
)));
471 ND_PRINT((ndo
, " %u", *cp
));
476 if (datalen
% 8 != 0) {
477 ND_PRINT((ndo
, "malformed sack"));
481 ND_PRINT((ndo
, " %d ", datalen
/ 8));
482 for (i
= 0; i
< datalen
; i
+= 8) {
484 s
= EXTRACT_32BITS(cp
+ i
);
486 e
= EXTRACT_32BITS(cp
+ i
+ 4);
494 ND_PRINT((ndo
, "{%u:%u}", s
, e
));
503 case TCPOPT_ECHOREPLY
:
506 * those options share their semantics.
511 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
514 case TCPOPT_TIMESTAMP
:
517 ND_PRINT((ndo
, " val %u ecr %u",
519 EXTRACT_32BITS(cp
+ 4)));
522 case TCPOPT_SIGNATURE
:
523 datalen
= TCP_SIGLEN
;
525 #ifdef HAVE_LIBCRYPTO
526 switch (tcp_verify_signature(ndo
, ip
, tp
,
527 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
529 case SIGNATURE_VALID
:
530 ND_PRINT((ndo
, "valid"));
533 case SIGNATURE_INVALID
:
534 ND_PRINT((ndo
, "invalid"));
537 case CANT_CHECK_SIGNATURE
:
538 ND_PRINT((ndo
, "can't check - "));
539 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
540 ND_PRINT((ndo
, "%02x", cp
[i
]));
544 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
545 ND_PRINT((ndo
, "%02x", cp
[i
]));
550 ND_PRINT((ndo
, "keyid %d", *cp
++));
552 for (i
= 0; i
< datalen
; ++i
) {
554 ND_PRINT((ndo
, "%02x", cp
[i
]));
563 * Nothing interesting.
571 utoval
= EXTRACT_16BITS(cp
);
572 ND_PRINT((ndo
, "0x%x", utoval
));
574 utoval
= (utoval
>> 1) * 60;
577 ND_PRINT((ndo
, " %u", utoval
));
583 if (!mptcp_print(ndo
, cp
-2, len
, flags
))
587 case TCPOPT_FASTOPEN
:
590 print_tcp_fastopen_option(ndo
, cp
, datalen
, FALSE
);
593 case TCPOPT_EXPERIMENT2
:
599 magic
= EXTRACT_16BITS(cp
);
600 ND_PRINT((ndo
, "-"));
604 case 0xf989: /* TCP Fast Open RFC 7413 */
605 print_tcp_fastopen_option(ndo
, cp
+ 2, datalen
- 2, TRUE
);
609 /* Unknown magic number */
610 ND_PRINT((ndo
, "%04x", magic
));
618 ND_PRINT((ndo
, " 0x"));
619 for (i
= 0; i
< datalen
; ++i
) {
621 ND_PRINT((ndo
, "%02x", cp
[i
]));
626 /* Account for data printed */
630 /* Check specification against observed length */
631 ++datalen
; /* option octet */
632 if (!ZEROLENOPT(opt
))
633 ++datalen
; /* size octet */
635 ND_PRINT((ndo
, "[len %d]", len
));
637 if (opt
== TCPOPT_EOL
)
640 ND_PRINT((ndo
, "]"));
644 * Print length field before crawling down the stack.
646 ND_PRINT((ndo
, ", length %u", length
));
652 * Decode payload if necessary.
654 bp
+= TH_OFF(tp
) * 4;
655 if ((flags
& TH_RST
) && ndo
->ndo_vflag
) {
656 print_tcp_rst_data(ndo
, bp
, length
);
660 if (ndo
->ndo_packettype
) {
661 switch (ndo
->ndo_packettype
) {
663 zmtp1_print(ndo
, bp
, length
);
669 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
670 telnet_print(ndo
, bp
, length
);
671 } else if (sport
== SMTP_PORT
|| dport
== SMTP_PORT
) {
672 ND_PRINT((ndo
, ": "));
673 smtp_print(ndo
, bp
, length
);
674 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
675 bgp_print(ndo
, bp
, length
);
676 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
678 #ifdef TCPDUMP_DO_SMB
679 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
680 nbt_tcp_print(ndo
, bp
, length
);
681 else if (sport
== SMB_PORT
|| dport
== SMB_PORT
)
682 smb_tcp_print(ndo
, bp
, length
);
684 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
685 beep_print(ndo
, bp
, length
);
686 else if (sport
== OPENFLOW_PORT_OLD
|| dport
== OPENFLOW_PORT_OLD
||
687 sport
== OPENFLOW_PORT_IANA
|| dport
== OPENFLOW_PORT_IANA
)
688 openflow_print(ndo
, bp
, length
);
689 else if (sport
== FTP_PORT
|| dport
== FTP_PORT
) {
690 ND_PRINT((ndo
, ": "));
691 ftp_print(ndo
, bp
, length
);
692 } else if (sport
== HTTP_PORT
|| dport
== HTTP_PORT
||
693 sport
== HTTP_PORT_ALT
|| dport
== HTTP_PORT_ALT
) {
694 ND_PRINT((ndo
, ": "));
695 http_print(ndo
, bp
, length
);
696 } else if (sport
== RTSP_PORT
|| dport
== RTSP_PORT
||
697 sport
== RTSP_PORT_ALT
|| dport
== RTSP_PORT_ALT
) {
698 ND_PRINT((ndo
, ": "));
699 rtsp_print(ndo
, bp
, length
);
700 } else if (length
> 2 &&
701 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
)) {
703 * TCP DNS query has 2byte length at the head.
704 * XXX packet could be unaligned, it can go strange
706 ns_print(ndo
, bp
+ 2, length
- 2, 0);
707 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
708 msdp_print(ndo
, bp
, length
);
709 } else if (sport
== RPKI_RTR_PORT
|| dport
== RPKI_RTR_PORT
) {
710 rpki_rtr_print(ndo
, bp
, length
);
712 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
713 ldp_print(ndo
, bp
, length
);
715 else if ((sport
== NFS_PORT
|| dport
== NFS_PORT
) &&
716 length
>= 4 && ND_TTEST2(*bp
, 4)) {
718 * If data present, header length valid, and NFS port used,
720 * Pass offset of data plus 4 bytes for RPC TCP msg length
721 * to NFS print routines.
724 register const struct sunrpc_msg
*rp
;
725 enum sunrpc_msg_type direction
;
727 fraglen
= EXTRACT_32BITS(bp
) & 0x7FFFFFFF;
728 if (fraglen
> (length
) - 4)
729 fraglen
= (length
) - 4;
730 rp
= (const struct sunrpc_msg
*)(bp
+ 4);
731 if (ND_TTEST(rp
->rm_direction
)) {
732 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
733 if (dport
== NFS_PORT
&& direction
== SUNRPC_CALL
) {
734 ND_PRINT((ndo
, ": NFS request xid %u ", EXTRACT_32BITS(&rp
->rm_xid
)));
735 nfsreq_print_noaddr(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
738 if (sport
== NFS_PORT
&& direction
== SUNRPC_REPLY
) {
739 ND_PRINT((ndo
, ": NFS reply xid %u ", EXTRACT_32BITS(&rp
->rm_xid
)));
740 nfsreply_print_noaddr(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
748 ND_PRINT((ndo
, "[bad opt]"));
750 ND_PRINT((ndo
, ">"));
753 ND_PRINT((ndo
, "[|tcp]"));
755 ND_PRINT((ndo
, ">"));
759 * RFC1122 says the following on data in RST segments:
761 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
763 * A TCP SHOULD allow a received RST segment to include data.
766 * It has been suggested that a RST segment could contain
767 * ASCII text that encoded and explained the cause of the
768 * RST. No standard has yet been established for such
774 print_tcp_rst_data(netdissect_options
*ndo
,
775 register const u_char
*sp
, u_int length
)
779 ND_PRINT((ndo
, ND_TTEST2(*sp
, length
) ? " [RST" : " [!RST"));
780 if (length
> MAX_RST_DATA_LEN
) {
781 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
782 ND_PRINT((ndo
, "+")); /* indicate we truncate */
784 ND_PRINT((ndo
, " "));
785 while (length
-- && sp
<= ndo
->ndo_snapend
) {
789 ND_PRINT((ndo
, "]"));
793 print_tcp_fastopen_option(netdissect_options
*ndo
, register const u_char
*cp
,
794 u_int datalen
, int exp
)
799 ND_PRINT((ndo
, "tfo"));
802 /* Fast Open Cookie Request */
803 ND_PRINT((ndo
, " cookiereq"));
805 /* Fast Open Cookie */
806 if (datalen
% 2 != 0 || datalen
< 4 || datalen
> 16) {
807 ND_PRINT((ndo
, " malformed"));
809 ND_PRINT((ndo
, " cookie "));
810 for (i
= 0; i
< datalen
; ++i
)
811 ND_PRINT((ndo
, "%02x", cp
[i
]));
816 #ifdef HAVE_LIBCRYPTO
817 USES_APPLE_DEPRECATED_API
819 tcp_verify_signature(netdissect_options
*ndo
,
820 const struct ip
*ip
, const struct tcphdr
*tp
,
821 const u_char
*data
, int length
, const u_char
*rcvsig
)
824 u_char sig
[TCP_SIGLEN
];
827 uint16_t savecsum
, tlen
;
829 const struct ip6_hdr
*ip6
;
834 if (data
+ length
> ndo
->ndo_snapend
) {
835 ND_PRINT((ndo
, "snaplen too short, "));
836 return (CANT_CHECK_SIGNATURE
);
841 if (ndo
->ndo_sigsecret
== NULL
) {
842 ND_PRINT((ndo
, "shared secret not supplied with -M, "));
843 return (CANT_CHECK_SIGNATURE
);
848 * Step 1: Update MD5 hash with IP pseudo-header.
851 MD5_Update(&ctx
, (const char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
852 MD5_Update(&ctx
, (const char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
853 MD5_Update(&ctx
, (const char *)&zero_proto
, sizeof(zero_proto
));
854 MD5_Update(&ctx
, (const char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
855 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
857 MD5_Update(&ctx
, (const char *)&tlen
, sizeof(tlen
));
859 } else if (IP_V(ip
) == 6) {
860 ip6
= (const struct ip6_hdr
*)ip
;
861 MD5_Update(&ctx
, (const char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
862 MD5_Update(&ctx
, (const char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
863 len32
= htonl(EXTRACT_16BITS(&ip6
->ip6_plen
));
864 MD5_Update(&ctx
, (const char *)&len32
, sizeof(len32
));
866 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
867 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
868 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
870 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
874 ND_PRINT((ndo
, "IP version not 4 or 6, "));
876 ND_PRINT((ndo
, "IP version not 4, "));
878 return (CANT_CHECK_SIGNATURE
);
882 * Step 2: Update MD5 hash with TCP header, excluding options.
883 * The TCP checksum must be set to zero.
885 savecsum
= tp1
.th_sum
;
887 MD5_Update(&ctx
, (const char *)&tp1
, sizeof(struct tcphdr
));
888 tp1
.th_sum
= savecsum
;
890 * Step 3: Update MD5 hash with TCP segment data, if present.
893 MD5_Update(&ctx
, data
, length
);
895 * Step 4: Update MD5 hash with shared secret.
897 MD5_Update(&ctx
, ndo
->ndo_sigsecret
, strlen(ndo
->ndo_sigsecret
));
898 MD5_Final(sig
, &ctx
);
900 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
901 return (SIGNATURE_VALID
);
903 return (SIGNATURE_INVALID
);
906 #endif /* HAVE_LIBCRYPTO */
910 * c-style: whitesmith