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

Add option to turn on/off fix packet header length #846

6 changes: 6 additions & 0 deletions src/tcpedit/parse_args.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ tcpedit_post_args(tcpedit_t *tcpedit)
if (HAVE_OPT(FIXCSUM))
tcpedit->fixcsum = true;

/* --fixhdrlen */
if (HAVE_OPT(FIXHDRLEN))
tcpedit->fixhdrlen = true;
else
tcpedit->fixhdrlen = false;

/* --efcs */
if (HAVE_OPT(EFCS))
tcpedit->efcs = true;
Expand Down
21 changes: 15 additions & 6 deletions src/tcpedit/tcpedit.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
*/

#include "defines.h"

Check failure on line 21 in src/tcpedit/tcpedit.c

View workflow job for this annotation

GitHub Actions / cpp-linter

src/tcpedit/tcpedit.c:21:10 [clang-diagnostic-error]

'defines.h' file not found
#include "config.h"
#include "common.h"
#include "edit_packet.h"
Expand All @@ -44,7 +44,7 @@
* 1 on change
*/
int
tcpedit_packet(tcpedit_t *tcpedit, struct pcap_pkthdr **pkthdr, u_char **pktdata, tcpr_dir_t direction)

Check warning on line 47 in src/tcpedit/tcpedit.c

View workflow job for this annotation

GitHub Actions / cpp-linter

src/tcpedit/tcpedit.c:47:1 [readability-function-cognitive-complexity]

function 'tcpedit_packet' has cognitive complexity of 101 (threshold 25)
{
bool fuzz_once = tcpedit->fuzz_seed != 0;
ipv4_hdr_t *ip_hdr;
Expand Down Expand Up @@ -315,12 +315,21 @@
}
}

/* ensure IP header length is correct */
if (ip_hdr != NULL) {
fix_ipv4_length(*pkthdr, ip_hdr, l2len);
needtorecalc = 1;
} else if (ip6_hdr != NULL) {
needtorecalc |= fix_ipv6_length(*pkthdr, ip6_hdr, l2len);
/* fixhdrlen option ensure IP header length is correct */
/* do we need to fix checksums? -- must always do this last! */
if (tcpedit->fixhdrlen) {
/* ensure IP header length is correct */
int changed = 0;
if (ip_hdr != NULL) {
fix_ipv4_length(*pkthdr, ip_hdr, l2len);
changed = 1;
} else if (ip6_hdr != NULL) {
changed = fix_ipv6_length(*pkthdr, ip6_hdr, l2len);
}
/* did the packet change? then needtorecalc checksum */
if (changed > 0) {
needtorecalc |= changed;
}
}

/* do we need to fix checksums? -- must always do this last! */
Expand Down
11 changes: 11 additions & 0 deletions src/tcpedit/tcpedit_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
*/

#include "defines.h"

Check failure on line 21 in src/tcpedit/tcpedit_api.c

View workflow job for this annotation

GitHub Actions / cpp-linter

src/tcpedit/tcpedit_api.c:21:10 [clang-diagnostic-error]

'defines.h' file not found
#include "config.h"
#include "dlt_utils.h"
#include "portmap.h"
Expand Down Expand Up @@ -136,6 +136,17 @@
return TCPEDIT_OK;
}

/**
* \brief should we fix(?) header length?
*/
int
tcpedit_set_fixhdrlen(tcpedit_t *tcpedit, bool value)
{
assert(tcpedit);
tcpedit->fixhdrlen = value;
return TCPEDIT_OK;
}

/**
* \brief should we remove the EFCS from the frame?
*/
Expand Down
1 change: 1 addition & 0 deletions src/tcpedit/tcpedit_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,20 @@
* so when using the config API you must manually specify it using one of
* the following functions
*/
int tcpedit_set_encoder_dltplugin_byid(tcpedit_t *, int);

Check failure on line 32 in src/tcpedit/tcpedit_api.h

View workflow job for this annotation

GitHub Actions / cpp-linter

src/tcpedit/tcpedit_api.h:32:40 [clang-diagnostic-error]

