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