5 * Copyright (C) 2007-2008 Intel Corporation. All rights reserved.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <dbus/dbus.h>
29 #include <connman/log.h>
30 #include <connman/dbus.h>
33 #include "supplicant.h"
37 #define IEEE80211_CAP_ESS 0x0001
38 #define IEEE80211_CAP_IBSS 0x0002
39 #define IEEE80211_CAP_PRIVACY 0x0010
41 struct supplicant_task {
44 struct connman_element *element;
45 struct supplicant_callback *callback;
49 enum supplicant_state state;
52 static GStaticMutex task_mutex = G_STATIC_MUTEX_INIT;
53 static GSList *task_list = NULL;
55 static DBusConnection *connection;
57 static struct supplicant_task *find_task_by_index(int index)
61 for (list = task_list; list; list = list->next) {
62 struct supplicant_task *task = list->data;
64 if (task->ifindex == index)
71 static struct supplicant_task *find_task_by_path(const char *path)
75 for (list = task_list; list; list = list->next) {
76 struct supplicant_task *task = list->data;
78 if (g_str_equal(task->path, path) == TRUE)
85 static int get_interface(struct supplicant_task *task)
87 DBusMessage *message, *reply;
93 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
94 SUPPLICANT_INTF, "getInterface");
98 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
101 dbus_error_init(&error);
103 reply = dbus_connection_send_with_reply_and_block(connection,
104 message, -1, &error);
106 if (dbus_error_is_set(&error) == TRUE) {
107 connman_error("%s", error.message);
108 dbus_error_free(&error);
110 connman_error("Failed to get interface");
111 dbus_message_unref(message);
115 dbus_message_unref(message);
117 dbus_error_init(&error);
119 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
120 DBUS_TYPE_INVALID) == FALSE) {
121 if (dbus_error_is_set(&error) == TRUE) {
122 connman_error("%s", error.message);
123 dbus_error_free(&error);
125 connman_error("Wrong arguments for interface");
126 dbus_message_unref(reply);
130 DBG("path %s", path);
132 task->path = g_strdup(path);
133 task->created = FALSE;
135 dbus_message_unref(reply);
140 static int add_interface(struct supplicant_task *task)
142 DBusMessage *message, *reply;
146 DBG("task %p", task);
148 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
149 SUPPLICANT_INTF, "addInterface");
153 dbus_error_init(&error);
155 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
158 reply = dbus_connection_send_with_reply_and_block(connection,
159 message, -1, &error);
161 if (dbus_error_is_set(&error) == TRUE) {
162 connman_error("%s", error.message);
163 dbus_error_free(&error);
165 connman_error("Failed to add interface");
166 dbus_message_unref(message);
170 dbus_message_unref(message);
172 dbus_error_init(&error);
174 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
175 DBUS_TYPE_INVALID) == FALSE) {
176 if (dbus_error_is_set(&error) == TRUE) {
177 connman_error("%s", error.message);
178 dbus_error_free(&error);
180 connman_error("Wrong arguments for interface");
181 dbus_message_unref(reply);
185 DBG("path %s", path);
187 task->path = g_strdup(path);
188 task->created = TRUE;
190 dbus_message_unref(reply);
195 static int remove_interface(struct supplicant_task *task)
197 DBusMessage *message, *reply;
200 DBG("task %p", task);
202 if (task->created == FALSE)
205 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
206 SUPPLICANT_INTF, "removeInterface");
210 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
213 dbus_error_init(&error);
215 reply = dbus_connection_send_with_reply_and_block(connection,
216 message, -1, &error);
218 if (dbus_error_is_set(&error) == TRUE) {
219 connman_error("%s", error.message);
220 dbus_error_free(&error);
222 connman_error("Failed to remove interface");
223 dbus_message_unref(message);
227 dbus_message_unref(message);
229 dbus_message_unref(reply);
234 static int set_ap_scan(struct supplicant_task *task)
236 DBusMessage *message, *reply;
240 DBG("task %p", task);
242 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
243 SUPPLICANT_INTF ".Interface", "setAPScan");
247 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
250 dbus_error_init(&error);
252 reply = dbus_connection_send_with_reply_and_block(connection,
253 message, -1, &error);
255 if (dbus_error_is_set(&error) == TRUE) {
256 connman_error("%s", error.message);
257 dbus_error_free(&error);
259 connman_error("Failed to set AP scan");
260 dbus_message_unref(message);
264 dbus_message_unref(message);
266 dbus_message_unref(reply);
271 static int add_network(struct supplicant_task *task)
273 DBusMessage *message, *reply;
277 DBG("task %p", task);
279 if (task->network != NULL)
282 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
283 SUPPLICANT_INTF ".Interface", "addNetwork");
287 dbus_error_init(&error);
289 reply = dbus_connection_send_with_reply_and_block(connection,
290 message, -1, &error);
292 if (dbus_error_is_set(&error) == TRUE) {
293 connman_error("%s", error.message);
294 dbus_error_free(&error);
296 connman_error("Failed to add network");
297 dbus_message_unref(message);
301 dbus_message_unref(message);
303 dbus_error_init(&error);
305 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
306 DBUS_TYPE_INVALID) == FALSE) {
307 if (dbus_error_is_set(&error) == TRUE) {
308 connman_error("%s", error.message);
309 dbus_error_free(&error);
311 connman_error("Wrong arguments for network");
312 dbus_message_unref(reply);
316 DBG("path %s", path);
318 task->network = g_strdup(path);
320 dbus_message_unref(reply);
325 static int remove_network(struct supplicant_task *task)
327 DBusMessage *message, *reply;
330 DBG("task %p", task);
332 if (task->network == NULL)
335 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
336 SUPPLICANT_INTF ".Interface", "removeNetwork");
340 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->network,
343 dbus_error_init(&error);
345 reply = dbus_connection_send_with_reply_and_block(connection,
346 message, -1, &error);
348 if (dbus_error_is_set(&error) == TRUE) {
349 connman_error("%s", error.message);
350 dbus_error_free(&error);
352 connman_error("Failed to remove network");
353 dbus_message_unref(message);
357 dbus_message_unref(message);
359 dbus_message_unref(reply);
361 g_free(task->network);
362 task->network = NULL;
367 static int select_network(struct supplicant_task *task)
369 DBusMessage *message, *reply;
372 DBG("task %p", task);
374 if (task->network == NULL)
377 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
378 SUPPLICANT_INTF ".Interface", "selectNetwork");
382 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->network,
385 dbus_error_init(&error);
387 reply = dbus_connection_send_with_reply_and_block(connection,
388 message, -1, &error);
390 if (dbus_error_is_set(&error) == TRUE) {
391 connman_error("%s", error.message);
392 dbus_error_free(&error);
394 connman_error("Failed to select network");
395 dbus_message_unref(message);
399 dbus_message_unref(message);
401 dbus_message_unref(reply);
406 static int enable_network(struct supplicant_task *task)
408 DBusMessage *message, *reply;
411 DBG("task %p", task);
413 if (task->network == NULL)
416 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
417 SUPPLICANT_INTF ".Network", "enable");
421 dbus_error_init(&error);
423 reply = dbus_connection_send_with_reply_and_block(connection,
424 message, -1, &error);
426 if (dbus_error_is_set(&error) == TRUE) {
427 connman_error("%s", error.message);
428 dbus_error_free(&error);
430 connman_error("Failed to enable network");
431 dbus_message_unref(message);
435 dbus_message_unref(message);
437 dbus_message_unref(reply);
442 static int disable_network(struct supplicant_task *task)
444 DBusMessage *message, *reply;
447 DBG("task %p", task);
449 if (task->network == NULL)
452 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
453 SUPPLICANT_INTF ".Network", "disable");
457 dbus_error_init(&error);
459 reply = dbus_connection_send_with_reply_and_block(connection,
460 message, -1, &error);
462 if (dbus_error_is_set(&error) == TRUE) {
463 connman_error("%s", error.message);
464 dbus_error_free(&error);
466 connman_error("Failed to disable network");
467 dbus_message_unref(message);
471 dbus_message_unref(message);
473 dbus_message_unref(reply);
478 static int set_network(struct supplicant_task *task,
479 const unsigned char *network, int len,
480 const char *passphrase)
482 DBusMessage *message, *reply;
483 DBusMessageIter array, dict;
486 DBG("task %p", task);
488 if (task->network == NULL)
491 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
492 SUPPLICANT_INTF ".Network", "set");
496 dbus_message_iter_init_append(message, &array);
498 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
499 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
500 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
501 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
503 connman_dbus_dict_append_array(&dict, "ssid",
504 DBUS_TYPE_BYTE, &network, len);
506 if (passphrase && strlen(passphrase) > 0) {
507 const char *key_mgmt = "WPA-PSK";
508 connman_dbus_dict_append_variant(&dict, "key_mgmt",
509 DBUS_TYPE_STRING, &key_mgmt);
510 connman_dbus_dict_append_variant(&dict, "psk",
511 DBUS_TYPE_STRING, &passphrase);
513 const char *key_mgmt = "NONE";
514 connman_dbus_dict_append_variant(&dict, "key_mgmt",
515 DBUS_TYPE_STRING, &key_mgmt);
518 dbus_message_iter_close_container(&array, &dict);
520 dbus_error_init(&error);
522 reply = dbus_connection_send_with_reply_and_block(connection,
523 message, -1, &error);
525 if (dbus_error_is_set(&error) == TRUE) {
526 connman_error("%s", error.message);
527 dbus_error_free(&error);
529 connman_error("Failed to set network options");
530 dbus_message_unref(message);
534 dbus_message_unref(message);
536 dbus_message_unref(reply);
541 static int initiate_scan(struct supplicant_task *task)
543 DBusMessage *message;
544 DBusPendingCall *call;
546 DBG("task %p", task);
548 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
549 SUPPLICANT_INTF ".Interface", "scan");
553 if (dbus_connection_send_with_reply(connection, message,
554 &call, TIMEOUT) == FALSE) {
555 connman_error("Failed to initiate scan");
556 dbus_message_unref(message);
560 dbus_message_unref(message);
565 static void extract_ssid(struct supplicant_network *network,
566 DBusMessageIter *value)
568 DBusMessageIter array;
572 dbus_message_iter_recurse(value, &array);
573 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
578 network->ssid = g_try_malloc(ssid_len);
579 if (network->ssid == NULL)
582 memcpy(network->ssid, ssid, ssid_len);
583 network->ssid_len = ssid_len;
585 network->identifier = g_try_malloc0(ssid_len + 1);
586 if (network->identifier == NULL)
589 memcpy(network->identifier, ssid, ssid_len);
592 static void extract_wpaie(struct supplicant_network *network,
593 DBusMessageIter *value)
595 DBusMessageIter array;
599 dbus_message_iter_recurse(value, &array);
600 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
603 network->has_wpa = TRUE;
606 static void extract_rsnie(struct supplicant_network *network,
607 DBusMessageIter *value)
609 DBusMessageIter array;
613 dbus_message_iter_recurse(value, &array);
614 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
617 network->has_rsn = TRUE;
620 static void extract_capabilites(struct supplicant_network *network,
621 DBusMessageIter *value)
625 dbus_message_iter_get_basic(value, &capabilities);
627 network->capabilities = capabilities;
629 if (capabilities & IEEE80211_CAP_PRIVACY)
630 network->has_wep = TRUE;
633 static void properties_reply(DBusPendingCall *call, void *user_data)
635 struct supplicant_task *task = user_data;
636 struct supplicant_network *network;
638 DBusMessageIter array, dict;
640 DBG("task %p", task);
642 reply = dbus_pending_call_steal_reply(call);
644 network = g_try_new0(struct supplicant_network, 1);
648 dbus_message_iter_init(reply, &array);
650 dbus_message_iter_recurse(&array, &dict);
652 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
653 DBusMessageIter entry, value;
656 dbus_message_iter_recurse(&dict, &entry);
657 dbus_message_iter_get_basic(&entry, &key);
659 dbus_message_iter_next(&entry);
661 dbus_message_iter_recurse(&entry, &value);
663 //type = dbus_message_iter_get_arg_type(&value);
664 //dbus_message_iter_get_basic(&value, &val);
666 if (g_str_equal(key, "ssid") == TRUE)
667 extract_ssid(network, &value);
668 else if (g_str_equal(key, "wpaie") == TRUE)
669 extract_wpaie(network, &value);
670 else if (g_str_equal(key, "rsnie") == TRUE)
671 extract_rsnie(network, &value);
672 else if (g_str_equal(key, "capabilities") == TRUE)
673 extract_capabilites(network, &value);
675 dbus_message_iter_next(&dict);
678 if (task->callback && task->callback->scan_result)
679 task->callback->scan_result(task->element, network);
681 g_free(network->identifier);
682 g_free(network->ssid);
686 dbus_message_unref(reply);
689 static int get_network_properties(struct supplicant_task *task,
692 DBusMessage *message;
693 DBusPendingCall *call;
695 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
696 SUPPLICANT_INTF ".BSSID",
701 if (dbus_connection_send_with_reply(connection, message,
702 &call, TIMEOUT) == FALSE) {
703 connman_error("Failed to get network properties");
704 dbus_message_unref(message);
708 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
710 dbus_message_unref(message);
715 static void scan_results_reply(DBusPendingCall *call, void *user_data)
717 struct supplicant_task *task = user_data;
723 DBG("task %p", task);
725 reply = dbus_pending_call_steal_reply(call);
727 dbus_error_init(&error);
729 if (dbus_message_get_args(reply, &error,
730 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
731 &results, &num_results,
732 DBUS_TYPE_INVALID) == FALSE) {
733 if (dbus_error_is_set(&error) == TRUE) {
734 connman_error("%s", error.message);
735 dbus_error_free(&error);
737 connman_error("Wrong arguments for scan result");
741 for (i = 0; i < num_results; i++)
742 get_network_properties(task, results[i]);
747 dbus_message_unref(reply);
750 static int scan_results_available(struct supplicant_task *task)
752 DBusMessage *message;
753 DBusPendingCall *call;
755 DBG("task %p", task);
757 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
758 SUPPLICANT_INTF ".Interface",
763 if (dbus_connection_send_with_reply(connection, message,
764 &call, TIMEOUT) == FALSE) {
765 connman_error("Failed to request scan result");
766 dbus_message_unref(message);
770 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
772 dbus_message_unref(message);
777 static void state_change(struct supplicant_task *task, DBusMessage *msg)
780 const char *state, *previous;
782 dbus_error_init(&error);
784 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &state,
785 DBUS_TYPE_STRING, &previous,
786 DBUS_TYPE_INVALID) == FALSE) {
787 if (dbus_error_is_set(&error) == TRUE) {
788 connman_error("%s", error.message);
789 dbus_error_free(&error);
791 connman_error("Wrong arguments for state change");
795 DBG("state %s ==> %s", previous, state);
797 if (g_str_equal(state, "INACTIVE") == TRUE)
798 task->state = STATE_INACTIVE;
799 else if (g_str_equal(state, "SCANNING") == TRUE)
800 task->state = STATE_SCANNING;
801 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
802 task->state = STATE_ASSOCIATING;
803 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
804 task->state = STATE_ASSOCIATED;
805 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
806 task->state = STATE_4WAY_HANDSHAKE;
807 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
808 task->state = STATE_4WAY_HANDSHAKE;
809 else if (g_str_equal(state, "COMPLETED") == TRUE)
810 task->state = STATE_COMPLETED;
811 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
812 task->state = STATE_DISCONNECTED;
814 if (task->callback && task->callback->state_change)
815 task->callback->state_change(task->element, task->state);
817 switch (task->state) {
818 case STATE_COMPLETED:
821 case STATE_DISCONNECTED:
829 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
830 DBusMessage *msg, void *data)
832 struct supplicant_task *task;
833 const char *member, *path;
835 if (dbus_message_has_interface(msg,
836 SUPPLICANT_INTF ".Interface") == FALSE)
837 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
839 member = dbus_message_get_member(msg);
841 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
843 path = dbus_message_get_path(msg);
845 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
847 task = find_task_by_path(path);
849 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
851 DBG("task %p member %s", task, member);
853 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
854 scan_results_available(task);
855 else if (g_str_equal(member, "StateChange") == TRUE)
856 state_change(task, msg);
858 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
861 static int add_filter(struct supplicant_task *task)
866 filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
867 SUPPLICANT_INTF, task->path);
869 DBG("filter %s", filter);
871 dbus_error_init(&error);
873 dbus_bus_add_match(connection, filter, &error);
877 if (dbus_error_is_set(&error) == TRUE) {
878 connman_error("Can't add match: %s", error.message);
879 dbus_error_free(&error);
885 static int remove_filter(struct supplicant_task *task)
890 filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
891 SUPPLICANT_INTF, task->path);
893 DBG("filter %s", filter);
895 dbus_error_init(&error);
897 dbus_bus_remove_match(connection, filter, &error);
901 if (dbus_error_is_set(&error) == TRUE) {
902 connman_error("Can't add match: %s", error.message);
903 dbus_error_free(&error);
909 int __supplicant_start(struct connman_element *element,
910 struct supplicant_callback *callback)
912 struct supplicant_task *task;
915 DBG("element %p name %s", element, element->name);
917 task = g_try_new0(struct supplicant_task, 1);
921 task->ifindex = element->index;
922 task->ifname = inet_index2name(element->index);
923 task->element = element;
924 task->callback = callback;
926 if (task->ifname == NULL) {
931 task->created = FALSE;
932 task->state = STATE_INACTIVE;
934 g_static_mutex_lock(&task_mutex);
935 task_list = g_slist_append(task_list, task);
936 g_static_mutex_unlock(&task_mutex);
938 err = get_interface(task);
940 err = add_interface(task);
954 int __supplicant_stop(struct connman_element *element)
956 struct supplicant_task *task;
958 DBG("element %p name %s", element, element->name);
960 task = find_task_by_index(element->index);
964 g_static_mutex_lock(&task_mutex);
965 task_list = g_slist_remove(task_list, task);
966 g_static_mutex_unlock(&task_mutex);
968 disable_network(task);
970 remove_network(task);
974 remove_interface(task);
976 g_free(task->ifname);
983 int __supplicant_scan(struct connman_element *element)
985 struct supplicant_task *task;
988 DBG("element %p name %s", element, element->name);
990 task = find_task_by_index(element->index);
994 switch (task->state) {
997 case STATE_ASSOCIATING:
998 case STATE_ASSOCIATED:
999 case STATE_4WAY_HANDSHAKE:
1000 case STATE_GROUP_HANDSHAKE:
1006 err = initiate_scan(task);
1011 int __supplicant_connect(struct connman_element *element,
1012 const unsigned char *ssid, int ssid_len,
1013 const char *passphrase)
1015 struct supplicant_task *task;
1017 DBG("element %p name %s", element, element->name);
1019 task = find_task_by_index(element->index);
1025 select_network(task);
1026 disable_network(task);
1028 set_network(task, ssid, ssid_len, passphrase);
1030 enable_network(task);
1035 int __supplicant_disconnect(struct connman_element *element)
1037 struct supplicant_task *task;
1039 DBG("element %p name %s", element, element->name);
1041 task = find_task_by_index(element->index);
1045 disable_network(task);
1047 remove_network(task);
1052 int __supplicant_init(void)
1054 connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1055 if (connection == NULL)
1058 if (dbus_connection_add_filter(connection,
1059 supplicant_filter, NULL, NULL) == FALSE) {
1060 dbus_connection_unref(connection);
1067 void __supplicant_exit(void)
1069 if (connection == NULL)
1072 dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1074 dbus_connection_unref(connection);