scan.c 32 KB

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