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