scan.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025
  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. local->hw.conf.channel);
  346. /*
  347. * After sending probe requests, wait for probe responses
  348. * on the channel.
  349. */
  350. *next_delay = IEEE80211_CHANNEL_TIME;
  351. local->next_scan_state = SCAN_DECISION;
  352. }
  353. static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
  354. struct cfg80211_scan_request *req)
  355. {
  356. struct ieee80211_local *local = sdata->local;
  357. int rc;
  358. lockdep_assert_held(&local->mtx);
  359. if (local->scan_req)
  360. return -EBUSY;
  361. if (!ieee80211_can_scan(local, sdata)) {
  362. /* wait for the work to finish/time out */
  363. local->scan_req = req;
  364. rcu_assign_pointer(local->scan_sdata, sdata);
  365. return 0;
  366. }
  367. if (local->ops->hw_scan) {
  368. u8 *ies;
  369. local->hw_scan_req = kmalloc(
  370. sizeof(*local->hw_scan_req) +
  371. req->n_channels * sizeof(req->channels[0]) +
  372. 2 + IEEE80211_MAX_SSID_LEN + local->scan_ies_len +
  373. req->ie_len, GFP_KERNEL);
  374. if (!local->hw_scan_req)
  375. return -ENOMEM;
  376. local->hw_scan_req->ssids = req->ssids;
  377. local->hw_scan_req->n_ssids = req->n_ssids;
  378. ies = (u8 *)local->hw_scan_req +
  379. sizeof(*local->hw_scan_req) +
  380. req->n_channels * sizeof(req->channels[0]);
  381. local->hw_scan_req->ie = ies;
  382. local->hw_scan_band = 0;
  383. /*
  384. * After allocating local->hw_scan_req, we must
  385. * go through until ieee80211_prep_hw_scan(), so
  386. * anything that might be changed here and leave
  387. * this function early must not go after this
  388. * allocation.
  389. */
  390. }
  391. local->scan_req = req;
  392. rcu_assign_pointer(local->scan_sdata, sdata);
  393. if (local->ops->hw_scan) {
  394. __set_bit(SCAN_HW_SCANNING, &local->scanning);
  395. } else if ((req->n_channels == 1) &&
  396. (req->channels[0] == local->oper_channel)) {
  397. /*
  398. * If we are scanning only on the operating channel
  399. * then we do not need to stop normal activities
  400. */
  401. unsigned long next_delay;
  402. __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
  403. ieee80211_recalc_idle(local);
  404. /* Notify driver scan is starting, keep order of operations
  405. * same as normal software scan, in case that matters. */
  406. drv_sw_scan_start(local);
  407. ieee80211_configure_filter(local); /* accept probe-responses */
  408. /* We need to ensure power level is at max for scanning. */
  409. ieee80211_hw_config(local, 0);
  410. if ((req->channels[0]->flags &
  411. IEEE80211_CHAN_PASSIVE_SCAN) ||
  412. !local->scan_req->n_ssids) {
  413. next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  414. } else {
  415. ieee80211_scan_state_send_probe(local, &next_delay);
  416. next_delay = IEEE80211_CHANNEL_TIME;
  417. }
  418. /* Now, just wait a bit and we are all done! */
  419. ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
  420. next_delay);
  421. return 0;
  422. } else {
  423. /* Do normal software scan */
  424. __set_bit(SCAN_SW_SCANNING, &local->scanning);
  425. }
  426. ieee80211_recalc_idle(local);
  427. if (local->ops->hw_scan) {
  428. WARN_ON(!ieee80211_prep_hw_scan(local));
  429. rc = drv_hw_scan(local, sdata, local->hw_scan_req);
  430. } else
  431. rc = ieee80211_start_sw_scan(local);
  432. if (rc) {
  433. kfree(local->hw_scan_req);
  434. local->hw_scan_req = NULL;
  435. local->scanning = 0;
  436. ieee80211_recalc_idle(local);
  437. local->scan_req = NULL;
  438. rcu_assign_pointer(local->scan_sdata, NULL);
  439. }
  440. return rc;
  441. }
  442. static unsigned long
  443. ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
  444. {
  445. /*
  446. * TODO: channel switching also consumes quite some time,
  447. * add that delay as well to get a better estimation
  448. */
  449. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  450. return IEEE80211_PASSIVE_CHANNEL_TIME;
  451. return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
  452. }
  453. static void ieee80211_scan_state_decision(struct ieee80211_local *local,
  454. unsigned long *next_delay)
  455. {
  456. bool associated = false;
  457. bool tx_empty = true;
  458. bool bad_latency;
  459. bool listen_int_exceeded;
  460. unsigned long min_beacon_int = 0;
  461. struct ieee80211_sub_if_data *sdata;
  462. struct ieee80211_channel *next_chan;
  463. /*
  464. * check if at least one STA interface is associated,
  465. * check if at least one STA interface has pending tx frames
  466. * and grab the lowest used beacon interval
  467. */
  468. mutex_lock(&local->iflist_mtx);
  469. list_for_each_entry(sdata, &local->interfaces, list) {
  470. if (!ieee80211_sdata_running(sdata))
  471. continue;
  472. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  473. if (sdata->u.mgd.associated) {
  474. associated = true;
  475. if (sdata->vif.bss_conf.beacon_int <
  476. min_beacon_int || min_beacon_int == 0)
  477. min_beacon_int =
  478. sdata->vif.bss_conf.beacon_int;
  479. if (!qdisc_all_tx_empty(sdata->dev)) {
  480. tx_empty = false;
  481. break;
  482. }
  483. }
  484. }
  485. }
  486. mutex_unlock(&local->iflist_mtx);
  487. next_chan = local->scan_req->channels[local->scan_channel_idx];
  488. /*
  489. * we're currently scanning a different channel, let's
  490. * see if we can scan another channel without interfering
  491. * with the current traffic situation.
  492. *
  493. * Since we don't know if the AP has pending frames for us
  494. * we can only check for our tx queues and use the current
  495. * pm_qos requirements for rx. Hence, if no tx traffic occurs
  496. * at all we will scan as many channels in a row as the pm_qos
  497. * latency allows us to. Additionally we also check for the
  498. * currently negotiated listen interval to prevent losing
  499. * frames unnecessarily.
  500. *
  501. * Otherwise switch back to the operating channel.
  502. */
  503. bad_latency = time_after(jiffies +
  504. ieee80211_scan_get_channel_time(next_chan),
  505. local->leave_oper_channel_time +
  506. usecs_to_jiffies(pm_qos_request(PM_QOS_NETWORK_LATENCY)));
  507. listen_int_exceeded = time_after(jiffies +
  508. ieee80211_scan_get_channel_time(next_chan),
  509. local->leave_oper_channel_time +
  510. usecs_to_jiffies(min_beacon_int * 1024) *
  511. local->hw.conf.listen_interval);
  512. if (associated && (!tx_empty || bad_latency || listen_int_exceeded))
  513. local->next_scan_state = SCAN_SUSPEND;
  514. else
  515. local->next_scan_state = SCAN_SET_CHANNEL;
  516. *next_delay = 0;
  517. }
  518. static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
  519. unsigned long *next_delay)
  520. {
  521. int skip;
  522. struct ieee80211_channel *chan;
  523. skip = 0;
  524. chan = local->scan_req->channels[local->scan_channel_idx];
  525. local->scan_channel = chan;
  526. if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
  527. skip = 1;
  528. /* advance state machine to next channel/band */
  529. local->scan_channel_idx++;
  530. if (skip) {
  531. /* if we skip this channel return to the decision state */
  532. local->next_scan_state = SCAN_DECISION;
  533. return;
  534. }
  535. /*
  536. * Probe delay is used to update the NAV, cf. 11.1.3.2.2
  537. * (which unfortunately doesn't say _why_ step a) is done,
  538. * but it waits for the probe delay or until a frame is
  539. * received - and the received frame would update the NAV).
  540. * For now, we do not support waiting until a frame is
  541. * received.
  542. *
  543. * In any case, it is not necessary for a passive scan.
  544. */
  545. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  546. !local->scan_req->n_ssids) {
  547. *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  548. local->next_scan_state = SCAN_DECISION;
  549. return;
  550. }
  551. /* active scan, send probes */
  552. *next_delay = IEEE80211_PROBE_DELAY;
  553. local->next_scan_state = SCAN_SEND_PROBE;
  554. }
  555. static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
  556. unsigned long *next_delay)
  557. {
  558. /* switch back to the operating channel */
  559. local->scan_channel = NULL;
  560. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  561. /*
  562. * Re-enable vifs and beaconing. Leave PS
  563. * in off-channel state..will put that back
  564. * on-channel at the end of scanning.
  565. */
  566. ieee80211_offchannel_return(local, false);
  567. *next_delay = HZ / 5;
  568. /* afterwards, resume scan & go to next channel */
  569. local->next_scan_state = SCAN_RESUME;
  570. }
  571. static void ieee80211_scan_state_resume(struct ieee80211_local *local,
  572. unsigned long *next_delay)
  573. {
  574. /* PS already is in off-channel mode */
  575. ieee80211_offchannel_stop_vifs(local, false);
  576. if (local->ops->flush) {
  577. drv_flush(local, false);
  578. *next_delay = 0;
  579. } else
  580. *next_delay = HZ / 10;
  581. /* remember when we left the operating channel */
  582. local->leave_oper_channel_time = jiffies;
  583. /* advance to the next channel to be scanned */
  584. local->next_scan_state = SCAN_SET_CHANNEL;
  585. }
  586. void ieee80211_scan_work(struct work_struct *work)
  587. {
  588. struct ieee80211_local *local =
  589. container_of(work, struct ieee80211_local, scan_work.work);
  590. struct ieee80211_sub_if_data *sdata;
  591. unsigned long next_delay = 0;
  592. bool aborted, hw_scan;
  593. mutex_lock(&local->mtx);
  594. sdata = rcu_dereference_protected(local->scan_sdata,
  595. lockdep_is_held(&local->mtx));
  596. /* When scanning on-channel, the first-callback means completed. */
  597. if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
  598. aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
  599. goto out_complete;
  600. }
  601. if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
  602. aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
  603. goto out_complete;
  604. }
  605. if (!sdata || !local->scan_req)
  606. goto out;
  607. if (local->scan_req && !local->scanning) {
  608. struct cfg80211_scan_request *req = local->scan_req;
  609. int rc;
  610. local->scan_req = NULL;
  611. rcu_assign_pointer(local->scan_sdata, NULL);
  612. rc = __ieee80211_start_scan(sdata, req);
  613. if (rc) {
  614. /* need to complete scan in cfg80211 */
  615. local->scan_req = req;
  616. aborted = true;
  617. goto out_complete;
  618. } else
  619. goto out;
  620. }
  621. /*
  622. * Avoid re-scheduling when the sdata is going away.
  623. */
  624. if (!ieee80211_sdata_running(sdata)) {
  625. aborted = true;
  626. goto out_complete;
  627. }
  628. /*
  629. * as long as no delay is required advance immediately
  630. * without scheduling a new work
  631. */
  632. do {
  633. if (!ieee80211_sdata_running(sdata)) {
  634. aborted = true;
  635. goto out_complete;
  636. }
  637. switch (local->next_scan_state) {
  638. case SCAN_DECISION:
  639. /* if no more bands/channels left, complete scan */
  640. if (local->scan_channel_idx >= local->scan_req->n_channels) {
  641. aborted = false;
  642. goto out_complete;
  643. }
  644. ieee80211_scan_state_decision(local, &next_delay);
  645. break;
  646. case SCAN_SET_CHANNEL:
  647. ieee80211_scan_state_set_channel(local, &next_delay);
  648. break;
  649. case SCAN_SEND_PROBE:
  650. ieee80211_scan_state_send_probe(local, &next_delay);
  651. break;
  652. case SCAN_SUSPEND:
  653. ieee80211_scan_state_suspend(local, &next_delay);
  654. break;
  655. case SCAN_RESUME:
  656. ieee80211_scan_state_resume(local, &next_delay);
  657. break;
  658. }
  659. } while (next_delay == 0);
  660. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
  661. goto out;
  662. out_complete:
  663. hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
  664. __ieee80211_scan_completed(&local->hw, aborted, hw_scan);
  665. out:
  666. mutex_unlock(&local->mtx);
  667. }
  668. int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
  669. struct cfg80211_scan_request *req)
  670. {
  671. int res;
  672. mutex_lock(&sdata->local->mtx);
  673. res = __ieee80211_start_scan(sdata, req);
  674. mutex_unlock(&sdata->local->mtx);
  675. return res;
  676. }
  677. int ieee80211_request_internal_scan(struct ieee80211_sub_if_data *sdata,
  678. const u8 *ssid, u8 ssid_len,
  679. struct ieee80211_channel *chan)
  680. {
  681. struct ieee80211_local *local = sdata->local;
  682. int ret = -EBUSY;
  683. enum ieee80211_band band;
  684. mutex_lock(&local->mtx);
  685. /* busy scanning */
  686. if (local->scan_req)
  687. goto unlock;
  688. /* fill internal scan request */
  689. if (!chan) {
  690. int i, nchan = 0;
  691. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  692. if (!local->hw.wiphy->bands[band])
  693. continue;
  694. for (i = 0;
  695. i < local->hw.wiphy->bands[band]->n_channels;
  696. i++) {
  697. local->int_scan_req->channels[nchan] =
  698. &local->hw.wiphy->bands[band]->channels[i];
  699. nchan++;
  700. }
  701. }
  702. local->int_scan_req->n_channels = nchan;
  703. } else {
  704. local->int_scan_req->channels[0] = chan;
  705. local->int_scan_req->n_channels = 1;
  706. }
  707. local->int_scan_req->ssids = &local->scan_ssid;
  708. local->int_scan_req->n_ssids = 1;
  709. memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
  710. local->int_scan_req->ssids[0].ssid_len = ssid_len;
  711. ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
  712. unlock:
  713. mutex_unlock(&local->mtx);
  714. return ret;
  715. }
  716. /*
  717. * Only call this function when a scan can't be queued -- under RTNL.
  718. */
  719. void ieee80211_scan_cancel(struct ieee80211_local *local)
  720. {
  721. /*
  722. * We are canceling software scan, or deferred scan that was not
  723. * yet really started (see __ieee80211_start_scan ).
  724. *
  725. * Regarding hardware scan:
  726. * - we can not call __ieee80211_scan_completed() as when
  727. * SCAN_HW_SCANNING bit is set this function change
  728. * local->hw_scan_req to operate on 5G band, what race with
  729. * driver which can use local->hw_scan_req
  730. *
  731. * - we can not cancel scan_work since driver can schedule it
  732. * by ieee80211_scan_completed(..., true) to finish scan
  733. *
  734. * Hence we only call the cancel_hw_scan() callback, but the low-level
  735. * driver is still responsible for calling ieee80211_scan_completed()
  736. * after the scan was completed/aborted.
  737. */
  738. mutex_lock(&local->mtx);
  739. if (!local->scan_req)
  740. goto out;
  741. if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
  742. if (local->ops->cancel_hw_scan)
  743. drv_cancel_hw_scan(local,
  744. rcu_dereference_protected(local->scan_sdata,
  745. lockdep_is_held(&local->mtx)));
  746. goto out;
  747. }
  748. /*
  749. * If the work is currently running, it must be blocked on
  750. * the mutex, but we'll set scan_sdata = NULL and it'll
  751. * simply exit once it acquires the mutex.
  752. */
  753. cancel_delayed_work(&local->scan_work);
  754. /* and clean up */
  755. __ieee80211_scan_completed(&local->hw, true, false);
  756. out:
  757. mutex_unlock(&local->mtx);
  758. }
  759. int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
  760. struct cfg80211_sched_scan_request *req)
  761. {
  762. struct ieee80211_local *local = sdata->local;
  763. struct ieee80211_sched_scan_ies sched_scan_ies;
  764. int ret, i;
  765. mutex_lock(&local->mtx);
  766. if (rcu_access_pointer(local->sched_scan_sdata)) {
  767. ret = -EBUSY;
  768. goto out;
  769. }
  770. if (!local->ops->sched_scan_start) {
  771. ret = -ENOTSUPP;
  772. goto out;
  773. }
  774. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  775. if (!local->hw.wiphy->bands[i])
  776. continue;
  777. sched_scan_ies.ie[i] = kzalloc(2 + IEEE80211_MAX_SSID_LEN +
  778. local->scan_ies_len +
  779. req->ie_len,
  780. GFP_KERNEL);
  781. if (!sched_scan_ies.ie[i]) {
  782. ret = -ENOMEM;
  783. goto out_free;
  784. }
  785. sched_scan_ies.len[i] =
  786. ieee80211_build_preq_ies(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, &sched_scan_ies);
  791. if (ret == 0)
  792. rcu_assign_pointer(local->sched_scan_sdata, sdata);
  793. out_free:
  794. while (i > 0)
  795. kfree(sched_scan_ies.ie[--i]);
  796. out:
  797. mutex_unlock(&local->mtx);
  798. return ret;
  799. }
  800. int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata)
  801. {
  802. struct ieee80211_local *local = sdata->local;
  803. int ret = 0;
  804. mutex_lock(&local->mtx);
  805. if (!local->ops->sched_scan_stop) {
  806. ret = -ENOTSUPP;
  807. goto out;
  808. }
  809. if (rcu_access_pointer(local->sched_scan_sdata))
  810. drv_sched_scan_stop(local, sdata);
  811. out:
  812. mutex_unlock(&local->mtx);
  813. return ret;
  814. }
  815. void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
  816. {
  817. struct ieee80211_local *local = hw_to_local(hw);
  818. trace_api_sched_scan_results(local);
  819. cfg80211_sched_scan_results(hw->wiphy);
  820. }
  821. EXPORT_SYMBOL(ieee80211_sched_scan_results);
  822. void ieee80211_sched_scan_stopped_work(struct work_struct *work)
  823. {
  824. struct ieee80211_local *local =
  825. container_of(work, struct ieee80211_local,
  826. sched_scan_stopped_work);
  827. mutex_lock(&local->mtx);
  828. if (!rcu_access_pointer(local->sched_scan_sdata)) {
  829. mutex_unlock(&local->mtx);
  830. return;
  831. }
  832. rcu_assign_pointer(local->sched_scan_sdata, NULL);
  833. mutex_unlock(&local->mtx);
  834. cfg80211_sched_scan_stopped(local->hw.wiphy);
  835. }
  836. void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
  837. {
  838. struct ieee80211_local *local = hw_to_local(hw);
  839. trace_api_sched_scan_stopped(local);
  840. ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work);
  841. }
  842. EXPORT_SYMBOL(ieee80211_sched_scan_stopped);