blob: d771a609b24b21abfc868379af7b39ed2f461790 [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/*
3 * iproute.c "ip route".
4 *
Bernhard Reutner-Fischerab187822005-10-26 10:47:26 +00005 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Glenn L McGrath9a2d2722002-11-10 01:33:55 +00006 *
7 * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
8 *
9 *
10 * Changes:
11 *
12 * Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
13 * Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized
14 */
15
Denis Vlasenko9a7d38f2007-05-31 22:42:12 +000016#include "ip_common.h" /* #include "libbb.h" is inside */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000017#include "rt_names.h"
18#include "utils.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000019
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000020#ifndef RTAX_RTTVAR
21#define RTAX_RTTVAR RTAX_HOPS
22#endif
23
24
Denys Vlasenko36659fd2010-02-05 14:40:23 +010025struct filter_t {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000026 int tb;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +000027 smallint flushed;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000028 char *flushb;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000029 int flushp;
30 int flushe;
31 struct rtnl_handle *rth;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010032 //int protocol, protocolmask; - write-only fields?!
33 //int scope, scopemask; - unused
34 //int type; - read-only
35 //int typemask; - unused
36 //int tos, tosmask; - unused
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000037 int iif, iifmask;
38 int oif, oifmask;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010039 //int realm, realmmask; - unused
40 //inet_prefix rprefsrc; - read-only
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000041 inet_prefix rvia;
42 inet_prefix rdst;
43 inet_prefix mdst;
44 inet_prefix rsrc;
45 inet_prefix msrc;
Denys Vlasenko36659fd2010-02-05 14:40:23 +010046} FIX_ALIASING;
47typedef struct filter_t filter_t;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000048
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010049#define G_filter (*(filter_t*)&bb_common_bufsiz1)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000050
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000051static int flush_update(void)
52{
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010053 if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
Denys Vlasenko651a2692010-03-23 16:25:17 +010054 bb_perror_msg("can't send flush request");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000055 return -1;
56 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +010057 G_filter.flushp = 0;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000058 return 0;
59}
60
Denis Vlasenkoc5193202006-12-31 19:07:23 +000061static unsigned get_hz(void)
Denis Vlasenko13463af2006-12-31 18:58:32 +000062{
Denis Vlasenkoc5193202006-12-31 19:07:23 +000063 static unsigned hz_internal;
Denis Vlasenko13463af2006-12-31 18:58:32 +000064 FILE *fp;
65
66 if (hz_internal)
67 return hz_internal;
68
Denis Vlasenko5415c852008-07-21 23:05:26 +000069 fp = fopen_for_read("/proc/net/psched");
Denis Vlasenko13463af2006-12-31 18:58:32 +000070 if (fp) {
71 unsigned nom, denom;
72
73 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
74 if (nom == 1000000)
75 hz_internal = denom;
76 fclose(fp);
77 }
78 if (!hz_internal)
79 hz_internal = sysconf(_SC_CLK_TCK);
80 return hz_internal;
81}
82
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020083static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000084 struct nlmsghdr *n, void *arg UNUSED_PARAM)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000085{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000086 struct rtmsg *r = NLMSG_DATA(n);
87 int len = n->nlmsg_len;
88 struct rtattr * tb[RTA_MAX+1];
89 char abuf[256];
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +000090 inet_prefix dst;
91 inet_prefix src;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000092 int host_len = -1;
93 SPRINT_BUF(b1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000094
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000095 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
96 fprintf(stderr, "Not a route: %08x %08x %08x\n",
97 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
98 return 0;
99 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100100 if (G_filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000101 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000102 len -= NLMSG_LENGTH(sizeof(*r));
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000103 if (len < 0)
104 bb_error_msg_and_die("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000105
106 if (r->rtm_family == AF_INET6)
107 host_len = 128;
108 else if (r->rtm_family == AF_INET)
109 host_len = 32;
110
111 if (r->rtm_family == AF_INET6) {
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100112 if (G_filter.tb) {
113 if (G_filter.tb < 0) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000114 if (!(r->rtm_flags & RTM_F_CLONED)) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000116 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000117 } else {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000118 if (r->rtm_flags & RTM_F_CLONED) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000119 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000120 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100121 if (G_filter.tb == RT_TABLE_LOCAL) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000122 if (r->rtm_type != RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000123 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000124 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100125 } else if (G_filter.tb == RT_TABLE_MAIN) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000126 if (r->rtm_type == RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000127 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000128 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000129 } else {
130 return 0;
131 }
132 }
133 }
134 } else {
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100135 if (G_filter.tb > 0 && G_filter.tb != r->rtm_table) {
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 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100139 if (G_filter.rdst.family
140 && (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len)
141 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000142 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000143 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100144 if (G_filter.mdst.family
145 && (r->rtm_family != G_filter.mdst.family
146 || (G_filter.mdst.bitlen >= 0 && G_filter.mdst.bitlen < r->rtm_dst_len)
147 )
148 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000149 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000150 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100151 if (G_filter.rsrc.family
152 && (r->rtm_family != G_filter.rsrc.family || G_filter.rsrc.bitlen > r->rtm_src_len)
153 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000154 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000155 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100156 if (G_filter.msrc.family
157 && (r->rtm_family != G_filter.msrc.family
158 || (G_filter.msrc.bitlen >= 0 && G_filter.msrc.bitlen < r->rtm_src_len)
159 )
160 ) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000161 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000162 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000163
164 memset(tb, 0, sizeof(tb));
165 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
166
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100167 if (G_filter.rdst.family
168 && inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen)
169 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000170 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100171 }
172 if (G_filter.mdst.family
173 && G_filter.mdst.bitlen >= 0
174 && inet_addr_match(&dst, &G_filter.mdst, r->rtm_dst_len)
175 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000176 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100177 }
178 if (G_filter.rsrc.family
179 && inet_addr_match(&src, &G_filter.rsrc, G_filter.rsrc.bitlen)
180 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000181 return 0;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +0100182 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100183 if (G_filter.msrc.family && G_filter.msrc.bitlen >= 0
184 && inet_addr_match(&src, &G_filter.msrc, r->rtm_src_len)
185 ) {
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000186 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100187 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100188 if (G_filter.flushb
189 && r->rtm_family == AF_INET6
190 && r->rtm_dst_len == 0
191 && r->rtm_type == RTN_UNREACHABLE
192 && tb[RTA_PRIORITY]
193 && *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1
194 ) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000195 return 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100196 }
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000197
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100198 if (G_filter.flushb) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000199 struct nlmsghdr *fn;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100200 if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000201 if (flush_update())
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000202 bb_error_msg_and_die("flush");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000203 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100204 fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000205 memcpy(fn, n, n->nlmsg_len);
206 fn->nlmsg_type = RTM_DELROUTE;
207 fn->nlmsg_flags = NLM_F_REQUEST;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100208 fn->nlmsg_seq = ++G_filter.rth->seq;
209 G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
210 G_filter.flushed = 1;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000211 return 0;
212 }
213
Glenn L McGrath16528552002-11-28 11:17:19 +0000214 if (n->nlmsg_type == RTM_DELROUTE) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000215 printf("Deleted ");
Glenn L McGrath16528552002-11-28 11:17:19 +0000216 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100217 if (r->rtm_type != RTN_UNICAST /* && !G_filter.type - always 0 */) {
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200218 printf("%s ", rtnl_rtntype_n2a(r->rtm_type, b1));
Glenn L McGrath16528552002-11-28 11:17:19 +0000219 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000220
221 if (tb[RTA_DST]) {
222 if (r->rtm_dst_len != host_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000223 printf("%s/%u ", rt_addr_n2a(r->rtm_family,
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000224 RTA_DATA(tb[RTA_DST]),
225 abuf, sizeof(abuf)),
226 r->rtm_dst_len
227 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000228 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000229 printf("%s ", format_host(r->rtm_family,
230 RTA_PAYLOAD(tb[RTA_DST]),
231 RTA_DATA(tb[RTA_DST]),
232 abuf, sizeof(abuf))
233 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234 }
235 } else if (r->rtm_dst_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000236 printf("0/%d ", r->rtm_dst_len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000237 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000238 printf("default ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000239 }
240 if (tb[RTA_SRC]) {
241 if (r->rtm_src_len != host_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000242 printf("from %s/%u ", rt_addr_n2a(r->rtm_family,
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000243 RTA_DATA(tb[RTA_SRC]),
244 abuf, sizeof(abuf)),
245 r->rtm_src_len
246 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000248 printf("from %s ", format_host(r->rtm_family,
249 RTA_PAYLOAD(tb[RTA_SRC]),
250 RTA_DATA(tb[RTA_SRC]),
251 abuf, sizeof(abuf))
252 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000253 }
254 } else if (r->rtm_src_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000255 printf("from 0/%u ", r->rtm_src_len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000256 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100257 if (tb[RTA_GATEWAY] && G_filter.rvia.bitlen != host_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000258 printf("via %s ", format_host(r->rtm_family,
259 RTA_PAYLOAD(tb[RTA_GATEWAY]),
260 RTA_DATA(tb[RTA_GATEWAY]),
261 abuf, sizeof(abuf)));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000262 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100263 if (tb[RTA_OIF] && G_filter.oifmask != -1) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000264 printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
Glenn L McGrath16528552002-11-28 11:17:19 +0000265 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100267 if (tb[RTA_PREFSRC] && /*G_filter.rprefsrc.bitlen - always 0*/ 0 != host_len) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000268 /* Do not use format_host(). It is our local addr
269 and symbolic name will not be useful.
270 */
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000271 printf(" src %s ", rt_addr_n2a(r->rtm_family,
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000272 RTA_DATA(tb[RTA_PREFSRC]),
273 abuf, sizeof(abuf)));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000274 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000275 if (tb[RTA_PRIORITY]) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000276 printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
Glenn L McGrath16528552002-11-28 11:17:19 +0000277 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000278 if (r->rtm_family == AF_INET6) {
279 struct rta_cacheinfo *ci = NULL;
Glenn L McGrath16528552002-11-28 11:17:19 +0000280 if (tb[RTA_CACHEINFO]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000281 ci = RTA_DATA(tb[RTA_CACHEINFO]);
Glenn L McGrath16528552002-11-28 11:17:19 +0000282 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000283 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000284 if (r->rtm_flags & RTM_F_CLONED) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000285 printf("%c cache ", _SL_);
Glenn L McGrath16528552002-11-28 11:17:19 +0000286 }
287 if (ci->rta_expires) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000288 printf(" expires %dsec", ci->rta_expires / get_hz());
Glenn L McGrath16528552002-11-28 11:17:19 +0000289 }
290 if (ci->rta_error != 0) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000291 printf(" error %d", ci->rta_error);
Glenn L McGrath16528552002-11-28 11:17:19 +0000292 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000293 } else if (ci) {
294 if (ci->rta_error != 0)
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000295 printf(" error %d", ci->rta_error);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000296 }
297 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100298 if (tb[RTA_IIF] && G_filter.iifmask != -1) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000299 printf(" iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000300 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000301 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000302 return 0;
303}
304
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000305/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000306static int iproute_modify(int cmd, unsigned flags, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000307{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000308 static const char keywords[] ALIGN1 =
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000309 "src\0""via\0""mtu\0""lock\0""protocol\0"IF_FEATURE_IP_RULE("table\0")
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000310 "dev\0""oif\0""to\0""metric\0";
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000311 enum {
312 ARG_src,
313 ARG_via,
314 ARG_mtu, PARM_lock,
315 ARG_protocol,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000316IF_FEATURE_IP_RULE(ARG_table,)
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000317 ARG_dev,
318 ARG_oif,
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000319 ARG_to,
320 ARG_metric,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000321 };
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000322 enum {
323 gw_ok = 1 << 0,
324 dst_ok = 1 << 1,
325 proto_ok = 1 << 2,
326 type_ok = 1 << 3
327 };
328 struct rtnl_handle rth;
329 struct {
330 struct nlmsghdr n;
331 struct rtmsg r;
332 char buf[1024];
333 } req;
334 char mxbuf[256];
335 struct rtattr * mxrta = (void*)mxbuf;
336 unsigned mxlock = 0;
337 char *d = NULL;
338 smalluint ok = 0;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000339 int arg;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000340
341 memset(&req, 0, sizeof(req));
342
343 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000344 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000345 req.n.nlmsg_type = cmd;
346 req.r.rtm_family = preferred_family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000347 if (RT_TABLE_MAIN) /* if it is zero, memset already did it */
348 req.r.rtm_table = RT_TABLE_MAIN;
349 if (RT_SCOPE_NOWHERE)
350 req.r.rtm_scope = RT_SCOPE_NOWHERE;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000351
352 if (cmd != RTM_DELROUTE) {
353 req.r.rtm_protocol = RTPROT_BOOT;
354 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
355 req.r.rtm_type = RTN_UNICAST;
356 }
357
358 mxrta->rta_type = RTA_METRICS;
359 mxrta->rta_len = RTA_LENGTH(0);
360
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000361 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000362 arg = index_in_substrings(keywords, *argv);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000363 if (arg == ARG_src) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000364 inet_prefix addr;
365 NEXT_ARG();
366 get_addr(&addr, *argv, req.r.rtm_family);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000367 if (req.r.rtm_family == AF_UNSPEC)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000368 req.r.rtm_family = addr.family;
369 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000370 } else if (arg == ARG_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000371 inet_prefix addr;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000372 ok |= gw_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000373 NEXT_ARG();
374 get_addr(&addr, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000375 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000376 req.r.rtm_family = addr.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000377 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000378 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000379 } else if (arg == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000380 unsigned mtu;
381 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000382 if (index_in_strings(keywords, *argv) == PARM_lock) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000383 mxlock |= (1 << RTAX_MTU);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000384 NEXT_ARG();
385 }
Denis Vlasenko76140a72009-03-05 09:21:57 +0000386 mtu = get_unsigned(*argv, "mtu");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000387 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000388 } else if (arg == ARG_protocol) {
Eric Andersend78aea82006-01-30 18:00:02 +0000389 uint32_t prot;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000390 NEXT_ARG();
391 if (rtnl_rtprot_a2n(&prot, *argv))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000392 invarg(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000393 req.r.rtm_protocol = prot;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000394 ok |= proto_ok;
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000395#if ENABLE_FEATURE_IP_RULE
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000396 } else if (arg == ARG_table) {
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000397 uint32_t tid;
398 NEXT_ARG();
399 if (rtnl_rttable_a2n(&tid, *argv))
400 invarg(*argv, "table");
401 req.r.rtm_table = tid;
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000402#endif
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000403 } else if (arg == ARG_dev || arg == ARG_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000404 NEXT_ARG();
405 d = *argv;
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000406 } else if (arg == ARG_metric) {
407 uint32_t metric;
408 NEXT_ARG();
Denis Vlasenko76140a72009-03-05 09:21:57 +0000409 metric = get_u32(*argv, "metric");
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000410 addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000411 } else {
412 int type;
413 inet_prefix dst;
414
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000415 if (arg == ARG_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000416 NEXT_ARG();
417 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000418 if ((**argv < '0' || **argv > '9')
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000419 && rtnl_rtntype_a2n(&type, *argv) == 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000420 NEXT_ARG();
421 req.r.rtm_type = type;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000422 ok |= type_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000423 }
424
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000425 if (ok & dst_ok) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000426 duparg2("to", *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000427 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000428 get_prefix(&dst, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000429 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000430 req.r.rtm_family = dst.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000431 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000432 req.r.rtm_dst_len = dst.bitlen;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000433 ok |= dst_ok;
Glenn L McGrath16528552002-11-28 11:17:19 +0000434 if (dst.bytelen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000435 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Glenn L McGrath16528552002-11-28 11:17:19 +0000436 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000437 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000438 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000439 }
440
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000441 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000442
Eric Andersen5780adb2002-11-15 09:12:47 +0000443 if (d) {
444 int idx;
445
446 ll_init_map(&rth);
447
448 if (d) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000449 idx = xll_name_to_index(d);
Eric Andersen5780adb2002-11-15 09:12:47 +0000450 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
451 }
452 }
453
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000454 if (mxrta->rta_len > RTA_LENGTH(0)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000455 if (mxlock) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000456 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
Glenn L McGrath16528552002-11-28 11:17:19 +0000457 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000458 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
459 }
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000460
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000461 if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
462 req.r.rtm_scope = RT_SCOPE_HOST;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100463 else
464 if (req.r.rtm_type == RTN_BROADCAST
465 || req.r.rtm_type == RTN_MULTICAST
466 || req.r.rtm_type == RTN_ANYCAST
467 ) {
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000468 req.r.rtm_scope = RT_SCOPE_LINK;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100469 }
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000470 else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
471 if (cmd == RTM_DELROUTE)
472 req.r.rtm_scope = RT_SCOPE_NOWHERE;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000473 else if (!(ok & gw_ok))
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000474 req.r.rtm_scope = RT_SCOPE_LINK;
475 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000476
Glenn L McGrath16528552002-11-28 11:17:19 +0000477 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000478 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000479 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000480
Glenn L McGrath16528552002-11-28 11:17:19 +0000481 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000482 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000483 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000484
485 return 0;
486}
487
488static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
489{
490 struct {
491 struct nlmsghdr nlh;
492 struct rtmsg rtm;
493 } req;
494 struct sockaddr_nl nladdr;
495
496 memset(&nladdr, 0, sizeof(nladdr));
497 memset(&req, 0, sizeof(req));
498 nladdr.nl_family = AF_NETLINK;
499
500 req.nlh.nlmsg_len = sizeof(req);
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000501 if (RTM_GETROUTE)
502 req.nlh.nlmsg_type = RTM_GETROUTE;
503 if (NLM_F_ROOT | NLM_F_REQUEST)
504 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
505 /*req.nlh.nlmsg_pid = 0; - memset did it already */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000506 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
507 req.rtm.rtm_family = family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000508 if (RTM_F_CLONED)
509 req.rtm.rtm_flags = RTM_F_CLONED;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000510
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000511 return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000512}
513
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000514static void iproute_flush_cache(void)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000515{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000516 static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000517 int flush_fd = open_or_warn(fn, O_WRONLY);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000518
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000519 if (flush_fd < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000520 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000521 }
522
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000523 if (write(flush_fd, "-1", 2) < 2) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100524 bb_perror_msg("can't flush routing cache");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000525 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000526 }
527 close(flush_fd);
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000528}
529
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000530static void iproute_reset_filter(void)
531{
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100532 memset(&G_filter, 0, sizeof(G_filter));
533 G_filter.mdst.bitlen = -1;
534 G_filter.msrc.bitlen = -1;
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000535}
536
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000537/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000538static int iproute_list_or_flush(char **argv, int flush)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000539{
540 int do_ipv6 = preferred_family;
541 struct rtnl_handle rth;
542 char *id = NULL;
543 char *od = NULL;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000544 static const char keywords[] ALIGN1 =
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000545 /* "ip route list/flush" parameters: */
546 "protocol\0" "dev\0" "oif\0" "iif\0"
547 "via\0" "table\0" "cache\0"
548 "from\0" "to\0"
549 /* and possible further keywords */
550 "all\0"
551 "root\0"
552 "match\0"
553 "exact\0"
554 "main\0"
555 ;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000556 enum {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000557 KW_proto, KW_dev, KW_oif, KW_iif,
558 KW_via, KW_table, KW_cache,
559 KW_from, KW_to,
560 /* */
561 KW_all,
562 KW_root,
563 KW_match,
564 KW_exact,
565 KW_main,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000566 };
567 int arg, parm;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000568
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000569 iproute_reset_filter();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100570 G_filter.tb = RT_TABLE_MAIN;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000571
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000572 if (flush && !*argv)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000573 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000574
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000575 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000576 arg = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000577 if (arg == KW_proto) {
Eric Andersend78aea82006-01-30 18:00:02 +0000578 uint32_t prot = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000579 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100580 //G_filter.protocolmask = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000581 if (rtnl_rtprot_a2n(&prot, *argv)) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000582 if (index_in_strings(keywords, *argv) != KW_all)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000583 invarg(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000584 prot = 0;
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100585 //G_filter.protocolmask = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000586 }
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100587 //G_filter.protocol = prot;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000588 } else if (arg == KW_dev || arg == KW_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000589 NEXT_ARG();
590 od = *argv;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000591 } else if (arg == KW_iif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000592 NEXT_ARG();
593 id = *argv;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000594 } else if (arg == KW_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000595 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100596 get_prefix(&G_filter.rvia, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000597 } else if (arg == KW_table) { /* table all/cache/main */
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000598 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000599 parm = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000600 if (parm == KW_cache)
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100601 G_filter.tb = -1;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000602 else if (parm == KW_all)
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100603 G_filter.tb = 0;
Denis Vlasenko52a8d972008-06-08 00:25:55 +0000604 else if (parm != KW_main) {
605#if ENABLE_FEATURE_IP_RULE
606 uint32_t tid;
607 if (rtnl_rttable_a2n(&tid, *argv))
608 invarg(*argv, "table");
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100609 G_filter.tb = tid;
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000610#else
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000611 invarg(*argv, "table");
Denis Vlasenko52a8d972008-06-08 00:25:55 +0000612#endif
613 }
Denis Vlasenko79c69042007-11-27 09:42:33 +0000614 } else if (arg == KW_cache) {
615 /* The command 'ip route flush cache' is used by OpenSWAN.
616 * Assuming it's a synonym for 'ip route flush table cache' */
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100617 G_filter.tb = -1;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000618 } else if (arg == KW_from) {
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000619 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000620 parm = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000621 if (parm == KW_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000622 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100623 get_prefix(&G_filter.rsrc, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000624 } else if (parm == KW_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000625 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100626 get_prefix(&G_filter.msrc, *argv, do_ipv6);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000627 } else {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000628 if (parm == KW_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000629 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100630 get_prefix(&G_filter.msrc, *argv, do_ipv6);
631 G_filter.rsrc = G_filter.msrc;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000632 }
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000633 } else { /* "to" is the default parameter */
634 if (arg == KW_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000635 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000636 arg = index_in_substrings(keywords, *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000637 }
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000638 /* parm = arg; - would be more plausible, but we reuse 'arg' here */
639 if (arg == KW_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000640 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100641 get_prefix(&G_filter.rdst, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000642 } else if (arg == KW_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000643 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100644 get_prefix(&G_filter.mdst, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000645 } else { /* "to exact" is the default */
646 if (arg == KW_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000647 NEXT_ARG();
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100648 get_prefix(&G_filter.mdst, *argv, do_ipv6);
649 G_filter.rdst = G_filter.mdst;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000650 }
651 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000652 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000653 }
654
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100655 if (do_ipv6 == AF_UNSPEC && G_filter.tb) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000656 do_ipv6 = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000657 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000658
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000659 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000660 ll_init_map(&rth);
661
662 if (id || od) {
663 int idx;
664
665 if (id) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000666 idx = xll_name_to_index(id);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100667 G_filter.iif = idx;
668 G_filter.iifmask = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000669 }
670 if (od) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000671 idx = xll_name_to_index(od);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100672 G_filter.oif = idx;
673 G_filter.oifmask = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000674 }
675 }
676
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000677 if (flush) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000678 char flushb[4096-512];
679
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100680 if (G_filter.tb == -1) { /* "flush table cache" */
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000681 if (do_ipv6 != AF_INET6)
682 iproute_flush_cache();
683 if (do_ipv6 == AF_INET)
684 return 0;
685 }
686
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100687 G_filter.flushb = flushb;
688 G_filter.flushp = 0;
689 G_filter.flushe = sizeof(flushb);
690 G_filter.rth = &rth;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000691
692 for (;;) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000693 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100694 G_filter.flushed = 0;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000695 xrtnl_dump_filter(&rth, print_route, NULL);
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100696 if (G_filter.flushed == 0)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000697 return 0;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000698 if (flush_update())
699 return 1;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000700 }
701 }
702
Denys Vlasenkoffc4bce2010-01-26 11:03:16 +0100703 if (G_filter.tb != -1) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000704 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000705 } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
Denys Vlasenko6331cf02009-11-13 09:08:27 +0100706 bb_perror_msg_and_die("can't send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000707 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000708 xrtnl_dump_filter(&rth, print_route, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000709
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000710 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000711}
712
713
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000714/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000715static int iproute_get(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000716{
717 struct rtnl_handle rth;
718 struct {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000719 struct nlmsghdr n;
720 struct rtmsg r;
721 char buf[1024];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000722 } req;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000723 char *idev = NULL;
724 char *odev = NULL;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000725 bool connected = 0;
726 bool from_ok = 0;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000727 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000728 "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000729
730 memset(&req, 0, sizeof(req));
731
732 iproute_reset_filter();
733
734 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000735 if (NLM_F_REQUEST)
736 req.n.nlmsg_flags = NLM_F_REQUEST;
737 if (RTM_GETROUTE)
738 req.n.nlmsg_type = RTM_GETROUTE;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000739 req.r.rtm_family = preferred_family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000740 /*req.r.rtm_table = 0; - memset did this already */
741 /*req.r.rtm_protocol = 0;*/
742 /*req.r.rtm_scope = 0;*/
743 /*req.r.rtm_type = 0;*/
744 /*req.r.rtm_src_len = 0;*/
745 /*req.r.rtm_dst_len = 0;*/
746 /*req.r.rtm_tos = 0;*/
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000747
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000748 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000749 switch (index_in_strings(options, *argv)) {
Glenn L McGrath18eae002002-12-02 00:54:10 +0000750 case 0: /* from */
751 {
752 inet_prefix addr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000753 NEXT_ARG();
Glenn L McGrath18eae002002-12-02 00:54:10 +0000754 from_ok = 1;
755 get_prefix(&addr, *argv, req.r.rtm_family);
756 if (req.r.rtm_family == AF_UNSPEC) {
757 req.r.rtm_family = addr.family;
758 }
759 if (addr.bytelen) {
760 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
761 }
762 req.r.rtm_src_len = addr.bitlen;
763 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000764 }
Glenn L McGrath18eae002002-12-02 00:54:10 +0000765 case 1: /* iif */
766 NEXT_ARG();
767 idev = *argv;
768 break;
769 case 2: /* oif */
770 case 3: /* dev */
771 NEXT_ARG();
772 odev = *argv;
773 break;
774 case 4: /* notify */
775 req.r.rtm_flags |= RTM_F_NOTIFY;
776 break;
777 case 5: /* connected */
778 connected = 1;
779 break;
780 case 6: /* to */
781 NEXT_ARG();
782 default:
783 {
784 inet_prefix addr;
785 get_prefix(&addr, *argv, req.r.rtm_family);
786 if (req.r.rtm_family == AF_UNSPEC) {
787 req.r.rtm_family = addr.family;
788 }
789 if (addr.bytelen) {
790 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
791 }
792 req.r.rtm_dst_len = addr.bitlen;
793 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000794 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000795 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000796 }
797
798 if (req.r.rtm_dst_len == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000799 bb_error_msg_and_die("need at least destination address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000800 }
801
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000802 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000803
804 ll_init_map(&rth);
805
806 if (idev || odev) {
807 int idx;
808
809 if (idev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000810 idx = xll_name_to_index(idev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000811 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
812 }
813 if (odev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000814 idx = xll_name_to_index(odev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000815 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
816 }
817 }
818
Glenn L McGrath16528552002-11-28 11:17:19 +0000819 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000820 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000821 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000822
Glenn L McGrath16528552002-11-28 11:17:19 +0000823 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000824 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000825 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000826
827 if (connected && !from_ok) {
828 struct rtmsg *r = NLMSG_DATA(&req.n);
829 int len = req.n.nlmsg_len;
830 struct rtattr * tb[RTA_MAX+1];
831
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000832 print_route(NULL, &req.n, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000833
834 if (req.n.nlmsg_type != RTM_NEWROUTE) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000835 bb_error_msg_and_die("not a route?");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000836 }
837 len -= NLMSG_LENGTH(sizeof(*r));
838 if (len < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000839 bb_error_msg_and_die("wrong len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000840 }
841
842 memset(tb, 0, sizeof(tb));
843 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
844
845 if (tb[RTA_PREFSRC]) {
846 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
847 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
848 } else if (!tb[RTA_SRC]) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100849 bb_error_msg_and_die("can't connect the route");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000850 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000851 if (!odev && tb[RTA_OIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000852 tb[RTA_OIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000853 }
854 if (tb[RTA_GATEWAY]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000855 tb[RTA_GATEWAY]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000856 }
857 if (!idev && tb[RTA_IIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000858 tb[RTA_IIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000859 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000860 req.n.nlmsg_flags = NLM_F_REQUEST;
861 req.n.nlmsg_type = RTM_GETROUTE;
862
Glenn L McGrath16528552002-11-28 11:17:19 +0000863 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000864 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000865 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000866 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000867 print_route(NULL, &req.n, NULL);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000868 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000869}
870
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000871/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000872int do_iproute(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000873{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000874 static const char ip_route_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000875 /*0-3*/ "add\0""append\0""change\0""chg\0"
876 /*4-7*/ "delete\0""get\0""list\0""show\0"
877 /*8..*/ "prepend\0""replace\0""test\0""flush\0";
Denis Vlasenko83c44222008-01-04 15:28:28 +0000878 int command_num;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000879 unsigned flags = 0;
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000880 int cmd = RTM_NEWROUTE;
881
Denis Vlasenko83c44222008-01-04 15:28:28 +0000882 if (!*argv)
883 return iproute_list_or_flush(argv, 0);
884
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000885 /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
886 /* It probably means that it is using "first match" rule */
Denis Vlasenko83c44222008-01-04 15:28:28 +0000887 command_num = index_in_substrings(ip_route_commands, *argv);
888
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000889 switch (command_num) {
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000890 case 0: /* add */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000891 flags = NLM_F_CREATE|NLM_F_EXCL;
892 break;
893 case 1: /* append */
894 flags = NLM_F_CREATE|NLM_F_APPEND;
895 break;
896 case 2: /* change */
897 case 3: /* chg */
898 flags = NLM_F_REPLACE;
899 break;
900 case 4: /* delete */
901 cmd = RTM_DELROUTE;
902 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000903 case 5: /* get */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000904 return iproute_get(argv+1);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000905 case 6: /* list */
906 case 7: /* show */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000907 return iproute_list_or_flush(argv+1, 0);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000908 case 8: /* prepend */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000909 flags = NLM_F_CREATE;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000910 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000911 case 9: /* replace */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000912 flags = NLM_F_CREATE|NLM_F_REPLACE;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000913 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000914 case 10: /* test */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000915 flags = NLM_F_EXCL;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000916 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000917 case 11: /* flush */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000918 return iproute_list_or_flush(argv+1, 1);
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000919 default:
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000920 bb_error_msg_and_die("unknown command %s", *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000921 }
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000922
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000923 return iproute_modify(cmd, flags, argv+1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000924}