scan.c 40 KB

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