Merge "[SPIN]Set interface callback during scan request" into tizen
[platform/upstream/connman.git] / gsupplicant / supplicant.c
1 /*
2  *
3  *  WPA supplicant library with GLib integration
4  *
5  *  Copyright (C) 2012-2013  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <stdint.h>
31 #include <syslog.h>
32 #include <ctype.h>
33 #include <stdbool.h>
34 #include <netinet/if_ether.h>
35 #include <netinet/in.h>
36
37 #include <glib.h>
38 #include <gdbus.h>
39
40 #include "dbus.h"
41 #include "gsupplicant.h"
42
43 #define IEEE80211_CAP_ESS       0x0001
44 #define IEEE80211_CAP_IBSS      0x0002
45 #define IEEE80211_CAP_PRIVACY   0x0010
46
47 #define BSS_UNKNOWN_STRENGTH    -90
48
49 static DBusConnection *connection;
50
51 static const GSupplicantCallbacks *callbacks_pointer;
52
53 static dbus_bool_t system_available = FALSE;
54 static dbus_bool_t system_ready = FALSE;
55
56 static dbus_int32_t debug_level;
57 static dbus_bool_t debug_timestamp = FALSE;
58 static dbus_bool_t debug_showkeys = FALSE;
59
60 static const char *debug_strings[] = {
61         "msgdump", "debug", "info", "warning", "error", NULL
62 };
63
64 static unsigned int eap_methods;
65
66 struct strvalmap {
67         const char *str;
68         unsigned int val;
69 };
70
71 static struct strvalmap eap_method_map[] = {
72         { "MD5",        G_SUPPLICANT_EAP_METHOD_MD5     },
73         { "TLS",        G_SUPPLICANT_EAP_METHOD_TLS     },
74         { "MSCHAPV2",   G_SUPPLICANT_EAP_METHOD_MSCHAPV2        },
75         { "PEAP",       G_SUPPLICANT_EAP_METHOD_PEAP    },
76         { "TTLS",       G_SUPPLICANT_EAP_METHOD_TTLS    },
77         { "GTC",        G_SUPPLICANT_EAP_METHOD_GTC     },
78         { "OTP",        G_SUPPLICANT_EAP_METHOD_OTP     },
79         { "LEAP",       G_SUPPLICANT_EAP_METHOD_LEAP    },
80         { "WSC",        G_SUPPLICANT_EAP_METHOD_WSC     },
81         { }
82 };
83
84 static struct strvalmap keymgmt_map[] = {
85         { "none",               G_SUPPLICANT_KEYMGMT_NONE               },
86         { "ieee8021x",          G_SUPPLICANT_KEYMGMT_IEEE8021X  },
87         { "wpa-none",           G_SUPPLICANT_KEYMGMT_WPA_NONE   },
88         { "wpa-psk",            G_SUPPLICANT_KEYMGMT_WPA_PSK    },
89         { "wpa-psk-sha256",     G_SUPPLICANT_KEYMGMT_WPA_PSK_256        },
90         { "wpa-ft-psk",         G_SUPPLICANT_KEYMGMT_WPA_FT_PSK },
91         { "wpa-ft-eap",         G_SUPPLICANT_KEYMGMT_WPA_FT_EAP },
92         { "wpa-eap",            G_SUPPLICANT_KEYMGMT_WPA_EAP    },
93         { "wpa-eap-sha256",     G_SUPPLICANT_KEYMGMT_WPA_EAP_256        },
94         { "wps",                G_SUPPLICANT_KEYMGMT_WPS                },
95         { }
96 };
97
98 static struct strvalmap authalg_capa_map[] = {
99         { "open",       G_SUPPLICANT_CAPABILITY_AUTHALG_OPEN    },
100         { "shared",     G_SUPPLICANT_CAPABILITY_AUTHALG_SHARED  },
101         { "leap",       G_SUPPLICANT_CAPABILITY_AUTHALG_LEAP    },
102         { }
103 };
104
105 static struct strvalmap proto_capa_map[] = {
106         { "wpa",        G_SUPPLICANT_CAPABILITY_PROTO_WPA               },
107         { "rsn",        G_SUPPLICANT_CAPABILITY_PROTO_RSN               },
108         { }
109 };
110
111 static struct strvalmap group_map[] = {
112         { "wep40",      G_SUPPLICANT_GROUP_WEP40        },
113         { "wep104",     G_SUPPLICANT_GROUP_WEP104       },
114         { "tkip",       G_SUPPLICANT_GROUP_TKIP },
115         { "ccmp",       G_SUPPLICANT_GROUP_CCMP },
116         { }
117 };
118
119 static struct strvalmap pairwise_map[] = {
120         { "none",       G_SUPPLICANT_PAIRWISE_NONE      },
121         { "tkip",       G_SUPPLICANT_PAIRWISE_TKIP      },
122         { "ccmp",       G_SUPPLICANT_PAIRWISE_CCMP      },
123         { }
124 };
125
126 static struct strvalmap scan_capa_map[] = {
127         { "active",     G_SUPPLICANT_CAPABILITY_SCAN_ACTIVE     },
128         { "passive",    G_SUPPLICANT_CAPABILITY_SCAN_PASSIVE    },
129         { "ssid",       G_SUPPLICANT_CAPABILITY_SCAN_SSID               },
130         { }
131 };
132
133 static struct strvalmap mode_capa_map[] = {
134         { "infrastructure",     G_SUPPLICANT_CAPABILITY_MODE_INFRA      },
135         { "ad-hoc",             G_SUPPLICANT_CAPABILITY_MODE_IBSS       },
136         { "ap",                 G_SUPPLICANT_CAPABILITY_MODE_AP         },
137         { "p2p",                G_SUPPLICANT_CAPABILITY_MODE_P2P        },
138         { }
139 };
140
141 static GHashTable *interface_table;
142 static GHashTable *bss_mapping;
143 static GHashTable *peer_mapping;
144 static GHashTable *group_mapping;
145 static GHashTable *pending_peer_connection;
146
147 struct _GSupplicantWpsCredentials {
148         unsigned char ssid[32];
149         unsigned int ssid_len;
150         char *key;
151 };
152
153 struct _GSupplicantInterface {
154         char *path;
155         char *network_path;
156         unsigned int keymgmt_capa;
157         unsigned int authalg_capa;
158         unsigned int proto_capa;
159         unsigned int group_capa;
160         unsigned int pairwise_capa;
161         unsigned int scan_capa;
162         unsigned int mode_capa;
163         unsigned int max_scan_ssids;
164         bool p2p_support;
165         bool p2p_finding;
166         dbus_bool_t ready;
167         GSupplicantState state;
168         dbus_bool_t scanning;
169         GSupplicantInterfaceCallback scan_callback;
170         void *scan_data;
171         int apscan;
172         char *ifname;
173         char *driver;
174         char *bridge;
175         struct _GSupplicantWpsCredentials wps_cred;
176         GSupplicantWpsState wps_state;
177         GHashTable *network_table;
178         GHashTable *peer_table;
179         GHashTable *group_table;
180         GHashTable *net_mapping;
181         GHashTable *bss_mapping;
182         void *data;
183         const char *pending_peer_path;
184 };
185
186 struct g_supplicant_bss {
187         GSupplicantInterface *interface;
188         char *path;
189         unsigned char bssid[6];
190         unsigned char ssid[32];
191         unsigned int ssid_len;
192         dbus_uint16_t frequency;
193         dbus_uint32_t maxrate;
194         dbus_int16_t signal;
195         GSupplicantMode mode;
196         GSupplicantSecurity security;
197         dbus_bool_t rsn_selected;
198         unsigned int wpa_keymgmt;
199         unsigned int wpa_pairwise;
200         unsigned int wpa_group;
201         unsigned int rsn_keymgmt;
202         unsigned int rsn_pairwise;
203         unsigned int rsn_group;
204         unsigned int keymgmt;
205         dbus_bool_t privacy;
206         dbus_bool_t psk;
207         dbus_bool_t ieee8021x;
208         unsigned int wps_capabilities;
209 };
210
211 struct _GSupplicantNetwork {
212         GSupplicantInterface *interface;
213         char *path;
214         char *group;
215         char *name;
216         unsigned char ssid[32];
217         unsigned int ssid_len;
218         dbus_int16_t signal;
219         dbus_uint16_t frequency;
220         struct g_supplicant_bss *best_bss;
221         GSupplicantMode mode;
222         GSupplicantSecurity security;
223         dbus_bool_t wps;
224         unsigned int wps_capabilities;
225         GHashTable *bss_table;
226         GHashTable *config_table;
227 #if defined TIZEN_EXT
228         unsigned int isHS20AP;
229         char *eap;
230         char *identity;
231         char *phase2;
232 #endif
233 };
234
235 struct _GSupplicantPeer {
236         GSupplicantInterface *interface;
237         char *path;
238         unsigned char device_address[ETH_ALEN];
239         unsigned char iface_address[ETH_ALEN];
240         char *name;
241         unsigned char *widi_ies;
242         int widi_ies_length;
243         char *identifier;
244         unsigned int wps_capabilities;
245         GSList *groups;
246         const GSupplicantInterface *current_group_iface;
247         bool connection_requested;
248 };
249
250 struct _GSupplicantGroup {
251         GSupplicantInterface *interface;
252         GSupplicantInterface *orig_interface;
253         char *path;
254         int role;
255         GSList *members;
256 };
257
258 static inline void debug(const char *format, ...)
259 {
260         char str[256];
261         va_list ap;
262
263         if (!callbacks_pointer->debug)
264                 return;
265
266         va_start(ap, format);
267
268         if (vsnprintf(str, sizeof(str), format, ap) > 0)
269                 callbacks_pointer->debug(str);
270
271         va_end(ap);
272 }
273
274 #define SUPPLICANT_DBG(fmt, arg...) \
275         debug("%s:%s() " fmt, __FILE__, __FUNCTION__ , ## arg);
276
277 static GSupplicantMode string2mode(const char *mode)
278 {
279         if (!mode)
280                 return G_SUPPLICANT_MODE_UNKNOWN;
281
282         if (g_str_equal(mode, "infrastructure"))
283                 return G_SUPPLICANT_MODE_INFRA;
284         else if (g_str_equal(mode, "ad-hoc"))
285                 return G_SUPPLICANT_MODE_IBSS;
286
287         return G_SUPPLICANT_MODE_UNKNOWN;
288 }
289
290 static const char *mode2string(GSupplicantMode mode)
291 {
292         switch (mode) {
293         case G_SUPPLICANT_MODE_UNKNOWN:
294                 break;
295         case G_SUPPLICANT_MODE_INFRA:
296                 return "managed";
297         case G_SUPPLICANT_MODE_IBSS:
298                 return "adhoc";
299         case G_SUPPLICANT_MODE_MASTER:
300                 return "ap";
301         }
302
303         return NULL;
304 }
305
306 static const char *security2string(GSupplicantSecurity security)
307 {
308         switch (security) {
309         case G_SUPPLICANT_SECURITY_UNKNOWN:
310                 break;
311         case G_SUPPLICANT_SECURITY_NONE:
312                 return "none";
313         case G_SUPPLICANT_SECURITY_WEP:
314                 return "wep";
315         case G_SUPPLICANT_SECURITY_PSK:
316                 return "psk";
317         case G_SUPPLICANT_SECURITY_IEEE8021X:
318                 return "ieee8021x";
319         }
320
321         return NULL;
322 }
323
324 static GSupplicantState string2state(const char *state)
325 {
326         if (!state)
327                 return G_SUPPLICANT_STATE_UNKNOWN;
328
329         if (g_str_equal(state, "unknown"))
330                 return G_SUPPLICANT_STATE_UNKNOWN;
331         else if (g_str_equal(state, "interface_disabled"))
332                 return G_SUPPLICANT_STATE_DISABLED;
333         else if (g_str_equal(state, "disconnected"))
334                 return G_SUPPLICANT_STATE_DISCONNECTED;
335         else if (g_str_equal(state, "inactive"))
336                 return G_SUPPLICANT_STATE_INACTIVE;
337         else if (g_str_equal(state, "scanning"))
338                 return G_SUPPLICANT_STATE_SCANNING;
339         else if (g_str_equal(state, "authenticating"))
340                 return G_SUPPLICANT_STATE_AUTHENTICATING;
341         else if (g_str_equal(state, "associating"))
342                 return G_SUPPLICANT_STATE_ASSOCIATING;
343         else if (g_str_equal(state, "associated"))
344                 return G_SUPPLICANT_STATE_ASSOCIATED;
345         else if (g_str_equal(state, "group_handshake"))
346                 return G_SUPPLICANT_STATE_GROUP_HANDSHAKE;
347         else if (g_str_equal(state, "4way_handshake"))
348                 return G_SUPPLICANT_STATE_4WAY_HANDSHAKE;
349         else if (g_str_equal(state, "completed"))
350                 return G_SUPPLICANT_STATE_COMPLETED;
351
352         return G_SUPPLICANT_STATE_UNKNOWN;
353 }
354
355 static void callback_system_ready(void)
356 {
357         if (system_ready)
358                 return;
359
360         system_ready = TRUE;
361
362         if (!callbacks_pointer)
363                 return;
364
365         if (!callbacks_pointer->system_ready)
366                 return;
367
368         callbacks_pointer->system_ready();
369 }
370
371 static void callback_system_killed(void)
372 {
373         system_ready = FALSE;
374
375         if (!callbacks_pointer)
376                 return;
377
378         if (!callbacks_pointer->system_killed)
379                 return;
380
381         callbacks_pointer->system_killed();
382 }
383
384 static void callback_interface_added(GSupplicantInterface *interface)
385 {
386         SUPPLICANT_DBG("");
387
388         if (!callbacks_pointer)
389                 return;
390
391         if (!callbacks_pointer->interface_added)
392                 return;
393
394         callbacks_pointer->interface_added(interface);
395 }
396
397 static void callback_interface_state(GSupplicantInterface *interface)
398 {
399         if (!callbacks_pointer)
400                 return;
401
402         if (!callbacks_pointer->interface_state)
403                 return;
404
405         callbacks_pointer->interface_state(interface);
406 }
407
408 static void callback_interface_removed(GSupplicantInterface *interface)
409 {
410         if (!callbacks_pointer)
411                 return;
412
413         if (!callbacks_pointer->interface_removed)
414                 return;
415
416         callbacks_pointer->interface_removed(interface);
417 }
418
419 #if !defined TIZEN_EXT
420 static void callback_p2p_support(GSupplicantInterface *interface)
421 {
422         SUPPLICANT_DBG("");
423
424         if (!interface->p2p_support)
425                 return;
426
427         if (callbacks_pointer && callbacks_pointer->p2p_support)
428                 callbacks_pointer->p2p_support(interface);
429 }
430 #endif
431
432 static void callback_scan_started(GSupplicantInterface *interface)
433 {
434         if (!callbacks_pointer)
435                 return;
436
437         if (!callbacks_pointer->scan_started)
438                 return;
439
440         callbacks_pointer->scan_started(interface);
441 }
442
443 static void callback_scan_finished(GSupplicantInterface *interface)
444 {
445         if (!callbacks_pointer)
446                 return;
447
448         if (!callbacks_pointer->scan_finished)
449                 return;
450
451         callbacks_pointer->scan_finished(interface);
452 }
453
454 static void callback_network_added(GSupplicantNetwork *network)
455 {
456         if (!callbacks_pointer)
457                 return;
458
459         if (!callbacks_pointer->network_added)
460                 return;
461
462         callbacks_pointer->network_added(network);
463 }
464
465 static void callback_network_removed(GSupplicantNetwork *network)
466 {
467         if (!callbacks_pointer)
468                 return;
469
470         if (!callbacks_pointer->network_removed)
471                 return;
472
473         callbacks_pointer->network_removed(network);
474 }
475
476 #if defined TIZEN_EXT
477 static void callback_network_merged(GSupplicantNetwork *network)
478 {
479         if (!callbacks_pointer)
480                 return;
481
482         if (!callbacks_pointer->network_merged)
483                 return;
484
485         callbacks_pointer->network_merged(network);
486 }
487 #endif
488
489 static void callback_network_changed(GSupplicantNetwork *network,
490                                         const char *property)
491 {
492         if (!callbacks_pointer)
493                 return;
494
495         if (!callbacks_pointer->network_changed)
496                 return;
497
498         callbacks_pointer->network_changed(network, property);
499 }
500
501 static void callback_peer_found(GSupplicantPeer *peer)
502 {
503         if (!callbacks_pointer)
504                 return;
505
506         if (!callbacks_pointer->peer_found)
507                 return;
508
509         callbacks_pointer->peer_found(peer);
510 }
511
512 static void callback_peer_lost(GSupplicantPeer *peer)
513 {
514         if (!callbacks_pointer)
515                 return;
516
517         if (!callbacks_pointer->peer_lost)
518                 return;
519
520         callbacks_pointer->peer_lost(peer);
521 }
522
523 static void callback_peer_changed(GSupplicantPeer *peer,
524                                                 GSupplicantPeerState state)
525 {
526         if (!callbacks_pointer)
527                 return;
528
529         if (!callbacks_pointer->peer_changed)
530                 return;
531
532         callbacks_pointer->peer_changed(peer, state);
533 }
534
535 static void callback_peer_request(GSupplicantPeer *peer)
536 {
537         if (!callbacks_pointer)
538                 return;
539
540         if (!callbacks_pointer->peer_request)
541                 return;
542
543         peer->connection_requested = true;
544
545         callbacks_pointer->peer_request(peer);
546 }
547
548 static void remove_group(gpointer data)
549 {
550         GSupplicantGroup *group = data;
551
552         if (group->members)
553                 g_slist_free_full(group->members, g_free);
554
555         g_free(group->path);
556         g_free(group);
557 }
558
559 static void remove_interface(gpointer data)
560 {
561         GSupplicantInterface *interface = data;
562
563         g_hash_table_destroy(interface->bss_mapping);
564         g_hash_table_destroy(interface->net_mapping);
565         g_hash_table_destroy(interface->network_table);
566         g_hash_table_destroy(interface->peer_table);
567         g_hash_table_destroy(interface->group_table);
568
569         if (interface->scan_callback) {
570                 SUPPLICANT_DBG("call interface %p callback %p scanning %d",
571                                 interface, interface->scan_callback,
572                                 interface->scanning);
573
574                 interface->scan_callback(-EIO, interface, interface->scan_data);
575                 interface->scan_callback = NULL;
576                 interface->scan_data = NULL;
577
578                 if (interface->scanning) {
579                         interface->scanning = FALSE;
580                         callback_scan_finished(interface);
581                 }
582         }
583
584         callback_interface_removed(interface);
585
586         g_free(interface->wps_cred.key);
587         g_free(interface->path);
588         g_free(interface->network_path);
589 #if defined TIZEN_EXT
590         interface->network_path = NULL;
591 #endif
592         g_free(interface->ifname);
593         g_free(interface->driver);
594         g_free(interface->bridge);
595         g_free(interface);
596 }
597
598 static void remove_network(gpointer data)
599 {
600         GSupplicantNetwork *network = data;
601
602         g_hash_table_destroy(network->bss_table);
603
604         callback_network_removed(network);
605
606         g_hash_table_destroy(network->config_table);
607
608         g_free(network->path);
609         g_free(network->group);
610         g_free(network->name);
611 #if defined TIZEN_EXT
612         g_free(network->eap);
613         g_free(network->identity);
614         g_free(network->phase2);
615 #endif
616         g_free(network);
617 }
618
619 static void remove_bss(gpointer data)
620 {
621         struct g_supplicant_bss *bss = data;
622
623         g_free(bss->path);
624         g_free(bss);
625 }
626
627 static void remove_peer(gpointer data)
628 {
629         GSupplicantPeer *peer = data;
630
631         callback_peer_lost(peer);
632
633         if (peer->groups)
634                 g_slist_free_full(peer->groups, g_free);
635
636         if (peer_mapping)
637                 g_hash_table_remove(peer_mapping, peer->path);
638
639         if (pending_peer_connection)
640                 g_hash_table_remove(pending_peer_connection, peer->path);
641
642         g_free(peer->path);
643         g_free(peer->name);
644         g_free(peer->identifier);
645
646         g_free(peer);
647 }
648
649 static void debug_strvalmap(const char *label, struct strvalmap *map,
650                                                         unsigned int val)
651 {
652         int i;
653
654         for (i = 0; map[i].str; i++) {
655                 if (val & map[i].val)
656                         SUPPLICANT_DBG("%s: %s", label, map[i].str);
657         }
658 }
659
660 static void interface_capability_keymgmt(DBusMessageIter *iter, void *user_data)
661 {
662         GSupplicantInterface *interface = user_data;
663         const char *str = NULL;
664         int i;
665
666         dbus_message_iter_get_basic(iter, &str);
667         if (!str)
668                 return;
669
670         for (i = 0; keymgmt_map[i].str; i++)
671                 if (strcmp(str, keymgmt_map[i].str) == 0) {
672                         interface->keymgmt_capa |= keymgmt_map[i].val;
673                         break;
674                 }
675 }
676
677 static void interface_capability_authalg(DBusMessageIter *iter, void *user_data)
678 {
679         GSupplicantInterface *interface = user_data;
680         const char *str = NULL;
681         int i;
682
683         dbus_message_iter_get_basic(iter, &str);
684         if (!str)
685                 return;
686
687         for (i = 0; authalg_capa_map[i].str; i++)
688                 if (strcmp(str, authalg_capa_map[i].str) == 0) {
689                         interface->authalg_capa |= authalg_capa_map[i].val;
690                         break;
691                 }
692 }
693
694 static void interface_capability_proto(DBusMessageIter *iter, void *user_data)
695 {
696         GSupplicantInterface *interface = user_data;
697         const char *str = NULL;
698         int i;
699
700         dbus_message_iter_get_basic(iter, &str);
701         if (!str)
702                 return;
703
704         for (i = 0; proto_capa_map[i].str; i++)
705                 if (strcmp(str, proto_capa_map[i].str) == 0) {
706                         interface->proto_capa |= proto_capa_map[i].val;
707                         break;
708                 }
709 }
710
711 static void interface_capability_pairwise(DBusMessageIter *iter,
712                                                         void *user_data)
713 {
714         GSupplicantInterface *interface = user_data;
715         const char *str = NULL;
716         int i;
717
718         dbus_message_iter_get_basic(iter, &str);
719         if (!str)
720                 return;
721
722         for (i = 0; pairwise_map[i].str; i++)
723                 if (strcmp(str, pairwise_map[i].str) == 0) {
724                         interface->pairwise_capa |= pairwise_map[i].val;
725                         break;
726                 }
727 }
728
729 static void interface_capability_group(DBusMessageIter *iter, void *user_data)
730 {
731         GSupplicantInterface *interface = user_data;
732         const char *str = NULL;
733         int i;
734
735         dbus_message_iter_get_basic(iter, &str);
736         if (!str)
737                 return;
738
739         for (i = 0; group_map[i].str; i++)
740                 if (strcmp(str, group_map[i].str) == 0) {
741                         interface->group_capa |= group_map[i].val;
742                         break;
743                 }
744 }
745
746 static void interface_capability_scan(DBusMessageIter *iter, void *user_data)
747 {
748         GSupplicantInterface *interface = user_data;
749         const char *str = NULL;
750         int i;
751
752         dbus_message_iter_get_basic(iter, &str);
753         if (!str)
754                 return;
755
756         for (i = 0; scan_capa_map[i].str; i++)
757                 if (strcmp(str, scan_capa_map[i].str) == 0) {
758                         interface->scan_capa |= scan_capa_map[i].val;
759                         break;
760                 }
761 }
762
763 static void interface_capability_mode(DBusMessageIter *iter, void *user_data)
764 {
765         GSupplicantInterface *interface = user_data;
766         const char *str = NULL;
767         int i;
768
769         dbus_message_iter_get_basic(iter, &str);
770         if (!str)
771                 return;
772
773         for (i = 0; mode_capa_map[i].str; i++)
774                 if (strcmp(str, mode_capa_map[i].str) == 0) {
775                         interface->mode_capa |= mode_capa_map[i].val;
776                         break;
777                 }
778 }
779
780 static void interface_capability(const char *key, DBusMessageIter *iter,
781                                                         void *user_data)
782 {
783         GSupplicantInterface *interface = user_data;
784
785         if (!key)
786                 return;
787
788         if (g_strcmp0(key, "KeyMgmt") == 0)
789                 supplicant_dbus_array_foreach(iter,
790                                 interface_capability_keymgmt, interface);
791         else if (g_strcmp0(key, "AuthAlg") == 0)
792                 supplicant_dbus_array_foreach(iter,
793                                 interface_capability_authalg, interface);
794         else if (g_strcmp0(key, "Protocol") == 0)
795                 supplicant_dbus_array_foreach(iter,
796                                 interface_capability_proto, interface);
797         else if (g_strcmp0(key, "Pairwise") == 0)
798                 supplicant_dbus_array_foreach(iter,
799                                 interface_capability_pairwise, interface);
800         else if (g_strcmp0(key, "Group") == 0)
801                 supplicant_dbus_array_foreach(iter,
802                                 interface_capability_group, interface);
803         else if (g_strcmp0(key, "Scan") == 0)
804                 supplicant_dbus_array_foreach(iter,
805                                 interface_capability_scan, interface);
806         else if (g_strcmp0(key, "Modes") == 0)
807                 supplicant_dbus_array_foreach(iter,
808                                 interface_capability_mode, interface);
809         else if (g_strcmp0(key, "MaxScanSSID") == 0) {
810                 dbus_int32_t max_scan_ssid;
811
812                 dbus_message_iter_get_basic(iter, &max_scan_ssid);
813                 if (max_scan_ssid < 2)
814                         max_scan_ssid = 0;
815                 interface->max_scan_ssids = max_scan_ssid;
816
817         } else
818                 SUPPLICANT_DBG("key %s type %c",
819                                 key, dbus_message_iter_get_arg_type(iter));
820 }
821
822 static void set_apscan(DBusMessageIter *iter, void *user_data)
823 {
824         unsigned int ap_scan = *(unsigned int *)user_data;
825
826         dbus_message_iter_append_basic(iter, DBUS_TYPE_UINT32, &ap_scan);
827 }
828
829 int g_supplicant_interface_set_apscan(GSupplicantInterface *interface,
830                                                         unsigned int ap_scan)
831 {
832         return supplicant_dbus_property_set(interface->path,
833                         SUPPLICANT_INTERFACE ".Interface",
834                                 "ApScan", DBUS_TYPE_UINT32_AS_STRING,
835                                         set_apscan, NULL, &ap_scan, NULL);
836 }
837
838 void g_supplicant_interface_set_data(GSupplicantInterface *interface,
839                                                                 void *data)
840 {
841         if (!interface)
842                 return;
843
844         interface->data = data;
845
846         if (!data)
847                 interface->scan_callback = NULL;
848 }
849
850 void *g_supplicant_interface_get_data(GSupplicantInterface *interface)
851 {
852         if (!interface)
853                 return NULL;
854
855         return interface->data;
856 }
857
858 const char *g_supplicant_interface_get_ifname(GSupplicantInterface *interface)
859 {
860         if (!interface)
861                 return NULL;
862
863         return interface->ifname;
864 }
865
866 const char *g_supplicant_interface_get_driver(GSupplicantInterface *interface)
867 {
868         if (!interface)
869                 return NULL;
870
871         return interface->driver;
872 }
873
874 GSupplicantState g_supplicant_interface_get_state(
875                                         GSupplicantInterface *interface)
876 {
877         if (!interface)
878                 return G_SUPPLICANT_STATE_UNKNOWN;
879
880         return interface->state;
881 }
882
883 const char *g_supplicant_interface_get_wps_key(GSupplicantInterface *interface)
884 {
885         if (!interface)
886                 return NULL;
887
888         return (const char *)interface->wps_cred.key;
889 }
890
891 const void *g_supplicant_interface_get_wps_ssid(GSupplicantInterface *interface,
892                                                         unsigned int *ssid_len)
893 {
894         if (!ssid_len)
895                 return NULL;
896
897         if (!interface || interface->wps_cred.ssid_len == 0) {
898                 *ssid_len = 0;
899                 return NULL;
900         }
901
902         *ssid_len = interface->wps_cred.ssid_len;
903         return interface->wps_cred.ssid;
904 }
905
906 GSupplicantWpsState g_supplicant_interface_get_wps_state(
907                                         GSupplicantInterface *interface)
908 {
909         if (!interface)
910                 return G_SUPPLICANT_WPS_STATE_UNKNOWN;
911
912         return interface->wps_state;
913 }
914
915 unsigned int g_supplicant_interface_get_mode(GSupplicantInterface *interface)
916 {
917         if (!interface)
918                 return 0;
919
920         return interface->mode_capa;
921 }
922
923 unsigned int g_supplicant_interface_get_max_scan_ssids(
924                                 GSupplicantInterface *interface)
925 {
926         if (!interface)
927                 return 0;
928
929         if (interface->max_scan_ssids == 0)
930                 return WPAS_MAX_SCAN_SSIDS;
931
932         return interface->max_scan_ssids;
933 }
934
935 static void set_network_enabled(DBusMessageIter *iter, void *user_data)
936 {
937         dbus_bool_t enable = *(dbus_bool_t *)user_data;
938
939         dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &enable);
940 }
941
942 int g_supplicant_interface_enable_selected_network(GSupplicantInterface *interface,
943                                                         dbus_bool_t enable)
944 {
945         if (!interface)
946                 return -1;
947
948         if (!interface->network_path)
949                 return -1;
950
951         SUPPLICANT_DBG(" ");
952         return supplicant_dbus_property_set(interface->network_path,
953                                 SUPPLICANT_INTERFACE ".Network",
954                                 "Enabled", DBUS_TYPE_BOOLEAN_AS_STRING,
955                                 set_network_enabled, NULL, &enable, NULL);
956 }
957
958 dbus_bool_t g_supplicant_interface_get_ready(GSupplicantInterface *interface)
959 {
960         if (!interface)
961                 return FALSE;
962
963         return interface->ready;
964 }
965
966 GSupplicantInterface *g_supplicant_network_get_interface(
967                                         GSupplicantNetwork *network)
968 {
969         if (!network)
970                 return NULL;
971
972         return network->interface;
973 }
974
975 const char *g_supplicant_network_get_name(GSupplicantNetwork *network)
976 {
977         if (!network || !network->name)
978                 return "";
979
980         return network->name;
981 }
982
983 const char *g_supplicant_network_get_identifier(GSupplicantNetwork *network)
984 {
985         if (!network || !network->group)
986                 return "";
987
988         return network->group;
989 }
990
991 const char *g_supplicant_network_get_path(GSupplicantNetwork *network)
992 {
993         if (!network || !network->path)
994                 return NULL;
995
996         return network->path;
997 }
998
999 const char *g_supplicant_network_get_mode(GSupplicantNetwork *network)
1000 {
1001         if (!network)
1002                 return G_SUPPLICANT_MODE_UNKNOWN;
1003
1004         return mode2string(network->mode);
1005 }
1006
1007 const char *g_supplicant_network_get_security(GSupplicantNetwork *network)
1008 {
1009         if (!network)
1010                 return G_SUPPLICANT_SECURITY_UNKNOWN;
1011
1012         return security2string(network->security);
1013 }
1014
1015 const void *g_supplicant_network_get_ssid(GSupplicantNetwork *network,
1016                                                 unsigned int *ssid_len)
1017 {
1018         if (!network) {
1019                 *ssid_len = 0;
1020                 return NULL;
1021         }
1022
1023         *ssid_len = network->ssid_len;
1024         return network->ssid;
1025 }
1026
1027 dbus_int16_t g_supplicant_network_get_signal(GSupplicantNetwork *network)
1028 {
1029         if (!network)
1030                 return 0;
1031
1032         return network->signal;
1033 }
1034
1035 dbus_uint16_t g_supplicant_network_get_frequency(GSupplicantNetwork *network)
1036 {
1037         if (!network)
1038                 return 0;
1039
1040         return network->frequency;
1041 }
1042
1043 dbus_bool_t g_supplicant_network_get_wps(GSupplicantNetwork *network)
1044 {
1045         if (!network)
1046                 return FALSE;
1047
1048         return network->wps;
1049 }
1050
1051 dbus_bool_t g_supplicant_network_is_wps_active(GSupplicantNetwork *network)
1052 {
1053         if (!network)
1054                 return FALSE;
1055
1056         if (network->wps_capabilities & G_SUPPLICANT_WPS_CONFIGURED)
1057                 return TRUE;
1058
1059         return FALSE;
1060 }
1061
1062 dbus_bool_t g_supplicant_network_is_wps_pbc(GSupplicantNetwork *network)
1063 {
1064         if (!network)
1065                 return FALSE;
1066
1067         if (network->wps_capabilities & G_SUPPLICANT_WPS_PBC)
1068                 return TRUE;
1069
1070         return FALSE;
1071 }
1072
1073 dbus_bool_t g_supplicant_network_is_wps_advertizing(GSupplicantNetwork *network)
1074 {
1075         if (!network)
1076                 return FALSE;
1077
1078         if (network->wps_capabilities & G_SUPPLICANT_WPS_REGISTRAR)
1079                 return TRUE;
1080
1081         return FALSE;
1082 }
1083
1084 GSupplicantInterface *g_supplicant_peer_get_interface(GSupplicantPeer *peer)
1085 {
1086         if (!peer)
1087                 return NULL;
1088
1089         return peer->interface;
1090 }
1091
1092 const char *g_supplicant_peer_get_path(GSupplicantPeer *peer)
1093 {
1094         if (!peer)
1095                 return NULL;
1096
1097         return peer->path;
1098 }
1099
1100 const char *g_supplicant_peer_get_identifier(GSupplicantPeer *peer)
1101 {
1102         if (!peer)
1103                 return NULL;
1104
1105         return peer->identifier;
1106 }
1107
1108 const void *g_supplicant_peer_get_device_address(GSupplicantPeer *peer)
1109 {
1110         if (!peer)
1111                 return NULL;
1112
1113         return peer->device_address;
1114 }
1115
1116 const void *g_supplicant_peer_get_iface_address(GSupplicantPeer *peer)
1117 {
1118         if (!peer)
1119                 return NULL;
1120
1121         return peer->iface_address;
1122 }
1123
1124 const char *g_supplicant_peer_get_name(GSupplicantPeer *peer)
1125 {
1126         if (!peer)
1127                 return NULL;
1128
1129         return peer->name;
1130 }
1131
1132 #if defined TIZEN_EXT
1133 unsigned int g_supplicant_network_is_hs20AP(GSupplicantNetwork *network)
1134 {
1135         if (!network)
1136                 return 0;
1137
1138         return network->isHS20AP;
1139 }
1140
1141 const char *g_supplicant_network_get_eap(GSupplicantNetwork *network)
1142 {
1143         if (!network || !network->eap)
1144                 return NULL;
1145
1146         return network->eap;
1147 }
1148
1149 const char *g_supplicant_network_get_identity(GSupplicantNetwork *network)
1150 {
1151         if (!network || !network->identity)
1152                 return NULL;
1153
1154         return network->identity;
1155 }
1156
1157 const char *g_supplicant_network_get_phase2(GSupplicantNetwork *network)
1158 {
1159         if (!network || !network->phase2)
1160                 return NULL;
1161
1162         return network->phase2;
1163 }
1164 #endif
1165
1166 const unsigned char *g_supplicant_peer_get_widi_ies(GSupplicantPeer *peer,
1167                                                                 int *length)
1168 {
1169         if (!peer || !length)
1170                 return NULL;
1171
1172         *length = peer->widi_ies_length;
1173         return peer->widi_ies;
1174 }
1175
1176 bool g_supplicant_peer_is_wps_pbc(GSupplicantPeer *peer)
1177 {
1178         if (!peer)
1179                 return false;
1180
1181         if (peer->wps_capabilities & G_SUPPLICANT_WPS_PBC)
1182                 return true;
1183
1184         return false;
1185 }
1186
1187 bool g_supplicant_peer_is_wps_pin(GSupplicantPeer *peer)
1188 {
1189         if (!peer)
1190                 return false;
1191
1192         if (peer->wps_capabilities & G_SUPPLICANT_WPS_PIN)
1193                 return true;
1194
1195         return false;
1196 }
1197
1198 bool g_supplicant_peer_is_in_a_group(GSupplicantPeer *peer)
1199 {
1200         if (!peer || !peer->groups)
1201                 return false;
1202
1203         return true;
1204 }
1205
1206 GSupplicantInterface *g_supplicant_peer_get_group_interface(GSupplicantPeer *peer)
1207 {
1208         if (!peer)
1209                 return NULL;
1210
1211         return (GSupplicantInterface *) peer->current_group_iface;
1212 }
1213
1214 bool g_supplicant_peer_is_client(GSupplicantPeer *peer)
1215 {
1216         GSupplicantGroup *group;
1217         GSList *list;
1218
1219         if (!peer)
1220                 return false;
1221
1222         for (list = peer->groups; list; list = list->next) {
1223                 const char *path = list->data;
1224
1225                 group = g_hash_table_lookup(group_mapping, path);
1226                 if (!group)
1227                         continue;
1228
1229                 if (group->role != G_SUPPLICANT_GROUP_ROLE_CLIENT ||
1230                                 group->orig_interface != peer->interface)
1231                         continue;
1232
1233                 if (group->interface == peer->current_group_iface)
1234                         return true;
1235         }
1236
1237         return false;
1238 }
1239
1240 bool g_supplicant_peer_has_requested_connection(GSupplicantPeer *peer)
1241 {
1242         if (!peer)
1243                 return false;
1244
1245         return peer->connection_requested;
1246 }
1247
1248 #if defined TIZEN_EXT
1249 /*
1250  * Description: Network client requires additional wifi specific info
1251  */
1252 const unsigned char *g_supplicant_network_get_bssid(GSupplicantNetwork *network)
1253 {
1254         if (network == NULL || network->best_bss == NULL)
1255                 return NULL;
1256
1257         return (const unsigned char *)network->best_bss->bssid;
1258 }
1259
1260 unsigned int g_supplicant_network_get_maxrate(GSupplicantNetwork *network)
1261 {
1262         if (network == NULL || network->best_bss == NULL)
1263                 return 0;
1264
1265         return network->best_bss->maxrate;
1266 }
1267
1268 const char *g_supplicant_network_get_enc_mode(GSupplicantNetwork *network)
1269 {
1270         if (network == NULL || network->best_bss == NULL)
1271                 return NULL;
1272
1273         if (network->best_bss->security == G_SUPPLICANT_SECURITY_PSK ||
1274             network->best_bss->security == G_SUPPLICANT_SECURITY_IEEE8021X) {
1275                 unsigned int pairwise;
1276
1277                 pairwise = network->best_bss->rsn_pairwise |
1278                                 network->best_bss->wpa_pairwise;
1279
1280                 if ((pairwise & G_SUPPLICANT_PAIRWISE_CCMP) &&
1281                     (pairwise & G_SUPPLICANT_PAIRWISE_TKIP))
1282                         return "mixed";
1283                 else if (pairwise & G_SUPPLICANT_PAIRWISE_CCMP)
1284                         return "aes";
1285                 else if (pairwise & G_SUPPLICANT_PAIRWISE_TKIP)
1286                         return "tkip";
1287
1288         } else if (network->best_bss->security == G_SUPPLICANT_SECURITY_WEP)
1289                 return "wep";
1290         else if (network->best_bss->security == G_SUPPLICANT_SECURITY_NONE)
1291                 return "none";
1292
1293         return NULL;
1294 }
1295 #endif
1296
1297 static void merge_network(GSupplicantNetwork *network)
1298 {
1299         GString *str;
1300         const char *ssid, *mode, *key_mgmt;
1301 #if defined TIZEN_EXT
1302         GSupplicantInterface *interface;
1303         const char *isHS20AP;
1304         const char *eap, *identity, *phase2;
1305 #endif
1306         unsigned int i, ssid_len;
1307         char *group;
1308
1309         ssid = g_hash_table_lookup(network->config_table, "ssid");
1310         mode = g_hash_table_lookup(network->config_table, "mode");
1311         key_mgmt = g_hash_table_lookup(network->config_table, "key_mgmt");
1312 #if defined TIZEN_EXT
1313         isHS20AP = g_hash_table_lookup(network->config_table, "isHS20AP");
1314         eap = g_hash_table_lookup(network->config_table, "eap");
1315         identity = g_hash_table_lookup(network->config_table, "identity");
1316         phase2 = g_hash_table_lookup(network->config_table, "phase2");
1317         interface = network->interface;
1318 #endif
1319
1320         SUPPLICANT_DBG("ssid %s mode %s", ssid, mode);
1321
1322         if (ssid)
1323                 ssid_len = strlen(ssid);
1324         else
1325                 ssid_len = 0;
1326
1327         str = g_string_sized_new((ssid_len * 2) + 24);
1328         if (!str)
1329                 return;
1330
1331         for (i = 0; i < ssid_len; i++)
1332 #if defined TIZEN_EXT
1333         {
1334                 if (ssid[i] != '"')
1335 #endif
1336                 g_string_append_printf(str, "%02x", ssid[i]);
1337 #if defined TIZEN_EXT
1338         }
1339 #endif
1340
1341         if (g_strcmp0(mode, "0") == 0)
1342                 g_string_append_printf(str, "_managed");
1343         else if (g_strcmp0(mode, "1") == 0)
1344                 g_string_append_printf(str, "_adhoc");
1345
1346         if (g_strcmp0(key_mgmt, "WPA-PSK") == 0)
1347                 g_string_append_printf(str, "_psk");
1348 #if defined TIZEN_EXT
1349         else if (g_strcmp0(key_mgmt, "WPA-EAP") == 0)
1350                 g_string_append_printf(str, "_ieee8021x");
1351         else
1352                 g_string_append_printf(str, "_none");
1353 #endif
1354
1355         group = g_string_free(str, FALSE);
1356
1357         SUPPLICANT_DBG("%s", group);
1358
1359 #if defined TIZEN_EXT
1360         if (g_strcmp0(isHS20AP, "1") == 0) {
1361                 network->isHS20AP = 1;
1362                 if (network->eap)
1363                         g_free(network->eap);
1364                 network->eap = g_strdup(eap);
1365
1366                 if (network->identity)
1367                         g_free(network->identity);
1368                 network->identity = g_strdup(identity);
1369
1370                 if (network->phase2)
1371                         g_free(network->phase2);
1372                 network->phase2 = g_strdup(phase2);
1373         } else
1374                 network->isHS20AP = 0;
1375
1376         if (interface)
1377                 interface->network_path = g_strdup(network->path);
1378
1379         network->group = g_strdup(group);
1380         callback_network_merged(network);
1381 #endif
1382
1383         g_free(group);
1384
1385         g_hash_table_destroy(network->config_table);
1386
1387         g_free(network->path);
1388         g_free(network);
1389 }
1390
1391 static void network_property(const char *key, DBusMessageIter *iter,
1392                                                         void *user_data)
1393 {
1394         GSupplicantNetwork *network = user_data;
1395
1396         if (!network->interface)
1397                 return;
1398
1399         if (!key) {
1400                 merge_network(network);
1401                 return;
1402         }
1403
1404         if (g_strcmp0(key, "Enabled") == 0) {
1405                 dbus_bool_t enabled = FALSE;
1406
1407                 dbus_message_iter_get_basic(iter, &enabled);
1408         } else if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
1409                 const char *str = NULL;
1410
1411                 dbus_message_iter_get_basic(iter, &str);
1412                 if (str) {
1413                         g_hash_table_replace(network->config_table,
1414                                                 g_strdup(key), g_strdup(str));
1415                 }
1416         } else
1417                 SUPPLICANT_DBG("key %s type %c",
1418                                 key, dbus_message_iter_get_arg_type(iter));
1419 }
1420
1421 static void interface_network_added(DBusMessageIter *iter, void *user_data)
1422 {
1423         GSupplicantInterface *interface = user_data;
1424         GSupplicantNetwork *network;
1425         const char *path = NULL;
1426
1427         SUPPLICANT_DBG("");
1428
1429         dbus_message_iter_get_basic(iter, &path);
1430         if (!path)
1431                 return;
1432
1433         if (g_strcmp0(path, "/") == 0)
1434                 return;
1435
1436         network = g_hash_table_lookup(interface->net_mapping, path);
1437         if (network)
1438                 return;
1439
1440         network = g_try_new0(GSupplicantNetwork, 1);
1441         if (!network)
1442                 return;
1443
1444         network->interface = interface;
1445         network->path = g_strdup(path);
1446
1447         network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1448                                                         g_free, g_free);
1449
1450         dbus_message_iter_next(iter);
1451         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
1452                 supplicant_dbus_property_foreach(iter, network_property,
1453                                                                 network);
1454                 network_property(NULL, NULL, network);
1455                 return;
1456         }
1457
1458         supplicant_dbus_property_get_all(path,
1459                                 SUPPLICANT_INTERFACE ".Network",
1460                                         network_property, network, NULL);
1461 }
1462
1463 static void interface_network_removed(DBusMessageIter *iter, void *user_data)
1464 {
1465         GSupplicantInterface *interface = user_data;
1466         GSupplicantNetwork *network;
1467         const char *path = NULL;
1468
1469         dbus_message_iter_get_basic(iter, &path);
1470         if (!path)
1471                 return;
1472
1473         network = g_hash_table_lookup(interface->net_mapping, path);
1474         if (!network)
1475                 return;
1476
1477         g_hash_table_remove(interface->net_mapping, path);
1478 }
1479
1480 static char *create_name(unsigned char *ssid, int ssid_len)
1481 {
1482         GString *string;
1483         const gchar *remainder, *invalid;
1484         int valid_bytes, remaining_bytes;
1485
1486         if (ssid_len < 1 || ssid[0] == '\0')
1487                 return g_strdup("");
1488
1489         string = NULL;
1490         remainder = (const gchar *)ssid;
1491         remaining_bytes = ssid_len;
1492
1493         while (remaining_bytes != 0) {
1494                 if (g_utf8_validate(remainder, remaining_bytes,
1495                                         &invalid)) {
1496                         break;
1497                 }
1498
1499                 valid_bytes = invalid - remainder;
1500
1501                 if (!string)
1502                         string = g_string_sized_new(remaining_bytes);
1503
1504                 g_string_append_len(string, remainder, valid_bytes);
1505
1506                 /* append U+FFFD REPLACEMENT CHARACTER */
1507                 g_string_append(string, "\357\277\275");
1508
1509                 remaining_bytes -= valid_bytes + 1;
1510                 remainder = invalid + 1;
1511         }
1512
1513         if (!string)
1514                 return g_strndup((const gchar *)ssid, ssid_len + 1);
1515
1516         g_string_append(string, remainder);
1517
1518         return g_string_free(string, FALSE);
1519 }
1520
1521 static char *create_group(struct g_supplicant_bss *bss)
1522 {
1523         GString *str;
1524         unsigned int i;
1525         const char *mode, *security;
1526
1527         str = g_string_sized_new((bss->ssid_len * 2) + 24);
1528         if (!str)
1529                 return NULL;
1530
1531         if (bss->ssid_len > 0 && bss->ssid[0] != '\0') {
1532                 for (i = 0; i < bss->ssid_len; i++)
1533                         g_string_append_printf(str, "%02x", bss->ssid[i]);
1534         } else
1535                 g_string_append_printf(str, "hidden");
1536
1537         mode = mode2string(bss->mode);
1538         if (mode)
1539                 g_string_append_printf(str, "_%s", mode);
1540
1541         security = security2string(bss->security);
1542         if (security)
1543                 g_string_append_printf(str, "_%s", security);
1544
1545         return g_string_free(str, FALSE);
1546 }
1547
1548 static void add_or_replace_bss_to_network(struct g_supplicant_bss *bss)
1549 {
1550         GSupplicantInterface *interface = bss->interface;
1551         GSupplicantNetwork *network;
1552         char *group;
1553
1554         group = create_group(bss);
1555         SUPPLICANT_DBG("New group created: %s", group);
1556
1557         if (!group)
1558                 return;
1559
1560         network = g_hash_table_lookup(interface->network_table, group);
1561         if (network) {
1562                 g_free(group);
1563                 SUPPLICANT_DBG("Network %s already exist", network->name);
1564
1565                 goto done;
1566         }
1567
1568         network = g_try_new0(GSupplicantNetwork, 1);
1569         if (!network) {
1570                 g_free(group);
1571                 return;
1572         }
1573
1574         network->interface = interface;
1575         if (!network->path)
1576                 network->path = g_strdup(bss->path);
1577         network->group = group;
1578         network->name = create_name(bss->ssid, bss->ssid_len);
1579         network->mode = bss->mode;
1580         network->security = bss->security;
1581         network->ssid_len = bss->ssid_len;
1582         memcpy(network->ssid, bss->ssid, bss->ssid_len);
1583         network->signal = bss->signal;
1584         network->frequency = bss->frequency;
1585         network->best_bss = bss;
1586
1587         SUPPLICANT_DBG("New network %s created", network->name);
1588
1589         network->bss_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1590                                                         NULL, remove_bss);
1591
1592         network->config_table = g_hash_table_new_full(g_str_hash, g_str_equal,
1593                                                         g_free, g_free);
1594
1595         g_hash_table_replace(interface->network_table,
1596                                                 network->group, network);
1597
1598         callback_network_added(network);
1599
1600 done:
1601         /* We update network's WPS properties if only bss provides WPS. */
1602         if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
1603                 network->wps = TRUE;
1604                 network->wps_capabilities |= bss->wps_capabilities;
1605         }
1606
1607         if (bss->signal > network->signal) {
1608                 network->signal = bss->signal;
1609                 network->best_bss = bss;
1610                 callback_network_changed(network, "Signal");
1611         }
1612
1613         g_hash_table_replace(interface->bss_mapping, bss->path, network);
1614         g_hash_table_replace(network->bss_table, bss->path, bss);
1615
1616         g_hash_table_replace(bss_mapping, bss->path, interface);
1617 }
1618
1619 static void bss_rates(DBusMessageIter *iter, void *user_data)
1620 {
1621         struct g_supplicant_bss *bss = user_data;
1622         dbus_uint32_t rate = 0;
1623
1624         dbus_message_iter_get_basic(iter, &rate);
1625         if (rate == 0)
1626                 return;
1627
1628         if (rate > bss->maxrate)
1629                 bss->maxrate = rate;
1630 }
1631
1632 static void bss_keymgmt(DBusMessageIter *iter, void *user_data)
1633 {
1634         unsigned int *keymgmt = user_data;
1635         const char *str = NULL;
1636         int i;
1637
1638         dbus_message_iter_get_basic(iter, &str);
1639         if (!str)
1640                 return;
1641
1642         for (i = 0; keymgmt_map[i].str; i++)
1643                 if (strcmp(str, keymgmt_map[i].str) == 0) {
1644                         SUPPLICANT_DBG("Keymgmt: %s", str);
1645                         *keymgmt |= keymgmt_map[i].val;
1646                         break;
1647                 }
1648 }
1649
1650 static void bss_group(DBusMessageIter *iter, void *user_data)
1651 {
1652         unsigned int *group = user_data;
1653         const char *str = NULL;
1654         int i;
1655
1656         dbus_message_iter_get_basic(iter, &str);
1657         if (!str)
1658                 return;
1659
1660         for (i = 0; group_map[i].str; i++)
1661                 if (strcmp(str, group_map[i].str) == 0) {
1662                         SUPPLICANT_DBG("Group: %s", str);
1663                         *group |= group_map[i].val;
1664                         break;
1665                 }
1666 }
1667
1668 static void bss_pairwise(DBusMessageIter *iter, void *user_data)
1669 {
1670         unsigned int *pairwise = user_data;
1671         const char *str = NULL;
1672         int i;
1673
1674         dbus_message_iter_get_basic(iter, &str);
1675         if (!str)
1676                 return;
1677
1678         for (i = 0; pairwise_map[i].str; i++)
1679                 if (strcmp(str, pairwise_map[i].str) == 0) {
1680                         SUPPLICANT_DBG("Pairwise: %s", str);
1681                         *pairwise |= pairwise_map[i].val;
1682                         break;
1683                 }
1684 }
1685
1686 static void bss_wpa(const char *key, DBusMessageIter *iter,
1687                         void *user_data)
1688 {
1689         struct g_supplicant_bss *bss = user_data;
1690         unsigned int value = 0;
1691
1692         SUPPLICANT_DBG("Key: %s", key);
1693
1694         if (g_strcmp0(key, "KeyMgmt") == 0) {
1695                 supplicant_dbus_array_foreach(iter, bss_keymgmt, &value);
1696
1697                 if (bss->rsn_selected)
1698                         bss->rsn_keymgmt = value;
1699                 else
1700                         bss->wpa_keymgmt = value;
1701         } else if (g_strcmp0(key, "Group") == 0) {
1702                 supplicant_dbus_array_foreach(iter, bss_group, &value);
1703
1704                 if (bss->rsn_selected)
1705                         bss->rsn_group = value;
1706                 else
1707                         bss->wpa_group = value;
1708         } else if (g_strcmp0(key, "Pairwise") == 0) {
1709                 supplicant_dbus_array_foreach(iter, bss_pairwise, &value);
1710
1711                 if (bss->rsn_selected)
1712                         bss->rsn_pairwise = value;
1713                 else
1714                         bss->wpa_pairwise = value;
1715         }
1716 }
1717
1718 static unsigned int get_tlv(unsigned char *ie, unsigned int ie_size,
1719                                                         unsigned int type)
1720 {
1721         unsigned int len = 0;
1722
1723         while (len + 4 < ie_size) {
1724                 unsigned int hi = ie[len];
1725                 unsigned int lo = ie[len + 1];
1726                 unsigned int tmp_type = (hi << 8) + lo;
1727                 unsigned int v_len = 0;
1728
1729                 /* hi and lo are used to recreate an unsigned int
1730                  * based on 2 8bits length unsigned int. */
1731
1732                 hi = ie[len + 2];
1733                 lo = ie[len + 3];
1734                 v_len = (hi << 8) + lo;
1735
1736                 if (tmp_type == type) {
1737                         unsigned int ret_value = 0;
1738                         unsigned char *value = (unsigned char *)&ret_value;
1739
1740                         SUPPLICANT_DBG("IE: match type 0x%x", type);
1741
1742                         /* Verifying length relevance */
1743                         if (v_len > sizeof(unsigned int) ||
1744                                 len + 4 + v_len > ie_size)
1745                                 break;
1746
1747                         memcpy(value, ie + len + 4, v_len);
1748
1749                         SUPPLICANT_DBG("returning 0x%x", ret_value);
1750                         return ret_value;
1751                 }
1752
1753                 len += v_len + 4;
1754         }
1755
1756         SUPPLICANT_DBG("returning 0");
1757         return 0;
1758 }
1759
1760 static void bss_process_ies(DBusMessageIter *iter, void *user_data)
1761 {
1762         struct g_supplicant_bss *bss = user_data;
1763         const unsigned char WPS_OUI[] = { 0x00, 0x50, 0xf2, 0x04 };
1764         unsigned char *ie, *ie_end;
1765         DBusMessageIter array;
1766         unsigned int value;
1767         int ie_len;
1768
1769 #define WMM_WPA1_WPS_INFO 221
1770 #define WPS_INFO_MIN_LEN  6
1771 #define WPS_VERSION_TLV   0x104A
1772 #define WPS_STATE_TLV     0x1044
1773 #define WPS_METHODS_TLV   0x1012
1774 #define WPS_REGISTRAR_TLV 0x1041
1775 #define WPS_VERSION       0x10
1776 #define WPS_PBC           0x04
1777 #define WPS_PIN           0x00
1778 #define WPS_CONFIGURED    0x02
1779
1780         dbus_message_iter_recurse(iter, &array);
1781         dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
1782
1783         if (!ie || ie_len < 2)
1784                 return;
1785
1786         bss->wps_capabilities = 0;
1787         bss->keymgmt = 0;
1788
1789         for (ie_end = ie + ie_len; ie < ie_end && ie + ie[1] + 1 <= ie_end;
1790                                                         ie += ie[1] + 2) {
1791
1792                 if (ie[0] != WMM_WPA1_WPS_INFO || ie[1] < WPS_INFO_MIN_LEN ||
1793                         memcmp(ie+2, WPS_OUI, sizeof(WPS_OUI)) != 0)
1794                         continue;
1795
1796                 SUPPLICANT_DBG("IE: match WPS_OUI");
1797
1798                 value = get_tlv(&ie[6], ie[1], WPS_STATE_TLV);
1799                 if (get_tlv(&ie[6], ie[1], WPS_VERSION_TLV) == WPS_VERSION &&
1800                                                                 value != 0) {
1801                         bss->keymgmt |= G_SUPPLICANT_KEYMGMT_WPS;
1802
1803                         if (value == WPS_CONFIGURED)
1804                                 bss->wps_capabilities |=
1805                                         G_SUPPLICANT_WPS_CONFIGURED;
1806                 }
1807
1808                 value = get_tlv(&ie[6], ie[1], WPS_METHODS_TLV);
1809                 if (value != 0) {
1810                         if (GUINT16_FROM_BE(value) == WPS_PBC)
1811                                 bss->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
1812                         if (GUINT16_FROM_BE(value) == WPS_PIN)
1813                                 bss->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
1814                 } else
1815                         bss->wps_capabilities |=
1816                                 G_SUPPLICANT_WPS_PBC | G_SUPPLICANT_WPS_PIN;
1817
1818                 /* If the AP sends this it means it's advertizing
1819                  * as a registrar and the WPS process is launched
1820                  * on its side */
1821                 if (get_tlv(&ie[6], ie[1], WPS_REGISTRAR_TLV) != 0)
1822                         bss->wps_capabilities |= G_SUPPLICANT_WPS_REGISTRAR;
1823
1824                 SUPPLICANT_DBG("WPS Methods 0x%x", bss->wps_capabilities);
1825         }
1826 }
1827
1828 static void bss_compute_security(struct g_supplicant_bss *bss)
1829 {
1830         /*
1831          * Combining RSN and WPA keymgmt
1832          * We combine it since parsing IEs might have set something for WPS. */
1833         bss->keymgmt |= bss->rsn_keymgmt | bss->wpa_keymgmt;
1834
1835         bss->ieee8021x = FALSE;
1836         bss->psk = FALSE;
1837
1838         if (bss->keymgmt &
1839                         (G_SUPPLICANT_KEYMGMT_WPA_EAP |
1840                                 G_SUPPLICANT_KEYMGMT_WPA_FT_EAP |
1841                                 G_SUPPLICANT_KEYMGMT_WPA_EAP_256))
1842                 bss->ieee8021x = TRUE;
1843
1844         if (bss->keymgmt &
1845                         (G_SUPPLICANT_KEYMGMT_WPA_PSK |
1846                                 G_SUPPLICANT_KEYMGMT_WPA_FT_PSK |
1847                                 G_SUPPLICANT_KEYMGMT_WPA_PSK_256))
1848                 bss->psk = TRUE;
1849
1850         if (bss->ieee8021x)
1851                 bss->security = G_SUPPLICANT_SECURITY_IEEE8021X;
1852         else if (bss->psk)
1853                 bss->security = G_SUPPLICANT_SECURITY_PSK;
1854         else if (bss->privacy)
1855                 bss->security = G_SUPPLICANT_SECURITY_WEP;
1856         else
1857                 bss->security = G_SUPPLICANT_SECURITY_NONE;
1858 }
1859
1860
1861 static void bss_property(const char *key, DBusMessageIter *iter,
1862                                                         void *user_data)
1863 {
1864         struct g_supplicant_bss *bss = user_data;
1865
1866         if (!bss->interface)
1867                 return;
1868
1869         SUPPLICANT_DBG("key %s", key);
1870
1871         if (!key)
1872                 return;
1873
1874         if (g_strcmp0(key, "BSSID") == 0) {
1875                 DBusMessageIter array;
1876                 unsigned char *addr;
1877                 int addr_len;
1878
1879                 dbus_message_iter_recurse(iter, &array);
1880                 dbus_message_iter_get_fixed_array(&array, &addr, &addr_len);
1881
1882                 if (addr_len == 6)
1883                         memcpy(bss->bssid, addr, addr_len);
1884         } else if (g_strcmp0(key, "SSID") == 0) {
1885                 DBusMessageIter array;
1886                 unsigned char *ssid;
1887                 int ssid_len;
1888
1889                 dbus_message_iter_recurse(iter, &array);
1890                 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
1891
1892                 if (ssid_len > 0 && ssid_len < 33) {
1893                         memcpy(bss->ssid, ssid, ssid_len);
1894                         bss->ssid_len = ssid_len;
1895                 } else {
1896                         memset(bss->ssid, 0, sizeof(bss->ssid));
1897                         bss->ssid_len = 0;
1898                 }
1899         } else if (g_strcmp0(key, "Capabilities") == 0) {
1900                 dbus_uint16_t capabilities = 0x0000;
1901
1902                 dbus_message_iter_get_basic(iter, &capabilities);
1903
1904                 if (capabilities & IEEE80211_CAP_ESS)
1905                         bss->mode = G_SUPPLICANT_MODE_INFRA;
1906                 else if (capabilities & IEEE80211_CAP_IBSS)
1907                         bss->mode = G_SUPPLICANT_MODE_IBSS;
1908
1909                 if (capabilities & IEEE80211_CAP_PRIVACY)
1910                         bss->privacy = TRUE;
1911         } else if (g_strcmp0(key, "Mode") == 0) {
1912                 const char *mode = NULL;
1913
1914                 dbus_message_iter_get_basic(iter, &mode);
1915                 bss->mode = string2mode(mode);
1916         } else if (g_strcmp0(key, "Frequency") == 0) {
1917                 dbus_uint16_t frequency = 0;
1918
1919                 dbus_message_iter_get_basic(iter, &frequency);
1920                 bss->frequency = frequency;
1921         } else if (g_strcmp0(key, "Signal") == 0) {
1922                 dbus_int16_t signal = 0;
1923
1924                 dbus_message_iter_get_basic(iter, &signal);
1925
1926                 bss->signal = signal;
1927                 if (!bss->signal)
1928                         bss->signal = BSS_UNKNOWN_STRENGTH;
1929
1930         } else if (g_strcmp0(key, "Level") == 0) {
1931                 dbus_int32_t level = 0;
1932
1933                 dbus_message_iter_get_basic(iter, &level);
1934         } else if (g_strcmp0(key, "Rates") == 0) {
1935                 supplicant_dbus_array_foreach(iter, bss_rates, bss);
1936         } else if (g_strcmp0(key, "MaxRate") == 0) {
1937                 dbus_uint32_t maxrate = 0;
1938
1939                 dbus_message_iter_get_basic(iter, &maxrate);
1940                 if (maxrate != 0)
1941                         bss->maxrate = maxrate;
1942         } else if (g_strcmp0(key, "Privacy") == 0) {
1943                 dbus_bool_t privacy = FALSE;
1944
1945                 dbus_message_iter_get_basic(iter, &privacy);
1946                 bss->privacy = privacy;
1947         } else if (g_strcmp0(key, "RSN") == 0) {
1948                 bss->rsn_selected = TRUE;
1949
1950                 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1951         } else if (g_strcmp0(key, "WPA") == 0) {
1952                 bss->rsn_selected = FALSE;
1953
1954                 supplicant_dbus_property_foreach(iter, bss_wpa, bss);
1955         } else if (g_strcmp0(key, "IEs") == 0)
1956                 bss_process_ies(iter, bss);
1957         else
1958                 SUPPLICANT_DBG("key %s type %c",
1959                                 key, dbus_message_iter_get_arg_type(iter));
1960 }
1961
1962 static struct g_supplicant_bss *interface_bss_added(DBusMessageIter *iter,
1963                                                         void *user_data)
1964 {
1965         GSupplicantInterface *interface = user_data;
1966         GSupplicantNetwork *network;
1967         struct g_supplicant_bss *bss;
1968         const char *path = NULL;
1969
1970         SUPPLICANT_DBG("");
1971
1972         dbus_message_iter_get_basic(iter, &path);
1973         if (!path)
1974                 return NULL;
1975
1976         if (g_strcmp0(path, "/") == 0)
1977                 return NULL;
1978
1979         SUPPLICANT_DBG("%s", path);
1980
1981         network = g_hash_table_lookup(interface->bss_mapping, path);
1982         if (network) {
1983                 bss = g_hash_table_lookup(network->bss_table, path);
1984                 if (bss)
1985                         return NULL;
1986         }
1987
1988         bss = g_try_new0(struct g_supplicant_bss, 1);
1989         if (!bss)
1990                 return NULL;
1991
1992         bss->interface = interface;
1993         bss->path = g_strdup(path);
1994         bss->signal = BSS_UNKNOWN_STRENGTH;
1995
1996         return bss;
1997 }
1998
1999 static void interface_bss_added_with_keys(DBusMessageIter *iter,
2000                                                 void *user_data)
2001 {
2002         struct g_supplicant_bss *bss;
2003
2004         SUPPLICANT_DBG("");
2005
2006         bss = interface_bss_added(iter, user_data);
2007         if (!bss)
2008                 return;
2009
2010         dbus_message_iter_next(iter);
2011
2012         if (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_INVALID)
2013                 return;
2014
2015         supplicant_dbus_property_foreach(iter, bss_property, bss);
2016
2017         bss_compute_security(bss);
2018         add_or_replace_bss_to_network(bss);
2019 }
2020
2021 static void interface_bss_added_without_keys(DBusMessageIter *iter,
2022                                                 void *user_data)
2023 {
2024         struct g_supplicant_bss *bss;
2025
2026         SUPPLICANT_DBG("");
2027
2028         bss = interface_bss_added(iter, user_data);
2029         if (!bss)
2030                 return;
2031
2032         supplicant_dbus_property_get_all(bss->path,
2033                                         SUPPLICANT_INTERFACE ".BSS",
2034                                         bss_property, bss, NULL);
2035
2036         bss_compute_security(bss);
2037         add_or_replace_bss_to_network(bss);
2038 }
2039
2040 static void update_signal(gpointer key, gpointer value,
2041                                                 gpointer user_data)
2042 {
2043         struct g_supplicant_bss *bss = value;
2044         GSupplicantNetwork *network = user_data;
2045
2046         if (bss->signal > network->signal) {
2047                 network->signal = bss->signal;
2048                 network->best_bss = bss;
2049         }
2050 }
2051
2052 static void update_network_signal(GSupplicantNetwork *network)
2053 {
2054         if (g_hash_table_size(network->bss_table) <= 1 && network->best_bss)
2055                 return;
2056
2057         g_hash_table_foreach(network->bss_table,
2058                                 update_signal, network);
2059
2060         SUPPLICANT_DBG("New network signal %d", network->signal);
2061 }
2062
2063 static void interface_bss_removed(DBusMessageIter *iter, void *user_data)
2064 {
2065         GSupplicantInterface *interface = user_data;
2066         GSupplicantNetwork *network;
2067         struct g_supplicant_bss *bss = NULL;
2068         const char *path = NULL;
2069
2070         dbus_message_iter_get_basic(iter, &path);
2071         if (!path)
2072                 return;
2073
2074         network = g_hash_table_lookup(interface->bss_mapping, path);
2075         if (!network)
2076                 return;
2077
2078         bss = g_hash_table_lookup(network->bss_table, path);
2079         if (network->best_bss == bss) {
2080                 network->best_bss = NULL;
2081                 network->signal = BSS_UNKNOWN_STRENGTH;
2082         }
2083
2084         g_hash_table_remove(bss_mapping, path);
2085
2086         g_hash_table_remove(interface->bss_mapping, path);
2087         g_hash_table_remove(network->bss_table, path);
2088
2089         update_network_signal(network);
2090
2091         if (g_hash_table_size(network->bss_table) == 0)
2092                 g_hash_table_remove(interface->network_table, network->group);
2093 }
2094
2095 static void set_config_methods(DBusMessageIter *iter, void *user_data)
2096 {
2097         const char *config_methods = "push_button";
2098
2099         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
2100                                                         &config_methods);
2101 }
2102
2103 static void interface_property(const char *key, DBusMessageIter *iter,
2104                                                         void *user_data)
2105 {
2106         GSupplicantInterface *interface = user_data;
2107
2108         if (!interface)
2109                 return;
2110
2111         SUPPLICANT_DBG("%s", key);
2112
2113         if (!key) {
2114                 debug_strvalmap("KeyMgmt capability", keymgmt_map,
2115                                                 interface->keymgmt_capa);
2116                 debug_strvalmap("AuthAlg capability", authalg_capa_map,
2117                                                 interface->authalg_capa);
2118                 debug_strvalmap("Protocol capability", proto_capa_map,
2119                                                 interface->proto_capa);
2120                 debug_strvalmap("Pairwise capability", pairwise_map,
2121                                                 interface->pairwise_capa);
2122                 debug_strvalmap("Group capability", group_map,
2123                                                 interface->group_capa);
2124                 debug_strvalmap("Scan capability", scan_capa_map,
2125                                                 interface->scan_capa);
2126                 debug_strvalmap("Mode capability", mode_capa_map,
2127                                                 interface->mode_capa);
2128
2129
2130                 supplicant_dbus_property_set(interface->path,
2131                                 SUPPLICANT_INTERFACE ".Interface.WPS",
2132                                 "ConfigMethods", DBUS_TYPE_STRING_AS_STRING,
2133                                 set_config_methods, NULL, NULL, NULL);
2134
2135                 if (interface->ready)
2136                         callback_interface_added(interface);
2137
2138                 return;
2139         }
2140
2141         if (g_strcmp0(key, "Capabilities") == 0) {
2142                 supplicant_dbus_property_foreach(iter, interface_capability,
2143                                                                 interface);
2144 #if !defined TIZEN_EXT
2145                 if (interface->mode_capa & G_SUPPLICANT_CAPABILITY_MODE_P2P)
2146                         interface->p2p_support = true;
2147 #endif
2148         } else if (g_strcmp0(key, "State") == 0) {
2149                 const char *str = NULL;
2150
2151                 dbus_message_iter_get_basic(iter, &str);
2152                 if (str)
2153                         if (string2state(str) != interface->state) {
2154                                 interface->state = string2state(str);
2155                                 callback_interface_state(interface);
2156                         }
2157                 if (interface->state == G_SUPPLICANT_STATE_DISABLED)
2158                         interface->ready = FALSE;
2159                 else
2160                         interface->ready = TRUE;
2161
2162                 SUPPLICANT_DBG("state %s (%d)", str, interface->state);
2163         } else if (g_strcmp0(key, "Scanning") == 0) {
2164                 dbus_bool_t scanning = FALSE;
2165
2166                 dbus_message_iter_get_basic(iter, &scanning);
2167                 interface->scanning = scanning;
2168
2169                 if (interface->ready) {
2170                         if (interface->scanning)
2171                                 callback_scan_started(interface);
2172                         else
2173                                 callback_scan_finished(interface);
2174                 }
2175         } else if (g_strcmp0(key, "ApScan") == 0) {
2176                 int apscan = 1;
2177
2178                 dbus_message_iter_get_basic(iter, &apscan);
2179                 interface->apscan = apscan;
2180         } else if (g_strcmp0(key, "Ifname") == 0) {
2181                 const char *str = NULL;
2182
2183                 dbus_message_iter_get_basic(iter, &str);
2184                 if (str) {
2185                         g_free(interface->ifname);
2186                         interface->ifname = g_strdup(str);
2187                 }
2188         } else if (g_strcmp0(key, "Driver") == 0) {
2189                 const char *str = NULL;
2190
2191                 dbus_message_iter_get_basic(iter, &str);
2192                 if (str) {
2193                         g_free(interface->driver);
2194                         interface->driver = g_strdup(str);
2195                 }
2196         } else if (g_strcmp0(key, "BridgeIfname") == 0) {
2197                 const char *str = NULL;
2198
2199                 dbus_message_iter_get_basic(iter, &str);
2200                 if (str) {
2201                         g_free(interface->bridge);
2202                         interface->bridge = g_strdup(str);
2203                 }
2204         } else if (g_strcmp0(key, "CurrentBSS") == 0) {
2205                 interface_bss_added_without_keys(iter, interface);
2206         } else if (g_strcmp0(key, "CurrentNetwork") == 0) {
2207                 interface_network_added(iter, interface);
2208         } else if (g_strcmp0(key, "BSSs") == 0) {
2209                 supplicant_dbus_array_foreach(iter,
2210                                         interface_bss_added_without_keys,
2211                                         interface);
2212         } else if (g_strcmp0(key, "Blobs") == 0) {
2213                 /* Nothing */
2214         } else if (g_strcmp0(key, "Networks") == 0) {
2215                 supplicant_dbus_array_foreach(iter, interface_network_added,
2216                                                                 interface);
2217         } else
2218                 SUPPLICANT_DBG("key %s type %c",
2219                                 key, dbus_message_iter_get_arg_type(iter));
2220 }
2221
2222 static void scan_network_update(DBusMessageIter *iter, void *user_data)
2223 {
2224         GSupplicantInterface *interface = user_data;
2225         GSupplicantNetwork *network;
2226         char *path;
2227
2228         if (!iter)
2229                 return;
2230
2231         dbus_message_iter_get_basic(iter, &path);
2232
2233         if (!path)
2234                 return;
2235
2236         if (g_strcmp0(path, "/") == 0)
2237                 return;
2238
2239         /* Update the network details based on scan BSS data */
2240         network = g_hash_table_lookup(interface->bss_mapping, path);
2241         if (network)
2242                 callback_network_added(network);
2243 }
2244
2245 static void scan_bss_data(const char *key, DBusMessageIter *iter,
2246                                 void *user_data)
2247 {
2248         GSupplicantInterface *interface = user_data;
2249
2250         if (iter)
2251                 supplicant_dbus_array_foreach(iter, scan_network_update,
2252                                                 interface);
2253
2254         if (interface->scan_callback)
2255                 interface->scan_callback(0, interface, interface->scan_data);
2256
2257         interface->scan_callback = NULL;
2258         interface->scan_data = NULL;
2259 }
2260
2261 static GSupplicantInterface *interface_alloc(const char *path)
2262 {
2263         GSupplicantInterface *interface;
2264
2265         interface = g_try_new0(GSupplicantInterface, 1);
2266         if (!interface)
2267                 return NULL;
2268
2269         interface->path = g_strdup(path);
2270
2271         interface->network_table = g_hash_table_new_full(g_str_hash,
2272                                         g_str_equal, NULL, remove_network);
2273         interface->peer_table = g_hash_table_new_full(g_str_hash,
2274                                         g_str_equal, NULL, remove_peer);
2275         interface->group_table = g_hash_table_new_full(g_str_hash,
2276                                         g_str_equal, NULL, remove_group);
2277         interface->net_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
2278                                                                 NULL, NULL);
2279         interface->bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
2280                                                                 NULL, NULL);
2281
2282         g_hash_table_replace(interface_table, interface->path, interface);
2283
2284         return interface;
2285 }
2286
2287 static void interface_added(DBusMessageIter *iter, void *user_data)
2288 {
2289         GSupplicantInterface *interface;
2290         const char *path = NULL;
2291
2292         SUPPLICANT_DBG("");
2293
2294         dbus_message_iter_get_basic(iter, &path);
2295         if (!path)
2296                 return;
2297
2298         if (g_strcmp0(path, "/") == 0)
2299                 return;
2300
2301         interface = g_hash_table_lookup(interface_table, path);
2302         if (interface)
2303                 return;
2304
2305         interface = interface_alloc(path);
2306         if (!interface)
2307                 return;
2308
2309         dbus_message_iter_next(iter);
2310         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
2311                 supplicant_dbus_property_foreach(iter, interface_property,
2312                                                                 interface);
2313                 interface_property(NULL, NULL, interface);
2314                 return;
2315         }
2316
2317         supplicant_dbus_property_get_all(path,
2318                                         SUPPLICANT_INTERFACE ".Interface",
2319                                         interface_property, interface,
2320                                         interface);
2321 }
2322
2323 static void interface_removed(DBusMessageIter *iter, void *user_data)
2324 {
2325         const char *path = NULL;
2326         GSupplicantInterface *interface = user_data;
2327
2328         dbus_message_iter_get_basic(iter, &path);
2329         if (!path)
2330                 return;
2331
2332         interface = g_hash_table_lookup(interface_table, path);
2333         g_supplicant_interface_cancel(interface);
2334
2335         g_hash_table_remove(interface_table, path);
2336 }
2337
2338 static void eap_method(DBusMessageIter *iter, void *user_data)
2339 {
2340         const char *str = NULL;
2341         int i;
2342
2343         dbus_message_iter_get_basic(iter, &str);
2344         if (!str)
2345                 return;
2346
2347         for (i = 0; eap_method_map[i].str; i++)
2348                 if (strcmp(str, eap_method_map[i].str) == 0) {
2349                         eap_methods |= eap_method_map[i].val;
2350                         break;
2351                 }
2352 }
2353
2354 static void service_property(const char *key, DBusMessageIter *iter,
2355                                                         void *user_data)
2356 {
2357         if (!key) {
2358                 callback_system_ready();
2359                 return;
2360         }
2361
2362         if (g_strcmp0(key, "DebugLevel") == 0) {
2363                 const char *str = NULL;
2364                 int i;
2365
2366                 dbus_message_iter_get_basic(iter, &str);
2367                 for (i = 0; debug_strings[i]; i++)
2368                         if (g_strcmp0(debug_strings[i], str) == 0) {
2369                                 debug_level = i;
2370                                 break;
2371                         }
2372                 SUPPLICANT_DBG("Debug level %d", debug_level);
2373         } else if (g_strcmp0(key, "DebugTimestamp") == 0) {
2374                 dbus_message_iter_get_basic(iter, &debug_timestamp);
2375                 SUPPLICANT_DBG("Debug timestamp %u", debug_timestamp);
2376         } else if (g_strcmp0(key, "DebugShowKeys") == 0) {
2377                 dbus_message_iter_get_basic(iter, &debug_showkeys);
2378                 SUPPLICANT_DBG("Debug show keys %u", debug_showkeys);
2379         } else if (g_strcmp0(key, "Interfaces") == 0) {
2380                 supplicant_dbus_array_foreach(iter, interface_added, NULL);
2381         } else if (g_strcmp0(key, "EapMethods") == 0) {
2382                 supplicant_dbus_array_foreach(iter, eap_method, NULL);
2383                 debug_strvalmap("EAP method", eap_method_map, eap_methods);
2384         } else if (g_strcmp0(key, "Country") == 0) {
2385                 const char *country = NULL;
2386
2387                 dbus_message_iter_get_basic(iter, &country);
2388                 SUPPLICANT_DBG("Country %s", country);
2389         } else
2390                 SUPPLICANT_DBG("key %s type %c",
2391                                 key, dbus_message_iter_get_arg_type(iter));
2392 }
2393
2394 static void signal_name_owner_changed(const char *path, DBusMessageIter *iter)
2395 {
2396         const char *name = NULL, *old = NULL, *new = NULL;
2397
2398         SUPPLICANT_DBG("");
2399
2400         if (g_strcmp0(path, DBUS_PATH_DBUS) != 0)
2401                 return;
2402
2403         dbus_message_iter_get_basic(iter, &name);
2404         if (!name)
2405                 return;
2406
2407         if (g_strcmp0(name, SUPPLICANT_SERVICE) != 0)
2408                 return;
2409
2410         dbus_message_iter_next(iter);
2411         dbus_message_iter_get_basic(iter, &old);
2412         dbus_message_iter_next(iter);
2413         dbus_message_iter_get_basic(iter, &new);
2414
2415         if (!old || !new)
2416                 return;
2417
2418         if (strlen(old) > 0 && strlen(new) == 0) {
2419                 system_available = FALSE;
2420                 g_hash_table_remove_all(bss_mapping);
2421                 g_hash_table_remove_all(peer_mapping);
2422                 g_hash_table_remove_all(group_mapping);
2423                 g_hash_table_remove_all(interface_table);
2424                 callback_system_killed();
2425         }
2426
2427         if (strlen(new) > 0 && strlen(old) == 0) {
2428                 system_available = TRUE;
2429                 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
2430                                                 SUPPLICANT_INTERFACE,
2431                                                 service_property, NULL, NULL);
2432         }
2433 }
2434
2435 static void signal_properties_changed(const char *path, DBusMessageIter *iter)
2436 {
2437         SUPPLICANT_DBG("");
2438
2439         if (g_strcmp0(path, SUPPLICANT_PATH) != 0)
2440                 return;
2441
2442         supplicant_dbus_property_foreach(iter, service_property, NULL);
2443 }
2444
2445 static void signal_interface_added(const char *path, DBusMessageIter *iter)
2446 {
2447         SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2448
2449         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
2450                 interface_added(iter, NULL);
2451 }
2452
2453 static void signal_interface_removed(const char *path, DBusMessageIter *iter)
2454 {
2455         SUPPLICANT_DBG("");
2456
2457         if (g_strcmp0(path, SUPPLICANT_PATH) == 0)
2458                 interface_removed(iter, NULL);
2459 }
2460
2461 static void signal_interface_changed(const char *path, DBusMessageIter *iter)
2462 {
2463         GSupplicantInterface *interface;
2464
2465         SUPPLICANT_DBG("");
2466
2467         interface = g_hash_table_lookup(interface_table, path);
2468         if (!interface)
2469                 return;
2470
2471         supplicant_dbus_property_foreach(iter, interface_property, interface);
2472 }
2473
2474 static void signal_scan_done(const char *path, DBusMessageIter *iter)
2475 {
2476         GSupplicantInterface *interface;
2477         dbus_bool_t success = FALSE;
2478
2479         SUPPLICANT_DBG("");
2480
2481         interface = g_hash_table_lookup(interface_table, path);
2482         if (!interface)
2483                 return;
2484
2485         dbus_message_iter_get_basic(iter, &success);
2486
2487         if (interface->scanning) {
2488                 callback_scan_finished(interface);
2489                 interface->scanning = FALSE;
2490         }
2491
2492         /*
2493          * If scan is unsuccessful return -EIO else get the scanned BSSs
2494          * and update the network details accordingly
2495          */
2496         if (!success) {
2497                 if (interface->scan_callback)
2498                         interface->scan_callback(-EIO, interface,
2499                                                 interface->scan_data);
2500
2501                 interface->scan_callback = NULL;
2502                 interface->scan_data = NULL;
2503
2504                 return;
2505         }
2506
2507         supplicant_dbus_property_get(path, SUPPLICANT_INTERFACE ".Interface",
2508                                 "BSSs", scan_bss_data, interface, interface);
2509 }
2510
2511 static void signal_bss_added(const char *path, DBusMessageIter *iter)
2512 {
2513         GSupplicantInterface *interface;
2514
2515         SUPPLICANT_DBG("");
2516
2517         interface = g_hash_table_lookup(interface_table, path);
2518         if (!interface)
2519                 return;
2520
2521         interface_bss_added_with_keys(iter, interface);
2522 }
2523
2524 static void signal_bss_removed(const char *path, DBusMessageIter *iter)
2525 {
2526         GSupplicantInterface *interface;
2527
2528         SUPPLICANT_DBG("");
2529
2530         interface = g_hash_table_lookup(interface_table, path);
2531         if (!interface)
2532                 return;
2533
2534         interface_bss_removed(iter, interface);
2535 }
2536
2537 static void signal_network_added(const char *path, DBusMessageIter *iter)
2538 {
2539         GSupplicantInterface *interface;
2540
2541         SUPPLICANT_DBG("");
2542
2543         interface = g_hash_table_lookup(interface_table, path);
2544         if (!interface)
2545                 return;
2546
2547         interface_network_added(iter, interface);
2548 }
2549
2550 static void signal_network_removed(const char *path, DBusMessageIter *iter)
2551 {
2552         GSupplicantInterface *interface;
2553
2554         SUPPLICANT_DBG("");
2555
2556         interface = g_hash_table_lookup(interface_table, path);
2557         if (!interface)
2558                 return;
2559
2560         interface_network_removed(iter, interface);
2561 }
2562
2563 static void signal_bss_changed(const char *path, DBusMessageIter *iter)
2564 {
2565         GSupplicantInterface *interface;
2566         GSupplicantNetwork *network;
2567         GSupplicantSecurity old_security;
2568         struct g_supplicant_bss *bss;
2569
2570         SUPPLICANT_DBG("");
2571
2572         interface = g_hash_table_lookup(bss_mapping, path);
2573         if (!interface)
2574                 return;
2575
2576         network = g_hash_table_lookup(interface->bss_mapping, path);
2577         if (!network)
2578                 return;
2579
2580         bss = g_hash_table_lookup(network->bss_table, path);
2581         if (!bss)
2582                 return;
2583
2584         supplicant_dbus_property_foreach(iter, bss_property, bss);
2585
2586         old_security = network->security;
2587         bss_compute_security(bss);
2588
2589         if (old_security != bss->security) {
2590                 struct g_supplicant_bss *new_bss;
2591
2592                 SUPPLICANT_DBG("New network security for %s", bss->ssid);
2593
2594                 /* Security change policy:
2595                  * - we first copy the current bss into a new one with
2596                  * its own pointer (path)
2597                  * - we remove the current bss related network which will
2598                  * tell the plugin about such removal. This is done due
2599                  * to the fact that a security change means a group change
2600                  * so a complete network change.
2601                  * (current bss becomes invalid as well)
2602                  * - we add the new bss: it adds new network and tell the
2603                  * plugin about it. */
2604
2605                 new_bss = g_try_new0(struct g_supplicant_bss, 1);
2606                 if (!new_bss)
2607                         return;
2608
2609                 memcpy(new_bss, bss, sizeof(struct g_supplicant_bss));
2610                 new_bss->path = g_strdup(bss->path);
2611
2612                 g_hash_table_remove(interface->network_table, network->group);
2613
2614                 add_or_replace_bss_to_network(new_bss);
2615
2616                 return;
2617         }
2618
2619 #if defined TIZEN_EXT
2620         if ((bss->keymgmt & G_SUPPLICANT_KEYMGMT_WPS) != 0) {
2621                 network->wps = TRUE;
2622                 network->wps_capabilities |= bss->wps_capabilities;
2623         } else
2624                 network->wps = FALSE;
2625 #endif
2626
2627         if (bss->signal == network->signal)
2628 #ifndef TIZEN_EXT
2629                 return;
2630 #else
2631         {
2632                 callback_network_changed(network, "");
2633                 return;
2634         }
2635 #endif
2636
2637         /*
2638          * If the new signal is lower than the SSID signal, we need
2639          * to check for the new maximum.
2640          */
2641         if (bss->signal < network->signal) {
2642                 if (bss != network->best_bss)
2643 #ifndef TIZEN_EXT
2644                         return;
2645 #else
2646                 {
2647                         callback_network_changed(network, "");
2648                         return;
2649                 }
2650 #endif
2651                 network->signal = bss->signal;
2652                 update_network_signal(network);
2653         } else {
2654                 network->signal = bss->signal;
2655                 network->best_bss = bss;
2656         }
2657
2658         SUPPLICANT_DBG("New network signal for %s %d dBm", network->ssid,
2659                         network->signal);
2660
2661         callback_network_changed(network, "Signal");
2662 }
2663
2664 static void wps_credentials(const char *key, DBusMessageIter *iter,
2665                         void *user_data)
2666 {
2667         GSupplicantInterface *interface = user_data;
2668
2669         if (!key)
2670                 return;
2671
2672         SUPPLICANT_DBG("key %s", key);
2673
2674         if (g_strcmp0(key, "Key") == 0) {
2675                 DBusMessageIter array;
2676                 unsigned char *key_val;
2677                 int key_len;
2678
2679                 dbus_message_iter_recurse(iter, &array);
2680                 dbus_message_iter_get_fixed_array(&array, &key_val, &key_len);
2681
2682                 g_free(interface->wps_cred.key);
2683                 interface->wps_cred.key = g_try_malloc0(
2684                                                 sizeof(char) * key_len + 1);
2685
2686                 if (!interface->wps_cred.key)
2687                         return;
2688
2689                 memcpy(interface->wps_cred.key, key_val,
2690                                                 sizeof(char) * key_len);
2691
2692                 SUPPLICANT_DBG("WPS key present");
2693         } else if (g_strcmp0(key, "SSID") == 0) {
2694                 DBusMessageIter array;
2695                 unsigned char *ssid;
2696                 int ssid_len;
2697
2698                 dbus_message_iter_recurse(iter, &array);
2699                 dbus_message_iter_get_fixed_array(&array, &ssid, &ssid_len);
2700
2701                 if (ssid_len > 0 && ssid_len < 33) {
2702                         memcpy(interface->wps_cred.ssid, ssid, ssid_len);
2703                         interface->wps_cred.ssid_len = ssid_len;
2704                 } else {
2705                         memset(interface->wps_cred.ssid, 0, 32);
2706                         interface->wps_cred.ssid_len = 0;
2707                 }
2708         }
2709 }
2710
2711 static void signal_wps_credentials(const char *path, DBusMessageIter *iter)
2712 {
2713         GSupplicantInterface *interface;
2714
2715         SUPPLICANT_DBG("");
2716
2717         interface = g_hash_table_lookup(interface_table, path);
2718         if (!interface)
2719                 return;
2720
2721         supplicant_dbus_property_foreach(iter, wps_credentials, interface);
2722 }
2723
2724 static void wps_event_args(const char *key, DBusMessageIter *iter,
2725                         void *user_data)
2726 {
2727         GSupplicantInterface *interface = user_data;
2728
2729         if (!key || !interface)
2730                 return;
2731
2732         SUPPLICANT_DBG("Arg Key %s", key);
2733 }
2734
2735 static void signal_wps_event(const char *path, DBusMessageIter *iter)
2736 {
2737         GSupplicantInterface *interface;
2738         const char *name = NULL;
2739
2740         SUPPLICANT_DBG("");
2741
2742         interface = g_hash_table_lookup(interface_table, path);
2743         if (!interface)
2744                 return;
2745
2746         dbus_message_iter_get_basic(iter, &name);
2747
2748         SUPPLICANT_DBG("Name: %s", name);
2749
2750         if (g_strcmp0(name, "success") == 0)
2751                 interface->wps_state = G_SUPPLICANT_WPS_STATE_SUCCESS;
2752         else if (g_strcmp0(name, "failed") == 0)
2753                 interface->wps_state = G_SUPPLICANT_WPS_STATE_FAIL;
2754         else
2755                 interface->wps_state = G_SUPPLICANT_WPS_STATE_UNKNOWN;
2756
2757         if (!dbus_message_iter_has_next(iter))
2758                 return;
2759
2760         dbus_message_iter_next(iter);
2761
2762         supplicant_dbus_property_foreach(iter, wps_event_args, interface);
2763 }
2764
2765 #if defined TIZEN_EXT
2766 static void signal_power_off(const char *path, DBusMessageIter *iter)
2767 {
2768         int poweroff_state = 0;
2769
2770         dbus_message_iter_get_basic(iter, &poweroff_state);
2771
2772         SUPPLICANT_DBG("poweroff_state(%d)", poweroff_state);
2773
2774         /* POWER_OFF_DIRECT 2 && POWER_OFF_RESTART 3 */
2775         if (poweroff_state != 2 && poweroff_state != 3)
2776                 return;
2777
2778         if (callbacks_pointer == NULL)
2779                 return;
2780
2781         if (callbacks_pointer->system_power_off == NULL)
2782                 return;
2783
2784         callbacks_pointer->system_power_off();
2785 }
2786 #endif
2787
2788 static void signal_station_connected(const char *path, DBusMessageIter *iter)
2789 {
2790         GSupplicantInterface *interface;
2791         const char *sta_mac = NULL;
2792
2793         SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2794
2795         if (callbacks_pointer->add_station == NULL)
2796                 return;
2797
2798         if (g_strcmp0(path, "/") == 0)
2799                 return;
2800
2801         interface = g_hash_table_lookup(interface_table, path);
2802         if (interface == NULL)
2803                 return;
2804
2805         dbus_message_iter_get_basic(iter, &sta_mac);
2806         if (sta_mac == NULL)
2807                 return;
2808
2809         SUPPLICANT_DBG("New station %s connected", sta_mac);
2810         callbacks_pointer->add_station(sta_mac);
2811 }
2812
2813 static void signal_station_disconnected(const char *path, DBusMessageIter *iter)
2814 {
2815         GSupplicantInterface *interface;
2816         const char *sta_mac = NULL;
2817
2818         SUPPLICANT_DBG("path %s %s", path, SUPPLICANT_PATH);
2819
2820         if (callbacks_pointer->remove_station == NULL)
2821                 return;
2822
2823         if (g_strcmp0(path, "/") == 0)
2824                 return;
2825
2826         interface = g_hash_table_lookup(interface_table, path);
2827         if (interface == NULL)
2828                 return;
2829
2830         dbus_message_iter_get_basic(iter, &sta_mac);
2831         if (sta_mac == NULL)
2832                 return;
2833
2834         SUPPLICANT_DBG("Station %s disconnected", sta_mac);
2835         callbacks_pointer->remove_station(sta_mac);
2836 }
2837
2838 static void create_peer_identifier(GSupplicantPeer *peer)
2839 {
2840         const unsigned char test[ETH_ALEN] = {};
2841
2842         if (!peer)
2843                 return;
2844
2845         if (!memcmp(peer->device_address, test, ETH_ALEN)) {
2846                 peer->identifier = g_strdup(peer->name);
2847                 return;
2848         }
2849
2850         peer->identifier = g_malloc0(19);
2851         snprintf(peer->identifier, 19, "%02x%02x%02x%02x%02x%02x",
2852                                                 peer->device_address[0],
2853                                                 peer->device_address[1],
2854                                                 peer->device_address[2],
2855                                                 peer->device_address[3],
2856                                                 peer->device_address[4],
2857                                                 peer->device_address[5]);
2858 }
2859
2860 struct peer_property_data {
2861         GSupplicantPeer *peer;
2862         GSList *old_groups;
2863         bool groups_changed;
2864         bool services_changed;
2865 };
2866
2867 static void peer_groups_relation(DBusMessageIter *iter, void *user_data)
2868 {
2869         struct peer_property_data *data = user_data;
2870         GSupplicantPeer *peer = data->peer;
2871         GSupplicantGroup *group;
2872         const char *str = NULL;
2873         GSList *elem;
2874
2875         dbus_message_iter_get_basic(iter, &str);
2876         if (!str)
2877                 return;
2878
2879         group = g_hash_table_lookup(group_mapping, str);
2880         if (!group)
2881                 return;
2882
2883         elem = g_slist_find_custom(data->old_groups, str, g_str_equal);
2884         if (elem) {
2885                 data->old_groups = g_slist_remove_link(data->old_groups, elem);
2886                 peer->groups = g_slist_concat(elem, peer->groups);
2887         } else {
2888                 peer->groups = g_slist_prepend(peer->groups, g_strdup(str));
2889                 data->groups_changed = true;
2890         }
2891 }
2892
2893 static void peer_property(const char *key, DBusMessageIter *iter,
2894                                                         void *user_data)
2895 {
2896         GSupplicantPeer *pending_peer;
2897         struct peer_property_data *data = user_data;
2898         GSupplicantPeer *peer = data->peer;
2899
2900         SUPPLICANT_DBG("key: %s", key);
2901
2902         if (!peer->interface)
2903                 return;
2904
2905         if (!key) {
2906                 if (peer->name) {
2907                         create_peer_identifier(peer);
2908                         callback_peer_found(peer);
2909                         pending_peer = g_hash_table_lookup(
2910                                         pending_peer_connection, peer->path);
2911
2912                         if (pending_peer && pending_peer == peer) {
2913                                 callback_peer_request(peer);
2914                                 g_hash_table_remove(pending_peer_connection,
2915                                                 peer->path);
2916                         }
2917
2918                         dbus_free(data);
2919                 }
2920
2921                 return;
2922         }
2923
2924         if (g_strcmp0(key, "DeviceAddress") == 0) {
2925                 unsigned char *dev_addr;
2926                 DBusMessageIter array;
2927                 int len;
2928
2929                 dbus_message_iter_recurse(iter, &array);
2930                 dbus_message_iter_get_fixed_array(&array, &dev_addr, &len);
2931
2932                 if (len == ETH_ALEN)
2933                         memcpy(peer->device_address, dev_addr, len);
2934         } else if (g_strcmp0(key, "DeviceName") == 0) {
2935                 const char *str = NULL;
2936
2937                 dbus_message_iter_get_basic(iter, &str);
2938                 if (str)
2939                         peer->name = g_strdup(str);
2940         } else if (g_strcmp0(key, "config_method") == 0) {
2941                 uint16_t wps_config;
2942
2943                 dbus_message_iter_get_basic(iter, &wps_config);
2944
2945                 if (wps_config & G_SUPPLICANT_WPS_CONFIG_PBC)
2946                         peer->wps_capabilities |= G_SUPPLICANT_WPS_PBC;
2947                 if (wps_config & ~G_SUPPLICANT_WPS_CONFIG_PBC)
2948                         peer->wps_capabilities |= G_SUPPLICANT_WPS_PIN;
2949         } else if (g_strcmp0(key, "Groups") == 0) {
2950                 data->old_groups = peer->groups;
2951                 peer->groups = NULL;
2952
2953                 supplicant_dbus_array_foreach(iter,
2954                                                 peer_groups_relation, data);
2955                 if (g_slist_length(data->old_groups) > 0) {
2956                         g_slist_free_full(data->old_groups, g_free);
2957                         data->groups_changed = true;
2958                 }
2959         } else if (g_strcmp0(key, "IEs") == 0) {
2960                 DBusMessageIter array;
2961                 unsigned char *ie;
2962                 int ie_len;
2963
2964                 dbus_message_iter_recurse(iter, &array);
2965                 dbus_message_iter_get_fixed_array(&array, &ie, &ie_len);
2966
2967                 if (!ie || ie_len < 2)
2968                         return;
2969
2970                 if (peer->widi_ies) {
2971                         if (memcmp(peer->widi_ies, ie, ie_len) == 0)
2972                                 return;
2973
2974                         g_free(peer->widi_ies);
2975                         peer->widi_ies_length = 0;
2976                 }
2977
2978                 peer->widi_ies = g_malloc0(ie_len * sizeof(unsigned char));
2979
2980                 memcpy(peer->widi_ies, ie, ie_len);
2981                 peer->widi_ies_length = ie_len;
2982                 data->services_changed = true;
2983         }
2984 }
2985
2986 static void signal_peer_found(const char *path, DBusMessageIter *iter)
2987 {
2988         struct peer_property_data *property_data;
2989         GSupplicantInterface *interface;
2990         const char *obj_path = NULL;
2991         GSupplicantPeer *peer;
2992
2993         SUPPLICANT_DBG("");
2994
2995         interface = g_hash_table_lookup(interface_table, path);
2996         if (!interface)
2997                 return;
2998
2999         dbus_message_iter_get_basic(iter, &obj_path);
3000         if (!obj_path || g_strcmp0(obj_path, "/") == 0)
3001                 return;
3002
3003         peer = g_hash_table_lookup(interface->peer_table, obj_path);
3004         if (peer)
3005                 return;
3006
3007         peer = g_try_new0(GSupplicantPeer, 1);
3008         if (!peer)
3009                 return;
3010
3011         peer->interface = interface;
3012         peer->path = g_strdup(obj_path);
3013         g_hash_table_insert(interface->peer_table, peer->path, peer);
3014         g_hash_table_replace(peer_mapping, peer->path, interface);
3015
3016         property_data = dbus_malloc0(sizeof(struct peer_property_data));
3017         property_data->peer = peer;
3018
3019         dbus_message_iter_next(iter);
3020         if (dbus_message_iter_get_arg_type(iter) != DBUS_TYPE_INVALID) {
3021                 supplicant_dbus_property_foreach(iter, peer_property,
3022                                                         property_data);
3023                 peer_property(NULL, NULL, property_data);
3024                 return;
3025         }
3026
3027         supplicant_dbus_property_get_all(obj_path,
3028                                         SUPPLICANT_INTERFACE ".Peer",
3029                                         peer_property, property_data, NULL);
3030 }
3031
3032 static void signal_peer_lost(const char *path, DBusMessageIter *iter)
3033 {
3034         GSupplicantInterface *interface;
3035         const char *obj_path = NULL;
3036         GSupplicantPeer *peer;
3037
3038         SUPPLICANT_DBG("");
3039
3040         interface = g_hash_table_lookup(interface_table, path);
3041         if (!interface)
3042                 return;
3043
3044         dbus_message_iter_get_basic(iter, &obj_path);
3045         if (!obj_path || g_strcmp0(obj_path, "/") == 0)
3046                 return;
3047
3048         peer = g_hash_table_lookup(interface->peer_table, obj_path);
3049         if (!peer)
3050                 return;
3051
3052         g_hash_table_remove(interface->peer_table, obj_path);
3053 }
3054
3055 static void signal_peer_changed(const char *path, DBusMessageIter *iter)
3056 {
3057         struct peer_property_data *property_data;
3058         GSupplicantInterface *interface;
3059         GSupplicantPeer *peer;
3060
3061         SUPPLICANT_DBG("");
3062
3063         interface = g_hash_table_lookup(peer_mapping, path);
3064         if (!interface)
3065                 return;
3066
3067         peer = g_hash_table_lookup(interface->peer_table, path);
3068         if (!peer) {
3069                 g_hash_table_remove(peer_mapping, path);
3070                 return;
3071         }
3072
3073         property_data = dbus_malloc0(sizeof(struct peer_property_data));
3074         property_data->peer = peer;
3075
3076         supplicant_dbus_property_foreach(iter, peer_property, property_data);
3077         if (property_data->services_changed)
3078                 callback_peer_changed(peer,
3079                                         G_SUPPLICANT_PEER_SERVICES_CHANGED);
3080
3081         if (property_data->groups_changed)
3082                 callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_CHANGED);
3083
3084         dbus_free(property_data);
3085
3086         if (!g_supplicant_peer_is_in_a_group(peer))
3087                 peer->connection_requested = false;
3088 }
3089
3090 struct group_sig_data {
3091         const char *peer_obj_path;
3092         unsigned char iface_address[ETH_ALEN];
3093         const char *interface_obj_path;
3094         const char *group_obj_path;
3095         int role;
3096 };
3097
3098 static void group_sig_property(const char *key, DBusMessageIter *iter,
3099                                                         void *user_data)
3100 {
3101         struct group_sig_data *data = user_data;
3102
3103         if (!key)
3104                 return;
3105
3106         if (g_strcmp0(key, "peer_interface_addr") == 0) {
3107                 unsigned char *dev_addr;
3108                 DBusMessageIter array;
3109                 int len;
3110
3111                 dbus_message_iter_recurse(iter, &array);
3112                 dbus_message_iter_get_fixed_array(&array, &dev_addr, &len);
3113
3114                 if (len == ETH_ALEN)
3115                         memcpy(data->iface_address, dev_addr, len);
3116         } else if (g_strcmp0(key, "role") == 0) {
3117                 const char *str = NULL;
3118
3119                 dbus_message_iter_get_basic(iter, &str);
3120                 if (g_strcmp0(str, "GO") == 0)
3121                         data->role = G_SUPPLICANT_GROUP_ROLE_GO;
3122                 else
3123                         data->role = G_SUPPLICANT_GROUP_ROLE_CLIENT;
3124         } else if (g_strcmp0(key, "peer_object") == 0)
3125                 dbus_message_iter_get_basic(iter, &data->peer_obj_path);
3126         else if (g_strcmp0(key, "interface_object") == 0)
3127                 dbus_message_iter_get_basic(iter, &data->interface_obj_path);
3128         else if (g_strcmp0(key, "group_object") == 0)
3129                 dbus_message_iter_get_basic(iter, &data->group_obj_path);
3130
3131 }
3132
3133 static void signal_group_success(const char *path, DBusMessageIter *iter)
3134 {
3135         GSupplicantInterface *interface;
3136         struct group_sig_data data = {};
3137         GSupplicantPeer *peer;
3138
3139         SUPPLICANT_DBG("");
3140
3141         interface = g_hash_table_lookup(interface_table, path);
3142         if (!interface)
3143                 return;
3144
3145         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3146         if (!data.peer_obj_path)
3147                 return;
3148
3149         peer = g_hash_table_lookup(interface->peer_table, data.peer_obj_path);
3150         if (!peer)
3151                 return;
3152
3153         memcpy(peer->iface_address, data.iface_address, ETH_ALEN);
3154         interface->pending_peer_path = peer->path;
3155 }
3156
3157 static void signal_group_failure(const char *path, DBusMessageIter *iter)
3158 {
3159         GSupplicantInterface *interface;
3160         struct group_sig_data data = {};
3161         GSupplicantPeer *peer;
3162
3163         SUPPLICANT_DBG("");
3164
3165         interface = g_hash_table_lookup(interface_table, path);
3166         if (!interface)
3167                 return;
3168
3169         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3170         if (!data.peer_obj_path)
3171                 return;
3172
3173         peer = g_hash_table_lookup(interface->peer_table, data.peer_obj_path);
3174         if (!peer)
3175                 return;
3176
3177         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_FAILED);
3178         peer->connection_requested = false;
3179 }
3180
3181 static void signal_group_started(const char *path, DBusMessageIter *iter)
3182 {
3183         GSupplicantInterface *interface, *g_interface;
3184         struct group_sig_data data = {};
3185         GSupplicantGroup *group;
3186         GSupplicantPeer *peer;
3187
3188         SUPPLICANT_DBG("");
3189
3190         interface = g_hash_table_lookup(interface_table, path);
3191         if (!interface)
3192                 return;
3193
3194         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3195         if (!data.interface_obj_path || !data.group_obj_path)
3196                 return;
3197
3198         peer = g_hash_table_lookup(interface->peer_table,
3199                                                 interface->pending_peer_path);
3200         interface->pending_peer_path = NULL;
3201         if (!peer)
3202                 return;
3203
3204         g_interface = g_hash_table_lookup(interface_table,
3205                                                 data.interface_obj_path);
3206         if (!g_interface)
3207                 return;
3208
3209         group = g_hash_table_lookup(interface->group_table,
3210                                                 data.group_obj_path);
3211         if (group)
3212                 return;
3213
3214         group = g_try_new0(GSupplicantGroup, 1);
3215         if (!group)
3216                 return;
3217
3218         group->interface = g_interface;
3219         group->orig_interface = interface;
3220         group->path = g_strdup(data.group_obj_path);
3221         group->role = data.role;
3222
3223         g_hash_table_insert(interface->group_table, group->path, group);
3224         g_hash_table_replace(group_mapping, group->path, group);
3225
3226         peer->current_group_iface = g_interface;
3227         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_STARTED);
3228 }
3229
3230 static void remove_peer_group_interface(GHashTable *group_table,
3231                                 const char* path)
3232 {
3233         GSupplicantGroup *group;
3234         GHashTableIter iter;
3235         gpointer value, key;
3236
3237         if (!group_table)
3238                 return;
3239
3240         group = g_hash_table_lookup(group_table, path);
3241
3242         if (!group || !group->orig_interface)
3243                 return;
3244
3245         g_hash_table_iter_init(&iter, group->orig_interface->peer_table);
3246
3247         while (g_hash_table_iter_next(&iter, &key, &value)) {
3248                 GSupplicantPeer *peer = value;
3249
3250                 if (peer->current_group_iface == group->interface)
3251                         peer->current_group_iface = NULL;
3252         }
3253 }
3254
3255 static void signal_group_finished(const char *path, DBusMessageIter *iter)
3256 {
3257         GSupplicantInterface *interface;
3258         struct group_sig_data data = {};
3259
3260         SUPPLICANT_DBG("");
3261
3262         interface = g_hash_table_lookup(interface_table, path);
3263         if (!interface)
3264                 return;
3265
3266         supplicant_dbus_property_foreach(iter, group_sig_property, &data);
3267         if (!data.interface_obj_path || !data.group_obj_path)
3268                 return;
3269
3270         remove_peer_group_interface(interface->group_table, data.group_obj_path);
3271
3272         g_hash_table_remove(group_mapping, data.group_obj_path);
3273
3274         g_hash_table_remove(interface->group_table, data.group_obj_path);
3275 }
3276
3277 static void signal_group_request(const char *path, DBusMessageIter *iter)
3278 {
3279         GSupplicantInterface *interface;
3280         GSupplicantPeer *peer;
3281         const char *obj_path;
3282
3283         SUPPLICANT_DBG("");
3284
3285         interface = g_hash_table_lookup(interface_table, path);
3286         if (!interface)
3287                 return;
3288
3289         dbus_message_iter_get_basic(iter, &obj_path);
3290         if (!obj_path || !g_strcmp0(obj_path, "/"))
3291                 return;
3292
3293         peer = g_hash_table_lookup(interface->peer_table, obj_path);
3294         if (!peer)
3295                 return;
3296
3297         /*
3298          * Peer has been previously found and property set,
3299          * otherwise, defer connection to when peer property
3300          * is set.
3301          */
3302         if (peer->identifier)
3303                 callback_peer_request(peer);
3304         else
3305                 g_hash_table_replace(pending_peer_connection, peer->path, peer);
3306 }
3307
3308 static void signal_group_peer_joined(const char *path, DBusMessageIter *iter)
3309 {
3310         const char *peer_path = NULL;
3311         GSupplicantInterface *interface;
3312         GSupplicantGroup *group;
3313         GSupplicantPeer *peer;
3314
3315         SUPPLICANT_DBG("");
3316
3317         group = g_hash_table_lookup(group_mapping, path);
3318         if (!group)
3319                 return;
3320
3321         dbus_message_iter_get_basic(iter, &peer_path);
3322         if (!peer_path)
3323                 return;
3324
3325         interface = g_hash_table_lookup(peer_mapping, peer_path);
3326         if (!interface)
3327                 return;
3328
3329         peer = g_hash_table_lookup(interface->peer_table, peer_path);
3330         if (!peer)
3331                 return;
3332
3333         group->members = g_slist_prepend(group->members, g_strdup(peer_path));
3334
3335         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_JOINED);
3336 }
3337
3338 static void signal_group_peer_disconnected(const char *path, DBusMessageIter *iter)
3339 {
3340         const char *peer_path = NULL;
3341         GSupplicantInterface *interface;
3342         GSupplicantGroup *group;
3343         GSupplicantPeer *peer;
3344         GSList *elem;
3345
3346         SUPPLICANT_DBG("");
3347
3348         group = g_hash_table_lookup(group_mapping, path);
3349         if (!group)
3350                 return;
3351
3352         dbus_message_iter_get_basic(iter, &peer_path);
3353         if (!peer_path)
3354                 return;
3355
3356         for (elem = group->members; elem; elem = elem->next) {
3357                 if (!g_strcmp0(elem->data, peer_path))
3358                         break;
3359         }
3360
3361         if (!elem)
3362                 return;
3363
3364         g_free(elem->data);
3365         group->members = g_slist_delete_link(group->members, elem);
3366
3367         interface = g_hash_table_lookup(peer_mapping, peer_path);
3368         if (!interface)
3369                 return;
3370
3371         peer = g_hash_table_lookup(interface->peer_table, peer_path);
3372         if (!peer)
3373                 return;
3374
3375         callback_peer_changed(peer, G_SUPPLICANT_PEER_GROUP_DISCONNECTED);
3376         peer->connection_requested = false;
3377 }
3378
3379 static struct {
3380         const char *interface;
3381         const char *member;
3382         void (*function) (const char *path, DBusMessageIter *iter);
3383 } signal_map[] = {
3384         { DBUS_INTERFACE_DBUS,  "NameOwnerChanged",  signal_name_owner_changed },
3385
3386         { SUPPLICANT_INTERFACE, "PropertiesChanged", signal_properties_changed },
3387         { SUPPLICANT_INTERFACE, "InterfaceAdded",    signal_interface_added    },
3388         { SUPPLICANT_INTERFACE, "InterfaceCreated",  signal_interface_added    },
3389         { SUPPLICANT_INTERFACE, "InterfaceRemoved",  signal_interface_removed  },
3390
3391         { SUPPLICANT_INTERFACE ".Interface", "PropertiesChanged", signal_interface_changed },
3392         { SUPPLICANT_INTERFACE ".Interface", "ScanDone",          signal_scan_done         },
3393         { SUPPLICANT_INTERFACE ".Interface", "BSSAdded",          signal_bss_added         },
3394         { SUPPLICANT_INTERFACE ".Interface", "BSSRemoved",        signal_bss_removed       },
3395         { SUPPLICANT_INTERFACE ".Interface", "NetworkAdded",      signal_network_added     },
3396         { SUPPLICANT_INTERFACE ".Interface", "NetworkRemoved",    signal_network_removed   },
3397
3398         { SUPPLICANT_INTERFACE ".BSS", "PropertiesChanged", signal_bss_changed   },
3399
3400         { SUPPLICANT_INTERFACE ".Interface.WPS", "Credentials", signal_wps_credentials },
3401         { SUPPLICANT_INTERFACE ".Interface.WPS", "Event",       signal_wps_event       },
3402 #if defined TIZEN_EXT
3403         { "org.tizen.system.deviced.PowerOff", "ChangeState", signal_power_off },
3404 #endif
3405
3406         { SUPPLICANT_INTERFACE".Interface", "StaAuthorized", signal_station_connected      },
3407         { SUPPLICANT_INTERFACE".Interface", "StaDeauthorized", signal_station_disconnected },
3408
3409         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeviceFound", signal_peer_found },
3410         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeviceLost",  signal_peer_lost  },
3411
3412         { SUPPLICANT_INTERFACE ".Peer", "PropertiesChanged", signal_peer_changed },
3413
3414         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationSuccess", signal_group_success },
3415         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationFailure", signal_group_failure },
3416         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GroupStarted", signal_group_started },
3417         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GroupFinished", signal_group_finished },
3418         { SUPPLICANT_INTERFACE ".Interface.P2PDevice", "GONegotiationRequest", signal_group_request },
3419
3420         { SUPPLICANT_INTERFACE ".Group", "PeerJoined", signal_group_peer_joined },
3421         { SUPPLICANT_INTERFACE ".Group", "PeerDisconnected", signal_group_peer_disconnected },
3422
3423         { }
3424 };
3425
3426 static DBusHandlerResult g_supplicant_filter(DBusConnection *conn,
3427                                         DBusMessage *message, void *data)
3428 {
3429         DBusMessageIter iter;
3430         const char *path;
3431         int i;
3432
3433         path = dbus_message_get_path(message);
3434         if (!path)
3435                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3436
3437         if (!dbus_message_iter_init(message, &iter))
3438                 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3439
3440         for (i = 0; signal_map[i].interface; i++) {
3441                 if (!dbus_message_has_interface(message, signal_map[i].interface))
3442                         continue;
3443
3444                 if (!dbus_message_has_member(message, signal_map[i].member))
3445                         continue;
3446
3447                 signal_map[i].function(path, &iter);
3448                 break;
3449         }
3450
3451         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
3452 }
3453
3454 void g_supplicant_interface_cancel(GSupplicantInterface *interface)
3455 {
3456         SUPPLICANT_DBG("Cancelling any pending DBus calls");
3457         supplicant_dbus_method_call_cancel_all(interface);
3458         supplicant_dbus_property_call_cancel_all(interface);
3459 }
3460
3461 struct supplicant_regdom {
3462         GSupplicantCountryCallback callback;
3463         const char *alpha2;
3464         const void *user_data;
3465 };
3466
3467 static void country_result(const char *error,
3468                                 DBusMessageIter *iter, void *user_data)
3469 {
3470         struct supplicant_regdom *regdom = user_data;
3471         int result = 0;
3472
3473         SUPPLICANT_DBG("Country setting result");
3474
3475         if (!user_data)
3476                 return;
3477
3478         if (error) {
3479                 SUPPLICANT_DBG("Country setting failure %s", error);
3480                 result = -EINVAL;
3481         }
3482
3483         if (regdom->callback)
3484                 regdom->callback(result, regdom->alpha2,
3485                                         (void *) regdom->user_data);
3486
3487         g_free(regdom);
3488 }
3489
3490 static void country_params(DBusMessageIter *iter, void *user_data)
3491 {
3492         struct supplicant_regdom *regdom = user_data;
3493
3494         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING,
3495                                                         &regdom->alpha2);
3496 }
3497
3498 int g_supplicant_set_country(const char *alpha2,
3499                                 GSupplicantCountryCallback callback,
3500                                         const void *user_data)
3501 {
3502         struct supplicant_regdom *regdom;
3503
3504         SUPPLICANT_DBG("Country setting %s", alpha2);
3505
3506         if (!system_available)
3507                 return -EFAULT;
3508
3509         regdom = dbus_malloc0(sizeof(*regdom));
3510         if (!regdom)
3511                 return -ENOMEM;
3512
3513         regdom->callback = callback;
3514         regdom->alpha2 = alpha2;
3515         regdom->user_data = user_data;
3516
3517         return supplicant_dbus_property_set(SUPPLICANT_PATH, SUPPLICANT_INTERFACE,
3518                                         "Country", DBUS_TYPE_STRING_AS_STRING,
3519                                         country_params, country_result,
3520                                         regdom, NULL);
3521 }
3522
3523 int g_supplicant_interface_set_country(GSupplicantInterface *interface,
3524                                         GSupplicantCountryCallback callback,
3525                                                         const char *alpha2,
3526                                                         void *user_data)
3527 {
3528         struct supplicant_regdom *regdom;
3529
3530         regdom = dbus_malloc0(sizeof(*regdom));
3531         if (!regdom)
3532                 return -ENOMEM;
3533
3534         regdom->callback = callback;
3535         regdom->alpha2 = alpha2;
3536         regdom->user_data = user_data;
3537
3538         return supplicant_dbus_property_set(interface->path,
3539                                 SUPPLICANT_INTERFACE ".Interface",
3540                                 "Country", DBUS_TYPE_STRING_AS_STRING,
3541                                 country_params, country_result,
3542                                         regdom, NULL);
3543 }
3544
3545 bool g_supplicant_interface_has_p2p(GSupplicantInterface *interface)
3546 {
3547         if (!interface)
3548                 return false;
3549
3550         return interface->p2p_support;
3551 }
3552
3553 struct supplicant_p2p_dev_config {
3554         char *device_name;
3555         char *dev_type;
3556 };
3557
3558 static void p2p_device_config_result(const char *error,
3559                                         DBusMessageIter *iter, void *user_data)
3560 {
3561         struct supplicant_p2p_dev_config *config = user_data;
3562
3563         if (error)
3564                 SUPPLICANT_DBG("Unable to set P2P Device configuration: %s",
3565                                                                         error);
3566
3567         g_free(config->device_name);
3568         g_free(config->dev_type);
3569         dbus_free(config);
3570 }
3571
3572 static int dev_type_str2bin(const char *type, unsigned char dev_type[8])
3573 {
3574         int length, pos, end;
3575         char b[3] = {};
3576         char *e = NULL;
3577
3578         end = strlen(type);
3579         for (length = pos = 0; type[pos] != '\0' && length < 8; length++) {
3580                 if (pos+2 > end)
3581                         return 0;
3582
3583                 b[0] = type[pos];
3584                 b[1] = type[pos+1];
3585
3586                 dev_type[length] = strtol(b, &e, 16);
3587                 if (e && *e != '\0')
3588                         return 0;
3589
3590                 pos += 2;
3591         }
3592
3593         return 8;
3594 }
3595
3596 static void p2p_device_config_params(DBusMessageIter *iter, void *user_data)
3597 {
3598         struct supplicant_p2p_dev_config *config = user_data;
3599         DBusMessageIter dict;
3600
3601         supplicant_dbus_dict_open(iter, &dict);
3602
3603         supplicant_dbus_dict_append_basic(&dict, "DeviceName",
3604                                 DBUS_TYPE_STRING, &config->device_name);
3605
3606         if (config->dev_type) {
3607                 unsigned char dev_type[8] = {}, *type;
3608                 int len;
3609
3610                 len = dev_type_str2bin(config->dev_type, dev_type);
3611                 if (len) {
3612                         type = dev_type;
3613                         supplicant_dbus_dict_append_fixed_array(&dict,
3614                                         "PrimaryDeviceType",
3615                                         DBUS_TYPE_BYTE, &type, len);
3616                 }
3617         }
3618
3619         supplicant_dbus_dict_close(iter, &dict);
3620 }
3621
3622 int g_supplicant_interface_set_p2p_device_config(GSupplicantInterface *interface,
3623                                         const char *device_name,
3624                                         const char *primary_dev_type)
3625 {
3626         struct supplicant_p2p_dev_config *config;
3627         int ret;
3628
3629         SUPPLICANT_DBG("P2P Device settings %s/%s",
3630                                         device_name, primary_dev_type);
3631
3632         config = dbus_malloc0(sizeof(*config));
3633         if (!config)
3634                 return -ENOMEM;
3635
3636         config->device_name = g_strdup(device_name);
3637         config->dev_type = g_strdup(primary_dev_type);
3638
3639         ret = supplicant_dbus_property_set(interface->path,
3640                                 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
3641                                 "P2PDeviceConfig",
3642                                 DBUS_TYPE_ARRAY_AS_STRING
3643                                 DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
3644                                 DBUS_TYPE_STRING_AS_STRING
3645                                 DBUS_TYPE_VARIANT_AS_STRING
3646                                 DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
3647                                 p2p_device_config_params,
3648                                 p2p_device_config_result, config, NULL);
3649         if (ret < 0) {
3650                 g_free(config->device_name);
3651                 g_free(config->dev_type);
3652                 dbus_free(config);
3653                 SUPPLICANT_DBG("Unable to set P2P Device configuration");
3654         }
3655
3656         return ret;
3657 }
3658
3659 static gboolean peer_lookup_by_identifier(gpointer key, gpointer value,
3660                                                         gpointer user_data)
3661 {
3662         const GSupplicantPeer *peer = value;
3663         const char *identifier = user_data;
3664
3665         if (!g_strcmp0(identifier, peer->identifier))
3666                 return TRUE;
3667
3668         return FALSE;
3669 }
3670
3671 GSupplicantPeer *g_supplicant_interface_peer_lookup(GSupplicantInterface *interface,
3672                                                         const char *identifier)
3673 {
3674         GSupplicantPeer *peer;
3675
3676         peer = g_hash_table_find(interface->peer_table,
3677                                         peer_lookup_by_identifier,
3678                                         (void *) identifier);
3679         return peer;
3680 }
3681
3682 struct interface_data {
3683         GSupplicantInterface *interface;
3684         char *path; /* Interface path cannot be taken from interface (above) as
3685                      * it might have been freed already.
3686                      */
3687         GSupplicantInterfaceCallback callback;
3688         void *user_data;
3689 };
3690
3691 struct interface_create_data {
3692         char *ifname;
3693         char *driver;
3694         char *bridge;
3695         GSupplicantInterface *interface;
3696         GSupplicantInterfaceCallback callback;
3697         void *user_data;
3698 };
3699
3700 struct interface_connect_data {
3701         GSupplicantInterface *interface;
3702         char *path;
3703         GSupplicantInterfaceCallback callback;
3704         union {
3705                 GSupplicantSSID *ssid;
3706                 GSupplicantPeerParams *peer;
3707         };
3708         void *user_data;
3709 };
3710
3711 struct interface_scan_data {
3712         GSupplicantInterface *interface;
3713         char *path;
3714         GSupplicantInterfaceCallback callback;
3715         GSupplicantScanParams *scan_params;
3716         void *user_data;
3717 };
3718
3719 static void interface_create_data_free(struct interface_create_data *data)
3720 {
3721         g_free(data->ifname);
3722         g_free(data->driver);
3723         g_free(data->bridge);
3724         dbus_free(data);
3725 }
3726
3727 static bool interface_exists(GSupplicantInterface *interface,
3728                                 const char *path)
3729 {
3730         GSupplicantInterface *tmp;
3731
3732         tmp = g_hash_table_lookup(interface_table, path);
3733         if (tmp && tmp == interface)
3734                 return true;
3735
3736         return false;
3737 }
3738
3739 static void interface_create_property(const char *key, DBusMessageIter *iter,
3740                                                         void *user_data)
3741 {
3742         struct interface_create_data *data = user_data;
3743         GSupplicantInterface *interface = data->interface;
3744
3745         if (!key) {
3746                 if (data->callback) {
3747                         data->callback(0, data->interface, data->user_data);
3748 #if !defined TIZEN_EXT
3749                         callback_p2p_support(interface);
3750 #endif
3751                 }
3752
3753                 interface_create_data_free(data);
3754         }
3755
3756         interface_property(key, iter, interface);
3757 }
3758
3759 static void interface_create_result(const char *error,
3760                                 DBusMessageIter *iter, void *user_data)
3761 {
3762         struct interface_create_data *data = user_data;
3763         const char *path = NULL;
3764         int err;
3765
3766         SUPPLICANT_DBG("");
3767
3768         if (error) {
3769                 g_warning("error %s", error);
3770                 err = -EIO;
3771                 goto done;
3772         }
3773
3774         dbus_message_iter_get_basic(iter, &path);
3775         if (!path) {
3776                 err = -EINVAL;
3777                 goto done;
3778         }
3779
3780         if (!system_available) {
3781                 err = -EFAULT;
3782                 goto done;
3783         }
3784
3785         data->interface = g_hash_table_lookup(interface_table, path);
3786         if (!data->interface) {
3787                 data->interface = interface_alloc(path);
3788                 if (!data->interface) {
3789                         err = -ENOMEM;
3790                         goto done;
3791                 }
3792         }
3793
3794         err = supplicant_dbus_property_get_all(path,
3795                                         SUPPLICANT_INTERFACE ".Interface",
3796                                         interface_create_property, data,
3797                                         NULL);
3798         if (err == 0)
3799                 return;
3800
3801 done:
3802         if (data->callback)
3803                 data->callback(err, NULL, data->user_data);
3804
3805         interface_create_data_free(data);
3806 }
3807
3808 static void interface_create_params(DBusMessageIter *iter, void *user_data)
3809 {
3810         struct interface_create_data *data = user_data;
3811         DBusMessageIter dict;
3812
3813         SUPPLICANT_DBG("");
3814
3815         supplicant_dbus_dict_open(iter, &dict);
3816
3817         supplicant_dbus_dict_append_basic(&dict, "Ifname",
3818                                         DBUS_TYPE_STRING, &data->ifname);
3819
3820         if (data->driver)
3821                 supplicant_dbus_dict_append_basic(&dict, "Driver",
3822                                         DBUS_TYPE_STRING, &data->driver);
3823
3824         if (data->bridge)
3825                 supplicant_dbus_dict_append_basic(&dict, "BridgeIfname",
3826                                         DBUS_TYPE_STRING, &data->bridge);
3827
3828         supplicant_dbus_dict_close(iter, &dict);
3829 }
3830
3831 static void interface_get_result(const char *error,
3832                                 DBusMessageIter *iter, void *user_data)
3833 {
3834         struct interface_create_data *data = user_data;
3835         GSupplicantInterface *interface;
3836         const char *path = NULL;
3837         int err;
3838
3839         SUPPLICANT_DBG("");
3840
3841         if (error) {
3842                 SUPPLICANT_DBG("Interface not created yet");
3843                 goto create;
3844         }
3845
3846         dbus_message_iter_get_basic(iter, &path);
3847         if (!path) {
3848                 err = -EINVAL;
3849                 goto done;
3850         }
3851
3852         interface = g_hash_table_lookup(interface_table, path);
3853         if (!interface) {
3854                 err = -ENOENT;
3855                 goto done;
3856         }
3857
3858         if (data->callback) {
3859                 data->callback(0, interface, data->user_data);
3860 #if !defined TIZEN_EXT
3861                 callback_p2p_support(interface);
3862 #endif
3863         }
3864
3865         interface_create_data_free(data);
3866
3867         return;
3868
3869 create:
3870         if (!system_available) {
3871                 err = -EFAULT;
3872                 goto done;
3873         }
3874
3875         SUPPLICANT_DBG("Creating interface");
3876
3877         err = supplicant_dbus_method_call(SUPPLICANT_PATH,
3878                                                 SUPPLICANT_INTERFACE,
3879                                                 "CreateInterface",
3880                                                 interface_create_params,
3881                                                 interface_create_result, data,
3882                                                 NULL);
3883         if (err == 0)
3884                 return;
3885
3886 done:
3887         if (data->callback)
3888                 data->callback(err, NULL, data->user_data);
3889
3890         interface_create_data_free(data);
3891 }
3892
3893 static void interface_get_params(DBusMessageIter *iter, void *user_data)
3894 {
3895         struct interface_create_data *data = user_data;
3896
3897         SUPPLICANT_DBG("");
3898
3899         dbus_message_iter_append_basic(iter, DBUS_TYPE_STRING, &data->ifname);
3900 }
3901
3902 int g_supplicant_interface_create(const char *ifname, const char *driver,
3903                                         const char *bridge,
3904                                         GSupplicantInterfaceCallback callback,
3905                                                         void *user_data)
3906 {
3907         struct interface_create_data *data;
3908         int ret;
3909
3910         SUPPLICANT_DBG("ifname %s", ifname);
3911
3912         if (!ifname)
3913                 return -EINVAL;
3914
3915         if (!system_available)
3916                 return -EFAULT;
3917
3918         data = dbus_malloc0(sizeof(*data));
3919         if (!data)
3920                 return -ENOMEM;
3921
3922         data->ifname = g_strdup(ifname);
3923         data->driver = g_strdup(driver);
3924         data->bridge = g_strdup(bridge);
3925         data->callback = callback;
3926         data->user_data = user_data;
3927
3928         ret = supplicant_dbus_method_call(SUPPLICANT_PATH,
3929                                                 SUPPLICANT_INTERFACE,
3930                                                 "GetInterface",
3931                                                 interface_get_params,
3932                                                 interface_get_result, data,
3933                                                 NULL);
3934         if (ret < 0)
3935                 interface_create_data_free(data);
3936
3937         return ret;
3938 }
3939
3940 static void interface_remove_result(const char *error,
3941                                 DBusMessageIter *iter, void *user_data)
3942 {
3943         struct interface_data *data = user_data;
3944         int err;
3945
3946         if (error) {
3947                 err = -EIO;
3948                 goto done;
3949         }
3950
3951         if (!system_available) {
3952                 err = -EFAULT;
3953                 goto done;
3954         }
3955
3956         /*
3957          * The gsupplicant interface is already freed by the InterfaceRemoved
3958          * signal callback. Simply invoke the interface_data callback.
3959          */
3960         err = 0;
3961
3962 done:
3963         g_free(data->path);
3964
3965         if (data->callback)
3966                 data->callback(err, NULL, data->user_data);
3967
3968         dbus_free(data);
3969 }
3970
3971
3972 static void interface_remove_params(DBusMessageIter *iter, void *user_data)
3973 {
3974         struct interface_data *data = user_data;
3975
3976         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
3977                                                         &data->interface->path);
3978 }
3979
3980
3981 int g_supplicant_interface_remove(GSupplicantInterface *interface,
3982                         GSupplicantInterfaceCallback callback,
3983                                                         void *user_data)
3984 {
3985         struct interface_data *data;
3986         int ret;
3987
3988         if (!interface)
3989                 return -EINVAL;
3990
3991         if (!system_available)
3992                 return -EFAULT;
3993
3994         g_supplicant_interface_cancel(interface);
3995
3996         data = dbus_malloc0(sizeof(*data));
3997         if (!data)
3998                 return -ENOMEM;
3999
4000         data->interface = interface;
4001         data->path = g_strdup(interface->path);
4002         data->callback = callback;
4003         data->user_data = user_data;
4004
4005         ret = supplicant_dbus_method_call(SUPPLICANT_PATH,
4006                                                 SUPPLICANT_INTERFACE,
4007                                                 "RemoveInterface",
4008                                                 interface_remove_params,
4009                                                 interface_remove_result, data,
4010                                                 NULL);
4011         if (ret < 0) {
4012                 g_free(data->path);
4013                 dbus_free(data);
4014         }
4015         return ret;
4016 }
4017
4018 static void interface_scan_result(const char *error,
4019                                 DBusMessageIter *iter, void *user_data)
4020 {
4021         struct interface_scan_data *data = user_data;
4022         int err = 0;
4023
4024         if (error) {
4025                 SUPPLICANT_DBG("error %s", error);
4026                 err = -EIO;
4027         }
4028
4029         /* A non ready interface cannot send/receive anything */
4030         if (interface_exists(data->interface, data->path)) {
4031                 if (!data->interface->ready)
4032                         err = -ENOLINK;
4033         }
4034
4035         g_free(data->path);
4036
4037         if (err != 0) {
4038                 if (data->callback)
4039                         data->callback(err, data->interface, data->user_data);
4040         } else {
4041                 data->interface->scan_callback = data->callback;
4042                 data->interface->scan_data = data->user_data;
4043         }
4044
4045         if (data->scan_params)
4046                 g_supplicant_free_scan_params(data->scan_params);
4047
4048         dbus_free(data);
4049 }
4050
4051 static void add_scan_frequency(DBusMessageIter *iter, unsigned int freq)
4052 {
4053         DBusMessageIter data;
4054         unsigned int width = 0; /* Not used by wpa_supplicant atm */
4055
4056         dbus_message_iter_open_container(iter, DBUS_TYPE_STRUCT, NULL, &data);
4057
4058         dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &freq);
4059         dbus_message_iter_append_basic(&data, DBUS_TYPE_UINT32, &width);
4060
4061         dbus_message_iter_close_container(iter, &data);
4062 }
4063
4064 static void add_scan_frequencies(DBusMessageIter *iter,
4065                                                 void *user_data)
4066 {
4067         GSupplicantScanParams *scan_data = user_data;
4068         unsigned int freq;
4069         int i;
4070
4071         for (i = 0; i < scan_data->num_freqs; i++) {
4072                 freq = scan_data->freqs[i];
4073                 if (!freq)
4074                         break;
4075
4076                 add_scan_frequency(iter, freq);
4077         }
4078 }
4079
4080 static void append_ssid(DBusMessageIter *iter,
4081                         const void *ssid, unsigned int len)
4082 {
4083         DBusMessageIter array;
4084
4085         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
4086         DBUS_TYPE_BYTE_AS_STRING, &array);
4087
4088         dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
4089                                                                 &ssid, len);
4090         dbus_message_iter_close_container(iter, &array);
4091 }
4092
4093 static void append_ssids(DBusMessageIter *iter, void *user_data)
4094 {
4095         GSupplicantScanParams *scan_data = user_data;
4096         GSList *list;
4097
4098         for (list = scan_data->ssids; list; list = list->next) {
4099                 struct scan_ssid *scan_ssid = list->data;
4100
4101                 append_ssid(iter, scan_ssid->ssid, scan_ssid->ssid_len);
4102         }
4103 }
4104
4105 static void supplicant_add_scan_frequency(DBusMessageIter *dict,
4106                 supplicant_dbus_array_function function,
4107                                         void *user_data)
4108 {
4109         GSupplicantScanParams *scan_params = user_data;
4110         DBusMessageIter entry, value, array;
4111         const char *key = "Channels";
4112
4113         if (scan_params->freqs && scan_params->freqs[0] != 0) {
4114                 dbus_message_iter_open_container(dict, DBUS_TYPE_DICT_ENTRY,
4115                                                 NULL, &entry);
4116
4117                 dbus_message_iter_append_basic(&entry, DBUS_TYPE_STRING, &key);
4118
4119                 dbus_message_iter_open_container(&entry, DBUS_TYPE_VARIANT,
4120                                         DBUS_TYPE_ARRAY_AS_STRING
4121                                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
4122                                         DBUS_TYPE_UINT32_AS_STRING
4123                                         DBUS_TYPE_UINT32_AS_STRING
4124                                         DBUS_STRUCT_END_CHAR_AS_STRING,
4125                                         &value);
4126
4127                 dbus_message_iter_open_container(&value, DBUS_TYPE_ARRAY,
4128                                         DBUS_STRUCT_BEGIN_CHAR_AS_STRING
4129                                         DBUS_TYPE_UINT32_AS_STRING
4130                                         DBUS_TYPE_UINT32_AS_STRING
4131                                         DBUS_STRUCT_END_CHAR_AS_STRING,
4132                                         &array);
4133
4134                 if (function)
4135                         function(&array, user_data);
4136
4137                 dbus_message_iter_close_container(&value, &array);
4138                 dbus_message_iter_close_container(&entry, &value);
4139                 dbus_message_iter_close_container(dict, &entry);
4140         }
4141 }
4142
4143 static void interface_scan_params(DBusMessageIter *iter, void *user_data)
4144 {
4145         DBusMessageIter dict;
4146         const char *type = "passive";
4147         struct interface_scan_data *data = user_data;
4148
4149         supplicant_dbus_dict_open(iter, &dict);
4150
4151         if (data && data->scan_params) {
4152                 type = "active";
4153
4154                 supplicant_dbus_dict_append_basic(&dict, "Type",
4155                                         DBUS_TYPE_STRING, &type);
4156
4157                 supplicant_dbus_dict_append_array(&dict, "SSIDs",
4158                                                 DBUS_TYPE_STRING,
4159                                                 append_ssids,
4160                                                 data->scan_params);
4161
4162                 supplicant_add_scan_frequency(&dict, add_scan_frequencies,
4163                                                 data->scan_params);
4164         } else
4165                 supplicant_dbus_dict_append_basic(&dict, "Type",
4166                                         DBUS_TYPE_STRING, &type);
4167
4168         supplicant_dbus_dict_close(iter, &dict);
4169 }
4170
4171 static int interface_ready_to_scan(GSupplicantInterface *interface)
4172 {
4173         if (!interface)
4174                 return -EINVAL;
4175
4176         if (!system_available)
4177                 return -EFAULT;
4178
4179         if (interface->scanning)
4180                 return -EALREADY;
4181
4182         switch (interface->state) {
4183         case G_SUPPLICANT_STATE_AUTHENTICATING:
4184         case G_SUPPLICANT_STATE_ASSOCIATING:
4185         case G_SUPPLICANT_STATE_ASSOCIATED:
4186         case G_SUPPLICANT_STATE_4WAY_HANDSHAKE:
4187         case G_SUPPLICANT_STATE_GROUP_HANDSHAKE:
4188                 return -EBUSY;
4189         case G_SUPPLICANT_STATE_UNKNOWN:
4190         case G_SUPPLICANT_STATE_DISABLED:
4191         case G_SUPPLICANT_STATE_DISCONNECTED:
4192         case G_SUPPLICANT_STATE_INACTIVE:
4193         case G_SUPPLICANT_STATE_SCANNING:
4194         case G_SUPPLICANT_STATE_COMPLETED:
4195                 break;
4196         }
4197
4198         return 0;
4199 }
4200
4201 int g_supplicant_interface_scan(GSupplicantInterface *interface,
4202                                 GSupplicantScanParams *scan_data,
4203                                 GSupplicantInterfaceCallback callback,
4204                                                         void *user_data)
4205 {
4206         struct interface_scan_data *data;
4207         int ret;
4208
4209         ret = interface_ready_to_scan(interface);
4210         if (ret)
4211                 return ret;
4212
4213         data = dbus_malloc0(sizeof(*data));
4214         if (!data)
4215                 return -ENOMEM;
4216
4217         data->interface = interface;
4218         data->path = g_strdup(interface->path);
4219 #if defined TIZEN_EXT
4220         data->interface->scan_callback = data->callback = callback;
4221         data->interface->scan_data = data->user_data = user_data;
4222 #else
4223         data->callback = callback;
4224         data->user_data = user_data;
4225 #endif
4226         data->scan_params = scan_data;
4227
4228         interface->scan_callback = callback;
4229         interface->scan_data = user_data;
4230
4231         ret = supplicant_dbus_method_call(interface->path,
4232                         SUPPLICANT_INTERFACE ".Interface", "Scan",
4233                         interface_scan_params, interface_scan_result, data,
4234                         interface);
4235
4236         if (ret < 0) {
4237                 g_free(data->path);
4238                 dbus_free(data);
4239         }
4240
4241         return ret;
4242 }
4243
4244 static int parse_supplicant_error(DBusMessageIter *iter)
4245 {
4246         int err = -ECANCELED;
4247         char *key;
4248
4249         if (!iter)
4250                 return err;
4251
4252         /* If the given passphrase is malformed wpa_s returns
4253          * "invalid message format" but this error should be interpreted as
4254          * invalid-key.
4255          */
4256         while (dbus_message_iter_get_arg_type(iter) == DBUS_TYPE_STRING) {
4257                 dbus_message_iter_get_basic(iter, &key);
4258                 if (strncmp(key, "psk", 3) == 0 ||
4259                                 strncmp(key, "wep_key", 7) == 0 ||
4260                                 strcmp(key, "invalid message format") == 0) {
4261                         err = -ENOKEY;
4262                         break;
4263                 }
4264                 dbus_message_iter_next(iter);
4265         }
4266
4267         return err;
4268 }
4269
4270 static void interface_select_network_result(const char *error,
4271                                 DBusMessageIter *iter, void *user_data)
4272 {
4273         struct interface_connect_data *data = user_data;
4274         int err;
4275
4276         SUPPLICANT_DBG("");
4277
4278         err = 0;
4279         if (error) {
4280                 SUPPLICANT_DBG("SelectNetwork error %s", error);
4281                 err = parse_supplicant_error(iter);
4282         }
4283
4284         g_free(data->path);
4285
4286         if (data->callback)
4287                 data->callback(err, data->interface, data->user_data);
4288
4289         g_free(data->ssid);
4290         dbus_free(data);
4291 }
4292
4293 static void interface_select_network_params(DBusMessageIter *iter,
4294                                                         void *user_data)
4295 {
4296         struct interface_connect_data *data = user_data;
4297         GSupplicantInterface *interface = data->interface;
4298
4299         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
4300                                         &interface->network_path);
4301 }
4302
4303 static void interface_add_network_result(const char *error,
4304                                 DBusMessageIter *iter, void *user_data)
4305 {
4306         struct interface_connect_data *data = user_data;
4307         GSupplicantInterface *interface = data->interface;
4308         const char *path;
4309         int err;
4310
4311         if (error)
4312                 goto error;
4313
4314         dbus_message_iter_get_basic(iter, &path);
4315         if (!path)
4316                 goto error;
4317
4318         SUPPLICANT_DBG("PATH: %s", path);
4319
4320         g_free(interface->network_path);
4321         interface->network_path = g_strdup(path);
4322
4323         supplicant_dbus_method_call(data->interface->path,
4324                         SUPPLICANT_INTERFACE ".Interface", "SelectNetwork",
4325                         interface_select_network_params,
4326                         interface_select_network_result, data,
4327                         interface);
4328
4329         return;
4330
4331 error:
4332         SUPPLICANT_DBG("AddNetwork error %s", error);
4333
4334         if (interface_exists(data->interface, data->interface->path)) {
4335                 err = parse_supplicant_error(iter);
4336                 if (data->callback)
4337                         data->callback(err, data->interface, data->user_data);
4338
4339                 g_free(interface->network_path);
4340                 interface->network_path = NULL;
4341         }
4342
4343         g_free(data->path);
4344         g_free(data->ssid);
4345         g_free(data);
4346 }
4347
4348 static void add_network_security_none(DBusMessageIter *dict)
4349 {
4350         const char *auth_alg = "OPEN";
4351
4352         supplicant_dbus_dict_append_basic(dict, "auth_alg",
4353                                         DBUS_TYPE_STRING, &auth_alg);
4354 }
4355
4356 static void add_network_security_wep(DBusMessageIter *dict,
4357                                         GSupplicantSSID *ssid)
4358 {
4359         const char *auth_alg = "OPEN SHARED";
4360         dbus_uint32_t key_index = 0;
4361
4362         supplicant_dbus_dict_append_basic(dict, "auth_alg",
4363                                         DBUS_TYPE_STRING, &auth_alg);
4364
4365         if (ssid->passphrase) {
4366                 int size = strlen(ssid->passphrase);
4367                 if (size == 10 || size == 26) {
4368                         unsigned char *key = g_try_malloc(13);
4369                         char tmp[3];
4370                         int i;
4371
4372                         memset(tmp, 0, sizeof(tmp));
4373                         if (!key)
4374                                 size = 0;
4375
4376                         for (i = 0; i < size / 2; i++) {
4377                                 memcpy(tmp, ssid->passphrase + (i * 2), 2);
4378                                 key[i] = (unsigned char) strtol(tmp, NULL, 16);
4379                         }
4380
4381                         supplicant_dbus_dict_append_fixed_array(dict,
4382                                                         "wep_key0",
4383                                                         DBUS_TYPE_BYTE,
4384                                                         &key, size / 2);
4385                         g_free(key);
4386                 } else if (size == 5 || size == 13) {
4387                         unsigned char *key = g_try_malloc(13);
4388                         int i;
4389
4390                         if (!key)
4391                                 size = 0;
4392
4393                         for (i = 0; i < size; i++)
4394                                 key[i] = (unsigned char) ssid->passphrase[i];
4395
4396                         supplicant_dbus_dict_append_fixed_array(dict,
4397                                                                 "wep_key0",
4398                                                                 DBUS_TYPE_BYTE,
4399                                                                 &key, size);
4400                         g_free(key);
4401                 } else
4402                         supplicant_dbus_dict_append_basic(dict,
4403                                                         "wep_key0",
4404                                                         DBUS_TYPE_STRING,
4405                                                         &ssid->passphrase);
4406
4407                 supplicant_dbus_dict_append_basic(dict, "wep_tx_keyidx",
4408                                         DBUS_TYPE_UINT32, &key_index);
4409         }
4410 }
4411
4412 static dbus_bool_t is_psk_raw_key(const char *psk)
4413 {
4414         int i;
4415
4416         /* A raw key is always 64 bytes length... */
4417         if (strlen(psk) != 64)
4418                 return FALSE;
4419
4420         /* ... and its content is in hex representation */
4421         for (i = 0; i < 64; i++)
4422                 if (!isxdigit((unsigned char) psk[i]))
4423                         return FALSE;
4424
4425         return TRUE;
4426 }
4427
4428 static unsigned char hexchar2bin(char c)
4429 {
4430         if ((c >= '0') && (c <= '9'))
4431                 return c - '0';
4432         else if ((c >= 'A') && (c <= 'F'))
4433                 return c - 'A' + 10;
4434         else if ((c >= 'a') && (c <= 'f'))
4435                 return c - 'a' + 10;
4436         else
4437                 return c;
4438 }
4439
4440 static void hexstring2bin(const char *string, unsigned char *data,
4441                                 size_t data_len)
4442 {
4443         size_t i;
4444
4445         for (i = 0; i < data_len; i++)
4446                 data[i] = (hexchar2bin(string[i * 2 + 0]) << 4 |
4447                            hexchar2bin(string[i * 2 + 1]) << 0);
4448 }
4449
4450 static void add_network_security_psk(DBusMessageIter *dict,
4451                                         GSupplicantSSID *ssid)
4452 {
4453         if (ssid->passphrase && strlen(ssid->passphrase) > 0) {
4454                 const char *key = "psk";
4455
4456                 if (is_psk_raw_key(ssid->passphrase)) {
4457                         unsigned char data[32];
4458                         unsigned char *datap = data;
4459
4460                         /* The above pointer alias is required by D-Bus because
4461                          * with D-Bus and GCC, non-heap-allocated arrays cannot
4462                          * be passed directly by their base pointer. */
4463
4464                         hexstring2bin(ssid->passphrase, datap, sizeof(data));
4465
4466                         supplicant_dbus_dict_append_fixed_array(dict,
4467                                                         key, DBUS_TYPE_BYTE,
4468                                                         &datap, sizeof(data));
4469                 } else
4470                         supplicant_dbus_dict_append_basic(dict,
4471                                                         key, DBUS_TYPE_STRING,
4472                                                         &ssid->passphrase);
4473         }
4474 }
4475
4476 static void add_network_security_tls(DBusMessageIter *dict,
4477                                         GSupplicantSSID *ssid)
4478 {
4479         /*
4480          * For TLS, we at least need:
4481          *              The client certificate
4482          *              The client private key file
4483          *              The client private key file password
4484          *
4485          * The Authority certificate is optional.
4486          */
4487         if (!ssid->client_cert_path)
4488                 return;
4489
4490         if (!ssid->private_key_path)
4491                 return;
4492
4493         if (!ssid->private_key_passphrase)
4494                 return;
4495
4496         if (ssid->ca_cert_path)
4497                 supplicant_dbus_dict_append_basic(dict, "ca_cert",
4498                                         DBUS_TYPE_STRING, &ssid->ca_cert_path);
4499
4500         supplicant_dbus_dict_append_basic(dict, "private_key",
4501                                                 DBUS_TYPE_STRING,
4502                                                 &ssid->private_key_path);
4503         supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
4504                                                 DBUS_TYPE_STRING,
4505                                                 &ssid->private_key_passphrase);
4506         supplicant_dbus_dict_append_basic(dict, "client_cert",
4507                                                 DBUS_TYPE_STRING,
4508                                                 &ssid->client_cert_path);
4509 }
4510
4511 static void add_network_security_peap(DBusMessageIter *dict,
4512                                         GSupplicantSSID *ssid)
4513 {
4514         char *phase2_auth;
4515
4516         /*
4517          * For PEAP/TTLS, we at least need
4518          *              The authority certificate
4519          *              The 2nd phase authentication method
4520          *              The 2nd phase passphrase
4521          *
4522          * The Client certificate is optional although strongly recommended
4523          * When setting it, we need in addition
4524          *              The Client private key file
4525          *              The Client private key file password
4526          */
4527         if (!ssid->passphrase)
4528                 return;
4529
4530         if (!ssid->phase2_auth)
4531                 return;
4532
4533         if (ssid->client_cert_path) {
4534                 if (!ssid->private_key_path)
4535                         return;
4536
4537                 if (!ssid->private_key_passphrase)
4538                         return;
4539
4540                 supplicant_dbus_dict_append_basic(dict, "client_cert",
4541                                                 DBUS_TYPE_STRING,
4542                                                 &ssid->client_cert_path);
4543
4544                 supplicant_dbus_dict_append_basic(dict, "private_key",
4545                                                 DBUS_TYPE_STRING,
4546                                                 &ssid->private_key_path);
4547
4548                 supplicant_dbus_dict_append_basic(dict, "private_key_passwd",
4549                                                 DBUS_TYPE_STRING,
4550                                                 &ssid->private_key_passphrase);
4551
4552         }
4553
4554         if (g_str_has_prefix(ssid->phase2_auth, "EAP-")) {
4555                 phase2_auth = g_strdup_printf("autheap=%s",
4556                                         ssid->phase2_auth + strlen("EAP-"));
4557         } else
4558                 phase2_auth = g_strdup_printf("auth=%s", ssid->phase2_auth);
4559
4560         supplicant_dbus_dict_append_basic(dict, "password",
4561                                                 DBUS_TYPE_STRING,
4562                                                 &ssid->passphrase);
4563
4564         if (ssid->ca_cert_path)
4565                 supplicant_dbus_dict_append_basic(dict, "ca_cert",
4566                                                 DBUS_TYPE_STRING,
4567                                                 &ssid->ca_cert_path);
4568
4569         supplicant_dbus_dict_append_basic(dict, "phase2",
4570                                                 DBUS_TYPE_STRING,
4571                                                 &phase2_auth);
4572
4573         g_free(phase2_auth);
4574 }
4575
4576 static void add_network_security_eap(DBusMessageIter *dict,
4577                                         GSupplicantSSID *ssid)
4578 {
4579         char *eap_value;
4580
4581 #if defined TIZEN_EXT
4582         if (!ssid->eap)
4583 #else
4584         if (!ssid->eap || !ssid->identity)
4585 #endif
4586                 return;
4587
4588         if (g_strcmp0(ssid->eap, "tls") == 0) {
4589                 add_network_security_tls(dict, ssid);
4590         } else if (g_strcmp0(ssid->eap, "peap") == 0 ||
4591                                 g_strcmp0(ssid->eap, "ttls") == 0) {
4592 #if defined TIZEN_EXT
4593                 if (!ssid->identity)
4594                         return;
4595 #endif
4596                 add_network_security_peap(dict, ssid);
4597
4598 #if defined TIZEN_EXT
4599         } else if (g_strcmp0(ssid->eap, "sim") == 0 ||
4600                         g_strcmp0(ssid->eap, "aka") == 0) {
4601 #endif
4602         } else
4603                 return;
4604
4605         eap_value = g_ascii_strup(ssid->eap, -1);
4606
4607         supplicant_dbus_dict_append_basic(dict, "eap",
4608                                                 DBUS_TYPE_STRING,
4609                                                 &eap_value);
4610 #if defined TIZEN_EXT
4611         if (ssid->identity != NULL)
4612                 supplicant_dbus_dict_append_basic(dict, "identity",
4613                                                         DBUS_TYPE_STRING,
4614                                                         &ssid->identity);
4615 #else
4616         supplicant_dbus_dict_append_basic(dict, "identity",
4617                                                 DBUS_TYPE_STRING,
4618                                                 &ssid->identity);
4619 #endif
4620
4621         g_free(eap_value);
4622 }
4623
4624 static void add_network_security_ciphers(DBusMessageIter *dict,
4625                                                 GSupplicantSSID *ssid)
4626 {
4627         unsigned int p_cipher, g_cipher, i;
4628         char *pairwise, *group;
4629         char *pair_ciphers[4];
4630         char *group_ciphers[5];
4631
4632         p_cipher = ssid->pairwise_cipher;
4633         g_cipher = ssid->group_cipher;
4634
4635         if (p_cipher == 0 && g_cipher == 0)
4636                 return;
4637
4638         i = 0;
4639
4640         if (p_cipher & G_SUPPLICANT_PAIRWISE_CCMP)
4641                 pair_ciphers[i++] = "CCMP";
4642
4643         if (p_cipher & G_SUPPLICANT_PAIRWISE_TKIP)
4644                 pair_ciphers[i++] = "TKIP";
4645
4646         if (p_cipher & G_SUPPLICANT_PAIRWISE_NONE)
4647                 pair_ciphers[i++] = "NONE";
4648
4649         pair_ciphers[i] = NULL;
4650
4651         i = 0;
4652
4653         if (g_cipher & G_SUPPLICANT_GROUP_CCMP)
4654                 group_ciphers[i++] = "CCMP";
4655
4656         if (g_cipher & G_SUPPLICANT_GROUP_TKIP)
4657                 group_ciphers[i++] = "TKIP";
4658
4659         if (g_cipher & G_SUPPLICANT_GROUP_WEP104)
4660                 group_ciphers[i++] = "WEP104";
4661
4662         if (g_cipher & G_SUPPLICANT_GROUP_WEP40)
4663                 group_ciphers[i++] = "WEP40";
4664
4665         group_ciphers[i] = NULL;
4666
4667         pairwise = g_strjoinv(" ", pair_ciphers);
4668         group = g_strjoinv(" ", group_ciphers);
4669
4670         SUPPLICANT_DBG("cipher %s %s", pairwise, group);
4671
4672         supplicant_dbus_dict_append_basic(dict, "pairwise",
4673                                                 DBUS_TYPE_STRING,
4674                                                 &pairwise);
4675         supplicant_dbus_dict_append_basic(dict, "group",
4676                                                 DBUS_TYPE_STRING,
4677                                                 &group);
4678
4679         g_free(pairwise);
4680         g_free(group);
4681 }
4682
4683 static void add_network_security_proto(DBusMessageIter *dict,
4684                                                 GSupplicantSSID *ssid)
4685 {
4686         unsigned int protocol, i;
4687         char *proto;
4688         char *protos[3];
4689
4690         protocol = ssid->protocol;
4691
4692         if (protocol == 0)
4693                 return;
4694
4695         i = 0;
4696
4697         if (protocol & G_SUPPLICANT_PROTO_RSN)
4698                 protos[i++] = "RSN";
4699
4700         if (protocol & G_SUPPLICANT_PROTO_WPA)
4701                 protos[i++] = "WPA";
4702
4703         protos[i] = NULL;
4704
4705         proto = g_strjoinv(" ", protos);
4706
4707         SUPPLICANT_DBG("proto %s", proto);
4708
4709         supplicant_dbus_dict_append_basic(dict, "proto",
4710                                                 DBUS_TYPE_STRING,
4711                                                 &proto);
4712
4713         g_free(proto);
4714 }
4715
4716 static void add_network_security(DBusMessageIter *dict, GSupplicantSSID *ssid)
4717 {
4718         char *key_mgmt;
4719
4720         switch (ssid->security) {
4721         case G_SUPPLICANT_SECURITY_NONE:
4722                 key_mgmt = "NONE";
4723                 add_network_security_none(dict);
4724                 add_network_security_ciphers(dict, ssid);
4725                 break;
4726         case G_SUPPLICANT_SECURITY_UNKNOWN:
4727         case G_SUPPLICANT_SECURITY_WEP:
4728                 key_mgmt = "NONE";
4729                 add_network_security_wep(dict, ssid);
4730                 add_network_security_ciphers(dict, ssid);
4731                 break;
4732         case G_SUPPLICANT_SECURITY_PSK:
4733                 key_mgmt = "WPA-PSK";
4734                 add_network_security_psk(dict, ssid);
4735                 add_network_security_ciphers(dict, ssid);
4736                 add_network_security_proto(dict, ssid);
4737                 break;
4738         case G_SUPPLICANT_SECURITY_IEEE8021X:
4739                 key_mgmt = "WPA-EAP";
4740                 add_network_security_eap(dict, ssid);
4741                 add_network_security_ciphers(dict, ssid);
4742                 add_network_security_proto(dict, ssid);
4743                 break;
4744         }
4745
4746         supplicant_dbus_dict_append_basic(dict, "key_mgmt",
4747                                 DBUS_TYPE_STRING, &key_mgmt);
4748 }
4749
4750 static void add_network_mode(DBusMessageIter *dict, GSupplicantSSID *ssid)
4751 {
4752         dbus_uint32_t mode;
4753
4754         switch (ssid->mode) {
4755         case G_SUPPLICANT_MODE_UNKNOWN:
4756         case G_SUPPLICANT_MODE_INFRA:
4757                 mode = 0;
4758                 break;
4759         case G_SUPPLICANT_MODE_IBSS:
4760                 mode = 1;
4761                 break;
4762         case G_SUPPLICANT_MODE_MASTER:
4763                 mode = 2;
4764                 break;
4765         }
4766
4767         supplicant_dbus_dict_append_basic(dict, "mode",
4768                                 DBUS_TYPE_UINT32, &mode);
4769 }
4770
4771 static void interface_add_network_params(DBusMessageIter *iter, void *user_data)
4772 {
4773         DBusMessageIter dict;
4774         struct interface_connect_data *data = user_data;
4775         GSupplicantSSID *ssid = data->ssid;
4776
4777         supplicant_dbus_dict_open(iter, &dict);
4778
4779         if (ssid->scan_ssid)
4780                 supplicant_dbus_dict_append_basic(&dict, "scan_ssid",
4781                                          DBUS_TYPE_UINT32, &ssid->scan_ssid);
4782
4783         if (ssid->freq)
4784                 supplicant_dbus_dict_append_basic(&dict, "frequency",
4785                                          DBUS_TYPE_UINT32, &ssid->freq);
4786
4787         if (ssid->bgscan)
4788                 supplicant_dbus_dict_append_basic(&dict, "bgscan",
4789                                         DBUS_TYPE_STRING, &ssid->bgscan);
4790
4791         add_network_mode(&dict, ssid);
4792
4793         add_network_security(&dict, ssid);
4794
4795         supplicant_dbus_dict_append_fixed_array(&dict, "ssid",
4796                                         DBUS_TYPE_BYTE, &ssid->ssid,
4797                                                 ssid->ssid_len);
4798
4799         supplicant_dbus_dict_append_basic(&dict, "ignore_broadcast_ssid",
4800                                         DBUS_TYPE_INT32,
4801                                         &ssid->ignore_broadcast_ssid);
4802
4803         supplicant_dbus_dict_close(iter, &dict);
4804 }
4805
4806 static void interface_wps_start_result(const char *error,
4807                                 DBusMessageIter *iter, void *user_data)
4808 {
4809         struct interface_connect_data *data = user_data;
4810         int err;
4811
4812         SUPPLICANT_DBG("");
4813
4814         err = 0;
4815         if (error) {
4816                 SUPPLICANT_DBG("error: %s", error);
4817                 err = parse_supplicant_error(iter);
4818         }
4819
4820         if(data->callback)
4821                 data->callback(err, data->interface, data->user_data);
4822
4823         g_free(data->path);
4824         g_free(data->ssid);
4825         dbus_free(data);
4826 }
4827
4828 static void interface_add_wps_params(DBusMessageIter *iter, void *user_data)
4829 {
4830         struct interface_connect_data *data = user_data;
4831         GSupplicantSSID *ssid = data->ssid;
4832         const char *role = "enrollee", *type;
4833         DBusMessageIter dict;
4834
4835         SUPPLICANT_DBG("");
4836
4837         supplicant_dbus_dict_open(iter, &dict);
4838
4839         supplicant_dbus_dict_append_basic(&dict, "Role",
4840                                                 DBUS_TYPE_STRING, &role);
4841
4842         type = "pbc";
4843         if (ssid->pin_wps) {
4844                 type = "pin";
4845                 supplicant_dbus_dict_append_basic(&dict, "Pin",
4846                                         DBUS_TYPE_STRING, &ssid->pin_wps);
4847         }
4848
4849         supplicant_dbus_dict_append_basic(&dict, "Type",
4850                                         DBUS_TYPE_STRING, &type);
4851
4852 #if defined TIZEN_EXT
4853         if (ssid->bssid)
4854                 supplicant_dbus_dict_append_fixed_array(&dict, "Bssid",
4855                                                 DBUS_TYPE_BYTE, &ssid->bssid, 6);
4856 #endif
4857
4858         supplicant_dbus_dict_close(iter, &dict);
4859 }
4860
4861 static void wps_start(const char *error, DBusMessageIter *iter, void *user_data)
4862 {
4863         struct interface_connect_data *data = user_data;
4864
4865         SUPPLICANT_DBG("");
4866
4867         if (error) {
4868                 SUPPLICANT_DBG("error: %s", error);
4869                 g_free(data->path);
4870                 g_free(data->ssid);
4871                 dbus_free(data);
4872                 return;
4873         }
4874 #if defined TIZEN_EXT
4875         GSupplicantSSID *ssid = data->ssid;
4876         if (ssid->pin_wps != NULL) {
4877                 if (!g_utf8_validate(ssid->pin_wps, 8, NULL)) {
4878                         SUPPLICANT_DBG("Invalid characters in WPS_PIN");
4879                         g_free(data->ssid);
4880                         dbus_free(data);
4881                         return;
4882                 }
4883         }
4884 #endif
4885         supplicant_dbus_method_call(data->interface->path,
4886                         SUPPLICANT_INTERFACE ".Interface.WPS", "Start",
4887                         interface_add_wps_params,
4888                         interface_wps_start_result, data, NULL);
4889 }
4890
4891 static void wps_process_credentials(DBusMessageIter *iter, void *user_data)
4892 {
4893         dbus_bool_t credentials = TRUE;
4894
4895         SUPPLICANT_DBG("");
4896
4897         dbus_message_iter_append_basic(iter, DBUS_TYPE_BOOLEAN, &credentials);
4898 }
4899
4900
4901 int g_supplicant_interface_connect(GSupplicantInterface *interface,
4902                                 GSupplicantSSID *ssid,
4903                                 GSupplicantInterfaceCallback callback,
4904                                                         void *user_data)
4905 {
4906         struct interface_connect_data *data;
4907         int ret;
4908
4909         if (!interface)
4910                 return -EINVAL;
4911
4912         if (!system_available)
4913                 return -EFAULT;
4914
4915         /* TODO: Check if we're already connected and switch */
4916
4917         data = dbus_malloc0(sizeof(*data));
4918         if (!data)
4919                 return -ENOMEM;
4920
4921         data->interface = interface;
4922         data->path = g_strdup(interface->path);
4923         data->callback = callback;
4924         data->ssid = ssid;
4925         data->user_data = user_data;
4926
4927         if (ssid->use_wps) {
4928                 g_free(interface->wps_cred.key);
4929                 memset(&interface->wps_cred, 0,
4930                                 sizeof(struct _GSupplicantWpsCredentials));
4931
4932                 ret = supplicant_dbus_property_set(interface->path,
4933                         SUPPLICANT_INTERFACE ".Interface.WPS",
4934                         "ProcessCredentials", DBUS_TYPE_BOOLEAN_AS_STRING,
4935                         wps_process_credentials, wps_start, data, interface);
4936         } else
4937                 ret = supplicant_dbus_method_call(interface->path,
4938                         SUPPLICANT_INTERFACE ".Interface", "AddNetwork",
4939                         interface_add_network_params,
4940                         interface_add_network_result, data,
4941                         interface);
4942
4943         if (ret < 0) {
4944                 g_free(data->path);
4945                 dbus_free(data);
4946                 return ret;
4947         }
4948
4949         return -EINPROGRESS;
4950 }
4951
4952 static void network_remove_result(const char *error,
4953                                 DBusMessageIter *iter, void *user_data)
4954 {
4955         struct interface_data *data = user_data;
4956         int result = 0;
4957
4958         SUPPLICANT_DBG("");
4959
4960         if (error) {
4961                 result = -EIO;
4962                 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
4963                                                 error) == 0)
4964                         result = -ECONNABORTED;
4965         }
4966
4967         g_free(data->path);
4968
4969         if (data->callback)
4970                 data->callback(result, data->interface, data->user_data);
4971
4972         dbus_free(data);
4973 }
4974
4975 static void network_remove_params(DBusMessageIter *iter, void *user_data)
4976 {
4977         struct interface_data *data = user_data;
4978         const char *path = data->interface->network_path;
4979
4980         SUPPLICANT_DBG("path %s", path);
4981
4982         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH, &path);
4983 }
4984
4985 static int network_remove(struct interface_data *data)
4986 {
4987         GSupplicantInterface *interface = data->interface;
4988
4989         SUPPLICANT_DBG("");
4990
4991 #if defined TIZEN_EXT
4992         GSupplicantInterface *intf = NULL;
4993         /*
4994          * Check if 'interface' is valid
4995          */
4996         intf = g_hash_table_lookup(interface_table, interface->path);
4997         if (intf == NULL)
4998                 return -EINVAL;
4999 #endif
5000
5001         return supplicant_dbus_method_call(interface->path,
5002                         SUPPLICANT_INTERFACE ".Interface", "RemoveNetwork",
5003                         network_remove_params, network_remove_result, data,
5004                         interface);
5005 }
5006
5007 static void interface_disconnect_result(const char *error,
5008                                 DBusMessageIter *iter, void *user_data)
5009 {
5010         struct interface_data *data = user_data;
5011         int result = 0;
5012
5013         SUPPLICANT_DBG("");
5014
5015         if (error) {
5016                 result = -EIO;
5017                 if (g_strcmp0("org.freedesktop.DBus.Error.UnknownMethod",
5018                                                 error) == 0)
5019                         result = -ECONNABORTED;
5020         }
5021
5022         if (result < 0 && data->callback) {
5023                 data->callback(result, data->interface, data->user_data);
5024                 data->callback = NULL;
5025         }
5026
5027         /* If we are disconnecting from previous WPS successful
5028          * association. i.e.: it did not went through AddNetwork,
5029          * and interface->network_path was never set. */
5030         if (!data->interface->network_path) {
5031                 g_free(data->path);
5032                 dbus_free(data);
5033                 return;
5034         }
5035
5036         if (result != -ECONNABORTED) {
5037                 if (network_remove(data) < 0) {
5038                         g_free(data->path);
5039                         dbus_free(data);
5040                 }
5041         } else {
5042                 g_free(data->path);
5043                 dbus_free(data);
5044         }
5045 }
5046
5047 int g_supplicant_interface_disconnect(GSupplicantInterface *interface,
5048                                         GSupplicantInterfaceCallback callback,
5049                                                         void *user_data)
5050 {
5051         struct interface_data *data;
5052         int ret;
5053
5054         SUPPLICANT_DBG("");
5055
5056         if (!interface)
5057                 return -EINVAL;
5058
5059         if (!system_available)
5060                 return -EFAULT;
5061
5062         data = dbus_malloc0(sizeof(*data));
5063         if (!data)
5064                 return -ENOMEM;
5065
5066         data->interface = interface;
5067         data->path = g_strdup(interface->path);
5068         data->callback = callback;
5069         data->user_data = user_data;
5070
5071         ret = supplicant_dbus_method_call(interface->path,
5072                         SUPPLICANT_INTERFACE ".Interface", "Disconnect",
5073                         NULL, interface_disconnect_result, data,
5074                         interface);
5075
5076         if (ret < 0) {
5077                 g_free(data->path);
5078                 dbus_free(data);
5079         }
5080
5081         return ret;
5082 }
5083
5084 static void interface_p2p_find_result(const char *error,
5085                                         DBusMessageIter *iter, void *user_data)
5086 {
5087         struct interface_scan_data *data = user_data;
5088         int err = 0;
5089
5090         SUPPLICANT_DBG("error %s", error);
5091
5092         if (error)
5093                 err = -EIO;
5094
5095         if (interface_exists(data->interface, data->path)) {
5096                 if (!data->interface->ready)
5097                         err = -ENOLINK;
5098                 if (!err)
5099                         data->interface->p2p_finding = true;
5100         }
5101
5102         if (data->callback)
5103                 data->callback(err, data->interface, data->user_data);
5104
5105         g_free(data->path);
5106         dbus_free(data);
5107 }
5108
5109 static void interface_p2p_find_params(DBusMessageIter *iter, void *user_data)
5110 {
5111         DBusMessageIter dict;
5112
5113         supplicant_dbus_dict_open(iter, &dict);
5114         supplicant_dbus_dict_close(iter, &dict);
5115 }
5116
5117 int g_supplicant_interface_p2p_find(GSupplicantInterface *interface,
5118                                         GSupplicantInterfaceCallback callback,
5119                                                         void *user_data)
5120 {
5121         struct interface_scan_data *data;
5122         int ret;
5123
5124         if (!interface->p2p_support)
5125                 return -ENOTSUP;
5126
5127         ret = interface_ready_to_scan(interface);
5128         if (ret && ret != -EALREADY)
5129                 return ret;
5130
5131         data = dbus_malloc0(sizeof(*data));
5132         if (!data)
5133                 return -ENOMEM;
5134
5135         data->interface = interface;
5136         data->path = g_strdup(interface->path);
5137         data->callback = callback;
5138         data->user_data = user_data;
5139
5140         ret = supplicant_dbus_method_call(interface->path,
5141                         SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Find",
5142                         interface_p2p_find_params, interface_p2p_find_result,
5143                         data, interface);
5144         if (ret < 0) {
5145                 g_free(data->path);
5146                 dbus_free(data);
5147         }
5148
5149         return ret;
5150 }
5151
5152 bool g_supplicant_interface_is_p2p_finding(GSupplicantInterface *interface)
5153 {
5154         if (!interface)
5155                 return false;
5156
5157         return interface->p2p_finding;
5158 }
5159
5160 int g_supplicant_interface_p2p_stop_find(GSupplicantInterface *interface)
5161 {
5162         if (!interface->p2p_finding)
5163                 return 0;
5164
5165         SUPPLICANT_DBG("");
5166
5167         interface->p2p_finding = false;
5168
5169         return supplicant_dbus_method_call(interface->path,
5170                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "StopFind",
5171                 NULL, NULL, NULL, NULL);
5172 }
5173
5174 static void interface_p2p_connect_result(const char *error,
5175                                         DBusMessageIter *iter, void *user_data)
5176 {
5177         struct interface_connect_data *data = user_data;
5178         int err = 0;
5179
5180         SUPPLICANT_DBG("");
5181
5182         if (error)
5183                 err = parse_supplicant_error(iter);
5184
5185         if (data->callback)
5186                 data->callback(err, data->interface, data->user_data);
5187
5188         g_free(data->path);
5189         g_free(data->peer->wps_pin);
5190         g_free(data->peer->path);
5191         g_free(data->peer);
5192         g_free(data);
5193 }
5194
5195 static void interface_p2p_connect_params(DBusMessageIter *iter, void *user_data)
5196 {
5197         struct interface_connect_data *data = user_data;
5198         const char *wps = "pbc";
5199         DBusMessageIter dict;
5200         int go_intent = 7;
5201
5202         SUPPLICANT_DBG("");
5203
5204         supplicant_dbus_dict_open(iter, &dict);
5205
5206         if (data->peer->master)
5207                 go_intent = 15;
5208
5209         if (data->peer->wps_pin)
5210                 wps = "pin";
5211
5212         supplicant_dbus_dict_append_basic(&dict, "peer",
5213                                 DBUS_TYPE_OBJECT_PATH, &data->peer->path);
5214         supplicant_dbus_dict_append_basic(&dict, "wps_method",
5215                                 DBUS_TYPE_STRING, &wps);
5216         if (data->peer->wps_pin) {
5217                 supplicant_dbus_dict_append_basic(&dict, "pin",
5218                                 DBUS_TYPE_STRING, &data->peer->wps_pin);
5219         }
5220
5221         supplicant_dbus_dict_append_basic(&dict, "go_intent",
5222                                         DBUS_TYPE_INT32, &go_intent);
5223
5224         supplicant_dbus_dict_close(iter, &dict);
5225 }
5226
5227 int g_supplicant_interface_p2p_connect(GSupplicantInterface *interface,
5228                                         GSupplicantPeerParams *peer_params,
5229                                         GSupplicantInterfaceCallback callback,
5230                                         void *user_data)
5231 {
5232         struct interface_connect_data *data;
5233         int ret;
5234
5235         SUPPLICANT_DBG("");
5236
5237         if (!interface->p2p_support)
5238                 return -ENOTSUP;
5239
5240         data = dbus_malloc0(sizeof(*data));
5241         data->interface = interface;
5242         data->path = g_strdup(interface->path);
5243         data->peer = peer_params;
5244         data->callback = callback;
5245         data->user_data = user_data;
5246
5247         ret = supplicant_dbus_method_call(interface->path,
5248                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "Connect",
5249                 interface_p2p_connect_params, interface_p2p_connect_result,
5250                 data, interface);
5251         if (ret < 0) {
5252                 g_free(data->path);
5253                 dbus_free(data);
5254                 return ret;
5255         }
5256
5257         return -EINPROGRESS;
5258 }
5259
5260 int g_supplicant_interface_p2p_disconnect(GSupplicantInterface *interface,
5261                                         GSupplicantPeerParams *peer_params)
5262 {
5263         GSupplicantPeer *peer;
5264         int count = 0;
5265         GSList *list;
5266
5267         SUPPLICANT_DBG("");
5268
5269         if (!interface->p2p_support)
5270                 return -ENOTSUP;
5271
5272         peer = g_hash_table_lookup(interface->peer_table, peer_params->path);
5273         if (!peer)
5274                 return -ENODEV;
5275
5276         for (list = peer->groups; list; list = list->next, count++) {
5277                 const char *group_obj_path = list->data;
5278                 GSupplicantInterface *g_interface;
5279                 GSupplicantGroup *group;
5280
5281                 group = g_hash_table_lookup(group_mapping, group_obj_path);
5282                 if (!group || !group->interface)
5283                         continue;
5284
5285                 g_interface = group->interface;
5286                 supplicant_dbus_method_call(g_interface->path,
5287                                 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5288                                 "Disconnect", NULL, NULL, NULL, g_interface);
5289         }
5290
5291         if (count == 0 && peer->current_group_iface) {
5292                 supplicant_dbus_method_call(peer->current_group_iface->path,
5293                                 SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5294                                 "Disconnect", NULL, NULL, NULL,
5295                                 peer->current_group_iface->path);
5296         }
5297
5298         peer->current_group_iface = NULL;
5299
5300         return -EINPROGRESS;
5301 }
5302
5303 struct p2p_service_data {
5304         bool registration;
5305         GSupplicantInterface *interface;
5306         GSupplicantP2PServiceParams *service;
5307         GSupplicantInterfaceCallback callback;
5308         void *user_data;
5309 };
5310
5311 static void interface_p2p_service_result(const char *error,
5312                                         DBusMessageIter *iter, void *user_data)
5313 {
5314         struct p2p_service_data *data = user_data;
5315         int result = 0;
5316
5317         SUPPLICANT_DBG("%s result - %s", data->registration ?
5318                                 "Registration" : "Deletion",
5319                                 error ? error : "Success");
5320         if (error)
5321                 result = -EINVAL;
5322
5323         if (data->callback)
5324                 data->callback(result, data->interface, data->user_data);
5325
5326         g_free(data->service->query);
5327         g_free(data->service->response);
5328         g_free(data->service->service);
5329         g_free(data->service->wfd_ies);
5330         g_free(data->service);
5331         dbus_free(data);
5332 }
5333
5334 static void interface_p2p_service_params(DBusMessageIter *iter,
5335                                                         void *user_data)
5336 {
5337         struct p2p_service_data *data = user_data;
5338         GSupplicantP2PServiceParams *service;
5339         DBusMessageIter dict;
5340         const char *type;
5341
5342         SUPPLICANT_DBG("");
5343
5344         service = data->service;
5345
5346         supplicant_dbus_dict_open(iter, &dict);
5347
5348         if (service->query && service->response) {
5349                 type = "bonjour";
5350                 supplicant_dbus_dict_append_basic(&dict, "service_type",
5351                                                 DBUS_TYPE_STRING, &type);
5352                 supplicant_dbus_dict_append_fixed_array(&dict, "query",
5353                                         DBUS_TYPE_BYTE, &service->query,
5354                                         service->query_length);
5355                 supplicant_dbus_dict_append_fixed_array(&dict, "response",
5356                                         DBUS_TYPE_BYTE, &service->response,
5357                                         service->response_length);
5358         } else if (service->version && service->service) {
5359                 type = "upnp";
5360                 supplicant_dbus_dict_append_basic(&dict, "service_type",
5361                                                 DBUS_TYPE_STRING, &type);
5362                 supplicant_dbus_dict_append_basic(&dict, "version",
5363                                         DBUS_TYPE_INT32, &service->version);
5364                 supplicant_dbus_dict_append_basic(&dict, "service",
5365                                         DBUS_TYPE_STRING, &service->service);
5366         }
5367
5368         supplicant_dbus_dict_close(iter, &dict);
5369 }
5370
5371 int g_supplicant_interface_p2p_add_service(GSupplicantInterface *interface,
5372                                 GSupplicantInterfaceCallback callback,
5373                                 GSupplicantP2PServiceParams *p2p_service_params,
5374                                 void *user_data)
5375 {
5376         struct p2p_service_data *data;
5377         int ret;
5378
5379         SUPPLICANT_DBG("");
5380
5381         if (!interface->p2p_support)
5382                 return -ENOTSUP;
5383
5384         data = dbus_malloc0(sizeof(*data));
5385         data->registration = true;
5386         data->interface = interface;
5387         data->service = p2p_service_params;
5388         data->callback = callback;
5389         data->user_data = user_data;
5390
5391         ret = supplicant_dbus_method_call(interface->path,
5392                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "AddService",
5393                 interface_p2p_service_params, interface_p2p_service_result,
5394                 data, interface);
5395         if (ret < 0) {
5396                 dbus_free(data);
5397                 return ret;
5398         }
5399
5400         return -EINPROGRESS;
5401 }
5402
5403 int g_supplicant_interface_p2p_del_service(GSupplicantInterface *interface,
5404                                 GSupplicantP2PServiceParams *p2p_service_params)
5405 {
5406         struct p2p_service_data *data;
5407         int ret;
5408
5409         SUPPLICANT_DBG("");
5410
5411         if (!interface->p2p_support)
5412                 return -ENOTSUP;
5413
5414         data = dbus_malloc0(sizeof(*data));
5415         data->interface = interface;
5416         data->service = p2p_service_params;
5417
5418         ret = supplicant_dbus_method_call(interface->path,
5419                 SUPPLICANT_INTERFACE ".Interface.P2PDevice", "DeleteService",
5420                 interface_p2p_service_params, interface_p2p_service_result,
5421                 data, interface);
5422         if (ret < 0) {
5423                 dbus_free(data);
5424                 return ret;
5425         }
5426
5427         return -EINPROGRESS;
5428 }
5429
5430 struct p2p_listen_data {
5431         int period;
5432         int interval;
5433 };
5434
5435 static void interface_p2p_listen_params(DBusMessageIter *iter, void *user_data)
5436 {
5437         struct p2p_listen_data *params = user_data;
5438         DBusMessageIter dict;
5439
5440         supplicant_dbus_dict_open(iter, &dict);
5441
5442         supplicant_dbus_dict_append_basic(&dict, "period",
5443                                         DBUS_TYPE_INT32, &params->period);
5444         supplicant_dbus_dict_append_basic(&dict, "interval",
5445                                         DBUS_TYPE_INT32, &params->interval);
5446         supplicant_dbus_dict_close(iter, &dict);
5447 }
5448
5449 int g_supplicant_interface_p2p_listen(GSupplicantInterface *interface,
5450                                                 int period, int interval)
5451 {
5452         struct p2p_listen_data params;
5453
5454         SUPPLICANT_DBG("");
5455
5456         if (!interface->p2p_support)
5457                 return -ENOTSUP;
5458
5459         params.period = period;
5460         params.interval = interval;
5461
5462         return supplicant_dbus_method_call(interface->path,
5463                         SUPPLICANT_INTERFACE ".Interface.P2PDevice",
5464                         "ExtendedListen", interface_p2p_listen_params,
5465                         NULL, &params, NULL);
5466 }
5467
5468 static void widi_ies_params(DBusMessageIter *iter, void *user_data)
5469 {
5470         struct p2p_service_data *data = user_data;
5471         GSupplicantP2PServiceParams *service = data->service;
5472         DBusMessageIter array;
5473
5474         SUPPLICANT_DBG("%p - %d", service->wfd_ies, service->wfd_ies_length);
5475
5476         dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY,
5477                                         DBUS_TYPE_BYTE_AS_STRING, &array);
5478
5479         if (service->wfd_ies && service->wfd_ies_length > 0) {
5480                 dbus_message_iter_append_fixed_array(&array, DBUS_TYPE_BYTE,
5481                                 &service->wfd_ies, service->wfd_ies_length);
5482         }
5483
5484         dbus_message_iter_close_container(iter, &array);
5485 }
5486
5487 int g_supplicant_set_widi_ies(GSupplicantP2PServiceParams *p2p_service_params,
5488                                         GSupplicantInterfaceCallback callback,
5489                                         void *user_data)
5490 {
5491         struct p2p_service_data *data;
5492         int ret;
5493
5494         SUPPLICANT_DBG("");
5495
5496         if (!system_available)
5497                 return -EFAULT;
5498
5499         data = dbus_malloc0(sizeof(*data));
5500         data->service = p2p_service_params;
5501         data->callback = callback;
5502         data->user_data = user_data;
5503
5504         if (p2p_service_params->wfd_ies)
5505                 data->registration = true;
5506
5507         ret = supplicant_dbus_property_set(SUPPLICANT_PATH,
5508                                         SUPPLICANT_INTERFACE, "WFDIEs",
5509                                         DBUS_TYPE_ARRAY_AS_STRING
5510                                         DBUS_TYPE_BYTE_AS_STRING,
5511                                         widi_ies_params,
5512                                         interface_p2p_service_result,
5513                                         data, NULL);
5514         if (ret < 0 && ret != -EINPROGRESS) {
5515                 dbus_free(data);
5516                 return ret;
5517         }
5518
5519         return -EINPROGRESS;
5520 }
5521
5522
5523 static const char *g_supplicant_rule0 = "type=signal,"
5524                                         "path=" DBUS_PATH_DBUS ","
5525                                         "sender=" DBUS_SERVICE_DBUS ","
5526                                         "interface=" DBUS_INTERFACE_DBUS ","
5527                                         "member=NameOwnerChanged,"
5528                                         "arg0=" SUPPLICANT_SERVICE;
5529 static const char *g_supplicant_rule1 = "type=signal,"
5530                         "interface=" SUPPLICANT_INTERFACE;
5531 static const char *g_supplicant_rule2 = "type=signal,"
5532                         "interface=" SUPPLICANT_INTERFACE ".Interface";
5533 static const char *g_supplicant_rule3 = "type=signal,"
5534                         "interface=" SUPPLICANT_INTERFACE ".Interface.WPS";
5535 static const char *g_supplicant_rule4 = "type=signal,"
5536                         "interface=" SUPPLICANT_INTERFACE ".BSS";
5537 static const char *g_supplicant_rule5 = "type=signal,"
5538                         "interface=" SUPPLICANT_INTERFACE ".Network";
5539 #if !defined TIZEN_EXT
5540 static const char *g_supplicant_rule6 = "type=signal,"
5541                 "interface=" SUPPLICANT_INTERFACE ".Interface.P2PDevice";
5542 static const char *g_supplicant_rule7 = "type=signal,"
5543                 "interface=" SUPPLICANT_INTERFACE ".Peer";
5544 static const char *g_supplicant_rule8 = "type=signal,"
5545                 "interface=" SUPPLICANT_INTERFACE ".Group";
5546 #endif
5547
5548 static void invoke_introspect_method(void)
5549 {
5550         DBusMessage *message;
5551
5552         message = dbus_message_new_method_call(SUPPLICANT_SERVICE,
5553                                         SUPPLICANT_PATH,
5554                                         DBUS_INTERFACE_INTROSPECTABLE,
5555                                         "Introspect");
5556
5557         if (!message)
5558                 return;
5559
5560         dbus_message_set_no_reply(message, TRUE);
5561         dbus_connection_send(connection, message, NULL);
5562         dbus_message_unref(message);
5563 }
5564
5565 int g_supplicant_register(const GSupplicantCallbacks *callbacks)
5566 {
5567         connection = dbus_bus_get(DBUS_BUS_SYSTEM, NULL);
5568         if (!connection)
5569                 return -EIO;
5570
5571         if (!dbus_connection_add_filter(connection, g_supplicant_filter,
5572                                                 NULL, NULL)) {
5573                 dbus_connection_unref(connection);
5574                 connection = NULL;
5575                 return -EIO;
5576         }
5577
5578         callbacks_pointer = callbacks;
5579         eap_methods = 0;
5580
5581         interface_table = g_hash_table_new_full(g_str_hash, g_str_equal,
5582                                                 NULL, remove_interface);
5583
5584         bss_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5585                                                                 NULL, NULL);
5586         peer_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5587                                                                 NULL, NULL);
5588         group_mapping = g_hash_table_new_full(g_str_hash, g_str_equal,
5589                                                                 NULL, NULL);
5590         pending_peer_connection = g_hash_table_new_full(g_str_hash, g_str_equal,
5591                                                                 NULL, NULL);
5592
5593         supplicant_dbus_setup(connection);
5594
5595         dbus_bus_add_match(connection, g_supplicant_rule0, NULL);
5596         dbus_bus_add_match(connection, g_supplicant_rule1, NULL);
5597         dbus_bus_add_match(connection, g_supplicant_rule2, NULL);
5598         dbus_bus_add_match(connection, g_supplicant_rule3, NULL);
5599         dbus_bus_add_match(connection, g_supplicant_rule4, NULL);
5600         dbus_bus_add_match(connection, g_supplicant_rule5, NULL);
5601 #if defined TIZEN_EXT
5602         dbus_bus_add_match(connection,
5603                         "type=signal,interface=org.tizen.system.deviced.PowerOff,"
5604                         "member=ChangeState", NULL);
5605 #endif
5606 #if !defined TIZEN_EXT
5607         dbus_bus_add_match(connection, g_supplicant_rule6, NULL);
5608         dbus_bus_add_match(connection, g_supplicant_rule7, NULL);
5609         dbus_bus_add_match(connection, g_supplicant_rule8, NULL);
5610 #endif
5611         dbus_connection_flush(connection);
5612
5613         if (dbus_bus_name_has_owner(connection,
5614                                         SUPPLICANT_SERVICE, NULL)) {
5615                 system_available = TRUE;
5616                 supplicant_dbus_property_get_all(SUPPLICANT_PATH,
5617                                                 SUPPLICANT_INTERFACE,
5618                                                 service_property, NULL, NULL);
5619         } else
5620                 invoke_introspect_method();
5621
5622         return 0;
5623 }
5624
5625 static void unregister_interface_remove_params(DBusMessageIter *iter,
5626                                                 void *user_data)
5627 {
5628         const char *path = user_data;
5629
5630         dbus_message_iter_append_basic(iter, DBUS_TYPE_OBJECT_PATH,
5631                                                         &path);
5632 }
5633
5634
5635 static void unregister_remove_interface(gpointer key, gpointer value,
5636                                                 gpointer user_data)
5637 {
5638         GSupplicantInterface *interface = value;
5639
5640         supplicant_dbus_method_call(SUPPLICANT_PATH,
5641                                         SUPPLICANT_INTERFACE,
5642                                         "RemoveInterface",
5643                                         unregister_interface_remove_params,
5644                                         NULL, interface->path, NULL);
5645 }
5646
5647 void g_supplicant_unregister(const GSupplicantCallbacks *callbacks)
5648 {
5649         SUPPLICANT_DBG("");
5650
5651         if (connection) {
5652 #if !defined TIZEN_EXT
5653                 dbus_bus_remove_match(connection, g_supplicant_rule8, NULL);
5654                 dbus_bus_remove_match(connection, g_supplicant_rule7, NULL);
5655                 dbus_bus_remove_match(connection, g_supplicant_rule6, NULL);
5656 #endif
5657                 dbus_bus_remove_match(connection, g_supplicant_rule5, NULL);
5658                 dbus_bus_remove_match(connection, g_supplicant_rule4, NULL);
5659                 dbus_bus_remove_match(connection, g_supplicant_rule3, NULL);
5660                 dbus_bus_remove_match(connection, g_supplicant_rule2, NULL);
5661                 dbus_bus_remove_match(connection, g_supplicant_rule1, NULL);
5662                 dbus_bus_remove_match(connection, g_supplicant_rule0, NULL);
5663                 dbus_connection_flush(connection);
5664
5665                 dbus_connection_remove_filter(connection,
5666                                                 g_supplicant_filter, NULL);
5667         }
5668
5669         if (bss_mapping) {
5670                 g_hash_table_destroy(bss_mapping);
5671                 bss_mapping = NULL;
5672         }
5673
5674         if (peer_mapping) {
5675                 g_hash_table_destroy(peer_mapping);
5676                 peer_mapping = NULL;
5677         }
5678
5679         if (group_mapping) {
5680                 g_hash_table_destroy(group_mapping);
5681                 group_mapping = NULL;
5682         }
5683
5684         if (interface_table) {
5685                 g_hash_table_foreach(interface_table,
5686                                         unregister_remove_interface, NULL);
5687                 g_hash_table_destroy(interface_table);
5688                 interface_table = NULL;
5689         }
5690
5691         if (system_available)
5692                 callback_system_killed();
5693
5694         if (connection) {
5695                 dbus_connection_unref(connection);
5696                 connection = NULL;
5697         }
5698
5699         callbacks_pointer = NULL;
5700         eap_methods = 0;
5701 }