scan.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313
  1. /*
  2. * cfg80211 scan result handling
  3. *
  4. * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/wireless.h>
  11. #include <linux/nl80211.h>
  12. #include <linux/etherdevice.h>
  13. #include <net/arp.h>
  14. #include <net/cfg80211.h>
  15. #include <net/cfg80211-wext.h>
  16. #include <net/iw_handler.h>
  17. #include "core.h"
  18. #include "nl80211.h"
  19. #include "wext-compat.h"
  20. #include "rdev-ops.h"
  21. #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
  22. static void bss_release(struct kref *ref)
  23. {
  24. struct cfg80211_bss_ies *ies;
  25. struct cfg80211_internal_bss *bss;
  26. bss = container_of(ref, struct cfg80211_internal_bss, ref);
  27. if (WARN_ON(atomic_read(&bss->hold)))
  28. return;
  29. ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
  30. if (ies)
  31. kfree_rcu(ies, rcu_head);
  32. ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
  33. if (ies)
  34. kfree_rcu(ies, rcu_head);
  35. kfree(bss);
  36. }
  37. /* must hold dev->bss_lock! */
  38. static void __cfg80211_unlink_bss(struct cfg80211_registered_device *dev,
  39. struct cfg80211_internal_bss *bss)
  40. {
  41. list_del_init(&bss->list);
  42. rb_erase(&bss->rbn, &dev->bss_tree);
  43. kref_put(&bss->ref, bss_release);
  44. }
  45. /* must hold dev->bss_lock! */
  46. static void __cfg80211_bss_expire(struct cfg80211_registered_device *dev,
  47. unsigned long expire_time)
  48. {
  49. struct cfg80211_internal_bss *bss, *tmp;
  50. bool expired = false;
  51. list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
  52. if (atomic_read(&bss->hold))
  53. continue;
  54. if (!time_after(expire_time, bss->ts))
  55. continue;
  56. __cfg80211_unlink_bss(dev, bss);
  57. expired = true;
  58. }
  59. if (expired)
  60. dev->bss_generation++;
  61. }
  62. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
  63. {
  64. struct cfg80211_scan_request *request;
  65. struct wireless_dev *wdev;
  66. #ifdef CONFIG_CFG80211_WEXT
  67. union iwreq_data wrqu;
  68. #endif
  69. ASSERT_RDEV_LOCK(rdev);
  70. request = rdev->scan_req;
  71. if (!request)
  72. return;
  73. wdev = request->wdev;
  74. /*
  75. * This must be before sending the other events!
  76. * Otherwise, wpa_supplicant gets completely confused with
  77. * wext events.
  78. */
  79. if (wdev->netdev)
  80. cfg80211_sme_scan_done(wdev->netdev);
  81. if (request->aborted) {
  82. nl80211_send_scan_aborted(rdev, wdev);
  83. } else {
  84. if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
  85. /* flush entries from previous scans */
  86. spin_lock_bh(&rdev->bss_lock);
  87. __cfg80211_bss_expire(rdev, request->scan_start);
  88. spin_unlock_bh(&rdev->bss_lock);
  89. }
  90. nl80211_send_scan_done(rdev, wdev);
  91. }
  92. #ifdef CONFIG_CFG80211_WEXT
  93. if (wdev->netdev && !request->aborted) {
  94. memset(&wrqu, 0, sizeof(wrqu));
  95. wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
  96. }
  97. #endif
  98. if (wdev->netdev)
  99. dev_put(wdev->netdev);
  100. rdev->scan_req = NULL;
  101. /*
  102. * OK. If this is invoked with "leak" then we can't
  103. * free this ... but we've cleaned it up anyway. The
  104. * driver failed to call the scan_done callback, so
  105. * all bets are off, it might still be trying to use
  106. * the scan request or not ... if it accesses the dev
  107. * in there (it shouldn't anyway) then it may crash.
  108. */
  109. if (!leak)
  110. kfree(request);
  111. }
  112. void __cfg80211_scan_done(struct work_struct *wk)
  113. {
  114. struct cfg80211_registered_device *rdev;
  115. rdev = container_of(wk, struct cfg80211_registered_device,
  116. scan_done_wk);
  117. cfg80211_lock_rdev(rdev);
  118. ___cfg80211_scan_done(rdev, false);
  119. cfg80211_unlock_rdev(rdev);
  120. }
  121. void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
  122. {
  123. trace_cfg80211_scan_done(request, aborted);
  124. WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
  125. request->aborted = aborted;
  126. queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
  127. }
  128. EXPORT_SYMBOL(cfg80211_scan_done);
  129. void __cfg80211_sched_scan_results(struct work_struct *wk)
  130. {
  131. struct cfg80211_registered_device *rdev;
  132. struct cfg80211_sched_scan_request *request;
  133. rdev = container_of(wk, struct cfg80211_registered_device,
  134. sched_scan_results_wk);
  135. request = rdev->sched_scan_req;
  136. mutex_lock(&rdev->sched_scan_mtx);
  137. /* we don't have sched_scan_req anymore if the scan is stopping */
  138. if (request) {
  139. if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
  140. /* flush entries from previous scans */
  141. spin_lock_bh(&rdev->bss_lock);
  142. __cfg80211_bss_expire(rdev, request->scan_start);
  143. spin_unlock_bh(&rdev->bss_lock);
  144. request->scan_start =
  145. jiffies + msecs_to_jiffies(request->interval);
  146. }
  147. nl80211_send_sched_scan_results(rdev, request->dev);
  148. }
  149. mutex_unlock(&rdev->sched_scan_mtx);
  150. }
  151. void cfg80211_sched_scan_results(struct wiphy *wiphy)
  152. {
  153. trace_cfg80211_sched_scan_results(wiphy);
  154. /* ignore if we're not scanning */
  155. if (wiphy_to_dev(wiphy)->sched_scan_req)
  156. queue_work(cfg80211_wq,
  157. &wiphy_to_dev(wiphy)->sched_scan_results_wk);
  158. }
  159. EXPORT_SYMBOL(cfg80211_sched_scan_results);
  160. void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
  161. {
  162. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  163. trace_cfg80211_sched_scan_stopped(wiphy);
  164. mutex_lock(&rdev->sched_scan_mtx);
  165. __cfg80211_stop_sched_scan(rdev, true);
  166. mutex_unlock(&rdev->sched_scan_mtx);
  167. }
  168. EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
  169. int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
  170. bool driver_initiated)
  171. {
  172. struct net_device *dev;
  173. lockdep_assert_held(&rdev->sched_scan_mtx);
  174. if (!rdev->sched_scan_req)
  175. return -ENOENT;
  176. dev = rdev->sched_scan_req->dev;
  177. if (!driver_initiated) {
  178. int err = rdev_sched_scan_stop(rdev, dev);
  179. if (err)
  180. return err;
  181. }
  182. nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
  183. kfree(rdev->sched_scan_req);
  184. rdev->sched_scan_req = NULL;
  185. return 0;
  186. }
  187. /* must hold dev->bss_lock! */
  188. void cfg80211_bss_age(struct cfg80211_registered_device *dev,
  189. unsigned long age_secs)
  190. {
  191. struct cfg80211_internal_bss *bss;
  192. unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
  193. list_for_each_entry(bss, &dev->bss_list, list)
  194. bss->ts -= age_jiffies;
  195. }
  196. void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
  197. {
  198. __cfg80211_bss_expire(dev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
  199. }
  200. const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
  201. {
  202. while (len > 2 && ies[0] != eid) {
  203. len -= ies[1] + 2;
  204. ies += ies[1] + 2;
  205. }
  206. if (len < 2)
  207. return NULL;
  208. if (len < 2 + ies[1])
  209. return NULL;
  210. return ies;
  211. }
  212. EXPORT_SYMBOL(cfg80211_find_ie);
  213. const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type,
  214. const u8 *ies, int len)
  215. {
  216. struct ieee80211_vendor_ie *ie;
  217. const u8 *pos = ies, *end = ies + len;
  218. int ie_oui;
  219. while (pos < end) {
  220. pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos,
  221. end - pos);
  222. if (!pos)
  223. return NULL;
  224. if (end - pos < sizeof(*ie))
  225. return NULL;
  226. ie = (struct ieee80211_vendor_ie *)pos;
  227. ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2];
  228. if (ie_oui == oui && ie->oui_type == oui_type)
  229. return pos;
  230. pos += 2 + ie->len;
  231. }
  232. return NULL;
  233. }
  234. EXPORT_SYMBOL(cfg80211_find_vendor_ie);
  235. static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
  236. const u8 *ssid, size_t ssid_len)
  237. {
  238. const struct cfg80211_bss_ies *ies;
  239. const u8 *ssidie;
  240. if (bssid && !ether_addr_equal(a->bssid, bssid))
  241. return false;
  242. if (!ssid)
  243. return true;
  244. ies = rcu_access_pointer(a->ies);
  245. if (!ies)
  246. return false;
  247. ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  248. if (!ssidie)
  249. return false;
  250. if (ssidie[1] != ssid_len)
  251. return false;
  252. return memcmp(ssidie + 2, ssid, ssid_len) == 0;
  253. }
  254. /**
  255. * enum bss_compare_mode - BSS compare mode
  256. * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
  257. * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
  258. * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
  259. */
  260. enum bss_compare_mode {
  261. BSS_CMP_REGULAR,
  262. BSS_CMP_HIDE_ZLEN,
  263. BSS_CMP_HIDE_NUL,
  264. };
  265. static int cmp_bss(struct cfg80211_bss *a,
  266. struct cfg80211_bss *b,
  267. enum bss_compare_mode mode)
  268. {
  269. const struct cfg80211_bss_ies *a_ies, *b_ies;
  270. const u8 *ie1 = NULL;
  271. const u8 *ie2 = NULL;
  272. int i, r;
  273. if (a->channel != b->channel)
  274. return b->channel->center_freq - a->channel->center_freq;
  275. a_ies = rcu_access_pointer(a->ies);
  276. if (!a_ies)
  277. return -1;
  278. b_ies = rcu_access_pointer(b->ies);
  279. if (!b_ies)
  280. return 1;
  281. if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
  282. ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  283. a_ies->data, a_ies->len);
  284. if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
  285. ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  286. b_ies->data, b_ies->len);
  287. if (ie1 && ie2) {
  288. int mesh_id_cmp;
  289. if (ie1[1] == ie2[1])
  290. mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  291. else
  292. mesh_id_cmp = ie2[1] - ie1[1];
  293. ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  294. a_ies->data, a_ies->len);
  295. ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  296. b_ies->data, b_ies->len);
  297. if (ie1 && ie2) {
  298. if (mesh_id_cmp)
  299. return mesh_id_cmp;
  300. if (ie1[1] != ie2[1])
  301. return ie2[1] - ie1[1];
  302. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  303. }
  304. }
  305. /*
  306. * we can't use compare_ether_addr here since we need a < > operator.
  307. * The binary return value of compare_ether_addr isn't enough
  308. */
  309. r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
  310. if (r)
  311. return r;
  312. ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
  313. ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
  314. if (!ie1 && !ie2)
  315. return 0;
  316. /*
  317. * Note that with "hide_ssid", the function returns a match if
  318. * the already-present BSS ("b") is a hidden SSID beacon for
  319. * the new BSS ("a").
  320. */
  321. /* sort missing IE before (left of) present IE */
  322. if (!ie1)
  323. return -1;
  324. if (!ie2)
  325. return 1;
  326. switch (mode) {
  327. case BSS_CMP_HIDE_ZLEN:
  328. /*
  329. * In ZLEN mode we assume the BSS entry we're
  330. * looking for has a zero-length SSID. So if
  331. * the one we're looking at right now has that,
  332. * return 0. Otherwise, return the difference
  333. * in length, but since we're looking for the
  334. * 0-length it's really equivalent to returning
  335. * the length of the one we're looking at.
  336. *
  337. * No content comparison is needed as we assume
  338. * the content length is zero.
  339. */
  340. return ie2[1];
  341. case BSS_CMP_REGULAR:
  342. default:
  343. /* sort by length first, then by contents */
  344. if (ie1[1] != ie2[1])
  345. return ie2[1] - ie1[1];
  346. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  347. case BSS_CMP_HIDE_NUL:
  348. if (ie1[1] != ie2[1])
  349. return ie2[1] - ie1[1];
  350. /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
  351. for (i = 0; i < ie2[1]; i++)
  352. if (ie2[i + 2])
  353. return -1;
  354. return 0;
  355. }
  356. }
  357. struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
  358. struct ieee80211_channel *channel,
  359. const u8 *bssid,
  360. const u8 *ssid, size_t ssid_len,
  361. u16 capa_mask, u16 capa_val)
  362. {
  363. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  364. struct cfg80211_internal_bss *bss, *res = NULL;
  365. unsigned long now = jiffies;
  366. trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask,
  367. capa_val);
  368. spin_lock_bh(&dev->bss_lock);
  369. list_for_each_entry(bss, &dev->bss_list, list) {
  370. if ((bss->pub.capability & capa_mask) != capa_val)
  371. continue;
  372. if (channel && bss->pub.channel != channel)
  373. continue;
  374. /* Don't get expired BSS structs */
  375. if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
  376. !atomic_read(&bss->hold))
  377. continue;
  378. if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
  379. res = bss;
  380. kref_get(&res->ref);
  381. break;
  382. }
  383. }
  384. spin_unlock_bh(&dev->bss_lock);
  385. if (!res)
  386. return NULL;
  387. trace_cfg80211_return_bss(&res->pub);
  388. return &res->pub;
  389. }
  390. EXPORT_SYMBOL(cfg80211_get_bss);
  391. static void rb_insert_bss(struct cfg80211_registered_device *dev,
  392. struct cfg80211_internal_bss *bss)
  393. {
  394. struct rb_node **p = &dev->bss_tree.rb_node;
  395. struct rb_node *parent = NULL;
  396. struct cfg80211_internal_bss *tbss;
  397. int cmp;
  398. while (*p) {
  399. parent = *p;
  400. tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
  401. cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
  402. if (WARN_ON(!cmp)) {
  403. /* will sort of leak this BSS */
  404. return;
  405. }
  406. if (cmp < 0)
  407. p = &(*p)->rb_left;
  408. else
  409. p = &(*p)->rb_right;
  410. }
  411. rb_link_node(&bss->rbn, parent, p);
  412. rb_insert_color(&bss->rbn, &dev->bss_tree);
  413. }
  414. static struct cfg80211_internal_bss *
  415. rb_find_bss(struct cfg80211_registered_device *dev,
  416. struct cfg80211_internal_bss *res,
  417. enum bss_compare_mode mode)
  418. {
  419. struct rb_node *n = dev->bss_tree.rb_node;
  420. struct cfg80211_internal_bss *bss;
  421. int r;
  422. while (n) {
  423. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  424. r = cmp_bss(&res->pub, &bss->pub, mode);
  425. if (r == 0)
  426. return bss;
  427. else if (r < 0)
  428. n = n->rb_left;
  429. else
  430. n = n->rb_right;
  431. }
  432. return NULL;
  433. }
  434. static void
  435. copy_hidden_ies(struct cfg80211_internal_bss *res,
  436. struct cfg80211_internal_bss *hidden)
  437. {
  438. const struct cfg80211_bss_ies *ies;
  439. if (rcu_access_pointer(res->pub.beacon_ies))
  440. return;
  441. ies = rcu_access_pointer(hidden->pub.beacon_ies);
  442. if (WARN_ON(!ies))
  443. return;
  444. ies = kmemdup(ies, sizeof(*ies) + ies->len, GFP_ATOMIC);
  445. if (unlikely(!ies))
  446. return;
  447. rcu_assign_pointer(res->pub.beacon_ies, ies);
  448. }
  449. static struct cfg80211_internal_bss *
  450. cfg80211_bss_update(struct cfg80211_registered_device *dev,
  451. struct cfg80211_internal_bss *tmp)
  452. {
  453. struct cfg80211_internal_bss *found = NULL;
  454. if (WARN_ON(!tmp->pub.channel))
  455. return NULL;
  456. tmp->ts = jiffies;
  457. spin_lock_bh(&dev->bss_lock);
  458. if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
  459. spin_unlock_bh(&dev->bss_lock);
  460. return NULL;
  461. }
  462. found = rb_find_bss(dev, tmp, BSS_CMP_REGULAR);
  463. if (found) {
  464. found->pub.beacon_interval = tmp->pub.beacon_interval;
  465. found->pub.tsf = tmp->pub.tsf;
  466. found->pub.signal = tmp->pub.signal;
  467. found->pub.capability = tmp->pub.capability;
  468. found->ts = tmp->ts;
  469. /* Update IEs */
  470. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  471. const struct cfg80211_bss_ies *old;
  472. old = rcu_access_pointer(found->pub.proberesp_ies);
  473. rcu_assign_pointer(found->pub.proberesp_ies,
  474. tmp->pub.proberesp_ies);
  475. /* Override possible earlier Beacon frame IEs */
  476. rcu_assign_pointer(found->pub.ies,
  477. tmp->pub.proberesp_ies);
  478. if (old)
  479. kfree_rcu((struct cfg80211_bss_ies *)old,
  480. rcu_head);
  481. } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
  482. const struct cfg80211_bss_ies *old, *ies;
  483. old = rcu_access_pointer(found->pub.beacon_ies);
  484. ies = rcu_access_pointer(found->pub.ies);
  485. rcu_assign_pointer(found->pub.beacon_ies,
  486. tmp->pub.beacon_ies);
  487. /* Override IEs if they were from a beacon before */
  488. if (old == ies)
  489. rcu_assign_pointer(found->pub.ies,
  490. tmp->pub.beacon_ies);
  491. if (old)
  492. kfree_rcu((struct cfg80211_bss_ies *)old,
  493. rcu_head);
  494. }
  495. } else {
  496. struct cfg80211_internal_bss *new;
  497. struct cfg80211_internal_bss *hidden;
  498. struct cfg80211_bss_ies *ies;
  499. /* First check if the beacon is a probe response from
  500. * a hidden bss. If so, copy beacon ies (with nullified
  501. * ssid) into the probe response bss entry (with real ssid).
  502. * It is required basically for PSM implementation
  503. * (probe responses do not contain tim ie) */
  504. /* TODO: The code is not trying to update existing probe
  505. * response bss entries when beacon ies are
  506. * getting changed. */
  507. hidden = rb_find_bss(dev, tmp, BSS_CMP_HIDE_ZLEN);
  508. if (hidden) {
  509. copy_hidden_ies(tmp, hidden);
  510. } else {
  511. hidden = rb_find_bss(dev, tmp, BSS_CMP_HIDE_NUL);
  512. if (hidden)
  513. copy_hidden_ies(tmp, hidden);
  514. }
  515. /*
  516. * create a copy -- the "res" variable that is passed in
  517. * is allocated on the stack since it's not needed in the
  518. * more common case of an update
  519. */
  520. new = kzalloc(sizeof(*new) + dev->wiphy.bss_priv_size,
  521. GFP_ATOMIC);
  522. if (!new) {
  523. ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
  524. if (ies)
  525. kfree_rcu(ies, rcu_head);
  526. ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
  527. if (ies)
  528. kfree_rcu(ies, rcu_head);
  529. spin_unlock_bh(&dev->bss_lock);
  530. return NULL;
  531. }
  532. memcpy(new, tmp, sizeof(*new));
  533. kref_init(&new->ref);
  534. list_add_tail(&new->list, &dev->bss_list);
  535. rb_insert_bss(dev, new);
  536. found = new;
  537. }
  538. dev->bss_generation++;
  539. spin_unlock_bh(&dev->bss_lock);
  540. kref_get(&found->ref);
  541. return found;
  542. }
  543. static struct ieee80211_channel *
  544. cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
  545. struct ieee80211_channel *channel)
  546. {
  547. const u8 *tmp;
  548. u32 freq;
  549. int channel_number = -1;
  550. tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
  551. if (tmp && tmp[1] == 1) {
  552. channel_number = tmp[2];
  553. } else {
  554. tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
  555. if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
  556. struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
  557. channel_number = htop->primary_chan;
  558. }
  559. }
  560. if (channel_number < 0)
  561. return channel;
  562. freq = ieee80211_channel_to_frequency(channel_number, channel->band);
  563. channel = ieee80211_get_channel(wiphy, freq);
  564. if (!channel)
  565. return NULL;
  566. if (channel->flags & IEEE80211_CHAN_DISABLED)
  567. return NULL;
  568. return channel;
  569. }
  570. struct cfg80211_bss*
  571. cfg80211_inform_bss(struct wiphy *wiphy,
  572. struct ieee80211_channel *channel,
  573. const u8 *bssid, u64 tsf, u16 capability,
  574. u16 beacon_interval, const u8 *ie, size_t ielen,
  575. s32 signal, gfp_t gfp)
  576. {
  577. struct cfg80211_bss_ies *ies;
  578. struct cfg80211_internal_bss tmp = {}, *res;
  579. if (WARN_ON(!wiphy))
  580. return NULL;
  581. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  582. (signal < 0 || signal > 100)))
  583. return NULL;
  584. channel = cfg80211_get_bss_channel(wiphy, ie, ielen, channel);
  585. if (!channel)
  586. return NULL;
  587. memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
  588. tmp.pub.channel = channel;
  589. tmp.pub.signal = signal;
  590. tmp.pub.tsf = tsf;
  591. tmp.pub.beacon_interval = beacon_interval;
  592. tmp.pub.capability = capability;
  593. /*
  594. * Since we do not know here whether the IEs are from a Beacon or Probe
  595. * Response frame, we need to pick one of the options and only use it
  596. * with the driver that does not provide the full Beacon/Probe Response
  597. * frame. Use Beacon frame pointer to avoid indicating that this should
  598. * override the IEs pointer should we have received an earlier
  599. * indication of Probe Response data.
  600. */
  601. ies = kmalloc(sizeof(*ies) + ielen, gfp);
  602. if (!ies)
  603. return NULL;
  604. ies->len = ielen;
  605. memcpy(ies->data, ie, ielen);
  606. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  607. rcu_assign_pointer(tmp.pub.ies, ies);
  608. res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
  609. if (!res)
  610. return NULL;
  611. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  612. regulatory_hint_found_beacon(wiphy, channel, gfp);
  613. trace_cfg80211_return_bss(&res->pub);
  614. /* cfg80211_bss_update gives us a referenced result */
  615. return &res->pub;
  616. }
  617. EXPORT_SYMBOL(cfg80211_inform_bss);
  618. struct cfg80211_bss *
  619. cfg80211_inform_bss_frame(struct wiphy *wiphy,
  620. struct ieee80211_channel *channel,
  621. struct ieee80211_mgmt *mgmt, size_t len,
  622. s32 signal, gfp_t gfp)
  623. {
  624. struct cfg80211_internal_bss tmp = {}, *res;
  625. struct cfg80211_bss_ies *ies;
  626. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  627. u.probe_resp.variable);
  628. BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
  629. offsetof(struct ieee80211_mgmt, u.beacon.variable));
  630. trace_cfg80211_inform_bss_frame(wiphy, channel, mgmt, len, signal);
  631. if (WARN_ON(!mgmt))
  632. return NULL;
  633. if (WARN_ON(!wiphy))
  634. return NULL;
  635. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  636. (signal < 0 || signal > 100)))
  637. return NULL;
  638. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  639. return NULL;
  640. channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
  641. ielen, channel);
  642. if (!channel)
  643. return NULL;
  644. ies = kmalloc(sizeof(*ies) + ielen, gfp);
  645. if (!ies)
  646. return NULL;
  647. ies->len = ielen;
  648. memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
  649. if (ieee80211_is_probe_resp(mgmt->frame_control))
  650. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  651. else
  652. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  653. rcu_assign_pointer(tmp.pub.ies, ies);
  654. memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
  655. tmp.pub.channel = channel;
  656. tmp.pub.signal = signal;
  657. tmp.pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  658. tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  659. tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  660. res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
  661. if (!res)
  662. return NULL;
  663. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  664. regulatory_hint_found_beacon(wiphy, channel, gfp);
  665. trace_cfg80211_return_bss(&res->pub);
  666. /* cfg80211_bss_update gives us a referenced result */
  667. return &res->pub;
  668. }
  669. EXPORT_SYMBOL(cfg80211_inform_bss_frame);
  670. void cfg80211_ref_bss(struct cfg80211_bss *pub)
  671. {
  672. struct cfg80211_internal_bss *bss;
  673. if (!pub)
  674. return;
  675. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  676. kref_get(&bss->ref);
  677. }
  678. EXPORT_SYMBOL(cfg80211_ref_bss);
  679. void cfg80211_put_bss(struct cfg80211_bss *pub)
  680. {
  681. struct cfg80211_internal_bss *bss;
  682. if (!pub)
  683. return;
  684. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  685. kref_put(&bss->ref, bss_release);
  686. }
  687. EXPORT_SYMBOL(cfg80211_put_bss);
  688. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  689. {
  690. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  691. struct cfg80211_internal_bss *bss;
  692. if (WARN_ON(!pub))
  693. return;
  694. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  695. spin_lock_bh(&dev->bss_lock);
  696. if (!list_empty(&bss->list)) {
  697. __cfg80211_unlink_bss(dev, bss);
  698. dev->bss_generation++;
  699. }
  700. spin_unlock_bh(&dev->bss_lock);
  701. }
  702. EXPORT_SYMBOL(cfg80211_unlink_bss);
  703. #ifdef CONFIG_CFG80211_WEXT
  704. int cfg80211_wext_siwscan(struct net_device *dev,
  705. struct iw_request_info *info,
  706. union iwreq_data *wrqu, char *extra)
  707. {
  708. struct cfg80211_registered_device *rdev;
  709. struct wiphy *wiphy;
  710. struct iw_scan_req *wreq = NULL;
  711. struct cfg80211_scan_request *creq = NULL;
  712. int i, err, n_channels = 0;
  713. enum ieee80211_band band;
  714. if (!netif_running(dev))
  715. return -ENETDOWN;
  716. if (wrqu->data.length == sizeof(struct iw_scan_req))
  717. wreq = (struct iw_scan_req *)extra;
  718. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  719. if (IS_ERR(rdev))
  720. return PTR_ERR(rdev);
  721. if (rdev->scan_req) {
  722. err = -EBUSY;
  723. goto out;
  724. }
  725. wiphy = &rdev->wiphy;
  726. /* Determine number of channels, needed to allocate creq */
  727. if (wreq && wreq->num_channels)
  728. n_channels = wreq->num_channels;
  729. else {
  730. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  731. if (wiphy->bands[band])
  732. n_channels += wiphy->bands[band]->n_channels;
  733. }
  734. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  735. n_channels * sizeof(void *),
  736. GFP_ATOMIC);
  737. if (!creq) {
  738. err = -ENOMEM;
  739. goto out;
  740. }
  741. creq->wiphy = wiphy;
  742. creq->wdev = dev->ieee80211_ptr;
  743. /* SSIDs come after channels */
  744. creq->ssids = (void *)&creq->channels[n_channels];
  745. creq->n_channels = n_channels;
  746. creq->n_ssids = 1;
  747. creq->scan_start = jiffies;
  748. /* translate "Scan on frequencies" request */
  749. i = 0;
  750. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  751. int j;
  752. if (!wiphy->bands[band])
  753. continue;
  754. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  755. /* ignore disabled channels */
  756. if (wiphy->bands[band]->channels[j].flags &
  757. IEEE80211_CHAN_DISABLED)
  758. continue;
  759. /* If we have a wireless request structure and the
  760. * wireless request specifies frequencies, then search
  761. * for the matching hardware channel.
  762. */
  763. if (wreq && wreq->num_channels) {
  764. int k;
  765. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  766. for (k = 0; k < wreq->num_channels; k++) {
  767. int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
  768. if (wext_freq == wiphy_freq)
  769. goto wext_freq_found;
  770. }
  771. goto wext_freq_not_found;
  772. }
  773. wext_freq_found:
  774. creq->channels[i] = &wiphy->bands[band]->channels[j];
  775. i++;
  776. wext_freq_not_found: ;
  777. }
  778. }
  779. /* No channels found? */
  780. if (!i) {
  781. err = -EINVAL;
  782. goto out;
  783. }
  784. /* Set real number of channels specified in creq->channels[] */
  785. creq->n_channels = i;
  786. /* translate "Scan for SSID" request */
  787. if (wreq) {
  788. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  789. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  790. err = -EINVAL;
  791. goto out;
  792. }
  793. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  794. creq->ssids[0].ssid_len = wreq->essid_len;
  795. }
  796. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  797. creq->n_ssids = 0;
  798. }
  799. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  800. if (wiphy->bands[i])
  801. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  802. rdev->scan_req = creq;
  803. err = rdev_scan(rdev, creq);
  804. if (err) {
  805. rdev->scan_req = NULL;
  806. /* creq will be freed below */
  807. } else {
  808. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  809. /* creq now owned by driver */
  810. creq = NULL;
  811. dev_hold(dev);
  812. }
  813. out:
  814. kfree(creq);
  815. cfg80211_unlock_rdev(rdev);
  816. return err;
  817. }
  818. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  819. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  820. const struct cfg80211_bss_ies *ies,
  821. char **current_ev, char *end_buf)
  822. {
  823. const u8 *pos, *end, *next;
  824. struct iw_event iwe;
  825. if (!ies)
  826. return;
  827. /*
  828. * If needed, fragment the IEs buffer (at IE boundaries) into short
  829. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  830. */
  831. pos = ies->data;
  832. end = pos + ies->len;
  833. while (end - pos > IW_GENERIC_IE_MAX) {
  834. next = pos + 2 + pos[1];
  835. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  836. next = next + 2 + next[1];
  837. memset(&iwe, 0, sizeof(iwe));
  838. iwe.cmd = IWEVGENIE;
  839. iwe.u.data.length = next - pos;
  840. *current_ev = iwe_stream_add_point(info, *current_ev,
  841. end_buf, &iwe,
  842. (void *)pos);
  843. pos = next;
  844. }
  845. if (end > pos) {
  846. memset(&iwe, 0, sizeof(iwe));
  847. iwe.cmd = IWEVGENIE;
  848. iwe.u.data.length = end - pos;
  849. *current_ev = iwe_stream_add_point(info, *current_ev,
  850. end_buf, &iwe,
  851. (void *)pos);
  852. }
  853. }
  854. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  855. {
  856. unsigned long end = jiffies;
  857. if (end >= start)
  858. return jiffies_to_msecs(end - start);
  859. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  860. }
  861. static char *
  862. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  863. struct cfg80211_internal_bss *bss, char *current_ev,
  864. char *end_buf)
  865. {
  866. const struct cfg80211_bss_ies *ies;
  867. struct iw_event iwe;
  868. const u8 *ie;
  869. u8 *buf, *cfg, *p;
  870. int rem, i, sig;
  871. bool ismesh = false;
  872. memset(&iwe, 0, sizeof(iwe));
  873. iwe.cmd = SIOCGIWAP;
  874. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  875. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  876. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  877. IW_EV_ADDR_LEN);
  878. memset(&iwe, 0, sizeof(iwe));
  879. iwe.cmd = SIOCGIWFREQ;
  880. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  881. iwe.u.freq.e = 0;
  882. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  883. IW_EV_FREQ_LEN);
  884. memset(&iwe, 0, sizeof(iwe));
  885. iwe.cmd = SIOCGIWFREQ;
  886. iwe.u.freq.m = bss->pub.channel->center_freq;
  887. iwe.u.freq.e = 6;
  888. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  889. IW_EV_FREQ_LEN);
  890. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  891. memset(&iwe, 0, sizeof(iwe));
  892. iwe.cmd = IWEVQUAL;
  893. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  894. IW_QUAL_NOISE_INVALID |
  895. IW_QUAL_QUAL_UPDATED;
  896. switch (wiphy->signal_type) {
  897. case CFG80211_SIGNAL_TYPE_MBM:
  898. sig = bss->pub.signal / 100;
  899. iwe.u.qual.level = sig;
  900. iwe.u.qual.updated |= IW_QUAL_DBM;
  901. if (sig < -110) /* rather bad */
  902. sig = -110;
  903. else if (sig > -40) /* perfect */
  904. sig = -40;
  905. /* will give a range of 0 .. 70 */
  906. iwe.u.qual.qual = sig + 110;
  907. break;
  908. case CFG80211_SIGNAL_TYPE_UNSPEC:
  909. iwe.u.qual.level = bss->pub.signal;
  910. /* will give range 0 .. 100 */
  911. iwe.u.qual.qual = bss->pub.signal;
  912. break;
  913. default:
  914. /* not reached */
  915. break;
  916. }
  917. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  918. &iwe, IW_EV_QUAL_LEN);
  919. }
  920. memset(&iwe, 0, sizeof(iwe));
  921. iwe.cmd = SIOCGIWENCODE;
  922. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  923. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  924. else
  925. iwe.u.data.flags = IW_ENCODE_DISABLED;
  926. iwe.u.data.length = 0;
  927. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  928. &iwe, "");
  929. rcu_read_lock();
  930. ies = rcu_dereference(bss->pub.ies);
  931. if (ies) {
  932. rem = ies->len;
  933. ie = ies->data;
  934. } else {
  935. rem = 0;
  936. ie = NULL;
  937. }
  938. while (ies && rem >= 2) {
  939. /* invalid data */
  940. if (ie[1] > rem - 2)
  941. break;
  942. switch (ie[0]) {
  943. case WLAN_EID_SSID:
  944. memset(&iwe, 0, sizeof(iwe));
  945. iwe.cmd = SIOCGIWESSID;
  946. iwe.u.data.length = ie[1];
  947. iwe.u.data.flags = 1;
  948. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  949. &iwe, (u8 *)ie + 2);
  950. break;
  951. case WLAN_EID_MESH_ID:
  952. memset(&iwe, 0, sizeof(iwe));
  953. iwe.cmd = SIOCGIWESSID;
  954. iwe.u.data.length = ie[1];
  955. iwe.u.data.flags = 1;
  956. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  957. &iwe, (u8 *)ie + 2);
  958. break;
  959. case WLAN_EID_MESH_CONFIG:
  960. ismesh = true;
  961. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  962. break;
  963. buf = kmalloc(50, GFP_ATOMIC);
  964. if (!buf)
  965. break;
  966. cfg = (u8 *)ie + 2;
  967. memset(&iwe, 0, sizeof(iwe));
  968. iwe.cmd = IWEVCUSTOM;
  969. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  970. "0x%02X", cfg[0]);
  971. iwe.u.data.length = strlen(buf);
  972. current_ev = iwe_stream_add_point(info, current_ev,
  973. end_buf,
  974. &iwe, buf);
  975. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  976. cfg[1]);
  977. iwe.u.data.length = strlen(buf);
  978. current_ev = iwe_stream_add_point(info, current_ev,
  979. end_buf,
  980. &iwe, buf);
  981. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  982. cfg[2]);
  983. iwe.u.data.length = strlen(buf);
  984. current_ev = iwe_stream_add_point(info, current_ev,
  985. end_buf,
  986. &iwe, buf);
  987. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  988. iwe.u.data.length = strlen(buf);
  989. current_ev = iwe_stream_add_point(info, current_ev,
  990. end_buf,
  991. &iwe, buf);
  992. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  993. iwe.u.data.length = strlen(buf);
  994. current_ev = iwe_stream_add_point(info, current_ev,
  995. end_buf,
  996. &iwe, buf);
  997. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  998. iwe.u.data.length = strlen(buf);
  999. current_ev = iwe_stream_add_point(info, current_ev,
  1000. end_buf,
  1001. &iwe, buf);
  1002. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1003. iwe.u.data.length = strlen(buf);
  1004. current_ev = iwe_stream_add_point(info, current_ev,
  1005. end_buf,
  1006. &iwe, buf);
  1007. kfree(buf);
  1008. break;
  1009. case WLAN_EID_SUPP_RATES:
  1010. case WLAN_EID_EXT_SUPP_RATES:
  1011. /* display all supported rates in readable format */
  1012. p = current_ev + iwe_stream_lcp_len(info);
  1013. memset(&iwe, 0, sizeof(iwe));
  1014. iwe.cmd = SIOCGIWRATE;
  1015. /* Those two flags are ignored... */
  1016. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1017. for (i = 0; i < ie[1]; i++) {
  1018. iwe.u.bitrate.value =
  1019. ((ie[i + 2] & 0x7f) * 500000);
  1020. p = iwe_stream_add_value(info, current_ev, p,
  1021. end_buf, &iwe, IW_EV_PARAM_LEN);
  1022. }
  1023. current_ev = p;
  1024. break;
  1025. }
  1026. rem -= ie[1] + 2;
  1027. ie += ie[1] + 2;
  1028. }
  1029. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1030. ismesh) {
  1031. memset(&iwe, 0, sizeof(iwe));
  1032. iwe.cmd = SIOCGIWMODE;
  1033. if (ismesh)
  1034. iwe.u.mode = IW_MODE_MESH;
  1035. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1036. iwe.u.mode = IW_MODE_MASTER;
  1037. else
  1038. iwe.u.mode = IW_MODE_ADHOC;
  1039. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  1040. &iwe, IW_EV_UINT_LEN);
  1041. }
  1042. buf = kmalloc(30, GFP_ATOMIC);
  1043. if (buf) {
  1044. memset(&iwe, 0, sizeof(iwe));
  1045. iwe.cmd = IWEVCUSTOM;
  1046. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  1047. iwe.u.data.length = strlen(buf);
  1048. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1049. &iwe, buf);
  1050. memset(&iwe, 0, sizeof(iwe));
  1051. iwe.cmd = IWEVCUSTOM;
  1052. sprintf(buf, " Last beacon: %ums ago",
  1053. elapsed_jiffies_msecs(bss->ts));
  1054. iwe.u.data.length = strlen(buf);
  1055. current_ev = iwe_stream_add_point(info, current_ev,
  1056. end_buf, &iwe, buf);
  1057. kfree(buf);
  1058. }
  1059. ieee80211_scan_add_ies(info, ies, &current_ev, end_buf);
  1060. rcu_read_unlock();
  1061. return current_ev;
  1062. }
  1063. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  1064. struct iw_request_info *info,
  1065. char *buf, size_t len)
  1066. {
  1067. char *current_ev = buf;
  1068. char *end_buf = buf + len;
  1069. struct cfg80211_internal_bss *bss;
  1070. spin_lock_bh(&dev->bss_lock);
  1071. cfg80211_bss_expire(dev);
  1072. list_for_each_entry(bss, &dev->bss_list, list) {
  1073. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1074. spin_unlock_bh(&dev->bss_lock);
  1075. return -E2BIG;
  1076. }
  1077. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  1078. current_ev, end_buf);
  1079. }
  1080. spin_unlock_bh(&dev->bss_lock);
  1081. return current_ev - buf;
  1082. }
  1083. int cfg80211_wext_giwscan(struct net_device *dev,
  1084. struct iw_request_info *info,
  1085. struct iw_point *data, char *extra)
  1086. {
  1087. struct cfg80211_registered_device *rdev;
  1088. int res;
  1089. if (!netif_running(dev))
  1090. return -ENETDOWN;
  1091. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1092. if (IS_ERR(rdev))
  1093. return PTR_ERR(rdev);
  1094. if (rdev->scan_req) {
  1095. res = -EAGAIN;
  1096. goto out;
  1097. }
  1098. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1099. data->length = 0;
  1100. if (res >= 0) {
  1101. data->length = res;
  1102. res = 0;
  1103. }
  1104. out:
  1105. cfg80211_unlock_rdev(rdev);
  1106. return res;
  1107. }
  1108. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  1109. #endif