Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 1 | /* |
| 2 | * hidl interface for wpa_hostapd daemon |
| 3 | * Copyright (c) 2004-2018, Jouni Malinen <j@w1.fi> |
| 4 | * Copyright (c) 2004-2018, 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 | */ |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 9 | #include <iomanip> |
| 10 | #include <sstream> |
| 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <android-base/file.h> |
| 15 | #include <android-base/stringprintf.h> |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 16 | |
| 17 | #include "hostapd.h" |
| 18 | #include "hidl_return_util.h" |
| 19 | |
Roshan Pius | 3455af4 | 2018-02-01 09:19:49 -0800 | [diff] [blame] | 20 | extern "C" |
| 21 | { |
| 22 | #include "utils/eloop.h" |
| 23 | } |
| 24 | |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 25 | // The HIDL implementation for hostapd creates a hostapd.conf dynamically for |
| 26 | // each interface. This file can then be used to hook onto the normal config |
| 27 | // file parsing logic in hostapd code. Helps us to avoid duplication of code |
| 28 | // in the HIDL interface. |
| 29 | // TOOD(b/71872409): Add unit tests for this. |
| 30 | namespace { |
| 31 | constexpr char kConfFileNameFmt[] = "/data/vendor/wifi/hostapd/hostapd_%s.conf"; |
| 32 | |
| 33 | using android::base::RemoveFileIfExists; |
| 34 | using android::base::StringPrintf; |
| 35 | using android::base::WriteStringToFile; |
| 36 | using android::hardware::wifi::hostapd::V1_0::IHostapd; |
| 37 | |
| 38 | std::string WriteHostapdConfig( |
| 39 | const std::string& interface_name, const std::string& config) |
| 40 | { |
| 41 | const std::string file_path = |
| 42 | StringPrintf(kConfFileNameFmt, interface_name.c_str()); |
| 43 | if (WriteStringToFile( |
| 44 | config, file_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP, |
| 45 | getuid(), getgid())) { |
| 46 | return file_path; |
| 47 | } |
| 48 | // Diagnose failure |
| 49 | int error = errno; |
| 50 | wpa_printf( |
| 51 | MSG_ERROR, "Cannot write hostapd config to %s, error: %s", |
| 52 | file_path.c_str(), strerror(error)); |
| 53 | struct stat st; |
| 54 | int result = stat(file_path.c_str(), &st); |
| 55 | if (result == 0) { |
| 56 | wpa_printf( |
| 57 | MSG_ERROR, "hostapd config file uid: %d, gid: %d, mode: %d", |
| 58 | st.st_uid, st.st_gid, st.st_mode); |
| 59 | } else { |
| 60 | wpa_printf( |
| 61 | MSG_ERROR, |
| 62 | "Error calling stat() on hostapd config file: %s", |
| 63 | strerror(errno)); |
| 64 | } |
| 65 | return ""; |
| 66 | } |
| 67 | |
| 68 | std::string CreateHostapdConfig( |
| 69 | const IHostapd::IfaceParams& iface_params, |
| 70 | const IHostapd::NetworkParams& nw_params) |
| 71 | { |
| 72 | if (nw_params.ssid.size() > |
| 73 | static_cast<uint32_t>( |
| 74 | IHostapd::ParamSizeLimits::SSID_MAX_LEN_IN_BYTES)) { |
| 75 | wpa_printf( |
| 76 | MSG_ERROR, "Invalid SSID size: %zu", nw_params.ssid.size()); |
| 77 | return ""; |
| 78 | } |
Roshan Pius | 0b8a64f | 2018-01-23 11:42:34 -0800 | [diff] [blame] | 79 | if ((nw_params.encryptionType != IHostapd::EncryptionType::NONE) && |
| 80 | (nw_params.pskPassphrase.size() < |
| 81 | static_cast<uint32_t>( |
| 82 | IHostapd::ParamSizeLimits:: |
| 83 | WPA2_PSK_PASSPHRASE_MIN_LEN_IN_BYTES) || |
| 84 | nw_params.pskPassphrase.size() > |
| 85 | static_cast<uint32_t>( |
| 86 | IHostapd::ParamSizeLimits:: |
| 87 | WPA2_PSK_PASSPHRASE_MAX_LEN_IN_BYTES))) { |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 88 | wpa_printf( |
| 89 | MSG_ERROR, "Invalid psk passphrase size: %zu", |
| 90 | nw_params.pskPassphrase.size()); |
| 91 | return ""; |
| 92 | } |
| 93 | |
| 94 | // SSID string |
| 95 | std::stringstream ss; |
| 96 | ss << std::hex; |
| 97 | ss << std::setfill('0'); |
| 98 | for (uint8_t b : nw_params.ssid) { |
| 99 | ss << std::setw(2) << static_cast<unsigned int>(b); |
| 100 | } |
| 101 | const std::string ssid_as_string = ss.str(); |
| 102 | |
| 103 | // Encryption config string |
| 104 | std::string encryption_config_as_string; |
| 105 | switch (nw_params.encryptionType) { |
| 106 | case IHostapd::EncryptionType::NONE: |
| 107 | // no security params |
| 108 | break; |
| 109 | case IHostapd::EncryptionType::WPA: |
| 110 | encryption_config_as_string = StringPrintf( |
| 111 | "wpa=3\n" |
| 112 | "wpa_pairwise=TKIP CCMP\n" |
| 113 | "wpa_passphrase=%s", |
| 114 | nw_params.pskPassphrase.c_str()); |
| 115 | break; |
| 116 | case IHostapd::EncryptionType::WPA2: |
| 117 | encryption_config_as_string = StringPrintf( |
| 118 | "wpa=2\n" |
| 119 | "rsn_pairwise=CCMP\n" |
| 120 | "wpa_passphrase=%s", |
| 121 | nw_params.pskPassphrase.c_str()); |
| 122 | break; |
| 123 | default: |
| 124 | wpa_printf(MSG_ERROR, "Unknown encryption type"); |
| 125 | return ""; |
| 126 | } |
| 127 | |
| 128 | std::string channel_config_as_string; |
| 129 | if (iface_params.channelParams.enableAcs) { |
| 130 | channel_config_as_string = StringPrintf( |
| 131 | "channel=0\n" |
| 132 | "acs_exclude_dfs=%d", |
| 133 | iface_params.channelParams.acsShouldExcludeDfs); |
| 134 | } else { |
| 135 | channel_config_as_string = StringPrintf( |
| 136 | "channel=%d", iface_params.channelParams.channel); |
| 137 | } |
| 138 | |
| 139 | // Hw Mode String |
| 140 | std::string hw_mode_as_string; |
| 141 | switch (iface_params.channelParams.band) { |
| 142 | case IHostapd::Band::BAND_2_4_GHZ: |
Roshan Pius | e453f71 | 2018-02-09 15:49:57 -0800 | [diff] [blame] | 143 | hw_mode_as_string = "hw_mode=g"; |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 144 | break; |
| 145 | case IHostapd::Band::BAND_5_GHZ: |
Roshan Pius | e453f71 | 2018-02-09 15:49:57 -0800 | [diff] [blame] | 146 | hw_mode_as_string = "hw_mode=a\n" |
| 147 | "ht_capab=[HT40+]\n" |
| 148 | "vht_oper_chwidth=1"; |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 149 | break; |
| 150 | case IHostapd::Band::BAND_ANY: |
Roshan Pius | e453f71 | 2018-02-09 15:49:57 -0800 | [diff] [blame] | 151 | hw_mode_as_string = "hw_mode=any\n" |
| 152 | "ht_capab=[HT40+]\n" |
| 153 | "vht_oper_chwidth=1"; |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 154 | break; |
| 155 | default: |
| 156 | wpa_printf(MSG_ERROR, "Invalid band"); |
| 157 | return ""; |
| 158 | } |
| 159 | |
| 160 | return StringPrintf( |
| 161 | "interface=%s\n" |
| 162 | "driver=nl80211\n" |
Daichi Ueura | caa74a8 | 2018-02-14 22:25:39 +0900 | [diff] [blame^] | 163 | "ctrl_interface=/data/vendor/wifi/hostapd/ctrl\n" |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 164 | // ssid2 signals to hostapd that the value is not a literal value |
| 165 | // for use as a SSID. In this case, we're giving it a hex |
| 166 | // std::string and hostapd needs to expect that. |
| 167 | "ssid2=%s\n" |
| 168 | "%s\n" |
| 169 | "ieee80211n=%d\n" |
| 170 | "ieee80211ac=%d\n" |
Roshan Pius | e453f71 | 2018-02-09 15:49:57 -0800 | [diff] [blame] | 171 | "%s\n" |
Roshan Pius | 30b452e | 2017-12-27 13:36:21 -0800 | [diff] [blame] | 172 | "ignore_broadcast_ssid=%d\n" |
| 173 | "wowlan_triggers=any\n" |
| 174 | "%s\n", |
| 175 | iface_params.ifaceName.c_str(), ssid_as_string.c_str(), |
| 176 | channel_config_as_string.c_str(), |
| 177 | iface_params.hwModeParams.enable80211N ? 1 : 0, |
| 178 | iface_params.hwModeParams.enable80211AC ? 1 : 0, |
| 179 | hw_mode_as_string.c_str(), nw_params.isHidden ? 1 : 0, |
| 180 | encryption_config_as_string.c_str()); |
| 181 | } |
| 182 | } // namespace |
| 183 | |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 184 | namespace android { |
| 185 | namespace hardware { |
| 186 | namespace wifi { |
| 187 | namespace hostapd { |
| 188 | namespace V1_0 { |
| 189 | namespace implementation { |
| 190 | using hidl_return_util::call; |
| 191 | |
| 192 | Hostapd::Hostapd(struct hapd_interfaces* interfaces) : interfaces_(interfaces) |
| 193 | {} |
| 194 | |
| 195 | Return<void> Hostapd::addAccessPoint( |
| 196 | const IfaceParams& iface_params, const NetworkParams& nw_params, |
| 197 | addAccessPoint_cb _hidl_cb) |
| 198 | { |
| 199 | return call( |
| 200 | this, &Hostapd::addAccessPointInternal, _hidl_cb, iface_params, |
| 201 | nw_params); |
| 202 | } |
| 203 | |
| 204 | Return<void> Hostapd::removeAccessPoint( |
| 205 | const hidl_string& iface_name, removeAccessPoint_cb _hidl_cb) |
| 206 | { |
| 207 | return call( |
| 208 | this, &Hostapd::removeAccessPointInternal, _hidl_cb, iface_name); |
| 209 | } |
| 210 | |
Roshan Pius | 3455af4 | 2018-02-01 09:19:49 -0800 | [diff] [blame] | 211 | Return<void> Hostapd::terminate() { |
| 212 | wpa_printf(MSG_INFO, "Terminating..."); |
| 213 | eloop_terminate(); |
| 214 | return Void(); |
| 215 | } |
| 216 | |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 217 | HostapdStatus Hostapd::addAccessPointInternal( |
| 218 | const IfaceParams& iface_params, const NetworkParams& nw_params) |
| 219 | { |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 220 | if (hostapd_get_iface(interfaces_, iface_params.ifaceName.c_str())) { |
| 221 | wpa_printf( |
| 222 | MSG_ERROR, "Interface %s already present", |
| 223 | iface_params.ifaceName.c_str()); |
| 224 | return {HostapdStatusCode::FAILURE_IFACE_EXISTS, ""}; |
| 225 | } |
| 226 | const auto conf_params = CreateHostapdConfig(iface_params, nw_params); |
| 227 | if (conf_params.empty()) { |
| 228 | wpa_printf(MSG_ERROR, "Failed to create config params"); |
| 229 | return {HostapdStatusCode::FAILURE_ARGS_INVALID, ""}; |
| 230 | } |
| 231 | const auto conf_file_path = |
| 232 | WriteHostapdConfig(iface_params.ifaceName, conf_params); |
| 233 | if (conf_file_path.empty()) { |
| 234 | wpa_printf(MSG_ERROR, "Failed to write config file"); |
| 235 | return {HostapdStatusCode::FAILURE_UNKNOWN, ""}; |
| 236 | } |
| 237 | std::string add_iface_param_str = StringPrintf( |
| 238 | "%s config=%s", iface_params.ifaceName.c_str(), |
| 239 | conf_file_path.c_str()); |
| 240 | std::vector<char> add_iface_param_vec( |
| 241 | add_iface_param_str.begin(), add_iface_param_str.end() + 1); |
| 242 | if (hostapd_add_iface(interfaces_, add_iface_param_vec.data()) < 0) { |
| 243 | wpa_printf( |
| 244 | MSG_ERROR, "Adding interface %s failed", |
| 245 | add_iface_param_str.c_str()); |
| 246 | return {HostapdStatusCode::FAILURE_UNKNOWN, ""}; |
| 247 | } |
| 248 | struct hostapd_data* iface_hapd = |
| 249 | hostapd_get_iface(interfaces_, iface_params.ifaceName.c_str()); |
| 250 | WPA_ASSERT(iface_hapd != nullptr && iface_hapd->iface != nullptr); |
| 251 | if (hostapd_enable_iface(iface_hapd->iface) < 0) { |
| 252 | wpa_printf( |
| 253 | MSG_ERROR, "Enabling interface %s failed", |
| 254 | iface_params.ifaceName.c_str()); |
| 255 | return {HostapdStatusCode::FAILURE_UNKNOWN, ""}; |
| 256 | } |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 257 | return {HostapdStatusCode::SUCCESS, ""}; |
| 258 | } |
| 259 | |
| 260 | HostapdStatus Hostapd::removeAccessPointInternal(const std::string& iface_name) |
| 261 | { |
Roshan Pius | 087d869 | 2017-12-27 14:11:44 -0800 | [diff] [blame] | 262 | std::vector<char> remove_iface_param_vec( |
| 263 | iface_name.begin(), iface_name.end() + 1); |
| 264 | if (hostapd_remove_iface(interfaces_, remove_iface_param_vec.data()) < |
| 265 | 0) { |
| 266 | wpa_printf( |
| 267 | MSG_ERROR, "Removing interface %s failed", |
| 268 | iface_name.c_str()); |
| 269 | return {HostapdStatusCode::FAILURE_UNKNOWN, ""}; |
| 270 | } |
Roshan Pius | cc81756 | 2017-12-22 14:45:05 -0800 | [diff] [blame] | 271 | return {HostapdStatusCode::SUCCESS, ""}; |
| 272 | } |
| 273 | } // namespace implementation |
| 274 | } // namespace V1_0 |
| 275 | } // namespace hostapd |
| 276 | } // namespace wifi |
| 277 | } // namespace hardware |
| 278 | } // namespace android |