2 * Routines for TCP packet disassembly
4 * $Id: packet-tcp.c,v 1.67 2000/04/12 22:53:15 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>
44 #ifdef NEED_SNPRINTF_H
50 # include "snprintf.h"
55 #include "packet-ip.h"
56 #include "packet-giop.h"
57 #include "packet-rpc.h"
58 #include "packet-yhoo.h"
60 extern FILE* data_out_file;
62 guint16 tcp_urgent_pointer;
64 static gchar info_str[COL_MAX_LEN];
67 static int proto_tcp = -1;
68 static int hf_tcp_srcport = -1;
69 static int hf_tcp_dstport = -1;
70 static int hf_tcp_port = -1;
71 static int hf_tcp_seq = -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_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;
94 #define TCP_PORT_SMTP 25
95 #define TCP_PORT_YHOO 5050
97 /* TCP structs and definitions */
99 typedef struct _e_tcphdr {
104 guint8 th_off_x2; /* combines th_off and th_x2 */
121 #define TCPOPT_NOP 1 /* Padding */
122 #define TCPOPT_EOL 0 /* End of options */
123 #define TCPOPT_MSS 2 /* Segment size negotiating */
124 #define TCPOPT_WINDOW 3 /* Window scaling */
125 #define TCPOPT_SACK_PERM 4 /* SACK Permitted */
126 #define TCPOPT_SACK 5 /* SACK Block */
127 #define TCPOPT_ECHO 6
128 #define TCPOPT_ECHOREPLY 7
129 #define TCPOPT_TIMESTAMP 8 /* Better RTT estimations/PAWS */
131 #define TCPOPT_CCNEW 12
132 #define TCPOPT_CCECHO 13
138 #define TCPOLEN_MSS 4
139 #define TCPOLEN_WINDOW 3
140 #define TCPOLEN_SACK_PERM 2
141 #define TCPOLEN_SACK_MIN 2
142 #define TCPOLEN_ECHO 6
143 #define TCPOLEN_ECHOREPLY 6
144 #define TCPOLEN_TIMESTAMP 10
146 #define TCPOLEN_CCNEW 6
147 #define TCPOLEN_CCECHO 6
150 tcp_info_append_uint(const char *abbrev, guint32 val) {
155 add_len = snprintf(&info_str[info_len], COL_MAX_LEN - info_len, " %s=%u",
162 dissect_tcpopt_maxseg(const ip_tcp_opt *optp, const u_char *opd,
163 int offset, guint optlen, proto_tree *opt_tree)
165 proto_tree_add_text(opt_tree, offset, optlen,
166 "%s: %u bytes", optp->name, pntohs(opd));
167 tcp_info_append_uint("MSS", pntohs(opd));
171 dissect_tcpopt_wscale(const ip_tcp_opt *optp, const u_char *opd,
172 int offset, guint optlen, proto_tree *opt_tree)
174 proto_tree_add_text(opt_tree, offset, optlen,
175 "%s: %u bytes", optp->name, *opd);
176 tcp_info_append_uint("WS", *opd);
180 dissect_tcpopt_sack(const ip_tcp_opt *optp, const u_char *opd,
181 int offset, guint optlen, proto_tree *opt_tree)
183 proto_tree *field_tree = NULL;
185 guint leftedge, rightedge;
187 tf = proto_tree_add_text(opt_tree, offset, optlen, "%s:", optp->name);
188 offset += 2; /* skip past type and length */
189 optlen -= 2; /* subtract size of type and length */
191 if (field_tree == NULL) {
192 /* Haven't yet made a subtree out of this option. Do so. */
193 field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
196 proto_tree_add_text(field_tree, offset, optlen,
197 "(suboption would go past end of option)");
200 /* XXX - check whether it goes past end of packet */
201 leftedge = pntohl(opd);
205 proto_tree_add_text(field_tree, offset, optlen,
206 "(suboption would go past end of option)");
209 /* XXX - check whether it goes past end of packet */
210 rightedge = pntohl(opd);
213 proto_tree_add_text(field_tree, offset, 8,
214 "left edge = %u, right edge = %u", leftedge, rightedge);
215 tcp_info_append_uint("SLE", leftedge);
216 tcp_info_append_uint("SRE", rightedge);
222 dissect_tcpopt_echo(const ip_tcp_opt *optp, const u_char *opd,
223 int offset, guint optlen, proto_tree *opt_tree)
225 proto_tree_add_text(opt_tree, offset, optlen,
226 "%s: %u", optp->name, pntohl(opd));
227 tcp_info_append_uint("ECHO", pntohl(opd));
231 dissect_tcpopt_timestamp(const ip_tcp_opt *optp, const u_char *opd,
232 int offset, guint optlen, proto_tree *opt_tree)
234 proto_tree_add_text(opt_tree, offset, optlen,
235 "%s: tsval %u, tsecr %u", optp->name, pntohl(opd), pntohl(opd + 4));
236 tcp_info_append_uint("TSV", pntohl(opd));
237 tcp_info_append_uint("TSER", pntohl(opd + 4));
241 dissect_tcpopt_cc(const ip_tcp_opt *optp, const u_char *opd,
242 int offset, guint optlen, proto_tree *opt_tree)
244 proto_tree_add_text(opt_tree, offset, optlen,
245 "%s: %u", optp->name, pntohl(opd));
246 tcp_info_append_uint("CC", pntohl(opd));
249 static const ip_tcp_opt tcpopts[] = {
268 "Maximum segment size",
272 dissect_tcpopt_maxseg
280 dissect_tcpopt_wscale
293 &ett_tcp_option_sack,
320 dissect_tcpopt_timestamp
348 #define N_TCP_OPTS (sizeof tcpopts / sizeof tcpopts[0])
351 static const true_false_string flags_set_truth = {
358 decode_tcp_ports( const u_char *pd, int offset, frame_data *fd, proto_tree *tree,
359 int src_port, int dst_port) {
361 /* Determine if there is a sub-dissector and call it. This has been */
362 /* separated into a stand alone routine to other protocol dissectors */
363 /* can call to it, ie. socks */
366 dissector_t sub_dissector;
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)) {
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,
402 dissector_try_port(subdissector_table, dst_port, pd, offset,
406 /* check existence of high level protocols */
408 #define PORT_IS(port) ( src_port == port || dst_port == port)
410 if (memcmp(&pd[offset], "GIOP", 4) == 0) {
411 dissect_giop(pd, offset, fd, tree);
415 if ( PORT_IS(TCP_PORT_YHOO) &&
416 (memcmp(&pd[offset], "YPNS", 4) == 0 ||
417 memcmp(&pd[offset], "YHOO", 4) == 0 )) {
418 dissect_yhoo(pd, offset, fd, tree);
422 dissect_data(pd, offset, fd, tree);
427 dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
429 proto_tree *tcp_tree = NULL, *field_tree = NULL;
431 gchar flags[64] = "<None>";
432 gchar *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG"};
437 guint packet_max = pi.len;
439 /* To do: Check for {cap len,pkt len} < struct len */
440 /* Avoids alignment problems on many architectures. */
441 memcpy(&th, &pd[offset], sizeof(e_tcphdr));
442 th.th_sport = ntohs(th.th_sport);
443 th.th_dport = ntohs(th.th_dport);
444 th.th_win = ntohs(th.th_win);
445 th.th_sum = ntohs(th.th_sum);
446 th.th_urp = ntohs(th.th_urp);
447 th.th_seq = ntohl(th.th_seq);
448 th.th_ack = ntohl(th.th_ack);
450 /* Export the urgent pointer, for the benefit of protocols such as
452 tcp_urgent_pointer = th.th_urp;
456 if (check_col(fd, COL_PROTOCOL) || tree) {
457 for (i = 0; i < 6; i++) {
459 if (th.th_flags & bpos) {
461 strcpy(&flags[fpos], ", ");
464 strcpy(&flags[fpos], fstr[i]);
471 hlen = hi_nibble(th.th_off_x2) * 4; /* TCP header length, in bytes */
473 if (check_col(fd, COL_PROTOCOL))
474 col_add_str(fd, COL_PROTOCOL, "TCP");
475 if (check_col(fd, COL_INFO)) {
476 /* Copy the data into info_str in case one of the option handling
477 routines needs to append to it. */
478 if (th.th_flags & TH_URG)
479 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u Len=%d",
480 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
481 th.th_seq, th.th_ack, th.th_win, th.th_urp, pi.len - offset - hlen);
483 info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Len=%d",
484 get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
485 th.th_seq, th.th_ack, th.th_win, pi.len - offset - hlen);
486 /* The info column is actually written after the options are decoded */
490 ti = proto_tree_add_protocol_format(tree, proto_tcp, 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);
491 tcp_tree = proto_item_add_subtree(ti, ett_tcp);
492 proto_tree_add_uint_format(tcp_tree, hf_tcp_srcport, offset, 2, th.th_sport,
493 "Source port: %s (%u)", get_tcp_port(th.th_sport), th.th_sport);
494 proto_tree_add_uint_format(tcp_tree, hf_tcp_dstport, offset + 2, 2, th.th_dport,
495 "Destination port: %s (%u)", get_tcp_port(th.th_dport), th.th_dport);
496 proto_tree_add_item_hidden(tcp_tree, hf_tcp_port, offset, 2, th.th_sport);
497 proto_tree_add_item_hidden(tcp_tree, hf_tcp_port, offset + 2, 2, th.th_dport);
498 proto_tree_add_item(tcp_tree, hf_tcp_seq, offset + 4, 4, th.th_seq);
499 if (th.th_flags & TH_ACK)
500 proto_tree_add_item(tcp_tree, hf_tcp_ack, offset + 8, 4, th.th_ack);
501 proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, offset + 12, 1, hlen,
502 "Header length: %u bytes", hlen);
503 tf = proto_tree_add_uint_format(tcp_tree, hf_tcp_flags, offset + 13, 1,
504 th.th_flags, "Flags: 0x%04x (%s)", th.th_flags, flags);
505 field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
506 proto_tree_add_item(field_tree, hf_tcp_flags_urg, offset + 13, 1, th.th_flags);
507 proto_tree_add_item(field_tree, hf_tcp_flags_ack, offset + 13, 1, th.th_flags);
508 proto_tree_add_item(field_tree, hf_tcp_flags_push, offset + 13, 1, th.th_flags);
509 proto_tree_add_item(field_tree, hf_tcp_flags_reset, offset + 13, 1, th.th_flags);
510 proto_tree_add_item(field_tree, hf_tcp_flags_syn, offset + 13, 1, th.th_flags);
511 proto_tree_add_item(field_tree, hf_tcp_flags_fin, offset + 13, 1, th.th_flags);
512 proto_tree_add_item(tcp_tree, hf_tcp_window_size, offset + 14, 2, th.th_win);
513 proto_tree_add_item(tcp_tree, hf_tcp_checksum, offset + 16, 2, th.th_sum);
514 if (th.th_flags & TH_URG)
515 proto_tree_add_item(tcp_tree, hf_tcp_urgent_pointer, offset + 18, 2, th.th_urp);
518 /* Decode TCP options, if any. */
519 if (tree && hlen > sizeof (e_tcphdr)) {
520 /* There's more than just the fixed-length header. Decode the
522 optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
523 tf = proto_tree_add_text(tcp_tree, offset + 20, optlen,
524 "Options: (%d bytes)", optlen);
525 field_tree = proto_item_add_subtree(tf, ett_tcp_options);
526 dissect_ip_tcp_options(&pd[offset + 20], offset + 20, optlen,
527 tcpopts, N_TCP_OPTS, TCPOPT_EOL, field_tree);
530 if (check_col(fd, COL_INFO))
531 col_add_str(fd, COL_INFO, info_str);
533 /* Skip over header + options */
537 pi.srcport = th.th_sport;
538 pi.destport = th.th_dport;
540 /* Check the packet length to see if there's more data
541 (it could be an ACK-only packet) */
542 if (packet_max > offset)
543 decode_tcp_ports( pd, offset, fd, tree, th.th_sport, th.th_dport);
545 if( data_out_file ) {
546 reassemble_tcp( th.th_seq, /* sequence number */
547 ( pi.len - offset ), /* data length */
548 ( pd+offset ), /* data */
549 ( pi.captured_len - offset ), /* captured data length */
550 ( th.th_flags & TH_SYN ), /* is syn set? */
559 proto_register_tcp(void)
561 static hf_register_info hf[] = {
564 { "Source Port", "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
568 { "Destination Port", "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
572 { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
576 { "Sequence number", "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
580 { "Acknowledgement number", "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
584 { "Header Length", "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
588 { "Flags", "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
592 { "Urgent", "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
596 { "Acknowledgment", "tcp.flags.ack", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ACK,
599 { &hf_tcp_flags_push,
600 { "Push", "tcp.flags.push", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_PUSH,
603 { &hf_tcp_flags_reset,
604 { "Reset", "tcp.flags.reset", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_RST,
608 { "Syn", "tcp.flags.syn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_SYN,
612 { "Fin", "tcp.flags.fin", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_FIN,
615 { &hf_tcp_window_size,
616 { "Window size", "tcp.window_size", FT_UINT16, BASE_DEC, NULL, 0x0,
620 { "Checksum", "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
623 { &hf_tcp_urgent_pointer,
624 { "Urgent pointer", "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
627 static gint *ett[] = {
631 &ett_tcp_option_sack,
634 proto_tcp = proto_register_protocol ("Transmission Control Protocol", "tcp");
635 proto_register_field_array(proto_tcp, hf, array_length(hf));
636 proto_register_subtree_array(ett, array_length(ett));
638 /* subdissector code */
639 subdissector_table = register_dissector_table(hf_tcp_port);