blob: e674e9a0d914b0c8df01a6c5942135a9324dc26d [file] [log] [blame]
Bernhard Reutner-Fischerd1d23a62006-01-12 12:08:46 +00001/* vi: set sw=4 ts=4: */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00002/*
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02003 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00004 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02005 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
7 * Changes:
8 *
Denys Vlasenkofb132e42010-10-29 11:46:52 +02009 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000010 * Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized
11 */
12
Denys Vlasenkofb132e42010-10-29 11:46:52 +020013#include "ip_common.h" /* #include "libbb.h" is inside */
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020014#include "common_bufsiz.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000015#include "rt_names.h"
16#include "utils.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000018#ifndef RTAX_RTTVAR
19#define RTAX_RTTVAR RTAX_HOPS
20#endif
21
22
Denys Vlasenko36659fd2010-02-05 14:40:23 +010023struct filter_t {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000024 int tb;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +000025 smallint flushed;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000026 char *flushb;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000027 int flushp;
28 int flushe;
29 struct rtnl_handle *rth;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010030 //int protocol, protocolmask; - write-only fields?!
31 //int scope, scopemask; - unused
32 //int type; - read-only
33 //int typemask; - unused
34 //int tos, tosmask; - unused
Denys Vlasenkof1334712011-02-09 04:39:09 +010035 int iif;
36 int oif;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010037 //int realm, realmmask; - unused
38 //inet_prefix rprefsrc; - read-only
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000039 inet_prefix rvia;
40 inet_prefix rdst;
41 inet_prefix mdst;
42 inet_prefix rsrc;
43 inet_prefix msrc;
Denys Vlasenko36659fd2010-02-05 14:40:23 +010044} FIX_ALIASING;
45typedef struct filter_t filter_t;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000046
Denys Vlasenkoe6a2f4c2016-04-21 16:26:30 +020047#define G_filter (*(filter_t*)bb_common_bufsiz1)
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +020048#define INIT_G() do { setup_common_bufsiz(); } while (0)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000049
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000050static int flush_update(void)
51{
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010052 if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
Denys Vlasenko651a2692010-03-23 16:25:17 +010053 bb_perror_msg("can't send flush request");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000054 return -1;
55 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010056 G_filter.flushp = 0;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000057 return 0;
58}
59
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020060static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000061 struct nlmsghdr *n, void *arg UNUSED_PARAM)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000062{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000063 struct rtmsg *r = NLMSG_DATA(n);
64 int len = n->nlmsg_len;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +010065 struct rtattr *tb[RTA_MAX+1];
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +000066 inet_prefix dst;
67 inet_prefix src;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068 int host_len = -1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000069
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000070 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
71 fprintf(stderr, "Not a route: %08x %08x %08x\n",
72 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
73 return 0;
74 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010075 if (G_filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000076 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000077 len -= NLMSG_LENGTH(sizeof(*r));
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000078 if (len < 0)
79 bb_error_msg_and_die("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000080
81 if (r->rtm_family == AF_INET6)
82 host_len = 128;
83 else if (r->rtm_family == AF_INET)
84 host_len = 32;
85
86 if (r->rtm_family == AF_INET6) {
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010087 if (G_filter.tb) {
88 if (G_filter.tb < 0) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +000089 if (!(r->rtm_flags & RTM_F_CLONED)) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +000091 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000092 } else {
Denis Vlasenko186c2b32007-11-26 18:29:52 +000093 if (r->rtm_flags & RTM_F_CLONED) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000094 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +000095 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010096 if (G_filter.tb == RT_TABLE_LOCAL) {
Glenn L McGrath16528552002-11-28 11:17:19 +000097 if (r->rtm_type != RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000098 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +000099 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100100 } else if (G_filter.tb == RT_TABLE_MAIN) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000101 if (r->rtm_type == RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000103 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000104 } else {
105 return 0;
106 }
107 }
108 }
109 } else {
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100110 if (G_filter.tb > 0 && G_filter.tb != r->rtm_table) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000111 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000112 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000113 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100114 if (G_filter.rdst.family
115 && (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len)
116 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000117 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000118 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100119 if (G_filter.mdst.family
120 && (r->rtm_family != G_filter.mdst.family
121 || (G_filter.mdst.bitlen >= 0 && G_filter.mdst.bitlen < r->rtm_dst_len)
122 )
123 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000124 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000125 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100126 if (G_filter.rsrc.family
127 && (r->rtm_family != G_filter.rsrc.family || G_filter.rsrc.bitlen > r->rtm_src_len)
128 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000129 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000130 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100131 if (G_filter.msrc.family
132 && (r->rtm_family != G_filter.msrc.family
133 || (G_filter.msrc.bitlen >= 0 && G_filter.msrc.bitlen < r->rtm_src_len)
134 )
135 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000137 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000138
139 memset(tb, 0, sizeof(tb));
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100140 memset(&src, 0, sizeof(src));
141 memset(&dst, 0, sizeof(dst));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000142 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
143
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100144 if (tb[RTA_SRC]) {
145 src.bitlen = r->rtm_src_len;
146 src.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
147 memcpy(src.data, RTA_DATA(tb[RTA_SRC]), src.bytelen);
148 }
149 if (tb[RTA_DST]) {
150 dst.bitlen = r->rtm_dst_len;
151 dst.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
152 memcpy(dst.data, RTA_DATA(tb[RTA_DST]), dst.bytelen);
153 }
154
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100155 if (G_filter.rdst.family
156 && inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen)
157 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000158 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100159 }
160 if (G_filter.mdst.family
161 && G_filter.mdst.bitlen >= 0
162 && inet_addr_match(&dst, &G_filter.mdst, r->rtm_dst_len)
163 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000164 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100165 }
166 if (G_filter.rsrc.family
167 && inet_addr_match(&src, &G_filter.rsrc, G_filter.rsrc.bitlen)
168 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000169 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100170 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100171 if (G_filter.msrc.family && G_filter.msrc.bitlen >= 0
172 && inet_addr_match(&src, &G_filter.msrc, r->rtm_src_len)
173 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000174 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100175 }
Denys Vlasenkof1334712011-02-09 04:39:09 +0100176 if (G_filter.oif != 0) {
177 if (!tb[RTA_OIF])
178 return 0;
179 if (G_filter.oif != *(int*)RTA_DATA(tb[RTA_OIF]))
180 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100181 }
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000182
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100183 if (G_filter.flushb) {
Denys Vlasenkoe3ece782011-02-10 09:50:07 +0100184 struct nlmsghdr *fn;
185
Denys Vlasenkof1334712011-02-09 04:39:09 +0100186 /* We are creating route flush commands */
187
188 if (r->rtm_family == AF_INET6
189 && r->rtm_dst_len == 0
190 && r->rtm_type == RTN_UNREACHABLE
191 && tb[RTA_PRIORITY]
192 && *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1
193 ) {
194 return 0;
195 }
196
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100197 if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000198 if (flush_update())
Curt Brune69934702015-10-14 12:53:47 +0200199 xfunc_die();
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000200 }
Denys Vlasenkoe3ece782011-02-10 09:50:07 +0100201 fn = (void*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000202 memcpy(fn, n, n->nlmsg_len);
203 fn->nlmsg_type = RTM_DELROUTE;
204 fn->nlmsg_flags = NLM_F_REQUEST;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100205 fn->nlmsg_seq = ++G_filter.rth->seq;
206 G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
207 G_filter.flushed = 1;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000208 return 0;
209 }
210
Denys Vlasenkof1334712011-02-09 04:39:09 +0100211 /* We are printing routes */
212
Glenn L McGrath16528552002-11-28 11:17:19 +0000213 if (n->nlmsg_type == RTM_DELROUTE) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000214 printf("Deleted ");
Glenn L McGrath16528552002-11-28 11:17:19 +0000215 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100216 if (r->rtm_type != RTN_UNICAST /* && !G_filter.type - always 0 */) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200217 printf("%s ", rtnl_rtntype_n2a(r->rtm_type));
Glenn L McGrath16528552002-11-28 11:17:19 +0000218 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000219
220 if (tb[RTA_DST]) {
221 if (r->rtm_dst_len != host_len) {
Denys Vlasenko926d8012015-10-14 13:56:42 +0200222 printf("%s/%u ",
223 rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_DST])),
224 r->rtm_dst_len
225 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000226 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000227 printf("%s ", format_host(r->rtm_family,
228 RTA_PAYLOAD(tb[RTA_DST]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200229 RTA_DATA(tb[RTA_DST]))
230 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000231 }
232 } else if (r->rtm_dst_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000233 printf("0/%d ", r->rtm_dst_len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000235 printf("default ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000236 }
237 if (tb[RTA_SRC]) {
238 if (r->rtm_src_len != host_len) {
Denys Vlasenko926d8012015-10-14 13:56:42 +0200239 printf("from %s/%u ",
240 rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
241 r->rtm_src_len
242 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000243 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000244 printf("from %s ", format_host(r->rtm_family,
245 RTA_PAYLOAD(tb[RTA_SRC]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200246 RTA_DATA(tb[RTA_SRC]))
247 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000248 }
249 } else if (r->rtm_src_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000250 printf("from 0/%u ", r->rtm_src_len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000251 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100252 if (tb[RTA_GATEWAY] && G_filter.rvia.bitlen != host_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000253 printf("via %s ", format_host(r->rtm_family,
254 RTA_PAYLOAD(tb[RTA_GATEWAY]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200255 RTA_DATA(tb[RTA_GATEWAY]))
256 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000257 }
Denys Vlasenkof1334712011-02-09 04:39:09 +0100258 if (tb[RTA_OIF]) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000259 printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
Glenn L McGrath16528552002-11-28 11:17:19 +0000260 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000261
Denys Vlasenkof1334712011-02-09 04:39:09 +0100262 /* Todo: parse & show "proto kernel", "scope link" here */
263
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100264 if (tb[RTA_PREFSRC] && /*G_filter.rprefsrc.bitlen - always 0*/ 0 != host_len) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000265 /* Do not use format_host(). It is our local addr
266 and symbolic name will not be useful.
267 */
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000268 printf(" src %s ", rt_addr_n2a(r->rtm_family,
Denys Vlasenko926d8012015-10-14 13:56:42 +0200269 RTA_DATA(tb[RTA_PREFSRC])));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000270 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000271 if (tb[RTA_PRIORITY]) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000272 printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
Glenn L McGrath16528552002-11-28 11:17:19 +0000273 }
Michael Tokarev1a114392014-07-28 10:05:41 +0400274 if (r->rtm_flags & RTNH_F_DEAD) {
275 printf("dead ");
276 }
277 if (r->rtm_flags & RTNH_F_ONLINK) {
278 printf("onlink ");
279 }
280 if (r->rtm_flags & RTNH_F_PERVASIVE) {
281 printf("pervasive ");
282 }
283 if (r->rtm_flags & RTM_F_NOTIFY) {
284 printf("notify ");
285 }
286
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000287 if (r->rtm_family == AF_INET6) {
288 struct rta_cacheinfo *ci = NULL;
Glenn L McGrath16528552002-11-28 11:17:19 +0000289 if (tb[RTA_CACHEINFO]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000290 ci = RTA_DATA(tb[RTA_CACHEINFO]);
Glenn L McGrath16528552002-11-28 11:17:19 +0000291 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000292 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000293 if (r->rtm_flags & RTM_F_CLONED) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000294 printf("%c cache ", _SL_);
Glenn L McGrath16528552002-11-28 11:17:19 +0000295 }
296 if (ci->rta_expires) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000297 printf(" expires %dsec", ci->rta_expires / get_hz());
Glenn L McGrath16528552002-11-28 11:17:19 +0000298 }
299 if (ci->rta_error != 0) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000300 printf(" error %d", ci->rta_error);
Glenn L McGrath16528552002-11-28 11:17:19 +0000301 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000302 } else if (ci) {
303 if (ci->rta_error != 0)
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000304 printf(" error %d", ci->rta_error);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000305 }
306 }
Denys Vlasenkof1334712011-02-09 04:39:09 +0100307 if (tb[RTA_IIF] && G_filter.iif == 0) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000308 printf(" iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000309 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000310 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000311 return 0;
312}
313
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000314/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000315static int iproute_modify(int cmd, unsigned flags, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000316{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000317 static const char keywords[] ALIGN1 =
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100318 "src\0""via\0""mtu\0""lock\0""scope\0""protocol\0"IF_FEATURE_IP_RULE("table\0")
Michael Tokarev1a114392014-07-28 10:05:41 +0400319 "dev\0""oif\0""to\0""metric\0""onlink\0";
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000320 enum {
321 ARG_src,
322 ARG_via,
323 ARG_mtu, PARM_lock,
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100324 ARG_scope,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000325 ARG_protocol,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000326IF_FEATURE_IP_RULE(ARG_table,)
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000327 ARG_dev,
328 ARG_oif,
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000329 ARG_to,
330 ARG_metric,
Michael Tokarev1a114392014-07-28 10:05:41 +0400331 ARG_onlink,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000332 };
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000333 enum {
334 gw_ok = 1 << 0,
335 dst_ok = 1 << 1,
336 proto_ok = 1 << 2,
337 type_ok = 1 << 3
338 };
339 struct rtnl_handle rth;
340 struct {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200341 struct nlmsghdr n;
342 struct rtmsg r;
343 char buf[1024];
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000344 } req;
345 char mxbuf[256];
346 struct rtattr * mxrta = (void*)mxbuf;
347 unsigned mxlock = 0;
348 char *d = NULL;
349 smalluint ok = 0;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100350 smalluint scope_ok = 0;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000351 int arg;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000352
353 memset(&req, 0, sizeof(req));
354
355 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000356 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000357 req.n.nlmsg_type = cmd;
358 req.r.rtm_family = preferred_family;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100359 if (RT_TABLE_MAIN != 0) /* if it is zero, memset already did it */
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000360 req.r.rtm_table = RT_TABLE_MAIN;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100361 if (RT_SCOPE_NOWHERE != 0)
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000362 req.r.rtm_scope = RT_SCOPE_NOWHERE;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000363
364 if (cmd != RTM_DELROUTE) {
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100365 if (RTPROT_BOOT != 0)
366 req.r.rtm_protocol = RTPROT_BOOT;
367 if (RT_SCOPE_UNIVERSE != 0)
368 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
369 if (RTN_UNICAST != 0)
370 req.r.rtm_type = RTN_UNICAST;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000371 }
372
373 mxrta->rta_type = RTA_METRICS;
374 mxrta->rta_len = RTA_LENGTH(0);
375
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000376 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000377 arg = index_in_substrings(keywords, *argv);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000378 if (arg == ARG_src) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000379 inet_prefix addr;
380 NEXT_ARG();
381 get_addr(&addr, *argv, req.r.rtm_family);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000382 if (req.r.rtm_family == AF_UNSPEC)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000383 req.r.rtm_family = addr.family;
384 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000385 } else if (arg == ARG_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000386 inet_prefix addr;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000387 ok |= gw_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000388 NEXT_ARG();
389 get_addr(&addr, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000390 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000391 req.r.rtm_family = addr.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000392 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000393 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000394 } else if (arg == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000395 unsigned mtu;
396 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000397 if (index_in_strings(keywords, *argv) == PARM_lock) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000398 mxlock |= (1 << RTAX_MTU);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000399 NEXT_ARG();
400 }
Denis Vlasenko76140a72009-03-05 09:21:57 +0000401 mtu = get_unsigned(*argv, "mtu");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000402 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100403 } else if (arg == ARG_scope) {
404 uint32_t scope;
405 NEXT_ARG();
406 if (rtnl_rtscope_a2n(&scope, *argv))
407 invarg_1_to_2(*argv, "scope");
408 req.r.rtm_scope = scope;
409 scope_ok = 1;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000410 } else if (arg == ARG_protocol) {
Eric Andersend78aea82006-01-30 18:00:02 +0000411 uint32_t prot;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000412 NEXT_ARG();
413 if (rtnl_rtprot_a2n(&prot, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200414 invarg_1_to_2(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000415 req.r.rtm_protocol = prot;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000416 ok |= proto_ok;
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000417#if ENABLE_FEATURE_IP_RULE
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000418 } else if (arg == ARG_table) {
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000419 uint32_t tid;
420 NEXT_ARG();
421 if (rtnl_rttable_a2n(&tid, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200422 invarg_1_to_2(*argv, "table");
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000423 req.r.rtm_table = tid;
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000424#endif
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000425 } else if (arg == ARG_dev || arg == ARG_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000426 NEXT_ARG();
427 d = *argv;
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000428 } else if (arg == ARG_metric) {
429 uint32_t metric;
430 NEXT_ARG();
Denis Vlasenko76140a72009-03-05 09:21:57 +0000431 metric = get_u32(*argv, "metric");
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000432 addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
Michael Tokarev1a114392014-07-28 10:05:41 +0400433 } else if (arg == ARG_onlink) {
434 req.r.rtm_flags |= RTNH_F_ONLINK;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000435 } else {
436 int type;
437 inet_prefix dst;
438
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000439 if (arg == ARG_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000440 NEXT_ARG();
441 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000442 if ((**argv < '0' || **argv > '9')
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100443 && rtnl_rtntype_a2n(&type, *argv) == 0
444 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000445 NEXT_ARG();
446 req.r.rtm_type = type;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000447 ok |= type_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000448 }
449
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000450 if (ok & dst_ok) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000451 duparg2("to", *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000452 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000453 get_prefix(&dst, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000454 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000455 req.r.rtm_family = dst.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000456 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000457 req.r.rtm_dst_len = dst.bitlen;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000458 ok |= dst_ok;
Glenn L McGrath16528552002-11-28 11:17:19 +0000459 if (dst.bytelen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000460 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Glenn L McGrath16528552002-11-28 11:17:19 +0000461 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000462 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000463 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000464 }
465
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000466 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000467
Eric Andersen5780adb2002-11-15 09:12:47 +0000468 if (d) {
469 int idx;
470
471 ll_init_map(&rth);
472
473 if (d) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000474 idx = xll_name_to_index(d);
Eric Andersen5780adb2002-11-15 09:12:47 +0000475 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
476 }
477 }
478
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000479 if (mxrta->rta_len > RTA_LENGTH(0)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000480 if (mxlock) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000481 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
Glenn L McGrath16528552002-11-28 11:17:19 +0000482 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000483 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
484 }
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000485
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100486 if (!scope_ok) {
487 if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
488 req.r.rtm_scope = RT_SCOPE_HOST;
489 else
490 if (req.r.rtm_type == RTN_BROADCAST
491 || req.r.rtm_type == RTN_MULTICAST
492 || req.r.rtm_type == RTN_ANYCAST
493 ) {
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000494 req.r.rtm_scope = RT_SCOPE_LINK;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100495 }
496 else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
497 if (cmd == RTM_DELROUTE)
498 req.r.rtm_scope = RT_SCOPE_NOWHERE;
499 else if (!(ok & gw_ok))
500 req.r.rtm_scope = RT_SCOPE_LINK;
501 }
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000502 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000503
Glenn L McGrath16528552002-11-28 11:17:19 +0000504 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000505 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000506 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000507
Glenn L McGrath16528552002-11-28 11:17:19 +0000508 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000509 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000510 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000511
512 return 0;
513}
514
515static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
516{
517 struct {
518 struct nlmsghdr nlh;
519 struct rtmsg rtm;
520 } req;
521 struct sockaddr_nl nladdr;
522
523 memset(&nladdr, 0, sizeof(nladdr));
524 memset(&req, 0, sizeof(req));
525 nladdr.nl_family = AF_NETLINK;
526
527 req.nlh.nlmsg_len = sizeof(req);
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000528 if (RTM_GETROUTE)
529 req.nlh.nlmsg_type = RTM_GETROUTE;
530 if (NLM_F_ROOT | NLM_F_REQUEST)
531 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
532 /*req.nlh.nlmsg_pid = 0; - memset did it already */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000533 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
534 req.rtm.rtm_family = family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000535 if (RTM_F_CLONED)
536 req.rtm.rtm_flags = RTM_F_CLONED;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000537
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000538 return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000539}
540
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000541static void iproute_flush_cache(void)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000542{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000543 static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000544 int flush_fd = open_or_warn(fn, O_WRONLY);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000545
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000546 if (flush_fd < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000547 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000548 }
549
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000550 if (write(flush_fd, "-1", 2) < 2) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100551 bb_perror_msg("can't flush routing cache");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000552 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000553 }
554 close(flush_fd);
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000555}
556
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000557static void iproute_reset_filter(void)
558{
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100559 memset(&G_filter, 0, sizeof(G_filter));
560 G_filter.mdst.bitlen = -1;
561 G_filter.msrc.bitlen = -1;
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000562}
563
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000564/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000565static int iproute_list_or_flush(char **argv, int flush)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000566{
567 int do_ipv6 = preferred_family;
568 struct rtnl_handle rth;
569 char *id = NULL;
570 char *od = NULL;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000571 static const char keywords[] ALIGN1 =
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000572 /* "ip route list/flush" parameters: */
573 "protocol\0" "dev\0" "oif\0" "iif\0"
574 "via\0" "table\0" "cache\0"
575 "from\0" "to\0"
576 /* and possible further keywords */
577 "all\0"
578 "root\0"
579 "match\0"
580 "exact\0"
581 "main\0"
582 ;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000583 enum {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000584 KW_proto, KW_dev, KW_oif, KW_iif,
585 KW_via, KW_table, KW_cache,
586 KW_from, KW_to,
587 /* */
588 KW_all,
589 KW_root,
590 KW_match,
591 KW_exact,
592 KW_main,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000593 };
594 int arg, parm;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000595
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000596 iproute_reset_filter();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100597 G_filter.tb = RT_TABLE_MAIN;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000598
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000599 if (flush && !*argv)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000600 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000601
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000602 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000603 arg = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000604 if (arg == KW_proto) {
Eric Andersend78aea82006-01-30 18:00:02 +0000605 uint32_t prot = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000606 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100607 //G_filter.protocolmask = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000608 if (rtnl_rtprot_a2n(&prot, *argv)) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000609 if (index_in_strings(keywords, *argv) != KW_all)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200610 invarg_1_to_2(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000611 prot = 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100612 //G_filter.protocolmask = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000613 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100614 //G_filter.protocol = prot;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000615 } else if (arg == KW_dev || arg == KW_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000616 NEXT_ARG();
617 od = *argv;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000618 } else if (arg == KW_iif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000619 NEXT_ARG();
620 id = *argv;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000621 } else if (arg == KW_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000622 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100623 get_prefix(&G_filter.rvia, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000624 } else if (arg == KW_table) { /* table all/cache/main */
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000625 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000626 parm = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000627 if (parm == KW_cache)
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100628 G_filter.tb = -1;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000629 else if (parm == KW_all)
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100630 G_filter.tb = 0;
Denis Vlasenko52a8d972008-06-08 00:25:55 +0000631 else if (parm != KW_main) {
632#if ENABLE_FEATURE_IP_RULE
633 uint32_t tid;
634 if (rtnl_rttable_a2n(&tid, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200635 invarg_1_to_2(*argv, "table");
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100636 G_filter.tb = tid;
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000637#else
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200638 invarg_1_to_2(*argv, "table");
Denis Vlasenko52a8d972008-06-08 00:25:55 +0000639#endif
640 }
Denis Vlasenko79c69042007-11-27 09:42:33 +0000641 } else if (arg == KW_cache) {
642 /* The command 'ip route flush cache' is used by OpenSWAN.
643 * Assuming it's a synonym for 'ip route flush table cache' */
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100644 G_filter.tb = -1;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000645 } else if (arg == KW_from) {
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000646 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000647 parm = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000648 if (parm == KW_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000649 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100650 get_prefix(&G_filter.rsrc, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000651 } else if (parm == KW_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000652 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100653 get_prefix(&G_filter.msrc, *argv, do_ipv6);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000654 } else {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000655 if (parm == KW_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000656 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100657 get_prefix(&G_filter.msrc, *argv, do_ipv6);
658 G_filter.rsrc = G_filter.msrc;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000659 }
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000660 } else { /* "to" is the default parameter */
661 if (arg == KW_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000662 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000663 arg = index_in_substrings(keywords, *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000664 }
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000665 /* parm = arg; - would be more plausible, but we reuse 'arg' here */
666 if (arg == KW_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000667 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100668 get_prefix(&G_filter.rdst, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000669 } else if (arg == KW_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000670 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100671 get_prefix(&G_filter.mdst, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000672 } else { /* "to exact" is the default */
673 if (arg == KW_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000674 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100675 get_prefix(&G_filter.mdst, *argv, do_ipv6);
676 G_filter.rdst = G_filter.mdst;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000677 }
678 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000679 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000680 }
681
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100682 if (do_ipv6 == AF_UNSPEC && G_filter.tb) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000683 do_ipv6 = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000684 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000685
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000686 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000687 ll_init_map(&rth);
688
689 if (id || od) {
690 int idx;
691
692 if (id) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000693 idx = xll_name_to_index(id);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100694 G_filter.iif = idx;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000695 }
696 if (od) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000697 idx = xll_name_to_index(od);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100698 G_filter.oif = idx;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000699 }
700 }
701
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000702 if (flush) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000703 char flushb[4096-512];
704
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100705 if (G_filter.tb == -1) { /* "flush table cache" */
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000706 if (do_ipv6 != AF_INET6)
707 iproute_flush_cache();
708 if (do_ipv6 == AF_INET)
709 return 0;
710 }
711
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100712 G_filter.flushb = flushb;
713 G_filter.flushp = 0;
714 G_filter.flushe = sizeof(flushb);
715 G_filter.rth = &rth;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000716
717 for (;;) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000718 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100719 G_filter.flushed = 0;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000720 xrtnl_dump_filter(&rth, print_route, NULL);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100721 if (G_filter.flushed == 0)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000722 return 0;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000723 if (flush_update())
724 return 1;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000725 }
726 }
727
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100728 if (G_filter.tb != -1) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000729 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000730 } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100731 bb_perror_msg_and_die("can't send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000732 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000733 xrtnl_dump_filter(&rth, print_route, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000734
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000735 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000736}
737
738
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000739/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000740static int iproute_get(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000741{
742 struct rtnl_handle rth;
743 struct {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000744 struct nlmsghdr n;
745 struct rtmsg r;
746 char buf[1024];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000747 } req;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000748 char *idev = NULL;
749 char *odev = NULL;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000750 bool connected = 0;
751 bool from_ok = 0;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000752 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000753 "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000754
755 memset(&req, 0, sizeof(req));
756
757 iproute_reset_filter();
758
759 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000760 if (NLM_F_REQUEST)
761 req.n.nlmsg_flags = NLM_F_REQUEST;
762 if (RTM_GETROUTE)
763 req.n.nlmsg_type = RTM_GETROUTE;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000764 req.r.rtm_family = preferred_family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000765 /*req.r.rtm_table = 0; - memset did this already */
766 /*req.r.rtm_protocol = 0;*/
767 /*req.r.rtm_scope = 0;*/
768 /*req.r.rtm_type = 0;*/
769 /*req.r.rtm_src_len = 0;*/
770 /*req.r.rtm_dst_len = 0;*/
771 /*req.r.rtm_tos = 0;*/
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000772
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000773 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000774 switch (index_in_strings(options, *argv)) {
Glenn L McGrath18eae002002-12-02 00:54:10 +0000775 case 0: /* from */
776 {
777 inet_prefix addr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000778 NEXT_ARG();
Glenn L McGrath18eae002002-12-02 00:54:10 +0000779 from_ok = 1;
780 get_prefix(&addr, *argv, req.r.rtm_family);
781 if (req.r.rtm_family == AF_UNSPEC) {
782 req.r.rtm_family = addr.family;
783 }
784 if (addr.bytelen) {
785 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
786 }
787 req.r.rtm_src_len = addr.bitlen;
788 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000789 }
Glenn L McGrath18eae002002-12-02 00:54:10 +0000790 case 1: /* iif */
791 NEXT_ARG();
792 idev = *argv;
793 break;
794 case 2: /* oif */
795 case 3: /* dev */
796 NEXT_ARG();
797 odev = *argv;
798 break;
799 case 4: /* notify */
800 req.r.rtm_flags |= RTM_F_NOTIFY;
801 break;
802 case 5: /* connected */
803 connected = 1;
804 break;
805 case 6: /* to */
806 NEXT_ARG();
807 default:
808 {
809 inet_prefix addr;
810 get_prefix(&addr, *argv, req.r.rtm_family);
811 if (req.r.rtm_family == AF_UNSPEC) {
812 req.r.rtm_family = addr.family;
813 }
814 if (addr.bytelen) {
815 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
816 }
817 req.r.rtm_dst_len = addr.bitlen;
818 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000819 }
Christian Hornung3bbfb582010-11-03 14:08:00 +0100820 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000821 }
822
823 if (req.r.rtm_dst_len == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000824 bb_error_msg_and_die("need at least destination address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000825 }
826
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000827 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000828
829 ll_init_map(&rth);
830
831 if (idev || odev) {
832 int idx;
833
834 if (idev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000835 idx = xll_name_to_index(idev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000836 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
837 }
838 if (odev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000839 idx = xll_name_to_index(odev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000840 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
841 }
842 }
843
Glenn L McGrath16528552002-11-28 11:17:19 +0000844 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000845 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000846 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000847
Glenn L McGrath16528552002-11-28 11:17:19 +0000848 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000849 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000850 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000851
852 if (connected && !from_ok) {
853 struct rtmsg *r = NLMSG_DATA(&req.n);
854 int len = req.n.nlmsg_len;
855 struct rtattr * tb[RTA_MAX+1];
856
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000857 print_route(NULL, &req.n, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000858
859 if (req.n.nlmsg_type != RTM_NEWROUTE) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000860 bb_error_msg_and_die("not a route?");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000861 }
862 len -= NLMSG_LENGTH(sizeof(*r));
863 if (len < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000864 bb_error_msg_and_die("wrong len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000865 }
866
867 memset(tb, 0, sizeof(tb));
868 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
869
870 if (tb[RTA_PREFSRC]) {
871 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
872 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
873 } else if (!tb[RTA_SRC]) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100874 bb_error_msg_and_die("can't connect the route");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000875 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000876 if (!odev && tb[RTA_OIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000877 tb[RTA_OIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000878 }
879 if (tb[RTA_GATEWAY]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000880 tb[RTA_GATEWAY]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000881 }
882 if (!idev && tb[RTA_IIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000883 tb[RTA_IIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000884 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000885 req.n.nlmsg_flags = NLM_F_REQUEST;
886 req.n.nlmsg_type = RTM_GETROUTE;
887
Glenn L McGrath16528552002-11-28 11:17:19 +0000888 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000889 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000890 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000891 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000892 print_route(NULL, &req.n, NULL);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000893 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000894}
895
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000896/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200897int FAST_FUNC do_iproute(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000898{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000899 static const char ip_route_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000900 /*0-3*/ "add\0""append\0""change\0""chg\0"
901 /*4-7*/ "delete\0""get\0""list\0""show\0"
902 /*8..*/ "prepend\0""replace\0""test\0""flush\0";
Denis Vlasenko83c44222008-01-04 15:28:28 +0000903 int command_num;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000904 unsigned flags = 0;
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000905 int cmd = RTM_NEWROUTE;
906
Denys Vlasenko9de2e5a2016-04-21 18:38:51 +0200907 INIT_G();
908
Denis Vlasenko83c44222008-01-04 15:28:28 +0000909 if (!*argv)
910 return iproute_list_or_flush(argv, 0);
911
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000912 /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
913 /* It probably means that it is using "first match" rule */
Denis Vlasenko83c44222008-01-04 15:28:28 +0000914 command_num = index_in_substrings(ip_route_commands, *argv);
915
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000916 switch (command_num) {
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000917 case 0: /* add */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000918 flags = NLM_F_CREATE|NLM_F_EXCL;
919 break;
920 case 1: /* append */
921 flags = NLM_F_CREATE|NLM_F_APPEND;
922 break;
923 case 2: /* change */
924 case 3: /* chg */
925 flags = NLM_F_REPLACE;
926 break;
927 case 4: /* delete */
928 cmd = RTM_DELROUTE;
929 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000930 case 5: /* get */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000931 return iproute_get(argv+1);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000932 case 6: /* list */
933 case 7: /* show */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000934 return iproute_list_or_flush(argv+1, 0);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000935 case 8: /* prepend */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000936 flags = NLM_F_CREATE;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000937 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000938 case 9: /* replace */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000939 flags = NLM_F_CREATE|NLM_F_REPLACE;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000940 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000941 case 10: /* test */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000942 flags = NLM_F_EXCL;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000943 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000944 case 11: /* flush */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000945 return iproute_list_or_flush(argv+1, 1);
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000946 default:
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200947 invarg_1_to_2(*argv, applet_name);
Glenn L McGrath16528552002-11-28 11:17:19 +0000948 }
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000949
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000950 return iproute_modify(cmd, flags, argv+1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000951}