technology: Implement Technology Scan and scan start/stop functionality
[framework/connectivity/connman.git] / tools / supplicant.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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 == NULL)
188                 return SUPPLICANT_MODE_UNKNOWN;
189
190         if (g_str_equal(mode, "infrastructure") == TRUE)
191                 return SUPPLICANT_MODE_INFRA;
192         else if (g_str_equal(mode, "ad-hoc") == TRUE)
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 == NULL)
233                 return SUPPLICANT_STATE_UNKNOWN;
234
235         if (g_str_equal(state, "unknown") == TRUE)
236                 return SUPPLICANT_STATE_UNKNOWN;
237         else if (g_str_equal(state, "disconnected") == TRUE)
238                 return SUPPLICANT_STATE_DISCONNECTED;
239         else if (g_str_equal(state, "inactive") == TRUE)
240                 return SUPPLICANT_STATE_INACTIVE;
241         else if (g_str_equal(state, "scanning") == TRUE)
242                 return SUPPLICANT_STATE_SCANNING;
243         else if (g_str_equal(state, "authenticating") == TRUE)
244                 return SUPPLICANT_STATE_AUTHENTICATING;
245         else if (g_str_equal(state, "associating") == TRUE)
246                 return SUPPLICANT_STATE_ASSOCIATING;
247         else if (g_str_equal(state, "associated") == TRUE)
248                 return SUPPLICANT_STATE_ASSOCIATED;
249         else if (g_str_equal(state, "group_handshake") == TRUE)
250                 return SUPPLICANT_STATE_GROUP_HANDSHAKE;
251         else if (g_str_equal(state, "4way_handshake") == TRUE)
252                 return SUPPLICANT_STATE_4WAY_HANDSHAKE;
253         else if (g_str_equal(state, "completed") == TRUE)
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 == TRUE)
262                 return;
263
264         system_ready = TRUE;
265
266         if (callbacks_pointer == NULL)
267                 return;
268
269         if (callbacks_pointer->system_ready == NULL)
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 == NULL)
280                 return;
281
282         if (callbacks_pointer->system_killed == NULL)
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 == NULL)
291                 return;
292
293         if (callbacks_pointer->interface_added == NULL)
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 == NULL)
302                 return;
303
304         if (callbacks_pointer->interface_removed == NULL)
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 == NULL)
313                 return;
314
315         if (callbacks_pointer->scan_started == NULL)
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 == NULL)
324                 return;
325
326         if (callbacks_pointer->scan_finished == NULL)
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 == NULL)
335                 return;
336
337         if (callbacks_pointer->network_added == NULL)
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 == NULL)
346                 return;
347
348         if (callbacks_pointer->network_removed == NULL)
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 != NULL; 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 == NULL)
413                 return;
414
415         for (i = 0; keymgmt_capa_map[i].str != NULL; 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 == NULL)
430                 return;
431
432         for (i = 0; authalg_capa_map[i].str != NULL; 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 == NULL)
447                 return;
448
449         for (i = 0; proto_capa_map[i].str != NULL; 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 == NULL)
464                 return;
465
466         for (i = 0; pairwise_capa_map[i].str != NULL; 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 == NULL)
481                 return;
482
483         for (i = 0; group_capa_map[i].str != NULL; 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 == NULL)
498                 return;
499
500         for (i = 0; scan_capa_map[i].str != NULL; 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 == NULL)
515                 return;
516
517         for (i = 0; mode_capa_map[i].str != NULL; 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 == NULL)
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 == NULL)
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 == NULL)
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 == NULL)
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 == NULL || network->name == NULL)
585                 return "";
586
587         return network->name;
588 }
589
590 const char *supplicant_network_get_identifier(struct supplicant_network *network)
591 {
592         if (network == NULL || network->group == NULL)
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 == NULL)
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 != NULL)
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 == NULL)
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 == NULL)
657                 return;
658
659         if (key == NULL) {
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 != NULL)
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 == NULL)
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 != NULL)
695                 return;
696
697         network = g_try_new0(struct supplicant_network, 1);
698         if (network == NULL)
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 == NULL)
728                 return;
729
730         network = g_hash_table_lookup(interface->net_mapping, path);
731         if (network == NULL)
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 == NULL)
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 == NULL)
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 != NULL)
778                 g_string_append_printf(str, "_%s", mode);
779
780         security = security2string(bss->security);
781         if (security != NULL)
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 == NULL)
795                 return;
796
797         network = g_hash_table_lookup(interface->network_table, group);
798         if (network != NULL) {
799                 g_free(group);
800                 goto done;
801         }
802
803         network = g_try_new0(struct supplicant_network, 1);
804         if (network == NULL) {
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         buf += 2 + (count * 4);
898         len -= 2 + (count * 4);
899 }
900
901 static void bss_rates(DBusMessageIter *iter, void *user_data)
902 {
903         struct supplicant_bss *bss = user_data;
904         dbus_uint32_t rate = 0;
905
906         dbus_message_iter_get_basic(iter, &rate);
907         if (rate == 0)
908                 return;
909
910         if (rate > bss->maxrate)
911                 bss->maxrate = rate;
912 }
913
914 static void bss_property(const char *key, DBusMessageIter *iter,
915                                                         void *user_data)
916 {
917         struct supplicant_bss *bss = user_data;
918
919         if (bss->interface == NULL)
920                 return;
921
922         if (key == NULL) {
923                 if (bss->ieee8021x == TRUE)
924                         bss->security = SUPPLICANT_SECURITY_IEEE8021X;
925                 else if (bss->psk == TRUE)
926                         bss->security = SUPPLICANT_SECURITY_PSK;
927                 else if (bss->privacy == TRUE)
928                         bss->security = SUPPLICANT_SECURITY_WEP;
929                 else
930                         bss->security = SUPPLICANT_SECURITY_NONE;
931
932                 add_bss_to_network(bss);
933                 return;
934         }
935
936         if (g_strcmp0(key, "BSSID") == 0) {
937                 DBusMessageIter array;
938                 unsigned char *addr;
939                 int addr_len;
940
941                 dbus_message_iter_recurse(iter, &array);
942                 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
943
944                 if (addr_len == 6)
945                         memcpy(bss->bssid, addr, addr_len);
946         } else if (g_strcmp0(key, "SSID") == 0) {
947                 DBusMessageIter array;
948                 unsigned char *ssid;
949                 int ssid_len;
950
951                 dbus_message_iter_recurse(iter, &array);
952                 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
953
954                 if (ssid_len > 0 && ssid_len < 33) {
955                         memcpy(bss->ssid, ssid, ssid_len);
956                         bss->ssid_len = ssid_len;
957                 } else {
958                         memset(bss->ssid, 0, sizeof(bss->ssid));
959                         bss->ssid_len = 0;
960                 }
961         } else if (g_strcmp0(key, "Capabilities") == 0) {
962                 dbus_uint16_t capabilities = 0x0000;
963
964                 dbus_message_iter_get_basic(iter, &capabilities);
965
966                 if (capabilities & IEEE80211_CAP_ESS)
967                         bss->mode = SUPPLICANT_MODE_INFRA;
968                 else if (capabilities & IEEE80211_CAP_IBSS)
969                         bss->mode = SUPPLICANT_MODE_IBSS;
970
971                 if (capabilities & IEEE80211_CAP_PRIVACY)
972                         bss->privacy = TRUE;
973         } else if (g_strcmp0(key, "Mode") == 0) {
974                 const char *mode = NULL;
975
976                 dbus_message_iter_get_basic(iter, &mode);
977                 bss->mode = string2mode(mode);
978         } else if (g_strcmp0(key, "Frequency") == 0) {
979                 dbus_uint16_t frequency = 0;
980
981                 dbus_message_iter_get_basic(iter, &frequency);
982                 bss->frequency = frequency;
983         } else if (g_strcmp0(key, "Signal") == 0) {
984                 dbus_int16_t signal = 0;
985
986                 dbus_message_iter_get_basic(iter, &signal);
987         } else if (g_strcmp0(key, "Level") == 0) {
988                 dbus_int32_t level = 0;
989
990                 dbus_message_iter_get_basic(iter, &level);
991         } else if (g_strcmp0(key, "Rates") == 0) {
992                 supplicant_dbus_array_foreach(iter, bss_rates, bss);
993         } else if (g_strcmp0(key, "MaxRate") == 0) {
994                 dbus_uint32_t maxrate = 0;
995
996                 dbus_message_iter_get_basic(iter, &maxrate);
997                 if (maxrate != 0)
998                         bss->maxrate =maxrate;
999         } else if (g_strcmp0(key, "Privacy") == 0) {
1000                 dbus_bool_t privacy = FALSE;
1001
1002                 dbus_message_iter_get_basic(iter, &privacy);
1003                 bss->privacy = privacy;
1004         } else if (g_strcmp0(key, "RSNIE") == 0) {
1005                 DBusMessageIter array;
1006                 unsigned char *ie;
1007                 int ie_len;
1008
1009                 dbus_message_iter_recurse(iter, &array);
1010                 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1011
1012                 if (ie_len > 2)
1013                         extract_rsn(bss, ie + 2, ie_len - 2);
1014         } else if (g_strcmp0(key, "WPAIE") == 0) {
1015                 DBusMessageIter array;
1016                 unsigned char *ie;
1017                 int ie_len;
1018
1019                 dbus_message_iter_recurse(iter, &array);
1020                 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1021
1022                 if (ie_len > 6)
1023                         extract_rsn(bss, ie + 6, ie_len - 6);
1024         } else if (g_strcmp0(key, "WPSIE") == 0) {
1025                 DBusMessageIter array;
1026                 unsigned char *ie;
1027                 int ie_len;
1028
1029                 dbus_message_iter_recurse(iter, &array);
1030                 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1031         } else
1032                 DBG("key %s type %c",
1033                                 key, dbus_message_iter_get_arg_type(iter));
1034 }
1035
1036 static void interface_bss_added(DBusMessageIter *iter, void *user_data)
1037 {
1038         struct supplicant_interface *interface = user_data;
1039         struct supplicant_network *network;
1040         struct supplicant_bss *bss;
1041         const char *path = NULL;
1042
1043         dbus_message_iter_get_basic(iter, &path);
1044         if (path == NULL)
1045                 return;
1046
1047         if (g_strcmp0(path, "/") == 0)
1048                 return;
1049
1050         network = g_hash_table_lookup(interface->bss_mapping, path);
1051         if (network != NULL) {
1052                 bss = g_hash_table_lookup(network->bss_table, path);
1053                 if (bss != NULL)
1054                         return;
1055         }
1056
1057         bss = g_try_new0(struct supplicant_bss, 1);
1058         if (bss == NULL)
1059                 return;
1060
1061         bss->interface = interface;
1062         bss->path = g_strdup(path);
1063
1064         dbus_message_iter_next(iter);
1065         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1066                 supplicant_dbus_property_foreach(iter, bss_property, bss);
1067                 bss_property(NULL, NULL, bss);
1068                 return;
1069         }
1070
1071         supplicant_dbus_property_get_all(path,
1072                                         SUPPLICANT_INTERFACE ".Interface.BSS",
1073                                                         bss_property, bss);
1074 }
1075
1076 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
1077 {
1078         struct supplicant_interface *interface = user_data;
1079         struct supplicant_network *network;
1080         const char *path = NULL;
1081
1082         dbus_message_iter_get_basic(iter, &path);
1083         if (path == NULL)
1084                 return;
1085
1086         network = g_hash_table_lookup(interface->bss_mapping, path);
1087         if (network == NULL)
1088                 return;
1089
1090         g_hash_table_remove(bss_mapping, path);
1091
1092         g_hash_table_remove(interface->bss_mapping, path);
1093         g_hash_table_remove(network->bss_table, path);
1094
1095         if (g_hash_table_size(network->bss_table) == 0)
1096                 g_hash_table_remove(interface->network_table, network->group);
1097 }
1098
1099 static void interface_property(const char *key, DBusMessageIter *iter,
1100                                                         void *user_data)
1101 {
1102         struct supplicant_interface *interface = user_data;
1103
1104         if (interface == NULL)
1105                 return;
1106
1107         if (key == NULL) {
1108                 debug_strvalmap("KeyMgmt capability", keymgmt_capa_map,
1109                                                 interface->keymgmt_capa);
1110                 debug_strvalmap("AuthAlg capability", authalg_capa_map,
1111                                                 interface->authalg_capa);
1112                 debug_strvalmap("Protocol capability", proto_capa_map,
1113                                                 interface->proto_capa);
1114                 debug_strvalmap("Pairwise capability", pairwise_capa_map,
1115                                                 interface->pairwise_capa);
1116                 debug_strvalmap("Group capability", group_capa_map,
1117                                                 interface->group_capa);
1118                 debug_strvalmap("Scan capability", scan_capa_map,
1119                                                 interface->scan_capa);
1120                 debug_strvalmap("Mode capability", mode_capa_map,
1121                                                 interface->mode_capa);
1122
1123                 interface->ready = TRUE;
1124                 callback_interface_added(interface);
1125                 return;
1126         }
1127
1128         if (g_strcmp0(key, "Capabilities") == 0) {
1129                 supplicant_dbus_property_foreach(iter, interface_capability,
1130                                                                 interface);
1131         } else if (g_strcmp0(key, "State") == 0) {
1132                 const char *str = NULL;
1133
1134                 dbus_message_iter_get_basic(iter, &str);
1135                 if (str != NULL)
1136                         interface->state = string2state(str);
1137
1138                 DBG("state %s (%d)", str, interface->state);
1139         } else if (g_strcmp0(key, "Scanning") == 0) {
1140                 dbus_bool_t scanning = FALSE;
1141
1142                 dbus_message_iter_get_basic(iter, &scanning);
1143                 interface->scanning = scanning;
1144
1145                 if (interface->ready == TRUE) {
1146                         if (interface->scanning == TRUE)
1147                                 callback_scan_started(interface);
1148                         else
1149                                 callback_scan_finished(interface);
1150                 }
1151         } else if (g_strcmp0(key, "ApScan") == 0) {
1152                 int apscan = 1;
1153
1154                 dbus_message_iter_get_basic(iter, &apscan);
1155                 interface->apscan = apscan;
1156         } else if (g_strcmp0(key, "Ifname") == 0) {
1157                 const char *str = NULL;
1158
1159                 dbus_message_iter_get_basic(iter, &str);
1160                 if (str != NULL)
1161                         interface->ifname = g_strdup(str);
1162         } else if (g_strcmp0(key, "Driver") == 0) {
1163                 const char *str = NULL;
1164
1165                 dbus_message_iter_get_basic(iter, &str);
1166                 if (str != NULL)
1167                         interface->driver = g_strdup(str);
1168         } else if (g_strcmp0(key, "BridgeIfname") == 0) {
1169                 const char *str = NULL;
1170
1171                 dbus_message_iter_get_basic(iter, &str);
1172                 if (str != NULL)
1173                         interface->bridge = g_strdup(str);
1174         } else if (g_strcmp0(key, "CurrentBSS") == 0) {
1175                 interface_bss_added(iter, interface);
1176         } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
1177                 interface_network_added(iter, interface);
1178         } else if (g_strcmp0(key, "BSSs") == 0) {
1179                 supplicant_dbus_array_foreach(iter, interface_bss_added,
1180                                                                 interface);
1181         } else if (g_strcmp0(key, "Blobs") == 0) {
1182         } else if (g_strcmp0(key, "Networks") == 0) {
1183                 supplicant_dbus_array_foreach(iter, interface_network_added,
1184                                                                 interface);
1185         } else
1186                 DBG("key %s type %c",
1187                                 key, dbus_message_iter_get_arg_type(iter));
1188 }
1189
1190 static struct supplicant_interface *interface_alloc(const char *path)
1191 {
1192         struct supplicant_interface *interface;
1193
1194         interface = g_try_new0(struct supplicant_interface, 1);
1195         if (interface == NULL)
1196                 return NULL;
1197
1198         interface->path = g_strdup(path);
1199
1200         interface->network_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1201                                                         NULL, remove_network);
1202
1203         interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1204                                                                 NULL, NULL);
1205         interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1206                                                                 NULL, NULL);
1207
1208         g_hash_table_replace(interface_table, interface->path, interface);
1209
1210         return interface;
1211 }
1212
1213 static void interface_added(DBusMessageIter *iter, void *user_data)
1214 {
1215         struct supplicant_interface *interface;
1216         const char *path = NULL;
1217
1218         dbus_message_iter_get_basic(iter, &path);
1219         if (path == NULL)
1220                 return;
1221
1222         if (g_strcmp0(path, "/") == 0)
1223                 return;
1224
1225         interface = g_hash_table_lookup(interface_table, path);
1226         if (interface != NULL)
1227                 return;
1228
1229         interface = interface_alloc(path);
1230         if (interface == NULL)
1231                 return;
1232
1233         dbus_message_iter_next(iter);
1234         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1235                 supplicant_dbus_property_foreach(iter, interface_property,
1236                                                                 interface);
1237                 interface_property(NULL, NULL, interface);
1238                 return;
1239         }
1240
1241         supplicant_dbus_property_get_all(path,
1242                                         SUPPLICANT_INTERFACE ".Interface",
1243                                                 interface_property, interface);
1244 }
1245
1246 static void interface_removed(DBusMessageIter *iter, void *user_data)
1247 {
1248         const char *path = NULL;
1249
1250         dbus_message_iter_get_basic(iter, &path);
1251         if (path == NULL)
1252                 return;
1253
1254         g_hash_table_remove(interface_table, path);
1255 }
1256
1257 static void eap_method(DBusMessageIter *iter, void *user_data)
1258 {
1259         const char *str = NULL;
1260         int i;
1261
1262         dbus_message_iter_get_basic(iter, &str);
1263         if (str == NULL)
1264                 return;
1265
1266         for (i = 0; eap_method_map[i].str != NULL; i++)
1267                 if (strcmp(str, eap_method_map[i].str) == 0) {
1268                         eap_methods |= eap_method_map[i].val;
1269                         break;
1270                 }
1271 }
1272
1273 static void service_property(const char *key, DBusMessageIter *iter,
1274                                                         void *user_data)
1275 {
1276         if (key == NULL) {
1277                 callback_system_ready();
1278                 return;
1279         }
1280
1281         if (g_strcmp0(key, "DebugLevel") == 0) {
1282                 const char *str = NULL;
1283                 int i;
1284
1285                 dbus_message_iter_get_basic(iter, &str);
1286                 for (i = 0; debug_strings[i] != NULL; i++)
1287                         if (g_strcmp0(debug_strings[i], str) == 0) {
1288                                 debug_level = i;
1289                                 break;
1290                         }
1291                 DBG("Debug level %d", debug_level);
1292         } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
1293                 dbus_message_iter_get_basic(iter, &debug_timestamp);
1294                 DBG("Debug timestamp %u", debug_timestamp);
1295         } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
1296                 dbus_message_iter_get_basic(iter, &debug_showkeys);
1297                 DBG("Debug show keys %u", debug_showkeys);
1298         } else if (g_strcmp0(key, "Interfaces") == 0) {
1299                 supplicant_dbus_array_foreach(iter, interface_added, NULL);
1300         } else if (g_strcmp0(key, "EapMethods") == 0) {
1301                 supplicant_dbus_array_foreach(iter, eap_method, NULL);
1302                 debug_strvalmap("EAP method", eap_method_map, eap_methods);
1303         } else
1304                 DBG("key %s type %c",
1305                                 key, dbus_message_iter_get_arg_type(iter));
1306 }
1307
1308 static void supplicant_bootstrap(void)
1309 {
1310         supplicant_dbus_property_get_all(SUPPLICANT_PATH,
1311                                                 SUPPLICANT_INTERFACE,
1312                                                 service_property, NULL);
1313 }
1314
1315 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
1316 {
1317         const char *name = NULL, *old = NULL, *new = NULL;
1318
1319         if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
1320                 return;
1321
1322         dbus_message_iter_get_basic(iter, &name);
1323         if (name == NULL)
1324                 return;
1325
1326         if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
1327                 return;
1328
1329         dbus_message_iter_next(iter);
1330         dbus_message_iter_get_basic(iter, &old);
1331         dbus_message_iter_next(iter);
1332         dbus_message_iter_get_basic(iter, &new);
1333
1334         if (old == NULL || new == NULL)
1335                 return;
1336
1337         if (strlen(old) > 0 && strlen(new) == 0) {
1338                 system_available = FALSE;
1339                 g_hash_table_remove_all(bss_mapping);
1340                 g_hash_table_remove_all(interface_table);
1341                 callback_system_killed();
1342         }
1343
1344         if (strlen(new) > 0 && strlen(old) == 0) {
1345                 system_available = TRUE;
1346                 supplicant_bootstrap();
1347         }
1348 }
1349
1350 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
1351 {
1352         if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
1353                 return;
1354
1355         supplicant_dbus_property_foreach(iter, service_property, NULL);
1356 }
1357
1358 static void signal_interface_added(const char *path, DBusMessageIter *iter)
1359 {
1360         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1361                 interface_added(iter, NULL);
1362 }
1363
1364 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
1365 {
1366         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
1367                 interface_removed(iter, NULL);
1368 }
1369
1370 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
1371 {
1372         struct supplicant_interface *interface;
1373
1374         interface = g_hash_table_lookup(interface_table, path);
1375         if (interface == NULL)
1376                 return;
1377
1378         supplicant_dbus_property_foreach(iter, interface_property, interface);
1379 }
1380
1381 static void signal_scan_done(const char *path, DBusMessageIter *iter)
1382 {
1383         struct supplicant_interface *interface;
1384         dbus_bool_t success = FALSE;
1385
1386         interface = g_hash_table_lookup(interface_table, path);
1387         if (interface == NULL)
1388                 return;
1389
1390         dbus_message_iter_get_basic(iter, &success);
1391
1392         if (interface->scan_callback != NULL) {
1393                 int result = 0;
1394
1395                 if (success == FALSE)
1396                         result = -EIO;
1397
1398                 interface->scan_callback(result, interface->scan_data);
1399         }
1400
1401         interface->scan_callback = NULL;
1402         interface->scan_data = NULL;
1403 }
1404
1405 static void signal_bss_added(const char *path, DBusMessageIter *iter)
1406 {
1407         struct supplicant_interface *interface;
1408
1409         interface = g_hash_table_lookup(interface_table, path);
1410         if (interface == NULL)
1411                 return;
1412
1413         interface_bss_added(iter, interface);
1414 }
1415
1416 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
1417 {
1418         struct supplicant_interface *interface;
1419
1420         interface = g_hash_table_lookup(interface_table, path);
1421         if (interface == NULL)
1422                 return;
1423
1424         interface_bss_removed(iter, interface);
1425 }
1426
1427 static void signal_network_added(const char *path, DBusMessageIter *iter)
1428 {
1429         struct supplicant_interface *interface;
1430
1431         interface = g_hash_table_lookup(interface_table, path);
1432         if (interface == NULL)
1433                 return;
1434
1435         interface_network_added(iter, interface);
1436 }
1437
1438 static void signal_network_removed(const char *path, DBusMessageIter *iter)
1439 {
1440         struct supplicant_interface *interface;
1441
1442         interface = g_hash_table_lookup(interface_table, path);
1443         if (interface == NULL)
1444                 return;
1445
1446         interface_network_removed(iter, interface);
1447 }
1448
1449 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
1450 {
1451         struct supplicant_interface *interface;
1452         struct supplicant_network *network;
1453         struct supplicant_bss *bss;
1454
1455         interface = g_hash_table_lookup(bss_mapping, path);
1456         if (interface == NULL)
1457                 return;
1458
1459         network = g_hash_table_lookup(interface->bss_mapping, path);
1460         if (network == NULL)
1461                 return;
1462
1463         bss = g_hash_table_lookup(network->bss_table, path);
1464         if (bss == NULL)
1465                 return;
1466
1467         supplicant_dbus_property_foreach(iter, bss_property, bss);
1468 }
1469
1470 static struct {
1471         const char *interface;
1472         const char *member;
1473         void (*function) (const char *path, DBusMessageIter *iter);
1474 } signal_map[] = {
1475         { DBUS_INTERFACE_DBUS,  "NameOwnerChanged",  signal_name_owner_changed },
1476
1477         { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
1478         { SUPPLICANT_INTERFACE, "InterfaceAdded",    signal_interface_added    },
1479         { SUPPLICANT_INTERFACE, "InterfaceCreated",  signal_interface_added    },
1480         { SUPPLICANT_INTERFACE, "InterfaceRemoved",  signal_interface_removed  },
1481
1482         { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
1483         { SUPPLICANT_INTERFACE ".Interface", "ScanDone",          signal_scan_done         },
1484         { SUPPLICANT_INTERFACE ".Interface", "BSSAdded",          signal_bss_added         },
1485         { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved",        signal_bss_removed       },
1486         { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded",      signal_network_added     },
1487         { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved",    signal_network_removed   },
1488
1489         { SUPPLICANT_INTERFACE ".Interface.BSS", "PropertiesChanged", signal_bss_changed   },
1490
1491         { }
1492 };
1493
1494 static DBusHandlerResult supplicant_filter(DBusConnection *conn,
1495                                         DBusMessage *message, void *data)
1496 {
1497         DBusMessageIter iter;
1498         const char *path;
1499         int i;
1500
1501         path = dbus_message_get_path(message);
1502         if (path == NULL)
1503                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1504
1505         if (dbus_message_iter_init(message, &iter) == FALSE)
1506                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1507
1508         for (i = 0; signal_map[i].interface != NULL; i++) {
1509                 if (dbus_message_has_interface(message,
1510                                         signal_map[i].interface) == FALSE)
1511                         continue;
1512
1513                 if (dbus_message_has_member(message,
1514                                         signal_map[i].member) == FALSE)
1515                         continue;
1516
1517                 signal_map[i].function(path, &iter);
1518                 break;
1519         }
1520
1521         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1522 }
1523
1524 static const char *supplicant_rule0 = "type=signal,"
1525                                         "path=" DBUS_PATH_DBUS ","
1526                                         "sender=" DBUS_SERVICE_DBUS ","
1527                                         "interface=" DBUS_INTERFACE_DBUS ","
1528                                         "member=NameOwnerChanged,"
1529                                         "arg0=" SUPPLICANT_SERVICE;
1530 static const char *supplicant_rule1 = "type=signal,"
1531                         "interface=" SUPPLICANT_INTERFACE;
1532 static const char *supplicant_rule2 = "type=signal,"
1533                         "interface=" SUPPLICANT_INTERFACE ".Interface";
1534 static const char *supplicant_rule3 = "type=signal,"
1535                         "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
1536 static const char *supplicant_rule4 = "type=signal,"
1537                         "interface=" SUPPLICANT_INTERFACE ".Interface.BSS";
1538 static const char *supplicant_rule5 = "type=signal,"
1539                         "interface=" SUPPLICANT_INTERFACE ".Interface.Network";
1540 static const char *supplicant_rule6 = "type=signal,"
1541                         "interface=" SUPPLICANT_INTERFACE ".Interface.Blob";
1542
1543 int supplicant_register(const struct supplicant_callbacks *callbacks)
1544 {
1545         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
1546         if (connection == NULL)
1547                 return -EIO;
1548
1549         if (dbus_connection_add_filter(connection,
1550                                 supplicant_filter, NULL, NULL) == FALSE) {
1551                 dbus_connection_unref(connection);
1552                 connection = NULL;
1553                 return -EIO;
1554         }
1555
1556         callbacks_pointer = callbacks;
1557         eap_methods = 0;
1558
1559         interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1560                                                 NULL, remove_interface);
1561
1562         bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
1563                                                                 NULL, NULL);
1564
1565         supplicant_dbus_setup(connection);
1566
1567         dbus_bus_add_match(connection, supplicant_rule0, NULL);
1568         dbus_bus_add_match(connection, supplicant_rule1, NULL);
1569         dbus_bus_add_match(connection, supplicant_rule2, NULL);
1570         dbus_bus_add_match(connection, supplicant_rule3, NULL);
1571         dbus_bus_add_match(connection, supplicant_rule4, NULL);
1572         dbus_bus_add_match(connection, supplicant_rule5, NULL);
1573         dbus_bus_add_match(connection, supplicant_rule6, NULL);
1574         dbus_connection_flush(connection);
1575
1576         if (dbus_bus_name_has_owner(connection,
1577                                         SUPPLICANT_SERVICE, NULL) == TRUE) {
1578                 system_available = TRUE;
1579                 supplicant_bootstrap();
1580         }
1581
1582         return 0;
1583 }
1584
1585 void supplicant_unregister(const struct supplicant_callbacks *callbacks)
1586 {
1587         if (connection != NULL) {
1588                 dbus_bus_remove_match(connection, supplicant_rule6, NULL);
1589                 dbus_bus_remove_match(connection, supplicant_rule5, NULL);
1590                 dbus_bus_remove_match(connection, supplicant_rule4, NULL);
1591                 dbus_bus_remove_match(connection, supplicant_rule3, NULL);
1592                 dbus_bus_remove_match(connection, supplicant_rule2, NULL);
1593                 dbus_bus_remove_match(connection, supplicant_rule1, NULL);
1594                 dbus_bus_remove_match(connection, supplicant_rule0, NULL);
1595                 dbus_connection_flush(connection);
1596
1597                 dbus_connection_remove_filter(connection,
1598                                                 supplicant_filter, NULL);
1599         }
1600
1601         if (bss_mapping != NULL) {
1602                 g_hash_table_destroy(bss_mapping);
1603                 bss_mapping = NULL;
1604         }
1605
1606         if (interface_table != NULL) {
1607                 g_hash_table_destroy(interface_table);
1608                 interface_table = NULL;
1609         }
1610
1611         if (system_available == TRUE)
1612                 callback_system_killed();
1613
1614         if (connection != NULL) {
1615                 dbus_connection_unref(connection);
1616                 connection = NULL;
1617         }
1618
1619         callbacks_pointer = NULL;
1620         eap_methods = 0;
1621 }
1622
1623 static void debug_level_result(const char *error,
1624                                 DBusMessageIter *iter, void *user_data)
1625 {
1626         if (error != NULL)
1627                 DBG("debug level failure: %s", error);
1628 }
1629
1630 static void debug_level_params(DBusMessageIter *iter, void *user_data)
1631 {
1632         guint level = GPOINTER_TO_UINT(user_data);
1633         const char *str;
1634
1635         if (level > 4)
1636                 level = 4;
1637
1638         str = debug_strings[level];
1639
1640         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &str);
1641 }
1642
1643 void supplicant_set_debug_level(unsigned int level)
1644 {
1645         if (system_available == FALSE)
1646                 return;
1647
1648         supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
1649                                 "DebugLevel", DBUS_TYPE_STRING_AS_STRING,
1650                                 debug_level_params, debug_level_result,
1651                                                 GUINT_TO_POINTER(level));
1652 }
1653
1654 struct interface_create_data {
1655         const char *ifname;
1656         const char *driver;
1657         struct supplicant_interface *interface;
1658         supplicant_interface_create_callback callback;
1659         void *user_data;
1660 };
1661
1662 static void interface_create_property(const char *key, DBusMessageIter *iter,
1663                                                         void *user_data)
1664 {
1665         struct interface_create_data *data = user_data;
1666         struct supplicant_interface *interface = data->interface;
1667
1668         if (key == NULL) {
1669                 if (data->callback != NULL)
1670                         data->callback(0, data->interface, data->user_data);
1671
1672                 dbus_free(data);
1673         }
1674
1675         interface_property(key, iter, interface);
1676 }
1677
1678 static void interface_create_result(const char *error,
1679                                 DBusMessageIter *iter, void *user_data)
1680 {
1681         struct interface_create_data *data = user_data;
1682         const char *path = NULL;
1683         int err;
1684
1685         if (error != NULL) {
1686                 err = -EIO;
1687                 goto done;
1688         }
1689
1690         dbus_message_iter_get_basic(iter, &path);
1691         if (path == NULL) {
1692                 err = -EINVAL;
1693                 goto done;
1694         }
1695
1696         if (system_available == FALSE) {
1697                 err = -EFAULT;
1698                 goto done;
1699         }
1700
1701         data->interface = g_hash_table_lookup(interface_table, path);
1702         if (data->interface == NULL) {
1703                 data->interface = interface_alloc(path);
1704                 if (data->interface == NULL) {
1705                         err = -ENOMEM;
1706                         goto done;
1707                 }
1708         }
1709
1710         err = supplicant_dbus_property_get_all(path,
1711                                         SUPPLICANT_INTERFACE ".Interface",
1712                                         interface_create_property, data);
1713         if (err == 0)
1714                 return;
1715
1716 done:
1717         if (data->callback != NULL)
1718                 data->callback(err, NULL, data->user_data);
1719
1720         dbus_free(data);
1721 }
1722
1723 static void interface_create_params(DBusMessageIter *iter, void *user_data)
1724 {
1725         struct interface_create_data *data = user_data;
1726         DBusMessageIter dict;
1727
1728         supplicant_dbus_dict_open(iter, &dict);
1729
1730         supplicant_dbus_dict_append_basic(&dict, "Ifname",
1731                                         DBUS_TYPE_STRING, &data->ifname);
1732
1733         if (data->driver != NULL)
1734                 supplicant_dbus_dict_append_basic(&dict, "Driver",
1735                                         DBUS_TYPE_STRING, &data->driver);
1736
1737         supplicant_dbus_dict_close(iter, &dict);
1738 }
1739
1740 static void interface_get_result(const char *error,
1741                                 DBusMessageIter *iter, void *user_data)
1742 {
1743         struct interface_create_data *data = user_data;
1744         struct supplicant_interface *interface;
1745         const char *path = NULL;
1746         int err;
1747
1748         if (error != NULL) {
1749                 err = -EIO;
1750                 goto create;
1751         }
1752
1753         dbus_message_iter_get_basic(iter, &path);
1754         if (path == NULL) {
1755                 err = -EINVAL;
1756                 goto done;
1757         }
1758
1759         interface = g_hash_table_lookup(interface_table, path);
1760         if (interface == NULL) {
1761                 err = -ENOENT;
1762                 goto done;
1763         }
1764
1765         if (data->callback != NULL)
1766                 data->callback(0, interface, data->user_data);
1767
1768         dbus_free(data);
1769
1770         return;
1771
1772 create:
1773         if (system_available == FALSE) {
1774                 err = -EFAULT;
1775                 goto done;
1776         }
1777
1778         err = supplicant_dbus_method_call(SUPPLICANT_PATH,
1779                                                 SUPPLICANT_INTERFACE,
1780                                                 "CreateInterface",
1781                                                 interface_create_params,
1782                                                 interface_create_result, data);
1783         if (err == 0)
1784                 return;
1785
1786 done:
1787         if (data->callback != NULL)
1788                 data->callback(err, NULL, data->user_data);
1789
1790         dbus_free(data);
1791 }
1792
1793 static void interface_get_params(DBusMessageIter *iter, void *user_data)
1794 {
1795         struct interface_create_data *data = user_data;
1796
1797         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
1798 }
1799
1800 int supplicant_interface_create(const char *ifname, const char *driver,
1801                         supplicant_interface_create_callback callback,
1802                                                         void *user_data)
1803 {
1804         struct interface_create_data *data;
1805
1806         if (ifname == NULL)
1807                 return -EINVAL;
1808
1809         if (system_available == FALSE)
1810                 return -EFAULT;
1811
1812         data = dbus_malloc0(sizeof(*data));
1813         if (data == NULL)
1814                 return -ENOMEM;
1815
1816         data->ifname = ifname;
1817         data->driver = driver;
1818         data->callback = callback;
1819         data->user_data = user_data;
1820
1821         return supplicant_dbus_method_call(SUPPLICANT_PATH,
1822                                                 SUPPLICANT_INTERFACE,
1823                                                 "GetInterface",
1824                                                 interface_get_params,
1825                                                 interface_get_result, data);
1826 }
1827
1828 int supplicant_interface_remove(struct supplicant_interface *interface,
1829                         supplicant_interface_remove_callback callback,
1830                                                         void *user_data)
1831 {
1832         if (interface == NULL)
1833                 return -EINVAL;
1834
1835         if (system_available == FALSE)
1836                 return -EFAULT;
1837
1838         return 0;
1839 }
1840
1841 struct interface_scan_data {
1842         struct supplicant_interface *interface;
1843         supplicant_interface_scan_callback callback;
1844         void *user_data;
1845 };
1846
1847 static void interface_scan_result(const char *error,
1848                                 DBusMessageIter *iter, void *user_data)
1849 {
1850         struct interface_scan_data *data = user_data;
1851
1852         if (error != NULL) {
1853                 if (data->callback != NULL)
1854                         data->callback(-EIO, data->user_data);
1855         } else {
1856                 data->interface->scan_callback = data->callback;
1857                 data->interface->scan_data = data->user_data;
1858         }
1859
1860         dbus_free(data);
1861 }
1862
1863 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
1864 {
1865         DBusMessageIter dict;
1866         const char *type = "passive";
1867
1868         supplicant_dbus_dict_open(iter, &dict);
1869
1870         supplicant_dbus_dict_append_basic(&dict, "Type",
1871                                                 DBUS_TYPE_STRING, &type);
1872
1873         supplicant_dbus_dict_close(iter, &dict);
1874 }
1875
1876 int supplicant_interface_scan(struct supplicant_interface *interface,
1877                         supplicant_interface_scan_callback callback,
1878                                                         void *user_data)
1879 {
1880         struct interface_scan_data *data;
1881
1882         if (interface == NULL)
1883                 return -EINVAL;
1884
1885         if (system_available == FALSE)
1886                 return -EFAULT;
1887
1888         if (interface->scanning == TRUE)
1889                 return -EALREADY;
1890
1891         data = dbus_malloc0(sizeof(*data));
1892         if (data == NULL)
1893                 return -ENOMEM;
1894
1895         data->interface = interface;
1896         data->callback = callback;
1897         data->user_data = user_data;
1898
1899         return supplicant_dbus_method_call(interface->path,
1900                         SUPPLICANT_INTERFACE ".Interface", "Scan",
1901                         interface_scan_params, interface_scan_result, data);
1902 }
1903
1904 struct interface_disconnect_data {
1905         supplicant_interface_disconnect_callback callback;
1906         void *user_data;
1907 };
1908
1909 static void interface_disconnect_result(const char *error,
1910                                 DBusMessageIter *iter, void *user_data)
1911 {
1912         struct interface_disconnect_data *data = user_data;
1913         int result = 0;
1914
1915         if (error != NULL)
1916                 result = -EIO;
1917
1918         if (data->callback != NULL)
1919                 data->callback(result, data->user_data);
1920
1921         dbus_free(data);
1922 }
1923
1924 int supplicant_interface_disconnect(struct supplicant_interface *interface,
1925                         supplicant_interface_disconnect_callback callback,
1926                                                         void *user_data)
1927 {
1928         struct interface_disconnect_data *data;
1929
1930         if (interface == NULL)
1931                 return -EINVAL;
1932
1933         if (system_available == FALSE)
1934                 return -EFAULT;
1935
1936         data = dbus_malloc0(sizeof(*data));
1937         if (data == NULL)
1938                 return -ENOMEM;
1939
1940         data->callback = callback;
1941         data->user_data = user_data;
1942
1943         return supplicant_dbus_method_call(interface->path,
1944                         SUPPLICANT_INTERFACE ".Interface", "Disconnect",
1945                                 NULL, interface_disconnect_result, data);
1946 }