Added vsie list of APs in netlink scan results
[platform/core/connectivity/net-config.git] / src / wifi-netlink-scan.c
index 5d935c2..5fb87f7 100755 (executable)
 #include <ctype.h>
 
 static GSList *bss_info_list = NULL;
-static unsigned char samsung_oui[3] = {0x00, 0x16, 0x32};
 
 void __netconfig_notify_netlink_scan_done(void)
 {
        GVariantBuilder *builder = NULL;
+       GVariantBuilder *builder1 = NULL;
+       GVariantBuilder *builder2 = NULL;
        GSList* list = NULL;
        const char *prop_ssid = "ssid";
        const char *prop_bssid = "bssid";
        const char *prop_freq = "freq";
        const char *prop_rssi = "rssi";
-       const char *prop_vsie = "vsie";
+       const char *prop_vsie_list = "vsie_list";
        const char *prop_sec = "security";
        const char *prop_enc = "encryption";
 
@@ -54,7 +55,7 @@ void __netconfig_notify_netlink_scan_done(void)
                if (bss_info) {
                        char *bssid = (char *)bss_info->bssid;
                        char *ssid = (char *)bss_info->ssid;
-                       char *vsie = (char *)bss_info->vsie;
+                       GSList *vsie_list = bss_info->vsie_list;
                        int freq = (int)bss_info->freq;
                        int signal = (int)bss_info->signal;
                        int sec_type = (int)bss_info->security_type;
@@ -64,7 +65,31 @@ void __netconfig_notify_netlink_scan_done(void)
                        g_variant_builder_add(builder, "{sv}", prop_bssid, g_variant_new_string(bssid));
                        g_variant_builder_add(builder, "{sv}", prop_freq, g_variant_new_int32(freq));
                        g_variant_builder_add(builder, "{sv}", prop_rssi, g_variant_new_int32(signal));
-                       g_variant_builder_add(builder, "{sv}", prop_vsie, g_variant_new_string(vsie));
+                       builder1 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
+                       GSList *list;
+                       unsigned char *net_vsie;
+                       unsigned int net_vsie_len;
+                       int count;
+                       for (list = vsie_list; list; list = list->next) {
+                               builder2 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
+                               net_vsie = (unsigned char *)list->data;
+                               net_vsie_len = net_vsie[1] + 2;
+
+                               for (count = 0; count < net_vsie_len; count++) {
+                                       g_variant_builder_add(builder2, "y", net_vsie[count]);
+                               }
+
+                               g_variant_builder_add(builder1, "{sv}", "Vsie", g_variant_builder_end(builder2));
+                               g_variant_builder_unref(builder2);
+                       }
+                       g_variant_builder_add(builder, "{sv}", prop_vsie_list, g_variant_builder_end(builder1));
+                       g_variant_builder_unref(builder1);
+
+                       if (vsie_list != NULL)
+                               g_slist_free_full(vsie_list, g_free);
+
+                       vsie_list = NULL;
+
                        g_variant_builder_add(builder, "{sv}", prop_sec, g_variant_new_int32(sec_type));
                        g_variant_builder_add(builder, "{sv}", prop_enc, g_variant_new_int32(enc_type));
                }
@@ -386,42 +411,30 @@ static void __netconfig_get_security(unsigned char *bss_element, int length, wif
        return;
 }
 
-static void __netconfig_get_vsie(unsigned char *bss_element, int length, char **dst)
+static void __netconfig_get_vsie(unsigned char *bss_element, int length, GSList **dst)
 {
-       int i = 0;
-       uint8_t len = 0;
-       gboolean vsie_found = FALSE;
+       unsigned char *vsie;
+       int vsie_len = 0;
 
        if (length < 3) {
                DBG("Vendor specific data not available");
                return;
        }
 
-       /** Check for vendor specific information element */
-       for (i = 0; i < length; i++) {
-               if (bss_element[i] == 221) {
-                       len = bss_element[i+1];
-                       vsie_found = TRUE;
-                       goto out;
-               }
-       }
-out:
-       if (vsie_found && memcmp(bss_element+i+2, samsung_oui, 3) == 0) {
-               DBG("Vendor Specific IE found, len: %d", len);
-               *dst = g_try_malloc0(2*(len+2) + 1);
-               if (*dst == NULL) {
-                       DBG("Failed to allocate memory");
-                       return;
-               }
-               char *buf = (*dst);
-               int j = 0;
-
-               for (j = i; j <= (i + len + 1); j++) {
-                       snprintf(buf, 3, "%02x", bss_element[j]);
-                       buf += 2;
+       while (length >= 2 && length >= bss_element[1]) {
+               if (bss_element[0] == NETCONFIG_VENDOR_SPECIFIC_ID) {
+                       vsie_len = bss_element[1]+2;
+                       vsie = (unsigned char *)g_try_malloc0(vsie_len);
+
+                       if (vsie) {
+                               memcpy(vsie, bss_element, vsie_len);
+                               *dst = g_slist_append(*dst, vsie);
+                       } else
+                               DBG("Failed to allocate memory");
                }
 
-               vsie_found = FALSE;
+               length -= bss_element[1] + 2;
+               bss_element += bss_element[1] + 2;
        }
 }
 
@@ -458,7 +471,7 @@ static int __netconfig_netlink_scan_cb(struct nl_msg *msg, void *user_data)
        char ssid[NETCONFIG_SSID_LEN+1] = {0, };
        wifi_security_type_e sec_type = WIFI_SECURITY_TYPE_NONE;
        wifi_encryption_type_e enc_type = WIFI_ENCRYPTION_TYPE_NONE;
-       char *vsie = NULL;
+       GSList *vsie = NULL;
        struct nlattr *tb[NL80211_ATTR_MAX + 1];
        struct nlattr *bss[NL80211_BSS_MAX + 1];
        struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
@@ -488,6 +501,19 @@ static int __netconfig_netlink_scan_cb(struct nl_msg *msg, void *user_data)
        __netconfig_get_security(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
                        nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), &sec_type, &enc_type);
 
+       if (sec_type == WIFI_SECURITY_TYPE_EAP) {
+               g_slist_free_full(vsie, g_free);
+               return NL_SKIP;
+       }
+       for (GSList *list = bss_info_list; list != NULL; list = list->next) {
+               struct bss_scan_info_t *bss_info = (struct bss_scan_info_t *)list->data;
+               if ((g_strcmp0(bss_info->ssid, ssid) == 0) && (bss_info->security_type == sec_type)
+                               && (bss_info->encryption_type == enc_type)) {
+                       g_slist_free_full(vsie, g_free);
+                       return NL_SKIP;
+               }
+       }
+
        /** Create AP info list. */
        if (ssid[0] != '\0') {
                struct bss_scan_info_t *bss_info;
@@ -495,17 +521,13 @@ static int __netconfig_netlink_scan_cb(struct nl_msg *msg, void *user_data)
 
                bss_info = g_try_new0(struct bss_scan_info_t, 1);
                if (bss_info == NULL) {
-                       g_free(vsie);
+                       g_slist_free_full(vsie, g_free);
                        return NL_SKIP;
                }
 
                g_strlcpy(bss_info->bssid, bssid, strlen(bssid)+1);
                g_strlcpy(bss_info->ssid, ssid, strlen(ssid)+1);
-               if (vsie) {
-                       g_strlcpy(bss_info->vsie, vsie, strlen(vsie)+1);
-                       g_free(vsie);
-                       vsie = NULL;
-               }
+               bss_info->vsie_list = vsie;
                bss_info->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
 
                if (bss[NL80211_BSS_SIGNAL_MBM]) {
@@ -516,18 +538,18 @@ static int __netconfig_netlink_scan_cb(struct nl_msg *msg, void *user_data)
 
                bss_info->security_type = sec_type;
                bss_info->encryption_type = enc_type;
-               DBG("%s %d %d %s %d %d [vsie: %s]", bss_info->bssid, bss_info->freq,
+               DBG("%s %d %d %s %d %d ", bss_info->bssid, bss_info->freq,
                                bss_info->signal, bss_info->ssid, bss_info->security_type,
-                               bss_info->encryption_type, bss_info->vsie);
+                               bss_info->encryption_type);
 
                if (bss_info->ssid[0] == '\0')
                        g_free(bss_info);
                else
                        bss_info_list = g_slist_append(bss_info_list, bss_info);
 
-       }
+       } else
+               g_slist_free_full(vsie, g_free);
 
-       g_free(vsie);
        return NL_SKIP;
 }
 
@@ -566,17 +588,24 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
        gchar *key;
        gboolean ssid_found = FALSE;
        int mcid = __netconfig_get_multicast_id(socket, "nl80211", "scan");
-       nl_socket_add_membership(socket, mcid);
+
+       ret = nl_socket_add_membership(socket, mcid);
+       if (ret < 0) {
+               DBG("Failed to add membership, error: (%s)", nl_geterror(-ret));
+               return ret;
+       }
 
        msg = nlmsg_alloc();
        if (!msg) {
                DBG("Failed to allocate msg");
+               nl_socket_drop_membership(socket, mcid);
                return -ENOMEM;
        }
        ssids = nlmsg_alloc();
        if (!ssids) {
                DBG("Failed to allocate ssids");
                nlmsg_free(msg);
+               nl_socket_drop_membership(socket, mcid);
                return -ENOMEM;
        }
        cb = nl_cb_alloc(NL_CB_DEFAULT);
@@ -584,12 +613,17 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
                DBG("Failed to allocate callbacks");
                nlmsg_free(msg);
                nlmsg_free(ssids);
+               nl_socket_drop_membership(socket, mcid);
                return -ENOMEM;
        }
 
        /** Set nl message and callback functions. */
        genlmsg_put(msg, 0, 0, id, 0, 0, NL80211_CMD_TRIGGER_SCAN, 0);
