3 * Copyright 1998 Mike Hall <mlh@io.com>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
35 #include <epan/packet.h>
36 #include <epan/to_str.h>
37 #include <epan/emem.h>
38 #include <epan/dissectors/packet-tcp.h>
40 #include <epan/conversation.h>
42 #define MAX_IPADDR_LEN 16
44 typedef struct _tcp_frag {
49 struct _tcp_frag *next;
53 FILE* data_out_file = NULL;
55 gboolean empty_tcp_stream;
56 gboolean incomplete_tcp_stream;
58 static guint32 tcp_stream_to_follow = 0;
59 static guint8 ip_address[2][MAX_IPADDR_LEN];
61 static guint bytes_written[2];
62 static gboolean is_ipv6 = FALSE;
64 static int check_fragments( int, tcp_stream_chunk *, guint32 );
65 static void write_packet_data( int, tcp_stream_chunk *, const char * );
68 follow_stats(follow_stats_t* stats)
72 for (i = 0; i < 2 ; i++) {
73 memcpy(stats->ip_address[i], ip_address[i], MAX_IPADDR_LEN);
74 stats->port[i] = port[i];
75 stats->bytes_written[i] = bytes_written[i];
76 stats->is_ipv6 = is_ipv6;
80 /* This will build a display filter text that will only
81 pass the packets related to the stream. There is a
82 chance that two streams could intersect, but not a
85 build_follow_conv_filter( packet_info *pi ) {
88 conversation_t *conv=NULL;
89 struct tcp_analysis *tcpd;
90 wmem_list_frame_t* protos;
92 const char* proto_name;
93 gboolean is_tcp = FALSE, is_udp = FALSE;
95 protos = wmem_list_head(pi->layers);
97 /* walk the list of a available protocols in the packet to
98 figure out if any of them affect context sensitivity */
99 while (protos != NULL)
101 proto_id = GPOINTER_TO_INT(wmem_list_frame_data(protos));
102 proto_name = proto_get_protocol_filter_name(proto_id);
104 if (!strcmp(proto_name, "tcp")) {
106 } else if (!strcmp(proto_name, "udp")) {
110 protos = wmem_list_frame_next(protos);
113 if( ((pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4) ||
114 (pi->net_src.type == AT_IPv6 && pi->net_dst.type == AT_IPv6))
115 && is_tcp && (conv=find_conversation(pi->fd->num, &pi->src, &pi->dst, pi->ptype,
116 pi->srcport, pi->destport, 0)) != NULL ) {
118 tcpd=get_tcp_conversation_data(conv, pi);
120 buf = g_strdup_printf("tcp.stream eq %d", tcpd->stream);
121 tcp_stream_to_follow = tcpd->stream;
122 if (pi->net_src.type == AT_IPv4) {
133 else if( pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4
136 buf = g_strdup_printf(
137 "(ip.addr eq %s and ip.addr eq %s) and (udp.port eq %d and udp.port eq %d)",
138 ip_to_str((const guint8 *)pi->net_src.data),
139 ip_to_str((const guint8 *)pi->net_dst.data),
140 pi->srcport, pi->destport );
144 else if( pi->net_src.type == AT_IPv6 && pi->net_dst.type == AT_IPv6
147 buf = g_strdup_printf(
148 "(ipv6.addr eq %s and ipv6.addr eq %s) and (udp.port eq %d and udp.port eq %d)",
149 ip6_to_str((const struct e_in6_addr *)pi->net_src.data),
150 ip6_to_str((const struct e_in6_addr *)pi->net_dst.data),
151 pi->srcport, pi->destport );
158 memcpy(ip_address[0], pi->net_src.data, len);
159 memcpy(ip_address[1], pi->net_dst.data, len);
160 port[0] = pi->srcport;
161 port[1] = pi->destport;
165 static gboolean find_tcp_addr;
166 static address tcp_addr[2];
167 static gboolean find_tcp_index;
170 build_follow_index_filter(void) {
173 find_tcp_addr = TRUE;
174 buf = g_strdup_printf("tcp.stream eq %d", tcp_stream_to_follow);
178 /* select a tcp stream to follow via it's address/port pairs */
180 follow_tcp_addr(const address *addr0, guint port0,
181 const address *addr1, guint port1)
183 if (addr0 == NULL || addr1 == NULL || addr0->type != addr1->type ||
184 port0 > G_MAXUINT16 || port1 > G_MAXUINT16 ) {
188 if (find_tcp_index || find_tcp_addr) {
192 switch (addr0->type) {
197 is_ipv6 = addr0->type == AT_IPv6;
201 find_tcp_index = TRUE;
203 memcpy(ip_address[0], addr0->data, addr0->len);
204 SET_ADDRESS(&tcp_addr[0], addr0->type, addr0->len, ip_address[0]);
207 memcpy(ip_address[1], addr1->data, addr1->len);
208 SET_ADDRESS(&tcp_addr[1], addr1->type, addr1->len, ip_address[1]);
214 /* select a tcp stream to follow via its index */
216 follow_tcp_index(guint32 indx)
218 if (find_tcp_index || find_tcp_addr) {
222 if (indx > get_tcp_stream_count()) {
226 find_tcp_addr = TRUE;
227 tcp_stream_to_follow = indx;
228 memset(ip_address, 0, sizeof ip_address);
229 port[0] = port[1] = 0;
235 get_follow_tcp_index(void) {
236 return tcp_stream_to_follow;
239 /* here we are going to try and reconstruct the data portion of a TCP
240 session. We will try and handle duplicates, TCP fragments, and out
241 of order packets in a smart way. */
243 static tcp_frag *frags[2] = { 0, 0 };
244 static guint32 seq[2];
245 static guint8 src_addr[2][MAX_IPADDR_LEN];
246 static guint src_port[2] = { 0, 0 };
249 reassemble_tcp( guint32 tcp_stream, guint32 sequence, guint32 acknowledgement,
250 guint32 length, const char* data, guint32 data_length,
251 int synflag, address *net_src, address *net_dst,
252 guint srcport, guint dstport, guint32 packet_num) {
253 guint8 srcx[MAX_IPADDR_LEN], dstx[MAX_IPADDR_LEN];
254 int src_index, j, first = 0, len;
261 /* First, check if this packet should be processed. */
262 if (find_tcp_index) {
263 if ((port[0] == srcport && port[1] == dstport &&
264 ADDRESSES_EQUAL(&tcp_addr[0], net_src) &&
265 ADDRESSES_EQUAL(&tcp_addr[1], net_dst))
267 (port[1] == srcport && port[0] == dstport &&
268 ADDRESSES_EQUAL(&tcp_addr[1], net_src) &&
269 ADDRESSES_EQUAL(&tcp_addr[0], net_dst))) {
270 find_tcp_index = FALSE;
271 tcp_stream_to_follow = tcp_stream;
277 else if ( tcp_stream != tcp_stream_to_follow )
280 if ((net_src->type != AT_IPv4 && net_src->type != AT_IPv6) ||
281 (net_dst->type != AT_IPv4 && net_dst->type != AT_IPv6))
284 if (net_src->type == AT_IPv4)
289 memcpy(srcx, net_src->data, len);
290 memcpy(dstx, net_dst->data, len);
292 /* follow_tcp_index() needs to learn address/port pairs */
294 find_tcp_addr = FALSE;
295 memcpy(ip_address[0], net_src->data, net_src->len);
297 memcpy(ip_address[1], net_dst->data, net_dst->len);
299 if (net_src->type == AT_IPv6 && net_dst->type == AT_IPv6) {
306 /* Check to see if we have seen this source IP and port before.
307 (Yes, we have to check both source IP and port; the connection
308 might be between two different ports on the same machine.) */
309 for( j=0; j<2; j++ ) {
310 if (memcmp(src_addr[j], srcx, len) == 0 && src_port[j] == srcport ) {
314 /* we didn't find it if src_index == -1 */
315 if( src_index < 0 ) {
316 /* assign it to a src_index and get going */
317 for( j=0; j<2; j++ ) {
318 if( src_port[j] == 0 ) {
319 memcpy(src_addr[j], srcx, len);
320 src_port[j] = srcport;
327 if( src_index < 0 ) {
328 fprintf( stderr, "ERROR in reassemble_tcp: Too many addresses!\n");
332 if( data_length < length ) {
333 incomplete_tcp_stream = TRUE;
336 /* Before adding data for this flow to the data_out_file, check whether
337 * this frame acks fragments that were already seen. This happens when
338 * frames are not in the capture file, but were actually seen by the
339 * receiving host (Fixes bug 592).
341 if( frags[1-src_index] ) {
342 memcpy(sc.src_addr, dstx, len);
343 sc.src_port = dstport;
344 sc.dlen = 0; /* Will be filled in in check_fragments */
345 while ( check_fragments( 1-src_index, &sc, acknowledgement ) )
349 /* Initialize our stream chunk. This data gets written to disk. */
350 memcpy(sc.src_addr, srcx, len);
351 sc.src_port = srcport;
352 sc.dlen = data_length;
353 sc.packet_num = packet_num;
355 /* now that we have filed away the srcs, lets get the sequence number stuff
358 /* this is the first time we have seen this src's sequence number */
359 seq[src_index] = sequence + length;
363 /* write out the packet data */
364 write_packet_data( src_index, &sc, data );
367 /* if we are here, we have already seen this src, let's
368 try and figure out if this packet is in the right place */
369 if( sequence < seq[src_index] ) {
370 /* this sequence number seems dated, but
371 check the end to make sure it has no more
372 info than we have already seen */
373 newseq = sequence + length;
374 if( newseq > seq[src_index] ) {
377 /* this one has more than we have seen. let's get the
378 payload that we have not seen. */
380 new_len = seq[src_index] - sequence;
382 if ( data_length <= new_len ) {
385 incomplete_tcp_stream = TRUE;
388 data_length -= new_len;
390 sc.dlen = data_length;
391 sequence = seq[src_index];
392 length = newseq - seq[src_index];
394 /* this will now appear to be right on time :) */
397 if ( sequence == seq[src_index] ) {
399 seq[src_index] += length;
400 if( synflag ) seq[src_index]++;
402 write_packet_data( src_index, &sc, data );
404 /* done with the packet, see if it caused a fragment to fit */
405 while( check_fragments( src_index, &sc, 0 ) )
409 /* out of order packet */
410 if(data_length > 0 && GT_SEQ(sequence, seq[src_index]) ) {
411 tmp_frag = (tcp_frag *)g_malloc( sizeof( tcp_frag ) );
412 tmp_frag->data = (gchar *)g_malloc( data_length );
413 tmp_frag->seq = sequence;
414 tmp_frag->len = length;
415 tmp_frag->data_len = data_length;
416 memcpy( tmp_frag->data, data, data_length );
417 if( frags[src_index] ) {
418 tmp_frag->next = frags[src_index];
420 tmp_frag->next = NULL;
422 frags[src_index] = tmp_frag;
425 } /* end reassemble_tcp */
427 /* here we search through all the frag we have collected to see if
430 check_fragments( int idx, tcp_stream_chunk *sc, guint32 acknowledged ) {
431 tcp_frag *prev = NULL;
436 current = frags[idx];
438 lowest_seq = current->seq;
440 if( GT_SEQ(lowest_seq, current->seq) ) {
441 lowest_seq = current->seq;
444 if( LT_SEQ(current->seq, seq[idx]) ) {
446 /* this sequence number seems dated, but
447 check the end to make sure it has no more
448 info than we have already seen */
449 newseq = current->seq + current->len;
450 if( newseq > seq[idx] ) {
453 /* this one has more than we have seen. let's get the
454 payload that we have not seen. This happens when
455 part of this frame has been retransmitted */
457 new_pos = seq[idx] - current->seq;
459 if ( current->data_len > new_pos ) {
460 sc->dlen = current->data_len - new_pos;
461 write_packet_data( idx, sc, current->data + new_pos );
464 seq[idx] += (current->len - new_pos);
467 /* Remove the fragment from the list as the "new" part of it
468 * has been processed or its data has been seen already in
471 prev->next = current->next;
473 frags[idx] = current->next;
475 g_free( current->data );
480 if( current->seq == seq[idx] ) {
481 /* this fragment fits the stream */
482 if( current->data ) {
483 sc->dlen = current->data_len;
484 write_packet_data( idx, sc, current->data );
486 seq[idx] += current->len;
488 prev->next = current->next;
490 frags[idx] = current->next;
492 g_free( current->data );
497 current = current->next;
499 if( GT_SEQ(acknowledged, lowest_seq) ) {
500 /* There are frames missing in the capture file that were seen
501 * by the receiving host. Add dummy stream chunk with the data
502 * "[xxx bytes missing in capture file]".
504 dummy_str = g_strdup_printf("[%d bytes missing in capture file]",
505 (int)(lowest_seq - seq[idx]) );
506 sc->dlen = (guint32) strlen(dummy_str);
507 write_packet_data( idx, sc, dummy_str );
509 seq[idx] = lowest_seq;
516 /* this should always be called before we start to reassemble a stream */
518 reset_tcp_reassembly(void)
520 tcp_frag *current, *next;
523 empty_tcp_stream = TRUE;
524 incomplete_tcp_stream = FALSE;
525 find_tcp_addr = FALSE;
526 find_tcp_index = FALSE;
527 for( i=0; i<2; i++ ) {
529 memset(src_addr[i], '\0', MAX_IPADDR_LEN);
531 memset(ip_address[i], '\0', MAX_IPADDR_LEN);
533 bytes_written[i] = 0;
536 next = current->next;
537 g_free( current->data );
546 write_packet_data( int idx, tcp_stream_chunk *sc, const char *data )
550 ret = fwrite( sc, 1, sizeof(tcp_stream_chunk), data_out_file );
551 DISSECTOR_ASSERT(sizeof(tcp_stream_chunk) == ret);
553 ret = fwrite( data, 1, sc->dlen, data_out_file );
554 DISSECTOR_ASSERT(sc->dlen == ret);
556 bytes_written[idx] += sc->dlen;
557 empty_tcp_stream = FALSE;