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