osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 1 | /* |
| 2 | * rt_names.c rtnetlink names DB. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; either version |
| 7 | * 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru> |
| 10 | */ |
| 11 | |
| 12 | #include <stdio.h> |
| 13 | #include <stdlib.h> |
| 14 | #include <unistd.h> |
| 15 | #include <syslog.h> |
| 16 | #include <fcntl.h> |
| 17 | #include <string.h> |
| 18 | #include <sys/time.h> |
osdl.org!shemminger | b88fd9f | 2004-06-08 23:50:43 +0000 | [diff] [blame] | 19 | #include <sys/socket.h> |
| 20 | |
| 21 | #include <asm/types.h> |
| 22 | #include <linux/rtnetlink.h> |
| 23 | |
| 24 | #include "rt_names.h" |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 25 | |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 26 | #ifndef CONFDIR |
| 27 | #define CONFDIR "/etc/iproute2" |
| 28 | #endif |
| 29 | |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 30 | struct rtnl_hash_entry { |
| 31 | struct rtnl_hash_entry *next; |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 32 | const char * name; |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 33 | unsigned int id; |
| 34 | }; |
| 35 | |
| 36 | static void |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 37 | rtnl_hash_initialize(const char *file, struct rtnl_hash_entry **hash, int size) |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 38 | { |
| 39 | struct rtnl_hash_entry *entry; |
| 40 | char buf[512]; |
| 41 | FILE *fp; |
| 42 | |
| 43 | fp = fopen(file, "r"); |
| 44 | if (!fp) |
| 45 | return; |
| 46 | while (fgets(buf, sizeof(buf), fp)) { |
| 47 | char *p = buf; |
| 48 | int id; |
| 49 | char namebuf[512]; |
| 50 | |
| 51 | while (*p == ' ' || *p == '\t') |
| 52 | p++; |
| 53 | if (*p == '#' || *p == '\n' || *p == 0) |
| 54 | continue; |
| 55 | if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 && |
| 56 | sscanf(p, "0x%x %s #", &id, namebuf) != 2 && |
| 57 | sscanf(p, "%d %s\n", &id, namebuf) != 2 && |
| 58 | sscanf(p, "%d %s #", &id, namebuf) != 2) { |
| 59 | fprintf(stderr, "Database %s is corrupted at %s\n", |
| 60 | file, p); |
Thomas Jarosch | 97c1358 | 2011-10-03 05:22:42 +0000 | [diff] [blame] | 61 | fclose(fp); |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 62 | return; |
| 63 | } |
| 64 | |
| 65 | if (id<0) |
| 66 | continue; |
| 67 | entry = malloc(sizeof(*entry)); |
| 68 | entry->id = id; |
| 69 | entry->name = strdup(namebuf); |
| 70 | entry->next = hash[id & (size - 1)]; |
| 71 | hash[id & (size - 1)] = entry; |
| 72 | } |
| 73 | fclose(fp); |
| 74 | } |
| 75 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 76 | static void rtnl_tab_initialize(const char *file, char **tab, int size) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 77 | { |
| 78 | char buf[512]; |
| 79 | FILE *fp; |
| 80 | |
| 81 | fp = fopen(file, "r"); |
| 82 | if (!fp) |
| 83 | return; |
| 84 | while (fgets(buf, sizeof(buf), fp)) { |
| 85 | char *p = buf; |
| 86 | int id; |
| 87 | char namebuf[512]; |
| 88 | |
| 89 | while (*p == ' ' || *p == '\t') |
| 90 | p++; |
| 91 | if (*p == '#' || *p == '\n' || *p == 0) |
| 92 | continue; |
| 93 | if (sscanf(p, "0x%x %s\n", &id, namebuf) != 2 && |
| 94 | sscanf(p, "0x%x %s #", &id, namebuf) != 2 && |
| 95 | sscanf(p, "%d %s\n", &id, namebuf) != 2 && |
| 96 | sscanf(p, "%d %s #", &id, namebuf) != 2) { |
| 97 | fprintf(stderr, "Database %s is corrupted at %s\n", |
| 98 | file, p); |
Thomas Jarosch | 97c1358 | 2011-10-03 05:22:42 +0000 | [diff] [blame] | 99 | fclose(fp); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | |
| 103 | if (id<0 || id>size) |
| 104 | continue; |
| 105 | |
| 106 | tab[id] = strdup(namebuf); |
| 107 | } |
| 108 | fclose(fp); |
| 109 | } |
| 110 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 111 | static char * rtnl_rtprot_tab[256] = { |
osdl.org!shemminger | b88fd9f | 2004-06-08 23:50:43 +0000 | [diff] [blame] | 112 | [RTPROT_UNSPEC] = "none", |
| 113 | [RTPROT_REDIRECT] ="redirect", |
| 114 | [RTPROT_KERNEL] = "kernel", |
| 115 | [RTPROT_BOOT] = "boot", |
| 116 | [RTPROT_STATIC] = "static", |
| 117 | |
| 118 | [RTPROT_GATED] = "gated", |
| 119 | [RTPROT_RA] = "ra", |
| 120 | [RTPROT_MRT] = "mrt", |
| 121 | [RTPROT_ZEBRA] ="zebra", |
| 122 | [RTPROT_BIRD] = "bird", |
| 123 | [RTPROT_DNROUTED] = "dnrouted", |
| 124 | [RTPROT_XORP] = "xorp", |
shemminger | 40b6c62 | 2006-03-10 23:44:04 +0000 | [diff] [blame] | 125 | [RTPROT_NTK] = "ntk", |
Stephen Hemminger | 1e659af | 2009-03-18 13:33:12 -0700 | [diff] [blame] | 126 | [RTPROT_DHCP] = "dhcp", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
| 129 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 130 | static int rtnl_rtprot_init; |
| 131 | |
| 132 | static void rtnl_rtprot_initialize(void) |
| 133 | { |
| 134 | rtnl_rtprot_init = 1; |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 135 | rtnl_tab_initialize(CONFDIR "/rt_protos", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 136 | rtnl_rtprot_tab, 256); |
| 137 | } |
| 138 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 139 | const char * rtnl_rtprot_n2a(int id, char *buf, int len) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 140 | { |
| 141 | if (id<0 || id>=256) { |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 142 | snprintf(buf, len, "%u", id); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 143 | return buf; |
| 144 | } |
| 145 | if (!rtnl_rtprot_tab[id]) { |
| 146 | if (!rtnl_rtprot_init) |
| 147 | rtnl_rtprot_initialize(); |
| 148 | } |
| 149 | if (rtnl_rtprot_tab[id]) |
| 150 | return rtnl_rtprot_tab[id]; |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 151 | snprintf(buf, len, "%u", id); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 152 | return buf; |
| 153 | } |
| 154 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 155 | int rtnl_rtprot_a2n(__u32 *id, const char *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 156 | { |
| 157 | static char *cache = NULL; |
| 158 | static unsigned long res; |
| 159 | char *end; |
| 160 | int i; |
| 161 | |
| 162 | if (cache && strcmp(cache, arg) == 0) { |
| 163 | *id = res; |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | if (!rtnl_rtprot_init) |
| 168 | rtnl_rtprot_initialize(); |
| 169 | |
| 170 | for (i=0; i<256; i++) { |
| 171 | if (rtnl_rtprot_tab[i] && |
| 172 | strcmp(rtnl_rtprot_tab[i], arg) == 0) { |
| 173 | cache = rtnl_rtprot_tab[i]; |
| 174 | res = i; |
| 175 | *id = res; |
| 176 | return 0; |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | res = strtoul(arg, &end, 0); |
| 181 | if (!end || end == arg || *end || res > 255) |
| 182 | return -1; |
| 183 | *id = res; |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 188 | static char * rtnl_rtscope_tab[256] = { |
| 189 | "global", |
| 190 | }; |
| 191 | |
| 192 | static int rtnl_rtscope_init; |
| 193 | |
| 194 | static void rtnl_rtscope_initialize(void) |
| 195 | { |
| 196 | rtnl_rtscope_init = 1; |
Masatake YAMATO | 58ed50e | 2014-03-07 19:21:36 +0900 | [diff] [blame] | 197 | rtnl_rtscope_tab[RT_SCOPE_NOWHERE] = "nowhere"; |
| 198 | rtnl_rtscope_tab[RT_SCOPE_HOST] = "host"; |
| 199 | rtnl_rtscope_tab[RT_SCOPE_LINK] = "link"; |
| 200 | rtnl_rtscope_tab[RT_SCOPE_SITE] = "site"; |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 201 | rtnl_tab_initialize(CONFDIR "/rt_scopes", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 202 | rtnl_rtscope_tab, 256); |
| 203 | } |
| 204 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 205 | const char *rtnl_rtscope_n2a(int id, char *buf, int len) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 206 | { |
| 207 | if (id<0 || id>=256) { |
| 208 | snprintf(buf, len, "%d", id); |
| 209 | return buf; |
| 210 | } |
| 211 | if (!rtnl_rtscope_tab[id]) { |
| 212 | if (!rtnl_rtscope_init) |
| 213 | rtnl_rtscope_initialize(); |
| 214 | } |
| 215 | if (rtnl_rtscope_tab[id]) |
| 216 | return rtnl_rtscope_tab[id]; |
| 217 | snprintf(buf, len, "%d", id); |
| 218 | return buf; |
| 219 | } |
| 220 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 221 | int rtnl_rtscope_a2n(__u32 *id, const char *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 222 | { |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 223 | static const char *cache = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 224 | static unsigned long res; |
| 225 | char *end; |
| 226 | int i; |
| 227 | |
| 228 | if (cache && strcmp(cache, arg) == 0) { |
| 229 | *id = res; |
| 230 | return 0; |
| 231 | } |
| 232 | |
| 233 | if (!rtnl_rtscope_init) |
| 234 | rtnl_rtscope_initialize(); |
| 235 | |
| 236 | for (i=0; i<256; i++) { |
| 237 | if (rtnl_rtscope_tab[i] && |
| 238 | strcmp(rtnl_rtscope_tab[i], arg) == 0) { |
| 239 | cache = rtnl_rtscope_tab[i]; |
| 240 | res = i; |
| 241 | *id = res; |
| 242 | return 0; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | res = strtoul(arg, &end, 0); |
| 247 | if (!end || end == arg || *end || res > 255) |
| 248 | return -1; |
| 249 | *id = res; |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 254 | static char * rtnl_rtrealm_tab[256] = { |
| 255 | "unknown", |
| 256 | }; |
| 257 | |
| 258 | static int rtnl_rtrealm_init; |
| 259 | |
| 260 | static void rtnl_rtrealm_initialize(void) |
| 261 | { |
| 262 | rtnl_rtrealm_init = 1; |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 263 | rtnl_tab_initialize(CONFDIR "/rt_realms", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 264 | rtnl_rtrealm_tab, 256); |
| 265 | } |
| 266 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 267 | const char *rtnl_rtrealm_n2a(int id, char *buf, int len) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 268 | { |
| 269 | if (id<0 || id>=256) { |
| 270 | snprintf(buf, len, "%d", id); |
| 271 | return buf; |
| 272 | } |
| 273 | if (!rtnl_rtrealm_tab[id]) { |
| 274 | if (!rtnl_rtrealm_init) |
| 275 | rtnl_rtrealm_initialize(); |
| 276 | } |
| 277 | if (rtnl_rtrealm_tab[id]) |
| 278 | return rtnl_rtrealm_tab[id]; |
| 279 | snprintf(buf, len, "%d", id); |
| 280 | return buf; |
| 281 | } |
| 282 | |
| 283 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 284 | int rtnl_rtrealm_a2n(__u32 *id, const char *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 285 | { |
| 286 | static char *cache = NULL; |
| 287 | static unsigned long res; |
| 288 | char *end; |
| 289 | int i; |
| 290 | |
| 291 | if (cache && strcmp(cache, arg) == 0) { |
| 292 | *id = res; |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | if (!rtnl_rtrealm_init) |
| 297 | rtnl_rtrealm_initialize(); |
| 298 | |
| 299 | for (i=0; i<256; i++) { |
| 300 | if (rtnl_rtrealm_tab[i] && |
| 301 | strcmp(rtnl_rtrealm_tab[i], arg) == 0) { |
| 302 | cache = rtnl_rtrealm_tab[i]; |
| 303 | res = i; |
| 304 | *id = res; |
| 305 | return 0; |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | res = strtoul(arg, &end, 0); |
| 310 | if (!end || end == arg || *end || res > 255) |
| 311 | return -1; |
| 312 | *id = res; |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | |
Masatake YAMATO | 81ebcb2 | 2014-03-07 19:21:37 +0900 | [diff] [blame] | 317 | static struct rtnl_hash_entry dflt_table_entry = { .name = "default" }; |
| 318 | static struct rtnl_hash_entry main_table_entry = { .name = "main" }; |
| 319 | static struct rtnl_hash_entry local_table_entry = { .name = "local" }; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 320 | |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 321 | static struct rtnl_hash_entry * rtnl_rttable_hash[256] = { |
Masatake YAMATO | 81ebcb2 | 2014-03-07 19:21:37 +0900 | [diff] [blame] | 322 | [RT_TABLE_DEFAULT] = &dflt_table_entry, |
| 323 | [RT_TABLE_MAIN] = &main_table_entry, |
| 324 | [RT_TABLE_LOCAL] = &local_table_entry, |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 325 | }; |
| 326 | |
| 327 | static int rtnl_rttable_init; |
| 328 | |
| 329 | static void rtnl_rttable_initialize(void) |
| 330 | { |
Masatake YAMATO | 81ebcb2 | 2014-03-07 19:21:37 +0900 | [diff] [blame] | 331 | int i; |
| 332 | |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 333 | rtnl_rttable_init = 1; |
Masatake YAMATO | 81ebcb2 | 2014-03-07 19:21:37 +0900 | [diff] [blame] | 334 | for (i = 0; i < 256; i++) { |
| 335 | if (rtnl_rttable_hash[i]) |
| 336 | rtnl_rttable_hash[i]->id = i; |
| 337 | } |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 338 | rtnl_hash_initialize(CONFDIR "/rt_tables", |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 339 | rtnl_rttable_hash, 256); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 342 | const char * rtnl_rttable_n2a(__u32 id, char *buf, int len) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 343 | { |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 344 | struct rtnl_hash_entry *entry; |
| 345 | |
Boian Bonev | 887a5d0 | 2006-11-27 14:12:31 -0800 | [diff] [blame] | 346 | if (id > RT_TABLE_MAX) { |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 347 | snprintf(buf, len, "%u", id); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 348 | return buf; |
| 349 | } |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 350 | if (!rtnl_rttable_init) |
| 351 | rtnl_rttable_initialize(); |
| 352 | entry = rtnl_rttable_hash[id & 255]; |
| 353 | while (entry && entry->id != id) |
| 354 | entry = entry->next; |
| 355 | if (entry) |
| 356 | return entry->name; |
| 357 | snprintf(buf, len, "%u", id); |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 358 | return buf; |
| 359 | } |
| 360 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 361 | int rtnl_rttable_a2n(__u32 *id, const char *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 362 | { |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 363 | static const char *cache = NULL; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 364 | static unsigned long res; |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 365 | struct rtnl_hash_entry *entry; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 366 | char *end; |
Patrick McHardy | 34e9564 | 2006-08-11 00:14:51 +0200 | [diff] [blame] | 367 | __u32 i; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 368 | |
| 369 | if (cache && strcmp(cache, arg) == 0) { |
| 370 | *id = res; |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | if (!rtnl_rttable_init) |
| 375 | rtnl_rttable_initialize(); |
| 376 | |
| 377 | for (i=0; i<256; i++) { |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 378 | entry = rtnl_rttable_hash[i]; |
| 379 | while (entry && strcmp(entry->name, arg)) |
| 380 | entry = entry->next; |
| 381 | if (entry) { |
| 382 | cache = entry->name; |
| 383 | res = entry->id; |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 384 | *id = res; |
| 385 | return 0; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | i = strtoul(arg, &end, 0); |
Patrick McHardy | 9c47d87 | 2006-08-11 00:14:50 +0200 | [diff] [blame] | 390 | if (!end || end == arg || *end || i > RT_TABLE_MAX) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 391 | return -1; |
| 392 | *id = i; |
| 393 | return 0; |
| 394 | } |
| 395 | |
| 396 | |
| 397 | static char * rtnl_rtdsfield_tab[256] = { |
| 398 | "0", |
| 399 | }; |
| 400 | |
| 401 | static int rtnl_rtdsfield_init; |
| 402 | |
| 403 | static void rtnl_rtdsfield_initialize(void) |
| 404 | { |
| 405 | rtnl_rtdsfield_init = 1; |
Christoph J. Thompson | fb72129 | 2012-03-01 06:44:43 +0000 | [diff] [blame] | 406 | rtnl_tab_initialize(CONFDIR "/rt_dsfield", |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 407 | rtnl_rtdsfield_tab, 256); |
| 408 | } |
| 409 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 410 | const char *rtnl_dsfield_n2a(int id, char *buf, int len) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 411 | { |
| 412 | if (id<0 || id>=256) { |
| 413 | snprintf(buf, len, "%d", id); |
| 414 | return buf; |
| 415 | } |
| 416 | if (!rtnl_rtdsfield_tab[id]) { |
| 417 | if (!rtnl_rtdsfield_init) |
| 418 | rtnl_rtdsfield_initialize(); |
| 419 | } |
| 420 | if (rtnl_rtdsfield_tab[id]) |
| 421 | return rtnl_rtdsfield_tab[id]; |
| 422 | snprintf(buf, len, "0x%02x", id); |
| 423 | return buf; |
| 424 | } |
| 425 | |
| 426 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 427 | int rtnl_dsfield_a2n(__u32 *id, const char *arg) |
osdl.org!shemminger | aba5acd | 2004-04-15 20:56:59 +0000 | [diff] [blame] | 428 | { |
| 429 | static char *cache = NULL; |
| 430 | static unsigned long res; |
| 431 | char *end; |
| 432 | int i; |
| 433 | |
| 434 | if (cache && strcmp(cache, arg) == 0) { |
| 435 | *id = res; |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | if (!rtnl_rtdsfield_init) |
| 440 | rtnl_rtdsfield_initialize(); |
| 441 | |
| 442 | for (i=0; i<256; i++) { |
| 443 | if (rtnl_rtdsfield_tab[i] && |
| 444 | strcmp(rtnl_rtdsfield_tab[i], arg) == 0) { |
| 445 | cache = rtnl_rtdsfield_tab[i]; |
| 446 | res = i; |
| 447 | *id = res; |
| 448 | return 0; |
| 449 | } |
| 450 | } |
| 451 | |
| 452 | res = strtoul(arg, &end, 16); |
| 453 | if (!end || end == arg || *end || res > 255) |
| 454 | return -1; |
| 455 | *id = res; |
| 456 | return 0; |
| 457 | } |
| 458 | |
Vlad Dogaru | ac694c3 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 459 | |
| 460 | static struct rtnl_hash_entry dflt_group_entry = { .id = 0, .name = "default" }; |
| 461 | |
| 462 | static struct rtnl_hash_entry * rtnl_group_hash[256] = { |
| 463 | [0] = &dflt_group_entry, |
| 464 | }; |
| 465 | |
| 466 | static int rtnl_group_init; |
| 467 | |
| 468 | static void rtnl_group_initialize(void) |
| 469 | { |
| 470 | rtnl_group_init = 1; |
| 471 | rtnl_hash_initialize("/etc/iproute2/group", |
| 472 | rtnl_group_hash, 256); |
| 473 | } |
| 474 | |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 475 | int rtnl_group_a2n(int *id, const char *arg) |
Vlad Dogaru | ac694c3 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 476 | { |
Stephen Hemminger | 46ac8a5 | 2013-02-12 11:39:07 -0800 | [diff] [blame] | 477 | static const char *cache = NULL; |
Vlad Dogaru | ac694c3 | 2011-02-02 20:23:40 +0200 | [diff] [blame] | 478 | static unsigned long res; |
| 479 | struct rtnl_hash_entry *entry; |
| 480 | char *end; |
| 481 | int i; |
| 482 | |
| 483 | if (cache && strcmp(cache, arg) == 0) { |
| 484 | *id = res; |
| 485 | return 0; |
| 486 | } |
| 487 | |
| 488 | if (!rtnl_group_init) |
| 489 | rtnl_group_initialize(); |
| 490 | |
| 491 | for (i=0; i<256; i++) { |
| 492 | entry = rtnl_group_hash[i]; |
| 493 | while (entry && strcmp(entry->name, arg)) |
| 494 | entry = entry->next; |
| 495 | if (entry) { |
| 496 | cache = entry->name; |
| 497 | res = entry->id; |
| 498 | *id = res; |
| 499 | return 0; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | i = strtol(arg, &end, 0); |
| 504 | if (!end || end == arg || *end || i < 0) |
| 505 | return -1; |
| 506 | *id = i; |
| 507 | return 0; |
| 508 | } |
Stefan Tomanek | c4fdf75 | 2013-08-03 14:20:53 +0200 | [diff] [blame] | 509 | |
| 510 | const char *rtnl_group_n2a(int id, char *buf, int len) |
| 511 | { |
| 512 | struct rtnl_hash_entry *entry; |
| 513 | int i; |
| 514 | |
| 515 | if (!rtnl_group_init) |
| 516 | rtnl_group_initialize(); |
| 517 | |
| 518 | for (i=0; i<256; i++) { |
| 519 | entry = rtnl_group_hash[i]; |
| 520 | if (entry && entry->id == id) { |
| 521 | return entry->name; |
| 522 | } |
| 523 | } |
| 524 | |
| 525 | snprintf(buf, len, "%d", id); |
| 526 | return buf; |
| 527 | } |
vadimk | eef43b5 | 2014-10-30 17:33:36 +0200 | [diff] [blame^] | 528 | |
| 529 | static char *nl_proto_tab[256] = { |
| 530 | [NETLINK_ROUTE] = "rtnl", |
| 531 | [NETLINK_UNUSED] = "unused", |
| 532 | [NETLINK_USERSOCK] = "usersock", |
| 533 | [NETLINK_FIREWALL] = "fw", |
| 534 | [NETLINK_SOCK_DIAG] = "tcpdiag", |
| 535 | [NETLINK_NFLOG] = "nflog", |
| 536 | [NETLINK_XFRM] = "xfrm", |
| 537 | [NETLINK_SELINUX] = "selinux", |
| 538 | [NETLINK_ISCSI] = "iscsi", |
| 539 | [NETLINK_AUDIT] = "audit", |
| 540 | [NETLINK_FIB_LOOKUP] = "fiblookup", |
| 541 | [NETLINK_CONNECTOR] = "connector", |
| 542 | [NETLINK_NETFILTER] = "nft", |
| 543 | [NETLINK_IP6_FW] = "ip6fw", |
| 544 | [NETLINK_DNRTMSG] = "dec-rt", |
| 545 | [NETLINK_KOBJECT_UEVENT] = "uevent", |
| 546 | [NETLINK_GENERIC] = "genl", |
| 547 | [NETLINK_SCSITRANSPORT] = "scsi-trans", |
| 548 | [NETLINK_ECRYPTFS] = "ecryptfs", |
| 549 | [NETLINK_RDMA] = "rdma", |
| 550 | [NETLINK_CRYPTO] = "crypto", |
| 551 | }; |
| 552 | |
| 553 | static int nl_proto_init; |
| 554 | |
| 555 | static void nl_proto_initialize(void) |
| 556 | { |
| 557 | nl_proto_init = 1; |
| 558 | rtnl_tab_initialize(CONFDIR "/nl_protos", |
| 559 | nl_proto_tab, 256); |
| 560 | } |
| 561 | |
| 562 | const char *nl_proto_n2a(int id, char *buf, int len) |
| 563 | { |
| 564 | if (id < 0 || id >= 256) { |
| 565 | snprintf(buf, len, "%u", id); |
| 566 | return buf; |
| 567 | } |
| 568 | |
| 569 | if (!nl_proto_init) |
| 570 | nl_proto_initialize(); |
| 571 | |
| 572 | if (nl_proto_tab[id]) |
| 573 | return nl_proto_tab[id]; |
| 574 | |
| 575 | snprintf(buf, len, "%u", id); |
| 576 | return buf; |
| 577 | } |
| 578 | |
| 579 | int nl_proto_a2n(__u32 *id, const char *arg) |
| 580 | { |
| 581 | static char *cache = NULL; |
| 582 | static unsigned long res; |
| 583 | char *end; |
| 584 | int i; |
| 585 | |
| 586 | if (cache && strcmp(cache, arg) == 0) { |
| 587 | *id = res; |
| 588 | return 0; |
| 589 | } |
| 590 | |
| 591 | if (!nl_proto_init) |
| 592 | nl_proto_initialize(); |
| 593 | |
| 594 | for (i = 0; i < 256; i++) { |
| 595 | if (nl_proto_tab[i] && |
| 596 | strcmp(nl_proto_tab[i], arg) == 0) { |
| 597 | cache = nl_proto_tab[i]; |
| 598 | res = i; |
| 599 | *id = res; |
| 600 | return 0; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | res = strtoul(arg, &end, 0); |
| 605 | if (!end || end == arg || *end || res > 255) |
| 606 | return -1; |
| 607 | *id = res; |
| 608 | return 0; |
| 609 | } |