blob: c4c3fc2ca5d32b18d3b390a3b0a625ed517a812d [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
Rob Landleyecae66a2006-06-02 20:53:38 +000016#include "libbb.h"
17
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000018#include "rt_names.h"
19#include "utils.h"
Bernhard Reutner-Fischer9a67ca32006-04-02 21:14:19 +000020#include "ip_common.h"
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000021
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000022#ifndef RTAX_RTTVAR
23#define RTAX_RTTVAR RTAX_HOPS
24#endif
25
26
27static struct
28{
29 int tb;
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000030 int flushed;
31 char *flushb;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000032 int flushp;
33 int flushe;
34 struct rtnl_handle *rth;
35 int protocol, protocolmask;
36 int scope, scopemask;
37 int type, typemask;
38 int tos, tosmask;
39 int iif, iifmask;
40 int oif, oifmask;
41 int realm, realmmask;
42 inet_prefix rprefsrc;
43 inet_prefix rvia;
44 inet_prefix rdst;
45 inet_prefix mdst;
46 inet_prefix rsrc;
47 inet_prefix msrc;
48} filter;
49
Glenn L McGrath4a4c6772003-02-15 11:50:33 +000050static int flush_update(void)
51{
52 if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
53 perror("Failed to send flush request\n");
54 return -1;
55 }
56 filter.flushp = 0;
57 return 0;
58}
59
Denis Vlasenkoc5193202006-12-31 19:07:23 +000060static unsigned get_hz(void)
Denis Vlasenko13463af2006-12-31 18:58:32 +000061{
Denis Vlasenkoc5193202006-12-31 19:07:23 +000062 static unsigned hz_internal;
Denis Vlasenko13463af2006-12-31 18:58:32 +000063 FILE *fp;
64
65 if (hz_internal)
66 return hz_internal;
67
68 fp = fopen("/proc/net/psched", "r");
69 if (fp) {
70 unsigned nom, denom;
71
72 if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
73 if (nom == 1000000)
74 hz_internal = denom;
75 fclose(fp);
76 }
77 if (!hz_internal)
78 hz_internal = sysconf(_SC_CLK_TCK);
79 return hz_internal;
80}
81
Bernhard Reutner-Fischer86f5c992006-01-22 22:55:11 +000082static int print_route(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
Bernhard Reutner-Fischerd1d23a62006-01-12 12:08:46 +000083 struct nlmsghdr *n, void *arg)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +000084{
85 FILE *fp = (FILE*)arg;
86 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
96 if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
97 fprintf(stderr, "Not a route: %08x %08x %08x\n",
98 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
99 return 0;
100 }
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000101 if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
102 return 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000103 len -= NLMSG_LENGTH(sizeof(*r));
104 if (len < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 bb_error_msg("wrong nlmsg len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000106 return -1;
107 }
108
109 if (r->rtm_family == AF_INET6)
110 host_len = 128;
111 else if (r->rtm_family == AF_INET)
112 host_len = 32;
113
114 if (r->rtm_family == AF_INET6) {
115 if (filter.tb) {
116 if (filter.tb < 0) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000117 if (!(r->rtm_flags&RTM_F_CLONED)) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000118 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000119 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000120 } else {
Glenn L McGrath16528552002-11-28 11:17:19 +0000121 if (r->rtm_flags&RTM_F_CLONED) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000122 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000123 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000124 if (filter.tb == RT_TABLE_LOCAL) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000125 if (r->rtm_type != RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000126 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000127 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000128 } else if (filter.tb == RT_TABLE_MAIN) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000129 if (r->rtm_type == RTN_LOCAL) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000130 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000131 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000132 } else {
133 return 0;
134 }
135 }
136 }
137 } else {
Glenn L McGrath16528552002-11-28 11:17:19 +0000138 if (filter.tb > 0 && filter.tb != r->rtm_table) {
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 }
142 if (filter.rdst.family &&
Glenn L McGrath16528552002-11-28 11:17:19 +0000143 (r->rtm_family != filter.rdst.family || filter.rdst.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.mdst.family &&
147 (r->rtm_family != filter.mdst.family ||
Glenn L McGrath16528552002-11-28 11:17:19 +0000148 (filter.mdst.bitlen >= 0 && filter.mdst.bitlen < r->rtm_dst_len))) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000149 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000150 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000151 if (filter.rsrc.family &&
Glenn L McGrath16528552002-11-28 11:17:19 +0000152 (r->rtm_family != filter.rsrc.family || filter.rsrc.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 if (filter.msrc.family &&
156 (r->rtm_family != filter.msrc.family ||
Glenn L McGrath16528552002-11-28 11:17:19 +0000157 (filter.msrc.bitlen >= 0 && filter.msrc.bitlen < r->rtm_src_len))) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000158 return 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000159 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000160
161 memset(tb, 0, sizeof(tb));
162 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
163
Glenn L McGrathfbf0b8a2003-04-26 02:22:19 +0000164 if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
165 return 0;
166 if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
167 inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
168 return 0;
169
170 if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
171 return 0;
172 if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
173 inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
174 return 0;
175
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000176 if (filter.flushb &&
177 r->rtm_family == AF_INET6 &&
178 r->rtm_dst_len == 0 &&
179 r->rtm_type == RTN_UNREACHABLE &&
180 tb[RTA_PRIORITY] &&
181 *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1)
182 return 0;
183
184 if (filter.flushb) {
185 struct nlmsghdr *fn;
186 if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
187 if (flush_update())
188 return -1;
189 }
190 fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
191 memcpy(fn, n, n->nlmsg_len);
192 fn->nlmsg_type = RTM_DELROUTE;
193 fn->nlmsg_flags = NLM_F_REQUEST;
194 fn->nlmsg_seq = ++filter.rth->seq;
195 filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
196 filter.flushed++;
197 return 0;
198 }
199
Glenn L McGrath16528552002-11-28 11:17:19 +0000200 if (n->nlmsg_type == RTM_DELROUTE) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000201 fprintf(fp, "Deleted ");
Glenn L McGrath16528552002-11-28 11:17:19 +0000202 }
203 if (r->rtm_type != RTN_UNICAST && !filter.type) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000204 fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
Glenn L McGrath16528552002-11-28 11:17:19 +0000205 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000206
207 if (tb[RTA_DST]) {
208 if (r->rtm_dst_len != host_len) {
209 fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
210 RTA_PAYLOAD(tb[RTA_DST]),
211 RTA_DATA(tb[RTA_DST]),
212 abuf, sizeof(abuf)),
213 r->rtm_dst_len
214 );
215 } else {
216 fprintf(fp, "%s ", format_host(r->rtm_family,
217 RTA_PAYLOAD(tb[RTA_DST]),
218 RTA_DATA(tb[RTA_DST]),
219 abuf, sizeof(abuf))
220 );
221 }
222 } else if (r->rtm_dst_len) {
223 fprintf(fp, "0/%d ", r->rtm_dst_len);
224 } else {
225 fprintf(fp, "default ");
226 }
227 if (tb[RTA_SRC]) {
228 if (r->rtm_src_len != host_len) {
229 fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
230 RTA_PAYLOAD(tb[RTA_SRC]),
231 RTA_DATA(tb[RTA_SRC]),
232 abuf, sizeof(abuf)),
233 r->rtm_src_len
234 );
235 } else {
236 fprintf(fp, "from %s ", format_host(r->rtm_family,
237 RTA_PAYLOAD(tb[RTA_SRC]),
238 RTA_DATA(tb[RTA_SRC]),
239 abuf, sizeof(abuf))
240 );
241 }
242 } else if (r->rtm_src_len) {
243 fprintf(fp, "from 0/%u ", r->rtm_src_len);
244 }
245 if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000246 fprintf(fp, "via %s ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000247 format_host(r->rtm_family,
248 RTA_PAYLOAD(tb[RTA_GATEWAY]),
249 RTA_DATA(tb[RTA_GATEWAY]),
250 abuf, sizeof(abuf)));
251 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000252 if (tb[RTA_OIF] && filter.oifmask != -1) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000253 fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
Glenn L McGrath16528552002-11-28 11:17:19 +0000254 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000255
256 if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
257 /* Do not use format_host(). It is our local addr
258 and symbolic name will not be useful.
259 */
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000260 fprintf(fp, " src %s ",
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000261 rt_addr_n2a(r->rtm_family,
262 RTA_PAYLOAD(tb[RTA_PREFSRC]),
263 RTA_DATA(tb[RTA_PREFSRC]),
264 abuf, sizeof(abuf)));
265 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000266 if (tb[RTA_PRIORITY]) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000267 fprintf(fp, " metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
Glenn L McGrath16528552002-11-28 11:17:19 +0000268 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000269 if (r->rtm_family == AF_INET6) {
270 struct rta_cacheinfo *ci = NULL;
Glenn L McGrath16528552002-11-28 11:17:19 +0000271 if (tb[RTA_CACHEINFO]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000272 ci = RTA_DATA(tb[RTA_CACHEINFO]);
Glenn L McGrath16528552002-11-28 11:17:19 +0000273 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000274 if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000275 if (r->rtm_flags & RTM_F_CLONED) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000276 fprintf(fp, "%s cache ", _SL_);
Glenn L McGrath16528552002-11-28 11:17:19 +0000277 }
278 if (ci->rta_expires) {
Denis Vlasenkoc5193202006-12-31 19:07:23 +0000279 fprintf(fp, " expires %dsec", ci->rta_expires / get_hz());
Glenn L McGrath16528552002-11-28 11:17:19 +0000280 }
281 if (ci->rta_error != 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000282 fprintf(fp, " error %d", ci->rta_error);
Glenn L McGrath16528552002-11-28 11:17:19 +0000283 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000284 } else if (ci) {
285 if (ci->rta_error != 0)
286 fprintf(fp, " error %d", ci->rta_error);
287 }
288 }
289 if (tb[RTA_IIF] && filter.iifmask != -1) {
290 fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
291 }
292 fprintf(fp, "\n");
293 fflush(fp);
294 return 0;
295}
296
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000297static int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000298{
299 struct rtnl_handle rth;
300 struct {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000301 struct nlmsghdr n;
302 struct rtmsg r;
303 char buf[1024];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000304 } req;
305 char mxbuf[256];
306 struct rtattr * mxrta = (void*)mxbuf;
307 unsigned mxlock = 0;
308 char *d = NULL;
309 int gw_ok = 0;
310 int dst_ok = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000311 int proto_ok = 0;
312 int type_ok = 0;
313
314 memset(&req, 0, sizeof(req));
315
316 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
317 req.n.nlmsg_flags = NLM_F_REQUEST|flags;
318 req.n.nlmsg_type = cmd;
319 req.r.rtm_family = preferred_family;
320 req.r.rtm_table = RT_TABLE_MAIN;
321 req.r.rtm_scope = RT_SCOPE_NOWHERE;
322
323 if (cmd != RTM_DELROUTE) {
324 req.r.rtm_protocol = RTPROT_BOOT;
325 req.r.rtm_scope = RT_SCOPE_UNIVERSE;
326 req.r.rtm_type = RTN_UNICAST;
327 }
328
329 mxrta->rta_type = RTA_METRICS;
330 mxrta->rta_len = RTA_LENGTH(0);
331
332 while (argc > 0) {
333 if (strcmp(*argv, "src") == 0) {
334 inet_prefix addr;
335 NEXT_ARG();
336 get_addr(&addr, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000337 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000338 req.r.rtm_family = addr.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000339 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000340 addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
341 } else if (strcmp(*argv, "via") == 0) {
342 inet_prefix addr;
343 gw_ok = 1;
344 NEXT_ARG();
345 get_addr(&addr, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000346 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000347 req.r.rtm_family = addr.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000348 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000349 addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
350 } else if (strcmp(*argv, "mtu") == 0) {
351 unsigned mtu;
352 NEXT_ARG();
353 if (strcmp(*argv, "lock") == 0) {
354 mxlock |= (1<<RTAX_MTU);
355 NEXT_ARG();
356 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000357 if (get_unsigned(&mtu, *argv, 0)) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000358 invarg(*argv, "mtu");
Glenn L McGrath16528552002-11-28 11:17:19 +0000359 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000360 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
361 } else if (matches(*argv, "protocol") == 0) {
Eric Andersend78aea82006-01-30 18:00:02 +0000362 uint32_t prot;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000363 NEXT_ARG();
364 if (rtnl_rtprot_a2n(&prot, *argv))
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000365 invarg(*argv, "protocol");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000366 req.r.rtm_protocol = prot;
367 proto_ok =1;
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000368#if ENABLE_FEATURE_IP_RULE
369 } else if (matches(*argv, "table") == 0) {
370 uint32_t tid;
371 NEXT_ARG();
372 if (rtnl_rttable_a2n(&tid, *argv))
373 invarg(*argv, "table");
374 req.r.rtm_table = tid;
375#endif
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000376 } else if (strcmp(*argv, "dev") == 0 ||
377 strcmp(*argv, "oif") == 0) {
378 NEXT_ARG();
379 d = *argv;
380 } else {
381 int type;
382 inet_prefix dst;
383
384 if (strcmp(*argv, "to") == 0) {
385 NEXT_ARG();
386 }
387 if ((**argv < '0' || **argv > '9') &&
388 rtnl_rtntype_a2n(&type, *argv) == 0) {
389 NEXT_ARG();
390 req.r.rtm_type = type;
391 type_ok = 1;
392 }
393
Glenn L McGrath16528552002-11-28 11:17:19 +0000394 if (dst_ok) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000395 duparg2("to", *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000396 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000397 get_prefix(&dst, *argv, req.r.rtm_family);
Glenn L McGrath16528552002-11-28 11:17:19 +0000398 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000399 req.r.rtm_family = dst.family;
Glenn L McGrath16528552002-11-28 11:17:19 +0000400 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000401 req.r.rtm_dst_len = dst.bitlen;
402 dst_ok = 1;
Glenn L McGrath16528552002-11-28 11:17:19 +0000403 if (dst.bytelen) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000404 addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
Glenn L McGrath16528552002-11-28 11:17:19 +0000405 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000406 }
407 argc--; argv++;
408 }
409
Glenn L McGrath16528552002-11-28 11:17:19 +0000410 if (rtnl_open(&rth, 0) < 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000411 exit(1);
Glenn L McGrath16528552002-11-28 11:17:19 +0000412 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000413
Eric Andersen5780adb2002-11-15 09:12:47 +0000414 if (d) {
415 int idx;
416
417 ll_init_map(&rth);
418
419 if (d) {
Denis Vlasenko98ee06d2006-12-31 18:57:37 +0000420 idx = ll_name_to_index(d);
421 if (idx == 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000422 bb_error_msg("cannot find device \"%s\"", d);
Eric Andersen5780adb2002-11-15 09:12:47 +0000423 return -1;
424 }
425 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
426 }
427 }
428
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000429 if (mxrta->rta_len > RTA_LENGTH(0)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000430 if (mxlock) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000431 rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
Glenn L McGrath16528552002-11-28 11:17:19 +0000432 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000433 addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
434 }
435
Glenn L McGrath16528552002-11-28 11:17:19 +0000436 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000437 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000438 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000439
Glenn L McGrath16528552002-11-28 11:17:19 +0000440 if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000441 exit(2);
Glenn L McGrath16528552002-11-28 11:17:19 +0000442 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000443
444 return 0;
445}
446
447static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
448{
449 struct {
450 struct nlmsghdr nlh;
451 struct rtmsg rtm;
452 } req;
453 struct sockaddr_nl nladdr;
454
455 memset(&nladdr, 0, sizeof(nladdr));
456 memset(&req, 0, sizeof(req));
457 nladdr.nl_family = AF_NETLINK;
458
459 req.nlh.nlmsg_len = sizeof(req);
460 req.nlh.nlmsg_type = RTM_GETROUTE;
461 req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
462 req.nlh.nlmsg_pid = 0;
463 req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
464 req.rtm.rtm_family = family;
465 req.rtm.rtm_flags |= RTM_F_CLONED;
466
467 return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
468}
469
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000470static int iproute_flush_cache(void)
471{
472#define ROUTE_FLUSH_PATH "/proc/sys/net/ipv4/route/flush"
473
474 int len;
475 int flush_fd = open (ROUTE_FLUSH_PATH, O_WRONLY);
476 char *buffer = "-1";
477
478 if (flush_fd < 0) {
Denis Vlasenko92258542006-11-01 10:25:35 +0000479 fprintf(stderr, "Cannot open \"%s\"\n", ROUTE_FLUSH_PATH);
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000480 return -1;
481 }
482
483 len = strlen (buffer);
484
485 if ((write (flush_fd, (void *)buffer, len)) < len) {
Denis Vlasenko92258542006-11-01 10:25:35 +0000486 fprintf(stderr, "Cannot flush routing cache\n");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000487 return -1;
488 }
489 close(flush_fd);
490 return 0;
491}
492
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000493static void iproute_reset_filter(void)
494{
495 memset(&filter, 0, sizeof(filter));
496 filter.mdst.bitlen = -1;
497 filter.msrc.bitlen = -1;
498}
499
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000500static int iproute_list_or_flush(int argc, char **argv, int flush)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000501{
502 int do_ipv6 = preferred_family;
503 struct rtnl_handle rth;
504 char *id = NULL;
505 char *od = NULL;
506
507 iproute_reset_filter();
508 filter.tb = RT_TABLE_MAIN;
509
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000510 if (flush && argc <= 0) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000511 bb_error_msg(bb_msg_requires_arg, "\"ip route flush\"");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000512 return -1;
513 }
514
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000515 while (argc > 0) {
516 if (matches(*argv, "protocol") == 0) {
Eric Andersend78aea82006-01-30 18:00:02 +0000517 uint32_t prot = 0;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000518 NEXT_ARG();
519 filter.protocolmask = -1;
520 if (rtnl_rtprot_a2n(&prot, *argv)) {
Glenn L McGrath16528552002-11-28 11:17:19 +0000521 if (strcmp(*argv, "all") != 0) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000522 invarg(*argv, "protocol");
Glenn L McGrath16528552002-11-28 11:17:19 +0000523 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000524 prot = 0;
525 filter.protocolmask = 0;
526 }
527 filter.protocol = prot;
528 } else if (strcmp(*argv, "dev") == 0 ||
529 strcmp(*argv, "oif") == 0) {
530 NEXT_ARG();
531 od = *argv;
532 } else if (strcmp(*argv, "iif") == 0) {
533 NEXT_ARG();
534 id = *argv;
535 } else if (matches(*argv, "from") == 0) {
536 NEXT_ARG();
537 if (matches(*argv, "root") == 0) {
538 NEXT_ARG();
539 get_prefix(&filter.rsrc, *argv, do_ipv6);
540 } else if (matches(*argv, "match") == 0) {
541 NEXT_ARG();
542 get_prefix(&filter.msrc, *argv, do_ipv6);
543 } else {
544 if (matches(*argv, "exact") == 0) {
545 NEXT_ARG();
546 }
547 get_prefix(&filter.msrc, *argv, do_ipv6);
548 filter.rsrc = filter.msrc;
549 }
550 } else {
551 if (matches(*argv, "to") == 0) {
552 NEXT_ARG();
553 }
554 if (matches(*argv, "root") == 0) {
555 NEXT_ARG();
556 get_prefix(&filter.rdst, *argv, do_ipv6);
557 } else if (matches(*argv, "match") == 0) {
558 NEXT_ARG();
559 get_prefix(&filter.mdst, *argv, do_ipv6);
Paul Fox5dc0cee2005-07-20 19:01:05 +0000560 } else if (matches(*argv, "table") == 0) {
561 NEXT_ARG();
562 if (matches(*argv, "cache") == 0) {
563 filter.tb = -1;
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000564#if 0 && ENABLE_FEATURE_IP_RULE
Denis Vlasenkof7996f32007-01-11 17:20:00 +0000565
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000566#else
Paul Fox5dc0cee2005-07-20 19:01:05 +0000567 } else if (matches(*argv, "main") != 0) {
Bernhard Reutner-Fischer19008b82006-06-07 20:17:41 +0000568 invarg(*argv, "table");
Paul Fox5dc0cee2005-07-20 19:01:05 +0000569 }
Denis Vlasenkocda6c632006-12-15 00:59:35 +0000570#endif
Paul Fox5dc0cee2005-07-20 19:01:05 +0000571 } else if (matches(*argv, "cache") == 0) {
572 filter.tb = -1;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000573 } else {
574 if (matches(*argv, "exact") == 0) {
575 NEXT_ARG();
576 }
577 get_prefix(&filter.mdst, *argv, do_ipv6);
578 filter.rdst = filter.mdst;
579 }
580 }
581 argc--; argv++;
582 }
583
Glenn L McGrath16528552002-11-28 11:17:19 +0000584 if (do_ipv6 == AF_UNSPEC && filter.tb) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000585 do_ipv6 = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000586 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000587
Glenn L McGrath16528552002-11-28 11:17:19 +0000588 if (rtnl_open(&rth, 0) < 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000589 exit(1);
Glenn L McGrath16528552002-11-28 11:17:19 +0000590 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000591
592 ll_init_map(&rth);
593
594 if (id || od) {
595 int idx;
596
597 if (id) {
598 if ((idx = ll_name_to_index(id)) == 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000599 bb_error_msg("cannot find device \"%s\"", id);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000600 return -1;
601 }
602 filter.iif = idx;
603 filter.iifmask = -1;
604 }
605 if (od) {
606 if ((idx = ll_name_to_index(od)) == 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000607 bb_error_msg("cannot find device \"%s\"", od);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000608 }
609 filter.oif = idx;
610 filter.oifmask = -1;
611 }
612 }
613
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000614 if (flush) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000615 char flushb[4096-512];
616
617 if (filter.tb == -1) {
618 if (do_ipv6 != AF_INET6)
619 iproute_flush_cache();
620 if (do_ipv6 == AF_INET)
621 return 0;
622 }
623
624 filter.flushb = flushb;
625 filter.flushp = 0;
626 filter.flushe = sizeof(flushb);
627 filter.rth = &rth;
628
629 for (;;) {
630 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
631 perror("Cannot send dump request");
632 return -1;
633 }
634 filter.flushed = 0;
635 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000636 bb_error_msg("flush terminated");
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000637 return -1;
638 }
639 if (filter.flushed == 0) {
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000640 fflush(stdout);
641 return 0;
642 }
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000643 if (flush_update() < 0)
644 exit(1);
645 }
646 }
647
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000648 if (filter.tb != -1) {
649 if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000650 bb_perror_msg_and_die("cannot send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000651 }
652 } else {
653 if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000654 bb_perror_msg_and_die("cannot send dump request");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000655 }
656 }
657
658 if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000659 bb_error_msg_and_die("dump terminated");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000660 }
661
662 exit(0);
663}
664
665
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000666static int iproute_get(int argc, char **argv)
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000667{
668 struct rtnl_handle rth;
669 struct {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000670 struct nlmsghdr n;
671 struct rtmsg r;
672 char buf[1024];
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000673 } req;
674 char *idev = NULL;
675 char *odev = NULL;
676 int connected = 0;
677 int from_ok = 0;
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000678 static const char * const options[] =
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000679 { "from", "iif", "oif", "dev", "notify", "connected", "to", 0 };
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000680
681 memset(&req, 0, sizeof(req));
682
683 iproute_reset_filter();
684
685 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
686 req.n.nlmsg_flags = NLM_F_REQUEST;
687 req.n.nlmsg_type = RTM_GETROUTE;
688 req.r.rtm_family = preferred_family;
689 req.r.rtm_table = 0;
690 req.r.rtm_protocol = 0;
691 req.r.rtm_scope = 0;
692 req.r.rtm_type = 0;
693 req.r.rtm_src_len = 0;
694 req.r.rtm_dst_len = 0;
695 req.r.rtm_tos = 0;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000696
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000697 while (argc > 0) {
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000698 switch (index_in_str_array(options, *argv)) {
Glenn L McGrath18eae002002-12-02 00:54:10 +0000699 case 0: /* from */
700 {
701 inet_prefix addr;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000702 NEXT_ARG();
Glenn L McGrath18eae002002-12-02 00:54:10 +0000703 from_ok = 1;
704 get_prefix(&addr, *argv, req.r.rtm_family);
705 if (req.r.rtm_family == AF_UNSPEC) {
706 req.r.rtm_family = addr.family;
707 }
708 if (addr.bytelen) {
709 addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
710 }
711 req.r.rtm_src_len = addr.bitlen;
712 break;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000713 }
Glenn L McGrath18eae002002-12-02 00:54:10 +0000714 case 1: /* iif */
715 NEXT_ARG();
716 idev = *argv;
717 break;
718 case 2: /* oif */
719 case 3: /* dev */
720 NEXT_ARG();
721 odev = *argv;
722 break;
723 case 4: /* notify */
724 req.r.rtm_flags |= RTM_F_NOTIFY;
725 break;
726 case 5: /* connected */
727 connected = 1;
728 break;
729 case 6: /* to */
730 NEXT_ARG();
731 default:
732 {
733 inet_prefix addr;
734 get_prefix(&addr, *argv, req.r.rtm_family);
735 if (req.r.rtm_family == AF_UNSPEC) {
736 req.r.rtm_family = addr.family;
737 }
738 if (addr.bytelen) {
739 addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
740 }
741 req.r.rtm_dst_len = addr.bitlen;
742 }
743 argc--; argv++;
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000744 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000745 }
746
747 if (req.r.rtm_dst_len == 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000748 bb_error_msg_and_die("need at least destination address");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000749 }
750
751 if (rtnl_open(&rth, 0) < 0)
752 exit(1);
753
754 ll_init_map(&rth);
755
756 if (idev || odev) {
757 int idx;
758
759 if (idev) {
760 if ((idx = ll_name_to_index(idev)) == 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000761 bb_error_msg("cannot find device \"%s\"", idev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000762 return -1;
763 }
764 addattr32(&req.n, sizeof(req), RTA_IIF, idx);
765 }
766 if (odev) {
767 if ((idx = ll_name_to_index(odev)) == 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000768 bb_error_msg("cannot find device \"%s\"", odev);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000769 return -1;
770 }
771 addattr32(&req.n, sizeof(req), RTA_OIF, idx);
772 }
773 }
774
Glenn L McGrath16528552002-11-28 11:17:19 +0000775 if (req.r.rtm_family == AF_UNSPEC) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000776 req.r.rtm_family = AF_INET;
Glenn L McGrath16528552002-11-28 11:17:19 +0000777 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000778
Glenn L McGrath16528552002-11-28 11:17:19 +0000779 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000780 exit(2);
Glenn L McGrath16528552002-11-28 11:17:19 +0000781 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000782
783 if (connected && !from_ok) {
784 struct rtmsg *r = NLMSG_DATA(&req.n);
785 int len = req.n.nlmsg_len;
786 struct rtattr * tb[RTA_MAX+1];
787
788 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000789 bb_error_msg_and_die("an error :-)");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000790 }
791
792 if (req.n.nlmsg_type != RTM_NEWROUTE) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000793 bb_error_msg("not a route?");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000794 return -1;
795 }
796 len -= NLMSG_LENGTH(sizeof(*r));
797 if (len < 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000798 bb_error_msg("wrong len %d", len);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000799 return -1;
800 }
801
802 memset(tb, 0, sizeof(tb));
803 parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
804
805 if (tb[RTA_PREFSRC]) {
806 tb[RTA_PREFSRC]->rta_type = RTA_SRC;
807 r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
808 } else if (!tb[RTA_SRC]) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000809 bb_error_msg("failed to connect the route");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000810 return -1;
811 }
Glenn L McGrath16528552002-11-28 11:17:19 +0000812 if (!odev && tb[RTA_OIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000813 tb[RTA_OIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000814 }
815 if (tb[RTA_GATEWAY]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000816 tb[RTA_GATEWAY]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000817 }
818 if (!idev && tb[RTA_IIF]) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000819 tb[RTA_IIF]->rta_type = 0;
Glenn L McGrath16528552002-11-28 11:17:19 +0000820 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000821 req.n.nlmsg_flags = NLM_F_REQUEST;
822 req.n.nlmsg_type = RTM_GETROUTE;
823
Glenn L McGrath16528552002-11-28 11:17:19 +0000824 if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000825 exit(2);
Glenn L McGrath16528552002-11-28 11:17:19 +0000826 }
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000827 }
828
829 if (print_route(NULL, &req.n, (void*)stdout) < 0) {
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000830 bb_error_msg_and_die("an error :-)");
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000831 }
832
833 exit(0);
834}
835
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000836int do_iproute(int argc, char **argv)
837{
Tim Rikerc1ef7bd2006-01-25 00:08:53 +0000838 static const char * const ip_route_commands[] =
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000839 { "add", "append", "change", "chg", "delete", "get",
"Vladimir N. Oleynik"2f0a5f92005-12-06 12:00:39 +0000840 "list", "show", "prepend", "replace", "test", "flush", 0 };
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000841 int command_num = 6;
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000842 unsigned int flags = 0;
843 int cmd = RTM_NEWROUTE;
844
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000845 /* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
846 /* It probably means that it is using "first match" rule */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000847 if (*argv) {
Denis Vlasenko5af906e2006-11-05 18:05:09 +0000848 command_num = index_in_substr_array(ip_route_commands, *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000849 }
Denis Vlasenkoc16bd212006-09-27 19:51:06 +0000850 switch (command_num) {
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000851 case 0: /* add*/
852 flags = NLM_F_CREATE|NLM_F_EXCL;
853 break;
854 case 1: /* append */
855 flags = NLM_F_CREATE|NLM_F_APPEND;
856 break;
857 case 2: /* change */
858 case 3: /* chg */
859 flags = NLM_F_REPLACE;
860 break;
861 case 4: /* delete */
Eric Andersen1a773a62003-12-19 10:47:40 +0000862 case 5: /* del */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000863 cmd = RTM_DELROUTE;
864 break;
Eric Andersen1a773a62003-12-19 10:47:40 +0000865 case 6: /* get */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000866 return iproute_get(argc-1, argv+1);
Eric Andersen1a773a62003-12-19 10:47:40 +0000867 case 7: /* list */
868 case 8: /* show */
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000869 return iproute_list_or_flush(argc-1, argv+1, 0);
Eric Andersen1a773a62003-12-19 10:47:40 +0000870 case 9: /* prepend */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000871 flags = NLM_F_CREATE;
Eric Andersen1a773a62003-12-19 10:47:40 +0000872 case 10: /* replace */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000873 flags = NLM_F_CREATE|NLM_F_REPLACE;
Eric Andersen1a773a62003-12-19 10:47:40 +0000874 case 11: /* test */
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000875 flags = NLM_F_EXCL;
Eric Andersen1a773a62003-12-19 10:47:40 +0000876 case 12: /* flush */
Glenn L McGrath4a4c6772003-02-15 11:50:33 +0000877 return iproute_list_or_flush(argc-1, argv+1, 1);
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000878 default:
Denis Vlasenkoc6f188d2006-10-26 00:37:00 +0000879 bb_error_msg_and_die("unknown command %s", *argv);
Glenn L McGrath16528552002-11-28 11:17:19 +0000880 }
Glenn L McGrathc82f2322002-12-02 00:35:23 +0000881
882 return iproute_modify(cmd, flags, argc-1, argv+1);
Glenn L McGrath9a2d2722002-11-10 01:33:55 +0000883}