Fix ex "modeline" so it works;
[obnox/wireshark/wip.git] / epan / dissectors / packet-tcp.c
1 /* packet-tcp.c
2  * Routines for TCP packet disassembly
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30 #include <string.h>
31 #include <glib.h>
32 #include <epan/in_cksum.h>
33
34 #include <epan/packet.h>
35 #include <epan/addr_resolv.h>
36 #include <epan/ipproto.h>
37 #include <epan/ip_opts.h>
38 #include <epan/follow.h>
39 #include <epan/prefs.h>
40 #include <epan/emem.h>
41 #include "packet-tcp.h"
42 #include "packet-frame.h"
43 #include <epan/conversation.h>
44 #include <epan/reassemble.h>
45 #include <epan/tap.h>
46 #include <epan/slab.h>
47 #include <epan/expert.h>
48
49 static int tcp_tap = -1;
50
51 /* Place TCP summary in proto tree */
52 static gboolean tcp_summary_in_tree = TRUE;
53
54 /*
55  * Flag to control whether to check the TCP checksum.
56  *
57  * In at least some Solaris network traces, there are packets with bad
58  * TCP checksums, but the traffic appears to indicate that the packets
59  * *were* received; the packets were probably sent by the host on which
60  * the capture was being done, on a network interface to which
61  * checksumming was offloaded, so that DLPI supplied an un-checksummed
62  * packet to the capture program but a checksummed packet got put onto
63  * the wire.
64  */
65 static gboolean tcp_check_checksum = FALSE;
66
67 extern FILE* data_out_file;
68
69 static int proto_tcp = -1;
70 static int hf_tcp_srcport = -1;
71 static int hf_tcp_dstport = -1;
72 static int hf_tcp_port = -1;
73 static int hf_tcp_stream = -1;
74 static int hf_tcp_seq = -1;
75 static int hf_tcp_nxtseq = -1;
76 static int hf_tcp_ack = -1;
77 static int hf_tcp_hdr_len = -1;
78 static int hf_tcp_flags = -1;
79 static int hf_tcp_flags_res = -1;
80 static int hf_tcp_flags_ns = -1;
81 static int hf_tcp_flags_cwr = -1;
82 static int hf_tcp_flags_ecn = -1;
83 static int hf_tcp_flags_urg = -1;
84 static int hf_tcp_flags_ack = -1;
85 static int hf_tcp_flags_push = -1;
86 static int hf_tcp_flags_reset = -1;
87 static int hf_tcp_flags_syn = -1;
88 static int hf_tcp_flags_fin = -1;
89 static int hf_tcp_window_size_value = -1;
90 static int hf_tcp_window_size = -1;
91 static int hf_tcp_window_size_scalefactor = -1;
92 static int hf_tcp_checksum = -1;
93 static int hf_tcp_checksum_bad = -1;
94 static int hf_tcp_checksum_good = -1;
95 static int hf_tcp_len = -1;
96 static int hf_tcp_urgent_pointer = -1;
97 static int hf_tcp_analysis_flags = -1;
98 static int hf_tcp_analysis_bytes_in_flight = -1;
99 static int hf_tcp_analysis_acks_frame = -1;
100 static int hf_tcp_analysis_ack_rtt = -1;
101 static int hf_tcp_analysis_rto = -1;
102 static int hf_tcp_analysis_rto_frame = -1;
103 static int hf_tcp_analysis_retransmission = -1;
104 static int hf_tcp_analysis_fast_retransmission = -1;
105 static int hf_tcp_analysis_out_of_order = -1;
106 static int hf_tcp_analysis_reused_ports = -1;
107 static int hf_tcp_analysis_lost_packet = -1;
108 static int hf_tcp_analysis_ack_lost_packet = -1;
109 static int hf_tcp_analysis_window_update = -1;
110 static int hf_tcp_analysis_window_full = -1;
111 static int hf_tcp_analysis_keep_alive = -1;
112 static int hf_tcp_analysis_keep_alive_ack = -1;
113 static int hf_tcp_analysis_duplicate_ack = -1;
114 static int hf_tcp_analysis_duplicate_ack_num = -1;
115 static int hf_tcp_analysis_duplicate_ack_frame = -1;
116 static int hf_tcp_analysis_zero_window = -1;
117 static int hf_tcp_analysis_zero_window_probe = -1;
118 static int hf_tcp_analysis_zero_window_probe_ack = -1;
119 static int hf_tcp_continuation_to = -1;
120 static int hf_tcp_pdu_time = -1;
121 static int hf_tcp_pdu_size = -1;
122 static int hf_tcp_pdu_last_frame = -1;
123 static int hf_tcp_reassembled_in = -1;
124 static int hf_tcp_reassembled_length = -1;
125 static int hf_tcp_segments = -1;
126 static int hf_tcp_segment = -1;
127 static int hf_tcp_segment_overlap = -1;
128 static int hf_tcp_segment_overlap_conflict = -1;
129 static int hf_tcp_segment_multiple_tails = -1;
130 static int hf_tcp_segment_too_long_fragment = -1;
131 static int hf_tcp_segment_error = -1;
132 static int hf_tcp_segment_count = -1;
133 static int hf_tcp_options = -1;
134 static int hf_tcp_option_kind = -1;
135 static int hf_tcp_option_len = -1;
136 static int hf_tcp_option_mss = -1;
137 static int hf_tcp_option_mss_val = -1;
138 static int hf_tcp_option_wscale_shift = -1;
139 static int hf_tcp_option_wscale_multiplier = -1;
140 static int hf_tcp_option_sack_perm = -1;
141 static int hf_tcp_option_sack = -1;
142 static int hf_tcp_option_sack_sle = -1;
143 static int hf_tcp_option_sack_sre = -1;
144 static int hf_tcp_option_echo = -1;
145 static int hf_tcp_option_echo_reply = -1;
146 static int hf_tcp_option_timestamp_tsval = -1;
147 static int hf_tcp_option_timestamp_tsecr = -1;
148 static int hf_tcp_option_cc = -1;
149 static int hf_tcp_option_ccnew = -1;
150 static int hf_tcp_option_ccecho = -1;
151 static int hf_tcp_option_md5 = -1;
152 static int hf_tcp_option_qs = -1;
153 static int hf_tcp_option_exp = -1;
154 static int hf_tcp_option_exp_data = -1;
155
156 static int hf_tcp_option_rvbd_probe = -1;
157 static int hf_tcp_option_rvbd_probe_version1 = -1;
158 static int hf_tcp_option_rvbd_probe_version2 = -1;
159 static int hf_tcp_option_rvbd_probe_type1 = -1;
160 static int hf_tcp_option_rvbd_probe_type2 = -1;
161 static int hf_tcp_option_rvbd_probe_optlen = -1;
162 static int hf_tcp_option_rvbd_probe_prober = -1;
163 static int hf_tcp_option_rvbd_probe_proxy = -1;
164 static int hf_tcp_option_rvbd_probe_client = -1;
165 static int hf_tcp_option_rvbd_probe_proxy_port = -1;
166 static int hf_tcp_option_rvbd_probe_appli_ver = -1;
167 static int hf_tcp_option_rvbd_probe_storeid = -1;
168 static int hf_tcp_option_rvbd_probe_flags = -1;
169 static int hf_tcp_option_rvbd_probe_flag_last_notify = -1;
170 static int hf_tcp_option_rvbd_probe_flag_server_connected = -1;
171 static int hf_tcp_option_rvbd_probe_flag_not_cfe = -1;
172 static int hf_tcp_option_rvbd_probe_flag_sslcert = -1;
173 static int hf_tcp_option_rvbd_probe_flag_probe_cache = -1;
174
175 static int hf_tcp_option_rvbd_trpy = -1;
176 static int hf_tcp_option_rvbd_trpy_flags = -1;
177 static int hf_tcp_option_rvbd_trpy_flag_mode = -1;
178 static int hf_tcp_option_rvbd_trpy_flag_oob = -1;
179 static int hf_tcp_option_rvbd_trpy_flag_chksum = -1;
180 static int hf_tcp_option_rvbd_trpy_flag_fw_rst = -1;
181 static int hf_tcp_option_rvbd_trpy_flag_fw_rst_inner = -1;
182 static int hf_tcp_option_rvbd_trpy_flag_fw_rst_probe = -1;
183 static int hf_tcp_option_rvbd_trpy_src = -1;
184 static int hf_tcp_option_rvbd_trpy_dst = -1;
185 static int hf_tcp_option_rvbd_trpy_src_port = -1;
186 static int hf_tcp_option_rvbd_trpy_dst_port = -1;
187 static int hf_tcp_option_rvbd_trpy_client_port = -1;
188
189 static int hf_tcp_ts_relative = -1;
190 static int hf_tcp_ts_delta = -1;
191 static int hf_tcp_option_scps = -1;
192 static int hf_tcp_option_scps_vector = -1;
193 static int hf_tcp_option_scps_binding = -1;
194 static int hf_tcp_option_scps_binding_len = -1;
195 static int hf_tcp_scpsoption_flags_bets = -1;
196 static int hf_tcp_scpsoption_flags_snack1 = -1;
197 static int hf_tcp_scpsoption_flags_snack2 = -1;
198 static int hf_tcp_scpsoption_flags_compress = -1;
199 static int hf_tcp_scpsoption_flags_nlts = -1;
200 static int hf_tcp_scpsoption_flags_resv1 = -1;
201 static int hf_tcp_scpsoption_flags_resv2 = -1;
202 static int hf_tcp_scpsoption_flags_resv3 = -1;
203 static int hf_tcp_scpsoption_connection_id = -1;
204 static int hf_tcp_option_snack = -1;
205 static int hf_tcp_option_snack_offset = -1;
206 static int hf_tcp_option_snack_size = -1;
207 static int hf_tcp_option_snack_le = -1;
208 static int hf_tcp_option_snack_re = -1;
209 static int hf_tcp_option_mood = -1;
210 static int hf_tcp_option_mood_val = -1;
211 static int hf_tcp_option_user_to = -1;
212 static int hf_tcp_option_user_to_granularity = -1;
213 static int hf_tcp_option_user_to_val = -1;
214 static int hf_tcp_proc_src_uid = -1;
215 static int hf_tcp_proc_src_pid = -1;
216 static int hf_tcp_proc_src_uname = -1;
217 static int hf_tcp_proc_src_cmd = -1;
218 static int hf_tcp_proc_dst_uid = -1;
219 static int hf_tcp_proc_dst_pid = -1;
220 static int hf_tcp_proc_dst_uname = -1;
221 static int hf_tcp_proc_dst_cmd = -1;
222 static int hf_tcp_data = -1;
223
224 static gint ett_tcp = -1;
225 static gint ett_tcp_flags = -1;
226 static gint ett_tcp_options = -1;
227 static gint ett_tcp_option_timestamp = -1;
228 static gint ett_tcp_option_mss = -1;
229 static gint ett_tcp_option_wscale = -1;
230 static gint ett_tcp_option_sack = -1;
231 static gint ett_tcp_option_scps = -1;
232 static gint ett_tcp_option_scps_extended = -1;
233 static gint ett_tcp_option_user_to = -1;
234 static gint ett_tcp_option_exp = -1;
235 static gint ett_tcp_option_sack_perm = -1;
236 static gint ett_tcp_analysis = -1;
237 static gint ett_tcp_analysis_faults = -1;
238 static gint ett_tcp_timestamps = -1;
239 static gint ett_tcp_segments = -1;
240 static gint ett_tcp_segment  = -1;
241 static gint ett_tcp_checksum = -1;
242 static gint ett_tcp_process_info = -1;
243
244 static gint ett_tcp_opt_rvbd_probe = -1;
245 static gint ett_tcp_opt_rvbd_probe_flags = -1;
246 static gint ett_tcp_opt_rvbd_trpy = -1;
247 static gint ett_tcp_opt_rvbd_trpy_flags = -1;
248
249 /*
250  *  TCP option
251  */
252
253 #define TCPOPT_NOP              1       /* Padding */
254 #define TCPOPT_EOL              0       /* End of options */
255 #define TCPOPT_MSS              2       /* Segment size negotiating */
256 #define TCPOPT_WINDOW           3       /* Window scaling */
257 #define TCPOPT_SACK_PERM        4       /* SACK Permitted */
258 #define TCPOPT_SACK             5       /* SACK Block */
259 #define TCPOPT_ECHO             6
260 #define TCPOPT_ECHOREPLY        7
261 #define TCPOPT_TIMESTAMP        8       /* Better RTT estimations/PAWS */
262 #define TCPOPT_CC               11
263 #define TCPOPT_CCNEW            12
264 #define TCPOPT_CCECHO           13
265 #define TCPOPT_MD5              19      /* RFC2385 */
266 #define TCPOPT_SCPS             20      /* SCPS Capabilities */
267 #define TCPOPT_SNACK            21      /* SCPS SNACK */
268 #define TCPOPT_RECBOUND         22      /* SCPS Record Boundary */
269 #define TCPOPT_CORREXP          23      /* SCPS Corruption Experienced */
270 #define TCPOPT_MOOD             25      /* RFC5841 TCP Packet Mood */
271 #define TCPOPT_QS               27      /* RFC4782 */
272 #define TCPOPT_USER_TO          28      /* RFC5482 */
273 #define TCPOPT_EXP_FD           0xfd    /* Experimental, reserved */
274 #define TCPOPT_EXP_FE           0xfe    /* Experimental, reserved */
275 /* Non IANA registered option numbers */
276 #define TCPOPT_RVBD_PROBE       76      /* Riverbed probe option */
277 #define TCPOPT_RVBD_TRPY        78      /* Riverbed transparency option */
278
279 /*
280  *     TCP option lengths
281  */
282
283 #define TCPOLEN_MSS            4
284 #define TCPOLEN_WINDOW         3
285 #define TCPOLEN_SACK_PERM      2
286 #define TCPOLEN_SACK_MIN       2
287 #define TCPOLEN_ECHO           6
288 #define TCPOLEN_ECHOREPLY      6
289 #define TCPOLEN_TIMESTAMP     10
290 #define TCPOLEN_CC             6
291 #define TCPOLEN_CCNEW          6
292 #define TCPOLEN_CCECHO         6
293 #define TCPOLEN_MD5           18
294 #define TCPOLEN_SCPS           2
295 #define TCPOLEN_SNACK          6
296 #define TCPOLEN_RECBOUND       2
297 #define TCPOLEN_CORREXP        2
298 #define TCPOLEN_MOOD_MIN       2
299 #define TCPOLEN_QS             8
300 #define TCPOLEN_USER_TO        4
301 #define TCPOLEN_RVBD_PROBE_MIN 3
302 #define TCPOLEN_RVBD_TRPY_MIN 16
303 #define TCPOLEN_EXP_MIN        2
304
305 static const true_false_string tcp_option_user_to_granularity = {
306   "Minutes", "Seconds"
307 };
308
309 static const value_string tcp_option_kind_vs[] = {
310     { TCPOPT_EXP_FD, "Experimental 0xFD" },
311     { TCPOPT_EXP_FE, "Experimental 0xFE" },
312     { TCPOPT_WINDOW, "Window Scale" },
313     { TCPOPT_SACK_PERM, "SACK Permission" },
314     { TCPOPT_MSS, "MSS size" },
315     { TCPOPT_TIMESTAMP, "Timestamp" },
316     { 0, NULL }
317 };
318
319 /* not all of the hf_fields below make sense for TCP but we have to provide
320    them anyways to comply with the api (which was aimed for ip fragment
321    reassembly) */
322 static const fragment_items tcp_segment_items = {
323     &ett_tcp_segment,
324     &ett_tcp_segments,
325     &hf_tcp_segments,
326     &hf_tcp_segment,
327     &hf_tcp_segment_overlap,
328     &hf_tcp_segment_overlap_conflict,
329     &hf_tcp_segment_multiple_tails,
330     &hf_tcp_segment_too_long_fragment,
331     &hf_tcp_segment_error,
332     &hf_tcp_segment_count,
333     &hf_tcp_reassembled_in,
334     &hf_tcp_reassembled_length,
335     "Segments"
336 };
337
338 static dissector_table_t subdissector_table;
339 static heur_dissector_list_t heur_subdissector_list;
340 static dissector_handle_t data_handle;
341 static guint32 tcp_stream_index;
342
343 /* TCP structs and definitions */
344
345 /* **************************************************************************
346  * RTT, relative sequence numbers, window scaling & etc.
347  * **************************************************************************/
348 static gboolean tcp_analyze_seq           = TRUE;
349 static gboolean tcp_relative_seq          = TRUE;
350 static gboolean tcp_track_bytes_in_flight = TRUE;
351 static gboolean tcp_calculate_ts          = FALSE;
352
353 /* SLAB allocator for tcp_unacked structures
354  */
355 SLAB_ITEM_TYPE_DEFINE(tcp_unacked_t)
356 static SLAB_FREE_LIST_DEFINE(tcp_unacked_t)
357 #define TCP_UNACKED_NEW(fi)                 \
358     SLAB_ALLOC(fi, tcp_unacked_t)
359 #define TCP_UNACKED_FREE(fi)                    \
360     SLAB_FREE(fi, tcp_unacked_t)
361
362
363 #define TCP_A_RETRANSMISSION        0x0001
364 #define TCP_A_LOST_PACKET           0x0002
365 #define TCP_A_ACK_LOST_PACKET       0x0004
366 #define TCP_A_KEEP_ALIVE            0x0008
367 #define TCP_A_DUPLICATE_ACK         0x0010
368 #define TCP_A_ZERO_WINDOW           0x0020
369 #define TCP_A_ZERO_WINDOW_PROBE     0x0040
370 #define TCP_A_ZERO_WINDOW_PROBE_ACK 0x0080
371 #define TCP_A_KEEP_ALIVE_ACK        0x0100
372 #define TCP_A_OUT_OF_ORDER          0x0200
373 #define TCP_A_FAST_RETRANSMISSION   0x0400
374 #define TCP_A_WINDOW_UPDATE         0x0800
375 #define TCP_A_WINDOW_FULL           0x1000
376 #define TCP_A_REUSED_PORTS          0x2000
377
378
379 static void
380 process_tcp_payload(tvbuff_t *tvb, volatile int offset, packet_info *pinfo,
381     proto_tree *tree, proto_tree *tcp_tree, int src_port, int dst_port,
382     guint32 seq, guint32 nxtseq, gboolean is_tcp_segment,
383     struct tcp_analysis *tcpd);
384
385
386 struct tcp_analysis *
387 init_tcp_conversation_data(packet_info *pinfo)
388 {
389     struct tcp_analysis *tcpd;
390
391     /* Initialize the tcp protocol data structure to add to the tcp conversation */
392     tcpd=se_alloc0(sizeof(struct tcp_analysis));
393     tcpd->flow1.win_scale=-1;
394     tcpd->flow1.window = G_MAXUINT32;
395     tcpd->flow1.multisegment_pdus=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "tcp_multisegment_pdus");
396     /*
397     tcpd->flow1.username = NULL;
398     tcpd->flow1.command = NULL;
399     */
400     tcpd->flow2.window = G_MAXUINT32;
401     tcpd->flow2.win_scale=-1;
402     tcpd->flow2.multisegment_pdus=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "tcp_multisegment_pdus");
403     /*
404     tcpd->flow2.username = NULL;
405     tcpd->flow2.command = NULL;
406     */
407     tcpd->acked_table=se_tree_create_non_persistent(EMEM_TREE_TYPE_RED_BLACK, "tcp_analyze_acked_table");
408     tcpd->ts_first.secs=pinfo->fd->abs_ts.secs;
409     tcpd->ts_first.nsecs=pinfo->fd->abs_ts.nsecs;
410     tcpd->ts_prev.secs=pinfo->fd->abs_ts.secs;
411     tcpd->ts_prev.nsecs=pinfo->fd->abs_ts.nsecs;
412     tcpd->flow1.valid_bif = 1;
413     tcpd->flow2.valid_bif = 1;
414     tcpd->stream = tcp_stream_index++;
415
416     return tcpd;
417 }
418
419 struct tcp_analysis *
420 get_tcp_conversation_data(conversation_t *conv, packet_info *pinfo)
421 {
422     int direction;
423     struct tcp_analysis *tcpd;
424
425     /* Did the caller supply the conversation pointer? */
426     if( conv==NULL )
427             conv = find_or_create_conversation(pinfo);
428
429     /* Get the data for this conversation */
430     tcpd=conversation_get_proto_data(conv, proto_tcp);
431
432     /* If the conversation was just created or it matched a
433      * conversation with template options, tcpd will not
434      * have been initialized. So, initialize
435      * a new tcpd structure for the conversation.
436      */
437     if (!tcpd) {
438         tcpd = init_tcp_conversation_data(pinfo);
439         conversation_add_proto_data(conv, proto_tcp, tcpd);
440     }
441
442     if (!tcpd) {
443       return NULL;
444     }
445
446     /* check direction and get ua lists */
447     direction=CMP_ADDRESS(&pinfo->src, &pinfo->dst);
448     /* if the addresses are equal, match the ports instead */
449     if(direction==0) {
450         direction= (pinfo->srcport > pinfo->destport) ? 1 : -1;
451     }
452     if(direction>=0){
453         tcpd->fwd=&(tcpd->flow1);
454         tcpd->rev=&(tcpd->flow2);
455     } else {
456         tcpd->fwd=&(tcpd->flow2);
457         tcpd->rev=&(tcpd->flow1);
458     }
459
460     tcpd->ta=NULL;
461     return tcpd;
462 }
463
464 /* Attach process info to a flow */
465 /* XXX - We depend on the TCP dissector finding the conversation first */
466 void
467 add_tcp_process_info(guint32 frame_num, address *local_addr, address *remote_addr, guint16 local_port, guint16 remote_port, guint32 uid, guint32 pid, gchar *username, gchar *command) {
468     conversation_t *conv;
469     struct tcp_analysis *tcpd;
470     tcp_flow_t *flow = NULL;
471
472     conv = find_conversation(frame_num, local_addr, remote_addr, PT_TCP, local_port, remote_port, 0);
473     if (!conv) {
474         return;
475     }
476
477     tcpd = conversation_get_proto_data(conv, proto_tcp);
478     if (!tcpd) {
479         return;
480     }
481
482     if (CMP_ADDRESS(local_addr, &conv->key_ptr->addr1) == 0 && local_port == conv->key_ptr->port1) {
483         flow = &tcpd->flow1;
484     } else if (CMP_ADDRESS(remote_addr, &conv->key_ptr->addr1) == 0 && remote_port == conv->key_ptr->port1) {
485         flow = &tcpd->flow2;
486     }
487     if (!flow || flow->command) {
488         return;
489     }
490
491     flow->process_uid = uid;
492     flow->process_pid = pid;
493     flow->username = se_strdup(username);
494     flow->command = se_strdup(command);
495 }
496
497
498 /* Calculate the timestamps relative to this conversation */
499 static void
500 tcp_calculate_timestamps(packet_info *pinfo, struct tcp_analysis *tcpd,
501             struct tcp_per_packet_data_t *tcppd)
502 {
503     if( !tcppd ) {
504         tcppd = se_alloc(sizeof(struct tcp_per_packet_data_t));
505         p_add_proto_data(pinfo->fd, proto_tcp, tcppd);
506     }
507
508     if (!tcpd)
509         return;
510
511     nstime_delta(&tcppd->ts_del, &pinfo->fd->abs_ts, &tcpd->ts_prev);
512
513     tcpd->ts_prev.secs=pinfo->fd->abs_ts.secs;
514     tcpd->ts_prev.nsecs=pinfo->fd->abs_ts.nsecs;
515 }
516
517 /* Add a subtree with the timestamps relative to this conversation */
518 static void
519 tcp_print_timestamps(packet_info *pinfo, tvbuff_t *tvb, proto_tree *parent_tree, struct tcp_analysis *tcpd, struct tcp_per_packet_data_t *tcppd)
520 {
521     proto_item  *item;
522     proto_tree  *tree;
523     nstime_t    ts;
524
525     if (!tcpd)
526         return;
527
528     item=proto_tree_add_text(parent_tree, tvb, 0, 0, "Timestamps");
529     PROTO_ITEM_SET_GENERATED(item);
530     tree=proto_item_add_subtree(item, ett_tcp_timestamps);
531
532     nstime_delta(&ts, &pinfo->fd->abs_ts, &tcpd->ts_first);
533     item = proto_tree_add_time(tree, hf_tcp_ts_relative, tvb, 0, 0, &ts);
534     PROTO_ITEM_SET_GENERATED(item);
535
536     if( !tcppd )
537         tcppd = p_get_proto_data(pinfo->fd, proto_tcp);
538
539     if( tcppd ) {
540         item = proto_tree_add_time(tree, hf_tcp_ts_delta, tvb, 0, 0,
541             &tcppd->ts_del);
542         PROTO_ITEM_SET_GENERATED(item);
543     }
544 }
545
546 static void
547 print_pdu_tracking_data(packet_info *pinfo, tvbuff_t *tvb, proto_tree *tcp_tree, struct tcp_multisegment_pdu *msp)
548 {
549     proto_item *item;
550
551     col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[Continuation to #%u] ", msp->first_frame);
552     item=proto_tree_add_uint(tcp_tree, hf_tcp_continuation_to,
553         tvb, 0, 0, msp->first_frame);
554     PROTO_ITEM_SET_GENERATED(item);
555 }
556
557 /* if we know that a PDU starts inside this segment, return the adjusted
558    offset to where that PDU starts or just return offset back
559    and let TCP try to find out what it can about this segment
560 */
561 static int
562 scan_for_next_pdu(tvbuff_t *tvb, proto_tree *tcp_tree, packet_info *pinfo, int offset, guint32 seq, guint32 nxtseq, emem_tree_t *multisegment_pdus)
563 {
564     struct tcp_multisegment_pdu *msp=NULL;
565
566     if(!pinfo->fd->flags.visited){
567         msp=se_tree_lookup32_le(multisegment_pdus, seq-1);
568         if(msp){
569             /* If this is a continuation of a PDU started in a
570              * previous segment we need to update the last_frame
571              * variables.
572             */
573             if(seq>msp->seq && seq<msp->nxtpdu){
574                 msp->last_frame=pinfo->fd->num;
575                 msp->last_frame_time=pinfo->fd->abs_ts;
576                 print_pdu_tracking_data(pinfo, tvb, tcp_tree, msp);
577             }
578
579             /* If this segment is completely within a previous PDU
580              * then we just skip this packet
581              */
582             if(seq>msp->seq && nxtseq<=msp->nxtpdu){
583                 return -1;
584             }
585             if(seq<msp->nxtpdu && nxtseq>msp->nxtpdu){
586                 offset+=msp->nxtpdu-seq;
587                 return offset;
588             }
589
590         }
591     } else {
592         /* First we try to find the start and transfer time for a PDU.
593          * We only print this for the very first segment of a PDU
594          * and only for PDUs spanning multiple segments.
595          * Se we look for if there was any multisegment PDU started
596          * just BEFORE the end of this segment. I.e. either inside this
597          * segment or in a previous segment.
598          * Since this might also match PDUs that are completely within
599          * this segment we also verify that the found PDU does span
600          * beyond the end of this segment.
601          */
602         msp=se_tree_lookup32_le(multisegment_pdus, nxtseq-1);
603         if(msp){
604             if(pinfo->fd->num==msp->first_frame) {
605                 proto_item *item;
606                 nstime_t ns;
607
608                 item=proto_tree_add_uint(tcp_tree, hf_tcp_pdu_last_frame, tvb, 0, 0, msp->last_frame);
609                 PROTO_ITEM_SET_GENERATED(item);
610
611                 nstime_delta(&ns, &msp->last_frame_time, &pinfo->fd->abs_ts);
612                 item = proto_tree_add_time(tcp_tree, hf_tcp_pdu_time,
613                         tvb, 0, 0, &ns);
614                 PROTO_ITEM_SET_GENERATED(item);
615             }
616         }
617
618         /* Second we check if this segment is part of a PDU started
619          * prior to the segment (seq-1)
620          */
621         msp=se_tree_lookup32_le(multisegment_pdus, seq-1);
622         if(msp){
623             /* If this segment is completely within a previous PDU
624              * then we just skip this packet
625              */
626             if(seq>msp->seq && nxtseq<=msp->nxtpdu){
627                 print_pdu_tracking_data(pinfo, tvb, tcp_tree, msp);
628                 return -1;
629             }
630
631             if(seq<msp->nxtpdu && nxtseq>msp->nxtpdu){
632                 offset+=msp->nxtpdu-seq;
633                 return offset;
634             }
635         }
636
637     }
638     return offset;
639 }
640
641 /* if we saw a PDU that extended beyond the end of the segment,
642    use this function to remember where the next pdu starts
643 */
644 struct tcp_multisegment_pdu *
645 pdu_store_sequencenumber_of_next_pdu(packet_info *pinfo, guint32 seq, guint32 nxtpdu, emem_tree_t *multisegment_pdus)
646 {
647     struct tcp_multisegment_pdu *msp;
648
649     msp=se_alloc(sizeof(struct tcp_multisegment_pdu));
650     msp->nxtpdu=nxtpdu;
651     msp->seq=seq;
652     msp->first_frame=pinfo->fd->num;
653     msp->last_frame=pinfo->fd->num;
654     msp->last_frame_time=pinfo->fd->abs_ts;
655     msp->flags=0;
656     se_tree_insert32(multisegment_pdus, seq, (void *)msp);
657     return msp;
658 }
659
660 /* This is called for SYN and SYN+ACK packets and the purpose is to verify
661  * that we have seen window scaling in both directions.
662  * If we cant find window scaling being set in both directions
663  * that means it was present in the SYN but not in the SYN+ACK
664  * (or the SYN was missing) and then we disable the window scaling
665  * for this tcp session.
666  */
667 static void
668 verify_tcp_window_scaling(gboolean is_synack, struct tcp_analysis *tcpd)
669 {
670     if( tcpd->fwd->win_scale==-1 ) {
671         /* We know window scaling will not be used as:
672          * a) this is the SYN and it does not have the WS option
673          *    (we set the reverse win_scale also in case we miss
674          *    the SYN/ACK)
675          * b) this is the SYN/ACK and either the SYN packet has not
676          *    been seen or it did have the WS option. As the SYN/ACK
677          *    does not have the WS option, window scaling will not be used.
678          *
679          * Setting win_scale to -2 to indicate that we can
680          * trust the window_size value in the TCP header.
681          */
682         tcpd->fwd->win_scale = -2;
683         tcpd->rev->win_scale = -2;
684
685     } else if( is_synack && tcpd->rev->win_scale==-2 ) {
686         /* The SYN/ACK has the WS option, while the SYN did not,
687          * this should not happen, but the endpoints will not
688          * have used window scaling, so we will neither
689          */
690         tcpd->fwd->win_scale = -2;
691     }
692 }
693
694 /* if we saw a window scaling option, store it for future reference
695 */
696 static void
697 pdu_store_window_scale_option(guint8 ws, struct tcp_analysis *tcpd)
698 {
699     if (tcpd)
700         tcpd->fwd->win_scale=ws;
701 }
702
703 /* when this function returns, it will (if createflag) populate the ta pointer.
704  */
705 static void
706 tcp_analyze_get_acked_struct(guint32 frame, gboolean createflag, struct tcp_analysis *tcpd)
707 {
708     if (!tcpd)
709         return;
710
711     tcpd->ta=se_tree_lookup32(tcpd->acked_table, frame);
712     if((!tcpd->ta) && createflag){
713         tcpd->ta=se_alloc0(sizeof(struct tcp_acked));
714         se_tree_insert32(tcpd->acked_table, frame, (void *)tcpd->ta);
715     }
716 }
717
718
719 /* fwd contains a list of all segments processed but not yet ACKed in the
720  *     same direction as the current segment.
721  * rev contains a list of all segments received but not yet ACKed in the
722  *     opposite direction to the current segment.
723  *
724  * New segments are always added to the head of the fwd/rev lists.
725  *
726  */
727 static void
728 tcp_analyze_sequence_number(packet_info *pinfo, guint32 seq, guint32 ack, guint32 seglen, guint16 flags, guint32 window, struct tcp_analysis *tcpd)
729 {
730     tcp_unacked_t *ual=NULL;
731     tcp_unacked_t *prevual=NULL;
732     guint32 nextseq;
733     int ackcount;
734
735 #ifdef REMOVED
736 printf("analyze_sequence numbers   frame:%u  direction:%s\n",pinfo->fd->num,direction>=0?"FWD":"REW");
737 printf("FWD list lastflags:0x%04x base_seq:0x%08x:\n",tcpd->fwd->lastsegmentflags,tcpd->fwd->base_seq);for(ual=tcpd->fwd->segments;ual;ual=ual->next)printf("Frame:%d Seq:%d Nextseq:%d\n",ual->frame,ual->seq,ual->nextseq);
738 printf("REV list lastflags:0x%04x base_seq:0x%08x:\n",tcpd->rev->lastsegmentflags,tcpd->rev->base_seq);for(ual=tcpd->rev->segments;ual;ual=ual->next)printf("Frame:%d Seq:%d Nextseq:%d\n",ual->frame,ual->seq,ual->nextseq);
739 #endif
740
741     if (!tcpd) {
742         return;
743     }
744
745     /* if this is the first segment for this list we need to store the
746      * base_seq
747      *
748      * Start relative seq and ack numbers at 1 if this
749      * is not a SYN packet. This makes the relative
750      * seq/ack numbers to be displayed correctly in the
751      * event that the SYN or SYN/ACK packet is not seen
752      * (this solves bug 1542)
753      */
754     if(tcpd->fwd->base_seq==0){
755         tcpd->fwd->base_seq = (flags & TH_SYN) ? seq : seq-1;
756     }
757
758     /* Only store reverse sequence if this isn't the SYN
759      * There's no guarantee that the ACK field of a SYN
760      * contains zeros; get the ISN from the first segment
761      * with the ACK bit set instead (usually the SYN/ACK).
762      */
763     if( (tcpd->rev->base_seq==0) && (flags & TH_ACK) ){
764         tcpd->rev->base_seq = (flags & TH_SYN) ? ack : ack-1;
765     }
766
767     if( flags & TH_ACK ){
768         tcpd->rev->valid_bif = 1;
769     }
770
771     /* ZERO WINDOW PROBE
772      * it is a zero window probe if
773      *  the sequnece number is the next expected one
774      *  the window in the other direction is 0
775      *  the segment is exactly 1 byte
776      */
777 /*QQQ tested*/
778     if( seglen==1
779     &&  seq==tcpd->fwd->nextseq
780     &&  tcpd->rev->window==0 ){
781         if(!tcpd->ta){
782             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
783         }
784         tcpd->ta->flags|=TCP_A_ZERO_WINDOW_PROBE;
785         goto finished_fwd;
786     }
787
788
789     /* ZERO WINDOW
790      * a zero window packet has window == 0   but none of the SYN/FIN/RST set
791      */
792 /*QQQ tested*/
793     if( window==0
794     && (flags&(TH_RST|TH_FIN|TH_SYN))==0 ){
795         if(!tcpd->ta){
796             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
797         }
798         tcpd->ta->flags|=TCP_A_ZERO_WINDOW;
799     }
800
801
802     /* LOST PACKET
803      * If this segment is beyond the last seen nextseq we must
804      * have missed some previous segment
805      *
806      * We only check for this if we have actually seen segments prior to this
807      * one.
808      * RST packets are not checked for this.
809      */
810     if( tcpd->fwd->nextseq
811     &&  GT_SEQ(seq, tcpd->fwd->nextseq)
812     &&  (flags&(TH_RST))==0 ){
813         if(!tcpd->ta){
814             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
815         }
816         tcpd->ta->flags|=TCP_A_LOST_PACKET;
817
818         /* Disable BiF until an ACK is seen in the other direction */
819         tcpd->fwd->valid_bif = 0;
820     }
821
822
823     /* KEEP ALIVE
824      * a keepalive contains 0 or 1 bytes of data and starts one byte prior
825      * to what should be the next sequence number.
826      * SYN/FIN/RST segments are never keepalives
827      */
828 /*QQQ tested */
829     if( (seglen==0||seglen==1)
830     &&  seq==(tcpd->fwd->nextseq-1)
831     &&  (flags&(TH_SYN|TH_FIN|TH_RST))==0 ){
832         if(!tcpd->ta){
833             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
834         }
835         tcpd->ta->flags|=TCP_A_KEEP_ALIVE;
836     }
837
838     /* WINDOW UPDATE
839      * A window update is a 0 byte segment with the same SEQ/ACK numbers as
840      * the previous seen segment and with a new window value
841      */
842     if( seglen==0
843     &&  window
844     &&  window!=tcpd->fwd->window
845     &&  seq==tcpd->fwd->nextseq
846     &&  ack==tcpd->fwd->lastack
847     &&  (flags&(TH_SYN|TH_FIN|TH_RST))==0 ){
848         if(!tcpd->ta){
849             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
850         }
851         tcpd->ta->flags|=TCP_A_WINDOW_UPDATE;
852     }
853
854
855     /* WINDOW FULL
856      * If we know the window scaling
857      * and if this segment contains data ang goes all the way to the
858      * edge of the advertized window
859      * then we mark it as WINDOW FULL
860      * SYN/RST/FIN packets are never WINDOW FULL
861      */
862 /*QQQ tested*/
863     if( seglen>0
864     &&  tcpd->rev->win_scale!=-1
865     &&  (seq+seglen)==(tcpd->rev->lastack+(tcpd->rev->window<<(tcpd->rev->win_scale==-2?0:tcpd->rev->win_scale)))
866     &&  (flags&(TH_SYN|TH_FIN|TH_RST))==0 ){
867         if(!tcpd->ta){
868             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
869         }
870         tcpd->ta->flags|=TCP_A_WINDOW_FULL;
871     }
872
873
874     /* KEEP ALIVE ACK
875      * It is a keepalive ack if it repeats the previous ACK and if
876      * the last segment in the reverse direction was a keepalive
877      */
878 /*QQQ tested*/
879     if( seglen==0
880     &&  window
881     &&  window==tcpd->fwd->window
882     &&  seq==tcpd->fwd->nextseq
883     &&  ack==tcpd->fwd->lastack
884     && (tcpd->rev->lastsegmentflags&TCP_A_KEEP_ALIVE)
885     &&  (flags&(TH_SYN|TH_FIN|TH_RST))==0 ){
886         if(!tcpd->ta){
887             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
888         }
889         tcpd->ta->flags|=TCP_A_KEEP_ALIVE_ACK;
890         goto finished_fwd;
891     }
892
893
894     /* ZERO WINDOW PROBE ACK
895      * It is a zerowindowprobe ack if it repeats the previous ACK and if
896      * the last segment in the reverse direction was a zerowindowprobe
897      * It also repeats the previous zero window indication
898      */
899 /*QQQ tested*/
900     if( seglen==0
901     &&  window==0
902     &&  window==tcpd->fwd->window
903     &&  seq==tcpd->fwd->nextseq
904     &&  ack==tcpd->fwd->lastack
905     && (tcpd->rev->lastsegmentflags&TCP_A_ZERO_WINDOW_PROBE)
906     &&  (flags&(TH_SYN|TH_FIN|TH_RST))==0 ){
907         if(!tcpd->ta){
908             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
909         }
910         tcpd->ta->flags|=TCP_A_ZERO_WINDOW_PROBE_ACK;
911         goto finished_fwd;
912     }
913
914
915     /* DUPLICATE ACK
916      * It is a duplicate ack if window/seq/ack is the same as the previous
917      * segment and if the segment length is 0
918      */
919     if( seglen==0
920     &&  window
921     &&  window==tcpd->fwd->window
922     &&  seq==tcpd->fwd->nextseq
923     &&  ack==tcpd->fwd->lastack
924     &&  (flags&(TH_SYN|TH_FIN|TH_RST))==0 ){
925         tcpd->fwd->dupacknum++;
926         if(!tcpd->ta){
927             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
928         }
929         tcpd->ta->flags|=TCP_A_DUPLICATE_ACK;
930         tcpd->ta->dupack_num=tcpd->fwd->dupacknum;
931         tcpd->ta->dupack_frame=tcpd->fwd->lastnondupack;
932     }
933
934
935
936 finished_fwd:
937     /* If this was NOT a dupack we must reset the dupack counters */
938     if( (!tcpd->ta) || !(tcpd->ta->flags&TCP_A_DUPLICATE_ACK) ){
939         tcpd->fwd->lastnondupack=pinfo->fd->num;
940         tcpd->fwd->dupacknum=0;
941     }
942
943
944     /* ACKED LOST PACKET
945      * If this segment acks beyond the 'max seq to be acked' in the other direction
946      * then that means we have missed packets going in the
947      * other direction
948      *
949      * We only check this if we have actually seen some seq numbers
950      * in the other direction.
951      */
952     if( tcpd->rev->maxseqtobeacked
953     &&  GT_SEQ(ack, tcpd->rev->maxseqtobeacked )
954     &&  (flags&(TH_ACK))!=0 ){
955 /*QQQ tested*/
956         if(!tcpd->ta){
957             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
958         }
959         tcpd->ta->flags|=TCP_A_ACK_LOST_PACKET;
960         /* update 'max seq to be acked' in the other direction so we dont get
961          * this indication again.
962          */
963         tcpd->rev->maxseqtobeacked=tcpd->rev->nextseq;
964     }
965
966
967     /* RETRANSMISSION/FAST RETRANSMISSION/OUT-OF-ORDER
968      * If the segments contains data and if it does not advance
969      * sequence number it must be either of these three.
970      * Only test for this if we know what the seq number should be
971      * (tcpd->fwd->nextseq)
972      *
973      * Note that a simple KeepAlive is not a retransmission
974      */
975     if( seglen>0
976     &&  tcpd->fwd->nextseq
977     &&  (LT_SEQ(seq, tcpd->fwd->nextseq)) ){
978         guint64 t;
979
980         if(tcpd->ta && (tcpd->ta->flags&TCP_A_KEEP_ALIVE) ){
981             goto finished_checking_retransmission_type;
982         }
983
984         /* If there were >=2 duplicate ACKs in the reverse direction
985          * (there might be duplicate acks missing from the trace)
986          * and if this sequence number matches those ACKs
987          * and if the packet occurs within 20ms of the last
988          * duplicate ack
989          * then this is a fast retransmission
990          */
991         t=(pinfo->fd->abs_ts.secs-tcpd->rev->lastacktime.secs)*1000000000;
992         t=t+(pinfo->fd->abs_ts.nsecs)-tcpd->rev->lastacktime.nsecs;
993         if( tcpd->rev->dupacknum>=2
994         &&  tcpd->rev->lastack==seq
995         &&  t<20000000 ){
996             if(!tcpd->ta){
997                 tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
998             }
999             tcpd->ta->flags|=TCP_A_FAST_RETRANSMISSION;
1000             goto finished_checking_retransmission_type;
1001         }
1002
1003         /* If the segment came <3ms since the segment with the highest
1004          * seen sequence number and it doesn't look like a retransmission
1005          * then it is an OUT-OF-ORDER segment.
1006          *   (3ms is an arbitrary number)
1007          */
1008         t=(pinfo->fd->abs_ts.secs-tcpd->fwd->nextseqtime.secs)*1000000000;
1009         t=t+(pinfo->fd->abs_ts.nsecs)-tcpd->fwd->nextseqtime.nsecs;
1010         if( t<3000000
1011         && tcpd->fwd->nextseq != seq + seglen ){
1012             if(!tcpd->ta){
1013                 tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
1014             }
1015             tcpd->ta->flags|=TCP_A_OUT_OF_ORDER;
1016             goto finished_checking_retransmission_type;
1017         }
1018
1019         /* Then it has to be a generic retransmission */
1020         if(!tcpd->ta){
1021             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
1022         }
1023         tcpd->ta->flags|=TCP_A_RETRANSMISSION;
1024         nstime_delta(&tcpd->ta->rto_ts, &pinfo->fd->abs_ts, &tcpd->fwd->nextseqtime);
1025         tcpd->ta->rto_frame=tcpd->fwd->nextseqframe;
1026     }
1027 finished_checking_retransmission_type:
1028
1029
1030         nextseq = seq+seglen;
1031         if (seglen || flags&(TH_SYN|TH_FIN)) {
1032         /* add this new sequence number to the fwd list */
1033         TCP_UNACKED_NEW(ual);
1034         ual->next=tcpd->fwd->segments;
1035         tcpd->fwd->segments=ual;
1036         ual->frame=pinfo->fd->num;
1037         ual->seq=seq;
1038         ual->ts=pinfo->fd->abs_ts;
1039
1040         /* next sequence number is seglen bytes away, plus SYN/FIN which counts as one byte */
1041         if( (flags&(TH_SYN|TH_FIN)) ){
1042         nextseq+=1;
1043             }
1044             ual->nextseq=nextseq;
1045         }
1046
1047     /* Store the highest number seen so far for nextseq so we can detect
1048      * when we receive segments that arrive with a "hole"
1049      * If we dont have anything since before, just store what we got.
1050      * ZeroWindowProbes are special and dont really advance the nextseq
1051      */
1052     if(GT_SEQ(nextseq, tcpd->fwd->nextseq) || !tcpd->fwd->nextseq) {
1053         if( !tcpd->ta || !(tcpd->ta->flags&TCP_A_ZERO_WINDOW_PROBE) ){
1054             tcpd->fwd->nextseq=nextseq;
1055             tcpd->fwd->nextseqframe=pinfo->fd->num;
1056             tcpd->fwd->nextseqtime.secs=pinfo->fd->abs_ts.secs;
1057             tcpd->fwd->nextseqtime.nsecs=pinfo->fd->abs_ts.nsecs;
1058         }
1059     }
1060
1061     /* Store the highest continuous seq number seen so far for 'max seq to be acked',
1062      so we can detect TCP_A_ACK_LOST_PACKET contition
1063      */
1064     if(EQ_SEQ(seq, tcpd->fwd->maxseqtobeacked) || !tcpd->fwd->maxseqtobeacked) {
1065         if( !tcpd->ta || !(tcpd->ta->flags&TCP_A_ZERO_WINDOW_PROBE) ){
1066             tcpd->fwd->maxseqtobeacked=tcpd->fwd->nextseq;
1067         }
1068     }
1069
1070
1071     /* remember what the ack/window is so we can track window updates and retransmissions */
1072     tcpd->fwd->window=window;
1073     tcpd->fwd->lastack=ack;
1074     tcpd->fwd->lastacktime.secs=pinfo->fd->abs_ts.secs;
1075     tcpd->fwd->lastacktime.nsecs=pinfo->fd->abs_ts.nsecs;
1076
1077
1078     /* if there were any flags set for this segment we need to remember them
1079      * we only remember the flags for the very last segment though.
1080      */
1081     if(tcpd->ta){
1082         tcpd->fwd->lastsegmentflags=tcpd->ta->flags;
1083     } else {
1084         tcpd->fwd->lastsegmentflags=0;
1085     }
1086
1087
1088     /* remove all segments this ACKs and we dont need to keep around any more
1089      */
1090     ackcount=0;
1091     prevual = NULL;
1092     ual = tcpd->rev->segments;
1093     while(ual){
1094         tcp_unacked_t *tmpual;
1095
1096         /* If this ack matches the segment, process accordingly */
1097         if(ack==ual->nextseq){
1098             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
1099             tcpd->ta->frame_acked=ual->frame;
1100             nstime_delta(&tcpd->ta->ts, &pinfo->fd->abs_ts, &ual->ts);
1101         }
1102         /* If this acknowledges a segment prior to this one, leave this segment alone and move on */
1103         else if (GT_SEQ(ual->nextseq,ack)){
1104             prevual = ual;
1105             ual = ual->next;
1106             continue;
1107         }
1108
1109         /* This segment is old, or an exact match.  Delete the segment from the list */
1110         ackcount++;
1111         tmpual=ual->next;
1112
1113         if (tcpd->rev->scps_capable) {
1114           /* Track largest segment successfully sent for SNACK analysis*/
1115           if ((ual->nextseq - ual->seq) > tcpd->fwd->maxsizeacked){
1116             tcpd->fwd->maxsizeacked = (ual->nextseq - ual->seq);
1117           }
1118         }
1119
1120         if (!prevual){
1121             tcpd->rev->segments = tmpual;
1122             TCP_UNACKED_FREE(ual);
1123             ual = tmpual;
1124         }
1125         else{
1126             prevual->next = tmpual;
1127             TCP_UNACKED_FREE(ual);
1128             ual = tmpual;
1129         }
1130     }
1131
1132     /* how many bytes of data are there in flight after this frame
1133      * was sent
1134      */
1135     ual=tcpd->fwd->segments;
1136     if (tcp_track_bytes_in_flight && seglen!=0 && ual && tcpd->fwd->valid_bif) {
1137         guint32 first_seq, last_seq, in_flight;
1138
1139         first_seq = ual->seq - tcpd->fwd->base_seq;
1140         last_seq = ual->nextseq - tcpd->fwd->base_seq;
1141         while (ual) {
1142             if ((ual->nextseq-tcpd->fwd->base_seq)>last_seq) {
1143                 last_seq = ual->nextseq-tcpd->fwd->base_seq;
1144             }
1145             if ((ual->seq-tcpd->fwd->base_seq)<first_seq) {
1146                 first_seq = ual->seq-tcpd->fwd->base_seq;
1147             }
1148             ual = ual->next;
1149         }
1150         in_flight = last_seq-first_seq;
1151
1152         if (in_flight>0 && in_flight<2000000000) {
1153             if(!tcpd->ta){
1154                 tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
1155             }
1156             tcpd->ta->bytes_in_flight = in_flight;
1157         }
1158     }
1159
1160 }
1161
1162 /*
1163  * Prints results of the sequence number analysis concerning tcp segments
1164  * retransmitted or out-of-order
1165  */
1166 static void
1167 tcp_sequence_number_analysis_print_retransmission(packet_info * pinfo,
1168                           tvbuff_t * tvb,
1169                           proto_tree * flags_tree,
1170                           struct tcp_acked *ta
1171                           )
1172 {
1173     proto_item * flags_item;
1174
1175     /* TCP Rentransmission */
1176     if (ta->flags & TCP_A_RETRANSMISSION) {
1177         flags_item=proto_tree_add_none_format(flags_tree,
1178                                               hf_tcp_analysis_retransmission,
1179                                               tvb, 0, 0,
1180                                               "This frame is a (suspected) "
1181                                               "retransmission"
1182             );
1183         PROTO_ITEM_SET_GENERATED(flags_item);
1184         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_NOTE,
1185                                "Retransmission (suspected)");
1186
1187         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[TCP Retransmission] ");
1188
1189         if (ta->rto_ts.secs || ta->rto_ts.nsecs) {
1190             flags_item = proto_tree_add_time(flags_tree, hf_tcp_analysis_rto,
1191                                              tvb, 0, 0, &ta->rto_ts);
1192             PROTO_ITEM_SET_GENERATED(flags_item);
1193             flags_item=proto_tree_add_uint(flags_tree, hf_tcp_analysis_rto_frame,
1194                                            tvb, 0, 0, ta->rto_frame);
1195             PROTO_ITEM_SET_GENERATED(flags_item);
1196         }
1197     }
1198     /* TCP Fast Rentransmission */
1199     if (ta->flags & TCP_A_FAST_RETRANSMISSION) {
1200         flags_item=proto_tree_add_none_format(flags_tree,
1201                                               hf_tcp_analysis_fast_retransmission,
1202                                               tvb, 0, 0,
1203                                               "This frame is a (suspected) fast"
1204                                               " retransmission"
1205             );
1206         PROTO_ITEM_SET_GENERATED(flags_item);
1207         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_NOTE,
1208                                "Fast retransmission (suspected)");
1209         flags_item=proto_tree_add_none_format(flags_tree,
1210                                               hf_tcp_analysis_retransmission,
1211                                               tvb, 0, 0,
1212                                               "This frame is a (suspected) "
1213                                               "retransmission"
1214             );
1215         PROTO_ITEM_SET_GENERATED(flags_item);
1216         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO,
1217                                "[TCP Fast Retransmission] ");
1218     }
1219     /* TCP Out-Of-Order */
1220     if (ta->flags & TCP_A_OUT_OF_ORDER) {
1221         flags_item=proto_tree_add_none_format(flags_tree,
1222                                               hf_tcp_analysis_out_of_order,
1223                                               tvb, 0, 0,
1224                                               "This frame is a (suspected) "
1225                                               "out-of-order segment"
1226             );
1227         PROTO_ITEM_SET_GENERATED(flags_item);
1228         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_WARN,
1229                                "Out-Of-Order segment");
1230         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[TCP Out-Of-Order] ");
1231     }
1232 }
1233
1234 /* Prints results of the sequence number analysis concerning reused ports */
1235 static void
1236 tcp_sequence_number_analysis_print_reused(packet_info * pinfo,
1237                       tvbuff_t * tvb,
1238                       proto_tree * flags_tree,
1239                       struct tcp_acked *ta
1240                       )
1241 {
1242     proto_item * flags_item;
1243
1244     /* TCP Ports Reused */
1245     if (ta->flags & TCP_A_REUSED_PORTS) {
1246         flags_item=proto_tree_add_none_format(flags_tree,
1247                                               hf_tcp_analysis_reused_ports,
1248                                               tvb, 0, 0,
1249                                               "A new tcp session is started with the same "
1250                                               "ports as an earlier session in this trace"
1251             );
1252         PROTO_ITEM_SET_GENERATED(flags_item);
1253         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_NOTE,
1254                                "TCP Port numbers reused for new session");
1255         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO,
1256                                "[TCP Port numbers reused] ");
1257     }
1258 }
1259
1260 /* Prints results of the sequence number analysis concerning lost tcp segments */
1261 static void
1262 tcp_sequence_number_analysis_print_lost(packet_info * pinfo,
1263                     tvbuff_t * tvb,
1264                     proto_tree * flags_tree,
1265                     struct tcp_acked *ta
1266                     )
1267 {
1268     proto_item * flags_item;
1269
1270     /* TCP Lost Segment */
1271     if (ta->flags & TCP_A_LOST_PACKET) {
1272         flags_item=proto_tree_add_none_format(flags_tree,
1273                                               hf_tcp_analysis_lost_packet,
1274                                               tvb, 0, 0,
1275                                               "A segment before this frame was "
1276                                               "lost"
1277             );
1278         PROTO_ITEM_SET_GENERATED(flags_item);
1279         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_WARN,
1280                                "Previous segment lost (common at capture start)");
1281         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO,
1282                                "[TCP Previous segment lost] ");
1283     }
1284     /* TCP Ack lost segment */
1285     if (ta->flags & TCP_A_ACK_LOST_PACKET) {
1286         flags_item=proto_tree_add_none_format(flags_tree,
1287                                               hf_tcp_analysis_ack_lost_packet,
1288                                               tvb, 0, 0,
1289                                               "This frame ACKs a segment we have "
1290                                               "not seen (lost?)"
1291             );
1292         PROTO_ITEM_SET_GENERATED(flags_item);
1293         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_WARN,
1294                                "ACKed lost segment (common at capture start)");
1295         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO,
1296                                "[TCP ACKed lost segment] ");
1297     }
1298 }
1299
1300 /* Prints results of the sequence number analysis concerning tcp window */
1301 static void
1302 tcp_sequence_number_analysis_print_window(packet_info * pinfo,
1303                       tvbuff_t * tvb,
1304                       proto_tree * flags_tree,
1305                       struct tcp_acked *ta
1306                       )
1307 {
1308     proto_item * flags_item;
1309
1310     /* TCP Window Update */
1311     if (ta->flags & TCP_A_WINDOW_UPDATE) {
1312         flags_item=proto_tree_add_none_format(flags_tree,
1313                                               hf_tcp_analysis_window_update,
1314                                               tvb, 0, 0,
1315                                               "This is a tcp window update"
1316             );
1317         PROTO_ITEM_SET_GENERATED(flags_item);
1318         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_CHAT,
1319                                "Window update");
1320         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[TCP Window Update] ");
1321     }
1322     /* TCP Full Window */
1323     if (ta->flags & TCP_A_WINDOW_FULL) {
1324         flags_item=proto_tree_add_none_format(flags_tree,
1325                                               hf_tcp_analysis_window_full,
1326                                               tvb, 0, 0,
1327                                               "The transmission window is now "
1328                                               "completely full"
1329             );
1330         PROTO_ITEM_SET_GENERATED(flags_item);
1331         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_WARN,
1332                                "Window is full");
1333         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[TCP Window Full] ");
1334     }
1335 }
1336
1337 /* Prints results of the sequence number analysis concerning tcp keepalive */
1338 static void
1339 tcp_sequence_number_analysis_print_keepalive(packet_info * pinfo,
1340                       tvbuff_t * tvb,
1341                       proto_tree * flags_tree,
1342                       struct tcp_acked *ta
1343                       )
1344 {
1345     proto_item * flags_item;
1346
1347     /*TCP Keep Alive */
1348     if (ta->flags & TCP_A_KEEP_ALIVE){
1349         flags_item=proto_tree_add_none_format(flags_tree,
1350                                               hf_tcp_analysis_keep_alive,
1351                                               tvb, 0, 0,
1352                                               "This is a TCP keep-alive segment"
1353             );
1354         PROTO_ITEM_SET_GENERATED(flags_item);
1355         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_NOTE,
1356                                "Keep-Alive");
1357         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[TCP Keep-Alive] ");
1358     }
1359     /* TCP Ack Keep Alive */
1360     if (ta->flags & TCP_A_KEEP_ALIVE_ACK) {
1361         flags_item=proto_tree_add_none_format(flags_tree,
1362                                               hf_tcp_analysis_keep_alive_ack,
1363                                               tvb, 0, 0,
1364                                               "This is an ACK to a TCP keep-alive "
1365                                               "segment"
1366             );
1367         PROTO_ITEM_SET_GENERATED(flags_item);
1368         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_NOTE,
1369                                "Keep-Alive ACK");
1370         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[TCP Keep-Alive ACK] ");
1371     }
1372 }
1373
1374 /* Prints results of the sequence number analysis concerning tcp duplicate ack */
1375 static void
1376 tcp_sequence_number_analysis_print_duplicate(packet_info * pinfo,
1377                           tvbuff_t * tvb,
1378                           proto_tree * flags_tree,
1379                           struct tcp_acked *ta,
1380                           proto_tree * tree
1381                         )
1382 {
1383     proto_item * flags_item;
1384
1385     /* TCP Duplicate ACK */
1386     if (ta->dupack_num) {
1387         if (ta->flags & TCP_A_DUPLICATE_ACK ) {
1388             flags_item=proto_tree_add_none_format(flags_tree,
1389                                                   hf_tcp_analysis_duplicate_ack,
1390                                                   tvb, 0, 0,
1391                                                   "This is a TCP duplicate ack"
1392                 );
1393             PROTO_ITEM_SET_GENERATED(flags_item);
1394             col_prepend_fence_fstr(pinfo->cinfo, COL_INFO,
1395                                    "[TCP Dup ACK %u#%u] ",
1396                                    ta->dupack_frame,
1397                                    ta->dupack_num
1398                 );
1399
1400         }
1401         flags_item=proto_tree_add_uint(tree, hf_tcp_analysis_duplicate_ack_num,
1402                                        tvb, 0, 0, ta->dupack_num);
1403         PROTO_ITEM_SET_GENERATED(flags_item);
1404         flags_item=proto_tree_add_uint(tree, hf_tcp_analysis_duplicate_ack_frame,
1405                                        tvb, 0, 0, ta->dupack_frame);
1406         PROTO_ITEM_SET_GENERATED(flags_item);
1407         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_NOTE,
1408                                "Duplicate ACK (#%u)",
1409                                ta->dupack_num
1410             );
1411     }
1412 }
1413
1414 /* Prints results of the sequence number analysis concerning tcp zero window */
1415 static void
1416 tcp_sequence_number_analysis_print_zero_window(packet_info * pinfo,
1417                           tvbuff_t * tvb,
1418                           proto_tree * flags_tree,
1419                           struct tcp_acked *ta
1420                         )
1421 {
1422     proto_item * flags_item;
1423
1424     /* TCP Zero Window Probe */
1425     if (ta->flags & TCP_A_ZERO_WINDOW_PROBE) {
1426         flags_item=proto_tree_add_none_format(flags_tree,
1427                                               hf_tcp_analysis_zero_window_probe,
1428                                               tvb, 0, 0,
1429                                               "This is a TCP zero-window-probe"
1430             );
1431         PROTO_ITEM_SET_GENERATED(flags_item);
1432         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_NOTE,
1433                                "Zero window probe");
1434         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[TCP ZeroWindowProbe] ");
1435     }
1436     /* TCP Zero Window */
1437     if (ta->flags&TCP_A_ZERO_WINDOW) {
1438         flags_item=proto_tree_add_none_format(flags_tree,
1439                                               hf_tcp_analysis_zero_window,
1440                                               tvb, 0, 0,
1441                                               "This is a ZeroWindow segment"
1442             );
1443         PROTO_ITEM_SET_GENERATED(flags_item);
1444         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_WARN,
1445                                "Zero window");
1446         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO, "[TCP ZeroWindow] ");
1447     }
1448     /* TCP Zero Window Probe Ack */
1449     if (ta->flags & TCP_A_ZERO_WINDOW_PROBE_ACK) {
1450         flags_item=proto_tree_add_none_format(flags_tree,
1451                                               hf_tcp_analysis_zero_window_probe_ack,
1452                                               tvb, 0, 0,
1453                                               "This is an ACK to a TCP zero-window-probe"
1454             );
1455         PROTO_ITEM_SET_GENERATED(flags_item);
1456         expert_add_info_format(pinfo, flags_item, PI_SEQUENCE, PI_NOTE,
1457                                "Zero window probe ACK");
1458         col_prepend_fence_fstr(pinfo->cinfo, COL_INFO,
1459                                "[TCP ZeroWindowProbeAck] ");
1460     }
1461 }
1462
1463
1464 /* Prints results of the sequence number analysis concerning how many bytes of data are in flight */
1465 static void
1466 tcp_sequence_number_analysis_print_bytes_in_flight(packet_info * pinfo _U_,
1467                           tvbuff_t * tvb _U_,
1468                           proto_tree * flags_tree _U_,
1469                           struct tcp_acked *ta
1470                         )
1471 {
1472     proto_item * flags_item;
1473
1474     if (tcp_track_bytes_in_flight) {
1475         flags_item=proto_tree_add_uint(flags_tree,
1476                                        hf_tcp_analysis_bytes_in_flight,
1477                                        tvb, 0, 0, ta->bytes_in_flight);
1478
1479         PROTO_ITEM_SET_GENERATED(flags_item);
1480     }
1481 }
1482
1483 static void
1484 tcp_print_sequence_number_analysis(packet_info *pinfo, tvbuff_t *tvb, proto_tree *parent_tree, struct tcp_analysis *tcpd)
1485 {
1486     struct tcp_acked *ta = NULL;
1487     proto_item *item;
1488     proto_tree *tree;
1489     proto_tree *flags_tree=NULL;
1490
1491     if (!tcpd) {
1492         return;
1493     }
1494     if(!tcpd->ta){
1495         tcp_analyze_get_acked_struct(pinfo->fd->num, FALSE, tcpd);
1496     }
1497     ta=tcpd->ta;
1498     if(!ta){
1499         return;
1500     }
1501
1502     item=proto_tree_add_text(parent_tree, tvb, 0, 0, "SEQ/ACK analysis");
1503     PROTO_ITEM_SET_GENERATED(item);
1504     tree=proto_item_add_subtree(item, ett_tcp_analysis);
1505
1506     /* encapsulate all proto_tree_add_xxx in ifs so we only print what
1507        data we actually have */
1508     if(ta->frame_acked){
1509         item = proto_tree_add_uint(tree, hf_tcp_analysis_acks_frame,
1510             tvb, 0, 0, ta->frame_acked);
1511             PROTO_ITEM_SET_GENERATED(item);
1512
1513         /* only display RTT if we actually have something we are acking */
1514         if( ta->ts.secs || ta->ts.nsecs ){
1515             item = proto_tree_add_time(tree, hf_tcp_analysis_ack_rtt,
1516             tvb, 0, 0, &ta->ts);
1517                 PROTO_ITEM_SET_GENERATED(item);
1518         }
1519     }
1520
1521     if(ta->bytes_in_flight) {
1522         /* print results for amount of data in flight */
1523         tcp_sequence_number_analysis_print_bytes_in_flight(pinfo, tvb, tree, ta);
1524     }
1525
1526     if(ta->flags){
1527         item = proto_tree_add_item(tree, hf_tcp_analysis_flags, tvb, 0, 0, FALSE);
1528         PROTO_ITEM_SET_GENERATED(item);
1529         flags_tree=proto_item_add_subtree(item, ett_tcp_analysis);
1530
1531         /* print results for reused tcp ports */
1532         tcp_sequence_number_analysis_print_reused(pinfo, tvb, flags_tree, ta);
1533
1534         /* print results for retransmission and out-of-order segments */
1535         tcp_sequence_number_analysis_print_retransmission(pinfo, tvb, flags_tree, ta);
1536
1537         /* print results for lost tcp segments */
1538         tcp_sequence_number_analysis_print_lost(pinfo, tvb, flags_tree, ta);
1539
1540         /* print results for tcp window information */
1541         tcp_sequence_number_analysis_print_window(pinfo, tvb, flags_tree, ta);
1542
1543         /* print results for tcp keep alive information */
1544         tcp_sequence_number_analysis_print_keepalive(pinfo, tvb, flags_tree, ta);
1545
1546         /* print results for tcp duplicate acks */
1547         tcp_sequence_number_analysis_print_duplicate(pinfo, tvb, flags_tree, ta, tree);
1548
1549         /* print results for tcp zero window  */
1550         tcp_sequence_number_analysis_print_zero_window(pinfo, tvb, flags_tree, ta);
1551
1552     }
1553
1554 }
1555
1556 static void
1557 print_tcp_fragment_tree(fragment_data *ipfd_head, proto_tree *tree, proto_tree *tcp_tree, packet_info *pinfo, tvbuff_t *next_tvb)
1558 {
1559     proto_item *tcp_tree_item, *frag_tree_item;
1560
1561     /*
1562      * The subdissector thought it was completely
1563      * desegmented (although the stuff at the
1564      * end may, in turn, require desegmentation),
1565      * so we show a tree with all segments.
1566      */
1567     show_fragment_tree(ipfd_head, &tcp_segment_items,
1568         tree, pinfo, next_tvb, &frag_tree_item);
1569     /*
1570      * The toplevel fragment subtree is now
1571      * behind all desegmented data; move it
1572      * right behind the TCP tree.
1573      */
1574     tcp_tree_item = proto_tree_get_parent(tcp_tree);
1575     if(frag_tree_item && tcp_tree_item) {
1576         proto_tree_move_item(tree, tcp_tree_item, frag_tree_item);
1577     }
1578 }
1579
1580 /* **************************************************************************
1581  * End of tcp sequence number analysis
1582  * **************************************************************************/
1583
1584
1585 /* Minimum TCP header length. */
1586 #define TCPH_MIN_LEN            20
1587
1588 /* Desegmentation of TCP streams */
1589 /* table to hold defragmented TCP streams */
1590 static GHashTable *tcp_fragment_table = NULL;
1591
1592 /* functions to trace tcp segments */
1593 /* Enable desegmenting of TCP streams */
1594 static gboolean tcp_desegment = TRUE;
1595
1596 static void
1597 desegment_tcp(tvbuff_t *tvb, packet_info *pinfo, int offset,
1598               guint32 seq, guint32 nxtseq,
1599               guint32 sport, guint32 dport,
1600               proto_tree *tree, proto_tree *tcp_tree,
1601               struct tcp_analysis *tcpd)
1602 {
1603     struct tcpinfo *tcpinfo = pinfo->private_data;
1604     fragment_data *ipfd_head;
1605     int last_fragment_len;
1606     gboolean must_desegment;
1607     gboolean called_dissector;
1608     int another_pdu_follows;
1609     int deseg_offset;
1610     guint32 deseg_seq;
1611     gint nbytes;
1612     proto_item *item;
1613     struct tcp_multisegment_pdu *msp;
1614     gboolean cleared_writable = col_get_writable(pinfo->cinfo);
1615
1616 again:
1617     ipfd_head = NULL;
1618     last_fragment_len = 0;
1619     must_desegment = FALSE;
1620     called_dissector = FALSE;
1621     another_pdu_follows = 0;
1622     msp = NULL;
1623
1624     /*
1625      * Initialize these to assume no desegmentation.
1626      * If that's not the case, these will be set appropriately
1627      * by the subdissector.
1628      */
1629     pinfo->desegment_offset = 0;
1630     pinfo->desegment_len = 0;
1631
1632     /*
1633      * Initialize this to assume that this segment will just be
1634      * added to the middle of a desegmented chunk of data, so
1635      * that we should show it all as data.
1636      * If that's not the case, it will be set appropriately.
1637      */
1638     deseg_offset = offset;
1639
1640     if (tcpd) {
1641         /* Have we seen this PDU before (and is it the start of a multi-
1642          * segment PDU)?
1643          */
1644         if ((msp = se_tree_lookup32(tcpd->fwd->multisegment_pdus, seq))) {
1645             const char* str;
1646
1647             /* Yes.  This could be because we've dissected this frame before
1648              * or because this is a retransmission of a previously-seen
1649              * segment.  Either way, we don't need to hand it off to the
1650              * subdissector and we certainly don't want to re-add it to the
1651              * multisegment_pdus list: if we did, subsequent lookups would
1652              * find this retransmission instead of the original transmission
1653              * (breaking desegmentation if we'd already linked other segments
1654              * to the original transmission's entry).
1655              */
1656
1657             if (msp->first_frame == PINFO_FD_NUM(pinfo)) {
1658                 str = "";
1659                 col_set_str(pinfo->cinfo, COL_INFO, "[TCP segment of a reassembled PDU]");
1660             } else {
1661                 str = "Retransmitted ";
1662                 /* TCP analysis already flags this (in COL_INFO) as a retransmission--if it's enabled */
1663             }
1664
1665             nbytes = tvb_reported_length_remaining(tvb, offset);
1666             proto_tree_add_bytes_format(tcp_tree, hf_tcp_data, tvb, offset,
1667                 nbytes, NULL, "%sTCP segment data (%u byte%s)", str, nbytes,
1668                 plurality(nbytes, "", "s"));
1669             return;
1670         }
1671
1672         /* Else, find the most previous PDU starting before this sequence number */
1673         msp = se_tree_lookup32_le(tcpd->fwd->multisegment_pdus, seq-1);
1674     }
1675
1676     if (msp && msp->seq <= seq && msp->nxtpdu > seq) {
1677         int len;
1678
1679         if (!PINFO_FD_VISITED(pinfo)) {
1680             msp->last_frame=pinfo->fd->num;
1681             msp->last_frame_time=pinfo->fd->abs_ts;
1682         }
1683
1684         /* OK, this PDU was found, which means the segment continues
1685          * a higher-level PDU and that we must desegment it.
1686          */
1687         if (msp->flags&MSP_FLAGS_REASSEMBLE_ENTIRE_SEGMENT) {
1688             /* The dissector asked for the entire segment */
1689             len = tvb_length_remaining(tvb, offset);
1690         } else {
1691             len = MIN(nxtseq, msp->nxtpdu) - seq;
1692         }
1693         last_fragment_len = len;
1694
1695         ipfd_head = fragment_add(tvb, offset, pinfo, msp->first_frame,
1696                                  tcp_fragment_table, seq - msp->seq, len,
1697                                  (LT_SEQ (nxtseq,msp->nxtpdu)) );
1698
1699         if (msp->flags & MSP_FLAGS_REASSEMBLE_ENTIRE_SEGMENT) {
1700             msp->flags &= (~MSP_FLAGS_REASSEMBLE_ENTIRE_SEGMENT);
1701
1702             /* If we consumed the entire segment there is no
1703              * other pdu starting anywhere inside this segment.
1704              * So update nxtpdu to point at least to the start
1705              * of the next segment.
1706              * (If the subdissector asks for even more data we
1707              * will advance nxtpdu even furhter later down in
1708              * the code.)
1709              */
1710             msp->nxtpdu = nxtseq;
1711         }
1712
1713         if( (msp->nxtpdu < nxtseq)
1714         &&  (msp->nxtpdu >= seq)
1715         &&  (len > 0)) {
1716             another_pdu_follows=msp->nxtpdu - seq;
1717         }
1718     } else {
1719         /* This segment was not found in our table, so it doesn't
1720          * contain a continuation of a higher-level PDU.
1721          * Call the normal subdissector.
1722          */
1723
1724         /*
1725          * Supply the sequence number of this segment. We set this here
1726          * because this segment could be after another in the same packet,
1727          * in which case seq was incremented at the end of the loop.
1728          */
1729         tcpinfo->seq = seq;
1730
1731         process_tcp_payload(tvb, offset, pinfo, tree, tcp_tree,
1732                             sport, dport, 0, 0, FALSE, tcpd);
1733         called_dissector = TRUE;
1734
1735         /* Did the subdissector ask us to desegment some more data
1736          * before it could handle the packet?
1737          * If so we have to create some structures in our table but
1738          * this is something we only do the first time we see this
1739          * packet.
1740          */
1741         if(pinfo->desegment_len) {
1742             if (!PINFO_FD_VISITED(pinfo))
1743                 must_desegment = TRUE;
1744
1745             /*
1746              * Set "deseg_offset" to the offset in "tvb"
1747              * of the first byte of data that the
1748              * subdissector didn't process.
1749              */
1750             deseg_offset = offset + pinfo->desegment_offset;
1751         }
1752
1753         /* Either no desegmentation is necessary, or this is
1754          * segment contains the beginning but not the end of
1755          * a higher-level PDU and thus isn't completely
1756          * desegmented.
1757          */
1758         ipfd_head = NULL;
1759     }
1760
1761
1762     /* is it completely desegmented? */
1763     if (ipfd_head) {
1764         /*
1765          * Yes, we think it is.
1766          * We only call subdissector for the last segment.
1767          * Note that the last segment may include more than what
1768          * we needed.
1769          */
1770         if(ipfd_head->reassembled_in == pinfo->fd->num) {
1771             /*
1772              * OK, this is the last segment.
1773              * Let's call the subdissector with the desegmented
1774              * data.
1775              */
1776             tvbuff_t *next_tvb;
1777             int old_len;
1778
1779             /* create a new TVB structure for desegmented data */
1780             next_tvb = tvb_new_child_real_data(tvb, ipfd_head->data,
1781                                                ipfd_head->datalen,
1782                                                ipfd_head->datalen);
1783
1784             /* add desegmented data to the data source list */
1785             add_new_data_source(pinfo, next_tvb, "Reassembled TCP");
1786
1787             /*
1788              * Supply the sequence number of the first of the
1789              * reassembled bytes.
1790              */
1791             tcpinfo->seq = msp->seq;
1792
1793             /* indicate that this is reassembled data */
1794             tcpinfo->is_reassembled = TRUE;
1795
1796             /* call subdissector */
1797             process_tcp_payload(next_tvb, 0, pinfo, tree, tcp_tree, sport,
1798                                 dport, 0, 0, FALSE, tcpd);
1799             called_dissector = TRUE;
1800
1801             /*
1802              * OK, did the subdissector think it was completely
1803              * desegmented, or does it think we need even more
1804              * data?
1805              */
1806             old_len = (int)(tvb_reported_length(next_tvb) - last_fragment_len);
1807             if (pinfo->desegment_len &&
1808                 pinfo->desegment_offset<=old_len) {
1809                 /*
1810                  * "desegment_len" isn't 0, so it needs more
1811                  * data for something - and "desegment_offset"
1812                  * is before "old_len", so it needs more data
1813                  * to dissect the stuff we thought was
1814                  * completely desegmented (as opposed to the
1815                  * stuff at the beginning being completely
1816                  * desegmented, but the stuff at the end
1817                  * being a new higher-level PDU that also
1818                  * needs desegmentation).
1819                  */
1820                 fragment_set_partial_reassembly(pinfo,msp->first_frame, tcp_fragment_table);
1821
1822                 /* Update msp->nxtpdu to point to the new next
1823                  * pdu boundary.
1824                  */
1825                 if (pinfo->desegment_len == DESEGMENT_ONE_MORE_SEGMENT) {
1826                     /* We want reassembly of at least one
1827                      * more segment so set the nxtpdu
1828                      * boundary to one byte into the next
1829                      * segment.
1830                      * This means that the next segment
1831                      * will complete reassembly even if it
1832                      * is only one single byte in length.
1833                      */
1834                     msp->nxtpdu = seq + tvb_reported_length_remaining(tvb, offset) + 1;
1835                     msp->flags |= MSP_FLAGS_REASSEMBLE_ENTIRE_SEGMENT;
1836                 } else if (pinfo->desegment_len == DESEGMENT_UNTIL_FIN) {
1837                     tcpd->fwd->flags |= TCP_FLOW_REASSEMBLE_UNTIL_FIN;
1838                 } else {
1839                     msp->nxtpdu=seq + last_fragment_len + pinfo->desegment_len;
1840                 }
1841
1842                 /* Since we need at least some more data
1843                  * there can be no pdu following in the
1844                  * tail of this segment.
1845                  */
1846                 another_pdu_follows = 0;
1847                 offset += last_fragment_len;
1848                 seq += last_fragment_len;
1849                 if (tvb_length_remaining(tvb, offset) > 0)
1850                     goto again;
1851             } else {
1852                 /*
1853                  * Show the stuff in this TCP segment as
1854                  * just raw TCP segment data.
1855                  */
1856                 nbytes = another_pdu_follows > 0
1857                     ? another_pdu_follows
1858                     : tvb_reported_length_remaining(tvb, offset);
1859                 proto_tree_add_bytes_format(tcp_tree, hf_tcp_data, tvb, offset,
1860                     nbytes, NULL, "TCP segment data (%u byte%s)", nbytes,
1861                     plurality(nbytes, "", "s"));
1862
1863                 print_tcp_fragment_tree(ipfd_head, tree, tcp_tree, pinfo, next_tvb);
1864
1865                 /* Did the subdissector ask us to desegment
1866                  * some more data?  This means that the data
1867                  * at the beginning of this segment completed
1868                  * a higher-level PDU, but the data at the
1869                  * end of this segment started a higher-level
1870                  * PDU but didn't complete it.
1871                  *
1872                  * If so, we have to create some structures
1873                  * in our table, but this is something we
1874                  * only do the first time we see this packet.
1875                  */
1876                 if(pinfo->desegment_len) {
1877                     if (!PINFO_FD_VISITED(pinfo))
1878                         must_desegment = TRUE;
1879
1880                     /* The stuff we couldn't dissect
1881                      * must have come from this segment,
1882                      * so it's all in "tvb".
1883                      *
1884                      * "pinfo->desegment_offset" is
1885                      * relative to the beginning of
1886                      * "next_tvb"; we want an offset
1887                      * relative to the beginning of "tvb".
1888                      *
1889                      * First, compute the offset relative
1890                      * to the *end* of "next_tvb" - i.e.,
1891                      * the number of bytes before the end
1892                      * of "next_tvb" at which the
1893                      * subdissector stopped.  That's the
1894                      * length of "next_tvb" minus the
1895                      * offset, relative to the beginning
1896                      * of "next_tvb, at which the
1897                      * subdissector stopped.
1898                      */
1899                     deseg_offset = ipfd_head->datalen - pinfo->desegment_offset;
1900
1901                     /* "tvb" and "next_tvb" end at the
1902                      * same byte of data, so the offset
1903                      * relative to the end of "next_tvb"
1904                      * of the byte at which we stopped
1905                      * is also the offset relative to
1906                      * the end of "tvb" of the byte at
1907                      * which we stopped.
1908                      *
1909                      * Convert that back into an offset
1910                      * relative to the beginninng of
1911                      * "tvb", by taking the length of
1912                      * "tvb" and subtracting the offset
1913                      * relative to the end.
1914                      */
1915                     deseg_offset = tvb_reported_length(tvb) - deseg_offset;
1916                 }
1917             }
1918         }
1919     }
1920
1921     if (must_desegment) {
1922         /* If the dissector requested "reassemble until FIN"
1923          * just set this flag for the flow and let reassembly
1924          * proceed at normal.  We will check/pick up these
1925          * reassembled PDUs later down in dissect_tcp() when checking
1926          * for the FIN flag.
1927          */
1928         if (tcpd && pinfo->desegment_len == DESEGMENT_UNTIL_FIN) {
1929             tcpd->fwd->flags |= TCP_FLOW_REASSEMBLE_UNTIL_FIN;
1930         }
1931         /*
1932          * The sequence number at which the stuff to be desegmented
1933          * starts is the sequence number of the byte at an offset
1934          * of "deseg_offset" into "tvb".
1935          *
1936          * The sequence number of the byte at an offset of "offset"
1937          * is "seq", i.e. the starting sequence number of this
1938          * segment, so the sequence number of the byte at
1939          * "deseg_offset" is "seq + (deseg_offset - offset)".
1940          */
1941         deseg_seq = seq + (deseg_offset - offset);
1942
1943         if (tcpd && ((nxtseq - deseg_seq) <= 1024*1024)
1944             && (!PINFO_FD_VISITED(pinfo))) {
1945             if(pinfo->desegment_len == DESEGMENT_ONE_MORE_SEGMENT) {
1946                 /* The subdissector asked to reassemble using the
1947                  * entire next segment.
1948                  * Just ask reassembly for one more byte
1949                  * but set this msp flag so we can pick it up
1950                  * above.
1951                  */
1952                 msp = pdu_store_sequencenumber_of_next_pdu(pinfo, deseg_seq,
1953                     nxtseq+1, tcpd->fwd->multisegment_pdus);
1954                 msp->flags |= MSP_FLAGS_REASSEMBLE_ENTIRE_SEGMENT;
1955             } else {
1956                 msp = pdu_store_sequencenumber_of_next_pdu(pinfo,
1957                     deseg_seq, nxtseq+pinfo->desegment_len, tcpd->fwd->multisegment_pdus);
1958             }
1959
1960             /* add this segment as the first one for this new pdu */
1961             fragment_add(tvb, deseg_offset, pinfo, msp->first_frame,
1962                          tcp_fragment_table, 0, nxtseq - deseg_seq,
1963                          LT_SEQ(nxtseq, msp->nxtpdu));
1964         }
1965     }
1966
1967     if (!called_dissector || pinfo->desegment_len != 0) {
1968         if (ipfd_head != NULL && ipfd_head->reassembled_in != 0 &&
1969             !(ipfd_head->flags & FD_PARTIAL_REASSEMBLY)) {
1970             /*
1971              * We know what frame this PDU is reassembled in;
1972              * let the user know.
1973              */
1974             item = proto_tree_add_uint(tcp_tree, hf_tcp_reassembled_in, tvb, 0,
1975                                        0, ipfd_head->reassembled_in);
1976             PROTO_ITEM_SET_GENERATED(item);
1977         }
1978
1979         /*
1980          * Either we didn't call the subdissector at all (i.e.,
1981          * this is a segment that contains the middle of a
1982          * higher-level PDU, but contains neither the beginning
1983          * nor the end), or the subdissector couldn't dissect it
1984          * all, as some data was missing (i.e., it set
1985          * "pinfo->desegment_len" to the amount of additional
1986          * data it needs).
1987          */
1988         if (pinfo->desegment_offset == 0) {
1989             /*
1990              * It couldn't, in fact, dissect any of it (the
1991              * first byte it couldn't dissect is at an offset
1992              * of "pinfo->desegment_offset" from the beginning
1993              * of the payload, and that's 0).
1994              * Just mark this as TCP.
1995              */
1996             col_set_str(pinfo->cinfo, COL_PROTOCOL, "TCP");
1997             col_set_str(pinfo->cinfo, COL_INFO, "[TCP segment of a reassembled PDU]");
1998         }
1999
2000         /*
2001          * Show what's left in the packet as just raw TCP segment
2002          * data.
2003          * XXX - remember what protocol the last subdissector
2004          * was, and report it as a continuation of that, instead?
2005          */
2006         nbytes = tvb_reported_length_remaining(tvb, deseg_offset);
2007         proto_tree_add_bytes_format(tcp_tree, hf_tcp_data, tvb, deseg_offset,
2008             -1, NULL, "TCP segment data (%u byte%s)", nbytes,
2009             plurality(nbytes, "", "s"));
2010     }
2011     pinfo->can_desegment = 0;
2012     pinfo->desegment_offset = 0;
2013     pinfo->desegment_len = 0;
2014
2015     if(another_pdu_follows) {
2016         /* there was another pdu following this one. */
2017         pinfo->can_desegment = 2;
2018         /* we also have to prevent the dissector from changing the
2019          * PROTOCOL and INFO colums since what follows may be an
2020          * incomplete PDU and we dont want it be changed back from
2021          *  <Protocol>   to <TCP>
2022          * XXX There is no good way to block the PROTOCOL column
2023          * from being changed yet so we set the entire row unwritable.
2024          * The flag cleared_writable stores the initial state.
2025          */
2026         col_set_fence(pinfo->cinfo, COL_INFO);
2027         cleared_writable |= col_get_writable(pinfo->cinfo);
2028         col_set_writable(pinfo->cinfo, FALSE);
2029         offset += another_pdu_follows;
2030         seq += another_pdu_follows;
2031         goto again;
2032     } else {
2033         /* remove any blocking set above otherwise the
2034          * proto,colinfo tap will break
2035          */
2036         if(cleared_writable) {
2037             col_set_writable(pinfo->cinfo, TRUE);
2038         }
2039     }
2040 }
2041
2042 /*
2043  * Loop for dissecting PDUs within a TCP stream; assumes that a PDU
2044  * consists of a fixed-length chunk of data that contains enough information
2045  * to determine the length of the PDU, followed by rest of the PDU.
2046  *
2047  * The first three arguments are the arguments passed to the dissector
2048  * that calls this routine.
2049  *
2050  * "proto_desegment" is the dissector's flag controlling whether it should
2051  * desegment PDUs that cross TCP segment boundaries.
2052  *
2053  * "fixed_len" is the length of the fixed-length part of the PDU.
2054  *
2055  * "get_pdu_len()" is a routine called to get the length of the PDU from
2056  * the fixed-length part of the PDU; it's passed "pinfo", "tvb" and "offset".
2057  *
2058  * "dissect_pdu()" is the routine to dissect a PDU.
2059  */
2060 void
2061 tcp_dissect_pdus(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree,
2062                  gboolean proto_desegment, guint fixed_len,
2063                  guint (*get_pdu_len)(packet_info *, tvbuff_t *, int),
2064                  dissector_t dissect_pdu)
2065 {
2066     volatile int offset = 0;
2067     int offset_before;
2068     guint length_remaining;
2069     guint plen;
2070     guint length;
2071     tvbuff_t *next_tvb;
2072     proto_item *item=NULL;
2073     void *pd_save;
2074
2075     while (tvb_reported_length_remaining(tvb, offset) != 0) {
2076         /*
2077          * We use "tvb_ensure_length_remaining()" to make sure there actually
2078          * *is* data remaining.  The protocol we're handling could conceivably
2079          * consists of a sequence of fixed-length PDUs, and therefore the
2080          * "get_pdu_len" routine might not actually fetch anything from
2081          * the tvbuff, and thus might not cause an exception to be thrown if
2082          * we've run past the end of the tvbuff.
2083          *
2084          * This means we're guaranteed that "length_remaining" is positive.
2085          */
2086         length_remaining = tvb_ensure_length_remaining(tvb, offset);
2087
2088         /*
2089          * Can we do reassembly?
2090          */
2091         if (proto_desegment && pinfo->can_desegment) {
2092             /*
2093              * Yes - is the fixed-length part of the PDU split across segment
2094              * boundaries?
2095              */
2096             if (length_remaining < fixed_len) {
2097                 /*
2098                  * Yes.  Tell the TCP dissector where the data for this message
2099                  * starts in the data it handed us and that we need "some more
2100                  * data."  Don't tell it exactly how many bytes we need because
2101                  * if/when we ask for even more (after the header) that will
2102                  * break reassembly.
2103                  */
2104                 pinfo->desegment_offset = offset;
2105                 pinfo->desegment_len = DESEGMENT_ONE_MORE_SEGMENT;
2106                 return;
2107             }
2108         }
2109
2110         /*
2111          * Get the length of the PDU.
2112          */
2113         plen = (*get_pdu_len)(pinfo, tvb, offset);
2114         if (plen < fixed_len) {
2115             /*
2116              * Either:
2117              *
2118              *  1) the length value extracted from the fixed-length portion
2119              *     doesn't include the fixed-length portion's length, and
2120              *     was so large that, when the fixed-length portion's
2121              *     length was added to it, the total length overflowed;
2122              *
2123              *  2) the length value extracted from the fixed-length portion
2124              *     includes the fixed-length portion's length, and the value
2125              *     was less than the fixed-length portion's length, i.e. it
2126              *     was bogus.
2127              *
2128              * Report this as a bounds error.
2129              */
2130             show_reported_bounds_error(tvb, pinfo, tree);
2131             return;
2132         }
2133
2134         /*
2135          * Do not display the the PDU length if it crosses the boundary of the
2136          * packet and no more packets are available.
2137          *
2138          * XXX - we don't necessarily know whether more packets are
2139          * available; we might be doing a one-pass read through the
2140          * capture in TShark, or we might be doing a live capture in
2141          * Wireshark.
2142          */
2143 #if 0
2144         if (length_remaining >= plen || there are more packets)
2145         {
2146 #endif
2147                 /*
2148                  * Display the PDU length as a field
2149                  */
2150                 item=proto_tree_add_uint(pinfo->tcp_tree, hf_tcp_pdu_size,
2151                                          tvb, offset, plen, plen);
2152                 PROTO_ITEM_SET_GENERATED(item);
2153 #if 0
2154         } else {
2155                 item = proto_tree_add_text(pinfo->tcp_tree, tvb, offset, -1,
2156                     "PDU Size: %u cut short at %u",plen,length_remaining);
2157                 PROTO_ITEM_SET_GENERATED(item);
2158         }
2159 #endif
2160
2161
2162         /* give a hint to TCP where the next PDU starts
2163          * so that it can attempt to find it in case it starts
2164          * somewhere in the middle of a segment.
2165          */
2166         if(!pinfo->fd->flags.visited && tcp_analyze_seq){
2167             guint remaining_bytes;
2168             remaining_bytes=tvb_reported_length_remaining(tvb, offset);
2169             if(plen>remaining_bytes){
2170                 pinfo->want_pdu_tracking=2;
2171                 pinfo->bytes_until_next_pdu=plen-remaining_bytes;
2172             }
2173         }
2174
2175         /*
2176          * Can we do reassembly?
2177          */
2178         if (proto_desegment && pinfo->can_desegment) {
2179             /*
2180              * Yes - is the PDU split across segment boundaries?
2181              */
2182             if (length_remaining < plen) {
2183                 /*
2184                  * Yes.  Tell the TCP dissector where the data for this message
2185                  * starts in the data it handed us, and how many more bytes we
2186                  * need, and return.
2187                  */
2188                 pinfo->desegment_offset = offset;
2189                 pinfo->desegment_len = plen - length_remaining;
2190                 return;
2191             }
2192         }
2193
2194         /*
2195          * Construct a tvbuff containing the amount of the payload we have
2196          * available.  Make its reported length the amount of data in the PDU.
2197          *
2198          * XXX - if reassembly isn't enabled. the subdissector will throw a
2199          * BoundsError exception, rather than a ReportedBoundsError exception.
2200          * We really want a tvbuff where the length is "length", the reported
2201          * length is "plen", and the "if the snapshot length were infinite"
2202          * length is the minimum of the reported length of the tvbuff handed
2203          * to us and "plen", with a new type of exception thrown if the offset
2204          * is within the reported length but beyond that third length, with
2205          * that exception getting the "Unreassembled Packet" error.
2206          */
2207         length = length_remaining;
2208         if (length > plen)
2209             length = plen;
2210         next_tvb = tvb_new_subset(tvb, offset, length, plen);
2211
2212         /*
2213          * Dissect the PDU.
2214          *
2215          * Catch the ReportedBoundsError exception; if this particular message
2216          * happens to get a ReportedBoundsError exception, that doesn't mean
2217          * that we should stop dissecting PDUs within this frame or chunk of
2218          * reassembled data.
2219          *
2220          * If it gets a BoundsError, we can stop, as there's nothing more to
2221          * see, so we just re-throw it.
2222          */
2223         pd_save = pinfo->private_data;
2224         TRY {
2225             (*dissect_pdu)(next_tvb, pinfo, tree);
2226         }
2227         CATCH(BoundsError) {
2228             RETHROW;
2229         }
2230         CATCH(ReportedBoundsError) {
2231             /*  Restore the private_data structure in case one of the
2232              *  called dissectors modified it (and, due to the exception,
2233              *  was unable to restore it).
2234              */
2235             pinfo->private_data = pd_save;
2236             show_reported_bounds_error(tvb, pinfo, tree);
2237         }
2238         ENDTRY;
2239
2240         /*
2241          * Step to the next PDU.
2242          * Make sure we don't overflow.
2243          */
2244         offset_before = offset;
2245         offset += plen;
2246         if (offset <= offset_before)
2247             break;
2248     }
2249 }
2250
2251 static void
2252 tcp_info_append_uint(packet_info *pinfo, const char *abbrev, guint32 val)
2253 {
2254     col_append_fstr(pinfo->cinfo, COL_INFO, " %s=%u", abbrev, val);
2255 }
2256
2257 /* Supports the reporting the contents of a parsed SCPS capabilities vector */
2258 static void
2259 tcp_info_append_str(packet_info *pinfo, const char *abbrev, const char *val)
2260 {
2261     col_append_fstr(pinfo->cinfo, COL_INFO, " %s[%s]", abbrev, val);
2262 }
2263
2264
2265 static void
2266 dissect_tcpopt_exp(const ip_tcp_opt *optp _U_, tvbuff_t *tvb,
2267     int offset, guint optlen, packet_info *pinfo, proto_tree *opt_tree)
2268 {
2269     proto_item *item;
2270     proto_tree *exp_tree;
2271
2272     item = proto_tree_add_item(opt_tree, hf_tcp_option_exp, tvb,
2273         offset, optlen, FALSE);
2274     exp_tree = proto_item_add_subtree(item, ett_tcp_option_exp);
2275     proto_tree_add_item(exp_tree, hf_tcp_option_kind, tvb, offset, 1, FALSE);
2276     proto_tree_add_item(exp_tree, hf_tcp_option_len, tvb, offset + 1, 1, FALSE);
2277     proto_tree_add_item(exp_tree, hf_tcp_option_exp_data, tvb,
2278         offset + 2, optlen - 2, FALSE);
2279     tcp_info_append_uint(pinfo, "Expxx", TRUE);
2280 }
2281
2282
2283 static void
2284 dissect_tcpopt_sack_perm(const ip_tcp_opt *optp _U_, tvbuff_t *tvb,
2285     int offset, guint optlen, packet_info *pinfo, proto_tree *opt_tree)
2286 {
2287     proto_item *item;
2288     proto_tree *exp_tree;
2289
2290     item = proto_tree_add_boolean(opt_tree, hf_tcp_option_sack_perm, tvb, offset,
2291                            optlen, TRUE);
2292     exp_tree = proto_item_add_subtree(item, ett_tcp_option_sack_perm);
2293     proto_tree_add_item(exp_tree, hf_tcp_option_kind, tvb, offset, 1, FALSE);
2294     proto_tree_add_item(exp_tree, hf_tcp_option_len, tvb, offset + 1, 1, FALSE);
2295     tcp_info_append_uint(pinfo, "SACK_PERM", TRUE);
2296 }
2297
2298 static void
2299 dissect_tcpopt_mss(const ip_tcp_opt *optp, tvbuff_t *tvb,
2300     int offset, guint optlen, packet_info *pinfo, proto_tree *opt_tree)
2301 {
2302     proto_item *item;
2303     proto_tree *exp_tree;
2304     guint16 mss;
2305
2306     mss = tvb_get_ntohs(tvb, offset + 2);
2307     item = proto_tree_add_none_format(opt_tree, hf_tcp_option_mss, tvb, offset,
2308         optlen, "%s: %u bytes", optp->name, mss);
2309     exp_tree = proto_item_add_subtree(item, ett_tcp_option_mss);
2310     proto_tree_add_item(exp_tree, hf_tcp_option_kind, tvb, offset, 1, FALSE);
2311     proto_tree_add_item(exp_tree, hf_tcp_option_len, tvb, offset + 1, 1, FALSE);
2312     proto_tree_add_item(exp_tree, hf_tcp_option_mss_val, tvb, offset + 2, 2, FALSE);
2313     tcp_info_append_uint(pinfo, "MSS", mss);
2314 }
2315
2316 /* The window scale extension is defined in RFC 1323 */
2317 static void
2318 dissect_tcpopt_wscale(const ip_tcp_opt *optp _U_, tvbuff_t *tvb,
2319     int offset, guint optlen _U_, packet_info *pinfo, proto_tree *opt_tree)
2320 {
2321     guint8 val, shift;
2322     proto_item *wscale_pi, *gen_pi;
2323     proto_tree *wscale_tree;
2324     struct tcp_analysis *tcpd=NULL;
2325
2326     tcpd=get_tcp_conversation_data(NULL,pinfo);
2327
2328     wscale_pi = proto_tree_add_text(opt_tree, tvb, offset, 3, "Window scale: ");
2329     wscale_tree = proto_item_add_subtree(wscale_pi, ett_tcp_option_wscale);
2330
2331     proto_tree_add_item(wscale_tree, hf_tcp_option_kind, tvb, offset, 1, ENC_NA);
2332     offset += 1;
2333
2334     proto_tree_add_item(wscale_tree, hf_tcp_option_len, tvb, offset, 1, ENC_NA);
2335     offset += 1;
2336
2337     proto_tree_add_item(wscale_tree, hf_tcp_option_wscale_shift, tvb, offset, 1,
2338                         ENC_NA);
2339     shift = tvb_get_guint8(tvb, offset);
2340
2341     gen_pi = proto_tree_add_uint(wscale_tree, hf_tcp_option_wscale_multiplier, tvb,
2342                                  offset, 1, 1 << shift);
2343     PROTO_ITEM_SET_GENERATED(gen_pi);
2344     val = tvb_get_guint8(tvb, offset);
2345
2346     proto_item_append_text(wscale_pi, "%u (multiply by %u)", val, 1 << shift);
2347
2348     tcp_info_append_uint(pinfo, "WS", 1 << shift);
2349
2350     if(!pinfo->fd->flags.visited){
2351         pdu_store_window_scale_option(shift, tcpd);
2352     }
2353 }
2354
2355 static void
2356 dissect_tcpopt_sack(const ip_tcp_opt *optp, tvbuff_t *tvb,
2357     int offset, guint optlen, packet_info *pinfo, proto_tree *opt_tree)
2358 {
2359     proto_tree *field_tree = NULL;
2360     proto_item *tf=NULL;
2361     proto_item *hidden_item;
2362     guint32 leftedge, rightedge;
2363     struct tcp_analysis *tcpd=NULL;
2364     guint32 base_ack=0;
2365
2366     if(tcp_analyze_seq && tcp_relative_seq){
2367         /* find(or create if needed) the conversation for this tcp session */
2368         tcpd=get_tcp_conversation_data(NULL,pinfo);
2369
2370         if (tcpd) {
2371             base_ack=tcpd->rev->base_seq;
2372         }
2373     }
2374
2375     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_kind, tvb,
2376                         offset, 1, FALSE);
2377     PROTO_ITEM_SET_HIDDEN(hidden_item);
2378     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_len, tvb,
2379                         offset + 1, 1, FALSE);
2380     PROTO_ITEM_SET_HIDDEN(hidden_item);
2381
2382     tf = proto_tree_add_text(opt_tree, tvb, offset,      optlen, "%s:", optp->name);
2383     offset += 2;    /* skip past type and length */
2384     optlen -= 2;    /* subtract size of type and length */
2385     while (optlen > 0) {
2386         if (field_tree == NULL) {
2387             /* Haven't yet made a subtree out of this option.  Do so. */
2388             field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
2389             hidden_item = proto_tree_add_boolean(field_tree, hf_tcp_option_sack, tvb,
2390                                                  offset, optlen, TRUE);
2391             PROTO_ITEM_SET_HIDDEN(hidden_item);
2392         }
2393         if (optlen < 4) {
2394             proto_tree_add_text(field_tree, tvb, offset,      optlen,
2395                                 "(suboption would go past end of option)");
2396             break;
2397         }
2398         leftedge = tvb_get_ntohl(tvb, offset)-base_ack;
2399         proto_tree_add_uint_format(field_tree, hf_tcp_option_sack_sle, tvb,
2400                                    offset, 4, leftedge,
2401                                    "left edge = %u%s", leftedge,
2402                                    tcp_relative_seq ? " (relative)" : "");
2403
2404         optlen -= 4;
2405         if (optlen < 4) {
2406             proto_tree_add_text(field_tree, tvb, offset,      optlen,
2407                                 "(suboption would go past end of option)");
2408             break;
2409         }
2410         /* XXX - check whether it goes past end of packet */
2411         rightedge = tvb_get_ntohl(tvb, offset + 4)-base_ack;
2412         optlen -= 4;
2413         proto_tree_add_uint_format(field_tree, hf_tcp_option_sack_sre, tvb,
2414                                    offset+4, 4, rightedge,
2415                                    "right edge = %u%s", rightedge,
2416                                    tcp_relative_seq ? " (relative)" : "");
2417         tcp_info_append_uint(pinfo, "SLE", leftedge);
2418         tcp_info_append_uint(pinfo, "SRE", rightedge);
2419         proto_item_append_text(field_tree, " %u-%u", leftedge, rightedge);
2420         offset += 8;
2421     }
2422 }
2423
2424 static void
2425 dissect_tcpopt_echo(const ip_tcp_opt *optp, tvbuff_t *tvb,
2426     int offset, guint optlen, packet_info *pinfo, proto_tree *opt_tree)
2427 {
2428     proto_item *hidden_item;
2429     guint32 echo;
2430
2431     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_kind, tvb,
2432                         offset, 1, FALSE);
2433     PROTO_ITEM_SET_HIDDEN(hidden_item);
2434     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_len, tvb,
2435                         offset + 1, 1, FALSE);
2436     PROTO_ITEM_SET_HIDDEN(hidden_item);
2437
2438     echo = tvb_get_ntohl(tvb, offset + 2);
2439     hidden_item = proto_tree_add_boolean(opt_tree, hf_tcp_option_echo, tvb, offset,
2440                                          optlen, TRUE);
2441     PROTO_ITEM_SET_HIDDEN(hidden_item);
2442     proto_tree_add_text(opt_tree, tvb, offset,      optlen,
2443                         "%s: %u", optp->name, echo);
2444     tcp_info_append_uint(pinfo, "ECHO", echo);
2445 }
2446
2447 /* If set, do not put the TCP timestamp information on the summary line */
2448 static gboolean tcp_ignore_timestamps = FALSE;
2449
2450 static void
2451 dissect_tcpopt_timestamp(const ip_tcp_opt *optp _U_, tvbuff_t *tvb,
2452     int offset, guint optlen _U_, packet_info *pinfo, proto_tree *opt_tree)
2453 {
2454     proto_item *ti;
2455     proto_tree *ts_tree;
2456     guint32 ts_val, ts_ecr;
2457
2458     ti = proto_tree_add_text(opt_tree, tvb, offset, 10, "Timestamps: ");
2459     ts_tree = proto_item_add_subtree(ti, ett_tcp_option_timestamp);
2460
2461     proto_tree_add_item(ts_tree, hf_tcp_option_kind, tvb, offset, 1, ENC_NA);
2462     offset += 1;
2463
2464     proto_tree_add_item(ts_tree, hf_tcp_option_len, tvb, offset, 1, ENC_NA);
2465     offset += 1;
2466
2467     proto_tree_add_item(ts_tree,  hf_tcp_option_timestamp_tsval, tvb, offset,
2468                         4, ENC_BIG_ENDIAN);
2469     ts_val = tvb_get_ntohl(tvb, offset);
2470     offset += 4;
2471
2472     proto_tree_add_item(ts_tree,  hf_tcp_option_timestamp_tsecr, tvb, offset,
2473                         4, ENC_BIG_ENDIAN);
2474     ts_ecr = tvb_get_ntohl(tvb, offset);
2475     offset += 4;
2476
2477     proto_item_append_text(ti, "TSval %u, TSecr %u", ts_val, ts_ecr);
2478     if (tcp_ignore_timestamps == FALSE) {
2479         tcp_info_append_uint(pinfo, "TSval", ts_val);
2480         tcp_info_append_uint(pinfo, "TSecr", ts_ecr);
2481     }
2482 }
2483
2484 static void
2485 dissect_tcpopt_cc(const ip_tcp_opt *optp, tvbuff_t *tvb,
2486     int offset, guint optlen, packet_info *pinfo, proto_tree *opt_tree)
2487 {
2488     proto_item *hidden_item;
2489     guint32 cc;
2490
2491     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_kind, tvb,
2492                         offset, 1, FALSE);
2493     PROTO_ITEM_SET_HIDDEN(hidden_item);
2494     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_len, tvb,
2495                         offset + 1, 1, FALSE);
2496     PROTO_ITEM_SET_HIDDEN(hidden_item);
2497
2498     cc = tvb_get_ntohl(tvb, offset + 2);
2499     hidden_item = proto_tree_add_boolean(opt_tree, hf_tcp_option_cc, tvb, offset,
2500                                          optlen, TRUE);
2501     PROTO_ITEM_SET_HIDDEN(hidden_item);
2502     proto_tree_add_text(opt_tree, tvb, offset,      optlen,
2503                         "%s: %u", optp->name, cc);
2504     tcp_info_append_uint(pinfo, "CC", cc);
2505 }
2506
2507 static void
2508 dissect_tcpopt_qs(const ip_tcp_opt *optp, tvbuff_t *tvb,
2509     int offset, guint optlen, packet_info *pinfo, proto_tree *opt_tree)
2510 {
2511     /* Quick-Start TCP option, as defined by RFC4782 */
2512     static const value_string qs_rates[] = {
2513         { 0, "0 bit/s"},
2514         { 1, "80 kbit/s"},
2515         { 2, "160 kbit/s"},
2516         { 3, "320 kbit/s"},
2517         { 4, "640 kbit/s"},
2518         { 5, "1.28 Mbit/s"},
2519         { 6, "2.56 Mbit/s"},
2520         { 7, "5.12 Mbit/s"},
2521         { 8, "10.24 Mbit/s"},
2522         { 9, "20.48 Mbit/s"},
2523         {10, "40.96 Mbit/s"},
2524         {11, "81.92 Mbit/s"},
2525         {12, "163.84 Mbit/s"},
2526         {13, "327.68 Mbit/s"},
2527         {14, "655.36 Mbit/s"},
2528         {15, "1.31072 Gbit/s"},
2529         {0, NULL}
2530     };
2531     static value_string_ext qs_rates_ext = VALUE_STRING_EXT_INIT(qs_rates);
2532
2533     proto_item *hidden_item;
2534
2535     guint8 rate = tvb_get_guint8(tvb, offset + 2) & 0x0f;
2536
2537     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_kind, tvb,
2538                         offset, 1, FALSE);
2539     PROTO_ITEM_SET_HIDDEN(hidden_item);
2540     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_len, tvb,
2541                         offset + 1, 1, FALSE);
2542     PROTO_ITEM_SET_HIDDEN(hidden_item);
2543
2544     hidden_item = proto_tree_add_boolean(opt_tree, hf_tcp_option_qs, tvb, offset,
2545                                          optlen, TRUE);
2546     PROTO_ITEM_SET_HIDDEN(hidden_item);
2547     proto_tree_add_text(opt_tree, tvb, offset,      optlen,
2548                         "%s: Rate response, %s, TTL diff %u ", optp->name,
2549                         val_to_str_ext(rate, &qs_rates_ext, "Unknown"),
2550                         tvb_get_guint8(tvb, offset + 3));
2551     col_append_fstr(pinfo->cinfo, COL_INFO, " QSresp=%s", val_to_str_ext(rate, &qs_rates_ext, "Unknown"));
2552 }
2553
2554
2555 static void
2556 dissect_tcpopt_scps(const ip_tcp_opt *optp, tvbuff_t *tvb,
2557             int offset, guint optlen, packet_info *pinfo,
2558             proto_tree *opt_tree)
2559 {
2560     struct tcp_analysis *tcpd;
2561     proto_tree *field_tree = NULL;
2562     tcp_flow_t *flow;
2563     int         direction;
2564     proto_item *tf = NULL, *hidden_item;
2565     gchar       flags[64] = "<None>";
2566     gchar      *fstr[] = {"BETS", "SNACK1", "SNACK2", "COMP", "NLTS", "RESV3", "RESV2", "RESV1"};
2567     gint        i, bpos;
2568     guint8      capvector;
2569     guint8      connid;
2570
2571     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_kind, tvb,
2572                         offset, 1, FALSE);
2573     PROTO_ITEM_SET_HIDDEN(hidden_item);
2574     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_len, tvb,
2575                         offset + 1, 1, FALSE);
2576     PROTO_ITEM_SET_HIDDEN(hidden_item);
2577
2578     tcpd = get_tcp_conversation_data(NULL,pinfo);
2579
2580     /* check direction and get ua lists */
2581     direction=CMP_ADDRESS(&pinfo->src, &pinfo->dst);
2582
2583     /* if the addresses are equal, match the ports instead */
2584     if(direction==0) {
2585         direction= (pinfo->srcport > pinfo->destport) ? 1 : -1;
2586     }
2587
2588     if(direction>=0)
2589         flow =&(tcpd->flow1);
2590     else
2591         flow =&(tcpd->flow2);
2592
2593     /* If the option length == 4, this is a real SCPS capability option
2594      * See "CCSDS 714.0-B-2 (CCSDS Recommended Standard for SCPS Transport Protocol
2595      * (SCPS-TP)" Section 3.2.3 for definition.
2596      */
2597     if (optlen == 4) {
2598         capvector = tvb_get_guint8(tvb, offset + 2);
2599         flags[0] = '\0';
2600
2601         /* Decode the capabilities vector for display */
2602         for (i = 0; i < 5; i++) {
2603             bpos = 128 >> i;
2604             if (capvector & bpos) {
2605                 if (flags[0]) {
2606                     g_strlcat(flags, ", ", 64);
2607                 }
2608                 g_strlcat(flags, fstr[i], 64);
2609             }
2610         }
2611
2612         /* If lossless header compression is offered, there will be a
2613          * single octet connectionId following the capabilities vector
2614          */
2615         if (capvector & 0x10)
2616             connid    = tvb_get_guint8(tvb, offset + 3);
2617         else
2618             connid    = 0;
2619
2620         tf = proto_tree_add_uint_format(opt_tree, hf_tcp_option_scps_vector, tvb,
2621                                         offset, optlen, capvector,
2622                                         "%s: 0x%02x (%s)",
2623                                         optp->name, capvector, flags);
2624         hidden_item = proto_tree_add_boolean(opt_tree, hf_tcp_option_scps,
2625                                              tvb, offset, optlen, TRUE);
2626         PROTO_ITEM_SET_HIDDEN(hidden_item);
2627
2628         field_tree = proto_item_add_subtree(tf, ett_tcp_option_scps);
2629
2630         proto_tree_add_boolean(field_tree, hf_tcp_scpsoption_flags_bets, tvb,
2631                                offset + 2, 1, capvector);
2632         proto_tree_add_boolean(field_tree, hf_tcp_scpsoption_flags_snack1, tvb,
2633                                offset + 2, 1, capvector);
2634         proto_tree_add_boolean(field_tree, hf_tcp_scpsoption_flags_snack2, tvb,
2635                                offset + 2, 1, capvector);
2636         proto_tree_add_boolean(field_tree, hf_tcp_scpsoption_flags_compress, tvb,
2637                                offset + 2, 1, capvector);
2638         proto_tree_add_boolean(field_tree, hf_tcp_scpsoption_flags_nlts, tvb,
2639                                offset + 2, 1, capvector);
2640         proto_tree_add_boolean(field_tree, hf_tcp_scpsoption_flags_resv3, tvb,
2641                                offset + 2, 1, capvector);
2642         proto_tree_add_boolean(field_tree, hf_tcp_scpsoption_flags_resv2, tvb,
2643                                offset + 2, 1, capvector);
2644         proto_tree_add_boolean(field_tree, hf_tcp_scpsoption_flags_resv1, tvb,
2645                                offset + 2, 1, capvector);
2646         proto_tree_add_item(field_tree, hf_tcp_scpsoption_connection_id, tvb,
2647                             offset + 3, 1, 0);
2648
2649         tcp_info_append_str(pinfo, "SCPS", flags);
2650
2651         flow->scps_capable = 1;
2652
2653         if (connid)
2654             tcp_info_append_uint(pinfo, "Connection ID", connid);
2655     } else {
2656         /* The option length != 4, so this is an infamous "extended capabilities
2657          * option. See "CCSDS 714.0-B-2 (CCSDS Recommended Standard for SCPS
2658          * Transport Protocol (SCPS-TP)" Section 3.2.5 for definition.
2659          *
2660          *  As the format of this option is only partially defined (it is
2661          * a community (or more likely vendor) defined format beyond that, so
2662          * at least for now, we only parse the standardized portion of the option.
2663          */
2664         guint8 local_offset = 2;
2665         guint8 binding_space;
2666         guint8 extended_cap_length;
2667
2668         if (flow->scps_capable != 1) {
2669             /* There was no SCPS capabilities option preceeding this */
2670             proto_tree_add_uint_format(opt_tree, hf_tcp_option_scps_vector,
2671                                        tvb, offset, optlen, 0,
2672                                        "Illegal SCPS Extended Capabilities (%d bytes)",
2673                                        optlen);
2674         } else {
2675             tf = proto_tree_add_uint_format(opt_tree, hf_tcp_option_scps_vector,
2676                                             tvb, offset, optlen, 0,
2677                                             "SCPS Extended Capabilities (%d bytes)",
2678                                             optlen);
2679             field_tree=proto_item_add_subtree(tf, ett_tcp_option_scps_extended);
2680
2681             /* There may be multiple binding spaces included in a single option,
2682              * so we will semi-parse each of the stacked binding spaces - skipping
2683              * over the octets following the binding space identifier and length.
2684              */
2685             while (optlen > local_offset) {
2686
2687                 /* 1st octet is Extended Capability Binding Space */
2688                 binding_space = tvb_get_guint8(tvb, (offset + local_offset));
2689
2690                 /* 2nd octet (upper 4-bits) has binding space length in 16-bit words.
2691                  * As defined by the specification, this length is exclusive of the
2692                  * octets containing the extended capability type and length
2693                  */
2694                 extended_cap_length =
2695                     (tvb_get_guint8(tvb, (offset + local_offset + 1)) >> 4);
2696
2697                 /* Convert the extended capabilities length into bytes for display */
2698                 extended_cap_length = (extended_cap_length << 1);
2699
2700                 proto_tree_add_item(field_tree, hf_tcp_option_scps_binding, tvb, offset + local_offset, 1, 0);
2701                 proto_tree_add_uint(field_tree, hf_tcp_option_scps_binding_len, tvb, offset + local_offset + 1, 1, extended_cap_length);
2702
2703                 /* Step past the binding space and length octets */
2704                 local_offset += 2;
2705
2706                 proto_tree_add_text(field_tree, tvb, offset + local_offset,
2707                                     extended_cap_length,
2708                                     "Binding Space Data (%u bytes)",
2709                                     extended_cap_length);
2710
2711                 tcp_info_append_uint(pinfo, "EXCAP", binding_space);
2712
2713                 /* Step past the Extended capability data
2714                  * Treat the extended capability data area as opaque;
2715                  * If one desires to parse the extended capability data
2716                  * (say, in a vendor aware build of wireshark), it would
2717                  * be trigged here.
2718                  */
2719                 local_offset += extended_cap_length;
2720             }
2721         }
2722     }
2723 }
2724
2725 static void
2726 dissect_tcpopt_user_to(const ip_tcp_opt *optp, tvbuff_t *tvb,
2727     int offset, guint optlen, packet_info *pinfo, proto_tree *opt_tree)
2728 {
2729     proto_item *hidden_item, *tf;
2730     proto_tree *field_tree;
2731     gboolean g;
2732     guint16 to;
2733
2734     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_kind, tvb,
2735                         offset, 1, FALSE);
2736     PROTO_ITEM_SET_HIDDEN(hidden_item);
2737     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_len, tvb,
2738                         offset + 1, 1, FALSE);
2739     PROTO_ITEM_SET_HIDDEN(hidden_item);
2740
2741     g = tvb_get_ntohs(tvb, offset + 2) & 0x8000;
2742     to = tvb_get_ntohs(tvb, offset + 2) & 0x7FFF;
2743     hidden_item = proto_tree_add_boolean(opt_tree, hf_tcp_option_user_to, tvb, offset,
2744                                          optlen, TRUE);
2745     PROTO_ITEM_SET_HIDDEN(hidden_item);
2746
2747     tf = proto_tree_add_uint_format(opt_tree, hf_tcp_option_user_to_val, tvb, offset,
2748                                optlen, to, "%s: %u %s", optp->name, to, g ? "minutes" : "seconds");
2749     field_tree = proto_item_add_subtree(tf, *optp->subtree_index);
2750     proto_tree_add_item(field_tree, hf_tcp_option_user_to_granularity, tvb, offset + 2, 2, FALSE);
2751     proto_tree_add_item(field_tree, hf_tcp_option_user_to_val, tvb, offset + 2, 2, FALSE);
2752
2753     tcp_info_append_uint(pinfo, "USER_TO", to);
2754 }
2755
2756 /* See "CCSDS 714.0-B-2 (CCSDS Recommended Standard for SCPS
2757  * Transport Protocol (SCPS-TP)" Section 3.5 for definition of the SNACK option
2758  */
2759 static void
2760 dissect_tcpopt_snack(const ip_tcp_opt *optp, tvbuff_t *tvb,
2761             int offset, guint optlen, packet_info *pinfo,
2762             proto_tree *opt_tree)
2763 {
2764     struct tcp_analysis *tcpd=NULL;
2765     guint16 relative_hole_offset;
2766     guint16 relative_hole_size;
2767     guint16 base_mss = 0;
2768     guint32 ack;
2769     guint32 hole_start;
2770     guint32 hole_end;
2771     char    null_modifier[] = "\0";
2772     char    relative_modifier[] = "(relative)";
2773     char   *modifier = null_modifier;
2774     proto_item *hidden_item;
2775
2776     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_kind, tvb,
2777                         offset, 1, FALSE);
2778     PROTO_ITEM_SET_HIDDEN(hidden_item);
2779     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_len, tvb,
2780                         offset + 1, 1, FALSE);
2781     PROTO_ITEM_SET_HIDDEN(hidden_item);
2782
2783     tcpd = get_tcp_conversation_data(NULL,pinfo);
2784
2785     /* The SNACK option reports missing data with a granualarity of segments. */
2786     relative_hole_offset = tvb_get_ntohs(tvb, offset + 2);
2787     relative_hole_size = tvb_get_ntohs(tvb, offset + 4);
2788
2789     hidden_item = proto_tree_add_boolean(opt_tree, hf_tcp_option_snack, tvb,
2790                                          offset, optlen, TRUE);
2791     PROTO_ITEM_SET_HIDDEN(hidden_item);
2792
2793     hidden_item = proto_tree_add_uint(opt_tree, hf_tcp_option_snack_offset,
2794                                       tvb, offset, optlen, relative_hole_offset);
2795     PROTO_ITEM_SET_HIDDEN(hidden_item);
2796
2797     hidden_item = proto_tree_add_uint(opt_tree, hf_tcp_option_snack_size,
2798                                       tvb, offset, optlen, relative_hole_size);
2799     PROTO_ITEM_SET_HIDDEN(hidden_item);
2800     proto_tree_add_text(opt_tree, tvb, offset, optlen,
2801                         "%s: Offset %u, Size %u", optp->name,
2802                         relative_hole_offset, relative_hole_size);
2803
2804     ack   = tvb_get_ntohl(tvb, 8);
2805
2806     if (tcp_relative_seq) {
2807         ack -= tcpd->rev->base_seq;
2808         modifier = relative_modifier;
2809     }
2810
2811     /* To aid analysis, we can use a simple but generally effective heuristic
2812      * to report the most likely boundaries of the missing data.  If the
2813      * flow is scps_capable, we track the maximum sized segment that was
2814      * acknowledged by the receiver and use that as the reporting granularity.
2815      * This may be different from the negotiated MTU due to PMTUD or flows
2816      * that do not send max-sized segments.
2817      */
2818     base_mss = tcpd->fwd->maxsizeacked;
2819
2820     if (base_mss) {
2821         /* Scale the reported offset and hole size by the largest segment acked */
2822         hole_start = ack + (base_mss * relative_hole_offset);
2823         hole_end   = hole_start + (base_mss * relative_hole_size);
2824
2825         hidden_item = proto_tree_add_uint(opt_tree, hf_tcp_option_snack_le,
2826                                           tvb, offset, optlen, hole_start);
2827         PROTO_ITEM_SET_HIDDEN(hidden_item);
2828
2829         hidden_item = proto_tree_add_uint(opt_tree, hf_tcp_option_snack_re,
2830                                           tvb, offset, optlen, hole_end);
2831         PROTO_ITEM_SET_HIDDEN(hidden_item);
2832         proto_tree_add_text(opt_tree, tvb, offset, optlen,
2833                             "\tMissing Sequence %u - %u %s",
2834                             hole_start, hole_end, modifier);
2835
2836         tcp_info_append_uint(pinfo, "SNLE", hole_start);
2837         tcp_info_append_uint(pinfo, "SNRE", hole_end);
2838
2839         expert_add_info_format(pinfo, NULL, PI_SEQUENCE, PI_NOTE,
2840                                "SNACK Sequence %u - %u %s",
2841                                hole_start, hole_end, modifier);
2842     }
2843 }
2844
2845 static void
2846 dissect_tcpopt_mood(const ip_tcp_opt _U_*optp, tvbuff_t *tvb,
2847             int offset, guint optlen, packet_info *pinfo,
2848             proto_tree *opt_tree)
2849 {
2850     /* Mood TCP option, as defined by RFC5841 */
2851
2852     static const string_string mood_type[] = {
2853         { ":)",  "Happy" },
2854         { ":(",  "Sad" },
2855         { ":D",  "Amused" },
2856         { "%(",  "Confused" },
2857         { ":o",  "Bored" },
2858         { ":O",  "Surprised" },
2859         { ":P",  "Silly" },
2860         { ":@",  "Frustrated" },
2861         { ">:@", "Angry" },
2862         { ":|",  "Apathetic" },
2863         { ";)",  "Sneaky" },
2864         { ">:)", "Evil" },
2865         { NULL, NULL }
2866     };
2867
2868     proto_item *hidden_item;
2869     proto_item *mood_item;
2870     gchar *mood;
2871     mood = tvb_get_ephemeral_string(tvb, offset + 2, optlen-2);
2872
2873     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_kind, tvb,
2874                         offset, 1, FALSE);
2875     PROTO_ITEM_SET_HIDDEN(hidden_item);
2876     hidden_item = proto_tree_add_item(opt_tree, hf_tcp_option_len, tvb,
2877                         offset + 1, 1, FALSE);
2878     PROTO_ITEM_SET_HIDDEN(hidden_item);
2879
2880     hidden_item = proto_tree_add_boolean(opt_tree, hf_tcp_option_mood, tvb, offset+2, optlen-2, TRUE);
2881
2882     PROTO_ITEM_SET_HIDDEN(hidden_item);
2883
2884     mood_item = proto_tree_add_string_format_value(opt_tree, hf_tcp_option_mood_val, tvb, offset+2, optlen-2, mood,"%s (%s)", mood, str_to_str(mood, mood_type, "Unknown") );
2885     tcp_info_append_str(pinfo, "Mood", mood);
2886
2887     expert_add_info_format(pinfo, mood_item, PI_PROTOCOL, PI_NOTE, "The packet Mood is %s (%s) (RFC 5841)", mood, str_to_str(mood, mood_type, "Unknown"));
2888
2889 }
2890
2891 enum
2892 {
2893     PROBE_VERSION_UNSPEC = 0,
2894     PROBE_VERSION_1      = 1,
2895     PROBE_VERSION_2      = 2,
2896     PROBE_VERSION_MAX
2897 };
2898
2899 /* Probe type definition. */
2900 enum
2901 {
2902     PROBE_QUERY          = 0,
2903     PROBE_RESPONSE       = 1,
2904     PROBE_INTERNAL       = 2,
2905     PROBE_TRACE          = 3,
2906     PROBE_QUERY_SH       = 4,
2907     PROBE_RESPONSE_SH    = 5,
2908     PROBE_QUERY_INFO     = 6,
2909     PROBE_RESPONSE_INFO  = 7,
2910     PROBE_QUERY_INFO_SH  = 8,
2911     PROBE_QUERY_INFO_SID = 9,
2912     PROBE_RST            = 10,
2913     PROBE_TYPE_MAX
2914 };
2915
2916 static const value_string rvbd_probe_type_vs[] = {
2917     { PROBE_QUERY,          "Probe Query" },
2918     { PROBE_RESPONSE,       "Probe Response" },
2919     { PROBE_INTERNAL,       "Probe Internal" },
2920     { PROBE_TRACE,          "Probe Trace" },
2921     { PROBE_QUERY_SH,       "Probe Query SH" },
2922     { PROBE_RESPONSE_SH,    "Probe Response SH" },
2923     { PROBE_QUERY_INFO,     "Probe Query Info" },
2924     { PROBE_RESPONSE_INFO,  "Probe Response Info" },
2925     { PROBE_QUERY_INFO_SH,  "Probe Query Info SH" },
2926     { PROBE_QUERY_INFO_SID, "Probe Query Info Store ID" },
2927     { PROBE_RST,            "Probe Reset" },
2928     { 0, NULL }
2929 };
2930
2931
2932 #define PROBE_OPTLEN_OFFSET            1
2933
2934 #define PROBE_VERSION_TYPE_OFFSET      2
2935 #define PROBE_V1_RESERVED_OFFSET       3
2936 #define PROBE_V1_PROBER_OFFSET         4
2937 #define PROBE_V1_APPLI_VERSION_OFFSET  8
2938 #define PROBE_V1_PROXY_ADDR_OFFSET     8
2939 #define PROBE_V1_PROXY_PORT_OFFSET    12
2940 #define PROBE_V1_SH_CLIENT_ADDR_OFFSET 8
2941 #define PROBE_V1_SH_PROXY_ADDR_OFFSET 12
2942 #define PROBE_V1_SH_PROXY_PORT_OFFSET 16
2943
2944 #define PROBE_V2_INFO_OFFSET           3
2945
2946 #define PROBE_V2_INFO_CLIENT_ADDR_OFFSET 4
2947 #define PROBE_V2_INFO_STOREID_OFFSET   4
2948
2949 #define PROBE_VERSION_MASK          0x01
2950
2951 /* Probe Query Extra Info flags */
2952 #define RVBD_FLAGS_PROBE_LAST       0x01
2953 #define RVBD_FLAGS_PROBE_NCFE       0x04
2954
2955 /* Probe Response Extra Info flags */
2956 #define RVBD_FLAGS_PROBE_SERVER     0x01
2957 #define RVBD_FLAGS_PROBE_SSLCERT    0x02
2958 #define RVBD_FLAGS_PROBE            0x10
2959
2960 static void
2961 rvbd_probe_decode_version_type(const guint8 vt, guint8 *ver, guint8 *type)
2962 {
2963     if (vt & PROBE_VERSION_MASK) {
2964         *ver = PROBE_VERSION_1;
2965         *type = vt >> 4;
2966     } else {
2967         *ver = PROBE_VERSION_2;
2968         *type = vt >> 1;
2969     }
2970 }
2971
2972 static void
2973 rvbd_probe_resp_add_info(proto_item *pitem, packet_info *pinfo, guint32 ip, guint16 port)
2974 {
2975     proto_item_append_text(pitem, ", Server Steelhead: %s:%u", ip_to_str((guint8 *)&ip), port);
2976
2977     col_prepend_fstr(pinfo->cinfo, COL_INFO, "SA+, ");
2978 }
2979
2980 static void
2981 dissect_tcpopt_rvbd_probe(const ip_tcp_opt *optp _U_, tvbuff_t *tvb, int offset,
2982                           guint optlen, packet_info *pinfo, proto_tree *opt_tree)
2983 {
2984     guint8 ver, type;
2985     proto_tree *field_tree;
2986     proto_item *pitem;
2987
2988     rvbd_probe_decode_version_type(
2989         tvb_get_guint8(tvb, offset + PROBE_VERSION_TYPE_OFFSET),
2990         &ver, &type);
2991
2992     pitem = proto_tree_add_boolean_format_value(
2993         opt_tree, hf_tcp_option_rvbd_probe, tvb, offset, optlen, 1,
2994         "%s", val_to_str(type, rvbd_probe_type_vs, "Probe Unknown"));
2995
2996     if (type >= PROBE_TYPE_MAX)
2997         return;
2998
2999     /* optlen, type, ver are common for all probes */
3000     field_tree = proto_item_add_subtree(pitem, ett_tcp_opt_rvbd_probe);
3001     pitem = proto_tree_add_item(field_tree, hf_tcp_option_len, tvb,
3002                         offset + PROBE_OPTLEN_OFFSET, 1, FALSE);
3003     PROTO_ITEM_SET_HIDDEN(pitem);
3004     pitem = proto_tree_add_item(field_tree, hf_tcp_option_kind, tvb,
3005                         offset, 1, FALSE);
3006     PROTO_ITEM_SET_HIDDEN(pitem);
3007     proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_optlen, tvb,
3008                         offset + PROBE_OPTLEN_OFFSET, 1, FALSE);
3009
3010     if (ver == PROBE_VERSION_1) {
3011         guint32 ip;
3012         guint16 port;
3013
3014         proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_type1, tvb,
3015                             offset + PROBE_VERSION_TYPE_OFFSET, 1, FALSE);
3016         proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_version1, tvb,
3017                             offset + PROBE_VERSION_TYPE_OFFSET, 1, FALSE);
3018
3019         if (type == PROBE_INTERNAL)
3020             return;
3021
3022         proto_tree_add_text(field_tree, tvb, offset + PROBE_V1_RESERVED_OFFSET,
3023                             1, "Reserved");
3024
3025         ip = tvb_get_ipv4(tvb, offset + PROBE_V1_PROBER_OFFSET);
3026         proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_prober, tvb,
3027                             offset + PROBE_V1_PROBER_OFFSET, 4, FALSE);
3028
3029         switch (type) {
3030
3031         case PROBE_QUERY:
3032         case PROBE_QUERY_SH:
3033         case PROBE_TRACE:
3034             proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_appli_ver, tvb,
3035                                 offset + PROBE_V1_APPLI_VERSION_OFFSET, 2,
3036                                 FALSE);
3037
3038             proto_item_append_text(pitem, ", CSH IP: %s", ip_to_str((guint8 *)&ip));
3039
3040             if (check_col(pinfo->cinfo, COL_INFO)) {
3041                 /* Small look-ahead hack to distinguish S+ from S+* */
3042 #define PROBE_V1_QUERY_LEN    10
3043                 const guint8 qinfo_hdr[] = { 0x4c, 0x04, 0x0c };
3044                 int not_cfe = 0;
3045                 /* tvb_memeql seems to be the only API that doesn't throw
3046                    an exception in case of an error */
3047                 if (tvb_memeql(tvb, offset + PROBE_V1_QUERY_LEN,
3048                                qinfo_hdr, sizeof(qinfo_hdr)) == 0) {
3049                         not_cfe = tvb_get_guint8(tvb, offset + PROBE_V1_QUERY_LEN +
3050                                                  sizeof(qinfo_hdr)) & RVBD_FLAGS_PROBE_NCFE;
3051                 }
3052                 col_prepend_fstr(pinfo->cinfo, COL_INFO, "S%s, ",
3053                                  type == PROBE_TRACE ? "#" :
3054                                  not_cfe ? "+*" : "+");
3055            }
3056            break;
3057
3058         case PROBE_RESPONSE:
3059             ip = tvb_get_ipv4(tvb, offset + PROBE_V1_PROXY_ADDR_OFFSET);
3060             proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_proxy, tvb,
3061                                 offset + PROBE_V1_PROXY_ADDR_OFFSET, 4, FALSE);
3062
3063             port = tvb_get_ntohs(tvb, offset + PROBE_V1_PROXY_PORT_OFFSET);
3064             proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_proxy_port, tvb,
3065                                 offset + PROBE_V1_PROXY_PORT_OFFSET, 2, FALSE);
3066
3067             rvbd_probe_resp_add_info(pitem, pinfo, ip, port);
3068             break;
3069
3070         case PROBE_RESPONSE_SH:
3071             proto_tree_add_item(field_tree,
3072                                 hf_tcp_option_rvbd_probe_client, tvb,
3073                                 offset + PROBE_V1_SH_CLIENT_ADDR_OFFSET, 4,
3074                                 FALSE);
3075
3076             ip = tvb_get_ipv4(tvb, offset + PROBE_V1_SH_PROXY_ADDR_OFFSET);
3077             proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_proxy, tvb,
3078                                 offset + PROBE_V1_SH_PROXY_ADDR_OFFSET, 4, FALSE);
3079
3080             port = tvb_get_ntohs(tvb, offset + PROBE_V1_SH_PROXY_PORT_OFFSET);
3081             proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_proxy_port, tvb,
3082                                 offset + PROBE_V1_SH_PROXY_PORT_OFFSET, 2, FALSE);
3083
3084             rvbd_probe_resp_add_info(pitem, pinfo, ip, port);
3085             break;
3086         }
3087     }
3088     else if (ver == PROBE_VERSION_2) {
3089         proto_item *ver_pi;
3090         proto_item *flag_pi;
3091         proto_tree *flag_tree;
3092         guint8 flags;
3093
3094         proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_type2, tvb,
3095                             offset + PROBE_VERSION_TYPE_OFFSET, 1, FALSE);
3096
3097         proto_tree_add_uint_format_value(
3098             field_tree, hf_tcp_option_rvbd_probe_version2, tvb,
3099             offset + PROBE_VERSION_TYPE_OFFSET, 1, ver, "%u", ver);
3100         /* Use version1 for filtering purposes because version2 packet
3101            value is 0, but filtering is usually done for value 2 */
3102         ver_pi = proto_tree_add_uint(field_tree, hf_tcp_option_rvbd_probe_version1, tvb,
3103                                      offset + PROBE_VERSION_TYPE_OFFSET, 1, ver);
3104         PROTO_ITEM_SET_HIDDEN(ver_pi);
3105
3106         switch (type) {
3107
3108         case PROBE_QUERY_INFO:
3109         case PROBE_QUERY_INFO_SH:
3110         case PROBE_QUERY_INFO_SID:
3111             flags = tvb_get_guint8(tvb, offset + PROBE_V2_INFO_OFFSET);
3112             flag_pi = proto_tree_add_uint(field_tree, hf_tcp_option_rvbd_probe_flags,
3113                                           tvb, offset + PROBE_V2_INFO_OFFSET,
3114                                           1, flags);
3115
3116             flag_tree = proto_item_add_subtree(flag_pi, ett_tcp_opt_rvbd_probe_flags);
3117             proto_tree_add_item(flag_tree,
3118                                 hf_tcp_option_rvbd_probe_flag_not_cfe,
3119                                 tvb, offset + PROBE_V2_INFO_OFFSET, 1, FALSE);
3120             proto_tree_add_item(flag_tree,
3121                                 hf_tcp_option_rvbd_probe_flag_last_notify,
3122                                 tvb, offset + PROBE_V2_INFO_OFFSET, 1, FALSE);
3123
3124             if (type == PROBE_QUERY_INFO_SH)
3125                 proto_tree_add_item(flag_tree,
3126                                     hf_tcp_option_rvbd_probe_client, tvb,
3127                                     offset + PROBE_V2_INFO_CLIENT_ADDR_OFFSET,
3128                                     4, FALSE);
3129             else if (type == PROBE_QUERY_INFO_SID)
3130                 proto_tree_add_item(flag_tree,
3131                                     hf_tcp_option_rvbd_probe_storeid, tvb,
3132                                     offset + PROBE_V2_INFO_STOREID_OFFSET,
3133                                     4, FALSE);
3134
3135             if (type != PROBE_QUERY_INFO_SID &&
3136                 check_col(pinfo->cinfo, COL_INFO) &&
3137                 (tvb_get_guint8(tvb, 13) & (TH_SYN|TH_ACK)) == (TH_SYN|TH_ACK) &&
3138                 (flags & RVBD_FLAGS_PROBE_LAST)) {
3139                 col_prepend_fstr(pinfo->cinfo, COL_INFO, "SA++, ");
3140             }
3141
3142             break;
3143
3144         case PROBE_RESPONSE_INFO:
3145             flag_pi = proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_flags,
3146                                           tvb, offset + PROBE_V2_INFO_OFFSET,
3147                                           1, FALSE);
3148
3149             flag_tree = proto_item_add_subtree(flag_pi, ett_tcp_opt_rvbd_probe_flags);
3150             proto_tree_add_item(flag_tree,
3151                                 hf_tcp_option_rvbd_probe_flag_probe_cache,
3152                                 tvb, offset + PROBE_V2_INFO_OFFSET, 1, FALSE);
3153             proto_tree_add_item(flag_tree,
3154                                 hf_tcp_option_rvbd_probe_flag_sslcert,
3155                                 tvb, offset + PROBE_V2_INFO_OFFSET, 1, FALSE);
3156             proto_tree_add_item(flag_tree,
3157                                 hf_tcp_option_rvbd_probe_flag_server_connected,
3158                                 tvb, offset + PROBE_V2_INFO_OFFSET, 1, FALSE);
3159             break;
3160
3161         case PROBE_RST:
3162             proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_flags,
3163                                   tvb, offset + PROBE_V2_INFO_OFFSET,
3164                                   1, FALSE);
3165             break;
3166         }
3167     }
3168 }
3169
3170 enum {
3171     TRPY_OPTNUM_OFFSET        = 0,
3172     TRPY_OPTLEN_OFFSET        = 1,
3173
3174     TRPY_OPTIONS_OFFSET       = 2,
3175     TRPY_SRC_ADDR_OFFSET      = 4,
3176     TRPY_DST_ADDR_OFFSET      = 8,
3177     TRPY_SRC_PORT_OFFSET      = 12,
3178     TRPY_DST_PORT_OFFSET      = 14,
3179     TRPY_CLIENT_PORT_OFFSET   = 16,
3180 };
3181
3182 /* Trpy Flags */
3183 #define RVBD_FLAGS_TRPY_MODE         0x0001
3184 #define RVBD_FLAGS_TRPY_OOB          0x0002
3185 #define RVBD_FLAGS_TRPY_CHKSUM       0x0004
3186 #define RVBD_FLAGS_TRPY_FW_RST       0x0100
3187 #define RVBD_FLAGS_TRPY_FW_RST_INNER 0x0200
3188 #define RVBD_FLAGS_TRPY_FW_RST_PROBE 0x0400
3189
3190 static const true_false_string trpy_mode_str = {
3191     "Port Transparency",
3192     "Full Transparency"
3193 };
3194
3195 static void
3196 dissect_tcpopt_rvbd_trpy(const ip_tcp_opt *optp _U_, tvbuff_t *tvb,
3197                         int offset, guint optlen, packet_info *pinfo,
3198                         proto_tree *opt_tree)
3199 {
3200     proto_tree *field_tree;
3201     proto_tree *flag_tree;
3202     proto_item *pitem;
3203     proto_item *flag_pi;
3204     guint32 src, dst;
3205     guint16 sport, dport, flags;
3206     static dissector_handle_t sport_handle = NULL;
3207
3208     col_prepend_fstr(pinfo->cinfo, COL_INFO, "TRPY, ");
3209
3210     pitem = proto_tree_add_boolean_format_value(
3211         opt_tree, hf_tcp_option_rvbd_trpy, tvb, offset, optlen, 1,
3212         "%s", "");
3213
3214     field_tree = proto_item_add_subtree(pitem, ett_tcp_opt_rvbd_trpy);
3215     pitem = proto_tree_add_item(field_tree, hf_tcp_option_len, tvb,
3216                         offset + PROBE_OPTLEN_OFFSET, 1, FALSE);
3217     PROTO_ITEM_SET_HIDDEN(pitem);
3218     pitem = proto_tree_add_item(field_tree, hf_tcp_option_kind, tvb,
3219                         offset, 1, FALSE);
3220     PROTO_ITEM_SET_HIDDEN(pitem);
3221     proto_tree_add_item(field_tree, hf_tcp_option_rvbd_probe_optlen, tvb,
3222                         offset + PROBE_OPTLEN_OFFSET, 1, FALSE);
3223
3224     flags = tvb_get_ntohs(tvb, offset + TRPY_OPTIONS_OFFSET);
3225     flag_pi = proto_tree_add_item(field_tree, hf_tcp_option_rvbd_trpy_flags,
3226                                   tvb, offset + TRPY_OPTIONS_OFFSET,
3227                                   2, FALSE);
3228
3229     flag_tree = proto_item_add_subtree(flag_pi, ett_tcp_opt_rvbd_trpy_flags);
3230     proto_tree_add_item(flag_tree, hf_tcp_option_rvbd_trpy_flag_fw_rst_probe,
3231                         tvb, offset + TRPY_OPTIONS_OFFSET, 2, FALSE);
3232     proto_tree_add_item(flag_tree, hf_tcp_option_rvbd_trpy_flag_fw_rst_inner,
3233                         tvb, offset + TRPY_OPTIONS_OFFSET, 2, FALSE);
3234     proto_tree_add_item(flag_tree, hf_tcp_option_rvbd_trpy_flag_fw_rst,
3235                         tvb, offset + TRPY_OPTIONS_OFFSET, 2, FALSE);
3236     proto_tree_add_item(flag_tree, hf_tcp_option_rvbd_trpy_flag_chksum,
3237                         tvb, offset + TRPY_OPTIONS_OFFSET, 2, FALSE);
3238     proto_tree_add_item(flag_tree, hf_tcp_option_rvbd_trpy_flag_oob,
3239                         tvb, offset + TRPY_OPTIONS_OFFSET, 2, FALSE);
3240     proto_tree_add_item(flag_tree, hf_tcp_option_rvbd_trpy_flag_mode,
3241                         tvb, offset + TRPY_OPTIONS_OFFSET, 2, FALSE);
3242
3243     src = tvb_get_ipv4(tvb, offset + TRPY_SRC_ADDR_OFFSET);
3244     proto_tree_add_item(field_tree, hf_tcp_option_rvbd_trpy_src,
3245                         tvb, offset + TRPY_SRC_ADDR_OFFSET, 4, FALSE);
3246
3247     dst = tvb_get_ipv4(tvb, offset + TRPY_DST_ADDR_OFFSET);
3248     proto_tree_add_item(field_tree, hf_tcp_option_rvbd_trpy_dst,
3249                         tvb, offset + TRPY_DST_ADDR_OFFSET, 4, FALSE);
3250
3251     sport = tvb_get_ntohs(tvb, offset + TRPY_SRC_PORT_OFFSET);
3252     proto_tree_add_item(field_tree, hf_tcp_option_rvbd_trpy_src_port,
3253                         tvb, offset + TRPY_SRC_PORT_OFFSET, 2, FALSE);
3254
3255     dport = tvb_get_ntohs(tvb, offset + TRPY_DST_PORT_OFFSET);
3256     proto_tree_add_item(field_tree, hf_tcp_option_rvbd_trpy_dst_port,
3257                         tvb, offset + TRPY_DST_PORT_OFFSET, 2, FALSE);
3258
3259     proto_item_append_text(pitem, "%s:%u -> %s:%u",
3260                            ip_to_str((guint8 *)&src), sport,
3261                            ip_to_str((guint8 *)&dst), dport);
3262
3263     /* Client port only set on SYN: optlen == 18 */
3264     if ((flags & RVBD_FLAGS_TRPY_OOB) && (optlen > TCPOLEN_RVBD_TRPY_MIN))
3265         proto_tree_add_item(field_tree, hf_tcp_option_rvbd_trpy_client_port,
3266                             tvb, offset + TRPY_CLIENT_PORT_OFFSET, 2, FALSE);
3267
3268     /* We need to map this TCP session on our own dissector instead of what
3269        Wireshark thinks runs on these ports */
3270     if (sport_handle == NULL) {
3271         sport_handle = find_dissector("sport");
3272     }
3273     if (sport_handle != NULL) {
3274         conversation_t *conversation;
3275         conversation = find_conversation(pinfo->fd->num,
3276             &pinfo->src, &pinfo->dst, pinfo->ptype,
3277             pinfo->srcport, pinfo->destport, 0);
3278         if (conversation == NULL) {
3279             conversation = conversation_new(pinfo->fd->num,
3280                 &pinfo->src, &pinfo->dst, pinfo->ptype,
3281                 pinfo->srcport, pinfo->destport, 0);
3282         }
3283         if (conversation->dissector_handle != sport_handle) {
3284             conversation_set_dissector(conversation, sport_handle);
3285         }
3286     }
3287 }
3288
3289 static const ip_tcp_opt tcpopts[] = {
3290     {
3291         TCPOPT_EOL,
3292         "End of Option List (EOL)",
3293         NULL,
3294         NO_LENGTH,
3295         0,
3296         NULL,
3297     },
3298     {
3299         TCPOPT_NOP,
3300         "No-Operation (NOP)",
3301         NULL,
3302         NO_LENGTH,
3303         0,
3304         NULL,
3305     },
3306     {
3307         TCPOPT_MSS,
3308         "Maximum segment size",
3309         NULL,
3310         FIXED_LENGTH,
3311         TCPOLEN_MSS,
3312         dissect_tcpopt_mss
3313     },
3314     {
3315         TCPOPT_WINDOW,
3316         "Window scale",
3317         NULL,
3318         FIXED_LENGTH,
3319         TCPOLEN_WINDOW,
3320         dissect_tcpopt_wscale
3321     },
3322     {
3323         TCPOPT_SACK_PERM,
3324         "SACK permitted",
3325         NULL,
3326         FIXED_LENGTH,
3327         TCPOLEN_SACK_PERM,
3328         dissect_tcpopt_sack_perm,
3329     },
3330     {
3331         TCPOPT_SACK,
3332         "SACK",
3333         &ett_tcp_option_sack,
3334         VARIABLE_LENGTH,
3335         TCPOLEN_SACK_MIN,
3336         dissect_tcpopt_sack
3337     },
3338     {
3339         TCPOPT_ECHO,
3340         "Echo",
3341         NULL,
3342         FIXED_LENGTH,
3343         TCPOLEN_ECHO,
3344         dissect_tcpopt_echo
3345     },
3346     {
3347         TCPOPT_ECHOREPLY,
3348         "Echo reply",
3349         NULL,
3350         FIXED_LENGTH,
3351         TCPOLEN_ECHOREPLY,
3352         dissect_tcpopt_echo
3353     },
3354     {
3355         TCPOPT_TIMESTAMP,
3356         "Timestamps",
3357         NULL,
3358         FIXED_LENGTH,
3359         TCPOLEN_TIMESTAMP,
3360         dissect_tcpopt_timestamp
3361     },
3362     {
3363         TCPOPT_CC,
3364         "CC",
3365         NULL,
3366         FIXED_LENGTH,
3367         TCPOLEN_CC,
3368         dissect_tcpopt_cc
3369     },
3370     {
3371         TCPOPT_CCNEW,
3372         "CC.NEW",
3373         NULL,
3374         FIXED_LENGTH,
3375         TCPOLEN_CCNEW,
3376         dissect_tcpopt_cc
3377     },
3378     {
3379         TCPOPT_CCECHO,
3380         "CC.ECHO",
3381         NULL,
3382         FIXED_LENGTH,
3383         TCPOLEN_CCECHO,
3384         dissect_tcpopt_cc
3385     },
3386     {
3387         TCPOPT_MD5,
3388         "TCP MD5 signature",
3389         NULL,
3390         FIXED_LENGTH,
3391         TCPOLEN_MD5,
3392         NULL
3393     },
3394     {
3395         TCPOPT_SCPS,
3396         "SCPS capabilities",
3397         &ett_tcp_option_scps,
3398         VARIABLE_LENGTH,
3399         TCPOLEN_SCPS,
3400         dissect_tcpopt_scps
3401     },
3402     {
3403         TCPOPT_SNACK,
3404         "Selective Negative Acknowledgement",
3405         NULL,
3406         FIXED_LENGTH,
3407         TCPOLEN_SNACK,
3408         dissect_tcpopt_snack
3409     },
3410     {
3411         TCPOPT_RECBOUND,
3412         "SCPS record boundary",
3413         NULL,
3414         FIXED_LENGTH,
3415         TCPOLEN_RECBOUND,
3416         NULL
3417     },
3418     {
3419         TCPOPT_CORREXP,
3420         "SCPS corruption experienced",
3421         NULL,
3422         FIXED_LENGTH,
3423         TCPOLEN_CORREXP,
3424         NULL
3425     },
3426     {
3427         TCPOPT_MOOD,
3428         "Packet Mood",
3429         NULL,
3430         VARIABLE_LENGTH,
3431         TCPOLEN_MOOD_MIN,
3432         dissect_tcpopt_mood
3433     },
3434     {
3435         TCPOPT_QS,
3436         "Quick-Start",
3437         NULL,
3438         FIXED_LENGTH,
3439         TCPOLEN_QS,
3440         dissect_tcpopt_qs
3441     },
3442     {
3443         TCPOPT_USER_TO,
3444         "User Timeout",
3445         &ett_tcp_option_user_to,
3446         FIXED_LENGTH,
3447         TCPOLEN_USER_TO,
3448         dissect_tcpopt_user_to
3449   },
3450   {
3451         TCPOPT_RVBD_PROBE,
3452         "Riverbed Probe",
3453         NULL,
3454         VARIABLE_LENGTH,
3455         TCPOLEN_RVBD_PROBE_MIN,
3456         dissect_tcpopt_rvbd_probe
3457   },
3458   {
3459         TCPOPT_RVBD_TRPY,
3460         "Riverbed Transparency",
3461         NULL,
3462         FIXED_LENGTH,
3463         TCPOLEN_RVBD_TRPY_MIN,
3464         dissect_tcpopt_rvbd_trpy
3465   },
3466   {
3467         TCPOPT_EXP_FD,
3468         "Experimental",
3469         NULL,
3470         VARIABLE_LENGTH,
3471         TCPOLEN_EXP_MIN,
3472         dissect_tcpopt_exp
3473   },
3474   {
3475         TCPOPT_EXP_FE,
3476         "Experimental",
3477         NULL,
3478         VARIABLE_LENGTH,
3479         TCPOLEN_EXP_MIN,
3480         dissect_tcpopt_exp
3481   }
3482 };
3483
3484 #define N_TCP_OPTS  array_length(tcpopts)
3485
3486 /* Determine if there is a sub-dissector and call it; return TRUE
3487    if there was a sub-dissector, FALSE otherwise.
3488
3489    This has been separated into a stand alone routine to other protocol
3490    dissectors can call to it, e.g., SOCKS. */
3491
3492 static gboolean try_heuristic_first = FALSE;
3493
3494
3495 /* this function can be called with tcpd==NULL as from the msproxy dissector */
3496 gboolean
3497 decode_tcp_ports(tvbuff_t *tvb, int offset, packet_info *pinfo,
3498     proto_tree *tree, int src_port, int dst_port,
3499     struct tcp_analysis *tcpd)
3500 {
3501     tvbuff_t *next_tvb;
3502     int low_port, high_port;
3503     int save_desegment_offset;
3504     guint32 save_desegment_len;
3505
3506     /* Don't call subdissectors for keepalives.  Even though they do contain
3507      * payload "data", it's just garbage.  Display any data the keepalive
3508      * packet might contain though.
3509      */
3510     if(tcpd && tcpd->ta){
3511         if(tcpd->ta->flags&TCP_A_KEEP_ALIVE){
3512             next_tvb = tvb_new_subset_remaining(tvb, offset);
3513             call_dissector(data_handle, next_tvb, pinfo, tree);
3514             return TRUE;
3515         }
3516     }
3517
3518     next_tvb = tvb_new_subset_remaining(tvb, offset);
3519
3520 /* determine if this packet is part of a conversation and call dissector */
3521 /* for the conversation if available */
3522
3523     if (try_conversation_dissector(&pinfo->src, &pinfo->dst, PT_TCP,
3524                                    src_port, dst_port, next_tvb, pinfo, tree)){
3525         pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
3526         return TRUE;
3527     }
3528
3529     if (try_heuristic_first) {
3530         /* do lookup with the heuristic subdissector table */
3531         save_desegment_offset = pinfo->desegment_offset;
3532         save_desegment_len = pinfo->desegment_len;
3533         if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree)){
3534             pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
3535             return TRUE;
3536         }
3537         /*
3538          * They rejected the packet; make sure they didn't also request
3539          * desegmentation (we could just override the request, but
3540          * rejecting a packet *and* requesting desegmentation is a sign
3541          * of the dissector's code needing clearer thought, so we fail
3542          * so that the problem is made more obvious).
3543          */
3544         DISSECTOR_ASSERT(save_desegment_offset == pinfo->desegment_offset &&
3545                          save_desegment_len == pinfo->desegment_len);
3546     }
3547
3548     /* Do lookups with the subdissector table.
3549        We try the port number with the lower value first, followed by the
3550        port number with the higher value.  This means that, for packets
3551        where a dissector is registered for *both* port numbers:
3552
3553        1) we pick the same dissector for traffic going in both directions;
3554
3555        2) we prefer the port number that's more likely to be the right
3556        one (as that prefers well-known ports to reserved ports);
3557
3558        although there is, of course, no guarantee that any such strategy
3559        will always pick the right port number.
3560
3561        XXX - we ignore port numbers of 0, as some dissectors use a port
3562        number of 0 to disable the port. */
3563     if (src_port > dst_port) {
3564         low_port = dst_port;
3565         high_port = src_port;
3566     } else {
3567         low_port = src_port;
3568         high_port = dst_port;
3569     }
3570     if (low_port != 0 &&
3571         dissector_try_uint(subdissector_table, low_port, next_tvb, pinfo, tree)){
3572         pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
3573         return TRUE;
3574     }
3575     if (high_port != 0 &&
3576         dissector_try_uint(subdissector_table, high_port, next_tvb, pinfo, tree)){
3577         pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
3578         return TRUE;
3579     }
3580
3581     if (!try_heuristic_first) {
3582         /* do lookup with the heuristic subdissector table */
3583         save_desegment_offset = pinfo->desegment_offset;
3584         save_desegment_len = pinfo->desegment_len;
3585         if (dissector_try_heuristic(heur_subdissector_list, next_tvb, pinfo, tree)){
3586             pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
3587             return TRUE;
3588         }
3589         /*
3590          * They rejected the packet; make sure they didn't also request
3591          * desegmentation (we could just override the request, but
3592          * rejecting a packet *and* requesting desegmentation is a sign
3593          * of the dissector's code needing clearer thought, so we fail
3594          * so that the problem is made more obvious).
3595          */
3596         DISSECTOR_ASSERT(save_desegment_offset == pinfo->desegment_offset &&
3597                          save_desegment_len == pinfo->desegment_len);
3598     }
3599
3600     /* Oh, well, we don't know this; dissect it as data. */
3601     call_dissector(data_handle,next_tvb, pinfo, tree);
3602
3603     pinfo->want_pdu_tracking -= !!(pinfo->want_pdu_tracking);
3604     return FALSE;
3605 }
3606
3607 static void
3608 process_tcp_payload(tvbuff_t *tvb, volatile int offset, packet_info *pinfo,
3609     proto_tree *tree, proto_tree *tcp_tree, int src_port, int dst_port,
3610     guint32 seq, guint32 nxtseq, gboolean is_tcp_segment,
3611     struct tcp_analysis *tcpd)
3612 {
3613     pinfo->want_pdu_tracking=0;
3614
3615     TRY {
3616         if(is_tcp_segment){
3617             /*qqq   see if it is an unaligned PDU */
3618             if(tcpd && tcp_analyze_seq && (!tcp_desegment)){
3619                 if(seq || nxtseq){
3620                     offset=scan_for_next_pdu(tvb, tcp_tree, pinfo, offset,
3621                         seq, nxtseq, tcpd->fwd->multisegment_pdus);
3622                 }
3623             }
3624         }
3625         /* if offset is -1 this means that this segment is known
3626          * to be fully inside a previously detected pdu
3627          * so we dont even need to try to dissect it either.
3628          */
3629         if( (offset!=-1) &&
3630             decode_tcp_ports(tvb, offset, pinfo, tree, src_port,
3631                 dst_port, tcpd) ){
3632             /*
3633              * We succeeded in handing off to a subdissector.
3634              *
3635              * Is this a TCP segment or a reassembled chunk of
3636              * TCP payload?
3637              */
3638             if(is_tcp_segment){
3639                 /* if !visited, check want_pdu_tracking and
3640                    store it in table */
3641                 if(tcpd && (!pinfo->fd->flags.visited) &&
3642                     tcp_analyze_seq && pinfo->want_pdu_tracking){
3643                     if(seq || nxtseq){
3644                         pdu_store_sequencenumber_of_next_pdu(
3645                             pinfo,
3646                             seq,
3647                             nxtseq+pinfo->bytes_until_next_pdu,
3648                             tcpd->fwd->multisegment_pdus);
3649                     }
3650                 }
3651             }
3652         }
3653     }
3654     CATCH_ALL {
3655         /* We got an exception. At this point the dissection is
3656          * completely aborted and execution will be transfered back
3657          * to (probably) the frame dissector.
3658          * Here we have to place whatever we want the dissector
3659          * to do before aborting the tcp dissection.
3660          */
3661         /*
3662          * Is this a TCP segment or a reassembled chunk of TCP
3663          * payload?
3664          */
3665         if(is_tcp_segment){
3666             /*
3667              * It's from a TCP segment.
3668              *
3669              * if !visited, check want_pdu_tracking and store it
3670              * in table
3671              */
3672             if(tcpd && (!pinfo->fd->flags.visited) && tcp_analyze_seq && pinfo->want_pdu_tracking){
3673                 if(seq || nxtseq){
3674                     pdu_store_sequencenumber_of_next_pdu(pinfo,
3675                         seq,
3676                         nxtseq+pinfo->bytes_until_next_pdu,
3677                         tcpd->fwd->multisegment_pdus);
3678                 }
3679             }
3680         }
3681         RETHROW;
3682     }
3683     ENDTRY;
3684 }
3685
3686 void
3687 dissect_tcp_payload(tvbuff_t *tvb, packet_info *pinfo, int offset, guint32 seq,
3688             guint32 nxtseq, guint32 sport, guint32 dport,
3689             proto_tree *tree, proto_tree *tcp_tree,
3690             struct tcp_analysis *tcpd)
3691 {
3692     gboolean save_fragmented;
3693
3694     /* Can we desegment this segment? */
3695     if (pinfo->can_desegment) {
3696         /* Yes. */
3697         desegment_tcp(tvb, pinfo, offset, seq, nxtseq, sport, dport, tree,
3698                       tcp_tree, tcpd);
3699     } else {
3700         /* No - just call the subdissector.
3701            Mark this as fragmented, so if somebody throws an exception,
3702            we don't report it as a malformed frame. */
3703         save_fragmented = pinfo->fragmented;
3704         pinfo->fragmented = TRUE;
3705         process_tcp_payload(tvb, offset, pinfo, tree, tcp_tree, sport, dport,
3706                             seq, nxtseq, TRUE, tcpd);
3707         pinfo->fragmented = save_fragmented;
3708     }
3709 }
3710
3711 static void
3712 dissect_tcp(tvbuff_t *tvb, packet_info *pinfo, proto_tree *tree)
3713 {
3714     guint8  th_off_x2; /* combines th_off and th_x2 */
3715     guint16 th_sum;
3716     guint32 ack;
3717     guint16 th_urp;
3718     proto_tree *tcp_tree = NULL, *field_tree = NULL;
3719     proto_item *ti = NULL, *tf, *hidden_item;
3720     int        offset = 0;
3721     emem_strbuf_t *flags_strbuf = ep_strbuf_new_label("<None>");
3722     const gchar *flags[] = {"FIN", "SYN", "RST", "PSH", "ACK", "URG", "ECN", "CWR", "NS"};
3723     gint       i;
3724     guint      bpos;
3725     guint      optlen;
3726     guint32    nxtseq = 0;
3727     guint      reported_len;
3728     vec_t      cksum_vec[4];
3729     guint32    phdr[2];
3730     guint16    computed_cksum;
3731     guint16    real_window;
3732     guint      length_remaining;
3733     gboolean   desegment_ok;
3734     struct tcpinfo tcpinfo;
3735     struct tcpheader *tcph;
3736     proto_item *tf_syn = NULL, *tf_fin = NULL, *tf_rst = NULL, *scaled_pi;
3737     conversation_t *conv=NULL;
3738     struct tcp_analysis *tcpd=NULL;
3739     struct tcp_per_packet_data_t *tcppd=NULL;
3740     proto_item *item;
3741     proto_tree *checksum_tree;
3742
3743     tcph=ep_alloc(sizeof(struct tcpheader));
3744     SET_ADDRESS(&tcph->ip_src, pinfo->src.type, pinfo->src.len, pinfo->src.data);
3745     SET_ADDRESS(&tcph->ip_dst, pinfo->dst.type, pinfo->dst.len, pinfo->dst.data);
3746
3747     col_set_str(pinfo->cinfo, COL_PROTOCOL, "TCP");
3748
3749     /* Clear out the Info column. */
3750     col_clear(pinfo->cinfo, COL_INFO);
3751
3752     tcph->th_sport = tvb_get_ntohs(tvb, offset);
3753     tcph->th_dport = tvb_get_ntohs(tvb, offset + 2);
3754     col_append_fstr(pinfo->cinfo, COL_INFO, "%s > %s",
3755                     get_tcp_port(tcph->th_sport), get_tcp_port(tcph->th_dport));
3756     if (tree) {
3757         if (tcp_summary_in_tree) {
3758             ti = proto_tree_add_protocol_format(tree, proto_tcp, tvb, 0, -1,
3759                                                 "Transmission Control Protocol, Src Port: %s (%u), Dst Port: %s (%u)",
3760                                                 get_tcp_port(tcph->th_sport), tcph->th_sport,
3761                                                 get_tcp_port(tcph->th_dport), tcph->th_dport);
3762         }
3763         else {
3764             ti = proto_tree_add_item(tree, proto_tcp, tvb, 0, -1, FALSE);
3765         }
3766         tcp_tree = proto_item_add_subtree(ti, ett_tcp);
3767         pinfo->tcp_tree=tcp_tree;
3768
3769         proto_tree_add_uint_format(tcp_tree, hf_tcp_srcport, tvb, offset, 2, tcph->th_sport,
3770                                    "Source port: %s (%u)", get_tcp_port(tcph->th_sport), tcph->th_sport);
3771         proto_tree_add_uint_format(tcp_tree, hf_tcp_dstport, tvb, offset + 2, 2, tcph->th_dport,
3772                                    "Destination port: %s (%u)", get_tcp_port(tcph->th_dport), tcph->th_dport);
3773         hidden_item = proto_tree_add_uint(tcp_tree, hf_tcp_port, tvb, offset, 2, tcph->th_sport);
3774         PROTO_ITEM_SET_HIDDEN(hidden_item);
3775         hidden_item = proto_tree_add_uint(tcp_tree, hf_tcp_port, tvb, offset + 2, 2, tcph->th_dport);
3776         PROTO_ITEM_SET_HIDDEN(hidden_item);
3777
3778         /*  If we're dissecting the headers of a TCP packet in an ICMP packet
3779          *  then go ahead and put the sequence numbers in the tree now (because
3780          *  they won't be put in later because the ICMP packet only contains up
3781          *  to the sequence number).
3782          *  We should only need to do this for IPv4 since IPv6 will hopefully
3783          *  carry enough TCP payload for this dissector to put the sequence
3784          *  numbers in via the regular code path.
3785          */
3786         if (pinfo->layer_names != NULL && pinfo->layer_names->str != NULL) {
3787             /*  use strstr because g_strrstr is only present in glib2.0 and
3788              *  g_str_has_suffix in glib2.2
3789              */
3790             if (strstr(pinfo->layer_names->str, "icmp:ip") != NULL)
3791                 proto_tree_add_item(tcp_tree, hf_tcp_seq, tvb, offset + 4, 4, FALSE);
3792         }
3793     }
3794
3795     /* Set the source and destination port numbers as soon as we get them,
3796        so that they're available to the "Follow TCP Stream" code even if
3797        we throw an exception dissecting the rest of the TCP header. */
3798     pinfo->ptype = PT_TCP;
3799     pinfo->srcport = tcph->th_sport;
3800     pinfo->destport = tcph->th_dport;
3801
3802     tcph->th_seq = tvb_get_ntohl(tvb, offset + 4);
3803     tcph->th_ack = tvb_get_ntohl(tvb, offset + 8);
3804     th_off_x2 = tvb_get_guint8(tvb, offset + 12);
3805     tcph->th_flags = tvb_get_ntohs(tvb, offset + 12) & 0x0FFF;
3806     tcph->th_win = tvb_get_ntohs(tvb, offset + 14);
3807     real_window = tcph->th_win;
3808     tcph->th_hlen = hi_nibble(th_off_x2) * 4;  /* TCP header length, in bytes */
3809
3810     /* find(or create if needed) the conversation for this tcp session */
3811     conv=find_or_create_conversation(pinfo);
3812     tcpd=get_tcp_conversation_data(conv,pinfo);
3813
3814     /* If this is a SYN packet, then check if it's seq-nr is different
3815      * from the base_seq of the retrieved conversation. If this is the
3816      * case, create a new conversation with the same addresses and ports
3817      * and set the TA_PORTS_REUSED flag. If the seq-nr is the same as
3818      * the base_seq, then do nothing so it will be marked as a retrans-
3819      * mission later.
3820      */
3821     if(tcpd && ((tcph->th_flags&(TH_SYN|TH_ACK))==TH_SYN) &&
3822        (tcpd->fwd->base_seq!=0) &&
3823        (tcph->th_seq!=tcpd->fwd->base_seq) ) {
3824         if (!(pinfo->fd->flags.visited)) {
3825             conv=conversation_new(pinfo->fd->num, &pinfo->src, &pinfo->dst, pinfo->ptype, pinfo->srcport, pinfo->destport, 0);
3826             tcpd=get_tcp_conversation_data(conv,pinfo);
3827         }
3828         if(!tcpd->ta)
3829             tcp_analyze_get_acked_struct(pinfo->fd->num, TRUE, tcpd);
3830         tcpd->ta->flags|=TCP_A_REUSED_PORTS;
3831     }
3832
3833     if (tcpd) {
3834         item = proto_tree_add_uint(tcp_tree, hf_tcp_stream, tvb, offset, 0, tcpd->stream);
3835         PROTO_ITEM_SET_GENERATED(item);
3836     }
3837
3838     /* Do we need to calculate timestamps relative to the tcp-stream? */
3839     if (tcp_calculate_ts) {
3840         tcppd = p_get_proto_data(pinfo->fd, proto_tcp);
3841
3842         /*
3843          * Calculate the timestamps relative to this conversation (but only on the
3844          * first run when frames are accessed sequentially)
3845          */
3846         if (!(pinfo->fd->flags.visited))
3847             tcp_calculate_timestamps(pinfo, tcpd, tcppd);
3848     }
3849
3850     /*
3851      * If we've been handed an IP fragment, we don't know how big the TCP
3852      * segment is, so don't do anything that requires that we know that.
3853      *
3854      * The same applies if we're part of an error packet.  (XXX - if the
3855      * ICMP and ICMPv6 dissectors could set a "this is how big the IP
3856      * header says it is" length in the tvbuff, we could use that; such
3857      * a length might also be useful for handling packets where the IP
3858      * length is bigger than the actual data available in the frame; the
3859      * dissectors should trust that length, and then throw a
3860      * ReportedBoundsError exception when they go past the end of the frame.)
3861      *
3862      * We also can't determine the segment length if the reported length
3863      * of the TCP packet is less than the TCP header length.
3864      */
3865     reported_len = tvb_reported_length(tvb);
3866
3867     if (!pinfo->fragmented && !pinfo->in_error_pkt) {
3868         if (reported_len < tcph->th_hlen) {
3869             proto_item *pi;
3870             pi = proto_tree_add_text(tcp_tree, tvb, offset, 0,
3871                                      "Short segment. Segment/fragment does not contain a full TCP header"
3872                                      " (might be NMAP or someone else deliberately sending unusual packets)");
3873             PROTO_ITEM_SET_GENERATED(pi);
3874             expert_add_info_format(pinfo, pi, PI_MALFORMED, PI_WARN, "Short segment");
3875             tcph->th_have_seglen = FALSE;
3876         } else {
3877             /* Compute the length of data in this segment. */
3878             tcph->th_seglen = reported_len - tcph->th_hlen;
3879             tcph->th_have_seglen = TRUE;
3880
3881             if (tree) { /* Add the seglen as an invisible field */
3882
3883                 hidden_item = proto_tree_add_uint(ti, hf_tcp_len, tvb, offset+12, 1, tcph->th_seglen);
3884                 PROTO_ITEM_SET_HIDDEN(hidden_item);
3885
3886             }
3887
3888
3889             /* handle TCP seq# analysis parse all new segments we see */
3890             if(tcp_analyze_seq){
3891                 if(!(pinfo->fd->flags.visited)){
3892                     tcp_analyze_sequence_number(pinfo, tcph->th_seq, tcph->th_ack, tcph->th_seglen, tcph->th_flags, tcph->th_win, tcpd);
3893                 }
3894                 if(tcpd && tcp_relative_seq) {
3895                     (tcph->th_seq) -= tcpd->fwd->base_seq;
3896                     (tcph->th_ack) -= tcpd->rev->base_seq;
3897                 }
3898             }
3899
3900             /* re-calculate window size, based on scaling factor */
3901             if (!(tcph->th_flags&TH_SYN)) {   /* SYNs are never scaled */
3902                 if (tcpd && (tcpd->fwd->win_scale>=0)) {
3903                     (tcph->th_win)<<=tcpd->fwd->win_scale;
3904                 }
3905             }
3906
3907             /* Compute the sequence number of next octet after this segment. */
3908             nxtseq = tcph->th_seq + tcph->th_seglen;
3909         }
3910     } else
3911         tcph->th_have_seglen = FALSE;
3912
3913     if (check_col(pinfo->cinfo, COL_INFO) || tree) {
3914         gboolean first_flag = TRUE;
3915         for (i = 0; i < 9; i++) {
3916             bpos = 1 << i;
3917             if (tcph->th_flags & bpos) {
3918                 if (first_flag) {
3919                     ep_strbuf_truncate(flags_strbuf, 0);
3920                 }
3921                 ep_strbuf_append_printf(flags_strbuf, "%s%s", first_flag ? "" : ", ", flags[i]);
3922                 first_flag = FALSE;
3923             }
3924         }
3925         if (tcph->th_flags & 0x0E00) {
3926             if (first_flag) {
3927                 ep_strbuf_truncate(flags_strbuf, 0);
3928             }
3929             ep_strbuf_append_printf(flags_strbuf, "%sReserved", first_flag ? "" : ", ");
3930         }
3931     }
3932
3933     col_append_fstr(pinfo->cinfo, COL_INFO, " [%s] Seq=%u", flags_strbuf->str, tcph->th_seq);
3934     if (tcph->th_flags&TH_ACK) {
3935         col_append_fstr(pinfo->cinfo, COL_INFO, " Ack=%u", tcph->th_ack);
3936     }
3937     col_append_fstr(pinfo->cinfo, COL_INFO, " Win=%u", tcph->th_win);
3938
3939     if (tree) {
3940         if (tcp_summary_in_tree) {
3941             proto_item_append_text(ti, ", Seq: %u", tcph->th_seq);
3942         }
3943         if(tcp_relative_seq){
3944             proto_tree_add_uint_format(tcp_tree, hf_tcp_seq, tvb, offset + 4, 4, tcph->th_seq, "Sequence number: %u    (relative sequence number)", tcph->th_seq);
3945         } else {
3946             proto_tree_add_uint(tcp_tree, hf_tcp_seq, tvb, offset + 4, 4, tcph->th_seq);
3947         }
3948     }
3949
3950     if (tcph->th_hlen < TCPH_MIN_LEN) {
3951         /* Give up at this point; we put the source and destination port in
3952            the tree, before fetching the header length, so that they'll
3953            show up if this is in the failing packet in an ICMP error packet,
3954            but it's now time to give up if the header length is bogus. */
3955         col_append_fstr(pinfo->cinfo, COL_INFO, ", bogus TCP header length (%u, must be at least %u)",
3956                         tcph->th_hlen, TCPH_MIN_LEN);
3957         if (tree) {
3958             proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, tvb, offset + 12, 1, tcph->th_hlen,
3959                                        "Header length: %u bytes (bogus, must be at least %u)", tcph->th_hlen,
3960                                        TCPH_MIN_LEN);
3961         }
3962         return;
3963     }
3964
3965     if (tree) {
3966         if (tcp_summary_in_tree) {
3967             if(tcph->th_flags&TH_ACK){
3968                 proto_item_append_text(ti, ", Ack: %u", tcph->th_ack);
3969             }
3970             if (tcph->th_have_seglen)
3971                 proto_item_append_text(ti, ", Len: %u", tcph->th_seglen);
3972         }
3973         proto_item_set_len(ti, tcph->th_hlen);
3974         if (tcph->th_have_seglen) {
3975             if (nxtseq != tcph->th_seq) {
3976                 if(tcp_relative_seq){
3977                     tf=proto_tree_add_uint_format(tcp_tree, hf_tcp_nxtseq, tvb, offset, 0, nxtseq, "Next sequence number: %u    (relative sequence number)", nxtseq);
3978                 } else {
3979                     tf=proto_tree_add_uint(tcp_tree, hf_tcp_nxtseq, tvb, offset, 0, nxtseq);
3980                 }
3981                 PROTO_ITEM_SET_GENERATED(tf);
3982             }
3983         }
3984     }
3985     
3986     if (tcph->th_flags & TH_ACK) {
3987         if (tree) {
3988             if (tcp_relative_seq){
3989                 proto_tree_add_uint_format(tcp_tree, hf_tcp_ack, tvb, offset + 8, 4, tcph->th_ack, "Acknowledgement number: %u    (relative ack number)", tcph->th_ack);
3990             } else {
3991                 proto_tree_add_uint(tcp_tree, hf_tcp_ack, tvb, offset + 8, 4, tcph->th_ack);
3992             }
3993         }
3994     } else {
3995         /* Verify that the ACK field is zero */
3996         ack = tvb_get_ntohl(tvb, offset+8);
3997         if (ack != 0){
3998             item = proto_tree_add_uint_format(tcp_tree, hf_tcp_ack, tvb, offset + 8, 4, ack,
3999                        "Acknowledgement Number: 0x%08x [should be 0x00000000 because ACK flag is not set]",
4000                        ack);
4001             expert_add_info_format(pinfo, item, PI_PROTOCOL, PI_WARN,
4002                 "Acknowledgement number: Broken TCP. The acknowledge field is nonzero while the ACK flag is not set");
4003         }
4004     }
4005
4006     if (tree) {
4007         proto_tree_add_uint_format(tcp_tree, hf_tcp_hdr_len, tvb, offset + 12, 1, tcph->th_hlen,
4008                                    "Header length: %u bytes", tcph->th_hlen);
4009         tf = proto_tree_add_uint_format(tcp_tree, hf_tcp_flags, tvb, offset + 12, 2,
4010                                         tcph->th_flags, "Flags: 0x%03x (%s)", tcph->th_flags, flags_strbuf->str);
4011         field_tree = proto_item_add_subtree(tf, ett_tcp_flags);
4012         proto_tree_add_boolean(field_tree, hf_tcp_flags_res, tvb, offset + 12, 1, tcph->th_flags);
4013         proto_tree_add_boolean(field_tree, hf_tcp_flags_ns, tvb, offset + 12, 1, tcph->th_flags);
4014         proto_tree_add_boolean(field_tree, hf_tcp_flags_cwr, tvb, offset + 13, 1, tcph->th_flags);
4015         proto_tree_add_boolean(field_tree, hf_tcp_flags_ecn, tvb, offset + 13, 1, tcph->th_flags);
4016         proto_tree_add_boolean(field_tree, hf_tcp_flags_urg, tvb, offset + 13, 1, tcph->th_flags);
4017         proto_tree_add_boolean(field_tree, hf_tcp_flags_ack, tvb, offset + 13, 1, tcph->th_flags);
4018         proto_tree_add_boolean(field_tree, hf_tcp_flags_push, tvb, offset + 13, 1, tcph->th_flags);
4019         tf_rst = proto_tree_add_boolean(field_tree, hf_tcp_flags_reset, tvb, offset + 13, 1, tcph->th_flags);
4020         tf_syn = proto_tree_add_boolean(field_tree, hf_tcp_flags_syn, tvb, offset + 13, 1, tcph->th_flags);
4021         tf_fin = proto_tree_add_boolean(field_tree, hf_tcp_flags_fin, tvb, offset + 13, 1, tcph->th_flags);
4022
4023         /* As discussed in bug 5541, it is better to use two separate
4024          * fields for the real and calculated window size.
4025          */
4026         proto_tree_add_uint(tcp_tree, hf_tcp_window_size_value, tvb, offset + 14, 2, real_window);
4027         scaled_pi = proto_tree_add_uint(tcp_tree, hf_tcp_window_size, tvb, offset + 14, 2, tcph->th_win);
4028         PROTO_ITEM_SET_GENERATED(scaled_pi);
4029
4030         if( !(tcph->th_flags&TH_SYN) && tcpd ) {
4031             switch (tcpd->fwd->win_scale) {
4032
4033             case -1:
4034                 scaled_pi = proto_tree_add_int_format(tcp_tree, hf_tcp_window_size_scalefactor, tvb, offset + 14, 2, tcpd->fwd->win_scale, "Window size scaling factor: %d (unknown)", tcpd->fwd->win_scale);
4035                 PROTO_ITEM_SET_GENERATED(scaled_pi);
4036                 break;
4037
4038             case -2:
4039                 scaled_pi = proto_tree_add_int_format(tcp_tree, hf_tcp_window_size_scalefactor, tvb, offset + 14, 2, tcpd->fwd->win_scale, "Window size scaling factor: %d (no window scaling used)", tcpd->fwd->win_scale);
4040                 PROTO_ITEM_SET_GENERATED(scaled_pi);
4041                 break;
4042
4043             default:
4044                 scaled_pi = proto_tree_add_int_format(tcp_tree, hf_tcp_window_size_scalefactor, tvb, offset + 14, 2, 1<<tcpd->fwd->win_scale, "Window size scaling factor: %d", 1<<tcpd->fwd->win_scale);
4045                 PROTO_ITEM_SET_GENERATED(scaled_pi);
4046             }
4047         }
4048     }
4049
4050     if(tcph->th_flags & TH_SYN) {
4051         if(tcph->th_flags & TH_ACK)
4052             expert_add_info_format(pinfo, tf_syn, PI_SEQUENCE, PI_CHAT, "Connection establish acknowledge (SYN+ACK): server port %s",
4053                                    get_tcp_port(tcph->th_sport));
4054         else
4055             expert_add_info_format(pinfo, tf_syn, PI_SEQUENCE, PI_CHAT, "Connection establish request (SYN): server port %s",
4056                                    get_tcp_port(tcph->th_dport));
4057     }
4058     if(tcph->th_flags & TH_FIN)
4059         /* XXX - find a way to know the server port and output only that one */
4060         expert_add_info_format(pinfo, tf_fin, PI_SEQUENCE, PI_CHAT, "Connection finish (FIN)");
4061     if(tcph->th_flags & TH_RST)
4062         /* XXX - find a way to know the server port and output only that one */
4063         expert_add_info_format(pinfo, tf_rst, PI_SEQUENCE, PI_CHAT, "Connection reset (RST)");
4064
4065     /* Supply the sequence number of the first byte and of the first byte
4066        after the segment. */
4067     tcpinfo.seq = tcph->th_seq;
4068     tcpinfo.nxtseq = nxtseq;
4069     tcpinfo.lastackseq = tcph->th_ack;
4070
4071     /* Assume we'll pass un-reassembled data to subdissectors. */
4072     tcpinfo.is_reassembled = FALSE;
4073
4074     pinfo->private_data = &tcpinfo;
4075
4076     /*
4077      * Assume, initially, that we can't desegment.
4078      */
4079     pinfo->can_desegment = 0;
4080     th_sum = tvb_get_ntohs(tvb, offset + 16);
4081     if (!pinfo->fragmented && tvb_bytes_exist(tvb, 0, reported_len)) {
4082         /* The packet isn't part of an un-reassembled fragmented datagram
4083            and isn't truncated.  This means we have all the data, and thus
4084            can checksum it and, unless it's being returned in an error
4085            packet, are willing to allow subdissectors to request reassembly
4086            on it. */
4087
4088         if (tcp_check_checksum) {
4089             /* We haven't turned checksum checking off; checksum it. */
4090
4091             /* Set up the fields of the pseudo-header. */
4092             cksum_vec[0].ptr = pinfo->src.data;
4093             cksum_vec[0].len = pinfo->src.len;
4094             cksum_vec[1].ptr = pinfo->dst.data;
4095             cksum_vec[1].len = pinfo->dst.len;
4096             cksum_vec[2].ptr = (const guint8 *)phdr;
4097             switch (pinfo->src.type) {
4098
4099             case AT_IPv4:
4100                 phdr[0] = g_htonl((IP_PROTO_TCP<<16) + reported_len);
4101                 cksum_vec[2].len = 4;
4102                 break;
4103
4104             case AT_IPv6:
4105                 phdr[0] = g_htonl(reported_len);
4106                 phdr[1] = g_htonl(IP_PROTO_TCP);
4107                 cksum_vec[2].len = 8;
4108                 break;
4109
4110             default:
4111                 /* TCP runs only atop IPv4 and IPv6.... */
4112                 DISSECTOR_ASSERT_NOT_REACHED();
4113                 break;
4114             }
4115             cksum_vec[3].ptr = tvb_get_ptr(tvb, offset, reported_len);
4116             cksum_vec[3].len = reported_len;
4117             computed_cksum = in_cksum(cksum_vec, 4);
4118             if (computed_cksum == 0 && th_sum == 0xffff) {
4119                 item = proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
4120                                                   offset + 16, 2, th_sum,
4121                                                   "Checksum: 0x%04x [should be 0x0000 (see RFC 1624)]", th_sum);
4122
4123                 checksum_tree = proto_item_add_subtree(item, ett_tcp_checksum);
4124                 item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_good, tvb,
4125                                               offset + 16, 2, FALSE);
4126                 PROTO_ITEM_SET_GENERATED(item);
4127                 item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_bad, tvb,
4128                                               offset + 16, 2, FALSE);
4129                 PROTO_ITEM_SET_GENERATED(item);
4130                 expert_add_info_format(pinfo, item, PI_CHECKSUM, PI_WARN, "TCP Checksum 0xffff instead of 0x0000 (see RFC 1624)");
4131
4132                 col_append_str(pinfo->cinfo, COL_INFO, " [TCP CHECKSUM 0xFFFF]");
4133
4134                 /* Checksum is treated as valid on most systems, so we're willing to desegment it. */
4135                 desegment_ok = TRUE;
4136             } else if (computed_cksum == 0) {
4137                 item = proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
4138                                                   offset + 16, 2, th_sum, "Checksum: 0x%04x [correct]", th_sum);
4139
4140                 checksum_tree = proto_item_add_subtree(item, ett_tcp_checksum);
4141                 item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_good, tvb,
4142                                               offset + 16, 2, TRUE);
4143                 PROTO_ITEM_SET_GENERATED(item);
4144                 item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_bad, tvb,
4145                                               offset + 16, 2, FALSE);
4146                 PROTO_ITEM_SET_GENERATED(item);
4147
4148                 /* Checksum is valid, so we're willing to desegment it. */
4149                 desegment_ok = TRUE;
4150             } else if (th_sum == 0) {
4151                 /* checksum is probably fine but checksum offload is used */
4152                 item = proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
4153                                                   offset + 16, 2, th_sum, "Checksum: 0x%04x [Checksum Offloaded]", th_sum);
4154
4155                 checksum_tree = proto_item_add_subtree(item, ett_tcp_checksum);
4156                 item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_good, tvb,
4157                                               offset + 16, 2, FALSE);
4158                 PROTO_ITEM_SET_GENERATED(item);
4159                 item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_bad, tvb,
4160                                               offset + 16, 2, FALSE);
4161                 PROTO_ITEM_SET_GENERATED(item);
4162
4163                 /* Checksum is (probably) valid, so we're willing to desegment it. */
4164                 desegment_ok = TRUE;
4165             } else {
4166                 item = proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
4167                                                   offset + 16, 2, th_sum,
4168                                                   "Checksum: 0x%04x [incorrect, should be 0x%04x (maybe caused by \"TCP checksum offload\"?)]", th_sum,
4169                                                   in_cksum_shouldbe(th_sum, computed_cksum));
4170
4171                 checksum_tree = proto_item_add_subtree(item, ett_tcp_checksum);
4172                 item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_good, tvb,
4173                                               offset + 16, 2, FALSE);
4174                 PROTO_ITEM_SET_GENERATED(item);
4175                 item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_bad, tvb,
4176                                               offset + 16, 2, TRUE);
4177                 PROTO_ITEM_SET_GENERATED(item);
4178                 expert_add_info_format(pinfo, item, PI_CHECKSUM, PI_ERROR, "Bad checksum");
4179
4180                 col_append_str(pinfo->cinfo, COL_INFO, " [TCP CHECKSUM INCORRECT]");
4181
4182                 /* Checksum is invalid, so we're not willing to desegment it. */
4183                 desegment_ok = FALSE;
4184                 pinfo->noreassembly_reason = " [incorrect TCP checksum]";
4185             }
4186         } else {
4187             item = proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
4188                                               offset + 16, 2, th_sum, "Checksum: 0x%04x [validation disabled]", th_sum);
4189
4190             checksum_tree = proto_item_add_subtree(item, ett_tcp_checksum);
4191             item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_good, tvb,
4192                                           offset + 16, 2, FALSE);
4193             PROTO_ITEM_SET_GENERATED(item);
4194             item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_bad, tvb,
4195                                           offset + 16, 2, FALSE);
4196             PROTO_ITEM_SET_GENERATED(item);
4197
4198             /* We didn't check the checksum, and don't care if it's valid,
4199                so we're willing to desegment it. */
4200             desegment_ok = TRUE;
4201         }
4202     } else {
4203         /* We don't have all the packet data, so we can't checksum it... */
4204         item = proto_tree_add_uint_format(tcp_tree, hf_tcp_checksum, tvb,
4205                                           offset + 16, 2, th_sum, "Checksum: 0x%04x [unchecked, not all data available]", th_sum);
4206
4207         checksum_tree = proto_item_add_subtree(item, ett_tcp_checksum);
4208         item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_good, tvb,
4209                                       offset + 16, 2, FALSE);
4210         PROTO_ITEM_SET_GENERATED(item);
4211         item = proto_tree_add_boolean(checksum_tree, hf_tcp_checksum_bad, tvb,
4212                                       offset + 16, 2, FALSE);
4213         PROTO_ITEM_SET_GENERATED(item);
4214
4215         /* ...and aren't willing to desegment it. */
4216         desegment_ok = FALSE;
4217     }
4218
4219     if (desegment_ok) {
4220         /* We're willing to desegment this.  Is desegmentation enabled? */
4221         if (tcp_desegment) {
4222             /* Yes - is this segment being returned in an error packet? */
4223             if (!pinfo->in_error_pkt) {
4224                 /* No - indicate that we will desegment.
4225                    We do NOT want to desegment segments returned in error
4226                    packets, as they're not part of a TCP connection. */
4227                 pinfo->can_desegment = 2;
4228             }
4229         }
4230     }
4231
4232     th_urp = tvb_get_ntohs(tvb, offset + 18);
4233     if (tcph->th_flags & TH_URG) {
4234         /* Export the urgent pointer, for the benefit of protocols such as
4235            rlogin. */
4236         tcpinfo.urgent = TRUE;
4237         tcpinfo.urgent_pointer = th_urp;
4238         col_append_fstr(pinfo->cinfo, COL_INFO, " Urg=%u", th_urp);
4239         if (tcp_tree != NULL)
4240             proto_tree_add_uint(tcp_tree, hf_tcp_urgent_pointer, tvb, offset + 18, 2, th_urp);
4241     } else {
4242         tcpinfo.urgent = FALSE;
4243         if (th_urp) {
4244             item = proto_tree_add_uint_format(tcp_tree, hf_tcp_urgent_pointer, tvb, offset + 18, 2, th_urp,
4245                                               "Urgent Pointer: 0x%04x [should be 0x0000 because URG flag is not set]",
4246                                               th_urp);
4247             expert_add_info_format(pinfo, item, PI_PROTOCOL, PI_WARN,
4248                 "Urgent Pointer: Broken TCP. The urgent pointer field is nonzero while the URG flag is not set");
4249         }
4250     }
4251
4252
4253     if (tcph->th_have_seglen) {
4254         col_append_fstr(pinfo->cinfo, COL_INFO, " Len=%u", tcph->th_seglen);
4255     }
4256
4257     /* Decode TCP options, if any. */
4258     if (tcph->th_hlen > TCPH_MIN_LEN) {
4259         /* There's more than just the fixed-length header.  Decode the
4260            options. */
4261         optlen = tcph->th_hlen - TCPH_MIN_LEN; /* length of options, in bytes */
4262         tvb_ensure_bytes_exist(tvb, offset +  20, optlen);
4263         if (tcp_tree != NULL) {
4264             guint8 *p_options = ep_tvb_memdup(tvb, offset + 20, optlen);
4265             tf = proto_tree_add_bytes_format(tcp_tree, hf_tcp_options, tvb, offset +  20,
4266                                              optlen, p_options, "Options: (%u bytes)", optlen);
4267             field_tree = proto_item_add_subtree(tf, ett_tcp_options);
4268         } else {
4269             tf = NULL;
4270             field_tree = NULL;
4271         }
4272         dissect_ip_tcp_options(tvb, offset + 20, optlen,
4273                                tcpopts, N_TCP_OPTS, TCPOPT_EOL, pinfo, field_tree, tf);
4274     }
4275
4276     if(!pinfo->fd->flags.visited){
4277         if((tcph->th_flags & TH_SYN)==TH_SYN) {
4278             /* Check the validity of the window scale value
4279              */
4280             verify_tcp_window_scaling((tcph->th_flags&TH_ACK)==TH_ACK,tcpd);
4281         }
4282
4283         if((tcph->th_flags & (TH_SYN|TH_ACK))==(TH_SYN|TH_ACK)) {
4284             /* If the SYN or the SYN+ACK offered SCPS capabilities,
4285              * validate the flow's bidirectional scps capabilities.
4286              * The or protects against broken implementations offering
4287              * SCPS capabilities on SYN+ACK even if it wasn't offered with the SYN
4288              */
4289             if(tcpd && ((tcpd->rev->scps_capable) || (tcpd->fwd->scps_capable))) {
4290                 if (tcpd->flow1.scps_capable && tcpd->flow2.scps_capable) {
4291                     expert_add_info_format(pinfo, 0, PI_SEQUENCE, PI_NOTE,
4292                                            "Connection establish request (SYN-ACK): SCPS Capabilities Negotiated");
4293                 }
4294             }
4295         }
4296     }
4297
4298     /* Skip over header + options */
4299     offset += tcph->th_hlen;
4300
4301     /* Check the packet length to see if there's more data
4302        (it could be an ACK-only packet) */
4303     length_remaining = tvb_length_remaining(tvb, offset);
4304
4305     if (tcph->th_have_seglen) {
4306         if( data_out_file ) {
4307             reassemble_tcp( tcpd->stream,                         /* tcp stream index */
4308                             tcph->th_seq,                         /* sequence number */
4309                             tcph->th_ack,                         /* acknowledgement number */
4310                             tcph->th_seglen,                      /* data length */
4311                             (gchar*)tvb_get_ptr(tvb, offset, length_remaining), /* data */
4312                             length_remaining,                     /* captured data length */
4313                             ( tcph->th_flags & TH_SYN ),          /* is syn set? */
4314                             &pinfo->net_src,
4315                             &pinfo->net_dst,
4316                             pinfo->srcport,
4317                             pinfo->destport);
4318         }
4319     }
4320
4321     /* handle TCP seq# analysis, print any extra SEQ/ACK data for this segment*/
4322     if(tcp_analyze_seq){
4323         tcp_print_sequence_number_analysis(pinfo, tvb, tcp_tree, tcpd);
4324     }
4325
4326     /* handle conversation timestamps */
4327     if(tcp_calculate_ts){
4328         tcp_print_timestamps(pinfo, tvb, tcp_tree, tcpd, tcppd);
4329     }
4330
4331     tap_queue_packet(tcp_tap, pinfo, tcph);
4332
4333
4334     /* A FIN packet might complete reassembly so we need to explicitly
4335      * check for this here.
4336      */
4337     if(tcph->th_have_seglen && tcpd && (tcph->th_flags & TH_FIN)
4338        && (tcpd->fwd->flags&TCP_FLOW_REASSEMBLE_UNTIL_FIN) ){
4339         struct tcp_multisegment_pdu *msp;
4340
4341         /* find the most previous PDU starting before this sequence number */
4342         msp=se_tree_lookup32_le(tcpd->fwd->multisegment_pdus, tcph->th_seq-1);
4343         if(msp){
4344             fragment_data *ipfd_head;
4345
4346             ipfd_head = fragment_add(tvb, offset, pinfo, msp->first_frame,
4347                                      tcp_fragment_table,
4348                                      tcph->th_seq - msp->seq,
4349                                      tcph->th_seglen,
4350                                      FALSE );
4351             if(ipfd_head){
4352                 tvbuff_t *next_tvb;
4353
4354                 /* create a new TVB structure for desegmented data
4355                  * datalen-1 to strip the dummy FIN byte off
4356                  */
4357                 next_tvb = tvb_new_child_real_data(tvb, ipfd_head->data, ipfd_head->datalen, ipfd_head->datalen);
4358
4359                 /* add desegmented data to the data source list */
4360                 add_new_data_source(pinfo, next_tvb, "Reassembled TCP");
4361
4362                 /* call the payload dissector
4363                  * but make sure we don't offer desegmentation any more
4364                  */
4365                 pinfo->can_desegment = 0;
4366
4367                 process_tcp_payload(next_tvb, 0, pinfo, tree, tcp_tree, tcph->th_sport, tcph->th_dport, tcph->th_seq, nxtseq, FALSE, tcpd);
4368
4369                 print_tcp_fragment_tree(ipfd_head, tree, tcp_tree, pinfo, next_tvb);
4370
4371                 return;
4372             }
4373         }
4374     }
4375
4376     if (tcpd && ((tcpd->fwd && tcpd->fwd->command) || (tcpd->rev && tcpd->rev->command))) {
4377         ti = proto_tree_add_text(tcp_tree, tvb, offset, 0, "Process Information");
4378         PROTO_ITEM_SET_GENERATED(ti);
4379         field_tree = proto_item_add_subtree(ti, ett_tcp_process_info);
4380         if (tcpd->fwd && tcpd->fwd->command) {
4381             proto_tree_add_uint_format_value(field_tree, hf_tcp_proc_dst_uid, tvb, 0, 0,
4382                                              tcpd->fwd->process_uid, "%u", tcpd->fwd->process_uid);
4383             proto_tree_add_uint_format_value(field_tree, hf_tcp_proc_dst_pid, tvb, 0, 0,
4384                                              tcpd->fwd->process_pid, "%u", tcpd->fwd->process_pid);
4385             proto_tree_add_string_format_value(field_tree, hf_tcp_proc_dst_uname, tvb, 0, 0,
4386                                                tcpd->fwd->username, "%s", tcpd->fwd->username);
4387             proto_tree_add_string_format_value(field_tree, hf_tcp_proc_dst_cmd, tvb, 0, 0,
4388                                                tcpd->fwd->command, "%s", tcpd->fwd->command);
4389         }
4390         if (tcpd->rev && tcpd->rev->command) {
4391             proto_tree_add_uint_format_value(field_tree, hf_tcp_proc_src_uid, tvb, 0, 0,
4392                                              tcpd->rev->process_uid, "%u", tcpd->rev->process_uid);
4393             proto_tree_add_uint_format_value(field_tree, hf_tcp_proc_src_pid, tvb, 0, 0,
4394                                              tcpd->rev->process_pid, "%u", tcpd->rev->process_pid);
4395             proto_tree_add_string_format_value(field_tree, hf_tcp_proc_src_uname, tvb, 0, 0,
4396                                                tcpd->rev->username, "%s", tcpd->rev->username);
4397             proto_tree_add_string_format_value(field_tree, hf_tcp_proc_src_cmd, tvb, 0, 0,
4398                                                tcpd->rev->command, "%s", tcpd->rev->command);
4399         }
4400     }
4401
4402     /*
4403      * XXX - what, if any, of this should we do if this is included in an
4404      * error packet?  It might be nice to see the details of the packet
4405      * that caused the ICMP error, but it might not be nice to have the
4406      * dissector update state based on it.
4407      * Also, we probably don't want to run TCP taps on those packets.
4408      */
4409     if (length_remaining != 0) {
4410         if (tcph->th_flags & TH_RST) {
4411             /*
4412              * RFC1122 says:
4413              *
4414              *  4.2.2.12  RST Segment: RFC-793 Section 3.4
4415              *
4416              *    A TCP SHOULD allow a received RST segment to include data.
4417              *
4418              *    DISCUSSION
4419              *         It has been suggested that a RST segment could contain
4420              *         ASCII text that encoded and explained the cause of the
4421              *         RST.  No standard has yet been established for such
4422              *         data.
4423              *
4424              * so for segments with RST we just display the data as text.
4425              */
4426             proto_tree_add_text(tcp_tree, tvb, offset, length_remaining,
4427                                 "Reset cause: %s",
4428                                 tvb_format_text(tvb, offset, length_remaining));
4429         } else {
4430             dissect_tcp_payload(tvb, pinfo, offset, tcph->th_seq, nxtseq,
4431                                 tcph->th_sport, tcph->th_dport, tree, tcp_tree, tcpd);
4432         }
4433     }
4434 }
4435
4436 static void
4437 tcp_init(void)
4438 {
4439     tcp_stream_index = 0;
4440     fragment_table_init(&tcp_fragment_table);
4441 }
4442
4443 void
4444 proto_register_tcp(void)
4445 {
4446     static hf_register_info hf[] = {
4447
4448         { &hf_tcp_srcport,
4449         { "Source Port",        "tcp.srcport", FT_UINT16, BASE_DEC, NULL, 0x0,
4450             NULL, HFILL }},
4451
4452         { &hf_tcp_dstport,
4453         { "Destination Port",       "tcp.dstport", FT_UINT16, BASE_DEC, NULL, 0x0,
4454             NULL, HFILL }},
4455
4456         { &hf_tcp_port,
4457         { "Source or Destination Port", "tcp.port", FT_UINT16, BASE_DEC, NULL, 0x0,
4458             NULL, HFILL }},
4459
4460         { &hf_tcp_stream,
4461         { "Stream index",       "tcp.stream", FT_UINT32, BASE_DEC, NULL, 0x0,
4462             NULL, HFILL }},
4463
4464         { &hf_tcp_seq,
4465         { "Sequence number",        "tcp.seq", FT_UINT32, BASE_DEC, NULL, 0x0,
4466             NULL, HFILL }},
4467
4468         { &hf_tcp_nxtseq,
4469         { "Next sequence number",   "tcp.nxtseq", FT_UINT32, BASE_DEC, NULL, 0x0,
4470             NULL, HFILL }},
4471
4472         { &hf_tcp_ack,
4473         { "Acknowledgement number", "tcp.ack", FT_UINT32, BASE_DEC, NULL, 0x0,
4474             NULL, HFILL }},
4475
4476         { &hf_tcp_hdr_len,
4477         { "Header Length",      "tcp.hdr_len", FT_UINT8, BASE_DEC, NULL, 0x0,
4478             NULL, HFILL }},
4479
4480         { &hf_tcp_flags,
4481         { "Flags",          "tcp.flags", FT_UINT8, BASE_HEX, NULL, 0x0,
4482             NULL, HFILL }},
4483
4484         { &hf_tcp_flags_res,
4485         { "Reserved",            "tcp.flags.res", FT_BOOLEAN, 12, TFS(&tfs_set_notset), TH_RES,
4486             "Three reserved bits (must be zero)", HFILL }},
4487
4488         { &hf_tcp_flags_ns,
4489         { "Nonce", "tcp.flags.ns", FT_BOOLEAN, 12, TFS(&tfs_set_notset), TH_NS,
4490             "ECN concealment protection (RFC 3540)", HFILL }},
4491
4492         { &hf_tcp_flags_cwr,
4493         { "Congestion Window Reduced (CWR)",            "tcp.flags.cwr", FT_BOOLEAN, 12, TFS(&tfs_set_notset), TH_CWR,
4494             NULL, HFILL }},
4495
4496         { &hf_tcp_flags_ecn,
4497         { "ECN-Echo",           "tcp.flags.ecn", FT_BOOLEAN, 12, TFS(&tfs_set_notset), TH_ECN,
4498             NULL, HFILL }},
4499
4500         { &hf_tcp_flags_urg,
4501         { "Urgent",         "tcp.flags.urg", FT_BOOLEAN, 12, TFS(&tfs_set_notset), TH_URG,
4502             NULL, HFILL }},
4503
4504         { &hf_tcp_flags_ack,
4505         { "Acknowledgement",        "tcp.flags.ack", FT_BOOLEAN, 12, TFS(&tfs_set_notset), TH_ACK,
4506             NULL, HFILL }},
4507
4508         { &hf_tcp_flags_push,
4509         { "Push",           "tcp.flags.push", FT_BOOLEAN, 12, TFS(&tfs_set_notset), TH_PUSH,
4510             NULL, HFILL }},
4511
4512         { &hf_tcp_flags_reset,
4513         { "Reset",          "tcp.flags.reset", FT_BOOLEAN, 12, TFS(&tfs_set_notset), TH_RST,
4514             NULL, HFILL }},
4515
4516         { &hf_tcp_flags_syn,
4517         { "Syn",            "tcp.flags.syn", FT_BOOLEAN, 12, TFS(&tfs_set_notset), TH_SYN,
4518             NULL, HFILL }},
4519
4520         { &hf_tcp_flags_fin,
4521         { "Fin",            "tcp.flags.fin", FT_BOOLEAN, 12, TFS(&tfs_set_notset), TH_FIN,
4522             NULL, HFILL }},
4523
4524         { &hf_tcp_window_size_value,
4525         { "Window size value",        "tcp.window_size_value", FT_UINT16, BASE_DEC, NULL, 0x0,
4526             "The window size value from the TCP header", HFILL }},
4527
4528         /* 32 bits so we can present some values adjusted to window scaling */
4529         { &hf_tcp_window_size,
4530         { "Calculated window size",        "tcp.window_size", FT_UINT32, BASE_DEC, NULL, 0x0,
4531             "The scaled window size (if scaling has been used)", HFILL }},
4532
4533         { &hf_tcp_window_size_scalefactor,
4534         { "Window size scaling factor", "tcp.window_size_scalefactor", FT_INT32, BASE_DEC, NULL, 0x0,
4535             "The window size scaling factor (-1 when unknown, -2 when no scaling is used)", HFILL }},
4536
4537         { &hf_tcp_checksum,
4538         { "Checksum",           "tcp.checksum", FT_UINT16, BASE_HEX, NULL, 0x0,
4539             "Details at: http://www.wireshark.org/docs/wsug_html_chunked/ChAdvChecksums.html", HFILL }},
4540
4541         { &hf_tcp_checksum_good,
4542         { "Good Checksum",      "tcp.checksum_good", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
4543             "True: checksum matches packet content; False: doesn't match content or not checked", HFILL }},
4544
4545         { &hf_tcp_checksum_bad,
4546         { "Bad Checksum",       "tcp.checksum_bad", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
4547             "True: checksum doesn't match packet content; False: matches content or not checked", HFILL }},
4548
4549         { &hf_tcp_analysis_flags,
4550         { "TCP Analysis Flags",     "tcp.analysis.flags", FT_NONE, BASE_NONE, NULL, 0x0,
4551             "This frame has some of the TCP analysis flags set", HFILL }},
4552
4553         { &hf_tcp_analysis_retransmission,
4554         { "Retransmission",     "tcp.analysis.retransmission", FT_NONE, BASE_NONE, NULL, 0x0,
4555             "This frame is a suspected TCP retransmission", HFILL }},
4556
4557         { &hf_tcp_analysis_fast_retransmission,
4558         { "Fast Retransmission",        "tcp.analysis.fast_retransmission", FT_NONE, BASE_NONE, NULL, 0x0,
4559             "This frame is a suspected TCP fast retransmission", HFILL }},
4560
4561         { &hf_tcp_analysis_out_of_order,
4562         { "Out Of Order",       "tcp.analysis.out_of_order", FT_NONE, BASE_NONE, NULL, 0x0,
4563             "This frame is a suspected Out-Of-Order segment", HFILL }},
4564
4565         { &hf_tcp_analysis_reused_ports,
4566         { "TCP Port numbers reused",        "tcp.analysis.reused_ports", FT_NONE, BASE_NONE, NULL, 0x0,
4567             "A new tcp session has started with previously used port numbers", HFILL }},
4568
4569         { &hf_tcp_analysis_lost_packet,
4570         { "Previous Segment Lost",      "tcp.analysis.lost_segment", FT_NONE, BASE_NONE, NULL, 0x0,
4571             "A segment before this one was lost from the capture", HFILL }},
4572
4573         { &hf_tcp_analysis_ack_lost_packet,
4574         { "ACKed Lost Packet",      "tcp.analysis.ack_lost_segment", FT_NONE, BASE_NONE, NULL, 0x0,
4575             "This frame ACKs a lost segment", HFILL }},
4576
4577         { &hf_tcp_analysis_window_update,
4578         { "Window update",      "tcp.analysis.window_update", FT_NONE, BASE_NONE, NULL, 0x0,
4579             "This frame is a tcp window update", HFILL }},
4580
4581         { &hf_tcp_analysis_window_full,
4582         { "Window full",        "tcp.analysis.window_full", FT_NONE, BASE_NONE, NULL, 0x0,
4583             "This segment has caused the allowed window to become 100% full", HFILL }},
4584
4585         { &hf_tcp_analysis_keep_alive,
4586         { "Keep Alive",     "tcp.analysis.keep_alive", FT_NONE, BASE_NONE, NULL, 0x0,
4587             "This is a keep-alive segment", HFILL }},
4588
4589         { &hf_tcp_analysis_keep_alive_ack,
4590         { "Keep Alive ACK",     "tcp.analysis.keep_alive_ack", FT_NONE, BASE_NONE, NULL, 0x0,
4591             "This is an ACK to a keep-alive segment", HFILL }},
4592
4593         { &hf_tcp_analysis_duplicate_ack,
4594         { "Duplicate ACK",      "tcp.analysis.duplicate_ack", FT_NONE, BASE_NONE, NULL, 0x0,
4595             "This is a duplicate ACK", HFILL }},
4596
4597         { &hf_tcp_analysis_duplicate_ack_num,
4598         { "Duplicate ACK #",        "tcp.analysis.duplicate_ack_num", FT_UINT32, BASE_DEC, NULL, 0x0,
4599             "This is duplicate ACK number #", HFILL }},
4600
4601         { &hf_tcp_analysis_duplicate_ack_frame,
4602         { "Duplicate to the ACK in frame",      "tcp.analysis.duplicate_ack_frame", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
4603             "This is a duplicate to the ACK in frame #", HFILL }},
4604
4605         { &hf_tcp_continuation_to,
4606         { "This is a continuation to the PDU in frame",     "tcp.continuation_to", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
4607             "This is a continuation to the PDU in frame #", HFILL }},
4608
4609         { &hf_tcp_analysis_zero_window_probe,
4610         { "Zero Window Probe",      "tcp.analysis.zero_window_probe", FT_NONE, BASE_NONE, NULL, 0x0,
4611             "This is a zero-window-probe", HFILL }},
4612
4613         { &hf_tcp_analysis_zero_window_probe_ack,
4614         { "Zero Window Probe Ack",      "tcp.analysis.zero_window_probe_ack", FT_NONE, BASE_NONE, NULL, 0x0,
4615             "This is an ACK to a zero-window-probe", HFILL }},
4616
4617         { &hf_tcp_analysis_zero_window,
4618         { "Zero Window",        "tcp.analysis.zero_window", FT_NONE, BASE_NONE, NULL, 0x0,
4619             "This is a zero-window", HFILL }},
4620
4621         { &hf_tcp_len,
4622           { "TCP Segment Len",            "tcp.len", FT_UINT32, BASE_DEC, NULL, 0x0,
4623             NULL, HFILL}},
4624
4625         { &hf_tcp_analysis_acks_frame,
4626           { "This is an ACK to the segment in frame",            "tcp.analysis.acks_frame", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
4627             "Which previous segment is this an ACK for", HFILL}},
4628
4629         { &hf_tcp_analysis_bytes_in_flight,
4630           { "Bytes in flight",            "tcp.analysis.bytes_in_flight", FT_UINT32, BASE_DEC, NULL, 0x0,
4631             "How many bytes are now in flight for this connection", HFILL}},
4632
4633         { &hf_tcp_analysis_ack_rtt,
4634           { "The RTT to ACK the segment was",            "tcp.analysis.ack_rtt", FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
4635             "How long time it took to ACK the segment (RTT)", HFILL}},
4636
4637         { &hf_tcp_analysis_rto,
4638           { "The RTO for this segment was",            "tcp.analysis.rto", FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
4639             "How long transmission was delayed before this segment was retransmitted (RTO)", HFILL}},
4640
4641         { &hf_tcp_analysis_rto_frame,
4642           { "RTO based on delta from frame", "tcp.analysis.rto_frame", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
4643             "This is the frame we measure the RTO from", HFILL }},
4644
4645         { &hf_tcp_urgent_pointer,
4646         { "Urgent pointer",     "tcp.urgent_pointer", FT_UINT16, BASE_DEC, NULL, 0x0,
4647             NULL, HFILL }},
4648
4649         { &hf_tcp_segment_overlap,
4650         { "Segment overlap",    "tcp.segment.overlap", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
4651             "Segment overlaps with other segments", HFILL }},
4652
4653         { &hf_tcp_segment_overlap_conflict,
4654         { "Conflicting data in segment overlap",    "tcp.segment.overlap.conflict", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
4655             "Overlapping segments contained conflicting data", HFILL }},
4656
4657         { &hf_tcp_segment_multiple_tails,
4658         { "Multiple tail segments found",   "tcp.segment.multipletails", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
4659             "Several tails were found when reassembling the pdu", HFILL }},
4660
4661         { &hf_tcp_segment_too_long_fragment,
4662         { "Segment too long",   "tcp.segment.toolongfragment", FT_BOOLEAN, BASE_NONE, NULL, 0x0,
4663             "Segment contained data past end of the pdu", HFILL }},
4664
4665         { &hf_tcp_segment_error,
4666         { "Reassembling error", "tcp.segment.error", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
4667             "Reassembling error due to illegal segments", HFILL }},
4668
4669         { &hf_tcp_segment_count,
4670         { "Segment count", "tcp.segment.count", FT_UINT32, BASE_DEC, NULL, 0x0,
4671             NULL, HFILL }},
4672
4673         { &hf_tcp_segment,
4674         { "TCP Segment", "tcp.segment", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
4675             NULL, HFILL }},
4676
4677         { &hf_tcp_segments,
4678         { "Reassembled TCP Segments", "tcp.segments", FT_NONE, BASE_NONE, NULL, 0x0,
4679             "TCP Segments", HFILL }},
4680
4681         { &hf_tcp_reassembled_in,
4682         { "Reassembled PDU in frame", "tcp.reassembled_in", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
4683             "The PDU that doesn't end in this segment is reassembled in this frame", HFILL }},
4684
4685         { &hf_tcp_reassembled_length,
4686         { "Reassembled TCP length", "tcp.reassembled.length", FT_UINT32, BASE_DEC, NULL, 0x0,
4687             "The total length of the reassembled payload", HFILL }},
4688
4689         { &hf_tcp_option_kind,
4690           { "Kind", "tcp.option_kind", FT_UINT8,
4691             BASE_DEC, VALS(tcp_option_kind_vs), 0x0, "This TCP option's kind", HFILL }},
4692
4693         { &hf_tcp_option_len,
4694           { "Length", "tcp.option_len", FT_UINT8,
4695             BASE_DEC, NULL, 0x0, "Length of this TCP option in bytes (including kind and length fields)", HFILL }},
4696
4697         { &hf_tcp_options,
4698           { "TCP Options", "tcp.options", FT_BYTES,
4699             BASE_NONE, NULL, 0x0, NULL, HFILL }},
4700
4701         { &hf_tcp_option_mss,
4702           { "TCP MSS Option", "tcp.options.mss", FT_NONE,
4703             BASE_NONE, NULL, 0x0, NULL, HFILL }},
4704
4705         { &hf_tcp_option_mss_val,
4706           { "MSS Value", "tcp.options.mss_val", FT_UINT16,
4707             BASE_DEC, NULL, 0x0, NULL, HFILL}},
4708
4709         { &hf_tcp_option_wscale_shift,
4710           { "Shift count", "tcp.options.wscale.shift", FT_UINT8,
4711             BASE_DEC, NULL, 0x0, "Logarithmically encoded power of 2 scale factor", HFILL}},
4712
4713         { &hf_tcp_option_wscale_multiplier,
4714           { "Multiplier", "tcp.options.wscale.multiplier",  FT_UINT8,
4715             BASE_DEC, NULL, 0x0, "Multiply segment window size by this for scaled window size", HFILL}},
4716
4717         { &hf_tcp_option_exp,
4718           { "TCP Option - Experimental", "tcp.options.experimental", FT_BYTES,
4719             BASE_NONE, NULL, 0x0, NULL, HFILL}},
4720
4721         { &hf_tcp_option_exp_data,
4722           { "Data", "tcp.options.experimental.data", FT_BYTES,
4723             BASE_NONE, NULL, 0x0, NULL, HFILL}},
4724
4725         { &hf_tcp_option_sack_perm,
4726           { "TCP SACK Permitted Option", "tcp.options.sack_perm",
4727             FT_BOOLEAN,
4728             BASE_NONE, NULL, 0x0, NULL, HFILL}},
4729
4730         { &hf_tcp_option_sack,
4731           { "TCP SACK Option", "tcp.options.sack", FT_BOOLEAN,
4732             BASE_NONE, NULL, 0x0, NULL, HFILL}},
4733
4734         { &hf_tcp_option_sack_sle,
4735           {"TCP SACK Left Edge", "tcp.options.sack_le", FT_UINT32,
4736            BASE_DEC, NULL, 0x0, NULL, HFILL}},
4737
4738         { &hf_tcp_option_sack_sre,
4739           {"TCP SACK Right Edge", "tcp.options.sack_re", FT_UINT32,
4740            BASE_DEC, NULL, 0x0, NULL, HFILL}},
4741
4742         { &hf_tcp_option_echo,
4743           { "TCP Echo Option", "tcp.options.echo", FT_BOOLEAN,
4744             BASE_NONE, NULL, 0x0, "TCP Sack Echo", HFILL}},
4745
4746         { &hf_tcp_option_echo_reply,
4747           { "TCP Echo Reply Option", "tcp.options.echo_reply",
4748             FT_BOOLEAN,
4749             BASE_NONE, NULL, 0x0, NULL, HFILL}},
4750
4751         { &hf_tcp_option_timestamp_tsval,
4752           { "Timestamp value", "tcp.options.timestamp.tsval", FT_UINT32,
4753             BASE_DEC, NULL, 0x0, "Value of sending machine's timestamp clock", HFILL}},
4754
4755         { &hf_tcp_option_timestamp_tsecr,
4756           { "Timestamp echo reply", "tcp.options.timestamp.tsecr", FT_UINT32,
4757             BASE_DEC, NULL, 0x0, "Echoed timestamp from remote machine", HFILL}},
4758
4759         { &hf_tcp_option_cc,
4760           { "TCP CC Option", "tcp.options.cc", FT_BOOLEAN, BASE_NONE,
4761             NULL, 0x0, NULL, HFILL}},
4762
4763         { &hf_tcp_option_ccnew,
4764           { "TCP CC New Option", "tcp.options.ccnew", FT_BOOLEAN,
4765             BASE_NONE, NULL, 0x0, NULL, HFILL}},
4766
4767         { &hf_tcp_option_ccecho,
4768           { "TCP CC Echo Option", "tcp.options.ccecho", FT_BOOLEAN,
4769             BASE_NONE, NULL, 0x0, NULL, HFILL}},
4770
4771         { &hf_tcp_option_md5,
4772           { "TCP MD5 Option", "tcp.options.md5", FT_BOOLEAN, BASE_NONE,
4773             NULL, 0x0, NULL, HFILL}},
4774
4775         { &hf_tcp_option_qs,
4776           { "TCP QS Option", "tcp.options.qs", FT_BOOLEAN, BASE_NONE,
4777             NULL, 0x0, NULL, HFILL}},
4778
4779         { &hf_tcp_option_scps,
4780           { "TCP SCPS Capabilities Option", "tcp.options.scps",
4781             FT_BOOLEAN, BASE_NONE, NULL,  0x0,
4782             NULL, HFILL}},
4783
4784         { &hf_tcp_option_scps_vector,
4785           { "TCP SCPS Capabilities Vector", "tcp.options.scps.vector",
4786             FT_UINT8, BASE_DEC, NULL, 0x0,
4787             NULL, HFILL}},
4788
4789         { &hf_tcp_option_scps_binding,
4790           { "Binding Space (Community) ID",
4791             "tcp.options.scps.binding.id",
4792             FT_UINT8, BASE_DEC, NULL, 0x0,
4793             "TCP SCPS Extended Binding Space (Community) ID", HFILL}},
4794
4795         { &hf_tcp_option_scps_binding_len,
4796           { "Extended Capability Length",
4797             "tcp.options.scps.binding.len",
4798             FT_UINT8, BASE_DEC, NULL, 0x0,
4799             "TCP SCPS Extended Capability Length in bytes", HFILL}},
4800
4801         { &hf_tcp_option_snack,
4802           { "TCP Selective Negative Acknowledgement Option",
4803             "tcp.options.snack",
4804             FT_BOOLEAN, BASE_NONE, NULL,  0x0,
4805             NULL, HFILL}},
4806
4807         { &hf_tcp_option_snack_offset,
4808           { "TCP SNACK Offset", "tcp.options.snack.offset",
4809             FT_UINT16, BASE_DEC, NULL, 0x0,
4810             NULL, HFILL}},
4811
4812         { &hf_tcp_option_snack_size,
4813           { "TCP SNACK Size", "tcp.options.snack.size",
4814             FT_UINT16, BASE_DEC, NULL, 0x0,
4815             NULL, HFILL}},
4816
4817         { &hf_tcp_option_snack_le,
4818           { "TCP SNACK Left Edge", "tcp.options.snack.le",
4819             FT_UINT16, BASE_DEC, NULL, 0x0,
4820             NULL, HFILL}},
4821
4822         { &hf_tcp_option_snack_re,
4823           { "TCP SNACK Right Edge", "tcp.options.snack.re",
4824             FT_UINT16, BASE_DEC, NULL, 0x0,
4825             NULL, HFILL}},
4826
4827         { &hf_tcp_scpsoption_flags_bets,
4828           { "Partial Reliability Capable (BETS)",
4829             "tcp.options.scpsflags.bets", FT_BOOLEAN, 8,
4830             TFS(&tfs_set_notset), 0x80, NULL, HFILL }},
4831
4832         { &hf_tcp_scpsoption_flags_snack1,
4833           { "Short Form SNACK Capable (SNACK1)",
4834             "tcp.options.scpsflags.snack1", FT_BOOLEAN, 8,
4835             TFS(&tfs_set_notset), 0x40, NULL, HFILL }},
4836
4837         { &hf_tcp_scpsoption_flags_snack2,
4838           { "Long Form SNACK Capable (SNACK2)",
4839             "tcp.options.scpsflags.snack2", FT_BOOLEAN, 8,
4840             TFS(&tfs_set_notset), 0x20, NULL, HFILL }},
4841
4842         { &hf_tcp_scpsoption_flags_compress,
4843           { "Lossless Header Compression (COMP)",
4844             "tcp.options.scpsflags.compress", FT_BOOLEAN, 8,
4845             TFS(&tfs_set_notset), 0x10, NULL, HFILL }},
4846
4847         { &hf_tcp_scpsoption_flags_nlts,
4848           { "Network Layer Timestamp (NLTS)",
4849             "tcp.options.scpsflags.nlts", FT_BOOLEAN, 8,
4850             TFS(&tfs_set_notset), 0x8, NULL, HFILL }},
4851
4852         { &hf_tcp_scpsoption_flags_resv3,
4853           { "Reserved Bit 3",
4854             "tcp.options.scpsflags.reserved3", FT_BOOLEAN, 8,
4855             TFS(&tfs_set_notset), 0x4, NULL, HFILL }},
4856
4857         { &hf_tcp_scpsoption_flags_resv2,
4858           { "Reserved Bit 2",
4859             "tcp.options.scpsflags.reserved2", FT_BOOLEAN, 8,
4860             TFS(&tfs_set_notset), 0x2, NULL, HFILL }},
4861
4862         { &hf_tcp_scpsoption_flags_resv1,
4863           { "Reserved Bit 1",
4864             "tcp.options.scpsflags.reserved1", FT_BOOLEAN, 8,
4865             TFS(&tfs_set_notset), 0x1, NULL, HFILL }},
4866
4867         { &hf_tcp_scpsoption_connection_id,
4868           { "Connection ID",
4869             "tcp.options.scps.binding",
4870             FT_UINT8, BASE_DEC, NULL, 0x0,
4871             "TCP SCPS Connection ID", HFILL}},
4872
4873         { &hf_tcp_option_mood,
4874           { "TCP Mood Option", "tcp.options.mood", FT_BOOLEAN,
4875             BASE_NONE, NULL, 0x0, NULL, HFILL }},
4876
4877         { &hf_tcp_option_mood_val,
4878           { "TCP Mood Option Value", "tcp.options.mood_val", FT_STRING,
4879             BASE_NONE, NULL, 0x0, NULL, HFILL}},
4880
4881         { &hf_tcp_option_user_to,
4882           { "TCP User Timeout", "tcp.options.user_to", FT_BOOLEAN,
4883             BASE_NONE, NULL, 0x0, NULL, HFILL }},
4884
4885         { &hf_tcp_option_user_to_granularity,
4886           { "Granularity", "tcp.options.user_to_granularity", FT_BOOLEAN,
4887             16, TFS(&tcp_option_user_to_granularity), 0x8000, "TCP User Timeout Granularity", HFILL}},
4888
4889         { &hf_tcp_option_user_to_val,
4890           { "User Timeout", "tcp.options.user_to_val", FT_UINT16,
4891             BASE_DEC, NULL, 0x7FFF, "TCP User Timeout Value", HFILL}},
4892
4893         { &hf_tcp_option_rvbd_probe,
4894           { "Riverbed Probe", "tcp.options.rvbd.probe", FT_BOOLEAN,
4895             BASE_NONE, NULL, 0x0, "RVBD TCP Probe Option", HFILL }},
4896
4897         { &hf_tcp_option_rvbd_probe_type1,
4898           { "Type", "tcp.options.rvbd.probe.type1",
4899             FT_UINT8, BASE_DEC, NULL, 0xF0, NULL, HFILL }},
4900
4901         { &hf_tcp_option_rvbd_probe_type2,
4902           { "Type", "tcp.options.rvbd.probe.type2",
4903             FT_UINT8, BASE_DEC, NULL, 0xFE, NULL, HFILL }},
4904
4905         { &hf_tcp_option_rvbd_probe_version1,
4906           { "Version", "tcp.options.rvbd.probe.version",
4907             FT_UINT8, BASE_DEC, NULL, 0x0F, NULL, HFILL }},
4908
4909         { &hf_tcp_option_rvbd_probe_version2,
4910           { "Version", "tcp.options.rvbd.probe.version_raw",
4911             FT_UINT8, BASE_DEC, NULL, 0x01, "Version 2 Raw Value", HFILL }},
4912
4913         { &hf_tcp_option_rvbd_probe_optlen,
4914           { "Length", "tcp.options.rvbd.probe.len",
4915             FT_UINT8, BASE_DEC, NULL, 0x0, NULL, HFILL }},
4916
4917         { &hf_tcp_option_rvbd_probe_prober,
4918           { "CSH IP", "tcp.options.rvbd.probe.prober",
4919             FT_IPv4, BASE_NONE, NULL, 0x0, NULL, HFILL }},
4920
4921         { &hf_tcp_option_rvbd_probe_proxy,
4922           { "SSH IP", "tcp.options.rvbd.probe.proxy.ip",
4923             FT_IPv4, BASE_NONE, NULL, 0x0, NULL, HFILL }},
4924
4925         { &hf_tcp_option_rvbd_probe_proxy_port,
4926           { "SSH Port", "tcp.options.rvbd.probe.proxy.port",
4927             FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
4928
4929         { &hf_tcp_option_rvbd_probe_appli_ver,
4930           { "Application Version", "tcp.options.rvbd.probe.appli_ver",
4931             FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
4932
4933         { &hf_tcp_option_rvbd_probe_client,
4934           { "Client IP", "tcp.options.rvbd.probe.client.ip",
4935             FT_IPv4, BASE_NONE, NULL, 0x0, NULL, HFILL }},
4936
4937         { &hf_tcp_option_rvbd_probe_storeid,
4938           { "CFE Store ID", "tcp.options.rvbd.probe.storeid",
4939             FT_UINT32, BASE_DEC, NULL, 0x0, NULL, HFILL }},
4940
4941         { &hf_tcp_option_rvbd_probe_flags,
4942           { "Probe Flags", "tcp.options.rvbd.probe.flags",
4943             FT_UINT8, BASE_HEX, NULL, 0x0, NULL, HFILL }},
4944
4945         { &hf_tcp_option_rvbd_probe_flag_not_cfe,
4946           { "Not CFE", "tcp.options.rvbd.probe.flags.notcfe",
4947             FT_BOOLEAN, 8, TFS(&tfs_set_notset), RVBD_FLAGS_PROBE_NCFE,
4948             NULL, HFILL }},
4949
4950         { &hf_tcp_option_rvbd_probe_flag_last_notify,
4951           { "Last Notify", "tcp.options.rvbd.probe.flags.last",
4952             FT_BOOLEAN, 8, TFS(&tfs_set_notset), RVBD_FLAGS_PROBE_LAST,
4953             NULL, HFILL }},
4954
4955         { &hf_tcp_option_rvbd_probe_flag_probe_cache,
4956           { "Disable Probe Cache on CSH", "tcp.options.rvbd.probe.flags.probe",
4957             FT_BOOLEAN, 8, TFS(&tfs_set_notset), RVBD_FLAGS_PROBE,
4958             NULL, HFILL }},
4959
4960         { &hf_tcp_option_rvbd_probe_flag_sslcert,
4961           { "SSL Enabled", "tcp.options.rvbd.probe.flags.ssl",
4962             FT_BOOLEAN, 8, TFS(&tfs_set_notset), RVBD_FLAGS_PROBE_SSLCERT,
4963             NULL, HFILL }},
4964
4965         { &hf_tcp_option_rvbd_probe_flag_server_connected,
4966           { "SSH outer to server established", "tcp.options.rvbd.probe.flags.server",
4967             FT_BOOLEAN, 8, TFS(&tfs_set_notset), RVBD_FLAGS_PROBE_SERVER,
4968             NULL, HFILL }},
4969
4970         { &hf_tcp_option_rvbd_trpy,
4971           { "Riverbed Transparency", "tcp.options.rvbd.trpy",
4972             FT_BOOLEAN, BASE_NONE, NULL, 0x0,
4973             "RVBD TCP Transparency Option", HFILL }},
4974
4975         { &hf_tcp_option_rvbd_trpy_flags,
4976           { "Transparency Options", "tcp.options.rvbd.trpy.flags",
4977             FT_UINT16, BASE_HEX, NULL, 0x0, NULL, HFILL }},
4978
4979         { &hf_tcp_option_rvbd_trpy_flag_fw_rst_probe,
4980           { "Enable FW traversal feature", "tcp.options.rvbd.trpy.flags.fw_rst_probe",
4981             FT_BOOLEAN, 16, TFS(&tfs_set_notset),
4982             RVBD_FLAGS_TRPY_FW_RST_PROBE,
4983             "Reset state created by probe on the nexthop firewall",
4984             HFILL }},
4985
4986         { &hf_tcp_option_rvbd_trpy_flag_fw_rst_inner,
4987           { "Enable Inner FW feature on All FWs", "tcp.options.rvbd.trpy.flags.fw_rst_inner",
4988             FT_BOOLEAN, 16, TFS(&tfs_set_notset),
4989             RVBD_FLAGS_TRPY_FW_RST_INNER,
4990             "Reset state created by transparent inner on all firewalls"
4991             " before passing connection through",
4992             HFILL }},
4993
4994         { &hf_tcp_option_rvbd_trpy_flag_fw_rst,
4995           { "Enable Transparency FW feature on All FWs", "tcp.options.rvbd.trpy.flags.fw_rst",
4996             FT_BOOLEAN, 16, TFS(&tfs_set_notset),
4997             RVBD_FLAGS_TRPY_FW_RST,
4998             "Reset state created by probe on all firewalls before "
4999             "establishing transparent inner connection", HFILL }},
5000
5001         { &hf_tcp_option_rvbd_trpy_flag_chksum,
5002           { "Reserved", "tcp.options.rvbd.trpy.flags.chksum",
5003             FT_BOOLEAN, 16, TFS(&tfs_set_notset),
5004             RVBD_FLAGS_TRPY_CHKSUM, NULL, HFILL }},
5005
5006         { &hf_tcp_option_rvbd_trpy_flag_oob,
5007           { "Out of band connection", "tcp.options.rvbd.trpy.flags.oob",
5008             FT_BOOLEAN, 16, TFS(&tfs_set_notset),
5009             RVBD_FLAGS_TRPY_OOB, NULL, HFILL }},
5010
5011         { &hf_tcp_option_rvbd_trpy_flag_mode,
5012           { "Transparency Mode", "tcp.options.rvbd.trpy.flags.mode",
5013             FT_BOOLEAN, 16, TFS(&trpy_mode_str),
5014             RVBD_FLAGS_TRPY_MODE, NULL, HFILL }},
5015
5016         { &hf_tcp_option_rvbd_trpy_src,
5017           { "Src SH IP Addr", "tcp.options.rvbd.trpy.src.ip",
5018             FT_IPv4, BASE_NONE, NULL, 0x0, NULL, HFILL }},
5019
5020         { &hf_tcp_option_rvbd_trpy_dst,
5021           { "Dst SH IP Addr", "tcp.options.rvbd.trpy.dst.ip",
5022             FT_IPv4, BASE_NONE, NULL, 0x0, NULL, HFILL }},
5023
5024         { &hf_tcp_option_rvbd_trpy_src_port,
5025           { "Src SH Inner Port", "tcp.options.rvbd.trpy.src.port",
5026             FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
5027
5028         { &hf_tcp_option_rvbd_trpy_dst_port,
5029           { "Dst SH Inner Port", "tcp.options.rvbd.trpy.dst.port",
5030             FT_UINT16, BASE_DEC, NULL, 0x0, NULL, HFILL }},
5031
5032         { &hf_tcp_option_rvbd_trpy_client_port,
5033           { "Out of band connection Client Port", "tcp.options.rvbd.trpy.client.port",
5034             FT_UINT16, BASE_DEC, NULL , 0x0, NULL, HFILL }},
5035
5036         { &hf_tcp_pdu_time,
5037           { "Time until the last segment of this PDU", "tcp.pdu.time", FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
5038             "How long time has passed until the last frame of this PDU", HFILL}},
5039
5040         { &hf_tcp_pdu_size,
5041           { "PDU Size", "tcp.pdu.size", FT_UINT32, BASE_DEC, NULL, 0x0,
5042             "The size of this PDU", HFILL}},
5043
5044         { &hf_tcp_pdu_last_frame,
5045           { "Last frame of this PDU", "tcp.pdu.last_frame", FT_FRAMENUM, BASE_NONE, NULL, 0x0,
5046             "This is the last frame of the PDU starting in this segment", HFILL }},
5047
5048         { &hf_tcp_ts_relative,
5049           { "Time since first frame in this TCP stream", "tcp.time_relative", FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
5050             "Time relative to first frame in this TCP stream", HFILL}},
5051
5052         { &hf_tcp_ts_delta,
5053           { "Time since previous frame in this TCP stream", "tcp.time_delta", FT_RELATIVE_TIME, BASE_NONE, NULL, 0x0,
5054             "Time delta from previous frame in this TCP stream", HFILL}},
5055
5056         { &hf_tcp_proc_src_uid,
5057           { "Source process user ID", "tcp.proc.srcuid", FT_UINT32, BASE_DEC, NULL, 0x0,
5058             NULL, HFILL}},
5059
5060         { &hf_tcp_proc_src_pid,
5061           { "Source process ID", "tcp.proc.srcpid", FT_UINT32, BASE_DEC, NULL, 0x0,
5062             NULL, HFILL}},
5063
5064         { &hf_tcp_proc_src_uname,
5065           { "Source process user name", "tcp.proc.srcuname", FT_STRING, BASE_NONE, NULL, 0x0,
5066             NULL, HFILL}},
5067
5068         { &hf_tcp_proc_src_cmd,
5069           { "Source process name", "tcp.proc.srccmd", FT_STRING, BASE_NONE, NULL, 0x0,
5070             "Source process command name", HFILL}},
5071
5072         { &hf_tcp_proc_dst_uid,
5073           { "Destination process user ID", "tcp.proc.dstuid", FT_UINT32, BASE_DEC, NULL, 0x0,
5074             NULL, HFILL}},
5075
5076         { &hf_tcp_proc_dst_pid,
5077           { "Destination process ID", "tcp.proc.dstpid", FT_UINT32, BASE_DEC, NULL, 0x0,
5078             NULL, HFILL}},
5079
5080         { &hf_tcp_proc_dst_uname,
5081           { "Destination process user name", "tcp.proc.dstuname", FT_STRING, BASE_NONE, NULL, 0x0,
5082             NULL, HFILL}},
5083
5084         { &hf_tcp_proc_dst_cmd,
5085           { "Destination process name", "tcp.proc.dstcmd", FT_STRING, BASE_NONE, NULL, 0x0,
5086             "Destination process command name", HFILL}},
5087
5088         { &hf_tcp_data,
5089           { "TCP segment data", "tcp.data", FT_BYTES, BASE_NONE, NULL, 0x0,
5090             NULL, HFILL}}
5091     };
5092
5093     static gint *ett[] = {
5094         &ett_tcp,
5095         &ett_tcp_flags,
5096         &ett_tcp_options,
5097         &ett_tcp_option_timestamp,
5098         &ett_tcp_option_wscale,
5099         &ett_tcp_option_sack,
5100         &ett_tcp_option_scps,
5101         &ett_tcp_option_scps_extended,
5102         &ett_tcp_option_user_to,
5103         &ett_tcp_option_exp,
5104         &ett_tcp_option_sack_perm,
5105         &ett_tcp_option_mss,
5106         &ett_tcp_opt_rvbd_probe,
5107         &ett_tcp_opt_rvbd_probe_flags,
5108         &ett_tcp_opt_rvbd_trpy,
5109         &ett_tcp_opt_rvbd_trpy_flags,
5110         &ett_tcp_analysis_faults,
5111         &ett_tcp_analysis,
5112         &ett_tcp_timestamps,
5113         &ett_tcp_segments,
5114         &ett_tcp_segment,
5115         &ett_tcp_checksum,
5116         &ett_tcp_process_info
5117     };
5118     module_t *tcp_module;
5119
5120     proto_tcp = proto_register_protocol("Transmission Control Protocol",
5121         "TCP", "tcp");
5122     register_dissector("tcp", dissect_tcp, proto_tcp);
5123     proto_register_field_array(proto_tcp, hf, array_length(hf));
5124     proto_register_subtree_array(ett, array_length(ett));
5125
5126     /* subdissector code */
5127     subdissector_table = register_dissector_table("tcp.port",
5128         "TCP port", FT_UINT16, BASE_DEC);
5129     register_heur_dissector_list("tcp", &heur_subdissector_list);
5130
5131     /* Register configuration preferences */
5132     tcp_module = prefs_register_protocol(proto_tcp, NULL);
5133     prefs_register_bool_preference(tcp_module, "summary_in_tree",
5134         "Show TCP summary in protocol tree",
5135         "Whether the TCP summary line should be shown in the protocol tree",
5136         &tcp_summary_in_tree);
5137     prefs_register_bool_preference(tcp_module, "check_checksum",
5138         "Validate the TCP checksum if possible",
5139         "Whether to validate the TCP checksum",
5140         &tcp_check_checksum);
5141     prefs_register_bool_preference(tcp_module, "desegment_tcp_streams",
5142         "Allow subdissector to reassemble TCP streams",
5143         "Whether subdissector can request TCP streams to be reassembled",
5144         &tcp_desegment);
5145     prefs_register_bool_preference(tcp_module, "analyze_sequence_numbers",
5146         "Analyze TCP sequence numbers",
5147         "Make the TCP dissector analyze TCP sequence numbers to find and flag segment retransmissions, missing segments and RTT",
5148         &tcp_analyze_seq);
5149     prefs_register_bool_preference(tcp_module, "relative_sequence_numbers",
5150         "Relative sequence numbers",
5151         "Make the TCP dissector use relative sequence numbers instead of absolute ones. "
5152         "To use this option you must also enable \"Analyze TCP sequence numbers\". ",
5153         &tcp_relative_seq);
5154
5155     /* We now have tcp.window_size_value and tcp.window_size, so no need
5156      * anymore for the preference window_scaling
5157      */
5158     prefs_register_obsolete_preference(tcp_module, "window_scaling");
5159
5160     prefs_register_bool_preference(tcp_module, "track_bytes_in_flight",
5161         "Track number of bytes in flight",
5162         "Make the TCP dissector track the number on un-ACKed bytes of data are in flight per packet. "
5163         "To use this option you must also enable \"Analyze TCP sequence numbers\". "
5164         "This takes a lot of memory but allows you to track how much data are in flight at a time and graphing it in io-graphs",
5165         &tcp_track_bytes_in_flight);
5166     prefs_register_bool_preference(tcp_module, "calculate_timestamps",
5167         "Calculate conversation timestamps",
5168         "Calculate timestamps relative to the first frame and the previous frame in the tcp conversation",
5169         &tcp_calculate_ts);
5170     prefs_register_bool_preference(tcp_module, "try_heuristic_first",
5171         "Try heuristic sub-dissectors first",
5172         "Try to decode a packet using an heuristic sub-dissector before using a sub-dissector registered to a specific port",
5173         &try_heuristic_first);
5174     prefs_register_bool_preference(tcp_module, "ignore_tcp_timestamps",
5175         "Ignore TCP Timestamps in summary",
5176         "Do not place the TCP Timestamps in the summary line",
5177         &tcp_ignore_timestamps);
5178
5179     register_init_routine(tcp_init);
5180 }
5181
5182 void
5183 proto_reg_handoff_tcp(void)
5184 {
5185     dissector_handle_t tcp_handle;
5186
5187     tcp_handle = find_dissector("tcp");
5188     dissector_add_uint("ip.proto", IP_PROTO_TCP, tcp_handle);
5189     data_handle = find_dissector("data");
5190     tcp_tap = register_tap("tcp");
5191 }
5192
5193 /*
5194  * Editor modelines
5195  *
5196  * Local Variables:
5197  * c-basic-offset: 4
5198  * tab-width: 8
5199  * indent-tabs-mode: nil
5200  * End:
5201  *
5202  * ex: set shiftwidth=4 tabstop=8 expandtab:
5203  * :indentSize=4:tabSize=8:noTabs=true:
5204  */