scan.c 20 KB

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