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