scan.c 28 KB

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