supplicant: Handle race condition on WiFi dongle removal
[platform/upstream/connman.git] / gsupplicant / supplicant.c
1 /*
2  *
3  *  WPA supplicant library with GLib integration
4  *
5  *  Copyright (C) 2012  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 <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <syslog.h>
32 #include <ctype.h>
33
34 #include <glib.h>
35 #include <gdbus.h>
36
37 #include "dbus.h"
38 #include "gsupplicant.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 static DBusConnection *connection;
47
48 static const GSupplicantCallbacks *callbacks_pointer;
49
50 static dbus_bool_t system_available = FALSE;
51 static dbus_bool_t system_ready = FALSE;
52
53 static dbus_int32_t debug_level;
54 static dbus_bool_t debug_timestamp = FALSE;
55 static dbus_bool_t debug_showkeys = FALSE;
56
57 static const char *debug_strings[] = {
58         "msgdump", "debug", "info", "warning", "error", NULL
59 };
60
61 static unsigned int eap_methods;
62
63 struct strvalmap {
64         const char *str;
65         unsigned int val;
66 };
67
68 static struct strvalmap eap_method_map[] = {
69         { "MD5",        G_SUPPLICANT_EAP_METHOD_MD5     },
70         { "TLS",        G_SUPPLICANT_EAP_METHOD_TLS     },
71         { "MSCHAPV2",   G_SUPPLICANT_EAP_METHOD_MSCHAPV2        },
72         { "PEAP",       G_SUPPLICANT_EAP_METHOD_PEAP    },
73         { "TTLS",       G_SUPPLICANT_EAP_METHOD_TTLS    },
74         { "GTC",        G_SUPPLICANT_EAP_METHOD_GTC     },
75         { "OTP",        G_SUPPLICANT_EAP_METHOD_OTP     },
76         { "LEAP",       G_SUPPLICANT_EAP_METHOD_LEAP    },
77         { "WSC",        G_SUPPLICANT_EAP_METHOD_WSC     },
78         { }
79 };
80
81 static struct strvalmap keymgmt_map[] = {
82         { "none",               G_SUPPLICANT_KEYMGMT_NONE               },
83         { "ieee8021x",          G_SUPPLICANT_KEYMGMT_IEEE8021X  },
84         { "wpa-none",           G_SUPPLICANT_KEYMGMT_WPA_NONE   },
85         { "wpa-psk",            G_SUPPLICANT_KEYMGMT_WPA_PSK    },
86         { "wpa-psk-sha256",     G_SUPPLICANT_KEYMGMT_WPA_PSK_256        },
87         { "wpa-ft-psk",         G_SUPPLICANT_KEYMGMT_WPA_FT_PSK },
88         { "wpa-ft-eap",         G_SUPPLICANT_KEYMGMT_WPA_FT_EAP },
89         { "wpa-eap",            G_SUPPLICANT_KEYMGMT_WPA_EAP    },
90         { "wpa-eap-sha256",     G_SUPPLICANT_KEYMGMT_WPA_EAP_256        },
91         { "wps",                G_SUPPLICANT_KEYMGMT_WPS                },
92         { }
93 };
94
95 static struct strvalmap authalg_capa_map[] = {
96         { "open",       G_SUPPLICANT_CAPABILITY_AUTHALG_OPEN    },
97         { "shared",     G_SUPPLICANT_CAPABILITY_AUTHALG_SHARED  },
98         { "leap",       G_SUPPLICANT_CAPABILITY_AUTHALG_LEAP    },
99         { }
100 };
101
102 static struct strvalmap proto_capa_map[] = {
103         { "wpa",        G_SUPPLICANT_CAPABILITY_PROTO_WPA               },
104         { "rsn",        G_SUPPLICANT_CAPABILITY_PROTO_RSN               },
105         { }
106 };
107
108 static struct strvalmap group_map[] = {
109         { "wep40",      G_SUPPLICANT_GROUP_WEP40        },
110         { "wep104",     G_SUPPLICANT_GROUP_WEP104       },
111         { "tkip",       G_SUPPLICANT_GROUP_TKIP },
112         { "ccmp",       G_SUPPLICANT_GROUP_CCMP },
113         { }
114 };
115
116 static struct strvalmap pairwise_map[] = {
117         { "none",       G_SUPPLICANT_PAIRWISE_NONE      },
118         { "tkip",       G_SUPPLICANT_PAIRWISE_TKIP      },
119         { "ccmp",       G_SUPPLICANT_PAIRWISE_CCMP      },
120         { }
121 };
122
123 static struct strvalmap scan_capa_map[] = {
124         { "active",     G_SUPPLICANT_CAPABILITY_SCAN_ACTIVE     },
125         { "passive",    G_SUPPLICANT_CAPABILITY_SCAN_PASSIVE    },
126         { "ssid",       G_SUPPLICANT_CAPABILITY_SCAN_SSID               },
127         { }
128 };
129
130 static struct strvalmap mode_capa_map[] = {
131         { "infrastructure",     G_SUPPLICANT_CAPABILITY_MODE_INFRA      },
132         { "ad-hoc",             G_SUPPLICANT_CAPABILITY_MODE_IBSS       },
133         { "ap",                 G_SUPPLICANT_CAPABILITY_MODE_AP         },
134         { }
135 };
136
137 static GHashTable *interface_table;
138 static GHashTable *bss_mapping;
139
140 struct _GSupplicantWpsCredentials {
141         unsigned char ssid[32];
142         unsigned int ssid_len;
143         char *key;
144 };
145
146 struct _GSupplicantInterface {
147         char *path;
148         char *network_path;
149         unsigned int keymgmt_capa;
150         unsigned int authalg_capa;
151         unsigned int proto_capa;
152         unsigned int group_capa;
153         unsigned int pairwise_capa;
154         unsigned int scan_capa;
155         unsigned int mode_capa;
156         unsigned int max_scan_ssids;
157         dbus_bool_t ready;
158         GSupplicantState state;
159         dbus_bool_t scanning;
160         GSupplicantInterfaceCallback scan_callback;
161         void *scan_data;
162         int apscan;
163         char *ifname;
164         char *driver;
165         char *bridge;
166         struct _GSupplicantWpsCredentials wps_cred;
167         GSupplicantWpsState wps_state;
168         GHashTable *network_table;
169         GHashTable *net_mapping;
170         GHashTable *bss_mapping;
171         void *data;
172 };
173
174 struct g_supplicant_bss {
175         GSupplicantInterface *interface;
176         char *path;
177         unsigned char bssid[6];
178         unsigned char ssid[32];
179         unsigned int ssid_len;
180         dbus_uint16_t frequency;
181         dbus_uint32_t maxrate;
182         dbus_int16_t signal;
183         GSupplicantMode mode;
184         GSupplicantSecurity security;
185         dbus_bool_t rsn_selected;
186         unsigned int wpa_keymgmt;
187         unsigned int wpa_pairwise;
188         unsigned int wpa_group;
189         unsigned int rsn_keymgmt;
190         unsigned int rsn_pairwise;
191         unsigned int rsn_group;
192         unsigned int keymgmt;
193         dbus_bool_t privacy;
194         dbus_bool_t psk;
195         dbus_bool_t ieee8021x;
196         unsigned int wps_capabilities;
197 };
198
199 struct _GSupplicantNetwork {
200         GSupplicantInterface *interface;
201         char *path;
202         char *group;
203         char *name;
204         unsigned char ssid[32];
205         unsigned int ssid_len;
206         dbus_int16_t signal;
207         dbus_uint16_t frequency;
208         struct g_supplicant_bss *best_bss;
209         GSupplicantMode mode;
210         GSupplicantSecurity security;
211         dbus_bool_t wps;
212         unsigned int wps_capabilities;
213         GHashTable *bss_table;
214         GHashTable *config_table;
215 };
216
217 static inline void debug(const char *format, ...)
218 {
219         char str[256];
220         va_list ap;
221
222         if (callbacks_pointer->debug == NULL)
223                 return;
224
225         va_start(ap, format);
226
227         if (vsnprintf(str, sizeof(str), format, ap) > 0)
228                 callbacks_pointer->debug(str);
229
230         va_end(ap);
231 }
232
233 #define SUPPLICANT_DBG(fmt, arg...) \
234         debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg);
235
236 static GSupplicantMode string2mode(const char *mode)
237 {
238         if (mode == NULL)
239                 return G_SUPPLICANT_MODE_UNKNOWN;
240
241         if (g_str_equal(mode, "infrastructure") == TRUE)
242                 return G_SUPPLICANT_MODE_INFRA;
243         else if (g_str_equal(mode, "ad-hoc") == TRUE)
244                 return G_SUPPLICANT_MODE_IBSS;
245
246         return G_SUPPLICANT_MODE_UNKNOWN;
247 }
248
249 static const char *mode2string(GSupplicantMode mode)
250 {
251         switch (mode) {
252         case G_SUPPLICANT_MODE_UNKNOWN:
253                 break;
254         case G_SUPPLICANT_MODE_INFRA:
255                 return "managed";
256         case G_SUPPLICANT_MODE_IBSS:
257                 return "adhoc";
258         case G_SUPPLICANT_MODE_MASTER:
259                 return "ap";
260         }
261
262         return NULL;
263 }
264
265 static const char *security2string(GSupplicantSecurity security)
266 {
267         switch (security) {
268         case G_SUPPLICANT_SECURITY_UNKNOWN:
269                 break;
270         case G_SUPPLICANT_SECURITY_NONE:
271                 return "none";
272         case G_SUPPLICANT_SECURITY_WEP:
273                 return "wep";
274         case G_SUPPLICANT_SECURITY_PSK:
275                 return "psk";
276         case G_SUPPLICANT_SECURITY_IEEE8021X:
277                 return "ieee8021x";
278         }
279
280         return NULL;
281 }
282
283 static GSupplicantState string2state(const char *state)
284 {
285         if (state == NULL)
286                 return G_SUPPLICANT_STATE_UNKNOWN;
287
288         if (g_str_equal(state, "unknown") == TRUE)
289                 return G_SUPPLICANT_STATE_UNKNOWN;
290         else if (g_str_equal(state, "interface_disabled") == TRUE)
291                 return G_SUPPLICANT_STATE_DISABLED;
292         else if (g_str_equal(state, "disconnected") == TRUE)
293                 return G_SUPPLICANT_STATE_DISCONNECTED;
294         else if (g_str_equal(state, "inactive") == TRUE)
295                 return G_SUPPLICANT_STATE_INACTIVE;
296         else if (g_str_equal(state, "scanning") == TRUE)
297                 return G_SUPPLICANT_STATE_SCANNING;
298         else if (g_str_equal(state, "authenticating") == TRUE)
299                 return G_SUPPLICANT_STATE_AUTHENTICATING;
300         else if (g_str_equal(state, "associating") == TRUE)
301                 return G_SUPPLICANT_STATE_ASSOCIATING;
302         else if (g_str_equal(state, "associated") == TRUE)
303                 return G_SUPPLICANT_STATE_ASSOCIATED;
304         else if (g_str_equal(state, "group_handshake") == TRUE)
305                 return G_SUPPLICANT_STATE_GROUP_HANDSHAKE;
306         else if (g_str_equal(state, "4way_handshake") == TRUE)
307                 return G_SUPPLICANT_STATE_4WAY_HANDSHAKE;
308         else if (g_str_equal(state, "completed") == TRUE)
309                 return G_SUPPLICANT_STATE_COMPLETED;
310
311         return G_SUPPLICANT_STATE_UNKNOWN;
312 }
313
314 static void callback_system_ready(void)
315 {
316         if (system_ready == TRUE)
317                 return;
318
319         system_ready = TRUE;
320
321         if (callbacks_pointer == NULL)
322                 return;
323
324         if (callbacks_pointer->system_ready == NULL)
325                 return;
326
327         callbacks_pointer->system_ready();
328 }
329
330 static void callback_system_killed(void)
331 {
332         system_ready = FALSE;
333
334         if (callbacks_pointer == NULL)
335                 return;
336
337         if (callbacks_pointer->system_killed == NULL)
338                 return;
339
340         callbacks_pointer->system_killed();
341 }
342
343 static void callback_interface_added(GSupplicantInterface *interface)
344 {
345         SUPPLICANT_DBG("");
346
347         if (callbacks_pointer == NULL)
348                 return;
349
350         if (callbacks_pointer->interface_added == NULL)
351                 return;
352
353         callbacks_pointer->interface_added(interface);
354 }
355
356 static void callback_interface_state(GSupplicantInterface *interface)
357 {
358         if (callbacks_pointer == NULL)
359                 return;
360
361         if (callbacks_pointer->interface_state == NULL)
362                 return;
363
364         callbacks_pointer->interface_state(interface);
365 }
366
367 static void callback_interface_removed(GSupplicantInterface *interface)
368 {
369         if (callbacks_pointer == NULL)
370                 return;
371
372         if (callbacks_pointer->interface_removed == NULL)
373                 return;
374
375         callbacks_pointer->interface_removed(interface);
376 }
377
378 static void callback_scan_started(GSupplicantInterface *interface)
379 {
380         if (callbacks_pointer == NULL)
381                 return;
382
383         if (callbacks_pointer->scan_started == NULL)
384                 return;
385
386         callbacks_pointer->scan_started(interface);
387 }
388
389 static void callback_scan_finished(GSupplicantInterface *interface)
390 {
391         if (callbacks_pointer == NULL)
392                 return;
393
394         if (callbacks_pointer->scan_finished == NULL)
395                 return;
396
397         callbacks_pointer->scan_finished(interface);
398 }
399
400 static void callback_network_added(GSupplicantNetwork *network)
401 {
402         if (callbacks_pointer == NULL)
403                 return;
404
405         if (callbacks_pointer->network_added == NULL)
406                 return;
407
408         callbacks_pointer->network_added(network);
409 }
410
411 static void callback_network_removed(GSupplicantNetwork *network)
412 {
413         if (callbacks_pointer == NULL)
414                 return;
415
416         if (callbacks_pointer->network_removed == NULL)
417                 return;
418
419         callbacks_pointer->network_removed(network);
420 }
421
422 static void callback_network_changed(GSupplicantNetwork *network,
423                                         const char *property)
424 {
425         if (callbacks_pointer == NULL)
426                 return;
427
428         if (callbacks_pointer->network_changed == NULL)
429                 return;
430
431         callbacks_pointer->network_changed(network, property);
432 }
433
434 static void remove_interface(gpointer data)
435 {
436         GSupplicantInterface *interface = data;
437
438         g_hash_table_destroy(interface->bss_mapping);
439         g_hash_table_destroy(interface->net_mapping);
440         g_hash_table_destroy(interface->network_table);
441
442         if (interface->scan_callback != NULL) {
443                 SUPPLICANT_DBG("call interface %p callback %p scanning %d",
444                                 interface, interface->scan_callback,
445                                 interface->scanning);
446
447                 interface->scan_callback(-EIO, interface, interface->scan_data);
448                 interface->scan_callback = NULL;
449                 interface->scan_data = NULL;
450
451                 if (interface->scanning == TRUE) {
452                         interface->scanning = FALSE;
453                         callback_scan_finished(interface);
454                 }
455         }
456
457         callback_interface_removed(interface);
458
459         g_free(interface->wps_cred.key);
460         g_free(interface->path);
461         g_free(interface->network_path);
462         g_free(interface->ifname);
463         g_free(interface->driver);
464         g_free(interface->bridge);
465         g_free(interface);
466 }
467
468 static void remove_network(gpointer data)
469 {
470         GSupplicantNetwork *network = data;
471
472         g_hash_table_destroy(network->bss_table);
473
474         callback_network_removed(network);
475
476         g_hash_table_destroy(network->config_table);
477
478         g_free(network->path);
479         g_free(network->group);
480         g_free(network->name);
481         g_free(network);
482 }
483
484 static void remove_bss(gpointer data)
485 {
486         struct g_supplicant_bss *bss = data;
487
488         g_free(bss->path);
489         g_free(bss);
490 }
491
492 static void debug_strvalmap(const char *label, struct strvalmap *map,
493                                                         unsigned int val)
494 {
495         int i;
496
497         for (i = 0; map[i].str != NULL; i++) {
498                 if (val & map[i].val)
499                         SUPPLICANT_DBG("%s: %s", label, map[i].str);
500         }
501 }
502
503 static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data)
504 {
505         GSupplicantInterface *interface = user_data;
506         const char *str = NULL;
507         int i;
508
509         dbus_message_iter_get_basic(iter, &str);
510         if (str == NULL)
511                 return;
512
513         for (i = 0; keymgmt_map[i].str != NULL; i++)
514                 if (strcmp(str, keymgmt_map[i].str) == 0) {
515                         interface->keymgmt_capa |= keymgmt_map[i].val;
516                         break;
517                 }
518 }
519
520 static void interface_capability_authalg(DBusMessageIter *iter, void *user_data)
521 {
522         GSupplicantInterface *interface = user_data;
523         const char *str = NULL;
524         int i;
525
526         dbus_message_iter_get_basic(iter, &str);
527         if (str == NULL)
528                 return;
529
530         for (i = 0; authalg_capa_map[i].str != NULL; i++)
531                 if (strcmp(str, authalg_capa_map[i].str) == 0) {
532                         interface->authalg_capa |= authalg_capa_map[i].val;
533                         break;
534                 }
535 }
536
537 static void interface_capability_proto(DBusMessageIter *iter, void *user_data)
538 {
539         GSupplicantInterface *interface = user_data;
540         const char *str = NULL;
541         int i;
542
543         dbus_message_iter_get_basic(iter, &str);
544         if (str == NULL)
545                 return;
546
547         for (i = 0; proto_capa_map[i].str != NULL; i++)
548                 if (strcmp(str, proto_capa_map[i].str) == 0) {
549                         interface->proto_capa |= proto_capa_map[i].val;
550                         break;
551                 }
552 }
553
554 static void interface_capability_pairwise(DBusMessageIter *iter,
555                                                         void *user_data)
556 {
557         GSupplicantInterface *interface = user_data;
558         const char *str = NULL;
559         int i;
560
561         dbus_message_iter_get_basic(iter, &str);
562         if (str == NULL)
563                 return;
564
565         for (i = 0; pairwise_map[i].str != NULL; i++)
566                 if (strcmp(str, pairwise_map[i].str) == 0) {
567                         interface->pairwise_capa |= pairwise_map[i].val;
568                         break;
569                 }
570 }
571
572 static void interface_capability_group(DBusMessageIter *iter, void *user_data)
573 {
574         GSupplicantInterface *interface = user_data;
575         const char *str = NULL;
576         int i;
577
578         dbus_message_iter_get_basic(iter, &str);
579         if (str == NULL)
580                 return;
581
582         for (i = 0; group_map[i].str != NULL; i++)
583                 if (strcmp(str, group_map[i].str) == 0) {
584                         interface->group_capa |= group_map[i].val;
585                         break;
586                 }
587 }
588
589 static void interface_capability_scan(DBusMessageIter *iter, void *user_data)
590 {
591         GSupplicantInterface *interface = user_data;
592         const char *str = NULL;
593         int i;
594
595         dbus_message_iter_get_basic(iter, &str);
596         if (str == NULL)
597                 return;
598
599         for (i = 0; scan_capa_map[i].str != NULL; i++)
600                 if (strcmp(str, scan_capa_map[i].str) == 0) {
601                         interface->scan_capa |= scan_capa_map[i].val;
602                         break;
603                 }
604 }
605
606 static void interface_capability_mode(DBusMessageIter *iter, void *user_data)
607 {
608         GSupplicantInterface *interface = user_data;
609         const char *str = NULL;
610         int i;
611
612         dbus_message_iter_get_basic(iter, &str);
613         if (str == NULL)
614                 return;
615
616         for (i = 0; mode_capa_map[i].str != NULL; i++)
617                 if (strcmp(str, mode_capa_map[i].str) == 0) {
618                         interface->mode_capa |= mode_capa_map[i].val;
619                         break;
620                 }
621 }
622
623 static void interface_capability(const char *key, DBusMessageIter *iter,
624                                                         void *user_data)
625 {
626         GSupplicantInterface *interface = user_data;
627
628         if (key == NULL)
629                 return;
630
631         if (g_strcmp0(key, "KeyMgmt") == 0)
632                 supplicant_dbus_array_foreach(iter,
633                                 interface_capability_keymgmt, interface);
634         else if (g_strcmp0(key, "AuthAlg") == 0)
635                 supplicant_dbus_array_foreach(iter,
636                                 interface_capability_authalg, interface);
637         else if (g_strcmp0(key, "Protocol") == 0)
638                 supplicant_dbus_array_foreach(iter,
639                                 interface_capability_proto, interface);
640         else if (g_strcmp0(key, "Pairwise") == 0)
641                 supplicant_dbus_array_foreach(iter,
642                                 interface_capability_pairwise, interface);
643         else if (g_strcmp0(key, "Group") == 0)
644                 supplicant_dbus_array_foreach(iter,
645                                 interface_capability_group, interface);
646         else if (g_strcmp0(key, "Scan") == 0)
647                 supplicant_dbus_array_foreach(iter,
648                                 interface_capability_scan, interface);
649         else if (g_strcmp0(key, "Modes") == 0)
650                 supplicant_dbus_array_foreach(iter,
651                                 interface_capability_mode, interface);
652         else if (g_strcmp0(key, "MaxScanSSID") == 0) {
653                 dbus_int32_t max_scan_ssid;
654
655                 dbus_message_iter_get_basic(iter, &max_scan_ssid);
656                 interface->max_scan_ssids = max_scan_ssid;
657
658         } else
659                 SUPPLICANT_DBG("key %s type %c",
660                                 key, dbus_message_iter_get_arg_type(iter));
661 }
662
663 static void set_apscan(DBusMessageIter *iter, void *user_data)
664 {
665         unsigned int ap_scan = *(unsigned int *)user_data;
666
667         dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &ap_scan);
668 }
669
670 int g_supplicant_interface_set_apscan(GSupplicantInterface *interface,
671                                                         unsigned int ap_scan)
672 {
673         return supplicant_dbus_property_set(interface->path,
674                         SUPPLICANT_INTERFACE ".Interface",
675                                 "ApScan", DBUS_TYPE_UINT32_AS_STRING,
676                                         set_apscan, NULL, &ap_scan);
677 }
678
679 void g_supplicant_interface_set_data(GSupplicantInterface *interface,
680                                                                 void *data)
681 {
682         if (interface == NULL)
683                 return;
684
685         interface->data = data;
686 }
687
688 void *g_supplicant_interface_get_data(GSupplicantInterface *interface)
689 {
690         if (interface == NULL)
691                 return NULL;
692
693         return interface->data;
694 }
695
696 const char *g_supplicant_interface_get_ifname(GSupplicantInterface *interface)
697 {
698         if (interface == NULL)
699                 return NULL;
700
701         return interface->ifname;
702 }
703
704 const char *g_supplicant_interface_get_driver(GSupplicantInterface *interface)
705 {
706         if (interface == NULL)
707                 return NULL;
708
709         return interface->driver;
710 }
711
712 GSupplicantState g_supplicant_interface_get_state(
713                                         GSupplicantInterface *interface)
714 {
715         if (interface == NULL)
716                 return G_SUPPLICANT_STATE_UNKNOWN;
717
718         return interface->state;
719 }
720
721 const char *g_supplicant_interface_get_wps_key(GSupplicantInterface *interface)
722 {
723         if (interface == NULL)
724                 return NULL;
725
726         return (const char *)interface->wps_cred.key;
727 }
728
729 const void *g_supplicant_interface_get_wps_ssid(GSupplicantInterface *interface,
730                                                         unsigned int *ssid_len)
731 {
732         if (ssid_len == NULL)
733                 return NULL;
734
735         if (interface == NULL || interface->wps_cred.ssid == NULL) {
736                 *ssid_len = 0;
737                 return NULL;
738         }
739
740         *ssid_len = interface->wps_cred.ssid_len;
741         return interface->wps_cred.ssid;
742 }
743
744 GSupplicantWpsState g_supplicant_interface_get_wps_state(
745                                         GSupplicantInterface *interface)
746 {
747         if (interface == NULL)
748                 return G_SUPPLICANT_WPS_STATE_UNKNOWN;
749
750         return interface->wps_state;
751 }
752
753 unsigned int g_supplicant_interface_get_mode(GSupplicantInterface *interface)
754 {
755         if (interface == NULL)
756                 return 0;
757
758         return interface->mode_capa;
759 }
760
761 unsigned int g_supplicant_interface_get_max_scan_ssids(
762                                 GSupplicantInterface *interface)
763 {
764         if (interface == NULL)
765                 return 0;
766
767         if (interface->max_scan_ssids == 0)
768                 return WPAS_MAX_SCAN_SSIDS;
769
770         return interface->max_scan_ssids;
771 }
772
773 static void set_network_enabled(DBusMessageIter *iter, void *user_data)
774 {
775         dbus_bool_t enable = *(dbus_bool_t *)user_data;
776
777         dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &enable);
778 }
779
780 int g_supplicant_interface_enable_selected_network(GSupplicantInterface *interface,
781                                                         dbus_bool_t enable)
782 {
783         if (interface == NULL)
784                 return -1;
785
786         if (interface->network_path == NULL)
787                 return -1;
788
789         SUPPLICANT_DBG(" ");
790         return supplicant_dbus_property_set(interface->network_path,
791                                 SUPPLICANT_INTERFACE ".Network",
792                                 "Enabled", DBUS_TYPE_BOOLEAN_AS_STRING,
793                                 set_network_enabled, NULL, &enable);
794 }
795
796 dbus_bool_t g_supplicant_interface_get_ready(GSupplicantInterface *interface)
797 {
798         if (interface == NULL)
799                 return FALSE;
800
801         return interface->ready;
802 }
803
804 GSupplicantInterface *g_supplicant_network_get_interface(
805                                         GSupplicantNetwork *network)
806 {
807         if (network == NULL)
808                 return NULL;
809
810         return network->interface;
811 }
812
813 const char *g_supplicant_network_get_name(GSupplicantNetwork *network)
814 {
815         if (network == NULL || network->name == NULL)
816                 return "";
817
818         return network->name;
819 }
820
821 const char *g_supplicant_network_get_identifier(GSupplicantNetwork *network)
822 {
823         if (network == NULL || network->group == NULL)
824                 return "";
825
826         return network->group;
827 }
828
829 const char *g_supplicant_network_get_path(GSupplicantNetwork *network)
830 {
831         if (network == NULL || network->path == NULL)
832                 return NULL;
833
834         return network->path;
835 }
836
837 const char *g_supplicant_network_get_mode(GSupplicantNetwork *network)
838 {
839         if (network == NULL)
840                 return G_SUPPLICANT_MODE_UNKNOWN;
841
842         return mode2string(network->mode);
843 }
844
845 const char *g_supplicant_network_get_security(GSupplicantNetwork *network)
846 {
847         if (network == NULL)
848                 return G_SUPPLICANT_SECURITY_UNKNOWN;
849
850         return security2string(network->security);
851 }
852
853 const void *g_supplicant_network_get_ssid(GSupplicantNetwork *network,
854                                                 unsigned int *ssid_len)
855 {
856         if (network == NULL || network->ssid == NULL) {
857                 *ssid_len = 0;
858                 return NULL;
859         }
860
861         *ssid_len = network->ssid_len;
862         return network->ssid;
863 }
864
865 dbus_int16_t g_supplicant_network_get_signal(GSupplicantNetwork *network)
866 {
867         if (network == NULL)
868                 return 0;
869
870         return network->signal;
871 }
872
873 dbus_uint16_t g_supplicant_network_get_frequency(GSupplicantNetwork *network)
874 {
875         if (network == NULL)
876                 return 0;
877
878         return network->frequency;
879 }
880
881 dbus_bool_t g_supplicant_network_get_wps(GSupplicantNetwork *network)
882 {
883         if (network == NULL)
884                 return FALSE;
885
886         return network->wps;
887 }
888
889 dbus_bool_t g_supplicant_network_is_wps_active(GSupplicantNetwork *network)
890 {
891         if (network == NULL)
892                 return FALSE;
893
894         if (network->wps_capabilities & G_SUPPLICANT_WPS_CONFIGURED)
895                 return TRUE;
896
897         return FALSE;
898 }
899
900 dbus_bool_t g_supplicant_network_is_wps_pbc(GSupplicantNetwork *network)
901 {
902         if (network == NULL)
903                 return FALSE;
904
905         if (network->wps_capabilities & G_SUPPLICANT_WPS_PBC)
906                 return TRUE;
907
908         return FALSE;
909 }
910
911 dbus_bool_t g_supplicant_network_is_wps_advertizing(GSupplicantNetwork *network)
912 {
913         if (network == NULL)
914                 return FALSE;
915
916         if (network->wps_capabilities & G_SUPPLICANT_WPS_REGISTRAR)
917                 return TRUE;
918
919         return FALSE;
920 }
921
922 static void merge_network(GSupplicantNetwork *network)
923 {
924         GString *str;
925         const char *ssid, *mode, *key_mgmt;
926         unsigned int i, ssid_len;
927         char *group;
928
929         ssid = g_hash_table_lookup(network->config_table, "ssid");
930         mode = g_hash_table_lookup(network->config_table, "mode");
931         key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
932
933         SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
934
935         if (ssid != NULL)
936                 ssid_len = strlen(ssid);
937         else
938                 ssid_len = 0;
939
940         str = g_string_sized_new((ssid_len * 2) + 24);
941         if (str == NULL)
942                 return;
943
944         for (i = 0; i < ssid_len; i++)
945                 g_string_append_printf(str, "%02x", ssid[i]);
946
947         if (g_strcmp0(mode, "0") == 0)
948                 g_string_append_printf(str, "_managed");
949         else if (g_strcmp0(mode, "1") == 0)
950                 g_string_append_printf(str, "_adhoc");
951
952         if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
953                 g_string_append_printf(str, "_psk");
954
955         group = g_string_free(str, FALSE);
956
957         SUPPLICANT_DBG("%s", group);
958
959         g_free(group);
960
961         g_hash_table_destroy(network->config_table);
962
963         g_free(network->path);
964         g_free(network);
965 }
966
967 static void network_property(const char *key, DBusMessageIter *iter,
968                                                         void *user_data)
969 {
970         GSupplicantNetwork *network = user_data;
971
972         if (network->interface == NULL)
973                 return;
974
975         if (key == NULL) {
976                 merge_network(network);
977                 return;
978         }
979
980         if (g_strcmp0(key, "Enabled") == 0) {
981                 dbus_bool_t enabled = FALSE;
982
983                 dbus_message_iter_get_basic(iter, &enabled);
984         } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
985                 const char *str = NULL;
986
987                 dbus_message_iter_get_basic(iter, &str);
988                 if (str != NULL) {
989                         g_hash_table_replace(network->config_table,
990                                                 g_strdup(key), g_strdup(str));
991                 }
992         } else
993                 SUPPLICANT_DBG("key %s type %c",
994                                 key, dbus_message_iter_get_arg_type(iter));
995 }
996
997 static void interface_network_added(DBusMessageIter *iter, void *user_data)
998 {
999         GSupplicantInterface *interface = user_data;
1000         GSupplicantNetwork *network;
1001         const char *path = NULL;
1002
1003         SUPPLICANT_DBG("");
1004
1005         dbus_message_iter_get_basic(iter, &path);
1006         if (path == NULL)
1007                 return;
1008
1009         if (g_strcmp0(path, "/") == 0)
1010                 return;
1011
1012         network = g_hash_table_lookup(interface->net_mapping, path);
1013         if (network != NULL)
1014                 return;
1015
1016         network = g_try_new0(GSupplicantNetwork, 1);
1017         if (network == NULL)
1018                 return;
1019
1020         network->interface = interface;
1021         network->path = g_strdup(path);
1022
1023         network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1024                                                         g_free, g_free);
1025
1026         dbus_message_iter_next(iter);
1027         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1028                 supplicant_dbus_property_foreach(iter, network_property,
1029                                                                 network);
1030                 network_property(NULL, NULL, network);
1031                 return;
1032         }
1033
1034         supplicant_dbus_property_get_all(path,
1035                                 SUPPLICANT_INTERFACE ".Network",
1036                                                 network_property, network);
1037 }
1038
1039 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
1040 {
1041         GSupplicantInterface *interface = user_data;
1042         GSupplicantNetwork *network;
1043         const char *path = NULL;
1044
1045         dbus_message_iter_get_basic(iter, &path);
1046         if (path == NULL)
1047                 return;
1048
1049         network = g_hash_table_lookup(interface->net_mapping, path);
1050         if (network == NULL)
1051                 return;
1052
1053         g_hash_table_remove(interface->net_mapping, path);
1054 }
1055
1056 static char *create_name(unsigned char *ssid, int ssid_len)
1057 {
1058         GString *string;
1059         const gchar *remainder, *invalid;
1060         int valid_bytes, remaining_bytes;
1061
1062         if (ssid_len < 1 || ssid[0] == '\0')
1063                 return g_strdup("");
1064
1065         string = NULL;
1066         remainder = (const gchar *)ssid;
1067         remaining_bytes = ssid_len;
1068
1069         while (remaining_bytes != 0) {
1070                 if (g_utf8_validate(remainder, remaining_bytes,
1071                                         &invalid) == TRUE) {
1072                         break;
1073                 }
1074
1075                 valid_bytes = invalid - remainder;
1076
1077                 if (string == NULL)
1078                         string = g_string_sized_new(remaining_bytes);
1079
1080                 g_string_append_len(string, remainder, valid_bytes);
1081
1082                 /* append U+FFFD REPLACEMENT CHARACTER */
1083                 g_string_append(string, "\357\277\275");
1084
1085                 remaining_bytes -= valid_bytes + 1;
1086                 remainder = invalid + 1;
1087         }
1088
1089         if (string == NULL)
1090                 return g_strndup((const gchar *)ssid, ssid_len + 1);
1091
1092         g_string_append(string, remainder);
1093
1094         return g_string_free(string, FALSE);
1095 }
1096
1097 static char *create_group(struct g_supplicant_bss *bss)
1098 {
1099         GString *str;
1100         unsigned int i;
1101         const char *mode, *security;
1102
1103         str = g_string_sized_new((bss->ssid_len * 2) + 24);
1104         if (str == NULL)
1105                 return NULL;
1106
1107         if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
1108                 for (i = 0; i < bss->ssid_len; i++)
1109                         g_string_append_printf(str, "%02x", bss->ssid[i]);
1110         } else
1111                 g_string_append_printf(str, "hidden");
1112
1113         mode = mode2string(bss->mode);
1114         if (mode != NULL)
1115                 g_string_append_printf(str, "_%s", mode);
1116
1117         security = security2string(bss->security);
1118         if (security != NULL)
1119                 g_string_append_printf(str, "_%s", security);
1120
1121         return g_string_free(str, FALSE);
1122 }
1123
1124 static void add_or_replace_bss_to_network(struct g_supplicant_bss *bss)
1125 {
1126         GSupplicantInterface *interface = bss->interface;
1127         GSupplicantNetwork *network;
1128         char *group;
1129
1130         group = create_group(bss);
1131         SUPPLICANT_DBG("New group created: %s", group);
1132
1133         if (group == NULL)
1134                 return;
1135
1136         network = g_hash_table_lookup(interface->network_table, group);
1137         if (network != NULL) {
1138                 g_free(group);
1139                 SUPPLICANT_DBG("Network %s already exist", network->name);
1140
1141                 goto done;
1142         }
1143
1144         network = g_try_new0(GSupplicantNetwork, 1);
1145         if (network == NULL) {
1146                 g_free(group);
1147                 return;
1148         }
1149
1150         network->interface = interface;
1151         if (network->path == NULL)
1152                 network->path = g_strdup(bss->path);
1153         network->group = group;
1154         network->name = create_name(bss->ssid, bss->ssid_len);
1155         network->mode = bss->mode;
1156         network->security = bss->security;
1157         network->ssid_len = bss->ssid_len;
1158         memcpy(network->ssid, bss->ssid, bss->ssid_len);
1159         network->signal = bss->signal;
1160         network->frequency = bss->frequency;
1161         network->best_bss = bss;
1162
1163         SUPPLICANT_DBG("New network %s created", network->name);
1164
1165         network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1166                                                         NULL, remove_bss);
1167
1168         network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1169                                                         g_free, g_free);
1170
1171         g_hash_table_replace(interface->network_table,
1172                                                 network->group, network);
1173
1174         callback_network_added(network);
1175
1176 done:
1177         /* We update network's WPS properties if only bss provides WPS. */
1178         if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
1179                 network->wps = TRUE;
1180                 network->wps_capabilities |= bss->wps_capabilities;
1181         }
1182
1183         if (bss->signal > network->signal) {
1184                 network->signal = bss->signal;
1185                 network->best_bss = bss;
1186                 callback_network_changed(network, "Signal");
1187         }
1188
1189         g_hash_table_replace(interface->bss_mapping, bss->path, network);
1190         g_hash_table_replace(network->bss_table, bss->path, bss);
1191
1192         g_hash_table_replace(bss_mapping, bss->path, interface);
1193 }
1194
1195 static void bss_rates(DBusMessageIter *iter, void *user_data)
1196 {
1197         struct g_supplicant_bss *bss = user_data;
1198         dbus_uint32_t rate = 0;
1199
1200         dbus_message_iter_get_basic(iter, &rate);
1201         if (rate == 0)
1202                 return;
1203
1204         if (rate > bss->maxrate)
1205                 bss->maxrate = rate;
1206 }
1207
1208 static void bss_keymgmt(DBusMessageIter *iter, void *user_data)
1209 {
1210         unsigned int *keymgmt = user_data;
1211         const char *str = NULL;
1212         int i;
1213
1214         dbus_message_iter_get_basic(iter, &str);
1215         if (str == NULL)
1216                 return;
1217
1218         for (i = 0; keymgmt_map[i].str != NULL; i++)
1219                 if (strcmp(str, keymgmt_map[i].str) == 0) {
1220                         SUPPLICANT_DBG("Keymgmt: %s", str);
1221                         *keymgmt |= keymgmt_map[i].val;
1222                         break;
1223                 }
1224 }
1225
1226 static void bss_group(DBusMessageIter *iter, void *user_data)
1227 {
1228         unsigned int *group = user_data;
1229         const char *str = NULL;
1230         int i;
1231
1232         dbus_message_iter_get_basic(iter, &str);
1233         if (str == NULL)
1234                 return;
1235
1236         for (i = 0; group_map[i].str != NULL; i++)
1237                 if (strcmp(str, group_map[i].str) == 0) {
1238                         SUPPLICANT_DBG("Group: %s", str);
1239                         *group |= group_map[i].val;
1240                         break;
1241                 }
1242 }
1243
1244 static void bss_pairwise(DBusMessageIter *iter, void *user_data)
1245 {
1246         unsigned int *pairwise = user_data;
1247         const char *str = NULL;
1248         int i;
1249
1250         dbus_message_iter_get_basic(iter, &str);
1251         if (str == NULL)
1252                 return;
1253
1254         for (i = 0; pairwise_map[i].str != NULL; i++)
1255                 if (strcmp(str, pairwise_map[i].str) == 0) {
1256                         SUPPLICANT_DBG("Pairwise: %s", str);
1257                         *pairwise |= pairwise_map[i].val;
1258                         break;
1259                 }
1260 }
1261
1262 static void bss_wpa(const char *key, DBusMessageIter *iter,
1263                         void *user_data)
1264 {
1265         struct g_supplicant_bss *bss = user_data;
1266         unsigned int value = 0;
1267
1268         SUPPLICANT_DBG("Key: %s", key);
1269
1270         if (g_strcmp0(key, "KeyMgmt") == 0) {
1271                 supplicant_dbus_array_foreach(iter, bss_keymgmt, &value);
1272
1273                 if (bss->rsn_selected == TRUE)
1274                         bss->rsn_keymgmt = value;
1275                 else
1276                         bss->wpa_keymgmt = value;
1277         } else if (g_strcmp0(key, "Group") == 0) {
1278                 supplicant_dbus_array_foreach(iter, bss_group, &value);
1279
1280                 if (bss->rsn_selected == TRUE)
1281                         bss->rsn_group = value;
1282                 else
1283                         bss->wpa_group = value;
1284         } else if (g_strcmp0(key, "Pairwise") == 0) {
1285                 supplicant_dbus_array_foreach(iter, bss_pairwise, &value);
1286
1287                 if (bss->rsn_selected == TRUE)
1288                         bss->rsn_pairwise = value;
1289                 else
1290                         bss->wpa_pairwise = value;
1291         }
1292 }
1293
1294 static unsigned int get_tlv(unsigned char *ie, unsigned int ie_size,
1295                                                         unsigned int type)
1296 {
1297         unsigned int len = 0;
1298
1299         while (len + 4 < ie_size) {
1300                 unsigned int hi = ie[len];
1301                 unsigned int lo = ie[len + 1];
1302                 unsigned int tmp_type = (hi << 8) + lo;
1303                 unsigned int v_len = 0;
1304
1305                 /* hi and lo are used to recreate an unsigned int
1306                  * based on 2 8bits length unsigned int. */
1307
1308                 hi = ie[len + 2];
1309                 lo = ie[len + 3];
1310                 v_len = (hi << 8) + lo;
1311
1312                 if (tmp_type == type) {
1313                         unsigned int ret_value = 0;
1314                         unsigned char *value = (unsigned char *)&ret_value;
1315
1316                         SUPPLICANT_DBG("IE: match type 0x%x", type);
1317
1318                         /* Verifying length relevance */
1319                         if (v_len > sizeof(unsigned int) ||
1320                                 len + 4 + v_len > ie_size)
1321                                 break;
1322
1323                         memcpy(value, ie + len + 4, v_len);
1324
1325                         SUPPLICANT_DBG("returning 0x%x", ret_value);
1326                         return ret_value;
1327                 }
1328
1329                 len += v_len + 4;
1330         }
1331
1332         SUPPLICANT_DBG("returning 0");
1333         return 0;
1334 }
1335
1336 static void bss_process_ies(DBusMessageIter *iter, void *user_data)
1337 {
1338         struct g_supplicant_bss *bss = user_data;
1339         const unsigned char WPS_OUI[] = { 0x00, 0x50, 0xf2, 0x04 };
1340         unsigned char *ie, *ie_end;
1341         DBusMessageIter array;
1342         unsigned int value;
1343         int ie_len;
1344
1345 #define WMM_WPA1_WPS_INFO 221
1346 #define WPS_INFO_MIN_LEN  6
1347 #define WPS_VERSION_TLV   0x104A
1348 #define WPS_STATE_TLV     0x1044
1349 #define WPS_METHODS_TLV   0x1012
1350 #define WPS_REGISTRAR_TLV 0x1041
1351 #define WPS_VERSION       0x10
1352 #define WPS_PBC           0x04
1353 #define WPS_PIN           0x00
1354 #define WPS_CONFIGURED    0x02
1355
1356         dbus_message_iter_recurse(iter, &array);
1357         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1358
1359         if (ie == NULL || ie_len < 2)
1360                 return;
1361
1362         bss->wps_capabilities = 0;
1363         bss->keymgmt = 0;
1364
1365         for (ie_end = ie + ie_len; ie < ie_end && ie + ie[1] + 1 <= ie_end;
1366                                                         ie += ie[1] + 2) {
1367
1368                 if (ie[0] != WMM_WPA1_WPS_INFO || ie[1] < WPS_INFO_MIN_LEN ||
1369                         memcmp(ie+2, WPS_OUI, sizeof(WPS_OUI)) != 0)
1370                         continue;
1371
1372                 SUPPLICANT_DBG("IE: match WPS_OUI");
1373
1374                 value = get_tlv(&ie[6], ie[1], WPS_STATE_TLV);
1375                 if (get_tlv(&ie[6], ie[1], WPS_VERSION_TLV) == WPS_VERSION &&
1376                                                                 value != 0) {
1377                         bss->keymgmt |= G_SUPPLICANT_KEYMGMT_WPS;
1378
1379                         if (value == WPS_CONFIGURED)
1380                                 bss->wps_capabilities |=
1381                                         G_SUPPLICANT_WPS_CONFIGURED;
1382                 }
1383
1384                 value = get_tlv(&ie[6], ie[1], WPS_METHODS_TLV);
1385                 if (value != 0) {
1386                         if (GUINT16_FROM_BE(value) == WPS_PBC)
1387                                 bss->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
1388                         if (GUINT16_FROM_BE(value) == WPS_PIN)
1389                                 bss->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
1390                 } else
1391                         bss->wps_capabilities |=
1392                                 G_SUPPLICANT_WPS_PBC | G_SUPPLICANT_WPS_PIN;
1393
1394                 /* If the AP sends this it means it's advertizing
1395                  * as a registrar and the WPS process is launched
1396                  * on its side */
1397                 if (get_tlv(&ie[6], ie[1], WPS_REGISTRAR_TLV) != 0)
1398                         bss->wps_capabilities |= G_SUPPLICANT_WPS_REGISTRAR;
1399
1400                 SUPPLICANT_DBG("WPS Methods 0x%x", bss->wps_capabilities);
1401         }
1402 }
1403
1404 static void bss_compute_security(struct g_supplicant_bss *bss)
1405 {
1406         /*
1407          * Combining RSN and WPA keymgmt
1408          * We combine it since parsing IEs might have set something for WPS. */
1409         bss->keymgmt |= bss->rsn_keymgmt | bss->wpa_keymgmt;
1410
1411         bss->ieee8021x = FALSE;
1412         bss->psk = FALSE;
1413
1414         if (bss->keymgmt &
1415                         (G_SUPPLICANT_KEYMGMT_WPA_EAP |
1416                                 G_SUPPLICANT_KEYMGMT_WPA_FT_EAP |
1417                                 G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
1418                 bss->ieee8021x = TRUE;
1419
1420         if (bss->keymgmt &
1421                         (G_SUPPLICANT_KEYMGMT_WPA_PSK |
1422                                 G_SUPPLICANT_KEYMGMT_WPA_FT_PSK |
1423                                 G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
1424                 bss->psk = TRUE;
1425
1426         if (bss->ieee8021x == TRUE)
1427                 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
1428         else if (bss->psk == TRUE)
1429                 bss->security = G_SUPPLICANT_SECURITY_PSK;
1430         else if (bss->privacy == TRUE)
1431                 bss->security = G_SUPPLICANT_SECURITY_WEP;
1432         else
1433                 bss->security = G_SUPPLICANT_SECURITY_NONE;
1434 }
1435
1436
1437 static void bss_property(const char *key, DBusMessageIter *iter,
1438                                                         void *user_data)
1439 {
1440         struct g_supplicant_bss *bss = user_data;
1441
1442         if (bss->interface == NULL)
1443                 return;
1444
1445         SUPPLICANT_DBG("key %s", key);
1446
1447         if (key == NULL)
1448                 return;
1449
1450         if (g_strcmp0(key, "BSSID") == 0) {
1451                 DBusMessageIter array;
1452                 unsigned char *addr;
1453                 int addr_len;
1454
1455                 dbus_message_iter_recurse(iter, &array);
1456                 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1457
1458                 if (addr_len == 6)
1459                         memcpy(bss->bssid, addr, addr_len);
1460         } else if (g_strcmp0(key, "SSID") == 0) {
1461                 DBusMessageIter array;
1462                 unsigned char *ssid;
1463                 int ssid_len;
1464
1465                 dbus_message_iter_recurse(iter, &array);
1466                 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1467
1468                 if (ssid_len > 0 && ssid_len < 33) {
1469                         memcpy(bss->ssid, ssid, ssid_len);
1470                         bss->ssid_len = ssid_len;
1471                 } else {
1472                         memset(bss->ssid, 0, sizeof(bss->ssid));
1473                         bss->ssid_len = 0;
1474                 }
1475         } else if (g_strcmp0(key, "Capabilities") == 0) {
1476                 dbus_uint16_t capabilities = 0x0000;
1477
1478                 dbus_message_iter_get_basic(iter, &capabilities);
1479
1480                 if (capabilities & IEEE80211_CAP_ESS)
1481                         bss->mode = G_SUPPLICANT_MODE_INFRA;
1482                 else if (capabilities & IEEE80211_CAP_IBSS)
1483                         bss->mode = G_SUPPLICANT_MODE_IBSS;
1484
1485                 if (capabilities & IEEE80211_CAP_PRIVACY)
1486                         bss->privacy = TRUE;
1487         } else if (g_strcmp0(key, "Mode") == 0) {
1488                 const char *mode = NULL;
1489
1490                 dbus_message_iter_get_basic(iter, &mode);
1491                 bss->mode = string2mode(mode);
1492         } else if (g_strcmp0(key, "Frequency") == 0) {
1493                 dbus_uint16_t frequency = 0;
1494
1495                 dbus_message_iter_get_basic(iter, &frequency);
1496                 bss->frequency = frequency;
1497         } else if (g_strcmp0(key, "Signal") == 0) {
1498                 dbus_int16_t signal = 0;
1499
1500                 dbus_message_iter_get_basic(iter, &signal);
1501
1502                 bss->signal = signal;
1503         } else if (g_strcmp0(key, "Level") == 0) {
1504                 dbus_int32_t level = 0;
1505
1506                 dbus_message_iter_get_basic(iter, &level);
1507         } else if (g_strcmp0(key, "Rates") == 0) {
1508                 supplicant_dbus_array_foreach(iter, bss_rates, bss);
1509         } else if (g_strcmp0(key, "MaxRate") == 0) {
1510                 dbus_uint32_t maxrate = 0;
1511
1512                 dbus_message_iter_get_basic(iter, &maxrate);
1513                 if (maxrate != 0)
1514                         bss->maxrate = maxrate;
1515         } else if (g_strcmp0(key, "Privacy") == 0) {
1516                 dbus_bool_t privacy = FALSE;
1517
1518                 dbus_message_iter_get_basic(iter, &privacy);
1519                 bss->privacy = privacy;
1520         } else if (g_strcmp0(key, "RSN") == 0) {
1521                 bss->rsn_selected = TRUE;
1522
1523                 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1524         } else if (g_strcmp0(key, "WPA") == 0) {
1525                 bss->rsn_selected = FALSE;
1526
1527                 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1528         } else if (g_strcmp0(key, "IEs") == 0)
1529                 bss_process_ies(iter, bss);
1530         else
1531                 SUPPLICANT_DBG("key %s type %c",
1532                                 key, dbus_message_iter_get_arg_type(iter));
1533 }
1534
1535 static struct g_supplicant_bss *interface_bss_added(DBusMessageIter *iter,
1536                                                         void *user_data)
1537 {
1538         GSupplicantInterface *interface = user_data;
1539         GSupplicantNetwork *network;
1540         struct g_supplicant_bss *bss;
1541         const char *path = NULL;
1542
1543         SUPPLICANT_DBG("");
1544
1545         dbus_message_iter_get_basic(iter, &path);
1546         if (path == NULL)
1547                 return NULL;
1548
1549         if (g_strcmp0(path, "/") == 0)
1550                 return NULL;
1551
1552         SUPPLICANT_DBG("%s", path);
1553
1554         network = g_hash_table_lookup(interface->bss_mapping, path);
1555         if (network != NULL) {
1556                 bss = g_hash_table_lookup(network->bss_table, path);
1557                 if (bss != NULL)
1558                         return NULL;
1559         }
1560
1561         bss = g_try_new0(struct g_supplicant_bss, 1);
1562         if (bss == NULL)
1563                 return NULL;
1564
1565         bss->interface = interface;
1566         bss->path = g_strdup(path);
1567
1568         return bss;
1569 }
1570
1571 static void interface_bss_added_with_keys(DBusMessageIter *iter,
1572                                                 void *user_data)
1573 {
1574         struct g_supplicant_bss *bss;
1575
1576         SUPPLICANT_DBG("");
1577
1578         bss = interface_bss_added(iter, user_data);
1579         if (bss == NULL)
1580                 return;
1581
1582         dbus_message_iter_next(iter);
1583
1584         if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_INVALID)
1585                 return;
1586
1587         supplicant_dbus_property_foreach(iter, bss_property, bss);
1588
1589         bss_compute_security(bss);
1590         add_or_replace_bss_to_network(bss);
1591 }
1592
1593 static void interface_bss_added_without_keys(DBusMessageIter *iter,
1594                                                 void *user_data)
1595 {
1596         struct g_supplicant_bss *bss;
1597
1598         SUPPLICANT_DBG("");
1599
1600         bss = interface_bss_added(iter, user_data);
1601         if (bss == NULL)
1602                 return;
1603
1604         supplicant_dbus_property_get_all(bss->path,
1605                                         SUPPLICANT_INTERFACE ".BSS",
1606                                                         bss_property, bss);
1607
1608         bss_compute_security(bss);
1609         add_or_replace_bss_to_network(bss);
1610 }
1611
1612 static void update_signal(gpointer key, gpointer value,
1613                                                 gpointer user_data)
1614 {
1615         struct g_supplicant_bss *bss = value;
1616         GSupplicantNetwork *network = user_data;
1617
1618         if (bss->signal > network->signal) {
1619                 network->signal = bss->signal;
1620                 network->best_bss = bss;
1621         }
1622 }
1623
1624 static void update_network_signal(GSupplicantNetwork *network)
1625 {
1626         if (g_hash_table_size(network->bss_table) <= 1)
1627                 return;
1628
1629         g_hash_table_foreach(network->bss_table,
1630                                 update_signal, network);
1631
1632         SUPPLICANT_DBG("New network signal %d", network->signal);
1633 }
1634
1635 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
1636 {
1637         GSupplicantInterface *interface = user_data;
1638         GSupplicantNetwork *network;
1639         const char *path = NULL;
1640
1641         dbus_message_iter_get_basic(iter, &path);
1642         if (path == NULL)
1643                 return;
1644
1645         network = g_hash_table_lookup(interface->bss_mapping, path);
1646         if (network == NULL)
1647                 return;
1648
1649         g_hash_table_remove(bss_mapping, path);
1650
1651         g_hash_table_remove(interface->bss_mapping, path);
1652         g_hash_table_remove(network->bss_table, path);
1653
1654         update_network_signal(network);
1655
1656         if (g_hash_table_size(network->bss_table) == 0)
1657                 g_hash_table_remove(interface->network_table, network->group);
1658 }
1659
1660 static void interface_property(const char *key, DBusMessageIter *iter,
1661                                                         void *user_data)
1662 {
1663         GSupplicantInterface *interface = user_data;
1664
1665         if (interface == NULL)
1666                 return;
1667
1668         SUPPLICANT_DBG("%s", key);
1669
1670         if (key == NULL) {
1671                 debug_strvalmap("KeyMgmt capability", keymgmt_map,
1672                                                 interface->keymgmt_capa);
1673                 debug_strvalmap("AuthAlg capability", authalg_capa_map,
1674                                                 interface->authalg_capa);
1675                 debug_strvalmap("Protocol capability", proto_capa_map,
1676                                                 interface->proto_capa);
1677                 debug_strvalmap("Pairwise capability", pairwise_map,
1678                                                 interface->pairwise_capa);
1679                 debug_strvalmap("Group capability", group_map,
1680                                                 interface->group_capa);
1681                 debug_strvalmap("Scan capability", scan_capa_map,
1682                                                 interface->scan_capa);
1683                 debug_strvalmap("Mode capability", mode_capa_map,
1684                                                 interface->mode_capa);
1685
1686                 callback_interface_added(interface);
1687                 return;
1688         }
1689
1690         if (g_strcmp0(key, "Capabilities") == 0) {
1691                 supplicant_dbus_property_foreach(iter, interface_capability,
1692                                                                 interface);
1693         } else if (g_strcmp0(key, "State") == 0) {
1694                 const char *str = NULL;
1695
1696                 dbus_message_iter_get_basic(iter, &str);
1697                 if (str != NULL)
1698                         if (string2state(str) != interface->state) {
1699                                 interface->state = string2state(str);
1700                                 callback_interface_state(interface);
1701                         }
1702                 if (interface->state == G_SUPPLICANT_STATE_DISABLED)
1703                         interface->ready = FALSE;
1704                 else
1705                         interface->ready = TRUE;
1706
1707                 SUPPLICANT_DBG("state %s (%d)", str, interface->state);
1708         } else if (g_strcmp0(key, "Scanning") == 0) {
1709                 dbus_bool_t scanning = FALSE;
1710
1711                 dbus_message_iter_get_basic(iter, &scanning);
1712                 interface->scanning = scanning;
1713
1714                 if (interface->ready == TRUE) {
1715                         if (interface->scanning == TRUE)
1716                                 callback_scan_started(interface);
1717                         else
1718                                 callback_scan_finished(interface);
1719                 }
1720         } else if (g_strcmp0(key, "ApScan") == 0) {
1721                 int apscan = 1;
1722
1723                 dbus_message_iter_get_basic(iter, &apscan);
1724                 interface->apscan = apscan;
1725         } else if (g_strcmp0(key, "Ifname") == 0) {
1726                 const char *str = NULL;
1727
1728                 dbus_message_iter_get_basic(iter, &str);
1729                 if (str != NULL) {
1730                         g_free(interface->ifname);
1731                         interface->ifname = g_strdup(str);
1732                 }
1733         } else if (g_strcmp0(key, "Driver") == 0) {
1734                 const char *str = NULL;
1735
1736                 dbus_message_iter_get_basic(iter, &str);
1737                 if (str != NULL) {
1738                         g_free(interface->driver);
1739                         interface->driver = g_strdup(str);
1740                 }
1741         } else if (g_strcmp0(key, "BridgeIfname") == 0) {
1742                 const char *str = NULL;
1743
1744                 dbus_message_iter_get_basic(iter, &str);
1745                 if (str != NULL) {
1746                         g_free(interface->bridge);
1747                         interface->bridge = g_strdup(str);
1748                 }
1749         } else if (g_strcmp0(key, "CurrentBSS") == 0) {
1750                 interface_bss_added_without_keys(iter, interface);
1751         } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
1752                 interface_network_added(iter, interface);
1753         } else if (g_strcmp0(key, "BSSs") == 0) {
1754                 supplicant_dbus_array_foreach(iter, interface_bss_added_without_keys,
1755                                                                 interface);
1756         } else if (g_strcmp0(key, "Blobs") == 0) {
1757                 /* Nothing */
1758         } else if (g_strcmp0(key, "Networks") == 0) {
1759                 supplicant_dbus_array_foreach(iter, interface_network_added,
1760                                                                 interface);
1761         } else
1762                 SUPPLICANT_DBG("key %s type %c",
1763                                 key, dbus_message_iter_get_arg_type(iter));
1764 }
1765
1766 static void scan_network_update(DBusMessageIter *iter, void *user_data)
1767 {
1768         GSupplicantInterface *interface = user_data;
1769         GSupplicantNetwork *network;
1770         char *path;
1771
1772         if (iter == NULL)
1773                 return;
1774
1775         dbus_message_iter_get_basic(iter, &path);
1776
1777         if (path == NULL)
1778                 return;
1779
1780         if (g_strcmp0(path, "/") == 0)
1781                 return;
1782
1783         /* Update the network details based on scan BSS data */
1784         network = g_hash_table_lookup(interface->bss_mapping, path);
1785         if (network != NULL)
1786                 callback_network_added(network);
1787 }
1788
1789 static void scan_bss_data(const char *key, DBusMessageIter *iter,
1790                                 void *user_data)
1791 {
1792         GSupplicantInterface *interface = user_data;
1793
1794         if (iter)
1795                 supplicant_dbus_array_foreach(iter, scan_network_update,
1796                                                 interface);
1797
1798         if (interface->scan_callback != NULL)
1799                 interface->scan_callback(0, interface, interface->scan_data);
1800
1801         interface->scan_callback = NULL;
1802         interface->scan_data = NULL;
1803 }
1804
1805 static GSupplicantInterface *interface_alloc(const char *path)
1806 {
1807         GSupplicantInterface *interface;
1808
1809         interface = g_try_new0(GSupplicantInterface, 1);
1810         if (interface == NULL)
1811                 return NULL;
1812
1813         interface->path = g_strdup(path);
1814
1815         interface->network_table = g_hash_table_new_full(g_str_hash,
1816                                         g_str_equal, NULL, remove_network);
1817
1818         interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1819                                                                 NULL, NULL);
1820         interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1821                                                                 NULL, NULL);
1822
1823         g_hash_table_replace(interface_table, interface->path, interface);
1824
1825         return interface;
1826 }
1827
1828 static void interface_added(DBusMessageIter *iter, void *user_data)
1829 {
1830         GSupplicantInterface *interface;
1831         const char *path = NULL;
1832
1833         SUPPLICANT_DBG("");
1834
1835         dbus_message_iter_get_basic(iter, &path);
1836         if (path == NULL)
1837                 return;
1838
1839         if (g_strcmp0(path, "/") == 0)
1840                 return;
1841
1842         interface = g_hash_table_lookup(interface_table, path);
1843         if (interface != NULL)
1844                 return;
1845
1846         interface = interface_alloc(path);
1847         if (interface == NULL)
1848                 return;
1849
1850         dbus_message_iter_next(iter);
1851         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1852                 supplicant_dbus_property_foreach(iter, interface_property,
1853                                                                 interface);
1854                 interface_property(NULL, NULL, interface);
1855                 return;
1856         }
1857
1858         supplicant_dbus_property_get_all(path,
1859                                         SUPPLICANT_INTERFACE ".Interface",
1860                                                 interface_property, interface);
1861 }
1862
1863 static void interface_removed(DBusMessageIter *iter, void *user_data)
1864 {
1865         const char *path = NULL;
1866
1867         dbus_message_iter_get_basic(iter, &path);
1868         if (path == NULL)
1869                 return;
1870
1871         g_hash_table_remove(interface_table, path);
1872 }
1873
1874 static void eap_method(DBusMessageIter *iter, void *user_data)
1875 {
1876         const char *str = NULL;
1877         int i;
1878
1879         dbus_message_iter_get_basic(iter, &str);
1880         if (str == NULL)
1881                 return;
1882
1883         for (i = 0; eap_method_map[i].str != NULL; i++)
1884                 if (strcmp(str, eap_method_map[i].str) == 0) {
1885                         eap_methods |= eap_method_map[i].val;
1886                         break;
1887                 }
1888 }
1889
1890 static void service_property(const char *key, DBusMessageIter *iter,
1891                                                         void *user_data)
1892 {
1893         if (key == NULL) {
1894                 callback_system_ready();
1895                 return;
1896         }
1897
1898         if (g_strcmp0(key, "DebugLevel") == 0) {
1899                 const char *str = NULL;
1900                 int i;
1901
1902                 dbus_message_iter_get_basic(iter, &str);
1903                 for (i = 0; debug_strings[i] != NULL; i++)
1904                         if (g_strcmp0(debug_strings[i], str) == 0) {
1905                                 debug_level = i;
1906                                 break;
1907                         }
1908                 SUPPLICANT_DBG("Debug level %d", debug_level);
1909         } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
1910                 dbus_message_iter_get_basic(iter, &debug_timestamp);
1911                 SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp);
1912         } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
1913                 dbus_message_iter_get_basic(iter, &debug_showkeys);
1914                 SUPPLICANT_DBG("Debug show keys %u", debug_showkeys);
1915         } else if (g_strcmp0(key, "Interfaces") == 0) {
1916                 supplicant_dbus_array_foreach(iter, interface_added, NULL);
1917         } else if (g_strcmp0(key, "EapMethods") == 0) {
1918                 supplicant_dbus_array_foreach(iter, eap_method, NULL);
1919                 debug_strvalmap("EAP method", eap_method_map, eap_methods);
1920         } else if (g_strcmp0(key, "Country") == 0) {
1921                 const char *country = NULL;
1922
1923                 dbus_message_iter_get_basic(iter, &country);
1924                 SUPPLICANT_DBG("Country %s", country);
1925         } else
1926                 SUPPLICANT_DBG("key %s type %c",
1927                                 key, dbus_message_iter_get_arg_type(iter));
1928 }
1929
1930 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
1931 {
1932         const char *name = NULL, *old = NULL, *new = NULL;
1933
1934         SUPPLICANT_DBG("");
1935
1936         if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
1937                 return;
1938
1939         dbus_message_iter_get_basic(iter, &name);
1940         if (name == NULL)
1941                 return;
1942
1943         if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
1944                 return;
1945
1946         dbus_message_iter_next(iter);
1947         dbus_message_iter_get_basic(iter, &old);
1948         dbus_message_iter_next(iter);
1949         dbus_message_iter_get_basic(iter, &new);
1950
1951         if (old == NULL || new == NULL)
1952                 return;
1953
1954         if (strlen(old) > 0 && strlen(new) == 0) {
1955                 system_available = FALSE;
1956                 g_hash_table_remove_all(bss_mapping);
1957                 g_hash_table_remove_all(interface_table);
1958                 callback_system_killed();
1959         }
1960
1961         if (strlen(new) > 0 && strlen(old) == 0) {
1962                 system_available = TRUE;
1963                 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
1964                                                         SUPPLICANT_INTERFACE,
1965                                                         service_property, NULL);
1966         }
1967 }
1968
1969 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
1970 {
1971         SUPPLICANT_DBG("");
1972
1973         if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
1974                 return;
1975
1976         supplicant_dbus_property_foreach(iter, service_property, NULL);
1977 }
1978
1979 static void signal_interface_added(const char *path, DBusMessageIter *iter)
1980 {
1981         SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
1982
1983         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1984                 interface_added(iter, NULL);
1985 }
1986
1987 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
1988 {
1989         SUPPLICANT_DBG("");
1990
1991         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1992                 interface_removed(iter, NULL);
1993 }
1994
1995 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
1996 {
1997         GSupplicantInterface *interface;
1998
1999         SUPPLICANT_DBG("");
2000
2001         interface = g_hash_table_lookup(interface_table, path);
2002         if (interface == NULL)
2003                 return;
2004
2005         supplicant_dbus_property_foreach(iter, interface_property, interface);
2006 }
2007
2008 static void signal_scan_done(const char *path, DBusMessageIter *iter)
2009 {
2010         GSupplicantInterface *interface;
2011         dbus_bool_t success = FALSE;
2012
2013         SUPPLICANT_DBG("");
2014
2015         interface = g_hash_table_lookup(interface_table, path);
2016         if (interface == NULL)
2017                 return;
2018
2019         dbus_message_iter_get_basic(iter, &success);
2020
2021         /*
2022          * If scan is unsuccessful return -EIO else get the scanned BSSs
2023          * and update the network details accordingly
2024          */
2025         if (success == FALSE) {
2026                 if (interface->scan_callback != NULL)
2027                         interface->scan_callback(-EIO, interface,
2028                                                 interface->scan_data);
2029
2030                 interface->scan_callback = NULL;
2031                 interface->scan_data = NULL;
2032
2033                 return;
2034         }
2035
2036         supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface",
2037                                         "BSSs", scan_bss_data, interface);
2038 }
2039
2040 static void signal_bss_added(const char *path, DBusMessageIter *iter)
2041 {
2042         GSupplicantInterface *interface;
2043
2044         SUPPLICANT_DBG("");
2045
2046         interface = g_hash_table_lookup(interface_table, path);
2047         if (interface == NULL)
2048                 return;
2049
2050         interface_bss_added_with_keys(iter, interface);
2051 }
2052
2053 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
2054 {
2055         GSupplicantInterface *interface;
2056
2057         SUPPLICANT_DBG("");
2058
2059         interface = g_hash_table_lookup(interface_table, path);
2060         if (interface == NULL)
2061                 return;
2062
2063         interface_bss_removed(iter, interface);
2064 }
2065
2066 static void signal_network_added(const char *path, DBusMessageIter *iter)
2067 {
2068         GSupplicantInterface *interface;
2069
2070         SUPPLICANT_DBG("");
2071
2072         interface = g_hash_table_lookup(interface_table, path);
2073         if (interface == NULL)
2074                 return;
2075
2076         interface_network_added(iter, interface);
2077 }
2078
2079 static void signal_network_removed(const char *path, DBusMessageIter *iter)
2080 {
2081         GSupplicantInterface *interface;
2082
2083         SUPPLICANT_DBG("");
2084
2085         interface = g_hash_table_lookup(interface_table, path);
2086         if (interface == NULL)
2087                 return;
2088
2089         interface_network_removed(iter, interface);
2090 }
2091
2092 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
2093 {
2094         GSupplicantInterface *interface;
2095         GSupplicantNetwork *network;
2096         GSupplicantSecurity old_security;
2097         struct g_supplicant_bss *bss;
2098
2099         SUPPLICANT_DBG("");
2100
2101         interface = g_hash_table_lookup(bss_mapping, path);
2102         if (interface == NULL)
2103                 return;
2104
2105         network = g_hash_table_lookup(interface->bss_mapping, path);
2106         if (network == NULL)
2107                 return;
2108
2109         bss = g_hash_table_lookup(network->bss_table, path);
2110         if (bss == NULL)
2111                 return;
2112
2113         supplicant_dbus_property_foreach(iter, bss_property, bss);
2114
2115         old_security = network->security;
2116         bss_compute_security(bss);
2117
2118         if (old_security != bss->security) {
2119                 struct g_supplicant_bss *new_bss;
2120
2121                 SUPPLICANT_DBG("New network security for %s", bss->ssid);
2122
2123                 /* Security change policy:
2124                  * - we first copy the current bss into a new one with
2125                  * its own pointer (path)
2126                  * - we remove the current bss related network which will
2127                  * tell the plugin about such removal. This is done due
2128                  * to the fact that a security change means a group change
2129                  * so a complete network change.
2130                  * (current bss becomes invalid as well)
2131                  * - we add the new bss: it adds new network and tell the
2132                  * plugin about it. */
2133
2134                 new_bss = g_try_new0(struct g_supplicant_bss, 1);
2135                 if (new_bss == NULL)
2136                         return;
2137
2138                 memcpy(new_bss, bss, sizeof(struct g_supplicant_bss));
2139                 new_bss->path = g_strdup(bss->path);
2140
2141                 g_hash_table_remove(interface->network_table, network->group);
2142
2143                 add_or_replace_bss_to_network(new_bss);
2144
2145                 return;
2146         }
2147
2148         if (bss->signal == network->signal)
2149                 return;
2150
2151         /*
2152          * If the new signal is lower than the SSID signal, we need
2153          * to check for the new maximum.
2154          */
2155         if (bss->signal < network->signal) {
2156                 if (bss != network->best_bss)
2157                         return;
2158                 network->signal = bss->signal;
2159                 update_network_signal(network);
2160         } else {
2161                 network->signal = bss->signal;
2162                 network->best_bss = bss;
2163         }
2164
2165         SUPPLICANT_DBG("New network signal for %s %d dBm", network->ssid, network->signal);
2166
2167         callback_network_changed(network, "Signal");
2168 }
2169
2170 static void wps_credentials(const char *key, DBusMessageIter *iter,
2171                         void *user_data)
2172 {
2173         GSupplicantInterface *interface = user_data;
2174
2175         if (key == NULL)
2176                 return;
2177
2178         SUPPLICANT_DBG("key %s", key);
2179
2180         if (g_strcmp0(key, "Key") == 0) {
2181                 DBusMessageIter array;
2182                 unsigned char *key_val;
2183                 int key_len;
2184
2185                 dbus_message_iter_recurse(iter, &array);
2186                 dbus_message_iter_get_fixed_array(&array, &key_val, &key_len);
2187
2188                 g_free(interface->wps_cred.key);
2189                 interface->wps_cred.key = g_try_malloc0(
2190                                                 sizeof(char) * key_len + 1);
2191
2192                 if (interface->wps_cred.key == NULL)
2193                         return;
2194
2195                 memcpy(interface->wps_cred.key, key_val,
2196                                                 sizeof(char) * key_len);
2197
2198                 SUPPLICANT_DBG("WPS key present");
2199         } else if (g_strcmp0(key, "SSID") == 0) {
2200                 DBusMessageIter array;
2201                 unsigned char *ssid;
2202                 int ssid_len;
2203
2204                 dbus_message_iter_recurse(iter, &array);
2205                 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
2206
2207                 if (ssid_len > 0 && ssid_len < 33) {
2208                         memcpy(interface->wps_cred.ssid, ssid, ssid_len);
2209                         interface->wps_cred.ssid_len = ssid_len;
2210                 } else {
2211                         memset(interface->wps_cred.ssid, 0, 32);
2212                         interface->wps_cred.ssid_len = 0;
2213                 }
2214         }
2215 }
2216
2217 static void signal_wps_credentials(const char *path, DBusMessageIter *iter)
2218 {
2219         GSupplicantInterface *interface;
2220
2221         SUPPLICANT_DBG("");
2222
2223         interface = g_hash_table_lookup(interface_table, path);
2224         if (interface == NULL)
2225                 return;
2226
2227         supplicant_dbus_property_foreach(iter, wps_credentials, interface);
2228 }
2229
2230 static void wps_event_args(const char *key, DBusMessageIter *iter,
2231                         void *user_data)
2232 {
2233         GSupplicantInterface *interface = user_data;
2234
2235         if (key == NULL || interface == NULL)
2236                 return;
2237
2238         SUPPLICANT_DBG("Arg Key %s", key);
2239 }
2240
2241 static void signal_wps_event(const char *path, DBusMessageIter *iter)
2242 {
2243         GSupplicantInterface *interface;
2244         const char *name = NULL;
2245
2246         SUPPLICANT_DBG("");
2247
2248         interface = g_hash_table_lookup(interface_table, path);
2249         if (interface == NULL)
2250                 return;
2251
2252         dbus_message_iter_get_basic(iter, &name);
2253
2254         SUPPLICANT_DBG("Name: %s", name);
2255
2256         if (g_strcmp0(name, "success") == 0)
2257                 interface->wps_state = G_SUPPLICANT_WPS_STATE_SUCCESS;
2258         else if (g_strcmp0(name, "failed") == 0)
2259                 interface->wps_state = G_SUPPLICANT_WPS_STATE_FAIL;
2260         else
2261                 interface->wps_state = G_SUPPLICANT_WPS_STATE_UNKNOWN;
2262
2263         if (!dbus_message_iter_has_next(iter))
2264                 return;
2265
2266         dbus_message_iter_next(iter);
2267
2268         supplicant_dbus_property_foreach(iter, wps_event_args, interface);
2269 }
2270
2271 static struct {
2272         const char *interface;
2273         const char *member;
2274         void (*function) (const char *path, DBusMessageIter *iter);
2275 } signal_map[] = {
2276         { DBUS_INTERFACE_DBUS,  "NameOwnerChanged",  signal_name_owner_changed },
2277
2278         { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
2279         { SUPPLICANT_INTERFACE, "InterfaceAdded",    signal_interface_added    },
2280         { SUPPLICANT_INTERFACE, "InterfaceCreated",  signal_interface_added    },
2281         { SUPPLICANT_INTERFACE, "InterfaceRemoved",  signal_interface_removed  },
2282
2283         { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
2284         { SUPPLICANT_INTERFACE ".Interface", "ScanDone",          signal_scan_done         },
2285         { SUPPLICANT_INTERFACE ".Interface", "BSSAdded",          signal_bss_added         },
2286         { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved",        signal_bss_removed       },
2287         { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded",      signal_network_added     },
2288         { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved",    signal_network_removed   },
2289
2290         { SUPPLICANT_INTERFACE ".BSS", "PropertiesChanged", signal_bss_changed   },
2291
2292         { SUPPLICANT_INTERFACE ".Interface.WPS", "Credentials", signal_wps_credentials },
2293         { SUPPLICANT_INTERFACE ".Interface.WPS", "Event",       signal_wps_event       },
2294
2295         { }
2296 };
2297
2298 static DBusHandlerResult g_supplicant_filter(DBusConnection *conn,
2299                                         DBusMessage *message, void *data)
2300 {
2301         DBusMessageIter iter;
2302         const char *path;
2303         int i;
2304
2305         path = dbus_message_get_path(message);
2306         if (path == NULL)
2307                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2308
2309         if (dbus_message_iter_init(message, &iter) == FALSE)
2310                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2311
2312         for (i = 0; signal_map[i].interface != NULL; i++) {
2313                 if (dbus_message_has_interface(message,
2314                                         signal_map[i].interface) == FALSE)
2315                         continue;
2316
2317                 if (dbus_message_has_member(message,
2318                                         signal_map[i].member) == FALSE)
2319                         continue;
2320
2321                 signal_map[i].function(path, &iter);
2322                 break;
2323         }
2324
2325         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
2326 }
2327
2328 struct supplicant_regdom {
2329         GSupplicantCountryCallback callback;
2330         const char *alpha2;
2331         const void *user_data;
2332 };
2333
2334 static void country_result(const char *error,
2335                                 DBusMessageIter *iter, void *user_data)
2336 {
2337         struct supplicant_regdom *regdom = user_data;
2338         int result = 0;
2339
2340         SUPPLICANT_DBG("Country setting result");
2341
2342         if (user_data == NULL)
2343                 return;
2344
2345         if (error != NULL) {
2346                 SUPPLICANT_DBG("Country setting failure %s", error);
2347                 result = -EINVAL;
2348         }
2349
2350         if (regdom->callback)
2351                 regdom->callback(result, regdom->alpha2,
2352                                         (void *) regdom->user_data);
2353
2354         g_free(regdom);
2355 }
2356
2357 static void country_params(DBusMessageIter *iter, void *user_data)
2358 {
2359         struct supplicant_regdom *regdom = user_data;
2360
2361         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
2362                                                         &regdom->alpha2);
2363 }
2364
2365 int g_supplicant_set_country(const char *alpha2,
2366                                 GSupplicantCountryCallback callback,
2367                                         const void *user_data)
2368 {
2369         struct supplicant_regdom *regdom;
2370
2371         SUPPLICANT_DBG("Country setting %s", alpha2);
2372
2373         if (system_available == FALSE)
2374                 return -EFAULT;
2375
2376         regdom = dbus_malloc0(sizeof(*regdom));
2377         if (regdom == NULL)
2378                 return -ENOMEM;
2379
2380         regdom->callback = callback;
2381         regdom->alpha2 = alpha2;
2382         regdom->user_data = user_data;
2383
2384         return supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
2385                                         "Country", DBUS_TYPE_STRING_AS_STRING,
2386                                         country_params, country_result,
2387                                                 regdom);
2388 }
2389
2390 int g_supplicant_interface_set_country(GSupplicantInterface *interface,
2391                                         GSupplicantCountryCallback callback,
2392                                                         const char *alpha2,
2393                                                         void *user_data)
2394 {
2395         struct supplicant_regdom *regdom;
2396
2397         regdom = dbus_malloc0(sizeof(*regdom));
2398         if (regdom == NULL)
2399                 return -ENOMEM;
2400
2401         regdom->callback = callback;
2402         regdom->alpha2 = alpha2;
2403         regdom->user_data = user_data;
2404
2405         return supplicant_dbus_property_set(interface->path,
2406                                 SUPPLICANT_INTERFACE ".Interface",
2407                                 "Country", DBUS_TYPE_STRING_AS_STRING,
2408                                 country_params, country_result,
2409                                         regdom);
2410 }
2411
2412 struct interface_data {
2413         GSupplicantInterface *interface;
2414         GSupplicantInterfaceCallback callback;
2415         void *user_data;
2416 };
2417
2418 struct interface_create_data {
2419         const char *ifname;
2420         const char *driver;
2421         const char *bridge;
2422         GSupplicantInterface *interface;
2423         GSupplicantInterfaceCallback callback;
2424         void *user_data;
2425 };
2426
2427 struct interface_connect_data {
2428         GSupplicantInterface *interface;
2429         GSupplicantInterfaceCallback callback;
2430         GSupplicantSSID *ssid;
2431         void *user_data;
2432 };
2433
2434 struct interface_scan_data {
2435         GSupplicantInterface *interface;
2436         GSupplicantInterfaceCallback callback;
2437         GSupplicantScanParams *scan_params;
2438         void *user_data;
2439 };
2440
2441 struct interface_autoscan_data {
2442         GSupplicantInterface *interface;
2443         GSupplicantInterfaceCallback callback;
2444         const char *autoscan_params;
2445         void *user_data;
2446 };
2447
2448 static void interface_create_property(const char *key, DBusMessageIter *iter,
2449                                                         void *user_data)
2450 {
2451         struct interface_create_data *data = user_data;
2452         GSupplicantInterface *interface = data->interface;
2453
2454         if (key == NULL) {
2455                 if (data->callback != NULL)
2456                         data->callback(0, data->interface, data->user_data);
2457
2458                 dbus_free(data);
2459         }
2460
2461         interface_property(key, iter, interface);
2462 }
2463
2464 static void interface_create_result(const char *error,
2465                                 DBusMessageIter *iter, void *user_data)
2466 {
2467         struct interface_create_data *data = user_data;
2468         const char *path = NULL;
2469         int err;
2470
2471         SUPPLICANT_DBG("");
2472
2473         if (error != NULL) {
2474                 g_warning("error %s", error);
2475                 err = -EIO;
2476                 goto done;
2477         }
2478
2479         dbus_message_iter_get_basic(iter, &path);
2480         if (path == NULL) {
2481                 err = -EINVAL;
2482                 goto done;
2483         }
2484
2485         if (system_available == FALSE) {
2486                 err = -EFAULT;
2487                 goto done;
2488         }
2489
2490         data->interface = g_hash_table_lookup(interface_table, path);
2491         if (data->interface == NULL) {
2492                 data->interface = interface_alloc(path);
2493                 if (data->interface == NULL) {
2494                         err = -ENOMEM;
2495                         goto done;
2496                 }
2497         }
2498
2499         err = supplicant_dbus_property_get_all(path,
2500                                         SUPPLICANT_INTERFACE ".Interface",
2501                                         interface_create_property, data);
2502         if (err == 0)
2503                 return;
2504
2505 done:
2506         if (data->callback != NULL)
2507                 data->callback(err, NULL, data->user_data);
2508
2509         dbus_free(data);
2510 }
2511
2512 static void interface_create_params(DBusMessageIter *iter, void *user_data)
2513 {
2514         struct interface_create_data *data = user_data;
2515         DBusMessageIter dict;
2516
2517         SUPPLICANT_DBG("");
2518
2519         supplicant_dbus_dict_open(iter, &dict);
2520
2521         supplicant_dbus_dict_append_basic(&dict, "Ifname",
2522                                         DBUS_TYPE_STRING, &data->ifname);
2523
2524         if (data->driver != NULL)
2525                 supplicant_dbus_dict_append_basic(&dict, "Driver",
2526                                         DBUS_TYPE_STRING, &data->driver);
2527
2528         if (data->bridge != NULL)
2529                 supplicant_dbus_dict_append_basic(&dict, "BridgeIfname",
2530                                         DBUS_TYPE_STRING, &data->bridge);
2531
2532         supplicant_dbus_dict_close(iter, &dict);
2533 }
2534
2535 static void interface_get_result(const char *error,
2536                                 DBusMessageIter *iter, void *user_data)
2537 {
2538         struct interface_create_data *data = user_data;
2539         GSupplicantInterface *interface;
2540         const char *path = NULL;
2541         int err;
2542
2543         SUPPLICANT_DBG("");
2544
2545         if (error != NULL) {
2546                 SUPPLICANT_DBG("Interface not created yet");
2547                 goto create;
2548         }
2549
2550         dbus_message_iter_get_basic(iter, &path);
2551         if (path == NULL) {
2552                 err = -EINVAL;
2553                 goto done;
2554         }
2555
2556         interface = g_hash_table_lookup(interface_table, path);
2557         if (interface == NULL) {
2558                 err = -ENOENT;
2559                 goto done;
2560         }
2561
2562         if (data->callback != NULL)
2563                 data->callback(0, interface, data->user_data);
2564
2565         dbus_free(data);
2566
2567         return;
2568
2569 create:
2570         if (system_available == FALSE) {
2571                 err = -EFAULT;
2572                 goto done;
2573         }
2574
2575         SUPPLICANT_DBG("Creating interface");
2576
2577         err = supplicant_dbus_method_call(SUPPLICANT_PATH,
2578                                                 SUPPLICANT_INTERFACE,
2579                                                 "CreateInterface",
2580                                                 interface_create_params,
2581                                                 interface_create_result, data);
2582         if (err == 0)
2583                 return;
2584
2585 done:
2586         if (data->callback != NULL)
2587                 data->callback(err, NULL, data->user_data);
2588
2589         dbus_free(data);
2590 }
2591
2592 static void interface_get_params(DBusMessageIter *iter, void *user_data)
2593 {
2594         struct interface_create_data *data = user_data;
2595
2596         SUPPLICANT_DBG("");
2597
2598         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
2599 }
2600
2601 int g_supplicant_interface_create(const char *ifname, const char *driver,
2602                                         const char *bridge,
2603                                         GSupplicantInterfaceCallback callback,
2604                                                         void *user_data)
2605 {
2606         struct interface_create_data *data;
2607
2608         SUPPLICANT_DBG("ifname %s", ifname);
2609
2610         if (ifname == NULL)
2611                 return -EINVAL;
2612
2613         if (system_available == FALSE)
2614                 return -EFAULT;
2615
2616         data = dbus_malloc0(sizeof(*data));
2617         if (data == NULL)
2618                 return -ENOMEM;
2619
2620         data->ifname = ifname;
2621         data->driver = driver;
2622         data->bridge = bridge;
2623         data->callback = callback;
2624         data->user_data = user_data;
2625
2626         return supplicant_dbus_method_call(SUPPLICANT_PATH,
2627                                                 SUPPLICANT_INTERFACE,
2628                                                 "GetInterface",
2629                                                 interface_get_params,
2630                                                 interface_get_result, data);
2631 }
2632
2633 static void interface_remove_result(const char *error,
2634                                 DBusMessageIter *iter, void *user_data)
2635 {
2636         struct interface_data *data = user_data;
2637         int err;
2638
2639         if (error != NULL) {
2640                 err = -EIO;
2641                 goto done;
2642         }
2643
2644         if (system_available == FALSE) {
2645                 err = -EFAULT;
2646                 goto done;
2647         }
2648
2649         /*
2650          * The gsupplicant interface is already freed by the InterfaceRemoved
2651          * signal callback. Simply invoke the interface_data callback.
2652          */
2653         err = 0;
2654
2655 done:
2656         if (data->callback != NULL)
2657                 data->callback(err, NULL, data->user_data);
2658
2659         dbus_free(data);
2660 }
2661
2662
2663 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
2664 {
2665         struct interface_data *data = user_data;
2666
2667         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
2668                                                         &data->interface->path);
2669 }
2670
2671
2672 int g_supplicant_interface_remove(GSupplicantInterface *interface,
2673                         GSupplicantInterfaceCallback callback,
2674                                                         void *user_data)
2675 {
2676         struct interface_data *data;
2677
2678         if (interface == NULL)
2679                 return -EINVAL;
2680
2681         if (system_available == FALSE)
2682                 return -EFAULT;
2683
2684         data = dbus_malloc0(sizeof(*data));
2685         if (data == NULL)
2686                 return -ENOMEM;
2687
2688         data->interface = interface;
2689         data->callback = callback;
2690         data->user_data = user_data;
2691
2692         return supplicant_dbus_method_call(SUPPLICANT_PATH,
2693                                                 SUPPLICANT_INTERFACE,
2694                                                 "RemoveInterface",
2695                                                 interface_remove_params,
2696                                                 interface_remove_result, data);
2697 }
2698
2699 static void interface_scan_result(const char *error,
2700                                 DBusMessageIter *iter, void *user_data)
2701 {
2702         struct interface_scan_data *data = user_data;
2703         int err = 0;
2704
2705         if (error != NULL) {
2706                 SUPPLICANT_DBG("error %s", error);
2707                 err = -EIO;
2708         }
2709
2710         /* A non ready interface cannot send/receive anything */
2711         if (data->interface->ready == FALSE)
2712                 err = -ENOLINK;
2713
2714         if (err != 0) {
2715                 if (data->callback != NULL)
2716                         data->callback(err, data->interface, data->user_data);
2717         } else {
2718                 data->interface->scan_callback = data->callback;
2719                 data->interface->scan_data = data->user_data;
2720         }
2721
2722         if (data != NULL && data->scan_params != NULL)
2723                 g_supplicant_free_scan_params(data->scan_params);
2724
2725         dbus_free(data);
2726 }
2727
2728 static void add_scan_frequency(DBusMessageIter *iter, unsigned int freq)
2729 {
2730         DBusMessageIter data;
2731         unsigned int width = 0; /* Not used by wpa_supplicant atm */
2732
2733         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &data);
2734
2735         dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &freq);
2736         dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &width);
2737
2738         dbus_message_iter_close_container(iter, &data);
2739 }
2740
2741 static void add_scan_frequencies(DBusMessageIter *iter,
2742                                                 void *user_data)
2743 {
2744         GSupplicantScanParams *scan_data = user_data;
2745         unsigned int freq;
2746         int i;
2747
2748         for (i = 0; i < scan_data->num_ssids; i++) {
2749                 freq = scan_data->freqs[i];
2750                 if (!freq)
2751                         break;
2752
2753                 add_scan_frequency(iter, freq);
2754         }
2755 }
2756
2757 static void append_ssid(DBusMessageIter *iter,
2758                         const void *ssid, unsigned int len)
2759 {
2760         DBusMessageIter array;
2761
2762         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
2763         DBUS_TYPE_BYTE_AS_STRING, &array);
2764
2765         dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
2766                                                                 &ssid, len);
2767         dbus_message_iter_close_container(iter, &array);
2768 }
2769
2770 static void append_ssids(DBusMessageIter *iter, void *user_data)
2771 {
2772         GSupplicantScanParams *scan_data = user_data;
2773         GSList *list;
2774
2775         for (list = scan_data->ssids; list; list = list->next) {
2776                 struct scan_ssid *scan_ssid = list->data;
2777
2778                 append_ssid(iter, scan_ssid->ssid, scan_ssid->ssid_len);
2779         }
2780 }
2781
2782 static void supplicant_add_scan_frequency(DBusMessageIter *dict,
2783                 supplicant_dbus_array_function function,
2784                                         void *user_data)
2785 {
2786         GSupplicantScanParams *scan_params = user_data;
2787         DBusMessageIter entry, value, array;
2788         const char *key = "Channels";
2789
2790         if (scan_params->freqs && scan_params->freqs[0] != 0) {
2791                 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
2792                                                 NULL, &entry);
2793
2794                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
2795
2796                 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
2797                                         DBUS_TYPE_ARRAY_AS_STRING
2798                                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
2799                                         DBUS_TYPE_UINT32_AS_STRING
2800                                         DBUS_TYPE_UINT32_AS_STRING
2801                                         DBUS_STRUCT_END_CHAR_AS_STRING,
2802                                         &value);
2803
2804                 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
2805                                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
2806                                         DBUS_TYPE_UINT32_AS_STRING
2807                                         DBUS_TYPE_UINT32_AS_STRING
2808                                         DBUS_STRUCT_END_CHAR_AS_STRING,
2809                                         &array);
2810
2811                 if (function)
2812                         function(&array, user_data);
2813
2814                 dbus_message_iter_close_container(&value, &array);
2815                 dbus_message_iter_close_container(&entry, &value);
2816                 dbus_message_iter_close_container(dict, &entry);
2817         }
2818 }
2819
2820 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
2821 {
2822         DBusMessageIter dict;
2823         const char *type = "passive";
2824         struct interface_scan_data *data = user_data;
2825
2826         supplicant_dbus_dict_open(iter, &dict);
2827
2828         if (data && data->scan_params) {
2829                 type = "active";
2830
2831                 supplicant_dbus_dict_append_basic(&dict, "Type",
2832                                         DBUS_TYPE_STRING, &type);
2833
2834                 supplicant_dbus_dict_append_array(&dict, "SSIDs",
2835                                                 DBUS_TYPE_STRING,
2836                                                 append_ssids,
2837                                                 data->scan_params);
2838
2839                 supplicant_add_scan_frequency(&dict, add_scan_frequencies,
2840                                                 data->scan_params);
2841         } else
2842                 supplicant_dbus_dict_append_basic(&dict, "Type",
2843                                         DBUS_TYPE_STRING, &type);
2844
2845         supplicant_dbus_dict_close(iter, &dict);
2846 }
2847
2848 int g_supplicant_interface_scan(GSupplicantInterface *interface,
2849                                 GSupplicantScanParams *scan_data,
2850                                 GSupplicantInterfaceCallback callback,
2851                                                         void *user_data)
2852 {
2853         struct interface_scan_data *data;
2854         int ret;
2855
2856         if (interface == NULL)
2857                 return -EINVAL;
2858
2859         if (system_available == FALSE)
2860                 return -EFAULT;
2861
2862         if (interface->scanning == TRUE)
2863                 return -EALREADY;
2864
2865         switch (interface->state) {
2866         case G_SUPPLICANT_STATE_AUTHENTICATING:
2867         case G_SUPPLICANT_STATE_ASSOCIATING:
2868         case G_SUPPLICANT_STATE_ASSOCIATED:
2869         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
2870         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
2871                 return -EBUSY;
2872         case G_SUPPLICANT_STATE_UNKNOWN:
2873         case G_SUPPLICANT_STATE_DISABLED:
2874         case G_SUPPLICANT_STATE_DISCONNECTED:
2875         case G_SUPPLICANT_STATE_INACTIVE:
2876         case G_SUPPLICANT_STATE_SCANNING:
2877         case G_SUPPLICANT_STATE_COMPLETED:
2878                 break;
2879         }
2880
2881         data = dbus_malloc0(sizeof(*data));
2882         if (data == NULL)
2883                 return -ENOMEM;
2884
2885         data->interface = interface;
2886         data->callback = callback;
2887         data->user_data = user_data;
2888         data->scan_params = scan_data;
2889
2890         ret = supplicant_dbus_method_call(interface->path,
2891                         SUPPLICANT_INTERFACE ".Interface", "Scan",
2892                         interface_scan_params, interface_scan_result, data);
2893
2894         if (ret < 0)
2895                 dbus_free(data);
2896
2897         return ret;
2898 }
2899
2900 static void interface_autoscan_result(const char *error,
2901                                 DBusMessageIter *iter, void *user_data)
2902 {
2903         struct interface_autoscan_data *data = user_data;
2904         int err = 0;
2905
2906         if (error != NULL) {
2907                 SUPPLICANT_DBG("error %s", error);
2908                 err = -EIO;
2909         }
2910
2911         if (data != NULL && data->callback != NULL)
2912                 data->callback(err, data->interface, data->user_data);
2913
2914         dbus_free(data);
2915 }
2916
2917 static void interface_autoscan_params(DBusMessageIter *iter, void *user_data)
2918 {
2919         struct interface_autoscan_data *data = user_data;
2920
2921         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
2922                                                  &data->autoscan_params);
2923 }
2924
2925 int g_supplicant_interface_autoscan(GSupplicantInterface *interface,
2926                                         const char *autoscan_data,
2927                                         GSupplicantInterfaceCallback callback,
2928                                                         void *user_data)
2929 {
2930         struct interface_autoscan_data *data;
2931         int ret;
2932
2933         data = dbus_malloc0(sizeof(*data));
2934         if (data == NULL)
2935                 return -ENOMEM;
2936
2937         data->interface = interface;
2938         data->callback = callback;
2939         data->autoscan_params = autoscan_data;
2940         data->user_data = user_data;
2941
2942         ret = supplicant_dbus_method_call(interface->path,
2943                         SUPPLICANT_INTERFACE ".Interface", "AutoScan",
2944                         interface_autoscan_params,
2945                         interface_autoscan_result, data);
2946         if (ret < 0)
2947                 dbus_free(data);
2948
2949         return ret;
2950 }
2951
2952 static int parse_supplicant_error(DBusMessageIter *iter)
2953 {
2954         int err = -ECANCELED;
2955         char *key;
2956
2957         /* If the given passphrase is malformed wpa_s returns
2958          * "invalid message format" but this error should be interpreted as
2959          * invalid-key.
2960          */
2961         while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
2962                 dbus_message_iter_get_basic(iter, &key);
2963                 if (strncmp(key, "psk", 3) == 0 ||
2964                                 strncmp(key, "wep_key", 7) == 0 ||
2965                                 strcmp(key, "invalid message format") == 0) {
2966                         err = -ENOKEY;
2967                         break;
2968                 }
2969                 dbus_message_iter_next(iter);
2970         }
2971
2972         return err;
2973 }
2974
2975 static void interface_select_network_result(const char *error,
2976                                 DBusMessageIter *iter, void *user_data)
2977 {
2978         struct interface_connect_data *data = user_data;
2979         int err;
2980
2981         SUPPLICANT_DBG("");
2982
2983         err = 0;
2984         if (error != NULL) {
2985                 SUPPLICANT_DBG("SelectNetwork error %s", error);
2986                 err = parse_supplicant_error(iter);
2987         }
2988
2989         if (data->callback != NULL)
2990                 data->callback(err, data->interface, data->user_data);
2991
2992         g_free(data->ssid);
2993         dbus_free(data);
2994 }
2995
2996 static void interface_select_network_params(DBusMessageIter *iter,
2997                                                         void *user_data)
2998 {
2999         struct interface_connect_data *data = user_data;
3000         GSupplicantInterface *interface = data->interface;
3001
3002         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
3003                                         &interface->network_path);
3004 }
3005
3006 static void interface_add_network_result(const char *error,
3007                                 DBusMessageIter *iter, void *user_data)
3008 {
3009         struct interface_connect_data *data = user_data;
3010         GSupplicantInterface *interface = data->interface;
3011         const char *path;
3012         int err;
3013
3014         if (error != NULL)
3015                 goto error;
3016
3017         dbus_message_iter_get_basic(iter, &path);
3018         if (path == NULL)
3019                 goto error;
3020
3021         SUPPLICANT_DBG("PATH: %s", path);
3022
3023         g_free(interface->network_path);
3024         interface->network_path = g_strdup(path);
3025
3026         supplicant_dbus_method_call(data->interface->path,
3027                         SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
3028                         interface_select_network_params,
3029                         interface_select_network_result, data);
3030
3031         return;
3032
3033 error:
3034         SUPPLICANT_DBG("AddNetwork error %s", error);
3035         err = parse_supplicant_error(iter);
3036         if (data->callback != NULL)
3037                 data->callback(err, data->interface, data->user_data);
3038
3039         g_free(interface->network_path);
3040         interface->network_path = NULL;
3041         g_free(data->ssid);
3042         g_free(data);
3043 }
3044
3045 static void add_network_security_wep(DBusMessageIter *dict,
3046                                         GSupplicantSSID *ssid)
3047 {
3048         const char *auth_alg = "OPEN SHARED";
3049         const char *key_index = "0";
3050
3051         supplicant_dbus_dict_append_basic(dict, "auth_alg",
3052                                         DBUS_TYPE_STRING, &auth_alg);
3053
3054         if (ssid->passphrase) {
3055                 int size = strlen(ssid->passphrase);
3056                 if (size == 10 || size == 26) {
3057                         unsigned char *key = g_try_malloc(13);
3058                         char tmp[3];
3059                         int i;
3060
3061                         memset(tmp, 0, sizeof(tmp));
3062                         if (key == NULL)
3063                                 size = 0;
3064
3065                         for (i = 0; i < size / 2; i++) {
3066                                 memcpy(tmp, ssid->passphrase + (i * 2), 2);
3067                                 key[i] = (unsigned char) strtol(tmp, NULL, 16);
3068                         }
3069
3070                         supplicant_dbus_dict_append_fixed_array(dict,
3071                                                         "wep_key0",
3072                                                         DBUS_TYPE_BYTE,
3073                                                         &key, size / 2);
3074                         g_free(key);
3075                 } else if (size == 5 || size == 13) {
3076                         unsigned char *key = g_try_malloc(13);
3077                         int i;
3078
3079                         if (key == NULL)
3080                                 size = 0;
3081
3082                         for (i = 0; i < size; i++)
3083                                 key[i] = (unsigned char) ssid->passphrase[i];
3084
3085                         supplicant_dbus_dict_append_fixed_array(dict,
3086                                                                 "wep_key0",
3087                                                                 DBUS_TYPE_BYTE,
3088                                                                 &key, size);
3089                         g_free(key);
3090                 } else
3091                         supplicant_dbus_dict_append_basic(dict,
3092                                                         "wep_key0",
3093                                                         DBUS_TYPE_STRING,
3094                                                         &ssid->passphrase);
3095
3096                 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
3097                                         DBUS_TYPE_STRING, &key_index);
3098         }
3099 }
3100
3101 static dbus_bool_t is_psk_raw_key(const char *psk)
3102 {
3103         int i;
3104
3105         /* A raw key is always 64 bytes length... */
3106         if (strlen(psk) != 64)
3107                 return FALSE;
3108
3109         /* ... and its content is in hex representation */
3110         for (i = 0; i < 64; i++)
3111                 if (!isxdigit((unsigned char) psk[i]))
3112                         return FALSE;
3113
3114         return TRUE;
3115 }
3116
3117 static unsigned char hexchar2bin(char c)
3118 {
3119         if ((c >= '0') && (c <= '9'))
3120                 return c - '0';
3121         else if ((c >= 'A') && (c <= 'F'))
3122                 return c - 'A' + 10;
3123         else if ((c >= 'a') && (c <= 'f'))
3124                 return c - 'a' + 10;
3125         else
3126                 return c;
3127 }
3128
3129 static void hexstring2bin(const char *string, unsigned char *data, size_t data_len)
3130 {
3131         size_t i;
3132
3133         for (i = 0; i < data_len; i++)
3134                 data[i] = (hexchar2bin(string[i * 2 + 0]) << 4 |
3135                            hexchar2bin(string[i * 2 + 1]) << 0);
3136 }
3137
3138 static void add_network_security_psk(DBusMessageIter *dict,
3139                                         GSupplicantSSID *ssid)
3140 {
3141         if (ssid->passphrase && strlen(ssid->passphrase) > 0) {
3142                 const char *key = "psk";
3143
3144                 if (is_psk_raw_key(ssid->passphrase) == TRUE) {
3145                         unsigned char data[32];
3146                         unsigned char *datap = data;
3147
3148                         /* The above pointer alias is required by D-Bus because
3149                          * with D-Bus and GCC, non-heap-allocated arrays cannot
3150                          * be passed directly by their base pointer. */
3151
3152                         hexstring2bin(ssid->passphrase, datap, sizeof(data));
3153
3154                         supplicant_dbus_dict_append_fixed_array(dict,
3155                                                         key, DBUS_TYPE_BYTE,
3156                                                         &datap, sizeof(data));
3157                 } else
3158                         supplicant_dbus_dict_append_basic(dict,
3159                                                         key, DBUS_TYPE_STRING,
3160                                                         &ssid->passphrase);
3161         }
3162 }
3163
3164 static void add_network_security_tls(DBusMessageIter *dict,
3165                                         GSupplicantSSID *ssid)
3166 {
3167         /*
3168          * For TLS, we at least need:
3169          *              The client certificate
3170          *              The client private key file
3171          *              The client private key file password
3172          *
3173          * The Authority certificate is optional.
3174          */
3175         if (ssid->client_cert_path == NULL)
3176                 return;
3177
3178         if (ssid->private_key_path == NULL)
3179                 return;
3180
3181         if (ssid->private_key_passphrase == NULL)
3182                 return;
3183
3184         if (ssid->ca_cert_path)
3185                 supplicant_dbus_dict_append_basic(dict, "ca_cert",
3186                                         DBUS_TYPE_STRING, &ssid->ca_cert_path);
3187
3188         supplicant_dbus_dict_append_basic(dict, "private_key",
3189                                                 DBUS_TYPE_STRING,
3190                                                 &ssid->private_key_path);
3191         supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
3192                                                 DBUS_TYPE_STRING,
3193                                                 &ssid->private_key_passphrase);
3194         supplicant_dbus_dict_append_basic(dict, "client_cert",
3195                                                 DBUS_TYPE_STRING,
3196                                                 &ssid->client_cert_path);
3197 }
3198
3199 static void add_network_security_peap(DBusMessageIter *dict,
3200                                         GSupplicantSSID *ssid)
3201 {
3202         char *phase2_auth;
3203
3204         /*
3205          * For PEAP/TTLS, we at least need
3206          *              The authority certificate
3207          *              The 2nd phase authentication method
3208          *              The 2nd phase passphrase
3209          *
3210          * The Client certificate is optional although strongly recommended
3211          * When setting it, we need in addition
3212          *              The Client private key file
3213          *              The Client private key file password
3214          */
3215         if (ssid->passphrase == NULL)
3216                 return;
3217
3218         if (ssid->phase2_auth == NULL)
3219                 return;
3220
3221         if (ssid->client_cert_path) {
3222                 if (ssid->private_key_path == NULL)
3223                         return;
3224
3225                 if (ssid->private_key_passphrase == NULL)
3226                         return;
3227
3228                 supplicant_dbus_dict_append_basic(dict, "client_cert",
3229                                                 DBUS_TYPE_STRING,
3230                                                 &ssid->client_cert_path);
3231
3232                 supplicant_dbus_dict_append_basic(dict, "private_key",
3233                                                 DBUS_TYPE_STRING,
3234                                                 &ssid->private_key_path);
3235
3236                 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
3237                                                 DBUS_TYPE_STRING,
3238                                                 &ssid->private_key_passphrase);
3239
3240         }
3241
3242         if (g_str_has_prefix(ssid->phase2_auth, "EAP-") == TRUE) {
3243                 phase2_auth = g_strdup_printf("autheap=%s",
3244                                         ssid->phase2_auth + strlen("EAP-"));
3245         } else
3246                 phase2_auth = g_strdup_printf("auth=%s", ssid->phase2_auth);
3247
3248         supplicant_dbus_dict_append_basic(dict, "password",
3249                                                 DBUS_TYPE_STRING,
3250                                                 &ssid->passphrase);
3251
3252         if (ssid->ca_cert_path)
3253                 supplicant_dbus_dict_append_basic(dict, "ca_cert",
3254                                                 DBUS_TYPE_STRING,
3255                                                 &ssid->ca_cert_path);
3256
3257         supplicant_dbus_dict_append_basic(dict, "phase2",
3258                                                 DBUS_TYPE_STRING,
3259                                                 &phase2_auth);
3260
3261         g_free(phase2_auth);
3262 }
3263
3264 static void add_network_security_eap(DBusMessageIter *dict,
3265                                         GSupplicantSSID *ssid)
3266 {
3267         char *eap_value;
3268
3269         if (ssid->eap == NULL || ssid->identity == NULL)
3270                 return;
3271
3272         if (g_strcmp0(ssid->eap, "tls") == 0) {
3273                 add_network_security_tls(dict, ssid);
3274         } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
3275                                 g_strcmp0(ssid->eap, "ttls") == 0) {
3276                 add_network_security_peap(dict, ssid);
3277         } else
3278                 return;
3279
3280         eap_value = g_ascii_strup(ssid->eap, -1);
3281
3282         supplicant_dbus_dict_append_basic(dict, "eap",
3283                                                 DBUS_TYPE_STRING,
3284                                                 &eap_value);
3285         supplicant_dbus_dict_append_basic(dict, "identity",
3286                                                 DBUS_TYPE_STRING,
3287                                                 &ssid->identity);
3288
3289         g_free(eap_value);
3290 }
3291
3292 static void add_network_security_ciphers(DBusMessageIter *dict,
3293                                                 GSupplicantSSID *ssid)
3294 {
3295         unsigned int p_cipher, g_cipher, i;
3296         char *pairwise, *group;
3297         char *pair_ciphers[4];
3298         char *group_ciphers[5];
3299
3300         p_cipher = ssid->pairwise_cipher;
3301         g_cipher = ssid->group_cipher;
3302
3303         if (p_cipher == 0 && g_cipher == 0)
3304                 return;
3305
3306         i = 0;
3307
3308         if (p_cipher & G_SUPPLICANT_PAIRWISE_CCMP)
3309                 pair_ciphers[i++] = "CCMP";
3310
3311         if (p_cipher & G_SUPPLICANT_PAIRWISE_TKIP)
3312                 pair_ciphers[i++] = "TKIP";
3313
3314         if (p_cipher & G_SUPPLICANT_PAIRWISE_NONE)
3315                 pair_ciphers[i++] = "NONE";
3316
3317         pair_ciphers[i] = NULL;
3318
3319         i = 0;
3320
3321         if (g_cipher & G_SUPPLICANT_GROUP_CCMP)
3322                 group_ciphers[i++] = "CCMP";
3323
3324         if (g_cipher & G_SUPPLICANT_GROUP_TKIP)
3325                 group_ciphers[i++] = "TKIP";
3326
3327         if (g_cipher & G_SUPPLICANT_GROUP_WEP104)
3328                 group_ciphers[i++] = "WEP104";
3329
3330         if (g_cipher & G_SUPPLICANT_GROUP_WEP40)
3331                 group_ciphers[i++] = "WEP40";
3332
3333         group_ciphers[i] = NULL;
3334
3335         pairwise = g_strjoinv(" ", pair_ciphers);
3336         group = g_strjoinv(" ", group_ciphers);
3337
3338         SUPPLICANT_DBG("cipher %s %s", pairwise, group);
3339
3340         supplicant_dbus_dict_append_basic(dict, "pairwise",
3341                                                 DBUS_TYPE_STRING,
3342                                                 &pairwise);
3343         supplicant_dbus_dict_append_basic(dict, "group",
3344                                                 DBUS_TYPE_STRING,
3345                                                 &group);
3346
3347         g_free(pairwise);
3348         g_free(group);
3349 }
3350
3351 static void add_network_security_proto(DBusMessageIter *dict,
3352                                                 GSupplicantSSID *ssid)
3353 {
3354         unsigned int protocol, i;
3355         char *proto;
3356         char *protos[3];
3357
3358         protocol = ssid->protocol;
3359
3360         if (protocol == 0)
3361                 return;
3362
3363         i = 0;
3364
3365         if (protocol & G_SUPPLICANT_PROTO_RSN)
3366                 protos[i++] = "RSN";
3367
3368         if (protocol & G_SUPPLICANT_PROTO_WPA)
3369                 protos[i++] = "WPA";
3370
3371         protos[i] = NULL;
3372
3373         proto = g_strjoinv(" ", protos);
3374
3375         SUPPLICANT_DBG("proto %s", proto);
3376
3377         supplicant_dbus_dict_append_basic(dict, "proto",
3378                                                 DBUS_TYPE_STRING,
3379                                                 &proto);
3380
3381         g_free(proto);
3382 }
3383
3384 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
3385 {
3386         char *key_mgmt;
3387
3388         switch (ssid->security) {
3389         case G_SUPPLICANT_SECURITY_UNKNOWN:
3390         case G_SUPPLICANT_SECURITY_NONE:
3391         case G_SUPPLICANT_SECURITY_WEP:
3392                 key_mgmt = "NONE";
3393                 add_network_security_wep(dict, ssid);
3394                 add_network_security_ciphers(dict, ssid);
3395                 break;
3396         case G_SUPPLICANT_SECURITY_PSK:
3397                 key_mgmt = "WPA-PSK";
3398                 add_network_security_psk(dict, ssid);
3399                 add_network_security_ciphers(dict, ssid);
3400                 add_network_security_proto(dict, ssid);
3401                 break;
3402         case G_SUPPLICANT_SECURITY_IEEE8021X:
3403                 key_mgmt = "WPA-EAP";
3404                 add_network_security_eap(dict, ssid);
3405                 add_network_security_ciphers(dict, ssid);
3406                 add_network_security_proto(dict, ssid);
3407                 break;
3408         }
3409
3410         supplicant_dbus_dict_append_basic(dict, "key_mgmt",
3411                                 DBUS_TYPE_STRING, &key_mgmt);
3412 }
3413
3414 static void add_network_mode(DBusMessageIter *dict, GSupplicantSSID *ssid)
3415 {
3416         dbus_uint32_t mode;
3417
3418         switch (ssid->mode) {
3419         case G_SUPPLICANT_MODE_UNKNOWN:
3420         case G_SUPPLICANT_MODE_INFRA:
3421                 mode = 0;
3422                 break;
3423         case G_SUPPLICANT_MODE_IBSS:
3424                 mode = 1;
3425                 break;
3426         case G_SUPPLICANT_MODE_MASTER:
3427                 mode = 2;
3428                 break;
3429         }
3430
3431         supplicant_dbus_dict_append_basic(dict, "mode",
3432                                 DBUS_TYPE_UINT32, &mode);
3433 }
3434
3435 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
3436 {
3437         DBusMessageIter dict;
3438         struct interface_connect_data *data = user_data;
3439         GSupplicantSSID *ssid = data->ssid;
3440
3441         supplicant_dbus_dict_open(iter, &dict);
3442
3443         if (ssid->scan_ssid)
3444                 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
3445                                          DBUS_TYPE_UINT32, &ssid->scan_ssid);
3446
3447         if (ssid->freq)
3448                 supplicant_dbus_dict_append_basic(&dict, "frequency",
3449                                          DBUS_TYPE_UINT32, &ssid->freq);
3450
3451         if (ssid->bgscan != NULL)
3452                 supplicant_dbus_dict_append_basic(&dict, "bgscan",
3453                                         DBUS_TYPE_STRING, &ssid->bgscan);
3454
3455         add_network_mode(&dict, ssid);
3456
3457         add_network_security(&dict, ssid);
3458
3459         supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
3460                                         DBUS_TYPE_BYTE, &ssid->ssid,
3461                                                 ssid->ssid_len);
3462
3463         supplicant_dbus_dict_close(iter, &dict);
3464 }
3465
3466 static void interface_wps_start_result(const char *error,
3467                                 DBusMessageIter *iter, void *user_data)
3468 {
3469         struct interface_connect_data *data = user_data;
3470
3471         SUPPLICANT_DBG("");
3472         if (error != NULL)
3473                 SUPPLICANT_DBG("error: %s", error);
3474
3475         g_free(data->ssid);
3476         dbus_free(data);
3477 }
3478
3479 static void interface_add_wps_params(DBusMessageIter *iter, void *user_data)
3480 {
3481         struct interface_connect_data *data = user_data;
3482         GSupplicantSSID *ssid = data->ssid;
3483         const char *role = "enrollee", *type;
3484         DBusMessageIter dict;
3485
3486         SUPPLICANT_DBG("");
3487
3488         supplicant_dbus_dict_open(iter, &dict);
3489
3490         supplicant_dbus_dict_append_basic(&dict, "Role",
3491                                                 DBUS_TYPE_STRING, &role);
3492
3493         type = "pbc";
3494         if (ssid->pin_wps != NULL) {
3495                 type = "pin";
3496                 supplicant_dbus_dict_append_basic(&dict, "Pin",
3497                                         DBUS_TYPE_STRING, &ssid->pin_wps);
3498         }
3499
3500         supplicant_dbus_dict_append_basic(&dict, "Type",
3501                                         DBUS_TYPE_STRING, &type);
3502
3503         supplicant_dbus_dict_close(iter, &dict);
3504 }
3505
3506 static void wps_start(const char *error, DBusMessageIter *iter, void *user_data)
3507 {
3508         struct interface_connect_data *data = user_data;
3509
3510         SUPPLICANT_DBG("");
3511
3512         if (error != NULL) {
3513                 SUPPLICANT_DBG("error: %s", error);
3514                 g_free(data->ssid);
3515                 dbus_free(data);
3516                 return;
3517         }
3518
3519         supplicant_dbus_method_call(data->interface->path,
3520                         SUPPLICANT_INTERFACE ".Interface.WPS", "Start",
3521                         interface_add_wps_params,
3522                         interface_wps_start_result, data);
3523 }
3524
3525 static void wps_process_credentials(DBusMessageIter *iter, void *user_data)
3526 {
3527         dbus_bool_t credentials = TRUE;
3528
3529         SUPPLICANT_DBG("");
3530
3531         dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &credentials);
3532 }
3533
3534
3535 int g_supplicant_interface_connect(GSupplicantInterface *interface,
3536                                 GSupplicantSSID *ssid,
3537                                 GSupplicantInterfaceCallback callback,
3538                                                         void *user_data)
3539 {
3540         struct interface_connect_data *data;
3541         int ret;
3542
3543         if (interface == NULL)
3544                 return -EINVAL;
3545
3546         if (system_available == FALSE)
3547                 return -EFAULT;
3548
3549         /* TODO: Check if we're already connected and switch */
3550
3551         data = dbus_malloc0(sizeof(*data));
3552         if (data == NULL)
3553                 return -ENOMEM;
3554
3555         data->interface = interface;
3556         data->callback = callback;
3557         data->ssid = ssid;
3558         data->user_data = user_data;
3559
3560         if (ssid->use_wps == TRUE) {
3561                 g_free(interface->wps_cred.key);
3562                 memset(&interface->wps_cred, 0,
3563                                 sizeof(struct _GSupplicantWpsCredentials));
3564
3565                 ret = supplicant_dbus_property_set(interface->path,
3566                         SUPPLICANT_INTERFACE ".Interface.WPS",
3567                         "ProcessCredentials", DBUS_TYPE_BOOLEAN_AS_STRING,
3568                         wps_process_credentials, wps_start, data);
3569         } else
3570                 ret = supplicant_dbus_method_call(interface->path,
3571                         SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
3572                         interface_add_network_params,
3573                         interface_add_network_result, data);
3574
3575         if (ret < 0)
3576                 return ret;
3577
3578         return -EINPROGRESS;
3579 }
3580
3581 static void network_remove_result(const char *error,
3582                                 DBusMessageIter *iter, void *user_data)
3583 {
3584         struct interface_data *data = user_data;
3585         int result = 0;
3586
3587         SUPPLICANT_DBG("");
3588
3589         if (error != NULL) {
3590                 result = -EIO;
3591                 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
3592                                                 error) == 0)
3593                         result = -ECONNABORTED;
3594         }
3595
3596         if (data->callback != NULL)
3597                 data->callback(result, data->interface, data->user_data);
3598
3599         dbus_free(data);
3600 }
3601
3602 static void network_remove_params(DBusMessageIter *iter, void *user_data)
3603 {
3604         struct interface_data *data = user_data;
3605         const char *path = data->interface->network_path;
3606
3607         SUPPLICANT_DBG("path %s", path);
3608
3609         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
3610 }
3611
3612 static int network_remove(struct interface_data *data)
3613 {
3614         GSupplicantInterface *interface = data->interface;
3615
3616         SUPPLICANT_DBG("");
3617
3618         return supplicant_dbus_method_call(interface->path,
3619                         SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
3620                         network_remove_params, network_remove_result, data);
3621 }
3622
3623 static void interface_disconnect_result(const char *error,
3624                                 DBusMessageIter *iter, void *user_data)
3625 {
3626         struct interface_data *data = user_data;
3627         int result = 0;
3628
3629         SUPPLICANT_DBG("");
3630
3631         if (error != NULL) {
3632                 result = -EIO;
3633                 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
3634                                                 error) == 0)
3635                         result = -ECONNABORTED;
3636         }
3637
3638         if (result < 0 && data->callback != NULL) {
3639                 data->callback(result, data->interface, data->user_data);
3640                 data->callback = NULL;
3641         }
3642
3643         /* If we are disconnecting from previous WPS successful
3644          * association. i.e.: it did not went through AddNetwork,
3645          * and interface->network_path was never set. */
3646         if (data->interface->network_path == NULL) {
3647                 dbus_free(data);
3648                 return;
3649         }
3650
3651         if (result != -ECONNABORTED)
3652                 network_remove(data);
3653         else
3654                 dbus_free(data);
3655 }
3656
3657 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
3658                                         GSupplicantInterfaceCallback callback,
3659                                                         void *user_data)
3660 {
3661         struct interface_data *data;
3662
3663         SUPPLICANT_DBG("");
3664
3665         if (interface == NULL)
3666                 return -EINVAL;
3667
3668         if (system_available == FALSE)
3669                 return -EFAULT;
3670
3671         data = dbus_malloc0(sizeof(*data));
3672         if (data == NULL)
3673                 return -ENOMEM;
3674
3675         data->interface = interface;
3676         data->callback = callback;
3677         data->user_data = user_data;
3678
3679         return supplicant_dbus_method_call(interface->path,
3680                         SUPPLICANT_INTERFACE ".Interface", "Disconnect",
3681                                 NULL, interface_disconnect_result, data);
3682 }
3683
3684
3685 static const char *g_supplicant_rule0 = "type=signal,"
3686                                         "path=" DBUS_PATH_DBUS ","
3687                                         "sender=" DBUS_SERVICE_DBUS ","
3688                                         "interface=" DBUS_INTERFACE_DBUS ","
3689                                         "member=NameOwnerChanged,"
3690                                         "arg0=" SUPPLICANT_SERVICE;
3691 static const char *g_supplicant_rule1 = "type=signal,"
3692                         "interface=" SUPPLICANT_INTERFACE;
3693 static const char *g_supplicant_rule2 = "type=signal,"
3694                         "interface=" SUPPLICANT_INTERFACE ".Interface";
3695 static const char *g_supplicant_rule3 = "type=signal,"
3696                         "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
3697 static const char *g_supplicant_rule4 = "type=signal,"
3698                         "interface=" SUPPLICANT_INTERFACE ".BSS";
3699 static const char *g_supplicant_rule5 = "type=signal,"
3700                         "interface=" SUPPLICANT_INTERFACE ".Network";
3701
3702 static void invoke_introspect_method(void)
3703 {
3704         DBusMessage *message;
3705
3706         message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
3707                                         SUPPLICANT_PATH,
3708                                         DBUS_INTERFACE_INTROSPECTABLE,
3709                                         "Introspect");
3710
3711         if (message == NULL)
3712                 return;
3713
3714         dbus_message_set_no_reply(message, TRUE);
3715         dbus_connection_send(connection, message, NULL);
3716         dbus_message_unref(message);
3717 }
3718
3719 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
3720 {
3721         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
3722         if (connection == NULL)
3723                 return -EIO;
3724
3725         if (dbus_connection_add_filter(connection,
3726                                 g_supplicant_filter, NULL, NULL) == FALSE) {
3727                 dbus_connection_unref(connection);
3728                 connection = NULL;
3729                 return -EIO;
3730         }
3731
3732         callbacks_pointer = callbacks;
3733         eap_methods = 0;
3734
3735         interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
3736                                                 NULL, remove_interface);
3737
3738         bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
3739                                                                 NULL, NULL);
3740
3741         supplicant_dbus_setup(connection);
3742
3743         dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
3744         dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
3745         dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
3746         dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
3747         dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
3748         dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
3749         dbus_connection_flush(connection);
3750
3751         if (dbus_bus_name_has_owner(connection,
3752                                         SUPPLICANT_SERVICE, NULL) == TRUE) {
3753                 system_available = TRUE;
3754                 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
3755                                                 SUPPLICANT_INTERFACE,
3756                                                 service_property, NULL);
3757         } else
3758                 invoke_introspect_method();
3759
3760         return 0;
3761 }
3762
3763 static void unregister_interface_remove_params(DBusMessageIter *iter,
3764                                                 void *user_data)
3765 {
3766         const char *path = user_data;
3767
3768         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
3769                                                         &path);
3770 }
3771
3772
3773 static void unregister_remove_interface(gpointer key, gpointer value,
3774                                                 gpointer user_data)
3775 {
3776         GSupplicantInterface *interface = value;
3777
3778         supplicant_dbus_method_call(SUPPLICANT_PATH,
3779                                         SUPPLICANT_INTERFACE,
3780                                         "RemoveInterface",
3781                                         unregister_interface_remove_params,
3782                                                 NULL, interface->path);
3783 }
3784
3785 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
3786 {
3787         SUPPLICANT_DBG("");
3788
3789         if (connection != NULL) {
3790                 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
3791                 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
3792                 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
3793                 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
3794                 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
3795                 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
3796                 dbus_connection_flush(connection);
3797
3798                 dbus_connection_remove_filter(connection,
3799                                                 g_supplicant_filter, NULL);
3800         }
3801
3802         if (bss_mapping != NULL) {
3803                 g_hash_table_destroy(bss_mapping);
3804                 bss_mapping = NULL;
3805         }
3806
3807         if (system_available == TRUE)
3808                 callback_system_killed();
3809
3810         if (interface_table != NULL) {
3811                 g_hash_table_foreach(interface_table,
3812                                         unregister_remove_interface, NULL);
3813                 g_hash_table_destroy(interface_table);
3814                 interface_table = NULL;
3815         }
3816
3817         if (connection != NULL) {
3818                 dbus_connection_unref(connection);
3819                 connection = NULL;
3820         }
3821
3822         callbacks_pointer = NULL;
3823         eap_methods = 0;
3824 }