1999-02-05 21:37:34 +00:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Configuration File Handling
|
|
|
|
*
|
2000-01-16 16:44:50 +00:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1999-02-05 21:37:34 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-06-03 18:23:00 +00:00
|
|
|
/**
|
|
|
|
* DOC: Configuration manager
|
|
|
|
*
|
2000-06-08 12:37:21 +00:00
|
|
|
* Configuration of BIRD is complex, yet straightforward. There are three
|
2000-06-03 18:23:00 +00:00
|
|
|
* modules taking care of the configuration: config manager (which takes care
|
2000-06-07 13:25:53 +00:00
|
|
|
* of storage of the config information and controls switching between configs),
|
2000-06-07 12:29:08 +00:00
|
|
|
* lexical analyzer and parser.
|
2000-06-03 18:23:00 +00:00
|
|
|
*
|
|
|
|
* The configuration manager stores each config as a &config structure
|
|
|
|
* accompanied by a linear pool from which all information associated
|
|
|
|
* with the config and pointed to by the &config structure is allocated.
|
|
|
|
*
|
2000-06-08 12:37:21 +00:00
|
|
|
* There can exist up to four different configurations at one time: an active
|
2000-06-03 18:23:00 +00:00
|
|
|
* one (pointed to by @config), configuration we are just switching from
|
|
|
|
* (@old_config), one queued for the next reconfiguration (@future_config;
|
|
|
|
* if it's non-%NULL and the user wants to reconfigure once again, we just
|
|
|
|
* free the previous queued config and replace it with the new one) and
|
|
|
|
* finally a config being parsed (@new_config).
|
|
|
|
*
|
|
|
|
* Loading of new configuration is very simple: just call config_alloc()
|
|
|
|
* to get a new &config structure, then use config_parse() to parse a
|
2000-06-07 13:25:53 +00:00
|
|
|
* configuration file and fill all fields of the structure
|
2000-06-03 18:23:00 +00:00
|
|
|
* and finally ask the config manager to switch to the new
|
|
|
|
* config by calling config_commit().
|
|
|
|
*
|
|
|
|
* CLI commands are parsed in a very similar way -- there is also a stripped-down
|
2000-06-07 12:29:08 +00:00
|
|
|
* &config structure associated with them and they are lex-ed and parsed by the
|
2000-06-03 18:23:00 +00:00
|
|
|
* same functions, only a special fake token is prepended before the command
|
|
|
|
* text to make the parser recognize only the rules corresponding to CLI commands.
|
|
|
|
*/
|
|
|
|
|
1999-02-05 21:37:34 +00:00
|
|
|
#include <setjmp.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
2000-03-12 21:01:38 +00:00
|
|
|
#undef LOCAL_DEBUG
|
2000-01-16 16:44:50 +00:00
|
|
|
|
1999-02-05 21:37:34 +00:00
|
|
|
#include "nest/bird.h"
|
1999-05-17 20:14:52 +00:00
|
|
|
#include "nest/route.h"
|
1999-02-05 21:37:34 +00:00
|
|
|
#include "nest/protocol.h"
|
|
|
|
#include "nest/iface.h"
|
|
|
|
#include "lib/resource.h"
|
|
|
|
#include "lib/string.h"
|
2000-01-16 16:44:50 +00:00
|
|
|
#include "lib/event.h"
|
2000-03-12 22:44:54 +00:00
|
|
|
#include "lib/timer.h"
|
1999-02-05 21:37:34 +00:00
|
|
|
#include "conf/conf.h"
|
|
|
|
#include "filter/filter.h"
|
|
|
|
|
|
|
|
static jmp_buf conf_jmpbuf;
|
|
|
|
|
2000-01-16 16:44:50 +00:00
|
|
|
struct config *config, *new_config, *old_config, *future_config;
|
|
|
|
static event *config_event;
|
2010-02-06 21:57:51 +00:00
|
|
|
int shutting_down, future_type;
|
2000-03-12 22:44:54 +00:00
|
|
|
bird_clock_t boot_time;
|
1999-02-05 21:37:34 +00:00
|
|
|
|
2000-06-03 18:23:00 +00:00
|
|
|
/**
|
|
|
|
* config_alloc - allocate a new configuration
|
|
|
|
* @name: name of the config
|
|
|
|
*
|
|
|
|
* This function creates new &config structure, attaches a resource
|
|
|
|
* pool and a linear memory pool to it and makes it available for
|
|
|
|
* further use. Returns a pointer to the structure.
|
|
|
|
*/
|
1999-02-05 21:37:34 +00:00
|
|
|
struct config *
|
|
|
|
config_alloc(byte *name)
|
|
|
|
{
|
|
|
|
pool *p = rp_new(&root_pool, "Config");
|
1999-04-05 20:06:02 +00:00
|
|
|
linpool *l = lp_new(p, 4080);
|
1999-02-05 21:37:34 +00:00
|
|
|
struct config *c = lp_allocz(l, sizeof(struct config));
|
|
|
|
|
2010-01-03 11:17:52 +00:00
|
|
|
c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */
|
1999-02-05 21:37:34 +00:00
|
|
|
c->pool = p;
|
|
|
|
cfg_mem = c->mem = l;
|
|
|
|
c->file_name = cfg_strdup(name);
|
2000-03-12 22:44:54 +00:00
|
|
|
c->load_time = now;
|
2010-02-02 23:19:24 +00:00
|
|
|
c->tf_base.fmt1 = c->tf_log.fmt1 = "%d-%m-%Y %T";
|
|
|
|
|
2000-03-12 22:44:54 +00:00
|
|
|
if (!boot_time)
|
|
|
|
boot_time = now;
|
1999-02-05 21:37:34 +00:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2000-06-03 18:23:00 +00:00
|
|
|
/**
|
|
|
|
* config_parse - parse a configuration
|
|
|
|
* @c: configuration
|
|
|
|
*
|
|
|
|
* config_parse() reads input by calling a hook function pointed to
|
|
|
|
* by @cf_read_hook and parses it according to the configuration
|
|
|
|
* grammar. It also calls all the preconfig and postconfig hooks
|
2000-06-07 12:29:08 +00:00
|
|
|
* before, resp. after parsing.
|
2000-06-03 18:23:00 +00:00
|
|
|
*
|
|
|
|
* Result: 1 if the config has been parsed successfully, 0 if any
|
2000-06-07 12:29:08 +00:00
|
|
|
* error has occurred (such as anybody calling cf_error()) and
|
2000-06-03 18:23:00 +00:00
|
|
|
* the @err_msg field has been set to the error message.
|
|
|
|
*/
|
1999-02-05 21:37:34 +00:00
|
|
|
int
|
|
|
|
config_parse(struct config *c)
|
|
|
|
{
|
2000-03-07 21:50:03 +00:00
|
|
|
DBG("Parsing configuration file `%s'\n", c->file_name);
|
1999-02-05 21:37:34 +00: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-10-31 17:47:47 +00:00
|
|
|
cf_lex_init(0);
|
1999-12-06 13:44:45 +00:00
|
|
|
sysdep_preconfig(c);
|
1999-02-05 21:37:34 +00:00
|
|
|
protos_preconfig(c);
|
1999-05-17 20:14:52 +00:00
|
|
|
rt_preconfig(c);
|
1999-02-05 21:37:34 +00:00
|
|
|
cf_parse();
|
|
|
|
protos_postconfig(c);
|
2010-03-17 11:19:22 +00:00
|
|
|
if (EMPTY_LIST(c->protos))
|
|
|
|
cf_error("No protocol is specified in the config file");
|
1999-08-03 19:36:06 +00:00
|
|
|
#ifdef IPV6
|
|
|
|
if (!c->router_id)
|
|
|
|
cf_error("Router ID must be configured manually on IPv6 routers");
|
|
|
|
#endif
|
1999-02-05 21:37:34 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-06-03 18:23:00 +00:00
|
|
|
/**
|
|
|
|
* cli_parse - parse a CLI command
|
|
|
|
* @c: temporary config structure
|
|
|
|
*
|
|
|
|
* cli_parse() is similar to config_parse(), but instead of a configuration,
|
|
|
|
* it parses a CLI command. See the CLI module for more information.
|
|
|
|
*/
|
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-10-31 17:47:47 +00:00
|
|
|
int
|
|
|
|
cli_parse(struct config *c)
|
|
|
|
{
|
|
|
|
new_config = c;
|
1999-11-30 14:03:36 +00: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-10-31 17:47:47 +00:00
|
|
|
cfg_mem = c->mem;
|
|
|
|
if (setjmp(conf_jmpbuf))
|
|
|
|
return 0;
|
|
|
|
cf_lex_init(1);
|
|
|
|
cf_parse();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2000-06-03 18:23:00 +00:00
|
|
|
/**
|
|
|
|
* config_free - free a configuration
|
|
|
|
* @c: configuration to be freed
|
|
|
|
*
|
|
|
|
* This function takes a &config structure and frees all resources
|
|
|
|
* associated with it.
|
|
|
|
*/
|
1999-02-05 21:37:34 +00:00
|
|
|
void
|
|
|
|
config_free(struct config *c)
|
|
|
|
{
|
|
|
|
rfree(c->pool);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-01-16 16:44:50 +00: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-05 21:37:34 +00:00
|
|
|
{
|
2000-01-16 16:44:50 +00: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-16 17:40:26 +00:00
|
|
|
global_commit(struct config *new, struct config *old)
|
2000-01-16 16:44:50 +00:00
|
|
|
{
|
|
|
|
if (!old)
|
|
|
|
return 0;
|
2009-06-18 17:20:07 +00:00
|
|
|
|
2009-06-23 09:08:30 +00:00
|
|
|
if (!ipa_equal(old->listen_bgp_addr, new->listen_bgp_addr) ||
|
|
|
|
(old->listen_bgp_port != new->listen_bgp_port) ||
|
|
|
|
(old->listen_bgp_flags != new->listen_bgp_flags))
|
2009-06-18 17:20:07 +00:00
|
|
|
log(L_WARN "Reconfiguration of BGP listening socket not implemented, please restart BIRD.");
|
|
|
|
|
2000-01-16 17:40:26 +00:00
|
|
|
if (!new->router_id)
|
|
|
|
new->router_id = old->router_id;
|
|
|
|
if (new->router_id != old->router_id)
|
2000-01-16 16:44:50 +00:00
|
|
|
return 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-06-19 21:49:34 +00:00
|
|
|
config_do_commit(struct config *c, int type)
|
2000-01-16 16:44:50 +00:00
|
|
|
{
|
|
|
|
int force_restart, nobs;
|
|
|
|
|
|
|
|
DBG("do_commit\n");
|
|
|
|
old_config = config;
|
2000-01-16 17:40:26 +00:00
|
|
|
config = new_config = c;
|
2000-01-16 16:44:50 +00:00
|
|
|
if (old_config)
|
|
|
|
old_config->obstacle_count++;
|
2010-02-06 21:57:51 +00:00
|
|
|
|
2000-01-16 16:44:50 +00:00
|
|
|
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");
|
2009-06-19 21:49:34 +00:00
|
|
|
protos_commit(c, old_config, force_restart, type);
|
2000-01-16 16:44:50 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2000-04-27 22:28:49 +00:00
|
|
|
static void
|
2004-06-05 09:10:56 +00:00
|
|
|
config_done(void *unused UNUSED)
|
2000-01-16 16:44:50 +00:00
|
|
|
{
|
|
|
|
struct config *c;
|
|
|
|
|
|
|
|
DBG("config_done\n");
|
|
|
|
for(;;)
|
|
|
|
{
|
2000-01-16 17:40:26 +00:00
|
|
|
if (config->shutdown)
|
|
|
|
sysdep_shutdown_done();
|
2000-01-16 16:44:50 +00: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;
|
2010-02-06 21:57:51 +00:00
|
|
|
log(L_INFO "Reconfiguring to queued configuration");
|
|
|
|
if (!config_do_commit(c, future_type))
|
2000-01-16 16:44:50 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-06-03 18:23:00 +00:00
|
|
|
/**
|
|
|
|
* config_commit - commit a configuration
|
|
|
|
* @c: new configuration
|
2009-06-19 21:49:34 +00:00
|
|
|
* @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
|
2000-06-03 18:23:00 +00:00
|
|
|
*
|
|
|
|
* When a configuration is parsed and prepared for use, the
|
|
|
|
* config_commit() function starts the process of reconfiguration.
|
|
|
|
* It checks whether there is already a reconfiguration in progress
|
|
|
|
* in which case it just queues the new config for later processing.
|
|
|
|
* Else it notifies all modules about the new configuration by calling
|
|
|
|
* their commit() functions which can either accept it immediately
|
|
|
|
* or call config_add_obstacle() to report that they need some time
|
|
|
|
* to complete the reconfiguration. After all such obstacles are removed
|
|
|
|
* using config_del_obstacle(), the old configuration is freed and
|
|
|
|
* everything runs according to the new one.
|
|
|
|
*
|
|
|
|
* Result: %CONF_DONE if the configuration has been accepted immediately,
|
|
|
|
* %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
|
|
|
|
* if it's been queued due to another reconfiguration being in progress now
|
|
|
|
* or %CONF_SHUTDOWN if BIRD is in shutdown mode and no new configurations
|
|
|
|
* are accepted.
|
|
|
|
*/
|
2000-01-16 16:44:50 +00:00
|
|
|
int
|
2009-06-19 21:49:34 +00:00
|
|
|
config_commit(struct config *c, int type)
|
2000-01-16 16:44:50 +00:00
|
|
|
{
|
|
|
|
if (!config) /* First-time configuration */
|
|
|
|
{
|
2009-06-19 21:49:34 +00:00
|
|
|
config_do_commit(c, RECONFIG_HARD);
|
2000-01-16 16:44:50 +00:00
|
|
|
return CONF_DONE;
|
|
|
|
}
|
|
|
|
if (old_config) /* Reconfiguration already in progress */
|
|
|
|
{
|
2008-10-26 22:09:46 +00:00
|
|
|
if (shutting_down == 2)
|
2000-01-16 17:40:26 +00:00
|
|
|
{
|
|
|
|
log(L_INFO "New configuration discarded due to shutdown");
|
|
|
|
config_free(c);
|
|
|
|
return CONF_SHUTDOWN;
|
|
|
|
}
|
2000-01-16 16:44:50 +00: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;
|
2010-02-06 21:57:51 +00:00
|
|
|
future_type = type;
|
2000-01-16 16:44:50 +00:00
|
|
|
return CONF_QUEUED;
|
|
|
|
}
|
2010-02-06 21:57:51 +00:00
|
|
|
|
|
|
|
if (!shutting_down)
|
|
|
|
log(L_INFO "Reconfiguring");
|
|
|
|
|
2009-06-19 21:49:34 +00:00
|
|
|
if (config_do_commit(c, type))
|
2000-01-16 16:44:50 +00:00
|
|
|
{
|
|
|
|
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-05 21:37:34 +00:00
|
|
|
}
|
|
|
|
|
2000-06-03 18:23:00 +00:00
|
|
|
/**
|
|
|
|
* order_shutdown - order BIRD shutdown
|
|
|
|
*
|
|
|
|
* This function initiates shutdown of BIRD. It's accomplished by asking
|
|
|
|
* for switching to an empty configuration.
|
|
|
|
*/
|
2000-01-16 17:40:26 +00: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;
|
|
|
|
shutting_down = 1;
|
2009-06-19 21:49:34 +00:00
|
|
|
config_commit(c, RECONFIG_HARD);
|
2008-10-26 22:09:46 +00:00
|
|
|
shutting_down = 2;
|
2000-01-16 17:40:26 +00:00
|
|
|
}
|
|
|
|
|
2000-06-03 18:23:00 +00:00
|
|
|
/**
|
|
|
|
* cf_error - report a configuration error
|
|
|
|
* @msg: printf-like format string
|
|
|
|
*
|
|
|
|
* cf_error() can be called during execution of config_parse(), that is
|
|
|
|
* from the parser, a preconfig hook or a postconfig hook, to report an
|
|
|
|
* error in the configuration.
|
|
|
|
*/
|
1999-02-05 21:37:34 +00: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);
|
|
|
|
}
|
|
|
|
|
2000-06-03 18:23:00 +00:00
|
|
|
/**
|
|
|
|
* cfg_strdup - copy a string to config memory
|
|
|
|
* @c: string to copy
|
|
|
|
*
|
|
|
|
* cfg_strdup() creates a new copy of the string in the memory
|
|
|
|
* pool associated with the configuration being currently parsed.
|
|
|
|
* It's often used when a string literal occurs in the configuration
|
|
|
|
* and we want to preserve it for further use.
|
|
|
|
*/
|
1999-02-05 21:37:34 +00:00
|
|
|
char *
|
|
|
|
cfg_strdup(char *c)
|
|
|
|
{
|
|
|
|
int l = strlen(c) + 1;
|
|
|
|
char *z = cfg_allocu(l);
|
|
|
|
memcpy(z, c, l);
|
|
|
|
return z;
|
|
|
|
}
|