2 * Copyright (C) 2016 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "wificond/server.h"
21 #include <android-base/file.h>
22 #include <android-base/logging.h>
23 #include <android-base/strings.h>
24 #include <binder/IPCThreadState.h>
25 #include <binder/PermissionCache.h>
27 #include "wificond/logging_utils.h"
28 #include "wificond/net/netlink_utils.h"
29 #include "wificond/scanning/scan_utils.h"
31 using android::base::WriteStringToFd;
32 using android::binder::Status;
34 using android::IBinder;
35 using android::net::wifi::IApInterface;
36 using android::net::wifi::IClientInterface;
37 using android::net::wifi::IInterfaceEventCallback;
38 using android::net::wifi::IRttClient;
39 using android::net::wifi::IRttController;
40 using android::wifi_system::HostapdManager;
41 using android::wifi_system::InterfaceTool;
42 using android::wifi_system::SupplicantManager;
45 using std::placeholders::_1;
47 using std::stringstream;
48 using std::unique_ptr;
56 constexpr const char* kPermissionDump = "android.permission.DUMP";
60 Server::Server(unique_ptr<InterfaceTool> if_tool,
61 unique_ptr<SupplicantManager> supplicant_manager,
62 unique_ptr<HostapdManager> hostapd_manager,
63 NetlinkUtils* netlink_utils,
64 ScanUtils* scan_utils)
65 : if_tool_(std::move(if_tool)),
66 supplicant_manager_(std::move(supplicant_manager)),
67 hostapd_manager_(std::move(hostapd_manager)),
68 netlink_utils_(netlink_utils),
69 scan_utils_(scan_utils) {
72 Status Server::RegisterCallback(const sp<IInterfaceEventCallback>& callback) {
73 for (auto& it : interface_event_callbacks_) {
74 if (IInterface::asBinder(callback) == IInterface::asBinder(it)) {
75 LOG(WARNING) << "Ignore duplicate interface event callback registration";
79 LOG(INFO) << "New interface event callback registered";
80 interface_event_callbacks_.push_back(callback);
84 Status Server::UnregisterCallback(const sp<IInterfaceEventCallback>& callback) {
85 for (auto it = interface_event_callbacks_.begin();
86 it != interface_event_callbacks_.end();
88 if (IInterface::asBinder(callback) == IInterface::asBinder(*it)) {
89 interface_event_callbacks_.erase(it);
90 LOG(INFO) << "Unregister interface event callback";
94 LOG(WARNING) << "Failed to find registered interface event callback"
99 Status Server::registerRttClient(const sp<IRttClient>& rtt_client,
100 sp<IRttController>* out_rtt_controller) {
101 if (rtt_controller_ == nullptr) {
102 rtt_controller_.reset(new RttControllerImpl());
104 rtt_controller_->RegisterRttClient(rtt_client);
106 *out_rtt_controller = rtt_controller_->GetBinder();
110 Status Server::unregisterRttClient(const sp<IRttClient>& rttClient) {
111 rtt_controller_->UnregisterRttClient(rttClient);
112 if (rtt_controller_->GetClientCount() == 0) {
113 rtt_controller_.reset();
118 Status Server::createApInterface(sp<IApInterface>* created_interface) {
119 InterfaceInfo interface;
120 if (!SetupInterface(&interface)) {
121 return Status::ok(); // Logging was done internally
124 unique_ptr<ApInterfaceImpl> ap_interface(new ApInterfaceImpl(
129 hostapd_manager_.get()));
130 *created_interface = ap_interface->GetBinder();
131 ap_interfaces_.push_back(std::move(ap_interface));
132 BroadcastApInterfaceReady(ap_interfaces_.back()->GetBinder());
137 Status Server::createClientInterface(sp<IClientInterface>* created_interface) {
138 InterfaceInfo interface;
139 if (!SetupInterface(&interface)) {
140 return Status::ok(); // Logging was done internally
143 unique_ptr<ClientInterfaceImpl> client_interface(new ClientInterfaceImpl(
147 interface.mac_address,
149 supplicant_manager_.get(),
152 *created_interface = client_interface->GetBinder();
153 client_interfaces_.push_back(std::move(client_interface));
154 BroadcastClientInterfaceReady(client_interfaces_.back()->GetBinder());
159 Status Server::tearDownInterfaces() {
160 for (auto& it : client_interfaces_) {
161 BroadcastClientInterfaceTornDown(it->GetBinder());
163 client_interfaces_.clear();
165 for (auto& it : ap_interfaces_) {
166 BroadcastApInterfaceTornDown(it->GetBinder());
168 ap_interfaces_.clear();
170 MarkDownAllInterfaces();
172 netlink_utils_->UnsubscribeRegDomainChange(wiphy_index_);
177 Status Server::GetClientInterfaces(vector<sp<IBinder>>* out_client_interfaces) {
178 vector<sp<android::IBinder>> client_interfaces_binder;
179 for (auto& it : client_interfaces_) {
180 out_client_interfaces->push_back(asBinder(it->GetBinder()));
182 return binder::Status::ok();
185 Status Server::GetApInterfaces(vector<sp<IBinder>>* out_ap_interfaces) {
186 vector<sp<IBinder>> ap_interfaces_binder;
187 for (auto& it : ap_interfaces_) {
188 out_ap_interfaces->push_back(asBinder(it->GetBinder()));
190 return binder::Status::ok();
193 status_t Server::dump(int fd, const Vector<String16>& /*args*/) {
194 if (!PermissionCache::checkCallingPermission(String16(kPermissionDump))) {
195 IPCThreadState* ipc = android::IPCThreadState::self();
196 LOG(ERROR) << "Caller (uid: " << ipc->getCallingUid()
197 << ") is not permitted to dump wificond state";
198 return PERMISSION_DENIED;
202 ss << "Current wiphy index: " << wiphy_index_ << endl;
203 ss << "Cached interfaces list from kernel message: " << endl;
204 for (const auto& iface : interfaces_) {
205 ss << "Interface index: " << iface.index
206 << ", name: " << iface.name
208 << LoggingUtils::GetMacString(iface.mac_address) << endl;
211 for (const auto& iface : client_interfaces_) {
215 for (const auto& iface : ap_interfaces_) {
219 if (!WriteStringToFd(ss.str(), fd)) {
220 PLOG(ERROR) << "Failed to dump state to fd " << fd;
221 return FAILED_TRANSACTION;
227 void Server::MarkDownAllInterfaces() {
228 uint32_t wiphy_index;
229 vector<InterfaceInfo> interfaces;
230 if (netlink_utils_->GetWiphyIndex(&wiphy_index) &&
231 netlink_utils_->GetInterfaces(wiphy_index, &interfaces)) {
232 for (InterfaceInfo& interface : interfaces) {
233 if_tool_->SetUpState(interface.name.c_str(), false);
238 void Server::CleanUpSystemState() {
239 supplicant_manager_->StopSupplicant();
240 hostapd_manager_->StopHostapd();
241 MarkDownAllInterfaces();
244 bool Server::SetupInterface(InterfaceInfo* interface) {
245 if (!ap_interfaces_.empty() || !client_interfaces_.empty()) {
246 // In the future we may support multiple interfaces at once. However,
247 // today, we support just one.
248 LOG(ERROR) << "Cannot create AP interface when other interfaces exist";
252 if (!RefreshWiphyIndex()) {
256 netlink_utils_->SubscribeRegDomainChange(
258 std::bind(&Server::OnRegDomainChanged,
263 if (!netlink_utils_->GetInterfaces(wiphy_index_, &interfaces_)) {
264 LOG(ERROR) << "Failed to get interfaces info from kernel";
268 for (const auto& iface : interfaces_) {
269 // Some kernel/driver uses station type for p2p interface.
270 // In that case we can only rely on hard-coded name to exclude
271 // p2p interface from station interfaces.
272 // Currently NAN interfaces also use station type.
273 // We should blacklist NAN interfaces as well.
274 if (iface.name != "p2p0" &&
275 !android::base::StartsWith(iface.name, "aware_data")) {
281 LOG(ERROR) << "No usable interface found";
285 bool Server::RefreshWiphyIndex() {
286 if (!netlink_utils_->GetWiphyIndex(&wiphy_index_)) {
287 LOG(ERROR) << "Failed to get wiphy index";
293 void Server::OnRegDomainChanged(std::string& country_code) {
294 if (country_code.empty()) {
295 LOG(INFO) << "Regulatory domain changed";
297 LOG(INFO) << "Regulatory domain changed to country: " << country_code;
302 void Server::LogSupportedBands() {
304 ScanCapabilities scan_capabilities;
305 WiphyFeatures wiphy_features;
306 netlink_utils_->GetWiphyInfo(wiphy_index_,
312 for (unsigned int i = 0; i < band_info.band_2g.size(); i++) {
313 ss << " " << band_info.band_2g[i];
315 LOG(INFO) << "2.4Ghz frequencies:"<< ss.str();
318 for (unsigned int i = 0; i < band_info.band_5g.size(); i++) {
319 ss << " " << band_info.band_5g[i];
321 LOG(INFO) << "5Ghz non-DFS frequencies:"<< ss.str();
324 for (unsigned int i = 0; i < band_info.band_dfs.size(); i++) {
325 ss << " " << band_info.band_dfs[i];
327 LOG(INFO) << "5Ghz DFS frequencies:"<< ss.str();
330 void Server::BroadcastClientInterfaceReady(
331 sp<IClientInterface> network_interface) {
332 for (auto& it : interface_event_callbacks_) {
333 it->OnClientInterfaceReady(network_interface);
337 void Server::BroadcastApInterfaceReady(
338 sp<IApInterface> network_interface) {
339 for (auto& it : interface_event_callbacks_) {
340 it->OnApInterfaceReady(network_interface);
344 void Server::BroadcastClientInterfaceTornDown(
345 sp<IClientInterface> network_interface) {
346 for (auto& it : interface_event_callbacks_) {
347 it->OnClientTorndownEvent(network_interface);
351 void Server::BroadcastApInterfaceTornDown(
352 sp<IApInterface> network_interface) {
353 for (auto& it : interface_event_callbacks_) {
354 it->OnApTorndownEvent(network_interface);
358 } // namespace wificond
359 } // namespace android