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