scan.c 21 KB

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