2 * Routines for TCP packet disassembly
4 * $Id: packet-tcp.c,v 1.97 2001/01/09 06:31:44 guy Exp $
6 * Ethereal - Network traffic analyzer
7 * By Gerald Combs <gerald@zing.org>
8 * Copyright 1998 Gerald Combs
11 * This program is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU General Public License
13 * as published by the Free Software Foundation; either version 2
14 * of the License, or (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
43 #ifdef NEED_SNPRINTF_H
44 # include "snprintf.h"
52 #include "packet-tcp.h"
53 #include "packet-ip.h"
54 #include "conversation.h"
57 /* Place TCP summary in proto tree */
58 static gboolean tcp_summary_in_tree = TRUE;
60 extern FILE* data_out_file;
62 guint16 tcp_urgent_pointer;
64 static int proto_tcp = -1;
65 static int hf_tcp_srcport = -1;
66 static int hf_tcp_dstport = -1;
67 static int hf_tcp_port = -1;
68 static int hf_tcp_seq = -1;
69 static int hf_tcp_nxtseq = -1;
70 static int hf_tcp_ack = -1;
71 static int hf_tcp_hdr_len = -1;
72 static int hf_tcp_flags = -1;
73 static int hf_tcp_flags_cwr = -1;
74 static int hf_tcp_flags_ecn = -1;
75 static int hf_tcp_flags_urg = -1;
76 static int hf_tcp_flags_ack = -1;
77 static int hf_tcp_flags_push = -1;
78 static int hf_tcp_flags_reset = -1;
79 static int hf_tcp_flags_syn = -1;
80 static int hf_tcp_flags_fin = -1;
81 static int hf_tcp_window_size = -1;
82 static int hf_tcp_checksum = -1;
83 static int hf_tcp_urgent_pointer = -1;
85 static gint ett_tcp = -1;
86 static gint ett_tcp_flags = -1;
87 static gint ett_tcp_options = -1;
88 static gint ett_tcp_option_sack = -1;
90 static dissector_table_t subdissector_table;
91 static heur_dissector_list_t heur_subdissector_list;
92 static conv_dissector_list_t conv_subdissector_list;
96 #define TCP_PORT_SMTP 25
98 /* TCP structs and definitions */
100 typedef struct _e_tcphdr {
105 guint8 th_off_x2; /* combines th_off and th_x2 */
124 #define TCPOPT_NOP 1 /* Padding */
125 #define TCPOPT_EOL 0 /* End of options */
126 #define TCPOPT_MSS 2 /* Segment size negotiating */
127 #define TCPOPT_WINDOW 3 /* Window scaling */
128 #define TCPOPT_SACK_PERM 4 /* SACK Permitted */
129 #define TCPOPT_SACK 5 /* SACK Block */
130 #define TCPOPT_ECHO 6
131 #define TCPOPT_ECHOREPLY 7
132 #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
134 #define TCPOPT_CCNEW 12
135 #define TCPOPT_CCECHO 13
136 #define TCPOPT_MD5 19 /* RFC2385 */
142 #define TCPOLEN_MSS 4
143 #define TCPOLEN_WINDOW 3
144 #define TCPOLEN_SACK_PERM 2
145 #define TCPOLEN_SACK_MIN 2
146 #define TCPOLEN_ECHO 6
147 #define TCPOLEN_ECHOREPLY 6
148 #define TCPOLEN_TIMESTAMP 10
150 #define TCPOLEN_CCNEW 6
151 #define TCPOLEN_CCECHO 6
152 #define TCPOLEN_MD5 18
155 tcp_info_append_uint(frame_data *fd, const char *abbrev, guint32 val)
157 if (check_col(fd, COL_INFO))
158 col_append_fstr(fd, COL_INFO, " %s=%u", abbrev, val);
162 dissect_tcpopt_maxseg(const ip_tcp_opt *optp, tvbuff_t *tvb,
163 int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
167 mss = tvb_get_ntohs(tvb, offset + 2);
168 proto_tree_add_text(opt_tree, tvb, offset, optlen,
169 "%s: %u bytes", optp->name, mss);
170 tcp_info_append_uint(fd, "MSS", mss);
174 dissect_tcpopt_wscale(const ip_tcp_opt *optp, tvbuff_t *tvb,
175 int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
179 ws = tvb_get_guint8(tvb, offset + 2);
180 proto_tree_add_text(opt_tree, tvb, offset, optlen,
181 "%s: %u bytes", optp->name, ws);
182 tcp_info_append_uint(fd, "WS", ws);
186 dissect_tcpopt_sack(const ip_tcp_opt *optp, tvbuff_t *tvb,
187 int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
189 proto_tree *field_tree = NULL;
191 guint leftedge, rightedge;
193 tf = proto_tree_add_text(opt_tree, tvb, offset, optlen, "%s:", optp->name);
194 offset += 2; /* skip past type and length */
195 optlen -= 2; /* subtract size of type and length */
197 if (field_tree == NULL) {
198 /* Haven't yet made a subtree out of this option. Do so. */
199 field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
202 proto_tree_add_text(field_tree, tvb, offset, optlen,
203 "(suboption would go past end of option)");
206 leftedge = tvb_get_ntohl(tvb, offset);
209 proto_tree_add_text(field_tree, tvb, offset, optlen,
210 "(suboption would go past end of option)");
213 /* XXX - check whether it goes past end of packet */
214 rightedge = tvb_get_ntohl(tvb, offset + 4);
216 proto_tree_add_text(field_tree, tvb, offset, 8,
217 "left edge = %u, right edge = %u", leftedge, rightedge);
218 tcp_info_append_uint(fd, "SLE", leftedge);
219 tcp_info_append_uint(fd, "SRE", rightedge);
225 dissect_tcpopt_echo(const ip_tcp_opt *optp, tvbuff_t *tvb,
226 int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
230 echo = tvb_get_ntohl(tvb, offset + 2);
231 proto_tree_add_text(opt_tree, tvb, offset, optlen,
232 "%s: %u", optp->name, echo);
233 tcp_info_append_uint(fd, "ECHO", echo);
237 dissect_tcpopt_timestamp(const ip_tcp_opt *optp, tvbuff_t *tvb,
238 int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
242 tsv = tvb_get_ntohl(tvb, offset + 2);
243 tser = tvb_get_ntohl(tvb, offset + 6);
244 proto_tree_add_text(opt_tree, tvb, offset, optlen,
245 "%s: tsval %u, tsecr %u", optp->name, tsv, tser);
246 tcp_info_append_uint(fd, "TSV", tsv);
247 tcp_info_append_uint(fd, "TSER", tser);
251 dissect_tcpopt_cc(const ip_tcp_opt *optp, tvbuff_t *tvb,
252 int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
256 cc = tvb_get_ntohl(tvb, offset + 2);
257 proto_tree_add_text(opt_tree, tvb, offset, optlen,
258 "%s: %u", optp->name, cc);
259 tcp_info_append_uint(fd, "CC", cc);
262 static const ip_tcp_opt tcpopts[] = {
281 "Maximum segment size",
285 dissect_tcpopt_maxseg
293 dissect_tcpopt_wscale
306 &ett_tcp_option_sack,
333 dissect_tcpopt_timestamp
369 #define N_TCP_OPTS (sizeof tcpopts / sizeof tcpopts[0])
372 static const true_false_string flags_set_truth = {
378 /* Determine if there is a sub-dissector and call it. This has been */
379 /* separated into a stand alone routine to other protocol dissectors */
380 /* can call to it, ie. socks */
383 decode_tcp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
384 proto_tree *tree, int src_port, int dst_port)
388 const u_char *next_pd;
392 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
394 /* determine if this packet is part of a conversation and call dissector */
395 /* for the conversation if available */
397 if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_TCP,
398 src_port, dst_port, next_tvb, pinfo, tree))
401 /* try to apply the plugins */
404 plugin *pt_plug = plugin_list;
406 if (enabled_plugins_number > 0) {
407 tvb_compat(next_tvb, &next_pd, &next_offset);
409 if (pt_plug->enabled && strstr(pt_plug->protocol, "tcp") &&
410 tree && dfilter_apply(pt_plug->filter, tree, next_pd, pinfo->fd->cap_len)) {
411 pt_plug->dissector(next_pd, next_offset, pinfo->fd, tree);
414 pt_plug = pt_plug->next;
420 /* do lookup with the subdissector table */
421 if (dissector_try_port(subdissector_table, src_port, next_tvb, pinfo, tree) ||
422 dissector_try_port(subdissector_table, dst_port, next_tvb, pinfo, tree))
425 /* do lookup with the heuristic subdissector table */
426 if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
429 /* Oh, well, we don't know this; dissect it as data. */
430 dissect_data(next_tvb, 0, pinfo, tree);
435 dissect_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
438 proto_tree *tcp_tree = NULL, *field_tree = NULL;
441 gchar flags[64] = "<None>";
442 gchar *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG", "ECN", "CWR" };
453 guint16 computed_cksum;
454 guint length_remaining;
456 CHECK_DISPLAY_AS_DATA(proto_tcp, tvb, pinfo, tree);
458 pinfo->current_proto = "TCP";
460 if (check_col(pinfo->fd, COL_PROTOCOL))
461 col_set_str(pinfo->fd, COL_PROTOCOL, "TCP");
463 /* Clear out the Info column. */
464 if (check_col(pinfo->fd, COL_INFO))
465 col_clear(pinfo->fd, COL_INFO);
467 /* Avoids alignment problems on many architectures. */
468 tvb_memcpy(tvb, (guint8 *)&th, offset, sizeof(e_tcphdr));
469 th.th_sport = ntohs(th.th_sport);
470 th.th_dport = ntohs(th.th_dport);
471 th.th_win = ntohs(th.th_win);
472 th.th_sum = ntohs(th.th_sum);
473 th.th_urp = ntohs(th.th_urp);
474 th.th_seq = ntohl(th.th_seq);
475 th.th_ack = ntohl(th.th_ack);
477 /* Export the urgent pointer, for the benefit of protocols such as
479 tcp_urgent_pointer = th.th_urp;
481 if (check_col(pinfo->fd, COL_INFO) || tree) {
482 for (i = 0; i < 8; i++) {
484 if (th.th_flags & bpos) {
486 strcpy(&flags[fpos], ", ");
489 strcpy(&flags[fpos], fstr[i]);
496 hlen = hi_nibble(th.th_off_x2) * 4; /* TCP header length, in bytes */
498 reported_len = tvb_reported_length(tvb);
499 len = tvb_length(tvb);
501 /* Compute the length of data in this segment. */
502 seglen = reported_len - hlen;
504 /* Compute the sequence number of next octet after this segment. */
505 nxtseq = th.th_seq + seglen;
507 if (check_col(pinfo->fd, COL_INFO)) {
508 if (th.th_flags & TH_URG)
509 col_append_fstr(pinfo->fd, COL_INFO, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u Len=%d",
510 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
511 th.th_seq, th.th_ack, th.th_win, th.th_urp, seglen);
513 col_append_fstr(pinfo->fd, COL_INFO, "%s > %s [%s] Seq=%u Ack=%u Win=%u Len=%d",
514 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
515 th.th_seq, th.th_ack, th.th_win, seglen);
519 if (tcp_summary_in_tree) {
520 ti = proto_tree_add_protocol_format(tree, proto_tcp, tvb, offset, hlen, "Transmission Control Protocol, Src Port: %s (%u), Dst Port: %s (%u), Seq: %u, Ack: %u", get_tcp_port(th.th_sport), th.th_sport, get_tcp_port(th.th_dport), th.th_dport, th.th_seq, th.th_ack);
523 ti = proto_tree_add_item(tree, proto_tcp, tvb, offset, hlen, FALSE);
525 tcp_tree = proto_item_add_subtree(ti, ett_tcp);
526 proto_tree_add_uint_format(tcp_tree, hf_tcp_srcport, tvb, offset, 2, th.th_sport,
527 "Source port: %s (%u)", get_tcp_port(th.th_sport), th.th_sport);
528 proto_tree_add_uint_format(tcp_tree, hf_tcp_dstport, tvb, offset + 2, 2, th.th_dport,
529 "Destination port: %s (%u)", get_tcp_port(th.th_dport), th.th_dport);
530 proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset, 2, th.th_sport);
531 proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset + 2, 2, th.th_dport);
532 proto_tree_add_uint(tcp_tree, hf_tcp_seq, tvb, offset + 4, 4, th.th_seq);
533 if (nxtseq != th.th_seq)
534 proto_tree_add_uint(tcp_tree, hf_tcp_nxtseq, tvb, offset, 0, nxtseq);
535 if (th.th_flags & TH_ACK)
536 proto_tree_add_uint(tcp_tree, hf_tcp_ack, tvb, offset + 8, 4, th.th_ack);
537 proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, tvb, offset + 12, 1, hlen,
538 "Header length: %u bytes", hlen);
539 tf = proto_tree_add_uint_format(tcp_tree, hf_tcp_flags, tvb, offset + 13, 1,
540 th.th_flags, "Flags: 0x%04x (%s)", th.th_flags, flags);
541 field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
542 proto_tree_add_boolean(field_tree, hf_tcp_flags_cwr, tvb, offset + 13, 1, th.th_flags);
543 proto_tree_add_boolean(field_tree, hf_tcp_flags_ecn, tvb, offset + 13, 1, th.th_flags);
544 proto_tree_add_boolean(field_tree, hf_tcp_flags_urg, tvb, offset + 13, 1, th.th_flags);
545 proto_tree_add_boolean(field_tree, hf_tcp_flags_ack, tvb, offset + 13, 1, th.th_flags);
546 proto_tree_add_boolean(field_tree, hf_tcp_flags_push, tvb, offset + 13, 1, th.th_flags);
547 proto_tree_add_boolean(field_tree, hf_tcp_flags_reset, tvb, offset + 13, 1, th.th_flags);
548 proto_tree_add_boolean(field_tree, hf_tcp_flags_syn, tvb, offset + 13, 1, th.th_flags);
549 proto_tree_add_boolean(field_tree, hf_tcp_flags_fin, tvb, offset + 13, 1, th.th_flags);
550 proto_tree_add_uint(tcp_tree, hf_tcp_window_size, tvb, offset + 14, 2, th.th_win);
551 if (!pinfo->fragmented && len >= reported_len) {
552 /* The packet isn't part of a fragmented datagram and isn't
553 truncated, so we can checksum it.
554 XXX - make a bigger scatter-gather list once we do fragment
557 /* Set up the fields of the pseudo-header. */
558 cksum_vec[0].ptr = pinfo->src.data;
559 cksum_vec[0].len = pinfo->src.len;
560 cksum_vec[1].ptr = pinfo->dst.data;
561 cksum_vec[1].len = pinfo->dst.len;
562 cksum_vec[2].ptr = (const guint8 *)&phdr;
563 switch (pinfo->src.type) {
566 phdr[0] = htonl((IP_PROTO_TCP<<16) + reported_len);
567 cksum_vec[2].len = 4;
571 phdr[0] = htonl(reported_len);
572 phdr[1] = htonl(IP_PROTO_TCP);
573 cksum_vec[2].len = 8;
577 /* TCP runs only atop IPv4 and IPv6.... */
578 g_assert_not_reached();
581 cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
582 cksum_vec[3].len = reported_len;
583 computed_cksum = in_cksum(&cksum_vec[0], 4);
584 if (computed_cksum == 0) {
585 proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
586 offset + 16, 2, th.th_sum, "Checksum: 0x%04x (correct)", th.th_sum);
588 proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
589 offset + 16, 2, th.th_sum,
590 "Checksum: 0x%04x (incorrect, should be 0x%04x)", th.th_sum,
591 in_cksum_shouldbe(th.th_sum, computed_cksum));
594 proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
595 offset + 16, 2, th.th_sum, "Checksum: 0x%04x", th.th_sum);
597 if (th.th_flags & TH_URG)
598 proto_tree_add_uint(tcp_tree, hf_tcp_urgent_pointer, tvb, offset + 18, 2, th.th_urp);
601 /* Decode TCP options, if any. */
602 if (tree && hlen > sizeof (e_tcphdr)) {
603 /* There's more than just the fixed-length header. Decode the
605 optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
606 tf = proto_tree_add_text(tcp_tree, tvb, offset + 20, optlen,
607 "Options: (%d bytes)", optlen);
608 field_tree = proto_item_add_subtree(tf, ett_tcp_options);
609 dissect_ip_tcp_options(tvb, offset + 20, optlen,
610 tcpopts, N_TCP_OPTS, TCPOPT_EOL, pinfo->fd, field_tree);
613 /* Skip over header + options */
616 pinfo->ptype = PT_TCP;
617 pinfo->srcport = th.th_sport;
618 pinfo->destport = th.th_dport;
620 /* Check the packet length to see if there's more data
621 (it could be an ACK-only packet) */
622 length_remaining = tvb_length_remaining(tvb, offset);
623 if (length_remaining != 0) {
624 if (th.th_flags & TH_RST) {
628 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
630 * A TCP SHOULD allow a received RST segment to include data.
633 * It has been suggested that a RST segment could contain
634 * ASCII text that encoded and explained the cause of the
635 * RST. No standard has yet been established for such
638 * so for segments with RST we just display the data as text.
640 proto_tree_add_text(tcp_tree, tvb, offset, length_remaining,
642 tvb_format_text(tvb, offset, length_remaining));
644 decode_tcp_ports( tvb, offset, pinfo, tree, th.th_sport, th.th_dport);
647 if( data_out_file ) {
648 reassemble_tcp( th.th_seq, /* sequence number */
649 seglen, /* data length */
650 tvb_get_ptr(tvb, offset, length_remaining), /* data */
651 length_remaining, /* captured data length */
652 ( th.th_flags & TH_SYN ), /* is syn set? */
661 proto_register_tcp(void)
663 static hf_register_info hf[] = {
666 { "Source Port", "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
670 { "Destination Port", "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
674 { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
678 { "Sequence number", "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
682 { "Next sequence number", "tcp.nxtseq", FT_UINT32, BASE_DEC, NULL, 0x0,
686 { "Acknowledgement number", "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
690 { "Header Length", "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
694 { "Flags", "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
698 { "Congestion Window Reduced (CWR)", "tcp.flags.cwr", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_CWR,
702 { "ECN-Echo", "tcp.flags.ecn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ECN,
706 { "Urgent", "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
710 { "Acknowledgment", "tcp.flags.ack", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ACK,
713 { &hf_tcp_flags_push,
714 { "Push", "tcp.flags.push", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_PUSH,
717 { &hf_tcp_flags_reset,
718 { "Reset", "tcp.flags.reset", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_RST,
722 { "Syn", "tcp.flags.syn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_SYN,
726 { "Fin", "tcp.flags.fin", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_FIN,
729 { &hf_tcp_window_size,
730 { "Window size", "tcp.window_size", FT_UINT16, BASE_DEC, NULL, 0x0,
734 { "Checksum", "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
737 { &hf_tcp_urgent_pointer,
738 { "Urgent pointer", "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
741 static gint *ett[] = {
745 &ett_tcp_option_sack,
747 module_t *tcp_module;
749 proto_tcp = proto_register_protocol("Transmission Control Protocol",
751 proto_register_field_array(proto_tcp, hf, array_length(hf));
752 proto_register_subtree_array(ett, array_length(ett));
754 /* subdissector code */
755 subdissector_table = register_dissector_table("tcp.port");
756 register_heur_dissector_list("tcp", &heur_subdissector_list);
757 register_conv_dissector_list("tcp", &conv_subdissector_list);
759 /* Register configuration preferences */
760 tcp_module = prefs_register_protocol(proto_tcp, NULL);
761 prefs_register_bool_preference(tcp_module, "tcp_summary_in_tree",
762 "Show TCP summary in protocol tree",
763 "Whether the TCP summary line should be shown in the protocol tree",
764 &tcp_summary_in_tree);
768 proto_reg_handoff_tcp(void)
770 dissector_add("ip.proto", IP_PROTO_TCP, dissect_tcp, proto_tcp);