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