blob: 9938b4793f10508c56337b0eb2b83ac698ac482f [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
Stefan Tomanek192dce42017-06-13 19:06:09 +020027/* If you add stuff here, update iprule_full_usage */
28static const char keywords[] ALIGN1 =
29 "from\0""to\0""preference\0""order\0""priority\0"
30 "tos\0""fwmark\0""realms\0""table\0""lookup\0"
31 "suppress_prefixlength\0""suppress_ifgroup\0"
32 "dev\0""iif\0""nat\0""map-to\0""type\0""help\0"
33 ;
34#define keyword_preference (keywords + sizeof("from") + sizeof("to"))
35#define keyword_fwmark (keyword_preference + sizeof("preference") + sizeof("order") + sizeof("priority") + sizeof("tos"))
36#define keyword_realms (keyword_fwmark + sizeof("fwmark"))
37#define keyword_suppress_prefixlength (keyword_realms + sizeof("realms") + sizeof("table") + sizeof("lookup"))
38#define keyword_suppress_ifgroup (keyword_suppress_prefixlength + sizeof("suppress_prefixlength"))
39enum {
40 ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
41 ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup,
42 ARG_suppress_prefixlength, ARG_suppress_ifgroup,
43 ARG_dev, ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help,
44};
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000045
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020046static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000047 struct nlmsghdr *n, void *arg UNUSED_PARAM)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000048{
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000049 struct rtmsg *r = NLMSG_DATA(n);
50 int len = n->nlmsg_len;
51 int host_len = -1;
52 struct rtattr * tb[RTA_MAX+1];
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000053
54 if (n->nlmsg_type != RTM_NEWRULE)
55 return 0;
56
57 len -= NLMSG_LENGTH(sizeof(*r));
58 if (len < 0)
59 return -1;
60
61 memset(tb, 0, sizeof(tb));
62 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
63
64 if (r->rtm_family == AF_INET)
65 host_len = 32;
66 else if (r->rtm_family == AF_INET6)
67 host_len = 128;
68/* else if (r->rtm_family == AF_DECnet)
69 host_len = 16;
70 else if (r->rtm_family == AF_IPX)
71 host_len = 80;
72*/
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +020073 printf("%u:\t", tb[RTA_PRIORITY] ?
74 *(unsigned*)RTA_DATA(tb[RTA_PRIORITY])
75 : 0);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000076 printf("from ");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000077 if (tb[RTA_SRC]) {
78 if (r->rtm_src_len != host_len) {
Denys Vlasenko926d8012015-10-14 13:56:42 +020079 printf("%s/%u",
80 rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000081 r->rtm_src_len
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010082 );
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000083 } else {
Denis Vlasenko605b20e2007-09-30 16:22:36 +000084 fputs(format_host(r->rtm_family,
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010085 RTA_PAYLOAD(tb[RTA_SRC]),
Denys Vlasenko926d8012015-10-14 13:56:42 +020086 RTA_DATA(tb[RTA_SRC])),
Denys Vlasenko60cb48c2013-01-14 15:57:44 +010087 stdout
88 );
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000089 }
90 } else if (r->rtm_src_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000091 printf("0/%d", r->rtm_src_len);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000092 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000093 printf("all");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000094 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000095 bb_putchar(' ');
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +000096
97 if (tb[RTA_DST]) {
98 if (r->rtm_dst_len != host_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +000099 printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
Denys Vlasenko926d8012015-10-14 13:56:42 +0200100 RTA_DATA(tb[RTA_DST])),
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000101 r->rtm_dst_len
102 );
103 } else {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000104 printf("to %s ", format_host(r->rtm_family,
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000105 RTA_PAYLOAD(tb[RTA_DST]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200106 RTA_DATA(tb[RTA_DST])));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000107 }
108 } else if (r->rtm_dst_len) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000109 printf("to 0/%d ", r->rtm_dst_len);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000110 }
111
112 if (r->rtm_tos) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200113 printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000114 }
115 if (tb[RTA_PROTOINFO]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000116 printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000117 }
118
119 if (tb[RTA_IIF]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000120 printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000121 }
122
Lukasz Nowakb42107f2016-12-13 12:58:31 +0000123 if (tb[RTA_TABLE])
124 printf("lookup %s ", rtnl_rttable_n2a(*(uint32_t*)RTA_DATA(tb[RTA_TABLE])));
125 else if (r->rtm_table)
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200126 printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000127
Stefan Tomanek192dce42017-06-13 19:06:09 +0200128 if (tb[FRA_SUPPRESS_PREFIXLEN]) {
129 int pl = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_PREFIXLEN]);
130 if (pl != -1)
131 printf("%s %d ", keyword_suppress_prefixlength, pl);
132 }
133 if (tb[FRA_SUPPRESS_IFGROUP]) {
134 int grp = *(uint32_t*)RTA_DATA(tb[FRA_SUPPRESS_IFGROUP]);
135 if (grp != -1)
136 printf("%s %d ", keyword_suppress_ifgroup, grp);
137 }
138
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000139 if (tb[RTA_FLOW]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000140 uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
141 uint32_t from = to>>16;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000142 to &= 0xFFFF;
143 if (from) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000144 printf("realms %s/",
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200145 rtnl_rtrealm_n2a(from));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000146 }
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000147 printf("%s ",
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200148 rtnl_rtrealm_n2a(to));
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000149 }
150
151 if (r->rtm_type == RTN_NAT) {
152 if (tb[RTA_GATEWAY]) {
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000153 printf("map-to %s ",
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000154 format_host(r->rtm_family,
155 RTA_PAYLOAD(tb[RTA_GATEWAY]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200156 RTA_DATA(tb[RTA_GATEWAY]))
157 );
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000158 } else
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000159 printf("masquerade");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000160 } else if (r->rtm_type != RTN_UNICAST)
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200161 fputs(rtnl_rtntype_n2a(r->rtm_type), stdout);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000162
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000163 bb_putchar('\n');
Denys Vlasenko8131eea2009-11-02 14:19:51 +0100164 /*fflush_all();*/
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000165 return 0;
166}
167
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000168/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000169static int iprule_list(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000170{
171 struct rtnl_handle rth;
172 int af = preferred_family;
173
174 if (af == AF_UNSPEC)
175 af = AF_INET;
176
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000177 if (*argv) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000178 //bb_error_msg("\"rule show\" needs no arguments");
Denys Vlasenkoa355da02010-01-04 13:16:08 +0100179 bb_warn_ignoring_args(*argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000180 return -1;
181 }
182
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000183 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000184
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000185 xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
Denis Vlasenkobedfabd2008-06-05 05:00:24 +0000186 xrtnl_dump_filter(&rth, print_rule, NULL);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000187
188 return 0;
189}
190
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000191/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000192static int iprule_modify(int cmd, char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000193{
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000194 bool table_ok = 0;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000195 struct rtnl_handle rth;
196 struct {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200197 struct nlmsghdr n;
198 struct rtmsg r;
199 char buf[1024];
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000200 } req;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000201 smalluint key;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000202
203 memset(&req, 0, sizeof(req));
204
205 req.n.nlmsg_type = cmd;
206 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
207 req.n.nlmsg_flags = NLM_F_REQUEST;
208 req.r.rtm_family = preferred_family;
209 req.r.rtm_protocol = RTPROT_BOOT;
Denys Vlasenko45482932016-08-14 02:08:56 +0200210 if (RT_SCOPE_UNIVERSE != 0)
211 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
212 /*req.r.rtm_table = 0; - already is */
213 if (RTN_UNSPEC != 0)
214 req.r.rtm_type = RTN_UNSPEC;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000215
216 if (cmd == RTM_NEWRULE) {
217 req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
218 req.r.rtm_type = RTN_UNICAST;
219 }
220
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000221 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000222 key = index_in_substrings(keywords, *argv) + 1;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000223 if (key == 0) /* no match found in keywords array, bail out. */
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200224 invarg_1_to_2(*argv, applet_name);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000225 if (key == ARG_from) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000226 inet_prefix dst;
227 NEXT_ARG();
228 get_prefix(&dst, *argv, req.r.rtm_family);
229 req.r.rtm_src_len = dst.bitlen;
230 addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000231 } else if (key == ARG_to) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000232 inet_prefix dst;
233 NEXT_ARG();
234 get_prefix(&dst, *argv, req.r.rtm_family);
235 req.r.rtm_dst_len = dst.bitlen;
236 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000237 } else if (key == ARG_preference ||
238 key == ARG_order ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100239 key == ARG_priority
240 ) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000241 uint32_t pref;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000242 NEXT_ARG();
Stefan Tomanek192dce42017-06-13 19:06:09 +0200243 pref = get_u32(*argv, keyword_preference);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000244 addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000245 } else if (key == ARG_tos) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000246 uint32_t tos;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000247 NEXT_ARG();
248 if (rtnl_dsfield_a2n(&tos, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200249 invarg_1_to_2(*argv, "TOS");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000250 req.r.rtm_tos = tos;
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000251 } else if (key == ARG_fwmark) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000252 uint32_t fwmark;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000253 NEXT_ARG();
Stefan Tomanek192dce42017-06-13 19:06:09 +0200254 fwmark = get_u32(*argv, keyword_fwmark);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000255 addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000256 } else if (key == ARG_realms) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000257 uint32_t realm;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000258 NEXT_ARG();
259 if (get_rt_realms(&realm, *argv))
Stefan Tomanek192dce42017-06-13 19:06:09 +0200260 invarg_1_to_2(*argv, keyword_realms);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000261 addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000262 } else if (key == ARG_table ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100263 key == ARG_lookup
264 ) {
Bernhard Reutner-Fischer4d9a3582007-01-28 00:20:46 +0000265 uint32_t tid;
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000266 NEXT_ARG();
267 if (rtnl_rttable_a2n(&tid, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200268 invarg_1_to_2(*argv, "table ID");
Lukasz Nowakb42107f2016-12-13 12:58:31 +0000269 if (tid < 256)
270 req.r.rtm_table = tid;
271 else {
272 req.r.rtm_table = RT_TABLE_UNSPEC;
273 addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
274 }
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000275 table_ok = 1;
Stefan Tomanek192dce42017-06-13 19:06:09 +0200276 } else if (key == ARG_suppress_prefixlength) {
277 int prefix_length;
278 NEXT_ARG();
279 prefix_length = get_u32(*argv, keyword_suppress_prefixlength);
280 addattr32(&req.n, sizeof(req), FRA_SUPPRESS_PREFIXLEN, prefix_length);
281 } else if (key == ARG_suppress_ifgroup) {
282 int grp;
283 NEXT_ARG();
284 grp = get_u32(*argv, keyword_suppress_ifgroup);
285 addattr32(&req.n, sizeof(req), FRA_SUPPRESS_IFGROUP, grp);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000286 } else if (key == ARG_dev ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100287 key == ARG_iif
288 ) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000289 NEXT_ARG();
290 addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000291 } else if (key == ARG_nat ||
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100292 key == ARG_map_to
293 ) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000294 NEXT_ARG();
295 addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
296 req.r.rtm_type = RTN_NAT;
297 } else {
298 int type;
299
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000300 if (key == ARG_type) {
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000301 NEXT_ARG();
302 }
Bernhard Reutner-Fischercd0e80c2007-06-20 14:53:49 +0000303 if (key == ARG_help)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000304 bb_show_usage();
305 if (rtnl_rtntype_a2n(&type, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200306 invarg_1_to_2(*argv, "type");
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000307 req.r.rtm_type = type;
308 }
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000309 argv++;
310 }
311
312 if (req.r.rtm_family == AF_UNSPEC)
313 req.r.rtm_family = AF_INET;
314
315 if (!table_ok && cmd == RTM_NEWRULE)
316 req.r.rtm_table = RT_TABLE_MAIN;
317
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000318 xrtnl_open(&rth);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000319
320 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
321 return 2;
322
323 return 0;
324}
325
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000326/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200327int FAST_FUNC do_iprule(char **argv)
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000328{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000329 static const char ip_rule_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000330 "add\0""delete\0""list\0""show\0";
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +0200331 if (*argv) {
Denys Vlasenko9b58fe92013-07-15 05:15:46 +0200332 int cmd = index_in_substrings(ip_rule_commands, *argv);
333 if (cmd < 0)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200334 invarg_1_to_2(*argv, applet_name);
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +0200335 argv++;
336 if (cmd < 2)
337 return iprule_modify((cmd == 0) ? RTM_NEWRULE : RTM_DELRULE, argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000338 }
Bernhard Reutner-Fischer1b354982010-05-25 17:22:17 +0200339 return iprule_list(argv);
Bernhard Reutner-Fischer921f5df2006-11-21 15:36:08 +0000340}