blob: 7031bed347805b1d47dead2812d0aad41f226f03 [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;
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;
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
Denis Vlasenko5415c852008-07-21 23:05:26 +000067 fp = fopen_for_read("/proc/net/psched");
Denis Vlasenko13463af2006-12-31 18:58:32 +000068 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
Denys Vlasenkod5f1b1b2009-06-05 12:06:05 +020081static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000082 struct nlmsghdr *n, void *arg UNUSED_PARAM)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000083{
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000084 struct rtmsg *r = NLMSG_DATA(n);
85 int len = n->nlmsg_len;
86 struct rtattr * tb[RTA_MAX+1];
87 char abuf[256];
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +000088 inet_prefix dst;
89 inet_prefix src;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000090 int host_len = -1;
91 SPRINT_BUF(b1);
Eric Andersenc7bda1c2004-03-15 08:29:22 +000092
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000093 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
94 fprintf(stderr, "Not a route: %08x %08x %08x\n",
95 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
96 return 0;
97 }
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000098 if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
99 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000100 len -= NLMSG_LENGTH(sizeof(*r));
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000101 if (len < 0)
102 bb_error_msg_and_die("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000103
104 if (r->rtm_family == AF_INET6)
105 host_len = 128;
106 else if (r->rtm_family == AF_INET)
107 host_len = 32;
108
109 if (r->rtm_family == AF_INET6) {
110 if (filter.tb) {
111 if (filter.tb < 0) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000112 if (!(r->rtm_flags & RTM_F_CLONED)) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000113 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000114 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000115 } else {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000116 if (r->rtm_flags & RTM_F_CLONED) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000117 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000118 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000119 if (filter.tb == RT_TABLE_LOCAL) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000120 if (r->rtm_type != RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000121 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000122 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000123 } else if (filter.tb == RT_TABLE_MAIN) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000124 if (r->rtm_type == RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000125 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000126 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000127 } else {
128 return 0;
129 }
130 }
131 }
132 } else {
Glenn L McGrath16528552002-11-28 11:17:19 +0000133 if (filter.tb > 0 && filter.tb != r->rtm_table) {
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 if (filter.rdst.family &&
Glenn L McGrath16528552002-11-28 11:17:19 +0000138 (r->rtm_family != filter.rdst.family || filter.rdst.bitlen > r->rtm_dst_len)) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000139 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000140 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000141 if (filter.mdst.family &&
142 (r->rtm_family != filter.mdst.family ||
Glenn L McGrath16528552002-11-28 11:17:19 +0000143 (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len))) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000144 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000145 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000146 if (filter.rsrc.family &&
Glenn L McGrath16528552002-11-28 11:17:19 +0000147 (r->rtm_family != filter.rsrc.family || filter.rsrc.bitlen > r->rtm_src_len)) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000148 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000149 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000150 if (filter.msrc.family &&
151 (r->rtm_family != filter.msrc.family ||
Glenn L McGrath16528552002-11-28 11:17:19 +0000152 (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len))) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000153 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000154 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000155
156 memset(tb, 0, sizeof(tb));
157 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
158
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000159 if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
160 return 0;
161 if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
162 inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
163 return 0;
164
165 if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
166 return 0;
167 if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
168 inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
169 return 0;
170
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000171 if (filter.flushb &&
172 r->rtm_family == AF_INET6 &&
173 r->rtm_dst_len == 0 &&
174 r->rtm_type == RTN_UNREACHABLE &&
175 tb[RTA_PRIORITY] &&
176 *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
177 return 0;
178
179 if (filter.flushb) {
180 struct nlmsghdr *fn;
181 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
182 if (flush_update())
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000183 bb_error_msg_and_die("flush");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000184 }
185 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
186 memcpy(fn, n, n->nlmsg_len);
187 fn->nlmsg_type = RTM_DELROUTE;
188 fn->nlmsg_flags = NLM_F_REQUEST;
189 fn->nlmsg_seq = ++filter.rth->seq;
190 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
Denis Vlasenko3e57adb2008-05-31 07:33:18 +0000191 filter.flushed = 1;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000192 return 0;
193 }
194
Glenn L McGrath16528552002-11-28 11:17:19 +0000195 if (n->nlmsg_type == RTM_DELROUTE) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000196 printf("Deleted ");
Glenn L McGrath16528552002-11-28 11:17:19 +0000197 }
198 if (r->rtm_type != RTN_UNICAST && !filter.type) {
Denys Vlasenkod31575a2009-10-13 17:58:24 +0200199 printf("%s ", rtnl_rtntype_n2a(r->rtm_type, b1));
Glenn L McGrath16528552002-11-28 11:17:19 +0000200 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000201
202 if (tb[RTA_DST]) {
203 if (r->rtm_dst_len != host_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000204 printf("%s/%u ", rt_addr_n2a(r->rtm_family,
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000205 RTA_DATA(tb[RTA_DST]),
206 abuf, sizeof(abuf)),
207 r->rtm_dst_len
208 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000209 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000210 printf("%s ", format_host(r->rtm_family,
211 RTA_PAYLOAD(tb[RTA_DST]),
212 RTA_DATA(tb[RTA_DST]),
213 abuf, sizeof(abuf))
214 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000215 }
216 } else if (r->rtm_dst_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000217 printf("0/%d ", r->rtm_dst_len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000218 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000219 printf("default ");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000220 }
221 if (tb[RTA_SRC]) {
222 if (r->rtm_src_len != host_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000223 printf("from %s/%u ", rt_addr_n2a(r->rtm_family,
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000224 RTA_DATA(tb[RTA_SRC]),
225 abuf, sizeof(abuf)),
226 r->rtm_src_len
227 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000228 } else {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000229 printf("from %s ", format_host(r->rtm_family,
230 RTA_PAYLOAD(tb[RTA_SRC]),
231 RTA_DATA(tb[RTA_SRC]),
232 abuf, sizeof(abuf))
233 );
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000234 }
235 } else if (r->rtm_src_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000236 printf("from 0/%u ", r->rtm_src_len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000237 }
238 if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000239 printf("via %s ", format_host(r->rtm_family,
240 RTA_PAYLOAD(tb[RTA_GATEWAY]),
241 RTA_DATA(tb[RTA_GATEWAY]),
242 abuf, sizeof(abuf)));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000243 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000244 if (tb[RTA_OIF] && filter.oifmask != -1) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000245 printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
Glenn L McGrath16528552002-11-28 11:17:19 +0000246 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247
248 if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
249 /* Do not use format_host(). It is our local addr
250 and symbolic name will not be useful.
251 */
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000252 printf(" src %s ", rt_addr_n2a(r->rtm_family,
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000253 RTA_DATA(tb[RTA_PREFSRC]),
254 abuf, sizeof(abuf)));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000255 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000256 if (tb[RTA_PRIORITY]) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000257 printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
Glenn L McGrath16528552002-11-28 11:17:19 +0000258 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000259 if (r->rtm_family == AF_INET6) {
260 struct rta_cacheinfo *ci = NULL;
Glenn L McGrath16528552002-11-28 11:17:19 +0000261 if (tb[RTA_CACHEINFO]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000262 ci = RTA_DATA(tb[RTA_CACHEINFO]);
Glenn L McGrath16528552002-11-28 11:17:19 +0000263 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000264 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000265 if (r->rtm_flags & RTM_F_CLONED) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000266 printf("%c cache ", _SL_);
Glenn L McGrath16528552002-11-28 11:17:19 +0000267 }
268 if (ci->rta_expires) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000269 printf(" expires %dsec", ci->rta_expires / get_hz());
Glenn L McGrath16528552002-11-28 11:17:19 +0000270 }
271 if (ci->rta_error != 0) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000272 printf(" error %d", ci->rta_error);
Glenn L McGrath16528552002-11-28 11:17:19 +0000273 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000274 } else if (ci) {
275 if (ci->rta_error != 0)
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000276 printf(" error %d", ci->rta_error);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000277 }
278 }
279 if (tb[RTA_IIF] && filter.iifmask != -1) {
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000280 printf(" iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000281 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000282 bb_putchar('\n');
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000283 return 0;
284}
285
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000286/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000287static int iproute_modify(int cmd, unsigned flags, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000288{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000289 static const char keywords[] ALIGN1 =
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000290 "src\0""via\0""mtu\0""lock\0""protocol\0"IF_FEATURE_IP_RULE("table\0")
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000291 "dev\0""oif\0""to\0""metric\0";
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000292 enum {
293 ARG_src,
294 ARG_via,
295 ARG_mtu, PARM_lock,
296 ARG_protocol,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000297IF_FEATURE_IP_RULE(ARG_table,)
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000298 ARG_dev,
299 ARG_oif,
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000300 ARG_to,
301 ARG_metric,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000302 };
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000303 enum {
304 gw_ok = 1 << 0,
305 dst_ok = 1 << 1,
306 proto_ok = 1 << 2,
307 type_ok = 1 << 3
308 };
309 struct rtnl_handle rth;
310 struct {
311 struct nlmsghdr n;
312 struct rtmsg r;
313 char buf[1024];
314 } req;
315 char mxbuf[256];
316 struct rtattr * mxrta = (void*)mxbuf;
317 unsigned mxlock = 0;
318 char *d = NULL;
319 smalluint ok = 0;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000320 int arg;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000321
322 memset(&req, 0, sizeof(req));
323
324 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000325 req.n.nlmsg_flags = NLM_F_REQUEST | flags;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000326 req.n.nlmsg_type = cmd;
327 req.r.rtm_family = preferred_family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000328 if (RT_TABLE_MAIN) /* if it is zero, memset already did it */
329 req.r.rtm_table = RT_TABLE_MAIN;
330 if (RT_SCOPE_NOWHERE)
331 req.r.rtm_scope = RT_SCOPE_NOWHERE;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000332
333 if (cmd != RTM_DELROUTE) {
334 req.r.rtm_protocol = RTPROT_BOOT;
335 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
336 req.r.rtm_type = RTN_UNICAST;
337 }
338
339 mxrta->rta_type = RTA_METRICS;
340 mxrta->rta_len = RTA_LENGTH(0);
341
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000342 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000343 arg = index_in_substrings(keywords, *argv);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000344 if (arg == ARG_src) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000345 inet_prefix addr;
346 NEXT_ARG();
347 get_addr(&addr, *argv, req.r.rtm_family);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000348 if (req.r.rtm_family == AF_UNSPEC)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000349 req.r.rtm_family = addr.family;
350 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000351 } else if (arg == ARG_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000352 inet_prefix addr;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000353 ok |= gw_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000354 NEXT_ARG();
355 get_addr(&addr, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000356 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000357 req.r.rtm_family = addr.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000358 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000359 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000360 } else if (arg == ARG_mtu) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000361 unsigned mtu;
362 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000363 if (index_in_strings(keywords, *argv) == PARM_lock) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000364 mxlock |= (1 << RTAX_MTU);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000365 NEXT_ARG();
366 }
Denis Vlasenko76140a72009-03-05 09:21:57 +0000367 mtu = get_unsigned(*argv, "mtu");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000368 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000369 } else if (arg == ARG_protocol) {
Eric Andersend78aea82006-01-30 18:00:02 +0000370 uint32_t prot;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000371 NEXT_ARG();
372 if (rtnl_rtprot_a2n(&prot, *argv))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000373 invarg(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000374 req.r.rtm_protocol = prot;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000375 ok |= proto_ok;
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000376#if ENABLE_FEATURE_IP_RULE
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000377 } else if (arg == ARG_table) {
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000378 uint32_t tid;
379 NEXT_ARG();
380 if (rtnl_rttable_a2n(&tid, *argv))
381 invarg(*argv, "table");
382 req.r.rtm_table = tid;
Bernhard Reutner-Fischerab51bf42007-04-16 14:56:01 +0000383#endif
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000384 } else if (arg == ARG_dev || arg == ARG_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000385 NEXT_ARG();
386 d = *argv;
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000387 } else if (arg == ARG_metric) {
388 uint32_t metric;
389 NEXT_ARG();
Denis Vlasenko76140a72009-03-05 09:21:57 +0000390 metric = get_u32(*argv, "metric");
Bernhard Reutner-Fischer578de862008-10-07 17:00:58 +0000391 addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000392 } else {
393 int type;
394 inet_prefix dst;
395
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000396 if (arg == ARG_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000397 NEXT_ARG();
398 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000399 if ((**argv < '0' || **argv > '9')
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000400 && rtnl_rtntype_a2n(&type, *argv) == 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000401 NEXT_ARG();
402 req.r.rtm_type = type;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000403 ok |= type_ok;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000404 }
405
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000406 if (ok & dst_ok) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000407 duparg2("to", *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000408 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000409 get_prefix(&dst, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000410 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000411 req.r.rtm_family = dst.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000412 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000413 req.r.rtm_dst_len = dst.bitlen;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000414 ok |= dst_ok;
Glenn L McGrath16528552002-11-28 11:17:19 +0000415 if (dst.bytelen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000416 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Glenn L McGrath16528552002-11-28 11:17:19 +0000417 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000418 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000419 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000420 }
421
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000422 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000423
Eric Andersen5780adb2002-11-15 09:12:47 +0000424 if (d) {
425 int idx;
426
427 ll_init_map(&rth);
428
429 if (d) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000430 idx = xll_name_to_index(d);
Eric Andersen5780adb2002-11-15 09:12:47 +0000431 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
432 }
433 }
434
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000435 if (mxrta->rta_len > RTA_LENGTH(0)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000436 if (mxlock) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000437 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
Glenn L McGrath16528552002-11-28 11:17:19 +0000438 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000439 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
440 }
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000441
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000442 if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
443 req.r.rtm_scope = RT_SCOPE_HOST;
444 else if (req.r.rtm_type == RTN_BROADCAST ||
445 req.r.rtm_type == RTN_MULTICAST ||
446 req.r.rtm_type == RTN_ANYCAST)
447 req.r.rtm_scope = RT_SCOPE_LINK;
448 else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
449 if (cmd == RTM_DELROUTE)
450 req.r.rtm_scope = RT_SCOPE_NOWHERE;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000451 else if (!(ok & gw_ok))
Bernhard Reutner-Fischer12c96a62007-04-11 16:23:57 +0000452 req.r.rtm_scope = RT_SCOPE_LINK;
453 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000454
Glenn L McGrath16528552002-11-28 11:17:19 +0000455 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000456 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000457 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000458
Glenn L McGrath16528552002-11-28 11:17:19 +0000459 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000460 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000461 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000462
463 return 0;
464}
465
466static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
467{
468 struct {
469 struct nlmsghdr nlh;
470 struct rtmsg rtm;
471 } req;
472 struct sockaddr_nl nladdr;
473
474 memset(&nladdr, 0, sizeof(nladdr));
475 memset(&req, 0, sizeof(req));
476 nladdr.nl_family = AF_NETLINK;
477
478 req.nlh.nlmsg_len = sizeof(req);
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000479 if (RTM_GETROUTE)
480 req.nlh.nlmsg_type = RTM_GETROUTE;
481 if (NLM_F_ROOT | NLM_F_REQUEST)
482 req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
483 /*req.nlh.nlmsg_pid = 0; - memset did it already */
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000484 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
485 req.rtm.rtm_family = family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000486 if (RTM_F_CLONED)
487 req.rtm.rtm_flags = RTM_F_CLONED;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000488
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000489 return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000490}
491
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000492static void iproute_flush_cache(void)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000493{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000494 static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
Denis Vlasenko50f7f442007-04-11 23:20:53 +0000495 int flush_fd = open_or_warn(fn, O_WRONLY);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000496
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000497 if (flush_fd < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000498 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000499 }
500
Denis Vlasenko89ef65f2007-01-29 23:43:18 +0000501 if (write(flush_fd, "-1", 2) < 2) {
502 bb_perror_msg("cannot flush routing cache");
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000503 return;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000504 }
505 close(flush_fd);
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000506}
507
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000508static void iproute_reset_filter(void)
509{
510 memset(&filter, 0, sizeof(filter));
511 filter.mdst.bitlen = -1;
512 filter.msrc.bitlen = -1;
513}
514
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000515/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000516static int iproute_list_or_flush(char **argv, int flush)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000517{
518 int do_ipv6 = preferred_family;
519 struct rtnl_handle rth;
520 char *id = NULL;
521 char *od = NULL;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000522 static const char keywords[] ALIGN1 =
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000523 /* "ip route list/flush" parameters: */
524 "protocol\0" "dev\0" "oif\0" "iif\0"
525 "via\0" "table\0" "cache\0"
526 "from\0" "to\0"
527 /* and possible further keywords */
528 "all\0"
529 "root\0"
530 "match\0"
531 "exact\0"
532 "main\0"
533 ;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000534 enum {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000535 KW_proto, KW_dev, KW_oif, KW_iif,
536 KW_via, KW_table, KW_cache,
537 KW_from, KW_to,
538 /* */
539 KW_all,
540 KW_root,
541 KW_match,
542 KW_exact,
543 KW_main,
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000544 };
545 int arg, parm;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000546
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000547 iproute_reset_filter();
548 filter.tb = RT_TABLE_MAIN;
549
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000550 if (flush && !*argv)
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000551 bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000552
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000553 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000554 arg = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000555 if (arg == KW_proto) {
Eric Andersend78aea82006-01-30 18:00:02 +0000556 uint32_t prot = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000557 NEXT_ARG();
558 filter.protocolmask = -1;
559 if (rtnl_rtprot_a2n(&prot, *argv)) {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000560 if (index_in_strings(keywords, *argv) != KW_all)
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000561 invarg(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000562 prot = 0;
563 filter.protocolmask = 0;
564 }
565 filter.protocol = prot;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000566 } else if (arg == KW_dev || arg == KW_oif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000567 NEXT_ARG();
568 od = *argv;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000569 } else if (arg == KW_iif) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000570 NEXT_ARG();
571 id = *argv;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000572 } else if (arg == KW_via) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000573 NEXT_ARG();
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000574 get_prefix(&filter.rvia, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000575 } else if (arg == KW_table) { /* table all/cache/main */
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000576 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000577 parm = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000578 if (parm == KW_cache)
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000579 filter.tb = -1;
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000580 else if (parm == KW_all)
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000581 filter.tb = 0;
Denis Vlasenko52a8d972008-06-08 00:25:55 +0000582 else if (parm != KW_main) {
583#if ENABLE_FEATURE_IP_RULE
584 uint32_t tid;
585 if (rtnl_rttable_a2n(&tid, *argv))
586 invarg(*argv, "table");
587 filter.tb = tid;
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000588#else
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000589 invarg(*argv, "table");
Denis Vlasenko52a8d972008-06-08 00:25:55 +0000590#endif
591 }
Denis Vlasenko79c69042007-11-27 09:42:33 +0000592 } else if (arg == KW_cache) {
593 /* The command 'ip route flush cache' is used by OpenSWAN.
594 * Assuming it's a synonym for 'ip route flush table cache' */
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000595 filter.tb = -1;
596 } else if (arg == KW_from) {
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000597 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000598 parm = index_in_substrings(keywords, *argv);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000599 if (parm == KW_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000600 NEXT_ARG();
601 get_prefix(&filter.rsrc, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000602 } else if (parm == KW_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000603 NEXT_ARG();
604 get_prefix(&filter.msrc, *argv, do_ipv6);
605 } else {
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000606 if (parm == KW_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000607 NEXT_ARG();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000608 get_prefix(&filter.msrc, *argv, do_ipv6);
609 filter.rsrc = filter.msrc;
610 }
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000611 } else { /* "to" is the default parameter */
612 if (arg == KW_to) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000613 NEXT_ARG();
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000614 arg = index_in_substrings(keywords, *argv);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000615 }
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000616 /* parm = arg; - would be more plausible, but we reuse 'arg' here */
617 if (arg == KW_root) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000618 NEXT_ARG();
619 get_prefix(&filter.rdst, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000620 } else if (arg == KW_match) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000621 NEXT_ARG();
622 get_prefix(&filter.mdst, *argv, do_ipv6);
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000623 } else { /* "to exact" is the default */
624 if (arg == KW_exact)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000625 NEXT_ARG();
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000626 get_prefix(&filter.mdst, *argv, do_ipv6);
627 filter.rdst = filter.mdst;
628 }
629 }
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000630 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000631 }
632
Glenn L McGrath16528552002-11-28 11:17:19 +0000633 if (do_ipv6 == AF_UNSPEC && filter.tb) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000634 do_ipv6 = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000635 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000636
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000637 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000638 ll_init_map(&rth);
639
640 if (id || od) {
641 int idx;
642
643 if (id) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000644 idx = xll_name_to_index(id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000645 filter.iif = idx;
646 filter.iifmask = -1;
647 }
648 if (od) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000649 idx = xll_name_to_index(od);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000650 filter.oif = idx;
651 filter.oifmask = -1;
652 }
653 }
654
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000655 if (flush) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000656 char flushb[4096-512];
657
Denis Vlasenko186c2b32007-11-26 18:29:52 +0000658 if (filter.tb == -1) { /* "flush table cache" */
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000659 if (do_ipv6 != AF_INET6)
660 iproute_flush_cache();
661 if (do_ipv6 == AF_INET)
662 return 0;
663 }
664
665 filter.flushb = flushb;
666 filter.flushp = 0;
667 filter.flushe = sizeof(flushb);
668 filter.rth = &rth;
669
670 for (;;) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000671 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000672 filter.flushed = 0;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000673 xrtnl_dump_filter(&rth, print_route, NULL);
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000674 if (filter.flushed == 0)
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000675 return 0;
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000676 if (flush_update())
677 return 1;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000678 }
679 }
680
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000681 if (filter.tb != -1) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000682 xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000683 } else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
684 bb_perror_msg_and_die("cannot send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000685 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000686 xrtnl_dump_filter(&rth, print_route, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000687
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000688 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000689}
690
691
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000692/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000693static int iproute_get(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000694{
695 struct rtnl_handle rth;
696 struct {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000697 struct nlmsghdr n;
698 struct rtmsg r;
699 char buf[1024];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000700 } req;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000701 char *idev = NULL;
702 char *odev = NULL;
Bernhard Reutner-Fischerc98c3172007-04-12 11:36:56 +0000703 bool connected = 0;
704 bool from_ok = 0;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000705 static const char options[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000706 "from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000707
708 memset(&req, 0, sizeof(req));
709
710 iproute_reset_filter();
711
712 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000713 if (NLM_F_REQUEST)
714 req.n.nlmsg_flags = NLM_F_REQUEST;
715 if (RTM_GETROUTE)
716 req.n.nlmsg_type = RTM_GETROUTE;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000717 req.r.rtm_family = preferred_family;
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000718 /*req.r.rtm_table = 0; - memset did this already */
719 /*req.r.rtm_protocol = 0;*/
720 /*req.r.rtm_scope = 0;*/
721 /*req.r.rtm_type = 0;*/
722 /*req.r.rtm_src_len = 0;*/
723 /*req.r.rtm_dst_len = 0;*/
724 /*req.r.rtm_tos = 0;*/
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000725
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000726 while (*argv) {
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000727 switch (index_in_strings(options, *argv)) {
Glenn L McGrath18eae002002-12-02 00:54:10 +0000728 case 0: /* from */
729 {
730 inet_prefix addr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000731 NEXT_ARG();
Glenn L McGrath18eae002002-12-02 00:54:10 +0000732 from_ok = 1;
733 get_prefix(&addr, *argv, req.r.rtm_family);
734 if (req.r.rtm_family == AF_UNSPEC) {
735 req.r.rtm_family = addr.family;
736 }
737 if (addr.bytelen) {
738 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
739 }
740 req.r.rtm_src_len = addr.bitlen;
741 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000742 }
Glenn L McGrath18eae002002-12-02 00:54:10 +0000743 case 1: /* iif */
744 NEXT_ARG();
745 idev = *argv;
746 break;
747 case 2: /* oif */
748 case 3: /* dev */
749 NEXT_ARG();
750 odev = *argv;
751 break;
752 case 4: /* notify */
753 req.r.rtm_flags |= RTM_F_NOTIFY;
754 break;
755 case 5: /* connected */
756 connected = 1;
757 break;
758 case 6: /* to */
759 NEXT_ARG();
760 default:
761 {
762 inet_prefix addr;
763 get_prefix(&addr, *argv, req.r.rtm_family);
764 if (req.r.rtm_family == AF_UNSPEC) {
765 req.r.rtm_family = addr.family;
766 }
767 if (addr.bytelen) {
768 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
769 }
770 req.r.rtm_dst_len = addr.bitlen;
771 }
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000772 argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000773 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000774 }
775
776 if (req.r.rtm_dst_len == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000777 bb_error_msg_and_die("need at least destination address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000778 }
779
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000780 xrtnl_open(&rth);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000781
782 ll_init_map(&rth);
783
784 if (idev || odev) {
785 int idx;
786
787 if (idev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000788 idx = xll_name_to_index(idev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000789 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
790 }
791 if (odev) {
Bernhard Reutner-Fischerb2908892007-04-12 11:34:39 +0000792 idx = xll_name_to_index(odev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000793 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
794 }
795 }
796
Glenn L McGrath16528552002-11-28 11:17:19 +0000797 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000798 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000799 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000800
Glenn L McGrath16528552002-11-28 11:17:19 +0000801 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000802 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000803 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000804
805 if (connected && !from_ok) {
806 struct rtmsg *r = NLMSG_DATA(&req.n);
807 int len = req.n.nlmsg_len;
808 struct rtattr * tb[RTA_MAX+1];
809
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000810 print_route(NULL, &req.n, NULL);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000811
812 if (req.n.nlmsg_type != RTM_NEWROUTE) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000813 bb_error_msg_and_die("not a route?");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000814 }
815 len -= NLMSG_LENGTH(sizeof(*r));
816 if (len < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000817 bb_error_msg_and_die("wrong len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000818 }
819
820 memset(tb, 0, sizeof(tb));
821 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
822
823 if (tb[RTA_PREFSRC]) {
824 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
825 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
826 } else if (!tb[RTA_SRC]) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000827 bb_error_msg_and_die("failed to connect the route");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000828 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000829 if (!odev && tb[RTA_OIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000830 tb[RTA_OIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000831 }
832 if (tb[RTA_GATEWAY]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000833 tb[RTA_GATEWAY]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000834 }
835 if (!idev && tb[RTA_IIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000836 tb[RTA_IIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000837 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000838 req.n.nlmsg_flags = NLM_F_REQUEST;
839 req.n.nlmsg_type = RTM_GETROUTE;
840
Glenn L McGrath16528552002-11-28 11:17:19 +0000841 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000842 return 2;
Glenn L McGrath16528552002-11-28 11:17:19 +0000843 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000844 }
Denis Vlasenko0db2c2e2008-06-29 06:22:40 +0000845 print_route(NULL, &req.n, NULL);
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000846 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000847}
848
Denis Vlasenko540a2a12007-04-07 01:14:45 +0000849/* Return value becomes exitcode. It's okay to not return at all */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000850int do_iproute(char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000851{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +0000852 static const char ip_route_commands[] ALIGN1 =
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000853 /*0-3*/ "add\0""append\0""change\0""chg\0"
854 /*4-7*/ "delete\0""get\0""list\0""show\0"
855 /*8..*/ "prepend\0""replace\0""test\0""flush\0";
Denis Vlasenko83c44222008-01-04 15:28:28 +0000856 int command_num;
Denis Vlasenko990d0f62007-07-24 15:54:42 +0000857 unsigned flags = 0;
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000858 int cmd = RTM_NEWROUTE;
859
Denis Vlasenko83c44222008-01-04 15:28:28 +0000860 if (!*argv)
861 return iproute_list_or_flush(argv, 0);
862
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000863 /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
864 /* It probably means that it is using "first match" rule */
Denis Vlasenko83c44222008-01-04 15:28:28 +0000865 command_num = index_in_substrings(ip_route_commands, *argv);
866
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000867 switch (command_num) {
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000868 case 0: /* add */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000869 flags = NLM_F_CREATE|NLM_F_EXCL;
870 break;
871 case 1: /* append */
872 flags = NLM_F_CREATE|NLM_F_APPEND;
873 break;
874 case 2: /* change */
875 case 3: /* chg */
876 flags = NLM_F_REPLACE;
877 break;
878 case 4: /* delete */
879 cmd = RTM_DELROUTE;
880 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000881 case 5: /* get */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000882 return iproute_get(argv+1);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000883 case 6: /* list */
884 case 7: /* show */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000885 return iproute_list_or_flush(argv+1, 0);
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000886 case 8: /* prepend */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000887 flags = NLM_F_CREATE;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000888 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000889 case 9: /* replace */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000890 flags = NLM_F_CREATE|NLM_F_REPLACE;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000891 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000892 case 10: /* test */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000893 flags = NLM_F_EXCL;
Denis Vlasenko1eecaf22007-09-30 16:04:21 +0000894 break;
Denis Vlasenkofd94efa2007-01-27 13:13:45 +0000895 case 11: /* flush */
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000896 return iproute_list_or_flush(argv+1, 1);
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000897 default:
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000898 bb_error_msg_and_die("unknown command %s", *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000899 }
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000900
Denis Vlasenkoed6a49c2007-11-18 22:56:25 +0000901 return iproute_modify(cmd, flags, argv+1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000902}