scan.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017
  1. /*
  2. * cfg80211 scan result handling
  3. *
  4. * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/module.h>
  8. #include <linux/netdevice.h>
  9. #include <linux/wireless.h>
  10. #include <linux/nl80211.h>
  11. #include <linux/etherdevice.h>
  12. #include <net/arp.h>
  13. #include <net/cfg80211.h>
  14. #include <net/iw_handler.h>
  15. #include "core.h"
  16. #include "nl80211.h"
  17. #include "wext-compat.h"
  18. #define IEEE80211_SCAN_RESULT_EXPIRE (15 * HZ)
  19. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
  20. {
  21. struct cfg80211_scan_request *request;
  22. struct net_device *dev;
  23. #ifdef CONFIG_WIRELESS_EXT
  24. union iwreq_data wrqu;
  25. #endif
  26. ASSERT_RDEV_LOCK(rdev);
  27. request = rdev->scan_req;
  28. if (!request)
  29. return;
  30. dev = request->dev;
  31. /*
  32. * This must be before sending the other events!
  33. * Otherwise, wpa_supplicant gets completely confused with
  34. * wext events.
  35. */
  36. cfg80211_sme_scan_done(dev);
  37. if (request->aborted)
  38. nl80211_send_scan_aborted(rdev, dev);
  39. else
  40. nl80211_send_scan_done(rdev, dev);
  41. #ifdef CONFIG_WIRELESS_EXT
  42. if (!request->aborted) {
  43. memset(&wrqu, 0, sizeof(wrqu));
  44. wireless_send_event(dev, SIOCGIWSCAN, &wrqu, NULL);
  45. }
  46. #endif
  47. dev_put(dev);
  48. rdev->scan_req = NULL;
  49. /*
  50. * OK. If this is invoked with "leak" then we can't
  51. * free this ... but we've cleaned it up anyway. The
  52. * driver failed to call the scan_done callback, so
  53. * all bets are off, it might still be trying to use
  54. * the scan request or not ... if it accesses the dev
  55. * in there (it shouldn't anyway) then it may crash.
  56. */
  57. if (!leak)
  58. kfree(request);
  59. }
  60. void __cfg80211_scan_done(struct work_struct *wk)
  61. {
  62. struct cfg80211_registered_device *rdev;
  63. rdev = container_of(wk, struct cfg80211_registered_device,
  64. scan_done_wk);
  65. cfg80211_lock_rdev(rdev);
  66. ___cfg80211_scan_done(rdev, false);
  67. cfg80211_unlock_rdev(rdev);
  68. }
  69. void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
  70. {
  71. WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
  72. request->aborted = aborted;
  73. schedule_work(&wiphy_to_dev(request->wiphy)->scan_done_wk);
  74. }
  75. EXPORT_SYMBOL(cfg80211_scan_done);
  76. static void bss_release(struct kref *ref)
  77. {
  78. struct cfg80211_internal_bss *bss;
  79. bss = container_of(ref, struct cfg80211_internal_bss, ref);
  80. if (bss->pub.free_priv)
  81. bss->pub.free_priv(&bss->pub);
  82. if (bss->ies_allocated)
  83. kfree(bss->pub.information_elements);
  84. BUG_ON(atomic_read(&bss->hold));
  85. kfree(bss);
  86. }
  87. /* must hold dev->bss_lock! */
  88. void cfg80211_bss_age(struct cfg80211_registered_device *dev,
  89. unsigned long age_secs)
  90. {
  91. struct cfg80211_internal_bss *bss;
  92. unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
  93. list_for_each_entry(bss, &dev->bss_list, list) {
  94. bss->ts -= age_jiffies;
  95. }
  96. }
  97. /* must hold dev->bss_lock! */
  98. void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
  99. {
  100. struct cfg80211_internal_bss *bss, *tmp;
  101. bool expired = false;
  102. list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
  103. if (atomic_read(&bss->hold))
  104. continue;
  105. if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
  106. continue;
  107. list_del(&bss->list);
  108. rb_erase(&bss->rbn, &dev->bss_tree);
  109. kref_put(&bss->ref, bss_release);
  110. expired = true;
  111. }
  112. if (expired)
  113. dev->bss_generation++;
  114. }
  115. static u8 *find_ie(u8 num, u8 *ies, int len)
  116. {
  117. while (len > 2 && ies[0] != num) {
  118. len -= ies[1] + 2;
  119. ies += ies[1] + 2;
  120. }
  121. if (len < 2)
  122. return NULL;
  123. if (len < 2 + ies[1])
  124. return NULL;
  125. return ies;
  126. }
  127. static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
  128. {
  129. const u8 *ie1 = find_ie(num, ies1, len1);
  130. const u8 *ie2 = find_ie(num, ies2, len2);
  131. int r;
  132. if (!ie1 && !ie2)
  133. return 0;
  134. if (!ie1 || !ie2)
  135. return -1;
  136. r = memcmp(ie1 + 2, ie2 + 2, min(ie1[1], ie2[1]));
  137. if (r == 0 && ie1[1] != ie2[1])
  138. return ie2[1] - ie1[1];
  139. return r;
  140. }
  141. static bool is_bss(struct cfg80211_bss *a,
  142. const u8 *bssid,
  143. const u8 *ssid, size_t ssid_len)
  144. {
  145. const u8 *ssidie;
  146. if (bssid && compare_ether_addr(a->bssid, bssid))
  147. return false;
  148. if (!ssid)
  149. return true;
  150. ssidie = find_ie(WLAN_EID_SSID,
  151. a->information_elements,
  152. a->len_information_elements);
  153. if (!ssidie)
  154. return false;
  155. if (ssidie[1] != ssid_len)
  156. return false;
  157. return memcmp(ssidie + 2, ssid, ssid_len) == 0;
  158. }
  159. static bool is_mesh(struct cfg80211_bss *a,
  160. const u8 *meshid, size_t meshidlen,
  161. const u8 *meshcfg)
  162. {
  163. const u8 *ie;
  164. if (!is_zero_ether_addr(a->bssid))
  165. return false;
  166. ie = find_ie(WLAN_EID_MESH_ID,
  167. a->information_elements,
  168. a->len_information_elements);
  169. if (!ie)
  170. return false;
  171. if (ie[1] != meshidlen)
  172. return false;
  173. if (memcmp(ie + 2, meshid, meshidlen))
  174. return false;
  175. ie = find_ie(WLAN_EID_MESH_CONFIG,
  176. a->information_elements,
  177. a->len_information_elements);
  178. if (!ie)
  179. return false;
  180. if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
  181. return false;
  182. /*
  183. * Ignore mesh capability (last two bytes of the IE) when
  184. * comparing since that may differ between stations taking
  185. * part in the same mesh.
  186. */
  187. return memcmp(ie + 2, meshcfg, IEEE80211_MESH_CONFIG_LEN - 2) == 0;
  188. }
  189. static int cmp_bss(struct cfg80211_bss *a,
  190. struct cfg80211_bss *b)
  191. {
  192. int r;
  193. if (a->channel != b->channel)
  194. return b->channel->center_freq - a->channel->center_freq;
  195. r = memcmp(a->bssid, b->bssid, ETH_ALEN);
  196. if (r)
  197. return r;
  198. if (is_zero_ether_addr(a->bssid)) {
  199. r = cmp_ies(WLAN_EID_MESH_ID,
  200. a->information_elements,
  201. a->len_information_elements,
  202. b->information_elements,
  203. b->len_information_elements);
  204. if (r)
  205. return r;
  206. return cmp_ies(WLAN_EID_MESH_CONFIG,
  207. a->information_elements,
  208. a->len_information_elements,
  209. b->information_elements,
  210. b->len_information_elements);
  211. }
  212. return cmp_ies(WLAN_EID_SSID,
  213. a->information_elements,
  214. a->len_information_elements,
  215. b->information_elements,
  216. b->len_information_elements);
  217. }
  218. struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
  219. struct ieee80211_channel *channel,
  220. const u8 *bssid,
  221. const u8 *ssid, size_t ssid_len,
  222. u16 capa_mask, u16 capa_val)
  223. {
  224. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  225. struct cfg80211_internal_bss *bss, *res = NULL;
  226. spin_lock_bh(&dev->bss_lock);
  227. list_for_each_entry(bss, &dev->bss_list, list) {
  228. if ((bss->pub.capability & capa_mask) != capa_val)
  229. continue;
  230. if (channel && bss->pub.channel != channel)
  231. continue;
  232. if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
  233. res = bss;
  234. kref_get(&res->ref);
  235. break;
  236. }
  237. }
  238. spin_unlock_bh(&dev->bss_lock);
  239. if (!res)
  240. return NULL;
  241. return &res->pub;
  242. }
  243. EXPORT_SYMBOL(cfg80211_get_bss);
  244. struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
  245. struct ieee80211_channel *channel,
  246. const u8 *meshid, size_t meshidlen,
  247. const u8 *meshcfg)
  248. {
  249. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  250. struct cfg80211_internal_bss *bss, *res = NULL;
  251. spin_lock_bh(&dev->bss_lock);
  252. list_for_each_entry(bss, &dev->bss_list, list) {
  253. if (channel && bss->pub.channel != channel)
  254. continue;
  255. if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
  256. res = bss;
  257. kref_get(&res->ref);
  258. break;
  259. }
  260. }
  261. spin_unlock_bh(&dev->bss_lock);
  262. if (!res)
  263. return NULL;
  264. return &res->pub;
  265. }
  266. EXPORT_SYMBOL(cfg80211_get_mesh);
  267. static void rb_insert_bss(struct cfg80211_registered_device *dev,
  268. struct cfg80211_internal_bss *bss)
  269. {
  270. struct rb_node **p = &dev->bss_tree.rb_node;
  271. struct rb_node *parent = NULL;
  272. struct cfg80211_internal_bss *tbss;
  273. int cmp;
  274. while (*p) {
  275. parent = *p;
  276. tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
  277. cmp = cmp_bss(&bss->pub, &tbss->pub);
  278. if (WARN_ON(!cmp)) {
  279. /* will sort of leak this BSS */
  280. return;
  281. }
  282. if (cmp < 0)
  283. p = &(*p)->rb_left;
  284. else
  285. p = &(*p)->rb_right;
  286. }
  287. rb_link_node(&bss->rbn, parent, p);
  288. rb_insert_color(&bss->rbn, &dev->bss_tree);
  289. }
  290. static struct cfg80211_internal_bss *
  291. rb_find_bss(struct cfg80211_registered_device *dev,
  292. struct cfg80211_internal_bss *res)
  293. {
  294. struct rb_node *n = dev->bss_tree.rb_node;
  295. struct cfg80211_internal_bss *bss;
  296. int r;
  297. while (n) {
  298. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  299. r = cmp_bss(&res->pub, &bss->pub);
  300. if (r == 0)
  301. return bss;
  302. else if (r < 0)
  303. n = n->rb_left;
  304. else
  305. n = n->rb_right;
  306. }
  307. return NULL;
  308. }
  309. static struct cfg80211_internal_bss *
  310. cfg80211_bss_update(struct cfg80211_registered_device *dev,
  311. struct cfg80211_internal_bss *res,
  312. bool overwrite)
  313. {
  314. struct cfg80211_internal_bss *found = NULL;
  315. const u8 *meshid, *meshcfg;
  316. /*
  317. * The reference to "res" is donated to this function.
  318. */
  319. if (WARN_ON(!res->pub.channel)) {
  320. kref_put(&res->ref, bss_release);
  321. return NULL;
  322. }
  323. res->ts = jiffies;
  324. if (is_zero_ether_addr(res->pub.bssid)) {
  325. /* must be mesh, verify */
  326. meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements,
  327. res->pub.len_information_elements);
  328. meshcfg = find_ie(WLAN_EID_MESH_CONFIG,
  329. res->pub.information_elements,
  330. res->pub.len_information_elements);
  331. if (!meshid || !meshcfg ||
  332. meshcfg[1] != IEEE80211_MESH_CONFIG_LEN) {
  333. /* bogus mesh */
  334. kref_put(&res->ref, bss_release);
  335. return NULL;
  336. }
  337. }
  338. spin_lock_bh(&dev->bss_lock);
  339. found = rb_find_bss(dev, res);
  340. if (found) {
  341. found->pub.beacon_interval = res->pub.beacon_interval;
  342. found->pub.tsf = res->pub.tsf;
  343. found->pub.signal = res->pub.signal;
  344. found->pub.capability = res->pub.capability;
  345. found->ts = res->ts;
  346. /* overwrite IEs */
  347. if (overwrite) {
  348. size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
  349. size_t ielen = res->pub.len_information_elements;
  350. if (!found->ies_allocated && ksize(found) >= used + ielen) {
  351. memcpy(found->pub.information_elements,
  352. res->pub.information_elements, ielen);
  353. found->pub.len_information_elements = ielen;
  354. } else {
  355. u8 *ies = found->pub.information_elements;
  356. if (found->ies_allocated)
  357. ies = krealloc(ies, ielen, GFP_ATOMIC);
  358. else
  359. ies = kmalloc(ielen, GFP_ATOMIC);
  360. if (ies) {
  361. memcpy(ies, res->pub.information_elements, ielen);
  362. found->ies_allocated = true;
  363. found->pub.information_elements = ies;
  364. found->pub.len_information_elements = ielen;
  365. }
  366. }
  367. }
  368. kref_put(&res->ref, bss_release);
  369. } else {
  370. /* this "consumes" the reference */
  371. list_add_tail(&res->list, &dev->bss_list);
  372. rb_insert_bss(dev, res);
  373. found = res;
  374. }
  375. dev->bss_generation++;
  376. spin_unlock_bh(&dev->bss_lock);
  377. kref_get(&found->ref);
  378. return found;
  379. }
  380. struct cfg80211_bss*
  381. cfg80211_inform_bss(struct wiphy *wiphy,
  382. struct ieee80211_channel *channel,
  383. const u8 *bssid,
  384. u64 timestamp, u16 capability, u16 beacon_interval,
  385. const u8 *ie, size_t ielen,
  386. s32 signal, gfp_t gfp)
  387. {
  388. struct cfg80211_internal_bss *res;
  389. size_t privsz;
  390. if (WARN_ON(!wiphy))
  391. return NULL;
  392. privsz = wiphy->bss_priv_size;
  393. if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
  394. (signal < 0 || signal > 100)))
  395. return NULL;
  396. res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
  397. if (!res)
  398. return NULL;
  399. memcpy(res->pub.bssid, bssid, ETH_ALEN);
  400. res->pub.channel = channel;
  401. res->pub.signal = signal;
  402. res->pub.tsf = timestamp;
  403. res->pub.beacon_interval = beacon_interval;
  404. res->pub.capability = capability;
  405. /* point to after the private area */
  406. res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
  407. memcpy(res->pub.information_elements, ie, ielen);
  408. res->pub.len_information_elements = ielen;
  409. kref_init(&res->ref);
  410. res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, 0);
  411. if (!res)
  412. return NULL;
  413. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  414. regulatory_hint_found_beacon(wiphy, channel, gfp);
  415. /* cfg80211_bss_update gives us a referenced result */
  416. return &res->pub;
  417. }
  418. EXPORT_SYMBOL(cfg80211_inform_bss);
  419. struct cfg80211_bss *
  420. cfg80211_inform_bss_frame(struct wiphy *wiphy,
  421. struct ieee80211_channel *channel,
  422. struct ieee80211_mgmt *mgmt, size_t len,
  423. s32 signal, gfp_t gfp)
  424. {
  425. struct cfg80211_internal_bss *res;
  426. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  427. u.probe_resp.variable);
  428. bool overwrite;
  429. size_t privsz = wiphy->bss_priv_size;
  430. if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
  431. (signal < 0 || signal > 100)))
  432. return NULL;
  433. if (WARN_ON(!mgmt || !wiphy ||
  434. len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  435. return NULL;
  436. res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
  437. if (!res)
  438. return NULL;
  439. memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
  440. res->pub.channel = channel;
  441. res->pub.signal = signal;
  442. res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  443. res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  444. res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  445. /* point to after the private area */
  446. res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
  447. memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
  448. res->pub.len_information_elements = ielen;
  449. kref_init(&res->ref);
  450. overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
  451. res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
  452. if (!res)
  453. return NULL;
  454. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  455. regulatory_hint_found_beacon(wiphy, channel, gfp);
  456. /* cfg80211_bss_update gives us a referenced result */
  457. return &res->pub;
  458. }
  459. EXPORT_SYMBOL(cfg80211_inform_bss_frame);
  460. void cfg80211_put_bss(struct cfg80211_bss *pub)
  461. {
  462. struct cfg80211_internal_bss *bss;
  463. if (!pub)
  464. return;
  465. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  466. kref_put(&bss->ref, bss_release);
  467. }
  468. EXPORT_SYMBOL(cfg80211_put_bss);
  469. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  470. {
  471. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  472. struct cfg80211_internal_bss *bss;
  473. if (WARN_ON(!pub))
  474. return;
  475. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  476. spin_lock_bh(&dev->bss_lock);
  477. list_del(&bss->list);
  478. dev->bss_generation++;
  479. rb_erase(&bss->rbn, &dev->bss_tree);
  480. spin_unlock_bh(&dev->bss_lock);
  481. kref_put(&bss->ref, bss_release);
  482. }
  483. EXPORT_SYMBOL(cfg80211_unlink_bss);
  484. #ifdef CONFIG_WIRELESS_EXT
  485. int cfg80211_wext_siwscan(struct net_device *dev,
  486. struct iw_request_info *info,
  487. union iwreq_data *wrqu, char *extra)
  488. {
  489. struct cfg80211_registered_device *rdev;
  490. struct wiphy *wiphy;
  491. struct iw_scan_req *wreq = NULL;
  492. struct cfg80211_scan_request *creq;
  493. int i, err, n_channels = 0;
  494. enum ieee80211_band band;
  495. if (!netif_running(dev))
  496. return -ENETDOWN;
  497. if (wrqu->data.length == sizeof(struct iw_scan_req))
  498. wreq = (struct iw_scan_req *)extra;
  499. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  500. if (IS_ERR(rdev))
  501. return PTR_ERR(rdev);
  502. if (rdev->scan_req) {
  503. err = -EBUSY;
  504. goto out;
  505. }
  506. wiphy = &rdev->wiphy;
  507. /* Determine number of channels, needed to allocate creq */
  508. if (wreq && wreq->num_channels)
  509. n_channels = wreq->num_channels;
  510. else {
  511. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  512. if (wiphy->bands[band])
  513. n_channels += wiphy->bands[band]->n_channels;
  514. }
  515. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  516. n_channels * sizeof(void *),
  517. GFP_ATOMIC);
  518. if (!creq) {
  519. err = -ENOMEM;
  520. goto out;
  521. }
  522. creq->wiphy = wiphy;
  523. creq->dev = dev;
  524. /* SSIDs come after channels */
  525. creq->ssids = (void *)&creq->channels[n_channels];
  526. creq->n_channels = n_channels;
  527. creq->n_ssids = 1;
  528. /* translate "Scan on frequencies" request */
  529. i = 0;
  530. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  531. int j;
  532. if (!wiphy->bands[band])
  533. continue;
  534. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  535. /* If we have a wireless request structure and the
  536. * wireless request specifies frequencies, then search
  537. * for the matching hardware channel.
  538. */
  539. if (wreq && wreq->num_channels) {
  540. int k;
  541. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  542. for (k = 0; k < wreq->num_channels; k++) {
  543. int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
  544. if (wext_freq == wiphy_freq)
  545. goto wext_freq_found;
  546. }
  547. goto wext_freq_not_found;
  548. }
  549. wext_freq_found:
  550. creq->channels[i] = &wiphy->bands[band]->channels[j];
  551. i++;
  552. wext_freq_not_found: ;
  553. }
  554. }
  555. /* Set real number of channels specified in creq->channels[] */
  556. creq->n_channels = i;
  557. /* translate "Scan for SSID" request */
  558. if (wreq) {
  559. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  560. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
  561. return -EINVAL;
  562. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  563. creq->ssids[0].ssid_len = wreq->essid_len;
  564. }
  565. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  566. creq->n_ssids = 0;
  567. }
  568. rdev->scan_req = creq;
  569. err = rdev->ops->scan(wiphy, dev, creq);
  570. if (err) {
  571. rdev->scan_req = NULL;
  572. kfree(creq);
  573. } else {
  574. nl80211_send_scan_start(rdev, dev);
  575. dev_hold(dev);
  576. }
  577. out:
  578. cfg80211_unlock_rdev(rdev);
  579. return err;
  580. }
  581. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  582. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  583. struct cfg80211_bss *bss,
  584. char **current_ev, char *end_buf)
  585. {
  586. u8 *pos, *end, *next;
  587. struct iw_event iwe;
  588. if (!bss->information_elements ||
  589. !bss->len_information_elements)
  590. return;
  591. /*
  592. * If needed, fragment the IEs buffer (at IE boundaries) into short
  593. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  594. */
  595. pos = bss->information_elements;
  596. end = pos + bss->len_information_elements;
  597. while (end - pos > IW_GENERIC_IE_MAX) {
  598. next = pos + 2 + pos[1];
  599. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  600. next = next + 2 + next[1];
  601. memset(&iwe, 0, sizeof(iwe));
  602. iwe.cmd = IWEVGENIE;
  603. iwe.u.data.length = next - pos;
  604. *current_ev = iwe_stream_add_point(info, *current_ev,
  605. end_buf, &iwe, pos);
  606. pos = next;
  607. }
  608. if (end > pos) {
  609. memset(&iwe, 0, sizeof(iwe));
  610. iwe.cmd = IWEVGENIE;
  611. iwe.u.data.length = end - pos;
  612. *current_ev = iwe_stream_add_point(info, *current_ev,
  613. end_buf, &iwe, pos);
  614. }
  615. }
  616. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  617. {
  618. unsigned long end = jiffies;
  619. if (end >= start)
  620. return jiffies_to_msecs(end - start);
  621. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  622. }
  623. static char *
  624. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  625. struct cfg80211_internal_bss *bss, char *current_ev,
  626. char *end_buf)
  627. {
  628. struct iw_event iwe;
  629. u8 *buf, *cfg, *p;
  630. u8 *ie = bss->pub.information_elements;
  631. int rem = bss->pub.len_information_elements, i, sig;
  632. bool ismesh = false;
  633. memset(&iwe, 0, sizeof(iwe));
  634. iwe.cmd = SIOCGIWAP;
  635. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  636. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  637. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  638. IW_EV_ADDR_LEN);
  639. memset(&iwe, 0, sizeof(iwe));
  640. iwe.cmd = SIOCGIWFREQ;
  641. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  642. iwe.u.freq.e = 0;
  643. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  644. IW_EV_FREQ_LEN);
  645. memset(&iwe, 0, sizeof(iwe));
  646. iwe.cmd = SIOCGIWFREQ;
  647. iwe.u.freq.m = bss->pub.channel->center_freq;
  648. iwe.u.freq.e = 6;
  649. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  650. IW_EV_FREQ_LEN);
  651. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  652. memset(&iwe, 0, sizeof(iwe));
  653. iwe.cmd = IWEVQUAL;
  654. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  655. IW_QUAL_NOISE_INVALID |
  656. IW_QUAL_QUAL_UPDATED;
  657. switch (wiphy->signal_type) {
  658. case CFG80211_SIGNAL_TYPE_MBM:
  659. sig = bss->pub.signal / 100;
  660. iwe.u.qual.level = sig;
  661. iwe.u.qual.updated |= IW_QUAL_DBM;
  662. if (sig < -110) /* rather bad */
  663. sig = -110;
  664. else if (sig > -40) /* perfect */
  665. sig = -40;
  666. /* will give a range of 0 .. 70 */
  667. iwe.u.qual.qual = sig + 110;
  668. break;
  669. case CFG80211_SIGNAL_TYPE_UNSPEC:
  670. iwe.u.qual.level = bss->pub.signal;
  671. /* will give range 0 .. 100 */
  672. iwe.u.qual.qual = bss->pub.signal;
  673. break;
  674. default:
  675. /* not reached */
  676. break;
  677. }
  678. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  679. &iwe, IW_EV_QUAL_LEN);
  680. }
  681. memset(&iwe, 0, sizeof(iwe));
  682. iwe.cmd = SIOCGIWENCODE;
  683. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  684. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  685. else
  686. iwe.u.data.flags = IW_ENCODE_DISABLED;
  687. iwe.u.data.length = 0;
  688. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  689. &iwe, "");
  690. while (rem >= 2) {
  691. /* invalid data */
  692. if (ie[1] > rem - 2)
  693. break;
  694. switch (ie[0]) {
  695. case WLAN_EID_SSID:
  696. memset(&iwe, 0, sizeof(iwe));
  697. iwe.cmd = SIOCGIWESSID;
  698. iwe.u.data.length = ie[1];
  699. iwe.u.data.flags = 1;
  700. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  701. &iwe, ie + 2);
  702. break;
  703. case WLAN_EID_MESH_ID:
  704. memset(&iwe, 0, sizeof(iwe));
  705. iwe.cmd = SIOCGIWESSID;
  706. iwe.u.data.length = ie[1];
  707. iwe.u.data.flags = 1;
  708. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  709. &iwe, ie + 2);
  710. break;
  711. case WLAN_EID_MESH_CONFIG:
  712. ismesh = true;
  713. if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
  714. break;
  715. buf = kmalloc(50, GFP_ATOMIC);
  716. if (!buf)
  717. break;
  718. cfg = ie + 2;
  719. memset(&iwe, 0, sizeof(iwe));
  720. iwe.cmd = IWEVCUSTOM;
  721. sprintf(buf, "Mesh network (version %d)", cfg[0]);
  722. iwe.u.data.length = strlen(buf);
  723. current_ev = iwe_stream_add_point(info, current_ev,
  724. end_buf,
  725. &iwe, buf);
  726. sprintf(buf, "Path Selection Protocol ID: "
  727. "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
  728. cfg[4]);
  729. iwe.u.data.length = strlen(buf);
  730. current_ev = iwe_stream_add_point(info, current_ev,
  731. end_buf,
  732. &iwe, buf);
  733. sprintf(buf, "Path Selection Metric ID: "
  734. "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
  735. cfg[8]);
  736. iwe.u.data.length = strlen(buf);
  737. current_ev = iwe_stream_add_point(info, current_ev,
  738. end_buf,
  739. &iwe, buf);
  740. sprintf(buf, "Congestion Control Mode ID: "
  741. "0x%02X%02X%02X%02X", cfg[9], cfg[10],
  742. cfg[11], cfg[12]);
  743. iwe.u.data.length = strlen(buf);
  744. current_ev = iwe_stream_add_point(info, current_ev,
  745. end_buf,
  746. &iwe, buf);
  747. sprintf(buf, "Channel Precedence: "
  748. "0x%02X%02X%02X%02X", cfg[13], cfg[14],
  749. cfg[15], cfg[16]);
  750. iwe.u.data.length = strlen(buf);
  751. current_ev = iwe_stream_add_point(info, current_ev,
  752. end_buf,
  753. &iwe, buf);
  754. kfree(buf);
  755. break;
  756. case WLAN_EID_SUPP_RATES:
  757. case WLAN_EID_EXT_SUPP_RATES:
  758. /* display all supported rates in readable format */
  759. p = current_ev + iwe_stream_lcp_len(info);
  760. memset(&iwe, 0, sizeof(iwe));
  761. iwe.cmd = SIOCGIWRATE;
  762. /* Those two flags are ignored... */
  763. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  764. for (i = 0; i < ie[1]; i++) {
  765. iwe.u.bitrate.value =
  766. ((ie[i + 2] & 0x7f) * 500000);
  767. p = iwe_stream_add_value(info, current_ev, p,
  768. end_buf, &iwe, IW_EV_PARAM_LEN);
  769. }
  770. current_ev = p;
  771. break;
  772. }
  773. rem -= ie[1] + 2;
  774. ie += ie[1] + 2;
  775. }
  776. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
  777. || ismesh) {
  778. memset(&iwe, 0, sizeof(iwe));
  779. iwe.cmd = SIOCGIWMODE;
  780. if (ismesh)
  781. iwe.u.mode = IW_MODE_MESH;
  782. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  783. iwe.u.mode = IW_MODE_MASTER;
  784. else
  785. iwe.u.mode = IW_MODE_ADHOC;
  786. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  787. &iwe, IW_EV_UINT_LEN);
  788. }
  789. buf = kmalloc(30, GFP_ATOMIC);
  790. if (buf) {
  791. memset(&iwe, 0, sizeof(iwe));
  792. iwe.cmd = IWEVCUSTOM;
  793. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  794. iwe.u.data.length = strlen(buf);
  795. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  796. &iwe, buf);
  797. memset(&iwe, 0, sizeof(iwe));
  798. iwe.cmd = IWEVCUSTOM;
  799. sprintf(buf, " Last beacon: %ums ago",
  800. elapsed_jiffies_msecs(bss->ts));
  801. iwe.u.data.length = strlen(buf);
  802. current_ev = iwe_stream_add_point(info, current_ev,
  803. end_buf, &iwe, buf);
  804. kfree(buf);
  805. }
  806. ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
  807. return current_ev;
  808. }
  809. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  810. struct iw_request_info *info,
  811. char *buf, size_t len)
  812. {
  813. char *current_ev = buf;
  814. char *end_buf = buf + len;
  815. struct cfg80211_internal_bss *bss;
  816. spin_lock_bh(&dev->bss_lock);
  817. cfg80211_bss_expire(dev);
  818. list_for_each_entry(bss, &dev->bss_list, list) {
  819. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  820. spin_unlock_bh(&dev->bss_lock);
  821. return -E2BIG;
  822. }
  823. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  824. current_ev, end_buf);
  825. }
  826. spin_unlock_bh(&dev->bss_lock);
  827. return current_ev - buf;
  828. }
  829. int cfg80211_wext_giwscan(struct net_device *dev,
  830. struct iw_request_info *info,
  831. struct iw_point *data, char *extra)
  832. {
  833. struct cfg80211_registered_device *rdev;
  834. int res;
  835. if (!netif_running(dev))
  836. return -ENETDOWN;
  837. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  838. if (IS_ERR(rdev))
  839. return PTR_ERR(rdev);
  840. if (rdev->scan_req) {
  841. res = -EAGAIN;
  842. goto out;
  843. }
  844. res = ieee80211_scan_results(rdev, info, extra, data->length);
  845. data->length = 0;
  846. if (res >= 0) {
  847. data->length = res;
  848. res = 0;
  849. }
  850. out:
  851. cfg80211_unlock_rdev(rdev);
  852. return res;
  853. }
  854. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  855. #endif