2006-01-12 12:08:46 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
2002-11-10 01:33:55 +00:00
|
|
|
/*
|
|
|
|
* iproute.c "ip route".
|
|
|
|
*
|
2005-10-26 10:47:26 +00:00
|
|
|
* Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
|
2002-11-10 01:33:55 +00:00
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2007-05-31 22:42:12 +00:00
|
|
|
#include "ip_common.h" /* #include "libbb.h" is inside */
|
2002-11-10 01:33:55 +00:00
|
|
|
#include "rt_names.h"
|
|
|
|
#include "utils.h"
|
|
|
|
|
|
|
|
#ifndef RTAX_RTTVAR
|
|
|
|
#define RTAX_RTTVAR RTAX_HOPS
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2010-02-05 13:40:23 +00:00
|
|
|
struct filter_t {
|
2002-11-10 01:33:55 +00:00
|
|
|
int tb;
|
2008-05-31 07:33:18 +00:00
|
|
|
smallint flushed;
|
2003-02-15 11:50:33 +00:00
|
|
|
char *flushb;
|
2002-11-10 01:33:55 +00:00
|
|
|
int flushp;
|
|
|
|
int flushe;
|
|
|
|
struct rtnl_handle *rth;
|
2010-01-26 10:03:16 +00:00
|
|
|
//int protocol, protocolmask; - write-only fields?!
|
|
|
|
//int scope, scopemask; - unused
|
|
|
|
//int type; - read-only
|
|
|
|
//int typemask; - unused
|
|
|
|
//int tos, tosmask; - unused
|
2002-11-10 01:33:55 +00:00
|
|
|
int iif, iifmask;
|
|
|
|
int oif, oifmask;
|
2010-01-26 10:03:16 +00:00
|
|
|
//int realm, realmmask; - unused
|
|
|
|
//inet_prefix rprefsrc; - read-only
|
2002-11-10 01:33:55 +00:00
|
|
|
inet_prefix rvia;
|
|
|
|
inet_prefix rdst;
|
|
|
|
inet_prefix mdst;
|
|
|
|
inet_prefix rsrc;
|
|
|
|
inet_prefix msrc;
|
2010-02-05 13:40:23 +00:00
|
|
|
} FIX_ALIASING;
|
|
|
|
typedef struct filter_t filter_t;
|
2007-04-07 01:14:45 +00:00
|
|
|
|
2010-01-26 10:03:16 +00:00
|
|
|
#define G_filter (*(filter_t*)&bb_common_bufsiz1)
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2003-02-15 11:50:33 +00:00
|
|
|
static int flush_update(void)
|
|
|
|
{
|
2010-01-26 10:03:16 +00:00
|
|
|
if (rtnl_send(G_filter.rth, G_filter.flushb, G_filter.flushp) < 0) {
|
2010-03-23 15:25:17 +00:00
|
|
|
bb_perror_msg("can't send flush request");
|
2003-02-15 11:50:33 +00:00
|
|
|
return -1;
|
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
G_filter.flushp = 0;
|
2003-02-15 11:50:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-12-31 19:07:23 +00:00
|
|
|
static unsigned get_hz(void)
|
2006-12-31 18:58:32 +00:00
|
|
|
{
|
2006-12-31 19:07:23 +00:00
|
|
|
static unsigned hz_internal;
|
2006-12-31 18:58:32 +00:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
if (hz_internal)
|
|
|
|
return hz_internal;
|
|
|
|
|
2008-07-21 23:05:26 +00:00
|
|
|
fp = fopen_for_read("/proc/net/psched");
|
2006-12-31 18:58:32 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2009-06-05 10:06:05 +00:00
|
|
|
static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
|
2008-07-05 09:18:54 +00:00
|
|
|
struct nlmsghdr *n, void *arg UNUSED_PARAM)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
struct rtmsg *r = NLMSG_DATA(n);
|
|
|
|
int len = n->nlmsg_len;
|
|
|
|
struct rtattr * tb[RTA_MAX+1];
|
|
|
|
char abuf[256];
|
2003-04-26 02:22:19 +00:00
|
|
|
inet_prefix dst;
|
|
|
|
inet_prefix src;
|
2002-11-10 01:33:55 +00:00
|
|
|
int host_len = -1;
|
|
|
|
SPRINT_BUF(b1);
|
2004-03-15 08:29:22 +00:00
|
|
|
|
2002-11-10 01:33:55 +00:00
|
|
|
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 10:03:16 +00:00
|
|
|
if (G_filter.flushb && n->nlmsg_type != RTM_NEWROUTE)
|
2003-02-15 11:50:33 +00:00
|
|
|
return 0;
|
2002-11-10 01:33:55 +00:00
|
|
|
len -= NLMSG_LENGTH(sizeof(*r));
|
2007-04-12 11:34:39 +00:00
|
|
|
if (len < 0)
|
|
|
|
bb_error_msg_and_die("wrong nlmsg len %d", len);
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
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 10:03:16 +00:00
|
|
|
if (G_filter.tb) {
|
|
|
|
if (G_filter.tb < 0) {
|
2007-11-26 18:29:52 +00:00
|
|
|
if (!(r->rtm_flags & RTM_F_CLONED)) {
|
2002-11-10 01:33:55 +00:00
|
|
|
return 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
} else {
|
2007-11-26 18:29:52 +00:00
|
|
|
if (r->rtm_flags & RTM_F_CLONED) {
|
2002-11-10 01:33:55 +00:00
|
|
|
return 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
if (G_filter.tb == RT_TABLE_LOCAL) {
|
2002-11-28 11:17:19 +00:00
|
|
|
if (r->rtm_type != RTN_LOCAL) {
|
2002-11-10 01:33:55 +00:00
|
|
|
return 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
} else if (G_filter.tb == RT_TABLE_MAIN) {
|
2002-11-28 11:17:19 +00:00
|
|
|
if (r->rtm_type == RTN_LOCAL) {
|
2002-11-10 01:33:55 +00:00
|
|
|
return 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2010-01-26 10:03:16 +00:00
|
|
|
if (G_filter.tb > 0 && G_filter.tb != r->rtm_table) {
|
2002-11-10 01:33:55 +00:00
|
|
|
return 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
if (G_filter.rdst.family
|
|
|
|
&& (r->rtm_family != G_filter.rdst.family || G_filter.rdst.bitlen > r->rtm_dst_len)
|
|
|
|
) {
|
2002-11-10 01:33:55 +00:00
|
|
|
return 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
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 01:33:55 +00:00
|
|
|
return 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
if (G_filter.rsrc.family
|
|
|
|
&& (r->rtm_family != G_filter.rsrc.family || G_filter.rsrc.bitlen > r->rtm_src_len)
|
|
|
|
) {
|
2002-11-10 01:33:55 +00:00
|
|
|
return 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
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 01:33:55 +00:00
|
|
|
return 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
memset(tb, 0, sizeof(tb));
|
|
|
|
parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
|
|
|
|
|
2010-01-28 01:24:24 +00:00
|
|
|
if (G_filter.rdst.family
|
|
|
|
&& inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen)
|
|
|
|
) {
|
2003-04-26 02:22:19 +00:00
|
|
|
return 0;
|
2010-01-28 01:24:24 +00:00
|
|
|
}
|
|
|
|
if (G_filter.mdst.family
|
|
|
|
&& G_filter.mdst.bitlen >= 0
|
|
|
|
&& inet_addr_match(&dst, &G_filter.mdst, r->rtm_dst_len)
|
|
|
|
) {
|
2003-04-26 02:22:19 +00:00
|
|
|
return 0;
|
2010-01-28 01:24:24 +00:00
|
|
|
}
|
|
|
|
if (G_filter.rsrc.family
|
|
|
|
&& inet_addr_match(&src, &G_filter.rsrc, G_filter.rsrc.bitlen)
|
|
|
|
) {
|
2003-04-26 02:22:19 +00:00
|
|
|
return 0;
|
2010-01-28 01:24:24 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
if (G_filter.msrc.family && G_filter.msrc.bitlen >= 0
|
|
|
|
&& inet_addr_match(&src, &G_filter.msrc, r->rtm_src_len)
|
|
|
|
) {
|
2003-04-26 02:22:19 +00:00
|
|
|
return 0;
|
2010-01-26 10:03:16 +00:00
|
|
|
}
|
|
|
|
if (G_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
|
|
|
|
) {
|
2003-02-15 11:50:33 +00:00
|
|
|
return 0;
|
2010-01-26 10:03:16 +00:00
|
|
|
}
|
2003-02-15 11:50:33 +00:00
|
|
|
|
2010-01-26 10:03:16 +00:00
|
|
|
if (G_filter.flushb) {
|
2003-02-15 11:50:33 +00:00
|
|
|
struct nlmsghdr *fn;
|
2010-01-26 10:03:16 +00:00
|
|
|
if (NLMSG_ALIGN(G_filter.flushp) + n->nlmsg_len > G_filter.flushe) {
|
2003-02-15 11:50:33 +00:00
|
|
|
if (flush_update())
|
2007-04-12 11:34:39 +00:00
|
|
|
bb_error_msg_and_die("flush");
|
2003-02-15 11:50:33 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
fn = (struct nlmsghdr*)(G_filter.flushb + NLMSG_ALIGN(G_filter.flushp));
|
2003-02-15 11:50:33 +00:00
|
|
|
memcpy(fn, n, n->nlmsg_len);
|
|
|
|
fn->nlmsg_type = RTM_DELROUTE;
|
|
|
|
fn->nlmsg_flags = NLM_F_REQUEST;
|
2010-01-26 10:03:16 +00:00
|
|
|
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 11:50:33 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-11-28 11:17:19 +00:00
|
|
|
if (n->nlmsg_type == RTM_DELROUTE) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf("Deleted ");
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
if (r->rtm_type != RTN_UNICAST /* && !G_filter.type - always 0 */) {
|
2009-10-13 15:58:24 +00:00
|
|
|
printf("%s ", rtnl_rtntype_n2a(r->rtm_type, b1));
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
if (tb[RTA_DST]) {
|
|
|
|
if (r->rtm_dst_len != host_len) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf("%s/%u ", rt_addr_n2a(r->rtm_family,
|
|
|
|
RTA_DATA(tb[RTA_DST]),
|
|
|
|
abuf, sizeof(abuf)),
|
|
|
|
r->rtm_dst_len
|
|
|
|
);
|
2002-11-10 01:33:55 +00:00
|
|
|
} else {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf("%s ", format_host(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_DST]),
|
|
|
|
RTA_DATA(tb[RTA_DST]),
|
|
|
|
abuf, sizeof(abuf))
|
|
|
|
);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
} else if (r->rtm_dst_len) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf("0/%d ", r->rtm_dst_len);
|
2002-11-10 01:33:55 +00:00
|
|
|
} else {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf("default ");
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
if (tb[RTA_SRC]) {
|
|
|
|
if (r->rtm_src_len != host_len) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf("from %s/%u ", rt_addr_n2a(r->rtm_family,
|
|
|
|
RTA_DATA(tb[RTA_SRC]),
|
|
|
|
abuf, sizeof(abuf)),
|
|
|
|
r->rtm_src_len
|
|
|
|
);
|
2002-11-10 01:33:55 +00:00
|
|
|
} else {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf("from %s ", format_host(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_SRC]),
|
|
|
|
RTA_DATA(tb[RTA_SRC]),
|
|
|
|
abuf, sizeof(abuf))
|
|
|
|
);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
} else if (r->rtm_src_len) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf("from 0/%u ", r->rtm_src_len);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
if (tb[RTA_GATEWAY] && G_filter.rvia.bitlen != host_len) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf("via %s ", format_host(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_GATEWAY]),
|
|
|
|
RTA_DATA(tb[RTA_GATEWAY]),
|
|
|
|
abuf, sizeof(abuf)));
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
if (tb[RTA_OIF] && G_filter.oifmask != -1) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf("dev %s ", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_OIF])));
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2010-01-26 10:03:16 +00:00
|
|
|
if (tb[RTA_PREFSRC] && /*G_filter.rprefsrc.bitlen - always 0*/ 0 != host_len) {
|
2002-11-10 01:33:55 +00:00
|
|
|
/* Do not use format_host(). It is our local addr
|
|
|
|
and symbolic name will not be useful.
|
|
|
|
*/
|
2008-06-29 06:22:40 +00:00
|
|
|
printf(" src %s ", rt_addr_n2a(r->rtm_family,
|
|
|
|
RTA_DATA(tb[RTA_PREFSRC]),
|
|
|
|
abuf, sizeof(abuf)));
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2002-11-28 11:17:19 +00:00
|
|
|
if (tb[RTA_PRIORITY]) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf(" metric %d ", *(uint32_t*)RTA_DATA(tb[RTA_PRIORITY]));
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
if (r->rtm_family == AF_INET6) {
|
|
|
|
struct rta_cacheinfo *ci = NULL;
|
2002-11-28 11:17:19 +00:00
|
|
|
if (tb[RTA_CACHEINFO]) {
|
2002-11-10 01:33:55 +00:00
|
|
|
ci = RTA_DATA(tb[RTA_CACHEINFO]);
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
if ((r->rtm_flags & RTM_F_CLONED) || (ci && ci->rta_expires)) {
|
2002-11-28 11:17:19 +00:00
|
|
|
if (r->rtm_flags & RTM_F_CLONED) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf("%c cache ", _SL_);
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
|
|
|
if (ci->rta_expires) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf(" expires %dsec", ci->rta_expires / get_hz());
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
|
|
|
if (ci->rta_error != 0) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf(" error %d", ci->rta_error);
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
} else if (ci) {
|
|
|
|
if (ci->rta_error != 0)
|
2008-06-29 06:22:40 +00:00
|
|
|
printf(" error %d", ci->rta_error);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
if (tb[RTA_IIF] && G_filter.iifmask != -1) {
|
2008-06-29 06:22:40 +00:00
|
|
|
printf(" iif %s", ll_index_to_name(*(int*)RTA_DATA(tb[RTA_IIF])));
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2008-06-29 06:22:40 +00:00
|
|
|
bb_putchar('\n');
|
2002-11-10 01:33:55 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2007-04-07 01:14:45 +00:00
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2007-11-18 22:56:25 +00:00
|
|
|
static int iproute_modify(int cmd, unsigned flags, char **argv)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char keywords[] ALIGN1 =
|
2009-04-21 11:09:40 +00:00
|
|
|
"src\0""via\0""mtu\0""lock\0""protocol\0"IF_FEATURE_IP_RULE("table\0")
|
2008-10-07 17:00:58 +00:00
|
|
|
"dev\0""oif\0""to\0""metric\0";
|
2007-07-24 15:54:42 +00:00
|
|
|
enum {
|
|
|
|
ARG_src,
|
|
|
|
ARG_via,
|
|
|
|
ARG_mtu, PARM_lock,
|
|
|
|
ARG_protocol,
|
2009-04-21 11:09:40 +00:00
|
|
|
IF_FEATURE_IP_RULE(ARG_table,)
|
2007-07-24 15:54:42 +00:00
|
|
|
ARG_dev,
|
|
|
|
ARG_oif,
|
2008-10-07 17:00:58 +00:00
|
|
|
ARG_to,
|
|
|
|
ARG_metric,
|
2007-07-24 15:54:42 +00:00
|
|
|
};
|
|
|
|
enum {
|
|
|
|
gw_ok = 1 << 0,
|
|
|
|
dst_ok = 1 << 1,
|
|
|
|
proto_ok = 1 << 2,
|
|
|
|
type_ok = 1 << 3
|
|
|
|
};
|
2002-11-10 01:33:55 +00:00
|
|
|
struct rtnl_handle rth;
|
|
|
|
struct {
|
2006-01-25 00:08:53 +00:00
|
|
|
struct nlmsghdr n;
|
|
|
|
struct rtmsg r;
|
|
|
|
char buf[1024];
|
2002-11-10 01:33:55 +00:00
|
|
|
} req;
|
2007-04-07 01:14:45 +00:00
|
|
|
char mxbuf[256];
|
2002-11-10 01:33:55 +00:00
|
|
|
struct rtattr * mxrta = (void*)mxbuf;
|
|
|
|
unsigned mxlock = 0;
|
2007-04-07 01:14:45 +00:00
|
|
|
char *d = NULL;
|
2007-04-12 11:36:56 +00:00
|
|
|
smalluint ok = 0;
|
|
|
|
int arg;
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
2008-06-29 06:22:40 +00:00
|
|
|
req.n.nlmsg_flags = NLM_F_REQUEST | flags;
|
2002-11-10 01:33:55 +00:00
|
|
|
req.n.nlmsg_type = cmd;
|
|
|
|
req.r.rtm_family = preferred_family;
|
2008-06-29 06:22:40 +00:00
|
|
|
if (RT_TABLE_MAIN) /* if it is zero, memset already did it */
|
|
|
|
req.r.rtm_table = RT_TABLE_MAIN;
|
|
|
|
if (RT_SCOPE_NOWHERE)
|
|
|
|
req.r.rtm_scope = RT_SCOPE_NOWHERE;
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2007-11-18 22:56:25 +00:00
|
|
|
while (*argv) {
|
2007-07-24 15:54:42 +00:00
|
|
|
arg = index_in_substrings(keywords, *argv);
|
2007-04-12 11:36:56 +00:00
|
|
|
if (arg == ARG_src) {
|
2002-11-10 01:33:55 +00:00
|
|
|
inet_prefix addr;
|
|
|
|
NEXT_ARG();
|
|
|
|
get_addr(&addr, *argv, req.r.rtm_family);
|
2007-04-12 11:36:56 +00:00
|
|
|
if (req.r.rtm_family == AF_UNSPEC)
|
2002-11-10 01:33:55 +00:00
|
|
|
req.r.rtm_family = addr.family;
|
|
|
|
addattr_l(&req.n, sizeof(req), RTA_PREFSRC, &addr.data, addr.bytelen);
|
2007-04-12 11:36:56 +00:00
|
|
|
} else if (arg == ARG_via) {
|
2002-11-10 01:33:55 +00:00
|
|
|
inet_prefix addr;
|
2007-04-12 11:36:56 +00:00
|
|
|
ok |= gw_ok;
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
get_addr(&addr, *argv, req.r.rtm_family);
|
2002-11-28 11:17:19 +00:00
|
|
|
if (req.r.rtm_family == AF_UNSPEC) {
|
2002-11-10 01:33:55 +00:00
|
|
|
req.r.rtm_family = addr.family;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
addattr_l(&req.n, sizeof(req), RTA_GATEWAY, &addr.data, addr.bytelen);
|
2007-04-12 11:36:56 +00:00
|
|
|
} else if (arg == ARG_mtu) {
|
2002-11-10 01:33:55 +00:00
|
|
|
unsigned mtu;
|
|
|
|
NEXT_ARG();
|
2007-07-24 15:54:42 +00:00
|
|
|
if (index_in_strings(keywords, *argv) == PARM_lock) {
|
2007-11-26 18:29:52 +00:00
|
|
|
mxlock |= (1 << RTAX_MTU);
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
}
|
2009-03-05 09:21:57 +00:00
|
|
|
mtu = get_unsigned(*argv, "mtu");
|
2002-11-10 01:33:55 +00:00
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_MTU, mtu);
|
2007-04-12 11:36:56 +00:00
|
|
|
} else if (arg == ARG_protocol) {
|
2006-01-30 18:00:02 +00:00
|
|
|
uint32_t prot;
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
if (rtnl_rtprot_a2n(&prot, *argv))
|
2006-06-07 20:17:41 +00:00
|
|
|
invarg(*argv, "protocol");
|
2002-11-10 01:33:55 +00:00
|
|
|
req.r.rtm_protocol = prot;
|
2007-04-12 11:36:56 +00:00
|
|
|
ok |= proto_ok;
|
2007-04-16 14:56:01 +00:00
|
|
|
#if ENABLE_FEATURE_IP_RULE
|
2007-04-12 11:36:56 +00:00
|
|
|
} else if (arg == ARG_table) {
|
2006-12-15 00:59:35 +00:00
|
|
|
uint32_t tid;
|
|
|
|
NEXT_ARG();
|
|
|
|
if (rtnl_rttable_a2n(&tid, *argv))
|
|
|
|
invarg(*argv, "table");
|
|
|
|
req.r.rtm_table = tid;
|
2007-04-16 14:56:01 +00:00
|
|
|
#endif
|
2007-04-12 11:36:56 +00:00
|
|
|
} else if (arg == ARG_dev || arg == ARG_oif) {
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
d = *argv;
|
2008-10-07 17:00:58 +00:00
|
|
|
} else if (arg == ARG_metric) {
|
|
|
|
uint32_t metric;
|
|
|
|
NEXT_ARG();
|
2009-03-05 09:21:57 +00:00
|
|
|
metric = get_u32(*argv, "metric");
|
2008-10-07 17:00:58 +00:00
|
|
|
addattr32(&req.n, sizeof(req), RTA_PRIORITY, metric);
|
2002-11-10 01:33:55 +00:00
|
|
|
} else {
|
|
|
|
int type;
|
|
|
|
inet_prefix dst;
|
|
|
|
|
2007-04-12 11:36:56 +00:00
|
|
|
if (arg == ARG_to) {
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
}
|
2007-04-07 01:14:45 +00:00
|
|
|
if ((**argv < '0' || **argv > '9')
|
2007-11-26 18:29:52 +00:00
|
|
|
&& rtnl_rtntype_a2n(&type, *argv) == 0) {
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
req.r.rtm_type = type;
|
2007-04-12 11:36:56 +00:00
|
|
|
ok |= type_ok;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2007-04-12 11:36:56 +00:00
|
|
|
if (ok & dst_ok) {
|
2002-11-10 01:33:55 +00:00
|
|
|
duparg2("to", *argv);
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
get_prefix(&dst, *argv, req.r.rtm_family);
|
2002-11-28 11:17:19 +00:00
|
|
|
if (req.r.rtm_family == AF_UNSPEC) {
|
2002-11-10 01:33:55 +00:00
|
|
|
req.r.rtm_family = dst.family;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
req.r.rtm_dst_len = dst.bitlen;
|
2007-04-12 11:36:56 +00:00
|
|
|
ok |= dst_ok;
|
2002-11-28 11:17:19 +00:00
|
|
|
if (dst.bytelen) {
|
2002-11-10 01:33:55 +00:00
|
|
|
addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2007-11-18 22:56:25 +00:00
|
|
|
argv++;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2007-04-12 11:34:39 +00:00
|
|
|
xrtnl_open(&rth);
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2002-11-15 09:12:47 +00:00
|
|
|
if (d) {
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
ll_init_map(&rth);
|
|
|
|
|
|
|
|
if (d) {
|
2007-04-12 11:34:39 +00:00
|
|
|
idx = xll_name_to_index(d);
|
2002-11-15 09:12:47 +00:00
|
|
|
addattr32(&req.n, sizeof(req), RTA_OIF, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-10 01:33:55 +00:00
|
|
|
if (mxrta->rta_len > RTA_LENGTH(0)) {
|
2002-11-28 11:17:19 +00:00
|
|
|
if (mxlock) {
|
2002-11-10 01:33:55 +00:00
|
|
|
rta_addattr32(mxrta, sizeof(mxbuf), RTAX_LOCK, mxlock);
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
addattr_l(&req.n, sizeof(req), RTA_METRICS, RTA_DATA(mxrta), RTA_PAYLOAD(mxrta));
|
|
|
|
}
|
2007-04-12 11:36:56 +00:00
|
|
|
|
2007-04-11 16:23:57 +00:00
|
|
|
if (req.r.rtm_type == RTN_LOCAL || req.r.rtm_type == RTN_NAT)
|
|
|
|
req.r.rtm_scope = RT_SCOPE_HOST;
|
2010-01-26 10:03:16 +00:00
|
|
|
else
|
|
|
|
if (req.r.rtm_type == RTN_BROADCAST
|
|
|
|
|| req.r.rtm_type == RTN_MULTICAST
|
|
|
|
|| req.r.rtm_type == RTN_ANYCAST
|
|
|
|
) {
|
2007-04-11 16:23:57 +00:00
|
|
|
req.r.rtm_scope = RT_SCOPE_LINK;
|
2010-01-26 10:03:16 +00:00
|
|
|
}
|
2007-04-11 16:23:57 +00:00
|
|
|
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;
|
2007-04-12 11:36:56 +00:00
|
|
|
else if (!(ok & gw_ok))
|
2007-04-11 16:23:57 +00:00
|
|
|
req.r.rtm_scope = RT_SCOPE_LINK;
|
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2002-11-28 11:17:19 +00:00
|
|
|
if (req.r.rtm_family == AF_UNSPEC) {
|
2002-11-10 01:33:55 +00:00
|
|
|
req.r.rtm_family = AF_INET;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2002-11-28 11:17:19 +00:00
|
|
|
if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0) {
|
2007-04-07 01:14:45 +00:00
|
|
|
return 2;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
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 06:22:40 +00:00
|
|
|
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 01:33:55 +00:00
|
|
|
req.nlh.nlmsg_seq = rth->dump = ++rth->seq;
|
|
|
|
req.rtm.rtm_family = family;
|
2008-06-29 06:22:40 +00:00
|
|
|
if (RTM_F_CLONED)
|
|
|
|
req.rtm.rtm_flags = RTM_F_CLONED;
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2007-04-12 11:34:39 +00:00
|
|
|
return xsendto(rth->fd, (void*)&req, sizeof(req), (struct sockaddr*)&nladdr, sizeof(nladdr));
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2007-04-07 01:14:45 +00:00
|
|
|
static void iproute_flush_cache(void)
|
2003-02-15 11:50:33 +00:00
|
|
|
{
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char fn[] ALIGN1 = "/proc/sys/net/ipv4/route/flush";
|
2007-04-11 23:20:53 +00:00
|
|
|
int flush_fd = open_or_warn(fn, O_WRONLY);
|
2007-04-07 01:14:45 +00:00
|
|
|
|
2003-02-15 11:50:33 +00:00
|
|
|
if (flush_fd < 0) {
|
2007-04-07 01:14:45 +00:00
|
|
|
return;
|
2003-02-15 11:50:33 +00:00
|
|
|
}
|
|
|
|
|
2007-01-29 23:43:18 +00:00
|
|
|
if (write(flush_fd, "-1", 2) < 2) {
|
2009-11-13 08:08:27 +00:00
|
|
|
bb_perror_msg("can't flush routing cache");
|
2007-04-07 01:14:45 +00:00
|
|
|
return;
|
2003-02-15 11:50:33 +00:00
|
|
|
}
|
|
|
|
close(flush_fd);
|
|
|
|
}
|
|
|
|
|
2002-12-02 00:35:23 +00:00
|
|
|
static void iproute_reset_filter(void)
|
|
|
|
{
|
2010-01-26 10:03:16 +00:00
|
|
|
memset(&G_filter, 0, sizeof(G_filter));
|
|
|
|
G_filter.mdst.bitlen = -1;
|
|
|
|
G_filter.msrc.bitlen = -1;
|
2002-12-02 00:35:23 +00:00
|
|
|
}
|
|
|
|
|
2007-04-07 01:14:45 +00:00
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2007-11-18 22:56:25 +00:00
|
|
|
static int iproute_list_or_flush(char **argv, int flush)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
int do_ipv6 = preferred_family;
|
|
|
|
struct rtnl_handle rth;
|
|
|
|
char *id = NULL;
|
|
|
|
char *od = NULL;
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char keywords[] ALIGN1 =
|
2007-11-26 18:29:52 +00:00
|
|
|
/* "ip route list/flush" parameters: */
|
|
|
|
"protocol\0" "dev\0" "oif\0" "iif\0"
|
|
|
|
"via\0" "table\0" "cache\0"
|
|
|
|
"from\0" "to\0"
|
|
|
|
/* and possible further keywords */
|
|
|
|
"all\0"
|
|
|
|
"root\0"
|
|
|
|
"match\0"
|
|
|
|
"exact\0"
|
|
|
|
"main\0"
|
|
|
|
;
|
2007-04-12 11:36:56 +00:00
|
|
|
enum {
|
2007-11-26 18:29:52 +00:00
|
|
|
KW_proto, KW_dev, KW_oif, KW_iif,
|
|
|
|
KW_via, KW_table, KW_cache,
|
|
|
|
KW_from, KW_to,
|
|
|
|
/* */
|
|
|
|
KW_all,
|
|
|
|
KW_root,
|
|
|
|
KW_match,
|
|
|
|
KW_exact,
|
|
|
|
KW_main,
|
2007-04-12 11:36:56 +00:00
|
|
|
};
|
|
|
|
int arg, parm;
|
2007-11-26 18:29:52 +00:00
|
|
|
|
2002-11-10 01:33:55 +00:00
|
|
|
iproute_reset_filter();
|
2010-01-26 10:03:16 +00:00
|
|
|
G_filter.tb = RT_TABLE_MAIN;
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2007-11-18 22:56:25 +00:00
|
|
|
if (flush && !*argv)
|
2007-04-07 01:14:45 +00:00
|
|
|
bb_error_msg_and_die(bb_msg_requires_arg, "\"ip route flush\"");
|
2003-02-15 11:50:33 +00:00
|
|
|
|
2007-11-18 22:56:25 +00:00
|
|
|
while (*argv) {
|
2007-07-24 15:54:42 +00:00
|
|
|
arg = index_in_substrings(keywords, *argv);
|
2007-11-26 18:29:52 +00:00
|
|
|
if (arg == KW_proto) {
|
2006-01-30 18:00:02 +00:00
|
|
|
uint32_t prot = 0;
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
2010-01-26 10:03:16 +00:00
|
|
|
//G_filter.protocolmask = -1;
|
2002-11-10 01:33:55 +00:00
|
|
|
if (rtnl_rtprot_a2n(&prot, *argv)) {
|
2007-11-26 18:29:52 +00:00
|
|
|
if (index_in_strings(keywords, *argv) != KW_all)
|
2006-06-07 20:17:41 +00:00
|
|
|
invarg(*argv, "protocol");
|
2002-11-10 01:33:55 +00:00
|
|
|
prot = 0;
|
2010-01-26 10:03:16 +00:00
|
|
|
//G_filter.protocolmask = 0;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2010-01-26 10:03:16 +00:00
|
|
|
//G_filter.protocol = prot;
|
2007-11-26 18:29:52 +00:00
|
|
|
} else if (arg == KW_dev || arg == KW_oif) {
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
od = *argv;
|
2007-11-26 18:29:52 +00:00
|
|
|
} else if (arg == KW_iif) {
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
id = *argv;
|
2007-11-26 18:29:52 +00:00
|
|
|
} else if (arg == KW_via) {
|
2007-04-12 11:36:56 +00:00
|
|
|
NEXT_ARG();
|
2010-01-26 10:03:16 +00:00
|
|
|
get_prefix(&G_filter.rvia, *argv, do_ipv6);
|
2007-11-26 18:29:52 +00:00
|
|
|
} else if (arg == KW_table) { /* table all/cache/main */
|
2007-04-12 11:36:56 +00:00
|
|
|
NEXT_ARG();
|
2007-07-24 15:54:42 +00:00
|
|
|
parm = index_in_substrings(keywords, *argv);
|
2007-11-26 18:29:52 +00:00
|
|
|
if (parm == KW_cache)
|
2010-01-26 10:03:16 +00:00
|
|
|
G_filter.tb = -1;
|
2007-11-26 18:29:52 +00:00
|
|
|
else if (parm == KW_all)
|
2010-01-26 10:03:16 +00:00
|
|
|
G_filter.tb = 0;
|
2008-06-08 00:25:55 +00:00
|
|
|
else if (parm != KW_main) {
|
|
|
|
#if ENABLE_FEATURE_IP_RULE
|
|
|
|
uint32_t tid;
|
|
|
|
if (rtnl_rttable_a2n(&tid, *argv))
|
|
|
|
invarg(*argv, "table");
|
2010-01-26 10:03:16 +00:00
|
|
|
G_filter.tb = tid;
|
2008-06-25 09:53:17 +00:00
|
|
|
#else
|
2007-04-12 11:36:56 +00:00
|
|
|
invarg(*argv, "table");
|
2008-06-08 00:25:55 +00:00
|
|
|
#endif
|
|
|
|
}
|
2007-11-27 09:42:33 +00:00
|
|
|
} 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 10:03:16 +00:00
|
|
|
G_filter.tb = -1;
|
2007-11-26 18:29:52 +00:00
|
|
|
} else if (arg == KW_from) {
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
2007-07-24 15:54:42 +00:00
|
|
|
parm = index_in_substrings(keywords, *argv);
|
2007-11-26 18:29:52 +00:00
|
|
|
if (parm == KW_root) {
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
2010-01-26 10:03:16 +00:00
|
|
|
get_prefix(&G_filter.rsrc, *argv, do_ipv6);
|
2007-11-26 18:29:52 +00:00
|
|
|
} else if (parm == KW_match) {
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
2010-01-26 10:03:16 +00:00
|
|
|
get_prefix(&G_filter.msrc, *argv, do_ipv6);
|
2002-11-10 01:33:55 +00:00
|
|
|
} else {
|
2007-11-26 18:29:52 +00:00
|
|
|
if (parm == KW_exact)
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
2010-01-26 10:03:16 +00:00
|
|
|
get_prefix(&G_filter.msrc, *argv, do_ipv6);
|
|
|
|
G_filter.rsrc = G_filter.msrc;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2007-11-26 18:29:52 +00:00
|
|
|
} else { /* "to" is the default parameter */
|
|
|
|
if (arg == KW_to) {
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
2007-07-24 15:54:42 +00:00
|
|
|
arg = index_in_substrings(keywords, *argv);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2007-11-26 18:29:52 +00:00
|
|
|
/* parm = arg; - would be more plausible, but we reuse 'arg' here */
|
|
|
|
if (arg == KW_root) {
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
2010-01-26 10:03:16 +00:00
|
|
|
get_prefix(&G_filter.rdst, *argv, do_ipv6);
|
2007-11-26 18:29:52 +00:00
|
|
|
} else if (arg == KW_match) {
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
2010-01-26 10:03:16 +00:00
|
|
|
get_prefix(&G_filter.mdst, *argv, do_ipv6);
|
2007-11-26 18:29:52 +00:00
|
|
|
} else { /* "to exact" is the default */
|
|
|
|
if (arg == KW_exact)
|
2002-11-10 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
2010-01-26 10:03:16 +00:00
|
|
|
get_prefix(&G_filter.mdst, *argv, do_ipv6);
|
|
|
|
G_filter.rdst = G_filter.mdst;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
2007-04-07 01:14:45 +00:00
|
|
|
argv++;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2010-01-26 10:03:16 +00:00
|
|
|
if (do_ipv6 == AF_UNSPEC && G_filter.tb) {
|
2002-11-10 01:33:55 +00:00
|
|
|
do_ipv6 = AF_INET;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2007-04-12 11:34:39 +00:00
|
|
|
xrtnl_open(&rth);
|
2002-11-10 01:33:55 +00:00
|
|
|
ll_init_map(&rth);
|
|
|
|
|
|
|
|
if (id || od) {
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (id) {
|
2007-04-12 11:34:39 +00:00
|
|
|
idx = xll_name_to_index(id);
|
2010-01-26 10:03:16 +00:00
|
|
|
G_filter.iif = idx;
|
|
|
|
G_filter.iifmask = -1;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
if (od) {
|
2007-04-12 11:34:39 +00:00
|
|
|
idx = xll_name_to_index(od);
|
2010-01-26 10:03:16 +00:00
|
|
|
G_filter.oif = idx;
|
|
|
|
G_filter.oifmask = -1;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-15 11:50:33 +00:00
|
|
|
if (flush) {
|
|
|
|
char flushb[4096-512];
|
|
|
|
|
2010-01-26 10:03:16 +00:00
|
|
|
if (G_filter.tb == -1) { /* "flush table cache" */
|
2003-02-15 11:50:33 +00:00
|
|
|
if (do_ipv6 != AF_INET6)
|
|
|
|
iproute_flush_cache();
|
|
|
|
if (do_ipv6 == AF_INET)
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2010-01-26 10:03:16 +00:00
|
|
|
G_filter.flushb = flushb;
|
|
|
|
G_filter.flushp = 0;
|
|
|
|
G_filter.flushe = sizeof(flushb);
|
|
|
|
G_filter.rth = &rth;
|
2003-02-15 11:50:33 +00:00
|
|
|
|
|
|
|
for (;;) {
|
2007-04-12 11:34:39 +00:00
|
|
|
xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
|
2010-01-26 10:03:16 +00:00
|
|
|
G_filter.flushed = 0;
|
2008-06-29 06:22:40 +00:00
|
|
|
xrtnl_dump_filter(&rth, print_route, NULL);
|
2010-01-26 10:03:16 +00:00
|
|
|
if (G_filter.flushed == 0)
|
2003-02-15 11:50:33 +00:00
|
|
|
return 0;
|
2007-04-07 01:14:45 +00:00
|
|
|
if (flush_update())
|
|
|
|
return 1;
|
2003-02-15 11:50:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-01-26 10:03:16 +00:00
|
|
|
if (G_filter.tb != -1) {
|
2007-04-12 11:34:39 +00:00
|
|
|
xrtnl_wilddump_request(&rth, do_ipv6, RTM_GETROUTE);
|
2007-07-24 15:54:42 +00:00
|
|
|
} else if (rtnl_rtcache_request(&rth, do_ipv6) < 0) {
|
2009-11-13 08:08:27 +00:00
|
|
|
bb_perror_msg_and_die("can't send dump request");
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2008-06-29 06:22:40 +00:00
|
|
|
xrtnl_dump_filter(&rth, print_route, NULL);
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2007-04-07 01:14:45 +00:00
|
|
|
return 0;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-04-07 01:14:45 +00:00
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2007-11-18 22:56:25 +00:00
|
|
|
static int iproute_get(char **argv)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
|
|
|
struct rtnl_handle rth;
|
|
|
|
struct {
|
2007-07-24 15:54:42 +00:00
|
|
|
struct nlmsghdr n;
|
|
|
|
struct rtmsg r;
|
|
|
|
char buf[1024];
|
2002-11-10 01:33:55 +00:00
|
|
|
} req;
|
2007-07-24 15:54:42 +00:00
|
|
|
char *idev = NULL;
|
|
|
|
char *odev = NULL;
|
2007-04-12 11:36:56 +00:00
|
|
|
bool connected = 0;
|
|
|
|
bool from_ok = 0;
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char options[] ALIGN1 =
|
2007-07-24 15:54:42 +00:00
|
|
|
"from\0""iif\0""oif\0""dev\0""notify\0""connected\0""to\0";
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
iproute_reset_filter();
|
|
|
|
|
|
|
|
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
2008-06-29 06:22:40 +00:00
|
|
|
if (NLM_F_REQUEST)
|
|
|
|
req.n.nlmsg_flags = NLM_F_REQUEST;
|
|
|
|
if (RTM_GETROUTE)
|
|
|
|
req.n.nlmsg_type = RTM_GETROUTE;
|
2002-11-10 01:33:55 +00:00
|
|
|
req.r.rtm_family = preferred_family;
|
2008-06-29 06:22:40 +00:00
|
|
|
/*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 08:29:22 +00:00
|
|
|
|
2007-11-18 22:56:25 +00:00
|
|
|
while (*argv) {
|
2007-07-24 15:54:42 +00:00
|
|
|
switch (index_in_strings(options, *argv)) {
|
2002-12-02 00:54:10 +00:00
|
|
|
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 01:33:55 +00:00
|
|
|
NEXT_ARG();
|
2002-12-02 00:54:10 +00:00
|
|
|
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 01:33:55 +00:00
|
|
|
}
|
2007-11-18 22:56:25 +00:00
|
|
|
argv++;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.r.rtm_dst_len == 0) {
|
2003-03-19 09:13:01 +00:00
|
|
|
bb_error_msg_and_die("need at least destination address");
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2007-04-12 11:34:39 +00:00
|
|
|
xrtnl_open(&rth);
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
ll_init_map(&rth);
|
|
|
|
|
|
|
|
if (idev || odev) {
|
|
|
|
int idx;
|
|
|
|
|
|
|
|
if (idev) {
|
2007-04-12 11:34:39 +00:00
|
|
|
idx = xll_name_to_index(idev);
|
2002-11-10 01:33:55 +00:00
|
|
|
addattr32(&req.n, sizeof(req), RTA_IIF, idx);
|
|
|
|
}
|
|
|
|
if (odev) {
|
2007-04-12 11:34:39 +00:00
|
|
|
idx = xll_name_to_index(odev);
|
2002-11-10 01:33:55 +00:00
|
|
|
addattr32(&req.n, sizeof(req), RTA_OIF, idx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-28 11:17:19 +00:00
|
|
|
if (req.r.rtm_family == AF_UNSPEC) {
|
2002-11-10 01:33:55 +00:00
|
|
|
req.r.rtm_family = AF_INET;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
|
2002-11-28 11:17:19 +00:00
|
|
|
if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
|
2007-04-07 01:14:45 +00:00
|
|
|
return 2;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
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 06:22:40 +00:00
|
|
|
print_route(NULL, &req.n, NULL);
|
2002-11-10 01:33:55 +00:00
|
|
|
|
|
|
|
if (req.n.nlmsg_type != RTM_NEWROUTE) {
|
2007-04-07 01:14:45 +00:00
|
|
|
bb_error_msg_and_die("not a route?");
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
len -= NLMSG_LENGTH(sizeof(*r));
|
|
|
|
if (len < 0) {
|
2007-04-07 01:14:45 +00:00
|
|
|
bb_error_msg_and_die("wrong len %d", len);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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]) {
|
2010-03-23 15:25:17 +00:00
|
|
|
bb_error_msg_and_die("can't connect the route");
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2002-11-28 11:17:19 +00:00
|
|
|
if (!odev && tb[RTA_OIF]) {
|
2002-11-10 01:33:55 +00:00
|
|
|
tb[RTA_OIF]->rta_type = 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
|
|
|
if (tb[RTA_GATEWAY]) {
|
2002-11-10 01:33:55 +00:00
|
|
|
tb[RTA_GATEWAY]->rta_type = 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
|
|
|
if (!idev && tb[RTA_IIF]) {
|
2002-11-10 01:33:55 +00:00
|
|
|
tb[RTA_IIF]->rta_type = 0;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
req.n.nlmsg_flags = NLM_F_REQUEST;
|
|
|
|
req.n.nlmsg_type = RTM_GETROUTE;
|
|
|
|
|
2002-11-28 11:17:19 +00:00
|
|
|
if (rtnl_talk(&rth, &req.n, 0, 0, &req.n, NULL, NULL) < 0) {
|
2007-04-07 01:14:45 +00:00
|
|
|
return 2;
|
2002-11-28 11:17:19 +00:00
|
|
|
}
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
2008-06-29 06:22:40 +00:00
|
|
|
print_route(NULL, &req.n, NULL);
|
2007-04-07 01:14:45 +00:00
|
|
|
return 0;
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|
|
|
|
|
2007-04-07 01:14:45 +00:00
|
|
|
/* Return value becomes exitcode. It's okay to not return at all */
|
2007-11-18 22:56:25 +00:00
|
|
|
int do_iproute(char **argv)
|
2002-11-10 01:33:55 +00:00
|
|
|
{
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char ip_route_commands[] ALIGN1 =
|
2007-07-24 15:54:42 +00:00
|
|
|
/*0-3*/ "add\0""append\0""change\0""chg\0"
|
|
|
|
/*4-7*/ "delete\0""get\0""list\0""show\0"
|
|
|
|
/*8..*/ "prepend\0""replace\0""test\0""flush\0";
|
2008-01-04 15:28:28 +00:00
|
|
|
int command_num;
|
2007-07-24 15:54:42 +00:00
|
|
|
unsigned flags = 0;
|
2002-12-02 00:35:23 +00:00
|
|
|
int cmd = RTM_NEWROUTE;
|
|
|
|
|
2008-01-04 15:28:28 +00:00
|
|
|
if (!*argv)
|
|
|
|
return iproute_list_or_flush(argv, 0);
|
|
|
|
|
2006-11-05 18:05:09 +00:00
|
|
|
/* "Standard" 'ip r a' treats 'a' as 'add', not 'append' */
|
|
|
|
/* It probably means that it is using "first match" rule */
|
2008-01-04 15:28:28 +00:00
|
|
|
command_num = index_in_substrings(ip_route_commands, *argv);
|
|
|
|
|
2006-09-27 19:51:06 +00:00
|
|
|
switch (command_num) {
|
2007-01-27 13:13:45 +00:00
|
|
|
case 0: /* add */
|
2002-12-02 00:35:23 +00:00
|
|
|
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 13:13:45 +00:00
|
|
|
case 5: /* get */
|
2007-11-18 22:56:25 +00:00
|
|
|
return iproute_get(argv+1);
|
2007-01-27 13:13:45 +00:00
|
|
|
case 6: /* list */
|
|
|
|
case 7: /* show */
|
2007-11-18 22:56:25 +00:00
|
|
|
return iproute_list_or_flush(argv+1, 0);
|
2007-01-27 13:13:45 +00:00
|
|
|
case 8: /* prepend */
|
2002-12-02 00:35:23 +00:00
|
|
|
flags = NLM_F_CREATE;
|
2007-09-30 16:04:21 +00:00
|
|
|
break;
|
2007-01-27 13:13:45 +00:00
|
|
|
case 9: /* replace */
|
2002-12-02 00:35:23 +00:00
|
|
|
flags = NLM_F_CREATE|NLM_F_REPLACE;
|
2007-09-30 16:04:21 +00:00
|
|
|
break;
|
2007-01-27 13:13:45 +00:00
|
|
|
case 10: /* test */
|
2002-12-02 00:35:23 +00:00
|
|
|
flags = NLM_F_EXCL;
|
2007-09-30 16:04:21 +00:00
|
|
|
break;
|
2007-01-27 13:13:45 +00:00
|
|
|
case 11: /* flush */
|
2007-11-18 22:56:25 +00:00
|
|
|
return iproute_list_or_flush(argv+1, 1);
|
2002-12-02 00:35:23 +00:00
|
|
|
default:
|
2006-10-26 00:37:00 +00:00
|
|
|
bb_error_msg_and_die("unknown command %s", *argv);
|
2002-12-02 00:35:23 +00:00
|
|
|
}
|
|
|
|
|
2007-11-18 22:56:25 +00:00
|
|
|
return iproute_modify(cmd, flags, argv+1);
|
2002-11-10 01:33:55 +00:00
|
|
|
}
|