"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 3 | * 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 McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 8 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 9 | */ |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bernhard Reutner-Fischer | 1d62d3b | 2005-10-08 20:47:15 +0000 | [diff] [blame] | 11 | #include "rt_names.h" |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 12 | |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 13 | typedef struct rtnl_tab_t { |
| 14 | const char *cached_str; |
| 15 | unsigned cached_result; |
| 16 | const char *tab[256]; |
| 17 | } rtnl_tab_t; |
| 18 | |
| 19 | static void rtnl_tab_initialize(const char *file, const char **tab) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 20 | { |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 21 | char *token[2]; |
| 22 | parser_t *parser = config_open2(file, fopen_for_read); |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 23 | |
Denis Vlasenko | 084266e | 2008-07-26 23:08:31 +0000 | [diff] [blame] | 24 | while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL)) { |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 25 | unsigned id = bb_strtou(token[0], NULL, 0); |
| 26 | if (id > 256) { |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 27 | bb_error_msg("database %s is corrupted at line %d", |
| 28 | file, parser->lineno); |
| 29 | break; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 30 | } |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 31 | tab[id] = xstrdup(token[1]); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 32 | } |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 33 | config_close(parser); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 34 | } |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 35 | |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 36 | static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base) |
| 37 | { |
| 38 | unsigned i; |
| 39 | |
| 40 | if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) { |
| 41 | *id = tab->cached_result; |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | for (i = 0; i < 256; i++) { |
| 46 | if (tab->tab[i] |
| 47 | && strcmp(tab->tab[i], arg) == 0 |
| 48 | ) { |
| 49 | tab->cached_str = tab->tab[i]; |
| 50 | tab->cached_result = i; |
| 51 | *id = i; |
| 52 | return 0; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | i = bb_strtou(arg, NULL, base); |
| 57 | if (i > 255) |
| 58 | return -1; |
| 59 | *id = i; |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | static rtnl_tab_t *rtnl_rtprot_tab; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 65 | |
| 66 | static void rtnl_rtprot_initialize(void) |
| 67 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 68 | static const char *const init_tab[] = { |
| 69 | "none", |
| 70 | "redirect", |
| 71 | "kernel", |
| 72 | "boot", |
| 73 | "static", |
| 74 | NULL, |
| 75 | NULL, |
| 76 | NULL, |
| 77 | "gated", |
| 78 | "ra", |
| 79 | "mrt", |
| 80 | "zebra", |
| 81 | "bird", |
| 82 | }; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 83 | |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 84 | if (rtnl_rtprot_tab) |
| 85 | return; |
| 86 | rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab)); |
| 87 | memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab)); |
| 88 | rtnl_tab_initialize("/etc/iproute2/rt_protos", rtnl_rtprot_tab->tab); |
| 89 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 90 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 91 | const char* FAST_FUNC rtnl_rtprot_n2a(int id, char *buf) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 92 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 93 | if (id < 0 || id >= 256) { |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 94 | sprintf(buf, "%d", id); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 95 | return buf; |
| 96 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 97 | |
| 98 | rtnl_rtprot_initialize(); |
| 99 | |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 100 | if (rtnl_rtprot_tab->tab[id]) |
| 101 | return rtnl_rtprot_tab->tab[id]; |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 102 | /* buf is SPRINT_BSIZE big */ |
| 103 | sprintf(buf, "%d", id); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 104 | return buf; |
| 105 | } |
| 106 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 107 | int FAST_FUNC rtnl_rtprot_a2n(uint32_t *id, char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 108 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 109 | rtnl_rtprot_initialize(); |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 110 | return rtnl_a2n(rtnl_rtprot_tab, id, arg, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 114 | static rtnl_tab_t *rtnl_rtscope_tab; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 115 | |
| 116 | static void rtnl_rtscope_initialize(void) |
| 117 | { |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 118 | if (rtnl_rtscope_tab) |
| 119 | return; |
| 120 | rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab)); |
| 121 | rtnl_rtscope_tab->tab[0] = "global"; |
| 122 | rtnl_rtscope_tab->tab[255] = "nowhere"; |
| 123 | rtnl_rtscope_tab->tab[254] = "host"; |
| 124 | rtnl_rtscope_tab->tab[253] = "link"; |
| 125 | rtnl_rtscope_tab->tab[200] = "site"; |
| 126 | rtnl_tab_initialize("/etc/iproute2/rt_scopes", rtnl_rtscope_tab->tab); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 129 | const char* FAST_FUNC rtnl_rtscope_n2a(int id, char *buf) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 130 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 131 | if (id < 0 || id >= 256) { |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 132 | sprintf(buf, "%d", id); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 133 | return buf; |
| 134 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 135 | |
| 136 | rtnl_rtscope_initialize(); |
| 137 | |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 138 | if (rtnl_rtscope_tab->tab[id]) |
| 139 | return rtnl_rtscope_tab->tab[id]; |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 140 | /* buf is SPRINT_BSIZE big */ |
| 141 | sprintf(buf, "%d", id); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 142 | return buf; |
| 143 | } |
| 144 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 145 | int FAST_FUNC rtnl_rtscope_a2n(uint32_t *id, char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 146 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 147 | rtnl_rtscope_initialize(); |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 148 | return rtnl_a2n(rtnl_rtscope_tab, id, arg, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 152 | static rtnl_tab_t *rtnl_rtrealm_tab; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 153 | |
| 154 | static void rtnl_rtrealm_initialize(void) |
| 155 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 156 | if (rtnl_rtrealm_tab) return; |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 157 | rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab)); |
| 158 | rtnl_rtrealm_tab->tab[0] = "unknown"; |
| 159 | rtnl_tab_initialize("/etc/iproute2/rt_realms", rtnl_rtrealm_tab->tab); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 162 | int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 163 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 164 | rtnl_rtrealm_initialize(); |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 165 | return rtnl_a2n(rtnl_rtrealm_tab, id, arg, 0); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 168 | #if ENABLE_FEATURE_IP_RULE |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 169 | const char* FAST_FUNC rtnl_rtrealm_n2a(int id, char *buf) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 170 | { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 171 | if (id < 0 || id >= 256) { |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 172 | sprintf(buf, "%d", id); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 173 | return buf; |
| 174 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 175 | |
| 176 | rtnl_rtrealm_initialize(); |
| 177 | |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 178 | if (rtnl_rtrealm_tab->tab[id]) |
| 179 | return rtnl_rtrealm_tab->tab[id]; |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 180 | /* buf is SPRINT_BSIZE big */ |
| 181 | sprintf(buf, "%d", id); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 182 | return buf; |
| 183 | } |
| 184 | #endif |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 185 | |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 186 | |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 187 | static rtnl_tab_t *rtnl_rtdsfield_tab; |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 188 | |
| 189 | static void rtnl_rtdsfield_initialize(void) |
| 190 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 191 | if (rtnl_rtdsfield_tab) return; |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 192 | rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab)); |
| 193 | rtnl_rtdsfield_tab->tab[0] = "0"; |
| 194 | rtnl_tab_initialize("/etc/iproute2/rt_dsfield", rtnl_rtdsfield_tab->tab); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 197 | const char* FAST_FUNC rtnl_dsfield_n2a(int id, char *buf) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 198 | { |
Denis Vlasenko | e27f156 | 2007-01-01 06:00:38 +0000 | [diff] [blame] | 199 | if (id < 0 || id >= 256) { |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 200 | sprintf(buf, "%d", id); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 201 | return buf; |
| 202 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 203 | |
| 204 | rtnl_rtdsfield_initialize(); |
| 205 | |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 206 | if (rtnl_rtdsfield_tab->tab[id]) |
| 207 | return rtnl_rtdsfield_tab->tab[id]; |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 208 | /* buf is SPRINT_BSIZE big */ |
| 209 | sprintf(buf, "0x%02x", id); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 210 | return buf; |
| 211 | } |
| 212 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 213 | int FAST_FUNC rtnl_dsfield_a2n(uint32_t *id, char *arg) |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 214 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 215 | rtnl_rtdsfield_initialize(); |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 216 | return rtnl_a2n(rtnl_rtdsfield_tab, id, arg, 16); |
Glenn L McGrath | 9a2d272 | 2002-11-10 01:33:55 +0000 | [diff] [blame] | 217 | } |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 218 | |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 219 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 220 | #if ENABLE_FEATURE_IP_RULE |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 221 | static rtnl_tab_t *rtnl_rttable_tab; |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 222 | |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 223 | static void rtnl_rttable_initialize(void) |
| 224 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 225 | if (rtnl_rtdsfield_tab) return; |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 226 | rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab)); |
| 227 | rtnl_rttable_tab->tab[0] = "unspec"; |
| 228 | rtnl_rttable_tab->tab[255] = "local"; |
| 229 | rtnl_rttable_tab->tab[254] = "main"; |
| 230 | rtnl_rttable_tab->tab[253] = "default"; |
| 231 | rtnl_tab_initialize("/etc/iproute2/rt_tables", rtnl_rttable_tab->tab); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 234 | const char* FAST_FUNC rtnl_rttable_n2a(int id, char *buf) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 235 | { |
| 236 | if (id < 0 || id >= 256) { |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 237 | sprintf(buf, "%d", id); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 238 | return buf; |
| 239 | } |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 240 | |
| 241 | rtnl_rttable_initialize(); |
| 242 | |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 243 | if (rtnl_rttable_tab->tab[id]) |
| 244 | return rtnl_rttable_tab->tab[id]; |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 245 | /* buf is SPRINT_BSIZE big */ |
| 246 | sprintf(buf, "%d", id); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 247 | return buf; |
| 248 | } |
| 249 | |
Denys Vlasenko | d31575a | 2009-10-13 17:58:24 +0200 | [diff] [blame] | 250 | int FAST_FUNC rtnl_rttable_a2n(uint32_t *id, char *arg) |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 251 | { |
Denis Vlasenko | d1a302b | 2006-12-31 20:40:20 +0000 | [diff] [blame] | 252 | rtnl_rttable_initialize(); |
Denys Vlasenko | 94466b8 | 2009-10-13 16:27:11 +0200 | [diff] [blame] | 253 | return rtnl_a2n(rtnl_rttable_tab, id, arg, 0); |
Bernhard Reutner-Fischer | 921f5df | 2006-11-21 15:36:08 +0000 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | #endif |