scan.c 27 KB

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