-       nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index);
+       ret = nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index);
+       if (ret < 0) {
+               DBG("Failed to add integer attribute to netlink message, error: (%s)", nl_geterror(-ret));
+               goto out;
+       }
 
        g_variant_get(params, "a{sv}", &iter);
        while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
@@ -599,8 +633,13 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
                                ssid_found = TRUE;
                                DBG("ssid [%s]", ssid);
 
-                               nla_put(ssids, 1, strlen(ssid), ssid);
+                               ret = nla_put(ssids, 1, strlen(ssid), ssid);
                                g_free(ssid);
+                               if (ret < 0) {
+                                       DBG("Failed to add ssid to netlink message, error: (%s)", nl_geterror(-ret));
+                                       g_variant_iter_free(iter);
+                                       goto out;
+                               }
                        }
                } else if (g_strcmp0(key, "VSIE") == 0) {
                        if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
@@ -616,15 +655,23 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
        }
        g_variant_iter_free(iter);
 
-       if (!ssid_found)
-               nla_put(ssids, 1, 0, "");
+       if (!ssid_found) {
+               ret = nla_put(ssids, 1, 0, "");
+               if (ret < 0) {
+                       DBG("nla_put error: (%s)", nl_geterror(-ret));
+                       goto out;
+               }
+       }
        nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
