scan.c 39 KB

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