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
rewrite_seqs(tcpedit_t * tcpedit,tcp_hdr_t * tcp_hdr)46 rewrite_seqs(tcpedit_t *tcpedit, tcp_hdr_t *tcp_hdr)
47 {
48 volatile uint32_t newnum;
49
50 newnum = ntohl(tcp_hdr->th_seq) + tcpedit->tcp_sequence_adjust;
51 csum_replace4(&tcp_hdr->th_sum, tcp_hdr->th_seq, htonl(newnum));
52 tcp_hdr->th_seq = htonl(newnum);
53
54 /* first packet of 3-way handshake must have an ACK of zero - #450 */
55 if (!((tcp_hdr->th_flags & TH_SYN) && !(tcp_hdr->th_flags & TH_ACK))) {
56 newnum = ntohl(tcp_hdr->th_ack) + tcpedit->tcp_sequence_adjust;
57 csum_replace4(&tcp_hdr->th_sum, tcp_hdr->th_ack, htonl(newnum));
58 tcp_hdr->th_ack = htonl(newnum);
59 }
60
61 return 0;
62 }
63
64
65 int
rewrite_ipv4_tcp_sequence(tcpedit_t * tcpedit,ipv4_hdr_t ** ip_hdr,const int l3len)66 rewrite_ipv4_tcp_sequence(tcpedit_t *tcpedit, ipv4_hdr_t **ip_hdr,
67 const int l3len)
68 {
69 assert(tcpedit);
70 assert(*ip_hdr && ip_hdr);
71
72 if (*ip_hdr && (*ip_hdr)->ip_p == IPPROTO_TCP) {
73 tcp_hdr_t *tcp_hdr = (tcp_hdr_t *)get_layer4_v4(*ip_hdr, l3len);
74 if (!tcp_hdr) {
75 tcpedit_setwarn(tcpedit, "caplen to small to set TCP sequence for IP packet: l3 len=%d",
76 l3len);
77 return TCPEDIT_WARN;
78 }
79
80 return rewrite_seqs(tcpedit, tcp_hdr);
81 }
82
83 return 0;
84 }
85
86 int
rewrite_ipv6_tcp_sequence(tcpedit_t * tcpedit,ipv6_hdr_t ** ip6_hdr,const int l3len)87 rewrite_ipv6_tcp_sequence(tcpedit_t *tcpedit, ipv6_hdr_t **ip6_hdr,
88 const int l3len)
89 {
90 assert(tcpedit);
91 assert(*ip6_hdr && ip6_hdr);
92
93 if (*ip6_hdr && (*ip6_hdr)->ip_nh == IPPROTO_TCP) {
94 tcp_hdr_t *tcp_hdr = (tcp_hdr_t *)get_layer4_v6(*ip6_hdr, l3len);
95 if (!tcp_hdr) {
96 tcpedit_setwarn(tcpedit, "caplen to small to set TCP sequence for IP packet: l3 len=%d",
97 l3len);
98 return TCPEDIT_WARN;
99 }
100
101 return rewrite_seqs(tcpedit, tcp_hdr);
102 }
103
104 return 0;
105 }
106