unknown type name 'tcpedit_t'
int tcpedit_set_encoder_dltplugin_byname(tcpedit_t *, const char *);

Check failure on line 33 in src/tcpedit/tcpedit_api.h

View workflow job for this annotation

GitHub Actions / cpp-linter

src/tcpedit/tcpedit_api.h:33:42 [clang-diagnostic-error]

unknown type name 'tcpedit_t'

/**
* setters always return TCPEDIT_OK on success or TCPEDIT_ERROR
* if there is a problem. You can use tcpedit_geterr() to get the reason
* for the failure
*/
int tcpedit_set_skip_broadcast(tcpedit_t *, bool);

Check failure on line 40 in src/tcpedit/tcpedit_api.h

View workflow job for this annotation

GitHub Actions / cpp-linter

src/tcpedit/tcpedit_api.h:40:32 [clang-diagnostic-error]

unknown type name 'tcpedit_t'
int tcpedit_set_fixlen(tcpedit_t *, tcpedit_fixlen);

Check failure on line 41 in src/tcpedit/tcpedit_api.h

View workflow job for this annotation

GitHub Actions / cpp-linter

src/tcpedit/tcpedit_api.h:41:24 [clang-diagnostic-error]

unknown type name 'tcpedit_t'
int tcpedit_set_fixcsum(tcpedit_t *, bool);

Check failure on line 42 in src/tcpedit/tcpedit_api.h

View workflow job for this annotation

GitHub Actions / cpp-linter

src/tcpedit/tcpedit_api.h:42:25 [clang-diagnostic-error]

unknown type name 'tcpedit_t'
int tcpedit_set_fixhdrlen(tcpedit_t *, bool);

Check failure on line 43 in src/tcpedit/tcpedit_api.h

View workflow job for this annotation

GitHub Actions / cpp-linter

src/tcpedit/tcpedit_api.h:43:27 [clang-diagnostic-error]

unknown type name 'tcpedit_t'
int tcpedit_set_efcs(tcpedit_t *, bool);

Check failure on line 44 in src/tcpedit/tcpedit_api.h

View workflow job for this annotation

GitHub Actions / cpp-linter

src/tcpedit/tcpedit_api.h:44:22 [clang-diagnostic-error]

unknown type name 'tcpedit_t'
int tcpedit_set_ttl_mode(tcpedit_t *, tcpedit_ttl_mode);

Check failure on line 45 in src/tcpedit/tcpedit_api.h

View workflow job for this annotation

GitHub Actions / cpp-linter

src/tcpedit/tcpedit_api.h:45:26 [clang-diagnostic-error]

unknown type name 'tcpedit_t'
int tcpedit_set_ttl_value(tcpedit_t *, uint8_t);
int tcpedit_set_tos(tcpedit_t *, uint8_t);
int tcpedit_set_tclass(tcpedit_t *, uint8_t);
Expand Down
13 changes: 13 additions & 0 deletions src/tcpedit/tcpedit_opts.def
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,19 @@ fixed. Automatically enabled for packets modified with @samp{--seed},
EOText;
};

flag = {
name = fixhdrlen;
// value = C;
descrip = "fix IP/TCP header len";
doc = <<- EOText
By default, tcpreplay will send packets with the original packet length,
However, you may want the packet length revised to minimum packet size.
Using this option, tcpreplay will rewrite (fix) the packet length,
and recalculate checksums.
Caution: undesired packet changes may occur when this option is specified.
EOText;
};

flag = {
name = mtu;
value = m;
Expand Down
3 changes: 3 additions & 0 deletions src/tcpedit/tcpedit_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@ typedef struct {

uint32_t fuzz_seed;
uint32_t fuzz_factor;

/* fix header length */
bool fixhdrlen;
} tcpedit_t;

#ifdef __cplusplus
Expand Down
3 changes: 3 additions & 0 deletions src/tcpreplay_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ typedef struct tcpreplay_s {
uint32_t skip_packets;
bool first_time;

/* fix header length */
bool fixhdrlen;

/* counter stats */
tcpreplay_stats_t stats;
tcpreplay_stats_t static_stats; /* stats returned by tcpreplay_get_stats() */
Expand Down
Loading