man: netsniff-ng: add note about netsniff
[netsniff-ng.git] / staging / layer1.c
blobf671bb5064a092111211ef7d9ad536e214d718d3
1 /*
2 * Mausezahn - A fast versatile traffic generator
3 * Copyright (C) 2008 Herbert Haas
4 *
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.
8 *
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
12 * details.
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 // ****************************************************************************
22 //
23 // This section contains functions to send an arbitrary byte stream out of
24 // the network card. Currently it works perfect for Ethernet cards.
25 //
26 // TODO: Access to the 802.11 header
27 //
28 // ****************************************************************************
30 #include "mz.h"
31 #include "cli.h"
33 int send_eth()
35 // Tasks:
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)
47 int
48 src, // flag telling whether user has specified a source address
49 dst, // flag telling whether user has specified a destination address
50 src_random=0,
51 dst_random=0,
52 byte_ptr=1,
53 bytestring_s=0,
54 min_size=15,
55 pad=0,
56 repeat, loop, update,
57 i=0,
58 j=0;
59 char
60 err_buf[LIBNET_ERRBUF_SIZE],
61 message[MAX_PAYLOAD_SIZE*3];
63 u_int8_t bytestring[MAX_PAYLOAD_SIZE];
64 libnet_ptag_t t;
65 libnet_t *l;
69 if (tx.dot1Q)
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");
73 exit(1);
76 if (tx.mpls)
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");
80 exit(1);
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
93 tx.padding=15;
94 if (mz_port) {
95 cli_print(gcli, "Note: Set padding to 15 bytes (total length)\n");
96 } else
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:
112 if (src)
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:
124 if (dst)
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:
137 if (src)
139 // bytestring only needs to contain eth_type
140 min_size-=12;
142 else if (dst)
144 // bytstring must contain src and type
145 min_size-=6;
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:
165 if ((dst) && (!src))
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);
180 byte_ptr=7;
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
189 // Set 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')
208 if (tx.padding)
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;
221 ALL_SPECIFIED:
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 );
227 if (l == NULL)
229 fprintf(stderr, " mz/send_eth: libnet_init() failed (%s)", err_buf);
230 return -1;
233 repeat=1;
235 if (tx.count == 0)
237 loop = 1000000;
238 if (!quiet)
239 fprintf(stderr, " mz: !!! Infinite mode! Will send frames until you stop Mausezahn!!!\n");
241 else
242 loop = tx.count;
244 if ( (!quiet) && (!tx.delay) && (tx.count==0) )
245 fprintf(stderr, " mz: !!! Will send at maximum frame rate without feedback!!!\n");
247 t=0;
248 update=1;
250 // this is for the statistics:
251 mz_start = clock();
252 total_d = tx.count;
254 while (repeat)
256 if (tx.count!=0) repeat=0; // count=0 means repeat ad inifinitum
258 for (i=0; i<loop; i++)
260 if (src_random)
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);
268 update=1;
271 if (dst_random)
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);
279 update=1;
282 if (update) // new frame parameters
284 t = libnet_build_ethernet (tx.eth_dst,
285 tx.eth_src,
286 tx.eth_type,
287 tx.eth_payload,
288 tx.eth_payload_s,
290 t);
292 if (t == -1)
294 fprintf(stderr, " mz/send_eth: %s", libnet_geterror(l));
295 return -1;
298 if (verbose)
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);
312 update=0;
313 if (verbose==2)
315 fprintf(stderr, "\n");
316 fprintf(stderr, "*** NOTE: Simulation only! Nothing has been sent! ***\n");
317 libnet_destroy(l);
318 return 0;
324 libnet_write(l);
326 if (tx.delay)
328 SLEEP (tx.delay);
329 if ( (verbose) && (!src_random) && (!dst_random) )
331 fprintf(stderr, ".");
335 } // end for
337 } // end while
339 if (verbose)
341 if ((tx.delay) || (tx.count==0))
343 fprintf(stderr,"\n");
346 fprintf(stderr, " mz: sent %u frames.\n",loop);
351 libnet_destroy(l);
354 return 0;
359 // ==========================================================================================
362 if (verbose)
364 fprintf(stderr," mz/send_bytes: \n");
365 bs2str(da,dast,6);
366 fprintf(stderr," DA = %s", dast);
367 bs2str(sa,sast,6);
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);