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 $");
37 #include "netdissect-stdinc.h"
42 #define ND_LONGJMP_FROM_TCHECK
43 #include "netdissect.h"
44 #include "addrtoname.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
[] = {
117 static const struct tok tcp_option_values
[] = {
118 { TCPOPT_EOL
, "eol" },
119 { TCPOPT_NOP
, "nop" },
120 { TCPOPT_MAXSEG
, "mss" },
121 { TCPOPT_WSCALE
, "wscale" },
122 { TCPOPT_SACKOK
, "sackOK" },
123 { TCPOPT_SACK
, "sack" },
124 { TCPOPT_ECHO
, "echo" },
125 { TCPOPT_ECHOREPLY
, "echoreply" },
126 { TCPOPT_TIMESTAMP
, "TS" },
128 { TCPOPT_CCNEW
, "ccnew" },
129 { TCPOPT_CCECHO
, "" },
130 { TCPOPT_SIGNATURE
, "md5" },
131 { TCPOPT_SCPS
, "scps" },
132 { TCPOPT_UTO
, "uto" },
133 { TCPOPT_TCPAO
, "tcp-ao" },
134 { TCPOPT_MPTCP
, "mptcp" },
135 { TCPOPT_FASTOPEN
, "tfo" },
136 { TCPOPT_EXPERIMENT2
, "exp" },
141 tcp_cksum(netdissect_options
*ndo
,
143 const struct tcphdr
*tp
,
146 return nextproto4_cksum(ndo
, ip
, (const uint8_t *)tp
, len
, len
,
151 tcp6_cksum(netdissect_options
*ndo
,
152 const struct ip6_hdr
*ip6
,
153 const struct tcphdr
*tp
,
156 return nextproto6_cksum(ndo
, ip6
, (const uint8_t *)tp
, len
, len
,
161 tcp_print(netdissect_options
*ndo
,
162 const u_char
*bp
, u_int length
,
163 const u_char
*bp2
, int fragmented
)
165 const struct tcphdr
*tp
;
170 uint16_t sport
, dport
, win
, urp
;
171 uint32_t seq
, ack
, thseq
, thack
;
175 const struct ip6_hdr
*ip6
;
176 u_int header_len
; /* Header length in bytes */
178 ndo
->ndo_protocol
= "tcp";
179 tp
= (const struct tcphdr
*)bp
;
180 ip
= (const struct ip
*)bp2
;
182 ip6
= (const struct ip6_hdr
*)bp2
;
186 if (!ND_TTEST_2(tp
->th_dport
)) {
189 GET_IP6ADDR_STRING(ip6
->ip6_src
),
190 GET_IP6ADDR_STRING(ip6
->ip6_dst
));
193 GET_IPADDR_STRING(ip
->ip_src
),
194 GET_IPADDR_STRING(ip
->ip_dst
));
196 nd_trunc_longjmp(ndo
);
199 sport
= GET_BE_U_2(tp
->th_sport
);
200 dport
= GET_BE_U_2(tp
->th_dport
);
203 if (GET_U_1(ip6
->ip6_nxt
) == IPPROTO_TCP
) {
204 ND_PRINT("%s.%s > %s.%s: ",
205 GET_IP6ADDR_STRING(ip6
->ip6_src
),
206 tcpport_string(ndo
, sport
),
207 GET_IP6ADDR_STRING(ip6
->ip6_dst
),
208 tcpport_string(ndo
, dport
));
210 ND_PRINT("%s > %s: ",
211 tcpport_string(ndo
, sport
), tcpport_string(ndo
, dport
));
214 if (GET_U_1(ip
->ip_p
) == IPPROTO_TCP
) {
215 ND_PRINT("%s.%s > %s.%s: ",
216 GET_IPADDR_STRING(ip
->ip_src
),
217 tcpport_string(ndo
, sport
),
218 GET_IPADDR_STRING(ip
->ip_dst
),
219 tcpport_string(ndo
, dport
));
221 ND_PRINT("%s > %s: ",
222 tcpport_string(ndo
, sport
), tcpport_string(ndo
, dport
));
226 hlen
= TH_OFF(tp
) * 4;
228 if (hlen
< sizeof(*tp
)) {
229 ND_PRINT(" tcp %u [bad hdr length %u - too short, < %zu]",
230 length
- hlen
, hlen
, sizeof(*tp
));
234 seq
= GET_BE_U_4(tp
->th_seq
);
235 ack
= GET_BE_U_4(tp
->th_ack
);
236 win
= GET_BE_U_2(tp
->th_win
);
237 urp
= GET_BE_U_2(tp
->th_urp
);
239 if (ndo
->ndo_qflag
) {
240 ND_PRINT("tcp %u", length
- hlen
);
242 ND_PRINT(" [bad hdr length %u - too long, > %u]",
249 flags
= GET_U_1(tp
->th_flags
);
250 ND_PRINT("Flags [%s]", bittok2str_nosep(tcp_flag_values
, "none", flags
));
252 if (!ndo
->ndo_Sflag
&& (flags
& TH_ACK
)) {
254 * Find (or record) the initial sequence numbers for
255 * this conversation. (we pick an arbitrary
256 * collating order so there's only one entry for
261 struct tcp_seq_hash6
*th
;
262 struct tcp_seq_hash6
*tcp_seq_hash
;
263 const void *src
, *dst
;
266 tcp_seq_hash
= tcp_seq_hash6
;
267 src
= (const void *)ip6
->ip6_src
;
268 dst
= (const void *)ip6
->ip6_dst
;
271 else if (sport
== dport
) {
272 if (UNALIGNED_MEMCMP(src
, dst
, sizeof(ip6
->ip6_dst
)) > 0)
276 UNALIGNED_MEMCPY(&tha
.src
, dst
, sizeof(ip6
->ip6_dst
));
277 UNALIGNED_MEMCPY(&tha
.dst
, src
, sizeof(ip6
->ip6_src
));
278 tha
.port
= ((u_int
)dport
) << 16 | sport
;
280 UNALIGNED_MEMCPY(&tha
.dst
, dst
, sizeof(ip6
->ip6_dst
));
281 UNALIGNED_MEMCPY(&tha
.src
, src
, sizeof(ip6
->ip6_src
));
282 tha
.port
= ((u_int
)sport
) << 16 | dport
;
285 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
286 th
->nxt
; th
= th
->nxt
)
287 if (memcmp((char *)&tha
, (char *)&th
->addr
,
288 sizeof(th
->addr
)) == 0)
291 if (!th
->nxt
|| (flags
& TH_SYN
)) {
292 /* didn't find it or new conversation */
293 /* calloc() return used by the 'tcp_seq_hash6'
294 hash table: do not free() */
295 if (th
->nxt
== NULL
) {
296 th
->nxt
= (struct tcp_seq_hash6
*)
297 calloc(1, sizeof(*th
));
299 (*ndo
->ndo_error
)(ndo
,
301 "%s: calloc", __func__
);
305 th
->ack
= seq
, th
->seq
= ack
- 1;
307 th
->seq
= seq
, th
->ack
= ack
- 1;
310 seq
-= th
->ack
, ack
-= th
->seq
;
312 seq
-= th
->seq
, ack
-= th
->ack
;
318 struct tcp_seq_hash
*th
;
319 struct tcp_seq_hash
*tcp_seq_hash
;
322 tcp_seq_hash
= tcp_seq_hash4
;
325 else if (sport
== dport
) {
326 if (UNALIGNED_MEMCMP(ip
->ip_src
, ip
->ip_dst
, sizeof(ip
->ip_dst
)) > 0)
330 UNALIGNED_MEMCPY(&tha
.src
, ip
->ip_dst
,
332 UNALIGNED_MEMCPY(&tha
.dst
, ip
->ip_src
,
334 tha
.port
= ((u_int
)dport
) << 16 | sport
;
336 UNALIGNED_MEMCPY(&tha
.dst
, ip
->ip_dst
,
338 UNALIGNED_MEMCPY(&tha
.src
, ip
->ip_src
,
340 tha
.port
= ((u_int
)sport
) << 16 | dport
;
343 for (th
= &tcp_seq_hash
[tha
.port
% TSEQ_HASHSIZE
];
344 th
->nxt
; th
= th
->nxt
)
345 if (memcmp((char *)&tha
, (char *)&th
->addr
,
346 sizeof(th
->addr
)) == 0)
349 if (!th
->nxt
|| (flags
& TH_SYN
)) {
350 /* didn't find it or new conversation */
351 /* calloc() return used by the 'tcp_seq_hash4'
352 hash table: do not free() */
353 if (th
->nxt
== NULL
) {
354 th
->nxt
= (struct tcp_seq_hash
*)
355 calloc(1, sizeof(*th
));
357 (*ndo
->ndo_error
)(ndo
,
359 "%s: calloc", __func__
);
363 th
->ack
= seq
, th
->seq
= ack
- 1;
365 th
->seq
= seq
, th
->ack
= ack
- 1;
368 seq
-= th
->ack
, ack
-= th
->seq
;
370 seq
-= th
->seq
, ack
-= th
->ack
;
378 thseq
= thack
= rev
= 0;
381 ND_PRINT(" [bad hdr length %u - too long, > %u]",
386 if (ndo
->ndo_vflag
&& !ndo
->ndo_Kflag
&& !fragmented
) {
387 /* Check the checksum, if possible. */
388 uint16_t sum
, tcp_sum
;
391 if (ND_TTEST_LEN(tp
->th_sport
, length
)) {
392 sum
= tcp_cksum(ndo
, ip
, tp
, length
);
393 tcp_sum
= GET_BE_U_2(tp
->th_sum
);
395 ND_PRINT(", cksum 0x%04x", tcp_sum
);
397 ND_PRINT(" (incorrect -> 0x%04x)",
398 in_cksum_shouldbe(tcp_sum
, sum
));
400 ND_PRINT(" (correct)");
402 } else if (IP_V(ip
) == 6) {
403 if (ND_TTEST_LEN(tp
->th_sport
, length
)) {
404 sum
= tcp6_cksum(ndo
, ip6
, tp
, length
);
405 tcp_sum
= GET_BE_U_2(tp
->th_sum
);
407 ND_PRINT(", cksum 0x%04x", tcp_sum
);
409 ND_PRINT(" (incorrect -> 0x%04x)",
410 in_cksum_shouldbe(tcp_sum
, sum
));
412 ND_PRINT(" (correct)");
419 if (ndo
->ndo_vflag
> 1 || length
> 0 || flags
& (TH_SYN
| TH_FIN
| TH_RST
)) {
420 ND_PRINT(", seq %u", seq
);
423 ND_PRINT(":%u", seq
+ length
);
427 if (flags
& TH_ACK
) {
428 ND_PRINT(", ack %u", ack
);
431 ND_PRINT(", win %u", win
);
434 ND_PRINT(", urg %u", urp
);
436 * Handle any options.
438 if (hlen
> sizeof(*tp
)) {
440 u_int i
, opt
, datalen
;
444 cp
= (const u_char
*)tp
+ sizeof(*tp
);
445 ND_PRINT(", options [");
455 cp
++; /* total including type, len */
456 if (len
< 2 || len
> hlen
)
458 --hlen
; /* account for length byte */
460 --hlen
; /* account for type byte */
463 /* Bail if "l" bytes of data are not left or were not captured */
464 #define LENCHECK(l) { if ((l) > hlen) goto bad; }
467 ND_PRINT("%s", tok2str(tcp_option_values
, "unknown-%u", opt
));
474 ND_PRINT(" %u", GET_BE_U_2(cp
));
480 ND_PRINT(" %u", GET_U_1(cp
));
485 if (datalen
% 8 != 0) {
486 ND_PRINT(" invalid sack");
490 ND_PRINT(" %u ", datalen
/ 8);
491 for (i
= 0; i
< datalen
; i
+= 8) {
493 s
= GET_BE_U_4(cp
+ i
);
495 e
= GET_BE_U_4(cp
+ i
+ 4);
503 ND_PRINT("{%u:%u}", s
, e
);
512 case TCPOPT_ECHOREPLY
:
515 * those options share their semantics.
520 ND_PRINT(" %u", GET_BE_U_4(cp
));
523 case TCPOPT_TIMESTAMP
:
526 ND_PRINT(" val %u ecr %u",
531 case TCPOPT_SIGNATURE
:
532 datalen
= TCP_SIGLEN
;
534 ND_TCHECK_LEN(cp
, datalen
);
536 #ifdef HAVE_LIBCRYPTO
537 switch (tcp_verify_signature(ndo
, ip
, tp
,
538 bp
+ TH_OFF(tp
) * 4, length
, cp
)) {
540 case SIGNATURE_VALID
:
544 case SIGNATURE_INVALID
:
545 nd_print_invalid(ndo
);
548 case CANT_CHECK_SIGNATURE
:
549 ND_PRINT("can't check - ");
550 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
556 for (i
= 0; i
< TCP_SIGLEN
; ++i
)
557 ND_PRINT("%02x", GET_U_1(cp
+ i
));
564 ND_PRINT(" cap %02x id %u", GET_U_1(cp
),
570 /* RFC 5925 Section 2.2:
571 * "The Length value MUST be greater than or equal to 4."
572 * (This includes the Kind and Length fields already processed
576 nd_print_invalid(ndo
);
579 ND_PRINT(" keyid %u", GET_U_1(cp
));
581 ND_PRINT(" rnextkeyid %u",
585 for (i
= 2; i
< datalen
; i
++) {
598 * Nothing interesting.
606 utoval
= GET_BE_U_2(cp
);
607 ND_PRINT(" 0x%x", utoval
);
609 utoval
= (utoval
>> 1) * 60;
612 ND_PRINT(" %u", utoval
);
617 const u_char
*snapend_save
;
622 /* FIXME: Proof-read mptcp_print() and if it
623 * always covers all bytes when it returns 1,
624 * only do ND_TCHECK_LEN() if it returned 0.
626 ND_TCHECK_LEN(cp
, datalen
);
627 /* Update the snapend to the end of the option
628 * before calling mptcp_print(). Some options
629 * (MPTCP or others) may be present after a
630 * MPTCP option. This prevents that, in
631 * mptcp_print(), the remaining length < the
634 snapend_save
= ndo
->ndo_snapend
;
635 ndo
->ndo_snapend
= ND_MIN(cp
- 2 + len
,
637 ret
= mptcp_print(ndo
, cp
- 2, len
, flags
);
638 ndo
->ndo_snapend
= snapend_save
;
644 case TCPOPT_FASTOPEN
:
647 ND_TCHECK_LEN(cp
, datalen
);
649 print_tcp_fastopen_option(ndo
, cp
, datalen
);
652 case TCPOPT_EXPERIMENT2
:
655 ND_TCHECK_LEN(cp
, datalen
);
659 magic
= GET_BE_U_2(cp
);
664 case 0xf989: /* TCP Fast Open RFC 7413 */
666 print_tcp_fastopen_option(ndo
, cp
+ 2, datalen
- 2);
670 /* Unknown magic number */
671 ND_PRINT("%04x", magic
);
680 for (i
= 0; i
< datalen
; ++i
) {
682 ND_PRINT("%02x", GET_U_1(cp
+ i
));
687 /* Account for data printed */
691 /* Check specification against observed length */
692 ++datalen
; /* option octet */
693 if (!ZEROLENOPT(opt
))
694 ++datalen
; /* size octet */
696 ND_PRINT("[len %u]", len
);
698 if (opt
== TCPOPT_EOL
)
705 * Print length field before crawling down the stack.
707 ND_PRINT(", length %u", length
);
713 * Decode payload if necessary.
715 header_len
= TH_OFF(tp
) * 4;
717 * Do a bounds check before decoding the payload.
718 * At least the header data is required.
720 if (!ND_TTEST_LEN(bp
, header_len
)) {
721 ND_PRINT(" [remaining caplen(%u) < header length(%u)]",
722 ND_BYTES_AVAILABLE_AFTER(bp
), header_len
);
723 nd_trunc_longjmp(ndo
);
726 if ((flags
& TH_RST
) && ndo
->ndo_vflag
) {
727 print_tcp_rst_data(ndo
, bp
, length
);
731 if (ndo
->ndo_packettype
) {
732 switch (ndo
->ndo_packettype
) {
734 zmtp1_print(ndo
, bp
, length
);
737 resp_print(ndo
, bp
, length
);
740 /* over_tcp: TRUE, is_mdns: FALSE */
741 domain_print(ndo
, bp
, length
, TRUE
, FALSE
);
747 if (IS_SRC_OR_DST_PORT(TELNET_PORT
)) {
748 telnet_print(ndo
, bp
, length
);
749 } else if (IS_SRC_OR_DST_PORT(SMTP_PORT
)) {
751 smtp_print(ndo
, bp
, length
);
752 } else if (IS_SRC_OR_DST_PORT(WHOIS_PORT
)) {
754 ndo
->ndo_protocol
= "whois"; /* needed by txtproto_print() */
755 txtproto_print(ndo
, bp
, length
, NULL
, 0); /* RFC 3912 */
756 } else if (IS_SRC_OR_DST_PORT(BGP_PORT
))
757 bgp_print(ndo
, bp
, length
);
758 else if (IS_SRC_OR_DST_PORT(PPTP_PORT
))
760 else if (IS_SRC_OR_DST_PORT(REDIS_PORT
))
761 resp_print(ndo
, bp
, length
);
762 else if (IS_SRC_OR_DST_PORT(SSH_PORT
))
763 ssh_print(ndo
, bp
, length
);
765 else if (IS_SRC_OR_DST_PORT(NETBIOS_SSN_PORT
))
766 nbt_tcp_print(ndo
, bp
, length
);
767 else if (IS_SRC_OR_DST_PORT(SMB_PORT
))
768 smb_tcp_print(ndo
, bp
, length
);
770 else if (IS_SRC_OR_DST_PORT(BEEP_PORT
))
771 beep_print(ndo
, bp
, length
);
772 else if (IS_SRC_OR_DST_PORT(OPENFLOW_PORT_OLD
) || IS_SRC_OR_DST_PORT(OPENFLOW_PORT_IANA
))
773 openflow_print(ndo
, bp
, length
);
774 else if (IS_SRC_OR_DST_PORT(FTP_PORT
)) {
776 ftp_print(ndo
, bp
, length
);
777 } else if (IS_SRC_OR_DST_PORT(HTTP_PORT
) || IS_SRC_OR_DST_PORT(HTTP_PORT_ALT
)) {
779 http_print(ndo
, bp
, length
);
780 } else if (IS_SRC_OR_DST_PORT(RTSP_PORT
) || IS_SRC_OR_DST_PORT(RTSP_PORT_ALT
)) {
782 rtsp_print(ndo
, bp
, length
);
783 } else if (IS_SRC_OR_DST_PORT(NAMESERVER_PORT
)) {
784 /* over_tcp: TRUE, is_mdns: FALSE */
785 domain_print(ndo
, bp
, length
, TRUE
, FALSE
);
786 } else if (IS_SRC_OR_DST_PORT(MSDP_PORT
)) {
787 msdp_print(ndo
, bp
, length
);
788 } else if (IS_SRC_OR_DST_PORT(RPKI_RTR_PORT
)) {
789 rpki_rtr_print(ndo
, bp
, length
);
790 } else if (IS_SRC_OR_DST_PORT(LDP_PORT
)) {
791 ldp_print(ndo
, bp
, length
);
792 } else if ((IS_SRC_OR_DST_PORT(NFS_PORT
)) &&
795 * If data present, header length valid, and NFS port used,
797 * Pass offset of data plus 4 bytes for RPC TCP msg length
798 * to NFS print routines.
801 const struct sunrpc_msg
*rp
;
802 enum sunrpc_msg_type direction
;
804 fraglen
= GET_BE_U_4(bp
) & 0x7FFFFFFF;
805 if (fraglen
> (length
) - 4)
806 fraglen
= (length
) - 4;
807 rp
= (const struct sunrpc_msg
*)(bp
+ 4);
808 if (ND_TTEST_4(rp
->rm_direction
)) {
809 direction
= (enum sunrpc_msg_type
) GET_BE_U_4(rp
->rm_direction
);
810 if (dport
== NFS_PORT
&& direction
== SUNRPC_CALL
) {
811 ND_PRINT(": NFS request xid %u ",
812 GET_BE_U_4(rp
->rm_xid
));
813 nfsreq_noaddr_print(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
816 if (sport
== NFS_PORT
&& direction
== SUNRPC_REPLY
) {
817 ND_PRINT(": NFS reply xid %u ",
818 GET_BE_U_4(rp
->rm_xid
));
819 nfsreply_noaddr_print(ndo
, (const u_char
*)rp
, fraglen
, (const u_char
*)ip
);
827 ND_PRINT("[bad opt]");
832 nd_print_invalid(ndo
);
836 * RFC1122 says the following on data in RST segments:
838 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
840 * A TCP SHOULD allow a received RST segment to include data.
843 * It has been suggested that a RST segment could contain
844 * ASCII text that encoded and explained the cause of the
845 * RST. No standard has yet been established for such
851 print_tcp_rst_data(netdissect_options
*ndo
,
852 const u_char
*sp
, u_int length
)
854 ND_PRINT(ND_TTEST_LEN(sp
, length
) ? " [RST" : " [!RST");
855 if (length
> MAX_RST_DATA_LEN
) {
856 length
= MAX_RST_DATA_LEN
; /* can use -X for longer */
857 ND_PRINT("+"); /* indicate we truncate */
860 (void)nd_printn(ndo
, sp
, length
, ndo
->ndo_snapend
);
865 print_tcp_fastopen_option(netdissect_options
*ndo
, const u_char
*cp
,
871 /* Fast Open Cookie Request */
872 ND_PRINT(" cookiereq");
874 /* Fast Open Cookie */
875 if (datalen
% 2 != 0 || datalen
< 4 || datalen
> 16) {
876 nd_print_invalid(ndo
);
878 ND_PRINT(" cookie ");
879 for (i
= 0; i
< datalen
; ++i
)
880 ND_PRINT("%02x", GET_U_1(cp
+ i
));
885 #ifdef HAVE_LIBCRYPTO
886 USES_APPLE_DEPRECATED_API
888 tcp_verify_signature(netdissect_options
*ndo
,
889 const struct ip
*ip
, const struct tcphdr
*tp
,
890 const u_char
*data
, u_int length
, const u_char
*rcvsig
)
893 u_char sig
[TCP_SIGLEN
];
896 uint16_t savecsum
, tlen
;
897 const struct ip6_hdr
*ip6
;
901 if (!ND_TTEST_LEN(data
, length
)) {
902 ND_PRINT("snaplen too short, ");
903 return (CANT_CHECK_SIGNATURE
);
908 if (ndo
->ndo_sigsecret
== NULL
) {
909 ND_PRINT("shared secret not supplied with -M, ");
910 return (CANT_CHECK_SIGNATURE
);
915 * Step 1: Update MD5 hash with IP pseudo-header.
918 MD5_Update(&ctx
, (const char *)&ip
->ip_src
, sizeof(ip
->ip_src
));
919 MD5_Update(&ctx
, (const char *)&ip
->ip_dst
, sizeof(ip
->ip_dst
));
920 MD5_Update(&ctx
, (const char *)&zero_proto
, sizeof(zero_proto
));
921 MD5_Update(&ctx
, (const char *)&ip
->ip_p
, sizeof(ip
->ip_p
));
922 tlen
= GET_BE_U_2(ip
->ip_len
) - IP_HL(ip
) * 4;
924 MD5_Update(&ctx
, (const char *)&tlen
, sizeof(tlen
));
925 } else if (IP_V(ip
) == 6) {
926 ip6
= (const struct ip6_hdr
*)ip
;
927 MD5_Update(&ctx
, (const char *)&ip6
->ip6_src
, sizeof(ip6
->ip6_src
));
928 MD5_Update(&ctx
, (const char *)&ip6
->ip6_dst
, sizeof(ip6
->ip6_dst
));
929 len32
= htonl(GET_BE_U_2(ip6
->ip6_plen
));
930 MD5_Update(&ctx
, (const char *)&len32
, sizeof(len32
));
932 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
933 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
934 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
936 MD5_Update(&ctx
, (const char *)&nxt
, sizeof(nxt
));
938 ND_PRINT("IP version not 4 or 6, ");
939 return (CANT_CHECK_SIGNATURE
);
943 * Step 2: Update MD5 hash with TCP header, excluding options.
944 * The TCP checksum must be set to zero.
946 memcpy(&savecsum
, tp1
.th_sum
, sizeof(savecsum
));
947 memset(tp1
.th_sum
, 0, sizeof(tp1
.th_sum
));
948 MD5_Update(&ctx
, (const char *)&tp1
, sizeof(struct tcphdr
));
949 memcpy(tp1
.th_sum
, &savecsum
, sizeof(tp1
.th_sum
));
951 * Step 3: Update MD5 hash with TCP segment data, if present.
954 MD5_Update(&ctx
, data
, length
);
956 * Step 4: Update MD5 hash with shared secret.
958 MD5_Update(&ctx
, ndo
->ndo_sigsecret
, strlen(ndo
->ndo_sigsecret
));
959 MD5_Final(sig
, &ctx
);
961 if (memcmp(rcvsig
, sig
, TCP_SIGLEN
) == 0)
962 return (SIGNATURE_VALID
);
964 return (SIGNATURE_INVALID
);
967 #endif /* HAVE_LIBCRYPTO */