{
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;
}
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)
) {
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;
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));
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)
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)
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;
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);
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);