scan.c 26 KB

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