scan.c 36 KB

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