Fix memory leak in __netconfig_request_netlink_scan()
[platform/core/connectivity/net-config.git] / src / wifi-netlink-scan.c
index d66f22a..3ad72c5 100755 (executable)
@@ -441,7 +441,7 @@ static void __netconfig_found_ap(unsigned char *bss_element, int length, char *s
                                else if (data[i] == ' ' && (i != 0 && i != len -1))
                                        snprintf(&str[i], 2, "%c", ' ');
                                else
-                                       snprintf(&str[i], 3, "%.2x", data[i]);
+                                       snprintf(&str[i], 2, "%c", data[i]);
                        }
                        break;
                }
@@ -488,6 +488,17 @@ 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)
+               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_free(vsie);
+                       return NL_SKIP;
+               }
+       }
+
        /** Create AP info list. */
        if (ssid[0] != '\0') {
                struct bss_scan_info_t *bss_info;
@@ -566,17 +577,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 +602,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 +622,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 +644,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 +672,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 +685,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 +693,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 +748,8 @@ fail:
 
 static int __netconfig_initialize_nl_msg(netconfig_nl_global *global)
 {
+       int rv;
+
        if (global == NULL) {
                DBG("Invalid parameter.");
                return -EINVAL;
@@ -715,7 +763,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;