scan.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795
  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/rtnetlink.h>
  16. #include <linux/pm_qos_params.h>
  17. #include <net/sch_generic.h>
  18. #include <linux/slab.h>
  19. #include <net/mac80211.h>
  20. #include "ieee80211_i.h"
  21. #include "driver-ops.h"
  22. #include "mesh.h"
  23. #define IEEE80211_PROBE_DELAY (HZ / 33)
  24. #define IEEE80211_CHANNEL_TIME (HZ / 33)
  25. #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 8)
  26. struct ieee80211_bss *
  27. ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
  28. u8 *ssid, u8 ssid_len)
  29. {
  30. struct cfg80211_bss *cbss;
  31. cbss = cfg80211_get_bss(local->hw.wiphy,
  32. ieee80211_get_channel(local->hw.wiphy, freq),
  33. bssid, ssid, ssid_len, 0, 0);
  34. if (!cbss)
  35. return NULL;
  36. return (void *)cbss->priv;
  37. }
  38. static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss)
  39. {
  40. struct ieee80211_bss *bss = (void *)cbss->priv;
  41. kfree(bss_mesh_id(bss));
  42. kfree(bss_mesh_cfg(bss));
  43. }
  44. void ieee80211_rx_bss_put(struct ieee80211_local *local,
  45. struct ieee80211_bss *bss)
  46. {
  47. if (!bss)
  48. return;
  49. cfg80211_put_bss(container_of((void *)bss, struct cfg80211_bss, priv));
  50. }
  51. static bool is_uapsd_supported(struct ieee802_11_elems *elems)
  52. {
  53. u8 qos_info;
  54. if (elems->wmm_info && elems->wmm_info_len == 7
  55. && elems->wmm_info[5] == 1)
  56. qos_info = elems->wmm_info[6];
  57. else if (elems->wmm_param && elems->wmm_param_len == 24
  58. && elems->wmm_param[5] == 1)
  59. qos_info = elems->wmm_param[6];
  60. else
  61. /* no valid wmm information or parameter element found */
  62. return false;
  63. return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
  64. }
  65. struct ieee80211_bss *
  66. ieee80211_bss_info_update(struct ieee80211_local *local,
  67. struct ieee80211_rx_status *rx_status,
  68. struct ieee80211_mgmt *mgmt,
  69. size_t len,
  70. struct ieee802_11_elems *elems,
  71. struct ieee80211_channel *channel,
  72. bool beacon)
  73. {
  74. struct cfg80211_bss *cbss;
  75. struct ieee80211_bss *bss;
  76. int clen, srlen;
  77. s32 signal = 0;
  78. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  79. signal = rx_status->signal * 100;
  80. else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
  81. signal = (rx_status->signal * 100) / local->hw.max_signal;
  82. cbss = cfg80211_inform_bss_frame(local->hw.wiphy, channel,
  83. mgmt, len, signal, GFP_ATOMIC);
  84. if (!cbss)
  85. return NULL;
  86. cbss->free_priv = ieee80211_rx_bss_free;
  87. bss = (void *)cbss->priv;
  88. /* save the ERP value so that it is available at association time */
  89. if (elems->erp_info && elems->erp_info_len >= 1) {
  90. bss->erp_value = elems->erp_info[0];
  91. bss->has_erp_value = 1;
  92. }
  93. if (elems->tim) {
  94. struct ieee80211_tim_ie *tim_ie =
  95. (struct ieee80211_tim_ie *)elems->tim;
  96. bss->dtim_period = tim_ie->dtim_period;
  97. }
  98. /* replace old supported rates if we get new values */
  99. srlen = 0;
  100. if (elems->supp_rates) {
  101. clen = IEEE80211_MAX_SUPP_RATES;
  102. if (clen > elems->supp_rates_len)
  103. clen = elems->supp_rates_len;
  104. memcpy(bss->supp_rates, elems->supp_rates, clen);
  105. srlen += clen;
  106. }
  107. if (elems->ext_supp_rates) {
  108. clen = IEEE80211_MAX_SUPP_RATES - srlen;
  109. if (clen > elems->ext_supp_rates_len)
  110. clen = elems->ext_supp_rates_len;
  111. memcpy(bss->supp_rates + srlen, elems->ext_supp_rates, clen);
  112. srlen += clen;
  113. }
  114. if (srlen)
  115. bss->supp_rates_len = srlen;
  116. bss->wmm_used = elems->wmm_param || elems->wmm_info;
  117. bss->uapsd_supported = is_uapsd_supported(elems);
  118. if (!beacon)
  119. bss->last_probe_resp = jiffies;
  120. return bss;
  121. }
  122. ieee80211_rx_result
  123. ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  124. {
  125. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  126. struct ieee80211_mgmt *mgmt;
  127. struct ieee80211_bss *bss;
  128. u8 *elements;
  129. struct ieee80211_channel *channel;
  130. size_t baselen;
  131. int freq;
  132. __le16 fc;
  133. bool presp, beacon = false;
  134. struct ieee802_11_elems elems;
  135. if (skb->len < 2)
  136. return RX_DROP_UNUSABLE;
  137. mgmt = (struct ieee80211_mgmt *) skb->data;
  138. fc = mgmt->frame_control;
  139. if (ieee80211_is_ctl(fc))
  140. return RX_CONTINUE;
  141. if (skb->len < 24)
  142. return RX_DROP_MONITOR;
  143. presp = ieee80211_is_probe_resp(fc);
  144. if (presp) {
  145. /* ignore ProbeResp to foreign address */
  146. if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
  147. return RX_DROP_MONITOR;
  148. presp = true;
  149. elements = mgmt->u.probe_resp.variable;
  150. baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  151. } else {
  152. beacon = ieee80211_is_beacon(fc);
  153. baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
  154. elements = mgmt->u.beacon.variable;
  155. }
  156. if (!presp && !beacon)
  157. return RX_CONTINUE;
  158. if (baselen > skb->len)
  159. return RX_DROP_MONITOR;
  160. ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
  161. if (elems.ds_params && elems.ds_params_len == 1)
  162. freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
  163. else
  164. freq = rx_status->freq;
  165. channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
  166. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  167. return RX_DROP_MONITOR;
  168. bss = ieee80211_bss_info_update(sdata->local, rx_status,
  169. mgmt, skb->len, &elems,
  170. channel, beacon);
  171. if (bss)
  172. ieee80211_rx_bss_put(sdata->local, bss);
  173. dev_kfree_skb(skb);
  174. return RX_QUEUED;
  175. }
  176. /* return false if no more work */
  177. static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
  178. {
  179. struct cfg80211_scan_request *req = local->scan_req;
  180. enum ieee80211_band band;
  181. int i, ielen, n_chans;
  182. do {
  183. if (local->hw_scan_band == IEEE80211_NUM_BANDS)
  184. return false;
  185. band = local->hw_scan_band;
  186. n_chans = 0;
  187. for (i = 0; i < req->n_channels; i++) {
  188. if (req->channels[i]->band == band) {
  189. local->hw_scan_req->channels[n_chans] =
  190. req->channels[i];
  191. n_chans++;
  192. }
  193. }
  194. local->hw_scan_band++;
  195. } while (!n_chans);
  196. local->hw_scan_req->n_channels = n_chans;
  197. ielen = ieee80211_build_preq_ies(local, (u8 *)local->hw_scan_req->ie,
  198. req->ie, req->ie_len, band);
  199. local->hw_scan_req->ie_len = ielen;
  200. return true;
  201. }
  202. void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
  203. {
  204. struct ieee80211_local *local = hw_to_local(hw);
  205. bool was_hw_scan;
  206. trace_api_scan_completed(local, aborted);
  207. mutex_lock(&local->scan_mtx);
  208. /*
  209. * It's ok to abort a not-yet-running scan (that
  210. * we have one at all will be verified by checking
  211. * local->scan_req next), but not to complete it
  212. * successfully.
  213. */
  214. if (WARN_ON(!local->scanning && !aborted))
  215. aborted = true;
  216. if (WARN_ON(!local->scan_req)) {
  217. mutex_unlock(&local->scan_mtx);
  218. return;
  219. }
  220. was_hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
  221. if (was_hw_scan && !aborted && ieee80211_prep_hw_scan(local)) {
  222. ieee80211_queue_delayed_work(&local->hw,
  223. &local->scan_work, 0);
  224. mutex_unlock(&local->scan_mtx);
  225. return;
  226. }
  227. kfree(local->hw_scan_req);
  228. local->hw_scan_req = NULL;
  229. if (local->scan_req != local->int_scan_req)
  230. cfg80211_scan_done(local->scan_req, aborted);
  231. local->scan_req = NULL;
  232. local->scan_sdata = NULL;
  233. local->scanning = 0;
  234. local->scan_channel = NULL;
  235. /* we only have to protect scan_req and hw/sw scan */
  236. mutex_unlock(&local->scan_mtx);
  237. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  238. if (was_hw_scan)
  239. goto done;
  240. ieee80211_configure_filter(local);
  241. drv_sw_scan_complete(local);
  242. ieee80211_offchannel_return(local, true);
  243. done:
  244. ieee80211_recalc_idle(local);
  245. ieee80211_mlme_notify_scan_completed(local);
  246. ieee80211_ibss_notify_scan_completed(local);
  247. ieee80211_mesh_notify_scan_completed(local);
  248. ieee80211_queue_work(&local->hw, &local->work_work);
  249. }
  250. EXPORT_SYMBOL(ieee80211_scan_completed);
  251. static int ieee80211_start_sw_scan(struct ieee80211_local *local)
  252. {
  253. /*
  254. * Hardware/driver doesn't support hw_scan, so use software
  255. * scanning instead. First send a nullfunc frame with power save
  256. * bit on so that AP will buffer the frames for us while we are not
  257. * listening, then send probe requests to each channel and wait for
  258. * the responses. After all channels are scanned, tune back to the
  259. * original channel and send a nullfunc frame with power save bit
  260. * off to trigger the AP to send us all the buffered frames.
  261. *
  262. * Note that while local->sw_scanning is true everything else but
  263. * nullfunc frames and probe requests will be dropped in
  264. * ieee80211_tx_h_check_assoc().
  265. */
  266. drv_sw_scan_start(local);
  267. ieee80211_offchannel_stop_beaconing(local);
  268. local->leave_oper_channel_time = 0;
  269. local->next_scan_state = SCAN_DECISION;
  270. local->scan_channel_idx = 0;
  271. drv_flush(local, false);
  272. ieee80211_configure_filter(local);
  273. ieee80211_queue_delayed_work(&local->hw,
  274. &local->scan_work,
  275. IEEE80211_CHANNEL_TIME);
  276. return 0;
  277. }
  278. static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
  279. struct cfg80211_scan_request *req)
  280. {
  281. struct ieee80211_local *local = sdata->local;
  282. int rc;
  283. if (local->scan_req)
  284. return -EBUSY;
  285. if (!list_empty(&local->work_list)) {
  286. /* wait for the work to finish/time out */
  287. local->scan_req = req;
  288. local->scan_sdata = sdata;
  289. return 0;
  290. }
  291. if (local->ops->hw_scan) {
  292. u8 *ies;
  293. local->hw_scan_req = kmalloc(
  294. sizeof(*local->hw_scan_req) +
  295. req->n_channels * sizeof(req->channels[0]) +
  296. 2 + IEEE80211_MAX_SSID_LEN + local->scan_ies_len +
  297. req->ie_len, GFP_KERNEL);
  298. if (!local->hw_scan_req)
  299. return -ENOMEM;
  300. local->hw_scan_req->ssids = req->ssids;
  301. local->hw_scan_req->n_ssids = req->n_ssids;
  302. ies = (u8 *)local->hw_scan_req +
  303. sizeof(*local->hw_scan_req) +
  304. req->n_channels * sizeof(req->channels[0]);
  305. local->hw_scan_req->ie = ies;
  306. local->hw_scan_band = 0;
  307. /*
  308. * After allocating local->hw_scan_req, we must
  309. * go through until ieee80211_prep_hw_scan(), so
  310. * anything that might be changed here and leave
  311. * this function early must not go after this
  312. * allocation.
  313. */
  314. }
  315. local->scan_req = req;
  316. local->scan_sdata = sdata;
  317. if (local->ops->hw_scan)
  318. __set_bit(SCAN_HW_SCANNING, &local->scanning);
  319. else
  320. __set_bit(SCAN_SW_SCANNING, &local->scanning);
  321. /*
  322. * Kicking off the scan need not be protected,
  323. * only the scan variable stuff, since now
  324. * local->scan_req is assigned and other callers
  325. * will abort their scan attempts.
  326. *
  327. * This avoids too many locking dependencies
  328. * so that the scan completed calls have more
  329. * locking freedom.
  330. */
  331. ieee80211_recalc_idle(local);
  332. mutex_unlock(&local->scan_mtx);
  333. if (local->ops->hw_scan) {
  334. WARN_ON(!ieee80211_prep_hw_scan(local));
  335. rc = drv_hw_scan(local, sdata, local->hw_scan_req);
  336. } else
  337. rc = ieee80211_start_sw_scan(local);
  338. mutex_lock(&local->scan_mtx);
  339. if (rc) {
  340. kfree(local->hw_scan_req);
  341. local->hw_scan_req = NULL;
  342. local->scanning = 0;
  343. ieee80211_recalc_idle(local);
  344. local->scan_req = NULL;
  345. local->scan_sdata = NULL;
  346. }
  347. return rc;
  348. }
  349. static unsigned long
  350. ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
  351. {
  352. /*
  353. * TODO: channel switching also consumes quite some time,
  354. * add that delay as well to get a better estimation
  355. */
  356. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  357. return IEEE80211_PASSIVE_CHANNEL_TIME;
  358. return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
  359. }
  360. static int ieee80211_scan_state_decision(struct ieee80211_local *local,
  361. unsigned long *next_delay)
  362. {
  363. bool associated = false;
  364. bool tx_empty = true;
  365. bool bad_latency;
  366. bool listen_int_exceeded;
  367. unsigned long min_beacon_int = 0;
  368. struct ieee80211_sub_if_data *sdata;
  369. struct ieee80211_channel *next_chan;
  370. /* if no more bands/channels left, complete scan and advance to the idle state */
  371. if (local->scan_channel_idx >= local->scan_req->n_channels) {
  372. ieee80211_scan_completed(&local->hw, false);
  373. return 1;
  374. }
  375. /*
  376. * check if at least one STA interface is associated,
  377. * check if at least one STA interface has pending tx frames
  378. * and grab the lowest used beacon interval
  379. */
  380. mutex_lock(&local->iflist_mtx);
  381. list_for_each_entry(sdata, &local->interfaces, list) {
  382. if (!ieee80211_sdata_running(sdata))
  383. continue;
  384. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  385. if (sdata->u.mgd.associated) {
  386. associated = true;
  387. if (sdata->vif.bss_conf.beacon_int <
  388. min_beacon_int || min_beacon_int == 0)
  389. min_beacon_int =
  390. sdata->vif.bss_conf.beacon_int;
  391. if (!qdisc_all_tx_empty(sdata->dev)) {
  392. tx_empty = false;
  393. break;
  394. }
  395. }
  396. }
  397. }
  398. mutex_unlock(&local->iflist_mtx);
  399. if (local->scan_channel) {
  400. /*
  401. * we're currently scanning a different channel, let's
  402. * see if we can scan another channel without interfering
  403. * with the current traffic situation.
  404. *
  405. * Since we don't know if the AP has pending frames for us
  406. * we can only check for our tx queues and use the current
  407. * pm_qos requirements for rx. Hence, if no tx traffic occurs
  408. * at all we will scan as many channels in a row as the pm_qos
  409. * latency allows us to. Additionally we also check for the
  410. * currently negotiated listen interval to prevent losing
  411. * frames unnecessarily.
  412. *
  413. * Otherwise switch back to the operating channel.
  414. */
  415. next_chan = local->scan_req->channels[local->scan_channel_idx];
  416. bad_latency = time_after(jiffies +
  417. ieee80211_scan_get_channel_time(next_chan),
  418. local->leave_oper_channel_time +
  419. usecs_to_jiffies(pm_qos_requirement(PM_QOS_NETWORK_LATENCY)));
  420. listen_int_exceeded = time_after(jiffies +
  421. ieee80211_scan_get_channel_time(next_chan),
  422. local->leave_oper_channel_time +
  423. usecs_to_jiffies(min_beacon_int * 1024) *
  424. local->hw.conf.listen_interval);
  425. if (associated && ( !tx_empty || bad_latency ||
  426. listen_int_exceeded))
  427. local->next_scan_state = SCAN_ENTER_OPER_CHANNEL;
  428. else
  429. local->next_scan_state = SCAN_SET_CHANNEL;
  430. } else {
  431. /*
  432. * we're on the operating channel currently, let's
  433. * leave that channel now to scan another one
  434. */
  435. local->next_scan_state = SCAN_LEAVE_OPER_CHANNEL;
  436. }
  437. *next_delay = 0;
  438. return 0;
  439. }
  440. static void ieee80211_scan_state_leave_oper_channel(struct ieee80211_local *local,
  441. unsigned long *next_delay)
  442. {
  443. ieee80211_offchannel_stop_station(local);
  444. __set_bit(SCAN_OFF_CHANNEL, &local->scanning);
  445. /*
  446. * What if the nullfunc frames didn't arrive?
  447. */
  448. drv_flush(local, false);
  449. if (local->ops->flush)
  450. *next_delay = 0;
  451. else
  452. *next_delay = HZ / 10;
  453. /* remember when we left the operating channel */
  454. local->leave_oper_channel_time = jiffies;
  455. /* advance to the next channel to be scanned */
  456. local->next_scan_state = SCAN_SET_CHANNEL;
  457. }
  458. static void ieee80211_scan_state_enter_oper_channel(struct ieee80211_local *local,
  459. unsigned long *next_delay)
  460. {
  461. /* switch back to the operating channel */
  462. local->scan_channel = NULL;
  463. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  464. /*
  465. * Only re-enable station mode interface now; beaconing will be
  466. * re-enabled once the full scan has been completed.
  467. */
  468. ieee80211_offchannel_return(local, false);
  469. __clear_bit(SCAN_OFF_CHANNEL, &local->scanning);
  470. *next_delay = HZ / 5;
  471. local->next_scan_state = SCAN_DECISION;
  472. }
  473. static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
  474. unsigned long *next_delay)
  475. {
  476. int skip;
  477. struct ieee80211_channel *chan;
  478. skip = 0;
  479. chan = local->scan_req->channels[local->scan_channel_idx];
  480. local->scan_channel = chan;
  481. if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
  482. skip = 1;
  483. /* advance state machine to next channel/band */
  484. local->scan_channel_idx++;
  485. if (skip) {
  486. /* if we skip this channel return to the decision state */
  487. local->next_scan_state = SCAN_DECISION;
  488. return;
  489. }
  490. /*
  491. * Probe delay is used to update the NAV, cf. 11.1.3.2.2
  492. * (which unfortunately doesn't say _why_ step a) is done,
  493. * but it waits for the probe delay or until a frame is
  494. * received - and the received frame would update the NAV).
  495. * For now, we do not support waiting until a frame is
  496. * received.
  497. *
  498. * In any case, it is not necessary for a passive scan.
  499. */
  500. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  501. !local->scan_req->n_ssids) {
  502. *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  503. local->next_scan_state = SCAN_DECISION;
  504. return;
  505. }
  506. /* active scan, send probes */
  507. *next_delay = IEEE80211_PROBE_DELAY;
  508. local->next_scan_state = SCAN_SEND_PROBE;
  509. }
  510. static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
  511. unsigned long *next_delay)
  512. {
  513. int i;
  514. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  515. for (i = 0; i < local->scan_req->n_ssids; i++)
  516. ieee80211_send_probe_req(
  517. sdata, NULL,
  518. local->scan_req->ssids[i].ssid,
  519. local->scan_req->ssids[i].ssid_len,
  520. local->scan_req->ie, local->scan_req->ie_len);
  521. /*
  522. * After sending probe requests, wait for probe responses
  523. * on the channel.
  524. */
  525. *next_delay = IEEE80211_CHANNEL_TIME;
  526. local->next_scan_state = SCAN_DECISION;
  527. }
  528. void ieee80211_scan_work(struct work_struct *work)
  529. {
  530. struct ieee80211_local *local =
  531. container_of(work, struct ieee80211_local, scan_work.work);
  532. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  533. unsigned long next_delay = 0;
  534. mutex_lock(&local->scan_mtx);
  535. if (!sdata || !local->scan_req) {
  536. mutex_unlock(&local->scan_mtx);
  537. return;
  538. }
  539. if (local->hw_scan_req) {
  540. int rc = drv_hw_scan(local, sdata, local->hw_scan_req);
  541. mutex_unlock(&local->scan_mtx);
  542. if (rc)
  543. ieee80211_scan_completed(&local->hw, true);
  544. return;
  545. }
  546. if (local->scan_req && !local->scanning) {
  547. struct cfg80211_scan_request *req = local->scan_req;
  548. int rc;
  549. local->scan_req = NULL;
  550. local->scan_sdata = NULL;
  551. rc = __ieee80211_start_scan(sdata, req);
  552. mutex_unlock(&local->scan_mtx);
  553. if (rc)
  554. ieee80211_scan_completed(&local->hw, true);
  555. return;
  556. }
  557. mutex_unlock(&local->scan_mtx);
  558. /*
  559. * Avoid re-scheduling when the sdata is going away.
  560. */
  561. if (!ieee80211_sdata_running(sdata)) {
  562. ieee80211_scan_completed(&local->hw, true);
  563. return;
  564. }
  565. /*
  566. * as long as no delay is required advance immediately
  567. * without scheduling a new work
  568. */
  569. do {
  570. switch (local->next_scan_state) {
  571. case SCAN_DECISION:
  572. if (ieee80211_scan_state_decision(local, &next_delay))
  573. return;
  574. break;
  575. case SCAN_SET_CHANNEL:
  576. ieee80211_scan_state_set_channel(local, &next_delay);
  577. break;
  578. case SCAN_SEND_PROBE:
  579. ieee80211_scan_state_send_probe(local, &next_delay);
  580. break;
  581. case SCAN_LEAVE_OPER_CHANNEL:
  582. ieee80211_scan_state_leave_oper_channel(local, &next_delay);
  583. break;
  584. case SCAN_ENTER_OPER_CHANNEL:
  585. ieee80211_scan_state_enter_oper_channel(local, &next_delay);
  586. break;
  587. }
  588. } while (next_delay == 0);
  589. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
  590. }
  591. int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
  592. struct cfg80211_scan_request *req)
  593. {
  594. int res;
  595. mutex_lock(&sdata->local->scan_mtx);
  596. res = __ieee80211_start_scan(sdata, req);
  597. mutex_unlock(&sdata->local->scan_mtx);
  598. return res;
  599. }
  600. int ieee80211_request_internal_scan(struct ieee80211_sub_if_data *sdata,
  601. const u8 *ssid, u8 ssid_len,
  602. struct ieee80211_channel *chan)
  603. {
  604. struct ieee80211_local *local = sdata->local;
  605. int ret = -EBUSY;
  606. enum nl80211_band band;
  607. mutex_lock(&local->scan_mtx);
  608. /* busy scanning */
  609. if (local->scan_req)
  610. goto unlock;
  611. /* fill internal scan request */
  612. if (!chan) {
  613. int i, nchan = 0;
  614. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  615. if (!local->hw.wiphy->bands[band])
  616. continue;
  617. for (i = 0;
  618. i < local->hw.wiphy->bands[band]->n_channels;
  619. i++) {
  620. local->int_scan_req->channels[nchan] =
  621. &local->hw.wiphy->bands[band]->channels[i];
  622. nchan++;
  623. }
  624. }
  625. local->int_scan_req->n_channels = nchan;
  626. } else {
  627. local->int_scan_req->channels[0] = chan;
  628. local->int_scan_req->n_channels = 1;
  629. }
  630. local->int_scan_req->ssids = &local->scan_ssid;
  631. local->int_scan_req->n_ssids = 1;
  632. memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
  633. local->int_scan_req->ssids[0].ssid_len = ssid_len;
  634. ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
  635. unlock:
  636. mutex_unlock(&local->scan_mtx);
  637. return ret;
  638. }
  639. void ieee80211_scan_cancel(struct ieee80211_local *local)
  640. {
  641. bool abortscan;
  642. cancel_delayed_work_sync(&local->scan_work);
  643. /*
  644. * Only call this function when a scan can't be
  645. * queued -- mostly at suspend under RTNL.
  646. */
  647. mutex_lock(&local->scan_mtx);
  648. abortscan = test_bit(SCAN_SW_SCANNING, &local->scanning) ||
  649. (!local->scanning && local->scan_req);
  650. mutex_unlock(&local->scan_mtx);
  651. if (abortscan)
  652. ieee80211_scan_completed(&local->hw, true);
  653. }