2006-01-12 17:38:46 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 07:03:55 +05:30
|
|
|
/*
|
|
|
|
* iproute.c "ip route".
|
|
|
|
*
|
2005-10-26 16:17:26 +05:30
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
2002-11-10 07:03:55 +05:30
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
*
|
|
|
|
* Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
|
|
|
|
* Kunihiro Ishiguro <kunihiro@zebra.org> 001102: rtnh_ifindex was not initialized
|
|
|
|
*/
|
|
|
|
|
2006-06-03 02:23:38 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
#include "rt_names.h"
|
|
|
|
#include "utils.h"
|
2006-04-03 02:44:19 +05:30
|
|
|
#include "ip_common.h"
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
#ifndef RTAX_RTTVAR
|
|
|
|
#define RTAX_RTTVAR RTAX_HOPS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
static struct
|
|
|
|
{
|
|
|
|
int tb;
|
2003-02-15 17:20:33 +05:30
|
|
|
int flushed;
|
|
|
|
char *flushb;
|
2002-11-10 07:03:55 +05:30
|
|
|
int flushp;
|
|
|
|
int flushe;
|
|
|
|
struct rtnl_handle *rth;
|
|
|
|
int protocol, protocolmask;
|
|
|
|
int scope, scopemask;
|
|
|
|
int type, typemask;
|
|
|
|
int tos, tosmask;
|
|
|
|
int iif, iifmask;
|
|
|
|
int oif, oifmask;
|
|
|
|
int realm, realmmask;
|
|
|
|
inet_prefix rprefsrc;
|
|
|
|
inet_prefix rvia;
|
|
|
|
inet_prefix rdst;
|
|
|
|
inet_prefix mdst;
|
|
|
|
inet_prefix rsrc;
|
|
|
|
inet_prefix msrc;
|
|
|
|
} filter;
|
|
|
|
|
2003-02-15 17:20:33 +05:30
|
|
|
static int flush_update(void)
|
|
|
|
{
|
|
|
|
if (rtnl_send(filter.rth, filter.flushb, filter.flushp) < 0) {
|
|
|
|
perror("Failed to send flush request\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
filter.flushp = 0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-01-01 00:37:23 +05:30
|
|
|
static unsigned get_hz(void)
|
2007-01-01 00:28:32 +05:30
|
|
|
{
|
2007-01-01 00:37:23 +05:30
|
|
|
static unsigned hz_internal;
|
2007-01-01 00:28:32 +05:30
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (hz_internal)
|
|
|
|
return hz_internal;
|
|
|
|
|
|
|
|
fp = fopen("/proc/net/psched", "r");
|
|
|
|
if (fp) {
|
|
|
|
unsigned nom, denom;
|
|
|
|
|
|
|
|
if (fscanf(fp, "%*08x%*08x%08x%08x", &nom, &denom) == 2)
|
|
|
|
if (nom == 1000000)
|
|
|
|
hz_internal = denom;
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
if (!hz_internal)
|
|
|
|
hz_internal = sysconf(_SC_CLK_TCK);
|
|
|
|
return hz_internal;
|
|
|
|
}
|
|
|
|
|
2006-01-23 04:25:11 +05:30
|
|
|
static int print_route(struct sockaddr_nl *who ATTRIBUTE_UNUSED,
|
2006-01-12 17:38:46 +05:30
|
|
|
struct nlmsghdr *n, void *arg)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
FILE *fp = (FILE*)arg;
|
|
|
|
struct rtmsg *r = NLMSG_DATA(n);
|
|
|
|
int len = n->nlmsg_len;
|
|
|
|
struct rtattr * tb[RTA_MAX+1];
|
|
|
|
char abuf[256];
|
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;
|
|
|
|
SPRINT_BUF(b1);
|
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;
|
|
|
|
}
|
2003-02-15 17:20:33 +05:30
|
|
|
if (filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
|
|
|
|
return 0;
|
2002-11-10 07:03:55 +05:30
|
|
|
len -= NLMSG_LENGTH(sizeof(*r));
|
|
|
|
if (len < 0) {
|
2003-03-19 14:43:01 +05:30
|
|
|
bb_error_msg("wrong nlmsg len %d", len);
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
if (filter.tb) {
|
|
|
|
if (filter.tb < 0) {
|
2002-11-28 16:47:19 +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 {
|
2002-11-28 16:47:19 +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
|
|
|
if (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
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
} else if (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 {
|
2002-11-28 16:47:19 +05:30
|
|
|
if (filter.tb > 0 && filter.tb != r->rtm_table) {
|
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
|
|
|
}
|
|
|
|
if (filter.rdst.family &&
|
2002-11-28 16:47:19 +05:30
|
|
|
(r->rtm_family != filter.rdst.family || 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
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
if (filter.mdst.family &&
|
|
|
|
(r->rtm_family != filter.mdst.family ||
|
2002-11-28 16:47:19 +05:30
|
|
|
(filter.mdst.bitlen >= 0 && 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
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
if (filter.rsrc.family &&
|
2002-11-28 16:47:19 +05:30
|
|
|
(r->rtm_family != filter.rsrc.family || 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
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
if (filter.msrc.family &&
|
|
|
|
(r->rtm_family != filter.msrc.family ||
|
2002-11-28 16:47:19 +05:30
|
|
|
(filter.msrc.bitlen >= 0 && 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
|
|
|
|
|
|
|
memset(tb, 0, sizeof(tb));
|
|
|
|
parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
|
|
|
|
|
2003-04-26 07:52:19 +05:30
|
|
|
if (filter.rdst.family && inet_addr_match(&dst, &filter.rdst, filter.rdst.bitlen))
|
|
|
|
return 0;
|
|
|
|
if (filter.mdst.family && filter.mdst.bitlen >= 0 &&
|
|
|
|
inet_addr_match(&dst, &filter.mdst, r->rtm_dst_len))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (filter.rsrc.family && inet_addr_match(&src, &filter.rsrc, filter.rsrc.bitlen))
|
|
|
|
return 0;
|
|
|
|
if (filter.msrc.family && filter.msrc.bitlen >= 0 &&
|
|
|
|
inet_addr_match(&src, &filter.msrc, r->rtm_src_len))
|
|
|
|
return 0;
|
|
|
|
|
2003-02-15 17:20:33 +05:30
|
|
|
if (filter.flushb &&
|
|
|
|
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;
|
|
|
|
|
|
|
|
if (filter.flushb) {
|
|
|
|
struct nlmsghdr *fn;
|
|
|
|
if (NLMSG_ALIGN(filter.flushp) + n->nlmsg_len > filter.flushe) {
|
|
|
|
if (flush_update())
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
fn = (struct nlmsghdr*)(filter.flushb + NLMSG_ALIGN(filter.flushp));
|
|
|
|
memcpy(fn, n, n->nlmsg_len);
|
|
|
|
fn->nlmsg_type = RTM_DELROUTE;
|
|
|
|
fn->nlmsg_flags = NLM_F_REQUEST;
|
|
|
|
fn->nlmsg_seq = ++filter.rth->seq;
|
|
|
|
filter.flushp = (((char*)fn) + n->nlmsg_len) - filter.flushb;
|
|
|
|
filter.flushed++;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-28 16:47:19 +05:30
|
|
|
if (n->nlmsg_type == RTM_DELROUTE) {
|
2002-11-10 07:03:55 +05:30
|
|
|
fprintf(fp, "Deleted ");
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
|
|
|
if (r->rtm_type != RTN_UNICAST && !filter.type) {
|
2002-11-10 07:03:55 +05:30
|
|
|
fprintf(fp, "%s ", rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)));
|
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) {
|
|
|
|
fprintf(fp, "%s/%u ", rt_addr_n2a(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_DST]),
|
|
|
|
RTA_DATA(tb[RTA_DST]),
|
|
|
|
abuf, sizeof(abuf)),
|
|
|
|
r->rtm_dst_len
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
fprintf(fp, "%s ", format_host(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_DST]),
|
|
|
|
RTA_DATA(tb[RTA_DST]),
|
|
|
|
abuf, sizeof(abuf))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (r->rtm_dst_len) {
|
|
|
|
fprintf(fp, "0/%d ", r->rtm_dst_len);
|
|
|
|
} else {
|
|
|
|
fprintf(fp, "default ");
|
|
|
|
}
|
|
|
|
if (tb[RTA_SRC]) {
|
|
|
|
if (r->rtm_src_len != host_len) {
|
|
|
|
fprintf(fp, "from %s/%u ", rt_addr_n2a(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_SRC]),
|
|
|
|
RTA_DATA(tb[RTA_SRC]),
|
|
|
|
abuf, sizeof(abuf)),
|
|
|
|
r->rtm_src_len
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
fprintf(fp, "from %s ", format_host(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_SRC]),
|
|
|
|
RTA_DATA(tb[RTA_SRC]),
|
|
|
|
abuf, sizeof(abuf))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (r->rtm_src_len) {
|
|
|
|
fprintf(fp, "from 0/%u ", r->rtm_src_len);
|
|
|
|
}
|
|
|
|
if (tb[RTA_GATEWAY] && filter.rvia.bitlen != host_len) {
|
2004-03-15 13:59:22 +05:30
|
|
|
fprintf(fp, "via %s ",
|
2002-11-10 07:03:55 +05:30
|
|
|
format_host(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_GATEWAY]),
|
|
|
|
RTA_DATA(tb[RTA_GATEWAY]),
|
|
|
|
abuf, sizeof(abuf)));
|
|
|
|
}
|
2002-11-28 16:47:19 +05:30
|
|
|
if (tb[RTA_OIF] && filter.oifmask != -1) {
|
2002-11-10 07:03:55 +05:30
|
|
|
fprintf(fp, "dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
if (tb[RTA_PREFSRC] && filter.rprefsrc.bitlen != host_len) {
|
|
|
|
/* Do not use format_host(). It is our local addr
|
|
|
|
and symbolic name will not be useful.
|
|
|
|
*/
|
2004-03-15 13:59:22 +05:30
|
|
|
fprintf(fp, " src %s ",
|
2002-11-10 07:03:55 +05:30
|
|
|
rt_addr_n2a(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_PREFSRC]),
|
|
|
|
RTA_DATA(tb[RTA_PREFSRC]),
|
|
|
|
abuf, sizeof(abuf)));
|
|
|
|
}
|
2002-11-28 16:47:19 +05:30
|
|
|
if (tb[RTA_PRIORITY]) {
|
2007-01-01 00:27:37 +05:30
|
|
|
fprintf(fp, " metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
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) {
|
2002-11-10 07:03:55 +05:30
|
|
|
fprintf(fp, "%s cache ", _SL_);
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
|
|
|
if (ci->rta_expires) {
|
2007-01-01 00:37:23 +05:30
|
|
|
fprintf(fp, " expires %dsec", ci->rta_expires / get_hz());
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
|
|
|
if (ci->rta_error != 0) {
|
2002-11-10 07:03:55 +05:30
|
|
|
fprintf(fp, " 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)
|
|
|
|
fprintf(fp, " error %d", ci->rta_error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (tb[RTA_IIF] && filter.iifmask != -1) {
|
|
|
|
fprintf(fp, " iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
|
|
|
|
}
|
|
|
|
fprintf(fp, "\n");
|
|
|
|
fflush(fp);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-02 06:05:23 +05:30
|
|
|
static int iproute_modify(int cmd, unsigned flags, int argc, char **argv)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct rtnl_handle rth;
|
|
|
|
struct {
|
2006-01-25 05:38:53 +05:30
|
|
|
struct nlmsghdr n;
|
|
|
|
struct rtmsg r;
|
|
|
|
char buf[1024];
|
2002-11-10 07:03:55 +05:30
|
|
|
} req;
|
|
|
|
char mxbuf[256];
|
|
|
|
struct rtattr * mxrta = (void*)mxbuf;
|
|
|
|
unsigned mxlock = 0;
|
|
|
|
char *d = NULL;
|
|
|
|
int gw_ok = 0;
|
|
|
|
int dst_ok = 0;
|
|
|
|
int proto_ok = 0;
|
|
|
|
int type_ok = 0;
|
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
|
|
|
req.n.nlmsg_flags = NLM_F_REQUEST|flags;
|
|
|
|
req.n.nlmsg_type = cmd;
|
|
|
|
req.r.rtm_family = preferred_family;
|
|
|
|
req.r.rtm_table = RT_TABLE_MAIN;
|
|
|
|
req.r.rtm_scope = RT_SCOPE_NOWHERE;
|
|
|
|
|
|
|
|
if (cmd != RTM_DELROUTE) {
|
|
|
|
req.r.rtm_protocol = RTPROT_BOOT;
|
|
|
|
req.r.rtm_scope = RT_SCOPE_UNIVERSE;
|
|
|
|
req.r.rtm_type = RTN_UNICAST;
|
|
|
|
}
|
|
|
|
|
|
|
|
mxrta->rta_type = RTA_METRICS;
|
|
|
|
mxrta->rta_len = RTA_LENGTH(0);
|
|
|
|
|
|
|
|
while (argc > 0) {
|
|
|
|
if (strcmp(*argv, "src") == 0) {
|
|
|
|
inet_prefix addr;
|
|
|
|
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_PREFSRC, &addr.data, addr.bytelen);
|
|
|
|
} else if (strcmp(*argv, "via") == 0) {
|
|
|
|
inet_prefix addr;
|
|
|
|
gw_ok = 1;
|
|
|
|
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);
|
|
|
|
} else if (strcmp(*argv, "mtu") == 0) {
|
|
|
|
unsigned mtu;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (strcmp(*argv, "lock") == 0) {
|
|
|
|
mxlock |= (1<<RTAX_MTU);
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
2002-11-28 16:47:19 +05:30
|
|
|
if (get_unsigned(&mtu, *argv, 0)) {
|
2006-06-08 01:47:41 +05:30
|
|
|
invarg(*argv, "mtu");
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
|
|
|
|
} else if (matches(*argv, "protocol") == 0) {
|
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))
|
2006-06-08 01:47:41 +05:30
|
|
|
invarg(*argv, "protocol");
|
2002-11-10 07:03:55 +05:30
|
|
|
req.r.rtm_protocol = prot;
|
|
|
|
proto_ok =1;
|
2006-12-15 06:29:35 +05:30
|
|
|
#if ENABLE_FEATURE_IP_RULE
|
|
|
|
} else if (matches(*argv, "table") == 0) {
|
|
|
|
uint32_t tid;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (rtnl_rttable_a2n(&tid, *argv))
|
|
|
|
invarg(*argv, "table");
|
|
|
|
req.r.rtm_table = tid;
|
|
|
|
#endif
|
2002-11-10 07:03:55 +05:30
|
|
|
} else if (strcmp(*argv, "dev") == 0 ||
|
|
|
|
strcmp(*argv, "oif") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
d = *argv;
|
|
|
|
} else {
|
|
|
|
int type;
|
|
|
|
inet_prefix dst;
|
|
|
|
|
|
|
|
if (strcmp(*argv, "to") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if ((**argv < '0' || **argv > '9') &&
|
|
|
|
rtnl_rtntype_a2n(&type, *argv) == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
req.r.rtm_type = type;
|
|
|
|
type_ok = 1;
|
|
|
|
}
|
|
|
|
|
2002-11-28 16:47:19 +05:30
|
|
|
if (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;
|
|
|
|
dst_ok = 1;
|
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
|
|
|
}
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
|
2002-11-28 16:47:19 +05:30
|
|
|
if (rtnl_open(&rth, 0) < 0) {
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(1);
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
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-01-01 00:27:37 +05:30
|
|
|
idx = ll_name_to_index(d);
|
|
|
|
if (idx == 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("cannot find device \"%s\"", d);
|
2002-11-15 14:42:47 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
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) {
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(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);
|
|
|
|
req.nlh.nlmsg_type = RTM_GETROUTE;
|
|
|
|
req.nlh.nlmsg_flags = NLM_F_ROOT|NLM_F_REQUEST;
|
|
|
|
req.nlh.nlmsg_pid = 0;
|
|
|
|
req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
|
|
|
|
req.rtm.rtm_family = family;
|
|
|
|
req.rtm.rtm_flags |= RTM_F_CLONED;
|
|
|
|
|
|
|
|
return sendto(rth->fd, (void*)&req, sizeof(req), 0, (struct sockaddr*)&nladdr, sizeof(nladdr));
|
|
|
|
}
|
|
|
|
|
2003-02-15 17:20:33 +05:30
|
|
|
static int iproute_flush_cache(void)
|
|
|
|
{
|
2007-01-30 05:13:18 +05:30
|
|
|
static const char fn[] = "/proc/sys/net/ipv4/route/flush";
|
|
|
|
int flush_fd = open(fn, O_WRONLY);
|
2003-02-15 17:20:33 +05:30
|
|
|
if (flush_fd < 0) {
|
2007-01-30 05:13:18 +05:30
|
|
|
bb_perror_msg("cannot open '%s'", fn);
|
2003-02-15 17:20:33 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-01-30 05:13:18 +05:30
|
|
|
if (write(flush_fd, "-1", 2) < 2) {
|
|
|
|
bb_perror_msg("cannot flush routing cache");
|
2003-02-15 17:20:33 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
close(flush_fd);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-12-02 06:05:23 +05:30
|
|
|
static void iproute_reset_filter(void)
|
|
|
|
{
|
|
|
|
memset(&filter, 0, sizeof(filter));
|
|
|
|
filter.mdst.bitlen = -1;
|
|
|
|
filter.msrc.bitlen = -1;
|
|
|
|
}
|
|
|
|
|
2003-02-15 17:20:33 +05:30
|
|
|
static int iproute_list_or_flush(int argc, 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;
|
|
|
|
|
|
|
|
iproute_reset_filter();
|
|
|
|
filter.tb = RT_TABLE_MAIN;
|
|
|
|
|
2003-02-15 17:20:33 +05:30
|
|
|
if (flush && argc <= 0) {
|
2006-06-08 01:47:41 +05:30
|
|
|
bb_error_msg(bb_msg_requires_arg, "\"ip route flush\"");
|
2003-02-15 17:20:33 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
while (argc > 0) {
|
|
|
|
if (matches(*argv, "protocol") == 0) {
|
2006-01-30 23:30:02 +05:30
|
|
|
uint32_t prot = 0;
|
2002-11-10 07:03:55 +05:30
|
|
|
NEXT_ARG();
|
|
|
|
filter.protocolmask = -1;
|
|
|
|
if (rtnl_rtprot_a2n(&prot, *argv)) {
|
2002-11-28 16:47:19 +05:30
|
|
|
if (strcmp(*argv, "all") != 0) {
|
2006-06-08 01:47:41 +05:30
|
|
|
invarg(*argv, "protocol");
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
prot = 0;
|
|
|
|
filter.protocolmask = 0;
|
|
|
|
}
|
|
|
|
filter.protocol = prot;
|
|
|
|
} else if (strcmp(*argv, "dev") == 0 ||
|
|
|
|
strcmp(*argv, "oif") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
od = *argv;
|
|
|
|
} else if (strcmp(*argv, "iif") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
id = *argv;
|
|
|
|
} else if (matches(*argv, "from") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (matches(*argv, "root") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
get_prefix(&filter.rsrc, *argv, do_ipv6);
|
|
|
|
} else if (matches(*argv, "match") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
get_prefix(&filter.msrc, *argv, do_ipv6);
|
|
|
|
} else {
|
|
|
|
if (matches(*argv, "exact") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
get_prefix(&filter.msrc, *argv, do_ipv6);
|
|
|
|
filter.rsrc = filter.msrc;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (matches(*argv, "to") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
if (matches(*argv, "root") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
get_prefix(&filter.rdst, *argv, do_ipv6);
|
|
|
|
} else if (matches(*argv, "match") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
get_prefix(&filter.mdst, *argv, do_ipv6);
|
2005-07-21 00:31:05 +05:30
|
|
|
} else if (matches(*argv, "table") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
if (matches(*argv, "cache") == 0) {
|
|
|
|
filter.tb = -1;
|
2006-12-15 06:29:35 +05:30
|
|
|
#if 0 && ENABLE_FEATURE_IP_RULE
|
2007-01-11 22:50:00 +05:30
|
|
|
|
2006-12-15 06:29:35 +05:30
|
|
|
#else
|
2005-07-21 00:31:05 +05:30
|
|
|
} else if (matches(*argv, "main") != 0) {
|
2006-06-08 01:47:41 +05:30
|
|
|
invarg(*argv, "table");
|
2005-07-21 00:31:05 +05:30
|
|
|
}
|
2006-12-15 06:29:35 +05:30
|
|
|
#endif
|
2005-07-21 00:31:05 +05:30
|
|
|
} else if (matches(*argv, "cache") == 0) {
|
|
|
|
filter.tb = -1;
|
2002-11-10 07:03:55 +05:30
|
|
|
} else {
|
|
|
|
if (matches(*argv, "exact") == 0) {
|
|
|
|
NEXT_ARG();
|
|
|
|
}
|
|
|
|
get_prefix(&filter.mdst, *argv, do_ipv6);
|
|
|
|
filter.rdst = filter.mdst;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
argc--; argv++;
|
|
|
|
}
|
|
|
|
|
2002-11-28 16:47:19 +05:30
|
|
|
if (do_ipv6 == AF_UNSPEC && 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
|
|
|
|
2002-11-28 16:47:19 +05:30
|
|
|
if (rtnl_open(&rth, 0) < 0) {
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(1);
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
|
|
|
|
ll_init_map(&rth);
|
|
|
|
|
|
|
|
if (id || od) {
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (id) {
|
|
|
|
if ((idx = ll_name_to_index(id)) == 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("cannot find device \"%s\"", id);
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
filter.iif = idx;
|
|
|
|
filter.iifmask = -1;
|
|
|
|
}
|
|
|
|
if (od) {
|
|
|
|
if ((idx = ll_name_to_index(od)) == 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("cannot find device \"%s\"", od);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
filter.oif = idx;
|
|
|
|
filter.oifmask = -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-15 17:20:33 +05:30
|
|
|
if (flush) {
|
|
|
|
char flushb[4096-512];
|
|
|
|
|
|
|
|
if (filter.tb == -1) {
|
|
|
|
if (do_ipv6 != AF_INET6)
|
|
|
|
iproute_flush_cache();
|
|
|
|
if (do_ipv6 == AF_INET)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
filter.flushb = flushb;
|
|
|
|
filter.flushp = 0;
|
|
|
|
filter.flushe = sizeof(flushb);
|
|
|
|
filter.rth = &rth;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
|
|
|
|
perror("Cannot send dump request");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
filter.flushed = 0;
|
|
|
|
if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("flush terminated");
|
2003-02-15 17:20:33 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (filter.flushed == 0) {
|
|
|
|
fflush(stdout);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (flush_update() < 0)
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
if (filter.tb != -1) {
|
|
|
|
if (rtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE) < 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_perror_msg_and_die("cannot send dump request");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_perror_msg_and_die("cannot send dump request");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rtnl_dump_filter(&rth, print_route, stdout, NULL, NULL) < 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg_and_die("dump terminated");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-12-02 06:05:23 +05:30
|
|
|
static int iproute_get(int argc, char **argv)
|
2002-11-10 07:03:55 +05:30
|
|
|
{
|
|
|
|
struct rtnl_handle rth;
|
|
|
|
struct {
|
2006-01-25 05:38:53 +05:30
|
|
|
struct nlmsghdr n;
|
|
|
|
struct rtmsg r;
|
|
|
|
char buf[1024];
|
2002-11-10 07:03:55 +05:30
|
|
|
} req;
|
|
|
|
char *idev = NULL;
|
|
|
|
char *odev = NULL;
|
|
|
|
int connected = 0;
|
|
|
|
int from_ok = 0;
|
2006-01-25 05:38:53 +05:30
|
|
|
static const char * const options[] =
|
2005-12-06 17:30:39 +05:30
|
|
|
{ "from", "iif", "oif", "dev", "notify", "connected", "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));
|
|
|
|
req.n.nlmsg_flags = NLM_F_REQUEST;
|
|
|
|
req.n.nlmsg_type = RTM_GETROUTE;
|
|
|
|
req.r.rtm_family = preferred_family;
|
|
|
|
req.r.rtm_table = 0;
|
|
|
|
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
|
|
|
|
2002-11-10 07:03:55 +05:30
|
|
|
while (argc > 0) {
|
2006-11-05 23:35:09 +05:30
|
|
|
switch (index_in_str_array(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
|
|
|
}
|
2002-12-02 06:24:10 +05:30
|
|
|
argc--; 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
|
|
|
}
|
|
|
|
|
|
|
|
if (rtnl_open(&rth, 0) < 0)
|
|
|
|
exit(1);
|
|
|
|
|
|
|
|
ll_init_map(&rth);
|
|
|
|
|
|
|
|
if (idev || odev) {
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (idev) {
|
|
|
|
if ((idx = ll_name_to_index(idev)) == 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("cannot find device \"%s\"", idev);
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
addattr32(&req.n, sizeof(req), RTA_IIF, idx);
|
|
|
|
}
|
|
|
|
if (odev) {
|
|
|
|
if ((idx = ll_name_to_index(odev)) == 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("cannot find device \"%s\"", odev);
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
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) {
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(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];
|
|
|
|
|
|
|
|
if (print_route(NULL, &req.n, (void*)stdout) < 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg_and_die("an error :-)");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (req.n.nlmsg_type != RTM_NEWROUTE) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("not a route?");
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
len -= NLMSG_LENGTH(sizeof(*r));
|
|
|
|
if (len < 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("wrong len %d", len);
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
memset(tb, 0, sizeof(tb));
|
|
|
|
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]) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg("failed to connect the route");
|
2002-11-10 07:03:55 +05:30
|
|
|
return -1;
|
|
|
|
}
|
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) {
|
2002-11-10 07:03:55 +05:30
|
|
|
exit(2);
|
2002-11-28 16:47:19 +05:30
|
|
|
}
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if (print_route(NULL, &req.n, (void*)stdout) < 0) {
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg_and_die("an error :-)");
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int do_iproute(int argc, char **argv)
|
|
|
|
{
|
2007-01-27 18:43:45 +05:30
|
|
|
static const char * const ip_route_commands[] = {
|
|
|
|
/*0-3*/ "add", "append", "change", "chg",
|
|
|
|
/*4-7*/ "delete", "get", "list", "show",
|
|
|
|
/*8..*/ "prepend", "replace", "test", "flush", 0
|
|
|
|
};
|
2006-11-05 23:35:09 +05:30
|
|
|
int command_num = 6;
|
2002-12-02 06:05:23 +05:30
|
|
|
unsigned int flags = 0;
|
|
|
|
int cmd = RTM_NEWROUTE;
|
|
|
|
|
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 */
|
2002-12-02 06:05:23 +05:30
|
|
|
if (*argv) {
|
2006-11-05 23:35:09 +05:30
|
|
|
command_num = index_in_substr_array(ip_route_commands, *argv);
|
2002-12-02 06:05:23 +05:30
|
|
|
}
|
2006-09-28 01:21:06 +05:30
|
|
|
switch (command_num) {
|
2007-01-27 18:43:45 +05:30
|
|
|
case 0: /* add */
|
2002-12-02 06:05:23 +05:30
|
|
|
flags = NLM_F_CREATE|NLM_F_EXCL;
|
|
|
|
break;
|
|
|
|
case 1: /* append */
|
|
|
|
flags = NLM_F_CREATE|NLM_F_APPEND;
|
|
|
|
break;
|
|
|
|
case 2: /* change */
|
|
|
|
case 3: /* chg */
|
|
|
|
flags = NLM_F_REPLACE;
|
|
|
|
break;
|
|
|
|
case 4: /* delete */
|
|
|
|
cmd = RTM_DELROUTE;
|
|
|
|
break;
|
2007-01-27 18:43:45 +05:30
|
|
|
case 5: /* get */
|
2002-12-02 06:05:23 +05:30
|
|
|
return iproute_get(argc-1, argv+1);
|
2007-01-27 18:43:45 +05:30
|
|
|
case 6: /* list */
|
|
|
|
case 7: /* show */
|
2003-02-15 17:20:33 +05:30
|
|
|
return iproute_list_or_flush(argc-1, argv+1, 0);
|
2007-01-27 18:43:45 +05:30
|
|
|
case 8: /* prepend */
|
2002-12-02 06:05:23 +05:30
|
|
|
flags = NLM_F_CREATE;
|
2007-01-27 18:43:45 +05:30
|
|
|
case 9: /* replace */
|
2002-12-02 06:05:23 +05:30
|
|
|
flags = NLM_F_CREATE|NLM_F_REPLACE;
|
2007-01-27 18:43:45 +05:30
|
|
|
case 10: /* test */
|
2002-12-02 06:05:23 +05:30
|
|
|
flags = NLM_F_EXCL;
|
2007-01-27 18:43:45 +05:30
|
|
|
case 11: /* flush */
|
2003-02-15 17:20:33 +05:30
|
|
|
return iproute_list_or_flush(argc-1, argv+1, 1);
|
2002-12-02 06:05:23 +05:30
|
|
|
default:
|
2006-10-26 06:07:00 +05:30
|
|
|
bb_error_msg_and_die("unknown command %s", *argv);
|
2002-12-02 06:05:23 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return iproute_modify(cmd, flags, argc-1, argv+1);
|
2002-11-10 07:03:55 +05:30
|
|
|
}
|