Set the correct key management for WEP protected networks
[connman] / plugins / supplicant.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2008  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 <string.h>
27 #include <dbus/dbus.h>
28
29 #include <connman/log.h>
30 #include <connman/dbus.h>
31
32 #include "inet.h"
33 #include "supplicant.h"
34
35 #define TIMEOUT 5000
36
37 #define IEEE80211_CAP_ESS       0x0001
38 #define IEEE80211_CAP_IBSS      0x0002
39 #define IEEE80211_CAP_PRIVACY   0x0010
40
41 struct supplicant_task {
42         int ifindex;
43         gchar *ifname;
44         struct connman_element *element;
45         struct supplicant_callback *callback;
46         gchar *path;
47         gboolean created;
48         gchar *network;
49         enum supplicant_state state;
50 };
51
52 static GSList *task_list = NULL;
53
54 static DBusConnection *connection;
55
56 static struct supplicant_task *find_task_by_index(int index)
57 {
58         GSList *list;
59
60         for (list = task_list; list; list = list->next) {
61                 struct supplicant_task *task = list->data;
62
63                 if (task->ifindex == index)
64                         return task;
65         }
66
67         return NULL;
68 }
69
70 static struct supplicant_task *find_task_by_path(const char *path)
71 {
72         GSList *list;
73
74         for (list = task_list; list; list = list->next) {
75                 struct supplicant_task *task = list->data;
76
77                 if (g_str_equal(task->path, path) == TRUE)
78                         return task;
79         }
80
81         return NULL;
82 }
83
84 static int get_interface(struct supplicant_task *task)
85 {
86         DBusMessage *message, *reply;
87         DBusError error;
88         const char *path;
89
90         DBG("task %p", task);
91
92         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
93                                         SUPPLICANT_INTF, "getInterface");
94         if (message == NULL)
95                 return -ENOMEM;
96
97         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
98                                                         DBUS_TYPE_INVALID);
99
100         dbus_error_init(&error);
101
102         reply = dbus_connection_send_with_reply_and_block(connection,
103                                                         message, -1, &error);
104         if (reply == NULL) {
105                 if (dbus_error_is_set(&error) == TRUE) {
106                         connman_error("%s", error.message);
107                         dbus_error_free(&error);
108                 } else
109                         connman_error("Failed to get interface");
110                 dbus_message_unref(message);
111                 return -EIO;
112         }
113
114         dbus_message_unref(message);
115
116         dbus_error_init(&error);
117
118         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
119                                                 DBUS_TYPE_INVALID) == FALSE) {
120                 if (dbus_error_is_set(&error) == TRUE) {
121                         connman_error("%s", error.message);
122                         dbus_error_free(&error);
123                 } else
124                         connman_error("Wrong arguments for interface");
125                 dbus_message_unref(reply);
126                 return -EIO;
127         }
128
129         DBG("path %s", path);
130
131         task->path = g_strdup(path);
132         task->created = FALSE;
133
134         dbus_message_unref(reply);
135
136         return 0;
137 }
138
139 static int add_interface(struct supplicant_task *task)
140 {
141         DBusMessage *message, *reply;
142         DBusError error;
143         const char *path;
144
145         DBG("task %p", task);
146
147         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
148                                         SUPPLICANT_INTF, "addInterface");
149         if (message == NULL)
150                 return -ENOMEM;
151
152         dbus_error_init(&error);
153
154         dbus_message_append_args(message, DBUS_TYPE_STRING, &task->ifname,
155                                                         DBUS_TYPE_INVALID);
156
157         reply = dbus_connection_send_with_reply_and_block(connection,
158                                                         message, -1, &error);
159         if (reply == NULL) {
160                 if (dbus_error_is_set(&error) == TRUE) {
161                         connman_error("%s", error.message);
162                         dbus_error_free(&error);
163                 } else
164                         connman_error("Failed to add interface");
165                 dbus_message_unref(message);
166                 return -EIO;
167         }
168
169         dbus_message_unref(message);
170
171         dbus_error_init(&error);
172
173         if (dbus_message_get_args(reply, &error, DBUS_TYPE_OBJECT_PATH, &path,
174                                                 DBUS_TYPE_INVALID) == FALSE) {
175                 if (dbus_error_is_set(&error) == TRUE) {
176                         connman_error("%s", error.message);
177                         dbus_error_free(&error);
178                 } else
179                         connman_error("Wrong arguments for interface");
180                 dbus_message_unref(reply);
181                 return -EIO;
182         }
183
184         DBG("path %s", path);
185
186         task->path = g_strdup(path);
187         task->created = TRUE;
188
189         dbus_message_unref(reply);
190
191         return 0;
192 }
193
194 static int remove_interface(struct supplicant_task *task)
195 {
196         DBusMessage *message, *reply;
197         DBusError error;
198
199         DBG("task %p", task);
200
201         if (task->created == FALSE)
202                 return -EINVAL;
203
204         message = dbus_message_new_method_call(SUPPLICANT_NAME, SUPPLICANT_PATH,
205                                         SUPPLICANT_INTF, "removeInterface");
206         if (message == NULL)
207                 return -ENOMEM;
208
209         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->path,
210                                                         DBUS_TYPE_INVALID);
211
212         dbus_error_init(&error);
213
214         reply = dbus_connection_send_with_reply_and_block(connection,
215                                                         message, -1, &error);
216         if (reply == NULL) {
217                 if (dbus_error_is_set(&error) == TRUE) {
218                         connman_error("%s", error.message);
219                         dbus_error_free(&error);
220                 } else
221                         connman_error("Failed to remove interface");
222                 dbus_message_unref(message);
223                 return -EIO;
224         }
225
226         dbus_message_unref(message);
227
228         dbus_message_unref(reply);
229
230         return 0;
231 }
232
233 static int set_ap_scan(struct supplicant_task *task)
234 {
235         DBusMessage *message, *reply;
236         DBusError error;
237         guint32 ap_scan = 1;
238
239         DBG("task %p", task);
240
241         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
242                                 SUPPLICANT_INTF ".Interface", "setAPScan");
243         if (message == NULL)
244                 return -ENOMEM;
245
246         dbus_message_append_args(message, DBUS_TYPE_UINT32, &ap_scan,
247                                                         DBUS_TYPE_INVALID);
248
249         dbus_error_init(&error);
250
251         reply = dbus_connection_send_with_reply_and_block(connection,
252                                                         message, -1, &error);
253         if (reply == NULL) {
254                 if (dbus_error_is_set(&error) == TRUE) {
255                         connman_error("%s", error.message);
256                         dbus_error_free(&error);
257                 } else
258                         connman_error("Failed to set AP scan");
259                 dbus_message_unref(message);
260                 return -EIO;
261         }
262
263         dbus_message_unref(message);
264
265         dbus_message_unref(reply);
266
267         return 0;
268 }
269
270 static int add_network(struct supplicant_task *task)
271 {
272         DBusMessage *message, *reply;
273         DBusError error;
274         const char *path;
275
276         DBG("task %p", task);
277
278         if (task->network != NULL)
279                 return -EALREADY;
280
281         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
282                                 SUPPLICANT_INTF ".Interface", "addNetwork");
283         if (message == NULL)
284                 return -ENOMEM;
285
286         dbus_error_init(&error);
287
288         reply = dbus_connection_send_with_reply_and_block(connection,
289                                                         message, -1, &error);
290         if (reply == NULL) {
291                 if (dbus_error_is_set(&error) == TRUE) {
292                         connman_error("%s", error.message);
293                         dbus_error_free(&error);
294                 } else
295                         connman_error("Failed to add network");
296                 dbus_message_unref(message);
297                 return -EIO;
298         }
299
300         dbus_message_unref(message);
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 network");
311                 dbus_message_unref(reply);
312                 return -EIO;
313         }
314
315         DBG("path %s", path);
316
317         task->network = g_strdup(path);
318
319         dbus_message_unref(reply);
320
321         return 0;
322 }
323
324 static int remove_network(struct supplicant_task *task)
325 {
326         DBusMessage *message, *reply;
327         DBusError error;
328
329         DBG("task %p", task);
330
331         if (task->network == NULL)
332                 return -EINVAL;
333
334         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
335                                 SUPPLICANT_INTF ".Interface", "removeNetwork");
336         if (message == NULL)
337                 return -ENOMEM;
338
339         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->network,
340                                                         DBUS_TYPE_INVALID);
341
342         dbus_error_init(&error);
343
344         reply = dbus_connection_send_with_reply_and_block(connection,
345                                                         message, -1, &error);
346         if (reply == NULL) {
347                 if (dbus_error_is_set(&error) == TRUE) {
348                         connman_error("%s", error.message);
349                         dbus_error_free(&error);
350                 } else
351                         connman_error("Failed to remove network");
352                 dbus_message_unref(message);
353                 return -EIO;
354         }
355
356         dbus_message_unref(message);
357
358         dbus_message_unref(reply);
359
360         g_free(task->network);
361         task->network = NULL;
362
363         return 0;
364 }
365
366 static int select_network(struct supplicant_task *task)
367 {
368         DBusMessage *message, *reply;
369         DBusError error;
370
371         DBG("task %p", task);
372
373         if (task->network == NULL)
374                 return -EINVAL;
375
376         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
377                                 SUPPLICANT_INTF ".Interface", "selectNetwork");
378         if (message == NULL)
379                 return -ENOMEM;
380
381         dbus_message_append_args(message, DBUS_TYPE_OBJECT_PATH, &task->network,
382                                                         DBUS_TYPE_INVALID);
383
384         dbus_error_init(&error);
385
386         reply = dbus_connection_send_with_reply_and_block(connection,
387                                                         message, -1, &error);
388         if (reply == NULL) {
389                 if (dbus_error_is_set(&error) == TRUE) {
390                         connman_error("%s", error.message);
391                         dbus_error_free(&error);
392                 } else
393                         connman_error("Failed to select network");
394                 dbus_message_unref(message);
395                 return -EIO;
396         }
397
398         dbus_message_unref(message);
399
400         dbus_message_unref(reply);
401
402         return 0;
403 }
404
405 static int enable_network(struct supplicant_task *task)
406 {
407         DBusMessage *message, *reply;
408         DBusError error;
409
410         DBG("task %p", task);
411
412         if (task->network == NULL)
413                 return -EINVAL;
414
415         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
416                                         SUPPLICANT_INTF ".Network", "enable");
417         if (message == NULL)
418                 return -ENOMEM;
419
420         dbus_error_init(&error);
421
422         reply = dbus_connection_send_with_reply_and_block(connection,
423                                                         message, -1, &error);
424         if (reply == NULL) {
425                 if (dbus_error_is_set(&error) == TRUE) {
426                         connman_error("%s", error.message);
427                         dbus_error_free(&error);
428                 } else
429                         connman_error("Failed to enable network");
430                 dbus_message_unref(message);
431                 return -EIO;
432         }
433
434         dbus_message_unref(message);
435
436         dbus_message_unref(reply);
437
438         return 0;
439 }
440
441 static int disable_network(struct supplicant_task *task)
442 {
443         DBusMessage *message, *reply;
444         DBusError error;
445
446         DBG("task %p", task);
447
448         if (task->network == NULL)
449                 return -EINVAL;
450
451         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
452                                         SUPPLICANT_INTF ".Network", "disable");
453         if (message == NULL)
454                 return -ENOMEM;
455
456         dbus_error_init(&error);
457
458         reply = dbus_connection_send_with_reply_and_block(connection,
459                                                         message, -1, &error);
460         if (reply == NULL) {
461                 if (dbus_error_is_set(&error) == TRUE) {
462                         connman_error("%s", error.message);
463                         dbus_error_free(&error);
464                 } else
465                         connman_error("Failed to disable network");
466                 dbus_message_unref(message);
467                 return -EIO;
468         }
469
470         dbus_message_unref(message);
471
472         dbus_message_unref(reply);
473
474         return 0;
475 }
476
477 static int set_network(struct supplicant_task *task,
478                                 const unsigned char *network, int len,
479                                 const char *security, const char *passphrase)
480 {
481         DBusMessage *message, *reply;
482         DBusMessageIter array, dict;
483         DBusError error;
484
485         DBG("task %p", task);
486
487         if (task->network == NULL)
488                 return -EINVAL;
489
490         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->network,
491                                         SUPPLICANT_INTF ".Network", "set");
492         if (message == NULL)
493                 return -ENOMEM;
494
495         dbus_message_iter_init_append(message, &array);
496
497         dbus_message_iter_open_container(&array, DBUS_TYPE_ARRAY,
498                         DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
499                         DBUS_TYPE_STRING_AS_STRING DBUS_TYPE_VARIANT_AS_STRING
500                         DBUS_DICT_ENTRY_END_CHAR_AS_STRING, &dict);
501
502         connman_dbus_dict_append_array(&dict, "ssid",
503                                         DBUS_TYPE_BYTE, &network, len);
504
505         if (g_ascii_strcasecmp(security, "wpa") == 0 ||
506                                 g_ascii_strcasecmp(security, "wpa2") == 0) {
507                 const char *key_mgmt = "WPA-PSK";
508                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
509                                                 DBUS_TYPE_STRING, &key_mgmt);
510
511                 if (passphrase && strlen(passphrase) > 0)
512                         connman_dbus_dict_append_variant(&dict, "psk",
513                                                 DBUS_TYPE_STRING, &passphrase);
514         } else if (g_ascii_strcasecmp(security, "wep") == 0) {
515                 const char *key_mgmt = "NONE", *index = "0";
516                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
517                                                 DBUS_TYPE_STRING, &key_mgmt);
518
519                 if (passphrase && strlen(passphrase) > 0) {
520                         connman_dbus_dict_append_variant(&dict, "wep_key0",
521                                                 DBUS_TYPE_STRING, &passphrase);
522                         connman_dbus_dict_append_variant(&dict, "wep_tx_keyidx",
523                                                 DBUS_TYPE_STRING, &index);
524                 }
525         } else {
526                 const char *key_mgmt = "NONE";
527                 connman_dbus_dict_append_variant(&dict, "key_mgmt",
528                                                 DBUS_TYPE_STRING, &key_mgmt);
529         }
530
531         dbus_message_iter_close_container(&array, &dict);
532
533         dbus_error_init(&error);
534
535         reply = dbus_connection_send_with_reply_and_block(connection,
536                                                         message, -1, &error);
537         if (reply == NULL) {
538                 if (dbus_error_is_set(&error) == TRUE) {
539                         connman_error("%s", error.message);
540                         dbus_error_free(&error);
541                 } else
542                         connman_error("Failed to set network options");
543                 dbus_message_unref(message);
544                 return -EIO;
545         }
546
547         dbus_message_unref(message);
548
549         dbus_message_unref(reply);
550
551         return 0;
552 }
553
554 static int initiate_scan(struct supplicant_task *task)
555 {
556         DBusMessage *message;
557         DBusPendingCall *call;
558
559         DBG("task %p", task);
560
561         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
562                                         SUPPLICANT_INTF ".Interface", "scan");
563         if (message == NULL)
564                 return -ENOMEM;
565
566         if (dbus_connection_send_with_reply(connection, message,
567                                                 &call, TIMEOUT) == FALSE) {
568                 connman_error("Failed to initiate scan");
569                 dbus_message_unref(message);
570                 return -EIO;
571         }
572
573         dbus_message_unref(message);
574
575         return 0;
576 }
577
578 static void extract_ssid(struct supplicant_network *network,
579                                                 DBusMessageIter *value)
580 {
581         DBusMessageIter array;
582         unsigned char *ssid;
583         int ssid_len;
584
585         dbus_message_iter_recurse(value, &array);
586         dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
587
588         if (ssid_len < 1)
589                 return;
590
591         network->ssid = g_try_malloc(ssid_len);
592         if (network->ssid == NULL)
593                 return;
594
595         memcpy(network->ssid, ssid, ssid_len);
596         network->ssid_len = ssid_len;
597
598         network->identifier = g_try_malloc0(ssid_len + 1);
599         if (network->identifier == NULL)
600                 return;
601
602         memcpy(network->identifier, ssid, ssid_len);
603 }
604
605 static void extract_wpaie(struct supplicant_network *network,
606                                                 DBusMessageIter *value)
607 {
608         DBusMessageIter array;
609         unsigned char *ie;
610         int ie_len;
611
612         dbus_message_iter_recurse(value, &array);
613         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
614
615         if (ie_len > 0)
616                 network->has_wpa = TRUE;
617 }
618
619 static void extract_rsnie(struct supplicant_network *network,
620                                                 DBusMessageIter *value)
621 {
622         DBusMessageIter array;
623         unsigned char *ie;
624         int ie_len;
625
626         dbus_message_iter_recurse(value, &array);
627         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
628
629         if (ie_len > 0)
630                 network->has_rsn = TRUE;
631 }
632
633 static void extract_capabilites(struct supplicant_network *network,
634                                                 DBusMessageIter *value)
635 {
636         dbus_message_iter_get_basic(value, &network->capabilities);
637
638         if (network->capabilities & IEEE80211_CAP_PRIVACY)
639                 network->has_wep = TRUE;
640 }
641
642 static void properties_reply(DBusPendingCall *call, void *user_data)
643 {
644         struct supplicant_task *task = user_data;
645         struct supplicant_network *network;
646         DBusMessage *reply;
647         DBusMessageIter array, dict;
648
649         DBG("task %p", task);
650
651         reply = dbus_pending_call_steal_reply(call);
652
653         network = g_try_new0(struct supplicant_network, 1);
654         if (network == NULL)
655                 goto done;
656
657         dbus_message_iter_init(reply, &array);
658
659         dbus_message_iter_recurse(&array, &dict);
660
661         while (dbus_message_iter_get_arg_type(&dict) == DBUS_TYPE_DICT_ENTRY) {
662                 DBusMessageIter entry, value;
663                 const char *key;
664
665                 dbus_message_iter_recurse(&dict, &entry);
666                 dbus_message_iter_get_basic(&entry, &key);
667
668                 dbus_message_iter_next(&entry);
669
670                 dbus_message_iter_recurse(&entry, &value);
671
672                 //type = dbus_message_iter_get_arg_type(&value);
673                 //dbus_message_iter_get_basic(&value, &val);
674
675                 /* 
676                  * bssid        : a (97)
677                  * ssid         : a (97)
678                  * wpaie        : a (97)
679                  * rsnie        : a (97)
680                  * frequency    : i (105)
681                  * capabilities : q (113)
682                  * quality      : i (105)
683                  * noise        : i (105)
684                  * level        : i (105)
685                  * maxrate      : i (105)
686                  */
687
688                 if (g_str_equal(key, "ssid") == TRUE)
689                         extract_ssid(network, &value);
690                 else if (g_str_equal(key, "wpaie") == TRUE)
691                         extract_wpaie(network, &value);
692                 else if (g_str_equal(key, "rsnie") == TRUE)
693                         extract_rsnie(network, &value);
694                 else if (g_str_equal(key, "capabilities") == TRUE)
695                         extract_capabilites(network, &value);
696                 else if (g_str_equal(key, "quality") == TRUE)
697                         dbus_message_iter_get_basic(&value, &network->quality);
698                 else if (g_str_equal(key, "noise") == TRUE)
699                         dbus_message_iter_get_basic(&value, &network->noise);
700                 else if (g_str_equal(key, "level") == TRUE)
701                         dbus_message_iter_get_basic(&value, &network->level);
702                 else if (g_str_equal(key, "maxrate") == TRUE)
703                         dbus_message_iter_get_basic(&value, &network->maxrate);
704
705
706                 dbus_message_iter_next(&dict);
707         }
708
709         if (task->callback && task->callback->scan_result)
710                 task->callback->scan_result(task->element, network);
711
712         g_free(network->identifier);
713         g_free(network->ssid);
714         g_free(network);
715
716 done:
717         dbus_message_unref(reply);
718 }
719
720 static int get_network_properties(struct supplicant_task *task,
721                                                         const char *path)
722 {
723         DBusMessage *message;
724         DBusPendingCall *call;
725
726         message = dbus_message_new_method_call(SUPPLICANT_NAME, path,
727                                                 SUPPLICANT_INTF ".BSSID",
728                                                                 "properties");
729         if (message == NULL)
730                 return -ENOMEM;
731
732         if (dbus_connection_send_with_reply(connection, message,
733                                                 &call, TIMEOUT) == FALSE) {
734                 connman_error("Failed to get network properties");
735                 dbus_message_unref(message);
736                 return -EIO;
737         }
738
739         dbus_pending_call_set_notify(call, properties_reply, task, NULL);
740
741         dbus_message_unref(message);
742
743         return 0;
744 }
745
746 static void scan_results_reply(DBusPendingCall *call, void *user_data)
747 {
748         struct supplicant_task *task = user_data;
749         DBusMessage *reply;
750         DBusError error;
751         char **results;
752         int i, num_results;
753
754         DBG("task %p", task);
755
756         reply = dbus_pending_call_steal_reply(call);
757
758         dbus_error_init(&error);
759
760         if (dbus_message_get_args(reply, &error,
761                                 DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH,
762                                                 &results, &num_results,
763                                                 DBUS_TYPE_INVALID) == FALSE) {
764                 if (dbus_error_is_set(&error) == TRUE) {
765                         connman_error("%s", error.message);
766                         dbus_error_free(&error);
767                 } else
768                         connman_error("Wrong arguments for scan result");
769                 goto done;
770         }
771
772         for (i = 0; i < num_results; i++)
773                 get_network_properties(task, results[i]);
774
775         g_strfreev(results);
776
777 done:
778         dbus_message_unref(reply);
779 }
780
781 static int scan_results_available(struct supplicant_task *task)
782 {
783         DBusMessage *message;
784         DBusPendingCall *call;
785
786         DBG("task %p", task);
787
788         message = dbus_message_new_method_call(SUPPLICANT_NAME, task->path,
789                                                 SUPPLICANT_INTF ".Interface",
790                                                         "scanResults");
791         if (message == NULL)
792                 return -ENOMEM;
793
794         if (dbus_connection_send_with_reply(connection, message,
795                                                 &call, TIMEOUT) == FALSE) {
796                 connman_error("Failed to request scan result");
797                 dbus_message_unref(message);
798                 return -EIO;
799         }
800
801         dbus_pending_call_set_notify(call, scan_results_reply, task, NULL);
802
803         dbus_message_unref(message);
804
805         return 0;
806 }
807
808 static void state_change(struct supplicant_task *task, DBusMessage *msg)
809 {
810         DBusError error;
811         const char *state, *previous;
812
813         dbus_error_init(&error);
814
815         if (dbus_message_get_args(msg, &error, DBUS_TYPE_STRING, &state,
816                                                 DBUS_TYPE_STRING, &previous,
817                                                 DBUS_TYPE_INVALID) == FALSE) {
818                 if (dbus_error_is_set(&error) == TRUE) {
819                         connman_error("%s", error.message);
820                         dbus_error_free(&error);
821                 } else
822                         connman_error("Wrong arguments for state change");
823                 return;
824         }
825
826         DBG("state %s ==> %s", previous, state);
827
828         if (g_str_equal(state, "INACTIVE") == TRUE)
829                 task->state = STATE_INACTIVE;
830         else if (g_str_equal(state, "SCANNING") == TRUE)
831                 task->state = STATE_SCANNING;
832         else if (g_str_equal(state, "ASSOCIATING") == TRUE)
833                 task->state = STATE_ASSOCIATING;
834         else if (g_str_equal(state, "ASSOCIATED") == TRUE)
835                 task->state = STATE_ASSOCIATED;
836         else if (g_str_equal(state, "GROUP_HANDSHAKE") == TRUE)
837                 task->state = STATE_4WAY_HANDSHAKE;
838         else if (g_str_equal(state, "4WAY_HANDSHAKE") == TRUE)
839                 task->state = STATE_4WAY_HANDSHAKE;
840         else if (g_str_equal(state, "COMPLETED") == TRUE)
841                 task->state = STATE_COMPLETED;
842         else if (g_str_equal(state, "DISCONNECTED") == TRUE)
843                 task->state = STATE_DISCONNECTED;
844
845         if (task->callback && task->callback->state_change)
846                 task->callback->state_change(task->element, task->state);
847
848         switch (task->state) {
849         case STATE_COMPLETED:
850                 /* carrier on */
851                 break;
852         case STATE_DISCONNECTED:
853                 /* carrier off */
854                 break;
855         default:
856                 break;
857         }
858 }
859
860 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
861                                                 DBusMessage *msg, void *data)
862 {
863         struct supplicant_task *task;
864         const char *member, *path;
865
866         if (dbus_message_has_interface(msg,
867                                 SUPPLICANT_INTF ".Interface") == FALSE)
868                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
869
870         member = dbus_message_get_member(msg);
871         if (member == NULL)
872                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
873
874         path = dbus_message_get_path(msg);
875         if (path == NULL)
876                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
877
878         task = find_task_by_path(path);
879         if (task == NULL)
880                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
881
882         DBG("task %p member %s", task, member);
883
884         if (g_str_equal(member, "ScanResultsAvailable") == TRUE)
885                 scan_results_available(task);
886         else if (g_str_equal(member, "StateChange") == TRUE)
887                 state_change(task, msg);
888
889         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
890 }
891
892 static int add_filter(struct supplicant_task *task)
893 {
894         DBusError error;
895         gchar *filter;
896
897         filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
898                                                 SUPPLICANT_INTF, task->path);
899
900         DBG("filter %s", filter);
901
902         dbus_error_init(&error);
903
904         dbus_bus_add_match(connection, filter, &error);
905
906         g_free(filter);
907
908         if (dbus_error_is_set(&error) == TRUE) {
909                 connman_error("Can't add match: %s", error.message);
910                 dbus_error_free(&error);
911         }
912
913         return 0;
914 }
915
916 static int remove_filter(struct supplicant_task *task)
917 {
918         DBusError error;
919         gchar *filter;
920
921         filter = g_strdup_printf("type=signal,interface=%s.Interface,path=%s",
922                                                 SUPPLICANT_INTF, task->path);
923
924         DBG("filter %s", filter);
925
926         dbus_error_init(&error);
927
928         dbus_bus_remove_match(connection, filter, &error);
929
930         g_free(filter);
931
932         if (dbus_error_is_set(&error) == TRUE) {
933                 connman_error("Can't add match: %s", error.message);
934                 dbus_error_free(&error);
935         }
936
937         return 0;
938 }
939
940 int __supplicant_start(struct connman_element *element,
941                                         struct supplicant_callback *callback)
942 {
943         struct supplicant_task *task;
944         int err;
945
946         DBG("element %p name %s", element, element->name);
947
948         task = g_try_new0(struct supplicant_task, 1);
949         if (task == NULL)
950                 return -ENOMEM;
951
952         task->ifindex = element->index;
953         task->ifname = inet_index2name(element->index);
954         task->element = element;
955         task->callback = callback;
956
957         if (task->ifname == NULL) {
958                 g_free(task);
959                 return -ENOMEM;
960         }
961
962         task->created = FALSE;
963         task->state = STATE_INACTIVE;
964
965         task_list = g_slist_append(task_list, task);
966
967         err = get_interface(task);
968         if (err < 0) {
969                 err = add_interface(task);
970                 if (err < 0) {
971                         g_free(task);
972                         return err;
973                 }
974         }
975
976         add_filter(task);
977
978         set_ap_scan(task);
979
980         return 0;
981 }
982
983 int __supplicant_stop(struct connman_element *element)
984 {
985         struct supplicant_task *task;
986
987         DBG("element %p name %s", element, element->name);
988
989         task = find_task_by_index(element->index);
990         if (task == NULL)
991                 return -ENODEV;
992
993         task_list = g_slist_remove(task_list, task);
994
995         disable_network(task);
996
997         remove_network(task);
998
999         remove_filter(task);
1000
1001         remove_interface(task);
1002
1003         g_free(task->ifname);
1004         g_free(task->path);
1005         g_free(task);
1006
1007         return 0;
1008 }
1009
1010 int __supplicant_scan(struct connman_element *element)
1011 {
1012         struct supplicant_task *task;
1013         int err;
1014
1015         DBG("element %p name %s", element, element->name);
1016
1017         task = find_task_by_index(element->index);
1018         if (task == NULL)
1019                 return -ENODEV;
1020
1021         switch (task->state) {
1022         case STATE_SCANNING:
1023                 return -EALREADY;
1024         case STATE_ASSOCIATING:
1025         case STATE_ASSOCIATED:
1026         case STATE_4WAY_HANDSHAKE:
1027         case STATE_GROUP_HANDSHAKE:
1028                 return -EBUSY;
1029         default:
1030                 break;
1031         }
1032
1033         err = initiate_scan(task);
1034
1035         return 0;
1036 }
1037
1038 int __supplicant_connect(struct connman_element *element,
1039                                 const unsigned char *ssid, int ssid_len,
1040                                 const char *security, const char *passphrase)
1041 {
1042         struct supplicant_task *task;
1043
1044         DBG("element %p name %s", element, element->name);
1045
1046         task = find_task_by_index(element->index);
1047         if (task == NULL)
1048                 return -ENODEV;
1049
1050         add_network(task);
1051
1052         select_network(task);
1053         disable_network(task);
1054
1055         set_network(task, ssid, ssid_len, security, passphrase);
1056
1057         enable_network(task);
1058
1059         return 0;
1060 }
1061
1062 int __supplicant_disconnect(struct connman_element *element)
1063 {
1064         struct supplicant_task *task;
1065
1066         DBG("element %p name %s", element, element->name);
1067
1068         task = find_task_by_index(element->index);
1069         if (task == NULL)
1070                 return -ENODEV;
1071
1072         disable_network(task);
1073
1074         remove_network(task);
1075
1076         return 0;
1077 }
1078
1079 int __supplicant_init(DBusConnection *conn)
1080 {
1081         connection = conn;
1082
1083         if (dbus_connection_add_filter(connection,
1084                                 supplicant_filter, NULL, NULL) == FALSE) {
1085                 dbus_connection_unref(connection);
1086                 return -EIO;
1087         }
1088
1089         return 0;
1090 }
1091
1092 void __supplicant_exit(void)
1093 {
1094         dbus_connection_remove_filter(connection, supplicant_filter, NULL);
1095 }