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>
32 #include "supplicant.h"
36 #define IEEE80211_CAP_ESS 0x0001
37 #define IEEE80211_CAP_IBSS 0x0002
38 #define IEEE80211_CAP_PRIVACY 0x0010
40 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 struct supplicant_task *find_task_by_index(int index)
59 for (list = task_list; list; list = list->next) {
60 struct supplicant_task *task = list->data;
62 if (task->ifindex == index)
69 static int get_interface(struct supplicant_task *task)
71 DBusMessage *message, *reply;
77 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
78 SUPPLICANT_INTF, "getInterface");
82 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
85 dbus_error_init(&error);
87 reply = dbus_connection_send_with_reply_and_block(task->conn,
90 if (dbus_error_is_set(&error) == TRUE) {
91 connman_error("%s", error.message);
92 dbus_error_free(&error);
94 connman_error("Failed to get interface");
95 dbus_message_unref(message);
99 dbus_message_unref(message);
101 dbus_error_init(&error);
103 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
104 DBUS_TYPE_INVALID) == FALSE) {
105 if (dbus_error_is_set(&error) == TRUE) {
106 connman_error("%s", error.message);
107 dbus_error_free(&error);
109 connman_error("Wrong arguments for interface");
110 dbus_message_unref(reply);
114 DBG("path %s", path);
116 task->path = g_strdup(path);
117 task->created = FALSE;
119 dbus_message_unref(reply);
124 static int add_interface(struct supplicant_task *task)
126 DBusMessage *message, *reply;
130 DBG("task %p", task);
132 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
133 SUPPLICANT_INTF, "addInterface");
137 dbus_error_init(&error);
139 dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
142 reply = dbus_connection_send_with_reply_and_block(task->conn,
143 message, -1, &error);
145 if (dbus_error_is_set(&error) == TRUE) {
146 connman_error("%s", error.message);
147 dbus_error_free(&error);
149 connman_error("Failed to add interface");
150 dbus_message_unref(message);
154 dbus_message_unref(message);
156 dbus_error_init(&error);
158 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
159 DBUS_TYPE_INVALID) == FALSE) {
160 if (dbus_error_is_set(&error) == TRUE) {
161 connman_error("%s", error.message);
162 dbus_error_free(&error);
164 connman_error("Wrong arguments for interface");
165 dbus_message_unref(reply);
169 DBG("path %s", path);
171 task->path = g_strdup(path);
172 task->created = TRUE;
174 dbus_message_unref(reply);
179 static int remove_interface(struct supplicant_task *task)
181 DBusMessage *message, *reply;
184 DBG("task %p", task);
186 if (task->created == FALSE)
189 message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
190 SUPPLICANT_INTF, "removeInterface");
194 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
197 dbus_error_init(&error);
199 reply = dbus_connection_send_with_reply_and_block(task->conn,
200 message, -1, &error);
202 if (dbus_error_is_set(&error) == TRUE) {
203 connman_error("%s", error.message);
204 dbus_error_free(&error);
206 connman_error("Failed to remove interface");
207 dbus_message_unref(message);
211 dbus_message_unref(message);
213 dbus_message_unref(reply);
218 static int set_ap_scan(struct supplicant_task *task)
220 DBusMessage *message, *reply;
224 DBG("task %p", task);
226 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
227 SUPPLICANT_INTF ".Interface", "setAPScan");
231 dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
234 dbus_error_init(&error);
236 reply = dbus_connection_send_with_reply_and_block(task->conn,
237 message, -1, &error);
239 if (dbus_error_is_set(&error) == TRUE) {
240 connman_error("%s", error.message);
241 dbus_error_free(&error);
243 connman_error("Failed to set AP scan");
244 dbus_message_unref(message);
248 dbus_message_unref(message);
250 dbus_message_unref(reply);
255 static int add_network(struct supplicant_task *task)
257 DBusMessage *message, *reply;
261 DBG("task %p", task);
263 if (task->network != NULL)
266 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
267 SUPPLICANT_INTF ".Interface", "addNetwork");
271 dbus_error_init(&error);
273 reply = dbus_connection_send_with_reply_and_block(task->conn,
274 message, -1, &error);
276 if (dbus_error_is_set(&error) == TRUE) {
277 connman_error("%s", error.message);
278 dbus_error_free(&error);
280 connman_error("Failed to add network");
281 dbus_message_unref(message);
285 dbus_message_unref(message);
287 dbus_error_init(&error);
289 if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
290 DBUS_TYPE_INVALID) == FALSE) {
291 if (dbus_error_is_set(&error) == TRUE) {
292 connman_error("%s", error.message);
293 dbus_error_free(&error);
295 connman_error("Wrong arguments for network");
296 dbus_message_unref(reply);
300 DBG("path %s", path);
302 task->network = g_strdup(path);
304 dbus_message_unref(reply);
309 static int remove_network(struct supplicant_task *task)
311 DBusMessage *message, *reply;
314 DBG("task %p", task);
316 if (task->network == NULL)
319 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
320 SUPPLICANT_INTF ".Interface", "removeNetwork");
324 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->network,
327 dbus_error_init(&error);
329 reply = dbus_connection_send_with_reply_and_block(task->conn,
330 message, -1, &error);
332 if (dbus_error_is_set(&error) == TRUE) {
333 connman_error("%s", error.message);
334 dbus_error_free(&error);
336 connman_error("Failed to remove network");
337 dbus_message_unref(message);
341 dbus_message_unref(message);
343 dbus_message_unref(reply);
345 g_free(task->network);
346 task->network = NULL;
351 static int select_network(struct supplicant_task *task)
353 DBusMessage *message, *reply;
356 DBG("task %p", task);
358 if (task->network == NULL)
361 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
362 SUPPLICANT_INTF ".Interface", "selectNetwork");
366 dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->network,
369 dbus_error_init(&error);
371 reply = dbus_connection_send_with_reply_and_block(task->conn,
372 message, -1, &error);
374 if (dbus_error_is_set(&error) == TRUE) {
375 connman_error("%s", error.message);
376 dbus_error_free(&error);
378 connman_error("Failed to select network");
379 dbus_message_unref(message);
383 dbus_message_unref(message);
385 dbus_message_unref(reply);
390 static int enable_network(struct supplicant_task *task)
392 DBusMessage *message, *reply;
395 DBG("task %p", task);
397 if (task->network == NULL)
400 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
401 SUPPLICANT_INTF ".Network", "enable");
405 dbus_error_init(&error);
407 reply = dbus_connection_send_with_reply_and_block(task->conn,
408 message, -1, &error);
410 if (dbus_error_is_set(&error) == TRUE) {
411 connman_error("%s", error.message);
412 dbus_error_free(&error);
414 connman_error("Failed to enable network");
415 dbus_message_unref(message);
419 dbus_message_unref(message);
421 dbus_message_unref(reply);
426 static int disable_network(struct supplicant_task *task)
428 DBusMessage *message, *reply;
431 DBG("task %p", task);
433 if (task->network == NULL)
436 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
437 SUPPLICANT_INTF ".Network", "disable");
441 dbus_error_init(&error);
443 reply = dbus_connection_send_with_reply_and_block(task->conn,
444 message, -1, &error);
446 if (dbus_error_is_set(&error) == TRUE) {
447 connman_error("%s", error.message);
448 dbus_error_free(&error);
450 connman_error("Failed to disable network");
451 dbus_message_unref(message);
455 dbus_message_unref(message);
457 dbus_message_unref(reply);
462 static void append_entry(DBusMessageIter *dict,
463 const char *key, int type, void *val)
465 DBusMessageIter entry, value;
466 const char *signature;
468 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
471 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
474 case DBUS_TYPE_STRING:
475 signature = DBUS_TYPE_STRING_AS_STRING;
477 case DBUS_TYPE_UINT16:
478 signature = DBUS_TYPE_UINT16_AS_STRING;
481 signature = DBUS_TYPE_VARIANT_AS_STRING;
485 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
487 dbus_message_iter_append_basic(&value, type, val);
488 dbus_message_iter_close_container(&entry, &value);
490 dbus_message_iter_close_container(dict, &entry);
493 static int set_network(struct supplicant_task *task, const char *network,
494 const char *passphrase)
496 DBusMessage *message, *reply;
497 DBusMessageIter array, dict;
500 DBG("task %p", task);
502 if (task->network == NULL)
505 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
506 SUPPLICANT_INTF ".Network", "set");
510 dbus_message_iter_init_append(message, &array);
512 dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
513 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
514 DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
515 DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
517 append_entry(&dict, "ssid", DBUS_TYPE_STRING, &network);
519 if (passphrase && strlen(passphrase) > 0) {
520 const char *key_mgmt = "WPA-PSK";
521 append_entry(&dict, "key_mgmt", DBUS_TYPE_STRING, &key_mgmt);
522 append_entry(&dict, "psk", DBUS_TYPE_STRING, &passphrase);
524 const char *key_mgmt = "NONE";
525 append_entry(&dict, "key_mgmt", DBUS_TYPE_STRING, &key_mgmt);
528 dbus_message_iter_close_container(&array, &dict);
530 dbus_error_init(&error);
532 reply = dbus_connection_send_with_reply_and_block(task->conn,
533 message, -1, &error);
535 if (dbus_error_is_set(&error) == TRUE) {
536 connman_error("%s", error.message);
537 dbus_error_free(&error);
539 connman_error("Failed to set network options");
540 dbus_message_unref(message);
544 dbus_message_unref(message);
546 dbus_message_unref(reply);
551 static int initiate_scan(struct supplicant_task *task)
553 DBusMessage *message;
554 DBusPendingCall *call;
556 DBG("task %p", task);
558 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
559 SUPPLICANT_INTF ".Interface", "scan");
563 if (dbus_connection_send_with_reply(task->conn, message,
564 &call, TIMEOUT) == FALSE) {
565 connman_error("Failed to initiate scan");
566 dbus_message_unref(message);
570 dbus_message_unref(message);
575 static void extract_ssid(struct supplicant_network *network,
576 DBusMessageIter *value)
578 DBusMessageIter array;
582 dbus_message_iter_recurse(value, &array);
583 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
585 network->identifier = g_strdup((char *) ssid);
588 static void extract_wpaie(struct supplicant_network *network,
589 DBusMessageIter *value)
591 DBusMessageIter array;
595 dbus_message_iter_recurse(value, &array);
596 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
599 network->has_wpa = TRUE;
602 static void extract_rsnie(struct supplicant_network *network,
603 DBusMessageIter *value)
605 DBusMessageIter array;
609 dbus_message_iter_recurse(value, &array);
610 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
613 network->has_rsn = TRUE;
616 static void extract_capabilites(struct supplicant_network *network,
617 DBusMessageIter *value)
621 dbus_message_iter_get_basic(value, &capabilities);
623 network->capabilities = capabilities;
625 if (capabilities & IEEE80211_CAP_PRIVACY)
626 network->has_wep = TRUE;
629 static void properties_reply(DBusPendingCall *call, void *user_data)
631 struct supplicant_task *task = user_data;
632 struct supplicant_network *network;
634 DBusMessageIter array, dict;
636 DBG("task %p", task);
638 reply = dbus_pending_call_steal_reply(call);
640 network = g_try_new0(struct supplicant_network, 1);
644 dbus_message_iter_init(reply, &array);
646 dbus_message_iter_recurse(&array, &dict);
648 while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
649 DBusMessageIter entry, value;
652 dbus_message_iter_recurse(&dict, &entry);
653 dbus_message_iter_get_basic(&entry, &key);
655 dbus_message_iter_next(&entry);
657 dbus_message_iter_recurse(&entry, &value);
659 //type = dbus_message_iter_get_arg_type(&value);
660 //dbus_message_iter_get_basic(&value, &val);
662 if (g_str_equal(key, "ssid") == TRUE)
663 extract_ssid(network, &value);
664 else if (g_str_equal(key, "wpaie") == TRUE)
665 extract_wpaie(network, &value);
666 else if (g_str_equal(key, "rsnie") == TRUE)
667 extract_rsnie(network, &value);
668 else if (g_str_equal(key, "capabilities") == TRUE)
669 extract_capabilites(network, &value);
671 dbus_message_iter_next(&dict);
674 if (task->callback && task->callback->scan_result)
675 task->callback->scan_result(task->element, network);
680 dbus_message_unref(reply);
683 static int get_network_properties(struct supplicant_task *task,
686 DBusMessage *message;
687 DBusPendingCall *call;
689 message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
690 SUPPLICANT_INTF ".BSSID",
695 if (dbus_connection_send_with_reply(task->conn, message,
696 &call, TIMEOUT) == FALSE) {
697 connman_error("Failed to get network properties");
698 dbus_message_unref(message);
702 dbus_pending_call_set_notify(call, properties_reply, task, NULL);
704 dbus_message_unref(message);
709 static void scan_results_reply(DBusPendingCall *call, void *user_data)
711 struct supplicant_task *task = user_data;
717 DBG("task %p", task);
719 reply = dbus_pending_call_steal_reply(call);
721 dbus_error_init(&error);
723 if (dbus_message_get_args(reply, &error,
724 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
725 &results, &num_results,
726 DBUS_TYPE_INVALID) == FALSE) {
727 if (dbus_error_is_set(&error) == TRUE) {
728 connman_error("%s", error.message);
729 dbus_error_free(&error);
731 connman_error("Wrong arguments for scan result");
735 for (i = 0; i < num_results; i++)
736 get_network_properties(task, results[i]);
741 dbus_message_unref(reply);
744 static int scan_results_available(struct supplicant_task *task)
746 DBusMessage *message;
747 DBusPendingCall *call;
749 DBG("task %p", task);
751 message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
752 SUPPLICANT_INTF ".Interface",
757 if (dbus_connection_send_with_reply(task->conn, message,
758 &call, TIMEOUT) == FALSE) {
759 connman_error("Failed to request scan result");
760 dbus_message_unref(message);
764 dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
766 dbus_message_unref(message);
771 static void state_change(struct supplicant_task *task, DBusMessage *msg)
774 const char *state, *previous;
776 dbus_error_init(&error);
778 if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &state,
779 DBUS_TYPE_STRING, &previous,
780 DBUS_TYPE_INVALID) == FALSE) {
781 if (dbus_error_is_set(&error) == TRUE) {
782 connman_error("%s", error.message);
783 dbus_error_free(&error);
785 connman_error("Wrong arguments for state change");
789 DBG("state %s ==> %s", previous, state);
791 if (g_str_equal(state, "INACTIVE") == TRUE)
792 task->state = STATE_INACTIVE;
793 else if (g_str_equal(state, "SCANNING") == TRUE)
794 task->state = STATE_SCANNING;
795 else if (g_str_equal(state, "ASSOCIATING") == TRUE)
796 task->state = STATE_ASSOCIATING;
797 else if (g_str_equal(state, "ASSOCIATED") == TRUE)
798 task->state = STATE_ASSOCIATED;
799 else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
800 task->state = STATE_4WAY_HANDSHAKE;
801 else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
802 task->state = STATE_4WAY_HANDSHAKE;
803 else if (g_str_equal(state, "COMPLETED") == TRUE)
804 task->state = STATE_COMPLETED;
805 else if (g_str_equal(state, "DISCONNECTED") == TRUE)
806 task->state = STATE_DISCONNECTED;
808 if (task->callback && task->callback->state_change)
809 task->callback->state_change(task->element, task->state);
811 switch (task->state) {
812 case STATE_COMPLETED:
815 case STATE_DISCONNECTED:
823 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
824 DBusMessage *msg, void *data)
826 struct supplicant_task *task = data;
829 if (dbus_message_has_interface(msg,
830 SUPPLICANT_INTF ".Interface") == FALSE)
831 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
833 member = dbus_message_get_member(msg);
835 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
837 DBG("task %p member %s", task, member);
839 if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
840 scan_results_available(task);
841 else if (g_str_equal(member, "StateChange") == TRUE)
842 state_change(task, msg);
844 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
847 static int add_filter(struct supplicant_task *task)
852 if (dbus_connection_add_filter(task->conn,
853 supplicant_filter, task, NULL) == FALSE)
856 filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
857 SUPPLICANT_INTF, task->path);
859 DBG("filter %s", filter);
861 dbus_error_init(&error);
863 dbus_bus_add_match(task->conn, filter, &error);
867 if (dbus_error_is_set(&error) == TRUE) {
868 connman_error("Can't add match: %s", error.message);
869 dbus_error_free(&error);
875 static int remove_filter(struct supplicant_task *task)
880 filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
881 SUPPLICANT_INTF, task->path);
883 DBG("filter %s", filter);
885 dbus_error_init(&error);
887 dbus_bus_add_match(task->conn, filter, &error);
891 if (dbus_error_is_set(&error) == TRUE) {
892 connman_error("Can't add match: %s", error.message);
893 dbus_error_free(&error);
896 dbus_connection_remove_filter(task->conn, supplicant_filter, task);
901 int __supplicant_start(struct connman_element *element,
902 struct supplicant_callback *callback)
904 struct supplicant_task *task;
907 DBG("element %p name %s", element, element->name);
909 task = g_try_new0(struct supplicant_task, 1);
913 task->ifindex = element->index;
914 task->ifname = inet_index2name(element->index);
915 task->element = element;
916 task->callback = callback;
918 if (task->ifname == NULL) {
923 task->conn = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
924 if (task->conn == NULL) {
929 task->created = FALSE;
930 task->state = STATE_INACTIVE;
932 g_static_mutex_lock(&task_mutex);
933 task_list = g_slist_append(task_list, task);
934 g_static_mutex_unlock(&task_mutex);
936 err = get_interface(task);
938 err = add_interface(task);
952 int __supplicant_stop(struct connman_element *element)
954 struct supplicant_task *task;
956 DBG("element %p name %s", element, element->name);
958 task = find_task_by_index(element->index);
962 g_static_mutex_lock(&task_mutex);
963 task_list = g_slist_remove(task_list, task);
964 g_static_mutex_unlock(&task_mutex);
966 disable_network(task);
968 remove_network(task);
972 remove_interface(task);
974 dbus_connection_unref(task->conn);
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, const char *ssid)
1013 struct supplicant_task *task;
1015 DBG("element %p name %s", element, element->name);
1017 task = find_task_by_index(element->index);
1023 select_network(task);
1024 disable_network(task);
1026 set_network(task, ssid, NULL);
1028 enable_network(task);
1033 int __supplicant_disconnect(struct connman_element *element)
1035 struct supplicant_task *task;
1037 DBG("element %p name %s", element, element->name);
1039 task = find_task_by_index(element->index);
1043 disable_network(task);
1045 remove_network(task);