scan.c 32 KB

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