scan.c 27 KB

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