Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * binder interface for wpa_supplicant daemon |
| 3 | * Copyright (c) 2004-2016, Jouni Malinen <j@w1.fi> |
| 4 | * Copyright (c) 2004-2016, Roshan Pius <rpius@google.com> |
| 5 | * |
| 6 | * This software may be distributed under the terms of the BSD license. |
| 7 | * See README for more details. |
| 8 | */ |
| 9 | |
Roshan Pius | d385445 | 2016-07-07 16:46:41 -0700 | [diff] [blame] | 10 | #include "binder_manager.h" |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 11 | #include "iface.h" |
| 12 | |
| 13 | namespace wpa_supplicant_binder { |
| 14 | |
Roshan Pius | 54e763a | 2016-07-06 15:41:53 -0700 | [diff] [blame] | 15 | Iface::Iface(struct wpa_global *wpa_global, const char ifname[]) |
| 16 | : wpa_global_(wpa_global), ifname_(ifname) |
| 17 | { |
| 18 | } |
Dmitry Shmidt | e466304 | 2016-04-04 10:07:49 -0700 | [diff] [blame] | 19 | |
Roshan Pius | 54e763a | 2016-07-06 15:41:53 -0700 | [diff] [blame] | 20 | android::binder::Status Iface::GetName(std::string *iface_name_out) |
| 21 | { |
| 22 | // We could directly return the name we hold, but let's verify |
| 23 | // if the underlying iface still exists. |
| 24 | struct wpa_supplicant *wpa_s = retrieveIfacePtr(); |
| 25 | if (!wpa_s) { |
| 26 | return android::binder::Status::fromServiceSpecificError( |
Roshan Pius | d6e3751 | 2016-07-07 13:20:46 -0700 | [diff] [blame] | 27 | ERROR_IFACE_INVALID, |
Roshan Pius | 54e763a | 2016-07-06 15:41:53 -0700 | [diff] [blame] | 28 | "wpa_supplicant does not control this interface."); |
| 29 | } |
| 30 | |
| 31 | *iface_name_out = ifname_; |
| 32 | return android::binder::Status::ok(); |
| 33 | } |
| 34 | |
Roshan Pius | d6e3751 | 2016-07-07 13:20:46 -0700 | [diff] [blame] | 35 | android::binder::Status Iface::AddNetwork( |
| 36 | android::sp<fi::w1::wpa_supplicant::INetwork> *network_object_out) |
| 37 | { |
Roshan Pius | d385445 | 2016-07-07 16:46:41 -0700 | [diff] [blame] | 38 | struct wpa_supplicant *wpa_s = retrieveIfacePtr(); |
| 39 | if (!wpa_s) { |
| 40 | return android::binder::Status::fromServiceSpecificError( |
| 41 | ERROR_IFACE_INVALID, |
| 42 | "wpa_supplicant does not control this interface."); |
| 43 | } |
| 44 | |
| 45 | struct wpa_ssid *ssid = wpa_config_add_network(wpa_s->conf); |
| 46 | if (!ssid) { |
| 47 | return android::binder::Status::fromServiceSpecificError( |
| 48 | ERROR_GENERIC, "wpa_supplicant couldn't add this network."); |
| 49 | } |
| 50 | |
| 51 | // This sequence of steps after network addition is following what is |
| 52 | // currently being done in |ctrl_iface.c| & |dbus_new_handlers|. |
| 53 | // Notify the control interfaces about the network addition. |
| 54 | wpas_notify_network_added(wpa_s, ssid); |
| 55 | // Set the new network to be disabled. |
| 56 | ssid->disabled = 1; |
| 57 | // Set defaults for the new network. |
| 58 | wpa_config_set_network_defaults(ssid); |
| 59 | |
| 60 | BinderManager *binder_manager = BinderManager::getInstance(); |
| 61 | if (!binder_manager || |
| 62 | binder_manager->getNetworkBinderObjectByIfnameAndNetworkId( |
| 63 | wpa_s->ifname, ssid->id, network_object_out)) { |
| 64 | return android::binder::Status::fromServiceSpecificError( |
| 65 | ERROR_GENERIC, |
| 66 | "wpa_supplicant encountered a binder error."); |
| 67 | } |
Roshan Pius | d6e3751 | 2016-07-07 13:20:46 -0700 | [diff] [blame] | 68 | return android::binder::Status::ok(); |
| 69 | } |
| 70 | |
| 71 | android::binder::Status Iface::RemoveNetwork(int network_id) |
| 72 | { |
Roshan Pius | d385445 | 2016-07-07 16:46:41 -0700 | [diff] [blame] | 73 | struct wpa_supplicant *wpa_s = retrieveIfacePtr(); |
| 74 | if (!wpa_s) { |
| 75 | return android::binder::Status::fromServiceSpecificError( |
| 76 | ERROR_IFACE_INVALID, |
| 77 | "wpa_supplicant does not control this interface."); |
| 78 | } |
| 79 | |
| 80 | struct wpa_ssid *ssid = wpa_config_get_network(wpa_s->conf, network_id); |
| 81 | if (!ssid) { |
| 82 | return android::binder::Status::fromServiceSpecificError( |
| 83 | ERROR_NETWORK_UNKNOWN, |
| 84 | "wpa_supplicant does not control this network."); |
| 85 | } |
| 86 | if (wpa_config_remove_network(wpa_s->conf, network_id)) { |
| 87 | return android::binder::Status::fromServiceSpecificError( |
| 88 | ERROR_GENERIC, |
| 89 | "wpa_supplicant couldn't remove this network."); |
| 90 | } |
| 91 | return android::binder::Status::ok(); |
| 92 | } |
| 93 | |
| 94 | android::binder::Status Iface::GetNetwork( |
| 95 | int network_id, |
| 96 | android::sp<fi::w1::wpa_supplicant::INetwork> *network_object_out) |
| 97 | { |
| 98 | struct wpa_supplicant *wpa_s = retrieveIfacePtr(); |
| 99 | if (!wpa_s) { |
| 100 | return android::binder::Status::fromServiceSpecificError( |
| 101 | ERROR_IFACE_INVALID, |
| 102 | "wpa_supplicant does not control this interface."); |
| 103 | } |
| 104 | |
| 105 | struct wpa_ssid *ssid = wpa_config_get_network(wpa_s->conf, network_id); |
| 106 | if (!ssid) { |
| 107 | return android::binder::Status::fromServiceSpecificError( |
| 108 | ERROR_NETWORK_UNKNOWN, |
| 109 | "wpa_supplicant does not control this network."); |
| 110 | } |
| 111 | |
| 112 | BinderManager *binder_manager = BinderManager::getInstance(); |
| 113 | if (!binder_manager || |
| 114 | binder_manager->getNetworkBinderObjectByIfnameAndNetworkId( |
| 115 | wpa_s->ifname, ssid->id, network_object_out)) { |
| 116 | return android::binder::Status::fromServiceSpecificError( |
| 117 | ERROR_GENERIC, |
| 118 | "wpa_supplicant encountered a binder error."); |
| 119 | } |
Roshan Pius | d6e3751 | 2016-07-07 13:20:46 -0700 | [diff] [blame] | 120 | return android::binder::Status::ok(); |
| 121 | } |
| 122 | |
Roshan Pius | 0470cc8 | 2016-07-14 16:37:07 -0700 | [diff] [blame] | 123 | android::binder::Status Iface::RegisterCallback( |
| 124 | const android::sp<fi::w1::wpa_supplicant::IIfaceCallback> &callback) |
| 125 | { |
| 126 | struct wpa_supplicant *wpa_s = retrieveIfacePtr(); |
| 127 | if (!wpa_s) { |
| 128 | return android::binder::Status::fromServiceSpecificError( |
| 129 | ERROR_IFACE_INVALID, |
| 130 | "wpa_supplicant does not control this interface."); |
| 131 | } |
| 132 | BinderManager *binder_manager = BinderManager::getInstance(); |
| 133 | if (!binder_manager || |
| 134 | binder_manager->addIfaceCallbackBinderObject(ifname_, callback)) { |
| 135 | return android::binder::Status::fromServiceSpecificError( |
| 136 | ERROR_GENERIC, |
| 137 | "wpa_supplicant encountered a binder error."); |
| 138 | } |
| 139 | return android::binder::Status::ok(); |
| 140 | } |
| 141 | |
Roshan Pius | 54e763a | 2016-07-06 15:41:53 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Retrieve the underlying |wpa_supplicant| struct pointer for |
| 144 | * this iface. |
| 145 | * If the underlying iface is removed, then all RPC method calls |
| 146 | * on this object will return failure. |
| 147 | */ |
| 148 | wpa_supplicant *Iface::retrieveIfacePtr() |
| 149 | { |
| 150 | return wpa_supplicant_get_iface( |
| 151 | (struct wpa_global *)wpa_global_, ifname_.c_str()); |
| 152 | } |
| 153 | |
| 154 | } // namespace wpa_supplicant_binder |