scan.c 29 KB

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