302-ip-rule-add-suppress-prefixlength.patch 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. From dbac30c3784c267bbe44a2a3ebed4e827c8fe82a Mon Sep 17 00:00:00 2001
  2. From: Stefan Tomanek <stefan.tomanek@wertarbyte.de>
  3. Date: Tue, 13 Jun 2017 19:06:09 +0200
  4. Subject: [PATCH] ip rule: add suppress_{prefixlength,ifgroup} options
  5. (cherry-picked from 192dce4b84fb32346ebc5194de7daa5da3b8d1b4)
  6. function old new delta
  7. iprule_modify 816 887 +71
  8. print_rule 610 680 +70
  9. ------------------------------------------------------------------------------
  10. (add/remove: 0/0 grow/shrink: 2/0 up/down: 141/0) Total: 141 bytes
  11. Signed-off-by: Stefan Tomanek <stefan.tomanek@wertarbyte.de>
  12. Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
  13. ---
  14. networking/libiproute/iprule.c | 73 ++++++++++++++++++++++++++----------------
  15. 1 file changed, 46 insertions(+), 27 deletions(-)
  16. --- a/networking/libiproute/iprule.c
  17. +++ b/networking/libiproute/iprule.c
  18. @@ -17,25 +17,32 @@
  19. #include <netinet/ip.h>
  20. #include <arpa/inet.h>
  21. +/* from <linux/fib_rules.h>: */
  22. +#define FRA_SUPPRESS_IFGROUP 13
  23. +#define FRA_SUPPRESS_PREFIXLEN 14
  24. +
  25. #include "ip_common.h" /* #include "libbb.h" is inside */
  26. #include "rt_names.h"
  27. #include "utils.h"
  28. -/*
  29. -static void usage(void) __attribute__((noreturn));
  30. -
  31. -static void usage(void)
  32. -{
  33. - fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n");
  34. - fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n");
  35. - fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n");
  36. - fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n");
  37. - fprintf(stderr, " [ prohibit | reject | unreachable ]\n");
  38. - fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n");
  39. - fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
  40. - exit(-1);
  41. -}
  42. -*/
  43. +/* If you add stuff here, update iprule_full_usage */
  44. +static const char keywords[] ALIGN1 =
  45. + "from\0""to\0""preference\0""order\0""priority\0"
  46. + "tos\0""fwmark\0""realms\0""table\0""lookup\0"
  47. + "suppress_prefixlength\0""suppress_ifgroup\0"
  48. + "dev\0""iif\0""nat\0""map-to\0""type\0""help\0"
  49. + ;
  50. +#define keyword_preference (keywords + sizeof("from") + sizeof("to"))
  51. +#define keyword_fwmark (keyword_preference + sizeof("preference") + sizeof("order") + sizeof("priority") + sizeof("tos"))
  52. +#define keyword_realms (keyword_fwmark + sizeof("fwmark"))
  53. +#define keyword_suppress_prefixlength (keyword_realms + sizeof("realms") + sizeof("table") + sizeof("lookup"))
  54. +#define keyword_suppress_ifgroup (keyword_suppress_prefixlength + sizeof("suppress_prefixlength"))
  55. +enum {
  56. + ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
  57. + ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup,
  58. + ARG_suppress_prefixlength, ARG_suppress_ifgroup,
  59. + ARG_dev, ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help,
  60. +};
  61. static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
  62. struct nlmsghdr *n, void *arg UNUSED_PARAM)
  63. @@ -119,6 +126,17 @@ static int FAST_FUNC print_rule(const st
  64. else if (r->rtm_table)
  65. printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table));
  66. + if (tb[FRA_SUPPRESS_PREFIXLEN]) {
  67. + int pl = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_PREFIXLEN]);
  68. + if (pl != -1)
  69. + printf("%s %d ", keyword_suppress_prefixlength, pl);
  70. + }
  71. + if (tb[FRA_SUPPRESS_IFGROUP]) {
  72. + int grp = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_IFGROUP]);
  73. + if (grp != -1)
  74. + printf("%s %d ", keyword_suppress_ifgroup, grp);
  75. + }
  76. +
  77. if (tb[RTA_FLOW]) {
  78. uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
  79. uint32_t from = to>>16;
  80. @@ -174,15 +192,6 @@ static int iprule_list(char **argv)
  81. /* Return value becomes exitcode. It's okay to not return at all */
  82. static int iprule_modify(int cmd, char **argv)
  83. {
  84. - static const char keywords[] ALIGN1 =
  85. - "from\0""to\0""preference\0""order\0""priority\0"
  86. - "tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
  87. - "iif\0""nat\0""map-to\0""type\0""help\0";
  88. - enum {
  89. - ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
  90. - ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
  91. - ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
  92. - };
  93. bool table_ok = 0;
  94. struct rtnl_handle rth;
  95. struct {
  96. @@ -232,7 +241,7 @@ static int iprule_modify(int cmd, char *
  97. ) {
  98. uint32_t pref;
  99. NEXT_ARG();
  100. - pref = get_u32(*argv, "preference");
  101. + pref = get_u32(*argv, keyword_preference);
  102. addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
  103. } else if (key == ARG_tos) {
  104. uint32_t tos;
  105. @@ -243,13 +252,13 @@ static int iprule_modify(int cmd, char *
  106. } else if (key == ARG_fwmark) {
  107. uint32_t fwmark;
  108. NEXT_ARG();
  109. - fwmark = get_u32(*argv, "fwmark");
  110. + fwmark = get_u32(*argv, keyword_fwmark);
  111. addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
  112. } else if (key == ARG_realms) {
  113. uint32_t realm;
  114. NEXT_ARG();
  115. if (get_rt_realms(&realm, *argv))
  116. - invarg_1_to_2(*argv, "realms");
  117. + invarg_1_to_2(*argv, keyword_realms);
  118. addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
  119. } else if (key == ARG_table ||
  120. key == ARG_lookup
  121. @@ -265,6 +274,16 @@ static int iprule_modify(int cmd, char *
  122. addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
  123. }
  124. table_ok = 1;
  125. + } else if (key == ARG_suppress_prefixlength) {
  126. + int prefix_length;
  127. + NEXT_ARG();
  128. + prefix_length = get_u32(*argv, keyword_suppress_prefixlength);
  129. + addattr32(&req.n, sizeof(req), FRA_SUPPRESS_PREFIXLEN, prefix_length);
  130. + } else if (key == ARG_suppress_ifgroup) {
  131. + int grp;
  132. + NEXT_ARG();
  133. + grp = get_u32(*argv, keyword_suppress_ifgroup);
  134. + addattr32(&req.n, sizeof(req), FRA_SUPPRESS_IFGROUP, grp);
  135. } else if (key == ARG_dev ||
  136. key == ARG_iif
  137. ) {