1
0

303-ip-route-fix-high-table-ids.patch 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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. The Linux kernel uses two distinct fields to denote the routing table ID in
  6. use by network routes; the 8 bit `rtm_table` member of `struct rtmsg` and the
  7. 32 bit `RTA_TABLE` netlink attribute.
  8. If a routing table ID is larger than 255, the `RT_TABLE` attribute must be used
  9. and the `rtm_table` field has to be set to the special `RT_TABLE_UNSPEC` value.
  10. This commit ...
  11. - switches the *_n2a() and *_a2n() functions of rt_names.c to use dynamically
  12. sized, name-sorted arrays instead of fixed arrays limited to 1024 slots in
  13. order to support IDs up to 65535
  14. - adds proper handling of high table IDs to iprule.c and iproute.c when
  15. adding, removing and dumping ip rules and network routes
  16. After this change, the Busybox ip applet fully supports IP rules with high ID
  17. numbers, using the same logic as the full iproute2.
  18. Signed-off-by: Jo-Philipp Wich <jo@mein.io>
  19. ---
  20. networking/libiproute/iproute.c | 75 ++++++++------
  21. networking/libiproute/iprule.c | 4 +-
  22. networking/libiproute/rt_names.c | 204 +++++++++++++++++++++++----------------
  23. 3 files changed, 169 insertions(+), 114 deletions(-)
  24. --- a/networking/libiproute/iproute.c
  25. +++ b/networking/libiproute/iproute.c
  26. @@ -66,6 +66,7 @@ static int FAST_FUNC print_route(const s
  27. inet_prefix dst;
  28. inet_prefix src;
  29. int host_len = -1;
  30. + uint32_t rtable;
  31. if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
  32. fprintf(stderr, "Not a route: %08x %08x %08x\n",
  33. @@ -83,34 +84,6 @@ static int FAST_FUNC print_route(const s
  34. else if (r->rtm_family == AF_INET)
  35. host_len = 32;
  36. - if (r->rtm_family == AF_INET6) {
  37. - if (G_filter.tb) {
  38. - if (G_filter.tb < 0) {
  39. - if (!(r->rtm_flags & RTM_F_CLONED)) {
  40. - return 0;
  41. - }
  42. - } else {
  43. - if (r->rtm_flags & RTM_F_CLONED) {
  44. - return 0;
  45. - }
  46. - if (G_filter.tb == RT_TABLE_LOCAL) {
  47. - if (r->rtm_type != RTN_LOCAL) {
  48. - return 0;
  49. - }
  50. - } else if (G_filter.tb == RT_TABLE_MAIN) {
  51. - if (r->rtm_type == RTN_LOCAL) {
  52. - return 0;
  53. - }
  54. - } else {
  55. - return 0;
  56. - }
  57. - }
  58. - }
  59. - } else {
  60. - if (G_filter.tb > 0 && G_filter.tb != r->rtm_table) {
  61. - return 0;
  62. - }
  63. - }
  64. if (G_filter.rdst.family
  65. && (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len)
  66. ) {
  67. @@ -141,6 +114,37 @@ static int FAST_FUNC print_route(const s
  68. memset(&dst, 0, sizeof(dst));
  69. parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
  70. + rtable = tb[RTA_TABLE] ? *(uint32_t*)RTA_DATA(tb[RTA_TABLE]) : r->rtm_table;
  71. +
  72. + if (G_filter.tb) {
  73. + if (r->rtm_family == AF_INET6) {
  74. + if (G_filter.tb < 0) {
  75. + if (!(r->rtm_flags & RTM_F_CLONED)) {
  76. + return 0;
  77. + }
  78. + } else {
  79. + if (r->rtm_flags & RTM_F_CLONED) {
  80. + return 0;
  81. + }
  82. + if (G_filter.tb == RT_TABLE_LOCAL) {
  83. + if (r->rtm_type != RTN_LOCAL) {
  84. + return 0;
  85. + }
  86. + } else if (G_filter.tb == RT_TABLE_MAIN) {
  87. + if (r->rtm_type == RTN_LOCAL) {
  88. + return 0;
  89. + }
  90. + } else if (G_filter.tb != rtable) {
  91. + return 0;
  92. + }
  93. + }
  94. + } else {
  95. + if (G_filter.tb != rtable) {
  96. + return 0;
  97. + }
  98. + }
  99. + }
  100. +
  101. if (tb[RTA_SRC]) {
  102. src.bitlen = r->rtm_src_len;
  103. src.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
  104. @@ -349,7 +353,9 @@ IF_FEATURE_IP_RULE(ARG_table,)
  105. smalluint ok = 0;
  106. smalluint scope_ok = 0;
  107. int arg;
  108. -
  109. +#if ENABLE_FEATURE_IP_RULE
  110. + uint32_t rtable = 0;
  111. +#endif
  112. memset(&req, 0, sizeof(req));
  113. req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
  114. @@ -419,7 +425,7 @@ IF_FEATURE_IP_RULE(ARG_table,)
  115. NEXT_ARG();
  116. if (rtnl_rttable_a2n(&tid, *argv))
  117. invarg_1_to_2(*argv, "table");
  118. - req.r.rtm_table = tid;
  119. + rtable = tid;
  120. #endif
  121. } else if (arg == ARG_dev || arg == ARG_oif) {
  122. NEXT_ARG();
  123. @@ -475,6 +481,15 @@ IF_FEATURE_IP_RULE(ARG_table,)
  124. }
  125. }
  126. +#if ENABLE_FEATURE_IP_RULE
  127. + if (rtable >= 256) {
  128. + addattr32(&req.n, sizeof(req), RTA_TABLE, rtable);
  129. + req.r.rtm_table = RT_TABLE_UNSPEC;
  130. + } else if (rtable > 0) {
  131. + req.r.rtm_table = rtable;
  132. + }
  133. +#endif
  134. +
  135. if (mxrta->rta_len > RTA_LENGTH(0)) {
  136. if (mxlock) {
  137. rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
  138. --- a/networking/libiproute/iprule.c
  139. +++ b/networking/libiproute/iprule.c
  140. @@ -114,7 +114,9 @@ static int FAST_FUNC print_rule(const st
  141. printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
  142. }
  143. - if (r->rtm_table)
  144. + if (tb[RTA_TABLE])
  145. + printf("lookup %s ", rtnl_rttable_n2a(*(uint32_t*)RTA_DATA(tb[RTA_TABLE])));
  146. + else if (r->rtm_table)
  147. printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table));
  148. if (tb[RTA_FLOW]) {
  149. --- a/networking/libiproute/rt_names.c
  150. +++ b/networking/libiproute/rt_names.c
  151. @@ -11,21 +11,26 @@
  152. #include "rt_names.h"
  153. #define CONFDIR CONFIG_FEATURE_IP_ROUTE_DIR
  154. +#define RT_TABLE_MAX 65535
  155. +
  156. +struct rtnl_tab_entry {
  157. + unsigned int id;
  158. + const char *name;
  159. +};
  160. typedef struct rtnl_tab_t {
  161. - const char *cached_str;
  162. - unsigned cached_result;
  163. - /* upstream version switched to a hash table and removed
  164. - * id < 256 limit. For now bbox bumps this array size from 256
  165. - * to 1024. If you plan to change this to a hash table,
  166. - * consider merging several hash tables we have (for example,
  167. - * awk has resizable one!
  168. - */
  169. -#define RT_TABLE_MAX 1023
  170. - const char *tab[RT_TABLE_MAX+1];
  171. + struct rtnl_tab_entry *tab;
  172. + size_t length;
  173. } rtnl_tab_t;
  174. -static void rtnl_tab_initialize(const char *file, const char **tab)
  175. +static int tabcmp(const void *p1, const void *p2)
  176. +{
  177. + const struct rtnl_tab_entry *e1 = p1;
  178. + const struct rtnl_tab_entry *e2 = p2;
  179. + return strcmp(e1->name, e2->name);
  180. +}
  181. +
  182. +static void rtnl_tab_initialize(const char *file, rtnl_tab_t *tab)
  183. {
  184. char *token[2];
  185. char fullname[sizeof(CONFDIR"/rt_dsfield") + 8];
  186. @@ -40,34 +45,42 @@ static void rtnl_tab_initialize(const ch
  187. file, parser->lineno);
  188. break;
  189. }
  190. - tab[id] = xstrdup(token[1]);
  191. +
  192. + tab->tab = xrealloc(tab->tab, (tab->length + 1) * sizeof(*tab->tab));
  193. + tab->tab[tab->length].id = id;
  194. + tab->tab[tab->length].name = xstrdup(token[1]);
  195. + tab->length++;
  196. }
  197. config_close(parser);
  198. + qsort(tab->tab, tab->length, sizeof(*tab->tab), tabcmp);
  199. }
  200. static int rtnl_a2n(rtnl_tab_t *tab, uint32_t *id, const char *arg, int base)
  201. {
  202. - unsigned i;
  203. -
  204. - if (tab->cached_str && strcmp(tab->cached_str, arg) == 0) {
  205. - *id = tab->cached_result;
  206. - return 0;
  207. - }
  208. + int delta;
  209. + ssize_t l = 0;
  210. + ssize_t r = tab->length - 1;
  211. + ssize_t m;
  212. + uint32_t i;
  213. +
  214. + while (l <= r) {
  215. + m = l + (r - l) / 2;
  216. + delta = strcmp(tab->tab[m].name, arg);
  217. - for (i = 0; i <= RT_TABLE_MAX; i++) {
  218. - if (tab->tab[i]
  219. - && strcmp(tab->tab[i], arg) == 0
  220. - ) {
  221. - tab->cached_str = tab->tab[i];
  222. - tab->cached_result = i;
  223. - *id = i;
  224. + if (delta == 0) {
  225. + *id = tab->tab[m].id;
  226. return 0;
  227. + } else if (delta < 0) {
  228. + l = m + 1;
  229. + } else {
  230. + r = m - 1;
  231. }
  232. }
  233. i = bb_strtou(arg, NULL, base);
  234. if (i > RT_TABLE_MAX)
  235. return -1;
  236. +
  237. *id = i;
  238. return 0;
  239. }
  240. @@ -77,40 +90,39 @@ static rtnl_tab_t *rtnl_rtprot_tab;
  241. static void rtnl_rtprot_initialize(void)
  242. {
  243. - static const char *const init_tab[] = {
  244. - "none",
  245. - "redirect",
  246. - "kernel",
  247. - "boot",
  248. - "static",
  249. - NULL,
  250. - NULL,
  251. - NULL,
  252. - "gated",
  253. - "ra",
  254. - "mrt",
  255. - "zebra",
  256. - "bird",
  257. + static const struct rtnl_tab_entry init_tab[] = {
  258. + { 0, "none" },
  259. + { 1, "redirect" },
  260. + { 2, "kernel" },
  261. + { 3, "boot" },
  262. + { 4, "static" },
  263. + { 8, "gated" },
  264. + { 9, "ra" },
  265. + { 10, "mrt" },
  266. + { 11, "zebra" },
  267. + { 12, "bird" }
  268. };
  269. if (rtnl_rtprot_tab)
  270. return;
  271. rtnl_rtprot_tab = xzalloc(sizeof(*rtnl_rtprot_tab));
  272. + rtnl_rtprot_tab->tab = xzalloc(sizeof(init_tab));
  273. + rtnl_rtprot_tab->length = sizeof(init_tab) / sizeof(init_tab[0]);
  274. memcpy(rtnl_rtprot_tab->tab, init_tab, sizeof(init_tab));
  275. - rtnl_tab_initialize("protos", rtnl_rtprot_tab->tab);
  276. + rtnl_tab_initialize("protos", rtnl_rtprot_tab);
  277. }
  278. #if 0 /* UNUSED */
  279. const char* FAST_FUNC rtnl_rtprot_n2a(int id)
  280. {
  281. - if (id < 0 || id > RT_TABLE_MAX) {
  282. - return itoa(id);
  283. - }
  284. + size_t i;
  285. rtnl_rtprot_initialize();
  286. - if (rtnl_rtprot_tab->tab[id])
  287. - return rtnl_rtprot_tab->tab[id];
  288. + for (i = 0; i < rtnl_rtprot_tab->length; i++)
  289. + if (rtnl_rtprot_tab->tab[i].id == id)
  290. + return rtnl_rtprot_tab->tab[i].name;
  291. +
  292. return itoa(id);
  293. }
  294. #endif
  295. @@ -126,27 +138,33 @@ static rtnl_tab_t *rtnl_rtscope_tab;
  296. static void rtnl_rtscope_initialize(void)
  297. {
  298. + static const struct rtnl_tab_entry init_tab[] = {
  299. + { 0, "global" },
  300. + { 200, "site" },
  301. + { 253, "link" },
  302. + { 254, "host" },
  303. + { 255, "nowhere" }
  304. + };
  305. +
  306. if (rtnl_rtscope_tab)
  307. return;
  308. rtnl_rtscope_tab = xzalloc(sizeof(*rtnl_rtscope_tab));
  309. - rtnl_rtscope_tab->tab[0] = "global";
  310. - rtnl_rtscope_tab->tab[255] = "nowhere";
  311. - rtnl_rtscope_tab->tab[254] = "host";
  312. - rtnl_rtscope_tab->tab[253] = "link";
  313. - rtnl_rtscope_tab->tab[200] = "site";
  314. - rtnl_tab_initialize("scopes", rtnl_rtscope_tab->tab);
  315. + rtnl_rtscope_tab->tab = xzalloc(sizeof(init_tab));
  316. + rtnl_rtscope_tab->length = sizeof(init_tab) / sizeof(init_tab[0]);
  317. + memcpy(rtnl_rtscope_tab->tab, init_tab, sizeof(init_tab));
  318. + rtnl_tab_initialize("scopes", rtnl_rtscope_tab);
  319. }
  320. const char* FAST_FUNC rtnl_rtscope_n2a(int id)
  321. {
  322. - if (id < 0 || id > RT_TABLE_MAX) {
  323. - return itoa(id);
  324. - }
  325. + size_t i;
  326. rtnl_rtscope_initialize();
  327. - if (rtnl_rtscope_tab->tab[id])
  328. - return rtnl_rtscope_tab->tab[id];
  329. + for (i = 0; i < rtnl_rtscope_tab->length; i++)
  330. + if (rtnl_rtscope_tab->tab[i].id == id)
  331. + return rtnl_rtscope_tab->tab[i].name;
  332. +
  333. return itoa(id);
  334. }
  335. @@ -161,10 +179,17 @@ static rtnl_tab_t *rtnl_rtrealm_tab;
  336. static void rtnl_rtrealm_initialize(void)
  337. {
  338. - if (rtnl_rtrealm_tab) return;
  339. + static const struct rtnl_tab_entry init_tab[] = {
  340. + { 0, "unknown" }
  341. + };
  342. +
  343. + if (rtnl_rtrealm_tab)
  344. + return;
  345. rtnl_rtrealm_tab = xzalloc(sizeof(*rtnl_rtrealm_tab));
  346. - rtnl_rtrealm_tab->tab[0] = "unknown";
  347. - rtnl_tab_initialize("realms", rtnl_rtrealm_tab->tab);
  348. + rtnl_rtrealm_tab->tab = xzalloc(sizeof(init_tab));
  349. + rtnl_rtrealm_tab->length = sizeof(init_tab) / sizeof(init_tab[0]);
  350. + memcpy(rtnl_rtrealm_tab->tab, init_tab, sizeof(init_tab));
  351. + rtnl_tab_initialize("realms", rtnl_rtrealm_tab);
  352. }
  353. int FAST_FUNC rtnl_rtrealm_a2n(uint32_t *id, char *arg)
  354. @@ -176,14 +201,14 @@ int FAST_FUNC rtnl_rtrealm_a2n(uint32_t
  355. #if ENABLE_FEATURE_IP_RULE
  356. const char* FAST_FUNC rtnl_rtrealm_n2a(int id)
  357. {
  358. - if (id < 0 || id > RT_TABLE_MAX) {
  359. - return itoa(id);
  360. - }
  361. + size_t i;
  362. rtnl_rtrealm_initialize();
  363. - if (rtnl_rtrealm_tab->tab[id])
  364. - return rtnl_rtrealm_tab->tab[id];
  365. + for (i = 0; i < rtnl_rtrealm_tab->length; i++)
  366. + if (rtnl_rtrealm_tab->tab[i].id == id)
  367. + return rtnl_rtrealm_tab->tab[i].name;
  368. +
  369. return itoa(id);
  370. }
  371. #endif
  372. @@ -193,22 +218,29 @@ static rtnl_tab_t *rtnl_rtdsfield_tab;
  373. static void rtnl_rtdsfield_initialize(void)
  374. {
  375. - if (rtnl_rtdsfield_tab) return;
  376. + static const struct rtnl_tab_entry init_tab[] = {
  377. + { 0, "0" }
  378. + };
  379. +
  380. + if (rtnl_rtdsfield_tab)
  381. + return;
  382. rtnl_rtdsfield_tab = xzalloc(sizeof(*rtnl_rtdsfield_tab));
  383. - rtnl_rtdsfield_tab->tab[0] = "0";
  384. - rtnl_tab_initialize("dsfield", rtnl_rtdsfield_tab->tab);
  385. + rtnl_rtdsfield_tab->tab = xzalloc(sizeof(init_tab));
  386. + rtnl_rtdsfield_tab->length = sizeof(init_tab) / sizeof(init_tab[0]);
  387. + memcpy(rtnl_rtdsfield_tab->tab, init_tab, sizeof(init_tab));
  388. + rtnl_tab_initialize("dsfield", rtnl_rtdsfield_tab);
  389. }
  390. const char* FAST_FUNC rtnl_dsfield_n2a(int id)
  391. {
  392. - if (id < 0 || id > RT_TABLE_MAX) {
  393. - return itoa(id);
  394. - }
  395. + size_t i;
  396. rtnl_rtdsfield_initialize();
  397. - if (rtnl_rtdsfield_tab->tab[id])
  398. - return rtnl_rtdsfield_tab->tab[id];
  399. + for (i = 0; i < rtnl_rtdsfield_tab->length; i++)
  400. + if (rtnl_rtdsfield_tab->tab[i].id == id)
  401. + return rtnl_rtdsfield_tab->tab[i].name;
  402. +
  403. return itoa(id);
  404. }
  405. @@ -224,27 +256,33 @@ static rtnl_tab_t *rtnl_rttable_tab;
  406. static void rtnl_rttable_initialize(void)
  407. {
  408. + static const struct rtnl_tab_entry tab_init[] = {
  409. + { 0, "unspec" },
  410. + { 253, "default" },
  411. + { 254, "main" },
  412. + { 255, "local" }
  413. + };
  414. +
  415. if (rtnl_rttable_tab)
  416. return;
  417. rtnl_rttable_tab = xzalloc(sizeof(*rtnl_rttable_tab));
  418. - rtnl_rttable_tab->tab[0] = "unspec";
  419. - rtnl_rttable_tab->tab[255] = "local";
  420. - rtnl_rttable_tab->tab[254] = "main";
  421. - rtnl_rttable_tab->tab[253] = "default";
  422. - rtnl_tab_initialize("tables", rtnl_rttable_tab->tab);
  423. + rtnl_rttable_tab->tab = xzalloc(sizeof(tab_init));
  424. + rtnl_rttable_tab->length = sizeof(tab_init) / sizeof(tab_init[0]);
  425. + memcpy(rtnl_rttable_tab->tab, tab_init, sizeof(tab_init));
  426. + rtnl_tab_initialize("tables", rtnl_rttable_tab);
  427. }
  428. const char* FAST_FUNC rtnl_rttable_n2a(int id)
  429. {
  430. - if (id < 0 || id > RT_TABLE_MAX) {
  431. - return itoa(id);
  432. - }
  433. + size_t i;
  434. rtnl_rttable_initialize();
  435. - if (rtnl_rttable_tab->tab[id])
  436. - return rtnl_rttable_tab->tab[id];
  437. + for (i = 0; i < rtnl_rttable_tab->length; i++)
  438. + if (rtnl_rttable_tab->tab[i].id == id)
  439. + return rtnl_rttable_tab->tab[i].name;
  440. +
  441. return itoa(id);
  442. }