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