scan.c 25 KB

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