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