blob: 3c2fad9126a220f29da418d85a99fcad47556063 [file] [log] [blame]
"Robert P. J. Day"63fc1a92006-07-02 19:47:05 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Denys Vlasenkofb132e42010-10-29 11:46:52 +02003 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00007 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00009 */
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000010#include "libbb.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000011#include "rt_names.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000012
Christoph J. Thompsond1eea8d2015-10-08 17:06:06 +020013#define CONFDIR CONFIG_FEATURE_IP_ROUTE_DIR
14
Denys Vlasenko94466b82009-10-13 16:27:11 +020015typedef struct rtnl_tab_t {
16 const char *cached_str;
17 unsigned cached_result;
Denys Vlasenko413feca2015-10-08 15:10:10 +020018 /* upstream version switched to a hash table and removed
19 * id < 256 limit. For now bbox bumps this array size from 256
20 * to 1024. If you plan to change this to a hash table,
21 * consider merging several hash tables we have (for example,
22 * awk has resizable one!
23 */
24#define RT_TABLE_MAX 1023
25 const char *tab[RT_TABLE_MAX+1];
Denys Vlasenko94466b82009-10-13 16:27:11 +020026} rtnl_tab_t;
27
28static void rtnl_tab_initialize(const char *file, const char **tab)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000029{
Denis Vlasenko0f99d492008-07-24 23:38:04 +000030 char *token[2];
Denys Vlasenko8403b012015-10-08 17:15:08 +020031 char fullname[sizeof(CONFDIR"/rt_dsfield") + 8];
32 parser_t *parser;
Denys Vlasenko94466b82009-10-13 16:27:11 +020033
Denys Vlasenko8403b012015-10-08 17:15:08 +020034 sprintf(fullname, CONFDIR"/rt_%s", file);
35 parser = config_open2(fullname, fopen_for_read);
Denis Vlasenko084266e2008-07-26 23:08:31 +000036 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denys Vlasenko94466b82009-10-13 16:27:11 +020037 unsigned id = bb_strtou(token[0], NULL, 0);
Denys Vlasenko413feca2015-10-08 15:10:10 +020038 if (id > RT_TABLE_MAX) {
Denis Vlasenko0f99d492008-07-24 23:38:04 +000039 bb_error_msg("database %s is corrupted at line %d",
40 file, parser->lineno);
41 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000042 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +000043 tab[id] = xstrdup(token[1]);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000044 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +000045 config_close(parser);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000046}
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000047
Denys Vlasenko94466b82009-10-13 16:27:11 +020048static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
49{
50 unsigned i;
51
52 if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
53 *id = tab->cached_result;
54 return 0;
55 }
56
Denys Vlasenko413feca2015-10-08 15:10:10 +020057 for (i = 0; i <= RT_TABLE_MAX; i++) {
Denys Vlasenko94466b82009-10-13 16:27:11 +020058 if (tab->tab[i]
59 && strcmp(tab->tab[i], arg) == 0
60 ) {
61 tab->cached_str = tab->tab[i];
62 tab->cached_result = i;
63 *id = i;
64 return 0;
65 }
66 }
67
68 i = bb_strtou(arg, NULL, base);
Denys Vlasenko413feca2015-10-08 15:10:10 +020069 if (i > RT_TABLE_MAX)
Denys Vlasenko94466b82009-10-13 16:27:11 +020070 return -1;
71 *id = i;
72 return 0;
73}
74
75
76static rtnl_tab_t *rtnl_rtprot_tab;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000077
78static void rtnl_rtprot_initialize(void)
79{
Denys Vlasenko965b7952020-11-30 13:03:03 +010080 static const char *const init_tab[] ALIGN_PTR = {
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000081 "none",
82 "redirect",
83 "kernel",
84 "boot",
85 "static",
86 NULL,
87 NULL,
88 NULL,
89 "gated",
90 "ra",
91 "mrt",
92 "zebra",
93 "bird",
94 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000095
Denys Vlasenko94466b82009-10-13 16:27:11 +020096 if (rtnl_rtprot_tab)
97 return;
98 rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab));
99 memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
Denys Vlasenko8403b012015-10-08 17:15:08 +0200100 rtnl_tab_initialize("protos", rtnl_rtprot_tab->tab);
Denys Vlasenko94466b82009-10-13 16:27:11 +0200101}
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000102
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200103#if 0 /* UNUSED */
104const char* FAST_FUNC rtnl_rtprot_n2a(int id)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000105{
Denys Vlasenko413feca2015-10-08 15:10:10 +0200106 if (id < 0 || id > RT_TABLE_MAX) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200107 return itoa(id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000108 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000109
110 rtnl_rtprot_initialize();
111
Denys Vlasenko94466b82009-10-13 16:27:11 +0200112 if (rtnl_rtprot_tab->tab[id])
113 return rtnl_rtprot_tab->tab[id];
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200114 return itoa(id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115}
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200116#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000117
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200118int FAST_FUNC rtnl_rtprot_a2n(uint32_t *id, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000119{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000120 rtnl_rtprot_initialize();
Denys Vlasenko94466b82009-10-13 16:27:11 +0200121 return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122}
123
124
Denys Vlasenko94466b82009-10-13 16:27:11 +0200125static rtnl_tab_t *rtnl_rtscope_tab;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000126
127static void rtnl_rtscope_initialize(void)
128{
Denys Vlasenko94466b82009-10-13 16:27:11 +0200129 if (rtnl_rtscope_tab)
130 return;
131 rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
132 rtnl_rtscope_tab->tab[0] = "global";
133 rtnl_rtscope_tab->tab[255] = "nowhere";
134 rtnl_rtscope_tab->tab[254] = "host";
135 rtnl_rtscope_tab->tab[253] = "link";
136 rtnl_rtscope_tab->tab[200] = "site";
Denys Vlasenko8403b012015-10-08 17:15:08 +0200137 rtnl_tab_initialize("scopes", rtnl_rtscope_tab->tab);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000138}
139
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200140const char* FAST_FUNC rtnl_rtscope_n2a(int id)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000141{
Denys Vlasenko413feca2015-10-08 15:10:10 +0200142 if (id < 0 || id > RT_TABLE_MAX) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200143 return itoa(id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000144 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000145
146 rtnl_rtscope_initialize();
147
Denys Vlasenko94466b82009-10-13 16:27:11 +0200148 if (rtnl_rtscope_tab->tab[id])
149 return rtnl_rtscope_tab->tab[id];
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200150 return itoa(id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000151}
152
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200153int FAST_FUNC rtnl_rtscope_a2n(uint32_t *id, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000154{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000155 rtnl_rtscope_initialize();
Denys Vlasenko94466b82009-10-13 16:27:11 +0200156 return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157}
158
159
Denys Vlasenko94466b82009-10-13 16:27:11 +0200160static rtnl_tab_t *rtnl_rtrealm_tab;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161
162static void rtnl_rtrealm_initialize(void)
163{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000164 if (rtnl_rtrealm_tab) return;
Denys Vlasenko94466b82009-10-13 16:27:11 +0200165 rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
166 rtnl_rtrealm_tab->tab[0] = "unknown";
Denys Vlasenko8403b012015-10-08 17:15:08 +0200167 rtnl_tab_initialize("realms", rtnl_rtrealm_tab->tab);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168}
169
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200170int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000171{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000172 rtnl_rtrealm_initialize();
Denys Vlasenko94466b82009-10-13 16:27:11 +0200173 return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000174}
175
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000176#if ENABLE_FEATURE_IP_RULE
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200177const char* FAST_FUNC rtnl_rtrealm_n2a(int id)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000178{
Denys Vlasenko413feca2015-10-08 15:10:10 +0200179 if (id < 0 || id > RT_TABLE_MAX) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200180 return itoa(id);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000181 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000182
183 rtnl_rtrealm_initialize();
184
Denys Vlasenko94466b82009-10-13 16:27:11 +0200185 if (rtnl_rtrealm_tab->tab[id])
186 return rtnl_rtrealm_tab->tab[id];
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200187 return itoa(id);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000188}
189#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000190
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000191
Denys Vlasenko94466b82009-10-13 16:27:11 +0200192static rtnl_tab_t *rtnl_rtdsfield_tab;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000193
194static void rtnl_rtdsfield_initialize(void)
195{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000196 if (rtnl_rtdsfield_tab) return;
Denys Vlasenko94466b82009-10-13 16:27:11 +0200197 rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
198 rtnl_rtdsfield_tab->tab[0] = "0";
Denys Vlasenko8403b012015-10-08 17:15:08 +0200199 rtnl_tab_initialize("dsfield", rtnl_rtdsfield_tab->tab);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000200}
201
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200202const char* FAST_FUNC rtnl_dsfield_n2a(int id)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000203{
Denys Vlasenko413feca2015-10-08 15:10:10 +0200204 if (id < 0 || id > RT_TABLE_MAX) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200205 return itoa(id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000206 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000207
208 rtnl_rtdsfield_initialize();
209
Denys Vlasenko94466b82009-10-13 16:27:11 +0200210 if (rtnl_rtdsfield_tab->tab[id])
211 return rtnl_rtdsfield_tab->tab[id];
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200212 return itoa(id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000213}
214
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200215int FAST_FUNC rtnl_dsfield_a2n(uint32_t *id, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000216{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000217 rtnl_rtdsfield_initialize();
Denys Vlasenko94466b82009-10-13 16:27:11 +0200218 return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000219}
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000220
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000221
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000222#if ENABLE_FEATURE_IP_RULE
Denys Vlasenko94466b82009-10-13 16:27:11 +0200223static rtnl_tab_t *rtnl_rttable_tab;
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000224
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000225static void rtnl_rttable_initialize(void)
226{
Denys Vlasenko413feca2015-10-08 15:10:10 +0200227 if (rtnl_rttable_tab)
228 return;
229
Denys Vlasenko94466b82009-10-13 16:27:11 +0200230 rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
231 rtnl_rttable_tab->tab[0] = "unspec";
232 rtnl_rttable_tab->tab[255] = "local";
233 rtnl_rttable_tab->tab[254] = "main";
234 rtnl_rttable_tab->tab[253] = "default";
Denys Vlasenko8403b012015-10-08 17:15:08 +0200235 rtnl_tab_initialize("tables", rtnl_rttable_tab->tab);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000236}
237
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200238const char* FAST_FUNC rtnl_rttable_n2a(int id)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000239{
Denys Vlasenko413feca2015-10-08 15:10:10 +0200240 if (id < 0 || id > RT_TABLE_MAX) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200241 return itoa(id);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000242 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000243
244 rtnl_rttable_initialize();
245
Denys Vlasenko94466b82009-10-13 16:27:11 +0200246 if (rtnl_rttable_tab->tab[id])
247 return rtnl_rttable_tab->tab[id];
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200248 return itoa(id);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000249}
250
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200251int FAST_FUNC rtnl_rttable_a2n(uint32_t *id, char *arg)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000252{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000253 rtnl_rttable_initialize();
Denys Vlasenko94466b82009-10-13 16:27:11 +0200254 return rtnl_a2n(rtnl_rttable_tab, id, arg, 0);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000255}
256
257#endif