scan.c 28 KB

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