blob: 82827488f547eb07d2c47ba2fafb5a760fcfdc0b [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 */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000014#include "rt_names.h"
15#include "utils.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000016
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017#ifndef RTAX_RTTVAR
18#define RTAX_RTTVAR RTAX_HOPS
19#endif
20
21
Denys Vlasenko36659fd2010-02-05 14:40:23 +010022struct filter_t {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000023 int tb;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +000024 smallint flushed;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000025 char *flushb;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000026 int flushp;
27 int flushe;
28 struct rtnl_handle *rth;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010029 //int protocol, protocolmask; - write-only fields?!
30 //int scope, scopemask; - unused
31 //int type; - read-only
32 //int typemask; - unused
33 //int tos, tosmask; - unused
Denys Vlasenkof1334712011-02-09 04:39:09 +010034 int iif;
35 int oif;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010036 //int realm, realmmask; - unused
37 //inet_prefix rprefsrc; - read-only
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000038 inet_prefix rvia;
39 inet_prefix rdst;
40 inet_prefix mdst;
41 inet_prefix rsrc;
42 inet_prefix msrc;
Denys Vlasenko36659fd2010-02-05 14:40:23 +010043} FIX_ALIASING;
44typedef struct filter_t filter_t;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000045
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010046#define G_filter (*(filter_t*)&bb_common_bufsiz1)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000047
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000048static int flush_update(void)
49{
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010050 if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
Denys Vlasenko651a2692010-03-23 16:25:17 +010051 bb_perror_msg("can't send flush request");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000052 return -1;
53 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010054 G_filter.flushp = 0;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000055 return 0;
56}
57
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020058static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000059 struct nlmsghdr *n, void *arg UNUSED_PARAM)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000060{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000061 struct rtmsg *r = NLMSG_DATA(n);
62 int len = n->nlmsg_len;
Denys Vlasenko3bb235c2011-02-23 01:20:44 +010063 struct rtattr *tb[RTA_MAX+1];
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +000064 inet_prefix dst;
65 inet_prefix src;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000066 int host_len = -1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000067
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000068 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
69 fprintf(stderr, "Not a route: %08x %08x %08x\n",
70 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
71 return 0;
72 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010073 if (G_filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000074 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000075 len -= NLMSG_LENGTH(sizeof(*r));
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +000076 if (len < 0)
77 bb_error_msg_and_die("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000078
79 if (r->rtm_family == AF_INET6)
80 host_len = 128;
81 else if (r->rtm_family == AF_INET)
82 host_len = 32;
83
84 if (r->rtm_family == AF_INET6) {
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010085 if (G_filter.tb) {
86 if (G_filter.tb < 0) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +000087 if (!(r->rtm_flags & RTM_F_CLONED)) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000088 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +000089 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090 } else {
Denis Vlasenko186c2b32007-11-26 18:29:52 +000091 if (r->rtm_flags & RTM_F_CLONED) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000092 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +000093 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010094 if (G_filter.tb == RT_TABLE_LOCAL) {
Glenn L McGrath16528552002-11-28 11:17:19 +000095 if (r->rtm_type != RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000096 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +000097 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010098 } else if (G_filter.tb == RT_TABLE_MAIN) {
Glenn L McGrath16528552002-11-28 11:17:19 +000099 if (r->rtm_type == RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000100 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000101 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102 } else {
103 return 0;
104 }
105 }
106 }
107 } else {
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100108 if (G_filter.tb > 0 && G_filter.tb != r->rtm_table) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000109 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000110 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000111 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100112 if (G_filter.rdst.family
113 && (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len)
114 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000116 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100117 if (G_filter.mdst.family
118 && (r->rtm_family != G_filter.mdst.family
119 || (G_filter.mdst.bitlen >= 0 && G_filter.mdst.bitlen < r->rtm_dst_len)
120 )
121 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000123 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100124 if (G_filter.rsrc.family
125 && (r->rtm_family != G_filter.rsrc.family || G_filter.rsrc.bitlen > r->rtm_src_len)
126 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000127 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000128 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100129 if (G_filter.msrc.family
130 && (r->rtm_family != G_filter.msrc.family
131 || (G_filter.msrc.bitlen >= 0 && G_filter.msrc.bitlen < r->rtm_src_len)
132 )
133 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000134 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000135 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000136
137 memset(tb, 0, sizeof(tb));
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100138 memset(&src, 0, sizeof(src));
139 memset(&dst, 0, sizeof(dst));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000140 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
141
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100142 if (tb[RTA_SRC]) {
143 src.bitlen = r->rtm_src_len;
144 src.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
145 memcpy(src.data, RTA_DATA(tb[RTA_SRC]), src.bytelen);
146 }
147 if (tb[RTA_DST]) {
148 dst.bitlen = r->rtm_dst_len;
149 dst.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
150 memcpy(dst.data, RTA_DATA(tb[RTA_DST]), dst.bytelen);
151 }
152
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100153 if (G_filter.rdst.family
154 && inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen)
155 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000156 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100157 }
158 if (G_filter.mdst.family
159 && G_filter.mdst.bitlen >= 0
160 && inet_addr_match(&dst, &G_filter.mdst, r->rtm_dst_len)
161 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000162 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100163 }
164 if (G_filter.rsrc.family
165 && inet_addr_match(&src, &G_filter.rsrc, G_filter.rsrc.bitlen)
166 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000167 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100168 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100169 if (G_filter.msrc.family && G_filter.msrc.bitlen >= 0
170 && inet_addr_match(&src, &G_filter.msrc, r->rtm_src_len)
171 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000172 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100173 }
Denys Vlasenkof1334712011-02-09 04:39:09 +0100174 if (G_filter.oif != 0) {
175 if (!tb[RTA_OIF])
176 return 0;
177 if (G_filter.oif != *(int*)RTA_DATA(tb[RTA_OIF]))
178 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100179 }
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000180
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100181 if (G_filter.flushb) {
Denys Vlasenkoe3ece782011-02-10 09:50:07 +0100182 struct nlmsghdr *fn;
183
Denys Vlasenkof1334712011-02-09 04:39:09 +0100184 /* We are creating route flush commands */
185
186 if (r->rtm_family == AF_INET6
187 && r->rtm_dst_len == 0
188 && r->rtm_type == RTN_UNREACHABLE
189 && tb[RTA_PRIORITY]
190 && *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1
191 ) {
192 return 0;
193 }
194
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100195 if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000196 if (flush_update())
Curt Brune69934702015-10-14 12:53:47 +0200197 xfunc_die();
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000198 }
Denys Vlasenkoe3ece782011-02-10 09:50:07 +0100199 fn = (void*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000200 memcpy(fn, n, n->nlmsg_len);
201 fn->nlmsg_type = RTM_DELROUTE;
202 fn->nlmsg_flags = NLM_F_REQUEST;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100203 fn->nlmsg_seq = ++G_filter.rth->seq;
204 G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
205 G_filter.flushed = 1;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000206 return 0;
207 }
208
Denys Vlasenkof1334712011-02-09 04:39:09 +0100209 /* We are printing routes */
210
Glenn L McGrath16528552002-11-28 11:17:19 +0000211 if (n->nlmsg_type == RTM_DELROUTE) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000212 printf("Deleted ");
Glenn L McGrath16528552002-11-28 11:17:19 +0000213 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100214 if (r->rtm_type != RTN_UNICAST /* && !G_filter.type - always 0 */) {
Denys Vlasenko3d8d5e82015-10-08 13:02:28 +0200215 printf("%s ", rtnl_rtntype_n2a(r->rtm_type));
Glenn L McGrath16528552002-11-28 11:17:19 +0000216 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000217
218 if (tb[RTA_DST]) {
219 if (r->rtm_dst_len != host_len) {
Denys Vlasenko926d8012015-10-14 13:56:42 +0200220 printf("%s/%u ",
221 rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_DST])),
222 r->rtm_dst_len
223 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000224 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000225 printf("%s ", format_host(r->rtm_family,
226 RTA_PAYLOAD(tb[RTA_DST]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200227 RTA_DATA(tb[RTA_DST]))
228 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000229 }
230 } else if (r->rtm_dst_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000231 printf("0/%d ", r->rtm_dst_len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000232 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000233 printf("default ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234 }
235 if (tb[RTA_SRC]) {
236 if (r->rtm_src_len != host_len) {
Denys Vlasenko926d8012015-10-14 13:56:42 +0200237 printf("from %s/%u ",
238 rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
239 r->rtm_src_len
240 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000241 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000242 printf("from %s ", format_host(r->rtm_family,
243 RTA_PAYLOAD(tb[RTA_SRC]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200244 RTA_DATA(tb[RTA_SRC]))
245 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000246 }
247 } else if (r->rtm_src_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000248 printf("from 0/%u ", r->rtm_src_len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000249 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100250 if (tb[RTA_GATEWAY] && G_filter.rvia.bitlen != host_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000251 printf("via %s ", format_host(r->rtm_family,
252 RTA_PAYLOAD(tb[RTA_GATEWAY]),
Denys Vlasenko926d8012015-10-14 13:56:42 +0200253 RTA_DATA(tb[RTA_GATEWAY]))
254 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000255 }
Denys Vlasenkof1334712011-02-09 04:39:09 +0100256 if (tb[RTA_OIF]) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000257 printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
Glenn L McGrath16528552002-11-28 11:17:19 +0000258 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259
Denys Vlasenkof1334712011-02-09 04:39:09 +0100260 /* Todo: parse & show "proto kernel", "scope link" here */
261
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100262 if (tb[RTA_PREFSRC] && /*G_filter.rprefsrc.bitlen - always 0*/ 0 != host_len) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000263 /* Do not use format_host(). It is our local addr
264 and symbolic name will not be useful.
265 */
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000266 printf(" src %s ", rt_addr_n2a(r->rtm_family,
Denys Vlasenko926d8012015-10-14 13:56:42 +0200267 RTA_DATA(tb[RTA_PREFSRC])));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000268 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000269 if (tb[RTA_PRIORITY]) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000270 printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
Glenn L McGrath16528552002-11-28 11:17:19 +0000271 }
Michael Tokarev1a114392014-07-28 10:05:41 +0400272 if (r->rtm_flags & RTNH_F_DEAD) {
273 printf("dead ");
274 }
275 if (r->rtm_flags & RTNH_F_ONLINK) {
276 printf("onlink ");
277 }
278 if (r->rtm_flags & RTNH_F_PERVASIVE) {
279 printf("pervasive ");
280 }
281 if (r->rtm_flags & RTM_F_NOTIFY) {
282 printf("notify ");
283 }
284
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000285 if (r->rtm_family == AF_INET6) {
286 struct rta_cacheinfo *ci = NULL;
Glenn L McGrath16528552002-11-28 11:17:19 +0000287 if (tb[RTA_CACHEINFO]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000288 ci = RTA_DATA(tb[RTA_CACHEINFO]);
Glenn L McGrath16528552002-11-28 11:17:19 +0000289 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000290 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000291 if (r->rtm_flags & RTM_F_CLONED) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000292 printf("%c cache ", _SL_);
Glenn L McGrath16528552002-11-28 11:17:19 +0000293 }
294 if (ci->rta_expires) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000295 printf(" expires %dsec", ci->rta_expires / get_hz());
Glenn L McGrath16528552002-11-28 11:17:19 +0000296 }
297 if (ci->rta_error != 0) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000298 printf(" error %d", ci->rta_error);
Glenn L McGrath16528552002-11-28 11:17:19 +0000299 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000300 } else if (ci) {
301 if (ci->rta_error != 0)
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000302 printf(" error %d", ci->rta_error);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000303 }
304 }
Denys Vlasenkof1334712011-02-09 04:39:09 +0100305 if (tb[RTA_IIF] && G_filter.iif == 0) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000306 printf(" iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000307 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000308 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000309 return 0;
310}
311
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000312/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000313static int iproute_modify(int cmd, unsigned flags, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000314{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000315 static const char keywords[] ALIGN1 =
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100316 "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 +0400317 "dev\0""oif\0""to\0""metric\0""onlink\0";
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000318 enum {
319 ARG_src,
320 ARG_via,
321 ARG_mtu, PARM_lock,
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100322 ARG_scope,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000323 ARG_protocol,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000324IF_FEATURE_IP_RULE(ARG_table,)
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000325 ARG_dev,
326 ARG_oif,
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000327 ARG_to,
328 ARG_metric,
Michael Tokarev1a114392014-07-28 10:05:41 +0400329 ARG_onlink,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000330 };
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000331 enum {
332 gw_ok = 1 << 0,
333 dst_ok = 1 << 1,
334 proto_ok = 1 << 2,
335 type_ok = 1 << 3
336 };
337 struct rtnl_handle rth;
338 struct {
Denys Vlasenkofb132e42010-10-29 11:46:52 +0200339 struct nlmsghdr n;
340 struct rtmsg r;
341 char buf[1024];
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000342 } req;
343 char mxbuf[256];
344 struct rtattr * mxrta = (void*)mxbuf;
345 unsigned mxlock = 0;
346 char *d = NULL;
347 smalluint ok = 0;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100348 smalluint scope_ok = 0;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000349 int arg;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000350
351 memset(&req, 0, sizeof(req));
352
353 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000354 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000355 req.n.nlmsg_type = cmd;
356 req.r.rtm_family = preferred_family;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100357 if (RT_TABLE_MAIN != 0) /* if it is zero, memset already did it */
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000358 req.r.rtm_table = RT_TABLE_MAIN;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100359 if (RT_SCOPE_NOWHERE != 0)
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000360 req.r.rtm_scope = RT_SCOPE_NOWHERE;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000361
362 if (cmd != RTM_DELROUTE) {
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100363 if (RTPROT_BOOT != 0)
364 req.r.rtm_protocol = RTPROT_BOOT;
365 if (RT_SCOPE_UNIVERSE != 0)
366 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
367 if (RTN_UNICAST != 0)
368 req.r.rtm_type = RTN_UNICAST;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000369 }
370
371 mxrta->rta_type = RTA_METRICS;
372 mxrta->rta_len = RTA_LENGTH(0);
373
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000374 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000375 arg = index_in_substrings(keywords, *argv);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000376 if (arg == ARG_src) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000377 inet_prefix addr;
378 NEXT_ARG();
379 get_addr(&addr, *argv, req.r.rtm_family);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000380 if (req.r.rtm_family == AF_UNSPEC)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000381 req.r.rtm_family = addr.family;
382 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000383 } else if (arg == ARG_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000384 inet_prefix addr;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000385 ok |= gw_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000386 NEXT_ARG();
387 get_addr(&addr, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000388 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000389 req.r.rtm_family = addr.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000390 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000391 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000392 } else if (arg == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000393 unsigned mtu;
394 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000395 if (index_in_strings(keywords, *argv) == PARM_lock) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000396 mxlock |= (1 << RTAX_MTU);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000397 NEXT_ARG();
398 }
Denis Vlasenko76140a72009-03-05 09:21:57 +0000399 mtu = get_unsigned(*argv, "mtu");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000400 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100401 } else if (arg == ARG_scope) {
402 uint32_t scope;
403 NEXT_ARG();
404 if (rtnl_rtscope_a2n(&scope, *argv))
405 invarg_1_to_2(*argv, "scope");
406 req.r.rtm_scope = scope;
407 scope_ok = 1;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000408 } else if (arg == ARG_protocol) {
Eric Andersend78aea82006-01-30 18:00:02 +0000409 uint32_t prot;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000410 NEXT_ARG();
411 if (rtnl_rtprot_a2n(&prot, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200412 invarg_1_to_2(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000413 req.r.rtm_protocol = prot;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000414 ok |= proto_ok;
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000415#if ENABLE_FEATURE_IP_RULE
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000416 } else if (arg == ARG_table) {
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000417 uint32_t tid;
418 NEXT_ARG();
419 if (rtnl_rttable_a2n(&tid, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200420 invarg_1_to_2(*argv, "table");
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000421 req.r.rtm_table = tid;
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000422#endif
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000423 } else if (arg == ARG_dev || arg == ARG_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000424 NEXT_ARG();
425 d = *argv;
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000426 } else if (arg == ARG_metric) {
427 uint32_t metric;
428 NEXT_ARG();
Denis Vlasenko76140a72009-03-05 09:21:57 +0000429 metric = get_u32(*argv, "metric");
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000430 addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
Michael Tokarev1a114392014-07-28 10:05:41 +0400431 } else if (arg == ARG_onlink) {
432 req.r.rtm_flags |= RTNH_F_ONLINK;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000433 } else {
434 int type;
435 inet_prefix dst;
436
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000437 if (arg == ARG_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000438 NEXT_ARG();
439 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000440 if ((**argv < '0' || **argv > '9')
Denys Vlasenko3bb235c2011-02-23 01:20:44 +0100441 && rtnl_rtntype_a2n(&type, *argv) == 0
442 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000443 NEXT_ARG();
444 req.r.rtm_type = type;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000445 ok |= type_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000446 }
447
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000448 if (ok & dst_ok) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000449 duparg2("to", *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000450 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000451 get_prefix(&dst, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000452 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000453 req.r.rtm_family = dst.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000454 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000455 req.r.rtm_dst_len = dst.bitlen;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000456 ok |= dst_ok;
Glenn L McGrath16528552002-11-28 11:17:19 +0000457 if (dst.bytelen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000458 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Glenn L McGrath16528552002-11-28 11:17:19 +0000459 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000460 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000461 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000462 }
463
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000464 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000465
Eric Andersen5780adb2002-11-15 09:12:47 +0000466 if (d) {
467 int idx;
468
469 ll_init_map(&rth);
470
471 if (d) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000472 idx = xll_name_to_index(d);
Eric Andersen5780adb2002-11-15 09:12:47 +0000473 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
474 }
475 }
476
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000477 if (mxrta->rta_len > RTA_LENGTH(0)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000478 if (mxlock) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000479 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
Glenn L McGrath16528552002-11-28 11:17:19 +0000480 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000481 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
482 }
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000483
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100484 if (!scope_ok) {
485 if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
486 req.r.rtm_scope = RT_SCOPE_HOST;
487 else
488 if (req.r.rtm_type == RTN_BROADCAST
489 || req.r.rtm_type == RTN_MULTICAST
490 || req.r.rtm_type == RTN_ANYCAST
491 ) {
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000492 req.r.rtm_scope = RT_SCOPE_LINK;
Denys Vlasenkoce4bc1e2015-12-30 17:32:51 +0100493 }
494 else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
495 if (cmd == RTM_DELROUTE)
496 req.r.rtm_scope = RT_SCOPE_NOWHERE;
497 else if (!(ok & gw_ok))
498 req.r.rtm_scope = RT_SCOPE_LINK;
499 }
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000500 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000501
Glenn L McGrath16528552002-11-28 11:17:19 +0000502 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000503 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000504 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000505
Glenn L McGrath16528552002-11-28 11:17:19 +0000506 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000507 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000508 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000509
510 return 0;
511}
512
513static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
514{
515 struct {
516 struct nlmsghdr nlh;
517 struct rtmsg rtm;
518 } req;
519 struct sockaddr_nl nladdr;
520
521 memset(&nladdr, 0, sizeof(nladdr));
522 memset(&req, 0, sizeof(req));
523 nladdr.nl_family = AF_NETLINK;
524
525 req.nlh.nlmsg_len = sizeof(req);
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000526 if (RTM_GETROUTE)
527 req.nlh.nlmsg_type = RTM_GETROUTE;
528 if (NLM_F_ROOT | NLM_F_REQUEST)
529 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
530 /*req.nlh.nlmsg_pid = 0; - memset did it already */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000531 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
532 req.rtm.rtm_family = family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000533 if (RTM_F_CLONED)
534 req.rtm.rtm_flags = RTM_F_CLONED;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000535
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000536 return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000537}
538
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000539static void iproute_flush_cache(void)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000540{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000541 static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000542 int flush_fd = open_or_warn(fn, O_WRONLY);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000543
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000544 if (flush_fd < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000545 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000546 }
547
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000548 if (write(flush_fd, "-1", 2) < 2) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100549 bb_perror_msg("can't flush routing cache");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000550 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000551 }
552 close(flush_fd);
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000553}
554
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000555static void iproute_reset_filter(void)
556{
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100557 memset(&G_filter, 0, sizeof(G_filter));
558 G_filter.mdst.bitlen = -1;
559 G_filter.msrc.bitlen = -1;
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000560}
561
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000562/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000563static int iproute_list_or_flush(char **argv, int flush)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000564{
565 int do_ipv6 = preferred_family;
566 struct rtnl_handle rth;
567 char *id = NULL;
568 char *od = NULL;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000569 static const char keywords[] ALIGN1 =
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000570 /* "ip route list/flush" parameters: */
571 "protocol\0" "dev\0" "oif\0" "iif\0"
572 "via\0" "table\0" "cache\0"
573 "from\0" "to\0"
574 /* and possible further keywords */
575 "all\0"
576 "root\0"
577 "match\0"
578 "exact\0"
579 "main\0"
580 ;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000581 enum {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000582 KW_proto, KW_dev, KW_oif, KW_iif,
583 KW_via, KW_table, KW_cache,
584 KW_from, KW_to,
585 /* */
586 KW_all,
587 KW_root,
588 KW_match,
589 KW_exact,
590 KW_main,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000591 };
592 int arg, parm;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000593
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000594 iproute_reset_filter();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100595 G_filter.tb = RT_TABLE_MAIN;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000596
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000597 if (flush && !*argv)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000598 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000599
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000600 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000601 arg = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000602 if (arg == KW_proto) {
Eric Andersend78aea82006-01-30 18:00:02 +0000603 uint32_t prot = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000604 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100605 //G_filter.protocolmask = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000606 if (rtnl_rtprot_a2n(&prot, *argv)) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000607 if (index_in_strings(keywords, *argv) != KW_all)
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200608 invarg_1_to_2(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000609 prot = 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100610 //G_filter.protocolmask = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000611 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100612 //G_filter.protocol = prot;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000613 } else if (arg == KW_dev || arg == KW_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000614 NEXT_ARG();
615 od = *argv;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000616 } else if (arg == KW_iif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000617 NEXT_ARG();
618 id = *argv;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000619 } else if (arg == KW_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000620 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100621 get_prefix(&G_filter.rvia, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000622 } else if (arg == KW_table) { /* table all/cache/main */
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000623 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000624 parm = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000625 if (parm == KW_cache)
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100626 G_filter.tb = -1;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000627 else if (parm == KW_all)
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100628 G_filter.tb = 0;
Denis Vlasenko52a8d972008-06-08 00:25:55 +0000629 else if (parm != KW_main) {
630#if ENABLE_FEATURE_IP_RULE
631 uint32_t tid;
632 if (rtnl_rttable_a2n(&tid, *argv))
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200633 invarg_1_to_2(*argv, "table");
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100634 G_filter.tb = tid;
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000635#else
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200636 invarg_1_to_2(*argv, "table");
Denis Vlasenko52a8d972008-06-08 00:25:55 +0000637#endif
638 }
Denis Vlasenko79c69042007-11-27 09:42:33 +0000639 } else if (arg == KW_cache) {
640 /* The command 'ip route flush cache' is used by OpenSWAN.
641 * Assuming it's a synonym for 'ip route flush table cache' */
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100642 G_filter.tb = -1;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000643 } else if (arg == KW_from) {
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000644 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000645 parm = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000646 if (parm == KW_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000647 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100648 get_prefix(&G_filter.rsrc, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000649 } else if (parm == KW_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000650 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100651 get_prefix(&G_filter.msrc, *argv, do_ipv6);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000652 } else {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000653 if (parm == KW_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000654 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100655 get_prefix(&G_filter.msrc, *argv, do_ipv6);
656 G_filter.rsrc = G_filter.msrc;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000657 }
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000658 } else { /* "to" is the default parameter */
659 if (arg == KW_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000660 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000661 arg = index_in_substrings(keywords, *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000662 }
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000663 /* parm = arg; - would be more plausible, but we reuse 'arg' here */
664 if (arg == KW_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000665 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100666 get_prefix(&G_filter.rdst, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000667 } else if (arg == KW_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000668 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100669 get_prefix(&G_filter.mdst, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000670 } else { /* "to exact" is the default */
671 if (arg == KW_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000672 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100673 get_prefix(&G_filter.mdst, *argv, do_ipv6);
674 G_filter.rdst = G_filter.mdst;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000675 }
676 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000677 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000678 }
679
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100680 if (do_ipv6 == AF_UNSPEC && G_filter.tb) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000681 do_ipv6 = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000682 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000683
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000684 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000685 ll_init_map(&rth);
686
687 if (id || od) {
688 int idx;
689
690 if (id) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000691 idx = xll_name_to_index(id);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100692 G_filter.iif = idx;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000693 }
694 if (od) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000695 idx = xll_name_to_index(od);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100696 G_filter.oif = idx;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000697 }
698 }
699
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000700 if (flush) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000701 char flushb[4096-512];
702
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100703 if (G_filter.tb == -1) { /* "flush table cache" */
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000704 if (do_ipv6 != AF_INET6)
705 iproute_flush_cache();
706 if (do_ipv6 == AF_INET)
707 return 0;
708 }
709
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100710 G_filter.flushb = flushb;
711 G_filter.flushp = 0;
712 G_filter.flushe = sizeof(flushb);
713 G_filter.rth = &rth;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000714
715 for (;;) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000716 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100717 G_filter.flushed = 0;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000718 xrtnl_dump_filter(&rth, print_route, NULL);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100719 if (G_filter.flushed == 0)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000720 return 0;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000721 if (flush_update())
722 return 1;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000723 }
724 }
725
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100726 if (G_filter.tb != -1) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000727 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000728 } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100729 bb_perror_msg_and_die("can't send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000730 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000731 xrtnl_dump_filter(&rth, print_route, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000732
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000733 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000734}
735
736
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000737/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000738static int iproute_get(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000739{
740 struct rtnl_handle rth;
741 struct {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000742 struct nlmsghdr n;
743 struct rtmsg r;
744 char buf[1024];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000745 } req;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000746 char *idev = NULL;
747 char *odev = NULL;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000748 bool connected = 0;
749 bool from_ok = 0;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000750 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000751 "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000752
753 memset(&req, 0, sizeof(req));
754
755 iproute_reset_filter();
756
757 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000758 if (NLM_F_REQUEST)
759 req.n.nlmsg_flags = NLM_F_REQUEST;
760 if (RTM_GETROUTE)
761 req.n.nlmsg_type = RTM_GETROUTE;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000762 req.r.rtm_family = preferred_family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000763 /*req.r.rtm_table = 0; - memset did this already */
764 /*req.r.rtm_protocol = 0;*/
765 /*req.r.rtm_scope = 0;*/
766 /*req.r.rtm_type = 0;*/
767 /*req.r.rtm_src_len = 0;*/
768 /*req.r.rtm_dst_len = 0;*/
769 /*req.r.rtm_tos = 0;*/
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000770
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000771 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000772 switch (index_in_strings(options, *argv)) {
Glenn L McGrath18eae002002-12-02 00:54:10 +0000773 case 0: /* from */
774 {
775 inet_prefix addr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000776 NEXT_ARG();
Glenn L McGrath18eae002002-12-02 00:54:10 +0000777 from_ok = 1;
778 get_prefix(&addr, *argv, req.r.rtm_family);
779 if (req.r.rtm_family == AF_UNSPEC) {
780 req.r.rtm_family = addr.family;
781 }
782 if (addr.bytelen) {
783 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
784 }
785 req.r.rtm_src_len = addr.bitlen;
786 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000787 }
Glenn L McGrath18eae002002-12-02 00:54:10 +0000788 case 1: /* iif */
789 NEXT_ARG();
790 idev = *argv;
791 break;
792 case 2: /* oif */
793 case 3: /* dev */
794 NEXT_ARG();
795 odev = *argv;
796 break;
797 case 4: /* notify */
798 req.r.rtm_flags |= RTM_F_NOTIFY;
799 break;
800 case 5: /* connected */
801 connected = 1;
802 break;
803 case 6: /* to */
804 NEXT_ARG();
805 default:
806 {
807 inet_prefix addr;
808 get_prefix(&addr, *argv, req.r.rtm_family);
809 if (req.r.rtm_family == AF_UNSPEC) {
810 req.r.rtm_family = addr.family;
811 }
812 if (addr.bytelen) {
813 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
814 }
815 req.r.rtm_dst_len = addr.bitlen;
816 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000817 }
Christian Hornung3bbfb582010-11-03 14:08:00 +0100818 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000819 }
820
821 if (req.r.rtm_dst_len == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000822 bb_error_msg_and_die("need at least destination address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000823 }
824
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000825 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000826
827 ll_init_map(&rth);
828
829 if (idev || odev) {
830 int idx;
831
832 if (idev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000833 idx = xll_name_to_index(idev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000834 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
835 }
836 if (odev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000837 idx = xll_name_to_index(odev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000838 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
839 }
840 }
841
Glenn L McGrath16528552002-11-28 11:17:19 +0000842 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000843 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000844 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000845
Glenn L McGrath16528552002-11-28 11:17:19 +0000846 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000847 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000848 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000849
850 if (connected && !from_ok) {
851 struct rtmsg *r = NLMSG_DATA(&req.n);
852 int len = req.n.nlmsg_len;
853 struct rtattr * tb[RTA_MAX+1];
854
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000855 print_route(NULL, &req.n, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000856
857 if (req.n.nlmsg_type != RTM_NEWROUTE) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000858 bb_error_msg_and_die("not a route?");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000859 }
860 len -= NLMSG_LENGTH(sizeof(*r));
861 if (len < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000862 bb_error_msg_and_die("wrong len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000863 }
864
865 memset(tb, 0, sizeof(tb));
866 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
867
868 if (tb[RTA_PREFSRC]) {
869 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
870 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
871 } else if (!tb[RTA_SRC]) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100872 bb_error_msg_and_die("can't connect the route");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000873 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000874 if (!odev && tb[RTA_OIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000875 tb[RTA_OIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000876 }
877 if (tb[RTA_GATEWAY]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000878 tb[RTA_GATEWAY]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000879 }
880 if (!idev && tb[RTA_IIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000881 tb[RTA_IIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000882 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000883 req.n.nlmsg_flags = NLM_F_REQUEST;
884 req.n.nlmsg_type = RTM_GETROUTE;
885
Glenn L McGrath16528552002-11-28 11:17:19 +0000886 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000887 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000888 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000889 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000890 print_route(NULL, &req.n, NULL);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000891 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000892}
893
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000894/* Return value becomes exitcode. It's okay to not return at all */
Denys Vlasenko2e9b5512010-07-24 23:27:38 +0200895int FAST_FUNC do_iproute(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000896{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000897 static const char ip_route_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000898 /*0-3*/ "add\0""append\0""change\0""chg\0"
899 /*4-7*/ "delete\0""get\0""list\0""show\0"
900 /*8..*/ "prepend\0""replace\0""test\0""flush\0";
Denis Vlasenko83c44222008-01-04 15:28:28 +0000901 int command_num;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000902 unsigned flags = 0;
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000903 int cmd = RTM_NEWROUTE;
904
Denis Vlasenko83c44222008-01-04 15:28:28 +0000905 if (!*argv)
906 return iproute_list_or_flush(argv, 0);
907
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000908 /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
909 /* It probably means that it is using "first match" rule */
Denis Vlasenko83c44222008-01-04 15:28:28 +0000910 command_num = index_in_substrings(ip_route_commands, *argv);
911
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000912 switch (command_num) {
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000913 case 0: /* add */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000914 flags = NLM_F_CREATE|NLM_F_EXCL;
915 break;
916 case 1: /* append */
917 flags = NLM_F_CREATE|NLM_F_APPEND;
918 break;
919 case 2: /* change */
920 case 3: /* chg */
921 flags = NLM_F_REPLACE;
922 break;
923 case 4: /* delete */
924 cmd = RTM_DELROUTE;
925 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000926 case 5: /* get */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000927 return iproute_get(argv+1);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000928 case 6: /* list */
929 case 7: /* show */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000930 return iproute_list_or_flush(argv+1, 0);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000931 case 8: /* prepend */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000932 flags = NLM_F_CREATE;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000933 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000934 case 9: /* replace */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000935 flags = NLM_F_CREATE|NLM_F_REPLACE;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000936 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000937 case 10: /* test */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000938 flags = NLM_F_EXCL;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000939 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000940 case 11: /* flush */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000941 return iproute_list_or_flush(argv+1, 1);
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000942 default:
Denys Vlasenko0f296a32015-10-14 13:21:01 +0200943 invarg_1_to_2(*argv, applet_name);
Glenn L McGrath16528552002-11-28 11:17:19 +0000944 }
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000945
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000946 return iproute_modify(cmd, flags, argv+1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000947}