blob: 0ce0dfeef6685a0498c41f23fa7386dce0c40eef [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 */
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000015#include <netinet/in.h>
16#include <netinet/ip.h>
17#include <arpa/inet.h>
18
Stefan Tomanek192dce42017-06-13 19:06:09 +020019/* from <linux/fib_rules.h>: */
20#define FRA_SUPPRESS_IFGROUP 13
21#define FRA_SUPPRESS_PREFIXLEN 14
22
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000023#include "ip_common.h" /* #include "libbb.h" is inside */
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000024#include "rt_names.h"
25#include "utils.h"
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000026
Eugene Rudoyecce3a12017-10-19 00:05:11 +020027#include <linux/version.h>
28/* RTA_TABLE is not a define, can't test with ifdef. */
29/* As a proxy, test which kernels toolchain expects: */
30#define HAVE_RTA_TABLE (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19))
31
Stefan Tomanek192dce42017-06-13 19:06:09 +020032/* If you add stuff here, update iprule_full_usage */
33static const char keywords[] ALIGN1 =
34 "from\0""to\0""preference\0""order\0""priority\0"
35 "tos\0""fwmark\0""realms\0""table\0""lookup\0"
36 "suppress_prefixlength\0""suppress_ifgroup\0"
37 "dev\0""iif\0""nat\0""map-to\0""type\0""help\0"
38 ;
39#define keyword_preference (keywords + sizeof("from") + sizeof("to"))
40#define keyword_fwmark (keyword_preference + sizeof("preference") + sizeof("order") + sizeof("priority") + sizeof("tos"))
41#define keyword_realms (keyword_fwmark + sizeof("fwmark"))
42#define keyword_suppress_prefixlength (keyword_realms + sizeof("realms") + sizeof("table") + sizeof("lookup"))
43#define keyword_suppress_ifgroup (keyword_suppress_prefixlength + sizeof("suppress_prefixlength"))
44enum {
45 ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
46 ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup,
47 ARG_suppress_prefixlength, ARG_suppress_ifgroup,
48 ARG_dev, ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help,
49};
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000050
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020051static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000052 struct nlmsghdr *n, void *arg UNUSED_PARAM)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000053{
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000054 struct rtmsg *r = NLMSG_DATA(n);
55 int len = n->nlmsg_len;
56 int host_len = -1;
57 struct rtattr * tb[RTA_MAX+1];
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000058
59 if (n->nlmsg_type != RTM_NEWRULE)
60 return 0;
61
62 len -= NLMSG_LENGTH(sizeof(*r));
63 if (len < 0)
64 return -1;
65
Denys Vlasenko68ae5422018-02-08 08:42:37 +010066 //memset(tb, 0, sizeof(tb)); - parse_rtattr does this
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000067 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
68
69 if (r->rtm_family == AF_INET)
70 host_len = 32;
71 else if (r->rtm_family == AF_INET6)
72 host_len = 128;
73/* else if (r->rtm_family == AF_DECnet)
74 host_len = 16;
75 else if (r->rtm_family == AF_IPX)
76 host_len = 80;
77*/
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +020078 printf("%u:\t", tb[RTA_PRIORITY] ?
79 *(unsigned*)RTA_DATA(tb[RTA_PRIORITY])
80 : 0);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000081 printf("from ");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000082 if (tb[RTA_SRC]) {
83 if (r->rtm_src_len != host_len) {
Denys Vlasenko926d8012015-10-14 13:56:42 +020084 printf("%s/%u",
85 rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000086 r->rtm_src_len
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010087 );
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000088 } else {
Denis Vlasenko605b20e2007-09-30 16:22:36 +000089 fputs(format_host(r->rtm_family,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010090 RTA_PAYLOAD(tb[RTA_SRC]),
Denys Vlasenko926d8012015-10-14 13:56:42 +020091 RTA_DATA(tb[RTA_SRC])),
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010092 stdout
93 );
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000094 }
95 } else if (r->rtm_src_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000096 printf("0/%d", r->rtm_src_len);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000097 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000098 printf("all");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000099 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000100 bb_putchar(' ');
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000101
102 if (tb[RTA_DST]) {
103 if (r->rtm_dst_len != host_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000104 printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
Denys Vlasenko926d8012015-10-14 13:56:42 +0200105 RTA_DATA(tb[RTA_DST])),
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000106 r->rtm_dst_len
107 );
108 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000109 printf("to %s ", format_host(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000110 RTA_PAYLOAD(tb[RTA_DST]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200111 RTA_DATA(tb[RTA_DST])));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000112 }
113 } else if (r->rtm_dst_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000114 printf("to 0/%d ", r->rtm_dst_len);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000115 }
116
117 if (r->rtm_tos) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200118 printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000119 }
120 if (tb[RTA_PROTOINFO]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000121 printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000122 }
123
124 if (tb[RTA_IIF]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000125 printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000126 }
127
Eugene Rudoyecce3a12017-10-19 00:05:11 +0200128#if HAVE_RTA_TABLE
Lukasz Nowakb42107f2016-12-13 12:58:31 +0000129 if (tb[RTA_TABLE])
130 printf("lookup %s ", rtnl_rttable_n2a(*(uint32_t*)RTA_DATA(tb[RTA_TABLE])));
Eugene Rudoyecce3a12017-10-19 00:05:11 +0200131 else
132#endif
133 if (r->rtm_table)
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200134 printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000135
Stefan Tomanek192dce42017-06-13 19:06:09 +0200136 if (tb[FRA_SUPPRESS_PREFIXLEN]) {
137 int pl = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_PREFIXLEN]);
138 if (pl != -1)
139 printf("%s %d ", keyword_suppress_prefixlength, pl);
140 }
141 if (tb[FRA_SUPPRESS_IFGROUP]) {
142 int grp = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_IFGROUP]);
143 if (grp != -1)
144 printf("%s %d ", keyword_suppress_ifgroup, grp);
145 }
146
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000147 if (tb[RTA_FLOW]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000148 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
149 uint32_t from = to>>16;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000150 to &= 0xFFFF;
151 if (from) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000152 printf("realms %s/",
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200153 rtnl_rtrealm_n2a(from));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000154 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000155 printf("%s ",
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200156 rtnl_rtrealm_n2a(to));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000157 }
158
159 if (r->rtm_type == RTN_NAT) {
160 if (tb[RTA_GATEWAY]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000161 printf("map-to %s ",
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000162 format_host(r->rtm_family,
163 RTA_PAYLOAD(tb[RTA_GATEWAY]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200164 RTA_DATA(tb[RTA_GATEWAY]))
165 );
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000166 } else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000167 printf("masquerade");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000168 } else if (r->rtm_type != RTN_UNICAST)
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200169 fputs(rtnl_rtntype_n2a(r->rtm_type), stdout);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000170
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000171 bb_putchar('\n');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100172 /*fflush_all();*/
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000173 return 0;
174}
175
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000176/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000177static int iprule_list(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000178{
179 struct rtnl_handle rth;
180 int af = preferred_family;
181
182 if (af == AF_UNSPEC)
183 af = AF_INET;
184
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000185 if (*argv) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000186 //bb_error_msg("\"rule show\" needs no arguments");
Denys Vlasenkoa355da02010-01-04 13:16:08 +0100187 bb_warn_ignoring_args(*argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000188 return -1;
189 }
190
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000191 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000192
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000193 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000194 xrtnl_dump_filter(&rth, print_rule, NULL);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000195
196 return 0;
197}
198
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000199/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000200static int iprule_modify(int cmd, char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000201{
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000202 bool table_ok = 0;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000203 struct rtnl_handle rth;
204 struct {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200205 struct nlmsghdr n;
206 struct rtmsg r;
207 char buf[1024];
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000208 } req;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000209 smalluint key;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000210
211 memset(&req, 0, sizeof(req));
212
213 req.n.nlmsg_type = cmd;
214 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
215 req.n.nlmsg_flags = NLM_F_REQUEST;
216 req.r.rtm_family = preferred_family;
217 req.r.rtm_protocol = RTPROT_BOOT;
Denys Vlasenko45482932016-08-14 02:08:56 +0200218 if (RT_SCOPE_UNIVERSE != 0)
219 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
220 /*req.r.rtm_table = 0; - already is */
221 if (RTN_UNSPEC != 0)
222 req.r.rtm_type = RTN_UNSPEC;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000223
224 if (cmd == RTM_NEWRULE) {
225 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
226 req.r.rtm_type = RTN_UNICAST;
227 }
228
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000229 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000230 key = index_in_substrings(keywords, *argv) + 1;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000231 if (key == 0) /* no match found in keywords array, bail out. */
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200232 invarg_1_to_2(*argv, applet_name);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000233 if (key == ARG_from) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000234 inet_prefix dst;
235 NEXT_ARG();
236 get_prefix(&dst, *argv, req.r.rtm_family);
237 req.r.rtm_src_len = dst.bitlen;
238 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000239 } else if (key == ARG_to) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000240 inet_prefix dst;
241 NEXT_ARG();
242 get_prefix(&dst, *argv, req.r.rtm_family);
243 req.r.rtm_dst_len = dst.bitlen;
244 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000245 } else if (key == ARG_preference ||
246 key == ARG_order ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100247 key == ARG_priority
248 ) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000249 uint32_t pref;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000250 NEXT_ARG();
Stefan Tomanek192dce42017-06-13 19:06:09 +0200251 pref = get_u32(*argv, keyword_preference);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000252 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000253 } else if (key == ARG_tos) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000254 uint32_t tos;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000255 NEXT_ARG();
256 if (rtnl_dsfield_a2n(&tos, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200257 invarg_1_to_2(*argv, "TOS");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000258 req.r.rtm_tos = tos;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000259 } else if (key == ARG_fwmark) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000260 uint32_t fwmark;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000261 NEXT_ARG();
Stefan Tomanek192dce42017-06-13 19:06:09 +0200262 fwmark = get_u32(*argv, keyword_fwmark);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000263 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000264 } else if (key == ARG_realms) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000265 uint32_t realm;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000266 NEXT_ARG();
267 if (get_rt_realms(&realm, *argv))
Stefan Tomanek192dce42017-06-13 19:06:09 +0200268 invarg_1_to_2(*argv, keyword_realms);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000269 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000270 } else if (key == ARG_table ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100271 key == ARG_lookup
272 ) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000273 uint32_t tid;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000274 NEXT_ARG();
275 if (rtnl_rttable_a2n(&tid, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200276 invarg_1_to_2(*argv, "table ID");
Eugene Rudoyecce3a12017-10-19 00:05:11 +0200277
278#if HAVE_RTA_TABLE
279 if (tid > 255) {
Lukasz Nowakb42107f2016-12-13 12:58:31 +0000280 req.r.rtm_table = RT_TABLE_UNSPEC;
281 addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
Eugene Rudoyecce3a12017-10-19 00:05:11 +0200282 } else
283#endif
284 req.r.rtm_table = tid;
285
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000286 table_ok = 1;
Stefan Tomanek192dce42017-06-13 19:06:09 +0200287 } else if (key == ARG_suppress_prefixlength) {
288 int prefix_length;
289 NEXT_ARG();
290 prefix_length = get_u32(*argv, keyword_suppress_prefixlength);
291 addattr32(&req.n, sizeof(req), FRA_SUPPRESS_PREFIXLEN, prefix_length);
292 } else if (key == ARG_suppress_ifgroup) {
293 int grp;
294 NEXT_ARG();
295 grp = get_u32(*argv, keyword_suppress_ifgroup);
296 addattr32(&req.n, sizeof(req), FRA_SUPPRESS_IFGROUP, grp);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000297 } else if (key == ARG_dev ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100298 key == ARG_iif
299 ) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000300 NEXT_ARG();
301 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000302 } else if (key == ARG_nat ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100303 key == ARG_map_to
304 ) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000305 NEXT_ARG();
306 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
307 req.r.rtm_type = RTN_NAT;
308 } else {
309 int type;
310
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000311 if (key == ARG_type) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000312 NEXT_ARG();
313 }
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000314 if (key == ARG_help)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000315 bb_show_usage();
316 if (rtnl_rtntype_a2n(&type, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200317 invarg_1_to_2(*argv, "type");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000318 req.r.rtm_type = type;
319 }
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000320 argv++;
321 }
322
323 if (req.r.rtm_family == AF_UNSPEC)
324 req.r.rtm_family = AF_INET;
325
326 if (!table_ok && cmd == RTM_NEWRULE)
327 req.r.rtm_table = RT_TABLE_MAIN;
328
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000329 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000330
331 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
332 return 2;
333
334 return 0;
335}
336
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000337/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200338int FAST_FUNC do_iprule(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000339{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000340 static const char ip_rule_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000341 "add\0""delete\0""list\0""show\0";
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +0200342 if (*argv) {
Denys Vlasenko9b58fe92013-07-15 05:15:46 +0200343 int cmd = index_in_substrings(ip_rule_commands, *argv);
344 if (cmd < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200345 invarg_1_to_2(*argv, applet_name);
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +0200346 argv++;
347 if (cmd < 2)
348 return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000349 }
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +0200350 return iprule_list(argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000351}