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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
|
/*
* Mausezahn - A fast versatile traffic generator
* Copyright (C) 2008 Herbert Haas
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License version 2 as published by the
* Free Software Foundation.
*
* This program 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
* this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
*
*/
// ***************************************************************************
// This sections contains (as alternative to 'send_frame' in send.c)
// a layer-2 based flexible sending function.
//
// Layer-2 modifications such as 802.1Q and MPLS is considered here!
//
// ***************************************************************************
#include "mz.h"
libnet_ptag_t create_eth_frame (libnet_t *l, libnet_ptag_t t3, libnet_ptag_t t4)
{
libnet_t *L=NULL;
char errbuf[LIBNET_ERRBUF_SIZE];
libnet_ptag_t t=0, tmpls;
char argval[128];
u_int8_t et[2];
int et_len;
int i=0, j, mlen, mkomma, len, count, offset=0, found_colon=0;
char *left, *right;
char *f, mtag[64];
char verbose_mpls_string[128];
u_int8_t *packet;
u_int32_t packet_s;
char *saveptr, *ptrsubstring, substring[16], tmp[4*MAX_8021Q_TAGS];
u_int8_t CoS; // 0..7
u_int16_t vlan; // 12 bit value (0..4095)
u_int8_t dot1Q[4*MAX_8021Q_TAGS], *ptr;
u_int16_t dot1Q_eth_type=0x8100;
int bytecnt=0;
int isdot1Q, tcp_seq_delta, dp_isrange, sp_isrange, ip_dst_isrange, ip_src_isrange, eth_src_rand, eth_dst_rand, rtp_mode=0;
unsigned int delay;
////////////////////////////////////////////////////
// Prepare MPLS header if required
if (tx.mpls)
{
// first check how many labels have been specified:
mlen = strlen (tx.mpls_txt);
mkomma=0;
for (i=0; i<mlen; i++)
{
if (tx.mpls_txt[i]==',') mkomma++;
}
f = strtok_r (tx.mpls_txt, ",", &saveptr);
tx.mpls_bos=1;
do
{
strncpy(mtag, f, 64);
/*
if (mkomma==0)
{
tx.mpls_bos=0;
}
else
{
printf("BOS=1\n");
tx.mpls_bos=1;
}
*/
if ( get_mpls_params(mtag) ) // error?
{
fprintf(stderr," mz/get_mpls_params: MPLS Parameters problem.\n");
exit (0);
}
tmpls = libnet_build_mpls(tx.mpls_label,
tx.mpls_exp,
tx.mpls_bos,
tx.mpls_ttl,
NULL,
0,
l,
0);
if (tmpls == -1)
{
fprintf(stderr, " mz/create_ip_packet: Can't build MPLS header: %s\n", libnet_geterror(l));
exit (0);
}
if (verbose)
{
sprintf(verbose_mpls_string,"[%u:%u:%u:%u]",
tx.mpls_label,
tx.mpls_exp,
tx.mpls_bos,
tx.mpls_ttl);
strcat(tx.mpls_verbose_string, verbose_mpls_string);
strcat(tx.mpls_verbose_string, " ");
}
tx.mpls_bos=0;
mkomma--;
}
while ( (f=strtok_r(NULL, ",", &saveptr)) != NULL);
}
////////////////////////////////////////////////////////////////////////////////////////////
// Evaluate Ethernet CLI options (-a and -b)
if (check_eth_mac_txt(ETH_DST)) // if true then problem (invalid user input?)
{
str2hex("ff:ff:ff:ff:ff:ff",tx.eth_dst, 6); // the default
}
// if not specified then own MAC will be used automatically
(void) check_eth_mac_txt(ETH_SRC);
// Get CLI arguments:
// If NOT set, default: 0x800 or ETHERTYPE_MPLS if MPLS is used (see init.c)
if (getarg(tx.arg_string,"ether_type", argval)==1)
{
et_len = str2hex (argval, et, 2);
if (et_len==1)
tx.eth_type = et[0];
else // 2 bytes specified
tx.eth_type = 256 * et[0] + et[1];
//tx.eth_type = (u_int16_t) str2int(argval);
}
/////////////////////////////////////////////////////////////////////////////////
// Ethernet with 802.1Q
//
// If multiple 802.1Q tags are specified then we need to build the whole
// frame manually because libnet only supports adding a single VLAN header.
// The easiest solution is to create the hex-string of the 802.1Q-chain as
// u_int8_t QinQ[] then add the IP packet as payload...
//
if (tx.dot1Q) // actually contains the number of VLAN tags
{
// we want our own context!
L = libnet_init(LIBNET_LINK_ADV, tx.device, errbuf);
if (L == NULL)
{
fprintf(stderr, "%s", errbuf);
exit(EXIT_FAILURE);
}
strncpy(tmp,tx.dot1Q_txt,(4*MAX_8021Q_TAGS));
ptrsubstring = strtok_r(tmp, ",.", &saveptr);
bytecnt=0;
do
{
// make a local copy
strncpy(substring, ptrsubstring, 16);
CoS=0; vlan=0;
// Get CoS and VLAN ID from partial string
len = strlen(substring);
found_colon=0;
for (i=0; i<len; i++)
{
if (substring[i]==':') found_colon=1;
}
if (found_colon) // Both CoS and VLAN specified
{
left = strtok (substring, ":");
right = strtok (NULL, ":");
CoS = (u_int8_t) str2int (left);
vlan = (u_int16_t) str2int (right);
}
else // Only VLAN specified
{
vlan = (u_int16_t) str2int (substring);
}
if (CoS > 7)
{
fprintf(stderr, " mz/create_eth_frame: CoS too high, adjusted to 7\n");
CoS = 7;
}
if (vlan > 4095)
{
fprintf(stderr, " mz/create_eth_frame: VLAN number too high, adjusted to 4095\n");
vlan = 4095;
}
// create 4 byte 802.1Q header:
dot1Q[bytecnt+0]=0x81;
dot1Q[bytecnt+1]=0x00;
ptr = (u_int8_t*) &vlan;
dot1Q[bytecnt+3]=*ptr;
ptr++;
*ptr = *ptr ^ (CoS<<5); // add CoS
dot1Q[bytecnt+2]=*ptr;
//check:
//printf("%02x %02x %02x %02x\n",dot1Q[bytecnt+0],dot1Q[bytecnt+1],dot1Q[bytecnt+2],dot1Q[bytecnt+3]);
bytecnt+=4; // next tag (note that bytecnt will finally hold the number of used bytes!)
} while ( (ptrsubstring = strtok_r(NULL, ",.", &saveptr)) !=NULL); //get all VLAN tags
// now create the whole packet:
dot1Q_eth_type = 0x8100; //these are also the first two bytes of dot1Q[]
bytecnt = bytecnt-2;
for (i=0;i<bytecnt;i++)
{
tx.eth_payload[i]=dot1Q[i+2];
}
// now add official EtherType for the payload (this has been determined some lines above)
ptr = (u_int8_t*) & tx.eth_type;
tx.eth_payload[i+1]= *ptr;
ptr++;
tx.eth_payload[i]= *ptr;
offset = i+2;
// -
// --
// ---
// ---- now all 802.1Q headers are genereated ----
// ---- and are placed already in tx.eth_payload ----
// ---- (and 'i' points to the next free byte) ----
// ---
// --
// -
// Finally get all bytes of upper layers (IP packet and payload)
if (libnet_adv_cull_packet(l, &packet, &packet_s) == -1)
{
fprintf(stderr, "%s", libnet_geterror(l));
}
// Copy the upper layer data to the eth_payload
for (j=0; j<packet_s; j++)
{
tx.eth_payload[j+offset]=packet[j];
}
// 'libnet_adv_cull_packet' performs an implicit malloc() and a corresponding call
// to libnet_adv_free_packet() should be made to free the memory packet occupies:
libnet_adv_free_packet(l, packet);
tx.eth_payload_s = j+offset;
tx.eth_type = dot1Q_eth_type;
t = libnet_build_ethernet (tx.eth_dst,
tx.eth_src,
tx.eth_type,
tx.eth_payload,
tx.eth_payload_s,
L,
0);
if (t == -1)
{
fprintf(stderr, " mz/create_eth_frame: Can't build Ethernet header: %s\n",
libnet_geterror(l));
exit(EXIT_FAILURE);
}
// NOW the whole frame is ready to send!
}
else // normal Ethernet header without any 802.1Q-tag or MPLS-label
{
t = libnet_build_ethernet (tx.eth_dst,
tx.eth_src,
tx.eth_type,
NULL, // the payload
0,
l,
0);
if (t == -1)
{
fprintf(stderr, " mz/create_eth_frame: Can't build Ethernet header: %s\n",
libnet_geterror(l));
exit(EXIT_FAILURE);
}
}
/////////////////////////////////////////////////////////////////////////////
//
// Now send everything - maybe lots of times with modifications.
//
//
// local vars are faster :-)
count = tx.count;
delay = tx.delay;
eth_src_rand = tx.eth_src_rand;
eth_dst_rand = tx.eth_dst_rand;
tcp_seq_delta = tx.tcp_seq_delta;
dp_isrange = tx.dp_isrange;
sp_isrange = tx.sp_isrange;
ip_dst_isrange = tx.ip_dst_isrange;
ip_src_isrange = tx.ip_src_isrange | tx.ip_src_rand; // either case should call update_SA()
isdot1Q = tx.dot1Q;
if (mode == RTP) rtp_mode = 1;
if (count==0) goto AGAIN;
for (i=0; i<count; i++)
{
AGAIN:
if (isdot1Q)
{
// Get all bytes of upper layers (IP packet and payload)
if (libnet_adv_cull_packet(l, &packet, &packet_s) == -1)
{
fprintf(stderr, "%s", libnet_geterror(l));
}
// Copy the upper layer data to the eth_payload
for (j=0; j<packet_s; j++)
{
tx.eth_payload[j+offset]=packet[j];
}
// 'libnet_adv_cull_packet' performs an implicit malloc() and a corresponding call
// to libnet_adv_free_packet() should be made to free the memory packet occupies:
libnet_adv_free_packet(l, packet);
if (eth_dst_rand) rand_addr(tx.eth_dst);
if (eth_src_rand) rand_addr(tx.eth_src);
t = libnet_build_ethernet (tx.eth_dst,
tx.eth_src,
tx.eth_type,
tx.eth_payload,
tx.eth_payload_s,
L,
t);
if (t == -1)
{
fprintf(stderr, " mz/create_eth_frame: Can't build Ethernet header: %s\n",
libnet_geterror(l));
exit(EXIT_FAILURE);
}
if (verbose) (void) print_frame_details();
libnet_write(L);
}
else // No QinQ and/or MPLS modifications => use normal 'l' context:
{
if (eth_dst_rand) rand_addr(tx.eth_dst);
if (eth_src_rand) rand_addr(tx.eth_src);
t = libnet_build_ethernet (tx.eth_dst,
tx.eth_src,
tx.eth_type,
NULL,
0,
l,
t);
if (verbose) (void) print_frame_details();
libnet_write(l);
}
// if (verbose) (void) print_frame_details();
if (delay) SLEEP (delay);
if (tcp_seq_delta)
{
if (update_TCP_SQNR(l, t4)==0) // end of range not yet reached
{
goto AGAIN;
}
}
if (dp_isrange)
{
if (update_DPORT(l, t4)==0) // end of range not yet reached
{
goto AGAIN;
}
}
if (sp_isrange)
{
if (update_SPORT(l, t4)==0) // end of range not yet reached
{
goto AGAIN;
}
}
if (ip_dst_isrange)
{
if (update_IP_DA(l, t3)==0) // end of range not yet reached
{
goto AGAIN;
}
}
if (ip_src_isrange)
{
if (update_IP_SA(l, t3)==0) // end of range not yet reached
{
goto AGAIN;
}
}
if (rtp_mode) // update SQNR and Timestamps in RTP header and payload
{
update_RTP(l, t4);
}
if (!count) goto AGAIN;
}
libnet_destroy(l);
if (isdot1Q)
libnet_destroy(L);
return t;
}
void print_dot1Q_help(void)
{
fprintf(stderr,"\n"
MAUSEZAHN_VERSION
"\n"
"| 802.1Q header Syntax: -Q tag[,tag[,tag[,...]]]\n"
"| where each tag may consist of a CoS value using the syntax:\n"
"|\n"
"| <CoS>:<tag value>\n"
"|\n"
"| Examples:\n"
"|\n"
"| # mz -Q 100\n"
"| # mz -Q 5:100\n"
"| # mz -Q 5:100,200\n"
"| # mz -Q 5:100,7:200\n"
"| # mz -Q 100,200,300,5:400\n"
"\n\n");
exit(0);
}
|