scan.c 32 KB

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