blob: 67e4c49f44d28759af4fee5603a52402bd3c1049 [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;
197 rtnl_rtscope_tab[255] = "nowhere";
198 rtnl_rtscope_tab[254] = "host";
199 rtnl_rtscope_tab[253] = "link";
200 rtnl_rtscope_tab[200] = "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
Patrick McHardy9c47d872006-08-11 00:14:50 +0200317static struct rtnl_hash_entry dflt_table_entry = { .id = 253, .name = "default" };
318static struct rtnl_hash_entry main_table_entry = { .id = 254, .name = "main" };
319static struct rtnl_hash_entry local_table_entry = { .id = 255, .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] = {
322 [253] = &dflt_table_entry,
323 [254] = &main_table_entry,
324 [255] = &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{
331 rtnl_rttable_init = 1;
Christoph J. Thompsonfb721292012-03-01 06:44:43 +0000332 rtnl_hash_initialize(CONFDIR "/rt_tables",
Patrick McHardy9c47d872006-08-11 00:14:50 +0200333 rtnl_rttable_hash, 256);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000334}
335
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800336const char * rtnl_rttable_n2a(__u32 id, char *buf, int len)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000337{
Patrick McHardy9c47d872006-08-11 00:14:50 +0200338 struct rtnl_hash_entry *entry;
339
Boian Bonev887a5d02006-11-27 14:12:31 -0800340 if (id > RT_TABLE_MAX) {
Patrick McHardy9c47d872006-08-11 00:14:50 +0200341 snprintf(buf, len, "%u", id);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000342 return buf;
343 }
Patrick McHardy9c47d872006-08-11 00:14:50 +0200344 if (!rtnl_rttable_init)
345 rtnl_rttable_initialize();
346 entry = rtnl_rttable_hash[id & 255];
347 while (entry && entry->id != id)
348 entry = entry->next;
349 if (entry)
350 return entry->name;
351 snprintf(buf, len, "%u", id);
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000352 return buf;
353}
354
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800355int rtnl_rttable_a2n(__u32 *id, const char *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000356{
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800357 static const char *cache = NULL;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000358 static unsigned long res;
Patrick McHardy9c47d872006-08-11 00:14:50 +0200359 struct rtnl_hash_entry *entry;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000360 char *end;
Patrick McHardy34e95642006-08-11 00:14:51 +0200361 __u32 i;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000362
363 if (cache && strcmp(cache, arg) == 0) {
364 *id = res;
365 return 0;
366 }
367
368 if (!rtnl_rttable_init)
369 rtnl_rttable_initialize();
370
371 for (i=0; i<256; i++) {
Patrick McHardy9c47d872006-08-11 00:14:50 +0200372 entry = rtnl_rttable_hash[i];
373 while (entry && strcmp(entry->name, arg))
374 entry = entry->next;
375 if (entry) {
376 cache = entry->name;
377 res = entry->id;
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000378 *id = res;
379 return 0;
380 }
381 }
382
383 i = strtoul(arg, &end, 0);
Patrick McHardy9c47d872006-08-11 00:14:50 +0200384 if (!end || end == arg || *end || i > RT_TABLE_MAX)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000385 return -1;
386 *id = i;
387 return 0;
388}
389
390
391static char * rtnl_rtdsfield_tab[256] = {
392 "0",
393};
394
395static int rtnl_rtdsfield_init;
396
397static void rtnl_rtdsfield_initialize(void)
398{
399 rtnl_rtdsfield_init = 1;
Christoph J. Thompsonfb721292012-03-01 06:44:43 +0000400 rtnl_tab_initialize(CONFDIR "/rt_dsfield",
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000401 rtnl_rtdsfield_tab, 256);
402}
403
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800404const char *rtnl_dsfield_n2a(int id, char *buf, int len)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000405{
406 if (id<0 || id>=256) {
407 snprintf(buf, len, "%d", id);
408 return buf;
409 }
410 if (!rtnl_rtdsfield_tab[id]) {
411 if (!rtnl_rtdsfield_init)
412 rtnl_rtdsfield_initialize();
413 }
414 if (rtnl_rtdsfield_tab[id])
415 return rtnl_rtdsfield_tab[id];
416 snprintf(buf, len, "0x%02x", id);
417 return buf;
418}
419
420
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800421int rtnl_dsfield_a2n(__u32 *id, const char *arg)
osdl.org!shemmingeraba5acd2004-04-15 20:56:59 +0000422{
423 static char *cache = NULL;
424 static unsigned long res;
425 char *end;
426 int i;
427
428 if (cache && strcmp(cache, arg) == 0) {
429 *id = res;
430 return 0;
431 }
432
433 if (!rtnl_rtdsfield_init)
434 rtnl_rtdsfield_initialize();
435
436 for (i=0; i<256; i++) {
437 if (rtnl_rtdsfield_tab[i] &&
438 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
439 cache = rtnl_rtdsfield_tab[i];
440 res = i;
441 *id = res;
442 return 0;
443 }
444 }
445
446 res = strtoul(arg, &end, 16);
447 if (!end || end == arg || *end || res > 255)
448 return -1;
449 *id = res;
450 return 0;
451}
452
Vlad Dogaruac694c32011-02-02 20:23:40 +0200453
454static struct rtnl_hash_entry dflt_group_entry = { .id = 0, .name = "default" };
455
456static struct rtnl_hash_entry * rtnl_group_hash[256] = {
457 [0] = &dflt_group_entry,
458};
459
460static int rtnl_group_init;
461
462static void rtnl_group_initialize(void)
463{
464 rtnl_group_init = 1;
465 rtnl_hash_initialize("/etc/iproute2/group",
466 rtnl_group_hash, 256);
467}
468
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800469int rtnl_group_a2n(int *id, const char *arg)
Vlad Dogaruac694c32011-02-02 20:23:40 +0200470{
Stephen Hemminger46ac8a52013-02-12 11:39:07 -0800471 static const char *cache = NULL;
Vlad Dogaruac694c32011-02-02 20:23:40 +0200472 static unsigned long res;
473 struct rtnl_hash_entry *entry;
474 char *end;
475 int i;
476
477 if (cache && strcmp(cache, arg) == 0) {
478 *id = res;
479 return 0;
480 }
481
482 if (!rtnl_group_init)
483 rtnl_group_initialize();
484
485 for (i=0; i<256; i++) {
486 entry = rtnl_group_hash[i];
487 while (entry && strcmp(entry->name, arg))
488 entry = entry->next;
489 if (entry) {
490 cache = entry->name;
491 res = entry->id;
492 *id = res;
493 return 0;
494 }
495 }
496
497 i = strtol(arg, &end, 0);
498 if (!end || end == arg || *end || i < 0)
499 return -1;
500 *id = i;
501 return 0;
502}
Stefan Tomanekc4fdf752013-08-03 14:20:53 +0200503
504const char *rtnl_group_n2a(int id, char *buf, int len)
505{
506 struct rtnl_hash_entry *entry;
507 int i;
508
509 if (!rtnl_group_init)
510 rtnl_group_initialize();
511
512 for (i=0; i<256; i++) {
513 entry = rtnl_group_hash[i];
514 if (entry && entry->id == id) {
515 return entry->name;
516 }
517 }
518
519 snprintf(buf, len, "%d", id);
520 return buf;
521}