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.
26 /* \summary: TCP printer */
30 __RCSID("$NetBSD: print-tcp.c,v 1.8 2007/07/24 11:53:48 drochner Exp $");
35 #include "netdissect-stdinc.h"
40 #define ND_LONGJMP_FROM_TCHECK
41 #include "netdissect.h"
42 #include "addrtoname.h"
45 #include "diag-control.h"
56 #include <openssl/md5.h>
57 #include "signature.h"
59 static int tcp_verify_signature(netdissect_options
*ndo
,
60 const struct ip
*ip
, const struct tcphdr
*tp
,
61 const u_char
*data
, u_int length
, const u_char
*rcvsig
);
64 static void print_tcp_rst_data(netdissect_options
*, const u_char
*sp
, u_int length
);
65 static void print_tcp_fastopen_option(netdissect_options
*ndo
, const u_char
*cp
,
68 #define MAX_RST_DATA_LEN 30
78 struct tcp_seq_hash
*nxt
;
90 struct tcp_seq_hash6
{
91 struct tcp_seq_hash6
*nxt
;
97 #define TSEQ_HASHSIZE 919
99 /* These tcp options do not have the size octet */
100 #define ZEROLENOPT(o) ((o) == TCPOPT_EOL || (o) == TCPOPT_NOP)
102 static struct tcp_seq_hash tcp_seq_hash4
[TSEQ_HASHSIZE
];
103 static struct tcp_seq_hash6 tcp_seq_hash6
[TSEQ_HASHSIZE
];
105 const struct tok tcp_flag_values
[] = {
118 static const struct tok tcp_option_values
[] = {
119 { TCPOPT_EOL
, "eol" },
120 { TCPOPT_NOP
, "nop" },
121 { TCPOPT_MAXSEG
, "mss" },
122 { TCPOPT_WSCALE
, "wscale" },
123 { TCPOPT_SACKOK
, "sackOK" },
124 { TCPOPT_SACK
, "sack" },
125 { TCPOPT_ECHO
, "echo" },
126 { TCPOPT_ECHOREPLY
, "echoreply" },
127 { TCPOPT_TIMESTAMP
, "TS" },
129 { TCPOPT_CCNEW
, "ccnew" },
130 { TCPOPT_CCECHO
, "ccecho" },
131 { TCPOPT_SIGNATURE
, "md5" },
132 { TCPOPT_SCPS
, "scps" },
133 { TCPOPT_UTO
, "uto" },
134 { TCPOPT_TCPAO
, "tcp-ao" },
135 { TCPOPT_MPTCP
, "mptcp" },
136 { TCPOPT_FASTOPEN
, "tfo" },
137 { TCPOPT_EXPERIMENT2
, "exp" },
142 tcp_cksum(netdissect_options
*ndo
,
144 const struct tcphdr
*tp
,
147 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)tp
, len
, len
,
152 tcp6_cksum(netdissect_options
*ndo
,
153 const struct ip6_hdr
*ip6
,
154 const struct tcphdr
*tp
,
157 return nextproto6_cksum(ndo
, ip6
, (const uint8_t *)tp
, len
, len
,
162 tcp_print(netdissect_options
*ndo
,
163 const u_char
*bp
, u_int length
,
164 const u_char
*bp2
, int fragmented
)
166 const struct tcphdr
*tp
;
171 uint16_t sport
, dport
, win
, urp
;
172 uint32_t seq
, ack
, thseq
, thack
;
176 const struct ip6_hdr
*ip6
;
177 u_int header_len
; /* Header length in bytes */
179 ndo
->ndo_protocol
= "tcp";
180 tp
= (const struct tcphdr
*)bp
;
181 ip
= (const struct ip
*)bp2
;
183 ip6
= (const struct ip6_hdr
*)bp2
;
187 if (!ND_TTEST_2(tp
->th_dport
)) {
190 GET_IP6ADDR_STRING(ip6
->ip6_src
),
191 GET_IP6ADDR_STRING(ip6
->ip6_dst
));
194 GET_IPADDR_STRING(ip
->ip_src
),
195 GET_IPADDR_STRING(ip
->ip_dst
));
197 nd_trunc_longjmp(ndo
);
200 sport
= GET_BE_U_2(tp
->th_sport
);
201 dport
= GET_BE_U_2(tp
->th_dport
);
204 if (GET_U_1(ip6
->ip6_nxt
) == IPPROTO_TCP
) {
205 ND_PRINT("%s.%s > %s.%s: ",
206 GET_IP6ADDR_STRING(ip6
->ip6_src
),
207 tcpport_string(ndo
, sport
),
208 GET_IP6ADDR_STRING(ip6
->ip6_dst
),
209 tcpport_string(ndo
, dport
));
211 ND_PRINT("%s > %s: ",
212 tcpport_string(ndo
, sport
), tcpport_string(ndo
, dport
));
215 if (GET_U_1(ip
->ip_p
) == IPPROTO_TCP
) {
216 ND_PRINT("%s.%s > %s.%s: ",
217 GET_IPADDR_STRING(ip
->ip_src
),
218 tcpport_string(ndo
, sport
),
219 GET_IPADDR_STRING(ip
->ip_dst
),
220 tcpport_string(ndo
, dport
));
222 ND_PRINT("%s > %s: ",
223 tcpport_string(ndo
, sport
), tcpport_string(ndo
, dport
));
227 hlen
= TH_OFF(tp
) * 4;
229 if (hlen
< sizeof(*tp
)) {
230 ND_PRINT(" tcp %u [bad hdr length %u - too short, < %zu]",
231 length
- hlen
, hlen
, sizeof(*tp
));
235 seq
= GET_BE_U_4(tp
->th_seq
);
236 ack
= GET_BE_U_4(tp
->th_ack
);
237 win
= GET_BE_U_2(tp
->th_win
);
238 urp
= GET_BE_U_2(tp
->th_urp
);
240 if (ndo
->ndo_qflag
) {
241 ND_PRINT("tcp %u", length
- hlen
);
243 ND_PRINT(" [bad hdr length %u - too long, > %u]",
250 flags
= tcp_get_flags(tp
);
251 ND_PRINT("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
253 if (!ndo
->ndo_Sflag
&& (flags
& TH_ACK
)) {
255 * Find (or record) the initial sequence numbers for
256 * this conversation. (we pick an arbitrary
257 * collating order so there's only one entry for
262 struct tcp_seq_hash6
*th
;
263 struct tcp_seq_hash6
*tcp_seq_hash
;
264 const void *src
, *dst
;
267 tcp_seq_hash
= tcp_seq_hash6
;
268 src
= (const void *)ip6
->ip6_src
;
269 dst
= (const void *)ip6
->ip6_dst
;
272 else if (sport
== dport
) {
273 if (UNALIGNED_MEMCMP(src
, dst
, sizeof(ip6
->ip6_dst
)) > 0)
277 UNALIGNED_MEMCPY(&tha
.src
, dst
, sizeof(ip6
->ip6_dst
));
278 UNALIGNED_MEMCPY(&tha
.dst
, src
, sizeof(ip6
->ip6_src
));
279 tha
.port
= ((u_int
)dport
) << 16 | sport
;
281 UNALIGNED_MEMCPY(&tha
.dst
, dst
, sizeof(ip6
->ip6_dst
));
282 UNALIGNED_MEMCPY(&tha
.src
, src
, sizeof(ip6
->ip6_src
));
283 tha
.port
= ((u_int
)sport
) << 16 | dport
;
286 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
287 th
->nxt
; th
= th
->nxt
)
288 if (memcmp((char *)&tha
, (char *)&th
->addr
,
289 sizeof(th
->addr
)) == 0)
292 if (!th
->nxt
|| (flags
& TH_SYN
)) {
293 /* didn't find it or new conversation */
294 /* calloc() return used by the 'tcp_seq_hash6'
295 hash table: do not free() */
296 if (th
->nxt
== NULL
) {
297 th
->nxt
= (struct tcp_seq_hash6
*)
298 calloc(1, sizeof(*th
));
300 (*ndo
->ndo_error
)(ndo
,
302 "%s: calloc", __func__
);
306 th
->ack
= seq
, th
->seq
= ack
- 1;
308 th
->seq
= seq
, th
->ack
= ack
- 1;
311 seq
-= th
->ack
, ack
-= th
->seq
;
313 seq
-= th
->seq
, ack
-= th
->ack
;
319 struct tcp_seq_hash
*th
;
320 struct tcp_seq_hash
*tcp_seq_hash
;
323 tcp_seq_hash
= tcp_seq_hash4
;
326 else if (sport
== dport
) {
327 if (UNALIGNED_MEMCMP(ip
->ip_src
, ip
->ip_dst
, sizeof(ip
->ip_dst
)) > 0)
331 UNALIGNED_MEMCPY(&tha
.src
, ip
->ip_dst
,
333 UNALIGNED_MEMCPY(&tha
.dst
, ip
->ip_src
,
335 tha
.port
= ((u_int
)dport
) << 16 | sport
;
337 UNALIGNED_MEMCPY(&tha
.dst
, ip
->ip_dst
,
339 UNALIGNED_MEMCPY(&tha
.src
, ip
->ip_src
,
341 tha
.port
= ((u_int
)sport
) << 16 | dport
;
344 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
345 th
->nxt
; th
= th
->nxt
)
346 if (memcmp((char *)&tha
, (char *)&th
->addr
,
347 sizeof(th
->addr
)) == 0)
350 if (!th
->nxt
|| (flags
& TH_SYN
)) {
351 /* didn't find it or new conversation */
352 /* calloc() return used by the 'tcp_seq_hash4'
353 hash table: do not free() */
354 if (th
->nxt
== NULL
) {
355 th
->nxt
= (struct tcp_seq_hash
*)
356 calloc(1, sizeof(*th
));
358 (*ndo
->ndo_error
)(ndo
,
360 "%s: calloc", __func__
);
364 th
->ack
= seq
, th
->seq
= ack
- 1;
366 th
->seq
= seq
, th
->ack
= ack
- 1;
369 seq
-= th
->ack
, ack
-= th
->seq
;
371 seq
-= th
->seq
, ack
-= th
->ack
;
379 thseq
= thack
= rev
= 0;
382 ND_PRINT(" [bad hdr length %u - too long, > %u]",
387 if (ndo
->ndo_vflag
&& !ndo
->ndo_Kflag
&& !fragmented
) {
388 /* Check the checksum, if possible. */
389 uint16_t sum
, tcp_sum
;
392 if (ND_TTEST_LEN(tp
->th_sport
, length
)) {
393 sum
= tcp_cksum(ndo
, ip
, tp
, length
);
394 tcp_sum
= GET_BE_U_2(tp
->th_sum
);
396 ND_PRINT(", cksum 0x%04x", tcp_sum
);
398 ND_PRINT(" (incorrect -> 0x%04x)",
399 in_cksum_shouldbe(tcp_sum
, sum
));
401 ND_PRINT(" (correct)");
403 } else if (IP_V(ip
) == 6) {
404 if (ND_TTEST_LEN(tp
->th_sport
, length
)) {
405 sum
= tcp6_cksum(ndo
, ip6
, tp
, length
);
406 tcp_sum
= GET_BE_U_2(tp
->th_sum
);
408 ND_PRINT(", cksum 0x%04x", tcp_sum
);
410 ND_PRINT(" (incorrect -> 0x%04x)",
411 in_cksum_shouldbe(tcp_sum
, sum
));
413 ND_PRINT(" (correct)");
420 if (ndo
->ndo_vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
421 ND_PRINT(", seq %u", seq
);
424 ND_PRINT(":%u", seq
+ length
);
428 if (flags
& TH_ACK
) {
429 ND_PRINT(", ack %u", ack
);
432 ND_PRINT(", win %u", win
);
435 ND_PRINT(", urg %u", urp
);
437 if (ndo
->ndo_vflag
> 1 && urp
!= 0)
438 ND_PRINT(", [urg %u != 0 while URG flag not set]", urp
);
440 * Handle any options.
442 if (hlen
> sizeof(*tp
)) {
444 u_int i
, opt
, datalen
;
448 cp
= (const u_char
*)tp
+ sizeof(*tp
);
449 ND_PRINT(", options [");
459 cp
++; /* total including type, len */
460 if (len
< 2 || len
> hlen
)
462 --hlen
; /* account for length byte */
464 --hlen
; /* account for type byte */
467 /* Bail if "l" bytes of data are not left or were not captured */
468 #define LENCHECK(l) { if ((l) > hlen) goto bad; }
471 ND_PRINT("%s", tok2str(tcp_option_values
, "unknown-%u", opt
));
478 ND_PRINT(" %u", GET_BE_U_2(cp
));
484 ND_PRINT(" %u", GET_U_1(cp
));
489 if (datalen
% 8 != 0) {
490 ND_PRINT(" invalid sack");
494 ND_PRINT(" %u ", datalen
/ 8);
495 for (i
= 0; i
< datalen
; i
+= 8) {
497 s
= GET_BE_U_4(cp
+ i
);
499 e
= GET_BE_U_4(cp
+ i
+ 4);
507 ND_PRINT("{%u:%u}", s
, e
);
516 case TCPOPT_ECHOREPLY
:
519 * those options share their semantics.
524 ND_PRINT(" %u", GET_BE_U_4(cp
));
527 case TCPOPT_TIMESTAMP
:
530 ND_PRINT(" val %u ecr %u",
535 case TCPOPT_SIGNATURE
:
536 datalen
= TCP_SIGLEN
;
538 ND_TCHECK_LEN(cp
, datalen
);
540 #ifdef HAVE_LIBCRYPTO
541 switch (tcp_verify_signature(ndo
, ip
, tp
,
542 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
544 case SIGNATURE_VALID
:
548 case SIGNATURE_INVALID
:
549 nd_print_invalid(ndo
);
552 case CANT_CHECK_SIGNATURE
:
553 ND_PRINT("can't check - ");
554 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
560 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
561 ND_PRINT("%02x", GET_U_1(cp
+ i
));
568 ND_PRINT(" cap %02x id %u", GET_U_1(cp
),
574 /* RFC 5925 Section 2.2:
575 * "The Length value MUST be greater than or equal to 4."
576 * (This includes the Kind and Length fields already processed
580 nd_print_invalid(ndo
);
583 ND_PRINT(" keyid %u", GET_U_1(cp
));
585 ND_PRINT(" rnextkeyid %u",
589 for (i
= 2; i
< datalen
; i
++) {
602 * Nothing interesting.
610 utoval
= GET_BE_U_2(cp
);
611 ND_PRINT(" 0x%x", utoval
);
613 utoval
= (utoval
>> 1) * 60;
616 ND_PRINT(" %u", utoval
);
621 const u_char
*snapend_save
;
626 /* FIXME: Proof-read mptcp_print() and if it
627 * always covers all bytes when it returns 1,
628 * only do ND_TCHECK_LEN() if it returned 0.
630 ND_TCHECK_LEN(cp
, datalen
);
631 /* Update the snapend to the end of the option
632 * before calling mptcp_print(). Some options
633 * (MPTCP or others) may be present after a
634 * MPTCP option. This prevents that, in
635 * mptcp_print(), the remaining length < the
638 snapend_save
= ndo
->ndo_snapend
;
639 ndo
->ndo_snapend
= ND_MIN(cp
- 2 + len
,
641 ret
= mptcp_print(ndo
, cp
- 2, len
, flags
);
642 ndo
->ndo_snapend
= snapend_save
;
648 case TCPOPT_FASTOPEN
:
651 ND_TCHECK_LEN(cp
, datalen
);
653 print_tcp_fastopen_option(ndo
, cp
, datalen
);
656 case TCPOPT_EXPERIMENT2
:
659 ND_TCHECK_LEN(cp
, datalen
);
663 magic
= GET_BE_U_2(cp
);
668 case 0xf989: /* TCP Fast Open RFC 7413 */
670 print_tcp_fastopen_option(ndo
, cp
+ 2, datalen
- 2);
674 /* Unknown magic number */
675 ND_PRINT("%04x", magic
);
684 for (i
= 0; i
< datalen
; ++i
) {
686 ND_PRINT("%02x", GET_U_1(cp
+ i
));
691 /* Account for data printed */
695 /* Check specification against observed length */
696 ++datalen
; /* option octet */
697 if (!ZEROLENOPT(opt
))
698 ++datalen
; /* size octet */
700 ND_PRINT("[len %u]", len
);
702 if (opt
== TCPOPT_EOL
)
709 * Print length field before crawling down the stack.
711 ND_PRINT(", length %u", length
);
717 * Decode payload if necessary.
719 header_len
= TH_OFF(tp
) * 4;
721 * Do a bounds check before decoding the payload.
722 * At least the header data is required.
724 if (!ND_TTEST_LEN(bp
, header_len
)) {
725 ND_PRINT(" [remaining caplen(%u) < header length(%u)]",
726 ND_BYTES_AVAILABLE_AFTER(bp
), header_len
);
727 nd_trunc_longjmp(ndo
);
730 if ((flags
& TH_RST
) && ndo
->ndo_vflag
) {
731 print_tcp_rst_data(ndo
, bp
, length
);
735 if (ndo
->ndo_packettype
) {
736 switch (ndo
->ndo_packettype
) {
738 zmtp1_print(ndo
, bp
, length
);
741 resp_print(ndo
, bp
, length
);
744 /* over_tcp: TRUE, is_mdns: FALSE */
745 domain_print(ndo
, bp
, length
, TRUE
, FALSE
);
751 if (IS_SRC_OR_DST_PORT(FTP_PORT
)) {
753 ftp_print(ndo
, bp
, length
);
754 } else if (IS_SRC_OR_DST_PORT(SSH_PORT
)) {
755 ssh_print(ndo
, bp
, length
);
756 } else if (IS_SRC_OR_DST_PORT(TELNET_PORT
)) {
757 telnet_print(ndo
, bp
, length
);
758 } else if (IS_SRC_OR_DST_PORT(SMTP_PORT
)) {
760 smtp_print(ndo
, bp
, length
);
761 } else if (IS_SRC_OR_DST_PORT(WHOIS_PORT
)) {
763 whois_print(ndo
, bp
, length
);
764 } else if (IS_SRC_OR_DST_PORT(NAMESERVER_PORT
)) {
765 /* over_tcp: TRUE, is_mdns: FALSE */
766 domain_print(ndo
, bp
, length
, TRUE
, FALSE
);
767 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT
)) {
769 http_print(ndo
, bp
, length
);
771 } else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT
)) {
772 nbt_tcp_print(ndo
, bp
, length
);
774 } else if (IS_SRC_OR_DST_PORT(BGP_PORT
)) {
775 bgp_print(ndo
, bp
, length
);
776 } else if (IS_SRC_OR_DST_PORT(RPKI_RTR_PORT
)) {
777 rpki_rtr_print(ndo
, bp
, length
);
779 } else if (IS_SRC_OR_DST_PORT(SMB_PORT
)) {
780 smb_tcp_print(ndo
, bp
, length
);
782 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT
)) {
784 rtsp_print(ndo
, bp
, length
);
785 } else if (IS_SRC_OR_DST_PORT(MSDP_PORT
)) {
786 msdp_print(ndo
, bp
, length
);
787 } else if (IS_SRC_OR_DST_PORT(LDP_PORT
)) {
788 ldp_print(ndo
, bp
, length
);
789 } else if (IS_SRC_OR_DST_PORT(PPTP_PORT
))
791 else if (IS_SRC_OR_DST_PORT(REDIS_PORT
))
792 resp_print(ndo
, bp
, length
);
793 else if (IS_SRC_OR_DST_PORT(BEEP_PORT
))
794 beep_print(ndo
, bp
, length
);
795 else if (IS_SRC_OR_DST_PORT(OPENFLOW_PORT_OLD
) || IS_SRC_OR_DST_PORT(OPENFLOW_PORT_IANA
)) {
796 openflow_print(ndo
, bp
, length
);
797 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT_ALT
)) {
799 http_print(ndo
, bp
, length
);
800 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT_ALT
)) {
802 rtsp_print(ndo
, bp
, length
);
803 } else if ((IS_SRC_OR_DST_PORT(NFS_PORT
)) &&
806 * If data present, header length valid, and NFS port used,
808 * Pass offset of data plus 4 bytes for RPC TCP msg length
809 * to NFS print routines.
812 const struct sunrpc_msg
*rp
;
813 enum sunrpc_msg_type direction
;
815 fraglen
= GET_BE_U_4(bp
) & 0x7FFFFFFF;
816 if (fraglen
> (length
) - 4)
817 fraglen
= (length
) - 4;
818 rp
= (const struct sunrpc_msg
*)(bp
+ 4);
819 if (ND_TTEST_4(rp
->rm_direction
)) {
820 direction
= (enum sunrpc_msg_type
) GET_BE_U_4(rp
->rm_direction
);
821 if (dport
== NFS_PORT
&& direction
== SUNRPC_CALL
) {
822 ND_PRINT(": NFS request xid %u ",
823 GET_BE_U_4(rp
->rm_xid
));
824 nfsreq_noaddr_print(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
827 if (sport
== NFS_PORT
&& direction
== SUNRPC_REPLY
) {
828 ND_PRINT(": NFS reply xid %u ",
829 GET_BE_U_4(rp
->rm_xid
));
830 nfsreply_noaddr_print(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
838 ND_PRINT("[bad opt]");
843 nd_print_invalid(ndo
);
847 * RFC1122 says the following on data in RST segments:
849 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
851 * A TCP SHOULD allow a received RST segment to include data.
854 * It has been suggested that a RST segment could contain
855 * ASCII text that encoded and explained the cause of the
856 * RST. No standard has yet been established for such
862 print_tcp_rst_data(netdissect_options
*ndo
,
863 const u_char
*sp
, u_int length
)
865 ND_PRINT(ND_TTEST_LEN(sp
, length
) ? " [RST" : " [!RST");
866 if (length
> MAX_RST_DATA_LEN
) {
867 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
868 ND_PRINT("+"); /* indicate we truncate */
871 (void)nd_printn(ndo
, sp
, length
, ndo
->ndo_snapend
);
876 print_tcp_fastopen_option(netdissect_options
*ndo
, const u_char
*cp
,
882 /* Fast Open Cookie Request */
883 ND_PRINT(" cookiereq");
885 /* Fast Open Cookie */
886 if (datalen
% 2 != 0 || datalen
< 4 || datalen
> 16) {
887 nd_print_invalid(ndo
);
889 ND_PRINT(" cookie ");
890 for (i
= 0; i
< datalen
; ++i
)
891 ND_PRINT("%02x", GET_U_1(cp
+ i
));
896 #ifdef HAVE_LIBCRYPTO
899 tcp_verify_signature(netdissect_options
*ndo
,
900 const struct ip
*ip
, const struct tcphdr
*tp
,
901 const u_char
*data
, u_int length
, const u_char
*rcvsig
)
904 u_char sig
[TCP_SIGLEN
];
907 uint16_t savecsum
, tlen
;
908 const struct ip6_hdr
*ip6
;
912 if (!ND_TTEST_LEN(data
, length
)) {
913 ND_PRINT("snaplen too short, ");
914 return (CANT_CHECK_SIGNATURE
);
919 if (ndo
->ndo_sigsecret
== NULL
) {
920 ND_PRINT("shared secret not supplied with -M, ");
921 return (CANT_CHECK_SIGNATURE
);
926 * Step 1: Update MD5 hash with IP pseudo-header.
929 MD5_Update(&ctx
, (const char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
930 MD5_Update(&ctx
, (const char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
931 MD5_Update(&ctx
, (const char *)&zero_proto
, sizeof(zero_proto
));
932 MD5_Update(&ctx
, (const char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
933 tlen
= GET_BE_U_2(ip
->ip_len
) - IP_HL(ip
) * 4;
935 MD5_Update(&ctx
, (const char *)&tlen
, sizeof(tlen
));
936 } else if (IP_V(ip
) == 6) {
937 ip6
= (const struct ip6_hdr
*)ip
;
938 MD5_Update(&ctx
, (const char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
939 MD5_Update(&ctx
, (const char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
940 len32
= htonl(GET_BE_U_2(ip6
->ip6_plen
));
941 MD5_Update(&ctx
, (const char *)&len32
, sizeof(len32
));
943 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
944 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
945 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
947 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
949 ND_PRINT("IP version not 4 or 6, ");
950 return (CANT_CHECK_SIGNATURE
);
954 * Step 2: Update MD5 hash with TCP header, excluding options.
955 * The TCP checksum must be set to zero.
957 memcpy(&savecsum
, tp1
.th_sum
, sizeof(savecsum
));
958 memset(tp1
.th_sum
, 0, sizeof(tp1
.th_sum
));
959 MD5_Update(&ctx
, (const char *)&tp1
, sizeof(struct tcphdr
));
960 memcpy(tp1
.th_sum
, &savecsum
, sizeof(tp1
.th_sum
));
962 * Step 3: Update MD5 hash with TCP segment data, if present.
965 MD5_Update(&ctx
, data
, length
);
967 * Step 4: Update MD5 hash with shared secret.
969 MD5_Update(&ctx
, ndo
->ndo_sigsecret
, strlen(ndo
->ndo_sigsecret
));
970 MD5_Final(sig
, &ctx
);
972 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
973 return (SIGNATURE_VALID
);
975 return (SIGNATURE_INVALID
);
978 #endif /* HAVE_LIBCRYPTO */