OSDN Git Service

Merge "Do not request MAC address randomization when associated"
[android-x86/system-connectivity-wificond.git] / server.cpp
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/logging.h>
22
23 #include "wificond/net/netlink_utils.h"
24 #include "wificond/scanning/scan_utils.h"
25
26 using android::binder::Status;
27 using android::sp;
28 using android::IBinder;
29 using android::net::wifi::IApInterface;
30 using android::net::wifi::IClientInterface;
31 using android::net::wifi::IInterfaceEventCallback;
32 using android::net::wifi::IRttClient;
33 using android::net::wifi::IRttController;
34 using android::wifi_hal::DriverTool;
35 using android::wifi_system::HalTool;
36 using android::wifi_system::HostapdManager;
37 using android::wifi_system::InterfaceTool;
38 using android::wifi_system::SupplicantManager;
39
40 using std::placeholders::_1;
41 using std::string;
42 using std::stringstream;
43 using std::unique_ptr;
44 using std::vector;
45
46 namespace android {
47 namespace wificond {
48
49 Server::Server(unique_ptr<HalTool> hal_tool,
50                unique_ptr<InterfaceTool> if_tool,
51                unique_ptr<DriverTool> driver_tool,
52                unique_ptr<SupplicantManager> supplicant_manager,
53                unique_ptr<HostapdManager> hostapd_manager,
54                NetlinkUtils* netlink_utils,
55                ScanUtils* scan_utils)
56     : hal_tool_(std::move(hal_tool)),
57       if_tool_(std::move(if_tool)),
58       driver_tool_(std::move(driver_tool)),
59       supplicant_manager_(std::move(supplicant_manager)),
60       hostapd_manager_(std::move(hostapd_manager)),
61       netlink_utils_(netlink_utils),
62       scan_utils_(scan_utils) {
63 }
64
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
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
92 Status Server::registerRttClient(const sp<IRttClient>& rtt_client,
93                                  sp<IRttController>* out_rtt_controller) {
94   if (rtt_controller_ == nullptr) {
95     rtt_controller_.reset(new RttControllerImpl());
96   }
97   rtt_controller_->RegisterRttClient(rtt_client);
98
99   *out_rtt_controller = rtt_controller_->GetBinder();
100   return Status::ok();
101 }
102
103 Status Server::unregisterRttClient(const sp<IRttClient>& rttClient) {
104   rtt_controller_->UnregisterRttClient(rttClient);
105   if (rtt_controller_->GetClientCount() == 0) {
106     rtt_controller_.reset();
107   }
108   return Status::ok();
109 }
110
111 Status Server::createApInterface(sp<IApInterface>* created_interface) {
112   string interface_name;
113   uint32_t interface_index;
114   vector<uint8_t> interface_mac_addr;
115   if (!SetupInterfaceForMode(DriverTool::kFirmwareModeAp,
116                              &interface_name,
117                              &interface_index,
118                              &interface_mac_addr)) {
119     return Status::ok();  // Logging was done internally
120   }
121
122   unique_ptr<ApInterfaceImpl> ap_interface(new ApInterfaceImpl(
123       interface_name,
124       interface_index,
125       if_tool_.get(),
126       hostapd_manager_.get()));
127   *created_interface = ap_interface->GetBinder();
128   ap_interfaces_.push_back(std::move(ap_interface));
129   BroadcastApInterfaceReady(ap_interfaces_.back()->GetBinder());
130
131   return Status::ok();
132 }
133
134 Status Server::createClientInterface(sp<IClientInterface>* created_interface) {
135   string interface_name;
136   uint32_t interface_index;
137   vector<uint8_t> interface_mac_addr;
138   if (!SetupInterfaceForMode(DriverTool::kFirmwareModeSta,
139                              &interface_name,
140                              &interface_index,
141                              &interface_mac_addr)) {
142     return Status::ok();  // Logging was done internally
143   }
144
145   unique_ptr<ClientInterfaceImpl> client_interface(new ClientInterfaceImpl(
146       wiphy_index_,
147       interface_name,
148       interface_index,
149       interface_mac_addr,
150       if_tool_.get(),
151       supplicant_manager_.get(),
152       netlink_utils_,
153       scan_utils_));
154   *created_interface = client_interface->GetBinder();
155   client_interfaces_.push_back(std::move(client_interface));
156   BroadcastClientInterfaceReady(client_interfaces_.back()->GetBinder());
157
158   return Status::ok();
159 }
160
161 Status Server::tearDownInterfaces() {
162   for (auto& it : client_interfaces_) {
163     BroadcastClientInterfaceTornDown(it->GetBinder());
164   }
165   client_interfaces_.clear();
166
167   for (auto& it : ap_interfaces_) {
168     BroadcastApInterfaceTornDown(it->GetBinder());
169   }
170   ap_interfaces_.clear();
171
172   netlink_utils_->UnsubscribeRegDomainChange(wiphy_index_);
173
174   if (!driver_tool_->UnloadDriver()) {
175     LOG(ERROR) << "Failed to unload WiFi driver!";
176   }
177   return Status::ok();
178 }
179
180 Status Server::GetClientInterfaces(vector<sp<IBinder>>* out_client_interfaces) {
181   vector<sp<android::IBinder>> client_interfaces_binder;
182   for (auto& it : client_interfaces_) {
183     out_client_interfaces->push_back(asBinder(it->GetBinder()));
184   }
185   return binder::Status::ok();
186 }
187
188 Status Server::GetApInterfaces(vector<sp<IBinder>>* out_ap_interfaces) {
189   vector<sp<IBinder>> ap_interfaces_binder;
190   for (auto& it : ap_interfaces_) {
191     out_ap_interfaces->push_back(asBinder(it->GetBinder()));
192   }
193   return binder::Status::ok();
194 }
195
196 void Server::CleanUpSystemState() {
197   supplicant_manager_->StopSupplicant();
198   hostapd_manager_->StopHostapd();
199
200   uint32_t phy_index = 0;
201   uint32_t if_index = 0;
202   vector<uint8_t> mac;
203   string if_name;
204   if (netlink_utils_->GetWiphyIndex(&phy_index) &&
205       netlink_utils_->GetInterfaceInfo(phy_index,
206                                        &if_name,
207                                        &if_index,
208                                        &mac)) {
209     // If the kernel knows about a network interface, mark it as down.
210     // This prevents us from beaconing as an AP, or remaining associated
211     // as a client.
212     if_tool_->SetUpState(if_name.c_str(), false);
213   }
214   // "unloading the driver" is frequently a no-op in systems that
215   // don't have kernel modules, but just in case.
216   driver_tool_->UnloadDriver();
217 }
218
219 bool Server::SetupInterfaceForMode(int mode,
220                                    string* interface_name,
221                                    uint32_t* interface_index,
222                                    vector<uint8_t>* interface_mac_addr) {
223   if (!ap_interfaces_.empty() || !client_interfaces_.empty()) {
224     // In the future we may support multiple interfaces at once.  However,
225     // today, we support just one.
226     LOG(ERROR) << "Cannot create AP interface when other interfaces exist";
227     return false;
228   }
229
230   string result;
231   if (!driver_tool_->LoadDriver()) {
232     LOG(ERROR) << "Failed to load WiFi driver!";
233     return false;
234   }
235   if (!driver_tool_->ChangeFirmwareMode(mode)) {
236     LOG(ERROR) << "Failed to change WiFi firmware mode!";
237     return false;
238   }
239
240   if (!RefreshWiphyIndex()) {
241     return false;
242   }
243
244   netlink_utils_->SubscribeRegDomainChange(
245           wiphy_index_,
246           std::bind(&Server::OnRegDomainChanged,
247           this,
248           _1));
249
250   if (!netlink_utils_->GetInterfaceInfo(wiphy_index_,
251                                         interface_name,
252                                         interface_index,
253                                         interface_mac_addr)) {
254     LOG(ERROR) << "Failed to get interface info from kernel";
255     return false;
256   }
257
258   return true;
259 }
260
261 bool Server::RefreshWiphyIndex() {
262   if (!netlink_utils_->GetWiphyIndex(&wiphy_index_)) {
263     LOG(ERROR) << "Failed to get wiphy index";
264     return false;
265   }
266   return true;
267 }
268
269 void Server::OnRegDomainChanged(std::string& country_code) {
270   if (country_code.empty()) {
271     LOG(INFO) << "Regulatory domain changed";
272   } else {
273     LOG(INFO) << "Regulatory domain changed to country: " << country_code;
274   }
275   LogSupportedBands();
276 }
277
278 void Server::LogSupportedBands() {
279   BandInfo band_info;
280   ScanCapabilities scan_capabilities;
281   WiphyFeatures wiphy_features;
282   netlink_utils_->GetWiphyInfo(wiphy_index_,
283                                &band_info,
284                                &scan_capabilities,
285                                &wiphy_features);
286
287   stringstream ss;
288   for (unsigned int i = 0; i < band_info.band_2g.size(); i++) {
289     ss << " " << band_info.band_2g[i];
290   }
291   LOG(INFO) << "2.4Ghz frequencies:"<< ss.str();
292   ss.str("");
293
294   for (unsigned int i = 0; i < band_info.band_5g.size(); i++) {
295     ss << " " << band_info.band_5g[i];
296   }
297   LOG(INFO) << "5Ghz non-DFS frequencies:"<< ss.str();
298   ss.str("");
299
300   for (unsigned int i = 0; i < band_info.band_dfs.size(); i++) {
301     ss << " " << band_info.band_dfs[i];
302   }
303   LOG(INFO) << "5Ghz DFS frequencies:"<< ss.str();
304 }
305
306 void Server::BroadcastClientInterfaceReady(
307     sp<IClientInterface> network_interface) {
308   for (auto& it : interface_event_callbacks_) {
309     it->OnClientInterfaceReady(network_interface);
310   }
311 }
312
313 void Server::BroadcastApInterfaceReady(
314     sp<IApInterface> network_interface) {
315   for (auto& it : interface_event_callbacks_) {
316     it->OnApInterfaceReady(network_interface);
317   }
318 }
319
320 void Server::BroadcastClientInterfaceTornDown(
321     sp<IClientInterface> network_interface) {
322   for (auto& it : interface_event_callbacks_) {
323     it->OnClientTorndownEvent(network_interface);
324   }
325 }
326
327 void Server::BroadcastApInterfaceTornDown(
328     sp<IApInterface> network_interface) {
329   for (auto& it : interface_event_callbacks_) {
330     it->OnApTorndownEvent(network_interface);
331   }
332 }
333
334 }  // namespace wificond
335 }  // namespace android