2006-11-21 15:36:08 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
|
|
|
/*
|
|
|
|
* iprule.c "ip rule".
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version
|
|
|
|
* 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Changes:
|
|
|
|
*
|
|
|
|
* Rani Assaf <rani@magic.metawire.com> 980929: resolve addresses
|
2008-09-25 12:13:34 +00:00
|
|
|
* initially integrated into busybox by Bernhard Reutner-Fischer
|
2006-11-21 15:36:08 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
2007-05-31 22:42:12 +00:00
|
|
|
#include "ip_common.h" /* #include "libbb.h" is inside */
|
2006-11-21 15:36:08 +00:00
|
|
|
#include "rt_names.h"
|
|
|
|
#include "utils.h"
|
2007-05-31 22:42:12 +00:00
|
|
|
|
2006-11-21 15:36:08 +00:00
|
|
|
/*
|
|
|
|
static void usage(void) __attribute__((noreturn));
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "Usage: ip rule [ list | add | del ] SELECTOR ACTION\n");
|
|
|
|
fprintf(stderr, "SELECTOR := [ from PREFIX ] [ to PREFIX ] [ tos TOS ] [ fwmark FWMARK ]\n");
|
|
|
|
fprintf(stderr, " [ dev STRING ] [ pref NUMBER ]\n");
|
|
|
|
fprintf(stderr, "ACTION := [ table TABLE_ID ] [ nat ADDRESS ]\n");
|
|
|
|
fprintf(stderr, " [ prohibit | reject | unreachable ]\n");
|
|
|
|
fprintf(stderr, " [ realms [SRCREALM/]DSTREALM ]\n");
|
|
|
|
fprintf(stderr, "TABLE_ID := [ local | main | default | NUMBER ]\n");
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
*/
|
2007-05-31 22:42:12 +00:00
|
|
|
|
2009-06-05 10:06:05 +00:00
|
|
|
static int FAST_FUNC print_rule(const struct sockaddr_nl *who UNUSED_PARAM,
|
2008-07-05 09:18:54 +00:00
|
|
|
struct nlmsghdr *n, void *arg UNUSED_PARAM)
|
2006-11-21 15:36:08 +00:00
|
|
|
{
|
|
|
|
struct rtmsg *r = NLMSG_DATA(n);
|
|
|
|
int len = n->nlmsg_len;
|
|
|
|
int host_len = -1;
|
|
|
|
struct rtattr * tb[RTA_MAX+1];
|
|
|
|
char abuf[256];
|
|
|
|
SPRINT_BUF(b1);
|
|
|
|
|
|
|
|
if (n->nlmsg_type != RTM_NEWRULE)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
len -= NLMSG_LENGTH(sizeof(*r));
|
|
|
|
if (len < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
memset(tb, 0, sizeof(tb));
|
|
|
|
parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
|
|
|
|
|
|
|
|
if (r->rtm_family == AF_INET)
|
|
|
|
host_len = 32;
|
|
|
|
else if (r->rtm_family == AF_INET6)
|
|
|
|
host_len = 128;
|
|
|
|
/* else if (r->rtm_family == AF_DECnet)
|
|
|
|
host_len = 16;
|
|
|
|
else if (r->rtm_family == AF_IPX)
|
|
|
|
host_len = 80;
|
|
|
|
*/
|
|
|
|
if (tb[RTA_PRIORITY])
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("%u:\t", *(unsigned*)RTA_DATA(tb[RTA_PRIORITY]));
|
2006-11-21 15:36:08 +00:00
|
|
|
else
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("0:\t");
|
2006-11-21 15:36:08 +00:00
|
|
|
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("from ");
|
2006-11-21 15:36:08 +00:00
|
|
|
if (tb[RTA_SRC]) {
|
|
|
|
if (r->rtm_src_len != host_len) {
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("%s/%u", rt_addr_n2a(r->rtm_family,
|
2006-11-21 15:36:08 +00:00
|
|
|
RTA_DATA(tb[RTA_SRC]),
|
|
|
|
abuf, sizeof(abuf)),
|
|
|
|
r->rtm_src_len
|
|
|
|
);
|
|
|
|
} else {
|
2007-09-30 16:22:36 +00:00
|
|
|
fputs(format_host(r->rtm_family,
|
2006-11-21 15:36:08 +00:00
|
|
|
RTA_PAYLOAD(tb[RTA_SRC]),
|
|
|
|
RTA_DATA(tb[RTA_SRC]),
|
2008-06-05 05:00:24 +00:00
|
|
|
abuf, sizeof(abuf)), stdout);
|
2006-11-21 15:36:08 +00:00
|
|
|
}
|
|
|
|
} else if (r->rtm_src_len) {
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("0/%d", r->rtm_src_len);
|
2006-11-21 15:36:08 +00:00
|
|
|
} else {
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("all");
|
2006-11-21 15:36:08 +00:00
|
|
|
}
|
2008-06-05 05:00:24 +00:00
|
|
|
bb_putchar(' ');
|
2006-11-21 15:36:08 +00:00
|
|
|
|
|
|
|
if (tb[RTA_DST]) {
|
|
|
|
if (r->rtm_dst_len != host_len) {
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("to %s/%u ", rt_addr_n2a(r->rtm_family,
|
2006-11-21 15:36:08 +00:00
|
|
|
RTA_DATA(tb[RTA_DST]),
|
|
|
|
abuf, sizeof(abuf)),
|
|
|
|
r->rtm_dst_len
|
|
|
|
);
|
|
|
|
} else {
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("to %s ", format_host(r->rtm_family,
|
2006-11-21 15:36:08 +00:00
|
|
|
RTA_PAYLOAD(tb[RTA_DST]),
|
|
|
|
RTA_DATA(tb[RTA_DST]),
|
|
|
|
abuf, sizeof(abuf)));
|
|
|
|
}
|
|
|
|
} else if (r->rtm_dst_len) {
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("to 0/%d ", r->rtm_dst_len);
|
2006-11-21 15:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r->rtm_tos) {
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("tos %s ", rtnl_dsfield_n2a(r->rtm_tos, b1, sizeof(b1)));
|
2006-11-21 15:36:08 +00:00
|
|
|
}
|
|
|
|
if (tb[RTA_PROTOINFO]) {
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("fwmark %#x ", *(uint32_t*)RTA_DATA(tb[RTA_PROTOINFO]));
|
2006-11-21 15:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (tb[RTA_IIF]) {
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("iif %s ", (char*)RTA_DATA(tb[RTA_IIF]));
|
2006-11-21 15:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (r->rtm_table)
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("lookup %s ", rtnl_rttable_n2a(r->rtm_table, b1, sizeof(b1)));
|
2006-11-21 15:36:08 +00:00
|
|
|
|
|
|
|
if (tb[RTA_FLOW]) {
|
2006-12-31 18:57:37 +00:00
|
|
|
uint32_t to = *(uint32_t*)RTA_DATA(tb[RTA_FLOW]);
|
|
|
|
uint32_t from = to>>16;
|
2006-11-21 15:36:08 +00:00
|
|
|
to &= 0xFFFF;
|
|
|
|
if (from) {
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("realms %s/",
|
2006-11-21 15:36:08 +00:00
|
|
|
rtnl_rtrealm_n2a(from, b1, sizeof(b1)));
|
|
|
|
}
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("%s ",
|
2006-11-21 15:36:08 +00:00
|
|
|
rtnl_rtrealm_n2a(to, b1, sizeof(b1)));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r->rtm_type == RTN_NAT) {
|
|
|
|
if (tb[RTA_GATEWAY]) {
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("map-to %s ",
|
2006-11-21 15:36:08 +00:00
|
|
|
format_host(r->rtm_family,
|
|
|
|
RTA_PAYLOAD(tb[RTA_GATEWAY]),
|
|
|
|
RTA_DATA(tb[RTA_GATEWAY]),
|
|
|
|
abuf, sizeof(abuf)));
|
|
|
|
} else
|
2008-06-05 05:00:24 +00:00
|
|
|
printf("masquerade");
|
2006-11-21 15:36:08 +00:00
|
|
|
} else if (r->rtm_type != RTN_UNICAST)
|
2008-06-05 05:00:24 +00:00
|
|
|
fputs(rtnl_rtntype_n2a(r->rtm_type, b1, sizeof(b1)), stdout);
|
2006-11-21 15:36:08 +00:00
|
|
|
|
2008-06-05 05:00:24 +00:00
|
|
|
bb_putchar('\n');
|
|
|
|
/*fflush(stdout);*/
|
2006-11-21 15:36:08 +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 iprule_list(char **argv)
|
2006-11-21 15:36:08 +00:00
|
|
|
{
|
|
|
|
struct rtnl_handle rth;
|
|
|
|
int af = preferred_family;
|
|
|
|
|
|
|
|
if (af == AF_UNSPEC)
|
|
|
|
af = AF_INET;
|
|
|
|
|
2007-11-18 22:56:25 +00:00
|
|
|
if (*argv) {
|
2007-01-28 00:20:46 +00:00
|
|
|
//bb_error_msg("\"rule show\" needs no arguments");
|
2007-11-18 22:56:25 +00:00
|
|
|
bb_warn_ignoring_args(1);
|
2006-11-21 15:36:08 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2007-04-12 11:34:39 +00:00
|
|
|
xrtnl_open(&rth);
|
2006-11-21 15:36:08 +00:00
|
|
|
|
2007-04-12 11:34:39 +00:00
|
|
|
xrtnl_wilddump_request(&rth, af, RTM_GETRULE);
|
2008-06-05 05:00:24 +00:00
|
|
|
xrtnl_dump_filter(&rth, print_rule, NULL);
|
2006-11-21 15:36:08 +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 iprule_modify(int cmd, char **argv)
|
2006-11-21 15:36:08 +00:00
|
|
|
{
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char keywords[] ALIGN1 =
|
2007-07-24 15:54:42 +00:00
|
|
|
"from\0""to\0""preference\0""order\0""priority\0"
|
|
|
|
"tos\0""fwmark\0""realms\0""table\0""lookup\0""dev\0"
|
|
|
|
"iif\0""nat\0""map-to\0""type\0""help\0";
|
|
|
|
enum {
|
|
|
|
ARG_from = 1, ARG_to, ARG_preference, ARG_order, ARG_priority,
|
|
|
|
ARG_tos, ARG_fwmark, ARG_realms, ARG_table, ARG_lookup, ARG_dev,
|
|
|
|
ARG_iif, ARG_nat, ARG_map_to, ARG_type, ARG_help
|
|
|
|
};
|
2007-06-20 14:53:49 +00:00
|
|
|
bool table_ok = 0;
|
2006-11-21 15:36:08 +00:00
|
|
|
struct rtnl_handle rth;
|
|
|
|
struct {
|
|
|
|
struct nlmsghdr n;
|
|
|
|
struct rtmsg r;
|
|
|
|
char buf[1024];
|
|
|
|
} req;
|
2007-06-20 14:53:49 +00:00
|
|
|
smalluint key;
|
2006-11-21 15:36:08 +00:00
|
|
|
|
|
|
|
memset(&req, 0, sizeof(req));
|
|
|
|
|
|
|
|
req.n.nlmsg_type = cmd;
|
|
|
|
req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct rtmsg));
|
|
|
|
req.n.nlmsg_flags = NLM_F_REQUEST;
|
|
|
|
req.r.rtm_family = preferred_family;
|
|
|
|
req.r.rtm_protocol = RTPROT_BOOT;
|
|
|
|
req.r.rtm_scope = RT_SCOPE_UNIVERSE;
|
|
|
|
req.r.rtm_table = 0;
|
|
|
|
req.r.rtm_type = RTN_UNSPEC;
|
|
|
|
|
|
|
|
if (cmd == RTM_NEWRULE) {
|
|
|
|
req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL;
|
|
|
|
req.r.rtm_type = RTN_UNICAST;
|
|
|
|
}
|
|
|
|
|
2007-11-18 22:56:25 +00:00
|
|
|
while (*argv) {
|
2007-07-24 15:54:42 +00:00
|
|
|
key = index_in_substrings(keywords, *argv) + 1;
|
2007-06-20 14:53:49 +00:00
|
|
|
if (key == 0) /* no match found in keywords array, bail out. */
|
|
|
|
bb_error_msg_and_die(bb_msg_invalid_arg, *argv, applet_name);
|
|
|
|
if (key == ARG_from) {
|
2006-11-21 15:36:08 +00:00
|
|
|
inet_prefix dst;
|
|
|
|
NEXT_ARG();
|
|
|
|
get_prefix(&dst, *argv, req.r.rtm_family);
|
|
|
|
req.r.rtm_src_len = dst.bitlen;
|
|
|
|
addattr_l(&req.n, sizeof(req), RTA_SRC, &dst.data, dst.bytelen);
|
2007-06-20 14:53:49 +00:00
|
|
|
} else if (key == ARG_to) {
|
2006-11-21 15:36:08 +00:00
|
|
|
inet_prefix dst;
|
|
|
|
NEXT_ARG();
|
|
|
|
get_prefix(&dst, *argv, req.r.rtm_family);
|
|
|
|
req.r.rtm_dst_len = dst.bitlen;
|
|
|
|
addattr_l(&req.n, sizeof(req), RTA_DST, &dst.data, dst.bytelen);
|
2007-06-20 14:53:49 +00:00
|
|
|
} else if (key == ARG_preference ||
|
|
|
|
key == ARG_order ||
|
|
|
|
key == ARG_priority) {
|
2006-12-31 18:57:37 +00:00
|
|
|
uint32_t pref;
|
2006-11-21 15:36:08 +00:00
|
|
|
NEXT_ARG();
|
2009-03-05 09:21:57 +00:00
|
|
|
pref = get_u32(*argv, "preference");
|
2006-11-21 15:36:08 +00:00
|
|
|
addattr32(&req.n, sizeof(req), RTA_PRIORITY, pref);
|
2007-06-20 14:53:49 +00:00
|
|
|
} else if (key == ARG_tos) {
|
2006-12-31 18:57:37 +00:00
|
|
|
uint32_t tos;
|
2006-11-21 15:36:08 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
if (rtnl_dsfield_a2n(&tos, *argv))
|
2007-06-20 14:53:49 +00:00
|
|
|
invarg(*argv, "TOS");
|
2006-11-21 15:36:08 +00:00
|
|
|
req.r.rtm_tos = tos;
|
2007-06-20 14:53:49 +00:00
|
|
|
} else if (key == ARG_fwmark) {
|
2006-12-31 18:57:37 +00:00
|
|
|
uint32_t fwmark;
|
2006-11-21 15:36:08 +00:00
|
|
|
NEXT_ARG();
|
2009-03-05 09:21:57 +00:00
|
|
|
fwmark = get_u32(*argv, "fwmark");
|
2006-11-21 15:36:08 +00:00
|
|
|
addattr32(&req.n, sizeof(req), RTA_PROTOINFO, fwmark);
|
2007-06-20 14:53:49 +00:00
|
|
|
} else if (key == ARG_realms) {
|
2006-12-31 18:57:37 +00:00
|
|
|
uint32_t realm;
|
2006-11-21 15:36:08 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
if (get_rt_realms(&realm, *argv))
|
2007-06-20 14:53:49 +00:00
|
|
|
invarg(*argv, "realms");
|
2006-11-21 15:36:08 +00:00
|
|
|
addattr32(&req.n, sizeof(req), RTA_FLOW, realm);
|
2007-06-20 14:53:49 +00:00
|
|
|
} else if (key == ARG_table ||
|
|
|
|
key == ARG_lookup) {
|
2007-01-28 00:20:46 +00:00
|
|
|
uint32_t tid;
|
2006-11-21 15:36:08 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
if (rtnl_rttable_a2n(&tid, *argv))
|
2007-06-20 14:53:49 +00:00
|
|
|
invarg(*argv, "table ID");
|
2006-11-21 15:36:08 +00:00
|
|
|
req.r.rtm_table = tid;
|
|
|
|
table_ok = 1;
|
2007-06-20 14:53:49 +00:00
|
|
|
} else if (key == ARG_dev ||
|
|
|
|
key == ARG_iif) {
|
2006-11-21 15:36:08 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
addattr_l(&req.n, sizeof(req), RTA_IIF, *argv, strlen(*argv)+1);
|
2007-06-20 14:53:49 +00:00
|
|
|
} else if (key == ARG_nat ||
|
|
|
|
key == ARG_map_to) {
|
2006-11-21 15:36:08 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
addattr32(&req.n, sizeof(req), RTA_GATEWAY, get_addr32(*argv));
|
|
|
|
req.r.rtm_type = RTN_NAT;
|
|
|
|
} else {
|
|
|
|
int type;
|
|
|
|
|
2007-06-20 14:53:49 +00:00
|
|
|
if (key == ARG_type) {
|
2006-11-21 15:36:08 +00:00
|
|
|
NEXT_ARG();
|
|
|
|
}
|
2007-06-20 14:53:49 +00:00
|
|
|
if (key == ARG_help)
|
2006-11-21 15:36:08 +00:00
|
|
|
bb_show_usage();
|
|
|
|
if (rtnl_rtntype_a2n(&type, *argv))
|
2007-06-20 14:53:49 +00:00
|
|
|
invarg(*argv, "type");
|
2006-11-21 15:36:08 +00:00
|
|
|
req.r.rtm_type = type;
|
|
|
|
}
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req.r.rtm_family == AF_UNSPEC)
|
|
|
|
req.r.rtm_family = AF_INET;
|
|
|
|
|
|
|
|
if (!table_ok && cmd == RTM_NEWRULE)
|
|
|
|
req.r.rtm_table = RT_TABLE_MAIN;
|
|
|
|
|
2007-04-12 11:34:39 +00:00
|
|
|
xrtnl_open(&rth);
|
2006-11-21 15:36:08 +00:00
|
|
|
|
|
|
|
if (rtnl_talk(&rth, &req.n, 0, 0, NULL, NULL, NULL) < 0)
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
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
|
|
|
int do_iprule(char **argv)
|
2006-11-21 15:36:08 +00:00
|
|
|
{
|
2007-08-12 20:58:27 +00:00
|
|
|
static const char ip_rule_commands[] ALIGN1 =
|
2007-07-24 15:54:42 +00:00
|
|
|
"add\0""delete\0""list\0""show\0";
|
2007-01-28 00:20:46 +00:00
|
|
|
int cmd = 2; /* list */
|
2006-11-21 15:36:08 +00:00
|
|
|
|
2007-11-18 22:56:25 +00:00
|
|
|
if (!*argv)
|
|
|
|
return iprule_list(argv);
|
2007-01-28 00:20:46 +00:00
|
|
|
|
2007-11-18 22:56:25 +00:00
|
|
|
cmd = index_in_substrings(ip_rule_commands, *argv);
|
2007-01-28 00:20:46 +00:00
|
|
|
switch (cmd) {
|
2006-11-21 15:36:08 +00:00
|
|
|
case 0: /* add */
|
|
|
|
cmd = RTM_NEWRULE;
|
|
|
|
break;
|
|
|
|
case 1: /* delete */
|
|
|
|
cmd = RTM_DELRULE;
|
|
|
|
break;
|
|
|
|
case 2: /* list */
|
|
|
|
case 3: /* show */
|
2007-11-18 22:56:25 +00:00
|
|
|
return iprule_list(argv+1);
|
2006-11-21 15:36:08 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
bb_error_msg_and_die("unknown command %s", *argv);
|
|
|
|
}
|
2007-11-18 22:56:25 +00:00
|
|
|
return iprule_modify(cmd, argv+1);
|
2006-11-21 15:36:08 +00:00
|
|
|
}
|