aboutsummaryrefslogtreecommitdiffstats
path: root/packet-tcp.c
diff options
context:
space:
mode:
Diffstat (limited to 'packet-tcp.c')
-rw-r--r--packet-tcp.c507
1 files changed, 472 insertions, 35 deletions
diff --git a/packet-tcp.c b/packet-tcp.c
index 547876df75..35cb968a89 100644
--- a/packet-tcp.c
+++ b/packet-tcp.c
@@ -1,7 +1,7 @@
/* packet-tcp.c
* Routines for TCP packet disassembly
*
- * $Id: packet-tcp.c,v 1.106 2001/09/03 17:57:17 guy Exp $
+ * $Id: packet-tcp.c,v 1.107 2001/09/13 07:53:51 guy Exp $
*
* Ethereal - Network traffic analyzer
* By Gerald Combs <gerald@ethereal.com>
@@ -51,6 +51,7 @@
#include "packet-ip.h"
#include "conversation.h"
#include "strutil.h"
+#include "reassemble.h"
/* Place TCP summary in proto tree */
static gboolean tcp_summary_in_tree = TRUE;
@@ -85,6 +86,7 @@ static gint ett_tcp = -1;
static gint ett_tcp_flags = -1;
static gint ett_tcp_options = -1;
static gint ett_tcp_option_sack = -1;
+static gint ett_tcp_segments = -1;
static dissector_table_t subdissector_table;
static heur_dissector_list_t heur_subdissector_list;
@@ -149,6 +151,410 @@ typedef struct _e_tcphdr {
#define TCPOLEN_CCECHO 6
#define TCPOLEN_MD5 18
+
+
+/* Desegmentation of TCP streams */
+/* table to hold defragmented TCP streams */
+static GHashTable *tcp_fragment_table = NULL;
+static void
+tcp_fragment_init(void)
+{
+ fragment_table_init(&tcp_fragment_table);
+}
+
+/* functions to trace tcp segments */
+/* Enable desegmenting of TCP streams */
+static gboolean tcp_desegment = FALSE;
+
+static GHashTable *tcp_segment_table = NULL;
+static GMemChunk *tcp_segment_key_chunk = NULL;
+static int tcp_segment_init_count = 200;
+
+typedef struct _tcp_segment_key {
+ /* for ouwn bookkeeping inside packet-tcp.c */
+ address *src;
+ address *dst;
+ guint32 seq;
+ /* xxx */
+ guint32 start_seq;
+ guint32 tot_len;
+ guint32 first_frame;
+} tcp_segment_key;
+
+static gboolean
+free_all_segments(gpointer key_arg, gpointer value, gpointer user_data)
+{
+ tcp_segment_key *key = key_arg;
+
+ if((key->src)&&(key->src->data)){
+ g_free((gpointer)key->src->data);
+ key->src->data=NULL;
+ g_free((gpointer)key->src);
+ key->src=NULL;
+ }
+ if((key->dst)&&(key->dst->data)){
+ g_free((gpointer)key->dst->data);
+ key->dst->data=NULL;
+ g_free((gpointer)key->dst);
+ key->dst=NULL;
+ }
+
+ return TRUE;
+}
+
+static guint
+tcp_segment_hash(gconstpointer k)
+{
+ tcp_segment_key *key = (tcp_segment_key *)k;
+
+ return key->seq;
+}
+
+static gint
+tcp_segment_equal(gconstpointer k1, gconstpointer k2)
+{
+ tcp_segment_key *key1 = (tcp_segment_key *)k1;
+ tcp_segment_key *key2 = (tcp_segment_key *)k2;
+
+ return ( ( (key1->seq==key2->seq)
+ &&(ADDRESSES_EQUAL(key1->src, key2->src))
+ &&(ADDRESSES_EQUAL(key1->dst, key2->dst))
+ ) ? TRUE:FALSE);
+}
+
+static void
+tcp_desegment_init(void)
+{
+
+ /* dont allocate any memory chunks unless the user really
+ uses this option
+ */
+ if(!tcp_desegment){
+ return;
+ }
+
+ if(tcp_segment_table){
+ g_hash_table_foreach_remove(tcp_segment_table,
+ free_all_segments, NULL);
+ } else {
+ tcp_segment_table = g_hash_table_new(tcp_segment_hash,
+ tcp_segment_equal);
+ }
+
+ if(tcp_segment_key_chunk){
+ g_mem_chunk_destroy(tcp_segment_key_chunk);
+ }
+ tcp_segment_key_chunk = g_mem_chunk_new("tcp_segment_key_chunk",
+ sizeof(tcp_segment_key),
+ tcp_segment_init_count*sizeof(tcp_segment_key),
+ G_ALLOC_ONLY);
+}
+
+static void
+desegment_tcp(tvbuff_t *tvb, packet_info *pinfo, int offset,
+ guint32 seq, guint32 nxtseq,
+ guint32 sport, guint32 dport,
+ proto_tree *tree, proto_tree *tcp_tree)
+{
+ fragment_data *ipfd_head;
+ tcp_segment_key old_tsk, *tsk;
+ gboolean must_desegment = FALSE;
+ gboolean called_dissector = FALSE;
+ int deseg_offset;
+
+ /*
+ * Initialize these to assume no desegmentation.
+ * If that's not the case, these will be set appropriately
+ * by the subdissector.
+ */
+ pinfo->desegment_offset = 0;
+ pinfo->desegment_len = 0;
+
+ /*
+ * Initialize this to assume that this segment will just be
+ * added to the middle of a desegmented chunk of data, so
+ * that we should show it all as data.
+ * If that's not the case, it will be set appropriately.
+ */
+ deseg_offset = offset;
+
+ /* First we must check if this TCP segment should be desegmented.
+ This is only to check if we should desegment this packet,
+ so we dont spend time doing COPY_ADDRESS/g_free.
+ We just "borrow" some address structures from pinfo instead. Cheaper.
+ */
+ old_tsk.src = &pinfo->src;
+ old_tsk.dst = &pinfo->dst;
+ old_tsk.seq = seq;
+ tsk = g_hash_table_lookup(tcp_segment_table, &old_tsk);
+
+ if(tsk){
+ /* OK, this segment was found, which means it continues
+ a higher-level PDU. This means we must desegment it.
+ Add it to the defragmentation lists.
+ */
+ ipfd_head = fragment_add(tvb, offset, pinfo, tsk->start_seq,
+ tcp_fragment_table,
+ seq - tsk->start_seq,
+ nxtseq - seq,
+ (nxtseq < (tsk->start_seq + tsk->tot_len)) );
+
+ if(!ipfd_head){
+ /* fragment_add() returned NULL, This means that
+ desegmentation is not completed yet.
+ (its like defragmentation but we know we will
+ always add the segments in order).
+ XXX - no, we don't; there is no guarantee that
+ TCP segments are in order on the wire.
+
+ we must add next segment to our table so we will
+ find it later.
+ */
+ tcp_segment_key *new_tsk;
+
+ new_tsk = g_mem_chunk_alloc(tcp_segment_key_chunk);
+ memcpy(new_tsk, tsk, sizeof(tcp_segment_key));
+ new_tsk->seq=nxtseq;
+ g_hash_table_insert(tcp_segment_table,new_tsk,new_tsk);
+ }
+ } else {
+ /* This segment was not found in our table, so it doesn't
+ contain a continuation of a higher-level PDU.
+ Call the normal subdissector.
+ */
+ decode_tcp_ports(tvb, offset, pinfo, tree,
+ sport, dport);
+ called_dissector = TRUE;
+
+ /*
+ * Advance the offset to the first byte that the
+ * subdissector didn't process.
+ */
+ offset += pinfo->desegment_offset;
+
+ /* Did the subdissector ask us to desegment some more data
+ before it could handle the packet?
+ If so we have to create some structures in our table but
+ this is something we only do the first time we see this
+ packet.
+ */
+ if(pinfo->desegment_len) {
+ if (!pinfo->fd->flags.visited)
+ must_desegment = TRUE;
+
+ /*
+ * Set "deseg_offset" to the offset in "tvb"
+ * of the first byte of data that the
+ * subdissector didn't process.
+ */
+ deseg_offset = offset;
+ }
+
+ /* Either no desegmentation is necessary, or this is
+ segment contains the beginning but not the end of
+ a higher-level PDU and thus isn't completely
+ desegmented.
+ */
+ ipfd_head = NULL;
+ }
+
+ /* is it completely desegmented? */
+ if(ipfd_head){
+ fragment_data *ipfd;
+ proto_tree *st = NULL;
+ proto_item *si = NULL;
+
+ /* first we show a tree with all segments */
+ si = proto_tree_add_text(tcp_tree, tvb, 0, 0,
+ "Segments");
+ st = proto_item_add_subtree(si, ett_tcp_segments);
+ for(ipfd=ipfd_head->next; ipfd; ipfd=ipfd->next){
+ proto_tree_add_text(st, tvb, 0, 0,
+ "Frame:%d seq#:%d-%d [%d-%d]",
+ ipfd->frame,
+ tsk->start_seq + ipfd->offset,
+ tsk->start_seq + ipfd->offset + ipfd->len - 1,
+ ipfd->offset,
+ ipfd->offset + ipfd->len - 1);
+ }
+
+ /*
+ * We only call subdissector for the last segment.
+ * Note that the last segment may include more than what
+ * we needed.
+ */
+ if(nxtseq >= (tsk->start_seq + tsk->tot_len)){
+ /* ok, lest call subdissector with desegmented data */
+ packet_info save_pi;
+ tvbuff_t *next_tvb;
+
+ /* create a new TVB structure for desegmented data */
+ next_tvb = tvb_new_real_data(ipfd_head->data,
+ ipfd_head->datalen, ipfd_head->datalen,
+ "Desegmented");
+
+ /* add this tvb as a child to the original one */
+ tvb_set_child_real_data_tvbuff(tvb, next_tvb);
+
+ /* add desegmented data to the data source list */
+ pinfo->fd->data_src = g_slist_append(pinfo->fd->data_src, next_tvb);
+
+ /* save current value of *pinfo across call to
+ dissector */
+ save_pi = *pinfo;
+ pinfo->compat_top_tvb = next_tvb;
+ pinfo->len = tvb_reported_length(next_tvb);
+ pinfo->captured_len = tvb_length(next_tvb);
+
+ /* call subdissector */
+ decode_tcp_ports(next_tvb, 0, pinfo, tree,
+ sport, dport);
+ called_dissector = TRUE;
+
+ /*
+ * Don't trash the new values of "desegment_offset"
+ * and "desegment_len".
+ */
+ save_pi.desegment_offset = pinfo->desegment_offset;
+ save_pi.desegment_len = pinfo->desegment_len;
+ *pinfo = save_pi;
+
+ /* Did the subdissector ask us to desegment some more
+ data? This means that the data at the beginning
+ of this segment completed a higher-level PDU,
+ but the data at the end of this segment started
+ a higher-level PDU but didn't complete it.
+
+ If so we have to create some structures in our
+ table but this is something we only do the first
+ time we see this packet.
+ */
+ if(pinfo->desegment_len) {
+ if (!pinfo->fd->flags.visited)
+ must_desegment = TRUE;
+
+ /*
+ * The stuff we couldn't dissect must have
+ * come from this segment, so it's all in
+ * "tvb".
+ *
+ * "pinfo->desegment_offset" is relative
+ * to the beginning of "next_tvb";
+ * we want an offset relative to the
+ * beginning of "tvb".
+ *
+ * First, compute the offset relative to
+ * the *end* of "next_tvb" - i.e., the number
+ * of bytes before the end of "next_tvb"
+ * at which the subdissector stopped.
+ * That's the length of "next_tvb" minus
+ * the offset, relative to the beginning
+ * of "next_tvb, at which the subdissector
+ * stopped.
+ */
+ deseg_offset =
+ ipfd_head->datalen - pinfo->desegment_offset;
+
+ /*
+ * "tvb" and "next_tvb" end at the same byte
+ * of data, so the offset relative to the
+ * end of "next_tvb" of the byte at which
+ * we stopped is also the offset relative
+ * to the end of "tvb" of the byte at which
+ * we stopped.
+ *
+ * Convert that back into an offset relative
+ * to the beginninng of "tvb", by taking
+ * the length of "tvb" and subtracting the
+ * offset relative to the end.
+ */
+ deseg_offset = tvb_length(tvb) - deseg_offset;
+ }
+ }
+ }
+
+ if (must_desegment) {
+ tcp_segment_key *tsk, *new_tsk;
+
+ /*
+ * XXX - how do we detect out-of-order transmissions?
+ * We can't just check for "nxtseq" being greater than
+ * "tsk->start_seq"; for now, we check for the difference
+ * being less than a megabyte, but this is a really
+ * gross hack - we really need to handle out-of-order
+ * transmissions correctly.
+ */
+ if ((nxtseq - (seq + pinfo->desegment_offset)) <= 1024*1024) {
+ /* OK, subdissector wants us to desegment
+ some data before it can process it. Add
+ what remains of this packet and set
+ up next packet/sequence number as well.
+
+ We must remember this segment
+ */
+ tsk = g_mem_chunk_alloc(tcp_segment_key_chunk);
+ tsk->src = g_malloc(sizeof(address));
+ COPY_ADDRESS(tsk->src, &pinfo->src);
+ tsk->dst = g_malloc(sizeof(address));
+ COPY_ADDRESS(tsk->dst, &pinfo->dst);
+ tsk->seq = seq + pinfo->desegment_offset;
+ tsk->start_seq = tsk->seq;
+ tsk->tot_len = nxtseq - tsk->start_seq + pinfo->desegment_len;
+ tsk->first_frame = pinfo->fd->num;
+ g_hash_table_insert(tcp_segment_table, tsk, tsk);
+
+ /* Add portion of segment unprocessed by the subdissector
+ to defragmentation lists */
+ fragment_add(tvb, deseg_offset, pinfo, tsk->start_seq,
+ tcp_fragment_table,
+ tsk->seq - tsk->start_seq,
+ nxtseq - tsk->start_seq,
+ (nxtseq < tsk->start_seq + tsk->tot_len));
+
+ /* this is the next segment in the sequence we want */
+ new_tsk = g_mem_chunk_alloc(tcp_segment_key_chunk);
+ memcpy(new_tsk, tsk, sizeof(tcp_segment_key));
+ new_tsk->seq = nxtseq;
+ g_hash_table_insert(tcp_segment_table,new_tsk,new_tsk);
+ }
+ }
+
+ if (!called_dissector || pinfo->desegment_len != 0) {
+ /*
+ * Either we didn't call the subdissector at all (i.e.,
+ * this is a segment that contains the middle of a
+ * higher-level PDU, but contains neither the beginning
+ * nor the end), or the subdissector couldn't dissect it
+ * all, as some data was missing (i.e., it set
+ * "pinfo->desegment_len" to the amount of additional
+ * data it needs).
+ */
+ if (pinfo->desegment_offset == 0) {
+ /*
+ * It couldn't, in fact, dissect any of it (the
+ * first byte it couldn't dissect is at an offset
+ * of "pinfo->desegment_offset" from the beginning
+ * of the payload, and that's 0).
+ * Just mark this as TCP.
+ */
+ if (check_col(pinfo->fd, COL_PROTOCOL)){
+ col_set_str(pinfo->fd, COL_PROTOCOL, "TCP");
+ }
+ if (check_col(pinfo->fd, COL_INFO)){
+ col_set_str(pinfo->fd, COL_INFO, "[Desegmented TCP]");
+ }
+ }
+
+ /*
+ * Show what's left in the packet as data.
+ */
+ dissect_data(tvb, deseg_offset, pinfo, tree);
+ }
+}
+
+
+
+
static void
tcp_info_append_uint(frame_data *fd, const char *abbrev, guint32 val)
{
@@ -537,57 +943,72 @@ dissect_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
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:
+ }
+
+ /*
+ * Assume, initially, that we can't desegment.
+ */
+ pinfo->can_desegment = FALSE;
+
+ 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:
+ case AT_IPv6:
phdr[0] = htonl(reported_len);
phdr[1] = htonl(IP_PROTO_TCP);
cksum_vec[2].len = 8;
break;
- default:
+ 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) {
+ /*
+ * We have all the data for this TCP segment, and the checksum of
+ * the header and the data is good, so we can desegment it.
+ * Is desegmentation enabled?
+ */
+ if (tcp_desegment) {
+ /* Yes - indicate that we will desegment. */
+ pinfo->can_desegment = TRUE;
}
- 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_boolean_hidden(tcp_tree, hf_tcp_checksum_bad, tvb,
+ 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_boolean_hidden(tcp_tree, hf_tcp_checksum_bad, tvb,
offset + 16, 2, TRUE);
- proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
+ 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_uint(tcp_tree, hf_tcp_urgent_pointer, tvb, offset + 18, 2, th.th_urp);
+ } 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_uint(tcp_tree, hf_tcp_urgent_pointer, tvb, offset + 18, 2, th.th_urp);
/* Decode TCP options, if any. */
if (tree && hlen > sizeof (e_tcphdr)) {
@@ -631,8 +1052,16 @@ dissect_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
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);
+ } else {
+ /* Can we desegment this segment? */
+ if (pinfo->can_desegment) {
+ /* Yes. */
+ desegment_tcp(tvb, pinfo, offset, th.th_seq, nxtseq, th.th_sport, th.th_dport, tree, tcp_tree);
+ } else {
+ /* No - just call the subdissector. */
+ decode_tcp_ports(tvb, offset, pinfo, tree, th.th_sport, th.th_dport);
+ }
+ }
}
if( data_out_file ) {
@@ -738,6 +1167,7 @@ proto_register_tcp(void)
&ett_tcp_flags,
&ett_tcp_options,
&ett_tcp_option_sack,
+ &ett_tcp_segments,
};
module_t *tcp_module;
@@ -757,6 +1187,13 @@ proto_register_tcp(void)
"Show TCP summary in protocol tree",
"Whether the TCP summary line should be shown in the protocol tree",
&tcp_summary_in_tree);
+ prefs_register_bool_preference(tcp_module, "desegment_tcp_streams",
+ "Allow subdissector to desegment TCP streams",
+"Whether subdissector can request TCP streams to be desegmented",
+ &tcp_desegment);
+
+ register_init_routine(tcp_desegment_init);
+ register_init_routine(tcp_fragment_init);
}
void