server.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. * Copyright (C) 2016 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "wificond/server.h"
  17. #include <sstream>
  18. #include <android-base/file.h>
  19. #include <android-base/logging.h>
  20. #include <android-base/strings.h>
  21. #include <binder/IPCThreadState.h>
  22. #include <binder/PermissionCache.h>
  23. #include "wificond/logging_utils.h"
  24. #include "wificond/net/netlink_utils.h"
  25. #include "wificond/scanning/scan_utils.h"
  26. using android::base::WriteStringToFd;
  27. using android::binder::Status;
  28. using android::sp;
  29. using android::IBinder;
  30. using android::net::wifi::IApInterface;
  31. using android::net::wifi::IClientInterface;
  32. using android::net::wifi::IInterfaceEventCallback;
  33. using android::wifi_system::InterfaceTool;
  34. using std::endl;
  35. using std::placeholders::_1;
  36. using std::string;
  37. using std::stringstream;
  38. using std::unique_ptr;
  39. using std::vector;
  40. namespace android {
  41. namespace wificond {
  42. namespace {
  43. constexpr const char* kPermissionDump = "android.permission.DUMP";
  44. } // namespace
  45. Server::Server(unique_ptr<InterfaceTool> if_tool,
  46. NetlinkUtils* netlink_utils,
  47. ScanUtils* scan_utils)
  48. : if_tool_(std::move(if_tool)),
  49. netlink_utils_(netlink_utils),
  50. scan_utils_(scan_utils) {
  51. }
  52. Status Server::RegisterCallback(const sp<IInterfaceEventCallback>& callback) {
  53. for (auto& it : interface_event_callbacks_) {
  54. if (IInterface::asBinder(callback) == IInterface::asBinder(it)) {
  55. LOG(WARNING) << "Ignore duplicate interface event callback registration";
  56. return Status::ok();
  57. }
  58. }
  59. LOG(INFO) << "New interface event callback registered";
  60. interface_event_callbacks_.push_back(callback);
  61. return Status::ok();
  62. }
  63. Status Server::UnregisterCallback(const sp<IInterfaceEventCallback>& callback) {
  64. for (auto it = interface_event_callbacks_.begin();
  65. it != interface_event_callbacks_.end();
  66. it++) {
  67. if (IInterface::asBinder(callback) == IInterface::asBinder(*it)) {
  68. interface_event_callbacks_.erase(it);
  69. LOG(INFO) << "Unregister interface event callback";
  70. return Status::ok();
  71. }
  72. }
  73. LOG(WARNING) << "Failed to find registered interface event callback"
  74. << " to unregister";
  75. return Status::ok();
  76. }
  77. Status Server::createApInterface(const std::string& iface_name,
  78. sp<IApInterface>* created_interface) {
  79. InterfaceInfo interface;
  80. if (!SetupInterface(iface_name, &interface)) {
  81. return Status::ok(); // Logging was done internally
  82. }
  83. unique_ptr<ApInterfaceImpl> ap_interface(new ApInterfaceImpl(
  84. interface.name,
  85. interface.index,
  86. netlink_utils_,
  87. if_tool_.get()));
  88. *created_interface = ap_interface->GetBinder();
  89. BroadcastApInterfaceReady(ap_interface->GetBinder());
  90. ap_interfaces_[iface_name] = std::move(ap_interface);
  91. return Status::ok();
  92. }
  93. Status Server::tearDownApInterface(const std::string& iface_name,
  94. bool* out_success) {
  95. *out_success = false;
  96. const auto iter = ap_interfaces_.find(iface_name);
  97. if (iter != ap_interfaces_.end()) {
  98. BroadcastApInterfaceTornDown(iter->second->GetBinder());
  99. ap_interfaces_.erase(iter);
  100. *out_success = true;
  101. }
  102. return Status::ok();
  103. }
  104. Status Server::createClientInterface(const std::string& iface_name,
  105. sp<IClientInterface>* created_interface) {
  106. InterfaceInfo interface;
  107. if (!SetupInterface(iface_name, &interface)) {
  108. return Status::ok(); // Logging was done internally
  109. }
  110. unique_ptr<ClientInterfaceImpl> client_interface(new ClientInterfaceImpl(
  111. wiphy_index_,
  112. interface.name,
  113. interface.index,
  114. interface.mac_address,
  115. if_tool_.get(),
  116. netlink_utils_,
  117. scan_utils_));
  118. *created_interface = client_interface->GetBinder();
  119. BroadcastClientInterfaceReady(client_interface->GetBinder());
  120. client_interfaces_[iface_name] = std::move(client_interface);
  121. return Status::ok();
  122. }
  123. Status Server::tearDownClientInterface(const std::string& iface_name,
  124. bool* out_success) {
  125. *out_success = false;
  126. const auto iter = client_interfaces_.find(iface_name);
  127. if (iter != client_interfaces_.end()) {
  128. BroadcastClientInterfaceTornDown(iter->second->GetBinder());
  129. client_interfaces_.erase(iter);
  130. *out_success = true;
  131. }
  132. return Status::ok();
  133. }
  134. Status Server::tearDownInterfaces() {
  135. for (auto& it : client_interfaces_) {
  136. BroadcastClientInterfaceTornDown(it.second->GetBinder());
  137. }
  138. client_interfaces_.clear();
  139. for (auto& it : ap_interfaces_) {
  140. BroadcastApInterfaceTornDown(it.second->GetBinder());
  141. }
  142. ap_interfaces_.clear();
  143. MarkDownAllInterfaces();
  144. netlink_utils_->UnsubscribeRegDomainChange(wiphy_index_);
  145. return Status::ok();
  146. }
  147. Status Server::GetClientInterfaces(vector<sp<IBinder>>* out_client_interfaces) {
  148. vector<sp<android::IBinder>> client_interfaces_binder;
  149. for (auto& it : client_interfaces_) {
  150. out_client_interfaces->push_back(asBinder(it.second->GetBinder()));
  151. }
  152. return binder::Status::ok();
  153. }
  154. Status Server::GetApInterfaces(vector<sp<IBinder>>* out_ap_interfaces) {
  155. vector<sp<IBinder>> ap_interfaces_binder;
  156. for (auto& it : ap_interfaces_) {
  157. out_ap_interfaces->push_back(asBinder(it.second->GetBinder()));
  158. }
  159. return binder::Status::ok();
  160. }
  161. status_t Server::dump(int fd, const Vector<String16>& /*args*/) {
  162. if (!PermissionCache::checkCallingPermission(String16(kPermissionDump))) {
  163. IPCThreadState* ipc = android::IPCThreadState::self();
  164. LOG(ERROR) << "Caller (uid: " << ipc->getCallingUid()
  165. << ") is not permitted to dump wificond state";
  166. return PERMISSION_DENIED;
  167. }
  168. stringstream ss;
  169. ss << "Current wiphy index: " << wiphy_index_ << endl;
  170. ss << "Cached interfaces list from kernel message: " << endl;
  171. for (const auto& iface : interfaces_) {
  172. ss << "Interface index: " << iface.index
  173. << ", name: " << iface.name
  174. << ", mac address: "
  175. << LoggingUtils::GetMacString(iface.mac_address) << endl;
  176. }
  177. string country_code;
  178. if (netlink_utils_->GetCountryCode(&country_code)) {
  179. ss << "Current country code from kernel: " << country_code << endl;
  180. } else {
  181. ss << "Failed to get country code from kernel." << endl;
  182. }
  183. for (const auto& iface : client_interfaces_) {
  184. iface.second->Dump(&ss);
  185. }
  186. for (const auto& iface : ap_interfaces_) {
  187. iface.second->Dump(&ss);
  188. }
  189. if (!WriteStringToFd(ss.str(), fd)) {
  190. PLOG(ERROR) << "Failed to dump state to fd " << fd;
  191. return FAILED_TRANSACTION;
  192. }
  193. return OK;
  194. }
  195. void Server::MarkDownAllInterfaces() {
  196. uint32_t wiphy_index;
  197. vector<InterfaceInfo> interfaces;
  198. if (netlink_utils_->GetWiphyIndex(&wiphy_index) &&
  199. netlink_utils_->GetInterfaces(wiphy_index, &interfaces)) {
  200. for (InterfaceInfo& interface : interfaces) {
  201. if_tool_->SetUpState(interface.name.c_str(), false);
  202. }
  203. }
  204. }
  205. Status Server::getAvailable2gChannels(
  206. std::unique_ptr<vector<int32_t>>* out_frequencies) {
  207. BandInfo band_info;
  208. ScanCapabilities scan_capabilities_ignored;
  209. WiphyFeatures wiphy_features_ignored;
  210. if (!netlink_utils_->GetWiphyInfo(wiphy_index_, &band_info,
  211. &scan_capabilities_ignored,
  212. &wiphy_features_ignored)) {
  213. LOG(ERROR) << "Failed to get wiphy info from kernel";
  214. out_frequencies->reset(nullptr);
  215. return Status::ok();
  216. }
  217. out_frequencies->reset(
  218. new vector<int32_t>(band_info.band_2g.begin(), band_info.band_2g.end()));
  219. return Status::ok();
  220. }
  221. Status Server::getAvailable5gNonDFSChannels(
  222. std::unique_ptr<vector<int32_t>>* out_frequencies) {
  223. BandInfo band_info;
  224. ScanCapabilities scan_capabilities_ignored;
  225. WiphyFeatures wiphy_features_ignored;
  226. if (!netlink_utils_->GetWiphyInfo(wiphy_index_, &band_info,
  227. &scan_capabilities_ignored,
  228. &wiphy_features_ignored)) {
  229. LOG(ERROR) << "Failed to get wiphy info from kernel";
  230. out_frequencies->reset(nullptr);
  231. return Status::ok();
  232. }
  233. out_frequencies->reset(
  234. new vector<int32_t>(band_info.band_5g.begin(), band_info.band_5g.end()));
  235. return Status::ok();
  236. }
  237. Status Server::getAvailableDFSChannels(
  238. std::unique_ptr<vector<int32_t>>* out_frequencies) {
  239. BandInfo band_info;
  240. ScanCapabilities scan_capabilities_ignored;
  241. WiphyFeatures wiphy_features_ignored;
  242. if (!netlink_utils_->GetWiphyInfo(wiphy_index_, &band_info,
  243. &scan_capabilities_ignored,
  244. &wiphy_features_ignored)) {
  245. LOG(ERROR) << "Failed to get wiphy info from kernel";
  246. out_frequencies->reset(nullptr);
  247. return Status::ok();
  248. }
  249. out_frequencies->reset(new vector<int32_t>(band_info.band_dfs.begin(),
  250. band_info.band_dfs.end()));
  251. return Status::ok();
  252. }
  253. bool Server::SetupInterface(const std::string& iface_name,
  254. InterfaceInfo* interface) {
  255. if (!RefreshWiphyIndex(iface_name)) {
  256. return false;
  257. }
  258. netlink_utils_->SubscribeRegDomainChange(
  259. wiphy_index_,
  260. std::bind(&Server::OnRegDomainChanged,
  261. this,
  262. _1));
  263. interfaces_.clear();
  264. if (!netlink_utils_->GetInterfaces(wiphy_index_, &interfaces_)) {
  265. LOG(ERROR) << "Failed to get interfaces info from kernel";
  266. return false;
  267. }
  268. for (const auto& iface : interfaces_) {
  269. if (iface.name == iface_name) {
  270. *interface = iface;
  271. return true;
  272. }
  273. }
  274. LOG(ERROR) << "No usable interface found";
  275. return false;
  276. }
  277. bool Server::RefreshWiphyIndex(const std::string& iface_name) {
  278. if (!netlink_utils_->GetWiphyIndex(&wiphy_index_, iface_name)) {
  279. LOG(ERROR) << "Failed to get wiphy index";
  280. return false;
  281. }
  282. return true;
  283. }
  284. void Server::OnRegDomainChanged(std::string& country_code) {
  285. if (country_code.empty()) {
  286. LOG(INFO) << "Regulatory domain changed";
  287. } else {
  288. LOG(INFO) << "Regulatory domain changed to country: " << country_code;
  289. }
  290. LogSupportedBands();
  291. }
  292. void Server::LogSupportedBands() {
  293. BandInfo band_info;
  294. ScanCapabilities scan_capabilities;
  295. WiphyFeatures wiphy_features;
  296. netlink_utils_->GetWiphyInfo(wiphy_index_,
  297. &band_info,
  298. &scan_capabilities,
  299. &wiphy_features);
  300. stringstream ss;
  301. for (unsigned int i = 0; i < band_info.band_2g.size(); i++) {
  302. ss << " " << band_info.band_2g[i];
  303. }
  304. LOG(INFO) << "2.4Ghz frequencies:"<< ss.str();
  305. ss.str("");
  306. for (unsigned int i = 0; i < band_info.band_5g.size(); i++) {
  307. ss << " " << band_info.band_5g[i];
  308. }
  309. LOG(INFO) << "5Ghz non-DFS frequencies:"<< ss.str();
  310. ss.str("");
  311. for (unsigned int i = 0; i < band_info.band_dfs.size(); i++) {
  312. ss << " " << band_info.band_dfs[i];
  313. }
  314. LOG(INFO) << "5Ghz DFS frequencies:"<< ss.str();
  315. }
  316. void Server::BroadcastClientInterfaceReady(
  317. sp<IClientInterface> network_interface) {
  318. for (auto& it : interface_event_callbacks_) {
  319. it->OnClientInterfaceReady(network_interface);
  320. }
  321. }
  322. void Server::BroadcastApInterfaceReady(
  323. sp<IApInterface> network_interface) {
  324. for (auto& it : interface_event_callbacks_) {
  325. it->OnApInterfaceReady(network_interface);
  326. }
  327. }
  328. void Server::BroadcastClientInterfaceTornDown(
  329. sp<IClientInterface> network_interface) {
  330. for (auto& it : interface_event_callbacks_) {
  331. it->OnClientTorndownEvent(network_interface);
  332. }
  333. }
  334. void Server::BroadcastApInterfaceTornDown(
  335. sp<IApInterface> network_interface) {
  336. for (auto& it : interface_event_callbacks_) {
  337. it->OnApTorndownEvent(network_interface);
  338. }
  339. }
  340. } // namespace wificond
  341. } // namespace android