2 * Mausezahn - A fast versatile traffic generator
3 * Copyright (C) 2008 Herbert Haas
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License version 2 as published by the
7 * Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, see http://www.gnu.org/licenses/gpl-2.0.html
21 // ****************************************************************************
23 // This section contains functions to send an arbitrary byte stream out of
24 // the network card. Currently it works perfect for Ethernet cards.
26 // TODO: Access to the 802.11 header
28 // ****************************************************************************
36 // 1. Check 'eth_src_txt' and 'eth_dst_txt' which contain either a MAC address or a keyword
37 // 'eth_dst' can be set without having 'eth_src_txt' specified (the next 6 bytes of the
38 // 'arg_string' will be used). But if 'eth_src_txt' is given then also 'eth_dst_txt'
39 // should have been specified, otherwise a default (ff-ff-ff-ff-ff-ff) will be used.
40 // 2. Check whether 'arg_string' contains a hex-string. If YES then convert it into an
41 // 'eth_payload' and extract eth_type.
42 // 3. Apply 'padding' if specified
43 // 4. Check if frame has at least minimum length (14 Bytes).
44 // 5. Send frame 'count' times and
45 // 6. Apply 'delay' (make precautions for better delay functions)
48 src
, // flag telling whether user has specified a source address
49 dst
, // flag telling whether user has specified a destination address
60 err_buf
[LIBNET_ERRBUF_SIZE
],
61 message
[MAX_PAYLOAD_SIZE
*3];
63 u_int8_t bytestring
[MAX_PAYLOAD_SIZE
];
71 fprintf(stderr
," Note: raw layer 2 mode does not support 802.1Q builder.\n"
72 " If you want to create VLAN tags then you must do it by hand.\n");
78 fprintf(stderr
," Note: raw layer 2 mode does not support MPLS builder.\n"
79 " If you want to create MPLS labels then you must do it by hand.\n");
86 // So other functions can use this function for sending Ethernet frames
87 // These other functions must set dst, src, type and payload!
88 if (tx
.eth_params_already_set
) goto ALL_SPECIFIED
;
91 if ((tx
.padding
) && (tx
.padding
<15)) // Note: ignored if padding==0
95 cli_print(gcli
, "Note: Set padding to 15 bytes (total length)\n");
97 fprintf(stderr
, " mz/send_eth: [Note] adjusted minimum frame size to 15 bytes.\n");
100 // Create a temporal, local bytestring:
102 for (i
=0; i
<MAX_PAYLOAD_SIZE
; i
++) bytestring
[i
]=0x00;
103 bytestring_s
= str2hex (tx
.arg_string
, bytestring
, MAX_PAYLOAD_SIZE
);
105 // Set the flags to shorten subsequent decisions:
106 src
= strlen(tx
.eth_src_txt
);
107 dst
= strlen(tx
.eth_dst_txt
);
110 // IN ANY CASE if src has been specified:
114 // Evaluate Ethernet CLI options (-a and -b)
115 if (check_eth_mac_txt(ETH_SRC
)) // if true then problem!
117 // use own (already set in init.c)
119 src_random
= tx
.eth_src_rand
; // local vars are faster
122 // IN ANY CASE if dst has been specified:
126 // Evaluate Ethernet CLI options (-a and -b)
127 if (check_eth_mac_txt(ETH_DST
)) // if true then problem!
129 str2hex("ff:ff:ff:ff:ff:ff",tx
.eth_dst
, 6); // the default
131 dst_random
= tx
.eth_dst_rand
; // local vars are faster
135 // Catch errors with too short bytestrings:
139 // bytestring only needs to contain eth_type
144 // bytstring must contain src and type
147 if (bytestring_s
< min_size
)
149 j
= min_size
- bytestring_s
; // this is what's missing
150 bytestring_s
+= j
; // note that bytestring has been filled up with 0x00, so we can do that
154 // ADDENDUM: If src specified, dst missing:
156 if ( (!dst
) && (src
) )
158 str2hex_mac ("ff:ff:ff:ff:ff:ff", tx
.eth_dst
);
163 // ADDENDUM: If dst specified, src missing:
167 // Get eth_src from bytestring:
168 if (bytestring_s
>=6) {
169 (void) getbytes (bytestring
, tx
.eth_src
, byte_ptr
, byte_ptr
+5);
170 byte_ptr
=7; // now points to eth_type within bytestring
174 // FINALLY: If both dst and src have NOT been specified:
176 if ((!dst
) && (!src
))
178 if (bytestring_s
>=6) {
179 (void) getbytes (bytestring
, tx
.eth_dst
, byte_ptr
, byte_ptr
+5);
183 if (bytestring_s
>=12) {
184 (void) getbytes (bytestring
, tx
.eth_src
, byte_ptr
, byte_ptr
+5);
185 byte_ptr
=13; // points to eth_type
191 if (bytestring_s
>=2) {
192 tx
.eth_type
= 256 * bytestring
[byte_ptr
-1] + bytestring
[byte_ptr
]; // don't forget: byte_ptr counts from 1 not 0
193 byte_ptr
+=2; // points to first payload byte (if available)
197 // Get remaining payload:
199 if ( (tx
.eth_payload_s
= bytestring_s
- byte_ptr
+1) > 0 ) // if there are any remaining bytes
201 (void) getbytes (bytestring
, tx
.eth_payload
, byte_ptr
, bytestring_s
);
206 // Add padding if desired.
207 // Note: padding means 'extend to given length' (not 'add these number of bytes')
210 pad
= tx
.padding
- (14 + tx
.eth_payload_s
); // number of additonal pad bytes required
211 for (i
=0; i
<pad
; i
++)
213 // tx.eth_payload[i+tx.eth_payload_s] = (u_int8_t) ( ((float) rand()/RAND_MAX)*256);
214 tx
.eth_payload
[i
+tx
.eth_payload_s
] = 0x00;
216 tx
.eth_payload_s
+= pad
;
222 // *** All Ethernet parameters have been determined !
223 // *** Now let's send the frame!
225 l
= libnet_init (LIBNET_LINK_ADV
, tx
.device
, err_buf
);
229 fprintf(stderr
, " mz/send_eth: libnet_init() failed (%s)", err_buf
);
239 fprintf(stderr
, " mz: !!! Infinite mode! Will send frames until you stop Mausezahn!!!\n");
244 if ( (!quiet
) && (!tx
.delay
) && (tx
.count
==0) )
245 fprintf(stderr
, " mz: !!! Will send at maximum frame rate without feedback!!!\n");
250 // this is for the statistics:
256 if (tx
.count
!=0) repeat
=0; // count=0 means repeat ad inifinitum
258 for (i
=0; i
<loop
; i
++)
262 tx
.eth_src
[0] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256) & 0xFE;
263 tx
.eth_src
[1] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256);
264 tx
.eth_src
[2] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256);
265 tx
.eth_src
[3] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256);
266 tx
.eth_src
[4] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256);
267 tx
.eth_src
[5] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256);
273 tx
.eth_dst
[0] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256);
274 tx
.eth_dst
[1] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256);
275 tx
.eth_dst
[2] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256);
276 tx
.eth_dst
[3] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256);
277 tx
.eth_dst
[4] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256);
278 tx
.eth_dst
[5] = (u_int8_t
) ( ((float) rand()/RAND_MAX
)*256);
282 if (update
) // new frame parameters
284 t
= libnet_build_ethernet (tx
.eth_dst
,
294 fprintf(stderr
, " mz/send_eth: %s", libnet_geterror(l
));
300 bs2str (tx
.eth_dst
, message
, 6); // DA
301 fprintf(stderr
, " mz: send %s",message
);
303 bs2str (tx
.eth_src
, message
, 6); // SA
304 fprintf(stderr
, " %s",message
);
306 type2str(tx
.eth_type
, message
);
307 fprintf(stderr
, " %s",message
); // Type
309 bs2str (tx
.eth_payload
, message
, tx
.eth_payload_s
); // Payload
310 fprintf(stderr
, " %s\n",message
);
315 fprintf(stderr
, "\n");
316 fprintf(stderr
, "*** NOTE: Simulation only! Nothing has been sent! ***\n");
329 if ( (verbose
) && (!src_random
) && (!dst_random
) )
331 fprintf(stderr
, ".");
341 if ((tx
.delay
) || (tx
.count
==0))
343 fprintf(stderr
,"\n");
346 fprintf(stderr
, " mz: sent %u frames.\n",loop
);
359 // ==========================================================================================
364 fprintf(stderr," mz/send_bytes: \n");
366 fprintf(stderr," DA = %s", dast);
368 fprintf(stderr," SA = %s", sast);
369 fprintf(stderr," type = %x",et);
370 bs2str(payload,pl,payload_s);
371 fprintf(stderr," data = %s\n",pl);