scan.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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. /* TODO: figure out how to avoid that the "current BSS" expires */
  15. #include <linux/wireless.h>
  16. #include <linux/if_arp.h>
  17. #include <linux/rtnetlink.h>
  18. #include <net/mac80211.h>
  19. #include <net/iw_handler.h>
  20. #include "ieee80211_i.h"
  21. #include "mesh.h"
  22. #define IEEE80211_PROBE_DELAY (HZ / 33)
  23. #define IEEE80211_CHANNEL_TIME (HZ / 33)
  24. #define IEEE80211_PASSIVE_CHANNEL_TIME (HZ / 5)
  25. struct ieee80211_bss *
  26. ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
  27. u8 *ssid, u8 ssid_len)
  28. {
  29. return (void *)cfg80211_get_bss(local->hw.wiphy,
  30. ieee80211_get_channel(local->hw.wiphy,
  31. freq),
  32. bssid, ssid, ssid_len,
  33. 0, 0);
  34. }
  35. static void ieee80211_rx_bss_free(struct cfg80211_bss *cbss)
  36. {
  37. struct ieee80211_bss *bss = (void *)cbss;
  38. kfree(bss_mesh_id(bss));
  39. kfree(bss_mesh_cfg(bss));
  40. }
  41. void ieee80211_rx_bss_put(struct ieee80211_local *local,
  42. struct ieee80211_bss *bss)
  43. {
  44. cfg80211_put_bss((struct cfg80211_bss *)bss);
  45. }
  46. struct ieee80211_bss *
  47. ieee80211_bss_info_update(struct ieee80211_local *local,
  48. struct ieee80211_rx_status *rx_status,
  49. struct ieee80211_mgmt *mgmt,
  50. size_t len,
  51. struct ieee802_11_elems *elems,
  52. struct ieee80211_channel *channel,
  53. bool beacon)
  54. {
  55. struct ieee80211_bss *bss;
  56. int clen;
  57. s32 signal = 0;
  58. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM)
  59. signal = rx_status->signal * 100;
  60. else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC)
  61. signal = (rx_status->signal * 100) / local->hw.max_signal;
  62. bss = (void *)cfg80211_inform_bss_frame(local->hw.wiphy, channel,
  63. mgmt, len, signal, GFP_ATOMIC);
  64. if (!bss)
  65. return NULL;
  66. bss->cbss.free_priv = ieee80211_rx_bss_free;
  67. /* save the ERP value so that it is available at association time */
  68. if (elems->erp_info && elems->erp_info_len >= 1) {
  69. bss->erp_value = elems->erp_info[0];
  70. bss->has_erp_value = 1;
  71. }
  72. if (elems->tim) {
  73. struct ieee80211_tim_ie *tim_ie =
  74. (struct ieee80211_tim_ie *)elems->tim;
  75. bss->dtim_period = tim_ie->dtim_period;
  76. }
  77. /* set default value for buggy APs */
  78. if (!elems->tim || bss->dtim_period == 0)
  79. bss->dtim_period = 1;
  80. bss->supp_rates_len = 0;
  81. if (elems->supp_rates) {
  82. clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
  83. if (clen > elems->supp_rates_len)
  84. clen = elems->supp_rates_len;
  85. memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
  86. clen);
  87. bss->supp_rates_len += clen;
  88. }
  89. if (elems->ext_supp_rates) {
  90. clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
  91. if (clen > elems->ext_supp_rates_len)
  92. clen = elems->ext_supp_rates_len;
  93. memcpy(&bss->supp_rates[bss->supp_rates_len],
  94. elems->ext_supp_rates, clen);
  95. bss->supp_rates_len += clen;
  96. }
  97. bss->wmm_used = elems->wmm_param || elems->wmm_info;
  98. if (!beacon)
  99. bss->last_probe_resp = jiffies;
  100. return bss;
  101. }
  102. void ieee80211_rx_bss_remove(struct ieee80211_sub_if_data *sdata, u8 *bssid,
  103. int freq, u8 *ssid, u8 ssid_len)
  104. {
  105. struct ieee80211_bss *bss;
  106. struct ieee80211_local *local = sdata->local;
  107. bss = ieee80211_rx_bss_get(local, bssid, freq, ssid, ssid_len);
  108. if (bss) {
  109. cfg80211_unlink_bss(local->hw.wiphy, (void *)bss);
  110. ieee80211_rx_bss_put(local, bss);
  111. }
  112. }
  113. ieee80211_rx_result
  114. ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
  115. struct ieee80211_rx_status *rx_status)
  116. {
  117. struct ieee80211_mgmt *mgmt;
  118. struct ieee80211_bss *bss;
  119. u8 *elements;
  120. struct ieee80211_channel *channel;
  121. size_t baselen;
  122. int freq;
  123. __le16 fc;
  124. bool presp, beacon = false;
  125. struct ieee802_11_elems elems;
  126. if (skb->len < 2)
  127. return RX_DROP_UNUSABLE;
  128. mgmt = (struct ieee80211_mgmt *) skb->data;
  129. fc = mgmt->frame_control;
  130. if (ieee80211_is_ctl(fc))
  131. return RX_CONTINUE;
  132. if (skb->len < 24)
  133. return RX_DROP_MONITOR;
  134. presp = ieee80211_is_probe_resp(fc);
  135. if (presp) {
  136. /* ignore ProbeResp to foreign address */
  137. if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
  138. return RX_DROP_MONITOR;
  139. presp = true;
  140. elements = mgmt->u.probe_resp.variable;
  141. baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  142. } else {
  143. beacon = ieee80211_is_beacon(fc);
  144. baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
  145. elements = mgmt->u.beacon.variable;
  146. }
  147. if (!presp && !beacon)
  148. return RX_CONTINUE;
  149. if (baselen > skb->len)
  150. return RX_DROP_MONITOR;
  151. ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
  152. if (elems.ds_params && elems.ds_params_len == 1)
  153. freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
  154. else
  155. freq = rx_status->freq;
  156. channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
  157. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  158. return RX_DROP_MONITOR;
  159. bss = ieee80211_bss_info_update(sdata->local, rx_status,
  160. mgmt, skb->len, &elems,
  161. channel, beacon);
  162. if (bss)
  163. ieee80211_rx_bss_put(sdata->local, bss);
  164. dev_kfree_skb(skb);
  165. return RX_QUEUED;
  166. }
  167. void ieee80211_scan_failed(struct ieee80211_local *local)
  168. {
  169. if (WARN_ON(!local->scan_req))
  170. return;
  171. /* notify cfg80211 about the failed scan */
  172. if (local->scan_req != &local->int_scan_req)
  173. cfg80211_scan_done(local->scan_req, true);
  174. local->scan_req = NULL;
  175. }
  176. /*
  177. * inform AP that we will go to sleep so that it will buffer the frames
  178. * while we scan
  179. */
  180. static void ieee80211_scan_ps_enable(struct ieee80211_sub_if_data *sdata)
  181. {
  182. struct ieee80211_local *local = sdata->local;
  183. bool ps = false;
  184. /* FIXME: what to do when local->pspolling is true? */
  185. del_timer_sync(&local->dynamic_ps_timer);
  186. cancel_work_sync(&local->dynamic_ps_enable_work);
  187. if (local->hw.conf.flags & IEEE80211_CONF_PS) {
  188. ps = true;
  189. local->hw.conf.flags &= ~IEEE80211_CONF_PS;
  190. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  191. }
  192. if (!ps || !(local->hw.flags & IEEE80211_HW_PS_NULLFUNC_STACK))
  193. /*
  194. * If power save was enabled, no need to send a nullfunc
  195. * frame because AP knows that we are sleeping. But if the
  196. * hardware is creating the nullfunc frame for power save
  197. * status (ie. IEEE80211_HW_PS_NULLFUNC_STACK is not
  198. * enabled) and power save was enabled, the firmware just
  199. * sent a null frame with power save disabled. So we need
  200. * to send a new nullfunc frame to inform the AP that we
  201. * are again sleeping.
  202. */
  203. ieee80211_send_nullfunc(local, sdata, 1);
  204. }
  205. /* inform AP that we are awake again, unless power save is enabled */
  206. static void ieee80211_scan_ps_disable(struct ieee80211_sub_if_data *sdata)
  207. {
  208. struct ieee80211_local *local = sdata->local;
  209. if (!local->ps_sdata)
  210. ieee80211_send_nullfunc(local, sdata, 0);
  211. else {
  212. /*
  213. * In !IEEE80211_HW_PS_NULLFUNC_STACK case the hardware
  214. * will send a nullfunc frame with the powersave bit set
  215. * even though the AP already knows that we are sleeping.
  216. * This could be avoided by sending a null frame with power
  217. * save bit disabled before enabling the power save, but
  218. * this doesn't gain anything.
  219. *
  220. * When IEEE80211_HW_PS_NULLFUNC_STACK is enabled, no need
  221. * to send a nullfunc frame because AP already knows that
  222. * we are sleeping, let's just enable power save mode in
  223. * hardware.
  224. */
  225. local->hw.conf.flags |= IEEE80211_CONF_PS;
  226. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_PS);
  227. }
  228. }
  229. void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
  230. {
  231. struct ieee80211_local *local = hw_to_local(hw);
  232. struct ieee80211_sub_if_data *sdata;
  233. if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
  234. return;
  235. if (WARN_ON(!local->scan_req))
  236. return;
  237. if (local->hw_scanning) {
  238. kfree(local->scan_req->ie);
  239. local->scan_req->ie = local->orig_ies;
  240. local->scan_req->ie_len = local->orig_ies_len;
  241. }
  242. if (local->scan_req != &local->int_scan_req)
  243. cfg80211_scan_done(local->scan_req, aborted);
  244. local->scan_req = NULL;
  245. if (local->hw_scanning) {
  246. local->hw_scanning = false;
  247. /*
  248. * Somebody might have requested channel change during scan
  249. * that we won't have acted upon, try now. ieee80211_hw_config
  250. * will set the flag based on actual changes.
  251. */
  252. ieee80211_hw_config(local, 0);
  253. goto done;
  254. }
  255. local->sw_scanning = false;
  256. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  257. netif_tx_lock_bh(local->mdev);
  258. netif_addr_lock(local->mdev);
  259. local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
  260. local->ops->configure_filter(local_to_hw(local),
  261. FIF_BCN_PRBRESP_PROMISC,
  262. &local->filter_flags,
  263. local->mdev->mc_count,
  264. local->mdev->mc_list);
  265. netif_addr_unlock(local->mdev);
  266. netif_tx_unlock_bh(local->mdev);
  267. if (local->ops->sw_scan_complete)
  268. local->ops->sw_scan_complete(local_to_hw(local));
  269. mutex_lock(&local->iflist_mtx);
  270. list_for_each_entry(sdata, &local->interfaces, list) {
  271. if (!netif_running(sdata->dev))
  272. continue;
  273. /* Tell AP we're back */
  274. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  275. if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED) {
  276. ieee80211_scan_ps_disable(sdata);
  277. netif_tx_wake_all_queues(sdata->dev);
  278. }
  279. } else
  280. netif_tx_wake_all_queues(sdata->dev);
  281. /* re-enable beaconing */
  282. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  283. sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  284. sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  285. ieee80211_if_config(sdata,
  286. IEEE80211_IFCC_BEACON_ENABLED);
  287. }
  288. mutex_unlock(&local->iflist_mtx);
  289. done:
  290. ieee80211_mlme_notify_scan_completed(local);
  291. ieee80211_ibss_notify_scan_completed(local);
  292. ieee80211_mesh_notify_scan_completed(local);
  293. }
  294. EXPORT_SYMBOL(ieee80211_scan_completed);
  295. void ieee80211_scan_work(struct work_struct *work)
  296. {
  297. struct ieee80211_local *local =
  298. container_of(work, struct ieee80211_local, scan_work.work);
  299. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  300. struct ieee80211_channel *chan;
  301. int skip, i;
  302. unsigned long next_delay = 0;
  303. /*
  304. * Avoid re-scheduling when the sdata is going away.
  305. */
  306. if (!netif_running(sdata->dev))
  307. return;
  308. switch (local->scan_state) {
  309. case SCAN_SET_CHANNEL:
  310. /* if no more bands/channels left, complete scan */
  311. if (local->scan_channel_idx >= local->scan_req->n_channels) {
  312. ieee80211_scan_completed(local_to_hw(local), false);
  313. return;
  314. }
  315. skip = 0;
  316. chan = local->scan_req->channels[local->scan_channel_idx];
  317. if (chan->flags & IEEE80211_CHAN_DISABLED ||
  318. (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  319. chan->flags & IEEE80211_CHAN_NO_IBSS))
  320. skip = 1;
  321. if (!skip) {
  322. local->scan_channel = chan;
  323. if (ieee80211_hw_config(local,
  324. IEEE80211_CONF_CHANGE_CHANNEL))
  325. skip = 1;
  326. }
  327. /* advance state machine to next channel/band */
  328. local->scan_channel_idx++;
  329. if (skip)
  330. break;
  331. next_delay = IEEE80211_PROBE_DELAY +
  332. usecs_to_jiffies(local->hw.channel_change_time);
  333. local->scan_state = SCAN_SEND_PROBE;
  334. break;
  335. case SCAN_SEND_PROBE:
  336. next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  337. local->scan_state = SCAN_SET_CHANNEL;
  338. if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  339. !local->scan_req->n_ssids)
  340. break;
  341. for (i = 0; i < local->scan_req->n_ssids; i++)
  342. ieee80211_send_probe_req(
  343. sdata, NULL,
  344. local->scan_req->ssids[i].ssid,
  345. local->scan_req->ssids[i].ssid_len,
  346. local->scan_req->ie, local->scan_req->ie_len);
  347. next_delay = IEEE80211_CHANNEL_TIME;
  348. break;
  349. }
  350. queue_delayed_work(local->hw.workqueue, &local->scan_work,
  351. next_delay);
  352. }
  353. int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
  354. struct cfg80211_scan_request *req)
  355. {
  356. struct ieee80211_local *local = scan_sdata->local;
  357. struct ieee80211_sub_if_data *sdata;
  358. if (!req)
  359. return -EINVAL;
  360. if (local->scan_req && local->scan_req != req)
  361. return -EBUSY;
  362. local->scan_req = req;
  363. /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
  364. * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
  365. * BSSID: MACAddress
  366. * SSID
  367. * ScanType: ACTIVE, PASSIVE
  368. * ProbeDelay: delay (in microseconds) to be used prior to transmitting
  369. * a Probe frame during active scanning
  370. * ChannelList
  371. * MinChannelTime (>= ProbeDelay), in TU
  372. * MaxChannelTime: (>= MinChannelTime), in TU
  373. */
  374. /* MLME-SCAN.confirm
  375. * BSSDescriptionSet
  376. * ResultCode: SUCCESS, INVALID_PARAMETERS
  377. */
  378. if (local->sw_scanning || local->hw_scanning) {
  379. if (local->scan_sdata == scan_sdata)
  380. return 0;
  381. return -EBUSY;
  382. }
  383. if (local->ops->hw_scan) {
  384. u8 *ies;
  385. int rc, ielen;
  386. ies = kmalloc(2 + IEEE80211_MAX_SSID_LEN +
  387. local->scan_ies_len + req->ie_len, GFP_KERNEL);
  388. if (!ies)
  389. return -ENOMEM;
  390. ielen = ieee80211_build_preq_ies(local, ies,
  391. req->ie, req->ie_len);
  392. local->orig_ies = req->ie;
  393. local->orig_ies_len = req->ie_len;
  394. req->ie = ies;
  395. req->ie_len = ielen;
  396. local->hw_scanning = true;
  397. rc = local->ops->hw_scan(local_to_hw(local), req);
  398. if (rc) {
  399. local->hw_scanning = false;
  400. kfree(ies);
  401. req->ie_len = local->orig_ies_len;
  402. req->ie = local->orig_ies;
  403. return rc;
  404. }
  405. local->scan_sdata = scan_sdata;
  406. return 0;
  407. }
  408. /*
  409. * Hardware/driver doesn't support hw_scan, so use software
  410. * scanning instead. First send a nullfunc frame with power save
  411. * bit on so that AP will buffer the frames for us while we are not
  412. * listening, then send probe requests to each channel and wait for
  413. * the responses. After all channels are scanned, tune back to the
  414. * original channel and send a nullfunc frame with power save bit
  415. * off to trigger the AP to send us all the buffered frames.
  416. *
  417. * Note that while local->sw_scanning is true everything else but
  418. * nullfunc frames and probe requests will be dropped in
  419. * ieee80211_tx_h_check_assoc().
  420. */
  421. local->sw_scanning = true;
  422. if (local->ops->sw_scan_start)
  423. local->ops->sw_scan_start(local_to_hw(local));
  424. mutex_lock(&local->iflist_mtx);
  425. list_for_each_entry(sdata, &local->interfaces, list) {
  426. if (!netif_running(sdata->dev))
  427. continue;
  428. /* disable beaconing */
  429. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  430. sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  431. sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  432. ieee80211_if_config(sdata,
  433. IEEE80211_IFCC_BEACON_ENABLED);
  434. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  435. if (sdata->u.mgd.flags & IEEE80211_STA_ASSOCIATED) {
  436. netif_tx_stop_all_queues(sdata->dev);
  437. ieee80211_scan_ps_enable(sdata);
  438. }
  439. } else
  440. netif_tx_stop_all_queues(sdata->dev);
  441. }
  442. mutex_unlock(&local->iflist_mtx);
  443. local->scan_state = SCAN_SET_CHANNEL;
  444. local->scan_channel_idx = 0;
  445. local->scan_sdata = scan_sdata;
  446. local->scan_req = req;
  447. netif_addr_lock_bh(local->mdev);
  448. local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
  449. local->ops->configure_filter(local_to_hw(local),
  450. FIF_BCN_PRBRESP_PROMISC,
  451. &local->filter_flags,
  452. local->mdev->mc_count,
  453. local->mdev->mc_list);
  454. netif_addr_unlock_bh(local->mdev);
  455. /* TODO: start scan as soon as all nullfunc frames are ACKed */
  456. queue_delayed_work(local->hw.workqueue, &local->scan_work,
  457. IEEE80211_CHANNEL_TIME);
  458. return 0;
  459. }
  460. int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
  461. struct cfg80211_scan_request *req)
  462. {
  463. struct ieee80211_local *local = sdata->local;
  464. struct ieee80211_if_managed *ifmgd;
  465. if (!req)
  466. return -EINVAL;
  467. if (local->scan_req && local->scan_req != req)
  468. return -EBUSY;
  469. local->scan_req = req;
  470. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  471. return ieee80211_start_scan(sdata, req);
  472. /*
  473. * STA has a state machine that might need to defer scanning
  474. * while it's trying to associate/authenticate, therefore we
  475. * queue it up to the state machine in that case.
  476. */
  477. if (local->sw_scanning || local->hw_scanning) {
  478. if (local->scan_sdata == sdata)
  479. return 0;
  480. return -EBUSY;
  481. }
  482. ifmgd = &sdata->u.mgd;
  483. set_bit(IEEE80211_STA_REQ_SCAN, &ifmgd->request);
  484. queue_work(local->hw.workqueue, &ifmgd->work);
  485. return 0;
  486. }