Skip to content

Commit d275b65

Browse files
committed
Merge branch 'Enhancement_#425_edit_tcp_seq-ack_numbers_staging' of github.com:appneta/tcpreplay into Enhancement_#425_edit_tcp_seq-ack_numbers_staging
* 'Enhancement_#425_edit_tcp_seq-ack_numbers_staging' of github.com:appneta/tcpreplay: show test log on test fail in CI Add ability to change tcp SEQ/ACK numbers Conflicts: test/Makefile.am
2 parents 4b96ddd + 204d8ef commit d275b65

File tree

10 files changed

+165
-8
lines changed

10 files changed

+165
-8
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ script:
3333
- ${CI_BUILD_PREFIX} ./configure --disable-local-libopts > build.log 2>&1 || (cat build.log && exit 1)
3434
- ${CI_BUILD_PREFIX} make > build.log 2>&1 || (cat build.log && exit 1)
3535
- make dist > build.log 2>&1 || (cat build.log && exit 1)
36-
- sudo make test
36+
- sudo make test || (cat test/test.log && exit 1)

src/tcpedit/Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ BUILT_SOURCES = tcpedit_stub.h
77

88
libtcpedit_a_SOURCES = tcpedit.c parse_args.c edit_packet.c \
99
portmap.c dlt.c checksum.c incremental_checksum.c \
10-
tcpedit_api.c fuzzing.c
10+
tcpedit_api.c fuzzing.c rewrite_sequence.c
1111

1212
manpages: tcpedit.1
1313

@@ -28,7 +28,7 @@ noinst_HEADERS = tcpedit.h edit_packet.h portmap.h \
2828
tcpedit_stub.h parse_args.h dlt.h checksum.h \
2929
incremental_checksum.h tcpedit_api.h \
3030
tcpedit_types.h plugins.h plugins_api.h \
31-
plugins_types.h fuzzing.h
31+
plugins_types.h fuzzing.h rewrite_sequence.h
3232

3333
MOSTLYCLEANFILES = *~
3434

src/tcpedit/parse_args.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ tcpedit_post_args(tcpedit_t *tcpedit) {
167167
}
168168
}
169169

