Convert identifier on-demand if needed
[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;
849         unsigned char strength;
850
851         DBG("task %p", task);
852
853         reply = dbus_pending_call_steal_reply(call);
854         if (reply == NULL) {
855                 get_properties(task);
856                 return;
857         }
858
859         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR) {
860                 dbus_message_unref(reply);
861                 get_properties(task);
862                 return;
863         }
864
865         memset(&result, 0, sizeof(result));
866
867         dbus_message_iter_init(reply, &array);
868
869         dbus_message_iter_recurse(&array, &dict);
870
871         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
872                 DBusMessageIter entry, value;
873                 const char *key;
874
875                 dbus_message_iter_recurse(&dict, &entry);
876                 dbus_message_iter_get_basic(&entry, &key);
877
878                 dbus_message_iter_next(&entry);
879
880                 dbus_message_iter_recurse(&entry, &value);
881
882                 //type = dbus_message_iter_get_arg_type(&value);
883                 //dbus_message_iter_get_basic(&value, &val);
884
885                 /* 
886                  * bssid        : a (97)
887                  * ssid         : a (97)
888                  * wpaie        : a (97)
889                  * rsnie        : a (97)
890                  * frequency    : i (105)
891                  * capabilities : q (113)
892                  * quality      : i (105)
893                  * noise        : i (105)
894                  * level        : i (105)
895                  * maxrate      : i (105)
896                  */
897
898                 if (g_str_equal(key, "ssid") == TRUE)
899                         extract_ssid(&value, &result);
900                 else if (g_str_equal(key, "wpaie") == TRUE)
901                         extract_wpaie(&value, &result);
902                 else if (g_str_equal(key, "rsnie") == TRUE)
903                         extract_rsnie(&value, &result);
904                 else if (g_str_equal(key, "capabilities") == TRUE)
905                         extract_capabilites(&value, &result);
906                 else if (g_str_equal(key, "quality") == TRUE)
907                         dbus_message_iter_get_basic(&value, &result.quality);
908                 else if (g_str_equal(key, "noise") == TRUE)
909                         dbus_message_iter_get_basic(&value, &result.noise);
910                 else if (g_str_equal(key, "level") == TRUE)
911                         dbus_message_iter_get_basic(&value, &result.level);
912                 else if (g_str_equal(key, "maxrate") == TRUE)
913                         dbus_message_iter_get_basic(&value, &result.maxrate);
914
915                 dbus_message_iter_next(&dict);
916         }
917
918         if (result.identifier == NULL)
919                 goto done;
920
921         if (result.identifier[0] == '\0')
922                 goto done;
923
924         strength = result.quality;
925
926         if (result.has_rsn == TRUE)
927                 security = "wpa2";
928         else if (result.has_wpa == TRUE)
929                 security = "wpa";
930         else if (result.has_wep == TRUE)
931                 security = "wep";
932         else
933                 security = "none";
934
935         network = connman_device_get_network(task->device, result.identifier);
936         if (network == NULL) {
937                 const char *mode;
938                 int index;
939
940                 network = connman_network_create(result.identifier,
941                                                 CONNMAN_NETWORK_TYPE_WIFI);
942                 if (network == NULL)
943                         goto done;
944
945                 index = connman_device_get_index(task->device);
946                 connman_network_set_index(network, index);
947
948                 connman_network_set_protocol(network,
949                                                 CONNMAN_NETWORK_PROTOCOL_IP);
950
951                 connman_network_set_string(network, "Name", result.identifier);
952
953                 connman_network_set_blob(network, "WiFi.SSID",
954                                                 result.ssid, result.ssid_len);
955
956                 mode = (result.adhoc == TRUE) ? "adhoc" : "managed";
957                 connman_network_set_string(network, "WiFi.Mode", mode);
958
959                 DBG("%s (%s %s) strength %d", result.identifier, mode,
960                                                         security, strength);
961
962                 if (connman_device_add_network(task->device, network) < 0) {
963                         connman_network_unref(network);
964                         goto done;
965                 }
966         }
967
968         connman_network_set_available(network, TRUE);
969         connman_network_set_uint8(network, "Strength", strength);
970
971         connman_network_set_string(network, "WiFi.Security", security);
972
973 done:
974         g_free(result.identifier);
975         g_free(result.ssid);
976
977         dbus_message_unref(reply);
978
979         get_properties(task);
980 }
981
982 static void get_properties(struct supplicant_task *task)
983 {
984         DBusMessage *message;
985         DBusPendingCall *call;
986         char *path;
987
988         path = g_slist_nth_data(task->scan_results, 0);
989         if (path == NULL)
990                 goto noscan;
991
992         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
993                                                 SUPPLICANT_INTF ".BSSID",
994                                                                 "properties");
995
996         task->scan_results = g_slist_remove(task->scan_results, path);
997         g_free(path);
998
999         if (message == NULL)
1000                 goto noscan;
1001
1002         if (dbus_connection_send_with_reply(connection, message,
1003                                                 &call, TIMEOUT) == FALSE) {
1004                 connman_error("Failed to get network properties");
1005                 dbus_message_unref(message);
1006                 goto noscan;
1007         }
1008
1009         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
1010
1011         dbus_message_unref(message);
1012
1013         return;
1014
1015 noscan:
1016         if (task->noscan == FALSE)
1017                 connman_device_set_scanning(task->device, FALSE);
1018 }
1019
1020 static void scan_results_reply(DBusPendingCall *call, void *user_data)
1021 {
1022         struct supplicant_task *task = user_data;
1023         DBusMessage *reply;
1024         DBusError error;
1025         char **results;
1026         int i, num_results;
1027
1028         DBG("task %p", task);
1029
1030         reply = dbus_pending_call_steal_reply(call);
1031         if (reply == NULL)
1032                 goto noscan;
1033
1034         if (dbus_message_get_type(reply) == DBUS_MESSAGE_TYPE_ERROR)
1035                 goto done;
1036
1037         dbus_error_init(&error);
1038
1039         if (dbus_message_get_args(reply, &error,
1040                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
1041                                                 &results, &num_results,
1042                                                 DBUS_TYPE_INVALID) == FALSE) {
1043                 if (dbus_error_is_set(&error) == TRUE) {
1044                         connman_error("%s", error.message);
1045                         dbus_error_free(&error);
1046                 } else
1047                         connman_error("Wrong arguments for scan result");
1048                 goto done;
1049         }
1050
1051         if (num_results == 0)
1052                 goto done;
1053
1054         for (i = 0; i < num_results; i++) {
1055                 char *path = g_strdup(results[i]);
1056                 if (path == NULL)
1057                         continue;
1058
1059                 task->scan_results = g_slist_append(task->scan_results, path);
1060         }
1061
1062         g_strfreev(results);
1063
1064         dbus_message_unref(reply);
1065
1066         get_properties(task);
1067
1068         return;
1069
1070 done:
1071         dbus_message_unref(reply);
1072
1073 noscan:
1074         if (task->noscan == FALSE)
1075                 connman_device_set_scanning(task->device, FALSE);
1076 }
1077
1078 static void scan_results_available(struct supplicant_task *task)
1079 {
1080         DBusMessage *message;
1081         DBusPendingCall *call;
1082
1083         DBG("task %p", task);
1084
1085         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
1086                                                 SUPPLICANT_INTF ".Interface",
1087                                                         "scanResults");
1088         if (message == NULL)
1089                 return;
1090
1091         if (dbus_connection_send_with_reply(connection, message,
1092                                                 &call, TIMEOUT) == FALSE) {
1093                 connman_error("Failed to request scan result");
1094                 goto done;
1095         }
1096
1097         if (task->noscan == FALSE)
1098                 connman_device_set_scanning(task->device, TRUE);
1099
1100         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
1101
1102 done:
1103         dbus_message_unref(message);
1104 }
1105
1106 static enum supplicant_state string2state(const char *state)
1107 {
1108         if (g_str_equal(state, "INACTIVE") == TRUE)
1109                 return WPA_INACTIVE;
1110         else if (g_str_equal(state, "SCANNING") == TRUE)
1111                 return WPA_SCANNING;
1112         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
1113                 return WPA_ASSOCIATING;
1114         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
1115                 return WPA_ASSOCIATED;
1116         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
1117                 return WPA_GROUP_HANDSHAKE;
1118         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
1119                 return WPA_4WAY_HANDSHAKE;
1120         else if (g_str_equal(state, "COMPLETED") == TRUE)
1121                 return WPA_COMPLETED;
1122         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
1123                 return WPA_DISCONNECTED;
1124         else
1125                 return WPA_INVALID;
1126 }
1127
1128 static void state_change(struct supplicant_task *task, DBusMessage *msg)
1129 {
1130         DBusError error;
1131         const char *newstate, *oldstate;
1132         enum supplicant_state state;
1133
1134         dbus_error_init(&error);
1135
1136         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &newstate,
1137                                                 DBUS_TYPE_STRING, &oldstate,
1138                                                 DBUS_TYPE_INVALID) == FALSE) {
1139                 if (dbus_error_is_set(&error) == TRUE) {
1140                         connman_error("%s", error.message);
1141                         dbus_error_free(&error);
1142                 } else
1143                         connman_error("Wrong arguments for state change");
1144                 return;
1145         }
1146
1147         DBG("state %s ==> %s", oldstate, newstate);
1148
1149         state = string2state(newstate);
1150         if (state == WPA_INVALID)
1151                 return;
1152
1153         task->state = state;
1154
1155         switch (task->state) {
1156         case WPA_SCANNING:
1157                 task->noscan = TRUE;
1158                 connman_device_set_scanning(task->device, TRUE);
1159                 break;
1160         case WPA_ASSOCIATING:
1161         case WPA_ASSOCIATED:
1162         case WPA_4WAY_HANDSHAKE:
1163         case WPA_GROUP_HANDSHAKE:
1164                 task->noscan = TRUE;
1165                 break;
1166         case WPA_COMPLETED:
1167         case WPA_DISCONNECTED:
1168                 task->noscan = FALSE;
1169                 break;
1170         case WPA_INACTIVE:
1171                 task->noscan = FALSE;
1172                 connman_device_set_scanning(task->device, FALSE);
1173                 break;
1174         case WPA_INVALID:
1175                 break;
1176         }
1177
1178         if (task->network == NULL)
1179                 return;
1180
1181         switch (task->state) {
1182         case WPA_COMPLETED:
1183                 /* carrier on */
1184                 connman_network_set_connected(task->network, TRUE);
1185                 connman_device_set_scanning(task->device, FALSE);
1186                 break;
1187         case WPA_DISCONNECTED:
1188                 /* carrier off */
1189                 connman_network_set_connected(task->network, FALSE);
1190                 connman_device_set_scanning(task->device, FALSE);
1191                 break;
1192         default:
1193                 break;
1194         }
1195 }
1196
1197 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1198                                                 DBusMessage *msg, void *data)
1199 {
1200         struct supplicant_task *task;
1201         const char *member, *path;
1202
1203         if (dbus_message_has_interface(msg,
1204                                 SUPPLICANT_INTF ".Interface") == FALSE)
1205                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1206
1207         member = dbus_message_get_member(msg);
1208         if (member == NULL)
1209                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1210
1211         path = dbus_message_get_path(msg);
1212         if (path == NULL)
1213                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1214
1215         task = find_task_by_path(path);
1216         if (task == NULL)
1217                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1218
1219         DBG("task %p member %s", task, member);
1220
1221         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
1222                 scan_results_available(task);
1223         else if (g_str_equal(member, "StateChange") == TRUE)
1224                 state_change(task, msg);
1225
1226         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1227 }
1228
1229 int supplicant_start(struct connman_device *device)
1230 {
1231         struct supplicant_task *task;
1232
1233         DBG("device %p", device);
1234
1235         task = g_try_new0(struct supplicant_task, 1);
1236         if (task == NULL)
1237                 return -ENOMEM;
1238
1239         task->ifindex = connman_device_get_index(device);
1240         task->ifname = inet_index2name(task->ifindex);
1241         task->device = device;
1242
1243         if (task->ifname == NULL) {
1244                 g_free(task);
1245                 return -ENOMEM;
1246         }
1247
1248         task->created = FALSE;
1249         task->noscan = FALSE;
1250         task->state = WPA_INVALID;
1251
1252         task_list = g_slist_append(task_list, task);
1253
1254         return create_interface(task);
1255 }
1256
1257 int supplicant_stop(struct connman_device *device)
1258 {
1259         int index = connman_device_get_index(device);
1260         struct supplicant_task *task;
1261
1262         DBG("device %p", device);
1263
1264         task = find_task_by_index(index);
1265         if (task == NULL)
1266                 return -ENODEV;
1267
1268         task_list = g_slist_remove(task_list, task);
1269
1270         disable_network(task);
1271
1272         remove_network(task);
1273
1274         return remove_interface(task);
1275 }
1276
1277 int supplicant_scan(struct connman_device *device)
1278 {
1279         int index = connman_device_get_index(device);
1280         struct supplicant_task *task;
1281         int err;
1282
1283         DBG("device %p", device);
1284
1285         task = find_task_by_index(index);
1286         if (task == NULL)
1287                 return -ENODEV;
1288
1289         switch (task->state) {
1290         case WPA_SCANNING:
1291                 return -EALREADY;
1292         case WPA_ASSOCIATING:
1293         case WPA_ASSOCIATED:
1294         case WPA_4WAY_HANDSHAKE:
1295         case WPA_GROUP_HANDSHAKE:
1296                 return -EBUSY;
1297         default:
1298                 break;
1299         }
1300
1301         err = initiate_scan(task);
1302
1303         return 0;
1304 }
1305
1306 int supplicant_connect(struct connman_network *network)
1307 {
1308         struct supplicant_task *task;
1309         const char *security, *passphrase;
1310         const void *ssid;
1311         unsigned int ssid_len;
1312         int index;
1313
1314         DBG("network %p", network);
1315
1316         security = connman_network_get_string(network, "WiFi.Security");
1317         passphrase = connman_network_get_string(network, "WiFi.Passphrase");
1318
1319         ssid = connman_network_get_blob(network, "WiFi.SSID", &ssid_len);
1320
1321         DBG("security %s passphrase %s", security, passphrase);
1322
1323         if (security == NULL && passphrase == NULL)
1324                 return -EINVAL;
1325
1326         if (g_str_equal(security, "none") == FALSE && passphrase == NULL)
1327                 return -EINVAL;
1328
1329         index = connman_network_get_index(network);
1330
1331         task = find_task_by_index(index);
1332         if (task == NULL)
1333                 return -ENODEV;
1334
1335         task->network = connman_network_ref(network);
1336
1337         add_network(task);
1338
1339         select_network(task);
1340         disable_network(task);
1341
1342         set_network(task, ssid, ssid_len, security, passphrase);
1343
1344         enable_network(task);
1345
1346         return 0;
1347 }
1348
1349 int supplicant_disconnect(struct connman_network *network)
1350 {
1351         struct supplicant_task *task;
1352         int index;
1353
1354         DBG("network %p", network);
1355
1356         index = connman_network_get_index(network);
1357
1358         task = find_task_by_index(index);
1359         if (task == NULL)
1360                 return -ENODEV;
1361
1362         disable_network(task);
1363
1364         remove_network(task);
1365
1366         connman_network_set_connected(task->network, FALSE);
1367
1368         connman_network_unref(task->network);
1369
1370         return 0;
1371 }
1372
1373 static void supplicant_activate(DBusConnection *conn)
1374 {
1375         DBusMessage *message;
1376
1377         DBG("conn %p", conn);
1378
1379         message = dbus_message_new_method_call(SUPPLICANT_NAME, "/",
1380                                 DBUS_INTERFACE_INTROSPECTABLE, "Introspect");
1381         if (message == NULL)
1382                 return;
1383
1384         dbus_message_set_no_reply(message, TRUE);
1385
1386         dbus_connection_send(conn, message, NULL);
1387
1388         dbus_message_unref(message);
1389 }
1390
1391 static GSList *driver_list = NULL;
1392
1393 static void supplicant_probe(DBusConnection *conn, void *user_data)
1394 {
1395         GSList *list;
1396
1397         DBG("conn %p", conn);
1398
1399         for (list = driver_list; list; list = list->next) {
1400                 struct supplicant_driver *driver = list->data;
1401
1402                 DBG("driver %p name %s", driver, driver->name);
1403
1404                 if (driver->probe)
1405                         driver->probe();
1406         }
1407 }
1408
1409 static void supplicant_remove(DBusConnection *conn, void *user_data)
1410 {
1411         GSList *list;
1412
1413         DBG("conn %p", conn);
1414
1415         for (list = driver_list; list; list = list->next) {
1416                 struct supplicant_driver *driver = list->data;
1417
1418                 DBG("driver %p name %s", driver, driver->name);
1419
1420                 if (driver->remove)
1421                         driver->remove();
1422         }
1423 }
1424
1425 static const char *supplicant_rule = "type=signal,"
1426                                 "interface=" SUPPLICANT_INTF ".Interface";
1427 static guint watch;
1428
1429 static int supplicant_create(void)
1430 {
1431         if (g_slist_length(driver_list) > 0)
1432                 return 0;
1433
1434         connection = connman_dbus_get_connection();
1435         if (connection == NULL)
1436                 return -EIO;
1437
1438         DBG("connection %p", connection);
1439
1440         if (dbus_connection_add_filter(connection,
1441                                 supplicant_filter, NULL, NULL) == FALSE) {
1442                 connection = connman_dbus_get_connection();
1443                 return -EIO;
1444         }
1445
1446         dbus_bus_add_match(connection, supplicant_rule, NULL);
1447         dbus_connection_flush(connection);
1448
1449         watch = g_dbus_add_service_watch(connection, SUPPLICANT_NAME,
1450                         supplicant_probe, supplicant_remove, NULL, NULL);
1451
1452         return 0;
1453 }
1454
1455 static void supplicant_destroy(void)
1456 {
1457         if (g_slist_length(driver_list) > 0)
1458                 return;
1459
1460         DBG("connection %p", connection);
1461
1462         if (watch > 0)
1463                 g_dbus_remove_watch(connection, watch);
1464
1465         dbus_bus_remove_match(connection, supplicant_rule, NULL);
1466         dbus_connection_flush(connection);
1467
1468         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1469
1470         dbus_connection_unref(connection);
1471         connection = NULL;
1472 }
1473
1474 int supplicant_register(struct supplicant_driver *driver)
1475 {
1476         int err;
1477
1478         DBG("driver %p name %s", driver, driver->name);
1479
1480         err = supplicant_create();
1481         if (err < 0)
1482                 return err;
1483
1484         driver_list = g_slist_append(driver_list, driver);
1485
1486         if (g_dbus_check_service(connection, SUPPLICANT_NAME) == TRUE)
1487                 supplicant_probe(connection, NULL);
1488         else
1489                 supplicant_activate(connection);
1490
1491         return 0;
1492 }
1493
1494 void supplicant_unregister(struct supplicant_driver *driver)
1495 {
1496         DBG("driver %p name %s", driver, driver->name);
1497
1498         supplicant_remove(connection, NULL);
1499
1500         driver_list = g_slist_remove(driver_list, driver);
1501
1502         supplicant_destroy();
1503 }