]> git.proxmox.com Git - mirror_frr.git/blob - vtysh/vtysh.c
*: don't null after XFREE; XFREE does this itself
[mirror_frr.git] / vtysh / vtysh.c
1 /* Virtual terminal interface shell.
2 * Copyright (C) 2000 Kunihiro Ishiguro
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; see the file COPYING; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include <zebra.h>
22
23 #include <sys/un.h>
24 #include <setjmp.h>
25 #include <sys/wait.h>
26 #include <sys/resource.h>
27 #include <sys/stat.h>
28
29 #include <readline/readline.h>
30 #include <readline/history.h>
31
32 #include <dirent.h>
33 #include <stdio.h>
34 #include <string.h>
35
36 #include "linklist.h"
37 #include "command.h"
38 #include "memory.h"
39 #include "filter.h"
40 #include "vtysh/vtysh.h"
41 #include "log.h"
42 #include "bgpd/bgp_vty.h"
43 #include "ns.h"
44 #include "vrf.h"
45 #include "libfrr.h"
46 #include "command_graph.h"
47 #include "frrstr.h"
48 #include "json.h"
49 #include "ferr.h"
50
51 DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CMD, "Vtysh cmd copy")
52
53 /* Struct VTY. */
54 struct vty *vty;
55
56 /* VTY shell pager name. */
57 char *vtysh_pager_name = NULL;
58
59 /* VTY shell client structure */
60 struct vtysh_client {
61 int fd;
62 const char *name;
63 int flag;
64 char path[MAXPATHLEN];
65 struct vtysh_client *next;
66 };
67
68 /* Some utility functions for working on vtysh-specific vty tasks */
69
70 static FILE *vty_open_pager(struct vty *vty)
71 {
72 if (vty->is_paged)
73 return vty->of;
74
75 if (!vtysh_pager_name)
76 return NULL;
77
78 vty->of_saved = vty->of;
79 vty->of = popen(vtysh_pager_name, "w");
80 if (vty->of == NULL) {
81 vty->of = vty->of_saved;
82 perror("popen");
83 exit(1);
84 }
85
86 vty->is_paged = true;
87
88 return vty->of;
89 }
90
91 static int vty_close_pager(struct vty *vty)
92 {
93 if (!vty->is_paged)
94 return 0;
95
96 fflush(vty->of);
97 if (pclose(vty->of) == -1) {
98 perror("pclose");
99 exit(1);
100 }
101
102 vty->of = vty->of_saved;
103 vty->is_paged = false;
104
105 return 0;
106 }
107
108 static void vtysh_pager_envdef(bool fallback)
109 {
110 char *pager_defined;
111
112 pager_defined = getenv("VTYSH_PAGER");
113
114 if (pager_defined)
115 vtysh_pager_name = strdup(pager_defined);
116 else if (fallback)
117 vtysh_pager_name = strdup(VTYSH_PAGER);
118 }
119
120 /* --- */
121
122 struct vtysh_client vtysh_client[] = {
123 {.fd = -1, .name = "zebra", .flag = VTYSH_ZEBRA, .next = NULL},
124 {.fd = -1, .name = "ripd", .flag = VTYSH_RIPD, .next = NULL},
125 {.fd = -1, .name = "ripngd", .flag = VTYSH_RIPNGD, .next = NULL},
126 {.fd = -1, .name = "ospfd", .flag = VTYSH_OSPFD, .next = NULL},
127 {.fd = -1, .name = "ospf6d", .flag = VTYSH_OSPF6D, .next = NULL},
128 {.fd = -1, .name = "ldpd", .flag = VTYSH_LDPD, .next = NULL},
129 {.fd = -1, .name = "bgpd", .flag = VTYSH_BGPD, .next = NULL},
130 {.fd = -1, .name = "isisd", .flag = VTYSH_ISISD, .next = NULL},
131 {.fd = -1, .name = "pimd", .flag = VTYSH_PIMD, .next = NULL},
132 {.fd = -1, .name = "nhrpd", .flag = VTYSH_NHRPD, .next = NULL},
133 {.fd = -1, .name = "eigrpd", .flag = VTYSH_EIGRPD, .next = NULL},
134 {.fd = -1, .name = "babeld", .flag = VTYSH_BABELD, .next = NULL},
135 {.fd = -1, .name = "sharpd", .flag = VTYSH_SHARPD, .next = NULL},
136 {.fd = -1, .name = "fabricd", .flag = VTYSH_FABRICD, .next = NULL},
137 {.fd = -1, .name = "watchfrr", .flag = VTYSH_WATCHFRR, .next = NULL},
138 {.fd = -1, .name = "pbrd", .flag = VTYSH_PBRD, .next = NULL},
139 {.fd = -1, .name = "staticd", .flag = VTYSH_STATICD, .next = NULL},
140 {.fd = -1, .name = "bfdd", .flag = VTYSH_BFDD, .next = NULL},
141 {.fd = -1, .name = "vrrpd", .flag = VTYSH_VRRPD, .next = NULL},
142 };
143
144 /* Searches for client by name, returns index */
145 static int vtysh_client_lookup(const char *name)
146 {
147 int idx = -1;
148
149 for (unsigned int i = 0; i < array_size(vtysh_client); i++) {
150 if (strmatch(vtysh_client[i].name, name)) {
151 idx = i;
152 break;
153 }
154 }
155
156 return idx;
157 }
158
159 enum vtysh_write_integrated vtysh_write_integrated =
160 WRITE_INTEGRATED_UNSPECIFIED;
161
162 static int vtysh_reconnect(struct vtysh_client *vclient);
163
164 static void vclient_close(struct vtysh_client *vclient)
165 {
166 if (vclient->fd >= 0) {
167 if (vty->of)
168 vty_out(vty,
169 "Warning: closing connection to %s because of an I/O error!\n",
170 vclient->name);
171 close(vclient->fd);
172 /* indicate as candidate for reconnect */
173 vclient->fd = VTYSH_WAS_ACTIVE;
174 }
175 }
176
177 /*
178 * Send a CLI command to a client and read the response.
179 *
180 * Output will be printed to vty->of. If you want to suppress output, set that
181 * to NULL.
182 *
183 * vclient
184 * the client to send the command to
185 *
186 * line
187 * the command to send
188 *
189 * callback
190 * if non-null, this will be called with each line of output received from
191 * the client passed in the second parameter
192 *
193 * cbarg
194 * optional first argument to pass to callback
195 *
196 * Returns:
197 * a status code
198 */
199 static int vtysh_client_run(struct vtysh_client *vclient, const char *line,
200 void (*callback)(void *, const char *), void *cbarg)
201 {
202 int ret;
203 char stackbuf[4096];
204 char *buf = stackbuf;
205 size_t bufsz = sizeof(stackbuf);
206 char *bufvalid, *end = NULL;
207 char terminator[3] = {0, 0, 0};
208
209 /* vclinet was previously active, try to reconnect */
210 if (vclient->fd == VTYSH_WAS_ACTIVE) {
211 ret = vtysh_reconnect(vclient);
212 if (ret < 0)
213 goto out_err;
214 }
215
216 if (vclient->fd < 0)
217 return CMD_SUCCESS;
218
219 ret = write(vclient->fd, line, strlen(line) + 1);
220 if (ret <= 0) {
221 /* close connection and try to reconnect */
222 vclient_close(vclient);
223 ret = vtysh_reconnect(vclient);
224 if (ret < 0)
225 goto out_err;
226 /* retry line */
227 ret = write(vclient->fd, line, strlen(line) + 1);
228 if (ret <= 0)
229 goto out_err;
230 }
231
232 bufvalid = buf;
233 do {
234 ssize_t nread =
235 read(vclient->fd, bufvalid, buf + bufsz - bufvalid - 1);
236
237 if (nread < 0 && (errno == EINTR || errno == EAGAIN))
238 continue;
239
240 if (nread <= 0) {
241 if (vty->of)
242 vty_out(vty,
243 "vtysh: error reading from %s: %s (%d)",
244 vclient->name, safe_strerror(errno),
245 errno);
246 goto out_err;
247 }
248
249 bufvalid += nread;
250
251 /* Null terminate so we may pass this to *printf later. */
252 bufvalid[0] = '\0';
253
254 /*
255 * We expect string output from daemons, so instead of looking
256 * for the full 3 null bytes of the terminator, we check for
257 * just one instead and assume it is the first byte of the
258 * terminator. The presence of the full terminator is checked
259 * later.
260 */
261 if (bufvalid - buf >= 4)
262 end = memmem(bufvalid - 4, 4, "\0", 1);
263
264 /*
265 * calculate # bytes we have, up to & not including the
266 * terminator if present
267 */
268 size_t textlen = (end ? end : bufvalid) - buf;
269 bool b = false;
270
271 /* feed line processing callback if present */
272 while (callback && bufvalid > buf && (end > buf || !end)) {
273 textlen = (end ? end : bufvalid) - buf;
274 char *eol = memchr(buf, '\n', textlen);
275 if (eol)
276 /* line break */
277 *eol++ = '\0';
278 else if (end == buf)
279 /*
280 * no line break, end of input, no text left
281 * before end; nothing to write
282 */
283 b = true;
284 else if (end)
285 /* no nl, end of input, but some text left */
286 eol = end;
287 else if (bufvalid == buf + bufsz - 1) {
288 /*
289 * no nl, no end of input, no buffer space;
290 * realloc
291 */
292 char *new;
293
294 bufsz *= 2;
295 if (buf == stackbuf) {
296 new = XMALLOC(MTYPE_TMP, bufsz);
297 memcpy(new, stackbuf, sizeof(stackbuf));
298 } else
299 new = XREALLOC(MTYPE_TMP, buf, bufsz);
300
301 bufvalid = bufvalid - buf + new;
302 buf = new;
303 /* if end != NULL, we won't be reading more
304 * data... */
305 assert(end == NULL);
306 b = true;
307 } else
308 b = true;
309
310 if (b)
311 break;
312
313 /* eol is at line end now, either \n => \0 or \0\0\0 */
314 assert(eol && eol <= bufvalid);
315
316 if (vty->of)
317 vty_out(vty, "%s\n", buf);
318
319 callback(cbarg, buf);
320
321 /* shift back data and adjust bufvalid */
322 memmove(buf, eol, bufvalid - eol);
323 bufvalid -= eol - buf;
324 if (end)
325 end -= eol - buf;
326 }
327
328 /* else if no callback, dump raw */
329 if (!callback) {
330 if (vty->of)
331 vty_out(vty, "%s", buf);
332 memmove(buf, buf + textlen, bufvalid - buf - textlen);
333 bufvalid -= textlen;
334 if (end)
335 end -= textlen;
336
337 /*
338 * ----------------------------------------------------
339 * At this point `buf` should be in one of two states:
340 * - Empty (i.e. buf == bufvalid)
341 * - Contains up to 4 bytes of the terminator
342 * ----------------------------------------------------
343 */
344 assert(((buf == bufvalid)
345 || (bufvalid - buf <= 4 && buf[0] == 0x00)));
346 }
347
348 /* if we have the terminator, break */
349 if (end && bufvalid - buf == 4) {
350 assert(!memcmp(buf, terminator, 3));
351 ret = buf[3];
352 break;
353 }
354
355 } while (true);
356 goto out;
357
358 out_err:
359 vclient_close(vclient);
360 ret = CMD_SUCCESS;
361 out:
362 if (buf != stackbuf)
363 XFREE(MTYPE_TMP, buf);
364 return ret;
365 }
366
367 static int vtysh_client_run_all(struct vtysh_client *head_client,
368 const char *line, int continue_on_err,
369 void (*callback)(void *, const char *),
370 void *cbarg)
371 {
372 struct vtysh_client *client;
373 int rc, rc_all = CMD_SUCCESS;
374 int correct_instance = 0, wrong_instance = 0;
375
376 for (client = head_client; client; client = client->next) {
377 rc = vtysh_client_run(client, line, callback, cbarg);
378 if (rc == CMD_NOT_MY_INSTANCE) {
379 wrong_instance++;
380 continue;
381 }
382 if (client->fd > 0)
383 correct_instance++;
384 if (rc != CMD_SUCCESS) {
385 if (!continue_on_err)
386 return rc;
387 rc_all = rc;
388 }
389 }
390 if (wrong_instance && !correct_instance && vty->of) {
391 vty_out(vty,
392 "%% [%s]: command ignored as it targets an instance that is not running\n",
393 head_client->name);
394 rc_all = CMD_WARNING_CONFIG_FAILED;
395 }
396 return rc_all;
397 }
398
399 /*
400 * Execute command against all daemons.
401 *
402 * head_client
403 * where to start walking in the daemon list
404 *
405 * line
406 * the specific command to execute
407 *
408 * Returns:
409 * a status code
410 */
411 static int vtysh_client_execute(struct vtysh_client *head_client,
412 const char *line)
413 {
414 return vtysh_client_run_all(head_client, line, 0, NULL, NULL);
415 }
416
417 /* Execute by name */
418 static int vtysh_client_execute_name(const char *name, const char *line)
419 {
420 int ret = CMD_SUCCESS;
421 int idx_client = -1;
422
423 idx_client = vtysh_client_lookup(name);
424 if (idx_client != -1)
425 ret = vtysh_client_execute(&vtysh_client[idx_client], line);
426 else {
427 vty_out(vty, "Client not found\n");
428 ret = CMD_WARNING;
429 }
430
431 return ret;
432 }
433
434 /*
435 * Retrieve all running config from daemons and parse it with the vtysh config
436 * parser. Returned output is not displayed to the user.
437 *
438 * head_client
439 * where to start walking in the daemon list
440 *
441 * line
442 * the specific command to execute
443 */
444 static void vtysh_client_config(struct vtysh_client *head_client, char *line)
445 {
446 /* watchfrr currently doesn't load any config, and has some hardcoded
447 * settings that show up in "show run". skip it here (for now at
448 * least) so we don't get that mangled up in config-write.
449 */
450 if (head_client->flag == VTYSH_WATCHFRR)
451 return;
452
453 /* suppress output to user */
454 vty->of_saved = vty->of;
455 vty->of = NULL;
456 vtysh_client_run_all(head_client, line, 1, vtysh_config_parse_line,
457 NULL);
458 vty->of = vty->of_saved;
459 }
460
461 /* Command execution over the vty interface. */
462 static int vtysh_execute_func(const char *line, int pager)
463 {
464 int ret, cmd_stat;
465 unsigned int i;
466 vector vline;
467 const struct cmd_element *cmd;
468 int tried = 0;
469 int saved_ret, saved_node;
470
471 /* Split readline string up into the vector. */
472 vline = cmd_make_strvec(line);
473
474 if (vline == NULL)
475 return CMD_SUCCESS;
476
477 if (user_mode) {
478 if (strncmp("en", vector_slot(vline, 0), 2) == 0) {
479 cmd_free_strvec(vline);
480 vty_out(vty, "%% Command not allowed: enable\n");
481 return CMD_WARNING;
482 }
483 }
484
485 saved_ret = ret = cmd_execute(vty, line, &cmd, 1);
486 saved_node = vty->node;
487
488 /*
489 * If command doesn't succeeded in current node, try to walk up in node
490 * tree. Changing vty->node is enough to try it just out without actual
491 * walkup in the vtysh.
492 */
493 while (ret != CMD_SUCCESS && ret != CMD_SUCCESS_DAEMON
494 && ret != CMD_WARNING && ret != CMD_WARNING_CONFIG_FAILED
495 && ret != CMD_ERR_AMBIGUOUS && ret != CMD_ERR_INCOMPLETE
496 && vty->node > CONFIG_NODE) {
497 vty->node = node_parent(vty->node);
498 ret = cmd_execute(vty, line, &cmd, 1);
499 tried++;
500 }
501
502 vty->node = saved_node;
503
504 /*
505 * If command succeeded in any other node than current (tried > 0) we
506 * have to move into node in the vtysh where it succeeded.
507 */
508 if (ret == CMD_SUCCESS || ret == CMD_SUCCESS_DAEMON
509 || ret == CMD_WARNING) {
510 if ((saved_node == BGP_VPNV4_NODE
511 || saved_node == BGP_VPNV6_NODE
512 || saved_node == BGP_IPV4_NODE
513 || saved_node == BGP_IPV6_NODE
514 || saved_node == BGP_FLOWSPECV4_NODE
515 || saved_node == BGP_FLOWSPECV6_NODE
516 || saved_node == BGP_IPV4M_NODE
517 || saved_node == BGP_IPV4L_NODE
518 || saved_node == BGP_IPV6L_NODE
519 || saved_node == BGP_IPV6M_NODE
520 || saved_node == BGP_EVPN_NODE
521 || saved_node == LDP_IPV4_NODE
522 || saved_node == LDP_IPV6_NODE)
523 && (tried == 1)) {
524 vtysh_execute("exit-address-family");
525 } else if ((saved_node == BGP_EVPN_VNI_NODE) && (tried == 1)) {
526 vtysh_execute("exit-vni");
527 } else if (saved_node == BGP_VRF_POLICY_NODE && (tried == 1)) {
528 vtysh_execute("exit-vrf-policy");
529 } else if ((saved_node == BGP_VNC_DEFAULTS_NODE
530 || saved_node == BGP_VNC_NVE_GROUP_NODE
531 || saved_node == BGP_VNC_L2_GROUP_NODE)
532 && (tried == 1)) {
533 vtysh_execute("exit-vnc");
534 } else if (saved_node == VRF_NODE && (tried == 1)) {
535 vtysh_execute("exit-vrf");
536 } else if ((saved_node == KEYCHAIN_KEY_NODE
537 || saved_node == LDP_PSEUDOWIRE_NODE
538 || saved_node == LDP_IPV4_IFACE_NODE
539 || saved_node == LDP_IPV6_IFACE_NODE)
540 && (tried == 1)) {
541 vtysh_execute("exit");
542 } else if (tried) {
543 vtysh_execute("end");
544 vtysh_execute("configure");
545 }
546 }
547 /*
548 * If command didn't succeed in any node, continue with return value
549 * from first try.
550 */
551 else if (tried) {
552 ret = saved_ret;
553 }
554
555 cmd_free_strvec(vline);
556
557 cmd_stat = ret;
558 switch (ret) {
559 case CMD_WARNING:
560 case CMD_WARNING_CONFIG_FAILED:
561 if (vty->type == VTY_FILE)
562 vty_out(vty, "Warning...\n");
563 break;
564 case CMD_ERR_AMBIGUOUS:
565 vty_out(vty, "%% Ambiguous command: %s\n", line);
566 break;
567 case CMD_ERR_NO_MATCH:
568 vty_out(vty, "%% Unknown command: %s\n", line);
569 break;
570 case CMD_ERR_INCOMPLETE:
571 vty_out(vty, "%% Command incomplete: %s\n", line);
572 break;
573 case CMD_SUCCESS_DAEMON: {
574 /*
575 * FIXME: Don't open pager for exit commands. popen() causes
576 * problems if exited from vtysh at all. This hack shouldn't
577 * cause any problem but is really ugly.
578 */
579 if (pager && strncmp(line, "exit", 4))
580 vty_open_pager(vty);
581
582 if (!strcmp(cmd->string, "configure")) {
583 for (i = 0; i < array_size(vtysh_client); i++) {
584 cmd_stat = vtysh_client_execute(
585 &vtysh_client[i], line);
586 if (cmd_stat == CMD_WARNING)
587 break;
588 }
589
590 if (cmd_stat) {
591 line = "end";
592 vline = cmd_make_strvec(line);
593
594
595 if (vline == NULL) {
596 if (vty->is_paged)
597 vty_close_pager(vty);
598 return CMD_SUCCESS;
599 }
600
601 ret = cmd_execute_command(vline, vty, &cmd, 1);
602 cmd_free_strvec(vline);
603 if (ret != CMD_SUCCESS_DAEMON)
604 break;
605 } else if (cmd->func) {
606 (*cmd->func)(cmd, vty, 0, NULL);
607 break;
608 }
609 }
610
611 cmd_stat = CMD_SUCCESS;
612 struct vtysh_client *vc;
613 for (i = 0; i < array_size(vtysh_client); i++) {
614 if (cmd->daemon & vtysh_client[i].flag) {
615 if (vtysh_client[i].fd < 0
616 && (cmd->daemon == vtysh_client[i].flag)) {
617 for (vc = &vtysh_client[i]; vc;
618 vc = vc->next)
619 if (vc->fd == VTYSH_WAS_ACTIVE)
620 vtysh_reconnect(vc);
621 }
622 if (vtysh_client[i].fd < 0
623 && (cmd->daemon == vtysh_client[i].flag)) {
624 bool any_inst = false;
625 for (vc = &vtysh_client[i]; vc;
626 vc = vc->next)
627 any_inst = any_inst
628 || (vc->fd > 0);
629 if (!any_inst) {
630 fprintf(stderr,
631 "%s is not running\n",
632 vtysh_client[i].name);
633 continue;
634 }
635 }
636 cmd_stat = vtysh_client_execute(
637 &vtysh_client[i], line);
638 if (cmd_stat != CMD_SUCCESS)
639 break;
640 }
641 }
642 if (cmd_stat != CMD_SUCCESS)
643 break;
644
645 if (cmd->func)
646 (*cmd->func)(cmd, vty, 0, NULL);
647 }
648 }
649 if (vty->is_paged)
650 vty_close_pager(vty);
651
652 return cmd_stat;
653 }
654
655 int vtysh_execute_no_pager(const char *line)
656 {
657 return vtysh_execute_func(line, 0);
658 }
659
660 int vtysh_execute(const char *line)
661 {
662 return vtysh_execute_func(line, 1);
663 }
664
665 static char *trim(char *s)
666 {
667 size_t size;
668 char *end;
669
670 size = strlen(s);
671
672 if (!size)
673 return s;
674
675 end = s + size - 1;
676 while (end >= s && isspace((unsigned char)*end))
677 end--;
678 *(end + 1) = '\0';
679
680 while (*s && isspace((unsigned char)*s))
681 s++;
682
683 return s;
684 }
685
686 int vtysh_mark_file(const char *filename)
687 {
688 struct vty *vty;
689 FILE *confp = NULL;
690 int ret;
691 vector vline;
692 int tried = 0;
693 const struct cmd_element *cmd;
694 int saved_ret, prev_node;
695 int lineno = 0;
696 char *vty_buf_copy = NULL;
697 char *vty_buf_trimmed = NULL;
698
699 if (strncmp("-", filename, 1) == 0)
700 confp = stdin;
701 else
702 confp = fopen(filename, "r");
703
704 if (confp == NULL) {
705 fprintf(stderr, "%% Can't open config file %s due to '%s'.\n",
706 filename, safe_strerror(errno));
707 return (CMD_ERR_NO_FILE);
708 }
709
710 vty = vty_new();
711 vty->wfd = STDERR_FILENO;
712 vty->type = VTY_TERM;
713 vty->node = CONFIG_NODE;
714
715 vtysh_execute_no_pager("enable");
716 vtysh_execute_no_pager("configure");
717 vty_buf_copy = XCALLOC(MTYPE_VTYSH_CMD, VTY_BUFSIZ);
718
719 while (fgets(vty->buf, VTY_BUFSIZ, confp)) {
720 lineno++;
721 tried = 0;
722 strlcpy(vty_buf_copy, vty->buf, VTY_BUFSIZ);
723 vty_buf_trimmed = trim(vty_buf_copy);
724
725 switch (vty->node) {
726 case LDP_IPV4_IFACE_NODE:
727 if (strncmp(vty_buf_copy, " ", 3)) {
728 vty_out(vty, " exit-ldp-if\n");
729 vty->node = LDP_IPV4_NODE;
730 }
731 break;
732 case LDP_IPV6_IFACE_NODE:
733 if (strncmp(vty_buf_copy, " ", 3)) {
734 vty_out(vty, " exit-ldp-if\n");
735 vty->node = LDP_IPV6_NODE;
736 }
737 break;
738 case LDP_PSEUDOWIRE_NODE:
739 if (strncmp(vty_buf_copy, " ", 2)) {
740 vty_out(vty, " exit\n");
741 vty->node = LDP_L2VPN_NODE;
742 }
743 break;
744 default:
745 break;
746 }
747
748 if (vty_buf_trimmed[0] == '!' || vty_buf_trimmed[0] == '#') {
749 vty_out(vty, "%s", vty->buf);
750 continue;
751 }
752
753 /* Split readline string up into the vector. */
754 vline = cmd_make_strvec(vty->buf);
755
756 if (vline == NULL) {
757 vty_out(vty, "%s", vty->buf);
758 continue;
759 }
760
761 /*
762 * Ignore the "end" lines, we will generate these where
763 * appropriate
764 */
765 if (strlen(vty_buf_trimmed) == 3
766 && strncmp("end", vty_buf_trimmed, 3) == 0) {
767 cmd_free_strvec(vline);
768 continue;
769 }
770
771 prev_node = vty->node;
772 saved_ret = ret = cmd_execute_command_strict(vline, vty, &cmd);
773
774 /*
775 * If command doesn't succeeded in current node, try to walk up
776 * in node tree. Changing vty->node is enough to try it just
777 * out without actual walkup in the vtysh.
778 */
779 while (ret != CMD_SUCCESS && ret != CMD_SUCCESS_DAEMON
780 && ret != CMD_WARNING && ret != CMD_WARNING_CONFIG_FAILED
781 && ret != CMD_ERR_AMBIGUOUS && ret != CMD_ERR_INCOMPLETE
782 && vty->node > CONFIG_NODE) {
783 vty->node = node_parent(vty->node);
784 ret = cmd_execute_command_strict(vline, vty, &cmd);
785 tried++;
786 }
787
788 /*
789 * If command succeeded in any other node than current (tried >
790 * 0) we have to move into node in the vtysh where it
791 * succeeded.
792 */
793 if (ret == CMD_SUCCESS || ret == CMD_SUCCESS_DAEMON
794 || ret == CMD_WARNING) {
795 if ((prev_node == BGP_VPNV4_NODE
796 || prev_node == BGP_VPNV6_NODE
797 || prev_node == BGP_IPV4_NODE
798 || prev_node == BGP_IPV6_NODE
799 || prev_node == BGP_FLOWSPECV4_NODE
800 || prev_node == BGP_FLOWSPECV6_NODE
801 || prev_node == BGP_IPV4L_NODE
802 || prev_node == BGP_IPV6L_NODE
803 || prev_node == BGP_IPV4M_NODE
804 || prev_node == BGP_IPV6M_NODE
805 || prev_node == BGP_EVPN_NODE)
806 && (tried == 1)) {
807 vty_out(vty, "exit-address-family\n");
808 } else if ((prev_node == BGP_EVPN_VNI_NODE)
809 && (tried == 1)) {
810 vty_out(vty, "exit-vni\n");
811 } else if ((prev_node == KEYCHAIN_KEY_NODE)
812 && (tried == 1)) {
813 vty_out(vty, "exit\n");
814 } else if (tried) {
815 vty_out(vty, "end\n");
816 }
817 }
818 /*
819 * If command didn't succeed in any node, continue with return
820 * value from first try.
821 */
822 else if (tried) {
823 ret = saved_ret;
824 vty->node = prev_node;
825 }
826
827 cmd_free_strvec(vline);
828 switch (ret) {
829 case CMD_WARNING:
830 case CMD_WARNING_CONFIG_FAILED:
831 if (vty->type == VTY_FILE)
832 fprintf(stderr, "line %d: Warning...: %s\n",
833 lineno, vty->buf);
834 fclose(confp);
835 vty_close(vty);
836 XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
837 return ret;
838 case CMD_ERR_AMBIGUOUS:
839 fprintf(stderr, "line %d: %% Ambiguous command: %s\n",
840 lineno, vty->buf);
841 fclose(confp);
842 vty_close(vty);
843 XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
844 return CMD_ERR_AMBIGUOUS;
845 case CMD_ERR_NO_MATCH:
846 fprintf(stderr, "line %d: %% Unknown command: %s\n",
847 lineno, vty->buf);
848 fclose(confp);
849 vty_close(vty);
850 XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
851 return CMD_ERR_NO_MATCH;
852 case CMD_ERR_INCOMPLETE:
853 fprintf(stderr, "line %d: %% Command incomplete: %s\n",
854 lineno, vty->buf);
855 fclose(confp);
856 vty_close(vty);
857 XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
858 return CMD_ERR_INCOMPLETE;
859 case CMD_SUCCESS:
860 vty_out(vty, "%s", vty->buf);
861 if (strmatch(vty_buf_trimmed, "exit-vrf"))
862 vty_out(vty, "end\n");
863 break;
864 case CMD_SUCCESS_DAEMON: {
865 int cmd_stat;
866
867 vty_out(vty, "%s", vty->buf);
868 if (strmatch(vty_buf_trimmed, "exit-vrf"))
869 vty_out(vty, "end\n");
870 cmd_stat = vtysh_client_execute(&vtysh_client[0],
871 vty->buf);
872 if (cmd_stat != CMD_SUCCESS)
873 break;
874
875 if (cmd->func)
876 (*cmd->func)(cmd, vty, 0, NULL);
877 }
878 }
879 }
880 /* This is the end */
881 vty_out(vty, "\nend\n");
882 vty_close(vty);
883 XFREE(MTYPE_VTYSH_CMD, vty_buf_copy);
884
885 if (confp != stdin)
886 fclose(confp);
887
888 return (0);
889 }
890
891 /* Configration make from file. */
892 int vtysh_config_from_file(struct vty *vty, FILE *fp)
893 {
894 int ret;
895 const struct cmd_element *cmd;
896 int lineno = 0;
897 /* once we have an error, we remember & return that */
898 int retcode = CMD_SUCCESS;
899
900 while (fgets(vty->buf, VTY_BUFSIZ, fp)) {
901 lineno++;
902
903 ret = command_config_read_one_line(vty, &cmd, lineno, 1);
904
905 switch (ret) {
906 case CMD_WARNING:
907 case CMD_WARNING_CONFIG_FAILED:
908 if (vty->type == VTY_FILE)
909 fprintf(stderr, "line %d: Warning[%d]...: %s\n",
910 lineno, vty->node, vty->buf);
911 retcode = ret;
912
913 break;
914 case CMD_ERR_AMBIGUOUS:
915 fprintf(stderr,
916 "line %d: %% Ambiguous command[%d]: %s\n",
917 lineno, vty->node, vty->buf);
918 retcode = CMD_ERR_AMBIGUOUS;
919 break;
920 case CMD_ERR_NO_MATCH:
921 fprintf(stderr, "line %d: %% Unknown command[%d]: %s",
922 lineno, vty->node, vty->buf);
923 retcode = CMD_ERR_NO_MATCH;
924 break;
925 case CMD_ERR_INCOMPLETE:
926 fprintf(stderr,
927 "line %d: %% Command incomplete[%d]: %s\n",
928 lineno, vty->node, vty->buf);
929 retcode = CMD_ERR_INCOMPLETE;
930 break;
931 case CMD_SUCCESS_DAEMON: {
932 unsigned int i;
933 int cmd_stat = CMD_SUCCESS;
934
935 for (i = 0; i < array_size(vtysh_client); i++) {
936 if (cmd->daemon & vtysh_client[i].flag) {
937 cmd_stat = vtysh_client_execute(
938 &vtysh_client[i], vty->buf);
939 /*
940 * CMD_WARNING - Can mean that the
941 * command was parsed successfully but
942 * it was already entered in a few
943 * spots. As such if we receive a
944 * CMD_WARNING from a daemon we
945 * shouldn't stop talking to the other
946 * daemons for the particular command.
947 */
948 if (cmd_stat != CMD_SUCCESS
949 && cmd_stat != CMD_WARNING) {
950 fprintf(stderr,
951 "line %d: Failure to communicate[%d] to %s, line: %s\n",
952 lineno, cmd_stat,
953 vtysh_client[i].name,
954 vty->buf);
955 retcode = cmd_stat;
956 break;
957 }
958 }
959 }
960 if (cmd_stat != CMD_SUCCESS)
961 break;
962
963 if (cmd->func)
964 (*cmd->func)(cmd, vty, 0, NULL);
965 }
966 }
967 }
968
969 return (retcode);
970 }
971
972 /*
973 * Function processes cli commands terminated with '?' character when entered
974 * through either 'vtysh' or 'vtysh -c' interfaces.
975 */
976 static int vtysh_process_questionmark(const char *input, int input_len)
977 {
978 int ret, width = 0;
979 unsigned int i;
980 vector vline, describe;
981 struct cmd_token *token;
982
983 if (!input)
984 return 1;
985
986 vline = cmd_make_strvec(input);
987
988 /* In case of '> ?'. */
989 if (vline == NULL) {
990 vline = vector_init(1);
991 vector_set(vline, NULL);
992 } else if (input_len && isspace((unsigned char)input[input_len - 1]))
993 vector_set(vline, NULL);
994
995 describe = cmd_describe_command(vline, vty, &ret);
996
997 /* Ambiguous and no match error. */
998 switch (ret) {
999 case CMD_ERR_AMBIGUOUS:
1000 cmd_free_strvec(vline);
1001 vector_free(describe);
1002 vty_out(vty, "%% Ambiguous command.\n");
1003 rl_on_new_line();
1004 return 0;
1005 break;
1006 case CMD_ERR_NO_MATCH:
1007 cmd_free_strvec(vline);
1008 if (describe)
1009 vector_free(describe);
1010 vty_out(vty, "%% There is no matched command.\n");
1011 rl_on_new_line();
1012 return 0;
1013 break;
1014 }
1015
1016 /* Get width of command string. */
1017 width = 0;
1018 for (i = 0; i < vector_active(describe); i++)
1019 if ((token = vector_slot(describe, i)) != NULL) {
1020 if (token->text[0] == '\0')
1021 continue;
1022
1023 int len = strlen(token->text);
1024
1025 if (width < len)
1026 width = len;
1027 }
1028
1029 for (i = 0; i < vector_active(describe); i++)
1030 if ((token = vector_slot(describe, i)) != NULL) {
1031 if (!token->desc)
1032 vty_out(vty, " %-s\n", token->text);
1033 else
1034 vty_out(vty, " %-*s %s\n", width, token->text,
1035 token->desc);
1036
1037 if (IS_VARYING_TOKEN(token->type)) {
1038 const char *ref = vector_slot(
1039 vline, vector_active(vline) - 1);
1040
1041 vector varcomps = vector_init(VECTOR_MIN_SIZE);
1042 cmd_variable_complete(token, ref, varcomps);
1043
1044 if (vector_active(varcomps) > 0) {
1045 int rows, cols;
1046 rl_get_screen_size(&rows, &cols);
1047
1048 char *ac = cmd_variable_comp2str(
1049 varcomps, cols);
1050 vty_out(vty, "%s\n", ac);
1051 XFREE(MTYPE_TMP, ac);
1052 }
1053
1054 vector_free(varcomps);
1055 }
1056 }
1057
1058 cmd_free_strvec(vline);
1059 vector_free(describe);
1060
1061 return 0;
1062 }
1063
1064 /*
1065 * Entry point for user commands terminated with '?' character and typed through
1066 * the usual vtysh's stdin interface. This is the function being registered with
1067 * readline() api's.
1068 */
1069 static int vtysh_rl_describe(int a, int b)
1070 {
1071 int ret;
1072
1073 vty_out(vty, "\n");
1074
1075 ret = vtysh_process_questionmark(rl_line_buffer, rl_end);
1076 rl_on_new_line();
1077
1078 return ret;
1079 }
1080
1081 /*
1082 * Function in charged of processing vtysh instructions terminating with '?'
1083 * character and received through the 'vtysh -c' interface. If user's
1084 * instruction is well-formatted, we will call the same processing routine
1085 * utilized by the traditional vtysh's stdin interface.
1086 */
1087 int vtysh_execute_command_questionmark(char *input)
1088 {
1089 int input_len, qmark_count = 0;
1090 const char *str;
1091
1092 if (!(input && *input))
1093 return 1;
1094
1095 /* Finding out question_mark count and strlen */
1096 for (str = input; *str; ++str) {
1097 if (*str == '?')
1098 qmark_count++;
1099 }
1100 input_len = str - input;
1101
1102 /*
1103 * Verify that user's input terminates in '?' and that patterns such as
1104 * 'cmd ? subcmd ?' are prevented.
1105 */
1106 if (qmark_count != 1 || input[input_len - 1] != '?')
1107 return 1;
1108
1109 /*
1110 * Questionmark-processing function is not expecting to receive '?'
1111 * character in input string.
1112 */
1113 input[input_len - 1] = '\0';
1114
1115 return vtysh_process_questionmark(input, input_len - 1);
1116 }
1117
1118 /* Result of cmd_complete_command() call will be stored here
1119 * and used in new_completion() in order to put the space in
1120 * correct places only. */
1121 int complete_status;
1122
1123 static char *command_generator(const char *text, int state)
1124 {
1125 vector vline;
1126 static char **matched = NULL;
1127 static int index = 0;
1128
1129 /* First call. */
1130 if (!state) {
1131 index = 0;
1132
1133 if (vty->node == AUTH_NODE || vty->node == AUTH_ENABLE_NODE)
1134 return NULL;
1135
1136 vline = cmd_make_strvec(rl_line_buffer);
1137 if (vline == NULL)
1138 return NULL;
1139
1140 if (rl_end &&
1141 isspace((unsigned char)rl_line_buffer[rl_end - 1]))
1142 vector_set(vline, NULL);
1143
1144 matched = cmd_complete_command(vline, vty, &complete_status);
1145 cmd_free_strvec(vline);
1146 }
1147
1148 if (matched && matched[index])
1149 /*
1150 * this is free()'d by readline, but we leak 1 count of
1151 * MTYPE_COMPLETION
1152 */
1153 return matched[index++];
1154
1155 XFREE(MTYPE_TMP, matched);
1156
1157 return NULL;
1158 }
1159
1160 static char **new_completion(const char *text, int start, int end)
1161 {
1162 char **matches;
1163
1164 matches = rl_completion_matches(text, command_generator);
1165
1166 if (matches) {
1167 rl_point = rl_end;
1168 if (complete_status != CMD_COMPLETE_FULL_MATCH)
1169 /* only append a space on full match */
1170 rl_completion_append_character = '\0';
1171 }
1172
1173 return matches;
1174 }
1175
1176 /* Vty node structures. */
1177 static struct cmd_node bgp_node = {
1178 BGP_NODE, "%s(config-router)# ",
1179 };
1180
1181 static struct cmd_node rip_node = {
1182 RIP_NODE, "%s(config-router)# ",
1183 };
1184
1185 static struct cmd_node isis_node = {
1186 ISIS_NODE, "%s(config-router)# ",
1187 };
1188
1189 static struct cmd_node openfabric_node = {
1190 OPENFABRIC_NODE, "%s(config-router)# ",
1191 };
1192
1193 static struct cmd_node interface_node = {
1194 INTERFACE_NODE, "%s(config-if)# ",
1195 };
1196
1197 static struct cmd_node pw_node = {
1198 PW_NODE, "%s(config-pw)# ",
1199 };
1200
1201 static struct cmd_node vrf_node = {
1202 VRF_NODE, "%s(config-vrf)# ",
1203 };
1204
1205 static struct cmd_node nh_group_node = {
1206 NH_GROUP_NODE,
1207 "%s(config-nh-group)# ",
1208 };
1209
1210 static struct cmd_node rmap_node = {RMAP_NODE, "%s(config-route-map)# "};
1211
1212 static struct cmd_node pbr_map_node = {PBRMAP_NODE, "%s(config-pbr-map)# "};
1213
1214 static struct cmd_node zebra_node = {ZEBRA_NODE, "%s(config-router)# "};
1215
1216 static struct cmd_node bgp_vpnv4_node = {BGP_VPNV4_NODE,
1217 "%s(config-router-af)# "};
1218
1219 static struct cmd_node bgp_vpnv6_node = {BGP_VPNV6_NODE,
1220 "%s(config-router-af)# "};
1221
1222 static struct cmd_node bgp_flowspecv4_node = {BGP_FLOWSPECV4_NODE,
1223 "%s(config-router-af)# "};
1224
1225 static struct cmd_node bgp_flowspecv6_node = {BGP_FLOWSPECV6_NODE,
1226 "%s(config-router-af)# "};
1227
1228 static struct cmd_node bgp_ipv4_node = {BGP_IPV4_NODE,
1229 "%s(config-router-af)# "};
1230
1231 static struct cmd_node bgp_ipv4m_node = {BGP_IPV4M_NODE,
1232 "%s(config-router-af)# "};
1233
1234 static struct cmd_node bgp_ipv4l_node = {BGP_IPV4L_NODE,
1235 "%s(config-router-af)# "};
1236
1237 static struct cmd_node bgp_ipv6_node = {BGP_IPV6_NODE,
1238 "%s(config-router-af)# "};
1239
1240 static struct cmd_node bgp_ipv6m_node = {BGP_IPV6M_NODE,
1241 "%s(config-router-af)# "};
1242
1243 static struct cmd_node bgp_evpn_node = {BGP_EVPN_NODE,
1244 "%s(config-router-af)# "};
1245
1246 static struct cmd_node bgp_evpn_vni_node = {BGP_EVPN_VNI_NODE,
1247 "%s(config-router-af-vni)# "};
1248
1249 static struct cmd_node bgp_ipv6l_node = {BGP_IPV6L_NODE,
1250 "%s(config-router-af)# "};
1251
1252 static struct cmd_node bgp_vnc_defaults_node = {
1253 BGP_VNC_DEFAULTS_NODE, "%s(config-router-vnc-defaults)# "};
1254
1255 static struct cmd_node bgp_vnc_nve_group_node = {
1256 BGP_VNC_NVE_GROUP_NODE, "%s(config-router-vnc-nve-group)# "};
1257
1258 static struct cmd_node bgp_vrf_policy_node = {BGP_VRF_POLICY_NODE,
1259 "%s(config-router-vrf-policy)# "};
1260
1261 static struct cmd_node bgp_vnc_l2_group_node = {
1262 BGP_VNC_L2_GROUP_NODE, "%s(config-router-vnc-l2-group)# "};
1263
1264 static struct cmd_node bmp_node = {BMP_NODE, "%s(config-bgp-bmp)# "};
1265
1266 static struct cmd_node ospf_node = {OSPF_NODE, "%s(config-router)# "};
1267
1268 static struct cmd_node eigrp_node = {EIGRP_NODE, "%s(config-router)# "};
1269
1270 static struct cmd_node babel_node = {BABEL_NODE, "%s(config-router)# "};
1271
1272 static struct cmd_node ripng_node = {RIPNG_NODE, "%s(config-router)# "};
1273
1274 static struct cmd_node ospf6_node = {OSPF6_NODE, "%s(config-ospf6)# "};
1275
1276 static struct cmd_node ldp_node = {LDP_NODE, "%s(config-ldp)# "};
1277
1278 static struct cmd_node ldp_ipv4_node = {LDP_IPV4_NODE, "%s(config-ldp-af)# "};
1279
1280 static struct cmd_node ldp_ipv6_node = {LDP_IPV6_NODE, "%s(config-ldp-af)# "};
1281
1282 static struct cmd_node ldp_ipv4_iface_node = {LDP_IPV4_IFACE_NODE,
1283 "%s(config-ldp-af-if)# "};
1284
1285 static struct cmd_node ldp_ipv6_iface_node = {LDP_IPV6_IFACE_NODE,
1286 "%s(config-ldp-af-if)# "};
1287
1288 static struct cmd_node ldp_l2vpn_node = {LDP_L2VPN_NODE, "%s(config-l2vpn)# "};
1289
1290 static struct cmd_node ldp_pseudowire_node = {LDP_PSEUDOWIRE_NODE,
1291 "%s(config-l2vpn-pw)# "};
1292
1293 static struct cmd_node keychain_node = {KEYCHAIN_NODE, "%s(config-keychain)# "};
1294
1295 static struct cmd_node keychain_key_node = {KEYCHAIN_KEY_NODE,
1296 "%s(config-keychain-key)# "};
1297
1298 struct cmd_node link_params_node = {
1299 LINK_PARAMS_NODE, "%s(config-link-params)# ",
1300 };
1301
1302 static struct cmd_node rpki_node = {RPKI_NODE, "%s(config-rpki)# ", 1};
1303
1304 #if HAVE_BFDD > 0
1305 static struct cmd_node bfd_node = {
1306 BFD_NODE,
1307 "%s(config-bfd)# ",
1308 };
1309
1310 static struct cmd_node bfd_peer_node = {
1311 BFD_PEER_NODE,
1312 "%s(config-bfd-peer)# ",
1313 };
1314 #endif /* HAVE_BFDD */
1315
1316 /* Defined in lib/vty.c */
1317 extern struct cmd_node vty_node;
1318
1319 /* When '^Z' is received from vty, move down to the enable mode. */
1320 static int vtysh_end(void)
1321 {
1322 switch (vty->node) {
1323 case VIEW_NODE:
1324 case ENABLE_NODE:
1325 /* Nothing to do. */
1326 break;
1327 default:
1328 vty->node = ENABLE_NODE;
1329 break;
1330 }
1331 return CMD_SUCCESS;
1332 }
1333
1334 DEFUNSH(VTYSH_REALLYALL, vtysh_end_all, vtysh_end_all_cmd, "end",
1335 "End current mode and change to enable mode\n")
1336 {
1337 return vtysh_end();
1338 }
1339
1340 DEFUNSH(VTYSH_BGPD, router_bgp, router_bgp_cmd,
1341 "router bgp [(1-4294967295) [<view|vrf> WORD]]",
1342 ROUTER_STR BGP_STR AS_STR
1343 "BGP view\nBGP VRF\n"
1344 "View/VRF name\n")
1345 {
1346 vty->node = BGP_NODE;
1347 return CMD_SUCCESS;
1348 }
1349
1350 #ifdef KEEP_OLD_VPN_COMMANDS
1351 DEFUNSH(VTYSH_BGPD, address_family_vpnv4, address_family_vpnv4_cmd,
1352 "address-family vpnv4 [unicast]",
1353 "Enter Address Family command mode\n"
1354 "Address Family\n"
1355 "Address Family modifier\n")
1356 {
1357 vty->node = BGP_VPNV4_NODE;
1358 return CMD_SUCCESS;
1359 }
1360
1361 DEFUNSH(VTYSH_BGPD, address_family_vpnv6, address_family_vpnv6_cmd,
1362 "address-family vpnv6 [unicast]",
1363 "Enter Address Family command mode\n"
1364 "Address Family\n"
1365 "Address Family modifier\n")
1366 {
1367 vty->node = BGP_VPNV6_NODE;
1368 return CMD_SUCCESS;
1369 }
1370 #endif /* KEEP_OLD_VPN_COMMANDS */
1371
1372 DEFUNSH(VTYSH_BGPD, address_family_ipv4, address_family_ipv4_cmd,
1373 "address-family ipv4 [unicast]",
1374 "Enter Address Family command mode\n"
1375 "Address Family\n"
1376 "Address Family Modifier\n")
1377 {
1378 vty->node = BGP_IPV4_NODE;
1379 return CMD_SUCCESS;
1380 }
1381
1382 DEFUNSH(VTYSH_BGPD, address_family_flowspecv4, address_family_flowspecv4_cmd,
1383 "address-family ipv4 flowspec",
1384 "Enter Address Family command mode\n"
1385 "Address Family\n"
1386 "Address Family Modifier\n")
1387 {
1388 vty->node = BGP_FLOWSPECV4_NODE;
1389 return CMD_SUCCESS;
1390 }
1391
1392 DEFUNSH(VTYSH_BGPD, address_family_flowspecv6, address_family_flowspecv6_cmd,
1393 "address-family ipv6 flowspec",
1394 "Enter Address Family command mode\n"
1395 "Address Family\n"
1396 "Address Family Modifier\n")
1397 {
1398 vty->node = BGP_FLOWSPECV6_NODE;
1399 return CMD_SUCCESS;
1400 }
1401
1402 DEFUNSH(VTYSH_BGPD, address_family_ipv4_multicast,
1403 address_family_ipv4_multicast_cmd, "address-family ipv4 multicast",
1404 "Enter Address Family command mode\n"
1405 "Address Family\n"
1406 "Address Family modifier\n")
1407 {
1408 vty->node = BGP_IPV4M_NODE;
1409 return CMD_SUCCESS;
1410 }
1411
1412 DEFUNSH(VTYSH_BGPD, address_family_ipv4_vpn, address_family_ipv4_vpn_cmd,
1413 "address-family ipv4 vpn",
1414 "Enter Address Family command mode\n"
1415 "Address Family\n"
1416 "Address Family modifier\n")
1417 {
1418 vty->node = BGP_VPNV4_NODE;
1419 return CMD_SUCCESS;
1420 }
1421
1422 DEFUNSH(VTYSH_BGPD, address_family_ipv4_labeled_unicast,
1423 address_family_ipv4_labeled_unicast_cmd,
1424 "address-family ipv4 labeled-unicast",
1425 "Enter Address Family command mode\n"
1426 "Address Family\n"
1427 "Address Family modifier\n")
1428 {
1429 vty->node = BGP_IPV4L_NODE;
1430 return CMD_SUCCESS;
1431 }
1432
1433 DEFUNSH(VTYSH_BGPD, address_family_ipv6, address_family_ipv6_cmd,
1434 "address-family ipv6 [unicast]",
1435 "Enter Address Family command mode\n"
1436 "Address Family\n"
1437 "Address Family modifier\n")
1438 {
1439 vty->node = BGP_IPV6_NODE;
1440 return CMD_SUCCESS;
1441 }
1442
1443 DEFUNSH(VTYSH_BGPD, address_family_ipv6_multicast,
1444 address_family_ipv6_multicast_cmd, "address-family ipv6 multicast",
1445 "Enter Address Family command mode\n"
1446 "Address Family\n"
1447 "Address Family modifier\n")
1448 {
1449 vty->node = BGP_IPV6M_NODE;
1450 return CMD_SUCCESS;
1451 }
1452
1453 DEFUNSH(VTYSH_BGPD, address_family_ipv6_vpn, address_family_ipv6_vpn_cmd,
1454 "address-family ipv6 vpn",
1455 "Enter Address Family command mode\n"
1456 "Address Family\n"
1457 "Address Family modifier\n")
1458 {
1459 vty->node = BGP_VPNV6_NODE;
1460 return CMD_SUCCESS;
1461 }
1462
1463 DEFUNSH(VTYSH_BGPD, address_family_ipv6_labeled_unicast,
1464 address_family_ipv6_labeled_unicast_cmd,
1465 "address-family ipv6 labeled-unicast",
1466 "Enter Address Family command mode\n"
1467 "Address Family\n"
1468 "Address Family modifier\n")
1469 {
1470 vty->node = BGP_IPV6L_NODE;
1471 return CMD_SUCCESS;
1472 }
1473
1474 DEFUNSH(VTYSH_BGPD,
1475 rpki,
1476 rpki_cmd,
1477 "rpki",
1478 "Enable rpki and enter rpki configuration mode\n")
1479 {
1480 vty->node = RPKI_NODE;
1481 return CMD_SUCCESS;
1482 }
1483
1484 DEFUNSH(VTYSH_BGPD,
1485 bmp_targets,
1486 bmp_targets_cmd,
1487 "bmp targets BMPTARGETS",
1488 "BGP Monitoring Protocol\n"
1489 "Create BMP target group\n"
1490 "Name of the BMP target group\n")
1491 {
1492 vty->node = BMP_NODE;
1493 return CMD_SUCCESS;
1494 }
1495
1496 DEFUNSH(VTYSH_BGPD, address_family_evpn, address_family_evpn_cmd,
1497 "address-family <l2vpn evpn>",
1498 "Enter Address Family command mode\n"
1499 "Address Family\n"
1500 "Address Family modifier\n")
1501 {
1502 vty->node = BGP_EVPN_NODE;
1503 return CMD_SUCCESS;
1504 }
1505
1506 #if defined(HAVE_CUMULUS)
1507 DEFUNSH_HIDDEN(VTYSH_BGPD, address_family_evpn2, address_family_evpn2_cmd,
1508 "address-family evpn",
1509 "Enter Address Family command mode\n"
1510 "EVPN Address family\n")
1511 {
1512 vty->node = BGP_EVPN_NODE;
1513 return CMD_SUCCESS;
1514 }
1515 #endif
1516
1517 DEFUNSH(VTYSH_BGPD, bgp_evpn_vni, bgp_evpn_vni_cmd, "vni " CMD_VNI_RANGE,
1518 "VXLAN Network Identifier\n"
1519 "VNI number\n")
1520 {
1521 vty->node = BGP_EVPN_VNI_NODE;
1522 return CMD_SUCCESS;
1523 }
1524
1525 #if defined(ENABLE_BGP_VNC)
1526 DEFUNSH(VTYSH_BGPD, vnc_defaults, vnc_defaults_cmd, "vnc defaults",
1527 "VNC/RFP related configuration\n"
1528 "Configure default NVE group\n")
1529 {
1530 vty->node = BGP_VNC_DEFAULTS_NODE;
1531 return CMD_SUCCESS;
1532 }
1533
1534 DEFUNSH(VTYSH_BGPD, vnc_nve_group, vnc_nve_group_cmd, "vnc nve-group NAME",
1535 "VNC/RFP related configuration\n"
1536 "Configure a NVE group\n"
1537 "Group name\n")
1538 {
1539 vty->node = BGP_VNC_NVE_GROUP_NODE;
1540 return CMD_SUCCESS;
1541 }
1542
1543 DEFUNSH(VTYSH_BGPD, vnc_vrf_policy, vnc_vrf_policy_cmd, "vrf-policy NAME",
1544 "Configure a VRF policy group\n"
1545 "Group name\n")
1546 {
1547 vty->node = BGP_VRF_POLICY_NODE;
1548 return CMD_SUCCESS;
1549 }
1550
1551 DEFUNSH(VTYSH_BGPD, vnc_l2_group, vnc_l2_group_cmd, "vnc l2-group NAME",
1552 "VNC/RFP related configuration\n"
1553 "Configure a L2 group\n"
1554 "Group name\n")
1555 {
1556 vty->node = BGP_VNC_L2_GROUP_NODE;
1557 return CMD_SUCCESS;
1558 }
1559 #endif
1560
1561 DEFUNSH(VTYSH_KEYS, key_chain, key_chain_cmd, "key chain WORD",
1562 "Authentication key management\n"
1563 "Key-chain management\n"
1564 "Key-chain name\n")
1565 {
1566 vty->node = KEYCHAIN_NODE;
1567 return CMD_SUCCESS;
1568 }
1569
1570 DEFUNSH(VTYSH_KEYS, key, key_cmd, "key (0-2147483647)",
1571 "Configure a key\n"
1572 "Key identifier number\n")
1573 {
1574 vty->node = KEYCHAIN_KEY_NODE;
1575 return CMD_SUCCESS;
1576 }
1577
1578 DEFUNSH(VTYSH_RIPD, router_rip, router_rip_cmd, "router rip [vrf NAME]",
1579 ROUTER_STR "RIP\n" VRF_CMD_HELP_STR)
1580 {
1581 vty->node = RIP_NODE;
1582 return CMD_SUCCESS;
1583 }
1584
1585 DEFUNSH(VTYSH_RIPNGD, router_ripng, router_ripng_cmd, "router ripng [vrf NAME]",
1586 ROUTER_STR "RIPng\n" VRF_CMD_HELP_STR)
1587 {
1588 vty->node = RIPNG_NODE;
1589 return CMD_SUCCESS;
1590 }
1591
1592 DEFUNSH(VTYSH_OSPFD, router_ospf, router_ospf_cmd,
1593 "router ospf [(1-65535)] [vrf NAME]",
1594 "Enable a routing process\n"
1595 "Start OSPF configuration\n"
1596 "Instance ID\n"
1597 VRF_CMD_HELP_STR)
1598 {
1599 vty->node = OSPF_NODE;
1600 return CMD_SUCCESS;
1601 }
1602
1603 DEFUNSH(VTYSH_EIGRPD, router_eigrp, router_eigrp_cmd, "router eigrp (1-65535) [vrf NAME]",
1604 "Enable a routing process\n"
1605 "Start EIGRP configuration\n"
1606 "AS number to use\n"
1607 VRF_CMD_HELP_STR)
1608 {
1609 vty->node = EIGRP_NODE;
1610 return CMD_SUCCESS;
1611 }
1612
1613 DEFUNSH(VTYSH_BABELD, router_babel, router_babel_cmd, "router babel",
1614 "Enable a routing process\n"
1615 "Make Babel instance command\n")
1616 {
1617 vty->node = BABEL_NODE;
1618 return CMD_SUCCESS;
1619 }
1620
1621 DEFUNSH(VTYSH_OSPF6D, router_ospf6, router_ospf6_cmd, "router ospf6",
1622 ROUTER_STR OSPF6_STR)
1623 {
1624 vty->node = OSPF6_NODE;
1625 return CMD_SUCCESS;
1626 }
1627
1628 #if defined(HAVE_LDPD)
1629 DEFUNSH(VTYSH_LDPD, ldp_mpls_ldp, ldp_mpls_ldp_cmd, "mpls ldp",
1630 "Global MPLS configuration subcommands\n"
1631 "Label Distribution Protocol\n")
1632 {
1633 vty->node = LDP_NODE;
1634 return CMD_SUCCESS;
1635 }
1636
1637 DEFUNSH(VTYSH_LDPD, ldp_address_family_ipv4, ldp_address_family_ipv4_cmd,
1638 "address-family ipv4",
1639 "Configure Address Family and its parameters\n"
1640 "IPv4\n")
1641 {
1642 vty->node = LDP_IPV4_NODE;
1643 return CMD_SUCCESS;
1644 }
1645
1646 DEFUNSH(VTYSH_LDPD, ldp_address_family_ipv6, ldp_address_family_ipv6_cmd,
1647 "address-family ipv6",
1648 "Configure Address Family and its parameters\n"
1649 "IPv6\n")
1650 {
1651 vty->node = LDP_IPV6_NODE;
1652 return CMD_SUCCESS;
1653 }
1654
1655 DEFUNSH(VTYSH_LDPD, ldp_exit_address_family, ldp_exit_address_family_cmd,
1656 "exit-address-family", "Exit from Address Family configuration mode\n")
1657 {
1658 if (vty->node == LDP_IPV4_NODE || vty->node == LDP_IPV6_NODE)
1659 vty->node = LDP_NODE;
1660 return CMD_SUCCESS;
1661 }
1662
1663 DEFUNSH(VTYSH_LDPD, ldp_interface_ifname, ldp_interface_ifname_cmd,
1664 "interface IFNAME",
1665 "Enable LDP on an interface and enter interface submode\n"
1666 "Interface's name\n")
1667 {
1668 switch (vty->node) {
1669 case LDP_IPV4_NODE:
1670 vty->node = LDP_IPV4_IFACE_NODE;
1671 break;
1672 case LDP_IPV6_NODE:
1673 vty->node = LDP_IPV6_IFACE_NODE;
1674 break;
1675 default:
1676 break;
1677 }
1678
1679 return CMD_SUCCESS;
1680 }
1681
1682 DEFUNSH(VTYSH_LDPD, ldp_l2vpn_word_type_vpls, ldp_l2vpn_word_type_vpls_cmd,
1683 "l2vpn WORD type vpls",
1684 "Configure l2vpn commands\n"
1685 "L2VPN name\n"
1686 "L2VPN type\n"
1687 "Virtual Private LAN Service\n")
1688 {
1689 vty->node = LDP_L2VPN_NODE;
1690 return CMD_SUCCESS;
1691 }
1692
1693 DEFUNSH(VTYSH_LDPD, ldp_member_pseudowire_ifname,
1694 ldp_member_pseudowire_ifname_cmd, "member pseudowire IFNAME",
1695 "L2VPN member configuration\n"
1696 "Pseudowire interface\n"
1697 "Interface's name\n")
1698 {
1699 vty->node = LDP_PSEUDOWIRE_NODE;
1700 return CMD_SUCCESS;
1701 }
1702 #endif
1703
1704 DEFUNSH(VTYSH_ISISD, router_isis, router_isis_cmd, "router isis WORD",
1705 ROUTER_STR
1706 "ISO IS-IS\n"
1707 "ISO Routing area tag\n")
1708 {
1709 vty->node = ISIS_NODE;
1710 return CMD_SUCCESS;
1711 }
1712
1713 DEFUNSH(VTYSH_FABRICD, router_openfabric, router_openfabric_cmd, "router openfabric WORD",
1714 ROUTER_STR
1715 "OpenFabric routing protocol\n"
1716 "ISO Routing area tag\n")
1717 {
1718 vty->node = OPENFABRIC_NODE;
1719 return CMD_SUCCESS;
1720 }
1721
1722 DEFUNSH(VTYSH_RMAP, vtysh_route_map, vtysh_route_map_cmd,
1723 "route-map WORD <deny|permit> (1-65535)",
1724 "Create route-map or enter route-map command mode\n"
1725 "Route map tag\n"
1726 "Route map denies set operations\n"
1727 "Route map permits set operations\n"
1728 "Sequence to insert to/delete from existing route-map entry\n")
1729 {
1730 vty->node = RMAP_NODE;
1731 return CMD_SUCCESS;
1732 }
1733
1734 DEFUNSH(VTYSH_PBRD, vtysh_pbr_map, vtysh_pbr_map_cmd,
1735 "pbr-map PBRMAP seq (1-700)",
1736 "Create pbr-map or enter pbr-map command mode\n"
1737 "The name of the PBR MAP\n"
1738 "Sequence to insert to/delete from existing pbr-map entry\n"
1739 "Sequence number\n")
1740 {
1741 vty->node = PBRMAP_NODE;
1742 return CMD_SUCCESS;
1743 }
1744
1745 #if HAVE_BFDD > 0
1746 DEFUNSH(VTYSH_BFDD, bfd_enter, bfd_enter_cmd, "bfd", "Configure BFD peers\n")
1747 {
1748 vty->node = BFD_NODE;
1749 return CMD_SUCCESS;
1750 }
1751
1752 DEFUNSH(VTYSH_BFDD, bfd_peer_enter, bfd_peer_enter_cmd,
1753 "peer <A.B.C.D|X:X::X:X> [{multihop|local-address <A.B.C.D|X:X::X:X>|interface IFNAME|vrf NAME}]",
1754 "Configure peer\n"
1755 "IPv4 peer address\n"
1756 "IPv6 peer address\n"
1757 "Configure multihop\n"
1758 "Configure local address\n"
1759 "IPv4 local address\n"
1760 "IPv6 local address\n"
1761 INTERFACE_STR
1762 "Configure interface name to use\n"
1763 "Configure VRF\n"
1764 "Configure VRF name\n")
1765 {
1766 vty->node = BFD_PEER_NODE;
1767 return CMD_SUCCESS;
1768 }
1769 #endif /* HAVE_BFDD */
1770
1771 DEFSH(VTYSH_PBRD, vtysh_no_pbr_map_cmd, "no pbr-map PBRMAP [seq (1-700)]",
1772 NO_STR
1773 "Delete pbr-map\n"
1774 "The name of the PBR MAP\n"
1775 "Sequence to delete from existing pbr-map entry\n"
1776 "Sequence number\n")
1777
1778 DEFUNSH(VTYSH_ALL, vtysh_line_vty, vtysh_line_vty_cmd, "line vty",
1779 "Configure a terminal line\n"
1780 "Virtual terminal\n")
1781 {
1782 vty->node = VTY_NODE;
1783 return CMD_SUCCESS;
1784 }
1785
1786 DEFUNSH(VTYSH_REALLYALL, vtysh_enable, vtysh_enable_cmd, "enable",
1787 "Turn on privileged mode command\n")
1788 {
1789 vty->node = ENABLE_NODE;
1790 return CMD_SUCCESS;
1791 }
1792
1793 DEFUNSH(VTYSH_REALLYALL, vtysh_disable, vtysh_disable_cmd, "disable",
1794 "Turn off privileged mode command\n")
1795 {
1796 if (vty->node == ENABLE_NODE)
1797 vty->node = VIEW_NODE;
1798 return CMD_SUCCESS;
1799 }
1800
1801 DEFUNSH(VTYSH_REALLYALL, vtysh_config_terminal, vtysh_config_terminal_cmd,
1802 "configure [terminal]",
1803 "Configuration from vty interface\n"
1804 "Configuration terminal\n")
1805 {
1806 vty->node = CONFIG_NODE;
1807 return CMD_SUCCESS;
1808 }
1809
1810 static int vtysh_exit(struct vty *vty)
1811 {
1812 switch (vty->node) {
1813 case VIEW_NODE:
1814 case ENABLE_NODE:
1815 exit(0);
1816 break;
1817 case CONFIG_NODE:
1818 vty->node = ENABLE_NODE;
1819 break;
1820 case INTERFACE_NODE:
1821 case PW_NODE:
1822 case VRF_NODE:
1823 case NH_GROUP_NODE:
1824 case ZEBRA_NODE:
1825 case BGP_NODE:
1826 case RIP_NODE:
1827 case RIPNG_NODE:
1828 case OSPF_NODE:
1829 case OSPF6_NODE:
1830 case EIGRP_NODE:
1831 case BABEL_NODE:
1832 case LDP_NODE:
1833 case LDP_L2VPN_NODE:
1834 case ISIS_NODE:
1835 case OPENFABRIC_NODE:
1836 case RMAP_NODE:
1837 case PBRMAP_NODE:
1838 case VTY_NODE:
1839 case KEYCHAIN_NODE:
1840 case BFD_NODE:
1841 case RPKI_NODE:
1842 vtysh_execute("end");
1843 vtysh_execute("configure");
1844 vty->node = CONFIG_NODE;
1845 break;
1846 case BGP_VPNV4_NODE:
1847 case BGP_VPNV6_NODE:
1848 case BGP_IPV4_NODE:
1849 case BGP_IPV4M_NODE:
1850 case BGP_IPV4L_NODE:
1851 case BGP_IPV6_NODE:
1852 case BGP_IPV6M_NODE:
1853 case BGP_IPV6L_NODE:
1854 case BGP_FLOWSPECV4_NODE:
1855 case BGP_FLOWSPECV6_NODE:
1856 case BGP_VRF_POLICY_NODE:
1857 case BGP_EVPN_NODE:
1858 case BGP_VNC_DEFAULTS_NODE:
1859 case BGP_VNC_NVE_GROUP_NODE:
1860 case BGP_VNC_L2_GROUP_NODE:
1861 case BMP_NODE:
1862 vty->node = BGP_NODE;
1863 break;
1864 case BGP_EVPN_VNI_NODE:
1865 vty->node = BGP_EVPN_NODE;
1866 break;
1867 case LDP_IPV4_NODE:
1868 case LDP_IPV6_NODE:
1869 vty->node = LDP_NODE;
1870 break;
1871 case LDP_IPV4_IFACE_NODE:
1872 vty->node = LDP_IPV4_NODE;
1873 break;
1874 case LDP_IPV6_IFACE_NODE:
1875 vty->node = LDP_IPV6_NODE;
1876 break;
1877 case LDP_PSEUDOWIRE_NODE:
1878 vty->node = LDP_L2VPN_NODE;
1879 break;
1880 case KEYCHAIN_KEY_NODE:
1881 vty->node = KEYCHAIN_NODE;
1882 break;
1883 case LINK_PARAMS_NODE:
1884 vty->node = INTERFACE_NODE;
1885 break;
1886 case BFD_PEER_NODE:
1887 vty->node = BFD_NODE;
1888 break;
1889 default:
1890 break;
1891 }
1892 return CMD_SUCCESS;
1893 }
1894
1895 DEFUNSH(VTYSH_REALLYALL, vtysh_exit_all, vtysh_exit_all_cmd, "exit",
1896 "Exit current mode and down to previous mode\n")
1897 {
1898 return vtysh_exit(vty);
1899 }
1900
1901 DEFUNSH(VTYSH_ALL, vtysh_quit_all, vtysh_quit_all_cmd, "quit",
1902 "Exit current mode and down to previous mode\n")
1903 {
1904 return vtysh_exit_all(self, vty, argc, argv);
1905 }
1906
1907 DEFUNSH(VTYSH_BGPD, exit_address_family, exit_address_family_cmd,
1908 "exit-address-family", "Exit from Address Family configuration mode\n")
1909 {
1910 if (vty->node == BGP_IPV4_NODE || vty->node == BGP_IPV4M_NODE
1911 || vty->node == BGP_IPV4L_NODE || vty->node == BGP_VPNV4_NODE
1912 || vty->node == BGP_VPNV6_NODE || vty->node == BGP_IPV6_NODE
1913 || vty->node == BGP_IPV6L_NODE || vty->node == BGP_IPV6M_NODE
1914 || vty->node == BGP_EVPN_NODE
1915 || vty->node == BGP_FLOWSPECV4_NODE
1916 || vty->node == BGP_FLOWSPECV6_NODE)
1917 vty->node = BGP_NODE;
1918 return CMD_SUCCESS;
1919 }
1920
1921 DEFUNSH(VTYSH_BGPD, exit_vni, exit_vni_cmd, "exit-vni", "Exit from VNI mode\n")
1922 {
1923 if (vty->node == BGP_EVPN_VNI_NODE)
1924 vty->node = BGP_EVPN_NODE;
1925 return CMD_SUCCESS;
1926 }
1927
1928 DEFUNSH(VTYSH_BGPD, exit_vnc_config, exit_vnc_config_cmd, "exit-vnc",
1929 "Exit from VNC configuration mode\n")
1930 {
1931 if (vty->node == BGP_VNC_DEFAULTS_NODE
1932 || vty->node == BGP_VNC_NVE_GROUP_NODE
1933 || vty->node == BGP_VNC_L2_GROUP_NODE)
1934 vty->node = BGP_NODE;
1935 return CMD_SUCCESS;
1936
1937 }
1938
1939 DEFUNSH(VTYSH_BGPD, rpki_exit, rpki_exit_cmd, "exit",
1940 "Exit current mode and down to previous mode\n")
1941 {
1942 vtysh_exit(vty);
1943 return CMD_SUCCESS;
1944 }
1945
1946 DEFUNSH(VTYSH_BGPD, rpki_quit, rpki_quit_cmd, "quit",
1947 "Exit current mode and down to previous mode\n")
1948 {
1949 return rpki_exit(self, vty, argc, argv);
1950 }
1951
1952 DEFUNSH(VTYSH_BGPD, bmp_exit, bmp_exit_cmd, "exit",
1953 "Exit current mode and down to previous mode\n")
1954 {
1955 vtysh_exit(vty);
1956 return CMD_SUCCESS;
1957 }
1958
1959 DEFUNSH(VTYSH_BGPD, bmp_quit, bmp_quit_cmd, "quit",
1960 "Exit current mode and down to previous mode\n")
1961 {
1962 return bmp_exit(self, vty, argc, argv);
1963 }
1964
1965 DEFUNSH(VTYSH_VRF, exit_vrf_config, exit_vrf_config_cmd, "exit-vrf",
1966 "Exit from VRF configuration mode\n")
1967 {
1968 if (vty->node == VRF_NODE)
1969 vty->node = CONFIG_NODE;
1970 return CMD_SUCCESS;
1971 }
1972
1973 DEFUNSH(VTYSH_BGPD, exit_vrf_policy, exit_vrf_policy_cmd, "exit-vrf-policy",
1974 "Exit from VRF policy configuration mode\n")
1975 {
1976 if (vty->node == BGP_VRF_POLICY_NODE)
1977 vty->node = BGP_NODE;
1978 return CMD_SUCCESS;
1979 }
1980
1981 DEFUNSH(VTYSH_RIPD, vtysh_exit_ripd, vtysh_exit_ripd_cmd, "exit",
1982 "Exit current mode and down to previous mode\n")
1983 {
1984 return vtysh_exit(vty);
1985 }
1986
1987 DEFUNSH(VTYSH_RIPD, vtysh_quit_ripd, vtysh_quit_ripd_cmd, "quit",
1988 "Exit current mode and down to previous mode\n")
1989 {
1990 return vtysh_exit_ripd(self, vty, argc, argv);
1991 }
1992
1993 DEFUNSH(VTYSH_RIPNGD, vtysh_exit_ripngd, vtysh_exit_ripngd_cmd, "exit",
1994 "Exit current mode and down to previous mode\n")
1995 {
1996 return vtysh_exit(vty);
1997 }
1998
1999 DEFUNSH(VTYSH_RIPNGD, vtysh_quit_ripngd, vtysh_quit_ripngd_cmd, "quit",
2000 "Exit current mode and down to previous mode\n")
2001 {
2002 return vtysh_exit_ripngd(self, vty, argc, argv);
2003 }
2004
2005 DEFUNSH(VTYSH_RMAP, vtysh_exit_rmap, vtysh_exit_rmap_cmd, "exit",
2006 "Exit current mode and down to previous mode\n")
2007 {
2008 return vtysh_exit(vty);
2009 }
2010
2011 DEFUNSH(VTYSH_RMAP, vtysh_quit_rmap, vtysh_quit_rmap_cmd, "quit",
2012 "Exit current mode and down to previous mode\n")
2013 {
2014 return vtysh_exit_rmap(self, vty, argc, argv);
2015 }
2016
2017 DEFUNSH(VTYSH_PBRD, vtysh_exit_pbr_map, vtysh_exit_pbr_map_cmd, "exit",
2018 "Exit current mode and down to previous mode\n")
2019 {
2020 return vtysh_exit(vty);
2021 }
2022
2023 DEFUNSH(VTYSH_PBRD, vtysh_quit_pbr_map, vtysh_quit_pbr_map_cmd, "quit",
2024 "Exit current mode and down to previous mode\n")
2025 {
2026 return vtysh_exit_rmap(self, vty, argc, argv);
2027 }
2028
2029 DEFUNSH(VTYSH_BGPD, vtysh_exit_bgpd, vtysh_exit_bgpd_cmd, "exit",
2030 "Exit current mode and down to previous mode\n")
2031 {
2032 return vtysh_exit(vty);
2033 }
2034
2035 DEFUNSH(VTYSH_BGPD, vtysh_quit_bgpd, vtysh_quit_bgpd_cmd, "quit",
2036 "Exit current mode and down to previous mode\n")
2037 {
2038 return vtysh_exit_bgpd(self, vty, argc, argv);
2039 }
2040
2041 DEFUNSH(VTYSH_OSPFD, vtysh_exit_ospfd, vtysh_exit_ospfd_cmd, "exit",
2042 "Exit current mode and down to previous mode\n")
2043 {
2044 return vtysh_exit(vty);
2045 }
2046
2047 DEFUNSH(VTYSH_OSPFD, vtysh_quit_ospfd, vtysh_quit_ospfd_cmd, "quit",
2048 "Exit current mode and down to previous mode\n")
2049 {
2050 return vtysh_exit_ospfd(self, vty, argc, argv);
2051 }
2052
2053 DEFUNSH(VTYSH_EIGRPD, vtysh_exit_eigrpd, vtysh_exit_eigrpd_cmd, "exit",
2054 "Exit current mode and down to previous mode\n")
2055 {
2056 return vtysh_exit(vty);
2057 }
2058
2059 DEFUNSH(VTYSH_EIGRPD, vtysh_quit_eigrpd, vtysh_quit_eigrpd_cmd, "quit",
2060 "Exit current mode and down to previous mode\n")
2061 {
2062 return vtysh_exit(vty);
2063 }
2064
2065 DEFUNSH(VTYSH_EIGRPD, vtysh_exit_babeld, vtysh_exit_babeld_cmd, "exit",
2066 "Exit current mode and down to previous mode\n")
2067 {
2068 return vtysh_exit(vty);
2069 }
2070
2071 DEFUNSH(VTYSH_BABELD, vtysh_quit_babeld, vtysh_quit_babeld_cmd, "quit",
2072 "Exit current mode and down to previous mode\n")
2073 {
2074 return vtysh_exit(vty);
2075 }
2076
2077 DEFUNSH(VTYSH_OSPF6D, vtysh_exit_ospf6d, vtysh_exit_ospf6d_cmd, "exit",
2078 "Exit current mode and down to previous mode\n")
2079 {
2080 return vtysh_exit(vty);
2081 }
2082
2083 DEFUNSH(VTYSH_OSPF6D, vtysh_quit_ospf6d, vtysh_quit_ospf6d_cmd, "quit",
2084 "Exit current mode and down to previous mode\n")
2085 {
2086 return vtysh_exit_ospf6d(self, vty, argc, argv);
2087 }
2088
2089 #if defined(HAVE_LDPD)
2090 DEFUNSH(VTYSH_LDPD, vtysh_exit_ldpd, vtysh_exit_ldpd_cmd, "exit",
2091 "Exit current mode and down to previous mode\n")
2092 {
2093 return vtysh_exit(vty);
2094 }
2095
2096 ALIAS(vtysh_exit_ldpd, vtysh_quit_ldpd_cmd, "quit",
2097 "Exit current mode and down to previous mode\n")
2098 #endif
2099
2100 DEFUNSH(VTYSH_ISISD, vtysh_exit_isisd, vtysh_exit_isisd_cmd, "exit",
2101 "Exit current mode and down to previous mode\n")
2102 {
2103 return vtysh_exit(vty);
2104 }
2105
2106 DEFUNSH(VTYSH_ISISD, vtysh_quit_isisd, vtysh_quit_isisd_cmd, "quit",
2107 "Exit current mode and down to previous mode\n")
2108 {
2109 return vtysh_exit_isisd(self, vty, argc, argv);
2110 }
2111
2112 #if HAVE_BFDD > 0
2113 DEFUNSH(VTYSH_BFDD, vtysh_exit_bfdd, vtysh_exit_bfdd_cmd, "exit",
2114 "Exit current mode and down to previous mode\n")
2115 {
2116 return vtysh_exit(vty);
2117 }
2118
2119 ALIAS(vtysh_exit_bfdd, vtysh_quit_bfdd_cmd, "quit",
2120 "Exit current mode and down to previous mode\n")
2121 #endif
2122
2123 DEFUNSH(VTYSH_FABRICD, vtysh_exit_fabricd, vtysh_exit_fabricd_cmd, "exit",
2124 "Exit current mode and down to previous mode\n")
2125 {
2126 return vtysh_exit(vty);
2127 }
2128
2129 DEFUNSH(VTYSH_FABRICD, vtysh_quit_fabricd, vtysh_quit_fabricd_cmd, "quit",
2130 "Exit current mode and down to previous mode\n")
2131 {
2132 return vtysh_exit_fabricd(self, vty, argc, argv);
2133 }
2134
2135 DEFUNSH(VTYSH_ALL, vtysh_exit_line_vty, vtysh_exit_line_vty_cmd, "exit",
2136 "Exit current mode and down to previous mode\n")
2137 {
2138 return vtysh_exit(vty);
2139 }
2140
2141 DEFUNSH(VTYSH_ALL, vtysh_quit_line_vty, vtysh_quit_line_vty_cmd, "quit",
2142 "Exit current mode and down to previous mode\n")
2143 {
2144 return vtysh_exit_line_vty(self, vty, argc, argv);
2145 }
2146
2147 DEFUNSH(VTYSH_INTERFACE, vtysh_interface, vtysh_interface_cmd,
2148 "interface IFNAME [vrf NAME]",
2149 "Select an interface to configure\n"
2150 "Interface's name\n" VRF_CMD_HELP_STR)
2151 {
2152 vty->node = INTERFACE_NODE;
2153 return CMD_SUCCESS;
2154 }
2155
2156 DEFUNSH(VTYSH_ZEBRA, vtysh_pseudowire, vtysh_pseudowire_cmd,
2157 "pseudowire IFNAME",
2158 "Static pseudowire configuration\n"
2159 "Pseudowire name\n")
2160 {
2161 vty->node = PW_NODE;
2162 return CMD_SUCCESS;
2163 }
2164
2165 DEFUNSH(VTYSH_NH_GROUP,
2166 vtysh_nexthop_group, vtysh_nexthop_group_cmd,
2167 "nexthop-group NHGNAME",
2168 "Nexthop Group configuration\n"
2169 "Name of the Nexthop Group\n")
2170 {
2171 vty->node = NH_GROUP_NODE;
2172 return CMD_SUCCESS;
2173 }
2174
2175 DEFSH(VTYSH_NH_GROUP, vtysh_no_nexthop_group_cmd,
2176 "no nexthop-group NHGNAME",
2177 NO_STR
2178 "Nexthop Group Configuration\n"
2179 "Name of the Nexthop Group\n")
2180
2181 DEFUNSH(VTYSH_VRF, vtysh_vrf, vtysh_vrf_cmd, "vrf NAME",
2182 "Select a VRF to configure\n"
2183 "VRF's name\n")
2184 {
2185 vty->node = VRF_NODE;
2186 return CMD_SUCCESS;
2187 }
2188
2189 DEFSH(VTYSH_ZEBRA, vtysh_vrf_netns_cmd,
2190 "netns NAME",
2191 "Attach VRF to a Namespace\n"
2192 "The file name in " NS_RUN_DIR ", or a full pathname\n")
2193
2194 DEFSH(VTYSH_ZEBRA, vtysh_no_vrf_netns_cmd,
2195 "no netns [NAME]",
2196 NO_STR
2197 "Detach VRF from a Namespace\n"
2198 "The file name in " NS_RUN_DIR ", or a full pathname\n")
2199
2200 DEFUNSH(VTYSH_VRF, vtysh_exit_vrf, vtysh_exit_vrf_cmd, "exit",
2201 "Exit current mode and down to previous mode\n")
2202 {
2203 return vtysh_exit(vty);
2204 }
2205
2206 DEFUNSH(VTYSH_VRF, vtysh_quit_vrf, vtysh_quit_vrf_cmd, "quit",
2207 "Exit current mode and down to previous mode\n")
2208 {
2209 return vtysh_exit_vrf(self, vty, argc, argv);
2210 }
2211
2212 DEFUNSH(VTYSH_NH_GROUP,
2213 vtysh_exit_nexthop_group, vtysh_exit_nexthop_group_cmd,
2214 "exit", "Exit current mode and down to previous mode\n")
2215 {
2216 return vtysh_exit(vty);
2217 }
2218
2219 DEFUNSH(VTYSH_NH_GROUP,
2220 vtysh_quit_nexthop_group, vtysh_quit_nexthop_group_cmd,
2221 "quit", "Exit current mode and down to previous mode\n")
2222 {
2223 return vtysh_exit_nexthop_group(self, vty, argc, argv);
2224 }
2225
2226 DEFUNSH(VTYSH_INTERFACE, vtysh_exit_interface, vtysh_exit_interface_cmd, "exit",
2227 "Exit current mode and down to previous mode\n")
2228 {
2229 return vtysh_exit(vty);
2230 }
2231
2232 DEFUNSH(VTYSH_INTERFACE, vtysh_quit_interface, vtysh_quit_interface_cmd, "quit",
2233 "Exit current mode and down to previous mode\n")
2234 {
2235 return vtysh_exit_interface(self, vty, argc, argv);
2236 }
2237
2238 DEFUN (vtysh_show_poll,
2239 vtysh_show_poll_cmd,
2240 "show thread poll",
2241 SHOW_STR
2242 "Thread information\n"
2243 "Thread Poll Information\n")
2244 {
2245 unsigned int i;
2246 int ret = CMD_SUCCESS;
2247 char line[100];
2248
2249 snprintf(line, sizeof(line), "do show thread poll\n");
2250 for (i = 0; i < array_size(vtysh_client); i++)
2251 if (vtysh_client[i].fd >= 0) {
2252 vty_out(vty, "Thread statistics for %s:\n",
2253 vtysh_client[i].name);
2254 ret = vtysh_client_execute(&vtysh_client[i], line);
2255 vty_out(vty, "\n");
2256 }
2257 return ret;
2258 }
2259
2260 #ifndef EXCLUDE_CPU_TIME
2261 DEFUN (vtysh_show_thread,
2262 vtysh_show_thread_cmd,
2263 "show thread cpu [FILTER]",
2264 SHOW_STR
2265 "Thread information\n"
2266 "Thread CPU usage\n"
2267 "Display filter (rwtexb)\n")
2268 {
2269 unsigned int i;
2270 int idx = 0;
2271 int ret = CMD_SUCCESS;
2272 char line[100];
2273
2274 const char *filter =
2275 argv_find(argv, argc, "FILTER", &idx) ? argv[idx]->arg : "";
2276
2277 snprintf(line, sizeof(line), "do show thread cpu %s\n", filter);
2278 for (i = 0; i < array_size(vtysh_client); i++)
2279 if (vtysh_client[i].fd >= 0) {
2280 vty_out(vty, "Thread statistics for %s:\n",
2281 vtysh_client[i].name);
2282 ret = vtysh_client_execute(&vtysh_client[i], line);
2283 vty_out(vty, "\n");
2284 }
2285 return ret;
2286 }
2287 #endif
2288
2289 DEFUN (vtysh_show_work_queues,
2290 vtysh_show_work_queues_cmd,
2291 "show work-queues",
2292 SHOW_STR
2293 "Work Queue information\n")
2294 {
2295 unsigned int i;
2296 int ret = CMD_SUCCESS;
2297 char line[] = "do show work-queues\n";
2298
2299 for (i = 0; i < array_size(vtysh_client); i++)
2300 if (vtysh_client[i].fd >= 0) {
2301 vty_out(vty, "Work queue statistics for %s:\n",
2302 vtysh_client[i].name);
2303 ret = vtysh_client_execute(&vtysh_client[i], line);
2304 vty_out(vty, "\n");
2305 }
2306
2307 return ret;
2308 }
2309
2310 DEFUN (vtysh_show_work_queues_daemon,
2311 vtysh_show_work_queues_daemon_cmd,
2312 "show work-queues " DAEMONS_LIST,
2313 SHOW_STR
2314 "Work Queue information\n"
2315 DAEMONS_STR)
2316 {
2317 int idx_protocol = 2;
2318
2319 return vtysh_client_execute_name(argv[idx_protocol]->text,
2320 "show work-queues\n");
2321 }
2322
2323 DEFUNSH(VTYSH_ZEBRA, vtysh_link_params, vtysh_link_params_cmd, "link-params",
2324 LINK_PARAMS_STR)
2325 {
2326 vty->node = LINK_PARAMS_NODE;
2327 return CMD_SUCCESS;
2328 }
2329
2330 DEFUNSH(VTYSH_ZEBRA, exit_link_params, exit_link_params_cmd, "exit-link-params",
2331 "Exit from Link Params configuration node\n")
2332 {
2333 if (vty->node == LINK_PARAMS_NODE)
2334 vty->node = INTERFACE_NODE;
2335 return CMD_SUCCESS;
2336 }
2337
2338 static int show_per_daemon(const char *line, const char *headline)
2339 {
2340 unsigned int i;
2341 int ret = CMD_SUCCESS;
2342
2343 for (i = 0; i < array_size(vtysh_client); i++)
2344 if (vtysh_client[i].fd >= 0) {
2345 vty_out(vty, headline, vtysh_client[i].name);
2346 ret = vtysh_client_execute(&vtysh_client[i], line);
2347 vty_out(vty, "\n");
2348 }
2349
2350 return ret;
2351 }
2352
2353 DEFUNSH_HIDDEN (0x00,
2354 vtysh_debug_all,
2355 vtysh_debug_all_cmd,
2356 "[no] debug all",
2357 NO_STR
2358 DEBUG_STR
2359 "Toggle all debugs on or off\n")
2360 {
2361 return CMD_SUCCESS;
2362 }
2363
2364 DEFUN (vtysh_show_debugging,
2365 vtysh_show_debugging_cmd,
2366 "show debugging",
2367 SHOW_STR
2368 DEBUG_STR)
2369 {
2370 return show_per_daemon("do show debugging\n", "");
2371 }
2372
2373 DEFUN (vtysh_show_debugging_hashtable,
2374 vtysh_show_debugging_hashtable_cmd,
2375 "show debugging hashtable [statistics]",
2376 SHOW_STR
2377 DEBUG_STR
2378 "Statistics about hash tables\n"
2379 "Statistics about hash tables\n")
2380 {
2381 vty_out(vty, "\n");
2382 vty_out(vty,
2383 "Load factor (LF) - average number of elements across all buckets\n");
2384 vty_out(vty,
2385 "Full load factor (FLF) - average number of elements across full buckets\n\n");
2386 vty_out(vty,
2387 "Standard deviation (SD) is calculated for both the LF and FLF\n");
2388 vty_out(vty,
2389 "and indicates the typical deviation of bucket chain length\n");
2390 vty_out(vty, "from the value in the corresponding load factor.\n\n");
2391
2392 return show_per_daemon("do show debugging hashtable\n",
2393 "Hashtable statistics for %s:\n");
2394 }
2395
2396 DEFUN (vtysh_show_error_code,
2397 vtysh_show_error_code_cmd,
2398 "show error <(1-4294967296)|all> [json]",
2399 SHOW_STR
2400 "Information on errors\n"
2401 "Error code to get info about\n"
2402 "Information on all errors\n"
2403 JSON_STR)
2404 {
2405 uint32_t arg = 0;
2406
2407 if (!strmatch(argv[2]->text, "all"))
2408 arg = strtoul(argv[2]->arg, NULL, 10);
2409
2410 /* If it's not a shared code, send it to all the daemons */
2411 if (arg < LIB_FERR_START || arg > LIB_FERR_END) {
2412 char *fcmd = argv_concat(argv, argc, 0);
2413 char cmd[256];
2414
2415 snprintf(cmd, sizeof(cmd), "do %s", fcmd);
2416 show_per_daemon(cmd, "");
2417 XFREE(MTYPE_TMP, fcmd);
2418 /* Otherwise, print it ourselves to avoid duplication */
2419 } else {
2420 bool json = strmatch(argv[argc - 1]->text, "json");
2421
2422 if (!strmatch(argv[2]->text, "all"))
2423 arg = strtoul(argv[2]->arg, NULL, 10);
2424
2425 log_ref_display(vty, arg, json);
2426 }
2427
2428 return CMD_SUCCESS;
2429 }
2430
2431 /* Northbound. */
2432 DEFUN (show_yang_operational_data,
2433 show_yang_operational_data_cmd,
2434 "show yang operational-data XPATH\
2435 [{\
2436 format <json|xml>\
2437 |translate WORD\
2438 }]" DAEMONS_LIST,
2439 SHOW_STR
2440 "YANG information\n"
2441 "Show YANG operational data\n"
2442 "XPath expression specifying the YANG data path\n"
2443 "Set the output format\n"
2444 "JavaScript Object Notation\n"
2445 "Extensible Markup Language\n"
2446 "Translate operational data\n"
2447 "YANG module translator\n"
2448 DAEMONS_STR)
2449 {
2450 int idx_protocol = argc - 1;
2451 char *fcmd = argv_concat(argv, argc - 1, 0);
2452 int ret = vtysh_client_execute_name(argv[idx_protocol]->text, fcmd);
2453 XFREE(MTYPE_TMP, fcmd);
2454 return ret;
2455 }
2456
2457 DEFUNSH(VTYSH_ALL, debug_nb,
2458 debug_nb_cmd,
2459 "[no] debug northbound\
2460 [<\
2461 callbacks [{configuration|state|rpc}]\
2462 |notifications\
2463 |events\
2464 |libyang\
2465 >]",
2466 NO_STR
2467 DEBUG_STR
2468 "Northbound debugging\n"
2469 "Callbacks\n"
2470 "Configuration\n"
2471 "State\n"
2472 "RPC\n"
2473 "Notifications\n"
2474 "Events\n"
2475 "libyang debugging\n")
2476 {
2477 return CMD_SUCCESS;
2478 }
2479
2480 /* Memory */
2481 DEFUN (vtysh_show_memory,
2482 vtysh_show_memory_cmd,
2483 "show memory",
2484 SHOW_STR
2485 "Memory statistics\n")
2486 {
2487 return show_per_daemon("do show memory\n", "Memory statistics for %s:\n");
2488 }
2489
2490 DEFUN (vtysh_show_modules,
2491 vtysh_show_modules_cmd,
2492 "show modules",
2493 SHOW_STR
2494 "Loaded modules\n")
2495 {
2496 return show_per_daemon("do show modules\n",
2497 "Module information for %s:\n");
2498 }
2499
2500 /* Logging commands. */
2501 DEFUN (vtysh_show_logging,
2502 vtysh_show_logging_cmd,
2503 "show logging",
2504 SHOW_STR
2505 "Show current logging configuration\n")
2506 {
2507 return show_per_daemon("do show logging\n",
2508 "Logging configuration for %s:\n");
2509 }
2510
2511 DEFUNSH(VTYSH_ALL, vtysh_log_stdout, vtysh_log_stdout_cmd, "log stdout",
2512 "Logging control\n"
2513 "Set stdout logging level\n")
2514 {
2515 return CMD_SUCCESS;
2516 }
2517
2518 DEFUNSH(VTYSH_ALL, vtysh_log_stdout_level, vtysh_log_stdout_level_cmd,
2519 "log stdout <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>",
2520 "Logging control\n"
2521 "Set stdout logging level\n" LOG_LEVEL_DESC)
2522 {
2523 return CMD_SUCCESS;
2524 }
2525
2526 DEFUNSH(VTYSH_ALL, no_vtysh_log_stdout, no_vtysh_log_stdout_cmd,
2527 "no log stdout [LEVEL]", NO_STR
2528 "Logging control\n"
2529 "Cancel logging to stdout\n"
2530 "Logging level\n")
2531 {
2532 return CMD_SUCCESS;
2533 }
2534
2535 DEFUNSH(VTYSH_ALL, vtysh_log_file, vtysh_log_file_cmd, "log file FILENAME",
2536 "Logging control\n"
2537 "Logging to file\n"
2538 "Logging filename\n")
2539 {
2540 return CMD_SUCCESS;
2541 }
2542
2543 DEFUNSH(VTYSH_ALL, vtysh_log_file_level, vtysh_log_file_level_cmd,
2544 "log file FILENAME <emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>",
2545 "Logging control\n"
2546 "Logging to file\n"
2547 "Logging filename\n" LOG_LEVEL_DESC)
2548 {
2549 return CMD_SUCCESS;
2550 }
2551
2552 DEFUNSH(VTYSH_ALL, no_vtysh_log_file, no_vtysh_log_file_cmd,
2553 "no log file [FILENAME [LEVEL]]", NO_STR
2554 "Logging control\n"
2555 "Cancel logging to file\n"
2556 "Logging file name\n"
2557 "Logging level\n")
2558 {
2559 return CMD_SUCCESS;
2560 }
2561
2562 DEFUNSH(VTYSH_ALL, vtysh_log_monitor, vtysh_log_monitor_cmd,
2563 "log monitor [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2564 "Logging control\n"
2565 "Set terminal line (monitor) logging level\n" LOG_LEVEL_DESC)
2566 {
2567 return CMD_SUCCESS;
2568 }
2569
2570 DEFUNSH(VTYSH_ALL, no_vtysh_log_monitor, no_vtysh_log_monitor_cmd,
2571 "no log monitor [LEVEL]", NO_STR
2572 "Logging control\n"
2573 "Disable terminal line (monitor) logging\n"
2574 "Logging level\n")
2575 {
2576 return CMD_SUCCESS;
2577 }
2578
2579 DEFUNSH(VTYSH_ALL, vtysh_log_syslog, vtysh_log_syslog_cmd,
2580 "log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2581 "Logging control\n"
2582 "Set syslog logging level\n" LOG_LEVEL_DESC)
2583 {
2584 return CMD_SUCCESS;
2585 }
2586
2587 DEFUNSH(VTYSH_ALL, no_vtysh_log_syslog, no_vtysh_log_syslog_cmd,
2588 "no log syslog [<emergencies|alerts|critical|errors|warnings|notifications|informational|debugging>]",
2589 NO_STR
2590 "Logging control\n"
2591 "Cancel logging to syslog\n"
2592 LOG_LEVEL_DESC)
2593 {
2594 return CMD_SUCCESS;
2595 }
2596
2597 DEFUNSH(VTYSH_ALL, vtysh_log_facility, vtysh_log_facility_cmd,
2598 "log facility <kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>",
2599 "Logging control\n"
2600 "Facility parameter for syslog messages\n" LOG_FACILITY_DESC)
2601 {
2602 return CMD_SUCCESS;
2603 }
2604
2605 DEFUNSH(VTYSH_ALL, no_vtysh_log_facility, no_vtysh_log_facility_cmd,
2606 "no log facility [<kern|user|mail|daemon|auth|syslog|lpr|news|uucp|cron|local0|local1|local2|local3|local4|local5|local6|local7>]",
2607 NO_STR
2608 "Logging control\n"
2609 "Reset syslog facility to default (daemon)\n"
2610 LOG_FACILITY_DESC)
2611 {
2612 return CMD_SUCCESS;
2613 }
2614
2615 DEFUNSH(VTYSH_ALL, vtysh_log_record_priority, vtysh_log_record_priority_cmd,
2616 "log record-priority",
2617 "Logging control\n"
2618 "Log the priority of the message within the message\n")
2619 {
2620 return CMD_SUCCESS;
2621 }
2622
2623 DEFUNSH(VTYSH_ALL, no_vtysh_log_record_priority,
2624 no_vtysh_log_record_priority_cmd, "no log record-priority", NO_STR
2625 "Logging control\n"
2626 "Do not log the priority of the message within the message\n")
2627 {
2628 return CMD_SUCCESS;
2629 }
2630
2631 DEFUNSH(VTYSH_ALL, vtysh_log_timestamp_precision,
2632 vtysh_log_timestamp_precision_cmd, "log timestamp precision (0-6)",
2633 "Logging control\n"
2634 "Timestamp configuration\n"
2635 "Set the timestamp precision\n"
2636 "Number of subsecond digits\n")
2637 {
2638 return CMD_SUCCESS;
2639 }
2640
2641 DEFUNSH(VTYSH_ALL, vtysh_debug_memstats,
2642 vtysh_debug_memstats_cmd, "[no] debug memstats-at-exit",
2643 NO_STR
2644 "Debug\n"
2645 "Print memory statistics at exit\n")
2646 {
2647 return CMD_SUCCESS;
2648 }
2649
2650 DEFUNSH(VTYSH_ALL, no_vtysh_log_timestamp_precision,
2651 no_vtysh_log_timestamp_precision_cmd, "no log timestamp precision",
2652 NO_STR
2653 "Logging control\n"
2654 "Timestamp configuration\n"
2655 "Reset the timestamp precision to the default value of 0\n")
2656 {
2657 return CMD_SUCCESS;
2658 }
2659
2660 DEFUNSH(VTYSH_ALL, vtysh_service_password_encrypt,
2661 vtysh_service_password_encrypt_cmd, "service password-encryption",
2662 "Set up miscellaneous service\n"
2663 "Enable encrypted passwords\n")
2664 {
2665 return CMD_SUCCESS;
2666 }
2667
2668 DEFUNSH(VTYSH_ALL, no_vtysh_service_password_encrypt,
2669 no_vtysh_service_password_encrypt_cmd, "no service password-encryption",
2670 NO_STR
2671 "Set up miscellaneous service\n"
2672 "Enable encrypted passwords\n")
2673 {
2674 return CMD_SUCCESS;
2675 }
2676
2677 DEFUNSH(VTYSH_ALL, vtysh_config_password, vtysh_password_cmd,
2678 "password [(8-8)] LINE",
2679 "Modify the terminal connection password\n"
2680 "Specifies a HIDDEN password will follow\n"
2681 "The password string\n")
2682 {
2683 return CMD_SUCCESS;
2684 }
2685
2686 DEFUNSH(VTYSH_ALL, no_vtysh_config_password, no_vtysh_password_cmd,
2687 "no password", NO_STR
2688 "Modify the terminal connection password\n")
2689 {
2690 vty_out(vty, NO_PASSWD_CMD_WARNING);
2691
2692 return CMD_SUCCESS;
2693 }
2694
2695 DEFUNSH(VTYSH_ALL, vtysh_config_enable_password, vtysh_enable_password_cmd,
2696 "enable password [(8-8)] LINE",
2697 "Modify enable password parameters\n"
2698 "Assign the privileged level password\n"
2699 "Specifies a HIDDEN password will follow\n"
2700 "The 'enable' password string\n")
2701 {
2702 return CMD_SUCCESS;
2703 }
2704
2705 DEFUNSH(VTYSH_ALL, no_vtysh_config_enable_password,
2706 no_vtysh_enable_password_cmd, "no enable password", NO_STR
2707 "Modify enable password parameters\n"
2708 "Assign the privileged level password\n")
2709 {
2710 vty_out(vty, NO_PASSWD_CMD_WARNING);
2711
2712 return CMD_SUCCESS;
2713 }
2714
2715 /* Log filter */
2716 DEFUN (vtysh_log_filter,
2717 vtysh_log_filter_cmd,
2718 "[no] log-filter WORD ["DAEMONS_LIST"]",
2719 NO_STR
2720 FILTER_LOG_STR
2721 "String to filter by\n"
2722 DAEMONS_STR)
2723 {
2724 char *filter = NULL;
2725 char *daemon = NULL;
2726 int found = 0;
2727 int idx = 0;
2728 int daemon_idx = 2;
2729 int total_len = 0;
2730 int len = 0;
2731
2732 char line[ZLOG_FILTER_LENGTH_MAX + 20];
2733
2734 found = argv_find(argv, argc, "no", &idx);
2735 if (found == 1) {
2736 len = snprintf(line, sizeof(line), "no log-filter");
2737 daemon_idx += 1;
2738 } else
2739 len = snprintf(line, sizeof(line), "log-filter");
2740
2741 total_len += len;
2742
2743 idx = 1;
2744 found = argv_find(argv, argc, "WORD", &idx);
2745 if (found != 1) {
2746 vty_out(vty, "%% No filter string given\n");
2747 return CMD_WARNING;
2748 }
2749 filter = argv[idx]->arg;
2750
2751 if (strnlen(filter, ZLOG_FILTER_LENGTH_MAX + 1)
2752 > ZLOG_FILTER_LENGTH_MAX) {
2753 vty_out(vty, "%% Filter is too long\n");
2754 return CMD_WARNING;
2755 }
2756
2757 len = snprintf(line + total_len, sizeof(line) - total_len, " %s\n",
2758 filter);
2759
2760 if ((len < 0) || (size_t)(total_len + len) > sizeof(line)) {
2761 vty_out(vty, "%% Error buffering filter to daemons\n");
2762 return CMD_ERR_INCOMPLETE;
2763 }
2764
2765 if (argc >= (daemon_idx + 1))
2766 daemon = argv[daemon_idx]->text;
2767
2768 if (daemon != NULL) {
2769 vty_out(vty, "Applying log filter change to %s:\n", daemon);
2770 return vtysh_client_execute_name(daemon, line);
2771 } else
2772 return show_per_daemon(line,
2773 "Applying log filter change to %s:\n");
2774 }
2775
2776 /* Clear log filters */
2777 DEFUN (vtysh_log_filter_clear,
2778 vtysh_log_filter_clear_cmd,
2779 "log-filter clear ["DAEMONS_LIST"]",
2780 FILTER_LOG_STR
2781 CLEAR_STR
2782 DAEMONS_STR)
2783 {
2784 char *daemon = NULL;
2785 int daemon_idx = 2;
2786
2787 char line[] = "clear log-filter\n";
2788
2789 if (argc >= (daemon_idx + 1))
2790 daemon = argv[daemon_idx]->text;
2791
2792 if (daemon != NULL) {
2793 vty_out(vty, "Clearing all filters applied to %s:\n", daemon);
2794 return vtysh_client_execute_name(daemon, line);
2795 } else
2796 return show_per_daemon(line,
2797 "Clearing all filters applied to %s:\n");
2798 }
2799
2800 /* Show log filter */
2801 DEFUN (vtysh_show_log_filter,
2802 vtysh_show_log_filter_cmd,
2803 "show log-filter",
2804 SHOW_STR
2805 FILTER_LOG_STR)
2806 {
2807 char line[] = "do show log-filter\n";
2808
2809 return show_per_daemon(line, "Log filters applied to %s:\n");
2810 }
2811
2812 DEFUN (vtysh_write_terminal,
2813 vtysh_write_terminal_cmd,
2814 "write terminal ["DAEMONS_LIST"]",
2815 "Write running configuration to memory, network, or terminal\n"
2816 "Write to terminal\n"
2817 DAEMONS_STR)
2818 {
2819 unsigned int i;
2820 char line[] = "do write terminal\n";
2821
2822 vty_out(vty, "Building configuration...\n");
2823 vty_out(vty, "\nCurrent configuration:\n");
2824 vty_out(vty, "!\n");
2825
2826 for (i = 0; i < array_size(vtysh_client); i++)
2827 if ((argc < 3)
2828 || (strmatch(vtysh_client[i].name, argv[2]->text)))
2829 vtysh_client_config(&vtysh_client[i], line);
2830
2831 /* Integrate vtysh specific configuration. */
2832 vty_open_pager(vty);
2833 vtysh_config_write();
2834 vtysh_config_dump();
2835 vty_close_pager(vty);
2836 vty_out(vty, "end\n");
2837
2838 return CMD_SUCCESS;
2839 }
2840
2841 DEFUN (vtysh_show_running_config,
2842 vtysh_show_running_config_cmd,
2843 "show running-config ["DAEMONS_LIST"]",
2844 SHOW_STR
2845 "Current operating configuration\n"
2846 DAEMONS_STR)
2847 {
2848 return vtysh_write_terminal(self, vty, argc, argv);
2849 }
2850
2851 DEFUN (vtysh_integrated_config,
2852 vtysh_integrated_config_cmd,
2853 "service integrated-vtysh-config",
2854 "Set up miscellaneous service\n"
2855 "Write configuration into integrated file\n")
2856 {
2857 vtysh_write_integrated = WRITE_INTEGRATED_YES;
2858 return CMD_SUCCESS;
2859 }
2860
2861 DEFUN (no_vtysh_integrated_config,
2862 no_vtysh_integrated_config_cmd,
2863 "no service integrated-vtysh-config",
2864 NO_STR
2865 "Set up miscellaneous service\n"
2866 "Write configuration into integrated file\n")
2867 {
2868 vtysh_write_integrated = WRITE_INTEGRATED_NO;
2869 return CMD_SUCCESS;
2870 }
2871
2872 static void backup_config_file(const char *fbackup)
2873 {
2874 char *integrate_sav = NULL;
2875
2876 size_t integrate_sav_sz = strlen(fbackup) + strlen(CONF_BACKUP_EXT) + 1;
2877 integrate_sav = malloc(integrate_sav_sz);
2878 strlcpy(integrate_sav, fbackup, integrate_sav_sz);
2879 strlcat(integrate_sav, CONF_BACKUP_EXT, integrate_sav_sz);
2880
2881 /* Move current configuration file to backup config file. */
2882 if (unlink(integrate_sav) != 0) {
2883 vty_out(vty, "Warning: %s unlink failed\n", integrate_sav);
2884 }
2885 if (rename(fbackup, integrate_sav) != 0) {
2886 vty_out(vty, "Error renaming %s to %s\n", fbackup,
2887 integrate_sav);
2888 }
2889 free(integrate_sav);
2890 }
2891
2892 int vtysh_write_config_integrated(void)
2893 {
2894 unsigned int i;
2895 char line[] = "do write terminal\n";
2896 FILE *fp;
2897 int fd;
2898 #ifdef FRR_USER
2899 struct passwd *pwentry;
2900 #endif
2901 #ifdef FRR_GROUP
2902 struct group *grentry;
2903 #endif
2904 uid_t uid = -1;
2905 gid_t gid = -1;
2906 struct stat st;
2907 int err = 0;
2908
2909 vty_out(vty, "Building Configuration...\n");
2910
2911 backup_config_file(frr_config);
2912 fp = fopen(frr_config, "w");
2913 if (fp == NULL) {
2914 vty_out(vty,
2915 "%% Error: failed to open configuration file %s: %s\n",
2916 frr_config, safe_strerror(errno));
2917 return CMD_WARNING_CONFIG_FAILED;
2918 }
2919 fd = fileno(fp);
2920
2921 for (i = 0; i < array_size(vtysh_client); i++)
2922 vtysh_client_config(&vtysh_client[i], line);
2923
2924 vtysh_config_write();
2925 vty->of_saved = vty->of;
2926 vty->of = fp;
2927 vtysh_config_dump();
2928 vty->of = vty->of_saved;
2929
2930 if (fchmod(fd, CONFIGFILE_MASK) != 0) {
2931 printf("%% Warning: can't chmod configuration file %s: %s\n",
2932 frr_config, safe_strerror(errno));
2933 err++;
2934 }
2935
2936 #ifdef FRR_USER
2937 pwentry = getpwnam(FRR_USER);
2938 if (pwentry)
2939 uid = pwentry->pw_uid;
2940 else {
2941 printf("%% Warning: could not look up user \"%s\"\n", FRR_USER);
2942 err++;
2943 }
2944 #endif
2945 #ifdef FRR_GROUP
2946 grentry = getgrnam(FRR_GROUP);
2947 if (grentry)
2948 gid = grentry->gr_gid;
2949 else {
2950 printf("%% Warning: could not look up group \"%s\"\n",
2951 FRR_GROUP);
2952 err++;
2953 }
2954 #endif
2955
2956 if (!fstat(fd, &st)) {
2957 if (st.st_uid == uid)
2958 uid = -1;
2959 if (st.st_gid == gid)
2960 gid = -1;
2961 if ((uid != (uid_t)-1 || gid != (gid_t)-1)
2962 && fchown(fd, uid, gid)) {
2963 printf("%% Warning: can't chown configuration file %s: %s\n",
2964 frr_config, safe_strerror(errno));
2965 err++;
2966 }
2967 } else {
2968 printf("%% Warning: stat() failed on %s: %s\n", frr_config,
2969 safe_strerror(errno));
2970 err++;
2971 }
2972
2973 fclose(fp);
2974
2975 printf("Integrated configuration saved to %s\n", frr_config);
2976 if (err)
2977 return CMD_WARNING;
2978
2979 printf("[OK]\n");
2980 return CMD_SUCCESS;
2981 }
2982
2983 static bool want_config_integrated(void)
2984 {
2985 struct stat s;
2986
2987 switch (vtysh_write_integrated) {
2988 case WRITE_INTEGRATED_UNSPECIFIED:
2989 if (stat(frr_config, &s) && errno == ENOENT)
2990 return false;
2991 return true;
2992 case WRITE_INTEGRATED_NO:
2993 return false;
2994 case WRITE_INTEGRATED_YES:
2995 return true;
2996 }
2997 return true;
2998 }
2999
3000 DEFUN (vtysh_write_memory,
3001 vtysh_write_memory_cmd,
3002 "write [<memory|file>]",
3003 "Write running configuration to memory, network, or terminal\n"
3004 "Write configuration to the file (same as write file)\n"
3005 "Write configuration to the file (same as write memory)\n")
3006 {
3007 int ret = CMD_SUCCESS;
3008 char line[] = "do write memory\n";
3009 unsigned int i;
3010
3011 vty_out(vty, "Note: this version of vtysh never writes vtysh.conf\n");
3012
3013 /* If integrated frr.conf explicitely set. */
3014 if (want_config_integrated()) {
3015 ret = CMD_WARNING_CONFIG_FAILED;
3016
3017 /* first attempt to use watchfrr if it's available */
3018 bool used_watchfrr = false;
3019
3020 for (i = 0; i < array_size(vtysh_client); i++)
3021 if (vtysh_client[i].flag == VTYSH_WATCHFRR)
3022 break;
3023 if (i < array_size(vtysh_client) && vtysh_client[i].fd != -1) {
3024 used_watchfrr = true;
3025 ret = vtysh_client_execute(&vtysh_client[i],
3026 "do write integrated");
3027 }
3028
3029 /*
3030 * If we didn't use watchfrr, fallback to writing the config
3031 * ourselves
3032 */
3033 if (!used_watchfrr) {
3034 printf("\nWarning: attempting direct configuration write without "
3035 "watchfrr.\nFile permissions and ownership may be "
3036 "incorrect, or write may fail.\n\n");
3037 ret = vtysh_write_config_integrated();
3038 }
3039 return ret;
3040 }
3041
3042 vty_out(vty, "Building Configuration...\n");
3043
3044 for (i = 0; i < array_size(vtysh_client); i++)
3045 ret = vtysh_client_execute(&vtysh_client[i], line);
3046
3047 return ret;
3048 }
3049
3050 DEFUN (vtysh_copy_running_config,
3051 vtysh_copy_running_config_cmd,
3052 "copy running-config startup-config",
3053 "Copy from one file to another\n"
3054 "Copy from current system configuration\n"
3055 "Copy to startup configuration\n")
3056 {
3057 return vtysh_write_memory(self, vty, argc, argv);
3058 }
3059
3060 DEFUN (vtysh_copy_to_running,
3061 vtysh_copy_to_running_cmd,
3062 "copy FILENAME running-config",
3063 "Apply a configuration file\n"
3064 "Configuration file to read\n"
3065 "Apply to current configuration\n")
3066 {
3067 int ret;
3068 const char *fname = argv[1]->arg;
3069
3070 ret = vtysh_read_config(fname);
3071
3072 /* Return to enable mode - the 'read_config' api leaves us up a level */
3073 vtysh_execute_no_pager("enable");
3074
3075 return ret;
3076 }
3077
3078 DEFUN (vtysh_terminal_paginate,
3079 vtysh_terminal_paginate_cmd,
3080 "[no] terminal paginate",
3081 NO_STR
3082 "Set terminal line parameters\n"
3083 "Use pager for output scrolling\n")
3084 {
3085 free(vtysh_pager_name);
3086 vtysh_pager_name = NULL;
3087
3088 if (strcmp(argv[0]->text, "no"))
3089 vtysh_pager_envdef(true);
3090 return CMD_SUCCESS;
3091 }
3092
3093 DEFUN (vtysh_terminal_length,
3094 vtysh_terminal_length_cmd,
3095 "[no] terminal length (0-4294967295)",
3096 NO_STR
3097 "Set terminal line parameters\n"
3098 "Set number of lines on a screen\n"
3099 "Number of lines on screen (0 for no pausing, nonzero to use pager)\n")
3100 {
3101 int idx_number = 2;
3102 unsigned long lines;
3103
3104 free(vtysh_pager_name);
3105 vtysh_pager_name = NULL;
3106
3107 if (!strcmp(argv[0]->text, "no") || !strcmp(argv[1]->text, "no")) {
3108 /* "terminal no length" = use VTYSH_PAGER */
3109 vtysh_pager_envdef(true);
3110 return CMD_SUCCESS;
3111 }
3112
3113 lines = strtoul(argv[idx_number]->arg, NULL, 10);
3114 if (lines != 0) {
3115 vty_out(vty,
3116 "%% The \"terminal length\" command is deprecated and its value is ignored.\n"
3117 "%% Please use \"terminal paginate\" instead with OS TTY length handling.\n");
3118 vtysh_pager_envdef(true);
3119 }
3120
3121 return CMD_SUCCESS;
3122 }
3123
3124 ALIAS_DEPRECATED(vtysh_terminal_length,
3125 vtysh_terminal_no_length_cmd,
3126 "terminal no length",
3127 "Set terminal line parameters\n"
3128 NO_STR
3129 "Set number of lines on a screen\n")
3130
3131 DEFUN (vtysh_show_daemons,
3132 vtysh_show_daemons_cmd,
3133 "show daemons",
3134 SHOW_STR
3135 "Show list of running daemons\n")
3136 {
3137 unsigned int i;
3138
3139 for (i = 0; i < array_size(vtysh_client); i++)
3140 if (vtysh_client[i].fd >= 0)
3141 vty_out(vty, " %s", vtysh_client[i].name);
3142 vty_out(vty, "\n");
3143
3144 return CMD_SUCCESS;
3145 }
3146
3147 /* Execute command in child process. */
3148 static void execute_command(const char *command, int argc, const char *arg1,
3149 const char *arg2)
3150 {
3151 pid_t pid;
3152 int status;
3153
3154 /* Call fork(). */
3155 pid = fork();
3156
3157 if (pid < 0) {
3158 /* Failure of fork(). */
3159 fprintf(stderr, "Can't fork: %s\n", safe_strerror(errno));
3160 exit(1);
3161 } else if (pid == 0) {
3162 /* This is child process. */
3163 switch (argc) {
3164 case 0:
3165 execlp(command, command, (const char *)NULL);
3166 break;
3167 case 1:
3168 execlp(command, command, arg1, (const char *)NULL);
3169 break;
3170 case 2:
3171 execlp(command, command, arg1, arg2,
3172 (const char *)NULL);
3173 break;
3174 }
3175
3176 /* When execlp suceed, this part is not executed. */
3177 fprintf(stderr, "Can't execute %s: %s\n", command,
3178 safe_strerror(errno));
3179 exit(1);
3180 } else {
3181 /* This is parent. */
3182 execute_flag = 1;
3183 wait4(pid, &status, 0, NULL);
3184 execute_flag = 0;
3185 }
3186 }
3187
3188 DEFUN (vtysh_ping,
3189 vtysh_ping_cmd,
3190 "ping WORD",
3191 "Send echo messages\n"
3192 "Ping destination address or hostname\n")
3193 {
3194 int idx = 1;
3195
3196 argv_find(argv, argc, "WORD", &idx);
3197 execute_command("ping", 1, argv[idx]->arg, NULL);
3198 return CMD_SUCCESS;
3199 }
3200
3201 ALIAS(vtysh_ping, vtysh_ping_ip_cmd, "ping ip WORD",
3202 "Send echo messages\n"
3203 "IP echo\n"
3204 "Ping destination address or hostname\n")
3205
3206 DEFUN (vtysh_traceroute,
3207 vtysh_traceroute_cmd,
3208 "traceroute WORD",
3209 "Trace route to destination\n"
3210 "Trace route to destination address or hostname\n")
3211 {
3212 int idx = 1;
3213
3214 argv_find(argv, argc, "WORD", &idx);
3215 execute_command("traceroute", 1, argv[idx]->arg, NULL);
3216 return CMD_SUCCESS;
3217 }
3218
3219 ALIAS(vtysh_traceroute, vtysh_traceroute_ip_cmd, "traceroute ip WORD",
3220 "Trace route to destination\n"
3221 "IP trace\n"
3222 "Trace route to destination address or hostname\n")
3223
3224 DEFUN (vtysh_mtrace,
3225 vtysh_mtrace_cmd,
3226 "mtrace WORD [WORD]",
3227 "Multicast trace route to multicast source\n"
3228 "Multicast trace route to multicast source address\n"
3229 "Multicast trace route for multicast group address\n")
3230 {
3231 if (argc == 2)
3232 execute_command("mtracebis", 1, argv[1]->arg, NULL);
3233 else
3234 execute_command("mtracebis", 2, argv[1]->arg, argv[2]->arg);
3235 return CMD_SUCCESS;
3236 }
3237
3238 DEFUN (vtysh_ping6,
3239 vtysh_ping6_cmd,
3240 "ping ipv6 WORD",
3241 "Send echo messages\n"
3242 "IPv6 echo\n"
3243 "Ping destination address or hostname\n")
3244 {
3245 execute_command("ping6", 1, argv[2]->arg, NULL);
3246 return CMD_SUCCESS;
3247 }
3248
3249 DEFUN (vtysh_traceroute6,
3250 vtysh_traceroute6_cmd,
3251 "traceroute ipv6 WORD",
3252 "Trace route to destination\n"
3253 "IPv6 trace\n"
3254 "Trace route to destination address or hostname\n")
3255 {
3256 execute_command("traceroute6", 1, argv[2]->arg, NULL);
3257 return CMD_SUCCESS;
3258 }
3259
3260 #if defined(HAVE_SHELL_ACCESS)
3261 DEFUN (vtysh_telnet,
3262 vtysh_telnet_cmd,
3263 "telnet WORD",
3264 "Open a telnet connection\n"
3265 "IP address or hostname of a remote system\n")
3266 {
3267 execute_command("telnet", 1, argv[1]->arg, NULL);
3268 return CMD_SUCCESS;
3269 }
3270
3271 DEFUN (vtysh_telnet_port,
3272 vtysh_telnet_port_cmd,
3273 "telnet WORD PORT",
3274 "Open a telnet connection\n"
3275 "IP address or hostname of a remote system\n"
3276 "TCP Port number\n")
3277 {
3278 execute_command("telnet", 2, argv[1]->arg, argv[2]->arg);
3279 return CMD_SUCCESS;
3280 }
3281
3282 DEFUN (vtysh_ssh,
3283 vtysh_ssh_cmd,
3284 "ssh WORD",
3285 "Open an ssh connection\n"
3286 "[user@]host\n")
3287 {
3288 execute_command("ssh", 1, argv[1]->arg, NULL);
3289 return CMD_SUCCESS;
3290 }
3291
3292 DEFUN (vtysh_start_shell,
3293 vtysh_start_shell_cmd,
3294 "start-shell",
3295 "Start UNIX shell\n")
3296 {
3297 execute_command("sh", 0, NULL, NULL);
3298 return CMD_SUCCESS;
3299 }
3300
3301 DEFUN (vtysh_start_bash,
3302 vtysh_start_bash_cmd,
3303 "start-shell bash",
3304 "Start UNIX shell\n"
3305 "Start bash\n")
3306 {
3307 execute_command("bash", 0, NULL, NULL);
3308 return CMD_SUCCESS;
3309 }
3310
3311 DEFUN (vtysh_start_zsh,
3312 vtysh_start_zsh_cmd,
3313 "start-shell zsh",
3314 "Start UNIX shell\n"
3315 "Start Z shell\n")
3316 {
3317 execute_command("zsh", 0, NULL, NULL);
3318 return CMD_SUCCESS;
3319 }
3320 #endif
3321
3322 DEFUN (config_list,
3323 config_list_cmd,
3324 "list [permutations]",
3325 "Print command list\n"
3326 "Print all possible command permutations\n")
3327 {
3328 return cmd_list_cmds(vty, argc == 2);
3329 }
3330
3331 DEFUN (vtysh_output_file,
3332 vtysh_output_file_cmd,
3333 "output file FILE",
3334 "Direct vtysh output to file\n"
3335 "Direct vtysh output to file\n"
3336 "Path to dump output to\n")
3337 {
3338 const char *path = argv[argc - 1]->arg;
3339 vty->of = fopen(path, "a");
3340 if (!vty->of) {
3341 vty_out(vty, "Failed to open file '%s': %s\n", path,
3342 safe_strerror(errno));
3343 vty->of = stdout;
3344 }
3345 return CMD_SUCCESS;
3346 }
3347
3348 DEFUN (no_vtysh_output_file,
3349 no_vtysh_output_file_cmd,
3350 "no output file [FILE]",
3351 NO_STR
3352 "Direct vtysh output to file\n"
3353 "Direct vtysh output to file\n"
3354 "Path to dump output to\n")
3355 {
3356 if (vty->of != stdout) {
3357 fclose(vty->of);
3358 vty->of = stdout;
3359 }
3360 return CMD_SUCCESS;
3361 }
3362
3363 DEFUN(find,
3364 find_cmd,
3365 "find REGEX",
3366 "Find CLI command matching a regular expression\n"
3367 "Search pattern (POSIX regex)\n")
3368 {
3369 char *pattern = argv[1]->arg;
3370 const struct cmd_node *node;
3371 const struct cmd_element *cli;
3372 vector clis;
3373
3374 regex_t exp = {};
3375
3376 int cr = regcomp(&exp, pattern, REG_NOSUB | REG_EXTENDED);
3377
3378 if (cr != 0) {
3379 switch (cr) {
3380 case REG_BADBR:
3381 vty_out(vty, "%% Invalid \\{...\\} expression\n");
3382 break;
3383 case REG_BADRPT:
3384 vty_out(vty, "%% Bad repetition operator\n");
3385 break;
3386 case REG_BADPAT:
3387 vty_out(vty, "%% Regex syntax error\n");
3388 break;
3389 case REG_ECOLLATE:
3390 vty_out(vty, "%% Invalid collating element\n");
3391 break;
3392 case REG_ECTYPE:
3393 vty_out(vty, "%% Invalid character class name\n");
3394 break;
3395 case REG_EESCAPE:
3396 vty_out(vty,
3397 "%% Regex ended with escape character (\\)\n");
3398 break;
3399 case REG_ESUBREG:
3400 vty_out(vty,
3401 "%% Invalid number in \\digit construction\n");
3402 break;
3403 case REG_EBRACK:
3404 vty_out(vty, "%% Unbalanced square brackets\n");
3405 break;
3406 case REG_EPAREN:
3407 vty_out(vty, "%% Unbalanced parentheses\n");
3408 break;
3409 case REG_EBRACE:
3410 vty_out(vty, "%% Unbalanced braces\n");
3411 break;
3412 case REG_ERANGE:
3413 vty_out(vty,
3414 "%% Invalid endpoint in range expression\n");
3415 break;
3416 case REG_ESPACE:
3417 vty_out(vty, "%% Failed to compile (out of memory)\n");
3418 break;
3419 }
3420
3421 goto done;
3422 }
3423
3424
3425 for (unsigned int i = 0; i < vector_active(cmdvec); i++) {
3426 node = vector_slot(cmdvec, i);
3427 if (!node)
3428 continue;
3429 clis = node->cmd_vector;
3430 for (unsigned int j = 0; j < vector_active(clis); j++) {
3431 cli = vector_slot(clis, j);
3432
3433 if (regexec(&exp, cli->string, 0, NULL, 0) == 0)
3434 vty_out(vty, " (%s) %s\n",
3435 node_names[node->node], cli->string);
3436 }
3437 }
3438
3439 done:
3440 regfree(&exp);
3441 return CMD_SUCCESS;
3442 }
3443
3444 DEFUN_HIDDEN(show_cli_graph_vtysh,
3445 show_cli_graph_vtysh_cmd,
3446 "show cli graph",
3447 SHOW_STR
3448 "CLI reflection\n"
3449 "Dump current command space as DOT graph\n")
3450 {
3451 struct cmd_node *cn = vector_slot(cmdvec, vty->node);
3452 char *dot = cmd_graph_dump_dot(cn->cmdgraph);
3453
3454 vty_out(vty, "%s\n", dot);
3455 XFREE(MTYPE_TMP, dot);
3456 return CMD_SUCCESS;
3457 }
3458
3459 static void vtysh_install_default(enum node_type node)
3460 {
3461 install_element(node, &config_list_cmd);
3462 install_element(node, &find_cmd);
3463 install_element(node, &show_cli_graph_vtysh_cmd);
3464 install_element(node, &vtysh_output_file_cmd);
3465 install_element(node, &no_vtysh_output_file_cmd);
3466 }
3467
3468 /* Making connection to protocol daemon. */
3469 static int vtysh_connect(struct vtysh_client *vclient)
3470 {
3471 int ret;
3472 int sock, len;
3473 struct sockaddr_un addr;
3474 struct stat s_stat;
3475 const char *path;
3476
3477 if (!vclient->path[0])
3478 snprintf(vclient->path, sizeof(vclient->path), "%s/%s.vty",
3479 vtydir, vclient->name);
3480 path = vclient->path;
3481
3482 /* Stat socket to see if we have permission to access it. */
3483 ret = stat(path, &s_stat);
3484 if (ret < 0 && errno != ENOENT) {
3485 fprintf(stderr, "vtysh_connect(%s): stat = %s\n", path,
3486 safe_strerror(errno));
3487 exit(1);
3488 }
3489
3490 if (ret >= 0) {
3491 if (!S_ISSOCK(s_stat.st_mode)) {
3492 fprintf(stderr, "vtysh_connect(%s): Not a socket\n",
3493 path);
3494 exit(1);
3495 }
3496 }
3497
3498 sock = socket(AF_UNIX, SOCK_STREAM, 0);
3499 if (sock < 0) {
3500 #ifdef DEBUG
3501 fprintf(stderr, "vtysh_connect(%s): socket = %s\n", path,
3502 safe_strerror(errno));
3503 #endif /* DEBUG */
3504 return -1;
3505 }
3506
3507 memset(&addr, 0, sizeof(struct sockaddr_un));
3508 addr.sun_family = AF_UNIX;
3509 strlcpy(addr.sun_path, path, sizeof(addr.sun_path));
3510 #ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
3511 len = addr.sun_len = SUN_LEN(&addr);
3512 #else
3513 len = sizeof(addr.sun_family) + strlen(addr.sun_path);
3514 #endif /* HAVE_STRUCT_SOCKADDR_UN_SUN_LEN */
3515
3516 ret = connect(sock, (struct sockaddr *)&addr, len);
3517 if (ret < 0) {
3518 #ifdef DEBUG
3519 fprintf(stderr, "vtysh_connect(%s): connect = %s\n", path,
3520 safe_strerror(errno));
3521 #endif /* DEBUG */
3522 close(sock);
3523 return -1;
3524 }
3525 vclient->fd = sock;
3526
3527 return 0;
3528 }
3529
3530 static int vtysh_reconnect(struct vtysh_client *vclient)
3531 {
3532 int ret;
3533
3534 fprintf(stderr, "Warning: connecting to %s...", vclient->name);
3535 ret = vtysh_connect(vclient);
3536 if (ret < 0) {
3537 fprintf(stderr, "failed!\n");
3538 return ret;
3539 }
3540 fprintf(stderr, "success!\n");
3541 if (vtysh_client_execute(vclient, "enable") < 0)
3542 return -1;
3543 return vtysh_execute_no_pager("end");
3544 }
3545
3546 /* Return true if str ends with suffix, else return false */
3547 static int ends_with(const char *str, const char *suffix)
3548 {
3549 if (!str || !suffix)
3550 return 0;
3551 size_t lenstr = strlen(str);
3552 size_t lensuffix = strlen(suffix);
3553 if (lensuffix > lenstr)
3554 return 0;
3555 return strncmp(str + lenstr - lensuffix, suffix, lensuffix) == 0;
3556 }
3557
3558 static void vtysh_client_sorted_insert(struct vtysh_client *head_client,
3559 struct vtysh_client *client)
3560 {
3561 struct vtysh_client *prev_node, *current_node;
3562
3563 prev_node = head_client;
3564 current_node = head_client->next;
3565 while (current_node) {
3566 if (strcmp(current_node->path, client->path) > 0)
3567 break;
3568
3569 prev_node = current_node;
3570 current_node = current_node->next;
3571 }
3572 client->next = current_node;
3573 prev_node->next = client;
3574 }
3575
3576 #define MAXIMUM_INSTANCES 10
3577
3578 static void vtysh_update_all_instances(struct vtysh_client *head_client)
3579 {
3580 struct vtysh_client *client;
3581 DIR *dir;
3582 struct dirent *file;
3583 int n = 0;
3584
3585 if (head_client->flag != VTYSH_OSPFD)
3586 return;
3587
3588 /* ls vty_sock_dir and look for all files ending in .vty */
3589 dir = opendir(vtydir);
3590 if (dir) {
3591 while ((file = readdir(dir)) != NULL) {
3592 if (frrstr_startswith(file->d_name, "ospfd-")
3593 && ends_with(file->d_name, ".vty")) {
3594 if (n == MAXIMUM_INSTANCES) {
3595 fprintf(stderr,
3596 "Parsing %s, client limit(%d) reached!\n",
3597 vtydir, n);
3598 break;
3599 }
3600 client = (struct vtysh_client *)malloc(
3601 sizeof(struct vtysh_client));
3602 client->fd = -1;
3603 client->name = "ospfd";
3604 client->flag = VTYSH_OSPFD;
3605 snprintf(client->path, sizeof(client->path),
3606 "%s/%s", vtydir, file->d_name);
3607 client->next = NULL;
3608 vtysh_client_sorted_insert(head_client, client);
3609 n++;
3610 }
3611 }
3612 closedir(dir);
3613 }
3614 }
3615
3616 static int vtysh_connect_all_instances(struct vtysh_client *head_client)
3617 {
3618 struct vtysh_client *client;
3619 int rc = 0;
3620
3621 vtysh_update_all_instances(head_client);
3622
3623 client = head_client->next;
3624 while (client) {
3625 if (vtysh_connect(client) == 0)
3626 rc++;
3627 client = client->next;
3628 }
3629
3630 return rc;
3631 }
3632
3633 int vtysh_connect_all(const char *daemon_name)
3634 {
3635 unsigned int i;
3636 int rc = 0;
3637 int matches = 0;
3638
3639 for (i = 0; i < array_size(vtysh_client); i++) {
3640 if (!daemon_name
3641 || !strcmp(daemon_name, vtysh_client[i].name)) {
3642 matches++;
3643 if (vtysh_connect(&vtysh_client[i]) == 0)
3644 rc++;
3645
3646 rc += vtysh_connect_all_instances(&vtysh_client[i]);
3647 }
3648 }
3649 if (!matches)
3650 fprintf(stderr, "Error: no daemons match name %s!\n",
3651 daemon_name);
3652 return rc;
3653 }
3654
3655 /* To disable readline's filename completion. */
3656 static char *vtysh_completion_entry_function(const char *ignore,
3657 int invoking_key)
3658 {
3659 return NULL;
3660 }
3661
3662 void vtysh_readline_init(void)
3663 {
3664 /* readline related settings. */
3665 rl_initialize();
3666 rl_bind_key('?', (rl_command_func_t *)vtysh_rl_describe);
3667 rl_completion_entry_function = vtysh_completion_entry_function;
3668 rl_attempted_completion_function = new_completion;
3669 }
3670
3671 char *vtysh_prompt(void)
3672 {
3673 static char buf[512];
3674
3675 snprintf(buf, sizeof buf, cmd_prompt(vty->node), cmd_hostname_get());
3676 return buf;
3677 }
3678
3679 static void vtysh_ac_line(void *arg, const char *line)
3680 {
3681 vector comps = arg;
3682 size_t i;
3683 for (i = 0; i < vector_active(comps); i++)
3684 if (!strcmp(line, (char *)vector_slot(comps, i)))
3685 return;
3686 vector_set(comps, XSTRDUP(MTYPE_COMPLETION, line));
3687 }
3688
3689 static void vtysh_autocomplete(vector comps, struct cmd_token *token)
3690 {
3691 char accmd[256];
3692 size_t i;
3693
3694 snprintf(accmd, sizeof(accmd), "autocomplete %d %s %s", token->type,
3695 token->text, token->varname ? token->varname : "-");
3696
3697 vty->of_saved = vty->of;
3698 vty->of = NULL;
3699 for (i = 0; i < array_size(vtysh_client); i++)
3700 vtysh_client_run_all(&vtysh_client[i], accmd, 1, vtysh_ac_line,
3701 comps);
3702 vty->of = vty->of_saved;
3703 }
3704
3705 static const struct cmd_variable_handler vtysh_var_handler[] = {
3706 {/* match all */
3707 .tokenname = NULL,
3708 .varname = NULL,
3709 .completions = vtysh_autocomplete},
3710 {.completions = NULL}};
3711
3712 void vtysh_uninit(void)
3713 {
3714 if (vty->of != stdout)
3715 fclose(vty->of);
3716 }
3717
3718 void vtysh_init_vty(void)
3719 {
3720 /* Make vty structure. */
3721 vty = vty_new();
3722 vty->type = VTY_SHELL;
3723 vty->node = VIEW_NODE;
3724
3725 /* set default output */
3726 vty->of = stdout;
3727 vtysh_pager_envdef(false);
3728
3729 /* Initialize commands. */
3730 cmd_init(0);
3731 cmd_variable_handler_register(vtysh_var_handler);
3732
3733 /* Install nodes. */
3734 install_node(&bgp_node, NULL);
3735 install_node(&rip_node, NULL);
3736 install_node(&interface_node, NULL);
3737 install_node(&pw_node, NULL);
3738 install_node(&link_params_node, NULL);
3739 install_node(&vrf_node, NULL);
3740 install_node(&nh_group_node, NULL);
3741 install_node(&rmap_node, NULL);
3742 install_node(&pbr_map_node, NULL);
3743 install_node(&zebra_node, NULL);
3744 install_node(&bgp_vpnv4_node, NULL);
3745 install_node(&bgp_vpnv6_node, NULL);
3746 install_node(&bgp_flowspecv4_node, NULL);
3747 install_node(&bgp_flowspecv6_node, NULL);
3748 install_node(&bgp_ipv4_node, NULL);
3749 install_node(&bgp_ipv4m_node, NULL);
3750 install_node(&bgp_ipv4l_node, NULL);
3751 install_node(&bgp_ipv6_node, NULL);
3752 install_node(&bgp_ipv6m_node, NULL);
3753 install_node(&bgp_ipv6l_node, NULL);
3754 install_node(&bgp_vrf_policy_node, NULL);
3755 install_node(&bgp_evpn_node, NULL);
3756 install_node(&bgp_evpn_vni_node, NULL);
3757 install_node(&bgp_vnc_defaults_node, NULL);
3758 install_node(&bgp_vnc_nve_group_node, NULL);
3759 install_node(&bgp_vnc_l2_group_node, NULL);
3760 install_node(&ospf_node, NULL);
3761 install_node(&eigrp_node, NULL);
3762 install_node(&babel_node, NULL);
3763 install_node(&ripng_node, NULL);
3764 install_node(&ospf6_node, NULL);
3765 install_node(&ldp_node, NULL);
3766 install_node(&ldp_ipv4_node, NULL);
3767 install_node(&ldp_ipv6_node, NULL);
3768 install_node(&ldp_ipv4_iface_node, NULL);
3769 install_node(&ldp_ipv6_iface_node, NULL);
3770 install_node(&ldp_l2vpn_node, NULL);
3771 install_node(&ldp_pseudowire_node, NULL);
3772 install_node(&keychain_node, NULL);
3773 install_node(&keychain_key_node, NULL);
3774 install_node(&isis_node, NULL);
3775 install_node(&openfabric_node, NULL);
3776 install_node(&vty_node, NULL);
3777 install_node(&rpki_node, NULL);
3778 install_node(&bmp_node, NULL);
3779 #if HAVE_BFDD > 0
3780 install_node(&bfd_node, NULL);
3781 install_node(&bfd_peer_node, NULL);
3782 #endif /* HAVE_BFDD */
3783
3784 struct cmd_node *node;
3785 for (unsigned int i = 0; i < vector_active(cmdvec); i++) {
3786 node = vector_slot(cmdvec, i);
3787 if (!node || node->node == VIEW_NODE)
3788 continue;
3789 vtysh_install_default(node->node);
3790 }
3791
3792 install_element(VIEW_NODE, &vtysh_enable_cmd);
3793 install_element(ENABLE_NODE, &vtysh_config_terminal_cmd);
3794 install_element(ENABLE_NODE, &vtysh_disable_cmd);
3795
3796 /* "exit" command. */
3797 install_element(VIEW_NODE, &vtysh_exit_all_cmd);
3798 install_element(CONFIG_NODE, &vtysh_exit_all_cmd);
3799 install_element(VIEW_NODE, &vtysh_quit_all_cmd);
3800 install_element(CONFIG_NODE, &vtysh_quit_all_cmd);
3801 install_element(RIP_NODE, &vtysh_exit_ripd_cmd);
3802 install_element(RIP_NODE, &vtysh_quit_ripd_cmd);
3803 install_element(RIPNG_NODE, &vtysh_exit_ripngd_cmd);
3804 install_element(RIPNG_NODE, &vtysh_quit_ripngd_cmd);
3805 install_element(OSPF_NODE, &vtysh_exit_ospfd_cmd);
3806 install_element(OSPF_NODE, &vtysh_quit_ospfd_cmd);
3807 install_element(EIGRP_NODE, &vtysh_exit_eigrpd_cmd);
3808 install_element(EIGRP_NODE, &vtysh_quit_eigrpd_cmd);
3809 install_element(BABEL_NODE, &vtysh_exit_babeld_cmd);
3810 install_element(BABEL_NODE, &vtysh_quit_babeld_cmd);
3811 install_element(OSPF6_NODE, &vtysh_exit_ospf6d_cmd);
3812 install_element(OSPF6_NODE, &vtysh_quit_ospf6d_cmd);
3813 #if defined(HAVE_LDPD)
3814 install_element(LDP_NODE, &vtysh_exit_ldpd_cmd);
3815 install_element(LDP_NODE, &vtysh_quit_ldpd_cmd);
3816 install_element(LDP_IPV4_NODE, &vtysh_exit_ldpd_cmd);
3817 install_element(LDP_IPV4_NODE, &vtysh_quit_ldpd_cmd);
3818 install_element(LDP_IPV4_NODE, &ldp_exit_address_family_cmd);
3819 install_element(LDP_IPV6_NODE, &vtysh_exit_ldpd_cmd);
3820 install_element(LDP_IPV6_NODE, &vtysh_quit_ldpd_cmd);
3821 install_element(LDP_IPV6_NODE, &ldp_exit_address_family_cmd);
3822 install_element(LDP_IPV4_IFACE_NODE, &vtysh_exit_ldpd_cmd);
3823 install_element(LDP_IPV4_IFACE_NODE, &vtysh_quit_ldpd_cmd);
3824 install_element(LDP_IPV6_IFACE_NODE, &vtysh_exit_ldpd_cmd);
3825 install_element(LDP_IPV6_IFACE_NODE, &vtysh_quit_ldpd_cmd);
3826 install_element(LDP_L2VPN_NODE, &vtysh_exit_ldpd_cmd);
3827 install_element(LDP_L2VPN_NODE, &vtysh_quit_ldpd_cmd);
3828 install_element(LDP_PSEUDOWIRE_NODE, &vtysh_exit_ldpd_cmd);
3829 install_element(LDP_PSEUDOWIRE_NODE, &vtysh_quit_ldpd_cmd);
3830 #endif
3831 install_element(BGP_NODE, &vtysh_exit_bgpd_cmd);
3832 install_element(BGP_NODE, &vtysh_quit_bgpd_cmd);
3833 install_element(BGP_VPNV4_NODE, &vtysh_exit_bgpd_cmd);
3834 install_element(BGP_VPNV4_NODE, &vtysh_quit_bgpd_cmd);
3835 install_element(BGP_VPNV6_NODE, &vtysh_exit_bgpd_cmd);
3836 install_element(BGP_VPNV6_NODE, &vtysh_quit_bgpd_cmd);
3837 install_element(BGP_FLOWSPECV4_NODE, &vtysh_exit_bgpd_cmd);
3838 install_element(BGP_FLOWSPECV4_NODE, &vtysh_quit_bgpd_cmd);
3839 install_element(BGP_FLOWSPECV6_NODE, &vtysh_exit_bgpd_cmd);
3840 install_element(BGP_FLOWSPECV6_NODE, &vtysh_quit_bgpd_cmd);
3841 install_element(BGP_IPV4_NODE, &vtysh_exit_bgpd_cmd);
3842 install_element(BGP_IPV4_NODE, &vtysh_quit_bgpd_cmd);
3843 install_element(BGP_IPV4M_NODE, &vtysh_exit_bgpd_cmd);
3844 install_element(BGP_IPV4M_NODE, &vtysh_quit_bgpd_cmd);
3845 install_element(BGP_IPV4L_NODE, &vtysh_exit_bgpd_cmd);
3846 install_element(BGP_IPV4L_NODE, &vtysh_quit_bgpd_cmd);
3847 install_element(BGP_IPV6_NODE, &vtysh_exit_bgpd_cmd);
3848 install_element(BGP_IPV6_NODE, &vtysh_quit_bgpd_cmd);
3849 install_element(BGP_IPV6M_NODE, &vtysh_exit_bgpd_cmd);
3850 install_element(BGP_IPV6M_NODE, &vtysh_quit_bgpd_cmd);
3851 install_element(BGP_EVPN_NODE, &vtysh_quit_bgpd_cmd);
3852 install_element(BGP_EVPN_NODE, &vtysh_exit_bgpd_cmd);
3853 install_element(BGP_EVPN_VNI_NODE, &vtysh_exit_bgpd_cmd);
3854 install_element(BGP_EVPN_VNI_NODE, &vtysh_quit_bgpd_cmd);
3855 install_element(BGP_IPV6L_NODE, &vtysh_exit_bgpd_cmd);
3856 install_element(BGP_IPV6L_NODE, &vtysh_quit_bgpd_cmd);
3857 #if defined(ENABLE_BGP_VNC)
3858 install_element(BGP_VRF_POLICY_NODE, &vtysh_exit_bgpd_cmd);
3859 install_element(BGP_VRF_POLICY_NODE, &vtysh_quit_bgpd_cmd);
3860 install_element(BGP_VNC_DEFAULTS_NODE, &vtysh_exit_bgpd_cmd);
3861 install_element(BGP_VNC_DEFAULTS_NODE, &vtysh_quit_bgpd_cmd);
3862 install_element(BGP_VNC_NVE_GROUP_NODE, &vtysh_exit_bgpd_cmd);
3863 install_element(BGP_VNC_NVE_GROUP_NODE, &vtysh_quit_bgpd_cmd);
3864 install_element(BGP_VNC_L2_GROUP_NODE, &vtysh_exit_bgpd_cmd);
3865 install_element(BGP_VNC_L2_GROUP_NODE, &vtysh_quit_bgpd_cmd);
3866 #endif
3867 install_element(ISIS_NODE, &vtysh_exit_isisd_cmd);
3868 install_element(ISIS_NODE, &vtysh_quit_isisd_cmd);
3869 install_element(OPENFABRIC_NODE, &vtysh_exit_fabricd_cmd);
3870 install_element(OPENFABRIC_NODE, &vtysh_quit_fabricd_cmd);
3871 install_element(KEYCHAIN_NODE, &vtysh_exit_ripd_cmd);
3872 install_element(KEYCHAIN_NODE, &vtysh_quit_ripd_cmd);
3873 install_element(KEYCHAIN_KEY_NODE, &vtysh_exit_ripd_cmd);
3874 install_element(KEYCHAIN_KEY_NODE, &vtysh_quit_ripd_cmd);
3875 install_element(RMAP_NODE, &vtysh_exit_rmap_cmd);
3876 install_element(RMAP_NODE, &vtysh_quit_rmap_cmd);
3877 install_element(PBRMAP_NODE, &vtysh_exit_pbr_map_cmd);
3878 install_element(PBRMAP_NODE, &vtysh_quit_pbr_map_cmd);
3879 #if HAVE_BFDD > 0
3880 /* Enter node. */
3881 install_element(CONFIG_NODE, &bfd_enter_cmd);
3882 install_element(BFD_NODE, &bfd_peer_enter_cmd);
3883
3884 /* Exit/quit node. */
3885 install_element(BFD_NODE, &vtysh_exit_bfdd_cmd);
3886 install_element(BFD_NODE, &vtysh_quit_bfdd_cmd);
3887 install_element(BFD_PEER_NODE, &vtysh_exit_bfdd_cmd);
3888 install_element(BFD_PEER_NODE, &vtysh_quit_bfdd_cmd);
3889
3890 /* End/exit all. */
3891 install_element(BFD_NODE, &vtysh_end_all_cmd);
3892 install_element(BFD_PEER_NODE, &vtysh_end_all_cmd);
3893 #endif /* HAVE_BFDD */
3894 install_element(VTY_NODE, &vtysh_exit_line_vty_cmd);
3895 install_element(VTY_NODE, &vtysh_quit_line_vty_cmd);
3896
3897 /* "end" command. */
3898 install_element(CONFIG_NODE, &vtysh_end_all_cmd);
3899 install_element(ENABLE_NODE, &vtysh_end_all_cmd);
3900 install_element(RIP_NODE, &vtysh_end_all_cmd);
3901 install_element(RIPNG_NODE, &vtysh_end_all_cmd);
3902 install_element(OSPF_NODE, &vtysh_end_all_cmd);
3903 install_element(EIGRP_NODE, &vtysh_end_all_cmd);
3904 install_element(BABEL_NODE, &vtysh_end_all_cmd);
3905 install_element(OSPF6_NODE, &vtysh_end_all_cmd);
3906 install_element(LDP_NODE, &vtysh_end_all_cmd);
3907 install_element(LDP_IPV4_NODE, &vtysh_end_all_cmd);
3908 install_element(LDP_IPV6_NODE, &vtysh_end_all_cmd);
3909 install_element(LDP_IPV4_IFACE_NODE, &vtysh_end_all_cmd);
3910 install_element(LDP_IPV6_IFACE_NODE, &vtysh_end_all_cmd);
3911 install_element(LDP_L2VPN_NODE, &vtysh_end_all_cmd);
3912 install_element(LDP_PSEUDOWIRE_NODE, &vtysh_end_all_cmd);
3913 install_element(BGP_NODE, &vtysh_end_all_cmd);
3914 install_element(BGP_IPV4_NODE, &vtysh_end_all_cmd);
3915 install_element(BGP_IPV4M_NODE, &vtysh_end_all_cmd);
3916 install_element(BGP_IPV4L_NODE, &vtysh_end_all_cmd);
3917 install_element(BGP_VPNV4_NODE, &vtysh_end_all_cmd);
3918 install_element(BGP_VPNV6_NODE, &vtysh_end_all_cmd);
3919 install_element(BGP_FLOWSPECV4_NODE, &vtysh_end_all_cmd);
3920 install_element(BGP_FLOWSPECV6_NODE, &vtysh_end_all_cmd);
3921 install_element(BGP_IPV6_NODE, &vtysh_end_all_cmd);
3922 install_element(BGP_IPV6M_NODE, &vtysh_end_all_cmd);
3923 install_element(BGP_IPV6L_NODE, &vtysh_end_all_cmd);
3924 install_element(BGP_VRF_POLICY_NODE, &vtysh_end_all_cmd);
3925 install_element(BGP_EVPN_NODE, &vtysh_end_all_cmd);
3926 install_element(BGP_EVPN_VNI_NODE, &vtysh_end_all_cmd);
3927 install_element(BGP_VNC_DEFAULTS_NODE, &vtysh_end_all_cmd);
3928 install_element(BGP_VNC_NVE_GROUP_NODE, &vtysh_end_all_cmd);
3929 install_element(BGP_VNC_L2_GROUP_NODE, &vtysh_end_all_cmd);
3930 install_element(ISIS_NODE, &vtysh_end_all_cmd);
3931 install_element(OPENFABRIC_NODE, &vtysh_end_all_cmd);
3932 install_element(KEYCHAIN_NODE, &vtysh_end_all_cmd);
3933 install_element(KEYCHAIN_KEY_NODE, &vtysh_end_all_cmd);
3934 install_element(RMAP_NODE, &vtysh_end_all_cmd);
3935 install_element(PBRMAP_NODE, &vtysh_end_all_cmd);
3936 install_element(VTY_NODE, &vtysh_end_all_cmd);
3937
3938 install_element(INTERFACE_NODE, &vtysh_end_all_cmd);
3939 install_element(INTERFACE_NODE, &vtysh_exit_interface_cmd);
3940 install_element(LINK_PARAMS_NODE, &exit_link_params_cmd);
3941 install_element(LINK_PARAMS_NODE, &vtysh_end_all_cmd);
3942 install_element(LINK_PARAMS_NODE, &vtysh_exit_interface_cmd);
3943 install_element(INTERFACE_NODE, &vtysh_quit_interface_cmd);
3944
3945 install_element(PW_NODE, &vtysh_end_all_cmd);
3946 install_element(PW_NODE, &vtysh_exit_interface_cmd);
3947 install_element(PW_NODE, &vtysh_quit_interface_cmd);
3948
3949 install_element(CONFIG_NODE, &vtysh_nexthop_group_cmd);
3950 install_element(NH_GROUP_NODE, &vtysh_end_all_cmd);
3951 install_element(NH_GROUP_NODE, &vtysh_exit_nexthop_group_cmd);
3952 install_element(NH_GROUP_NODE, &vtysh_quit_nexthop_group_cmd);
3953
3954 install_element(VRF_NODE, &vtysh_end_all_cmd);
3955 install_element(VRF_NODE, &vtysh_exit_vrf_cmd);
3956 install_element(VRF_NODE, &vtysh_quit_vrf_cmd);
3957
3958 install_element(CONFIG_NODE, &router_eigrp_cmd);
3959 install_element(CONFIG_NODE, &router_babel_cmd);
3960 install_element(CONFIG_NODE, &router_rip_cmd);
3961 install_element(CONFIG_NODE, &router_ripng_cmd);
3962 install_element(CONFIG_NODE, &router_ospf_cmd);
3963 install_element(CONFIG_NODE, &router_ospf6_cmd);
3964 #if defined(HAVE_LDPD)
3965 install_element(CONFIG_NODE, &ldp_mpls_ldp_cmd);
3966 install_element(LDP_NODE, &ldp_address_family_ipv4_cmd);
3967 install_element(LDP_NODE, &ldp_address_family_ipv6_cmd);
3968 install_element(LDP_IPV4_NODE, &ldp_interface_ifname_cmd);
3969 install_element(LDP_IPV6_NODE, &ldp_interface_ifname_cmd);
3970 install_element(CONFIG_NODE, &ldp_l2vpn_word_type_vpls_cmd);
3971 install_element(LDP_L2VPN_NODE, &ldp_member_pseudowire_ifname_cmd);
3972 #endif
3973 install_element(CONFIG_NODE, &router_isis_cmd);
3974 install_element(CONFIG_NODE, &router_openfabric_cmd);
3975 install_element(CONFIG_NODE, &router_bgp_cmd);
3976 #ifdef KEEP_OLD_VPN_COMMANDS
3977 install_element(BGP_NODE, &address_family_vpnv4_cmd);
3978 install_element(BGP_NODE, &address_family_vpnv6_cmd);
3979 #endif /* KEEP_OLD_VPN_COMMANDS */
3980 #if defined(ENABLE_BGP_VNC)
3981 install_element(BGP_NODE, &vnc_vrf_policy_cmd);
3982 install_element(BGP_NODE, &vnc_defaults_cmd);
3983 install_element(BGP_NODE, &vnc_nve_group_cmd);
3984 install_element(BGP_NODE, &vnc_l2_group_cmd);
3985 #endif
3986 install_element(BGP_NODE, &address_family_ipv4_cmd);
3987 install_element(BGP_NODE, &address_family_ipv4_multicast_cmd);
3988 install_element(BGP_NODE, &address_family_ipv4_vpn_cmd);
3989 install_element(BGP_NODE, &address_family_ipv4_labeled_unicast_cmd);
3990 install_element(BGP_NODE, &address_family_ipv6_cmd);
3991 install_element(BGP_NODE, &address_family_ipv6_multicast_cmd);
3992 install_element(BGP_NODE, &address_family_ipv6_vpn_cmd);
3993 install_element(BGP_NODE, &address_family_ipv6_labeled_unicast_cmd);
3994 install_element(BGP_NODE, &address_family_evpn_cmd);
3995 install_element(BGP_NODE, &address_family_flowspecv4_cmd);
3996 install_element(BGP_NODE, &address_family_flowspecv6_cmd);
3997 #if defined(HAVE_CUMULUS)
3998 install_element(BGP_NODE, &address_family_evpn2_cmd);
3999 #endif
4000 install_element(BGP_VPNV4_NODE, &exit_address_family_cmd);
4001 install_element(BGP_VPNV6_NODE, &exit_address_family_cmd);
4002 install_element(BGP_IPV4_NODE, &exit_address_family_cmd);
4003 install_element(BGP_IPV4M_NODE, &exit_address_family_cmd);
4004 install_element(BGP_IPV4L_NODE, &exit_address_family_cmd);
4005 install_element(BGP_IPV6_NODE, &exit_address_family_cmd);
4006 install_element(BGP_IPV6M_NODE, &exit_address_family_cmd);
4007 install_element(BGP_EVPN_NODE, &exit_address_family_cmd);
4008 install_element(BGP_IPV6L_NODE, &exit_address_family_cmd);
4009 install_element(BGP_FLOWSPECV4_NODE, &exit_address_family_cmd);
4010 install_element(BGP_FLOWSPECV6_NODE, &exit_address_family_cmd);
4011
4012 install_element(BGP_NODE, &bmp_targets_cmd);
4013 install_element(BMP_NODE, &bmp_exit_cmd);
4014 install_element(BMP_NODE, &bmp_quit_cmd);
4015 install_element(BMP_NODE, &vtysh_end_all_cmd);
4016
4017 install_element(CONFIG_NODE, &rpki_cmd);
4018 install_element(RPKI_NODE, &rpki_exit_cmd);
4019 install_element(RPKI_NODE, &rpki_quit_cmd);
4020 install_element(RPKI_NODE, &vtysh_end_all_cmd);
4021
4022 /* EVPN commands */
4023 install_element(BGP_EVPN_NODE, &bgp_evpn_vni_cmd);
4024 install_element(BGP_EVPN_VNI_NODE, &exit_vni_cmd);
4025
4026 install_element(BGP_VRF_POLICY_NODE, &exit_vrf_policy_cmd);
4027 install_element(BGP_VNC_DEFAULTS_NODE, &exit_vnc_config_cmd);
4028 install_element(BGP_VNC_NVE_GROUP_NODE, &exit_vnc_config_cmd);
4029 install_element(BGP_VNC_L2_GROUP_NODE, &exit_vnc_config_cmd);
4030
4031 install_element(CONFIG_NODE, &key_chain_cmd);
4032 install_element(CONFIG_NODE, &vtysh_route_map_cmd);
4033 install_element(CONFIG_NODE, &vtysh_pbr_map_cmd);
4034 install_element(CONFIG_NODE, &vtysh_no_pbr_map_cmd);
4035 install_element(CONFIG_NODE, &vtysh_line_vty_cmd);
4036 install_element(KEYCHAIN_NODE, &key_cmd);
4037 install_element(KEYCHAIN_NODE, &key_chain_cmd);
4038 install_element(KEYCHAIN_KEY_NODE, &key_chain_cmd);
4039 install_element(CONFIG_NODE, &vtysh_interface_cmd);
4040 install_element(CONFIG_NODE, &vtysh_pseudowire_cmd);
4041 install_element(INTERFACE_NODE, &vtysh_link_params_cmd);
4042 install_element(ENABLE_NODE, &vtysh_show_running_config_cmd);
4043 install_element(ENABLE_NODE, &vtysh_copy_running_config_cmd);
4044 install_element(ENABLE_NODE, &vtysh_copy_to_running_cmd);
4045
4046 install_element(CONFIG_NODE, &vtysh_vrf_cmd);
4047 install_element(VRF_NODE, &vtysh_vrf_netns_cmd);
4048 install_element(VRF_NODE, &vtysh_no_vrf_netns_cmd);
4049 install_element(VRF_NODE, &exit_vrf_config_cmd);
4050
4051 install_element(CONFIG_NODE, &vtysh_no_nexthop_group_cmd);
4052
4053 /* "write terminal" command. */
4054 install_element(ENABLE_NODE, &vtysh_write_terminal_cmd);
4055
4056 install_element(CONFIG_NODE, &vtysh_integrated_config_cmd);
4057 install_element(CONFIG_NODE, &no_vtysh_integrated_config_cmd);
4058
4059 /* "write memory" command. */
4060 install_element(ENABLE_NODE, &vtysh_write_memory_cmd);
4061
4062 install_element(CONFIG_NODE, &vtysh_terminal_paginate_cmd);
4063 install_element(VIEW_NODE, &vtysh_terminal_paginate_cmd);
4064 install_element(VIEW_NODE, &vtysh_terminal_length_cmd);
4065 install_element(VIEW_NODE, &vtysh_terminal_no_length_cmd);
4066 install_element(VIEW_NODE, &vtysh_show_daemons_cmd);
4067
4068 install_element(VIEW_NODE, &vtysh_ping_cmd);
4069 install_element(VIEW_NODE, &vtysh_ping_ip_cmd);
4070 install_element(VIEW_NODE, &vtysh_traceroute_cmd);
4071 install_element(VIEW_NODE, &vtysh_traceroute_ip_cmd);
4072 install_element(VIEW_NODE, &vtysh_mtrace_cmd);
4073 install_element(VIEW_NODE, &vtysh_ping6_cmd);
4074 install_element(VIEW_NODE, &vtysh_traceroute6_cmd);
4075 #if defined(HAVE_SHELL_ACCESS)
4076 install_element(VIEW_NODE, &vtysh_telnet_cmd);
4077 install_element(VIEW_NODE, &vtysh_telnet_port_cmd);
4078 install_element(VIEW_NODE, &vtysh_ssh_cmd);
4079 #endif
4080 #if defined(HAVE_SHELL_ACCESS)
4081 install_element(ENABLE_NODE, &vtysh_start_shell_cmd);
4082 install_element(ENABLE_NODE, &vtysh_start_bash_cmd);
4083 install_element(ENABLE_NODE, &vtysh_start_zsh_cmd);
4084 #endif
4085
4086 /* debugging */
4087 install_element(VIEW_NODE, &vtysh_show_debugging_cmd);
4088 install_element(VIEW_NODE, &vtysh_show_error_code_cmd);
4089 install_element(VIEW_NODE, &vtysh_show_debugging_hashtable_cmd);
4090 install_element(ENABLE_NODE, &vtysh_debug_all_cmd);
4091 install_element(CONFIG_NODE, &vtysh_debug_all_cmd);
4092 install_element(ENABLE_NODE, &vtysh_debug_memstats_cmd);
4093 install_element(CONFIG_NODE, &vtysh_debug_memstats_cmd);
4094
4095 /* northbound */
4096 install_element(VIEW_NODE, &show_yang_operational_data_cmd);
4097 install_element(ENABLE_NODE, &debug_nb_cmd);
4098 install_element(CONFIG_NODE, &debug_nb_cmd);
4099
4100 /* misc lib show commands */
4101 install_element(VIEW_NODE, &vtysh_show_memory_cmd);
4102 install_element(VIEW_NODE, &vtysh_show_modules_cmd);
4103 install_element(VIEW_NODE, &vtysh_show_work_queues_cmd);
4104 install_element(VIEW_NODE, &vtysh_show_work_queues_daemon_cmd);
4105 #ifndef EXCLUDE_CPU_TIME
4106 install_element(VIEW_NODE, &vtysh_show_thread_cmd);
4107 #endif
4108 install_element(VIEW_NODE, &vtysh_show_poll_cmd);
4109
4110 /* Logging */
4111 install_element(VIEW_NODE, &vtysh_show_logging_cmd);
4112 install_element(VIEW_NODE, &vtysh_show_log_filter_cmd);
4113 install_element(CONFIG_NODE, &vtysh_log_filter_cmd);
4114 install_element(CONFIG_NODE, &vtysh_log_filter_clear_cmd);
4115 install_element(CONFIG_NODE, &vtysh_log_stdout_cmd);
4116 install_element(CONFIG_NODE, &vtysh_log_stdout_level_cmd);
4117 install_element(CONFIG_NODE, &no_vtysh_log_stdout_cmd);
4118 install_element(CONFIG_NODE, &vtysh_log_file_cmd);
4119 install_element(CONFIG_NODE, &vtysh_log_file_level_cmd);
4120 install_element(CONFIG_NODE, &no_vtysh_log_file_cmd);
4121 install_element(CONFIG_NODE, &vtysh_log_monitor_cmd);
4122 install_element(CONFIG_NODE, &no_vtysh_log_monitor_cmd);
4123 install_element(CONFIG_NODE, &vtysh_log_syslog_cmd);
4124 install_element(CONFIG_NODE, &no_vtysh_log_syslog_cmd);
4125 install_element(CONFIG_NODE, &vtysh_log_facility_cmd);
4126 install_element(CONFIG_NODE, &no_vtysh_log_facility_cmd);
4127 install_element(CONFIG_NODE, &vtysh_log_record_priority_cmd);
4128 install_element(CONFIG_NODE, &no_vtysh_log_record_priority_cmd);
4129 install_element(CONFIG_NODE, &vtysh_log_timestamp_precision_cmd);
4130 install_element(CONFIG_NODE, &no_vtysh_log_timestamp_precision_cmd);
4131
4132 install_element(CONFIG_NODE, &vtysh_service_password_encrypt_cmd);
4133 install_element(CONFIG_NODE, &no_vtysh_service_password_encrypt_cmd);
4134
4135 install_element(CONFIG_NODE, &vtysh_password_cmd);
4136 install_element(CONFIG_NODE, &no_vtysh_password_cmd);
4137 install_element(CONFIG_NODE, &vtysh_enable_password_cmd);
4138 install_element(CONFIG_NODE, &no_vtysh_enable_password_cmd);
4139 }