scan.c 29 KB

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