scan.c 27 KB

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