scan.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937
  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:
  15. * order BSS list by RSSI(?) ("quality of AP")
  16. * scan result table filtering (by capability (privacy, IBSS/BSS, WPA/RSN IE,
  17. * SSID)
  18. */
  19. #include <linux/wireless.h>
  20. #include <linux/if_arp.h>
  21. #include <net/mac80211.h>
  22. #include <net/iw_handler.h>
  23. #include "ieee80211_i.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 / 5)
  28. void ieee80211_rx_bss_list_init(struct ieee80211_local *local)
  29. {
  30. spin_lock_init(&local->bss_lock);
  31. INIT_LIST_HEAD(&local->bss_list);
  32. }
  33. void ieee80211_rx_bss_list_deinit(struct ieee80211_local *local)
  34. {
  35. struct ieee80211_bss *bss, *tmp;
  36. list_for_each_entry_safe(bss, tmp, &local->bss_list, list)
  37. ieee80211_rx_bss_put(local, bss);
  38. }
  39. struct ieee80211_bss *
  40. ieee80211_rx_bss_get(struct ieee80211_local *local, u8 *bssid, int freq,
  41. u8 *ssid, u8 ssid_len)
  42. {
  43. struct ieee80211_bss *bss;
  44. spin_lock_bh(&local->bss_lock);
  45. bss = local->bss_hash[STA_HASH(bssid)];
  46. while (bss) {
  47. if (!bss_mesh_cfg(bss) &&
  48. !memcmp(bss->bssid, bssid, ETH_ALEN) &&
  49. bss->freq == freq &&
  50. bss->ssid_len == ssid_len &&
  51. (ssid_len == 0 || !memcmp(bss->ssid, ssid, ssid_len))) {
  52. atomic_inc(&bss->users);
  53. break;
  54. }
  55. bss = bss->hnext;
  56. }
  57. spin_unlock_bh(&local->bss_lock);
  58. return bss;
  59. }
  60. /* Caller must hold local->bss_lock */
  61. static void __ieee80211_rx_bss_hash_add(struct ieee80211_local *local,
  62. struct ieee80211_bss *bss)
  63. {
  64. u8 hash_idx;
  65. if (bss_mesh_cfg(bss))
  66. hash_idx = mesh_id_hash(bss_mesh_id(bss),
  67. bss_mesh_id_len(bss));
  68. else
  69. hash_idx = STA_HASH(bss->bssid);
  70. bss->hnext = local->bss_hash[hash_idx];
  71. local->bss_hash[hash_idx] = bss;
  72. }
  73. /* Caller must hold local->bss_lock */
  74. static void __ieee80211_rx_bss_hash_del(struct ieee80211_local *local,
  75. struct ieee80211_bss *bss)
  76. {
  77. struct ieee80211_bss *b, *prev = NULL;
  78. b = local->bss_hash[STA_HASH(bss->bssid)];
  79. while (b) {
  80. if (b == bss) {
  81. if (!prev)
  82. local->bss_hash[STA_HASH(bss->bssid)] =
  83. bss->hnext;
  84. else
  85. prev->hnext = bss->hnext;
  86. break;
  87. }
  88. prev = b;
  89. b = b->hnext;
  90. }
  91. }
  92. struct ieee80211_bss *
  93. ieee80211_rx_bss_add(struct ieee80211_local *local, u8 *bssid, int freq,
  94. u8 *ssid, u8 ssid_len)
  95. {
  96. struct ieee80211_bss *bss;
  97. bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
  98. if (!bss)
  99. return NULL;
  100. atomic_set(&bss->users, 2);
  101. memcpy(bss->bssid, bssid, ETH_ALEN);
  102. bss->freq = freq;
  103. if (ssid && ssid_len <= IEEE80211_MAX_SSID_LEN) {
  104. memcpy(bss->ssid, ssid, ssid_len);
  105. bss->ssid_len = ssid_len;
  106. }
  107. spin_lock_bh(&local->bss_lock);
  108. /* TODO: order by RSSI? */
  109. list_add_tail(&bss->list, &local->bss_list);
  110. __ieee80211_rx_bss_hash_add(local, bss);
  111. spin_unlock_bh(&local->bss_lock);
  112. return bss;
  113. }
  114. #ifdef CONFIG_MAC80211_MESH
  115. static struct ieee80211_bss *
  116. ieee80211_rx_mesh_bss_get(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
  117. u8 *mesh_cfg, int freq)
  118. {
  119. struct ieee80211_bss *bss;
  120. spin_lock_bh(&local->bss_lock);
  121. bss = local->bss_hash[mesh_id_hash(mesh_id, mesh_id_len)];
  122. while (bss) {
  123. if (bss_mesh_cfg(bss) &&
  124. !memcmp(bss_mesh_cfg(bss), mesh_cfg, MESH_CFG_CMP_LEN) &&
  125. bss->freq == freq &&
  126. mesh_id_len == bss->mesh_id_len &&
  127. (mesh_id_len == 0 || !memcmp(bss->mesh_id, mesh_id,
  128. mesh_id_len))) {
  129. atomic_inc(&bss->users);
  130. break;
  131. }
  132. bss = bss->hnext;
  133. }
  134. spin_unlock_bh(&local->bss_lock);
  135. return bss;
  136. }
  137. static struct ieee80211_bss *
  138. ieee80211_rx_mesh_bss_add(struct ieee80211_local *local, u8 *mesh_id, int mesh_id_len,
  139. u8 *mesh_cfg, int mesh_config_len, int freq)
  140. {
  141. struct ieee80211_bss *bss;
  142. if (mesh_config_len != MESH_CFG_LEN)
  143. return NULL;
  144. bss = kzalloc(sizeof(*bss), GFP_ATOMIC);
  145. if (!bss)
  146. return NULL;
  147. bss->mesh_cfg = kmalloc(MESH_CFG_CMP_LEN, GFP_ATOMIC);
  148. if (!bss->mesh_cfg) {
  149. kfree(bss);
  150. return NULL;
  151. }
  152. if (mesh_id_len && mesh_id_len <= IEEE80211_MAX_MESH_ID_LEN) {
  153. bss->mesh_id = kmalloc(mesh_id_len, GFP_ATOMIC);
  154. if (!bss->mesh_id) {
  155. kfree(bss->mesh_cfg);
  156. kfree(bss);
  157. return NULL;
  158. }
  159. memcpy(bss->mesh_id, mesh_id, mesh_id_len);
  160. }
  161. atomic_set(&bss->users, 2);
  162. memcpy(bss->mesh_cfg, mesh_cfg, MESH_CFG_CMP_LEN);
  163. bss->mesh_id_len = mesh_id_len;
  164. bss->freq = freq;
  165. spin_lock_bh(&local->bss_lock);
  166. /* TODO: order by RSSI? */
  167. list_add_tail(&bss->list, &local->bss_list);
  168. __ieee80211_rx_bss_hash_add(local, bss);
  169. spin_unlock_bh(&local->bss_lock);
  170. return bss;
  171. }
  172. #endif
  173. static void ieee80211_rx_bss_free(struct ieee80211_bss *bss)
  174. {
  175. kfree(bss->ies);
  176. kfree(bss_mesh_id(bss));
  177. kfree(bss_mesh_cfg(bss));
  178. kfree(bss);
  179. }
  180. void ieee80211_rx_bss_put(struct ieee80211_local *local,
  181. struct ieee80211_bss *bss)
  182. {
  183. local_bh_disable();
  184. if (!atomic_dec_and_lock(&bss->users, &local->bss_lock)) {
  185. local_bh_enable();
  186. return;
  187. }
  188. __ieee80211_rx_bss_hash_del(local, bss);
  189. list_del(&bss->list);
  190. spin_unlock_bh(&local->bss_lock);
  191. ieee80211_rx_bss_free(bss);
  192. }
  193. struct ieee80211_bss *
  194. ieee80211_bss_info_update(struct ieee80211_local *local,
  195. struct ieee80211_rx_status *rx_status,
  196. struct ieee80211_mgmt *mgmt,
  197. size_t len,
  198. struct ieee802_11_elems *elems,
  199. int freq, bool beacon)
  200. {
  201. struct ieee80211_bss *bss;
  202. int clen;
  203. #ifdef CONFIG_MAC80211_MESH
  204. if (elems->mesh_config)
  205. bss = ieee80211_rx_mesh_bss_get(local, elems->mesh_id,
  206. elems->mesh_id_len, elems->mesh_config, freq);
  207. else
  208. #endif
  209. bss = ieee80211_rx_bss_get(local, mgmt->bssid, freq,
  210. elems->ssid, elems->ssid_len);
  211. if (!bss) {
  212. #ifdef CONFIG_MAC80211_MESH
  213. if (elems->mesh_config)
  214. bss = ieee80211_rx_mesh_bss_add(local, elems->mesh_id,
  215. elems->mesh_id_len, elems->mesh_config,
  216. elems->mesh_config_len, freq);
  217. else
  218. #endif
  219. bss = ieee80211_rx_bss_add(local, mgmt->bssid, freq,
  220. elems->ssid, elems->ssid_len);
  221. if (!bss)
  222. return NULL;
  223. } else {
  224. #if 0
  225. /* TODO: order by RSSI? */
  226. spin_lock_bh(&local->bss_lock);
  227. list_move_tail(&bss->list, &local->bss_list);
  228. spin_unlock_bh(&local->bss_lock);
  229. #endif
  230. }
  231. /* save the ERP value so that it is available at association time */
  232. if (elems->erp_info && elems->erp_info_len >= 1) {
  233. bss->erp_value = elems->erp_info[0];
  234. bss->has_erp_value = 1;
  235. }
  236. bss->beacon_int = le16_to_cpu(mgmt->u.beacon.beacon_int);
  237. bss->capability = le16_to_cpu(mgmt->u.beacon.capab_info);
  238. if (elems->tim) {
  239. struct ieee80211_tim_ie *tim_ie =
  240. (struct ieee80211_tim_ie *)elems->tim;
  241. bss->dtim_period = tim_ie->dtim_period;
  242. }
  243. /* set default value for buggy APs */
  244. if (!elems->tim || bss->dtim_period == 0)
  245. bss->dtim_period = 1;
  246. bss->supp_rates_len = 0;
  247. if (elems->supp_rates) {
  248. clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
  249. if (clen > elems->supp_rates_len)
  250. clen = elems->supp_rates_len;
  251. memcpy(&bss->supp_rates[bss->supp_rates_len], elems->supp_rates,
  252. clen);
  253. bss->supp_rates_len += clen;
  254. }
  255. if (elems->ext_supp_rates) {
  256. clen = IEEE80211_MAX_SUPP_RATES - bss->supp_rates_len;
  257. if (clen > elems->ext_supp_rates_len)
  258. clen = elems->ext_supp_rates_len;
  259. memcpy(&bss->supp_rates[bss->supp_rates_len],
  260. elems->ext_supp_rates, clen);
  261. bss->supp_rates_len += clen;
  262. }
  263. bss->band = rx_status->band;
  264. bss->timestamp = le64_to_cpu(mgmt->u.beacon.timestamp);
  265. bss->last_update = jiffies;
  266. bss->signal = rx_status->signal;
  267. bss->noise = rx_status->noise;
  268. bss->qual = rx_status->qual;
  269. bss->wmm_used = elems->wmm_param || elems->wmm_info;
  270. if (!beacon)
  271. bss->last_probe_resp = jiffies;
  272. /*
  273. * For probe responses, or if we don't have any information yet,
  274. * use the IEs from the beacon.
  275. */
  276. if (!bss->ies || !beacon) {
  277. if (bss->ies == NULL || bss->ies_len < elems->total_len) {
  278. kfree(bss->ies);
  279. bss->ies = kmalloc(elems->total_len, GFP_ATOMIC);
  280. }
  281. if (bss->ies) {
  282. memcpy(bss->ies, elems->ie_start, elems->total_len);
  283. bss->ies_len = elems->total_len;
  284. } else
  285. bss->ies_len = 0;
  286. }
  287. return bss;
  288. }
  289. ieee80211_rx_result
  290. ieee80211_scan_rx(struct ieee80211_sub_if_data *sdata, struct sk_buff *skb,
  291. struct ieee80211_rx_status *rx_status)
  292. {
  293. struct ieee80211_mgmt *mgmt;
  294. struct ieee80211_bss *bss;
  295. u8 *elements;
  296. struct ieee80211_channel *channel;
  297. size_t baselen;
  298. int freq;
  299. __le16 fc;
  300. bool presp, beacon = false;
  301. struct ieee802_11_elems elems;
  302. if (skb->len < 2)
  303. return RX_DROP_UNUSABLE;
  304. mgmt = (struct ieee80211_mgmt *) skb->data;
  305. fc = mgmt->frame_control;
  306. if (ieee80211_is_ctl(fc))
  307. return RX_CONTINUE;
  308. if (skb->len < 24)
  309. return RX_DROP_MONITOR;
  310. presp = ieee80211_is_probe_resp(fc);
  311. if (presp) {
  312. /* ignore ProbeResp to foreign address */
  313. if (memcmp(mgmt->da, sdata->dev->dev_addr, ETH_ALEN))
  314. return RX_DROP_MONITOR;
  315. presp = true;
  316. elements = mgmt->u.probe_resp.variable;
  317. baselen = offsetof(struct ieee80211_mgmt, u.probe_resp.variable);
  318. } else {
  319. beacon = ieee80211_is_beacon(fc);
  320. baselen = offsetof(struct ieee80211_mgmt, u.beacon.variable);
  321. elements = mgmt->u.beacon.variable;
  322. }
  323. if (!presp && !beacon)
  324. return RX_CONTINUE;
  325. if (baselen > skb->len)
  326. return RX_DROP_MONITOR;
  327. ieee802_11_parse_elems(elements, skb->len - baselen, &elems);
  328. if (elems.ds_params && elems.ds_params_len == 1)
  329. freq = ieee80211_channel_to_frequency(elems.ds_params[0]);
  330. else
  331. freq = rx_status->freq;
  332. channel = ieee80211_get_channel(sdata->local->hw.wiphy, freq);
  333. if (!channel || channel->flags & IEEE80211_CHAN_DISABLED)
  334. return RX_DROP_MONITOR;
  335. bss = ieee80211_bss_info_update(sdata->local, rx_status,
  336. mgmt, skb->len, &elems,
  337. freq, beacon);
  338. ieee80211_rx_bss_put(sdata->local, bss);
  339. dev_kfree_skb(skb);
  340. return RX_QUEUED;
  341. }
  342. static void ieee80211_send_nullfunc(struct ieee80211_local *local,
  343. struct ieee80211_sub_if_data *sdata,
  344. int powersave)
  345. {
  346. struct sk_buff *skb;
  347. struct ieee80211_hdr *nullfunc;
  348. __le16 fc;
  349. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
  350. if (!skb) {
  351. printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
  352. "frame\n", sdata->dev->name);
  353. return;
  354. }
  355. skb_reserve(skb, local->hw.extra_tx_headroom);
  356. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
  357. memset(nullfunc, 0, 24);
  358. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  359. IEEE80211_FCTL_TODS);
  360. if (powersave)
  361. fc |= cpu_to_le16(IEEE80211_FCTL_PM);
  362. nullfunc->frame_control = fc;
  363. memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
  364. memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
  365. memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
  366. ieee80211_tx_skb(sdata, skb, 0);
  367. }
  368. void ieee80211_scan_completed(struct ieee80211_hw *hw)
  369. {
  370. struct ieee80211_local *local = hw_to_local(hw);
  371. struct ieee80211_sub_if_data *sdata;
  372. union iwreq_data wrqu;
  373. if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
  374. return;
  375. local->last_scan_completed = jiffies;
  376. memset(&wrqu, 0, sizeof(wrqu));
  377. /*
  378. * local->scan_sdata could have been NULLed by the interface
  379. * down code in case we were scanning on an interface that is
  380. * being taken down.
  381. */
  382. sdata = local->scan_sdata;
  383. if (sdata)
  384. wireless_send_event(sdata->dev, SIOCGIWSCAN, &wrqu, NULL);
  385. if (local->hw_scanning) {
  386. local->hw_scanning = false;
  387. if (ieee80211_hw_config(local))
  388. printk(KERN_DEBUG "%s: failed to restore operational "
  389. "channel after scan\n", wiphy_name(local->hw.wiphy));
  390. goto done;
  391. }
  392. local->sw_scanning = false;
  393. if (ieee80211_hw_config(local))
  394. printk(KERN_DEBUG "%s: failed to restore operational "
  395. "channel after scan\n", wiphy_name(local->hw.wiphy));
  396. netif_tx_lock_bh(local->mdev);
  397. netif_addr_lock(local->mdev);
  398. local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
  399. local->ops->configure_filter(local_to_hw(local),
  400. FIF_BCN_PRBRESP_PROMISC,
  401. &local->filter_flags,
  402. local->mdev->mc_count,
  403. local->mdev->mc_list);
  404. netif_addr_unlock(local->mdev);
  405. netif_tx_unlock_bh(local->mdev);
  406. rcu_read_lock();
  407. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  408. /* Tell AP we're back */
  409. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  410. if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
  411. ieee80211_send_nullfunc(local, sdata, 0);
  412. netif_tx_wake_all_queues(sdata->dev);
  413. }
  414. } else
  415. netif_tx_wake_all_queues(sdata->dev);
  416. }
  417. rcu_read_unlock();
  418. done:
  419. ieee80211_mlme_notify_scan_completed(local);
  420. ieee80211_mesh_notify_scan_completed(local);
  421. }
  422. EXPORT_SYMBOL(ieee80211_scan_completed);
  423. void ieee80211_scan_work(struct work_struct *work)
  424. {
  425. struct ieee80211_local *local =
  426. container_of(work, struct ieee80211_local, scan_work.work);
  427. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  428. struct ieee80211_supported_band *sband;
  429. struct ieee80211_channel *chan;
  430. int skip;
  431. unsigned long next_delay = 0;
  432. /*
  433. * Avoid re-scheduling when the sdata is going away.
  434. */
  435. if (!netif_running(sdata->dev))
  436. return;
  437. switch (local->scan_state) {
  438. case SCAN_SET_CHANNEL:
  439. /*
  440. * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
  441. * after we successfully scanned the last channel of the last
  442. * band (and the last band is supported by the hw)
  443. */
  444. if (local->scan_band < IEEE80211_NUM_BANDS)
  445. sband = local->hw.wiphy->bands[local->scan_band];
  446. else
  447. sband = NULL;
  448. /*
  449. * If we are at an unsupported band and have more bands
  450. * left to scan, advance to the next supported one.
  451. */
  452. while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
  453. local->scan_band++;
  454. sband = local->hw.wiphy->bands[local->scan_band];
  455. local->scan_channel_idx = 0;
  456. }
  457. /* if no more bands/channels left, complete scan */
  458. if (!sband || local->scan_channel_idx >= sband->n_channels) {
  459. ieee80211_scan_completed(local_to_hw(local));
  460. return;
  461. }
  462. skip = 0;
  463. chan = &sband->channels[local->scan_channel_idx];
  464. if (chan->flags & IEEE80211_CHAN_DISABLED ||
  465. (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  466. chan->flags & IEEE80211_CHAN_NO_IBSS))
  467. skip = 1;
  468. if (!skip) {
  469. local->scan_channel = chan;
  470. if (ieee80211_hw_config(local)) {
  471. printk(KERN_DEBUG "%s: failed to set freq to "
  472. "%d MHz for scan\n", wiphy_name(local->hw.wiphy),
  473. chan->center_freq);
  474. skip = 1;
  475. }
  476. }
  477. /* advance state machine to next channel/band */
  478. local->scan_channel_idx++;
  479. if (local->scan_channel_idx >= sband->n_channels) {
  480. /*
  481. * scan_band may end up == IEEE80211_NUM_BANDS, but
  482. * we'll catch that case above and complete the scan
  483. * if that is the case.
  484. */
  485. local->scan_band++;
  486. local->scan_channel_idx = 0;
  487. }
  488. if (skip)
  489. break;
  490. next_delay = IEEE80211_PROBE_DELAY +
  491. usecs_to_jiffies(local->hw.channel_change_time);
  492. local->scan_state = SCAN_SEND_PROBE;
  493. break;
  494. case SCAN_SEND_PROBE:
  495. next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  496. local->scan_state = SCAN_SET_CHANNEL;
  497. if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  498. break;
  499. ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
  500. local->scan_ssid_len);
  501. next_delay = IEEE80211_CHANNEL_TIME;
  502. break;
  503. }
  504. queue_delayed_work(local->hw.workqueue, &local->scan_work,
  505. next_delay);
  506. }
  507. int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
  508. u8 *ssid, size_t ssid_len)
  509. {
  510. struct ieee80211_local *local = scan_sdata->local;
  511. struct ieee80211_sub_if_data *sdata;
  512. if (ssid_len > IEEE80211_MAX_SSID_LEN)
  513. return -EINVAL;
  514. /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
  515. * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
  516. * BSSID: MACAddress
  517. * SSID
  518. * ScanType: ACTIVE, PASSIVE
  519. * ProbeDelay: delay (in microseconds) to be used prior to transmitting
  520. * a Probe frame during active scanning
  521. * ChannelList
  522. * MinChannelTime (>= ProbeDelay), in TU
  523. * MaxChannelTime: (>= MinChannelTime), in TU
  524. */
  525. /* MLME-SCAN.confirm
  526. * BSSDescriptionSet
  527. * ResultCode: SUCCESS, INVALID_PARAMETERS
  528. */
  529. if (local->sw_scanning || local->hw_scanning) {
  530. if (local->scan_sdata == scan_sdata)
  531. return 0;
  532. return -EBUSY;
  533. }
  534. if (local->ops->hw_scan) {
  535. int rc;
  536. local->hw_scanning = true;
  537. rc = local->ops->hw_scan(local_to_hw(local), ssid, ssid_len);
  538. if (rc) {
  539. local->hw_scanning = false;
  540. return rc;
  541. }
  542. local->scan_sdata = scan_sdata;
  543. return 0;
  544. }
  545. local->sw_scanning = true;
  546. rcu_read_lock();
  547. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  548. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  549. if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
  550. netif_tx_stop_all_queues(sdata->dev);
  551. ieee80211_send_nullfunc(local, sdata, 1);
  552. }
  553. } else
  554. netif_tx_stop_all_queues(sdata->dev);
  555. }
  556. rcu_read_unlock();
  557. if (ssid) {
  558. local->scan_ssid_len = ssid_len;
  559. memcpy(local->scan_ssid, ssid, ssid_len);
  560. } else
  561. local->scan_ssid_len = 0;
  562. local->scan_state = SCAN_SET_CHANNEL;
  563. local->scan_channel_idx = 0;
  564. local->scan_band = IEEE80211_BAND_2GHZ;
  565. local->scan_sdata = scan_sdata;
  566. netif_addr_lock_bh(local->mdev);
  567. local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
  568. local->ops->configure_filter(local_to_hw(local),
  569. FIF_BCN_PRBRESP_PROMISC,
  570. &local->filter_flags,
  571. local->mdev->mc_count,
  572. local->mdev->mc_list);
  573. netif_addr_unlock_bh(local->mdev);
  574. /* TODO: start scan as soon as all nullfunc frames are ACKed */
  575. queue_delayed_work(local->hw.workqueue, &local->scan_work,
  576. IEEE80211_CHANNEL_TIME);
  577. return 0;
  578. }
  579. int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
  580. u8 *ssid, size_t ssid_len)
  581. {
  582. struct ieee80211_local *local = sdata->local;
  583. struct ieee80211_if_sta *ifsta;
  584. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  585. return ieee80211_start_scan(sdata, ssid, ssid_len);
  586. /*
  587. * STA has a state machine that might need to defer scanning
  588. * while it's trying to associate/authenticate, therefore we
  589. * queue it up to the state machine in that case.
  590. */
  591. if (local->sw_scanning || local->hw_scanning) {
  592. if (local->scan_sdata == sdata)
  593. return 0;
  594. return -EBUSY;
  595. }
  596. ifsta = &sdata->u.sta;
  597. ifsta->scan_ssid_len = ssid_len;
  598. if (ssid_len)
  599. memcpy(ifsta->scan_ssid, ssid, ssid_len);
  600. set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
  601. queue_work(local->hw.workqueue, &ifsta->work);
  602. return 0;
  603. }
  604. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  605. struct ieee80211_bss *bss,
  606. char **current_ev, char *end_buf)
  607. {
  608. u8 *pos, *end, *next;
  609. struct iw_event iwe;
  610. if (bss == NULL || bss->ies == NULL)
  611. return;
  612. /*
  613. * If needed, fragment the IEs buffer (at IE boundaries) into short
  614. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  615. */
  616. pos = bss->ies;
  617. end = pos + bss->ies_len;
  618. while (end - pos > IW_GENERIC_IE_MAX) {
  619. next = pos + 2 + pos[1];
  620. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  621. next = next + 2 + next[1];
  622. memset(&iwe, 0, sizeof(iwe));
  623. iwe.cmd = IWEVGENIE;
  624. iwe.u.data.length = next - pos;
  625. *current_ev = iwe_stream_add_point(info, *current_ev,
  626. end_buf, &iwe, pos);
  627. pos = next;
  628. }
  629. if (end > pos) {
  630. memset(&iwe, 0, sizeof(iwe));
  631. iwe.cmd = IWEVGENIE;
  632. iwe.u.data.length = end - pos;
  633. *current_ev = iwe_stream_add_point(info, *current_ev,
  634. end_buf, &iwe, pos);
  635. }
  636. }
  637. static char *
  638. ieee80211_scan_result(struct ieee80211_local *local,
  639. struct iw_request_info *info,
  640. struct ieee80211_bss *bss,
  641. char *current_ev, char *end_buf)
  642. {
  643. struct iw_event iwe;
  644. char *buf;
  645. if (time_after(jiffies,
  646. bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
  647. return current_ev;
  648. memset(&iwe, 0, sizeof(iwe));
  649. iwe.cmd = SIOCGIWAP;
  650. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  651. memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
  652. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  653. IW_EV_ADDR_LEN);
  654. memset(&iwe, 0, sizeof(iwe));
  655. iwe.cmd = SIOCGIWESSID;
  656. if (bss_mesh_cfg(bss)) {
  657. iwe.u.data.length = bss_mesh_id_len(bss);
  658. iwe.u.data.flags = 1;
  659. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  660. &iwe, bss_mesh_id(bss));
  661. } else {
  662. iwe.u.data.length = bss->ssid_len;
  663. iwe.u.data.flags = 1;
  664. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  665. &iwe, bss->ssid);
  666. }
  667. if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
  668. || bss_mesh_cfg(bss)) {
  669. memset(&iwe, 0, sizeof(iwe));
  670. iwe.cmd = SIOCGIWMODE;
  671. if (bss_mesh_cfg(bss))
  672. iwe.u.mode = IW_MODE_MESH;
  673. else if (bss->capability & WLAN_CAPABILITY_ESS)
  674. iwe.u.mode = IW_MODE_MASTER;
  675. else
  676. iwe.u.mode = IW_MODE_ADHOC;
  677. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  678. &iwe, IW_EV_UINT_LEN);
  679. }
  680. memset(&iwe, 0, sizeof(iwe));
  681. iwe.cmd = SIOCGIWFREQ;
  682. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
  683. iwe.u.freq.e = 0;
  684. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  685. IW_EV_FREQ_LEN);
  686. memset(&iwe, 0, sizeof(iwe));
  687. iwe.cmd = SIOCGIWFREQ;
  688. iwe.u.freq.m = bss->freq;
  689. iwe.u.freq.e = 6;
  690. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  691. IW_EV_FREQ_LEN);
  692. memset(&iwe, 0, sizeof(iwe));
  693. iwe.cmd = IWEVQUAL;
  694. iwe.u.qual.qual = bss->qual;
  695. iwe.u.qual.level = bss->signal;
  696. iwe.u.qual.noise = bss->noise;
  697. iwe.u.qual.updated = local->wstats_flags;
  698. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  699. IW_EV_QUAL_LEN);
  700. memset(&iwe, 0, sizeof(iwe));
  701. iwe.cmd = SIOCGIWENCODE;
  702. if (bss->capability & WLAN_CAPABILITY_PRIVACY)
  703. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  704. else
  705. iwe.u.data.flags = IW_ENCODE_DISABLED;
  706. iwe.u.data.length = 0;
  707. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  708. &iwe, "");
  709. ieee80211_scan_add_ies(info, bss, &current_ev, end_buf);
  710. if (bss->supp_rates_len > 0) {
  711. /* display all supported rates in readable format */
  712. char *p = current_ev + iwe_stream_lcp_len(info);
  713. int i;
  714. memset(&iwe, 0, sizeof(iwe));
  715. iwe.cmd = SIOCGIWRATE;
  716. /* Those two flags are ignored... */
  717. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  718. for (i = 0; i < bss->supp_rates_len; i++) {
  719. iwe.u.bitrate.value = ((bss->supp_rates[i] &
  720. 0x7f) * 500000);
  721. p = iwe_stream_add_value(info, current_ev, p,
  722. end_buf, &iwe, IW_EV_PARAM_LEN);
  723. }
  724. current_ev = p;
  725. }
  726. buf = kmalloc(30, GFP_ATOMIC);
  727. if (buf) {
  728. memset(&iwe, 0, sizeof(iwe));
  729. iwe.cmd = IWEVCUSTOM;
  730. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
  731. iwe.u.data.length = strlen(buf);
  732. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  733. &iwe, buf);
  734. memset(&iwe, 0, sizeof(iwe));
  735. iwe.cmd = IWEVCUSTOM;
  736. sprintf(buf, " Last beacon: %dms ago",
  737. jiffies_to_msecs(jiffies - bss->last_update));
  738. iwe.u.data.length = strlen(buf);
  739. current_ev = iwe_stream_add_point(info, current_ev,
  740. end_buf, &iwe, buf);
  741. kfree(buf);
  742. }
  743. if (bss_mesh_cfg(bss)) {
  744. u8 *cfg = bss_mesh_cfg(bss);
  745. buf = kmalloc(50, GFP_ATOMIC);
  746. if (buf) {
  747. memset(&iwe, 0, sizeof(iwe));
  748. iwe.cmd = IWEVCUSTOM;
  749. sprintf(buf, "Mesh network (version %d)", cfg[0]);
  750. iwe.u.data.length = strlen(buf);
  751. current_ev = iwe_stream_add_point(info, current_ev,
  752. end_buf,
  753. &iwe, buf);
  754. sprintf(buf, "Path Selection Protocol ID: "
  755. "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
  756. cfg[4]);
  757. iwe.u.data.length = strlen(buf);
  758. current_ev = iwe_stream_add_point(info, current_ev,
  759. end_buf,
  760. &iwe, buf);
  761. sprintf(buf, "Path Selection Metric ID: "
  762. "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
  763. cfg[8]);
  764. iwe.u.data.length = strlen(buf);
  765. current_ev = iwe_stream_add_point(info, current_ev,
  766. end_buf,
  767. &iwe, buf);
  768. sprintf(buf, "Congestion Control Mode ID: "
  769. "0x%02X%02X%02X%02X", cfg[9], cfg[10],
  770. cfg[11], cfg[12]);
  771. iwe.u.data.length = strlen(buf);
  772. current_ev = iwe_stream_add_point(info, current_ev,
  773. end_buf,
  774. &iwe, buf);
  775. sprintf(buf, "Channel Precedence: "
  776. "0x%02X%02X%02X%02X", cfg[13], cfg[14],
  777. cfg[15], cfg[16]);
  778. iwe.u.data.length = strlen(buf);
  779. current_ev = iwe_stream_add_point(info, current_ev,
  780. end_buf,
  781. &iwe, buf);
  782. kfree(buf);
  783. }
  784. }
  785. return current_ev;
  786. }
  787. int ieee80211_scan_results(struct ieee80211_local *local,
  788. struct iw_request_info *info,
  789. char *buf, size_t len)
  790. {
  791. char *current_ev = buf;
  792. char *end_buf = buf + len;
  793. struct ieee80211_bss *bss;
  794. spin_lock_bh(&local->bss_lock);
  795. list_for_each_entry(bss, &local->bss_list, list) {
  796. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  797. spin_unlock_bh(&local->bss_lock);
  798. return -E2BIG;
  799. }
  800. current_ev = ieee80211_scan_result(local, info, bss,
  801. current_ev, end_buf);
  802. }
  803. spin_unlock_bh(&local->bss_lock);
  804. return current_ev - buf;
  805. }