2 * Routines for TCP packet disassembly
4 * $Id: packet-tcp.c,v 1.88 2000/11/18 10:38:25 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>
42 #ifdef NEED_SNPRINTF_H
43 # include "snprintf.h"
51 #include "packet-tcp.h"
52 #include "packet-ip.h"
53 #include "conversation.h"
56 /* Place TCP summary in proto tree */
57 gboolean g_tcp_summary_in_tree = TRUE;
59 extern FILE* data_out_file;
61 guint16 tcp_urgent_pointer;
63 static gchar info_str[COL_MAX_LEN];
66 static int proto_tcp = -1;
67 static int hf_tcp_srcport = -1;
68 static int hf_tcp_dstport = -1;
69 static int hf_tcp_port = -1;
70 static int hf_tcp_seq = -1;
71 static int hf_tcp_nxtseq = -1;
72 static int hf_tcp_ack = -1;
73 static int hf_tcp_hdr_len = -1;
74 static int hf_tcp_flags = -1;
75 static int hf_tcp_flags_cwr = -1;
76 static int hf_tcp_flags_ecn = -1;
77 static int hf_tcp_flags_urg = -1;
78 static int hf_tcp_flags_ack = -1;
79 static int hf_tcp_flags_push = -1;
80 static int hf_tcp_flags_reset = -1;
81 static int hf_tcp_flags_syn = -1;
82 static int hf_tcp_flags_fin = -1;
83 static int hf_tcp_window_size = -1;
84 static int hf_tcp_checksum = -1;
85 static int hf_tcp_urgent_pointer = -1;
87 static gint ett_tcp = -1;
88 static gint ett_tcp_flags = -1;
89 static gint ett_tcp_options = -1;
90 static gint ett_tcp_option_sack = -1;
92 static dissector_table_t subdissector_table;
93 static heur_dissector_list_t heur_subdissector_list;
97 #define TCP_PORT_SMTP 25
99 /* TCP structs and definitions */
101 typedef struct _e_tcphdr {
106 guint8 th_off_x2; /* combines th_off and th_x2 */
125 #define TCPOPT_NOP 1 /* Padding */
126 #define TCPOPT_EOL 0 /* End of options */
127 #define TCPOPT_MSS 2 /* Segment size negotiating */
128 #define TCPOPT_WINDOW 3 /* Window scaling */
129 #define TCPOPT_SACK_PERM 4 /* SACK Permitted */
130 #define TCPOPT_SACK 5 /* SACK Block */
131 #define TCPOPT_ECHO 6
132 #define TCPOPT_ECHOREPLY 7
133 #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
135 #define TCPOPT_CCNEW 12
136 #define TCPOPT_CCECHO 13
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
154 tcp_info_append_uint(const char *abbrev, guint32 val) {
159 add_len = snprintf(&info_str[info_len], COL_MAX_LEN - info_len, " %s=%u",
166 dissect_tcpopt_maxseg(const ip_tcp_opt *optp, tvbuff_t *tvb,
167 int offset, guint optlen, proto_tree *opt_tree)
171 mss = tvb_get_ntohs(tvb, offset + 2);
172 proto_tree_add_text(opt_tree, tvb, offset, optlen,
173 "%s: %u bytes", optp->name, mss);
174 tcp_info_append_uint("MSS", mss);
178 dissect_tcpopt_wscale(const ip_tcp_opt *optp, tvbuff_t *tvb,
179 int offset, guint optlen, proto_tree *opt_tree)
183 ws = tvb_get_guint8(tvb, offset + 2);
184 proto_tree_add_text(opt_tree, tvb, offset, optlen,
185 "%s: %u bytes", optp->name, ws);
186 tcp_info_append_uint("WS", ws);
190 dissect_tcpopt_sack(const ip_tcp_opt *optp, tvbuff_t *tvb,
191 int offset, guint optlen, proto_tree *opt_tree)
193 proto_tree *field_tree = NULL;
195 guint leftedge, rightedge;
197 tf = proto_tree_add_text(opt_tree, tvb, offset, optlen, "%s:", optp->name);
198 offset += 2; /* skip past type and length */
199 optlen -= 2; /* subtract size of type and length */
201 if (field_tree == NULL) {
202 /* Haven't yet made a subtree out of this option. Do so. */
203 field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
206 proto_tree_add_text(field_tree, tvb, offset, optlen,
207 "(suboption would go past end of option)");
210 leftedge = tvb_get_ntohl(tvb, offset);
213 proto_tree_add_text(field_tree, tvb, offset, optlen,
214 "(suboption would go past end of option)");
217 /* XXX - check whether it goes past end of packet */
218 rightedge = tvb_get_ntohl(tvb, offset + 4);
220 proto_tree_add_text(field_tree, tvb, offset, 8,
221 "left edge = %u, right edge = %u", leftedge, rightedge);
222 tcp_info_append_uint("SLE", leftedge);
223 tcp_info_append_uint("SRE", rightedge);
229 dissect_tcpopt_echo(const ip_tcp_opt *optp, tvbuff_t *tvb,
230 int offset, guint optlen, proto_tree *opt_tree)
234 echo = tvb_get_ntohl(tvb, offset + 2);
235 proto_tree_add_text(opt_tree, tvb, offset, optlen,
236 "%s: %u", optp->name, echo);
237 tcp_info_append_uint("ECHO", echo);
241 dissect_tcpopt_timestamp(const ip_tcp_opt *optp, tvbuff_t *tvb,
242 int offset, guint optlen, proto_tree *opt_tree)
246 tsv = tvb_get_ntohl(tvb, offset + 2);
247 tser = tvb_get_ntohl(tvb, offset + 6);
248 proto_tree_add_text(opt_tree, tvb, offset, optlen,
249 "%s: tsval %u, tsecr %u", optp->name, tsv, tser);
250 tcp_info_append_uint("TSV", tsv);
251 tcp_info_append_uint("TSER", tser);
255 dissect_tcpopt_cc(const ip_tcp_opt *optp, tvbuff_t *tvb,
256 int offset, guint optlen, proto_tree *opt_tree)
260 cc = tvb_get_ntohl(tvb, offset + 2);
261 proto_tree_add_text(opt_tree, tvb, offset, optlen,
262 "%s: %u", optp->name, cc);
263 tcp_info_append_uint("CC", cc);
266 static const ip_tcp_opt tcpopts[] = {
285 "Maximum segment size",
289 dissect_tcpopt_maxseg
297 dissect_tcpopt_wscale
310 &ett_tcp_option_sack,
337 dissect_tcpopt_timestamp
365 #define N_TCP_OPTS (sizeof tcpopts / sizeof tcpopts[0])
368 static const true_false_string flags_set_truth = {
374 /* Determine if there is a sub-dissector and call it. This has been */
375 /* separated into a stand alone routine to other protocol dissectors */
376 /* can call to it, ie. socks */
379 decode_tcp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
380 proto_tree *tree, int src_port, int dst_port)
383 const u_char *next_pd;
386 next_tvb = tvb_new_subset(tvb, offset, -1, -1);
388 /* determine if this packet is part of a conversation and call dissector */
389 /* for the conversation if available */
391 if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_TCP,
392 src_port, dst_port, next_tvb, pinfo, tree))
395 /* try to apply the plugins */
398 plugin *pt_plug = plugin_list;
400 if (enabled_plugins_number > 0) {
401 tvb_compat(next_tvb, &next_pd, &next_offset);
403 if (pt_plug->enabled && strstr(pt_plug->protocol, "tcp") &&
404 tree && dfilter_apply(pt_plug->filter, tree, next_pd, pinfo->fd->cap_len)) {
405 pt_plug->dissector(next_pd, next_offset, pinfo->fd, tree);
408 pt_plug = pt_plug->next;
414 /* do lookup with the subdissector table */
415 if (dissector_try_port(subdissector_table, src_port, next_tvb, pinfo, tree) ||
416 dissector_try_port(subdissector_table, dst_port, next_tvb, pinfo, tree))
419 /* do lookup with the heuristic subdissector table */
420 if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
423 /* Oh, well, we don't know this; dissect it as data. */
424 dissect_data(next_tvb, 0, pinfo, tree);
429 dissect_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
432 proto_tree *tcp_tree = NULL, *field_tree = NULL;
435 gchar flags[64] = "<None>";
436 gchar *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG", "ECN", "CWR" };
443 guint length_remaining;
445 CHECK_DISPLAY_AS_DATA(proto_tcp, tvb, pinfo, tree);
447 pinfo->current_proto = "TCP";
449 /* Avoids alignment problems on many architectures. */
450 tvb_memcpy(tvb, (guint8 *)&th, offset, sizeof(e_tcphdr));
451 th.th_sport = ntohs(th.th_sport);
452 th.th_dport = ntohs(th.th_dport);
453 th.th_win = ntohs(th.th_win);
454 th.th_sum = ntohs(th.th_sum);
455 th.th_urp = ntohs(th.th_urp);
456 th.th_seq = ntohl(th.th_seq);
457 th.th_ack = ntohl(th.th_ack);
459 /* Export the urgent pointer, for the benefit of protocols such as
461 tcp_urgent_pointer = th.th_urp;
465 if (check_col(pinfo->fd, COL_INFO) || tree) {
466 for (i = 0; i < 8; i++) {
468 if (th.th_flags & bpos) {
470 strcpy(&flags[fpos], ", ");
473 strcpy(&flags[fpos], fstr[i]);
480 hlen = hi_nibble(th.th_off_x2) * 4; /* TCP header length, in bytes */
482 /* Compute the length of data in this segment. */
483 seglen = tvb_reported_length(tvb) - hlen;
485 /* Compute the sequence number of next octet after this segment. */
486 nxtseq = th.th_seq + seglen;
488 if (check_col(pinfo->fd, COL_PROTOCOL))
489 col_add_str(pinfo->fd, COL_PROTOCOL, "TCP");
490 if (check_col(pinfo->fd, COL_INFO)) {
491 /* Copy the data into info_str in case one of the option handling
492 routines needs to append to it. */
493 if (th.th_flags & TH_URG)
494 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u Len=%d",
495 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
496 th.th_seq, th.th_ack, th.th_win, th.th_urp, seglen);
498 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Len=%d",
499 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
500 th.th_seq, th.th_ack, th.th_win, seglen);
501 /* The info column is actually written after the options are decoded */
505 if (g_tcp_summary_in_tree) {
506 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);
509 ti = proto_tree_add_item(tree, proto_tcp, tvb, offset, hlen, FALSE);
511 tcp_tree = proto_item_add_subtree(ti, ett_tcp);
512 proto_tree_add_uint_format(tcp_tree, hf_tcp_srcport, tvb, offset, 2, th.th_sport,
513 "Source port: %s (%u)", get_tcp_port(th.th_sport), th.th_sport);
514 proto_tree_add_uint_format(tcp_tree, hf_tcp_dstport, tvb, offset + 2, 2, th.th_dport,
515 "Destination port: %s (%u)", get_tcp_port(th.th_dport), th.th_dport);
516 proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset, 2, th.th_sport);
517 proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset + 2, 2, th.th_dport);
518 proto_tree_add_uint(tcp_tree, hf_tcp_seq, tvb, offset + 4, 4, th.th_seq);
519 if (nxtseq != th.th_seq)
520 proto_tree_add_uint(tcp_tree, hf_tcp_nxtseq, tvb, offset, 0, nxtseq);
521 if (th.th_flags & TH_ACK)
522 proto_tree_add_uint(tcp_tree, hf_tcp_ack, tvb, offset + 8, 4, th.th_ack);
523 proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, tvb, offset + 12, 1, hlen,
524 "Header length: %u bytes", hlen);
525 tf = proto_tree_add_uint_format(tcp_tree, hf_tcp_flags, tvb, offset + 13, 1,
526 th.th_flags, "Flags: 0x%04x (%s)", th.th_flags, flags);
527 field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
528 proto_tree_add_boolean(field_tree, hf_tcp_flags_cwr, tvb, offset + 13, 1, th.th_flags);
529 proto_tree_add_boolean(field_tree, hf_tcp_flags_ecn, tvb, offset + 13, 1, th.th_flags);
530 proto_tree_add_boolean(field_tree, hf_tcp_flags_urg, tvb, offset + 13, 1, th.th_flags);
531 proto_tree_add_boolean(field_tree, hf_tcp_flags_ack, tvb, offset + 13, 1, th.th_flags);
532 proto_tree_add_boolean(field_tree, hf_tcp_flags_push, tvb, offset + 13, 1, th.th_flags);
533 proto_tree_add_boolean(field_tree, hf_tcp_flags_reset, tvb, offset + 13, 1, th.th_flags);
534 proto_tree_add_boolean(field_tree, hf_tcp_flags_syn, tvb, offset + 13, 1, th.th_flags);
535 proto_tree_add_boolean(field_tree, hf_tcp_flags_fin, tvb, offset + 13, 1, th.th_flags);
536 proto_tree_add_uint(tcp_tree, hf_tcp_window_size, tvb, offset + 14, 2, th.th_win);
537 proto_tree_add_uint(tcp_tree, hf_tcp_checksum, tvb, offset + 16, 2, th.th_sum);
538 if (th.th_flags & TH_URG)
539 proto_tree_add_uint(tcp_tree, hf_tcp_urgent_pointer, tvb, offset + 18, 2, th.th_urp);
542 /* Decode TCP options, if any. */
543 if (tree && hlen > sizeof (e_tcphdr)) {
544 /* There's more than just the fixed-length header. Decode the
546 optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
547 tf = proto_tree_add_text(tcp_tree, tvb, offset + 20, optlen,
548 "Options: (%d bytes)", optlen);
549 field_tree = proto_item_add_subtree(tf, ett_tcp_options);
550 dissect_ip_tcp_options(tvb, offset + 20, optlen,
551 tcpopts, N_TCP_OPTS, TCPOPT_EOL, field_tree);
554 if (check_col(pinfo->fd, COL_INFO))
555 col_add_str(pinfo->fd, COL_INFO, info_str);
557 /* Skip over header + options */
560 pinfo->ptype = PT_TCP;
561 pinfo->srcport = th.th_sport;
562 pinfo->destport = th.th_dport;
564 /* Check the packet length to see if there's more data
565 (it could be an ACK-only packet) */
566 length_remaining = tvb_length_remaining(tvb, offset);
567 if (length_remaining != 0) {
568 if (th.th_flags & TH_RST) {
572 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
574 * A TCP SHOULD allow a received RST segment to include data.
577 * It has been suggested that a RST segment could contain
578 * ASCII text that encoded and explained the cause of the
579 * RST. No standard has yet been established for such
582 * so for segments with RST we just display the data as text.
584 proto_tree_add_text(tcp_tree, tvb, offset, length_remaining,
586 tvb_format_text(tvb, offset, length_remaining));
588 decode_tcp_ports( tvb, offset, pinfo, tree, th.th_sport, th.th_dport);
591 if( data_out_file ) {
592 reassemble_tcp( th.th_seq, /* sequence number */
593 seglen, /* data length */
594 tvb_get_ptr(tvb, offset, length_remaining), /* data */
595 length_remaining, /* captured data length */
596 ( th.th_flags & TH_SYN ), /* is syn set? */
605 proto_register_tcp(void)
607 static hf_register_info hf[] = {
610 { "Source Port", "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
614 { "Destination Port", "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
618 { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
622 { "Sequence number", "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
626 { "Next sequence number", "tcp.nxtseq", FT_UINT32, BASE_DEC, NULL, 0x0,
630 { "Acknowledgement number", "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
634 { "Header Length", "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
638 { "Flags", "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
642 { "Congestion Window Reduced (CWR)", "tcp.flags.cwr", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_CWR,
646 { "ECN-Echo", "tcp.flags.ecn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ECN,
650 { "Urgent", "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
654 { "Acknowledgment", "tcp.flags.ack", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ACK,
657 { &hf_tcp_flags_push,
658 { "Push", "tcp.flags.push", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_PUSH,
661 { &hf_tcp_flags_reset,
662 { "Reset", "tcp.flags.reset", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_RST,
666 { "Syn", "tcp.flags.syn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_SYN,
670 { "Fin", "tcp.flags.fin", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_FIN,
673 { &hf_tcp_window_size,
674 { "Window size", "tcp.window_size", FT_UINT16, BASE_DEC, NULL, 0x0,
678 { "Checksum", "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
681 { &hf_tcp_urgent_pointer,
682 { "Urgent pointer", "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
685 static gint *ett[] = {
689 &ett_tcp_option_sack,
691 module_t *tcp_module;
693 proto_tcp = proto_register_protocol ("Transmission Control Protocol", "tcp");
694 proto_register_field_array(proto_tcp, hf, array_length(hf));
695 proto_register_subtree_array(ett, array_length(ett));
697 /* subdissector code */
698 subdissector_table = register_dissector_table("tcp.port");
699 register_heur_dissector_list("tcp", &heur_subdissector_list);
701 /* Register a configuration preferences */
702 tcp_module = prefs_register_module("tcp", "TCP", NULL);
703 prefs_register_bool_preference(tcp_module, "tcp_summary_in_tree",
704 "Show TCP summary in protocol tree",
705 "Whether the TCP summary line should be shown in the protocol tree",
706 &g_tcp_summary_in_tree);
710 proto_reg_handoff_tcp(void)
712 dissector_add("ip.proto", IP_PROTO_TCP, dissect_tcp);