Added support of WPA3-SAE security mode.
[platform/upstream/connman.git] / src / 6to4.c
old mode 100644 (file)
new mode 100755 (executable)
index 802a1df..71a2882
@@ -49,6 +49,9 @@ static int tunnel_pending;
 static char *tunnel_ip_address;
 static GWeb *web;
 static guint web_request_id;
+static unsigned int newlink_watch;
+static unsigned int newlink_flags;
+static int newlink_timeout_id;
 
 #define STATUS_URL "http://ipv6.connman.net/online/status.html"
 
@@ -60,7 +63,7 @@ static int tunnel_create(struct in_addr *addr)
 {
        struct ip_tunnel_parm p;
        struct ifreq ifr;
-       int fd = -1;
+       int fd;
        int ret;
 
        /* ip tunnel add tun6to4 mode sit remote any local 1.2.3.4 ttl 64 */
@@ -74,21 +77,23 @@ static int tunnel_create(struct in_addr *addr)
        p.iph.protocol = IPPROTO_IPV6;
        p.iph.saddr = addr->s_addr;
        p.iph.ttl = 64;
-       strncpy(p.name, "tun6to4", IFNAMSIZ);
+       strncpy(p.name, "tun6to4", sizeof(p.name) - 1);
 
-       strncpy(ifr.ifr_name, "sit0", IFNAMSIZ);
+       strncpy(ifr.ifr_name, "sit0", sizeof(ifr.ifr_name) - 1);
        ifr.ifr_ifru.ifru_data = (void *)&p;
        fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
+       if (fd < 0)
+               return -errno;
        ret = ioctl(fd, SIOCADDTUNNEL, &ifr);
        if (ret)
                connman_error("add tunnel %s failed: %s", ifr.ifr_name,
                                                        strerror(errno));
        close(fd);
 
-       return ret;
+       return -ret;
 }
 
-static void tunnel_destroy()
+static void tunnel_destroy(void)
 {
        struct ip_tunnel_parm p;
        struct ifreq ifr;
@@ -106,9 +111,9 @@ static void tunnel_destroy()
        p.iph.version = 4;
        p.iph.ihl = 5;
        p.iph.protocol = IPPROTO_IPV6;
-       strncpy(p.name, "tun6to4", IFNAMSIZ);
+       strncpy(p.name, "tun6to4", sizeof(p.name) - 1);
 
-       strncpy(ifr.ifr_name, "tun6to4", IFNAMSIZ);
+       strncpy(ifr.ifr_name, "tun6to4", sizeof(ifr.ifr_name) - 1);
        ifr.ifr_ifru.ifru_data = (void *)&p;
        fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
        if (fd < 0) {
@@ -130,7 +135,7 @@ static void tunnel_destroy()
        tunnel_ip_address = NULL;
 }
 
-static int tunnel_add_route()
+static int tunnel_add_route(void)
 {
        struct __connman_inet_rtnl_handle rth;
        struct in6_addr addr6;
@@ -228,12 +233,12 @@ static gboolean unref_web(gpointer user_data)
        return FALSE;
 }
 
-static gboolean web_result(GWebResult *result, gpointer user_data)
+static bool web_result(GWebResult *result, gpointer user_data)
 {
        guint16 status;
 
        if (web_request_id == 0)
-               return FALSE;
+               return false;
 
        status = g_web_result_get_status(result);
 
@@ -248,7 +253,7 @@ static gboolean web_result(GWebResult *result, gpointer user_data)
 
        g_timeout_add_seconds(1, unref_web, NULL);
 
-       return FALSE;
+       return false;
 }
 
 static void web_debug(const char *str, void *data)
@@ -256,6 +261,71 @@ static void web_debug(const char *str, void *data)
        connman_info("%s: %s\n", (const char *) data, str);
 }
 
