scan.c 20 KB

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