1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* Configuration generator.
3 * Copyright (C) 2000 Kunihiro Ishiguro
13 #include "vtysh/vtysh.h"
14 #include "vtysh/vtysh_user.h"
16 DEFINE_MGROUP(MVTYSH
, "vtysh");
17 DEFINE_MTYPE_STATIC(MVTYSH
, VTYSH_CONFIG
, "Vtysh configuration");
18 DEFINE_MTYPE_STATIC(MVTYSH
, VTYSH_CONFIG_LINE
, "Vtysh configuration line");
22 PREDECL_LIST(config_master
);
23 PREDECL_HASH(config_master_hash
);
26 /* Configuration node name. */
29 /* Configuration string line. */
32 /* Configuration can be nested. */
33 struct config
*parent
;
39 /* Index of this config. */
42 /* Node entry for the typed Red-black tree */
43 struct config_master_item rbt_item
;
44 struct config_master_hash_item hash_item
;
47 struct list
*config_top
;
49 static int line_cmp(char *c1
, char *c2
)
51 return strcmp(c1
, c2
);
54 static void line_del(char *line
)
56 XFREE(MTYPE_VTYSH_CONFIG_LINE
, line
);
59 static struct config
*config_new(void)
61 struct config
*config
;
62 config
= XCALLOC(MTYPE_VTYSH_CONFIG
, sizeof(struct config
));
66 static void config_del(struct config
*config
)
68 vector_free(config
->nested
);
69 list_delete(&config
->line
);
71 XFREE(MTYPE_VTYSH_CONFIG_LINE
, config
->exit
);
72 XFREE(MTYPE_VTYSH_CONFIG_LINE
, config
->name
);
73 XFREE(MTYPE_VTYSH_CONFIG
, config
);
76 static int config_cmp(const struct config
*c1
, const struct config
*c2
)
78 return strcmp(c1
->name
, c2
->name
);
81 static uint32_t config_hash(const struct config
*c
)
83 return string_hash_make(c
->name
);
86 DECLARE_LIST(config_master
, struct config
, rbt_item
);
87 DECLARE_HASH(config_master_hash
, struct config
, hash_item
, config_cmp
,
91 * The config_master_head is a list for order of receipt
92 * The hash is for quick lookup under this NODE
94 struct configuration
{
95 struct config_master_head master
;
96 struct config_master_hash_head hash_master
;
99 static struct config
*config_get_vec(vector vec
, int index
, const char *line
)
101 struct config
*config
, *config_loop
;
102 struct configuration
*configuration
;
103 struct config lookup
;
105 config
= config_loop
= NULL
;
107 configuration
= vector_lookup_ensure(vec
, index
);
109 if (!configuration
) {
110 configuration
= XMALLOC(MTYPE_VTYSH_CONFIG
,
111 sizeof(struct configuration
));
112 config_master_init(&configuration
->master
);
113 config_master_hash_init(&configuration
->hash_master
);
114 vector_set_index(vec
, index
, configuration
);
117 lookup
.name
= (char *)line
;
118 config
= config_master_hash_find(&configuration
->hash_master
, &lookup
);
121 config
= config_new();
122 config
->line
= list_new();
123 config
->line
->del
= (void (*)(void *))line_del
;
124 config
->line
->cmp
= (int (*)(void *, void *))line_cmp
;
125 config
->name
= XSTRDUP(MTYPE_VTYSH_CONFIG_LINE
, line
);
127 config
->index
= index
;
128 config
->nested
= vector_init(1);
129 config_master_add_tail(&configuration
->master
, config
);
130 config_master_hash_add(&configuration
->hash_master
, config
);
135 static struct config
*config_get(int index
, const char *line
)
137 return config_get_vec(configvec
, index
, line
);
140 static struct config
*config_get_nested(struct config
*parent
, int index
,
143 struct config
*config
;
145 config
= config_get_vec(parent
->nested
, index
, line
);
146 config
->parent
= parent
;
151 void config_add_line(struct list
*config
, const char *line
)
153 listnode_add(config
, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE
, line
));
156 static void config_add_line_uniq(struct list
*config
, const char *line
)
158 struct listnode
*node
, *nnode
;
161 for (ALL_LIST_ELEMENTS(config
, node
, nnode
, pnt
)) {
162 if (strcmp(pnt
, line
) == 0)
165 listnode_add_sort(config
, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE
, line
));
169 * Add a line that should only be shown once, and always show at the end of the
172 * If the line already exists, it will be moved to the end of the block. If it
173 * does not exist, it will be added at the end of the block.
175 * Note that this only makes sense when there is just one such line that should
176 * show up at the very end of a config block. Furthermore, if the same block
177 * can show up from multiple daemons, all of them must make sure to print the
178 * line at the end of their config, otherwise the line will show at the end of
179 * the config for the last daemon that printed it.
181 * Here is a motivating example with the 'exit-vrf' command. Suppose we receive
182 * a config from Zebra like so:
189 * Then suppose we later receive this config from PIM:
192 * ip msdp mesh-group MyGroup member 1.2.3.4
195 * Then we will combine them into one config block like so:
200 * ip msdp mesh-group MyGroup member 1.2.3.4
203 * Because PIM also sent us an 'exit-vrf', we noticed that we already had one
204 * under the 'vrf BLUE' config block and so we moved it to the end of the
205 * config block again. If PIM had neglected to send us 'exit-vrf', the result
212 * ip msdp mesh-group MyGroup member 1.2.3.4
214 * Therefore, daemons that share config blocks must take care to consistently
215 * print the same block terminators.
217 * Ideally this would be solved by adding a string to struct config that is
218 * always printed at the end when dumping a config. However, this would only
219 * work when the user is using integrated config. In the non-integrated config
220 * case, daemons are responsible for writing their own config files, and so the
221 * must be able to print these blocks correctly independently of vtysh, which
222 * means they are the ones that need to handle printing the block terminators
223 * and VTYSH needs to be smart enough to combine them properly.
228 * The config to add the line to
231 * The line to add to the end of the config
233 static void config_add_line_uniq_end(struct list
*config
, const char *line
)
235 struct listnode
*node
;
238 for (ALL_LIST_ELEMENTS_RO(config
, node
, pnt
)) {
239 if (strcmp(pnt
, line
) == 0)
244 config_add_line(config
, line
);
246 listnode_move_to_tail(config
, node
);
249 static void config_add_line_head(struct list
*config
, const char *line
)
251 listnode_add_head(config
, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE
, line
));
254 void vtysh_config_parse_line(void *arg
, const char *line
)
257 static struct config
*config
= NULL
;
268 /* Suppress exclamation points ! and commented lines. The !s are
270 * dynamically in vtysh_config_dump() */
275 /* Store line to current configuration. */
277 if (config
->index
== KEYCHAIN_NODE
278 && strncmp(line
, " key", strlen(" key")) == 0) {
279 config
= config_get_nested(
280 config
, KEYCHAIN_KEY_NODE
, line
);
281 } else if (config
->index
== KEYCHAIN_KEY_NODE
) {
282 if (strncmp(line
, " exit", strlen(" exit"))
284 config_add_line_uniq_end(config
->line
,
286 config
= config
->parent
;
288 config_add_line_uniq(config
->line
,
291 } else if (strncmp(line
, " link-params",
292 strlen(" link-params"))
294 config_add_line(config
->line
, line
);
295 config
->index
= LINK_PARAMS_NODE
;
296 } else if (strncmp(line
, " ip multicast boundary",
297 strlen(" ip multicast boundary"))
299 config_add_line_uniq_end(config
->line
, line
);
300 } else if (strncmp(line
, " ip igmp query-interval",
301 strlen(" ip igmp query-interval"))
303 config_add_line_uniq_end(config
->line
, line
);
304 } else if (config
->index
== LINK_PARAMS_NODE
305 && strncmp(line
, " exit-link-params",
308 config_add_line(config
->line
, line
);
309 config
->index
= INTERFACE_NODE
;
310 } else if (!strncmp(line
, " vrrp", strlen(" vrrp"))
311 || !strncmp(line
, " no vrrp",
312 strlen(" no vrrp"))) {
313 config_add_line(config
->line
, line
);
314 } else if (!strncmp(line
, " ip mroute",
315 strlen(" ip mroute"))) {
316 config_add_line_uniq_end(config
->line
, line
);
317 } else if (config
->index
== RMAP_NODE
||
318 config
->index
== INTERFACE_NODE
||
319 config
->index
== VTY_NODE
)
320 config_add_line_uniq(config
->line
, line
);
321 else if (config
->index
== NH_GROUP_NODE
) {
322 if (strncmp(line
, " resilient",
323 strlen(" resilient")) == 0)
324 config_add_line_head(config
->line
,
327 config_add_line_uniq_end(config
->line
,
330 config_add_line(config
->line
, line
);
332 config_add_line(config_top
, line
);
335 if (strncmp(line
, "exit", strlen("exit")) == 0) {
338 XFREE(MTYPE_VTYSH_CONFIG_LINE
,
341 XSTRDUP(MTYPE_VTYSH_CONFIG_LINE
, line
);
343 } else if (strncmp(line
, "interface", strlen("interface")) == 0)
344 config
= config_get(INTERFACE_NODE
, line
);
345 else if (strncmp(line
, "pseudowire", strlen("pseudowire")) == 0)
346 config
= config_get(PW_NODE
, line
);
347 else if (strncmp(line
, "vrf", strlen("vrf")) == 0)
348 config
= config_get(VRF_NODE
, line
);
349 else if (strncmp(line
, "nexthop-group", strlen("nexthop-group"))
351 config
= config_get(NH_GROUP_NODE
, line
);
352 else if (strncmp(line
, "router-id", strlen("router-id")) == 0)
353 config
= config_get(ZEBRA_NODE
, line
);
354 else if (strncmp(line
, "router rip", strlen("router rip")) == 0)
355 config
= config_get(RIP_NODE
, line
);
356 else if (strncmp(line
, "router ripng", strlen("router ripng"))
358 config
= config_get(RIPNG_NODE
, line
);
359 else if (strncmp(line
, "router eigrp", strlen("router eigrp"))
361 config
= config_get(EIGRP_NODE
, line
);
362 else if (strncmp(line
, "router babel", strlen("router babel"))
364 config
= config_get(BABEL_NODE
, line
);
365 else if (strncmp(line
, "router ospf", strlen("router ospf"))
367 config
= config_get(OSPF_NODE
, line
);
368 else if (strncmp(line
, "router ospf6", strlen("router ospf6"))
370 config
= config_get(OSPF6_NODE
, line
);
371 else if (strncmp(line
, "mpls ldp", strlen("mpls ldp")) == 0)
372 config
= config_get(LDP_NODE
, line
);
373 else if (strncmp(line
, "l2vpn", strlen("l2vpn")) == 0)
374 config
= config_get(LDP_L2VPN_NODE
, line
);
375 else if (strncmp(line
, "router bgp", strlen("router bgp")) == 0)
376 config
= config_get(BGP_NODE
, line
);
377 else if (strncmp(line
, "router isis", strlen("router isis"))
379 config
= config_get(ISIS_NODE
, line
);
380 else if (strncmp(line
, "router openfabric", strlen("router openfabric"))
382 config
= config_get(OPENFABRIC_NODE
, line
);
383 else if (strncmp(line
, "affinity-map",
384 strlen("affinity-map")) == 0)
385 config
= config_get(AFFMAP_NODE
, line
);
386 else if (strncmp(line
, "route-map", strlen("route-map")) == 0)
387 config
= config_get(RMAP_NODE
, line
);
388 else if (strncmp(line
, "no route-map", strlen("no route-map"))
390 config
= config_get(RMAP_NODE
, line
);
391 else if (strncmp(line
, "pbr-map", strlen("pbr-map")) == 0)
392 config
= config_get(PBRMAP_NODE
, line
);
393 else if (strncmp(line
, "access-list", strlen("access-list"))
395 config
= config_get(ACCESS_NODE
, line
);
396 else if (strncmp(line
, "ipv6 access-list",
397 strlen("ipv6 access-list"))
399 config
= config_get(ACCESS_IPV6_NODE
, line
);
400 else if (strncmp(line
, "mac access-list",
401 strlen("mac access-list"))
403 config
= config_get(ACCESS_MAC_NODE
, line
);
404 else if (strncmp(line
, "ip prefix-list",
405 strlen("ip prefix-list"))
407 config
= config_get(PREFIX_NODE
, line
);
408 else if (strncmp(line
, "ipv6 prefix-list",
409 strlen("ipv6 prefix-list"))
411 config
= config_get(PREFIX_IPV6_NODE
, line
);
412 else if (strncmp(line
, "bgp as-path access-list",
413 strlen("bgp as-path access-list"))
415 config
= config_get(AS_LIST_NODE
, line
);
416 else if (strncmp(line
, "bgp community-list",
417 strlen("bgp community-list"))
419 || strncmp(line
, "bgp extcommunity-list",
420 strlen("bgp extcommunity-list"))
422 || strncmp(line
, "bgp large-community-list",
423 strlen("bgp large-community-list"))
425 config
= config_get(COMMUNITY_LIST_NODE
, line
);
426 else if (strncmp(line
, "bgp community alias",
427 strlen("bgp community alias")) == 0)
428 config
= config_get(COMMUNITY_ALIAS_NODE
, line
);
429 else if (strncmp(line
, "ip route", strlen("ip route")) == 0)
430 config
= config_get(IP_NODE
, line
);
431 else if (strncmp(line
, "ipv6 route", strlen("ipv6 route")) == 0)
432 config
= config_get(IP_NODE
, line
);
433 else if (strncmp(line
, "key", strlen("key")) == 0)
434 config
= config_get(KEYCHAIN_NODE
, line
);
435 else if (strncmp(line
, "line", strlen("line")) == 0)
436 config
= config_get(VTY_NODE
, line
);
437 else if ((strncmp(line
, "ipv6 forwarding",
438 strlen("ipv6 forwarding"))
440 || (strncmp(line
, "ip forwarding",
441 strlen("ip forwarding"))
443 config
= config_get(FORWARDING_NODE
, line
);
444 else if (strncmp(line
, "debug vrf", strlen("debug vrf")) == 0)
445 config
= config_get(VRF_DEBUG_NODE
, line
);
446 else if (strncmp(line
, "debug northbound",
447 strlen("debug northbound"))
449 config
= config_get(NORTHBOUND_DEBUG_NODE
, line
);
450 else if (strncmp(line
, "debug route-map",
451 strlen("debug route-map"))
453 config
= config_get(RMAP_DEBUG_NODE
, line
);
454 else if (strncmp(line
, "debug resolver",
455 strlen("debug resolver")) == 0)
456 config
= config_get(RESOLVER_DEBUG_NODE
, line
);
457 else if (strncmp(line
, "debug", strlen("debug")) == 0)
458 config
= config_get(DEBUG_NODE
, line
);
459 else if (strncmp(line
, "password", strlen("password")) == 0
460 || strncmp(line
, "enable password",
461 strlen("enable password"))
463 config
= config_get(AAA_NODE
, line
);
464 else if (strncmp(line
, "ip protocol", strlen("ip protocol"))
466 config
= config_get(PROTOCOL_NODE
, line
);
467 else if (strncmp(line
, "ipv6 protocol", strlen("ipv6 protocol"))
469 config
= config_get(PROTOCOL_NODE
, line
);
470 else if (strncmp(line
, "ip nht", strlen("ip nht")) == 0)
471 config
= config_get(PROTOCOL_NODE
, line
);
472 else if (strncmp(line
, "ipv6 nht", strlen("ipv6 nht")) == 0)
473 config
= config_get(PROTOCOL_NODE
, line
);
474 else if (strncmp(line
, "mpls", strlen("mpls")) == 0)
475 config
= config_get(MPLS_NODE
, line
);
476 else if (strncmp(line
, "segment-routing",
477 strlen("segment-routing"))
479 config
= config_get(SEGMENT_ROUTING_NODE
, line
);
480 else if (strncmp(line
, "bfd", strlen("bfd")) == 0)
481 config
= config_get(BFD_NODE
, line
);
482 else if (strncmp(line
, "rpki", strlen("rpki")) == 0)
483 config
= config_get(RPKI_NODE
, line
);
485 if (strncmp(line
, "log", strlen("log")) == 0 ||
486 strncmp(line
, "hostname", strlen("hostname")) ==
488 strncmp(line
, "domainname", strlen("domainname")) ==
490 strncmp(line
, "allow-reserved-ranges",
491 strlen("allow-reserved-ranges")) == 0 ||
492 strncmp(line
, "frr", strlen("frr")) == 0 ||
493 strncmp(line
, "agentx", strlen("agentx")) == 0 ||
494 strncmp(line
, "no log", strlen("no log")) == 0 ||
495 strncmp(line
, "no ip prefix-list",
496 strlen("no ip prefix-list")) == 0 ||
497 strncmp(line
, "no ipv6 prefix-list",
498 strlen("no ipv6 prefix-list")) == 0 ||
499 strncmp(line
, "service ", strlen("service ")) ==
501 strncmp(line
, "no service cputime-stats",
502 strlen("no service cputime-stats")) == 0 ||
503 strncmp(line
, "service cputime-warning",
504 strlen("service cputime-warning")) == 0)
505 config_add_line_uniq(config_top
, line
);
507 config_add_line(config_top
, line
);
514 /* Macro to check delimiter is needed between each configuration line
516 #define NO_DELIMITER(I) \
517 ((I) == AFFMAP_NODE || (I) == ACCESS_NODE || (I) == PREFIX_NODE || \
518 (I) == IP_NODE || (I) == AS_LIST_NODE || \
519 (I) == COMMUNITY_LIST_NODE || (I) == COMMUNITY_ALIAS_NODE || \
520 (I) == ACCESS_IPV6_NODE || (I) == ACCESS_MAC_NODE || \
521 (I) == PREFIX_IPV6_NODE || (I) == FORWARDING_NODE || \
522 (I) == DEBUG_NODE || (I) == AAA_NODE || (I) == VRF_DEBUG_NODE || \
523 (I) == NORTHBOUND_DEBUG_NODE || (I) == RMAP_DEBUG_NODE || \
524 (I) == RESOLVER_DEBUG_NODE || (I) == MPLS_NODE || \
525 (I) == KEYCHAIN_KEY_NODE)
527 static void configvec_dump(vector vec
, bool nested
)
529 struct listnode
*mnode
, *mnnode
;
530 struct config
*config
;
531 struct configuration
*configuration
;
535 for (i
= 0; i
< vector_active(vec
); i
++)
536 if ((configuration
= vector_slot(vec
, i
)) != NULL
) {
537 while ((config
= config_master_pop(
538 &configuration
->master
))) {
539 config_master_hash_del(
540 &configuration
->hash_master
, config
);
541 /* Don't print empty sections for interface.
543 * other hand could have a legitimate empty
544 * section at the end.
545 * VRF is handled in the backend, we could have
546 * "configured" VRFs with static routes which
547 * are not under the VRF node.
549 if (config
->index
== INTERFACE_NODE
550 && (listcount(config
->line
) == 1)
551 && (line
= listnode_head(config
->line
))
552 && strmatch(line
, "exit")) {
557 vty_out(vty
, "%s\n", config
->name
);
559 for (ALL_LIST_ELEMENTS(config
->line
, mnode
,
561 vty_out(vty
, "%s\n", line
);
563 configvec_dump(config
->nested
, true);
566 vty_out(vty
, "%s\n", config
->exit
);
568 if (!NO_DELIMITER(i
))
573 config_master_fini(&configuration
->master
);
574 config_master_hash_fini(&configuration
->hash_master
);
575 XFREE(MTYPE_VTYSH_CONFIG
, configuration
);
576 vector_slot(vec
, i
) = NULL
;
577 if (!nested
&& NO_DELIMITER(i
))
582 void vtysh_config_dump(void)
584 struct listnode
*node
, *nnode
;
587 for (ALL_LIST_ELEMENTS(config_top
, node
, nnode
, line
))
588 vty_out(vty
, "%s\n", line
);
590 list_delete_all_node(config_top
);
594 configvec_dump(configvec
, false);
597 /* Read up configuration file from file_name. */
598 static int vtysh_read_file(FILE *confp
, bool dry_run
)
604 vty
->wfd
= STDERR_FILENO
;
605 vty
->type
= VTY_TERM
;
606 vty
->node
= CONFIG_NODE
;
608 vtysh_execute_no_pager("enable");
609 vtysh_execute_no_pager("configure terminal");
612 vtysh_execute_no_pager("XFRR_start_configuration");
614 /* Execute configuration file. */
615 ret
= vtysh_config_from_file(vty
, confp
);
618 vtysh_execute_no_pager("XFRR_end_configuration");
620 vtysh_execute_no_pager("end");
621 vtysh_execute_no_pager("disable");
628 /* Read up configuration file from config_default_dir. */
629 int vtysh_read_config(const char *config_default_dir
, bool dry_run
)
635 confp
= fopen(config_default_dir
, "r");
638 "%% Can't open configuration file %s due to '%s'.\n",
639 config_default_dir
, safe_strerror(errno
));
640 return CMD_ERR_NO_FILE
;
643 save
= vtysh_add_timestamp
;
644 vtysh_add_timestamp
= false;
646 ret
= vtysh_read_file(confp
, dry_run
);
649 vtysh_add_timestamp
= save
;
654 /* We don't write vtysh specific into file from vtysh. vtysh.conf should
655 * be edited by hand. So, we handle only "write terminal" case here and
656 * integrate vtysh specific conf with conf from daemons.
658 void vtysh_config_write(void)
663 name
= cmd_hostname_get();
664 if (name
&& name
[0] != '\0') {
665 snprintf(line
, sizeof(line
), "hostname %s", name
);
666 vtysh_config_parse_line(NULL
, line
);
669 name
= cmd_domainname_get();
670 if (name
&& name
[0] != '\0') {
671 snprintf(line
, sizeof(line
), "domainname %s", name
);
672 vtysh_config_parse_line(NULL
, line
);
675 if (vtysh_write_integrated
== WRITE_INTEGRATED_NO
)
676 vtysh_config_parse_line(NULL
,
677 "no service integrated-vtysh-config");
678 if (vtysh_write_integrated
== WRITE_INTEGRATED_YES
)
679 vtysh_config_parse_line(NULL
,
680 "service integrated-vtysh-config");
685 void vtysh_config_init(void)
687 config_top
= list_new();
688 config_top
->del
= (void (*)(void *))line_del
;
689 configvec
= vector_init(1);