blob: 911e4d2752b1c7e2441c40bf762d36c4858e055b [file] [log] [blame]
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +00001/*
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!shemmingerb88fd9f2004-06-08 23:50:43 +000019#include <sys/socket.h>
20
21#include <asm/types.h>
22#include <linux/rtnetlink.h>
23
24#include "rt_names.h"
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000025
Christoph J. Thompsonfb721292012-03-01 06:44:43 +000026#ifndef CONFDIR
27#define CONFDIR "/etc/iproute2"
28#endif
29
Patrick McHardy9c47d872006-08-11 00:14:50 +020030struct rtnl_hash_entry {
31 struct rtnl_hash_entry *next;
Stephen Hemminger46ac8a52013-02-12 11:39:07 -080032 const char * name;
Patrick McHardy9c47d872006-08-11 00:14:50 +020033 unsigned int id;
34};
35
36static void
Stephen Hemminger46ac8a52013-02-12 11:39:07 -080037rtnl_hash_initialize(const char *file, struct rtnl_hash_entry **hash, int size)
Patrick McHardy9c47d872006-08-11 00:14:50 +020038{
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 Jarosch97c13582011-10-03 05:22:42 +000061 fclose(fp);
Patrick McHardy9c47d872006-08-11 00:14:50 +020062 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 Hemminger46ac8a52013-02-12 11:39:07 -080076static void rtnl_tab_initialize(const char *file, char **tab, int size)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +000077{
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 Jarosch97c13582011-10-03 05:22:42 +000099 fclose(fp);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000100 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!shemmingeraba5acd2004-04-15 20:56:59 +0000111static char * rtnl_rtprot_tab[256] = {
osdl.org!shemmingerb88fd9f2004-06-08 23:50:43 +0000112 [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",
shemminger40b6c622006-03-10 23:44:04 +0000125 [RTPROT_NTK] = "ntk",
Stephen Hemminger1e659af2009-03-18 13:33:12 -0700126 [RTPROT_DHCP] = "dhcp",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000127};
128
129
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000130static int rtnl_rtprot_init;
131
132static void rtnl_rtprot_initialize(void)
133{
134 rtnl_rtprot_init = 1;
Christoph J. Thompsonfb721292012-03-01 06:44:43 +0000135 rtnl_tab_initialize(CONFDIR "/rt_protos",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000136 rtnl_rtprot_tab, 256);
137}
138
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800139const char * rtnl_rtprot_n2a(int id, char *buf, int len)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000140{
141 if (id<0 || id>=256) {
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800142 snprintf(buf, len, "%u", id);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000143 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 Hemminger46ac8a52013-02-12 11:39:07 -0800151 snprintf(buf, len, "%u", id);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000152 return buf;
153}
154
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800155int rtnl_rtprot_a2n(__u32 *id, const char *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000156{
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!shemmingeraba5acd2004-04-15 20:56:59 +0000188static char * rtnl_rtscope_tab[256] = {
189 "global",
190};
191
192static int rtnl_rtscope_init;
193
194static void rtnl_rtscope_initialize(void)
195{
196 rtnl_rtscope_init = 1;
Masatake YAMATO58ed50e2014-03-07 19:21:36 +0900197 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. Thompsonfb721292012-03-01 06:44:43 +0000201 rtnl_tab_initialize(CONFDIR "/rt_scopes",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000202 rtnl_rtscope_tab, 256);
203}
204
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800205const char *rtnl_rtscope_n2a(int id, char *buf, int len)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000206{
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 Hemminger46ac8a52013-02-12 11:39:07 -0800221int rtnl_rtscope_a2n(__u32 *id, const char *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000222{
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800223 static const char *cache = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000224 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!shemmingeraba5acd2004-04-15 20:56:59 +0000254static char * rtnl_rtrealm_tab[256] = {
255 "unknown",
256};
257
258static int rtnl_rtrealm_init;
259
260static void rtnl_rtrealm_initialize(void)
261{
262 rtnl_rtrealm_init = 1;
Christoph J. Thompsonfb721292012-03-01 06:44:43 +0000263 rtnl_tab_initialize(CONFDIR "/rt_realms",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000264 rtnl_rtrealm_tab, 256);
265}
266
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800267const char *rtnl_rtrealm_n2a(int id, char *buf, int len)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000268{
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 Hemminger46ac8a52013-02-12 11:39:07 -0800284int rtnl_rtrealm_a2n(__u32 *id, const char *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000285{
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 YAMATO81ebcb22014-03-07 19:21:37 +0900317static struct rtnl_hash_entry dflt_table_entry = { .name = "default" };
318static struct rtnl_hash_entry main_table_entry = { .name = "main" };
319static struct rtnl_hash_entry local_table_entry = { .name = "local" };
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000320
Patrick McHardy9c47d872006-08-11 00:14:50 +0200321static struct rtnl_hash_entry * rtnl_rttable_hash[256] = {
Masatake YAMATO81ebcb22014-03-07 19:21:37 +0900322 [RT_TABLE_DEFAULT] = &dflt_table_entry,
323 [RT_TABLE_MAIN] = &main_table_entry,
324 [RT_TABLE_LOCAL] = &local_table_entry,
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000325};
326
327static int rtnl_rttable_init;
328
329static void rtnl_rttable_initialize(void)
330{
Masatake YAMATO81ebcb22014-03-07 19:21:37 +0900331 int i;
332
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000333 rtnl_rttable_init = 1;
Masatake YAMATO81ebcb22014-03-07 19:21:37 +0900334 for (i = 0; i < 256; i++) {
335 if (rtnl_rttable_hash[i])
336 rtnl_rttable_hash[i]->id = i;
337 }
Christoph J. Thompsonfb721292012-03-01 06:44:43 +0000338 rtnl_hash_initialize(CONFDIR "/rt_tables",
Patrick McHardy9c47d872006-08-11 00:14:50 +0200339 rtnl_rttable_hash, 256);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000340}
341
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800342const char * rtnl_rttable_n2a(__u32 id, char *buf, int len)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000343{
Patrick McHardy9c47d872006-08-11 00:14:50 +0200344 struct rtnl_hash_entry *entry;
345
Boian Bonev887a5d02006-11-27 14:12:31 -0800346 if (id > RT_TABLE_MAX) {
Patrick McHardy9c47d872006-08-11 00:14:50 +0200347 snprintf(buf, len, "%u", id);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000348 return buf;
349 }
Patrick McHardy9c47d872006-08-11 00:14:50 +0200350 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!shemmingeraba5acd2004-04-15 20:56:59 +0000358 return buf;
359}
360
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800361int rtnl_rttable_a2n(__u32 *id, const char *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000362{
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800363 static const char *cache = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000364 static unsigned long res;
Patrick McHardy9c47d872006-08-11 00:14:50 +0200365 struct rtnl_hash_entry *entry;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000366 char *end;
Patrick McHardy34e95642006-08-11 00:14:51 +0200367 __u32 i;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000368
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 McHardy9c47d872006-08-11 00:14:50 +0200378 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!shemmingeraba5acd2004-04-15 20:56:59 +0000384 *id = res;
385 return 0;
386 }
387 }
388
389 i = strtoul(arg, &end, 0);
Patrick McHardy9c47d872006-08-11 00:14:50 +0200390 if (!end || end == arg || *end || i > RT_TABLE_MAX)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000391 return -1;
392 *id = i;
393 return 0;
394}
395
396
397static char * rtnl_rtdsfield_tab[256] = {
398 "0",
399};
400
401static int rtnl_rtdsfield_init;
402
403static void rtnl_rtdsfield_initialize(void)
404{
405 rtnl_rtdsfield_init = 1;
Christoph J. Thompsonfb721292012-03-01 06:44:43 +0000406 rtnl_tab_initialize(CONFDIR "/rt_dsfield",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000407 rtnl_rtdsfield_tab, 256);
408}
409
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800410const char *rtnl_dsfield_n2a(int id, char *buf, int len)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000411{
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 Hemminger46ac8a52013-02-12 11:39:07 -0800427int rtnl_dsfield_a2n(__u32 *id, const char *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000428{
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 Dogaruac694c32011-02-02 20:23:40 +0200459
460static struct rtnl_hash_entry dflt_group_entry = { .id = 0, .name = "default" };
461
462static struct rtnl_hash_entry * rtnl_group_hash[256] = {
463 [0] = &dflt_group_entry,
464};
465
466static int rtnl_group_init;
467
468static 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 Hemminger46ac8a52013-02-12 11:39:07 -0800475int rtnl_group_a2n(int *id, const char *arg)
Vlad Dogaruac694c32011-02-02 20:23:40 +0200476{
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800477 static const char *cache = NULL;
Vlad Dogaruac694c32011-02-02 20:23:40 +0200478 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 Tomanekc4fdf752013-08-03 14:20:53 +0200509
510const 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}