summaryrefslogtreecommitdiff
path: root/staging/cli_eth.c
blob: 668aa9582abe356df170ef35ad15740d089f9e47 (plain)
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
/*
 * 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"





int cmd_packet_mac_address_source (struct cli_def *cli, const char *command, char *argv[], int argc)
{
   int i,j;
   
   if ( (strcmp(argv[argc-1],"?")==0) || (argc>1) )
     {
	cli_print(cli, "XX:XX:XX:XX:XX:XX     Configure a source MAC address\n");
	cli_print(cli, "Optionally you may use randomly generated (unicast)\r");
	cli_print(cli, "MAC addresses, using the keyword 'random'\n");
	return CLI_OK;
     }

   if (argc==1)
     {
	if (mz_strcmp(argv[0], "random", 3)==0)
	  {
	     clipkt->eth_src_israndom = 1;
	     return CLI_OK;
	  }
	
	if (mz_strcmp(argv[0], "default", 3)==0)
	  {
	     // find index of device_list with the device configured in clipkt:
	     i=0;
	     while (strncmp(device_list[i].dev, clipkt->device, 10) && (i<device_list_entries)) i++;
	     for (j=0;j<6;j++) clipkt->eth_src[j] = device_list[i].mac_mops[j];
	     clipkt->eth_src_israndom = 0;
	     return CLI_OK;
	  }
	
	if (mops_pdesc_mac(clipkt->eth_src, argv[0]))
	  {
	     cli_print(cli,"Invalid MAC address (use format: XX:XX:XX:XX:XX:XX)\n");
	  }
	else // MAC was OK
	  {
	     clipkt->eth_src_israndom = 0;
	  }
     }
   else
     cli_print(cli, "Invalid MAC format!\n");
   

   return CLI_OK;
}



int cmd_packet_mac_address_destination (struct cli_def *cli, const char *command, char *argv[], int argc)
{

   if ( (strcmp(argv[argc-1],"?")==0) || (argc>1) )
     {
	cli_print(cli, "XX:XX:XX:XX:XX:XX     Configure a destination MAC address\n");
	return CLI_OK;
     }
   if (argc==1)
     {
	if (mz_strcmp(argv[0], "bcast", 2)==0)
	  {
	     mops_pdesc_mac (clipkt->eth_dst, "ff:ff:ff:ff:ff:ff");
	     return CLI_OK;
	  }
	else if (mz_strcmp(argv[0], "pvst", 2)==0)
	  {
	     mops_pdesc_mac (clipkt->eth_dst, "01:00:0C:CC:CC:CD");
	     return CLI_OK;
	  }
	else if (mz_strcmp(argv[0], "cisco", 2)==0)
	  {
	     mops_pdesc_mac (clipkt->eth_dst, "01:00:0C:CC:CC:CC");
	     return CLI_OK;
	  }
	else if (mz_strcmp(argv[0], "stp", 2)==0)
	  {
	     mops_pdesc_mac (clipkt->eth_dst, "01:80:C2:00:00:00");
	     return CLI_OK;
	  }

	if (mops_pdesc_mac(clipkt->eth_dst, argv[0]))
	  {
	     cli_print(cli,"Invalid MAC address (use format: XX:XX:XX:XX:XX:XX)\n");
	  }
     }
   else
     cli_print(cli, "Invalid MAC format!\n");

   return CLI_OK;
}






int cmd_eth_type (struct cli_def *cli, const char *command, char *argv[], int argc)
{
   unsigned long int t32;
   
   if ( (strcmp(argv[argc-1],"?")==0) || (argc>1) )
     {
	cli_print(cli, "Specify the Ethernet type field in hexadecimal format.\n");
	cli_print(cli, "For example:\n");
	cli_print(cli, "  800   ......... IP\r");
	cli_print(cli, "  806   ......... ARP\r");
	cli_print(cli, "  835   ......... RARP\r");
	cli_print(cli, " 8100   ......... 802.1Q\r");
	cli_print(cli, " 888E   ......... 802.1X\r");
	cli_print(cli, "\n");
	return CLI_OK;
     }
   
   if (argc==1)
     {
	t32 = xstr2int(argv[0]);
	if (t32>0xffff) 
	  {
	     cli_print(cli, "EtherType must not exceed ffff.\n");
	     return CLI_OK;
	  }
	if (t32<0x800) 
	  {
	     cli_print(cli, "WARNING: 'Officially' the EtherType must be greater or equal 800.\n");
	  }
	
	clipkt->eth_type = (u_int16_t) t32;
     }
   else
     {
	cli_print(cli, "Only one parameter accepted.\n");
     }
   
   return CLI_OK;
}




int cmd_eth_length (struct cli_def *cli, const char *command, char *argv[], int argc)
{
   unsigned long int t32;
   
   if ( (strcmp(argv[argc-1],"?")==0) || (argc>1) )
     {
	cli_print(cli, "Specify the 802.3 length field in decimal notation.\r");
	cli_print(cli, "\n");
	return CLI_OK;
     }

      if (argc==1)
     {
	t32 = str2int(argv[0]);
	if (t32>0xffff) 
	  {
	     cli_print(cli, "The length field must not exceed 65535.\n");
	     return CLI_OK;
	  }
	if (t32>0x7ff) 
	  {
	     cli_print(cli, "WARNING: 'Officially' the 802.3 length field must not be greater than 1522.\n");
	  }
	
	clipkt->eth_len = (u_int16_t) t32;
     }
   else
     {
	cli_print(cli, "Only one parameter accepted.\n");
     }

   
   
   return CLI_OK;
}





int cmd_eth_llc (struct cli_def *cli, const char *command, char *argv[], int argc)
{

   if ( (strcmp(argv[argc-1],"?")==0) || (argc>1) )
     {
	cli_print(cli, "Specify the IEEE 802.2 Logical Link Control (LLC) in hexadecimal format.\n");
	return CLI_OK;
     }
   
   // DSAP-SSAP-Ctrl
   // ***** TODO *****
   cli_print(cli, "Not supported in this version.\n");
   
   return CLI_OK;
}




int cmd_eth_snap (struct cli_def *cli, const char *command, char *argv[], int argc)
{

   u_int8_t
     oui[16], 
     etp[16],
     t8[16] = {0xAA, 0xAA, 0x03};
   
   
   if ( (strcmp(argv[argc-1],"?")==0) || (argc>1) )
     {
	cli_print(cli, "Specify the SNAP header (OUI+Type) in hexadecimal format\r");
	cli_print(cli, "Example: 00:00:0e 08:00\r");
	cli_print(cli, "\n");
	return CLI_OK;
     }
   
   if (argc!=2)
     {
	cli_print(cli, "Two arguments required: 3-byte OUI and 2-byte EtherType\n");
	return CLI_OK;
     }
   
   if (str2hex(argv[0], oui, 15)!=3)
     {
	cli_print(cli, "Three bytes required for the OUI\n");
	return CLI_OK;
     }
   
   if (str2hex(argv[1], etp, 15)!=2)
     {
	cli_print(cli, "Two bytes required for the EtherType\n");
	return CLI_OK;
     }
   
   
   memcpy(&clipkt->eth_snap[0], &t8, 3);
   memcpy(&clipkt->eth_snap[3], &oui, 3);
   memcpy(&clipkt->eth_snap[6], &etp, 2);
   clipkt->eth_snap_s = 8;


   
   return CLI_OK;
}