]> git.proxmox.com Git - mirror_iproute2.git/blob - lib/rt_names.c
Add support for rt_protos.d
[mirror_iproute2.git] / lib / rt_names.c
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>
19 #include <sys/socket.h>
20 #include <dirent.h>
21 #include <limits.h>
22
23 #include <asm/types.h>
24 #include <linux/rtnetlink.h>
25
26 #include "rt_names.h"
27 #include "utils.h"
28
29 #define NAME_MAX_LEN 512
30
31 struct rtnl_hash_entry {
32 struct rtnl_hash_entry *next;
33 const char *name;
34 unsigned int id;
35 };
36
37 static int fread_id_name(FILE *fp, int *id, char *namebuf)
38 {
39 char buf[NAME_MAX_LEN];
40
41 while (fgets(buf, sizeof(buf), fp)) {
42 char *p = buf;
43
44 while (*p == ' ' || *p == '\t')
45 p++;
46
47 if (*p == '#' || *p == '\n' || *p == 0)
48 continue;
49
50 if (sscanf(p, "0x%x %s\n", id, namebuf) != 2 &&
51 sscanf(p, "0x%x %s #", id, namebuf) != 2 &&
52 sscanf(p, "%d %s\n", id, namebuf) != 2 &&
53 sscanf(p, "%d %s #", id, namebuf) != 2) {
54 strcpy(namebuf, p);
55 return -1;
56 }
57 return 1;
58 }
59 return 0;
60 }
61
62 static void
63 rtnl_hash_initialize(const char *file, struct rtnl_hash_entry **hash, int size)
64 {
65 struct rtnl_hash_entry *entry;
66 FILE *fp;
67 int id;
68 char namebuf[NAME_MAX_LEN] = {0};
69 int ret;
70
71 fp = fopen(file, "r");
72 if (!fp)
73 return;
74
75 while ((ret = fread_id_name(fp, &id, &namebuf[0]))) {
76 if (ret == -1) {
77 fprintf(stderr, "Database %s is corrupted at %s\n",
78 file, namebuf);
79 fclose(fp);
80 return;
81 }
82
83 if (id < 0)
84 continue;
85
86 entry = malloc(sizeof(*entry));
87 entry->id = id;
88 entry->name = strdup(namebuf);
89 entry->next = hash[id & (size - 1)];
90 hash[id & (size - 1)] = entry;
91 }
92 fclose(fp);
93 }
94
95 static void rtnl_tab_initialize(const char *file, char **tab, int size)
96 {
97 FILE *fp;
98 int id;
99 char namebuf[NAME_MAX_LEN] = {0};
100 int ret;
101
102 fp = fopen(file, "r");
103 if (!fp)
104 return;
105
106 while ((ret = fread_id_name(fp, &id, &namebuf[0]))) {
107 if (ret == -1) {
108 fprintf(stderr, "Database %s is corrupted at %s\n",
109 file, namebuf);
110 fclose(fp);
111 return;
112 }
113 if (id < 0 || id > size)
114 continue;
115
116 tab[id] = strdup(namebuf);
117 }
118 fclose(fp);
119 }
120
121 static char *rtnl_rtprot_tab[256] = {
122 [RTPROT_UNSPEC] = "none",
123 [RTPROT_REDIRECT] = "redirect",
124 [RTPROT_KERNEL] = "kernel",
125 [RTPROT_BOOT] = "boot",
126 [RTPROT_STATIC] = "static",
127
128 [RTPROT_GATED] = "gated",
129 [RTPROT_RA] = "ra",
130 [RTPROT_MRT] = "mrt",
131 [RTPROT_ZEBRA] = "zebra",
132 [RTPROT_BIRD] = "bird",
133 [RTPROT_BABEL] = "babel",
134 [RTPROT_DNROUTED] = "dnrouted",
135 [RTPROT_XORP] = "xorp",
136 [RTPROT_NTK] = "ntk",
137 [RTPROT_DHCP] = "dhcp",
138 };
139
140
141 static int rtnl_rtprot_init;
142
143 static void rtnl_rtprot_initialize(void)
144 {
145 struct dirent *de;
146 DIR *d;
147
148 rtnl_rtprot_init = 1;
149 rtnl_tab_initialize(CONFDIR "/rt_protos",
150 rtnl_rtprot_tab, 256);
151
152 d = opendir(CONFDIR "/rt_protos.d");
153 if (!d)
154 return;
155
156 while ((de = readdir(d)) != NULL) {
157 char path[PATH_MAX];
158 size_t len;
159
160 if (*de->d_name == '.')
161 continue;
162
163 /* only consider filenames ending in '.conf' */
164 len = strlen(de->d_name);
165 if (len <= 5)
166 continue;
167 if (strcmp(de->d_name + len - 5, ".conf"))
168 continue;
169
170 snprintf(path, sizeof(path), CONFDIR "/rt_protos.d/%s",
171 de->d_name);
172 rtnl_tab_initialize(path, rtnl_rtprot_tab, 256);
173 }
174 closedir(d);
175 }
176
177 const char *rtnl_rtprot_n2a(int id, char *buf, int len)
178 {
179 if (id < 0 || id >= 256) {
180 snprintf(buf, len, "%u", id);
181 return buf;
182 }
183 if (!rtnl_rtprot_tab[id]) {
184 if (!rtnl_rtprot_init)
185 rtnl_rtprot_initialize();
186 }
187 if (rtnl_rtprot_tab[id])
188 return rtnl_rtprot_tab[id];
189 snprintf(buf, len, "%u", id);
190 return buf;
191 }
192
193 int rtnl_rtprot_a2n(__u32 *id, const char *arg)
194 {
195 static char *cache;
196 static unsigned long res;
197 char *end;
198 int i;
199
200 if (cache && strcmp(cache, arg) == 0) {
201 *id = res;
202 return 0;
203 }
204
205 if (!rtnl_rtprot_init)
206 rtnl_rtprot_initialize();
207
208 for (i = 0; i < 256; i++) {
209 if (rtnl_rtprot_tab[i] &&
210 strcmp(rtnl_rtprot_tab[i], arg) == 0) {
211 cache = rtnl_rtprot_tab[i];
212 res = i;
213 *id = res;
214 return 0;
215 }
216 }
217
218 res = strtoul(arg, &end, 0);
219 if (!end || end == arg || *end || res > 255)
220 return -1;
221 *id = res;
222 return 0;
223 }
224
225
226 static char *rtnl_rtscope_tab[256] = {
227 [RT_SCOPE_UNIVERSE] = "global",
228 [RT_SCOPE_NOWHERE] = "nowhere",
229 [RT_SCOPE_HOST] = "host",
230 [RT_SCOPE_LINK] = "link",
231 [RT_SCOPE_SITE] = "site",
232 };
233
234 static int rtnl_rtscope_init;
235
236 static void rtnl_rtscope_initialize(void)
237 {
238 rtnl_rtscope_init = 1;
239 rtnl_tab_initialize(CONFDIR "/rt_scopes",
240 rtnl_rtscope_tab, 256);
241 }
242
243 const char *rtnl_rtscope_n2a(int id, char *buf, int len)
244 {
245 if (id < 0 || id >= 256) {
246 snprintf(buf, len, "%d", id);
247 return buf;
248 }
249
250 if (!rtnl_rtscope_tab[id]) {
251 if (!rtnl_rtscope_init)
252 rtnl_rtscope_initialize();
253 }
254
255 if (rtnl_rtscope_tab[id])
256 return rtnl_rtscope_tab[id];
257
258 snprintf(buf, len, "%d", id);
259 return buf;
260 }
261
262 int rtnl_rtscope_a2n(__u32 *id, const char *arg)
263 {
264 static const char *cache;
265 static unsigned long res;
266 char *end;
267 int i;
268
269 if (cache && strcmp(cache, arg) == 0) {
270 *id = res;
271 return 0;
272 }
273
274 if (!rtnl_rtscope_init)
275 rtnl_rtscope_initialize();
276
277 for (i = 0; i < 256; i++) {
278 if (rtnl_rtscope_tab[i] &&
279 strcmp(rtnl_rtscope_tab[i], arg) == 0) {
280 cache = rtnl_rtscope_tab[i];
281 res = i;
282 *id = res;
283 return 0;
284 }
285 }
286
287 res = strtoul(arg, &end, 0);
288 if (!end || end == arg || *end || res > 255)
289 return -1;
290 *id = res;
291 return 0;
292 }
293
294
295 static char *rtnl_rtrealm_tab[256] = {
296 "unknown",
297 };
298
299 static int rtnl_rtrealm_init;
300
301 static void rtnl_rtrealm_initialize(void)
302 {
303 rtnl_rtrealm_init = 1;
304 rtnl_tab_initialize(CONFDIR "/rt_realms",
305 rtnl_rtrealm_tab, 256);
306 }
307
308 const char *rtnl_rtrealm_n2a(int id, char *buf, int len)
309 {
310 if (id < 0 || id >= 256) {
311 snprintf(buf, len, "%d", id);
312 return buf;
313 }
314 if (!rtnl_rtrealm_tab[id]) {
315 if (!rtnl_rtrealm_init)
316 rtnl_rtrealm_initialize();
317 }
318 if (rtnl_rtrealm_tab[id])
319 return rtnl_rtrealm_tab[id];
320 snprintf(buf, len, "%d", id);
321 return buf;
322 }
323
324
325 int rtnl_rtrealm_a2n(__u32 *id, const char *arg)
326 {
327 static char *cache;
328 static unsigned long res;
329 char *end;
330 int i;
331
332 if (cache && strcmp(cache, arg) == 0) {
333 *id = res;
334 return 0;
335 }
336
337 if (!rtnl_rtrealm_init)
338 rtnl_rtrealm_initialize();
339
340 for (i = 0; i < 256; i++) {
341 if (rtnl_rtrealm_tab[i] &&
342 strcmp(rtnl_rtrealm_tab[i], arg) == 0) {
343 cache = rtnl_rtrealm_tab[i];
344 res = i;
345 *id = res;
346 return 0;
347 }
348 }
349
350 res = strtoul(arg, &end, 0);
351 if (!end || end == arg || *end || res > 255)
352 return -1;
353 *id = res;
354 return 0;
355 }
356
357
358 static struct rtnl_hash_entry dflt_table_entry = { .name = "default" };
359 static struct rtnl_hash_entry main_table_entry = { .name = "main" };
360 static struct rtnl_hash_entry local_table_entry = { .name = "local" };
361
362 static struct rtnl_hash_entry *rtnl_rttable_hash[256] = {
363 [RT_TABLE_DEFAULT] = &dflt_table_entry,
364 [RT_TABLE_MAIN] = &main_table_entry,
365 [RT_TABLE_LOCAL] = &local_table_entry,
366 };
367
368 static int rtnl_rttable_init;
369
370 static void rtnl_rttable_initialize(void)
371 {
372 struct dirent *de;
373 DIR *d;
374 int i;
375
376 rtnl_rttable_init = 1;
377 for (i = 0; i < 256; i++) {
378 if (rtnl_rttable_hash[i])
379 rtnl_rttable_hash[i]->id = i;
380 }
381 rtnl_hash_initialize(CONFDIR "/rt_tables",
382 rtnl_rttable_hash, 256);
383
384 d = opendir(CONFDIR "/rt_tables.d");
385 if (!d)
386 return;
387
388 while ((de = readdir(d)) != NULL) {
389 char path[PATH_MAX];
390 size_t len;
391
392 if (*de->d_name == '.')
393 continue;
394
395 /* only consider filenames ending in '.conf' */
396 len = strlen(de->d_name);
397 if (len <= 5)
398 continue;
399 if (strcmp(de->d_name + len - 5, ".conf"))
400 continue;
401
402 snprintf(path, sizeof(path),
403 CONFDIR "/rt_tables.d/%s", de->d_name);
404 rtnl_hash_initialize(path, rtnl_rttable_hash, 256);
405 }
406 closedir(d);
407 }
408
409 const char *rtnl_rttable_n2a(__u32 id, char *buf, int len)
410 {
411 struct rtnl_hash_entry *entry;
412
413 if (id > RT_TABLE_MAX) {
414 snprintf(buf, len, "%u", id);
415 return buf;
416 }
417 if (!rtnl_rttable_init)
418 rtnl_rttable_initialize();
419 entry = rtnl_rttable_hash[id & 255];
420 while (entry && entry->id != id)
421 entry = entry->next;
422 if (entry)
423 return entry->name;
424 snprintf(buf, len, "%u", id);
425 return buf;
426 }
427
428 int rtnl_rttable_a2n(__u32 *id, const char *arg)
429 {
430 static const char *cache;
431 static unsigned long res;
432 struct rtnl_hash_entry *entry;
433 char *end;
434 __u32 i;
435
436 if (cache && strcmp(cache, arg) == 0) {
437 *id = res;
438 return 0;
439 }
440
441 if (!rtnl_rttable_init)
442 rtnl_rttable_initialize();
443
444 for (i = 0; i < 256; i++) {
445 entry = rtnl_rttable_hash[i];
446 while (entry && strcmp(entry->name, arg))
447 entry = entry->next;
448 if (entry) {
449 cache = entry->name;
450 res = entry->id;
451 *id = res;
452 return 0;
453 }
454 }
455
456 i = strtoul(arg, &end, 0);
457 if (!end || end == arg || *end || i > RT_TABLE_MAX)
458 return -1;
459 *id = i;
460 return 0;
461 }
462
463
464 static char *rtnl_rtdsfield_tab[256] = {
465 "0",
466 };
467
468 static int rtnl_rtdsfield_init;
469
470 static void rtnl_rtdsfield_initialize(void)
471 {
472 rtnl_rtdsfield_init = 1;
473 rtnl_tab_initialize(CONFDIR "/rt_dsfield",
474 rtnl_rtdsfield_tab, 256);
475 }
476
477 const char *rtnl_dsfield_n2a(int id, char *buf, int len)
478 {
479 if (id < 0 || id >= 256) {
480 snprintf(buf, len, "%d", id);
481 return buf;
482 }
483 if (!rtnl_rtdsfield_tab[id]) {
484 if (!rtnl_rtdsfield_init)
485 rtnl_rtdsfield_initialize();
486 }
487 if (rtnl_rtdsfield_tab[id])
488 return rtnl_rtdsfield_tab[id];
489 snprintf(buf, len, "0x%02x", id);
490 return buf;
491 }
492
493
494 int rtnl_dsfield_a2n(__u32 *id, const char *arg)
495 {
496 static char *cache;
497 static unsigned long res;
498 char *end;
499 int i;
500
501 if (cache && strcmp(cache, arg) == 0) {
502 *id = res;
503 return 0;
504 }
505
506 if (!rtnl_rtdsfield_init)
507 rtnl_rtdsfield_initialize();
508
509 for (i = 0; i < 256; i++) {
510 if (rtnl_rtdsfield_tab[i] &&
511 strcmp(rtnl_rtdsfield_tab[i], arg) == 0) {
512 cache = rtnl_rtdsfield_tab[i];
513 res = i;
514 *id = res;
515 return 0;
516 }
517 }
518
519 res = strtoul(arg, &end, 16);
520 if (!end || end == arg || *end || res > 255)
521 return -1;
522 *id = res;
523 return 0;
524 }
525
526
527 static struct rtnl_hash_entry dflt_group_entry = {
528 .id = 0, .name = "default"
529 };
530
531 static struct rtnl_hash_entry *rtnl_group_hash[256] = {
532 [0] = &dflt_group_entry,
533 };
534
535 static int rtnl_group_init;
536
537 static void rtnl_group_initialize(void)
538 {
539 rtnl_group_init = 1;
540 rtnl_hash_initialize(CONFDIR "/group",
541 rtnl_group_hash, 256);
542 }
543
544 int rtnl_group_a2n(int *id, const char *arg)
545 {
546 static const char *cache;
547 static unsigned long res;
548 struct rtnl_hash_entry *entry;
549 char *end;
550 int i;
551
552 if (cache && strcmp(cache, arg) == 0) {
553 *id = res;
554 return 0;
555 }
556
557 if (!rtnl_group_init)
558 rtnl_group_initialize();
559
560 for (i = 0; i < 256; i++) {
561 entry = rtnl_group_hash[i];
562 while (entry && strcmp(entry->name, arg))
563 entry = entry->next;
564 if (entry) {
565 cache = entry->name;
566 res = entry->id;
567 *id = res;
568 return 0;
569 }
570 }
571
572 i = strtol(arg, &end, 0);
573 if (!end || end == arg || *end || i < 0)
574 return -1;
575 *id = i;
576 return 0;
577 }
578
579 const char *rtnl_group_n2a(int id, char *buf, int len)
580 {
581 struct rtnl_hash_entry *entry;
582 int i;
583
584 if (!rtnl_group_init)
585 rtnl_group_initialize();
586
587 for (i = 0; i < 256; i++) {
588 entry = rtnl_group_hash[i];
589
590 while (entry) {
591 if (entry->id == id)
592 return entry->name;
593 entry = entry->next;
594 }
595 }
596
597 snprintf(buf, len, "%d", id);
598 return buf;
599 }
600
601 static char *nl_proto_tab[256] = {
602 [NETLINK_ROUTE] = "rtnl",
603 [NETLINK_UNUSED] = "unused",
604 [NETLINK_USERSOCK] = "usersock",
605 [NETLINK_FIREWALL] = "fw",
606 [NETLINK_SOCK_DIAG] = "tcpdiag",
607 [NETLINK_NFLOG] = "nflog",
608 [NETLINK_XFRM] = "xfrm",
609 [NETLINK_SELINUX] = "selinux",
610 [NETLINK_ISCSI] = "iscsi",
611 [NETLINK_AUDIT] = "audit",
612 [NETLINK_FIB_LOOKUP] = "fiblookup",
613 [NETLINK_CONNECTOR] = "connector",
614 [NETLINK_NETFILTER] = "nft",
615 [NETLINK_IP6_FW] = "ip6fw",
616 [NETLINK_DNRTMSG] = "dec-rt",
617 [NETLINK_KOBJECT_UEVENT] = "uevent",
618 [NETLINK_GENERIC] = "genl",
619 [NETLINK_SCSITRANSPORT] = "scsi-trans",
620 [NETLINK_ECRYPTFS] = "ecryptfs",
621 [NETLINK_RDMA] = "rdma",
622 [NETLINK_CRYPTO] = "crypto",
623 };
624
625 static int nl_proto_init;
626
627 static void nl_proto_initialize(void)
628 {
629 nl_proto_init = 1;
630 rtnl_tab_initialize(CONFDIR "/nl_protos",
631 nl_proto_tab, 256);
632 }
633
634 const char *nl_proto_n2a(int id, char *buf, int len)
635 {
636 if (id < 0 || id >= 256) {
637 snprintf(buf, len, "%u", id);
638 return buf;
639 }
640
641 if (!nl_proto_init)
642 nl_proto_initialize();
643
644 if (nl_proto_tab[id])
645 return nl_proto_tab[id];
646
647 snprintf(buf, len, "%u", id);
648 return buf;
649 }
650
651 int nl_proto_a2n(__u32 *id, const char *arg)
652 {
653 static char *cache;
654 static unsigned long res;
655 char *end;
656 int i;
657
658 if (cache && strcmp(cache, arg) == 0) {
659 *id = res;
660 return 0;
661 }
662
663 if (!nl_proto_init)
664 nl_proto_initialize();
665
666 for (i = 0; i < 256; i++) {
667 if (nl_proto_tab[i] &&
668 strcmp(nl_proto_tab[i], arg) == 0) {
669 cache = nl_proto_tab[i];
670 res = i;
671 *id = res;
672 return 0;
673 }
674 }
675
676 res = strtoul(arg, &end, 0);
677 if (!end || end == arg || *end || res > 255)
678 return -1;
679 *id = res;
680 return 0;
681 }