1 From 485fcc89b99eae9cc7501eaff344b104e52ab7bf Mon Sep 17 00:00:00 2001
2 From: Jo-Philipp Wich <jo@mein.io>
3 Date: Mon, 26 Sep 2016 17:48:22 +0200
4 Subject: [PATCH] iproute: properly support high routing table IDs
6 The Linux kernel uses two distinct fields to denote the routing table ID in
7 use by network routes; the 8 bit `rtm_table` member of `struct rtmsg` and the
8 32 bit `RTA_TABLE` netlink attribute.
10 If a routing table ID is larger than 255, the `RT_TABLE` attribute must be used
11 and the `rtm_table` field has to be set to the special `RT_TABLE_UNSPEC` value.
14 - switches the *_n2a() and *_a2n() functions of rt_names.c to use dynamically
15 sized, name-sorted arrays instead of fixed arrays limited to 1024 slots in
16 order to support IDs up to 65535
17 - adds proper handling of high table IDs to iprule.c and iproute.c when
18 adding, removing and dumping ip rules and network routes
20 After this change, the Busybox ip applet fully supports IP rules with high ID
21 numbers, using the same logic as the full iproute2.
23 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
25 networking/libiproute/iproute.c | 75 ++++++++------
26 networking/libiproute/iprule.c | 4 +-
27 networking/libiproute/rt_names.c | 204 +++++++++++++++++++++++----------------
28 3 files changed, 169 insertions(+), 114 deletions(-)
30 --- a/networking/libiproute/iproute.c
31 +++ b/networking/libiproute/iproute.c
32 @@ -66,6 +66,7 @@ static int FAST_FUNC print_route(const s
38 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
39 fprintf(stderr, "Not a route: %08x %08x %08x\n",
40 @@ -83,34 +84,6 @@ static int FAST_FUNC print_route(const s
41 else if (r->rtm_family == AF_INET)
44 - if (r->rtm_family == AF_INET6) {
46 - if (G_filter.tb < 0) {
47 - if (!(r->rtm_flags & RTM_F_CLONED)) {
51 - if (r->rtm_flags & RTM_F_CLONED) {
54 - if (G_filter.tb == RT_TABLE_LOCAL) {
55 - if (r->rtm_type != RTN_LOCAL) {
58 - } else if (G_filter.tb == RT_TABLE_MAIN) {
59 - if (r->rtm_type == RTN_LOCAL) {
68 - if (G_filter.tb > 0 && G_filter.tb != r->rtm_table) {
72 if (G_filter.rdst.family
73 && (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len)
75 @@ -141,6 +114,37 @@ static int FAST_FUNC print_route(const s
76 memset(&dst, 0, sizeof(dst));
77 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
79 + rtable = tb[RTA_TABLE] ? *(uint32_t*)RTA_DATA(tb[RTA_TABLE]) : r->rtm_table;
82 + if (r->rtm_family == AF_INET6) {
83 + if (G_filter.tb < 0) {
84 + if (!(r->rtm_flags & RTM_F_CLONED)) {
88 + if (r->rtm_flags & RTM_F_CLONED) {
91 + if (G_filter.tb == RT_TABLE_LOCAL) {
92 + if (r->rtm_type != RTN_LOCAL) {
95 + } else if (G_filter.tb == RT_TABLE_MAIN) {
96 + if (r->rtm_type == RTN_LOCAL) {
99 + } else if (G_filter.tb != rtable) {
104 + if (G_filter.tb != rtable) {
111 src.bitlen = r->rtm_src_len;
112 src.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
113 @@ -349,7 +353,9 @@ IF_FEATURE_IP_RULE(ARG_table,)
115 smalluint scope_ok = 0;
118 +#if ENABLE_FEATURE_IP_RULE
119 + uint32_t rtable = 0;
121 memset(&req, 0, sizeof(req));
123 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
124 @@ -419,7 +425,7 @@ IF_FEATURE_IP_RULE(ARG_table,)
126 if (rtnl_rttable_a2n(&tid, *argv))
127 invarg_1_to_2(*argv, "table");
128 - req.r.rtm_table = tid;
131 } else if (arg == ARG_dev || arg == ARG_oif) {
133 @@ -475,6 +481,15 @@ IF_FEATURE_IP_RULE(ARG_table,)
137 +#if ENABLE_FEATURE_IP_RULE
138 + if (rtable >= 256) {
139 + addattr32(&req.n, sizeof(req), RTA_TABLE, rtable);
140 + req.r.rtm_table = RT_TABLE_UNSPEC;
141 + } else if (rtable > 0) {
142 + req.r.rtm_table = rtable;
146 if (mxrta->rta_len > RTA_LENGTH(0)) {
148 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
149 --- a/networking/libiproute/iprule.c
150 +++ b/networking/libiproute/iprule.c
151 @@ -114,7 +114,9 @@ static int FAST_FUNC print_rule(const st
152 printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
157 + printf("lookup %s ", rtnl_rttable_n2a(*(uint32_t*)RTA_DATA(tb[RTA_TABLE])));
158 + else if (r->rtm_table)
159 printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table));
162 --- a/networking/libiproute/rt_names.c
163 +++ b/networking/libiproute/rt_names.c
165 #include "rt_names.h"
167 #define CONFDIR CONFIG_FEATURE_IP_ROUTE_DIR
168 +#define RT_TABLE_MAX 65535
170 +struct rtnl_tab_entry {
175 typedef struct rtnl_tab_t {
176 - const char *cached_str;
177 - unsigned cached_result;
178 - /* upstream version switched to a hash table and removed
179 - * id < 256 limit. For now bbox bumps this array size from 256
180 - * to 1024. If you plan to change this to a hash table,
181 - * consider merging several hash tables we have (for example,
182 - * awk has resizable one!
184 -#define RT_TABLE_MAX 1023
185 - const char *tab[RT_TABLE_MAX+1];
186 + struct rtnl_tab_entry *tab;
190 -static void rtnl_tab_initialize(const char *file, const char **tab)
191 +static int tabcmp(const void *p1, const void *p2)
193 + const struct rtnl_tab_entry *e1 = p1;
194 + const struct rtnl_tab_entry *e2 = p2;
195 + return strcmp(e1->name, e2->name);
198 +static void rtnl_tab_initialize(const char *file, rtnl_tab_t *tab)
201 char fullname[sizeof(CONFDIR"/rt_dsfield") + 8];
202 @@ -40,34 +45,42 @@ static void rtnl_tab_initialize(const ch
203 file, parser->lineno);
206 - tab[id] = xstrdup(token[1]);
208 + tab->tab = xrealloc(tab->tab, (tab->length + 1) * sizeof(*tab->tab));
209 + tab->tab[tab->length].id = id;
210 + tab->tab[tab->length].name = xstrdup(token[1]);
213 config_close(parser);
214 + qsort(tab->tab, tab->length, sizeof(*tab->tab), tabcmp);
217 static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
221 - if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
222 - *id = tab->cached_result;
227 + ssize_t r = tab->length - 1;
232 + m = l + (r - l) / 2;
233 + delta = strcmp(tab->tab[m].name, arg);
235 - for (i = 0; i <= RT_TABLE_MAX; i++) {
237 - && strcmp(tab->tab[i], arg) == 0
239 - tab->cached_str = tab->tab[i];
240 - tab->cached_result = i;
243 + *id = tab->tab[m].id;
245 + } else if (delta < 0) {
252 i = bb_strtou(arg, NULL, base);
253 if (i > RT_TABLE_MAX)
259 @@ -77,40 +90,39 @@ static rtnl_tab_t *rtnl_rtprot_tab;
261 static void rtnl_rtprot_initialize(void)
263 - static const char *const init_tab[] = {
277 + static const struct rtnl_tab_entry init_tab[] = {
292 rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab));
293 + rtnl_rtprot_tab->tab = xzalloc(sizeof(init_tab));
294 + rtnl_rtprot_tab->length = sizeof(init_tab) / sizeof(init_tab[0]);
295 memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
296 - rtnl_tab_initialize("protos", rtnl_rtprot_tab->tab);
297 + rtnl_tab_initialize("protos", rtnl_rtprot_tab);
301 const char* FAST_FUNC rtnl_rtprot_n2a(int id)
303 - if (id < 0 || id > RT_TABLE_MAX) {
308 rtnl_rtprot_initialize();
310 - if (rtnl_rtprot_tab->tab[id])
311 - return rtnl_rtprot_tab->tab[id];
312 + for (i = 0; i < rtnl_rtprot_tab->length; i++)
313 + if (rtnl_rtprot_tab->tab[i].id == id)
314 + return rtnl_rtprot_tab->tab[i].name;
319 @@ -126,27 +138,33 @@ static rtnl_tab_t *rtnl_rtscope_tab;
321 static void rtnl_rtscope_initialize(void)
323 + static const struct rtnl_tab_entry init_tab[] = {
331 if (rtnl_rtscope_tab)
333 rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
334 - rtnl_rtscope_tab->tab[0] = "global";
335 - rtnl_rtscope_tab->tab[255] = "nowhere";
336 - rtnl_rtscope_tab->tab[254] = "host";
337 - rtnl_rtscope_tab->tab[253] = "link";
338 - rtnl_rtscope_tab->tab[200] = "site";
339 - rtnl_tab_initialize("scopes", rtnl_rtscope_tab->tab);
340 + rtnl_rtscope_tab->tab = xzalloc(sizeof(init_tab));
341 + rtnl_rtscope_tab->length = sizeof(init_tab) / sizeof(init_tab[0]);
342 + memcpy(rtnl_rtscope_tab->tab, init_tab, sizeof(init_tab));
343 + rtnl_tab_initialize("scopes", rtnl_rtscope_tab);
346 const char* FAST_FUNC rtnl_rtscope_n2a(int id)
348 - if (id < 0 || id > RT_TABLE_MAX) {
353 rtnl_rtscope_initialize();
355 - if (rtnl_rtscope_tab->tab[id])
356 - return rtnl_rtscope_tab->tab[id];
357 + for (i = 0; i < rtnl_rtscope_tab->length; i++)
358 + if (rtnl_rtscope_tab->tab[i].id == id)
359 + return rtnl_rtscope_tab->tab[i].name;
364 @@ -161,10 +179,17 @@ static rtnl_tab_t *rtnl_rtrealm_tab;
366 static void rtnl_rtrealm_initialize(void)
368 - if (rtnl_rtrealm_tab) return;
369 + static const struct rtnl_tab_entry init_tab[] = {
373 + if (rtnl_rtrealm_tab)
375 rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
376 - rtnl_rtrealm_tab->tab[0] = "unknown";
377 - rtnl_tab_initialize("realms", rtnl_rtrealm_tab->tab);
378 + rtnl_rtrealm_tab->tab = xzalloc(sizeof(init_tab));
379 + rtnl_rtrealm_tab->length = sizeof(init_tab) / sizeof(init_tab[0]);
380 + memcpy(rtnl_rtrealm_tab->tab, init_tab, sizeof(init_tab));
381 + rtnl_tab_initialize("realms", rtnl_rtrealm_tab);
384 int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg)
385 @@ -176,14 +201,14 @@ int FAST_FUNC rtnl_rtrealm_a2n(uint32_t
386 #if ENABLE_FEATURE_IP_RULE
387 const char* FAST_FUNC rtnl_rtrealm_n2a(int id)
389 - if (id < 0 || id > RT_TABLE_MAX) {
394 rtnl_rtrealm_initialize();
396 - if (rtnl_rtrealm_tab->tab[id])
397 - return rtnl_rtrealm_tab->tab[id];
398 + for (i = 0; i < rtnl_rtrealm_tab->length; i++)
399 + if (rtnl_rtrealm_tab->tab[i].id == id)
400 + return rtnl_rtrealm_tab->tab[i].name;
405 @@ -193,22 +218,29 @@ static rtnl_tab_t *rtnl_rtdsfield_tab;
407 static void rtnl_rtdsfield_initialize(void)
409 - if (rtnl_rtdsfield_tab) return;
410 + static const struct rtnl_tab_entry init_tab[] = {
414 + if (rtnl_rtdsfield_tab)
416 rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
417 - rtnl_rtdsfield_tab->tab[0] = "0";
418 - rtnl_tab_initialize("dsfield", rtnl_rtdsfield_tab->tab);
419 + rtnl_rtdsfield_tab->tab = xzalloc(sizeof(init_tab));
420 + rtnl_rtdsfield_tab->length = sizeof(init_tab) / sizeof(init_tab[0]);
421 + memcpy(rtnl_rtdsfield_tab->tab, init_tab, sizeof(init_tab));
422 + rtnl_tab_initialize("dsfield", rtnl_rtdsfield_tab);
425 const char* FAST_FUNC rtnl_dsfield_n2a(int id)
427 - if (id < 0 || id > RT_TABLE_MAX) {
432 rtnl_rtdsfield_initialize();
434 - if (rtnl_rtdsfield_tab->tab[id])
435 - return rtnl_rtdsfield_tab->tab[id];
436 + for (i = 0; i < rtnl_rtdsfield_tab->length; i++)
437 + if (rtnl_rtdsfield_tab->tab[i].id == id)
438 + return rtnl_rtdsfield_tab->tab[i].name;
443 @@ -224,27 +256,33 @@ static rtnl_tab_t *rtnl_rttable_tab;
445 static void rtnl_rttable_initialize(void)
447 + static const struct rtnl_tab_entry tab_init[] = {
449 + { 253, "default" },
454 if (rtnl_rttable_tab)
457 rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
458 - rtnl_rttable_tab->tab[0] = "unspec";
459 - rtnl_rttable_tab->tab[255] = "local";
460 - rtnl_rttable_tab->tab[254] = "main";
461 - rtnl_rttable_tab->tab[253] = "default";
462 - rtnl_tab_initialize("tables", rtnl_rttable_tab->tab);
463 + rtnl_rttable_tab->tab = xzalloc(sizeof(tab_init));
464 + rtnl_rttable_tab->length = sizeof(tab_init) / sizeof(tab_init[0]);
465 + memcpy(rtnl_rttable_tab->tab, tab_init, sizeof(tab_init));
466 + rtnl_tab_initialize("tables", rtnl_rttable_tab);
469 const char* FAST_FUNC rtnl_rttable_n2a(int id)
471 - if (id < 0 || id > RT_TABLE_MAX) {
476 rtnl_rttable_initialize();
478 - if (rtnl_rttable_tab->tab[id])
479 - return rtnl_rttable_tab->tab[id];
480 + for (i = 0; i < rtnl_rttable_tab->length; i++)
481 + if (rtnl_rttable_tab->tab[i].id == id)
482 + return rtnl_rttable_tab->tab[i].name;