blob: 8dd16e3d38d2539e0a1555c15168ab9c855a4a70 [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/*
3 * rt_names.c rtnetlink names DB.
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version
8 * 2 of the License, or (at your option) any later version.
9 *
10 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
11 */
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000012#include "libbb.h"
Bernhard Reutner-Fischer1d62d3b2005-10-08 20:47:15 +000013#include "rt_names.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000014
Denys Vlasenko94466b82009-10-13 16:27:11 +020015typedef struct rtnl_tab_t {
16 const char *cached_str;
17 unsigned cached_result;
18 const char *tab[256];
19} rtnl_tab_t;
20
21static void rtnl_tab_initialize(const char *file, const char **tab)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000022{
Denis Vlasenko0f99d492008-07-24 23:38:04 +000023 char *token[2];
24 parser_t *parser = config_open2(file, fopen_for_read);
Denys Vlasenko94466b82009-10-13 16:27:11 +020025
Denis Vlasenko084266e2008-07-26 23:08:31 +000026 while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) {
Denys Vlasenko94466b82009-10-13 16:27:11 +020027 unsigned id = bb_strtou(token[0], NULL, 0);
28 if (id > 256) {
Denis Vlasenko0f99d492008-07-24 23:38:04 +000029 bb_error_msg("database %s is corrupted at line %d",
30 file, parser->lineno);
31 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +000033 tab[id] = xstrdup(token[1]);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000034 }
Denis Vlasenko0f99d492008-07-24 23:38:04 +000035 config_close(parser);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000036}
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000037
Denys Vlasenko94466b82009-10-13 16:27:11 +020038static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
39{
40 unsigned i;
41
42 if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
43 *id = tab->cached_result;
44 return 0;
45 }
46
47 for (i = 0; i < 256; i++) {
48 if (tab->tab[i]
49 && strcmp(tab->tab[i], arg) == 0
50 ) {
51 tab->cached_str = tab->tab[i];
52 tab->cached_result = i;
53 *id = i;
54 return 0;
55 }
56 }
57
58 i = bb_strtou(arg, NULL, base);
59 if (i > 255)
60 return -1;
61 *id = i;
62 return 0;
63}
64
65
66static rtnl_tab_t *rtnl_rtprot_tab;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000067
68static void rtnl_rtprot_initialize(void)
69{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000070 static const char *const init_tab[] = {
71 "none",
72 "redirect",
73 "kernel",
74 "boot",
75 "static",
76 NULL,
77 NULL,
78 NULL,
79 "gated",
80 "ra",
81 "mrt",
82 "zebra",
83 "bird",
84 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085
Denys Vlasenko94466b82009-10-13 16:27:11 +020086 if (rtnl_rtprot_tab)
87 return;
88 rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab));
89 memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
90 rtnl_tab_initialize("/etc/iproute2/rt_protos", rtnl_rtprot_tab->tab);
91}
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000092
Denys Vlasenkod31575a2009-10-13 17:58:24 +020093const char* FAST_FUNC rtnl_rtprot_n2a(int id, char *buf)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000094{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000095 if (id < 0 || id >= 256) {
Denys Vlasenkod31575a2009-10-13 17:58:24 +020096 sprintf(buf, "%d", id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000097 return buf;
98 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +000099
100 rtnl_rtprot_initialize();
101
Denys Vlasenko94466b82009-10-13 16:27:11 +0200102 if (rtnl_rtprot_tab->tab[id])
103 return rtnl_rtprot_tab->tab[id];
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200104 /* buf is SPRINT_BSIZE big */
105 sprintf(buf, "%d", id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000106 return buf;
107}
108
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200109int FAST_FUNC rtnl_rtprot_a2n(uint32_t *id, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000110{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000111 rtnl_rtprot_initialize();
Denys Vlasenko94466b82009-10-13 16:27:11 +0200112 return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000113}
114
115
Denys Vlasenko94466b82009-10-13 16:27:11 +0200116static rtnl_tab_t *rtnl_rtscope_tab;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000117
118static void rtnl_rtscope_initialize(void)
119{
Denys Vlasenko94466b82009-10-13 16:27:11 +0200120 if (rtnl_rtscope_tab)
121 return;
122 rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
123 rtnl_rtscope_tab->tab[0] = "global";
124 rtnl_rtscope_tab->tab[255] = "nowhere";
125 rtnl_rtscope_tab->tab[254] = "host";
126 rtnl_rtscope_tab->tab[253] = "link";
127 rtnl_rtscope_tab->tab[200] = "site";
128 rtnl_tab_initialize("/etc/iproute2/rt_scopes", rtnl_rtscope_tab->tab);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000129}
130
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200131const char* FAST_FUNC rtnl_rtscope_n2a(int id, char *buf)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000132{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000133 if (id < 0 || id >= 256) {
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200134 sprintf(buf, "%d", id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000135 return buf;
136 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000137
138 rtnl_rtscope_initialize();
139
Denys Vlasenko94466b82009-10-13 16:27:11 +0200140 if (rtnl_rtscope_tab->tab[id])
141 return rtnl_rtscope_tab->tab[id];
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200142 /* buf is SPRINT_BSIZE big */
143 sprintf(buf, "%d", id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000144 return buf;
145}
146
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200147int FAST_FUNC rtnl_rtscope_a2n(uint32_t *id, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000148{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000149 rtnl_rtscope_initialize();
Denys Vlasenko94466b82009-10-13 16:27:11 +0200150 return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000151}
152
153
Denys Vlasenko94466b82009-10-13 16:27:11 +0200154static rtnl_tab_t *rtnl_rtrealm_tab;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155
156static void rtnl_rtrealm_initialize(void)
157{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000158 if (rtnl_rtrealm_tab) return;
Denys Vlasenko94466b82009-10-13 16:27:11 +0200159 rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
160 rtnl_rtrealm_tab->tab[0] = "unknown";
161 rtnl_tab_initialize("/etc/iproute2/rt_realms", rtnl_rtrealm_tab->tab);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000162}
163
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200164int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000165{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000166 rtnl_rtrealm_initialize();
Denys Vlasenko94466b82009-10-13 16:27:11 +0200167 return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000168}
169
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000170#if ENABLE_FEATURE_IP_RULE
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200171const char* FAST_FUNC rtnl_rtrealm_n2a(int id, char *buf)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000172{
Denis Vlasenkoe27f1562007-01-01 06:00:38 +0000173 if (id < 0 || id >= 256) {
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200174 sprintf(buf, "%d", id);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000175 return buf;
176 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000177
178 rtnl_rtrealm_initialize();
179
Denys Vlasenko94466b82009-10-13 16:27:11 +0200180 if (rtnl_rtrealm_tab->tab[id])
181 return rtnl_rtrealm_tab->tab[id];
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200182 /* buf is SPRINT_BSIZE big */
183 sprintf(buf, "%d", id);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000184 return buf;
185}
186#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000187
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000188
Denys Vlasenko94466b82009-10-13 16:27:11 +0200189static rtnl_tab_t *rtnl_rtdsfield_tab;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000190
191static void rtnl_rtdsfield_initialize(void)
192{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000193 if (rtnl_rtdsfield_tab) return;
Denys Vlasenko94466b82009-10-13 16:27:11 +0200194 rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
195 rtnl_rtdsfield_tab->tab[0] = "0";
196 rtnl_tab_initialize("/etc/iproute2/rt_dsfield", rtnl_rtdsfield_tab->tab);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000197}
198
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200199const char* FAST_FUNC rtnl_dsfield_n2a(int id, char *buf)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000200{
Denis Vlasenkoe27f1562007-01-01 06:00:38 +0000201 if (id < 0 || id >= 256) {
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200202 sprintf(buf, "%d", id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000203 return buf;
204 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000205
206 rtnl_rtdsfield_initialize();
207
Denys Vlasenko94466b82009-10-13 16:27:11 +0200208 if (rtnl_rtdsfield_tab->tab[id])
209 return rtnl_rtdsfield_tab->tab[id];
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200210 /* buf is SPRINT_BSIZE big */
211 sprintf(buf, "0x%02x", id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000212 return buf;
213}
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{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000227 if (rtnl_rtdsfield_tab) return;
Denys Vlasenko94466b82009-10-13 16:27:11 +0200228 rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
229 rtnl_rttable_tab->tab[0] = "unspec";
230 rtnl_rttable_tab->tab[255] = "local";
231 rtnl_rttable_tab->tab[254] = "main";
232 rtnl_rttable_tab->tab[253] = "default";
233 rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab->tab);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000234}
235
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200236const char* FAST_FUNC rtnl_rttable_n2a(int id, char *buf)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000237{
238 if (id < 0 || id >= 256) {
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200239 sprintf(buf, "%d", id);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000240 return buf;
241 }
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000242
243 rtnl_rttable_initialize();
244
Denys Vlasenko94466b82009-10-13 16:27:11 +0200245 if (rtnl_rttable_tab->tab[id])
246 return rtnl_rttable_tab->tab[id];
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200247 /* buf is SPRINT_BSIZE big */
248 sprintf(buf, "%d", id);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000249 return buf;
250}
251
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200252int FAST_FUNC rtnl_rttable_a2n(uint32_t *id, char *arg)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000253{
Denis Vlasenkod1a302b2006-12-31 20:40:20 +0000254 rtnl_rttable_initialize();
Denys Vlasenko94466b82009-10-13 16:27:11 +0200255 return rtnl_a2n(rtnl_rttable_tab, id, arg, 0);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000256}
257
258#endif