scan.c 27 KB

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