scan.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  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. /* No channels found? */
  556. if (!i) {
  557. err = -EINVAL;
  558. goto out;
  559. }
  560. /* Set real number of channels specified in creq->channels[] */
  561. creq->n_channels = i;
  562. /* translate "Scan for SSID" request */
  563. if (wreq) {
  564. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  565. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
  566. return -EINVAL;
  567. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  568. creq->ssids[0].ssid_len = wreq->essid_len;
  569. }
  570. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  571. creq->n_ssids = 0;
  572. }
  573. rdev->scan_req = creq;
  574. err = rdev->ops->scan(wiphy, dev, creq);
  575. if (err) {
  576. rdev->scan_req = NULL;
  577. kfree(creq);
  578. } else {
  579. nl80211_send_scan_start(rdev, dev);
  580. dev_hold(dev);
  581. }
  582. out:
  583. cfg80211_unlock_rdev(rdev);
  584. return err;
  585. }
  586. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  587. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  588. struct cfg80211_bss *bss,
  589. char **current_ev, char *end_buf)
  590. {
  591. u8 *pos, *end, *next;
  592. struct iw_event iwe;
  593. if (!bss->information_elements ||
  594. !bss->len_information_elements)
  595. return;
  596. /*
  597. * If needed, fragment the IEs buffer (at IE boundaries) into short
  598. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  599. */
  600. pos = bss->information_elements;
  601. end = pos + bss->len_information_elements;
  602. while (end - pos > IW_GENERIC_IE_MAX) {
  603. next = pos + 2 + pos[1];
  604. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  605. next = next + 2 + next[1];
  606. memset(&iwe, 0, sizeof(iwe));
  607. iwe.cmd = IWEVGENIE;
  608. iwe.u.data.length = next - pos;
  609. *current_ev = iwe_stream_add_point(info, *current_ev,
  610. end_buf, &iwe, pos);
  611. pos = next;
  612. }
  613. if (end > pos) {
  614. memset(&iwe, 0, sizeof(iwe));
  615. iwe.cmd = IWEVGENIE;
  616. iwe.u.data.length = end - pos;
  617. *current_ev = iwe_stream_add_point(info, *current_ev,
  618. end_buf, &iwe, pos);
  619. }
  620. }
  621. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  622. {
  623. unsigned long end = jiffies;
  624. if (end >= start)
  625. return jiffies_to_msecs(end - start);
  626. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  627. }
  628. static char *
  629. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  630. struct cfg80211_internal_bss *bss, char *current_ev,
  631. char *end_buf)
  632. {
  633. struct iw_event iwe;
  634. u8 *buf, *cfg, *p;
  635. u8 *ie = bss->pub.information_elements;
  636. int rem = bss->pub.len_information_elements, i, sig;
  637. bool ismesh = false;
  638. memset(&iwe, 0, sizeof(iwe));
  639. iwe.cmd = SIOCGIWAP;
  640. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  641. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  642. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  643. IW_EV_ADDR_LEN);
  644. memset(&iwe, 0, sizeof(iwe));
  645. iwe.cmd = SIOCGIWFREQ;
  646. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  647. iwe.u.freq.e = 0;
  648. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  649. IW_EV_FREQ_LEN);
  650. memset(&iwe, 0, sizeof(iwe));
  651. iwe.cmd = SIOCGIWFREQ;
  652. iwe.u.freq.m = bss->pub.channel->center_freq;
  653. iwe.u.freq.e = 6;
  654. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  655. IW_EV_FREQ_LEN);
  656. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  657. memset(&iwe, 0, sizeof(iwe));
  658. iwe.cmd = IWEVQUAL;
  659. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  660. IW_QUAL_NOISE_INVALID |
  661. IW_QUAL_QUAL_UPDATED;
  662. switch (wiphy->signal_type) {
  663. case CFG80211_SIGNAL_TYPE_MBM:
  664. sig = bss->pub.signal / 100;
  665. iwe.u.qual.level = sig;
  666. iwe.u.qual.updated |= IW_QUAL_DBM;
  667. if (sig < -110) /* rather bad */
  668. sig = -110;
  669. else if (sig > -40) /* perfect */
  670. sig = -40;
  671. /* will give a range of 0 .. 70 */
  672. iwe.u.qual.qual = sig + 110;
  673. break;
  674. case CFG80211_SIGNAL_TYPE_UNSPEC:
  675. iwe.u.qual.level = bss->pub.signal;
  676. /* will give range 0 .. 100 */
  677. iwe.u.qual.qual = bss->pub.signal;
  678. break;
  679. default:
  680. /* not reached */
  681. break;
  682. }
  683. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  684. &iwe, IW_EV_QUAL_LEN);
  685. }
  686. memset(&iwe, 0, sizeof(iwe));
  687. iwe.cmd = SIOCGIWENCODE;
  688. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  689. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  690. else
  691. iwe.u.data.flags = IW_ENCODE_DISABLED;
  692. iwe.u.data.length = 0;
  693. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  694. &iwe, "");
  695. while (rem >= 2) {
  696. /* invalid data */
  697. if (ie[1] > rem - 2)
  698. break;
  699. switch (ie[0]) {
  700. case WLAN_EID_SSID:
  701. memset(&iwe, 0, sizeof(iwe));
  702. iwe.cmd = SIOCGIWESSID;
  703. iwe.u.data.length = ie[1];
  704. iwe.u.data.flags = 1;
  705. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  706. &iwe, ie + 2);
  707. break;
  708. case WLAN_EID_MESH_ID:
  709. memset(&iwe, 0, sizeof(iwe));
  710. iwe.cmd = SIOCGIWESSID;
  711. iwe.u.data.length = ie[1];
  712. iwe.u.data.flags = 1;
  713. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  714. &iwe, ie + 2);
  715. break;
  716. case WLAN_EID_MESH_CONFIG:
  717. ismesh = true;
  718. if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
  719. break;
  720. buf = kmalloc(50, GFP_ATOMIC);
  721. if (!buf)
  722. break;
  723. cfg = ie + 2;
  724. memset(&iwe, 0, sizeof(iwe));
  725. iwe.cmd = IWEVCUSTOM;
  726. sprintf(buf, "Mesh network (version %d)", cfg[0]);
  727. iwe.u.data.length = strlen(buf);
  728. current_ev = iwe_stream_add_point(info, current_ev,
  729. end_buf,
  730. &iwe, buf);
  731. sprintf(buf, "Path Selection Protocol ID: "
  732. "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
  733. cfg[4]);
  734. iwe.u.data.length = strlen(buf);
  735. current_ev = iwe_stream_add_point(info, current_ev,
  736. end_buf,
  737. &iwe, buf);
  738. sprintf(buf, "Path Selection Metric ID: "
  739. "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
  740. cfg[8]);
  741. iwe.u.data.length = strlen(buf);
  742. current_ev = iwe_stream_add_point(info, current_ev,
  743. end_buf,
  744. &iwe, buf);
  745. sprintf(buf, "Congestion Control Mode ID: "
  746. "0x%02X%02X%02X%02X", cfg[9], cfg[10],
  747. cfg[11], cfg[12]);
  748. iwe.u.data.length = strlen(buf);
  749. current_ev = iwe_stream_add_point(info, current_ev,
  750. end_buf,
  751. &iwe, buf);
  752. sprintf(buf, "Channel Precedence: "
  753. "0x%02X%02X%02X%02X", cfg[13], cfg[14],
  754. cfg[15], cfg[16]);
  755. iwe.u.data.length = strlen(buf);
  756. current_ev = iwe_stream_add_point(info, current_ev,
  757. end_buf,
  758. &iwe, buf);
  759. kfree(buf);
  760. break;
  761. case WLAN_EID_SUPP_RATES:
  762. case WLAN_EID_EXT_SUPP_RATES:
  763. /* display all supported rates in readable format */
  764. p = current_ev + iwe_stream_lcp_len(info);
  765. memset(&iwe, 0, sizeof(iwe));
  766. iwe.cmd = SIOCGIWRATE;
  767. /* Those two flags are ignored... */
  768. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  769. for (i = 0; i < ie[1]; i++) {
  770. iwe.u.bitrate.value =
  771. ((ie[i + 2] & 0x7f) * 500000);
  772. p = iwe_stream_add_value(info, current_ev, p,
  773. end_buf, &iwe, IW_EV_PARAM_LEN);
  774. }
  775. current_ev = p;
  776. break;
  777. }
  778. rem -= ie[1] + 2;
  779. ie += ie[1] + 2;
  780. }
  781. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
  782. || ismesh) {
  783. memset(&iwe, 0, sizeof(iwe));
  784. iwe.cmd = SIOCGIWMODE;
  785. if (ismesh)
  786. iwe.u.mode = IW_MODE_MESH;
  787. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  788. iwe.u.mode = IW_MODE_MASTER;
  789. else
  790. iwe.u.mode = IW_MODE_ADHOC;
  791. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  792. &iwe, IW_EV_UINT_LEN);
  793. }
  794. buf = kmalloc(30, GFP_ATOMIC);
  795. if (buf) {
  796. memset(&iwe, 0, sizeof(iwe));
  797. iwe.cmd = IWEVCUSTOM;
  798. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  799. iwe.u.data.length = strlen(buf);
  800. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  801. &iwe, buf);
  802. memset(&iwe, 0, sizeof(iwe));
  803. iwe.cmd = IWEVCUSTOM;
  804. sprintf(buf, " Last beacon: %ums ago",
  805. elapsed_jiffies_msecs(bss->ts));
  806. iwe.u.data.length = strlen(buf);
  807. current_ev = iwe_stream_add_point(info, current_ev,
  808. end_buf, &iwe, buf);
  809. kfree(buf);
  810. }
  811. ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
  812. return current_ev;
  813. }
  814. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  815. struct iw_request_info *info,
  816. char *buf, size_t len)
  817. {
  818. char *current_ev = buf;
  819. char *end_buf = buf + len;
  820. struct cfg80211_internal_bss *bss;
  821. spin_lock_bh(&dev->bss_lock);
  822. cfg80211_bss_expire(dev);
  823. list_for_each_entry(bss, &dev->bss_list, list) {
  824. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  825. spin_unlock_bh(&dev->bss_lock);
  826. return -E2BIG;
  827. }
  828. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  829. current_ev, end_buf);
  830. }
  831. spin_unlock_bh(&dev->bss_lock);
  832. return current_ev - buf;
  833. }
  834. int cfg80211_wext_giwscan(struct net_device *dev,
  835. struct iw_request_info *info,
  836. struct iw_point *data, char *extra)
  837. {
  838. struct cfg80211_registered_device *rdev;
  839. int res;
  840. if (!netif_running(dev))
  841. return -ENETDOWN;
  842. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  843. if (IS_ERR(rdev))
  844. return PTR_ERR(rdev);
  845. if (rdev->scan_req) {
  846. res = -EAGAIN;
  847. goto out;
  848. }
  849. res = ieee80211_scan_results(rdev, info, extra, data->length);
  850. data->length = 0;
  851. if (res >= 0) {
  852. data->length = res;
  853. res = 0;
  854. }
  855. out:
  856. cfg80211_unlock_rdev(rdev);
  857. return res;
  858. }
  859. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  860. #endif