2 * Mausezahn - A fast versatile traffic generator
3 * Copyright (C) 2008-2010 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
26 // ------- TOC ---------
28 // int cmd_ip_address_source (struct cli_def *cli, char *command, char *argv[], int argc)
29 // int cmd_ip_address_destination (struct cli_def *cli, char *command, char *argv[], int argc)
30 // int cmd_ip_version (struct cli_def *cli, char *command, char *argv[], int argc)
31 // int cmd_ip_ttl (struct cli_def *cli, char *command, char *argv[], int argc)
32 // int cmd_ip_protocol (struct cli_def *cli, char *command, char *argv[], int argc)
33 // int cmd_ip_hlen (struct cli_def *cli, char *command, char *argv[], int argc)
34 // int cmd_ip_len (struct cli_def *cli, char *command, char *argv[], int argc)
35 // int cmd_ip_id (struct cli_def *cli, char *command, char *argv[], int argc)
36 // int cmd_ip_offset (struct cli_def *cli, char *command, char *argv[], int argc)
37 // int cmd_ip_sum (struct cli_def *cli, char *command, char *argv[], int argc)
38 // int cmd_ip_tos (struct cli_def *cli, char *command, char *argv[], int argc)
39 // int cmd_ip_dscp (struct cli_def *cli, char *command, char *argv[], int argc)
40 // int cmd_ip_rsv (struct cli_def *cli, char *command, char *argv[], int argc)
41 // int cmd_ip_df (struct cli_def *cli, char *command, char *argv[], int argc)
42 // int cmd_ip_mf (struct cli_def *cli, char *command, char *argv[], int argc)
43 // int cmd_ip_option (struct cli_def *cli, char *command, char *argv[], int argc)
47 // ip-address source default|<IP>|rand|range
54 int cmd_ip_address_source (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
56 u_int8_t IP1
[4], IP2
[4];
59 u_int32_t mask
, invmask
;
62 if ( (strcmp(argv
[argc
-1],"?")==0) || (argc
>2) )
64 cli_print(cli
, "A.B.C.D configure a source IP address\n");
65 cli_print(cli
, "Optionally you may specify\r");
66 cli_print(cli
, "- a range of addresses, such as: 192.168.0.0 /16\r");
67 cli_print(cli
, " or: 192.168.0.1 192.168.255.255\r");
68 cli_print(cli
, "- 'random' for a randomly generated source address\r");
69 cli_print(cli
, "- 'default' for the interface default settings\n");
76 if (mz_strcmp(argv
[0], "default", 3)==0)
78 // find index of device_list with the device configured in clipkt:
80 while (strncmp(device_list
[i
].dev
, clipkt
->device
, 10) && (i
<device_list_entries
)) i
++;
81 clipkt
->ip_src
= device_list
[i
].ip_mops
[3]
82 + device_list
[i
].ip_mops
[2] * 256
83 + device_list
[i
].ip_mops
[1] * 256 * 256
84 + device_list
[i
].ip_mops
[0] * 256 * 256 * 256;
85 clipkt
->ip_src_israndom
= 0;
86 clipkt
->ip_src_isrange
= 0;
88 else if (mz_strcmp(argv
[0], "random", 3)==0)
90 clipkt
->ip_src_israndom
= 1;
91 clipkt
->ip_src_isrange
= 0;
93 else if (mops_pdesc_ip (IP1
, argv
[0])==0) // check if format is really an IP address
95 clipkt
->ip_src
= str2ip32(argv
[0]);
96 clipkt
->ip_src_israndom
= 0;
97 clipkt
->ip_src_isrange
= 0;
101 cli_print(cli
,"Invalid address/keyword\n");
104 case 2: // MUST be either like '10.1.1.0 /24' or '10.1.1.1 10.1.1.254'
105 if (mops_pdesc_ip (IP1
, argv
[0])==0) // check if format is really an IP address
107 clipkt
->ip_src_start
= str2ip32(argv
[0]);
108 if (strlen(argv
[1])<4) // probably prefix?
110 r
=sscanf(argv
[1],"/%u",&prefix
);
111 if ((r
==EOF
) || (r
==0) || (prefix
<1) || (prefix
>31))
112 cli_print(cli
, "Invalid prefix!\n");
116 mask
<<= (32-prefix
);
117 invmask
= 0xffffffff - mask
;
118 ip1
= ((str2ip32 (argv
[0])) & mask
) +1; // the '+1' is to ensure that we do not start with the net-id
120 clipkt
->ip_src_start
= ip1
;
121 clipkt
->ip_src_stop
= ip2
;
122 clipkt
->ip_src_isrange
= 1;
123 clipkt
->ip_src_israndom
= 0;
126 else if (mops_pdesc_ip (IP2
, argv
[1])==0) // probably 2nd IP address?
128 if (str2ip32(argv
[1]) > clipkt
->ip_src_start
)
130 clipkt
->ip_src_stop
= str2ip32(argv
[1]);
131 clipkt
->ip_src_isrange
= 1;
132 clipkt
->ip_src_israndom
= 0;
136 cli_print(cli
, "Invalid range! The second IP address must be greater than the first!\n");
141 cli_print(cli
, "Second parameter must be either a valid IP address or a prefix length \n");
144 else // first string is not a valid IP address
146 cli_print(cli
, "First parameter must be a valid IP address\n");
150 cli_print(cli
, "Invalid format!\n");
158 // ip-address destination <IP>|range
159 int cmd_ip_address_destination (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
161 u_int8_t IP1
[4], IP2
[4];
164 u_int32_t mask
, invmask
;
167 if ( (strcmp(argv
[argc
-1],"?")==0) || (argc
>2) )
169 cli_print(cli
, "A.B.C.D configure a destination IP address\n");
170 cli_print(cli
, "Optionally specify a range of addresses, such as: 192.168.0.0 /16\r");
171 cli_print(cli
, " or: 192.168.0.1 192.168.255.255\n");
178 if (mops_pdesc_ip (IP1
, argv
[0])==0) // check if format is really an IP address
180 clipkt
->ip_dst
= str2ip32(argv
[0]);
181 clipkt
->ip_dst_isrange
= 0;
185 cli_print(cli
,"Invalid address/range\n");
188 case 2: // MUST be either like '10.1.1.0 /24' or '10.1.1.1 10.1.1.254'
189 if (mops_pdesc_ip (IP1
, argv
[0])==0) // check if format is really an IP address
191 clipkt
->ip_dst_start
= str2ip32(argv
[0]);
192 if (strlen(argv
[1])<4) // probably prefix?
194 r
=sscanf(argv
[1],"/%u",&prefix
);
195 if ((r
==EOF
) || (r
==0) || (prefix
<1) || (prefix
>31))
196 cli_print(cli
, "Invalid prefix!\n");
200 mask
<<= (32-prefix
);
201 invmask
= 0xffffffff - mask
;
202 ip1
= ((str2ip32 (argv
[0])) & mask
) +1; // the '+1' is to ensure that we do not start with the net-id
204 clipkt
->ip_dst_start
= ip1
;
205 clipkt
->ip_dst_stop
= ip2
;
206 clipkt
->ip_dst_isrange
= 1;
209 else if (mops_pdesc_ip (IP2
, argv
[1])==0) // probably 2nd IP address?
211 if (str2ip32(argv
[1]) > clipkt
->ip_dst_start
)
213 clipkt
->ip_dst_stop
= str2ip32(argv
[1]);
214 clipkt
->ip_dst_isrange
= 1;
218 cli_print(cli
, "Range requirement: The second IP address must be greater than the first!\n");
223 cli_print(cli
, "Second parameter must be either a valid IP address or a prefix length \n");
226 else // first string is not a valid IP address
228 cli_print(cli
, "First parameter must be a valid IP address\n");
232 cli_print(cli
, "Invalid IP or range specification!\n");
241 int cmd_ip_version (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
245 if (strncmp(argv
[argc
-1], "?", 2)==0)
247 cli_print(cli
, "Specify the IP version (default: 4).\n");
251 ver
= (int) str2int(argv
[0]);
255 cli_print(cli
, "Version must be within range 0..15\n");
259 clipkt
->ip_version
= ver
;
266 int cmd_ip_ttl (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
270 if (strncmp(argv
[argc
-1], "?", 2)==0)
272 cli_print(cli
, "Specify the TTL (default: 255).\n");
277 ttl
= (int) str2int(argv
[0]);
281 cli_print(cli
, "TTL must be within range 0..255\n");
285 clipkt
->ip_ttl
= ttl
;
293 int cmd_ip_protocol (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
297 if (strncmp(argv
[argc
-1], "?", 2)==0)
299 cli_print(cli
, "Specify the protocol number (default: 0).\n");
304 proto
= (int) str2int(argv
[0]);
308 cli_print(cli
, "The protocol number must be within range 0..255\n");
312 clipkt
->ip_proto
= proto
;
321 int cmd_ip_hlen (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
325 if (strncmp(argv
[argc
-1], "?", 2)==0)
327 cli_print(cli
, "Specify the header length in multiple of 4 bytes.\n");
332 ihl
= (int) str2int(argv
[0]);
336 cli_print(cli
, "The IHL must be within range 0..15\n");
340 clipkt
->ip_IHL
= ihl
;
349 int cmd_ip_len (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
353 if (strncmp(argv
[argc
-1], "?", 2)==0)
355 cli_print(cli
, "Specify the total packet length (0..65535).\n");
360 len
= (int) str2int(argv
[0]);
364 cli_print(cli
, "The packet length must be within range 0..65535\n");
368 clipkt
->ip_len
= len
;
377 int cmd_ip_id (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
382 if (strncmp(argv
[argc
-1], "?", 2)==0)
384 cli_print(cli
, "Specify the packet identification number (0..4294967295).\n");
388 if (mz_strcmp(argv
[0], "hex", 2)==0)
390 id
= xstr2int (argv
[1]);
394 id
= str2int (argv
[0]);
407 int cmd_ip_offset (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
412 if (strncmp(argv
[argc
-1], "?", 2)==0) {
413 cli_print(cli
, "Specify the fragment offset in multiples of 8 bytes.\n");
417 offset
= (int) str2int(argv
[0]);
420 cli_print(cli
, "The fragment offset must be within range 0..8191\n");
424 clipkt
->ip_frag_offset
= offset
;
433 int cmd_ip_sum (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
437 if (strncmp(argv
[argc
-1], "?", 2)==0)
439 cli_print(cli
, "Specify the IP checksum in hexadecimal or use the keyword 'auto'.\r");
440 cli_print(cli
, "By default, the checksum is computed automatically.\n");
444 if (mz_strcmp(argv
[0], "auto", 2)==0)
446 clipkt
->ip_sum_false
=0;
450 sum
= (int) xstr2int(argv
[0]);
454 cli_print(cli
, "The checksum must be within range 0..ffff\n");
458 clipkt
->ip_sum
= (u_int16_t
) sum
;
459 clipkt
->ip_sum_false
=1;
470 int cmd_ip_tos (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
474 if (strncmp(argv
[argc
-1], "?", 2)==0)
476 cli_print(cli
, "Specify the Type of Service field: <IPP> [<ToS>] [MBZ]\n");
477 cli_print(cli
, " - IP precedence (IPP) 0..7\r");
478 cli_print(cli
, " - ToS: delay/throughput/reliability/cost 0..15\r");
479 cli_print(cli
, " - MBZ ('must be zero' - however, not with Mausezahn...)\r");
480 cli_print(cli
, "Or, alternatively, configure the whole byte in hex.\n");
481 cli_print(cli
, "EXAMPLES:\n");
482 cli_print(cli
, " 5 ... IPP = 5\r");
483 cli_print(cli
, " 5 9 ... IPP = 5 and ToS = 9\r");
484 cli_print(cli
, " 5 MBZ ... IPP = 5 and MBZ is set\r");
485 cli_print(cli
, " 5 9 MBZ ... All three fields configured\r");
486 cli_print(cli
, " hex a8 ... the whole byte is set to 10101000\r");
487 cli_print(cli
, " 10101000 ... the whole byte in binary\n");
491 if ((argc
==1) && (mz_strisbinary(argv
[0])==8))
493 clipkt
->ip_tos
= (u_int8_t
) str2bin8 (argv
[0]);
497 if ((argc
==2) && (mz_strcmp(argv
[0], "hex", 2)==0))
503 cli_print(cli
, "You must specify a 2-digit hexadecimal value\n");
507 if (!(isxdigit(tmp
[0])) || (!(isxdigit(tmp
[1]))))
509 cli_print(cli
, "Non-hexadecimal value!\n");
513 clipkt
->ip_tos
= (u_int8_t
) xstr2int (tmp
);
520 if (mz_strcmp(argv
[0], "mbz", 1)==0)
522 mops_ip_tos(clipkt
, -1, -1, 1);
526 if (mops_ip_tos(clipkt
, (int)str2int(argv
[0]), -1, 0))
527 cli_print(cli
, "Invalid IP Precedence value\n");
532 if (mz_strcmp(argv
[1], "mbz", 1)==0)
534 if (mops_ip_tos(clipkt
, (int)str2int(argv
[0]), -1, 1))
535 cli_print(cli
, "Invalid IP Precedence value\n");
539 if (mops_ip_tos(clipkt
, (int)str2int(argv
[0]), (int)str2int(argv
[1]), 0))
540 cli_print(cli
, "Invalid values\n");
545 if (mz_strcmp(argv
[2], "mbz", 1)!=0)
546 cli_print(cli
, "In this case the 3rd argument must be 'mbz'\n");
548 if (mops_ip_tos(clipkt
, (int)str2int(argv
[0]), (int)str2int(argv
[1]), 1))
549 cli_print(cli
, "Invalid values\n");
562 int cmd_ip_dscp (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
564 if ((argc
!=1) || (strncmp(argv
[argc
-1], "?", 2)==0))
566 cli_print(cli
, "Specify the Type of Service field using the DSCP format.\r");
567 cli_print(cli
, "Multiple notations are supported.\n");
568 cli_print(cli
, "Examples:\r");
569 cli_print(cli
, " AF32 .... specify AF codepoint with class 3 and drop probability 2\r");
570 cli_print(cli
, " EF .... specify Expedited Forwarding\r");
571 cli_print(cli
, " CS7 .... specify Code Selector 7\r");
572 cli_print(cli
, " 101110 .... specify the DSCP in binary\r");
573 cli_print(cli
, " 56 .... specify the DSCP in decimal\r");
574 cli_print(cli
, "\r");
578 switch (mops_ip_dscp(clipkt
, argv
[0]))
581 cli_print(cli
, "Invalid DSCP specification (use '?')\n");
584 cli_print(cli
, "Invalid AF code point (use '?')\n");
587 cli_print(cli
, "Invalid Code Selector (CS0..CS7)\n");
590 cli_print(cli
, "Invalid DSCP value (0..63)\n");
601 int cmd_ip_rsv (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
604 if (strncmp(argv
[argc
-1], "?", 2)==0)
606 cli_print(cli
, "Set or unset the reserved flag.\n");
612 cli_print(cli
, "Use the 'set' or 'unset' keywords.\n");
617 if (mz_strcmp(argv
[0], "set", 1)==0)
619 clipkt
->ip_flags_RS
= 1;
623 if (mz_strcmp(argv
[0], "unset", 1)==0)
625 clipkt
->ip_flags_RS
= 0;
629 cli_print(cli
, "Unknown keyword. Use the 'set' or 'unset' keywords.\n");
639 int cmd_ip_df (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
642 if (strncmp(argv
[argc
-1], "?", 2)==0)
644 cli_print(cli
, "Set or unset the don't fragment flag.\n");
651 cli_print(cli
, "Use the 'set' or 'unset' keywords.\n");
656 if (mz_strcmp(argv
[0], "set", 1)==0)
658 clipkt
->ip_flags_DF
= 1;
662 if (mz_strcmp(argv
[0], "unset", 1)==0)
664 clipkt
->ip_flags_DF
= 0;
668 cli_print(cli
, "Unknown keyword. Use the 'set' or 'unset' keywords.\n");
679 int cmd_ip_mf (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
682 if (strncmp(argv
[argc
-1], "?", 2)==0)
684 cli_print(cli
, "Set or unset the more fragments flag.\n");
691 cli_print(cli
, "Use the 'set' or 'unset' keywords.\n");
696 if (mz_strcmp(argv
[0], "set", 1)==0)
698 clipkt
->ip_flags_MF
= 1;
702 if (mz_strcmp(argv
[0], "unset", 1)==0)
704 clipkt
->ip_flags_MF
= 0;
708 cli_print(cli
, "Unknown keyword. Use the 'set' or 'unset' keywords.\n");
715 int cmd_ip_fragsize (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
717 u_int32_t fragsize
=0;
719 if (strncmp(argv
[argc
-1], "?", 2)==0) {
720 cli_print(cli
, "Enable fragmentation by configuring a fragment size.\n");
721 cli_print(cli
, "Note that the fragment size specifies the number of bytes in the IP payload\r");
722 cli_print(cli
, "and NOT the assumed MTU on that link. The total packet size of each fragment\r");
723 cli_print(cli
, "will be 20 bytes larger (=size of IP header if no IP options are used).\n");
724 cli_print(cli
, "WARNING: The fragment size SHOULD be a multiple of 8 bytes if you expect\r");
725 cli_print(cli
, " a valid result.\n");
726 cli_print(cli
, "ARGUMENTS: <frag-size>\n");
731 cli_print(cli
, "Specify the fragment size in bytes.\n");
736 fragsize
= (u_int32_t
) str2int(argv
[0]);
738 if ((fragsize
<0) || (fragsize
>8000)) {
739 cli_print(cli
, "The fragment size must be within range 0..8000\n");
744 cli_print(cli
, "Warning: The fragment-size is not a multiple of 8.\n");
747 clipkt
->ip_fragsize
= fragsize
;
755 int cmd_ip_fragoverlap (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
759 if (strncmp(argv
[argc
-1], "?", 2)==0) {
760 cli_print(cli
, "Specify how many bytes should overlap when IP fragmentation is enabled.\n");
761 cli_print(cli
, "NOTE: The number of overlap bytes is either 0 (default, no overlap) or\r");
762 cli_print(cli
, " a multiple of 8 bytes but smaller than frag-size.\n");
763 cli_print(cli
, "ARGUMENTS: <overlap>\n");
768 cli_print(cli
, "Specify how many bytes should overlap between successive IP fragments.\n");
773 overlap
= (u_int32_t
) str2int(argv
[0]);
775 if (clipkt
->ip_fragsize
== 0) {
776 cli_print(cli
, "Please configure the fragment size first.\n");
780 if ((overlap
>clipkt
->ip_fragsize
) || (overlap
%8)) {
781 cli_print(cli
, "The overlap MUST be a multiple of 8 and MUST NOT exceed frag-size!\n");
785 clipkt
->ip_frag_overlap
= overlap
;
794 int cmd_ip_option (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
798 if ((strncmp(argv
[argc
-1], "?", 2)==0) || (argc
==0)) {
799 cli_print(cli
, "Add or delete IP options.\n");
800 cli_print(cli
, "You can only add one option after the other; if you want to configure multiple\r");
801 cli_print(cli
, "options then repeat this command. The options are added to the IP header in the\r");
802 cli_print(cli
, "same order as you configure them.\n");
803 cli_print(cli
, "Currently the following options are supported:\n");
804 cli_print(cli
, "router-alert [<value>] ... signal transit routers to examine the content of this\r");
805 cli_print(cli
, " packet.\r");
806 cli_print(cli
, "\n");
807 cli_print(cli
, "clear ..................... remove all options from the packet\n");
811 if (mz_strcmp(argv
[0], "router-alert", 3)==0) {
817 val
= (int) str2int(argv
[1]);
820 cli_print(cli
, "Too many arguments!\n");
823 if (mops_ip_option_ra (clipkt
, val
)) {
824 cli_print(cli
, "Value must be within 0..65535\n");
828 } else if (mz_strcmp(argv
[0], "loose-source-route", 3)==0) {
829 cli_print(cli
, "Currently not implemented\n");
831 } else if (mz_strcmp(argv
[0], "record-route", 3)==0) {
832 cli_print(cli
, "Currently not implemented\n");
836 else if (mz_strcmp(argv
[0], "clear", 2)==0) {
837 mops_ip_option_remove_all (clipkt
);
845 // By default we use ARP to determine the destination MAC and therefore support
846 // automatic (in)direct delivery of IP packets. Alternatively the user may turn
847 // this off and may configure an arbitrary destination MAC address
849 int cmd_ip_delivery (struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
852 if (strncmp(argv
[argc
-1], "?", 2)==0) {
853 cli_print(cli
, "Enable or disable IP auto-delivery.\n");
854 sprintf(str
, "%s", (clipkt
->auto_delivery_off
) ? "DISABLED" : "ENABLED");
855 cli_print(cli
, "Currently, IP auto-delivery is %s\n", str
);
860 cli_print(cli
, "Argument missing. Enter either 'enable' or 'disable'\n");
864 if (mz_strcmp(argv
[0], "enable", 1)==0)
865 clipkt
->auto_delivery_off
=0;
866 else if (mz_strcmp(argv
[0], "disable", 1)==0)
867 clipkt
->auto_delivery_off
=1;
869 cli_print(cli
, "Unknown keyword. Enter either 'enable' or 'disable'\n");
873 sprintf(str
, "%s", (clipkt
->auto_delivery_off
) ? "DISABLED" : "ENABLED");
874 cli_print(cli
, "IP auto-delivery is now %s\n", str
);
882 int cmd_ip_end(struct cli_def
*cli
, const char *command
, char *argv
[], int argc
)
885 sprintf(prompt
, "pkt-%i",clipkt
->id
);
886 cli_set_configmode(cli
, MZ_MODE_PACKET
, prompt
);