2 * WPA Supplicant / dbus-based control interface
3 * Copyright (c) 2006, Dan Williams <dcbw@redhat.com> and Red Hat, Inc.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
20 #include "wpa_supplicant_i.h"
21 #include "drivers/driver.h"
23 #include "ctrl_iface_dbus.h"
24 #include "ctrl_iface_dbus_handlers.h"
26 #define _DBUS_VERSION (DBUS_VERSION_MAJOR << 8 | DBUS_VERSION_MINOR)
27 #define DBUS_VER(major, minor) ((major) << 8 | (minor))
29 #if _DBUS_VERSION < DBUS_VER(1,1)
30 #define dbus_watch_get_unix_fd dbus_watch_get_fd
34 struct ctrl_iface_dbus_priv {
37 struct wpa_global *global;
43 static void process_watch(struct ctrl_iface_dbus_priv *iface,
44 DBusWatch *watch, eloop_event_type type)
46 dbus_connection_ref(iface->con);
48 iface->should_dispatch = 0;
50 if (type == EVENT_TYPE_READ)
51 dbus_watch_handle(watch, DBUS_WATCH_READABLE);
52 else if (type == EVENT_TYPE_WRITE)
53 dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
54 else if (type == EVENT_TYPE_EXCEPTION)
55 dbus_watch_handle(watch, DBUS_WATCH_ERROR);
57 if (iface->should_dispatch) {
58 while (dbus_connection_get_dispatch_status(iface->con) ==
59 DBUS_DISPATCH_DATA_REMAINS)
60 dbus_connection_dispatch(iface->con);
61 iface->should_dispatch = 0;
64 dbus_connection_unref(iface->con);
68 static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
70 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
74 static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
76 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
80 static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
82 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
86 static void connection_setup_add_watch(struct ctrl_iface_dbus_priv *iface,
92 if (!dbus_watch_get_enabled(watch))
95 flags = dbus_watch_get_flags(watch);
96 fd = dbus_watch_get_unix_fd(watch);
98 eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
101 if (flags & DBUS_WATCH_READABLE) {
102 eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
105 if (flags & DBUS_WATCH_WRITABLE) {
106 eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
110 dbus_watch_set_data(watch, iface, NULL);
114 static void connection_setup_remove_watch(struct ctrl_iface_dbus_priv *iface,
120 flags = dbus_watch_get_flags(watch);
121 fd = dbus_watch_get_unix_fd(watch);
123 eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
125 if (flags & DBUS_WATCH_READABLE)
126 eloop_unregister_sock(fd, EVENT_TYPE_READ);
127 if (flags & DBUS_WATCH_WRITABLE)
128 eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
130 dbus_watch_set_data(watch, NULL, NULL);
134 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
136 connection_setup_add_watch(data, watch);
141 static void remove_watch(DBusWatch *watch, void *data)
143 connection_setup_remove_watch(data, watch);
147 static void watch_toggled(DBusWatch *watch, void *data)
149 if (dbus_watch_get_enabled(watch))
150 add_watch(watch, data);
152 remove_watch(watch, data);
156 static void process_timeout(void *eloop_ctx, void *sock_ctx)
158 DBusTimeout *timeout = sock_ctx;
160 dbus_timeout_handle(timeout);
164 static void connection_setup_add_timeout(struct ctrl_iface_dbus_priv *iface,
165 DBusTimeout *timeout)
167 if (!dbus_timeout_get_enabled(timeout))
170 eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
171 process_timeout, iface, timeout);
173 dbus_timeout_set_data(timeout, iface, NULL);
177 static void connection_setup_remove_timeout(struct ctrl_iface_dbus_priv *iface,
178 DBusTimeout *timeout)
180 eloop_cancel_timeout(process_timeout, iface, timeout);
181 dbus_timeout_set_data(timeout, NULL, NULL);
185 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
187 if (!dbus_timeout_get_enabled(timeout))
190 connection_setup_add_timeout(data, timeout);
196 static void remove_timeout(DBusTimeout *timeout, void *data)
198 connection_setup_remove_timeout(data, timeout);
202 static void timeout_toggled(DBusTimeout *timeout, void *data)
204 if (dbus_timeout_get_enabled(timeout))
205 add_timeout(timeout, data);
207 remove_timeout(timeout, data);
211 static void process_wakeup_main(int sig, void *eloop_ctx, void *signal_ctx)
213 struct ctrl_iface_dbus_priv *iface = signal_ctx;
215 if (sig != SIGPOLL || !iface->con)
218 if (dbus_connection_get_dispatch_status(iface->con) !=
219 DBUS_DISPATCH_DATA_REMAINS)
222 /* Only dispatch once - we do not want to starve other events */
223 dbus_connection_ref(iface->con);
224 dbus_connection_dispatch(iface->con);
225 dbus_connection_unref(iface->con);
230 * wakeup_main - Attempt to wake our mainloop up
231 * @data: dbus control interface private data
233 * Try to wake up the main eloop so it will process
234 * dbus events that may have happened.
236 static void wakeup_main(void *data)
238 struct ctrl_iface_dbus_priv *iface = data;
240 /* Use SIGPOLL to break out of the eloop select() */
242 iface->should_dispatch = 1;
247 * connection_setup_wakeup_main - Tell dbus about our wakeup_main function
248 * @iface: dbus control interface private data
249 * Returns: 0 on success, -1 on failure
251 * Register our wakeup_main handler with dbus
253 static int connection_setup_wakeup_main(struct ctrl_iface_dbus_priv *iface)
255 if (eloop_register_signal(SIGPOLL, process_wakeup_main, iface))
258 dbus_connection_set_wakeup_main_function(iface->con, wakeup_main,
266 * wpa_supplicant_dbus_next_objid - Return next available object id
267 * @iface: dbus control interface private data
270 u32 wpa_supplicant_dbus_next_objid (struct ctrl_iface_dbus_priv *iface)
272 return iface->next_objid++;
277 * wpas_dbus_decompose_object_path - Decompose an interface object path into parts
278 * @path: The dbus object path
279 * @network: (out) the configured network this object path refers to, if any
280 * @bssid: (out) the scanned bssid this object path refers to, if any
281 * Returns: The object path of the network interface this path refers to
283 * For a given object path, decomposes the object path into object id, network,
284 * and BSSID parts, if those parts exist.
286 char * wpas_dbus_decompose_object_path(const char *path, char **network,
289 const unsigned int dev_path_prefix_len =
290 strlen(WPAS_DBUS_PATH_INTERFACES "/");
294 /* Be a bit paranoid about path */
295 if (!path || strncmp(path, WPAS_DBUS_PATH_INTERFACES "/",
296 dev_path_prefix_len))
299 /* Ensure there's something at the end of the path */
300 if ((path + dev_path_prefix_len)[0] == '\0')
303 obj_path_only = strdup(path);
304 if (obj_path_only == NULL)
307 next_sep = strchr(obj_path_only + dev_path_prefix_len, '/');
308 if (next_sep != NULL) {
309 const char *net_part = strstr(next_sep,
310 WPAS_DBUS_NETWORKS_PART "/");
311 const char *bssid_part = strstr(next_sep,
312 WPAS_DBUS_BSSIDS_PART "/");
314 if (network && net_part) {
315 /* Deal with a request for a configured network */
316 const char *net_name = net_part +
317 strlen(WPAS_DBUS_NETWORKS_PART "/");
319 if (strlen(net_name))
320 *network = strdup(net_name);
321 } else if (bssid && bssid_part) {
322 /* Deal with a request for a scanned BSSID */
323 const char *bssid_name = bssid_part +
324 strlen(WPAS_DBUS_BSSIDS_PART "/");
325 if (strlen(bssid_name))
326 *bssid = strdup(bssid_name);
331 /* Cut off interface object path before "/" */
335 return obj_path_only;
340 * wpas_dbus_new_invalid_iface_error - Return a new invalid interface error message
341 * @message: Pointer to incoming dbus message this error refers to
342 * Returns: A dbus error message
344 * Convenience function to create and return an invalid interface error
346 DBusMessage * wpas_dbus_new_invalid_iface_error(DBusMessage *message)
348 return dbus_message_new_error(message, WPAS_ERROR_INVALID_IFACE,
349 "wpa_supplicant knows nothing about "
355 * wpas_dbus_new_invalid_network_error - Return a new invalid network error message
356 * @message: Pointer to incoming dbus message this error refers to
357 * Returns: a dbus error message
359 * Convenience function to create and return an invalid network error
361 DBusMessage * wpas_dbus_new_invalid_network_error(DBusMessage *message)
363 return dbus_message_new_error(message, WPAS_ERROR_INVALID_NETWORK,
364 "The requested network does not exist.");
369 * wpas_dbus_new_invalid_bssid_error - Return a new invalid bssid error message
370 * @message: Pointer to incoming dbus message this error refers to
371 * Returns: a dbus error message
373 * Convenience function to create and return an invalid bssid error
375 static DBusMessage * wpas_dbus_new_invalid_bssid_error(DBusMessage *message)
377 return dbus_message_new_error(message, WPAS_ERROR_INVALID_BSSID,
378 "The BSSID requested was invalid.");
383 * wpas_dispatch_network_method - dispatch messages for configured networks
384 * @message: the incoming dbus message
385 * @wpa_s: a network interface's data
386 * @network_id: id of the configured network we're interested in
387 * Returns: a reply dbus message, or a dbus error message
389 * This function dispatches all incoming dbus messages for configured networks.
391 static DBusMessage * wpas_dispatch_network_method(DBusMessage *message,
392 struct wpa_supplicant *wpa_s,
395 DBusMessage *reply = NULL;
396 const char *method = dbus_message_get_member(message);
397 struct wpa_ssid *ssid;
399 ssid = wpa_config_get_network(wpa_s->conf, network_id);
401 return wpas_dbus_new_invalid_network_error(message);
403 if (!strcmp(method, "set"))
404 reply = wpas_dbus_iface_set_network(message, wpa_s, ssid);
405 else if (!strcmp(method, "enable"))
406 reply = wpas_dbus_iface_enable_network(message, wpa_s, ssid);
407 else if (!strcmp(method, "disable"))
408 reply = wpas_dbus_iface_disable_network(message, wpa_s, ssid);
415 * wpas_dispatch_bssid_method - dispatch messages for scanned networks
416 * @message: the incoming dbus message
417 * @wpa_s: a network interface's data
418 * @bssid: bssid of the scanned network we're interested in
419 * Returns: a reply dbus message, or a dbus error message
421 * This function dispatches all incoming dbus messages for scanned networks.
423 static DBusMessage * wpas_dispatch_bssid_method(DBusMessage *message,
424 struct wpa_supplicant *wpa_s,
427 DBusMessage *reply = NULL;
428 const char *method = dbus_message_get_member(message);
429 struct wpa_scan_res *res = NULL;
432 /* Ensure we actually have scan data */
433 if (wpa_s->scan_res == NULL &&
434 wpa_supplicant_get_scan_results(wpa_s) < 0) {
435 reply = wpas_dbus_new_invalid_bssid_error(message);
439 /* Find the bssid's scan data */
440 for (i = 0; i < wpa_s->scan_res->num; i++) {
441 struct wpa_scan_res *search_res = wpa_s->scan_res->res[i];
444 memset(mac_str, 0, sizeof(mac_str));
445 snprintf(mac_str, sizeof(mac_str) - 1, WPAS_DBUS_BSSID_FORMAT,
446 MAC2STR(search_res->bssid));
447 if (!strcmp(bssid, mac_str)) {
454 reply = wpas_dbus_new_invalid_bssid_error(message);
458 /* Dispatch the method call against the scanned bssid */
459 if (!strcmp(method, "properties"))
460 reply = wpas_dbus_bssid_properties(message, wpa_s, res);
468 * wpas_iface_message_handler - Dispatch messages for interfaces or networks
469 * @connection: Connection to the system message bus
470 * @message: An incoming dbus message
471 * @user_data: A pointer to a dbus control interface data structure
472 * Returns: Whether or not the message was handled
474 * This function dispatches all incoming dbus messages for network interfaces,
475 * or objects owned by them, such as scanned BSSIDs and configured networks.
477 static DBusHandlerResult wpas_iface_message_handler(DBusConnection *connection,
478 DBusMessage *message,
481 struct wpa_supplicant *wpa_s = user_data;
482 const char *method = dbus_message_get_member(message);
483 const char *path = dbus_message_get_path(message);
484 const char *msg_interface = dbus_message_get_interface(message);
485 char *iface_obj_path = NULL;
486 char *network = NULL;
488 DBusMessage *reply = NULL;
490 /* Caller must specify a message interface */
494 iface_obj_path = wpas_dbus_decompose_object_path(path, &network,
496 if (iface_obj_path == NULL) {
497 reply = wpas_dbus_new_invalid_iface_error(message);
501 /* Make sure the message's object path actually refers to the
502 * wpa_supplicant structure it's supposed to (which is wpa_s)
504 if (wpa_supplicant_get_iface_by_dbus_path(wpa_s->global,
505 iface_obj_path) != wpa_s) {
506 reply = wpas_dbus_new_invalid_iface_error(message);
510 if (network && !strcmp(msg_interface, WPAS_DBUS_IFACE_NETWORK)) {
511 /* A method for one of this interface's configured networks */
512 int nid = strtoul(network, NULL, 10);
514 reply = wpas_dispatch_network_method(message, wpa_s,
517 reply = wpas_dbus_new_invalid_network_error(message);
518 } else if (bssid && !strcmp(msg_interface, WPAS_DBUS_IFACE_BSSID)) {
519 /* A method for one of this interface's scanned BSSIDs */
520 reply = wpas_dispatch_bssid_method(message, wpa_s, bssid);
521 } else if (!strcmp(msg_interface, WPAS_DBUS_IFACE_INTERFACE)) {
522 /* A method for an interface only. */
523 if (!strcmp(method, "scan"))
524 reply = wpas_dbus_iface_scan(message, wpa_s);
525 else if (!strcmp(method, "scanResults"))
526 reply = wpas_dbus_iface_scan_results(message, wpa_s);
527 else if (!strcmp(method, "addNetwork"))
528 reply = wpas_dbus_iface_add_network(message, wpa_s);
529 else if (!strcmp(method, "removeNetwork"))
530 reply = wpas_dbus_iface_remove_network(message, wpa_s);
531 else if (!strcmp(method, "selectNetwork"))
532 reply = wpas_dbus_iface_select_network(message, wpa_s);
533 else if (!strcmp(method, "capabilities"))
534 reply = wpas_dbus_iface_capabilities(message, wpa_s);
535 else if (!strcmp(method, "disconnect"))
536 reply = wpas_dbus_iface_disconnect(message, wpa_s);
537 else if (!strcmp(method, "setAPScan"))
538 reply = wpas_dbus_iface_set_ap_scan(message, wpa_s);
539 else if (!strcmp(method, "setSmartcardModules"))
540 reply = wpas_dbus_iface_set_smartcard_modules(message,
542 else if (!strcmp(method, "state"))
543 reply = wpas_dbus_iface_get_state(message, wpa_s);
544 else if (!strcmp(method, "scanning"))
545 reply = wpas_dbus_iface_get_scanning(message, wpa_s);
546 else if (!strcmp(method, "setBlobs"))
547 reply = wpas_dbus_iface_set_blobs(message, wpa_s);
548 else if (!strcmp(method, "removeBlobs"))
549 reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
551 else if (!os_strcmp(method, "wpsPbc"))
552 reply = wpas_dbus_iface_wps_pbc(message, wpa_s);
553 else if (!os_strcmp(method, "wpsPin"))
554 reply = wpas_dbus_iface_wps_pin(message, wpa_s);
555 else if (!os_strcmp(method, "wpsReg"))
556 reply = wpas_dbus_iface_wps_reg(message, wpa_s);
557 #endif /* CONFIG_WPS */
560 /* If the message was handled, send back the reply */
562 if (!dbus_message_get_no_reply(message))
563 dbus_connection_send(connection, reply, NULL);
564 dbus_message_unref(reply);
568 free(iface_obj_path);
571 return reply ? DBUS_HANDLER_RESULT_HANDLED :
572 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
577 * wpas_message_handler - dispatch incoming dbus messages
578 * @connection: connection to the system message bus
579 * @message: an incoming dbus message
580 * @user_data: a pointer to a dbus control interface data structure
581 * Returns: whether or not the message was handled
583 * This function dispatches all incoming dbus messages to the correct
584 * handlers, depending on what the message's target object path is,
585 * and what the method call is.
587 static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
588 DBusMessage *message, void *user_data)
590 struct ctrl_iface_dbus_priv *ctrl_iface = user_data;
593 const char *msg_interface;
594 DBusMessage *reply = NULL;
596 method = dbus_message_get_member(message);
597 path = dbus_message_get_path(message);
598 msg_interface = dbus_message_get_interface(message);
599 if (!method || !path || !ctrl_iface || !msg_interface)
600 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
602 /* Validate the method interface */
603 if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
604 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
606 if (!strcmp(path, WPAS_DBUS_PATH)) {
607 /* dispatch methods against our global dbus interface here */
608 if (!strcmp(method, "addInterface")) {
609 reply = wpas_dbus_global_add_interface(
610 message, ctrl_iface->global);
611 } else if (!strcmp(method, "removeInterface")) {
612 reply = wpas_dbus_global_remove_interface(
613 message, ctrl_iface->global);
614 } else if (!strcmp(method, "getInterface")) {
615 reply = wpas_dbus_global_get_interface(
616 message, ctrl_iface->global);
617 } else if (!strcmp(method, "setDebugParams")) {
618 reply = wpas_dbus_global_set_debugparams(
619 message, ctrl_iface->global);
623 /* If the message was handled, send back the reply */
625 if (!dbus_message_get_no_reply(message))
626 dbus_connection_send(connection, reply, NULL);
627 dbus_message_unref(reply);
630 return reply ? DBUS_HANDLER_RESULT_HANDLED :
631 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
636 * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
637 * @wpa_s: %wpa_supplicant network interface data
638 * Returns: 0 on success, -1 on failure
640 * Notify listeners that this interface has updated scan results.
642 void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
644 struct ctrl_iface_dbus_priv *iface = wpa_s->global->dbus_ctrl_iface;
645 DBusMessage *_signal;
648 /* Do nothing if the control interface is not turned on */
652 path = wpa_supplicant_get_dbus_path(wpa_s);
654 perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
655 "interface didn't have a dbus path");
656 wpa_printf(MSG_ERROR,
657 "wpa_supplicant_dbus_notify_scan_results[dbus]: "
658 "interface didn't have a dbus path; can't send "
659 "scan result signal.");
662 _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
663 "ScanResultsAvailable");
664 if (_signal == NULL) {
665 perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
666 "couldn't create dbus signal; likely out of memory");
667 wpa_printf(MSG_ERROR, "dbus control interface: not enough "
668 "memory to send scan results signal.");
671 dbus_connection_send(iface->con, _signal, NULL);
672 dbus_message_unref(_signal);
677 * wpa_supplicant_dbus_notify_state_change - Send a state change signal
678 * @wpa_s: %wpa_supplicant network interface data
679 * @new_state: new state wpa_supplicant is entering
680 * @old_state: old state wpa_supplicant is leaving
681 * Returns: 0 on success, -1 on failure
683 * Notify listeners that wpa_supplicant has changed state
685 void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
686 wpa_states new_state,
687 wpa_states old_state)
689 struct ctrl_iface_dbus_priv *iface;
690 DBusMessage *_signal = NULL;
692 const char *new_state_str, *old_state_str;
694 /* Do nothing if the control interface is not turned on */
695 if (wpa_s->global == NULL)
697 iface = wpa_s->global->dbus_ctrl_iface;
701 /* Only send signal if state really changed */
702 if (new_state == old_state)
705 path = wpa_supplicant_get_dbus_path(wpa_s);
707 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
708 "interface didn't have a dbus path");
709 wpa_printf(MSG_ERROR,
710 "wpa_supplicant_dbus_notify_state_change[dbus]: "
711 "interface didn't have a dbus path; can't send "
715 _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
717 if (_signal == NULL) {
718 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
719 "couldn't create dbus signal; likely out of memory");
720 wpa_printf(MSG_ERROR,
721 "wpa_supplicant_dbus_notify_state_change[dbus]: "
722 "couldn't create dbus signal; likely out of "
727 new_state_str = wpa_supplicant_state_txt(new_state);
728 old_state_str = wpa_supplicant_state_txt(old_state);
729 if (new_state_str == NULL || old_state_str == NULL) {
730 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
731 "couldn't convert state strings");
732 wpa_printf(MSG_ERROR,
733 "wpa_supplicant_dbus_notify_state_change[dbus]: "
734 "couldn't convert state strings.");
738 if (!dbus_message_append_args(_signal,
739 DBUS_TYPE_STRING, &new_state_str,
740 DBUS_TYPE_STRING, &old_state_str,
741 DBUS_TYPE_INVALID)) {
742 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
743 "not enough memory to construct state change signal.");
744 wpa_printf(MSG_ERROR,
745 "wpa_supplicant_dbus_notify_state_change[dbus]: "
746 "not enough memory to construct state change "
751 dbus_connection_send(iface->con, _signal, NULL);
754 dbus_message_unref(_signal);
759 * wpa_supplicant_dbus_notify_scanning - send scanning status
760 * @wpa_s: %wpa_supplicant network interface data
761 * Returns: 0 on success, -1 on failure
763 * Notify listeners of interface scanning state changes
765 void wpa_supplicant_dbus_notify_scanning(struct wpa_supplicant *wpa_s)
767 struct ctrl_iface_dbus_priv *iface = wpa_s->global->dbus_ctrl_iface;
768 DBusMessage *_signal;
770 dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
772 /* Do nothing if the control interface is not turned on */
776 path = wpa_supplicant_get_dbus_path(wpa_s);
778 perror("wpa_supplicant_dbus_notify_scanning[dbus]: interface "
779 "didn't have a dbus path");
780 wpa_printf(MSG_ERROR,
781 "%s[dbus]: interface didn't have a dbus path; "
782 "can't send scanning signal.", __FUNCTION__);
785 _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
787 if (_signal == NULL) {
788 perror("wpa_supplicant_dbus_notify_scanning[dbus]: couldn't "
789 "create dbus signal; likely out of memory");
790 wpa_printf(MSG_ERROR, "%s[dbus]: dbus control interface: not "
791 "enough memory to send scan results signal.",
796 if (dbus_message_append_args(_signal,
797 DBUS_TYPE_BOOLEAN, &scanning,
798 DBUS_TYPE_INVALID)) {
799 dbus_connection_send(iface->con, _signal, NULL);
801 perror("wpa_supplicant_dbus_notify_scanning[dbus]: not enough "
802 "memory to construct signal.");
803 wpa_printf(MSG_ERROR, "%s[dbus]: not enough memory to "
804 "construct signal.", __FUNCTION__);
806 dbus_message_unref(_signal);
811 void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
812 const struct wps_credential *cred)
814 struct ctrl_iface_dbus_priv *iface;
815 DBusMessage *_signal = NULL;
818 /* Do nothing if the control interface is not turned on */
819 if (wpa_s->global == NULL)
821 iface = wpa_s->global->dbus_ctrl_iface;
825 path = wpa_supplicant_get_dbus_path(wpa_s);
827 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
828 "interface didn't have a dbus path");
829 wpa_printf(MSG_ERROR,
830 "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
831 "interface didn't have a dbus path; can't send "
835 _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
837 if (_signal == NULL) {
838 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
839 "couldn't create dbus signal; likely out of memory");
840 wpa_printf(MSG_ERROR,
841 "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
842 "couldn't create dbus signal; likely out of "
847 if (!dbus_message_append_args(_signal,
848 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
849 &cred->cred_attr, cred->cred_attr_len,
850 DBUS_TYPE_INVALID)) {
851 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
852 "not enough memory to construct signal.");
853 wpa_printf(MSG_ERROR,
854 "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
855 "not enough memory to construct signal.");
859 dbus_connection_send(iface->con, _signal, NULL);
862 dbus_message_unref(_signal);
864 #else /* CONFIG_WPS */
865 void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
866 const struct wps_credential *cred)
869 #endif /* CONFIG_WPS */
873 * integrate_with_eloop - Register our mainloop integration with dbus
874 * @connection: connection to the system message bus
875 * @iface: a dbus control interface data structure
876 * Returns: 0 on success, -1 on failure
878 * We register our mainloop integration functions with dbus here.
880 static int integrate_with_eloop(DBusConnection *connection,
881 struct ctrl_iface_dbus_priv *iface)
883 if (!dbus_connection_set_watch_functions(connection, add_watch,
884 remove_watch, watch_toggled,
886 perror("dbus_connection_set_watch_functions[dbus]");
887 wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
891 if (!dbus_connection_set_timeout_functions(connection, add_timeout,
893 timeout_toggled, iface,
895 perror("dbus_connection_set_timeout_functions[dbus]");
896 wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
900 if (connection_setup_wakeup_main(iface) < 0) {
901 perror("connection_setup_wakeup_main[dbus]");
902 wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
911 * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
913 * @eloop_ctx: the DBusConnection to dispatch on
914 * @timeout_ctx: unused
916 * If clients are quick to notice that wpa_supplicant claimed its bus name,
917 * there may have been messages that came in before initialization was
918 * all finished. Dispatch those here.
920 static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
922 DBusConnection *con = eloop_ctx;
924 while (dbus_connection_get_dispatch_status(con) ==
925 DBUS_DISPATCH_DATA_REMAINS)
926 dbus_connection_dispatch(con);
931 * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
932 * @global: Pointer to global data from wpa_supplicant_init()
933 * Returns: Pointer to dbus_ctrl_iface date or %NULL on failure
935 * Initialize the dbus control interface and start receiving commands from
936 * external programs over the bus.
938 struct ctrl_iface_dbus_priv *
939 wpa_supplicant_dbus_ctrl_iface_init(struct wpa_global *global)
941 struct ctrl_iface_dbus_priv *iface;
944 DBusObjectPathVTable wpas_vtable = {
945 NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
948 iface = os_zalloc(sizeof(struct ctrl_iface_dbus_priv));
952 iface->global = global;
954 /* Get a reference to the system bus */
955 dbus_error_init(&error);
956 iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
957 dbus_error_free(&error);
959 perror("dbus_bus_get[ctrl_iface_dbus]");
960 wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
964 /* Tell dbus about our mainloop integration functions */
965 if (integrate_with_eloop(iface->con, iface))
968 /* Register the message handler for the global dbus interface */
969 if (!dbus_connection_register_object_path(iface->con,
970 WPAS_DBUS_PATH, &wpas_vtable,
972 perror("dbus_connection_register_object_path[dbus]");
973 wpa_printf(MSG_ERROR, "Could not set up DBus message "
978 /* Register our service with the message bus */
979 dbus_error_init(&error);
980 switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
982 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
985 case DBUS_REQUEST_NAME_REPLY_EXISTS:
986 case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
987 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
988 perror("dbus_bus_request_name[dbus]");
989 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
990 "already registered.");
993 perror("dbus_bus_request_name[dbus]");
994 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
995 "%s %s.", error.name, error.message);
998 dbus_error_free(&error);
1003 wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
1007 * Dispatch initial DBus messages that may have come in since the bus
1008 * name was claimed above. Happens when clients are quick to notice the
1009 * wpa_supplicant service.
1011 * FIXME: is there a better solution to this problem?
1013 eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
1019 wpa_supplicant_dbus_ctrl_iface_deinit(iface);
1025 * wpa_supplicant_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
1026 * @iface: Pointer to dbus private data from
1027 * wpa_supplicant_dbus_ctrl_iface_init()
1029 * Deinitialize the dbus control interface that was initialized with
1030 * wpa_supplicant_dbus_ctrl_iface_init().
1032 void wpa_supplicant_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_priv *iface)
1038 eloop_cancel_timeout(dispatch_initial_dbus_messages,
1040 dbus_connection_set_watch_functions(iface->con, NULL, NULL,
1042 dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
1044 dbus_connection_unref(iface->con);
1047 memset(iface, 0, sizeof(struct ctrl_iface_dbus_priv));
1053 * wpas_dbus_register_new_iface - Register a new interface with dbus
1054 * @wpa_s: %wpa_supplicant interface description structure to register
1055 * Returns: 0 on success, -1 on error
1057 * Registers a new interface with dbus and assigns it a dbus object path.
1059 int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
1061 struct ctrl_iface_dbus_priv *ctrl_iface =
1062 wpa_s->global->dbus_ctrl_iface;
1063 DBusConnection * con;
1065 DBusObjectPathVTable vtable = {
1066 NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
1071 /* Do nothing if the control interface is not turned on */
1072 if (ctrl_iface == NULL)
1075 con = ctrl_iface->con;
1076 next = wpa_supplicant_dbus_next_objid(ctrl_iface);
1078 /* Create and set the interface's object path */
1079 path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
1082 snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
1083 WPAS_DBUS_PATH_INTERFACES "/%u",
1085 if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
1086 wpa_printf(MSG_DEBUG,
1087 "Failed to set dbus path for interface %s",
1092 /* Register the message handler for the interface functions */
1093 if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
1094 perror("wpas_dbus_register_iface [dbus]");
1095 wpa_printf(MSG_ERROR, "Could not set up DBus message "
1096 "handler for interface %s.", wpa_s->ifname);
1108 * wpas_dbus_unregister_iface - Unregister an interface from dbus
1109 * @wpa_s: wpa_supplicant interface structure
1110 * Returns: 0 on success, -1 on failure
1112 * Unregisters the interface with dbus
1114 int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
1116 struct ctrl_iface_dbus_priv *ctrl_iface;
1117 DBusConnection *con;
1120 /* Do nothing if the control interface is not turned on */
1121 if (wpa_s == NULL || wpa_s->global == NULL)
1123 ctrl_iface = wpa_s->global->dbus_ctrl_iface;
1124 if (ctrl_iface == NULL)
1127 con = ctrl_iface->con;
1128 path = wpa_supplicant_get_dbus_path(wpa_s);
1130 if (!dbus_connection_unregister_object_path(con, path))
1133 free(wpa_s->dbus_path);
1134 wpa_s->dbus_path = NULL;
1141 * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
1142 * @global: Pointer to global data from wpa_supplicant_init()
1143 * @path: Pointer to a dbus object path representing an interface
1144 * Returns: Pointer to the interface or %NULL if not found
1146 struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
1147 struct wpa_global *global, const char *path)
1149 struct wpa_supplicant *wpa_s;
1151 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1152 if (strcmp(wpa_s->dbus_path, path) == 0)
1160 * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
1161 * @wpa_s: wpa_supplicant interface structure
1162 * @path: dbus path to set on the interface
1163 * Returns: 0 on succes, -1 on error
1165 int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
1168 u32 len = strlen (path);
1169 if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
1171 if (wpa_s->dbus_path)
1173 wpa_s->dbus_path = strdup(path);
1179 * wpa_supplicant_get_dbus_path - Get an interface's dbus path
1180 * @wpa_s: %wpa_supplicant interface structure
1181 * Returns: Interface's dbus object path, or %NULL on error
1183 const char * wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s)
1185 return wpa_s->dbus_path;