Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature #853 direct traffic to pcap #871

Merged
merged 5 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ dnl $Id$
AC_PREREQ([2.69])

dnl Set version info here!
AC_INIT([tcpreplay],[4.5.0-beta2],[https://github.com/appneta/tcpreplay/issues],[tcpreplay],[http://tcpreplay.sourceforge.net/])
AC_INIT([tcpreplay],[4.5.0-beta3],[https://github.com/appneta/tcpreplay/issues],[tcpreplay],[http://tcpreplay.sourceforge.net/])
AC_CONFIG_SRCDIR([src/tcpreplay.c])
AC_CONFIG_HEADERS([src/config.h])
AC_CONFIG_AUX_DIR(config)
Expand Down
5 changes: 3 additions & 2 deletions docs/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
06/23/2024 Version 4.5.0-beta3
06/29/2024 Version 4.5.0-beta3
- tcpreplay --include / --exclude to control which packets are replayed (#884)
- add -w (--suppress-warnings) option to suppress warning messages (#878)
- add -W (--suppress-warnings) option to suppress warning messages (#878)
- AF_XDP compile issue due to merge issue (#876)
- memory leak in tcpprep when using include/exclude (#869)
- memory leak in tcpprep when using RegEx (#867)
- fix nanosecond timestamp regression bug (#863)
- autotools - AC_HELP_STRING is obsolete in 2.70 (#856)
- add -w output.pcap command line option to direct the output to a pcap (#853)
- configure.ac: do not run conftest in case of cross compilation (#849)
- Haiku support (#847)
- --fixhdrlen option added to control action on packet length changes (#846)
Expand Down
145 changes: 95 additions & 50 deletions src/common/sendpacket.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

/*
* Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
* Copyright (c) 2013-2024 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
* Copyright (c) 2013-2022 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
*
* The Tcpreplay Suite of tools is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License as
Expand All @@ -27,6 +27,7 @@
* injection method, then by all means add it here (and send me a patch).
*
* Anyways, long story short, for now the order of preference is:
* 0. pcap_dump
* 1. TX_RING
* 2. PF_PACKET
* 3. BPF
Expand Down Expand Up @@ -232,7 +233,8 @@
#undef INJECT_METHOD
#define INJECT_METHOD "xsk_ring_prod_submit()"
#endif
static sendpacket_t *sendpacket_open_pcap_dump(const char *, char *) _U_;
static void sendpacket_seterr(sendpacket_t *sp, const char *fmt, ...);

Check warning on line 237 in src/common/sendpacket.c

View workflow job for this annotation

GitHub Actions / cpp-linter

src/common/sendpacket.c:237:45 [readability-identifier-length]

parameter name 'sp' is too short, expected at least 3 characters
static sendpacket_t *sendpacket_open_khial(const char *, char *) _U_;
static struct tcpr_ether_addr *sendpacket_get_hwaddr_khial(sendpacket_t *) _U_;

Expand Down Expand Up @@ -457,7 +459,12 @@

break;

case SP_TYPE_LIBPCAP_DUMP:
pcap_dump((u_char *)sp->handle.dump.dump, pkthdr, data);
retcode = len;
break;

case SP_TYPE_NETMAP:

Check warning on line 467 in src/common/sendpacket.c

View workflow job for this annotation

GitHub Actions / cpp-linter

src/common/sendpacket.c:467:5 [bugprone-branch-clone]

switch has 2 consecutive identical branches
#ifdef HAVE_NETMAP
retcode = sendpacket_send_netmap(sp, data, len);

Expand Down Expand Up @@ -532,55 +539,57 @@

errbuf[0] = '\0';

/* khial is universal */
if (stat(device, &sdata) == 0) {
if (((sdata.st_mode & S_IFMT) == S_IFCHR)) {
sp = sendpacket_open_khial(device, errbuf);

} else {
switch (sdata.st_mode & S_IFMT) {
case S_IFBLK:
errx(-1, "\"%s\" is a block device and is not a valid Tcpreplay device", device);
case S_IFDIR:
errx(-1, "\"%s\" is a directory and is not a valid Tcpreplay device", device);
case S_IFIFO:
errx(-1, "\"%s\" is a FIFO and is not a valid Tcpreplay device", device);
case S_IFLNK:
errx(-1, "\"%s\" is a symbolic link and is not a valid Tcpreplay device", device);
case S_IFREG:
errx(-1, "\"%s\" is a file and is not a valid Tcpreplay device", device);
default:
errx(-1, "\"%s\" is not a valid Tcpreplay device", device);
if (sendpacket_type == SP_TYPE_LIBPCAP_DUMP) {
sp = sendpacket_open_pcap_dump(device, errbuf);

Check warning on line 543 in src/common/sendpacket.c

View workflow job for this annotation

GitHub Actions / cpp-linter

src/common/sendpacket.c:543:14 [performance-no-int-to-ptr]

integer to pointer cast pessimizes optimization opportunities
} else {
/* khial is universal */
if (stat(device, &sdata) == 0) {
if (((sdata.st_mode & S_IFMT) == S_IFCHR)) {
sp = sendpacket_open_khial(device, errbuf);

Check warning on line 548 in src/common/sendpacket.c

View workflow job for this annotation

GitHub Actions / cpp-linter

src/common/sendpacket.c:548:22 [performance-no-int-to-ptr]

integer to pointer cast pessimizes optimization opportunities

} else {
switch (sdata.st_mode & S_IFMT) {
case S_IFBLK:
errx(-1, "\"%s\" is a block device and is not a valid Tcpreplay device", device);
case S_IFDIR:
errx(-1, "\"%s\" is a directory and is not a valid Tcpreplay device", device);
case S_IFIFO:
errx(-1, "\"%s\" is a FIFO and is not a valid Tcpreplay device", device);
case S_IFLNK:
errx(-1, "\"%s\" is a symbolic link and is not a valid Tcpreplay device", device);
case S_IFREG:
errx(-1, "\"%s\" is a file and is not a valid Tcpreplay device", device);
default:
errx(-1, "\"%s\" is not a valid Tcpreplay device", device);
}
}
}
#ifdef HAVE_TUNTAP
} else if (strncmp(device, "tap", 3) == 0) {
sp = sendpacket_open_tuntap(device, errbuf);
} else if (strncmp(device, "tap", 3) == 0) {
sp = sendpacket_open_tuntap(device, errbuf);
#endif
} else {
} else {
#ifdef HAVE_NETMAP
if (sendpacket_type == SP_TYPE_NETMAP)
sp = (sendpacket_t *)sendpacket_open_netmap(device, errbuf, arg);
else
if (sendpacket_type == SP_TYPE_NETMAP)
sp = (sendpacket_t *)sendpacket_open_netmap(device, errbuf, arg);
else
#endif
#ifdef HAVE_LIBXDP
if (sendpacket_type == SP_TYPE_LIBXDP)
sp = sendpacket_open_xsk(device, errbuf);
else
if (sendpacket_type == SP_TYPE_LIBXDP)
sp = sendpacket_open_xsk(device, errbuf);
else
#endif
#if defined HAVE_PF_PACKET
sp = sendpacket_open_pf(device, errbuf);
sp = sendpacket_open_pf(device, errbuf);
#elif defined HAVE_BPF
sp = sendpacket_open_bpf(device, errbuf);
sp = sendpacket_open_bpf(device, errbuf);
#elif defined HAVE_LIBDNET
sp = sendpacket_open_libdnet(device, errbuf);
sp = sendpacket_open_libdnet(device, errbuf);
#elif (defined HAVE_PCAP_INJECT || defined HAVE_PCAP_SENDPACKET)
sp = sendpacket_open_pcap(device, errbuf);
#elif defined HAVE_LIBXDP
sp = sendpacket_open_xsk(device, errbuf);
sp = sendpacket_open_pcap(device, errbuf);
#else
#error "No defined packet injection method for sendpacket_open()"
#endif
}
}

if (sp) {
Expand Down Expand Up @@ -670,6 +679,11 @@
#endif
break;

case SP_TYPE_LIBPCAP_DUMP:
pcap_dump_close(sp->handle.dump.dump);
pcap_close(sp->handle.dump.pcap);
break;

case SP_TYPE_LIBDNET:
#ifdef HAVE_LIBDNET
eth_close(sp->handle.ldnet);
Expand Down Expand Up @@ -717,6 +731,9 @@

if (sp->handle_type == SP_TYPE_KHIAL) {
addr = sendpacket_get_hwaddr_khial(sp);
} else if (sp->handle_type == SP_TYPE_LIBPCAP_DUMP) {
sendpacket_seterr(sp, "Error: sendpacket_get_hwaddr() not yet supported for pcap dump");
return NULL;
} else {
#if defined HAVE_PF_PACKET
addr = sendpacket_get_hwaddr_pf(sp);
Expand Down Expand Up @@ -815,6 +832,37 @@
}
#endif /* HAVE_PCAP_INJECT || HAVE_PCAP_SENDPACKET */

/**
* Inner sendpacket_open() method for using libpcap
*/
static sendpacket_t *
sendpacket_open_pcap_dump(const char *device, char *errbuf)
{
pcap_t *pcap;
pcap_dumper_t* dump;
sendpacket_t *sp;

Check warning on line 843 in src/common/sendpacket.c

View workflow job for this annotation

GitHub Actions / cpp-linter

src/common/sendpacket.c:843:19 [cppcoreguidelines-init-variables]

variable 'sp' is not initialized

Check warning on line 843 in src/common/sendpacket.c

View workflow job for this annotation

GitHub Actions / cpp-linter

src/common/sendpacket.c:843:19 [readability-identifier-length]

variable name 'sp' is too short, expected at least 3 characters

assert(device);
assert(errbuf);

dbg(1, "sendpacket: using Libpcap");

pcap = pcap_open_dead(DLT_EN10MB, MAX_SNAPLEN);
if ((dump = pcap_dump_open(pcap, device)) == NULL){
char* err_msg = pcap_geterr(pcap);

Check warning on line 852 in src/common/sendpacket.c

View workflow job for this annotation

GitHub Actions / cpp-linter

src/common/sendpacket.c:852:15 [cppcoreguidelines-init-variables]

variable 'err_msg' is not initialized
strlcpy(errbuf, err_msg, PCAP_ERRBUF_SIZE);
pcap_close(pcap);
return NULL;
}

sp = (sendpacket_t *)safe_malloc(sizeof(sendpacket_t));

Check warning on line 858 in src/common/sendpacket.c

View workflow job for this annotation

GitHub Actions / cpp-linter

src/common/sendpacket.c:858:10 [performance-no-int-to-ptr]

integer to pointer cast pessimizes optimization opportunities
strlcpy(sp->device, device, sizeof(sp->device));
sp->handle.dump.pcap = pcap;
sp->handle.dump.dump = dump;
sp->handle_type = SP_TYPE_LIBPCAP_DUMP;
return sp;
}

#if defined HAVE_LIBDNET && !defined HAVE_PF_PACKET && !defined HAVE_BPF
/**
* Inner sendpacket_open() method for using libdnet
Expand Down Expand Up @@ -886,12 +934,9 @@
strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name) - 1);

if (ioctl(tapfd, TUNSETIFF, (void *)&ifr) < 0) {
// ignore EBUSY - it just means that the tunnel has already been opened
if (errno != EBUSY) {
snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to create tuntap interface: %s errno=%d", device, errno);
close(tapfd);
return NULL;
}
snprintf(errbuf, SENDPACKET_ERRBUF_SIZE, "Unable to create tuntap interface: %s", device);
close(tapfd);
return NULL;
}
#elif defined(HAVE_FREEBSD)
if (*device == '/') {
Expand Down Expand Up @@ -1255,14 +1300,14 @@
int dlt = DLT_EN10MB;

switch (sp->handle_type) {
case SP_TYPE_KHIAL:
case SP_TYPE_NETMAP:
case SP_TYPE_TUNTAP:
case SP_TYPE_LIBXDP:
/* always EN10MB */
return dlt;
default:
;
case SP_TYPE_KHIAL:
case SP_TYPE_NETMAP:
case SP_TYPE_TUNTAP:
case SP_TYPE_LIBXDP:
case SP_TYPE_LIBPCAP_DUMP:
/* always EN10MB */
return dlt;
default:;
}

#if defined HAVE_BPF
Expand Down
7 changes: 7 additions & 0 deletions src/common/sendpacket.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

#pragma once

#include "defines.h"

Check failure on line 23 in src/common/sendpacket.h

View workflow job for this annotation

GitHub Actions / cpp-linter

src/common/sendpacket.h:23:10 [clang-diagnostic-error]

'defines.h' file not found
#include "config.h"
#include <sys/socket.h>

Expand Down Expand Up @@ -68,6 +68,7 @@
SP_TYPE_KHIAL,
SP_TYPE_NETMAP,
SP_TYPE_TUNTAP,
SP_TYPE_LIBPCAP_DUMP,
SP_TYPE_LIBXDP
} sendpacket_type_t;

Expand All @@ -81,8 +82,14 @@
KHIAL_DIRECTION_TX,
} khial_direction_t;

typedef struct pcap_dump_s{
pcap_t *pcap;
pcap_dumper_t* dump;
} pcap_dump_t;

union sendpacket_handle {
pcap_t *pcap;
pcap_dump_t dump;
int fd;
#ifdef HAVE_LIBDNET
eth_t *ldnet;
Expand Down
Loading
Loading