scan.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938
  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. if (bss)
  339. ieee80211_rx_bss_put(sdata->local, bss);
  340. dev_kfree_skb(skb);
  341. return RX_QUEUED;
  342. }
  343. static void ieee80211_send_nullfunc(struct ieee80211_local *local,
  344. struct ieee80211_sub_if_data *sdata,
  345. int powersave)
  346. {
  347. struct sk_buff *skb;
  348. struct ieee80211_hdr *nullfunc;
  349. __le16 fc;
  350. skb = dev_alloc_skb(local->hw.extra_tx_headroom + 24);
  351. if (!skb) {
  352. printk(KERN_DEBUG "%s: failed to allocate buffer for nullfunc "
  353. "frame\n", sdata->dev->name);
  354. return;
  355. }
  356. skb_reserve(skb, local->hw.extra_tx_headroom);
  357. nullfunc = (struct ieee80211_hdr *) skb_put(skb, 24);
  358. memset(nullfunc, 0, 24);
  359. fc = cpu_to_le16(IEEE80211_FTYPE_DATA | IEEE80211_STYPE_NULLFUNC |
  360. IEEE80211_FCTL_TODS);
  361. if (powersave)
  362. fc |= cpu_to_le16(IEEE80211_FCTL_PM);
  363. nullfunc->frame_control = fc;
  364. memcpy(nullfunc->addr1, sdata->u.sta.bssid, ETH_ALEN);
  365. memcpy(nullfunc->addr2, sdata->dev->dev_addr, ETH_ALEN);
  366. memcpy(nullfunc->addr3, sdata->u.sta.bssid, ETH_ALEN);
  367. ieee80211_tx_skb(sdata, skb, 0);
  368. }
  369. void ieee80211_scan_completed(struct ieee80211_hw *hw)
  370. {
  371. struct ieee80211_local *local = hw_to_local(hw);
  372. struct ieee80211_sub_if_data *sdata;
  373. union iwreq_data wrqu;
  374. if (WARN_ON(!local->hw_scanning && !local->sw_scanning))
  375. return;
  376. local->last_scan_completed = jiffies;
  377. memset(&wrqu, 0, sizeof(wrqu));
  378. /*
  379. * local->scan_sdata could have been NULLed by the interface
  380. * down code in case we were scanning on an interface that is
  381. * being taken down.
  382. */
  383. sdata = local->scan_sdata;
  384. if (sdata)
  385. wireless_send_event(sdata->dev, SIOCGIWSCAN, &wrqu, NULL);
  386. if (local->hw_scanning) {
  387. local->hw_scanning = false;
  388. if (ieee80211_hw_config(local))
  389. printk(KERN_DEBUG "%s: failed to restore operational "
  390. "channel after scan\n", wiphy_name(local->hw.wiphy));
  391. goto done;
  392. }
  393. local->sw_scanning = false;
  394. if (ieee80211_hw_config(local))
  395. printk(KERN_DEBUG "%s: failed to restore operational "
  396. "channel after scan\n", wiphy_name(local->hw.wiphy));
  397. netif_tx_lock_bh(local->mdev);
  398. netif_addr_lock(local->mdev);
  399. local->filter_flags &= ~FIF_BCN_PRBRESP_PROMISC;
  400. local->ops->configure_filter(local_to_hw(local),
  401. FIF_BCN_PRBRESP_PROMISC,
  402. &local->filter_flags,
  403. local->mdev->mc_count,
  404. local->mdev->mc_list);
  405. netif_addr_unlock(local->mdev);
  406. netif_tx_unlock_bh(local->mdev);
  407. rcu_read_lock();
  408. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  409. /* Tell AP we're back */
  410. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  411. if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
  412. ieee80211_send_nullfunc(local, sdata, 0);
  413. netif_tx_wake_all_queues(sdata->dev);
  414. }
  415. } else
  416. netif_tx_wake_all_queues(sdata->dev);
  417. }
  418. rcu_read_unlock();
  419. done:
  420. ieee80211_mlme_notify_scan_completed(local);
  421. ieee80211_mesh_notify_scan_completed(local);
  422. }
  423. EXPORT_SYMBOL(ieee80211_scan_completed);
  424. void ieee80211_scan_work(struct work_struct *work)
  425. {
  426. struct ieee80211_local *local =
  427. container_of(work, struct ieee80211_local, scan_work.work);
  428. struct ieee80211_sub_if_data *sdata = local->scan_sdata;
  429. struct ieee80211_supported_band *sband;
  430. struct ieee80211_channel *chan;
  431. int skip;
  432. unsigned long next_delay = 0;
  433. /*
  434. * Avoid re-scheduling when the sdata is going away.
  435. */
  436. if (!netif_running(sdata->dev))
  437. return;
  438. switch (local->scan_state) {
  439. case SCAN_SET_CHANNEL:
  440. /*
  441. * Get current scan band. scan_band may be IEEE80211_NUM_BANDS
  442. * after we successfully scanned the last channel of the last
  443. * band (and the last band is supported by the hw)
  444. */
  445. if (local->scan_band < IEEE80211_NUM_BANDS)
  446. sband = local->hw.wiphy->bands[local->scan_band];
  447. else
  448. sband = NULL;
  449. /*
  450. * If we are at an unsupported band and have more bands
  451. * left to scan, advance to the next supported one.
  452. */
  453. while (!sband && local->scan_band < IEEE80211_NUM_BANDS - 1) {
  454. local->scan_band++;
  455. sband = local->hw.wiphy->bands[local->scan_band];
  456. local->scan_channel_idx = 0;
  457. }
  458. /* if no more bands/channels left, complete scan */
  459. if (!sband || local->scan_channel_idx >= sband->n_channels) {
  460. ieee80211_scan_completed(local_to_hw(local));
  461. return;
  462. }
  463. skip = 0;
  464. chan = &sband->channels[local->scan_channel_idx];
  465. if (chan->flags & IEEE80211_CHAN_DISABLED ||
  466. (sdata->vif.type == NL80211_IFTYPE_ADHOC &&
  467. chan->flags & IEEE80211_CHAN_NO_IBSS))
  468. skip = 1;
  469. if (!skip) {
  470. local->scan_channel = chan;
  471. if (ieee80211_hw_config(local)) {
  472. printk(KERN_DEBUG "%s: failed to set freq to "
  473. "%d MHz for scan\n", wiphy_name(local->hw.wiphy),
  474. chan->center_freq);
  475. skip = 1;
  476. }
  477. }
  478. /* advance state machine to next channel/band */
  479. local->scan_channel_idx++;
  480. if (local->scan_channel_idx >= sband->n_channels) {
  481. /*
  482. * scan_band may end up == IEEE80211_NUM_BANDS, but
  483. * we'll catch that case above and complete the scan
  484. * if that is the case.
  485. */
  486. local->scan_band++;
  487. local->scan_channel_idx = 0;
  488. }
  489. if (skip)
  490. break;
  491. next_delay = IEEE80211_PROBE_DELAY +
  492. usecs_to_jiffies(local->hw.channel_change_time);
  493. local->scan_state = SCAN_SEND_PROBE;
  494. break;
  495. case SCAN_SEND_PROBE:
  496. next_delay = IEEE80211_PASSIVE_CHANNEL_TIME;
  497. local->scan_state = SCAN_SET_CHANNEL;
  498. if (local->scan_channel->flags & IEEE80211_CHAN_PASSIVE_SCAN)
  499. break;
  500. ieee80211_send_probe_req(sdata, NULL, local->scan_ssid,
  501. local->scan_ssid_len);
  502. next_delay = IEEE80211_CHANNEL_TIME;
  503. break;
  504. }
  505. queue_delayed_work(local->hw.workqueue, &local->scan_work,
  506. next_delay);
  507. }
  508. int ieee80211_start_scan(struct ieee80211_sub_if_data *scan_sdata,
  509. u8 *ssid, size_t ssid_len)
  510. {
  511. struct ieee80211_local *local = scan_sdata->local;
  512. struct ieee80211_sub_if_data *sdata;
  513. if (ssid_len > IEEE80211_MAX_SSID_LEN)
  514. return -EINVAL;
  515. /* MLME-SCAN.request (page 118) page 144 (11.1.3.1)
  516. * BSSType: INFRASTRUCTURE, INDEPENDENT, ANY_BSS
  517. * BSSID: MACAddress
  518. * SSID
  519. * ScanType: ACTIVE, PASSIVE
  520. * ProbeDelay: delay (in microseconds) to be used prior to transmitting
  521. * a Probe frame during active scanning
  522. * ChannelList
  523. * MinChannelTime (>= ProbeDelay), in TU
  524. * MaxChannelTime: (>= MinChannelTime), in TU
  525. */
  526. /* MLME-SCAN.confirm
  527. * BSSDescriptionSet
  528. * ResultCode: SUCCESS, INVALID_PARAMETERS
  529. */
  530. if (local->sw_scanning || local->hw_scanning) {
  531. if (local->scan_sdata == scan_sdata)
  532. return 0;
  533. return -EBUSY;
  534. }
  535. if (local->ops->hw_scan) {
  536. int rc;
  537. local->hw_scanning = true;
  538. rc = local->ops->hw_scan(local_to_hw(local), ssid, ssid_len);
  539. if (rc) {
  540. local->hw_scanning = false;
  541. return rc;
  542. }
  543. local->scan_sdata = scan_sdata;
  544. return 0;
  545. }
  546. local->sw_scanning = true;
  547. rcu_read_lock();
  548. list_for_each_entry_rcu(sdata, &local->interfaces, list) {
  549. if (sdata->vif.type == NL80211_IFTYPE_STATION) {
  550. if (sdata->u.sta.flags & IEEE80211_STA_ASSOCIATED) {
  551. netif_tx_stop_all_queues(sdata->dev);
  552. ieee80211_send_nullfunc(local, sdata, 1);
  553. }
  554. } else
  555. netif_tx_stop_all_queues(sdata->dev);
  556. }
  557. rcu_read_unlock();
  558. if (ssid) {
  559. local->scan_ssid_len = ssid_len;
  560. memcpy(local->scan_ssid, ssid, ssid_len);
  561. } else
  562. local->scan_ssid_len = 0;
  563. local->scan_state = SCAN_SET_CHANNEL;
  564. local->scan_channel_idx = 0;
  565. local->scan_band = IEEE80211_BAND_2GHZ;
  566. local->scan_sdata = scan_sdata;
  567. netif_addr_lock_bh(local->mdev);
  568. local->filter_flags |= FIF_BCN_PRBRESP_PROMISC;
  569. local->ops->configure_filter(local_to_hw(local),
  570. FIF_BCN_PRBRESP_PROMISC,
  571. &local->filter_flags,
  572. local->mdev->mc_count,
  573. local->mdev->mc_list);
  574. netif_addr_unlock_bh(local->mdev);
  575. /* TODO: start scan as soon as all nullfunc frames are ACKed */
  576. queue_delayed_work(local->hw.workqueue, &local->scan_work,
  577. IEEE80211_CHANNEL_TIME);
  578. return 0;
  579. }
  580. int ieee80211_request_scan(struct ieee80211_sub_if_data *sdata,
  581. u8 *ssid, size_t ssid_len)
  582. {
  583. struct ieee80211_local *local = sdata->local;
  584. struct ieee80211_if_sta *ifsta;
  585. if (sdata->vif.type != NL80211_IFTYPE_STATION)
  586. return ieee80211_start_scan(sdata, ssid, ssid_len);
  587. /*
  588. * STA has a state machine that might need to defer scanning
  589. * while it's trying to associate/authenticate, therefore we
  590. * queue it up to the state machine in that case.
  591. */
  592. if (local->sw_scanning || local->hw_scanning) {
  593. if (local->scan_sdata == sdata)
  594. return 0;
  595. return -EBUSY;
  596. }
  597. ifsta = &sdata->u.sta;
  598. ifsta->scan_ssid_len = ssid_len;
  599. if (ssid_len)
  600. memcpy(ifsta->scan_ssid, ssid, ssid_len);
  601. set_bit(IEEE80211_STA_REQ_SCAN, &ifsta->request);
  602. queue_work(local->hw.workqueue, &ifsta->work);
  603. return 0;
  604. }
  605. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  606. struct ieee80211_bss *bss,
  607. char **current_ev, char *end_buf)
  608. {
  609. u8 *pos, *end, *next;
  610. struct iw_event iwe;
  611. if (bss == NULL || bss->ies == NULL)
  612. return;
  613. /*
  614. * If needed, fragment the IEs buffer (at IE boundaries) into short
  615. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  616. */
  617. pos = bss->ies;
  618. end = pos + bss->ies_len;
  619. while (end - pos > IW_GENERIC_IE_MAX) {
  620. next = pos + 2 + pos[1];
  621. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  622. next = next + 2 + next[1];
  623. memset(&iwe, 0, sizeof(iwe));
  624. iwe.cmd = IWEVGENIE;
  625. iwe.u.data.length = next - pos;
  626. *current_ev = iwe_stream_add_point(info, *current_ev,
  627. end_buf, &iwe, pos);
  628. pos = next;
  629. }
  630. if (end > pos) {
  631. memset(&iwe, 0, sizeof(iwe));
  632. iwe.cmd = IWEVGENIE;
  633. iwe.u.data.length = end - pos;
  634. *current_ev = iwe_stream_add_point(info, *current_ev,
  635. end_buf, &iwe, pos);
  636. }
  637. }
  638. static char *
  639. ieee80211_scan_result(struct ieee80211_local *local,
  640. struct iw_request_info *info,
  641. struct ieee80211_bss *bss,
  642. char *current_ev, char *end_buf)
  643. {
  644. struct iw_event iwe;
  645. char *buf;
  646. if (time_after(jiffies,
  647. bss->last_update + IEEE80211_SCAN_RESULT_EXPIRE))
  648. return current_ev;
  649. memset(&iwe, 0, sizeof(iwe));
  650. iwe.cmd = SIOCGIWAP;
  651. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  652. memcpy(iwe.u.ap_addr.sa_data, bss->bssid, ETH_ALEN);
  653. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  654. IW_EV_ADDR_LEN);
  655. memset(&iwe, 0, sizeof(iwe));
  656. iwe.cmd = SIOCGIWESSID;
  657. if (bss_mesh_cfg(bss)) {
  658. iwe.u.data.length = bss_mesh_id_len(bss);
  659. iwe.u.data.flags = 1;
  660. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  661. &iwe, bss_mesh_id(bss));
  662. } else {
  663. iwe.u.data.length = bss->ssid_len;
  664. iwe.u.data.flags = 1;
  665. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  666. &iwe, bss->ssid);
  667. }
  668. if (bss->capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
  669. || bss_mesh_cfg(bss)) {
  670. memset(&iwe, 0, sizeof(iwe));
  671. iwe.cmd = SIOCGIWMODE;
  672. if (bss_mesh_cfg(bss))
  673. iwe.u.mode = IW_MODE_MESH;
  674. else if (bss->capability & WLAN_CAPABILITY_ESS)
  675. iwe.u.mode = IW_MODE_MASTER;
  676. else
  677. iwe.u.mode = IW_MODE_ADHOC;
  678. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  679. &iwe, IW_EV_UINT_LEN);
  680. }
  681. memset(&iwe, 0, sizeof(iwe));
  682. iwe.cmd = SIOCGIWFREQ;
  683. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->freq);
  684. iwe.u.freq.e = 0;
  685. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  686. IW_EV_FREQ_LEN);
  687. memset(&iwe, 0, sizeof(iwe));
  688. iwe.cmd = SIOCGIWFREQ;
  689. iwe.u.freq.m = bss->freq;
  690. iwe.u.freq.e = 6;
  691. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  692. IW_EV_FREQ_LEN);
  693. memset(&iwe, 0, sizeof(iwe));
  694. iwe.cmd = IWEVQUAL;
  695. iwe.u.qual.qual = bss->qual;
  696. iwe.u.qual.level = bss->signal;
  697. iwe.u.qual.noise = bss->noise;
  698. iwe.u.qual.updated = local->wstats_flags;
  699. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  700. IW_EV_QUAL_LEN);
  701. memset(&iwe, 0, sizeof(iwe));
  702. iwe.cmd = SIOCGIWENCODE;
  703. if (bss->capability & WLAN_CAPABILITY_PRIVACY)
  704. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  705. else
  706. iwe.u.data.flags = IW_ENCODE_DISABLED;
  707. iwe.u.data.length = 0;
  708. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  709. &iwe, "");
  710. ieee80211_scan_add_ies(info, bss, &current_ev, end_buf);
  711. if (bss->supp_rates_len > 0) {
  712. /* display all supported rates in readable format */
  713. char *p = current_ev + iwe_stream_lcp_len(info);
  714. int i;
  715. memset(&iwe, 0, sizeof(iwe));
  716. iwe.cmd = SIOCGIWRATE;
  717. /* Those two flags are ignored... */
  718. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  719. for (i = 0; i < bss->supp_rates_len; i++) {
  720. iwe.u.bitrate.value = ((bss->supp_rates[i] &
  721. 0x7f) * 500000);
  722. p = iwe_stream_add_value(info, current_ev, p,
  723. end_buf, &iwe, IW_EV_PARAM_LEN);
  724. }
  725. current_ev = p;
  726. }
  727. buf = kmalloc(30, GFP_ATOMIC);
  728. if (buf) {
  729. memset(&iwe, 0, sizeof(iwe));
  730. iwe.cmd = IWEVCUSTOM;
  731. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->timestamp));
  732. iwe.u.data.length = strlen(buf);
  733. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  734. &iwe, buf);
  735. memset(&iwe, 0, sizeof(iwe));
  736. iwe.cmd = IWEVCUSTOM;
  737. sprintf(buf, " Last beacon: %dms ago",
  738. jiffies_to_msecs(jiffies - bss->last_update));
  739. iwe.u.data.length = strlen(buf);
  740. current_ev = iwe_stream_add_point(info, current_ev,
  741. end_buf, &iwe, buf);
  742. kfree(buf);
  743. }
  744. if (bss_mesh_cfg(bss)) {
  745. u8 *cfg = bss_mesh_cfg(bss);
  746. buf = kmalloc(50, GFP_ATOMIC);
  747. if (buf) {
  748. memset(&iwe, 0, sizeof(iwe));
  749. iwe.cmd = IWEVCUSTOM;
  750. sprintf(buf, "Mesh network (version %d)", cfg[0]);
  751. iwe.u.data.length = strlen(buf);
  752. current_ev = iwe_stream_add_point(info, current_ev,
  753. end_buf,
  754. &iwe, buf);
  755. sprintf(buf, "Path Selection Protocol ID: "
  756. "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
  757. cfg[4]);
  758. iwe.u.data.length = strlen(buf);
  759. current_ev = iwe_stream_add_point(info, current_ev,
  760. end_buf,
  761. &iwe, buf);
  762. sprintf(buf, "Path Selection Metric ID: "
  763. "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
  764. cfg[8]);
  765. iwe.u.data.length = strlen(buf);
  766. current_ev = iwe_stream_add_point(info, current_ev,
  767. end_buf,
  768. &iwe, buf);
  769. sprintf(buf, "Congestion Control Mode ID: "
  770. "0x%02X%02X%02X%02X", cfg[9], cfg[10],
  771. cfg[11], cfg[12]);
  772. iwe.u.data.length = strlen(buf);
  773. current_ev = iwe_stream_add_point(info, current_ev,
  774. end_buf,
  775. &iwe, buf);
  776. sprintf(buf, "Channel Precedence: "
  777. "0x%02X%02X%02X%02X", cfg[13], cfg[14],
  778. cfg[15], cfg[16]);
  779. iwe.u.data.length = strlen(buf);
  780. current_ev = iwe_stream_add_point(info, current_ev,
  781. end_buf,
  782. &iwe, buf);
  783. kfree(buf);
  784. }
  785. }
  786. return current_ev;
  787. }
  788. int ieee80211_scan_results(struct ieee80211_local *local,
  789. struct iw_request_info *info,
  790. char *buf, size_t len)
  791. {
  792. char *current_ev = buf;
  793. char *end_buf = buf + len;
  794. struct ieee80211_bss *bss;
  795. spin_lock_bh(&local->bss_lock);
  796. list_for_each_entry(bss, &local->bss_list, list) {
  797. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  798. spin_unlock_bh(&local->bss_lock);
  799. return -E2BIG;
  800. }
  801. current_ev = ieee80211_scan_result(local, info, bss,
  802. current_ev, end_buf);
  803. }
  804. spin_unlock_bh(&local->bss_lock);
  805. return current_ev - buf;
  806. }