summaryrefslogtreecommitdiff
path: root/staging/cli_lldp.c
blob: 899113735caf4de77b4a843cfa565a51f4a9a476 (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
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
 * 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_lldp_conformance (struct cli_def *cli, const char *command, char *argv[], int argc)
{
	struct mops_ext_lldp * pd = clipkt->p_desc;
	

	if ( (strcmp(argv[argc-1],"?")==0) || (argc!=1)) {
		cli_print(cli, "Enables or disables LLDP standard conformance mode.\n");
		cli_print(cli, "Keywords: enable | disable\n");
		cli_print(cli, "Per default, standard LLDP messages are created which require a fixed\r");
		cli_print(cli, "order of the mandatory TLVs. If the standard conformance mode is disabled\r");
		cli_print(cli, "then you can configure an arbitrary sequence of LLDP TLVs. \n");
		cli_print(cli, "Currently, the LLDP standard conformance mode is %s\n", (pd->non_conform) ? "DISABLED" : "ENABLED");
		return CLI_OK;
	}

	if (mz_strcmp(argv[0], "enable", 1)==0) {
		pd->non_conform = 0;
		return CLI_OK;
	}
	

	if (mz_strcmp(argv[0], "disable", 1)==0) {
		pd->non_conform = 1;
		return CLI_OK;
	}
	
	cli_print(cli, "Enter enable or disable\n");
	return CLI_OK;
}


int cmd_lldp_chassis_id (struct cli_def *cli, const char *command, char *argv[], int argc)
{
	struct mops_ext_lldp * pd = clipkt->p_desc;
	int subtype = 4;
	char *cid;
	int cl, cidl;
	u_int8_t tmp[512];
	
	if ( (strcmp(argv[argc-1],"?")==0) || (argc>2)) {
		cli_print(cli, "Configure a Chassis ID TLV.\n");
		cli_print(cli, "ARGUMENTS: [<subtype>] <chassis-id>\n");
		cli_print(cli, "By default the <subtype> is of kind 'mac address (4)' and the <chassis-id>\r");
		cli_print(cli, "must be a hexadecimal string (e. g. 00:01:ca:fe:de:ad) of max 255 bytes\n");
		return CLI_OK;
	}

	if (argc==2) {
		subtype = (int) str2int(argv[0]);
		if ((subtype>255) || (mz_strisnum(argv[0])==0)) {
			cli_print(cli, "Invalid subtype\n");
			return CLI_OK;
		}
		cid = argv[1];
	} else
		cid = argv[0];

	cl=strnlen(cid, 1024);
	
	if (cl>=1024) {
		cli_print(cli, "Chassis-ID too long\n");
		return CLI_OK;
	} else cidl=str2hex(cid, tmp, 511);

	if (pd->non_conform == 0) {
		pd->chassis_id_subtype = subtype; 
		memcpy((void*) pd->chassis_id, (void*)tmp, cidl);
		pd->chassis_id_len = cidl;
	} else { 
		// non_conform
		mops_lldp_opt_tlv_chassis (clipkt, subtype, cidl, tmp);
	}

	return CLI_OK;
}




int cmd_lldp_port_id (struct cli_def *cli, const char *command, char *argv[], int argc)
{
	struct mops_ext_lldp * pd = clipkt->p_desc;
	int subtype = 4;
	char *pid;
	int pl;
	
	if ( (strcmp(argv[argc-1],"?")==0) || (argc>2)) {
		cli_print(cli, "Configure a Port ID TLV.\n");
		cli_print(cli, "ARGUMENTS: [<subtype>] <port-id>\n");
		cli_print(cli, "By default the <subtype> is of kind 'Interface name (5)' and the <port-id>\r");
		cli_print(cli, "must be a ascii string (usually the name of the interface e. g. eth3) of\r");
		cli_print(cli, "max 255 bytes.\n");
		return CLI_OK;
	}

	if (argc==2) {
		subtype = (int) str2int(argv[0]);
		if ((subtype>255) || (mz_strisnum(argv[0])==0)) {
			cli_print(cli, "Invalid subtype\n");
			return CLI_OK;
		}
		pid = argv[1];
	} else
		pid = argv[0];

	pl=strnlen(pid, 256);
	
	if (pl>255) {
		cli_print(cli, "Port-ID too long\n");
		return CLI_OK;
	} 


	if (pd->non_conform == 0) {
		pd->port_id_subtype = subtype; 
		memcpy((void*) pd->port_id, (void*) pid, pl);
		pd->port_id_len = pl;
	} else { 
		// non_conform
		mops_lldp_opt_tlv_port (clipkt, subtype, pl, (u_int8_t*) pid);
	}

	return CLI_OK;
}



int cmd_lldp_ttl (struct cli_def *cli, const char *command, char *argv[], int argc)
{
	struct mops_ext_lldp * pd = clipkt->p_desc;
	int ttl;
	
	if ( (strcmp(argv[argc-1],"?")==0) || (argc!=1)) {
		cli_print(cli, "Configure the LLDP TTL.\n");
		cli_print(cli, "ARGUMENTS: <time-to-live>\n");
		cli_print(cli, "The TTL must be within 0..65535\n");
		return CLI_OK;
	}

	ttl = (int) str2int(argv[0]);
	
	if (mz_strisnum(argv[0])==0) {
		cli_print(cli, "Invalid argument\n");
		return CLI_OK;
	}

	if (ttl>0xffff) {
		cli_print(cli, "TTL must be within 0..65535\n");
		return CLI_OK;
	}

	if (pd->non_conform == 0) {
		pd->TTL = ttl;
	} else { 
		// non_conform
		mops_lldp_opt_tlv_TTL (clipkt, ttl);
	}

	return CLI_OK;
}



int cmd_lldp_vlan (struct cli_def *cli, const char *command, char *argv[], int argc)
{
	int vlan;
	
	if ( (strcmp(argv[argc-1],"?")==0) || (argc!=1)) {
		cli_print(cli, "Configure the LLDP Port VLAN-ID.\n");
		cli_print(cli, "ARGUMENTS: <vlan-id>\n");
		cli_print(cli, "The vlan-id must be within 0..65535\n");
		return CLI_OK;
	}

	vlan = (int) str2int(argv[0]);
	
	if (mz_strisnum(argv[0])==0) {
		cli_print(cli, "Invalid argument\n");
		return CLI_OK;
	}

	if (vlan>0xffff) {
		cli_print(cli, "The VLAN-ID must be within 0..65535\n");
		return CLI_OK;
	}

	
	mops_lldp_opt_tlv_vlan (clipkt, vlan);

	return CLI_OK;
}



int cmd_lldp_opt_tlv (struct cli_def *cli, const char *command, char *argv[], int argc)
{
	int type=0, len=0;
	u_int8_t tmp[512];
	
	
	if ( (strcmp(argv[argc-1],"?")==0) || (argc!=3)) {
		cli_print(cli, "Configure an arbitrary optional TLV.\n");
		cli_print(cli, "ARGUMENTS: ascii|hex <type> <value>\n");
		cli_print(cli, "The TLV type must be between 0..127, the value length is up to 511 bytes.\n");
		return CLI_OK;
	}

	
	if (mz_strcmp(argv[0], "ascii", 1)==0) {
		if ((len=strnlen(argv[2],512))>511) {
			cli_print(cli, "<value> must be smaller or equal 511 characters\n");
			return CLI_OK;
		}
		mz_strncpy((char*) tmp, argv[2], 511);
	} else if (mz_strcmp(argv[0], "hex", 1)==0) {
		len=str2hex(argv[2], tmp, 512);
		if (len>511) {
			cli_print(cli, "<value> must be smaller or equal 511 bytes\n");
			return CLI_OK;
		}
	}
	
	type = (int) str2int(argv[1]);
	
	if (mz_strisnum(argv[1])==0) {
		cli_print(cli, "Invalid type\n");
		return CLI_OK;
	}

	if (type>127) {
		cli_print(cli, "<type> must be within 0..127\n");
		return CLI_OK;
	}

	
	if (mops_lldp_opt_tlv (clipkt, type, len, tmp)==0)
		cli_print(cli, "Invalid TLV values\n");

	return CLI_OK;
}




int cmd_lldp_opt_tlv_bad (struct cli_def *cli, const char *command, char *argv[], int argc)
{
	int type, len=0, wronglen;
	u_int8_t tmp[512];
	
	
	if ( (strcmp(argv[argc-1],"?")==0) || (argc!=4)) {
		cli_print(cli, "Configure an arbitrary optional *BAD* TLV.\n");
		cli_print(cli, "ARGUMENTS: ascii|hex <type> <wrong-length> <value>\n");
		cli_print(cli, "Using this command you can add a custom TLV with a wrong length parameter.\r");
		cli_print(cli, "Such TLV can be used to verify whether LLDP receivers are robust enough\r\r");
		cli_print(cli, "since a too small <wrong-length> could cause a buffer overflow. The TLV type\r");
		cli_print(cli, "must be between 0..127, the <wrong-length> can be within 0..511 (and can be\r");
		cli_print(cli, "also the true length of course\n");
		return CLI_OK;
	}

	
	if (mz_strcmp(argv[0], "ascii", 1)==0) {
		if ((len=strnlen(argv[3],512))>511) {
			cli_print(cli, "<value> must be smaller or equal 511 characters\n");
			return CLI_OK;
		}
		mz_strncpy((char*) tmp, argv[3], 511);
	} else if (mz_strcmp(argv[0], "hex", 1)==0) {
		len=str2hex(argv[3], tmp, 512);
		if (len>511) {
			cli_print(cli, "<value> must be smaller or equal 511 bytes\n");
			return CLI_OK;
		}
	}
	
	type = (int) str2int(argv[1]);
	
	if (mz_strisnum(argv[1])==0) {
		cli_print(cli, "Invalid type\n");
		return CLI_OK;
	}

	if (type>127) {
		cli_print(cli, "<type> must be within 0..127\n");
		return CLI_OK;
	}

	wronglen = (int) str2int(argv[2]);
	
	if (mz_strisnum(argv[2])==0) {
		cli_print(cli, "Invalid length\n");
		return CLI_OK;
	}

	if (wronglen>511) {
		cli_print(cli, "<wrong-length> must be within 0..511\n");
		return CLI_OK;
	}

	if (mops_lldp_opt_tlv_bad (clipkt, type, wronglen, len, tmp)==0)
		cli_print(cli, "Invalid TLV values\n");

	return CLI_OK;
}



int cmd_lldp_opt_org (struct cli_def *cli, const char *command, char *argv[], int argc)
{
	int subtype, len=0, oui=0;
	u_int8_t tmp[512];
	
	
	if ( (strcmp(argv[argc-1],"?")==0) || (argc!=4)) {
		cli_print(cli, "Configure an organisational TLV.\n");
		cli_print(cli, "ARGUMENTS: ascii|hex <oui> <subtype> <value>\n");
		cli_print(cli, "Using this command you can add an arbitrary organisational TLV. The <oui> represents\r");
		cli_print(cli, "the 'Organisational Unique Identifier' and consists of exactly three bytes in hexadecimal\r");
		cli_print(cli, "format, such as '00005e' The <subtype> is a value between <0..255>, and the length of the\r");
		cli_print(cli, "value is up to 507 bytes.\n");
		return CLI_OK;
	}

	
	if (mz_strcmp(argv[0], "ascii", 1)==0) {
		if ((len=strnlen(argv[3],512))>511) {
			cli_print(cli, "<value> must be smaller or equal 511 characters\n");
			return CLI_OK;
		}
		mz_strncpy((char*) tmp, argv[3], 511);
	} else if (mz_strcmp(argv[0], "hex", 1)==0) {
		len=str2hex(argv[3], tmp, 512);
		if (len>511) {
			cli_print(cli, "<value> must be smaller or equal 511 bytes\n");
			return CLI_OK;
		}
	}

	oui = xstr2int(argv[1]);
	if (mz_strishex(argv[1])==0) {
		cli_print(cli, "Invalid oui value\n");
		return CLI_OK;
	}

	if (oui>0xffffff) {
		cli_print(cli, "<oui> must be within 0..ffffff\n");
		return CLI_OK;
	}
	
	subtype = (int) str2int(argv[2]);
	
	if (mz_strisnum(argv[2])==0) {
		cli_print(cli, "Invalid subtype\n");
		return CLI_OK;
	}

	if (subtype>255) {
		cli_print(cli, "<subtype> must be within 0..255\n");
		return CLI_OK;
	}


	if (mops_lldp_opt_tlv_org (clipkt, oui, subtype, len, tmp)==0)
		cli_print(cli, "Invalid TLV values\n");

	return CLI_OK;
}





int cmd_lldp_endtlv (struct cli_def *cli, const char *command, char *argv[], int argc)
{
	
	if ( (strcmp(argv[argc-1],"?")==0) || (argc>0)) {
		cli_print(cli, "Add an 'End of LLDP' TLV\n");
		cli_print(cli, "ARGUMENTS: none\n");
		cli_print(cli, "This command allows you to insert an 'End of LLDP' TLV at any\r");
		cli_print(cli, "point within the optional TLV list. You usually want this to\r");
		cli_print(cli, "create an invalid LLDPU to test the receiver.\n");
		return CLI_OK;
	}

	
	mops_lldp_opt_tlv_end (clipkt);

	return CLI_OK;
}


int cmd_lldp_reset (struct cli_def *cli, const char *command, char *argv[], int argc)
{
	
	if ( (strcmp(argv[argc-1],"?")==0) || (argc>0)) {
		cli_print(cli, "Reset the LLPDU and clear all optional TLVs.\n");
		cli_print(cli, "ARGUMENTS: none\n");
		cli_print(cli, "All optional TLVs are added in the sequence as you configure them.\r");
		cli_print(cli, "Use this command to reset the LLDP and reconfigure all optional\r");
		cli_print(cli, "TLVs again. Additionally the parameters of the mandatory TLVs are\r");
		cli_print(cli, "reset to defaults.\n");
		return CLI_OK;
	}

	mops_init_pdesc_lldp(clipkt);
	
	return CLI_OK;
}