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.
26 #include "netsupplicant.h"
27 #include "wifi-ssid-scan.h"
28 #include "wifi-background-scan.h"
30 #define MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
31 #define MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
33 #define WIFI_KEYMGMT_NONE (1 << 0)
34 #define WIFI_KEYMGMT_IEEE8021X (1 << 1)
35 #define WIFI_KEYMGMT_WPA_NONE (1 << 2)
36 #define WIFI_KEYMGMT_WPA_PSK (1 << 3)
37 #define WIFI_KEYMGMT_WPA_PSK_256 (1 << 4)
38 #define WIFI_KEYMGMT_WPA_FT_PSK (1 << 5)
39 #define WIFI_KEYMGMT_WPA_FT_EAP (1 << 6)
40 #define WIFI_KEYMGMT_WPA_EAP (1 << 7)
41 #define WIFI_KEYMGMT_WPA_EAP_256 (1 << 8)
42 #define WIFI_KEYMGMT_WPS (1 << 9)
44 #define WIFI_PAIRWISE_NONE (1 << 0)
45 #define WIFI_PAIRWISE_TKIP (1 << 1)
46 #define WIFI_PAIRWISE_CCMP (1 << 2)
53 static strval_s wifi_keymgmt[] = {
54 { "none", WIFI_KEYMGMT_NONE },
55 { "ieee8021x", WIFI_KEYMGMT_IEEE8021X },
56 { "wpa-none", WIFI_KEYMGMT_WPA_NONE },
57 { "wpa-psk", WIFI_KEYMGMT_WPA_PSK },
58 { "wpa-psk-sha256", WIFI_KEYMGMT_WPA_PSK_256 },
59 { "wpa-ft-psk", WIFI_KEYMGMT_WPA_FT_PSK },
60 { "wpa-ft-eap", WIFI_KEYMGMT_WPA_FT_EAP },
61 { "wpa-eap", WIFI_KEYMGMT_WPA_EAP },
62 { "wpa-eap-sha256", WIFI_KEYMGMT_WPA_EAP_256 },
63 { "wps", WIFI_KEYMGMT_WPS },
67 static strval_s wifi_pairwise[] = {
68 { "none", WIFI_PAIRWISE_NONE },
69 { "tkip", WIFI_PAIRWISE_TKIP },
70 { "ccmp", WIFI_PAIRWISE_CCMP },
75 WIFI_SECURITY_UNKNOWN = 0x00,
80 WIFI_SECURITY_IEEE8021X,
84 unsigned char ssid[33];
85 unsigned char bssid[6];
86 wifi_security_e security;
87 unsigned int wpa_keymgmt;
88 unsigned int wpa_pairwise;
89 unsigned int rsn_keymgmt;
90 unsigned int rsn_pairwise;
91 gboolean rsn_selected;
96 static gboolean g_ssid_scan_state = FALSE;
97 static GSList *bss_info_list = NULL;
98 static guint ssid_scan_timer = 0;
99 static char *g_ssid = NULL;
101 static void __check_keymgmt(const char *str_keymgmt, unsigned int *key_info)
105 for (i = 0; wifi_keymgmt[i].str; i++) {
106 if (g_strcmp0(str_keymgmt, wifi_keymgmt[i].str) == 0) {
107 INFO("keymgmt : %s", str_keymgmt);
108 *key_info |= wifi_keymgmt[i].val;
114 static void __check_pairwise(const char *str_pairwise, unsigned int *pairwise_info)
118 for (i = 0; wifi_pairwise[i].str; i++) {
119 if (g_strcmp0(str_pairwise, wifi_pairwise[i].str) == 0) {
120 INFO("pairwise : %s", str_pairwise);
121 *pairwise_info |= wifi_pairwise[i].val;
127 static wifi_security_e __check_security(bss_info_t *bss_info)
129 gboolean ieee8021x = FALSE;
130 gboolean psk = FALSE;
131 gboolean ft_ieee8021x = FALSE;
132 gboolean ft_psk = FALSE;
133 unsigned int keymgmt = bss_info->rsn_keymgmt | bss_info->wpa_keymgmt;
135 if (keymgmt & (WIFI_KEYMGMT_WPA_EAP | WIFI_KEYMGMT_WPA_EAP_256))
137 else if (keymgmt & WIFI_KEYMGMT_WPA_FT_EAP)
140 if (keymgmt & (WIFI_KEYMGMT_WPA_PSK | WIFI_KEYMGMT_WPA_PSK_256))
142 else if (keymgmt & WIFI_KEYMGMT_WPA_FT_PSK)
145 if (ieee8021x || ft_ieee8021x)
146 bss_info->security = WIFI_SECURITY_IEEE8021X;
147 else if (psk || ft_psk)
148 bss_info->security = WIFI_SECURITY_PSK;
149 else if (bss_info->privacy)
150 bss_info->security = WIFI_SECURITY_WEP;
152 bss_info->security = WIFI_SECURITY_NONE;
154 if (bss_info->rsn_selected) {
155 unsigned int pairwise = bss_info->rsn_pairwise | bss_info->wpa_pairwise;
156 if ((pairwise & WIFI_PAIRWISE_CCMP) ||
157 (pairwise & (WIFI_PAIRWISE_CCMP | WIFI_PAIRWISE_TKIP)))
158 bss_info->security = WIFI_SECURITY_PSK2;
161 return bss_info->security;
164 static gboolean __ssid_scan_timeout(gpointer data)
166 wifi_ssid_scan_emit_scan_completed();
171 static void _start_ssid_scan_timer(void)
173 INFO("Wi-Fi SSID scan started");
174 g_ssid_scan_state = TRUE;
176 netconfig_start_timer_seconds(5, __ssid_scan_timeout, NULL, &ssid_scan_timer);
179 static void _stop_ssid_scan_timer(void)
181 INFO("Wi-Fi SSID scan finished");
182 g_ssid_scan_state = FALSE;
184 netconfig_stop_timer(&ssid_scan_timer);
187 static void _parse_wpa_message(GVariant *param, bss_info_t *bss_info)
193 g_variant_get(param, "a{sv}", &iter1);
194 while (g_variant_iter_loop(iter1, "{sv}", &key, &var)) {
195 if (g_strcmp0(key, "KeyMgmt") == 0) {
197 g_variant_get(var, "as", &iter2);
199 while (g_variant_iter_loop(iter2, "s", &str)) {
202 unsigned int key_info = 0;
203 __check_keymgmt(str, &key_info);
204 if (bss_info->rsn_selected)
205 bss_info->rsn_keymgmt = key_info;
207 bss_info->wpa_keymgmt = key_info;
209 g_variant_iter_free(iter2);
210 } else if (g_strcmp0(key, "Pairwise") == 0) {
212 g_variant_get(var, "as", &iter2);
214 while (g_variant_iter_loop(iter2, "s", &str)) {
217 unsigned int pairwise_info = 0;
218 __check_pairwise(str, &pairwise_info);
219 if (bss_info->rsn_selected)
220 bss_info->rsn_pairwise = pairwise_info;
222 bss_info->wpa_pairwise = pairwise_info;
224 g_variant_iter_free(iter2);
228 g_variant_iter_free(iter1);
233 static gboolean _request_ssid_scan(const char *object_path, const char *ssid)
235 /* TODO: Revise following code */
237 GDBusConnection *connection = NULL;
238 GVariant *reply = NULL;
239 GVariant *params = NULL;
240 GError *error = NULL;
241 GVariantBuilder *builder1 = NULL;
242 GVariantBuilder *builder2 = NULL;
243 GVariantBuilder *builder3 = NULL;
244 const gchar *key1 = "Type";
245 const gchar *val1 = "active";
246 const gchar *key2 = "SSIDs";
249 connection = netdbus_get_connection();
250 if (connection == NULL) {
251 DBG("Failed to get GDBusconnection");
255 builder1 = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
256 g_variant_builder_add(builder1, "{sv}", key1, g_variant_new_string(val1));
258 builder2 = g_variant_builder_new(G_VARIANT_TYPE("aay"));
259 builder3 = g_variant_builder_new(G_VARIANT_TYPE("ay"));
261 for (i = 0; i < strlen(ssid); i++)
262 g_variant_builder_add(builder3, "y", ssid[i]);
264 g_variant_builder_add(builder2, "@ay", g_variant_builder_end(builder3));
265 g_variant_builder_add(builder1, "{sv}", key2, g_variant_builder_end(builder2));
267 params = g_variant_new("(@a{sv})", g_variant_builder_end(builder1));
269 g_variant_builder_unref(builder1);
270 g_variant_builder_unref(builder2);
271 g_variant_builder_unref(builder3);
273 reply = g_dbus_connection_call_sync(
277 SUPPLICANT_INTERFACE ".Interface",
281 G_DBUS_CALL_FLAGS_NONE,
282 NETCONFIG_DBUS_REPLY_TIMEOUT,
283 netdbus_get_cancellable(),
288 ERR("Error!!! dbus_connection_send_with_reply_and_block() failed. "
289 "DBus error [%d: %s]", error->code, error->message);
292 ERR("Error!!! Failed to get properties");
300 g_ssid = g_strdup(ssid);
302 g_variant_unref(reply);
307 static void _emit_ssid_scan_completed(void)
309 GVariantBuilder *builder = NULL;
311 const char *prop_ssid = "ssid";
312 const char *prop_bssid = "bssid";
313 char bssid_buf[18] = {0,};
314 char *bssid_str = bssid_buf;
315 const char *prop_security = "security";
316 const char *prop_wps = "wps";
318 builder = g_variant_builder_new(G_VARIANT_TYPE("a{sv}"));
319 for (list = bss_info_list; list != NULL; list = list->next) {
320 bss_info_t *bss_info = (bss_info_t *)list->data;
321 if (bss_info && g_strcmp0((char *)bss_info->ssid, g_ssid) == 0) {
322 const gchar *ssid = (char *)bss_info->ssid;
323 const gchar *bssid = (gchar *)&bss_info->bssid[0];
324 wifi_security_e security = __check_security(bss_info);
325 gboolean wps = bss_info->wps;
326 DBG("BSS found; SSID:%s security:%d WPS:%d", ssid, security, wps);
329 snprintf(bssid_str, sizeof(bssid_buf), MACSTR, MAC2STR(bssid));
330 DBG("BSSID: %s", bssid_str);
332 g_variant_builder_add(builder, "{sv}", prop_ssid, g_variant_new_string(ssid));
333 g_variant_builder_add(builder, "{sv}", prop_bssid,
334 g_variant_new_string(bssid));
335 g_variant_builder_add(builder, "{sv}", prop_security, g_variant_new_int32(security));
337 g_variant_builder_add(builder, "{sv}", prop_wps, g_variant_new_boolean(wps));
341 wifi_emit_specific_scan_completed((Wifi *)get_wifi_object(), g_variant_builder_end(builder));
344 g_variant_builder_unref(builder);
346 if (bss_info_list != NULL) {
347 g_slist_free_full(bss_info_list, g_free);
348 bss_info_list = NULL;
351 if (g_ssid != NULL) {
356 INFO("SpecificScanCompleted");
361 gboolean wifi_ssid_scan(const char *ssid)
364 static char *scan_ssid = NULL;
366 netconfig_wifi_bgscan_stop();
369 if (scan_ssid != NULL)
371 scan_ssid = g_strdup(ssid);
374 if (scan_ssid == NULL)
377 if_path = netconfig_wifi_get_supplicant_interface();
378 if (if_path == NULL) {
379 DBG("Fail to get wpa_supplicant DBus path");
383 if (netconfig_wifi_get_scanning() == TRUE) {
384 DBG("Wi-Fi scan in progress, %s scan will be delayed", scan_ssid);
389 g_slist_free_full(bss_info_list, g_free);
390 bss_info_list = NULL;
393 INFO("Start Wi-Fi scan with %s(%d)", scan_ssid, strlen(scan_ssid));
394 if (_request_ssid_scan(if_path, (const char *)scan_ssid) == TRUE) {
395 _start_ssid_scan_timer();
402 if (scan_ssid != NULL) {
407 netconfig_wifi_bgscan_start(FALSE);
412 gboolean wifi_ssid_scan_get_state(void)
414 return g_ssid_scan_state;
417 void wifi_ssid_scan_emit_scan_completed(void)
419 if (g_ssid_scan_state != TRUE)
422 _stop_ssid_scan_timer();
423 _emit_ssid_scan_completed();
426 void wifi_ssid_scan_add_bss(GVariant *message)
432 bss_info_t *bss_info;
434 if (g_ssid_scan_state != TRUE)
437 INFO("NEW BSS added");
439 if (message == NULL) {
440 DBG("Message does not have parameters");
445 INFO("Object path of BSS added is %s", path);
447 bss_info = g_try_new0(bss_info_t, 1);
448 if (bss_info == NULL)
451 g_variant_get(message, "(oa{sv})", &path, &iter);
452 while (g_variant_iter_loop(iter, "{sv}", &key, &value)) {
453 if (g_strcmp0(key, "SSID") == 0) {
456 ssid = g_variant_get_fixed_array(value, &ssid_len, sizeof(guchar));
457 if (ssid != NULL && ssid_len > 0 && ssid_len < 33)
458 memcpy(bss_info->ssid, ssid, ssid_len);
460 memset(bss_info->ssid, 0, sizeof(bss_info->ssid));
461 } else if (g_strcmp0(key, "Privacy") == 0) {
462 gboolean privacy = FALSE;
463 privacy = g_variant_get_boolean(value);
464 bss_info->privacy = privacy;
465 } else if (g_strcmp0(key, "RSN") == 0) {
466 bss_info->rsn_selected = TRUE;
467 _parse_wpa_message(value, bss_info);
468 } else if (g_strcmp0(key, "WPA") == 0) {
469 bss_info->rsn_selected = FALSE;
470 _parse_wpa_message(value, bss_info);
471 } else if (g_strcmp0(key, "IEs") == 0) {
474 ie = g_variant_get_fixed_array(value, &ie_len, sizeof(guchar));
475 DBG("The IE : %s", ie);
476 } else if(g_strcmp0(key, "BSSID") == 0) {
480 bssid = g_variant_get_fixed_array(value, &bssid_len, sizeof(guchar));
481 if(bssid != NULL && bssid_len == 6)
482 memcpy(bss_info->bssid,bssid,bssid_len);
486 g_variant_iter_free(iter);
490 if (bss_info->ssid[0] == '\0') {
495 if (bss_info->security == WIFI_SECURITY_UNKNOWN) {
496 if (bss_info->privacy == TRUE)
497 bss_info->security = WIFI_SECURITY_WEP;
499 bss_info->security = WIFI_SECURITY_NONE;
502 bss_info_list = g_slist_append(bss_info_list, bss_info);
505 gboolean handle_request_specific_scan(Wifi *wifi,
506 GDBusMethodInvocation *context, const gchar *ssid)
508 gboolean result = FALSE;
510 g_return_val_if_fail(wifi != NULL, FALSE);
511 g_return_val_if_fail(ssid != NULL, FALSE);
513 result = wifi_ssid_scan((const char *)ssid);
516 netconfig_error_dbus_method_return(context, NETCONFIG_ERROR_INTERNAL, "FailSpecificScan");
518 wifi_complete_request_specific_scan(wifi, context);