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"
22 #include "ctrl_iface_dbus.h"
23 #include "ctrl_iface_dbus_handlers.h"
25 #define _DBUS_VERSION (DBUS_VERSION_MAJOR << 8 | DBUS_VERSION_MINOR)
26 #define DBUS_VER(major, minor) ((major) << 8 | (minor))
28 #if _DBUS_VERSION < DBUS_VER(1,1)
29 #define dbus_watch_get_unix_fd dbus_watch_get_fd
33 struct ctrl_iface_dbus_priv {
36 struct wpa_global *global;
42 static void process_watch(struct ctrl_iface_dbus_priv *iface,
43 DBusWatch *watch, eloop_event_type type)
45 dbus_connection_ref(iface->con);
47 iface->should_dispatch = 0;
49 if (type == EVENT_TYPE_READ)
50 dbus_watch_handle(watch, DBUS_WATCH_READABLE);
51 else if (type == EVENT_TYPE_WRITE)
52 dbus_watch_handle(watch, DBUS_WATCH_WRITABLE);
53 else if (type == EVENT_TYPE_EXCEPTION)
54 dbus_watch_handle(watch, DBUS_WATCH_ERROR);
56 if (iface->should_dispatch) {
57 while (dbus_connection_get_dispatch_status(iface->con) ==
58 DBUS_DISPATCH_DATA_REMAINS)
59 dbus_connection_dispatch(iface->con);
60 iface->should_dispatch = 0;
63 dbus_connection_unref(iface->con);
67 static void process_watch_exception(int sock, void *eloop_ctx, void *sock_ctx)
69 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_EXCEPTION);
73 static void process_watch_read(int sock, void *eloop_ctx, void *sock_ctx)
75 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_READ);
79 static void process_watch_write(int sock, void *eloop_ctx, void *sock_ctx)
81 process_watch(eloop_ctx, sock_ctx, EVENT_TYPE_WRITE);
85 static void connection_setup_add_watch(struct ctrl_iface_dbus_priv *iface,
91 if (!dbus_watch_get_enabled(watch))
94 flags = dbus_watch_get_flags(watch);
95 fd = dbus_watch_get_unix_fd(watch);
97 eloop_register_sock(fd, EVENT_TYPE_EXCEPTION, process_watch_exception,
100 if (flags & DBUS_WATCH_READABLE) {
101 eloop_register_sock(fd, EVENT_TYPE_READ, process_watch_read,
104 if (flags & DBUS_WATCH_WRITABLE) {
105 eloop_register_sock(fd, EVENT_TYPE_WRITE, process_watch_write,
109 dbus_watch_set_data(watch, iface, NULL);
113 static void connection_setup_remove_watch(struct ctrl_iface_dbus_priv *iface,
119 flags = dbus_watch_get_flags(watch);
120 fd = dbus_watch_get_unix_fd(watch);
122 eloop_unregister_sock(fd, EVENT_TYPE_EXCEPTION);
124 if (flags & DBUS_WATCH_READABLE)
125 eloop_unregister_sock(fd, EVENT_TYPE_READ);
126 if (flags & DBUS_WATCH_WRITABLE)
127 eloop_unregister_sock(fd, EVENT_TYPE_WRITE);
129 dbus_watch_set_data(watch, NULL, NULL);
133 static dbus_bool_t add_watch(DBusWatch *watch, void *data)
135 connection_setup_add_watch(data, watch);
140 static void remove_watch(DBusWatch *watch, void *data)
142 connection_setup_remove_watch(data, watch);
146 static void watch_toggled(DBusWatch *watch, void *data)
148 if (dbus_watch_get_enabled(watch))
149 add_watch(watch, data);
151 remove_watch(watch, data);
155 static void process_timeout(void *eloop_ctx, void *sock_ctx)
157 DBusTimeout *timeout = sock_ctx;
159 dbus_timeout_handle(timeout);
163 static void connection_setup_add_timeout(struct ctrl_iface_dbus_priv *iface,
164 DBusTimeout *timeout)
166 if (!dbus_timeout_get_enabled(timeout))
169 eloop_register_timeout(0, dbus_timeout_get_interval(timeout) * 1000,
170 process_timeout, iface, timeout);
172 dbus_timeout_set_data(timeout, iface, NULL);
176 static void connection_setup_remove_timeout(struct ctrl_iface_dbus_priv *iface,
177 DBusTimeout *timeout)
179 eloop_cancel_timeout(process_timeout, iface, timeout);
180 dbus_timeout_set_data(timeout, NULL, NULL);
184 static dbus_bool_t add_timeout(DBusTimeout *timeout, void *data)
186 if (!dbus_timeout_get_enabled(timeout))
189 connection_setup_add_timeout(data, timeout);
195 static void remove_timeout(DBusTimeout *timeout, void *data)
197 connection_setup_remove_timeout(data, timeout);
201 static void timeout_toggled(DBusTimeout *timeout, void *data)
203 if (dbus_timeout_get_enabled(timeout))
204 add_timeout(timeout, data);
206 remove_timeout(timeout, data);
210 static void process_wakeup_main(int sig, void *eloop_ctx, void *signal_ctx)
212 struct ctrl_iface_dbus_priv *iface = signal_ctx;
214 if (sig != SIGPOLL || !iface->con)
217 if (dbus_connection_get_dispatch_status(iface->con) !=
218 DBUS_DISPATCH_DATA_REMAINS)
221 /* Only dispatch once - we do not want to starve other events */
222 dbus_connection_ref(iface->con);
223 dbus_connection_dispatch(iface->con);
224 dbus_connection_unref(iface->con);
229 * wakeup_main - Attempt to wake our mainloop up
230 * @data: dbus control interface private data
232 * Try to wake up the main eloop so it will process
233 * dbus events that may have happened.
235 static void wakeup_main(void *data)
237 struct ctrl_iface_dbus_priv *iface = data;
239 /* Use SIGPOLL to break out of the eloop select() */
241 iface->should_dispatch = 1;
246 * connection_setup_wakeup_main - Tell dbus about our wakeup_main function
247 * @iface: dbus control interface private data
248 * Returns: 0 on success, -1 on failure
250 * Register our wakeup_main handler with dbus
252 static int connection_setup_wakeup_main(struct ctrl_iface_dbus_priv *iface)
254 if (eloop_register_signal(SIGPOLL, process_wakeup_main, iface))
257 dbus_connection_set_wakeup_main_function(iface->con, wakeup_main,
265 * wpa_supplicant_dbus_next_objid - Return next available object id
266 * @iface: dbus control interface private data
269 u32 wpa_supplicant_dbus_next_objid (struct ctrl_iface_dbus_priv *iface)
271 return iface->next_objid++;
276 * wpas_dbus_decompose_object_path - Decompose an interface object path into parts
277 * @path: The dbus object path
278 * @network: (out) the configured network this object path refers to, if any
279 * @bssid: (out) the scanned bssid this object path refers to, if any
280 * Returns: The object path of the network interface this path refers to
282 * For a given object path, decomposes the object path into object id, network,
283 * and BSSID parts, if those parts exist.
285 char * wpas_dbus_decompose_object_path(const char *path, char **network,
288 const unsigned int dev_path_prefix_len =
289 strlen(WPAS_DBUS_PATH_INTERFACES "/");
293 /* Be a bit paranoid about path */
294 if (!path || strncmp(path, WPAS_DBUS_PATH_INTERFACES "/",
295 dev_path_prefix_len))
298 /* Ensure there's something at the end of the path */
299 if ((path + dev_path_prefix_len)[0] == '\0')
302 obj_path_only = strdup(path);
303 if (obj_path_only == NULL)
306 next_sep = strchr(obj_path_only + dev_path_prefix_len, '/');
307 if (next_sep != NULL) {
308 const char *net_part = strstr(next_sep,
309 WPAS_DBUS_NETWORKS_PART "/");
310 const char *bssid_part = strstr(next_sep,
311 WPAS_DBUS_BSSIDS_PART "/");
313 if (network && net_part) {
314 /* Deal with a request for a configured network */
315 const char *net_name = net_part +
316 strlen(WPAS_DBUS_NETWORKS_PART "/");
318 if (strlen(net_name))
319 *network = strdup(net_name);
320 } else if (bssid && bssid_part) {
321 /* Deal with a request for a scanned BSSID */
322 const char *bssid_name = bssid_part +
323 strlen(WPAS_DBUS_BSSIDS_PART "/");
324 if (strlen(bssid_name))
325 *bssid = strdup(bssid_name);
330 /* Cut off interface object path before "/" */
334 return obj_path_only;
339 * wpas_dbus_new_invalid_iface_error - Return a new invalid interface error message
340 * @message: Pointer to incoming dbus message this error refers to
341 * Returns: A dbus error message
343 * Convenience function to create and return an invalid interface error
345 DBusMessage * wpas_dbus_new_invalid_iface_error(DBusMessage *message)
347 return dbus_message_new_error(message, WPAS_ERROR_INVALID_IFACE,
348 "wpa_supplicant knows nothing about "
354 * wpas_dbus_new_invalid_network_error - Return a new invalid network error message
355 * @message: Pointer to incoming dbus message this error refers to
356 * Returns: a dbus error message
358 * Convenience function to create and return an invalid network error
360 DBusMessage * wpas_dbus_new_invalid_network_error(DBusMessage *message)
362 return dbus_message_new_error(message, WPAS_ERROR_INVALID_NETWORK,
363 "The requested network does not exist.");
368 * wpas_dbus_new_invalid_bssid_error - Return a new invalid bssid error message
369 * @message: Pointer to incoming dbus message this error refers to
370 * Returns: a dbus error message
372 * Convenience function to create and return an invalid bssid error
374 static DBusMessage * wpas_dbus_new_invalid_bssid_error(DBusMessage *message)
376 return dbus_message_new_error(message, WPAS_ERROR_INVALID_BSSID,
377 "The BSSID requested was invalid.");
382 * wpas_dispatch_network_method - dispatch messages for configured networks
383 * @message: the incoming dbus message
384 * @wpa_s: a network interface's data
385 * @network_id: id of the configured network we're interested in
386 * Returns: a reply dbus message, or a dbus error message
388 * This function dispatches all incoming dbus messages for configured networks.
390 static DBusMessage * wpas_dispatch_network_method(DBusMessage *message,
391 struct wpa_supplicant *wpa_s,
394 DBusMessage *reply = NULL;
395 const char *method = dbus_message_get_member(message);
396 struct wpa_ssid *ssid;
398 ssid = wpa_config_get_network(wpa_s->conf, network_id);
400 return wpas_dbus_new_invalid_network_error(message);
402 if (!strcmp(method, "set"))
403 reply = wpas_dbus_iface_set_network(message, wpa_s, ssid);
404 else if (!strcmp(method, "enable"))
405 reply = wpas_dbus_iface_enable_network(message, wpa_s, ssid);
406 else if (!strcmp(method, "disable"))
407 reply = wpas_dbus_iface_disable_network(message, wpa_s, ssid);
414 * wpas_dispatch_bssid_method - dispatch messages for scanned networks
415 * @message: the incoming dbus message
416 * @wpa_s: a network interface's data
417 * @bssid: bssid of the scanned network we're interested in
418 * Returns: a reply dbus message, or a dbus error message
420 * This function dispatches all incoming dbus messages for scanned networks.
422 static DBusMessage * wpas_dispatch_bssid_method(DBusMessage *message,
423 struct wpa_supplicant *wpa_s,
426 DBusMessage *reply = NULL;
427 const char *method = dbus_message_get_member(message);
428 struct wpa_scan_res *res = NULL;
431 /* Ensure we actually have scan data */
432 if (wpa_s->scan_res == NULL &&
433 wpa_supplicant_get_scan_results(wpa_s) < 0) {
434 reply = wpas_dbus_new_invalid_bssid_error(message);
438 /* Find the bssid's scan data */
439 for (i = 0; i < wpa_s->scan_res->num; i++) {
440 struct wpa_scan_res *search_res = wpa_s->scan_res->res[i];
443 memset(mac_str, 0, sizeof(mac_str));
444 snprintf(mac_str, sizeof(mac_str) - 1, WPAS_DBUS_BSSID_FORMAT,
445 MAC2STR(search_res->bssid));
446 if (!strcmp(bssid, mac_str)) {
453 reply = wpas_dbus_new_invalid_bssid_error(message);
457 /* Dispatch the method call against the scanned bssid */
458 if (!strcmp(method, "properties"))
459 reply = wpas_dbus_bssid_properties(message, wpa_s, res);
467 * wpas_iface_message_handler - Dispatch messages for interfaces or networks
468 * @connection: Connection to the system message bus
469 * @message: An incoming dbus message
470 * @user_data: A pointer to a dbus control interface data structure
471 * Returns: Whether or not the message was handled
473 * This function dispatches all incoming dbus messages for network interfaces,
474 * or objects owned by them, such as scanned BSSIDs and configured networks.
476 static DBusHandlerResult wpas_iface_message_handler(DBusConnection *connection,
477 DBusMessage *message,
480 struct wpa_supplicant *wpa_s = user_data;
481 const char *method = dbus_message_get_member(message);
482 const char *path = dbus_message_get_path(message);
483 const char *msg_interface = dbus_message_get_interface(message);
484 char *iface_obj_path = NULL;
485 char *network = NULL;
487 DBusMessage *reply = NULL;
489 /* Caller must specify a message interface */
493 iface_obj_path = wpas_dbus_decompose_object_path(path, &network,
495 if (iface_obj_path == NULL) {
496 reply = wpas_dbus_new_invalid_iface_error(message);
500 /* Make sure the message's object path actually refers to the
501 * wpa_supplicant structure it's supposed to (which is wpa_s)
503 if (wpa_supplicant_get_iface_by_dbus_path(wpa_s->global,
504 iface_obj_path) != wpa_s) {
505 reply = wpas_dbus_new_invalid_iface_error(message);
509 if (network && !strcmp(msg_interface, WPAS_DBUS_IFACE_NETWORK)) {
510 /* A method for one of this interface's configured networks */
511 int nid = strtoul(network, NULL, 10);
513 reply = wpas_dispatch_network_method(message, wpa_s,
516 reply = wpas_dbus_new_invalid_network_error(message);
517 } else if (bssid && !strcmp(msg_interface, WPAS_DBUS_IFACE_BSSID)) {
518 /* A method for one of this interface's scanned BSSIDs */
519 reply = wpas_dispatch_bssid_method(message, wpa_s, bssid);
520 } else if (!strcmp(msg_interface, WPAS_DBUS_IFACE_INTERFACE)) {
521 /* A method for an interface only. */
522 if (!strcmp(method, "scan"))
523 reply = wpas_dbus_iface_scan(message, wpa_s);
524 else if (!strcmp(method, "scanResults"))
525 reply = wpas_dbus_iface_scan_results(message, wpa_s);
526 else if (!strcmp(method, "addNetwork"))
527 reply = wpas_dbus_iface_add_network(message, wpa_s);
528 else if (!strcmp(method, "removeNetwork"))
529 reply = wpas_dbus_iface_remove_network(message, wpa_s);
530 else if (!strcmp(method, "selectNetwork"))
531 reply = wpas_dbus_iface_select_network(message, wpa_s);
532 else if (!strcmp(method, "capabilities"))
533 reply = wpas_dbus_iface_capabilities(message, wpa_s);
534 else if (!strcmp(method, "disconnect"))
535 reply = wpas_dbus_iface_disconnect(message, wpa_s);
536 else if (!strcmp(method, "setAPScan"))
537 reply = wpas_dbus_iface_set_ap_scan(message, wpa_s);
538 else if (!strcmp(method, "setSmartcardModules"))
539 reply = wpas_dbus_iface_set_smartcard_modules(message,
541 else if (!strcmp(method, "state"))
542 reply = wpas_dbus_iface_get_state(message, wpa_s);
543 else if (!strcmp(method, "scanning"))
544 reply = wpas_dbus_iface_get_scanning(message, wpa_s);
545 else if (!strcmp(method, "setBlobs"))
546 reply = wpas_dbus_iface_set_blobs(message, wpa_s);
547 else if (!strcmp(method, "removeBlobs"))
548 reply = wpas_dbus_iface_remove_blobs(message, wpa_s);
551 /* If the message was handled, send back the reply */
553 if (!dbus_message_get_no_reply(message))
554 dbus_connection_send(connection, reply, NULL);
555 dbus_message_unref(reply);
559 free(iface_obj_path);
562 return reply ? DBUS_HANDLER_RESULT_HANDLED :
563 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
568 * wpas_message_handler - dispatch incoming dbus messages
569 * @connection: connection to the system message bus
570 * @message: an incoming dbus message
571 * @user_data: a pointer to a dbus control interface data structure
572 * Returns: whether or not the message was handled
574 * This function dispatches all incoming dbus messages to the correct
575 * handlers, depending on what the message's target object path is,
576 * and what the method call is.
578 static DBusHandlerResult wpas_message_handler(DBusConnection *connection,
579 DBusMessage *message, void *user_data)
581 struct ctrl_iface_dbus_priv *ctrl_iface = user_data;
584 const char *msg_interface;
585 DBusMessage *reply = NULL;
587 method = dbus_message_get_member(message);
588 path = dbus_message_get_path(message);
589 msg_interface = dbus_message_get_interface(message);
590 if (!method || !path || !ctrl_iface || !msg_interface)
591 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
593 /* Validate the method interface */
594 if (strcmp(msg_interface, WPAS_DBUS_INTERFACE) != 0)
595 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
597 if (!strcmp(path, WPAS_DBUS_PATH)) {
598 /* dispatch methods against our global dbus interface here */
599 if (!strcmp(method, "addInterface")) {
600 reply = wpas_dbus_global_add_interface(
601 message, ctrl_iface->global);
602 } else if (!strcmp(method, "removeInterface")) {
603 reply = wpas_dbus_global_remove_interface(
604 message, ctrl_iface->global);
605 } else if (!strcmp(method, "getInterface")) {
606 reply = wpas_dbus_global_get_interface(
607 message, ctrl_iface->global);
608 } else if (!strcmp(method, "setDebugParams")) {
609 reply = wpas_dbus_global_set_debugparams(
610 message, ctrl_iface->global);
614 /* If the message was handled, send back the reply */
616 if (!dbus_message_get_no_reply(message))
617 dbus_connection_send(connection, reply, NULL);
618 dbus_message_unref(reply);
621 return reply ? DBUS_HANDLER_RESULT_HANDLED :
622 DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
627 * wpa_supplicant_dbus_notify_scan_results - Send a scan results signal
628 * @wpa_s: %wpa_supplicant network interface data
629 * Returns: 0 on success, -1 on failure
631 * Notify listeners that this interface has updated scan results.
633 void wpa_supplicant_dbus_notify_scan_results(struct wpa_supplicant *wpa_s)
635 struct ctrl_iface_dbus_priv *iface = wpa_s->global->dbus_ctrl_iface;
636 DBusMessage *_signal;
639 /* Do nothing if the control interface is not turned on */
643 path = wpa_supplicant_get_dbus_path(wpa_s);
645 perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
646 "interface didn't have a dbus path");
647 wpa_printf(MSG_ERROR,
648 "wpa_supplicant_dbus_notify_scan_results[dbus]: "
649 "interface didn't have a dbus path; can't send "
650 "scan result signal.");
653 _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
654 "ScanResultsAvailable");
655 if (_signal == NULL) {
656 perror("wpa_supplicant_dbus_notify_scan_results[dbus]: "
657 "couldn't create dbus signal; likely out of memory");
658 wpa_printf(MSG_ERROR, "dbus control interface: not enough "
659 "memory to send scan results signal.");
662 dbus_connection_send(iface->con, _signal, NULL);
663 dbus_message_unref(_signal);
668 * wpa_supplicant_dbus_notify_state_change - Send a state change signal
669 * @wpa_s: %wpa_supplicant network interface data
670 * @new_state: new state wpa_supplicant is entering
671 * @old_state: old state wpa_supplicant is leaving
672 * Returns: 0 on success, -1 on failure
674 * Notify listeners that wpa_supplicant has changed state
676 void wpa_supplicant_dbus_notify_state_change(struct wpa_supplicant *wpa_s,
677 wpa_states new_state,
678 wpa_states old_state)
680 struct ctrl_iface_dbus_priv *iface;
681 DBusMessage *_signal = NULL;
683 const char *new_state_str, *old_state_str;
685 /* Do nothing if the control interface is not turned on */
686 if (wpa_s->global == NULL)
688 iface = wpa_s->global->dbus_ctrl_iface;
692 /* Only send signal if state really changed */
693 if (new_state == old_state)
696 path = wpa_supplicant_get_dbus_path(wpa_s);
698 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
699 "interface didn't have a dbus path");
700 wpa_printf(MSG_ERROR,
701 "wpa_supplicant_dbus_notify_state_change[dbus]: "
702 "interface didn't have a dbus path; can't send "
706 _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
708 if (_signal == NULL) {
709 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
710 "couldn't create dbus signal; likely out of memory");
711 wpa_printf(MSG_ERROR,
712 "wpa_supplicant_dbus_notify_state_change[dbus]: "
713 "couldn't create dbus signal; likely out of "
718 new_state_str = wpa_supplicant_state_txt(new_state);
719 old_state_str = wpa_supplicant_state_txt(old_state);
720 if (new_state_str == NULL || old_state_str == NULL) {
721 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
722 "couldn't convert state strings");
723 wpa_printf(MSG_ERROR,
724 "wpa_supplicant_dbus_notify_state_change[dbus]: "
725 "couldn't convert state strings.");
729 if (!dbus_message_append_args(_signal,
730 DBUS_TYPE_STRING, &new_state_str,
731 DBUS_TYPE_STRING, &old_state_str,
732 DBUS_TYPE_INVALID)) {
733 perror("wpa_supplicant_dbus_notify_state_change[dbus]: "
734 "not enough memory to construct state change signal.");
735 wpa_printf(MSG_ERROR,
736 "wpa_supplicant_dbus_notify_state_change[dbus]: "
737 "not enough memory to construct state change "
742 dbus_connection_send(iface->con, _signal, NULL);
745 dbus_message_unref(_signal);
750 * wpa_supplicant_dbus_notify_scanning - send scanning status
751 * @wpa_s: %wpa_supplicant network interface data
752 * Returns: 0 on success, -1 on failure
754 * Notify listeners of interface scanning state changes
756 void wpa_supplicant_dbus_notify_scanning(struct wpa_supplicant *wpa_s)
758 struct ctrl_iface_dbus_priv *iface = wpa_s->global->dbus_ctrl_iface;
759 DBusMessage *_signal;
761 dbus_bool_t scanning = wpa_s->scanning ? TRUE : FALSE;
763 /* Do nothing if the control interface is not turned on */
767 path = wpa_supplicant_get_dbus_path(wpa_s);
769 perror("wpa_supplicant_dbus_notify_scanning[dbus]: interface "
770 "didn't have a dbus path");
771 wpa_printf(MSG_ERROR,
772 "%s[dbus]: interface didn't have a dbus path; "
773 "can't send scanning signal.", __FUNCTION__);
776 _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
778 if (_signal == NULL) {
779 perror("wpa_supplicant_dbus_notify_scanning[dbus]: couldn't "
780 "create dbus signal; likely out of memory");
781 wpa_printf(MSG_ERROR, "%s[dbus]: dbus control interface: not "
782 "enough memory to send scan results signal.",
787 if (dbus_message_append_args(_signal,
788 DBUS_TYPE_BOOLEAN, &scanning,
789 DBUS_TYPE_INVALID)) {
790 dbus_connection_send(iface->con, _signal, NULL);
792 perror("wpa_supplicant_dbus_notify_scanning[dbus]: not enough "
793 "memory to construct signal.");
794 wpa_printf(MSG_ERROR, "%s[dbus]: not enough memory to "
795 "construct signal.", __FUNCTION__);
797 dbus_message_unref(_signal);
802 void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
803 const struct wps_credential *cred)
805 struct ctrl_iface_dbus_priv *iface;
806 DBusMessage *_signal = NULL;
809 /* Do nothing if the control interface is not turned on */
810 if (wpa_s->global == NULL)
812 iface = wpa_s->global->dbus_ctrl_iface;
816 path = wpa_supplicant_get_dbus_path(wpa_s);
818 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
819 "interface didn't have a dbus path");
820 wpa_printf(MSG_ERROR,
821 "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
822 "interface didn't have a dbus path; can't send "
826 _signal = dbus_message_new_signal(path, WPAS_DBUS_IFACE_INTERFACE,
828 if (_signal == NULL) {
829 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
830 "couldn't create dbus signal; likely out of memory");
831 wpa_printf(MSG_ERROR,
832 "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
833 "couldn't create dbus signal; likely out of "
838 if (!dbus_message_append_args(_signal,
839 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
840 &cred->cred_attr, cred->cred_attr_len,
841 DBUS_TYPE_INVALID)) {
842 perror("wpa_supplicant_dbus_notify_wps_cred[dbus]: "
843 "not enough memory to construct signal.");
844 wpa_printf(MSG_ERROR,
845 "wpa_supplicant_dbus_notify_wps_cred[dbus]: "
846 "not enough memory to construct signal.");
850 dbus_connection_send(iface->con, _signal, NULL);
853 dbus_message_unref(_signal);
855 #else /* CONFIG_WPS */
856 void wpa_supplicant_dbus_notify_wps_cred(struct wpa_supplicant *wpa_s,
857 const struct wps_credential *cred)
860 #endif /* CONFIG_WPS */
864 * integrate_with_eloop - Register our mainloop integration with dbus
865 * @connection: connection to the system message bus
866 * @iface: a dbus control interface data structure
867 * Returns: 0 on success, -1 on failure
869 * We register our mainloop integration functions with dbus here.
871 static int integrate_with_eloop(DBusConnection *connection,
872 struct ctrl_iface_dbus_priv *iface)
874 if (!dbus_connection_set_watch_functions(connection, add_watch,
875 remove_watch, watch_toggled,
877 perror("dbus_connection_set_watch_functions[dbus]");
878 wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
882 if (!dbus_connection_set_timeout_functions(connection, add_timeout,
884 timeout_toggled, iface,
886 perror("dbus_connection_set_timeout_functions[dbus]");
887 wpa_printf(MSG_ERROR, "Not enough memory to set up dbus.");
891 if (connection_setup_wakeup_main(iface) < 0) {
892 perror("connection_setup_wakeup_main[dbus]");
893 wpa_printf(MSG_ERROR, "Could not setup main wakeup function.");
902 * dispatch_initial_dbus_messages - Dispatch initial dbus messages after
904 * @eloop_ctx: the DBusConnection to dispatch on
905 * @timeout_ctx: unused
907 * If clients are quick to notice that wpa_supplicant claimed its bus name,
908 * there may have been messages that came in before initialization was
909 * all finished. Dispatch those here.
911 static void dispatch_initial_dbus_messages(void *eloop_ctx, void *timeout_ctx)
913 DBusConnection *con = eloop_ctx;
915 while (dbus_connection_get_dispatch_status(con) ==
916 DBUS_DISPATCH_DATA_REMAINS)
917 dbus_connection_dispatch(con);
922 * wpa_supplicant_dbus_ctrl_iface_init - Initialize dbus control interface
923 * @global: Pointer to global data from wpa_supplicant_init()
924 * Returns: Pointer to dbus_ctrl_iface date or %NULL on failure
926 * Initialize the dbus control interface and start receiving commands from
927 * external programs over the bus.
929 struct ctrl_iface_dbus_priv *
930 wpa_supplicant_dbus_ctrl_iface_init(struct wpa_global *global)
932 struct ctrl_iface_dbus_priv *iface;
935 DBusObjectPathVTable wpas_vtable = {
936 NULL, &wpas_message_handler, NULL, NULL, NULL, NULL
939 iface = os_zalloc(sizeof(struct ctrl_iface_dbus_priv));
943 iface->global = global;
945 /* Get a reference to the system bus */
946 dbus_error_init(&error);
947 iface->con = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
948 dbus_error_free(&error);
950 perror("dbus_bus_get[ctrl_iface_dbus]");
951 wpa_printf(MSG_ERROR, "Could not acquire the system bus.");
955 /* Tell dbus about our mainloop integration functions */
956 if (integrate_with_eloop(iface->con, iface))
959 /* Register the message handler for the global dbus interface */
960 if (!dbus_connection_register_object_path(iface->con,
961 WPAS_DBUS_PATH, &wpas_vtable,
963 perror("dbus_connection_register_object_path[dbus]");
964 wpa_printf(MSG_ERROR, "Could not set up DBus message "
969 /* Register our service with the message bus */
970 dbus_error_init(&error);
971 switch (dbus_bus_request_name(iface->con, WPAS_DBUS_SERVICE,
973 case DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER:
976 case DBUS_REQUEST_NAME_REPLY_EXISTS:
977 case DBUS_REQUEST_NAME_REPLY_IN_QUEUE:
978 case DBUS_REQUEST_NAME_REPLY_ALREADY_OWNER:
979 perror("dbus_bus_request_name[dbus]");
980 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
981 "already registered.");
984 perror("dbus_bus_request_name[dbus]");
985 wpa_printf(MSG_ERROR, "Could not request DBus service name: "
986 "%s %s.", error.name, error.message);
989 dbus_error_free(&error);
994 wpa_printf(MSG_DEBUG, "Providing DBus service '" WPAS_DBUS_SERVICE
998 * Dispatch initial DBus messages that may have come in since the bus
999 * name was claimed above. Happens when clients are quick to notice the
1000 * wpa_supplicant service.
1002 * FIXME: is there a better solution to this problem?
1004 eloop_register_timeout(0, 50, dispatch_initial_dbus_messages,
1010 wpa_supplicant_dbus_ctrl_iface_deinit(iface);
1016 * wpa_supplicant_dbus_ctrl_iface_deinit - Deinitialize dbus ctrl interface
1017 * @iface: Pointer to dbus private data from
1018 * wpa_supplicant_dbus_ctrl_iface_init()
1020 * Deinitialize the dbus control interface that was initialized with
1021 * wpa_supplicant_dbus_ctrl_iface_init().
1023 void wpa_supplicant_dbus_ctrl_iface_deinit(struct ctrl_iface_dbus_priv *iface)
1029 eloop_cancel_timeout(dispatch_initial_dbus_messages,
1031 dbus_connection_set_watch_functions(iface->con, NULL, NULL,
1033 dbus_connection_set_timeout_functions(iface->con, NULL, NULL,
1035 dbus_connection_unref(iface->con);
1038 memset(iface, 0, sizeof(struct ctrl_iface_dbus_priv));
1044 * wpas_dbus_register_new_iface - Register a new interface with dbus
1045 * @wpa_s: %wpa_supplicant interface description structure to register
1046 * Returns: 0 on success, -1 on error
1048 * Registers a new interface with dbus and assigns it a dbus object path.
1050 int wpas_dbus_register_iface(struct wpa_supplicant *wpa_s)
1052 struct ctrl_iface_dbus_priv *ctrl_iface =
1053 wpa_s->global->dbus_ctrl_iface;
1054 DBusConnection * con;
1056 DBusObjectPathVTable vtable = {
1057 NULL, &wpas_iface_message_handler, NULL, NULL, NULL, NULL
1062 /* Do nothing if the control interface is not turned on */
1063 if (ctrl_iface == NULL)
1066 con = ctrl_iface->con;
1067 next = wpa_supplicant_dbus_next_objid(ctrl_iface);
1069 /* Create and set the interface's object path */
1070 path = os_zalloc(WPAS_DBUS_OBJECT_PATH_MAX);
1073 snprintf(path, WPAS_DBUS_OBJECT_PATH_MAX,
1074 WPAS_DBUS_PATH_INTERFACES "/%u",
1076 if (wpa_supplicant_set_dbus_path(wpa_s, path)) {
1077 wpa_printf(MSG_DEBUG,
1078 "Failed to set dbus path for interface %s",
1083 /* Register the message handler for the interface functions */
1084 if (!dbus_connection_register_fallback(con, path, &vtable, wpa_s)) {
1085 perror("wpas_dbus_register_iface [dbus]");
1086 wpa_printf(MSG_ERROR, "Could not set up DBus message "
1087 "handler for interface %s.", wpa_s->ifname);
1099 * wpas_dbus_unregister_iface - Unregister an interface from dbus
1100 * @wpa_s: wpa_supplicant interface structure
1101 * Returns: 0 on success, -1 on failure
1103 * Unregisters the interface with dbus
1105 int wpas_dbus_unregister_iface(struct wpa_supplicant *wpa_s)
1107 struct ctrl_iface_dbus_priv *ctrl_iface;
1108 DBusConnection *con;
1111 /* Do nothing if the control interface is not turned on */
1112 if (wpa_s == NULL || wpa_s->global == NULL)
1114 ctrl_iface = wpa_s->global->dbus_ctrl_iface;
1115 if (ctrl_iface == NULL)
1118 con = ctrl_iface->con;
1119 path = wpa_supplicant_get_dbus_path(wpa_s);
1121 if (!dbus_connection_unregister_object_path(con, path))
1124 free(wpa_s->dbus_path);
1125 wpa_s->dbus_path = NULL;
1132 * wpa_supplicant_get_iface_by_dbus_path - Get a new network interface
1133 * @global: Pointer to global data from wpa_supplicant_init()
1134 * @path: Pointer to a dbus object path representing an interface
1135 * Returns: Pointer to the interface or %NULL if not found
1137 struct wpa_supplicant * wpa_supplicant_get_iface_by_dbus_path(
1138 struct wpa_global *global, const char *path)
1140 struct wpa_supplicant *wpa_s;
1142 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
1143 if (strcmp(wpa_s->dbus_path, path) == 0)
1151 * wpa_supplicant_set_dbus_path - Assign a dbus path to an interface
1152 * @wpa_s: wpa_supplicant interface structure
1153 * @path: dbus path to set on the interface
1154 * Returns: 0 on succes, -1 on error
1156 int wpa_supplicant_set_dbus_path(struct wpa_supplicant *wpa_s,
1159 u32 len = strlen (path);
1160 if (len >= WPAS_DBUS_OBJECT_PATH_MAX)
1162 if (wpa_s->dbus_path)
1164 wpa_s->dbus_path = strdup(path);
1170 * wpa_supplicant_get_dbus_path - Get an interface's dbus path
1171 * @wpa_s: %wpa_supplicant interface structure
1172 * Returns: Interface's dbus object path, or %NULL on error
1174 const char * wpa_supplicant_get_dbus_path(struct wpa_supplicant *wpa_s)
1176 return wpa_s->dbus_path;