]> git.proxmox.com Git - mirror_frr.git/blob - vtysh/vtysh_config.c
zebra: Allow ns delete to happen after under/over flow checks
[mirror_frr.git] / vtysh / vtysh_config.c
1 /* Configuration generator.
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 "command.h"
24 #include "linklist.h"
25 #include "memory.h"
26
27 #include "vtysh/vtysh.h"
28 #include "vtysh/vtysh_user.h"
29
30 DEFINE_MGROUP(MVTYSH, "vtysh")
31 DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CONFIG, "Vtysh configuration")
32 DEFINE_MTYPE_STATIC(MVTYSH, VTYSH_CONFIG_LINE, "Vtysh configuration line")
33
34 vector configvec;
35
36 struct config {
37 /* Configuration node name. */
38 char *name;
39
40 /* Configuration string line. */
41 struct list *line;
42
43 /* Configuration can be nest. */
44 struct config *config;
45
46 /* Index of this config. */
47 uint32_t index;
48 };
49
50 struct list *config_top;
51
52 static int line_cmp(char *c1, char *c2)
53 {
54 return strcmp(c1, c2);
55 }
56
57 static void line_del(char *line)
58 {
59 XFREE(MTYPE_VTYSH_CONFIG_LINE, line);
60 }
61
62 static struct config *config_new(void)
63 {
64 struct config *config;
65 config = XCALLOC(MTYPE_VTYSH_CONFIG, sizeof(struct config));
66 return config;
67 }
68
69 static int config_cmp(struct config *c1, struct config *c2)
70 {
71 return strcmp(c1->name, c2->name);
72 }
73
74 static void config_del(struct config *config)
75 {
76 list_delete(&config->line);
77 if (config->name)
78 XFREE(MTYPE_VTYSH_CONFIG_LINE, config->name);
79 XFREE(MTYPE_VTYSH_CONFIG, config);
80 }
81
82 static struct config *config_get(int index, const char *line)
83 {
84 struct config *config;
85 struct config *config_loop;
86 struct list *master;
87 struct listnode *node, *nnode;
88
89 config = config_loop = NULL;
90
91 master = vector_lookup_ensure(configvec, index);
92
93 if (!master) {
94 master = list_new();
95 master->del = (void (*)(void *))config_del;
96 master->cmp = (int (*)(void *, void *))config_cmp;
97 vector_set_index(configvec, index, master);
98 }
99
100 for (ALL_LIST_ELEMENTS(master, node, nnode, config_loop)) {
101 if (strcmp(config_loop->name, line) == 0)
102 config = config_loop;
103 }
104
105 if (!config) {
106 config = config_new();
107 config->line = list_new();
108 config->line->del = (void (*)(void *))line_del;
109 config->line->cmp = (int (*)(void *, void *))line_cmp;
110 config->name = XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line);
111 config->index = index;
112 listnode_add(master, config);
113 }
114 return config;
115 }
116
117 void config_add_line(struct list *config, const char *line)
118 {
119 listnode_add(config, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line));
120 }
121
122 static void config_add_line_uniq(struct list *config, const char *line)
123 {
124 struct listnode *node, *nnode;
125 char *pnt;
126
127 for (ALL_LIST_ELEMENTS(config, node, nnode, pnt)) {
128 if (strcmp(pnt, line) == 0)
129 return;
130 }
131 listnode_add_sort(config, XSTRDUP(MTYPE_VTYSH_CONFIG_LINE, line));
132 }
133
134 /*
135 * Add a line that should only be shown once, and always show at the end of the
136 * config block.
137 *
138 * If the line already exists, it will be moved to the end of the block. If it
139 * does not exist, it will be added at the end of the block.
140 *
141 * Note that this only makes sense when there is just one such line that should
142 * show up at the very end of a config block. Furthermore, if the same block
143 * can show up from multiple daemons, all of them must make sure to print the
144 * line at the end of their config, otherwise the line will show at the end of
145 * the config for the last daemon that printed it.
146 *
147 * Here is a motivating example with the 'exit-vrf' command. Suppose we receive
148 * a config from Zebra like so:
149 *
150 * vrf BLUE
151 * ip route A
152 * ip route B
153 * exit-vrf
154 *
155 * Then suppose we later receive this config from PIM:
156 *
157 * vrf BLUE
158 * ip msdp mesh-group MyGroup member 1.2.3.4
159 * exit-vrf
160 *
161 * Then we will combine them into one config block like so:
162 *
163 * vrf BLUE
164 * ip route A
165 * ip route B
166 * ip msdp mesh-group MyGroup member 1.2.3.4
167 * exit-vrf
168 *
169 * Because PIM also sent us an 'exit-vrf', we noticed that we already had one
170 * under the 'vrf BLUE' config block and so we moved it to the end of the
171 * config block again. If PIM had neglected to send us 'exit-vrf', the result
172 * would be this:
173 *
174 * vrf BLUE
175 * ip route A
176 * ip route B
177 * exit-vrf
178 * ip msdp mesh-group MyGroup member 1.2.3.4
179 *
180 * Therefore, daemons that share config blocks must take care to consistently
181 * print the same block terminators.
182 *
183 * Ideally this would be solved by adding a string to struct config that is
184 * always printed at the end when dumping a config. However, this would only
185 * work when the user is using integrated config. In the non-integrated config
186 * case, daemons are responsible for writing their own config files, and so the
187 * must be able to print these blocks correctly independently of vtysh, which
188 * means they are the ones that need to handle printing the block terminators
189 * and VTYSH needs to be smart enough to combine them properly.
190 *
191 * ---
192 *
193 * config
194 * The config to add the line to
195 *
196 * line
197 * The line to add to the end of the config
198 */
199 static void config_add_line_uniq_end(struct list *config, const char *line)
200 {
201 struct listnode *node;
202 char *pnt;
203
204 for (ALL_LIST_ELEMENTS_RO(config, node, pnt)) {
205 if (strcmp(pnt, line) == 0)
206 break;
207 }
208
209 if (!node)
210 config_add_line(config, line);
211 else
212 listnode_move_to_tail(config, node);
213 }
214
215 void vtysh_config_parse_line(void *arg, const char *line)
216 {
217 char c;
218 static struct config *config = NULL;
219
220 if (!line)
221 return;
222
223 c = line[0];
224
225 if (c == '\0')
226 return;
227
228 switch (c) {
229 /* Suppress exclamation points ! and commented lines. The !s are
230 * generated
231 * dynamically in vtysh_config_dump() */
232 case '!':
233 case '#':
234 break;
235 case ' ':
236 /* Store line to current configuration. */
237 if (config) {
238 if (strncmp(line, " link-params",
239 strlen(" link-params"))
240 == 0) {
241 config_add_line(config->line, line);
242 config->index = LINK_PARAMS_NODE;
243 } else if (strncmp(line, " ip multicast boundary",
244 strlen(" ip multicast boundary"))
245 == 0) {
246 config_add_line_uniq_end(config->line, line);
247 } else if (strncmp(line, " ip igmp query-interval",
248 strlen(" ip igmp query-interval")) == 0) {
249 config_add_line_uniq_end(config->line, line);
250 } else if (config->index == LINK_PARAMS_NODE
251 && strncmp(line, " exit-link-params",
252 strlen(" exit"))
253 == 0) {
254 config_add_line(config->line, line);
255 config->index = INTERFACE_NODE;
256 } else if (config->index == VRF_NODE
257 && strncmp(line, " exit-vrf",
258 strlen(" exit-vrf"))
259 == 0) {
260 config_add_line_uniq_end(config->line, line);
261 } else if (config->index == RMAP_NODE
262 || config->index == INTERFACE_NODE
263 || config->index == LOGICALROUTER_NODE
264 || config->index == VTY_NODE
265 || config->index == VRF_NODE)
266 config_add_line_uniq(config->line, line);
267 else
268 config_add_line(config->line, line);
269 } else
270 config_add_line(config_top, line);
271 break;
272 default:
273 if (strncmp(line, "interface", strlen("interface")) == 0)
274 config = config_get(INTERFACE_NODE, line);
275 else if (strncmp(line, "pseudowire", strlen("pseudowire")) == 0)
276 config = config_get(PW_NODE, line);
277 else if (strncmp(line, "logical-router", strlen("logical-router")) == 0)
278 config = config_get(LOGICALROUTER_NODE, line);
279 else if (strncmp(line, "vrf", strlen("vrf")) == 0)
280 config = config_get(VRF_NODE, line);
281 else if (strncmp(line, "nexthop-group", strlen("nexthop-group"))
282 == 0)
283 config = config_get(NH_GROUP_NODE, line);
284 else if (strncmp(line, "router-id", strlen("router-id")) == 0)
285 config = config_get(ZEBRA_NODE, line);
286 else if (strncmp(line, "router rip", strlen("router rip")) == 0)
287 config = config_get(RIP_NODE, line);
288 else if (strncmp(line, "router ripng", strlen("router ripng"))
289 == 0)
290 config = config_get(RIPNG_NODE, line);
291 else if (strncmp(line, "router eigrp", strlen("router eigrp"))
292 == 0)
293 config = config_get(EIGRP_NODE, line);
294 else if (strncmp(line, "router babel", strlen("router babel"))
295 == 0)
296 config = config_get(BABEL_NODE, line);
297 else if (strncmp(line, "router ospf", strlen("router ospf"))
298 == 0)
299 config = config_get(OSPF_NODE, line);
300 else if (strncmp(line, "router ospf6", strlen("router ospf6"))
301 == 0)
302 config = config_get(OSPF6_NODE, line);
303 else if (strncmp(line, "mpls ldp", strlen("mpls ldp")) == 0)
304 config = config_get(LDP_NODE, line);
305 else if (strncmp(line, "l2vpn", strlen("l2vpn")) == 0)
306 config = config_get(LDP_L2VPN_NODE, line);
307 else if (strncmp(line, "router bgp", strlen("router bgp")) == 0)
308 config = config_get(BGP_NODE, line);
309 else if (strncmp(line, "router isis", strlen("router isis"))
310 == 0)
311 config = config_get(ISIS_NODE, line);
312 else if (strncmp(line, "router openfabric", strlen("router openfabric"))
313 == 0)
314 config = config_get(OPENFABRIC_NODE, line);
315 else if (strncmp(line, "route-map", strlen("route-map")) == 0)
316 config = config_get(RMAP_NODE, line);
317 else if (strncmp(line, "pbr-map", strlen("pbr-map")) == 0)
318 config = config_get(PBRMAP_NODE, line);
319 else if (strncmp(line, "access-list", strlen("access-list"))
320 == 0)
321 config = config_get(ACCESS_NODE, line);
322 else if (strncmp(line, "ipv6 access-list",
323 strlen("ipv6 access-list"))
324 == 0)
325 config = config_get(ACCESS_IPV6_NODE, line);
326 else if (strncmp(line, "mac access-list",
327 strlen("mac access-list"))
328 == 0)
329 config = config_get(ACCESS_MAC_NODE, line);
330 else if (strncmp(line, "ip prefix-list",
331 strlen("ip prefix-list"))
332 == 0)
333 config = config_get(PREFIX_NODE, line);
334 else if (strncmp(line, "ipv6 prefix-list",
335 strlen("ipv6 prefix-list"))
336 == 0)
337 config = config_get(PREFIX_IPV6_NODE, line);
338 else if (strncmp(line, "bgp as-path access-list",
339 strlen("bgp as-path access-list"))
340 == 0)
341 config = config_get(AS_LIST_NODE, line);
342 else if (strncmp(line, "bgp community-list",
343 strlen("bgp community-list"))
344 == 0
345 || strncmp(line, "bgp extcommunity-list",
346 strlen("bgp extcommunity-list"))
347 == 0
348 || strncmp(line, "bgp large-community-list",
349 strlen("bgp large-community-list"))
350 == 0)
351 config = config_get(COMMUNITY_LIST_NODE, line);
352 else if (strncmp(line, "ip route", strlen("ip route")) == 0)
353 config = config_get(IP_NODE, line);
354 else if (strncmp(line, "ipv6 route", strlen("ipv6 route")) == 0)
355 config = config_get(IP_NODE, line);
356 else if (strncmp(line, "key", strlen("key")) == 0)
357 config = config_get(KEYCHAIN_NODE, line);
358 else if (strncmp(line, "line", strlen("line")) == 0)
359 config = config_get(VTY_NODE, line);
360 else if ((strncmp(line, "ipv6 forwarding",
361 strlen("ipv6 forwarding"))
362 == 0)
363 || (strncmp(line, "ip forwarding",
364 strlen("ip forwarding"))
365 == 0))
366 config = config_get(FORWARDING_NODE, line);
367 else if (strncmp(line, "debug vrf", strlen("debug vrf")) == 0)
368 config = config_get(VRF_DEBUG_NODE, line);
369 else if (strncmp(line, "debug northbound",
370 strlen("debug northbound"))
371 == 0)
372 config = config_get(NORTHBOUND_DEBUG_NODE, line);
373 else if (strncmp(line, "debug", strlen("debug")) == 0)
374 config = config_get(DEBUG_NODE, line);
375 else if (strncmp(line, "password", strlen("password")) == 0
376 || strncmp(line, "enable password",
377 strlen("enable password"))
378 == 0)
379 config = config_get(AAA_NODE, line);
380 else if (strncmp(line, "ip protocol", strlen("ip protocol"))
381 == 0)
382 config = config_get(PROTOCOL_NODE, line);
383 else if (strncmp(line, "ipv6 protocol", strlen("ipv6 protocol"))
384 == 0)
385 config = config_get(PROTOCOL_NODE, line);
386 else if (strncmp(line, "ip nht", strlen("ip nht")) == 0)
387 config = config_get(PROTOCOL_NODE, line);
388 else if (strncmp(line, "ipv6 nht", strlen("ipv6 nht")) == 0)
389 config = config_get(PROTOCOL_NODE, line);
390 else if (strncmp(line, "mpls", strlen("mpls")) == 0)
391 config = config_get(MPLS_NODE, line);
392 else if (strncmp(line, "bfd", strlen("bfd")) == 0)
393 config = config_get(BFD_NODE, line);
394 else {
395 if (strncmp(line, "log", strlen("log")) == 0
396 || strncmp(line, "hostname", strlen("hostname"))
397 == 0
398 || strncmp(line, "frr", strlen("frr")) == 0
399 || strncmp(line, "agentx", strlen("agentx")) == 0
400 || strncmp(line, "no log", strlen("no log")) == 0)
401 config_add_line_uniq(config_top, line);
402 else
403 config_add_line(config_top, line);
404 config = NULL;
405 }
406 break;
407 }
408 }
409
410 /* Macro to check delimiter is needed between each configuration line
411 * or not. */
412 #define NO_DELIMITER(I) \
413 ((I) == ACCESS_NODE || (I) == PREFIX_NODE || (I) == IP_NODE \
414 || (I) == AS_LIST_NODE || (I) == COMMUNITY_LIST_NODE \
415 || (I) == ACCESS_IPV6_NODE || (I) == ACCESS_MAC_NODE \
416 || (I) == PREFIX_IPV6_NODE || (I) == FORWARDING_NODE \
417 || (I) == DEBUG_NODE || (I) == AAA_NODE || (I) == VRF_DEBUG_NODE \
418 || (I) == NORTHBOUND_DEBUG_NODE || (I) == MPLS_NODE)
419
420 /* Display configuration to file pointer. */
421 void vtysh_config_dump(void)
422 {
423 struct listnode *node, *nnode;
424 struct listnode *mnode, *mnnode;
425 struct config *config;
426 struct list *master;
427 char *line;
428 unsigned int i;
429
430 for (ALL_LIST_ELEMENTS(config_top, node, nnode, line))
431 vty_out(vty, "%s\n", line);
432
433 vty_out(vty, "!\n");
434
435 for (i = 0; i < vector_active(configvec); i++)
436 if ((master = vector_slot(configvec, i)) != NULL) {
437 for (ALL_LIST_ELEMENTS(master, node, nnode, config)) {
438 /* Don't print empty sections for interface.
439 * Route maps on the
440 * other hand could have a legitimate empty
441 * section at the end.
442 * VRF is handled in the backend, we could have
443 * "configured" VRFs with static routes which
444 * are not under the VRF node.
445 */
446 if (config->index == INTERFACE_NODE
447 && list_isempty(config->line))
448 continue;
449
450 vty_out(vty, "%s\n", config->name);
451
452 for (ALL_LIST_ELEMENTS(config->line, mnode,
453 mnnode, line))
454 vty_out(vty, "%s\n", line);
455 if (!NO_DELIMITER(i))
456 vty_out(vty, "!\n");
457 }
458 if (NO_DELIMITER(i))
459 vty_out(vty, "!\n");
460 }
461
462 for (i = 0; i < vector_active(configvec); i++)
463 if ((master = vector_slot(configvec, i)) != NULL) {
464 list_delete(&master);
465 vector_slot(configvec, i) = NULL;
466 }
467 list_delete_all_node(config_top);
468 }
469
470 /* Read up configuration file from file_name. */
471 static int vtysh_read_file(FILE *confp)
472 {
473 struct vty *vty;
474 int ret;
475
476 vty = vty_new();
477 vty->wfd = STDERR_FILENO;
478 vty->type = VTY_TERM;
479 vty->node = CONFIG_NODE;
480
481 vtysh_execute_no_pager("enable");
482 vtysh_execute_no_pager("configure terminal");
483
484 /* Execute configuration file. */
485 ret = vtysh_config_from_file(vty, confp);
486
487 vtysh_execute_no_pager("end");
488 vtysh_execute_no_pager("disable");
489
490 vty_close(vty);
491
492 return (ret);
493 }
494
495 /* Read up configuration file from config_default_dir. */
496 int vtysh_read_config(const char *config_default_dir)
497 {
498 FILE *confp = NULL;
499 int ret;
500
501 confp = fopen(config_default_dir, "r");
502 if (confp == NULL) {
503 fprintf(stderr,
504 "%% Can't open configuration file %s due to '%s'.\n",
505 config_default_dir, safe_strerror(errno));
506 return (CMD_ERR_NO_FILE);
507 }
508
509 ret = vtysh_read_file(confp);
510 fclose(confp);
511
512 return (ret);
513 }
514
515 /* We don't write vtysh specific into file from vtysh. vtysh.conf should
516 * be edited by hand. So, we handle only "write terminal" case here and
517 * integrate vtysh specific conf with conf from daemons.
518 */
519 void vtysh_config_write()
520 {
521 char line[81];
522
523 if (cmd_hostname_get()) {
524 sprintf(line, "hostname %s", cmd_hostname_get());
525 vtysh_config_parse_line(NULL, line);
526 }
527
528 if (cmd_domainname_get()) {
529 sprintf(line, "domainname %s", cmd_domainname_get());
530 vtysh_config_parse_line(NULL, line);
531 }
532 if (vtysh_write_integrated == WRITE_INTEGRATED_NO)
533 vtysh_config_parse_line(NULL,
534 "no service integrated-vtysh-config");
535 if (vtysh_write_integrated == WRITE_INTEGRATED_YES)
536 vtysh_config_parse_line(NULL,
537 "service integrated-vtysh-config");
538
539 user_config_write();
540 }
541
542 void vtysh_config_init()
543 {
544 config_top = list_new();
545 config_top->del = (void (*)(void *))line_del;
546 configvec = vector_init(1);
547 }