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