]> git.ipfire.org Git - thirdparty/bird.git/blame - conf/conf.c
NEWS and version update.
[thirdparty/bird.git] / conf / conf.c
CommitLineData
31b3e1bb
MM
1/*
2 * BIRD Internet Routing Daemon -- Configuration File Handling
3 *
50fe90ed 4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
31b3e1bb
MM
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
06607335
MM
9/**
10 * DOC: Configuration manager
11 *
725270cb 12 * Configuration of BIRD is complex, yet straightforward. There are three
06607335 13 * modules taking care of the configuration: config manager (which takes care
58f7d004 14 * of storage of the config information and controls switching between configs),
2e9b2421 15 * lexical analyzer and parser.
06607335
MM
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 *
725270cb 21 * There can exist up to four different configurations at one time: an active
06607335
MM
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 it's non-%NULL 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).
27 *
28 * Loading of new configuration is very simple: just call config_alloc()
29 * to get a new &config structure, then use config_parse() to parse a
58f7d004 30 * configuration file and fill all fields of the structure
06607335
MM
31 * and finally ask the config manager to switch to the new
32 * config by calling config_commit().
33 *
34 * CLI commands are parsed in a very similar way -- there is also a stripped-down
2e9b2421 35 * &config structure associated with them and they are lex-ed and parsed by the
06607335
MM
36 * same functions, only a special fake token is prepended before the command
37 * text to make the parser recognize only the rules corresponding to CLI commands.
38 */
39
31b3e1bb
MM
40#include <setjmp.h>
41#include <stdarg.h>
42
6b9fa320 43#undef LOCAL_DEBUG
50fe90ed 44
31b3e1bb 45#include "nest/bird.h"
0e02abfd 46#include "nest/route.h"
31b3e1bb
MM
47#include "nest/protocol.h"
48#include "nest/iface.h"
49#include "lib/resource.h"
50#include "lib/string.h"
50fe90ed 51#include "lib/event.h"
43270902 52#include "lib/timer.h"
31b3e1bb
MM
53#include "conf/conf.h"
54#include "filter/filter.h"
55
56static jmp_buf conf_jmpbuf;
57
50fe90ed
MM
58struct config *config, *new_config, *old_config, *future_config;
59static event *config_event;
76b53a4e 60int shutting_down, future_type;
43270902 61bird_clock_t boot_time;
31b3e1bb 62
06607335
MM
63/**
64 * config_alloc - allocate a new configuration
65 * @name: name of the config
66 *
67 * This function creates new &config structure, attaches a resource
68 * pool and a linear memory pool to it and makes it available for
69 * further use. Returns a pointer to the structure.
70 */
31b3e1bb
MM
71struct config *
72config_alloc(byte *name)
73{
74 pool *p = rp_new(&root_pool, "Config");
d4ff7482 75 linpool *l = lp_new(p, 4080);
31b3e1bb
MM
76 struct config *c = lp_allocz(l, sizeof(struct config));
77
cf31112f 78 c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */
31b3e1bb
MM
79 c->pool = p;
80 cfg_mem = c->mem = l;
31b3e1bb 81 c->file_name = cfg_strdup(name);
43270902 82 c->load_time = now;
c37e7851
OZ
83 c->tf_base.fmt1 = c->tf_log.fmt1 = "%d-%m-%Y %T";
84
43270902
MM
85 if (!boot_time)
86 boot_time = now;
31b3e1bb
MM
87 return c;
88}
89
06607335
MM
90/**
91 * config_parse - parse a configuration
92 * @c: configuration
93 *
94 * config_parse() reads input by calling a hook function pointed to
95 * by @cf_read_hook and parses it according to the configuration
96 * grammar. It also calls all the preconfig and postconfig hooks
2e9b2421 97 * before, resp. after parsing.
06607335
MM
98 *
99 * Result: 1 if the config has been parsed successfully, 0 if any
2e9b2421 100 * error has occurred (such as anybody calling cf_error()) and
06607335
MM
101 * the @err_msg field has been set to the error message.
102 */
31b3e1bb
MM
103int
104config_parse(struct config *c)
105{
f30b86f9 106 DBG("Parsing configuration file `%s'\n", c->file_name);
31b3e1bb 107 new_config = c;
31b3e1bb
MM
108 cfg_mem = c->mem;
109 if (setjmp(conf_jmpbuf))
110 return 0;
bc2fb680 111 cf_lex_init(0);
7c0cc76e 112 sysdep_preconfig(c);
31b3e1bb 113 protos_preconfig(c);
0e02abfd 114 rt_preconfig(c);
31b3e1bb 115 cf_parse();
31b3e1bb 116 protos_postconfig(c);
97e46d28
OZ
117 if (EMPTY_LIST(c->protos))
118 cf_error("No protocol is specified in the config file");
dce26783
MM
119#ifdef IPV6
120 if (!c->router_id)
121 cf_error("Router ID must be configured manually on IPv6 routers");
122#endif
31b3e1bb
MM
123 return 1;
124}
125
06607335
MM
126/**
127 * cli_parse - parse a CLI command
128 * @c: temporary config structure
129 *
130 * cli_parse() is similar to config_parse(), but instead of a configuration,
131 * it parses a CLI command. See the CLI module for more information.
132 */
bc2fb680
MM
133int
134cli_parse(struct config *c)
135{
136 new_config = c;
c9aae7f4 137 c->sym_fallback = config->sym_hash;
bc2fb680
MM
138 cfg_mem = c->mem;
139 if (setjmp(conf_jmpbuf))
140 return 0;
141 cf_lex_init(1);
142 cf_parse();
143 return 1;
144}
145
06607335
MM
146/**
147 * config_free - free a configuration
148 * @c: configuration to be freed
149 *
150 * This function takes a &config structure and frees all resources
151 * associated with it.
152 */
31b3e1bb
MM
153void
154config_free(struct config *c)
155{
156 rfree(c->pool);
157}
158
159void
50fe90ed
MM
160config_add_obstacle(struct config *c)
161{
162 DBG("+++ adding obstacle %d\n", c->obstacle_count);
163 c->obstacle_count++;
164}
165
166void
167config_del_obstacle(struct config *c)
31b3e1bb 168{
50fe90ed
MM
169 DBG("+++ deleting obstacle %d\n", c->obstacle_count);
170 c->obstacle_count--;
171 if (!c->obstacle_count)
172 {
173 ASSERT(config_event);
174 ev_schedule(config_event);
175 }
176}
177
178static int
bf8558bc 179global_commit(struct config *new, struct config *old)
50fe90ed
MM
180{
181 if (!old)
182 return 0;
789772ed 183
d72cdff4
OZ
184 if (!ipa_equal(old->listen_bgp_addr, new->listen_bgp_addr) ||
185 (old->listen_bgp_port != new->listen_bgp_port) ||
186 (old->listen_bgp_flags != new->listen_bgp_flags))
789772ed
OZ
187 log(L_WARN "Reconfiguration of BGP listening socket not implemented, please restart BIRD.");
188
bf8558bc
MM
189 if (!new->router_id)
190 new->router_id = old->router_id;
191 if (new->router_id != old->router_id)
50fe90ed
MM
192 return 1;
193 return 0;
194}
195
196static int
bf1aec97 197config_do_commit(struct config *c, int type)
50fe90ed
MM
198{
199 int force_restart, nobs;
200
201 DBG("do_commit\n");
202 old_config = config;
bf8558bc 203 config = new_config = c;
50fe90ed
MM
204 if (old_config)
205 old_config->obstacle_count++;
76b53a4e 206
50fe90ed
MM
207 DBG("sysdep_commit\n");
208 force_restart = sysdep_commit(c, old_config);
209 DBG("global_commit\n");
210 force_restart |= global_commit(c, old_config);
211 DBG("rt_commit\n");
212 rt_commit(c, old_config);
213 DBG("protos_commit\n");
bf1aec97 214 protos_commit(c, old_config, force_restart, type);
50fe90ed
MM
215 new_config = NULL; /* Just to be sure nobody uses that now */
216 if (old_config)
217 nobs = --old_config->obstacle_count;
218 else
219 nobs = 0;
220 DBG("do_commit finished with %d obstacles remaining\n", nobs);
221 return !nobs;
222}
223
8f6accb5 224static void
7c103b1e 225config_done(void *unused UNUSED)
50fe90ed
MM
226{
227 struct config *c;
228
229 DBG("config_done\n");
230 for(;;)
231 {
bf8558bc
MM
232 if (config->shutdown)
233 sysdep_shutdown_done();
50fe90ed
MM
234 log(L_INFO "Reconfigured");
235 if (old_config)
236 {
237 config_free(old_config);
238 old_config = NULL;
239 }
240 if (!future_config)
241 break;
242 c = future_config;
243 future_config = NULL;
76b53a4e
OZ
244 log(L_INFO "Reconfiguring to queued configuration");
245 if (!config_do_commit(c, future_type))
50fe90ed
MM
246 break;
247 }
50fe90ed
MM
248}
249
06607335
MM
250/**
251 * config_commit - commit a configuration
252 * @c: new configuration
bf1aec97 253 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
06607335
MM
254 *
255 * When a configuration is parsed and prepared for use, the
256 * config_commit() function starts the process of reconfiguration.
257 * It checks whether there is already a reconfiguration in progress
258 * in which case it just queues the new config for later processing.
259 * Else it notifies all modules about the new configuration by calling
260 * their commit() functions which can either accept it immediately
261 * or call config_add_obstacle() to report that they need some time
262 * to complete the reconfiguration. After all such obstacles are removed
263 * using config_del_obstacle(), the old configuration is freed and
264 * everything runs according to the new one.
265 *
266 * Result: %CONF_DONE if the configuration has been accepted immediately,
267 * %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
268 * if it's been queued due to another reconfiguration being in progress now
269 * or %CONF_SHUTDOWN if BIRD is in shutdown mode and no new configurations
270 * are accepted.
271 */
50fe90ed 272int
bf1aec97 273config_commit(struct config *c, int type)
50fe90ed
MM
274{
275 if (!config) /* First-time configuration */
276 {
bf1aec97 277 config_do_commit(c, RECONFIG_HARD);
50fe90ed
MM
278 return CONF_DONE;
279 }
280 if (old_config) /* Reconfiguration already in progress */
281 {
1567edea 282 if (shutting_down == 2)
bf8558bc
MM
283 {
284 log(L_INFO "New configuration discarded due to shutdown");
285 config_free(c);
286 return CONF_SHUTDOWN;
287 }
50fe90ed
MM
288 if (future_config)
289 {
290 log(L_INFO "Queueing new configuration, ignoring the one already queued");
291 config_free(future_config);
292 }
293 else
294 log(L_INFO "Queued new configuration");
295 future_config = c;
76b53a4e 296 future_type = type;
50fe90ed
MM
297 return CONF_QUEUED;
298 }
76b53a4e
OZ
299
300 if (!shutting_down)
301 log(L_INFO "Reconfiguring");
302
bf1aec97 303 if (config_do_commit(c, type))
50fe90ed
MM
304 {
305 config_done(NULL);
306 return CONF_DONE;
307 }
308 if (!config_event)
309 {
310 config_event = ev_new(&root_pool);
311 config_event->hook = config_done;
312 }
313 return CONF_PROGRESS;
31b3e1bb
MM
314}
315
06607335
MM
316/**
317 * order_shutdown - order BIRD shutdown
318 *
319 * This function initiates shutdown of BIRD. It's accomplished by asking
320 * for switching to an empty configuration.
321 */
bf8558bc
MM
322void
323order_shutdown(void)
324{
325 struct config *c;
326
327 if (shutting_down)
328 return;
329 log(L_INFO "Shutting down");
330 c = lp_alloc(config->mem, sizeof(struct config));
331 memcpy(c, config, sizeof(struct config));
332 init_list(&c->protos);
333 init_list(&c->tables);
334 c->shutdown = 1;
bf8558bc 335 shutting_down = 1;
bf1aec97 336 config_commit(c, RECONFIG_HARD);
1567edea 337 shutting_down = 2;
bf8558bc
MM
338}
339
06607335
MM
340/**
341 * cf_error - report a configuration error
342 * @msg: printf-like format string
343 *
344 * cf_error() can be called during execution of config_parse(), that is
345 * from the parser, a preconfig hook or a postconfig hook, to report an
346 * error in the configuration.
347 */
31b3e1bb
MM
348void
349cf_error(char *msg, ...)
350{
351 char buf[1024];
352 va_list args;
353
354 va_start(args, msg);
355 if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
356 strcpy(buf, "<bug: error message too long>");
357 new_config->err_msg = cfg_strdup(buf);
358 new_config->err_lino = conf_lino;
359 longjmp(conf_jmpbuf, 1);
360}
361
06607335
MM
362/**
363 * cfg_strdup - copy a string to config memory
364 * @c: string to copy
365 *
366 * cfg_strdup() creates a new copy of the string in the memory
367 * pool associated with the configuration being currently parsed.
368 * It's often used when a string literal occurs in the configuration
369 * and we want to preserve it for further use.
370 */
31b3e1bb
MM
371char *
372cfg_strdup(char *c)
373{
374 int l = strlen(c) + 1;
375 char *z = cfg_allocu(l);
376 memcpy(z, c, l);
377 return z;
378}