static const struct blobmsg_policy route_attr[__ROUTE_LAST] = {
[ROUTE_TARGET] = { .name = "target", .type = BLOBMSG_TYPE_STRING },
- [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_INT32 },
+ [ROUTE_MASK] = { .name = "mask", .type = BLOBMSG_TYPE_STRING },
[ROUTE_GATEWAY] = { .name = "gateway", .type = BLOBMSG_TYPE_STRING },
[ROUTE_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
};
}
if (!tb[NOTIFY_IFNAME]) {
- if (!state->iface->main_dev.dev)
+ if (!state->proto.iface->main_dev.dev)
return UBUS_STATUS_INVALID_ARGUMENT;
} else if (!state->l3_dev.dev) {
device_add_user(&state->l3_dev,
proto_apply_static_settings(struct interface *iface, struct blob_attr *attr)
{
struct blob_attr *tb[__OPT_MAX];
- struct in_addr ina;
const char *error;
- int netmask = 32;
+ unsigned int netmask = 32;
int n_v4 = 0, n_v6 = 0;
blobmsg_parse(static_attrs, __OPT_MAX, tb, blob_data(attr), blob_len(attr));
if (tb[OPT_NETMASK]) {
- if (!inet_aton(blobmsg_data(tb[OPT_NETMASK]), &ina)) {
+ netmask = parse_netmask_string(blobmsg_data(tb[OPT_NETMASK]), false);
+ if (netmask > 32) {
error = "INVALID_NETMASK";
goto error;
}
-
- netmask = 32 - fls(~(ntohl(ina.s_addr)));
}
if (tb[OPT_IPADDR])
static struct avl_tree handlers;
+unsigned int
+parse_netmask_string(const char *str, bool v6)
+{
+ struct in_addr addr;
+ unsigned int ret;
+ char *err = NULL;
+
+ if (!strchr(str, '.')) {
+ ret = strtoul(str, &err, 0);
+ if (err && *err)
+ goto error;
+
+ return ret;
+ }
+
+ if (v6)
+ goto error;
+
+ if (inet_aton(str, &addr) != 1)
+ goto error;
+
+ return 32 - fls(~(ntohl(addr.s_addr)));
+
+error:
+ return ~0;
+}
+
static bool
-split_netmask(char *str, unsigned int *netmask)
+split_netmask(char *str, unsigned int *netmask, bool v6)
{
- char *delim, *err = NULL;
+ char *delim = strchr(str, '/');
- delim = strchr(str, '/');
if (delim) {
*(delim++) = 0;
- *netmask = strtoul(delim, &err, 10);
- if (err && *err)
- return false;
+ *netmask = parse_netmask_string(delim, v6);
}
return true;
}
char *astr = alloca(strlen(str) + 1);
strcpy(astr, str);
- if (!split_netmask(astr, netmask))
+ if (!split_netmask(astr, netmask, af == AF_INET6))
return 0;
if (af == AF_INET6) {
int interface_proto_event(struct interface_proto_state *proto,
enum interface_proto_cmd cmd, bool force);
struct device_addr *proto_parse_ip_addr_string(const char *str, bool v6, int mask);
+unsigned int parse_netmask_string(const char *str, bool v6);
#endif