iproute: fix parsing and matching of of "short" IP addrs like 10/8
function old new delta print_route 1613 1813 +200 get_addr_1 209 258 +49 get_prefix 393 356 -37 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 2/1 up/down: 249/-37) Total: 212 bytes Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
parent
bac0a25f72
commit
3bb235c3b5
@ -82,7 +82,7 @@ static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
{
|
{
|
||||||
struct rtmsg *r = NLMSG_DATA(n);
|
struct rtmsg *r = NLMSG_DATA(n);
|
||||||
int len = n->nlmsg_len;
|
int len = n->nlmsg_len;
|
||||||
struct rtattr * tb[RTA_MAX+1];
|
struct rtattr *tb[RTA_MAX+1];
|
||||||
char abuf[256];
|
char abuf[256];
|
||||||
inet_prefix dst;
|
inet_prefix dst;
|
||||||
inet_prefix src;
|
inet_prefix src;
|
||||||
@ -159,8 +159,21 @@ static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM,
|
|||||||
}
|
}
|
||||||
|
|
||||||
memset(tb, 0, sizeof(tb));
|
memset(tb, 0, sizeof(tb));
|
||||||
|
memset(&src, 0, sizeof(src));
|
||||||
|
memset(&dst, 0, sizeof(dst));
|
||||||
parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
|
parse_rtattr(tb, RTA_MAX, RTM_RTA(r), len);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
if (G_filter.rdst.family
|
if (G_filter.rdst.family
|
||||||
&& inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen)
|
&& inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen)
|
||||||
) {
|
) {
|
||||||
@ -426,7 +439,8 @@ IF_FEATURE_IP_RULE(ARG_table,)
|
|||||||
NEXT_ARG();
|
NEXT_ARG();
|
||||||
}
|
}
|
||||||
if ((**argv < '0' || **argv > '9')
|
if ((**argv < '0' || **argv > '9')
|
||||||
&& rtnl_rtntype_a2n(&type, *argv) == 0) {
|
&& rtnl_rtntype_a2n(&type, *argv) == 0
|
||||||
|
) {
|
||||||
NEXT_ARG();
|
NEXT_ARG();
|
||||||
req.r.rtm_type = type;
|
req.r.rtm_type = type;
|
||||||
ok |= type_ok;
|
ok |= type_ok;
|
||||||
|
@ -393,7 +393,7 @@ void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, in
|
|||||||
if (rta->rta_type <= max) {
|
if (rta->rta_type <= max) {
|
||||||
tb[rta->rta_type] = rta;
|
tb[rta->rta_type] = rta;
|
||||||
}
|
}
|
||||||
rta = RTA_NEXT(rta,len);
|
rta = RTA_NEXT(rta, len);
|
||||||
}
|
}
|
||||||
if (len) {
|
if (len) {
|
||||||
bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
|
bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len);
|
||||||
|
@ -83,20 +83,43 @@ int get_addr_1(inet_prefix *addr, char *name, int family)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
addr->family = AF_INET;
|
|
||||||
if (family != AF_UNSPEC && family != AF_INET)
|
if (family != AF_UNSPEC && family != AF_INET)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
/* Try to parse it as IPv4 */
|
||||||
|
addr->family = AF_INET;
|
||||||
|
#if 0 /* Doesn't handle e.g. "10.10", for example, "ip r l root 10.10/16" */
|
||||||
if (inet_pton(AF_INET, name, addr->data) <= 0)
|
if (inet_pton(AF_INET, name, addr->data) <= 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
#else
|
||||||
|
{
|
||||||
|
unsigned i = 0;
|
||||||
|
unsigned n = 0;
|
||||||
|
const char *cp = name - 1;
|
||||||
|
while (*++cp) {
|
||||||
|
if ((unsigned char)(*cp - '0') <= 9) {
|
||||||
|
n = 10 * n + (unsigned char)(*cp - '0');
|
||||||
|
if (n >= 256)
|
||||||
|
return -1;
|
||||||
|
((uint8_t*)addr->data)[i] = n;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (*cp == '.' && ++i <= 3) {
|
||||||
|
n = 0;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#endif
|
||||||
addr->bytelen = 4;
|
addr->bytelen = 4;
|
||||||
addr->bitlen = -1;
|
addr->bitlen = -1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int get_prefix_1(inet_prefix *dst, char *arg, int family)
|
static void get_prefix_1(inet_prefix *dst, char *arg, int family)
|
||||||
{
|
{
|
||||||
int err;
|
|
||||||
unsigned plen;
|
|
||||||
char *slash;
|
char *slash;
|
||||||
|
|
||||||
memset(dst, 0, sizeof(*dst));
|
memset(dst, 0, sizeof(*dst));
|
||||||
@ -108,48 +131,50 @@ static int get_prefix_1(inet_prefix *dst, char *arg, int family)
|
|||||||
dst->family = family;
|
dst->family = family;
|
||||||
/*dst->bytelen = 0; - done by memset */
|
/*dst->bytelen = 0; - done by memset */
|
||||||
/*dst->bitlen = 0;*/
|
/*dst->bitlen = 0;*/
|
||||||
return 0;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
slash = strchr(arg, '/');
|
slash = strchr(arg, '/');
|
||||||
if (slash)
|
if (slash)
|
||||||
*slash = '\0';
|
*slash = '\0';
|
||||||
err = get_addr_1(dst, arg, family);
|
|
||||||
if (err == 0) {
|
if (get_addr_1(dst, arg, family) == 0) {
|
||||||
dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
|
dst->bitlen = (dst->family == AF_INET6) ? 128 : 32;
|
||||||
if (slash) {
|
if (slash) {
|
||||||
|
unsigned plen;
|
||||||
inet_prefix netmask_pfx;
|
inet_prefix netmask_pfx;
|
||||||
|
|
||||||
netmask_pfx.family = AF_UNSPEC;
|
netmask_pfx.family = AF_UNSPEC;
|
||||||
plen = bb_strtou(slash + 1, NULL, 0);
|
plen = bb_strtou(slash + 1, NULL, 0);
|
||||||
if ((errno || plen > dst->bitlen)
|
if ((errno || plen > dst->bitlen)
|
||||||
&& (get_addr_1(&netmask_pfx, slash + 1, family)))
|
&& get_addr_1(&netmask_pfx, slash + 1, family) != 0
|
||||||
err = -1;
|
) {
|
||||||
else if (netmask_pfx.family == AF_INET) {
|
goto bad;
|
||||||
|
}
|
||||||
|
if (netmask_pfx.family == AF_INET) {
|
||||||
/* fill in prefix length of dotted quad */
|
/* fill in prefix length of dotted quad */
|
||||||
uint32_t mask = ntohl(netmask_pfx.data[0]);
|
uint32_t mask = ntohl(netmask_pfx.data[0]);
|
||||||
uint32_t host = ~mask;
|
uint32_t host = ~mask;
|
||||||
|
|
||||||
/* a valid netmask must be 2^n - 1 */
|
/* a valid netmask must be 2^n - 1 */
|
||||||
if (!(host & (host + 1))) {
|
if (host & (host + 1))
|
||||||
|
goto bad;
|
||||||
|
|
||||||
for (plen = 0; mask; mask <<= 1)
|
for (plen = 0; mask; mask <<= 1)
|
||||||
++plen;
|
++plen;
|
||||||
if (plen <= dst->bitlen) {
|
if (plen > dst->bitlen)
|
||||||
dst->bitlen = plen;
|
goto bad;
|
||||||
/* dst->flags |= PREFIXLEN_SPECIFIED; */
|
/* dst->flags |= PREFIXLEN_SPECIFIED; */
|
||||||
} else
|
}
|
||||||
err = -1;
|
|
||||||
} else
|
|
||||||
err = -1;
|
|
||||||
} else {
|
|
||||||
/* plain prefix */
|
|
||||||
dst->bitlen = plen;
|
dst->bitlen = plen;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
if (slash)
|
if (slash)
|
||||||
*slash = '/';
|
*slash = '/';
|
||||||
return err;
|
return;
|
||||||
|
bad:
|
||||||
|
bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_addr(inet_prefix *dst, char *arg, int family)
|
int get_addr(inet_prefix *dst, char *arg, int family)
|
||||||
@ -163,15 +188,12 @@ int get_addr(inet_prefix *dst, char *arg, int family)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int get_prefix(inet_prefix *dst, char *arg, int family)
|
void get_prefix(inet_prefix *dst, char *arg, int family)
|
||||||
{
|
{
|
||||||
if (family == AF_PACKET) {
|
if (family == AF_PACKET) {
|
||||||
bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
|
bb_error_msg_and_die("\"%s\" may be inet %s, but it is not allowed in this context", arg, "prefix");
|
||||||
}
|
}
|
||||||
if (get_prefix_1(dst, arg, family)) {
|
get_prefix_1(dst, arg, family);
|
||||||
bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg);
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t get_addr32(char *name)
|
uint32_t get_addr32(char *name)
|
||||||
@ -204,10 +226,10 @@ void duparg2(const char *key, const char *arg)
|
|||||||
bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
|
bb_error_msg_and_die("either \"%s\" is duplicate, or \"%s\" is garbage", key, arg);
|
||||||
}
|
}
|
||||||
|
|
||||||
int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits)
|
int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits)
|
||||||
{
|
{
|
||||||
uint32_t *a1 = a->data;
|
const uint32_t *a1 = a->data;
|
||||||
uint32_t *a2 = b->data;
|
const uint32_t *a2 = b->data;
|
||||||
int words = bits >> 5;
|
int words = bits >> 5;
|
||||||
|
|
||||||
bits &= 0x1f;
|
bits &= 0x1f;
|
||||||
|
@ -58,9 +58,9 @@ struct ipx_addr {
|
|||||||
|
|
||||||
extern uint32_t get_addr32(char *name);
|
extern uint32_t get_addr32(char *name);
|
||||||
extern int get_addr_1(inet_prefix *dst, char *arg, int family);
|
extern int get_addr_1(inet_prefix *dst, char *arg, int family);
|
||||||
/*extern int get_prefix_1(inet_prefix *dst, char *arg, int family);*/
|
/*extern void get_prefix_1(inet_prefix *dst, char *arg, int family);*/
|
||||||
extern int get_addr(inet_prefix *dst, char *arg, int family);
|
extern int get_addr(inet_prefix *dst, char *arg, int family);
|
||||||
extern int get_prefix(inet_prefix *dst, char *arg, int family);
|
extern void get_prefix(inet_prefix *dst, char *arg, int family);
|
||||||
|
|
||||||
extern unsigned get_unsigned(char *arg, const char *errmsg);
|
extern unsigned get_unsigned(char *arg, const char *errmsg);
|
||||||
extern uint32_t get_u32(char *arg, const char *errmsg);
|
extern uint32_t get_u32(char *arg, const char *errmsg);
|
||||||
@ -77,7 +77,7 @@ extern const char *format_host(int af, int len, void *addr, char *buf, int bufle
|
|||||||
void invarg(const char *, const char *) NORETURN;
|
void invarg(const char *, const char *) NORETURN;
|
||||||
void duparg(const char *, const char *) NORETURN;
|
void duparg(const char *, const char *) NORETURN;
|
||||||
void duparg2(const char *, const char *) NORETURN;
|
void duparg2(const char *, const char *) NORETURN;
|
||||||
int inet_addr_match(inet_prefix *a, inet_prefix *b, int bits);
|
int inet_addr_match(const inet_prefix *a, const inet_prefix *b, int bits);
|
||||||
|
|
||||||
const char *dnet_ntop(int af, const void *addr, char *str, size_t len);
|
const char *dnet_ntop(int af, const void *addr, char *str, size_t len);
|
||||||
int dnet_pton(int af, const char *src, void *addr);
|
int dnet_pton(int af, const char *src, void *addr);
|
||||||
|
Loading…
Reference in New Issue
Block a user