scan.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942
  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.h>
  17. #include <net/sch_generic.h>
  18. #include <linux/slab.h>
  19. #include <linux/export.h>
  20. #include <net/mac80211.h>
  21. #include "ieee80211_i.h"
  22. #include "driver-ops.h"
  23. #include "mesh.h"
  24. #define IEEE80211_PROBE_DELAY (HZ / 33)
  25. #define IEEE80211_CHANNEL_TIME (HZ / 33)
  26. #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 8)
  27. struct ieee80211_bss *
  28. ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
  29. u8 *ssid, u8 ssid_len)
  30. {
  31. struct cfg80211_bss *cbss;
  32. cbss = cfg80211_get_bss(local->hw.wiphy,
  33. ieee80211_get_channel(local->hw.wiphy, freq),
  34. bssid, ssid, ssid_len, 0, 0);
  35. if (!cbss)
  36. return NULL;
  37. return (void *)cbss->priv;
  38. }
  39. static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss)
  40. {
  41. struct ieee80211_bss *bss = (void *)cbss->priv;
  42. kfree(bss_mesh_id(bss));
  43. kfree(bss_mesh_cfg(bss));
  44. }
  45. void ieee80211_rx_bss_put(struct ieee80211_local *local,
  46. struct ieee80211_bss *bss)
  47. {
  48. if (!bss)
  49. return;
  50. cfg80211_put_bss(container_of((void *)bss, struct cfg80211_bss, priv));
  51. }
  52. static bool is_uapsd_supported(struct ieee802_11_elems *elems)
  53. {
  54. u8 qos_info;
  55. if (elems->wmm_info && elems->wmm_info_len == 7
  56. && elems->wmm_info[5] == 1)
  57. qos_info = elems->wmm_info[6];
  58. else if (elems->wmm_param && elems->wmm_param_len == 24
  59. && elems->wmm_param[5] == 1)
  60. qos_info = elems->wmm_param[6];
  61. else
  62. /* no valid wmm information or parameter element found */
  63. return false;
  64. return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
  65. }
  66. struct ieee80211_bss *
  67. ieee80211_bss_info_update(struct ieee80211_local *local,
  68. struct ieee80211_rx_status *rx_status,
  69. struct ieee80211_mgmt *mgmt,
  70. size_t len,
  71. struct ieee802_11_elems *elems,
  72. struct ieee80211_channel *channel,
  73. bool beacon)
  74. {
  75. struct cfg80211_bss *cbss;
  76. struct ieee80211_bss *bss;
  77. int clen, srlen;
  78. s32 signal = 0;
  79. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  80. signal = rx_status->signal * 100;
  81. else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
  82. signal = (rx_status->signal * 100) / local->hw.max_signal;
  83. cbss = cfg80211_inform_bss_frame(local->hw.wiphy, channel,
  84. mgmt, len, signal, GFP_ATOMIC);
  85. if (!cbss)
  86. return NULL;
  87. cbss->free_priv = ieee80211_rx_bss_free;
  88. bss = (void *)cbss->priv;
  89. /* save the ERP value so that it is available at association time */
  90. if (elems->erp_info && elems->erp_info_len >= 1) {
  91. bss->erp_value = elems->erp_info[0];
  92. bss->has_erp_value = true;
  93. }
  94. if (elems->tim) {
  95. struct ieee80211_tim_ie *tim_ie =
  96. (struct ieee80211_tim_ie *)elems->tim;
  97. bss->dtim_period = tim_ie->dtim_period;
  98. }
  99. /* If the beacon had no TIM IE, or it was invalid, use 1 */
  100. if (beacon && !bss->dtim_period)
  101. bss->dtim_period = 1;
  102. /* replace old supported rates if we get new values */
  103. srlen = 0;
  104. if (elems->supp_rates) {
  105. clen = IEEE80211_MAX_SUPP_RATES;
  106. if (clen > elems->supp_rates_len)
  107. clen = elems->supp_rates_len;
  108. memcpy(bss->supp_rates, elems->supp_rates, clen);
  109. srlen += clen;
  110. }
  111. if (elems->ext_supp_rates) {
  112. clen = IEEE80211_MAX_SUPP_RATES - srlen;
  113. if (clen > elems->ext_supp_rates_len)
  114. clen = elems->ext_supp_rates_len;
  115. memcpy(bss->supp_rates + srlen, elems->ext_supp_rates, clen);
  116. srlen += clen;
  117. }
  118. if (srlen)
  119. bss->supp_rates_len = srlen;
  120. bss->wmm_used = elems->wmm_param || elems->wmm_info;
  121. bss->uapsd_supported = is_uapsd_supported(elems);
  122. if (!beacon)
  123. bss->last_probe_resp = jiffies;
  124. return bss;
  125. }
  126. ieee80211_rx_result
  127. ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  128. {
  129. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  130. struct ieee80211_mgmt *mgmt;
  131. struct ieee80211_bss *bss;
  132. u8 *elements;
  133. struct ieee80211_channel *channel;
  134. size_t baselen;
  135. int freq;
  136. __le16 fc;
  137. bool presp, beacon = false;
  138. struct ieee802_11_elems elems;
  139. if (skb->len < 2)
  140. return RX_DROP_UNUSABLE;
  141. mgmt = (struct ieee80211_mgmt *) skb->data;
  142. fc = mgmt->frame_control;
  143. if (ieee80211_is_ctl(fc))
  144. return RX_CONTINUE;
  145. if (skb->len < 24)
  146. return RX_CONTINUE;
  147. presp = ieee80211_is_probe_resp(fc);
  148. if (presp) {
  149. /* ignore ProbeResp to foreign address */
  150. if (memcmp(mgmt->da, sdata->vif.addr, ETH_ALEN))
  151. return RX_DROP_MONITOR;
  152. presp = true;
  153. elements = mgmt->u.probe_resp.variable;
  154. baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  155. } else {
  156. beacon = ieee80211_is_beacon(fc);
  157. baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
  158. elements = mgmt->u.beacon.variable;
  159. }
  160. if (!presp && !beacon)
  161. return RX_CONTINUE;
  162. if (baselen > skb->len)
  163. return RX_DROP_MONITOR;
  164. ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
  165. if (elems.ds_params && elems.ds_params_len == 1)
  166. freq = ieee80211_channel_to_frequency(elems.ds_params[0],
  167. rx_status->band);
  168. else
  169. freq = rx_status->freq;
  170. channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
  171. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  172. return RX_DROP_MONITOR;
  173. bss = ieee80211_bss_info_update(sdata->local, rx_status,
  174. mgmt, skb->len, &elems,
  175. channel, beacon);
  176. if (bss)
  177. ieee80211_rx_bss_put(sdata->local, bss);
  178. if (channel == sdata->local->oper_channel)
  179. return RX_CONTINUE;
  180. dev_kfree_skb(skb);
  181. return RX_QUEUED;
  182. }
  183. /* return false if no more work */
  184. static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
  185. {
  186. struct cfg80211_scan_request *req = local->scan_req;
  187. enum ieee80211_band band;
  188. int i, ielen, n_chans;
  189. do {
  190. if (local->hw_scan_band == IEEE80211_NUM_BANDS)
  191. return false;
  192. band = local->hw_scan_band;
  193. n_chans = 0;
  194. for (i = 0; i < req->n_channels; i++) {
  195. if (req->channels[i]->band == band) {
  196. local->hw_scan_req->channels[n_chans] =
  197. req->channels[i];
  198. n_chans++;
  199. }
  200. }
  201. local->hw_scan_band++;
  202. } while (!n_chans);
  203. local->hw_scan_req->n_channels = n_chans;
  204. ielen = ieee80211_build_preq_ies(local, (u8 *)local->hw_scan_req->ie,
  205. req->ie, req->ie_len, band,
  206. req->rates[band], 0);
  207. local->hw_scan_req->ie_len = ielen;
  208. local->hw_scan_req->no_cck = req->no_cck;
  209. return true;
  210. }
  211. static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted,
  212. bool was_hw_scan)
  213. {
  214. struct ieee80211_local *local = hw_to_local(hw);
  215. lockdep_assert_held(&local->mtx);
  216. /*
  217. * It's ok to abort a not-yet-running scan (that
  218. * we have one at all will be verified by checking
  219. * local->scan_req next), but not to complete it
  220. * successfully.
  221. */
  222. if (WARN_ON(!local->scanning && !aborted))
  223. aborted = true;
  224. if (WARN_ON(!local->scan_req))
  225. return;
  226. if (was_hw_scan && !aborted && ieee80211_prep_hw_scan(local)) {
  227. int rc = drv_hw_scan(local, local->scan_sdata, local->hw_scan_req);
  228. if (rc == 0)
  229. return;
  230. }
  231. kfree(local->hw_scan_req);
  232. local->hw_scan_req = NULL;
  233. if (local->scan_req != local->int_scan_req)
  234. cfg80211_scan_done(local->scan_req, aborted);
  235. local->scan_req = NULL;
  236. local->scan_sdata = NULL;
  237. local->scanning = 0;
  238. local->scan_channel = NULL;
  239. /* Set power back to normal operating levels. */
  240. ieee80211_hw_config(local, 0);
  241. if (!was_hw_scan) {
  242. ieee80211_configure_filter(local);
  243. drv_sw_scan_complete(local);
  244. ieee80211_offchannel_return(local, true);
  245. }
  246. ieee80211_recalc_idle(local);
  247. ieee80211_mlme_notify_scan_completed(local);
  248. ieee80211_ibss_notify_scan_completed(local);
  249. ieee80211_mesh_notify_scan_completed(local);
  250. ieee80211_queue_work(&local->hw, &local->work_work);
  251. }
  252. void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
  253. {
  254. struct ieee80211_local *local = hw_to_local(hw);
  255. trace_api_scan_completed(local, aborted);
  256. set_bit(SCAN_COMPLETED, &local->scanning);
  257. if (aborted)
  258. set_bit(SCAN_ABORTED, &local->scanning);
  259. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
  260. }
  261. EXPORT_SYMBOL(ieee80211_scan_completed);
  262. static int ieee80211_start_sw_scan(struct ieee80211_local *local)
  263. {
  264. /*
  265. * Hardware/driver doesn't support hw_scan, so use software
  266. * scanning instead. First send a nullfunc frame with power save
  267. * bit on so that AP will buffer the frames for us while we are not
  268. * listening, then send probe requests to each channel and wait for
  269. * the responses. After all channels are scanned, tune back to the
  270. * original channel and send a nullfunc frame with power save bit
  271. * off to trigger the AP to send us all the buffered frames.
  272. *
  273. * Note that while local->sw_scanning is true everything else but
  274. * nullfunc frames and probe requests will be dropped in
  275. * ieee80211_tx_h_check_assoc().
  276. */
  277. drv_sw_scan_start(local);
  278. local->leave_oper_channel_time = 0;
  279. local->next_scan_state = SCAN_DECISION;
  280. local->scan_channel_idx = 0;
  281. ieee80211_offchannel_stop_vifs(local, true);
  282. ieee80211_configure_filter(local);
  283. /* We need to set power level at maximum rate for scanning. */
  284. ieee80211_hw_config(local, 0);
  285. ieee80211_queue_delayed_work(&local->hw,
  286. &local->scan_work, 0);
  287. return 0;
  288. }
  289. static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
  290. struct cfg80211_scan_request *req)
  291. {
  292. struct ieee80211_local *local = sdata->local;
  293. int rc;
  294. lockdep_assert_held(&local->mtx);
  295. if (local->scan_req)
  296. return -EBUSY;
  297. if (!list_empty(&local->work_list)) {
  298. /* wait for the work to finish/time out */
  299. local->scan_req = req;
  300. local->scan_sdata = sdata;
  301. return 0;
  302. }
  303. if (local->ops->hw_scan) {
  304. u8 *ies;
  305. local->hw_scan_req = kmalloc(
  306. sizeof(*local->hw_scan_req) +
  307. req->n_channels * sizeof(req->channels[0]) +
  308. 2 + IEEE80211_MAX_SSID_LEN + local->scan_ies_len +
  309. req->ie_len, GFP_KERNEL);
  310. if (!local->hw_scan_req)
  311. return -ENOMEM;
  312. local->hw_scan_req->ssids = req->ssids;
  313. local->hw_scan_req->n_ssids = req->n_ssids;
  314. ies = (u8 *)local->hw_scan_req +
  315. sizeof(*local->hw_scan_req) +
  316. req->n_channels * sizeof(req->channels[0]);
  317. local->hw_scan_req->ie = ies;
  318. local->hw_scan_band = 0;
  319. /*
  320. * After allocating local->hw_scan_req, we must
  321. * go through until ieee80211_prep_hw_scan(), so
  322. * anything that might be changed here and leave
  323. * this function early must not go after this
  324. * allocation.
  325. */
  326. }
  327. local->scan_req = req;
  328. local->scan_sdata = sdata;
  329. if (local->ops->hw_scan)
  330. __set_bit(SCAN_HW_SCANNING, &local->scanning);
  331. else
  332. __set_bit(SCAN_SW_SCANNING, &local->scanning);
  333. ieee80211_recalc_idle(local);
  334. if (local->ops->hw_scan) {
  335. WARN_ON(!ieee80211_prep_hw_scan(local));
  336. rc = drv_hw_scan(local, sdata, local->hw_scan_req);
  337. } else
  338. rc = ieee80211_start_sw_scan(local);
  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 void 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. /*
  371. * check if at least one STA interface is associated,
  372. * check if at least one STA interface has pending tx frames
  373. * and grab the lowest used beacon interval
  374. */
  375. mutex_lock(&local->iflist_mtx);
  376. list_for_each_entry(sdata, &local->interfaces, list) {
  377. if (!ieee80211_sdata_running(sdata))
  378. continue;
  379. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  380. if (sdata->u.mgd.associated) {
  381. associated = true;
  382. if (sdata->vif.bss_conf.beacon_int <
  383. min_beacon_int || min_beacon_int == 0)
  384. min_beacon_int =
  385. sdata->vif.bss_conf.beacon_int;
  386. if (!qdisc_all_tx_empty(sdata->dev)) {
  387. tx_empty = false;
  388. break;
  389. }
  390. }
  391. }
  392. }
  393. mutex_unlock(&local->iflist_mtx);
  394. next_chan = local->scan_req->channels[local->scan_channel_idx];
  395. /*
  396. * we're currently scanning a different channel, let's
  397. * see if we can scan another channel without interfering
  398. * with the current traffic situation.
  399. *
  400. * Since we don't know if the AP has pending frames for us
  401. * we can only check for our tx queues and use the current
  402. * pm_qos requirements for rx. Hence, if no tx traffic occurs
  403. * at all we will scan as many channels in a row as the pm_qos
  404. * latency allows us to. Additionally we also check for the
  405. * currently negotiated listen interval to prevent losing
  406. * frames unnecessarily.
  407. *
  408. * Otherwise switch back to the operating channel.
  409. */
  410. bad_latency = time_after(jiffies +
  411. ieee80211_scan_get_channel_time(next_chan),
  412. local->leave_oper_channel_time +
  413. usecs_to_jiffies(pm_qos_request(PM_QOS_NETWORK_LATENCY)));
  414. listen_int_exceeded = time_after(jiffies +
  415. ieee80211_scan_get_channel_time(next_chan),
  416. local->leave_oper_channel_time +
  417. usecs_to_jiffies(min_beacon_int * 1024) *
  418. local->hw.conf.listen_interval);
  419. if (associated && (!tx_empty || bad_latency || listen_int_exceeded))
  420. local->next_scan_state = SCAN_SUSPEND;
  421. else
  422. local->next_scan_state = SCAN_SET_CHANNEL;
  423. *next_delay = 0;
  424. }
  425. static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
  426. unsigned long *next_delay)
  427. {
  428. int skip;
  429. struct ieee80211_channel *chan;
  430. skip = 0;
  431. chan = local->scan_req->channels[local->scan_channel_idx];
  432. local->scan_channel = chan;
  433. if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
  434. skip = 1;
  435. /* advance state machine to next channel/band */
  436. local->scan_channel_idx++;
  437. if (skip) {
  438. /* if we skip this channel return to the decision state */
  439. local->next_scan_state = SCAN_DECISION;
  440. return;
  441. }
  442. /*
  443. * Probe delay is used to update the NAV, cf. 11.1.3.2.2
  444. * (which unfortunately doesn't say _why_ step a) is done,
  445. * but it waits for the probe delay or until a frame is
  446. * received - and the received frame would update the NAV).
  447. * For now, we do not support waiting until a frame is
  448. * received.
  449. *
  450. * In any case, it is not necessary for a passive scan.
  451. */
  452. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  453. !local->scan_req->n_ssids) {
  454. *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  455. local->next_scan_state = SCAN_DECISION;
  456. return;
  457. }
  458. /* active scan, send probes */
  459. *next_delay = IEEE80211_PROBE_DELAY;
  460. local->next_scan_state = SCAN_SEND_PROBE;
  461. }
  462. static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
  463. unsigned long *next_delay)
  464. {
  465. int i;
  466. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  467. enum ieee80211_band band = local->hw.conf.channel->band;
  468. for (i = 0; i < local->scan_req->n_ssids; i++)
  469. ieee80211_send_probe_req(
  470. sdata, NULL,
  471. local->scan_req->ssids[i].ssid,
  472. local->scan_req->ssids[i].ssid_len,
  473. local->scan_req->ie, local->scan_req->ie_len,
  474. local->scan_req->rates[band], false,
  475. local->scan_req->no_cck);
  476. /*
  477. * After sending probe requests, wait for probe responses
  478. * on the channel.
  479. */
  480. *next_delay = IEEE80211_CHANNEL_TIME;
  481. local->next_scan_state = SCAN_DECISION;
  482. }
  483. static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
  484. unsigned long *next_delay)
  485. {
  486. /* switch back to the operating channel */
  487. local->scan_channel = NULL;
  488. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  489. /*
  490. * Re-enable vifs and beaconing. Leave PS
  491. * in off-channel state..will put that back
  492. * on-channel at the end of scanning.
  493. */
  494. ieee80211_offchannel_return(local, false);
  495. *next_delay = HZ / 5;
  496. /* afterwards, resume scan & go to next channel */
  497. local->next_scan_state = SCAN_RESUME;
  498. }
  499. static void ieee80211_scan_state_resume(struct ieee80211_local *local,
  500. unsigned long *next_delay)
  501. {
  502. /* PS already is in off-channel mode */
  503. ieee80211_offchannel_stop_vifs(local, false);
  504. if (local->ops->flush) {
  505. drv_flush(local, false);
  506. *next_delay = 0;
  507. } else
  508. *next_delay = HZ / 10;
  509. /* remember when we left the operating channel */
  510. local->leave_oper_channel_time = jiffies;
  511. /* advance to the next channel to be scanned */
  512. local->next_scan_state = SCAN_SET_CHANNEL;
  513. }
  514. void ieee80211_scan_work(struct work_struct *work)
  515. {
  516. struct ieee80211_local *local =
  517. container_of(work, struct ieee80211_local, scan_work.work);
  518. struct ieee80211_sub_if_data *sdata;
  519. unsigned long next_delay = 0;
  520. bool aborted, hw_scan;
  521. mutex_lock(&local->mtx);
  522. sdata = local->scan_sdata;
  523. if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
  524. aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
  525. goto out_complete;
  526. }
  527. if (!sdata || !local->scan_req)
  528. goto out;
  529. if (local->scan_req && !local->scanning) {
  530. struct cfg80211_scan_request *req = local->scan_req;
  531. int rc;
  532. local->scan_req = NULL;
  533. local->scan_sdata = NULL;
  534. rc = __ieee80211_start_scan(sdata, req);
  535. if (rc) {
  536. /* need to complete scan in cfg80211 */
  537. local->scan_req = req;
  538. aborted = true;
  539. goto out_complete;
  540. } else
  541. goto out;
  542. }
  543. /*
  544. * Avoid re-scheduling when the sdata is going away.
  545. */
  546. if (!ieee80211_sdata_running(sdata)) {
  547. aborted = true;
  548. goto out_complete;
  549. }
  550. /*
  551. * as long as no delay is required advance immediately
  552. * without scheduling a new work
  553. */
  554. do {
  555. if (!ieee80211_sdata_running(sdata)) {
  556. aborted = true;
  557. goto out_complete;
  558. }
  559. switch (local->next_scan_state) {
  560. case SCAN_DECISION:
  561. /* if no more bands/channels left, complete scan */
  562. if (local->scan_channel_idx >= local->scan_req->n_channels) {
  563. aborted = false;
  564. goto out_complete;
  565. }
  566. ieee80211_scan_state_decision(local, &next_delay);
  567. break;
  568. case SCAN_SET_CHANNEL:
  569. ieee80211_scan_state_set_channel(local, &next_delay);
  570. break;
  571. case SCAN_SEND_PROBE:
  572. ieee80211_scan_state_send_probe(local, &next_delay);
  573. break;
  574. case SCAN_SUSPEND:
  575. ieee80211_scan_state_suspend(local, &next_delay);
  576. break;
  577. case SCAN_RESUME:
  578. ieee80211_scan_state_resume(local, &next_delay);
  579. break;
  580. }
  581. } while (next_delay == 0);
  582. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
  583. goto out;
  584. out_complete:
  585. hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
  586. __ieee80211_scan_completed(&local->hw, aborted, hw_scan);
  587. out:
  588. mutex_unlock(&local->mtx);
  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->mtx);
  595. res = __ieee80211_start_scan(sdata, req);
  596. mutex_unlock(&sdata->local->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. struct ieee80211_channel *chan)
  602. {
  603. struct ieee80211_local *local = sdata->local;
  604. int ret = -EBUSY;
  605. enum ieee80211_band band;
  606. mutex_lock(&local->mtx);
  607. /* busy scanning */
  608. if (local->scan_req)
  609. goto unlock;
  610. /* fill internal scan request */
  611. if (!chan) {
  612. int i, nchan = 0;
  613. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  614. if (!local->hw.wiphy->bands[band])
  615. continue;
  616. for (i = 0;
  617. i < local->hw.wiphy->bands[band]->n_channels;
  618. i++) {
  619. local->int_scan_req->channels[nchan] =
  620. &local->hw.wiphy->bands[band]->channels[i];
  621. nchan++;
  622. }
  623. }
  624. local->int_scan_req->n_channels = nchan;
  625. } else {
  626. local->int_scan_req->channels[0] = chan;
  627. local->int_scan_req->n_channels = 1;
  628. }
  629. local->int_scan_req->ssids = &local->scan_ssid;
  630. local->int_scan_req->n_ssids = 1;
  631. memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
  632. local->int_scan_req->ssids[0].ssid_len = ssid_len;
  633. ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
  634. unlock:
  635. mutex_unlock(&local->mtx);
  636. return ret;
  637. }
  638. /*
  639. * Only call this function when a scan can't be queued -- under RTNL.
  640. */
  641. void ieee80211_scan_cancel(struct ieee80211_local *local)
  642. {
  643. /*
  644. * We are canceling software scan, or deferred scan that was not
  645. * yet really started (see __ieee80211_start_scan ).
  646. *
  647. * Regarding hardware scan:
  648. * - we can not call __ieee80211_scan_completed() as when
  649. * SCAN_HW_SCANNING bit is set this function change
  650. * local->hw_scan_req to operate on 5G band, what race with
  651. * driver which can use local->hw_scan_req
  652. *
  653. * - we can not cancel scan_work since driver can schedule it
  654. * by ieee80211_scan_completed(..., true) to finish scan
  655. *
  656. * Hence we only call the cancel_hw_scan() callback, but the low-level
  657. * driver is still responsible for calling ieee80211_scan_completed()
  658. * after the scan was completed/aborted.
  659. */
  660. mutex_lock(&local->mtx);
  661. if (!local->scan_req)
  662. goto out;
  663. if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
  664. if (local->ops->cancel_hw_scan)
  665. drv_cancel_hw_scan(local, local->scan_sdata);
  666. goto out;
  667. }
  668. /*
  669. * If the work is currently running, it must be blocked on
  670. * the mutex, but we'll set scan_sdata = NULL and it'll
  671. * simply exit once it acquires the mutex.
  672. */
  673. cancel_delayed_work(&local->scan_work);
  674. /* and clean up */
  675. __ieee80211_scan_completed(&local->hw, true, false);
  676. out:
  677. mutex_unlock(&local->mtx);
  678. }
  679. int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
  680. struct cfg80211_sched_scan_request *req)
  681. {
  682. struct ieee80211_local *local = sdata->local;
  683. int ret, i;
  684. mutex_lock(&sdata->local->mtx);
  685. if (local->sched_scanning) {
  686. ret = -EBUSY;
  687. goto out;
  688. }
  689. if (!local->ops->sched_scan_start) {
  690. ret = -ENOTSUPP;
  691. goto out;
  692. }
  693. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  694. local->sched_scan_ies.ie[i] = kzalloc(2 +
  695. IEEE80211_MAX_SSID_LEN +
  696. local->scan_ies_len +
  697. req->ie_len,
  698. GFP_KERNEL);
  699. if (!local->sched_scan_ies.ie[i]) {
  700. ret = -ENOMEM;
  701. goto out_free;
  702. }
  703. local->sched_scan_ies.len[i] =
  704. ieee80211_build_preq_ies(local,
  705. local->sched_scan_ies.ie[i],
  706. req->ie, req->ie_len, i,
  707. (u32) -1, 0);
  708. }
  709. ret = drv_sched_scan_start(local, sdata, req,
  710. &local->sched_scan_ies);
  711. if (ret == 0) {
  712. local->sched_scanning = true;
  713. goto out;
  714. }
  715. out_free:
  716. while (i > 0)
  717. kfree(local->sched_scan_ies.ie[--i]);
  718. out:
  719. mutex_unlock(&sdata->local->mtx);
  720. return ret;
  721. }
  722. int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata)
  723. {
  724. struct ieee80211_local *local = sdata->local;
  725. int ret = 0, i;
  726. mutex_lock(&sdata->local->mtx);
  727. if (!local->ops->sched_scan_stop) {
  728. ret = -ENOTSUPP;
  729. goto out;
  730. }
  731. if (local->sched_scanning) {
  732. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  733. kfree(local->sched_scan_ies.ie[i]);
  734. drv_sched_scan_stop(local, sdata);
  735. local->sched_scanning = false;
  736. }
  737. out:
  738. mutex_unlock(&sdata->local->mtx);
  739. return ret;
  740. }
  741. void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
  742. {
  743. struct ieee80211_local *local = hw_to_local(hw);
  744. trace_api_sched_scan_results(local);
  745. cfg80211_sched_scan_results(hw->wiphy);
  746. }
  747. EXPORT_SYMBOL(ieee80211_sched_scan_results);
  748. void ieee80211_sched_scan_stopped_work(struct work_struct *work)
  749. {
  750. struct ieee80211_local *local =
  751. container_of(work, struct ieee80211_local,
  752. sched_scan_stopped_work);
  753. int i;
  754. mutex_lock(&local->mtx);
  755. if (!local->sched_scanning) {
  756. mutex_unlock(&local->mtx);
  757. return;
  758. }
  759. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  760. kfree(local->sched_scan_ies.ie[i]);
  761. local->sched_scanning = false;
  762. mutex_unlock(&local->mtx);
  763. cfg80211_sched_scan_stopped(local->hw.wiphy);
  764. }
  765. void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
  766. {
  767. struct ieee80211_local *local = hw_to_local(hw);
  768. trace_api_sched_scan_stopped(local);
  769. ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work);
  770. }
  771. EXPORT_SYMBOL(ieee80211_sched_scan_stopped);