scan.c 26 KB

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