f187b86a282575d90fc4cc648b2abe3ed0dc1833
[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         connman_device_unref(task->device);
366
367         free_task(task);
368
369         dbus_message_unref(reply);
370 }
371
372 static int remove_interface(struct supplicant_task *task)
373 {
374         DBusMessage *message;
375         DBusPendingCall *call;
376
377         DBG("task %p", task);
378
379         if (task->created == FALSE) {
380                 connman_device_set_powered(task->device, FALSE);
381                 return 0;
382         }
383
384         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
385                                         SUPPLICANT_INTF, "removeInterface");
386         if (message == NULL)
387                 return -ENOMEM;
388
389         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
390                                                         DBUS_TYPE_INVALID);
391
392         if (dbus_connection_send_with_reply(connection, message,
393                                                 &call, TIMEOUT) == FALSE) {
394                 connman_error("Failed to remove interface");
395                 dbus_message_unref(message);
396                 return -EIO;
397         }
398
399         dbus_pending_call_set_notify(call, remove_interface_reply, task, NULL);
400
401         dbus_message_unref(message);
402
403         return -EINPROGRESS;
404 }
405
406 #if 0
407 static int set_ap_scan(struct supplicant_task *task)
408 {
409         DBusMessage *message, *reply;
410         DBusError error;
411         guint32 ap_scan = 1;
412
413         DBG("task %p", task);
414
415         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
416                                 SUPPLICANT_INTF ".Interface", "setAPScan");
417         if (message == NULL)
418                 return -ENOMEM;
419
420         dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
421                                                         DBUS_TYPE_INVALID);
422
423         dbus_error_init(&error);
424
425         reply = dbus_connection_send_with_reply_and_block(connection,
426                                                         message, -1, &error);
427         if (reply == NULL) {
428                 if (dbus_error_is_set(&error) == TRUE) {
429                         connman_error("%s", error.message);
430                         dbus_error_free(&error);
431                 } else
432                         connman_error("Failed to set AP scan");
433                 dbus_message_unref(message);
434                 return -EIO;
435         }
436
437         dbus_message_unref(message);
438
439         dbus_message_unref(reply);
440
441         return 0;
442 }
443 #endif
444
445 static int add_network(struct supplicant_task *task)
446 {
447         DBusMessage *message, *reply;
448         DBusError error;
449         const char *path;
450
451         DBG("task %p", task);
452
453         if (task->netpath != NULL)
454                 return -EALREADY;
455
456         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
457                                 SUPPLICANT_INTF ".Interface", "addNetwork");
458         if (message == NULL)
459                 return -ENOMEM;
460
461         dbus_error_init(&error);
462
463         reply = dbus_connection_send_with_reply_and_block(connection,
464                                                         message, -1, &error);
465         if (reply == NULL) {
466                 if (dbus_error_is_set(&error) == TRUE) {
467                         connman_error("%s", error.message);
468                         dbus_error_free(&error);
469                 } else
470                         connman_error("Failed to add network");
471                 dbus_message_unref(message);
472                 return -EIO;
473         }
474
475         dbus_message_unref(message);
476
477         dbus_error_init(&error);
478
479         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
480                                                 DBUS_TYPE_INVALID) == FALSE) {
481                 if (dbus_error_is_set(&error) == TRUE) {
482                         connman_error("%s", error.message);
483                         dbus_error_free(&error);
484                 } else
485                         connman_error("Wrong arguments for network");
486                 dbus_message_unref(reply);
487                 return -EIO;
488         }
489
490         DBG("path %s", path);
491
492         task->netpath = g_strdup(path);
493
494         dbus_message_unref(reply);
495
496         return 0;
497 }
498
499 static int remove_network(struct supplicant_task *task)
500 {
501         DBusMessage *message, *reply;
502         DBusError error;
503
504         DBG("task %p", task);
505
506         if (task->netpath == NULL)
507                 return -EINVAL;
508
509         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
510                                 SUPPLICANT_INTF ".Interface", "removeNetwork");
511         if (message == NULL)
512                 return -ENOMEM;
513
514         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
515                                                         DBUS_TYPE_INVALID);
516
517         dbus_error_init(&error);
518
519         reply = dbus_connection_send_with_reply_and_block(connection,
520                                                         message, -1, &error);
521         if (reply == NULL) {
522                 if (dbus_error_is_set(&error) == TRUE) {
523                         connman_error("%s", error.message);
524                         dbus_error_free(&error);
525                 } else
526                         connman_error("Failed to remove network");
527                 dbus_message_unref(message);
528                 return -EIO;
529         }
530
531         dbus_message_unref(message);
532
533         dbus_message_unref(reply);
534
535         g_free(task->netpath);
536         task->netpath = NULL;
537
538         return 0;
539 }
540
541 static int select_network(struct supplicant_task *task)
542 {
543         DBusMessage *message, *reply;
544         DBusError error;
545
546         DBG("task %p", task);
547
548         if (task->netpath == NULL)
549                 return -EINVAL;
550
551         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
552                                 SUPPLICANT_INTF ".Interface", "selectNetwork");
553         if (message == NULL)
554                 return -ENOMEM;
555
556         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->netpath,
557                                                         DBUS_TYPE_INVALID);
558
559         dbus_error_init(&error);
560
561         reply = dbus_connection_send_with_reply_and_block(connection,
562                                                         message, -1, &error);
563         if (reply == NULL) {
564                 if (dbus_error_is_set(&error) == TRUE) {
565                         connman_error("%s", error.message);
566                         dbus_error_free(&error);
567                 } else
568                         connman_error("Failed to select network");
569                 dbus_message_unref(message);
570                 return -EIO;
571         }
572
573         dbus_message_unref(message);
574
575         dbus_message_unref(reply);
576
577         return 0;
578 }
579
580 static int enable_network(struct supplicant_task *task)
581 {
582         DBusMessage *message, *reply;
583         DBusError error;
584
585         DBG("task %p", task);
586
587         if (task->netpath == NULL)
588                 return -EINVAL;
589
590         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
591                                         SUPPLICANT_INTF ".Network", "enable");
592         if (message == NULL)
593                 return -ENOMEM;
594
595         dbus_error_init(&error);
596
597         reply = dbus_connection_send_with_reply_and_block(connection,
598                                                         message, -1, &error);
599         if (reply == NULL) {
600                 if (dbus_error_is_set(&error) == TRUE) {
601                         connman_error("%s", error.message);
602                         dbus_error_free(&error);
603                 } else
604                         connman_error("Failed to enable network");
605                 dbus_message_unref(message);
606                 return -EIO;
607         }
608
609         dbus_message_unref(message);
610
611         dbus_message_unref(reply);
612
613         return 0;
614 }
615
616 static int disable_network(struct supplicant_task *task)
617 {
618         DBusMessage *message, *reply;
619         DBusError error;
620
621         DBG("task %p", task);
622
623         if (task->netpath == NULL)
624                 return -EINVAL;
625
626         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
627                                         SUPPLICANT_INTF ".Network", "disable");
628         if (message == NULL)
629                 return -ENOMEM;
630
631         dbus_error_init(&error);
632
633         reply = dbus_connection_send_with_reply_and_block(connection,
634                                                         message, -1, &error);
635         if (reply == NULL) {
636                 if (dbus_error_is_set(&error) == TRUE) {
637                         connman_error("%s", error.message);
638                         dbus_error_free(&error);
639                 } else
640                         connman_error("Failed to disable network");
641                 dbus_message_unref(message);
642                 return -EIO;
643         }
644
645         dbus_message_unref(message);
646
647         dbus_message_unref(reply);
648
649         return 0;
650 }
651
652 static int set_network(struct supplicant_task *task,
653                                 const unsigned char *network, int len,
654                                 const char *security, const char *passphrase)
655 {
656         DBusMessage *message, *reply;
657         DBusMessageIter array, dict;
658         DBusError error;
659
660         DBG("task %p", task);
661
662         if (task->netpath == NULL)
663                 return -EINVAL;
664
665         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->netpath,
666                                         SUPPLICANT_INTF ".Network", "set");
667         if (message == NULL)
668                 return -ENOMEM;
669
670         dbus_message_iter_init_append(message, &array);
671
672         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
673                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
674                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
675                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
676
677         connman_dbus_dict_append_array(&dict, "ssid",
678                                         DBUS_TYPE_BYTE, &network, len);
679
680         if (g_ascii_strcasecmp(security, "wpa") == 0 ||
681                                 g_ascii_strcasecmp(security, "wpa2") == 0) {
682                 const char *key_mgmt = "WPA-PSK";
683                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
684                                                 DBUS_TYPE_STRING, &key_mgmt);
685
686                 if (passphrase && strlen(passphrase) > 0)
687                         connman_dbus_dict_append_variant(&dict, "psk",
688                                                 DBUS_TYPE_STRING, &passphrase);
689         } else if (g_ascii_strcasecmp(security, "wep") == 0) {
690                 const char *key_mgmt = "NONE", *index = "0";
691                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
692                                                 DBUS_TYPE_STRING, &key_mgmt);
693
694                 if (passphrase) {
695                         int size = strlen(passphrase);
696                         if (size == 10 || size == 26) {
697                                 unsigned char *key = malloc(13);
698                                 char tmp[3];
699                                 int i;
700                                 memset(tmp, 0, sizeof(tmp));
701                                 if (key == NULL)
702                                         size = 0;
703                                 for (i = 0; i < size / 2; i++) {
704                                         memcpy(tmp, passphrase + (i * 2), 2);
705                                         key[i] = (unsigned char) strtol(tmp,
706                                                                 NULL, 16);
707                                 }
708                                 connman_dbus_dict_append_array(&dict,
709                                                 "wep_key0", DBUS_TYPE_BYTE,
710                                                         &key, size / 2);
711                                 free(key);
712                         } else
713                                 connman_dbus_dict_append_variant(&dict,
714                                                 "wep_key0", DBUS_TYPE_STRING,
715                                                                 &passphrase);
716                         connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
717                                                 DBUS_TYPE_STRING, &index);
718                 }
719         } else {
720                 const char *key_mgmt = "NONE";
721                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
722                                                 DBUS_TYPE_STRING, &key_mgmt);
723         }
724
725         dbus_message_iter_close_container(&array, &dict);
726
727         dbus_error_init(&error);
728
729         reply = dbus_connection_send_with_reply_and_block(connection,
730                                                         message, -1, &error);
731         if (reply == NULL) {
732                 if (dbus_error_is_set(&error) == TRUE) {
733                         connman_error("%s", error.message);
734                         dbus_error_free(&error);
735                 } else
736                         connman_error("Failed to set network options");
737                 dbus_message_unref(message);
738                 return -EIO;
739         }
740
741         dbus_message_unref(message);
742
743         dbus_message_unref(reply);
744
745         return 0;
746 }
747
748 static int initiate_scan(struct supplicant_task *task)
749 {
750         DBusMessage *message;
751         DBusPendingCall *call;
752
753         DBG("task %p", task);
754
755         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
756                                         SUPPLICANT_INTF ".Interface", "scan");
757         if (message == NULL)
758                 return -ENOMEM;
759
760         if (dbus_connection_send_with_reply(connection, message,
761                                                 &call, TIMEOUT) == FALSE) {
762                 connman_error("Failed to initiate scan");
763                 dbus_message_unref(message);
764                 return -EIO;
765         }
766
767         dbus_message_unref(message);
768
769         return 0;
770 }
771
772 static void extract_ssid(DBusMessageIter *value,
773                                         struct supplicant_result *result)
774 {
775         DBusMessageIter array;
776         unsigned char *ssid;
777         int ssid_len;
778
779         dbus_message_iter_recurse(value, &array);
780         dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
781
782         if (ssid_len < 1)
783                 return;
784
785         result->ssid = g_try_malloc(ssid_len);
786         if (result->ssid == NULL)
787                 return;
788
789         memcpy(result->ssid, ssid, ssid_len);
790         result->ssid_len = ssid_len;
791
792         result->identifier = g_try_malloc0(ssid_len + 1);
793         if (result->identifier == NULL)
794                 return;
795
796         memcpy(result->identifier, ssid, ssid_len);
797 }
798
799 static void extract_wpaie(DBusMessageIter *value,
800                                         struct supplicant_result *result)
801 {
802         DBusMessageIter array;
803         unsigned char *ie;
804         int ie_len;
805
806         dbus_message_iter_recurse(value, &array);
807         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
808
809         if (ie_len > 0)
810                 result->has_wpa = TRUE;
811 }
812
813 static void extract_rsnie(DBusMessageIter *value,
814                                         struct supplicant_result *result)
815 {
816         DBusMessageIter array;
817         unsigned char *ie;
818         int ie_len;
819
820         dbus_message_iter_recurse(value, &array);
821         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
822
823         if (ie_len > 0)
824                 result->has_rsn = TRUE;
825 }
826
827 static void extract_capabilites(DBusMessageIter *value,
828                                         struct supplicant_result *result)
829 {
830         dbus_message_iter_get_basic(value, &result->capabilities);
831
832         if (result->capabilities & IEEE80211_CAP_ESS)
833                 result->adhoc = FALSE;
834         else if (result->capabilities & IEEE80211_CAP_IBSS)
835                 result->adhoc = TRUE;
836
837         if (result->capabilities & IEEE80211_CAP_PRIVACY)
838                 result->has_wep = TRUE;
839 }
840
841 static void get_properties(struct supplicant_task *task);
842
843 static void properties_reply(DBusPendingCall *call, void *user_data)
844 {
845         struct supplicant_task *task = user_data;
846         struct supplicant_result result;
847         struct connman_network *network;
848         DBusMessage *reply;
849         DBusMessageIter array, dict;
850         char *security;
851         unsigned char strength;
852
853         DBG("task %p", task);
854
855         reply = dbus_pending_call_steal_reply(call);
856         if (reply == NULL) {
857                 get_properties(task);
858                 return;
859         }
860
861         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
862                 dbus_message_unref(reply);
863                 get_properties(task);
864                 return;
865         }
866
867         memset(&result, 0, sizeof(result));
868
869         dbus_message_iter_init(reply, &array);
870
871         dbus_message_iter_recurse(&array, &dict);
872
873         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
874                 DBusMessageIter entry, value;
875                 const char *key;
876
877                 dbus_message_iter_recurse(&dict, &entry);
878                 dbus_message_iter_get_basic(&entry, &key);
879
880                 dbus_message_iter_next(&entry);
881
882                 dbus_message_iter_recurse(&entry, &value);
883
884                 //type = dbus_message_iter_get_arg_type(&value);
885                 //dbus_message_iter_get_basic(&value, &val);
886
887                 /* 
888                  * bssid        : a (97)
889                  * ssid         : a (97)
890                  * wpaie        : a (97)
891                  * rsnie        : a (97)
892                  * frequency    : i (105)
893                  * capabilities : q (113)
894                  * quality      : i (105)
895                  * noise        : i (105)
896                  * level        : i (105)
897                  * maxrate      : i (105)
898                  */
899
900                 if (g_str_equal(key, "ssid") == TRUE)
901                         extract_ssid(&value, &result);
902                 else if (g_str_equal(key, "wpaie") == TRUE)
903                         extract_wpaie(&value, &result);
904                 else if (g_str_equal(key, "rsnie") == TRUE)
905                         extract_rsnie(&value, &result);
906                 else if (g_str_equal(key, "capabilities") == TRUE)
907                         extract_capabilites(&value, &result);
908                 else if (g_str_equal(key, "quality") == TRUE)
909                         dbus_message_iter_get_basic(&value, &result.quality);
910                 else if (g_str_equal(key, "noise") == TRUE)
911                         dbus_message_iter_get_basic(&value, &result.noise);
912                 else if (g_str_equal(key, "level") == TRUE)
913                         dbus_message_iter_get_basic(&value, &result.level);
914                 else if (g_str_equal(key, "maxrate") == TRUE)
915                         dbus_message_iter_get_basic(&value, &result.maxrate);
916
917                 dbus_message_iter_next(&dict);
918         }
919
920         if (result.identifier == NULL)
921                 goto done;
922
923         if (result.identifier[0] == '\0')
924                 goto done;
925
926         strength = result.quality;
927
928         if (result.has_rsn == TRUE)
929                 security = "wpa2";
930         else if (result.has_wpa == TRUE)
931                 security = "wpa";
932         else if (result.has_wep == TRUE)
933                 security = "wep";
934         else
935                 security = "none";
936
937         network = connman_device_get_network(task->device, result.identifier);
938         if (network == NULL) {
939                 const char *mode;
940                 int index;
941
942                 network = connman_network_create(result.identifier,
943                                                 CONNMAN_NETWORK_TYPE_WIFI);
944                 if (network == NULL)
945                         goto done;
946
947                 index = connman_device_get_index(task->device);
948                 connman_network_set_index(network, index);
949
950                 connman_network_set_protocol(network,
951                                                 CONNMAN_NETWORK_PROTOCOL_IP);
952
953                 connman_network_set_string(network, "Name", result.identifier);
954
955                 connman_network_set_blob(network, "WiFi.SSID",
956                                                 result.ssid, result.ssid_len);
957
958                 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
959                 connman_network_set_string(network, "WiFi.Mode", mode);
960
961                 DBG("%s (%s %s) strength %d", result.identifier, mode,
962                                                         security, strength);
963
964                 if (connman_device_add_network(task->device, network) < 0) {
965                         connman_network_unref(network);
966                         goto done;
967                 }
968         }
969
970         connman_network_set_available(network, TRUE);
971         connman_network_set_uint8(network, "Strength", strength);
972
973         connman_network_set_string(network, "WiFi.Security", security);
974
975 done:
976         g_free(result.identifier);
977         g_free(result.ssid);
978
979         dbus_message_unref(reply);
980
981         get_properties(task);
982 }
983
984 static void get_properties(struct supplicant_task *task)
985 {
986         DBusMessage *message;
987         DBusPendingCall *call;
988         char *path;
989
990         path = g_slist_nth_data(task->scan_results, 0);
991         if (path == NULL)
992                 goto noscan;
993
994         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
995                                                 SUPPLICANT_INTF ".BSSID",
996                                                                 "properties");
997
998         task->scan_results = g_slist_remove(task->scan_results, path);
999         g_free(path);
1000
1001         if (message == NULL)
1002                 goto noscan;
1003
1004         if (dbus_connection_send_with_reply(connection, message,
1005                                                 &call, TIMEOUT) == FALSE) {
1006                 connman_error("Failed to get network properties");
1007                 dbus_message_unref(message);
1008                 goto noscan;
1009         }
1010
1011         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1012
1013         dbus_message_unref(message);
1014
1015         return;
1016
1017 noscan:
1018         if (task->noscan == FALSE)
1019                 connman_device_set_scanning(task->device, FALSE);
1020 }
1021
1022 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1023 {
1024         struct supplicant_task *task = user_data;
1025         DBusMessage *reply;
1026         DBusError error;
1027         char **results;
1028         int i, num_results;
1029
1030         DBG("task %p", task);
1031
1032         reply = dbus_pending_call_steal_reply(call);
1033         if (reply == NULL)
1034                 goto noscan;
1035
1036         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1037                 goto done;
1038
1039         dbus_error_init(&error);
1040
1041         if (dbus_message_get_args(reply, &error,
1042                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1043                                                 &results, &num_results,
1044                                                 DBUS_TYPE_INVALID) == FALSE) {
1045                 if (dbus_error_is_set(&error) == TRUE) {
1046                         connman_error("%s", error.message);
1047                         dbus_error_free(&error);
1048                 } else
1049                         connman_error("Wrong arguments for scan result");
1050                 goto done;
1051         }
1052
1053         if (num_results == 0)
1054                 goto done;
1055
1056         for (i = 0; i < num_results; i++) {
1057                 char *path = g_strdup(results[i]);
1058                 if (path == NULL)
1059                         continue;
1060
1061                 task->scan_results = g_slist_append(task->scan_results, path);
1062         }
1063
1064         g_strfreev(results);
1065
1066         dbus_message_unref(reply);
1067
1068         get_properties(task);
1069
1070         return;
1071
1072 done:
1073         dbus_message_unref(reply);
1074
1075 noscan:
1076         if (task->noscan == FALSE)
1077                 connman_device_set_scanning(task->device, FALSE);
1078 }
1079
1080 static void scan_results_available(struct supplicant_task *task)
1081 {
1082         DBusMessage *message;
1083         DBusPendingCall *call;
1084
1085         DBG("task %p", task);
1086
1087         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1088                                                 SUPPLICANT_INTF ".Interface",
1089                                                         "scanResults");
1090         if (message == NULL)
1091                 return;
1092
1093         if (dbus_connection_send_with_reply(connection, message,
1094                                                 &call, TIMEOUT) == FALSE) {
1095                 connman_error("Failed to request scan result");
1096                 goto done;
1097         }
1098
1099         if (task->noscan == FALSE)
1100                 connman_device_set_scanning(task->device, TRUE);
1101
1102         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1103
1104 done:
1105         dbus_message_unref(message);
1106 }
1107
1108 static enum supplicant_state string2state(const char *state)
1109 {
1110         if (g_str_equal(state, "INACTIVE") == TRUE)
1111                 return WPA_INACTIVE;
1112         else if (g_str_equal(state, "SCANNING") == TRUE)
1113                 return WPA_SCANNING;
1114         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1115                 return WPA_ASSOCIATING;
1116         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1117                 return WPA_ASSOCIATED;
1118         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1119                 return WPA_GROUP_HANDSHAKE;
1120         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1121                 return WPA_4WAY_HANDSHAKE;
1122         else if (g_str_equal(state, "COMPLETED") == TRUE)
1123                 return WPA_COMPLETED;
1124         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1125                 return WPA_DISCONNECTED;
1126         else
1127                 return WPA_INVALID;
1128 }
1129
1130 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1131 {
1132         DBusError error;
1133         const char *newstate, *oldstate;
1134         enum supplicant_state state;
1135
1136         dbus_error_init(&error);
1137
1138         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1139                                                 DBUS_TYPE_STRING, &oldstate,
1140                                                 DBUS_TYPE_INVALID) == FALSE) {
1141                 if (dbus_error_is_set(&error) == TRUE) {
1142                         connman_error("%s", error.message);
1143                         dbus_error_free(&error);
1144                 } else
1145                         connman_error("Wrong arguments for state change");
1146                 return;
1147         }
1148
1149         DBG("state %s ==> %s", oldstate, newstate);
1150
1151         state = string2state(newstate);
1152         if (state == WPA_INVALID)
1153                 return;
1154
1155         task->state = state;
1156
1157         switch (task->state) {
1158         case WPA_SCANNING:
1159                 task->noscan = TRUE;
1160                 connman_device_set_scanning(task->device, TRUE);
1161                 break;
1162         case WPA_ASSOCIATING:
1163         case WPA_ASSOCIATED:
1164         case WPA_4WAY_HANDSHAKE:
1165         case WPA_GROUP_HANDSHAKE:
1166                 task->noscan = TRUE;
1167                 break;
1168         case WPA_COMPLETED:
1169         case WPA_DISCONNECTED:
1170                 task->noscan = FALSE;
1171                 break;
1172         case WPA_INACTIVE:
1173                 task->noscan = FALSE;
1174                 connman_device_set_scanning(task->device, FALSE);
1175                 break;
1176         case WPA_INVALID:
1177                 break;
1178         }
1179
1180         if (task->network == NULL)
1181                 return;
1182
1183         switch (task->state) {
1184         case WPA_COMPLETED:
1185                 /* carrier on */
1186                 connman_network_set_connected(task->network, TRUE);
1187                 connman_device_set_scanning(task->device, FALSE);
1188                 break;
1189         case WPA_DISCONNECTED:
1190                 /* carrier off */
1191                 connman_network_set_connected(task->network, FALSE);
1192                 connman_device_set_scanning(task->device, FALSE);
1193                 break;
1194         default:
1195                 break;
1196         }
1197 }
1198
1199 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1200                                                 DBusMessage *msg, void *data)
1201 {
1202         struct supplicant_task *task;
1203         const char *member, *path;
1204
1205         if (dbus_message_has_interface(msg,
1206                                 SUPPLICANT_INTF ".Interface") == FALSE)
1207                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1208
1209         member = dbus_message_get_member(msg);
1210         if (member == NULL)
1211                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1212
1213         path = dbus_message_get_path(msg);
1214         if (path == NULL)
1215                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1216
1217         task = find_task_by_path(path);
1218         if (task == NULL)
1219                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1220
1221         DBG("task %p member %s", task, member);
1222
1223         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1224                 scan_results_available(task);
1225         else if (g_str_equal(member, "StateChange") == TRUE)
1226                 state_change(task, msg);
1227
1228         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1229 }
1230
1231 int supplicant_start(struct connman_device *device)
1232 {
1233         struct supplicant_task *task;
1234
1235         DBG("device %p", device);
1236
1237         task = g_try_new0(struct supplicant_task, 1);
1238         if (task == NULL)
1239                 return -ENOMEM;
1240
1241         task->ifindex = connman_device_get_index(device);
1242         task->ifname = inet_index2name(task->ifindex);
1243
1244         if (task->ifname == NULL) {
1245                 g_free(task);
1246                 return -ENOMEM;
1247         }
1248
1249         task->device = connman_device_ref(device);
1250
1251         task->created = FALSE;
1252         task->noscan = FALSE;
1253         task->state = WPA_INVALID;
1254
1255         task_list = g_slist_append(task_list, task);
1256
1257         return create_interface(task);
1258 }
1259
1260 int supplicant_stop(struct connman_device *device)
1261 {
1262         int index = connman_device_get_index(device);
1263         struct supplicant_task *task;
1264
1265         DBG("device %p", device);
1266
1267         task = find_task_by_index(index);
1268         if (task == NULL)
1269                 return -ENODEV;
1270
1271         task_list = g_slist_remove(task_list, task);
1272
1273         disable_network(task);
1274
1275         remove_network(task);
1276
1277         return remove_interface(task);
1278 }
1279
1280 int supplicant_scan(struct connman_device *device)
1281 {
1282         int index = connman_device_get_index(device);
1283         struct supplicant_task *task;
1284         int err;
1285
1286         DBG("device %p", device);
1287
1288         task = find_task_by_index(index);
1289         if (task == NULL)
1290                 return -ENODEV;
1291
1292         switch (task->state) {
1293         case WPA_SCANNING:
1294                 return -EALREADY;
1295         case WPA_ASSOCIATING:
1296         case WPA_ASSOCIATED:
1297         case WPA_4WAY_HANDSHAKE:
1298         case WPA_GROUP_HANDSHAKE:
1299                 return -EBUSY;
1300         default:
1301                 break;
1302         }
1303
1304         err = initiate_scan(task);
1305
1306         return 0;
1307 }
1308
1309 int supplicant_connect(struct connman_network *network)
1310 {
1311         struct supplicant_task *task;
1312         const char *security, *passphrase;
1313         const void *ssid;
1314         unsigned int ssid_len;
1315         int index;
1316
1317         DBG("network %p", network);
1318
1319         security = connman_network_get_string(network, "WiFi.Security");
1320         passphrase = connman_network_get_string(network, "WiFi.Passphrase");
1321
1322         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
1323
1324         DBG("security %s passphrase %s", security, passphrase);
1325
1326         if (security == NULL && passphrase == NULL)
1327                 return -EINVAL;
1328
1329         if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1330                 return -EINVAL;
1331
1332         index = connman_network_get_index(network);
1333
1334         task = find_task_by_index(index);
1335         if (task == NULL)
1336                 return -ENODEV;
1337
1338         task->network = connman_network_ref(network);
1339
1340         add_network(task);
1341
1342         select_network(task);
1343         disable_network(task);
1344
1345         set_network(task, ssid, ssid_len, security, passphrase);
1346
1347         enable_network(task);
1348
1349         return 0;
1350 }
1351
1352 int supplicant_disconnect(struct connman_network *network)
1353 {
1354         struct supplicant_task *task;
1355         int index;
1356
1357         DBG("network %p", network);
1358
1359         index = connman_network_get_index(network);
1360
1361         task = find_task_by_index(index);
1362         if (task == NULL)
1363                 return -ENODEV;
1364
1365         disable_network(task);
1366
1367         remove_network(task);
1368
1369         connman_network_set_connected(task->network, FALSE);
1370
1371         connman_network_unref(task->network);
1372
1373         return 0;
1374 }
1375
1376 static void supplicant_activate(DBusConnection *conn)
1377 {
1378         DBusMessage *message;
1379
1380         DBG("conn %p", conn);
1381
1382         message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1383                                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1384         if (message == NULL)
1385                 return;
1386
1387         dbus_message_set_no_reply(message, TRUE);
1388
1389         dbus_connection_send(conn, message, NULL);
1390
1391         dbus_message_unref(message);
1392 }
1393
1394 static GSList *driver_list = NULL;
1395
1396 static void supplicant_probe(DBusConnection *conn, void *user_data)
1397 {
1398         GSList *list;
1399
1400         DBG("conn %p", conn);
1401
1402         for (list = driver_list; list; list = list->next) {
1403                 struct supplicant_driver *driver = list->data;
1404
1405                 DBG("driver %p name %s", driver, driver->name);
1406
1407                 if (driver->probe)
1408                         driver->probe();
1409         }
1410 }
1411
1412 static void supplicant_remove(DBusConnection *conn, void *user_data)
1413 {
1414         GSList *list;
1415
1416         DBG("conn %p", conn);
1417
1418         for (list = driver_list; list; list = list->next) {
1419                 struct supplicant_driver *driver = list->data;
1420
1421                 DBG("driver %p name %s", driver, driver->name);
1422
1423                 if (driver->remove)
1424                         driver->remove();
1425         }
1426 }
1427
1428 static const char *supplicant_rule = "type=signal,"
1429                                 "interface=" SUPPLICANT_INTF ".Interface";
1430 static guint watch;
1431
1432 static int supplicant_create(void)
1433 {
1434         if (g_slist_length(driver_list) > 0)
1435                 return 0;
1436
1437         connection = connman_dbus_get_connection();
1438         if (connection == NULL)
1439                 return -EIO;
1440
1441         DBG("connection %p", connection);
1442
1443         if (dbus_connection_add_filter(connection,
1444                                 supplicant_filter, NULL, NULL) == FALSE) {
1445                 connection = connman_dbus_get_connection();
1446                 return -EIO;
1447         }
1448
1449         dbus_bus_add_match(connection, supplicant_rule, NULL);
1450         dbus_connection_flush(connection);
1451
1452         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1453                         supplicant_probe, supplicant_remove, NULL, NULL);
1454
1455         return 0;
1456 }
1457
1458 static void supplicant_destroy(void)
1459 {
1460         if (g_slist_length(driver_list) > 0)
1461                 return;
1462
1463         DBG("connection %p", connection);
1464
1465         if (watch > 0)
1466                 g_dbus_remove_watch(connection, watch);
1467
1468         dbus_bus_remove_match(connection, supplicant_rule, NULL);
1469         dbus_connection_flush(connection);
1470
1471         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1472
1473         dbus_connection_unref(connection);
1474         connection = NULL;
1475 }
1476
1477 int supplicant_register(struct supplicant_driver *driver)
1478 {
1479         int err;
1480
1481         DBG("driver %p name %s", driver, driver->name);
1482
1483         err = supplicant_create();
1484         if (err < 0)
1485                 return err;
1486
1487         driver_list = g_slist_append(driver_list, driver);
1488
1489         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1490                 supplicant_probe(connection, NULL);
1491         else
1492                 supplicant_activate(connection);
1493
1494         return 0;
1495 }
1496
1497 void supplicant_unregister(struct supplicant_driver *driver)
1498 {
1499         DBG("driver %p name %s", driver, driver->name);
1500
1501         supplicant_remove(connection, NULL);
1502
1503         driver_list = g_slist_remove(driver_list, driver);
1504
1505         supplicant_destroy();
1506 }