scan.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974
  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 / 8)
  28. struct ieee80211_bss *
  29. ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
  30. u8 *ssid, u8 ssid_len)
  31. {
  32. struct cfg80211_bss *cbss;
  33. cbss = cfg80211_get_bss(local->hw.wiphy,
  34. ieee80211_get_channel(local->hw.wiphy, freq),
  35. bssid, ssid, ssid_len, 0, 0);
  36. if (!cbss)
  37. return NULL;
  38. return (void *)cbss->priv;
  39. }
  40. static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss)
  41. {
  42. struct ieee80211_bss *bss = (void *)cbss->priv;
  43. kfree(bss_mesh_id(bss));
  44. kfree(bss_mesh_cfg(bss));
  45. }
  46. void ieee80211_rx_bss_put(struct ieee80211_local *local,
  47. struct ieee80211_bss *bss)
  48. {
  49. if (!bss)
  50. return;
  51. cfg80211_put_bss(container_of((void *)bss, struct cfg80211_bss, priv));
  52. }
  53. static bool is_uapsd_supported(struct ieee802_11_elems *elems)
  54. {
  55. u8 qos_info;
  56. if (elems->wmm_info && elems->wmm_info_len == 7
  57. && elems->wmm_info[5] == 1)
  58. qos_info = elems->wmm_info[6];
  59. else if (elems->wmm_param && elems->wmm_param_len == 24
  60. && elems->wmm_param[5] == 1)
  61. qos_info = elems->wmm_param[6];
  62. else
  63. /* no valid wmm information or parameter element found */
  64. return false;
  65. return qos_info & IEEE80211_WMM_IE_AP_QOSINFO_UAPSD;
  66. }
  67. struct ieee80211_bss *
  68. ieee80211_bss_info_update(struct ieee80211_local *local,
  69. struct ieee80211_rx_status *rx_status,
  70. struct ieee80211_mgmt *mgmt,
  71. size_t len,
  72. struct ieee802_11_elems *elems,
  73. struct ieee80211_channel *channel,
  74. bool beacon)
  75. {
  76. struct cfg80211_bss *cbss;
  77. struct ieee80211_bss *bss;
  78. int clen, srlen;
  79. s32 signal = 0;
  80. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  81. signal = rx_status->signal * 100;
  82. else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
  83. signal = (rx_status->signal * 100) / local->hw.max_signal;
  84. cbss = cfg80211_inform_bss_frame(local->hw.wiphy, channel,
  85. mgmt, len, signal, GFP_ATOMIC);
  86. if (!cbss)
  87. return NULL;
  88. cbss->free_priv = ieee80211_rx_bss_free;
  89. bss = (void *)cbss->priv;
  90. if (elems->parse_error) {
  91. if (beacon)
  92. bss->corrupt_data |= IEEE80211_BSS_CORRUPT_BEACON;
  93. else
  94. bss->corrupt_data |= IEEE80211_BSS_CORRUPT_PROBE_RESP;
  95. } else {
  96. if (beacon)
  97. bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_BEACON;
  98. else
  99. bss->corrupt_data &= ~IEEE80211_BSS_CORRUPT_PROBE_RESP;
  100. }
  101. /* save the ERP value so that it is available at association time */
  102. if (elems->erp_info && elems->erp_info_len >= 1 &&
  103. (!elems->parse_error ||
  104. !(bss->valid_data & IEEE80211_BSS_VALID_ERP))) {
  105. bss->erp_value = elems->erp_info[0];
  106. bss->has_erp_value = true;
  107. if (!elems->parse_error)
  108. bss->valid_data |= IEEE80211_BSS_VALID_ERP;
  109. }
  110. if (elems->tim && (!elems->parse_error ||
  111. !(bss->valid_data & IEEE80211_BSS_VALID_DTIM))) {
  112. struct ieee80211_tim_ie *tim_ie =
  113. (struct ieee80211_tim_ie *)elems->tim;
  114. bss->dtim_period = tim_ie->dtim_period;
  115. if (!elems->parse_error)
  116. bss->valid_data |= IEEE80211_BSS_VALID_DTIM;
  117. }
  118. /* If the beacon had no TIM IE, or it was invalid, use 1 */
  119. if (beacon && !bss->dtim_period)
  120. bss->dtim_period = 1;
  121. /* replace old supported rates if we get new values */
  122. if (!elems->parse_error ||
  123. !(bss->valid_data & IEEE80211_BSS_VALID_RATES)) {
  124. srlen = 0;
  125. if (elems->supp_rates) {
  126. clen = IEEE80211_MAX_SUPP_RATES;
  127. if (clen > elems->supp_rates_len)
  128. clen = elems->supp_rates_len;
  129. memcpy(bss->supp_rates, elems->supp_rates, clen);
  130. srlen += clen;
  131. }
  132. if (elems->ext_supp_rates) {
  133. clen = IEEE80211_MAX_SUPP_RATES - srlen;
  134. if (clen > elems->ext_supp_rates_len)
  135. clen = elems->ext_supp_rates_len;
  136. memcpy(bss->supp_rates + srlen, elems->ext_supp_rates,
  137. clen);
  138. srlen += clen;
  139. }
  140. if (srlen) {
  141. bss->supp_rates_len = srlen;
  142. if (!elems->parse_error)
  143. bss->valid_data |= IEEE80211_BSS_VALID_RATES;
  144. }
  145. }
  146. if (!elems->parse_error ||
  147. !(bss->valid_data & IEEE80211_BSS_VALID_WMM)) {
  148. bss->wmm_used = elems->wmm_param || elems->wmm_info;
  149. bss->uapsd_supported = is_uapsd_supported(elems);
  150. if (!elems->parse_error)
  151. bss->valid_data |= IEEE80211_BSS_VALID_WMM;
  152. }
  153. if (!beacon)
  154. bss->last_probe_resp = jiffies;
  155. return bss;
  156. }
  157. ieee80211_rx_result
  158. ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb)
  159. {
  160. struct ieee80211_rx_status *rx_status = IEEE80211_SKB_RXCB(skb);
  161. struct ieee80211_mgmt *mgmt;
  162. struct ieee80211_bss *bss;
  163. u8 *elements;
  164. struct ieee80211_channel *channel;
  165. size_t baselen;
  166. int freq;
  167. __le16 fc;
  168. bool presp, beacon = false;
  169. struct ieee802_11_elems elems;
  170. if (skb->len < 2)
  171. return RX_DROP_UNUSABLE;
  172. mgmt = (struct ieee80211_mgmt *) skb->data;
  173. fc = mgmt->frame_control;
  174. if (ieee80211_is_ctl(fc))
  175. return RX_CONTINUE;
  176. if (skb->len < 24)
  177. return RX_CONTINUE;
  178. presp = ieee80211_is_probe_resp(fc);
  179. if (presp) {
  180. /* ignore ProbeResp to foreign address */
  181. if (compare_ether_addr(mgmt->da, sdata->vif.addr))
  182. return RX_DROP_MONITOR;
  183. presp = true;
  184. elements = mgmt->u.probe_resp.variable;
  185. baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  186. } else {
  187. beacon = ieee80211_is_beacon(fc);
  188. baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
  189. elements = mgmt->u.beacon.variable;
  190. }
  191. if (!presp && !beacon)
  192. return RX_CONTINUE;
  193. if (baselen > skb->len)
  194. return RX_DROP_MONITOR;
  195. ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
  196. if (elems.ds_params && elems.ds_params_len == 1)
  197. freq = ieee80211_channel_to_frequency(elems.ds_params[0],
  198. rx_status->band);
  199. else
  200. freq = rx_status->freq;
  201. channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
  202. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  203. return RX_DROP_MONITOR;
  204. bss = ieee80211_bss_info_update(sdata->local, rx_status,
  205. mgmt, skb->len, &elems,
  206. channel, beacon);
  207. if (bss)
  208. ieee80211_rx_bss_put(sdata->local, bss);
  209. if (channel == sdata->local->oper_channel)
  210. return RX_CONTINUE;
  211. dev_kfree_skb(skb);
  212. return RX_QUEUED;
  213. }
  214. /* return false if no more work */
  215. static bool ieee80211_prep_hw_scan(struct ieee80211_local *local)
  216. {
  217. struct cfg80211_scan_request *req = local->scan_req;
  218. enum ieee80211_band band;
  219. int i, ielen, n_chans;
  220. do {
  221. if (local->hw_scan_band == IEEE80211_NUM_BANDS)
  222. return false;
  223. band = local->hw_scan_band;
  224. n_chans = 0;
  225. for (i = 0; i < req->n_channels; i++) {
  226. if (req->channels[i]->band == band) {
  227. local->hw_scan_req->channels[n_chans] =
  228. req->channels[i];
  229. n_chans++;
  230. }
  231. }
  232. local->hw_scan_band++;
  233. } while (!n_chans);
  234. local->hw_scan_req->n_channels = n_chans;
  235. ielen = ieee80211_build_preq_ies(local, (u8 *)local->hw_scan_req->ie,
  236. req->ie, req->ie_len, band,
  237. req->rates[band], 0);
  238. local->hw_scan_req->ie_len = ielen;
  239. local->hw_scan_req->no_cck = req->no_cck;
  240. return true;
  241. }
  242. static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted,
  243. bool was_hw_scan)
  244. {
  245. struct ieee80211_local *local = hw_to_local(hw);
  246. lockdep_assert_held(&local->mtx);
  247. /*
  248. * It's ok to abort a not-yet-running scan (that
  249. * we have one at all will be verified by checking
  250. * local->scan_req next), but not to complete it
  251. * successfully.
  252. */
  253. if (WARN_ON(!local->scanning && !aborted))
  254. aborted = true;
  255. if (WARN_ON(!local->scan_req))
  256. return;
  257. if (was_hw_scan && !aborted && ieee80211_prep_hw_scan(local)) {
  258. int rc = drv_hw_scan(local, local->scan_sdata, local->hw_scan_req);
  259. if (rc == 0)
  260. return;
  261. }
  262. kfree(local->hw_scan_req);
  263. local->hw_scan_req = NULL;
  264. if (local->scan_req != local->int_scan_req)
  265. cfg80211_scan_done(local->scan_req, aborted);
  266. local->scan_req = NULL;
  267. local->scan_sdata = NULL;
  268. local->scanning = 0;
  269. local->scan_channel = NULL;
  270. /* Set power back to normal operating levels. */
  271. ieee80211_hw_config(local, 0);
  272. if (!was_hw_scan) {
  273. ieee80211_configure_filter(local);
  274. drv_sw_scan_complete(local);
  275. ieee80211_offchannel_return(local, true);
  276. }
  277. ieee80211_recalc_idle(local);
  278. ieee80211_mlme_notify_scan_completed(local);
  279. ieee80211_ibss_notify_scan_completed(local);
  280. ieee80211_mesh_notify_scan_completed(local);
  281. ieee80211_queue_work(&local->hw, &local->work_work);
  282. }
  283. void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
  284. {
  285. struct ieee80211_local *local = hw_to_local(hw);
  286. trace_api_scan_completed(local, aborted);
  287. set_bit(SCAN_COMPLETED, &local->scanning);
  288. if (aborted)
  289. set_bit(SCAN_ABORTED, &local->scanning);
  290. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
  291. }
  292. EXPORT_SYMBOL(ieee80211_scan_completed);
  293. static int ieee80211_start_sw_scan(struct ieee80211_local *local)
  294. {
  295. /*
  296. * Hardware/driver doesn't support hw_scan, so use software
  297. * scanning instead. First send a nullfunc frame with power save
  298. * bit on so that AP will buffer the frames for us while we are not
  299. * listening, then send probe requests to each channel and wait for
  300. * the responses. After all channels are scanned, tune back to the
  301. * original channel and send a nullfunc frame with power save bit
  302. * off to trigger the AP to send us all the buffered frames.
  303. *
  304. * Note that while local->sw_scanning is true everything else but
  305. * nullfunc frames and probe requests will be dropped in
  306. * ieee80211_tx_h_check_assoc().
  307. */
  308. drv_sw_scan_start(local);
  309. local->leave_oper_channel_time = 0;
  310. local->next_scan_state = SCAN_DECISION;
  311. local->scan_channel_idx = 0;
  312. ieee80211_offchannel_stop_vifs(local, true);
  313. ieee80211_configure_filter(local);
  314. /* We need to set power level at maximum rate for scanning. */
  315. ieee80211_hw_config(local, 0);
  316. ieee80211_queue_delayed_work(&local->hw,
  317. &local->scan_work, 0);
  318. return 0;
  319. }
  320. static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
  321. struct cfg80211_scan_request *req)
  322. {
  323. struct ieee80211_local *local = sdata->local;
  324. int rc;
  325. lockdep_assert_held(&local->mtx);
  326. if (local->scan_req)
  327. return -EBUSY;
  328. if (!list_empty(&local->work_list)) {
  329. /* wait for the work to finish/time out */
  330. local->scan_req = req;
  331. local->scan_sdata = sdata;
  332. return 0;
  333. }
  334. if (local->ops->hw_scan) {
  335. u8 *ies;
  336. local->hw_scan_req = kmalloc(
  337. sizeof(*local->hw_scan_req) +
  338. req->n_channels * sizeof(req->channels[0]) +
  339. 2 + IEEE80211_MAX_SSID_LEN + local->scan_ies_len +
  340. req->ie_len, GFP_KERNEL);
  341. if (!local->hw_scan_req)
  342. return -ENOMEM;
  343. local->hw_scan_req->ssids = req->ssids;
  344. local->hw_scan_req->n_ssids = req->n_ssids;
  345. ies = (u8 *)local->hw_scan_req +
  346. sizeof(*local->hw_scan_req) +
  347. req->n_channels * sizeof(req->channels[0]);
  348. local->hw_scan_req->ie = ies;
  349. local->hw_scan_band = 0;
  350. /*
  351. * After allocating local->hw_scan_req, we must
  352. * go through until ieee80211_prep_hw_scan(), so
  353. * anything that might be changed here and leave
  354. * this function early must not go after this
  355. * allocation.
  356. */
  357. }
  358. local->scan_req = req;
  359. local->scan_sdata = sdata;
  360. if (local->ops->hw_scan)
  361. __set_bit(SCAN_HW_SCANNING, &local->scanning);
  362. else
  363. __set_bit(SCAN_SW_SCANNING, &local->scanning);
  364. ieee80211_recalc_idle(local);
  365. if (local->ops->hw_scan) {
  366. WARN_ON(!ieee80211_prep_hw_scan(local));
  367. rc = drv_hw_scan(local, sdata, local->hw_scan_req);
  368. } else
  369. rc = ieee80211_start_sw_scan(local);
  370. if (rc) {
  371. kfree(local->hw_scan_req);
  372. local->hw_scan_req = NULL;
  373. local->scanning = 0;
  374. ieee80211_recalc_idle(local);
  375. local->scan_req = NULL;
  376. local->scan_sdata = NULL;
  377. }
  378. return rc;
  379. }
  380. static unsigned long
  381. ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
  382. {
  383. /*
  384. * TODO: channel switching also consumes quite some time,
  385. * add that delay as well to get a better estimation
  386. */
  387. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  388. return IEEE80211_PASSIVE_CHANNEL_TIME;
  389. return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
  390. }
  391. static void ieee80211_scan_state_decision(struct ieee80211_local *local,
  392. unsigned long *next_delay)
  393. {
  394. bool associated = false;
  395. bool tx_empty = true;
  396. bool bad_latency;
  397. bool listen_int_exceeded;
  398. unsigned long min_beacon_int = 0;
  399. struct ieee80211_sub_if_data *sdata;
  400. struct ieee80211_channel *next_chan;
  401. /*
  402. * check if at least one STA interface is associated,
  403. * check if at least one STA interface has pending tx frames
  404. * and grab the lowest used beacon interval
  405. */
  406. mutex_lock(&local->iflist_mtx);
  407. list_for_each_entry(sdata, &local->interfaces, list) {
  408. if (!ieee80211_sdata_running(sdata))
  409. continue;
  410. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  411. if (sdata->u.mgd.associated) {
  412. associated = true;
  413. if (sdata->vif.bss_conf.beacon_int <
  414. min_beacon_int || min_beacon_int == 0)
  415. min_beacon_int =
  416. sdata->vif.bss_conf.beacon_int;
  417. if (!qdisc_all_tx_empty(sdata->dev)) {
  418. tx_empty = false;
  419. break;
  420. }
  421. }
  422. }
  423. }
  424. mutex_unlock(&local->iflist_mtx);
  425. next_chan = local->scan_req->channels[local->scan_channel_idx];
  426. /*
  427. * we're currently scanning a different channel, let's
  428. * see if we can scan another channel without interfering
  429. * with the current traffic situation.
  430. *
  431. * Since we don't know if the AP has pending frames for us
  432. * we can only check for our tx queues and use the current
  433. * pm_qos requirements for rx. Hence, if no tx traffic occurs
  434. * at all we will scan as many channels in a row as the pm_qos
  435. * latency allows us to. Additionally we also check for the
  436. * currently negotiated listen interval to prevent losing
  437. * frames unnecessarily.
  438. *
  439. * Otherwise switch back to the operating channel.
  440. */
  441. bad_latency = time_after(jiffies +
  442. ieee80211_scan_get_channel_time(next_chan),
  443. local->leave_oper_channel_time +
  444. usecs_to_jiffies(pm_qos_request(PM_QOS_NETWORK_LATENCY)));
  445. listen_int_exceeded = time_after(jiffies +
  446. ieee80211_scan_get_channel_time(next_chan),
  447. local->leave_oper_channel_time +
  448. usecs_to_jiffies(min_beacon_int * 1024) *
  449. local->hw.conf.listen_interval);
  450. if (associated && (!tx_empty || bad_latency || listen_int_exceeded))
  451. local->next_scan_state = SCAN_SUSPEND;
  452. else
  453. local->next_scan_state = SCAN_SET_CHANNEL;
  454. *next_delay = 0;
  455. }
  456. static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
  457. unsigned long *next_delay)
  458. {
  459. int skip;
  460. struct ieee80211_channel *chan;
  461. skip = 0;
  462. chan = local->scan_req->channels[local->scan_channel_idx];
  463. local->scan_channel = chan;
  464. if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
  465. skip = 1;
  466. /* advance state machine to next channel/band */
  467. local->scan_channel_idx++;
  468. if (skip) {
  469. /* if we skip this channel return to the decision state */
  470. local->next_scan_state = SCAN_DECISION;
  471. return;
  472. }
  473. /*
  474. * Probe delay is used to update the NAV, cf. 11.1.3.2.2
  475. * (which unfortunately doesn't say _why_ step a) is done,
  476. * but it waits for the probe delay or until a frame is
  477. * received - and the received frame would update the NAV).
  478. * For now, we do not support waiting until a frame is
  479. * received.
  480. *
  481. * In any case, it is not necessary for a passive scan.
  482. */
  483. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  484. !local->scan_req->n_ssids) {
  485. *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  486. local->next_scan_state = SCAN_DECISION;
  487. return;
  488. }
  489. /* active scan, send probes */
  490. *next_delay = IEEE80211_PROBE_DELAY;
  491. local->next_scan_state = SCAN_SEND_PROBE;
  492. }
  493. static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
  494. unsigned long *next_delay)
  495. {
  496. int i;
  497. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  498. enum ieee80211_band band = local->hw.conf.channel->band;
  499. for (i = 0; i < local->scan_req->n_ssids; i++)
  500. ieee80211_send_probe_req(
  501. sdata, NULL,
  502. local->scan_req->ssids[i].ssid,
  503. local->scan_req->ssids[i].ssid_len,
  504. local->scan_req->ie, local->scan_req->ie_len,
  505. local->scan_req->rates[band], false,
  506. local->scan_req->no_cck);
  507. /*
  508. * After sending probe requests, wait for probe responses
  509. * on the channel.
  510. */
  511. *next_delay = IEEE80211_CHANNEL_TIME;
  512. local->next_scan_state = SCAN_DECISION;
  513. }
  514. static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
  515. unsigned long *next_delay)
  516. {
  517. /* switch back to the operating channel */
  518. local->scan_channel = NULL;
  519. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  520. /*
  521. * Re-enable vifs and beaconing. Leave PS
  522. * in off-channel state..will put that back
  523. * on-channel at the end of scanning.
  524. */
  525. ieee80211_offchannel_return(local, false);
  526. *next_delay = HZ / 5;
  527. /* afterwards, resume scan & go to next channel */
  528. local->next_scan_state = SCAN_RESUME;
  529. }
  530. static void ieee80211_scan_state_resume(struct ieee80211_local *local,
  531. unsigned long *next_delay)
  532. {
  533. /* PS already is in off-channel mode */
  534. ieee80211_offchannel_stop_vifs(local, false);
  535. if (local->ops->flush) {
  536. drv_flush(local, false);
  537. *next_delay = 0;
  538. } else
  539. *next_delay = HZ / 10;
  540. /* remember when we left the operating channel */
  541. local->leave_oper_channel_time = jiffies;
  542. /* advance to the next channel to be scanned */
  543. local->next_scan_state = SCAN_SET_CHANNEL;
  544. }
  545. void ieee80211_scan_work(struct work_struct *work)
  546. {
  547. struct ieee80211_local *local =
  548. container_of(work, struct ieee80211_local, scan_work.work);
  549. struct ieee80211_sub_if_data *sdata;
  550. unsigned long next_delay = 0;
  551. bool aborted, hw_scan;
  552. mutex_lock(&local->mtx);
  553. sdata = local->scan_sdata;
  554. if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
  555. aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
  556. goto out_complete;
  557. }
  558. if (!sdata || !local->scan_req)
  559. goto out;
  560. if (local->scan_req && !local->scanning) {
  561. struct cfg80211_scan_request *req = local->scan_req;
  562. int rc;
  563. local->scan_req = NULL;
  564. local->scan_sdata = NULL;
  565. rc = __ieee80211_start_scan(sdata, req);
  566. if (rc) {
  567. /* need to complete scan in cfg80211 */
  568. local->scan_req = req;
  569. aborted = true;
  570. goto out_complete;
  571. } else
  572. goto out;
  573. }
  574. /*
  575. * Avoid re-scheduling when the sdata is going away.
  576. */
  577. if (!ieee80211_sdata_running(sdata)) {
  578. aborted = true;
  579. goto out_complete;
  580. }
  581. /*
  582. * as long as no delay is required advance immediately
  583. * without scheduling a new work
  584. */
  585. do {
  586. if (!ieee80211_sdata_running(sdata)) {
  587. aborted = true;
  588. goto out_complete;
  589. }
  590. switch (local->next_scan_state) {
  591. case SCAN_DECISION:
  592. /* if no more bands/channels left, complete scan */
  593. if (local->scan_channel_idx >= local->scan_req->n_channels) {
  594. aborted = false;
  595. goto out_complete;
  596. }
  597. ieee80211_scan_state_decision(local, &next_delay);
  598. break;
  599. case SCAN_SET_CHANNEL:
  600. ieee80211_scan_state_set_channel(local, &next_delay);
  601. break;
  602. case SCAN_SEND_PROBE:
  603. ieee80211_scan_state_send_probe(local, &next_delay);
  604. break;
  605. case SCAN_SUSPEND:
  606. ieee80211_scan_state_suspend(local, &next_delay);
  607. break;
  608. case SCAN_RESUME:
  609. ieee80211_scan_state_resume(local, &next_delay);
  610. break;
  611. }
  612. } while (next_delay == 0);
  613. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
  614. goto out;
  615. out_complete:
  616. hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
  617. __ieee80211_scan_completed(&local->hw, aborted, hw_scan);
  618. out:
  619. mutex_unlock(&local->mtx);
  620. }
  621. int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
  622. struct cfg80211_scan_request *req)
  623. {
  624. int res;
  625. mutex_lock(&sdata->local->mtx);
  626. res = __ieee80211_start_scan(sdata, req);
  627. mutex_unlock(&sdata->local->mtx);
  628. return res;
  629. }
  630. int ieee80211_request_internal_scan(struct ieee80211_sub_if_data *sdata,
  631. const u8 *ssid, u8 ssid_len,
  632. struct ieee80211_channel *chan)
  633. {
  634. struct ieee80211_local *local = sdata->local;
  635. int ret = -EBUSY;
  636. enum ieee80211_band band;
  637. mutex_lock(&local->mtx);
  638. /* busy scanning */
  639. if (local->scan_req)
  640. goto unlock;
  641. /* fill internal scan request */
  642. if (!chan) {
  643. int i, nchan = 0;
  644. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  645. if (!local->hw.wiphy->bands[band])
  646. continue;
  647. for (i = 0;
  648. i < local->hw.wiphy->bands[band]->n_channels;
  649. i++) {
  650. local->int_scan_req->channels[nchan] =
  651. &local->hw.wiphy->bands[band]->channels[i];
  652. nchan++;
  653. }
  654. }
  655. local->int_scan_req->n_channels = nchan;
  656. } else {
  657. local->int_scan_req->channels[0] = chan;
  658. local->int_scan_req->n_channels = 1;
  659. }
  660. local->int_scan_req->ssids = &local->scan_ssid;
  661. local->int_scan_req->n_ssids = 1;
  662. memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
  663. local->int_scan_req->ssids[0].ssid_len = ssid_len;
  664. ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
  665. unlock:
  666. mutex_unlock(&local->mtx);
  667. return ret;
  668. }
  669. /*
  670. * Only call this function when a scan can't be queued -- under RTNL.
  671. */
  672. void ieee80211_scan_cancel(struct ieee80211_local *local)
  673. {
  674. /*
  675. * We are canceling software scan, or deferred scan that was not
  676. * yet really started (see __ieee80211_start_scan ).
  677. *
  678. * Regarding hardware scan:
  679. * - we can not call __ieee80211_scan_completed() as when
  680. * SCAN_HW_SCANNING bit is set this function change
  681. * local->hw_scan_req to operate on 5G band, what race with
  682. * driver which can use local->hw_scan_req
  683. *
  684. * - we can not cancel scan_work since driver can schedule it
  685. * by ieee80211_scan_completed(..., true) to finish scan
  686. *
  687. * Hence we only call the cancel_hw_scan() callback, but the low-level
  688. * driver is still responsible for calling ieee80211_scan_completed()
  689. * after the scan was completed/aborted.
  690. */
  691. mutex_lock(&local->mtx);
  692. if (!local->scan_req)
  693. goto out;
  694. if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
  695. if (local->ops->cancel_hw_scan)
  696. drv_cancel_hw_scan(local, local->scan_sdata);
  697. goto out;
  698. }
  699. /*
  700. * If the work is currently running, it must be blocked on
  701. * the mutex, but we'll set scan_sdata = NULL and it'll
  702. * simply exit once it acquires the mutex.
  703. */
  704. cancel_delayed_work(&local->scan_work);
  705. /* and clean up */
  706. __ieee80211_scan_completed(&local->hw, true, false);
  707. out:
  708. mutex_unlock(&local->mtx);
  709. }
  710. int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
  711. struct cfg80211_sched_scan_request *req)
  712. {
  713. struct ieee80211_local *local = sdata->local;
  714. int ret, i;
  715. mutex_lock(&sdata->local->mtx);
  716. if (local->sched_scanning) {
  717. ret = -EBUSY;
  718. goto out;
  719. }
  720. if (!local->ops->sched_scan_start) {
  721. ret = -ENOTSUPP;
  722. goto out;
  723. }
  724. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  725. local->sched_scan_ies.ie[i] = kzalloc(2 +
  726. IEEE80211_MAX_SSID_LEN +
  727. local->scan_ies_len +
  728. req->ie_len,
  729. GFP_KERNEL);
  730. if (!local->sched_scan_ies.ie[i]) {
  731. ret = -ENOMEM;
  732. goto out_free;
  733. }
  734. local->sched_scan_ies.len[i] =
  735. ieee80211_build_preq_ies(local,
  736. local->sched_scan_ies.ie[i],
  737. req->ie, req->ie_len, i,
  738. (u32) -1, 0);
  739. }
  740. ret = drv_sched_scan_start(local, sdata, req,
  741. &local->sched_scan_ies);
  742. if (ret == 0) {
  743. local->sched_scanning = true;
  744. goto out;
  745. }
  746. out_free:
  747. while (i > 0)
  748. kfree(local->sched_scan_ies.ie[--i]);
  749. out:
  750. mutex_unlock(&sdata->local->mtx);
  751. return ret;
  752. }
  753. int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata)
  754. {
  755. struct ieee80211_local *local = sdata->local;
  756. int ret = 0, i;
  757. mutex_lock(&sdata->local->mtx);
  758. if (!local->ops->sched_scan_stop) {
  759. ret = -ENOTSUPP;
  760. goto out;
  761. }
  762. if (local->sched_scanning) {
  763. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  764. kfree(local->sched_scan_ies.ie[i]);
  765. drv_sched_scan_stop(local, sdata);
  766. local->sched_scanning = false;
  767. }
  768. out:
  769. mutex_unlock(&sdata->local->mtx);
  770. return ret;
  771. }
  772. void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
  773. {
  774. struct ieee80211_local *local = hw_to_local(hw);
  775. trace_api_sched_scan_results(local);
  776. cfg80211_sched_scan_results(hw->wiphy);
  777. }
  778. EXPORT_SYMBOL(ieee80211_sched_scan_results);
  779. void ieee80211_sched_scan_stopped_work(struct work_struct *work)
  780. {
  781. struct ieee80211_local *local =
  782. container_of(work, struct ieee80211_local,
  783. sched_scan_stopped_work);
  784. int i;
  785. mutex_lock(&local->mtx);
  786. if (!local->sched_scanning) {
  787. mutex_unlock(&local->mtx);
  788. return;
  789. }
  790. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  791. kfree(local->sched_scan_ies.ie[i]);
  792. local->sched_scanning = false;
  793. mutex_unlock(&local->mtx);
  794. cfg80211_sched_scan_stopped(local->hw.wiphy);
  795. }
  796. void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
  797. {
  798. struct ieee80211_local *local = hw_to_local(hw);
  799. trace_api_sched_scan_stopped(local);
  800. ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work);
  801. }
  802. EXPORT_SYMBOL(ieee80211_sched_scan_stopped);