Rewrite ipip6-tunnel setup to use netlink and add support for FMRs
authorSteven Barth <steven@midlink.org>
Wed, 21 May 2014 19:25:50 +0000 (21:25 +0200)
committerSteven Barth <steven@midlink.org>
Wed, 21 May 2014 19:25:50 +0000 (21:25 +0200)
Signed-off-by: Steven Barth <steven@midlink.org>
system-linux.c
system.c
system.h

index 7a194caec9dd9e6054ae4d2396fff551510bca47..06226ef68fd2824bf8de20332f1adee5620753b6 100644 (file)
@@ -1672,24 +1672,84 @@ int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
                }
 #endif
        } else if (!strcmp(str, "ipip6")) {
-               struct ip6_tnl_parm p = {
-                       .link = link,
-                       .proto = IPPROTO_IPIP,
-                       .hop_limit = (ttl) ? ttl : 64,
-                       .encap_limit = 4,
-               };
+               struct nl_msg *nlm = nlmsg_alloc_simple(RTM_NEWLINK,
+                               NLM_F_REQUEST | NLM_F_REPLACE | NLM_F_CREATE);
 
-               if ((cur = tb[TUNNEL_ATTR_LOCAL]) &&
-                               inet_pton(AF_INET6, blobmsg_data(cur), &p.laddr) < 1)
-                       return -EINVAL;
+               struct ifinfomsg ifi = { .ifi_family = AF_UNSPEC };
+               nlmsg_append(nlm, &ifi, sizeof(ifi), 0);
+               nla_put_string(nlm, IFLA_IFNAME, name);
 
-               if ((cur = tb[TUNNEL_ATTR_REMOTE]) &&
-                               inet_pton(AF_INET6, blobmsg_data(cur), &p.raddr) < 1)
-                       return -EINVAL;
+               if (link)
+                       nla_put_u32(nlm, IFLA_LINK, link);
 
-               strncpy(p.name, name, sizeof(p.name));
-               if (tunnel_ioctl("ip6tnl0", SIOCADDTUNNEL, &p) < 0)
-                       return -1;
+               struct nlattr *linkinfo = nla_nest_start(nlm, IFLA_LINKINFO);
+               nla_put_string(nlm, IFLA_INFO_KIND, "ip6tnl");
+               struct nlattr *infodata = nla_nest_start(nlm, IFLA_INFO_DATA);
+
+               if (link)
+                       nla_put_u32(nlm, IFLA_IPTUN_LINK, link);
+
+               nla_put_u8(nlm, IFLA_IPTUN_PROTO, IPPROTO_IPIP);
+               nla_put_u8(nlm, IFLA_IPTUN_TTL, (ttl) ? ttl : 64);
+               nla_put_u8(nlm, IFLA_IPTUN_ENCAP_LIMIT, 4);
+
+               struct in6_addr in6buf;
+               if ((cur = tb[TUNNEL_ATTR_LOCAL])) {
+                       if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1)
+                               return -EINVAL;
+                       nla_put(nlm, IFLA_IPTUN_LOCAL, sizeof(in6buf), &in6buf);
+               }
+
+               if ((cur = tb[TUNNEL_ATTR_REMOTE])) {
+                       if (inet_pton(AF_INET6, blobmsg_data(cur), &in6buf) < 1)
+                               return -EINVAL;
+                       nla_put(nlm, IFLA_IPTUN_REMOTE, sizeof(in6buf), &in6buf);
+               }
+
+#ifdef IFLA_IPTUN_FMR_MAX
+               if ((cur = tb[TUNNEL_ATTR_FMRS])) {
+                       struct nlattr *fmrs = nla_nest_start(nlm, IFLA_IPTUN_FMRS);
+
+                       struct blob_attr *fmr;
+                       unsigned rem, fmrcnt = 0;
+                       blobmsg_for_each_attr(fmr, cur, rem) {
+                               if (blobmsg_type(fmr) != BLOBMSG_TYPE_STRING)
+                                       continue;
+
+                               unsigned ip4len, ip6len, ealen, offset = 6;
+                               char ip6buf[48];
+                               char ip4buf[16];
+
+                               if (sscanf(blobmsg_get_string(fmr), "%47[^/]/%u,%15[^/]/%u,%u,%u",
+                                               ip6buf, &ip6len, ip4buf, &ip4len, &ealen, &offset) < 5)
+                                       return -EINVAL;
+
+                               struct in6_addr ip6prefix;
+                               struct in_addr ip4prefix;
+                               if (inet_pton(AF_INET6, ip6buf, &ip6prefix) != 1 ||
+                                               inet_pton(AF_INET, ip4buf, &ip4prefix) != 1)
+                                       return -EINVAL;
+
+                               struct nlattr *rule = nla_nest_start(nlm, ++fmrcnt);
+
+                               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
+
+               nla_nest_end(nlm, infodata);
+               nla_nest_end(nlm, linkinfo);
+
+               return system_rtnl_call(nlm);
        } else
                return -EINVAL;
 
index 3dd90b42505b1a4e7d64eee52a33ae3681d07652..8e1fc31c0045a007311ff8eb4f4ff71c539aa070 100644 (file)
--- a/system.c
+++ b/system.c
@@ -25,6 +25,7 @@ static const struct blobmsg_policy tunnel_attrs[__TUNNEL_ATTR_MAX] = {
        [TUNNEL_ATTR_6RD_PREFIX] = {.name =  "6rd-prefix", .type = BLOBMSG_TYPE_STRING },
        [TUNNEL_ATTR_6RD_RELAY_PREFIX] = { .name = "6rd-relay-prefix", .type = BLOBMSG_TYPE_STRING },
        [TUNNEL_ATTR_LINK] = { .name = "link", .type = BLOBMSG_TYPE_STRING },
+       [TUNNEL_ATTR_FMRS] = { .name = "fmrs", .type = BLOBMSG_TYPE_ARRAY },
 };
 
 const struct uci_blob_param_list tunnel_attr_list = {
index 1ad861a79736877cec00466c343e723f4eec9374..a8f12ac9c4ff692ee80c1ff08329ae889ac8586b 100644 (file)
--- a/system.h
+++ b/system.h
@@ -31,6 +31,7 @@ enum tunnel_param {
        TUNNEL_ATTR_6RD_PREFIX,
        TUNNEL_ATTR_6RD_RELAY_PREFIX,
        TUNNEL_ATTR_LINK,
+       TUNNEL_ATTR_FMRS,
        __TUNNEL_ATTR_MAX
 };