2006-07-03 01:17:05 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 07:03:55 +05:30
|
|
|
/*
|
2010-10-29 15:16:52 +05:30
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
2010-10-29 15:16:52 +05:30
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
2002-11-10 07:03:55 +05:30
|
|
|
*/
|
2007-01-01 02:10:20 +05:30
|
|
|
#include "libbb.h"
|
2005-10-09 02:17:15 +05:30
|
|
|
#include "rt_names.h"
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2015-10-08 20:36:06 +05:30
|
|
|
#define CONFDIR CONFIG_FEATURE_IP_ROUTE_DIR
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
typedef struct rtnl_tab_t {
|
|
|
|
const char *cached_str;
|
|
|
|
unsigned cached_result;
|
2015-10-08 18:40:10 +05:30
|
|
|
/* upstream version switched to a hash table and removed
|
|
|
|
* id < 256 limit. For now bbox bumps this array size from 256
|
|
|
|
* to 1024. If you plan to change this to a hash table,
|
|
|
|
* consider merging several hash tables we have (for example,
|
|
|
|
* awk has resizable one!
|
|
|
|
*/
|
|
|
|
#define RT_TABLE_MAX 1023
|
|
|
|
const char *tab[RT_TABLE_MAX+1];
|
2009-10-13 19:57:11 +05:30
|
|
|
} rtnl_tab_t;
|
|
|
|
|
|
|
|
static void rtnl_tab_initialize(const char *file, const char **tab)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2008-07-25 05:08:04 +05:30
|
|
|
char *token[2];
|
2015-10-08 20:45:08 +05:30
|
|
|
char fullname[sizeof(CONFDIR"/rt_dsfield") + 8];
|
|
|
|
parser_t *parser;
|
2009-10-13 19:57:11 +05:30
|
|
|
|
2015-10-08 20:45:08 +05:30
|
|
|
sprintf(fullname, CONFDIR"/rt_%s", file);
|
|
|
|
parser = config_open2(fullname, fopen_for_read);
|
2008-07-27 04:38:31 +05:30
|
|
|
while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
|
2009-10-13 19:57:11 +05:30
|
|
|
unsigned id = bb_strtou(token[0], NULL, 0);
|
2015-10-08 18:40:10 +05:30
|
|
|
if (id > RT_TABLE_MAX) {
|
2008-07-25 05:08:04 +05:30
|
|
|
bb_error_msg("database %s is corrupted at line %d",
|
|
|
|
file, parser->lineno);
|
|
|
|
break;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2008-07-25 05:08:04 +05:30
|
|
|
tab[id] = xstrdup(token[1]);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2008-07-25 05:08:04 +05:30
|
|
|
config_close(parser);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
|
|
|
|
*id = tab->cached_result;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-08 18:40:10 +05:30
|
|
|
for (i = 0; i <= RT_TABLE_MAX; i++) {
|
2009-10-13 19:57:11 +05:30
|
|
|
if (tab->tab[i]
|
|
|
|
&& strcmp(tab->tab[i], arg) == 0
|
|
|
|
) {
|
|
|
|
tab->cached_str = tab->tab[i];
|
|
|
|
tab->cached_result = i;
|
|
|
|
*id = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
i = bb_strtou(arg, NULL, base);
|
2015-10-08 18:40:10 +05:30
|
|
|
if (i > RT_TABLE_MAX)
|
2009-10-13 19:57:11 +05:30
|
|
|
return -1;
|
|
|
|
*id = i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static rtnl_tab_t *rtnl_rtprot_tab;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
static void rtnl_rtprot_initialize(void)
|
|
|
|
{
|
2020-11-30 17:33:03 +05:30
|
|
|
static const char *const init_tab[] ALIGN_PTR = {
|
2007-01-01 02:10:20 +05:30
|
|
|
"none",
|
|
|
|
"redirect",
|
|
|
|
"kernel",
|
|
|
|
"boot",
|
|
|
|
"static",
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
"gated",
|
|
|
|
"ra",
|
|
|
|
"mrt",
|
|
|
|
"zebra",
|
|
|
|
"bird",
|
|
|
|
};
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
if (rtnl_rtprot_tab)
|
|
|
|
return;
|
|
|
|
rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab));
|
|
|
|
memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
|
2015-10-08 20:45:08 +05:30
|
|
|
rtnl_tab_initialize("protos", rtnl_rtprot_tab->tab);
|
2009-10-13 19:57:11 +05:30
|
|
|
}
|
2007-01-01 02:10:20 +05:30
|
|
|
|
2015-10-08 16:32:28 +05:30
|
|
|
#if 0 /* UNUSED */
|
|
|
|
const char* FAST_FUNC rtnl_rtprot_n2a(int id)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2015-10-08 18:40:10 +05:30
|
|
|
if (id < 0 || id > RT_TABLE_MAX) {
|
2015-10-08 16:32:28 +05:30
|
|
|
return itoa(id);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2007-01-01 02:10:20 +05:30
|
|
|
|
|
|
|
rtnl_rtprot_initialize();
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
if (rtnl_rtprot_tab->tab[id])
|
|
|
|
return rtnl_rtprot_tab->tab[id];
|
2015-10-08 16:32:28 +05:30
|
|
|
return itoa(id);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2015-10-08 16:32:28 +05:30
|
|
|
#endif
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2009-10-13 21:28:24 +05:30
|
|
|
int FAST_FUNC rtnl_rtprot_a2n(uint32_t *id, char *arg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-01-01 02:10:20 +05:30
|
|
|
rtnl_rtprot_initialize();
|
2009-10-13 19:57:11 +05:30
|
|
|
return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
static rtnl_tab_t *rtnl_rtscope_tab;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
static void rtnl_rtscope_initialize(void)
|
|
|
|
{
|
2009-10-13 19:57:11 +05:30
|
|
|
if (rtnl_rtscope_tab)
|
|
|
|
return;
|
|
|
|
rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
|
|
|
|
rtnl_rtscope_tab->tab[0] = "global";
|
|
|
|
rtnl_rtscope_tab->tab[255] = "nowhere";
|
|
|
|
rtnl_rtscope_tab->tab[254] = "host";
|
|
|
|
rtnl_rtscope_tab->tab[253] = "link";
|
|
|
|
rtnl_rtscope_tab->tab[200] = "site";
|
2015-10-08 20:45:08 +05:30
|
|
|
rtnl_tab_initialize("scopes", rtnl_rtscope_tab->tab);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2015-10-08 16:32:28 +05:30
|
|
|
const char* FAST_FUNC rtnl_rtscope_n2a(int id)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2015-10-08 18:40:10 +05:30
|
|
|
if (id < 0 || id > RT_TABLE_MAX) {
|
2015-10-08 16:32:28 +05:30
|
|
|
return itoa(id);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2007-01-01 02:10:20 +05:30
|
|
|
|
|
|
|
rtnl_rtscope_initialize();
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
if (rtnl_rtscope_tab->tab[id])
|
|
|
|
return rtnl_rtscope_tab->tab[id];
|
2015-10-08 16:32:28 +05:30
|
|
|
return itoa(id);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2009-10-13 21:28:24 +05:30
|
|
|
int FAST_FUNC rtnl_rtscope_a2n(uint32_t *id, char *arg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-01-01 02:10:20 +05:30
|
|
|
rtnl_rtscope_initialize();
|
2009-10-13 19:57:11 +05:30
|
|
|
return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
static rtnl_tab_t *rtnl_rtrealm_tab;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
static void rtnl_rtrealm_initialize(void)
|
|
|
|
{
|
2007-01-01 02:10:20 +05:30
|
|
|
if (rtnl_rtrealm_tab) return;
|
2009-10-13 19:57:11 +05:30
|
|
|
rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
|
|
|
|
rtnl_rtrealm_tab->tab[0] = "unknown";
|
2015-10-08 20:45:08 +05:30
|
|
|
rtnl_tab_initialize("realms", rtnl_rtrealm_tab->tab);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2009-10-13 21:28:24 +05:30
|
|
|
int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-01-01 02:10:20 +05:30
|
|
|
rtnl_rtrealm_initialize();
|
2009-10-13 19:57:11 +05:30
|
|
|
return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2006-11-21 21:06:08 +05:30
|
|
|
#if ENABLE_FEATURE_IP_RULE
|
2015-10-08 16:32:28 +05:30
|
|
|
const char* FAST_FUNC rtnl_rtrealm_n2a(int id)
|
2006-11-21 21:06:08 +05:30
|
|
|
{
|
2015-10-08 18:40:10 +05:30
|
|
|
if (id < 0 || id > RT_TABLE_MAX) {
|
2015-10-08 16:32:28 +05:30
|
|
|
return itoa(id);
|
2006-11-21 21:06:08 +05:30
|
|
|
}
|
2007-01-01 02:10:20 +05:30
|
|
|
|
|
|
|
rtnl_rtrealm_initialize();
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
if (rtnl_rtrealm_tab->tab[id])
|
|
|
|
return rtnl_rtrealm_tab->tab[id];
|
2015-10-08 16:32:28 +05:30
|
|
|
return itoa(id);
|
2006-11-21 21:06:08 +05:30
|
|
|
}
|
|
|
|
#endif
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
static rtnl_tab_t *rtnl_rtdsfield_tab;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
static void rtnl_rtdsfield_initialize(void)
|
|
|
|
{
|
2007-01-01 02:10:20 +05:30
|
|
|
if (rtnl_rtdsfield_tab) return;
|
2009-10-13 19:57:11 +05:30
|
|
|
rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
|
|
|
|
rtnl_rtdsfield_tab->tab[0] = "0";
|
2015-10-08 20:45:08 +05:30
|
|
|
rtnl_tab_initialize("dsfield", rtnl_rtdsfield_tab->tab);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2015-10-08 16:32:28 +05:30
|
|
|
const char* FAST_FUNC rtnl_dsfield_n2a(int id)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2015-10-08 18:40:10 +05:30
|
|
|
if (id < 0 || id > RT_TABLE_MAX) {
|
2015-10-08 16:32:28 +05:30
|
|
|
return itoa(id);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2007-01-01 02:10:20 +05:30
|
|
|
|
|
|
|
rtnl_rtdsfield_initialize();
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
if (rtnl_rtdsfield_tab->tab[id])
|
|
|
|
return rtnl_rtdsfield_tab->tab[id];
|
2015-10-08 16:32:28 +05:30
|
|
|
return itoa(id);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2009-10-13 21:28:24 +05:30
|
|
|
int FAST_FUNC rtnl_dsfield_a2n(uint32_t *id, char *arg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-01-01 02:10:20 +05:30
|
|
|
rtnl_rtdsfield_initialize();
|
2009-10-13 19:57:11 +05:30
|
|
|
return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2006-11-21 21:06:08 +05:30
|
|
|
|
2007-01-01 02:10:20 +05:30
|
|
|
|
2006-11-21 21:06:08 +05:30
|
|
|
#if ENABLE_FEATURE_IP_RULE
|
2009-10-13 19:57:11 +05:30
|
|
|
static rtnl_tab_t *rtnl_rttable_tab;
|
2007-01-01 02:10:20 +05:30
|
|
|
|
2006-11-21 21:06:08 +05:30
|
|
|
static void rtnl_rttable_initialize(void)
|
|
|
|
{
|
2015-10-08 18:40:10 +05:30
|
|
|
if (rtnl_rttable_tab)
|
|
|
|
return;
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
|
|
|
|
rtnl_rttable_tab->tab[0] = "unspec";
|
|
|
|
rtnl_rttable_tab->tab[255] = "local";
|
|
|
|
rtnl_rttable_tab->tab[254] = "main";
|
|
|
|
rtnl_rttable_tab->tab[253] = "default";
|
2015-10-08 20:45:08 +05:30
|
|
|
rtnl_tab_initialize("tables", rtnl_rttable_tab->tab);
|
2006-11-21 21:06:08 +05:30
|
|
|
}
|
|
|
|
|
2015-10-08 16:32:28 +05:30
|
|
|
const char* FAST_FUNC rtnl_rttable_n2a(int id)
|
2006-11-21 21:06:08 +05:30
|
|
|
{
|
2015-10-08 18:40:10 +05:30
|
|
|
if (id < 0 || id > RT_TABLE_MAX) {
|
2015-10-08 16:32:28 +05:30
|
|
|
return itoa(id);
|
2006-11-21 21:06:08 +05:30
|
|
|
}
|
2007-01-01 02:10:20 +05:30
|
|
|
|
|
|
|
rtnl_rttable_initialize();
|
|
|
|
|
2009-10-13 19:57:11 +05:30
|
|
|
if (rtnl_rttable_tab->tab[id])
|
|
|
|
return rtnl_rttable_tab->tab[id];
|
2015-10-08 16:32:28 +05:30
|
|
|
return itoa(id);
|
2006-11-21 21:06:08 +05:30
|
|
|
}
|
|
|
|
|
2009-10-13 21:28:24 +05:30
|
|
|
int FAST_FUNC rtnl_rttable_a2n(uint32_t *id, char *arg)
|
2006-11-21 21:06:08 +05:30
|
|
|
{
|
2007-01-01 02:10:20 +05:30
|
|
|
rtnl_rttable_initialize();
|
2009-10-13 19:57:11 +05:30
|
|
|
return rtnl_a2n(rtnl_rttable_tab, id, arg, 0);
|
2006-11-21 21:06:08 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|