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
17 #include "wificond/server.h"
18
19 #include <sstream>
20
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>
26
27 #include "wificond/logging_utils.h"
28 #include "wificond/net/netlink_utils.h"
29 #include "wificond/scanning/scan_utils.h"
30
31 using android::base::WriteStringToFd;
32 using android::binder::Status;
33 using android::sp;
34 using android::IBinder;
35 using android::net::wifi::nl80211::IApInterface;
36 using android::net::wifi::nl80211::IClientInterface;
37 using android::net::wifi::nl80211::IInterfaceEventCallback;
38 using android::net::wifi::nl80211::DeviceWiphyCapabilities;
39 using android::wifi_system::InterfaceTool;
40
41 using std::endl;
42 using std::placeholders::_1;
43 using std::string;
44 using std::stringstream;
45 using std::unique_ptr;
46 using std::vector;
47
48 namespace android {
49 namespace wificond {
50
51 namespace {
52
53 constexpr const char* kPermissionDump = "android.permission.DUMP";
54
55 } // namespace
56
Server(unique_ptr<InterfaceTool> if_tool,NetlinkUtils * netlink_utils,ScanUtils * scan_utils)57 Server::Server(unique_ptr<InterfaceTool> if_tool,
58 NetlinkUtils* netlink_utils,
59 ScanUtils* scan_utils)
60 : if_tool_(std::move(if_tool)),
61 netlink_utils_(netlink_utils),
62 scan_utils_(scan_utils) {
63 }
64
RegisterCallback(const sp<IInterfaceEventCallback> & callback)65 Status Server::RegisterCallback(const sp<IInterfaceEventCallback>& callback) {
66 for (auto& it : interface_event_callbacks_) {
67 if (IInterface::asBinder(callback) == IInterface::asBinder(it)) {
68 LOG(WARNING) << "Ignore duplicate interface event callback registration";
69 return Status::ok();
70 }
71 }
72 LOG(INFO) << "New interface event callback registered";
73 interface_event_callbacks_.push_back(callback);
74 return Status::ok();
75 }
76
UnregisterCallback(const sp<IInterfaceEventCallback> & callback)77 Status Server::UnregisterCallback(const sp<IInterfaceEventCallback>& callback) {
78 for (auto it = interface_event_callbacks_.begin();
79 it != interface_event_callbacks_.end();
80 it++) {
81 if (IInterface::asBinder(callback) == IInterface::asBinder(*it)) {
82 interface_event_callbacks_.erase(it);
83 LOG(INFO) << "Unregister interface event callback";
84 return Status::ok();
85 }
86 }
87 LOG(WARNING) << "Failed to find registered interface event callback"
88 << " to unregister";
89 return Status::ok();
90 }
91
createApInterface(const std::string & iface_name,sp<IApInterface> * created_interface)92 Status Server::createApInterface(const std::string& iface_name,
93 sp<IApInterface>* created_interface) {
94 InterfaceInfo interface;
95 if (!SetupInterface(iface_name, &interface)) {
96 return Status::ok(); // Logging was done internally
97 }
98
99 unique_ptr<ApInterfaceImpl> ap_interface(new ApInterfaceImpl(
100 interface.name,
101 interface.index,
102 netlink_utils_,
103 if_tool_.get()));
104 *created_interface = ap_interface->GetBinder();
105 BroadcastApInterfaceReady(ap_interface->GetBinder());
106 ap_interfaces_[iface_name] = std::move(ap_interface);
107
108 return Status::ok();
109 }
110
tearDownApInterface(const std::string & iface_name,bool * out_success)111 Status Server::tearDownApInterface(const std::string& iface_name,
112 bool* out_success) {
113 *out_success = false;
114 const auto iter = ap_interfaces_.find(iface_name);
115 if (iter != ap_interfaces_.end()) {
116 BroadcastApInterfaceTornDown(iter->second->GetBinder());
117 ap_interfaces_.erase(iter);
118 *out_success = true;
119 }
120 return Status::ok();
121 }
122
createClientInterface(const std::string & iface_name,sp<IClientInterface> * created_interface)123 Status Server::createClientInterface(const std::string& iface_name,
124 sp<IClientInterface>* created_interface) {
125 InterfaceInfo interface;
126 if (!SetupInterface(iface_name, &interface)) {
127 return Status::ok(); // Logging was done internally
128 }
129
130 unique_ptr<ClientInterfaceImpl> client_interface(new ClientInterfaceImpl(
131 wiphy_index_,
132 interface.name,
133 interface.index,
134 interface.mac_address,
135 if_tool_.get(),
136 netlink_utils_,
137 scan_utils_));
138 *created_interface = client_interface->GetBinder();
139 BroadcastClientInterfaceReady(client_interface->GetBinder());
140 client_interfaces_[iface_name] = std::move(client_interface);
141
142 return Status::ok();
143 }
144
tearDownClientInterface(const std::string & iface_name,bool * out_success)145 Status Server::tearDownClientInterface(const std::string& iface_name,
146 bool* out_success) {
147 *out_success = false;
148 const auto iter = client_interfaces_.find(iface_name);
149 if (iter != client_interfaces_.end()) {
150 BroadcastClientInterfaceTornDown(iter->second->GetBinder());
151 client_interfaces_.erase(iter);
152 *out_success = true;
153 }
154 return Status::ok();
155 }
156
tearDownInterfaces()157 Status Server::tearDownInterfaces() {
158 for (auto& it : client_interfaces_) {
159 BroadcastClientInterfaceTornDown(it.second->GetBinder());
160 }
161 client_interfaces_.clear();
162
163 for (auto& it : ap_interfaces_) {
164 BroadcastApInterfaceTornDown(it.second->GetBinder());
165 }
166 ap_interfaces_.clear();
167
168 MarkDownAllInterfaces();
169
170 netlink_utils_->UnsubscribeRegDomainChange(wiphy_index_);
171
172 return Status::ok();
173 }
174
GetClientInterfaces(vector<sp<IBinder>> * out_client_interfaces)175 Status Server::GetClientInterfaces(vector<sp<IBinder>>* out_client_interfaces) {
176 vector<sp<android::IBinder>> client_interfaces_binder;
177 for (auto& it : client_interfaces_) {
178 out_client_interfaces->push_back(asBinder(it.second->GetBinder()));
179 }
180 return binder::Status::ok();
181 }
182
GetApInterfaces(vector<sp<IBinder>> * out_ap_interfaces)183 Status Server::GetApInterfaces(vector<sp<IBinder>>* out_ap_interfaces) {
184 vector<sp<IBinder>> ap_interfaces_binder;
185 for (auto& it : ap_interfaces_) {
186 out_ap_interfaces->push_back(asBinder(it.second->GetBinder()));
187 }
188 return binder::Status::ok();
189 }
190
dump(int fd,const Vector<String16> &)191 status_t Server::dump(int fd, const Vector<String16>& /*args*/) {
192 if (!PermissionCache::checkCallingPermission(String16(kPermissionDump))) {
193 IPCThreadState* ipc = android::IPCThreadState::self();
194 LOG(ERROR) << "Caller (uid: " << ipc->getCallingUid()
195 << ") is not permitted to dump wificond state";
196 return PERMISSION_DENIED;
197 }
198
199 stringstream ss;
200 ss << "Current wiphy index: " << wiphy_index_ << endl;
201 ss << "Cached interfaces list from kernel message: " << endl;
202 for (const auto& iface : interfaces_) {
203 ss << "Interface index: " << iface.index
204 << ", name: " << iface.name
205 << ", mac address: "
206 << LoggingUtils::GetMacString(iface.mac_address) << endl;
207 }
208
209 string country_code;
210 if (netlink_utils_->GetCountryCode(&country_code)) {
211 ss << "Current country code from kernel: " << country_code << endl;
212 } else {
213 ss << "Failed to get country code from kernel." << endl;
214 }
215
216 for (const auto& iface : client_interfaces_) {
217 iface.second->Dump(&ss);
218 }
219
220 for (const auto& iface : ap_interfaces_) {
221 iface.second->Dump(&ss);
222 }
223
224 if (!WriteStringToFd(ss.str(), fd)) {
225 PLOG(ERROR) << "Failed to dump state to fd " << fd;
226 return FAILED_TRANSACTION;
227 }
228
229 return OK;
230 }
231
MarkDownAllInterfaces()232 void Server::MarkDownAllInterfaces() {
233 uint32_t wiphy_index;
234 vector<InterfaceInfo> interfaces;
235 if (netlink_utils_->GetWiphyIndex(&wiphy_index) &&
236 netlink_utils_->GetInterfaces(wiphy_index, &interfaces)) {
237 for (InterfaceInfo& interface : interfaces) {
238 if_tool_->SetUpState(interface.name.c_str(), false);
239 }
240 }
241 }
242
getAvailable2gChannels(std::unique_ptr<vector<int32_t>> * out_frequencies)243 Status Server::getAvailable2gChannels(
244 std::unique_ptr<vector<int32_t>>* out_frequencies) {
245 BandInfo band_info;
246 ScanCapabilities scan_capabilities_ignored;
247 WiphyFeatures wiphy_features_ignored;
248
249 if (!netlink_utils_->GetWiphyInfo(wiphy_index_, &band_info,
250 &scan_capabilities_ignored,
251 &wiphy_features_ignored)) {
252 LOG(ERROR) << "Failed to get wiphy info from kernel";
253 out_frequencies->reset(nullptr);
254 return Status::ok();
255 }
256
257 out_frequencies->reset(
258 new vector<int32_t>(band_info.band_2g.begin(), band_info.band_2g.end()));
259 return Status::ok();
260 }
261
getAvailable5gNonDFSChannels(std::unique_ptr<vector<int32_t>> * out_frequencies)262 Status Server::getAvailable5gNonDFSChannels(
263 std::unique_ptr<vector<int32_t>>* out_frequencies) {
264 BandInfo band_info;
265 ScanCapabilities scan_capabilities_ignored;
266 WiphyFeatures wiphy_features_ignored;
267
268 if (!netlink_utils_->GetWiphyInfo(wiphy_index_, &band_info,
269 &scan_capabilities_ignored,
270 &wiphy_features_ignored)) {
271 LOG(ERROR) << "Failed to get wiphy info from kernel";
272 out_frequencies->reset(nullptr);
273 return Status::ok();
274 }
275
276 out_frequencies->reset(
277 new vector<int32_t>(band_info.band_5g.begin(), band_info.band_5g.end()));
278 return Status::ok();
279 }
280
getAvailableDFSChannels(std::unique_ptr<vector<int32_t>> * out_frequencies)281 Status Server::getAvailableDFSChannels(
282 std::unique_ptr<vector<int32_t>>* out_frequencies) {
283 BandInfo band_info;
284 ScanCapabilities scan_capabilities_ignored;
285 WiphyFeatures wiphy_features_ignored;
286
287 if (!netlink_utils_->GetWiphyInfo(wiphy_index_, &band_info,
288 &scan_capabilities_ignored,
289 &wiphy_features_ignored)) {
290 LOG(ERROR) << "Failed to get wiphy info from kernel";
291 out_frequencies->reset(nullptr);
292 return Status::ok();
293 }
294
295 out_frequencies->reset(new vector<int32_t>(band_info.band_dfs.begin(),
296 band_info.band_dfs.end()));
297 return Status::ok();
298 }
299
getAvailable6gChannels(std::unique_ptr<vector<int32_t>> * out_frequencies)300 Status Server::getAvailable6gChannels(
301 std::unique_ptr<vector<int32_t>>* out_frequencies) {
302 BandInfo band_info;
303 ScanCapabilities scan_capabilities_ignored;
304 WiphyFeatures wiphy_features_ignored;
305
306 if (!netlink_utils_->GetWiphyInfo(wiphy_index_, &band_info,
307 &scan_capabilities_ignored,
308 &wiphy_features_ignored)) {
309 LOG(ERROR) << "Failed to get wiphy info from kernel";
310 out_frequencies->reset(nullptr);
311 return Status::ok();
312 }
313
314 out_frequencies->reset(
315 new vector<int32_t>(band_info.band_6g.begin(), band_info.band_6g.end()));
316 return Status::ok();
317 }
318
getDeviceWiphyCapabilities(const std::string & iface_name,std::unique_ptr<DeviceWiphyCapabilities> * capabilities)319 Status Server::getDeviceWiphyCapabilities(
320 const std::string& iface_name,
321 std::unique_ptr<DeviceWiphyCapabilities>* capabilities) {
322 if (!RefreshWiphyIndex(iface_name)) {
323 capabilities = nullptr;
324 return Status::ok();
325 }
326
327 BandInfo band_info;
328 ScanCapabilities scan_capabilities_ignored;
329 WiphyFeatures wiphy_features_ignored;
330
331 if (!netlink_utils_->GetWiphyInfo(wiphy_index_, &band_info,
332 &scan_capabilities_ignored,
333 &wiphy_features_ignored)) {
334 LOG(ERROR) << "Failed to get wiphy info from kernel";
335 capabilities = nullptr;
336 return Status::ok();
337 }
338
339 capabilities->reset(new DeviceWiphyCapabilities());
340
341 capabilities->get()->is80211nSupported_ = band_info.is_80211n_supported;
342 capabilities->get()->is80211acSupported_ = band_info.is_80211ac_supported;
343 capabilities->get()->is80211axSupported_ = band_info.is_80211ax_supported;
344 capabilities->get()->is160MhzSupported_ = band_info.is_160_mhz_supported;
345 capabilities->get()->is80p80MhzSupported_ = band_info.is_80p80_mhz_supported;
346 capabilities->get()->maxTxStreams_ = band_info.max_tx_streams;
347 capabilities->get()->maxRxStreams_ = band_info.max_rx_streams;
348
349 return Status::ok();
350 }
351
SetupInterface(const std::string & iface_name,InterfaceInfo * interface)352 bool Server::SetupInterface(const std::string& iface_name,
353 InterfaceInfo* interface) {
354 if (!RefreshWiphyIndex(iface_name)) {
355 return false;
356 }
357
358 netlink_utils_->SubscribeRegDomainChange(
359 wiphy_index_,
360 std::bind(&Server::OnRegDomainChanged,
361 this,
362 _1));
363
364 interfaces_.clear();
365 if (!netlink_utils_->GetInterfaces(wiphy_index_, &interfaces_)) {
366 LOG(ERROR) << "Failed to get interfaces info from kernel";
367 return false;
368 }
369
370 for (const auto& iface : interfaces_) {
371 if (iface.name == iface_name) {
372 *interface = iface;
373 return true;
374 }
375 }
376
377 LOG(ERROR) << "No usable interface found";
378 return false;
379 }
380
RefreshWiphyIndex(const std::string & iface_name)381 bool Server::RefreshWiphyIndex(const std::string& iface_name) {
382 if (!netlink_utils_->GetWiphyIndex(&wiphy_index_, iface_name)) {
383 LOG(ERROR) << "Failed to get wiphy index";
384 return false;
385 }
386 return true;
387 }
388
OnRegDomainChanged(std::string & country_code)389 void Server::OnRegDomainChanged(std::string& country_code) {
390 if (country_code.empty()) {
391 LOG(INFO) << "Regulatory domain changed";
392 } else {
393 LOG(INFO) << "Regulatory domain changed to country: " << country_code;
394 }
395 LogSupportedBands();
396 }
397
LogSupportedBands()398 void Server::LogSupportedBands() {
399 BandInfo band_info;
400 ScanCapabilities scan_capabilities;
401 WiphyFeatures wiphy_features;
402 netlink_utils_->GetWiphyInfo(wiphy_index_,
403 &band_info,
404 &scan_capabilities,
405 &wiphy_features);
406
407 stringstream ss;
408 for (unsigned int i = 0; i < band_info.band_2g.size(); i++) {
409 ss << " " << band_info.band_2g[i];
410 }
411 LOG(INFO) << "2.4Ghz frequencies:"<< ss.str();
412 ss.str("");
413
414 for (unsigned int i = 0; i < band_info.band_5g.size(); i++) {
415 ss << " " << band_info.band_5g[i];
416 }
417 LOG(INFO) << "5Ghz non-DFS frequencies:"<< ss.str();
418 ss.str("");
419
420 for (unsigned int i = 0; i < band_info.band_dfs.size(); i++) {
421 ss << " " << band_info.band_dfs[i];
422 }
423 LOG(INFO) << "5Ghz DFS frequencies:"<< ss.str();
424
425 for (unsigned int i = 0; i < band_info.band_6g.size(); i++) {
426 ss << " " << band_info.band_6g[i];
427 }
428 LOG(INFO) << "6Ghz frequencies:"<< ss.str();
429 }
430
BroadcastClientInterfaceReady(sp<IClientInterface> network_interface)431 void Server::BroadcastClientInterfaceReady(
432 sp<IClientInterface> network_interface) {
433 for (auto& it : interface_event_callbacks_) {
434 it->OnClientInterfaceReady(network_interface);
435 }
436 }
437
BroadcastApInterfaceReady(sp<IApInterface> network_interface)438 void Server::BroadcastApInterfaceReady(
439 sp<IApInterface> network_interface) {
440 for (auto& it : interface_event_callbacks_) {
441 it->OnApInterfaceReady(network_interface);
442 }
443 }
444
BroadcastClientInterfaceTornDown(sp<IClientInterface> network_interface)445 void Server::BroadcastClientInterfaceTornDown(
446 sp<IClientInterface> network_interface) {
447 for (auto& it : interface_event_callbacks_) {
448 it->OnClientTorndownEvent(network_interface);
449 }
450 }
451
BroadcastApInterfaceTornDown(sp<IApInterface> network_interface)452 void Server::BroadcastApInterfaceTornDown(
453 sp<IApInterface> network_interface) {
454 for (auto& it : interface_event_callbacks_) {
455 it->OnApTorndownEvent(network_interface);
456 }
457 }
458
459 } // namespace wificond
460 } // namespace android
461