scan.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316
  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 iies pointer should we have received an earlier
  599. * indication of Probe Response data.
  600. *
  601. * The initial buffer for the IEs is allocated with the BSS entry and
  602. * is located after the private area.
  603. */
  604. ies = kmalloc(sizeof(*ies) + ielen, gfp);
  605. if (!ies)
  606. return NULL;
  607. ies->len = ielen;
  608. memcpy(ies->data, ie, ielen);
  609. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  610. rcu_assign_pointer(tmp.pub.ies, ies);
  611. res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
  612. if (!res)
  613. return NULL;
  614. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  615. regulatory_hint_found_beacon(wiphy, channel, gfp);
  616. trace_cfg80211_return_bss(&res->pub);
  617. /* cfg80211_bss_update gives us a referenced result */
  618. return &res->pub;
  619. }
  620. EXPORT_SYMBOL(cfg80211_inform_bss);
  621. struct cfg80211_bss *
  622. cfg80211_inform_bss_frame(struct wiphy *wiphy,
  623. struct ieee80211_channel *channel,
  624. struct ieee80211_mgmt *mgmt, size_t len,
  625. s32 signal, gfp_t gfp)
  626. {
  627. struct cfg80211_internal_bss tmp = {}, *res;
  628. struct cfg80211_bss_ies *ies;
  629. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  630. u.probe_resp.variable);
  631. BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
  632. offsetof(struct ieee80211_mgmt, u.beacon.variable));
  633. trace_cfg80211_inform_bss_frame(wiphy, channel, mgmt, len, signal);
  634. if (WARN_ON(!mgmt))
  635. return NULL;
  636. if (WARN_ON(!wiphy))
  637. return NULL;
  638. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  639. (signal < 0 || signal > 100)))
  640. return NULL;
  641. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  642. return NULL;
  643. channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
  644. ielen, channel);
  645. if (!channel)
  646. return NULL;
  647. ies = kmalloc(sizeof(*ies) + ielen, gfp);
  648. if (!ies)
  649. return NULL;
  650. ies->len = ielen;
  651. memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
  652. if (ieee80211_is_probe_resp(mgmt->frame_control))
  653. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  654. else
  655. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  656. rcu_assign_pointer(tmp.pub.ies, ies);
  657. memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
  658. tmp.pub.channel = channel;
  659. tmp.pub.signal = signal;
  660. tmp.pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  661. tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  662. tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  663. res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
  664. if (!res)
  665. return NULL;
  666. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  667. regulatory_hint_found_beacon(wiphy, channel, gfp);
  668. trace_cfg80211_return_bss(&res->pub);
  669. /* cfg80211_bss_update gives us a referenced result */
  670. return &res->pub;
  671. }
  672. EXPORT_SYMBOL(cfg80211_inform_bss_frame);
  673. void cfg80211_ref_bss(struct cfg80211_bss *pub)
  674. {
  675. struct cfg80211_internal_bss *bss;
  676. if (!pub)
  677. return;
  678. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  679. kref_get(&bss->ref);
  680. }
  681. EXPORT_SYMBOL(cfg80211_ref_bss);
  682. void cfg80211_put_bss(struct cfg80211_bss *pub)
  683. {
  684. struct cfg80211_internal_bss *bss;
  685. if (!pub)
  686. return;
  687. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  688. kref_put(&bss->ref, bss_release);
  689. }
  690. EXPORT_SYMBOL(cfg80211_put_bss);
  691. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  692. {
  693. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  694. struct cfg80211_internal_bss *bss;
  695. if (WARN_ON(!pub))
  696. return;
  697. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  698. spin_lock_bh(&dev->bss_lock);
  699. if (!list_empty(&bss->list)) {
  700. __cfg80211_unlink_bss(dev, bss);
  701. dev->bss_generation++;
  702. }
  703. spin_unlock_bh(&dev->bss_lock);
  704. }
  705. EXPORT_SYMBOL(cfg80211_unlink_bss);
  706. #ifdef CONFIG_CFG80211_WEXT
  707. int cfg80211_wext_siwscan(struct net_device *dev,
  708. struct iw_request_info *info,
  709. union iwreq_data *wrqu, char *extra)
  710. {
  711. struct cfg80211_registered_device *rdev;
  712. struct wiphy *wiphy;
  713. struct iw_scan_req *wreq = NULL;
  714. struct cfg80211_scan_request *creq = NULL;
  715. int i, err, n_channels = 0;
  716. enum ieee80211_band band;
  717. if (!netif_running(dev))
  718. return -ENETDOWN;
  719. if (wrqu->data.length == sizeof(struct iw_scan_req))
  720. wreq = (struct iw_scan_req *)extra;
  721. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  722. if (IS_ERR(rdev))
  723. return PTR_ERR(rdev);
  724. if (rdev->scan_req) {
  725. err = -EBUSY;
  726. goto out;
  727. }
  728. wiphy = &rdev->wiphy;
  729. /* Determine number of channels, needed to allocate creq */
  730. if (wreq && wreq->num_channels)
  731. n_channels = wreq->num_channels;
  732. else {
  733. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  734. if (wiphy->bands[band])
  735. n_channels += wiphy->bands[band]->n_channels;
  736. }
  737. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  738. n_channels * sizeof(void *),
  739. GFP_ATOMIC);
  740. if (!creq) {
  741. err = -ENOMEM;
  742. goto out;
  743. }
  744. creq->wiphy = wiphy;
  745. creq->wdev = dev->ieee80211_ptr;
  746. /* SSIDs come after channels */
  747. creq->ssids = (void *)&creq->channels[n_channels];
  748. creq->n_channels = n_channels;
  749. creq->n_ssids = 1;
  750. creq->scan_start = jiffies;
  751. /* translate "Scan on frequencies" request */
  752. i = 0;
  753. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  754. int j;
  755. if (!wiphy->bands[band])
  756. continue;
  757. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  758. /* ignore disabled channels */
  759. if (wiphy->bands[band]->channels[j].flags &
  760. IEEE80211_CHAN_DISABLED)
  761. continue;
  762. /* If we have a wireless request structure and the
  763. * wireless request specifies frequencies, then search
  764. * for the matching hardware channel.
  765. */
  766. if (wreq && wreq->num_channels) {
  767. int k;
  768. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  769. for (k = 0; k < wreq->num_channels; k++) {
  770. int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
  771. if (wext_freq == wiphy_freq)
  772. goto wext_freq_found;
  773. }
  774. goto wext_freq_not_found;
  775. }
  776. wext_freq_found:
  777. creq->channels[i] = &wiphy->bands[band]->channels[j];
  778. i++;
  779. wext_freq_not_found: ;
  780. }
  781. }
  782. /* No channels found? */
  783. if (!i) {
  784. err = -EINVAL;
  785. goto out;
  786. }
  787. /* Set real number of channels specified in creq->channels[] */
  788. creq->n_channels = i;
  789. /* translate "Scan for SSID" request */
  790. if (wreq) {
  791. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  792. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  793. err = -EINVAL;
  794. goto out;
  795. }
  796. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  797. creq->ssids[0].ssid_len = wreq->essid_len;
  798. }
  799. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  800. creq->n_ssids = 0;
  801. }
  802. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  803. if (wiphy->bands[i])
  804. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  805. rdev->scan_req = creq;
  806. err = rdev_scan(rdev, creq);
  807. if (err) {
  808. rdev->scan_req = NULL;
  809. /* creq will be freed below */
  810. } else {
  811. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  812. /* creq now owned by driver */
  813. creq = NULL;
  814. dev_hold(dev);
  815. }
  816. out:
  817. kfree(creq);
  818. cfg80211_unlock_rdev(rdev);
  819. return err;
  820. }
  821. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  822. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  823. const struct cfg80211_bss_ies *ies,
  824. char **current_ev, char *end_buf)
  825. {
  826. const u8 *pos, *end, *next;
  827. struct iw_event iwe;
  828. if (!ies)
  829. return;
  830. /*
  831. * If needed, fragment the IEs buffer (at IE boundaries) into short
  832. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  833. */
  834. pos = ies->data;
  835. end = pos + ies->len;
  836. while (end - pos > IW_GENERIC_IE_MAX) {
  837. next = pos + 2 + pos[1];
  838. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  839. next = next + 2 + next[1];
  840. memset(&iwe, 0, sizeof(iwe));
  841. iwe.cmd = IWEVGENIE;
  842. iwe.u.data.length = next - pos;
  843. *current_ev = iwe_stream_add_point(info, *current_ev,
  844. end_buf, &iwe,
  845. (void *)pos);
  846. pos = next;
  847. }
  848. if (end > pos) {
  849. memset(&iwe, 0, sizeof(iwe));
  850. iwe.cmd = IWEVGENIE;
  851. iwe.u.data.length = end - pos;
  852. *current_ev = iwe_stream_add_point(info, *current_ev,
  853. end_buf, &iwe,
  854. (void *)pos);
  855. }
  856. }
  857. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  858. {
  859. unsigned long end = jiffies;
  860. if (end >= start)
  861. return jiffies_to_msecs(end - start);
  862. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  863. }
  864. static char *
  865. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  866. struct cfg80211_internal_bss *bss, char *current_ev,
  867. char *end_buf)
  868. {
  869. const struct cfg80211_bss_ies *ies;
  870. struct iw_event iwe;
  871. const u8 *ie;
  872. u8 *buf, *cfg, *p;
  873. int rem, i, sig;
  874. bool ismesh = false;
  875. memset(&iwe, 0, sizeof(iwe));
  876. iwe.cmd = SIOCGIWAP;
  877. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  878. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  879. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  880. IW_EV_ADDR_LEN);
  881. memset(&iwe, 0, sizeof(iwe));
  882. iwe.cmd = SIOCGIWFREQ;
  883. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  884. iwe.u.freq.e = 0;
  885. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  886. IW_EV_FREQ_LEN);
  887. memset(&iwe, 0, sizeof(iwe));
  888. iwe.cmd = SIOCGIWFREQ;
  889. iwe.u.freq.m = bss->pub.channel->center_freq;
  890. iwe.u.freq.e = 6;
  891. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  892. IW_EV_FREQ_LEN);
  893. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  894. memset(&iwe, 0, sizeof(iwe));
  895. iwe.cmd = IWEVQUAL;
  896. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  897. IW_QUAL_NOISE_INVALID |
  898. IW_QUAL_QUAL_UPDATED;
  899. switch (wiphy->signal_type) {
  900. case CFG80211_SIGNAL_TYPE_MBM:
  901. sig = bss->pub.signal / 100;
  902. iwe.u.qual.level = sig;
  903. iwe.u.qual.updated |= IW_QUAL_DBM;
  904. if (sig < -110) /* rather bad */
  905. sig = -110;
  906. else if (sig > -40) /* perfect */
  907. sig = -40;
  908. /* will give a range of 0 .. 70 */
  909. iwe.u.qual.qual = sig + 110;
  910. break;
  911. case CFG80211_SIGNAL_TYPE_UNSPEC:
  912. iwe.u.qual.level = bss->pub.signal;
  913. /* will give range 0 .. 100 */
  914. iwe.u.qual.qual = bss->pub.signal;
  915. break;
  916. default:
  917. /* not reached */
  918. break;
  919. }
  920. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  921. &iwe, IW_EV_QUAL_LEN);
  922. }
  923. memset(&iwe, 0, sizeof(iwe));
  924. iwe.cmd = SIOCGIWENCODE;
  925. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  926. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  927. else
  928. iwe.u.data.flags = IW_ENCODE_DISABLED;
  929. iwe.u.data.length = 0;
  930. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  931. &iwe, "");
  932. rcu_read_lock();
  933. ies = rcu_dereference(bss->pub.ies);
  934. if (ies) {
  935. rem = ies->len;
  936. ie = ies->data;
  937. } else {
  938. rem = 0;
  939. ie = NULL;
  940. }
  941. while (ies && rem >= 2) {
  942. /* invalid data */
  943. if (ie[1] > rem - 2)
  944. break;
  945. switch (ie[0]) {
  946. case WLAN_EID_SSID:
  947. memset(&iwe, 0, sizeof(iwe));
  948. iwe.cmd = SIOCGIWESSID;
  949. iwe.u.data.length = ie[1];
  950. iwe.u.data.flags = 1;
  951. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  952. &iwe, (u8 *)ie + 2);
  953. break;
  954. case WLAN_EID_MESH_ID:
  955. memset(&iwe, 0, sizeof(iwe));
  956. iwe.cmd = SIOCGIWESSID;
  957. iwe.u.data.length = ie[1];
  958. iwe.u.data.flags = 1;
  959. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  960. &iwe, (u8 *)ie + 2);
  961. break;
  962. case WLAN_EID_MESH_CONFIG:
  963. ismesh = true;
  964. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  965. break;
  966. buf = kmalloc(50, GFP_ATOMIC);
  967. if (!buf)
  968. break;
  969. cfg = (u8 *)ie + 2;
  970. memset(&iwe, 0, sizeof(iwe));
  971. iwe.cmd = IWEVCUSTOM;
  972. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  973. "0x%02X", cfg[0]);
  974. iwe.u.data.length = strlen(buf);
  975. current_ev = iwe_stream_add_point(info, current_ev,
  976. end_buf,
  977. &iwe, buf);
  978. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  979. cfg[1]);
  980. iwe.u.data.length = strlen(buf);
  981. current_ev = iwe_stream_add_point(info, current_ev,
  982. end_buf,
  983. &iwe, buf);
  984. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  985. cfg[2]);
  986. iwe.u.data.length = strlen(buf);
  987. current_ev = iwe_stream_add_point(info, current_ev,
  988. end_buf,
  989. &iwe, buf);
  990. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  991. iwe.u.data.length = strlen(buf);
  992. current_ev = iwe_stream_add_point(info, current_ev,
  993. end_buf,
  994. &iwe, buf);
  995. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  996. iwe.u.data.length = strlen(buf);
  997. current_ev = iwe_stream_add_point(info, current_ev,
  998. end_buf,
  999. &iwe, buf);
  1000. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  1001. iwe.u.data.length = strlen(buf);
  1002. current_ev = iwe_stream_add_point(info, current_ev,
  1003. end_buf,
  1004. &iwe, buf);
  1005. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1006. iwe.u.data.length = strlen(buf);
  1007. current_ev = iwe_stream_add_point(info, current_ev,
  1008. end_buf,
  1009. &iwe, buf);
  1010. kfree(buf);
  1011. break;
  1012. case WLAN_EID_SUPP_RATES:
  1013. case WLAN_EID_EXT_SUPP_RATES:
  1014. /* display all supported rates in readable format */
  1015. p = current_ev + iwe_stream_lcp_len(info);
  1016. memset(&iwe, 0, sizeof(iwe));
  1017. iwe.cmd = SIOCGIWRATE;
  1018. /* Those two flags are ignored... */
  1019. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1020. for (i = 0; i < ie[1]; i++) {
  1021. iwe.u.bitrate.value =
  1022. ((ie[i + 2] & 0x7f) * 500000);
  1023. p = iwe_stream_add_value(info, current_ev, p,
  1024. end_buf, &iwe, IW_EV_PARAM_LEN);
  1025. }
  1026. current_ev = p;
  1027. break;
  1028. }
  1029. rem -= ie[1] + 2;
  1030. ie += ie[1] + 2;
  1031. }
  1032. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1033. ismesh) {
  1034. memset(&iwe, 0, sizeof(iwe));
  1035. iwe.cmd = SIOCGIWMODE;
  1036. if (ismesh)
  1037. iwe.u.mode = IW_MODE_MESH;
  1038. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1039. iwe.u.mode = IW_MODE_MASTER;
  1040. else
  1041. iwe.u.mode = IW_MODE_ADHOC;
  1042. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  1043. &iwe, IW_EV_UINT_LEN);
  1044. }
  1045. buf = kmalloc(30, GFP_ATOMIC);
  1046. if (buf) {
  1047. memset(&iwe, 0, sizeof(iwe));
  1048. iwe.cmd = IWEVCUSTOM;
  1049. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  1050. iwe.u.data.length = strlen(buf);
  1051. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1052. &iwe, buf);
  1053. memset(&iwe, 0, sizeof(iwe));
  1054. iwe.cmd = IWEVCUSTOM;
  1055. sprintf(buf, " Last beacon: %ums ago",
  1056. elapsed_jiffies_msecs(bss->ts));
  1057. iwe.u.data.length = strlen(buf);
  1058. current_ev = iwe_stream_add_point(info, current_ev,
  1059. end_buf, &iwe, buf);
  1060. kfree(buf);
  1061. }
  1062. ieee80211_scan_add_ies(info, ies, &current_ev, end_buf);
  1063. rcu_read_unlock();
  1064. return current_ev;
  1065. }
  1066. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  1067. struct iw_request_info *info,
  1068. char *buf, size_t len)
  1069. {
  1070. char *current_ev = buf;
  1071. char *end_buf = buf + len;
  1072. struct cfg80211_internal_bss *bss;
  1073. spin_lock_bh(&dev->bss_lock);
  1074. cfg80211_bss_expire(dev);
  1075. list_for_each_entry(bss, &dev->bss_list, list) {
  1076. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1077. spin_unlock_bh(&dev->bss_lock);
  1078. return -E2BIG;
  1079. }
  1080. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  1081. current_ev, end_buf);
  1082. }
  1083. spin_unlock_bh(&dev->bss_lock);
  1084. return current_ev - buf;
  1085. }
  1086. int cfg80211_wext_giwscan(struct net_device *dev,
  1087. struct iw_request_info *info,
  1088. struct iw_point *data, char *extra)
  1089. {
  1090. struct cfg80211_registered_device *rdev;
  1091. int res;
  1092. if (!netif_running(dev))
  1093. return -ENETDOWN;
  1094. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1095. if (IS_ERR(rdev))
  1096. return PTR_ERR(rdev);
  1097. if (rdev->scan_req) {
  1098. res = -EAGAIN;
  1099. goto out;
  1100. }
  1101. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1102. data->length = 0;
  1103. if (res >= 0) {
  1104. data->length = res;
  1105. res = 0;
  1106. }
  1107. out:
  1108. cfg80211_unlock_rdev(rdev);
  1109. return res;
  1110. }
  1111. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  1112. #endif