scan.c 24 KB

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