Add support for storing SSID details of hidden services
[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         unsigned int ssid_len;
1069
1070         DBG("task %p", task);
1071
1072         reply = dbus_pending_call_steal_reply(call);
1073         if (reply == NULL) {
1074                 get_properties(task);
1075                 return;
1076         }
1077
1078         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
1079                 dbus_message_unref(reply);
1080                 get_properties(task);
1081                 return;
1082         }
1083
1084         memset(&result, 0, sizeof(result));
1085         result.frequency = -1;
1086         result.quality = -1;
1087         result.level = 0;
1088         result.noise = 0;
1089
1090         dbus_message_iter_init(reply, &array);
1091
1092         dbus_message_iter_recurse(&array, &dict);
1093
1094         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
1095                 DBusMessageIter entry, value;
1096                 const char *key;
1097
1098                 dbus_message_iter_recurse(&dict, &entry);
1099                 dbus_message_iter_get_basic(&entry, &key);
1100
1101                 dbus_message_iter_next(&entry);
1102
1103                 dbus_message_iter_recurse(&entry, &value);
1104
1105                 //type = dbus_message_iter_get_arg_type(&value);
1106                 //dbus_message_iter_get_basic(&value, &val);
1107
1108                 /* 
1109                  * bssid        : a (97)
1110                  * ssid         : a (97)
1111                  * wpaie        : a (97)
1112                  * rsnie        : a (97)
1113                  * wpsie        : a (97)
1114                  * frequency    : i (105)
1115                  * capabilities : q (113)
1116                  * quality      : i (105)
1117                  * noise        : i (105)
1118                  * level        : i (105)
1119                  * maxrate      : i (105)
1120                  */
1121
1122                 if (g_str_equal(key, "bssid") == TRUE)
1123                         extract_addr(&value, &result);
1124                 else if (g_str_equal(key, "ssid") == TRUE)
1125                         extract_ssid(&value, &result);
1126                 else if (g_str_equal(key, "wpaie") == TRUE)
1127                         extract_wpaie(&value, &result);
1128                 else if (g_str_equal(key, "rsnie") == TRUE)
1129                         extract_rsnie(&value, &result);
1130                 else if (g_str_equal(key, "wpsie") == TRUE)
1131                         extract_wpsie(&value, &result);
1132                 else if (g_str_equal(key, "capabilities") == TRUE)
1133                         extract_capabilites(&value, &result);
1134                 else if (g_str_equal(key, "frequency") == TRUE)
1135                         dbus_message_iter_get_basic(&value, &result.frequency);
1136                 else if (g_str_equal(key, "quality") == TRUE)
1137                         dbus_message_iter_get_basic(&value, &result.quality);
1138                 else if (g_str_equal(key, "noise") == TRUE)
1139                         dbus_message_iter_get_basic(&value, &result.noise);
1140                 else if (g_str_equal(key, "level") == TRUE)
1141                         dbus_message_iter_get_basic(&value, &result.level);
1142                 else if (g_str_equal(key, "maxrate") == TRUE)
1143                         dbus_message_iter_get_basic(&value, &result.maxrate);
1144
1145                 dbus_message_iter_next(&dict);
1146         }
1147
1148         if (result.path == NULL)
1149                 goto done;
1150
1151         if (result.path[0] == '\0')
1152                 goto done;
1153
1154         if (result.frequency > 0 && result.frequency < 14)
1155                 result.frequency = 2407 + (5 * result.frequency);
1156         else if (result.frequency == 14)
1157                 result.frequency = 2484;
1158
1159         strength = calculate_strength(task, &result);
1160         channel  = calculate_channel(&result);
1161
1162         frequency = (result.frequency < 0) ? 0 : result.frequency;
1163
1164         if (result.has_rsn == TRUE)
1165                 security = "rsn";
1166         else if (result.has_wpa == TRUE)
1167                 security = "wpa";
1168         else if (result.has_wep == TRUE)
1169                 security = "wep";
1170         else
1171                 security = "none";
1172
1173         mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
1174
1175         group = build_group(result.path, result.name,
1176                                         result.ssid, result.ssid_len,
1177                                                         mode, security);
1178
1179         network = connman_device_get_network(task->device, result.path);
1180         if (network == NULL) {
1181                 int index;
1182
1183                 network = connman_network_create(result.path,
1184                                                 CONNMAN_NETWORK_TYPE_WIFI);
1185                 if (network == NULL)
1186                         goto done;
1187
1188                 index = connman_device_get_index(task->device);
1189                 connman_network_set_index(network, index);
1190
1191                 connman_network_set_protocol(network,
1192                                                 CONNMAN_NETWORK_PROTOCOL_IP);
1193
1194                 connman_network_set_address(network, result.addr,
1195                                                         result.addr_len);
1196
1197                 if (connman_device_add_network(task->device, network) < 0) {
1198                         connman_network_unref(network);
1199                         goto done;
1200                 }
1201         }
1202
1203         if (result.name != NULL && result.name[0] != '\0')
1204                 connman_network_set_name(network, result.name);
1205
1206         if (connman_network_get_blob(network, "WiFi.SSID", &ssid_len) == NULL) {
1207                 connman_network_set_blob(network, "WiFi.SSID",
1208                                          result.ssid, result.ssid_len);
1209         }
1210
1211         connman_network_set_string(network, "WiFi.Mode", mode);
1212
1213         DBG("%s (%s %s) strength %d (%s)",
1214                                 result.name, mode, security, strength,
1215                                 (result.has_wps == TRUE) ? "WPS" : "no WPS");
1216
1217         connman_network_set_available(network, TRUE);
1218         connman_network_set_strength(network, strength);
1219
1220         connman_network_set_uint16(network, "Frequency", frequency);
1221         connman_network_set_uint16(network, "WiFi.Channel", channel);
1222         connman_network_set_string(network, "WiFi.Security", security);
1223
1224         connman_network_set_group(network, group);
1225
1226         g_free(group);
1227
1228 done:
1229         g_free(result.path);
1230         g_free(result.addr);
1231         g_free(result.name);
1232         g_free(result.ssid);
1233
1234         dbus_message_unref(reply);
1235
1236         get_properties(task);
1237 }
1238
1239 static void get_properties(struct supplicant_task *task)
1240 {
1241         DBusMessage *message;
1242         DBusPendingCall *call;
1243         char *path;
1244
1245         path = g_slist_nth_data(task->scan_results, 0);
1246         if (path == NULL)
1247                 goto noscan;
1248
1249         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1250                                                 SUPPLICANT_INTF ".BSSID",
1251                                                                 "properties");
1252
1253         task->scan_results = g_slist_remove(task->scan_results, path);
1254         g_free(path);
1255
1256         if (message == NULL)
1257                 goto noscan;
1258
1259         if (dbus_connection_send_with_reply(connection, message,
1260                                                 &call, TIMEOUT) == FALSE) {
1261                 connman_error("Failed to get network properties");
1262                 dbus_message_unref(message);
1263                 goto noscan;
1264         }
1265
1266         if (call == NULL) {
1267                 connman_error("D-Bus connection not available");
1268                 dbus_message_unref(message);
1269                 goto noscan;
1270         }
1271
1272         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1273
1274         dbus_message_unref(message);
1275
1276         return;
1277
1278 noscan:
1279         if (task->noscan == FALSE)
1280                 connman_device_set_scanning(task->device, FALSE);
1281 }
1282
1283 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1284 {
1285         struct supplicant_task *task = user_data;
1286         DBusMessage *reply;
1287         DBusError error;
1288         char **results;
1289         int i, num_results;
1290
1291         DBG("task %p", task);
1292
1293         reply = dbus_pending_call_steal_reply(call);
1294         if (reply == NULL)
1295                 goto noscan;
1296
1297         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1298                 goto done;
1299
1300         dbus_error_init(&error);
1301
1302         if (dbus_message_get_args(reply, &error,
1303                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1304                                                 &results, &num_results,
1305                                                 DBUS_TYPE_INVALID) == FALSE) {
1306                 if (dbus_error_is_set(&error) == TRUE) {
1307                         connman_error("%s", error.message);
1308                         dbus_error_free(&error);
1309                 } else
1310                         connman_error("Wrong arguments for scan result");
1311                 goto done;
1312         }
1313
1314         if (num_results == 0)
1315                 goto done;
1316
1317         for (i = 0; i < num_results; i++) {
1318                 char *path = g_strdup(results[i]);
1319                 if (path == NULL)
1320                         continue;
1321
1322                 task->scan_results = g_slist_append(task->scan_results, path);
1323         }
1324
1325         g_strfreev(results);
1326
1327         dbus_message_unref(reply);
1328
1329         get_properties(task);
1330
1331         return;
1332
1333 done:
1334         dbus_message_unref(reply);
1335
1336 noscan:
1337         if (task->noscan == FALSE)
1338                 connman_device_set_scanning(task->device, FALSE);
1339 }
1340
1341 static void scan_results_available(struct supplicant_task *task)
1342 {
1343         DBusMessage *message;
1344         DBusPendingCall *call;
1345
1346         DBG("task %p", task);
1347
1348         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1349                                                 SUPPLICANT_INTF ".Interface",
1350                                                         "scanResults");
1351         if (message == NULL)
1352                 return;
1353
1354         if (dbus_connection_send_with_reply(connection, message,
1355                                                 &call, TIMEOUT) == FALSE) {
1356                 connman_error("Failed to request scan result");
1357                 goto done;
1358         }
1359
1360         if (task->noscan == FALSE)
1361                 connman_device_set_scanning(task->device, TRUE);
1362
1363         if (call == NULL) {
1364                 connman_error("D-Bus connection not available");
1365                 goto done;
1366         }
1367
1368         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1369
1370 done:
1371         dbus_message_unref(message);
1372 }
1373
1374 static enum supplicant_state string2state(const char *state)
1375 {
1376         if (g_str_equal(state, "INACTIVE") == TRUE)
1377                 return WPA_INACTIVE;
1378         else if (g_str_equal(state, "SCANNING") == TRUE)
1379                 return WPA_SCANNING;
1380         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1381                 return WPA_ASSOCIATING;
1382         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1383                 return WPA_ASSOCIATED;
1384         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1385                 return WPA_GROUP_HANDSHAKE;
1386         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1387                 return WPA_4WAY_HANDSHAKE;
1388         else if (g_str_equal(state, "COMPLETED") == TRUE)
1389                 return WPA_COMPLETED;
1390         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1391                 return WPA_DISCONNECTED;
1392         else
1393                 return WPA_INVALID;
1394 }
1395
1396 static int task_connect(struct supplicant_task *task)
1397 {
1398         const char *address, *security, *passphrase;
1399         const void *ssid;
1400         unsigned int ssid_len;
1401
1402         address = connman_network_get_string(task->network, "Address");
1403         security = connman_network_get_string(task->network, "WiFi.Security");
1404         passphrase = connman_network_get_string(task->network, "WiFi.Passphrase");
1405
1406         ssid = connman_network_get_blob(task->network, "WiFi.SSID", &ssid_len);
1407
1408         DBG("address %s security %s passphrase %s",
1409                                         address, security, passphrase);
1410
1411         if (security == NULL && passphrase == NULL)
1412                 return -EINVAL;
1413
1414         if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1415                 return -EINVAL;
1416
1417         add_network(task);
1418
1419         select_network(task);
1420         disable_network(task);
1421
1422         set_network(task, ssid, ssid_len, address, security, passphrase);
1423
1424         enable_network(task);
1425
1426         return 0;
1427 }
1428
1429 static char *get_bssid(struct connman_device *device)
1430 {
1431         char *bssid;
1432         unsigned char ioctl_bssid[ETH_ALEN];
1433         int fd, ret;
1434         struct iwreq wrq;
1435
1436         if (connman_device_get_type(device) != CONNMAN_DEVICE_TYPE_WIFI)
1437                 return NULL;
1438
1439         fd = socket(PF_INET, SOCK_DGRAM, 0);
1440         if (fd < 0)
1441                 return NULL;
1442
1443         memset(&wrq, 0, sizeof(wrq));
1444         strncpy(wrq.ifr_name, connman_device_get_interface(device), IFNAMSIZ);
1445
1446         ret = ioctl(fd, SIOCGIWAP, &wrq);
1447         close(fd);
1448         if (ret != 0)
1449                 return NULL;
1450
1451         memcpy(ioctl_bssid, wrq.u.ap_addr.sa_data, ETH_ALEN);
1452
1453         bssid = g_try_malloc0(13);
1454         if (bssid == NULL)
1455                 return NULL;
1456
1457         snprintf(bssid, 13, "%02x%02x%02x%02x%02x%02x",
1458                  ioctl_bssid[0], ioctl_bssid[1],
1459                  ioctl_bssid[2], ioctl_bssid[3],
1460                  ioctl_bssid[4], ioctl_bssid[5]);
1461
1462         return bssid;
1463 }
1464
1465
1466 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1467 {
1468         DBusError error;
1469         const char *newstate, *oldstate;
1470         enum supplicant_state state;
1471
1472         dbus_error_init(&error);
1473
1474         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1475                                                 DBUS_TYPE_STRING, &oldstate,
1476                                                 DBUS_TYPE_INVALID) == FALSE) {
1477                 if (dbus_error_is_set(&error) == TRUE) {
1478                         connman_error("%s", error.message);
1479                         dbus_error_free(&error);
1480                 } else
1481                         connman_error("Wrong arguments for state change");
1482                 return;
1483         }
1484
1485         DBG("state %s ==> %s", oldstate, newstate);
1486
1487         state = string2state(newstate);
1488         if (state == WPA_INVALID)
1489                 return;
1490
1491         task->state = state;
1492
1493         switch (task->state) {
1494         case WPA_SCANNING:
1495                 task->noscan = TRUE;
1496                 connman_device_set_scanning(task->device, TRUE);
1497                 break;
1498         case WPA_ASSOCIATING:
1499         case WPA_ASSOCIATED:
1500         case WPA_4WAY_HANDSHAKE:
1501         case WPA_GROUP_HANDSHAKE:
1502                 task->noscan = TRUE;
1503                 break;
1504         case WPA_COMPLETED:
1505         case WPA_DISCONNECTED:
1506                 task->noscan = FALSE;
1507                 break;
1508         case WPA_INACTIVE:
1509                 task->noscan = FALSE;
1510                 connman_device_set_scanning(task->device, FALSE);
1511                 break;
1512         case WPA_INVALID:
1513                 break;
1514         }
1515
1516         if (task->network == NULL)
1517                 return;
1518
1519         switch (task->state) {
1520         case WPA_COMPLETED:
1521                 /* carrier on */
1522                 if (connman_network_get_group(task->network) == NULL) {
1523                         const char *name, *mode, *security;
1524                         char *group, *bssid;
1525
1526                         /*
1527                          * This is a hidden network, we need to set its
1528                          * group based on the BSSID we just joined.
1529                          */
1530                         bssid = get_bssid(task->device);
1531
1532                         name = connman_network_get_string(task->network,
1533                                                           "Name");
1534                         mode = connman_network_get_string(task->network,
1535                                                           "WiFi.Mode");
1536                         security = connman_network_get_string(task->network,
1537                                                               "WiFi.Security");
1538
1539                         if (bssid && name && mode && security) {
1540                                 group = build_group(bssid, name, NULL, 0,
1541                                                     mode, security);
1542                                 connman_network_set_group(task->network, group);
1543                         }
1544
1545                         g_free(bssid);
1546                         g_free(group);
1547                 }
1548                 connman_network_set_connected(task->network, TRUE);
1549                 connman_device_set_scanning(task->device, FALSE);
1550                 break;
1551         case WPA_DISCONNECTED:
1552                 if (task->disconnecting == TRUE) {
1553                         connman_network_set_connected(task->network, FALSE);
1554                         connman_network_unref(task->network);
1555                         task->disconnecting = FALSE;
1556
1557                         if (task->pending_network != NULL) {
1558                                 task->network = task->pending_network;
1559                                 task->pending_network = NULL;
1560                                 task_connect(task);
1561                         }
1562                 } else {
1563                         /* carrier off */
1564                         connman_network_set_connected(task->network, FALSE);
1565                         connman_device_set_scanning(task->device, FALSE);
1566                 }
1567                 break;
1568         case WPA_ASSOCIATING:
1569                 connman_network_set_associating(task->network, TRUE);
1570                 break;
1571         default:
1572                 connman_network_set_associating(task->network, FALSE);
1573                 break;
1574         }
1575 }
1576
1577 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1578                                                 DBusMessage *msg, void *data)
1579 {
1580         struct supplicant_task *task;
1581         const char *member, *path;
1582
1583         if (dbus_message_has_interface(msg,
1584                                 SUPPLICANT_INTF ".Interface") == FALSE)
1585                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1586
1587         member = dbus_message_get_member(msg);
1588         if (member == NULL)
1589                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1590
1591         path = dbus_message_get_path(msg);
1592         if (path == NULL)
1593                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1594
1595         task = find_task_by_path(path);
1596         if (task == NULL)
1597                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1598
1599         DBG("task %p member %s", task, member);
1600
1601         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1602                 scan_results_available(task);
1603         else if (g_str_equal(member, "StateChange") == TRUE)
1604                 state_change(task, msg);
1605
1606         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1607 }
1608
1609 static int supplicant_get_range(struct supplicant_task *task)
1610 {
1611         struct iwreq wrq;
1612         int fd, err;
1613
1614         fd = socket(PF_INET, SOCK_DGRAM, 0);
1615         if (fd < 0)
1616                 return -1;
1617
1618         memset(&wrq, 0, sizeof(struct iwreq));
1619         strncpy(wrq.ifr_name, task->ifname, IFNAMSIZ);
1620         wrq.u.data.pointer = task->range;
1621         wrq.u.data.length = sizeof(struct iw_range);
1622
1623         err = ioctl(fd, SIOCGIWRANGE, &wrq);
1624
1625         close(fd);
1626
1627         return err;
1628 }
1629
1630 int supplicant_start(struct connman_device *device)
1631 {
1632         struct supplicant_task *task;
1633         int err;
1634
1635         DBG("device %p", device);
1636
1637         task = g_try_new0(struct supplicant_task, 1);
1638         if (task == NULL)
1639                 return -ENOMEM;
1640
1641         task->ifindex = connman_device_get_index(device);
1642         task->ifname = connman_inet_ifname(task->ifindex);
1643
1644         if (task->ifname == NULL) {
1645                 err = -ENOMEM;
1646                 goto failed;
1647         }
1648
1649         task->range = g_try_malloc0(sizeof(struct iw_range));
1650         if (task->range == NULL) {
1651                 err = -ENOMEM;
1652                 goto failed;
1653         }
1654
1655         err = supplicant_get_range(task);
1656         if (err < 0)
1657                 goto failed;
1658
1659         task->device = connman_device_ref(device);
1660
1661         task->created = FALSE;
1662         task->noscan = FALSE;
1663         task->state = WPA_INVALID;
1664         task->disconnecting = FALSE;
1665         task->pending_network = NULL;
1666
1667         task_list = g_slist_append(task_list, task);
1668
1669         return create_interface(task);
1670
1671 failed:
1672         g_free(task->range);
1673         g_free(task->ifname);
1674         g_free(task);
1675
1676         return err;
1677 }
1678
1679 int supplicant_stop(struct connman_device *device)
1680 {
1681         int index = connman_device_get_index(device);
1682         struct supplicant_task *task;
1683
1684         DBG("device %p", device);
1685
1686         task = find_task_by_index(index);
1687         if (task == NULL)
1688                 return -ENODEV;
1689
1690         g_free(task->range);
1691
1692         task_list = g_slist_remove(task_list, task);
1693
1694         disable_network(task);
1695
1696         remove_network(task);
1697
1698         return remove_interface(task);
1699 }
1700
1701 int supplicant_scan(struct connman_device *device)
1702 {
1703         int index = connman_device_get_index(device);
1704         struct supplicant_task *task;
1705         int err;
1706
1707         DBG("device %p", device);
1708
1709         task = find_task_by_index(index);
1710         if (task == NULL)
1711                 return -ENODEV;
1712
1713         switch (task->state) {
1714         case WPA_SCANNING:
1715                 return -EALREADY;
1716         case WPA_ASSOCIATING:
1717         case WPA_ASSOCIATED:
1718         case WPA_4WAY_HANDSHAKE:
1719         case WPA_GROUP_HANDSHAKE:
1720                 return -EBUSY;
1721         default:
1722                 break;
1723         }
1724
1725         err = initiate_scan(task);
1726
1727         return 0;
1728 }
1729
1730 int supplicant_connect(struct connman_network *network)
1731 {
1732         struct supplicant_task *task;
1733         int index;
1734
1735         DBG("network %p", network);
1736
1737         index = connman_network_get_index(network);
1738
1739         task = find_task_by_index(index);
1740         if (task == NULL)
1741                 return -ENODEV;
1742
1743         if (task->disconnecting == TRUE)
1744                 task->pending_network = connman_network_ref(network);
1745         else {
1746                 task->network = connman_network_ref(network);
1747                 return task_connect(task);
1748         }
1749
1750         return 0;
1751 }
1752
1753 int supplicant_disconnect(struct connman_network *network)
1754 {
1755         struct supplicant_task *task;
1756         int index;
1757
1758         DBG("network %p", network);
1759
1760         index = connman_network_get_index(network);
1761
1762         task = find_task_by_index(index);
1763         if (task == NULL)
1764                 return -ENODEV;
1765
1766         if (task->disconnecting == TRUE)
1767                 return -EINPROGRESS;
1768
1769         disable_network(task);
1770
1771         remove_network(task);
1772
1773         task->disconnecting = TRUE;
1774
1775         return 0;
1776 }
1777
1778 static void supplicant_activate(DBusConnection *conn)
1779 {
1780         DBusMessage *message;
1781
1782         DBG("conn %p", conn);
1783
1784         message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1785                                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1786         if (message == NULL)
1787                 return;
1788
1789         dbus_message_set_no_reply(message, TRUE);
1790
1791         dbus_connection_send(conn, message, NULL);
1792
1793         dbus_message_unref(message);
1794 }
1795
1796 static GSList *driver_list = NULL;
1797
1798 static void supplicant_probe(DBusConnection *conn, void *user_data)
1799 {
1800         GSList *list;
1801
1802         DBG("conn %p", conn);
1803
1804         for (list = driver_list; list; list = list->next) {
1805                 struct supplicant_driver *driver = list->data;
1806
1807                 DBG("driver %p name %s", driver, driver->name);
1808
1809                 if (driver->probe)
1810                         driver->probe();
1811         }
1812 }
1813
1814 static void supplicant_remove(DBusConnection *conn, void *user_data)
1815 {
1816         GSList *list;
1817
1818         DBG("conn %p", conn);
1819
1820         for (list = driver_list; list; list = list->next) {
1821                 struct supplicant_driver *driver = list->data;
1822
1823                 DBG("driver %p name %s", driver, driver->name);
1824
1825                 if (driver->remove)
1826                         driver->remove();
1827         }
1828 }
1829
1830 static const char *supplicant_rule = "type=signal,"
1831                                 "interface=" SUPPLICANT_INTF ".Interface";
1832 static guint watch;
1833
1834 static int supplicant_create(void)
1835 {
1836         if (g_slist_length(driver_list) > 0)
1837                 return 0;
1838
1839         connection = connman_dbus_get_connection();
1840         if (connection == NULL)
1841                 return -EIO;
1842
1843         DBG("connection %p", connection);
1844
1845         if (dbus_connection_add_filter(connection,
1846                                 supplicant_filter, NULL, NULL) == FALSE) {
1847                 connection = connman_dbus_get_connection();
1848                 return -EIO;
1849         }
1850
1851         dbus_bus_add_match(connection, supplicant_rule, NULL);
1852         dbus_connection_flush(connection);
1853
1854         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1855                         supplicant_probe, supplicant_remove, NULL, NULL);
1856
1857         return 0;
1858 }
1859
1860 static void supplicant_destroy(void)
1861 {
1862         if (g_slist_length(driver_list) > 0)
1863                 return;
1864
1865         DBG("connection %p", connection);
1866
1867         if (watch > 0)
1868                 g_dbus_remove_watch(connection, watch);
1869
1870         dbus_bus_remove_match(connection, supplicant_rule, NULL);
1871         dbus_connection_flush(connection);
1872
1873         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1874
1875         dbus_connection_unref(connection);
1876         connection = NULL;
1877 }
1878
1879 int supplicant_register(struct supplicant_driver *driver)
1880 {
1881         int err;
1882
1883         DBG("driver %p name %s", driver, driver->name);
1884
1885         err = supplicant_create();
1886         if (err < 0)
1887                 return err;
1888
1889         driver_list = g_slist_append(driver_list, driver);
1890
1891         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1892                 supplicant_probe(connection, NULL);
1893         else
1894                 supplicant_activate(connection);
1895
1896         return 0;
1897 }
1898
1899 void supplicant_unregister(struct supplicant_driver *driver)
1900 {
1901         DBG("driver %p name %s", driver, driver->name);
1902
1903         supplicant_remove(connection, NULL);
1904
1905         driver_list = g_slist_remove(driver_list, driver);
1906
1907         supplicant_destroy();
1908 }