scan.c 33 KB

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