scan.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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_CFG80211_WEXT
  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_CFG80211_WEXT
  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. queue_work(cfg80211_wq, &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] != sizeof(struct ieee80211_meshconf_ie))
  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,
  188. sizeof(struct ieee80211_meshconf_ie) - 2) == 0;
  189. }
  190. static int cmp_bss(struct cfg80211_bss *a,
  191. struct cfg80211_bss *b)
  192. {
  193. int r;
  194. if (a->channel != b->channel)
  195. return b->channel->center_freq - a->channel->center_freq;
  196. r = memcmp(a->bssid, b->bssid, ETH_ALEN);
  197. if (r)
  198. return r;
  199. if (is_zero_ether_addr(a->bssid)) {
  200. r = cmp_ies(WLAN_EID_MESH_ID,
  201. a->information_elements,
  202. a->len_information_elements,
  203. b->information_elements,
  204. b->len_information_elements);
  205. if (r)
  206. return r;
  207. return cmp_ies(WLAN_EID_MESH_CONFIG,
  208. a->information_elements,
  209. a->len_information_elements,
  210. b->information_elements,
  211. b->len_information_elements);
  212. }
  213. return cmp_ies(WLAN_EID_SSID,
  214. a->information_elements,
  215. a->len_information_elements,
  216. b->information_elements,
  217. b->len_information_elements);
  218. }
  219. struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
  220. struct ieee80211_channel *channel,
  221. const u8 *bssid,
  222. const u8 *ssid, size_t ssid_len,
  223. u16 capa_mask, u16 capa_val)
  224. {
  225. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  226. struct cfg80211_internal_bss *bss, *res = NULL;
  227. spin_lock_bh(&dev->bss_lock);
  228. list_for_each_entry(bss, &dev->bss_list, list) {
  229. if ((bss->pub.capability & capa_mask) != capa_val)
  230. continue;
  231. if (channel && bss->pub.channel != channel)
  232. continue;
  233. if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
  234. res = bss;
  235. kref_get(&res->ref);
  236. break;
  237. }
  238. }
  239. spin_unlock_bh(&dev->bss_lock);
  240. if (!res)
  241. return NULL;
  242. return &res->pub;
  243. }
  244. EXPORT_SYMBOL(cfg80211_get_bss);
  245. struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
  246. struct ieee80211_channel *channel,
  247. const u8 *meshid, size_t meshidlen,
  248. const u8 *meshcfg)
  249. {
  250. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  251. struct cfg80211_internal_bss *bss, *res = NULL;
  252. spin_lock_bh(&dev->bss_lock);
  253. list_for_each_entry(bss, &dev->bss_list, list) {
  254. if (channel && bss->pub.channel != channel)
  255. continue;
  256. if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
  257. res = bss;
  258. kref_get(&res->ref);
  259. break;
  260. }
  261. }
  262. spin_unlock_bh(&dev->bss_lock);
  263. if (!res)
  264. return NULL;
  265. return &res->pub;
  266. }
  267. EXPORT_SYMBOL(cfg80211_get_mesh);
  268. static void rb_insert_bss(struct cfg80211_registered_device *dev,
  269. struct cfg80211_internal_bss *bss)
  270. {
  271. struct rb_node **p = &dev->bss_tree.rb_node;
  272. struct rb_node *parent = NULL;
  273. struct cfg80211_internal_bss *tbss;
  274. int cmp;
  275. while (*p) {
  276. parent = *p;
  277. tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
  278. cmp = cmp_bss(&bss->pub, &tbss->pub);
  279. if (WARN_ON(!cmp)) {
  280. /* will sort of leak this BSS */
  281. return;
  282. }
  283. if (cmp < 0)
  284. p = &(*p)->rb_left;
  285. else
  286. p = &(*p)->rb_right;
  287. }
  288. rb_link_node(&bss->rbn, parent, p);
  289. rb_insert_color(&bss->rbn, &dev->bss_tree);
  290. }
  291. static struct cfg80211_internal_bss *
  292. rb_find_bss(struct cfg80211_registered_device *dev,
  293. struct cfg80211_internal_bss *res)
  294. {
  295. struct rb_node *n = dev->bss_tree.rb_node;
  296. struct cfg80211_internal_bss *bss;
  297. int r;
  298. while (n) {
  299. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  300. r = cmp_bss(&res->pub, &bss->pub);
  301. if (r == 0)
  302. return bss;
  303. else if (r < 0)
  304. n = n->rb_left;
  305. else
  306. n = n->rb_right;
  307. }
  308. return NULL;
  309. }
  310. static struct cfg80211_internal_bss *
  311. cfg80211_bss_update(struct cfg80211_registered_device *dev,
  312. struct cfg80211_internal_bss *res,
  313. bool overwrite)
  314. {
  315. struct cfg80211_internal_bss *found = NULL;
  316. const u8 *meshid, *meshcfg;
  317. /*
  318. * The reference to "res" is donated to this function.
  319. */
  320. if (WARN_ON(!res->pub.channel)) {
  321. kref_put(&res->ref, bss_release);
  322. return NULL;
  323. }
  324. res->ts = jiffies;
  325. if (is_zero_ether_addr(res->pub.bssid)) {
  326. /* must be mesh, verify */
  327. meshid = find_ie(WLAN_EID_MESH_ID, res->pub.information_elements,
  328. res->pub.len_information_elements);
  329. meshcfg = find_ie(WLAN_EID_MESH_CONFIG,
  330. res->pub.information_elements,
  331. res->pub.len_information_elements);
  332. if (!meshid || !meshcfg ||
  333. meshcfg[1] != sizeof(struct ieee80211_meshconf_ie)) {
  334. /* bogus mesh */
  335. kref_put(&res->ref, bss_release);
  336. return NULL;
  337. }
  338. }
  339. spin_lock_bh(&dev->bss_lock);
  340. found = rb_find_bss(dev, res);
  341. if (found) {
  342. found->pub.beacon_interval = res->pub.beacon_interval;
  343. found->pub.tsf = res->pub.tsf;
  344. found->pub.signal = res->pub.signal;
  345. found->pub.capability = res->pub.capability;
  346. found->ts = res->ts;
  347. /* overwrite IEs */
  348. if (overwrite) {
  349. size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
  350. size_t ielen = res->pub.len_information_elements;
  351. if (!found->ies_allocated && ksize(found) >= used + ielen) {
  352. memcpy(found->pub.information_elements,
  353. res->pub.information_elements, ielen);
  354. found->pub.len_information_elements = ielen;
  355. } else {
  356. u8 *ies = found->pub.information_elements;
  357. if (found->ies_allocated)
  358. ies = krealloc(ies, ielen, GFP_ATOMIC);
  359. else
  360. ies = kmalloc(ielen, GFP_ATOMIC);
  361. if (ies) {
  362. memcpy(ies, res->pub.information_elements, ielen);
  363. found->ies_allocated = true;
  364. found->pub.information_elements = ies;
  365. found->pub.len_information_elements = ielen;
  366. }
  367. }
  368. }
  369. kref_put(&res->ref, bss_release);
  370. } else {
  371. /* this "consumes" the reference */
  372. list_add_tail(&res->list, &dev->bss_list);
  373. rb_insert_bss(dev, res);
  374. found = res;
  375. }
  376. dev->bss_generation++;
  377. spin_unlock_bh(&dev->bss_lock);
  378. kref_get(&found->ref);
  379. return found;
  380. }
  381. struct cfg80211_bss*
  382. cfg80211_inform_bss(struct wiphy *wiphy,
  383. struct ieee80211_channel *channel,
  384. const u8 *bssid,
  385. u64 timestamp, u16 capability, u16 beacon_interval,
  386. const u8 *ie, size_t ielen,
  387. s32 signal, gfp_t gfp)
  388. {
  389. struct cfg80211_internal_bss *res;
  390. size_t privsz;
  391. if (WARN_ON(!wiphy))
  392. return NULL;
  393. privsz = wiphy->bss_priv_size;
  394. if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
  395. (signal < 0 || signal > 100)))
  396. return NULL;
  397. res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
  398. if (!res)
  399. return NULL;
  400. memcpy(res->pub.bssid, bssid, ETH_ALEN);
  401. res->pub.channel = channel;
  402. res->pub.signal = signal;
  403. res->pub.tsf = timestamp;
  404. res->pub.beacon_interval = beacon_interval;
  405. res->pub.capability = capability;
  406. /* point to after the private area */
  407. res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
  408. memcpy(res->pub.information_elements, ie, ielen);
  409. res->pub.len_information_elements = ielen;
  410. kref_init(&res->ref);
  411. res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, 0);
  412. if (!res)
  413. return NULL;
  414. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  415. regulatory_hint_found_beacon(wiphy, channel, gfp);
  416. /* cfg80211_bss_update gives us a referenced result */
  417. return &res->pub;
  418. }
  419. EXPORT_SYMBOL(cfg80211_inform_bss);
  420. struct cfg80211_bss *
  421. cfg80211_inform_bss_frame(struct wiphy *wiphy,
  422. struct ieee80211_channel *channel,
  423. struct ieee80211_mgmt *mgmt, size_t len,
  424. s32 signal, gfp_t gfp)
  425. {
  426. struct cfg80211_internal_bss *res;
  427. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  428. u.probe_resp.variable);
  429. bool overwrite;
  430. size_t privsz = wiphy->bss_priv_size;
  431. if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
  432. (signal < 0 || signal > 100)))
  433. return NULL;
  434. if (WARN_ON(!mgmt || !wiphy ||
  435. len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  436. return NULL;
  437. res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
  438. if (!res)
  439. return NULL;
  440. memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
  441. res->pub.channel = channel;
  442. res->pub.signal = signal;
  443. res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  444. res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  445. res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  446. /* point to after the private area */
  447. res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
  448. memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
  449. res->pub.len_information_elements = ielen;
  450. kref_init(&res->ref);
  451. overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
  452. res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
  453. if (!res)
  454. return NULL;
  455. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  456. regulatory_hint_found_beacon(wiphy, channel, gfp);
  457. /* cfg80211_bss_update gives us a referenced result */
  458. return &res->pub;
  459. }
  460. EXPORT_SYMBOL(cfg80211_inform_bss_frame);
  461. void cfg80211_put_bss(struct cfg80211_bss *pub)
  462. {
  463. struct cfg80211_internal_bss *bss;
  464. if (!pub)
  465. return;
  466. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  467. kref_put(&bss->ref, bss_release);
  468. }
  469. EXPORT_SYMBOL(cfg80211_put_bss);
  470. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  471. {
  472. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  473. struct cfg80211_internal_bss *bss;
  474. if (WARN_ON(!pub))
  475. return;
  476. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  477. spin_lock_bh(&dev->bss_lock);
  478. list_del(&bss->list);
  479. dev->bss_generation++;
  480. rb_erase(&bss->rbn, &dev->bss_tree);
  481. spin_unlock_bh(&dev->bss_lock);
  482. kref_put(&bss->ref, bss_release);
  483. }
  484. EXPORT_SYMBOL(cfg80211_unlink_bss);
  485. #ifdef CONFIG_CFG80211_WEXT
  486. int cfg80211_wext_siwscan(struct net_device *dev,
  487. struct iw_request_info *info,
  488. union iwreq_data *wrqu, char *extra)
  489. {
  490. struct cfg80211_registered_device *rdev;
  491. struct wiphy *wiphy;
  492. struct iw_scan_req *wreq = NULL;
  493. struct cfg80211_scan_request *creq = NULL;
  494. int i, err, n_channels = 0;
  495. enum ieee80211_band band;
  496. if (!netif_running(dev))
  497. return -ENETDOWN;
  498. if (wrqu->data.length == sizeof(struct iw_scan_req))
  499. wreq = (struct iw_scan_req *)extra;
  500. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  501. if (IS_ERR(rdev))
  502. return PTR_ERR(rdev);
  503. if (rdev->scan_req) {
  504. err = -EBUSY;
  505. goto out;
  506. }
  507. wiphy = &rdev->wiphy;
  508. /* Determine number of channels, needed to allocate creq */
  509. if (wreq && wreq->num_channels)
  510. n_channels = wreq->num_channels;
  511. else {
  512. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  513. if (wiphy->bands[band])
  514. n_channels += wiphy->bands[band]->n_channels;
  515. }
  516. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  517. n_channels * sizeof(void *),
  518. GFP_ATOMIC);
  519. if (!creq) {
  520. err = -ENOMEM;
  521. goto out;
  522. }
  523. creq->wiphy = wiphy;
  524. creq->dev = dev;
  525. /* SSIDs come after channels */
  526. creq->ssids = (void *)&creq->channels[n_channels];
  527. creq->n_channels = n_channels;
  528. creq->n_ssids = 1;
  529. /* translate "Scan on frequencies" request */
  530. i = 0;
  531. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  532. int j;
  533. if (!wiphy->bands[band])
  534. continue;
  535. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  536. /* ignore disabled channels */
  537. if (wiphy->bands[band]->channels[j].flags &
  538. IEEE80211_CHAN_DISABLED)
  539. continue;
  540. /* If we have a wireless request structure and the
  541. * wireless request specifies frequencies, then search
  542. * for the matching hardware channel.
  543. */
  544. if (wreq && wreq->num_channels) {
  545. int k;
  546. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  547. for (k = 0; k < wreq->num_channels; k++) {
  548. int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
  549. if (wext_freq == wiphy_freq)
  550. goto wext_freq_found;
  551. }
  552. goto wext_freq_not_found;
  553. }
  554. wext_freq_found:
  555. creq->channels[i] = &wiphy->bands[band]->channels[j];
  556. i++;
  557. wext_freq_not_found: ;
  558. }
  559. }
  560. /* No channels found? */
  561. if (!i) {
  562. err = -EINVAL;
  563. goto out;
  564. }
  565. /* Set real number of channels specified in creq->channels[] */
  566. creq->n_channels = i;
  567. /* translate "Scan for SSID" request */
  568. if (wreq) {
  569. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  570. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  571. err = -EINVAL;
  572. goto out;
  573. }
  574. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  575. creq->ssids[0].ssid_len = wreq->essid_len;
  576. }
  577. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  578. creq->n_ssids = 0;
  579. }
  580. rdev->scan_req = creq;
  581. err = rdev->ops->scan(wiphy, dev, creq);
  582. if (err) {
  583. rdev->scan_req = NULL;
  584. /* creq will be freed below */
  585. } else {
  586. nl80211_send_scan_start(rdev, dev);
  587. /* creq now owned by driver */
  588. creq = NULL;
  589. dev_hold(dev);
  590. }
  591. out:
  592. kfree(creq);
  593. cfg80211_unlock_rdev(rdev);
  594. return err;
  595. }
  596. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  597. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  598. struct cfg80211_bss *bss,
  599. char **current_ev, char *end_buf)
  600. {
  601. u8 *pos, *end, *next;
  602. struct iw_event iwe;
  603. if (!bss->information_elements ||
  604. !bss->len_information_elements)
  605. return;
  606. /*
  607. * If needed, fragment the IEs buffer (at IE boundaries) into short
  608. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  609. */
  610. pos = bss->information_elements;
  611. end = pos + bss->len_information_elements;
  612. while (end - pos > IW_GENERIC_IE_MAX) {
  613. next = pos + 2 + pos[1];
  614. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  615. next = next + 2 + next[1];
  616. memset(&iwe, 0, sizeof(iwe));
  617. iwe.cmd = IWEVGENIE;
  618. iwe.u.data.length = next - pos;
  619. *current_ev = iwe_stream_add_point(info, *current_ev,
  620. end_buf, &iwe, pos);
  621. pos = next;
  622. }
  623. if (end > pos) {
  624. memset(&iwe, 0, sizeof(iwe));
  625. iwe.cmd = IWEVGENIE;
  626. iwe.u.data.length = end - pos;
  627. *current_ev = iwe_stream_add_point(info, *current_ev,
  628. end_buf, &iwe, pos);
  629. }
  630. }
  631. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  632. {
  633. unsigned long end = jiffies;
  634. if (end >= start)
  635. return jiffies_to_msecs(end - start);
  636. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  637. }
  638. static char *
  639. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  640. struct cfg80211_internal_bss *bss, char *current_ev,
  641. char *end_buf)
  642. {
  643. struct iw_event iwe;
  644. u8 *buf, *cfg, *p;
  645. u8 *ie = bss->pub.information_elements;
  646. int rem = bss->pub.len_information_elements, i, sig;
  647. bool ismesh = false;
  648. memset(&iwe, 0, sizeof(iwe));
  649. iwe.cmd = SIOCGIWAP;
  650. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  651. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  652. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  653. IW_EV_ADDR_LEN);
  654. memset(&iwe, 0, sizeof(iwe));
  655. iwe.cmd = SIOCGIWFREQ;
  656. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  657. iwe.u.freq.e = 0;
  658. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  659. IW_EV_FREQ_LEN);
  660. memset(&iwe, 0, sizeof(iwe));
  661. iwe.cmd = SIOCGIWFREQ;
  662. iwe.u.freq.m = bss->pub.channel->center_freq;
  663. iwe.u.freq.e = 6;
  664. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  665. IW_EV_FREQ_LEN);
  666. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  667. memset(&iwe, 0, sizeof(iwe));
  668. iwe.cmd = IWEVQUAL;
  669. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  670. IW_QUAL_NOISE_INVALID |
  671. IW_QUAL_QUAL_UPDATED;
  672. switch (wiphy->signal_type) {
  673. case CFG80211_SIGNAL_TYPE_MBM:
  674. sig = bss->pub.signal / 100;
  675. iwe.u.qual.level = sig;
  676. iwe.u.qual.updated |= IW_QUAL_DBM;
  677. if (sig < -110) /* rather bad */
  678. sig = -110;
  679. else if (sig > -40) /* perfect */
  680. sig = -40;
  681. /* will give a range of 0 .. 70 */
  682. iwe.u.qual.qual = sig + 110;
  683. break;
  684. case CFG80211_SIGNAL_TYPE_UNSPEC:
  685. iwe.u.qual.level = bss->pub.signal;
  686. /* will give range 0 .. 100 */
  687. iwe.u.qual.qual = bss->pub.signal;
  688. break;
  689. default:
  690. /* not reached */
  691. break;
  692. }
  693. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  694. &iwe, IW_EV_QUAL_LEN);
  695. }
  696. memset(&iwe, 0, sizeof(iwe));
  697. iwe.cmd = SIOCGIWENCODE;
  698. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  699. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  700. else
  701. iwe.u.data.flags = IW_ENCODE_DISABLED;
  702. iwe.u.data.length = 0;
  703. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  704. &iwe, "");
  705. while (rem >= 2) {
  706. /* invalid data */
  707. if (ie[1] > rem - 2)
  708. break;
  709. switch (ie[0]) {
  710. case WLAN_EID_SSID:
  711. memset(&iwe, 0, sizeof(iwe));
  712. iwe.cmd = SIOCGIWESSID;
  713. iwe.u.data.length = ie[1];
  714. iwe.u.data.flags = 1;
  715. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  716. &iwe, ie + 2);
  717. break;
  718. case WLAN_EID_MESH_ID:
  719. memset(&iwe, 0, sizeof(iwe));
  720. iwe.cmd = SIOCGIWESSID;
  721. iwe.u.data.length = ie[1];
  722. iwe.u.data.flags = 1;
  723. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  724. &iwe, ie + 2);
  725. break;
  726. case WLAN_EID_MESH_CONFIG:
  727. ismesh = true;
  728. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  729. break;
  730. buf = kmalloc(50, GFP_ATOMIC);
  731. if (!buf)
  732. break;
  733. cfg = ie + 2;
  734. memset(&iwe, 0, sizeof(iwe));
  735. iwe.cmd = IWEVCUSTOM;
  736. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  737. "0x%02X", cfg[0]);
  738. iwe.u.data.length = strlen(buf);
  739. current_ev = iwe_stream_add_point(info, current_ev,
  740. end_buf,
  741. &iwe, buf);
  742. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  743. cfg[1]);
  744. iwe.u.data.length = strlen(buf);
  745. current_ev = iwe_stream_add_point(info, current_ev,
  746. end_buf,
  747. &iwe, buf);
  748. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  749. cfg[2]);
  750. iwe.u.data.length = strlen(buf);
  751. current_ev = iwe_stream_add_point(info, current_ev,
  752. end_buf,
  753. &iwe, buf);
  754. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  755. iwe.u.data.length = strlen(buf);
  756. current_ev = iwe_stream_add_point(info, current_ev,
  757. end_buf,
  758. &iwe, buf);
  759. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  760. iwe.u.data.length = strlen(buf);
  761. current_ev = iwe_stream_add_point(info, current_ev,
  762. end_buf,
  763. &iwe, buf);
  764. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  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, "Capabilities: 0x%02X", cfg[6]);
  770. iwe.u.data.length = strlen(buf);
  771. current_ev = iwe_stream_add_point(info, current_ev,
  772. end_buf,
  773. &iwe, buf);
  774. kfree(buf);
  775. break;
  776. case WLAN_EID_SUPP_RATES:
  777. case WLAN_EID_EXT_SUPP_RATES:
  778. /* display all supported rates in readable format */
  779. p = current_ev + iwe_stream_lcp_len(info);
  780. memset(&iwe, 0, sizeof(iwe));
  781. iwe.cmd = SIOCGIWRATE;
  782. /* Those two flags are ignored... */
  783. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  784. for (i = 0; i < ie[1]; i++) {
  785. iwe.u.bitrate.value =
  786. ((ie[i + 2] & 0x7f) * 500000);
  787. p = iwe_stream_add_value(info, current_ev, p,
  788. end_buf, &iwe, IW_EV_PARAM_LEN);
  789. }
  790. current_ev = p;
  791. break;
  792. }
  793. rem -= ie[1] + 2;
  794. ie += ie[1] + 2;
  795. }
  796. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  797. ismesh) {
  798. memset(&iwe, 0, sizeof(iwe));
  799. iwe.cmd = SIOCGIWMODE;
  800. if (ismesh)
  801. iwe.u.mode = IW_MODE_MESH;
  802. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  803. iwe.u.mode = IW_MODE_MASTER;
  804. else
  805. iwe.u.mode = IW_MODE_ADHOC;
  806. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  807. &iwe, IW_EV_UINT_LEN);
  808. }
  809. buf = kmalloc(30, GFP_ATOMIC);
  810. if (buf) {
  811. memset(&iwe, 0, sizeof(iwe));
  812. iwe.cmd = IWEVCUSTOM;
  813. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  814. iwe.u.data.length = strlen(buf);
  815. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  816. &iwe, buf);
  817. memset(&iwe, 0, sizeof(iwe));
  818. iwe.cmd = IWEVCUSTOM;
  819. sprintf(buf, " Last beacon: %ums ago",
  820. elapsed_jiffies_msecs(bss->ts));
  821. iwe.u.data.length = strlen(buf);
  822. current_ev = iwe_stream_add_point(info, current_ev,
  823. end_buf, &iwe, buf);
  824. kfree(buf);
  825. }
  826. ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
  827. return current_ev;
  828. }
  829. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  830. struct iw_request_info *info,
  831. char *buf, size_t len)
  832. {
  833. char *current_ev = buf;
  834. char *end_buf = buf + len;
  835. struct cfg80211_internal_bss *bss;
  836. spin_lock_bh(&dev->bss_lock);
  837. cfg80211_bss_expire(dev);
  838. list_for_each_entry(bss, &dev->bss_list, list) {
  839. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  840. spin_unlock_bh(&dev->bss_lock);
  841. return -E2BIG;
  842. }
  843. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  844. current_ev, end_buf);
  845. }
  846. spin_unlock_bh(&dev->bss_lock);
  847. return current_ev - buf;
  848. }
  849. int cfg80211_wext_giwscan(struct net_device *dev,
  850. struct iw_request_info *info,
  851. struct iw_point *data, char *extra)
  852. {
  853. struct cfg80211_registered_device *rdev;
  854. int res;
  855. if (!netif_running(dev))
  856. return -ENETDOWN;
  857. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  858. if (IS_ERR(rdev))
  859. return PTR_ERR(rdev);
  860. if (rdev->scan_req) {
  861. res = -EAGAIN;
  862. goto out;
  863. }
  864. res = ieee80211_scan_results(rdev, info, extra, data->length);
  865. data->length = 0;
  866. if (res >= 0) {
  867. data->length = res;
  868. res = 0;
  869. }
  870. out:
  871. cfg80211_unlock_rdev(rdev);
  872. return res;
  873. }
  874. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  875. #endif