scan.c 25 KB

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