2009-07-08 13:19:16 -07:00
|
|
|
|
/*
|
2015-07-22 11:22:01 -07:00
|
|
|
|
* Copyright (c) 2009, 2010, 2012, 2013, 2014, 2015 Nicira, Inc.
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at:
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*
|
2009-06-15 15:11:30 -07:00
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
2013-03-15 02:19:31 -07:00
|
|
|
|
#include "pcap-file.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <inttypes.h>
|
2013-09-23 10:27:10 -07:00
|
|
|
|
#include <stdlib.h>
|
2009-07-08 13:19:16 -07:00
|
|
|
|
#include <string.h>
|
2013-09-23 10:27:10 -07:00
|
|
|
|
#include <sys/stat.h>
|
2013-12-02 14:53:27 -08:00
|
|
|
|
#include "byte-order.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
|
#include "compiler.h"
|
2015-02-22 03:21:09 -08:00
|
|
|
|
#include "dp-packet.h"
|
2013-11-22 13:17:23 -08:00
|
|
|
|
#include "flow.h"
|
2016-07-12 16:37:34 -05:00
|
|
|
|
#include "openvswitch/hmap.h"
|
2013-11-22 13:17:23 -08:00
|
|
|
|
#include "packets.h"
|
2013-11-22 11:42:06 -08:00
|
|
|
|
#include "timeval.h"
|
2013-11-22 13:17:23 -08:00
|
|
|
|
#include "unaligned.h"
|
2016-07-12 16:37:34 -05:00
|
|
|
|
#include "util.h"
|
2014-12-15 14:10:38 +01:00
|
|
|
|
#include "openvswitch/vlog.h"
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2010-10-19 14:47:01 -07:00
|
|
|
|
VLOG_DEFINE_THIS_MODULE(pcap);
|
2010-07-16 11:02:49 -07:00
|
|
|
|
|
2018-10-05 12:52:40 -04:00
|
|
|
|
enum ts_resolution {
|
|
|
|
|
PCAP_USEC,
|
|
|
|
|
PCAP_NSEC,
|
|
|
|
|
};
|
|
|
|
|
|
2018-11-11 15:41:08 -08:00
|
|
|
|
enum network_type {
|
|
|
|
|
PCAP_ETHERNET = 0,
|
|
|
|
|
PCAP_LINUX_SLL = 0x71
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-05 12:52:40 -04:00
|
|
|
|
struct pcap_file {
|
|
|
|
|
FILE *file;
|
|
|
|
|
enum ts_resolution resolution;
|
2018-11-11 15:41:08 -08:00
|
|
|
|
enum network_type network;
|
2018-10-05 12:52:40 -04:00
|
|
|
|
};
|
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
|
struct pcap_hdr {
|
|
|
|
|
uint32_t magic_number; /* magic number */
|
|
|
|
|
uint16_t version_major; /* major version number */
|
|
|
|
|
uint16_t version_minor; /* minor version number */
|
|
|
|
|
int32_t thiszone; /* GMT to local correction */
|
|
|
|
|
uint32_t sigfigs; /* accuracy of timestamps */
|
|
|
|
|
uint32_t snaplen; /* max length of captured packets */
|
|
|
|
|
uint32_t network; /* data link type */
|
2010-02-12 13:56:12 -08:00
|
|
|
|
};
|
|
|
|
|
BUILD_ASSERT_DECL(sizeof(struct pcap_hdr) == 24);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
|
|
struct pcaprec_hdr {
|
|
|
|
|
uint32_t ts_sec; /* timestamp seconds */
|
2018-10-05 12:52:40 -04:00
|
|
|
|
uint32_t ts_subsec; /* timestamp subseconds */
|
2009-07-08 13:19:16 -07:00
|
|
|
|
uint32_t incl_len; /* number of octets of packet saved in file */
|
|
|
|
|
uint32_t orig_len; /* actual length of packet */
|
2010-02-12 13:56:12 -08:00
|
|
|
|
};
|
|
|
|
|
BUILD_ASSERT_DECL(sizeof(struct pcaprec_hdr) == 16);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2018-10-05 12:52:40 -04:00
|
|
|
|
struct pcap_file *
|
2014-01-23 17:24:03 +01:00
|
|
|
|
ovs_pcap_open(const char *file_name, const char *mode)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
2013-09-23 10:27:10 -07:00
|
|
|
|
struct stat s;
|
2018-10-05 12:52:40 -04:00
|
|
|
|
struct pcap_file *p_file;
|
2013-09-23 10:27:10 -07:00
|
|
|
|
int error;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2013-09-23 10:27:10 -07:00
|
|
|
|
ovs_assert(!strcmp(mode, "rb") ||
|
|
|
|
|
!strcmp(mode, "wb") ||
|
|
|
|
|
!strcmp(mode, "ab"));
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
2018-10-05 12:52:40 -04:00
|
|
|
|
p_file = xmalloc(sizeof *p_file);
|
|
|
|
|
p_file->file = fopen(file_name, mode);
|
|
|
|
|
p_file->resolution = PCAP_USEC;
|
|
|
|
|
if (p_file->file == NULL) {
|
2013-09-23 10:27:10 -07:00
|
|
|
|
VLOG_WARN("%s: failed to open pcap file for %s (%s)", file_name,
|
|
|
|
|
(mode[0] == 'r' ? "reading"
|
|
|
|
|
: mode[0] == 'w' ? "writing"
|
|
|
|
|
: "appending"),
|
|
|
|
|
ovs_strerror(errno));
|
2009-07-08 13:19:16 -07:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
2013-09-23 10:27:10 -07:00
|
|
|
|
switch (mode[0]) {
|
|
|
|
|
case 'r':
|
2018-10-05 12:52:40 -04:00
|
|
|
|
error = ovs_pcap_read_header(p_file);
|
2013-09-23 10:27:10 -07:00
|
|
|
|
if (error) {
|
|
|
|
|
errno = error;
|
2018-10-05 12:52:40 -04:00
|
|
|
|
ovs_pcap_close(p_file);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2013-09-23 10:27:10 -07:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'w':
|
2018-10-05 12:52:40 -04:00
|
|
|
|
ovs_pcap_write_header(p_file);
|
2013-09-23 10:27:10 -07:00
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 'a':
|
2018-10-05 12:52:40 -04:00
|
|
|
|
if (!fstat(fileno(p_file->file), &s) && !s.st_size) {
|
|
|
|
|
ovs_pcap_write_header(p_file);
|
2013-09-23 10:27:10 -07:00
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
2013-12-17 10:32:12 -08:00
|
|
|
|
OVS_NOT_REACHED();
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
2018-10-05 12:52:40 -04:00
|
|
|
|
|
|
|
|
|
return p_file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct pcap_file *
|
|
|
|
|
ovs_pcap_stdout(void)
|
|
|
|
|
{
|
|
|
|
|
struct pcap_file *p_file = xmalloc(sizeof *p_file);
|
|
|
|
|
p_file->file = stdout;
|
|
|
|
|
return p_file;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2018-10-05 12:52:40 -04:00
|
|
|
|
ovs_pcap_read_header(struct pcap_file *p_file)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
|
|
|
|
struct pcap_hdr ph;
|
2018-10-05 12:52:40 -04:00
|
|
|
|
if (fread(&ph, sizeof ph, 1, p_file->file) != 1) {
|
|
|
|
|
int error = ferror(p_file->file) ? errno : EOF;
|
2011-01-30 11:29:14 -08:00
|
|
|
|
VLOG_WARN("failed to read pcap header: %s", ovs_retval_to_string(error));
|
2009-07-08 13:19:16 -07:00
|
|
|
|
return error;
|
|
|
|
|
}
|
2018-11-11 15:41:08 -08:00
|
|
|
|
bool byte_swap;
|
2018-10-05 12:52:40 -04:00
|
|
|
|
if (ph.magic_number == 0xa1b2c3d4 || ph.magic_number == 0xd4c3b2a1) {
|
2018-11-11 15:41:08 -08:00
|
|
|
|
byte_swap = ph.magic_number == 0xd4c3b2a1;
|
2018-10-05 12:52:40 -04:00
|
|
|
|
p_file->resolution = PCAP_USEC;
|
|
|
|
|
} else if (ph.magic_number == 0xa1b23c4d ||
|
|
|
|
|
ph.magic_number == 0x4d3cb2a1) {
|
2018-11-11 15:41:08 -08:00
|
|
|
|
byte_swap = ph.magic_number == 0x4d3cb2a1;
|
2018-10-05 12:52:40 -04:00
|
|
|
|
p_file->resolution = PCAP_NSEC;
|
|
|
|
|
} else {
|
2009-07-08 13:19:16 -07:00
|
|
|
|
VLOG_WARN("bad magic 0x%08"PRIx32" reading pcap file "
|
2018-10-05 12:52:40 -04:00
|
|
|
|
"(expected 0xa1b2c3d4, 0xa1b23c4d, 0xd4c3b2a1, "
|
|
|
|
|
"or 0x4d3cb2a1)", ph.magic_number);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
return EPROTO;
|
|
|
|
|
}
|
2018-11-11 15:41:08 -08:00
|
|
|
|
p_file->network = byte_swap ? uint32_byteswap(ph.network) : ph.network;
|
|
|
|
|
if (p_file->network != PCAP_ETHERNET &&
|
|
|
|
|
p_file->network != PCAP_LINUX_SLL) {
|
|
|
|
|
VLOG_WARN("unknown network type %"PRIu16" reading pcap file",
|
|
|
|
|
p_file->network);
|
|
|
|
|
return EPROTO;
|
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2018-10-05 12:52:40 -04:00
|
|
|
|
ovs_pcap_write_header(struct pcap_file *p_file)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
|
|
|
|
/* The pcap reader is responsible for figuring out endianness based on the
|
|
|
|
|
* magic number, so the lack of htonX calls here is intentional. */
|
|
|
|
|
struct pcap_hdr ph;
|
|
|
|
|
ph.magic_number = 0xa1b2c3d4;
|
|
|
|
|
ph.version_major = 2;
|
|
|
|
|
ph.version_minor = 4;
|
|
|
|
|
ph.thiszone = 0;
|
|
|
|
|
ph.sigfigs = 0;
|
|
|
|
|
ph.snaplen = 1518;
|
|
|
|
|
ph.network = 1; /* Ethernet */
|
2018-10-05 12:52:40 -04:00
|
|
|
|
ignore(fwrite(&ph, sizeof ph, 1, p_file->file));
|
|
|
|
|
fflush(p_file->file);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
2018-10-05 12:52:40 -04:00
|
|
|
|
ovs_pcap_read(struct pcap_file *p_file, struct dp_packet **bufp,
|
|
|
|
|
long long int *when)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
|
|
|
|
struct pcaprec_hdr prh;
|
2015-02-22 03:21:09 -08:00
|
|
|
|
struct dp_packet *buf;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
void *data;
|
|
|
|
|
size_t len;
|
2013-11-22 11:42:06 -08:00
|
|
|
|
bool swap;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
|
|
|
|
|
*bufp = NULL;
|
|
|
|
|
|
|
|
|
|
/* Read header. */
|
2018-10-05 12:52:40 -04:00
|
|
|
|
if (fread(&prh, sizeof prh, 1, p_file->file) != 1) {
|
|
|
|
|
if (ferror(p_file->file)) {
|
2013-09-23 10:14:35 -07:00
|
|
|
|
int error = errno;
|
|
|
|
|
VLOG_WARN("failed to read pcap record header: %s",
|
|
|
|
|
ovs_retval_to_string(error));
|
|
|
|
|
return error;
|
|
|
|
|
} else {
|
|
|
|
|
return EOF;
|
|
|
|
|
}
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Calculate length. */
|
|
|
|
|
len = prh.incl_len;
|
2013-11-22 11:42:06 -08:00
|
|
|
|
swap = len > 0xffff;
|
|
|
|
|
if (swap) {
|
|
|
|
|
len = uint32_byteswap(len);
|
|
|
|
|
if (len > 0xffff) {
|
2018-07-31 13:34:59 -07:00
|
|
|
|
VLOG_WARN("bad packet length %"PRIuSIZE" or %"PRIu32" "
|
2009-07-08 13:19:16 -07:00
|
|
|
|
"reading pcap file",
|
2013-11-22 11:42:06 -08:00
|
|
|
|
len, uint32_byteswap(len));
|
2009-07-08 13:19:16 -07:00
|
|
|
|
return EPROTO;
|
|
|
|
|
}
|
2013-11-22 11:42:06 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Calculate time. */
|
|
|
|
|
if (when) {
|
|
|
|
|
uint32_t ts_sec = swap ? uint32_byteswap(prh.ts_sec) : prh.ts_sec;
|
2018-10-05 12:52:40 -04:00
|
|
|
|
uint32_t ts_subsec = swap ? uint32_byteswap(prh.ts_subsec)
|
|
|
|
|
: prh.ts_subsec;
|
|
|
|
|
ts_subsec = p_file->resolution == PCAP_USEC ? ts_subsec / 1000
|
|
|
|
|
: ts_subsec / 1000000;
|
|
|
|
|
*when = ts_sec * 1000LL + ts_subsec;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
|
|
|
|
|
2017-04-25 16:29:59 +00:00
|
|
|
|
/* Read packet. Packet type is Ethernet */
|
2015-02-22 03:21:09 -08:00
|
|
|
|
buf = dp_packet_new(len);
|
|
|
|
|
data = dp_packet_put_uninit(buf, len);
|
2018-10-05 12:52:40 -04:00
|
|
|
|
if (fread(data, len, 1, p_file->file) != 1) {
|
|
|
|
|
int error = ferror(p_file->file) ? errno : EOF;
|
2009-07-08 13:19:16 -07:00
|
|
|
|
VLOG_WARN("failed to read pcap packet: %s",
|
2011-01-30 11:29:14 -08:00
|
|
|
|
ovs_retval_to_string(error));
|
2015-02-22 03:21:09 -08:00
|
|
|
|
dp_packet_delete(buf);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
return error;
|
|
|
|
|
}
|
2018-11-11 15:41:08 -08:00
|
|
|
|
|
|
|
|
|
if (p_file->network == PCAP_LINUX_SLL) {
|
|
|
|
|
/* This format doesn't include the destination Ethernet address, which
|
|
|
|
|
* is weird. */
|
|
|
|
|
|
|
|
|
|
struct sll_header {
|
|
|
|
|
ovs_be16 packet_type;
|
|
|
|
|
ovs_be16 arp_hrd;
|
|
|
|
|
ovs_be16 lla_len;
|
|
|
|
|
struct eth_addr dl_src;
|
|
|
|
|
ovs_be16 reserved;
|
|
|
|
|
ovs_be16 protocol;
|
|
|
|
|
};
|
|
|
|
|
const struct sll_header *sll;
|
|
|
|
|
if (len < sizeof *sll) {
|
|
|
|
|
VLOG_WARN("pcap packet too short for SLL header");
|
|
|
|
|
dp_packet_delete(buf);
|
|
|
|
|
return EPROTO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Pull Linux SLL header. */
|
|
|
|
|
sll = dp_packet_pull(buf, sizeof *sll);
|
|
|
|
|
if (sll->lla_len != htons(6)) {
|
|
|
|
|
ovs_hex_dump(stdout, sll, sizeof *sll, 0, false);
|
|
|
|
|
VLOG_WARN("bad SLL header");
|
|
|
|
|
dp_packet_delete(buf);
|
|
|
|
|
return EPROTO;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Push Ethernet header. */
|
|
|
|
|
struct eth_header eth = {
|
|
|
|
|
/* eth_dst is all zeros because the format doesn't include it. */
|
|
|
|
|
.eth_src = sll->dl_src,
|
|
|
|
|
.eth_type = sll->protocol,
|
|
|
|
|
};
|
|
|
|
|
dp_packet_push(buf, ð, sizeof eth);
|
|
|
|
|
}
|
|
|
|
|
|
2009-07-08 13:19:16 -07:00
|
|
|
|
*bufp = buf;
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2018-10-05 12:52:40 -04:00
|
|
|
|
ovs_pcap_write(struct pcap_file *p_file, struct dp_packet *buf)
|
2009-07-08 13:19:16 -07:00
|
|
|
|
{
|
|
|
|
|
struct pcaprec_hdr prh;
|
2013-11-22 11:42:06 -08:00
|
|
|
|
struct timeval tv;
|
|
|
|
|
|
2017-04-25 16:29:59 +00:00
|
|
|
|
ovs_assert(buf->packet_type == htonl(PT_ETH));
|
|
|
|
|
|
2013-11-22 11:42:06 -08:00
|
|
|
|
xgettimeofday(&tv);
|
|
|
|
|
prh.ts_sec = tv.tv_sec;
|
2018-10-05 12:52:40 -04:00
|
|
|
|
prh.ts_subsec = tv.tv_usec;
|
2015-02-22 03:21:09 -08:00
|
|
|
|
prh.incl_len = dp_packet_size(buf);
|
|
|
|
|
prh.orig_len = dp_packet_size(buf);
|
2018-10-05 12:52:40 -04:00
|
|
|
|
ignore(fwrite(&prh, sizeof prh, 1, p_file->file));
|
|
|
|
|
ignore(fwrite(dp_packet_data(buf), dp_packet_size(buf), 1, p_file->file));
|
|
|
|
|
fflush(p_file->file);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
ovs_pcap_close(struct pcap_file *p_file)
|
|
|
|
|
{
|
|
|
|
|
if (p_file->file != stdout) {
|
|
|
|
|
fclose(p_file->file);
|
|
|
|
|
}
|
|
|
|
|
free(p_file);
|
2009-07-08 13:19:16 -07:00
|
|
|
|
}
|
2013-11-22 13:17:23 -08:00
|
|
|
|
|
|
|
|
|
struct tcp_key {
|
|
|
|
|
ovs_be32 nw_src, nw_dst;
|
|
|
|
|
ovs_be16 tp_src, tp_dst;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct tcp_stream {
|
|
|
|
|
struct hmap_node hmap_node;
|
|
|
|
|
struct tcp_key key;
|
|
|
|
|
uint32_t seq_no;
|
2015-02-22 03:21:09 -08:00
|
|
|
|
struct dp_packet payload;
|
2013-11-22 13:17:23 -08:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct tcp_reader {
|
|
|
|
|
struct hmap streams;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
tcp_stream_destroy(struct tcp_reader *r, struct tcp_stream *stream)
|
|
|
|
|
{
|
|
|
|
|
hmap_remove(&r->streams, &stream->hmap_node);
|
2015-02-22 03:21:09 -08:00
|
|
|
|
dp_packet_uninit(&stream->payload);
|
2013-11-22 13:17:23 -08:00
|
|
|
|
free(stream);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Returns a new data structure for extracting TCP stream data from an
|
|
|
|
|
* Ethernet packet capture */
|
|
|
|
|
struct tcp_reader *
|
|
|
|
|
tcp_reader_open(void)
|
|
|
|
|
{
|
|
|
|
|
struct tcp_reader *r;
|
|
|
|
|
|
|
|
|
|
r = xmalloc(sizeof *r);
|
|
|
|
|
hmap_init(&r->streams);
|
|
|
|
|
return r;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Closes and frees 'r'. */
|
|
|
|
|
void
|
|
|
|
|
tcp_reader_close(struct tcp_reader *r)
|
|
|
|
|
{
|
|
|
|
|
struct tcp_stream *stream, *next_stream;
|
|
|
|
|
|
|
|
|
|
HMAP_FOR_EACH_SAFE (stream, next_stream, hmap_node, &r->streams) {
|
|
|
|
|
tcp_stream_destroy(r, stream);
|
|
|
|
|
}
|
|
|
|
|
hmap_destroy(&r->streams);
|
|
|
|
|
free(r);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct tcp_stream *
|
2014-02-04 09:01:16 -08:00
|
|
|
|
tcp_stream_lookup(struct tcp_reader *r,
|
|
|
|
|
const struct tcp_key *key, uint32_t hash)
|
2013-11-22 13:17:23 -08:00
|
|
|
|
{
|
|
|
|
|
struct tcp_stream *stream;
|
|
|
|
|
|
|
|
|
|
HMAP_FOR_EACH_WITH_HASH (stream, hmap_node, hash, &r->streams) {
|
2014-02-04 09:01:16 -08:00
|
|
|
|
if (!memcmp(&stream->key, key, sizeof *key)) {
|
2013-11-22 13:17:23 -08:00
|
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-02-04 09:01:16 -08:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static struct tcp_stream *
|
|
|
|
|
tcp_stream_new(struct tcp_reader *r, const struct tcp_key *key, uint32_t hash)
|
|
|
|
|
{
|
|
|
|
|
struct tcp_stream *stream;
|
2013-11-22 13:17:23 -08:00
|
|
|
|
|
|
|
|
|
stream = xmalloc(sizeof *stream);
|
|
|
|
|
hmap_insert(&r->streams, &stream->hmap_node, hash);
|
2014-02-04 09:01:16 -08:00
|
|
|
|
memcpy(&stream->key, key, sizeof *key);
|
2013-11-22 13:17:23 -08:00
|
|
|
|
stream->seq_no = 0;
|
2015-02-22 03:21:09 -08:00
|
|
|
|
dp_packet_init(&stream->payload, 2048);
|
2013-11-22 13:17:23 -08:00
|
|
|
|
return stream;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Processes 'packet' through TCP reader 'r'. The caller must have already
|
|
|
|
|
* extracted the packet's headers into 'flow', using flow_extract().
|
|
|
|
|
*
|
|
|
|
|
* If 'packet' is a TCP packet, then the reader attempts to reconstruct the
|
2015-02-22 03:21:09 -08:00
|
|
|
|
* data stream. If successful, it returns an dp_packet that represents the data
|
|
|
|
|
* stream so far. The caller may examine the data in the dp_packet and pull off
|
2013-11-22 13:17:23 -08:00
|
|
|
|
* any data that it has fully processed. The remaining data that the caller
|
|
|
|
|
* does not pull off will be presented again in future calls if more data
|
|
|
|
|
* arrives in the stream.
|
|
|
|
|
*
|
|
|
|
|
* Returns null if 'packet' doesn't add new data to a TCP stream. */
|
2015-02-22 03:21:09 -08:00
|
|
|
|
struct dp_packet *
|
2013-11-22 13:17:23 -08:00
|
|
|
|
tcp_reader_run(struct tcp_reader *r, const struct flow *flow,
|
2015-02-22 03:21:09 -08:00
|
|
|
|
const struct dp_packet *packet)
|
2013-11-22 13:17:23 -08:00
|
|
|
|
{
|
|
|
|
|
struct tcp_stream *stream;
|
|
|
|
|
struct tcp_header *tcp;
|
2015-02-22 03:21:09 -08:00
|
|
|
|
struct dp_packet *payload;
|
2014-02-04 09:01:16 -08:00
|
|
|
|
unsigned int l7_length;
|
|
|
|
|
struct tcp_key key;
|
|
|
|
|
uint32_t hash;
|
2013-11-22 13:17:23 -08:00
|
|
|
|
uint32_t seq;
|
|
|
|
|
uint8_t flags;
|
2015-02-22 03:21:09 -08:00
|
|
|
|
const char *l7 = dp_packet_get_tcp_payload(packet);
|
2013-11-22 13:17:23 -08:00
|
|
|
|
|
|
|
|
|
if (flow->dl_type != htons(ETH_TYPE_IP)
|
|
|
|
|
|| flow->nw_proto != IPPROTO_TCP
|
2014-03-25 15:26:23 -07:00
|
|
|
|
|| !l7) {
|
2013-11-22 13:17:23 -08:00
|
|
|
|
return NULL;
|
|
|
|
|
}
|
2015-02-22 03:21:09 -08:00
|
|
|
|
tcp = dp_packet_l4(packet);
|
2013-11-22 13:17:23 -08:00
|
|
|
|
flags = TCP_FLAGS(tcp->tcp_ctl);
|
2015-02-22 03:21:09 -08:00
|
|
|
|
l7_length = (char *) dp_packet_tail(packet) - l7;
|
2013-11-22 13:17:23 -08:00
|
|
|
|
seq = ntohl(get_16aligned_be32(&tcp->tcp_seq));
|
2014-02-04 09:01:16 -08:00
|
|
|
|
|
|
|
|
|
/* Construct key. */
|
|
|
|
|
memset(&key, 0, sizeof key);
|
|
|
|
|
key.nw_src = flow->nw_src;
|
|
|
|
|
key.nw_dst = flow->nw_dst;
|
|
|
|
|
key.tp_src = flow->tp_src;
|
|
|
|
|
key.tp_dst = flow->tp_dst;
|
|
|
|
|
hash = hash_bytes(&key, sizeof key, 0);
|
|
|
|
|
|
|
|
|
|
/* Find existing stream or start a new one for a SYN or if there's data. */
|
|
|
|
|
stream = tcp_stream_lookup(r, &key, hash);
|
|
|
|
|
if (!stream) {
|
|
|
|
|
if (flags & TCP_SYN || l7_length) {
|
|
|
|
|
stream = tcp_stream_new(r, &key, hash);
|
|
|
|
|
stream->seq_no = flags & TCP_SYN ? seq + 1 : seq;
|
|
|
|
|
} else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
payload = &stream->payload;
|
|
|
|
|
if (flags & TCP_SYN || !stream->seq_no) {
|
2015-02-22 03:21:09 -08:00
|
|
|
|
dp_packet_clear(payload);
|
2013-11-22 13:17:23 -08:00
|
|
|
|
stream->seq_no = seq + 1;
|
|
|
|
|
return NULL;
|
|
|
|
|
} else if (flags & (TCP_FIN | TCP_RST)) {
|
|
|
|
|
tcp_stream_destroy(r, stream);
|
|
|
|
|
return NULL;
|
|
|
|
|
} else if (seq == stream->seq_no) {
|
|
|
|
|
/* Shift all of the existing payload to the very beginning of the
|
|
|
|
|
* allocated space, so that we reuse allocated space instead of
|
|
|
|
|
* continually expanding it. */
|
2015-02-22 03:21:09 -08:00
|
|
|
|
dp_packet_shift(payload, (char *) dp_packet_base(payload) - (char *) dp_packet_data(payload));
|
2013-11-22 13:17:23 -08:00
|
|
|
|
|
2015-02-22 03:21:09 -08:00
|
|
|
|
dp_packet_put(payload, l7, l7_length);
|
2014-02-04 09:01:16 -08:00
|
|
|
|
stream->seq_no += l7_length;
|
2013-11-22 13:17:23 -08:00
|
|
|
|
return payload;
|
|
|
|
|
} else {
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|