scan.c 40 KB

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