| 1 |
/* SPDX-License-Identifier: GPL-2.0-or-later */ |
| 2 |
/* |
| 3 |
* rt_names.c rtnetlink names DB. |
| 4 |
* |
| 5 |
* Authors: Alexey Kuznetsov, <[email protected]> |
| 6 |
*/ |
| 7 |
|
| 8 |
#include <stdio.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <unistd.h> |
| 11 |
#include <fcntl.h> |
| 12 |
#include <string.h> |
| 13 |
#include <sys/time.h> |
| 14 |
#include <sys/socket.h> |
| 15 |
#include <sys/stat.h> |
| 16 |
#include <dirent.h> |
| 17 |
#include <limits.h> |
| 18 |
#include <errno.h> |
| 19 |
|
| 20 |
#include <asm/types.h> |
| 21 |
#include <linux/rtnetlink.h> |
| 22 |
|
| 23 |
#include "rt_names.h" |
| 24 |
#include "utils.h" |
| 25 |
|
| 26 |
#define NAME_MAX_LEN 512 |
| 27 |
|
| 28 |
int numeric; |
| 29 |
|
| 30 |
struct rtnl_hash_entry { |
| 31 |
struct rtnl_hash_entry *next; |
| 32 |
const char *name; |
| 33 |
unsigned int id; |
| 34 |
}; |
| 35 |
|
| 36 |
static int fread_id_name(FILE *fp, int *id, char *namebuf) |
| 37 |
{ |
| 38 |
char buf[NAME_MAX_LEN]; |
| 39 |
|
| 40 |
while (fgets(buf, sizeof(buf), fp)) { |
| 41 |
char *p = buf; |
| 42 |
|
| 43 |
while (*p == ' ' || *p == '\t') |
| 44 |
p++; |
| 45 |
|
| 46 |
if (*p == '#' || *p == '\n' || *p == 0) |
| 47 |
continue; |
| 48 |
|
| 49 |
if (sscanf(p, "0x%x %s\n", id, namebuf) != 2 && |
| 50 |
sscanf(p, "0x%x %s #", id, namebuf) != 2 && |
| 51 |
sscanf(p, "%d %s\n", id, namebuf) != 2 && |
| 52 |
sscanf(p, "%d %s #", id, namebuf) != 2) { |
| 53 |
strcpy(namebuf, p); |
| 54 |
return -1; |
| 55 |
} |
| 56 |
return 1; |
| 57 |
} |
| 58 |
return 0; |
| 59 |
} |
| 60 |
|
| 61 |
static int |
| 62 |
rtnl_hash_initialize(const char *file, struct rtnl_hash_entry **hash, int size) |
| 63 |
{ |
| 64 |
struct rtnl_hash_entry *entry; |
| 65 |
FILE *fp; |
| 66 |
int id; |
| 67 |
char namebuf[NAME_MAX_LEN] = {0}; |
| 68 |
int ret; |
| 69 |
|
| 70 |
fp = fopen(file, "r"); |
| 71 |
if (!fp) |
| 72 |
return -errno; |
| 73 |
|
| 74 |
while ((ret = fread_id_name(fp, &id, &namebuf[0]))) { |
| 75 |
if (ret == -1) { |
| 76 |
fprintf(stderr, "Database %s is corrupted at %s\n", |
| 77 |
file, namebuf); |
| 78 |
fclose(fp); |
| 79 |
return -EINVAL; |
| 80 |
} |
| 81 |
|
| 82 |
if (id < 0) |
| 83 |
continue; |
| 84 |
|
| 85 |
entry = malloc(sizeof(*entry)); |
| 86 |
if (entry == NULL) { |
| 87 |
fprintf(stderr, "malloc error: for entry\n"); |
| 88 |
break; |
| 89 |
} |
| 90 |
entry->id = id; |
| 91 |
entry->name = strdup(namebuf); |
| 92 |
entry->next = hash[id & (size - 1)]; |
| 93 |
hash[id & (size - 1)] = entry; |
| 94 |
} |
| 95 |
fclose(fp); |
| 96 |
|
| 97 |
return 0; |
| 98 |
} |
| 99 |
|
| 100 |
static int rtnl_tab_initialize(const char *file, char **tab, int size) |
| 101 |
{ |
| 102 |
FILE *fp; |
| 103 |
int id; |
| 104 |
char namebuf[NAME_MAX_LEN] = {0}; |
| 105 |
int ret; |
| 106 |
|
| 107 |
fp = fopen(file, "r"); |
| 108 |
if (!fp) |
| 109 |
return -errno; |
| 110 |
|
| 111 |
while ((ret = fread_id_name(fp, &id, &namebuf[0]))) { |
| 112 |
if (ret == -1) { |
| 113 |
fprintf(stderr, "Database %s is corrupted at %s\n", |
| 114 |
file, namebuf); |
| 115 |
fclose(fp); |
| 116 |
return -EINVAL; |
| 117 |
} |
| 118 |
if (id < 0 || id > size) |
| 119 |
continue; |
| 120 |
|
| 121 |
tab[id] = strdup(namebuf); |
| 122 |
} |
| 123 |
fclose(fp); |
| 124 |
|
| 125 |
return 0; |
| 126 |
} |
| 127 |
|
| 128 |
static char *rtnl_rtprot_tab[256] = { |
| 129 |
[RTPROT_UNSPEC] = "unspec", |
| 130 |
[RTPROT_REDIRECT] = "redirect", |
| 131 |
[RTPROT_KERNEL] = "kernel", |
| 132 |
[RTPROT_BOOT] = "boot", |
| 133 |
[RTPROT_STATIC] = "static", |
| 134 |
|
| 135 |
[RTPROT_GATED] = "gated", |
| 136 |
[RTPROT_RA] = "ra", |
| 137 |
[RTPROT_MRT] = "mrt", |
| 138 |
[RTPROT_ZEBRA] = "zebra", |
| 139 |
[RTPROT_BIRD] = "bird", |
| 140 |
[RTPROT_BABEL] = "babel", |
| 141 |
[RTPROT_DNROUTED] = "dnrouted", |
| 142 |
[RTPROT_XORP] = "xorp", |
| 143 |
[RTPROT_NTK] = "ntk", |
| 144 |
[RTPROT_DHCP] = "dhcp", |
| 145 |
[RTPROT_KEEPALIVED] = "keepalived", |
| 146 |
[RTPROT_BGP] = "bgp", |
| 147 |
[RTPROT_ISIS] = "isis", |
| 148 |
[RTPROT_OSPF] = "ospf", |
| 149 |
[RTPROT_RIP] = "rip", |
| 150 |
[RTPROT_EIGRP] = "eigrp", |
| 151 |
}; |
| 152 |
|
| 153 |
struct tabhash { |
| 154 |
enum { TAB, HASH } type; |
| 155 |
union tab_or_hash { |
| 156 |
char **tab; |
| 157 |
struct rtnl_hash_entry **hash; |
| 158 |
} data; |
| 159 |
}; |
| 160 |
|
| 161 |
static void |
| 162 |
rtnl_tabhash_readdir(const char *dirpath_base, const char *dirpath_overload, |
| 163 |
const struct tabhash tabhash, const int size) |
| 164 |
{ |
| 165 |
struct dirent *de; |
| 166 |
DIR *d; |
| 167 |
|
| 168 |
d = opendir(dirpath_base); |
| 169 |
while (d && (de = readdir(d)) != NULL) { |
| 170 |
char path[PATH_MAX]; |
| 171 |
size_t len; |
| 172 |
struct stat sb; |
| 173 |
|
| 174 |
if (*de->d_name == '.') |
| 175 |
continue; |
| 176 |
|
| 177 |
/* only consider filenames ending in '.conf' */ |
| 178 |
len = strlen(de->d_name); |
| 179 |
if (len <= 5) |
| 180 |
continue; |
| 181 |
if (strcmp(de->d_name + len - 5, ".conf")) |
| 182 |
continue; |
| 183 |
|
| 184 |
if (dirpath_overload) { |
| 185 |
/* only consider filenames not present in |
| 186 |
the overloading directory, e.g. /etc */ |
| 187 |
snprintf(path, sizeof(path), "%s/%s", dirpath_overload, de->d_name); |
| 188 |
if (lstat(path, &sb) == 0) |
| 189 |
continue; |
| 190 |
} |
| 191 |
|
| 192 |
/* load the conf file in the base directory, e.g., /usr */ |
| 193 |
snprintf(path, sizeof(path), "%s/%s", dirpath_base, de->d_name); |
| 194 |
if (tabhash.type == TAB) |
| 195 |
rtnl_tab_initialize(path, tabhash.data.tab, size); |
| 196 |
else |
| 197 |
rtnl_hash_initialize(path, tabhash.data.hash, size); |
| 198 |
} |
| 199 |
if (d) |
| 200 |
closedir(d); |
| 201 |
} |
| 202 |
|
| 203 |
static void |
| 204 |
rtnl_tabhash_initialize_dir(const char *ddir, const struct tabhash tabhash, const int size) |
| 205 |
{ |
| 206 |
char dirpath_usr[PATH_MAX], dirpath_etc[PATH_MAX]; |
| 207 |
|
| 208 |
snprintf(dirpath_usr, sizeof(dirpath_usr), "%s/%s", CONF_USR_DIR, ddir); |
| 209 |
snprintf(dirpath_etc, sizeof(dirpath_etc), "%s/%s", CONF_ETC_DIR, ddir); |
| 210 |
|
| 211 |
/* |
| 212 |
* load CONF_USR_DIR/iproute2/foo.d/X conf files |
| 213 |
* unless CONF_ETC_DIR/iproute2/foo.d/X exists |
| 214 |
*/ |
| 215 |
rtnl_tabhash_readdir(dirpath_usr, dirpath_etc, tabhash, size); |
| 216 |
|
| 217 |
/* load /etc/iproute2/foo.d/X conf files */ |
| 218 |
rtnl_tabhash_readdir(dirpath_etc, NULL, tabhash, size); |
| 219 |
} |
| 220 |
|
| 221 |
static void |
| 222 |
rtnl_tab_initialize_dir(const char *ddir, char **tab, const int size) |
| 223 |
{ |
| 224 |
struct tabhash tab_data = {.type = TAB, .data.tab = tab}; |
| 225 |
rtnl_tabhash_initialize_dir(ddir, tab_data, size); |
| 226 |
} |
| 227 |
|
| 228 |
static void |
| 229 |
rtnl_hash_initialize_dir(const char *ddir, struct rtnl_hash_entry **hash, |
| 230 |
const int size) { |
| 231 |
struct tabhash hash_data = {.type = HASH, .data.hash = hash}; |
| 232 |
rtnl_tabhash_initialize_dir(ddir, hash_data, size); |
| 233 |
} |
| 234 |
|
| 235 |
static int rtnl_rtprot_init; |
| 236 |
|
| 237 |
static void rtnl_rtprot_initialize(void) |
| 238 |
{ |
| 239 |
int ret; |
| 240 |
|
| 241 |
rtnl_rtprot_init = 1; |
| 242 |
ret = rtnl_tab_initialize(CONF_ETC_DIR "/rt_protos", |
| 243 |
rtnl_rtprot_tab, 256); |
| 244 |
if (ret == -ENOENT) |
| 245 |
rtnl_tab_initialize(CONF_USR_DIR "/rt_protos", |
| 246 |
rtnl_rtprot_tab, 256); |
| 247 |
|
| 248 |
rtnl_tab_initialize_dir("rt_protos.d", rtnl_rtprot_tab, 256); |
| 249 |
} |
| 250 |
|
| 251 |
const char *rtnl_rtprot_n2a(int id, char *buf, int len) |
| 252 |
{ |
| 253 |
if (id < 0 || id >= 256 || numeric) { |
| 254 |
snprintf(buf, len, "%u", id); |
| 255 |
return buf; |
| 256 |
} |
| 257 |
if (!rtnl_rtprot_tab[id]) { |
| 258 |
if (!rtnl_rtprot_init) |
| 259 |
rtnl_rtprot_initialize(); |
| 260 |
} |
| 261 |
if (rtnl_rtprot_tab[id]) |
| 262 |
return rtnl_rtprot_tab[id]; |
| 263 |
snprintf(buf, len, "%u", id); |
| 264 |
return buf; |
| 265 |
} |
| 266 |
|
| 267 |
int rtnl_rtprot_a2n(__u32 *id, const char *arg) |
| 268 |
{ |
| 269 |
static char *cache; |
| 270 |
static unsigned long res; |
| 271 |
char *end; |
| 272 |
int i; |
| 273 |
|
| 274 |
if (cache && strcmp(cache, arg) == 0) { |
| 275 |
*id = res; |
| 276 |
return 0; |
| 277 |
} |
| 278 |
|
| 279 |
if (!rtnl_rtprot_init) |
| 280 |
rtnl_rtprot_initialize(); |
| 281 |
|
| 282 |
for (i = 0; i < 256; i++) { |
| 283 |
if (rtnl_rtprot_tab[i] && |
| 284 |
strcmp(rtnl_rtprot_tab[i], arg) == 0) { |
| 285 |
cache = rtnl_rtprot_tab[i]; |
| 286 |
res = i; |
| 287 |
*id = res; |
| 288 |
return 0; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
res = strtoul(arg, &end, 0); |
| 293 |
if (!end || end == arg || *end || res > 255) |
| 294 |
return -1; |
| 295 |
*id = res; |
| 296 |
return 0; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
static char *rtnl_addrprot_tab[256] = { |
| 301 |
[IFAPROT_UNSPEC] = "unspec", |
| 302 |
[IFAPROT_KERNEL_LO] = "kernel_lo", |
| 303 |
[IFAPROT_KERNEL_RA] = "kernel_ra", |
| 304 |
[IFAPROT_KERNEL_LL] = "kernel_ll", |
| 305 |
}; |
| 306 |
static bool rtnl_addrprot_tab_initialized; |
| 307 |
|
| 308 |
static void rtnl_addrprot_initialize(void) |
| 309 |
{ |
| 310 |
int ret; |
| 311 |
|
| 312 |
rtnl_addrprot_tab_initialized = true; |
| 313 |
|
| 314 |
ret = rtnl_tab_initialize(CONF_ETC_DIR "/rt_addrprotos", |
| 315 |
rtnl_addrprot_tab, |
| 316 |
ARRAY_SIZE(rtnl_addrprot_tab)); |
| 317 |
if (ret == -ENOENT) |
| 318 |
ret = rtnl_tab_initialize(CONF_USR_DIR "/rt_addrprotos", |
| 319 |
rtnl_addrprot_tab, |
| 320 |
ARRAY_SIZE(rtnl_addrprot_tab)); |
| 321 |
|
| 322 |
rtnl_tab_initialize_dir("rt_addrprotos.d", rtnl_addrprot_tab, |
| 323 |
ARRAY_SIZE(rtnl_addrprot_tab)); |
| 324 |
} |
| 325 |
|
| 326 |
const char *rtnl_addrprot_n2a(__u8 id, char *buf, int len) |
| 327 |
{ |
| 328 |
if (numeric) |
| 329 |
goto numeric; |
| 330 |
if (!rtnl_addrprot_tab_initialized) |
| 331 |
rtnl_addrprot_initialize(); |
| 332 |
if (rtnl_addrprot_tab[id]) |
| 333 |
return rtnl_addrprot_tab[id]; |
| 334 |
numeric: |
| 335 |
snprintf(buf, len, "%#x", id); |
| 336 |
return buf; |
| 337 |
} |
| 338 |
|
| 339 |
int rtnl_addrprot_a2n(__u8 *id, const char *arg) |
| 340 |
{ |
| 341 |
unsigned long res; |
| 342 |
char *end; |
| 343 |
int i; |
| 344 |
|
| 345 |
if (!rtnl_addrprot_tab_initialized) |
| 346 |
rtnl_addrprot_initialize(); |
| 347 |
|
| 348 |
for (i = 0; i < 256; i++) { |
| 349 |
if (rtnl_addrprot_tab[i] && |
| 350 |
strcmp(rtnl_addrprot_tab[i], arg) == 0) { |
| 351 |
*id = i; |
| 352 |
return 0; |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
res = strtoul(arg, &end, 0); |
| 357 |
if (!end || end == arg || *end || res > 255) |
| 358 |
return -1; |
| 359 |
*id = res; |
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
static char *rtnl_rtscope_tab[256] = { |
| 365 |
[RT_SCOPE_UNIVERSE] = "global", |
| 366 |
[RT_SCOPE_NOWHERE] = "nowhere", |
| 367 |
[RT_SCOPE_HOST] = "host", |
| 368 |
[RT_SCOPE_LINK] = "link", |
| 369 |
[RT_SCOPE_SITE] = "site", |
| 370 |
}; |
| 371 |
|
| 372 |
static int rtnl_rtscope_init; |
| 373 |
|
| 374 |
static void rtnl_rtscope_initialize(void) |
| 375 |
{ |
| 376 |
int ret; |
| 377 |
|
| 378 |
rtnl_rtscope_init = 1; |
| 379 |
ret = rtnl_tab_initialize(CONF_ETC_DIR "/rt_scopes", |
| 380 |
rtnl_rtscope_tab, 256); |
| 381 |
if (ret == -ENOENT) |
| 382 |
rtnl_tab_initialize(CONF_USR_DIR "/rt_scopes", |
| 383 |
rtnl_rtscope_tab, 256); |
| 384 |
} |
| 385 |
|
| 386 |
const char *rtnl_rtscope_n2a(int id, char *buf, int len) |
| 387 |
{ |
| 388 |
if (id < 0 || id >= 256 || numeric) { |
| 389 |
snprintf(buf, len, "%d", id); |
| 390 |
return buf; |
| 391 |
} |
| 392 |
|
| 393 |
if (!rtnl_rtscope_tab[id]) { |
| 394 |
if (!rtnl_rtscope_init) |
| 395 |
rtnl_rtscope_initialize(); |
| 396 |
} |
| 397 |
|
| 398 |
if (rtnl_rtscope_tab[id]) |
| 399 |
return rtnl_rtscope_tab[id]; |
| 400 |
|
| 401 |
snprintf(buf, len, "%d", id); |
| 402 |
return buf; |
| 403 |
} |
| 404 |
|
| 405 |
int rtnl_rtscope_a2n(__u32 *id, const char *arg) |
| 406 |
{ |
| 407 |
static const char *cache; |
| 408 |
static unsigned long res; |
| 409 |
char *end; |
| 410 |
int i; |
| 411 |
|
| 412 |
if (cache && strcmp(cache, arg) == 0) { |
| 413 |
*id = res; |
| 414 |
return 0; |
| 415 |
} |
| 416 |
|
| 417 |
if (!rtnl_rtscope_init) |
| 418 |
rtnl_rtscope_initialize(); |
| 419 |
|
| 420 |
for (i = 0; i < 256; i++) { |
| 421 |
if (rtnl_rtscope_tab[i] && |
| 422 |
strcmp(rtnl_rtscope_tab[i], arg) == 0) { |
| 423 |
cache = rtnl_rtscope_tab[i]; |
| 424 |
res = i; |
| 425 |
*id = res; |
| 426 |
return 0; |
| 427 |
} |
| 428 |
} |
| 429 |
|
| 430 |
res = strtoul(arg, &end, 0); |
| 431 |
if (!end || end == arg || *end || res > 255) |
| 432 |
return -1; |
| 433 |
*id = res; |
| 434 |
return 0; |
| 435 |
} |
| 436 |
|
| 437 |
|
| 438 |
static char *rtnl_rtrealm_tab[256] = { |
| 439 |
"unknown", |
| 440 |
}; |
| 441 |
|
| 442 |
static int rtnl_rtrealm_init; |
| 443 |
|
| 444 |
static void rtnl_rtrealm_initialize(void) |
| 445 |
{ |
| 446 |
int ret; |
| 447 |
|
| 448 |
rtnl_rtrealm_init = 1; |
| 449 |
ret = rtnl_tab_initialize(CONF_ETC_DIR "/rt_realms", |
| 450 |
rtnl_rtrealm_tab, 256); |
| 451 |
if (ret == -ENOENT) |
| 452 |
rtnl_tab_initialize(CONF_USR_DIR "/rt_realms", |
| 453 |
rtnl_rtrealm_tab, 256); |
| 454 |
} |
| 455 |
|
| 456 |
const char *rtnl_rtrealm_n2a(int id, char *buf, int len) |
| 457 |
{ |
| 458 |
if (id < 0 || id >= 256 || numeric) { |
| 459 |
snprintf(buf, len, "%d", id); |
| 460 |
return buf; |
| 461 |
} |
| 462 |
if (!rtnl_rtrealm_tab[id]) { |
| 463 |
if (!rtnl_rtrealm_init) |
| 464 |
rtnl_rtrealm_initialize(); |
| 465 |
} |
| 466 |
if (rtnl_rtrealm_tab[id]) |
| 467 |
return rtnl_rtrealm_tab[id]; |
| 468 |
snprintf(buf, len, "%d", id); |
| 469 |
return buf; |
| 470 |
} |
| 471 |
|
| 472 |
|
| 473 |
int rtnl_rtrealm_a2n(__u32 *id, const char *arg) |
| 474 |
{ |
| 475 |
static char *cache; |
| 476 |
static unsigned long res; |
| 477 |
char *end; |
| 478 |
int i; |
| 479 |
|
| 480 |
if (cache && strcmp(cache, arg) == 0) { |
| 481 |
*id = res; |
| 482 |
return 0; |
| 483 |
} |
| 484 |
|
| 485 |
if (!rtnl_rtrealm_init) |
| 486 |
rtnl_rtrealm_initialize(); |
| 487 |
|
| 488 |
for (i = 0; i < 256; i++) { |
| 489 |
if (rtnl_rtrealm_tab[i] && |
| 490 |
strcmp(rtnl_rtrealm_tab[i], arg) == 0) { |
| 491 |
cache = rtnl_rtrealm_tab[i]; |
| 492 |
res = i; |
| 493 |
*id = res; |
| 494 |
return 0; |
| 495 |
} |
| 496 |
} |
| 497 |
|
| 498 |
res = strtoul(arg, &end, 0); |
| 499 |
if (!end || end == arg || *end || res > 255) |
| 500 |
return -1; |
| 501 |
*id = res; |
| 502 |
return 0; |
| 503 |
} |
| 504 |
|
| 505 |
|
| 506 |
static struct rtnl_hash_entry dflt_table_entry = { .name = "default" }; |
| 507 |
static struct rtnl_hash_entry main_table_entry = { .name = "main" }; |
| 508 |
static struct rtnl_hash_entry local_table_entry = { .name = "local" }; |
| 509 |
|
| 510 |
static struct rtnl_hash_entry *rtnl_rttable_hash[256] = { |
| 511 |
[RT_TABLE_DEFAULT] = &dflt_table_entry, |
| 512 |
[RT_TABLE_MAIN] = &main_table_entry, |
| 513 |
[RT_TABLE_LOCAL] = &local_table_entry, |
| 514 |
}; |
| 515 |
|
| 516 |
static int rtnl_rttable_init; |
| 517 |
|
| 518 |
static void rtnl_rttable_initialize(void) |
| 519 |
{ |
| 520 |
int i; |
| 521 |
int ret; |
| 522 |
|
| 523 |
rtnl_rttable_init = 1; |
| 524 |
for (i = 0; i < 256; i++) { |
| 525 |
if (rtnl_rttable_hash[i]) |
| 526 |
rtnl_rttable_hash[i]->id = i; |
| 527 |
} |
| 528 |
ret = rtnl_hash_initialize(CONF_ETC_DIR "/rt_tables", |
| 529 |
rtnl_rttable_hash, 256); |
| 530 |
if (ret == -ENOENT) |
| 531 |
rtnl_hash_initialize(CONF_USR_DIR "/rt_tables", |
| 532 |
rtnl_rttable_hash, 256); |
| 533 |
|
| 534 |
rtnl_hash_initialize_dir("rt_tables.d", rtnl_rttable_hash, 256); |
| 535 |
} |
| 536 |
|
| 537 |
const char *rtnl_rttable_n2a(__u32 id, char *buf, int len) |
| 538 |
{ |
| 539 |
struct rtnl_hash_entry *entry; |
| 540 |
|
| 541 |
if (!rtnl_rttable_init) |
| 542 |
rtnl_rttable_initialize(); |
| 543 |
entry = rtnl_rttable_hash[id & 255]; |
| 544 |
while (entry && entry->id != id) |
| 545 |
entry = entry->next; |
| 546 |
if (!numeric && entry) |
| 547 |
return entry->name; |
| 548 |
snprintf(buf, len, "%u", id); |
| 549 |
return buf; |
| 550 |
} |
| 551 |
|
| 552 |
int rtnl_rttable_a2n(__u32 *id, const char *arg) |
| 553 |
{ |
| 554 |
static const char *cache; |
| 555 |
static unsigned long res; |
| 556 |
struct rtnl_hash_entry *entry; |
| 557 |
char *end; |
| 558 |
unsigned long i; |
| 559 |
|
| 560 |
if (cache && strcmp(cache, arg) == 0) { |
| 561 |
*id = res; |
| 562 |
return 0; |
| 563 |
} |
| 564 |
|
| 565 |
if (!rtnl_rttable_init) |
| 566 |
rtnl_rttable_initialize(); |
| 567 |
|
| 568 |
for (i = 0; i < 256; i++) { |
| 569 |
entry = rtnl_rttable_hash[i]; |
| 570 |
while (entry && strcmp(entry->name, arg)) |
| 571 |
entry = entry->next; |
| 572 |
if (entry) { |
| 573 |
cache = entry->name; |
| 574 |
res = entry->id; |
| 575 |
*id = res; |
| 576 |
return 0; |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
i = strtoul(arg, &end, 0); |
| 581 |
if (!end || end == arg || *end || i > RT_TABLE_MAX) |
| 582 |
return -1; |
| 583 |
*id = i; |
| 584 |
return 0; |
| 585 |
} |
| 586 |
|
| 587 |
|
| 588 |
static char *rtnl_rtdsfield_tab[256] = { |
| 589 |
"0", |
| 590 |
}; |
| 591 |
|
| 592 |
static int rtnl_rtdsfield_init; |
| 593 |
|
| 594 |
static void rtnl_rtdsfield_initialize(void) |
| 595 |
{ |
| 596 |
int ret; |
| 597 |
|
| 598 |
rtnl_rtdsfield_init = 1; |
| 599 |
ret = rtnl_tab_initialize(CONF_ETC_DIR "/rt_dsfield", |
| 600 |
rtnl_rtdsfield_tab, 256); |
| 601 |
if (ret == -ENOENT) |
| 602 |
rtnl_tab_initialize(CONF_USR_DIR "/rt_dsfield", |
| 603 |
rtnl_rtdsfield_tab, 256); |
| 604 |
} |
| 605 |
|
| 606 |
const char *rtnl_dsfield_n2a(int id, char *buf, int len) |
| 607 |
{ |
| 608 |
const char *name; |
| 609 |
|
| 610 |
if (id < 0 || id >= 256) { |
| 611 |
snprintf(buf, len, "%d", id); |
| 612 |
return buf; |
| 613 |
} |
| 614 |
if (!numeric) { |
| 615 |
name = rtnl_dsfield_get_name(id); |
| 616 |
if (name != NULL) |
| 617 |
return name; |
| 618 |
} |
| 619 |
snprintf(buf, len, "0x%02x", id); |
| 620 |
return buf; |
| 621 |
} |
| 622 |
|
| 623 |
const char *rtnl_dsfield_get_name(int id) |
| 624 |
{ |
| 625 |
if (id < 0 || id >= 256) |
| 626 |
return NULL; |
| 627 |
if (!rtnl_rtdsfield_tab[id]) { |
| 628 |
if (!rtnl_rtdsfield_init) |
| 629 |
rtnl_rtdsfield_initialize(); |
| 630 |
} |
| 631 |
return rtnl_rtdsfield_tab[id]; |
| 632 |
} |
| 633 |
|
| 634 |
const char *rtnl_dscp_n2a(int id, char *buf, int len) |
| 635 |
{ |
| 636 |
if (!numeric) { |
| 637 |
const char *name = rtnl_dsfield_get_name(id << 2); |
| 638 |
|
| 639 |
if (name != NULL) |
| 640 |
return name; |
| 641 |
} |
| 642 |
snprintf(buf, len, "%u", id); |
| 643 |
return buf; |
| 644 |
} |
| 645 |
|
| 646 |
int rtnl_dsfield_a2n(__u32 *id, const char *arg) |
| 647 |
{ |
| 648 |
static char *cache; |
| 649 |
static unsigned long res; |
| 650 |
char *end; |
| 651 |
int i; |
| 652 |
|
| 653 |
if (cache && strcmp(cache, arg) == 0) { |
| 654 |
*id = res; |
| 655 |
return 0; |
| 656 |
} |
| 657 |
|
| 658 |
if (!rtnl_rtdsfield_init) |
| 659 |
rtnl_rtdsfield_initialize(); |
| 660 |
|
| 661 |
for (i = 0; i < 256; i++) { |
| 662 |
if (rtnl_rtdsfield_tab[i] && |
| 663 |
strcmp(rtnl_rtdsfield_tab[i], arg) == 0) { |
| 664 |
cache = rtnl_rtdsfield_tab[i]; |
| 665 |
res = i; |
| 666 |
*id = res; |
| 667 |
return 0; |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
res = strtoul(arg, &end, 16); |
| 672 |
if (!end || end == arg || *end || res > 255) |
| 673 |
return -1; |
| 674 |
*id = res; |
| 675 |
return 0; |
| 676 |
} |
| 677 |
|
| 678 |
int rtnl_dscp_a2n(__u32 *id, const char *arg) |
| 679 |
{ |
| 680 |
if (get_u32(id, arg, 0) == 0) |
| 681 |
return 0; |
| 682 |
|
| 683 |
if (rtnl_dsfield_a2n(id, arg) != 0) |
| 684 |
return -1; |
| 685 |
/* Convert from DS field to DSCP */ |
| 686 |
*id >>= 2; |
| 687 |
|
| 688 |
return 0; |
| 689 |
} |
| 690 |
|
| 691 |
static struct rtnl_hash_entry dflt_group_entry = { |
| 692 |
.id = 0, .name = "default" |
| 693 |
}; |
| 694 |
|
| 695 |
static struct rtnl_hash_entry *rtnl_group_hash[256] = { |
| 696 |
[0] = &dflt_group_entry, |
| 697 |
}; |
| 698 |
|
| 699 |
static int rtnl_group_init; |
| 700 |
|
| 701 |
static void rtnl_group_initialize(void) |
| 702 |
{ |
| 703 |
int ret; |
| 704 |
|
| 705 |
rtnl_group_init = 1; |
| 706 |
ret = rtnl_hash_initialize(CONF_ETC_DIR "/group", |
| 707 |
rtnl_group_hash, 256); |
| 708 |
if (ret == -ENOENT) |
| 709 |
rtnl_hash_initialize(CONF_USR_DIR "/group", |
| 710 |
rtnl_group_hash, 256); |
| 711 |
} |
| 712 |
|
| 713 |
int rtnl_group_a2n(int *id, const char *arg) |
| 714 |
{ |
| 715 |
static const char *cache; |
| 716 |
static unsigned long res; |
| 717 |
struct rtnl_hash_entry *entry; |
| 718 |
char *end; |
| 719 |
int i; |
| 720 |
|
| 721 |
if (cache && strcmp(cache, arg) == 0) { |
| 722 |
*id = res; |
| 723 |
return 0; |
| 724 |
} |
| 725 |
|
| 726 |
if (!rtnl_group_init) |
| 727 |
rtnl_group_initialize(); |
| 728 |
|
| 729 |
for (i = 0; i < 256; i++) { |
| 730 |
entry = rtnl_group_hash[i]; |
| 731 |
while (entry && strcmp(entry->name, arg)) |
| 732 |
entry = entry->next; |
| 733 |
if (entry) { |
| 734 |
cache = entry->name; |
| 735 |
res = entry->id; |
| 736 |
*id = res; |
| 737 |
return 0; |
| 738 |
} |
| 739 |
} |
| 740 |
|
| 741 |
i = strtol(arg, &end, 0); |
| 742 |
if (!end || end == arg || *end || i < 0) |
| 743 |
return -1; |
| 744 |
*id = i; |
| 745 |
return 0; |
| 746 |
} |
| 747 |
|
| 748 |
const char *rtnl_group_n2a(int id, char *buf, int len) |
| 749 |
{ |
| 750 |
struct rtnl_hash_entry *entry; |
| 751 |
int i; |
| 752 |
|
| 753 |
if (!rtnl_group_init) |
| 754 |
rtnl_group_initialize(); |
| 755 |
|
| 756 |
for (i = 0; !numeric && i < 256; i++) { |
| 757 |
entry = rtnl_group_hash[i]; |
| 758 |
|
| 759 |
while (entry) { |
| 760 |
if (entry->id == id) |
| 761 |
return entry->name; |
| 762 |
entry = entry->next; |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
snprintf(buf, len, "%d", id); |
| 767 |
return buf; |
| 768 |
} |
| 769 |
|
| 770 |
static char *nl_proto_tab[256] = { |
| 771 |
[NETLINK_ROUTE] = "rtnl", |
| 772 |
[NETLINK_UNUSED] = "unused", |
| 773 |
[NETLINK_USERSOCK] = "usersock", |
| 774 |
[NETLINK_FIREWALL] = "fw", |
| 775 |
[NETLINK_SOCK_DIAG] = "tcpdiag", |
| 776 |
[NETLINK_NFLOG] = "nflog", |
| 777 |
[NETLINK_XFRM] = "xfrm", |
| 778 |
[NETLINK_SELINUX] = "selinux", |
| 779 |
[NETLINK_ISCSI] = "iscsi", |
| 780 |
[NETLINK_AUDIT] = "audit", |
| 781 |
[NETLINK_FIB_LOOKUP] = "fiblookup", |
| 782 |
[NETLINK_CONNECTOR] = "connector", |
| 783 |
[NETLINK_NETFILTER] = "nft", |
| 784 |
[NETLINK_IP6_FW] = "ip6fw", |
| 785 |
[NETLINK_DNRTMSG] = "dec-rt", |
| 786 |
[NETLINK_KOBJECT_UEVENT] = "uevent", |
| 787 |
[NETLINK_GENERIC] = "genl", |
| 788 |
[NETLINK_SCSITRANSPORT] = "scsi-trans", |
| 789 |
[NETLINK_ECRYPTFS] = "ecryptfs", |
| 790 |
[NETLINK_RDMA] = "rdma", |
| 791 |
[NETLINK_CRYPTO] = "crypto", |
| 792 |
}; |
| 793 |
|
| 794 |
static int nl_proto_init; |
| 795 |
|
| 796 |
static void nl_proto_initialize(void) |
| 797 |
{ |
| 798 |
int ret; |
| 799 |
|
| 800 |
nl_proto_init = 1; |
| 801 |
ret = rtnl_tab_initialize(CONF_ETC_DIR "/nl_protos", |
| 802 |
nl_proto_tab, 256); |
| 803 |
if (ret == -ENOENT) |
| 804 |
rtnl_tab_initialize(CONF_USR_DIR "/nl_protos", |
| 805 |
nl_proto_tab, 256); |
| 806 |
} |
| 807 |
|
| 808 |
const char *nl_proto_n2a(int id, char *buf, int len) |
| 809 |
{ |
| 810 |
if (id < 0 || id >= 256 || numeric) { |
| 811 |
snprintf(buf, len, "%d", id); |
| 812 |
return buf; |
| 813 |
} |
| 814 |
|
| 815 |
if (!nl_proto_init) |
| 816 |
nl_proto_initialize(); |
| 817 |
|
| 818 |
if (nl_proto_tab[id]) |
| 819 |
return nl_proto_tab[id]; |
| 820 |
|
| 821 |
snprintf(buf, len, "%u", id); |
| 822 |
return buf; |
| 823 |
} |
| 824 |
|
| 825 |
int nl_proto_a2n(__u32 *id, const char *arg) |
| 826 |
{ |
| 827 |
static char *cache; |
| 828 |
static unsigned long res; |
| 829 |
char *end; |
| 830 |
int i; |
| 831 |
|
| 832 |
if (cache && strcmp(cache, arg) == 0) { |
| 833 |
*id = res; |
| 834 |
return 0; |
| 835 |
} |
| 836 |
|
| 837 |
if (!nl_proto_init) |
| 838 |
nl_proto_initialize(); |
| 839 |
|
| 840 |
for (i = 0; i < 256; i++) { |
| 841 |
if (nl_proto_tab[i] && |
| 842 |
strcmp(nl_proto_tab[i], arg) == 0) { |
| 843 |
cache = nl_proto_tab[i]; |
| 844 |
res = i; |
| 845 |
*id = res; |
| 846 |
return 0; |
| 847 |
} |
| 848 |
} |
| 849 |
|
| 850 |
res = strtoul(arg, &end, 0); |
| 851 |
if (!end || end == arg || *end || res > 255) |
| 852 |
return -1; |
| 853 |
*id = res; |
| 854 |
return 0; |
| 855 |
} |
| 856 |
|
| 857 |
#define PROTODOWN_REASON_NUM_BITS 32 |
| 858 |
static char *protodown_reason_tab[PROTODOWN_REASON_NUM_BITS] = { |
| 859 |
}; |
| 860 |
|
| 861 |
static int protodown_reason_init; |
| 862 |
|
| 863 |
static void protodown_reason_initialize(void) |
| 864 |
{ |
| 865 |
protodown_reason_init = 1; |
| 866 |
|
| 867 |
rtnl_tab_initialize_dir("protodown_reasons.d", protodown_reason_tab, |
| 868 |
PROTODOWN_REASON_NUM_BITS); |
| 869 |
} |
| 870 |
|
| 871 |
int protodown_reason_n2a(int id, char *buf, int len) |
| 872 |
{ |
| 873 |
if (id < 0 || id >= PROTODOWN_REASON_NUM_BITS) |
| 874 |
return -1; |
| 875 |
|
| 876 |
if (numeric) { |
| 877 |
snprintf(buf, len, "%d", id); |
| 878 |
return 0; |
| 879 |
} |
| 880 |
|
| 881 |
if (!protodown_reason_init) |
| 882 |
protodown_reason_initialize(); |
| 883 |
|
| 884 |
if (protodown_reason_tab[id]) |
| 885 |
snprintf(buf, len, "%s", protodown_reason_tab[id]); |
| 886 |
else |
| 887 |
snprintf(buf, len, "%d", id); |
| 888 |
|
| 889 |
return 0; |
| 890 |
} |
| 891 |
|
| 892 |
int protodown_reason_a2n(__u32 *id, const char *arg) |
| 893 |
{ |
| 894 |
static char *cache; |
| 895 |
static unsigned long res; |
| 896 |
char *end; |
| 897 |
int i; |
| 898 |
|
| 899 |
if (cache && strcmp(cache, arg) == 0) { |
| 900 |
*id = res; |
| 901 |
return 0; |
| 902 |
} |
| 903 |
|
| 904 |
if (!protodown_reason_init) |
| 905 |
protodown_reason_initialize(); |
| 906 |
|
| 907 |
for (i = 0; i < PROTODOWN_REASON_NUM_BITS; i++) { |
| 908 |
if (protodown_reason_tab[i] && |
| 909 |
strcmp(protodown_reason_tab[i], arg) == 0) { |
| 910 |
cache = protodown_reason_tab[i]; |
| 911 |
res = i; |
| 912 |
*id = res; |
| 913 |
return 0; |
| 914 |
} |
| 915 |
} |
| 916 |
|
| 917 |
res = strtoul(arg, &end, 0); |
| 918 |
if (!end || end == arg || *end || res >= PROTODOWN_REASON_NUM_BITS) |
| 919 |
return -1; |
| 920 |
*id = res; |
| 921 |
return 0; |
| 922 |
} |