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