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