scan.c 30 KB

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