scan.c 26 KB

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