Support for reassembly of DCERPC over SMB, from Ronnie Sahlberg.
[obnox/wireshark/wip.git] / packet-tcp.c
1 /* packet-tcp.c
2  * Routines for TCP packet disassembly
3  *
4  * $Id: packet-tcp.c,v 1.121 2001/12/05 08:20:30 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  * 
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #ifdef HAVE_SYS_TYPES_H
30 # include <sys/types.h>
31 #endif
32
33 #ifdef HAVE_NETINET_IN_H
34 # include <netinet/in.h>
35 #endif
36
37 #include <stdio.h>
38 #include <string.h>
39 #include <glib.h>
40 #include "in_cksum.h"
41
42 #ifdef NEED_SNPRINTF_H
43 # include "snprintf.h"
44 #endif
45
46 #include "resolv.h"
47 #include "ipproto.h"
48 #include "follow.h"
49 #include "prefs.h"
50 #include "packet-tcp.h"
51 #include "packet-ip.h"
52 #include "conversation.h"
53 #include "strutil.h"
54 #include "reassemble.h"
55
56 /* Place TCP summary in proto tree */
57 static gboolean tcp_summary_in_tree = TRUE;
58
59 extern FILE* data_out_file;
60
61 static int proto_tcp = -1;
62 static int hf_tcp_srcport = -1;
63 static int hf_tcp_dstport = -1;
64 static int hf_tcp_port = -1;
65 static int hf_tcp_seq = -1;
66 static int hf_tcp_nxtseq = -1;
67 static int hf_tcp_ack = -1;
68 static int hf_tcp_hdr_len = -1;
69 static int hf_tcp_flags = -1;
70 static int hf_tcp_flags_cwr = -1;
71 static int hf_tcp_flags_ecn = -1;
72 static int hf_tcp_flags_urg = -1;
73 static int hf_tcp_flags_ack = -1;
74 static int hf_tcp_flags_push = -1;
75 static int hf_tcp_flags_reset = -1;
76 static int hf_tcp_flags_syn = -1;
77 static int hf_tcp_flags_fin = -1;
78 static int hf_tcp_window_size = -1;
79 static int hf_tcp_checksum = -1;
80 static int hf_tcp_checksum_bad = -1;
81 static int hf_tcp_urgent_pointer = -1;
82
83 static gint ett_tcp = -1;
84 static gint ett_tcp_flags = -1;
85 static gint ett_tcp_options = -1;
86 static gint ett_tcp_option_sack = -1;
87 static gint ett_tcp_segments = -1;
88
89 static dissector_table_t subdissector_table;
90 static heur_dissector_list_t heur_subdissector_list;
91 static dissector_handle_t data_handle;
92
93 /* TCP structs and definitions */
94
95 #define TH_FIN  0x01
96 #define TH_SYN  0x02
97 #define TH_RST  0x04
98 #define TH_PUSH 0x08
99 #define TH_ACK  0x10
100 #define TH_URG  0x20
101 #define TH_ECN  0x40
102 #define TH_CWR  0x80
103
104 /* Minimum TCP header length. */
105 #define TCPH_MIN_LEN    20
106
107 /*
108  *      TCP option
109  */
110  
111 #define TCPOPT_NOP              1       /* Padding */
112 #define TCPOPT_EOL              0       /* End of options */
113 #define TCPOPT_MSS              2       /* Segment size negotiating */
114 #define TCPOPT_WINDOW           3       /* Window scaling */
115 #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
116 #define TCPOPT_SACK             5       /* SACK Block */
117 #define TCPOPT_ECHO             6
118 #define TCPOPT_ECHOREPLY        7
119 #define TCPOPT_TIMESTAMP        8       /* Better RTT estimations/PAWS */
120 #define TCPOPT_CC               11
121 #define TCPOPT_CCNEW            12
122 #define TCPOPT_CCECHO           13
123 #define TCPOPT_MD5              19      /* RFC2385 */
124
125 /*
126  *     TCP option lengths
127  */
128
129 #define TCPOLEN_MSS            4
130 #define TCPOLEN_WINDOW         3
131 #define TCPOLEN_SACK_PERM      2
132 #define TCPOLEN_SACK_MIN       2
133 #define TCPOLEN_ECHO           6
134 #define TCPOLEN_ECHOREPLY      6
135 #define TCPOLEN_TIMESTAMP      10
136 #define TCPOLEN_CC             6
137 #define TCPOLEN_CCNEW          6
138 #define TCPOLEN_CCECHO         6
139 #define TCPOLEN_MD5            18
140
141
142
143 /* Desegmentation of TCP streams */
144 /* table to hold defragmented TCP streams */
145 static GHashTable *tcp_fragment_table = NULL;
146 static void
147 tcp_fragment_init(void)
148 {
149         fragment_table_init(&tcp_fragment_table);
150 }
151
152 /* functions to trace tcp segments */
153 /* Enable desegmenting of TCP streams */
154 static gboolean tcp_desegment = FALSE;
155
156 static GHashTable *tcp_segment_table = NULL;
157 static GMemChunk *tcp_segment_key_chunk = NULL;
158 static int tcp_segment_init_count = 200;
159 static GMemChunk *tcp_segment_address_chunk = NULL;
160 static int tcp_segment_address_init_count = 500;
161
162 typedef struct _tcp_segment_key {
163         /* for ouwn bookkeeping inside packet-tcp.c */
164         address *src;
165         address *dst;
166         guint32 seq;
167         /* xxx */
168         guint32 start_seq;
169         guint32 tot_len;
170         guint32 first_frame;
171 } tcp_segment_key;
172
173 static gboolean
174 free_all_segments(gpointer key_arg, gpointer value, gpointer user_data)
175 {
176         tcp_segment_key *key = key_arg;
177
178         if((key->src)&&(key->src->data)){
179                 g_free((gpointer)key->src->data);
180                 key->src->data=NULL;
181         }
182
183         if((key->dst)&&(key->dst->data)){
184                 g_free((gpointer)key->dst->data);
185                 key->dst->data=NULL;
186         }
187
188         return TRUE;
189 }
190
191 static guint
192 tcp_segment_hash(gconstpointer k)
193 {
194         tcp_segment_key *key = (tcp_segment_key *)k;
195
196         return key->seq;
197 }
198
199 static gint
200 tcp_segment_equal(gconstpointer k1, gconstpointer k2)
201 {
202         tcp_segment_key *key1 = (tcp_segment_key *)k1;
203         tcp_segment_key *key2 = (tcp_segment_key *)k2;
204
205         return ( ( (key1->seq==key2->seq)
206                  &&(ADDRESSES_EQUAL(key1->src, key2->src))
207                  &&(ADDRESSES_EQUAL(key1->dst, key2->dst))
208                  ) ? TRUE:FALSE);
209 }
210
211 static void
212 tcp_desegment_init(void)
213 {
214
215         /* dont allocate any memory chunks unless the user really
216            uses this option
217         */
218         if(!tcp_desegment){
219                 return;
220         }
221
222         if(tcp_segment_table){
223                 g_hash_table_foreach_remove(tcp_segment_table,
224                         free_all_segments, NULL);
225         } else {
226                 tcp_segment_table = g_hash_table_new(tcp_segment_hash,
227                         tcp_segment_equal);
228         }
229
230         if(tcp_segment_key_chunk){
231                 g_mem_chunk_destroy(tcp_segment_key_chunk);
232         }
233         tcp_segment_key_chunk = g_mem_chunk_new("tcp_segment_key_chunk",
234                 sizeof(tcp_segment_key),
235                 tcp_segment_init_count*sizeof(tcp_segment_key),
236                 G_ALLOC_ONLY);
237
238         if(tcp_segment_address_chunk){
239                 g_mem_chunk_destroy(tcp_segment_address_chunk);
240         }
241         tcp_segment_address_chunk = g_mem_chunk_new("tcp_segment_address_chunk",
242                 sizeof(address),
243                 tcp_segment_address_init_count*sizeof(address),
244                 G_ALLOC_ONLY);
245 }
246
247 static void
248 desegment_tcp(tvbuff_t *tvb, packet_info *pinfo, int offset,
249                 guint32 seq, guint32 nxtseq,
250                 guint32 sport, guint32 dport,
251                 proto_tree *tree, proto_tree *tcp_tree)
252 {
253         struct tcpinfo *tcpinfo = pinfo->private_data;
254         fragment_data *ipfd_head;
255         tcp_segment_key old_tsk, *tsk;
256         gboolean must_desegment = FALSE;
257         gboolean called_dissector = FALSE;
258         int deseg_offset;
259         guint32 deseg_seq;
260
261         /*
262          * Initialize these to assume no desegmentation.
263          * If that's not the case, these will be set appropriately
264          * by the subdissector.
265          */
266         pinfo->desegment_offset = 0;
267         pinfo->desegment_len = 0;
268
269         /*
270          * Initialize this to assume that this segment will just be
271          * added to the middle of a desegmented chunk of data, so
272          * that we should show it all as data.
273          * If that's not the case, it will be set appropriately.
274          */
275         deseg_offset = offset;
276
277         /* First we must check if this TCP segment should be desegmented.
278            This is only to check if we should desegment this packet,
279            so we dont spend time doing COPY_ADDRESS/g_free.
280            We just "borrow" some address structures from pinfo instead. Cheaper.
281         */
282         old_tsk.src = &pinfo->src;
283         old_tsk.dst = &pinfo->dst;
284         old_tsk.seq = seq;
285         tsk = g_hash_table_lookup(tcp_segment_table, &old_tsk);
286
287         if(tsk){
288                 /* OK, this segment was found, which means it continues
289                    a higher-level PDU. This means we must desegment it.
290                    Add it to the defragmentation lists.
291                 */
292                 ipfd_head = fragment_add(tvb, offset, pinfo, tsk->start_seq,
293                         tcp_fragment_table,
294                         seq - tsk->start_seq,
295                         nxtseq - seq,
296                         (nxtseq < (tsk->start_seq + tsk->tot_len)) );
297
298                 if(!ipfd_head){
299                         /* fragment_add() returned NULL, This means that 
300                            desegmentation is not completed yet.
301                            (its like defragmentation but we know we will
302                             always add the segments in order).
303                            XXX - no, we don't; there is no guarantee that
304                            TCP segments are in order on the wire.
305
306                            we must add next segment to our table so we will
307                            find it later.
308                         */
309                         tcp_segment_key *new_tsk;
310
311                         new_tsk = g_mem_chunk_alloc(tcp_segment_key_chunk);
312                         memcpy(new_tsk, tsk, sizeof(tcp_segment_key));
313                         new_tsk->seq=nxtseq;
314                         g_hash_table_insert(tcp_segment_table,new_tsk,new_tsk);
315                 }
316         } else {
317                 /* This segment was not found in our table, so it doesn't
318                    contain a continuation of a higher-level PDU.
319                    Call the normal subdissector.
320                 */
321                 decode_tcp_ports(tvb, offset, pinfo, tree, 
322                                 sport, dport);
323                 called_dissector = TRUE;
324
325                 /* Did the subdissector ask us to desegment some more data
326                    before it could handle the packet? 
327                    If so we have to create some structures in our table but
328                    this is something we only do the first time we see this 
329                    packet.
330                 */
331                 if(pinfo->desegment_len) {
332                         if (!pinfo->fd->flags.visited)
333                                 must_desegment = TRUE;
334
335                         /*
336                          * Set "deseg_offset" to the offset in "tvb"
337                          * of the first byte of data that the
338                          * subdissector didn't process.
339                          */
340                         deseg_offset = offset + pinfo->desegment_offset;
341                 }
342
343                 /* Either no desegmentation is necessary, or this is
344                    segment contains the beginning but not the end of
345                    a higher-level PDU and thus isn't completely
346                    desegmented.
347                 */
348                 ipfd_head = NULL;
349         }
350
351         /* is it completely desegmented? */
352         if(ipfd_head){
353                 fragment_data *ipfd;
354                 proto_tree *st = NULL;
355                 proto_item *si = NULL;
356
357                 /* first we show a tree with all segments */
358                 si = proto_tree_add_text(tcp_tree, tvb, 0, 0,
359                                 "Segments");
360                 st = proto_item_add_subtree(si, ett_tcp_segments);
361                 for(ipfd=ipfd_head->next; ipfd; ipfd=ipfd->next){
362                         proto_tree_add_text(st, tvb, 0, 0,
363                                 "Frame:%d  seq#:%d-%d [%d-%d]",
364                                 ipfd->frame,
365                                 tsk->start_seq + ipfd->offset,
366                                 tsk->start_seq + ipfd->offset + ipfd->len - 1,
367                                 ipfd->offset,
368                                 ipfd->offset + ipfd->len - 1); 
369                 }
370
371                 /*
372                  * We only call subdissector for the last segment.
373                  * Note that the last segment may include more than what
374                  * we needed.
375                  */
376                 if(nxtseq >= (tsk->start_seq + tsk->tot_len)){
377                         /* ok, lest call subdissector with desegmented data */
378                         tvbuff_t *next_tvb;
379
380                         /* create a new TVB structure for desegmented data */
381                         next_tvb = tvb_new_real_data(ipfd_head->data,
382                                         ipfd_head->datalen, ipfd_head->datalen,
383                                         "Desegmented");
384
385                         /* add this tvb as a child to the original one */
386                         tvb_set_child_real_data_tvbuff(tvb, next_tvb);
387
388                         /* add desegmented data to the data source list */
389                         pinfo->fd->data_src = g_slist_append(pinfo->fd->data_src, next_tvb);
390
391                         /* indicate that this is reassembled data */
392                         tcpinfo->is_reassembled = TRUE;
393
394                         /* call subdissector */
395                         decode_tcp_ports(next_tvb, 0, pinfo, tree,
396                                 sport, dport);
397                         called_dissector = TRUE;
398
399                         /* Did the subdissector ask us to desegment some more
400                            data?  This means that the data at the beginning
401                            of this segment completed a higher-level PDU,
402                            but the data at the end of this segment started
403                            a higher-level PDU but didn't complete it.
404
405                            If so we have to create some structures in our
406                            table but this is something we only do the first
407                            time we see this packet.
408                         */
409                         if(pinfo->desegment_len) {
410                                 if (!pinfo->fd->flags.visited)
411                                         must_desegment = TRUE;
412
413                                 /*
414                                  * The stuff we couldn't dissect must have
415                                  * come from this segment, so it's all in
416                                  * "tvb".
417                                  *
418                                  * "pinfo->desegment_offset" is relative
419                                  * to the beginning of "next_tvb";
420                                  * we want an offset relative to the
421                                  * beginning of "tvb".
422                                  *
423                                  * First, compute the offset relative to
424                                  * the *end* of "next_tvb" - i.e., the number
425                                  * of bytes before the end of "next_tvb"
426                                  * at which the subdissector stopped.
427                                  * That's the length of "next_tvb" minus
428                                  * the offset, relative to the beginning
429                                  * of "next_tvb, at which the subdissector
430                                  * stopped.
431                                  */
432                                 deseg_offset =
433                                     ipfd_head->datalen - pinfo->desegment_offset;
434
435                                 /*
436                                  * "tvb" and "next_tvb" end at the same byte
437                                  * of data, so the offset relative to the
438                                  * end of "next_tvb" of the byte at which
439                                  * we stopped is also the offset relative
440                                  * to the end of "tvb" of the byte at which
441                                  * we stopped.
442                                  *
443                                  * Convert that back into an offset relative
444                                  * to the beginninng of "tvb", by taking
445                                  * the length of "tvb" and subtracting the
446                                  * offset relative to the end.
447                                  */
448                                 deseg_offset = tvb_length(tvb) - deseg_offset;
449                         }
450                 }
451         }
452
453         if (must_desegment) {
454             tcp_segment_key *tsk, *new_tsk;
455
456             /*
457              * The sequence number at which the stuff to be desegmented
458              * starts is the sequence number of the byte at an offset
459              * of "deseg_offset" into "tvb".
460              *
461              * The sequence number of the byte at an offset of "offset"
462              * is "seq", i.e. the starting sequence number of this
463              * segment, so the sequence number of the byte at
464              * "deseg_offset" is "seq + (deseg_offset - offset)".
465              */
466             deseg_seq = seq + (deseg_offset - offset);
467
468             /*
469              * XXX - how do we detect out-of-order transmissions?
470              * We can't just check for "nxtseq" being greater than
471              * "tsk->start_seq"; for now, we check for the difference
472              * being less than a megabyte, but this is a really
473              * gross hack - we really need to handle out-of-order
474              * transmissions correctly.
475              */
476             if ((nxtseq - deseg_seq) <= 1024*1024) {
477                 /* OK, subdissector wants us to desegment
478                    some data before it can process it. Add
479                    what remains of this packet and set
480                    up next packet/sequence number as well.
481
482                    We must remember this segment
483                 */
484                 tsk = g_mem_chunk_alloc(tcp_segment_key_chunk);
485                 tsk->src = g_mem_chunk_alloc(tcp_segment_address_chunk);
486                 COPY_ADDRESS(tsk->src, &pinfo->src);
487                 tsk->dst = g_mem_chunk_alloc(tcp_segment_address_chunk);
488                 COPY_ADDRESS(tsk->dst, &pinfo->dst);
489                 tsk->seq = deseg_seq;
490                 tsk->start_seq = tsk->seq;
491                 tsk->tot_len = nxtseq - tsk->start_seq + pinfo->desegment_len;
492                 tsk->first_frame = pinfo->fd->num;
493                 g_hash_table_insert(tcp_segment_table, tsk, tsk);
494
495                 /* Add portion of segment unprocessed by the subdissector
496                    to defragmentation lists */
497                 fragment_add(tvb, deseg_offset, pinfo, tsk->start_seq,
498                     tcp_fragment_table,
499                     tsk->seq - tsk->start_seq,
500                     nxtseq - tsk->start_seq,
501                     (nxtseq < tsk->start_seq + tsk->tot_len));
502
503                 /* this is the next segment in the sequence we want */
504                 new_tsk = g_mem_chunk_alloc(tcp_segment_key_chunk);
505                 memcpy(new_tsk, tsk, sizeof(tcp_segment_key));
506                 new_tsk->seq = nxtseq;
507                 g_hash_table_insert(tcp_segment_table,new_tsk,new_tsk);
508             }
509         }
510
511         if (!called_dissector || pinfo->desegment_len != 0) {
512                 /*
513                  * Either we didn't call the subdissector at all (i.e.,
514                  * this is a segment that contains the middle of a
515                  * higher-level PDU, but contains neither the beginning
516                  * nor the end), or the subdissector couldn't dissect it
517                  * all, as some data was missing (i.e., it set
518                  * "pinfo->desegment_len" to the amount of additional
519                  * data it needs).
520                  */
521                 if (pinfo->desegment_offset == 0) {
522                         /*
523                          * It couldn't, in fact, dissect any of it (the
524                          * first byte it couldn't dissect is at an offset
525                          * of "pinfo->desegment_offset" from the beginning
526                          * of the payload, and that's 0).
527                          * Just mark this as TCP.
528                          */
529                         if (check_col(pinfo->fd, COL_PROTOCOL)){
530                                 col_set_str(pinfo->fd, COL_PROTOCOL, "TCP");
531                         }
532                         if (check_col(pinfo->fd, COL_INFO)){
533                                 col_set_str(pinfo->fd, COL_INFO, "[Desegmented TCP]");
534                         }
535                 }
536
537                 /*
538                  * Show what's left in the packet as data.
539                  */
540                 call_dissector(data_handle,tvb_new_subset(tvb, deseg_offset,-1,tvb_reported_length_remaining(tvb,deseg_offset)), pinfo, tree);
541         }
542         pinfo->can_desegment=0;
543         pinfo->desegment_offset = 0;
544         pinfo->desegment_len = 0;
545 }
546
547
548
549
550 static void
551 tcp_info_append_uint(frame_data *fd, const char *abbrev, guint32 val)
552 {
553   if (check_col(fd, COL_INFO))
554     col_append_fstr(fd, COL_INFO, " %s=%u", abbrev, val);
555 }
556
557 static void
558 dissect_tcpopt_maxseg(const ip_tcp_opt *optp, tvbuff_t *tvb,
559     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
560 {
561   guint16 mss;
562
563   mss = tvb_get_ntohs(tvb, offset + 2);
564   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
565                         "%s: %u bytes", optp->name, mss);
566   tcp_info_append_uint(fd, "MSS", mss);
567 }
568
569 static void
570 dissect_tcpopt_wscale(const ip_tcp_opt *optp, tvbuff_t *tvb,
571     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
572 {
573   guint8 ws;
574
575   ws = tvb_get_guint8(tvb, offset + 2);
576   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
577                         "%s: %u bytes", optp->name, ws);
578   tcp_info_append_uint(fd, "WS", ws);
579 }
580
581 static void
582 dissect_tcpopt_sack(const ip_tcp_opt *optp, tvbuff_t *tvb,
583     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
584 {
585   proto_tree *field_tree = NULL;
586   proto_item *tf;
587   guint leftedge, rightedge;
588
589   tf = proto_tree_add_text(opt_tree, tvb, offset,      optlen, "%s:", optp->name);
590   offset += 2;  /* skip past type and length */
591   optlen -= 2;  /* subtract size of type and length */
592   while (optlen > 0) {
593     if (field_tree == NULL) {
594       /* Haven't yet made a subtree out of this option.  Do so. */
595       field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
596     }
597     if (optlen < 4) {
598       proto_tree_add_text(field_tree, tvb, offset,      optlen,
599         "(suboption would go past end of option)");
600       break;
601     }
602     leftedge = tvb_get_ntohl(tvb, offset);
603     optlen -= 4;
604     if (optlen < 4) {
605       proto_tree_add_text(field_tree, tvb, offset,      optlen,
606         "(suboption would go past end of option)");
607       break;
608     }
609     /* XXX - check whether it goes past end of packet */
610     rightedge = tvb_get_ntohl(tvb, offset + 4);
611     optlen -= 4;
612     proto_tree_add_text(field_tree, tvb, offset,      8,
613         "left edge = %u, right edge = %u", leftedge, rightedge);
614     tcp_info_append_uint(fd, "SLE", leftedge);
615     tcp_info_append_uint(fd, "SRE", rightedge);
616     offset += 8;
617   }
618 }
619
620 static void
621 dissect_tcpopt_echo(const ip_tcp_opt *optp, tvbuff_t *tvb,
622     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
623 {
624   guint32 echo;
625
626   echo = tvb_get_ntohl(tvb, offset + 2);
627   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
628                         "%s: %u", optp->name, echo);
629   tcp_info_append_uint(fd, "ECHO", echo);
630 }
631
632 static void
633 dissect_tcpopt_timestamp(const ip_tcp_opt *optp, tvbuff_t *tvb,
634     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
635 {
636   guint32 tsv, tser;
637
638   tsv = tvb_get_ntohl(tvb, offset + 2);
639   tser = tvb_get_ntohl(tvb, offset + 6);
640   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
641     "%s: tsval %u, tsecr %u", optp->name, tsv, tser);
642   tcp_info_append_uint(fd, "TSV", tsv);
643   tcp_info_append_uint(fd, "TSER", tser);
644 }
645
646 static void
647 dissect_tcpopt_cc(const ip_tcp_opt *optp, tvbuff_t *tvb,
648     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
649 {
650   guint32 cc;
651
652   cc = tvb_get_ntohl(tvb, offset + 2);
653   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
654                         "%s: %u", optp->name, cc);
655   tcp_info_append_uint(fd, "CC", cc);
656 }
657
658 static const ip_tcp_opt tcpopts[] = {
659   {
660     TCPOPT_EOL,
661     "EOL",
662     NULL,
663     NO_LENGTH,
664     0,
665     NULL,
666   },
667   {
668     TCPOPT_NOP,
669     "NOP",
670     NULL,
671     NO_LENGTH,
672     0,
673     NULL,
674   },
675   {
676     TCPOPT_MSS,
677     "Maximum segment size",
678     NULL,
679     FIXED_LENGTH,
680     TCPOLEN_MSS,
681     dissect_tcpopt_maxseg
682   },
683   {
684     TCPOPT_WINDOW,
685     "Window scale",
686     NULL,
687     FIXED_LENGTH,
688     TCPOLEN_WINDOW,
689     dissect_tcpopt_wscale
690   },
691   {
692     TCPOPT_SACK_PERM,
693     "SACK permitted",
694     NULL,
695     FIXED_LENGTH,
696     TCPOLEN_SACK_PERM,
697     NULL,
698   },
699   {
700     TCPOPT_SACK,
701     "SACK",
702     &ett_tcp_option_sack,
703     VARIABLE_LENGTH,
704     TCPOLEN_SACK_MIN,
705     dissect_tcpopt_sack
706   },
707   {
708     TCPOPT_ECHO,
709     "Echo",
710     NULL,
711     FIXED_LENGTH,
712     TCPOLEN_ECHO,
713     dissect_tcpopt_echo
714   },
715   {
716     TCPOPT_ECHOREPLY,
717     "Echo reply",
718     NULL,
719     FIXED_LENGTH,
720     TCPOLEN_ECHOREPLY,
721     dissect_tcpopt_echo
722   },
723   {
724     TCPOPT_TIMESTAMP,
725     "Time stamp",
726     NULL,
727     FIXED_LENGTH,
728     TCPOLEN_TIMESTAMP,
729     dissect_tcpopt_timestamp
730   },
731   {
732     TCPOPT_CC,
733     "CC",
734     NULL,
735     FIXED_LENGTH,
736     TCPOLEN_CC,
737     dissect_tcpopt_cc
738   },
739   {
740     TCPOPT_CCNEW,
741     "CC.NEW",
742     NULL,
743     FIXED_LENGTH,
744     TCPOLEN_CCNEW,
745     dissect_tcpopt_cc
746   },
747   {
748     TCPOPT_CCECHO,
749     "CC.ECHO",
750     NULL,
751     FIXED_LENGTH,
752     TCPOLEN_CCECHO,
753     dissect_tcpopt_cc
754   },
755   {
756     TCPOPT_MD5,
757     "TCP MD5 signature",
758     NULL,
759     FIXED_LENGTH,
760     TCPOLEN_MD5,
761     NULL
762   }
763 };
764
765 #define N_TCP_OPTS      (sizeof tcpopts / sizeof tcpopts[0])
766
767 /* TCP flags flag */
768 static const true_false_string flags_set_truth = {
769   "Set",
770   "Not set"
771 };
772
773
774 /* Determine if there is a sub-dissector and call it.  This has been */
775 /* separated into a stand alone routine to other protocol dissectors */
776 /* can call to it, ie. socks    */
777
778 void
779 decode_tcp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
780         proto_tree *tree, int src_port, int dst_port)
781 {
782   tvbuff_t *next_tvb;
783
784   next_tvb = tvb_new_subset(tvb, offset, -1, -1);
785
786 /* determine if this packet is part of a conversation and call dissector */
787 /* for the conversation if available */
788
789   if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_TCP,
790                 src_port, dst_port, next_tvb, pinfo, tree))
791     return;
792
793   /* do lookup with the subdissector table */
794   if (dissector_try_port(subdissector_table, src_port, next_tvb, pinfo, tree) ||
795       dissector_try_port(subdissector_table, dst_port, next_tvb, pinfo, tree))
796     return;
797
798   /* do lookup with the heuristic subdissector table */
799   if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
800     return;
801
802   /* Oh, well, we don't know this; dissect it as data. */
803   call_dissector(data_handle,next_tvb, pinfo, tree);
804 }
805
806
807 static void
808 dissect_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
809 {
810   guint16 th_sport;
811   guint16 th_dport;
812   guint32 th_seq;
813   guint32 th_ack;
814   guint8  th_off_x2; /* combines th_off and th_x2 */
815   guint8  th_flags;
816   guint16 th_win;
817   guint16 th_sum;
818   guint16 th_urp;
819   proto_tree *tcp_tree = NULL, *field_tree = NULL;
820   proto_item *ti = NULL, *tf;
821   int        offset = 0;
822   gchar      flags[64] = "<None>";
823   gchar     *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG", "ECN", "CWR" };
824   gint       fpos = 0, i;
825   guint      bpos;
826   guint      hlen;
827   guint      optlen;
828   guint32    seglen;
829   guint32    nxtseq;
830   guint      len;
831   guint      reported_len;
832   vec_t      cksum_vec[4];
833   guint32    phdr[2];
834   guint16    computed_cksum;
835   guint      length_remaining;
836   struct tcpinfo tcpinfo;
837
838   if (check_col(pinfo->fd, COL_PROTOCOL))
839     col_set_str(pinfo->fd, COL_PROTOCOL, "TCP");
840
841   /* Clear out the Info column. */
842   if (check_col(pinfo->fd, COL_INFO))
843     col_clear(pinfo->fd, COL_INFO);
844
845   th_sport = tvb_get_ntohs(tvb, offset);
846   th_dport = tvb_get_ntohs(tvb, offset + 2);
847   if (check_col(pinfo->fd, COL_INFO)) {
848     col_append_fstr(pinfo->fd, COL_INFO, "%s > %s",
849       get_tcp_port(th_sport), get_tcp_port(th_dport));
850   }
851   
852   if (tree) {
853     if (tcp_summary_in_tree) {
854             ti = proto_tree_add_protocol_format(tree, proto_tcp, tvb, 0,
855                 tvb_length(tvb),
856                 "Transmission Control Protocol, Src Port: %s (%u), Dst Port: %s (%u)",
857                 get_tcp_port(th_sport), th_sport,
858                 get_tcp_port(th_dport), th_dport);
859     }
860     else {
861             ti = proto_tree_add_item(tree, proto_tcp, tvb, 0,
862                 tvb_length(tvb), FALSE);
863     }
864     tcp_tree = proto_item_add_subtree(ti, ett_tcp);
865     proto_tree_add_uint_format(tcp_tree, hf_tcp_srcport, tvb, offset, 2, th_sport,
866         "Source port: %s (%u)", get_tcp_port(th_sport), th_sport);
867     proto_tree_add_uint_format(tcp_tree, hf_tcp_dstport, tvb, offset + 2, 2, th_dport,
868         "Destination port: %s (%u)", get_tcp_port(th_dport), th_dport);
869     proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset, 2, th_sport);
870     proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset + 2, 2, th_dport);
871   }
872
873   th_seq = tvb_get_ntohl(tvb, offset + 4);
874   th_ack = tvb_get_ntohl(tvb, offset + 8);
875   th_off_x2 = tvb_get_guint8(tvb, offset + 12);
876   th_flags = tvb_get_guint8(tvb, offset + 13);
877   th_win = tvb_get_ntohs(tvb, offset + 14);
878   
879   if (check_col(pinfo->fd, COL_INFO) || tree) {  
880     for (i = 0; i < 8; i++) {
881       bpos = 1 << i;
882       if (th_flags & bpos) {
883         if (fpos) {
884           strcpy(&flags[fpos], ", ");
885           fpos += 2;
886         }
887         strcpy(&flags[fpos], fstr[i]);
888         fpos += 3;
889       }
890     }
891     flags[fpos] = '\0';
892   }
893
894   if (check_col(pinfo->fd, COL_INFO)) {
895     col_append_fstr(pinfo->fd, COL_INFO, " [%s] Seq=%u Ack=%u Win=%u",
896       flags, th_seq, th_ack, th_win);
897   }
898
899   if (tree) {
900     if (tcp_summary_in_tree)
901       proto_item_append_text(ti, ", Seq: %u", th_seq);
902     proto_tree_add_uint(tcp_tree, hf_tcp_seq, tvb, offset + 4, 4, th_seq);
903   }
904
905   hlen = hi_nibble(th_off_x2) * 4;  /* TCP header length, in bytes */
906
907   if (hlen < TCPH_MIN_LEN) {
908     /* Give up at this point; we put the source and destination port in
909        the tree, before fetching the header length, so that they'll
910        show up if this is in the failing packet in an ICMP error packet,
911        but it's now time to give up if the header length is bogus. */
912     if (check_col(pinfo->fd, COL_INFO))
913       col_append_fstr(pinfo->fd, COL_INFO, ", bogus TCP header length (%u, must be at least %u)",
914         hlen, TCPH_MIN_LEN);
915     if (tree) {
916       proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, tvb, offset + 12, 1, hlen,
917        "Header length: %u bytes (bogus, must be at least %u)", hlen,
918        TCPH_MIN_LEN);
919     }
920     return;
921   }
922
923   reported_len = tvb_reported_length(tvb);
924   len = tvb_length(tvb);
925
926   /* Compute the length of data in this segment. */
927   seglen = reported_len - hlen;
928
929   /* Compute the sequence number of next octet after this segment. */
930   nxtseq = th_seq + seglen;
931
932   if (tree) {
933     if (tcp_summary_in_tree)
934       proto_item_append_text(ti, ", Ack: %u", th_ack);
935     proto_item_set_len(ti, hlen);
936     if (nxtseq != th_seq)
937       proto_tree_add_uint(tcp_tree, hf_tcp_nxtseq, tvb, offset, 0, nxtseq);
938     if (th_flags & TH_ACK)
939       proto_tree_add_uint(tcp_tree, hf_tcp_ack, tvb, offset + 8, 4, th_ack);
940     proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, tvb, offset + 12, 1, hlen,
941         "Header length: %u bytes", hlen);
942     tf = proto_tree_add_uint_format(tcp_tree, hf_tcp_flags, tvb, offset + 13, 1,
943         th_flags, "Flags: 0x%04x (%s)", th_flags, flags);
944     field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
945     proto_tree_add_boolean(field_tree, hf_tcp_flags_cwr, tvb, offset + 13, 1, th_flags);
946     proto_tree_add_boolean(field_tree, hf_tcp_flags_ecn, tvb, offset + 13, 1, th_flags);
947     proto_tree_add_boolean(field_tree, hf_tcp_flags_urg, tvb, offset + 13, 1, th_flags);
948     proto_tree_add_boolean(field_tree, hf_tcp_flags_ack, tvb, offset + 13, 1, th_flags);
949     proto_tree_add_boolean(field_tree, hf_tcp_flags_push, tvb, offset + 13, 1, th_flags);
950     proto_tree_add_boolean(field_tree, hf_tcp_flags_reset, tvb, offset + 13, 1, th_flags);
951     proto_tree_add_boolean(field_tree, hf_tcp_flags_syn, tvb, offset + 13, 1, th_flags);
952     proto_tree_add_boolean(field_tree, hf_tcp_flags_fin, tvb, offset + 13, 1, th_flags);
953     proto_tree_add_uint(tcp_tree, hf_tcp_window_size, tvb, offset + 14, 2, th_win);
954   }
955
956   /* Assume we'll pass un-reassembled data to subdissectors. */
957   tcpinfo.is_reassembled = FALSE;
958
959   pinfo->private_data = &tcpinfo;
960
961   /*
962    * Assume, initially, that we can't desegment.
963    */
964   pinfo->can_desegment = 0;
965
966   th_sum = tvb_get_ntohs(tvb, offset + 16);
967   if (!pinfo->fragmented && len >= reported_len) {
968     /* The packet isn't part of a fragmented datagram, isn't being
969        returned inside an ICMP error packet, and isn't truncated, so we
970        can checksum it.
971        XXX - make a bigger scatter-gather list once we do fragment
972        reassembly? */
973
974     /* Set up the fields of the pseudo-header. */
975     cksum_vec[0].ptr = pinfo->src.data;
976     cksum_vec[0].len = pinfo->src.len;
977     cksum_vec[1].ptr = pinfo->dst.data;
978     cksum_vec[1].len = pinfo->dst.len;
979     cksum_vec[2].ptr = (const guint8 *)&phdr;
980     switch (pinfo->src.type) {
981
982     case AT_IPv4:
983         phdr[0] = htonl((IP_PROTO_TCP<<16) + reported_len);
984         cksum_vec[2].len = 4;
985         break;
986
987     case AT_IPv6:
988         phdr[0] = htonl(reported_len);
989         phdr[1] = htonl(IP_PROTO_TCP);
990         cksum_vec[2].len = 8;
991         break;
992
993     default:
994         /* TCP runs only atop IPv4 and IPv6.... */
995         g_assert_not_reached();
996         break;
997     }
998     cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
999     cksum_vec[3].len = reported_len;
1000     computed_cksum = in_cksum(&cksum_vec[0], 4);
1001     if (computed_cksum == 0) {
1002       /*
1003        * We have all the data for this TCP segment, and the checksum of
1004        * the header and the data is good, so we can desegment it.
1005        * Is desegmentation enabled?
1006        */
1007       if (tcp_desegment) {
1008         /* Yes - is this segment being returned in an error packet? */
1009         if (!pinfo->in_error_pkt) {
1010           /* No - indicate that we will desegment.
1011              We do NOT want to desegment segments returned in error
1012              packets, as they're not part of a TCP connection. */
1013           pinfo->can_desegment = 2;
1014         }
1015       }
1016       proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
1017          offset + 16, 2, th_sum, "Checksum: 0x%04x (correct)", th_sum);
1018     } else {
1019       proto_tree_add_boolean_hidden(tcp_tree, hf_tcp_checksum_bad, tvb,
1020            offset + 16, 2, TRUE);
1021       proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
1022            offset + 16, 2, th_sum,
1023            "Checksum: 0x%04x (incorrect, should be 0x%04x)", th_sum,
1024            in_cksum_shouldbe(th_sum, computed_cksum));
1025     }
1026   } else {
1027     proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
1028        offset + 16, 2, th_sum, "Checksum: 0x%04x", th_sum);
1029   }
1030   if (th_flags & TH_URG) {
1031     th_urp = tvb_get_ntohs(tvb, offset + 18);
1032     /* Export the urgent pointer, for the benefit of protocols such as
1033        rlogin. */
1034     tcpinfo.urgent = TRUE;
1035     tcpinfo.urgent_pointer = th_urp;
1036     if (check_col(pinfo->fd, COL_INFO))
1037       col_append_fstr(pinfo->fd, COL_INFO, " Urg=%u", th_urp);
1038     if (tcp_tree != NULL)
1039       proto_tree_add_uint(tcp_tree, hf_tcp_urgent_pointer, tvb, offset + 18, 2, th_urp);
1040   } else
1041     tcpinfo.urgent = FALSE;
1042
1043   if (check_col(pinfo->fd, COL_INFO))
1044     col_append_fstr(pinfo->fd, COL_INFO, " Len=%d", seglen);
1045
1046   /* Decode TCP options, if any. */
1047   if (tree && hlen > TCPH_MIN_LEN) {
1048     /* There's more than just the fixed-length header.  Decode the
1049        options. */
1050     optlen = hlen - TCPH_MIN_LEN; /* length of options, in bytes */
1051     tf = proto_tree_add_text(tcp_tree, tvb, offset +  20, optlen,
1052       "Options: (%d bytes)", optlen);
1053     field_tree = proto_item_add_subtree(tf, ett_tcp_options);
1054     dissect_ip_tcp_options(tvb, offset + 20, optlen,
1055       tcpopts, N_TCP_OPTS, TCPOPT_EOL, pinfo->fd, field_tree);
1056   }
1057
1058   /* Skip over header + options */
1059   offset += hlen;
1060
1061   pinfo->ptype = PT_TCP;
1062   pinfo->srcport = th_sport;
1063   pinfo->destport = th_dport;
1064   
1065   /* Check the packet length to see if there's more data
1066      (it could be an ACK-only packet) */
1067   length_remaining = tvb_length_remaining(tvb, offset);
1068   if (length_remaining != 0) {
1069     if (th_flags & TH_RST) {
1070       /*
1071        * RFC1122 says:
1072        *
1073        *        4.2.2.12  RST Segment: RFC-793 Section 3.4
1074        *
1075        *          A TCP SHOULD allow a received RST segment to include data.
1076        *
1077        *          DISCUSSION
1078        *               It has been suggested that a RST segment could contain
1079        *               ASCII text that encoded and explained the cause of the
1080        *               RST.  No standard has yet been established for such
1081        *               data.
1082        *
1083        * so for segments with RST we just display the data as text.
1084        */
1085       proto_tree_add_text(tcp_tree, tvb, offset, length_remaining,
1086                             "Reset cause: %s",
1087                             tvb_format_text(tvb, offset, length_remaining));
1088     } else {
1089       /* Can we desegment this segment? */
1090       if (pinfo->can_desegment) {
1091         /* Yes. */
1092         desegment_tcp(tvb, pinfo, offset, th_seq, nxtseq, th_sport, th_dport, tree, tcp_tree);
1093       } else {
1094         /* No - just call the subdissector. */
1095         decode_tcp_ports(tvb, offset, pinfo, tree, th_sport, th_dport);
1096       }
1097     }
1098   }
1099  
1100   if( data_out_file ) {
1101     reassemble_tcp( th_seq,             /* sequence number */
1102         seglen,                         /* data length */
1103         tvb_get_ptr(tvb, offset, length_remaining),     /* data */
1104         length_remaining,               /* captured data length */
1105         ( th_flags & TH_SYN ),          /* is syn set? */
1106         &pinfo->net_src,
1107         &pinfo->net_dst,
1108         pinfo->srcport,
1109         pinfo->destport);
1110   }
1111 }
1112
1113 void
1114 proto_register_tcp(void)
1115 {
1116         static hf_register_info hf[] = {
1117
1118                 { &hf_tcp_srcport,
1119                 { "Source Port",                "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
1120                         "", HFILL }},
1121
1122                 { &hf_tcp_dstport,
1123                 { "Destination Port",           "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
1124                         "", HFILL }},
1125
1126                 { &hf_tcp_port,
1127                 { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
1128                         "", HFILL }},
1129
1130                 { &hf_tcp_seq,
1131                 { "Sequence number",            "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
1132                         "", HFILL }},
1133
1134                 { &hf_tcp_nxtseq,
1135                 { "Next sequence number",       "tcp.nxtseq", FT_UINT32, BASE_DEC, NULL, 0x0,
1136                         "", HFILL }},
1137
1138                 { &hf_tcp_ack,
1139                 { "Acknowledgement number",     "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
1140                         "", HFILL }},
1141
1142                 { &hf_tcp_hdr_len,
1143                 { "Header Length",              "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
1144                         "", HFILL }},
1145
1146                 { &hf_tcp_flags,
1147                 { "Flags",                      "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
1148                         "", HFILL }},
1149
1150                 { &hf_tcp_flags_cwr,
1151                 { "Congestion Window Reduced (CWR)",                    "tcp.flags.cwr", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_CWR,
1152                         "", HFILL }},
1153
1154                 { &hf_tcp_flags_ecn,
1155                 { "ECN-Echo",                   "tcp.flags.ecn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ECN,
1156                         "", HFILL }},
1157
1158                 { &hf_tcp_flags_urg,
1159                 { "Urgent",                     "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
1160                         "", HFILL }},
1161
1162                 { &hf_tcp_flags_ack,
1163                 { "Acknowledgment",             "tcp.flags.ack", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ACK,
1164                         "", HFILL }},
1165
1166                 { &hf_tcp_flags_push,
1167                 { "Push",                       "tcp.flags.push", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_PUSH,
1168                         "", HFILL }},
1169
1170                 { &hf_tcp_flags_reset,
1171                 { "Reset",                      "tcp.flags.reset", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_RST,
1172                         "", HFILL }},
1173
1174                 { &hf_tcp_flags_syn,
1175                 { "Syn",                        "tcp.flags.syn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_SYN,
1176                         "", HFILL }},
1177
1178                 { &hf_tcp_flags_fin,
1179                 { "Fin",                        "tcp.flags.fin", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_FIN,
1180                         "", HFILL }},
1181
1182                 { &hf_tcp_window_size,
1183                 { "Window size",                "tcp.window_size", FT_UINT16, BASE_DEC, NULL, 0x0,
1184                         "", HFILL }},
1185
1186                 { &hf_tcp_checksum,
1187                 { "Checksum",                   "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
1188                         "", HFILL }},
1189
1190                 { &hf_tcp_checksum_bad,
1191                 { "Bad Checksum",               "tcp.checksum_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
1192                         "", HFILL }},
1193
1194                 { &hf_tcp_urgent_pointer,
1195                 { "Urgent pointer",             "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
1196                         "", HFILL }},
1197         };
1198         static gint *ett[] = {
1199                 &ett_tcp,
1200                 &ett_tcp_flags,
1201                 &ett_tcp_options,
1202                 &ett_tcp_option_sack,
1203                 &ett_tcp_segments,
1204         };
1205         module_t *tcp_module;
1206
1207         proto_tcp = proto_register_protocol("Transmission Control Protocol",
1208             "TCP", "tcp");
1209         proto_register_field_array(proto_tcp, hf, array_length(hf));
1210         proto_register_subtree_array(ett, array_length(ett));
1211
1212         /* subdissector code */
1213         subdissector_table = register_dissector_table("tcp.port");
1214         register_heur_dissector_list("tcp", &heur_subdissector_list);
1215
1216         /* Register configuration preferences */
1217         tcp_module = prefs_register_protocol(proto_tcp, NULL);
1218         prefs_register_bool_preference(tcp_module, "tcp_summary_in_tree",
1219             "Show TCP summary in protocol tree",
1220 "Whether the TCP summary line should be shown in the protocol tree",
1221             &tcp_summary_in_tree);
1222         prefs_register_bool_preference(tcp_module, "desegment_tcp_streams",
1223             "Allow subdissector to desegment TCP streams",
1224 "Whether subdissector can request TCP streams to be desegmented",
1225             &tcp_desegment);
1226
1227         register_init_routine(tcp_desegment_init);
1228         register_init_routine(tcp_fragment_init);
1229 }
1230
1231 void
1232 proto_reg_handoff_tcp(void)
1233 {
1234         dissector_handle_t tcp_handle;
1235
1236         tcp_handle = create_dissector_handle(dissect_tcp, proto_tcp);
1237         dissector_add("ip.proto", IP_PROTO_TCP, tcp_handle);
1238         data_handle = find_dissector("data");
1239 }