Fix handling of WiFi scanning state
[connman] / plugins / supplicant.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2009  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <gdbus.h>
31
32 #define CONNMAN_API_SUBJECT_TO_CHANGE
33 #include <connman/device.h>
34 #include <connman/dbus.h>
35 #include <connman/log.h>
36
37 #include "inet.h"
38 #include "supplicant.h"
39
40 #define TIMEOUT 5000
41
42 #define IEEE80211_CAP_ESS       0x0001
43 #define IEEE80211_CAP_IBSS      0x0002
44 #define IEEE80211_CAP_PRIVACY   0x0010
45
46 #define SUPPLICANT_NAME  "fi.epitest.hostap.WPASupplicant"
47 #define SUPPLICANT_INTF  "fi.epitest.hostap.WPASupplicant"
48 #define SUPPLICANT_PATH  "/fi/epitest/hostap/WPASupplicant"
49
50 /* Taken from "WPA Supplicant - Common definitions" */
51 enum supplicant_state {
52         /**
53          * WPA_DISCONNECTED - Disconnected state
54          *
55          * This state indicates that client is not associated, but is likely to
56          * start looking for an access point. This state is entered when a
57          * connection is lost.
58          */
59         WPA_DISCONNECTED,
60
61         /**
62          * WPA_INACTIVE - Inactive state (wpa_supplicant disabled)
63          *
64          * This state is entered if there are no enabled networks in the
65          * configuration. wpa_supplicant is not trying to associate with a new
66          * network and external interaction (e.g., ctrl_iface call to add or
67          * enable a network) is needed to start association.
68          */
69         WPA_INACTIVE,
70
71         /**
72          * WPA_SCANNING - Scanning for a network
73          *
74          * This state is entered when wpa_supplicant starts scanning for a
75          * network.
76          */
77         WPA_SCANNING,
78
79         /**
80          * WPA_ASSOCIATING - Trying to associate with a BSS/SSID
81          *
82          * This state is entered when wpa_supplicant has found a suitable BSS
83          * to associate with and the driver is configured to try to associate
84          * with this BSS in ap_scan=1 mode. When using ap_scan=2 mode, this
85          * state is entered when the driver is configured to try to associate
86          * with a network using the configured SSID and security policy.
87          */
88         WPA_ASSOCIATING,
89
90         /**
91          * WPA_ASSOCIATED - Association completed
92          *
93          * This state is entered when the driver reports that association has
94          * been successfully completed with an AP. If IEEE 802.1X is used
95          * (with or without WPA/WPA2), wpa_supplicant remains in this state
96          * until the IEEE 802.1X/EAPOL authentication has been completed.
97          */
98         WPA_ASSOCIATED,
99
100         /**
101          * WPA_4WAY_HANDSHAKE - WPA 4-Way Key Handshake in progress
102          *
103          * This state is entered when WPA/WPA2 4-Way Handshake is started. In
104          * case of WPA-PSK, this happens when receiving the first EAPOL-Key
105          * frame after association. In case of WPA-EAP, this state is entered
106          * when the IEEE 802.1X/EAPOL authentication has been completed.
107          */
108         WPA_4WAY_HANDSHAKE,
109
110         /**
111          * WPA_GROUP_HANDSHAKE - WPA Group Key Handshake in progress
112          *
113          * This state is entered when 4-Way Key Handshake has been completed
114          * (i.e., when the supplicant sends out message 4/4) and when Group
115          * Key rekeying is started by the AP (i.e., when supplicant receives
116          * message 1/2).
117          */
118         WPA_GROUP_HANDSHAKE,
119
120         /**
121          * WPA_COMPLETED - All authentication completed
122          *
123          * This state is entered when the full authentication process is
124          * completed. In case of WPA2, this happens when the 4-Way Handshake is
125          * successfully completed. With WPA, this state is entered after the
126          * Group Key Handshake; with IEEE 802.1X (non-WPA) connection is
127          * completed after dynamic keys are received (or if not used, after
128          * the EAP authentication has been completed). With static WEP keys and
129          * plaintext connections, this state is entered when an association
130          * has been completed.
131          *
132          * This state indicates that the supplicant has completed its
133          * processing for the association phase and that data connection is
134          * fully configured.
135          */
136         WPA_COMPLETED,
137
138         /**
139          * WPA_INVALID - Invalid state (parsing error)
140          *
141          * This state is returned if the string input is invalid. It is not
142          * an official wpa_supplicant state.
143          */
144         WPA_INVALID,
145 };
146
147 struct supplicant_result {
148         char *identifier;
149         unsigned char *ssid;
150         unsigned int ssid_len;
151         dbus_uint16_t capabilities;
152         gboolean adhoc;
153         gboolean has_wep;
154         gboolean has_wpa;
155         gboolean has_rsn;
156         dbus_int32_t quality;
157         dbus_int32_t noise;
158         dbus_int32_t level;
159         dbus_int32_t maxrate;
160 };
161
162 struct supplicant_task {
163         int ifindex;
164         char *ifname;
165         struct connman_device *device;
166         struct connman_network *network;
167         char *path;
168         char *netpath;
169         gboolean created;
170         enum supplicant_state state;
171         gboolean noscan;
172         GSList *scan_results;
173 };
174
175 static GSList *task_list = NULL;
176
177 static DBusConnection *connection;
178
179 static void free_task(struct supplicant_task *task)
180 {
181         DBG("task %p", task);
182
183         g_free(task->ifname);
184         g_free(task->path);
185         g_free(task);
186 }
187
188 static struct supplicant_task *find_task_by_index(int index)
189 {
190         GSList *list;
191
192         for (list = task_list; list; list = list->next) {
193                 struct supplicant_task *task = list->data;
194
195                 if (task->ifindex == index)
196                         return task;
197         }
198
199         return NULL;
200 }
201
202 static struct supplicant_task *find_task_by_path(const char *path)
203 {
204         GSList *list;
205
206         for (list = task_list; list; list = list->next) {
207                 struct supplicant_task *task = list->data;
208
209                 if (g_str_equal(task->path, path) == TRUE)
210                         return task;
211         }
212
213         return NULL;
214 }
215
216 static void add_interface_reply(DBusPendingCall *call, void *user_data)
217 {
218         struct supplicant_task *task = user_data;
219         DBusMessage *reply;
220         DBusError error;
221         const char *path;
222
223         DBG("task %p", task);
224
225         reply = dbus_pending_call_steal_reply(call);
226         if (reply == NULL)
227                 return;
228
229         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
230                 goto done;
231
232         dbus_error_init(&error);
233
234         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
235                                                 DBUS_TYPE_INVALID) == FALSE) {
236                 if (dbus_error_is_set(&error) == TRUE) {
237                         connman_error("%s", error.message);
238                         dbus_error_free(&error);
239                 } else
240                         connman_error("Wrong arguments for add interface");
241                 goto done;
242         }
243
244         DBG("path %s", path);
245
246         task->path = g_strdup(path);
247         task->created = TRUE;
248
249         connman_device_set_powered(task->device, TRUE);
250
251 done:
252         dbus_message_unref(reply);
253 }
254
255 static int add_interface(struct supplicant_task *task)
256 {
257         DBusMessage *message;
258         DBusPendingCall *call;
259
260         DBG("task %p", task);
261
262         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
263                                         SUPPLICANT_INTF, "addInterface");
264         if (message == NULL)
265                 return -ENOMEM;
266
267         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
268                                                         DBUS_TYPE_INVALID);
269
270         if (dbus_connection_send_with_reply(connection, message,
271                                                 &call, TIMEOUT) == FALSE) {
272                 connman_error("Failed to add interface");
273                 dbus_message_unref(message);
274                 return -EIO;
275         }
276
277         dbus_pending_call_set_notify(call, add_interface_reply, task, NULL);
278
279         dbus_message_unref(message);
280
281         return -EINPROGRESS;
282 }
283
284 static void get_interface_reply(DBusPendingCall *call, void *user_data)
285 {
286         struct supplicant_task *task = user_data;
287         DBusMessage *reply;
288         DBusError error;
289         const char *path;
290
291         DBG("task %p", task);
292
293         reply = dbus_pending_call_steal_reply(call);
294         if (reply == NULL)
295                 return;
296
297         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
298                 add_interface(task);
299                 goto done;
300         }
301
302         dbus_error_init(&error);
303
304         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
305                                                 DBUS_TYPE_INVALID) == FALSE) {
306                 if (dbus_error_is_set(&error) == TRUE) {
307                         connman_error("%s", error.message);
308                         dbus_error_free(&error);
309                 } else
310                         connman_error("Wrong arguments for get interface");
311                 goto done;
312         }
313
314         DBG("path %s", path);
315
316         task->path = g_strdup(path);
317         task->created = FALSE;
318
319         connman_device_set_powered(task->device, TRUE);
320
321 done:
322         dbus_message_unref(reply);
323 }
324
325 static int create_interface(struct supplicant_task *task)
326 {
327         DBusMessage *message;
328         DBusPendingCall *call;
329
330         DBG("task %p", task);
331
332         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
333                                         SUPPLICANT_INTF, "getInterface");
334         if (message == NULL)
335                 return -ENOMEM;
336
337         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
338                                                         DBUS_TYPE_INVALID);
339
340         if (dbus_connection_send_with_reply(connection, message,
341                                                 &call, TIMEOUT) == FALSE) {
342                 connman_error("Failed to get interface");
343                 dbus_message_unref(message);
344                 return -EIO;
345         }
346
347         dbus_pending_call_set_notify(call, get_interface_reply, task, NULL);
348
349         dbus_message_unref(message);
350
351         return -EINPROGRESS;
352 }
353
354 static void remove_interface_reply(DBusPendingCall *call, void *user_data)
355 {
356         struct supplicant_task *task = user_data;
357         DBusMessage *reply;
358
359         DBG("task %p", task);
360
361         reply = dbus_pending_call_steal_reply(call);
362
363         connman_device_set_powered(task->device, FALSE);
364
365         free_task(task);
366
367         dbus_message_unref(reply);
368 }
369
370 static int remove_interface(struct supplicant_task *task)
371 {
372         DBusMessage *message;
373         DBusPendingCall *call;
374
375         DBG("task %p", task);
376
377         if (task->created == FALSE) {
378                 connman_device_set_powered(task->device, FALSE);
379                 return 0;
380         }
381
382         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
383                                         SUPPLICANT_INTF, "removeInterface");
384         if (message == NULL)
385                 return -ENOMEM;
386
387         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
388                                                         DBUS_TYPE_INVALID);
389
390         if (dbus_connection_send_with_reply(connection, message,
391                                                 &call, TIMEOUT) == FALSE) {
392                 connman_error("Failed to remove interface");
393                 dbus_message_unref(message);
394                 return -EIO;
395         }
396
397         dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
398
399         dbus_message_unref(message);
400
401         return -EINPROGRESS;
402 }
403
404 #if 0
405 static int set_ap_scan(struct supplicant_task *task)
406 {
407         DBusMessage *message, *reply;
408         DBusError error;
409         guint32 ap_scan = 1;
410
411         DBG("task %p", task);
412
413         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
414                                 SUPPLICANT_INTF ".Interface", "setAPScan");
415         if (message == NULL)
416                 return -ENOMEM;
417
418         dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
419                                                         DBUS_TYPE_INVALID);
420
421         dbus_error_init(&error);
422
423         reply = dbus_connection_send_with_reply_and_block(connection,
424                                                         message, -1, &error);
425         if (reply == NULL) {
426                 if (dbus_error_is_set(&error) == TRUE) {
427                         connman_error("%s", error.message);
428                         dbus_error_free(&error);
429                 } else
430                         connman_error("Failed to set AP scan");
431                 dbus_message_unref(message);
432                 return -EIO;
433         }
434
435         dbus_message_unref(message);
436
437         dbus_message_unref(reply);
438
439         return 0;
440 }
441 #endif
442
443 static int add_network(struct supplicant_task *task)
444 {
445         DBusMessage *message, *reply;
446         DBusError error;
447         const char *path;
448
449         DBG("task %p", task);
450
451         if (task->netpath != NULL)
452                 return -EALREADY;
453
454         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
455                                 SUPPLICANT_INTF ".Interface", "addNetwork");
456         if (message == NULL)
457                 return -ENOMEM;
458
459         dbus_error_init(&error);
460
461         reply = dbus_connection_send_with_reply_and_block(connection,
462                                                         message, -1, &error);
463         if (reply == NULL) {
464                 if (dbus_error_is_set(&error) == TRUE) {
465                         connman_error("%s", error.message);
466                         dbus_error_free(&error);
467                 } else
468                         connman_error("Failed to add network");
469                 dbus_message_unref(message);
470                 return -EIO;
471         }
472
473         dbus_message_unref(message);
474
475         dbus_error_init(&error);
476
477         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
478                                                 DBUS_TYPE_INVALID) == FALSE) {
479                 if (dbus_error_is_set(&error) == TRUE) {
480                         connman_error("%s", error.message);
481                         dbus_error_free(&error);
482                 } else
483                         connman_error("Wrong arguments for network");
484                 dbus_message_unref(reply);
485                 return -EIO;
486         }
487
488         DBG("path %s", path);
489
490         task->netpath = g_strdup(path);
491
492         dbus_message_unref(reply);
493
494         return 0;
495 }
496
497 static int remove_network(struct supplicant_task *task)
498 {
499         DBusMessage *message, *reply;
500         DBusError error;
501
502         DBG("task %p", task);
503
504         if (task->netpath == NULL)
505                 return -EINVAL;
506
507         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
508                                 SUPPLICANT_INTF ".Interface", "removeNetwork");
509         if (message == NULL)
510                 return -ENOMEM;
511
512         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
513                                                         DBUS_TYPE_INVALID);
514
515         dbus_error_init(&error);
516
517         reply = dbus_connection_send_with_reply_and_block(connection,
518                                                         message, -1, &error);
519         if (reply == NULL) {
520                 if (dbus_error_is_set(&error) == TRUE) {
521                         connman_error("%s", error.message);
522                         dbus_error_free(&error);
523                 } else
524                         connman_error("Failed to remove network");
525                 dbus_message_unref(message);
526                 return -EIO;
527         }
528
529         dbus_message_unref(message);
530
531         dbus_message_unref(reply);
532
533         g_free(task->netpath);
534         task->netpath = NULL;
535
536         return 0;
537 }
538
539 static int select_network(struct supplicant_task *task)
540 {
541         DBusMessage *message, *reply;
542         DBusError error;
543
544         DBG("task %p", task);
545
546         if (task->netpath == NULL)
547                 return -EINVAL;
548
549         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
550                                 SUPPLICANT_INTF ".Interface", "selectNetwork");
551         if (message == NULL)
552                 return -ENOMEM;
553
554         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
555                                                         DBUS_TYPE_INVALID);
556
557         dbus_error_init(&error);
558
559         reply = dbus_connection_send_with_reply_and_block(connection,
560                                                         message, -1, &error);
561         if (reply == NULL) {
562                 if (dbus_error_is_set(&error) == TRUE) {
563                         connman_error("%s", error.message);
564                         dbus_error_free(&error);
565                 } else
566                         connman_error("Failed to select network");
567                 dbus_message_unref(message);
568                 return -EIO;
569         }
570
571         dbus_message_unref(message);
572
573         dbus_message_unref(reply);
574
575         return 0;
576 }
577
578 static int enable_network(struct supplicant_task *task)
579 {
580         DBusMessage *message, *reply;
581         DBusError error;
582
583         DBG("task %p", task);
584
585         if (task->netpath == NULL)
586                 return -EINVAL;
587
588         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
589                                         SUPPLICANT_INTF ".Network", "enable");
590         if (message == NULL)
591                 return -ENOMEM;
592
593         dbus_error_init(&error);
594
595         reply = dbus_connection_send_with_reply_and_block(connection,
596                                                         message, -1, &error);
597         if (reply == NULL) {
598                 if (dbus_error_is_set(&error) == TRUE) {
599                         connman_error("%s", error.message);
600                         dbus_error_free(&error);
601                 } else
602                         connman_error("Failed to enable network");
603                 dbus_message_unref(message);
604                 return -EIO;
605         }
606
607         dbus_message_unref(message);
608
609         dbus_message_unref(reply);
610
611         return 0;
612 }
613
614 static int disable_network(struct supplicant_task *task)
615 {
616         DBusMessage *message, *reply;
617         DBusError error;
618
619         DBG("task %p", task);
620
621         if (task->netpath == NULL)
622                 return -EINVAL;
623
624         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
625                                         SUPPLICANT_INTF ".Network", "disable");
626         if (message == NULL)
627                 return -ENOMEM;
628
629         dbus_error_init(&error);
630
631         reply = dbus_connection_send_with_reply_and_block(connection,
632                                                         message, -1, &error);
633         if (reply == NULL) {
634                 if (dbus_error_is_set(&error) == TRUE) {
635                         connman_error("%s", error.message);
636                         dbus_error_free(&error);
637                 } else
638                         connman_error("Failed to disable network");
639                 dbus_message_unref(message);
640                 return -EIO;
641         }
642
643         dbus_message_unref(message);
644
645         dbus_message_unref(reply);
646
647         return 0;
648 }
649
650 static int set_network(struct supplicant_task *task,
651                                 const unsigned char *network, int len,
652                                 const char *security, const char *passphrase)
653 {
654         DBusMessage *message, *reply;
655         DBusMessageIter array, dict;
656         DBusError error;
657
658         DBG("task %p", task);
659
660         if (task->netpath == NULL)
661                 return -EINVAL;
662
663         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
664                                         SUPPLICANT_INTF ".Network", "set");
665         if (message == NULL)
666                 return -ENOMEM;
667
668         dbus_message_iter_init_append(message, &array);
669
670         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
671                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
672                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
673                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
674
675         connman_dbus_dict_append_array(&dict, "ssid",
676                                         DBUS_TYPE_BYTE, &network, len);
677
678         if (g_ascii_strcasecmp(security, "wpa") == 0 ||
679                                 g_ascii_strcasecmp(security, "wpa2") == 0) {
680                 const char *key_mgmt = "WPA-PSK";
681                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
682                                                 DBUS_TYPE_STRING, &key_mgmt);
683
684                 if (passphrase && strlen(passphrase) > 0)
685                         connman_dbus_dict_append_variant(&dict, "psk",
686                                                 DBUS_TYPE_STRING, &passphrase);
687         } else if (g_ascii_strcasecmp(security, "wep") == 0) {
688                 const char *key_mgmt = "NONE", *index = "0";
689                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
690                                                 DBUS_TYPE_STRING, &key_mgmt);
691
692                 if (passphrase) {
693                         int size = strlen(passphrase);
694                         if (size == 10 || size == 26) {
695                                 unsigned char *key = malloc(13);
696                                 char tmp[3];
697                                 int i;
698                                 memset(tmp, 0, sizeof(tmp));
699                                 if (key == NULL)
700                                         size = 0;
701                                 for (i = 0; i < size / 2; i++) {
702                                         memcpy(tmp, passphrase + (i * 2), 2);
703                                         key[i] = (unsigned char) strtol(tmp,
704                                                                 NULL, 16);
705                                 }
706                                 connman_dbus_dict_append_array(&dict,
707                                                 "wep_key0", DBUS_TYPE_BYTE,
708                                                         &key, size / 2);
709                                 free(key);
710                         } else
711                                 connman_dbus_dict_append_variant(&dict,
712                                                 "wep_key0", DBUS_TYPE_STRING,
713                                                                 &passphrase);
714                         connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
715                                                 DBUS_TYPE_STRING, &index);
716                 }
717         } else {
718                 const char *key_mgmt = "NONE";
719                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
720                                                 DBUS_TYPE_STRING, &key_mgmt);
721         }
722
723         dbus_message_iter_close_container(&array, &dict);
724
725         dbus_error_init(&error);
726
727         reply = dbus_connection_send_with_reply_and_block(connection,
728                                                         message, -1, &error);
729         if (reply == NULL) {
730                 if (dbus_error_is_set(&error) == TRUE) {
731                         connman_error("%s", error.message);
732                         dbus_error_free(&error);
733                 } else
734                         connman_error("Failed to set network options");
735                 dbus_message_unref(message);
736                 return -EIO;
737         }
738
739         dbus_message_unref(message);
740
741         dbus_message_unref(reply);
742
743         return 0;
744 }
745
746 static int initiate_scan(struct supplicant_task *task)
747 {
748         DBusMessage *message;
749         DBusPendingCall *call;
750
751         DBG("task %p", task);
752
753         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
754                                         SUPPLICANT_INTF ".Interface", "scan");
755         if (message == NULL)
756                 return -ENOMEM;
757
758         if (dbus_connection_send_with_reply(connection, message,
759                                                 &call, TIMEOUT) == FALSE) {
760                 connman_error("Failed to initiate scan");
761                 dbus_message_unref(message);
762                 return -EIO;
763         }
764
765         dbus_message_unref(message);
766
767         return 0;
768 }
769
770 static void extract_ssid(DBusMessageIter *value,
771                                         struct supplicant_result *result)
772 {
773         DBusMessageIter array;
774         unsigned char *ssid;
775         int ssid_len;
776
777         dbus_message_iter_recurse(value, &array);
778         dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
779
780         if (ssid_len < 1)
781                 return;
782
783         result->ssid = g_try_malloc(ssid_len);
784         if (result->ssid == NULL)
785                 return;
786
787         memcpy(result->ssid, ssid, ssid_len);
788         result->ssid_len = ssid_len;
789
790         result->identifier = g_try_malloc0(ssid_len + 1);
791         if (result->identifier == NULL)
792                 return;
793
794         memcpy(result->identifier, ssid, ssid_len);
795 }
796
797 static void extract_wpaie(DBusMessageIter *value,
798                                         struct supplicant_result *result)
799 {
800         DBusMessageIter array;
801         unsigned char *ie;
802         int ie_len;
803
804         dbus_message_iter_recurse(value, &array);
805         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
806
807         if (ie_len > 0)
808                 result->has_wpa = TRUE;
809 }
810
811 static void extract_rsnie(DBusMessageIter *value,
812                                         struct supplicant_result *result)
813 {
814         DBusMessageIter array;
815         unsigned char *ie;
816         int ie_len;
817
818         dbus_message_iter_recurse(value, &array);
819         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
820
821         if (ie_len > 0)
822                 result->has_rsn = TRUE;
823 }
824
825 static void extract_capabilites(DBusMessageIter *value,
826                                         struct supplicant_result *result)
827 {
828         dbus_message_iter_get_basic(value, &result->capabilities);
829
830         if (result->capabilities & IEEE80211_CAP_ESS)
831                 result->adhoc = FALSE;
832         else if (result->capabilities & IEEE80211_CAP_IBSS)
833                 result->adhoc = TRUE;
834
835         if (result->capabilities & IEEE80211_CAP_PRIVACY)
836                 result->has_wep = TRUE;
837 }
838
839 static void get_properties(struct supplicant_task *task);
840
841 static void properties_reply(DBusPendingCall *call, void *user_data)
842 {
843         struct supplicant_task *task = user_data;
844         struct supplicant_result result;
845         struct connman_network *network;
846         DBusMessage *reply;
847         DBusMessageIter array, dict;
848         char *security, *temp = NULL;
849         unsigned char strength;
850         unsigned int i;
851
852         DBG("task %p", task);
853
854         reply = dbus_pending_call_steal_reply(call);
855         if (reply == NULL) {
856                 get_properties(task);
857                 return;
858         }
859
860         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
861                 dbus_message_unref(reply);
862                 get_properties(task);
863                 return;
864         }
865
866         memset(&result, 0, sizeof(result));
867
868         dbus_message_iter_init(reply, &array);
869
870         dbus_message_iter_recurse(&array, &dict);
871
872         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
873                 DBusMessageIter entry, value;
874                 const char *key;
875
876                 dbus_message_iter_recurse(&dict, &entry);
877                 dbus_message_iter_get_basic(&entry, &key);
878
879                 dbus_message_iter_next(&entry);
880
881                 dbus_message_iter_recurse(&entry, &value);
882
883                 //type = dbus_message_iter_get_arg_type(&value);
884                 //dbus_message_iter_get_basic(&value, &val);
885
886                 /* 
887                  * bssid        : a (97)
888                  * ssid         : a (97)
889                  * wpaie        : a (97)
890                  * rsnie        : a (97)
891                  * frequency    : i (105)
892                  * capabilities : q (113)
893                  * quality      : i (105)
894                  * noise        : i (105)
895                  * level        : i (105)
896                  * maxrate      : i (105)
897                  */
898
899                 if (g_str_equal(key, "ssid") == TRUE)
900                         extract_ssid(&value, &result);
901                 else if (g_str_equal(key, "wpaie") == TRUE)
902                         extract_wpaie(&value, &result);
903                 else if (g_str_equal(key, "rsnie") == TRUE)
904                         extract_rsnie(&value, &result);
905                 else if (g_str_equal(key, "capabilities") == TRUE)
906                         extract_capabilites(&value, &result);
907                 else if (g_str_equal(key, "quality") == TRUE)
908                         dbus_message_iter_get_basic(&value, &result.quality);
909                 else if (g_str_equal(key, "noise") == TRUE)
910                         dbus_message_iter_get_basic(&value, &result.noise);
911                 else if (g_str_equal(key, "level") == TRUE)
912                         dbus_message_iter_get_basic(&value, &result.level);
913                 else if (g_str_equal(key, "maxrate") == TRUE)
914                         dbus_message_iter_get_basic(&value, &result.maxrate);
915
916                 dbus_message_iter_next(&dict);
917         }
918
919         if (result.identifier == NULL)
920                 goto done;
921
922         if (result.identifier[0] == '\0')
923                 goto done;
924
925         temp = g_strdup(result.identifier);
926         if (temp == NULL)
927                 goto done;
928
929         for (i = 0; i < strlen(temp); i++) {
930                 char tmp = temp[i];
931                 if ((tmp < '0' || tmp > '9') && (tmp < 'A' || tmp > 'Z') &&
932                                                 (tmp < 'a' || tmp > 'z'))
933                         temp[i] = '_';
934         }
935
936         strength = result.quality;
937
938         if (result.has_rsn == TRUE)
939                 security = "wpa2";
940         else if (result.has_wpa == TRUE)
941                 security = "wpa";
942         else if (result.has_wep == TRUE)
943                 security = "wep";
944         else
945                 security = "none";
946
947         network = connman_device_get_network(task->device, temp);
948         if (network == NULL) {
949                 const char *mode;
950                 int index;
951
952                 network = connman_network_create(temp,
953                                                 CONNMAN_NETWORK_TYPE_WIFI);
954                 if (network == NULL)
955                         goto done;
956
957                 index = connman_device_get_index(task->device);
958                 connman_network_set_index(network, index);
959
960                 connman_network_set_protocol(network,
961                                                 CONNMAN_NETWORK_PROTOCOL_IP);
962
963                 connman_network_set_string(network, "Name", result.identifier);
964
965                 connman_network_set_blob(network, "WiFi.SSID",
966                                                 result.ssid, result.ssid_len);
967
968                 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
969                 connman_network_set_string(network, "WiFi.Mode", mode);
970
971                 DBG("%s (%s %s) strength %d", result.identifier, mode,
972                                                         security, strength);
973
974                 if (connman_device_add_network(task->device, network) < 0) {
975                         connman_network_unref(network);
976                         goto done;
977                 }
978         }
979
980         connman_network_set_uint8(network, "Strength", strength);
981
982         connman_network_set_string(network, "WiFi.Security", security);
983
984 done:
985         g_free(result.identifier);
986         g_free(result.ssid);
987         g_free(temp);
988
989         dbus_message_unref(reply);
990
991         get_properties(task);
992 }
993
994 static void get_properties(struct supplicant_task *task)
995 {
996         DBusMessage *message;
997         DBusPendingCall *call;
998         char *path;
999
1000         path = g_slist_nth_data(task->scan_results, 0);
1001         if (path == NULL)
1002                 goto noscan;
1003
1004         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
1005                                                 SUPPLICANT_INTF ".BSSID",
1006                                                                 "properties");
1007
1008         task->scan_results = g_slist_remove(task->scan_results, path);
1009         g_free(path);
1010
1011         if (message == NULL)
1012                 goto noscan;
1013
1014         if (dbus_connection_send_with_reply(connection, message,
1015                                                 &call, TIMEOUT) == FALSE) {
1016                 connman_error("Failed to get network properties");
1017                 dbus_message_unref(message);
1018                 goto noscan;
1019         }
1020
1021         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1022
1023         dbus_message_unref(message);
1024
1025         return;
1026
1027 noscan:
1028         if (task->noscan == FALSE)
1029                 connman_device_set_scanning(task->device, FALSE);
1030 }
1031
1032 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1033 {
1034         struct supplicant_task *task = user_data;
1035         DBusMessage *reply;
1036         DBusError error;
1037         char **results;
1038         int i, num_results;
1039
1040         DBG("task %p", task);
1041
1042         reply = dbus_pending_call_steal_reply(call);
1043         if (reply == NULL)
1044                 goto noscan;
1045
1046         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1047                 goto done;
1048
1049         dbus_error_init(&error);
1050
1051         if (dbus_message_get_args(reply, &error,
1052                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1053                                                 &results, &num_results,
1054                                                 DBUS_TYPE_INVALID) == FALSE) {
1055                 if (dbus_error_is_set(&error) == TRUE) {
1056                         connman_error("%s", error.message);
1057                         dbus_error_free(&error);
1058                 } else
1059                         connman_error("Wrong arguments for scan result");
1060                 goto done;
1061         }
1062
1063         if (num_results == 0)
1064                 goto done;
1065
1066         for (i = 0; i < num_results; i++) {
1067                 char *path = g_strdup(results[i]);
1068                 if (path == NULL)
1069                         continue;
1070
1071                 task->scan_results = g_slist_append(task->scan_results, path);
1072         }
1073
1074         g_strfreev(results);
1075
1076         dbus_message_unref(reply);
1077
1078         get_properties(task);
1079
1080         return;
1081
1082 done:
1083         dbus_message_unref(reply);
1084
1085 noscan:
1086         if (task->noscan == FALSE)
1087                 connman_device_set_scanning(task->device, FALSE);
1088 }
1089
1090 static void scan_results_available(struct supplicant_task *task)
1091 {
1092         DBusMessage *message;
1093         DBusPendingCall *call;
1094
1095         DBG("task %p", task);
1096
1097         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1098                                                 SUPPLICANT_INTF ".Interface",
1099                                                         "scanResults");
1100         if (message == NULL)
1101                 return;
1102
1103         if (dbus_connection_send_with_reply(connection, message,
1104                                                 &call, TIMEOUT) == FALSE) {
1105                 connman_error("Failed to request scan result");
1106                 goto done;
1107         }
1108
1109         if (task->noscan == FALSE)
1110                 connman_device_set_scanning(task->device, TRUE);
1111
1112         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1113
1114 done:
1115         dbus_message_unref(message);
1116 }
1117
1118 static enum supplicant_state string2state(const char *state)
1119 {
1120         if (g_str_equal(state, "INACTIVE") == TRUE)
1121                 return WPA_INACTIVE;
1122         else if (g_str_equal(state, "SCANNING") == TRUE)
1123                 return WPA_SCANNING;
1124         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1125                 return WPA_ASSOCIATING;
1126         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1127                 return WPA_ASSOCIATED;
1128         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1129                 return WPA_GROUP_HANDSHAKE;
1130         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1131                 return WPA_4WAY_HANDSHAKE;
1132         else if (g_str_equal(state, "COMPLETED") == TRUE)
1133                 return WPA_COMPLETED;
1134         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1135                 return WPA_DISCONNECTED;
1136         else
1137                 return WPA_INVALID;
1138 }
1139
1140 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1141 {
1142         DBusError error;
1143         const char *newstate, *oldstate;
1144         enum supplicant_state state;
1145
1146         dbus_error_init(&error);
1147
1148         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1149                                                 DBUS_TYPE_STRING, &oldstate,
1150                                                 DBUS_TYPE_INVALID) == FALSE) {
1151                 if (dbus_error_is_set(&error) == TRUE) {
1152                         connman_error("%s", error.message);
1153                         dbus_error_free(&error);
1154                 } else
1155                         connman_error("Wrong arguments for state change");
1156                 return;
1157         }
1158
1159         DBG("state %s ==> %s", oldstate, newstate);
1160
1161         state = string2state(newstate);
1162         if (state == WPA_INVALID)
1163                 return;
1164
1165         task->state = state;
1166
1167         switch (task->state) {
1168         case WPA_SCANNING:
1169                 task->noscan = TRUE;
1170                 connman_device_set_scanning(task->device, TRUE);
1171                 break;
1172         case WPA_ASSOCIATING:
1173         case WPA_ASSOCIATED:
1174         case WPA_4WAY_HANDSHAKE:
1175         case WPA_GROUP_HANDSHAKE:
1176                 task->noscan = TRUE;
1177                 break;
1178         case WPA_COMPLETED:
1179         case WPA_DISCONNECTED:
1180                 task->noscan = FALSE;
1181                 break;
1182         case WPA_INACTIVE:
1183                 task->noscan = FALSE;
1184                 connman_device_set_scanning(task->device, FALSE);
1185                 break;
1186         case WPA_INVALID:
1187                 break;
1188         }
1189
1190         if (task->network == NULL)
1191                 return;
1192
1193         switch (task->state) {
1194         case WPA_COMPLETED:
1195                 /* carrier on */
1196                 connman_network_set_connected(task->network, TRUE);
1197                 connman_device_set_scanning(task->device, FALSE);
1198                 break;
1199         case WPA_DISCONNECTED:
1200                 /* carrier off */
1201                 connman_network_set_connected(task->network, FALSE);
1202                 connman_device_set_scanning(task->device, FALSE);
1203                 break;
1204         default:
1205                 break;
1206         }
1207 }
1208
1209 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1210                                                 DBusMessage *msg, void *data)
1211 {
1212         struct supplicant_task *task;
1213         const char *member, *path;
1214
1215         if (dbus_message_has_interface(msg,
1216                                 SUPPLICANT_INTF ".Interface") == FALSE)
1217                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1218
1219         member = dbus_message_get_member(msg);
1220         if (member == NULL)
1221                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1222
1223         path = dbus_message_get_path(msg);
1224         if (path == NULL)
1225                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1226
1227         task = find_task_by_path(path);
1228         if (task == NULL)
1229                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1230
1231         DBG("task %p member %s", task, member);
1232
1233         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1234                 scan_results_available(task);
1235         else if (g_str_equal(member, "StateChange") == TRUE)
1236                 state_change(task, msg);
1237
1238         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1239 }
1240
1241 int supplicant_start(struct connman_device *device)
1242 {
1243         struct supplicant_task *task;
1244
1245         DBG("device %p", device);
1246
1247         task = g_try_new0(struct supplicant_task, 1);
1248         if (task == NULL)
1249                 return -ENOMEM;
1250
1251         task->ifindex = connman_device_get_index(device);
1252         task->ifname = inet_index2name(task->ifindex);
1253         task->device = device;
1254
1255         if (task->ifname == NULL) {
1256                 g_free(task);
1257                 return -ENOMEM;
1258         }
1259
1260         task->created = FALSE;
1261         task->noscan = FALSE;
1262         task->state = WPA_INVALID;
1263
1264         task_list = g_slist_append(task_list, task);
1265
1266         return create_interface(task);
1267 }
1268
1269 int supplicant_stop(struct connman_device *device)
1270 {
1271         int index = connman_device_get_index(device);
1272         struct supplicant_task *task;
1273
1274         DBG("device %p", device);
1275
1276         task = find_task_by_index(index);
1277         if (task == NULL)
1278                 return -ENODEV;
1279
1280         task_list = g_slist_remove(task_list, task);
1281
1282         disable_network(task);
1283
1284         remove_network(task);
1285
1286         return remove_interface(task);
1287 }
1288
1289 int supplicant_scan(struct connman_device *device)
1290 {
1291         int index = connman_device_get_index(device);
1292         struct supplicant_task *task;
1293         int err;
1294
1295         DBG("device %p", device);
1296
1297         task = find_task_by_index(index);
1298         if (task == NULL)
1299                 return -ENODEV;
1300
1301         switch (task->state) {
1302         case WPA_SCANNING:
1303                 return -EALREADY;
1304         case WPA_ASSOCIATING:
1305         case WPA_ASSOCIATED:
1306         case WPA_4WAY_HANDSHAKE:
1307         case WPA_GROUP_HANDSHAKE:
1308                 return -EBUSY;
1309         default:
1310                 break;
1311         }
1312
1313         err = initiate_scan(task);
1314
1315         return 0;
1316 }
1317
1318 int supplicant_connect(struct connman_network *network)
1319 {
1320         struct supplicant_task *task;
1321         const char *security, *passphrase;
1322         const void *ssid;
1323         unsigned int ssid_len;
1324         int index;
1325
1326         DBG("network %p", network);
1327
1328         security = connman_network_get_string(network, "WiFi.Security");
1329         passphrase = connman_network_get_string(network, "WiFi.Passphrase");
1330
1331         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
1332
1333         DBG("security %s passphrase %s", security, passphrase);
1334
1335         if (security == NULL && passphrase == NULL)
1336                 return -EINVAL;
1337
1338         if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1339                 return -EINVAL;
1340
1341         index = connman_network_get_index(network);
1342
1343         task = find_task_by_index(index);
1344         if (task == NULL)
1345                 return -ENODEV;
1346
1347         task->network = connman_network_ref(network);
1348
1349         add_network(task);
1350
1351         select_network(task);
1352         disable_network(task);
1353
1354         set_network(task, ssid, ssid_len, security, passphrase);
1355
1356         enable_network(task);
1357
1358         return 0;
1359 }
1360
1361 int supplicant_disconnect(struct connman_network *network)
1362 {
1363         struct supplicant_task *task;
1364         int index;
1365
1366         DBG("network %p", network);
1367
1368         index = connman_network_get_index(network);
1369
1370         task = find_task_by_index(index);
1371         if (task == NULL)
1372                 return -ENODEV;
1373
1374         disable_network(task);
1375
1376         remove_network(task);
1377
1378         connman_network_unref(task->network);
1379
1380         return 0;
1381 }
1382
1383 static void supplicant_activate(DBusConnection *conn)
1384 {
1385         DBusMessage *message;
1386
1387         DBG("conn %p", conn);
1388
1389         message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1390                                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1391         if (message == NULL)
1392                 return;
1393
1394         dbus_message_set_no_reply(message, TRUE);
1395
1396         dbus_connection_send(conn, message, NULL);
1397
1398         dbus_message_unref(message);
1399 }
1400
1401 static GSList *driver_list = NULL;
1402
1403 static void supplicant_probe(DBusConnection *conn, void *user_data)
1404 {
1405         GSList *list;
1406
1407         DBG("conn %p", conn);
1408
1409         for (list = driver_list; list; list = list->next) {
1410                 struct supplicant_driver *driver = list->data;
1411
1412                 DBG("driver %p name %s", driver, driver->name);
1413
1414                 if (driver->probe)
1415                         driver->probe();
1416         }
1417 }
1418
1419 static void supplicant_remove(DBusConnection *conn, void *user_data)
1420 {
1421         GSList *list;
1422
1423         DBG("conn %p", conn);
1424
1425         for (list = driver_list; list; list = list->next) {
1426                 struct supplicant_driver *driver = list->data;
1427
1428                 DBG("driver %p name %s", driver, driver->name);
1429
1430                 if (driver->remove)
1431                         driver->remove();
1432         }
1433 }
1434
1435 static const char *supplicant_rule = "type=signal,"
1436                                 "interface=" SUPPLICANT_INTF ".Interface";
1437 static guint watch;
1438
1439 static int supplicant_create(void)
1440 {
1441         if (g_slist_length(driver_list) > 0)
1442                 return 0;
1443
1444         connection = connman_dbus_get_connection();
1445         if (connection == NULL)
1446                 return -EIO;
1447
1448         DBG("connection %p", connection);
1449
1450         if (dbus_connection_add_filter(connection,
1451                                 supplicant_filter, NULL, NULL) == FALSE) {
1452                 connection = connman_dbus_get_connection();
1453                 return -EIO;
1454         }
1455
1456         dbus_bus_add_match(connection, supplicant_rule, NULL);
1457         dbus_connection_flush(connection);
1458
1459         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1460                         supplicant_probe, supplicant_remove, NULL, NULL);
1461
1462         return 0;
1463 }
1464
1465 static void supplicant_destroy(void)
1466 {
1467         if (g_slist_length(driver_list) > 0)
1468                 return;
1469
1470         DBG("connection %p", connection);
1471
1472         if (watch > 0)
1473                 g_dbus_remove_watch(connection, watch);
1474
1475         dbus_bus_remove_match(connection, supplicant_rule, NULL);
1476         dbus_connection_flush(connection);
1477
1478         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1479
1480         dbus_connection_unref(connection);
1481         connection = NULL;
1482 }
1483
1484 int supplicant_register(struct supplicant_driver *driver)
1485 {
1486         int err;
1487
1488         DBG("driver %p name %s", driver, driver->name);
1489
1490         err = supplicant_create();
1491         if (err < 0)
1492                 return err;
1493
1494         driver_list = g_slist_append(driver_list, driver);
1495
1496         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1497                 supplicant_probe(connection, NULL);
1498         else
1499                 supplicant_activate(connection);
1500
1501         return 0;
1502 }
1503
1504 void supplicant_unregister(struct supplicant_driver *driver)
1505 {
1506         DBG("driver %p name %s", driver, driver->name);
1507
1508         supplicant_remove(connection, NULL);
1509
1510         driver_list = g_slist_remove(driver_list, driver);
1511
1512         supplicant_destroy();
1513 }