Fix the Frame Relay dissector to call subdissectors regardless of
[obnox/wireshark/wip.git] / packet-tcp.c
index cb23b3a0f91ca77cc6e9d18bfaf57884b48db094..44059d4b079ee5466e445dc786a84342e99550b3 100644 (file)
@@ -1,7 +1,7 @@
 /* packet-tcp.c
  * Routines for TCP packet disassembly
  *
- * $Id: packet-tcp.c,v 1.63 2000/04/04 05:37:35 guy Exp $
+ * $Id: packet-tcp.c,v 1.95 2001/01/03 07:53:43 guy Exp $
  *
  * Ethereal - Network traffic analyzer
  * By Gerald Combs <gerald@zing.org>
 #endif
 
 #include <stdio.h>
+#include <string.h>
 #include <glib.h>
-#include "globals.h"
-#include "resolv.h"
-#include "follow.h"
+#include "in_cksum.h"
 
 #ifdef NEED_SNPRINTF_H
-# ifdef HAVE_STDARG_H
-#  include <stdarg.h>
-# else
-#  include <varargs.h>
-# endif
 # include "snprintf.h"
 #endif
 
+#include "globals.h"
+#include "resolv.h"
+#include "follow.h"
+#include "prefs.h"
 #include "plugins.h"
-
-#include "packet-bgp.h"
+#include "packet-tcp.h"
 #include "packet-ip.h"
-#include "packet-ftp.h"
-#include "packet-giop.h"
-#include "packet-http.h"
-#include "packet-imap.h"
-#include "packet-irc.h"
-#include "packet-ldap.h"
-#include "packet-lpd.h"
-#include "packet-mapi.h"
-#include "packet-nbns.h"
-#include "packet-ncp.h"
-#include "packet-nntp.h"
-#include "packet-ntp.h"
-#include "packet-pop.h"
-#include "packet-pptp.h"
-#include "packet-rpc.h"
-#include "packet-rtsp.h"
-#include "packet-srvloc.h"
-#include "packet-tacacs.h"
-#include "packet-telnet.h"
-#include "packet-tns.h"
-#include "packet-yhoo.h"
+#include "conversation.h"
+#include "strutil.h"
+
+/* Place TCP summary in proto tree */
+static gboolean tcp_summary_in_tree = TRUE;
 
 extern FILE* data_out_file;
 
-static gchar info_str[COL_MAX_LEN];
-static int   info_len;
+guint16 tcp_urgent_pointer;
 
 static int proto_tcp = -1;
 static int hf_tcp_srcport = -1;
 static int hf_tcp_dstport = -1;
 static int hf_tcp_port = -1;
 static int hf_tcp_seq = -1;
+static int hf_tcp_nxtseq = -1;
 static int hf_tcp_ack = -1;
 static int hf_tcp_hdr_len = -1;
 static int hf_tcp_flags = -1;
+static int hf_tcp_flags_cwr = -1;
+static int hf_tcp_flags_ecn = -1;
 static int hf_tcp_flags_urg = -1;
 static int hf_tcp_flags_ack = -1;
 static int hf_tcp_flags_push = -1;
@@ -105,35 +88,11 @@ static gint ett_tcp_options = -1;
 static gint ett_tcp_option_sack = -1;
 
 static dissector_table_t subdissector_table;
+static heur_dissector_list_t heur_subdissector_list;
 
 /* TCP Ports */
 
-#define TCP_PORT_FTPDATA               20
-#define TCP_PORT_FTP                   21
-#define TCP_PORT_TELNET                        23
 #define TCP_PORT_SMTP                  25
-#define TCP_PORT_HTTP                  80
-#define TCP_PORT_TACACS                        49
-#define TCP_PORT_POP                   110
-#define TCP_PORT_NNTP                  119
-#define TCP_PORT_NTP                   123
-#define TCP_PORT_NBSS                  139
-#define TCP_PORT_IMAP                  143
-#define TCP_PORT_BGP                   179
-#define TCP_PORT_LDAP                  389
-#define TCP_PORT_SRVLOC                        427
-#define TCP_PORT_PRINTER               515
-#define TCP_PORT_NCP                   524
-#define TCP_PORT_RTSP                  554
-#define TCP_PORT_MAPI                  1065
-#define TCP_PORT_TNS                   1521
-#define TCP_PORT_PPTP                  1723
-#define TCP_PORT_PROXY_HTTP            3128
-#define TCP_PORT_PROXY_ADMIN_HTTP      3132
-#define TCP_PORT_YHOO                  5050
-#define TCP_ALT_PORT_HTTP              8080
-#define TCP_PORT_IRC                   6667
-       /* good candidate for dynamic port specification */
 
 /* TCP structs and definitions */
 
