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 "android/net/wifi/nl80211/IWifiScannerImpl.h"
18 #include "wificond/scanning/scan_utils.h"
19 
20 #include <array>
21 #include <vector>
22 
23 #include <linux/netlink.h>
24 #include <linux/if_ether.h>
25 
26 #include <android-base/logging.h>
27 
28 #include "wificond/net/kernel-header-latest/nl80211.h"
29 #include "wificond/net/netlink_manager.h"
30 #include "wificond/net/nl80211_packet.h"
31 #include "wificond/scanning/scan_result.h"
32 
33 using android::net::wifi::nl80211::IWifiScannerImpl;
34 using android::net::wifi::nl80211::NativeScanResult;
35 using android::net::wifi::nl80211::RadioChainInfo;
36 using std::array;
37 using std::unique_ptr;
38 using std::vector;
39 
40 namespace android {
41 namespace wificond {
42 namespace {
43 
44 constexpr uint8_t kElemIdSsid = 0;
45 constexpr unsigned int kMsecPerSec = 1000;
46 
47 }  // namespace
48 
ScanUtils(NetlinkManager * netlink_manager)49 ScanUtils::ScanUtils(NetlinkManager* netlink_manager)
50     : netlink_manager_(netlink_manager) {
51   if (!netlink_manager_->IsStarted()) {
52     netlink_manager_->Start();
53   }
54 }
55 
~ScanUtils()56 ScanUtils::~ScanUtils() {}
57 
SubscribeScanResultNotification(uint32_t interface_index,OnScanResultsReadyHandler handler)58 void ScanUtils::SubscribeScanResultNotification(
59     uint32_t interface_index,
60     OnScanResultsReadyHandler handler) {
61   netlink_manager_->SubscribeScanResultNotification(interface_index, handler);
62 }
63 
UnsubscribeScanResultNotification(uint32_t interface_index)64 void ScanUtils::UnsubscribeScanResultNotification(uint32_t interface_index) {
65   netlink_manager_->UnsubscribeScanResultNotification(interface_index);
66 }
67 
SubscribeSchedScanResultNotification(uint32_t interface_index,OnSchedScanResultsReadyHandler handler)68 void ScanUtils::SubscribeSchedScanResultNotification(
69     uint32_t interface_index,
70     OnSchedScanResultsReadyHandler handler) {
71   netlink_manager_->SubscribeSchedScanResultNotification(interface_index,
72                                                          handler);
73 }
74 
UnsubscribeSchedScanResultNotification(uint32_t interface_index)75 void ScanUtils::UnsubscribeSchedScanResultNotification(
76     uint32_t interface_index) {
77   netlink_manager_->UnsubscribeSchedScanResultNotification(interface_index);
78 }
79 
GetScanResult(uint32_t interface_index,vector<NativeScanResult> * out_scan_results)80 bool ScanUtils::GetScanResult(uint32_t interface_index,
81                               vector<NativeScanResult>* out_scan_results) {
82   NL80211Packet get_scan(
83       netlink_manager_->GetFamilyId(),
84       NL80211_CMD_GET_SCAN,
85       netlink_manager_->GetSequenceNumber(),
86       getpid());
87   get_scan.AddFlag(NLM_F_DUMP);
88   NL80211Attr<uint32_t> ifindex(NL80211_ATTR_IFINDEX, interface_index);
89   get_scan.AddAttribute(ifindex);
90 
91   vector<unique_ptr<const NL80211Packet>> response;
92   if (!netlink_manager_->SendMessageAndGetResponses(get_scan, &response))  {
93     LOG(ERROR) << "NL80211_CMD_GET_SCAN dump failed";
94     return false;
95   }
96   if (response.empty()) {
97     LOG(INFO) << "Unexpected empty scan result!";
98     return true;
99   }
100 
101   for (auto& packet : response) {
102     if (packet->GetMessageType() == NLMSG_ERROR) {
103       LOG(ERROR) << "Receive ERROR message: "
104                  << strerror(packet->GetErrorCode());
105       continue;
106     }
107     if (packet->GetMessageType() != netlink_manager_->GetFamilyId()) {
108       LOG(ERROR) << "Wrong message type: "
109                  << packet->GetMessageType();
110       continue;
111     }
112     uint32_t if_index;
113     if (!packet->GetAttributeValue(NL80211_ATTR_IFINDEX, &if_index)) {
114       LOG(ERROR) << "No interface index in scan result.";
115       continue;
116     }
117     if (if_index != interface_index) {
118       LOG(WARNING) << "Uninteresting scan result for interface: " << if_index;
119       continue;
120     }
121 
122     NativeScanResult scan_result;
123     if (!ParseScanResult(std::move(packet), &scan_result)) {
124       LOG(DEBUG) << "Ignore invalid scan result";
125       continue;
126     }
127     out_scan_results->push_back(std::move(scan_result));
128   }
129   return true;
130 }
131 
ParseScanResult(unique_ptr<const NL80211Packet> packet,NativeScanResult * scan_result)132 bool ScanUtils::ParseScanResult(unique_ptr<const NL80211Packet> packet,
133                                 NativeScanResult* scan_result) {
134   if (packet->GetCommand() != NL80211_CMD_NEW_SCAN_RESULTS) {
135     LOG(ERROR) << "Wrong command for new scan result message";
136     return false;
137   }
138   NL80211NestedAttr bss(0);
139   if (packet->GetAttribute(NL80211_ATTR_BSS, &bss)) {
140     array<uint8_t, ETH_ALEN> bssid;
141     if (!bss.GetAttributeValue(NL80211_BSS_BSSID, &bssid)) {
142       LOG(ERROR) << "Failed to get BSSID from scan result packet";
143       return false;
144     }
145     uint32_t freq;
146     if (!bss.GetAttributeValue(NL80211_BSS_FREQUENCY, &freq)) {
147       LOG(ERROR) << "Failed to get Frequency from scan result packet";
148       return false;
149     }
150     vector<uint8_t> ie;
151     if (!bss.GetAttributeValue(NL80211_BSS_INFORMATION_ELEMENTS, &ie)) {
152       LOG(ERROR) << "Failed to get Information Element from scan result packet";
153       return false;
154     }
155     vector<uint8_t> ssid;
156     if (!GetSSIDFromInfoElement(ie, &ssid)) {
157       // Skip BSS without SSID IE.
158       // These scan results are considered as malformed.
159       return false;
160     }
161     uint64_t last_seen_since_boot_microseconds;
162     if (!GetBssTimestamp(bss, &last_seen_since_boot_microseconds)) {
163       // Logging is done inside |GetBssTimestamp|.
164       return false;
165     }
166     int32_t signal;
167     if (!bss.GetAttributeValue(NL80211_BSS_SIGNAL_MBM, &signal)) {
168       LOG(ERROR) << "Failed to get Signal Strength from scan result packet";
169       return false;
170     }
171     uint16_t capability;
172     if (!bss.GetAttributeValue(NL80211_BSS_CAPABILITY, &capability)) {
173       LOG(ERROR) << "Failed to get capability field from scan result packet";
174       return false;
175     }
176     bool associated = false;
177     uint32_t bss_status;
178     if (bss.GetAttributeValue(NL80211_BSS_STATUS, &bss_status) &&
179             (bss_status == NL80211_BSS_STATUS_AUTHENTICATED ||
180                 bss_status == NL80211_BSS_STATUS_ASSOCIATED)) {
181       associated = true;
182     }
183     std::vector<RadioChainInfo> radio_chain_infos;
184     ParseRadioChainInfos(bss, &radio_chain_infos);
185 
186     *scan_result =
187         NativeScanResult(ssid, bssid, ie, freq, signal,
188                          last_seen_since_boot_microseconds,
189                          capability, associated, radio_chain_infos);
190   }
191   return true;
192 }
193 
GetBssTimestampForTesting(const NL80211NestedAttr & bss,uint64_t * last_seen_since_boot_microseconds)194 bool ScanUtils::GetBssTimestampForTesting(
195     const NL80211NestedAttr& bss,
196     uint64_t* last_seen_since_boot_microseconds){
197   return GetBssTimestamp(bss, last_seen_since_boot_microseconds);
198 }
199 
GetBssTimestamp(const NL80211NestedAttr & bss,uint64_t * last_seen_since_boot_microseconds)200 bool ScanUtils::GetBssTimestamp(const NL80211NestedAttr& bss,
201                                 uint64_t* last_seen_since_boot_microseconds){
202   uint64_t last_seen_since_boot_nanoseconds;
203   if (bss.GetAttributeValue(NL80211_BSS_LAST_SEEN_BOOTTIME,
204                             &last_seen_since_boot_nanoseconds)) {
205     *last_seen_since_boot_microseconds = last_seen_since_boot_nanoseconds / 1000;
206   } else {
207     // Fall back to use TSF if we can't find NL80211_BSS_LAST_SEEN_BOOTTIME
208     // attribute.
209     if (!bss.GetAttributeValue(NL80211_BSS_TSF, last_seen_since_boot_microseconds)) {
210       LOG(ERROR) << "Failed to get TSF from scan result packet";
211       return false;
212     }
213     uint64_t beacon_tsf_microseconds;
214     if (bss.GetAttributeValue(NL80211_BSS_BEACON_TSF, &beacon_tsf_microseconds)) {
215       *last_seen_since_boot_microseconds = std::max(*last_seen_since_boot_microseconds,
216                                                     beacon_tsf_microseconds);
217     }
218   }
219   return true;
220 }
221 
ParseRadioChainInfos(const NL80211NestedAttr & bss,std::vector<RadioChainInfo> * radio_chain_infos)222 bool ScanUtils::ParseRadioChainInfos(
223     const NL80211NestedAttr& bss,
224     std::vector<RadioChainInfo> *radio_chain_infos) {
225   *radio_chain_infos = {};
226   // Contains a nested array of signal strength attributes: (ChainId, Rssi in dBm)
227   NL80211NestedAttr radio_chain_infos_attr(0);
228   if (!bss.GetAttribute(NL80211_BSS_CHAIN_SIGNAL, &radio_chain_infos_attr)) {
229     return false;
230   }
231   std::vector<NL80211Attr<int8_t>> radio_chain_infos_attrs;
232   if (!radio_chain_infos_attr.GetListOfAttributes(
233         &radio_chain_infos_attrs)) {
234     LOG(ERROR) << "Failed to get radio chain info attrs within "
235                << "NL80211_BSS_CHAIN_SIGNAL";
236     return false;
237   }
238   for (const auto& attr : radio_chain_infos_attrs) {
239     RadioChainInfo radio_chain_info;
240     radio_chain_info.chain_id = attr.GetAttributeId();
241     radio_chain_info.level = attr.GetValue();
242     radio_chain_infos->push_back(radio_chain_info);
243   }
244   return true;
245 }
246 
GetSSIDFromInfoElement(const vector<uint8_t> & ie,vector<uint8_t> * ssid)247 bool ScanUtils::GetSSIDFromInfoElement(const vector<uint8_t>& ie,
248                                        vector<uint8_t>* ssid) {
249   // Information elements are stored in 'TLV' format.
250   // Field:  |   Type     |          Length           |      Value      |
251   // Length: |     1      |             1             |     variable    |
252   // Content:| Element ID | Length of the Value field | Element payload |
253   const uint8_t* end = ie.data() + ie.size();
254   const uint8_t* ptr = ie.data();
255   // +1 means we must have space for the length field.
256   while (ptr + 1  < end) {
257     uint8_t type = *ptr;
258     uint8_t length = *(ptr + 1);
259     // Length field is invalid.
260     if (ptr + 1 + length >= end) {
261       return false;
262     }
263     // SSID element is found.
264     if (type == kElemIdSsid) {
265       // SSID is an empty string.
266       if (length == 0) {
267         *ssid = vector<uint8_t>();
268       } else {
269         *ssid = vector<uint8_t>(ptr + 2, ptr + length + 2);
270       }
271       return true;
272     }
273     ptr += 2 + length;
274   }
275   return false;
276 }
277 
Scan(uint32_t interface_index,bool request_random_mac,int scan_type,bool enable_6ghz_rnr,const vector<vector<uint8_t>> & ssids,const vector<uint32_t> & freqs,const vector<uint8_t> & vendor_ies,int * error_code)278 bool ScanUtils::Scan(uint32_t interface_index,
279                      bool request_random_mac,
280                      int scan_type,
281                      bool enable_6ghz_rnr,
282                      const vector<vector<uint8_t>>& ssids,
283                      const vector<uint32_t>& freqs,
284                      const vector<uint8_t>& vendor_ies,
285                      int* error_code) {
286   NL80211Packet trigger_scan(
287       netlink_manager_->GetFamilyId(),
288       NL80211_CMD_TRIGGER_SCAN,
289       netlink_manager_->GetSequenceNumber(),
290       getpid());
291   // If we do not use NLM_F_ACK, we only receive a unicast repsonse
292   // when there is an error. If everything is good, scan results notification
293   // will only be sent through multicast.
294   // If NLM_F_ACK is set, there will always be an unicast repsonse, either an
295   // ERROR or an ACK message. The handler will always be called and removed by
296   // NetlinkManager.
297   trigger_scan.AddFlag(NLM_F_ACK);
298   NL80211Attr<uint32_t> if_index_attr(NL80211_ATTR_IFINDEX, interface_index);
299 
300   NL80211NestedAttr ssids_attr(NL80211_ATTR_SCAN_SSIDS);
301   for (size_t i = 0; i < ssids.size(); i++) {
302     ssids_attr.AddAttribute(NL80211Attr<vector<uint8_t>>(i, ssids[i]));
303   }
304   NL80211NestedAttr freqs_attr(NL80211_ATTR_SCAN_FREQUENCIES);
305   for (size_t i = 0; i < freqs.size(); i++) {
306     freqs_attr.AddAttribute(NL80211Attr<uint32_t>(i, freqs[i]));
307   }
308 
309   trigger_scan.AddAttribute(if_index_attr);
310   trigger_scan.AddAttribute(ssids_attr);
311   // An absence of NL80211_ATTR_SCAN_FREQUENCIES attribue informs kernel to
312   // scan all supported frequencies.
313   if (!freqs.empty()) {
314     trigger_scan.AddAttribute(freqs_attr);
315   }
316 
317   uint32_t scan_flags = 0;
318   if (request_random_mac) {
319     scan_flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
320   }
321   switch (scan_type) {
322     case IWifiScannerImpl::SCAN_TYPE_LOW_SPAN:
323       scan_flags |= NL80211_SCAN_FLAG_LOW_SPAN;
324       break;
325     case IWifiScannerImpl::SCAN_TYPE_LOW_POWER:
326       scan_flags |= NL80211_SCAN_FLAG_LOW_POWER;
327       break;
328     case IWifiScannerImpl::SCAN_TYPE_HIGH_ACCURACY:
329       scan_flags |= NL80211_SCAN_FLAG_HIGH_ACCURACY;
330       break;
331     case IWifiScannerImpl::SCAN_TYPE_DEFAULT:
332       break;
333     default:
334       CHECK(0) << "Invalid scan type received: " << scan_type;
335   }
336   if (enable_6ghz_rnr) {
337     scan_flags |= NL80211_SCAN_FLAG_COLOCATED_6GHZ;
338   }
339   if (scan_flags) {
340     trigger_scan.AddAttribute(
341         NL80211Attr<uint32_t>(NL80211_ATTR_SCAN_FLAGS,
342                               scan_flags));
343     LOG(DEBUG) << "Triggering scan with scan_flag=" << scan_flags;
344   }
345 
346   if (!vendor_ies.empty()) {
347     NL80211Attr<vector<uint8_t>> vendor_ie_attr(NL80211_ATTR_IE, vendor_ies);
348     trigger_scan.AddAttribute(vendor_ie_attr);
349   }
350 
351   // We are receiving an ERROR/ACK message instead of the actual
352   // scan results here, so it is OK to expect a timely response because
353   // kernel is supposed to send the ERROR/ACK back before the scan starts.
354   vector<unique_ptr<const NL80211Packet>> response;
355   if (!netlink_manager_->SendMessageAndGetAckOrError(trigger_scan,
356                                                      error_code)) {
357     // Logging is done inside |SendMessageAndGetAckOrError|.
358     return false;
359   }
360   if (*error_code != 0) {
361     LOG(ERROR) << "NL80211_CMD_TRIGGER_SCAN failed: " << strerror(*error_code);
362     return false;
363   }
364   return true;
365 }
366 
StopScheduledScan(uint32_t interface_index)367 bool ScanUtils::StopScheduledScan(uint32_t interface_index) {
368   NL80211Packet stop_sched_scan(
369       netlink_manager_->GetFamilyId(),
370       NL80211_CMD_STOP_SCHED_SCAN,
371       netlink_manager_->GetSequenceNumber(),
372       getpid());
373   // Force an ACK response upon success.
374   stop_sched_scan.AddFlag(NLM_F_ACK);
375   stop_sched_scan.AddAttribute(
376       NL80211Attr<uint32_t>(NL80211_ATTR_IFINDEX, interface_index));
377   vector<unique_ptr<const NL80211Packet>> response;
378   int error_code;
379   if (!netlink_manager_->SendMessageAndGetAckOrError(stop_sched_scan,
380                                                      &error_code))  {
381     LOG(ERROR) << "NL80211_CMD_STOP_SCHED_SCAN failed";
382     return false;
383   }
384   if (error_code == ENOENT) {
385     LOG(WARNING) << "Scheduled scan is not running!";
386     return false;
387   } else if (error_code != 0) {
388     LOG(ERROR) << "Receive ERROR message in response to"
389                << " 'stop scheduled scan' request: "
390                << strerror(error_code);
391     return false;
392   }
393   return true;
394 }
395 
AbortScan(uint32_t interface_index)396 bool ScanUtils::AbortScan(uint32_t interface_index) {
397   NL80211Packet abort_scan(
398       netlink_manager_->GetFamilyId(),
399       NL80211_CMD_ABORT_SCAN,
400       netlink_manager_->GetSequenceNumber(),
401       getpid());
402 
403   // Force an ACK response upon success.
404   abort_scan.AddFlag(NLM_F_ACK);
405   abort_scan.AddAttribute(
406       NL80211Attr<uint32_t>(NL80211_ATTR_IFINDEX, interface_index));
407 
408   if (!netlink_manager_->SendMessageAndGetAck(abort_scan)) {
409     LOG(ERROR) << "NL80211_CMD_ABORT_SCAN failed";
410     return false;
411   }
412   return true;
413 }
414 
StartScheduledScan(uint32_t interface_index,const SchedScanIntervalSetting & interval_setting,int32_t rssi_threshold_2g,int32_t rssi_threshold_5g,int32_t rssi_threshold_6g,const SchedScanReqFlags & req_flags,const std::vector<std::vector<uint8_t>> & scan_ssids,const std::vector<std::vector<uint8_t>> & match_ssids,const std::vector<uint32_t> & freqs,int * error_code)415 bool ScanUtils::StartScheduledScan(
416     uint32_t interface_index,
417     const SchedScanIntervalSetting& interval_setting,
418     int32_t rssi_threshold_2g,
419     int32_t rssi_threshold_5g,
420     int32_t rssi_threshold_6g,
421     const SchedScanReqFlags& req_flags,
422     const std::vector<std::vector<uint8_t>>& scan_ssids,
423     const std::vector<std::vector<uint8_t>>& match_ssids,
424     const std::vector<uint32_t>& freqs,
425     int* error_code) {
426   NL80211Packet start_sched_scan(
427       netlink_manager_->GetFamilyId(),
428       NL80211_CMD_START_SCHED_SCAN,
429       netlink_manager_->GetSequenceNumber(),
430       getpid());
431   // Force an ACK response upon success.
432   start_sched_scan.AddFlag(NLM_F_ACK);
433 
434   NL80211NestedAttr scan_ssids_attr(NL80211_ATTR_SCAN_SSIDS);
435   for (size_t i = 0; i < scan_ssids.size(); i++) {
436     scan_ssids_attr.AddAttribute(NL80211Attr<vector<uint8_t>>(i, scan_ssids[i]));
437   }
438   NL80211NestedAttr freqs_attr(NL80211_ATTR_SCAN_FREQUENCIES);
439   for (size_t i = 0; i < freqs.size(); i++) {
440     freqs_attr.AddAttribute(NL80211Attr<uint32_t>(i, freqs[i]));
441   }
442 
443   //   Structure of attributes of scheduled scan filters:
444   // |                                Nested Attribute: id: NL80211_ATTR_SCHED_SCAN_MATCH                           |
445   // |     Nested Attributed: id: 0       |    Nested Attributed: id: 1         |      Nested Attr: id: 2     | ... |
446   // | MATCH_SSID  | MATCH_RSSI(optional) | MATCH_SSID  | MACTCH_RSSI(optional) | MATCH_RSSI(optinal, global) | ... |
447   NL80211NestedAttr scan_match_attr(NL80211_ATTR_SCHED_SCAN_MATCH);
448   for (size_t i = 0; i < match_ssids.size(); i++) {
449     NL80211NestedAttr match_group(i);
450     match_group.AddAttribute(
451         NL80211Attr<vector<uint8_t>>(NL80211_SCHED_SCAN_MATCH_ATTR_SSID, match_ssids[i]));
452     match_group.AddAttribute(
453         NL80211Attr<int32_t>(NL80211_SCHED_SCAN_MATCH_ATTR_RSSI, rssi_threshold_5g));
454     scan_match_attr.AddAttribute(match_group);
455   }
456   start_sched_scan.AddAttribute(scan_match_attr);
457 
458   // We set 5g threshold for default and ajust threshold for 2g band.
459   // check sched_scan supported before set NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST attribute.
460   if (req_flags.request_sched_scan_relative_rssi) {
461       struct nl80211_bss_select_rssi_adjust rssi_adjust;
462       rssi_adjust.band = NL80211_BAND_2GHZ;
463       rssi_adjust.delta = static_cast<int8_t>(rssi_threshold_2g - rssi_threshold_5g);
464       NL80211Attr<vector<uint8_t>> rssi_adjust_attr(
465           NL80211_ATTR_SCHED_SCAN_RSSI_ADJUST,
466           vector<uint8_t>(
467               reinterpret_cast<uint8_t*>(&rssi_adjust),
468               reinterpret_cast<uint8_t*>(&rssi_adjust) + sizeof(rssi_adjust)));
469       start_sched_scan.AddAttribute(rssi_adjust_attr);
470   }
471 
472   //TODO: No adjustment is possible now for 6GHz due to lack of definition in
473   //nl80211.h for NL80211_BAND_6GHZ attribute
474 
475   // Append all attributes to the NL80211_CMD_START_SCHED_SCAN packet.
476   start_sched_scan.AddAttribute(
477       NL80211Attr<uint32_t>(NL80211_ATTR_IFINDEX, interface_index));
478   start_sched_scan.AddAttribute(scan_ssids_attr);
479   // An absence of NL80211_ATTR_SCAN_FREQUENCIES attribue informs kernel to
480   // scan all supported frequencies.
481   if (!freqs.empty()) {
482     start_sched_scan.AddAttribute(freqs_attr);
483   }
484 
485   if (!interval_setting.plans.empty()) {
486     NL80211NestedAttr scan_plans(NL80211_ATTR_SCHED_SCAN_PLANS);
487     for (unsigned int i = 0; i < interval_setting.plans.size(); i++) {
488       NL80211NestedAttr scan_plan(i + 1);
489       scan_plan.AddAttribute(
490           NL80211Attr<uint32_t>(NL80211_SCHED_SCAN_PLAN_INTERVAL,
491                                 interval_setting.plans[i].interval_ms / kMsecPerSec));
492       scan_plan.AddAttribute(
493           NL80211Attr<uint32_t>(NL80211_SCHED_SCAN_PLAN_ITERATIONS,
494                                 interval_setting.plans[i].n_iterations));
495       scan_plans.AddAttribute(scan_plan);
496     }
497     NL80211NestedAttr last_scan_plan(interval_setting.plans.size() + 1);
498     last_scan_plan.AddAttribute(
499         NL80211Attr<uint32_t>(NL80211_SCHED_SCAN_PLAN_INTERVAL,
500                               interval_setting.final_interval_ms / kMsecPerSec));
501     scan_plans.AddAttribute(last_scan_plan);
502     start_sched_scan.AddAttribute(scan_plans);
503   } else {
504     start_sched_scan.AddAttribute(
505         NL80211Attr<uint32_t>(NL80211_ATTR_SCHED_SCAN_INTERVAL,
506                               interval_setting.final_interval_ms));
507   }
508   uint32_t scan_flags = 0;
509   if (req_flags.request_random_mac) {
510     scan_flags |= NL80211_SCAN_FLAG_RANDOM_ADDR;
511   }
512   if (req_flags.request_low_power) {
513     scan_flags |= NL80211_SCAN_FLAG_LOW_POWER;
514   }
515   if (scan_flags) {
516     start_sched_scan.AddAttribute(
517         NL80211Attr<uint32_t>(NL80211_ATTR_SCAN_FLAGS,
518                               scan_flags));
519   }
520 
521   vector<unique_ptr<const NL80211Packet>> response;
522   if (!netlink_manager_->SendMessageAndGetAckOrError(start_sched_scan,
523                                                      error_code)) {
524     // Logging is done inside |SendMessageAndGetAckOrError|.
525     return false;
526   }
527   if (*error_code != 0) {
528     LOG(ERROR) << "NL80211_CMD_START_SCHED_SCAN failed: " << strerror(*error_code);
529     return false;
530   }
531 
532   return true;
533 }
534 
535 }  // namespace wificond
536 }  // namespace android
537