iproute: properly support high routing table IDs
[openwrt/openwrt.git] / package / utils / busybox / patches / 303-ip-route-fix-high-table-ids.patch
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
5
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.
9
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.
12
13 This commit ...
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
19
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.
22
23 Signed-off-by: Jo-Philipp Wich <jo@mein.io>
24 ---
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(-)
29
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
33 inet_prefix dst;
34 inet_prefix src;
35 int host_len = -1;
36 + uint32_t rtable;
37
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)
42 host_len = 32;
43
44 - if (r->rtm_family == AF_INET6) {
45 - if (G_filter.tb) {
46 - if (G_filter.tb < 0) {
47 - if (!(r->rtm_flags & RTM_F_CLONED)) {
48 - return 0;
49 - }
50 - } else {
51 - if (r->rtm_flags & RTM_F_CLONED) {
52 - return 0;
53 - }
54 - if (G_filter.tb == RT_TABLE_LOCAL) {
55 - if (r->rtm_type != RTN_LOCAL) {
56 - return 0;
57 - }
58 - } else if (G_filter.tb == RT_TABLE_MAIN) {
59 - if (r->rtm_type == RTN_LOCAL) {
60 - return 0;
61 - }
62 - } else {
63 - return 0;
64 - }
65 - }
66 - }
67 - } else {
68 - if (G_filter.tb > 0 && G_filter.tb != r->rtm_table) {
69 - return 0;
70 - }
71 - }
72 if (G_filter.rdst.family
73 && (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len)
74 ) {
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);
78
79 + rtable = tb[RTA_TABLE] ? *(uint32_t*)RTA_DATA(tb[RTA_TABLE]) : r->rtm_table;
80 +
81 + if (G_filter.tb) {
82 + if (r->rtm_family == AF_INET6) {
83 + if (G_filter.tb < 0) {
84 + if (!(r->rtm_flags & RTM_F_CLONED)) {
85 + return 0;
86 + }
87 + } else {
88 + if (r->rtm_flags & RTM_F_CLONED) {
89 + return 0;
90 + }
91 + if (G_filter.tb == RT_TABLE_LOCAL) {
92 + if (r->rtm_type != RTN_LOCAL) {
93 + return 0;
94 + }
95 + } else if (G_filter.tb == RT_TABLE_MAIN) {
96 + if (r->rtm_type == RTN_LOCAL) {
97 + return 0;
98 + }
99 + } else if (G_filter.tb != rtable) {
100 + return 0;
101 + }
102 + }
103 + } else {
104 + if (G_filter.tb != rtable) {
105 + return 0;
106 + }
107 + }
108 + }
109 +
110 if (tb[RTA_SRC]) {
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,)
114 smalluint ok = 0;
115 smalluint scope_ok = 0;
116 int arg;
117 -
118 +#if ENABLE_FEATURE_IP_RULE
119 + uint32_t rtable = 0;
120 +#endif
121 memset(&req, 0, sizeof(req));
122
123 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
124 @@ -420,7 +426,7 @@ IF_FEATURE_IP_RULE(ARG_table,)
125 NEXT_ARG();
126 if (rtnl_rttable_a2n(&tid, *argv))
127 invarg_1_to_2(*argv, "table");
128 - req.r.rtm_table = tid;
129 + rtable = tid;
130 #endif
131 } else if (arg == ARG_dev || arg == ARG_oif) {
132 NEXT_ARG();
133 @@ -476,6 +482,15 @@ IF_FEATURE_IP_RULE(ARG_table,)
134 }
135 }
136
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;
143 + }
144 +#endif
145 +
146 if (mxrta->rta_len > RTA_LENGTH(0)) {
147 if (mxlock) {
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]));
153 }
154
155 - if (r->rtm_table)
156 + if (tb[RTA_TABLE])
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));
160
161 if (tb[RTA_FLOW]) {
162 --- a/networking/libiproute/rt_names.c
163 +++ b/networking/libiproute/rt_names.c
164 @@ -11,21 +11,26 @@
165 #include "rt_names.h"
166
167 #define CONFDIR CONFIG_FEATURE_IP_ROUTE_DIR
168 +#define RT_TABLE_MAX 65535
169 +
170 +struct rtnl_tab_entry {
171 + unsigned int id;
172 + const char *name;
173 +};
174
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!
183 - */
184 -#define RT_TABLE_MAX 1023
185 - const char *tab[RT_TABLE_MAX+1];
186 + struct rtnl_tab_entry *tab;
187 + size_t length;
188 } rtnl_tab_t;
189
190 -static void rtnl_tab_initialize(const char *file, const char **tab)
191 +static int tabcmp(const void *p1, const void *p2)
192 +{
193 + const struct rtnl_tab_entry *e1 = p1;
194 + const struct rtnl_tab_entry *e2 = p2;
195 + return strcmp(e1->name, e2->name);
196 +}
197 +
198 +static void rtnl_tab_initialize(const char *file, rtnl_tab_t *tab)
199 {
200 char *token[2];
201 char fullname[sizeof(CONFDIR"/rt_dsfield") + 8];
202 @@ -40,34 +45,42 @@ static void rtnl_tab_initialize(const ch
203 file, parser->lineno);
204 break;
205 }
206 - tab[id] = xstrdup(token[1]);
207 +
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]);
211 + tab->length++;
212 }
213 config_close(parser);
214 + qsort(tab->tab, tab->length, sizeof(*tab->tab), tabcmp);
215 }
216
217 static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
218 {
219 - unsigned i;
220 -
221 - if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
222 - *id = tab->cached_result;
223 - return 0;
224 - }
225 + int delta;
226 + ssize_t l = 0;
227 + ssize_t r = tab->length - 1;
228 + ssize_t m;
229 + uint32_t i;
230 +
231 + while (l <= r) {
232 + m = l + (r - l) / 2;
233 + delta = strcmp(tab->tab[m].name, arg);
234
235 - for (i = 0; i <= RT_TABLE_MAX; i++) {
236 - if (tab->tab[i]
237 - && strcmp(tab->tab[i], arg) == 0
238 - ) {
239 - tab->cached_str = tab->tab[i];
240 - tab->cached_result = i;
241 - *id = i;
242 + if (delta == 0) {
243 + *id = tab->tab[m].id;
244 return 0;
245 + } else if (delta < 0) {
246 + l = m + 1;
247 + } else {
248 + r = m - 1;
249 }
250 }
251
252 i = bb_strtou(arg, NULL, base);
253 if (i > RT_TABLE_MAX)
254 return -1;
255 +
256 *id = i;
257 return 0;
258 }
259 @@ -77,40 +90,39 @@ static rtnl_tab_t *rtnl_rtprot_tab;
260
261 static void rtnl_rtprot_initialize(void)
262 {
263 - static const char *const init_tab[] = {
264 - "none",
265 - "redirect",
266 - "kernel",
267 - "boot",
268 - "static",
269 - NULL,
270 - NULL,
271 - NULL,
272 - "gated",
273 - "ra",
274 - "mrt",
275 - "zebra",
276 - "bird",
277 + static const struct rtnl_tab_entry init_tab[] = {
278 + { 0, "none" },
279 + { 1, "redirect" },
280 + { 2, "kernel" },
281 + { 3, "boot" },
282 + { 4, "static" },
283 + { 8, "gated" },
284 + { 9, "ra" },
285 + { 10, "mrt" },
286 + { 11, "zebra" },
287 + { 12, "bird" }
288 };
289
290 if (rtnl_rtprot_tab)
291 return;
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);
298 }
299
300 #if 0 /* UNUSED */
301 const char* FAST_FUNC rtnl_rtprot_n2a(int id)
302 {
303 - if (id < 0 || id > RT_TABLE_MAX) {
304 - return itoa(id);
305 - }
306 + size_t i;
307
308 rtnl_rtprot_initialize();
309
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;
315 +
316 return itoa(id);
317 }
318 #endif
319 @@ -126,27 +138,33 @@ static rtnl_tab_t *rtnl_rtscope_tab;
320
321 static void rtnl_rtscope_initialize(void)
322 {
323 + static const struct rtnl_tab_entry init_tab[] = {
324 + { 0, "global" },
325 + { 200, "site" },
326 + { 253, "link" },
327 + { 254, "host" },
328 + { 255, "nowhere" }
329 + };
330 +
331 if (rtnl_rtscope_tab)
332 return;
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);
344 }
345
346 const char* FAST_FUNC rtnl_rtscope_n2a(int id)
347 {
348 - if (id < 0 || id > RT_TABLE_MAX) {
349 - return itoa(id);
350 - }
351 + size_t i;
352
353 rtnl_rtscope_initialize();
354
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;
360 +
361 return itoa(id);
362 }
363
364 @@ -161,10 +179,17 @@ static rtnl_tab_t *rtnl_rtrealm_tab;
365
366 static void rtnl_rtrealm_initialize(void)
367 {
368 - if (rtnl_rtrealm_tab) return;
369 + static const struct rtnl_tab_entry init_tab[] = {
370 + { 0, "unknown" }
371 + };
372 +
373 + if (rtnl_rtrealm_tab)
374 + return;
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);
382 }
383
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)
388 {
389 - if (id < 0 || id > RT_TABLE_MAX) {
390 - return itoa(id);
391 - }
392 + size_t i;
393
394 rtnl_rtrealm_initialize();
395
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;
401 +
402 return itoa(id);
403 }
404 #endif
405 @@ -193,22 +218,29 @@ static rtnl_tab_t *rtnl_rtdsfield_tab;
406
407 static void rtnl_rtdsfield_initialize(void)
408 {
409 - if (rtnl_rtdsfield_tab) return;
410 + static const struct rtnl_tab_entry init_tab[] = {
411 + { 0, "0" }
412 + };
413 +
414 + if (rtnl_rtdsfield_tab)
415 + return;
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);
423 }
424
425 const char* FAST_FUNC rtnl_dsfield_n2a(int id)
426 {
427 - if (id < 0 || id > RT_TABLE_MAX) {
428 - return itoa(id);
429 - }
430 + size_t i;
431
432 rtnl_rtdsfield_initialize();
433
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;
439 +
440 return itoa(id);
441 }
442
443 @@ -224,27 +256,33 @@ static rtnl_tab_t *rtnl_rttable_tab;
444
445 static void rtnl_rttable_initialize(void)
446 {
447 + static const struct rtnl_tab_entry tab_init[] = {
448 + { 0, "unspec" },
449 + { 253, "default" },
450 + { 254, "main" },
451 + { 255, "local" }
452 + };
453 +
454 if (rtnl_rttable_tab)
455 return;
456
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);
467 }
468
469 const char* FAST_FUNC rtnl_rttable_n2a(int id)
470 {
471 - if (id < 0 || id > RT_TABLE_MAX) {
472 - return itoa(id);
473 - }
474 + size_t i;
475
476 rtnl_rttable_initialize();
477
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;
483 +
484 return itoa(id);
485 }
486