+static gboolean newlink_timeout(gpointer user_data)
+{
+       /*
+        * Stop if the timeout has been cancelled already by tun_newlink()
+        */
+       if (newlink_timeout_id == 0)
+               return FALSE;
+
+       DBG("");
+
+       if (newlink_watch != 0) {
+               connman_rtnl_remove_watch(newlink_watch);
+               newlink_watch = 0;
+       }
+
+       newlink_flags = 0;
+
+       if (web_request_id == 0)
+               tunnel_destroy();
+
+       newlink_timeout_id = 0;
+
+       return FALSE;
+}
+
+static void tun_newlink(unsigned flags, unsigned change, void *user_data)
+{
+       int index = GPOINTER_TO_INT(user_data);
+
+       if ((newlink_flags & IFF_UP) == (flags & IFF_UP)) {
+               newlink_flags = flags;
+               return;
+       }
+
+       if (flags & IFF_UP) {
+               /*
+                * We try to verify that connectivity through tunnel works ok.
+                */
+               if (newlink_timeout_id > 0) {
+                       g_source_remove(newlink_timeout_id);
+                       newlink_timeout_id = 0;
+               }
+
+               web = g_web_new(index);
+               if (!web) {
+                       tunnel_destroy();
+                       return;
+               }
+
+               g_web_set_accept(web, NULL);
+               g_web_set_user_agent(web, "ConnMan/%s", VERSION);
+               g_web_set_close_connection(web, TRUE);
+
+               if (getenv("CONNMAN_WEB_DEBUG"))
+                       g_web_set_debug(web, web_debug, "6to4");
+
+               web_request_id = g_web_request_get(web, STATUS_URL,
+                               web_result, NULL,  NULL);
+
+               newlink_timeout(NULL);
+       }
+
+       newlink_flags = flags;
+}
+
 static int init_6to4(struct in_addr *ip4addr)
 {
        unsigned int a, b, c, d;
@@ -293,20 +363,10 @@ static int init_6to4(struct in_addr *ip4addr)
        if (if_index < 0)
                goto error;
 
-       /* We try to verify that connectivity through tunnel works ok.
-        */
-       web = g_web_new(if_index);
-       if (web == NULL)
-               goto error;
-
-       g_web_set_accept(web, NULL);
-       g_web_set_user_agent(web, "ConnMan/%s", VERSION);
-       g_web_set_close_connection(web, TRUE);
-
-       if (getenv("CONNMAN_WEB_DEBUG"))
-               g_web_set_debug(web, web_debug, "6to4");
+       newlink_watch = connman_rtnl_add_newlink_watch(if_index,
+                               tun_newlink, GINT_TO_POINTER(if_index));
 
-       web_request_id = g_web_request_get(web, STATUS_URL, web_result, NULL);
+       newlink_timeout_id = g_timeout_add_seconds(1, newlink_timeout, NULL);
 
        return 0;
 
@@ -326,7 +386,7 @@ static void receive_rs_reply(struct nd_router_advert *reply,
        /* We try to create tunnel if autoconfiguration did not work i.e.,
         * we did not receive any reply to router solicitation message.
         */
-       if (reply == NULL && inet_aton(address, &ip4addr) != 0)
+       if (!reply && inet_aton(address, &ip4addr) != 0)
                init_6to4(&ip4addr);
 
        g_free(address);
@@ -348,15 +408,15 @@ int __connman_6to4_probe(struct connman_service *service)
        if (tunnel_created || tunnel_pending)
                return 0;
 
-       if (service == NULL)
+       if (!service)
                return -1;
 
        ip4config = __connman_service_get_ip4config(service);
-       if (ip4config == NULL)
+       if (!ip4config)
                return -1;
 
        ip6config = __connman_service_get_ip6config(service);
-       if (ip6config == NULL)
+       if (!ip6config)
                return -1;
 
        method = __connman_ipconfig_get_method(ip6config);
@@ -364,7 +424,7 @@ int __connman_6to4_probe(struct connman_service *service)
                return -1;
 
        address = __connman_ipconfig_get_local(ip4config);
-       if (address == NULL)
+       if (!address)
                return -1;
 
        if (inet_aton(address, &ip4addr) == 0)
@@ -397,11 +457,11 @@ void __connman_6to4_remove(struct connman_ipconfig *ip4config)
 
        DBG("tunnel ip address %s", tunnel_ip_address);
 
-       if (ip4config == NULL)
+       if (!ip4config)
                return;
 
        address = __connman_ipconfig_get_local(ip4config);
-       if (address == NULL)
+       if (!address)
                return;
 
        if (g_strcmp0(address, tunnel_ip_address) != 0)
@@ -415,14 +475,14 @@ int __connman_6to4_check(struct connman_ipconfig *ip4config)
 {
        const char *address;
 
-       if (ip4config == NULL || tunnel_created == 0 ||
+       if (!ip4config || tunnel_created == 0 ||
                                        tunnel_pending == 1)
                return -1;
 
        DBG("tunnel ip address %s", tunnel_ip_address);
 
        address = __connman_ipconfig_get_local(ip4config);
-       if (address == NULL)
+       if (!address)
                return -1;
 
        if (g_strcmp0(address, tunnel_ip_address) == 0)