From 7d94ede3c38dd7b8e08c8a2fd8ae5ede71a2059b Mon Sep 17 00:00:00 2001 From: Hans Dedecker Date: Sun, 27 Aug 2017 21:48:28 +0200 Subject: [PATCH] system-linux: parse map-e fmrs parameters as nested data json object Parse map-e fmrs parameters IPv6 prefix, IPv4 prefix, ealen and offset as array elements nested in a data json object. At the same time remove the now obsolete TUNNEL_ATTR_FMRS tunnel attribute. Signed-off-by: Hans Dedecker --- system-linux.c | 81 ++++++++++++++++++++++++++++++++------------------ system.c | 13 +++++++- system.h | 10 ++++++- 3 files changed, 73 insertions(+), 31 deletions(-) diff --git a/system-linux.c b/system-linux.c index 0bf071e..6d97a02 100644 --- a/system-linux.c +++ b/system-linux.c @@ -2328,45 +2328,68 @@ static int system_add_ip6_tunnel(const char *name, const unsigned int link, } #ifdef IFLA_IPTUN_FMR_MAX - if ((cur = tb[TUNNEL_ATTR_FMRS])) { + if ((cur = tb[TUNNEL_ATTR_DATA])) { struct nlattr *fmrs = nla_nest_start(nlm, IFLA_IPTUN_FMRS); + struct blob_attr *dcur; + unsigned drem, fmrcnt = 0; - struct blob_attr *fmr; - unsigned rem, fmrcnt = 0; - blobmsg_for_each_attr(fmr, cur, rem) { - if (blobmsg_type(fmr) != BLOBMSG_TYPE_STRING) + blobmsg_for_each_attr(dcur, cur, drem) { + if (blobmsg_type(dcur) != BLOBMSG_TYPE_ARRAY || + strcmp(blobmsg_name(dcur), "fmrs") || + blobmsg_check_array(dcur, BLOBMSG_TYPE_UNSPEC) <= 0) continue; - unsigned ip4len, ip6len, ealen, offset = 6; - char ip6buf[48]; - char ip4buf[16]; + struct blob_attr *rcur; + unsigned rrem; + blobmsg_for_each_attr(rcur, dcur, rrem) { + struct blob_attr *tb_fmr[__FMR_DATA_ATTR_MAX], *tb_cur; + struct in6_addr ip6prefix; + struct in_addr ip4prefix; + unsigned ip4len, ip6len, ealen, offset; + + blobmsg_parse(fmr_data_attr_list.params, __FMR_DATA_ATTR_MAX, tb_fmr, + blobmsg_data(rcur), blobmsg_len(rcur)); + + if (!(tb_cur = tb_fmr[FMR_DATA_PREFIX6]) || + !parse_ip_and_netmask(AF_INET6, + blobmsg_data(tb_cur), &ip6prefix, + &ip6len)) { + ret = -EINVAL; + goto failure; + } - if (sscanf(blobmsg_get_string(fmr), "%47[^/]/%u,%15[^/]/%u,%u,%u", - ip6buf, &ip6len, ip4buf, &ip4len, &ealen, &offset) < 5) { - ret = -EINVAL; - goto failure; - } + if (!(tb_cur = tb_fmr[FMR_DATA_PREFIX4]) || + !parse_ip_and_netmask(AF_INET, + blobmsg_data(tb_cur), &ip4prefix, + &ip4len)) { + ret = -EINVAL; + goto failure; + } - struct in6_addr ip6prefix; - struct in_addr ip4prefix; - if (inet_pton(AF_INET6, ip6buf, &ip6prefix) != 1 || - inet_pton(AF_INET, ip4buf, &ip4prefix) != 1) { - ret = -EINVAL; - goto failure; - } + if (!(tb_cur = tb_fmr[FMR_DATA_EALEN])) { + ret = -EINVAL; + goto failure; + } + ealen = blobmsg_get_u32(tb_cur); - struct nlattr *rule = nla_nest_start(nlm, ++fmrcnt); + if (!(tb_cur = tb_fmr[FMR_DATA_OFFSET])) { + ret = -EINVAL; + goto failure; + } + offset = blobmsg_get_u32(tb_cur); - nla_put(nlm, IFLA_IPTUN_FMR_IP6_PREFIX, sizeof(ip6prefix), &ip6prefix); - nla_put(nlm, IFLA_IPTUN_FMR_IP4_PREFIX, sizeof(ip4prefix), &ip4prefix); - nla_put_u8(nlm, IFLA_IPTUN_FMR_IP6_PREFIX_LEN, ip6len); - nla_put_u8(nlm, IFLA_IPTUN_FMR_IP4_PREFIX_LEN, ip4len); - nla_put_u8(nlm, IFLA_IPTUN_FMR_EA_LEN, ealen); - nla_put_u8(nlm, IFLA_IPTUN_FMR_OFFSET, offset); + struct nlattr *rule = nla_nest_start(nlm, ++fmrcnt); - nla_nest_end(nlm, rule); - } + nla_put(nlm, IFLA_IPTUN_FMR_IP6_PREFIX, sizeof(ip6prefix), &ip6prefix); + nla_put(nlm, IFLA_IPTUN_FMR_IP4_PREFIX, sizeof(ip4prefix), &ip4prefix); + nla_put_u8(nlm, IFLA_IPTUN_FMR_IP6_PREFIX_LEN, ip6len); + nla_put_u8(nlm, IFLA_IPTUN_FMR_IP4_PREFIX_LEN, ip4len); + nla_put_u8(nlm, IFLA_IPTUN_FMR_EA_LEN, ealen); + nla_put_u8(nlm, IFLA_IPTUN_FMR_OFFSET, offset); + nla_nest_end(nlm, rule); + } + } nla_nest_end(nlm, fmrs); } #endif diff --git a/system.c b/system.c index bf3fd77..5555272 100644 --- a/system.c +++ b/system.c @@ -24,7 +24,6 @@ static const struct blobmsg_policy tunnel_attrs[__TUNNEL_ATTR_MAX] = { [TUNNEL_ATTR_TTL] = { .name = "ttl", .type = BLOBMSG_TYPE_INT32 }, [TUNNEL_ATTR_TOS] = { .name = "tos", .type = BLOBMSG_TYPE_STRING }, [TUNNEL_ATTR_LINK] = { .name = "link", .type = BLOBMSG_TYPE_STRING }, - [TUNNEL_ATTR_FMRS] = { .name = "fmrs", .type = BLOBMSG_TYPE_ARRAY }, [TUNNEL_ATTR_DATA] = { .name = "data", .type = BLOBMSG_TYPE_TABLE }, }; @@ -78,6 +77,18 @@ const struct uci_blob_param_list sixrd_data_attr_list = { .params = sixrd_data_attrs, }; +static const struct blobmsg_policy fmr_data_attrs[__FMR_DATA_ATTR_MAX] = { + [FMR_DATA_PREFIX6] = { .name = "prefix6", .type = BLOBMSG_TYPE_STRING }, + [FMR_DATA_PREFIX4] = { .name = "prefix4", .type = BLOBMSG_TYPE_STRING }, + [FMR_DATA_EALEN] = { .name = "ealen", .type = BLOBMSG_TYPE_INT32 }, + [FMR_DATA_OFFSET] = { .name = "offset", .type = BLOBMSG_TYPE_INT32 }, +}; + +const struct uci_blob_param_list fmr_data_attr_list = { + .n_params = __FMR_DATA_ATTR_MAX, + .params = fmr_data_attrs, +}; + void system_fd_set_cloexec(int fd) { #ifdef FD_CLOEXEC diff --git a/system.h b/system.h index a5817ca..61c72c2 100644 --- a/system.h +++ b/system.h @@ -31,7 +31,6 @@ enum tunnel_param { TUNNEL_ATTR_TTL, TUNNEL_ATTR_TOS, TUNNEL_ATTR_LINK, - TUNNEL_ATTR_FMRS, TUNNEL_ATTR_DATA, __TUNNEL_ATTR_MAX }; @@ -67,10 +66,19 @@ enum sixrd_data { __SIXRD_DATA_ATTR_MAX }; +enum fmr_data { + FMR_DATA_PREFIX6, + FMR_DATA_PREFIX4, + FMR_DATA_EALEN, + FMR_DATA_OFFSET, + __FMR_DATA_ATTR_MAX +}; + extern const struct uci_blob_param_list vxlan_data_attr_list; extern const struct uci_blob_param_list gre_data_attr_list; extern const struct uci_blob_param_list vti_data_attr_list; extern const struct uci_blob_param_list sixrd_data_attr_list; +extern const struct uci_blob_param_list fmr_data_attr_list; enum bridge_opt { /* stp and forward delay always set */ -- 2.30.2