scan.c 26 KB

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