diff options
author | Denys Vlasenko | 2011-02-23 01:20:44 +0100 |
---|---|---|
committer | Denys Vlasenko | 2011-02-23 01:20:44 +0100 |
commit | 3bb235c3b50ca072e8bcfc20d5030f5460439c07 (patch) | |
tree | 8704db0e2526381902606da0c5ba5f7a91fc8e1e /networking | |
parent | bac0a25f72cdf59f34638aa3fd95bb14b103c286 (diff) | |
download | busybox-3bb235c3b50ca072e8bcfc20d5030f5460439c07.zip busybox-3bb235c3b50ca072e8bcfc20d5030f5460439c07.tar.gz |
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>
Diffstat (limited to 'networking')
-rw-r--r-- | networking/libiproute/iproute.c | 18 | ||||
-rw-r--r-- | networking/libiproute/libnetlink.c | 2 | ||||
-rw-r--r-- | networking/libiproute/utils.c | 86 | ||||
-rw-r--r-- | networking/libiproute/utils.h | 6 |
4 files changed, 74 insertions, 38 deletions
diff --git a/networking/libiproute/iproute.c b/networking/libiproute/iproute.c index 393a376..f8a67d9 100644 --- a/networking/libiproute/iproute.c +++ b/networking/libiproute/iproute.c @@ -82,7 +82,7 @@ static int FAST_FUNC print_route(const struct sockaddr_nl *who UNUSED_PARAM, { struct rtmsg *r = NLMSG_DATA(n); int len = n->nlmsg_len; - struct rtattr * tb[RTA_MAX+1]; + struct rtattr *tb[RTA_MAX+1]; char abuf[256]; inet_prefix dst; 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(&src, 0, sizeof(src)); + memset(&dst, 0, sizeof(dst)); 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 && inet_addr_match(&dst, &G_filter.rdst, G_filter.rdst.bitlen) ) { @@ -426,7 +439,8 @@ IF_FEATURE_IP_RULE(ARG_table,) NEXT_ARG(); } if ((**argv < '0' || **argv > '9') - && rtnl_rtntype_a2n(&type, *argv) == 0) { + && rtnl_rtntype_a2n(&type, *argv) == 0 + ) { NEXT_ARG(); req.r.rtm_type = type; ok |= type_ok; diff --git a/networking/libiproute/libnetlink.c b/networking/libiproute/libnetlink.c index 547013f..c7533a4 100644 --- a/networking/libiproute/libnetlink.c +++ b/networking/libiproute/libnetlink.c @@ -393,7 +393,7 @@ void FAST_FUNC parse_rtattr(struct rtattr *tb[], int max, struct rtattr *rta, in if (rta->rta_type <= max) { tb[rta->rta_type] = rta; } - rta = RTA_NEXT(rta,len); + rta = RTA_NEXT(rta, len); } if (len) { bb_error_msg("deficit %d, rta_len=%d!", len, rta->rta_len); diff --git a/networking/libiproute/utils.c b/networking/libiproute/utils.c index d32db8d..d0fe306 100644 --- a/networking/libiproute/utils.c +++ b/networking/libiproute/utils.c @@ -83,20 +83,43 @@ int get_addr_1(inet_prefix *addr, char *name, int family) return 0; } - addr->family = AF_INET; if (family != AF_UNSPEC && family != AF_INET) 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) 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->bitlen = -1; + 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; 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->bytelen = 0; - done by memset */ /*dst->bitlen = 0;*/ - return 0; + return; } slash = strchr(arg, '/'); if (slash) *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; if (slash) { + unsigned plen; inet_prefix netmask_pfx; netmask_pfx.family = AF_UNSPEC; plen = bb_strtou(slash + 1, NULL, 0); if ((errno || plen > dst->bitlen) - && (get_addr_1(&netmask_pfx, slash + 1, family))) - err = -1; - else if (netmask_pfx.family == AF_INET) { + && get_addr_1(&netmask_pfx, slash + 1, family) != 0 + ) { + goto bad; + } + if (netmask_pfx.family == AF_INET) { /* fill in prefix length of dotted quad */ uint32_t mask = ntohl(netmask_pfx.data[0]); uint32_t host = ~mask; /* a valid netmask must be 2^n - 1 */ - if (!(host & (host + 1))) { - for (plen = 0; mask; mask <<= 1) - ++plen; - if (plen <= dst->bitlen) { - dst->bitlen = plen; - /* dst->flags |= PREFIXLEN_SPECIFIED; */ - } else - err = -1; - } else - err = -1; - } else { - /* plain prefix */ - dst->bitlen = plen; + if (host & (host + 1)) + goto bad; + + for (plen = 0; mask; mask <<= 1) + ++plen; + if (plen > dst->bitlen) + goto bad; + /* dst->flags |= PREFIXLEN_SPECIFIED; */ } + dst->bitlen = plen; } } + if (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) @@ -163,15 +188,12 @@ int get_addr(inet_prefix *dst, char *arg, int family) 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) { 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)) { - bb_error_msg_and_die("an %s %s is expected rather than \"%s\"", "inet", "prefix", arg); - } - return 0; + get_prefix_1(dst, arg, family); } 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); } -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; - uint32_t *a2 = b->data; + const uint32_t *a1 = a->data; + const uint32_t *a2 = b->data; int words = bits >> 5; bits &= 0x1f; diff --git a/networking/libiproute/utils.h b/networking/libiproute/utils.h index 93c9d25..5fb4a86 100644 --- a/networking/libiproute/utils.h +++ b/networking/libiproute/utils.h @@ -58,9 +58,9 @@ struct ipx_addr { extern uint32_t get_addr32(char *name); 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_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 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 duparg(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); int dnet_pton(int af, const char *src, void *addr); |