170+
/* --rewrite-sequence */
171+
if (HAVE_OPT(REWRITE_SEQUENCE))
172+
tcpedit->rewrite_sequence = 1;
173+
else
174+
tcpedit->rewrite_sequence = 0;
175+
170176
/* TCP/UDP port rewriting */
171177
if (HAVE_OPT(PORTMAP)) {
172178
int ct = STACKCT_OPT(PORTMAP);

src/tcpedit/rewrite_sequence.c

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* $Id$ */
2+
3+
/*
4+
* Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
5+
* Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
6+
* Copyright (c) 2017 Mario D. Santana <tcpreplay at elorangutan dot com> - El Orangutan
7+
*
8+
* The Tcpreplay Suite of tools is free software: you can redistribute it
9+
* and/or modify it under the terms of the GNU General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or with the authors permission any later version.
12+
*
13+
* The Tcpreplay Suite is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
/*
23+
* This file contains routines to manipulate port maps, in which
24+
* one port number is mapped to another.
25+
*/
26+
#include "config.h"
27+
#include "defines.h"
28+
#include "common.h"
29+
30+
#include <stdlib.h>
31+
#include <string.h>
32+
#include <errno.h>
33+
34+
#include "tcpreplay.h"
35+
#include "tcpedit.h"
36+
#include "rewrite_sequence.h"
37+
#include "incremental_checksum.h"
38+
39+
40+
/**
41+
* rewrites the TCP sequence and ack numbers
42+
* returns 1 for changes made or 0 for none
43+
*/
44+
45+
static int
46+
rewrite_seqs(tcpedit_t *tcpedit, tcp_hdr_t *tcp_hdr)
47+
{
48+
uint32_t newnum;
49+
50+
while (tcpedit->rewrite_sequence == 1)
51+
tcpedit->rewrite_sequence = rand() * (4294967296 / RAND_MAX);
52+
newnum = tcp_hdr->th_seq + tcpedit->rewrite_sequence;
53+
csum_replace4(&tcp_hdr->th_sum, tcp_hdr->th_seq, newnum);
54+
tcp_hdr->th_seq = newnum;
55+
newnum = tcp_hdr->th_ack + tcpedit->rewrite_sequence;
56+
csum_replace4(&tcp_hdr->th_sum, tcp_hdr->th_ack, newnum);
57+
tcp_hdr->th_ack = newnum;
58+
return 0;
59+
}
60+
61+
62+
int
63+
rewrite_ipv4_sequence(tcpedit_t *tcpedit, ipv4_hdr_t **ip_hdr)
64+
{
65+
assert(tcpedit);
66+
tcp_hdr_t *tcp_hdr = NULL;
67+
68+
if (*ip_hdr && (*ip_hdr)->ip_p == IPPROTO_TCP) {
69+
tcp_hdr = (tcp_hdr_t *)get_layer4_v4(*ip_hdr, 65536);
70+
return rewrite_seqs(tcpedit, tcp_hdr);
71+
}
72+
return 0;
73+
}
74+
75+
int
76+
rewrite_ipv6_sequence(tcpedit_t *tcpedit, ipv6_hdr_t **ip6_hdr)
77+
{
78+
assert(tcpedit);
79+
tcp_hdr_t *tcp_hdr = NULL;
80+
81+
if (*ip6_hdr && (*ip6_hdr)->ip_nh == IPPROTO_TCP) {
82+
tcp_hdr = (tcp_hdr_t *)get_layer4_v6(*ip6_hdr, 65535);
83+
return rewrite_seqs(tcpedit, tcp_hdr);
84+
}
85+
return 0;
86+
}

src/tcpedit/rewrite_sequence.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* $Id$ */
2+
3+
/*
4+
* Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
5+
* Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
6+
* Copyright (c) 2017 Mario D. Santana <tcpreplay at elorangutan dot com> - El Orangutan
7+
*
8+
* The Tcpreplay Suite of tools is free software: you can redistribute it
9+
* and/or modify it under the terms of the GNU General Public License as
10+
* published by the Free Software Foundation, either version 3 of the
11+
* License, or with the authors permission any later version.
12+
*
13+
* The Tcpreplay Suite is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16+
* GNU General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU General Public License
19+
* along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
20+
*/
21+
22+
#include "tcpedit_types.h"
23+
24+
#ifndef __REWRITE_SEQUENCE_H__
25+
#define __REWRITE_SEQUENCE_H__
26+
27+
int rewrite_ipv4_sequence(tcpedit_t *tcpedit, ipv4_hdr_t **ip_hdr);
28+
int rewrite_ipv6_sequence(tcpedit_t *tcpedit, ipv6_hdr_t **ip_hdr);
29+
30+
int rewrite_ipv4_sequence(tcpedit_t *tcpedit, ipv4_hdr_t **ip_hdr);
31+
int rewrite_ipv6_sequence(tcpedit_t *tcpedit, ipv6_hdr_t **ip6_hdr);
32+
33+
#endif

src/tcpedit/tcpedit.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,9 @@ tcpedit_packet(tcpedit_t *tcpedit, struct pcap_pkthdr **pkthdr,
187187
if ((retval = rewrite_ipv4_ports(tcpedit, &ip_hdr, (*pkthdr)->caplen)) < 0)
188188
return TCPEDIT_ERROR;
189189
}
190+
191+
if (tcpedit->rewrite_sequence)
192+
rewrite_ipv4_sequence(tcpedit, &ip_hdr);
190193
}
191194
/* IPv6 edits */
192195
else if (ip6_hdr != NULL) {
@@ -224,6 +227,9 @@ tcpedit_packet(tcpedit_t *tcpedit, struct pcap_pkthdr **pkthdr,
224227
if ((retval = rewrite_ipv6_ports(tcpedit, &ip6_hdr, (*pkthdr)->caplen)) < 0)
225228
return TCPEDIT_ERROR;
226229
}
230+
231+
if (tcpedit->rewrite_sequence)
232+
rewrite_ipv6_sequence(tcpedit, &ip_hdr);
227233
}
228234

229235
if (tcpedit->fuzz_seed != 0) {

src/tcpedit/tcpedit_opts.def

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,16 @@ Examples:
4343
EOText;
4444
};
4545

