scan.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003
  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. if (test_bit(SCAN_HW_CANCELLED, &local->scanning))
  221. return false;
  222. do {
  223. if (local->hw_scan_band == IEEE80211_NUM_BANDS)
  224. return false;
  225. band = local->hw_scan_band;
  226. n_chans = 0;
  227. for (i = 0; i < req->n_channels; i++) {
  228. if (req->channels[i]->band == band) {
  229. local->hw_scan_req->channels[n_chans] =
  230. req->channels[i];
  231. n_chans++;
  232. }
  233. }
  234. local->hw_scan_band++;
  235. } while (!n_chans);
  236. local->hw_scan_req->n_channels = n_chans;
  237. ielen = ieee80211_build_preq_ies(local, (u8 *)local->hw_scan_req->ie,
  238. req->ie, req->ie_len, band,
  239. req->rates[band], 0);
  240. local->hw_scan_req->ie_len = ielen;
  241. local->hw_scan_req->no_cck = req->no_cck;
  242. return true;
  243. }
  244. static void __ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted,
  245. bool was_hw_scan)
  246. {
  247. struct ieee80211_local *local = hw_to_local(hw);
  248. lockdep_assert_held(&local->mtx);
  249. /*
  250. * It's ok to abort a not-yet-running scan (that
  251. * we have one at all will be verified by checking
  252. * local->scan_req next), but not to complete it
  253. * successfully.
  254. */
  255. if (WARN_ON(!local->scanning && !aborted))
  256. aborted = true;
  257. if (WARN_ON(!local->scan_req))
  258. return;
  259. if (was_hw_scan && !aborted && ieee80211_prep_hw_scan(local)) {
  260. int rc = drv_hw_scan(local, local->scan_sdata, local->hw_scan_req);
  261. if (rc == 0)
  262. return;
  263. }
  264. kfree(local->hw_scan_req);
  265. local->hw_scan_req = NULL;
  266. if (local->scan_req != local->int_scan_req)
  267. cfg80211_scan_done(local->scan_req, aborted);
  268. local->scan_req = NULL;
  269. local->scan_sdata = NULL;
  270. local->scanning = 0;
  271. local->scan_channel = NULL;
  272. /* Set power back to normal operating levels. */
  273. ieee80211_hw_config(local, 0);
  274. if (!was_hw_scan) {
  275. ieee80211_configure_filter(local);
  276. drv_sw_scan_complete(local);
  277. ieee80211_offchannel_return(local);
  278. }
  279. ieee80211_recalc_idle(local);
  280. ieee80211_mlme_notify_scan_completed(local);
  281. ieee80211_ibss_notify_scan_completed(local);
  282. ieee80211_mesh_notify_scan_completed(local);
  283. ieee80211_queue_work(&local->hw, &local->work_work);
  284. }
  285. void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
  286. {
  287. struct ieee80211_local *local = hw_to_local(hw);
  288. trace_api_scan_completed(local, aborted);
  289. set_bit(SCAN_COMPLETED, &local->scanning);
  290. if (aborted)
  291. set_bit(SCAN_ABORTED, &local->scanning);
  292. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, 0);
  293. }
  294. EXPORT_SYMBOL(ieee80211_scan_completed);
  295. static int ieee80211_start_sw_scan(struct ieee80211_local *local)
  296. {
  297. /*
  298. * Hardware/driver doesn't support hw_scan, so use software
  299. * scanning instead. First send a nullfunc frame with power save
  300. * bit on so that AP will buffer the frames for us while we are not
  301. * listening, then send probe requests to each channel and wait for
  302. * the responses. After all channels are scanned, tune back to the
  303. * original channel and send a nullfunc frame with power save bit
  304. * off to trigger the AP to send us all the buffered frames.
  305. *
  306. * Note that while local->sw_scanning is true everything else but
  307. * nullfunc frames and probe requests will be dropped in
  308. * ieee80211_tx_h_check_assoc().
  309. */
  310. drv_sw_scan_start(local);
  311. local->leave_oper_channel_time = jiffies;
  312. local->next_scan_state = SCAN_DECISION;
  313. local->scan_channel_idx = 0;
  314. ieee80211_offchannel_stop_vifs(local);
  315. ieee80211_configure_filter(local);
  316. /* We need to set power level at maximum rate for scanning. */
  317. ieee80211_hw_config(local, 0);
  318. ieee80211_queue_delayed_work(&local->hw,
  319. &local->scan_work, 0);
  320. return 0;
  321. }
  322. static int __ieee80211_start_scan(struct ieee80211_sub_if_data *sdata,
  323. struct cfg80211_scan_request *req)
  324. {
  325. struct ieee80211_local *local = sdata->local;
  326. int rc;
  327. lockdep_assert_held(&local->mtx);
  328. if (local->scan_req)
  329. return -EBUSY;
  330. if (!list_empty(&local->work_list)) {
  331. /* wait for the work to finish/time out */
  332. local->scan_req = req;
  333. local->scan_sdata = sdata;
  334. return 0;
  335. }
  336. if (local->ops->hw_scan) {
  337. u8 *ies;
  338. local->hw_scan_req = kmalloc(
  339. sizeof(*local->hw_scan_req) +
  340. req->n_channels * sizeof(req->channels[0]) +
  341. 2 + IEEE80211_MAX_SSID_LEN + local->scan_ies_len +
  342. req->ie_len, GFP_KERNEL);
  343. if (!local->hw_scan_req)
  344. return -ENOMEM;
  345. local->hw_scan_req->ssids = req->ssids;
  346. local->hw_scan_req->n_ssids = req->n_ssids;
  347. ies = (u8 *)local->hw_scan_req +
  348. sizeof(*local->hw_scan_req) +
  349. req->n_channels * sizeof(req->channels[0]);
  350. local->hw_scan_req->ie = ies;
  351. local->hw_scan_band = 0;
  352. /*
  353. * After allocating local->hw_scan_req, we must
  354. * go through until ieee80211_prep_hw_scan(), so
  355. * anything that might be changed here and leave
  356. * this function early must not go after this
  357. * allocation.
  358. */
  359. }
  360. local->scan_req = req;
  361. local->scan_sdata = sdata;
  362. if (local->ops->hw_scan)
  363. __set_bit(SCAN_HW_SCANNING, &local->scanning);
  364. else
  365. __set_bit(SCAN_SW_SCANNING, &local->scanning);
  366. ieee80211_recalc_idle(local);
  367. if (local->ops->hw_scan) {
  368. WARN_ON(!ieee80211_prep_hw_scan(local));
  369. rc = drv_hw_scan(local, sdata, local->hw_scan_req);
  370. } else
  371. rc = ieee80211_start_sw_scan(local);
  372. if (rc) {
  373. kfree(local->hw_scan_req);
  374. local->hw_scan_req = NULL;
  375. local->scanning = 0;
  376. ieee80211_recalc_idle(local);
  377. local->scan_req = NULL;
  378. local->scan_sdata = NULL;
  379. }
  380. return rc;
  381. }
  382. static unsigned long
  383. ieee80211_scan_get_channel_time(struct ieee80211_channel *chan)
  384. {
  385. /*
  386. * TODO: channel switching also consumes quite some time,
  387. * add that delay as well to get a better estimation
  388. */
  389. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  390. return IEEE80211_PASSIVE_CHANNEL_TIME;
  391. return IEEE80211_PROBE_DELAY + IEEE80211_CHANNEL_TIME;
  392. }
  393. static void ieee80211_scan_state_decision(struct ieee80211_local *local,
  394. unsigned long *next_delay)
  395. {
  396. bool associated = false;
  397. bool tx_empty = true;
  398. bool bad_latency;
  399. bool listen_int_exceeded;
  400. unsigned long min_beacon_int = 0;
  401. struct ieee80211_sub_if_data *sdata;
  402. struct ieee80211_channel *next_chan;
  403. /*
  404. * check if at least one STA interface is associated,
  405. * check if at least one STA interface has pending tx frames
  406. * and grab the lowest used beacon interval
  407. */
  408. mutex_lock(&local->iflist_mtx);
  409. list_for_each_entry(sdata, &local->interfaces, list) {
  410. if (!ieee80211_sdata_running(sdata))
  411. continue;
  412. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  413. if (sdata->u.mgd.associated) {
  414. associated = true;
  415. if (sdata->vif.bss_conf.beacon_int <
  416. min_beacon_int || min_beacon_int == 0)
  417. min_beacon_int =
  418. sdata->vif.bss_conf.beacon_int;
  419. if (!qdisc_all_tx_empty(sdata->dev)) {
  420. tx_empty = false;
  421. break;
  422. }
  423. }
  424. }
  425. }
  426. mutex_unlock(&local->iflist_mtx);
  427. next_chan = local->scan_req->channels[local->scan_channel_idx];
  428. /*
  429. * we're currently scanning a different channel, let's
  430. * see if we can scan another channel without interfering
  431. * with the current traffic situation.
  432. *
  433. * Since we don't know if the AP has pending frames for us
  434. * we can only check for our tx queues and use the current
  435. * pm_qos requirements for rx. Hence, if no tx traffic occurs
  436. * at all we will scan as many channels in a row as the pm_qos
  437. * latency allows us to. Additionally we also check for the
  438. * currently negotiated listen interval to prevent losing
  439. * frames unnecessarily.
  440. *
  441. * Otherwise switch back to the operating channel.
  442. */
  443. bad_latency = time_after(jiffies +
  444. ieee80211_scan_get_channel_time(next_chan),
  445. local->leave_oper_channel_time +
  446. usecs_to_jiffies(pm_qos_request(PM_QOS_NETWORK_LATENCY)));
  447. listen_int_exceeded = time_after(jiffies +
  448. ieee80211_scan_get_channel_time(next_chan),
  449. local->leave_oper_channel_time +
  450. usecs_to_jiffies(min_beacon_int * 1024) *
  451. local->hw.conf.listen_interval);
  452. if (associated && (!tx_empty || bad_latency || listen_int_exceeded))
  453. local->next_scan_state = SCAN_SUSPEND;
  454. else
  455. local->next_scan_state = SCAN_SET_CHANNEL;
  456. *next_delay = 0;
  457. }
  458. static void ieee80211_scan_state_set_channel(struct ieee80211_local *local,
  459. unsigned long *next_delay)
  460. {
  461. int skip;
  462. struct ieee80211_channel *chan;
  463. skip = 0;
  464. chan = local->scan_req->channels[local->scan_channel_idx];
  465. local->scan_channel = chan;
  466. if (ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL))
  467. skip = 1;
  468. /* advance state machine to next channel/band */
  469. local->scan_channel_idx++;
  470. if (skip) {
  471. /* if we skip this channel return to the decision state */
  472. local->next_scan_state = SCAN_DECISION;
  473. return;
  474. }
  475. /*
  476. * Probe delay is used to update the NAV, cf. 11.1.3.2.2
  477. * (which unfortunately doesn't say _why_ step a) is done,
  478. * but it waits for the probe delay or until a frame is
  479. * received - and the received frame would update the NAV).
  480. * For now, we do not support waiting until a frame is
  481. * received.
  482. *
  483. * In any case, it is not necessary for a passive scan.
  484. */
  485. if (chan->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  486. !local->scan_req->n_ssids) {
  487. *next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  488. local->next_scan_state = SCAN_DECISION;
  489. return;
  490. }
  491. /* active scan, send probes */
  492. *next_delay = IEEE80211_PROBE_DELAY;
  493. local->next_scan_state = SCAN_SEND_PROBE;
  494. }
  495. static void ieee80211_scan_state_send_probe(struct ieee80211_local *local,
  496. unsigned long *next_delay)
  497. {
  498. int i;
  499. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  500. enum ieee80211_band band = local->hw.conf.channel->band;
  501. for (i = 0; i < local->scan_req->n_ssids; i++)
  502. ieee80211_send_probe_req(
  503. sdata, NULL,
  504. local->scan_req->ssids[i].ssid,
  505. local->scan_req->ssids[i].ssid_len,
  506. local->scan_req->ie, local->scan_req->ie_len,
  507. local->scan_req->rates[band], false,
  508. local->scan_req->no_cck);
  509. /*
  510. * After sending probe requests, wait for probe responses
  511. * on the channel.
  512. */
  513. *next_delay = IEEE80211_CHANNEL_TIME;
  514. local->next_scan_state = SCAN_DECISION;
  515. }
  516. static void ieee80211_scan_state_suspend(struct ieee80211_local *local,
  517. unsigned long *next_delay)
  518. {
  519. /* switch back to the operating channel */
  520. local->scan_channel = NULL;
  521. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  522. /* disable PS */
  523. ieee80211_offchannel_return(local);
  524. *next_delay = HZ / 5;
  525. /* afterwards, resume scan & go to next channel */
  526. local->next_scan_state = SCAN_RESUME;
  527. }
  528. static void ieee80211_scan_state_resume(struct ieee80211_local *local,
  529. unsigned long *next_delay)
  530. {
  531. ieee80211_offchannel_stop_vifs(local);
  532. if (local->ops->flush) {
  533. drv_flush(local, false);
  534. *next_delay = 0;
  535. } else
  536. *next_delay = HZ / 10;
  537. /* remember when we left the operating channel */
  538. local->leave_oper_channel_time = jiffies;
  539. /* advance to the next channel to be scanned */
  540. local->next_scan_state = SCAN_SET_CHANNEL;
  541. }
  542. void ieee80211_scan_work(struct work_struct *work)
  543. {
  544. struct ieee80211_local *local =
  545. container_of(work, struct ieee80211_local, scan_work.work);
  546. struct ieee80211_sub_if_data *sdata;
  547. unsigned long next_delay = 0;
  548. bool aborted, hw_scan;
  549. mutex_lock(&local->mtx);
  550. sdata = local->scan_sdata;
  551. if (test_and_clear_bit(SCAN_COMPLETED, &local->scanning)) {
  552. aborted = test_and_clear_bit(SCAN_ABORTED, &local->scanning);
  553. goto out_complete;
  554. }
  555. if (!sdata || !local->scan_req)
  556. goto out;
  557. if (local->scan_req && !local->scanning) {
  558. struct cfg80211_scan_request *req = local->scan_req;
  559. int rc;
  560. local->scan_req = NULL;
  561. local->scan_sdata = NULL;
  562. rc = __ieee80211_start_scan(sdata, req);
  563. if (rc) {
  564. /* need to complete scan in cfg80211 */
  565. local->scan_req = req;
  566. aborted = true;
  567. goto out_complete;
  568. } else
  569. goto out;
  570. }
  571. /*
  572. * Avoid re-scheduling when the sdata is going away.
  573. */
  574. if (!ieee80211_sdata_running(sdata)) {
  575. aborted = true;
  576. goto out_complete;
  577. }
  578. /*
  579. * as long as no delay is required advance immediately
  580. * without scheduling a new work
  581. */
  582. do {
  583. if (!ieee80211_sdata_running(sdata)) {
  584. aborted = true;
  585. goto out_complete;
  586. }
  587. switch (local->next_scan_state) {
  588. case SCAN_DECISION:
  589. /* if no more bands/channels left, complete scan */
  590. if (local->scan_channel_idx >= local->scan_req->n_channels) {
  591. aborted = false;
  592. goto out_complete;
  593. }
  594. ieee80211_scan_state_decision(local, &next_delay);
  595. break;
  596. case SCAN_SET_CHANNEL:
  597. ieee80211_scan_state_set_channel(local, &next_delay);
  598. break;
  599. case SCAN_SEND_PROBE:
  600. ieee80211_scan_state_send_probe(local, &next_delay);
  601. break;
  602. case SCAN_SUSPEND:
  603. ieee80211_scan_state_suspend(local, &next_delay);
  604. break;
  605. case SCAN_RESUME:
  606. ieee80211_scan_state_resume(local, &next_delay);
  607. break;
  608. }
  609. } while (next_delay == 0);
  610. ieee80211_queue_delayed_work(&local->hw, &local->scan_work, next_delay);
  611. goto out;
  612. out_complete:
  613. hw_scan = test_bit(SCAN_HW_SCANNING, &local->scanning);
  614. __ieee80211_scan_completed(&local->hw, aborted, hw_scan);
  615. out:
  616. mutex_unlock(&local->mtx);
  617. }
  618. int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
  619. struct cfg80211_scan_request *req)
  620. {
  621. int res;
  622. mutex_lock(&sdata->local->mtx);
  623. res = __ieee80211_start_scan(sdata, req);
  624. mutex_unlock(&sdata->local->mtx);
  625. return res;
  626. }
  627. int ieee80211_request_ibss_scan(struct ieee80211_sub_if_data *sdata,
  628. const u8 *ssid, u8 ssid_len,
  629. struct ieee80211_channel *chan)
  630. {
  631. struct ieee80211_local *local = sdata->local;
  632. int ret = -EBUSY;
  633. enum ieee80211_band band;
  634. mutex_lock(&local->mtx);
  635. /* busy scanning */
  636. if (local->scan_req)
  637. goto unlock;
  638. /* fill internal scan request */
  639. if (!chan) {
  640. int i, max_n;
  641. int n_ch = 0;
  642. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  643. if (!local->hw.wiphy->bands[band])
  644. continue;
  645. max_n = local->hw.wiphy->bands[band]->n_channels;
  646. for (i = 0; i < max_n; i++) {
  647. struct ieee80211_channel *tmp_ch =
  648. &local->hw.wiphy->bands[band]->channels[i];
  649. if (tmp_ch->flags & (IEEE80211_CHAN_NO_IBSS |
  650. IEEE80211_CHAN_DISABLED))
  651. continue;
  652. local->int_scan_req->channels[n_ch] = tmp_ch;
  653. n_ch++;
  654. }
  655. }
  656. if (WARN_ON_ONCE(n_ch == 0))
  657. goto unlock;
  658. local->int_scan_req->n_channels = n_ch;
  659. } else {
  660. if (WARN_ON_ONCE(chan->flags & (IEEE80211_CHAN_NO_IBSS |
  661. IEEE80211_CHAN_DISABLED)))
  662. goto unlock;
  663. local->int_scan_req->channels[0] = chan;
  664. local->int_scan_req->n_channels = 1;
  665. }
  666. local->int_scan_req->ssids = &local->scan_ssid;
  667. local->int_scan_req->n_ssids = 1;
  668. memcpy(local->int_scan_req->ssids[0].ssid, ssid, IEEE80211_MAX_SSID_LEN);
  669. local->int_scan_req->ssids[0].ssid_len = ssid_len;
  670. ret = __ieee80211_start_scan(sdata, sdata->local->int_scan_req);
  671. unlock:
  672. mutex_unlock(&local->mtx);
  673. return ret;
  674. }
  675. /*
  676. * Only call this function when a scan can't be queued -- under RTNL.
  677. */
  678. void ieee80211_scan_cancel(struct ieee80211_local *local)
  679. {
  680. /*
  681. * We are canceling software scan, or deferred scan that was not
  682. * yet really started (see __ieee80211_start_scan ).
  683. *
  684. * Regarding hardware scan:
  685. * - we can not call __ieee80211_scan_completed() as when
  686. * SCAN_HW_SCANNING bit is set this function change
  687. * local->hw_scan_req to operate on 5G band, what race with
  688. * driver which can use local->hw_scan_req
  689. *
  690. * - we can not cancel scan_work since driver can schedule it
  691. * by ieee80211_scan_completed(..., true) to finish scan
  692. *
  693. * Hence we only call the cancel_hw_scan() callback, but the low-level
  694. * driver is still responsible for calling ieee80211_scan_completed()
  695. * after the scan was completed/aborted.
  696. */
  697. mutex_lock(&local->mtx);
  698. if (!local->scan_req)
  699. goto out;
  700. /*
  701. * We have a scan running and the driver already reported completion,
  702. * but the worker hasn't run yet or is stuck on the mutex - mark it as
  703. * cancelled.
  704. */
  705. if (test_bit(SCAN_HW_SCANNING, &local->scanning) &&
  706. test_bit(SCAN_COMPLETED, &local->scanning)) {
  707. set_bit(SCAN_HW_CANCELLED, &local->scanning);
  708. goto out;
  709. }
  710. if (test_bit(SCAN_HW_SCANNING, &local->scanning)) {
  711. /*
  712. * Make sure that __ieee80211_scan_completed doesn't trigger a
  713. * scan on another band.
  714. */
  715. set_bit(SCAN_HW_CANCELLED, &local->scanning);
  716. if (local->ops->cancel_hw_scan)
  717. drv_cancel_hw_scan(local, local->scan_sdata);
  718. goto out;
  719. }
  720. /*
  721. * If the work is currently running, it must be blocked on
  722. * the mutex, but we'll set scan_sdata = NULL and it'll
  723. * simply exit once it acquires the mutex.
  724. */
  725. cancel_delayed_work(&local->scan_work);
  726. /* and clean up */
  727. __ieee80211_scan_completed(&local->hw, true, false);
  728. out:
  729. mutex_unlock(&local->mtx);
  730. }
  731. int ieee80211_request_sched_scan_start(struct ieee80211_sub_if_data *sdata,
  732. struct cfg80211_sched_scan_request *req)
  733. {
  734. struct ieee80211_local *local = sdata->local;
  735. int ret, i;
  736. mutex_lock(&sdata->local->mtx);
  737. if (local->sched_scanning) {
  738. ret = -EBUSY;
  739. goto out;
  740. }
  741. if (!local->ops->sched_scan_start) {
  742. ret = -ENOTSUPP;
  743. goto out;
  744. }
  745. for (i = 0; i < IEEE80211_NUM_BANDS; i++) {
  746. local->sched_scan_ies.ie[i] = kzalloc(2 +
  747. IEEE80211_MAX_SSID_LEN +
  748. local->scan_ies_len +
  749. req->ie_len,
  750. GFP_KERNEL);
  751. if (!local->sched_scan_ies.ie[i]) {
  752. ret = -ENOMEM;
  753. goto out_free;
  754. }
  755. local->sched_scan_ies.len[i] =
  756. ieee80211_build_preq_ies(local,
  757. local->sched_scan_ies.ie[i],
  758. req->ie, req->ie_len, i,
  759. (u32) -1, 0);
  760. }
  761. ret = drv_sched_scan_start(local, sdata, req,
  762. &local->sched_scan_ies);
  763. if (ret == 0) {
  764. local->sched_scanning = true;
  765. goto out;
  766. }
  767. out_free:
  768. while (i > 0)
  769. kfree(local->sched_scan_ies.ie[--i]);
  770. out:
  771. mutex_unlock(&sdata->local->mtx);
  772. return ret;
  773. }
  774. int ieee80211_request_sched_scan_stop(struct ieee80211_sub_if_data *sdata)
  775. {
  776. struct ieee80211_local *local = sdata->local;
  777. int ret = 0, i;
  778. mutex_lock(&sdata->local->mtx);
  779. if (!local->ops->sched_scan_stop) {
  780. ret = -ENOTSUPP;
  781. goto out;
  782. }
  783. if (local->sched_scanning) {
  784. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  785. kfree(local->sched_scan_ies.ie[i]);
  786. drv_sched_scan_stop(local, sdata);
  787. local->sched_scanning = false;
  788. }
  789. out:
  790. mutex_unlock(&sdata->local->mtx);
  791. return ret;
  792. }
  793. void ieee80211_sched_scan_results(struct ieee80211_hw *hw)
  794. {
  795. struct ieee80211_local *local = hw_to_local(hw);
  796. trace_api_sched_scan_results(local);
  797. cfg80211_sched_scan_results(hw->wiphy);
  798. }
  799. EXPORT_SYMBOL(ieee80211_sched_scan_results);
  800. void ieee80211_sched_scan_stopped_work(struct work_struct *work)
  801. {
  802. struct ieee80211_local *local =
  803. container_of(work, struct ieee80211_local,
  804. sched_scan_stopped_work);
  805. int i;
  806. mutex_lock(&local->mtx);
  807. if (!local->sched_scanning) {
  808. mutex_unlock(&local->mtx);
  809. return;
  810. }
  811. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  812. kfree(local->sched_scan_ies.ie[i]);
  813. local->sched_scanning = false;
  814. mutex_unlock(&local->mtx);
  815. cfg80211_sched_scan_stopped(local->hw.wiphy);
  816. }
  817. void ieee80211_sched_scan_stopped(struct ieee80211_hw *hw)
  818. {
  819. struct ieee80211_local *local = hw_to_local(hw);
  820. trace_api_sched_scan_stopped(local);
  821. ieee80211_queue_work(&local->hw, &local->sched_scan_stopped_work);
  822. }
  823. EXPORT_SYMBOL(ieee80211_sched_scan_stopped);