scan.c 30 KB

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