]> git.ipfire.org Git - thirdparty/bird.git/blob - conf/conf.c
Merge branch 'master' into add-path
[thirdparty/bird.git] / conf / conf.c
1 /*
2 * BIRD Internet Routing Daemon -- Configuration File Handling
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 /**
10 * DOC: Configuration manager
11 *
12 * Configuration of BIRD is complex, yet straightforward. There are three
13 * modules taking care of the configuration: config manager (which takes care
14 * of storage of the config information and controls switching between configs),
15 * lexical analyzer and parser.
16 *
17 * The configuration manager stores each config as a &config structure
18 * accompanied by a linear pool from which all information associated
19 * with the config and pointed to by the &config structure is allocated.
20 *
21 * There can exist up to four different configurations at one time: an active
22 * one (pointed to by @config), configuration we are just switching from
23 * (@old_config), one queued for the next reconfiguration (@future_config;
24 * if there is one and the user wants to reconfigure once again, we just
25 * free the previous queued config and replace it with the new one) and
26 * finally a config being parsed (@new_config). The stored @old_config
27 * is also used for undo reconfiguration, which works in a similar way.
28 * Reconfiguration could also have timeout (using @config_timer) and undo
29 * is automatically called if the new configuration is not confirmed later.
30 *
31 * Loading of new configuration is very simple: just call config_alloc()
32 * to get a new &config structure, then use config_parse() to parse a
33 * configuration file and fill all fields of the structure
34 * and finally ask the config manager to switch to the new
35 * config by calling config_commit().
36 *
37 * CLI commands are parsed in a very similar way -- there is also a stripped-down
38 * &config structure associated with them and they are lex-ed and parsed by the
39 * same functions, only a special fake token is prepended before the command
40 * text to make the parser recognize only the rules corresponding to CLI commands.
41 */
42
43 #include <setjmp.h>
44 #include <stdarg.h>
45
46 #undef LOCAL_DEBUG
47
48 #include "nest/bird.h"
49 #include "nest/route.h"
50 #include "nest/protocol.h"
51 #include "nest/iface.h"
52 #include "lib/resource.h"
53 #include "lib/string.h"
54 #include "lib/event.h"
55 #include "lib/timer.h"
56 #include "conf/conf.h"
57 #include "filter/filter.h"
58
59 static jmp_buf conf_jmpbuf;
60
61 struct config *config, *new_config;
62
63 static struct config *old_config; /* Old configuration */
64 static struct config *future_config; /* New config held here if recon requested during recon */
65 static int old_cftype; /* Type of transition old_config -> config (RECONFIG_SOFT/HARD) */
66 static int future_cftype; /* Type of scheduled transition, may also be RECONFIG_UNDO */
67 /* Note that when future_cftype is RECONFIG_UNDO, then future_config is NULL,
68 therefore proper check for future scheduled config checks future_cftype */
69
70 static event *config_event; /* Event for finalizing reconfiguration */
71 static timer *config_timer; /* Timer for scheduled configuration rollback */
72
73 /* These are public just for cmd_show_status(), should not be accessed elsewhere */
74 int shutting_down; /* Shutdown requested, do not accept new config changes */
75 int configuring; /* Reconfiguration is running */
76 int undo_available; /* Undo was not requested from last reconfiguration */
77 /* Note that both shutting_down and undo_available are related to requests, not processing */
78
79 /**
80 * config_alloc - allocate a new configuration
81 * @name: name of the config
82 *
83 * This function creates new &config structure, attaches a resource
84 * pool and a linear memory pool to it and makes it available for
85 * further use. Returns a pointer to the structure.
86 */
87 struct config *
88 config_alloc(byte *name)
89 {
90 pool *p = rp_new(&root_pool, "Config");
91 linpool *l = lp_new(p, 4080);
92 struct config *c = lp_allocz(l, sizeof(struct config));
93
94 c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */
95 c->pool = p;
96 cfg_mem = c->mem = l;
97 c->file_name = cfg_strdup(name);
98 c->load_time = now;
99 c->tf_route = c->tf_proto = (struct timeformat){"%T", "%F", 20*3600};
100 c->tf_base = c->tf_log = (struct timeformat){"%F %T", NULL, 0};
101
102 return c;
103 }
104
105 /**
106 * config_parse - parse a configuration
107 * @c: configuration
108 *
109 * config_parse() reads input by calling a hook function pointed to
110 * by @cf_read_hook and parses it according to the configuration
111 * grammar. It also calls all the preconfig and postconfig hooks
112 * before, resp. after parsing.
113 *
114 * Result: 1 if the config has been parsed successfully, 0 if any
115 * error has occurred (such as anybody calling cf_error()) and
116 * the @err_msg field has been set to the error message.
117 */
118 int
119 config_parse(struct config *c)
120 {
121 DBG("Parsing configuration file `%s'\n", c->file_name);
122 new_config = c;
123 cfg_mem = c->mem;
124 if (setjmp(conf_jmpbuf))
125 return 0;
126 cf_lex_init(0, c);
127 sysdep_preconfig(c);
128 protos_preconfig(c);
129 rt_preconfig(c);
130 roa_preconfig(c);
131 cf_parse();
132 protos_postconfig(c);
133 if (EMPTY_LIST(c->protos))
134 cf_error("No protocol is specified in the config file");
135 #ifdef IPV6
136 if (!c->router_id)
137 cf_error("Router ID must be configured manually on IPv6 routers");
138 #endif
139 return 1;
140 }
141
142 /**
143 * cli_parse - parse a CLI command
144 * @c: temporary config structure
145 *
146 * cli_parse() is similar to config_parse(), but instead of a configuration,
147 * it parses a CLI command. See the CLI module for more information.
148 */
149 int
150 cli_parse(struct config *c)
151 {
152 new_config = c;
153 c->sym_fallback = config->sym_hash;
154 cfg_mem = c->mem;
155 if (setjmp(conf_jmpbuf))
156 return 0;
157 cf_lex_init(1, c);
158 cf_parse();
159 return 1;
160 }
161
162 /**
163 * config_free - free a configuration
164 * @c: configuration to be freed
165 *
166 * This function takes a &config structure and frees all resources
167 * associated with it.
168 */
169 void
170 config_free(struct config *c)
171 {
172 if (c)
173 rfree(c->pool);
174 }
175
176 void
177 config_add_obstacle(struct config *c)
178 {
179 DBG("+++ adding obstacle %d\n", c->obstacle_count);
180 c->obstacle_count++;
181 }
182
183 void
184 config_del_obstacle(struct config *c)
185 {
186 DBG("+++ deleting obstacle %d\n", c->obstacle_count);
187 c->obstacle_count--;
188 if (!c->obstacle_count)
189 ev_schedule(config_event);
190 }
191
192 static int
193 global_commit(struct config *new, struct config *old)
194 {
195 if (!old)
196 return 0;
197
198 if (!ipa_equal(old->listen_bgp_addr, new->listen_bgp_addr) ||
199 (old->listen_bgp_port != new->listen_bgp_port) ||
200 (old->listen_bgp_flags != new->listen_bgp_flags))
201 log(L_WARN "Reconfiguration of BGP listening socket not implemented, please restart BIRD.");
202
203 if (!new->router_id)
204 {
205 new->router_id = old->router_id;
206
207 if (new->router_id_from)
208 {
209 u32 id = if_choose_router_id(new->router_id_from, old->router_id);
210 if (!id)
211 log(L_WARN "Cannot determine router ID, using old one");
212 else
213 new->router_id = id;
214 }
215 }
216
217 return 0;
218 }
219
220 static int
221 config_do_commit(struct config *c, int type)
222 {
223 if (type == RECONFIG_UNDO)
224 {
225 c = old_config;
226 type = old_cftype;
227 }
228 else
229 config_free(old_config);
230
231 old_config = config;
232 old_cftype = type;
233 config = c;
234
235 configuring = 1;
236 if (old_config && !config->shutdown)
237 log(L_INFO "Reconfiguring");
238
239 /* This should not be necessary, but it seems there are some
240 functions that access new_config instead of config */
241 new_config = config;
242
243 if (old_config)
244 old_config->obstacle_count++;
245
246 DBG("sysdep_commit\n");
247 int force_restart = sysdep_commit(c, old_config);
248 DBG("global_commit\n");
249 force_restart |= global_commit(c, old_config);
250 DBG("rt_commit\n");
251 rt_commit(c, old_config);
252 roa_commit(c, old_config);
253 DBG("protos_commit\n");
254 protos_commit(c, old_config, force_restart, type);
255
256 /* Just to be sure nobody uses that now */
257 new_config = NULL;
258
259 int obs = 0;
260 if (old_config)
261 obs = --old_config->obstacle_count;
262
263 DBG("do_commit finished with %d obstacles remaining\n", obs);
264 return !obs;
265 }
266
267 static void
268 config_done(void *unused UNUSED)
269 {
270 if (config->shutdown)
271 sysdep_shutdown_done();
272
273 configuring = 0;
274 if (old_config)
275 log(L_INFO "Reconfigured");
276
277 if (future_cftype)
278 {
279 int type = future_cftype;
280 struct config *conf = future_config;
281 future_cftype = RECONFIG_NONE;
282 future_config = NULL;
283
284 log(L_INFO "Reconfiguring to queued configuration");
285 if (config_do_commit(conf, type))
286 config_done(NULL);
287 }
288 }
289
290 /**
291 * config_commit - commit a configuration
292 * @c: new configuration
293 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
294 * @timeout: timeout for undo (or 0 for no timeout)
295 *
296 * When a configuration is parsed and prepared for use, the
297 * config_commit() function starts the process of reconfiguration.
298 * It checks whether there is already a reconfiguration in progress
299 * in which case it just queues the new config for later processing.
300 * Else it notifies all modules about the new configuration by calling
301 * their commit() functions which can either accept it immediately
302 * or call config_add_obstacle() to report that they need some time
303 * to complete the reconfiguration. After all such obstacles are removed
304 * using config_del_obstacle(), the old configuration is freed and
305 * everything runs according to the new one.
306 *
307 * When @timeout is nonzero, the undo timer is activated with given
308 * timeout. The timer is deactivated when config_commit(),
309 * config_confirm() or config_undo() is called.
310 *
311 * Result: %CONF_DONE if the configuration has been accepted immediately,
312 * %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
313 * if it's been queued due to another reconfiguration being in progress now
314 * or %CONF_SHUTDOWN if BIRD is in shutdown mode and no new configurations
315 * are accepted.
316 */
317 int
318 config_commit(struct config *c, int type, int timeout)
319 {
320 if (shutting_down)
321 {
322 config_free(c);
323 return CONF_SHUTDOWN;
324 }
325
326 undo_available = 1;
327 if (timeout > 0)
328 tm_start(config_timer, timeout);
329 else
330 tm_stop(config_timer);
331
332 if (configuring)
333 {
334 if (future_cftype)
335 {
336 log(L_INFO "Queueing new configuration, ignoring the one already queued");
337 config_free(future_config);
338 }
339 else
340 log(L_INFO "Queueing new configuration");
341
342 future_cftype = type;
343 future_config = c;
344 return CONF_QUEUED;
345 }
346
347 if (config_do_commit(c, type))
348 {
349 config_done(NULL);
350 return CONF_DONE;
351 }
352 return CONF_PROGRESS;
353 }
354
355 /**
356 * config_confirm - confirm a commited configuration
357 *
358 * When the undo timer is activated by config_commit() with nonzero timeout,
359 * this function can be used to deactivate it and therefore confirm
360 * the current configuration.
361 *
362 * Result: %CONF_CONFIRM when the current configuration is confirmed,
363 * %CONF_NONE when there is nothing to confirm (i.e. undo timer is not active).
364 */
365 int
366 config_confirm(void)
367 {
368 if (config_timer->expires == 0)
369 return CONF_NOTHING;
370
371 tm_stop(config_timer);
372
373 return CONF_CONFIRM;
374 }
375
376 /**
377 * config_undo - undo a configuration
378 *
379 * Function config_undo() can be used to change the current
380 * configuration back to stored %old_config. If no reconfiguration is
381 * running, this stored configuration is commited in the same way as a
382 * new configuration in config_commit(). If there is already a
383 * reconfiguration in progress and no next reconfiguration is
384 * scheduled, then the undo is scheduled for later processing as
385 * usual, but if another reconfiguration is already scheduled, then
386 * such reconfiguration is removed instead (i.e. undo is applied on
387 * the last commit that scheduled it).
388 *
389 * Result: %CONF_DONE if the configuration has been accepted immediately,
390 * %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
391 * if it's been queued due to another reconfiguration being in progress now,
392 * %CONF_UNQUEUED if a scheduled reconfiguration is removed, %CONF_NOTHING
393 * if there is no relevant configuration to undo (the previous config request
394 * was config_undo() too) or %CONF_SHUTDOWN if BIRD is in shutdown mode and
395 * no new configuration changes are accepted.
396 */
397 int
398 config_undo(void)
399 {
400 if (shutting_down)
401 return CONF_SHUTDOWN;
402
403 if (!undo_available || !old_config)
404 return CONF_NOTHING;
405
406 undo_available = 0;
407 tm_stop(config_timer);
408
409 if (configuring)
410 {
411 if (future_cftype)
412 {
413 config_free(future_config);
414 future_config = NULL;
415
416 log(L_INFO "Removing queued configuration");
417 future_cftype = RECONFIG_NONE;
418 return CONF_UNQUEUED;
419 }
420 else
421 {
422 log(L_INFO "Queueing undo configuration");
423 future_cftype = RECONFIG_UNDO;
424 return CONF_QUEUED;
425 }
426 }
427
428 if (config_do_commit(NULL, RECONFIG_UNDO))
429 {
430 config_done(NULL);
431 return CONF_DONE;
432 }
433 return CONF_PROGRESS;
434 }
435
436 extern void cmd_reconfig_undo_notify(void);
437
438 static void
439 config_timeout(struct timer *t)
440 {
441 log(L_INFO "Config timeout expired, starting undo");
442 cmd_reconfig_undo_notify();
443
444 int r = config_undo();
445 if (r < 0)
446 log(L_ERR "Undo request failed");
447 }
448
449 void
450 config_init(void)
451 {
452 config_event = ev_new(&root_pool);
453 config_event->hook = config_done;
454
455 config_timer = tm_new(&root_pool);
456 config_timer->hook = config_timeout;
457 }
458
459 /**
460 * order_shutdown - order BIRD shutdown
461 *
462 * This function initiates shutdown of BIRD. It's accomplished by asking
463 * for switching to an empty configuration.
464 */
465 void
466 order_shutdown(void)
467 {
468 struct config *c;
469
470 if (shutting_down)
471 return;
472
473 log(L_INFO "Shutting down");
474 c = lp_alloc(config->mem, sizeof(struct config));
475 memcpy(c, config, sizeof(struct config));
476 init_list(&c->protos);
477 init_list(&c->tables);
478 c->shutdown = 1;
479
480 config_commit(c, RECONFIG_HARD, 0);
481 shutting_down = 1;
482 }
483
484 /**
485 * cf_error - report a configuration error
486 * @msg: printf-like format string
487 *
488 * cf_error() can be called during execution of config_parse(), that is
489 * from the parser, a preconfig hook or a postconfig hook, to report an
490 * error in the configuration.
491 */
492 void
493 cf_error(char *msg, ...)
494 {
495 char buf[1024];
496 va_list args;
497
498 va_start(args, msg);
499 if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
500 strcpy(buf, "<bug: error message too long>");
501 new_config->err_msg = cfg_strdup(buf);
502 new_config->err_lino = ifs->lino;
503 new_config->err_file_name = ifs->file_name;
504 longjmp(conf_jmpbuf, 1);
505 }
506
507 /**
508 * cfg_strdup - copy a string to config memory
509 * @c: string to copy
510 *
511 * cfg_strdup() creates a new copy of the string in the memory
512 * pool associated with the configuration being currently parsed.
513 * It's often used when a string literal occurs in the configuration
514 * and we want to preserve it for further use.
515 */
516 char *
517 cfg_strdup(char *c)
518 {
519 int l = strlen(c) + 1;
520 char *z = cfg_allocu(l);
521 memcpy(z, c, l);
522 return z;
523 }
524
525
526 void
527 cfg_copy_list(list *dest, list *src, unsigned node_size)
528 {
529 node *dn, *sn;
530
531 init_list(dest);
532 WALK_LIST(sn, *src)
533 {
534 dn = cfg_alloc(node_size);
535 memcpy(dn, sn, node_size);
536 add_tail(dest, dn);
537 }
538 }