1999-02-06 05:37:34 +08:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Configuration File Handling
|
|
|
|
*
|
2000-01-17 00:44:50 +08:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1999-02-06 05:37:34 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2000-03-13 05:01:38 +08:00
|
|
|
#undef LOCAL_DEBUG
|
2000-01-17 00:44:50 +08:00
|
|
|
|
1999-02-06 05:37:34 +08:00
|
|
|
#include "nest/bird.h"
|
1999-05-18 04:14:52 +08:00
|
|
|
#include "nest/route.h"
|
1999-02-06 05:37:34 +08:00
|
|
|
#include "nest/protocol.h"
|
|
|
|
#include "nest/iface.h"
|
|
|
|
#include "lib/resource.h"
|
|
|
|
#include "lib/string.h"
|
2000-01-17 00:44:50 +08:00
|
|
|
#include "lib/event.h"
|
2000-03-13 06:44:54 +08:00
|
|
|
#include "lib/timer.h"
|
1999-02-06 05:37:34 +08:00
|
|
|
#include "conf/conf.h"
|
|
|
|
#include "filter/filter.h"
|
|
|
|
|
|
|
|
static jmp_buf conf_jmpbuf;
|
|
|
|
|
2000-01-17 00:44:50 +08:00
|
|
|
struct config *config, *new_config, *old_config, *future_config;
|
|
|
|
static event *config_event;
|
2000-01-17 01:40:26 +08:00
|
|
|
int shutting_down;
|
2000-03-13 06:44:54 +08:00
|
|
|
bird_clock_t boot_time;
|
1999-02-06 05:37:34 +08:00
|
|
|
|
|
|
|
struct config *
|
|
|
|
config_alloc(byte *name)
|
|
|
|
{
|
|
|
|
pool *p = rp_new(&root_pool, "Config");
|
1999-04-06 04:06:02 +08:00
|
|
|
linpool *l = lp_new(p, 4080);
|
1999-02-06 05:37:34 +08:00
|
|
|
struct config *c = lp_allocz(l, sizeof(struct config));
|
|
|
|
|
|
|
|
c->pool = p;
|
|
|
|
cfg_mem = c->mem = l;
|
|
|
|
c->file_name = cfg_strdup(name);
|
2000-03-13 06:44:54 +08:00
|
|
|
c->load_time = now;
|
|
|
|
if (!boot_time)
|
|
|
|
boot_time = now;
|
1999-02-06 05:37:34 +08:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
config_parse(struct config *c)
|
|
|
|
{
|
2000-03-08 05:50:03 +08:00
|
|
|
DBG("Parsing configuration file `%s'\n", c->file_name);
|
1999-02-06 05:37:34 +08:00
|
|
|
new_config = c;
|
|
|
|
cfg_mem = c->mem;
|
|
|
|
if (setjmp(conf_jmpbuf))
|
|
|
|
return 0;
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-11-01 01:47:47 +08:00
|
|
|
cf_lex_init(0);
|
1999-12-06 21:44:45 +08:00
|
|
|
sysdep_preconfig(c);
|
1999-02-06 05:37:34 +08:00
|
|
|
protos_preconfig(c);
|
1999-05-18 04:14:52 +08:00
|
|
|
rt_preconfig(c);
|
1999-02-06 05:37:34 +08:00
|
|
|
cf_parse();
|
|
|
|
filters_postconfig(); /* FIXME: Do we really need this? */
|
|
|
|
protos_postconfig(c);
|
1999-08-04 03:36:06 +08:00
|
|
|
#ifdef IPV6
|
|
|
|
if (!c->router_id)
|
|
|
|
cf_error("Router ID must be configured manually on IPv6 routers");
|
|
|
|
#endif
|
1999-02-06 05:37:34 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-11-01 01:47:47 +08:00
|
|
|
int
|
|
|
|
cli_parse(struct config *c)
|
|
|
|
{
|
|
|
|
new_config = c;
|
1999-11-30 22:03:36 +08:00
|
|
|
c->sym_fallback = config->sym_hash;
|
Parse CLI commands. We use the same parser as for configuration files (because
we want to allow filter and similar complex constructs to be used in commands
and we should avoid code duplication), only with CLI_MARKER token prepended
before the whole input.
Defined macro CF_CLI(cmd, args, help) for defining CLI commands in .Y files.
The first argument specifies the command itself, the remaining two arguments
are copied to the help file (er, will be copied after the help file starts
to exist). This macro automatically creates a skeleton rule for the command,
you only need to append arguments as in:
CF_CLI(STEAL MONEY, <$>, [[Steal <$> US dollars or equivalent in any other currency]]): NUM {
cli_msg(0, "%d$ stolen", $3);
} ;
Also don't forget to reset lexer state between inputs.
1999-11-01 01:47:47 +08:00
|
|
|
cfg_mem = c->mem;
|
|
|
|
if (setjmp(conf_jmpbuf))
|
|
|
|
return 0;
|
|
|
|
cf_lex_init(1);
|
|
|
|
cf_parse();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-02-06 05:37:34 +08:00
|
|
|
void
|
|
|
|
config_free(struct config *c)
|
|
|
|
{
|
|
|
|
rfree(c->pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-01-17 00:44:50 +08:00
|
|
|
config_add_obstacle(struct config *c)
|
|
|
|
{
|
|
|
|
DBG("+++ adding obstacle %d\n", c->obstacle_count);
|
|
|
|
c->obstacle_count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
config_del_obstacle(struct config *c)
|
1999-02-06 05:37:34 +08:00
|
|
|
{
|
2000-01-17 00:44:50 +08:00
|
|
|
DBG("+++ deleting obstacle %d\n", c->obstacle_count);
|
|
|
|
c->obstacle_count--;
|
|
|
|
if (!c->obstacle_count)
|
|
|
|
{
|
|
|
|
ASSERT(config_event);
|
|
|
|
ev_schedule(config_event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2000-01-17 01:40:26 +08:00
|
|
|
global_commit(struct config *new, struct config *old)
|
2000-01-17 00:44:50 +08:00
|
|
|
{
|
|
|
|
if (!old)
|
|
|
|
return 0;
|
2000-01-17 01:40:26 +08:00
|
|
|
if (!new->router_id)
|
|
|
|
new->router_id = old->router_id;
|
|
|
|
if (new->router_id != old->router_id)
|
2000-01-17 00:44:50 +08:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_do_commit(struct config *c)
|
|
|
|
{
|
|
|
|
int force_restart, nobs;
|
|
|
|
|
|
|
|
DBG("do_commit\n");
|
|
|
|
old_config = config;
|
2000-01-17 01:40:26 +08:00
|
|
|
config = new_config = c;
|
2000-01-17 00:44:50 +08:00
|
|
|
if (old_config)
|
|
|
|
old_config->obstacle_count++;
|
|
|
|
DBG("sysdep_commit\n");
|
|
|
|
force_restart = sysdep_commit(c, old_config);
|
|
|
|
DBG("global_commit\n");
|
|
|
|
force_restart |= global_commit(c, old_config);
|
|
|
|
DBG("rt_commit\n");
|
|
|
|
rt_commit(c, old_config);
|
|
|
|
DBG("protos_commit\n");
|
|
|
|
protos_commit(c, old_config, force_restart);
|
|
|
|
new_config = NULL; /* Just to be sure nobody uses that now */
|
|
|
|
if (old_config)
|
|
|
|
nobs = --old_config->obstacle_count;
|
|
|
|
else
|
|
|
|
nobs = 0;
|
|
|
|
DBG("do_commit finished with %d obstacles remaining\n", nobs);
|
|
|
|
return !nobs;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
config_done(void *unused)
|
|
|
|
{
|
|
|
|
struct config *c;
|
|
|
|
|
|
|
|
DBG("config_done\n");
|
|
|
|
for(;;)
|
|
|
|
{
|
2000-01-17 01:40:26 +08:00
|
|
|
if (config->shutdown)
|
|
|
|
sysdep_shutdown_done();
|
2000-01-17 00:44:50 +08:00
|
|
|
log(L_INFO "Reconfigured");
|
|
|
|
if (old_config)
|
|
|
|
{
|
|
|
|
config_free(old_config);
|
|
|
|
old_config = NULL;
|
|
|
|
}
|
|
|
|
if (!future_config)
|
|
|
|
break;
|
|
|
|
c = future_config;
|
|
|
|
future_config = NULL;
|
|
|
|
log(L_INFO "Switching to queued configuration...");
|
|
|
|
if (!config_do_commit(c))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
config_commit(struct config *c)
|
|
|
|
{
|
|
|
|
if (!config) /* First-time configuration */
|
|
|
|
{
|
|
|
|
config_do_commit(c);
|
|
|
|
return CONF_DONE;
|
|
|
|
}
|
|
|
|
if (old_config) /* Reconfiguration already in progress */
|
|
|
|
{
|
2000-01-17 01:40:26 +08:00
|
|
|
if (shutting_down)
|
|
|
|
{
|
|
|
|
log(L_INFO "New configuration discarded due to shutdown");
|
|
|
|
config_free(c);
|
|
|
|
return CONF_SHUTDOWN;
|
|
|
|
}
|
2000-01-17 00:44:50 +08:00
|
|
|
if (future_config)
|
|
|
|
{
|
|
|
|
log(L_INFO "Queueing new configuration, ignoring the one already queued");
|
|
|
|
config_free(future_config);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
log(L_INFO "Queued new configuration");
|
|
|
|
future_config = c;
|
|
|
|
return CONF_QUEUED;
|
|
|
|
}
|
|
|
|
if (config_do_commit(c))
|
|
|
|
{
|
|
|
|
config_done(NULL);
|
|
|
|
return CONF_DONE;
|
|
|
|
}
|
|
|
|
if (!config_event)
|
|
|
|
{
|
|
|
|
config_event = ev_new(&root_pool);
|
|
|
|
config_event->hook = config_done;
|
|
|
|
}
|
|
|
|
return CONF_PROGRESS;
|
1999-02-06 05:37:34 +08:00
|
|
|
}
|
|
|
|
|
2000-01-17 01:40:26 +08:00
|
|
|
void
|
|
|
|
order_shutdown(void)
|
|
|
|
{
|
|
|
|
struct config *c;
|
|
|
|
|
|
|
|
if (shutting_down)
|
|
|
|
return;
|
|
|
|
log(L_INFO "Shutting down");
|
|
|
|
c = lp_alloc(config->mem, sizeof(struct config));
|
|
|
|
memcpy(c, config, sizeof(struct config));
|
|
|
|
init_list(&c->protos);
|
|
|
|
init_list(&c->tables);
|
|
|
|
c->shutdown = 1;
|
|
|
|
config_commit(c);
|
|
|
|
shutting_down = 1;
|
|
|
|
}
|
|
|
|
|
1999-02-06 05:37:34 +08:00
|
|
|
void
|
|
|
|
cf_error(char *msg, ...)
|
|
|
|
{
|
|
|
|
char buf[1024];
|
|
|
|
va_list args;
|
|
|
|
|
|
|
|
va_start(args, msg);
|
|
|
|
if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
|
|
|
|
strcpy(buf, "<bug: error message too long>");
|
|
|
|
new_config->err_msg = cfg_strdup(buf);
|
|
|
|
new_config->err_lino = conf_lino;
|
|
|
|
longjmp(conf_jmpbuf, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
cfg_strdup(char *c)
|
|
|
|
{
|
|
|
|
int l = strlen(c) + 1;
|
|
|
|
char *z = cfg_allocu(l);
|
|
|
|
memcpy(z, c, l);
|
|
|
|
return z;
|
|
|
|
}
|