2 * Routines for TCP packet disassembly
4 * $Id: packet-tcp.c,v 1.83 2000/09/14 21:58:48 gram 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>
41 #ifdef NEED_SNPRINTF_H
42 # include "snprintf.h"
50 #include "packet-tcp.h"
51 #include "packet-ip.h"
52 #include "conversation.h"
55 /* Place TCP summary in proto tree */
56 gboolean g_tcp_summary_in_tree = TRUE;
58 extern FILE* data_out_file;
60 guint16 tcp_urgent_pointer;
62 static gchar info_str[COL_MAX_LEN];
65 static int proto_tcp = -1;
66 static int hf_tcp_srcport = -1;
67 static int hf_tcp_dstport = -1;
68 static int hf_tcp_port = -1;
69 static int hf_tcp_seq = -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;
95 #define TCP_PORT_SMTP 25
97 /* TCP structs and definitions */
99 typedef struct _e_tcphdr {
104 guint8 th_off_x2; /* combines th_off and th_x2 */
123 #define TCPOPT_NOP 1 /* Padding */
124 #define TCPOPT_EOL 0 /* End of options */
125 #define TCPOPT_MSS 2 /* Segment size negotiating */
126 #define TCPOPT_WINDOW 3 /* Window scaling */
127 #define TCPOPT_SACK_PERM 4 /* SACK Permitted */
128 #define TCPOPT_SACK 5 /* SACK Block */
129 #define TCPOPT_ECHO 6
130 #define TCPOPT_ECHOREPLY 7
131 #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
133 #define TCPOPT_CCNEW 12
134 #define TCPOPT_CCECHO 13
140 #define TCPOLEN_MSS 4
141 #define TCPOLEN_WINDOW 3
142 #define TCPOLEN_SACK_PERM 2
143 #define TCPOLEN_SACK_MIN 2
144 #define TCPOLEN_ECHO 6
145 #define TCPOLEN_ECHOREPLY 6
146 #define TCPOLEN_TIMESTAMP 10
148 #define TCPOLEN_CCNEW 6
149 #define TCPOLEN_CCECHO 6
152 tcp_info_append_uint(const char *abbrev, guint32 val) {
157 add_len = snprintf(&info_str[info_len], COL_MAX_LEN - info_len, " %s=%u",
164 dissect_tcpopt_maxseg(const ip_tcp_opt *optp, const u_char *opd,
165 int offset, guint optlen, proto_tree *opt_tree)
167 proto_tree_add_text(opt_tree, NullTVB, offset, optlen,
168 "%s: %u bytes", optp->name, pntohs(opd));
169 tcp_info_append_uint("MSS", pntohs(opd));
173 dissect_tcpopt_wscale(const ip_tcp_opt *optp, const u_char *opd,
174 int offset, guint optlen, proto_tree *opt_tree)
176 proto_tree_add_text(opt_tree, NullTVB, offset, optlen,
177 "%s: %u bytes", optp->name, *opd);
178 tcp_info_append_uint("WS", *opd);
182 dissect_tcpopt_sack(const ip_tcp_opt *optp, const u_char *opd,
183 int offset, guint optlen, proto_tree *opt_tree)
185 proto_tree *field_tree = NULL;
187 guint leftedge, rightedge;
189 tf = proto_tree_add_text(opt_tree, NullTVB, offset, optlen, "%s:", optp->name);
190 offset += 2; /* skip past type and length */
191 optlen -= 2; /* subtract size of type and length */
193 if (field_tree == NULL) {
194 /* Haven't yet made a subtree out of this option. Do so. */
195 field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
198 proto_tree_add_text(field_tree, NullTVB, offset, optlen,
199 "(suboption would go past end of option)");
202 /* XXX - check whether it goes past end of packet */
203 leftedge = pntohl(opd);
207 proto_tree_add_text(field_tree, NullTVB, offset, optlen,
208 "(suboption would go past end of option)");
211 /* XXX - check whether it goes past end of packet */
212 rightedge = pntohl(opd);
215 proto_tree_add_text(field_tree, NullTVB, offset, 8,
216 "left edge = %u, right edge = %u", leftedge, rightedge);
217 tcp_info_append_uint("SLE", leftedge);
218 tcp_info_append_uint("SRE", rightedge);
224 dissect_tcpopt_echo(const ip_tcp_opt *optp, const u_char *opd,
225 int offset, guint optlen, proto_tree *opt_tree)
227 proto_tree_add_text(opt_tree, NullTVB, offset, optlen,
228 "%s: %u", optp->name, pntohl(opd));
229 tcp_info_append_uint("ECHO", pntohl(opd));
233 dissect_tcpopt_timestamp(const ip_tcp_opt *optp, const u_char *opd,
234 int offset, guint optlen, proto_tree *opt_tree)
236 proto_tree_add_text(opt_tree, NullTVB, offset, optlen,
237 "%s: tsval %u, tsecr %u", optp->name, pntohl(opd), pntohl(opd + 4));
238 tcp_info_append_uint("TSV", pntohl(opd));
239 tcp_info_append_uint("TSER", pntohl(opd + 4));
243 dissect_tcpopt_cc(const ip_tcp_opt *optp, const u_char *opd,
244 int offset, guint optlen, proto_tree *opt_tree)
246 proto_tree_add_text(opt_tree, NullTVB, offset, optlen,
247 "%s: %u", optp->name, pntohl(opd));
248 tcp_info_append_uint("CC", pntohl(opd));
251 static const ip_tcp_opt tcpopts[] = {
270 "Maximum segment size",
274 dissect_tcpopt_maxseg
282 dissect_tcpopt_wscale
295 &ett_tcp_option_sack,
322 dissect_tcpopt_timestamp
350 #define N_TCP_OPTS (sizeof tcpopts / sizeof tcpopts[0])
353 static const true_false_string flags_set_truth = {
359 /* Determine if there is a sub-dissector and call it. This has been */
360 /* separated into a stand alone routine to other protocol dissectors */
361 /* can call to it, ie. socks */
364 decode_tcp_ports(const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
365 int src_port, int dst_port)
367 /* determine if this packet is part of a conversation and call dissector */
368 /* for the conversation if available */
370 if (old_try_conversation_dissector(&pi.src, &pi.dst, PT_TCP,
371 src_port, dst_port, pd, offset, fd, tree))
374 /* try to apply the plugins */
377 plugin *pt_plug = plugin_list;
379 if (enabled_plugins_number > 0) {
381 if (pt_plug->enabled && !strcmp(pt_plug->protocol, "tcp") &&
382 tree && dfilter_apply(pt_plug->filter, tree, pd, fd->cap_len)) {
383 pt_plug->dissector(pd, offset, fd, tree);
386 pt_plug = pt_plug->next;
392 /* do lookup with the subdissector table */
393 if (old_dissector_try_port(subdissector_table, src_port, pd, offset, fd, tree) ||
394 old_dissector_try_port(subdissector_table, dst_port, pd, offset, fd, tree))
397 /* do lookup with the heuristic subdissector table */
398 if (old_dissector_try_heuristic(heur_subdissector_list, pd, offset, fd, tree))
401 /* Oh, well, we don't know this; dissect it as data. */
402 old_dissect_data(pd, offset, fd, tree);
407 dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
409 proto_tree *tcp_tree = NULL, *field_tree = NULL;
411 gchar flags[64] = "<None>";
412 gchar *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG", "ECN", "CWR" };
417 guint packet_max = pi.len;
419 OLD_CHECK_DISPLAY_AS_DATA(proto_tcp, pd, offset, fd, tree);
421 /* To do: Check for {cap len,pkt len} < struct len */
422 /* Avoids alignment problems on many architectures. */
423 memcpy(&th, &pd[offset], sizeof(e_tcphdr));
424 th.th_sport = ntohs(th.th_sport);
425 th.th_dport = ntohs(th.th_dport);
426 th.th_win = ntohs(th.th_win);
427 th.th_sum = ntohs(th.th_sum);
428 th.th_urp = ntohs(th.th_urp);
429 th.th_seq = ntohl(th.th_seq);
430 th.th_ack = ntohl(th.th_ack);
432 /* Export the urgent pointer, for the benefit of protocols such as
434 tcp_urgent_pointer = th.th_urp;
438 if (check_col(fd, COL_PROTOCOL) || tree) {
439 for (i = 0; i < 8; i++) {
441 if (th.th_flags & bpos) {
443 strcpy(&flags[fpos], ", ");
446 strcpy(&flags[fpos], fstr[i]);
453 hlen = hi_nibble(th.th_off_x2) * 4; /* TCP header length, in bytes */
455 if (check_col(fd, COL_PROTOCOL))
456 col_add_str(fd, COL_PROTOCOL, "TCP");
457 if (check_col(fd, COL_INFO)) {
458 /* Copy the data into info_str in case one of the option handling
459 routines needs to append to it. */
460 if (th.th_flags & TH_URG)
461 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u Len=%d",
462 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
463 th.th_seq, th.th_ack, th.th_win, th.th_urp, pi.len - offset - hlen);
465 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Len=%d",
466 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
467 th.th_seq, th.th_ack, th.th_win, pi.len - offset - hlen);
468 /* The info column is actually written after the options are decoded */
472 if (g_tcp_summary_in_tree) {
473 ti = proto_tree_add_protocol_format(tree, proto_tcp, NullTVB, 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);
476 ti = proto_tree_add_item(tree, proto_tcp, NullTVB, offset, hlen, FALSE);
478 tcp_tree = proto_item_add_subtree(ti, ett_tcp);
479 proto_tree_add_uint_format(tcp_tree, hf_tcp_srcport, NullTVB, offset, 2, th.th_sport,
480 "Source port: %s (%u)", get_tcp_port(th.th_sport), th.th_sport);
481 proto_tree_add_uint_format(tcp_tree, hf_tcp_dstport, NullTVB, offset + 2, 2, th.th_dport,
482 "Destination port: %s (%u)", get_tcp_port(th.th_dport), th.th_dport);
483 proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, NullTVB, offset, 2, th.th_sport);
484 proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, NullTVB, offset + 2, 2, th.th_dport);
485 proto_tree_add_uint(tcp_tree, hf_tcp_seq, NullTVB, offset + 4, 4, th.th_seq);
486 if (th.th_flags & TH_ACK)
487 proto_tree_add_uint(tcp_tree, hf_tcp_ack, NullTVB, offset + 8, 4, th.th_ack);
488 proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, NullTVB, offset + 12, 1, hlen,
489 "Header length: %u bytes", hlen);
490 tf = proto_tree_add_uint_format(tcp_tree, hf_tcp_flags, NullTVB, offset + 13, 1,
491 th.th_flags, "Flags: 0x%04x (%s)", th.th_flags, flags);
492 field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
493 proto_tree_add_boolean(field_tree, hf_tcp_flags_cwr, NullTVB, offset + 13, 1, th.th_flags);
494 proto_tree_add_boolean(field_tree, hf_tcp_flags_ecn, NullTVB, offset + 13, 1, th.th_flags);
495 proto_tree_add_boolean(field_tree, hf_tcp_flags_urg, NullTVB, offset + 13, 1, th.th_flags);
496 proto_tree_add_boolean(field_tree, hf_tcp_flags_ack, NullTVB, offset + 13, 1, th.th_flags);
497 proto_tree_add_boolean(field_tree, hf_tcp_flags_push, NullTVB, offset + 13, 1, th.th_flags);
498 proto_tree_add_boolean(field_tree, hf_tcp_flags_reset, NullTVB, offset + 13, 1, th.th_flags);
499 proto_tree_add_boolean(field_tree, hf_tcp_flags_syn, NullTVB, offset + 13, 1, th.th_flags);
500 proto_tree_add_boolean(field_tree, hf_tcp_flags_fin, NullTVB, offset + 13, 1, th.th_flags);
501 proto_tree_add_uint(tcp_tree, hf_tcp_window_size, NullTVB, offset + 14, 2, th.th_win);
502 proto_tree_add_uint(tcp_tree, hf_tcp_checksum, NullTVB, offset + 16, 2, th.th_sum);
503 if (th.th_flags & TH_URG)
504 proto_tree_add_uint(tcp_tree, hf_tcp_urgent_pointer, NullTVB, offset + 18, 2, th.th_urp);
507 /* Decode TCP options, if any. */
508 if (tree && hlen > sizeof (e_tcphdr)) {
509 /* There's more than just the fixed-length header. Decode the
511 optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
512 tf = proto_tree_add_text(tcp_tree, NullTVB, offset + 20, optlen,
513 "Options: (%d bytes)", optlen);
514 field_tree = proto_item_add_subtree(tf, ett_tcp_options);
515 dissect_ip_tcp_options(&pd[offset + 20], offset + 20, optlen,
516 tcpopts, N_TCP_OPTS, TCPOPT_EOL, field_tree);
519 if (check_col(fd, COL_INFO))
520 col_add_str(fd, COL_INFO, info_str);
522 /* Skip over header + options */
526 pi.srcport = th.th_sport;
527 pi.destport = th.th_dport;
529 /* Check the packet length to see if there's more data
530 (it could be an ACK-only packet) */
531 if (packet_max > offset) {
532 if (th.th_flags & TH_RST) {
536 * 4.2.2.12 RST Segment: RFC-793 Section 3.4
538 * A TCP SHOULD allow a received RST segment to include data.
541 * It has been suggested that a RST segment could contain
542 * ASCII text that encoded and explained the cause of the
543 * RST. No standard has yet been established for such
546 * so for segments with RST we just display the data as text.
548 proto_tree_add_text(tcp_tree, NullTVB, offset, END_OF_FRAME,
550 format_text(&pd[offset], END_OF_FRAME));
552 decode_tcp_ports( pd, offset, fd, tree, th.th_sport, th.th_dport);
555 if( data_out_file ) {
556 reassemble_tcp( th.th_seq, /* sequence number */
557 ( pi.len - offset ), /* data length */
558 ( pd+offset ), /* data */
559 ( pi.captured_len - offset ), /* captured data length */
560 ( th.th_flags & TH_SYN ), /* is syn set? */
569 proto_register_tcp(void)
571 static hf_register_info hf[] = {
574 { "Source Port", "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
578 { "Destination Port", "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
582 { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
586 { "Sequence number", "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
590 { "Acknowledgement number", "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
594 { "Header Length", "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
598 { "Flags", "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
602 { "Congestion Window Reduced (CWR)", "tcp.flags.cwr", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_CWR,
606 { "ECN-Echo", "tcp.flags.ecn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ECN,
610 { "Urgent", "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
614 { "Acknowledgment", "tcp.flags.ack", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ACK,
617 { &hf_tcp_flags_push,
618 { "Push", "tcp.flags.push", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_PUSH,
621 { &hf_tcp_flags_reset,
622 { "Reset", "tcp.flags.reset", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_RST,
626 { "Syn", "tcp.flags.syn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_SYN,
630 { "Fin", "tcp.flags.fin", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_FIN,
633 { &hf_tcp_window_size,
634 { "Window size", "tcp.window_size", FT_UINT16, BASE_DEC, NULL, 0x0,
638 { "Checksum", "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
641 { &hf_tcp_urgent_pointer,
642 { "Urgent pointer", "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
645 static gint *ett[] = {
649 &ett_tcp_option_sack,
651 module_t *tcp_module;
653 proto_tcp = proto_register_protocol ("Transmission Control Protocol", "tcp");
654 proto_register_field_array(proto_tcp, hf, array_length(hf));
655 proto_register_subtree_array(ett, array_length(ett));
657 /* subdissector code */
658 subdissector_table = register_dissector_table("tcp.port");
659 register_heur_dissector_list("tcp", &heur_subdissector_list);
661 /* Register a configuration preferences */
662 tcp_module = prefs_register_module("tcp", "TCP", NULL);
663 prefs_register_bool_preference(tcp_module, "tcp_summary_in_tree",
664 "Show TCP summary in protocol tree",
665 "Whether the TCP summary line should be shown in the protocol tree",
666 &g_tcp_summary_in_tree);
670 proto_reg_handoff_tcp(void)
672 old_dissector_add("ip.proto", IP_PROTO_TCP, dissect_tcp);