2 * Network Configuration Module
4 * Copyright (c) 2000 - 2012 Samsung Electronics Co., Ltd. All rights reserved.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
22 #include "netsupplicant.h"
25 #include "wifi-config.h"
26 #include "wifi-netlink-scan.h"
27 #include <netlink/genl/genl.h>
28 #include <netlink/genl/family.h>
29 #include <netlink/genl/ctrl.h>
30 #include <netlink/msg.h>
31 #include <netlink/attr.h>
32 #include <netlink/netlink.h>
35 static GSList *bss_info_list = NULL;
37 static gint __netconfig_compare_bss_by_rssi(gconstpointer a, gconstpointer b)
39 struct bss_scan_info_t *entry_a = (struct bss_scan_info_t *) a;
40 struct bss_scan_info_t *entry_b = (struct bss_scan_info_t *) b;
42 if (entry_a->signal > entry_b->signal)
45 if (entry_a->signal < entry_b->signal)
51 void __netconfig_notify_netlink_scan_done(const char *interface_name)
53 GVariantBuilder *builder = NULL;
54 GVariantBuilder *builder1 = NULL;
55 GVariantBuilder *builder2 = NULL;
57 const char *prop_ssid = "ssid";
58 const char *prop_bssid = "bssid";
59 const char *prop_freq = "freq";
60 const char *prop_rssi = "rssi";
61 const char *prop_vsie_list = "vsie_list";
62 const char *prop_sec = "security";
63 const char *prop_enc = "encryption";
64 const char *prop_pmf = "pmf";
66 bss_info_list = g_slist_sort(bss_info_list, __netconfig_compare_bss_by_rssi);
68 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
69 for (list = bss_info_list; list != NULL; list = list->next) {
70 struct bss_scan_info_t *bss_info = (struct bss_scan_info_t *)list->data;
73 char *bssid = (char *)bss_info->bssid;
74 char *ssid = (char *)bss_info->ssid;
75 GSList *vsie_list = bss_info->vsie_list;
76 int freq = (int)bss_info->freq;
77 int signal = (int)bss_info->signal;
78 int sec_type = (int)bss_info->security_type;
79 int enc_type = (int)bss_info->encryption_type;
80 int pmf_required = (int)bss_info->pmf_required;
82 g_variant_builder_add(builder, "{sv}", prop_ssid, g_variant_new_string(ssid));
83 g_variant_builder_add(builder, "{sv}", prop_bssid, g_variant_new_string(bssid));
84 g_variant_builder_add(builder, "{sv}", prop_freq, g_variant_new_int32(freq));
85 g_variant_builder_add(builder, "{sv}", prop_rssi, g_variant_new_int32(signal));
86 builder1 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
88 unsigned char *net_vsie;
89 unsigned int net_vsie_len;
91 for (list = vsie_list; list; list = list->next) {
92 builder2 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
93 net_vsie = (unsigned char *)list->data;
94 net_vsie_len = net_vsie[1] + 2;
96 for (count = 0; count < net_vsie_len; count++) {
97 g_variant_builder_add(builder2, "y", net_vsie[count]);
100 g_variant_builder_add(builder1, "{sv}", "Vsie", g_variant_builder_end(builder2));
101 g_variant_builder_unref(builder2);
103 g_variant_builder_add(builder, "{sv}", prop_vsie_list, g_variant_builder_end(builder1));
104 g_variant_builder_unref(builder1);
106 if (vsie_list != NULL)
107 g_slist_free_full(vsie_list, g_free);
111 g_variant_builder_add(builder, "{sv}", prop_sec, g_variant_new_int32(sec_type));
112 g_variant_builder_add(builder, "{sv}", prop_enc, g_variant_new_int32(enc_type));
113 g_variant_builder_add(builder, "{sv}", prop_pmf, g_variant_new_int32(pmf_required));
117 wifi_emit_netlink_scan_completed((Wifi *)get_wifi_object(),
118 interface_name, g_variant_builder_end(builder));
119 g_variant_builder_unref(builder);
121 if (bss_info_list != NULL)
122 g_slist_free_full(bss_info_list, g_free);
124 bss_info_list = NULL;
125 INFO("NetlinkScanCompleted");
130 static int ack_handler(struct nl_msg *msg, void *user_data)
132 int *err = user_data;
138 static int finish_handler(struct nl_msg *msg, void *user_data)
140 int *ret = user_data;
147 static int error_handler(struct sockaddr_nl *nla, struct nlmsgerr *err,
150 int *ret = user_data;
157 static int no_seq_check(struct nl_msg *msg, void *user_data)
163 static int __netconfig_family_handler(struct nl_msg *msg, void *user_data)
165 /** Callback for NL_CB_VALID in multicast group */
166 struct netconfig_netlink_scan_handler_args *grp = user_data;
167 struct nlattr *tb[CTRL_ATTR_MAX + 1];
168 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
169 struct nlattr *mc_grp;
172 nla_parse(tb, CTRL_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
174 if (!tb[CTRL_ATTR_MCAST_GROUPS])
177 nla_for_each_nested(mc_grp, tb[CTRL_ATTR_MCAST_GROUPS], rem_mc_grp) {
178 struct nlattr *tb_mc_grp[CTRL_ATTR_MCAST_GRP_MAX + 1];
180 nla_parse(tb_mc_grp, CTRL_ATTR_MCAST_GRP_MAX, nla_data(mc_grp), nla_len(mc_grp), NULL);
182 if (!tb_mc_grp[CTRL_ATTR_MCAST_GRP_NAME] || !tb_mc_grp[CTRL_ATTR_MCAST_GRP_ID])
184 if (strncmp(nla_data(tb_mc_grp[CTRL_ATTR_MCAST_GRP_NAME]), grp->group,
185 nla_len(tb_mc_grp[CTRL_ATTR_MCAST_GRP_NAME])))
188 grp->id = nla_get_u32(tb_mc_grp[CTRL_ATTR_MCAST_GRP_ID]);
195 static int __netconfig_get_multicast_id(struct nl_sock *socket, const char *family, const char *group)
197 struct nl_msg *msg = NULL;
198 struct nl_cb *cb = NULL;
200 struct netconfig_netlink_scan_handler_args grp = { .group = group, .id = -ENOENT, };
206 cb = nl_cb_alloc(NL_CB_DEFAULT);
212 ctrl_id = genl_ctrl_resolve(socket, "nlctrl");
214 genlmsg_put(msg, 0, 0, ctrl_id, 0, 0, CTRL_CMD_GETFAMILY, 0);
217 NLA_PUT_STRING(msg, CTRL_ATTR_FAMILY_NAME, family);
219 ret = nl_send_auto_complete(socket, msg);
225 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &ret);
226 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &ret);
227 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, __netconfig_family_handler, &grp);
230 nl_recvmsgs(socket, cb);
243 static void __netconfig_macaddress_str(char *bssid, unsigned char *user_data)
247 for (i = 0; i < 6; i++) {
249 snprintf(bssid, 3, "%02x", user_data[i]);
252 snprintf(bssid, 4, ":%02x", user_data[i]);
259 WIFI_SECURITY_TYPE_NONE = 0,
260 WIFI_SECURITY_TYPE_WEP = 1,
261 WIFI_SECURITY_TYPE_WPA_PSK = 2,
262 WIFI_SECURITY_TYPE_WPA2_PSK = 3,
263 WIFI_SECURITY_TYPE_SAE = 4,
264 WIFI_SECURITY_TYPE_EAP = 5,
265 } wifi_security_type_e;
268 WIFI_ENCRYPTION_TYPE_NONE = 0,
269 WIFI_ENCRYPTION_TYPE_WEP = 1,
270 WIFI_ENCRYPTION_TYPE_TKIP = 2,
271 WIFI_ENCRYPTION_TYPE_AES = 3,
272 WIFI_ENCRYPTION_TYPE_TKIP_AES_MIXED = 4,
273 } wifi_encryption_type_e;
275 static unsigned char ms_oui[3] = { 0x00, 0x50, 0xf2 };
276 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
278 static void __netconfig_get_security(unsigned char *bss_element, int length,
279 wifi_security_type_e *sec_type, wifi_encryption_type_e *enc_type, int *pmf_required)
286 gboolean ieee80211_psk = false;
288 *sec_type = WIFI_SECURITY_TYPE_NONE;
289 *enc_type = WIFI_ENCRYPTION_TYPE_NONE;
291 while (length >= 2 && length >= bss_element[1]) {
292 if (bss_element[0] == 221 && (bss_element[1] >= 4 && memcmp(bss_element + 2, ms_oui, 3) == 0)) {
293 data = bss_element + 2 + 4 + 2 + 4;
294 len = bss_element[1] - 4 - 2 - 4;
297 length -= bss_element[1] + 2;
298 bss_element += bss_element[1] + 2;
302 count = data[0] | (data[1] << 8);
304 if (2 + (count * 4) > len) {
305 length -= bss_element[1] + 2;
306 bss_element += bss_element[1] + 2;
310 for (i = 0; i < count; i++) {
311 t_data = (data + 2 + (i * 4));
313 if ((memcmp(t_data, ms_oui, 3) == 0) || (memcmp(t_data, ieee80211_oui, 3) == 0)) {
314 if (t_data[3] == 1 || t_data[3] == 5) { // 1 : WEP-40, 5 : WEP-104
315 *sec_type = WIFI_SECURITY_TYPE_WEP;
316 *enc_type = WIFI_ENCRYPTION_TYPE_WEP;
318 } else if (t_data[3] == 2) { // 2 : TKIP
319 if (*sec_type != WIFI_SECURITY_TYPE_WPA2_PSK)
320 *sec_type = WIFI_SECURITY_TYPE_WPA_PSK;
321 if (*enc_type == WIFI_ENCRYPTION_TYPE_AES)
322 *enc_type = WIFI_ENCRYPTION_TYPE_TKIP_AES_MIXED;
324 *enc_type = WIFI_ENCRYPTION_TYPE_TKIP;
325 } else if (t_data[3] == 4) { // 3 : CCMP
326 if (*sec_type != WIFI_SECURITY_TYPE_WPA2_PSK)
327 *sec_type = WIFI_SECURITY_TYPE_WPA_PSK;
328 if (*enc_type == WIFI_ENCRYPTION_TYPE_TKIP)
329 *enc_type = WIFI_ENCRYPTION_TYPE_TKIP_AES_MIXED;
331 *enc_type = WIFI_ENCRYPTION_TYPE_AES;
336 data += 2 + (count * 4);
337 len -= 2 + (count * 4);
340 length -= bss_element[1] + 2;
341 bss_element += bss_element[1] + 2;
345 count = data[0] | (data[1] << 8);
347 if (2 + (count * 4) > len) {
348 length -= bss_element[1] + 2;
349 bss_element += bss_element[1] + 2;
353 for (i = 0; i < count; i++) {
354 t_data = (data + 2 + (i * 4));
356 if ((memcmp(t_data, ms_oui, 3) == 0) || (memcmp(t_data, ieee80211_oui, 3) == 0)) {
357 if (t_data[3] == 1 || t_data[3] == 3 || t_data[3] == 5) { // 1 : IEEE 802.1X, 3 : FT/IEEE 802.1X, 5 : IEEE 802.1X/SHA-256
358 *sec_type = WIFI_SECURITY_TYPE_EAP;
359 } else if (t_data[3] == 2 || t_data[3] == 4 || t_data[3] == 6) { // 2 : PSK, 4 : FT/PSK, 6 : PSK/SHA-256
362 if (*sec_type != WIFI_SECURITY_TYPE_WPA2_PSK)
363 *sec_type = WIFI_SECURITY_TYPE_WPA_PSK;
367 } else if (bss_element[0] == 48 && (bss_element[1] >= 2 && bss_element[1] <= 255)) {
368 data = bss_element + 2 + 2 + 4;
369 len = bss_element[1] - 2 - 4;
372 length -= bss_element[1] + 2;
373 bss_element += bss_element[1] + 2;
377 count = data[0] | (data[1] << 8);
378 if (2 + (count + 4) > len) {
379 length -= bss_element[1] + 2;
380 bss_element += bss_element[1] + 2;
384 for (i = 0; i < count; i++) {
385 t_data = (data + 2 + (i * 4));
387 if ((memcmp(t_data, ms_oui, 3) == 0) || (memcmp(t_data, ieee80211_oui, 3) == 0)) {
388 if (t_data[3] == 1 || t_data[3] == 5) { // 1 : WEP-40, 5 : WEP-104
389 *sec_type = WIFI_SECURITY_TYPE_WEP;
390 *enc_type = WIFI_ENCRYPTION_TYPE_WEP;
392 } else if (t_data[3] == 2) { // 2 : TKIP
393 *sec_type = WIFI_SECURITY_TYPE_WPA2_PSK;
394 if (*enc_type == WIFI_ENCRYPTION_TYPE_AES)
395 *enc_type = WIFI_ENCRYPTION_TYPE_TKIP_AES_MIXED;
397 *enc_type = WIFI_ENCRYPTION_TYPE_TKIP;
398 } else if (t_data[3] == 4) { // 3 : CCMP
399 *sec_type = WIFI_SECURITY_TYPE_WPA2_PSK;
400 if (*enc_type == WIFI_ENCRYPTION_TYPE_TKIP)
401 *enc_type = WIFI_ENCRYPTION_TYPE_TKIP_AES_MIXED;
403 *enc_type = WIFI_ENCRYPTION_TYPE_AES;
408 data += 2 + (count * 4);
409 len -= 2 + (count * 4);
412 length -= bss_element[1] + 2;
413 bss_element += bss_element[1] + 2;
417 count = data[0] | (data[1] << 8);
418 if (2 + (count * 4) > len) {
419 length -= bss_element[1] + 2;
420 bss_element += bss_element[1] + 2;
424 for (i = 0; i < count; i++) {
425 t_data = (data + 2 + (i * 4));
427 if ((memcmp(t_data, ms_oui, 3) == 0) || (memcmp(t_data, ieee80211_oui, 3) == 0)) {
428 if (t_data[3] == 1 || t_data[3] == 3 || t_data[3] == 5) { // 1 : IEEE 802.1X, 3 : FT/IEEE 802.1X, 5 : IEEE 802.1X/SHA-256
429 *sec_type = WIFI_SECURITY_TYPE_EAP;
430 } else if (t_data[3] == 2 || t_data[3] == 4 || t_data[3] == 6) { // 2 : PSK, 4 : FT/PSK, 6 : PSK/SHA-256
433 *sec_type = WIFI_SECURITY_TYPE_WPA2_PSK;
434 ieee80211_psk = true;
435 } else if (t_data[3] == 8) { // Add SAE security type for netlink scan
436 if (ieee80211_psk != true)
437 *sec_type = WIFI_SECURITY_TYPE_SAE;
443 length -= bss_element[1] + 2;
444 bss_element += bss_element[1] + 2;
450 static void __netconfig_get_vsie(unsigned char *bss_element, int length, GSList **dst)
456 DBG("Vendor specific data not available");
460 while (length >= 2 && length >= bss_element[1]) {
461 if (bss_element[0] == NETCONFIG_VENDOR_SPECIFIC_ID) {
462 vsie_len = bss_element[1]+2;
463 vsie = (unsigned char *)g_try_malloc0(vsie_len);
466 memcpy(vsie, bss_element, vsie_len);
467 *dst = g_slist_append(*dst, vsie);
469 DBG("Failed to allocate memory");
472 length -= bss_element[1] + 2;
473 bss_element += bss_element[1] + 2;
477 static void __netconfig_found_ap(unsigned char *bss_element, int length, char *str)
483 while (length >= 2 && length >= bss_element[1]) {
484 if (bss_element[0] == 0 && bss_element[1] <= 32) {
485 len = bss_element[1];
486 data = bss_element + 2;
487 for (i = 0; i < len; i++) {
488 if (isprint(data[i]) && data[i] != ' ' && data[i] != '\\')
489 snprintf(&str[i], 2, "%c", data[i]);
490 else if (data[i] == ' ' && (i != 0 && i != len -1))
491 snprintf(&str[i], 2, "%c", ' ');
493 snprintf(&str[i], 2, "%c", data[i]);
497 length -= bss_element[1] + 2;
498 bss_element += bss_element[1] + 2;
502 static int __netconfig_netlink_scan_cb(struct nl_msg *msg, void *user_data)
504 /** Called by the kernel with a dump of the successful scan's data. Called for each SSID. */
505 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
506 char bssid[NETCONFIG_BSSID_LEN+1];
507 char ssid[NETCONFIG_SSID_LEN+1] = {0, };
508 wifi_security_type_e sec_type = WIFI_SECURITY_TYPE_NONE;
509 wifi_encryption_type_e enc_type = WIFI_ENCRYPTION_TYPE_NONE;
512 struct nlattr *tb[NL80211_ATTR_MAX + 1];
513 struct nlattr *bss[NL80211_BSS_MAX + 1];
514 struct nla_policy bss_policy[NL80211_BSS_MAX + 1] = {
515 [NL80211_BSS_FREQUENCY] = {.type = NLA_U32},
516 [NL80211_BSS_BSSID] = { },
517 [NL80211_BSS_CAPABILITY] = {.type = NLA_U16},
518 [NL80211_BSS_INFORMATION_ELEMENTS] = { },
519 [NL80211_BSS_SIGNAL_MBM] = {.type = NLA_U32},
524 /** Parse nl message and check error. */
525 nla_parse(tb, NL80211_ATTR_MAX, genlmsg_attrdata(gnlh, 0), genlmsg_attrlen(gnlh, 0), NULL);
526 if (!tb[NL80211_ATTR_BSS]) {
527 DBG("BSS info not available");
530 if (nla_parse_nested(bss, NL80211_BSS_MAX, tb[NL80211_ATTR_BSS], bss_policy)) {
531 DBG("Failed to parse nested attributes");
534 if ((!bss[NL80211_BSS_BSSID]) || (!bss[NL80211_BSS_INFORMATION_ELEMENTS]))
537 /** Extract BSSID and AP info. */
538 __netconfig_macaddress_str(bssid, nla_data(bss[NL80211_BSS_BSSID]));
539 __netconfig_found_ap(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), ssid);
540 __netconfig_get_vsie(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]), nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), &vsie);
542 if (bss[NL80211_BSS_CAPABILITY]) {
543 __u16 capa = nla_get_u16(bss[NL80211_BSS_CAPABILITY]);
544 if (capa & WLAN_CAPABILITY_PRIVACY) {
548 __netconfig_get_security(nla_data(bss[NL80211_BSS_INFORMATION_ELEMENTS]),
549 nla_len(bss[NL80211_BSS_INFORMATION_ELEMENTS]), &sec_type, &enc_type, &pmf_needed);
551 if (wep_check && sec_type == WIFI_SECURITY_TYPE_NONE) {
552 sec_type = WIFI_SECURITY_TYPE_WEP;
553 enc_type = WIFI_ENCRYPTION_TYPE_WEP;
556 if (sec_type == WIFI_SECURITY_TYPE_EAP) {
557 g_slist_free_full(vsie, g_free);
560 for (GSList *list = bss_info_list; list != NULL; list = list->next) {
561 struct bss_scan_info_t *bss_info = (struct bss_scan_info_t *)list->data;
562 if ((g_strcmp0(bss_info->ssid, ssid) == 0) && (bss_info->security_type == sec_type)
563 && (bss_info->encryption_type == enc_type)) {
564 g_slist_free_full(vsie, g_free);
569 /** Create AP info list. */
570 if (ssid[0] != '\0') {
571 struct bss_scan_info_t *bss_info;
574 bss_info = g_try_new0(struct bss_scan_info_t, 1);
575 if (bss_info == NULL) {
576 g_slist_free_full(vsie, g_free);
580 g_strlcpy(bss_info->bssid, bssid, strlen(bssid)+1);
581 g_strlcpy(bss_info->ssid, ssid, strlen(ssid)+1);
582 bss_info->vsie_list = vsie;
583 bss_info->freq = nla_get_u32(bss[NL80211_BSS_FREQUENCY]);
585 if (bss[NL80211_BSS_SIGNAL_MBM]) {
586 signal = nla_get_u32(bss[NL80211_BSS_SIGNAL_MBM]);
587 signal /= 100; /** mBm to dBm */
588 bss_info->signal = signal;
591 bss_info->security_type = sec_type;
592 bss_info->encryption_type = enc_type;
593 bss_info->pmf_required = pmf_needed;
594 SECURE_LOGD("%s %d %d %s %d %d %d ", bss_info->bssid, bss_info->freq,
595 bss_info->signal, bss_info->ssid, bss_info->security_type,
596 bss_info->encryption_type, bss_info->pmf_required);
598 if (bss_info->ssid[0] == '\0')
601 bss_info_list = g_slist_append(bss_info_list, bss_info);
604 g_slist_free_full(vsie, g_free);
609 static int __netconfig_netlink_scan_reply(struct nl_msg *msg, void *user_data)
611 /** Called by the kernel when the scan is done or has been aborted. */
612 struct genlmsghdr *gnlh = nlmsg_data(nlmsg_hdr(msg));
613 struct netconfig_netlink_scan_results *results = user_data;
615 if (gnlh->cmd == NL80211_CMD_NEW_SCAN_RESULTS) {
616 DBG("Received NL80211_CMD_NEW_SCAN_RESULTS in reply");
618 results->aborted = 0;
619 } else if (gnlh->cmd == NL80211_CMD_SCAN_ABORTED) {
620 DBG("Received NL80211_CMD_SCAN_ABORTED in reply");
622 results->aborted = 1;
628 static int __netconfig_trigger_netlink_scan(struct nl_sock *socket,
629 int if_index, int id, GSList *ssid_list, char *vsie)
631 struct netconfig_netlink_scan_results results = { .done = 0, .aborted = 0 };
632 struct nl_msg *msg = NULL;
633 struct nl_cb *cb = NULL;
634 struct nl_msg *ssids = NULL;
637 unsigned char ies[NETCONFIG_MAX_VSIE_LEN+1] = {0x00, };
639 int mcid = __netconfig_get_multicast_id(socket, "nl80211", "scan");
642 ret = nl_socket_add_membership(socket, mcid);
644 DBG("Failed to add membership, error: (%s)", nl_geterror(-ret));
650 DBG("Failed to allocate msg");
651 nl_socket_drop_membership(socket, mcid);
654 ssids = nlmsg_alloc();
656 DBG("Failed to allocate ssids");
658 nl_socket_drop_membership(socket, mcid);
661 cb = nl_cb_alloc(NL_CB_DEFAULT);
663 DBG("Failed to allocate callbacks");
666 nl_socket_drop_membership(socket, mcid);
670 /** Set nl message and callback functions. */
671 genlmsg_put(msg, 0, 0, id, 0, 0, NL80211_CMD_TRIGGER_SCAN, 0);
672 ret = nla_put_u32(msg, NL80211_ATTR_IFINDEX, if_index);
674 DBG("Failed to add integer attribute to netlink message, error: (%s)", nl_geterror(-ret));
678 for (GSList *i = ssid_list; i; i = i->next) {
679 char *ssid = (char *)i->data;
680 ret = nla_put(ssids, 1, strlen(ssid), ssid);
682 DBG("Failed to add ssid to netlink message, error: (%s)", nl_geterror(-ret));
687 if (g_slist_length(ssid_list) == 0) {
688 DBG("Scan with no ssids");
689 ret = nla_put(ssids, 1, 0, "");
691 DBG("nla_put error: (%s)", nl_geterror(-ret));
695 nla_put_nested(msg, NL80211_ATTR_SCAN_SSIDS, ssids);
698 int vsie_len = strlen(vsie);
699 DBG("vsie: %s vsie_len: %d", vsie, vsie_len);
700 ies_len = (vsie_len % 2) ? ((vsie_len / 2) + 1) : (vsie_len / 2);
701 __netconfig_hex_str_to_bin(vsie, ies, ies_len);
704 if (ies[0] == NETCONFIG_VENDOR_SPECIFIC_ID && ies[1] >= 4) {
705 DBG("ies_len: %d ies: %02x %02x %02x %02x %02x %02x %02x", ies_len,
706 ies[0], ies[1], ies[2], ies[3], ies[4], ies[5], ies[6]);
707 ret = nla_put(msg, NL80211_ATTR_IE, ies_len, ies);
709 DBG("Failed to add vsie data to netlink message, error: (%s)", nl_geterror(-ret));
715 nl_cb_set(cb, NL_CB_VALID, NL_CB_CUSTOM, __netconfig_netlink_scan_reply, &results);
716 nl_cb_err(cb, NL_CB_CUSTOM, error_handler, &err);
717 nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler, &err);
718 nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler, &err);
719 nl_cb_set(cb, NL_CB_SEQ_CHECK, NL_CB_CUSTOM, no_seq_check, NULL);
720 ret = nl_socket_set_nonblocking(socket);
721 DBG("Set the non-blocking mode [%d]", ret);
723 /** Send NL80211_CMD_TRIGGER_SCAN to start the scan. */
724 ret = nl_send_auto_complete(socket, msg);
727 DBG("nl_send_auto_complete() error: (%s)", nl_geterror(-ret));
731 DBG("Sent %d bytes to the kernel", ret);
734 ret = nl_recvmsgs(socket, cb);
736 DBG("nl_recvmsgs() ret: %d (%s)", ret, nl_geterror(-ret));
742 DBG("error code = %d", err);
747 while (!results.done) {
748 ret = nl_recvmsgs(socket, cb);
749 #if !defined TIZEN_WEARABLE
750 if (ret == -NLE_AGAIN && err >= 0)
758 DBG("scan failed err: %d", err);
764 if (results.aborted) {
771 /** Release memory */
775 nl_socket_drop_membership(socket, mcid);
777 if (ret < 0 || ret == 1)
784 static int __netconfig_netlink_scan(netconfig_nl_global *global, GSList *ssid_list, char *vsie)
787 int ret = __netconfig_trigger_netlink_scan(global->socket, global->if_index, global->id, ssid_list, vsie);
788 if (ret == -NLE_AGAIN) {
790 ret = __netconfig_trigger_netlink_scan(global->socket, global->if_index, global->id, ssid_list, vsie);
795 static int __netconfig_initialize_nl80211(netconfig_nl_global *global,
796 const char *interface_name)
800 global->if_index = __netconfig_get_interface_index(interface_name);
801 if (global->if_index < 0) {
802 DBG("Failed to get interface index");
806 global->socket = nl_socket_alloc();
807 if (!global->socket) {
808 DBG("Failed to allocate netlink socket.");
812 if (genl_connect(global->socket)) {
813 DBG("Failed to connect to generic netlink.");
818 global->id = genl_ctrl_resolve(global->socket, "nl80211");
819 if (global->id < 0) {
820 DBG("Failed to find the nl80211 driver");
828 nl_socket_free(global->socket);
832 static void __netconfig_deinitialize_nl80211(netconfig_nl_global *global)
837 if (global->socket) {
838 nl_socket_free(global->socket);
839 global->socket = NULL;
843 static int __netconfig_initialize_nl_msg(netconfig_nl_global *global)
847 if (global == NULL) {
848 DBG("Invalid parameter.");
852 global->msg = nlmsg_alloc();
853 if (global->msg == NULL) {
854 DBG("Failed to allocate netlink message");
858 /* Set command into message */
859 genlmsg_put(global->msg, 0, 0, global->id, 0, NLM_F_DUMP, NL80211_CMD_GET_SCAN, 0);
860 rv = nla_put_u32(global->msg, NL80211_ATTR_IFINDEX, global->if_index);
862 DBG("Failed to add integer attribute to netlink message.");
863 nlmsg_free(global->msg);
866 nl_socket_modify_cb(global->socket, NL_CB_VALID, NL_CB_CUSTOM, __netconfig_netlink_scan_cb, NULL);
871 static void __netconfig_get_netlink_scan_parameters(GVariant *params, GSList *ssid_list, char **vsie)
876 g_variant_get(params, "a{sv}", &iter);
877 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
878 if (g_strcmp0(key, "SSID") == 0) {
879 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING)) {
880 char *ssid = g_strdup(g_variant_get_string(value, NULL));
881 DBG("ssid [%s]", ssid);
882 ssid_list = g_slist_append(ssid_list, ssid);
884 } else if (g_strcmp0(key, "VSIE") == 0) {
885 if (g_variant_is_of_type(value, G_VARIANT_TYPE_STRING))
886 *vsie = g_strdup(g_variant_get_string(value, NULL));
889 g_variant_iter_free(iter);
892 static int __netconfig_get_scan_results(netconfig_nl_global *global)
894 int ret = __netconfig_initialize_nl_msg(global);
896 DBG("__netconfig_initialize_nl_msg() failed, error %d", ret);
900 ret = nl_send_auto_complete(global->socket, global->msg);
902 DBG("nl_send_auto_complete() failed, error %d", ret);
905 DBG("NL80211_CMD_GET_SCAN sent %d bytes to the kernel", ret);
907 /** Receive the kernel message. */
908 ret = nl_recvmsgs_default(global->socket);
909 nlmsg_free(global->msg);
911 DBG("nl_recvmsgs_default() failed. ret: %d (error: %s)", ret, nl_geterror(-ret));
917 int handle_netlink_scan(Wifi *wifi, GDBusMethodInvocation *context,
918 const gchar *ifname, GVariant *params)
921 netconfig_nl_global global = {
928 /** Initialize netlink socket */
929 int ret = __netconfig_initialize_nl80211(&global, ifname);
931 DBG("__netconfig_initialize_nl80211() failed, error %d", ret);
932 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "AccessDenied");
936 GSList *ssid_list = NULL;
938 __netconfig_get_netlink_scan_parameters(params, ssid_list, &vsie);
939 /** Request NL80211_CMD_TRIGGER_SCAN to the kernel. */
940 ret = __netconfig_netlink_scan(&global, ssid_list, vsie);
941 g_slist_free_full(ssid_list, g_free);
945 DBG("__netconfig_netlink_scan() failed, error %d", ret);
946 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "AccessDenied");
947 __netconfig_deinitialize_nl80211(&global);
951 ret = __netconfig_get_scan_results(&global);
953 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "AccessDenied");
954 __netconfig_deinitialize_nl80211(&global);
958 wifi_complete_netlink_scan(wifi, context);
959 __netconfig_notify_netlink_scan_done(ifname);
960 __netconfig_deinitialize_nl80211(&global);