scan.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  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. enum cfg80211_signal_type sigtype = CFG80211_SIGNAL_TYPE_NONE;
  58. s32 signal = 0;
  59. if (local->hw.flags & IEEE80211_HW_SIGNAL_DBM) {
  60. sigtype = CFG80211_SIGNAL_TYPE_MBM;
  61. signal = rx_status->signal * 100;
  62. } else if (local->hw.flags & IEEE80211_HW_SIGNAL_UNSPEC) {
  63. sigtype = CFG80211_SIGNAL_TYPE_UNSPEC;
  64. signal = (rx_status->signal * 100) / local->hw.max_signal;
  65. }
  66. bss = (void *)cfg80211_inform_bss_frame(local->hw.wiphy, channel,
  67. mgmt, len, signal, sigtype,
  68. GFP_ATOMIC);
  69. if (!bss)
  70. return NULL;
  71. bss->cbss.free_priv = ieee80211_rx_bss_free;
  72. /* save the ERP value so that it is available at association time */
  73. if (elems->erp_info && elems->erp_info_len >= 1) {
  74. bss->erp_value = elems->erp_info[0];
  75. bss->has_erp_value = 1;
  76. }
  77. if (elems->tim) {
  78. struct ieee80211_tim_ie *tim_ie =
  79. (struct ieee80211_tim_ie *)elems->tim;
  80. bss->dtim_period = tim_ie->dtim_period;
  81. }
  82. /* set default value for buggy APs */
  83. if (!elems->tim || bss->dtim_period == 0)
  84. bss->dtim_period = 1;
  85. bss->supp_rates_len = 0;
  86. if (elems->supp_rates) {
  87. clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
  88. if (clen > elems->supp_rates_len)
  89. clen = elems->supp_rates_len;
  90. memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
  91. clen);
  92. bss->supp_rates_len += clen;
  93. }
  94. if (elems->ext_supp_rates) {
  95. clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
  96. if (clen > elems->ext_supp_rates_len)
  97. clen = elems->ext_supp_rates_len;
  98. memcpy(&bss->supp_rates[bss->supp_rates_len],
  99. elems->ext_supp_rates, clen);
  100. bss->supp_rates_len += clen;
  101. }
  102. bss->wmm_used = elems->wmm_param || elems->wmm_info;
  103. if (!beacon)
  104. bss->last_probe_resp = jiffies;
  105. return bss;
  106. }
  107. void ieee80211_rx_bss_remove(struct ieee80211_sub_if_data *sdata, u8 *bssid,
  108. int freq, u8 *ssid, u8 ssid_len)
  109. {
  110. struct ieee80211_bss *bss;
  111. struct ieee80211_local *local = sdata->local;
  112. bss = ieee80211_rx_bss_get(local, bssid, freq, ssid, ssid_len);
  113. if (bss) {
  114. cfg80211_unlink_bss(local->hw.wiphy, (void *)bss);
  115. ieee80211_rx_bss_put(local, bss);
  116. }
  117. }
  118. ieee80211_rx_result
  119. ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
  120. struct ieee80211_rx_status *rx_status)
  121. {
  122. struct ieee80211_mgmt *mgmt;
  123. struct ieee80211_bss *bss;
  124. u8 *elements;
  125. struct ieee80211_channel *channel;
  126. size_t baselen;
  127. int freq;
  128. __le16 fc;
  129. bool presp, beacon = false;
  130. struct ieee802_11_elems elems;
  131. if (skb->len < 2)
  132. return RX_DROP_UNUSABLE;
  133. mgmt = (struct ieee80211_mgmt *) skb->data;
  134. fc = mgmt->frame_control;
  135. if (ieee80211_is_ctl(fc))
  136. return RX_CONTINUE;
  137. if (skb->len < 24)
  138. return RX_DROP_MONITOR;
  139. presp = ieee80211_is_probe_resp(fc);
  140. if (presp) {
  141. /* ignore ProbeResp to foreign address */
  142. if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
  143. return RX_DROP_MONITOR;
  144. presp = true;
  145. elements = mgmt->u.probe_resp.variable;
  146. baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  147. } else {
  148. beacon = ieee80211_is_beacon(fc);
  149. baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
  150. elements = mgmt->u.beacon.variable;
  151. }
  152. if (!presp && !beacon)
  153. return RX_CONTINUE;
  154. if (baselen > skb->len)
  155. return RX_DROP_MONITOR;
  156. ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
  157. if (elems.ds_params && elems.ds_params_len == 1)
  158. freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
  159. else
  160. freq = rx_status->freq;
  161. channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
  162. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  163. return RX_DROP_MONITOR;
  164. bss = ieee80211_bss_info_update(sdata->local, rx_status,
  165. mgmt, skb->len, &elems,
  166. channel, beacon);
  167. if (bss)
  168. ieee80211_rx_bss_put(sdata->local, bss);
  169. dev_kfree_skb(skb);
  170. return RX_QUEUED;
  171. }
  172. void ieee80211_send_nullfunc(struct ieee80211_local *local,
  173. struct ieee80211_sub_if_data *sdata,
  174. int powersave)
  175. {
  176. struct sk_buff *skb;
  177. struct ieee80211_hdr *nullfunc;
  178. __le16 fc;
  179. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
  180. if (!skb) {
  181. printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
  182. "frame\n", sdata->dev->name);
  183. return;
  184. }
  185. skb_reserve(skb, local->hw.extra_tx_headroom);
  186. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
  187. memset(nullfunc, 0, 24);
  188. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  189. IEEE80211_FCTL_TODS);
  190. if (powersave)
  191. fc |= cpu_to_le16(IEEE80211_FCTL_PM);
  192. nullfunc->frame_control = fc;
  193. memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
  194. memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
  195. memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
  196. ieee80211_tx_skb(sdata, skb, 0);
  197. }
  198. void ieee80211_scan_completed(struct ieee80211_hw *hw, bool aborted)
  199. {
  200. struct ieee80211_local *local = hw_to_local(hw);
  201. struct ieee80211_sub_if_data *sdata;
  202. if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
  203. return;
  204. if (WARN_ON(!local->scan_req))
  205. return;
  206. if (local->scan_req != &local->int_scan_req)
  207. cfg80211_scan_done(local->scan_req, aborted);
  208. local->scan_req = NULL;
  209. local->last_scan_completed = jiffies;
  210. if (local->hw_scanning) {
  211. local->hw_scanning = false;
  212. /*
  213. * Somebody might have requested channel change during scan
  214. * that we won't have acted upon, try now. ieee80211_hw_config
  215. * will set the flag based on actual changes.
  216. */
  217. ieee80211_hw_config(local, 0);
  218. goto done;
  219. }
  220. local->sw_scanning = false;
  221. ieee80211_hw_config(local, IEEE80211_CONF_CHANGE_CHANNEL);
  222. netif_tx_lock_bh(local->mdev);
  223. netif_addr_lock(local->mdev);
  224. local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
  225. local->ops->configure_filter(local_to_hw(local),
  226. FIF_BCN_PRBRESP_PROMISC,
  227. &local->filter_flags,
  228. local->mdev->mc_count,
  229. local->mdev->mc_list);
  230. netif_addr_unlock(local->mdev);
  231. netif_tx_unlock_bh(local->mdev);
  232. mutex_lock(&local->iflist_mtx);
  233. list_for_each_entry(sdata, &local->interfaces, list) {
  234. if (!netif_running(sdata->dev))
  235. continue;
  236. /* Tell AP we're back */
  237. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  238. if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
  239. ieee80211_send_nullfunc(local, sdata, 0);
  240. netif_tx_wake_all_queues(sdata->dev);
  241. }
  242. } else
  243. netif_tx_wake_all_queues(sdata->dev);
  244. /* re-enable beaconing */
  245. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  246. sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  247. sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  248. ieee80211_if_config(sdata,
  249. IEEE80211_IFCC_BEACON_ENABLED);
  250. }
  251. mutex_unlock(&local->iflist_mtx);
  252. done:
  253. ieee80211_mlme_notify_scan_completed(local);
  254. ieee80211_mesh_notify_scan_completed(local);
  255. }
  256. EXPORT_SYMBOL(ieee80211_scan_completed);
  257. void ieee80211_scan_work(struct work_struct *work)
  258. {
  259. struct ieee80211_local *local =
  260. container_of(work, struct ieee80211_local, scan_work.work);
  261. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  262. struct ieee80211_channel *chan;
  263. int skip, i;
  264. unsigned long next_delay = 0;
  265. /*
  266. * Avoid re-scheduling when the sdata is going away.
  267. */
  268. if (!netif_running(sdata->dev))
  269. return;
  270. switch (local->scan_state) {
  271. case SCAN_SET_CHANNEL:
  272. /* if no more bands/channels left, complete scan */
  273. if (local->scan_channel_idx >= local->scan_req->n_channels) {
  274. ieee80211_scan_completed(local_to_hw(local), false);
  275. return;
  276. }
  277. skip = 0;
  278. chan = local->scan_req->channels[local->scan_channel_idx];
  279. if (chan->flags & IEEE80211_CHAN_DISABLED ||
  280. (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  281. chan->flags & IEEE80211_CHAN_NO_IBSS))
  282. skip = 1;
  283. if (!skip) {
  284. local->scan_channel = chan;
  285. if (ieee80211_hw_config(local,
  286. IEEE80211_CONF_CHANGE_CHANNEL))
  287. skip = 1;
  288. }
  289. /* advance state machine to next channel/band */
  290. local->scan_channel_idx++;
  291. if (skip)
  292. break;
  293. next_delay = IEEE80211_PROBE_DELAY +
  294. usecs_to_jiffies(local->hw.channel_change_time);
  295. local->scan_state = SCAN_SEND_PROBE;
  296. break;
  297. case SCAN_SEND_PROBE:
  298. next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  299. local->scan_state = SCAN_SET_CHANNEL;
  300. if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN ||
  301. !local->scan_req->n_ssids)
  302. break;
  303. for (i = 0; i < local->scan_req->n_ssids; i++)
  304. ieee80211_send_probe_req(
  305. sdata, NULL,
  306. local->scan_req->ssids[i].ssid,
  307. local->scan_req->ssids[i].ssid_len);
  308. next_delay = IEEE80211_CHANNEL_TIME;
  309. break;
  310. }
  311. queue_delayed_work(local->hw.workqueue, &local->scan_work,
  312. next_delay);
  313. }
  314. int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
  315. struct cfg80211_scan_request *req)
  316. {
  317. struct ieee80211_local *local = scan_sdata->local;
  318. struct ieee80211_sub_if_data *sdata;
  319. if (!req)
  320. return -EINVAL;
  321. if (local->scan_req && local->scan_req != req)
  322. return -EBUSY;
  323. local->scan_req = req;
  324. /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
  325. * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
  326. * BSSID: MACAddress
  327. * SSID
  328. * ScanType: ACTIVE, PASSIVE
  329. * ProbeDelay: delay (in microseconds) to be used prior to transmitting
  330. * a Probe frame during active scanning
  331. * ChannelList
  332. * MinChannelTime (>= ProbeDelay), in TU
  333. * MaxChannelTime: (>= MinChannelTime), in TU
  334. */
  335. /* MLME-SCAN.confirm
  336. * BSSDescriptionSet
  337. * ResultCode: SUCCESS, INVALID_PARAMETERS
  338. */
  339. if (local->sw_scanning || local->hw_scanning) {
  340. if (local->scan_sdata == scan_sdata)
  341. return 0;
  342. return -EBUSY;
  343. }
  344. if (local->ops->hw_scan) {
  345. int rc;
  346. local->hw_scanning = true;
  347. rc = local->ops->hw_scan(local_to_hw(local), req);
  348. if (rc) {
  349. local->hw_scanning = false;
  350. return rc;
  351. }
  352. local->scan_sdata = scan_sdata;
  353. return 0;
  354. }
  355. local->sw_scanning = true;
  356. mutex_lock(&local->iflist_mtx);
  357. list_for_each_entry(sdata, &local->interfaces, list) {
  358. if (!netif_running(sdata->dev))
  359. continue;
  360. /* disable beaconing */
  361. if (sdata->vif.type == NL80211_IFTYPE_AP ||
  362. sdata->vif.type == NL80211_IFTYPE_ADHOC ||
  363. sdata->vif.type == NL80211_IFTYPE_MESH_POINT)
  364. ieee80211_if_config(sdata,
  365. IEEE80211_IFCC_BEACON_ENABLED);
  366. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  367. if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
  368. netif_tx_stop_all_queues(sdata->dev);
  369. ieee80211_send_nullfunc(local, sdata, 1);
  370. }
  371. } else
  372. netif_tx_stop_all_queues(sdata->dev);
  373. }
  374. mutex_unlock(&local->iflist_mtx);
  375. local->scan_state = SCAN_SET_CHANNEL;
  376. local->scan_channel_idx = 0;
  377. local->scan_sdata = scan_sdata;
  378. local->scan_req = req;
  379. netif_addr_lock_bh(local->mdev);
  380. local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
  381. local->ops->configure_filter(local_to_hw(local),
  382. FIF_BCN_PRBRESP_PROMISC,
  383. &local->filter_flags,
  384. local->mdev->mc_count,
  385. local->mdev->mc_list);
  386. netif_addr_unlock_bh(local->mdev);
  387. /* TODO: start scan as soon as all nullfunc frames are ACKed */
  388. queue_delayed_work(local->hw.workqueue, &local->scan_work,
  389. IEEE80211_CHANNEL_TIME);
  390. return 0;
  391. }
  392. int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
  393. struct cfg80211_scan_request *req)
  394. {
  395. struct ieee80211_local *local = sdata->local;
  396. struct ieee80211_if_sta *ifsta;
  397. if (!req)
  398. return -EINVAL;
  399. if (local->scan_req && local->scan_req != req)
  400. return -EBUSY;
  401. local->scan_req = req;
  402. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  403. return ieee80211_start_scan(sdata, req);
  404. /*
  405. * STA has a state machine that might need to defer scanning
  406. * while it's trying to associate/authenticate, therefore we
  407. * queue it up to the state machine in that case.
  408. */
  409. if (local->sw_scanning || local->hw_scanning) {
  410. if (local->scan_sdata == sdata)
  411. return 0;
  412. return -EBUSY;
  413. }
  414. ifsta = &sdata->u.sta;
  415. set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
  416. queue_work(local->hw.workqueue, &ifsta->work);
  417. return 0;
  418. }