Add task_connect() function to WiFi supplicant
[platform/upstream/connman.git] / 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 <unistd.h>
28 #include <errno.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <linux/if_arp.h>
34 #include <linux/wireless.h>
35 #include <net/ethernet.h>
36
37 #include <gdbus.h>
38
39 #define CONNMAN_API_SUBJECT_TO_CHANGE
40 #include <connman/device.h>
41 #include <connman/option.h>
42 #include <connman/inet.h>
43 #include <connman/dbus.h>
44 #include <connman/log.h>
45
46 #include "supplicant.h"
47
48 #define TIMEOUT 5000
49
50 #define IEEE80211_CAP_ESS       0x0001
51 #define IEEE80211_CAP_IBSS      0x0002
52 #define IEEE80211_CAP_PRIVACY   0x0010
53
54 #define SUPPLICANT_NAME  "fi.epitest.hostap.WPASupplicant"
55 #define SUPPLICANT_INTF  "fi.epitest.hostap.WPASupplicant"
56 #define SUPPLICANT_PATH  "/fi/epitest/hostap/WPASupplicant"
57
58 /* Taken from "WPA Supplicant - Common definitions" */
59 enum supplicant_state {
60         /**
61          * WPA_DISCONNECTED - Disconnected state
62          *
63          * This state indicates that client is not associated, but is likely to
64          * start looking for an access point. This state is entered when a
65          * connection is lost.
66          */
67         WPA_DISCONNECTED,
68
69         /**
70          * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
71          *
72          * This state is entered if there are no enabled networks in the
73          * configuration. wpa_supplicant is not trying to associate with a new
74          * network and external interaction (e.g., ctrl_iface call to add or
75          * enable a network) is needed to start association.
76          */
77         WPA_INACTIVE,
78
79         /**
80          * WPA_SCANNING - Scanning for a network
81          *
82          * This state is entered when wpa_supplicant starts scanning for a
83          * network.
84          */
85         WPA_SCANNING,
86
87         /**
88          * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
89          *
90          * This state is entered when wpa_supplicant has found a suitable BSS
91          * to associate with and the driver is configured to try to associate
92          * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
93          * state is entered when the driver is configured to try to associate
94          * with a network using the configured SSID and security policy.
95          */
96         WPA_ASSOCIATING,
97
98         /**
99          * WPA_ASSOCIATED - Association completed
100          *
101          * This state is entered when the driver reports that association has
102          * been successfully completed with an AP. If IEEE 802.1X is used
103          * (with or without WPA/WPA2), wpa_supplicant remains in this state
104          * until the IEEE 802.1X/EAPOL authentication has been completed.
105          */
106         WPA_ASSOCIATED,
107
108         /**
109          * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
110          *
111          * This state is entered when WPA/WPA2 4-Way Handshake is started. In
112          * case of WPA-PSK, this happens when receiving the first EAPOL-Key
113          * frame after association. In case of WPA-EAP, this state is entered
114          * when the IEEE 802.1X/EAPOL authentication has been completed.
115          */
116         WPA_4WAY_HANDSHAKE,
117
118         /**
119          * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
120          *
121          * This state is entered when 4-Way Key Handshake has been completed
122          * (i.e., when the supplicant sends out message 4/4) and when Group
123          * Key rekeying is started by the AP (i.e., when supplicant receives
124          * message 1/2).
125          */
126         WPA_GROUP_HANDSHAKE,
127
128         /**
129          * WPA_COMPLETED - All authentication completed
130          *
131          * This state is entered when the full authentication process is
132          * completed. In case of WPA2, this happens when the 4-Way Handshake is
133          * successfully completed. With WPA, this state is entered after the
134          * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
135          * completed after dynamic keys are received (or if not used, after
136          * the EAP authentication has been completed). With static WEP keys and
137          * plaintext connections, this state is entered when an association
138          * has been completed.
139          *
140          * This state indicates that the supplicant has completed its
141          * processing for the association phase and that data connection is
142          * fully configured.
143          */
144         WPA_COMPLETED,
145
146         /**
147          * WPA_INVALID - Invalid state (parsing error)
148          *
149          * This state is returned if the string input is invalid. It is not
150          * an official wpa_supplicant state.
151          */
152         WPA_INVALID,
153 };
154
155 struct supplicant_result {
156         char *path;
157         char *name;
158         unsigned char *addr;
159         unsigned int addr_len;
160         unsigned char *ssid;
161         unsigned int ssid_len;
162         dbus_uint16_t capabilities;
163         gboolean adhoc;
164         gboolean has_wep;
165         gboolean has_wpa;
166         gboolean has_rsn;
167         gboolean has_wps;
168         dbus_int32_t frequency;
169         dbus_int32_t quality;
170         dbus_int32_t noise;
171         dbus_int32_t level;
172         dbus_int32_t maxrate;
173 };
174
175 struct supplicant_task {
176         int ifindex;
177         char *ifname;
178         struct connman_device *device;
179         struct connman_network *network;
180         char *path;
181         char *netpath;
182         gboolean created;
183         enum supplicant_state state;
184         gboolean noscan;
185         GSList *scan_results;
186         struct iw_range *range;
187 };
188
189 static GSList *task_list = NULL;
190
191 static DBusConnection *connection;
192
193 static void free_task(struct supplicant_task *task)
194 {
195         DBG("task %p", task);
196
197         g_free(task->ifname);
198         g_free(task->path);
199         g_free(task);
200 }
201
202 static struct supplicant_task *find_task_by_index(int index)
203 {
204         GSList *list;
205
206         for (list = task_list; list; list = list->next) {
207                 struct supplicant_task *task = list->data;
208
209                 if (task->ifindex == index)
210                         return task;
211         }
212
213         return NULL;
214 }
215
216 static struct supplicant_task *find_task_by_path(const char *path)
217 {
218         GSList *list;
219
220         for (list = task_list; list; list = list->next) {
221                 struct supplicant_task *task = list->data;
222
223                 if (g_strcmp0(task->path, path) == 0)
224                         return task;
225         }
226
227         return NULL;
228 }
229
230 static void add_interface_reply(DBusPendingCall *call, void *user_data)
231 {
232         struct supplicant_task *task = user_data;
233         DBusMessage *reply;
234         DBusError error;
235         const char *path;
236
237         DBG("task %p", task);
238
239         reply = dbus_pending_call_steal_reply(call);
240         if (reply == NULL)
241                 return;
242
243         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
244                 goto failed;
245
246         dbus_error_init(&error);
247
248         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
249                                                 DBUS_TYPE_INVALID) == FALSE) {
250                 if (dbus_error_is_set(&error) == TRUE) {
251                         connman_error("%s", error.message);
252                         dbus_error_free(&error);
253                 } else
254                         connman_error("Wrong arguments for add interface");
255                 goto failed;
256         }
257
258         DBG("path %s", path);
259
260         task->path = g_strdup(path);
261         task->created = TRUE;
262
263         connman_device_set_powered(task->device, TRUE);
264
265         dbus_message_unref(reply);
266
267         return;
268
269 failed:
270         task_list = g_slist_remove(task_list, task);
271
272         connman_device_unref(task->device);
273
274         free_task(task);
275 }
276
277 static int add_interface(struct supplicant_task *task)
278 {
279         const char *driver = connman_option_get_string("wifi");
280         DBusMessage *message;
281         DBusMessageIter array, dict;
282         DBusPendingCall *call;
283
284         DBG("task %p", task);
285
286         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
287                                         SUPPLICANT_INTF, "addInterface");
288         if (message == NULL)
289                 return -ENOMEM;
290
291         dbus_message_iter_init_append(message, &array);
292
293         dbus_message_iter_append_basic(&array,
294                                         DBUS_TYPE_STRING, &task->ifname);
295
296         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
297                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
298                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
299                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
300
301         connman_dbus_dict_append_variant(&dict, "driver",
302                                                 DBUS_TYPE_STRING, &driver);
303
304         dbus_message_iter_close_container(&array, &dict);
305
306         if (dbus_connection_send_with_reply(connection, message,
307                                                 &call, TIMEOUT) == FALSE) {
308                 connman_error("Failed to add interface");
309                 dbus_message_unref(message);
310                 return -EIO;
311         }
312
313         if (call == NULL) {
314                 connman_error("D-Bus connection not available");
315                 dbus_message_unref(message);
316                 return -EIO;
317         }
318
319         dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
320
321         dbus_message_unref(message);
322
323         return -EINPROGRESS;
324 }
325
326 static void get_interface_reply(DBusPendingCall *call, void *user_data)
327 {
328         struct supplicant_task *task = user_data;
329         DBusMessage *reply;
330         DBusError error;
331         const char *path;
332
333         DBG("task %p", task);
334
335         reply = dbus_pending_call_steal_reply(call);
336         if (reply == NULL)
337                 return;
338
339         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
340                 add_interface(task);
341                 goto done;
342         }
343
344         dbus_error_init(&error);
345
346         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
347                                                 DBUS_TYPE_INVALID) == FALSE) {
348                 if (dbus_error_is_set(&error) == TRUE) {
349                         connman_error("%s", error.message);
350                         dbus_error_free(&error);
351                 } else
352                         connman_error("Wrong arguments for get interface");
353                 goto done;
354         }
355
356         DBG("path %s", path);
357
358         task->path = g_strdup(path);
359         task->created = FALSE;
360
361         connman_device_set_powered(task->device, TRUE);
362
363 done:
364         dbus_message_unref(reply);
365 }
366
367 static int create_interface(struct supplicant_task *task)
368 {
369         DBusMessage *message;
370         DBusPendingCall *call;
371
372         DBG("task %p", task);
373
374         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
375                                         SUPPLICANT_INTF, "getInterface");
376         if (message == NULL)
377                 return -ENOMEM;
378
379         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
380                                                         DBUS_TYPE_INVALID);
381
382         if (dbus_connection_send_with_reply(connection, message,
383                                                 &call, TIMEOUT) == FALSE) {
384                 connman_error("Failed to get interface");
385                 dbus_message_unref(message);
386                 return -EIO;
387         }
388
389         if (call == NULL) {
390                 connman_error("D-Bus connection not available");
391                 dbus_message_unref(message);
392                 return -EIO;
393         }
394
395         dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
396
397         dbus_message_unref(message);
398
399         return -EINPROGRESS;
400 }
401
402 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
403 {
404         struct supplicant_task *task = user_data;
405         DBusMessage *reply;
406
407         DBG("task %p", task);
408
409         reply = dbus_pending_call_steal_reply(call);
410
411         connman_device_set_powered(task->device, FALSE);
412
413         connman_device_unref(task->device);
414
415         connman_inet_ifdown(task->ifindex);
416
417         free_task(task);
418
419         dbus_message_unref(reply);
420 }
421
422 static int remove_interface(struct supplicant_task *task)
423 {
424         DBusMessage *message;
425         DBusPendingCall *call;
426
427         DBG("task %p", task);
428
429         if (task->created == FALSE) {
430                 connman_device_set_powered(task->device, FALSE);
431                 return 0;
432         }
433
434         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
435                                         SUPPLICANT_INTF, "removeInterface");
436         if (message == NULL)
437                 return -ENOMEM;
438
439         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
440                                                         DBUS_TYPE_INVALID);
441
442         if (dbus_connection_send_with_reply(connection, message,
443                                                 &call, TIMEOUT) == FALSE) {
444                 connman_error("Failed to remove interface");
445                 dbus_message_unref(message);
446                 return -EIO;
447         }
448
449         if (call == NULL) {
450                 connman_error("D-Bus connection not available");
451                 dbus_message_unref(message);
452                 return -EIO;
453         }
454
455         dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
456
457         dbus_message_unref(message);
458
459         return -EINPROGRESS;
460 }
461
462 #if 0
463 static int set_ap_scan(struct supplicant_task *task)
464 {
465         DBusMessage *message, *reply;
466         DBusError error;
467         guint32 ap_scan = 1;
468
469         DBG("task %p", task);
470
471         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
472                                 SUPPLICANT_INTF ".Interface", "setAPScan");
473         if (message == NULL)
474                 return -ENOMEM;
475
476         dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
477                                                         DBUS_TYPE_INVALID);
478
479         dbus_error_init(&error);
480
481         reply = dbus_connection_send_with_reply_and_block(connection,
482                                                         message, -1, &error);
483         if (reply == NULL) {
484                 if (dbus_error_is_set(&error) == TRUE) {
485                         connman_error("%s", error.message);
486                         dbus_error_free(&error);
487                 } else
488                         connman_error("Failed to set AP scan");
489                 dbus_message_unref(message);
490                 return -EIO;
491         }
492
493         dbus_message_unref(message);
494
495         dbus_message_unref(reply);
496
497         return 0;
498 }
499 #endif
500
501 static int add_network(struct supplicant_task *task)
502 {
503         DBusMessage *message, *reply;
504         DBusError error;
505         const char *path;
506
507         DBG("task %p", task);
508
509         if (task->netpath != NULL)
510                 return -EALREADY;
511
512         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
513                                 SUPPLICANT_INTF ".Interface", "addNetwork");
514         if (message == NULL)
515                 return -ENOMEM;
516
517         dbus_error_init(&error);
518
519         reply = dbus_connection_send_with_reply_and_block(connection,
520                                                         message, -1, &error);
521         if (reply == NULL) {
522                 if (dbus_error_is_set(&error) == TRUE) {
523                         connman_error("%s", error.message);
524                         dbus_error_free(&error);
525                 } else
526                         connman_error("Failed to add network");
527                 dbus_message_unref(message);
528                 return -EIO;
529         }
530
531         dbus_message_unref(message);
532
533         dbus_error_init(&error);
534
535         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
536                                                 DBUS_TYPE_INVALID) == FALSE) {
537                 if (dbus_error_is_set(&error) == TRUE) {
538                         connman_error("%s", error.message);
539                         dbus_error_free(&error);
540                 } else
541                         connman_error("Wrong arguments for network");
542                 dbus_message_unref(reply);
543                 return -EIO;
544         }
545
546         DBG("path %s", path);
547
548         task->netpath = g_strdup(path);
549
550         dbus_message_unref(reply);
551
552         return 0;
553 }
554
555 static int remove_network(struct supplicant_task *task)
556 {
557         DBusMessage *message, *reply;
558         DBusError error;
559
560         DBG("task %p", task);
561
562         if (task->netpath == NULL)
563                 return -EINVAL;
564
565         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
566                                 SUPPLICANT_INTF ".Interface", "removeNetwork");
567         if (message == NULL)
568                 return -ENOMEM;
569
570         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
571                                                         DBUS_TYPE_INVALID);
572
573         dbus_error_init(&error);
574
575         reply = dbus_connection_send_with_reply_and_block(connection,
576                                                         message, -1, &error);
577         if (reply == NULL) {
578                 if (dbus_error_is_set(&error) == TRUE) {
579                         connman_error("%s", error.message);
580                         dbus_error_free(&error);
581                 } else
582                         connman_error("Failed to remove network");
583                 dbus_message_unref(message);
584                 return -EIO;
585         }
586
587         dbus_message_unref(message);
588
589         dbus_message_unref(reply);
590
591         g_free(task->netpath);
592         task->netpath = NULL;
593
594         return 0;
595 }
596
597 static int select_network(struct supplicant_task *task)
598 {
599         DBusMessage *message, *reply;
600         DBusError error;
601
602         DBG("task %p", task);
603
604         if (task->netpath == NULL)
605                 return -EINVAL;
606
607         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
608                                 SUPPLICANT_INTF ".Interface", "selectNetwork");
609         if (message == NULL)
610                 return -ENOMEM;
611
612         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
613                                                         DBUS_TYPE_INVALID);
614
615         dbus_error_init(&error);
616
617         reply = dbus_connection_send_with_reply_and_block(connection,
618                                                         message, -1, &error);
619         if (reply == NULL) {
620                 if (dbus_error_is_set(&error) == TRUE) {
621                         connman_error("%s", error.message);
622                         dbus_error_free(&error);
623                 } else
624                         connman_error("Failed to select network");
625                 dbus_message_unref(message);
626                 return -EIO;
627         }
628
629         dbus_message_unref(message);
630
631         dbus_message_unref(reply);
632
633         return 0;
634 }
635
636 static int enable_network(struct supplicant_task *task)
637 {
638         DBusMessage *message, *reply;
639         DBusError error;
640
641         DBG("task %p", task);
642
643         if (task->netpath == NULL)
644                 return -EINVAL;
645
646         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
647                                         SUPPLICANT_INTF ".Network", "enable");
648         if (message == NULL)
649                 return -ENOMEM;
650
651         dbus_error_init(&error);
652
653         reply = dbus_connection_send_with_reply_and_block(connection,
654                                                         message, -1, &error);
655         if (reply == NULL) {
656                 if (dbus_error_is_set(&error) == TRUE) {
657                         connman_error("%s", error.message);
658                         dbus_error_free(&error);
659                 } else
660                         connman_error("Failed to enable network");
661                 dbus_message_unref(message);
662                 return -EIO;
663         }
664
665         dbus_message_unref(message);
666
667         dbus_message_unref(reply);
668
669         return 0;
670 }
671
672 static int disable_network(struct supplicant_task *task)
673 {
674         DBusMessage *message, *reply;
675         DBusError error;
676
677         DBG("task %p", task);
678
679         if (task->netpath == NULL)
680                 return -EINVAL;
681
682         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
683                                         SUPPLICANT_INTF ".Network", "disable");
684         if (message == NULL)
685                 return -ENOMEM;
686
687         dbus_error_init(&error);
688
689         reply = dbus_connection_send_with_reply_and_block(connection,
690                                                         message, -1, &error);
691         if (reply == NULL) {
692                 if (dbus_error_is_set(&error) == TRUE) {
693                         connman_error("%s", error.message);
694                         dbus_error_free(&error);
695                 } else
696                         connman_error("Failed to disable network");
697                 dbus_message_unref(message);
698                 return -EIO;
699         }
700
701         dbus_message_unref(message);
702
703         dbus_message_unref(reply);
704
705         return 0;
706 }
707
708 static int set_network(struct supplicant_task *task,
709                                 const unsigned char *network, int len,
710                                 const char *address, const char *security,
711                                                         const char *passphrase)
712 {
713         DBusMessage *message, *reply;
714         DBusMessageIter array, dict;
715         DBusError error;
716
717         DBG("task %p", task);
718
719         if (task->netpath == NULL)
720                 return -EINVAL;
721
722         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
723                                         SUPPLICANT_INTF ".Network", "set");
724         if (message == NULL)
725                 return -ENOMEM;
726
727         dbus_message_iter_init_append(message, &array);
728
729         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
730                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
731                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
732                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
733
734         if (address == NULL) {
735                 dbus_uint32_t scan_ssid = 1;
736                 connman_dbus_dict_append_variant(&dict, "scan_ssid",
737                                                 DBUS_TYPE_UINT32, &scan_ssid);
738         } else
739                 connman_dbus_dict_append_variant(&dict, "bssid",
740                                                 DBUS_TYPE_STRING, &address);
741
742         connman_dbus_dict_append_array(&dict, "ssid",
743                                         DBUS_TYPE_BYTE, &network, len);
744
745         if (g_ascii_strcasecmp(security, "wpa") == 0 ||
746                                 g_ascii_strcasecmp(security, "rsn") == 0) {
747                 const char *key_mgmt = "WPA-PSK";
748                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
749                                                 DBUS_TYPE_STRING, &key_mgmt);
750
751                 if (passphrase && strlen(passphrase) > 0)
752                         connman_dbus_dict_append_variant(&dict, "psk",
753                                                 DBUS_TYPE_STRING, &passphrase);
754         } else if (g_ascii_strcasecmp(security, "wep") == 0) {
755                 const char *key_mgmt = "NONE", *index = "0";
756                 const char *auth_alg = "OPEN SHARED";
757                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
758                                                 DBUS_TYPE_STRING, &key_mgmt);
759
760                 connman_dbus_dict_append_variant(&dict, "auth_alg",
761                                                 DBUS_TYPE_STRING, &auth_alg);
762
763                 if (passphrase) {
764                         int size = strlen(passphrase);
765                         if (size == 10 || size == 26) {
766                                 unsigned char *key = malloc(13);
767                                 char tmp[3];
768                                 int i;
769                                 memset(tmp, 0, sizeof(tmp));
770                                 if (key == NULL)
771                                         size = 0;
772                                 for (i = 0; i < size / 2; i++) {
773                                         memcpy(tmp, passphrase + (i * 2), 2);
774                                         key[i] = (unsigned char) strtol(tmp,
775                                                                 NULL, 16);
776                                 }
777                                 connman_dbus_dict_append_array(&dict,
778                                                 "wep_key0", DBUS_TYPE_BYTE,
779                                                         &key, size / 2);
780                                 free(key);
781                         } else
782                                 connman_dbus_dict_append_variant(&dict,
783                                                 "wep_key0", DBUS_TYPE_STRING,
784                                                                 &passphrase);
785                         connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
786                                                 DBUS_TYPE_STRING, &index);
787                 }
788         } else {
789                 const char *key_mgmt = "NONE";
790                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
791                                                 DBUS_TYPE_STRING, &key_mgmt);
792         }
793
794         dbus_message_iter_close_container(&array, &dict);
795
796         dbus_error_init(&error);
797
798         reply = dbus_connection_send_with_reply_and_block(connection,
799                                                         message, -1, &error);
800         if (reply == NULL) {
801                 if (dbus_error_is_set(&error) == TRUE) {
802                         connman_error("%s", error.message);
803                         dbus_error_free(&error);
804                 } else
805                         connman_error("Failed to set network options");
806                 dbus_message_unref(message);
807                 return -EIO;
808         }
809
810         dbus_message_unref(message);
811
812         dbus_message_unref(reply);
813
814         return 0;
815 }
816
817 static int initiate_scan(struct supplicant_task *task)
818 {
819         DBusMessage *message;
820         DBusPendingCall *call;
821
822         DBG("task %p", task);
823
824         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
825                                         SUPPLICANT_INTF ".Interface", "scan");
826         if (message == NULL)
827                 return -ENOMEM;
828
829         if (dbus_connection_send_with_reply(connection, message,
830                                                 &call, TIMEOUT) == FALSE) {
831                 connman_error("Failed to initiate scan");
832                 dbus_message_unref(message);
833                 return -EIO;
834         }
835
836         dbus_message_unref(message);
837
838         return 0;
839 }
840
841 static struct {
842         char *name;
843         char *value;
844 } special_ssid[] = {
845         { "<hidden>", "hidden"  },
846         { "default",  "linksys" },
847         { "wireless"  },
848         { "linksys"   },
849         { "netgear"   },
850         { "dlink"     },
851         { "2wire"     },
852         { "compaq"    },
853         { "tsunami"   },
854         { "comcomcom", "3com"     },
855         { "3Com",      "3com"     },
856         { "Symbol",    "symbol"   },
857         { "Motorola",  "motorola" },
858         { "Wireless" , "wireless" },
859         { "WLAN",      "wlan"     },
860         { }
861 };
862
863 static char *build_group(const char *addr, const char *name,
864                         const unsigned char *ssid, unsigned int ssid_len,
865                                         const char *mode, const char *security)
866 {
867         GString *str;
868         unsigned int i;
869
870         if (addr == NULL)
871                 return NULL;
872
873         str = g_string_sized_new((ssid_len * 2) + 24);
874         if (str == NULL)
875                 return NULL;
876
877         for (i = 0; special_ssid[i].name; i++) {
878                 if (g_strcmp0(special_ssid[i].name, name) == 0) {
879                         if (special_ssid[i].value == NULL)
880                                 g_string_append_printf(str, "%s_%s",
881                                                                 name, addr);
882                         else
883                                 g_string_append_printf(str, "%s_%s",
884                                                 special_ssid[i].value, addr);
885                         goto done;
886                 }
887         }
888
889         if (ssid_len > 0 && ssid[0] != '\0') {
890                 for (i = 0; i < ssid_len; i++)
891                         g_string_append_printf(str, "%02x", ssid[i]);
892         } else
893                 g_string_append_printf(str, "hidden_%s", addr);
894
895 done:
896         g_string_append_printf(str, "_%s_%s", mode, security);
897
898         return g_string_free(str, FALSE);
899 }
900
901 static void extract_addr(DBusMessageIter *value,
902                                         struct supplicant_result *result)
903 {
904         DBusMessageIter array;
905         struct ether_addr *eth;
906         unsigned char *addr;
907         int addr_len;
908
909         dbus_message_iter_recurse(value, &array);
910         dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
911
912         if (addr_len != 6)
913                 return;
914
915         result->addr = g_try_malloc(addr_len);
916         if (result->addr == NULL)
917                 return;
918
919         memcpy(result->addr, addr, addr_len);
920         result->addr_len = addr_len;
921
922         result->path = g_try_malloc0(13);
923         if (result->path == NULL)
924                 return;
925
926         eth = (void *) addr;
927
928         snprintf(result->path, 13, "%02x%02x%02x%02x%02x%02x",
929                                                 eth->ether_addr_octet[0],
930                                                 eth->ether_addr_octet[1],
931                                                 eth->ether_addr_octet[2],
932                                                 eth->ether_addr_octet[3],
933                                                 eth->ether_addr_octet[4],
934                                                 eth->ether_addr_octet[5]);
935 }
936
937 static void extract_ssid(DBusMessageIter *value,
938                                         struct supplicant_result *result)
939 {
940         DBusMessageIter array;
941         unsigned char *ssid;
942         int ssid_len, i;
943
944         dbus_message_iter_recurse(value, &array);
945         dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
946
947         if (ssid_len < 1)
948                 return;
949
950         result->ssid = g_try_malloc(ssid_len);
951         if (result->ssid == NULL)
952                 return;
953
954         memcpy(result->ssid, ssid, ssid_len);
955         result->ssid_len = ssid_len;
956
957         result->name = g_try_malloc0(ssid_len + 1);
958         if (result->name == NULL)
959                 return;
960
961         for (i = 0; i < ssid_len; i++) {
962                 if (g_ascii_isprint(ssid[i]))
963                         result->name[i] = ssid[i];
964                 else
965                         result->name[i] = ' ';
966         }
967 }
968
969 static void extract_wpaie(DBusMessageIter *value,
970                                         struct supplicant_result *result)
971 {
972         DBusMessageIter array;
973         unsigned char *ie;
974         int ie_len;
975
976         dbus_message_iter_recurse(value, &array);
977         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
978
979         if (ie_len > 0)
980                 result->has_wpa = TRUE;
981 }
982
983 static void extract_rsnie(DBusMessageIter *value,
984                                         struct supplicant_result *result)
985 {
986         DBusMessageIter array;
987         unsigned char *ie;
988         int ie_len;
989
990         dbus_message_iter_recurse(value, &array);
991         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
992
993         if (ie_len > 0)
994                 result->has_rsn = TRUE;
995 }
996
997 static void extract_wpsie(DBusMessageIter *value,
998                                         struct supplicant_result *result)
999 {
1000         DBusMessageIter array;
1001         unsigned char *ie;
1002         int ie_len;
1003
1004         dbus_message_iter_recurse(value, &array);
1005         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1006
1007         if (ie_len > 0)
1008                 result->has_wps = TRUE;
1009 }
1010
1011 static void extract_capabilites(DBusMessageIter *value,
1012                                         struct supplicant_result *result)
1013 {
1014         dbus_message_iter_get_basic(value, &result->capabilities);
1015
1016         if (result->capabilities & IEEE80211_CAP_ESS)
1017                 result->adhoc = FALSE;
1018         else if (result->capabilities & IEEE80211_CAP_IBSS)
1019                 result->adhoc = TRUE;
1020
1021         if (result->capabilities & IEEE80211_CAP_PRIVACY)
1022                 result->has_wep = TRUE;
1023 }
1024
1025 static unsigned char calculate_strength(struct supplicant_task *task,
1026                                         struct supplicant_result *result)
1027 {
1028         if (task->range->max_qual.qual == 0) {
1029                 unsigned char strength;
1030
1031                 if (result->level > 0)
1032                         strength = 100 - result->level;
1033                 else
1034                         strength = 120 + result->level;
1035
1036                 if (strength > 100)
1037                         strength = 100;
1038
1039                 return strength;
1040         }
1041
1042         return (result->quality * 100) / task->range->max_qual.qual;
1043 }
1044
1045 static unsigned short calculate_channel(struct supplicant_result *result)
1046 {
1047         if (result->frequency < 0)
1048                 return 0;
1049
1050         return (result->frequency - 2407) / 5;
1051 }
1052
1053 static void get_properties(struct supplicant_task *task);
1054
1055 static void properties_reply(DBusPendingCall *call, void *user_data)
1056 {
1057         struct supplicant_task *task = user_data;
1058         struct supplicant_result result;
1059         struct connman_network *network;
1060         DBusMessage *reply;
1061         DBusMessageIter array, dict;
1062         unsigned char strength;
1063         unsigned short channel, frequency;
1064         const char *mode, *security;
1065         char *group;
1066
1067         DBG("task %p", task);
1068
1069         reply = dbus_pending_call_steal_reply(call);
1070         if (reply == NULL) {
1071                 get_properties(task);
1072                 return;
1073         }
1074
1075         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1076                 dbus_message_unref(reply);
1077                 get_properties(task);
1078                 return;
1079         }
1080
1081         memset(&result, 0, sizeof(result));
1082         result.frequency = -1;
1083         result.quality = -1;
1084         result.level = 0;
1085         result.noise = 0;
1086
1087         dbus_message_iter_init(reply, &array);
1088
1089         dbus_message_iter_recurse(&array, &dict);
1090
1091         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1092                 DBusMessageIter entry, value;
1093                 const char *key;
1094
1095                 dbus_message_iter_recurse(&dict, &entry);
1096                 dbus_message_iter_get_basic(&entry, &key);
1097
1098                 dbus_message_iter_next(&entry);
1099
1100                 dbus_message_iter_recurse(&entry, &value);
1101
1102                 //type = dbus_message_iter_get_arg_type(&value);
1103                 //dbus_message_iter_get_basic(&value, &val);
1104
1105                 /* 
1106                  * bssid        : a (97)
1107                  * ssid         : a (97)
1108                  * wpaie        : a (97)
1109                  * rsnie        : a (97)
1110                  * wpsie        : a (97)
1111                  * frequency    : i (105)
1112                  * capabilities : q (113)
1113                  * quality      : i (105)
1114                  * noise        : i (105)
1115                  * level        : i (105)
1116                  * maxrate      : i (105)
1117                  */
1118
1119                 if (g_str_equal(key, "bssid") == TRUE)
1120                         extract_addr(&value, &result);
1121                 else if (g_str_equal(key, "ssid") == TRUE)
1122                         extract_ssid(&value, &result);
1123                 else if (g_str_equal(key, "wpaie") == TRUE)
1124                         extract_wpaie(&value, &result);
1125                 else if (g_str_equal(key, "rsnie") == TRUE)
1126                         extract_rsnie(&value, &result);
1127                 else if (g_str_equal(key, "wpsie") == TRUE)
1128                         extract_wpsie(&value, &result);
1129                 else if (g_str_equal(key, "capabilities") == TRUE)
1130                         extract_capabilites(&value, &result);
1131                 else if (g_str_equal(key, "frequency") == TRUE)
1132                         dbus_message_iter_get_basic(&value, &result.frequency);
1133                 else if (g_str_equal(key, "quality") == TRUE)
1134                         dbus_message_iter_get_basic(&value, &result.quality);
1135                 else if (g_str_equal(key, "noise") == TRUE)
1136                         dbus_message_iter_get_basic(&value, &result.noise);
1137                 else if (g_str_equal(key, "level") == TRUE)
1138                         dbus_message_iter_get_basic(&value, &result.level);
1139                 else if (g_str_equal(key, "maxrate") == TRUE)
1140                         dbus_message_iter_get_basic(&value, &result.maxrate);
1141
1142                 dbus_message_iter_next(&dict);
1143         }
1144
1145         if (result.path == NULL)
1146                 goto done;
1147
1148         if (result.path[0] == '\0')
1149                 goto done;
1150
1151         if (result.frequency > 0 && result.frequency < 14)
1152                 result.frequency = 2407 + (5 * result.frequency);
1153         else if (result.frequency == 14)
1154                 result.frequency = 2484;
1155
1156         strength = calculate_strength(task, &result);
1157         channel  = calculate_channel(&result);
1158
1159         frequency = (result.frequency < 0) ? 0 : result.frequency;
1160
1161         if (result.has_rsn == TRUE)
1162                 security = "rsn";
1163         else if (result.has_wpa == TRUE)
1164                 security = "wpa";
1165         else if (result.has_wep == TRUE)
1166                 security = "wep";
1167         else
1168                 security = "none";
1169
1170         mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1171
1172         group = build_group(result.path, result.name,
1173                                         result.ssid, result.ssid_len,
1174                                                         mode, security);
1175
1176         network = connman_device_get_network(task->device, result.path);
1177         if (network == NULL) {
1178                 int index;
1179
1180                 network = connman_network_create(result.path,
1181                                                 CONNMAN_NETWORK_TYPE_WIFI);
1182                 if (network == NULL)
1183                         goto done;
1184
1185                 index = connman_device_get_index(task->device);
1186                 connman_network_set_index(network, index);
1187
1188                 connman_network_set_protocol(network,
1189                                                 CONNMAN_NETWORK_PROTOCOL_IP);
1190
1191                 connman_network_set_address(network, result.addr,
1192                                                         result.addr_len);
1193
1194                 if (connman_device_add_network(task->device, network) < 0) {
1195                         connman_network_unref(network);
1196                         goto done;
1197                 }
1198         }
1199
1200         if (result.name != NULL && result.name[0] != '\0')
1201                 connman_network_set_name(network, result.name);
1202
1203         connman_network_set_blob(network, "WiFi.SSID",
1204                                                 result.ssid, result.ssid_len);
1205
1206         connman_network_set_string(network, "WiFi.Mode", mode);
1207
1208         DBG("%s (%s %s) strength %d (%s)",
1209                                 result.name, mode, security, strength,
1210                                 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1211
1212         connman_network_set_available(network, TRUE);
1213         connman_network_set_strength(network, strength);
1214
1215         connman_network_set_uint16(network, "Frequency", frequency);
1216         connman_network_set_uint16(network, "WiFi.Channel", channel);
1217         connman_network_set_string(network, "WiFi.Security", security);
1218
1219         connman_network_set_group(network, group);
1220
1221         g_free(group);
1222
1223 done:
1224         g_free(result.path);
1225         g_free(result.addr);
1226         g_free(result.name);
1227         g_free(result.ssid);
1228
1229         dbus_message_unref(reply);
1230
1231         get_properties(task);
1232 }
1233
1234 static void get_properties(struct supplicant_task *task)
1235 {
1236         DBusMessage *message;
1237         DBusPendingCall *call;
1238         char *path;
1239
1240         path = g_slist_nth_data(task->scan_results, 0);
1241         if (path == NULL)
1242                 goto noscan;
1243
1244         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1245                                                 SUPPLICANT_INTF ".BSSID",
1246                                                                 "properties");
1247
1248         task->scan_results = g_slist_remove(task->scan_results, path);
1249         g_free(path);
1250
1251         if (message == NULL)
1252                 goto noscan;
1253
1254         if (dbus_connection_send_with_reply(connection, message,
1255                                                 &call, TIMEOUT) == FALSE) {
1256                 connman_error("Failed to get network properties");
1257                 dbus_message_unref(message);
1258                 goto noscan;
1259         }
1260
1261         if (call == NULL) {
1262                 connman_error("D-Bus connection not available");
1263                 dbus_message_unref(message);
1264                 goto noscan;
1265         }
1266
1267         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1268
1269         dbus_message_unref(message);
1270
1271         return;
1272
1273 noscan:
1274         if (task->noscan == FALSE)
1275                 connman_device_set_scanning(task->device, FALSE);
1276 }
1277
1278 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1279 {
1280         struct supplicant_task *task = user_data;
1281         DBusMessage *reply;
1282         DBusError error;
1283         char **results;
1284         int i, num_results;
1285
1286         DBG("task %p", task);
1287
1288         reply = dbus_pending_call_steal_reply(call);
1289         if (reply == NULL)
1290                 goto noscan;
1291
1292         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1293                 goto done;
1294
1295         dbus_error_init(&error);
1296
1297         if (dbus_message_get_args(reply, &error,
1298                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1299                                                 &results, &num_results,
1300                                                 DBUS_TYPE_INVALID) == FALSE) {
1301                 if (dbus_error_is_set(&error) == TRUE) {
1302                         connman_error("%s", error.message);
1303                         dbus_error_free(&error);
1304                 } else
1305                         connman_error("Wrong arguments for scan result");
1306                 goto done;
1307         }
1308
1309         if (num_results == 0)
1310                 goto done;
1311
1312         for (i = 0; i < num_results; i++) {
1313                 char *path = g_strdup(results[i]);
1314                 if (path == NULL)
1315                         continue;
1316
1317                 task->scan_results = g_slist_append(task->scan_results, path);
1318         }
1319
1320         g_strfreev(results);
1321
1322         dbus_message_unref(reply);
1323
1324         get_properties(task);
1325
1326         return;
1327
1328 done:
1329         dbus_message_unref(reply);
1330
1331 noscan:
1332         if (task->noscan == FALSE)
1333                 connman_device_set_scanning(task->device, FALSE);
1334 }
1335
1336 static void scan_results_available(struct supplicant_task *task)
1337 {
1338         DBusMessage *message;
1339         DBusPendingCall *call;
1340
1341         DBG("task %p", task);
1342
1343         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1344                                                 SUPPLICANT_INTF ".Interface",
1345                                                         "scanResults");
1346         if (message == NULL)
1347                 return;
1348
1349         if (dbus_connection_send_with_reply(connection, message,
1350                                                 &call, TIMEOUT) == FALSE) {
1351                 connman_error("Failed to request scan result");
1352                 goto done;
1353         }
1354
1355         if (task->noscan == FALSE)
1356                 connman_device_set_scanning(task->device, TRUE);
1357
1358         if (call == NULL) {
1359                 connman_error("D-Bus connection not available");
1360                 goto done;
1361         }
1362
1363         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1364
1365 done:
1366         dbus_message_unref(message);
1367 }
1368
1369 static enum supplicant_state string2state(const char *state)
1370 {
1371         if (g_str_equal(state, "INACTIVE") == TRUE)
1372                 return WPA_INACTIVE;
1373         else if (g_str_equal(state, "SCANNING") == TRUE)
1374                 return WPA_SCANNING;
1375         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1376                 return WPA_ASSOCIATING;
1377         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1378                 return WPA_ASSOCIATED;
1379         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1380                 return WPA_GROUP_HANDSHAKE;
1381         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1382                 return WPA_4WAY_HANDSHAKE;
1383         else if (g_str_equal(state, "COMPLETED") == TRUE)
1384                 return WPA_COMPLETED;
1385         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1386                 return WPA_DISCONNECTED;
1387         else
1388                 return WPA_INVALID;
1389 }
1390
1391 static int task_connect(struct supplicant_task *task)
1392 {
1393         const char *address, *security, *passphrase;
1394         const void *ssid;
1395         unsigned int ssid_len;
1396
1397         address = connman_network_get_string(task->network, "Address");
1398         security = connman_network_get_string(task->network, "WiFi.Security");
1399         passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1400
1401         ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1402
1403         DBG("address %s security %s passphrase %s",
1404                                         address, security, passphrase);
1405
1406         if (security == NULL && passphrase == NULL)
1407                 return -EINVAL;
1408
1409         if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1410                 return -EINVAL;
1411
1412         add_network(task);
1413
1414         select_network(task);
1415         disable_network(task);
1416
1417         set_network(task, ssid, ssid_len, address, security, passphrase);
1418
1419         enable_network(task);
1420
1421         return 0;
1422 }
1423
1424 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1425 {
1426         DBusError error;
1427         const char *newstate, *oldstate;
1428         enum supplicant_state state;
1429
1430         dbus_error_init(&error);
1431
1432         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1433                                                 DBUS_TYPE_STRING, &oldstate,
1434                                                 DBUS_TYPE_INVALID) == FALSE) {
1435                 if (dbus_error_is_set(&error) == TRUE) {
1436                         connman_error("%s", error.message);
1437                         dbus_error_free(&error);
1438                 } else
1439                         connman_error("Wrong arguments for state change");
1440                 return;
1441         }
1442
1443         DBG("state %s ==> %s", oldstate, newstate);
1444
1445         state = string2state(newstate);
1446         if (state == WPA_INVALID)
1447                 return;
1448
1449         task->state = state;
1450
1451         switch (task->state) {
1452         case WPA_SCANNING:
1453                 task->noscan = TRUE;
1454                 connman_device_set_scanning(task->device, TRUE);
1455                 break;
1456         case WPA_ASSOCIATING:
1457         case WPA_ASSOCIATED:
1458         case WPA_4WAY_HANDSHAKE:
1459         case WPA_GROUP_HANDSHAKE:
1460                 task->noscan = TRUE;
1461                 break;
1462         case WPA_COMPLETED:
1463         case WPA_DISCONNECTED:
1464                 task->noscan = FALSE;
1465                 break;
1466         case WPA_INACTIVE:
1467                 task->noscan = FALSE;
1468                 connman_device_set_scanning(task->device, FALSE);
1469                 break;
1470         case WPA_INVALID:
1471                 break;
1472         }
1473
1474         if (task->network == NULL)
1475                 return;
1476
1477         switch (task->state) {
1478         case WPA_COMPLETED:
1479                 /* carrier on */
1480                 connman_network_set_connected(task->network, TRUE);
1481                 connman_device_set_scanning(task->device, FALSE);
1482                 break;
1483         case WPA_DISCONNECTED:
1484                 /* carrier off */
1485                 connman_network_set_connected(task->network, FALSE);
1486                 connman_device_set_scanning(task->device, FALSE);
1487                 break;
1488         case WPA_ASSOCIATING:
1489                 connman_network_set_associating(task->network, TRUE);
1490                 break;
1491         default:
1492                 connman_network_set_associating(task->network, FALSE);
1493                 break;
1494         }
1495 }
1496
1497 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1498                                                 DBusMessage *msg, void *data)
1499 {
1500         struct supplicant_task *task;
1501         const char *member, *path;
1502
1503         if (dbus_message_has_interface(msg,
1504                                 SUPPLICANT_INTF ".Interface") == FALSE)
1505                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1506
1507         member = dbus_message_get_member(msg);
1508         if (member == NULL)
1509                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1510
1511         path = dbus_message_get_path(msg);
1512         if (path == NULL)
1513                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1514
1515         task = find_task_by_path(path);
1516         if (task == NULL)
1517                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1518
1519         DBG("task %p member %s", task, member);
1520
1521         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1522                 scan_results_available(task);
1523         else if (g_str_equal(member, "StateChange") == TRUE)
1524                 state_change(task, msg);
1525
1526         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1527 }
1528
1529 static int supplicant_get_range(struct supplicant_task *task)
1530 {
1531         struct iwreq wrq;
1532         int fd, err;
1533
1534         fd = socket(PF_INET, SOCK_DGRAM, 0);
1535         if (fd < 0)
1536                 return -1;
1537
1538         memset(&wrq, 0, sizeof(struct iwreq));
1539         strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
1540         wrq.u.data.pointer = task->range;
1541         wrq.u.data.length = sizeof(struct iw_range);
1542
1543         err = ioctl(fd, SIOCGIWRANGE, &wrq);
1544
1545         close(fd);
1546
1547         return err;
1548 }
1549
1550 int supplicant_start(struct connman_device *device)
1551 {
1552         struct supplicant_task *task;
1553         int err;
1554
1555         DBG("device %p", device);
1556
1557         task = g_try_new0(struct supplicant_task, 1);
1558         if (task == NULL)
1559                 return -ENOMEM;
1560
1561         task->ifindex = connman_device_get_index(device);
1562         task->ifname = connman_inet_ifname(task->ifindex);
1563
1564         if (task->ifname == NULL) {
1565                 err = -ENOMEM;
1566                 goto failed;
1567         }
1568
1569         task->range = g_try_malloc0(sizeof(struct iw_range));
1570         if (task->range == NULL) {
1571                 err = -ENOMEM;
1572                 goto failed;
1573         }
1574
1575         err = supplicant_get_range(task);
1576         if (err < 0)
1577                 goto failed;
1578
1579         task->device = connman_device_ref(device);
1580
1581         task->created = FALSE;
1582         task->noscan = FALSE;
1583         task->state = WPA_INVALID;
1584
1585         task_list = g_slist_append(task_list, task);
1586
1587         return create_interface(task);
1588
1589 failed:
1590         g_free(task->range);
1591         g_free(task->ifname);
1592         g_free(task);
1593
1594         return err;
1595 }
1596
1597 int supplicant_stop(struct connman_device *device)
1598 {
1599         int index = connman_device_get_index(device);
1600         struct supplicant_task *task;
1601
1602         DBG("device %p", device);
1603
1604         task = find_task_by_index(index);
1605         if (task == NULL)
1606                 return -ENODEV;
1607
1608         g_free(task->range);
1609
1610         task_list = g_slist_remove(task_list, task);
1611
1612         disable_network(task);
1613
1614         remove_network(task);
1615
1616         return remove_interface(task);
1617 }
1618
1619 int supplicant_scan(struct connman_device *device)
1620 {
1621         int index = connman_device_get_index(device);
1622         struct supplicant_task *task;
1623         int err;
1624
1625         DBG("device %p", device);
1626
1627         task = find_task_by_index(index);
1628         if (task == NULL)
1629                 return -ENODEV;
1630
1631         switch (task->state) {
1632         case WPA_SCANNING:
1633                 return -EALREADY;
1634         case WPA_ASSOCIATING:
1635         case WPA_ASSOCIATED:
1636         case WPA_4WAY_HANDSHAKE:
1637         case WPA_GROUP_HANDSHAKE:
1638                 return -EBUSY;
1639         default:
1640                 break;
1641         }
1642
1643         err = initiate_scan(task);
1644
1645         return 0;
1646 }
1647
1648 int supplicant_connect(struct connman_network *network)
1649 {
1650         struct supplicant_task *task;
1651         int index;
1652
1653         DBG("network %p", network);
1654
1655         index = connman_network_get_index(network);
1656
1657         task = find_task_by_index(index);
1658         if (task == NULL)
1659                 return -ENODEV;
1660
1661         task->network = connman_network_ref(network);
1662
1663         return task_connect(task);
1664 }
1665
1666 int supplicant_disconnect(struct connman_network *network)
1667 {
1668         struct supplicant_task *task;
1669         int index;
1670
1671         DBG("network %p", network);
1672
1673         index = connman_network_get_index(network);
1674
1675         task = find_task_by_index(index);
1676         if (task == NULL)
1677                 return -ENODEV;
1678
1679         disable_network(task);
1680
1681         remove_network(task);
1682
1683         connman_network_set_connected(task->network, FALSE);
1684
1685         connman_network_unref(task->network);
1686
1687         return 0;
1688 }
1689
1690 static void supplicant_activate(DBusConnection *conn)
1691 {
1692         DBusMessage *message;
1693
1694         DBG("conn %p", conn);
1695
1696         message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1697                                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1698         if (message == NULL)
1699                 return;
1700
1701         dbus_message_set_no_reply(message, TRUE);
1702
1703         dbus_connection_send(conn, message, NULL);
1704
1705         dbus_message_unref(message);
1706 }
1707
1708 static GSList *driver_list = NULL;
1709
1710 static void supplicant_probe(DBusConnection *conn, void *user_data)
1711 {
1712         GSList *list;
1713
1714         DBG("conn %p", conn);
1715
1716         for (list = driver_list; list; list = list->next) {
1717                 struct supplicant_driver *driver = list->data;
1718
1719                 DBG("driver %p name %s", driver, driver->name);
1720
1721                 if (driver->probe)
1722                         driver->probe();
1723         }
1724 }
1725
1726 static void supplicant_remove(DBusConnection *conn, void *user_data)
1727 {
1728         GSList *list;
1729
1730         DBG("conn %p", conn);
1731
1732         for (list = driver_list; list; list = list->next) {
1733                 struct supplicant_driver *driver = list->data;
1734
1735                 DBG("driver %p name %s", driver, driver->name);
1736
1737                 if (driver->remove)
1738                         driver->remove();
1739         }
1740 }
1741
1742 static const char *supplicant_rule = "type=signal,"
1743                                 "interface=" SUPPLICANT_INTF ".Interface";
1744 static guint watch;
1745
1746 static int supplicant_create(void)
1747 {
1748         if (g_slist_length(driver_list) > 0)
1749                 return 0;
1750
1751         connection = connman_dbus_get_connection();
1752         if (connection == NULL)
1753                 return -EIO;
1754
1755         DBG("connection %p", connection);
1756
1757         if (dbus_connection_add_filter(connection,
1758                                 supplicant_filter, NULL, NULL) == FALSE) {
1759                 connection = connman_dbus_get_connection();
1760                 return -EIO;
1761         }
1762
1763         dbus_bus_add_match(connection, supplicant_rule, NULL);
1764         dbus_connection_flush(connection);
1765
1766         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1767                         supplicant_probe, supplicant_remove, NULL, NULL);
1768
1769         return 0;
1770 }
1771
1772 static void supplicant_destroy(void)
1773 {
1774         if (g_slist_length(driver_list) > 0)
1775                 return;
1776
1777         DBG("connection %p", connection);
1778
1779         if (watch > 0)
1780                 g_dbus_remove_watch(connection, watch);
1781
1782         dbus_bus_remove_match(connection, supplicant_rule, NULL);
1783         dbus_connection_flush(connection);
1784
1785         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1786
1787         dbus_connection_unref(connection);
1788         connection = NULL;
1789 }
1790
1791 int supplicant_register(struct supplicant_driver *driver)
1792 {
1793         int err;
1794
1795         DBG("driver %p name %s", driver, driver->name);
1796
1797         err = supplicant_create();
1798         if (err < 0)
1799                 return err;
1800
1801         driver_list = g_slist_append(driver_list, driver);
1802
1803         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1804                 supplicant_probe(connection, NULL);
1805         else
1806                 supplicant_activate(connection);
1807
1808         return 0;
1809 }
1810
1811 void supplicant_unregister(struct supplicant_driver *driver)
1812 {
1813         DBG("driver %p name %s", driver, driver->name);
1814
1815         supplicant_remove(connection, NULL);
1816
1817         driver_list = g_slist_remove(driver_list, driver);
1818
1819         supplicant_destroy();
1820 }