forked from intel/openlldp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lldptool_cmds.c
649 lines (554 loc) · 12.9 KB
/
lldptool_cmds.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
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/*******************************************************************************
LLDP Agent Daemon (LLDPAD) Software
Copyright(c) 2007-2010 Intel Corporation.
This program is free software; you can redistribute it and/or modify it
under the terms and conditions of the GNU General Public License,
version 2, as published by the Free Software Foundation.
This program is distributed in the hope 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, write to the Free Software Foundation, Inc.,
51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
The full GNU General Public License is included in this distribution in
the file called "COPYING".
Contact Information:
open-lldp Mailing List <lldp-devel@open-lldp.org>
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/types.h>
#include "clif.h"
#include "dcb_types.h"
#include "lldptool.h"
#include "lldp_mand_clif.h"
#include "lldptool_cli.h"
#include "lldpad.h"
#include "lldp.h"
#include "lldp_mod.h"
#include "messages.h"
#include "lldp_util.h"
#include "lldpad_status.h"
static char *print_status(cmd_status status);
static int render_cmd(struct cmd *cmd, int argc, char **args, char **argvals)
{
int len;
int i;
len = sizeof(cmd->obuf);
/* all command messages begin this way */
snprintf(cmd->obuf, len, "%c%08x%c%1x%02x%08x%02x%s%02x",
MOD_CMD, cmd->module_id, CMD_REQUEST, CLIF_MSG_VERSION,
cmd->cmd, cmd->ops, (unsigned int) strlen(cmd->ifname),
cmd->ifname, cmd->type);
/* if the command is a tlv command, add the tlvid to the message */
if (cmd->cmd == cmd_gettlv || cmd->cmd == cmd_settlv)
snprintf(cmd->obuf+strlen(cmd->obuf), len-strlen(cmd->obuf),
"%08x", cmd->tlvid);
/* add any args and argvals to the command message */
for (i = 0; i < argc; i++) {
if (args[i])
snprintf(cmd->obuf+strlen(cmd->obuf), len-strlen(cmd->obuf),
"%02x%s", (unsigned int)strlen(args[i]), args[i]);
if (argvals[i])
snprintf(cmd->obuf+strlen(cmd->obuf),
len-strlen(cmd->obuf), "%04x%s",
(unsigned int)strlen(argvals[i]), argvals[i]);
}
return strlen(cmd->obuf);
}
int parse_response(char *buf)
{
return hex2int(buf+CLIF_STAT_OFF);
}
void get_arg_value(char *str, char **arg, char **argval)
{
unsigned int i;
for (i = 0; i < strlen(str); i++)
if (!isprint(str[i]))
return;
for (i = 0; i < strlen(str); i++)
if (str[i] == '=')
break;
if (i < strlen(str)) {
str[i] = '\0';
*argval = &str[i+1];
}
*arg = str;
}
int cli_cmd_getstats(struct clif *clif, int argc, UNUSED char *argv[],
struct cmd *cmd, int raw)
{
char **args;
char **argvals;
args = calloc(argc, sizeof(char *));
if (!args)
return cmd_failed;
argvals = calloc(argc, sizeof(char *));
if (!argvals) {
free(args);
return cmd_failed;
}
render_cmd(cmd, argc, args, argvals);
free(args);
free(argvals);
return clif_command(clif, cmd->obuf, raw);
}
int cli_cmd_gettlv(struct clif *clif, int argc, char *argv[],
struct cmd *cmd, int raw)
{
int numargs = 0;
char **args;
char **argvals;
int i;
args = calloc(argc, sizeof(char *));
if (!args)
return cmd_failed;
argvals = calloc(argc, sizeof(char *));
if (!argvals) {
free(args);
return cmd_failed;
}
for (i = 0; i < argc; i++)
get_arg_value(argv[i], &args[i], &argvals[i]);
numargs = i;
/* default is local tlv query */
if (!(cmd->ops & op_neighbor))
cmd->ops |= op_local;
if (numargs) {
/* Only commands with the config option should have
* arguments.
*/
if (!(cmd->ops & op_config)) {
printf("%s\n", print_status(cmd_invalid));
goto out;
}
/* Commands to get neighbor TLVs cannot have
* arguments.
*/
if (cmd->ops & op_neighbor) {
printf("%s\n", print_status(cmd_invalid));
goto out;
}
cmd->ops |= op_arg;
}
for (i = 0; i < numargs; i++) {
if (argvals[i]) {
printf("%s\n", print_status(cmd_invalid));
goto out;
}
}
render_cmd(cmd, argc, args, argvals);
free(args);
free(argvals);
return clif_command(clif, cmd->obuf, raw);
out:
free(args);
free(argvals);
return cmd_invalid;
}
int cli_cmd_settlv(struct clif *clif, int argc, char *argv[],
struct cmd *cmd, int raw)
{
int numargs = 0;
char **args;
char **argvals;
int i;
args = calloc(argc, sizeof(char *));
if (!args)
return cmd_failed;
argvals = calloc(argc, sizeof(char *));
if (!argvals) {
free(args);
return cmd_failed;
}
for (i = 0; i < argc; i++)
get_arg_value(argv[i], &args[i], &argvals[i]);
numargs = i;
for (i = 0; i < numargs; i++) {
if (!argvals[i]) {
printf("%s\n", print_status(cmd_invalid));
goto out;
}
}
if (numargs)
cmd->ops |= (op_arg | op_argval);
render_cmd(cmd, argc, args, argvals);
free(args);
free(argvals);
return clif_command(clif, cmd->obuf, raw);
out:
free(args);
free(argvals);
return cmd_invalid;
}
int cli_cmd_getlldp(struct clif *clif, int argc, char *argv[],
struct cmd *cmd, int raw)
{
int numargs = 0;
int numargvals = 0;
char **args;
char **argvals;
int i;
args = calloc(argc, sizeof(char *));
if (!args)
return cmd_failed;
argvals = calloc(argc, sizeof(char *));
if (!argvals) {
free(args);
return cmd_failed;
}
for (i = 0; i < argc; i++)
get_arg_value(argv[i], &args[i], &argvals[i]);
numargs = i;
for (i = 0; i < numargs; i++)
if (argvals[i])
numargvals++;
if (!numargs || numargvals) {
printf("%s\n", print_status(cmd_invalid));
goto out;
}
if (numargs)
cmd->ops |= op_arg;
render_cmd(cmd, argc, args, argvals);
free(args);
free(argvals);
return clif_command(clif, cmd->obuf, raw);
out:
free(args);
free(argvals);
return cmd_invalid;
}
int cli_cmd_setlldp(struct clif *clif, int argc, char *argv[],
struct cmd *cmd, int raw)
{
int numargs = 0;
char **args;
char **argvals;
int i;
args = calloc(argc, sizeof(char *));
if (!args)
return cmd_failed;
argvals = calloc(argc, sizeof(char *));
if (!argvals) {
free(args);
return cmd_failed;
}
for (i = 0; i < argc; i++)
get_arg_value(argv[i], &args[i], &argvals[i]);
numargs = i;
for (i = 0; i < numargs; i++) {
if (!argvals[i]) {
printf("%s\n", print_status(cmd_invalid));
goto out;
}
}
if (numargs)
cmd->ops |= (op_arg | op_argval);
render_cmd(cmd, argc, args, argvals);
free(args);
free(argvals);
return clif_command(clif, cmd->obuf, raw);
out:
free(args);
free(argvals);
return cmd_invalid;
}
static char *print_status(cmd_status status)
{
char *str;
switch(status) {
case cmd_success:
str = "Successful";
break;
case cmd_failed:
str = "Failed";
break;
case cmd_device_not_found:
str = "Device not found or inactive";
break;
case cmd_agent_not_found:
str = "Agent instance for device not found";
break;
case cmd_invalid:
str = "Invalid command";
break;
case cmd_bad_params:
str = "Invalid parameters";
break;
case cmd_peer_not_present:
str = "Peer feature not present";
break;
case cmd_ctrl_vers_not_compatible:
str = "Version not compatible";
break;
case cmd_not_capable:
str = "Device not capable";
break;
case cmd_not_applicable:
str = "Command not applicable";
break;
case cmd_no_access:
str = "Access denied";
break;
case cmd_agent_not_supported:
str = "TLV does not support agent type";
break;
default:
str = "Unknown status";
break;
}
return str;
}
u32 get_tlvid(u16 tlv_type, char* ibuf)
{
u32 tlvid;
if (tlv_type < 127) {
tlvid = tlv_type;
} else {
hexstr2bin(ibuf, (u8 *)&tlvid, sizeof(tlvid));
tlvid = ntohl(tlvid);
}
return tlvid;
}
static int print_arg_value(char *ibuf)
{
int ioff = 0;
char **args;
char **argvals;
int numargs;
int i, offset;
int ilen = strlen(ibuf);
/* count args and argvalus */
offset = ioff;
for (numargs = 0; (ilen - offset) > 2; numargs++) {
offset += 2;
if (ilen - offset > 0) {
offset++;
if (ilen - offset > 4)
offset += 4;
}
}
args = calloc(numargs, sizeof(char *));
if (!args)
return cmd_failed;
argvals = calloc(numargs, sizeof(char *));
if (!argvals) {
free(args);
return cmd_failed;
}
numargs = get_arg_val_list(ibuf, ilen, &ioff, args, argvals);
for (i = 0; i < numargs; i++) {
printf("%s", args[i]);
printf("=%s\n", argvals[i]);
}
free(args);
free(argvals);
return ioff;
}
static void print_lldp(char *ibuf)
{
print_arg_value(ibuf);
}
static void print_tlvs(struct cmd *cmd, char *ibuf)
{
unsigned int ilen;
u16 tlv_type;
u16 tlv_len;
u32 tlvid;
int offset = 0;
int printed;
struct lldp_module *np;
ilen = strlen(ibuf);
if (cmd->ops & op_config)
offset = print_arg_value(ibuf);
ilen = strlen(ibuf + offset);
while (ilen > 0) {
tlv_len = 2*sizeof(u16);
if (ilen < 2*sizeof(u16)) {
printf("corrupted TLV ilen=%d, tlv_len=%d\n",
ilen, tlv_len);
break;
}
hexstr2bin(ibuf+offset, (u8 *)&tlv_type, sizeof(tlv_type));
tlv_type = ntohs(tlv_type);
tlv_len = tlv_type;
tlv_type >>= 9;
tlv_len &= 0x01ff;
offset += 2*sizeof(u16);
ilen -= 2*sizeof(u16);
if (ilen < (unsigned) 2*tlv_len) {
printf("corrupted TLV ilen = %d, tlv_len=%d\n",
ilen, tlv_len);
break;
}
tlvid = get_tlvid(tlv_type, ibuf+offset);
if (tlvid > INVALID_TLVID)
offset += 8;
printed = 0;
LIST_FOREACH(np, &lldp_mod_head, lldp) {
if (np->ops->print_tlv(tlvid, tlv_len, ibuf+offset)) {
printed = 1;
break;
}
}
if (!printed) {
if (tlvid < INVALID_TLVID)
printf("Unidentified TLV\n\ttype:%d %*.*s\n",
tlv_type, tlv_len*2, tlv_len*2,
ibuf+offset);
else
printf("Unidentified Org Specific TLV\n\t"
"OUI: 0x%06x, Subtype: %d, Info: %*.*s\n",
tlvid >> 8, tlvid & 0x0ff,
tlv_len*2-8, tlv_len*2-8,
ibuf+offset);
}
if (tlvid > INVALID_TLVID)
offset += (2*tlv_len - 8);
else
offset += 2*tlv_len;
ilen -= 2*tlv_len;
if (tlvid == END_OF_LLDPDU_TLV)
break;
}
}
static void print_port_stats(char *ibuf)
{
static char *stat_names[] = {
"Total Frames Transmitted ",
"Total Discarded Frames Received",
"Total Error Frames Received ",
"Total Frames Received ",
"Total Discarded TLVs ",
"Total Unrecognized TLVs ",
"Total Ageouts ",
"" };
int i;
int offset = 0;
u32 value;
for(i = 0; strlen(stat_names[i]); i++) {
hexstr2bin(ibuf+offset, (u8 *)&value, sizeof(value));
value = ntohl(value);
printf("%s = %u\n", stat_names[i], value);
offset += 8;
}
}
void print_cmd_response(char *ibuf, int status)
{
struct cmd cmd;
u8 len;
int ioff;
if (status != cmd_success) {
printf("%s %s\n",
print_status(status),
ibuf + CMD_IF + CMD_IF_LEN);
return;
}
hexstr2bin(ibuf+CMD_CODE, (u8 *)&cmd.cmd, sizeof(cmd.cmd));
hexstr2bin(ibuf+CMD_OPS, (u8 *)&cmd.ops, sizeof(cmd.ops));
cmd.ops = ntohl(cmd.ops);
hexstr2bin(ibuf+CMD_IF_LEN, &len, sizeof(len));
ioff = CMD_IF;
if (len < sizeof(cmd.ifname)) {
memcpy(cmd.ifname, ibuf+CMD_IF, len);
} else {
printf("Response ifname too long: %*s\n", (int)len, cmd.ifname);
return;
}
cmd.ifname[len] = '\0';
ioff += len;
if (cmd.cmd == cmd_gettlv || cmd.cmd == cmd_settlv) {
hexstr2bin(ibuf+ioff, (u8 *)&cmd.tlvid, sizeof(cmd.tlvid));
cmd.tlvid = ntohl(cmd.tlvid);
ioff += 2*sizeof(cmd.tlvid);
}
switch (cmd.cmd) {
case cmd_getstats:
print_port_stats(ibuf + ioff);
break;
case cmd_gettlv:
print_tlvs(&cmd, ibuf+ioff);
break;
case cmd_get_lldp:
print_lldp(ibuf + ioff);
break;
case cmd_settlv:
case cmd_set_lldp:
printf("%s", ibuf+ioff);
break;
default:
return;
}
}
void print_response(char *buf, int status)
{
switch(buf[CLIF_RSP_OFF]) {
case PING_CMD:
if (status)
printf("FAILED:%s\n", print_status(status));
else
printf("%s\n", buf+CLIF_RSP_OFF+5);
break;
case ATTACH_CMD:
case DETACH_CMD:
case LEVEL_CMD:
if (status)
printf("FAILED:%s\n", print_status(status));
else
printf("OK\n");
break;
case CMD_REQUEST:
print_cmd_response(buf+CLIF_RSP_OFF, status);
break;
default:
printf("Unknown LLDP command response: %s\n", buf);
break;
}
return;
}
void print_event_msg(char *buf)
{
int level;
printf("\nEvent Message\n");
level = buf[EV_LEVEL_OFF] & 0x0f;
printf("Level: \t");
switch (level) {
case MSG_MSGDUMP:
printf("DUMP\n");
break;
case MSG_DEBUG:
printf("DEBUG\n");
break;
case MSG_INFO:
printf("INFO\n");
break;
case MSG_WARNING:
printf("WARNING\n");
break;
case MSG_ERROR:
printf("ERROR\n");
break;
case MSG_EVENT:
printf("LLDP EVENT\n");
break;
default:
printf("Unknown event message: %d", level);
return;
}
if (level != MSG_EVENT) {
printf("Message: \t%s\n\n", buf+EV_GENMSG_OFF);
return;
} else {
int cmd = atoi(&buf[EV_GENMSG_OFF]);
switch (cmd) {
case LLDP_RCHANGE:
printf("Message: \tRemote Change\n");
break;
default:
printf("Message: \tUnknown Event Command\n");
break;
}
}
}