scan.c 27 KB

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