system-linux: parse map-e fmrs parameters as nested data json object
authorHans Dedecker <dedeckeh@gmail.com>
Sun, 27 Aug 2017 19:48:28 +0000 (21:48 +0200)
committerHans Dedecker <dedeckeh@gmail.com>
Tue, 29 Aug 2017 19:02:30 +0000 (21:02 +0200)
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 <dedeckeh@gmail.com>
system-linux.c
system.c
system.h

index 0bf071e70d9095360bd27510cabca5067cf7f3a1..6d97a02961c788d30275107816d8dbf298e7111f 100644 (file)
@@ -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
index bf3fd777f013fb360e68cf8c3f07419386b77236..55552725443e899878f5b0d499b20a2b27c84f2b 100644 (file)
--- 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
index a5817ca98dc716215d857a49fa78da50027f2747..61c72c2b92d7fda179d82d9ff8ec1215cd0e1854 100644 (file)
--- 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 */