blob: 75e52939cecc221caaf68cd943f2efd33e2f1e39 [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
Denis Vlasenko540a2a12007-04-07 01:14:45 +000025typedef struct filter_t {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000026 int tb;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000027 int flushed;
28 char *flushb;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000029 int flushp;
30 int flushe;
31 struct rtnl_handle *rth;
32 int protocol, protocolmask;
33 int scope, scopemask;
34 int type, typemask;
35 int tos, tosmask;
36 int iif, iifmask;
37 int oif, oifmask;
38 int realm, realmmask;
39 inet_prefix rprefsrc;
40 inet_prefix rvia;
41 inet_prefix rdst;
42 inet_prefix mdst;
43 inet_prefix rsrc;
44 inet_prefix msrc;
Denis Vlasenko540a2a12007-04-07 01:14:45 +000045} filter_t;
46
47#define filter (*(filter_t*)&bb_common_bufsiz1)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000048
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000049static int flush_update(void)
50{
51 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +000052 bb_perror_msg("failed to send flush request");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000053 return -1;
54 }
55 filter.flushp = 0;
56 return 0;
57}
58
Denis Vlasenkoc5193202006-12-31 19:07:23 +000059static unsigned get_hz(void)
Denis Vlasenko13463af2006-12-31 18:58:32 +000060{
Denis Vlasenkoc5193202006-12-31 19:07:23 +000061 static unsigned hz_internal;
Denis Vlasenko13463af2006-12-31 18:58:32 +000062 FILE *fp;
63
64 if (hz_internal)
65 return hz_internal;
66
67 fp = fopen("/proc/net/psched", "r");
68 if (fp) {
69 unsigned nom, denom;
70
71 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
72 if (nom == 1000000)
73 hz_internal = denom;
74 fclose(fp);
75 }
76 if (!hz_internal)
77 hz_internal = sysconf(_SC_CLK_TCK);
78 return hz_internal;
79}
80
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000081static int print_route(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
Bernhard Reutner-Fischerd1d23a62006-01-12 12:08:46 +000082 struct nlmsghdr *n, void *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000083{
84 FILE *fp = (FILE*)arg;
85 struct rtmsg *r = NLMSG_DATA(n);
86 int len = n->nlmsg_len;
87 struct rtattr * tb[RTA_MAX+1];
88 char abuf[256];
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +000089 inet_prefix dst;
90 inet_prefix src;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000091 int host_len = -1;
92 SPRINT_BUF(b1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000093
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000094
95 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 }
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000100 if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
101 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) {
112 if (filter.tb) {
113 if (filter.tb < 0) {
Glenn L McGrath16528552002-11-28 11:17:19 +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 {
Glenn L McGrath16528552002-11-28 11:17:19 +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 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000121 if (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 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000125 } else if (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 {
Glenn L McGrath16528552002-11-28 11:17:19 +0000135 if (filter.tb > 0 && 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 }
139 if (filter.rdst.family &&
Glenn L McGrath16528552002-11-28 11:17:19 +0000140 (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len)) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000141 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000142 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000143 if (filter.mdst.family &&
144 (r->rtm_family != filter.mdst.family ||
Glenn L McGrath16528552002-11-28 11:17:19 +0000145 (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len))) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000146 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000147 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000148 if (filter.rsrc.family &&
Glenn L McGrath16528552002-11-28 11:17:19 +0000149 (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len)) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000150 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000151 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000152 if (filter.msrc.family &&
153 (r->rtm_family != filter.msrc.family ||
Glenn L McGrath16528552002-11-28 11:17:19 +0000154 (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len))) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000156 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000157
158 memset(tb, 0, sizeof(tb));
159 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
160
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000161 if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
162 return 0;
163 if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
164 inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
165 return 0;
166
167 if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
168 return 0;
169 if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
170 inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
171 return 0;
172
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000173 if (filter.flushb &&
174 r->rtm_family == AF_INET6 &&
175 r->rtm_dst_len == 0 &&
176 r->rtm_type == RTN_UNREACHABLE &&
177 tb[RTA_PRIORITY] &&
178 *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
179 return 0;
180
181 if (filter.flushb) {
182 struct nlmsghdr *fn;
183 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
184 if (flush_update())
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000185 bb_error_msg_and_die("flush");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000186 }
187 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
188 memcpy(fn, n, n->nlmsg_len);
189 fn->nlmsg_type = RTM_DELROUTE;
190 fn->nlmsg_flags = NLM_F_REQUEST;
191 fn->nlmsg_seq = ++filter.rth->seq;
192 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
193 filter.flushed++;
194 return 0;
195 }
196
Glenn L McGrath16528552002-11-28 11:17:19 +0000197 if (n->nlmsg_type == RTM_DELROUTE) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000198 fprintf(fp, "Deleted ");
Glenn L McGrath16528552002-11-28 11:17:19 +0000199 }
200 if (r->rtm_type != RTN_UNICAST && !filter.type) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000201 fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
Glenn L McGrath16528552002-11-28 11:17:19 +0000202 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000203
204 if (tb[RTA_DST]) {
205 if (r->rtm_dst_len != host_len) {
206 fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
207 RTA_PAYLOAD(tb[RTA_DST]),
208 RTA_DATA(tb[RTA_DST]),
209 abuf, sizeof(abuf)),
210 r->rtm_dst_len
211 );
212 } else {
213 fprintf(fp, "%s ", format_host(r->rtm_family,
214 RTA_PAYLOAD(tb[RTA_DST]),
215 RTA_DATA(tb[RTA_DST]),
216 abuf, sizeof(abuf))
217 );
218 }
219 } else if (r->rtm_dst_len) {
220 fprintf(fp, "0/%d ", r->rtm_dst_len);
221 } else {
222 fprintf(fp, "default ");
223 }
224 if (tb[RTA_SRC]) {
225 if (r->rtm_src_len != host_len) {
226 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
227 RTA_PAYLOAD(tb[RTA_SRC]),
228 RTA_DATA(tb[RTA_SRC]),
229 abuf, sizeof(abuf)),
230 r->rtm_src_len
231 );
232 } else {
233 fprintf(fp, "from %s ", format_host(r->rtm_family,
234 RTA_PAYLOAD(tb[RTA_SRC]),
235 RTA_DATA(tb[RTA_SRC]),
236 abuf, sizeof(abuf))
237 );
238 }
239 } else if (r->rtm_src_len) {
240 fprintf(fp, "from 0/%u ", r->rtm_src_len);
241 }
242 if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000243 fprintf(fp, "via %s ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000244 format_host(r->rtm_family,
245 RTA_PAYLOAD(tb[RTA_GATEWAY]),
246 RTA_DATA(tb[RTA_GATEWAY]),
247 abuf, sizeof(abuf)));
248 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000249 if (tb[RTA_OIF] && filter.oifmask != -1) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000250 fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
Glenn L McGrath16528552002-11-28 11:17:19 +0000251 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000252
253 if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
254 /* Do not use format_host(). It is our local addr
255 and symbolic name will not be useful.
256 */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000257 fprintf(fp, " src %s ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000258 rt_addr_n2a(r->rtm_family,
259 RTA_PAYLOAD(tb[RTA_PREFSRC]),
260 RTA_DATA(tb[RTA_PREFSRC]),
261 abuf, sizeof(abuf)));
262 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000263 if (tb[RTA_PRIORITY]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000264 fprintf(fp, " metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
Glenn L McGrath16528552002-11-28 11:17:19 +0000265 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000266 if (r->rtm_family == AF_INET6) {
267 struct rta_cacheinfo *ci = NULL;
Glenn L McGrath16528552002-11-28 11:17:19 +0000268 if (tb[RTA_CACHEINFO]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000269 ci = RTA_DATA(tb[RTA_CACHEINFO]);
Glenn L McGrath16528552002-11-28 11:17:19 +0000270 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000271 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000272 if (r->rtm_flags & RTM_F_CLONED) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000273 fprintf(fp, "%c cache ", _SL_);
Glenn L McGrath16528552002-11-28 11:17:19 +0000274 }
275 if (ci->rta_expires) {
Denis Vlasenkoc5193202006-12-31 19:07:23 +0000276 fprintf(fp, " expires %dsec", ci->rta_expires / get_hz());
Glenn L McGrath16528552002-11-28 11:17:19 +0000277 }
278 if (ci->rta_error != 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000279 fprintf(fp, " error %d", ci->rta_error);
Glenn L McGrath16528552002-11-28 11:17:19 +0000280 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000281 } else if (ci) {
282 if (ci->rta_error != 0)
283 fprintf(fp, " error %d", ci->rta_error);
284 }
285 }
286 if (tb[RTA_IIF] && filter.iifmask != -1) {
287 fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
288 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000289 fputc('\n', fp);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000290 fflush(fp);
291 return 0;
292}
293
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000294/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000295static int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000296{
297 struct rtnl_handle rth;
298 struct {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000299 struct nlmsghdr n;
300 struct rtmsg r;
301 char buf[1024];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000302 } req;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000303 char mxbuf[256];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000304 struct rtattr * mxrta = (void*)mxbuf;
305 unsigned mxlock = 0;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000306 char *d = NULL;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000307 enum { gw_ok = 1<<0, dst_ok = 1<<1, proto_ok = 1<<2, type_ok = 1<<3};
308 smalluint ok = 0;
309 static const char * const keywords[] = {
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000310 "src", "via", "mtu", "lock", "protocol", USE_FEATURE_IP_RULE("table",)
311 "dev", "oif", "to", NULL
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000312 };
313 enum {
314 ARG_src,
315 ARG_via,
316 ARG_mtu, PARM_lock,
317 ARG_protocol,
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000318USE_FEATURE_IP_RULE(ARG_table,)
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000319 ARG_dev,
320 ARG_oif,
321 ARG_to
322 };
323 int arg;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000324
325 memset(&req, 0, sizeof(req));
326
327 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
328 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
329 req.n.nlmsg_type = cmd;
330 req.r.rtm_family = preferred_family;
331 req.r.rtm_table = RT_TABLE_MAIN;
332 req.r.rtm_scope = RT_SCOPE_NOWHERE;
333
334 if (cmd != RTM_DELROUTE) {
335 req.r.rtm_protocol = RTPROT_BOOT;
336 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
337 req.r.rtm_type = RTN_UNICAST;
338 }
339
340 mxrta->rta_type = RTA_METRICS;
341 mxrta->rta_len = RTA_LENGTH(0);
342
343 while (argc > 0) {
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000344 arg = index_in_substr_array(keywords, *argv);
345 if (arg == ARG_src) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000346 inet_prefix addr;
347 NEXT_ARG();
348 get_addr(&addr, *argv, req.r.rtm_family);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000349 if (req.r.rtm_family == AF_UNSPEC)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000350 req.r.rtm_family = addr.family;
351 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000352 } else if (arg == ARG_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000353 inet_prefix addr;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000354 ok |= gw_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000355 NEXT_ARG();
356 get_addr(&addr, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000357 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000358 req.r.rtm_family = addr.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000359 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000360 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000361 } else if (arg == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000362 unsigned mtu;
363 NEXT_ARG();
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000364 if (index_in_str_array(keywords, *argv) == PARM_lock) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000365 mxlock |= (1<<RTAX_MTU);
366 NEXT_ARG();
367 }
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000368 if (get_unsigned(&mtu, *argv, 0))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000369 invarg(*argv, "mtu");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000370 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000371 } else if (arg == ARG_protocol) {
Eric Andersend78aea82006-01-30 18:00:02 +0000372 uint32_t prot;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000373 NEXT_ARG();
374 if (rtnl_rtprot_a2n(&prot, *argv))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000375 invarg(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000376 req.r.rtm_protocol = prot;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000377 ok |= proto_ok;
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000378#if ENABLE_FEATURE_IP_RULE
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000379 } else if (arg == ARG_table) {
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000380 uint32_t tid;
381 NEXT_ARG();
382 if (rtnl_rttable_a2n(&tid, *argv))
383 invarg(*argv, "table");
384 req.r.rtm_table = tid;
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000385#endif
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000386 } else if (arg == ARG_dev || arg == ARG_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000387 NEXT_ARG();
388 d = *argv;
389 } else {
390 int type;
391 inet_prefix dst;
392
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000393 if (arg == ARG_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000394 NEXT_ARG();
395 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000396 if ((**argv < '0' || **argv > '9')
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000397 && rtnl_rtntype_a2n(&type, *argv) == 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000398 NEXT_ARG();
399 req.r.rtm_type = type;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000400 ok |= type_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000401 }
402
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000403 if (ok & dst_ok) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000404 duparg2("to", *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000405 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000406 get_prefix(&dst, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000407 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000408 req.r.rtm_family = dst.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000409 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000410 req.r.rtm_dst_len = dst.bitlen;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000411 ok |= dst_ok;
Glenn L McGrath16528552002-11-28 11:17:19 +0000412 if (dst.bytelen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000413 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Glenn L McGrath16528552002-11-28 11:17:19 +0000414 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000415 }
416 argc--; argv++;
417 }
418
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000419 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000420
Eric Andersen5780adb2002-11-15 09:12:47 +0000421 if (d) {
422 int idx;
423
424 ll_init_map(&rth);
425
426 if (d) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000427 idx = xll_name_to_index(d);
Eric Andersen5780adb2002-11-15 09:12:47 +0000428 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
429 }
430 }
431
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000432 if (mxrta->rta_len > RTA_LENGTH(0)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000433 if (mxlock) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000434 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
Glenn L McGrath16528552002-11-28 11:17:19 +0000435 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000436 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
437 }
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000438
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000439 if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
440 req.r.rtm_scope = RT_SCOPE_HOST;
441 else if (req.r.rtm_type == RTN_BROADCAST ||
442 req.r.rtm_type == RTN_MULTICAST ||
443 req.r.rtm_type == RTN_ANYCAST)
444 req.r.rtm_scope = RT_SCOPE_LINK;
445 else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
446 if (cmd == RTM_DELROUTE)
447 req.r.rtm_scope = RT_SCOPE_NOWHERE;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000448 else if (!(ok & gw_ok))
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000449 req.r.rtm_scope = RT_SCOPE_LINK;
450 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000451
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 = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000454 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000455
Glenn L McGrath16528552002-11-28 11:17:19 +0000456 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000457 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000458 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000459
460 return 0;
461}
462
463static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
464{
465 struct {
466 struct nlmsghdr nlh;
467 struct rtmsg rtm;
468 } req;
469 struct sockaddr_nl nladdr;
470
471 memset(&nladdr, 0, sizeof(nladdr));
472 memset(&req, 0, sizeof(req));
473 nladdr.nl_family = AF_NETLINK;
474
475 req.nlh.nlmsg_len = sizeof(req);
476 req.nlh.nlmsg_type = RTM_GETROUTE;
477 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
478 req.nlh.nlmsg_pid = 0;
479 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
480 req.rtm.rtm_family = family;
481 req.rtm.rtm_flags |= RTM_F_CLONED;
482
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000483 return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000484}
485
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000486static void iproute_flush_cache(void)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000487{
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000488 static const char fn[] = "/proc/sys/net/ipv4/route/flush";
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000489 int flush_fd = open_or_warn(fn, O_WRONLY);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000490
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000491 if (flush_fd < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000492 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000493 }
494
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000495 if (write(flush_fd, "-1", 2) < 2) {
496 bb_perror_msg("cannot flush routing cache");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000497 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000498 }
499 close(flush_fd);
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000500}
501
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000502static void iproute_reset_filter(void)
503{
504 memset(&filter, 0, sizeof(filter));
505 filter.mdst.bitlen = -1;
506 filter.msrc.bitlen = -1;
507}
508
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000509/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000510static int iproute_list_or_flush(int argc, char **argv, int flush)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000511{
512 int do_ipv6 = preferred_family;
513 struct rtnl_handle rth;
514 char *id = NULL;
515 char *od = NULL;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000516 static const char * const keywords[] = {
517 "protocol", "all", "dev", "oif", "iif", "via", "table", "cache",/*all,*/
518 "from", "root", "match", "exact", "to", /*root,match,exact*/ NULL
519 };
520 enum {
521 ARG_proto, PARM_all,
522 ARG_dev,
523 ARG_oif,
524 ARG_iif,
525 ARG_via,
526 ARG_table, PARM_cache, /*PARM_all,*/
527 ARG_from, PARM_root, PARM_match, PARM_exact,
528 ARG_to /*PARM_root, PARM_match, PARM_exact*/
529 };
530 int arg, parm;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000531 iproute_reset_filter();
532 filter.tb = RT_TABLE_MAIN;
533
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000534 if (flush && argc <= 0)
535 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000536
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000537 while (argc > 0) {
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000538 arg = index_in_substr_array(keywords, *argv);
539 if (arg == ARG_proto) {
Eric Andersend78aea82006-01-30 18:00:02 +0000540 uint32_t prot = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000541 NEXT_ARG();
542 filter.protocolmask = -1;
543 if (rtnl_rtprot_a2n(&prot, *argv)) {
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000544 if (index_in_str_array(keywords, *argv) != PARM_all)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000545 invarg(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000546 prot = 0;
547 filter.protocolmask = 0;
548 }
549 filter.protocol = prot;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000550 } else if (arg == ARG_dev || arg == ARG_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000551 NEXT_ARG();
552 od = *argv;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000553 } else if (arg == ARG_iif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000554 NEXT_ARG();
555 id = *argv;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000556 } else if (arg == ARG_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000557 NEXT_ARG();
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000558 get_prefix(&filter.rvia, *argv, do_ipv6);
559 } else if (arg == ARG_table) {
560 NEXT_ARG();
561 parm = index_in_substr_array(keywords, *argv);
562 if (parm == PARM_cache)
563 filter.tb = -1;
564 else if (parm == PARM_all)
565 filter.tb = 0;
566 else
567 invarg(*argv, "table");
568 } else if (arg == ARG_from) {
569 NEXT_ARG();
570 parm = index_in_substr_array(keywords, *argv);
571 if (parm == PARM_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000572 NEXT_ARG();
573 get_prefix(&filter.rsrc, *argv, do_ipv6);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000574 } else if (parm == PARM_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000575 NEXT_ARG();
576 get_prefix(&filter.msrc, *argv, do_ipv6);
577 } else {
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000578 if (parm == PARM_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000579 NEXT_ARG();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000580 get_prefix(&filter.msrc, *argv, do_ipv6);
581 filter.rsrc = filter.msrc;
582 }
583 } else {
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000584 /* parm = arg; // would be more plausible, we reuse arg here */
585 if (arg == ARG_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000586 NEXT_ARG();
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000587 arg = index_in_substr_array(keywords, *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000588 }
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000589 if (arg == PARM_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000590 NEXT_ARG();
591 get_prefix(&filter.rdst, *argv, do_ipv6);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000592 } else if (arg == PARM_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000593 NEXT_ARG();
594 get_prefix(&filter.mdst, *argv, do_ipv6);
595 } else {
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000596 if (arg == PARM_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000597 NEXT_ARG();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000598 get_prefix(&filter.mdst, *argv, do_ipv6);
599 filter.rdst = filter.mdst;
600 }
601 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000602 argc--;
603 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000604 }
605
Glenn L McGrath16528552002-11-28 11:17:19 +0000606 if (do_ipv6 == AF_UNSPEC && filter.tb) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000607 do_ipv6 = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000608 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000609
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000610 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000611
612 ll_init_map(&rth);
613
614 if (id || od) {
615 int idx;
616
617 if (id) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000618 idx = xll_name_to_index(id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000619 filter.iif = idx;
620 filter.iifmask = -1;
621 }
622 if (od) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000623 idx = xll_name_to_index(od);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000624 filter.oif = idx;
625 filter.oifmask = -1;
626 }
627 }
628
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000629 if (flush) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000630 char flushb[4096-512];
631
632 if (filter.tb == -1) {
633 if (do_ipv6 != AF_INET6)
634 iproute_flush_cache();
635 if (do_ipv6 == AF_INET)
636 return 0;
637 }
638
639 filter.flushb = flushb;
640 filter.flushp = 0;
641 filter.flushe = sizeof(flushb);
642 filter.rth = &rth;
643
644 for (;;) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000645 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000646 filter.flushed = 0;
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000647 xrtnl_dump_filter(&rth, print_route, stdout);
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000648 if (filter.flushed == 0) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000649 return 0;
650 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000651 if (flush_update())
652 return 1;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000653 }
654 }
655
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000656 if (filter.tb != -1) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000657 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000658 } else {
659 if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000660 bb_perror_msg_and_die("cannot send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000661 }
662 }
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000663 xrtnl_dump_filter(&rth, print_route, stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000664
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000665 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000666}
667
668
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000669/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000670static int iproute_get(int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000671{
672 struct rtnl_handle rth;
673 struct {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000674 struct nlmsghdr n;
675 struct rtmsg r;
676 char buf[1024];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000677 } req;
678 char *idev = NULL;
679 char *odev = NULL;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000680 bool connected = 0;
681 bool from_ok = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000682 static const char * const options[] =
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000683 { "from", "iif", "oif", "dev", "notify", "connected", "to", 0 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000684
685 memset(&req, 0, sizeof(req));
686
687 iproute_reset_filter();
688
689 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
690 req.n.nlmsg_flags = NLM_F_REQUEST;
691 req.n.nlmsg_type = RTM_GETROUTE;
692 req.r.rtm_family = preferred_family;
693 req.r.rtm_table = 0;
694 req.r.rtm_protocol = 0;
695 req.r.rtm_scope = 0;
696 req.r.rtm_type = 0;
697 req.r.rtm_src_len = 0;
698 req.r.rtm_dst_len = 0;
699 req.r.rtm_tos = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000700
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000701 while (argc > 0) {
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000702 switch (index_in_str_array(options, *argv)) {
Glenn L McGrath18eae002002-12-02 00:54:10 +0000703 case 0: /* from */
704 {
705 inet_prefix addr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000706 NEXT_ARG();
Glenn L McGrath18eae002002-12-02 00:54:10 +0000707 from_ok = 1;
708 get_prefix(&addr, *argv, req.r.rtm_family);
709 if (req.r.rtm_family == AF_UNSPEC) {
710 req.r.rtm_family = addr.family;
711 }
712 if (addr.bytelen) {
713 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
714 }
715 req.r.rtm_src_len = addr.bitlen;
716 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000717 }
Glenn L McGrath18eae002002-12-02 00:54:10 +0000718 case 1: /* iif */
719 NEXT_ARG();
720 idev = *argv;
721 break;
722 case 2: /* oif */
723 case 3: /* dev */
724 NEXT_ARG();
725 odev = *argv;
726 break;
727 case 4: /* notify */
728 req.r.rtm_flags |= RTM_F_NOTIFY;
729 break;
730 case 5: /* connected */
731 connected = 1;
732 break;
733 case 6: /* to */
734 NEXT_ARG();
735 default:
736 {
737 inet_prefix addr;
738 get_prefix(&addr, *argv, req.r.rtm_family);
739 if (req.r.rtm_family == AF_UNSPEC) {
740 req.r.rtm_family = addr.family;
741 }
742 if (addr.bytelen) {
743 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
744 }
745 req.r.rtm_dst_len = addr.bitlen;
746 }
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000747 argc--; argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000748 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000749 }
750
751 if (req.r.rtm_dst_len == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000752 bb_error_msg_and_die("need at least destination address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000753 }
754
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000755 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000756
757 ll_init_map(&rth);
758
759 if (idev || odev) {
760 int idx;
761
762 if (idev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000763 idx = xll_name_to_index(idev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000764 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
765 }
766 if (odev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000767 idx = xll_name_to_index(odev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000768 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
769 }
770 }
771
Glenn L McGrath16528552002-11-28 11:17:19 +0000772 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000773 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000774 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000775
Glenn L McGrath16528552002-11-28 11:17:19 +0000776 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000777 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000778 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000779
780 if (connected && !from_ok) {
781 struct rtmsg *r = NLMSG_DATA(&req.n);
782 int len = req.n.nlmsg_len;
783 struct rtattr * tb[RTA_MAX+1];
784
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000785 print_route(NULL, &req.n, (void*)stdout);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000786
787 if (req.n.nlmsg_type != RTM_NEWROUTE) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000788 bb_error_msg_and_die("not a route?");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000789 }
790 len -= NLMSG_LENGTH(sizeof(*r));
791 if (len < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000792 bb_error_msg_and_die("wrong len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000793 }
794
795 memset(tb, 0, sizeof(tb));
796 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
797
798 if (tb[RTA_PREFSRC]) {
799 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
800 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
801 } else if (!tb[RTA_SRC]) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000802 bb_error_msg_and_die("failed to connect the route");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000803 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000804 if (!odev && tb[RTA_OIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000805 tb[RTA_OIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000806 }
807 if (tb[RTA_GATEWAY]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000808 tb[RTA_GATEWAY]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000809 }
810 if (!idev && tb[RTA_IIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000811 tb[RTA_IIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000812 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000813 req.n.nlmsg_flags = NLM_F_REQUEST;
814 req.n.nlmsg_type = RTM_GETROUTE;
815
Glenn L McGrath16528552002-11-28 11:17:19 +0000816 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000817 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000818 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000819 }
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000820 print_route(NULL, &req.n, (void*)stdout);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000821 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000822}
823
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000824/* Return value becomes exitcode. It's okay to not return at all */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000825int do_iproute(int argc, char **argv)
826{
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000827 static const char * const ip_route_commands[] = {
828 /*0-3*/ "add", "append", "change", "chg",
829 /*4-7*/ "delete", "get", "list", "show",
830 /*8..*/ "prepend", "replace", "test", "flush", 0
831 };
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000832 int command_num = 6;
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000833 unsigned int flags = 0;
834 int cmd = RTM_NEWROUTE;
835
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000836 /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
837 /* It probably means that it is using "first match" rule */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000838 if (*argv) {
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000839 command_num = index_in_substr_array(ip_route_commands, *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000840 }
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000841 switch (command_num) {
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000842 case 0: /* add */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000843 flags = NLM_F_CREATE|NLM_F_EXCL;
844 break;
845 case 1: /* append */
846 flags = NLM_F_CREATE|NLM_F_APPEND;
847 break;
848 case 2: /* change */
849 case 3: /* chg */
850 flags = NLM_F_REPLACE;
851 break;
852 case 4: /* delete */
853 cmd = RTM_DELROUTE;
854 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000855 case 5: /* get */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000856 return iproute_get(argc-1, argv+1);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000857 case 6: /* list */
858 case 7: /* show */
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000859 return iproute_list_or_flush(argc-1, argv+1, 0);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000860 case 8: /* prepend */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000861 flags = NLM_F_CREATE;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000862 case 9: /* replace */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000863 flags = NLM_F_CREATE|NLM_F_REPLACE;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000864 case 10: /* test */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000865 flags = NLM_F_EXCL;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000866 case 11: /* flush */
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000867 return iproute_list_or_flush(argc-1, argv+1, 1);
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000868 default:
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000869 bb_error_msg_and_die("unknown command %s", *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000870 }
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000871
872 return iproute_modify(cmd, flags, argc-1, argv+1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000873}