forked from appneta/tcpreplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcpedit_api.c
342 lines (298 loc) · 7.63 KB
/
tcpedit_api.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/* $Id$ */
/*
* Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
* 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
* published by the Free Software Foundation, either version 3 of the
* License, or with the authors permission any later version.
*
* The Tcpreplay Suite is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
*/
#include "defines.h"
#include "config.h"
#include "dlt_utils.h"
#include "portmap.h"
#include "tcpedit.h"
#include <sys/types.h>
/**
* Set output DLT plugin by using it's DLT_<type>. Note that the user plugin
* is DLT_USER0.
*/
int
tcpedit_set_encoder_dltplugin_byid(tcpedit_t *tcpedit, int dlt)
{
tcpeditdlt_plugin_t *plugin;
tcpeditdlt_t *ctx;
assert(tcpedit);
ctx = tcpedit->dlt_ctx;
assert(ctx);
if (ctx->encoder) {
tcpedit_seterr(tcpedit, "You have already selected a DLT encoder: %s", ctx->encoder->name);
return TCPEDIT_ERROR;
}
plugin = tcpedit_dlt_getplugin(ctx, dlt);
if (plugin == NULL) {
tcpedit_seterr(tcpedit, "No output DLT plugin decoder with DLT type: 0x%04x", dlt);
return TCPEDIT_ERROR;
}
ctx->encoder = plugin;
/* init the encoder plugin if it's not the decoder plugin too */
if (ctx->encoder->dlt != ctx->decoder->dlt) {
if (ctx->encoder->plugin_init(ctx) != TCPEDIT_OK) {
/* plugin should generate the error */
return TCPEDIT_ERROR;
}
}
return TCPEDIT_OK;
}
/**
* same as tcpedit_set_encoder_plugin_byid() except we take the DLT_<name>
* as a string to select it
*/
int
tcpedit_set_encoder_dltplugin_byname(tcpedit_t *tcpedit, const char *name)
{
tcpeditdlt_plugin_t *plugin;
tcpeditdlt_t *ctx;
assert(tcpedit);
ctx = tcpedit->dlt_ctx;
assert(ctx);
if (ctx->encoder) {
tcpedit_seterr(tcpedit, "You have already selected a DLT encoder: %s", ctx->encoder->name);
return TCPEDIT_ERROR;
}
plugin = tcpedit_dlt_getplugin_byname(ctx, name);
if (plugin == NULL) {
tcpedit_seterr(tcpedit, "No output DLT plugin available for: %s", name);
return TCPEDIT_ERROR;
}
ctx->encoder = plugin;
/* init the encoder plugin if it's not the decoder plugin too */
if (ctx->encoder->dlt != ctx->decoder->dlt) {
if (ctx->encoder->plugin_init(ctx) != TCPEDIT_OK) {
/* plugin should generate the error */
return TCPEDIT_ERROR;
}
}
return TCPEDIT_OK;
}
/**
* Set whether we should edit broadcast & multicast IP addresses
*/
int
tcpedit_set_skip_broadcast(tcpedit_t *tcpedit, bool value)
{
assert(tcpedit);
tcpedit->skip_broadcast = value;
return TCPEDIT_OK;
}
/**
* \brief force fixing L3 & L4 data by padding or truncating packets
*/
int
tcpedit_set_fixlen(tcpedit_t *tcpedit, tcpedit_fixlen value)
{
assert(tcpedit);
tcpedit->fixlen = value;
return TCPEDIT_OK;
}
/**
* \brief should we always recalculate L3 & L4 checksums?
*/
int
tcpedit_set_fixcsum(tcpedit_t *tcpedit, bool value)
{
assert(tcpedit);
tcpedit->fixcsum = value;
return TCPEDIT_OK;
}
/**
* \brief should we remove the EFCS from the frame?
*/
int
tcpedit_set_efcs(tcpedit_t *tcpedit, bool value)
{
assert(tcpedit);
tcpedit->efcs = value;
return TCPEDIT_OK;
}
/**
* \brief set the IPv4 TTL mode
*/
int
tcpedit_set_ttl_mode(tcpedit_t *tcpedit, tcpedit_ttl_mode value)
{
assert(tcpedit);
tcpedit->ttl_mode = value;
return TCPEDIT_OK;
}
/**
* \brief set the IPv4 ttl value
*/
int
tcpedit_set_ttl_value(tcpedit_t *tcpedit, uint8_t value)
{
assert(tcpedit);
tcpedit->ttl_value = value;
return TCPEDIT_OK;
}
/**
* \brief set the IPv4 TOS/DiffServ/ECN byte value
*/
int
tcpedit_set_tos(tcpedit_t *tcpedit, uint8_t value)
{
assert(tcpedit);
tcpedit->tos = value;
return TCPEDIT_OK;
}
/**
* \brief set the IPv6 Traffic Class byte value
*/
int
tcpedit_set_tclass(tcpedit_t *tcpedit, uint8_t value)
{
assert(tcpedit);
tcpedit->tclass = value;
return TCPEDIT_OK;
}
/**
* \brief set the IPv6 Flow Label 20bit value
*/
int
tcpedit_set_flowlabel(tcpedit_t *tcpedit, uint32_t value)
{
assert(tcpedit);
tcpedit->flowlabel = (int)value;
return TCPEDIT_OK;
}
/**
* Set the IPv4 IP address randomization seed
*/
int
tcpedit_set_seed(tcpedit_t *tcpedit)
{
assert(tcpedit);
tcpedit->rewrite_ip = true;
tcpedit->seed = 1;
tcpedit->seed = tcpr_random(&tcpedit->seed);
return TCPEDIT_OK;
}
/**
* Set the MTU of the frames
*/
int
tcpedit_set_mtu(tcpedit_t *tcpedit, int value)
{
assert(tcpedit);
tcpedit->mtu = value;
return TCPEDIT_OK;
}
/**
* Enable truncating packets to the MTU length
*/
int
tcpedit_set_mtu_truncate(tcpedit_t *tcpedit, bool value)
{
assert(tcpedit);
tcpedit->mtu_truncate = value;
return TCPEDIT_OK;
}
/**
* Set the maxpacket- currently not supported
*/
int
tcpedit_set_maxpacket(tcpedit_t *tcpedit, int value)
{
assert(tcpedit);
tcpedit->maxpacket = value;
return TCPEDIT_OK;
}
/**
* \brief Set the server to client (primary) CIDR map (Pseudo NAT)
*
* Set the server to client (primary) CIDR map using the given string
* which is in the format of:
* <match cidr>:<target cidr>,...
* 192.168.0.0/16:10.77.0.0/16,172.16.0.0/12:10.1.0.0/24
*/
int
tcpedit_set_cidrmap_s2c(tcpedit_t *tcpedit, char *value)
{
assert(tcpedit);
tcpedit->rewrite_ip = true;
if (!parse_cidr_map(&tcpedit->cidrmap1, value)) {
tcpedit_seterr(tcpedit, "Unable to parse: %s", value);
return TCPEDIT_ERROR;
}
return TCPEDIT_OK;
}
/**
* \brief Set the client to server (secondary) CIDR map (Pseudo NAT)
*
* Set the client to server (secondary) CIDR map using the given string
* which is in the format of:
* <match cidr>:<target cidr>,...
* 192.168.0.0/16:10.77.0.0/16,172.16.0.0/12:10.1.0.0/24
*/
int
tcpedit_set_cidrmap_c2s(tcpedit_t *tcpedit, char *value)
{
assert(tcpedit);
tcpedit->rewrite_ip = true;
if (!parse_cidr_map(&tcpedit->cidrmap2, value)) {
tcpedit_seterr(tcpedit, "Unable to parse: %s", value);
return TCPEDIT_ERROR;
}
return TCPEDIT_OK;
}
/**
* Rewrite the Source IP of any packet
*/
int
tcpedit_set_srcip_map(tcpedit_t *tcpedit, char *value)
{
assert(tcpedit);
tcpedit->rewrite_ip = true;
if (!parse_cidr_map(&tcpedit->srcipmap, value)) {
tcpedit_seterr(tcpedit, "Unable to parse source ip map: %s", value);
return TCPEDIT_ERROR;
}
return TCPEDIT_OK;
}
/**
* Rewrite the Destination IP of any packet
*/
int
tcpedit_set_dstip_map(tcpedit_t *tcpedit, char *value)
{
assert(tcpedit);
tcpedit->rewrite_ip = true;
if (!parse_cidr_map(&tcpedit->dstipmap, value)) {
tcpedit_seterr(tcpedit, "Unable to parse destination ip map: %s", value);
return TCPEDIT_ERROR;
}
return TCPEDIT_OK;
}
/**
* Rewrite TCP/UDP ports using the following format:
* <src>:<dst>,...
*/
int
tcpedit_set_port_map(tcpedit_t *tcpedit, char *value)
{
assert(tcpedit);
if (!parse_portmap(&tcpedit->portmap, value)) {
tcpedit_seterr(tcpedit, "Unable to parse portmap: %s", value);
return TCPEDIT_ERROR;
}
return TCPEDIT_OK;
}