c11b885850746c07a189d683bdeb84edfa687164
[connman] / plugins / supplicant.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #include <gdbus.h>
30
31 #define CONNMAN_API_SUBJECT_TO_CHANGE
32 #include <connman/log.h>
33 #include <connman/dbus.h>
34
35 #include "inet.h"
36 #include "supplicant.h"
37
38 #define TIMEOUT 5000
39
40 #define IEEE80211_CAP_ESS       0x0001
41 #define IEEE80211_CAP_IBSS      0x0002
42 #define IEEE80211_CAP_PRIVACY   0x0010
43
44 static GSList *driver_list = NULL;
45
46 static void process_state_change(struct connman_device *device,
47                                                 enum supplicant_state state)
48 {
49         GSList *list;
50
51         for (list = driver_list; list; list = list->next) {
52                 struct supplicant_driver *driver = list->data;
53
54                 if (driver->state_change)
55                         driver->state_change(device, state);
56         }
57 }
58
59 static void process_clear_results(struct connman_device *device)
60 {
61         GSList *list;
62
63         for (list = driver_list; list; list = list->next) {
64                 struct supplicant_driver *driver = list->data;
65
66                 if (driver->clear_results)
67                         driver->clear_results(device);
68         }
69 }
70
71 static void process_scan_result(struct connman_device *device,
72                                         struct supplicant_network *network)
73 {
74         GSList *list;
75
76         for (list = driver_list; list; list = list->next) {
77                 struct supplicant_driver *driver = list->data;
78
79                 if (driver->scan_result)
80                         driver->scan_result(device, network);
81         }
82 }
83
84 struct supplicant_task {
85         int ifindex;
86         gchar *ifname;
87         struct connman_device *device;
88         gchar *path;
89         gboolean created;
90         gchar *network;
91         enum supplicant_state state;
92 };
93
94 static GSList *task_list = NULL;
95
96 static DBusConnection *connection;
97
98 static void free_task(struct supplicant_task *task)
99 {
100         DBG("task %p", task);
101
102         g_free(task->ifname);
103         g_free(task->path);
104         g_free(task);
105 }
106
107 static struct supplicant_task *find_task_by_index(int index)
108 {
109         GSList *list;
110
111         for (list = task_list; list; list = list->next) {
112                 struct supplicant_task *task = list->data;
113
114                 if (task->ifindex == index)
115                         return task;
116         }
117
118         return NULL;
119 }
120
121 static struct supplicant_task *find_task_by_path(const char *path)
122 {
123         GSList *list;
124
125         for (list = task_list; list; list = list->next) {
126                 struct supplicant_task *task = list->data;
127
128                 if (g_str_equal(task->path, path) == TRUE)
129                         return task;
130         }
131
132         return NULL;
133 }
134
135 static void add_interface_reply(DBusPendingCall *call, void *user_data)
136 {
137         struct supplicant_task *task = user_data;
138         DBusMessage *reply;
139         DBusError error;
140         const char *path;
141
142         DBG("task %p", task);
143
144         reply = dbus_pending_call_steal_reply(call);
145         if (reply == NULL)
146                 return;
147
148         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
149                 goto done;
150
151         dbus_error_init(&error);
152
153         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
154                                                 DBUS_TYPE_INVALID) == FALSE) {
155                 if (dbus_error_is_set(&error) == TRUE) {
156                         connman_error("%s", error.message);
157                         dbus_error_free(&error);
158                 } else
159                         connman_error("Wrong arguments for add interface");
160                 goto done;
161         }
162
163         DBG("path %s", path);
164
165         task->path = g_strdup(path);
166         task->created = TRUE;
167
168         connman_device_set_powered(task->device, TRUE);
169
170 done:
171         dbus_message_unref(reply);
172 }
173
174 static int add_interface(struct supplicant_task *task)
175 {
176         DBusMessage *message;
177         DBusPendingCall *call;
178
179         DBG("task %p", task);
180
181         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
182                                         SUPPLICANT_INTF, "addInterface");
183         if (message == NULL)
184                 return -ENOMEM;
185
186         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
187                                                         DBUS_TYPE_INVALID);
188
189         if (dbus_connection_send_with_reply(connection, message,
190                                                 &call, TIMEOUT) == FALSE) {
191                 connman_error("Failed to add interface");
192                 dbus_message_unref(message);
193                 return -EIO;
194         }
195
196         dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
197
198         dbus_message_unref(message);
199
200         return -EINPROGRESS;
201 }
202
203 static void get_interface_reply(DBusPendingCall *call, void *user_data)
204 {
205         struct supplicant_task *task = user_data;
206         DBusMessage *reply;
207         DBusError error;
208         const char *path;
209
210         DBG("task %p", task);
211
212         reply = dbus_pending_call_steal_reply(call);
213         if (reply == NULL)
214                 return;
215
216         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
217                 add_interface(task);
218                 goto done;
219         }
220
221         dbus_error_init(&error);
222
223         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
224                                                 DBUS_TYPE_INVALID) == FALSE) {
225                 if (dbus_error_is_set(&error) == TRUE) {
226                         connman_error("%s", error.message);
227                         dbus_error_free(&error);
228                 } else
229                         connman_error("Wrong arguments for get interface");
230                 goto done;
231         }
232
233         DBG("path %s", path);
234
235         task->path = g_strdup(path);
236         task->created = FALSE;
237
238         connman_device_set_powered(task->device, TRUE);
239
240 done:
241         dbus_message_unref(reply);
242 }
243
244 static int create_interface(struct supplicant_task *task)
245 {
246         DBusMessage *message;
247         DBusPendingCall *call;
248
249         DBG("task %p", task);
250
251         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
252                                         SUPPLICANT_INTF, "getInterface");
253         if (message == NULL)
254                 return -ENOMEM;
255
256         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
257                                                         DBUS_TYPE_INVALID);
258
259         if (dbus_connection_send_with_reply(connection, message,
260                                                 &call, TIMEOUT) == FALSE) {
261                 connman_error("Failed to get interface");
262                 dbus_message_unref(message);
263                 return -EIO;
264         }
265
266         dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
267
268         dbus_message_unref(message);
269
270         return -EINPROGRESS;
271 }
272
273 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
274 {
275         struct supplicant_task *task = user_data;
276         DBusMessage *reply;
277
278         DBG("task %p", task);
279
280         reply = dbus_pending_call_steal_reply(call);
281
282         connman_device_set_powered(task->device, FALSE);
283
284         free_task(task);
285
286         dbus_message_unref(reply);
287 }
288
289 static int remove_interface(struct supplicant_task *task)
290 {
291         DBusMessage *message;
292         DBusPendingCall *call;
293
294         DBG("task %p", task);
295
296         if (task->created == FALSE) {
297                 connman_device_set_powered(task->device, FALSE);
298                 return 0;
299         }
300
301         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
302                                         SUPPLICANT_INTF, "removeInterface");
303         if (message == NULL)
304                 return -ENOMEM;
305
306         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
307                                                         DBUS_TYPE_INVALID);
308
309         if (dbus_connection_send_with_reply(connection, message,
310                                                 &call, TIMEOUT) == FALSE) {
311                 connman_error("Failed to remove interface");
312                 dbus_message_unref(message);
313                 return -EIO;
314         }
315
316         dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
317
318         dbus_message_unref(message);
319
320         return -EINPROGRESS;
321 }
322
323 #if 0
324 static int set_ap_scan(struct supplicant_task *task)
325 {
326         DBusMessage *message, *reply;
327         DBusError error;
328         guint32 ap_scan = 1;
329
330         DBG("task %p", task);
331
332         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
333                                 SUPPLICANT_INTF ".Interface", "setAPScan");
334         if (message == NULL)
335                 return -ENOMEM;
336
337         dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
338                                                         DBUS_TYPE_INVALID);
339
340         dbus_error_init(&error);
341
342         reply = dbus_connection_send_with_reply_and_block(connection,
343                                                         message, -1, &error);
344         if (reply == NULL) {
345                 if (dbus_error_is_set(&error) == TRUE) {
346                         connman_error("%s", error.message);
347                         dbus_error_free(&error);
348                 } else
349                         connman_error("Failed to set AP scan");
350                 dbus_message_unref(message);
351                 return -EIO;
352         }
353
354         dbus_message_unref(message);
355
356         dbus_message_unref(reply);
357
358         return 0;
359 }
360 #endif
361
362 static int add_network(struct supplicant_task *task)
363 {
364         DBusMessage *message, *reply;
365         DBusError error;
366         const char *path;
367
368         DBG("task %p", task);
369
370         if (task->network != NULL)
371                 return -EALREADY;
372
373         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
374                                 SUPPLICANT_INTF ".Interface", "addNetwork");
375         if (message == NULL)
376                 return -ENOMEM;
377
378         dbus_error_init(&error);
379
380         reply = dbus_connection_send_with_reply_and_block(connection,
381                                                         message, -1, &error);
382         if (reply == NULL) {
383                 if (dbus_error_is_set(&error) == TRUE) {
384                         connman_error("%s", error.message);
385                         dbus_error_free(&error);
386                 } else
387                         connman_error("Failed to add network");
388                 dbus_message_unref(message);
389                 return -EIO;
390         }
391
392         dbus_message_unref(message);
393
394         dbus_error_init(&error);
395
396         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
397                                                 DBUS_TYPE_INVALID) == FALSE) {
398                 if (dbus_error_is_set(&error) == TRUE) {
399                         connman_error("%s", error.message);
400                         dbus_error_free(&error);
401                 } else
402                         connman_error("Wrong arguments for network");
403                 dbus_message_unref(reply);
404                 return -EIO;
405         }
406
407         DBG("path %s", path);
408
409         task->network = g_strdup(path);
410
411         dbus_message_unref(reply);
412
413         return 0;
414 }
415
416 static int remove_network(struct supplicant_task *task)
417 {
418         DBusMessage *message, *reply;
419         DBusError error;
420
421         DBG("task %p", task);
422
423         if (task->network == NULL)
424                 return -EINVAL;
425
426         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
427                                 SUPPLICANT_INTF ".Interface", "removeNetwork");
428         if (message == NULL)
429                 return -ENOMEM;
430
431         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->network,
432                                                         DBUS_TYPE_INVALID);
433
434         dbus_error_init(&error);
435
436         reply = dbus_connection_send_with_reply_and_block(connection,
437                                                         message, -1, &error);
438         if (reply == NULL) {
439                 if (dbus_error_is_set(&error) == TRUE) {
440                         connman_error("%s", error.message);
441                         dbus_error_free(&error);
442                 } else
443                         connman_error("Failed to remove network");
444                 dbus_message_unref(message);
445                 return -EIO;
446         }
447
448         dbus_message_unref(message);
449
450         dbus_message_unref(reply);
451
452         g_free(task->network);
453         task->network = NULL;
454
455         return 0;
456 }
457
458 static int select_network(struct supplicant_task *task)
459 {
460         DBusMessage *message, *reply;
461         DBusError error;
462
463         DBG("task %p", task);
464
465         if (task->network == NULL)
466                 return -EINVAL;
467
468         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
469                                 SUPPLICANT_INTF ".Interface", "selectNetwork");
470         if (message == NULL)
471                 return -ENOMEM;
472
473         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->network,
474                                                         DBUS_TYPE_INVALID);
475
476         dbus_error_init(&error);
477
478         reply = dbus_connection_send_with_reply_and_block(connection,
479                                                         message, -1, &error);
480         if (reply == NULL) {
481                 if (dbus_error_is_set(&error) == TRUE) {
482                         connman_error("%s", error.message);
483                         dbus_error_free(&error);
484                 } else
485                         connman_error("Failed to select network");
486                 dbus_message_unref(message);
487                 return -EIO;
488         }
489
490         dbus_message_unref(message);
491
492         dbus_message_unref(reply);
493
494         return 0;
495 }
496
497 static int enable_network(struct supplicant_task *task)
498 {
499         DBusMessage *message, *reply;
500         DBusError error;
501
502         DBG("task %p", task);
503
504         if (task->network == NULL)
505                 return -EINVAL;
506
507         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
508                                         SUPPLICANT_INTF ".Network", "enable");
509         if (message == NULL)
510                 return -ENOMEM;
511
512         dbus_error_init(&error);
513
514         reply = dbus_connection_send_with_reply_and_block(connection,
515                                                         message, -1, &error);
516         if (reply == NULL) {
517                 if (dbus_error_is_set(&error) == TRUE) {
518                         connman_error("%s", error.message);
519                         dbus_error_free(&error);
520                 } else
521                         connman_error("Failed to enable network");
522                 dbus_message_unref(message);
523                 return -EIO;
524         }
525
526         dbus_message_unref(message);
527
528         dbus_message_unref(reply);
529
530         return 0;
531 }
532
533 static int disable_network(struct supplicant_task *task)
534 {
535         DBusMessage *message, *reply;
536         DBusError error;
537
538         DBG("task %p", task);
539
540         if (task->network == NULL)
541                 return -EINVAL;
542
543         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
544                                         SUPPLICANT_INTF ".Network", "disable");
545         if (message == NULL)
546                 return -ENOMEM;
547
548         dbus_error_init(&error);
549
550         reply = dbus_connection_send_with_reply_and_block(connection,
551                                                         message, -1, &error);
552         if (reply == NULL) {
553                 if (dbus_error_is_set(&error) == TRUE) {
554                         connman_error("%s", error.message);
555                         dbus_error_free(&error);
556                 } else
557                         connman_error("Failed to disable network");
558                 dbus_message_unref(message);
559                 return -EIO;
560         }
561
562         dbus_message_unref(message);
563
564         dbus_message_unref(reply);
565
566         return 0;
567 }
568
569 static int set_network(struct supplicant_task *task,
570                                 const unsigned char *network, int len,
571                                 const char *security, const char *passphrase)
572 {
573         DBusMessage *message, *reply;
574         DBusMessageIter array, dict;
575         DBusError error;
576
577         DBG("task %p", task);
578
579         if (task->network == NULL)
580                 return -EINVAL;
581
582         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
583                                         SUPPLICANT_INTF ".Network", "set");
584         if (message == NULL)
585                 return -ENOMEM;
586
587         dbus_message_iter_init_append(message, &array);
588
589         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
590                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
591                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
592                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
593
594         connman_dbus_dict_append_array(&dict, "ssid",
595                                         DBUS_TYPE_BYTE, &network, len);
596
597         if (g_ascii_strcasecmp(security, "wpa") == 0 ||
598                                 g_ascii_strcasecmp(security, "wpa2") == 0) {
599                 const char *key_mgmt = "WPA-PSK";
600                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
601                                                 DBUS_TYPE_STRING, &key_mgmt);
602
603                 if (passphrase && strlen(passphrase) > 0)
604                         connman_dbus_dict_append_variant(&dict, "psk",
605                                                 DBUS_TYPE_STRING, &passphrase);
606         } else if (g_ascii_strcasecmp(security, "wep") == 0) {
607                 const char *key_mgmt = "NONE", *index = "0";
608                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
609                                                 DBUS_TYPE_STRING, &key_mgmt);
610
611                 if (passphrase) {
612                         int size = strlen(passphrase);
613                         if (size == 10 || size == 26) {
614                                 unsigned char *key = malloc(13);
615                                 char tmp[3];
616                                 int i;
617                                 memset(tmp, 0, sizeof(tmp));
618                                 if (key == NULL)
619                                         size = 0;
620                                 for (i = 0; i < size / 2; i++) {
621                                         memcpy(tmp, passphrase + (i * 2), 2);
622                                         key[i] = (unsigned char) strtol(tmp,
623                                                                 NULL, 16);
624                                 }
625                                 connman_dbus_dict_append_array(&dict,
626                                                 "wep_key0", DBUS_TYPE_BYTE,
627                                                         &key, size / 2);
628                                 free(key);
629                         } else
630                                 connman_dbus_dict_append_variant(&dict,
631                                                 "wep_key0", DBUS_TYPE_STRING,
632                                                                 &passphrase);
633                         connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
634                                                 DBUS_TYPE_STRING, &index);
635                 }
636         } else {
637                 const char *key_mgmt = "NONE";
638                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
639                                                 DBUS_TYPE_STRING, &key_mgmt);
640         }
641
642         dbus_message_iter_close_container(&array, &dict);
643
644         dbus_error_init(&error);
645
646         reply = dbus_connection_send_with_reply_and_block(connection,
647                                                         message, -1, &error);
648         if (reply == NULL) {
649                 if (dbus_error_is_set(&error) == TRUE) {
650                         connman_error("%s", error.message);
651                         dbus_error_free(&error);
652                 } else
653                         connman_error("Failed to set network options");
654                 dbus_message_unref(message);
655                 return -EIO;
656         }
657
658         dbus_message_unref(message);
659
660         dbus_message_unref(reply);
661
662         return 0;
663 }
664
665 static int initiate_scan(struct supplicant_task *task)
666 {
667         DBusMessage *message;
668         DBusPendingCall *call;
669
670         DBG("task %p", task);
671
672         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
673                                         SUPPLICANT_INTF ".Interface", "scan");
674         if (message == NULL)
675                 return -ENOMEM;
676
677         if (dbus_connection_send_with_reply(connection, message,
678                                                 &call, TIMEOUT) == FALSE) {
679                 connman_error("Failed to initiate scan");
680                 dbus_message_unref(message);
681                 return -EIO;
682         }
683
684         dbus_message_unref(message);
685
686         return 0;
687 }
688
689 static void extract_ssid(struct supplicant_network *network,
690                                                 DBusMessageIter *value)
691 {
692         DBusMessageIter array;
693         unsigned char *ssid;
694         int ssid_len;
695
696         dbus_message_iter_recurse(value, &array);
697         dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
698
699         if (ssid_len < 1)
700                 return;
701
702         network->ssid = g_try_malloc(ssid_len);
703         if (network->ssid == NULL)
704                 return;
705
706         memcpy(network->ssid, ssid, ssid_len);
707         network->ssid_len = ssid_len;
708
709         network->identifier = g_try_malloc0(ssid_len + 1);
710         if (network->identifier == NULL)
711                 return;
712
713         memcpy(network->identifier, ssid, ssid_len);
714 }
715
716 static void extract_wpaie(struct supplicant_network *network,
717                                                 DBusMessageIter *value)
718 {
719         DBusMessageIter array;
720         unsigned char *ie;
721         int ie_len;
722
723         dbus_message_iter_recurse(value, &array);
724         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
725
726         if (ie_len > 0)
727                 network->has_wpa = TRUE;
728 }
729
730 static void extract_rsnie(struct supplicant_network *network,
731                                                 DBusMessageIter *value)
732 {
733         DBusMessageIter array;
734         unsigned char *ie;
735         int ie_len;
736
737         dbus_message_iter_recurse(value, &array);
738         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
739
740         if (ie_len > 0)
741                 network->has_rsn = TRUE;
742 }
743
744 static void extract_capabilites(struct supplicant_network *network,
745                                                 DBusMessageIter *value)
746 {
747         dbus_message_iter_get_basic(value, &network->capabilities);
748
749         if (network->capabilities & IEEE80211_CAP_ESS)
750                 network->adhoc = FALSE;
751         else if (network->capabilities & IEEE80211_CAP_IBSS)
752                 network->adhoc = TRUE;
753
754         if (network->capabilities & IEEE80211_CAP_PRIVACY)
755                 network->has_wep = TRUE;
756 }
757
758 static void properties_reply(DBusPendingCall *call, void *user_data)
759 {
760         struct supplicant_task *task = user_data;
761         struct supplicant_network *network;
762         DBusMessage *reply;
763         DBusMessageIter array, dict;
764
765         DBG("task %p", task);
766
767         reply = dbus_pending_call_steal_reply(call);
768
769         network = g_try_new0(struct supplicant_network, 1);
770         if (network == NULL)
771                 goto done;
772
773         dbus_message_iter_init(reply, &array);
774
775         dbus_message_iter_recurse(&array, &dict);
776
777         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
778                 DBusMessageIter entry, value;
779                 const char *key;
780
781                 dbus_message_iter_recurse(&dict, &entry);
782                 dbus_message_iter_get_basic(&entry, &key);
783
784                 dbus_message_iter_next(&entry);
785
786                 dbus_message_iter_recurse(&entry, &value);
787
788                 //type = dbus_message_iter_get_arg_type(&value);
789                 //dbus_message_iter_get_basic(&value, &val);
790
791                 /* 
792                  * bssid        : a (97)
793                  * ssid         : a (97)
794                  * wpaie        : a (97)
795                  * rsnie        : a (97)
796                  * frequency    : i (105)
797                  * capabilities : q (113)
798                  * quality      : i (105)
799                  * noise        : i (105)
800                  * level        : i (105)
801                  * maxrate      : i (105)
802                  */
803
804                 if (g_str_equal(key, "ssid") == TRUE)
805                         extract_ssid(network, &value);
806                 else if (g_str_equal(key, "wpaie") == TRUE)
807                         extract_wpaie(network, &value);
808                 else if (g_str_equal(key, "rsnie") == TRUE)
809                         extract_rsnie(network, &value);
810                 else if (g_str_equal(key, "capabilities") == TRUE)
811                         extract_capabilites(network, &value);
812                 else if (g_str_equal(key, "quality") == TRUE)
813                         dbus_message_iter_get_basic(&value, &network->quality);
814                 else if (g_str_equal(key, "noise") == TRUE)
815                         dbus_message_iter_get_basic(&value, &network->noise);
816                 else if (g_str_equal(key, "level") == TRUE)
817                         dbus_message_iter_get_basic(&value, &network->level);
818                 else if (g_str_equal(key, "maxrate") == TRUE)
819                         dbus_message_iter_get_basic(&value, &network->maxrate);
820
821
822                 dbus_message_iter_next(&dict);
823         }
824
825         process_scan_result(task->device, network);
826
827         g_free(network->identifier);
828         g_free(network->ssid);
829         g_free(network);
830
831 done:
832         dbus_message_unref(reply);
833 }
834
835 static int get_network_properties(struct supplicant_task *task,
836                                                         const char *path)
837 {
838         DBusMessage *message;
839         DBusPendingCall *call;
840
841         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
842                                                 SUPPLICANT_INTF ".BSSID",
843                                                                 "properties");
844         if (message == NULL)
845                 return -ENOMEM;
846
847         if (dbus_connection_send_with_reply(connection, message,
848                                                 &call, TIMEOUT) == FALSE) {
849                 connman_error("Failed to get network properties");
850                 dbus_message_unref(message);
851                 return -EIO;
852         }
853
854         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
855
856         dbus_message_unref(message);
857
858         return 0;
859 }
860
861 static void scan_results_reply(DBusPendingCall *call, void *user_data)
862 {
863         struct supplicant_task *task = user_data;
864         DBusMessage *reply;
865         DBusError error;
866         char **results;
867         int i, num_results;
868
869         DBG("task %p", task);
870
871         reply = dbus_pending_call_steal_reply(call);
872
873         dbus_error_init(&error);
874
875         if (dbus_message_get_args(reply, &error,
876                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
877                                                 &results, &num_results,
878                                                 DBUS_TYPE_INVALID) == FALSE) {
879                 if (dbus_error_is_set(&error) == TRUE) {
880                         connman_error("%s", error.message);
881                         dbus_error_free(&error);
882                 } else
883                         connman_error("Wrong arguments for scan result");
884                 goto done;
885         }
886
887         process_clear_results(task->device);
888
889         for (i = 0; i < num_results; i++)
890                 get_network_properties(task, results[i]);
891
892         g_strfreev(results);
893
894 done:
895         dbus_message_unref(reply);
896 }
897
898 static int scan_results_available(struct supplicant_task *task)
899 {
900         DBusMessage *message;
901         DBusPendingCall *call;
902
903         DBG("task %p", task);
904
905         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
906                                                 SUPPLICANT_INTF ".Interface",
907                                                         "scanResults");
908         if (message == NULL)
909                 return -ENOMEM;
910
911         if (dbus_connection_send_with_reply(connection, message,
912                                                 &call, TIMEOUT) == FALSE) {
913                 connman_error("Failed to request scan result");
914                 dbus_message_unref(message);
915                 return -EIO;
916         }
917
918         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
919
920         dbus_message_unref(message);
921
922         return 0;
923 }
924
925 static void state_change(struct supplicant_task *task, DBusMessage *msg)
926 {
927         DBusError error;
928         const char *state, *previous;
929
930         dbus_error_init(&error);
931
932         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &state,
933                                                 DBUS_TYPE_STRING, &previous,
934                                                 DBUS_TYPE_INVALID) == FALSE) {
935                 if (dbus_error_is_set(&error) == TRUE) {
936                         connman_error("%s", error.message);
937                         dbus_error_free(&error);
938                 } else
939                         connman_error("Wrong arguments for state change");
940                 return;
941         }
942
943         DBG("state %s ==> %s", previous, state);
944
945         if (g_str_equal(state, "INACTIVE") == TRUE)
946                 task->state = STATE_INACTIVE;
947         else if (g_str_equal(state, "SCANNING") == TRUE)
948                 task->state = STATE_SCANNING;
949         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
950                 task->state = STATE_ASSOCIATING;
951         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
952                 task->state = STATE_ASSOCIATED;
953         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
954                 task->state = STATE_4WAY_HANDSHAKE;
955         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
956                 task->state = STATE_4WAY_HANDSHAKE;
957         else if (g_str_equal(state, "COMPLETED") == TRUE)
958                 task->state = STATE_COMPLETED;
959         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
960                 task->state = STATE_DISCONNECTED;
961
962         process_state_change(task->device, task->state);
963
964         switch (task->state) {
965         case STATE_COMPLETED:
966                 /* carrier on */
967                 break;
968         case STATE_DISCONNECTED:
969                 /* carrier off */
970                 break;
971         default:
972                 break;
973         }
974 }
975
976 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
977                                                 DBusMessage *msg, void *data)
978 {
979         struct supplicant_task *task;
980         const char *member, *path;
981
982         if (dbus_message_has_interface(msg,
983                                 SUPPLICANT_INTF ".Interface") == FALSE)
984                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
985
986         member = dbus_message_get_member(msg);
987         if (member == NULL)
988                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
989
990         path = dbus_message_get_path(msg);
991         if (path == NULL)
992                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
993
994         task = find_task_by_path(path);
995         if (task == NULL)
996                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
997
998         DBG("task %p member %s", task, member);
999
1000         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1001                 scan_results_available(task);
1002         else if (g_str_equal(member, "StateChange") == TRUE)
1003                 state_change(task, msg);
1004
1005         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1006 }
1007
1008 int supplicant_start(struct connman_device *device)
1009 {
1010         struct supplicant_task *task;
1011
1012         DBG("device %p", device);
1013
1014         task = g_try_new0(struct supplicant_task, 1);
1015         if (task == NULL)
1016                 return -ENOMEM;
1017
1018         task->ifindex = connman_device_get_index(device);
1019         task->ifname = inet_index2name(task->ifindex);
1020         task->device = device;
1021
1022         if (task->ifname == NULL) {
1023                 g_free(task);
1024                 return -ENOMEM;
1025         }
1026
1027         task->created = FALSE;
1028         task->state = STATE_INACTIVE;
1029
1030         task_list = g_slist_append(task_list, task);
1031
1032         return create_interface(task);
1033 }
1034
1035 int supplicant_stop(struct connman_device *device)
1036 {
1037         int index = connman_device_get_index(device);
1038         struct supplicant_task *task;
1039
1040         DBG("device %p", device);
1041
1042         task = find_task_by_index(index);
1043         if (task == NULL)
1044                 return -ENODEV;
1045
1046         task_list = g_slist_remove(task_list, task);
1047
1048         disable_network(task);
1049
1050         remove_network(task);
1051
1052         return remove_interface(task);
1053 }
1054
1055 int supplicant_scan(struct connman_device *device)
1056 {
1057         int index = connman_device_get_index(device);
1058         struct supplicant_task *task;
1059         int err;
1060
1061         DBG("device %p", device);
1062
1063         task = find_task_by_index(index);
1064         if (task == NULL)
1065                 return -ENODEV;
1066
1067         switch (task->state) {
1068         case STATE_SCANNING:
1069                 return -EALREADY;
1070         case STATE_ASSOCIATING:
1071         case STATE_ASSOCIATED:
1072         case STATE_4WAY_HANDSHAKE:
1073         case STATE_GROUP_HANDSHAKE:
1074                 return -EBUSY;
1075         default:
1076                 break;
1077         }
1078
1079         err = initiate_scan(task);
1080
1081         return 0;
1082 }
1083
1084 int __supplicant_connect(struct connman_element *element,
1085                                 const unsigned char *ssid, int ssid_len,
1086                                 const char *security, const char *passphrase)
1087 {
1088         struct supplicant_task *task;
1089
1090         DBG("element %p", element);
1091
1092         task = find_task_by_index(element->index);
1093         if (task == NULL)
1094                 return -ENODEV;
1095
1096         add_network(task);
1097
1098         select_network(task);
1099         disable_network(task);
1100
1101         set_network(task, ssid, ssid_len, security, passphrase);
1102
1103         enable_network(task);
1104
1105         return 0;
1106 }
1107
1108 int __supplicant_disconnect(struct connman_element *element)
1109 {
1110         struct supplicant_task *task;
1111
1112         DBG("element %p", element);
1113
1114         task = find_task_by_index(element->index);
1115         if (task == NULL)
1116                 return -ENODEV;
1117
1118         disable_network(task);
1119
1120         remove_network(task);
1121
1122         return 0;
1123 }
1124
1125 static void supplicant_activate(DBusConnection *conn)
1126 {
1127         DBusMessage *message;
1128
1129         DBG("conn %p", conn);
1130
1131         message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1132                                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1133         if (message == NULL)
1134                 return;
1135
1136         dbus_message_set_no_reply(message, TRUE);
1137
1138         dbus_connection_send(conn, message, NULL);
1139
1140         dbus_message_unref(message);
1141 }
1142
1143 static void supplicant_probe(DBusConnection *conn, void *user_data)
1144 {
1145         GSList *list;
1146
1147         DBG("conn %p", conn);
1148
1149         for (list = driver_list; list; list = list->next) {
1150                 struct supplicant_driver *driver = list->data;
1151
1152                 DBG("driver %p name %s", driver, driver->name);
1153
1154                 if (driver->probe)
1155                         driver->probe();
1156         }
1157 }
1158
1159 static void supplicant_remove(DBusConnection *conn, void *user_data)
1160 {
1161         GSList *list;
1162
1163         DBG("conn %p", conn);
1164
1165         for (list = driver_list; list; list = list->next) {
1166                 struct supplicant_driver *driver = list->data;
1167
1168                 DBG("driver %p name %s", driver, driver->name);
1169
1170                 if (driver->remove)
1171                         driver->remove();
1172         }
1173 }
1174
1175 static const char *supplicant_rule = "type=signal,"
1176                                 "interface=" SUPPLICANT_INTF ".Interface";
1177 static guint watch;
1178
1179 static int supplicant_create(void)
1180 {
1181         if (g_slist_length(driver_list) > 0)
1182                 return 0;
1183
1184         connection = connman_dbus_get_connection();
1185         if (connection == NULL)
1186                 return -EIO;
1187
1188         DBG("connection %p", connection);
1189
1190         if (dbus_connection_add_filter(connection,
1191                                 supplicant_filter, NULL, NULL) == FALSE) {
1192                 connection = connman_dbus_get_connection();
1193                 return -EIO;
1194         }
1195
1196         dbus_bus_add_match(connection, supplicant_rule, NULL);
1197         dbus_connection_flush(connection);
1198
1199         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1200                         supplicant_probe, supplicant_remove, NULL, NULL);
1201
1202         return 0;
1203 }
1204
1205 static void supplicant_destroy(void)
1206 {
1207         if (g_slist_length(driver_list) > 0)
1208                 return;
1209
1210         DBG("connection %p", connection);
1211
1212         if (watch > 0)
1213                 g_dbus_remove_watch(connection, watch);
1214
1215         dbus_bus_remove_match(connection, supplicant_rule, NULL);
1216         dbus_connection_flush(connection);
1217
1218         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1219
1220         dbus_connection_unref(connection);
1221         connection = NULL;
1222 }
1223
1224 int supplicant_register(struct supplicant_driver *driver)
1225 {
1226         int err;
1227
1228         DBG("driver %p name %s", driver, driver->name);
1229
1230         err = supplicant_create();
1231         if (err < 0)
1232                 return err;
1233
1234         driver_list = g_slist_append(driver_list, driver);
1235
1236         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1237                 supplicant_probe(connection, NULL);
1238         else
1239                 supplicant_activate(connection);
1240
1241         return 0;
1242 }
1243
1244 void supplicant_unregister(struct supplicant_driver *driver)
1245 {
1246         DBG("driver %p name %s", driver, driver->name);
1247
1248         supplicant_remove(connection, NULL);
1249
1250         driver_list = g_slist_remove(driver_list, driver);
1251
1252         supplicant_destroy();
1253 }