scan.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. /*
  2. * Scanning implementation
  3. *
  4. * Copyright 2003, Jouni Malinen <jkmaline@cc.hut.fi>
  5. * Copyright 2004, Instant802 Networks, Inc.
  6. * Copyright 2005, Devicescape Software, Inc.
  7. * Copyright 2006-2007 Jiri Benc <jbenc@suse.cz>
  8. * Copyright 2007, Michael Wu <flamingice@sourmilk.net>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. */
  14. #include <linux/if_arp.h>
  15. #include <linux/etherdevice.h>
  16. #include <linux/rtnetlink.h>
  17. #include <linux/pm_qos.h>
  18. #include <net/sch_generic.h>
  19. #include <linux/slab.h>
  20. #include <linux/export.h>
  21. #include <net/mac80211.h>
  22. #include "ieee80211_i.h"
  23. #include "driver-ops.h"
  24. #include "mesh.h"
  25. #define IEEE80211_PROBE_DELAY (HZ / 33)
  26. #define IEEE80211_CHANNEL_TIME (HZ / 33)
  27. #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 9)
  28. void ieee80211_rx_bss_put(struct ieee80211_local *local,
  29. struct ieee80211_bss *bss)
  30. {
  31. if (!bss)
  32. return;
  33. cfg80211_put_bss(local->hw.wiphy,
  34. container_of((void *)bss, struct cfg80211_bss, priv));
  35. }
  36. static bool is_uapsd_supported(struct ieee802_11_elems *elems)
  37. {
  38. u8 qos_info;
  39. if (elems->wmm_info && elems->wmm_info_len == 7
  40. && elems->wmm_info[5] == 1)
  41. qos_info = elems->wmm_info[6];
  42. else if (elems->wmm_param && elems->wmm_param_len == 24
  43. && elems->wmm_param[5] == 1)
  44. qos_info = elems->wmm_param[6];
  45. else
  46. /* no valid wmm information or parameter element found */
  47. return false;
  48. return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
  49. }
  50. struct ieee80211_bss *
  51. ieee80211_bss_info_update(struct ieee80211_local *local,
  52. struct ieee80211_rx_status *rx_status,
  53. struct ieee80211_mgmt *mgmt, size_t len,
  54. struct ieee802_11_elems *elems,
  55. struct ieee80211_channel *channel)
  56. {
  57. bool beacon = ieee80211_is_beacon(mgmt->frame_control);
  58. struct cfg80211_bss *cbss;
  59. struct ieee80211_bss *bss;
  60. int clen, srlen;
  61. s32 signal = 0;
  62. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  63. signal = rx_status->signal * 100;
  64. else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
  65. signal = (rx_status->signal * 100) / local->hw.max_signal;
  66. cbss = cfg80211_inform_bss_frame(local->hw.wiphy, channel,
  67. mgmt, len, signal, GFP_ATOMIC);
  68. if (!cbss)
  69. return NULL;
  70. bss = (void *)cbss->priv;
  71. if (beacon)
  72. bss->device_ts_beacon = rx_status->device_timestamp;
  73. else
  74. bss->device_ts_presp = rx_status->device_timestamp;
  75. if (elems->parse_error) {
  76. if (beacon)
  77. bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;
  78. else
  79. bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;
  80. } else {
  81. if (beacon)
  82. bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;
  83. else
  84. bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;
  85. }
  86. /* save the ERP value so that it is available at association time */
  87. if (elems->erp_info && (!elems->parse_error ||
  88. !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {
  89. bss->erp_value = elems->erp_info[0];
  90. bss->has_erp_value = true;
  91. if (!elems->parse_error)
  92. bss->valid_data |= IEEE80211_BSS_VALID_ERP;
  93. }
  94. /* replace old supported rates if we get new values */
  95. if (!elems->parse_error ||
  96. !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {
  97. srlen = 0;
  98. if (elems->supp_rates) {
  99. clen = IEEE80211_MAX_SUPP_RATES;
  100. if (clen > elems->supp_rates_len)
  101. clen = elems->supp_rates_len;
  102. memcpy(bss->supp_rates, elems->supp_rates, clen);
  103. srlen += clen;
  104. }
  105. if (elems->ext_supp_rates) {
  106. clen = IEEE80211_MAX_SUPP_RATES - srlen;
  107. if (clen > elems->ext_supp_rates_len)
  108. clen = elems->ext_supp_rates_len;
  109. memcpy(bss->supp_rates + srlen, elems->ext_supp_rates,
  110. clen);
  111. srlen += clen;
  112. }
  113. if (srlen) {
  114. bss->supp_rates_len = srlen;
  115. if (!elems->parse_error)
  116. bss->valid_data |= IEEE80211_BSS_VALID_RATES;
  117. }
  118. }
  119. if (!elems->parse_error ||
  120. !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {
  121. bss->wmm_used = elems->wmm_param || elems->wmm_info;
  122. bss->uapsd_supported = is_uapsd_supported(elems);
  123. if (!elems->parse_error)
  124. bss->valid_data |= IEEE80211_BSS_VALID_WMM;
  125. }
  126. return bss;
  127. }
  128. void ieee80211_scan_rx(struct ieee80211_local *local, struct sk_buff *skb)
  129. {
  130. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  131. struct ieee80211_sub_if_data *sdata1, *sdata2;
  132. struct ieee80211_mgmt *mgmt = (void *)skb->data;
  133. struct ieee80211_bss *bss;
  134. u8 *elements;
  135. struct ieee80211_channel *channel;
  136. size_t baselen;
  137. struct ieee802_11_elems elems;
  138. if (skb->len < 24 ||
  139. (!ieee80211_is_probe_resp(mgmt->frame_control) &&
  140. !ieee80211_is_beacon(mgmt->frame_control)))
  141. return;
  142. sdata1 = rcu_dereference(local->scan_sdata);
  143. sdata2 = rcu_dereference(local->sched_scan_sdata);
  144. if (likely(!sdata1 && !sdata2))
  145. return;
  146. if (ieee80211_is_probe_resp(mgmt->frame_control)) {
  147. /* ignore ProbeResp to foreign address */
  148. if ((!sdata1 || !ether_addr_equal(mgmt->da, sdata1->vif.addr)) &&
  149. (!sdata2 || !ether_addr_equal(mgmt->da, sdata2->vif.addr)))
  150. return;
  151. elements = mgmt->u.probe_resp.variable;
  152. baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  153. } else {
  154. baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
  155. elements = mgmt->u.beacon.variable;
  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. /* ensure nullfunc is transmitted before leaving operating channel */
  277. ieee80211_flush_queues(local, NULL);
  278. ieee80211_configure_filter(local);
  279. /* We need to set power level at maximum rate for scanning. */
  280. ieee80211_hw_config(local, 0);
  281. ieee80211_queue_delayed_work(&local->hw,
  282. &local->scan_work, 0);
  283. return 0;
  284. }
  285. static bool ieee80211_can_scan(struct ieee80211_local *local,
  286. struct ieee80211_sub_if_data *sdata)
  287. {
  288. if (local->radar_detect_enabled)
  289. return false;
  290. if (!list_empty(&local->roc_list))
  291. return false;
  292. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  293. sdata->u.mgd.flags & (IEEE80211_STA_BEACON_POLL |
  294. IEEE80211_STA_CONNECTION_POLL))
  295. return false;
  296. return true;
  297. }
  298. void ieee80211_run_deferred_scan(struct ieee80211_local *local)
  299. {
  300. lockdep_assert_held(&local->mtx);
  301. if (!local->scan_req || local->scanning)
  302. return;
  303. if (!ieee80211_can_scan(local,
  304. rcu_dereference_protected(
  305. local->scan_sdata,
  306. lockdep_is_held(&local->mtx))))
  307. return;
  308. ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
  309. round_jiffies_relative(0));
  310. }
  311. static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
  312. unsigned long *next_delay)
  313. {
  314. int i;
  315. struct ieee80211_sub_if_data *sdata;
  316. enum ieee80211_band band = local->hw.conf.chandef.chan->band;
  317. u32 tx_flags;
  318. tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
  319. if (local->scan_req->no_cck)
  320. tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
  321. sdata = rcu_dereference_protected(local->scan_sdata,
  322. lockdep_is_held(&local->mtx));
  323. for (i = 0; i < local->scan_req->n_ssids; i++)
  324. ieee80211_send_probe_req(
  325. sdata, NULL,
  326. local->scan_req->ssids[i].ssid,
  327. local->scan_req->ssids[i].ssid_len,
  328. local->scan_req->ie, local->scan_req->ie_len,
  329. local->scan_req->rates[band], false,
  330. tx_flags, local->hw.conf.chandef.chan, true);
  331. /*
  332. * After sending probe requests, wait for probe responses
  333. * on the channel.
  334. */
  335. *next_delay = IEEE80211_CHANNEL_TIME;
  336. local->next_scan_state = SCAN_DECISION;
  337. }
  338. static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
  339. struct cfg80211_scan_request *req)
  340. {
  341. struct ieee80211_local *local = sdata->local;
  342. int rc;
  343. lockdep_assert_held(&local->mtx);
  344. if (local->scan_req)
  345. return -EBUSY;
  346. if (!ieee80211_can_scan(local, sdata)) {
  347. /* wait for the work to finish/time out */
  348. local->scan_req = req;
  349. rcu_assign_pointer(local->scan_sdata, sdata);
  350. return 0;
  351. }
  352. if (local->ops->hw_scan) {
  353. u8 *ies;
  354. local->hw_scan_ies_bufsize = 2 + IEEE80211_MAX_SSID_LEN +
  355. local->scan_ies_len +
  356. req->ie_len;
  357. local->hw_scan_req = kmalloc(
  358. sizeof(*local->hw_scan_req) +
  359. req->n_channels * sizeof(req->channels[0]) +
  360. local->hw_scan_ies_bufsize, GFP_KERNEL);
  361. if (!local->hw_scan_req)
  362. return -ENOMEM;
  363. local->hw_scan_req->ssids = req->ssids;
  364. local->hw_scan_req->n_ssids = req->n_ssids;
  365. ies = (u8 *)local->hw_scan_req +
  366. sizeof(*local->hw_scan_req) +
  367. req->n_channels * sizeof(req->channels[0]);
  368. local->hw_scan_req->ie = ies;
  369. local->hw_scan_req->flags = req->flags;
  370. local->hw_scan_band = 0;
  371. /*
  372. * After allocating local->hw_scan_req, we must
  373. * go through until ieee80211_prep_hw_scan(), so
  374. * anything that might be changed here and leave
  375. * this function early must not go after this
  376. * allocation.
  377. */
  378. }
  379. local->scan_req = req;
  380. rcu_assign_pointer(local->scan_sdata, sdata);
  381. if (local->ops->hw_scan) {
  382. __set_bit(SCAN_HW_SCANNING, &local->scanning);
  383. } else if ((req->n_channels == 1) &&
  384. (req->channels[0] == local->_oper_chandef.chan)) {
  385. /*
  386. * If we are scanning only on the operating channel
  387. * then we do not need to stop normal activities
  388. */
  389. unsigned long next_delay;
  390. __set_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning);
  391. ieee80211_recalc_idle(local);
  392. /* Notify driver scan is starting, keep order of operations
  393. * same as normal software scan, in case that matters. */
  394. drv_sw_scan_start(local);
  395. ieee80211_configure_filter(local); /* accept probe-responses */
  396. /* We need to ensure power level is at max for scanning. */
  397. ieee80211_hw_config(local, 0);
  398. if ((req->channels[0]->flags &
  399. IEEE80211_CHAN_PASSIVE_SCAN) ||
  400. !local->scan_req->n_ssids) {
  401. next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  402. } else {
  403. ieee80211_scan_state_send_probe(local, &next_delay);
  404. next_delay = IEEE80211_CHANNEL_TIME;
  405. }
  406. /* Now, just wait a bit and we are all done! */
  407. ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
  408. next_delay);
  409. return 0;
  410. } else {
  411. /* Do normal software scan */
  412. __set_bit(SCAN_SW_SCANNING, &local->scanning);
  413. }
  414. ieee80211_recalc_idle(local);
  415. if (local->ops->hw_scan) {
  416. WARN_ON(!ieee80211_prep_hw_scan(local));
  417. rc = drv_hw_scan(local, sdata, local->hw_scan_req);
  418. } else
  419. rc = ieee80211_start_sw_scan(local);
  420. if (rc) {
  421. kfree(local->hw_scan_req);
  422. local->hw_scan_req = NULL;
  423. local->scanning = 0;
  424. ieee80211_recalc_idle(local);
  425. local->scan_req = NULL;
  426. rcu_assign_pointer(local->scan_sdata, NULL);
  427. }
  428. return rc;
  429. }
  430. static unsigned long
  431. ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
  432. {
  433. /*
  434. * TODO: channel switching also consumes quite some time,
  435. * add that delay as well to get a better estimation
  436. */
  437. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  438. return IEEE80211_PASSIVE_CHANNEL_TIME;
  439. return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
  440. }
  441. static void ieee80211_scan_state_decision(struct ieee80211_local *local,
  442. unsigned long *next_delay)
  443. {
  444. bool associated = false;
  445. bool tx_empty = true;
  446. bool bad_latency;
  447. struct ieee80211_sub_if_data *sdata;
  448. struct ieee80211_channel *next_chan;
  449. enum mac80211_scan_state next_scan_state;
  450. /*
  451. * check if at least one STA interface is associated,
  452. * check if at least one STA interface has pending tx frames
  453. * and grab the lowest used beacon interval
  454. */
  455. mutex_lock(&local->iflist_mtx);
  456. list_for_each_entry(sdata, &local->interfaces, list) {
  457. if (!ieee80211_sdata_running(sdata))
  458. continue;
  459. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  460. if (sdata->u.mgd.associated) {
  461. associated = true;
  462. if (!qdisc_all_tx_empty(sdata->dev)) {
  463. tx_empty = false;
  464. break;
  465. }
  466. }
  467. }
  468. }
  469. mutex_unlock(&local->iflist_mtx);
  470. next_chan = local->scan_req->channels[local->scan_channel_idx];
  471. /*
  472. * we're currently scanning a different channel, let's
  473. * see if we can scan another channel without interfering
  474. * with the current traffic situation.
  475. *
  476. * Keep good latency, do not stay off-channel more than 125 ms.
  477. */
  478. bad_latency = time_after(jiffies +
  479. ieee80211_scan_get_channel_time(next_chan),
  480. local->leave_oper_channel_time + HZ / 8);
  481. if (associated && !tx_empty) {
  482. if (local->scan_req->flags & NL80211_SCAN_FLAG_LOW_PRIORITY)
  483. next_scan_state = SCAN_ABORT;
  484. else
  485. next_scan_state = SCAN_SUSPEND;
  486. } else if (associated && bad_latency) {
  487. next_scan_state = SCAN_SUSPEND;
  488. } else {
  489. next_scan_state = SCAN_SET_CHANNEL;
  490. }
  491. local->next_scan_state = next_scan_state;
  492. *next_delay = 0;
  493. }
  494. static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
  495. unsigned long *next_delay)
  496. {
  497. int skip;
  498. struct ieee80211_channel *chan;
  499. skip = 0;
  500. chan = local->scan_req->channels[local->scan_channel_idx];
  501. local->scan_channel = chan;
  502. if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
  503. skip = 1;
  504. /* advance state machine to next channel/band */
  505. local->scan_channel_idx++;
  506. if (skip) {
  507. /* if we skip this channel return to the decision state */
  508. local->next_scan_state = SCAN_DECISION;
  509. return;
  510. }
  511. /*
  512. * Probe delay is used to update the NAV, cf. 11.1.3.2.2
  513. * (which unfortunately doesn't say _why_ step a) is done,
  514. * but it waits for the probe delay or until a frame is
  515. * received - and the received frame would update the NAV).
  516. * For now, we do not support waiting until a frame is
  517. * received.
  518. *
  519. * In any case, it is not necessary for a passive scan.
  520. */
  521. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  522. !local->scan_req->n_ssids) {
  523. *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  524. local->next_scan_state = SCAN_DECISION;
  525. return;
  526. }
  527. /* active scan, send probes */
  528. *next_delay = IEEE80211_PROBE_DELAY;
  529. local->next_scan_state = SCAN_SEND_PROBE;
  530. }
  531. static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
  532. unsigned long *next_delay)
  533. {
  534. /* switch back to the operating channel */
  535. local->scan_channel = NULL;
  536. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  537. /* disable PS */
  538. ieee80211_offchannel_return(local);
  539. *next_delay = HZ / 5;
  540. /* afterwards, resume scan & go to next channel */
  541. local->next_scan_state = SCAN_RESUME;
  542. }
  543. static void ieee80211_scan_state_resume(struct ieee80211_local *local,
  544. unsigned long *next_delay)
  545. {
  546. ieee80211_offchannel_stop_vifs(local);
  547. if (local->ops->flush) {
  548. ieee80211_flush_queues(local, NULL);
  549. *next_delay = 0;
  550. } else
  551. *next_delay = HZ / 10;
  552. /* remember when we left the operating channel */
  553. local->leave_oper_channel_time = jiffies;
  554. /* advance to the next channel to be scanned */
  555. local->next_scan_state = SCAN_SET_CHANNEL;
  556. }
  557. void ieee80211_scan_work(struct work_struct *work)
  558. {
  559. struct ieee80211_local *local =
  560. container_of(work, struct ieee80211_local, scan_work.work);
  561. struct ieee80211_sub_if_data *sdata;
  562. unsigned long next_delay = 0;
  563. bool aborted, hw_scan;
  564. mutex_lock(&local->mtx);
  565. sdata = rcu_dereference_protected(local->scan_sdata,
  566. lockdep_is_held(&local->mtx));
  567. /* When scanning on-channel, the first-callback means completed. */
  568. if (test_bit(SCAN_ONCHANNEL_SCANNING, &local->scanning)) {
  569. aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
  570. goto out_complete;
  571. }
  572. if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
  573. aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
  574. goto out_complete;
  575. }
  576. if (!sdata || !local->scan_req)
  577. goto out;
  578. if (local->scan_req && !local->scanning) {
  579. struct cfg80211_scan_request *req = local->scan_req;
  580. int rc;
  581. local->scan_req = NULL;
  582. rcu_assign_pointer(local->scan_sdata, NULL);
  583. rc = __ieee80211_start_scan(sdata, req);
  584. if (rc) {
  585. /* need to complete scan in cfg80211 */
  586. local->scan_req = req;
  587. aborted = true;
  588. goto out_complete;
  589. } else
  590. goto out;
  591. }
  592. /*
  593. * Avoid re-scheduling when the sdata is going away.
  594. */
  595. if (!ieee80211_sdata_running(sdata)) {
  596. aborted = true;
  597. goto out_complete;
  598. }
  599. /*
  600. * as long as no delay is required advance immediately
  601. * without scheduling a new work
  602. */
  603. do {
  604. if (!ieee80211_sdata_running(sdata)) {
  605. aborted = true;
  606. goto out_complete;
  607. }
  608. switch (local->next_scan_state) {
  609. case SCAN_DECISION:
  610. /* if no more bands/channels left, complete scan */
  611. if (local->scan_channel_idx >= local->scan_req->n_channels) {
  612. aborted = false;
  613. goto out_complete;
  614. }
  615. ieee80211_scan_state_decision(local, &next_delay);
  616. break;
  617. case SCAN_SET_CHANNEL:
  618. ieee80211_scan_state_set_channel(local, &next_delay);
  619. break;
  620. case SCAN_SEND_PROBE:
  621. ieee80211_scan_state_send_probe(local, &next_delay);
  622. break;
  623. case SCAN_SUSPEND:
  624. ieee80211_scan_state_suspend(local, &next_delay);
  625. break;
  626. case SCAN_RESUME:
  627. ieee80211_scan_state_resume(local, &next_delay);
  628. break;
  629. case SCAN_ABORT:
  630. aborted = true;
  631. goto out_complete;
  632. }
  633. } while (next_delay == 0);
  634. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
  635. goto out;
  636. out_complete:
  637. hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
  638. __ieee80211_scan_completed(&local->hw, aborted, hw_scan);
  639. out:
  640. mutex_unlock(&local->mtx);
  641. }
  642. int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
  643. struct cfg80211_scan_request *req)
  644. {
  645. int res;
  646. mutex_lock(&sdata->local->mtx);
  647. res = __ieee80211_start_scan(sdata, req);
  648. mutex_unlock(&sdata->local->mtx);
  649. return res;
  650. }
  651. int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
  652. const u8 *ssid, u8 ssid_len,
  653. struct ieee80211_channel *chan)
  654. {
  655. struct ieee80211_local *local = sdata->local;
  656. int ret = -EBUSY;
  657. enum ieee80211_band band;
  658. mutex_lock(&local->mtx);
  659. /* busy scanning */
  660. if (local->scan_req)
  661. goto unlock;
  662. /* fill internal scan request */
  663. if (!chan) {
  664. int i, max_n;
  665. int n_ch = 0;
  666. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  667. if (!local->hw.wiphy->bands[band])
  668. continue;
  669. max_n = local->hw.wiphy->bands[band]->n_channels;
  670. for (i = 0; i < max_n; i++) {
  671. struct ieee80211_channel *tmp_ch =
  672. &local->hw.wiphy->bands[band]->channels[i];
  673. if (tmp_ch->flags & (IEEE80211_CHAN_NO_IBSS |
  674. IEEE80211_CHAN_DISABLED))
  675. continue;
  676. local->int_scan_req->channels[n_ch] = tmp_ch;
  677. n_ch++;
  678. }
  679. }
  680. if (WARN_ON_ONCE(n_ch == 0))
  681. goto unlock;
  682. local->int_scan_req->n_channels = n_ch;
  683. } else {
  684. if (WARN_ON_ONCE(chan->flags & (IEEE80211_CHAN_NO_IBSS |
  685. IEEE80211_CHAN_DISABLED)))
  686. goto unlock;
  687. local->int_scan_req->channels[0] = chan;
  688. local->int_scan_req->n_channels = 1;
  689. }
  690. local->int_scan_req->ssids = &local->scan_ssid;
  691. local->int_scan_req->n_ssids = 1;
  692. memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
  693. local->int_scan_req->ssids[0].ssid_len = ssid_len;
  694. ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
  695. unlock:
  696. mutex_unlock(&local->mtx);
  697. return ret;
  698. }
  699. /*
  700. * Only call this function when a scan can't be queued -- under RTNL.
  701. */
  702. void ieee80211_scan_cancel(struct ieee80211_local *local)
  703. {
  704. /*
  705. * We are canceling software scan, or deferred scan that was not
  706. * yet really started (see __ieee80211_start_scan ).
  707. *
  708. * Regarding hardware scan:
  709. * - we can not call __ieee80211_scan_completed() as when
  710. * SCAN_HW_SCANNING bit is set this function change
  711. * local->hw_scan_req to operate on 5G band, what race with
  712. * driver which can use local->hw_scan_req
  713. *
  714. * - we can not cancel scan_work since driver can schedule it
  715. * by ieee80211_scan_completed(..., true) to finish scan
  716. *
  717. * Hence we only call the cancel_hw_scan() callback, but the low-level
  718. * driver is still responsible for calling ieee80211_scan_completed()
  719. * after the scan was completed/aborted.
  720. */
  721. mutex_lock(&local->mtx);
  722. if (!local->scan_req)
  723. goto out;
  724. if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
  725. if (local->ops->cancel_hw_scan)
  726. drv_cancel_hw_scan(local,
  727. rcu_dereference_protected(local->scan_sdata,
  728. lockdep_is_held(&local->mtx)));
  729. goto out;
  730. }
  731. /*
  732. * If the work is currently running, it must be blocked on
  733. * the mutex, but we'll set scan_sdata = NULL and it'll
  734. * simply exit once it acquires the mutex.
  735. */
  736. cancel_delayed_work(&local->scan_work);
  737. /* and clean up */
  738. __ieee80211_scan_completed(&local->hw, true, false);
  739. out:
  740. mutex_unlock(&local->mtx);
  741. }
  742. int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
  743. struct cfg80211_sched_scan_request *req)
  744. {
  745. struct ieee80211_local *local = sdata->local;
  746. struct ieee80211_sched_scan_ies sched_scan_ies = {};
  747. int ret, i, iebufsz;
  748. iebufsz = 2 + IEEE80211_MAX_SSID_LEN +
  749. local->scan_ies_len + req->ie_len;
  750. mutex_lock(&local->mtx);
  751. if (rcu_access_pointer(local->sched_scan_sdata)) {
  752. ret = -EBUSY;
  753. goto out;
  754. }
  755. if (!local->ops->sched_scan_start) {
  756. ret = -ENOTSUPP;
  757. goto out;
  758. }
  759. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  760. if (!local->hw.wiphy->bands[i])
  761. continue;
  762. sched_scan_ies.ie[i] = kzalloc(iebufsz, GFP_KERNEL);
  763. if (!sched_scan_ies.ie[i]) {
  764. ret = -ENOMEM;
  765. goto out_free;
  766. }
  767. sched_scan_ies.len[i] =
  768. ieee80211_build_preq_ies(local, sched_scan_ies.ie[i],
  769. iebufsz, req->ie, req->ie_len,
  770. i, (u32) -1, 0);
  771. }
  772. ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
  773. if (ret == 0)
  774. rcu_assign_pointer(local->sched_scan_sdata, sdata);
  775. out_free:
  776. while (i > 0)
  777. kfree(sched_scan_ies.ie[--i]);
  778. out:
  779. mutex_unlock(&local->mtx);
  780. return ret;
  781. }
  782. int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata)
  783. {
  784. struct ieee80211_local *local = sdata->local;
  785. int ret = 0;
  786. mutex_lock(&local->mtx);
  787. if (!local->ops->sched_scan_stop) {
  788. ret = -ENOTSUPP;
  789. goto out;
  790. }
  791. if (rcu_access_pointer(local->sched_scan_sdata))
  792. drv_sched_scan_stop(local, sdata);
  793. out:
  794. mutex_unlock(&local->mtx);
  795. return ret;
  796. }
  797. void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
  798. {
  799. struct ieee80211_local *local = hw_to_local(hw);
  800. trace_api_sched_scan_results(local);
  801. cfg80211_sched_scan_results(hw->wiphy);
  802. }
  803. EXPORT_SYMBOL(ieee80211_sched_scan_results);
  804. void ieee80211_sched_scan_stopped_work(struct work_struct *work)
  805. {
  806. struct ieee80211_local *local =
  807. container_of(work, struct ieee80211_local,
  808. sched_scan_stopped_work);
  809. mutex_lock(&local->mtx);
  810. if (!rcu_access_pointer(local->sched_scan_sdata)) {
  811. mutex_unlock(&local->mtx);
  812. return;
  813. }
  814. rcu_assign_pointer(local->sched_scan_sdata, NULL);
  815. mutex_unlock(&local->mtx);
  816. cfg80211_sched_scan_stopped(local->hw.wiphy);
  817. }
  818. void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
  819. {
  820. struct ieee80211_local *local = hw_to_local(hw);
  821. trace_api_sched_scan_stopped(local);
  822. ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work);
  823. }
  824. EXPORT_SYMBOL(ieee80211_sched_scan_stopped);