scan.c 27 KB

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