3e924dcfd7b94f81b53ac6e69d2c875ec89ffab1
[libeap.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 program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  *
11  * Alternatively, this software may be distributed under the terms of BSD
12  * license.
13  *
14  * See README and COPYING for more details.
15  */
16
17 #include "includes.h"
18
19 #include "common.h"
20 #include "common/ieee802_11_defs.h"
21 #include "eap_peer/eap_methods.h"
22 #include "eapol_supp/eapol_supp_sm.h"
23 #include "rsn_supp/wpa.h"
24 #include "../config.h"
25 #include "../wpa_supplicant_i.h"
26 #include "../driver_i.h"
27 #include "../notify.h"
28 #include "../wpas_glue.h"
29 #include "../bss.h"
30 #include "../scan.h"
31 #include "dbus_new_helpers.h"
32 #include "dbus_new.h"
33 #include "dbus_new_handlers.h"
34 #include "dbus_dict_helpers.h"
35
36 extern int wpa_debug_level;
37 extern int wpa_debug_show_keys;
38 extern int wpa_debug_timestamp;
39
40 static const char *debug_strings[] = {
41         "msgdump", "debug", "info", "warning", "error", NULL
42 };
43
44
45 /**
46  * wpas_dbus_new_decompose_object_path - Decompose an interface object path into parts
47  * @path: The dbus object path
48  * @network: (out) the configured network this object path refers to, if any
49  * @bssid: (out) the scanned bssid this object path refers to, if any
50  * Returns: The object path of the network interface this path refers to
51  *
52  * For a given object path, decomposes the object path into object id, network,
53  * and BSSID parts, if those parts exist.
54  */
55 static char * wpas_dbus_new_decompose_object_path(const char *path,
56                                                   char **network,
57                                                   char **bssid)
58 {
59         const unsigned int dev_path_prefix_len =
60                 strlen(WPAS_DBUS_NEW_PATH_INTERFACES "/");
61         char *obj_path_only;
62         char *next_sep;
63
64         /* Be a bit paranoid about path */
65         if (!path || os_strncmp(path, WPAS_DBUS_NEW_PATH_INTERFACES "/",
66                                 dev_path_prefix_len))
67                 return NULL;
68
69         /* Ensure there's something at the end of the path */
70         if ((path + dev_path_prefix_len)[0] == '\0')
71                 return NULL;
72
73         obj_path_only = os_strdup(path);
74         if (obj_path_only == NULL)
75                 return NULL;
76
77         next_sep = os_strchr(obj_path_only + dev_path_prefix_len, '/');
78         if (next_sep != NULL) {
79                 const char *net_part = os_strstr(
80                         next_sep, WPAS_DBUS_NEW_NETWORKS_PART "/");
81                 const char *bssid_part = os_strstr(
82                         next_sep, WPAS_DBUS_NEW_BSSIDS_PART "/");
83
84                 if (network && net_part) {
85                         /* Deal with a request for a configured network */
86                         const char *net_name = net_part +
87                                 os_strlen(WPAS_DBUS_NEW_NETWORKS_PART "/");
88                         *network = NULL;
89                         if (os_strlen(net_name))
90                                 *network = os_strdup(net_name);
91                 } else if (bssid && bssid_part) {
92                         /* Deal with a request for a scanned BSSID */
93                         const char *bssid_name = bssid_part +
94                                 os_strlen(WPAS_DBUS_NEW_BSSIDS_PART "/");
95                         if (strlen(bssid_name))
96                                 *bssid = os_strdup(bssid_name);
97                         else
98                                 *bssid = NULL;
99                 }
100
101                 /* Cut off interface object path before "/" */
102                 *next_sep = '\0';
103         }
104
105         return obj_path_only;
106 }
107
108
109 /**
110  * wpas_dbus_error_unknown_error - Return a new InvalidArgs error message
111  * @message: Pointer to incoming dbus message this error refers to
112  * @arg: Optional string appended to error message
113  * Returns: a dbus error message
114  *
115  * Convenience function to create and return an UnknownError
116  */
117 DBusMessage * wpas_dbus_error_unknown_error(DBusMessage *message,
118                                             const char *arg)
119 {
120         return dbus_message_new_error(message, WPAS_DBUS_ERROR_UNKNOWN_ERROR,
121                                       arg);
122 }
123
124
125 /**
126  * wpas_dbus_error_iface_unknown - Return a new invalid interface error message
127  * @message: Pointer to incoming dbus message this error refers to
128  * Returns: A dbus error message
129  *
130  * Convenience function to create and return an invalid interface error
131  */
132 static DBusMessage * wpas_dbus_error_iface_unknown(DBusMessage *message)
133 {
134         return dbus_message_new_error(message, WPAS_DBUS_ERROR_IFACE_UNKNOWN,
135                                       "wpa_supplicant knows nothing about "
136                                       "this interface.");
137 }
138
139
140 /**
141  * wpas_dbus_error_network_unknown - Return a new NetworkUnknown error message
142  * @message: Pointer to incoming dbus message this error refers to
143  * Returns: a dbus error message
144  *
145  * Convenience function to create and return an invalid network error
146  */
147 static DBusMessage * wpas_dbus_error_network_unknown(DBusMessage *message)
148 {
149         return dbus_message_new_error(message, WPAS_DBUS_ERROR_NETWORK_UNKNOWN,
150                                       "There is no such a network in this "
151                                       "interface.");
152 }
153
154
155 /**
156  * wpas_dbus_error_invalid_args - Return a new InvalidArgs error message
157  * @message: Pointer to incoming dbus message this error refers to
158  * Returns: a dbus error message
159  *
160  * Convenience function to create and return an invalid options error
161  */
162 DBusMessage * wpas_dbus_error_invalid_args(DBusMessage *message,
163                                           const char *arg)
164 {
165         DBusMessage *reply;
166
167         reply = dbus_message_new_error(message, WPAS_DBUS_ERROR_INVALID_ARGS,
168                                        "Did not receive correct message "
169                                        "arguments.");
170         if (arg != NULL)
171                 dbus_message_append_args(reply, DBUS_TYPE_STRING, &arg,
172                                          DBUS_TYPE_INVALID);
173
174         return reply;
175 }
176
177
178 static const char *dont_quote[] = {
179         "key_mgmt", "proto", "pairwise", "auth_alg", "group", "eap",
180         "opensc_engine_path", "pkcs11_engine_path", "pkcs11_module_path",
181         "bssid", NULL
182 };
183
184 static dbus_bool_t should_quote_opt(const char *key)
185 {
186         int i = 0;
187         while (dont_quote[i] != NULL) {
188                 if (os_strcmp(key, dont_quote[i]) == 0)
189                         return FALSE;
190                 i++;
191         }
192         return TRUE;
193 }
194
195 /**
196  * get_iface_by_dbus_path - Get a new network interface
197  * @global: Pointer to global data from wpa_supplicant_init()
198  * @path: Pointer to a dbus object path representing an interface
199  * Returns: Pointer to the interface or %NULL if not found
200  */
201 static struct wpa_supplicant * get_iface_by_dbus_path(
202         struct wpa_global *global, const char *path)
203 {
204         struct wpa_supplicant *wpa_s;
205
206         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
207                 if (os_strcmp(wpa_s->dbus_new_path, path) == 0)
208                         return wpa_s;
209         }
210         return NULL;
211 }
212
213
214 /**
215  * set_network_properties - Set properties of a configured network
216  * @message: Pointer to incoming dbus message
217  * @wpa_s: wpa_supplicant structure for a network interface
218  * @ssid: wpa_ssid structure for a configured network
219  * @iter: DBus message iterator containing dictionary of network
220  * properties to set.
221  * Returns: NULL when succeed or DBus error on failure
222  *
223  * Sets network configuration with parameters given id DBus dictionary
224  */
225 static DBusMessage * set_network_properties(DBusMessage *message,
226                                             struct wpa_supplicant *wpa_s,
227                                             struct wpa_ssid *ssid,
228                                             DBusMessageIter *iter)
229 {
230
231         struct wpa_dbus_dict_entry entry = { .type = DBUS_TYPE_STRING };
232         DBusMessage *reply = NULL;
233         DBusMessageIter iter_dict;
234
235         if (!wpa_dbus_dict_open_read(iter, &iter_dict))
236                 return wpas_dbus_error_invalid_args(message, NULL);
237
238         while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
239                 char *value = NULL;
240                 size_t size = 50;
241                 int ret;
242                 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry)) {
243                         reply = wpas_dbus_error_invalid_args(message, NULL);
244                         break;
245                 }
246                 if (entry.type == DBUS_TYPE_ARRAY &&
247                     entry.array_type == DBUS_TYPE_BYTE) {
248                         if (entry.array_len <= 0)
249                                 goto error;
250
251                         size = entry.array_len * 2 + 1;
252                         value = os_zalloc(size);
253                         if (value == NULL)
254                                 goto error;
255
256                         ret = wpa_snprintf_hex(value, size,
257                                                (u8 *) entry.bytearray_value,
258                                                entry.array_len);
259                         if (ret <= 0)
260                                 goto error;
261                 } else if (entry.type == DBUS_TYPE_STRING) {
262                         if (should_quote_opt(entry.key)) {
263                                 size = os_strlen(entry.str_value);
264                                 if (size <= 0)
265                                         goto error;
266
267                                 size += 3;
268                                 value = os_zalloc(size);
269                                 if (value == NULL)
270                                         goto error;
271
272                                 ret = os_snprintf(value, size, "\"%s\"",
273                                                   entry.str_value);
274                                 if (ret < 0 || (size_t) ret != (size - 1))
275                                         goto error;
276                         } else {
277                                 value = os_strdup(entry.str_value);
278                                 if (value == NULL)
279                                         goto error;
280                         }
281                 } else if (entry.type == DBUS_TYPE_UINT32) {
282                         value = os_zalloc(size);
283                         if (value == NULL)
284                                 goto error;
285
286                         ret = os_snprintf(value, size, "%u",
287                                           entry.uint32_value);
288                         if (ret <= 0)
289                                 goto error;
290                 } else if (entry.type == DBUS_TYPE_INT32) {
291                         value = os_zalloc(size);
292                         if (value == NULL)
293                                 goto error;
294
295                         ret = os_snprintf(value, size, "%d",
296                                           entry.int32_value);
297                         if (ret <= 0)
298                                 goto error;
299                 } else
300                         goto error;
301
302                 if (wpa_config_set(ssid, entry.key, value, 0) < 0)
303                         goto error;
304
305                 if ((os_strcmp(entry.key, "psk") == 0 &&
306                      value[0] == '"' && ssid->ssid_len) ||
307                     (strcmp(entry.key, "ssid") == 0 && ssid->passphrase))
308                         wpa_config_update_psk(ssid);
309                 else if (os_strcmp(entry.key, "priority") == 0)
310                         wpa_config_update_prio_list(wpa_s->conf);
311
312                 os_free(value);
313                 wpa_dbus_dict_entry_clear(&entry);
314                 continue;
315
316         error:
317                 os_free(value);
318                 reply = wpas_dbus_error_invalid_args(message, entry.key);
319                 wpa_dbus_dict_entry_clear(&entry);
320                 break;
321         }
322
323         return reply;
324 }
325
326
327 /**
328  * wpas_dbus_simple_property_getter - Get basic type property
329  * @message: Pointer to incoming dbus message
330  * @type: DBus type of property (must be basic type)
331  * @val: pointer to place holding property value
332  * Returns: The DBus message containing response for Properties.Get call
333  * or DBus error message if error occurred.
334  *
335  * Generic getter for basic type properties. Type is required to be basic.
336  */
337 DBusMessage * wpas_dbus_simple_property_getter(DBusMessage *message,
338                                                const int type, const void *val)
339 {
340         DBusMessage *reply = NULL;
341         DBusMessageIter iter, variant_iter;
342
343         if (!dbus_type_is_basic(type)) {
344                 wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_getter:"
345                            " given type is not basic");
346                 return wpas_dbus_error_unknown_error(message, NULL);
347         }
348
349         if (message == NULL)
350                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
351         else
352                 reply = dbus_message_new_method_return(message);
353
354         if (reply != NULL) {
355                 dbus_message_iter_init_append(reply, &iter);
356                 if (!dbus_message_iter_open_container(
357                             &iter, DBUS_TYPE_VARIANT,
358                             wpa_dbus_type_as_string(type), &variant_iter) ||
359                     !dbus_message_iter_append_basic(&variant_iter, type,
360                                                     val) ||
361                     !dbus_message_iter_close_container(&iter, &variant_iter)) {
362                         wpa_printf(MSG_ERROR, "dbus: "
363                                    "wpas_dbus_simple_property_getter: out of "
364                                    "memory to put property value into "
365                                    "message");
366                         dbus_message_unref(reply);
367                         reply = dbus_message_new_error(message,
368                                                        DBUS_ERROR_NO_MEMORY,
369                                                        NULL);
370                 }
371         } else {
372                 wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_getter:"
373                            " out of memory to return property value");
374                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
375                                                NULL);
376         }
377
378         return reply;
379 }
380
381
382 /**
383  * wpas_dbus_simple_property_setter - Set basic type property
384  * @message: Pointer to incoming dbus message
385  * @type: DBus type of property (must be basic type)
386  * @val: pointer to place where value being set will be stored
387  * Returns: NULL or DBus error message if error occurred.
388  *
389  * Generic setter for basic type properties. Type is required to be basic.
390  */
391 DBusMessage * wpas_dbus_simple_property_setter(DBusMessage *message,
392                                                const int type, void *val)
393 {
394         DBusMessageIter iter, variant_iter;
395
396         if (!dbus_type_is_basic(type)) {
397                 wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_setter:"
398                            " given type is not basic");
399                 return wpas_dbus_error_unknown_error(message, NULL);
400         }
401
402         if (!dbus_message_iter_init(message, &iter)) {
403                 wpa_printf(MSG_ERROR, "dbus: wpas_dbus_simple_property_setter:"
404                            " out of memory to return scanning state");
405                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
406                                               NULL);
407         }
408
409         /* omit first and second argument and get value from third */
410         dbus_message_iter_next(&iter);
411         dbus_message_iter_next(&iter);
412         dbus_message_iter_recurse(&iter, &variant_iter);
413
414         if (dbus_message_iter_get_arg_type(&variant_iter) != type) {
415                 wpa_printf(MSG_DEBUG, "dbus: wpas_dbus_simple_property_setter:"
416                            " wrong property type");
417                 return wpas_dbus_error_invalid_args(message,
418                                                     "wrong property type");
419         }
420         dbus_message_iter_get_basic(&variant_iter, val);
421
422         return NULL;
423 }
424
425
426 /**
427  * wpas_dbus_simple_array_property_getter - Get array type property
428  * @message: Pointer to incoming dbus message
429  * @type: DBus type of property array elements (must be basic type)
430  * @array: pointer to array of elements to put into response message
431  * @array_len: length of above array
432  * Returns: The DBus message containing response for Properties.Get call
433  * or DBus error message if error occurred.
434  *
435  * Generic getter for array type properties. Array elements type is
436  * required to be basic.
437  */
438 DBusMessage * wpas_dbus_simple_array_property_getter(DBusMessage *message,
439                                                      const int type,
440                                                      const void *array,
441                                                      size_t array_len)
442 {
443         DBusMessage *reply = NULL;
444         DBusMessageIter iter, variant_iter, array_iter;
445         char type_str[] = "a?"; /* ? will be replaced with subtype letter; */
446         const char *sub_type_str;
447         size_t element_size, i;
448
449         if (!dbus_type_is_basic(type)) {
450                 wpa_printf(MSG_ERROR, "dbus: "
451                            "wpas_dbus_simple_array_property_getter: given "
452                            "type is not basic");
453                 return wpas_dbus_error_unknown_error(message, NULL);
454         }
455
456         sub_type_str = wpa_dbus_type_as_string(type);
457         type_str[1] = sub_type_str[0];
458
459         if (message == NULL)
460                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
461         else
462                 reply = dbus_message_new_method_return(message);
463         if (reply == NULL) {
464                 wpa_printf(MSG_ERROR, "dbus: "
465                            "wpas_dbus_simple_array_property_getter: out of "
466                            "memory to create return message");
467                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
468                                               NULL);
469         }
470
471         dbus_message_iter_init_append(reply, &iter);
472
473         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
474                                               type_str, &variant_iter) ||
475             !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
476                                               sub_type_str, &array_iter)) {
477                 wpa_printf(MSG_ERROR, "dbus: "
478                            "wpas_dbus_simple_array_property_getter: out of "
479                            "memory to open container");
480                 dbus_message_unref(reply);
481                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
482                                               NULL);
483         }
484
485         switch(type) {
486         case DBUS_TYPE_BYTE:
487         case DBUS_TYPE_BOOLEAN:
488                 element_size = 1;
489                 break;
490         case DBUS_TYPE_INT16:
491         case DBUS_TYPE_UINT16:
492                 element_size = sizeof(uint16_t);
493                 break;
494         case DBUS_TYPE_INT32:
495         case DBUS_TYPE_UINT32:
496                 element_size = sizeof(uint32_t);
497                 break;
498         case DBUS_TYPE_INT64:
499         case DBUS_TYPE_UINT64:
500                 element_size = sizeof(uint64_t);
501                 break;
502         case DBUS_TYPE_DOUBLE:
503                 element_size = sizeof(double);
504                 break;
505         case DBUS_TYPE_STRING:
506         case DBUS_TYPE_OBJECT_PATH:
507                 element_size = sizeof(char *);
508                 break;
509         default:
510                 wpa_printf(MSG_ERROR, "dbus: "
511                            "wpas_dbus_simple_array_property_getter: "
512                            "fatal: unknown element type");
513                 element_size = 1;
514                 break;
515         }
516
517         for (i = 0; i < array_len; i++) {
518                 dbus_message_iter_append_basic(&array_iter, type,
519                                                array + i * element_size);
520         }
521
522         if (!dbus_message_iter_close_container(&variant_iter, &array_iter) ||
523             !dbus_message_iter_close_container(&iter, &variant_iter)) {
524                 wpa_printf(MSG_ERROR, "dbus: "
525                            "wpas_dbus_simple_array_property_getter: out of "
526                            "memory to close container");
527                 dbus_message_unref(reply);
528                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
529                                               NULL);
530         }
531
532         return reply;
533 }
534
535
536 /**
537  * wpas_dbus_handler_create_interface - Request registration of a network iface
538  * @message: Pointer to incoming dbus message
539  * @global: %wpa_supplicant global data structure
540  * Returns: The object path of the new interface object,
541  *          or a dbus error message with more information
542  *
543  * Handler function for "CreateInterface" method call. Handles requests
544  * by dbus clients to register a network interface that wpa_supplicant
545  * will manage.
546  */
547 DBusMessage * wpas_dbus_handler_create_interface(DBusMessage *message,
548                                                  struct wpa_global *global)
549 {
550         DBusMessageIter iter_dict;
551         DBusMessage *reply = NULL;
552         DBusMessageIter iter;
553         struct wpa_dbus_dict_entry entry;
554         char *driver = NULL;
555         char *ifname = NULL;
556         char *bridge_ifname = NULL;
557
558         dbus_message_iter_init(message, &iter);
559
560         if (!wpa_dbus_dict_open_read(&iter, &iter_dict))
561                 goto error;
562         while (wpa_dbus_dict_has_dict_entry(&iter_dict)) {
563                 if (!wpa_dbus_dict_get_entry(&iter_dict, &entry))
564                         goto error;
565                 if (!strcmp(entry.key, "Driver") &&
566                     (entry.type == DBUS_TYPE_STRING)) {
567                         driver = os_strdup(entry.str_value);
568                         wpa_dbus_dict_entry_clear(&entry);
569                         if (driver == NULL)
570                                 goto error;
571                 } else if (!strcmp(entry.key, "Ifname") &&
572                            (entry.type == DBUS_TYPE_STRING)) {
573                         ifname = os_strdup(entry.str_value);
574                         wpa_dbus_dict_entry_clear(&entry);
575                         if (ifname == NULL)
576                                 goto error;
577                 } else if (!strcmp(entry.key, "BridgeIfname") &&
578                            (entry.type == DBUS_TYPE_STRING)) {
579                         bridge_ifname = os_strdup(entry.str_value);
580                         wpa_dbus_dict_entry_clear(&entry);
581                         if (bridge_ifname == NULL)
582                                 goto error;
583                 } else {
584                         wpa_dbus_dict_entry_clear(&entry);
585                         goto error;
586                 }
587         }
588
589         if (ifname == NULL)
590                 goto error; /* Required Ifname argument missing */
591
592         /*
593          * Try to get the wpa_supplicant record for this iface, return
594          * an error if we already control it.
595          */
596         if (wpa_supplicant_get_iface(global, ifname) != NULL) {
597                 reply = dbus_message_new_error(message,
598                                                WPAS_DBUS_ERROR_IFACE_EXISTS,
599                                                "wpa_supplicant already "
600                                                "controls this interface.");
601         } else {
602                 struct wpa_supplicant *wpa_s;
603                 struct wpa_interface iface;
604                 os_memset(&iface, 0, sizeof(iface));
605                 iface.driver = driver;
606                 iface.ifname = ifname;
607                 iface.bridge_ifname = bridge_ifname;
608                 /* Otherwise, have wpa_supplicant attach to it. */
609                 if ((wpa_s = wpa_supplicant_add_iface(global, &iface))) {
610                         const char *path = wpa_s->dbus_new_path;
611                         reply = dbus_message_new_method_return(message);
612                         dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH,
613                                                  &path, DBUS_TYPE_INVALID);
614                 } else {
615                         reply = wpas_dbus_error_unknown_error(
616                                 message, "wpa_supplicant couldn't grab this "
617                                 "interface.");
618                 }
619         }
620
621 out:
622         os_free(driver);
623         os_free(ifname);
624         os_free(bridge_ifname);
625         return reply;
626
627 error:
628         reply = wpas_dbus_error_invalid_args(message, NULL);
629         goto out;
630 }
631
632
633 /**
634  * wpas_dbus_handler_remove_interface - Request deregistration of an interface
635  * @message: Pointer to incoming dbus message
636  * @global: wpa_supplicant global data structure
637  * Returns: a dbus message containing a UINT32 indicating success (1) or
638  *          failure (0), or returns a dbus error message with more information
639  *
640  * Handler function for "removeInterface" method call.  Handles requests
641  * by dbus clients to deregister a network interface that wpa_supplicant
642  * currently manages.
643  */
644 DBusMessage * wpas_dbus_handler_remove_interface(DBusMessage *message,
645                                                  struct wpa_global *global)
646 {
647         struct wpa_supplicant *wpa_s;
648         char *path;
649         DBusMessage *reply = NULL;
650
651         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &path,
652                               DBUS_TYPE_INVALID);
653
654         wpa_s = get_iface_by_dbus_path(global, path);
655         if (wpa_s == NULL)
656                 reply = wpas_dbus_error_iface_unknown(message);
657         else if (wpa_supplicant_remove_iface(global, wpa_s)) {
658                 reply = wpas_dbus_error_unknown_error(
659                         message, "wpa_supplicant couldn't remove this "
660                         "interface.");
661         }
662
663         return reply;
664 }
665
666
667 /**
668  * wpas_dbus_handler_get_interface - Get the object path for an interface name
669  * @message: Pointer to incoming dbus message
670  * @global: %wpa_supplicant global data structure
671  * Returns: The object path of the interface object,
672  *          or a dbus error message with more information
673  *
674  * Handler function for "getInterface" method call.
675  */
676 DBusMessage * wpas_dbus_handler_get_interface(DBusMessage *message,
677                                               struct wpa_global *global)
678 {
679         DBusMessage *reply = NULL;
680         const char *ifname;
681         const char *path;
682         struct wpa_supplicant *wpa_s;
683
684         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &ifname,
685                               DBUS_TYPE_INVALID);
686
687         wpa_s = wpa_supplicant_get_iface(global, ifname);
688         if (wpa_s == NULL)
689                 return wpas_dbus_error_iface_unknown(message);
690
691         path = wpa_s->dbus_new_path;
692         reply = dbus_message_new_method_return(message);
693         if (reply == NULL)
694                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
695                                               NULL);
696         if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
697                                       DBUS_TYPE_INVALID)) {
698                 dbus_message_unref(reply);
699                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
700                                               NULL);
701         }
702
703         return reply;
704 }
705
706
707 /**
708  * wpas_dbus_getter_debug_level - Get debug level
709  * @message: Pointer to incoming dbus message
710  * @global: %wpa_supplicant global data structure
711  * Returns: DBus message with value of debug level
712  *
713  * Getter for "DebugLevel" property.
714  */
715 DBusMessage * wpas_dbus_getter_debug_level(DBusMessage *message,
716                                            struct wpa_global *global)
717 {
718         const char *str;
719         int idx = wpa_debug_level;
720         if (idx < 0)
721                 idx = 0;
722         if (idx > 4)
723                 idx = 4;
724         str = debug_strings[idx];
725         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
726                                                 &str);
727 }
728
729
730 /**
731  * wpas_dbus_getter_debug_timestamp - Get debug timestamp
732  * @message: Pointer to incoming dbus message
733  * @global: %wpa_supplicant global data structure
734  * Returns: DBus message with value of debug timestamp
735  *
736  * Getter for "DebugTimestamp" property.
737  */
738 DBusMessage * wpas_dbus_getter_debug_timestamp(DBusMessage *message,
739                                                struct wpa_global *global)
740 {
741         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
742                                                 &wpa_debug_timestamp);
743
744 }
745
746
747 /**
748  * wpas_dbus_getter_debug_show_keys - Get debug show keys
749  * @message: Pointer to incoming dbus message
750  * @global: %wpa_supplicant global data structure
751  * Returns: DBus message with value of debug show_keys
752  *
753  * Getter for "DebugShowKeys" property.
754  */
755 DBusMessage * wpas_dbus_getter_debug_show_keys(DBusMessage *message,
756                                                struct wpa_global *global)
757 {
758         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
759                                                 &wpa_debug_show_keys);
760
761 }
762
763 /**
764  * wpas_dbus_setter_debug_level - Set debug level
765  * @message: Pointer to incoming dbus message
766  * @global: %wpa_supplicant global data structure
767  * Returns: %NULL or DBus error message
768  *
769  * Setter for "DebugLevel" property.
770  */
771 DBusMessage * wpas_dbus_setter_debug_level(DBusMessage *message,
772                                            struct wpa_global *global)
773 {
774         DBusMessage *reply;
775         const char *str = NULL;
776         int i, val = -1;
777
778         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_STRING,
779                                                  &str);
780         if (reply)
781                 return reply;
782
783         for (i = 0; debug_strings[i]; i++)
784                 if (os_strcmp(debug_strings[i], str) == 0) {
785                         val = i;
786                         break;
787                 }
788
789         if (val < 0 ||
790             wpa_supplicant_set_debug_params(global, val, wpa_debug_timestamp,
791                                             wpa_debug_show_keys)) {
792                 dbus_message_unref(reply);
793                 return wpas_dbus_error_invalid_args(
794                         message, "Wrong debug level value");
795         }
796
797         return NULL;
798 }
799
800
801 /**
802  * wpas_dbus_setter_debug_timestamp - Set debug timestamp
803  * @message: Pointer to incoming dbus message
804  * @global: %wpa_supplicant global data structure
805  * Returns: %NULL or DBus error message
806  *
807  * Setter for "DebugTimestamp" property.
808  */
809 DBusMessage * wpas_dbus_setter_debug_timestamp(DBusMessage *message,
810                                                struct wpa_global *global)
811 {
812         DBusMessage *reply;
813         dbus_bool_t val;
814
815         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
816                                                  &val);
817         if (reply)
818                 return reply;
819
820         wpa_supplicant_set_debug_params(global, wpa_debug_level, val ? 1 : 0,
821                                         wpa_debug_show_keys);
822
823         return NULL;
824 }
825
826
827 /**
828  * wpas_dbus_setter_debug_show_keys - Set debug show keys
829  * @message: Pointer to incoming dbus message
830  * @global: %wpa_supplicant global data structure
831  * Returns: %NULL or DBus error message
832  *
833  * Setter for "DebugShowKeys" property.
834  */
835 DBusMessage * wpas_dbus_setter_debug_show_keys(DBusMessage *message,
836                                                struct wpa_global *global)
837 {
838         DBusMessage *reply;
839         dbus_bool_t val;
840
841         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
842                                                  &val);
843         if (reply)
844                 return reply;
845
846         wpa_supplicant_set_debug_params(global, wpa_debug_level,
847                                         wpa_debug_timestamp,
848                                         val ? 1 : 0);
849
850         return NULL;
851 }
852
853
854 /**
855  * wpas_dbus_getter_interfaces - Request registered interfaces list
856  * @message: Pointer to incoming dbus message
857  * @global: %wpa_supplicant global data structure
858  * Returns: The object paths array containing registered interfaces
859  * objects paths or DBus error on failure
860  *
861  * Getter for "Interfaces" property. Handles requests
862  * by dbus clients to return list of registered interfaces objects
863  * paths
864  */
865 DBusMessage * wpas_dbus_getter_interfaces(DBusMessage *message,
866                                           struct wpa_global *global)
867 {
868         DBusMessage *reply = NULL;
869         struct wpa_supplicant *wpa_s;
870         const char **paths;
871         unsigned int i = 0, num = 0;
872
873         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
874                 num++;
875
876         paths = os_zalloc(num * sizeof(char*));
877         if (!paths) {
878                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
879                                               NULL);
880         }
881
882         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next)
883                 paths[i] = wpa_s->dbus_new_path;
884
885         reply = wpas_dbus_simple_array_property_getter(message,
886                                                        DBUS_TYPE_OBJECT_PATH,
887                                                        paths, num);
888
889         os_free(paths);
890         return reply;
891 }
892
893
894 /**
895  * wpas_dbus_getter_eap_methods - Request supported EAP methods list
896  * @message: Pointer to incoming dbus message
897  * @nothing: not used argument. may be NULL or anything else
898  * Returns: The object paths array containing supported EAP methods
899  * represented by strings or DBus error on failure
900  *
901  * Getter for "EapMethods" property. Handles requests
902  * by dbus clients to return list of strings with supported EAP methods
903  */
904 DBusMessage * wpas_dbus_getter_eap_methods(DBusMessage *message, void *nothing)
905 {
906         DBusMessage *reply = NULL;
907         char **eap_methods;
908         size_t num_items = 0;
909
910         eap_methods = eap_get_names_as_string_array(&num_items);
911         if (!eap_methods) {
912                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
913                                               NULL);
914         }
915
916         reply = wpas_dbus_simple_array_property_getter(message,
917                                                        DBUS_TYPE_STRING,
918                                                        eap_methods, num_items);
919
920         while (num_items)
921                 os_free(eap_methods[--num_items]);
922         os_free(eap_methods);
923         return reply;
924 }
925
926
927 static int wpas_dbus_get_scan_type(DBusMessage *message, DBusMessageIter *var,
928                                    char **type, DBusMessage **reply)
929 {
930         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_STRING) {
931                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
932                            "Type must be a string");
933                 *reply = wpas_dbus_error_invalid_args(
934                         message, "Wrong Type value type. String required");
935                 return -1;
936         }
937         dbus_message_iter_get_basic(var, type);
938         return 0;
939 }
940
941
942 static int wpas_dbus_get_scan_ssids(DBusMessage *message, DBusMessageIter *var,
943                                     struct wpa_driver_scan_params *params,
944                                     DBusMessage **reply)
945 {
946         struct wpa_driver_scan_ssid *ssids = params->ssids;
947         size_t ssids_num = 0;
948         u8 *ssid;
949         DBusMessageIter array_iter, sub_array_iter;
950         char *val;
951         int len;
952
953         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
954                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
955                            "must be an array of arrays of bytes");
956                 *reply = wpas_dbus_error_invalid_args(
957                         message, "Wrong SSIDs value type. Array of arrays of "
958                         "bytes required");
959                 return -1;
960         }
961
962         dbus_message_iter_recurse(var, &array_iter);
963
964         if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
965             dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
966         {
967                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ssids "
968                            "must be an array of arrays of bytes");
969                 *reply = wpas_dbus_error_invalid_args(
970                         message, "Wrong SSIDs value type. Array of arrays of "
971                         "bytes required");
972                 return -1;
973         }
974
975         while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
976         {
977                 if (ssids_num >= WPAS_MAX_SCAN_SSIDS) {
978                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
979                                    "Too many ssids specified on scan dbus "
980                                    "call");
981                         *reply = wpas_dbus_error_invalid_args(
982                                 message, "Too many ssids specified. Specify "
983                                 "at most four");
984                         return -1;
985                 }
986
987                 dbus_message_iter_recurse(&array_iter, &sub_array_iter);
988
989                 dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
990                 if (len == 0) {
991                         dbus_message_iter_next(&array_iter);
992                         continue;
993                 }
994
995                 ssid = os_malloc(len);
996                 if (ssid == NULL) {
997                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
998                                    "out of memory. Cannot allocate memory for "
999                                    "SSID");
1000                         *reply = dbus_message_new_error(
1001                                 message, DBUS_ERROR_NO_MEMORY, NULL);
1002                         return -1;
1003                 }
1004                 os_memcpy(ssid, val, len);
1005                 ssids[ssids_num].ssid = ssid;
1006                 ssids[ssids_num].ssid_len = len;
1007
1008                 dbus_message_iter_next(&array_iter);
1009                 ssids_num++;
1010         }
1011
1012         params->num_ssids = ssids_num;
1013         return 0;
1014 }
1015
1016
1017 static int wpas_dbus_get_scan_ies(DBusMessage *message, DBusMessageIter *var,
1018                                   struct wpa_driver_scan_params *params,
1019                                   DBusMessage **reply)
1020 {
1021         u8 *ies = NULL, *nies;
1022         int ies_len = 0;
1023         DBusMessageIter array_iter, sub_array_iter;
1024         char *val;
1025         int len;
1026
1027         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
1028                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
1029                            "be an array of arrays of bytes");
1030                 *reply = wpas_dbus_error_invalid_args(
1031                         message, "Wrong IEs value type. Array of arrays of "
1032                         "bytes required");
1033                 return -1;
1034         }
1035
1036         dbus_message_iter_recurse(var, &array_iter);
1037
1038         if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_ARRAY ||
1039             dbus_message_iter_get_element_type(&array_iter) != DBUS_TYPE_BYTE)
1040         {
1041                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: ies must "
1042                            "be an array of arrays of bytes");
1043                 *reply = wpas_dbus_error_invalid_args(
1044                         message, "Wrong IEs value type. Array required");
1045                 return -1;
1046         }
1047
1048         while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_ARRAY)
1049         {
1050                 dbus_message_iter_recurse(&array_iter, &sub_array_iter);
1051
1052                 dbus_message_iter_get_fixed_array(&sub_array_iter, &val, &len);
1053                 if (len == 0) {
1054                         dbus_message_iter_next(&array_iter);
1055                         continue;
1056                 }
1057
1058                 nies = os_realloc(ies, ies_len + len);
1059                 if (nies == NULL) {
1060                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1061                                    "out of memory. Cannot allocate memory for "
1062                                    "IE");
1063                         os_free(ies);
1064                         *reply = dbus_message_new_error(
1065                                 message, DBUS_ERROR_NO_MEMORY, NULL);
1066                         return -1;
1067                 }
1068                 ies = nies;
1069                 os_memcpy(ies + ies_len, val, len);
1070                 ies_len += len;
1071
1072                 dbus_message_iter_next(&array_iter);
1073         }
1074
1075         params->extra_ies = ies;
1076         params->extra_ies_len = ies_len;
1077         return 0;
1078 }
1079
1080
1081 static int wpas_dbus_get_scan_channels(DBusMessage *message,
1082                                        DBusMessageIter *var,
1083                                        struct wpa_driver_scan_params *params,
1084                                        DBusMessage **reply)
1085 {
1086         DBusMessageIter array_iter, sub_array_iter;
1087         int *freqs = NULL, *nfreqs;
1088         int freqs_num = 0;
1089
1090         if (dbus_message_iter_get_arg_type(var) != DBUS_TYPE_ARRAY) {
1091                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1092                            "Channels must be an array of structs");
1093                 *reply = wpas_dbus_error_invalid_args(
1094                         message, "Wrong Channels value type. Array of structs "
1095                         "required");
1096                 return -1;
1097         }
1098
1099         dbus_message_iter_recurse(var, &array_iter);
1100
1101         if (dbus_message_iter_get_arg_type(&array_iter) != DBUS_TYPE_STRUCT) {
1102                 wpa_printf(MSG_DEBUG,
1103                            "wpas_dbus_handler_scan[dbus]: Channels must be an "
1104                            "array of structs");
1105                 *reply = wpas_dbus_error_invalid_args(
1106                         message, "Wrong Channels value type. Array of structs "
1107                         "required");
1108                 return -1;
1109         }
1110
1111         while (dbus_message_iter_get_arg_type(&array_iter) == DBUS_TYPE_STRUCT)
1112         {
1113                 int freq, width;
1114
1115                 dbus_message_iter_recurse(&array_iter, &sub_array_iter);
1116
1117                 if (dbus_message_iter_get_arg_type(&sub_array_iter) !=
1118                     DBUS_TYPE_UINT32) {
1119                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1120                                    "Channel must by specified by struct of "
1121                                    "two UINT32s %c",
1122                                    dbus_message_iter_get_arg_type(
1123                                            &sub_array_iter));
1124                         *reply = wpas_dbus_error_invalid_args(
1125                                 message, "Wrong Channel struct. Two UINT32s "
1126                                 "required");
1127                         os_free(freqs);
1128                         return -1;
1129                 }
1130                 dbus_message_iter_get_basic(&sub_array_iter, &freq);
1131
1132                 if (!dbus_message_iter_next(&sub_array_iter) ||
1133                     dbus_message_iter_get_arg_type(&sub_array_iter) !=
1134                     DBUS_TYPE_UINT32) {
1135                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1136                                    "Channel must by specified by struct of "
1137                                    "two UINT32s");
1138                         *reply = wpas_dbus_error_invalid_args(
1139                                 message,
1140                                 "Wrong Channel struct. Two UINT32s required");
1141                         os_free(freqs);
1142                         return -1;
1143                 }
1144
1145                 dbus_message_iter_get_basic(&sub_array_iter, &width);
1146
1147 #define FREQS_ALLOC_CHUNK 32
1148                 if (freqs_num % FREQS_ALLOC_CHUNK == 0) {
1149                         nfreqs = os_realloc(freqs, sizeof(int) *
1150                                             (freqs_num + FREQS_ALLOC_CHUNK));
1151                         if (nfreqs == NULL)
1152                                 os_free(freqs);
1153                         freqs = nfreqs;
1154                 }
1155                 if (freqs == NULL) {
1156                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1157                                    "out of memory. can't allocate memory for "
1158                                    "freqs");
1159                         *reply = dbus_message_new_error(
1160                                 message, DBUS_ERROR_NO_MEMORY, NULL);
1161                         return -1;
1162                 }
1163
1164                 freqs[freqs_num] = freq;
1165
1166                 freqs_num++;
1167                 dbus_message_iter_next(&array_iter);
1168         }
1169
1170         nfreqs = os_realloc(freqs,
1171                             sizeof(int) * (freqs_num + 1));
1172         if (nfreqs == NULL)
1173                 os_free(freqs);
1174         freqs = nfreqs;
1175         if (freqs == NULL) {
1176                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1177                            "out of memory. Can't allocate memory for freqs");
1178                 *reply = dbus_message_new_error(
1179                         message, DBUS_ERROR_NO_MEMORY, NULL);
1180                 return -1;
1181         }
1182         freqs[freqs_num] = 0;
1183
1184         params->freqs = freqs;
1185         return 0;
1186 }
1187
1188
1189 /**
1190  * wpas_dbus_handler_scan - Request a wireless scan on an interface
1191  * @message: Pointer to incoming dbus message
1192  * @wpa_s: wpa_supplicant structure for a network interface
1193  * Returns: NULL indicating success or DBus error message on failure
1194  *
1195  * Handler function for "Scan" method call of a network device. Requests
1196  * that wpa_supplicant perform a wireless scan as soon as possible
1197  * on a particular wireless interface.
1198  */
1199 DBusMessage * wpas_dbus_handler_scan(DBusMessage *message,
1200                                      struct wpa_supplicant *wpa_s)
1201 {
1202         DBusMessage *reply = NULL;
1203         DBusMessageIter iter, dict_iter, entry_iter, variant_iter;
1204         char *key = NULL, *type = NULL;
1205         struct wpa_driver_scan_params params;
1206         size_t i;
1207
1208         os_memset(&params, 0, sizeof(params));
1209
1210         dbus_message_iter_init(message, &iter);
1211
1212         dbus_message_iter_recurse(&iter, &dict_iter);
1213
1214         while (dbus_message_iter_get_arg_type(&dict_iter) ==
1215                         DBUS_TYPE_DICT_ENTRY) {
1216                 dbus_message_iter_recurse(&dict_iter, &entry_iter);
1217                 dbus_message_iter_get_basic(&entry_iter, &key);
1218                 dbus_message_iter_next(&entry_iter);
1219                 dbus_message_iter_recurse(&entry_iter, &variant_iter);
1220
1221                 if (os_strcmp(key, "Type") == 0) {
1222                         if (wpas_dbus_get_scan_type(message, &variant_iter,
1223                                                     &type, &reply) < 0)
1224                                 goto out;
1225                 } else if (os_strcmp(key, "SSIDs") == 0) {
1226                         if (wpas_dbus_get_scan_ssids(message, &variant_iter,
1227                                                      &params, &reply) < 0)
1228                                 goto out;
1229                 } else if (os_strcmp(key, "IEs") == 0) {
1230                         if (wpas_dbus_get_scan_ies(message, &variant_iter,
1231                                                    &params, &reply) < 0)
1232                                 goto out;
1233                 } else if (os_strcmp(key, "Channels") == 0) {
1234                         if (wpas_dbus_get_scan_channels(message, &variant_iter,
1235                                                         &params, &reply) < 0)
1236                                 goto out;
1237                 } else {
1238                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1239                                    "Unknown argument %s", key);
1240                         reply = wpas_dbus_error_invalid_args(message, key);
1241                         goto out;
1242                 }
1243
1244                 dbus_message_iter_next(&dict_iter);
1245         }
1246
1247         if (!type) {
1248                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1249                            "Scan type not specified");
1250                 reply = wpas_dbus_error_invalid_args(message, key);
1251                 goto out;
1252         }
1253
1254         if (!os_strcmp(type, "passive")) {
1255                 if (params.num_ssids || params.extra_ies_len) {
1256                         wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1257                                    "SSIDs or IEs specified for passive scan.");
1258                         reply = wpas_dbus_error_invalid_args(
1259                                 message, "You can specify only Channels in "
1260                                 "passive scan");
1261                         goto out;
1262                 } else if (params.freqs && params.freqs[0]) {
1263                         /* wildcard ssid */
1264                         params.num_ssids++;
1265                         wpa_supplicant_trigger_scan(wpa_s, &params);
1266                 } else {
1267                         wpa_s->scan_req = 2;
1268                         wpa_supplicant_req_scan(wpa_s, 0, 0);
1269                 }
1270         } else if (!os_strcmp(type, "active")) {
1271                 wpa_supplicant_trigger_scan(wpa_s, &params);
1272         } else {
1273                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_scan[dbus]: "
1274                            "Unknown scan type: %s", type);
1275                 reply = wpas_dbus_error_invalid_args(message,
1276                                                      "Wrong scan type");
1277                 goto out;
1278         }
1279
1280 out:
1281         for (i = 0; i < WPAS_MAX_SCAN_SSIDS; i++)
1282                 os_free((u8 *) params.ssids[i].ssid);
1283         os_free((u8 *) params.extra_ies);
1284         os_free(params.freqs);
1285         return reply;
1286 }
1287
1288
1289 /*
1290  * wpas_dbus_handler_disconnect - Terminate the current connection
1291  * @message: Pointer to incoming dbus message
1292  * @wpa_s: wpa_supplicant structure for a network interface
1293  * Returns: NotConnected DBus error message if already not connected
1294  * or NULL otherwise.
1295  *
1296  * Handler function for "Disconnect" method call of network interface.
1297  */
1298 DBusMessage * wpas_dbus_handler_disconnect(DBusMessage *message,
1299                                            struct wpa_supplicant *wpa_s)
1300 {
1301         if (wpa_s->current_ssid != NULL) {
1302                 wpa_s->disconnected = 1;
1303                 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1304
1305                 return NULL;
1306         }
1307
1308         return dbus_message_new_error(message, WPAS_DBUS_ERROR_NOT_CONNECTED,
1309                                       "This interface is not connected");
1310 }
1311
1312
1313 /**
1314  * wpas_dbus_new_iface_add_network - Add a new configured network
1315  * @message: Pointer to incoming dbus message
1316  * @wpa_s: wpa_supplicant structure for a network interface
1317  * Returns: A dbus message containing the object path of the new network
1318  *
1319  * Handler function for "AddNetwork" method call of a network interface.
1320  */
1321 DBusMessage * wpas_dbus_handler_add_network(DBusMessage *message,
1322                                             struct wpa_supplicant *wpa_s)
1323 {
1324         DBusMessage *reply = NULL;
1325         DBusMessageIter iter;
1326         struct wpa_ssid *ssid = NULL;
1327         char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *path = path_buf;
1328
1329         dbus_message_iter_init(message, &iter);
1330
1331         ssid = wpa_config_add_network(wpa_s->conf);
1332         if (ssid == NULL) {
1333                 wpa_printf(MSG_ERROR, "wpas_dbus_handler_add_network[dbus]: "
1334                            "can't add new interface.");
1335                 reply = wpas_dbus_error_unknown_error(
1336                         message,
1337                         "wpa_supplicant could not add "
1338                         "a network on this interface.");
1339                 goto err;
1340         }
1341         wpas_notify_network_added(wpa_s, ssid);
1342         ssid->disabled = 1;
1343         wpa_config_set_network_defaults(ssid);
1344
1345         reply = set_network_properties(message, wpa_s, ssid, &iter);
1346         if (reply) {
1347                 wpa_printf(MSG_DEBUG, "wpas_dbus_handler_add_network[dbus]:"
1348                            "control interface couldn't set network "
1349                            "properties");
1350                 goto err;
1351         }
1352
1353         /* Construct the object path for this network. */
1354         os_snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
1355                     "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
1356                     wpa_s->dbus_new_path, ssid->id);
1357
1358         reply = dbus_message_new_method_return(message);
1359         if (reply == NULL) {
1360                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1361                                                NULL);
1362                 goto err;
1363         }
1364         if (!dbus_message_append_args(reply, DBUS_TYPE_OBJECT_PATH, &path,
1365                                       DBUS_TYPE_INVALID)) {
1366                 dbus_message_unref(reply);
1367                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1368                                                NULL);
1369                 goto err;
1370         }
1371
1372         return reply;
1373
1374 err:
1375         if (ssid) {
1376                 wpas_notify_network_removed(wpa_s, ssid);
1377                 wpa_config_remove_network(wpa_s->conf, ssid->id);
1378         }
1379         return reply;
1380 }
1381
1382
1383 /**
1384  * wpas_dbus_handler_remove_network - Remove a configured network
1385  * @message: Pointer to incoming dbus message
1386  * @wpa_s: wpa_supplicant structure for a network interface
1387  * Returns: NULL on success or dbus error on failure
1388  *
1389  * Handler function for "RemoveNetwork" method call of a network interface.
1390  */
1391 DBusMessage * wpas_dbus_handler_remove_network(DBusMessage *message,
1392                                                struct wpa_supplicant *wpa_s)
1393 {
1394         DBusMessage *reply = NULL;
1395         const char *op;
1396         char *iface = NULL, *net_id = NULL;
1397         int id;
1398         struct wpa_ssid *ssid;
1399
1400         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
1401                               DBUS_TYPE_INVALID);
1402
1403         /* Extract the network ID and ensure the network */
1404         /* is actually a child of this interface */
1405         iface = wpas_dbus_new_decompose_object_path(op, &net_id, NULL);
1406         if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
1407                 reply = wpas_dbus_error_invalid_args(message, op);
1408                 goto out;
1409         }
1410
1411         id = strtoul(net_id, NULL, 10);
1412         if (errno == EINVAL) {
1413                 reply = wpas_dbus_error_invalid_args(message, op);
1414                 goto out;
1415         }
1416
1417         ssid = wpa_config_get_network(wpa_s->conf, id);
1418         if (ssid == NULL) {
1419                 reply = wpas_dbus_error_network_unknown(message);
1420                 goto out;
1421         }
1422
1423         wpas_notify_network_removed(wpa_s, ssid);
1424
1425         if (wpa_config_remove_network(wpa_s->conf, id) < 0) {
1426                 wpa_printf(MSG_ERROR,
1427                            "wpas_dbus_handler_remove_network[dbus]: "
1428                            "error occurred when removing network %d", id);
1429                 reply = wpas_dbus_error_unknown_error(
1430                         message, "error removing the specified network on "
1431                         "this interface.");
1432                 goto out;
1433         }
1434
1435         if (ssid == wpa_s->current_ssid)
1436                 wpa_supplicant_disassociate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
1437
1438 out:
1439         os_free(iface);
1440         os_free(net_id);
1441         return reply;
1442 }
1443
1444
1445 /**
1446  * wpas_dbus_handler_select_network - Attempt association with a network
1447  * @message: Pointer to incoming dbus message
1448  * @wpa_s: wpa_supplicant structure for a network interface
1449  * Returns: NULL on success or dbus error on failure
1450  *
1451  * Handler function for "SelectNetwork" method call of network interface.
1452  */
1453 DBusMessage * wpas_dbus_handler_select_network(DBusMessage *message,
1454                                                struct wpa_supplicant *wpa_s)
1455 {
1456         DBusMessage *reply = NULL;
1457         const char *op;
1458         char *iface = NULL, *net_id = NULL;
1459         int id;
1460         struct wpa_ssid *ssid;
1461
1462         dbus_message_get_args(message, NULL, DBUS_TYPE_OBJECT_PATH, &op,
1463                               DBUS_TYPE_INVALID);
1464
1465         /* Extract the network ID and ensure the network */
1466         /* is actually a child of this interface */
1467         iface = wpas_dbus_new_decompose_object_path(op, &net_id, NULL);
1468         if (iface == NULL || os_strcmp(iface, wpa_s->dbus_new_path) != 0) {
1469                 reply = wpas_dbus_error_invalid_args(message, op);
1470                 goto out;
1471         }
1472
1473         id = strtoul(net_id, NULL, 10);
1474         if (errno == EINVAL) {
1475                 reply = wpas_dbus_error_invalid_args(message, op);
1476                 goto out;
1477         }
1478
1479         ssid = wpa_config_get_network(wpa_s->conf, id);
1480         if (ssid == NULL) {
1481                 reply = wpas_dbus_error_network_unknown(message);
1482                 goto out;
1483         }
1484
1485         /* Finally, associate with the network */
1486         wpa_supplicant_select_network(wpa_s, ssid);
1487
1488 out:
1489         os_free(iface);
1490         os_free(net_id);
1491         return reply;
1492 }
1493
1494
1495 /**
1496  * wpas_dbus_handler_add_blob - Store named binary blob (ie, for certificates)
1497  * @message: Pointer to incoming dbus message
1498  * @wpa_s: %wpa_supplicant data structure
1499  * Returns: A dbus message containing an error on failure or NULL on success
1500  *
1501  * Asks wpa_supplicant to internally store a binary blobs.
1502  */
1503 DBusMessage * wpas_dbus_handler_add_blob(DBusMessage *message,
1504                                          struct wpa_supplicant *wpa_s)
1505 {
1506         DBusMessage *reply = NULL;
1507         DBusMessageIter iter, array_iter;
1508
1509         char *blob_name;
1510         u8 *blob_data;
1511         int blob_len;
1512         struct wpa_config_blob *blob = NULL;
1513
1514         dbus_message_iter_init(message, &iter);
1515         dbus_message_iter_get_basic(&iter, &blob_name);
1516
1517         if (wpa_config_get_blob(wpa_s->conf, blob_name)) {
1518                 return dbus_message_new_error(message,
1519                                               WPAS_DBUS_ERROR_BLOB_EXISTS,
1520                                               NULL);
1521         }
1522
1523         dbus_message_iter_next(&iter);
1524         dbus_message_iter_recurse(&iter, &array_iter);
1525
1526         dbus_message_iter_get_fixed_array(&array_iter, &blob_data, &blob_len);
1527
1528         blob = os_zalloc(sizeof(*blob));
1529         if (!blob) {
1530                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1531                                                NULL);
1532                 goto err;
1533         }
1534
1535         blob->data = os_malloc(blob_len);
1536         if (!blob->data) {
1537                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1538                                                NULL);
1539                 goto err;
1540         }
1541         os_memcpy(blob->data, blob_data, blob_len);
1542
1543         blob->len = blob_len;
1544         blob->name = os_strdup(blob_name);
1545         if (!blob->name) {
1546                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1547                                                NULL);
1548                 goto err;
1549         }
1550
1551         wpa_config_set_blob(wpa_s->conf, blob);
1552         wpas_notify_blob_added(wpa_s, blob->name);
1553
1554         return reply;
1555
1556 err:
1557         if (blob) {
1558                 os_free(blob->name);
1559                 os_free(blob->data);
1560                 os_free(blob);
1561         }
1562         return reply;
1563 }
1564
1565
1566 /**
1567  * wpas_dbus_handler_get_blob - Get named binary blob (ie, for certificates)
1568  * @message: Pointer to incoming dbus message
1569  * @wpa_s: %wpa_supplicant data structure
1570  * Returns: A dbus message containing array of bytes (blob)
1571  *
1572  * Gets one wpa_supplicant's binary blobs.
1573  */
1574 DBusMessage * wpas_dbus_handler_get_blob(DBusMessage *message,
1575                                          struct wpa_supplicant *wpa_s)
1576 {
1577         DBusMessage *reply = NULL;
1578         DBusMessageIter iter, array_iter;
1579
1580         char *blob_name;
1581         const struct wpa_config_blob *blob;
1582
1583         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
1584                               DBUS_TYPE_INVALID);
1585
1586         blob = wpa_config_get_blob(wpa_s->conf, blob_name);
1587         if (!blob) {
1588                 return dbus_message_new_error(message,
1589                                               WPAS_DBUS_ERROR_BLOB_UNKNOWN,
1590                                               "Blob id not set");
1591         }
1592
1593         reply = dbus_message_new_method_return(message);
1594         if (!reply) {
1595                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1596                                                NULL);
1597                 goto out;
1598         }
1599
1600         dbus_message_iter_init_append(reply, &iter);
1601
1602         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY,
1603                                               DBUS_TYPE_BYTE_AS_STRING,
1604                                               &array_iter)) {
1605                 dbus_message_unref(reply);
1606                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1607                                                NULL);
1608                 goto out;
1609         }
1610
1611         if (!dbus_message_iter_append_fixed_array(&array_iter, DBUS_TYPE_BYTE,
1612                                                   &(blob->data), blob->len)) {
1613                 dbus_message_unref(reply);
1614                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1615                                                NULL);
1616                 goto out;
1617         }
1618
1619         if (!dbus_message_iter_close_container(&iter, &array_iter)) {
1620                 dbus_message_unref(reply);
1621                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1622                                                NULL);
1623                 goto out;
1624         }
1625
1626 out:
1627         return reply;
1628 }
1629
1630
1631 /**
1632  * wpas_remove_handler_remove_blob - Remove named binary blob
1633  * @message: Pointer to incoming dbus message
1634  * @wpa_s: %wpa_supplicant data structure
1635  * Returns: NULL on success or dbus error
1636  *
1637  * Asks wpa_supplicant to internally remove a binary blobs.
1638  */
1639 DBusMessage * wpas_dbus_handler_remove_blob(DBusMessage *message,
1640                                             struct wpa_supplicant *wpa_s)
1641 {
1642         DBusMessage *reply = NULL;
1643         char *blob_name;
1644
1645         dbus_message_get_args(message, NULL, DBUS_TYPE_STRING, &blob_name,
1646                               DBUS_TYPE_INVALID);
1647
1648         if (wpa_config_remove_blob(wpa_s->conf, blob_name)) {
1649                 return dbus_message_new_error(message,
1650                                               WPAS_DBUS_ERROR_BLOB_UNKNOWN,
1651                                               "Blob id not set");
1652         }
1653         wpas_notify_blob_removed(wpa_s, blob_name);
1654
1655         return reply;
1656
1657 }
1658
1659
1660 /**
1661  * wpas_dbus_getter_capabilities - Return interface capabilities
1662  * @message: Pointer to incoming dbus message
1663  * @wpa_s: wpa_supplicant structure for a network interface
1664  * Returns: A dbus message containing a dict of strings
1665  *
1666  * Getter for "Capabilities" property of an interface.
1667  */
1668 DBusMessage * wpas_dbus_getter_capabilities(DBusMessage *message,
1669                                             struct wpa_supplicant *wpa_s)
1670 {
1671         DBusMessage *reply = NULL;
1672         struct wpa_driver_capa capa;
1673         int res;
1674         DBusMessageIter iter, iter_dict;
1675         DBusMessageIter iter_dict_entry, iter_dict_val, iter_array,
1676                 variant_iter;
1677         const char *scans[] = { "active", "passive", "ssid" };
1678         const char *modes[] = { "infrastructure", "ad-hoc", "ap" };
1679         int n = sizeof(modes) / sizeof(char *);
1680
1681         if (message == NULL)
1682                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
1683         else
1684                 reply = dbus_message_new_method_return(message);
1685         if (!reply)
1686                 goto nomem;
1687
1688         dbus_message_iter_init_append(reply, &iter);
1689         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
1690                                               "a{sv}", &variant_iter))
1691                 goto nomem;
1692
1693         if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
1694                 goto nomem;
1695
1696         res = wpa_drv_get_capa(wpa_s, &capa);
1697
1698         /***** pairwise cipher */
1699         if (res < 0) {
1700                 const char *args[] = {"ccmp", "tkip", "none"};
1701                 if (!wpa_dbus_dict_append_string_array(
1702                             &iter_dict, "Pairwise", args,
1703                             sizeof(args) / sizeof(char*)))
1704                         goto nomem;
1705         } else {
1706                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Pairwise",
1707                                                       &iter_dict_entry,
1708                                                       &iter_dict_val,
1709                                                       &iter_array))
1710                         goto nomem;
1711
1712                 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1713                         if (!wpa_dbus_dict_string_array_add_element(
1714                                     &iter_array, "ccmp"))
1715                                 goto nomem;
1716                 }
1717
1718                 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1719                         if (!wpa_dbus_dict_string_array_add_element(
1720                                     &iter_array, "tkip"))
1721                                 goto nomem;
1722                 }
1723
1724                 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1725                         if (!wpa_dbus_dict_string_array_add_element(
1726                                     &iter_array, "none"))
1727                                 goto nomem;
1728                 }
1729
1730                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
1731                                                     &iter_dict_entry,
1732                                                     &iter_dict_val,
1733                                                     &iter_array))
1734                         goto nomem;
1735         }
1736
1737         /***** group cipher */
1738         if (res < 0) {
1739                 const char *args[] = {
1740                         "ccmp", "tkip", "wep104", "wep40"
1741                 };
1742                 if (!wpa_dbus_dict_append_string_array(
1743                             &iter_dict, "Group", args,
1744                             sizeof(args) / sizeof(char*)))
1745                         goto nomem;
1746         } else {
1747                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Group",
1748                                                       &iter_dict_entry,
1749                                                       &iter_dict_val,
1750                                                       &iter_array))
1751                         goto nomem;
1752
1753                 if (capa.enc & WPA_DRIVER_CAPA_ENC_CCMP) {
1754                         if (!wpa_dbus_dict_string_array_add_element(
1755                                     &iter_array, "ccmp"))
1756                                 goto nomem;
1757                 }
1758
1759                 if (capa.enc & WPA_DRIVER_CAPA_ENC_TKIP) {
1760                         if (!wpa_dbus_dict_string_array_add_element(
1761                                     &iter_array, "tkip"))
1762                                 goto nomem;
1763                 }
1764
1765                 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP104) {
1766                         if (!wpa_dbus_dict_string_array_add_element(
1767                                     &iter_array, "wep104"))
1768                                 goto nomem;
1769                 }
1770
1771                 if (capa.enc & WPA_DRIVER_CAPA_ENC_WEP40) {
1772                         if (!wpa_dbus_dict_string_array_add_element(
1773                                     &iter_array, "wep40"))
1774                                 goto nomem;
1775                 }
1776
1777                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
1778                                                     &iter_dict_entry,
1779                                                     &iter_dict_val,
1780                                                     &iter_array))
1781                         goto nomem;
1782         }
1783
1784         /***** key management */
1785         if (res < 0) {
1786                 const char *args[] = {
1787                         "wpa-psk", "wpa-eap", "ieee8021x", "wpa-none",
1788 #ifdef CONFIG_WPS
1789                         "wps",
1790 #endif /* CONFIG_WPS */
1791                         "none"
1792                 };
1793                 if (!wpa_dbus_dict_append_string_array(
1794                             &iter_dict, "KeyMgmt", args,
1795                             sizeof(args) / sizeof(char*)))
1796                         goto nomem;
1797         } else {
1798                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "KeyMgmt",
1799                                                       &iter_dict_entry,
1800                                                       &iter_dict_val,
1801                                                       &iter_array))
1802                         goto nomem;
1803
1804                 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
1805                                                             "none"))
1806                         goto nomem;
1807
1808                 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
1809                                                             "ieee8021x"))
1810                         goto nomem;
1811
1812                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1813                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA2)) {
1814                         if (!wpa_dbus_dict_string_array_add_element(
1815                                     &iter_array, "wpa-eap"))
1816                                 goto nomem;
1817                 }
1818
1819                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK |
1820                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1821                         if (!wpa_dbus_dict_string_array_add_element(
1822                                     &iter_array, "wpa-psk"))
1823                                 goto nomem;
1824                 }
1825
1826                 if (capa.key_mgmt & WPA_DRIVER_CAPA_KEY_MGMT_WPA_NONE) {
1827                         if (!wpa_dbus_dict_string_array_add_element(
1828                                     &iter_array, "wpa-none"))
1829                                 goto nomem;
1830                 }
1831
1832
1833 #ifdef CONFIG_WPS
1834                 if (!wpa_dbus_dict_string_array_add_element(&iter_array,
1835                                                             "wps"))
1836                         goto nomem;
1837 #endif /* CONFIG_WPS */
1838
1839                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
1840                                                     &iter_dict_entry,
1841                                                     &iter_dict_val,
1842                                                     &iter_array))
1843                         goto nomem;
1844         }
1845
1846         /***** WPA protocol */
1847         if (res < 0) {
1848                 const char *args[] = { "rsn", "wpa" };
1849                 if (!wpa_dbus_dict_append_string_array(
1850                             &iter_dict, "Protocol", args,
1851                             sizeof(args) / sizeof(char*)))
1852                         goto nomem;
1853         } else {
1854                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "Protocol",
1855                                                       &iter_dict_entry,
1856                                                       &iter_dict_val,
1857                                                       &iter_array))
1858                         goto nomem;
1859
1860                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA2 |
1861                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA2_PSK)) {
1862                         if (!wpa_dbus_dict_string_array_add_element(
1863                                     &iter_array, "rsn"))
1864                                 goto nomem;
1865                 }
1866
1867                 if (capa.key_mgmt & (WPA_DRIVER_CAPA_KEY_MGMT_WPA |
1868                                      WPA_DRIVER_CAPA_KEY_MGMT_WPA_PSK)) {
1869                         if (!wpa_dbus_dict_string_array_add_element(
1870                                     &iter_array, "wpa"))
1871                                 goto nomem;
1872                 }
1873
1874                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
1875                                                     &iter_dict_entry,
1876                                                     &iter_dict_val,
1877                                                     &iter_array))
1878                         goto nomem;
1879         }
1880
1881         /***** auth alg */
1882         if (res < 0) {
1883                 const char *args[] = { "open", "shared", "leap" };
1884                 if (!wpa_dbus_dict_append_string_array(
1885                             &iter_dict, "AuthAlg", args,
1886                             sizeof(args) / sizeof(char*)))
1887                         goto nomem;
1888         } else {
1889                 if (!wpa_dbus_dict_begin_string_array(&iter_dict, "AuthAlg",
1890                                                       &iter_dict_entry,
1891                                                       &iter_dict_val,
1892                                                       &iter_array))
1893                         goto nomem;
1894
1895                 if (capa.auth & (WPA_DRIVER_AUTH_OPEN)) {
1896                         if (!wpa_dbus_dict_string_array_add_element(
1897                                     &iter_array, "open"))
1898                                 goto nomem;
1899                 }
1900
1901                 if (capa.auth & (WPA_DRIVER_AUTH_SHARED)) {
1902                         if (!wpa_dbus_dict_string_array_add_element(
1903                                     &iter_array, "shared"))
1904                                 goto nomem;
1905                 }
1906
1907                 if (capa.auth & (WPA_DRIVER_AUTH_LEAP)) {
1908                         if (!wpa_dbus_dict_string_array_add_element(
1909                                     &iter_array, "leap"))
1910                                 goto nomem;
1911                 }
1912
1913                 if (!wpa_dbus_dict_end_string_array(&iter_dict,
1914                                                     &iter_dict_entry,
1915                                                     &iter_dict_val,
1916                                                     &iter_array))
1917                         goto nomem;
1918         }
1919
1920         /***** Scan */
1921         if (!wpa_dbus_dict_append_string_array(&iter_dict, "Scan", scans,
1922                                                sizeof(scans) / sizeof(char *)))
1923                 goto nomem;
1924
1925         /***** Modes */
1926         if (res < 0 || !(capa.flags & WPA_DRIVER_FLAGS_AP))
1927                 n--; /* exclude ap mode if it is not supported by the driver */
1928         if (!wpa_dbus_dict_append_string_array(&iter_dict, "Modes", modes, n))
1929                 goto nomem;
1930
1931         if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
1932                 goto nomem;
1933         if (!dbus_message_iter_close_container(&iter, &variant_iter))
1934                 goto nomem;
1935
1936         return reply;
1937
1938 nomem:
1939         if (reply)
1940                 dbus_message_unref(reply);
1941
1942         return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
1943 }
1944
1945
1946 /**
1947  * wpas_dbus_getter_state - Get interface state
1948  * @message: Pointer to incoming dbus message
1949  * @wpa_s: wpa_supplicant structure for a network interface
1950  * Returns: A dbus message containing a STRING representing the current
1951  *          interface state
1952  *
1953  * Getter for "State" property.
1954  */
1955 DBusMessage * wpas_dbus_getter_state(DBusMessage *message,
1956                                      struct wpa_supplicant *wpa_s)
1957 {
1958         DBusMessage *reply = NULL;
1959         const char *str_state;
1960         char *state_ls, *tmp;
1961
1962         str_state = wpa_supplicant_state_txt(wpa_s->wpa_state);
1963
1964         /* make state string lowercase to fit new DBus API convention
1965          */
1966         state_ls = tmp = os_strdup(str_state);
1967         if (!tmp) {
1968                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
1969                                               NULL);
1970         }
1971         while (*tmp) {
1972                 *tmp = tolower(*tmp);
1973                 tmp++;
1974         }
1975
1976         reply = wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
1977                                                  &state_ls);
1978
1979         os_free(state_ls);
1980
1981         return reply;
1982 }
1983
1984
1985 /**
1986  * wpas_dbus_new_iface_get_scanning - Get interface scanning state
1987  * @message: Pointer to incoming dbus message
1988  * @wpa_s: wpa_supplicant structure for a network interface
1989  * Returns: A dbus message containing whether the interface is scanning
1990  *
1991  * Getter for "scanning" property.
1992  */
1993 DBusMessage * wpas_dbus_getter_scanning(DBusMessage *message,
1994                                         struct wpa_supplicant *wpa_s)
1995 {
1996         dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
1997         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
1998                                                 &scanning);
1999 }
2000
2001
2002 /**
2003  * wpas_dbus_getter_ap_scan - Control roaming mode
2004  * @message: Pointer to incoming dbus message
2005  * @wpa_s: wpa_supplicant structure for a network interface
2006  * Returns: A message containong value of ap_scan variable
2007  *
2008  * Getter function for "ApScan" property.
2009  */
2010 DBusMessage * wpas_dbus_getter_ap_scan(DBusMessage *message,
2011                                        struct wpa_supplicant *wpa_s)
2012 {
2013         dbus_uint32_t ap_scan = wpa_s->conf->ap_scan;
2014         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT32,
2015                                                 &ap_scan);
2016 }
2017
2018
2019 /**
2020  * wpas_dbus_setter_ap_scan - Control roaming mode
2021  * @message: Pointer to incoming dbus message
2022  * @wpa_s: wpa_supplicant structure for a network interface
2023  * Returns: NULL
2024  *
2025  * Setter function for "ApScan" property.
2026  */
2027 DBusMessage * wpas_dbus_setter_ap_scan(DBusMessage *message,
2028                                        struct wpa_supplicant *wpa_s)
2029 {
2030         DBusMessage *reply = NULL;
2031         dbus_uint32_t ap_scan;
2032
2033         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_UINT32,
2034                                                  &ap_scan);
2035         if (reply)
2036                 return reply;
2037
2038         if (wpa_supplicant_set_ap_scan(wpa_s, ap_scan)) {
2039                 return wpas_dbus_error_invalid_args(
2040                         message, "ap_scan must equal 0, 1 or 2");
2041         }
2042         return NULL;
2043 }
2044
2045
2046 /**
2047  * wpas_dbus_getter_ifname - Get interface name
2048  * @message: Pointer to incoming dbus message
2049  * @wpa_s: wpa_supplicant structure for a network interface
2050  * Returns: A dbus message containing a name of network interface
2051  * associated with with wpa_s
2052  *
2053  * Getter for "Ifname" property.
2054  */
2055 DBusMessage * wpas_dbus_getter_ifname(DBusMessage *message,
2056                                       struct wpa_supplicant *wpa_s)
2057 {
2058         const char *ifname = wpa_s->ifname;
2059         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
2060                                                 &ifname);
2061 }
2062
2063
2064 /**
2065  * wpas_dbus_getter_driver - Get interface name
2066  * @message: Pointer to incoming dbus message
2067  * @wpa_s: wpa_supplicant structure for a network interface
2068  * Returns: A dbus message containing a name of network interface
2069  * driver associated with with wpa_s
2070  *
2071  * Getter for "Driver" property.
2072  */
2073 DBusMessage * wpas_dbus_getter_driver(DBusMessage *message,
2074                                       struct wpa_supplicant *wpa_s)
2075 {
2076         const char *driver;
2077
2078         if (wpa_s->driver == NULL || wpa_s->driver->name == NULL) {
2079                 wpa_printf(MSG_DEBUG, "wpas_dbus_getter_driver[dbus]: "
2080                            "wpa_s has no driver set");
2081                 return wpas_dbus_error_unknown_error(message, NULL);
2082         }
2083
2084         driver = wpa_s->driver->name;
2085         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
2086                                                 &driver);
2087 }
2088
2089
2090 /**
2091  * wpas_dbus_getter_current_bss - Get current bss object path
2092  * @message: Pointer to incoming dbus message
2093  * @wpa_s: wpa_supplicant structure for a network interface
2094  * Returns: A dbus message containing a DBus object path to
2095  * current BSS
2096  *
2097  * Getter for "CurrentBSS" property.
2098  */
2099 DBusMessage * wpas_dbus_getter_current_bss(DBusMessage *message,
2100                                            struct wpa_supplicant *wpa_s)
2101 {
2102         DBusMessage *reply;
2103         char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *bss_obj_path = path_buf;
2104
2105         if (wpa_s->current_bss)
2106                 os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
2107                             "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
2108                             wpa_s->dbus_new_path, wpa_s->current_bss->id);
2109         else
2110                 os_snprintf(bss_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
2111
2112         reply = wpas_dbus_simple_property_getter(message,
2113                                                  DBUS_TYPE_OBJECT_PATH,
2114                                                  &bss_obj_path);
2115
2116         return reply;
2117 }
2118
2119
2120 /**
2121  * wpas_dbus_getter_current_network - Get current network object path
2122  * @message: Pointer to incoming dbus message
2123  * @wpa_s: wpa_supplicant structure for a network interface
2124  * Returns: A dbus message containing a DBus object path to
2125  * current network
2126  *
2127  * Getter for "CurrentNetwork" property.
2128  */
2129 DBusMessage * wpas_dbus_getter_current_network(DBusMessage *message,
2130                                                struct wpa_supplicant *wpa_s)
2131 {
2132         DBusMessage *reply;
2133         char path_buf[WPAS_DBUS_OBJECT_PATH_MAX], *net_obj_path = path_buf;
2134
2135         if (wpa_s->current_ssid)
2136                 os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX,
2137                             "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%u",
2138                             wpa_s->dbus_new_path, wpa_s->current_ssid->id);
2139         else
2140                 os_snprintf(net_obj_path, WPAS_DBUS_OBJECT_PATH_MAX, "/");
2141
2142         reply = wpas_dbus_simple_property_getter(message,
2143                                                  DBUS_TYPE_OBJECT_PATH,
2144                                                  &net_obj_path);
2145
2146         return reply;
2147 }
2148
2149
2150 /**
2151  * wpas_dbus_getter_bridge_ifname - Get interface name
2152  * @message: Pointer to incoming dbus message
2153  * @wpa_s: wpa_supplicant structure for a network interface
2154  * Returns: A dbus message containing a name of bridge network
2155  * interface associated with with wpa_s
2156  *
2157  * Getter for "BridgeIfname" property.
2158  */
2159 DBusMessage * wpas_dbus_getter_bridge_ifname(DBusMessage *message,
2160                                              struct wpa_supplicant *wpa_s)
2161 {
2162         const char *bridge_ifname = NULL;
2163
2164         bridge_ifname = wpa_s->bridge_ifname;
2165         if (bridge_ifname == NULL) {
2166                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bridge_ifname[dbus]: "
2167                            "wpa_s has no bridge interface name set");
2168                 return wpas_dbus_error_unknown_error(message, NULL);
2169         }
2170
2171         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
2172                                                 &bridge_ifname);
2173 }
2174
2175
2176 /**
2177  * wpas_dbus_getter_bsss - Get array of BSSs objects
2178  * @message: Pointer to incoming dbus message
2179  * @wpa_s: wpa_supplicant structure for a network interface
2180  * Returns: a dbus message containing an array of all known BSS objects
2181  * dbus paths
2182  *
2183  * Getter for "BSSs" property.
2184  */
2185 DBusMessage * wpas_dbus_getter_bsss(DBusMessage *message,
2186                                     struct wpa_supplicant *wpa_s)
2187 {
2188         DBusMessage *reply = NULL;
2189         struct wpa_bss *bss;
2190         char **paths;
2191         unsigned int i = 0;
2192
2193         paths = os_zalloc(wpa_s->num_bss * sizeof(char *));
2194         if (!paths) {
2195                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2196                                               NULL);
2197         }
2198
2199         /* Loop through scan results and append each result's object path */
2200         dl_list_for_each(bss, &wpa_s->bss_id, struct wpa_bss, list_id) {
2201                 paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
2202                 if (paths[i] == NULL) {
2203                         reply = dbus_message_new_error(message,
2204                                                        DBUS_ERROR_NO_MEMORY,
2205                                                        NULL);
2206                         goto out;
2207                 }
2208                 /* Construct the object path for this BSS. */
2209                 os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
2210                             "%s/" WPAS_DBUS_NEW_BSSIDS_PART "/%u",
2211                             wpa_s->dbus_new_path, bss->id);
2212         }
2213
2214         reply = wpas_dbus_simple_array_property_getter(message,
2215                                                        DBUS_TYPE_OBJECT_PATH,
2216                                                        paths, wpa_s->num_bss);
2217
2218 out:
2219         while (i)
2220                 os_free(paths[--i]);
2221         os_free(paths);
2222         return reply;
2223 }
2224
2225
2226 /**
2227  * wpas_dbus_getter_networks - Get array of networks objects
2228  * @message: Pointer to incoming dbus message
2229  * @wpa_s: wpa_supplicant structure for a network interface
2230  * Returns: a dbus message containing an array of all configured
2231  * networks dbus object paths.
2232  *
2233  * Getter for "Networks" property.
2234  */
2235 DBusMessage * wpas_dbus_getter_networks(DBusMessage *message,
2236                                         struct wpa_supplicant *wpa_s)
2237 {
2238         DBusMessage *reply = NULL;
2239         struct wpa_ssid *ssid;
2240         char **paths;
2241         unsigned int i = 0, num = 0;
2242
2243         if (wpa_s->conf == NULL) {
2244                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_networks[dbus]: "
2245                            "An error occurred getting networks list.");
2246                 return wpas_dbus_error_unknown_error(message, NULL);
2247         }
2248
2249         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next)
2250                 num++;
2251
2252         paths = os_zalloc(num * sizeof(char *));
2253         if (!paths) {
2254                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2255                                               NULL);
2256         }
2257
2258         /* Loop through configured networks and append object path of each */
2259         for (ssid = wpa_s->conf->ssid; ssid; ssid = ssid->next) {
2260                 paths[i] = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
2261                 if (paths[i] == NULL) {
2262                         reply = dbus_message_new_error(message,
2263                                                        DBUS_ERROR_NO_MEMORY,
2264                                                        NULL);
2265                         goto out;
2266                 }
2267
2268                 /* Construct the object path for this network. */
2269                 os_snprintf(paths[i++], WPAS_DBUS_OBJECT_PATH_MAX,
2270                             "%s/" WPAS_DBUS_NEW_NETWORKS_PART "/%d",
2271                             wpa_s->dbus_new_path, ssid->id);
2272         }
2273
2274         reply = wpas_dbus_simple_array_property_getter(message,
2275                                                        DBUS_TYPE_OBJECT_PATH,
2276                                                        paths, num);
2277
2278 out:
2279         while (i)
2280                 os_free(paths[--i]);
2281         os_free(paths);
2282         return reply;
2283 }
2284
2285
2286 /**
2287  * wpas_dbus_getter_blobs - Get all blobs defined for this interface
2288  * @message: Pointer to incoming dbus message
2289  * @wpa_s: wpa_supplicant structure for a network interface
2290  * Returns: a dbus message containing a dictionary of pairs (blob_name, blob)
2291  *
2292  * Getter for "Blobs" property.
2293  */
2294 DBusMessage * wpas_dbus_getter_blobs(DBusMessage *message,
2295                                      struct wpa_supplicant *wpa_s)
2296 {
2297         DBusMessage *reply = NULL;
2298         DBusMessageIter iter, variant_iter, dict_iter, entry_iter, array_iter;
2299         struct wpa_config_blob *blob;
2300
2301         if (message == NULL)
2302                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
2303         else
2304                 reply = dbus_message_new_method_return(message);
2305         if (!reply)
2306                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2307                                               NULL);
2308
2309         dbus_message_iter_init_append(reply, &iter);
2310
2311         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
2312                                               "a{say}", &variant_iter) ||
2313             !dbus_message_iter_open_container(&variant_iter, DBUS_TYPE_ARRAY,
2314                                               "{say}", &dict_iter)) {
2315                 dbus_message_unref(reply);
2316                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2317                                               NULL);
2318         }
2319
2320         blob = wpa_s->conf->blobs;
2321         while (blob) {
2322                 if (!dbus_message_iter_open_container(&dict_iter,
2323                                                       DBUS_TYPE_DICT_ENTRY,
2324                                                       NULL, &entry_iter) ||
2325                     !dbus_message_iter_append_basic(&entry_iter,
2326                                                     DBUS_TYPE_STRING,
2327                                                     &(blob->name)) ||
2328                     !dbus_message_iter_open_container(&entry_iter,
2329                                                       DBUS_TYPE_ARRAY,
2330                                                       DBUS_TYPE_BYTE_AS_STRING,
2331                                                       &array_iter) ||
2332                     !dbus_message_iter_append_fixed_array(&array_iter,
2333                                                           DBUS_TYPE_BYTE,
2334                                                           &(blob->data),
2335                                                           blob->len) ||
2336                     !dbus_message_iter_close_container(&entry_iter,
2337                                                        &array_iter) ||
2338                     !dbus_message_iter_close_container(&dict_iter,
2339                                                        &entry_iter)) {
2340                         dbus_message_unref(reply);
2341                         return dbus_message_new_error(message,
2342                                                       DBUS_ERROR_NO_MEMORY,
2343                                                       NULL);
2344                 }
2345
2346                 blob = blob->next;
2347         }
2348
2349         if (!dbus_message_iter_close_container(&variant_iter, &dict_iter) ||
2350             !dbus_message_iter_close_container(&iter, &variant_iter)) {
2351                 dbus_message_unref(reply);
2352                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2353                                               NULL);
2354         }
2355
2356         return reply;
2357 }
2358
2359
2360 /**
2361  * wpas_dbus_getter_bss_bssid - Return the BSSID of a BSS
2362  * @message: Pointer to incoming dbus message
2363  * @bss: a pair of interface describing structure and bss's id
2364  * Returns: a dbus message containing the bssid for the requested bss
2365  *
2366  * Getter for "BSSID" property.
2367  */
2368 DBusMessage * wpas_dbus_getter_bss_bssid(DBusMessage *message,
2369                                          struct bss_handler_args *bss)
2370 {
2371         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2372
2373         if (!res) {
2374                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_bssid[dbus]: no "
2375                            "bss with id %d found", bss->id);
2376                 return NULL;
2377         }
2378
2379         return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
2380                                                       res->bssid, ETH_ALEN);
2381 }
2382
2383
2384 /**
2385  * wpas_dbus_getter_bss_ssid - Return the SSID of a BSS
2386  * @message: Pointer to incoming dbus message
2387  * @bss: a pair of interface describing structure and bss's id
2388  * Returns: a dbus message containing the ssid for the requested bss
2389  *
2390  * Getter for "SSID" property.
2391  */
2392 DBusMessage * wpas_dbus_getter_bss_ssid(DBusMessage *message,
2393                                               struct bss_handler_args *bss)
2394 {
2395         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2396
2397         if (!res) {
2398                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_ssid[dbus]: no "
2399                            "bss with id %d found", bss->id);
2400                 return NULL;
2401         }
2402
2403         return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
2404                                                       res->ssid,
2405                                                       res->ssid_len);
2406 }
2407
2408
2409 /**
2410  * wpas_dbus_getter_bss_privacy - Return the privacy flag of a BSS
2411  * @message: Pointer to incoming dbus message
2412  * @bss: a pair of interface describing structure and bss's id
2413  * Returns: a dbus message containing the privacy flag value of requested bss
2414  *
2415  * Getter for "Privacy" property.
2416  */
2417 DBusMessage * wpas_dbus_getter_bss_privacy(DBusMessage *message,
2418                                            struct bss_handler_args *bss)
2419 {
2420         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2421         dbus_bool_t privacy;
2422
2423         if (!res) {
2424                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_privacy[dbus]: no "
2425                            "bss with id %d found", bss->id);
2426                 return NULL;
2427         }
2428
2429         privacy = (res->caps & IEEE80211_CAP_PRIVACY) ? TRUE : FALSE;
2430         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
2431                                                 &privacy);
2432 }
2433
2434
2435 /**
2436  * wpas_dbus_getter_bss_mode - Return the mode of a BSS
2437  * @message: Pointer to incoming dbus message
2438  * @bss: a pair of interface describing structure and bss's id
2439  * Returns: a dbus message containing the mode of requested bss
2440  *
2441  * Getter for "Mode" property.
2442  */
2443 DBusMessage * wpas_dbus_getter_bss_mode(DBusMessage *message,
2444                                         struct bss_handler_args *bss)
2445 {
2446         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2447         const char *mode;
2448
2449         if (!res) {
2450                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_mode[dbus]: no "
2451                            "bss with id %d found", bss->id);
2452                 return NULL;
2453         }
2454
2455         if (res->caps & IEEE80211_CAP_IBSS)
2456                 mode = "ad-hoc";
2457         else
2458                 mode = "infrastructure";
2459
2460         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_STRING,
2461                                                 &mode);
2462 }
2463
2464
2465 /**
2466  * wpas_dbus_getter_bss_level - Return the signal strength of a BSS
2467  * @message: Pointer to incoming dbus message
2468  * @bss: a pair of interface describing structure and bss's id
2469  * Returns: a dbus message containing the signal strength of requested bss
2470  *
2471  * Getter for "Level" property.
2472  */
2473 DBusMessage * wpas_dbus_getter_bss_signal(DBusMessage *message,
2474                                           struct bss_handler_args *bss)
2475 {
2476         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2477
2478         if (!res) {
2479                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_signal[dbus]: no "
2480                            "bss with id %d found", bss->id);
2481                 return NULL;
2482         }
2483
2484         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_INT16,
2485                                                 &res->level);
2486 }
2487
2488
2489 /**
2490  * wpas_dbus_getter_bss_frequency - Return the frequency of a BSS
2491  * @message: Pointer to incoming dbus message
2492  * @bss: a pair of interface describing structure and bss's id
2493  * Returns: a dbus message containing the frequency of requested bss
2494  *
2495  * Getter for "Frequency" property.
2496  */
2497 DBusMessage * wpas_dbus_getter_bss_frequency(DBusMessage *message,
2498                                              struct bss_handler_args *bss)
2499 {
2500         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2501
2502         if (!res) {
2503                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_frequency[dbus]: "
2504                            "no bss with id %d found", bss->id);
2505                 return NULL;
2506         }
2507
2508         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_UINT16,
2509                                                 &res->freq);
2510 }
2511
2512
2513 static int cmp_u8s_desc(const void *a, const void *b)
2514 {
2515         return (*(u8 *) b - *(u8 *) a);
2516 }
2517
2518
2519 /**
2520  * wpas_dbus_getter_bss_rates - Return available bit rates of a BSS
2521  * @message: Pointer to incoming dbus message
2522  * @bss: a pair of interface describing structure and bss's id
2523  * Returns: a dbus message containing sorted array of bit rates
2524  *
2525  * Getter for "Rates" property.
2526  */
2527 DBusMessage * wpas_dbus_getter_bss_rates(DBusMessage *message,
2528                                             struct bss_handler_args *bss)
2529 {
2530         DBusMessage *reply;
2531         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2532         u8 *ie_rates = NULL;
2533         u32 *real_rates;
2534         int rates_num, i;
2535
2536         if (!res) {
2537                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_rates[dbus]: "
2538                            "no bss with id %d found", bss->id);
2539                 return NULL;
2540         }
2541
2542         rates_num = wpa_bss_get_bit_rates(res, &ie_rates);
2543         if (rates_num < 0)
2544                 return NULL;
2545
2546         qsort(ie_rates, rates_num, 1, cmp_u8s_desc);
2547
2548         real_rates = os_malloc(sizeof(u32) * rates_num);
2549         if (!real_rates) {
2550                 os_free(ie_rates);
2551                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2552                                               NULL);
2553         }
2554
2555         for (i = 0; i < rates_num; i++)
2556                 real_rates[i] = ie_rates[i] * 500000;
2557
2558         reply = wpas_dbus_simple_array_property_getter(message,
2559                                                        DBUS_TYPE_UINT32,
2560                                                        real_rates, rates_num);
2561
2562         os_free(ie_rates);
2563         os_free(real_rates);
2564         return reply;
2565 }
2566
2567
2568 static DBusMessage * wpas_dbus_get_bss_security_prop(
2569         DBusMessage *message, struct wpa_ie_data *ie_data)
2570 {
2571         DBusMessage *reply;
2572         DBusMessageIter iter, iter_dict, variant_iter;
2573         const char *group;
2574         const char *pairwise[2]; /* max 2 pairwise ciphers is supported */
2575         const char *key_mgmt[7]; /* max 7 key managements may be supported */
2576         int n;
2577
2578         if (message == NULL)
2579                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
2580         else
2581                 reply = dbus_message_new_method_return(message);
2582         if (!reply)
2583                 goto nomem;
2584
2585         dbus_message_iter_init_append(reply, &iter);
2586         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
2587                                               "a{sv}", &variant_iter))
2588                 goto nomem;
2589
2590         if (!wpa_dbus_dict_open_write(&variant_iter, &iter_dict))
2591                 goto nomem;
2592
2593         /* KeyMgmt */
2594         n = 0;
2595         if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK)
2596                 key_mgmt[n++] = "wpa-psk";
2597         if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_PSK)
2598                 key_mgmt[n++] = "wpa-ft-psk";
2599         if (ie_data->key_mgmt & WPA_KEY_MGMT_PSK_SHA256)
2600                 key_mgmt[n++] = "wpa-psk-sha256";
2601         if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X)
2602                 key_mgmt[n++] = "wpa-eap";
2603         if (ie_data->key_mgmt & WPA_KEY_MGMT_FT_IEEE8021X)
2604                 key_mgmt[n++] = "wpa-ft-eap";
2605         if (ie_data->key_mgmt & WPA_KEY_MGMT_IEEE8021X_SHA256)
2606                 key_mgmt[n++] = "wpa-eap-sha256";
2607         if (ie_data->key_mgmt & WPA_KEY_MGMT_NONE)
2608                 key_mgmt[n++] = "wpa-none";
2609
2610         if (!wpa_dbus_dict_append_string_array(&iter_dict, "KeyMgmt",
2611                                                key_mgmt, n))
2612                 goto nomem;
2613
2614         /* Group */
2615         switch (ie_data->group_cipher) {
2616         case WPA_CIPHER_WEP40:
2617                 group = "wep40";
2618                 break;
2619         case WPA_CIPHER_TKIP:
2620                 group = "tkip";
2621                 break;
2622         case WPA_CIPHER_CCMP:
2623                 group = "ccmp";
2624                 break;
2625         case WPA_CIPHER_WEP104:
2626                 group = "wep104";
2627                 break;
2628         default:
2629                 group = "";
2630                 break;
2631         }
2632
2633         if (!wpa_dbus_dict_append_string(&iter_dict, "Group", group))
2634                 goto nomem;
2635
2636         /* Pairwise */
2637         n = 0;
2638         if (ie_data->pairwise_cipher & WPA_CIPHER_TKIP)
2639                 pairwise[n++] = "tkip";
2640         if (ie_data->pairwise_cipher & WPA_CIPHER_CCMP)
2641                 pairwise[n++] = "ccmp";
2642
2643         if (!wpa_dbus_dict_append_string_array(&iter_dict, "Pairwise",
2644                                                pairwise, n))
2645                 goto nomem;
2646
2647         /* Management group (RSN only) */
2648         if (ie_data->proto == WPA_PROTO_RSN) {
2649                 switch (ie_data->mgmt_group_cipher) {
2650 #ifdef CONFIG_IEEE80211W
2651                 case WPA_CIPHER_AES_128_CMAC:
2652                         group = "aes128cmac";
2653                         break;
2654 #endif /* CONFIG_IEEE80211W */
2655                 default:
2656                         group = "";
2657                         break;
2658                 }
2659
2660                 if (!wpa_dbus_dict_append_string(&iter_dict, "MgmtGroup",
2661                                                  group))
2662                         goto nomem;
2663         }
2664
2665         if (!wpa_dbus_dict_close_write(&variant_iter, &iter_dict))
2666                 goto nomem;
2667         if (!dbus_message_iter_close_container(&iter, &variant_iter))
2668                 goto nomem;
2669
2670         return reply;
2671
2672 nomem:
2673         if (reply)
2674                 dbus_message_unref(reply);
2675
2676         return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY, NULL);
2677 }
2678
2679
2680 /**
2681  * wpas_dbus_getter_bss_wpa - Return the WPA options of a BSS
2682  * @message: Pointer to incoming dbus message
2683  * @bss: a pair of interface describing structure and bss's id
2684  * Returns: a dbus message containing the WPA options of requested bss
2685  *
2686  * Getter for "WPA" property.
2687  */
2688 DBusMessage * wpas_dbus_getter_bss_wpa(DBusMessage *message,
2689                                        struct bss_handler_args *bss)
2690 {
2691         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2692         struct wpa_ie_data wpa_data;
2693         const u8 *ie;
2694
2695         if (!res) {
2696                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_wpa[dbus]: no "
2697                            "bss with id %d found", bss->id);
2698                 return NULL;
2699         }
2700
2701         os_memset(&wpa_data, 0, sizeof(wpa_data));
2702         ie = wpa_bss_get_vendor_ie(res, WPA_IE_VENDOR_TYPE);
2703         if (ie)
2704                 wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data);
2705
2706         return wpas_dbus_get_bss_security_prop(message, &wpa_data);
2707 }
2708
2709
2710 /**
2711  * wpas_dbus_getter_bss_rsn - Return the RSN options of a BSS
2712  * @message: Pointer to incoming dbus message
2713  * @bss: a pair of interface describing structure and bss's id
2714  * Returns: a dbus message containing the RSN options of requested bss
2715  *
2716  * Getter for "RSN" property.
2717  */
2718 DBusMessage * wpas_dbus_getter_bss_rsn(DBusMessage *message,
2719                                        struct bss_handler_args *bss)
2720 {
2721         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2722         struct wpa_ie_data wpa_data;
2723         const u8 *ie;
2724
2725         if (!res) {
2726                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_rsn[dbus]: no "
2727                            "bss with id %d found", bss->id);
2728                 return NULL;
2729         }
2730
2731         os_memset(&wpa_data, 0, sizeof(wpa_data));
2732         ie = wpa_bss_get_ie(res, WLAN_EID_RSN);
2733         if (ie)
2734                 wpa_parse_wpa_ie(ie, 2 + ie[1], &wpa_data);
2735
2736         return wpas_dbus_get_bss_security_prop(message, &wpa_data);
2737 }
2738
2739
2740 /**
2741  * wpas_dbus_getter_bss_ies - Return all IEs of a BSS
2742  * @message: Pointer to incoming dbus message
2743  * @bss: a pair of interface describing structure and bss's id
2744  * Returns: a dbus message containing IEs byte array
2745  *
2746  * Getter for "IEs" property.
2747  */
2748 DBusMessage * wpas_dbus_getter_bss_ies(DBusMessage *message,
2749                                        struct bss_handler_args *bss)
2750 {
2751         struct wpa_bss *res = wpa_bss_get_id(bss->wpa_s, bss->id);
2752
2753         if (!res) {
2754                 wpa_printf(MSG_ERROR, "wpas_dbus_getter_bss_ies[dbus]: no "
2755                            "bss with id %d found", bss->id);
2756                 return NULL;
2757         }
2758
2759         return wpas_dbus_simple_array_property_getter(message, DBUS_TYPE_BYTE,
2760                                                       res + 1, res->ie_len);
2761 }
2762
2763
2764 /**
2765  * wpas_dbus_getter_enabled - Check whether network is enabled or disabled
2766  * @message: Pointer to incoming dbus message
2767  * @wpas_dbus_setter_enabled: wpa_supplicant structure for a network interface
2768  * and wpa_ssid structure for a configured network
2769  * Returns: DBus message with boolean indicating state of configured network
2770  * or DBus error on failure
2771  *
2772  * Getter for "enabled" property of a configured network.
2773  */
2774 DBusMessage * wpas_dbus_getter_enabled(DBusMessage *message,
2775                                        struct network_handler_args *net)
2776 {
2777         dbus_bool_t enabled = net->ssid->disabled ? FALSE : TRUE;
2778         return wpas_dbus_simple_property_getter(message, DBUS_TYPE_BOOLEAN,
2779                                                 &enabled);
2780 }
2781
2782
2783 /**
2784  * wpas_dbus_setter_enabled - Mark a configured network as enabled or disabled
2785  * @message: Pointer to incoming dbus message
2786  * @wpas_dbus_setter_enabled: wpa_supplicant structure for a network interface
2787  * and wpa_ssid structure for a configured network
2788  * Returns: NULL indicating success or DBus error on failure
2789  *
2790  * Setter for "Enabled" property of a configured network.
2791  */
2792 DBusMessage * wpas_dbus_setter_enabled(DBusMessage *message,
2793                                        struct network_handler_args *net)
2794 {
2795         DBusMessage *reply = NULL;
2796
2797         struct wpa_supplicant *wpa_s;
2798         struct wpa_ssid *ssid;
2799
2800         dbus_bool_t enable;
2801
2802         reply = wpas_dbus_simple_property_setter(message, DBUS_TYPE_BOOLEAN,
2803                                                  &enable);
2804
2805         if (reply)
2806                 return reply;
2807
2808         wpa_s = net->wpa_s;
2809         ssid = net->ssid;
2810
2811         if (enable)
2812                 wpa_supplicant_enable_network(wpa_s, ssid);
2813         else
2814                 wpa_supplicant_disable_network(wpa_s, ssid);
2815
2816         return NULL;
2817 }
2818
2819
2820 /**
2821  * wpas_dbus_getter_network_properties - Get options for a configured network
2822  * @message: Pointer to incoming dbus message
2823  * @net: wpa_supplicant structure for a network interface and
2824  * wpa_ssid structure for a configured network
2825  * Returns: DBus message with network properties or DBus error on failure
2826  *
2827  * Getter for "Properties" property of a configured network.
2828  */
2829 DBusMessage * wpas_dbus_getter_network_properties(
2830         DBusMessage *message, struct network_handler_args *net)
2831 {
2832         DBusMessage *reply = NULL;
2833         DBusMessageIter iter, variant_iter, dict_iter;
2834         char **iterator;
2835         char **props = wpa_config_get_all(net->ssid, 0);
2836         if (!props)
2837                 return dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2838                                               NULL);
2839
2840         if (message == NULL)
2841                 reply = dbus_message_new(DBUS_MESSAGE_TYPE_SIGNAL);
2842         else
2843                 reply = dbus_message_new_method_return(message);
2844         if (!reply) {
2845                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2846                                                NULL);
2847                 goto out;
2848         }
2849
2850         dbus_message_iter_init_append(reply, &iter);
2851
2852         if (!dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT,
2853                         "a{sv}", &variant_iter) ||
2854             !wpa_dbus_dict_open_write(&variant_iter, &dict_iter)) {
2855                 dbus_message_unref(reply);
2856                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2857                                                NULL);
2858                 goto out;
2859         }
2860
2861         iterator = props;
2862         while (*iterator) {
2863                 if (!wpa_dbus_dict_append_string(&dict_iter, *iterator,
2864                                                  *(iterator + 1))) {
2865                         dbus_message_unref(reply);
2866                         reply = dbus_message_new_error(message,
2867                                                        DBUS_ERROR_NO_MEMORY,
2868                                                        NULL);
2869                         goto out;
2870                 }
2871                 iterator += 2;
2872         }
2873
2874
2875         if (!wpa_dbus_dict_close_write(&variant_iter, &dict_iter) ||
2876             !dbus_message_iter_close_container(&iter, &variant_iter)) {
2877                 dbus_message_unref(reply);
2878                 reply = dbus_message_new_error(message, DBUS_ERROR_NO_MEMORY,
2879                                                NULL);
2880                 goto out;
2881         }
2882
2883 out:
2884         iterator = props;
2885         while (*iterator) {
2886                 os_free(*iterator);
2887                 iterator++;
2888         }
2889         os_free(props);
2890         return reply;
2891 }
2892
2893
2894 /**
2895  * wpas_dbus_setter_network_properties - Set options for a configured network
2896  * @message: Pointer to incoming dbus message
2897  * @net: wpa_supplicant structure for a network interface and
2898  * wpa_ssid structure for a configured network
2899  * Returns: NULL indicating success or DBus error on failure
2900  *
2901  * Setter for "Properties" property of a configured network.
2902  */
2903 DBusMessage * wpas_dbus_setter_network_properties(
2904         DBusMessage *message, struct network_handler_args *net)
2905 {
2906         struct wpa_ssid *ssid = net->ssid;
2907
2908         DBusMessage *reply = NULL;
2909         DBusMessageIter iter, variant_iter;
2910
2911         dbus_message_iter_init(message, &iter);
2912
2913         dbus_message_iter_next(&iter);
2914         dbus_message_iter_next(&iter);
2915
2916         dbus_message_iter_recurse(&iter, &variant_iter);
2917
2918         reply = set_network_properties(message, net->wpa_s, ssid,
2919                                        &variant_iter);
2920         if (reply)
2921                 wpa_printf(MSG_DEBUG, "dbus control interface couldn't set "
2922                            "network properties");
2923
2924         return reply;
2925 }