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