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