-       nlmsg_free(ssids);
 
        if (ies[0] == NETCONFIG_VENDOR_SPECIFIC_ID && ies[1] >= 4) {
                DBG("ies_len: %d ies: %02x %02x %02x %02x %02x %02x %02x", ies_len,
                                ies[0], ies[1], ies[2], ies[3], ies[4], ies[5], ies[6]);
-               nla_put(msg, NL80211_ATTR_IE, ies_len, ies);
+               ret = nla_put(msg, NL80211_ATTR_IE, ies_len, ies);
+               if (ret < 0) {
+                       DBG("Failed to add vsie data to netlink message, error: (%s)", nl_geterror(-ret));
+                       goto out;
+               }
        }
 
        err = 1;
@@ -636,6 +683,11 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
 
        /** Send NL80211_CMD_TRIGGER_SCAN to start the scan. */
        ret = nl_send_auto_complete(socket, msg);
+       if (ret < 0) {
+               DBG("nl_send_auto_complete() error: (%s)", nl_geterror(-ret));
+               goto out;
+       }
+
        DBG("Sent %d bytes to the kernel", ret);
        ssid_found = FALSE;
 
@@ -644,8 +696,7 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
 
        if (ret < 0) {
                DBG("nl_recvmsgs() ret: %d (%s)", ret, nl_geterror(-ret));
-               nl_cb_put(cb);
-               return ret;
+               goto out;
        }
 
        while (!results.done)
@@ -653,14 +704,20 @@ static int __netconfig_request_netlink_scan(struct nl_sock *socket,
 
        if (results.aborted) {
                DBG("scan aborted");
-               return 1;
+               goto out;
        }
-       DBG("Scan done");
 
+out:
        /** Release memory */
+       nlmsg_free(ssids);
        nlmsg_free(msg);
        nl_cb_put(cb);
        nl_socket_drop_membership(socket, mcid);
+
+       if (ret < 0 || ret == 1)
+               return ret;
+
+       DBG("Scan done");
        return 0;
 }
 
@@ -702,6 +759,8 @@ fail:
 
 static int __netconfig_initialize_nl_msg(netconfig_nl_global *global)
 {
+       int rv;
+
        if (global == NULL) {
                DBG("Invalid parameter.");
                return -EINVAL;
@@ -715,7 +774,12 @@ static int __netconfig_initialize_nl_msg(netconfig_nl_global *global)
 
        /* Set command into message */
        genlmsg_put(global->msg, 0, 0, global->id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0);
-       nla_put_u32(global->msg, NL80211_ATTR_IFINDEX, global->if_index);
+       rv = nla_put_u32(global->msg, NL80211_ATTR_IFINDEX, global->if_index);
+       if (rv < 0) {
+               DBG("Failed to add integer attribute to netlink message.");
+               nlmsg_free(global->msg);
+               return rv;
+       }
        nl_socket_modify_cb(global->socket, NL_CB_VALID, NL_CB_CUSTOM, __netconfig_netlink_scan_cb, NULL);
 
        return 0;