dbus_new: Add DBus TDLS methods
[mech_eap.git] / wpa_supplicant / dbus / dbus_new_handlers.c
1 /*
2  * WPA Supplicant / dbus-based control interface
3  * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
4  * Copyright (c) 2009-2010, Witold Sowa <witold.sowa@gmail.com>
5  * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
6  *
7  * This software may be distributed under the terms of the BSD license.
8  * See README for more details.
9  */
10
11 #include "includes.h"
12
13 #include "common.h"
14 #include "common/ieee802_11_defs.h"
15 #include "eap_peer/eap_methods.h"
16 #include "eapol_supp/eapol_supp_sm.h"
17 #include "rsn_supp/wpa.h"
18 #include "../config.h"
19 #include "../wpa_supplicant_i.h"
20 #include "../driver_i.h"
21 #include "../notify.h"
22 #include "../bss.h"
23 #include "../scan.h"
24 #include "../autoscan.h"
25 #include "dbus_new_helpers.h"
26 #include "dbus_new.h"
27 #include "dbus_new_handlers.h"
28 #include "dbus_dict_helpers.h"
29 #include "dbus_common_i.h"
30
31 extern int wpa_debug_level;
32 extern int wpa_debug_show_keys;
33 extern int wpa_debug_timestamp;
34
35 static const char *debug_strings[] = {
36         "excessive", "msgdump", "debug", "info", "warning", "error", NULL
37 };
38
39
40 /**
41  * wpas_dbus_error_unknown_error - Return a new InvalidArgs error message
42  * @message: Pointer to incoming dbus message this error refers to
43  * @arg: Optional string appended to error message
44  * Returns: a dbus error message
45  *
46  * Convenience function to create and return an UnknownError
47  */
48 DBusMessage * wpas_dbus_error_unknown_error(DBusMessage *message,
49                                             const char *arg)
50 {
51         /*
52          * This function can be called as a result of a failure
53          * within internal getter calls, which will call this function
54          * with a NULL message parameter.  However, dbus_message_new_error
55          * looks very unkindly (i.e, abort()) on a NULL message, so
56          * in this case, we should not call it.
57          */
58         if (message == NULL) {
59                 wpa_printf(MSG_INFO, "dbus: wpas_dbus_error_unknown_error "
60                            "called with NULL message (arg=%s)",
61                            arg ? arg : "N/A");
62                 return NULL;
63         }
64
65         return dbus_message_new_error(message, WPAS_DBUS_ERROR_UNKNOWN_ERROR,
66                                       arg);
67 }
68
69
70 /**
71  * wpas_dbus_error_iface_unknown - Return a new invalid interface error message
72  * @message: Pointer to incoming dbus message this error refers to
73  * Returns: A dbus error message
74  *
75  * Convenience function to create and return an invalid interface error
76  */
77 static DBusMessage * wpas_dbus_error_iface_unknown(DBusMessage *message)
78 {
79         return dbus_message_new_error(message, WPAS_DBUS_ERROR_IFACE_UNKNOWN,
80                                       "wpa_supplicant knows nothing about "
81                                       "this interface.");
82 }
83
84
85 /**
86  * wpas_dbus_error_network_unknown - Return a new NetworkUnknown error message
87  * @message: Pointer to incoming dbus message this error refers to
88  * Returns: a dbus error message
89  *
90  * Convenience function to create and return an invalid network error
91  */
92 static DBusMessage * wpas_dbus_error_network_unknown(DBusMessage *message)
93 {
94         return dbus_message_new_error(message, WPAS_DBUS_ERROR_NETWORK_UNKNOWN,
95                                       "There is no such a network in this "
96                                       "interface.");
97 }
98
99
100 /**
101  * wpas_dbus_error_invalid_args - Return a new InvalidArgs error message
102  * @message: Pointer to incoming dbus message this error refers to
103  * Returns: a dbus error message
104  *
105  * Convenience function to create and return an invalid options error
106  */
107 DBusMessage * wpas_dbus_error_invalid_args(DBusMessage *message,
108                                           const char *arg)
109 {
110         DBusMessage *reply;
111
112         reply = dbus_message_new_error(message, WPAS_DBUS_ERROR_INVALID_ARGS,
113                                        "Did not receive correct message "
114                                        "arguments.");
115         if (arg != NULL)
116                 dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
117                                          DBUS_TYPE_INVALID);
118
119         return reply;
120 }
121
122
123 static const char *dont_quote[] = {
124         "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
125         "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
126         "bssid", "scan_freq", "freq_list", NULL
127 };
128
129 static dbus_bool_t should_quote_opt(const char *key)
130 {
131         int i = 0;
132         while (dont_quote[i] != NULL) {
133                 if (os_strcmp(key, dont_quote[i]) == 0)
134                         return FALSE;
135                 i++;
136         }
137         return TRUE;
138 }
139
140 /**
141  * get_iface_by_dbus_path - Get a new network interface
142  * @global: Pointer to global data from wpa_supplicant_init()
143  * @path: Pointer to a dbus object path representing an interface
144  * Returns: Pointer to the interface or %NULL if not found
145  */
146 static struct wpa_supplicant * get_iface_by_dbus_path(
147         struct wpa_global *global, const char *path)
148 {
149         struct wpa_supplicant *wpa_s;
150
151         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
152                 if (os_strcmp(wpa_s->dbus_new_path, path) == 0)
153                         return wpa_s;
154         }
155         return NULL;
156 }
157
158
159 /**
160  * set_network_properties - Set properties of a configured network
161  * @wpa_s: wpa_supplicant structure for a network interface
162  * @ssid: wpa_ssid structure for a configured network
163  * @iter: DBus message iterator containing dictionary of network
164  * properties to set.
165  * @error: On failure, an error describing the failure
166  * Returns: TRUE if the request succeeds, FALSE if it failed
167  *
168  * Sets network configuration with parameters given id DBus dictionary
169  */
170 dbus_bool_t set_network_properties(struct wpa_supplicant *wpa_s,
171                                    struct wpa_ssid *ssid,
172                                    DBusMessageIter *iter,
173                                    DBusError *error)
174 {
175         struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
176         DBusMessageIter iter_dict;
177         char *value = NULL;
178
179         if (!wpa_dbus_dict_open_read(iter, &iter_dict, error))
180                 return FALSE;
181
182         while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
183                 size_t size = 50;
184                 int ret;
185
186                 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
187                         goto error;
188
189                 value = NULL;
190                 if (entry.type == DBUS_TYPE_ARRAY &&
191                     entry.array_type == DBUS_TYPE_BYTE) {
192                         if (entry.array_len <= 0)
193                                 goto error;
194
195                         size = entry.array_len * 2 + 1;
196                         value = os_zalloc(size);
197                         if (value == NULL)
198                                 goto error;
199
200                         ret = wpa_snprintf_hex(value, size,
201                                                (u8 *) entry.bytearray_value,
202                                                entry.array_len);
203                         if (ret <= 0)
204                                 goto error;
205                 } else if (entry.type == DBUS_TYPE_STRING) {
206                         if (should_quote_opt(entry.key)) {
207                                 size = os_strlen(entry.str_value);
208                                 if (size <= 0)
209                                         goto error;
210
211                                 size += 3;
212                                 value = os_zalloc(size);
213                                 if (value == NULL)
214                                         goto error;
215
216                                 ret = os_snprintf(value, size, "\"%s\"",
217                                                   entry.str_value);
218                                 if (ret < 0 || (size_t) ret != (size - 1))
219                                         goto error;
220                         } else {
221                                 value = os_strdup(entry.str_value);
222                                 if (value == NULL)
223                                         goto error;
224                         }
225                 } else if (entry.type == DBUS_TYPE_UINT32) {
226                         value = os_zalloc(size);
227                         if (value == NULL)
228                                 goto error;
229
230                         ret = os_snprintf(value, size, "%u",
231                                           entry.uint32_value);
232                         if (ret <= 0)
233                                 goto error;
234                 } else if (entry.type == DBUS_TYPE_INT32) {
235                         value = os_zalloc(size);
236                         if (value == NULL)
237                                 goto error;
238
239                         ret = os_snprintf(value, size, "%d",
240                                           entry.int32_value);
241                         if (ret <= 0)
242                                 goto error;
243                 } else
244                         goto error;
245
246                 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
247                         goto error;
248
249                 if ((os_strcmp(entry.key, "psk") == 0 &&
250                      value[0] == '"' && ssid->ssid_len) ||
251                     (os_strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
252                         wpa_config_update_psk(ssid);
253                 else if (os_strcmp(entry.key, "priority") == 0)
254                         wpa_config_update_prio_list(wpa_s->conf);
255
256                 os_free(value);
257                 wpa_dbus_dict_entry_clear(&entry);
258         }
259
260         return TRUE;
261
262 error:
263         os_free(value);
264         wpa_dbus_dict_entry_clear(&entry);
265         dbus_set_error_const(error, DBUS_ERROR_INVALID_ARGS,
266                              "invalid message format");
267         return FALSE;
268 }
269
270
271 /**
272  * wpas_dbus_simple_property_getter - Get basic type property
273  * @iter: Message iter to use when appending arguments
274  * @type: DBus type of property (must be basic type)
275  * @val: pointer to place holding property value
276  * @error: On failure an error describing the failure
277  * Returns: TRUE if the request was successful, FALSE if it failed
278  *
279  * Generic getter for basic type properties. Type is required to be basic.
280  */
281 dbus_bool_t wpas_dbus_simple_property_getter(DBusMessageIter *iter,
282                                              const int type,
283                                              const void *val,
284                                              DBusError *error)
285 {
286         DBusMessageIter variant_iter;
287
288         if (!dbus_type_is_basic(type)) {
289                 dbus_set_error(error, DBUS_ERROR_FAILED,
290                                "%s: given type is not basic", __func__);
291                 return FALSE;
292         }
293
294         if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
295                                               wpa_dbus_type_as_string(type),
296                                               &variant_iter))
297                 goto error;
298
299         if (!dbus_message_iter_append_basic(&variant_iter, type, val))
300                 goto error;
301
302         if (!dbus_message_iter_close_container(iter, &variant_iter))
303                 goto error;
304
305         return TRUE;
306
307 error:
308         dbus_set_error(error, DBUS_ERROR_FAILED,
309                        "%s: error constructing reply", __func__);
310         return FALSE;
311 }
312
313
314 /**
315  * wpas_dbus_simple_property_setter - Set basic type property
316  * @message: Pointer to incoming dbus message
317  * @type: DBus type of property (must be basic type)
318  * @val: pointer to place where value being set will be stored
319  * Returns: TRUE if the request was successful, FALSE if it failed
320  *
321  * Generic setter for basic type properties. Type is required to be basic.
322  */
323 dbus_bool_t wpas_dbus_simple_property_setter(DBusMessageIter *iter,
324                                              DBusError *error,
325                                              const int type, void *val)
326 {
327         DBusMessageIter variant_iter;
328
329         if (!dbus_type_is_basic(type)) {
330                 dbus_set_error(error, DBUS_ERROR_FAILED,
331                                "%s: given type is not basic", __func__);
332                 return FALSE;
333         }
334
335         /* Look at the new value */
336         dbus_message_iter_recurse(iter, &variant_iter);
337         if (dbus_message_iter_get_arg_type(&variant_iter) != type) {
338                 dbus_set_error_const(error, DBUS_ERROR_FAILED,
339                                      "wrong property type");
340                 return FALSE;
341         }
342         dbus_message_iter_get_basic(&variant_iter, val);
343
344         return TRUE;
345 }
346
347
348 /**
349  * wpas_dbus_simple_array_property_getter - Get array type property
350  * @iter: Pointer to incoming dbus message iterator
351  * @type: DBus type of property array elements (must be basic type)
352  * @array: pointer to array of elements to put into response message
353  * @array_len: length of above array
354  * @error: a pointer to an error to fill on failure
355  * Returns: TRUE if the request succeeded, FALSE if it failed
356  *
357  * Generic getter for array type properties. Array elements type is
358  * required to be basic.
359  */
360 dbus_bool_t wpas_dbus_simple_array_property_getter(DBusMessageIter *iter,
361                                                    const int type,
362                                                    const void *array,
363                                                    size_t array_len,
364                                                    DBusError *error)
365 {
366         DBusMessageIter variant_iter, array_iter;
367         char type_str[] = "a?"; /* ? will be replaced with subtype letter; */
368         const char *sub_type_str;
369         size_t element_size, i;
370
371         if (!dbus_type_is_basic(type)) {
372                 dbus_set_error(error, DBUS_ERROR_FAILED,
373                                "%s: given type is not basic", __func__);
374                 return FALSE;
375         }
376
377         sub_type_str = wpa_dbus_type_as_string(type);
378         type_str[1] = sub_type_str[0];
379
380         if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
381                                               type_str, &variant_iter)) {
382                 dbus_set_error(error, DBUS_ERROR_FAILED,
383                                "%s: failed to construct message 1", __func__);
384                 return FALSE;
385         }
386
387         if (!dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
388                                               sub_type_str, &array_iter)) {
389                 dbus_set_error(error, DBUS_ERROR_FAILED,
390                                "%s: failed to construct message 2", __func__);
391                 return FALSE;
392         }
393
394         switch(type) {
395         case DBUS_TYPE_BYTE:
396         case DBUS_TYPE_BOOLEAN:
397                 element_size = 1;
398                 break;
399         case DBUS_TYPE_INT16:
400         case DBUS_TYPE_UINT16:
401                 element_size = sizeof(uint16_t);
402                 break;
403         case DBUS_TYPE_INT32:
404         case DBUS_TYPE_UINT32:
405                 element_size = sizeof(uint32_t);
406                 break;
407         case DBUS_TYPE_INT64:
408         case DBUS_TYPE_UINT64:
409                 element_size = sizeof(uint64_t);
410                 break;
411         case DBUS_TYPE_DOUBLE:
412                 element_size = sizeof(double);
413                 break;
414         case DBUS_TYPE_STRING:
415         case DBUS_TYPE_OBJECT_PATH:
416                 element_size = sizeof(char *);
417                 break;
418         default:
419                 dbus_set_error(error, DBUS_ERROR_FAILED,
420                                "%s: unknown element type %d", __func__, type);
421                 return FALSE;
422         }
423
424         for (i = 0; i < array_len; i++) {
425                 dbus_message_iter_append_basic(&array_iter, type,
426                                                array + i * element_size);
427         }
428
429         if (!dbus_message_iter_close_container(&variant_iter, &array_iter)) {
430                 dbus_set_error(error, DBUS_ERROR_FAILED,
431                                "%s: failed to construct message 3", __func__);
432                 return FALSE;
433         }
434
435         if (!dbus_message_iter_close_container(iter, &variant_iter)) {
436                 dbus_set_error(error, DBUS_ERROR_FAILED,
437                                "%s: failed to construct message 4", __func__);
438                 return FALSE;
439         }
440
441         return TRUE;
442 }
443
444
445 /**
446  * wpas_dbus_simple_array_array_property_getter - Get array array type property
447  * @iter: Pointer to incoming dbus message iterator
448  * @type: DBus type of property array elements (must be basic type)
449  * @array: pointer to array of elements to put into response message
450  * @array_len: length of above array
451  * @error: a pointer to an error to fill on failure
452  * Returns: TRUE if the request succeeded, FALSE if it failed
453  *
454  * Generic getter for array type properties. Array elements type is
455  * required to be basic.
456  */
457 dbus_bool_t wpas_dbus_simple_array_array_property_getter(DBusMessageIter *iter,
458                                                          const int type,
459                                                          struct wpabuf **array,
460                                                          size_t array_len,
461                                                          DBusError *error)
462 {
463         DBusMessageIter variant_iter, array_iter;
464         char type_str[] = "aa?";
465         char inner_type_str[] = "a?";
466         const char *sub_type_str;
467         size_t i;
468
469         if (!dbus_type_is_basic(type)) {
470                 dbus_set_error(error, DBUS_ERROR_FAILED,
471                                "%s: given type is not basic", __func__);
472                 return FALSE;
473         }
474
475         sub_type_str = wpa_dbus_type_as_string(type);
476         type_str[2] = sub_type_str[0];
477         inner_type_str[1] = sub_type_str[0];
478
479         if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
480                                               type_str, &variant_iter)) {
481                 dbus_set_error(error, DBUS_ERROR_FAILED,
482                                "%s: failed to construct message 1", __func__);
483                 return FALSE;
484         }
485         if (!dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
486                                               inner_type_str, &array_iter)) {
487                 dbus_set_error(error, DBUS_ERROR_FAILED,
488                                "%s: failed to construct message 2", __func__);
489                 return FALSE;
490         }
491
492         for (i = 0; i < array_len; i++) {
493                 wpa_dbus_dict_bin_array_add_element(&array_iter,
494                                                     wpabuf_head(array[i]),
495                                                     wpabuf_len(array[i]));
496
497         }
498
499         if (!dbus_message_iter_close_container(&variant_iter, &array_iter)) {
500                 dbus_set_error(error, DBUS_ERROR_FAILED,
501                                "%s: failed to close message 2", __func__);
502                 return FALSE;
503         }
504
505         if (!dbus_message_iter_close_container(iter, &variant_iter)) {
506                 dbus_set_error(error, DBUS_ERROR_FAILED,
507                                "%s: failed to close message 1", __func__);
508                 return FALSE;
509         }
510
511         return TRUE;
512 }
513
514
515 /**
516  * wpas_dbus_handler_create_interface - Request registration of a network iface
517  * @message: Pointer to incoming dbus message
518  * @global: %wpa_supplicant global data structure
519  * Returns: The object path of the new interface object,
520  *          or a dbus error message with more information
521  *
522  * Handler function for "CreateInterface" method call. Handles requests
523  * by dbus clients to register a network interface that wpa_supplicant
524  * will manage.
525  */
526 DBusMessage * wpas_dbus_handler_create_interface(DBusMessage *message,
527                                                  struct wpa_global *global)
528 {
529         DBusMessageIter iter_dict;
530         DBusMessage *reply = NULL;
531         DBusMessageIter iter;
532         struct wpa_dbus_dict_entry entry;
533         char *driver = NULL;
534         char *ifname = NULL;
535         char *confname = NULL;
536         char *bridge_ifname = NULL;
537
538         dbus_message_iter_init(message, &iter);
539
540         if (!wpa_dbus_dict_open_read(&iter, &iter_dict, NULL))
541                 goto error;
542         while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
543                 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
544                         goto error;
545                 if (!os_strcmp(entry.key, "Driver") &&
546                     (entry.type == DBUS_TYPE_STRING)) {
547                         driver = os_strdup(entry.str_value);
548                         wpa_dbus_dict_entry_clear(&entry);
549                         if (driver == NULL)
550                                 goto error;
551                 } else if (!os_strcmp(entry.key, "Ifname") &&
552                            (entry.type == DBUS_TYPE_STRING)) {
553                         ifname = os_strdup(entry.str_value);
554                         wpa_dbus_dict_entry_clear(&entry);
555                         if (ifname == NULL)
556                                 goto error;
557                 } else if (!os_strcmp(entry.key, "ConfigFile") &&
558                            (entry.type == DBUS_TYPE_STRING)) {
559                         confname = os_strdup(entry.str_value);
560                         wpa_dbus_dict_entry_clear(&entry);
561                         if (confname == NULL)
562                                 goto error;
563                 } else if (!os_strcmp(entry.key, "BridgeIfname") &&
564                            (entry.type == DBUS_TYPE_STRING)) {
565                         bridge_ifname = os_strdup(entry.str_value);
566                         wpa_dbus_dict_entry_clear(&entry);
567                         if (bridge_ifname == NULL)
568                                 goto error;
569                 } else {
570                         wpa_dbus_dict_entry_clear(&entry);
571                         goto error;
572                 }
573         }
574
575         if (ifname == NULL)
576                 goto error; /* Required Ifname argument missing */
577
578         /*
579          * Try to get the wpa_supplicant record for this iface, return
580          * an error if we already control it.
581          */
582         if (wpa_supplicant_get_iface(global, ifname) != NULL) {
583                 reply = dbus_message_new_error(message,
584                                                WPAS_DBUS_ERROR_IFACE_EXISTS,
585                                                "wpa_supplicant already "
586                                                "controls this interface.");
587         } else {
588                 struct wpa_supplicant *wpa_s;
589                 struct wpa_interface iface;
590                 os_memset(&iface, 0, sizeof(iface));
591                 iface.driver = driver;
592                 iface.ifname = ifname;
593                 iface.confname = confname;
594                 iface.bridge_ifname = bridge_ifname;
595                 /* Otherwise, have wpa_supplicant attach to it. */
596                 if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
597                         const char *path = wpa_s->dbus_new_path;
598                         reply = dbus_message_new_method_return(message);
599                         dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
600                                                  &path, DBUS_TYPE_INVALID);
601                 } else {
602                         reply = wpas_dbus_error_unknown_error(
603                                 message, "wpa_supplicant couldn't grab this "
604                                 "interface.");
605                 }
606         }
607
608 out:
609         os_free(driver);
610         os_free(ifname);
611         os_free(confname);
612         os_free(bridge_ifname);
613         return reply;
614
615 error:
616         reply = wpas_dbus_error_invalid_args(message, NULL);
617         goto out;
618 }
619
620
621 /**
622  * wpas_dbus_handler_remove_interface - Request deregistration of an interface
623  * @message: Pointer to incoming dbus message
624  * @global: wpa_supplicant global data structure
625  * Returns: a dbus message containing a UINT32 indicating success (1) or
626  *          failure (0), or returns a dbus error message with more information
627  *
628  * Handler function for "removeInterface" method call.  Handles requests
629  * by dbus clients to deregister a network interface that wpa_supplicant
630  * currently manages.
631  */
632 DBusMessage * wpas_dbus_handler_remove_interface(DBusMessage *message,
633                                                  struct wpa_global *global)
634 {
635         struct wpa_supplicant *wpa_s;
636         char *path;
637         DBusMessage *reply = NULL;
638
639         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
640                               DBUS_TYPE_INVALID);
641
642         wpa_s = get_iface_by_dbus_path(global, path);
643         if (wpa_s == NULL)
644                 reply = wpas_dbus_error_iface_unknown(message);
645         else if (wpa_supplicant_remove_iface(global, wpa_s, 0)) {
646                 reply = wpas_dbus_error_unknown_error(
647                         message, "wpa_supplicant couldn't remove this "
648                         "interface.");
649         }
650
651         return reply;
652 }
653
654
655 /**
656  * wpas_dbus_handler_get_interface - Get the object path for an interface name
657  * @message: Pointer to incoming dbus message
658  * @global: %wpa_supplicant global data structure
659  * Returns: The object path of the interface object,
660  *          or a dbus error message with more information
661  *
662  * Handler function for "getInterface" method call.
663  */
664 DBusMessage * wpas_dbus_handler_get_interface(DBusMessage *message,
665                                               struct wpa_global *global)
666 {
667         DBusMessage *reply = NULL;
668         const char *ifname;
669         const char *path;
670         struct wpa_supplicant *wpa_s;
671
672         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &ifname,
673                               DBUS_TYPE_INVALID);
674
675         wpa_s = wpa_supplicant_get_iface(global, ifname);
676         if (wpa_s == NULL)
677                 return wpas_dbus_error_iface_unknown(message);
678
679         path = wpa_s->dbus_new_path;
680         reply = dbus_message_new_method_return(message);
681         if (reply == NULL)
682                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
683                                               NULL);
684         if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
685                                       DBUS_TYPE_INVALID)) {
686                 dbus_message_unref(reply);
687                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
688                                               NULL);
689         }
690
691         return reply;
692 }
693
694
695 /**
696  * wpas_dbus_getter_debug_level - Get debug level
697  * @iter: Pointer to incoming dbus message iter
698  * @error: Location to store error on failure
699  * @user_data: Function specific data
700  * Returns: TRUE on success, FALSE on failure
701  *
702  * Getter for "DebugLevel" property.
703  */
704 dbus_bool_t wpas_dbus_getter_debug_level(DBusMessageIter *iter,
705                                          DBusError *error,
706                                          void *user_data)
707 {
708         const char *str;
709         int idx = wpa_debug_level;
710
711         if (idx < 0)
712                 idx = 0;
713         if (idx > 5)
714                 idx = 5;
715         str = debug_strings[idx];
716         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
717                                                 &str, error);
718 }
719
720
721 /**
722  * wpas_dbus_getter_debug_timestamp - Get debug timestamp
723  * @iter: Pointer to incoming dbus message iter
724  * @error: Location to store error on failure
725  * @user_data: Function specific data
726  * Returns: TRUE on success, FALSE on failure
727  *
728  * Getter for "DebugTimestamp" property.
729  */
730 dbus_bool_t wpas_dbus_getter_debug_timestamp(DBusMessageIter *iter,
731                                              DBusError *error,
732                                              void *user_data)
733 {
734         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
735                                                 &wpa_debug_timestamp, error);
736
737 }
738
739
740 /**
741  * wpas_dbus_getter_debug_show_keys - Get debug show keys
742  * @iter: Pointer to incoming dbus message iter
743  * @error: Location to store error on failure
744  * @user_data: Function specific data
745  * Returns: TRUE on success, FALSE on failure
746  *
747  * Getter for "DebugShowKeys" property.
748  */
749 dbus_bool_t wpas_dbus_getter_debug_show_keys(DBusMessageIter *iter,
750                                              DBusError *error,
751                                              void *user_data)
752 {
753         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
754                                                 &wpa_debug_show_keys, error);
755
756 }
757
758 /**
759  * wpas_dbus_setter_debug_level - Set debug level
760  * @iter: Pointer to incoming dbus message iter
761  * @error: Location to store error on failure
762  * @user_data: Function specific data
763  * Returns: TRUE on success, FALSE on failure
764  *
765  * Setter for "DebugLevel" property.
766  */
767 dbus_bool_t wpas_dbus_setter_debug_level(DBusMessageIter *iter,
768                                          DBusError *error, void *user_data)
769 {
770         struct wpa_global *global = user_data;
771         const char *str = NULL;
772         int i, val = -1;
773
774         if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
775                                               &str))
776                 return FALSE;
777
778         for (i = 0; debug_strings[i]; i++)
779                 if (os_strcmp(debug_strings[i], str) == 0) {
780                         val = i;
781                         break;
782                 }
783
784         if (val < 0 ||
785             wpa_supplicant_set_debug_params(global, val, wpa_debug_timestamp,
786                                             wpa_debug_show_keys)) {
787                 dbus_set_error_const(error, DBUS_ERROR_FAILED, "wrong debug "
788                                      "level value");
789                 return FALSE;
790         }
791
792         return TRUE;
793 }
794
795
796 /**
797  * wpas_dbus_setter_debug_timestamp - Set debug timestamp
798  * @iter: Pointer to incoming dbus message iter
799  * @error: Location to store error on failure
800  * @user_data: Function specific data
801  * Returns: TRUE on success, FALSE on failure
802  *
803  * Setter for "DebugTimestamp" property.
804  */
805 dbus_bool_t wpas_dbus_setter_debug_timestamp(DBusMessageIter *iter,
806                                              DBusError *error,
807                                              void *user_data)
808 {
809         struct wpa_global *global = user_data;
810         dbus_bool_t val;
811
812         if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
813                                               &val))
814                 return FALSE;
815
816         wpa_supplicant_set_debug_params(global, wpa_debug_level, val ? 1 : 0,
817                                         wpa_debug_show_keys);
818         return TRUE;
819 }
820
821
822 /**
823  * wpas_dbus_setter_debug_show_keys - Set debug show keys
824  * @iter: Pointer to incoming dbus message iter
825  * @error: Location to store error on failure
826  * @user_data: Function specific data
827  * Returns: TRUE on success, FALSE on failure
828  *
829  * Setter for "DebugShowKeys" property.
830  */
831 dbus_bool_t wpas_dbus_setter_debug_show_keys(DBusMessageIter *iter,
832                                              DBusError *error,
833                                              void *user_data)
834 {
835         struct wpa_global *global = user_data;
836         dbus_bool_t val;
837
838         if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
839                                               &val))
840                 return FALSE;
841
842         wpa_supplicant_set_debug_params(global, wpa_debug_level,
843                                         wpa_debug_timestamp,
844                                         val ? 1 : 0);
845         return TRUE;
846 }
847
848
849 /**
850  * wpas_dbus_getter_interfaces - Request registered interfaces list
851  * @iter: Pointer to incoming dbus message iter
852  * @error: Location to store error on failure
853  * @user_data: Function specific data
854  * Returns: TRUE on success, FALSE on failure
855  *
856  * Getter for "Interfaces" property. Handles requests
857  * by dbus clients to return list of registered interfaces objects
858  * paths
859  */
860 dbus_bool_t wpas_dbus_getter_interfaces(DBusMessageIter *iter,
861                                         DBusError *error,
862                                         void *user_data)
863 {
864         struct wpa_global *global = user_data;
865         struct wpa_supplicant *wpa_s;
866         const char **paths;
867         unsigned int i = 0, num = 0;
868         dbus_bool_t success;
869
870         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
871                 num++;
872
873         paths = os_calloc(num, sizeof(char *));
874         if (!paths) {
875                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
876                 return FALSE;
877         }
878
879         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
880                 paths[i++] = wpa_s->dbus_new_path;
881
882         success = wpas_dbus_simple_array_property_getter(iter,
883                                                          DBUS_TYPE_OBJECT_PATH,
884                                                          paths, num, error);
885
886         os_free(paths);
887         return success;
888 }
889
890
891 /**
892  * wpas_dbus_getter_eap_methods - Request supported EAP methods list
893  * @iter: Pointer to incoming dbus message iter
894  * @error: Location to store error on failure
895  * @user_data: Function specific data
896  * Returns: TRUE on success, FALSE on failure
897  *
898  * Getter for "EapMethods" property. Handles requests
899  * by dbus clients to return list of strings with supported EAP methods
900  */
901 dbus_bool_t wpas_dbus_getter_eap_methods(DBusMessageIter *iter,
902                                          DBusError *error, void *user_data)
903 {
904         char **eap_methods;
905         size_t num_items = 0;
906         dbus_bool_t success;
907
908         eap_methods = eap_get_names_as_string_array(&num_items);
909         if (!eap_methods) {
910                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
911                 return FALSE;
912         }
913
914         success = wpas_dbus_simple_array_property_getter(iter,
915                                                          DBUS_TYPE_STRING,
916                                                          eap_methods,
917                                                          num_items, error);
918
919         while (num_items)
920                 os_free(eap_methods[--num_items]);
921         os_free(eap_methods);
922         return success;
923 }
924
925
926 /**
927  * wpas_dbus_getter_global_capabilities - Request supported global capabilities
928  * @iter: Pointer to incoming dbus message iter
929  * @error: Location to store error on failure
930  * @user_data: Function specific data
931  * Returns: TRUE on success, FALSE on failure
932  *
933  * Getter for "Capabilities" property. Handles requests by dbus clients to
934  * return a list of strings with supported capabilities like AP, RSN IBSS,
935  * and P2P that are determined at compile time.
936  */
937 dbus_bool_t wpas_dbus_getter_global_capabilities(DBusMessageIter *iter,
938                                                  DBusError *error,
939                                                  void *user_data)
940 {
941         const char *capabilities[5] = { NULL, NULL, NULL, NULL, NULL };
942         size_t num_items = 0;
943
944 #ifdef CONFIG_AP
945         capabilities[num_items++] = "ap";
946 #endif /* CONFIG_AP */
947 #ifdef CONFIG_IBSS_RSN
948         capabilities[num_items++] = "ibss-rsn";
949 #endif /* CONFIG_IBSS_RSN */
950 #ifdef CONFIG_P2P
951         capabilities[num_items++] = "p2p";
952 #endif /* CONFIG_P2P */
953 #ifdef CONFIG_INTERWORKING
954         capabilities[num_items++] = "interworking";
955 #endif /* CONFIG_INTERWORKING */
956
957         return wpas_dbus_simple_array_property_getter(iter,
958                                                       DBUS_TYPE_STRING,
959                                                       capabilities,
960                                                       num_items, error);
961 }
962
963
964 static int wpas_dbus_get_scan_type(DBusMessage *message, DBusMessageIter *var,
965                                    char **type, DBusMessage **reply)
966 {
967         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_STRING) {
968                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
969                            "Type must be a string");
970                 *reply = wpas_dbus_error_invalid_args(
971                         message, "Wrong Type value type. String required");
972                 return -1;
973         }
974         dbus_message_iter_get_basic(var, type);
975         return 0;
976 }
977
978
979 static int wpas_dbus_get_scan_ssids(DBusMessage *message, DBusMessageIter *var,
980                                     struct wpa_driver_scan_params *params,
981                                     DBusMessage **reply)
982 {
983         struct wpa_driver_scan_ssid *ssids = params->ssids;
984         size_t ssids_num = 0;
985         u8 *ssid;
986         DBusMessageIter array_iter, sub_array_iter;
987         char *val;
988         int len;
989
990         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
991                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
992                            "must be an array of arrays of bytes");
993                 *reply = wpas_dbus_error_invalid_args(
994                         message, "Wrong SSIDs value type. Array of arrays of "
995                         "bytes required");
996                 return -1;
997         }
998
999         dbus_message_iter_recurse(var, &array_iter);
1000
1001         if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
1002             dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
1003         {
1004                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
1005                            "must be an array of arrays of bytes");
1006                 *reply = wpas_dbus_error_invalid_args(
1007                         message, "Wrong SSIDs value type. Array of arrays of "
1008                         "bytes required");
1009                 return -1;
1010         }
1011
1012         while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
1013         {
1014                 if (ssids_num >= WPAS_MAX_SCAN_SSIDS) {
1015                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1016                                    "Too many ssids specified on scan dbus "
1017                                    "call");
1018                         *reply = wpas_dbus_error_invalid_args(
1019                                 message, "Too many ssids specified. Specify "
1020                                 "at most four");
1021                         return -1;
1022                 }
1023
1024                 dbus_message_iter_recurse(&array_iter, &sub_array_iter);
1025
1026                 dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
1027
1028                 if (len > MAX_SSID_LEN) {
1029                         wpa_printf(MSG_DEBUG,
1030                                    "wpas_dbus_handler_scan[dbus]: "
1031                                    "SSID too long (len=%d max_len=%d)",
1032                                    len, MAX_SSID_LEN);
1033                         *reply = wpas_dbus_error_invalid_args(
1034                                 message, "Invalid SSID: too long");
1035                         return -1;
1036                 }
1037
1038                 if (len != 0) {
1039                         ssid = os_malloc(len);
1040                         if (ssid == NULL) {
1041                                 wpa_printf(MSG_DEBUG,
1042                                            "wpas_dbus_handler_scan[dbus]: "
1043                                            "out of memory. Cannot allocate "
1044                                            "memory for SSID");
1045                                 *reply = dbus_message_new_error(
1046                                         message, DBUS_ERROR_NO_MEMORY, NULL);
1047                                 return -1;
1048                         }
1049                         os_memcpy(ssid, val, len);
1050                 } else {
1051                         /* Allow zero-length SSIDs */
1052                         ssid = NULL;
1053                 }
1054
1055                 ssids[ssids_num].ssid = ssid;
1056                 ssids[ssids_num].ssid_len = len;
1057
1058                 dbus_message_iter_next(&array_iter);
1059                 ssids_num++;
1060         }
1061
1062         params->num_ssids = ssids_num;
1063         return 0;
1064 }
1065
1066
1067 static int wpas_dbus_get_scan_ies(DBusMessage *message, DBusMessageIter *var,
1068                                   struct wpa_driver_scan_params *params,
1069                                   DBusMessage **reply)
1070 {
1071         u8 *ies = NULL, *nies;
1072         int ies_len = 0;
1073         DBusMessageIter array_iter, sub_array_iter;
1074         char *val;
1075         int len;
1076
1077         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
1078                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
1079                            "be an array of arrays of bytes");
1080                 *reply = wpas_dbus_error_invalid_args(
1081                         message, "Wrong IEs value type. Array of arrays of "
1082                         "bytes required");
1083                 return -1;
1084         }
1085
1086         dbus_message_iter_recurse(var, &array_iter);
1087
1088         if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
1089             dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
1090         {
1091                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
1092                            "be an array of arrays of bytes");
1093                 *reply = wpas_dbus_error_invalid_args(
1094                         message, "Wrong IEs value type. Array required");
1095                 return -1;
1096         }
1097
1098         while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
1099         {
1100                 dbus_message_iter_recurse(&array_iter, &sub_array_iter);
1101
1102                 dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
1103                 if (len == 0) {
1104                         dbus_message_iter_next(&array_iter);
1105                         continue;
1106                 }
1107
1108                 nies = os_realloc(ies, ies_len + len);
1109                 if (nies == NULL) {
1110                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1111                                    "out of memory. Cannot allocate memory for "
1112                                    "IE");
1113                         os_free(ies);
1114                         *reply = dbus_message_new_error(
1115                                 message, DBUS_ERROR_NO_MEMORY, NULL);
1116                         return -1;
1117                 }
1118                 ies = nies;
1119                 os_memcpy(ies + ies_len, val, len);
1120                 ies_len += len;
1121
1122                 dbus_message_iter_next(&array_iter);
1123         }
1124
1125         params->extra_ies = ies;
1126         params->extra_ies_len = ies_len;
1127         return 0;
1128 }
1129
1130
1131 static int wpas_dbus_get_scan_channels(DBusMessage *message,
1132                                        DBusMessageIter *var,
1133                                        struct wpa_driver_scan_params *params,
1134                                        DBusMessage **reply)
1135 {
1136         DBusMessageIter array_iter, sub_array_iter;
1137         int *freqs = NULL, *nfreqs;
1138         int freqs_num = 0;
1139
1140         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
1141                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1142                            "Channels must be an array of structs");
1143                 *reply = wpas_dbus_error_invalid_args(
1144                         message, "Wrong Channels value type. Array of structs "
1145                         "required");
1146                 return -1;
1147         }
1148
1149         dbus_message_iter_recurse(var, &array_iter);
1150
1151         if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_STRUCT) {
1152                 wpa_printf(MSG_DEBUG,
1153                            "wpas_dbus_handler_scan[dbus]: Channels must be an "
1154                            "array of structs");
1155                 *reply = wpas_dbus_error_invalid_args(
1156                         message, "Wrong Channels value type. Array of structs "
1157                         "required");
1158                 return -1;
1159         }
1160
1161         while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_STRUCT)
1162         {
1163                 int freq, width;
1164
1165                 dbus_message_iter_recurse(&array_iter, &sub_array_iter);
1166
1167                 if (dbus_message_iter_get_arg_type(&sub_array_iter) !=
1168                     DBUS_TYPE_UINT32) {
1169                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1170                                    "Channel must by specified by struct of "
1171                                    "two UINT32s %c",
1172                                    dbus_message_iter_get_arg_type(
1173                                            &sub_array_iter));
1174                         *reply = wpas_dbus_error_invalid_args(
1175                                 message, "Wrong Channel struct. Two UINT32s "
1176                                 "required");
1177                         os_free(freqs);
1178                         return -1;
1179                 }
1180                 dbus_message_iter_get_basic(&sub_array_iter, &freq);
1181
1182                 if (!dbus_message_iter_next(&sub_array_iter) ||
1183                     dbus_message_iter_get_arg_type(&sub_array_iter) !=
1184                     DBUS_TYPE_UINT32) {
1185                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1186                                    "Channel must by specified by struct of "
1187                                    "two UINT32s");
1188                         *reply = wpas_dbus_error_invalid_args(
1189                                 message,
1190                                 "Wrong Channel struct. Two UINT32s required");
1191                         os_free(freqs);
1192                         return -1;
1193                 }
1194
1195                 dbus_message_iter_get_basic(&sub_array_iter, &width);
1196
1197 #define FREQS_ALLOC_CHUNK 32
1198                 if (freqs_num % FREQS_ALLOC_CHUNK == 0) {
1199                         nfreqs = os_realloc_array(
1200                                 freqs, freqs_num + FREQS_ALLOC_CHUNK,
1201                                 sizeof(int));
1202                         if (nfreqs == NULL)
1203                                 os_free(freqs);
1204                         freqs = nfreqs;
1205                 }
1206                 if (freqs == NULL) {
1207                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1208                                    "out of memory. can't allocate memory for "
1209                                    "freqs");
1210                         *reply = dbus_message_new_error(
1211                                 message, DBUS_ERROR_NO_MEMORY, NULL);
1212                         return -1;
1213                 }
1214
1215                 freqs[freqs_num] = freq;
1216
1217                 freqs_num++;
1218                 dbus_message_iter_next(&array_iter);
1219         }
1220
1221         nfreqs = os_realloc_array(freqs, freqs_num + 1, sizeof(int));
1222         if (nfreqs == NULL)
1223                 os_free(freqs);
1224         freqs = nfreqs;
1225         if (freqs == NULL) {
1226                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1227                            "out of memory. Can't allocate memory for freqs");
1228                 *reply = dbus_message_new_error(
1229                         message, DBUS_ERROR_NO_MEMORY, NULL);
1230                 return -1;
1231         }
1232         freqs[freqs_num] = 0;
1233
1234         params->freqs = freqs;
1235         return 0;
1236 }
1237
1238
1239 static int wpas_dbus_get_scan_allow_roam(DBusMessage *message,
1240                                          DBusMessageIter *var,
1241                                          dbus_bool_t *allow,
1242                                          DBusMessage **reply)
1243 {
1244         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_BOOLEAN) {
1245                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1246                            "Type must be a boolean");
1247                 *reply = wpas_dbus_error_invalid_args(
1248                         message, "Wrong Type value type. Boolean required");
1249                 return -1;
1250         }
1251         dbus_message_iter_get_basic(var, allow);
1252         return 0;
1253 }
1254
1255
1256 /**
1257  * wpas_dbus_handler_scan - Request a wireless scan on an interface
1258  * @message: Pointer to incoming dbus message
1259  * @wpa_s: wpa_supplicant structure for a network interface
1260  * Returns: NULL indicating success or DBus error message on failure
1261  *
1262  * Handler function for "Scan" method call of a network device. Requests
1263  * that wpa_supplicant perform a wireless scan as soon as possible
1264  * on a particular wireless interface.
1265  */
1266 DBusMessage * wpas_dbus_handler_scan(DBusMessage *message,
1267                                      struct wpa_supplicant *wpa_s)
1268 {
1269         DBusMessage *reply = NULL;
1270         DBusMessageIter iter, dict_iter, entry_iter, variant_iter;
1271         char *key = NULL, *type = NULL;
1272         struct wpa_driver_scan_params params;
1273         size_t i;
1274         dbus_bool_t allow_roam = 1;
1275
1276         os_memset(&params, 0, sizeof(params));
1277
1278         dbus_message_iter_init(message, &iter);
1279
1280         dbus_message_iter_recurse(&iter, &dict_iter);
1281
1282         while (dbus_message_iter_get_arg_type(&dict_iter) ==
1283                         DBUS_TYPE_DICT_ENTRY) {
1284                 dbus_message_iter_recurse(&dict_iter, &entry_iter);
1285                 dbus_message_iter_get_basic(&entry_iter, &key);
1286                 dbus_message_iter_next(&entry_iter);
1287                 dbus_message_iter_recurse(&entry_iter, &variant_iter);
1288
1289                 if (os_strcmp(key, "Type") == 0) {
1290                         if (wpas_dbus_get_scan_type(message, &variant_iter,
1291                                                     &type, &reply) < 0)
1292                                 goto out;
1293                 } else if (os_strcmp(key, "SSIDs") == 0) {
1294                         if (wpas_dbus_get_scan_ssids(message, &variant_iter,
1295                                                      &params, &reply) < 0)
1296                                 goto out;
1297                 } else if (os_strcmp(key, "IEs") == 0) {
1298                         if (wpas_dbus_get_scan_ies(message, &variant_iter,
1299                                                    &params, &reply) < 0)
1300                                 goto out;
1301                 } else if (os_strcmp(key, "Channels") == 0) {
1302                         if (wpas_dbus_get_scan_channels(message, &variant_iter,
1303                                                         &params, &reply) < 0)
1304                                 goto out;
1305                 } else if (os_strcmp(key, "AllowRoam") == 0) {
1306                         if (wpas_dbus_get_scan_allow_roam(message,
1307                                                           &variant_iter,
1308                                                           &allow_roam,
1309                                                           &reply) < 0)
1310                                 goto out;
1311                 } else {
1312                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1313                                    "Unknown argument %s", key);
1314                         reply = wpas_dbus_error_invalid_args(message, key);
1315                         goto out;
1316                 }
1317
1318                 dbus_message_iter_next(&dict_iter);
1319         }
1320
1321         if (!type) {
1322                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1323                            "Scan type not specified");
1324                 reply = wpas_dbus_error_invalid_args(message, key);
1325                 goto out;
1326         }
1327
1328         if (!os_strcmp(type, "passive")) {
1329                 if (params.num_ssids || params.extra_ies_len) {
1330                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1331                                    "SSIDs or IEs specified for passive scan.");
1332                         reply = wpas_dbus_error_invalid_args(
1333                                 message, "You can specify only Channels in "
1334                                 "passive scan");
1335                         goto out;
1336                 } else if (params.freqs && params.freqs[0]) {
1337                         wpa_supplicant_trigger_scan(wpa_s, &params);
1338                 } else {
1339                         wpa_s->scan_req = MANUAL_SCAN_REQ;
1340                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1341                 }
1342         } else if (!os_strcmp(type, "active")) {
1343                 if (!params.num_ssids) {
1344                         /* Add wildcard ssid */
1345                         params.num_ssids++;
1346                 }
1347 #ifdef CONFIG_AUTOSCAN
1348                 autoscan_deinit(wpa_s);
1349 #endif /* CONFIG_AUTOSCAN */
1350                 wpa_supplicant_trigger_scan(wpa_s, &params);
1351         } else {
1352                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1353                            "Unknown scan type: %s", type);
1354                 reply = wpas_dbus_error_invalid_args(message,
1355                                                      "Wrong scan type");
1356                 goto out;
1357         }
1358
1359         if (!allow_roam)
1360                 wpa_s->scan_res_handler = scan_only_handler;
1361
1362 out:
1363         for (i = 0; i < WPAS_MAX_SCAN_SSIDS; i++)
1364                 os_free((u8 *) params.ssids[i].ssid);
1365         os_free((u8 *) params.extra_ies);
1366         os_free(params.freqs);
1367         return reply;
1368 }
1369
1370
1371 /*
1372  * wpas_dbus_handler_disconnect - Terminate the current connection
1373  * @message: Pointer to incoming dbus message
1374  * @wpa_s: wpa_supplicant structure for a network interface
1375  * Returns: NotConnected DBus error message if already not connected
1376  * or NULL otherwise.
1377  *
1378  * Handler function for "Disconnect" method call of network interface.
1379  */
1380 DBusMessage * wpas_dbus_handler_disconnect(DBusMessage *message,
1381                                            struct wpa_supplicant *wpa_s)
1382 {
1383         if (wpa_s->current_ssid != NULL) {
1384                 wpa_s->disconnected = 1;
1385                 wpa_supplicant_deauthenticate(wpa_s,
1386                                               WLAN_REASON_DEAUTH_LEAVING);
1387
1388                 return NULL;
1389         }
1390
1391         return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
1392                                       "This interface is not connected");
1393 }
1394
1395
1396 /**
1397  * wpas_dbus_new_iface_add_network - Add a new configured network
1398  * @message: Pointer to incoming dbus message
1399  * @wpa_s: wpa_supplicant structure for a network interface
1400  * Returns: A dbus message containing the object path of the new network
1401  *
1402  * Handler function for "AddNetwork" method call of a network interface.
1403  */
1404 DBusMessage * wpas_dbus_handler_add_network(DBusMessage *message,
1405                                             struct wpa_supplicant *wpa_s)
1406 {
1407         DBusMessage *reply = NULL;
1408         DBusMessageIter iter;
1409         struct wpa_ssid *ssid = NULL;
1410         char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
1411         DBusError error;
1412
1413         dbus_message_iter_init(message, &iter);
1414
1415         ssid = wpa_config_add_network(wpa_s->conf);
1416         if (ssid == NULL) {
1417                 wpa_printf(MSG_ERROR, "wpas_dbus_handler_add_network[dbus]: "
1418                            "can't add new interface.");
1419                 reply = wpas_dbus_error_unknown_error(
1420                         message,
1421                         "wpa_supplicant could not add "
1422                         "a network on this interface.");
1423                 goto err;
1424         }
1425         wpas_notify_network_added(wpa_s, ssid);
1426         ssid->disabled = 1;
1427         wpa_config_set_network_defaults(ssid);
1428
1429         dbus_error_init(&error);
1430         if (!set_network_properties(wpa_s, ssid, &iter, &error)) {
1431                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_add_network[dbus]:"
1432                            "control interface couldn't set network "
1433                            "properties");
1434                 reply = wpas_dbus_reply_new_from_error(message, &error,
1435                                                        DBUS_ERROR_INVALID_ARGS,
1436                                                        "Failed to add network");
1437                 dbus_error_free(&error);
1438                 goto err;
1439         }
1440
1441         /* Construct the object path for this network. */
1442         os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
1443                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
1444                     wpa_s->dbus_new_path, ssid->id);
1445
1446         reply = dbus_message_new_method_return(message);
1447         if (reply == NULL) {
1448                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1449                                                NULL);
1450                 goto err;
1451         }
1452         if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
1453                                       DBUS_TYPE_INVALID)) {
1454                 dbus_message_unref(reply);
1455                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1456                                                NULL);
1457                 goto err;
1458         }
1459
1460         return reply;
1461
1462 err:
1463         if (ssid) {
1464                 wpas_notify_network_removed(wpa_s, ssid);
1465                 wpa_config_remove_network(wpa_s->conf, ssid->id);
1466         }
1467         return reply;
1468 }
1469
1470
1471 /**
1472  * wpas_dbus_handler_reassociate - Reassociate to current AP
1473  * @message: Pointer to incoming dbus message
1474  * @wpa_s: wpa_supplicant structure for a network interface
1475  * Returns: NotConnected DBus error message if not connected
1476  * or NULL otherwise.
1477  *
1478  * Handler function for "Reassociate" method call of network interface.
1479  */
1480 DBusMessage * wpas_dbus_handler_reassociate(DBusMessage *message,
1481                                             struct wpa_supplicant *wpa_s)
1482 {
1483         if (wpa_s->current_ssid != NULL) {
1484                 wpas_request_connection(wpa_s);
1485                 return NULL;
1486         }
1487
1488         return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
1489                                       "This interface is not connected");
1490 }
1491
1492
1493 /**
1494  * wpas_dbus_handler_remove_network - Remove a configured network
1495  * @message: Pointer to incoming dbus message
1496  * @wpa_s: wpa_supplicant structure for a network interface
1497  * Returns: NULL on success or dbus error on failure
1498  *
1499  * Handler function for "RemoveNetwork" method call of a network interface.
1500  */
1501 DBusMessage * wpas_dbus_handler_remove_network(DBusMessage *message,
1502                                                struct wpa_supplicant *wpa_s)
1503 {
1504         DBusMessage *reply = NULL;
1505         const char *op;
1506         char *iface = NULL, *net_id = NULL;
1507         int id;
1508         struct wpa_ssid *ssid;
1509         int was_disabled;
1510
1511         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
1512                               DBUS_TYPE_INVALID);
1513
1514         /* Extract the network ID and ensure the network */
1515         /* is actually a child of this interface */
1516         iface = wpas_dbus_new_decompose_object_path(op, 0, &net_id, NULL);
1517         if (iface == NULL || net_id == NULL ||
1518             os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
1519                 reply = wpas_dbus_error_invalid_args(message, op);
1520                 goto out;
1521         }
1522
1523         errno = 0;
1524         id = strtoul(net_id, NULL, 10);
1525         if (errno != 0) {
1526                 reply = wpas_dbus_error_invalid_args(message, op);
1527                 goto out;
1528         }
1529
1530         ssid = wpa_config_get_network(wpa_s->conf, id);
1531         if (ssid == NULL) {
1532                 reply = wpas_dbus_error_network_unknown(message);
1533                 goto out;
1534         }
1535
1536         was_disabled = ssid->disabled;
1537
1538         wpas_notify_network_removed(wpa_s, ssid);
1539
1540         if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
1541                 wpa_printf(MSG_ERROR,
1542                            "wpas_dbus_handler_remove_network[dbus]: "
1543                            "error occurred when removing network %d", id);
1544                 reply = wpas_dbus_error_unknown_error(
1545                         message, "error removing the specified network on "
1546                         "this interface.");
1547                 goto out;
1548         }
1549
1550         if (ssid == wpa_s->current_ssid)
1551                 wpa_supplicant_deauthenticate(wpa_s,
1552                                               WLAN_REASON_DEAUTH_LEAVING);
1553         else if (!was_disabled && wpa_s->sched_scanning) {
1554                 wpa_printf(MSG_DEBUG, "Stop ongoing sched_scan to remove "
1555                            "network from filters");
1556                 wpa_supplicant_cancel_sched_scan(wpa_s);
1557                 wpa_supplicant_req_scan(wpa_s, 0, 0);
1558         }
1559
1560
1561 out:
1562         os_free(iface);
1563         os_free(net_id);
1564         return reply;
1565 }
1566
1567
1568 static void remove_network(void *arg, struct wpa_ssid *ssid)
1569 {
1570         struct wpa_supplicant *wpa_s = arg;
1571
1572         wpas_notify_network_removed(wpa_s, ssid);
1573
1574         if (wpa_config_remove_network(wpa_s->conf, ssid->id) < 0) {
1575                 wpa_printf(MSG_ERROR,
1576                            "wpas_dbus_handler_remove_all_networks[dbus]: "
1577                            "error occurred when removing network %d",
1578                            ssid->id);
1579                 return;
1580         }
1581
1582         if (ssid == wpa_s->current_ssid)
1583                 wpa_supplicant_deauthenticate(wpa_s,
1584                                               WLAN_REASON_DEAUTH_LEAVING);
1585 }
1586
1587
1588 /**
1589  * wpas_dbus_handler_remove_all_networks - Remove all configured networks
1590  * @message: Pointer to incoming dbus message
1591  * @wpa_s: wpa_supplicant structure for a network interface
1592  * Returns: NULL on success or dbus error on failure
1593  *
1594  * Handler function for "RemoveAllNetworks" method call of a network interface.
1595  */
1596 DBusMessage * wpas_dbus_handler_remove_all_networks(
1597         DBusMessage *message, struct wpa_supplicant *wpa_s)
1598 {
1599         if (wpa_s->sched_scanning)
1600                 wpa_supplicant_cancel_sched_scan(wpa_s);
1601
1602         /* NB: could check for failure and return an error */
1603         wpa_config_foreach_network(wpa_s->conf, remove_network, wpa_s);
1604         return NULL;
1605 }
1606
1607
1608 /**
1609  * wpas_dbus_handler_select_network - Attempt association with a network
1610  * @message: Pointer to incoming dbus message
1611  * @wpa_s: wpa_supplicant structure for a network interface
1612  * Returns: NULL on success or dbus error on failure
1613  *
1614  * Handler function for "SelectNetwork" method call of network interface.
1615  */
1616 DBusMessage * wpas_dbus_handler_select_network(DBusMessage *message,
1617                                                struct wpa_supplicant *wpa_s)
1618 {
1619         DBusMessage *reply = NULL;
1620         const char *op;
1621         char *iface = NULL, *net_id = NULL;
1622         int id;
1623         struct wpa_ssid *ssid;
1624
1625         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
1626                               DBUS_TYPE_INVALID);
1627
1628         /* Extract the network ID and ensure the network */
1629         /* is actually a child of this interface */
1630         iface = wpas_dbus_new_decompose_object_path(op, 0, &net_id, NULL);
1631         if (iface == NULL || net_id == NULL ||
1632             os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
1633                 reply = wpas_dbus_error_invalid_args(message, op);
1634                 goto out;
1635         }
1636
1637         errno = 0;
1638         id = strtoul(net_id, NULL, 10);
1639         if (errno != 0) {
1640                 reply = wpas_dbus_error_invalid_args(message, op);
1641                 goto out;
1642         }
1643
1644         ssid = wpa_config_get_network(wpa_s->conf, id);
1645         if (ssid == NULL) {
1646                 reply = wpas_dbus_error_network_unknown(message);
1647                 goto out;
1648         }
1649
1650         /* Finally, associate with the network */
1651         wpa_supplicant_select_network(wpa_s, ssid);
1652
1653 out:
1654         os_free(iface);
1655         os_free(net_id);
1656         return reply;
1657 }
1658
1659
1660 /**
1661  * wpas_dbus_handler_network_reply - Reply to a NetworkRequest signal
1662  * @message: Pointer to incoming dbus message
1663  * @wpa_s: wpa_supplicant structure for a network interface
1664  * Returns: NULL on success or dbus error on failure
1665  *
1666  * Handler function for "NetworkReply" method call of network interface.
1667  */
1668 DBusMessage * wpas_dbus_handler_network_reply(DBusMessage *message,
1669                                               struct wpa_supplicant *wpa_s)
1670 {
1671 #ifdef IEEE8021X_EAPOL
1672         DBusMessage *reply = NULL;
1673         const char *op, *field, *value;
1674         char *iface = NULL, *net_id = NULL;
1675         int id;
1676         struct wpa_ssid *ssid;
1677
1678         if (!dbus_message_get_args(message, NULL,
1679                                    DBUS_TYPE_OBJECT_PATH, &op,
1680                                    DBUS_TYPE_STRING, &field,
1681                                    DBUS_TYPE_STRING, &value,
1682                                    DBUS_TYPE_INVALID))
1683                 return wpas_dbus_error_invalid_args(message, NULL);
1684
1685         /* Extract the network ID and ensure the network */
1686         /* is actually a child of this interface */
1687         iface = wpas_dbus_new_decompose_object_path(op, 0, &net_id, NULL);
1688         if (iface == NULL || net_id == NULL ||
1689             os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
1690                 reply = wpas_dbus_error_invalid_args(message, op);
1691                 goto out;
1692         }
1693
1694         errno = 0;
1695         id = strtoul(net_id, NULL, 10);
1696         if (errno != 0) {
1697                 reply = wpas_dbus_error_invalid_args(message, net_id);
1698                 goto out;
1699         }
1700
1701         ssid = wpa_config_get_network(wpa_s->conf, id);
1702         if (ssid == NULL) {
1703                 reply = wpas_dbus_error_network_unknown(message);
1704                 goto out;
1705         }
1706
1707         if (wpa_supplicant_ctrl_iface_ctrl_rsp_handle(wpa_s, ssid,
1708                                                       field, value) < 0)
1709                 reply = wpas_dbus_error_invalid_args(message, field);
1710         else {
1711                 /* Tell EAP to retry immediately */
1712                 eapol_sm_notify_ctrl_response(wpa_s->eapol);
1713         }
1714
1715 out:
1716         os_free(iface);
1717         os_free(net_id);
1718         return reply;
1719 #else /* IEEE8021X_EAPOL */
1720         wpa_printf(MSG_DEBUG, "CTRL_IFACE: 802.1X not included");
1721         return wpas_dbus_error_unknown_error(message, "802.1X not included");
1722 #endif /* IEEE8021X_EAPOL */
1723 }
1724
1725
1726 #ifndef CONFIG_NO_CONFIG_BLOBS
1727
1728 /**
1729  * wpas_dbus_handler_add_blob - Store named binary blob (ie, for certificates)
1730  * @message: Pointer to incoming dbus message
1731  * @wpa_s: %wpa_supplicant data structure
1732  * Returns: A dbus message containing an error on failure or NULL on success
1733  *
1734  * Asks wpa_supplicant to internally store a binary blobs.
1735  */
1736 DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
1737                                          struct wpa_supplicant *wpa_s)
1738 {
1739         DBusMessage *reply = NULL;
1740         DBusMessageIter iter, array_iter;
1741
1742         char *blob_name;
1743         u8 *blob_data;
1744         int blob_len;
1745         struct wpa_config_blob *blob = NULL;
1746
1747         dbus_message_iter_init(message, &iter);
1748         dbus_message_iter_get_basic(&iter, &blob_name);
1749
1750         if (wpa_config_get_blob(wpa_s->conf, blob_name)) {
1751                 return dbus_message_new_error(message,
1752                                               WPAS_DBUS_ERROR_BLOB_EXISTS,
1753                                               NULL);
1754         }
1755
1756         dbus_message_iter_next(&iter);
1757         dbus_message_iter_recurse(&iter, &array_iter);
1758
1759         dbus_message_iter_get_fixed_array(&array_iter, &blob_data, &blob_len);
1760
1761         blob = os_zalloc(sizeof(*blob));
1762         if (!blob) {
1763                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1764                                                NULL);
1765                 goto err;
1766         }
1767
1768         blob->data = os_malloc(blob_len);
1769         if (!blob->data) {
1770                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1771                                                NULL);
1772                 goto err;
1773         }
1774         os_memcpy(blob->data, blob_data, blob_len);
1775
1776         blob->len = blob_len;
1777         blob->name = os_strdup(blob_name);
1778         if (!blob->name) {
1779                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1780                                                NULL);
1781                 goto err;
1782         }
1783
1784         wpa_config_set_blob(wpa_s->conf, blob);
1785         wpas_notify_blob_added(wpa_s, blob->name);
1786
1787         return reply;
1788
1789 err:
1790         if (blob) {
1791                 os_free(blob->name);
1792                 os_free(blob->data);
1793                 os_free(blob);
1794         }
1795         return reply;
1796 }
1797
1798
1799 /**
1800  * wpas_dbus_handler_get_blob - Get named binary blob (ie, for certificates)
1801  * @message: Pointer to incoming dbus message
1802  * @wpa_s: %wpa_supplicant data structure
1803  * Returns: A dbus message containing array of bytes (blob)
1804  *
1805  * Gets one wpa_supplicant's binary blobs.
1806  */
1807 DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
1808                                          struct wpa_supplicant *wpa_s)
1809 {
1810         DBusMessage *reply = NULL;
1811         DBusMessageIter iter, array_iter;
1812
1813         char *blob_name;
1814         const struct wpa_config_blob *blob;
1815
1816         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
1817                               DBUS_TYPE_INVALID);
1818
1819         blob = wpa_config_get_blob(wpa_s->conf, blob_name);
1820         if (!blob) {
1821                 return dbus_message_new_error(message,
1822                                               WPAS_DBUS_ERROR_BLOB_UNKNOWN,
1823                                               "Blob id not set");
1824         }
1825
1826         reply = dbus_message_new_method_return(message);
1827         if (!reply) {
1828                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1829                                                NULL);
1830                 goto out;
1831         }
1832
1833         dbus_message_iter_init_append(reply, &iter);
1834
1835         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1836                                               DBUS_TYPE_BYTE_AS_STRING,
1837                                               &array_iter)) {
1838                 dbus_message_unref(reply);
1839                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1840                                                NULL);
1841                 goto out;
1842         }
1843
1844         if (!dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
1845                                                   &(blob->data), blob->len)) {
1846                 dbus_message_unref(reply);
1847                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1848                                                NULL);
1849                 goto out;
1850         }
1851
1852         if (!dbus_message_iter_close_container(&iter, &array_iter)) {
1853                 dbus_message_unref(reply);
1854                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1855                                                NULL);
1856                 goto out;
1857         }
1858
1859 out:
1860         return reply;
1861 }
1862
1863
1864 /**
1865  * wpas_remove_handler_remove_blob - Remove named binary blob
1866  * @message: Pointer to incoming dbus message
1867  * @wpa_s: %wpa_supplicant data structure
1868  * Returns: NULL on success or dbus error
1869  *
1870  * Asks wpa_supplicant to internally remove a binary blobs.
1871  */
1872 DBusMessage * wpas_dbus_handler_remove_blob(DBusMessage *message,
1873                                             struct wpa_supplicant *wpa_s)
1874 {
1875         DBusMessage *reply = NULL;
1876         char *blob_name;
1877
1878         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
1879                               DBUS_TYPE_INVALID);
1880
1881         if (wpa_config_remove_blob(wpa_s->conf, blob_name)) {
1882                 return dbus_message_new_error(message,
1883                                               WPAS_DBUS_ERROR_BLOB_UNKNOWN,
1884                                               "Blob id not set");
1885         }
1886         wpas_notify_blob_removed(wpa_s, blob_name);
1887
1888         return reply;
1889
1890 }
1891
1892 #endif /* CONFIG_NO_CONFIG_BLOBS */
1893
1894
1895 /*
1896  * wpas_dbus_handler_flush_bss - Flush the BSS cache
1897  * @message: Pointer to incoming dbus message
1898  * @wpa_s: wpa_supplicant structure for a network interface
1899  * Returns: NULL
1900  *
1901  * Handler function for "FlushBSS" method call of network interface.
1902  */
1903 DBusMessage * wpas_dbus_handler_flush_bss(DBusMessage *message,
1904                                           struct wpa_supplicant *wpa_s)
1905 {
1906         dbus_uint32_t age;
1907
1908         dbus_message_get_args(message, NULL, DBUS_TYPE_UINT32, &age,
1909                               DBUS_TYPE_INVALID);
1910
1911         if (age == 0)
1912                 wpa_bss_flush(wpa_s);
1913         else
1914                 wpa_bss_flush_by_age(wpa_s, age);
1915
1916         return NULL;
1917 }
1918
1919
1920 #ifdef CONFIG_AUTOSCAN
1921 /**
1922  * wpas_dbus_handler_autoscan - Set autoscan parameters for the interface
1923  * @message: Pointer to incoming dbus message
1924  * @wpa_s: wpa_supplicant structure for a network interface
1925  * Returns: NULL
1926  *
1927  * Handler function for "AutoScan" method call of network interface.
1928  */
1929 DBusMessage * wpas_dbus_handler_autoscan(DBusMessage *message,
1930                                          struct wpa_supplicant *wpa_s)
1931 {
1932         DBusMessage *reply = NULL;
1933         enum wpa_states state = wpa_s->wpa_state;
1934         char *arg;
1935
1936         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &arg,
1937                               DBUS_TYPE_INVALID);
1938
1939         if (arg != NULL && os_strlen(arg) > 0) {
1940                 char *tmp;
1941                 tmp = os_strdup(arg);
1942                 if (tmp == NULL) {
1943                         reply = dbus_message_new_error(message,
1944                                                        DBUS_ERROR_NO_MEMORY,
1945                                                        NULL);
1946                 } else {
1947                         os_free(wpa_s->conf->autoscan);
1948                         wpa_s->conf->autoscan = tmp;
1949                         if (state == WPA_DISCONNECTED || state == WPA_INACTIVE)
1950                                 autoscan_init(wpa_s, 1);
1951                         else if (state == WPA_SCANNING)
1952                                 wpa_supplicant_reinit_autoscan(wpa_s);
1953                 }
1954         } else if (arg != NULL && os_strlen(arg) == 0) {
1955                 os_free(wpa_s->conf->autoscan);
1956                 wpa_s->conf->autoscan = NULL;
1957                 autoscan_deinit(wpa_s);
1958         } else
1959                 reply = dbus_message_new_error(message,
1960                                                DBUS_ERROR_INVALID_ARGS,
1961                                                NULL);
1962
1963         return reply;
1964 }
1965 #endif /* CONFIG_AUTOSCAN */
1966
1967
1968 /*
1969  * wpas_dbus_handler_eap_logoff - IEEE 802.1X EAPOL state machine logoff
1970  * @message: Pointer to incoming dbus message
1971  * @wpa_s: wpa_supplicant structure for a network interface
1972  * Returns: NULL
1973  *
1974  * Handler function for "EAPLogoff" method call of network interface.
1975  */
1976 DBusMessage * wpas_dbus_handler_eap_logoff(DBusMessage *message,
1977                                            struct wpa_supplicant *wpa_s)
1978 {
1979         eapol_sm_notify_logoff(wpa_s->eapol, TRUE);
1980         return NULL;
1981 }
1982
1983
1984 /*
1985  * wpas_dbus_handler_eap_logon - IEEE 802.1X EAPOL state machine logon
1986  * @message: Pointer to incoming dbus message
1987  * @wpa_s: wpa_supplicant structure for a network interface
1988  * Returns: NULL
1989  *
1990  * Handler function for "EAPLogin" method call of network interface.
1991  */
1992 DBusMessage * wpas_dbus_handler_eap_logon(DBusMessage *message,
1993                                           struct wpa_supplicant *wpa_s)
1994 {
1995         eapol_sm_notify_logoff(wpa_s->eapol, FALSE);
1996         return NULL;
1997 }
1998
1999
2000 #ifdef CONFIG_TDLS
2001
2002 static DBusMessage * get_peer_hwaddr_helper(DBusMessage *message,
2003                                             const char *func_name,
2004                                             u8 *peer_address)
2005 {
2006         const char *peer_string;
2007
2008         if (!dbus_message_get_args(message, NULL,
2009                                    DBUS_TYPE_STRING, &peer_string,
2010                                    DBUS_TYPE_INVALID))
2011                 return wpas_dbus_error_invalid_args(message, NULL);
2012
2013         if (hwaddr_aton(peer_string, peer_address)) {
2014                 wpa_printf(MSG_DEBUG, "%s: invalid address '%s'",
2015                            func_name, peer_string);
2016                 return wpas_dbus_error_invalid_args(
2017                         message, "Invalid hardware address format");
2018         }
2019
2020         return NULL;
2021 }
2022
2023
2024 /*
2025  * wpas_dbus_handler_tdls_discover - Discover TDLS peer
2026  * @message: Pointer to incoming dbus message
2027  * @wpa_s: wpa_supplicant structure for a network interface
2028  * Returns: NULL indicating success or DBus error message on failure
2029  *
2030  * Handler function for "TDLSDiscover" method call of network interface.
2031  */
2032 DBusMessage * wpas_dbus_handler_tdls_discover(DBusMessage *message,
2033                                               struct wpa_supplicant *wpa_s)
2034 {
2035         u8 peer[ETH_ALEN];
2036         DBusMessage *error_reply;
2037         int ret;
2038
2039         error_reply = get_peer_hwaddr_helper(message, __func__, peer);
2040         if (error_reply)
2041                 return error_reply;
2042
2043         wpa_printf(MSG_DEBUG, "DBUS TDLS_DISCOVER " MACSTR, MAC2STR(peer));
2044
2045         if (wpa_tdls_is_external_setup(wpa_s->wpa))
2046                 ret = wpa_tdls_send_discovery_request(wpa_s->wpa, peer);
2047         else
2048                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_DISCOVERY_REQ, peer);
2049
2050         if (ret) {
2051                 return wpas_dbus_error_unknown_error(
2052                         message, "error performing TDLS discovery");
2053         }
2054
2055         return NULL;
2056 }
2057
2058
2059 /*
2060  * wpas_dbus_handler_tdls_setup - Setup TDLS session
2061  * @message: Pointer to incoming dbus message
2062  * @wpa_s: wpa_supplicant structure for a network interface
2063  * Returns: NULL indicating success or DBus error message on failure
2064  *
2065  * Handler function for "TDLSSetup" method call of network interface.
2066  */
2067 DBusMessage * wpas_dbus_handler_tdls_setup(DBusMessage *message,
2068                                            struct wpa_supplicant *wpa_s)
2069 {
2070         u8 peer[ETH_ALEN];
2071         DBusMessage *error_reply;
2072         int ret;
2073
2074         error_reply = get_peer_hwaddr_helper(message, __func__, peer);
2075         if (error_reply)
2076                 return error_reply;
2077
2078         wpa_printf(MSG_DEBUG, "DBUS TDLS_SETUP " MACSTR, MAC2STR(peer));
2079
2080         wpa_tdls_remove(wpa_s->wpa, peer);
2081         if (wpa_tdls_is_external_setup(wpa_s->wpa))
2082                 ret = wpa_tdls_start(wpa_s->wpa, peer);
2083         else
2084                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_SETUP, peer);
2085
2086         if (ret) {
2087                 return wpas_dbus_error_unknown_error(
2088                         message, "error performing TDLS setup");
2089         }
2090
2091         return NULL;
2092 }
2093
2094
2095 /*
2096  * wpas_dbus_handler_tdls_status - Return TDLS session status
2097  * @message: Pointer to incoming dbus message
2098  * @wpa_s: wpa_supplicant structure for a network interface
2099  * Returns: A string representing the state of the link to this TDLS peer
2100  *
2101  * Handler function for "TDLSStatus" method call of network interface.
2102  */
2103 DBusMessage * wpas_dbus_handler_tdls_status(DBusMessage *message,
2104                                             struct wpa_supplicant *wpa_s)
2105 {
2106         u8 peer[ETH_ALEN];
2107         DBusMessage *reply;
2108         const char *tdls_status;
2109
2110         reply = get_peer_hwaddr_helper(message, __func__, peer);
2111         if (reply)
2112                 return reply;
2113
2114         wpa_printf(MSG_DEBUG, "DBUS TDLS_STATUS " MACSTR, MAC2STR(peer));
2115
2116         tdls_status = wpa_tdls_get_link_status(wpa_s->wpa, peer);
2117
2118         reply = dbus_message_new_method_return(message);
2119         dbus_message_append_args(reply, DBUS_TYPE_STRING,
2120                                  &tdls_status, DBUS_TYPE_INVALID);
2121         return reply;
2122 }
2123
2124
2125 /*
2126  * wpas_dbus_handler_tdls_teardown - Teardown TDLS session
2127  * @message: Pointer to incoming dbus message
2128  * @wpa_s: wpa_supplicant structure for a network interface
2129  * Returns: NULL indicating success or DBus error message on failure
2130  *
2131  * Handler function for "TDLSTeardown" method call of network interface.
2132  */
2133 DBusMessage * wpas_dbus_handler_tdls_teardown(DBusMessage *message,
2134                                               struct wpa_supplicant *wpa_s)
2135 {
2136         u8 peer[ETH_ALEN];
2137         DBusMessage *error_reply;
2138         int ret;
2139
2140         error_reply = get_peer_hwaddr_helper(message, __func__, peer);
2141         if (error_reply)
2142                 return error_reply;
2143
2144         wpa_printf(MSG_DEBUG, "DBUS TDLS_TEARDOWN " MACSTR, MAC2STR(peer));
2145
2146         if (wpa_tdls_is_external_setup(wpa_s->wpa))
2147                 ret = wpa_tdls_teardown_link(
2148                         wpa_s->wpa, peer,
2149                         WLAN_REASON_TDLS_TEARDOWN_UNSPECIFIED);
2150         else
2151                 ret = wpa_drv_tdls_oper(wpa_s, TDLS_TEARDOWN, peer);
2152
2153         if (ret) {
2154                 return wpas_dbus_error_unknown_error(
2155                         message, "error performing TDLS teardown");
2156         }
2157
2158         return NULL;
2159 }
2160
2161 #endif /* CONFIG_TDLS */
2162
2163
2164 /**
2165  * wpas_dbus_getter_capabilities - Return interface capabilities
2166  * @iter: Pointer to incoming dbus message iter
2167  * @error: Location to store error on failure
2168  * @user_data: Function specific data
2169  * Returns: TRUE on success, FALSE on failure
2170  *
2171  * Getter for "Capabilities" property of an interface.
2172  */
2173 dbus_bool_t wpas_dbus_getter_capabilities(DBusMessageIter *iter,
2174                                           DBusError *error, void *user_data)
2175 {
2176         struct wpa_supplicant *wpa_s = user_data;
2177         struct wpa_driver_capa capa;
2178         int res;
2179         DBusMessageIter iter_dict, iter_dict_entry, iter_dict_val, iter_array,
2180                 variant_iter;
2181         const char *scans[] = { "active", "passive", "ssid" };
2182
2183         if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
2184                                               "a{sv}", &variant_iter))
2185                 goto nomem;
2186
2187         if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
2188                 goto nomem;
2189
2190         res = wpa_drv_get_capa(wpa_s, &capa);
2191
2192         /***** pairwise cipher */
2193         if (res < 0) {
2194                 const char *args[] = {"ccmp", "tkip", "none"};
2195                 if (!wpa_dbus_dict_append_string_array(
2196                             &iter_dict, "Pairwise", args,
2197                             ARRAY_SIZE(args)))
2198                         goto nomem;
2199         } else {
2200                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
2201                                                       &iter_dict_entry,
2202                                                       &iter_dict_val,
2203                                                       &iter_array))
2204                         goto nomem;
2205
2206                 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2207                         if (!wpa_dbus_dict_string_array_add_element(
2208                                     &iter_array, "ccmp"))
2209                                 goto nomem;
2210                 }
2211
2212                 if (capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2213                         if (!wpa_dbus_dict_string_array_add_element(
2214                                     &iter_array, "gcmp"))
2215                                 goto nomem;
2216                 }
2217
2218                 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2219                         if (!wpa_dbus_dict_string_array_add_element(
2220                                     &iter_array, "tkip"))
2221                                 goto nomem;
2222                 }
2223
2224                 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2225                         if (!wpa_dbus_dict_string_array_add_element(
2226                                     &iter_array, "none"))
2227                                 goto nomem;
2228                 }
2229
2230                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
2231                                                     &iter_dict_entry,
2232                                                     &iter_dict_val,
2233                                                     &iter_array))
2234                         goto nomem;
2235         }
2236
2237         /***** group cipher */
2238         if (res < 0) {
2239                 const char *args[] = {
2240                         "ccmp", "tkip", "wep104", "wep40"
2241                 };
2242                 if (!wpa_dbus_dict_append_string_array(
2243                             &iter_dict, "Group", args,
2244                             ARRAY_SIZE(args)))
2245                         goto nomem;
2246         } else {
2247                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
2248                                                       &iter_dict_entry,
2249                                                       &iter_dict_val,
2250                                                       &iter_array))
2251                         goto nomem;
2252
2253                 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
2254                         if (!wpa_dbus_dict_string_array_add_element(
2255                                     &iter_array, "ccmp"))
2256                                 goto nomem;
2257                 }
2258
2259                 if (capa.enc & WPA_DRIVER_CAPA_ENC_GCMP) {
2260                         if (!wpa_dbus_dict_string_array_add_element(
2261                                     &iter_array, "gcmp"))
2262                                 goto nomem;
2263                 }
2264
2265                 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
2266                         if (!wpa_dbus_dict_string_array_add_element(
2267                                     &iter_array, "tkip"))
2268                                 goto nomem;
2269                 }
2270
2271                 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
2272                         if (!wpa_dbus_dict_string_array_add_element(
2273                                     &iter_array, "wep104"))
2274                                 goto nomem;
2275                 }
2276
2277                 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
2278                         if (!wpa_dbus_dict_string_array_add_element(
2279                                     &iter_array, "wep40"))
2280                                 goto nomem;
2281                 }
2282
2283                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
2284                                                     &iter_dict_entry,
2285                                                     &iter_dict_val,
2286                                                     &iter_array))
2287                         goto nomem;
2288         }
2289
2290         /***** key management */
2291         if (res < 0) {
2292                 const char *args[] = {
2293                         "wpa-psk", "wpa-eap", "ieee8021x", "wpa-none",
2294 #ifdef CONFIG_WPS
2295                         "wps",
2296 #endif /* CONFIG_WPS */
2297                         "none"
2298                 };
2299                 if (!wpa_dbus_dict_append_string_array(
2300                             &iter_dict, "KeyMgmt", args,
2301                             ARRAY_SIZE(args)))
2302                         goto nomem;
2303         } else {
2304                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
2305                                                       &iter_dict_entry,
2306                                                       &iter_dict_val,
2307                                                       &iter_array))
2308                         goto nomem;
2309
2310                 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
2311                                                             "none"))
2312                         goto nomem;
2313
2314                 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
2315                                                             "ieee8021x"))
2316                         goto nomem;
2317
2318                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2319                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
2320                         if (!wpa_dbus_dict_string_array_add_element(
2321                                     &iter_array, "wpa-eap"))
2322                                 goto nomem;
2323
2324                         if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT)
2325                                 if (!wpa_dbus_dict_string_array_add_element(
2326                                             &iter_array, "wpa-ft-eap"))
2327                                         goto nomem;
2328
2329 /* TODO: Ensure that driver actually supports sha256 encryption. */
2330 #ifdef CONFIG_IEEE80211W
2331                         if (!wpa_dbus_dict_string_array_add_element(
2332                                     &iter_array, "wpa-eap-sha256"))
2333                                 goto nomem;
2334 #endif /* CONFIG_IEEE80211W */
2335                 }
2336
2337                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
2338                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2339                         if (!wpa_dbus_dict_string_array_add_element(
2340                                     &iter_array, "wpa-psk"))
2341                                 goto nomem;
2342
2343                         if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_FT_PSK)
2344                                 if (!wpa_dbus_dict_string_array_add_element(
2345                                             &iter_array, "wpa-ft-psk"))
2346                                         goto nomem;
2347
2348 /* TODO: Ensure that driver actually supports sha256 encryption. */
2349 #ifdef CONFIG_IEEE80211W
2350                         if (!wpa_dbus_dict_string_array_add_element(
2351                                     &iter_array, "wpa-psk-sha256"))
2352                                 goto nomem;
2353 #endif /* CONFIG_IEEE80211W */
2354                 }
2355
2356                 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
2357                         if (!wpa_dbus_dict_string_array_add_element(
2358                                     &iter_array, "wpa-none"))
2359                                 goto nomem;
2360                 }
2361
2362
2363 #ifdef CONFIG_WPS
2364                 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
2365                                                             "wps"))
2366                         goto nomem;
2367 #endif /* CONFIG_WPS */
2368
2369                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
2370                                                     &iter_dict_entry,
2371                                                     &iter_dict_val,
2372                                                     &iter_array))
2373                         goto nomem;
2374         }
2375
2376         /***** WPA protocol */
2377         if (res < 0) {
2378                 const char *args[] = { "rsn", "wpa" };
2379                 if (!wpa_dbus_dict_append_string_array(
2380                             &iter_dict, "Protocol", args,
2381                             ARRAY_SIZE(args)))
2382                         goto nomem;
2383         } else {
2384                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
2385                                                       &iter_dict_entry,
2386                                                       &iter_dict_val,
2387                                                       &iter_array))
2388                         goto nomem;
2389
2390                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
2391                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
2392                         if (!wpa_dbus_dict_string_array_add_element(
2393                                     &iter_array, "rsn"))
2394                                 goto nomem;
2395                 }
2396
2397                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
2398                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
2399                         if (!wpa_dbus_dict_string_array_add_element(
2400                                     &iter_array, "wpa"))
2401                                 goto nomem;
2402                 }
2403
2404                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
2405                                                     &iter_dict_entry,
2406                                                     &iter_dict_val,
2407                                                     &iter_array))
2408                         goto nomem;
2409         }
2410
2411         /***** auth alg */
2412         if (res < 0) {
2413                 const char *args[] = { "open", "shared", "leap" };
2414                 if (!wpa_dbus_dict_append_string_array(
2415                             &iter_dict, "AuthAlg", args,
2416                             ARRAY_SIZE(args)))
2417                         goto nomem;
2418         } else {
2419                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
2420                                                       &iter_dict_entry,
2421                                                       &iter_dict_val,
2422                                                       &iter_array))
2423                         goto nomem;
2424
2425                 if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
2426                         if (!wpa_dbus_dict_string_array_add_element(
2427                                     &iter_array, "open"))
2428                                 goto nomem;
2429                 }
2430
2431                 if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
2432                         if (!wpa_dbus_dict_string_array_add_element(
2433                                     &iter_array, "shared"))
2434                                 goto nomem;
2435                 }
2436
2437                 if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
2438                         if (!wpa_dbus_dict_string_array_add_element(
2439                                     &iter_array, "leap"))
2440                                 goto nomem;
2441                 }
2442
2443                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
2444                                                     &iter_dict_entry,
2445                                                     &iter_dict_val,
2446                                                     &iter_array))
2447                         goto nomem;
2448         }
2449
2450         /***** Scan */
2451         if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
2452                                                ARRAY_SIZE(scans)))
2453                 goto nomem;
2454
2455         /***** Modes */
2456         if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Modes",
2457                                               &iter_dict_entry,
2458                                               &iter_dict_val,
2459                                               &iter_array))
2460                 goto nomem;
2461
2462         if (!wpa_dbus_dict_string_array_add_element(
2463                             &iter_array, "infrastructure"))
2464                 goto nomem;
2465
2466         if (!wpa_dbus_dict_string_array_add_element(
2467                             &iter_array, "ad-hoc"))
2468                 goto nomem;
2469
2470         if (res >= 0) {
2471                 if (capa.flags & (WPA_DRIVER_FLAGS_AP)) {
2472                         if (!wpa_dbus_dict_string_array_add_element(
2473                                     &iter_array, "ap"))
2474                                 goto nomem;
2475                 }
2476
2477                 if (capa.flags & (WPA_DRIVER_FLAGS_P2P_CAPABLE)) {
2478                         if (!wpa_dbus_dict_string_array_add_element(
2479                                     &iter_array, "p2p"))
2480                                 goto nomem;
2481                 }
2482         }
2483
2484         if (!wpa_dbus_dict_end_string_array(&iter_dict,
2485                                             &iter_dict_entry,
2486                                             &iter_dict_val,
2487                                             &iter_array))
2488                 goto nomem;
2489         /***** Modes end */
2490
2491         if (res >= 0) {
2492                 dbus_int32_t max_scan_ssid = capa.max_scan_ssids;
2493
2494                 if (!wpa_dbus_dict_append_int32(&iter_dict, "MaxScanSSID",
2495                                                 max_scan_ssid))
2496                         goto nomem;
2497         }
2498
2499         if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
2500                 goto nomem;
2501         if (!dbus_message_iter_close_container(iter, &variant_iter))
2502                 goto nomem;
2503
2504         return TRUE;
2505
2506 nomem:
2507         dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
2508         return FALSE;
2509 }
2510
2511
2512 /**
2513  * wpas_dbus_getter_state - Get interface state
2514  * @iter: Pointer to incoming dbus message iter
2515  * @error: Location to store error on failure
2516  * @user_data: Function specific data
2517  * Returns: TRUE on success, FALSE on failure
2518  *
2519  * Getter for "State" property.
2520  */
2521 dbus_bool_t wpas_dbus_getter_state(DBusMessageIter *iter, DBusError *error,
2522                                    void *user_data)
2523 {
2524         struct wpa_supplicant *wpa_s = user_data;
2525         const char *str_state;
2526         char *state_ls, *tmp;
2527         dbus_bool_t success = FALSE;
2528
2529         str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
2530
2531         /* make state string lowercase to fit new DBus API convention
2532          */
2533         state_ls = tmp = os_strdup(str_state);
2534         if (!tmp) {
2535                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
2536                 return FALSE;
2537         }
2538         while (*tmp) {
2539                 *tmp = tolower(*tmp);
2540                 tmp++;
2541         }
2542
2543         success = wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
2544                                                    &state_ls, error);
2545
2546         os_free(state_ls);
2547
2548         return success;
2549 }
2550
2551
2552 /**
2553  * wpas_dbus_new_iface_get_scanning - Get interface scanning state
2554  * @iter: Pointer to incoming dbus message iter
2555  * @error: Location to store error on failure
2556  * @user_data: Function specific data
2557  * Returns: TRUE on success, FALSE on failure
2558  *
2559  * Getter for "scanning" property.
2560  */
2561 dbus_bool_t wpas_dbus_getter_scanning(DBusMessageIter *iter, DBusError *error,
2562                                       void *user_data)
2563 {
2564         struct wpa_supplicant *wpa_s = user_data;
2565         dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
2566
2567         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
2568                                                 &scanning, error);
2569 }
2570
2571
2572 /**
2573  * wpas_dbus_getter_ap_scan - Control roaming mode
2574  * @iter: Pointer to incoming dbus message iter
2575  * @error: Location to store error on failure
2576  * @user_data: Function specific data
2577  * Returns: TRUE on success, FALSE on failure
2578  *
2579  * Getter function for "ApScan" property.
2580  */
2581 dbus_bool_t wpas_dbus_getter_ap_scan(DBusMessageIter *iter, DBusError *error,
2582                                      void *user_data)
2583 {
2584         struct wpa_supplicant *wpa_s = user_data;
2585         dbus_uint32_t ap_scan = wpa_s->conf->ap_scan;
2586
2587         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
2588                                                 &ap_scan, error);
2589 }
2590
2591
2592 /**
2593  * wpas_dbus_setter_ap_scan - Control roaming mode
2594  * @iter: Pointer to incoming dbus message iter
2595  * @error: Location to store error on failure
2596  * @user_data: Function specific data
2597  * Returns: TRUE on success, FALSE on failure
2598  *
2599  * Setter function for "ApScan" property.
2600  */
2601 dbus_bool_t wpas_dbus_setter_ap_scan(DBusMessageIter *iter, DBusError *error,
2602                                      void *user_data)
2603 {
2604         struct wpa_supplicant *wpa_s = user_data;
2605         dbus_uint32_t ap_scan;
2606
2607         if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
2608                                               &ap_scan))
2609                 return FALSE;
2610
2611         if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
2612                 dbus_set_error_const(error, DBUS_ERROR_FAILED,
2613                                      "ap_scan must be 0, 1, or 2");
2614                 return FALSE;
2615         }
2616         return TRUE;
2617 }
2618
2619
2620 /**
2621  * wpas_dbus_getter_fast_reauth - Control fast
2622  * reauthentication (TLS session resumption)
2623  * @iter: Pointer to incoming dbus message iter
2624  * @error: Location to store error on failure
2625  * @user_data: Function specific data
2626  * Returns: TRUE on success, FALSE on failure
2627  *
2628  * Getter function for "FastReauth" property.
2629  */
2630 dbus_bool_t wpas_dbus_getter_fast_reauth(DBusMessageIter *iter,
2631                                          DBusError *error,
2632                                          void *user_data)
2633 {
2634         struct wpa_supplicant *wpa_s = user_data;
2635         dbus_bool_t fast_reauth = wpa_s->conf->fast_reauth ? TRUE : FALSE;
2636
2637         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
2638                                                 &fast_reauth, error);
2639 }
2640
2641
2642 /**
2643  * wpas_dbus_setter_fast_reauth - Control fast
2644  * reauthentication (TLS session resumption)
2645  * @iter: Pointer to incoming dbus message iter
2646  * @error: Location to store error on failure
2647  * @user_data: Function specific data
2648  * Returns: TRUE on success, FALSE on failure
2649  *
2650  * Setter function for "FastReauth" property.
2651  */
2652 dbus_bool_t wpas_dbus_setter_fast_reauth(DBusMessageIter *iter,
2653                                      DBusError *error,
2654                                      void *user_data)
2655 {
2656         struct wpa_supplicant *wpa_s = user_data;
2657         dbus_bool_t fast_reauth;
2658
2659         if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
2660                                               &fast_reauth))
2661                 return FALSE;
2662
2663         wpa_s->conf->fast_reauth = fast_reauth;
2664         return TRUE;
2665 }
2666
2667
2668 /**
2669  * wpas_dbus_getter_disconnect_reason - Get most recent reason for disconnect
2670  * @iter: Pointer to incoming dbus message iter
2671  * @error: Location to store error on failure
2672  * @user_data: Function specific data
2673  * Returns: TRUE on success, FALSE on failure
2674  *
2675  * Getter for "DisconnectReason" property.  The reason is negative if it is
2676  * locally generated.
2677  */
2678 dbus_bool_t wpas_dbus_getter_disconnect_reason(DBusMessageIter *iter,
2679                                                DBusError *error,
2680                                                void *user_data)
2681 {
2682         struct wpa_supplicant *wpa_s = user_data;
2683         dbus_int32_t reason = wpa_s->disconnect_reason;
2684         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
2685                                                 &reason, error);
2686 }
2687
2688
2689 /**
2690  * wpas_dbus_getter_bss_expire_age - Get BSS entry expiration age
2691  * @iter: Pointer to incoming dbus message iter
2692  * @error: Location to store error on failure
2693  * @user_data: Function specific data
2694  * Returns: TRUE on success, FALSE on failure
2695  *
2696  * Getter function for "BSSExpireAge" property.
2697  */
2698 dbus_bool_t wpas_dbus_getter_bss_expire_age(DBusMessageIter *iter,
2699                                             DBusError *error,
2700                                             void *user_data)
2701 {
2702         struct wpa_supplicant *wpa_s = user_data;
2703         dbus_uint32_t expire_age = wpa_s->conf->bss_expiration_age;
2704
2705         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
2706                                                 &expire_age, error);
2707 }
2708
2709
2710 /**
2711  * wpas_dbus_setter_bss_expire_age - Control BSS entry expiration age
2712  * @iter: Pointer to incoming dbus message iter
2713  * @error: Location to store error on failure
2714  * @user_data: Function specific data
2715  * Returns: TRUE on success, FALSE on failure
2716  *
2717  * Setter function for "BSSExpireAge" property.
2718  */
2719 dbus_bool_t wpas_dbus_setter_bss_expire_age(DBusMessageIter *iter,
2720                                             DBusError *error,
2721                                             void *user_data)
2722 {
2723         struct wpa_supplicant *wpa_s = user_data;
2724         dbus_uint32_t expire_age;
2725
2726         if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
2727                                               &expire_age))
2728                 return FALSE;
2729
2730         if (wpa_supplicant_set_bss_expiration_age(wpa_s, expire_age)) {
2731                 dbus_set_error_const(error, DBUS_ERROR_FAILED,
2732                                      "BSSExpireAge must be >= 10");
2733                 return FALSE;
2734         }
2735         return TRUE;
2736 }
2737
2738
2739 /**
2740  * wpas_dbus_getter_bss_expire_count - Get BSS entry expiration scan count
2741  * @iter: Pointer to incoming dbus message iter
2742  * @error: Location to store error on failure
2743  * @user_data: Function specific data
2744  * Returns: TRUE on success, FALSE on failure
2745  *
2746  * Getter function for "BSSExpireCount" property.
2747  */
2748 dbus_bool_t wpas_dbus_getter_bss_expire_count(DBusMessageIter *iter,
2749                                               DBusError *error,
2750                                               void *user_data)
2751 {
2752         struct wpa_supplicant *wpa_s = user_data;
2753         dbus_uint32_t expire_count = wpa_s->conf->bss_expiration_scan_count;
2754
2755         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT32,
2756                                                 &expire_count, error);
2757 }
2758
2759
2760 /**
2761  * wpas_dbus_setter_bss_expire_count - Control BSS entry expiration scan count
2762  * @iter: Pointer to incoming dbus message iter
2763  * @error: Location to store error on failure
2764  * @user_data: Function specific data
2765  * Returns: TRUE on success, FALSE on failure
2766  *
2767  * Setter function for "BSSExpireCount" property.
2768  */
2769 dbus_bool_t wpas_dbus_setter_bss_expire_count(DBusMessageIter *iter,
2770                                               DBusError *error,
2771                                               void *user_data)
2772 {
2773         struct wpa_supplicant *wpa_s = user_data;
2774         dbus_uint32_t expire_count;
2775
2776         if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_UINT32,
2777                                               &expire_count))
2778                 return FALSE;
2779
2780         if (wpa_supplicant_set_bss_expiration_count(wpa_s, expire_count)) {
2781                 dbus_set_error_const(error, DBUS_ERROR_FAILED,
2782                                      "BSSExpireCount must be > 0");
2783                 return FALSE;
2784         }
2785         return TRUE;
2786 }
2787
2788
2789 /**
2790  * wpas_dbus_getter_country - Control country code
2791  * @iter: Pointer to incoming dbus message iter
2792  * @error: Location to store error on failure
2793  * @user_data: Function specific data
2794  * Returns: TRUE on success, FALSE on failure
2795  *
2796  * Getter function for "Country" property.
2797  */
2798 dbus_bool_t wpas_dbus_getter_country(DBusMessageIter *iter, DBusError *error,
2799                                      void *user_data)
2800 {
2801         struct wpa_supplicant *wpa_s = user_data;
2802         char country[3];
2803         char *str = country;
2804
2805         country[0] = wpa_s->conf->country[0];
2806         country[1] = wpa_s->conf->country[1];
2807         country[2] = '\0';
2808
2809         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
2810                                                 &str, error);
2811 }
2812
2813
2814 /**
2815  * wpas_dbus_setter_country - Control country code
2816  * @iter: Pointer to incoming dbus message iter
2817  * @error: Location to store error on failure
2818  * @user_data: Function specific data
2819  * Returns: TRUE on success, FALSE on failure
2820  *
2821  * Setter function for "Country" property.
2822  */
2823 dbus_bool_t wpas_dbus_setter_country(DBusMessageIter *iter, DBusError *error,
2824                                      void *user_data)
2825 {
2826         struct wpa_supplicant *wpa_s = user_data;
2827         const char *country;
2828
2829         if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_STRING,
2830                                               &country))
2831                 return FALSE;
2832
2833         if (!country[0] || !country[1]) {
2834                 dbus_set_error_const(error, DBUS_ERROR_FAILED,
2835                                      "invalid country code");
2836                 return FALSE;
2837         }
2838
2839         if (wpa_s->drv_priv != NULL && wpa_drv_set_country(wpa_s, country)) {
2840                 wpa_printf(MSG_DEBUG, "Failed to set country");
2841                 dbus_set_error_const(error, DBUS_ERROR_FAILED,
2842                                      "failed to set country code");
2843                 return FALSE;
2844         }
2845
2846         wpa_s->conf->country[0] = country[0];
2847         wpa_s->conf->country[1] = country[1];
2848         return TRUE;
2849 }
2850
2851
2852 /**
2853  * wpas_dbus_getter_scan_interval - Get scan interval
2854  * @iter: Pointer to incoming dbus message iter
2855  * @error: Location to store error on failure
2856  * @user_data: Function specific data
2857  * Returns: TRUE on success, FALSE on failure
2858  *
2859  * Getter function for "ScanInterval" property.
2860  */
2861 dbus_bool_t wpas_dbus_getter_scan_interval(DBusMessageIter *iter,
2862                                            DBusError *error,
2863                                            void *user_data)
2864 {
2865         struct wpa_supplicant *wpa_s = user_data;
2866         dbus_int32_t scan_interval = wpa_s->scan_interval;
2867
2868         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT32,
2869                                                 &scan_interval, error);
2870 }
2871
2872
2873 /**
2874  * wpas_dbus_setter_scan_interval - Control scan interval
2875  * @iter: Pointer to incoming dbus message iter
2876  * @error: Location to store error on failure
2877  * @user_data: Function specific data
2878  * Returns: TRUE on success, FALSE on failure
2879  *
2880  * Setter function for "ScanInterval" property.
2881  */
2882 dbus_bool_t wpas_dbus_setter_scan_interval(DBusMessageIter *iter,
2883                                            DBusError *error,
2884                                            void *user_data)
2885 {
2886         struct wpa_supplicant *wpa_s = user_data;
2887         dbus_int32_t scan_interval;
2888
2889         if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_INT32,
2890                                               &scan_interval))
2891                 return FALSE;
2892
2893         if (wpa_supplicant_set_scan_interval(wpa_s, scan_interval)) {
2894                 dbus_set_error_const(error, DBUS_ERROR_FAILED,
2895                                      "scan_interval must be >= 0");
2896                 return FALSE;
2897         }
2898         return TRUE;
2899 }
2900
2901
2902 /**
2903  * wpas_dbus_getter_ifname - Get interface name
2904  * @iter: Pointer to incoming dbus message iter
2905  * @error: Location to store error on failure
2906  * @user_data: Function specific data
2907  * Returns: TRUE on success, FALSE on failure
2908  *
2909  * Getter for "Ifname" property.
2910  */
2911 dbus_bool_t wpas_dbus_getter_ifname(DBusMessageIter *iter, DBusError *error,
2912                                     void *user_data)
2913 {
2914         struct wpa_supplicant *wpa_s = user_data;
2915         const char *ifname = wpa_s->ifname;
2916
2917         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
2918                                                 &ifname, error);
2919 }
2920
2921
2922 /**
2923  * wpas_dbus_getter_driver - Get interface name
2924  * @iter: Pointer to incoming dbus message iter
2925  * @error: Location to store error on failure
2926  * @user_data: Function specific data
2927  * Returns: TRUE on success, FALSE on failure
2928  *
2929  * Getter for "Driver" property.
2930  */
2931 dbus_bool_t wpas_dbus_getter_driver(DBusMessageIter *iter, DBusError *error,
2932                                     void *user_data)
2933 {
2934         struct wpa_supplicant *wpa_s = user_data;
2935         const char *driver;
2936
2937         if (wpa_s->driver == NULL || wpa_s->driver->name == NULL) {
2938                 wpa_printf(MSG_DEBUG, "wpas_dbus_getter_driver[dbus]: "
2939                            "wpa_s has no driver set");
2940                 dbus_set_error(error, DBUS_ERROR_FAILED, "%s: no driver set",
2941                                __func__);
2942                 return FALSE;
2943         }
2944
2945         driver = wpa_s->driver->name;
2946         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
2947                                                 &driver, error);
2948 }
2949
2950
2951 /**
2952  * wpas_dbus_getter_current_bss - Get current bss object path
2953  * @iter: Pointer to incoming dbus message iter
2954  * @error: Location to store error on failure
2955  * @user_data: Function specific data
2956  * Returns: TRUE on success, FALSE on failure
2957  *
2958  * Getter for "CurrentBSS" property.
2959  */
2960 dbus_bool_t wpas_dbus_getter_current_bss(DBusMessageIter *iter,
2961                                          DBusError *error,
2962                                          void *user_data)
2963 {
2964         struct wpa_supplicant *wpa_s = user_data;
2965         char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *bss_obj_path = path_buf;
2966
2967         if (wpa_s->current_bss)
2968                 os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
2969                             "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
2970                             wpa_s->dbus_new_path, wpa_s->current_bss->id);
2971         else
2972                 os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
2973
2974         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
2975                                                 &bss_obj_path, error);
2976 }
2977
2978
2979 /**
2980  * wpas_dbus_getter_current_network - Get current network object path
2981  * @iter: Pointer to incoming dbus message iter
2982  * @error: Location to store error on failure
2983  * @user_data: Function specific data
2984  * Returns: TRUE on success, FALSE on failure
2985  *
2986  * Getter for "CurrentNetwork" property.
2987  */
2988 dbus_bool_t wpas_dbus_getter_current_network(DBusMessageIter *iter,
2989                                              DBusError *error,
2990                                              void *user_data)
2991 {
2992         struct wpa_supplicant *wpa_s = user_data;
2993         char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *net_obj_path = path_buf;
2994
2995         if (wpa_s->current_ssid)
2996                 os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
2997                             "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
2998                             wpa_s->dbus_new_path, wpa_s->current_ssid->id);
2999         else
3000                 os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
3001
3002         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_OBJECT_PATH,
3003                                                 &net_obj_path, error);
3004 }
3005
3006
3007 /**
3008  * wpas_dbus_getter_current_auth_mode - Get current authentication type
3009  * @iter: Pointer to incoming dbus message iter
3010  * @error: Location to store error on failure
3011  * @user_data: Function specific data
3012  * Returns: TRUE on success, FALSE on failure
3013  *
3014  * Getter for "CurrentAuthMode" property.
3015  */
3016 dbus_bool_t wpas_dbus_getter_current_auth_mode(DBusMessageIter *iter,
3017                                                DBusError *error,
3018                                                void *user_data)
3019 {
3020         struct wpa_supplicant *wpa_s = user_data;
3021         const char *eap_mode;
3022         const char *auth_mode;
3023         char eap_mode_buf[WPAS_DBUS_AUTH_MODE_MAX];
3024
3025         if (wpa_s->wpa_state != WPA_COMPLETED) {
3026                 auth_mode = "INACTIVE";
3027         } else if (wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X ||
3028             wpa_s->key_mgmt == WPA_KEY_MGMT_IEEE8021X_NO_WPA) {
3029                 eap_mode = wpa_supplicant_get_eap_mode(wpa_s);
3030                 os_snprintf(eap_mode_buf, WPAS_DBUS_AUTH_MODE_MAX,
3031                             "EAP-%s", eap_mode);
3032                 auth_mode = eap_mode_buf;
3033
3034         } else {
3035                 auth_mode = wpa_key_mgmt_txt(wpa_s->key_mgmt,
3036                                              wpa_s->current_ssid->proto);
3037         }
3038
3039         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
3040                                                 &auth_mode, error);
3041 }
3042
3043
3044 /**
3045  * wpas_dbus_getter_bridge_ifname - Get interface name
3046  * @iter: Pointer to incoming dbus message iter
3047  * @error: Location to store error on failure
3048  * @user_data: Function specific data
3049  * Returns: TRUE on success, FALSE on failure
3050  *
3051  * Getter for "BridgeIfname" property.
3052  */
3053 dbus_bool_t wpas_dbus_getter_bridge_ifname(DBusMessageIter *iter,
3054                                            DBusError *error,
3055                                            void *user_data)
3056 {
3057         struct wpa_supplicant *wpa_s = user_data;
3058         const char *bridge_ifname = wpa_s->bridge_ifname;
3059         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
3060                                                 &bridge_ifname, error);
3061 }
3062
3063
3064 /**
3065  * wpas_dbus_getter_bsss - Get array of BSSs objects
3066  * @iter: Pointer to incoming dbus message iter
3067  * @error: Location to store error on failure
3068  * @user_data: Function specific data
3069  * Returns: TRUE on success, FALSE on failure
3070  *
3071  * Getter for "BSSs" property.
3072  */
3073 dbus_bool_t wpas_dbus_getter_bsss(DBusMessageIter *iter, DBusError *error,
3074                                   void *user_data)
3075 {
3076         struct wpa_supplicant *wpa_s = user_data;
3077         struct wpa_bss *bss;
3078         char **paths;
3079         unsigned int i = 0;
3080         dbus_bool_t success = FALSE;
3081
3082         paths = os_calloc(wpa_s->num_bss, sizeof(char *));
3083         if (!paths) {
3084                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
3085                 return FALSE;
3086         }
3087
3088         /* Loop through scan results and append each result's object path */
3089         dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
3090                 paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
3091                 if (paths[i] == NULL) {
3092                         dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
3093                                              "no memory");
3094                         goto out;
3095                 }
3096                 /* Construct the object path for this BSS. */
3097                 os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
3098                             "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
3099                             wpa_s->dbus_new_path, bss->id);
3100         }
3101
3102         success = wpas_dbus_simple_array_property_getter(iter,
3103                                                          DBUS_TYPE_OBJECT_PATH,
3104                                                          paths, wpa_s->num_bss,
3105                                                          error);
3106
3107 out:
3108         while (i)
3109                 os_free(paths[--i]);
3110         os_free(paths);
3111         return success;
3112 }
3113
3114
3115 /**
3116  * wpas_dbus_getter_networks - Get array of networks objects
3117  * @iter: Pointer to incoming dbus message iter
3118  * @error: Location to store error on failure
3119  * @user_data: Function specific data
3120  * Returns: TRUE on success, FALSE on failure
3121  *
3122  * Getter for "Networks" property.
3123  */
3124 dbus_bool_t wpas_dbus_getter_networks(DBusMessageIter *iter, DBusError *error,
3125                                       void *user_data)
3126 {
3127         struct wpa_supplicant *wpa_s = user_data;
3128         struct wpa_ssid *ssid;
3129         char **paths;
3130         unsigned int i = 0, num = 0;
3131         dbus_bool_t success = FALSE;
3132
3133         if (wpa_s->conf == NULL) {
3134                 wpa_printf(MSG_ERROR, "%s[dbus]: An error occurred getting "
3135                            "networks list.", __func__);
3136                 dbus_set_error(error, DBUS_ERROR_FAILED, "%s: an error "
3137                                "occurred getting the networks list", __func__);
3138                 return FALSE;
3139         }
3140
3141         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
3142                 if (!network_is_persistent_group(ssid))
3143                         num++;
3144
3145         paths = os_calloc(num, sizeof(char *));
3146         if (!paths) {
3147                 dbus_set_error(error, DBUS_ERROR_NO_MEMORY, "no memory");
3148                 return FALSE;
3149         }
3150
3151         /* Loop through configured networks and append object path of each */
3152         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
3153                 if (network_is_persistent_group(ssid))
3154                         continue;
3155                 paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
3156                 if (paths[i] == NULL) {
3157                         dbus_set_error(error, DBUS_ERROR_NO_MEMORY, "no memory");
3158                         goto out;
3159                 }
3160
3161                 /* Construct the object path for this network. */
3162                 os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
3163                             "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
3164                             wpa_s->dbus_new_path, ssid->id);
3165         }
3166
3167         success = wpas_dbus_simple_array_property_getter(iter,
3168                                                          DBUS_TYPE_OBJECT_PATH,
3169                                                          paths, num, error);
3170
3171 out:
3172         while (i)
3173                 os_free(paths[--i]);
3174         os_free(paths);
3175         return success;
3176 }
3177
3178
3179 /**
3180  * wpas_dbus_getter_blobs - Get all blobs defined for this interface
3181  * @iter: Pointer to incoming dbus message iter
3182  * @error: Location to store error on failure
3183  * @user_data: Function specific data
3184  * Returns: TRUE on success, FALSE on failure
3185  *
3186  * Getter for "Blobs" property.
3187  */
3188 dbus_bool_t wpas_dbus_getter_blobs(DBusMessageIter *iter, DBusError *error,
3189                                    void *user_data)
3190 {
3191         struct wpa_supplicant *wpa_s = user_data;
3192         DBusMessageIter variant_iter, dict_iter, entry_iter, array_iter;
3193         struct wpa_config_blob *blob;
3194
3195         if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
3196                                               "a{say}", &variant_iter) ||
3197             !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
3198                                               "{say}", &dict_iter)) {
3199                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
3200                 return FALSE;
3201         }
3202
3203         blob = wpa_s->conf->blobs;
3204         while (blob) {
3205                 if (!dbus_message_iter_open_container(&dict_iter,
3206                                                       DBUS_TYPE_DICT_ENTRY,
3207                                                       NULL, &entry_iter) ||
3208                     !dbus_message_iter_append_basic(&entry_iter,
3209                                                     DBUS_TYPE_STRING,
3210                                                     &(blob->name)) ||
3211                     !dbus_message_iter_open_container(&entry_iter,
3212                                                       DBUS_TYPE_ARRAY,
3213                                                       DBUS_TYPE_BYTE_AS_STRING,
3214                                                       &array_iter) ||
3215                     !dbus_message_iter_append_fixed_array(&array_iter,
3216                                                           DBUS_TYPE_BYTE,
3217                                                           &(blob->data),
3218                                                           blob->len) ||
3219                     !dbus_message_iter_close_container(&entry_iter,
3220                                                        &array_iter) ||
3221                     !dbus_message_iter_close_container(&dict_iter,
3222                                                        &entry_iter)) {
3223                         dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
3224                                              "no memory");
3225                         return FALSE;
3226                 }
3227
3228                 blob = blob->next;
3229         }
3230
3231         if (!dbus_message_iter_close_container(&variant_iter, &dict_iter) ||
3232             !dbus_message_iter_close_container(iter, &variant_iter)) {
3233                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
3234                 return FALSE;
3235         }
3236
3237         return TRUE;
3238 }
3239
3240
3241 static struct wpa_bss * get_bss_helper(struct bss_handler_args *args,
3242                                        DBusError *error, const char *func_name)
3243 {
3244         struct wpa_bss *res = wpa_bss_get_id(args->wpa_s, args->id);
3245
3246         if (!res) {
3247                 wpa_printf(MSG_ERROR, "%s[dbus]: no bss with id %d found",
3248                            func_name, args->id);
3249                 dbus_set_error(error, DBUS_ERROR_FAILED,
3250                                "%s: BSS %d not found",
3251                                func_name, args->id);
3252         }
3253
3254         return res;
3255 }
3256
3257
3258 /**
3259  * wpas_dbus_getter_bss_bssid - Return the BSSID of a BSS
3260  * @iter: Pointer to incoming dbus message iter
3261  * @error: Location to store error on failure
3262  * @user_data: Function specific data
3263  * Returns: TRUE on success, FALSE on failure
3264  *
3265  * Getter for "BSSID" property.
3266  */
3267 dbus_bool_t wpas_dbus_getter_bss_bssid(DBusMessageIter *iter, DBusError *error,
3268                                        void *user_data)
3269 {
3270         struct bss_handler_args *args = user_data;
3271         struct wpa_bss *res;
3272
3273         res = get_bss_helper(args, error, __func__);
3274         if (!res)
3275                 return FALSE;
3276
3277         return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
3278                                                       res->bssid, ETH_ALEN,
3279                                                       error);
3280 }
3281
3282
3283 /**
3284  * wpas_dbus_getter_bss_ssid - Return the SSID of a BSS
3285  * @iter: Pointer to incoming dbus message iter
3286  * @error: Location to store error on failure
3287  * @user_data: Function specific data
3288  * Returns: TRUE on success, FALSE on failure
3289  *
3290  * Getter for "SSID" property.
3291  */
3292 dbus_bool_t wpas_dbus_getter_bss_ssid(DBusMessageIter *iter, DBusError *error,
3293                                       void *user_data)
3294 {
3295         struct bss_handler_args *args = user_data;
3296         struct wpa_bss *res;
3297
3298         res = get_bss_helper(args, error, __func__);
3299         if (!res)
3300                 return FALSE;
3301
3302         return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
3303                                                       res->ssid, res->ssid_len,
3304                                                       error);
3305 }
3306
3307
3308 /**
3309  * wpas_dbus_getter_bss_privacy - Return the privacy flag of a BSS
3310  * @iter: Pointer to incoming dbus message iter
3311  * @error: Location to store error on failure
3312  * @user_data: Function specific data
3313  * Returns: TRUE on success, FALSE on failure
3314  *
3315  * Getter for "Privacy" property.
3316  */
3317 dbus_bool_t wpas_dbus_getter_bss_privacy(DBusMessageIter *iter,
3318                                          DBusError *error, void *user_data)
3319 {
3320         struct bss_handler_args *args = user_data;
3321         struct wpa_bss *res;
3322         dbus_bool_t privacy;
3323
3324         res = get_bss_helper(args, error, __func__);
3325         if (!res)
3326                 return FALSE;
3327
3328         privacy = (res->caps & IEEE80211_CAP_PRIVACY) ? TRUE : FALSE;
3329         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
3330                                                 &privacy, error);
3331 }
3332
3333
3334 /**
3335  * wpas_dbus_getter_bss_mode - Return the mode of a BSS
3336  * @iter: Pointer to incoming dbus message iter
3337  * @error: Location to store error on failure
3338  * @user_data: Function specific data
3339  * Returns: TRUE on success, FALSE on failure
3340  *
3341  * Getter for "Mode" property.
3342  */
3343 dbus_bool_t wpas_dbus_getter_bss_mode(DBusMessageIter *iter, DBusError *error,
3344                                       void *user_data)
3345 {
3346         struct bss_handler_args *args = user_data;
3347         struct wpa_bss *res;
3348         const char *mode;
3349
3350         res = get_bss_helper(args, error, __func__);
3351         if (!res)
3352                 return FALSE;
3353
3354         if (res->caps & IEEE80211_CAP_IBSS)
3355                 mode = "ad-hoc";
3356         else
3357                 mode = "infrastructure";
3358
3359         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_STRING,
3360                                                 &mode, error);
3361 }
3362
3363
3364 /**
3365  * wpas_dbus_getter_bss_level - Return the signal strength of a BSS
3366  * @iter: Pointer to incoming dbus message iter
3367  * @error: Location to store error on failure
3368  * @user_data: Function specific data
3369  * Returns: TRUE on success, FALSE on failure
3370  *
3371  * Getter for "Level" property.
3372  */
3373 dbus_bool_t wpas_dbus_getter_bss_signal(DBusMessageIter *iter,
3374                                         DBusError *error, void *user_data)
3375 {
3376         struct bss_handler_args *args = user_data;
3377         struct wpa_bss *res;
3378         s16 level;
3379
3380         res = get_bss_helper(args, error, __func__);
3381         if (!res)
3382                 return FALSE;
3383
3384         level = (s16) res->level;
3385         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_INT16,
3386                                                 &level, error);
3387 }
3388
3389
3390 /**
3391  * wpas_dbus_getter_bss_frequency - Return the frequency of a BSS
3392  * @iter: Pointer to incoming dbus message iter
3393  * @error: Location to store error on failure
3394  * @user_data: Function specific data
3395  * Returns: TRUE on success, FALSE on failure
3396  *
3397  * Getter for "Frequency" property.
3398  */
3399 dbus_bool_t wpas_dbus_getter_bss_frequency(DBusMessageIter *iter,
3400                                            DBusError *error, void *user_data)
3401 {
3402         struct bss_handler_args *args = user_data;
3403         struct wpa_bss *res;
3404         u16 freq;
3405
3406         res = get_bss_helper(args, error, __func__);
3407         if (!res)
3408                 return FALSE;
3409
3410         freq = (u16) res->freq;
3411         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_UINT16,
3412                                                 &freq, error);
3413 }
3414
3415
3416 static int cmp_u8s_desc(const void *a, const void *b)
3417 {
3418         return (*(u8 *) b - *(u8 *) a);
3419 }
3420
3421
3422 /**
3423  * wpas_dbus_getter_bss_rates - Return available bit rates of a BSS
3424  * @iter: Pointer to incoming dbus message iter
3425  * @error: Location to store error on failure
3426  * @user_data: Function specific data
3427  * Returns: TRUE on success, FALSE on failure
3428  *
3429  * Getter for "Rates" property.
3430  */
3431 dbus_bool_t wpas_dbus_getter_bss_rates(DBusMessageIter *iter,
3432                                        DBusError *error, void *user_data)
3433 {
3434         struct bss_handler_args *args = user_data;
3435         struct wpa_bss *res;
3436         u8 *ie_rates = NULL;
3437         u32 *real_rates;
3438         int rates_num, i;
3439         dbus_bool_t success = FALSE;
3440
3441         res = get_bss_helper(args, error, __func__);
3442         if (!res)
3443                 return FALSE;
3444
3445         rates_num = wpa_bss_get_bit_rates(res, &ie_rates);
3446         if (rates_num < 0)
3447                 return FALSE;
3448
3449         qsort(ie_rates, rates_num, 1, cmp_u8s_desc);
3450
3451         real_rates = os_malloc(sizeof(u32) * rates_num);
3452         if (!real_rates) {
3453                 os_free(ie_rates);
3454                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
3455                 return FALSE;
3456         }
3457
3458         for (i = 0; i < rates_num; i++)
3459                 real_rates[i] = ie_rates[i] * 500000;
3460
3461         success = wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_UINT32,
3462                                                          real_rates, rates_num,
3463                                                          error);
3464
3465         os_free(ie_rates);
3466         os_free(real_rates);
3467         return success;
3468 }
3469
3470
3471 static dbus_bool_t wpas_dbus_get_bss_security_prop(DBusMessageIter *iter,
3472                                                    struct wpa_ie_data *ie_data,
3473                                                    DBusError *error)
3474 {
3475         DBusMessageIter iter_dict, variant_iter;
3476         const char *group;
3477         const char *pairwise[3]; /* max 3 pairwise ciphers is supported */
3478         const char *key_mgmt[7]; /* max 7 key managements may be supported */
3479         int n;
3480
3481         if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
3482                                               "a{sv}", &variant_iter))
3483                 goto nomem;
3484
3485         if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
3486                 goto nomem;
3487
3488         /* KeyMgmt */
3489         n = 0;
3490         if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK)
3491                 key_mgmt[n++] = "wpa-psk";
3492         if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_PSK)
3493                 key_mgmt[n++] = "wpa-ft-psk";
3494         if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
3495                 key_mgmt[n++] = "wpa-psk-sha256";
3496         if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X)
3497                 key_mgmt[n++] = "wpa-eap";
3498         if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
3499                 key_mgmt[n++] = "wpa-ft-eap";
3500         if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
3501                 key_mgmt[n++] = "wpa-eap-sha256";
3502         if (ie_data->key_mgmt & WPA_KEY_MGMT_NONE)
3503                 key_mgmt[n++] = "wpa-none";
3504
3505         if (!wpa_dbus_dict_append_string_array(&iter_dict, "KeyMgmt",
3506                                                key_mgmt, n))
3507                 goto nomem;
3508
3509         /* Group */
3510         switch (ie_data->group_cipher) {
3511         case WPA_CIPHER_WEP40:
3512                 group = "wep40";
3513                 break;
3514         case WPA_CIPHER_TKIP:
3515                 group = "tkip";
3516                 break;
3517         case WPA_CIPHER_CCMP:
3518                 group = "ccmp";
3519                 break;
3520         case WPA_CIPHER_GCMP:
3521                 group = "gcmp";
3522                 break;
3523         case WPA_CIPHER_WEP104:
3524                 group = "wep104";
3525                 break;
3526         default:
3527                 group = "";
3528                 break;
3529         }
3530
3531         if (!wpa_dbus_dict_append_string(&iter_dict, "Group", group))
3532                 goto nomem;
3533
3534         /* Pairwise */
3535         n = 0;
3536         if (ie_data->pairwise_cipher & WPA_CIPHER_TKIP)
3537                 pairwise[n++] = "tkip";
3538         if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP)
3539                 pairwise[n++] = "ccmp";
3540         if (ie_data->pairwise_cipher & WPA_CIPHER_GCMP)
3541                 pairwise[n++] = "gcmp";
3542
3543         if (!wpa_dbus_dict_append_string_array(&iter_dict, "Pairwise",
3544                                                pairwise, n))
3545                 goto nomem;
3546
3547         /* Management group (RSN only) */
3548         if (ie_data->proto == WPA_PROTO_RSN) {
3549                 switch (ie_data->mgmt_group_cipher) {
3550 #ifdef CONFIG_IEEE80211W
3551                 case WPA_CIPHER_AES_128_CMAC:
3552                         group = "aes128cmac";
3553                         break;
3554 #endif /* CONFIG_IEEE80211W */
3555                 default:
3556                         group = "";
3557                         break;
3558                 }
3559
3560                 if (!wpa_dbus_dict_append_string(&iter_dict, "MgmtGroup",
3561                                                  group))
3562                         goto nomem;
3563         }
3564
3565         if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
3566                 goto nomem;
3567         if (!dbus_message_iter_close_container(iter, &variant_iter))
3568                 goto nomem;
3569
3570         return TRUE;
3571
3572 nomem:
3573         dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
3574         return FALSE;
3575 }
3576
3577
3578 /**
3579  * wpas_dbus_getter_bss_wpa - Return the WPA options of a BSS
3580  * @iter: Pointer to incoming dbus message iter
3581  * @error: Location to store error on failure
3582  * @user_data: Function specific data
3583  * Returns: TRUE on success, FALSE on failure
3584  *
3585  * Getter for "WPA" property.
3586  */
3587 dbus_bool_t wpas_dbus_getter_bss_wpa(DBusMessageIter *iter, DBusError *error,
3588                                      void *user_data)
3589 {
3590         struct bss_handler_args *args = user_data;
3591         struct wpa_bss *res;
3592         struct wpa_ie_data wpa_data;
3593         const u8 *ie;
3594
3595         res = get_bss_helper(args, error, __func__);
3596         if (!res)
3597                 return FALSE;
3598
3599         os_memset(&wpa_data, 0, sizeof(wpa_data));
3600         ie = wpa_bss_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
3601         if (ie) {
3602                 if (wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
3603                         dbus_set_error_const(error, DBUS_ERROR_FAILED,
3604                                              "failed to parse WPA IE");
3605                         return FALSE;
3606                 }
3607         }
3608
3609         return wpas_dbus_get_bss_security_prop(iter, &wpa_data, error);
3610 }
3611
3612
3613 /**
3614  * wpas_dbus_getter_bss_rsn - Return the RSN options of a BSS
3615  * @iter: Pointer to incoming dbus message iter
3616  * @error: Location to store error on failure
3617  * @user_data: Function specific data
3618  * Returns: TRUE on success, FALSE on failure
3619  *
3620  * Getter for "RSN" property.
3621  */
3622 dbus_bool_t wpas_dbus_getter_bss_rsn(DBusMessageIter *iter, DBusError *error,
3623                                      void *user_data)
3624 {
3625         struct bss_handler_args *args = user_data;
3626         struct wpa_bss *res;
3627         struct wpa_ie_data wpa_data;
3628         const u8 *ie;
3629
3630         res = get_bss_helper(args, error, __func__);
3631         if (!res)
3632                 return FALSE;
3633
3634         os_memset(&wpa_data, 0, sizeof(wpa_data));
3635         ie = wpa_bss_get_ie(res, WLAN_EID_RSN);
3636         if (ie) {
3637                 if (wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data) < 0) {
3638                         dbus_set_error_const(error, DBUS_ERROR_FAILED,
3639                                              "failed to parse RSN IE");
3640                         return FALSE;
3641                 }
3642         }
3643
3644         return wpas_dbus_get_bss_security_prop(iter, &wpa_data, error);
3645 }
3646
3647
3648 /**
3649  * wpas_dbus_getter_bss_wps - Return the WPS options of a BSS
3650  * @iter: Pointer to incoming dbus message iter
3651  * @error: Location to store error on failure
3652  * @user_data: Function specific data
3653  * Returns: TRUE on success, FALSE on failure
3654  *
3655  * Getter for "WPS" property.
3656  */
3657 dbus_bool_t wpas_dbus_getter_bss_wps(DBusMessageIter *iter, DBusError *error,
3658                                      void *user_data)
3659 {
3660         struct bss_handler_args *args = user_data;
3661         struct wpa_bss *res;
3662 #ifdef CONFIG_WPS
3663         struct wpabuf *wps_ie;
3664 #endif /* CONFIG_WPS */
3665         DBusMessageIter iter_dict, variant_iter;
3666         const char *type = "";
3667
3668         res = get_bss_helper(args, error, __func__);
3669         if (!res)
3670                 return FALSE;
3671
3672         if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT,
3673                                               "a{sv}", &variant_iter))
3674                 goto nomem;
3675
3676         if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
3677                 goto nomem;
3678
3679 #ifdef CONFIG_WPS
3680         wps_ie = wpa_bss_get_vendor_ie_multi(res, WPS_IE_VENDOR_TYPE);
3681         if (wps_ie) {
3682                 if (wps_is_selected_pbc_registrar(wps_ie))
3683                         type = "pbc";
3684                 else if (wps_is_selected_pin_registrar(wps_ie))
3685                         type = "pin";
3686         }
3687 #endif /* CONFIG_WPS */
3688
3689         if (!wpa_dbus_dict_append_string(&iter_dict, "Type", type))
3690                 goto nomem;
3691
3692         if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
3693                 goto nomem;
3694         if (!dbus_message_iter_close_container(iter, &variant_iter))
3695                 goto nomem;
3696
3697         return TRUE;
3698
3699 nomem:
3700         dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
3701         return FALSE;
3702 }
3703
3704
3705 /**
3706  * wpas_dbus_getter_bss_ies - Return all IEs of a BSS
3707  * @iter: Pointer to incoming dbus message iter
3708  * @error: Location to store error on failure
3709  * @user_data: Function specific data
3710  * Returns: TRUE on success, FALSE on failure
3711  *
3712  * Getter for "IEs" property.
3713  */
3714 dbus_bool_t wpas_dbus_getter_bss_ies(DBusMessageIter *iter, DBusError *error,
3715                                      void *user_data)
3716 {
3717         struct bss_handler_args *args = user_data;
3718         struct wpa_bss *res;
3719
3720         res = get_bss_helper(args, error, __func__);
3721         if (!res)
3722                 return FALSE;
3723
3724         return wpas_dbus_simple_array_property_getter(iter, DBUS_TYPE_BYTE,
3725                                                       res + 1, res->ie_len,
3726                                                       error);
3727 }
3728
3729
3730 /**
3731  * wpas_dbus_getter_enabled - Check whether network is enabled or disabled
3732  * @iter: Pointer to incoming dbus message iter
3733  * @error: Location to store error on failure
3734  * @user_data: Function specific data
3735  * Returns: TRUE on success, FALSE on failure
3736  *
3737  * Getter for "enabled" property of a configured network.
3738  */
3739 dbus_bool_t wpas_dbus_getter_enabled(DBusMessageIter *iter, DBusError *error,
3740                                      void *user_data)
3741 {
3742         struct network_handler_args *net = user_data;
3743         dbus_bool_t enabled = net->ssid->disabled ? FALSE : TRUE;
3744
3745         return wpas_dbus_simple_property_getter(iter, DBUS_TYPE_BOOLEAN,
3746                                                 &enabled, error);
3747 }
3748
3749
3750 /**
3751  * wpas_dbus_setter_enabled - Mark a configured network as enabled or disabled
3752  * @iter: Pointer to incoming dbus message iter
3753  * @error: Location to store error on failure
3754  * @user_data: Function specific data
3755  * Returns: TRUE on success, FALSE on failure
3756  *
3757  * Setter for "Enabled" property of a configured network.
3758  */
3759 dbus_bool_t wpas_dbus_setter_enabled(DBusMessageIter *iter, DBusError *error,
3760                                      void *user_data)
3761 {
3762         struct network_handler_args *net = user_data;
3763         struct wpa_supplicant *wpa_s;
3764         struct wpa_ssid *ssid;
3765         dbus_bool_t enable;
3766
3767         if (!wpas_dbus_simple_property_setter(iter, error, DBUS_TYPE_BOOLEAN,
3768                                               &enable))
3769                 return FALSE;
3770
3771         wpa_s = net->wpa_s;
3772         ssid = net->ssid;
3773
3774         if (enable)
3775                 wpa_supplicant_enable_network(wpa_s, ssid);
3776         else
3777                 wpa_supplicant_disable_network(wpa_s, ssid);
3778
3779         return TRUE;
3780 }
3781
3782
3783 /**
3784  * wpas_dbus_getter_network_properties - Get options for a configured network
3785  * @iter: Pointer to incoming dbus message iter
3786  * @error: Location to store error on failure
3787  * @user_data: Function specific data
3788  * Returns: TRUE on success, FALSE on failure
3789  *
3790  * Getter for "Properties" property of a configured network.
3791  */
3792 dbus_bool_t wpas_dbus_getter_network_properties(DBusMessageIter *iter,
3793                                                 DBusError *error,
3794                                                 void *user_data)
3795 {
3796         struct network_handler_args *net = user_data;
3797         DBusMessageIter variant_iter, dict_iter;
3798         char **iterator;
3799         char **props = wpa_config_get_all(net->ssid, 1);
3800         dbus_bool_t success = FALSE;
3801
3802         if (!props) {
3803                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
3804                 return FALSE;
3805         }
3806
3807         if (!dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "a{sv}",
3808                                               &variant_iter) ||
3809             !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) {
3810                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
3811                 goto out;
3812         }
3813
3814         iterator = props;
3815         while (*iterator) {
3816                 if (!wpa_dbus_dict_append_string(&dict_iter, *iterator,
3817                                                  *(iterator + 1))) {
3818                         dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY,
3819                                              "no memory");
3820                         goto out;
3821                 }
3822                 iterator += 2;
3823         }
3824
3825
3826         if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
3827             !dbus_message_iter_close_container(iter, &variant_iter)) {
3828                 dbus_set_error_const(error, DBUS_ERROR_NO_MEMORY, "no memory");
3829                 goto out;
3830         }
3831
3832         success = TRUE;
3833
3834 out:
3835         iterator = props;
3836         while (*iterator) {
3837                 os_free(*iterator);
3838                 iterator++;
3839         }
3840         os_free(props);
3841         return success;
3842 }
3843
3844
3845 /**
3846  * wpas_dbus_setter_network_properties - Set options for a configured network
3847  * @iter: Pointer to incoming dbus message iter
3848  * @error: Location to store error on failure
3849  * @user_data: Function specific data
3850  * Returns: TRUE on success, FALSE on failure
3851  *
3852  * Setter for "Properties" property of a configured network.
3853  */
3854 dbus_bool_t wpas_dbus_setter_network_properties(DBusMessageIter *iter,
3855                                                 DBusError *error,
3856                                                 void *user_data)
3857 {
3858         struct network_handler_args *net = user_data;
3859         struct wpa_ssid *ssid = net->ssid;
3860         DBusMessageIter variant_iter;
3861
3862         dbus_message_iter_recurse(iter, &variant_iter);
3863         return set_network_properties(net->wpa_s, ssid, &variant_iter, error);
3864 }
3865
3866
3867 #ifdef CONFIG_AP
3868
3869 DBusMessage * wpas_dbus_handler_subscribe_preq(
3870         DBusMessage *message, struct wpa_supplicant *wpa_s)
3871 {
3872         struct wpas_dbus_priv *priv = wpa_s->global->dbus;
3873         char *name;
3874
3875         if (wpa_s->preq_notify_peer != NULL) {
3876                 if (os_strcmp(dbus_message_get_sender(message),
3877                               wpa_s->preq_notify_peer) == 0)
3878                         return NULL;
3879
3880                 return dbus_message_new_error(message,
3881                         WPAS_DBUS_ERROR_SUBSCRIPTION_IN_USE,
3882                         "Another application is already subscribed");
3883         }
3884
3885         name = os_strdup(dbus_message_get_sender(message));
3886         if (!name)
3887                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
3888                                               "out of memory");
3889
3890         wpa_s->preq_notify_peer = name;
3891
3892         /* Subscribe to clean up if application closes socket */
3893         wpas_dbus_subscribe_noc(priv);
3894
3895         /*
3896          * Double-check it's still alive to make sure that we didn't
3897          * miss the NameOwnerChanged signal, e.g. while strdup'ing.
3898          */
3899         if (!dbus_bus_name_has_owner(priv->con, name, NULL)) {
3900                 /*
3901                  * Application no longer exists, clean up.
3902                  * The return value is irrelevant now.
3903                  *
3904                  * Need to check if the NameOwnerChanged handling
3905                  * already cleaned up because we have processed
3906                  * DBus messages while checking if the name still
3907                  * has an owner.
3908                  */
3909                 if (!wpa_s->preq_notify_peer)
3910                         return NULL;
3911                 os_free(wpa_s->preq_notify_peer);
3912                 wpa_s->preq_notify_peer = NULL;
3913                 wpas_dbus_unsubscribe_noc(priv);
3914         }
3915
3916         return NULL;
3917 }
3918
3919
3920 DBusMessage * wpas_dbus_handler_unsubscribe_preq(
3921         DBusMessage *message, struct wpa_supplicant *wpa_s)
3922 {
3923         struct wpas_dbus_priv *priv = wpa_s->global->dbus;
3924
3925         if (!wpa_s->preq_notify_peer)
3926                 return dbus_message_new_error(message,
3927                         WPAS_DBUS_ERROR_NO_SUBSCRIPTION,
3928                         "Not subscribed");
3929
3930         if (os_strcmp(wpa_s->preq_notify_peer,
3931                       dbus_message_get_sender(message)))
3932                 return dbus_message_new_error(message,
3933                         WPAS_DBUS_ERROR_SUBSCRIPTION_EPERM,
3934                         "Can't unsubscribe others");
3935
3936         os_free(wpa_s->preq_notify_peer);
3937         wpa_s->preq_notify_peer = NULL;
3938         wpas_dbus_unsubscribe_noc(priv);
3939         return NULL;
3940 }
3941
3942
3943 void wpas_dbus_signal_preq(struct wpa_supplicant *wpa_s,
3944                            const u8 *addr, const u8 *dst, const u8 *bssid,
3945                            const u8 *ie, size_t ie_len, u32 ssi_signal)
3946 {
3947         DBusMessage *msg;
3948         DBusMessageIter iter, dict_iter;
3949         struct wpas_dbus_priv *priv = wpa_s->global->dbus;
3950
3951         /* Do nothing if the control interface is not turned on */
3952         if (priv == NULL)
3953                 return;
3954
3955         if (wpa_s->preq_notify_peer == NULL)
3956                 return;
3957
3958         msg = dbus_message_new_signal(wpa_s->dbus_new_path,
3959                                       WPAS_DBUS_NEW_IFACE_INTERFACE,
3960                                       "ProbeRequest");
3961         if (msg == NULL)
3962                 return;
3963
3964         dbus_message_set_destination(msg, wpa_s->preq_notify_peer);
3965
3966         dbus_message_iter_init_append(msg, &iter);
3967
3968         if (!wpa_dbus_dict_open_write(&iter, &dict_iter))
3969                 goto fail;
3970         if (addr && !wpa_dbus_dict_append_byte_array(&dict_iter, "addr",
3971                                                      (const char *) addr,
3972                                                      ETH_ALEN))
3973                 goto fail;
3974         if (dst && !wpa_dbus_dict_append_byte_array(&dict_iter, "dst",
3975                                                     (const char *) dst,
3976                                                     ETH_ALEN))
3977                 goto fail;
3978         if (bssid && !wpa_dbus_dict_append_byte_array(&dict_iter, "bssid",
3979                                                       (const char *) bssid,
3980                                                       ETH_ALEN))
3981                 goto fail;
3982         if (ie && ie_len && !wpa_dbus_dict_append_byte_array(&dict_iter, "ies",
3983                                                              (const char *) ie,
3984                                                              ie_len))
3985                 goto fail;
3986         if (ssi_signal && !wpa_dbus_dict_append_int32(&dict_iter, "signal",
3987                                                       ssi_signal))
3988                 goto fail;
3989         if (!wpa_dbus_dict_close_write(&iter, &dict_iter))
3990                 goto fail;
3991
3992         dbus_connection_send(priv->con, msg, NULL);
3993         goto out;
3994 fail:
3995         wpa_printf(MSG_ERROR, "dbus: Failed to construct signal");
3996 out:
3997         dbus_message_unref(msg);
3998 }
3999
4000 #endif /* CONFIG_AP */