Fix PASSED_TO_PROC_AFTER_FREE.EX
[platform/upstream/connman.git] / tools / supplicant.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-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 <string.h>
28 #include <stdint.h>
29 #include <syslog.h>
30
31 #include <glib.h>
32 #include <gdbus.h>
33
34 #include "supplicant-dbus.h"
35 #include "supplicant.h"
36
37 #define DBG(fmt, arg...) do { \
38         syslog(LOG_DEBUG, "%s() " fmt, __FUNCTION__ , ## arg); \
39 } while (0)
40
41 #define TIMEOUT 5000
42
43 #define IEEE80211_CAP_ESS       0x0001
44 #define IEEE80211_CAP_IBSS      0x0002
45 #define IEEE80211_CAP_PRIVACY   0x0010
46
47 static DBusConnection *connection;
48
49 static const struct supplicant_callbacks *callbacks_pointer;
50
51 static dbus_bool_t system_available = FALSE;
52 static dbus_bool_t system_ready = FALSE;
53
54 static dbus_int32_t debug_level = 0;
55 static dbus_bool_t debug_timestamp = FALSE;
56 static dbus_bool_t debug_showkeys = FALSE;
57
58 static const char *debug_strings[] = {
59         "msgdump", "debug", "info", "warning", "error", NULL
60 };
61
62 static unsigned int eap_methods;
63
64 struct strvalmap {
65         const char *str;
66         unsigned int val;
67 };
68
69 static struct strvalmap eap_method_map[] = {
70         { "MD5",        SUPPLICANT_EAP_METHOD_MD5       },
71         { "TLS",        SUPPLICANT_EAP_METHOD_TLS       },
72         { "MSCHAPV2",   SUPPLICANT_EAP_METHOD_MSCHAPV2  },
73         { "PEAP",       SUPPLICANT_EAP_METHOD_PEAP      },
74         { "TTLS",       SUPPLICANT_EAP_METHOD_TTLS      },
75         { "GTC",        SUPPLICANT_EAP_METHOD_GTC       },
76         { "OTP",        SUPPLICANT_EAP_METHOD_OTP       },
77         { "LEAP",       SUPPLICANT_EAP_METHOD_LEAP      },
78         { "WSC",        SUPPLICANT_EAP_METHOD_WSC       },
79         { }
80 };
81
82 static struct strvalmap keymgmt_capa_map[] = {
83         { "none",       SUPPLICANT_CAPABILITY_KEYMGMT_NONE      },
84         { "ieee8021x",  SUPPLICANT_CAPABILITY_KEYMGMT_IEEE8021X },
85         { "wpa-none",   SUPPLICANT_CAPABILITY_KEYMGMT_WPA_NONE  },
86         { "wpa-psk",    SUPPLICANT_CAPABILITY_KEYMGMT_WPA_PSK   },
87         { "wpa-eap",    SUPPLICANT_CAPABILITY_KEYMGMT_WPA_EAP   },
88         { "wps",        SUPPLICANT_CAPABILITY_KEYMGMT_WPS       },
89         { }
90 };
91
92 static struct strvalmap authalg_capa_map[] = {
93         { "open",       SUPPLICANT_CAPABILITY_AUTHALG_OPEN      },
94         { "shared",     SUPPLICANT_CAPABILITY_AUTHALG_SHARED    },
95         { "leap",       SUPPLICANT_CAPABILITY_AUTHALG_LEAP      },
96         { }
97 };
98
99 static struct strvalmap proto_capa_map[] = {
100         { "wpa",        SUPPLICANT_CAPABILITY_PROTO_WPA         },
101         { "rsn",        SUPPLICANT_CAPABILITY_PROTO_RSN         },
102         { }
103 };
104
105 static struct strvalmap group_capa_map[] = {
106         { "wep40",      SUPPLICANT_CAPABILITY_GROUP_WEP40       },
107         { "wep104",     SUPPLICANT_CAPABILITY_GROUP_WEP104      },
108         { "tkip",       SUPPLICANT_CAPABILITY_GROUP_TKIP        },
109         { "ccmp",       SUPPLICANT_CAPABILITY_GROUP_CCMP        },
110         { }
111 };
112
113 static struct strvalmap pairwise_capa_map[] = {
114         { "none",       SUPPLICANT_CAPABILITY_PAIRWISE_NONE     },
115         { "tkip",       SUPPLICANT_CAPABILITY_PAIRWISE_TKIP     },
116         { "ccmp",       SUPPLICANT_CAPABILITY_PAIRWISE_CCMP     },
117         { }
118 };
119
120 static struct strvalmap scan_capa_map[] = {
121         { "active",     SUPPLICANT_CAPABILITY_SCAN_ACTIVE       },
122         { "passive",    SUPPLICANT_CAPABILITY_SCAN_PASSIVE      },
123         { "ssid",       SUPPLICANT_CAPABILITY_SCAN_SSID         },
124         { }
125 };
126
127 static struct strvalmap mode_capa_map[] = {
128         { "infrastructure",     SUPPLICANT_CAPABILITY_MODE_INFRA        },
129         { "ad-hoc",             SUPPLICANT_CAPABILITY_MODE_IBSS         },
130         { "ap",                 SUPPLICANT_CAPABILITY_MODE_AP           },
131         { }
132 };
133
134 static GHashTable *interface_table;
135 static GHashTable *bss_mapping;
136
137 struct supplicant_interface {
138         char *path;
139         unsigned int keymgmt_capa;
140         unsigned int authalg_capa;
141         unsigned int proto_capa;
142         unsigned int group_capa;
143         unsigned int pairwise_capa;
144         unsigned int scan_capa;
145         unsigned int mode_capa;
146         dbus_bool_t ready;
147         enum supplicant_state state;
148         dbus_bool_t scanning;
149         supplicant_interface_scan_callback scan_callback;
150         void *scan_data;
151         int apscan;
152         char *ifname;
153         char *driver;
154         char *bridge;
155         GHashTable *network_table;
156         GHashTable *net_mapping;
157         GHashTable *bss_mapping;
158 };
159
160 struct supplicant_network {
161         struct supplicant_interface *interface;
162         char *path;
163         char *group;
164         char *name;
165         enum supplicant_mode mode;
166         GHashTable *bss_table;
167         GHashTable *config_table;
168 };
169
170 struct supplicant_bss {
171         struct supplicant_interface *interface;
172         char *path;
173         unsigned char bssid[6];
174         unsigned char ssid[32];
175         unsigned int ssid_len;
176         dbus_uint16_t frequency;
177         dbus_uint32_t maxrate;
178         enum supplicant_mode mode;
179         enum supplicant_security security;
180         dbus_bool_t privacy;
181         dbus_bool_t psk;
182         dbus_bool_t ieee8021x;
183 };
184
185 static enum supplicant_mode string2mode(const char *mode)
186 {
187         if (!mode)
188                 return SUPPLICANT_MODE_UNKNOWN;
189
190         if (g_str_equal(mode, "infrastructure"))
191                 return SUPPLICANT_MODE_INFRA;
192         else if (g_str_equal(mode, "ad-hoc"))
193                 return SUPPLICANT_MODE_IBSS;
194
195         return SUPPLICANT_MODE_UNKNOWN;
196 }
197
198 static const char *mode2string(enum supplicant_mode mode)
199 {
200         switch (mode) {
201         case SUPPLICANT_MODE_UNKNOWN:
202                 break;
203         case SUPPLICANT_MODE_INFRA:
204                 return "infra";
205         case SUPPLICANT_MODE_IBSS:
206                 return "adhoc";
207         }
208
209         return NULL;
210 }
211
212 static const char *security2string(enum supplicant_security security)
213 {
214         switch (security) {
215         case SUPPLICANT_SECURITY_UNKNOWN:
216                 break;
217         case SUPPLICANT_SECURITY_NONE:
218                 return "none";
219         case SUPPLICANT_SECURITY_WEP:
220                 return "wep";
221         case SUPPLICANT_SECURITY_PSK:
222                 return "psk";
223         case SUPPLICANT_SECURITY_IEEE8021X:
224                 return "ieee8021x";
225         }
226
227         return NULL;
228 }
229
230 static enum supplicant_state string2state(const char *state)
231 {
232         if (!state)
233                 return SUPPLICANT_STATE_UNKNOWN;
234
235         if (g_str_equal(state, "unknown"))
236                 return SUPPLICANT_STATE_UNKNOWN;
237         else if (g_str_equal(state, "disconnected"))
238                 return SUPPLICANT_STATE_DISCONNECTED;
239         else if (g_str_equal(state, "inactive"))
240                 return SUPPLICANT_STATE_INACTIVE;
241         else if (g_str_equal(state, "scanning"))
242                 return SUPPLICANT_STATE_SCANNING;
243         else if (g_str_equal(state, "authenticating"))
244                 return SUPPLICANT_STATE_AUTHENTICATING;
245         else if (g_str_equal(state, "associating"))
246                 return SUPPLICANT_STATE_ASSOCIATING;
247         else if (g_str_equal(state, "associated"))
248                 return SUPPLICANT_STATE_ASSOCIATED;
249         else if (g_str_equal(state, "group_handshake"))
250                 return SUPPLICANT_STATE_GROUP_HANDSHAKE;
251         else if (g_str_equal(state, "4way_handshake"))
252                 return SUPPLICANT_STATE_4WAY_HANDSHAKE;
253         else if (g_str_equal(state, "completed"))
254                 return SUPPLICANT_STATE_COMPLETED;
255
256         return SUPPLICANT_STATE_UNKNOWN;
257 }
258
259 static void callback_system_ready(void)
260 {
261         if (system_ready)
262                 return;
263
264         system_ready = TRUE;
265
266         if (!callbacks_pointer)
267                 return;
268
269         if (!callbacks_pointer->system_ready)
270                 return;
271
272         callbacks_pointer->system_ready();
273 }
274
275 static void callback_system_killed(void)
276 {
277         system_ready = FALSE;
278
279         if (!callbacks_pointer)
280                 return;
281
282         if (!callbacks_pointer->system_killed)
283                 return;
284
285         callbacks_pointer->system_killed();
286 }
287
288 static void callback_interface_added(struct supplicant_interface *interface)
289 {
290         if (!callbacks_pointer)
291                 return;
292
293         if (!callbacks_pointer->interface_added)
294                 return;
295
296         callbacks_pointer->interface_added(interface);
297 }
298
299 static void callback_interface_removed(struct supplicant_interface *interface)
300 {
301         if (!callbacks_pointer)
302                 return;
303
304         if (!callbacks_pointer->interface_removed)
305                 return;
306
307         callbacks_pointer->interface_removed(interface);
308 }
309
310 static void callback_scan_started(struct supplicant_interface *interface)
311 {
312         if (!callbacks_pointer)
313                 return;
314
315         if (!callbacks_pointer->scan_started)
316                 return;
317
318         callbacks_pointer->scan_started(interface);
319 }
320
321 static void callback_scan_finished(struct supplicant_interface *interface)
322 {
323         if (!callbacks_pointer)
324                 return;
325
326         if (!callbacks_pointer->scan_finished)
327                 return;
328
329         callbacks_pointer->scan_finished(interface);
330 }
331
332 static void callback_network_added(struct supplicant_network *network)
333 {
334         if (!callbacks_pointer)
335                 return;
336
337         if (!callbacks_pointer->network_added)
338                 return;
339
340         callbacks_pointer->network_added(network);
341 }
342
343 static void callback_network_removed(struct supplicant_network *network)
344 {
345         if (!callbacks_pointer)
346                 return;
347
348         if (!callbacks_pointer->network_removed)
349                 return;
350
351         callbacks_pointer->network_removed(network);
352 }
353
354 static void remove_interface(gpointer data)
355 {
356         struct supplicant_interface *interface = data;
357
358         g_hash_table_destroy(interface->bss_mapping);
359         g_hash_table_destroy(interface->net_mapping);
360         g_hash_table_destroy(interface->network_table);
361
362         callback_interface_removed(interface);
363
364         g_free(interface->path);
365         g_free(interface->ifname);
366         g_free(interface->driver);
367         g_free(interface->bridge);
368         g_free(interface);
369 }
370
371 static void remove_network(gpointer data)
372 {
373         struct supplicant_network *network = data;
374
375         g_hash_table_destroy(network->bss_table);
376
377         callback_network_removed(network);
378
379         g_hash_table_destroy(network->config_table);
380
381         g_free(network->group);
382         g_free(network->name);
383         g_free(network);
384 }
385
386 static void remove_bss(gpointer data)
387 {
388         struct supplicant_bss *bss = data;
389
390         g_free(bss->path);
391         g_free(bss);
392 }
393
394 static void debug_strvalmap(const char *label, struct strvalmap *map,
395                                                         unsigned int val)
396 {
397         int i;
398
399         for (i = 0; map[i].str; i++) {
400                 if (val & map[i].val)
401                         DBG("%s: %s", label, map[i].str);
402         }
403 }
404
405 static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data)
406 {
407         struct supplicant_interface *interface = user_data;
408         const char *str = NULL;
409         int i;
410
411         dbus_message_iter_get_basic(iter, &str);
412         if (!str)
413                 return;
414
415         for (i = 0; keymgmt_capa_map[i].str; i++)
416                 if (strcmp(str, keymgmt_capa_map[i].str) == 0) {
417                         interface->keymgmt_capa |= keymgmt_capa_map[i].val;
418                         break;
419                 }
420 }
421
422 static void interface_capability_authalg(DBusMessageIter *iter, void *user_data)
423 {
424         struct supplicant_interface *interface = user_data;
425         const char *str = NULL;
426         int i;
427
428         dbus_message_iter_get_basic(iter, &str);
429         if (!str)
430                 return;
431
432         for (i = 0; authalg_capa_map[i].str; i++)
433                 if (strcmp(str, authalg_capa_map[i].str) == 0) {
434                         interface->authalg_capa |= authalg_capa_map[i].val;
435                         break;
436                 }
437 }
438
439 static void interface_capability_proto(DBusMessageIter *iter, void *user_data)
440 {
441         struct supplicant_interface *interface = user_data;
442         const char *str = NULL;
443         int i;
444
445         dbus_message_iter_get_basic(iter, &str);
446         if (!str)
447                 return;
448
449         for (i = 0; proto_capa_map[i].str; i++)
450                 if (strcmp(str, proto_capa_map[i].str) == 0) {
451                         interface->proto_capa |= proto_capa_map[i].val;
452                         break;
453                 }
454 }
455
456 static void interface_capability_pairwise(DBusMessageIter *iter, void *user_data)
457 {
458         struct supplicant_interface *interface = user_data;
459         const char *str = NULL;
460         int i;
461
462         dbus_message_iter_get_basic(iter, &str);
463         if (!str)
464                 return;
465
466         for (i = 0; pairwise_capa_map[i].str; i++)
467                 if (strcmp(str, pairwise_capa_map[i].str) == 0) {
468                         interface->pairwise_capa |= pairwise_capa_map[i].val;
469                         break;
470                 }
471 }
472
473 static void interface_capability_group(DBusMessageIter *iter, void *user_data)
474 {
475         struct supplicant_interface *interface = user_data;
476         const char *str = NULL;
477         int i;
478
479         dbus_message_iter_get_basic(iter, &str);
480         if (!str)
481                 return;
482
483         for (i = 0; group_capa_map[i].str; i++)
484                 if (strcmp(str, group_capa_map[i].str) == 0) {
485                         interface->group_capa |= group_capa_map[i].val;
486                         break;
487                 }
488 }
489
490 static void interface_capability_scan(DBusMessageIter *iter, void *user_data)
491 {
492         struct supplicant_interface *interface = user_data;
493         const char *str = NULL;
494         int i;
495
496         dbus_message_iter_get_basic(iter, &str);
497         if (!str)
498                 return;
499
500         for (i = 0; scan_capa_map[i].str; i++)
501                 if (strcmp(str, scan_capa_map[i].str) == 0) {
502                         interface->scan_capa |= scan_capa_map[i].val;
503                         break;
504                 }
505 }
506
507 static void interface_capability_mode(DBusMessageIter *iter, void *user_data)
508 {
509         struct supplicant_interface *interface = user_data;
510         const char *str = NULL;
511         int i;
512
513         dbus_message_iter_get_basic(iter, &str);
514         if (!str)
515                 return;
516
517         for (i = 0; mode_capa_map[i].str; i++)
518                 if (strcmp(str, mode_capa_map[i].str) == 0) {
519                         interface->mode_capa |= mode_capa_map[i].val;
520                         break;
521                 }
522 }
523
524 static void interface_capability(const char *key, DBusMessageIter *iter,
525                                                         void *user_data)
526 {
527         struct supplicant_interface *interface = user_data;
528
529         if (!key)
530                 return;
531
532         if (g_strcmp0(key, "KeyMgmt") == 0)
533                 supplicant_dbus_array_foreach(iter,
534                                 interface_capability_keymgmt, interface);
535         else if (g_strcmp0(key, "AuthAlg") == 0)
536                 supplicant_dbus_array_foreach(iter,
537                                 interface_capability_authalg, interface);
538         else if (g_strcmp0(key, "Protocol") == 0)
539                 supplicant_dbus_array_foreach(iter,
540                                 interface_capability_proto, interface);
541         else if (g_strcmp0(key, "Pairwise") == 0)
542                 supplicant_dbus_array_foreach(iter,
543                                 interface_capability_pairwise, interface);
544         else if (g_strcmp0(key, "Group") == 0)
545                 supplicant_dbus_array_foreach(iter,
546                                 interface_capability_group, interface);
547         else if (g_strcmp0(key, "Scan") == 0)
548                 supplicant_dbus_array_foreach(iter,
549                                 interface_capability_scan, interface);
550         else if (g_strcmp0(key, "Modes") == 0)
551                 supplicant_dbus_array_foreach(iter,
552                                 interface_capability_mode, interface);
553         else
554                 DBG("key %s type %c",
555                                 key, dbus_message_iter_get_arg_type(iter));
556 }
557
558 const char *supplicant_interface_get_ifname(struct supplicant_interface *interface)
559 {
560         if (!interface)
561                 return NULL;
562
563         return interface->ifname;
564 }
565
566 const char *supplicant_interface_get_driver(struct supplicant_interface *interface)
567 {
568         if (!interface)
569                 return NULL;
570
571         return interface->driver;
572 }
573
574 struct supplicant_interface *supplicant_network_get_interface(struct supplicant_network *network)
575 {
576         if (!network)
577                 return NULL;
578
579         return network->interface;
580 }
581
582 const char *supplicant_network_get_name(struct supplicant_network *network)
583 {
584         if (!network || !network->name)
585                 return "";
586
587         return network->name;
588 }
589
590 const char *supplicant_network_get_identifier(struct supplicant_network *network)
591 {
592         if (!network || !network->group)
593                 return "";
594
595         return network->group;
596 }
597
598 enum supplicant_mode supplicant_network_get_mode(struct supplicant_network *network)
599 {
600         if (!network)
601                 return SUPPLICANT_MODE_UNKNOWN;
602
603         return network->mode;
604 }
605
606 static void merge_network(struct supplicant_network *network)
607 {
608         GString *str;
609         const char *ssid, *mode, *key_mgmt;
610         unsigned int i, ssid_len;
611         char *group;
612
613         ssid = g_hash_table_lookup(network->config_table, "ssid");
614         mode = g_hash_table_lookup(network->config_table, "mode");
615         key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
616
617         DBG("ssid %s mode %s", ssid, mode);
618
619         if (ssid)
620                 ssid_len = strlen(ssid);
621         else
622                 ssid_len = 0;
623
624         str = g_string_sized_new((ssid_len * 2) + 24);
625         if (!str)
626                 return;
627
628         for (i = 0; i < ssid_len; i++)
629                 g_string_append_printf(str, "%02x", ssid[i]);
630
631         if (g_strcmp0(mode, "0") == 0)
632                 g_string_append_printf(str, "_infra");
633         else if (g_strcmp0(mode, "1") == 0)
634                 g_string_append_printf(str, "_adhoc");
635
636         if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
637                 g_string_append_printf(str, "_psk");
638
639         group = g_string_free(str, FALSE);
640
641         DBG("%s", group);
642
643         g_free(group);
644
645         g_hash_table_destroy(network->config_table);
646
647         g_free(network->path);
648         g_free(network);
649 }
650
651 static void network_property(const char *key, DBusMessageIter *iter,
652                                                         void *user_data)
653 {
654         struct supplicant_network *network = user_data;
655
656         if (!network->interface)
657                 return;
658
659         if (!key) {
660                 merge_network(network);
661                 return;
662         }
663
664         if (g_strcmp0(key, "Enabled") == 0) {
665                 dbus_bool_t enabled = FALSE;
666
667                 dbus_message_iter_get_basic(iter, &enabled);
668         } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
669                 const char *str = NULL;
670
671                 dbus_message_iter_get_basic(iter, &str);
672                 if (str)
673                         g_hash_table_replace(network->config_table,
674                                                 g_strdup(key), g_strdup(str));
675         } else
676                 DBG("key %s type %c",
677                                 key, dbus_message_iter_get_arg_type(iter));
678 }
679
680 static void interface_network_added(DBusMessageIter *iter, void *user_data)
681 {
682         struct supplicant_interface *interface = user_data;
683         struct supplicant_network *network;
684         const char *path = NULL;
685
686         dbus_message_iter_get_basic(iter, &path);
687         if (!path)
688                 return;
689
690         if (g_strcmp0(path, "/") == 0)
691                 return;
692
693         network = g_hash_table_lookup(interface->net_mapping, path);
694         if (network)
695                 return;
696
697         network = g_try_new0(struct supplicant_network, 1);
698         if (!network)
699                 return;
700
701         network->interface = interface;
702         network->path = g_strdup(path);
703
704         network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
705                                                         g_free, g_free);
706
707         dbus_message_iter_next(iter);
708         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
709                 supplicant_dbus_property_foreach(iter, network_property,
710                                                                 network);
711                 network_property(NULL, NULL, network);
712                 return;
713         }
714
715         supplicant_dbus_property_get_all(path,
716                                 SUPPLICANT_INTERFACE ".Interface.Network",
717                                                 network_property, network);
718 }
719
720 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
721 {
722         struct supplicant_interface *interface = user_data;
723         struct supplicant_network *network;
724         const char *path = NULL;
725
726         dbus_message_iter_get_basic(iter, &path);
727         if (!path)
728                 return;
729
730         network = g_hash_table_lookup(interface->net_mapping, path);
731         if (!network)
732                 return;
733
734         g_hash_table_remove(interface->net_mapping, path);
735 }
736
737 static char *create_name(unsigned char *ssid, int ssid_len)
738 {
739         char *name;
740         int i;
741
742         if (ssid_len < 1 || ssid[0] == '\0')
743                 name = NULL;
744         else
745                 name = g_try_malloc0(ssid_len + 1);
746
747         if (!name)
748                 return g_strdup("");
749
750         for (i = 0; i < ssid_len; i++) {
751                 if (g_ascii_isprint(ssid[i]))
752                         name[i] = ssid[i];
753                 else
754                         name[i] = ' ';
755         }
756
757         return name;
758 }
759
760 static char *create_group(struct supplicant_bss *bss)
761 {
762         GString *str;
763         unsigned int i;
764         const char *mode, *security;
765
766         str = g_string_sized_new((bss->ssid_len * 2) + 24);
767         if (!str)
768                 return NULL;
769
770         if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
771                 for (i = 0; i < bss->ssid_len; i++)
772                         g_string_append_printf(str, "%02x", bss->ssid[i]);
773         } else
774                 g_string_append_printf(str, "hidden");
775
776         mode = mode2string(bss->mode);
777         if (mode)
778                 g_string_append_printf(str, "_%s", mode);
779
780         security = security2string(bss->security);
781         if (security)
782                 g_string_append_printf(str, "_%s", security);
783
784         return g_string_free(str, FALSE);
785 }
786
787 static void add_bss_to_network(struct supplicant_bss *bss)
788 {
789         struct supplicant_interface *interface = bss->interface;
790         struct supplicant_network *network;
791         char *group;
792
793         group = create_group(bss);
794         if (!group)
795                 return;
796
797         network = g_hash_table_lookup(interface->network_table, group);
798         if (network) {
799                 g_free(group);
800                 goto done;
801         }
802
803         network = g_try_new0(struct supplicant_network, 1);
804         if (!network) {
805                 g_free(group);
806                 return;
807         }
808
809         network->group = group;
810         network->name = create_name(bss->ssid, bss->ssid_len);
811         network->mode = bss->mode;
812
813         network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
814                                                         NULL, remove_bss);
815
816         network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
817                                                         g_free, g_free);
818
819         g_hash_table_replace(interface->network_table,
820                                                 network->group, network);
821
822         callback_network_added(network);
823
824 done:
825         g_hash_table_replace(interface->bss_mapping, bss->path, network);
826         g_hash_table_replace(network->bss_table, bss->path, bss);
827
828         g_hash_table_replace(bss_mapping, bss->path, interface);
829 }
830
831 static unsigned char wifi_oui[3]      = { 0x00, 0x50, 0xf2 };
832 static unsigned char ieee80211_oui[3] = { 0x00, 0x0f, 0xac };
833
834 static void extract_rsn(struct supplicant_bss *bss,
835                                         const unsigned char *buf, int len)
836 {
837         uint16_t count;
838         int i;
839
840         /* Version */
841         if (len < 2)
842                 return;
843
844         buf += 2;
845         len -= 2;
846
847         /* Group cipher */
848         if (len < 4)
849                 return;
850
851         buf += 4;
852         len -= 4;
853
854         /* Pairwise cipher */
855         if (len < 2)
856                 return;
857
858         count = buf[0] | (buf[1] << 8);
859         if (2 + (count * 4) > len)
860                 return;
861
862         buf += 2 + (count * 4);
863         len -= 2 + (count * 4);
864
865         /* Authentication */
866         if (len < 2)
867                 return;
868
869         count = buf[0] | (buf[1] << 8);
870         if (2 + (count * 4) > len)
871                 return;
872
873         for (i = 0; i < count; i++) {
874                 const unsigned char *ptr = buf + 2 + (i * 4);
875
876                 if (memcmp(ptr, wifi_oui, 3) == 0) {
877                         switch (ptr[3]) {
878                         case 1:
879                                 bss->ieee8021x = TRUE;
880                                 break;
881                         case 2:
882                                 bss->psk = TRUE;
883                                 break;
884                         }
885                 } else if (memcmp(ptr, ieee80211_oui, 3) == 0) {
886                         switch (ptr[3]) {
887                         case 1:
888                                 bss->ieee8021x = TRUE;
889                                 break;
890                         case 2:
891                                 bss->psk = TRUE;
892                                 break;
893                         }
894                 }
895         }
896
897 }
898
899 static void bss_rates(DBusMessageIter *iter, void *user_data)
900 {
901         struct supplicant_bss *bss = user_data;
902         dbus_uint32_t rate = 0;
903
904         dbus_message_iter_get_basic(iter, &rate);
905         if (rate == 0)
906                 return;
907
908         if (rate > bss->maxrate)
909                 bss->maxrate = rate;
910 }
911
912 static void bss_property(const char *key, DBusMessageIter *iter,
913                                                         void *user_data)
914 {
915         struct supplicant_bss *bss = user_data;
916
917         if (!bss->interface)
918                 return;
919
920         if (!key) {
921                 if (bss->ieee8021x)
922                         bss->security = SUPPLICANT_SECURITY_IEEE8021X;
923                 else if (bss->psk)
924                         bss->security = SUPPLICANT_SECURITY_PSK;
925                 else if (bss->privacy)
926                         bss->security = SUPPLICANT_SECURITY_WEP;
927                 else
928                         bss->security = SUPPLICANT_SECURITY_NONE;
929
930                 add_bss_to_network(bss);
931                 return;
932         }
933
934         if (g_strcmp0(key, "BSSID") == 0) {
935                 DBusMessageIter array;
936                 unsigned char *addr;
937                 int addr_len;
938
939                 dbus_message_iter_recurse(iter, &array);
940                 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
941
942                 if (addr_len == 6)
943                         memcpy(bss->bssid, addr, addr_len);
944         } else if (g_strcmp0(key, "SSID") == 0) {
945                 DBusMessageIter array;
946                 unsigned char *ssid;
947                 int ssid_len;
948
949                 dbus_message_iter_recurse(iter, &array);
950                 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
951
952                 if (ssid_len > 0 && ssid_len < 33) {
953                         memcpy(bss->ssid, ssid, ssid_len);
954                         bss->ssid_len = ssid_len;
955                 } else {
956                         memset(bss->ssid, 0, sizeof(bss->ssid));
957                         bss->ssid_len = 0;
958                 }
959         } else if (g_strcmp0(key, "Capabilities") == 0) {
960                 dbus_uint16_t capabilities = 0x0000;
961
962                 dbus_message_iter_get_basic(iter, &capabilities);
963
964                 if (capabilities & IEEE80211_CAP_ESS)
965                         bss->mode = SUPPLICANT_MODE_INFRA;
966                 else if (capabilities & IEEE80211_CAP_IBSS)
967                         bss->mode = SUPPLICANT_MODE_IBSS;
968
969                 if (capabilities & IEEE80211_CAP_PRIVACY)
970                         bss->privacy = TRUE;
971         } else if (g_strcmp0(key, "Mode") == 0) {
972                 const char *mode = NULL;
973
974                 dbus_message_iter_get_basic(iter, &mode);
975                 bss->mode = string2mode(mode);
976         } else if (g_strcmp0(key, "Frequency") == 0) {
977                 dbus_uint16_t frequency = 0;
978
979                 dbus_message_iter_get_basic(iter, &frequency);
980                 bss->frequency = frequency;
981         } else if (g_strcmp0(key, "Signal") == 0) {
982                 dbus_int16_t signal = 0;
983
984                 dbus_message_iter_get_basic(iter, &signal);
985         } else if (g_strcmp0(key, "Level") == 0) {
986                 dbus_int32_t level = 0;
987
988                 dbus_message_iter_get_basic(iter, &level);
989         } else if (g_strcmp0(key, "Rates") == 0) {
990                 supplicant_dbus_array_foreach(iter, bss_rates, bss);
991         } else if (g_strcmp0(key, "MaxRate") == 0) {
992                 dbus_uint32_t maxrate = 0;
993
994                 dbus_message_iter_get_basic(iter, &maxrate);
995                 if (maxrate != 0)
996                         bss->maxrate =maxrate;
997         } else if (g_strcmp0(key, "Privacy") == 0) {
998                 dbus_bool_t privacy = FALSE;
999
1000                 dbus_message_iter_get_basic(iter, &privacy);
1001                 bss->privacy = privacy;
1002         } else if (g_strcmp0(key, "RSNIE") == 0) {
1003                 DBusMessageIter array;
1004                 unsigned char *ie;
1005                 int ie_len;
1006
1007                 dbus_message_iter_recurse(iter, &array);
1008                 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1009
1010                 if (ie_len > 2)
1011                         extract_rsn(bss, ie + 2, ie_len - 2);
1012         } else if (g_strcmp0(key, "WPAIE") == 0) {
1013                 DBusMessageIter array;
1014                 unsigned char *ie;
1015                 int ie_len;
1016
1017                 dbus_message_iter_recurse(iter, &array);
1018                 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1019
1020                 if (ie_len > 6)
1021                         extract_rsn(bss, ie + 6, ie_len - 6);
1022         } else if (g_strcmp0(key, "WPSIE") == 0) {
1023                 DBusMessageIter array;
1024                 unsigned char *ie;
1025                 int ie_len;
1026
1027                 dbus_message_iter_recurse(iter, &array);
1028                 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1029         } else
1030                 DBG("key %s type %c",
1031                                 key, dbus_message_iter_get_arg_type(iter));
1032 }
1033
1034 static void interface_bss_added(DBusMessageIter *iter, void *user_data)
1035 {
1036         struct supplicant_interface *interface = user_data;
1037         struct supplicant_network *network;
1038         struct supplicant_bss *bss;
1039         const char *path = NULL;
1040
1041         dbus_message_iter_get_basic(iter, &path);
1042         if (!path)
1043                 return;
1044
1045         if (g_strcmp0(path, "/") == 0)
1046                 return;
1047
1048         network = g_hash_table_lookup(interface->bss_mapping, path);
1049         if (network) {
1050                 bss = g_hash_table_lookup(network->bss_table, path);
1051                 if (bss)
1052                         return;
1053         }
1054
1055         bss = g_try_new0(struct supplicant_bss, 1);
1056         if (!bss)
1057                 return;
1058
1059         bss->interface = interface;
1060         bss->path = g_strdup(path);
1061
1062         dbus_message_iter_next(iter);
1063         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1064                 supplicant_dbus_property_foreach(iter, bss_property, bss);
1065                 bss_property(NULL, NULL, bss);
1066                 return;
1067         }
1068
1069         supplicant_dbus_property_get_all(path,
1070                                         SUPPLICANT_INTERFACE ".Interface.BSS",
1071                                                         bss_property, bss);
1072 }
1073
1074 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
1075 {
1076         struct supplicant_interface *interface = user_data;
1077         struct supplicant_network *network;
1078         const char *path = NULL;
1079
1080         dbus_message_iter_get_basic(iter, &path);
1081         if (!path)
1082                 return;
1083
1084         network = g_hash_table_lookup(interface->bss_mapping, path);
1085         if (!network)
1086                 return;
1087
1088         g_hash_table_remove(bss_mapping, path);
1089
1090         g_hash_table_remove(interface->bss_mapping, path);
1091         g_hash_table_remove(network->bss_table, path);
1092
1093         if (g_hash_table_size(network->bss_table) == 0)
1094                 g_hash_table_remove(interface->network_table, network->group);
1095 }
1096
1097 static void interface_property(const char *key, DBusMessageIter *iter,
1098                                                         void *user_data)
1099 {
1100         struct supplicant_interface *interface = user_data;
1101
1102         if (!interface)
1103                 return;
1104
1105         if (!key) {
1106                 debug_strvalmap("KeyMgmt capability", keymgmt_capa_map,
1107                                                 interface->keymgmt_capa);
1108                 debug_strvalmap("AuthAlg capability", authalg_capa_map,
1109                                                 interface->authalg_capa);
1110                 debug_strvalmap("Protocol capability", proto_capa_map,
1111                                                 interface->proto_capa);
1112                 debug_strvalmap("Pairwise capability", pairwise_capa_map,
1113                                                 interface->pairwise_capa);
1114                 debug_strvalmap("Group capability", group_capa_map,
1115                                                 interface->group_capa);
1116                 debug_strvalmap("Scan capability", scan_capa_map,
1117                                                 interface->scan_capa);
1118                 debug_strvalmap("Mode capability", mode_capa_map,
1119                                                 interface->mode_capa);
1120
1121                 interface->ready = TRUE;
1122                 callback_interface_added(interface);
1123                 return;
1124         }
1125
1126         if (g_strcmp0(key, "Capabilities") == 0) {
1127                 supplicant_dbus_property_foreach(iter, interface_capability,
1128                                                                 interface);
1129         } else if (g_strcmp0(key, "State") == 0) {
1130                 const char *str = NULL;
1131
1132                 dbus_message_iter_get_basic(iter, &str);
1133                 if (str)
1134                         interface->state = string2state(str);
1135
1136                 DBG("state %s (%d)", str, interface->state);
1137         } else if (g_strcmp0(key, "Scanning") == 0) {
1138                 dbus_bool_t scanning = FALSE;
1139
1140                 dbus_message_iter_get_basic(iter, &scanning);
1141                 interface->scanning = scanning;
1142
1143                 if (interface->ready) {
1144                         if (interface->scanning)
1145                                 callback_scan_started(interface);
1146                         else
1147                                 callback_scan_finished(interface);
1148                 }
1149         } else if (g_strcmp0(key, "ApScan") == 0) {
1150                 int apscan = 1;
1151
1152                 dbus_message_iter_get_basic(iter, &apscan);
1153                 interface->apscan = apscan;
1154         } else if (g_strcmp0(key, "Ifname") == 0) {
1155                 const char *str = NULL;
1156
1157                 dbus_message_iter_get_basic(iter, &str);
1158                 if (str)
1159                         interface->ifname = g_strdup(str);
1160         } else if (g_strcmp0(key, "Driver") == 0) {
1161                 const char *str = NULL;
1162
1163                 dbus_message_iter_get_basic(iter, &str);
1164                 if (str)
1165                         interface->driver = g_strdup(str);
1166         } else if (g_strcmp0(key, "BridgeIfname") == 0) {
1167                 const char *str = NULL;
1168
1169                 dbus_message_iter_get_basic(iter, &str);
1170                 if (str)
1171                         interface->bridge = g_strdup(str);
1172         } else if (g_strcmp0(key, "CurrentBSS") == 0) {
1173                 interface_bss_added(iter, interface);
1174         } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
1175                 interface_network_added(iter, interface);
1176         } else if (g_strcmp0(key, "BSSs") == 0) {
1177                 supplicant_dbus_array_foreach(iter, interface_bss_added,
1178                                                                 interface);
1179         } else if (g_strcmp0(key, "Blobs") == 0) {
1180         } else if (g_strcmp0(key, "Networks") == 0) {
1181                 supplicant_dbus_array_foreach(iter, interface_network_added,
1182                                                                 interface);
1183         } else
1184                 DBG("key %s type %c",
1185                                 key, dbus_message_iter_get_arg_type(iter));
1186 }
1187
1188 static struct supplicant_interface *interface_alloc(const char *path)
1189 {
1190         struct supplicant_interface *interface;
1191
1192         interface = g_try_new0(struct supplicant_interface, 1);
1193         if (!interface)
1194                 return NULL;
1195
1196         interface->path = g_strdup(path);
1197
1198         interface->network_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1199                                                         NULL, remove_network);
1200
1201         interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1202                                                                 NULL, NULL);
1203         interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1204                                                                 NULL, NULL);
1205
1206         g_hash_table_replace(interface_table, interface->path, interface);
1207
1208         return interface;
1209 }
1210
1211 static void interface_added(DBusMessageIter *iter, void *user_data)
1212 {
1213         struct supplicant_interface *interface;
1214         const char *path = NULL;
1215
1216         dbus_message_iter_get_basic(iter, &path);
1217         if (!path)
1218                 return;
1219
1220         if (g_strcmp0(path, "/") == 0)
1221                 return;
1222
1223         interface = g_hash_table_lookup(interface_table, path);
1224         if (interface)
1225                 return;
1226
1227         interface = interface_alloc(path);
1228         if (!interface)
1229                 return;
1230
1231         dbus_message_iter_next(iter);
1232         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1233                 supplicant_dbus_property_foreach(iter, interface_property,
1234                                                                 interface);
1235                 interface_property(NULL, NULL, interface);
1236                 return;
1237         }
1238
1239         supplicant_dbus_property_get_all(path,
1240                                         SUPPLICANT_INTERFACE ".Interface",
1241                                                 interface_property, interface);
1242 }
1243
1244 static void interface_removed(DBusMessageIter *iter, void *user_data)
1245 {
1246         const char *path = NULL;
1247
1248         dbus_message_iter_get_basic(iter, &path);
1249         if (!path)
1250                 return;
1251
1252         g_hash_table_remove(interface_table, path);
1253 }
1254
1255 static void eap_method(DBusMessageIter *iter, void *user_data)
1256 {
1257         const char *str = NULL;
1258         int i;
1259
1260         dbus_message_iter_get_basic(iter, &str);
1261         if (!str)
1262                 return;
1263
1264         for (i = 0; eap_method_map[i].str; i++)
1265                 if (strcmp(str, eap_method_map[i].str) == 0) {
1266                         eap_methods |= eap_method_map[i].val;
1267                         break;
1268                 }
1269 }
1270
1271 static void service_property(const char *key, DBusMessageIter *iter,
1272                                                         void *user_data)
1273 {
1274         if (!key) {
1275                 callback_system_ready();
1276                 return;
1277         }
1278
1279         if (g_strcmp0(key, "DebugLevel") == 0) {
1280                 const char *str = NULL;
1281                 int i;
1282
1283                 dbus_message_iter_get_basic(iter, &str);
1284                 for (i = 0; debug_strings[i]; i++)
1285                         if (g_strcmp0(debug_strings[i], str) == 0) {
1286                                 debug_level = i;
1287                                 break;
1288                         }
1289                 DBG("Debug level %d", debug_level);
1290         } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
1291                 dbus_message_iter_get_basic(iter, &debug_timestamp);
1292                 DBG("Debug timestamp %u", debug_timestamp);
1293         } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
1294                 dbus_message_iter_get_basic(iter, &debug_showkeys);
1295                 DBG("Debug show keys %u", debug_showkeys);
1296         } else if (g_strcmp0(key, "Interfaces") == 0) {
1297                 supplicant_dbus_array_foreach(iter, interface_added, NULL);
1298         } else if (g_strcmp0(key, "EapMethods") == 0) {
1299                 supplicant_dbus_array_foreach(iter, eap_method, NULL);
1300                 debug_strvalmap("EAP method", eap_method_map, eap_methods);
1301         } else
1302                 DBG("key %s type %c",
1303                                 key, dbus_message_iter_get_arg_type(iter));
1304 }
1305
1306 static void supplicant_bootstrap(void)
1307 {
1308         supplicant_dbus_property_get_all(SUPPLICANT_PATH,
1309                                                 SUPPLICANT_INTERFACE,
1310                                                 service_property, NULL);
1311 }
1312
1313 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
1314 {
1315         const char *name = NULL, *old = NULL, *new = NULL;
1316
1317         if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
1318                 return;
1319
1320         dbus_message_iter_get_basic(iter, &name);
1321         if (!name)
1322                 return;
1323
1324         if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
1325                 return;
1326
1327         dbus_message_iter_next(iter);
1328         dbus_message_iter_get_basic(iter, &old);
1329         dbus_message_iter_next(iter);
1330         dbus_message_iter_get_basic(iter, &new);
1331
1332         if (!old || !new)
1333                 return;
1334
1335         if (strlen(old) > 0 && strlen(new) == 0) {
1336                 system_available = FALSE;
1337                 g_hash_table_remove_all(bss_mapping);
1338                 g_hash_table_remove_all(interface_table);
1339                 callback_system_killed();
1340         }
1341
1342         if (strlen(new) > 0 && strlen(old) == 0) {
1343                 system_available = TRUE;
1344                 supplicant_bootstrap();
1345         }
1346 }
1347
1348 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
1349 {
1350         if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
1351                 return;
1352
1353         supplicant_dbus_property_foreach(iter, service_property, NULL);
1354 }
1355
1356 static void signal_interface_added(const char *path, DBusMessageIter *iter)
1357 {
1358         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1359                 interface_added(iter, NULL);
1360 }
1361
1362 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
1363 {
1364         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1365                 interface_removed(iter, NULL);
1366 }
1367
1368 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
1369 {
1370         struct supplicant_interface *interface;
1371
1372         interface = g_hash_table_lookup(interface_table, path);
1373         if (!interface)
1374                 return;
1375
1376         supplicant_dbus_property_foreach(iter, interface_property, interface);
1377 }
1378
1379 static void signal_scan_done(const char *path, DBusMessageIter *iter)
1380 {
1381         struct supplicant_interface *interface;
1382         dbus_bool_t success = FALSE;
1383
1384         interface = g_hash_table_lookup(interface_table, path);
1385         if (!interface)
1386                 return;
1387
1388         dbus_message_iter_get_basic(iter, &success);
1389
1390         if (interface->scan_callback) {
1391                 int result = 0;
1392
1393                 if (!success)
1394                         result = -EIO;
1395
1396                 interface->scan_callback(result, interface->scan_data);
1397         }
1398
1399         interface->scan_callback = NULL;
1400         interface->scan_data = NULL;
1401 }
1402
1403 static void signal_bss_added(const char *path, DBusMessageIter *iter)
1404 {
1405         struct supplicant_interface *interface;
1406
1407         interface = g_hash_table_lookup(interface_table, path);
1408         if (!interface)
1409                 return;
1410
1411         interface_bss_added(iter, interface);
1412 }
1413
1414 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
1415 {
1416         struct supplicant_interface *interface;
1417
1418         interface = g_hash_table_lookup(interface_table, path);
1419         if (!interface)
1420                 return;
1421
1422         interface_bss_removed(iter, interface);
1423 }
1424
1425 static void signal_network_added(const char *path, DBusMessageIter *iter)
1426 {
1427         struct supplicant_interface *interface;
1428
1429         interface = g_hash_table_lookup(interface_table, path);
1430         if (!interface)
1431                 return;
1432
1433         interface_network_added(iter, interface);
1434 }
1435
1436 static void signal_network_removed(const char *path, DBusMessageIter *iter)
1437 {
1438         struct supplicant_interface *interface;
1439
1440         interface = g_hash_table_lookup(interface_table, path);
1441         if (!interface)
1442                 return;
1443
1444         interface_network_removed(iter, interface);
1445 }
1446
1447 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
1448 {
1449         struct supplicant_interface *interface;
1450         struct supplicant_network *network;
1451         struct supplicant_bss *bss;
1452
1453         interface = g_hash_table_lookup(bss_mapping, path);
1454         if (!interface)
1455                 return;
1456
1457         network = g_hash_table_lookup(interface->bss_mapping, path);
1458         if (!network)
1459                 return;
1460
1461         bss = g_hash_table_lookup(network->bss_table, path);
1462         if (!bss)
1463                 return;
1464
1465         supplicant_dbus_property_foreach(iter, bss_property, bss);
1466 }
1467
1468 static struct {
1469         const char *interface;
1470         const char *member;
1471         void (*function) (const char *path, DBusMessageIter *iter);
1472 } signal_map[] = {
1473         { DBUS_INTERFACE_DBUS,  "NameOwnerChanged",  signal_name_owner_changed },
1474
1475         { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
1476         { SUPPLICANT_INTERFACE, "InterfaceAdded",    signal_interface_added    },
1477         { SUPPLICANT_INTERFACE, "InterfaceCreated",  signal_interface_added    },
1478         { SUPPLICANT_INTERFACE, "InterfaceRemoved",  signal_interface_removed  },
1479
1480         { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
1481         { SUPPLICANT_INTERFACE ".Interface", "ScanDone",          signal_scan_done         },
1482         { SUPPLICANT_INTERFACE ".Interface", "BSSAdded",          signal_bss_added         },
1483         { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved",        signal_bss_removed       },
1484         { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded",      signal_network_added     },
1485         { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved",    signal_network_removed   },
1486
1487         { SUPPLICANT_INTERFACE ".Interface.BSS", "PropertiesChanged", signal_bss_changed   },
1488
1489         { }
1490 };
1491
1492 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1493                                         DBusMessage *message, void *data)
1494 {
1495         DBusMessageIter iter;
1496         const char *path;
1497         int i;
1498
1499         path = dbus_message_get_path(message);
1500         if (!path)
1501                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1502
1503         if (!dbus_message_iter_init(message, &iter))
1504                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1505
1506         for (i = 0; signal_map[i].interface; i++) {
1507                 if (!dbus_message_has_interface(message, signal_map[i].interface))
1508                         continue;
1509
1510                 if (!dbus_message_has_member(message, signal_map[i].member))
1511                         continue;
1512
1513                 signal_map[i].function(path, &iter);
1514                 break;
1515         }
1516
1517         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1518 }
1519
1520 static const char *supplicant_rule0 = "type=signal,"
1521                                         "path=" DBUS_PATH_DBUS ","
1522                                         "sender=" DBUS_SERVICE_DBUS ","
1523                                         "interface=" DBUS_INTERFACE_DBUS ","
1524                                         "member=NameOwnerChanged,"
1525                                         "arg0=" SUPPLICANT_SERVICE;
1526 static const char *supplicant_rule1 = "type=signal,"
1527                         "interface=" SUPPLICANT_INTERFACE;
1528 static const char *supplicant_rule2 = "type=signal,"
1529                         "interface=" SUPPLICANT_INTERFACE ".Interface";
1530 static const char *supplicant_rule3 = "type=signal,"
1531                         "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
1532 static const char *supplicant_rule4 = "type=signal,"
1533                         "interface=" SUPPLICANT_INTERFACE ".Interface.BSS";
1534 static const char *supplicant_rule5 = "type=signal,"
1535                         "interface=" SUPPLICANT_INTERFACE ".Interface.Network";
1536 static const char *supplicant_rule6 = "type=signal,"
1537                         "interface=" SUPPLICANT_INTERFACE ".Interface.Blob";
1538
1539 int supplicant_register(const struct supplicant_callbacks *callbacks)
1540 {
1541         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1542         if (!connection)
1543                 return -EIO;
1544
1545         if (!dbus_connection_add_filter(connection, supplicant_filter, NULL, NULL)) {
1546                 dbus_connection_unref(connection);
1547                 connection = NULL;
1548                 return -EIO;
1549         }
1550
1551         callbacks_pointer = callbacks;
1552         eap_methods = 0;
1553
1554         interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1555                                                 NULL, remove_interface);
1556
1557         bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1558                                                                 NULL, NULL);
1559
1560         supplicant_dbus_setup(connection);
1561
1562         dbus_bus_add_match(connection, supplicant_rule0, NULL);
1563         dbus_bus_add_match(connection, supplicant_rule1, NULL);
1564         dbus_bus_add_match(connection, supplicant_rule2, NULL);
1565         dbus_bus_add_match(connection, supplicant_rule3, NULL);
1566         dbus_bus_add_match(connection, supplicant_rule4, NULL);
1567         dbus_bus_add_match(connection, supplicant_rule5, NULL);
1568         dbus_bus_add_match(connection, supplicant_rule6, NULL);
1569         dbus_connection_flush(connection);
1570
1571         if (dbus_bus_name_has_owner(connection,
1572                                         SUPPLICANT_SERVICE, NULL)) {
1573                 system_available = TRUE;
1574                 supplicant_bootstrap();
1575         }
1576
1577         return 0;
1578 }
1579
1580 void supplicant_unregister(const struct supplicant_callbacks *callbacks)
1581 {
1582         if (connection) {
1583                 dbus_bus_remove_match(connection, supplicant_rule6, NULL);
1584                 dbus_bus_remove_match(connection, supplicant_rule5, NULL);
1585                 dbus_bus_remove_match(connection, supplicant_rule4, NULL);
1586                 dbus_bus_remove_match(connection, supplicant_rule3, NULL);
1587                 dbus_bus_remove_match(connection, supplicant_rule2, NULL);
1588                 dbus_bus_remove_match(connection, supplicant_rule1, NULL);
1589                 dbus_bus_remove_match(connection, supplicant_rule0, NULL);
1590                 dbus_connection_flush(connection);
1591
1592                 dbus_connection_remove_filter(connection,
1593                                                 supplicant_filter, NULL);
1594         }
1595
1596         if (bss_mapping) {
1597                 g_hash_table_destroy(bss_mapping);
1598                 bss_mapping = NULL;
1599         }
1600
1601         if (interface_table) {
1602                 g_hash_table_destroy(interface_table);
1603                 interface_table = NULL;
1604         }
1605
1606         if (system_available)
1607                 callback_system_killed();
1608
1609         if (connection) {
1610                 dbus_connection_unref(connection);
1611                 connection = NULL;
1612         }
1613
1614         callbacks_pointer = NULL;
1615         eap_methods = 0;
1616 }
1617
1618 static void debug_level_result(const char *error,
1619                                 DBusMessageIter *iter, void *user_data)
1620 {
1621         if (error)
1622                 DBG("debug level failure: %s", error);
1623 }
1624
1625 static void debug_level_params(DBusMessageIter *iter, void *user_data)
1626 {
1627         guint level = GPOINTER_TO_UINT(user_data);
1628         const char *str;
1629
1630         if (level > 4)
1631                 level = 4;
1632
1633         str = debug_strings[level];
1634
1635         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &str);
1636 }
1637
1638 void supplicant_set_debug_level(unsigned int level)
1639 {
1640         if (!system_available)
1641                 return;
1642
1643         supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
1644                                 "DebugLevel", DBUS_TYPE_STRING_AS_STRING,
1645                                 debug_level_params, debug_level_result,
1646                                                 GUINT_TO_POINTER(level));
1647 }
1648
1649 struct interface_create_data {
1650         const char *ifname;
1651         const char *driver;
1652         struct supplicant_interface *interface;
1653         supplicant_interface_create_callback callback;
1654         void *user_data;
1655 };
1656
1657 static void interface_create_property(const char *key, DBusMessageIter *iter,
1658                                                         void *user_data)
1659 {
1660         struct interface_create_data *data = user_data;
1661         struct supplicant_interface *interface = data->interface;
1662
1663         if (!key) {
1664                 if (data->callback)
1665                         data->callback(0, data->interface, data->user_data);
1666
1667                 dbus_free(data);
1668         }
1669
1670         interface_property(key, iter, interface);
1671 }
1672
1673 static void interface_create_result(const char *error,
1674                                 DBusMessageIter *iter, void *user_data)
1675 {
1676         struct interface_create_data *data = user_data;
1677         const char *path = NULL;
1678         int err;
1679
1680         if (error) {
1681                 err = -EIO;
1682                 goto done;
1683         }
1684
1685         dbus_message_iter_get_basic(iter, &path);
1686         if (!path) {
1687                 err = -EINVAL;
1688                 goto done;
1689         }
1690
1691         if (!system_available) {
1692                 err = -EFAULT;
1693                 goto done;
1694         }
1695
1696         data->interface = g_hash_table_lookup(interface_table, path);
1697         if (!data->interface) {
1698                 data->interface = interface_alloc(path);
1699                 if (!data->interface) {
1700                         err = -ENOMEM;
1701                         goto done;
1702                 }
1703         }
1704
1705         err = supplicant_dbus_property_get_all(path,
1706                                         SUPPLICANT_INTERFACE ".Interface",
1707                                         interface_create_property, data);
1708         if (err == 0)
1709                 return;
1710
1711 done:
1712         if (data->callback)
1713                 data->callback(err, NULL, data->user_data);
1714
1715         dbus_free(data);
1716 }
1717
1718 static void interface_create_params(DBusMessageIter *iter, void *user_data)
1719 {
1720         struct interface_create_data *data = user_data;
1721         DBusMessageIter dict;
1722
1723         supplicant_dbus_dict_open(iter, &dict);
1724
1725         supplicant_dbus_dict_append_basic(&dict, "Ifname",
1726                                         DBUS_TYPE_STRING, &data->ifname);
1727
1728         if (data->driver)
1729                 supplicant_dbus_dict_append_basic(&dict, "Driver",
1730                                         DBUS_TYPE_STRING, &data->driver);
1731
1732         supplicant_dbus_dict_close(iter, &dict);
1733 }
1734
1735 static void interface_get_result(const char *error,
1736                                 DBusMessageIter *iter, void *user_data)
1737 {
1738         struct interface_create_data *data = user_data;
1739         struct supplicant_interface *interface;
1740         const char *path = NULL;
1741         int err;
1742
1743         if (error)
1744                 goto create;
1745
1746         dbus_message_iter_get_basic(iter, &path);
1747         if (!path) {
1748                 err = -EINVAL;
1749                 goto done;
1750         }
1751
1752         interface = g_hash_table_lookup(interface_table, path);
1753         if (!interface) {
1754                 err = -ENOENT;
1755                 goto done;
1756         }
1757
1758         if (data->callback)
1759                 data->callback(0, interface, data->user_data);
1760
1761         dbus_free(data);
1762
1763         return;
1764
1765 create:
1766         if (!system_available) {
1767                 err = -EFAULT;
1768                 goto done;
1769         }
1770
1771         err = supplicant_dbus_method_call(SUPPLICANT_PATH,
1772                                                 SUPPLICANT_INTERFACE,
1773                                                 "CreateInterface",
1774                                                 interface_create_params,
1775                                                 interface_create_result, data);
1776         if (err == 0)
1777                 return;
1778
1779 done:
1780         if (data->callback)
1781                 data->callback(err, NULL, data->user_data);
1782
1783         dbus_free(data);
1784 }
1785
1786 static void interface_get_params(DBusMessageIter *iter, void *user_data)
1787 {
1788         struct interface_create_data *data = user_data;
1789
1790         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
1791 }
1792
1793 int supplicant_interface_create(const char *ifname, const char *driver,
1794                         supplicant_interface_create_callback callback,
1795                                                         void *user_data)
1796 {
1797         struct interface_create_data *data;
1798
1799         if (!ifname)
1800                 return -EINVAL;
1801
1802         if (!system_available)
1803                 return -EFAULT;
1804
1805         data = dbus_malloc0(sizeof(*data));
1806         if (!data)
1807                 return -ENOMEM;
1808
1809         data->ifname = ifname;
1810         data->driver = driver;
1811         data->callback = callback;
1812         data->user_data = user_data;
1813
1814         return supplicant_dbus_method_call(SUPPLICANT_PATH,
1815                                                 SUPPLICANT_INTERFACE,
1816                                                 "GetInterface",
1817                                                 interface_get_params,
1818                                                 interface_get_result, data);
1819 }
1820
1821 int supplicant_interface_remove(struct supplicant_interface *interface,
1822                         supplicant_interface_remove_callback callback,
1823                                                         void *user_data)
1824 {
1825         if (!interface)
1826                 return -EINVAL;
1827
1828         if (!system_available)
1829                 return -EFAULT;
1830
1831         return 0;
1832 }
1833
1834 struct interface_scan_data {
1835         struct supplicant_interface *interface;
1836         supplicant_interface_scan_callback callback;
1837         void *user_data;
1838 };
1839
1840 static void interface_scan_result(const char *error,
1841                                 DBusMessageIter *iter, void *user_data)
1842 {
1843         struct interface_scan_data *data = user_data;
1844
1845         if (error) {
1846                 if (data->callback)
1847                         data->callback(-EIO, data->user_data);
1848         } else {
1849                 data->interface->scan_callback = data->callback;
1850                 data->interface->scan_data = data->user_data;
1851         }
1852
1853         dbus_free(data);
1854 }
1855
1856 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
1857 {
1858         DBusMessageIter dict;
1859         const char *type = "passive";
1860
1861         supplicant_dbus_dict_open(iter, &dict);
1862
1863         supplicant_dbus_dict_append_basic(&dict, "Type",
1864                                                 DBUS_TYPE_STRING, &type);
1865
1866         supplicant_dbus_dict_close(iter, &dict);
1867 }
1868
1869 int supplicant_interface_scan(struct supplicant_interface *interface,
1870                         supplicant_interface_scan_callback callback,
1871                                                         void *user_data)
1872 {
1873         struct interface_scan_data *data;
1874
1875         if (!interface)
1876                 return -EINVAL;
1877
1878         if (!system_available)
1879                 return -EFAULT;
1880
1881         if (interface->scanning)
1882                 return -EALREADY;
1883
1884         data = dbus_malloc0(sizeof(*data));
1885         if (!data)
1886                 return -ENOMEM;
1887
1888         data->interface = interface;
1889         data->callback = callback;
1890         data->user_data = user_data;
1891
1892         return supplicant_dbus_method_call(interface->path,
1893                         SUPPLICANT_INTERFACE ".Interface", "Scan",
1894                         interface_scan_params, interface_scan_result, data);
1895 }
1896
1897 struct interface_disconnect_data {
1898         supplicant_interface_disconnect_callback callback;
1899         void *user_data;
1900 };
1901
1902 static void interface_disconnect_result(const char *error,
1903                                 DBusMessageIter *iter, void *user_data)
1904 {
1905         struct interface_disconnect_data *data = user_data;
1906         int result = 0;
1907
1908         if (error)
1909                 result = -EIO;
1910
1911         if (data->callback)
1912                 data->callback(result, data->user_data);
1913
1914         dbus_free(data);
1915 }
1916
1917 int supplicant_interface_disconnect(struct supplicant_interface *interface,
1918                         supplicant_interface_disconnect_callback callback,
1919                                                         void *user_data)
1920 {
1921         struct interface_disconnect_data *data;
1922
1923         if (!interface)
1924                 return -EINVAL;
1925
1926         if (!system_available)
1927                 return -EFAULT;
1928
1929         data = dbus_malloc0(sizeof(*data));
1930         if (!data)
1931                 return -ENOMEM;
1932
1933         data->callback = callback;
1934         data->user_data = user_data;
1935
1936         return supplicant_dbus_method_call(interface->path,
1937                         SUPPLICANT_INTERFACE ".Interface", "Disconnect",
1938                                 NULL, interface_disconnect_result, data);
1939 }