1998-09-17 03:12:28 +00:00
|
|
|
/* follow.c
|
|
|
|
*
|
|
|
|
* Copyright 1998 Mike Hall <mlh@io.com>
|
|
|
|
*
|
2006-05-21 05:12:17 +00:00
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
1998-09-17 03:12:28 +00:00
|
|
|
* Copyright 1998 Gerald Combs
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
1998-09-17 03:12:28 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
1998-09-17 03:12:28 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-08-28 21:04:11 +00:00
|
|
|
*
|
1998-09-17 03:12:28 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
1998-09-17 03:12:28 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2012-09-20 01:48:30 +00:00
|
|
|
#include "config.h"
|
1998-10-10 03:32:20 +00:00
|
|
|
|
1998-12-17 05:42:33 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
1998-09-17 03:12:28 +00:00
|
|
|
#include <string.h>
|
1999-07-13 02:53:26 +00:00
|
|
|
#ifdef HAVE_UNISTD_H
|
1998-09-17 03:12:28 +00:00
|
|
|
#include <unistd.h>
|
1999-07-13 02:53:26 +00:00
|
|
|
#endif
|
1998-09-17 03:12:28 +00:00
|
|
|
|
1999-07-07 22:52:57 +00:00
|
|
|
#include <glib.h>
|
2002-01-21 07:37:49 +00:00
|
|
|
#include <epan/packet.h>
|
2013-11-09 13:41:10 +00:00
|
|
|
#include <epan/to_str.h>
|
2011-07-17 11:08:02 +00:00
|
|
|
#include <epan/dissectors/packet-tcp.h>
|
2014-12-27 22:15:41 +00:00
|
|
|
#include <epan/dissectors/packet-udp.h>
|
1998-09-17 03:12:28 +00:00
|
|
|
#include "follow.h"
|
2008-09-30 13:29:15 +00:00
|
|
|
#include <epan/conversation.h>
|
2014-12-27 22:15:41 +00:00
|
|
|
#include <epan/tap.h>
|
1998-09-17 03:12:28 +00:00
|
|
|
|
2004-01-23 01:42:45 +00:00
|
|
|
typedef struct _tcp_frag {
|
2012-12-26 05:57:06 +00:00
|
|
|
guint32 seq;
|
|
|
|
guint32 len;
|
|
|
|
guint32 data_len;
|
2004-01-23 01:42:45 +00:00
|
|
|
gchar *data;
|
|
|
|
struct _tcp_frag *next;
|
|
|
|
} tcp_frag;
|
|
|
|
|
2013-07-03 01:43:39 +00:00
|
|
|
WS_DLL_PUBLIC_DEF
|
2002-02-28 19:35:09 +00:00
|
|
|
FILE* data_out_file = NULL;
|
1998-09-17 03:12:28 +00:00
|
|
|
|
2007-05-22 00:00:09 +00:00
|
|
|
gboolean empty_tcp_stream;
|
|
|
|
gboolean incomplete_tcp_stream;
|
1999-03-23 20:25:50 +00:00
|
|
|
|
2014-12-27 22:15:41 +00:00
|
|
|
static guint32 stream_to_follow[MAX_STREAM] = {0};
|
|
|
|
static gboolean find_addr[MAX_STREAM] = {FALSE};
|
|
|
|
static gboolean find_index[MAX_STREAM] = {FALSE};
|
|
|
|
static address tcp_addr[2];
|
2016-01-05 11:30:16 +00:00
|
|
|
static stream_addr ip_address[2];
|
2007-11-03 04:45:35 +00:00
|
|
|
static guint port[2];
|
2002-08-02 23:36:07 +00:00
|
|
|
static guint bytes_written[2];
|
2000-08-11 22:18:22 +00:00
|
|
|
static gboolean is_ipv6 = FALSE;
|
1999-07-31 13:55:16 +00:00
|
|
|
|
2000-08-09 05:18:45 +00:00
|
|
|
void
|
2007-11-03 04:45:35 +00:00
|
|
|
follow_stats(follow_stats_t* stats)
|
2000-08-09 05:18:45 +00:00
|
|
|
{
|
2014-04-01 11:57:26 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 2 ; i++) {
|
2016-01-05 11:30:16 +00:00
|
|
|
stats->ip_address[i] = ip_address[i];
|
2014-04-01 11:57:26 +00:00
|
|
|
stats->port[i] = port[i];
|
|
|
|
stats->bytes_written[i] = bytes_written[i];
|
|
|
|
}
|
2016-01-05 11:30:16 +00:00
|
|
|
stats->is_ipv6 = is_ipv6;
|
2000-08-09 05:18:45 +00:00
|
|
|
}
|
1999-07-07 01:41:15 +00:00
|
|
|
|
2013-11-13 22:18:01 +00:00
|
|
|
/* This will build a display filter text that will only
|
2002-08-28 21:04:11 +00:00
|
|
|
pass the packets related to the stream. There is a
|
|
|
|
chance that two streams could intersect, but not a
|
1998-09-17 03:12:28 +00:00
|
|
|
very good one */
|
2013-11-13 22:18:01 +00:00
|
|
|
gchar*
|
2016-01-02 13:58:08 +00:00
|
|
|
build_follow_conv_filter( packet_info *pi, const char* append_filter ) {
|
2005-08-08 18:50:39 +00:00
|
|
|
char* buf;
|
2000-08-11 22:18:22 +00:00
|
|
|
int len;
|
2008-09-30 13:29:15 +00:00
|
|
|
conversation_t *conv=NULL;
|
2011-07-17 11:08:02 +00:00
|
|
|
struct tcp_analysis *tcpd;
|
2014-12-27 22:15:41 +00:00
|
|
|
struct udp_analysis *udpd;
|
2013-11-22 02:49:00 +00:00
|
|
|
wmem_list_frame_t* protos;
|
|
|
|
int proto_id;
|
|
|
|
const char* proto_name;
|
|
|
|
gboolean is_tcp = FALSE, is_udp = FALSE;
|
|
|
|
|
|
|
|
protos = wmem_list_head(pi->layers);
|
|
|
|
|
|
|
|
/* walk the list of a available protocols in the packet to
|
|
|
|
figure out if any of them affect context sensitivity */
|
|
|
|
while (protos != NULL)
|
|
|
|
{
|
|
|
|
proto_id = GPOINTER_TO_INT(wmem_list_frame_data(protos));
|
|
|
|
proto_name = proto_get_protocol_filter_name(proto_id);
|
|
|
|
|
|
|
|
if (!strcmp(proto_name, "tcp")) {
|
|
|
|
is_tcp = TRUE;
|
|
|
|
} else if (!strcmp(proto_name, "udp")) {
|
|
|
|
is_udp = TRUE;
|
|
|
|
}
|
2014-02-25 20:42:35 +00:00
|
|
|
|
2013-11-22 02:49:00 +00:00
|
|
|
protos = wmem_list_frame_next(protos);
|
|
|
|
}
|
2008-09-30 13:29:15 +00:00
|
|
|
|
2011-07-17 11:08:02 +00:00
|
|
|
if( ((pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4) ||
|
|
|
|
(pi->net_src.type == AT_IPv6 && pi->net_dst.type == AT_IPv6))
|
2014-04-01 11:57:26 +00:00
|
|
|
&& is_tcp && (conv=find_conversation(pi->fd->num, &pi->src, &pi->dst, pi->ptype,
|
2008-09-30 13:29:15 +00:00
|
|
|
pi->srcport, pi->destport, 0)) != NULL ) {
|
2014-12-27 22:15:41 +00:00
|
|
|
/* TCP over IPv4/6 */
|
2011-07-17 11:08:02 +00:00
|
|
|
tcpd=get_tcp_conversation_data(conv, pi);
|
|
|
|
if (tcpd) {
|
2016-01-02 13:58:08 +00:00
|
|
|
if (append_filter == NULL) {
|
|
|
|
buf = g_strdup_printf("tcp.stream eq %d", tcpd->stream);
|
|
|
|
} else {
|
|
|
|
buf = g_strdup_printf("((tcp.stream eq %d) && (%s))", tcpd->stream, append_filter);
|
|
|
|
}
|
2014-12-27 22:15:41 +00:00
|
|
|
stream_to_follow[TCP_STREAM] = tcpd->stream;
|
2011-07-17 11:08:02 +00:00
|
|
|
if (pi->net_src.type == AT_IPv4) {
|
|
|
|
len = 4;
|
|
|
|
is_ipv6 = FALSE;
|
|
|
|
} else {
|
|
|
|
len = 16;
|
|
|
|
is_ipv6 = TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2000-08-11 22:18:22 +00:00
|
|
|
}
|
2014-12-27 22:15:41 +00:00
|
|
|
else if( ((pi->net_src.type == AT_IPv4 && pi->net_dst.type == AT_IPv4) ||
|
|
|
|
(pi->net_src.type == AT_IPv6 && pi->net_dst.type == AT_IPv6))
|
|
|
|
&& is_udp && (conv=find_conversation(pi->fd->num, &pi->src, &pi->dst, pi->ptype,
|
|
|
|
pi->srcport, pi->destport, 0)) != NULL ) {
|
|
|
|
/* UDP over IPv4/6 */
|
|
|
|
udpd=get_udp_conversation_data(conv, pi);
|
|
|
|
if (udpd) {
|
2016-01-02 13:58:08 +00:00
|
|
|
if (append_filter == NULL) {
|
|
|
|
buf = g_strdup_printf("udp.stream eq %d", udpd->stream);
|
|
|
|
} else {
|
|
|
|
buf = g_strdup_printf("((udp.stream eq %d) && (%s))", udpd->stream, append_filter);
|
|
|
|
}
|
2014-12-27 22:15:41 +00:00
|
|
|
stream_to_follow[UDP_STREAM] = udpd->stream;
|
|
|
|
if (pi->net_src.type == AT_IPv4) {
|
|
|
|
len = 4;
|
|
|
|
is_ipv6 = FALSE;
|
|
|
|
} else {
|
|
|
|
len = 16;
|
|
|
|
is_ipv6 = TRUE;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
2007-11-03 04:45:35 +00:00
|
|
|
}
|
2002-08-28 21:04:11 +00:00
|
|
|
else {
|
1998-09-17 03:12:28 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2016-01-05 11:30:16 +00:00
|
|
|
memcpy(&ip_address[0], pi->net_src.data, len);
|
|
|
|
memcpy(&ip_address[1], pi->net_dst.data, len);
|
2007-11-03 04:45:35 +00:00
|
|
|
port[0] = pi->srcport;
|
|
|
|
port[1] = pi->destport;
|
1998-09-17 03:12:28 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2014-12-27 22:15:41 +00:00
|
|
|
static gboolean
|
|
|
|
udp_follow_packet(void *tapdata _U_, packet_info *pinfo,
|
|
|
|
epan_dissect_t *edt _U_, const void *data _U_)
|
|
|
|
{
|
|
|
|
if (find_addr[UDP_STREAM]) {
|
|
|
|
if (pinfo->net_src.type == AT_IPv6) {
|
|
|
|
is_ipv6 = TRUE;
|
|
|
|
} else {
|
|
|
|
is_ipv6 = FALSE;
|
|
|
|
}
|
2016-01-05 11:30:16 +00:00
|
|
|
memcpy(&ip_address[0], pinfo->net_src.data, pinfo->net_src.len);
|
|
|
|
memcpy(&ip_address[1], pinfo->net_dst.data, pinfo->net_dst.len);
|
2014-12-27 22:15:41 +00:00
|
|
|
port[0] = pinfo->srcport;
|
|
|
|
port[1] = pinfo->destport;
|
|
|
|
find_addr[UDP_STREAM] = FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2016-01-03 05:58:31 +00:00
|
|
|
|
|
|
|
/* here we are going to try and reconstruct the data portion of a TCP
|
|
|
|
session. We will try and handle duplicates, TCP fragments, and out
|
|
|
|
of order packets in a smart way. */
|
|
|
|
|
|
|
|
static tcp_frag *frags[2] = { 0, 0 };
|
|
|
|
static guint32 seq[2];
|
2016-01-05 11:30:16 +00:00
|
|
|
static stream_addr src_addr[2];
|
2016-01-03 05:58:31 +00:00
|
|
|
static guint src_port[2] = { 0, 0 };
|
|
|
|
|
2014-12-27 22:15:41 +00:00
|
|
|
void
|
2016-01-03 05:58:31 +00:00
|
|
|
reset_stream_follow(stream_type stream) {
|
|
|
|
tcp_frag *current, *next;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
remove_tap_listener(&stream_to_follow[stream]);
|
|
|
|
find_addr[stream] = FALSE;
|
|
|
|
find_index[stream] = FALSE;
|
|
|
|
if (stream == TCP_STREAM) {
|
|
|
|
empty_tcp_stream = TRUE;
|
|
|
|
incomplete_tcp_stream = FALSE;
|
|
|
|
|
|
|
|
for( i=0; i<2; i++ ) {
|
|
|
|
seq[i] = 0;
|
2016-01-05 11:30:16 +00:00
|
|
|
memset(&src_addr[i], 0, sizeof(src_addr[i]));
|
2016-01-03 05:58:31 +00:00
|
|
|
src_port[i] = 0;
|
2016-01-05 11:30:16 +00:00
|
|
|
memset(&ip_address[i], 0, sizeof(src_addr[i]));
|
2016-01-03 05:58:31 +00:00
|
|
|
port[i] = 0;
|
|
|
|
bytes_written[i] = 0;
|
|
|
|
current = frags[i];
|
|
|
|
while( current ) {
|
|
|
|
next = current->next;
|
|
|
|
g_free( current->data );
|
|
|
|
g_free( current );
|
|
|
|
current = next;
|
|
|
|
}
|
|
|
|
frags[i] = NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-12-27 22:15:41 +00:00
|
|
|
}
|
2012-02-05 10:39:12 +00:00
|
|
|
|
2013-11-13 22:18:01 +00:00
|
|
|
gchar*
|
2014-12-27 22:15:41 +00:00
|
|
|
build_follow_index_filter(stream_type stream) {
|
2013-11-13 22:18:01 +00:00
|
|
|
gchar *buf;
|
|
|
|
|
2014-12-27 22:15:41 +00:00
|
|
|
find_addr[stream] = TRUE;
|
|
|
|
if (stream == TCP_STREAM) {
|
|
|
|
buf = g_strdup_printf("tcp.stream eq %d", stream_to_follow[TCP_STREAM]);
|
|
|
|
} else {
|
|
|
|
GString * error_string;
|
|
|
|
buf = g_strdup_printf("udp.stream eq %d", stream_to_follow[UDP_STREAM]);
|
|
|
|
error_string = register_tap_listener("udp_follow", &stream_to_follow[UDP_STREAM], buf, 0, NULL, udp_follow_packet, NULL);
|
|
|
|
if (error_string) {
|
|
|
|
g_string_free(error_string, TRUE);
|
|
|
|
}
|
|
|
|
}
|
2013-11-13 22:18:01 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2012-02-05 10:39:12 +00:00
|
|
|
/* select a tcp stream to follow via it's address/port pairs */
|
|
|
|
gboolean
|
2014-12-27 22:15:41 +00:00
|
|
|
follow_addr(stream_type stream, const address *addr0, guint port0,
|
|
|
|
const address *addr1, guint port1)
|
2012-02-05 10:39:12 +00:00
|
|
|
{
|
|
|
|
if (addr0 == NULL || addr1 == NULL || addr0->type != addr1->type ||
|
|
|
|
port0 > G_MAXUINT16 || port1 > G_MAXUINT16 ) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-12-27 22:15:41 +00:00
|
|
|
if (find_index[stream] || find_addr[stream]) {
|
2012-02-05 10:39:12 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (addr0->type) {
|
|
|
|
default:
|
|
|
|
return FALSE;
|
|
|
|
case AT_IPv4:
|
|
|
|
case AT_IPv6:
|
|
|
|
is_ipv6 = addr0->type == AT_IPv6;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-05 11:30:16 +00:00
|
|
|
memcpy(&ip_address[0], addr0->data, addr0->len);
|
2012-02-05 10:39:12 +00:00
|
|
|
port[0] = port0;
|
|
|
|
|
2016-01-05 11:30:16 +00:00
|
|
|
memcpy(&ip_address[1], addr1->data, addr1->len);
|
2012-02-05 10:39:12 +00:00
|
|
|
port[1] = port1;
|
|
|
|
|
2014-12-27 22:15:41 +00:00
|
|
|
if (stream == TCP_STREAM) {
|
|
|
|
find_index[TCP_STREAM] = TRUE;
|
2016-01-05 11:30:16 +00:00
|
|
|
set_address(&tcp_addr[0], addr0->type, addr0->len, &ip_address[0]);
|
|
|
|
set_address(&tcp_addr[1], addr1->type, addr1->len, &ip_address[1]);
|
2014-12-27 22:15:41 +00:00
|
|
|
}
|
|
|
|
|
2012-02-05 10:39:12 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2014-12-27 22:15:41 +00:00
|
|
|
/* select a stream to follow via its index */
|
2012-02-05 10:39:12 +00:00
|
|
|
gboolean
|
2014-12-27 22:15:41 +00:00
|
|
|
follow_index(stream_type stream, guint32 indx)
|
2012-02-05 10:39:12 +00:00
|
|
|
{
|
2014-12-27 22:15:41 +00:00
|
|
|
if (find_index[stream] || find_addr[stream]) {
|
2012-02-05 10:39:12 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-12-27 22:15:41 +00:00
|
|
|
find_addr[stream] = TRUE;
|
|
|
|
stream_to_follow[stream] = indx;
|
2012-02-05 10:39:12 +00:00
|
|
|
memset(ip_address, 0, sizeof ip_address);
|
|
|
|
port[0] = port[1] = 0;
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2013-11-13 22:18:01 +00:00
|
|
|
guint32
|
2014-12-27 22:15:41 +00:00
|
|
|
get_follow_index(stream_type stream) {
|
|
|
|
return stream_to_follow[stream];
|
2013-11-13 22:18:01 +00:00
|
|
|
}
|
|
|
|
|
2014-10-10 14:41:10 +00:00
|
|
|
/*
|
|
|
|
* Editor modelines - http://www.wireshark.org/tools/modelines.html
|
|
|
|
*
|
|
|
|
* Local Variables:
|
|
|
|
* c-basic-offset: 2
|
|
|
|
* tab-width: 8
|
|
|
|
* indent-tabs-mode: nil
|
|
|
|
* End:
|
|
|
|
*
|
|
|
|
* ex: set shiftwidth=2 tabstop=8 expandtab:
|
|
|
|
* :indentSize=2:tabSize=8:noTabs=true:
|
|
|
|
*/
|