scan.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093
  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. bool was_hw_scan)
  236. {
  237. struct ieee80211_local *local = hw_to_local(hw);
  238. lockdep_assert_held(&local->mtx);
  239. /*
  240. * It's ok to abort a not-yet-running scan (that
  241. * we have one at all will be verified by checking
  242. * local->scan_req next), but not to complete it
  243. * successfully.
  244. */
  245. if (WARN_ON(!local->scanning && !aborted))
  246. aborted = true;
  247. if (WARN_ON(!local->scan_req))
  248. return;
  249. if (was_hw_scan && !aborted && ieee80211_prep_hw_scan(local)) {
  250. int rc;
  251. rc = drv_hw_scan(local,
  252. rcu_dereference_protected(local->scan_sdata,
  253. lockdep_is_held(&local->mtx)),
  254. local->hw_scan_req);
  255. if (rc == 0)
  256. return;
  257. }
  258. kfree(local->hw_scan_req);
  259. local->hw_scan_req = NULL;
  260. if (local->scan_req != local->int_scan_req)
  261. cfg80211_scan_done(local->scan_req, aborted);
  262. local->scan_req = NULL;
  263. rcu_assign_pointer(local->scan_sdata, NULL);
  264. local->scanning = 0;
  265. local->scan_chandef.chan = NULL;
  266. /* Set power back to normal operating levels. */
  267. ieee80211_hw_config(local, 0);
  268. if (!was_hw_scan) {
  269. ieee80211_configure_filter(local);
  270. drv_sw_scan_complete(local);
  271. ieee80211_offchannel_return(local);
  272. }
  273. ieee80211_recalc_idle(local);
  274. ieee80211_mlme_notify_scan_completed(local);
  275. ieee80211_ibss_notify_scan_completed(local);
  276. ieee80211_mesh_notify_scan_completed(local);
  277. ieee80211_start_next_roc(local);
  278. }
  279. void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
  280. {
  281. struct ieee80211_local *local = hw_to_local(hw);
  282. trace_api_scan_completed(local, aborted);
  283. set_bit(SCAN_COMPLETED, &local->scanning);
  284. if (aborted)
  285. set_bit(SCAN_ABORTED, &local->scanning);
  286. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
  287. }
  288. EXPORT_SYMBOL(ieee80211_scan_completed);
  289. static int ieee80211_start_sw_scan(struct ieee80211_local *local)
  290. {
  291. /* Software scan is not supported in multi-channel cases */
  292. if (local->use_chanctx)
  293. return -EOPNOTSUPP;
  294. /*
  295. * Hardware/driver doesn't support hw_scan, so use software
  296. * scanning instead. First send a nullfunc frame with power save
  297. * bit on so that AP will buffer the frames for us while we are not
  298. * listening, then send probe requests to each channel and wait for
  299. * the responses. After all channels are scanned, tune back to the
  300. * original channel and send a nullfunc frame with power save bit
  301. * off to trigger the AP to send us all the buffered frames.
  302. *
  303. * Note that while local->sw_scanning is true everything else but
  304. * nullfunc frames and probe requests will be dropped in
  305. * ieee80211_tx_h_check_assoc().
  306. */
  307. drv_sw_scan_start(local);
  308. local->leave_oper_channel_time = jiffies;
  309. local->next_scan_state = SCAN_DECISION;
  310. local->scan_channel_idx = 0;
  311. ieee80211_offchannel_stop_vifs(local);
  312. /* ensure nullfunc is transmitted before leaving operating channel */
  313. ieee80211_flush_queues(local, NULL);
  314. ieee80211_configure_filter(local);
  315. /* We need to set power level at maximum rate for scanning. */
  316. ieee80211_hw_config(local, 0);
  317. ieee80211_queue_delayed_work(&local->hw,
  318. &local->scan_work, 0);
  319. return 0;
  320. }
  321. static bool ieee80211_can_scan(struct ieee80211_local *local,
  322. struct ieee80211_sub_if_data *sdata)
  323. {
  324. if (local->radar_detect_enabled)
  325. return false;
  326. if (!list_empty(&local->roc_list))
  327. return false;
  328. if (sdata->vif.type == NL80211_IFTYPE_STATION &&
  329. sdata->u.mgd.flags & IEEE80211_STA_CONNECTION_POLL)
  330. return false;
  331. return true;
  332. }
  333. void ieee80211_run_deferred_scan(struct ieee80211_local *local)
  334. {
  335. lockdep_assert_held(&local->mtx);
  336. if (!local->scan_req || local->scanning)
  337. return;
  338. if (!ieee80211_can_scan(local,
  339. rcu_dereference_protected(
  340. local->scan_sdata,
  341. lockdep_is_held(&local->mtx))))
  342. return;
  343. ieee80211_queue_delayed_work(&local->hw, &local->scan_work,
  344. round_jiffies_relative(0));
  345. }
  346. static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
  347. unsigned long *next_delay)
  348. {
  349. int i;
  350. struct ieee80211_sub_if_data *sdata;
  351. enum ieee80211_band band = local->hw.conf.chandef.chan->band;
  352. u32 tx_flags;
  353. tx_flags = IEEE80211_TX_INTFL_OFFCHAN_TX_OK;
  354. if (local->scan_req->no_cck)
  355. tx_flags |= IEEE80211_TX_CTL_NO_CCK_RATE;
  356. sdata = rcu_dereference_protected(local->scan_sdata,
  357. lockdep_is_held(&local->mtx));
  358. for (i = 0; i < local->scan_req->n_ssids; i++)
  359. ieee80211_send_probe_req(
  360. sdata, NULL,
  361. local->scan_req->ssids[i].ssid,
  362. local->scan_req->ssids[i].ssid_len,
  363. local->scan_req->ie, local->scan_req->ie_len,
  364. local->scan_req->rates[band], false,
  365. tx_flags, local->hw.conf.chandef.chan, true);
  366. /*
  367. * After sending probe requests, wait for probe responses
  368. * on the channel.
  369. */
  370. *next_delay = IEEE80211_CHANNEL_TIME;
  371. local->next_scan_state = SCAN_DECISION;
  372. }
  373. static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
  374. struct cfg80211_scan_request *req)
  375. {
  376. struct ieee80211_local *local = sdata->local;
  377. int rc;
  378. lockdep_assert_held(&local->mtx);
  379. if (local->scan_req)
  380. return -EBUSY;
  381. if (!ieee80211_can_scan(local, sdata)) {
  382. /* wait for the work to finish/time out */
  383. local->scan_req = req;
  384. rcu_assign_pointer(local->scan_sdata, sdata);
  385. return 0;
  386. }
  387. if (local->ops->hw_scan) {
  388. u8 *ies;
  389. local->hw_scan_ies_bufsize = 2 + IEEE80211_MAX_SSID_LEN +
  390. local->scan_ies_len +
  391. 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_PASSIVE_SCAN) ||
  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_PASSIVE_SCAN)
  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_PASSIVE_SCAN ||
  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, hw_scan;
  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. * Avoid re-scheduling when the sdata is going away.
  652. */
  653. if (!ieee80211_sdata_running(sdata)) {
  654. aborted = true;
  655. goto out_complete;
  656. }
  657. /*
  658. * as long as no delay is required advance immediately
  659. * without scheduling a new work
  660. */
  661. do {
  662. if (!ieee80211_sdata_running(sdata)) {
  663. aborted = true;
  664. goto out_complete;
  665. }
  666. switch (local->next_scan_state) {
  667. case SCAN_DECISION:
  668. /* if no more bands/channels left, complete scan */
  669. if (local->scan_channel_idx >= local->scan_req->n_channels) {
  670. aborted = false;
  671. goto out_complete;
  672. }
  673. ieee80211_scan_state_decision(local, &next_delay);
  674. break;
  675. case SCAN_SET_CHANNEL:
  676. ieee80211_scan_state_set_channel(local, &next_delay);
  677. break;
  678. case SCAN_SEND_PROBE:
  679. ieee80211_scan_state_send_probe(local, &next_delay);
  680. break;
  681. case SCAN_SUSPEND:
  682. ieee80211_scan_state_suspend(local, &next_delay);
  683. break;
  684. case SCAN_RESUME:
  685. ieee80211_scan_state_resume(local, &next_delay);
  686. break;
  687. case SCAN_ABORT:
  688. aborted = true;
  689. goto out_complete;
  690. }
  691. } while (next_delay == 0);
  692. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
  693. goto out;
  694. out_complete:
  695. hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
  696. __ieee80211_scan_completed(&local->hw, aborted, hw_scan);
  697. out:
  698. mutex_unlock(&local->mtx);
  699. }
  700. int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
  701. struct cfg80211_scan_request *req)
  702. {
  703. int res;
  704. mutex_lock(&sdata->local->mtx);
  705. res = __ieee80211_start_scan(sdata, req);
  706. mutex_unlock(&sdata->local->mtx);
  707. return res;
  708. }
  709. int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
  710. const u8 *ssid, u8 ssid_len,
  711. struct ieee80211_channel *chan,
  712. enum nl80211_bss_scan_width scan_width)
  713. {
  714. struct ieee80211_local *local = sdata->local;
  715. int ret = -EBUSY;
  716. enum ieee80211_band band;
  717. mutex_lock(&local->mtx);
  718. /* busy scanning */
  719. if (local->scan_req)
  720. goto unlock;
  721. /* fill internal scan request */
  722. if (!chan) {
  723. int i, max_n;
  724. int n_ch = 0;
  725. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  726. if (!local->hw.wiphy->bands[band])
  727. continue;
  728. max_n = local->hw.wiphy->bands[band]->n_channels;
  729. for (i = 0; i < max_n; i++) {
  730. struct ieee80211_channel *tmp_ch =
  731. &local->hw.wiphy->bands[band]->channels[i];
  732. if (tmp_ch->flags & (IEEE80211_CHAN_NO_IBSS |
  733. IEEE80211_CHAN_DISABLED))
  734. continue;
  735. local->int_scan_req->channels[n_ch] = tmp_ch;
  736. n_ch++;
  737. }
  738. }
  739. if (WARN_ON_ONCE(n_ch == 0))
  740. goto unlock;
  741. local->int_scan_req->n_channels = n_ch;
  742. } else {
  743. if (WARN_ON_ONCE(chan->flags & (IEEE80211_CHAN_NO_IBSS |
  744. IEEE80211_CHAN_DISABLED)))
  745. goto unlock;
  746. local->int_scan_req->channels[0] = chan;
  747. local->int_scan_req->n_channels = 1;
  748. }
  749. local->int_scan_req->ssids = &local->scan_ssid;
  750. local->int_scan_req->n_ssids = 1;
  751. local->int_scan_req->scan_width = scan_width;
  752. memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
  753. local->int_scan_req->ssids[0].ssid_len = ssid_len;
  754. ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
  755. unlock:
  756. mutex_unlock(&local->mtx);
  757. return ret;
  758. }
  759. /*
  760. * Only call this function when a scan can't be queued -- under RTNL.
  761. */
  762. void ieee80211_scan_cancel(struct ieee80211_local *local)
  763. {
  764. /*
  765. * We are canceling software scan, or deferred scan that was not
  766. * yet really started (see __ieee80211_start_scan ).
  767. *
  768. * Regarding hardware scan:
  769. * - we can not call __ieee80211_scan_completed() as when
  770. * SCAN_HW_SCANNING bit is set this function change
  771. * local->hw_scan_req to operate on 5G band, what race with
  772. * driver which can use local->hw_scan_req
  773. *
  774. * - we can not cancel scan_work since driver can schedule it
  775. * by ieee80211_scan_completed(..., true) to finish scan
  776. *
  777. * Hence we only call the cancel_hw_scan() callback, but the low-level
  778. * driver is still responsible for calling ieee80211_scan_completed()
  779. * after the scan was completed/aborted.
  780. */
  781. mutex_lock(&local->mtx);
  782. if (!local->scan_req)
  783. goto out;
  784. /*
  785. * We have a scan running and the driver already reported completion,
  786. * but the worker hasn't run yet or is stuck on the mutex - mark it as
  787. * cancelled.
  788. */
  789. if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
  790. test_bit(SCAN_COMPLETED, &local->scanning)) {
  791. set_bit(SCAN_HW_CANCELLED, &local->scanning);
  792. goto out;
  793. }
  794. if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
  795. /*
  796. * Make sure that __ieee80211_scan_completed doesn't trigger a
  797. * scan on another band.
  798. */
  799. set_bit(SCAN_HW_CANCELLED, &local->scanning);
  800. if (local->ops->cancel_hw_scan)
  801. drv_cancel_hw_scan(local,
  802. rcu_dereference_protected(local->scan_sdata,
  803. lockdep_is_held(&local->mtx)));
  804. goto out;
  805. }
  806. /*
  807. * If the work is currently running, it must be blocked on
  808. * the mutex, but we'll set scan_sdata = NULL and it'll
  809. * simply exit once it acquires the mutex.
  810. */
  811. cancel_delayed_work(&local->scan_work);
  812. /* and clean up */
  813. __ieee80211_scan_completed(&local->hw, true, false);
  814. out:
  815. mutex_unlock(&local->mtx);
  816. }
  817. int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
  818. struct cfg80211_sched_scan_request *req)
  819. {
  820. struct ieee80211_local *local = sdata->local;
  821. struct ieee80211_sched_scan_ies sched_scan_ies = {};
  822. struct cfg80211_chan_def chandef;
  823. int ret, i, iebufsz;
  824. iebufsz = 2 + IEEE80211_MAX_SSID_LEN +
  825. local->scan_ies_len + req->ie_len;
  826. mutex_lock(&local->mtx);
  827. if (rcu_access_pointer(local->sched_scan_sdata)) {
  828. ret = -EBUSY;
  829. goto out;
  830. }
  831. if (!local->ops->sched_scan_start) {
  832. ret = -ENOTSUPP;
  833. goto out;
  834. }
  835. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  836. if (!local->hw.wiphy->bands[i])
  837. continue;
  838. sched_scan_ies.ie[i] = kzalloc(iebufsz, GFP_KERNEL);
  839. if (!sched_scan_ies.ie[i]) {
  840. ret = -ENOMEM;
  841. goto out_free;
  842. }
  843. ieee80211_prepare_scan_chandef(&chandef, req->scan_width);
  844. sched_scan_ies.len[i] =
  845. ieee80211_build_preq_ies(local, sched_scan_ies.ie[i],
  846. iebufsz, req->ie, req->ie_len,
  847. i, (u32) -1, &chandef);
  848. }
  849. ret = drv_sched_scan_start(local, sdata, req, &sched_scan_ies);
  850. if (ret == 0)
  851. rcu_assign_pointer(local->sched_scan_sdata, sdata);
  852. out_free:
  853. while (i > 0)
  854. kfree(sched_scan_ies.ie[--i]);
  855. out:
  856. mutex_unlock(&local->mtx);
  857. return ret;
  858. }
  859. int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata)
  860. {
  861. struct ieee80211_local *local = sdata->local;
  862. int ret = 0;
  863. mutex_lock(&local->mtx);
  864. if (!local->ops->sched_scan_stop) {
  865. ret = -ENOTSUPP;
  866. goto out;
  867. }
  868. if (rcu_access_pointer(local->sched_scan_sdata))
  869. drv_sched_scan_stop(local, sdata);
  870. out:
  871. mutex_unlock(&local->mtx);
  872. return ret;
  873. }
  874. void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
  875. {
  876. struct ieee80211_local *local = hw_to_local(hw);
  877. trace_api_sched_scan_results(local);
  878. cfg80211_sched_scan_results(hw->wiphy);
  879. }
  880. EXPORT_SYMBOL(ieee80211_sched_scan_results);
  881. void ieee80211_sched_scan_stopped_work(struct work_struct *work)
  882. {
  883. struct ieee80211_local *local =
  884. container_of(work, struct ieee80211_local,
  885. sched_scan_stopped_work);
  886. mutex_lock(&local->mtx);
  887. if (!rcu_access_pointer(local->sched_scan_sdata)) {
  888. mutex_unlock(&local->mtx);
  889. return;
  890. }
  891. rcu_assign_pointer(local->sched_scan_sdata, NULL);
  892. mutex_unlock(&local->mtx);
  893. cfg80211_sched_scan_stopped(local->hw.wiphy);
  894. }
  895. void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
  896. {
  897. struct ieee80211_local *local = hw_to_local(hw);
  898. trace_api_sched_scan_stopped(local);
  899. ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work);
  900. }
  901. EXPORT_SYMBOL(ieee80211_sched_scan_stopped);