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::wifi_system::HostapdManager;
39 using android::wifi_system::InterfaceTool;
40 using android::wifi_system::SupplicantManager;
43 using std::placeholders::_1;
45 using std::stringstream;
46 using std::unique_ptr;
54 constexpr const char* kPermissionDump = "android.permission.DUMP";
58 Server::Server(unique_ptr<InterfaceTool> if_tool,
59 unique_ptr<SupplicantManager> supplicant_manager,
60 unique_ptr<HostapdManager> hostapd_manager,
61 NetlinkUtils* netlink_utils,
62 ScanUtils* scan_utils)
63 : if_tool_(std::move(if_tool)),
64 supplicant_manager_(std::move(supplicant_manager)),
65 hostapd_manager_(std::move(hostapd_manager)),
66 netlink_utils_(netlink_utils),
67 scan_utils_(scan_utils) {
70 Status Server::RegisterCallback(const sp<IInterfaceEventCallback>& callback) {
71 for (auto& it : interface_event_callbacks_) {
72 if (IInterface::asBinder(callback) == IInterface::asBinder(it)) {
73 LOG(WARNING) << "Ignore duplicate interface event callback registration";
77 LOG(INFO) << "New interface event callback registered";
78 interface_event_callbacks_.push_back(callback);
82 Status Server::UnregisterCallback(const sp<IInterfaceEventCallback>& callback) {
83 for (auto it = interface_event_callbacks_.begin();
84 it != interface_event_callbacks_.end();
86 if (IInterface::asBinder(callback) == IInterface::asBinder(*it)) {
87 interface_event_callbacks_.erase(it);
88 LOG(INFO) << "Unregister interface event callback";
92 LOG(WARNING) << "Failed to find registered interface event callback"
97 Status Server::createApInterface(sp<IApInterface>* created_interface) {
98 InterfaceInfo interface;
99 if (!SetupInterface(&interface)) {
100 return Status::ok(); // Logging was done internally
103 unique_ptr<ApInterfaceImpl> ap_interface(new ApInterfaceImpl(
108 hostapd_manager_.get()));
109 *created_interface = ap_interface->GetBinder();
110 ap_interfaces_.push_back(std::move(ap_interface));
111 BroadcastApInterfaceReady(ap_interfaces_.back()->GetBinder());
116 Status Server::createClientInterface(sp<IClientInterface>* created_interface) {
117 InterfaceInfo interface;
118 if (!SetupInterface(&interface)) {
119 return Status::ok(); // Logging was done internally
122 unique_ptr<ClientInterfaceImpl> client_interface(new ClientInterfaceImpl(
126 interface.mac_address,
128 supplicant_manager_.get(),
131 *created_interface = client_interface->GetBinder();
132 client_interfaces_.push_back(std::move(client_interface));
133 BroadcastClientInterfaceReady(client_interfaces_.back()->GetBinder());
138 Status Server::tearDownInterfaces() {
139 for (auto& it : client_interfaces_) {
140 BroadcastClientInterfaceTornDown(it->GetBinder());
142 client_interfaces_.clear();
144 for (auto& it : ap_interfaces_) {
145 BroadcastApInterfaceTornDown(it->GetBinder());
147 ap_interfaces_.clear();
149 MarkDownAllInterfaces();
151 netlink_utils_->UnsubscribeRegDomainChange(wiphy_index_);
156 Status Server::GetClientInterfaces(vector<sp<IBinder>>* out_client_interfaces) {
157 vector<sp<android::IBinder>> client_interfaces_binder;
158 for (auto& it : client_interfaces_) {
159 out_client_interfaces->push_back(asBinder(it->GetBinder()));
161 return binder::Status::ok();
164 Status Server::GetApInterfaces(vector<sp<IBinder>>* out_ap_interfaces) {
165 vector<sp<IBinder>> ap_interfaces_binder;
166 for (auto& it : ap_interfaces_) {
167 out_ap_interfaces->push_back(asBinder(it->GetBinder()));
169 return binder::Status::ok();
172 status_t Server::dump(int fd, const Vector<String16>& /*args*/) {
173 if (!PermissionCache::checkCallingPermission(String16(kPermissionDump))) {
174 IPCThreadState* ipc = android::IPCThreadState::self();
175 LOG(ERROR) << "Caller (uid: " << ipc->getCallingUid()
176 << ") is not permitted to dump wificond state";
177 return PERMISSION_DENIED;
181 ss << "Current wiphy index: " << wiphy_index_ << endl;
182 ss << "Cached interfaces list from kernel message: " << endl;
183 for (const auto& iface : interfaces_) {
184 ss << "Interface index: " << iface.index
185 << ", name: " << iface.name
187 << LoggingUtils::GetMacString(iface.mac_address) << endl;
190 for (const auto& iface : client_interfaces_) {
194 for (const auto& iface : ap_interfaces_) {
198 if (!WriteStringToFd(ss.str(), fd)) {
199 PLOG(ERROR) << "Failed to dump state to fd " << fd;
200 return FAILED_TRANSACTION;
206 void Server::MarkDownAllInterfaces() {
207 uint32_t wiphy_index;
208 vector<InterfaceInfo> interfaces;
209 if (netlink_utils_->GetWiphyIndex(&wiphy_index) &&
210 netlink_utils_->GetInterfaces(wiphy_index, &interfaces)) {
211 for (InterfaceInfo& interface : interfaces) {
212 if_tool_->SetUpState(interface.name.c_str(), false);
217 void Server::CleanUpSystemState() {
218 supplicant_manager_->StopSupplicant();
219 hostapd_manager_->StopHostapd();
220 MarkDownAllInterfaces();
223 bool Server::SetupInterface(InterfaceInfo* interface) {
224 if (!ap_interfaces_.empty() || !client_interfaces_.empty()) {
225 // In the future we may support multiple interfaces at once. However,
226 // today, we support just one.
227 LOG(ERROR) << "Cannot create AP interface when other interfaces exist";
231 if (!RefreshWiphyIndex()) {
235 netlink_utils_->SubscribeRegDomainChange(
237 std::bind(&Server::OnRegDomainChanged,
242 if (!netlink_utils_->GetInterfaces(wiphy_index_, &interfaces_)) {
243 LOG(ERROR) << "Failed to get interfaces info from kernel";
247 for (const auto& iface : interfaces_) {
248 // Some kernel/driver uses station type for p2p interface.
249 // In that case we can only rely on hard-coded name to exclude
250 // p2p interface from station interfaces.
251 // Currently NAN interfaces also use station type.
252 // We should blacklist NAN interfaces as well.
253 if (iface.name != "p2p0" &&
254 !android::base::StartsWith(iface.name, "aware_data")) {
260 LOG(ERROR) << "No usable interface found";
264 bool Server::RefreshWiphyIndex() {
265 if (!netlink_utils_->GetWiphyIndex(&wiphy_index_)) {
266 LOG(ERROR) << "Failed to get wiphy index";
272 void Server::OnRegDomainChanged(std::string& country_code) {
273 if (country_code.empty()) {
274 LOG(INFO) << "Regulatory domain changed";
276 LOG(INFO) << "Regulatory domain changed to country: " << country_code;
281 void Server::LogSupportedBands() {
283 ScanCapabilities scan_capabilities;
284 WiphyFeatures wiphy_features;
285 netlink_utils_->GetWiphyInfo(wiphy_index_,
291 for (unsigned int i = 0; i < band_info.band_2g.size(); i++) {
292 ss << " " << band_info.band_2g[i];
294 LOG(INFO) << "2.4Ghz frequencies:"<< ss.str();
297 for (unsigned int i = 0; i < band_info.band_5g.size(); i++) {
298 ss << " " << band_info.band_5g[i];
300 LOG(INFO) << "5Ghz non-DFS frequencies:"<< ss.str();
303 for (unsigned int i = 0; i < band_info.band_dfs.size(); i++) {
304 ss << " " << band_info.band_dfs[i];
306 LOG(INFO) << "5Ghz DFS frequencies:"<< ss.str();
309 void Server::BroadcastClientInterfaceReady(
310 sp<IClientInterface> network_interface) {
311 for (auto& it : interface_event_callbacks_) {
312 it->OnClientInterfaceReady(network_interface);
316 void Server::BroadcastApInterfaceReady(
317 sp<IApInterface> network_interface) {
318 for (auto& it : interface_event_callbacks_) {
319 it->OnApInterfaceReady(network_interface);
323 void Server::BroadcastClientInterfaceTornDown(
324 sp<IClientInterface> network_interface) {
325 for (auto& it : interface_event_callbacks_) {
326 it->OnClientTorndownEvent(network_interface);
330 void Server::BroadcastApInterfaceTornDown(
331 sp<IApInterface> network_interface) {
332 for (auto& it : interface_event_callbacks_) {
333 it->OnApTorndownEvent(network_interface);
337 } // namespace wificond
338 } // namespace android