Fix to replace unprintable characters with a white space
[platform/upstream/connman.git] / 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                 const char *auth_alg = "OPEN SHARED";
751                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
752                                                 DBUS_TYPE_STRING, &key_mgmt);
753
754                 connman_dbus_dict_append_variant(&dict, "auth_alg",
755                                                 DBUS_TYPE_STRING, &auth_alg);
756
757                 if (passphrase) {
758                         int size = strlen(passphrase);
759                         if (size == 10 || size == 26) {
760                                 unsigned char *key = malloc(13);
761                                 char tmp[3];
762                                 int i;
763                                 memset(tmp, 0, sizeof(tmp));
764                                 if (key == NULL)
765                                         size = 0;
766                                 for (i = 0; i < size / 2; i++) {
767                                         memcpy(tmp, passphrase + (i * 2), 2);
768                                         key[i] = (unsigned char) strtol(tmp,
769                                                                 NULL, 16);
770                                 }
771                                 connman_dbus_dict_append_array(&dict,
772                                                 "wep_key0", DBUS_TYPE_BYTE,
773                                                         &key, size / 2);
774                                 free(key);
775                         } else
776                                 connman_dbus_dict_append_variant(&dict,
777                                                 "wep_key0", DBUS_TYPE_STRING,
778                                                                 &passphrase);
779                         connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
780                                                 DBUS_TYPE_STRING, &index);
781                 }
782         } else {
783                 const char *key_mgmt = "NONE";
784                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
785                                                 DBUS_TYPE_STRING, &key_mgmt);
786         }
787
788         dbus_message_iter_close_container(&array, &dict);
789
790         dbus_error_init(&error);
791
792         reply = dbus_connection_send_with_reply_and_block(connection,
793                                                         message, -1, &error);
794         if (reply == NULL) {
795                 if (dbus_error_is_set(&error) == TRUE) {
796                         connman_error("%s", error.message);
797                         dbus_error_free(&error);
798                 } else
799                         connman_error("Failed to set network options");
800                 dbus_message_unref(message);
801                 return -EIO;
802         }
803
804         dbus_message_unref(message);
805
806         dbus_message_unref(reply);
807
808         return 0;
809 }
810
811 static int initiate_scan(struct supplicant_task *task)
812 {
813         DBusMessage *message;
814         DBusPendingCall *call;
815
816         DBG("task %p", task);
817
818         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
819                                         SUPPLICANT_INTF ".Interface", "scan");
820         if (message == NULL)
821                 return -ENOMEM;
822
823         if (dbus_connection_send_with_reply(connection, message,
824                                                 &call, TIMEOUT) == FALSE) {
825                 connman_error("Failed to initiate scan");
826                 dbus_message_unref(message);
827                 return -EIO;
828         }
829
830         dbus_message_unref(message);
831
832         return 0;
833 }
834
835 static struct {
836         char *name;
837         char *value;
838 } special_ssid[] = {
839         { "<hidden>", "hidden"  },
840         { "default",  "linksys" },
841         { "wireless"  },
842         { "linksys"   },
843         { "netgear"   },
844         { "dlink"     },
845         { "2wire"     },
846         { "compaq"    },
847         { "tsunami"   },
848         { "comcomcom", "3com"     },
849         { "3Com",      "3com"     },
850         { "Symbol",    "symbol"   },
851         { "Motorola",  "motorola" },
852         { "Wireless" , "wireless" },
853         { "WLAN",      "wlan"     },
854         { }
855 };
856
857 static char *build_group(const char *addr, const char *name,
858                         const unsigned char *ssid, unsigned int ssid_len,
859                                         const char *mode, const char *security)
860 {
861         GString *str;
862         unsigned int i;
863
864         if (addr == NULL)
865                 return NULL;
866
867         str = g_string_sized_new((ssid_len * 2) + 24);
868         if (str == NULL)
869                 return NULL;
870
871         for (i = 0; special_ssid[i].name; i++) {
872                 if (g_strcmp0(special_ssid[i].name, name) == 0) {
873                         if (special_ssid[i].value == NULL)
874                                 g_string_append_printf(str, "%s_%s",
875                                                                 name, addr);
876                         else
877                                 g_string_append_printf(str, "%s_%s",
878                                                 special_ssid[i].value, addr);
879                         goto done;
880                 }
881         }
882
883         if (ssid_len > 0 && ssid[0] != '\0') {
884                 for (i = 0; i < ssid_len; i++)
885                         g_string_append_printf(str, "%02x", ssid[i]);
886         } else
887                 g_string_append_printf(str, "hidden_%s", addr);
888
889 done:
890         g_string_append_printf(str, "_%s_%s", mode, security);
891
892         return g_string_free(str, FALSE);
893 }
894
895 static void extract_addr(DBusMessageIter *value,
896                                         struct supplicant_result *result)
897 {
898         DBusMessageIter array;
899         struct ether_addr *eth;
900         unsigned char *addr;
901         int addr_len;
902
903         dbus_message_iter_recurse(value, &array);
904         dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
905
906         if (addr_len != 6)
907                 return;
908
909         result->addr = g_try_malloc(addr_len);
910         if (result->addr == NULL)
911                 return;
912
913         memcpy(result->addr, addr, addr_len);
914         result->addr_len = addr_len;
915
916         result->path = g_try_malloc0(13);
917         if (result->path == NULL)
918                 return;
919
920         eth = (void *) addr;
921
922         snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
923                                                 eth->ether_addr_octet[0],
924                                                 eth->ether_addr_octet[1],
925                                                 eth->ether_addr_octet[2],
926                                                 eth->ether_addr_octet[3],
927                                                 eth->ether_addr_octet[4],
928                                                 eth->ether_addr_octet[5]);
929 }
930
931 static void extract_ssid(DBusMessageIter *value,
932                                         struct supplicant_result *result)
933 {
934         DBusMessageIter array;
935         unsigned char *ssid;
936         int ssid_len, i;
937
938         dbus_message_iter_recurse(value, &array);
939         dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
940
941         if (ssid_len < 1)
942                 return;
943
944         result->ssid = g_try_malloc(ssid_len);
945         if (result->ssid == NULL)
946                 return;
947
948         memcpy(result->ssid, ssid, ssid_len);
949         result->ssid_len = ssid_len;
950
951         result->name = g_try_malloc0(ssid_len + 1);
952         if (result->name == NULL)
953                 return;
954
955         for (i = 0; i < ssid_len; i++) {
956                 if (g_ascii_isprint(ssid[i]))
957                         result->name[i] = ssid[i];
958                 else
959                         result->name[i] = ' ';
960         }
961 }
962
963 static void extract_wpaie(DBusMessageIter *value,
964                                         struct supplicant_result *result)
965 {
966         DBusMessageIter array;
967         unsigned char *ie;
968         int ie_len;
969
970         dbus_message_iter_recurse(value, &array);
971         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
972
973         if (ie_len > 0)
974                 result->has_wpa = TRUE;
975 }
976
977 static void extract_rsnie(DBusMessageIter *value,
978                                         struct supplicant_result *result)
979 {
980         DBusMessageIter array;
981         unsigned char *ie;
982         int ie_len;
983
984         dbus_message_iter_recurse(value, &array);
985         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
986
987         if (ie_len > 0)
988                 result->has_rsn = TRUE;
989 }
990
991 static void extract_wpsie(DBusMessageIter *value,
992                                         struct supplicant_result *result)
993 {
994         DBusMessageIter array;
995         unsigned char *ie;
996         int ie_len;
997
998         dbus_message_iter_recurse(value, &array);
999         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1000
1001         if (ie_len > 0)
1002                 result->has_wps = TRUE;
1003 }
1004
1005 static void extract_capabilites(DBusMessageIter *value,
1006                                         struct supplicant_result *result)
1007 {
1008         dbus_message_iter_get_basic(value, &result->capabilities);
1009
1010         if (result->capabilities & IEEE80211_CAP_ESS)
1011                 result->adhoc = FALSE;
1012         else if (result->capabilities & IEEE80211_CAP_IBSS)
1013                 result->adhoc = TRUE;
1014
1015         if (result->capabilities & IEEE80211_CAP_PRIVACY)
1016                 result->has_wep = TRUE;
1017 }
1018
1019 static unsigned char calculate_strength(struct supplicant_result *result)
1020 {
1021         if (result->quality < 0) {
1022                 unsigned char strength;
1023
1024                 if (result->level > 0)
1025                         strength = 100 - result->level;
1026                 else
1027                         strength = 120 + result->level;
1028
1029                 if (strength > 100)
1030                         strength = 100;
1031
1032                 return strength;
1033         }
1034
1035         return result->quality;
1036 }
1037
1038 static unsigned short calculate_channel(struct supplicant_result *result)
1039 {
1040         if (result->frequency < 0)
1041                 return 0;
1042
1043         return (result->frequency - 2407) / 5;
1044 }
1045
1046 static void get_properties(struct supplicant_task *task);
1047
1048 static void properties_reply(DBusPendingCall *call, void *user_data)
1049 {
1050         struct supplicant_task *task = user_data;
1051         struct supplicant_result result;
1052         struct connman_network *network;
1053         DBusMessage *reply;
1054         DBusMessageIter array, dict;
1055         unsigned char strength;
1056         unsigned short channel, frequency;
1057         const char *mode, *security;
1058         char *group;
1059
1060         DBG("task %p", task);
1061
1062         reply = dbus_pending_call_steal_reply(call);
1063         if (reply == NULL) {
1064                 get_properties(task);
1065                 return;
1066         }
1067
1068         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1069                 dbus_message_unref(reply);
1070                 get_properties(task);
1071                 return;
1072         }
1073
1074         memset(&result, 0, sizeof(result));
1075         result.frequency = -1;
1076         result.quality = -1;
1077         result.level = 0;
1078         result.noise = 0;
1079
1080         dbus_message_iter_init(reply, &array);
1081
1082         dbus_message_iter_recurse(&array, &dict);
1083
1084         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1085                 DBusMessageIter entry, value;
1086                 const char *key;
1087
1088                 dbus_message_iter_recurse(&dict, &entry);
1089                 dbus_message_iter_get_basic(&entry, &key);
1090
1091                 dbus_message_iter_next(&entry);
1092
1093                 dbus_message_iter_recurse(&entry, &value);
1094
1095                 //type = dbus_message_iter_get_arg_type(&value);
1096                 //dbus_message_iter_get_basic(&value, &val);
1097
1098                 /* 
1099                  * bssid        : a (97)
1100                  * ssid         : a (97)
1101                  * wpaie        : a (97)
1102                  * rsnie        : a (97)
1103                  * wpsie        : a (97)
1104                  * frequency    : i (105)
1105                  * capabilities : q (113)
1106                  * quality      : i (105)
1107                  * noise        : i (105)
1108                  * level        : i (105)
1109                  * maxrate      : i (105)
1110                  */
1111
1112                 if (g_str_equal(key, "bssid") == TRUE)
1113                         extract_addr(&value, &result);
1114                 else if (g_str_equal(key, "ssid") == TRUE)
1115                         extract_ssid(&value, &result);
1116                 else if (g_str_equal(key, "wpaie") == TRUE)
1117                         extract_wpaie(&value, &result);
1118                 else if (g_str_equal(key, "rsnie") == TRUE)
1119                         extract_rsnie(&value, &result);
1120                 else if (g_str_equal(key, "wpsie") == TRUE)
1121                         extract_wpsie(&value, &result);
1122                 else if (g_str_equal(key, "capabilities") == TRUE)
1123                         extract_capabilites(&value, &result);
1124                 else if (g_str_equal(key, "frequency") == TRUE)
1125                         dbus_message_iter_get_basic(&value, &result.frequency);
1126                 else if (g_str_equal(key, "quality") == TRUE)
1127                         dbus_message_iter_get_basic(&value, &result.quality);
1128                 else if (g_str_equal(key, "noise") == TRUE)
1129                         dbus_message_iter_get_basic(&value, &result.noise);
1130                 else if (g_str_equal(key, "level") == TRUE)
1131                         dbus_message_iter_get_basic(&value, &result.level);
1132                 else if (g_str_equal(key, "maxrate") == TRUE)
1133                         dbus_message_iter_get_basic(&value, &result.maxrate);
1134
1135                 dbus_message_iter_next(&dict);
1136         }
1137
1138         if (result.path == NULL)
1139                 goto done;
1140
1141         if (result.path[0] == '\0')
1142                 goto done;
1143
1144         if (result.frequency > 0 && result.frequency < 14)
1145                 result.frequency = 2407 + (5 * result.frequency);
1146         else if (result.frequency == 14)
1147                 result.frequency = 2484;
1148
1149         strength = calculate_strength(&result);
1150         channel  = calculate_channel(&result);
1151
1152         frequency = (result.frequency < 0) ? 0 : result.frequency;
1153
1154         if (result.has_rsn == TRUE)
1155                 security = "rsn";
1156         else if (result.has_wpa == TRUE)
1157                 security = "wpa";
1158         else if (result.has_wep == TRUE)
1159                 security = "wep";
1160         else
1161                 security = "none";
1162
1163         mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1164
1165         group = build_group(result.path, result.name,
1166                                         result.ssid, result.ssid_len,
1167                                                         mode, security);
1168
1169         network = connman_device_get_network(task->device, result.path);
1170         if (network == NULL) {
1171                 int index;
1172
1173                 network = connman_network_create(result.path,
1174                                                 CONNMAN_NETWORK_TYPE_WIFI);
1175                 if (network == NULL)
1176                         goto done;
1177
1178                 index = connman_device_get_index(task->device);
1179                 connman_network_set_index(network, index);
1180
1181                 connman_network_set_protocol(network,
1182                                                 CONNMAN_NETWORK_PROTOCOL_IP);
1183
1184                 connman_network_set_address(network, result.addr,
1185                                                         result.addr_len);
1186
1187                 if (connman_device_add_network(task->device, network) < 0) {
1188                         connman_network_unref(network);
1189                         goto done;
1190                 }
1191         }
1192
1193         if (result.name != NULL && result.name[0] != '\0')
1194                 connman_network_set_name(network, result.name);
1195
1196         connman_network_set_blob(network, "WiFi.SSID",
1197                                                 result.ssid, result.ssid_len);
1198
1199         connman_network_set_string(network, "WiFi.Mode", mode);
1200
1201         DBG("%s (%s %s) strength %d (%s)",
1202                                 result.name, mode, security, strength,
1203                                 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1204
1205         connman_network_set_available(network, TRUE);
1206         connman_network_set_strength(network, strength);
1207
1208         connman_network_set_uint16(network, "Frequency", frequency);
1209         connman_network_set_uint16(network, "WiFi.Channel", channel);
1210         connman_network_set_string(network, "WiFi.Security", security);
1211
1212         connman_network_set_group(network, group);
1213
1214         g_free(group);
1215
1216 done:
1217         g_free(result.path);
1218         g_free(result.addr);
1219         g_free(result.name);
1220         g_free(result.ssid);
1221
1222         dbus_message_unref(reply);
1223
1224         get_properties(task);
1225 }
1226
1227 static void get_properties(struct supplicant_task *task)
1228 {
1229         DBusMessage *message;
1230         DBusPendingCall *call;
1231         char *path;
1232
1233         path = g_slist_nth_data(task->scan_results, 0);
1234         if (path == NULL)
1235                 goto noscan;
1236
1237         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1238                                                 SUPPLICANT_INTF ".BSSID",
1239                                                                 "properties");
1240
1241         task->scan_results = g_slist_remove(task->scan_results, path);
1242         g_free(path);
1243
1244         if (message == NULL)
1245                 goto noscan;
1246
1247         if (dbus_connection_send_with_reply(connection, message,
1248                                                 &call, TIMEOUT) == FALSE) {
1249                 connman_error("Failed to get network properties");
1250                 dbus_message_unref(message);
1251                 goto noscan;
1252         }
1253
1254         if (call == NULL) {
1255                 connman_error("D-Bus connection not available");
1256                 dbus_message_unref(message);
1257                 goto noscan;
1258         }
1259
1260         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1261
1262         dbus_message_unref(message);
1263
1264         return;
1265
1266 noscan:
1267         if (task->noscan == FALSE)
1268                 connman_device_set_scanning(task->device, FALSE);
1269 }
1270
1271 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1272 {
1273         struct supplicant_task *task = user_data;
1274         DBusMessage *reply;
1275         DBusError error;
1276         char **results;
1277         int i, num_results;
1278
1279         DBG("task %p", task);
1280
1281         reply = dbus_pending_call_steal_reply(call);
1282         if (reply == NULL)
1283                 goto noscan;
1284
1285         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1286                 goto done;
1287
1288         dbus_error_init(&error);
1289
1290         if (dbus_message_get_args(reply, &error,
1291                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1292                                                 &results, &num_results,
1293                                                 DBUS_TYPE_INVALID) == FALSE) {
1294                 if (dbus_error_is_set(&error) == TRUE) {
1295                         connman_error("%s", error.message);
1296                         dbus_error_free(&error);
1297                 } else
1298                         connman_error("Wrong arguments for scan result");
1299                 goto done;
1300         }
1301
1302         if (num_results == 0)
1303                 goto done;
1304
1305         for (i = 0; i < num_results; i++) {
1306                 char *path = g_strdup(results[i]);
1307                 if (path == NULL)
1308                         continue;
1309
1310                 task->scan_results = g_slist_append(task->scan_results, path);
1311         }
1312
1313         g_strfreev(results);
1314
1315         dbus_message_unref(reply);
1316
1317         get_properties(task);
1318
1319         return;
1320
1321 done:
1322         dbus_message_unref(reply);
1323
1324 noscan:
1325         if (task->noscan == FALSE)
1326                 connman_device_set_scanning(task->device, FALSE);
1327 }
1328
1329 static void scan_results_available(struct supplicant_task *task)
1330 {
1331         DBusMessage *message;
1332         DBusPendingCall *call;
1333
1334         DBG("task %p", task);
1335
1336         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1337                                                 SUPPLICANT_INTF ".Interface",
1338                                                         "scanResults");
1339         if (message == NULL)
1340                 return;
1341
1342         if (dbus_connection_send_with_reply(connection, message,
1343                                                 &call, TIMEOUT) == FALSE) {
1344                 connman_error("Failed to request scan result");
1345                 goto done;
1346         }
1347
1348         if (task->noscan == FALSE)
1349                 connman_device_set_scanning(task->device, TRUE);
1350
1351         if (call == NULL) {
1352                 connman_error("D-Bus connection not available");
1353                 goto done;
1354         }
1355
1356         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1357
1358 done:
1359         dbus_message_unref(message);
1360 }
1361
1362 static enum supplicant_state string2state(const char *state)
1363 {
1364         if (g_str_equal(state, "INACTIVE") == TRUE)
1365                 return WPA_INACTIVE;
1366         else if (g_str_equal(state, "SCANNING") == TRUE)
1367                 return WPA_SCANNING;
1368         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1369                 return WPA_ASSOCIATING;
1370         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1371                 return WPA_ASSOCIATED;
1372         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1373                 return WPA_GROUP_HANDSHAKE;
1374         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1375                 return WPA_4WAY_HANDSHAKE;
1376         else if (g_str_equal(state, "COMPLETED") == TRUE)
1377                 return WPA_COMPLETED;
1378         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1379                 return WPA_DISCONNECTED;
1380         else
1381                 return WPA_INVALID;
1382 }
1383
1384 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1385 {
1386         DBusError error;
1387         const char *newstate, *oldstate;
1388         enum supplicant_state state;
1389
1390         dbus_error_init(&error);
1391
1392         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1393                                                 DBUS_TYPE_STRING, &oldstate,
1394                                                 DBUS_TYPE_INVALID) == FALSE) {
1395                 if (dbus_error_is_set(&error) == TRUE) {
1396                         connman_error("%s", error.message);
1397                         dbus_error_free(&error);
1398                 } else
1399                         connman_error("Wrong arguments for state change");
1400                 return;
1401         }
1402
1403         DBG("state %s ==> %s", oldstate, newstate);
1404
1405         state = string2state(newstate);
1406         if (state == WPA_INVALID)
1407                 return;
1408
1409         task->state = state;
1410
1411         switch (task->state) {
1412         case WPA_SCANNING:
1413                 task->noscan = TRUE;
1414                 connman_device_set_scanning(task->device, TRUE);
1415                 break;
1416         case WPA_ASSOCIATING:
1417         case WPA_ASSOCIATED:
1418         case WPA_4WAY_HANDSHAKE:
1419         case WPA_GROUP_HANDSHAKE:
1420                 task->noscan = TRUE;
1421                 break;
1422         case WPA_COMPLETED:
1423         case WPA_DISCONNECTED:
1424                 task->noscan = FALSE;
1425                 break;
1426         case WPA_INACTIVE:
1427                 task->noscan = FALSE;
1428                 connman_device_set_scanning(task->device, FALSE);
1429                 break;
1430         case WPA_INVALID:
1431                 break;
1432         }
1433
1434         if (task->network == NULL)
1435                 return;
1436
1437         switch (task->state) {
1438         case WPA_COMPLETED:
1439                 /* carrier on */
1440                 connman_network_set_connected(task->network, TRUE);
1441                 connman_device_set_scanning(task->device, FALSE);
1442                 break;
1443         case WPA_DISCONNECTED:
1444                 /* carrier off */
1445                 connman_network_set_connected(task->network, FALSE);
1446                 connman_device_set_scanning(task->device, FALSE);
1447                 break;
1448         case WPA_ASSOCIATING:
1449                 connman_network_set_associating(task->network, TRUE);
1450                 break;
1451         default:
1452                 connman_network_set_associating(task->network, FALSE);
1453                 break;
1454         }
1455 }
1456
1457 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1458                                                 DBusMessage *msg, void *data)
1459 {
1460         struct supplicant_task *task;
1461         const char *member, *path;
1462
1463         if (dbus_message_has_interface(msg,
1464                                 SUPPLICANT_INTF ".Interface") == FALSE)
1465                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1466
1467         member = dbus_message_get_member(msg);
1468         if (member == NULL)
1469                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1470
1471         path = dbus_message_get_path(msg);
1472         if (path == NULL)
1473                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1474
1475         task = find_task_by_path(path);
1476         if (task == NULL)
1477                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1478
1479         DBG("task %p member %s", task, member);
1480
1481         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1482                 scan_results_available(task);
1483         else if (g_str_equal(member, "StateChange") == TRUE)
1484                 state_change(task, msg);
1485
1486         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1487 }
1488
1489 int supplicant_start(struct connman_device *device)
1490 {
1491         struct supplicant_task *task;
1492
1493         DBG("device %p", device);
1494
1495         task = g_try_new0(struct supplicant_task, 1);
1496         if (task == NULL)
1497                 return -ENOMEM;
1498
1499         task->ifindex = connman_device_get_index(device);
1500         task->ifname = connman_inet_ifname(task->ifindex);
1501
1502         if (task->ifname == NULL) {
1503                 g_free(task);
1504                 return -ENOMEM;
1505         }
1506
1507         task->device = connman_device_ref(device);
1508
1509         task->created = FALSE;
1510         task->noscan = FALSE;
1511         task->state = WPA_INVALID;
1512
1513         task_list = g_slist_append(task_list, task);
1514
1515         return create_interface(task);
1516 }
1517
1518 int supplicant_stop(struct connman_device *device)
1519 {
1520         int index = connman_device_get_index(device);
1521         struct supplicant_task *task;
1522
1523         DBG("device %p", device);
1524
1525         task = find_task_by_index(index);
1526         if (task == NULL)
1527                 return -ENODEV;
1528
1529         task_list = g_slist_remove(task_list, task);
1530
1531         disable_network(task);
1532
1533         remove_network(task);
1534
1535         return remove_interface(task);
1536 }
1537
1538 int supplicant_scan(struct connman_device *device)
1539 {
1540         int index = connman_device_get_index(device);
1541         struct supplicant_task *task;
1542         int err;
1543
1544         DBG("device %p", device);
1545
1546         task = find_task_by_index(index);
1547         if (task == NULL)
1548                 return -ENODEV;
1549
1550         switch (task->state) {
1551         case WPA_SCANNING:
1552                 return -EALREADY;
1553         case WPA_ASSOCIATING:
1554         case WPA_ASSOCIATED:
1555         case WPA_4WAY_HANDSHAKE:
1556         case WPA_GROUP_HANDSHAKE:
1557                 return -EBUSY;
1558         default:
1559                 break;
1560         }
1561
1562         err = initiate_scan(task);
1563
1564         return 0;
1565 }
1566
1567 int supplicant_connect(struct connman_network *network)
1568 {
1569         struct supplicant_task *task;
1570         const char *address, *security, *passphrase;
1571         const void *ssid;
1572         unsigned int ssid_len;
1573         int index;
1574
1575         DBG("network %p", network);
1576
1577         address = connman_network_get_string(network, "Address");
1578         security = connman_network_get_string(network, "WiFi.Security");
1579         passphrase = connman_network_get_string(network, "WiFi.Passphrase");
1580
1581         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
1582
1583         DBG("address %s security %s passphrase %s",
1584                                         address, security, passphrase);
1585
1586         if (security == NULL && passphrase == NULL)
1587                 return -EINVAL;
1588
1589         if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1590                 return -EINVAL;
1591
1592         index = connman_network_get_index(network);
1593
1594         task = find_task_by_index(index);
1595         if (task == NULL)
1596                 return -ENODEV;
1597
1598         task->network = connman_network_ref(network);
1599
1600         add_network(task);
1601
1602         select_network(task);
1603         disable_network(task);
1604
1605         set_network(task, ssid, ssid_len, address, security, passphrase);
1606
1607         enable_network(task);
1608
1609         connman_network_set_associating(task->network, TRUE);
1610
1611         return 0;
1612 }
1613
1614 int supplicant_disconnect(struct connman_network *network)
1615 {
1616         struct supplicant_task *task;
1617         int index;
1618
1619         DBG("network %p", network);
1620
1621         index = connman_network_get_index(network);
1622
1623         task = find_task_by_index(index);
1624         if (task == NULL)
1625                 return -ENODEV;
1626
1627         disable_network(task);
1628
1629         remove_network(task);
1630
1631         connman_network_set_connected(task->network, FALSE);
1632
1633         connman_network_unref(task->network);
1634
1635         return 0;
1636 }
1637
1638 static void supplicant_activate(DBusConnection *conn)
1639 {
1640         DBusMessage *message;
1641
1642         DBG("conn %p", conn);
1643
1644         message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1645                                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1646         if (message == NULL)
1647                 return;
1648
1649         dbus_message_set_no_reply(message, TRUE);
1650
1651         dbus_connection_send(conn, message, NULL);
1652
1653         dbus_message_unref(message);
1654 }
1655
1656 static GSList *driver_list = NULL;
1657
1658 static void supplicant_probe(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->probe)
1670                         driver->probe();
1671         }
1672 }
1673
1674 static void supplicant_remove(DBusConnection *conn, void *user_data)
1675 {
1676         GSList *list;
1677
1678         DBG("conn %p", conn);
1679
1680         for (list = driver_list; list; list = list->next) {
1681                 struct supplicant_driver *driver = list->data;
1682
1683                 DBG("driver %p name %s", driver, driver->name);
1684
1685                 if (driver->remove)
1686                         driver->remove();
1687         }
1688 }
1689
1690 static const char *supplicant_rule = "type=signal,"
1691                                 "interface=" SUPPLICANT_INTF ".Interface";
1692 static guint watch;
1693
1694 static int supplicant_create(void)
1695 {
1696         if (g_slist_length(driver_list) > 0)
1697                 return 0;
1698
1699         connection = connman_dbus_get_connection();
1700         if (connection == NULL)
1701                 return -EIO;
1702
1703         DBG("connection %p", connection);
1704
1705         if (dbus_connection_add_filter(connection,
1706                                 supplicant_filter, NULL, NULL) == FALSE) {
1707                 connection = connman_dbus_get_connection();
1708                 return -EIO;
1709         }
1710
1711         dbus_bus_add_match(connection, supplicant_rule, NULL);
1712         dbus_connection_flush(connection);
1713
1714         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1715                         supplicant_probe, supplicant_remove, NULL, NULL);
1716
1717         return 0;
1718 }
1719
1720 static void supplicant_destroy(void)
1721 {
1722         if (g_slist_length(driver_list) > 0)
1723                 return;
1724
1725         DBG("connection %p", connection);
1726
1727         if (watch > 0)
1728                 g_dbus_remove_watch(connection, watch);
1729
1730         dbus_bus_remove_match(connection, supplicant_rule, NULL);
1731         dbus_connection_flush(connection);
1732
1733         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1734
1735         dbus_connection_unref(connection);
1736         connection = NULL;
1737 }
1738
1739 int supplicant_register(struct supplicant_driver *driver)
1740 {
1741         int err;
1742
1743         DBG("driver %p name %s", driver, driver->name);
1744
1745         err = supplicant_create();
1746         if (err < 0)
1747                 return err;
1748
1749         driver_list = g_slist_append(driver_list, driver);
1750
1751         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1752                 supplicant_probe(connection, NULL);
1753         else
1754                 supplicant_activate(connection);
1755
1756         return 0;
1757 }
1758
1759 void supplicant_unregister(struct supplicant_driver *driver)
1760 {
1761         DBG("driver %p name %s", driver, driver->name);
1762
1763         supplicant_remove(connection, NULL);
1764
1765         driver_list = g_slist_remove(driver_list, driver);
1766
1767         supplicant_destroy();
1768 }