This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
mz.c
361 lines (299 loc) · 12.4 KB
/
mz.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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/*
* Mausezahn - A fast versatile traffic generator
* Copyright (C) 2008-2010 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
*
*/
#include "mz.h"
#include "cli.h"
#include "mops.h"
#include "llist.h"
// Catch SIGINT and clean up, close everything...
void clean_up(int sig)
{
int i;
struct arp_table_struct *cur, *next;
if (!quiet) fprintf(stderr, "\nMausezahn cleans up...\n");
if (fp!=NULL) {
if (verbose) fprintf(stderr, " close files (1) ...\n");
(void) fflush(fp);
(void) fclose(fp);
}
if (fp2!=NULL) {
if (verbose) fprintf(stderr, " close files (2) ...\n");
(void) fflush(fp2);
(void) fclose(fp2);
}
// interactive mode?
if (mz_port) {
if (verbose) fprintf(stderr, " clear mops list...\n");
mops_cleanup (mp_head);
if (verbose) fprintf(stderr, " clear automops list...\n");
automops_cleanup (amp_head);
if (verbose) fprintf(stderr, " clear packet sequences...\n");
mz_ll_delete_list (packet_sequences);
}
for (i=0; i<device_list_entries; i++) {
if (device_list[i].p_arp!=NULL) {
pcap_close(device_list[i].p_arp);
fprintf(stderr, " stopped ARP process for device %s\n", device_list[i].dev);
}
if (device_list[i].arprx_thread!=0) {
pthread_cancel(device_list[i].arprx_thread);
if (verbose)
fprintf(stderr, " (ARP thread for device %s done)\n", device_list[i].dev);
}
if (device_list[i].arp_table!=NULL) {
cur=device_list[i].arp_table;
while (cur!=NULL) {
next = cur->next;
if (cur!=NULL) free(cur);
cur=next;
}
}
// close packet sockets
if (device_list[i].ps>=0) {
close(device_list[i].ps);
}
}
if (verbose) fprintf(stderr, "finished.\n");
exit(sig);
}
void usage()
{
(void) fprintf (stderr,"\n"
MAUSEZAHN_VERSION
"\n"
"|\n"
"| USAGE: mz [options] [interface] keyword | arg_string | hex_string\n"
"|\n"
"| Short option description (see doc or manpage for more information):\n"
"| -h Prints this information.\n"
"| -4 IPv4 mode (default)\n"
"| -6 IPv6 mode\n"
"| -c <count> Send the packet count times (default: 1, infinite: 0).\n"
"| -d <delay> Apply delay between transmissions. The delay value can be\n"
"| specified in usec (default, no additional unit needed), or in\n"
"| msec (e. g. 100m or 100msec), or in seconds (e. g. 100s or 100sec).\n"
"| -r Multiplies the specified delay with a random value.\n"
"| -p <length> Pad the raw frame to specified length (using random bytes).\n"
"| -a <Src_MAC|keyword> Use specified source mac address, no matter what has\n"
"| been specified with other arguments. Keywords see below.\n"
"| Default is own interface MAC.\n"
"| -b <Dst_MAC|keyword> Same with destination mac address.\n"
"| Keywords are: \n"
"| rand use a random MAC address\n"
"| bc use a broadcast MAC address\n"
"| own use own interface MAC address (default for source MAC)\n"
"| stp use IEEE 802.1d STP multicast address\n"
"| cisco use Cisco multicast address as used for CDP, VTP, or PVST+\n"
"| -A <Src_IP> Use specified source IP address (default is own interface IP).\n"
"| -B <Dst_IP|DNS_name> Send packet to specified destination IP or domain name.\n"
"| -P <ASCII Payload> Use the specified ASCII payload.\n"
"| -f <filename> Read the ASCII payload from a file.\n"
"| -F <filename> Read the hexadecimal payload from a file.\n"
"| -Q <[CoS:]vlan> Specify 802.1Q VLAN tag and optional Class of Service. You can\n"
"| specify multiple 802.1Q VLAN tags (QinQ...) by separating them\n"
"| via a comma or a period (e. g. '5:10,20,2:30').\n"
"| -t <packet_type> Specify packet type for autobuild (you don't need to care for\n"
"| encapsulations in lower layers. Most packet types allow/require\n"
"| additional packet-specific arguments in an arg_string.\n"
"| Currently supported types: arp, bpdu, cdp, ip, icmp, udp, tcp,\n"
"| dns, rtp, syslog, lldp.\n"
"| For context-help use 'help' as arg_string!\n"
"| -T <packet_type> Specify packet type for server mode. Currently only rtp is supported.\n"
"| Enter -T help or -T rtp help for further information.\n"
"| -M <MPLS label> Insert a MPLS label. Enter '-M help' for a syntax description.\n"
"| -v|V Verbose and more verbose mode\n"
"| -q Quiet mode, i. e. even omit 'important standard short messages'.\n"
"| -S Simulation mode: DOES NOT put anything on the wire. This is\n"
"| typically combined with one of the verbose modes (v or V).\n"
"\n"
);
exit(0);
}
int main(int argc, char *argv[])
{
// These handles are only used when creating L3 and above packets.
libnet_t *l; // the context
libnet_ptag_t t2=0, t3=0, t4=0; // handles to layers
double cpu_time_used;
// Check if we have root priviliges
if ( (getuid()!=0) && (geteuid()!=0) )
{
fprintf(stderr, " Mausezahn requires root privileges.\n Exit.\n");
return 1;
}
// Reset all globals
(void) reset(0);
// Get all CLI options (sets globals, see mz.h)
if ( getopts(argc, argv) )
{
(void) fprintf(stderr, " Invalid command line parameters!\n");
usage();
}
// Check whether hires timers are supported or not:
(void) check_timer();
// *********************************************************************
// First prefer data in a mausezahn description file!
// *********************************************************************
// >>> TODO:
// Note that argument 'device' is also used here!
// Support libpcap
// Must end in state machine!
// *********************************************************************
// If no MDF given, then send packet according CLI specifications
// *********************************************************************
(void) signal(SIGINT, clean_up); // to close all file pointers etc upon SIGINT
switch (mode)
{
case BYTE_STREAM:
send_eth();
break;
case ARP:
(void) send_arp();
break;
case BPDU:
(void) send_bpdu();
break;
case CDP:
(void) send_cdp();
break;
case IP: // From now on a new much more modular method is used:
l = get_link_context();
t3 = create_ip_packet(l); // t3 can be used for later header changes
if (!quiet) complexity();
if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
else
send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
break;
case ICMP:
tx.ip_proto = 1;
l = get_link_context();
t4 = create_icmp_packet(l); // t4 can be used for later header changes
t3 = create_ip_packet(l); // t3 can be used for later header changes
if (!quiet) complexity();
if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
else
send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
break;
case ICMP6:
tx.ip_proto = 58;
l = get_link_context();
t4 = create_icmp6_packet(l); // t4 can be used for later header changes
t3 = create_ip_packet(l); // t3 can be used for later header changes
if (ipv6_mode)
update_ISUM(l, t4);
if (!quiet) complexity();
if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
else
send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
break;
case UDP:
tx.ip_proto = 17;
l = get_link_context();
t4 = create_udp_packet(l); // t4 can be used for later header changes
t3 = create_ip_packet(l); // t3 can be used for later header changes
if (ipv6_mode)
update_USUM(l, t4);
if (!quiet) complexity();
if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
else
send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
break;
case TCP:
tx.ip_proto = 6;
l = get_link_context();
t4 = create_tcp_packet(l); // t4 can be used for later header changes
t3 = create_ip_packet(l); // t3 can be used for later header changes
if (ipv6_mode)
update_TSUM(l, t4);
if (!quiet) complexity();
if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
else
send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
break;
case DNS:
tx.ip_proto = 17;
l = get_link_context();
(void) create_dns_packet();
t4 = create_udp_packet(l); // t4 can be used for later header changes
t3 = create_ip_packet(l); // t3 can be used for later header changes
if (!quiet) complexity();
if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
else
send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
break;
case RTP:
tx.ip_proto = 17;
l = get_link_context();
if (!quiet) fprintf(stderr, " mz: RTP mode! (count=%u, delay=%u usec)\n\n", tx.count, tx.delay);
(void) create_rtp_packet();
t4 = create_udp_packet(l); // t4 can be used for later header changes
t3 = create_ip_packet(l); // t3 can be used for later header changes
if (!quiet) complexity();
if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
else
send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
break;
case RX_RTP: // Receive RTP packets
rcv_rtp_init();
rcv_rtp();
break;
case SYSLOG:
tx.ip_proto = 17;
l = get_link_context();
(void) create_syslog_packet();
t4 = create_udp_packet(l); // t4 can be used for later header changes
t3 = create_ip_packet(l); // t3 can be used for later header changes
if (!quiet) complexity();
if (tx.packet_mode==0) // Ethernet manipulation features does NOT use ARP to determine eth_dst
t2 = create_eth_frame(l, t3, t4); // t2 can be used for later header changes
else
send_frame (l, t3, t4); // NOTE: send_frame also destroys context finaly
break;
case LLDP: // start with a new concept here
//l = get_link_context();
//(void) create_lldp_packet();
// // // printf("SIZE=%lu\n",sizeof(struct tx_struct));
fprintf(stderr, "LLDP is currently only supported via the interactive mode\n");
exit(1);
break;
default:
(void) fprintf(stderr," mz/main: unknown mode! Stop.\n");
return (1);
}
if (!quiet)
{
mz_stop = clock();
cpu_time_used = ((double) (mz_stop - mz_start)) / CLOCKS_PER_SEC;
if (cpu_time_used > 0)
{
total_d /= cpu_time_used;
fprintf(stderr, "%.2f seconds (%.Lf packets per second)\n",cpu_time_used,total_d);
}
else
{
fprintf(stderr, "\n");
}
}
return(0);
}