scan.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Scanning implementation
  4. *
  5. * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
  6. * Copyright 2004, Instant802 Networks, Inc.
  7. * Copyright 2005, Devicescape Software, Inc.
  8. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  9. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  10. * Copyright 2013-2015 Intel Mobile Communications GmbH
  11. * Copyright 2016-2017 Intel Deutschland GmbH
  12. * Copyright (C) 2018-2024 Intel Corporation
  13. */
  14. #include <linux/if_arp.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/rtnetlink.h>
  17. #include <net/sch_generic.h>
  18. #include <linux/slab.h>
  19. #include <linux/export.h>
  20. #include <linux/random.h>
  21. #include <net/mac80211.h>
  22. #include "ieee80211_i.h"
  23. #include "driver-ops.h"
  24. #include "mesh.h"
  25. #define IEEE80211_PROBE_DELAY (HZ / 33)
  26. #define IEEE80211_CHANNEL_TIME (HZ / 33)
  27. #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9)
  28. void ieee80211_rx_bss_put(struct ieee80211_local *local,
  29. struct ieee80211_bss *bss)
  30. {
  31. if (!bss)
  32. return;
  33. cfg80211_put_bss(local->hw.wiphy,
  34. container_of((void *)bss, struct cfg80211_bss, priv));
  35. }
  36. static bool is_uapsd_supported(struct ieee802_11_elems *elems)
  37. {
  38. u8 qos_info;
  39. if (elems->wmm_info && elems->wmm_info_len == 7
  40. && elems->wmm_info[5] == 1)
  41. qos_info = elems->wmm_info[6];
  42. else if (elems->wmm_param && elems->wmm_param_len == 24
  43. && elems->wmm_param[5] == 1)
  44. qos_info = elems->wmm_param[6];
  45. else
  46. /* no valid wmm information or parameter element found */
  47. return false;
  48. return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
  49. }
  50. struct inform_bss_update_data {
  51. struct ieee80211_rx_status *rx_status;
  52. bool beacon;
  53. };
  54. void ieee80211_inform_bss(struct wiphy *wiphy,
  55. struct cfg80211_bss *cbss,
  56. const struct cfg80211_bss_ies *ies,
  57. void *data)
  58. {
  59. struct ieee80211_local *local = wiphy_priv(wiphy);
  60. struct inform_bss_update_data *update_data = data;
  61. struct ieee80211_bss *bss = (void *)cbss->priv;
  62. struct ieee80211_rx_status *rx_status;
  63. struct ieee802_11_elems *elems;
  64. int clen, srlen;
  65. /* This happens while joining an IBSS */
  66. if (!update_data)
  67. return;
  68. elems = ieee802_11_parse_elems(ies->data, ies->len, false, NULL);
  69. if (!elems)
  70. return;
  71. rx_status = update_data->rx_status;
  72. if (update_data->beacon)
  73. bss->device_ts_beacon = rx_status->device_timestamp;
  74. else
  75. bss->device_ts_presp = rx_status->device_timestamp;
  76. if (elems->parse_error) {
  77. if (update_data->beacon)
  78. bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;
  79. else
  80. bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;
  81. } else {
  82. if (update_data->beacon)
  83. bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;
  84. else
  85. bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;
  86. }
  87. /* save the ERP value so that it is available at association time */
  88. if (elems->erp_info && (!elems->parse_error ||
  89. !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {
  90. bss->erp_value = elems->erp_info[0];
  91. bss->has_erp_value = true;
  92. if (!elems->parse_error)
  93. bss->valid_data |= IEEE80211_BSS_VALID_ERP;
  94. }
  95. /* replace old supported rates if we get new values */
  96. if (!elems->parse_error ||
  97. !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {
  98. srlen = 0;
  99. if (elems->supp_rates) {
  100. clen = IEEE80211_MAX_SUPP_RATES;
  101. if (clen > elems->supp_rates_len)
  102. clen = elems->supp_rates_len;
  103. memcpy(bss->supp_rates, elems->supp_rates, clen);
  104. srlen += clen;
  105. }
  106. if (elems->ext_supp_rates) {
  107. clen = IEEE80211_MAX_SUPP_RATES - srlen;
  108. if (clen > elems->ext_supp_rates_len)
  109. clen = elems->ext_supp_rates_len;
  110. memcpy(bss->supp_rates + srlen, elems->ext_supp_rates,
  111. clen);
  112. srlen += clen;
  113. }
  114. if (srlen) {
  115. bss->supp_rates_len = srlen;
  116. if (!elems->parse_error)
  117. bss->valid_data |= IEEE80211_BSS_VALID_RATES;
  118. }
  119. }
  120. if (!elems->parse_error ||
  121. !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {
  122. bss->wmm_used = elems->wmm_param || elems->wmm_info;
  123. bss->uapsd_supported = is_uapsd_supported(elems);
  124. if (!elems->parse_error)
  125. bss->valid_data |= IEEE80211_BSS_VALID_WMM;
  126. }
  127. if (update_data->beacon) {
  128. struct ieee80211_supported_band *sband =
  129. local->hw.wiphy->bands[rx_status->band];
  130. if (!(rx_status->encoding == RX_ENC_HT) &&
  131. !(rx_status->encoding == RX_ENC_VHT))
  132. bss->beacon_rate =
  133. &sband->bitrates[rx_status->rate_idx];
  134. }
  135. if (elems->vht_cap_elem)
  136. bss->vht_cap_info =
  137. le32_to_cpu(elems->vht_cap_elem->vht_cap_info);
  138. else
  139. bss->vht_cap_info = 0;
  140. kfree(elems);
  141. }
  142. struct ieee80211_bss *
  143. ieee80211_bss_info_update(struct ieee80211_local *local,
  144. struct ieee80211_rx_status *rx_status,
  145. struct ieee80211_mgmt *mgmt, size_t len,
  146. struct ieee80211_channel *channel)
  147. {
  148. bool beacon = ieee80211_is_beacon(mgmt->frame_control) ||
  149. ieee80211_is_s1g_beacon(mgmt->frame_control);
  150. struct cfg80211_bss *cbss;
  151. struct inform_bss_update_data update_data = {
  152. .rx_status = rx_status,
  153. .beacon = beacon,
  154. };
  155. struct cfg80211_inform_bss bss_meta = {
  156. .boottime_ns = rx_status->boottime_ns,
  157. .drv_data = (void *)&update_data,
  158. };
  159. bool signal_valid;
  160. struct ieee80211_sub_if_data *scan_sdata;
  161. if (rx_status->flag & RX_FLAG_NO_SIGNAL_VAL)
  162. bss_meta.signal = 0; /* invalid signal indication */
  163. else if (ieee80211_hw_check(&local->hw, SIGNAL_DBM))
  164. bss_meta.signal = rx_status->signal * 100;
  165. else if (ieee80211_hw_check(&local->hw, SIGNAL_UNSPEC))
  166. bss_meta.signal = (rx_status->signal * 100) / local->hw.max_signal;
  167. bss_meta.chan = channel;
  168. rcu_read_lock();
  169. scan_sdata = rcu_dereference(local->scan_sdata);
  170. if (scan_sdata && scan_sdata->vif.type == NL80211_IFTYPE_STATION &&
  171. scan_sdata->vif.cfg.assoc &&
  172. ieee80211_have_rx_timestamp(rx_status)) {
  173. struct ieee80211_bss_conf *link_conf = NULL;
  174. /* for an MLO connection, set the TSF data only in case we have
  175. * an indication on which of the links the frame was received
  176. */
  177. if (ieee80211_vif_is_mld(&scan_sdata->vif)) {
  178. if (rx_status->link_valid) {
  179. s8 link_id = rx_status->link_id;
  180. link_conf =
  181. rcu_dereference(scan_sdata->vif.link_conf[link_id]);
  182. }
  183. } else {
  184. link_conf = &scan_sdata->vif.bss_conf;
  185. }
  186. if (link_conf) {
  187. bss_meta.parent_tsf =
  188. ieee80211_calculate_rx_timestamp(local,
  189. rx_status,
  190. len + FCS_LEN,
  191. 24);
  192. ether_addr_copy(bss_meta.parent_bssid,
  193. link_conf->bssid);
  194. }
  195. }
  196. rcu_read_unlock();
  197. cbss = cfg80211_inform_bss_frame_data(local->hw.wiphy, &bss_meta,
  198. mgmt, len, GFP_ATOMIC);
  199. if (!cbss)
  200. return NULL;
  201. /* In case the signal is invalid update the status */
  202. signal_valid = channel == cbss->channel;
  203. if (!signal_valid)
  204. rx_status->flag |= RX_FLAG_NO_SIGNAL_VAL;
  205. return (void *)cbss->priv;
  206. }
  207. static bool ieee80211_scan_accept_presp(struct ieee80211_sub_if_data *sdata,
  208. struct ieee80211_channel *channel,
  209. u32 scan_flags, const u8 *da)
  210. {
  211. if (!sdata)
  212. return false;
  213. /* accept broadcast on 6 GHz and for OCE */
  214. if (is_broadcast_ether_addr(da) &&
  215. (channel->band == NL80211_BAND_6GHZ ||
  216. scan_flags & NL80211_SCAN_FLAG_ACCEPT_BCAST_PROBE_RESP))
  217. return true;
  218. if (scan_flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
  219. return true;
  220. return ether_addr_equal(da, sdata->vif.addr);
  221. }
  222. void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
  223. {
  224. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  225. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  226. struct ieee80211_bss *bss;
  227. struct ieee80211_channel *channel;
  228. size_t min_hdr_len = offsetof(struct ieee80211_mgmt,
  229. u.probe_resp.variable);
  230. if (!ieee80211_is_probe_resp(mgmt->frame_control) &&
  231. !ieee80211_is_beacon(mgmt->frame_control) &&
  232. !ieee80211_is_s1g_beacon(mgmt->frame_control))
  233. return;
  234. if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
  235. if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
  236. min_hdr_len = offsetof(struct ieee80211_ext,
  237. u.s1g_short_beacon.variable);
  238. else
  239. min_hdr_len = offsetof(struct ieee80211_ext,
  240. u.s1g_beacon);
  241. }
  242. if (skb->len < min_hdr_len)
  243. return;
  244. if (test_and_clear_bit(SCAN_BEACON_WAIT, &local->scanning)) {
  245. /*
  246. * we were passive scanning because of radar/no-IR, but
  247. * the beacon/proberesp rx gives us an opportunity to upgrade
  248. * to active scan
  249. */
  250. set_bit(SCAN_BEACON_DONE, &local->scanning);
  251. wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 0);
  252. }
  253. channel = ieee80211_get_channel_khz(local->hw.wiphy,
  254. ieee80211_rx_status_to_khz(rx_status));
  255. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  256. return;
  257. if (ieee80211_is_probe_resp(mgmt->frame_control)) {
  258. struct ieee80211_sub_if_data *sdata1, *sdata2;
  259. struct cfg80211_scan_request *scan_req;
  260. struct cfg80211_sched_scan_request *sched_scan_req;
  261. u32 scan_req_flags = 0, sched_scan_req_flags = 0;
  262. sdata1 = rcu_dereference(local->scan_sdata);
  263. sdata2 = rcu_dereference(local->sched_scan_sdata);
  264. if (likely(!sdata1 && !sdata2))
  265. return;
  266. scan_req = rcu_dereference(local->scan_req);
  267. sched_scan_req = rcu_dereference(local->sched_scan_req);
  268. if (scan_req)
  269. scan_req_flags = scan_req->flags;
  270. if (sched_scan_req)
  271. sched_scan_req_flags = sched_scan_req->flags;
  272. /* ignore ProbeResp to foreign address or non-bcast (OCE)
  273. * unless scanning with randomised address
  274. */
  275. if (!ieee80211_scan_accept_presp(sdata1, channel,
  276. scan_req_flags,
  277. mgmt->da) &&
  278. !ieee80211_scan_accept_presp(sdata2, channel,
  279. sched_scan_req_flags,
  280. mgmt->da))
  281. return;
  282. } else {
  283. /* Beacons are expected only with broadcast address */
  284. if (!is_broadcast_ether_addr(mgmt->da))
  285. return;
  286. }
  287. /* Do not update the BSS table in case of only monitor interfaces */
  288. if (local->open_count == local->monitors)
  289. return;
  290. bss = ieee80211_bss_info_update(local, rx_status,
  291. mgmt, skb->len,
  292. channel);
  293. if (bss)
  294. ieee80211_rx_bss_put(local, bss);
  295. }
  296. static void ieee80211_prepare_scan_chandef(struct cfg80211_chan_def *chandef)
  297. {
  298. memset(chandef, 0, sizeof(*chandef));
  299. chandef->width = NL80211_CHAN_WIDTH_20_NOHT;
  300. }
  301. /* return false if no more work */
  302. static bool ieee80211_prep_hw_scan(struct ieee80211_sub_if_data *sdata)
  303. {
  304. struct ieee80211_local *local = sdata->local;
  305. struct cfg80211_scan_request *req;
  306. struct cfg80211_chan_def chandef;
  307. u8 bands_used = 0;
  308. int i, ielen;
  309. u32 *n_chans;
  310. u32 flags = 0;
  311. req = rcu_dereference_protected(local->scan_req,
  312. lockdep_is_held(&local->hw.wiphy->mtx));
  313. if (test_bit(SCAN_HW_CANCELLED, &local->scanning))
  314. return false;
  315. if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
  316. local->hw_scan_req->req.n_channels = req->n_channels;
  317. for (i = 0; i < req->n_channels; i++) {
  318. local->hw_scan_req->req.channels[i] = req->channels[i];
  319. bands_used |= BIT(req->channels[i]->band);
  320. }
  321. } else {
  322. do {
  323. if (local->hw_scan_band == NUM_NL80211_BANDS)
  324. return false;
  325. n_chans = &local->hw_scan_req->req.n_channels;
  326. *n_chans = 0;
  327. for (i = 0; i < req->n_channels; i++) {
  328. if (req->channels[i]->band !=
  329. local->hw_scan_band)
  330. continue;
  331. local->hw_scan_req->req.channels[(*n_chans)++] =
  332. req->channels[i];
  333. bands_used |= BIT(req->channels[i]->band);
  334. }
  335. local->hw_scan_band++;
  336. } while (!*n_chans);
  337. }
  338. ieee80211_prepare_scan_chandef(&chandef);
  339. if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
  340. flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
  341. ielen = ieee80211_build_preq_ies(sdata,
  342. (u8 *)local->hw_scan_req->req.ie,
  343. local->hw_scan_ies_bufsize,
  344. &local->hw_scan_req->ies,
  345. req->ie, req->ie_len,
  346. bands_used, req->rates, &chandef,
  347. flags);
  348. if (ielen < 0)
  349. return false;
  350. local->hw_scan_req->req.ie_len = ielen;
  351. local->hw_scan_req->req.no_cck = req->no_cck;
  352. ether_addr_copy(local->hw_scan_req->req.mac_addr, req->mac_addr);
  353. ether_addr_copy(local->hw_scan_req->req.mac_addr_mask,
  354. req->mac_addr_mask);
  355. ether_addr_copy(local->hw_scan_req->req.bssid, req->bssid);
  356. return true;
  357. }
  358. static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
  359. {
  360. struct ieee80211_local *local = hw_to_local(hw);
  361. bool hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
  362. bool was_scanning = local->scanning;
  363. struct cfg80211_scan_request *scan_req;
  364. struct ieee80211_sub_if_data *scan_sdata;
  365. struct ieee80211_sub_if_data *sdata;
  366. lockdep_assert_wiphy(local->hw.wiphy);
  367. /*
  368. * It's ok to abort a not-yet-running scan (that
  369. * we have one at all will be verified by checking
  370. * local->scan_req next), but not to complete it
  371. * successfully.
  372. */
  373. if (WARN_ON(!local->scanning && !aborted))
  374. aborted = true;
  375. if (WARN_ON(!local->scan_req))
  376. return;
  377. scan_sdata = rcu_dereference_protected(local->scan_sdata,
  378. lockdep_is_held(&local->hw.wiphy->mtx));
  379. if (hw_scan && !aborted &&
  380. !ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS) &&
  381. ieee80211_prep_hw_scan(scan_sdata)) {
  382. int rc;
  383. rc = drv_hw_scan(local,
  384. rcu_dereference_protected(local->scan_sdata,
  385. lockdep_is_held(&local->hw.wiphy->mtx)),
  386. local->hw_scan_req);
  387. if (rc == 0)
  388. return;
  389. /* HW scan failed and is going to be reported as aborted,
  390. * so clear old scan info.
  391. */
  392. memset(&local->scan_info, 0, sizeof(local->scan_info));
  393. aborted = true;
  394. }
  395. kfree(local->hw_scan_req);
  396. local->hw_scan_req = NULL;
  397. scan_req = rcu_dereference_protected(local->scan_req,
  398. lockdep_is_held(&local->hw.wiphy->mtx));
  399. RCU_INIT_POINTER(local->scan_req, NULL);
  400. RCU_INIT_POINTER(local->scan_sdata, NULL);
  401. local->scanning = 0;
  402. local->scan_chandef.chan = NULL;
  403. synchronize_rcu();
  404. if (scan_req != local->int_scan_req) {
  405. local->scan_info.aborted = aborted;
  406. cfg80211_scan_done(scan_req, &local->scan_info);
  407. }
  408. /* Set power back to normal operating levels. */
  409. ieee80211_hw_conf_chan(local);
  410. if (!hw_scan && was_scanning) {
  411. ieee80211_configure_filter(local);
  412. drv_sw_scan_complete(local, scan_sdata);
  413. ieee80211_offchannel_return(local);
  414. }
  415. ieee80211_recalc_idle(local);
  416. ieee80211_mlme_notify_scan_completed(local);
  417. ieee80211_ibss_notify_scan_completed(local);
  418. /* Requeue all the work that might have been ignored while
  419. * the scan was in progress; if there was none this will
  420. * just be a no-op for the particular interface.
  421. */
  422. list_for_each_entry(sdata, &local->interfaces, list) {
  423. if (ieee80211_sdata_running(sdata))
  424. wiphy_work_queue(sdata->local->hw.wiphy, &sdata->work);
  425. }
  426. if (was_scanning)
  427. ieee80211_start_next_roc(local);
  428. }
  429. void ieee80211_scan_completed(struct ieee80211_hw *hw,
  430. struct cfg80211_scan_info *info)
  431. {
  432. struct ieee80211_local *local = hw_to_local(hw);
  433. trace_api_scan_completed(local, info->aborted);
  434. set_bit(SCAN_COMPLETED, &local->scanning);
  435. if (info->aborted)
  436. set_bit(SCAN_ABORTED, &local->scanning);
  437. memcpy(&local->scan_info, info, sizeof(*info));
  438. wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 0);
  439. }
  440. EXPORT_SYMBOL(ieee80211_scan_completed);
  441. static int ieee80211_start_sw_scan(struct ieee80211_local *local,
  442. struct ieee80211_sub_if_data *sdata)
  443. {
  444. /* Software scan is not supported in multi-channel cases */
  445. if (!local->emulate_chanctx)
  446. return -EOPNOTSUPP;
  447. /*
  448. * Hardware/driver doesn't support hw_scan, so use software
  449. * scanning instead. First send a nullfunc frame with power save
  450. * bit on so that AP will buffer the frames for us while we are not
  451. * listening, then send probe requests to each channel and wait for
  452. * the responses. After all channels are scanned, tune back to the
  453. * original channel and send a nullfunc frame with power save bit
  454. * off to trigger the AP to send us all the buffered frames.
  455. *
  456. * Note that while local->sw_scanning is true everything else but
  457. * nullfunc frames and probe requests will be dropped in
  458. * ieee80211_tx_h_check_assoc().
  459. */
  460. drv_sw_scan_start(local, sdata, local->scan_addr);
  461. local->leave_oper_channel_time = jiffies;
  462. local->next_scan_state = SCAN_DECISION;
  463. local->scan_channel_idx = 0;
  464. ieee80211_offchannel_stop_vifs(local);
  465. /* ensure nullfunc is transmitted before leaving operating channel */
  466. ieee80211_flush_queues(local, NULL, false);
  467. ieee80211_configure_filter(local);
  468. /* We need to set power level at maximum rate for scanning. */
  469. ieee80211_hw_conf_chan(local);
  470. wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work, 0);
  471. return 0;
  472. }
  473. static bool __ieee80211_can_leave_ch(struct ieee80211_sub_if_data *sdata)
  474. {
  475. struct ieee80211_local *local = sdata->local;
  476. struct ieee80211_sub_if_data *sdata_iter;
  477. unsigned int link_id;
  478. lockdep_assert_wiphy(local->hw.wiphy);
  479. if (!ieee80211_is_radar_required(local))
  480. return true;
  481. if (!regulatory_pre_cac_allowed(local->hw.wiphy))
  482. return false;
  483. list_for_each_entry(sdata_iter, &local->interfaces, list) {
  484. for_each_valid_link(&sdata_iter->wdev, link_id)
  485. if (sdata_iter->wdev.links[link_id].cac_started)
  486. return false;
  487. }
  488. return true;
  489. }
  490. static bool ieee80211_can_scan(struct ieee80211_local *local,
  491. struct ieee80211_sub_if_data *sdata)
  492. {
  493. if (!__ieee80211_can_leave_ch(sdata))
  494. return false;
  495. if (!list_empty(&local->roc_list))
  496. return false;
  497. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  498. sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL)
  499. return false;
  500. return true;
  501. }
  502. void ieee80211_run_deferred_scan(struct ieee80211_local *local)
  503. {
  504. lockdep_assert_wiphy(local->hw.wiphy);
  505. if (!local->scan_req || local->scanning)
  506. return;
  507. if (!ieee80211_can_scan(local,
  508. rcu_dereference_protected(
  509. local->scan_sdata,
  510. lockdep_is_held(&local->hw.wiphy->mtx))))
  511. return;
  512. wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work,
  513. round_jiffies_relative(0));
  514. }
  515. static void ieee80211_send_scan_probe_req(struct ieee80211_sub_if_data *sdata,
  516. const u8 *src, const u8 *dst,
  517. const u8 *ssid, size_t ssid_len,
  518. const u8 *ie, size_t ie_len,
  519. u32 ratemask, u32 flags, u32 tx_flags,
  520. struct ieee80211_channel *channel)
  521. {
  522. struct sk_buff *skb;
  523. skb = ieee80211_build_probe_req(sdata, src, dst, ratemask, channel,
  524. ssid, ssid_len,
  525. ie, ie_len, flags);
  526. if (skb) {
  527. if (flags & IEEE80211_PROBE_FLAG_RANDOM_SN) {
  528. struct ieee80211_hdr *hdr = (void *)skb->data;
  529. struct ieee80211_tx_info *info = IEEE80211_SKB_CB(skb);
  530. u16 sn = get_random_u16();
  531. info->control.flags |= IEEE80211_TX_CTRL_NO_SEQNO;
  532. hdr->seq_ctrl =
  533. cpu_to_le16(IEEE80211_SN_TO_SEQ(sn));
  534. }
  535. IEEE80211_SKB_CB(skb)->flags |= tx_flags;
  536. IEEE80211_SKB_CB(skb)->control.flags |= IEEE80211_TX_CTRL_DONT_USE_RATE_MASK;
  537. ieee80211_tx_skb_tid_band(sdata, skb, 7, channel->band);
  538. }
  539. }
  540. static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
  541. unsigned long *next_delay)
  542. {
  543. int i;
  544. struct ieee80211_sub_if_data *sdata;
  545. struct cfg80211_scan_request *scan_req;
  546. enum nl80211_band band = local->hw.conf.chandef.chan->band;
  547. u32 flags = 0, tx_flags;
  548. scan_req = rcu_dereference_protected(local->scan_req,
  549. lockdep_is_held(&local->hw.wiphy->mtx));
  550. tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
  551. if (scan_req->no_cck)
  552. tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
  553. if (scan_req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
  554. flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
  555. if (scan_req->flags & NL80211_SCAN_FLAG_RANDOM_SN)
  556. flags |= IEEE80211_PROBE_FLAG_RANDOM_SN;
  557. sdata = rcu_dereference_protected(local->scan_sdata,
  558. lockdep_is_held(&local->hw.wiphy->mtx));
  559. for (i = 0; i < scan_req->n_ssids; i++)
  560. ieee80211_send_scan_probe_req(
  561. sdata, local->scan_addr, scan_req->bssid,
  562. scan_req->ssids[i].ssid, scan_req->ssids[i].ssid_len,
  563. scan_req->ie, scan_req->ie_len,
  564. scan_req->rates[band], flags,
  565. tx_flags, local->hw.conf.chandef.chan);
  566. /*
  567. * After sending probe requests, wait for probe responses
  568. * on the channel.
  569. */
  570. *next_delay = msecs_to_jiffies(scan_req->duration) >
  571. IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME ?
  572. msecs_to_jiffies(scan_req->duration) - IEEE80211_PROBE_DELAY :
  573. IEEE80211_CHANNEL_TIME;
  574. local->next_scan_state = SCAN_DECISION;
  575. }
  576. static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
  577. struct cfg80211_scan_request *req)
  578. {
  579. struct ieee80211_local *local = sdata->local;
  580. bool hw_scan = local->ops->hw_scan;
  581. int rc;
  582. lockdep_assert_wiphy(local->hw.wiphy);
  583. if (local->scan_req)
  584. return -EBUSY;
  585. /* For an MLO connection, if a link ID was specified, validate that it
  586. * is indeed active.
  587. */
  588. if (ieee80211_vif_is_mld(&sdata->vif) && req->tsf_report_link_id >= 0 &&
  589. !(sdata->vif.active_links & BIT(req->tsf_report_link_id)))
  590. return -EINVAL;
  591. if (!__ieee80211_can_leave_ch(sdata))
  592. return -EBUSY;
  593. if (!ieee80211_can_scan(local, sdata)) {
  594. /* wait for the work to finish/time out */
  595. rcu_assign_pointer(local->scan_req, req);
  596. rcu_assign_pointer(local->scan_sdata, sdata);
  597. return 0;
  598. }
  599. again:
  600. if (hw_scan) {
  601. u8 *ies;
  602. local->hw_scan_ies_bufsize = local->scan_ies_len + req->ie_len;
  603. if (ieee80211_hw_check(&local->hw, SINGLE_SCAN_ON_ALL_BANDS)) {
  604. int i, n_bands = 0;
  605. u8 bands_counted = 0;
  606. for (i = 0; i < req->n_channels; i++) {
  607. if (bands_counted & BIT(req->channels[i]->band))
  608. continue;
  609. bands_counted |= BIT(req->channels[i]->band);
  610. n_bands++;
  611. }
  612. local->hw_scan_ies_bufsize *= n_bands;
  613. }
  614. local->hw_scan_req = kmalloc(struct_size(local->hw_scan_req,
  615. req.channels,
  616. req->n_channels) +
  617. local->hw_scan_ies_bufsize,
  618. GFP_KERNEL);
  619. if (!local->hw_scan_req)
  620. return -ENOMEM;
  621. local->hw_scan_req->req.ssids = req->ssids;
  622. local->hw_scan_req->req.n_ssids = req->n_ssids;
  623. /* None of the channels are actually set
  624. * up but let UBSAN know the boundaries.
  625. */
  626. local->hw_scan_req->req.n_channels = req->n_channels;
  627. ies = (u8 *)local->hw_scan_req +
  628. sizeof(*local->hw_scan_req) +
  629. req->n_channels * sizeof(req->channels[0]);
  630. local->hw_scan_req->req.ie = ies;
  631. local->hw_scan_req->req.flags = req->flags;
  632. eth_broadcast_addr(local->hw_scan_req->req.bssid);
  633. local->hw_scan_req->req.duration = req->duration;
  634. local->hw_scan_req->req.duration_mandatory =
  635. req->duration_mandatory;
  636. local->hw_scan_req->req.tsf_report_link_id =
  637. req->tsf_report_link_id;
  638. local->hw_scan_band = 0;
  639. local->hw_scan_req->req.n_6ghz_params = req->n_6ghz_params;
  640. local->hw_scan_req->req.scan_6ghz_params =
  641. req->scan_6ghz_params;
  642. local->hw_scan_req->req.scan_6ghz = req->scan_6ghz;
  643. /*
  644. * After allocating local->hw_scan_req, we must
  645. * go through until ieee80211_prep_hw_scan(), so
  646. * anything that might be changed here and leave
  647. * this function early must not go after this
  648. * allocation.
  649. */
  650. }
  651. rcu_assign_pointer(local->scan_req, req);
  652. rcu_assign_pointer(local->scan_sdata, sdata);
  653. if (req->flags & NL80211_SCAN_FLAG_RANDOM_ADDR)
  654. get_random_mask_addr(local->scan_addr,
  655. req->mac_addr,
  656. req->mac_addr_mask);
  657. else
  658. memcpy(local->scan_addr, sdata->vif.addr, ETH_ALEN);
  659. if (hw_scan) {
  660. __set_bit(SCAN_HW_SCANNING, &local->scanning);
  661. } else if ((req->n_channels == 1) &&
  662. (req->channels[0] == local->hw.conf.chandef.chan)) {
  663. /*
  664. * If we are scanning only on the operating channel
  665. * then we do not need to stop normal activities
  666. */
  667. unsigned long next_delay;
  668. __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
  669. ieee80211_recalc_idle(local);
  670. /* Notify driver scan is starting, keep order of operations
  671. * same as normal software scan, in case that matters. */
  672. drv_sw_scan_start(local, sdata, local->scan_addr);
  673. ieee80211_configure_filter(local); /* accept probe-responses */
  674. /* We need to ensure power level is at max for scanning. */
  675. ieee80211_hw_conf_chan(local);
  676. if ((req->channels[0]->flags & (IEEE80211_CHAN_NO_IR |
  677. IEEE80211_CHAN_RADAR)) ||
  678. !req->n_ssids) {
  679. next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  680. if (req->n_ssids)
  681. set_bit(SCAN_BEACON_WAIT, &local->scanning);
  682. } else {
  683. ieee80211_scan_state_send_probe(local, &next_delay);
  684. next_delay = IEEE80211_CHANNEL_TIME;
  685. }
  686. /* Now, just wait a bit and we are all done! */
  687. wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work,
  688. next_delay);
  689. return 0;
  690. } else {
  691. /* Do normal software scan */
  692. __set_bit(SCAN_SW_SCANNING, &local->scanning);
  693. }
  694. ieee80211_recalc_idle(local);
  695. if (hw_scan) {
  696. WARN_ON(!ieee80211_prep_hw_scan(sdata));
  697. rc = drv_hw_scan(local, sdata, local->hw_scan_req);
  698. } else {
  699. rc = ieee80211_start_sw_scan(local, sdata);
  700. }
  701. if (rc) {
  702. kfree(local->hw_scan_req);
  703. local->hw_scan_req = NULL;
  704. local->scanning = 0;
  705. ieee80211_recalc_idle(local);
  706. local->scan_req = NULL;
  707. RCU_INIT_POINTER(local->scan_sdata, NULL);
  708. }
  709. if (hw_scan && rc == 1) {
  710. /*
  711. * we can't fall back to software for P2P-GO
  712. * as it must update NoA etc.
  713. */
  714. if (ieee80211_vif_type_p2p(&sdata->vif) ==
  715. NL80211_IFTYPE_P2P_GO)
  716. return -EOPNOTSUPP;
  717. hw_scan = false;
  718. goto again;
  719. }
  720. return rc;
  721. }
  722. static unsigned long
  723. ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
  724. {
  725. /*
  726. * TODO: channel switching also consumes quite some time,
  727. * add that delay as well to get a better estimation
  728. */
  729. if (chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR))
  730. return IEEE80211_PASSIVE_CHANNEL_TIME;
  731. return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
  732. }
  733. static void ieee80211_scan_state_decision(struct ieee80211_local *local,
  734. unsigned long *next_delay)
  735. {
  736. bool associated = false;
  737. bool tx_empty = true;
  738. bool bad_latency;
  739. struct ieee80211_sub_if_data *sdata;
  740. struct ieee80211_channel *next_chan;
  741. enum mac80211_scan_state next_scan_state;
  742. struct cfg80211_scan_request *scan_req;
  743. lockdep_assert_wiphy(local->hw.wiphy);
  744. /*
  745. * check if at least one STA interface is associated,
  746. * check if at least one STA interface has pending tx frames
  747. * and grab the lowest used beacon interval
  748. */
  749. list_for_each_entry(sdata, &local->interfaces, list) {
  750. if (!ieee80211_sdata_running(sdata))
  751. continue;
  752. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  753. if (sdata->u.mgd.associated) {
  754. associated = true;
  755. if (!qdisc_all_tx_empty(sdata->dev)) {
  756. tx_empty = false;
  757. break;
  758. }
  759. }
  760. }
  761. }
  762. scan_req = rcu_dereference_protected(local->scan_req,
  763. lockdep_is_held(&local->hw.wiphy->mtx));
  764. next_chan = scan_req->channels[local->scan_channel_idx];
  765. /*
  766. * we're currently scanning a different channel, let's
  767. * see if we can scan another channel without interfering
  768. * with the current traffic situation.
  769. *
  770. * Keep good latency, do not stay off-channel more than 125 ms.
  771. */
  772. bad_latency = time_after(jiffies +
  773. ieee80211_scan_get_channel_time(next_chan),
  774. local->leave_oper_channel_time + HZ / 8);
  775. if (associated && !tx_empty) {
  776. if (scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
  777. next_scan_state = SCAN_ABORT;
  778. else
  779. next_scan_state = SCAN_SUSPEND;
  780. } else if (associated && bad_latency) {
  781. next_scan_state = SCAN_SUSPEND;
  782. } else {
  783. next_scan_state = SCAN_SET_CHANNEL;
  784. }
  785. local->next_scan_state = next_scan_state;
  786. *next_delay = 0;
  787. }
  788. static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
  789. unsigned long *next_delay)
  790. {
  791. int skip;
  792. struct ieee80211_channel *chan;
  793. struct cfg80211_scan_request *scan_req;
  794. scan_req = rcu_dereference_protected(local->scan_req,
  795. lockdep_is_held(&local->hw.wiphy->mtx));
  796. skip = 0;
  797. chan = scan_req->channels[local->scan_channel_idx];
  798. local->scan_chandef.chan = chan;
  799. local->scan_chandef.center_freq1 = chan->center_freq;
  800. local->scan_chandef.freq1_offset = chan->freq_offset;
  801. local->scan_chandef.center_freq2 = 0;
  802. /* For scanning on the S1G band, detect the channel width according to
  803. * the channel being scanned.
  804. */
  805. if (chan->band == NL80211_BAND_S1GHZ) {
  806. local->scan_chandef.width = ieee80211_s1g_channel_width(chan);
  807. goto set_channel;
  808. }
  809. /* If scanning on oper channel, use whatever channel-type
  810. * is currently in use.
  811. */
  812. if (chan == local->hw.conf.chandef.chan)
  813. local->scan_chandef = local->hw.conf.chandef;
  814. else
  815. local->scan_chandef.width = NL80211_CHAN_WIDTH_20_NOHT;
  816. set_channel:
  817. if (ieee80211_hw_conf_chan(local))
  818. skip = 1;
  819. /* advance state machine to next channel/band */
  820. local->scan_channel_idx++;
  821. if (skip) {
  822. /* if we skip this channel return to the decision state */
  823. local->next_scan_state = SCAN_DECISION;
  824. return;
  825. }
  826. /*
  827. * Probe delay is used to update the NAV, cf. 11.1.3.2.2
  828. * (which unfortunately doesn't say _why_ step a) is done,
  829. * but it waits for the probe delay or until a frame is
  830. * received - and the received frame would update the NAV).
  831. * For now, we do not support waiting until a frame is
  832. * received.
  833. *
  834. * In any case, it is not necessary for a passive scan.
  835. */
  836. if ((chan->flags & (IEEE80211_CHAN_NO_IR | IEEE80211_CHAN_RADAR)) ||
  837. !scan_req->n_ssids) {
  838. *next_delay = max(msecs_to_jiffies(scan_req->duration),
  839. IEEE80211_PASSIVE_CHANNEL_TIME);
  840. local->next_scan_state = SCAN_DECISION;
  841. if (scan_req->n_ssids)
  842. set_bit(SCAN_BEACON_WAIT, &local->scanning);
  843. return;
  844. }
  845. /* active scan, send probes */
  846. *next_delay = IEEE80211_PROBE_DELAY;
  847. local->next_scan_state = SCAN_SEND_PROBE;
  848. }
  849. static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
  850. unsigned long *next_delay)
  851. {
  852. /* switch back to the operating channel */
  853. local->scan_chandef.chan = NULL;
  854. ieee80211_hw_conf_chan(local);
  855. /* disable PS */
  856. ieee80211_offchannel_return(local);
  857. *next_delay = HZ / 5;
  858. /* afterwards, resume scan & go to next channel */
  859. local->next_scan_state = SCAN_RESUME;
  860. }
  861. static void ieee80211_scan_state_resume(struct ieee80211_local *local,
  862. unsigned long *next_delay)
  863. {
  864. ieee80211_offchannel_stop_vifs(local);
  865. if (local->ops->flush) {
  866. ieee80211_flush_queues(local, NULL, false);
  867. *next_delay = 0;
  868. } else
  869. *next_delay = HZ / 10;
  870. /* remember when we left the operating channel */
  871. local->leave_oper_channel_time = jiffies;
  872. /* advance to the next channel to be scanned */
  873. local->next_scan_state = SCAN_SET_CHANNEL;
  874. }
  875. void ieee80211_scan_work(struct wiphy *wiphy, struct wiphy_work *work)
  876. {
  877. struct ieee80211_local *local =
  878. container_of(work, struct ieee80211_local, scan_work.work);
  879. struct ieee80211_sub_if_data *sdata;
  880. struct cfg80211_scan_request *scan_req;
  881. unsigned long next_delay = 0;
  882. bool aborted;
  883. lockdep_assert_wiphy(local->hw.wiphy);
  884. if (!ieee80211_can_run_worker(local)) {
  885. aborted = true;
  886. goto out_complete;
  887. }
  888. sdata = rcu_dereference_protected(local->scan_sdata,
  889. lockdep_is_held(&local->hw.wiphy->mtx));
  890. scan_req = rcu_dereference_protected(local->scan_req,
  891. lockdep_is_held(&local->hw.wiphy->mtx));
  892. /* When scanning on-channel, the first-callback means completed. */
  893. if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
  894. aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
  895. goto out_complete;
  896. }
  897. if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
  898. aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
  899. goto out_complete;
  900. }
  901. if (!sdata || !scan_req)
  902. return;
  903. if (!local->scanning) {
  904. int rc;
  905. RCU_INIT_POINTER(local->scan_req, NULL);
  906. RCU_INIT_POINTER(local->scan_sdata, NULL);
  907. rc = __ieee80211_start_scan(sdata, scan_req);
  908. if (!rc)
  909. return;
  910. /* need to complete scan in cfg80211 */
  911. rcu_assign_pointer(local->scan_req, scan_req);
  912. aborted = true;
  913. goto out_complete;
  914. }
  915. clear_bit(SCAN_BEACON_WAIT, &local->scanning);
  916. /*
  917. * as long as no delay is required advance immediately
  918. * without scheduling a new work
  919. */
  920. do {
  921. if (!ieee80211_sdata_running(sdata)) {
  922. aborted = true;
  923. goto out_complete;
  924. }
  925. if (test_and_clear_bit(SCAN_BEACON_DONE, &local->scanning) &&
  926. local->next_scan_state == SCAN_DECISION)
  927. local->next_scan_state = SCAN_SEND_PROBE;
  928. switch (local->next_scan_state) {
  929. case SCAN_DECISION:
  930. /* if no more bands/channels left, complete scan */
  931. if (local->scan_channel_idx >= scan_req->n_channels) {
  932. aborted = false;
  933. goto out_complete;
  934. }
  935. ieee80211_scan_state_decision(local, &next_delay);
  936. break;
  937. case SCAN_SET_CHANNEL:
  938. ieee80211_scan_state_set_channel(local, &next_delay);
  939. break;
  940. case SCAN_SEND_PROBE:
  941. ieee80211_scan_state_send_probe(local, &next_delay);
  942. break;
  943. case SCAN_SUSPEND:
  944. ieee80211_scan_state_suspend(local, &next_delay);
  945. break;
  946. case SCAN_RESUME:
  947. ieee80211_scan_state_resume(local, &next_delay);
  948. break;
  949. case SCAN_ABORT:
  950. aborted = true;
  951. goto out_complete;
  952. }
  953. } while (next_delay == 0);
  954. wiphy_delayed_work_queue(local->hw.wiphy, &local->scan_work,
  955. next_delay);
  956. return;
  957. out_complete:
  958. __ieee80211_scan_completed(&local->hw, aborted);
  959. }
  960. int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
  961. struct cfg80211_scan_request *req)
  962. {
  963. lockdep_assert_wiphy(sdata->local->hw.wiphy);
  964. return __ieee80211_start_scan(sdata, req);
  965. }
  966. int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
  967. const u8 *ssid, u8 ssid_len,
  968. struct ieee80211_channel **channels,
  969. unsigned int n_channels)
  970. {
  971. struct ieee80211_local *local = sdata->local;
  972. int ret = -EBUSY, i, n_ch = 0;
  973. enum nl80211_band band;
  974. lockdep_assert_wiphy(local->hw.wiphy);
  975. /* busy scanning */
  976. if (local->scan_req)
  977. goto unlock;
  978. /* fill internal scan request */
  979. if (!channels) {
  980. int max_n;
  981. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  982. if (!local->hw.wiphy->bands[band] ||
  983. band == NL80211_BAND_6GHZ)
  984. continue;
  985. max_n = local->hw.wiphy->bands[band]->n_channels;
  986. for (i = 0; i < max_n; i++) {
  987. struct ieee80211_channel *tmp_ch =
  988. &local->hw.wiphy->bands[band]->channels[i];
  989. if (tmp_ch->flags & (IEEE80211_CHAN_NO_IR |
  990. IEEE80211_CHAN_DISABLED))
  991. continue;
  992. local->int_scan_req->channels[n_ch] = tmp_ch;
  993. n_ch++;
  994. }
  995. }
  996. if (WARN_ON_ONCE(n_ch == 0))
  997. goto unlock;
  998. local->int_scan_req->n_channels = n_ch;
  999. } else {
  1000. for (i = 0; i < n_channels; i++) {
  1001. if (channels[i]->flags & (IEEE80211_CHAN_NO_IR |
  1002. IEEE80211_CHAN_DISABLED))
  1003. continue;
  1004. local->int_scan_req->channels[n_ch] = channels[i];
  1005. n_ch++;
  1006. }
  1007. if (WARN_ON_ONCE(n_ch == 0))
  1008. goto unlock;
  1009. local->int_scan_req->n_channels = n_ch;
  1010. }
  1011. local->int_scan_req->ssids = &local->scan_ssid;
  1012. local->int_scan_req->n_ssids = 1;
  1013. memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
  1014. local->int_scan_req->ssids[0].ssid_len = ssid_len;
  1015. ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
  1016. unlock:
  1017. return ret;
  1018. }
  1019. void ieee80211_scan_cancel(struct ieee80211_local *local)
  1020. {
  1021. /* ensure a new scan cannot be queued */
  1022. lockdep_assert_wiphy(local->hw.wiphy);
  1023. /*
  1024. * We are canceling software scan, or deferred scan that was not
  1025. * yet really started (see __ieee80211_start_scan ).
  1026. *
  1027. * Regarding hardware scan:
  1028. * - we can not call __ieee80211_scan_completed() as when
  1029. * SCAN_HW_SCANNING bit is set this function change
  1030. * local->hw_scan_req to operate on 5G band, what race with
  1031. * driver which can use local->hw_scan_req
  1032. *
  1033. * - we can not cancel scan_work since driver can schedule it
  1034. * by ieee80211_scan_completed(..., true) to finish scan
  1035. *
  1036. * Hence we only call the cancel_hw_scan() callback, but the low-level
  1037. * driver is still responsible for calling ieee80211_scan_completed()
  1038. * after the scan was completed/aborted.
  1039. */
  1040. if (!local->scan_req)
  1041. return;
  1042. /*
  1043. * We have a scan running and the driver already reported completion,
  1044. * but the worker hasn't run yet or is stuck on the mutex - mark it as
  1045. * cancelled.
  1046. */
  1047. if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
  1048. test_bit(SCAN_COMPLETED, &local->scanning)) {
  1049. set_bit(SCAN_HW_CANCELLED, &local->scanning);
  1050. return;
  1051. }
  1052. if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
  1053. /*
  1054. * Make sure that __ieee80211_scan_completed doesn't trigger a
  1055. * scan on another band.
  1056. */
  1057. set_bit(SCAN_HW_CANCELLED, &local->scanning);
  1058. if (local->ops->cancel_hw_scan)
  1059. drv_cancel_hw_scan(local,
  1060. rcu_dereference_protected(local->scan_sdata,
  1061. lockdep_is_held(&local->hw.wiphy->mtx)));
  1062. return;
  1063. }
  1064. wiphy_delayed_work_cancel(local->hw.wiphy, &local->scan_work);
  1065. /* and clean up */
  1066. memset(&local->scan_info, 0, sizeof(local->scan_info));
  1067. __ieee80211_scan_completed(&local->hw, true);
  1068. }
  1069. int __ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
  1070. struct cfg80211_sched_scan_request *req)
  1071. {
  1072. struct ieee80211_local *local = sdata->local;
  1073. struct ieee80211_scan_ies sched_scan_ies = {};
  1074. struct cfg80211_chan_def chandef;
  1075. int ret, i, iebufsz, num_bands = 0;
  1076. u32 rate_masks[NUM_NL80211_BANDS] = {};
  1077. u8 bands_used = 0;
  1078. u8 *ie;
  1079. u32 flags = 0;
  1080. lockdep_assert_wiphy(local->hw.wiphy);
  1081. iebufsz = local->scan_ies_len + req->ie_len;
  1082. if (!local->ops->sched_scan_start)
  1083. return -EOPNOTSUPP;
  1084. for (i = 0; i < NUM_NL80211_BANDS; i++) {
  1085. if (local->hw.wiphy->bands[i]) {
  1086. bands_used |= BIT(i);
  1087. rate_masks[i] = (u32) -1;
  1088. num_bands++;
  1089. }
  1090. }
  1091. if (req->flags & NL80211_SCAN_FLAG_MIN_PREQ_CONTENT)
  1092. flags |= IEEE80211_PROBE_FLAG_MIN_CONTENT;
  1093. ie = kcalloc(iebufsz, num_bands, GFP_KERNEL);
  1094. if (!ie) {
  1095. ret = -ENOMEM;
  1096. goto out;
  1097. }
  1098. ieee80211_prepare_scan_chandef(&chandef);
  1099. ret = ieee80211_build_preq_ies(sdata, ie, num_bands * iebufsz,
  1100. &sched_scan_ies, req->ie,
  1101. req->ie_len, bands_used, rate_masks,
  1102. &chandef, flags);
  1103. if (ret < 0)
  1104. goto error;
  1105. ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
  1106. if (ret == 0) {
  1107. rcu_assign_pointer(local->sched_scan_sdata, sdata);
  1108. rcu_assign_pointer(local->sched_scan_req, req);
  1109. }
  1110. error:
  1111. kfree(ie);
  1112. out:
  1113. if (ret) {
  1114. /* Clean in case of failure after HW restart or upon resume. */
  1115. RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
  1116. RCU_INIT_POINTER(local->sched_scan_req, NULL);
  1117. }
  1118. return ret;
  1119. }
  1120. int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
  1121. struct cfg80211_sched_scan_request *req)
  1122. {
  1123. struct ieee80211_local *local = sdata->local;
  1124. lockdep_assert_wiphy(local->hw.wiphy);
  1125. if (rcu_access_pointer(local->sched_scan_sdata))
  1126. return -EBUSY;
  1127. return __ieee80211_request_sched_scan_start(sdata, req);
  1128. }
  1129. int ieee80211_request_sched_scan_stop(struct ieee80211_local *local)
  1130. {
  1131. struct ieee80211_sub_if_data *sched_scan_sdata;
  1132. int ret = -ENOENT;
  1133. lockdep_assert_wiphy(local->hw.wiphy);
  1134. if (!local->ops->sched_scan_stop)
  1135. return -EOPNOTSUPP;
  1136. /* We don't want to restart sched scan anymore. */
  1137. RCU_INIT_POINTER(local->sched_scan_req, NULL);
  1138. sched_scan_sdata = rcu_dereference_protected(local->sched_scan_sdata,
  1139. lockdep_is_held(&local->hw.wiphy->mtx));
  1140. if (sched_scan_sdata) {
  1141. ret = drv_sched_scan_stop(local, sched_scan_sdata);
  1142. if (!ret)
  1143. RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
  1144. }
  1145. return ret;
  1146. }
  1147. void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
  1148. {
  1149. struct ieee80211_local *local = hw_to_local(hw);
  1150. trace_api_sched_scan_results(local);
  1151. cfg80211_sched_scan_results(hw->wiphy, 0);
  1152. }
  1153. EXPORT_SYMBOL(ieee80211_sched_scan_results);
  1154. void ieee80211_sched_scan_end(struct ieee80211_local *local)
  1155. {
  1156. lockdep_assert_wiphy(local->hw.wiphy);
  1157. if (!rcu_access_pointer(local->sched_scan_sdata))
  1158. return;
  1159. RCU_INIT_POINTER(local->sched_scan_sdata, NULL);
  1160. /* If sched scan was aborted by the driver. */
  1161. RCU_INIT_POINTER(local->sched_scan_req, NULL);
  1162. cfg80211_sched_scan_stopped_locked(local->hw.wiphy, 0);
  1163. }
  1164. void ieee80211_sched_scan_stopped_work(struct wiphy *wiphy,
  1165. struct wiphy_work *work)
  1166. {
  1167. struct ieee80211_local *local =
  1168. container_of(work, struct ieee80211_local,
  1169. sched_scan_stopped_work);
  1170. ieee80211_sched_scan_end(local);
  1171. }
  1172. void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
  1173. {
  1174. struct ieee80211_local *local = hw_to_local(hw);
  1175. trace_api_sched_scan_stopped(local);
  1176. /*
  1177. * this shouldn't really happen, so for simplicity
  1178. * simply ignore it, and let mac80211 reconfigure
  1179. * the sched scan later on.
  1180. */
  1181. if (local->in_reconfig)
  1182. return;
  1183. wiphy_work_queue(hw->wiphy, &local->sched_scan_stopped_work);
  1184. }
  1185. EXPORT_SYMBOL(ieee80211_sched_scan_stopped);