scan.c 33 KB

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