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 <netdissect-stdinc.h>
40 #include "netdissect.h"
41 #include "addrtoname.h"
53 #include <openssl/md5.h>
54 #include "signature.h"
56 static int tcp_verify_signature(netdissect_options
*ndo
,
57 const struct ip
*ip
, const struct tcphdr
*tp
,
58 const u_char
*data
, int length
, const u_char
*rcvsig
);
61 static void print_tcp_rst_data(netdissect_options
*, register const u_char
*sp
, u_int length
);
62 static void print_tcp_fastopen_option(netdissect_options
*ndo
, register const u_char
*cp
,
63 u_int datalen
, int exp
);
65 #define MAX_RST_DATA_LEN 30
75 struct tcp_seq_hash
*nxt
;
87 struct tcp_seq_hash6
{
88 struct tcp_seq_hash6
*nxt
;
94 #define TSEQ_HASHSIZE 919
96 /* These tcp optinos do not have the size octet */
97 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
99 static struct tcp_seq_hash tcp_seq_hash4
[TSEQ_HASHSIZE
];
100 static struct tcp_seq_hash6 tcp_seq_hash6
[TSEQ_HASHSIZE
];
102 static const struct tok tcp_flag_values
[] = {
114 static const struct tok tcp_option_values
[] = {
115 { TCPOPT_EOL
, "eol" },
116 { TCPOPT_NOP
, "nop" },
117 { TCPOPT_MAXSEG
, "mss" },
118 { TCPOPT_WSCALE
, "wscale" },
119 { TCPOPT_SACKOK
, "sackOK" },
120 { TCPOPT_SACK
, "sack" },
121 { TCPOPT_ECHO
, "echo" },
122 { TCPOPT_ECHOREPLY
, "echoreply" },
123 { TCPOPT_TIMESTAMP
, "TS" },
125 { TCPOPT_CCNEW
, "ccnew" },
126 { TCPOPT_CCECHO
, "" },
127 { TCPOPT_SIGNATURE
, "md5" },
128 { TCPOPT_AUTH
, "enhanced auth" },
129 { TCPOPT_UTO
, "uto" },
130 { TCPOPT_MPTCP
, "mptcp" },
131 { TCPOPT_FASTOPEN
, "tfo" },
132 { TCPOPT_EXPERIMENT2
, "exp" },
137 tcp_cksum(netdissect_options
*ndo
,
138 register const struct ip
*ip
,
139 register const struct tcphdr
*tp
,
142 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)tp
, len
, len
,
147 tcp_print(netdissect_options
*ndo
,
148 register const u_char
*bp
, register u_int length
,
149 register const u_char
*bp2
, int fragmented
)
151 register const struct tcphdr
*tp
;
152 register const struct ip
*ip
;
153 register u_char flags
;
156 uint16_t sport
, dport
, win
, urp
;
157 uint32_t seq
, ack
, thseq
, thack
;
161 register const struct ip6_hdr
*ip6
;
163 tp
= (const struct tcphdr
*)bp
;
164 ip
= (const struct ip
*)bp2
;
166 ip6
= (const struct ip6_hdr
*)bp2
;
170 if (!ND_TTEST(tp
->th_dport
)) {
171 ND_PRINT((ndo
, "%s > %s: [|tcp]",
172 ipaddr_string(ndo
, &ip
->ip_src
),
173 ipaddr_string(ndo
, &ip
->ip_dst
)));
177 sport
= EXTRACT_16BITS(&tp
->th_sport
);
178 dport
= EXTRACT_16BITS(&tp
->th_dport
);
180 hlen
= TH_OFF(tp
) * 4;
183 if (ip6
->ip6_nxt
== IPPROTO_TCP
) {
184 ND_PRINT((ndo
, "%s.%s > %s.%s: ",
185 ip6addr_string(ndo
, &ip6
->ip6_src
),
186 tcpport_string(ndo
, sport
),
187 ip6addr_string(ndo
, &ip6
->ip6_dst
),
188 tcpport_string(ndo
, dport
)));
190 ND_PRINT((ndo
, "%s > %s: ",
191 tcpport_string(ndo
, sport
), tcpport_string(ndo
, dport
)));
194 if (ip
->ip_p
== IPPROTO_TCP
) {
195 ND_PRINT((ndo
, "%s.%s > %s.%s: ",
196 ipaddr_string(ndo
, &ip
->ip_src
),
197 tcpport_string(ndo
, sport
),
198 ipaddr_string(ndo
, &ip
->ip_dst
),
199 tcpport_string(ndo
, dport
)));
201 ND_PRINT((ndo
, "%s > %s: ",
202 tcpport_string(ndo
, sport
), tcpport_string(ndo
, dport
)));
206 if (hlen
< sizeof(*tp
)) {
207 ND_PRINT((ndo
, " tcp %d [bad hdr length %u - too short, < %lu]",
208 length
- hlen
, hlen
, (unsigned long)sizeof(*tp
)));
214 seq
= EXTRACT_32BITS(&tp
->th_seq
);
215 ack
= EXTRACT_32BITS(&tp
->th_ack
);
216 win
= EXTRACT_16BITS(&tp
->th_win
);
217 urp
= EXTRACT_16BITS(&tp
->th_urp
);
219 if (ndo
->ndo_qflag
) {
220 ND_PRINT((ndo
, "tcp %d", length
- hlen
));
222 ND_PRINT((ndo
, " [bad hdr length %u - too long, > %u]",
228 flags
= tp
->th_flags
;
229 ND_PRINT((ndo
, "Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
)));
231 if (!ndo
->ndo_Sflag
&& (flags
& TH_ACK
)) {
233 * Find (or record) the initial sequence numbers for
234 * this conversation. (we pick an arbitrary
235 * collating order so there's only one entry for
240 register struct tcp_seq_hash6
*th
;
241 struct tcp_seq_hash6
*tcp_seq_hash
;
242 const struct in6_addr
*src
, *dst
;
245 tcp_seq_hash
= tcp_seq_hash6
;
250 else if (sport
== dport
) {
251 if (UNALIGNED_MEMCMP(src
, dst
, sizeof ip6
->ip6_dst
) > 0)
255 UNALIGNED_MEMCPY(&tha
.src
, dst
, sizeof ip6
->ip6_dst
);
256 UNALIGNED_MEMCPY(&tha
.dst
, src
, sizeof ip6
->ip6_src
);
257 tha
.port
= dport
<< 16 | sport
;
259 UNALIGNED_MEMCPY(&tha
.dst
, dst
, sizeof ip6
->ip6_dst
);
260 UNALIGNED_MEMCPY(&tha
.src
, src
, sizeof ip6
->ip6_src
);
261 tha
.port
= sport
<< 16 | dport
;
264 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
265 th
->nxt
; th
= th
->nxt
)
266 if (memcmp((char *)&tha
, (char *)&th
->addr
,
267 sizeof(th
->addr
)) == 0)
270 if (!th
->nxt
|| (flags
& TH_SYN
)) {
271 /* didn't find it or new conversation */
272 if (th
->nxt
== NULL
) {
273 th
->nxt
= (struct tcp_seq_hash6
*)
274 calloc(1, sizeof(*th
));
276 (*ndo
->ndo_error
)(ndo
,
277 "tcp_print: calloc");
281 th
->ack
= seq
, th
->seq
= ack
- 1;
283 th
->seq
= seq
, th
->ack
= ack
- 1;
286 seq
-= th
->ack
, ack
-= th
->seq
;
288 seq
-= th
->seq
, ack
-= th
->ack
;
294 register struct tcp_seq_hash
*th
;
295 struct tcp_seq_hash
*tcp_seq_hash
;
296 const struct in_addr
*src
, *dst
;
299 tcp_seq_hash
= tcp_seq_hash4
;
304 else if (sport
== dport
) {
305 if (UNALIGNED_MEMCMP(src
, dst
, sizeof ip
->ip_dst
) > 0)
309 UNALIGNED_MEMCPY(&tha
.src
, dst
, sizeof ip
->ip_dst
);
310 UNALIGNED_MEMCPY(&tha
.dst
, src
, sizeof ip
->ip_src
);
311 tha
.port
= dport
<< 16 | sport
;
313 UNALIGNED_MEMCPY(&tha
.dst
, dst
, sizeof ip
->ip_dst
);
314 UNALIGNED_MEMCPY(&tha
.src
, src
, sizeof ip
->ip_src
);
315 tha
.port
= sport
<< 16 | dport
;
318 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
319 th
->nxt
; th
= th
->nxt
)
320 if (memcmp((char *)&tha
, (char *)&th
->addr
,
321 sizeof(th
->addr
)) == 0)
324 if (!th
->nxt
|| (flags
& TH_SYN
)) {
325 /* didn't find it or new conversation */
326 if (th
->nxt
== NULL
) {
327 th
->nxt
= (struct tcp_seq_hash
*)
328 calloc(1, sizeof(*th
));
330 (*ndo
->ndo_error
)(ndo
,
331 "tcp_print: calloc");
335 th
->ack
= seq
, th
->seq
= ack
- 1;
337 th
->seq
= seq
, th
->ack
= ack
- 1;
340 seq
-= th
->ack
, ack
-= th
->seq
;
342 seq
-= th
->seq
, ack
-= th
->ack
;
350 thseq
= thack
= rev
= 0;
353 ND_PRINT((ndo
, " [bad hdr length %u - too long, > %u]",
358 if (ndo
->ndo_vflag
&& !ndo
->ndo_Kflag
&& !fragmented
) {
359 /* Check the checksum, if possible. */
360 uint16_t sum
, tcp_sum
;
363 if (ND_TTEST2(tp
->th_sport
, length
)) {
364 sum
= tcp_cksum(ndo
, ip
, tp
, length
);
365 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
367 ND_PRINT((ndo
, ", cksum 0x%04x", tcp_sum
));
369 ND_PRINT((ndo
, " (incorrect -> 0x%04x)",
370 in_cksum_shouldbe(tcp_sum
, sum
)));
372 ND_PRINT((ndo
, " (correct)"));
374 } else if (IP_V(ip
) == 6 && ip6
->ip6_plen
) {
375 if (ND_TTEST2(tp
->th_sport
, length
)) {
376 sum
= nextproto6_cksum(ip6
, (const uint8_t *)tp
,
377 length
, length
, IPPROTO_TCP
);
378 tcp_sum
= EXTRACT_16BITS(&tp
->th_sum
);
380 ND_PRINT((ndo
, ", cksum 0x%04x", tcp_sum
));
382 ND_PRINT((ndo
, " (incorrect -> 0x%04x)",
383 in_cksum_shouldbe(tcp_sum
, sum
)));
385 ND_PRINT((ndo
, " (correct)"));
392 if (ndo
->ndo_vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
393 ND_PRINT((ndo
, ", seq %u", seq
));
396 ND_PRINT((ndo
, ":%u", seq
+ length
));
400 if (flags
& TH_ACK
) {
401 ND_PRINT((ndo
, ", ack %u", ack
));
404 ND_PRINT((ndo
, ", win %d", win
));
407 ND_PRINT((ndo
, ", urg %d", urp
));
409 * Handle any options.
411 if (hlen
> sizeof(*tp
)) {
412 register const u_char
*cp
;
413 register u_int i
, opt
, datalen
;
417 cp
= (const u_char
*)tp
+ sizeof(*tp
);
418 ND_PRINT((ndo
, ", options ["));
421 ND_PRINT((ndo
, "%c", ch
));
428 len
= *cp
++; /* total including type, len */
429 if (len
< 2 || len
> hlen
)
431 --hlen
; /* account for length byte */
433 --hlen
; /* account for type byte */
436 /* Bail if "l" bytes of data are not left or were not captured */
437 #define LENCHECK(l) { if ((l) > hlen) goto bad; ND_TCHECK2(*cp, l); }
440 ND_PRINT((ndo
, "%s", tok2str(tcp_option_values
, "unknown-%u", opt
)));
447 ND_PRINT((ndo
, " %u", EXTRACT_16BITS(cp
)));
453 ND_PRINT((ndo
, " %u", *cp
));
458 if (datalen
% 8 != 0) {
459 ND_PRINT((ndo
, "invalid sack"));
463 ND_PRINT((ndo
, " %d ", datalen
/ 8));
464 for (i
= 0; i
< datalen
; i
+= 8) {
466 s
= EXTRACT_32BITS(cp
+ i
);
468 e
= EXTRACT_32BITS(cp
+ i
+ 4);
476 ND_PRINT((ndo
, "{%u:%u}", s
, e
));
485 case TCPOPT_ECHOREPLY
:
488 * those options share their semantics.
493 ND_PRINT((ndo
, " %u", EXTRACT_32BITS(cp
)));
496 case TCPOPT_TIMESTAMP
:
499 ND_PRINT((ndo
, " val %u ecr %u",
501 EXTRACT_32BITS(cp
+ 4)));
504 case TCPOPT_SIGNATURE
:
505 datalen
= TCP_SIGLEN
;
507 #ifdef HAVE_LIBCRYPTO
508 switch (tcp_verify_signature(ndo
, ip
, tp
,
509 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
511 case SIGNATURE_VALID
:
512 ND_PRINT((ndo
, "valid"));
515 case SIGNATURE_INVALID
:
516 ND_PRINT((ndo
, "invalid"));
519 case CANT_CHECK_SIGNATURE
:
520 ND_PRINT((ndo
, "can't check - "));
521 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
522 ND_PRINT((ndo
, "%02x", cp
[i
]));
526 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
527 ND_PRINT((ndo
, "%02x", cp
[i
]));
532 ND_PRINT((ndo
, "keyid %d", *cp
++));
534 for (i
= 0; i
< datalen
; ++i
) {
536 ND_PRINT((ndo
, "%02x", cp
[i
]));
545 * Nothing interesting.
553 utoval
= EXTRACT_16BITS(cp
);
554 ND_PRINT((ndo
, "0x%x", utoval
));
556 utoval
= (utoval
>> 1) * 60;
559 ND_PRINT((ndo
, " %u", utoval
));
565 if (!mptcp_print(ndo
, cp
-2, len
, flags
))
569 case TCPOPT_FASTOPEN
:
572 print_tcp_fastopen_option(ndo
, cp
, datalen
, FALSE
);
575 case TCPOPT_EXPERIMENT2
:
581 magic
= EXTRACT_16BITS(cp
);
582 ND_PRINT((ndo
, "-"));
586 case 0xf989: /* TCP Fast Open RFC 7413 */
587 print_tcp_fastopen_option(ndo
, cp
+ 2, datalen
- 2, TRUE
);
591 /* Unknown magic number */
592 ND_PRINT((ndo
, "%04x", magic
));
600 ND_PRINT((ndo
, " 0x"));
601 for (i
= 0; i
< datalen
; ++i
) {
603 ND_PRINT((ndo
, "%02x", cp
[i
]));
608 /* Account for data printed */
612 /* Check specification against observed length */
613 ++datalen
; /* option octet */
614 if (!ZEROLENOPT(opt
))
615 ++datalen
; /* size octet */
617 ND_PRINT((ndo
, "[len %d]", len
));
619 if (opt
== TCPOPT_EOL
)
622 ND_PRINT((ndo
, "]"));
626 * Print length field before crawling down the stack.
628 ND_PRINT((ndo
, ", length %u", length
));
634 * Decode payload if necessary.
636 bp
+= TH_OFF(tp
) * 4;
637 if ((flags
& TH_RST
) && ndo
->ndo_vflag
) {
638 print_tcp_rst_data(ndo
, bp
, length
);
642 if (ndo
->ndo_packettype
) {
643 switch (ndo
->ndo_packettype
) {
645 zmtp1_print(ndo
, bp
, length
);
651 if (sport
== TELNET_PORT
|| dport
== TELNET_PORT
) {
652 telnet_print(ndo
, bp
, length
);
653 } else if (sport
== SMTP_PORT
|| dport
== SMTP_PORT
) {
654 ND_PRINT((ndo
, ": "));
655 smtp_print(ndo
, bp
, length
);
656 } else if (sport
== BGP_PORT
|| dport
== BGP_PORT
)
657 bgp_print(ndo
, bp
, length
);
658 else if (sport
== PPTP_PORT
|| dport
== PPTP_PORT
)
660 #ifdef TCPDUMP_DO_SMB
661 else if (sport
== NETBIOS_SSN_PORT
|| dport
== NETBIOS_SSN_PORT
)
662 nbt_tcp_print(ndo
, bp
, length
);
663 else if (sport
== SMB_PORT
|| dport
== SMB_PORT
)
664 smb_tcp_print(ndo
, bp
, length
);
666 else if (sport
== BEEP_PORT
|| dport
== BEEP_PORT
)
667 beep_print(ndo
, bp
, length
);
668 else if (sport
== OPENFLOW_PORT_OLD
|| dport
== OPENFLOW_PORT_OLD
||
669 sport
== OPENFLOW_PORT_IANA
|| dport
== OPENFLOW_PORT_IANA
)
670 openflow_print(ndo
, bp
, length
);
671 else if (sport
== FTP_PORT
|| dport
== FTP_PORT
) {
672 ND_PRINT((ndo
, ": "));
673 ftp_print(ndo
, bp
, length
);
674 } else if (sport
== HTTP_PORT
|| dport
== HTTP_PORT
||
675 sport
== HTTP_PORT_ALT
|| dport
== HTTP_PORT_ALT
) {
676 ND_PRINT((ndo
, ": "));
677 http_print(ndo
, bp
, length
);
678 } else if (sport
== RTSP_PORT
|| dport
== RTSP_PORT
||
679 sport
== RTSP_PORT_ALT
|| dport
== RTSP_PORT_ALT
) {
680 ND_PRINT((ndo
, ": "));
681 rtsp_print(ndo
, bp
, length
);
682 } else if (length
> 2 &&
683 (sport
== NAMESERVER_PORT
|| dport
== NAMESERVER_PORT
)) {
685 * TCP DNS query has 2byte length at the head.
686 * XXX packet could be unaligned, it can go strange
688 ns_print(ndo
, bp
+ 2, length
- 2, 0);
689 } else if (sport
== MSDP_PORT
|| dport
== MSDP_PORT
) {
690 msdp_print(ndo
, bp
, length
);
691 } else if (sport
== RPKI_RTR_PORT
|| dport
== RPKI_RTR_PORT
) {
692 rpki_rtr_print(ndo
, bp
, length
);
694 else if (length
> 0 && (sport
== LDP_PORT
|| dport
== LDP_PORT
)) {
695 ldp_print(ndo
, bp
, length
);
697 else if ((sport
== NFS_PORT
|| dport
== NFS_PORT
) &&
698 length
>= 4 && ND_TTEST2(*bp
, 4)) {
700 * If data present, header length valid, and NFS port used,
702 * Pass offset of data plus 4 bytes for RPC TCP msg length
703 * to NFS print routines.
706 register const struct sunrpc_msg
*rp
;
707 enum sunrpc_msg_type direction
;
709 fraglen
= EXTRACT_32BITS(bp
) & 0x7FFFFFFF;
710 if (fraglen
> (length
) - 4)
711 fraglen
= (length
) - 4;
712 rp
= (const struct sunrpc_msg
*)(bp
+ 4);
713 if (ND_TTEST(rp
->rm_direction
)) {
714 direction
= (enum sunrpc_msg_type
)EXTRACT_32BITS(&rp
->rm_direction
);
715 if (dport
== NFS_PORT
&& direction
== SUNRPC_CALL
) {
716 ND_PRINT((ndo
, ": NFS request xid %u ", EXTRACT_32BITS(&rp
->rm_xid
)));
717 nfsreq_print_noaddr(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
720 if (sport
== NFS_PORT
&& direction
== SUNRPC_REPLY
) {
721 ND_PRINT((ndo
, ": NFS reply xid %u ", EXTRACT_32BITS(&rp
->rm_xid
)));
722 nfsreply_print_noaddr(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
730 ND_PRINT((ndo
, "[bad opt]"));
732 ND_PRINT((ndo
, ">"));
735 ND_PRINT((ndo
, "[|tcp]"));
737 ND_PRINT((ndo
, ">"));
741 * RFC1122 says the following on data in RST segments:
743 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
745 * A TCP SHOULD allow a received RST segment to include data.
748 * It has been suggested that a RST segment could contain
749 * ASCII text that encoded and explained the cause of the
750 * RST. No standard has yet been established for such
756 print_tcp_rst_data(netdissect_options
*ndo
,
757 register const u_char
*sp
, u_int length
)
761 ND_PRINT((ndo
, ND_TTEST2(*sp
, length
) ? " [RST" : " [!RST"));
762 if (length
> MAX_RST_DATA_LEN
) {
763 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
764 ND_PRINT((ndo
, "+")); /* indicate we truncate */
766 ND_PRINT((ndo
, " "));
767 while (length
-- && sp
<= ndo
->ndo_snapend
) {
771 ND_PRINT((ndo
, "]"));
775 print_tcp_fastopen_option(netdissect_options
*ndo
, register const u_char
*cp
,
776 u_int datalen
, int exp
)
781 ND_PRINT((ndo
, "tfo"));
784 /* Fast Open Cookie Request */
785 ND_PRINT((ndo
, " cookiereq"));
787 /* Fast Open Cookie */
788 if (datalen
% 2 != 0 || datalen
< 4 || datalen
> 16) {
789 ND_PRINT((ndo
, " invalid"));
791 ND_PRINT((ndo
, " cookie "));
792 for (i
= 0; i
< datalen
; ++i
)
793 ND_PRINT((ndo
, "%02x", cp
[i
]));
798 #ifdef HAVE_LIBCRYPTO
799 USES_APPLE_DEPRECATED_API
801 tcp_verify_signature(netdissect_options
*ndo
,
802 const struct ip
*ip
, const struct tcphdr
*tp
,
803 const u_char
*data
, int length
, const u_char
*rcvsig
)
806 u_char sig
[TCP_SIGLEN
];
809 uint16_t savecsum
, tlen
;
810 const struct ip6_hdr
*ip6
;
814 if (data
+ length
> ndo
->ndo_snapend
) {
815 ND_PRINT((ndo
, "snaplen too short, "));
816 return (CANT_CHECK_SIGNATURE
);
821 if (ndo
->ndo_sigsecret
== NULL
) {
822 ND_PRINT((ndo
, "shared secret not supplied with -M, "));
823 return (CANT_CHECK_SIGNATURE
);
828 * Step 1: Update MD5 hash with IP pseudo-header.
831 MD5_Update(&ctx
, (const char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
832 MD5_Update(&ctx
, (const char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
833 MD5_Update(&ctx
, (const char *)&zero_proto
, sizeof(zero_proto
));
834 MD5_Update(&ctx
, (const char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
835 tlen
= EXTRACT_16BITS(&ip
->ip_len
) - IP_HL(ip
) * 4;
837 MD5_Update(&ctx
, (const char *)&tlen
, sizeof(tlen
));
838 } else if (IP_V(ip
) == 6) {
839 ip6
= (const struct ip6_hdr
*)ip
;
840 MD5_Update(&ctx
, (const char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
841 MD5_Update(&ctx
, (const char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
842 len32
= htonl(EXTRACT_16BITS(&ip6
->ip6_plen
));
843 MD5_Update(&ctx
, (const char *)&len32
, sizeof(len32
));
845 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
846 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
847 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
849 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
851 ND_PRINT((ndo
, "IP version not 4 or 6, "));
852 return (CANT_CHECK_SIGNATURE
);
856 * Step 2: Update MD5 hash with TCP header, excluding options.
857 * The TCP checksum must be set to zero.
859 savecsum
= tp1
.th_sum
;
861 MD5_Update(&ctx
, (const char *)&tp1
, sizeof(struct tcphdr
));
862 tp1
.th_sum
= savecsum
;
864 * Step 3: Update MD5 hash with TCP segment data, if present.
867 MD5_Update(&ctx
, data
, length
);
869 * Step 4: Update MD5 hash with shared secret.
871 MD5_Update(&ctx
, ndo
->ndo_sigsecret
, strlen(ndo
->ndo_sigsecret
));
872 MD5_Final(sig
, &ctx
);
874 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
875 return (SIGNATURE_VALID
);
877 return (SIGNATURE_INVALID
);
880 #endif /* HAVE_LIBCRYPTO */
884 * c-style: whitesmith