2006-01-12 17:38:46 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 07:03:55 +05:30
|
|
|
/*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
2010-10-29 15:16:52 +05:30
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
*
|
2010-10-29 15:16:52 +05:30
|
|
|
* Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
|
2002-11-10 07:03:55 +05:30
|
|
|
* Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized
|
|
|
|
*/
|
2010-10-29 15:16:52 +05:30
|
|
|
#include "ip_common.h" /* #include "libbb.h" is inside */
|
2016-04-21 19:56:30 +05:30
|
|
|
#include "common_bufsiz.h"
|
2002-11-10 07:03:55 +05:30
|
|
|
#include "rt_names.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
2017-10-19 03:35:11 +05:30
|
|
|
#include <linux/version.h>
|
|
|
|
/* RTA_TABLE is not a define, can't test with ifdef. */
|
|
|
|
/* As a proxy, test which kernels toolchain expects: */
|
|
|
|
#define HAVE_RTA_TABLE (LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,19))
|
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
#ifndef RTAX_RTTVAR
|
|
|
|
#define RTAX_RTTVAR RTAX_HOPS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-02-05 19:10:23 +05:30
|
|
|
struct filter_t {
|
2002-11-10 07:03:55 +05:30
|
|
|
int tb;
|
2008-05-31 13:03:18 +05:30
|
|
|
smallint flushed;
|
2003-02-15 17:20:33 +05:30
|
|
|
char *flushb;
|
2002-11-10 07:03:55 +05:30
|
|
|
int flushp;
|
|
|
|
int flushe;
|
|
|
|
struct rtnl_handle *rth;
|
2010-01-26 15:33:16 +05:30
|
|
|
//int protocol, protocolmask; - write-only fields?!
|
2017-06-13 23:29:59 +05:30
|
|
|
int scope, scopemask;
|
2010-01-26 15:33:16 +05:30
|
|
|
//int type; - read-only
|
|
|
|
//int typemask; - unused
|
|
|
|
//int tos, tosmask; - unused
|
2011-02-09 09:09:09 +05:30
|
|
|
int iif;
|
|
|
|
int oif;
|
2010-01-26 15:33:16 +05:30
|
|
|
//int realm, realmmask; - unused
|
|
|
|
//inet_prefix rprefsrc; - read-only
|
2002-11-10 07:03:55 +05:30
|
|
|
inet_prefix rvia;
|
|
|
|
inet_prefix rdst;
|
|
|
|
inet_prefix mdst;
|
|
|
|
inet_prefix rsrc;
|
|
|
|
inet_prefix msrc;
|
2010-02-05 19:10:23 +05:30
|
|
|
} FIX_ALIASING;
|
|
|
|
typedef struct filter_t filter_t;
|
2007-04-07 06:44:45 +05:30
|
|
|
|
2016-04-21 19:56:30 +05:30
|
|
|
#define G_filter (*(filter_t*)bb_common_bufsiz1)
|
2016-04-21 22:08:51 +05:30
|
|
|
#define INIT_G() do { setup_common_bufsiz(); } while (0)
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2003-02-15 17:20:33 +05:30
|
|
|
static int flush_update(void)
|
|
|
|
{
|
2010-01-26 15:33:16 +05:30
|
|
|
if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
|
2010-03-23 20:55:17 +05:30
|
|
|
bb_perror_msg("can't send flush request");
|
2003-02-15 17:20:33 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2010-01-26 15:33:16 +05:30
|
|
|
G_filter.flushp = 0;
|
2003-02-15 17:20:33 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-06-05 15:36:05 +05:30
|
|
|
static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
|
2008-07-05 14:48:54 +05:30
|
|
|
struct nlmsghdr *n, void *arg UNUSED_PARAM)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct rtmsg *r = NLMSG_DATA(n);
|
|
|
|
int len = n->nlmsg_len;
|
2011-02-23 05:50:44 +05:30
|
|
|
struct rtattr *tb[RTA_MAX+1];
|
2003-04-26 07:52:19 +05:30
|
|
|
inet_prefix dst;
|
|
|
|
inet_prefix src;
|
2002-11-10 07:03:55 +05:30
|
|
|
int host_len = -1;
|
2016-12-13 18:28:31 +05:30
|
|
|
uint32_t tid;
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
if (n->nlmsg_type != RTM_NEWROUTE && n->nlmsg_type != RTM_DELROUTE) {
|
|
|
|
fprintf(stderr, "Not a route: %08x %08x %08x\n",
|
|
|
|
n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
|
|
|
|
return 0;
|
|
|
|
}
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
|
2003-02-15 17:20:33 +05:30
|
|
|
return 0;
|
2002-11-10 07:03:55 +05:30
|
|
|
len -= NLMSG_LENGTH(sizeof(*r));
|
2007-04-12 17:04:39 +05:30
|
|
|
if (len < 0)
|
|
|
|
bb_error_msg_and_die("wrong nlmsg len %d", len);
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2018-02-08 13:12:37 +05:30
|
|
|
//memset(tb, 0, sizeof(tb)); - parse_rtattr does this
|
2016-12-13 18:28:31 +05:30
|
|
|
parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
|
|
|
|
|
2017-10-19 03:35:11 +05:30
|
|
|
#if HAVE_RTA_TABLE
|
2016-12-13 18:28:31 +05:30
|
|
|
if (tb[RTA_TABLE])
|
|
|
|
tid = *(uint32_t *)RTA_DATA(tb[RTA_TABLE]);
|
|
|
|
else
|
2017-10-19 03:35:11 +05:30
|
|
|
#endif
|
2016-12-13 18:28:31 +05:30
|
|
|
tid = r->rtm_table;
|
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
if (r->rtm_family == AF_INET6)
|
|
|
|
host_len = 128;
|
|
|
|
else if (r->rtm_family == AF_INET)
|
|
|
|
host_len = 32;
|
|
|
|
|
|
|
|
if (r->rtm_family == AF_INET6) {
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.tb) {
|
|
|
|
if (G_filter.tb < 0) {
|
2007-11-26 23:59:52 +05:30
|
|
|
if (!(r->rtm_flags & RTM_F_CLONED)) {
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
} else {
|
2007-11-26 23:59:52 +05:30
|
|
|
if (r->rtm_flags & RTM_F_CLONED) {
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.tb == RT_TABLE_LOCAL) {
|
2002-11-28 16:47:19 +05:30
|
|
|
if (r->rtm_type != RTN_LOCAL) {
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2010-01-26 15:33:16 +05:30
|
|
|
} else if (G_filter.tb == RT_TABLE_MAIN) {
|
2002-11-28 16:47:19 +05:30
|
|
|
if (r->rtm_type == RTN_LOCAL) {
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2016-12-13 18:28:31 +05:30
|
|
|
if (G_filter.tb > 0 && G_filter.tb != tid) {
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2017-06-13 23:29:59 +05:30
|
|
|
if ((G_filter.scope ^ r->rtm_scope) & G_filter.scopemask)
|
|
|
|
return 0;
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.rdst.family
|
|
|
|
&& (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len)
|
|
|
|
) {
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.mdst.family
|
|
|
|
&& (r->rtm_family != G_filter.mdst.family
|
|
|
|
|| (G_filter.mdst.bitlen >= 0 && G_filter.mdst.bitlen < r->rtm_dst_len)
|
|
|
|
)
|
|
|
|
) {
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.rsrc.family
|
|
|
|
&& (r->rtm_family != G_filter.rsrc.family || G_filter.rsrc.bitlen > r->rtm_src_len)
|
|
|
|
) {
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.msrc.family
|
|
|
|
&& (r->rtm_family != G_filter.msrc.family
|
|
|
|
|| (G_filter.msrc.bitlen >= 0 && G_filter.msrc.bitlen < r->rtm_src_len)
|
|
|
|
)
|
|
|
|
) {
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2011-02-23 05:50:44 +05:30
|
|
|
memset(&src, 0, sizeof(src));
|
|
|
|
memset(&dst, 0, sizeof(dst));
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2011-02-23 05:50:44 +05:30
|
|
|
if (tb[RTA_SRC]) {
|
|
|
|
src.bitlen = r->rtm_src_len;
|
|
|
|
src.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
|
|
|
|
memcpy(src.data, RTA_DATA(tb[RTA_SRC]), src.bytelen);
|
|
|
|
}
|
|
|
|
if (tb[RTA_DST]) {
|
|
|
|
dst.bitlen = r->rtm_dst_len;
|
|
|
|
dst.bytelen = (r->rtm_family == AF_INET6 ? 16 : 4);
|
|
|
|
memcpy(dst.data, RTA_DATA(tb[RTA_DST]), dst.bytelen);
|
|
|
|
}
|
|
|
|
|
2010-01-28 06:54:24 +05:30
|
|
|
if (G_filter.rdst.family
|
|
|
|
&& inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen)
|
|
|
|
) {
|
2003-04-26 07:52:19 +05:30
|
|
|
return 0;
|
2010-01-28 06:54:24 +05:30
|
|
|
}
|
|
|
|
if (G_filter.mdst.family
|
|
|
|
&& G_filter.mdst.bitlen >= 0
|
|
|
|
&& inet_addr_match(&dst, &G_filter.mdst, r->rtm_dst_len)
|
|
|
|
) {
|
2003-04-26 07:52:19 +05:30
|
|
|
return 0;
|
2010-01-28 06:54:24 +05:30
|
|
|
}
|
|
|
|
if (G_filter.rsrc.family
|
|
|
|
&& inet_addr_match(&src, &G_filter.rsrc, G_filter.rsrc.bitlen)
|
|
|
|
) {
|
2003-04-26 07:52:19 +05:30
|
|
|
return 0;
|
2010-01-28 06:54:24 +05:30
|
|
|
}
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.msrc.family && G_filter.msrc.bitlen >= 0
|
|
|
|
&& inet_addr_match(&src, &G_filter.msrc, r->rtm_src_len)
|
|
|
|
) {
|
2003-04-26 07:52:19 +05:30
|
|
|
return 0;
|
2010-01-26 15:33:16 +05:30
|
|
|
}
|
2011-02-09 09:09:09 +05:30
|
|
|
if (G_filter.oif != 0) {
|
|
|
|
if (!tb[RTA_OIF])
|
|
|
|
return 0;
|
|
|
|
if (G_filter.oif != *(int*)RTA_DATA(tb[RTA_OIF]))
|
|
|
|
return 0;
|
2010-01-26 15:33:16 +05:30
|
|
|
}
|
2003-02-15 17:20:33 +05:30
|
|
|
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.flushb) {
|
2011-02-10 14:20:07 +05:30
|
|
|
struct nlmsghdr *fn;
|
|
|
|
|
2011-02-09 09:09:09 +05:30
|
|
|
/* We are creating route flush commands */
|
|
|
|
|
|
|
|
if (r->rtm_family == AF_INET6
|
|
|
|
&& r->rtm_dst_len == 0
|
|
|
|
&& r->rtm_type == RTN_UNREACHABLE
|
|
|
|
&& tb[RTA_PRIORITY]
|
|
|
|
&& *(int*)RTA_DATA(tb[RTA_PRIORITY]) == -1
|
|
|
|
) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-26 15:33:16 +05:30
|
|
|
if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
|
2003-02-15 17:20:33 +05:30
|
|
|
if (flush_update())
|
2015-10-14 16:23:47 +05:30
|
|
|
xfunc_die();
|
2003-02-15 17:20:33 +05:30
|
|
|
}
|
2011-02-10 14:20:07 +05:30
|
|
|
fn = (void*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
|
2003-02-15 17:20:33 +05:30
|
|
|
memcpy(fn, n, n->nlmsg_len);
|
|
|
|
fn->nlmsg_type = RTM_DELROUTE;
|
|
|
|
fn->nlmsg_flags = NLM_F_REQUEST;
|
2010-01-26 15:33:16 +05:30
|
|
|
fn->nlmsg_seq = ++G_filter.rth->seq;
|
|
|
|
G_filter.flushp = (((char*)fn) + n->nlmsg_len) - G_filter.flushb;
|
|
|
|
G_filter.flushed = 1;
|
2003-02-15 17:20:33 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-02-09 09:09:09 +05:30
|
|
|
/* We are printing routes */
|
|
|
|
|
2002-11-28 16:47:19 +05:30
|
|
|
if (n->nlmsg_type == RTM_DELROUTE) {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf("Deleted ");
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2010-01-26 15:33:16 +05:30
|
|
|
if (r->rtm_type != RTN_UNICAST /* && !G_filter.type - always 0 */) {
|
2015-10-08 16:32:28 +05:30
|
|
|
printf("%s ", rtnl_rtntype_n2a(r->rtm_type));
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
if (tb[RTA_DST]) {
|
|
|
|
if (r->rtm_dst_len != host_len) {
|
2015-10-14 17:26:42 +05:30
|
|
|
printf("%s/%u ",
|
|
|
|
rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_DST])),
|
|
|
|
r->rtm_dst_len
|
|
|
|
);
|
2002-11-10 07:03:55 +05:30
|
|
|
} else {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf("%s ", format_host(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_DST]),
|
2015-10-14 17:26:42 +05:30
|
|
|
RTA_DATA(tb[RTA_DST]))
|
|
|
|
);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
} else if (r->rtm_dst_len) {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf("0/%d ", r->rtm_dst_len);
|
2002-11-10 07:03:55 +05:30
|
|
|
} else {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf("default ");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
if (tb[RTA_SRC]) {
|
|
|
|
if (r->rtm_src_len != host_len) {
|
2015-10-14 17:26:42 +05:30
|
|
|
printf("from %s/%u ",
|
|
|
|
rt_addr_n2a(r->rtm_family, RTA_DATA(tb[RTA_SRC])),
|
|
|
|
r->rtm_src_len
|
|
|
|
);
|
2002-11-10 07:03:55 +05:30
|
|
|
} else {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf("from %s ", format_host(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_SRC]),
|
2015-10-14 17:26:42 +05:30
|
|
|
RTA_DATA(tb[RTA_SRC]))
|
|
|
|
);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
} else if (r->rtm_src_len) {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf("from 0/%u ", r->rtm_src_len);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2010-01-26 15:33:16 +05:30
|
|
|
if (tb[RTA_GATEWAY] && G_filter.rvia.bitlen != host_len) {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf("via %s ", format_host(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_GATEWAY]),
|
2015-10-14 17:26:42 +05:30
|
|
|
RTA_DATA(tb[RTA_GATEWAY]))
|
|
|
|
);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2011-02-09 09:09:09 +05:30
|
|
|
if (tb[RTA_OIF]) {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2016-12-13 18:28:31 +05:30
|
|
|
#if ENABLE_FEATURE_IP_RULE
|
|
|
|
if (tid && tid != RT_TABLE_MAIN && !G_filter.tb)
|
|
|
|
printf("table %s ", rtnl_rttable_n2a(tid));
|
|
|
|
#endif
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2017-06-13 23:29:59 +05:30
|
|
|
/* Todo: parse & show "proto kernel" here */
|
|
|
|
if (!(r->rtm_flags & RTM_F_CLONED)) {
|
|
|
|
if ((r->rtm_scope != RT_SCOPE_UNIVERSE) && G_filter.scopemask != -1)
|
|
|
|
printf("scope %s ", rtnl_rtscope_n2a(r->rtm_scope));
|
|
|
|
}
|
2011-02-09 09:09:09 +05:30
|
|
|
|
2010-01-26 15:33:16 +05:30
|
|
|
if (tb[RTA_PREFSRC] && /*G_filter.rprefsrc.bitlen - always 0*/ 0 != host_len) {
|
2002-11-10 07:03:55 +05:30
|
|
|
/* Do not use format_host(). It is our local addr
|
|
|
|
and symbolic name will not be useful.
|
|
|
|
*/
|
2008-06-29 11:52:40 +05:30
|
|
|
printf(" src %s ", rt_addr_n2a(r->rtm_family,
|
2015-10-14 17:26:42 +05:30
|
|
|
RTA_DATA(tb[RTA_PREFSRC])));
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2002-11-28 16:47:19 +05:30
|
|
|
if (tb[RTA_PRIORITY]) {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2014-07-28 11:35:41 +05:30
|
|
|
if (r->rtm_flags & RTNH_F_DEAD) {
|
|
|
|
printf("dead ");
|
|
|
|
}
|
|
|
|
if (r->rtm_flags & RTNH_F_ONLINK) {
|
|
|
|
printf("onlink ");
|
|
|
|
}
|
|
|
|
if (r->rtm_flags & RTNH_F_PERVASIVE) {
|
|
|
|
printf("pervasive ");
|
|
|
|
}
|
|
|
|
if (r->rtm_flags & RTM_F_NOTIFY) {
|
|
|
|
printf("notify ");
|
|
|
|
}
|
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
if (r->rtm_family == AF_INET6) {
|
|
|
|
struct rta_cacheinfo *ci = NULL;
|
2002-11-28 16:47:19 +05:30
|
|
|
if (tb[RTA_CACHEINFO]) {
|
2002-11-10 07:03:55 +05:30
|
|
|
ci = RTA_DATA(tb[RTA_CACHEINFO]);
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
|
2002-11-28 16:47:19 +05:30
|
|
|
if (r->rtm_flags & RTM_F_CLONED) {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf("%c cache ", _SL_);
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
|
|
|
if (ci->rta_expires) {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf(" expires %dsec", ci->rta_expires / get_hz());
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
|
|
|
if (ci->rta_error != 0) {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf(" error %d", ci->rta_error);
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
} else if (ci) {
|
|
|
|
if (ci->rta_error != 0)
|
2008-06-29 11:52:40 +05:30
|
|
|
printf(" error %d", ci->rta_error);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
2011-02-09 09:09:09 +05:30
|
|
|
if (tb[RTA_IIF] && G_filter.iif == 0) {
|
2008-06-29 11:52:40 +05:30
|
|
|
printf(" iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2008-06-29 11:52:40 +05:30
|
|
|
bb_putchar('\n');
|
2002-11-10 07:03:55 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-04-07 20:30:53 +05:30
|
|
|
static int str_is_lock(const char *str)
|
|
|
|
{
|
|
|
|
return strcmp(str, "lock") == 0;
|
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2007-11-19 04:26:25 +05:30
|
|
|
static int iproute_modify(int cmd, unsigned flags, char **argv)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2017-04-06 21:24:38 +05:30
|
|
|
/* If you add stuff here, update iproute_full_usage */
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char keywords[] ALIGN1 =
|
2017-04-07 20:30:53 +05:30
|
|
|
"src\0""via\0"
|
|
|
|
"mtu\0""advmss\0"
|
|
|
|
"scope\0""protocol\0"IF_FEATURE_IP_RULE("table\0")
|
2014-07-28 11:35:41 +05:30
|
|
|
"dev\0""oif\0""to\0""metric\0""onlink\0";
|
2017-04-07 21:03:26 +05:30
|
|
|
#define keyword_via (keywords + sizeof("src"))
|
|
|
|
#define keyword_mtu (keyword_via + sizeof("via"))
|
|
|
|
#define keyword_advmss (keyword_mtu + sizeof("mtu"))
|
|
|
|
#define keyword_scope (keyword_advmss + sizeof("advmss"))
|
|
|
|
#define keyword_proto (keyword_scope + sizeof("scope"))
|
|
|
|
#define keyword_table (keyword_proto + sizeof("protocol"))
|
2007-07-24 21:24:42 +05:30
|
|
|
enum {
|
|
|
|
ARG_src,
|
|
|
|
ARG_via,
|
2017-04-06 21:24:38 +05:30
|
|
|
ARG_mtu,
|
2017-04-07 20:30:53 +05:30
|
|
|
ARG_advmss,
|
2015-12-30 22:02:51 +05:30
|
|
|
ARG_scope,
|
2007-07-24 21:24:42 +05:30
|
|
|
ARG_protocol,
|
2009-04-21 16:39:40 +05:30
|
|
|
IF_FEATURE_IP_RULE(ARG_table,)
|
2007-07-24 21:24:42 +05:30
|
|
|
ARG_dev,
|
|
|
|
ARG_oif,
|
2008-10-07 22:30:58 +05:30
|
|
|
ARG_to,
|
|
|
|
ARG_metric,
|
2014-07-28 11:35:41 +05:30
|
|
|
ARG_onlink,
|
2007-07-24 21:24:42 +05:30
|
|
|
};
|
|
|
|
enum {
|
|
|
|
gw_ok = 1 << 0,
|
|
|
|
dst_ok = 1 << 1,
|
|
|
|
proto_ok = 1 << 2,
|
|
|
|
type_ok = 1 << 3
|
|
|
|
};
|
2002-11-10 07:03:55 +05:30
|
|
|
struct rtnl_handle rth;
|
|
|
|
struct {
|
2010-10-29 15:16:52 +05:30
|
|
|
struct nlmsghdr n;
|
|
|
|
struct rtmsg r;
|
|
|
|
char buf[1024];
|
2002-11-10 07:03:55 +05:30
|
|
|
} req;
|
2007-04-07 06:44:45 +05:30
|
|
|
char mxbuf[256];
|
2002-11-10 07:03:55 +05:30
|
|
|
struct rtattr * mxrta = (void*)mxbuf;
|
|
|
|
unsigned mxlock = 0;
|
2007-04-07 06:44:45 +05:30
|
|
|
char *d = NULL;
|
2007-04-12 17:06:56 +05:30
|
|
|
smalluint ok = 0;
|
2015-12-30 22:02:51 +05:30
|
|
|
smalluint scope_ok = 0;
|
2007-04-12 17:06:56 +05:30
|
|
|
int arg;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
2008-06-29 11:52:40 +05:30
|
|
|
req.n.nlmsg_flags = NLM_F_REQUEST | flags;
|
2002-11-10 07:03:55 +05:30
|
|
|
req.n.nlmsg_type = cmd;
|
|
|
|
req.r.rtm_family = preferred_family;
|
2015-12-30 22:02:51 +05:30
|
|
|
if (RT_TABLE_MAIN != 0) /* if it is zero, memset already did it */
|
2008-06-29 11:52:40 +05:30
|
|
|
req.r.rtm_table = RT_TABLE_MAIN;
|
2015-12-30 22:02:51 +05:30
|
|
|
if (RT_SCOPE_NOWHERE != 0)
|
2008-06-29 11:52:40 +05:30
|
|
|
req.r.rtm_scope = RT_SCOPE_NOWHERE;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
if (cmd != RTM_DELROUTE) {
|
2016-08-14 05:00:34 +05:30
|
|
|
req.r.rtm_scope = RT_SCOPE_UNIVERSE;
|
2015-12-30 22:02:51 +05:30
|
|
|
if (RTPROT_BOOT != 0)
|
|
|
|
req.r.rtm_protocol = RTPROT_BOOT;
|
|
|
|
if (RTN_UNICAST != 0)
|
|
|
|
req.r.rtm_type = RTN_UNICAST;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
mxrta->rta_type = RTA_METRICS;
|
|
|
|
mxrta->rta_len = RTA_LENGTH(0);
|
|
|
|
|
2007-11-19 04:26:25 +05:30
|
|
|
while (*argv) {
|
2007-07-24 21:24:42 +05:30
|
|
|
arg = index_in_substrings(keywords, *argv);
|
2007-04-12 17:06:56 +05:30
|
|
|
if (arg == ARG_src) {
|
2002-11-10 07:03:55 +05:30
|
|
|
inet_prefix addr;
|
|
|
|
NEXT_ARG();
|
|
|
|
get_addr(&addr, *argv, req.r.rtm_family);
|
2007-04-12 17:06:56 +05:30
|
|
|
if (req.r.rtm_family == AF_UNSPEC)
|
2002-11-10 07:03:55 +05:30
|
|
|
req.r.rtm_family = addr.family;
|
|
|
|
addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
|
2007-04-12 17:06:56 +05:30
|
|
|
} else if (arg == ARG_via) {
|
2002-11-10 07:03:55 +05:30
|
|
|
inet_prefix addr;
|
2007-04-12 17:06:56 +05:30
|
|
|
ok |= gw_ok;
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
get_addr(&addr, *argv, req.r.rtm_family);
|
2002-11-28 16:47:19 +05:30
|
|
|
if (req.r.rtm_family == AF_UNSPEC) {
|
2002-11-10 07:03:55 +05:30
|
|
|
req.r.rtm_family = addr.family;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
|
2007-04-12 17:06:56 +05:30
|
|
|
} else if (arg == ARG_mtu) {
|
2002-11-10 07:03:55 +05:30
|
|
|
unsigned mtu;
|
|
|
|
NEXT_ARG();
|
2017-04-07 20:30:53 +05:30
|
|
|
if (str_is_lock(*argv)) {
|
2007-11-26 23:59:52 +05:30
|
|
|
mxlock |= (1 << RTAX_MTU);
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
}
|
2017-04-07 21:03:26 +05:30
|
|
|
mtu = get_unsigned(*argv, keyword_mtu);
|
2002-11-10 07:03:55 +05:30
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
|
2017-04-07 20:30:53 +05:30
|
|
|
} else if (arg == ARG_advmss) {
|
|
|
|
unsigned mss;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (str_is_lock(*argv)) {
|
|
|
|
mxlock |= (1 << RTAX_ADVMSS);
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
2017-04-07 21:03:26 +05:30
|
|
|
mss = get_unsigned(*argv, keyword_advmss);
|
2017-04-07 20:30:53 +05:30
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_ADVMSS, mss);
|
2015-12-30 22:02:51 +05:30
|
|
|
} else if (arg == ARG_scope) {
|
|
|
|
uint32_t scope;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (rtnl_rtscope_a2n(&scope, *argv))
|
2017-04-07 21:03:26 +05:30
|
|
|
invarg_1_to_2(*argv, keyword_scope);
|
2015-12-30 22:02:51 +05:30
|
|
|
req.r.rtm_scope = scope;
|
|
|
|
scope_ok = 1;
|
2007-04-12 17:06:56 +05:30
|
|
|
} else if (arg == ARG_protocol) {
|
2006-01-30 23:30:02 +05:30
|
|
|
uint32_t prot;
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
if (rtnl_rtprot_a2n(&prot, *argv))
|
2017-04-07 21:03:26 +05:30
|
|
|
invarg_1_to_2(*argv, keyword_proto);
|
2002-11-10 07:03:55 +05:30
|
|
|
req.r.rtm_protocol = prot;
|
2007-04-12 17:06:56 +05:30
|
|
|
ok |= proto_ok;
|
2007-04-16 20:26:01 +05:30
|
|
|
#if ENABLE_FEATURE_IP_RULE
|
2007-04-12 17:06:56 +05:30
|
|
|
} else if (arg == ARG_table) {
|
2006-12-15 06:29:35 +05:30
|
|
|
uint32_t tid;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (rtnl_rttable_a2n(&tid, *argv))
|
2017-04-07 21:03:26 +05:30
|
|
|
invarg_1_to_2(*argv, keyword_table);
|
2017-10-19 03:35:11 +05:30
|
|
|
#if HAVE_RTA_TABLE
|
|
|
|
if (tid > 255) {
|
2016-12-13 18:28:31 +05:30
|
|
|
req.r.rtm_table = RT_TABLE_UNSPEC;
|
|
|
|
addattr32(&req.n, sizeof(req), RTA_TABLE, tid);
|
2017-10-19 03:35:11 +05:30
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
req.r.rtm_table = tid;
|
2007-04-16 20:26:01 +05:30
|
|
|
#endif
|
2007-04-12 17:06:56 +05:30
|
|
|
} else if (arg == ARG_dev || arg == ARG_oif) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
d = *argv;
|
2008-10-07 22:30:58 +05:30
|
|
|
} else if (arg == ARG_metric) {
|
2017-04-06 21:24:38 +05:30
|
|
|
//TODO: "metric", "priority" and "preference" are synonyms
|
2008-10-07 22:30:58 +05:30
|
|
|
uint32_t metric;
|
|
|
|
NEXT_ARG();
|
2009-03-05 14:51:57 +05:30
|
|
|
metric = get_u32(*argv, "metric");
|
2008-10-07 22:30:58 +05:30
|
|
|
addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
|
2014-07-28 11:35:41 +05:30
|
|
|
} else if (arg == ARG_onlink) {
|
|
|
|
req.r.rtm_flags |= RTNH_F_ONLINK;
|
2002-11-10 07:03:55 +05:30
|
|
|
} else {
|
|
|
|
int type;
|
|
|
|
inet_prefix dst;
|
|
|
|
|
2007-04-12 17:06:56 +05:30
|
|
|
if (arg == ARG_to) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
}
|
2007-04-07 06:44:45 +05:30
|
|
|
if ((**argv < '0' || **argv > '9')
|
2011-02-23 05:50:44 +05:30
|
|
|
&& rtnl_rtntype_a2n(&type, *argv) == 0
|
|
|
|
) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
req.r.rtm_type = type;
|
2007-04-12 17:06:56 +05:30
|
|
|
ok |= type_ok;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-04-12 17:06:56 +05:30
|
|
|
if (ok & dst_ok) {
|
2002-11-10 07:03:55 +05:30
|
|
|
duparg2("to", *argv);
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
get_prefix(&dst, *argv, req.r.rtm_family);
|
2002-11-28 16:47:19 +05:30
|
|
|
if (req.r.rtm_family == AF_UNSPEC) {
|
2002-11-10 07:03:55 +05:30
|
|
|
req.r.rtm_family = dst.family;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
req.r.rtm_dst_len = dst.bitlen;
|
2007-04-12 17:06:56 +05:30
|
|
|
ok |= dst_ok;
|
2002-11-28 16:47:19 +05:30
|
|
|
if (dst.bytelen) {
|
2002-11-10 07:03:55 +05:30
|
|
|
addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2017-04-06 21:24:38 +05:30
|
|
|
/* Other keywords recognized by iproute2-3.19.0: */
|
|
|
|
#if 0
|
|
|
|
} else if (strcmp(*argv, "from") == 0) {
|
|
|
|
inet_prefix addr;
|
|
|
|
NEXT_ARG();
|
|
|
|
get_prefix(&addr, *argv, req.r.rtm_family);
|
|
|
|
if (req.r.rtm_family == AF_UNSPEC)
|
|
|
|
req.r.rtm_family = addr.family;
|
|
|
|
if (addr.bytelen)
|
|
|
|
addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
|
|
|
|
req.r.rtm_src_len = addr.bitlen;
|
|
|
|
} else if (strcmp(*argv, "tos") == 0 ||
|
|
|
|
matches(*argv, "dsfield") == 0) {
|
|
|
|
__u32 tos;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (rtnl_dsfield_a2n(&tos, *argv))
|
|
|
|
invarg("\"tos\" value is invalid\n", *argv);
|
|
|
|
req.r.rtm_tos = tos;
|
|
|
|
} else if (strcmp(*argv, "hoplimit") == 0) {
|
|
|
|
unsigned hoplimit;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "lock") == 0) {
|
|
|
|
mxlock |= (1<<RTAX_HOPLIMIT);
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (get_unsigned(&hoplimit, *argv, 0))
|
|
|
|
invarg("\"hoplimit\" value is invalid\n", *argv);
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_HOPLIMIT, hoplimit);
|
|
|
|
} else if (matches(*argv, "reordering") == 0) {
|
|
|
|
unsigned reord;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "lock") == 0) {
|
|
|
|
mxlock |= (1<<RTAX_REORDERING);
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (get_unsigned(&reord, *argv, 0))
|
|
|
|
invarg("\"reordering\" value is invalid\n", *argv);
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_REORDERING, reord);
|
|
|
|
} else if (strcmp(*argv, "rtt") == 0) {
|
|
|
|
unsigned rtt;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "lock") == 0) {
|
|
|
|
mxlock |= (1<<RTAX_RTT);
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (get_time_rtt(&rtt, *argv, &raw))
|
|
|
|
invarg("\"rtt\" value is invalid\n", *argv);
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTT,
|
|
|
|
(raw) ? rtt : rtt * 8);
|
|
|
|
} else if (strcmp(*argv, "rto_min") == 0) {
|
|
|
|
unsigned rto_min;
|
|
|
|
NEXT_ARG();
|
|
|
|
mxlock |= (1<<RTAX_RTO_MIN);
|
|
|
|
if (get_time_rtt(&rto_min, *argv, &raw))
|
|
|
|
invarg("\"rto_min\" value is invalid\n",
|
|
|
|
*argv);
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTO_MIN,
|
|
|
|
rto_min);
|
|
|
|
} else if (matches(*argv, "window") == 0) {
|
|
|
|
unsigned win;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "lock") == 0) {
|
|
|
|
mxlock |= (1<<RTAX_WINDOW);
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (get_unsigned(&win, *argv, 0))
|
|
|
|
invarg("\"window\" value is invalid\n", *argv);
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_WINDOW, win);
|
|
|
|
} else if (matches(*argv, "cwnd") == 0) {
|
|
|
|
unsigned win;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "lock") == 0) {
|
|
|
|
mxlock |= (1<<RTAX_CWND);
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (get_unsigned(&win, *argv, 0))
|
|
|
|
invarg("\"cwnd\" value is invalid\n", *argv);
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_CWND, win);
|
|
|
|
} else if (matches(*argv, "initcwnd") == 0) {
|
|
|
|
unsigned win;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "lock") == 0) {
|
|
|
|
mxlock |= (1<<RTAX_INITCWND);
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (get_unsigned(&win, *argv, 0))
|
|
|
|
invarg("\"initcwnd\" value is invalid\n", *argv);
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITCWND, win);
|
|
|
|
} else if (matches(*argv, "initrwnd") == 0) {
|
|
|
|
unsigned win;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "lock") == 0) {
|
|
|
|
mxlock |= (1<<RTAX_INITRWND);
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (get_unsigned(&win, *argv, 0))
|
|
|
|
invarg("\"initrwnd\" value is invalid\n", *argv);
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_INITRWND, win);
|
|
|
|
} else if (matches(*argv, "features") == 0) {
|
|
|
|
unsigned int features = 0;
|
|
|
|
|
|
|
|
while (argc > 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
|
|
|
|
if (strcmp(*argv, "ecn") == 0)
|
|
|
|
features |= RTAX_FEATURE_ECN;
|
|
|
|
else
|
|
|
|
invarg("\"features\" value not valid\n", *argv);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_FEATURES, features);
|
|
|
|
} else if (matches(*argv, "quickack") == 0) {
|
|
|
|
unsigned quickack;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (get_unsigned(&quickack, *argv, 0))
|
|
|
|
invarg("\"quickack\" value is invalid\n", *argv);
|
|
|
|
if (quickack != 1 && quickack != 0)
|
|
|
|
invarg("\"quickack\" value should be 0 or 1\n", *argv);
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_QUICKACK, quickack);
|
|
|
|
} else if (matches(*argv, "rttvar") == 0) {
|
|
|
|
unsigned win;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "lock") == 0) {
|
|
|
|
mxlock |= (1<<RTAX_RTTVAR);
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (get_time_rtt(&win, *argv, &raw))
|
|
|
|
invarg("\"rttvar\" value is invalid\n", *argv);
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_RTTVAR,
|
|
|
|
(raw) ? win : win * 4);
|
|
|
|
} else if (matches(*argv, "ssthresh") == 0) {
|
|
|
|
unsigned win;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "lock") == 0) {
|
|
|
|
mxlock |= (1<<RTAX_SSTHRESH);
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (get_unsigned(&win, *argv, 0))
|
|
|
|
invarg("\"ssthresh\" value is invalid\n", *argv);
|
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_SSTHRESH, win);
|
|
|
|
} else if (matches(*argv, "realms") == 0) {
|
|
|
|
__u32 realm;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (get_rt_realms(&realm, *argv))
|
|
|
|
invarg("\"realm\" value is invalid\n", *argv);
|
|
|
|
addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
|
|
|
|
} else if (strcmp(*argv, "nexthop") == 0) {
|
|
|
|
nhs_ok = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif
|
2007-11-19 04:26:25 +05:30
|
|
|
argv++;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-04-12 17:04:39 +05:30
|
|
|
xrtnl_open(&rth);
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2002-11-15 14:42:47 +05:30
|
|
|
if (d) {
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
ll_init_map(&rth);
|
|
|
|
|
|
|
|
if (d) {
|
2007-04-12 17:04:39 +05:30
|
|
|
idx = xll_name_to_index(d);
|
2002-11-15 14:42:47 +05:30
|
|
|
addattr32(&req.n, sizeof(req), RTA_OIF, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
if (mxrta->rta_len > RTA_LENGTH(0)) {
|
2002-11-28 16:47:19 +05:30
|
|
|
if (mxlock) {
|
2002-11-10 07:03:55 +05:30
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
|
|
|
|
}
|
2007-04-12 17:06:56 +05:30
|
|
|
|
2015-12-30 22:02:51 +05:30
|
|
|
if (!scope_ok) {
|
|
|
|
if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
|
|
|
|
req.r.rtm_scope = RT_SCOPE_HOST;
|
|
|
|
else
|
|
|
|
if (req.r.rtm_type == RTN_BROADCAST
|
|
|
|
|| req.r.rtm_type == RTN_MULTICAST
|
|
|
|
|| req.r.rtm_type == RTN_ANYCAST
|
|
|
|
) {
|
2007-04-11 21:53:57 +05:30
|
|
|
req.r.rtm_scope = RT_SCOPE_LINK;
|
2015-12-30 22:02:51 +05:30
|
|
|
}
|
|
|
|
else if (req.r.rtm_type == RTN_UNICAST || req.r.rtm_type == RTN_UNSPEC) {
|
|
|
|
if (cmd == RTM_DELROUTE)
|
|
|
|
req.r.rtm_scope = RT_SCOPE_NOWHERE;
|
|
|
|
else if (!(ok & gw_ok))
|
|
|
|
req.r.rtm_scope = RT_SCOPE_LINK;
|
|
|
|
}
|
2007-04-11 21:53:57 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2002-11-28 16:47:19 +05:30
|
|
|
if (req.r.rtm_family == AF_UNSPEC) {
|
2002-11-10 07:03:55 +05:30
|
|
|
req.r.rtm_family = AF_INET;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2002-11-28 16:47:19 +05:30
|
|
|
if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
|
2007-04-07 06:44:45 +05:30
|
|
|
return 2;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rtnl_rtcache_request(struct rtnl_handle *rth, int family)
|
|
|
|
{
|
|
|
|
struct {
|
|
|
|
struct nlmsghdr nlh;
|
|
|
|
struct rtmsg rtm;
|
|
|
|
} req;
|
|
|
|
struct sockaddr_nl nladdr;
|
|
|
|
|
|
|
|
memset(&nladdr, 0, sizeof(nladdr));
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
nladdr.nl_family = AF_NETLINK;
|
|
|
|
|
|
|
|
req.nlh.nlmsg_len = sizeof(req);
|
2008-06-29 11:52:40 +05:30
|
|
|
if (RTM_GETROUTE)
|
|
|
|
req.nlh.nlmsg_type = RTM_GETROUTE;
|
|
|
|
if (NLM_F_ROOT | NLM_F_REQUEST)
|
|
|
|
req.nlh.nlmsg_flags = NLM_F_ROOT | NLM_F_REQUEST;
|
|
|
|
/*req.nlh.nlmsg_pid = 0; - memset did it already */
|
2002-11-10 07:03:55 +05:30
|
|
|
req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
|
|
|
|
req.rtm.rtm_family = family;
|
2008-06-29 11:52:40 +05:30
|
|
|
if (RTM_F_CLONED)
|
|
|
|
req.rtm.rtm_flags = RTM_F_CLONED;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2007-04-12 17:04:39 +05:30
|
|
|
return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
static void iproute_flush_cache(void)
|
2003-02-15 17:20:33 +05:30
|
|
|
{
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
|
2007-04-12 04:50:53 +05:30
|
|
|
int flush_fd = open_or_warn(fn, O_WRONLY);
|
2007-04-07 06:44:45 +05:30
|
|
|
|
2003-02-15 17:20:33 +05:30
|
|
|
if (flush_fd < 0) {
|
2007-04-07 06:44:45 +05:30
|
|
|
return;
|
2003-02-15 17:20:33 +05:30
|
|
|
}
|
|
|
|
|
2007-01-30 05:13:18 +05:30
|
|
|
if (write(flush_fd, "-1", 2) < 2) {
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_perror_msg("can't flush routing cache");
|
2007-04-07 06:44:45 +05:30
|
|
|
return;
|
2003-02-15 17:20:33 +05:30
|
|
|
}
|
|
|
|
close(flush_fd);
|
|
|
|
}
|
|
|
|
|
2002-12-02 06:05:23 +05:30
|
|
|
static void iproute_reset_filter(void)
|
|
|
|
{
|
2010-01-26 15:33:16 +05:30
|
|
|
memset(&G_filter, 0, sizeof(G_filter));
|
|
|
|
G_filter.mdst.bitlen = -1;
|
|
|
|
G_filter.msrc.bitlen = -1;
|
2002-12-02 06:05:23 +05:30
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2007-11-19 04:26:25 +05:30
|
|
|
static int iproute_list_or_flush(char **argv, int flush)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
int do_ipv6 = preferred_family;
|
|
|
|
struct rtnl_handle rth;
|
|
|
|
char *id = NULL;
|
|
|
|
char *od = NULL;
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char keywords[] ALIGN1 =
|
2017-06-13 23:29:59 +05:30
|
|
|
/* If you add stuff here, update iproute_full_usage */
|
2007-11-26 23:59:52 +05:30
|
|
|
/* "ip route list/flush" parameters: */
|
|
|
|
"protocol\0" "dev\0" "oif\0" "iif\0"
|
|
|
|
"via\0" "table\0" "cache\0"
|
2017-06-13 23:29:59 +05:30
|
|
|
"from\0" "to\0" "scope\0"
|
2007-11-26 23:59:52 +05:30
|
|
|
/* and possible further keywords */
|
|
|
|
"all\0"
|
|
|
|
"root\0"
|
|
|
|
"match\0"
|
|
|
|
"exact\0"
|
|
|
|
"main\0"
|
|
|
|
;
|
2007-04-12 17:06:56 +05:30
|
|
|
enum {
|
2007-11-26 23:59:52 +05:30
|
|
|
KW_proto, KW_dev, KW_oif, KW_iif,
|
|
|
|
KW_via, KW_table, KW_cache,
|
2017-06-13 23:29:59 +05:30
|
|
|
KW_from, KW_to, KW_scope,
|
2007-11-26 23:59:52 +05:30
|
|
|
/* */
|
|
|
|
KW_all,
|
|
|
|
KW_root,
|
|
|
|
KW_match,
|
|
|
|
KW_exact,
|
|
|
|
KW_main,
|
2007-04-12 17:06:56 +05:30
|
|
|
};
|
|
|
|
int arg, parm;
|
2007-11-26 23:59:52 +05:30
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
iproute_reset_filter();
|
2010-01-26 15:33:16 +05:30
|
|
|
G_filter.tb = RT_TABLE_MAIN;
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2007-11-19 04:26:25 +05:30
|
|
|
if (flush && !*argv)
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
|
2003-02-15 17:20:33 +05:30
|
|
|
|
2007-11-19 04:26:25 +05:30
|
|
|
while (*argv) {
|
2007-07-24 21:24:42 +05:30
|
|
|
arg = index_in_substrings(keywords, *argv);
|
2007-11-26 23:59:52 +05:30
|
|
|
if (arg == KW_proto) {
|
2006-01-30 23:30:02 +05:30
|
|
|
uint32_t prot = 0;
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2010-01-26 15:33:16 +05:30
|
|
|
//G_filter.protocolmask = -1;
|
2002-11-10 07:03:55 +05:30
|
|
|
if (rtnl_rtprot_a2n(&prot, *argv)) {
|
2007-11-26 23:59:52 +05:30
|
|
|
if (index_in_strings(keywords, *argv) != KW_all)
|
2015-10-14 16:51:01 +05:30
|
|
|
invarg_1_to_2(*argv, "protocol");
|
2002-11-10 07:03:55 +05:30
|
|
|
prot = 0;
|
2010-01-26 15:33:16 +05:30
|
|
|
//G_filter.protocolmask = 0;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2010-01-26 15:33:16 +05:30
|
|
|
//G_filter.protocol = prot;
|
2007-11-26 23:59:52 +05:30
|
|
|
} else if (arg == KW_dev || arg == KW_oif) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
od = *argv;
|
2007-11-26 23:59:52 +05:30
|
|
|
} else if (arg == KW_iif) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
id = *argv;
|
2007-11-26 23:59:52 +05:30
|
|
|
} else if (arg == KW_via) {
|
2007-04-12 17:06:56 +05:30
|
|
|
NEXT_ARG();
|
2010-01-26 15:33:16 +05:30
|
|
|
get_prefix(&G_filter.rvia, *argv, do_ipv6);
|
2007-11-26 23:59:52 +05:30
|
|
|
} else if (arg == KW_table) { /* table all/cache/main */
|
2007-04-12 17:06:56 +05:30
|
|
|
NEXT_ARG();
|
2007-07-24 21:24:42 +05:30
|
|
|
parm = index_in_substrings(keywords, *argv);
|
2007-11-26 23:59:52 +05:30
|
|
|
if (parm == KW_cache)
|
2010-01-26 15:33:16 +05:30
|
|
|
G_filter.tb = -1;
|
2007-11-26 23:59:52 +05:30
|
|
|
else if (parm == KW_all)
|
2010-01-26 15:33:16 +05:30
|
|
|
G_filter.tb = 0;
|
2008-06-08 05:55:55 +05:30
|
|
|
else if (parm != KW_main) {
|
|
|
|
#if ENABLE_FEATURE_IP_RULE
|
|
|
|
uint32_t tid;
|
|
|
|
if (rtnl_rttable_a2n(&tid, *argv))
|
2015-10-14 16:51:01 +05:30
|
|
|
invarg_1_to_2(*argv, "table");
|
2010-01-26 15:33:16 +05:30
|
|
|
G_filter.tb = tid;
|
2008-06-25 15:23:17 +05:30
|
|
|
#else
|
2015-10-14 16:51:01 +05:30
|
|
|
invarg_1_to_2(*argv, "table");
|
2008-06-08 05:55:55 +05:30
|
|
|
#endif
|
|
|
|
}
|
2007-11-27 15:12:33 +05:30
|
|
|
} else if (arg == KW_cache) {
|
|
|
|
/* The command 'ip route flush cache' is used by OpenSWAN.
|
|
|
|
* Assuming it's a synonym for 'ip route flush table cache' */
|
2010-01-26 15:33:16 +05:30
|
|
|
G_filter.tb = -1;
|
2017-06-13 23:29:59 +05:30
|
|
|
} else if (arg == KW_scope) {
|
|
|
|
uint32_t scope;
|
|
|
|
NEXT_ARG();
|
|
|
|
G_filter.scopemask = -1;
|
|
|
|
if (rtnl_rtscope_a2n(&scope, *argv)) {
|
|
|
|
if (strcmp(*argv, "all") != 0)
|
|
|
|
invarg_1_to_2(*argv, "scope");
|
|
|
|
scope = RT_SCOPE_NOWHERE;
|
|
|
|
G_filter.scopemask = 0;
|
|
|
|
}
|
|
|
|
G_filter.scope = scope;
|
2007-11-26 23:59:52 +05:30
|
|
|
} else if (arg == KW_from) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2007-07-24 21:24:42 +05:30
|
|
|
parm = index_in_substrings(keywords, *argv);
|
2007-11-26 23:59:52 +05:30
|
|
|
if (parm == KW_root) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2010-01-26 15:33:16 +05:30
|
|
|
get_prefix(&G_filter.rsrc, *argv, do_ipv6);
|
2007-11-26 23:59:52 +05:30
|
|
|
} else if (parm == KW_match) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2010-01-26 15:33:16 +05:30
|
|
|
get_prefix(&G_filter.msrc, *argv, do_ipv6);
|
2002-11-10 07:03:55 +05:30
|
|
|
} else {
|
2007-11-26 23:59:52 +05:30
|
|
|
if (parm == KW_exact)
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2010-01-26 15:33:16 +05:30
|
|
|
get_prefix(&G_filter.msrc, *argv, do_ipv6);
|
|
|
|
G_filter.rsrc = G_filter.msrc;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2007-11-26 23:59:52 +05:30
|
|
|
} else { /* "to" is the default parameter */
|
|
|
|
if (arg == KW_to) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2007-07-24 21:24:42 +05:30
|
|
|
arg = index_in_substrings(keywords, *argv);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2007-11-26 23:59:52 +05:30
|
|
|
/* parm = arg; - would be more plausible, but we reuse 'arg' here */
|
|
|
|
if (arg == KW_root) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2010-01-26 15:33:16 +05:30
|
|
|
get_prefix(&G_filter.rdst, *argv, do_ipv6);
|
2007-11-26 23:59:52 +05:30
|
|
|
} else if (arg == KW_match) {
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2010-01-26 15:33:16 +05:30
|
|
|
get_prefix(&G_filter.mdst, *argv, do_ipv6);
|
2007-11-26 23:59:52 +05:30
|
|
|
} else { /* "to exact" is the default */
|
|
|
|
if (arg == KW_exact)
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2010-01-26 15:33:16 +05:30
|
|
|
get_prefix(&G_filter.mdst, *argv, do_ipv6);
|
|
|
|
G_filter.rdst = G_filter.mdst;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
2007-04-07 06:44:45 +05:30
|
|
|
argv++;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2010-01-26 15:33:16 +05:30
|
|
|
if (do_ipv6 == AF_UNSPEC && G_filter.tb) {
|
2002-11-10 07:03:55 +05:30
|
|
|
do_ipv6 = AF_INET;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2007-04-12 17:04:39 +05:30
|
|
|
xrtnl_open(&rth);
|
2002-11-10 07:03:55 +05:30
|
|
|
ll_init_map(&rth);
|
|
|
|
|
|
|
|
if (id || od) {
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (id) {
|
2007-04-12 17:04:39 +05:30
|
|
|
idx = xll_name_to_index(id);
|
2010-01-26 15:33:16 +05:30
|
|
|
G_filter.iif = idx;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
if (od) {
|
2007-04-12 17:04:39 +05:30
|
|
|
idx = xll_name_to_index(od);
|
2010-01-26 15:33:16 +05:30
|
|
|
G_filter.oif = idx;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-15 17:20:33 +05:30
|
|
|
if (flush) {
|
|
|
|
char flushb[4096-512];
|
|
|
|
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.tb == -1) { /* "flush table cache" */
|
2003-02-15 17:20:33 +05:30
|
|
|
if (do_ipv6 != AF_INET6)
|
|
|
|
iproute_flush_cache();
|
|
|
|
if (do_ipv6 == AF_INET)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-26 15:33:16 +05:30
|
|
|
G_filter.flushb = flushb;
|
|
|
|
G_filter.flushp = 0;
|
|
|
|
G_filter.flushe = sizeof(flushb);
|
|
|
|
G_filter.rth = &rth;
|
2003-02-15 17:20:33 +05:30
|
|
|
|
|
|
|
for (;;) {
|
2007-04-12 17:04:39 +05:30
|
|
|
xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
|
2010-01-26 15:33:16 +05:30
|
|
|
G_filter.flushed = 0;
|
2008-06-29 11:52:40 +05:30
|
|
|
xrtnl_dump_filter(&rth, print_route, NULL);
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.flushed == 0)
|
2003-02-15 17:20:33 +05:30
|
|
|
return 0;
|
2007-04-07 06:44:45 +05:30
|
|
|
if (flush_update())
|
|
|
|
return 1;
|
2003-02-15 17:20:33 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-26 15:33:16 +05:30
|
|
|
if (G_filter.tb != -1) {
|
2007-04-12 17:04:39 +05:30
|
|
|
xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
|
2007-07-24 21:24:42 +05:30
|
|
|
} else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
|
2009-11-13 13:38:27 +05:30
|
|
|
bb_perror_msg_and_die("can't send dump request");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2008-06-29 11:52:40 +05:30
|
|
|
xrtnl_dump_filter(&rth, print_route, NULL);
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
return 0;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2007-11-19 04:26:25 +05:30
|
|
|
static int iproute_get(char **argv)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct rtnl_handle rth;
|
|
|
|
struct {
|
2007-07-24 21:24:42 +05:30
|
|
|
struct nlmsghdr n;
|
|
|
|
struct rtmsg r;
|
|
|
|
char buf[1024];
|
2002-11-10 07:03:55 +05:30
|
|
|
} req;
|
2007-07-24 21:24:42 +05:30
|
|
|
char *idev = NULL;
|
|
|
|
char *odev = NULL;
|
2007-04-12 17:06:56 +05:30
|
|
|
bool connected = 0;
|
|
|
|
bool from_ok = 0;
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char options[] ALIGN1 =
|
2007-07-24 21:24:42 +05:30
|
|
|
"from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
iproute_reset_filter();
|
|
|
|
|
|
|
|
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
2008-06-29 11:52:40 +05:30
|
|
|
if (NLM_F_REQUEST)
|
|
|
|
req.n.nlmsg_flags = NLM_F_REQUEST;
|
|
|
|
if (RTM_GETROUTE)
|
|
|
|
req.n.nlmsg_type = RTM_GETROUTE;
|
2002-11-10 07:03:55 +05:30
|
|
|
req.r.rtm_family = preferred_family;
|
2008-06-29 11:52:40 +05:30
|
|
|
/*req.r.rtm_table = 0; - memset did this already */
|
|
|
|
/*req.r.rtm_protocol = 0;*/
|
|
|
|
/*req.r.rtm_scope = 0;*/
|
|
|
|
/*req.r.rtm_type = 0;*/
|
|
|
|
/*req.r.rtm_src_len = 0;*/
|
|
|
|
/*req.r.rtm_dst_len = 0;*/
|
|
|
|
/*req.r.rtm_tos = 0;*/
|
2004-03-15 13:59:22 +05:30
|
|
|
|
2007-11-19 04:26:25 +05:30
|
|
|
while (*argv) {
|
2007-07-24 21:24:42 +05:30
|
|
|
switch (index_in_strings(options, *argv)) {
|
2002-12-02 06:24:10 +05:30
|
|
|
case 0: /* from */
|
|
|
|
{
|
|
|
|
inet_prefix addr;
|
|
|
|
NEXT_ARG();
|
|
|
|
from_ok = 1;
|
|
|
|
get_prefix(&addr, *argv, req.r.rtm_family);
|
|
|
|
if (req.r.rtm_family == AF_UNSPEC) {
|
|
|
|
req.r.rtm_family = addr.family;
|
|
|
|
}
|
|
|
|
if (addr.bytelen) {
|
|
|
|
addattr_l(&req.n, sizeof(req), RTA_SRC, &addr.data, addr.bytelen);
|
|
|
|
}
|
|
|
|
req.r.rtm_src_len = addr.bitlen;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: /* iif */
|
|
|
|
NEXT_ARG();
|
|
|
|
idev = *argv;
|
|
|
|
break;
|
|
|
|
case 2: /* oif */
|
|
|
|
case 3: /* dev */
|
|
|
|
NEXT_ARG();
|
|
|
|
odev = *argv;
|
|
|
|
break;
|
|
|
|
case 4: /* notify */
|
|
|
|
req.r.rtm_flags |= RTM_F_NOTIFY;
|
|
|
|
break;
|
|
|
|
case 5: /* connected */
|
|
|
|
connected = 1;
|
|
|
|
break;
|
|
|
|
case 6: /* to */
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
2002-12-02 06:24:10 +05:30
|
|
|
default:
|
|
|
|
{
|
|
|
|
inet_prefix addr;
|
|
|
|
get_prefix(&addr, *argv, req.r.rtm_family);
|
|
|
|
if (req.r.rtm_family == AF_UNSPEC) {
|
|
|
|
req.r.rtm_family = addr.family;
|
|
|
|
}
|
|
|
|
if (addr.bytelen) {
|
|
|
|
addattr_l(&req.n, sizeof(req), RTA_DST, &addr.data, addr.bytelen);
|
|
|
|
}
|
|
|
|
req.r.rtm_dst_len = addr.bitlen;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
2010-11-03 18:38:00 +05:30
|
|
|
argv++;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (req.r.rtm_dst_len == 0) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg_and_die("need at least destination address");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-04-12 17:04:39 +05:30
|
|
|
xrtnl_open(&rth);
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
ll_init_map(&rth);
|
|
|
|
|
|
|
|
if (idev || odev) {
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (idev) {
|
2007-04-12 17:04:39 +05:30
|
|
|
idx = xll_name_to_index(idev);
|
2002-11-10 07:03:55 +05:30
|
|
|
addattr32(&req.n, sizeof(req), RTA_IIF, idx);
|
|
|
|
}
|
|
|
|
if (odev) {
|
2007-04-12 17:04:39 +05:30
|
|
|
idx = xll_name_to_index(odev);
|
2002-11-10 07:03:55 +05:30
|
|
|
addattr32(&req.n, sizeof(req), RTA_OIF, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-28 16:47:19 +05:30
|
|
|
if (req.r.rtm_family == AF_UNSPEC) {
|
2002-11-10 07:03:55 +05:30
|
|
|
req.r.rtm_family = AF_INET;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
|
2002-11-28 16:47:19 +05:30
|
|
|
if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
|
2007-04-07 06:44:45 +05:30
|
|
|
return 2;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
if (connected && !from_ok) {
|
|
|
|
struct rtmsg *r = NLMSG_DATA(&req.n);
|
|
|
|
int len = req.n.nlmsg_len;
|
|
|
|
struct rtattr * tb[RTA_MAX+1];
|
|
|
|
|
2008-06-29 11:52:40 +05:30
|
|
|
print_route(NULL, &req.n, NULL);
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
if (req.n.nlmsg_type != RTM_NEWROUTE) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_error_msg_and_die("not a route?");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
len -= NLMSG_LENGTH(sizeof(*r));
|
|
|
|
if (len < 0) {
|
2007-04-07 06:44:45 +05:30
|
|
|
bb_error_msg_and_die("wrong len %d", len);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2018-02-08 13:12:37 +05:30
|
|
|
//memset(tb, 0, sizeof(tb)); - parse_rtattr does this
|
2002-11-10 07:03:55 +05:30
|
|
|
parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
|
|
|
|
|
|
|
|
if (tb[RTA_PREFSRC]) {
|
|
|
|
tb[RTA_PREFSRC]->rta_type = RTA_SRC;
|
|
|
|
r->rtm_src_len = 8*RTA_PAYLOAD(tb[RTA_PREFSRC]);
|
|
|
|
} else if (!tb[RTA_SRC]) {
|
2010-03-23 20:55:17 +05:30
|
|
|
bb_error_msg_and_die("can't connect the route");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2002-11-28 16:47:19 +05:30
|
|
|
if (!odev && tb[RTA_OIF]) {
|
2002-11-10 07:03:55 +05:30
|
|
|
tb[RTA_OIF]->rta_type = 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
|
|
|
if (tb[RTA_GATEWAY]) {
|
2002-11-10 07:03:55 +05:30
|
|
|
tb[RTA_GATEWAY]->rta_type = 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
|
|
|
if (!idev && tb[RTA_IIF]) {
|
2002-11-10 07:03:55 +05:30
|
|
|
tb[RTA_IIF]->rta_type = 0;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
req.n.nlmsg_flags = NLM_F_REQUEST;
|
|
|
|
req.n.nlmsg_type = RTM_GETROUTE;
|
|
|
|
|
2002-11-28 16:47:19 +05:30
|
|
|
if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
|
2007-04-07 06:44:45 +05:30
|
|
|
return 2;
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
2008-06-29 11:52:40 +05:30
|
|
|
print_route(NULL, &req.n, NULL);
|
2007-04-07 06:44:45 +05:30
|
|
|
return 0;
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
2007-04-07 06:44:45 +05:30
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2010-07-25 02:57:38 +05:30
|
|
|
int FAST_FUNC do_iproute(char **argv)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
2007-08-13 02:28:27 +05:30
|
|
|
static const char ip_route_commands[] ALIGN1 =
|
2017-04-07 21:03:26 +05:30
|
|
|
"a\0""add\0""append\0""change\0""chg\0"
|
|
|
|
"delete\0""get\0""list\0""show\0"
|
|
|
|
"prepend\0""replace\0""test\0""flush\0"
|
|
|
|
;
|
|
|
|
enum {
|
|
|
|
CMD_a = 0, CMD_add, CMD_append, CMD_change, CMD_chg,
|
|
|
|
CMD_delete, CMD_get, CMD_list, CMD_show,
|
|
|
|
CMD_prepend, CMD_replace, CMD_test, CMD_flush,
|
|
|
|
};
|
2008-01-04 20:58:28 +05:30
|
|
|
int command_num;
|
2007-07-24 21:24:42 +05:30
|
|
|
unsigned flags = 0;
|
2002-12-02 06:05:23 +05:30
|
|
|
int cmd = RTM_NEWROUTE;
|
|
|
|
|
2016-04-21 22:08:51 +05:30
|
|
|
INIT_G();
|
|
|
|
|
2008-01-04 20:58:28 +05:30
|
|
|
if (!*argv)
|
|
|
|
return iproute_list_or_flush(argv, 0);
|
|
|
|
|
2006-11-05 23:35:09 +05:30
|
|
|
/* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
|
|
|
|
/* It probably means that it is using "first match" rule */
|
2008-01-04 20:58:28 +05:30
|
|
|
command_num = index_in_substrings(ip_route_commands, *argv);
|
|
|
|
|
2006-09-28 01:21:06 +05:30
|
|
|
switch (command_num) {
|
2017-04-07 21:03:26 +05:30
|
|
|
case CMD_a:
|
|
|
|
case CMD_add:
|
2002-12-02 06:05:23 +05:30
|
|
|
flags = NLM_F_CREATE|NLM_F_EXCL;
|
|
|
|
break;
|
2017-04-07 21:03:26 +05:30
|
|
|
case CMD_append:
|
2002-12-02 06:05:23 +05:30
|
|
|
flags = NLM_F_CREATE|NLM_F_APPEND;
|
|
|
|
break;
|
2017-04-07 21:03:26 +05:30
|
|
|
case CMD_change:
|
|
|
|
case CMD_chg:
|
2002-12-02 06:05:23 +05:30
|
|
|
flags = NLM_F_REPLACE;
|
|
|
|
break;
|
2017-04-07 21:03:26 +05:30
|
|
|
case CMD_delete:
|
2002-12-02 06:05:23 +05:30
|
|
|
cmd = RTM_DELROUTE;
|
|
|
|
break;
|
2017-04-07 21:03:26 +05:30
|
|
|
case CMD_get:
|
|
|
|
return iproute_get(argv + 1);
|
|
|
|
case CMD_list:
|
|
|
|
case CMD_show:
|
|
|
|
return iproute_list_or_flush(argv + 1, 0);
|
|
|
|
case CMD_prepend:
|
2002-12-02 06:05:23 +05:30
|
|
|
flags = NLM_F_CREATE;
|
2007-09-30 21:34:21 +05:30
|
|
|
break;
|
2017-04-07 21:03:26 +05:30
|
|
|
case CMD_replace:
|
2002-12-02 06:05:23 +05:30
|
|
|
flags = NLM_F_CREATE|NLM_F_REPLACE;
|
2007-09-30 21:34:21 +05:30
|
|
|
break;
|
2017-04-07 21:03:26 +05:30
|
|
|
case CMD_test:
|
2002-12-02 06:05:23 +05:30
|
|
|
flags = NLM_F_EXCL;
|
2007-09-30 21:34:21 +05:30
|
|
|
break;
|
2017-04-07 21:03:26 +05:30
|
|
|
case CMD_flush:
|
|
|
|
return iproute_list_or_flush(argv + 1, 1);
|
2002-12-02 06:05:23 +05:30
|
|
|
default:
|
2015-10-14 16:51:01 +05:30
|
|
|
invarg_1_to_2(*argv, applet_name);
|
2002-12-02 06:05:23 +05:30
|
|
|
}
|
|
|
|
|
2017-04-07 21:03:26 +05:30
|
|
|
return iproute_modify(cmd, flags, argv + 1);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|