Handle the case when adding a new interface fails
[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 <stdio.h>
27 #include <errno.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <net/ethernet.h>
31
32 #include <gdbus.h>
33
34 #define CONNMAN_API_SUBJECT_TO_CHANGE
35 #include <connman/device.h>
36 #include <connman/option.h>
37 #include <connman/inet.h>
38 #include <connman/dbus.h>
39 #include <connman/log.h>
40
41 #include "supplicant.h"
42
43 #define TIMEOUT 5000
44
45 #define IEEE80211_CAP_ESS       0x0001
46 #define IEEE80211_CAP_IBSS      0x0002
47 #define IEEE80211_CAP_PRIVACY   0x0010
48
49 #define SUPPLICANT_NAME  "fi.epitest.hostap.WPASupplicant"
50 #define SUPPLICANT_INTF  "fi.epitest.hostap.WPASupplicant"
51 #define SUPPLICANT_PATH  "/fi/epitest/hostap/WPASupplicant"
52
53 /* Taken from "WPA Supplicant - Common definitions" */
54 enum supplicant_state {
55         /**
56          * WPA_DISCONNECTED - Disconnected state
57          *
58          * This state indicates that client is not associated, but is likely to
59          * start looking for an access point. This state is entered when a
60          * connection is lost.
61          */
62         WPA_DISCONNECTED,
63
64         /**
65          * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
66          *
67          * This state is entered if there are no enabled networks in the
68          * configuration. wpa_supplicant is not trying to associate with a new
69          * network and external interaction (e.g., ctrl_iface call to add or
70          * enable a network) is needed to start association.
71          */
72         WPA_INACTIVE,
73
74         /**
75          * WPA_SCANNING - Scanning for a network
76          *
77          * This state is entered when wpa_supplicant starts scanning for a
78          * network.
79          */
80         WPA_SCANNING,
81
82         /**
83          * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
84          *
85          * This state is entered when wpa_supplicant has found a suitable BSS
86          * to associate with and the driver is configured to try to associate
87          * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
88          * state is entered when the driver is configured to try to associate
89          * with a network using the configured SSID and security policy.
90          */
91         WPA_ASSOCIATING,
92
93         /**
94          * WPA_ASSOCIATED - Association completed
95          *
96          * This state is entered when the driver reports that association has
97          * been successfully completed with an AP. If IEEE 802.1X is used
98          * (with or without WPA/WPA2), wpa_supplicant remains in this state
99          * until the IEEE 802.1X/EAPOL authentication has been completed.
100          */
101         WPA_ASSOCIATED,
102
103         /**
104          * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
105          *
106          * This state is entered when WPA/WPA2 4-Way Handshake is started. In
107          * case of WPA-PSK, this happens when receiving the first EAPOL-Key
108          * frame after association. In case of WPA-EAP, this state is entered
109          * when the IEEE 802.1X/EAPOL authentication has been completed.
110          */
111         WPA_4WAY_HANDSHAKE,
112
113         /**
114          * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
115          *
116          * This state is entered when 4-Way Key Handshake has been completed
117          * (i.e., when the supplicant sends out message 4/4) and when Group
118          * Key rekeying is started by the AP (i.e., when supplicant receives
119          * message 1/2).
120          */
121         WPA_GROUP_HANDSHAKE,
122
123         /**
124          * WPA_COMPLETED - All authentication completed
125          *
126          * This state is entered when the full authentication process is
127          * completed. In case of WPA2, this happens when the 4-Way Handshake is
128          * successfully completed. With WPA, this state is entered after the
129          * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
130          * completed after dynamic keys are received (or if not used, after
131          * the EAP authentication has been completed). With static WEP keys and
132          * plaintext connections, this state is entered when an association
133          * has been completed.
134          *
135          * This state indicates that the supplicant has completed its
136          * processing for the association phase and that data connection is
137          * fully configured.
138          */
139         WPA_COMPLETED,
140
141         /**
142          * WPA_INVALID - Invalid state (parsing error)
143          *
144          * This state is returned if the string input is invalid. It is not
145          * an official wpa_supplicant state.
146          */
147         WPA_INVALID,
148 };
149
150 struct supplicant_result {
151         char *path;
152         char *name;
153         unsigned char *addr;
154         unsigned int addr_len;
155         unsigned char *ssid;
156         unsigned int ssid_len;
157         dbus_uint16_t capabilities;
158         gboolean adhoc;
159         gboolean has_wep;
160         gboolean has_wpa;
161         gboolean has_rsn;
162         gboolean has_wps;
163         dbus_int32_t frequency;
164         dbus_int32_t quality;
165         dbus_int32_t noise;
166         dbus_int32_t level;
167         dbus_int32_t maxrate;
168 };
169
170 struct supplicant_task {
171         int ifindex;
172         char *ifname;
173         struct connman_device *device;
174         struct connman_network *network;
175         char *path;
176         char *netpath;
177         gboolean created;
178         enum supplicant_state state;
179         gboolean noscan;
180         GSList *scan_results;
181 };
182
183 static GSList *task_list = NULL;
184
185 static DBusConnection *connection;
186
187 static void free_task(struct supplicant_task *task)
188 {
189         DBG("task %p", task);
190
191         g_free(task->ifname);
192         g_free(task->path);
193         g_free(task);
194 }
195
196 static struct supplicant_task *find_task_by_index(int index)
197 {
198         GSList *list;
199
200         for (list = task_list; list; list = list->next) {
201                 struct supplicant_task *task = list->data;
202
203                 if (task->ifindex == index)
204                         return task;
205         }
206
207         return NULL;
208 }
209
210 static struct supplicant_task *find_task_by_path(const char *path)
211 {
212         GSList *list;
213
214         for (list = task_list; list; list = list->next) {
215                 struct supplicant_task *task = list->data;
216
217                 if (g_strcmp0(task->path, path) == 0)
218                         return task;
219         }
220
221         return NULL;
222 }
223
224 static void add_interface_reply(DBusPendingCall *call, void *user_data)
225 {
226         struct supplicant_task *task = user_data;
227         DBusMessage *reply;
228         DBusError error;
229         const char *path;
230
231         DBG("task %p", task);
232
233         reply = dbus_pending_call_steal_reply(call);
234         if (reply == NULL)
235                 return;
236
237         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
238                 goto failed;
239
240         dbus_error_init(&error);
241
242         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
243                                                 DBUS_TYPE_INVALID) == FALSE) {
244                 if (dbus_error_is_set(&error) == TRUE) {
245                         connman_error("%s", error.message);
246                         dbus_error_free(&error);
247                 } else
248                         connman_error("Wrong arguments for add interface");
249                 goto failed;
250         }
251
252         DBG("path %s", path);
253
254         task->path = g_strdup(path);
255         task->created = TRUE;
256
257         connman_device_set_powered(task->device, TRUE);
258
259         dbus_message_unref(reply);
260
261         return;
262
263 failed:
264         task_list = g_slist_remove(task_list, task);
265
266         connman_device_unref(task->device);
267
268         free_task(task);
269 }
270
271 static int add_interface(struct supplicant_task *task)
272 {
273         const char *driver = connman_option_get_string("wifi");
274         DBusMessage *message;
275         DBusMessageIter array, dict;
276         DBusPendingCall *call;
277
278         DBG("task %p", task);
279
280         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
281                                         SUPPLICANT_INTF, "addInterface");
282         if (message == NULL)
283                 return -ENOMEM;
284
285         dbus_message_iter_init_append(message, &array);
286
287         dbus_message_iter_append_basic(&array,
288                                         DBUS_TYPE_STRING, &task->ifname);
289
290         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
291                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
292                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
293                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
294
295         connman_dbus_dict_append_variant(&dict, "driver",
296                                                 DBUS_TYPE_STRING, &driver);
297
298         dbus_message_iter_close_container(&array, &dict);
299
300         if (dbus_connection_send_with_reply(connection, message,
301                                                 &call, TIMEOUT) == FALSE) {
302                 connman_error("Failed to add interface");
303                 dbus_message_unref(message);
304                 return -EIO;
305         }
306
307         if (call == NULL) {
308                 connman_error("D-Bus connection not available");
309                 dbus_message_unref(message);
310                 return -EIO;
311         }
312
313         dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
314
315         dbus_message_unref(message);
316
317         return -EINPROGRESS;
318 }
319
320 static void get_interface_reply(DBusPendingCall *call, void *user_data)
321 {
322         struct supplicant_task *task = user_data;
323         DBusMessage *reply;
324         DBusError error;
325         const char *path;
326
327         DBG("task %p", task);
328
329         reply = dbus_pending_call_steal_reply(call);
330         if (reply == NULL)
331                 return;
332
333         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
334                 add_interface(task);
335                 goto done;
336         }
337
338         dbus_error_init(&error);
339
340         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
341                                                 DBUS_TYPE_INVALID) == FALSE) {
342                 if (dbus_error_is_set(&error) == TRUE) {
343                         connman_error("%s", error.message);
344                         dbus_error_free(&error);
345                 } else
346                         connman_error("Wrong arguments for get interface");
347                 goto done;
348         }
349
350         DBG("path %s", path);
351
352         task->path = g_strdup(path);
353         task->created = FALSE;
354
355         connman_device_set_powered(task->device, TRUE);
356
357 done:
358         dbus_message_unref(reply);
359 }
360
361 static int create_interface(struct supplicant_task *task)
362 {
363         DBusMessage *message;
364         DBusPendingCall *call;
365
366         DBG("task %p", task);
367
368         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
369                                         SUPPLICANT_INTF, "getInterface");
370         if (message == NULL)
371                 return -ENOMEM;
372
373         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
374                                                         DBUS_TYPE_INVALID);
375
376         if (dbus_connection_send_with_reply(connection, message,
377                                                 &call, TIMEOUT) == FALSE) {
378                 connman_error("Failed to get interface");
379                 dbus_message_unref(message);
380                 return -EIO;
381         }
382
383         if (call == NULL) {
384                 connman_error("D-Bus connection not available");
385                 dbus_message_unref(message);
386                 return -EIO;
387         }
388
389         dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
390
391         dbus_message_unref(message);
392
393         return -EINPROGRESS;
394 }
395
396 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
397 {
398         struct supplicant_task *task = user_data;
399         DBusMessage *reply;
400
401         DBG("task %p", task);
402
403         reply = dbus_pending_call_steal_reply(call);
404
405         connman_device_set_powered(task->device, FALSE);
406
407         connman_device_unref(task->device);
408
409         connman_inet_ifdown(task->ifindex);
410
411         free_task(task);
412
413         dbus_message_unref(reply);
414 }
415
416 static int remove_interface(struct supplicant_task *task)
417 {
418         DBusMessage *message;
419         DBusPendingCall *call;
420
421         DBG("task %p", task);
422
423         if (task->created == FALSE) {
424                 connman_device_set_powered(task->device, FALSE);
425                 return 0;
426         }
427
428         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
429                                         SUPPLICANT_INTF, "removeInterface");
430         if (message == NULL)
431                 return -ENOMEM;
432
433         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
434                                                         DBUS_TYPE_INVALID);
435
436         if (dbus_connection_send_with_reply(connection, message,
437                                                 &call, TIMEOUT) == FALSE) {
438                 connman_error("Failed to remove interface");
439                 dbus_message_unref(message);
440                 return -EIO;
441         }
442
443         if (call == NULL) {
444                 connman_error("D-Bus connection not available");
445                 dbus_message_unref(message);
446                 return -EIO;
447         }
448
449         dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
450
451         dbus_message_unref(message);
452
453         return -EINPROGRESS;
454 }
455
456 #if 0
457 static int set_ap_scan(struct supplicant_task *task)
458 {
459         DBusMessage *message, *reply;
460         DBusError error;
461         guint32 ap_scan = 1;
462
463         DBG("task %p", task);
464
465         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
466                                 SUPPLICANT_INTF ".Interface", "setAPScan");
467         if (message == NULL)
468                 return -ENOMEM;
469
470         dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
471                                                         DBUS_TYPE_INVALID);
472
473         dbus_error_init(&error);
474
475         reply = dbus_connection_send_with_reply_and_block(connection,
476                                                         message, -1, &error);
477         if (reply == NULL) {
478                 if (dbus_error_is_set(&error) == TRUE) {
479                         connman_error("%s", error.message);
480                         dbus_error_free(&error);
481                 } else
482                         connman_error("Failed to set AP scan");
483                 dbus_message_unref(message);
484                 return -EIO;
485         }
486
487         dbus_message_unref(message);
488
489         dbus_message_unref(reply);
490
491         return 0;
492 }
493 #endif
494
495 static int add_network(struct supplicant_task *task)
496 {
497         DBusMessage *message, *reply;
498         DBusError error;
499         const char *path;
500
501         DBG("task %p", task);
502
503         if (task->netpath != NULL)
504                 return -EALREADY;
505
506         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
507                                 SUPPLICANT_INTF ".Interface", "addNetwork");
508         if (message == NULL)
509                 return -ENOMEM;
510
511         dbus_error_init(&error);
512
513         reply = dbus_connection_send_with_reply_and_block(connection,
514                                                         message, -1, &error);
515         if (reply == NULL) {
516                 if (dbus_error_is_set(&error) == TRUE) {
517                         connman_error("%s", error.message);
518                         dbus_error_free(&error);
519                 } else
520                         connman_error("Failed to add network");
521                 dbus_message_unref(message);
522                 return -EIO;
523         }
524
525         dbus_message_unref(message);
526
527         dbus_error_init(&error);
528
529         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
530                                                 DBUS_TYPE_INVALID) == FALSE) {
531                 if (dbus_error_is_set(&error) == TRUE) {
532                         connman_error("%s", error.message);
533                         dbus_error_free(&error);
534                 } else
535                         connman_error("Wrong arguments for network");
536                 dbus_message_unref(reply);
537                 return -EIO;
538         }
539
540         DBG("path %s", path);
541
542         task->netpath = g_strdup(path);
543
544         dbus_message_unref(reply);
545
546         return 0;
547 }
548
549 static int remove_network(struct supplicant_task *task)
550 {
551         DBusMessage *message, *reply;
552         DBusError error;
553
554         DBG("task %p", task);
555
556         if (task->netpath == NULL)
557                 return -EINVAL;
558
559         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
560                                 SUPPLICANT_INTF ".Interface", "removeNetwork");
561         if (message == NULL)
562                 return -ENOMEM;
563
564         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
565                                                         DBUS_TYPE_INVALID);
566
567         dbus_error_init(&error);
568
569         reply = dbus_connection_send_with_reply_and_block(connection,
570                                                         message, -1, &error);
571         if (reply == NULL) {
572                 if (dbus_error_is_set(&error) == TRUE) {
573                         connman_error("%s", error.message);
574                         dbus_error_free(&error);
575                 } else
576                         connman_error("Failed to remove network");
577                 dbus_message_unref(message);
578                 return -EIO;
579         }
580
581         dbus_message_unref(message);
582
583         dbus_message_unref(reply);
584
585         g_free(task->netpath);
586         task->netpath = NULL;
587
588         return 0;
589 }
590
591 static int select_network(struct supplicant_task *task)
592 {
593         DBusMessage *message, *reply;
594         DBusError error;
595
596         DBG("task %p", task);
597
598         if (task->netpath == NULL)
599                 return -EINVAL;
600
601         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
602                                 SUPPLICANT_INTF ".Interface", "selectNetwork");
603         if (message == NULL)
604                 return -ENOMEM;
605
606         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
607                                                         DBUS_TYPE_INVALID);
608
609         dbus_error_init(&error);
610
611         reply = dbus_connection_send_with_reply_and_block(connection,
612                                                         message, -1, &error);
613         if (reply == NULL) {
614                 if (dbus_error_is_set(&error) == TRUE) {
615                         connman_error("%s", error.message);
616                         dbus_error_free(&error);
617                 } else
618                         connman_error("Failed to select network");
619                 dbus_message_unref(message);
620                 return -EIO;
621         }
622
623         dbus_message_unref(message);
624
625         dbus_message_unref(reply);
626
627         return 0;
628 }
629
630 static int enable_network(struct supplicant_task *task)
631 {
632         DBusMessage *message, *reply;
633         DBusError error;
634
635         DBG("task %p", task);
636
637         if (task->netpath == NULL)
638                 return -EINVAL;
639
640         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
641                                         SUPPLICANT_INTF ".Network", "enable");
642         if (message == NULL)
643                 return -ENOMEM;
644
645         dbus_error_init(&error);
646
647         reply = dbus_connection_send_with_reply_and_block(connection,
648                                                         message, -1, &error);
649         if (reply == NULL) {
650                 if (dbus_error_is_set(&error) == TRUE) {
651                         connman_error("%s", error.message);
652                         dbus_error_free(&error);
653                 } else
654                         connman_error("Failed to enable network");
655                 dbus_message_unref(message);
656                 return -EIO;
657         }
658
659         dbus_message_unref(message);
660
661         dbus_message_unref(reply);
662
663         return 0;
664 }
665
666 static int disable_network(struct supplicant_task *task)
667 {
668         DBusMessage *message, *reply;
669         DBusError error;
670
671         DBG("task %p", task);
672
673         if (task->netpath == NULL)
674                 return -EINVAL;
675
676         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
677                                         SUPPLICANT_INTF ".Network", "disable");
678         if (message == NULL)
679                 return -ENOMEM;
680
681         dbus_error_init(&error);
682
683         reply = dbus_connection_send_with_reply_and_block(connection,
684                                                         message, -1, &error);
685         if (reply == NULL) {
686                 if (dbus_error_is_set(&error) == TRUE) {
687                         connman_error("%s", error.message);
688                         dbus_error_free(&error);
689                 } else
690                         connman_error("Failed to disable network");
691                 dbus_message_unref(message);
692                 return -EIO;
693         }
694
695         dbus_message_unref(message);
696
697         dbus_message_unref(reply);
698
699         return 0;
700 }
701
702 static int set_network(struct supplicant_task *task,
703                                 const unsigned char *network, int len,
704                                 const char *address, const char *security,
705                                                         const char *passphrase)
706 {
707         DBusMessage *message, *reply;
708         DBusMessageIter array, dict;
709         DBusError error;
710
711         DBG("task %p", task);
712
713         if (task->netpath == NULL)
714                 return -EINVAL;
715
716         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
717                                         SUPPLICANT_INTF ".Network", "set");
718         if (message == NULL)
719                 return -ENOMEM;
720
721         dbus_message_iter_init_append(message, &array);
722
723         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
724                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
725                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
726                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
727
728         if (address == NULL) {
729                 dbus_uint32_t scan_ssid = 1;
730                 connman_dbus_dict_append_variant(&dict, "scan_ssid",
731                                                 DBUS_TYPE_UINT32, &scan_ssid);
732         } else
733                 connman_dbus_dict_append_variant(&dict, "bssid",
734                                                 DBUS_TYPE_STRING, &address);
735
736         connman_dbus_dict_append_array(&dict, "ssid",
737                                         DBUS_TYPE_BYTE, &network, len);
738
739         if (g_ascii_strcasecmp(security, "wpa") == 0 ||
740                                 g_ascii_strcasecmp(security, "rsn") == 0) {
741                 const char *key_mgmt = "WPA-PSK";
742                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
743                                                 DBUS_TYPE_STRING, &key_mgmt);
744
745                 if (passphrase && strlen(passphrase) > 0)
746                         connman_dbus_dict_append_variant(&dict, "psk",
747                                                 DBUS_TYPE_STRING, &passphrase);
748         } else if (g_ascii_strcasecmp(security, "wep") == 0) {
749                 const char *key_mgmt = "NONE", *index = "0";
750                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
751                                                 DBUS_TYPE_STRING, &key_mgmt);
752
753                 if (passphrase) {
754                         int size = strlen(passphrase);
755                         if (size == 10 || size == 26) {
756                                 unsigned char *key = malloc(13);
757                                 char tmp[3];
758                                 int i;
759                                 memset(tmp, 0, sizeof(tmp));
760                                 if (key == NULL)
761                                         size = 0;
762                                 for (i = 0; i < size / 2; i++) {
763                                         memcpy(tmp, passphrase + (i * 2), 2);
764                                         key[i] = (unsigned char) strtol(tmp,
765                                                                 NULL, 16);
766                                 }
767                                 connman_dbus_dict_append_array(&dict,
768                                                 "wep_key0", DBUS_TYPE_BYTE,
769                                                         &key, size / 2);
770                                 free(key);
771                         } else
772                                 connman_dbus_dict_append_variant(&dict,
773                                                 "wep_key0", DBUS_TYPE_STRING,
774                                                                 &passphrase);
775                         connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
776                                                 DBUS_TYPE_STRING, &index);
777                 }
778         } else {
779                 const char *key_mgmt = "NONE";
780                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
781                                                 DBUS_TYPE_STRING, &key_mgmt);
782         }
783
784         dbus_message_iter_close_container(&array, &dict);
785
786         dbus_error_init(&error);
787
788         reply = dbus_connection_send_with_reply_and_block(connection,
789                                                         message, -1, &error);
790         if (reply == NULL) {
791                 if (dbus_error_is_set(&error) == TRUE) {
792                         connman_error("%s", error.message);
793                         dbus_error_free(&error);
794                 } else
795                         connman_error("Failed to set network options");
796                 dbus_message_unref(message);
797                 return -EIO;
798         }
799
800         dbus_message_unref(message);
801
802         dbus_message_unref(reply);
803
804         return 0;
805 }
806
807 static int initiate_scan(struct supplicant_task *task)
808 {
809         DBusMessage *message;
810         DBusPendingCall *call;
811
812         DBG("task %p", task);
813
814         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
815                                         SUPPLICANT_INTF ".Interface", "scan");
816         if (message == NULL)
817                 return -ENOMEM;
818
819         if (dbus_connection_send_with_reply(connection, message,
820                                                 &call, TIMEOUT) == FALSE) {
821                 connman_error("Failed to initiate scan");
822                 dbus_message_unref(message);
823                 return -EIO;
824         }
825
826         dbus_message_unref(message);
827
828         return 0;
829 }
830
831 static struct {
832         char *name;
833         char *value;
834 } special_ssid[] = {
835         { "<hidden>", "hidden"  },
836         { "default",  "linksys" },
837         { "wireless"  },
838         { "linksys"   },
839         { "netgear"   },
840         { "dlink"     },
841         { "2wire"     },
842         { "compaq"    },
843         { "tsunami"   },
844         { "comcomcom", "3com"     },
845         { "3Com",      "3com"     },
846         { "Symbol",    "symbol"   },
847         { "Wireless" , "wireless" },
848         { "WLAN",      "wlan"     },
849         { }
850 };
851
852 static char *build_group(const char *addr, const char *name,
853                         const unsigned char *ssid, unsigned int ssid_len,
854                                         const char *mode, const char *security)
855 {
856         GString *str;
857         unsigned int i;
858
859         if (addr == NULL)
860                 return NULL;
861
862         str = g_string_sized_new((ssid_len * 2) + 24);
863         if (str == NULL)
864                 return NULL;
865
866         for (i = 0; special_ssid[i].name; i++) {
867                 if (g_strcmp0(special_ssid[i].name, name) == 0) {
868                         if (special_ssid[i].value == NULL)
869                                 g_string_append_printf(str, "%s_%s",
870                                                                 name, addr);
871                         else
872                                 g_string_append_printf(str, "%s_%s",
873                                                 special_ssid[i].value, addr);
874                         goto done;
875                 }
876         }
877
878         if (ssid_len > 0 && ssid[0] != '\0') {
879                 for (i = 0; i < ssid_len; i++)
880                         g_string_append_printf(str, "%02x", ssid[i]);
881         } else
882                 g_string_append_printf(str, "hidden_%s", addr);
883
884 done:
885         g_string_append_printf(str, "_%s_%s", mode, security);
886
887         return g_string_free(str, FALSE);
888 }
889
890 static void extract_addr(DBusMessageIter *value,
891                                         struct supplicant_result *result)
892 {
893         DBusMessageIter array;
894         struct ether_addr *eth;
895         unsigned char *addr;
896         int addr_len;
897
898         dbus_message_iter_recurse(value, &array);
899         dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
900
901         if (addr_len != 6)
902                 return;
903
904         result->addr = g_try_malloc(addr_len);
905         if (result->addr == NULL)
906                 return;
907
908         memcpy(result->addr, addr, addr_len);
909         result->addr_len = addr_len;
910
911         result->path = g_try_malloc0(13);
912         if (result->path == NULL)
913                 return;
914
915         eth = (void *) addr;
916
917         snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
918                                                 eth->ether_addr_octet[0],
919                                                 eth->ether_addr_octet[1],
920                                                 eth->ether_addr_octet[2],
921                                                 eth->ether_addr_octet[3],
922                                                 eth->ether_addr_octet[4],
923                                                 eth->ether_addr_octet[5]);
924 }
925
926 static void extract_ssid(DBusMessageIter *value,
927                                         struct supplicant_result *result)
928 {
929         DBusMessageIter array;
930         unsigned char *ssid;
931         int ssid_len;
932
933         dbus_message_iter_recurse(value, &array);
934         dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
935
936         if (ssid_len < 1)
937                 return;
938
939         result->ssid = g_try_malloc(ssid_len);
940         if (result->ssid == NULL)
941                 return;
942
943         memcpy(result->ssid, ssid, ssid_len);
944         result->ssid_len = ssid_len;
945
946         result->name = g_try_malloc0(ssid_len + 1);
947         if (result->name == NULL)
948                 return;
949
950         memcpy(result->name, ssid, ssid_len);
951 }
952
953 static void extract_wpaie(DBusMessageIter *value,
954                                         struct supplicant_result *result)
955 {
956         DBusMessageIter array;
957         unsigned char *ie;
958         int ie_len;
959
960         dbus_message_iter_recurse(value, &array);
961         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
962
963         if (ie_len > 0)
964                 result->has_wpa = TRUE;
965 }
966
967 static void extract_rsnie(DBusMessageIter *value,
968                                         struct supplicant_result *result)
969 {
970         DBusMessageIter array;
971         unsigned char *ie;
972         int ie_len;
973
974         dbus_message_iter_recurse(value, &array);
975         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
976
977         if (ie_len > 0)
978                 result->has_rsn = TRUE;
979 }
980
981 static void extract_wpsie(DBusMessageIter *value,
982                                         struct supplicant_result *result)
983 {
984         DBusMessageIter array;
985         unsigned char *ie;
986         int ie_len;
987
988         dbus_message_iter_recurse(value, &array);
989         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
990
991         if (ie_len > 0)
992                 result->has_wps = TRUE;
993 }
994
995 static void extract_capabilites(DBusMessageIter *value,
996                                         struct supplicant_result *result)
997 {
998         dbus_message_iter_get_basic(value, &result->capabilities);
999
1000         if (result->capabilities & IEEE80211_CAP_ESS)
1001                 result->adhoc = FALSE;
1002         else if (result->capabilities & IEEE80211_CAP_IBSS)
1003                 result->adhoc = TRUE;
1004
1005         if (result->capabilities & IEEE80211_CAP_PRIVACY)
1006                 result->has_wep = TRUE;
1007 }
1008
1009 static unsigned char calculate_strength(struct supplicant_result *result)
1010 {
1011         if (result->quality < 0) {
1012                 unsigned char strength;
1013
1014                 if (result->level > 0)
1015                         strength = 100 - result->level;
1016                 else
1017                         strength = 120 + result->level;
1018
1019                 if (strength > 100)
1020                         strength = 100;
1021
1022                 return strength;
1023         }
1024
1025         return result->quality;
1026 }
1027
1028 static unsigned short calculate_channel(struct supplicant_result *result)
1029 {
1030         if (result->frequency < 0)
1031                 return 0;
1032
1033         return (result->frequency - 2407) / 5;
1034 }
1035
1036 static void get_properties(struct supplicant_task *task);
1037
1038 static void properties_reply(DBusPendingCall *call, void *user_data)
1039 {
1040         struct supplicant_task *task = user_data;
1041         struct supplicant_result result;
1042         struct connman_network *network;
1043         DBusMessage *reply;
1044         DBusMessageIter array, dict;
1045         unsigned char strength;
1046         unsigned short channel, frequency;
1047         const char *mode, *security;
1048         char *group;
1049
1050         DBG("task %p", task);
1051
1052         reply = dbus_pending_call_steal_reply(call);
1053         if (reply == NULL) {
1054                 get_properties(task);
1055                 return;
1056         }
1057
1058         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1059                 dbus_message_unref(reply);
1060                 get_properties(task);
1061                 return;
1062         }
1063
1064         memset(&result, 0, sizeof(result));
1065         result.frequency = -1;
1066         result.quality = -1;
1067         result.level = 0;
1068         result.noise = 0;
1069
1070         dbus_message_iter_init(reply, &array);
1071
1072         dbus_message_iter_recurse(&array, &dict);
1073
1074         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1075                 DBusMessageIter entry, value;
1076                 const char *key;
1077
1078                 dbus_message_iter_recurse(&dict, &entry);
1079                 dbus_message_iter_get_basic(&entry, &key);
1080
1081                 dbus_message_iter_next(&entry);
1082
1083                 dbus_message_iter_recurse(&entry, &value);
1084
1085                 //type = dbus_message_iter_get_arg_type(&value);
1086                 //dbus_message_iter_get_basic(&value, &val);
1087
1088                 /* 
1089                  * bssid        : a (97)
1090                  * ssid         : a (97)
1091                  * wpaie        : a (97)
1092                  * rsnie        : a (97)
1093                  * wpsie        : a (97)
1094                  * frequency    : i (105)
1095                  * capabilities : q (113)
1096                  * quality      : i (105)
1097                  * noise        : i (105)
1098                  * level        : i (105)
1099                  * maxrate      : i (105)
1100                  */
1101
1102                 if (g_str_equal(key, "bssid") == TRUE)
1103                         extract_addr(&value, &result);
1104                 else if (g_str_equal(key, "ssid") == TRUE)
1105                         extract_ssid(&value, &result);
1106                 else if (g_str_equal(key, "wpaie") == TRUE)
1107                         extract_wpaie(&value, &result);
1108                 else if (g_str_equal(key, "rsnie") == TRUE)
1109                         extract_rsnie(&value, &result);
1110                 else if (g_str_equal(key, "wpsie") == TRUE)
1111                         extract_wpsie(&value, &result);
1112                 else if (g_str_equal(key, "capabilities") == TRUE)
1113                         extract_capabilites(&value, &result);
1114                 else if (g_str_equal(key, "frequency") == TRUE)
1115                         dbus_message_iter_get_basic(&value, &result.frequency);
1116                 else if (g_str_equal(key, "quality") == TRUE)
1117                         dbus_message_iter_get_basic(&value, &result.quality);
1118                 else if (g_str_equal(key, "noise") == TRUE)
1119                         dbus_message_iter_get_basic(&value, &result.noise);
1120                 else if (g_str_equal(key, "level") == TRUE)
1121                         dbus_message_iter_get_basic(&value, &result.level);
1122                 else if (g_str_equal(key, "maxrate") == TRUE)
1123                         dbus_message_iter_get_basic(&value, &result.maxrate);
1124
1125                 dbus_message_iter_next(&dict);
1126         }
1127
1128         if (result.path == NULL)
1129                 goto done;
1130
1131         if (result.path[0] == '\0')
1132                 goto done;
1133
1134         if (result.frequency > 0 && result.frequency < 14)
1135                 result.frequency = 2407 + (5 * result.frequency);
1136         else if (result.frequency == 14)
1137                 result.frequency = 2484;
1138
1139         strength = calculate_strength(&result);
1140         channel  = calculate_channel(&result);
1141
1142         frequency = (result.frequency < 0) ? 0 : result.frequency;
1143
1144         if (result.has_rsn == TRUE)
1145                 security = "rsn";
1146         else if (result.has_wpa == TRUE)
1147                 security = "wpa";
1148         else if (result.has_wep == TRUE)
1149                 security = "wep";
1150         else
1151                 security = "none";
1152
1153         mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1154
1155         group = build_group(result.path, result.name,
1156                                         result.ssid, result.ssid_len,
1157                                                         mode, security);
1158
1159         network = connman_device_get_network(task->device, result.path);
1160         if (network == NULL) {
1161                 int index;
1162
1163                 network = connman_network_create(result.path,
1164                                                 CONNMAN_NETWORK_TYPE_WIFI);
1165                 if (network == NULL)
1166                         goto done;
1167
1168                 index = connman_device_get_index(task->device);
1169                 connman_network_set_index(network, index);
1170
1171                 connman_network_set_protocol(network,
1172                                                 CONNMAN_NETWORK_PROTOCOL_IP);
1173
1174                 connman_network_set_address(network, result.addr,
1175                                                         result.addr_len);
1176
1177                 if (connman_device_add_network(task->device, network) < 0) {
1178                         connman_network_unref(network);
1179                         goto done;
1180                 }
1181         }
1182
1183         if (result.name != NULL && result.name[0] != '\0')
1184                 connman_network_set_name(network, result.name);
1185
1186         connman_network_set_blob(network, "WiFi.SSID",
1187                                                 result.ssid, result.ssid_len);
1188
1189         connman_network_set_string(network, "WiFi.Mode", mode);
1190
1191         DBG("%s (%s %s) strength %d (%s)",
1192                                 result.name, mode, security, strength,
1193                                 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1194
1195         connman_network_set_available(network, TRUE);
1196         connman_network_set_strength(network, strength);
1197
1198         connman_network_set_uint16(network, "Frequency", frequency);
1199         connman_network_set_uint16(network, "WiFi.Channel", channel);
1200         connman_network_set_string(network, "WiFi.Security", security);
1201
1202         connman_network_set_group(network, group);
1203
1204         g_free(group);
1205
1206 done:
1207         g_free(result.path);
1208         g_free(result.addr);
1209         g_free(result.name);
1210         g_free(result.ssid);
1211
1212         dbus_message_unref(reply);
1213
1214         get_properties(task);
1215 }
1216
1217 static void get_properties(struct supplicant_task *task)
1218 {
1219         DBusMessage *message;
1220         DBusPendingCall *call;
1221         char *path;
1222
1223         path = g_slist_nth_data(task->scan_results, 0);
1224         if (path == NULL)
1225                 goto noscan;
1226
1227         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1228                                                 SUPPLICANT_INTF ".BSSID",
1229                                                                 "properties");
1230
1231         task->scan_results = g_slist_remove(task->scan_results, path);
1232         g_free(path);
1233
1234         if (message == NULL)
1235                 goto noscan;
1236
1237         if (dbus_connection_send_with_reply(connection, message,
1238                                                 &call, TIMEOUT) == FALSE) {
1239                 connman_error("Failed to get network properties");
1240                 dbus_message_unref(message);
1241                 goto noscan;
1242         }
1243
1244         if (call == NULL) {
1245                 connman_error("D-Bus connection not available");
1246                 dbus_message_unref(message);
1247                 goto noscan;
1248         }
1249
1250         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1251
1252         dbus_message_unref(message);
1253
1254         return;
1255
1256 noscan:
1257         if (task->noscan == FALSE)
1258                 connman_device_set_scanning(task->device, FALSE);
1259 }
1260
1261 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1262 {
1263         struct supplicant_task *task = user_data;
1264         DBusMessage *reply;
1265         DBusError error;
1266         char **results;
1267         int i, num_results;
1268
1269         DBG("task %p", task);
1270
1271         reply = dbus_pending_call_steal_reply(call);
1272         if (reply == NULL)
1273                 goto noscan;
1274
1275         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1276                 goto done;
1277
1278         dbus_error_init(&error);
1279
1280         if (dbus_message_get_args(reply, &error,
1281                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1282                                                 &results, &num_results,
1283                                                 DBUS_TYPE_INVALID) == FALSE) {
1284                 if (dbus_error_is_set(&error) == TRUE) {
1285                         connman_error("%s", error.message);
1286                         dbus_error_free(&error);
1287                 } else
1288                         connman_error("Wrong arguments for scan result");
1289                 goto done;
1290         }
1291
1292         if (num_results == 0)
1293                 goto done;
1294
1295         for (i = 0; i < num_results; i++) {
1296                 char *path = g_strdup(results[i]);
1297                 if (path == NULL)
1298                         continue;
1299
1300                 task->scan_results = g_slist_append(task->scan_results, path);
1301         }
1302
1303         g_strfreev(results);
1304
1305         dbus_message_unref(reply);
1306
1307         get_properties(task);
1308
1309         return;
1310
1311 done:
1312         dbus_message_unref(reply);
1313
1314 noscan:
1315         if (task->noscan == FALSE)
1316                 connman_device_set_scanning(task->device, FALSE);
1317 }
1318
1319 static void scan_results_available(struct supplicant_task *task)
1320 {
1321         DBusMessage *message;
1322         DBusPendingCall *call;
1323
1324         DBG("task %p", task);
1325
1326         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1327                                                 SUPPLICANT_INTF ".Interface",
1328                                                         "scanResults");
1329         if (message == NULL)
1330                 return;
1331
1332         if (dbus_connection_send_with_reply(connection, message,
1333                                                 &call, TIMEOUT) == FALSE) {
1334                 connman_error("Failed to request scan result");
1335                 goto done;
1336         }
1337
1338         if (task->noscan == FALSE)
1339                 connman_device_set_scanning(task->device, TRUE);
1340
1341         if (call == NULL) {
1342                 connman_error("D-Bus connection not available");
1343                 goto done;
1344         }
1345
1346         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1347
1348 done:
1349         dbus_message_unref(message);
1350 }
1351
1352 static enum supplicant_state string2state(const char *state)
1353 {
1354         if (g_str_equal(state, "INACTIVE") == TRUE)
1355                 return WPA_INACTIVE;
1356         else if (g_str_equal(state, "SCANNING") == TRUE)
1357                 return WPA_SCANNING;
1358         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1359                 return WPA_ASSOCIATING;
1360         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1361                 return WPA_ASSOCIATED;
1362         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1363                 return WPA_GROUP_HANDSHAKE;
1364         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1365                 return WPA_4WAY_HANDSHAKE;
1366         else if (g_str_equal(state, "COMPLETED") == TRUE)
1367                 return WPA_COMPLETED;
1368         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1369                 return WPA_DISCONNECTED;
1370         else
1371                 return WPA_INVALID;
1372 }
1373
1374 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1375 {
1376         DBusError error;
1377         const char *newstate, *oldstate;
1378         enum supplicant_state state;
1379
1380         dbus_error_init(&error);
1381
1382         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1383                                                 DBUS_TYPE_STRING, &oldstate,
1384                                                 DBUS_TYPE_INVALID) == FALSE) {
1385                 if (dbus_error_is_set(&error) == TRUE) {
1386                         connman_error("%s", error.message);
1387                         dbus_error_free(&error);
1388                 } else
1389                         connman_error("Wrong arguments for state change");
1390                 return;
1391         }
1392
1393         DBG("state %s ==> %s", oldstate, newstate);
1394
1395         state = string2state(newstate);
1396         if (state == WPA_INVALID)
1397                 return;
1398
1399         task->state = state;
1400
1401         switch (task->state) {
1402         case WPA_SCANNING:
1403                 task->noscan = TRUE;
1404                 connman_device_set_scanning(task->device, TRUE);
1405                 break;
1406         case WPA_ASSOCIATING:
1407         case WPA_ASSOCIATED:
1408         case WPA_4WAY_HANDSHAKE:
1409         case WPA_GROUP_HANDSHAKE:
1410                 task->noscan = TRUE;
1411                 break;
1412         case WPA_COMPLETED:
1413         case WPA_DISCONNECTED:
1414                 task->noscan = FALSE;
1415                 break;
1416         case WPA_INACTIVE:
1417                 task->noscan = FALSE;
1418                 connman_device_set_scanning(task->device, FALSE);
1419                 break;
1420         case WPA_INVALID:
1421                 break;
1422         }
1423
1424         if (task->network == NULL)
1425                 return;
1426
1427         switch (task->state) {
1428         case WPA_COMPLETED:
1429                 /* carrier on */
1430                 connman_network_set_connected(task->network, TRUE);
1431                 connman_device_set_scanning(task->device, FALSE);
1432                 break;
1433         case WPA_DISCONNECTED:
1434                 /* carrier off */
1435                 connman_network_set_connected(task->network, FALSE);
1436                 connman_device_set_scanning(task->device, FALSE);
1437                 break;
1438         case WPA_ASSOCIATING:
1439                 connman_network_set_associating(task->network, TRUE);
1440                 break;
1441         default:
1442                 connman_network_set_associating(task->network, FALSE);
1443                 break;
1444         }
1445 }
1446
1447 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1448                                                 DBusMessage *msg, void *data)
1449 {
1450         struct supplicant_task *task;
1451         const char *member, *path;
1452
1453         if (dbus_message_has_interface(msg,
1454                                 SUPPLICANT_INTF ".Interface") == FALSE)
1455                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1456
1457         member = dbus_message_get_member(msg);
1458         if (member == NULL)
1459                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1460
1461         path = dbus_message_get_path(msg);
1462         if (path == NULL)
1463                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1464
1465         task = find_task_by_path(path);
1466         if (task == NULL)
1467                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1468
1469         DBG("task %p member %s", task, member);
1470
1471         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1472                 scan_results_available(task);
1473         else if (g_str_equal(member, "StateChange") == TRUE)
1474                 state_change(task, msg);
1475
1476         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1477 }
1478
1479 int supplicant_start(struct connman_device *device)
1480 {
1481         struct supplicant_task *task;
1482
1483         DBG("device %p", device);
1484
1485         task = g_try_new0(struct supplicant_task, 1);
1486         if (task == NULL)
1487                 return -ENOMEM;
1488
1489         task->ifindex = connman_device_get_index(device);
1490         task->ifname = connman_inet_ifname(task->ifindex);
1491
1492         if (task->ifname == NULL) {
1493                 g_free(task);
1494                 return -ENOMEM;
1495         }
1496
1497         task->device = connman_device_ref(device);
1498
1499         task->created = FALSE;
1500         task->noscan = FALSE;
1501         task->state = WPA_INVALID;
1502
1503         task_list = g_slist_append(task_list, task);
1504
1505         return create_interface(task);
1506 }
1507
1508 int supplicant_stop(struct connman_device *device)
1509 {
1510         int index = connman_device_get_index(device);
1511         struct supplicant_task *task;
1512
1513         DBG("device %p", device);
1514
1515         task = find_task_by_index(index);
1516         if (task == NULL)
1517                 return -ENODEV;
1518
1519         task_list = g_slist_remove(task_list, task);
1520
1521         disable_network(task);
1522
1523         remove_network(task);
1524
1525         return remove_interface(task);
1526 }
1527
1528 int supplicant_scan(struct connman_device *device)
1529 {
1530         int index = connman_device_get_index(device);
1531         struct supplicant_task *task;
1532         int err;
1533
1534         DBG("device %p", device);
1535
1536         task = find_task_by_index(index);
1537         if (task == NULL)
1538                 return -ENODEV;
1539
1540         switch (task->state) {
1541         case WPA_SCANNING:
1542                 return -EALREADY;
1543         case WPA_ASSOCIATING:
1544         case WPA_ASSOCIATED:
1545         case WPA_4WAY_HANDSHAKE:
1546         case WPA_GROUP_HANDSHAKE:
1547                 return -EBUSY;
1548         default:
1549                 break;
1550         }
1551
1552         err = initiate_scan(task);
1553
1554         return 0;
1555 }
1556
1557 int supplicant_connect(struct connman_network *network)
1558 {
1559         struct supplicant_task *task;
1560         const char *address, *security, *passphrase;
1561         const void *ssid;
1562         unsigned int ssid_len;
1563         int index;
1564
1565         DBG("network %p", network);
1566
1567         address = connman_network_get_string(network, "Address");
1568         security = connman_network_get_string(network, "WiFi.Security");
1569         passphrase = connman_network_get_string(network, "WiFi.Passphrase");
1570
1571         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
1572
1573         DBG("address %s security %s passphrase %s",
1574                                         address, security, passphrase);
1575
1576         if (security == NULL && passphrase == NULL)
1577                 return -EINVAL;
1578
1579         if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1580                 return -EINVAL;
1581
1582         index = connman_network_get_index(network);
1583
1584         task = find_task_by_index(index);
1585         if (task == NULL)
1586                 return -ENODEV;
1587
1588         task->network = connman_network_ref(network);
1589
1590         add_network(task);
1591
1592         select_network(task);
1593         disable_network(task);
1594
1595         set_network(task, ssid, ssid_len, address, security, passphrase);
1596
1597         enable_network(task);
1598
1599         connman_network_set_associating(task->network, TRUE);
1600
1601         return 0;
1602 }
1603
1604 int supplicant_disconnect(struct connman_network *network)
1605 {
1606         struct supplicant_task *task;
1607         int index;
1608
1609         DBG("network %p", network);
1610
1611         index = connman_network_get_index(network);
1612
1613         task = find_task_by_index(index);
1614         if (task == NULL)
1615                 return -ENODEV;
1616
1617         disable_network(task);
1618
1619         remove_network(task);
1620
1621         connman_network_set_connected(task->network, FALSE);
1622
1623         connman_network_unref(task->network);
1624
1625         return 0;
1626 }
1627
1628 static void supplicant_activate(DBusConnection *conn)
1629 {
1630         DBusMessage *message;
1631
1632         DBG("conn %p", conn);
1633
1634         message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1635                                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1636         if (message == NULL)
1637                 return;
1638
1639         dbus_message_set_no_reply(message, TRUE);
1640
1641         dbus_connection_send(conn, message, NULL);
1642
1643         dbus_message_unref(message);
1644 }
1645
1646 static GSList *driver_list = NULL;
1647
1648 static void supplicant_probe(DBusConnection *conn, void *user_data)
1649 {
1650         GSList *list;
1651
1652         DBG("conn %p", conn);
1653
1654         for (list = driver_list; list; list = list->next) {
1655                 struct supplicant_driver *driver = list->data;
1656
1657                 DBG("driver %p name %s", driver, driver->name);
1658
1659                 if (driver->probe)
1660                         driver->probe();
1661         }
1662 }
1663
1664 static void supplicant_remove(DBusConnection *conn, void *user_data)
1665 {
1666         GSList *list;
1667
1668         DBG("conn %p", conn);
1669
1670         for (list = driver_list; list; list = list->next) {
1671                 struct supplicant_driver *driver = list->data;
1672
1673                 DBG("driver %p name %s", driver, driver->name);
1674
1675                 if (driver->remove)
1676                         driver->remove();
1677         }
1678 }
1679
1680 static const char *supplicant_rule = "type=signal,"
1681                                 "interface=" SUPPLICANT_INTF ".Interface";
1682 static guint watch;
1683
1684 static int supplicant_create(void)
1685 {
1686         if (g_slist_length(driver_list) > 0)
1687                 return 0;
1688
1689         connection = connman_dbus_get_connection();
1690         if (connection == NULL)
1691                 return -EIO;
1692
1693         DBG("connection %p", connection);
1694
1695         if (dbus_connection_add_filter(connection,
1696                                 supplicant_filter, NULL, NULL) == FALSE) {
1697                 connection = connman_dbus_get_connection();
1698                 return -EIO;
1699         }
1700
1701         dbus_bus_add_match(connection, supplicant_rule, NULL);
1702         dbus_connection_flush(connection);
1703
1704         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1705                         supplicant_probe, supplicant_remove, NULL, NULL);
1706
1707         return 0;
1708 }
1709
1710 static void supplicant_destroy(void)
1711 {
1712         if (g_slist_length(driver_list) > 0)
1713                 return;
1714
1715         DBG("connection %p", connection);
1716
1717         if (watch > 0)
1718                 g_dbus_remove_watch(connection, watch);
1719
1720         dbus_bus_remove_match(connection, supplicant_rule, NULL);
1721         dbus_connection_flush(connection);
1722
1723         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1724
1725         dbus_connection_unref(connection);
1726         connection = NULL;
1727 }
1728
1729 int supplicant_register(struct supplicant_driver *driver)
1730 {
1731         int err;
1732
1733         DBG("driver %p name %s", driver, driver->name);
1734
1735         err = supplicant_create();
1736         if (err < 0)
1737                 return err;
1738
1739         driver_list = g_slist_append(driver_list, driver);
1740
1741         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1742                 supplicant_probe(connection, NULL);
1743         else
1744                 supplicant_activate(connection);
1745
1746         return 0;
1747 }
1748
1749 void supplicant_unregister(struct supplicant_driver *driver)
1750 {
1751         DBG("driver %p name %s", driver, driver->name);
1752
1753         supplicant_remove(connection, NULL);
1754
1755         driver_list = g_slist_remove(driver_list, driver);
1756
1757         supplicant_destroy();
1758 }