@@ -150,6 +109,8 @@ typedef struct _e_tcphdr {
 #define TH_PUSH 0x08
 #define TH_ACK  0x10
 #define TH_URG  0x20
+#define TH_ECN  0x40
+#define TH_CWR  0x80
   guint16 th_win;
   guint16 th_sum;
   guint16 th_urp;
@@ -171,6 +132,7 @@ typedef struct _e_tcphdr {
 #define TCPOPT_CC               11
 #define TCPOPT_CCNEW            12
 #define TCPOPT_CCECHO           13
+#define TCPOPT_MD5              19      /* RFC2385 */
 
 /*
  *     TCP option lengths
@@ -186,46 +148,48 @@ typedef struct _e_tcphdr {
 #define TCPOLEN_CC             6
 #define TCPOLEN_CCNEW          6
 #define TCPOLEN_CCECHO         6
+#define TCPOLEN_MD5            18
 
 static void
-tcp_info_append_uint(const char *abbrev, guint32 val) {
-  int add_len = 0;
-  
-  if (info_len > 0)
-  if(info_len > 0)
-    add_len = snprintf(&info_str[info_len], COL_MAX_LEN - info_len, " %s=%u",
-      abbrev, val);
-  if (add_len > 0)
-    info_len += add_len;
+tcp_info_append_uint(frame_data *fd, const char *abbrev, guint32 val)
+{
+  if (check_col(fd, COL_INFO))
+    col_append_fstr(fd, COL_INFO, " %s=%u", abbrev, val);
 }
 
 static void
-dissect_tcpopt_maxseg(const ip_tcp_opt *optp, const u_char *opd,
-    int offset, guint optlen, proto_tree *opt_tree)
+dissect_tcpopt_maxseg(const ip_tcp_opt *optp, tvbuff_t *tvb,
+    int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
 {
-  proto_tree_add_text(opt_tree, offset,      optlen,
-                       "%s: %u bytes", optp->name, pntohs(opd));
-  tcp_info_append_uint("MSS", pntohs(opd));
+  guint16 mss;
+
+  mss = tvb_get_ntohs(tvb, offset + 2);
+  proto_tree_add_text(opt_tree, tvb, offset,      optlen,
+                       "%s: %u bytes", optp->name, mss);
+  tcp_info_append_uint(fd, "MSS", mss);
 }
 
 static void
-dissect_tcpopt_wscale(const ip_tcp_opt *optp, const u_char *opd,
-    int offset, guint optlen, proto_tree *opt_tree)
+dissect_tcpopt_wscale(const ip_tcp_opt *optp, tvbuff_t *tvb,
+    int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
 {
-  proto_tree_add_text(opt_tree, offset,      optlen,
-                       "%s: %u bytes", optp->name, *opd);
-  tcp_info_append_uint("WS", *opd);
+  guint8 ws;
+
+  ws = tvb_get_guint8(tvb, offset + 2);
+  proto_tree_add_text(opt_tree, tvb, offset,      optlen,
+                       "%s: %u bytes", optp->name, ws);
+  tcp_info_append_uint(fd, "WS", ws);
 }
 
 static void
-dissect_tcpopt_sack(const ip_tcp_opt *optp, const u_char *opd,
-    int offset, guint optlen, proto_tree *opt_tree)
+dissect_tcpopt_sack(const ip_tcp_opt *optp, tvbuff_t *tvb,
+    int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
 {
   proto_tree *field_tree = NULL;
   proto_item *tf;
   guint leftedge, rightedge;
 
-  tf = proto_tree_add_text(opt_tree, offset,      optlen, "%s:", optp->name);
+  tf = proto_tree_add_text(opt_tree, tvb, offset,      optlen, "%s:", optp->name);
   offset += 2; /* skip past type and length */
   optlen -= 2; /* subtract size of type and length */
   while (optlen > 0) {
@@ -234,57 +198,64 @@ dissect_tcpopt_sack(const ip_tcp_opt *optp, const u_char *opd,
       field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
     }
     if (optlen < 4) {
-      proto_tree_add_text(field_tree, offset,      optlen,
+      proto_tree_add_text(field_tree, tvb, offset,      optlen,
         "(suboption would go past end of option)");
       break;
     }
-    /* XXX - check whether it goes past end of packet */
-    leftedge = pntohl(opd);
-    opd += 4;
+    leftedge = tvb_get_ntohl(tvb, offset);
     optlen -= 4;
     if (optlen < 4) {
-      proto_tree_add_text(field_tree, offset,      optlen,
+      proto_tree_add_text(field_tree, tvb, offset,      optlen,
         "(suboption would go past end of option)");
       break;
     }
     /* XXX - check whether it goes past end of packet */
-    rightedge = pntohl(opd);
-    opd += 4;
+    rightedge = tvb_get_ntohl(tvb, offset + 4);
     optlen -= 4;
-    proto_tree_add_text(field_tree, offset,      8,
+    proto_tree_add_text(field_tree, tvb, offset,      8,
         "left edge = %u, right edge = %u", leftedge, rightedge);
-    tcp_info_append_uint("SLE", leftedge);
-    tcp_info_append_uint("SRE", rightedge);
+    tcp_info_append_uint(fd, "SLE", leftedge);
+    tcp_info_append_uint(fd, "SRE", rightedge);
     offset += 8;
   }
 }
 
 static void
-dissect_tcpopt_echo(const ip_tcp_opt *optp, const u_char *opd,
-    int offset, guint optlen, proto_tree *opt_tree)
+dissect_tcpopt_echo(const ip_tcp_opt *optp, tvbuff_t *tvb,
+    int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
 {
-  proto_tree_add_text(opt_tree, offset,      optlen,
-                       "%s: %u", optp->name, pntohl(opd));
-  tcp_info_append_uint("ECHO", pntohl(opd));
+  guint32 echo;
+
+  echo = tvb_get_ntohl(tvb, offset + 2);
+  proto_tree_add_text(opt_tree, tvb, offset,      optlen,
+                       "%s: %u", optp->name, echo);
+  tcp_info_append_uint(fd, "ECHO", echo);
 }
 
 static void
-dissect_tcpopt_timestamp(const ip_tcp_opt *optp, const u_char *opd,
-    int offset, guint optlen, proto_tree *opt_tree)
+dissect_tcpopt_timestamp(const ip_tcp_opt *optp, tvbuff_t *tvb,
+    int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
 {
-  proto_tree_add_text(opt_tree, offset,      optlen,
-    "%s: tsval %u, tsecr %u", optp->name, pntohl(opd), pntohl(opd + 4));
-  tcp_info_append_uint("TSV", pntohl(opd));
-  tcp_info_append_uint("TSER", pntohl(opd + 4));
+  guint32 tsv, tser;
+
+  tsv = tvb_get_ntohl(tvb, offset + 2);
+  tser = tvb_get_ntohl(tvb, offset + 6);
+  proto_tree_add_text(opt_tree, tvb, offset,      optlen,
+    "%s: tsval %u, tsecr %u", optp->name, tsv, tser);
+  tcp_info_append_uint(fd, "TSV", tsv);
+  tcp_info_append_uint(fd, "TSER", tser);
 }
 
 static void
-dissect_tcpopt_cc(const ip_tcp_opt *optp, const u_char *opd,
-    int offset, guint optlen, proto_tree *opt_tree)
+dissect_tcpopt_cc(const ip_tcp_opt *optp, tvbuff_t *tvb,
+    int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
 {
-  proto_tree_add_text(opt_tree, offset,      optlen,
-                       "%s: %u", optp->name, pntohl(opd));
-  tcp_info_append_uint("CC", pntohl(opd));
+  guint32 cc;
+
+  cc = tvb_get_ntohl(tvb, offset + 2);
+  proto_tree_add_text(opt_tree, tvb, offset,      optlen,
+                       "%s: %u", optp->name, cc);
+  tcp_info_append_uint(fd, "CC", cc);
 }
 
 static const ip_tcp_opt tcpopts[] = {
@@ -383,6 +354,14 @@ static const ip_tcp_opt tcpopts[] = {
     FIXED_LENGTH,
     TCPOLEN_CCECHO,
     dissect_tcpopt_cc
+  },
+  {
+    TCPOPT_MD5,
+    "TCP MD5 signature",
+    NULL,
+    FIXED_LENGTH,
+    TCPOLEN_MD5,
+    NULL
   }
 };
 
@@ -394,22 +373,98 @@ static const true_false_string flags_set_truth = {
   "Not set"
 };
 
+
+/* Determine if there is a sub-dissector and call it.  This has been */
+/* separated into a stand alone routine to other protocol dissectors */
+/* can call to it, ie. socks   */
+
 void
-dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
+decode_tcp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
+       proto_tree *tree, int src_port, int dst_port)
+{
+  tvbuff_t *next_tvb;
+#ifdef HAVE_PLUGINS
+  const u_char *next_pd;
+  int next_offset;
+#endif
+
+  next_tvb = tvb_new_subset(tvb, offset, -1, -1);
+
+/* determine if this packet is part of a conversation and call dissector */
+/* for the conversation if available */
+
+  if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_TCP,
+               src_port, dst_port, next_tvb, pinfo, tree))
+       return;
+
+  /* try to apply the plugins */
+#ifdef HAVE_PLUGINS
+  {
+    plugin *pt_plug = plugin_list;
+
+    if (enabled_plugins_number > 0) {
+      tvb_compat(next_tvb, &next_pd, &next_offset);
+      while (pt_plug) {
+       if (pt_plug->enabled && strstr(pt_plug->protocol, "tcp") &&
+           tree && dfilter_apply(pt_plug->filter, tree, next_pd, pinfo->fd->cap_len)) {
+         pt_plug->dissector(next_pd, next_offset, pinfo->fd, tree);
+         return;
+       }
+       pt_plug = pt_plug->next;
+      }
+    }
+  }
+#endif
+
+  /* do lookup with the subdissector table */
+  if (dissector_try_port(subdissector_table, src_port, next_tvb, pinfo, tree) ||
+      dissector_try_port(subdissector_table, dst_port, next_tvb, pinfo, tree))
+    return;
+
+  /* do lookup with the heuristic subdissector table */
+  if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
+    return;
+
+  /* Oh, well, we don't know this; dissect it as data. */
+  dissect_data(next_tvb, 0, pinfo, tree);
+}
+
+
+static void
+dissect_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
+{
   e_tcphdr   th;
   proto_tree *tcp_tree = NULL, *field_tree = NULL;
   proto_item *ti, *tf;
+  int        offset = 0;
   gchar      flags[64] = "<None>";
-  gchar     *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG"};
+  gchar     *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG", "ECN", "CWR" };
   gint       fpos = 0, i;
   guint      bpos;
   guint      hlen;
   guint      optlen;
-  guint      packet_max = pi.len;
+  guint32    seglen;
+  guint32    nxtseq;
+  guint      len;
+  guint      reported_len;
+  vec_t      cksum_vec[4];
+  guint32    phdr[2];
+  guint16    computed_cksum;
+  guint      length_remaining;
+
+  CHECK_DISPLAY_AS_DATA(proto_tcp, tvb, pinfo, tree);
+
+  pinfo->current_proto = "TCP";
+
+  if (check_col(pinfo->fd, COL_PROTOCOL))
+    col_set_str(pinfo->fd, COL_PROTOCOL, "TCP");
+
+  /* Clear out the Info column. */
+  if (check_col(pinfo->fd, COL_INFO))
+    col_clear(pinfo->fd, COL_INFO);
 
-  /* To do: Check for {cap len,pkt len} < struct len */
   /* Avoids alignment problems on many architectures. */
-  memcpy(&th, &pd[offset], sizeof(e_tcphdr));
+  tvb_memcpy(tvb, (guint8 *)&th, offset, sizeof(e_tcphdr));
   th.th_sport = ntohs(th.th_sport);
   th.th_dport = ntohs(th.th_dport);
   th.th_win   = ntohs(th.th_win);
@@ -417,11 +472,13 @@ dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
   th.th_urp   = ntohs(th.th_urp);
   th.th_seq   = ntohl(th.th_seq);
   th.th_ack   = ntohl(th.th_ack);
-  
-  info_len = 0;
 
-  if (check_col(fd, COL_PROTOCOL) || tree) {  
-    for (i = 0; i < 6; i++) {
+  /* Export the urgent pointer, for the benefit of protocols such as
+     rlogin. */
+  tcp_urgent_pointer = th.th_urp;
+  if (check_col(pinfo->fd, COL_INFO) || tree) {  
+    for (i = 0; i < 8; i++) {
       bpos = 1 << i;
       if (th.th_flags & bpos) {
         if (fpos) {
@@ -437,49 +494,107 @@ dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
   
   hlen = hi_nibble(th.th_off_x2) * 4;  /* TCP header length, in bytes */
 
-  if (check_col(fd, COL_PROTOCOL))
-    col_add_str(fd, COL_PROTOCOL, "TCP");
-  if (check_col(fd, COL_INFO)) {
-    /* Copy the data into info_str in case one of the option handling
-       routines needs to append to it. */
+  reported_len = tvb_reported_length(tvb);
+  len = tvb_length(tvb);
+
+  /* Compute the length of data in this segment. */
+  seglen = reported_len - hlen;
+
+  /* Compute the sequence number of next octet after this segment. */
+  nxtseq = th.th_seq + seglen;
+
+  if (check_col(pinfo->fd, COL_INFO)) {
     if (th.th_flags & TH_URG)
-      info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u Len=%d",
+      col_append_fstr(pinfo->fd, COL_INFO, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u Len=%d",
         get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
-        th.th_seq, th.th_ack, th.th_win, th.th_urp, pi.len - offset - hlen);
+        th.th_seq, th.th_ack, th.th_win, th.th_urp, seglen);
     else
-      info_len = snprintf(info_str, COL_MAX_LEN, "%s > %s [%s] Seq=%u Ack=%u Win=%u Len=%d",
+      col_append_fstr(pinfo->fd, COL_INFO, "%s > %s [%s] Seq=%u Ack=%u Win=%u Len=%d",
         get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
-        th.th_seq, th.th_ack, th.th_win, pi.len - offset - hlen);
-    /* The info column is actually written after the options are decoded */
+        th.th_seq, th.th_ack, th.th_win, seglen);
   }
   
   if (tree) {
-    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);
+    if (tcp_summary_in_tree) {
+           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);
+    }
+    else {
+           ti = proto_tree_add_item(tree, proto_tcp, tvb, offset, hlen, FALSE);
+    }
     tcp_tree = proto_item_add_subtree(ti, ett_tcp);
-    proto_tree_add_uint_format(tcp_tree, hf_tcp_srcport, offset, 2, th.th_sport,
+    proto_tree_add_uint_format(tcp_tree, hf_tcp_srcport, tvb, offset, 2, th.th_sport,
        "Source port: %s (%u)", get_tcp_port(th.th_sport), th.th_sport);
-    proto_tree_add_uint_format(tcp_tree, hf_tcp_dstport, offset + 2, 2, th.th_dport,
+    proto_tree_add_uint_format(tcp_tree, hf_tcp_dstport, tvb, offset + 2, 2, th.th_dport,
        "Destination port: %s (%u)", get_tcp_port(th.th_dport), th.th_dport);
-    proto_tree_add_item_hidden(tcp_tree, hf_tcp_port, offset, 2, th.th_sport);
-    proto_tree_add_item_hidden(tcp_tree, hf_tcp_port, offset + 2, 2, th.th_dport);
-    proto_tree_add_item(tcp_tree, hf_tcp_seq, offset + 4, 4, th.th_seq);
+    proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset, 2, th.th_sport);
+    proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset + 2, 2, th.th_dport);
+    proto_tree_add_uint(tcp_tree, hf_tcp_seq, tvb, offset + 4, 4, th.th_seq);
+    if (nxtseq != th.th_seq)
+      proto_tree_add_uint(tcp_tree, hf_tcp_nxtseq, tvb, offset, 0, nxtseq);
     if (th.th_flags & TH_ACK)
-      proto_tree_add_item(tcp_tree, hf_tcp_ack, offset + 8, 4, th.th_ack);
-    proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, offset + 12, 1, hlen,
+      proto_tree_add_uint(tcp_tree, hf_tcp_ack, tvb, offset + 8, 4, th.th_ack);
+    proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, tvb, offset + 12, 1, hlen,
        "Header length: %u bytes", hlen);
-    tf = proto_tree_add_uint_format(tcp_tree, hf_tcp_flags, offset + 13, 1,
+    tf = proto_tree_add_uint_format(tcp_tree, hf_tcp_flags, tvb, offset + 13, 1,
        th.th_flags, "Flags: 0x%04x (%s)", th.th_flags, flags);
     field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
-    proto_tree_add_item(field_tree, hf_tcp_flags_urg, offset + 13, 1, th.th_flags);
-    proto_tree_add_item(field_tree, hf_tcp_flags_ack, offset + 13, 1, th.th_flags);
-    proto_tree_add_item(field_tree, hf_tcp_flags_push, offset + 13, 1, th.th_flags);
-    proto_tree_add_item(field_tree, hf_tcp_flags_reset, offset + 13, 1, th.th_flags);
-    proto_tree_add_item(field_tree, hf_tcp_flags_syn, offset + 13, 1, th.th_flags);
-    proto_tree_add_item(field_tree, hf_tcp_flags_fin, offset + 13, 1, th.th_flags);
-    proto_tree_add_item(tcp_tree, hf_tcp_window_size, offset + 14, 2, th.th_win);
-    proto_tree_add_item(tcp_tree, hf_tcp_checksum, offset + 16, 2, th.th_sum);
+    proto_tree_add_boolean(field_tree, hf_tcp_flags_cwr, tvb, offset + 13, 1, th.th_flags);
+    proto_tree_add_boolean(field_tree, hf_tcp_flags_ecn, tvb, offset + 13, 1, th.th_flags);
+    proto_tree_add_boolean(field_tree, hf_tcp_flags_urg, tvb, offset + 13, 1, th.th_flags);
+    proto_tree_add_boolean(field_tree, hf_tcp_flags_ack, tvb, offset + 13, 1, th.th_flags);
+    proto_tree_add_boolean(field_tree, hf_tcp_flags_push, tvb, offset + 13, 1, th.th_flags);
+    proto_tree_add_boolean(field_tree, hf_tcp_flags_reset, tvb, offset + 13, 1, th.th_flags);
+    proto_tree_add_boolean(field_tree, hf_tcp_flags_syn, tvb, offset + 13, 1, th.th_flags);
+    proto_tree_add_boolean(field_tree, hf_tcp_flags_fin, tvb, offset + 13, 1, th.th_flags);
+    proto_tree_add_uint(tcp_tree, hf_tcp_window_size, tvb, offset + 14, 2, th.th_win);
+    if (!pinfo->fragmented && len >= reported_len) {
+      /* The packet isn't part of a fragmented datagram and isn't
+         truncated, so we can checksum it.
+        XXX - make a bigger scatter-gather list once we do fragment
+        reassembly? */
+
+      /* Set up the fields of the pseudo-header. */
+      cksum_vec[0].ptr = pinfo->src.data;
+      cksum_vec[0].len = pinfo->src.len;
+      cksum_vec[1].ptr = pinfo->dst.data;
+      cksum_vec[1].len = pinfo->dst.len;
+      cksum_vec[2].ptr = (const guint8 *)&phdr;
+      switch (pinfo->src.type) {
+
+      case AT_IPv4:
+       phdr[0] = htonl((IP_PROTO_TCP<<16) + reported_len);
+       cksum_vec[2].len = 4;
+       break;
+
+      case AT_IPv6:
+        phdr[0] = htonl(reported_len);
+        phdr[1] = htonl(IP_PROTO_TCP);
+        cksum_vec[2].len = 8;
+        break;
+
+      default:
+        /* TCP runs only atop IPv4 and IPv6.... */
+        g_assert_not_reached();
+        break;
+      }
+      cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
+      cksum_vec[3].len = reported_len;
+      computed_cksum = in_cksum(&cksum_vec[0], 4);
+      if (computed_cksum == 0) {
+        proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
+           offset + 16, 2, th.th_sum, "Checksum: 0x%04x (correct)", th.th_sum);
+      } else {
+        proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
+           offset + 16, 2, th.th_sum,
+          "Checksum: 0x%04x (incorrect, should be 0x%04x)", th.th_sum,
+          in_cksum_shouldbe(th.th_sum, computed_cksum));
+      }
+    } else {
+      proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
+       offset + 16, 2, th.th_sum, "Checksum: 0x%04x", th.th_sum);
+    }
     if (th.th_flags & TH_URG)
-      proto_tree_add_item(tcp_tree, hf_tcp_urgent_pointer, offset + 18, 2, th.th_urp);
+      proto_tree_add_uint(tcp_tree, hf_tcp_urgent_pointer, tvb, offset + 18, 2, th.th_urp);
   }
 
   /* Decode TCP options, if any. */
@@ -487,89 +602,57 @@ dissect_tcp(const u_char *pd, int offset, frame_data *fd, proto_tree *tree) {
     /* There's more than just the fixed-length header.  Decode the
        options. */
     optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
-    tf = proto_tree_add_text(tcp_tree, offset +  20, optlen,
+    tf = proto_tree_add_text(tcp_tree, tvb, offset +  20, optlen,
       "Options: (%d bytes)", optlen);
     field_tree = proto_item_add_subtree(tf, ett_tcp_options);
-    dissect_ip_tcp_options(&pd[offset + 20], offset + 20, optlen,
-      tcpopts, N_TCP_OPTS, TCPOPT_EOL, field_tree);
+    dissect_ip_tcp_options(tvb, offset + 20, optlen,
+      tcpopts, N_TCP_OPTS, TCPOPT_EOL, pinfo->fd, field_tree);
   }
 
-  if (check_col(fd, COL_INFO))
-    col_add_str(fd, COL_INFO, info_str);
-
   /* Skip over header + options */
   offset += hlen;
 
-  pi.ptype = PT_TCP;
-  pi.srcport = th.th_sport;
-  pi.destport = th.th_dport;
+  pinfo->ptype = PT_TCP;
+  pinfo->srcport = th.th_sport;
+  pinfo->destport = th.th_dport;
   
   /* Check the packet length to see if there's more data
      (it could be an ACK-only packet) */
-  if (packet_max > offset) {
-
-    /* ONC RPC.  We can't base this on anything in the TCP header; we have
-       to look at the payload.  If "dissect_rpc()" returns TRUE, it was
-       an RPC packet, otherwise it's some other type of packet. */
-    if (dissect_rpc(pd, offset, fd, tree))
-      goto reas;
-
-    /* try to apply the plugins */
-#ifdef HAVE_PLUGINS
-    {
-      plugin *pt_plug = plugin_list;
-
-      if (pt_plug) {
-       while (pt_plug) {
-         if (pt_plug->enabled && !strcmp(pt_plug->protocol, "tcp") &&
-             tree && dfilter_apply(pt_plug->filter, tree, pd)) {
-           pt_plug->dissector(pd, offset, fd, tree);
-           goto reas;
-         }
-         pt_plug = pt_plug->next;
-       }
-      }
-    }
-#endif
-
-    /* do lookup with the subdissector table */
-    if (dissector_try_port(subdissector_table, th.th_sport, pd, offset,
-                               fd, tree) ||
-        dissector_try_port(subdissector_table, th.th_dport, pd, offset,
-                               fd, tree))
-       goto reas;
-
-    /* check existence of high level protocols */
-
-#define PORT_IS(port)   (th.th_sport == port || th.th_dport == port)    
-
-    if (memcmp(&pd[offset], "GIOP",  4) == 0) {
-       dissect_giop(pd, offset, fd, tree);
-       goto reas;
-    }
-
-    if ( PORT_IS(TCP_PORT_YHOO) && 
-               (memcmp(&pd[offset], "YPNS",  4) == 0 ||
-               memcmp(&pd[offset], "YHOO",  4) == 0 )) {
-       dissect_yhoo(pd, offset, fd, tree);
-       goto reas;
-    }
-
-    dissect_data(pd, offset, fd, tree);
+  length_remaining = tvb_length_remaining(tvb, offset);
+  if (length_remaining != 0) {
+    if (th.th_flags & TH_RST) {
+      /*
+       * RFC1122 says:
+       *
+       *       4.2.2.12  RST Segment: RFC-793 Section 3.4
+       *
+       *         A TCP SHOULD allow a received RST segment to include data.
+       *
+       *         DISCUSSION
+       *              It has been suggested that a RST segment could contain
+       *              ASCII text that encoded and explained the cause of the
+       *              RST.  No standard has yet been established for such
+       *              data.
+       *
+       * so for segments with RST we just display the data as text.
+       */
+      proto_tree_add_text(tcp_tree, tvb, offset, length_remaining,
+                           "Reset cause: %s",
+                           tvb_format_text(tvb, offset, length_remaining));
+    } else
+      decode_tcp_ports( tvb, offset, pinfo, tree, th.th_sport, th.th_dport);
   }
-
-reas:
  
   if( data_out_file ) {
     reassemble_tcp( th.th_seq,         /* sequence number */
-        ( pi.len - offset ),           /* data length */
-        ( pd+offset ),                 /* data */
-        ( pi.captured_len - offset ),  /* captured data length */
+        seglen,                                /* data length */
+        tvb_get_ptr(tvb, offset, length_remaining),    /* data */
+        length_remaining,              /* captured data length */
         ( th.th_flags & TH_SYN ),      /* is syn set? */
-        &pi.net_src,
-       &pi.net_dst,
-       pi.srcport,
-       pi.destport);
+        &pinfo->net_src,
+       &pinfo->net_dst,
+       pinfo->srcport,
+       pinfo->destport);
   }
 }
 
@@ -594,6 +677,10 @@ proto_register_tcp(void)
                { "Sequence number",            "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
                        "" }},
 
+               { &hf_tcp_nxtseq,
+               { "Next sequence number",       "tcp.nxtseq", FT_UINT32, BASE_DEC, NULL, 0x0,
+                       "" }},
+
                { &hf_tcp_ack,
                { "Acknowledgement number",     "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
                        "" }},
@@ -606,6 +693,14 @@ proto_register_tcp(void)
                { "Flags",                      "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
                        "" }},
 
+               { &hf_tcp_flags_cwr,
+               { "Congestion Window Reduced (CWR)",                    "tcp.flags.cwr", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_CWR,
+                       "" }},
+
+               { &hf_tcp_flags_ecn,
+               { "ECN-Echo",                   "tcp.flags.ecn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ECN,
+                       "" }},
+
                { &hf_tcp_flags_urg,
                { "Urgent",                     "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
                        "" }},
@@ -648,37 +743,27 @@ proto_register_tcp(void)
                &ett_tcp_options,
                &ett_tcp_option_sack,
        };
+       module_t *tcp_module;
 
-       proto_tcp = proto_register_protocol ("Transmission Control Protocol", "tcp");
+       proto_tcp = proto_register_protocol("Transmission Control Protocol",
+           "TCP", "tcp");
        proto_register_field_array(proto_tcp, hf, array_length(hf));
        proto_register_subtree_array(ett, array_length(ett));
 
-/* subdissector code */
-       subdissector_table = register_dissector_table(hf_tcp_port);
-
-       dissector_add( "tcp.port", TCP_PORT_PRINTER, &dissect_lpd);
-       dissector_add( "tcp.port", TCP_PORT_TELNET, &dissect_telnet);
-       dissector_add( "tcp.port", TCP_PORT_FTPDATA, &dissect_ftpdata);
-       dissector_add( "tcp.port", TCP_PORT_FTP, &dissect_ftp);
-       dissector_add( "tcp.port", TCP_PORT_POP, &dissect_pop);
-       dissector_add( "tcp.port", TCP_PORT_IMAP, &dissect_imap);
-       dissector_add( "tcp.port", TCP_PORT_NNTP, &dissect_nntp);
-       dissector_add( "tcp.port", TCP_PORT_NTP, &dissect_ntp);
-       dissector_add( "tcp.port", TCP_PORT_PPTP, &dissect_pptp);
-       dissector_add( "tcp.port", TCP_PORT_PPTP, &dissect_pptp);
-       dissector_add( "tcp.port", TCP_PORT_HTTP, &dissect_http);
-       dissector_add( "tcp.port", TCP_ALT_PORT_HTTP, &dissect_http);
-       dissector_add( "tcp.port", 631, &dissect_http); /* IPP */
-       dissector_add( "tcp.port", TCP_PORT_PROXY_HTTP, &dissect_http);
-       dissector_add( "tcp.port", TCP_PORT_PROXY_ADMIN_HTTP, &dissect_http);
-       dissector_add( "tcp.port", TCP_PORT_NBSS, &dissect_nbss);
-       dissector_add( "tcp.port", TCP_PORT_RTSP, &dissect_rtsp);
-       dissector_add( "tcp.port", TCP_PORT_BGP, &dissect_bgp);
-       dissector_add( "tcp.port", TCP_PORT_TACACS, &dissect_tacplus);
-       dissector_add( "tcp.port", TCP_PORT_MAPI, &dissect_mapi);
-       dissector_add( "tcp.port", TCP_PORT_TNS, &dissect_tns);
-       dissector_add( "tcp.port", TCP_PORT_IRC, &dissect_irc);
-       dissector_add( "tcp.port", TCP_PORT_LDAP, &dissect_ldap);
-       dissector_add( "tcp.port", TCP_PORT_SRVLOC, &dissect_srvloc);
-       dissector_add( "tcp.port", TCP_PORT_NCP, &dissect_ncp);
+       /* subdissector code */
+       subdissector_table = register_dissector_table("tcp.port");
+       register_heur_dissector_list("tcp", &heur_subdissector_list);
+
+       /* Register configuration preferences */
+       tcp_module = prefs_register_protocol(proto_tcp, NULL);
+       prefs_register_bool_preference(tcp_module, "tcp_summary_in_tree",
+           "Show TCP summary in protocol tree",
+"Whether the TCP summary line should be shown in the protocol tree",
+           &tcp_summary_in_tree);
+}
+
+void
+proto_reg_handoff_tcp(void)
+{
+       dissector_add("ip.proto", IP_PROTO_TCP, dissect_tcp);
 }