2 * Routines for TCP packet disassembly
4 * $Id: packet-tcp.c,v 1.74 2000/05/11 08:15:52 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>
44 #ifdef NEED_SNPRINTF_H
50 # include "snprintf.h"
54 #include "packet-tcp.h"
56 #include "packet-ip.h"
57 #include "packet-rpc.h"
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_ack = -1;
72 static int hf_tcp_hdr_len = -1;
73 static int hf_tcp_flags = -1;
74 static int hf_tcp_flags_urg = -1;
75 static int hf_tcp_flags_ack = -1;
76 static int hf_tcp_flags_push = -1;
77 static int hf_tcp_flags_reset = -1;
78 static int hf_tcp_flags_syn = -1;
79 static int hf_tcp_flags_fin = -1;
80 static int hf_tcp_window_size = -1;
81 static int hf_tcp_checksum = -1;
82 static int hf_tcp_urgent_pointer = -1;
84 static gint ett_tcp = -1;
85 static gint ett_tcp_flags = -1;
86 static gint ett_tcp_options = -1;
87 static gint ett_tcp_option_sack = -1;
89 static dissector_table_t subdissector_table;
90 static heur_dissector_list_t heur_subdissector_list;
94 #define TCP_PORT_SMTP 25
96 /* TCP structs and definitions */
98 typedef struct _e_tcphdr {
103 guint8 th_off_x2; /* combines th_off and th_x2 */
120 #define TCPOPT_NOP 1 /* Padding */
121 #define TCPOPT_EOL 0 /* End of options */
122 #define TCPOPT_MSS 2 /* Segment size negotiating */
123 #define TCPOPT_WINDOW 3 /* Window scaling */
124 #define TCPOPT_SACK_PERM 4 /* SACK Permitted */
125 #define TCPOPT_SACK 5 /* SACK Block */
126 #define TCPOPT_ECHO 6
127 #define TCPOPT_ECHOREPLY 7
128 #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
130 #define TCPOPT_CCNEW 12
131 #define TCPOPT_CCECHO 13
137 #define TCPOLEN_MSS 4
138 #define TCPOLEN_WINDOW 3
139 #define TCPOLEN_SACK_PERM 2
140 #define TCPOLEN_SACK_MIN 2
141 #define TCPOLEN_ECHO 6
142 #define TCPOLEN_ECHOREPLY 6
143 #define TCPOLEN_TIMESTAMP 10
145 #define TCPOLEN_CCNEW 6
146 #define TCPOLEN_CCECHO 6
149 tcp_info_append_uint(const char *abbrev, guint32 val) {
154 add_len = snprintf(&info_str[info_len], COL_MAX_LEN - info_len, " %s=%u",
161 dissect_tcpopt_maxseg(const ip_tcp_opt *optp, const u_char *opd,
162 int offset, guint optlen, proto_tree *opt_tree)
164 proto_tree_add_text(opt_tree, NullTVB, offset, optlen,
165 "%s: %u bytes", optp->name, pntohs(opd));
166 tcp_info_append_uint("MSS", pntohs(opd));
170 dissect_tcpopt_wscale(const ip_tcp_opt *optp, const u_char *opd,
171 int offset, guint optlen, proto_tree *opt_tree)
173 proto_tree_add_text(opt_tree, NullTVB, offset, optlen,
174 "%s: %u bytes", optp->name, *opd);
175 tcp_info_append_uint("WS", *opd);
179 dissect_tcpopt_sack(const ip_tcp_opt *optp, const u_char *opd,
180 int offset, guint optlen, proto_tree *opt_tree)
182 proto_tree *field_tree = NULL;
184 guint leftedge, rightedge;
186 tf = proto_tree_add_text(opt_tree, NullTVB, offset, optlen, "%s:", optp->name);
187 offset += 2; /* skip past type and length */
188 optlen -= 2; /* subtract size of type and length */
190 if (field_tree == NULL) {
191 /* Haven't yet made a subtree out of this option. Do so. */
192 field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
195 proto_tree_add_text(field_tree, NullTVB, offset, optlen,
196 "(suboption would go past end of option)");
199 /* XXX - check whether it goes past end of packet */
200 leftedge = pntohl(opd);
204 proto_tree_add_text(field_tree, NullTVB, offset, optlen,
205 "(suboption would go past end of option)");
208 /* XXX - check whether it goes past end of packet */
209 rightedge = pntohl(opd);
212 proto_tree_add_text(field_tree, NullTVB, offset, 8,
213 "left edge = %u, right edge = %u", leftedge, rightedge);
214 tcp_info_append_uint("SLE", leftedge);
215 tcp_info_append_uint("SRE", rightedge);
221 dissect_tcpopt_echo(const ip_tcp_opt *optp, const u_char *opd,
222 int offset, guint optlen, proto_tree *opt_tree)
224 proto_tree_add_text(opt_tree, NullTVB, offset, optlen,
225 "%s: %u", optp->name, pntohl(opd));
226 tcp_info_append_uint("ECHO", pntohl(opd));
230 dissect_tcpopt_timestamp(const ip_tcp_opt *optp, const u_char *opd,
231 int offset, guint optlen, proto_tree *opt_tree)
233 proto_tree_add_text(opt_tree, NullTVB, offset, optlen,
234 "%s: tsval %u, tsecr %u", optp->name, pntohl(opd), pntohl(opd + 4));
235 tcp_info_append_uint("TSV", pntohl(opd));
236 tcp_info_append_uint("TSER", pntohl(opd + 4));
240 dissect_tcpopt_cc(const ip_tcp_opt *optp, const u_char *opd,
241 int offset, guint optlen, proto_tree *opt_tree)
243 proto_tree_add_text(opt_tree, NullTVB, offset, optlen,
244 "%s: %u", optp->name, pntohl(opd));
245 tcp_info_append_uint("CC", pntohl(opd));
248 static const ip_tcp_opt tcpopts[] = {
267 "Maximum segment size",
271 dissect_tcpopt_maxseg
279 dissect_tcpopt_wscale
292 &ett_tcp_option_sack,
319 dissect_tcpopt_timestamp
347 #define N_TCP_OPTS (sizeof tcpopts / sizeof tcpopts[0])
350 static const true_false_string flags_set_truth = {
356 /* Determine if there is a sub-dissector and call it. This has been */
357 /* separated into a stand alone routine to other protocol dissectors */
358 /* can call to it, ie. socks */
361 decode_tcp_ports( const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
362 int src_port, int dst_port) {
363 dissector_t sub_dissector;
365 /* determine if this packet is part of a conversation and call dissector */
366 /* for the conversation if available */
368 sub_dissector = find_conversation_dissector( &pi.src, &pi.dst, PT_TCP,
371 (sub_dissector)(pd, offset, fd, tree);
375 /* ONC RPC. We can't base this on anything in the TCP header; we have
376 to look at the payload. If "dissect_rpc()" returns TRUE, it was
377 an RPC packet, otherwise it's some other type of packet. */
378 if (dissect_rpc(pd, offset, fd, tree))
381 /* try to apply the plugins */
384 plugin *pt_plug = plugin_list;
386 if (enabled_plugins_number > 0) {
388 if (pt_plug->enabled && !strcmp(pt_plug->protocol, "tcp") &&
389 tree && dfilter_apply(pt_plug->filter, tree, pd, fd->cap_len)) {
390 pt_plug->dissector(pd, offset, fd, tree);
393 pt_plug = pt_plug->next;
399 /* do lookup with the subdissector table */
400 if (dissector_try_port(subdissector_table, src_port, pd, offset, fd, tree) ||
401 dissector_try_port(subdissector_table, dst_port, pd, offset, fd, tree))
404 /* do lookup with the heuristic subdissector table */
405 if (dissector_try_heuristic(heur_subdissector_list, pd, offset, fd, tree))
408 /* Oh, well, we don't know this; dissect it as data. */
409 dissect_data(pd, offset, fd, tree);
414 dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
416 proto_tree *tcp_tree = NULL, *field_tree = NULL;
418 gchar flags[64] = "<None>";
419 gchar *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG"};
424 guint packet_max = pi.len;
426 /* To do: Check for {cap len,pkt len} < struct len */
427 /* Avoids alignment problems on many architectures. */
428 memcpy(&th, &pd[offset], sizeof(e_tcphdr));
429 th.th_sport = ntohs(th.th_sport);
430 th.th_dport = ntohs(th.th_dport);
431 th.th_win = ntohs(th.th_win);
432 th.th_sum = ntohs(th.th_sum);
433 th.th_urp = ntohs(th.th_urp);
434 th.th_seq = ntohl(th.th_seq);
435 th.th_ack = ntohl(th.th_ack);
437 /* Export the urgent pointer, for the benefit of protocols such as
439 tcp_urgent_pointer = th.th_urp;
443 if (check_col(fd, COL_PROTOCOL) || tree) {
444 for (i = 0; i < 6; i++) {
446 if (th.th_flags & bpos) {
448 strcpy(&flags[fpos], ", ");
451 strcpy(&flags[fpos], fstr[i]);
458 hlen = hi_nibble(th.th_off_x2) * 4; /* TCP header length, in bytes */
460 if (check_col(fd, COL_PROTOCOL))
461 col_add_str(fd, COL_PROTOCOL, "TCP");
462 if (check_col(fd, COL_INFO)) {
463 /* Copy the data into info_str in case one of the option handling
464 routines needs to append to it. */
465 if (th.th_flags & TH_URG)
466 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u Len=%d",
467 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
468 th.th_seq, th.th_ack, th.th_win, th.th_urp, pi.len - offset - hlen);
470 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Len=%d",
471 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
472 th.th_seq, th.th_ack, th.th_win, pi.len - offset - hlen);
473 /* The info column is actually written after the options are decoded */
477 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);
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_item_hidden(tcp_tree, hf_tcp_port, NullTVB, offset, 2, th.th_sport);
484 proto_tree_add_item_hidden(tcp_tree, hf_tcp_port, NullTVB, offset + 2, 2, th.th_dport);
485 proto_tree_add_item(tcp_tree, hf_tcp_seq, NullTVB, offset + 4, 4, th.th_seq);
486 if (th.th_flags & TH_ACK)
487 proto_tree_add_item(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_item(field_tree, hf_tcp_flags_urg, NullTVB, offset + 13, 1, th.th_flags);
494 proto_tree_add_item(field_tree, hf_tcp_flags_ack, NullTVB, offset + 13, 1, th.th_flags);
495 proto_tree_add_item(field_tree, hf_tcp_flags_push, NullTVB, offset + 13, 1, th.th_flags);
496 proto_tree_add_item(field_tree, hf_tcp_flags_reset, NullTVB, offset + 13, 1, th.th_flags);
497 proto_tree_add_item(field_tree, hf_tcp_flags_syn, NullTVB, offset + 13, 1, th.th_flags);
498 proto_tree_add_item(field_tree, hf_tcp_flags_fin, NullTVB, offset + 13, 1, th.th_flags);
499 proto_tree_add_item(tcp_tree, hf_tcp_window_size, NullTVB, offset + 14, 2, th.th_win);
500 proto_tree_add_item(tcp_tree, hf_tcp_checksum, NullTVB, offset + 16, 2, th.th_sum);
501 if (th.th_flags & TH_URG)
502 proto_tree_add_item(tcp_tree, hf_tcp_urgent_pointer, NullTVB, offset + 18, 2, th.th_urp);
505 /* Decode TCP options, if any. */
506 if (tree && hlen > sizeof (e_tcphdr)) {
507 /* There's more than just the fixed-length header. Decode the
509 optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
510 tf = proto_tree_add_text(tcp_tree, NullTVB, offset + 20, optlen,
511 "Options: (%d bytes)", optlen);
512 field_tree = proto_item_add_subtree(tf, ett_tcp_options);
513 dissect_ip_tcp_options(&pd[offset + 20], offset + 20, optlen,
514 tcpopts, N_TCP_OPTS, TCPOPT_EOL, field_tree);
517 if (check_col(fd, COL_INFO))
518 col_add_str(fd, COL_INFO, info_str);
520 /* Skip over header + options */
524 pi.srcport = th.th_sport;
525 pi.destport = th.th_dport;
527 /* Check the packet length to see if there's more data
528 (it could be an ACK-only packet) */
529 if (packet_max > offset)
530 decode_tcp_ports( pd, offset, fd, tree, th.th_sport, th.th_dport);
532 if( data_out_file ) {
533 reassemble_tcp( th.th_seq, /* sequence number */
534 ( pi.len - offset ), /* data length */
535 ( pd+offset ), /* data */
536 ( pi.captured_len - offset ), /* captured data length */
537 ( th.th_flags & TH_SYN ), /* is syn set? */
546 proto_register_tcp(void)
548 static hf_register_info hf[] = {
551 { "Source Port", "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
555 { "Destination Port", "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
559 { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
563 { "Sequence number", "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
567 { "Acknowledgement number", "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
571 { "Header Length", "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
575 { "Flags", "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
579 { "Urgent", "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
583 { "Acknowledgment", "tcp.flags.ack", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ACK,
586 { &hf_tcp_flags_push,
587 { "Push", "tcp.flags.push", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_PUSH,
590 { &hf_tcp_flags_reset,
591 { "Reset", "tcp.flags.reset", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_RST,
595 { "Syn", "tcp.flags.syn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_SYN,
599 { "Fin", "tcp.flags.fin", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_FIN,
602 { &hf_tcp_window_size,
603 { "Window size", "tcp.window_size", FT_UINT16, BASE_DEC, NULL, 0x0,
607 { "Checksum", "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
610 { &hf_tcp_urgent_pointer,
611 { "Urgent pointer", "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
614 static gint *ett[] = {
618 &ett_tcp_option_sack,
621 proto_tcp = proto_register_protocol ("Transmission Control Protocol", "tcp");
622 proto_register_field_array(proto_tcp, hf, array_length(hf));
623 proto_register_subtree_array(ett, array_length(ett));
625 /* subdissector code */
626 subdissector_table = register_dissector_table("tcp.port");
627 register_heur_dissector_list("tcp", &heur_subdissector_list);
631 proto_reg_handoff_tcp(void)
633 dissector_add("ip.proto", IP_PROTO_TCP, dissect_tcp);