scan.c 33 KB

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