Add hidden fields for bad checksums to various IP-family protocols.
[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.100 2001/02/28 19:33:49 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@zing.org>
8  * Copyright 1998 Gerald Combs
9  *
10  * 
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  * 
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #ifdef HAVE_SYS_TYPES_H
31 # include <sys/types.h>
32 #endif
33
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
36 #endif
37
38 #include <stdio.h>
39 #include <string.h>
40 #include <glib.h>
41 #include "in_cksum.h"
42
43 #ifdef NEED_SNPRINTF_H
44 # include "snprintf.h"
45 #endif
46
47 #include "globals.h"
48 #include "resolv.h"
49 #include "follow.h"
50 #include "prefs.h"
51 #include "packet-tcp.h"
52 #include "packet-ip.h"
53 #include "conversation.h"
54 #include "strutil.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 guint16 tcp_urgent_pointer;
62
63 static int proto_tcp = -1;
64 static int hf_tcp_srcport = -1;
65 static int hf_tcp_dstport = -1;
66 static int hf_tcp_port = -1;
67 static int hf_tcp_seq = -1;
68 static int hf_tcp_nxtseq = -1;
69 static int hf_tcp_ack = -1;
70 static int hf_tcp_hdr_len = -1;
71 static int hf_tcp_flags = -1;
72 static int hf_tcp_flags_cwr = -1;
73 static int hf_tcp_flags_ecn = -1;
74 static int hf_tcp_flags_urg = -1;
75 static int hf_tcp_flags_ack = -1;
76 static int hf_tcp_flags_push = -1;
77 static int hf_tcp_flags_reset = -1;
78 static int hf_tcp_flags_syn = -1;
79 static int hf_tcp_flags_fin = -1;
80 static int hf_tcp_window_size = -1;
81 static int hf_tcp_checksum = -1;
82 static int hf_tcp_checksum_bad = -1;
83 static int hf_tcp_urgent_pointer = -1;
84
85 static gint ett_tcp = -1;
86 static gint ett_tcp_flags = -1;
87 static gint ett_tcp_options = -1;
88 static gint ett_tcp_option_sack = -1;
89
90 static dissector_table_t subdissector_table;
91 static heur_dissector_list_t heur_subdissector_list;
92 static conv_dissector_list_t conv_subdissector_list;
93
94 /* TCP Ports */
95
96 #define TCP_PORT_SMTP                   25
97
98 /* TCP structs and definitions */
99
100 typedef struct _e_tcphdr {
101   guint16 th_sport;
102   guint16 th_dport;
103   guint32 th_seq;
104   guint32 th_ack;
105   guint8  th_off_x2; /* combines th_off and th_x2 */
106   guint8  th_flags;
107 #define TH_FIN  0x01
108 #define TH_SYN  0x02
109 #define TH_RST  0x04
110 #define TH_PUSH 0x08
111 #define TH_ACK  0x10
112 #define TH_URG  0x20
113 #define TH_ECN  0x40
114 #define TH_CWR  0x80
115   guint16 th_win;
116   guint16 th_sum;
117   guint16 th_urp;
118 } e_tcphdr;
119
120 /*
121  *      TCP option
122  */
123  
124 #define TCPOPT_NOP              1       /* Padding */
125 #define TCPOPT_EOL              0       /* End of options */
126 #define TCPOPT_MSS              2       /* Segment size negotiating */
127 #define TCPOPT_WINDOW           3       /* Window scaling */
128 #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
129 #define TCPOPT_SACK             5       /* SACK Block */
130 #define TCPOPT_ECHO             6
131 #define TCPOPT_ECHOREPLY        7
132 #define TCPOPT_TIMESTAMP        8       /* Better RTT estimations/PAWS */
133 #define TCPOPT_CC               11
134 #define TCPOPT_CCNEW            12
135 #define TCPOPT_CCECHO           13
136 #define TCPOPT_MD5              19      /* RFC2385 */
137
138 /*
139  *     TCP option lengths
140  */
141
142 #define TCPOLEN_MSS            4
143 #define TCPOLEN_WINDOW         3
144 #define TCPOLEN_SACK_PERM      2
145 #define TCPOLEN_SACK_MIN       2
146 #define TCPOLEN_ECHO           6
147 #define TCPOLEN_ECHOREPLY      6
148 #define TCPOLEN_TIMESTAMP      10
149 #define TCPOLEN_CC             6
150 #define TCPOLEN_CCNEW          6
151 #define TCPOLEN_CCECHO         6
152 #define TCPOLEN_MD5            18
153
154 static void
155 tcp_info_append_uint(frame_data *fd, const char *abbrev, guint32 val)
156 {
157   if (check_col(fd, COL_INFO))
158     col_append_fstr(fd, COL_INFO, " %s=%u", abbrev, val);
159 }
160
161 static void
162 dissect_tcpopt_maxseg(const ip_tcp_opt *optp, tvbuff_t *tvb,
163     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
164 {
165   guint16 mss;
166
167   mss = tvb_get_ntohs(tvb, offset + 2);
168   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
169                         "%s: %u bytes", optp->name, mss);
170   tcp_info_append_uint(fd, "MSS", mss);
171 }
172
173 static void
174 dissect_tcpopt_wscale(const ip_tcp_opt *optp, tvbuff_t *tvb,
175     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
176 {
177   guint8 ws;
178
179   ws = tvb_get_guint8(tvb, offset + 2);
180   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
181                         "%s: %u bytes", optp->name, ws);
182   tcp_info_append_uint(fd, "WS", ws);
183 }
184
185 static void
186 dissect_tcpopt_sack(const ip_tcp_opt *optp, tvbuff_t *tvb,
187     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
188 {
189   proto_tree *field_tree = NULL;
190   proto_item *tf;
191   guint leftedge, rightedge;
192
193   tf = proto_tree_add_text(opt_tree, tvb, offset,      optlen, "%s:", optp->name);
194   offset += 2;  /* skip past type and length */
195   optlen -= 2;  /* subtract size of type and length */
196   while (optlen > 0) {
197     if (field_tree == NULL) {
198       /* Haven't yet made a subtree out of this option.  Do so. */
199       field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
200     }
201     if (optlen < 4) {
202       proto_tree_add_text(field_tree, tvb, offset,      optlen,
203         "(suboption would go past end of option)");
204       break;
205     }
206     leftedge = tvb_get_ntohl(tvb, offset);
207     optlen -= 4;
208     if (optlen < 4) {
209       proto_tree_add_text(field_tree, tvb, offset,      optlen,
210         "(suboption would go past end of option)");
211       break;
212     }
213     /* XXX - check whether it goes past end of packet */
214     rightedge = tvb_get_ntohl(tvb, offset + 4);
215     optlen -= 4;
216     proto_tree_add_text(field_tree, tvb, offset,      8,
217         "left edge = %u, right edge = %u", leftedge, rightedge);
218     tcp_info_append_uint(fd, "SLE", leftedge);
219     tcp_info_append_uint(fd, "SRE", rightedge);
220     offset += 8;
221   }
222 }
223
224 static void
225 dissect_tcpopt_echo(const ip_tcp_opt *optp, tvbuff_t *tvb,
226     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
227 {
228   guint32 echo;
229
230   echo = tvb_get_ntohl(tvb, offset + 2);
231   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
232                         "%s: %u", optp->name, echo);
233   tcp_info_append_uint(fd, "ECHO", echo);
234 }
235
236 static void
237 dissect_tcpopt_timestamp(const ip_tcp_opt *optp, tvbuff_t *tvb,
238     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
239 {
240   guint32 tsv, tser;
241
242   tsv = tvb_get_ntohl(tvb, offset + 2);
243   tser = tvb_get_ntohl(tvb, offset + 6);
244   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
245     "%s: tsval %u, tsecr %u", optp->name, tsv, tser);
246   tcp_info_append_uint(fd, "TSV", tsv);
247   tcp_info_append_uint(fd, "TSER", tser);
248 }
249
250 static void
251 dissect_tcpopt_cc(const ip_tcp_opt *optp, tvbuff_t *tvb,
252     int offset, guint optlen, frame_data *fd, proto_tree *opt_tree)
253 {
254   guint32 cc;
255
256   cc = tvb_get_ntohl(tvb, offset + 2);
257   proto_tree_add_text(opt_tree, tvb, offset,      optlen,
258                         "%s: %u", optp->name, cc);
259   tcp_info_append_uint(fd, "CC", cc);
260 }
261
262 static const ip_tcp_opt tcpopts[] = {
263   {
264     TCPOPT_EOL,
265     "EOL",
266     NULL,
267     NO_LENGTH,
268     0,
269     NULL,
270   },
271   {
272     TCPOPT_NOP,
273     "NOP",
274     NULL,
275     NO_LENGTH,
276     0,
277     NULL,
278   },
279   {
280     TCPOPT_MSS,
281     "Maximum segment size",
282     NULL,
283     FIXED_LENGTH,
284     TCPOLEN_MSS,
285     dissect_tcpopt_maxseg
286   },
287   {
288     TCPOPT_WINDOW,
289     "Window scale",
290     NULL,
291     FIXED_LENGTH,
292     TCPOLEN_WINDOW,
293     dissect_tcpopt_wscale
294   },
295   {
296     TCPOPT_SACK_PERM,
297     "SACK permitted",
298     NULL,
299     FIXED_LENGTH,
300     TCPOLEN_SACK_PERM,
301     NULL,
302   },
303   {
304     TCPOPT_SACK,
305     "SACK",
306     &ett_tcp_option_sack,
307     VARIABLE_LENGTH,
308     TCPOLEN_SACK_MIN,
309     dissect_tcpopt_sack
310   },
311   {
312     TCPOPT_ECHO,
313     "Echo",
314     NULL,
315     FIXED_LENGTH,
316     TCPOLEN_ECHO,
317     dissect_tcpopt_echo
318   },
319   {
320     TCPOPT_ECHOREPLY,
321     "Echo reply",
322     NULL,
323     FIXED_LENGTH,
324     TCPOLEN_ECHOREPLY,
325     dissect_tcpopt_echo
326   },
327   {
328     TCPOPT_TIMESTAMP,
329     "Time stamp",
330     NULL,
331     FIXED_LENGTH,
332     TCPOLEN_TIMESTAMP,
333     dissect_tcpopt_timestamp
334   },
335   {
336     TCPOPT_CC,
337     "CC",
338     NULL,
339     FIXED_LENGTH,
340     TCPOLEN_CC,
341     dissect_tcpopt_cc
342   },
343   {
344     TCPOPT_CCNEW,
345     "CC.NEW",
346     NULL,
347     FIXED_LENGTH,
348     TCPOPT_CCNEW,
349     dissect_tcpopt_cc
350   },
351   {
352     TCPOPT_CCECHO,
353     "CC.ECHO",
354     NULL,
355     FIXED_LENGTH,
356     TCPOLEN_CCECHO,
357     dissect_tcpopt_cc
358   },
359   {
360     TCPOPT_MD5,
361     "TCP MD5 signature",
362     NULL,
363     FIXED_LENGTH,
364     TCPOLEN_MD5,
365     NULL
366   }
367 };
368
369 #define N_TCP_OPTS      (sizeof tcpopts / sizeof tcpopts[0])
370
371 /* TCP flags flag */
372 static const true_false_string flags_set_truth = {
373   "Set",
374   "Not set"
375 };
376
377
378 /* Determine if there is a sub-dissector and call it.  This has been */
379 /* separated into a stand alone routine to other protocol dissectors */
380 /* can call to it, ie. socks    */
381
382 void
383 decode_tcp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
384         proto_tree *tree, int src_port, int dst_port)
385 {
386   tvbuff_t *next_tvb;
387
388   next_tvb = tvb_new_subset(tvb, offset, -1, -1);
389
390 /* determine if this packet is part of a conversation and call dissector */
391 /* for the conversation if available */
392
393   if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_TCP,
394                 src_port, dst_port, next_tvb, pinfo, tree))
395     return;
396
397   /* do lookup with the subdissector table */
398   if (dissector_try_port(subdissector_table, src_port, next_tvb, pinfo, tree) ||
399       dissector_try_port(subdissector_table, dst_port, next_tvb, pinfo, tree))
400     return;
401
402   /* do lookup with the heuristic subdissector table */
403   if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree))
404     return;
405
406   /* Oh, well, we don't know this; dissect it as data. */
407   dissect_data(next_tvb, 0, pinfo, tree);
408 }
409
410
411 static void
412 dissect_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
413 {
414   e_tcphdr   th;
415   proto_tree *tcp_tree = NULL, *field_tree = NULL;
416   proto_item *ti, *tf;
417   int        offset = 0;
418   gchar      flags[64] = "<None>";
419   gchar     *fstr[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG", "ECN", "CWR" };
420   gint       fpos = 0, i;
421   guint      bpos;
422   guint      hlen;
423   guint      optlen;
424   guint32    seglen;
425   guint32    nxtseq;
426   guint      len;
427   guint      reported_len;
428   vec_t      cksum_vec[4];
429   guint32    phdr[2];
430   guint16    computed_cksum;
431   guint      length_remaining;
432
433   if (check_col(pinfo->fd, COL_PROTOCOL))
434     col_set_str(pinfo->fd, COL_PROTOCOL, "TCP");
435
436   /* Clear out the Info column. */
437   if (check_col(pinfo->fd, COL_INFO))
438     col_clear(pinfo->fd, COL_INFO);
439
440   /* Avoids alignment problems on many architectures. */
441   tvb_memcpy(tvb, (guint8 *)&th, offset, sizeof(e_tcphdr));
442   th.th_sport = ntohs(th.th_sport);
443   th.th_dport = ntohs(th.th_dport);
444   th.th_win   = ntohs(th.th_win);
445   th.th_sum   = ntohs(th.th_sum);
446   th.th_urp   = ntohs(th.th_urp);
447   th.th_seq   = ntohl(th.th_seq);
448   th.th_ack   = ntohl(th.th_ack);
449
450   /* Export the urgent pointer, for the benefit of protocols such as
451      rlogin. */
452   tcp_urgent_pointer = th.th_urp;
453  
454   if (check_col(pinfo->fd, COL_INFO) || tree) {  
455     for (i = 0; i < 8; i++) {
456       bpos = 1 << i;
457       if (th.th_flags & bpos) {
458         if (fpos) {
459           strcpy(&flags[fpos], ", ");
460           fpos += 2;
461         }
462         strcpy(&flags[fpos], fstr[i]);
463         fpos += 3;
464       }
465     }
466     flags[fpos] = '\0';
467   }
468   
469   hlen = hi_nibble(th.th_off_x2) * 4;  /* TCP header length, in bytes */
470
471   reported_len = tvb_reported_length(tvb);
472   len = tvb_length(tvb);
473
474   /* Compute the length of data in this segment. */
475   seglen = reported_len - hlen;
476
477   /* Compute the sequence number of next octet after this segment. */
478   nxtseq = th.th_seq + seglen;
479
480   if (check_col(pinfo->fd, COL_INFO)) {
481     if (th.th_flags & TH_URG)
482       col_append_fstr(pinfo->fd, COL_INFO, "%s > %s [%s] Seq=%u Ack=%u Win=%u Urg=%u Len=%d",
483         get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
484         th.th_seq, th.th_ack, th.th_win, th.th_urp, seglen);
485     else
486       col_append_fstr(pinfo->fd, COL_INFO, "%s > %s [%s] Seq=%u Ack=%u Win=%u Len=%d",
487         get_tcp_port(th.th_sport), get_tcp_port(th.th_dport), flags,
488         th.th_seq, th.th_ack, th.th_win, seglen);
489   }
490   
491   if (tree) {
492     if (tcp_summary_in_tree) {
493             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);
494     }
495     else {
496             ti = proto_tree_add_item(tree, proto_tcp, tvb, offset, hlen, FALSE);
497     }
498     tcp_tree = proto_item_add_subtree(ti, ett_tcp);
499     proto_tree_add_uint_format(tcp_tree, hf_tcp_srcport, tvb, offset, 2, th.th_sport,
500         "Source port: %s (%u)", get_tcp_port(th.th_sport), th.th_sport);
501     proto_tree_add_uint_format(tcp_tree, hf_tcp_dstport, tvb, offset + 2, 2, th.th_dport,
502         "Destination port: %s (%u)", get_tcp_port(th.th_dport), th.th_dport);
503     proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset, 2, th.th_sport);
504     proto_tree_add_uint_hidden(tcp_tree, hf_tcp_port, tvb, offset + 2, 2, th.th_dport);
505     proto_tree_add_uint(tcp_tree, hf_tcp_seq, tvb, offset + 4, 4, th.th_seq);
506     if (nxtseq != th.th_seq)
507       proto_tree_add_uint(tcp_tree, hf_tcp_nxtseq, tvb, offset, 0, nxtseq);
508     if (th.th_flags & TH_ACK)
509       proto_tree_add_uint(tcp_tree, hf_tcp_ack, tvb, offset + 8, 4, th.th_ack);
510     proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, tvb, offset + 12, 1, hlen,
511         "Header length: %u bytes", hlen);
512     tf = proto_tree_add_uint_format(tcp_tree, hf_tcp_flags, tvb, offset + 13, 1,
513         th.th_flags, "Flags: 0x%04x (%s)", th.th_flags, flags);
514     field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
515     proto_tree_add_boolean(field_tree, hf_tcp_flags_cwr, tvb, offset + 13, 1, th.th_flags);
516     proto_tree_add_boolean(field_tree, hf_tcp_flags_ecn, tvb, offset + 13, 1, th.th_flags);
517     proto_tree_add_boolean(field_tree, hf_tcp_flags_urg, tvb, offset + 13, 1, th.th_flags);
518     proto_tree_add_boolean(field_tree, hf_tcp_flags_ack, tvb, offset + 13, 1, th.th_flags);
519     proto_tree_add_boolean(field_tree, hf_tcp_flags_push, tvb, offset + 13, 1, th.th_flags);
520     proto_tree_add_boolean(field_tree, hf_tcp_flags_reset, tvb, offset + 13, 1, th.th_flags);
521     proto_tree_add_boolean(field_tree, hf_tcp_flags_syn, tvb, offset + 13, 1, th.th_flags);
522     proto_tree_add_boolean(field_tree, hf_tcp_flags_fin, tvb, offset + 13, 1, th.th_flags);
523     proto_tree_add_uint(tcp_tree, hf_tcp_window_size, tvb, offset + 14, 2, th.th_win);
524     if (!pinfo->fragmented && len >= reported_len) {
525       /* The packet isn't part of a fragmented datagram and isn't
526          truncated, so we can checksum it.
527          XXX - make a bigger scatter-gather list once we do fragment
528          reassembly? */
529
530       /* Set up the fields of the pseudo-header. */
531       cksum_vec[0].ptr = pinfo->src.data;
532       cksum_vec[0].len = pinfo->src.len;
533       cksum_vec[1].ptr = pinfo->dst.data;
534       cksum_vec[1].len = pinfo->dst.len;
535       cksum_vec[2].ptr = (const guint8 *)&phdr;
536       switch (pinfo->src.type) {
537
538       case AT_IPv4:
539         phdr[0] = htonl((IP_PROTO_TCP<<16) + reported_len);
540         cksum_vec[2].len = 4;
541         break;
542
543       case AT_IPv6:
544         phdr[0] = htonl(reported_len);
545         phdr[1] = htonl(IP_PROTO_TCP);
546         cksum_vec[2].len = 8;
547         break;
548
549       default:
550         /* TCP runs only atop IPv4 and IPv6.... */
551         g_assert_not_reached();
552         break;
553       }
554       cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, len);
555       cksum_vec[3].len = reported_len;
556       computed_cksum = in_cksum(&cksum_vec[0], 4);
557       if (computed_cksum == 0) {
558         proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
559            offset + 16, 2, th.th_sum, "Checksum: 0x%04x (correct)", th.th_sum);
560       } else {
561         proto_tree_add_item_hidden(tcp_tree, hf_tcp_checksum_bad, tvb,
562            offset + 16, 2, TRUE);
563         proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
564            offset + 16, 2, th.th_sum,
565            "Checksum: 0x%04x (incorrect, should be 0x%04x)", th.th_sum,
566            in_cksum_shouldbe(th.th_sum, computed_cksum));
567       }
568     } else {
569       proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
570        offset + 16, 2, th.th_sum, "Checksum: 0x%04x", th.th_sum);
571     }
572     if (th.th_flags & TH_URG)
573       proto_tree_add_uint(tcp_tree, hf_tcp_urgent_pointer, tvb, offset + 18, 2, th.th_urp);
574   }
575
576   /* Decode TCP options, if any. */
577   if (tree  && hlen > sizeof (e_tcphdr)) {
578     /* There's more than just the fixed-length header.  Decode the
579        options. */
580     optlen = hlen - sizeof (e_tcphdr); /* length of options, in bytes */
581     tf = proto_tree_add_text(tcp_tree, tvb, offset +  20, optlen,
582       "Options: (%d bytes)", optlen);
583     field_tree = proto_item_add_subtree(tf, ett_tcp_options);
584     dissect_ip_tcp_options(tvb, offset + 20, optlen,
585       tcpopts, N_TCP_OPTS, TCPOPT_EOL, pinfo->fd, field_tree);
586   }
587
588   /* Skip over header + options */
589   offset += hlen;
590
591   pinfo->ptype = PT_TCP;
592   pinfo->srcport = th.th_sport;
593   pinfo->destport = th.th_dport;
594   
595   /* Check the packet length to see if there's more data
596      (it could be an ACK-only packet) */
597   length_remaining = tvb_length_remaining(tvb, offset);
598   if (length_remaining != 0) {
599     if (th.th_flags & TH_RST) {
600       /*
601        * RFC1122 says:
602        *
603        *        4.2.2.12  RST Segment: RFC-793 Section 3.4
604        *
605        *          A TCP SHOULD allow a received RST segment to include data.
606        *
607        *          DISCUSSION
608        *               It has been suggested that a RST segment could contain
609        *               ASCII text that encoded and explained the cause of the
610        *               RST.  No standard has yet been established for such
611        *               data.
612        *
613        * so for segments with RST we just display the data as text.
614        */
615       proto_tree_add_text(tcp_tree, tvb, offset, length_remaining,
616                             "Reset cause: %s",
617                             tvb_format_text(tvb, offset, length_remaining));
618     } else
619       decode_tcp_ports( tvb, offset, pinfo, tree, th.th_sport, th.th_dport);
620   }
621  
622   if( data_out_file ) {
623     reassemble_tcp( th.th_seq,          /* sequence number */
624         seglen,                         /* data length */
625         tvb_get_ptr(tvb, offset, length_remaining),     /* data */
626         length_remaining,               /* captured data length */
627         ( th.th_flags & TH_SYN ),       /* is syn set? */
628         &pinfo->net_src,
629         &pinfo->net_dst,
630         pinfo->srcport,
631         pinfo->destport);
632   }
633 }
634
635 void
636 proto_register_tcp(void)
637 {
638         static hf_register_info hf[] = {
639
640                 { &hf_tcp_srcport,
641                 { "Source Port",                "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
642                         "" }},
643
644                 { &hf_tcp_dstport,
645                 { "Destination Port",           "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
646                         "" }},
647
648                 { &hf_tcp_port,
649                 { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
650                         "" }},
651
652                 { &hf_tcp_seq,
653                 { "Sequence number",            "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
654                         "" }},
655
656                 { &hf_tcp_nxtseq,
657                 { "Next sequence number",       "tcp.nxtseq", FT_UINT32, BASE_DEC, NULL, 0x0,
658                         "" }},
659
660                 { &hf_tcp_ack,
661                 { "Acknowledgement number",     "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
662                         "" }},
663
664                 { &hf_tcp_hdr_len,
665                 { "Header Length",              "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
666                         "" }},
667
668                 { &hf_tcp_flags,
669                 { "Flags",                      "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
670                         "" }},
671
672                 { &hf_tcp_flags_cwr,
673                 { "Congestion Window Reduced (CWR)",                    "tcp.flags.cwr", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_CWR,
674                         "" }},
675
676                 { &hf_tcp_flags_ecn,
677                 { "ECN-Echo",                   "tcp.flags.ecn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ECN,
678                         "" }},
679
680                 { &hf_tcp_flags_urg,
681                 { "Urgent",                     "tcp.flags.urg", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_URG,
682                         "" }},
683
684                 { &hf_tcp_flags_ack,
685                 { "Acknowledgment",             "tcp.flags.ack", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_ACK,
686                         "" }},
687
688                 { &hf_tcp_flags_push,
689                 { "Push",                       "tcp.flags.push", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_PUSH,
690                         "" }},
691
692                 { &hf_tcp_flags_reset,
693                 { "Reset",                      "tcp.flags.reset", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_RST,
694                         "" }},
695
696                 { &hf_tcp_flags_syn,
697                 { "Syn",                        "tcp.flags.syn", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_SYN,
698                         "" }},
699
700                 { &hf_tcp_flags_fin,
701                 { "Fin",                        "tcp.flags.fin", FT_BOOLEAN, 8, TFS(&flags_set_truth), TH_FIN,
702                         "" }},
703
704                 { &hf_tcp_window_size,
705                 { "Window size",                "tcp.window_size", FT_UINT16, BASE_DEC, NULL, 0x0,
706                         "" }},
707
708                 { &hf_tcp_checksum,
709                 { "Checksum",                   "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
710                         "" }},
711
712                 { &hf_tcp_checksum_bad,
713                 { "Bad Checksum",               "tcp.checksum_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
714                         "" }},
715
716                 { &hf_tcp_urgent_pointer,
717                 { "Urgent pointer",             "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
718                         "" }},
719         };
720         static gint *ett[] = {
721                 &ett_tcp,
722                 &ett_tcp_flags,
723                 &ett_tcp_options,
724                 &ett_tcp_option_sack,
725         };
726         module_t *tcp_module;
727
728         proto_tcp = proto_register_protocol("Transmission Control Protocol",
729             "TCP", "tcp");
730         proto_register_field_array(proto_tcp, hf, array_length(hf));
731         proto_register_subtree_array(ett, array_length(ett));
732
733         /* subdissector code */
734         subdissector_table = register_dissector_table("tcp.port");
735         register_heur_dissector_list("tcp", &heur_subdissector_list);
736         register_conv_dissector_list("tcp", &conv_subdissector_list);
737
738         /* Register configuration preferences */
739         tcp_module = prefs_register_protocol(proto_tcp, NULL);
740         prefs_register_bool_preference(tcp_module, "tcp_summary_in_tree",
741             "Show TCP summary in protocol tree",
742 "Whether the TCP summary line should be shown in the protocol tree",
743             &tcp_summary_in_tree);
744 }
745
746 void
747 proto_reg_handoff_tcp(void)
748 {
749         dissector_add("ip.proto", IP_PROTO_TCP, dissect_tcp, proto_tcp);
750 }