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