blob: 1bb5e759e01c22ad89d362d09c0d9f26f488a66c [file] [log] [blame]
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +00001/* vi: set sw=4 ts=4: */
2/*
Denys Vlasenkofb132e42010-10-29 11:46:52 +02003 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License
5 * as published by the Free Software Foundation; either version
6 * 2 of the License, or (at your option) any later version.
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +00007 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02008 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +00009 *
10 * Changes:
11 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +020012 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +000013 * initially integrated into busybox by Bernhard Reutner-Fischer
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000014 */
15
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000016#include <netinet/in.h>
17#include <netinet/ip.h>
18#include <arpa/inet.h>
19
Stefan Tomanek192dce42017-06-13 19:06:09 +020020/* from <linux/fib_rules.h>: */
21#define FRA_SUPPRESS_IFGROUP 13
22#define FRA_SUPPRESS_PREFIXLEN 14
23
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000024#include "ip_common.h" /* #include "libbb.h" is inside */
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000025#include "rt_names.h"
26#include "utils.h"
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000027
Stefan Tomanek192dce42017-06-13 19:06:09 +020028/* If you add stuff here, update iprule_full_usage */
29static const char keywords[] ALIGN1 =
30 "from\0""to\0""preference\0""order\0""priority\0"
31 "tos\0""fwmark\0""realms\0""table\0""lookup\0"
32 "suppress_prefixlength\0""suppress_ifgroup\0"
33 "dev\0""iif\0""nat\0""map-to\0""type\0""help\0"
34 ;
35#define keyword_preference (keywords + sizeof("from") + sizeof("to"))
36#define keyword_fwmark (keyword_preference + sizeof("preference") + sizeof("order") + sizeof("priority") + sizeof("tos"))
37#define keyword_realms (keyword_fwmark + sizeof("fwmark"))
38#define keyword_suppress_prefixlength (keyword_realms + sizeof("realms") + sizeof("table") + sizeof("lookup"))
39#define keyword_suppress_ifgroup (keyword_suppress_prefixlength + sizeof("suppress_prefixlength"))
40enum {
41 ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
42 ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup,
43 ARG_suppress_prefixlength, ARG_suppress_ifgroup,
44 ARG_dev, ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help,
45};
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000046
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020047static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000048 struct nlmsghdr *n, void *arg UNUSED_PARAM)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000049{
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000050 struct rtmsg *r = NLMSG_DATA(n);
51 int len = n->nlmsg_len;
52 int host_len = -1;
53 struct rtattr * tb[RTA_MAX+1];
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000054
55 if (n->nlmsg_type != RTM_NEWRULE)
56 return 0;
57
58 len -= NLMSG_LENGTH(sizeof(*r));
59 if (len < 0)
60 return -1;
61
62 memset(tb, 0, sizeof(tb));
63 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
64
65 if (r->rtm_family == AF_INET)
66 host_len = 32;
67 else if (r->rtm_family == AF_INET6)
68 host_len = 128;
69/* else if (r->rtm_family == AF_DECnet)
70 host_len = 16;
71 else if (r->rtm_family == AF_IPX)
72 host_len = 80;
73*/
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +020074 printf("%u:\t", tb[RTA_PRIORITY] ?
75 *(unsigned*)RTA_DATA(tb[RTA_PRIORITY])
76 : 0);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000077 printf("from ");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000078 if (tb[RTA_SRC]) {
79 if (r->rtm_src_len != host_len) {
Denys Vlasenko926d8012015-10-14 13:56:42 +020080 printf("%s/%u",
81 rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000082 r->rtm_src_len
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010083 );
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000084 } else {
Denis Vlasenko605b20e2007-09-30 16:22:36 +000085 fputs(format_host(r->rtm_family,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010086 RTA_PAYLOAD(tb[RTA_SRC]),
Denys Vlasenko926d8012015-10-14 13:56:42 +020087 RTA_DATA(tb[RTA_SRC])),
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010088 stdout
89 );
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000090 }
91 } else if (r->rtm_src_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000092 printf("0/%d", r->rtm_src_len);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000093 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000094 printf("all");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000095 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000096 bb_putchar(' ');
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000097
98 if (tb[RTA_DST]) {
99 if (r->rtm_dst_len != host_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000100 printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
Denys Vlasenko926d8012015-10-14 13:56:42 +0200101 RTA_DATA(tb[RTA_DST])),
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000102 r->rtm_dst_len
103 );
104 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000105 printf("to %s ", format_host(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000106 RTA_PAYLOAD(tb[RTA_DST]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200107 RTA_DATA(tb[RTA_DST])));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000108 }
109 } else if (r->rtm_dst_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000110 printf("to 0/%d ", r->rtm_dst_len);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000111 }
112
113 if (r->rtm_tos) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200114 printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000115 }
116 if (tb[RTA_PROTOINFO]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000117 printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000118 }
119
120 if (tb[RTA_IIF]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000121 printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000122 }
123
Lukasz Nowakb42107f2016-12-13 12:58:31 +0000124 if (tb[RTA_TABLE])
125 printf("lookup %s ", rtnl_rttable_n2a(*(uint32_t*)RTA_DATA(tb[RTA_TABLE])));
126 else if (r->rtm_table)
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200127 printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000128
Stefan Tomanek192dce42017-06-13 19:06:09 +0200129 if (tb[FRA_SUPPRESS_PREFIXLEN]) {
130 int pl = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_PREFIXLEN]);
131 if (pl != -1)
132 printf("%s %d ", keyword_suppress_prefixlength, pl);
133 }
134 if (tb[FRA_SUPPRESS_IFGROUP]) {
135 int grp = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_IFGROUP]);
136 if (grp != -1)
137 printf("%s %d ", keyword_suppress_ifgroup, grp);
138 }
139
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000140 if (tb[RTA_FLOW]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000141 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
142 uint32_t from = to>>16;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000143 to &= 0xFFFF;
144 if (from) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000145 printf("realms %s/",
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200146 rtnl_rtrealm_n2a(from));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000147 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000148 printf("%s ",
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200149 rtnl_rtrealm_n2a(to));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000150 }
151
152 if (r->rtm_type == RTN_NAT) {
153 if (tb[RTA_GATEWAY]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000154 printf("map-to %s ",
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000155 format_host(r->rtm_family,
156 RTA_PAYLOAD(tb[RTA_GATEWAY]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200157 RTA_DATA(tb[RTA_GATEWAY]))
158 );
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000159 } else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000160 printf("masquerade");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000161 } else if (r->rtm_type != RTN_UNICAST)
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200162 fputs(rtnl_rtntype_n2a(r->rtm_type), stdout);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000163
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000164 bb_putchar('\n');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100165 /*fflush_all();*/
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000166 return 0;
167}
168
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000169/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000170static int iprule_list(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000171{
172 struct rtnl_handle rth;
173 int af = preferred_family;
174
175 if (af == AF_UNSPEC)
176 af = AF_INET;
177
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000178 if (*argv) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000179 //bb_error_msg("\"rule show\" needs no arguments");
Denys Vlasenkoa355da02010-01-04 13:16:08 +0100180 bb_warn_ignoring_args(*argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000181 return -1;
182 }
183
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000184 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000185
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000186 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000187 xrtnl_dump_filter(&rth, print_rule, NULL);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000188
189 return 0;
190}
191
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000192/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000193static int iprule_modify(int cmd, char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000194{
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000195 bool table_ok = 0;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000196 struct rtnl_handle rth;
197 struct {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200198 struct nlmsghdr n;
199 struct rtmsg r;
200 char buf[1024];
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000201 } req;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000202 smalluint key;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000203
204 memset(&req, 0, sizeof(req));
205
206 req.n.nlmsg_type = cmd;
207 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
208 req.n.nlmsg_flags = NLM_F_REQUEST;
209 req.r.rtm_family = preferred_family;
210 req.r.rtm_protocol = RTPROT_BOOT;
Denys Vlasenko45482932016-08-14 02:08:56 +0200211 if (RT_SCOPE_UNIVERSE != 0)
212 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
213 /*req.r.rtm_table = 0; - already is */
214 if (RTN_UNSPEC != 0)
215 req.r.rtm_type = RTN_UNSPEC;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000216
217 if (cmd == RTM_NEWRULE) {
218 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
219 req.r.rtm_type = RTN_UNICAST;
220 }
221
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000222 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000223 key = index_in_substrings(keywords, *argv) + 1;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000224 if (key == 0) /* no match found in keywords array, bail out. */
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200225 invarg_1_to_2(*argv, applet_name);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000226 if (key == ARG_from) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000227 inet_prefix dst;
228 NEXT_ARG();
229 get_prefix(&dst, *argv, req.r.rtm_family);
230 req.r.rtm_src_len = dst.bitlen;
231 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000232 } else if (key == ARG_to) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000233 inet_prefix dst;
234 NEXT_ARG();
235 get_prefix(&dst, *argv, req.r.rtm_family);
236 req.r.rtm_dst_len = dst.bitlen;
237 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000238 } else if (key == ARG_preference ||
239 key == ARG_order ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100240 key == ARG_priority
241 ) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000242 uint32_t pref;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000243 NEXT_ARG();
Stefan Tomanek192dce42017-06-13 19:06:09 +0200244 pref = get_u32(*argv, keyword_preference);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000245 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000246 } else if (key == ARG_tos) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000247 uint32_t tos;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000248 NEXT_ARG();
249 if (rtnl_dsfield_a2n(&tos, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200250 invarg_1_to_2(*argv, "TOS");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000251 req.r.rtm_tos = tos;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000252 } else if (key == ARG_fwmark) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000253 uint32_t fwmark;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000254 NEXT_ARG();
Stefan Tomanek192dce42017-06-13 19:06:09 +0200255 fwmark = get_u32(*argv, keyword_fwmark);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000256 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000257 } else if (key == ARG_realms) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000258 uint32_t realm;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000259 NEXT_ARG();
260 if (get_rt_realms(&realm, *argv))
Stefan Tomanek192dce42017-06-13 19:06:09 +0200261 invarg_1_to_2(*argv, keyword_realms);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000262 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000263 } else if (key == ARG_table ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100264 key == ARG_lookup
265 ) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000266 uint32_t tid;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000267 NEXT_ARG();
268 if (rtnl_rttable_a2n(&tid, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200269 invarg_1_to_2(*argv, "table ID");
Lukasz Nowakb42107f2016-12-13 12:58:31 +0000270 if (tid < 256)
271 req.r.rtm_table = tid;
272 else {
273 req.r.rtm_table = RT_TABLE_UNSPEC;
274 addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
275 }
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000276 table_ok = 1;
Stefan Tomanek192dce42017-06-13 19:06:09 +0200277 } else if (key == ARG_suppress_prefixlength) {
278 int prefix_length;
279 NEXT_ARG();
280 prefix_length = get_u32(*argv, keyword_suppress_prefixlength);
281 addattr32(&req.n, sizeof(req), FRA_SUPPRESS_PREFIXLEN, prefix_length);
282 } else if (key == ARG_suppress_ifgroup) {
283 int grp;
284 NEXT_ARG();
285 grp = get_u32(*argv, keyword_suppress_ifgroup);
286 addattr32(&req.n, sizeof(req), FRA_SUPPRESS_IFGROUP, grp);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000287 } else if (key == ARG_dev ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100288 key == ARG_iif
289 ) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000290 NEXT_ARG();
291 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000292 } else if (key == ARG_nat ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100293 key == ARG_map_to
294 ) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000295 NEXT_ARG();
296 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
297 req.r.rtm_type = RTN_NAT;
298 } else {
299 int type;
300
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000301 if (key == ARG_type) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000302 NEXT_ARG();
303 }
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000304 if (key == ARG_help)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000305 bb_show_usage();
306 if (rtnl_rtntype_a2n(&type, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200307 invarg_1_to_2(*argv, "type");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000308 req.r.rtm_type = type;
309 }
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000310 argv++;
311 }
312
313 if (req.r.rtm_family == AF_UNSPEC)
314 req.r.rtm_family = AF_INET;
315
316 if (!table_ok && cmd == RTM_NEWRULE)
317 req.r.rtm_table = RT_TABLE_MAIN;
318
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000319 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000320
321 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
322 return 2;
323
324 return 0;
325}
326
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000327/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200328int FAST_FUNC do_iprule(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000329{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000330 static const char ip_rule_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000331 "add\0""delete\0""list\0""show\0";
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +0200332 if (*argv) {
Denys Vlasenko9b58fe92013-07-15 05:15:46 +0200333 int cmd = index_in_substrings(ip_rule_commands, *argv);
334 if (cmd < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200335 invarg_1_to_2(*argv, applet_name);
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +0200336 argv++;
337 if (cmd < 2)
338 return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000339 }
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +0200340 return iprule_list(argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000341}