-
Notifications
You must be signed in to change notification settings - Fork 134
/
nwcli.c
643 lines (544 loc) · 20.9 KB
/
nwcli.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
/*
* =====================================================================================
*
* Filename: nwcli.c
*
* Description: This file implements CLI commands to interact with the project
*
* Version: 1.0
* Created: Friday 20 September 2019 06:36:26 IST
* Revision: 1.0
* Compiler: gcc
*
* Author: Er. Abhishek Sagar, Networking Developer (AS), [email protected]
* Company: Brocade Communications(Jul 2012- Mar 2016), Current : Juniper Networks(Apr 2017 - Present)
*
* This file is part of the NetworkGraph distribution (https://github.com/sachinites).
* Copyright (c) 2017 Abhishek Sagar.
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* 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/>.
*
* =====================================================================================
*/
#include "graph.h"
#include <stdio.h>
#include "CommandParser/libcli.h"
#include "CommandParser/cmdtlv.h"
#include "cmdcodes.h"
extern graph_t *topo;
/* Display functions when user presses ?*/
void
display_graph_nodes(param_t *param, ser_buff_t *tlv_buf){
node_t *node;
glthread_t *curr;
ITERATE_GLTHREAD_BEGIN(&topo->node_list, curr){
node = graph_glue_to_node(curr);
printf("%s\n", node->node_name);
} ITERATE_GLTHREAD_END(&topo->node_list, curr);
}
/*General Validations*/
int
validate_node_extistence(char *node_name){
node_t *node = get_node_by_node_name(topo, node_name);
if(node)
return VALIDATION_SUCCESS;
printf("Error : Node %s do not exist\n", node_name);
return VALIDATION_FAILED;
}
int
validate_vlan_id(char *vlan_value){
unsigned int vlan = atoi(vlan_value);
if(!vlan){
printf("Error : Invalid Vlan Value\n");
return VALIDATION_FAILED;
}
if(vlan >= 1 && vlan <= 4095)
return VALIDATION_SUCCESS;
return VALIDATION_FAILED;
}
int
validate_l2_mode_value(char *l2_mode_value){
if((strncmp(l2_mode_value, "access", strlen("access")) == 0) ||
(strncmp(l2_mode_value, "trunk", strlen("trunk")) == 0))
return VALIDATION_SUCCESS;
return VALIDATION_FAILED;
}
int
validate_mask_value(char *mask_str){
unsigned int mask = atoi(mask_str);
if(!mask){
printf("Error : Invalid Mask Value\n");
return VALIDATION_FAILED;
}
if(mask >= 0 && mask <= 32)
return VALIDATION_SUCCESS;
return VALIDATION_FAILED;
}
/*Generic Topology Commands*/
static int
show_nw_topology_handler(param_t *param, ser_buff_t *tlv_buf, op_mode enable_or_disable){
int CMDCODE = -1;
CMDCODE = EXTRACT_CMD_CODE(tlv_buf);
switch(CMDCODE){
case CMDCODE_SHOW_NW_TOPOLOGY:
dump_nw_graph(topo);
break;
default:
;
}
return 0;
}
/*Layer 2 Commands*/
typedef struct arp_table_ arp_table_t;
extern void
dump_arp_table(arp_table_t *arp_table);
static int
show_arp_handler(param_t *param, ser_buff_t *tlv_buf,
op_mode enable_or_disable){
node_t *node;
char *node_name;
tlv_struct_t *tlv = NULL;
TLV_LOOP_BEGIN(tlv_buf, tlv){
if(strncmp(tlv->leaf_id, "node-name", strlen("node-name")) ==0)
node_name = tlv->value;
}TLV_LOOP_END;
node = get_node_by_node_name(topo, node_name);
dump_arp_table(NODE_ARP_TABLE(node));
return 0;
}
typedef struct mac_table_ mac_table_t;
extern void
dump_mac_table(mac_table_t *mac_table);
static int
show_mac_handler(param_t *param, ser_buff_t *tlv_buf,
op_mode enable_or_disable){
node_t *node;
char *node_name;
tlv_struct_t *tlv = NULL;
TLV_LOOP_BEGIN(tlv_buf, tlv){
if(strncmp(tlv->leaf_id, "node-name", strlen("node-name")) ==0)
node_name = tlv->value;
}TLV_LOOP_END;
node = get_node_by_node_name(topo, node_name);
dump_mac_table(NODE_MAC_TABLE(node));
return 0;
}
extern void
send_arp_broadcast_request(node_t *node,
interface_t *oif,
char *ip_addr);
static int
arp_handler(param_t *param, ser_buff_t *tlv_buf,
op_mode enable_or_disable){
node_t *node;
char *node_name;
char *ip_addr;
tlv_struct_t *tlv = NULL;
TLV_LOOP_BEGIN(tlv_buf, tlv){
if(strncmp(tlv->leaf_id, "node-name", strlen("node-name")) ==0)
node_name = tlv->value;
else if(strncmp(tlv->leaf_id, "ip-address", strlen("ip-address")) ==0)
ip_addr = tlv->value;
} TLV_LOOP_END;
node = get_node_by_node_name(topo, node_name);
send_arp_broadcast_request(node, NULL, ip_addr);
return 0;
}
/*Layer 3 Commands*/
extern void
layer5_ping_fn(node_t *node, char *dst_ip_addr);
extern void
layer3_ero_ping_fn(node_t *node, char *dst_ip_addr,
char *ero_ip_address);
static int
ping_handler(param_t *param, ser_buff_t *tlv_buf, op_mode enable_or_disable){
int CMDCODE;
node_t *node;
char *ip_addr = NULL,
*ero_ip_addr = NULL;
char *node_name;
CMDCODE = EXTRACT_CMD_CODE(tlv_buf);
tlv_struct_t *tlv = NULL;
TLV_LOOP_BEGIN(tlv_buf, tlv){
if (strncmp(tlv->leaf_id, "node-name", strlen("node-name")) ==0)
node_name = tlv->value;
else if(strncmp(tlv->leaf_id, "ip-address", strlen("ip-address")) ==0)
ip_addr = tlv->value;
else if(strncmp(tlv->leaf_id, "ero-ip-address", strlen("ero-ip-address")) ==0)
ero_ip_addr = tlv->value;
else
assert(0);
}TLV_LOOP_END;
node = get_node_by_node_name(topo, node_name);
switch(CMDCODE){
case CMDCODE_PING:
layer5_ping_fn(node, ip_addr);
break;
case CMDCODE_ERO_PING:
layer3_ero_ping_fn(node, ip_addr, ero_ip_addr);
default:
;
}
return 0;
}
typedef struct rt_table_ rt_table_t;
extern void
dump_rt_table(rt_table_t *rt_table);
static int
show_rt_handler(param_t *param, ser_buff_t *tlv_buf,
op_mode enable_or_disable){
node_t *node;
char *node_name;
tlv_struct_t *tlv = NULL;
TLV_LOOP_BEGIN(tlv_buf, tlv){
if(strncmp(tlv->leaf_id, "node-name", strlen("node-name")) ==0)
node_name = tlv->value;
}TLV_LOOP_END;
node = get_node_by_node_name(topo, node_name);
dump_rt_table(NODE_RT_TABLE(node));
return 0;
}
extern void
delete_rt_table_entry(rt_table_t *rt_table,
char *ip_addr, char mask);
extern void
rt_table_add_route(rt_table_t *rt_table,
char *dst, char mask,
char *gw, char *oif);
static int
l3_config_handler(param_t *param, ser_buff_t *tlv_buf, op_mode enable_or_disable){
node_t *node = NULL;
char *node_name = NULL;
char *intf_name = NULL;
char *gwip = NULL;
char *mask_str = NULL;
char *dest = NULL;
int CMDCODE = -1;
CMDCODE = EXTRACT_CMD_CODE(tlv_buf);
tlv_struct_t *tlv = NULL;
TLV_LOOP_BEGIN(tlv_buf, tlv){
if (strncmp(tlv->leaf_id, "node-name", strlen("node-name")) ==0)
node_name = tlv->value;
else if(strncmp(tlv->leaf_id, "ip-address", strlen("ip-address")) ==0)
dest = tlv->value;
else if(strncmp(tlv->leaf_id, "gw-ip", strlen("gw-ip")) ==0)
gwip = tlv->value;
else if(strncmp(tlv->leaf_id, "mask", strlen("mask")) ==0)
mask_str = tlv->value;
else if(strncmp(tlv->leaf_id, "oif", strlen("oif")) ==0)
intf_name = tlv->value;
else
assert(0);
}TLV_LOOP_END;
node = get_node_by_node_name(topo, node_name);
char mask;
if(mask_str){
mask = atoi(mask_str);
}
switch(CMDCODE){
case CMDCODE_CONF_NODE_L3ROUTE:
switch(enable_or_disable){
case CONFIG_ENABLE:
{
interface_t *intf;
if(intf_name){
intf = get_node_if_by_name(node, intf_name);
if(!intf){
printf("Config Error : Non-Existing Interface : %s\n", intf_name);
return -1;
}
if(!IS_INTF_L3_MODE(intf)){
printf("Config Error : Not L3 Mode Interface : %s\n", intf_name);
return -1;
}
}
rt_table_add_route(NODE_RT_TABLE(node), dest, mask, gwip, intf_name);
}
break;
case CONFIG_DISABLE:
delete_rt_table_entry(NODE_RT_TABLE(node), dest, mask);
break;
default:
;
}
break;
default:
break;
}
return 0;
}
/*Layer 4 Commands*/
/*Layer 5 Commands*/
/*Interface Config Handler*/
extern void
interface_set_l2_mode(node_t *node,
interface_t *interface,
char *l2_mode_option);
extern void
interface_unset_l2_mode(node_t *node,
interface_t *interface,
char *l2_mode_option);
extern void
interface_set_vlan(node_t *node,
interface_t *interface,
unsigned int vlan);
extern void
interface_unset_vlan(node_t *node,
interface_t *interface,
unsigned int vlan);
static int
intf_config_handler(param_t *param, ser_buff_t *tlv_buf,
op_mode enable_or_disable){
char *node_name;
char *intf_name;
unsigned int vlan_id;
char *l2_mode_option;
int CMDCODE;
tlv_struct_t *tlv = NULL;
node_t *node;
interface_t *interface;
CMDCODE = EXTRACT_CMD_CODE(tlv_buf);
TLV_LOOP_BEGIN(tlv_buf, tlv){
if (strncmp(tlv->leaf_id, "node-name", strlen("node-name")) ==0)
node_name = tlv->value;
else if(strncmp(tlv->leaf_id, "if-name", strlen("if-name")) ==0)
intf_name = tlv->value;
else if(strncmp(tlv->leaf_id, "vlan-id", strlen("vlan-d")) ==0)
vlan_id = atoi(tlv->value);
else if(strncmp(tlv->leaf_id, "l2-mode-val", strlen("l2-mode-val")) == 0)
l2_mode_option = tlv->value;
else
assert(0);
} TLV_LOOP_END;
node = get_node_by_node_name(topo, node_name);
interface = get_node_if_by_name(node, intf_name);
if(!interface){
printf("Error : Interface %s do not exist\n", interface->if_name);
return -1;
}
switch(CMDCODE){
case CMDCODE_INTF_CONFIG_L2_MODE:
switch(enable_or_disable){
case CONFIG_ENABLE:
interface_set_l2_mode(node, interface, l2_mode_option);
break;
case CONFIG_DISABLE:
interface_unset_l2_mode(node, interface, l2_mode_option);
break;
default:
;
}
break;
case CMDCODE_INTF_CONFIG_VLAN:
switch(enable_or_disable){
case CONFIG_ENABLE:
interface_set_vlan(node, interface, vlan_id);
break;
case CONFIG_DISABLE:
interface_unset_vlan(node, interface, vlan_id);
break;
default:
;
}
break;
default:
;
}
return 0;
}
void
nw_init_cli(){
init_libcli();
param_t *show = libcli_get_show_hook();
param_t *debug = libcli_get_debug_hook();
param_t *config = libcli_get_config_hook();
param_t *run = libcli_get_run_hook();
param_t *debug_show = libcli_get_debug_show_hook();
param_t *root = libcli_get_root();
{
/*show topology*/
static param_t topology;
init_param(&topology, CMD, "topology", show_nw_topology_handler, 0, INVALID, 0, "Dump Complete Network Topology");
libcli_register_param(show, &topology);
set_param_cmd_code(&topology, CMDCODE_SHOW_NW_TOPOLOGY);
{
/*show node*/
static param_t node;
init_param(&node, CMD, "node", 0, 0, INVALID, 0, "\"node\" keyword");
libcli_register_param(show, &node);
libcli_register_display_callback(&node, display_graph_nodes);
{
/*show node <node-name>*/
static param_t node_name;
init_param(&node_name, LEAF, 0, 0, validate_node_extistence, STRING, "node-name", "Node Name");
libcli_register_param(&node, &node_name);
{
/*show node <node-name> arp*/
static param_t arp;
init_param(&arp, CMD, "arp", show_arp_handler, 0, INVALID, 0, "Dump Arp Table");
libcli_register_param(&node_name, &arp);
set_param_cmd_code(&arp, CMDCODE_SHOW_NODE_ARP_TABLE);
}
{
/*show node <node-name> mac*/
static param_t mac;
init_param(&mac, CMD, "mac", show_mac_handler, 0, INVALID, 0, "Dump Mac Table");
libcli_register_param(&node_name, &mac);
set_param_cmd_code(&mac, CMDCODE_SHOW_NODE_MAC_TABLE);
}
{
/*show node <node-name> rt*/
static param_t rt;
init_param(&rt, CMD, "rt", show_rt_handler, 0, INVALID, 0, "Dump L3 Routing table");
libcli_register_param(&node_name, &rt);
set_param_cmd_code(&rt, CMDCODE_SHOW_NODE_RT_TABLE);
}
}
}
}
{
/*run node*/
static param_t node;
init_param(&node, CMD, "node", 0, 0, INVALID, 0, "\"node\" keyword");
libcli_register_param(run, &node);
libcli_register_display_callback(&node, display_graph_nodes);
{
/*run node <node-name>*/
static param_t node_name;
init_param(&node_name, LEAF, 0, 0, validate_node_extistence, STRING, "node-name", "Node Name");
libcli_register_param(&node, &node_name);
{
/*run node <node-name> ping */
static param_t ping;
init_param(&ping, CMD, "ping" , 0, 0, INVALID, 0, "Ping utility");
libcli_register_param(&node_name, &ping);
{
/*run node <node-name> ping <ip-address>*/
static param_t ip_addr;
init_param(&ip_addr, LEAF, 0, ping_handler, 0, IPV4, "ip-address", "Ipv4 Address");
libcli_register_param(&ping, &ip_addr);
set_param_cmd_code(&ip_addr, CMDCODE_PING);
{
static param_t ero;
init_param(&ero, CMD, "ero", 0, 0, INVALID, 0, "ERO(Explicit Route Object)");
libcli_register_param(&ip_addr, &ero);
{
static param_t ero_ip_addr;
init_param(&ero_ip_addr, LEAF, 0, ping_handler, 0, IPV4, "ero-ip-address", "ERO Ipv4 Address");
libcli_register_param(&ero, &ero_ip_addr);
set_param_cmd_code(&ero_ip_addr, CMDCODE_ERO_PING);
}
}
}
}
{
/*run node <node-name> resolve-arp*/
static param_t resolve_arp;
init_param(&resolve_arp, CMD, "resolve-arp", 0, 0, INVALID, 0, "Resolve ARP");
libcli_register_param(&node_name, &resolve_arp);
{
/*run node <node-name> resolve-arp <ip-address>*/
static param_t ip_addr;
init_param(&ip_addr, LEAF, 0, arp_handler, 0, IPV4, "ip-address", "Nbr IPv4 Address");
libcli_register_param(&resolve_arp, &ip_addr);
set_param_cmd_code(&ip_addr, CMDCODE_RUN_ARP);
}
}
}
}
/*config node*/
{
static param_t node;
init_param(&node, CMD, "node", 0, 0, INVALID, 0, "\"node\" keyword");
libcli_register_param(config, &node);
libcli_register_display_callback(&node, display_graph_nodes);
{
/*config node <node-name>*/
static param_t node_name;
init_param(&node_name, LEAF, 0, 0, validate_node_extistence, STRING, "node-name", "Node Name");
libcli_register_param(&node, &node_name);
{
/*config node <node-name> interface*/
static param_t interface;
init_param(&interface, CMD, "interface", 0, 0, INVALID, 0, "\"interface\" keyword");
libcli_register_param(&node_name, &interface);
{
/*config node <node-name> interface <if-name>*/
static param_t if_name;
init_param(&if_name, LEAF, 0, 0, 0, STRING, "if-name", "Interface Name");
libcli_register_param(&interface, &if_name);
{
/*config node <node-name> interface <if-name> l2mode*/
static param_t l2_mode;
init_param(&l2_mode, CMD, "l2mode", 0, 0, INVALID, 0, "\"l2mode\" keyword");
libcli_register_param(&if_name, &l2_mode);
{
/*config node <node-name> interface <if-name> l2mode <access|trunk>*/
static param_t l2_mode_val;
init_param(&l2_mode_val, LEAF, 0, intf_config_handler, validate_l2_mode_value, STRING, "l2-mode-val", "access|trunk");
libcli_register_param(&l2_mode, &l2_mode_val);
set_param_cmd_code(&l2_mode_val, CMDCODE_INTF_CONFIG_L2_MODE);
}
}
{
/*config node <node-name> interface <if-name> vlan*/
static param_t vlan;
init_param(&vlan, CMD, "vlan", 0, 0, INVALID, 0, "\"vlan\" keyword");
libcli_register_param(&if_name, &vlan);
{
/*config node <node-name> interface <if-name> vlan <vlan-id>*/
static param_t vlan_id;
init_param(&vlan_id, LEAF, 0, intf_config_handler, validate_vlan_id, INT, "vlan-id", "vlan id(1-4096)");
libcli_register_param(&vlan, &vlan_id);
set_param_cmd_code(&vlan_id, CMDCODE_INTF_CONFIG_VLAN);
}
}
}
}
{
/*config node <node-name> route*/
static param_t route;
init_param(&route, CMD, "route", 0, 0, INVALID, 0, "L3 route");
libcli_register_param(&node_name, &route);
{
/*config node <node-name> route <ip-address>*/
static param_t ip_addr;
init_param(&ip_addr, LEAF, 0, 0, 0, IPV4, "ip-address", "IPv4 Address");
libcli_register_param(&route, &ip_addr);
{
/*config node <node-name> route <ip-address> <mask>*/
static param_t mask;
init_param(&mask, LEAF, 0, l3_config_handler, validate_mask_value, INT, "mask", "mask(0-32");
libcli_register_param(&ip_addr, &mask);
set_param_cmd_code(&mask, CMDCODE_CONF_NODE_L3ROUTE);
{
/*config node <node-name> route <ip-address> <mask> <gw-ip>*/
static param_t gwip;
init_param(&gwip, LEAF, 0, 0, 0, IPV4, "gw-ip", "IPv4 Address");
libcli_register_param(&mask, &gwip);
{
/*config node <node-name> route <ip-address> <mask> <gw-ip> <oif>*/
static param_t oif;
init_param(&oif, LEAF, 0, l3_config_handler, 0, STRING, "oif", "Out-going intf Name");
libcli_register_param(&gwip, &oif);
set_param_cmd_code(&oif, CMDCODE_CONF_NODE_L3ROUTE);
}
}
}
}
}
support_cmd_negation(&node_name);
}
}
support_cmd_negation(config);
/*Do not Add any param here*/
}