46+
flag = {
47+
name = rewrite-sequence;
48+
value = n;
49+
descrip = "Change TCP sequence (and ACK) numbers";
50+
doc = <<- EOText
51+
Change all TCP sequence numbers, and related sequence-acknowledgement numbers.
52+
They will be shifted by a random amount between 1 and 2^32.
53+
EOText;
54+
};
55+
4656
flag = {
4757
name = seed;
4858
flags-cant = fuzz-seed;

src/tcpedit/tcpedit_types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ typedef struct {
119119

120120
/* rewrite ip? */
121121
bool rewrite_ip;
122+
123+
/* rewrite seq/ack numbers? */
124+
u_int32_t rewrite_sequence;
122125

123126
/* fix IP/TCP/UDP checksums */
124127
bool fixcsum;

test/Makefile.am

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ EXTRA_DIST = test.pcap test.auto_bridge test.auto_client test.auto_router \
2323
test.rewrite_skip test.rewrite_dltuser test.rewrite_dlthdlc \
2424
test.rewrite_vlandel test.rewrite_efcs test.rewrite_1ttl \
2525
test.rewrite_2ttl test.rewrite_3ttl test.rewrite_enet_subsmac \
26-
test.rewrite_mtutrunc test.rewrite_mac_seed test.rewrite_range_portmap \
27-
test.rewrite_mac_seed_keep test.rewrite_l7fuzzing\
26+
test.rewrite_mtutrunc test.rewrite_mac_seed seed test.rewrite_range_portmap \
27+
test.rewrite_mac_seed_keep test.rewrite_l7fuzzing test.rewrite_sequence \
2828
test2.rewrite_seed test2.rewrite_portmap test2.rewrite_endpoint \
2929
test2.rewrite_pnat test2.rewrite_pad test2.rewrite_trunc \
3030
test2.rewrite_mac test2.rewrite_layer2 test2.rewrite_config \
@@ -34,7 +34,7 @@ EXTRA_DIST = test.pcap test.auto_bridge test.auto_client test.auto_router \
3434
test2.rewrite_2ttl test2.rewrite_3ttl test.rewrite_tos test2.rewrite_tos \
3535
test2.rewrite_enet_subsmac test2.rewrite_mac_seed \
3636
test2.rewrite_range_portmap test2.rewrite_mac_seed_keep \
37-
test2.rewrite_l7fuzzing
37+
test2.rewrite_l7fuzzing test2.rewrite_sequence
3838

3939
test: all
4040
all: clearlog check tcpprep tcpreplay tcprewrite
@@ -78,6 +78,7 @@ standard_bigendian:
7878
$(TCPREWRITE) -i test.pcap -o test.rewrite_tos --tos=50
7979
$(TCPREWRITE) -i test.pcap -o test.rewrite_portmap -r 80:8080
8080
$(TCPREWRITE) -i test.pcap -o test.rewrite_range_portmap -r 1-100:49148
81+
$(TCPREWRITE) -i test.pcap -o test.rewrite_sequence -n
8182
$(TCPREWRITE) -i test.pcap -o test.rewrite_endpoint \
8283
-e 10.10.0.1:10.10.0.2 -c test.auto_router
8384
$(TCPREWRITE) -i test.pcap -o test.rewrite_pnat \
@@ -118,7 +119,8 @@ standard_littleendian:
118119
$(TCPREWRITE) -i test.pcap -o test2.rewrite_seed -s 55
119120
$(TCPREWRITE) -i test.pcap -o test2.rewrite_tos --tos=50
120121
$(TCPREWRITE) -i test.pcap -o test2.rewrite_portmap -r 80:8080
121-
$(TCPREWRITE) -i test.pcap -o test2.rewrite_range_portmap -r 1-100:49148
122+
$(TCPREWRITE) -i test.pcap -o test2.rewrite_range_portmap -r 1-100:49148
123+
$(TCPREWRITE) -i test.pcap -o test2.rewrite_sequence -n
122124
$(TCPREWRITE) -i test.pcap -o test2.rewrite_endpoint \
123125
-e 10.10.0.1:10.10.0.2 -c test.auto_router
124126
$(TCPREWRITE) -i test.pcap -o test2.rewrite_pnat \
@@ -164,7 +166,7 @@ tcprewrite: rewrite_portmap rewrite_range_portmap rewrite_endpoint \
164166
rewrite_layer2 rewrite_config rewrite_skip rewrite_dltuser rewrite_dlthdlc \
165167
rewrite_vlandel rewrite_efcs rewrite_1ttl rewrite_2ttl rewrite_3ttl \
166168
rewrite_tos rewrite_mtutrunc rewrite_enet_subsmac rewrite_mac_seed \
167-
rewrite_mac_seed_keep rewrite_l7fuzzing
169+
rewrite_mac_seed_keep rewrite_l7fuzzing rewrite_sequence
168170

169171
tcpreplay: replay_basic replay_cache replay_pps replay_rate replay_top \
170172
replay_config replay_multi replay_pps_multi replay_precache \
@@ -326,6 +328,17 @@ else
326328
endif
327329
if [ $? ] ; then $(PRINTF) "\t\t\t%s\n" "FAILED"; else $(PRINTF) "\t\t%s\n" "OK"; fi
328330

331+
rewrite_sequence:
332+
$(PRINTF) "%s" "[tcprewrite] Resequence test: "
333+
$(PRINTF) "%s\n" "*** [tcprewrite] Resequence test: " >>test.log
334+
$(TCPREWRITE) $(ENABLE_DEBUG) -i test.pcap -o test.$@1 -n >>test.log 2>&1
335+
if WORDS_BIGENDIAN
336+
diff test.$@ test.$@1 >>test.log 2>&1
337+
else
338+
diff test2.$@ test.$@1 >>test.log 2>&1
339+
endif
340+
if [ $? ] ; then $(PRINTF) "\t\t\t%s\n" "FAILED"; else $(PRINTF) "\t\t\t%s\n" "OK"; fi
341+
329342
rewrite_endpoint:
330343
$(PRINTF) "%s" "[tcprewrite] Endpoint test: "
331344
$(PRINTF) "%s\n" "*** [tcprewrite] Endpoint test: " >>test.log

test/test2.rewrite_sequence

63.5 KB
Binary file not shown.

0 commit comments

Comments
 (0)