+ if ((cur = tb[TUNNEL_ATTR_TTL]))
+ p.iph.ttl = blobmsg_get_u32(cur);
+
+ if ((cur = tb[TUNNEL_ATTR_TOS])) {
+ char *str = blobmsg_get_string(cur);
+ if (strcmp(str, "inherit")) {
+ unsigned uval;
+
+ if (!system_tos_aton(str, &uval))
+ return -EINVAL;
+
+ p.iph.tos = uval;
+ } else
+ p.iph.tos = 1;
+ }
+
+ p.iph.frag_off = set_df ? htons(IP_DF) : 0;
+ /* ttl !=0 and nopmtudisc are incompatible */
+ if (p.iph.ttl && p.iph.frag_off == 0)
+ return -EINVAL;
+
+ strncpy(p.name, name, sizeof(p.name));
+
+ switch (p.iph.protocol) {
+ case IPPROTO_IPIP:
+ return tunnel_ioctl("tunl0", SIOCADDTUNNEL, &p);
+ case IPPROTO_IPV6:
+ return tunnel_ioctl("sit0", SIOCADDTUNNEL, &p);
+ default:
+ break;
+ }
+ return -1;
+}
+
+static int __system_del_ip_tunnel(const char *name, struct blob_attr **tb)
+{
+ struct blob_attr *cur;
+ const char *str;
+
+ if (!(cur = tb[TUNNEL_ATTR_TYPE]))
+ return -EINVAL;
+ str = blobmsg_data(cur);
+
+ if (!strcmp(str, "greip") || !strcmp(str, "gretapip") ||
+ !strcmp(str, "greip6") || !strcmp(str, "gretapip6") ||
+ !strcmp(str, "vtiip") || !strcmp(str, "vtiip6") ||
+ !strcmp(str, "vxlan") || !strcmp(str, "vxlan6"))
+ return system_link_del(name);
+ else
+ return tunnel_ioctl(name, SIOCDELTUNNEL, NULL);
+}
+
+int system_del_ip_tunnel(const char *name, struct blob_attr *attr)
+{
+ struct blob_attr *tb[__TUNNEL_ATTR_MAX];
+
+ blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
+ blob_data(attr), blob_len(attr));
+
+ return __system_del_ip_tunnel(name, tb);
+}
+
+int system_update_ipv6_mtu(struct device *dev, int mtu)
+{
+ int ret = -1;
+ char buf[64];
+ int fd;
+
+ snprintf(buf, sizeof(buf), "/proc/sys/net/ipv6/conf/%s/mtu",
+ dev->ifname);
+
+ fd = open(buf, O_RDWR);
+ if (fd < 0)
+ return ret;
+
+ if (!mtu) {
+ ssize_t len = read(fd, buf, sizeof(buf) - 1);
+ if (len < 0)
+ goto out;
+
+ buf[len] = 0;
+ ret = atoi(buf);
+ } else {
+ if (write(fd, buf, snprintf(buf, sizeof(buf), "%i", mtu)) > 0)
+ ret = mtu;
+ }
+
+out:
+ close(fd);
+ return ret;
+}
+
+int system_add_ip_tunnel(const char *name, struct blob_attr *attr)
+{
+ struct blob_attr *tb[__TUNNEL_ATTR_MAX];
+ struct blob_attr *cur;
+ const char *str;
+
+ blobmsg_parse(tunnel_attr_list.params, __TUNNEL_ATTR_MAX, tb,
+ blob_data(attr), blob_len(attr));
+
+ __system_del_ip_tunnel(name, tb);
+
+ if (!(cur = tb[TUNNEL_ATTR_TYPE]))
+ return -EINVAL;
+ str = blobmsg_data(cur);
+
+ unsigned int ttl = 0;
+ if ((cur = tb[TUNNEL_ATTR_TTL])) {
+ ttl = blobmsg_get_u32(cur);
+ if (ttl > 255)
+ return -EINVAL;
+ }
+
+ unsigned int link = 0;
+ if ((cur = tb[TUNNEL_ATTR_LINK])) {
+ struct interface *iface = vlist_find(&interfaces, blobmsg_data(cur), iface, node);
+ if (!iface)
+ return -EINVAL;
+
+ if (iface->l3_dev.dev)
+ link = iface->l3_dev.dev->ifindex;
+ }
+
+ if (!strcmp(str, "sit"))
+ return system_add_sit_tunnel(name, link, tb);
+#ifdef IFLA_IPTUN_MAX
+ else if (!strcmp(str, "ipip6")) {
+ return system_add_ip6_tunnel(name, link, tb);
+ } else if (!strcmp(str, "greip")) {
+ return system_add_gre_tunnel(name, "gre", link, tb, false);
+ } else if (!strcmp(str, "gretapip")) {
+ return system_add_gre_tunnel(name, "gretap", link, tb, false);
+ } else if (!strcmp(str, "greip6")) {
+ return system_add_gre_tunnel(name, "ip6gre", link, tb, true);
+ } else if (!strcmp(str, "gretapip6")) {
+ return system_add_gre_tunnel(name, "ip6gretap", link, tb, true);
+#ifdef IFLA_VTI_MAX
+ } else if (!strcmp(str, "vtiip")) {
+ return system_add_vti_tunnel(name, "vti", link, tb, false);
+ } else if (!strcmp(str, "vtiip6")) {
+ return system_add_vti_tunnel(name, "vti6", link, tb, true);
+#endif
+#ifdef IFLA_VXLAN_MAX
+ } else if(!strcmp(str, "vxlan")) {
+ return system_add_vxlan(name, link, tb, false);
+ } else if(!strcmp(str, "vxlan6")) {
+ return system_add_vxlan(name, link, tb, true);
+#endif
+#endif
+ } else if (!strcmp(str, "ipip")) {
+ return system_add_proto_tunnel(name, IPPROTO_IPIP, link, tb);
+ }
+ else