scan.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397
  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 int cmp_ies(u8 num, const u8 *ies1, int len1, const u8 *ies2, int len2)
  236. {
  237. const u8 *ie1 = cfg80211_find_ie(num, ies1, len1);
  238. const u8 *ie2 = cfg80211_find_ie(num, ies2, len2);
  239. /* equal if both missing */
  240. if (!ie1 && !ie2)
  241. return 0;
  242. /* sort missing IE before (left of) present IE */
  243. if (!ie1)
  244. return -1;
  245. if (!ie2)
  246. return 1;
  247. /* sort by length first, then by contents */
  248. if (ie1[1] != ie2[1])
  249. return ie2[1] - ie1[1];
  250. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  251. }
  252. static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
  253. const u8 *ssid, size_t ssid_len)
  254. {
  255. const struct cfg80211_bss_ies *ies;
  256. const u8 *ssidie;
  257. if (bssid && !ether_addr_equal(a->bssid, bssid))
  258. return false;
  259. if (!ssid)
  260. return true;
  261. ies = rcu_access_pointer(a->ies);
  262. if (!ies)
  263. return false;
  264. ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  265. if (!ssidie)
  266. return false;
  267. if (ssidie[1] != ssid_len)
  268. return false;
  269. return memcmp(ssidie + 2, ssid, ssid_len) == 0;
  270. }
  271. static bool is_mesh_bss(struct cfg80211_bss *a)
  272. {
  273. const struct cfg80211_bss_ies *ies;
  274. const u8 *ie;
  275. if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
  276. return false;
  277. ies = rcu_access_pointer(a->ies);
  278. if (!ies)
  279. return false;
  280. ie = cfg80211_find_ie(WLAN_EID_MESH_ID, ies->data, ies->len);
  281. if (!ie)
  282. return false;
  283. ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, ies->data, ies->len);
  284. if (!ie)
  285. return false;
  286. return true;
  287. }
  288. static bool is_mesh(struct cfg80211_bss *a,
  289. const u8 *meshid, size_t meshidlen,
  290. const u8 *meshcfg)
  291. {
  292. const struct cfg80211_bss_ies *ies;
  293. const u8 *ie;
  294. if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
  295. return false;
  296. ies = rcu_access_pointer(a->ies);
  297. if (!ies)
  298. return false;
  299. ie = cfg80211_find_ie(WLAN_EID_MESH_ID, ies->data, ies->len);
  300. if (!ie)
  301. return false;
  302. if (ie[1] != meshidlen)
  303. return false;
  304. if (memcmp(ie + 2, meshid, meshidlen))
  305. return false;
  306. ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG, ies->data, ies->len);
  307. if (!ie)
  308. return false;
  309. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  310. return false;
  311. /*
  312. * Ignore mesh capability (last two bytes of the IE) when
  313. * comparing since that may differ between stations taking
  314. * part in the same mesh.
  315. */
  316. return memcmp(ie + 2, meshcfg,
  317. sizeof(struct ieee80211_meshconf_ie) - 2) == 0;
  318. }
  319. static int cmp_bss_core(struct cfg80211_bss *a, struct cfg80211_bss *b)
  320. {
  321. const struct cfg80211_bss_ies *a_ies, *b_ies;
  322. int r;
  323. if (a->channel != b->channel)
  324. return b->channel->center_freq - a->channel->center_freq;
  325. if (is_mesh_bss(a) && is_mesh_bss(b)) {
  326. a_ies = rcu_access_pointer(a->ies);
  327. if (!a_ies)
  328. return -1;
  329. b_ies = rcu_access_pointer(b->ies);
  330. if (!b_ies)
  331. return 1;
  332. r = cmp_ies(WLAN_EID_MESH_ID,
  333. a_ies->data, a_ies->len,
  334. b_ies->data, b_ies->len);
  335. if (r)
  336. return r;
  337. return cmp_ies(WLAN_EID_MESH_CONFIG,
  338. a_ies->data, a_ies->len,
  339. b_ies->data, b_ies->len);
  340. }
  341. /*
  342. * we can't use compare_ether_addr here since we need a < > operator.
  343. * The binary return value of compare_ether_addr isn't enough
  344. */
  345. return memcmp(a->bssid, b->bssid, sizeof(a->bssid));
  346. }
  347. static int cmp_bss(struct cfg80211_bss *a,
  348. struct cfg80211_bss *b,
  349. bool hide_ssid)
  350. {
  351. const struct cfg80211_bss_ies *a_ies, *b_ies;
  352. const u8 *ie1;
  353. const u8 *ie2;
  354. int i, r;
  355. r = cmp_bss_core(a, b);
  356. if (r)
  357. return r;
  358. a_ies = rcu_access_pointer(a->ies);
  359. if (!a_ies)
  360. return -1;
  361. b_ies = rcu_access_pointer(b->ies);
  362. if (!b_ies)
  363. return 1;
  364. ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
  365. ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
  366. if (!ie1 && !ie2)
  367. return 0;
  368. /*
  369. * Note that with "hide_ssid", the function returns a match if
  370. * the already-present BSS ("b") is a hidden SSID beacon for
  371. * the new BSS ("a").
  372. */
  373. /* sort missing IE before (left of) present IE */
  374. if (!ie1)
  375. return -1;
  376. if (!ie2)
  377. return 1;
  378. /* zero-length SSID is used as an indication of the hidden bss */
  379. if (hide_ssid && !ie2[1])
  380. return 0;
  381. /* sort by length first, then by contents */
  382. if (ie1[1] != ie2[1])
  383. return ie2[1] - ie1[1];
  384. if (!hide_ssid)
  385. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  386. /*
  387. * zeroed SSID ie is another indication of a hidden bss;
  388. * if it isn't zeroed just return the regular sort value
  389. * to find the next candidate
  390. */
  391. for (i = 0; i < ie2[1]; i++)
  392. if (ie2[i + 2])
  393. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  394. return 0;
  395. }
  396. struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
  397. struct ieee80211_channel *channel,
  398. const u8 *bssid,
  399. const u8 *ssid, size_t ssid_len,
  400. u16 capa_mask, u16 capa_val)
  401. {
  402. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  403. struct cfg80211_internal_bss *bss, *res = NULL;
  404. unsigned long now = jiffies;
  405. trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, capa_mask,
  406. capa_val);
  407. spin_lock_bh(&dev->bss_lock);
  408. list_for_each_entry(bss, &dev->bss_list, list) {
  409. if ((bss->pub.capability & capa_mask) != capa_val)
  410. continue;
  411. if (channel && bss->pub.channel != channel)
  412. continue;
  413. /* Don't get expired BSS structs */
  414. if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
  415. !atomic_read(&bss->hold))
  416. continue;
  417. if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
  418. res = bss;
  419. kref_get(&res->ref);
  420. break;
  421. }
  422. }
  423. spin_unlock_bh(&dev->bss_lock);
  424. if (!res)
  425. return NULL;
  426. trace_cfg80211_return_bss(&res->pub);
  427. return &res->pub;
  428. }
  429. EXPORT_SYMBOL(cfg80211_get_bss);
  430. struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
  431. struct ieee80211_channel *channel,
  432. const u8 *meshid, size_t meshidlen,
  433. const u8 *meshcfg)
  434. {
  435. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  436. struct cfg80211_internal_bss *bss, *res = NULL;
  437. spin_lock_bh(&dev->bss_lock);
  438. list_for_each_entry(bss, &dev->bss_list, list) {
  439. if (channel && bss->pub.channel != channel)
  440. continue;
  441. if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
  442. res = bss;
  443. kref_get(&res->ref);
  444. break;
  445. }
  446. }
  447. spin_unlock_bh(&dev->bss_lock);
  448. if (!res)
  449. return NULL;
  450. return &res->pub;
  451. }
  452. EXPORT_SYMBOL(cfg80211_get_mesh);
  453. static void rb_insert_bss(struct cfg80211_registered_device *dev,
  454. struct cfg80211_internal_bss *bss)
  455. {
  456. struct rb_node **p = &dev->bss_tree.rb_node;
  457. struct rb_node *parent = NULL;
  458. struct cfg80211_internal_bss *tbss;
  459. int cmp;
  460. while (*p) {
  461. parent = *p;
  462. tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
  463. cmp = cmp_bss(&bss->pub, &tbss->pub, false);
  464. if (WARN_ON(!cmp)) {
  465. /* will sort of leak this BSS */
  466. return;
  467. }
  468. if (cmp < 0)
  469. p = &(*p)->rb_left;
  470. else
  471. p = &(*p)->rb_right;
  472. }
  473. rb_link_node(&bss->rbn, parent, p);
  474. rb_insert_color(&bss->rbn, &dev->bss_tree);
  475. }
  476. static struct cfg80211_internal_bss *
  477. rb_find_bss(struct cfg80211_registered_device *dev,
  478. struct cfg80211_internal_bss *res,
  479. bool hidden)
  480. {
  481. struct rb_node *n = dev->bss_tree.rb_node;
  482. struct cfg80211_internal_bss *bss;
  483. int r;
  484. while (n) {
  485. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  486. r = cmp_bss(&res->pub, &bss->pub, hidden);
  487. if (r == 0)
  488. return bss;
  489. else if (r < 0)
  490. n = n->rb_left;
  491. else
  492. n = n->rb_right;
  493. }
  494. return NULL;
  495. }
  496. static void
  497. copy_hidden_ies(struct cfg80211_internal_bss *res,
  498. struct cfg80211_internal_bss *hidden)
  499. {
  500. const struct cfg80211_bss_ies *ies;
  501. if (rcu_access_pointer(res->pub.beacon_ies))
  502. return;
  503. ies = rcu_access_pointer(hidden->pub.beacon_ies);
  504. if (WARN_ON(!ies))
  505. return;
  506. ies = kmemdup(ies, sizeof(*ies) + ies->len, GFP_ATOMIC);
  507. if (unlikely(!ies))
  508. return;
  509. rcu_assign_pointer(res->pub.beacon_ies, ies);
  510. }
  511. static struct cfg80211_internal_bss *
  512. cfg80211_bss_update(struct cfg80211_registered_device *dev,
  513. struct cfg80211_internal_bss *tmp)
  514. {
  515. struct cfg80211_internal_bss *found = NULL;
  516. if (WARN_ON(!tmp->pub.channel))
  517. return NULL;
  518. tmp->ts = jiffies;
  519. spin_lock_bh(&dev->bss_lock);
  520. if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
  521. spin_unlock_bh(&dev->bss_lock);
  522. return NULL;
  523. }
  524. found = rb_find_bss(dev, tmp, false);
  525. if (found) {
  526. found->pub.beacon_interval = tmp->pub.beacon_interval;
  527. found->pub.tsf = tmp->pub.tsf;
  528. found->pub.signal = tmp->pub.signal;
  529. found->pub.capability = tmp->pub.capability;
  530. found->ts = tmp->ts;
  531. /* Update IEs */
  532. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  533. const struct cfg80211_bss_ies *old;
  534. old = rcu_access_pointer(found->pub.proberesp_ies);
  535. rcu_assign_pointer(found->pub.proberesp_ies,
  536. tmp->pub.proberesp_ies);
  537. /* Override possible earlier Beacon frame IEs */
  538. rcu_assign_pointer(found->pub.ies,
  539. tmp->pub.proberesp_ies);
  540. if (old)
  541. kfree_rcu((struct cfg80211_bss_ies *)old,
  542. rcu_head);
  543. } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
  544. const struct cfg80211_bss_ies *old, *ies;
  545. old = rcu_access_pointer(found->pub.beacon_ies);
  546. ies = rcu_access_pointer(found->pub.ies);
  547. rcu_assign_pointer(found->pub.beacon_ies,
  548. tmp->pub.beacon_ies);
  549. /* Override IEs if they were from a beacon before */
  550. if (old == ies)
  551. rcu_assign_pointer(found->pub.ies,
  552. tmp->pub.beacon_ies);
  553. if (old)
  554. kfree_rcu((struct cfg80211_bss_ies *)old,
  555. rcu_head);
  556. }
  557. } else {
  558. struct cfg80211_internal_bss *new;
  559. struct cfg80211_internal_bss *hidden;
  560. struct cfg80211_bss_ies *ies;
  561. /* First check if the beacon is a probe response from
  562. * a hidden bss. If so, copy beacon ies (with nullified
  563. * ssid) into the probe response bss entry (with real ssid).
  564. * It is required basically for PSM implementation
  565. * (probe responses do not contain tim ie) */
  566. /* TODO: The code is not trying to update existing probe
  567. * response bss entries when beacon ies are
  568. * getting changed. */
  569. hidden = rb_find_bss(dev, tmp, true);
  570. if (hidden)
  571. copy_hidden_ies(tmp, hidden);
  572. /*
  573. * create a copy -- the "res" variable that is passed in
  574. * is allocated on the stack since it's not needed in the
  575. * more common case of an update
  576. */
  577. new = kzalloc(sizeof(*new) + dev->wiphy.bss_priv_size,
  578. GFP_ATOMIC);
  579. if (!new) {
  580. ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
  581. if (ies)
  582. kfree_rcu(ies, rcu_head);
  583. ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
  584. if (ies)
  585. kfree_rcu(ies, rcu_head);
  586. spin_unlock_bh(&dev->bss_lock);
  587. return NULL;
  588. }
  589. memcpy(new, tmp, sizeof(*new));
  590. kref_init(&new->ref);
  591. list_add_tail(&new->list, &dev->bss_list);
  592. rb_insert_bss(dev, new);
  593. found = new;
  594. }
  595. dev->bss_generation++;
  596. spin_unlock_bh(&dev->bss_lock);
  597. kref_get(&found->ref);
  598. return found;
  599. }
  600. static struct ieee80211_channel *
  601. cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
  602. struct ieee80211_channel *channel)
  603. {
  604. const u8 *tmp;
  605. u32 freq;
  606. int channel_number = -1;
  607. tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
  608. if (tmp && tmp[1] == 1) {
  609. channel_number = tmp[2];
  610. } else {
  611. tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
  612. if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
  613. struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
  614. channel_number = htop->primary_chan;
  615. }
  616. }
  617. if (channel_number < 0)
  618. return channel;
  619. freq = ieee80211_channel_to_frequency(channel_number, channel->band);
  620. channel = ieee80211_get_channel(wiphy, freq);
  621. if (!channel)
  622. return NULL;
  623. if (channel->flags & IEEE80211_CHAN_DISABLED)
  624. return NULL;
  625. return channel;
  626. }
  627. struct cfg80211_bss*
  628. cfg80211_inform_bss(struct wiphy *wiphy,
  629. struct ieee80211_channel *channel,
  630. const u8 *bssid, u64 tsf, u16 capability,
  631. u16 beacon_interval, const u8 *ie, size_t ielen,
  632. s32 signal, gfp_t gfp)
  633. {
  634. struct cfg80211_bss_ies *ies;
  635. struct cfg80211_internal_bss tmp = {}, *res;
  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. channel = cfg80211_get_bss_channel(wiphy, ie, ielen, channel);
  642. if (!channel)
  643. return NULL;
  644. memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
  645. tmp.pub.channel = channel;
  646. tmp.pub.signal = signal;
  647. tmp.pub.tsf = tsf;
  648. tmp.pub.beacon_interval = beacon_interval;
  649. tmp.pub.capability = capability;
  650. /*
  651. * Since we do not know here whether the IEs are from a Beacon or Probe
  652. * Response frame, we need to pick one of the options and only use it
  653. * with the driver that does not provide the full Beacon/Probe Response
  654. * frame. Use Beacon frame pointer to avoid indicating that this should
  655. * override the iies pointer should we have received an earlier
  656. * indication of Probe Response data.
  657. *
  658. * The initial buffer for the IEs is allocated with the BSS entry and
  659. * is located after the private area.
  660. */
  661. ies = kmalloc(sizeof(*ies) + ielen, gfp);
  662. if (!ies)
  663. return NULL;
  664. ies->len = ielen;
  665. memcpy(ies->data, ie, ielen);
  666. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  667. rcu_assign_pointer(tmp.pub.ies, ies);
  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);
  678. struct cfg80211_bss *
  679. cfg80211_inform_bss_frame(struct wiphy *wiphy,
  680. struct ieee80211_channel *channel,
  681. struct ieee80211_mgmt *mgmt, size_t len,
  682. s32 signal, gfp_t gfp)
  683. {
  684. struct cfg80211_internal_bss tmp = {}, *res;
  685. struct cfg80211_bss_ies *ies;
  686. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  687. u.probe_resp.variable);
  688. BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
  689. offsetof(struct ieee80211_mgmt, u.beacon.variable));
  690. trace_cfg80211_inform_bss_frame(wiphy, channel, mgmt, len, signal);
  691. if (WARN_ON(!mgmt))
  692. return NULL;
  693. if (WARN_ON(!wiphy))
  694. return NULL;
  695. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  696. (signal < 0 || signal > 100)))
  697. return NULL;
  698. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  699. return NULL;
  700. channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
  701. ielen, channel);
  702. if (!channel)
  703. return NULL;
  704. ies = kmalloc(sizeof(*ies) + ielen, gfp);
  705. if (!ies)
  706. return NULL;
  707. ies->len = ielen;
  708. memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
  709. if (ieee80211_is_probe_resp(mgmt->frame_control))
  710. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  711. else
  712. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  713. rcu_assign_pointer(tmp.pub.ies, ies);
  714. memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
  715. tmp.pub.channel = channel;
  716. tmp.pub.signal = signal;
  717. tmp.pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  718. tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  719. tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  720. res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
  721. if (!res)
  722. return NULL;
  723. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  724. regulatory_hint_found_beacon(wiphy, channel, gfp);
  725. trace_cfg80211_return_bss(&res->pub);
  726. /* cfg80211_bss_update gives us a referenced result */
  727. return &res->pub;
  728. }
  729. EXPORT_SYMBOL(cfg80211_inform_bss_frame);
  730. void cfg80211_ref_bss(struct cfg80211_bss *pub)
  731. {
  732. struct cfg80211_internal_bss *bss;
  733. if (!pub)
  734. return;
  735. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  736. kref_get(&bss->ref);
  737. }
  738. EXPORT_SYMBOL(cfg80211_ref_bss);
  739. void cfg80211_put_bss(struct cfg80211_bss *pub)
  740. {
  741. struct cfg80211_internal_bss *bss;
  742. if (!pub)
  743. return;
  744. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  745. kref_put(&bss->ref, bss_release);
  746. }
  747. EXPORT_SYMBOL(cfg80211_put_bss);
  748. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  749. {
  750. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  751. struct cfg80211_internal_bss *bss;
  752. if (WARN_ON(!pub))
  753. return;
  754. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  755. spin_lock_bh(&dev->bss_lock);
  756. if (!list_empty(&bss->list)) {
  757. __cfg80211_unlink_bss(dev, bss);
  758. dev->bss_generation++;
  759. }
  760. spin_unlock_bh(&dev->bss_lock);
  761. }
  762. EXPORT_SYMBOL(cfg80211_unlink_bss);
  763. #ifdef CONFIG_CFG80211_WEXT
  764. int cfg80211_wext_siwscan(struct net_device *dev,
  765. struct iw_request_info *info,
  766. union iwreq_data *wrqu, char *extra)
  767. {
  768. struct cfg80211_registered_device *rdev;
  769. struct wiphy *wiphy;
  770. struct iw_scan_req *wreq = NULL;
  771. struct cfg80211_scan_request *creq = NULL;
  772. int i, err, n_channels = 0;
  773. enum ieee80211_band band;
  774. if (!netif_running(dev))
  775. return -ENETDOWN;
  776. if (wrqu->data.length == sizeof(struct iw_scan_req))
  777. wreq = (struct iw_scan_req *)extra;
  778. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  779. if (IS_ERR(rdev))
  780. return PTR_ERR(rdev);
  781. if (rdev->scan_req) {
  782. err = -EBUSY;
  783. goto out;
  784. }
  785. wiphy = &rdev->wiphy;
  786. /* Determine number of channels, needed to allocate creq */
  787. if (wreq && wreq->num_channels)
  788. n_channels = wreq->num_channels;
  789. else {
  790. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  791. if (wiphy->bands[band])
  792. n_channels += wiphy->bands[band]->n_channels;
  793. }
  794. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  795. n_channels * sizeof(void *),
  796. GFP_ATOMIC);
  797. if (!creq) {
  798. err = -ENOMEM;
  799. goto out;
  800. }
  801. creq->wiphy = wiphy;
  802. creq->wdev = dev->ieee80211_ptr;
  803. /* SSIDs come after channels */
  804. creq->ssids = (void *)&creq->channels[n_channels];
  805. creq->n_channels = n_channels;
  806. creq->n_ssids = 1;
  807. creq->scan_start = jiffies;
  808. /* translate "Scan on frequencies" request */
  809. i = 0;
  810. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  811. int j;
  812. if (!wiphy->bands[band])
  813. continue;
  814. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  815. /* ignore disabled channels */
  816. if (wiphy->bands[band]->channels[j].flags &
  817. IEEE80211_CHAN_DISABLED)
  818. continue;
  819. /* If we have a wireless request structure and the
  820. * wireless request specifies frequencies, then search
  821. * for the matching hardware channel.
  822. */
  823. if (wreq && wreq->num_channels) {
  824. int k;
  825. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  826. for (k = 0; k < wreq->num_channels; k++) {
  827. int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
  828. if (wext_freq == wiphy_freq)
  829. goto wext_freq_found;
  830. }
  831. goto wext_freq_not_found;
  832. }
  833. wext_freq_found:
  834. creq->channels[i] = &wiphy->bands[band]->channels[j];
  835. i++;
  836. wext_freq_not_found: ;
  837. }
  838. }
  839. /* No channels found? */
  840. if (!i) {
  841. err = -EINVAL;
  842. goto out;
  843. }
  844. /* Set real number of channels specified in creq->channels[] */
  845. creq->n_channels = i;
  846. /* translate "Scan for SSID" request */
  847. if (wreq) {
  848. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  849. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  850. err = -EINVAL;
  851. goto out;
  852. }
  853. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  854. creq->ssids[0].ssid_len = wreq->essid_len;
  855. }
  856. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  857. creq->n_ssids = 0;
  858. }
  859. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  860. if (wiphy->bands[i])
  861. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  862. rdev->scan_req = creq;
  863. err = rdev_scan(rdev, creq);
  864. if (err) {
  865. rdev->scan_req = NULL;
  866. /* creq will be freed below */
  867. } else {
  868. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  869. /* creq now owned by driver */
  870. creq = NULL;
  871. dev_hold(dev);
  872. }
  873. out:
  874. kfree(creq);
  875. cfg80211_unlock_rdev(rdev);
  876. return err;
  877. }
  878. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  879. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  880. const struct cfg80211_bss_ies *ies,
  881. char **current_ev, char *end_buf)
  882. {
  883. const u8 *pos, *end, *next;
  884. struct iw_event iwe;
  885. if (!ies)
  886. return;
  887. /*
  888. * If needed, fragment the IEs buffer (at IE boundaries) into short
  889. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  890. */
  891. pos = ies->data;
  892. end = pos + ies->len;
  893. while (end - pos > IW_GENERIC_IE_MAX) {
  894. next = pos + 2 + pos[1];
  895. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  896. next = next + 2 + next[1];
  897. memset(&iwe, 0, sizeof(iwe));
  898. iwe.cmd = IWEVGENIE;
  899. iwe.u.data.length = next - pos;
  900. *current_ev = iwe_stream_add_point(info, *current_ev,
  901. end_buf, &iwe,
  902. (void *)pos);
  903. pos = next;
  904. }
  905. if (end > pos) {
  906. memset(&iwe, 0, sizeof(iwe));
  907. iwe.cmd = IWEVGENIE;
  908. iwe.u.data.length = end - pos;
  909. *current_ev = iwe_stream_add_point(info, *current_ev,
  910. end_buf, &iwe,
  911. (void *)pos);
  912. }
  913. }
  914. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  915. {
  916. unsigned long end = jiffies;
  917. if (end >= start)
  918. return jiffies_to_msecs(end - start);
  919. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  920. }
  921. static char *
  922. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  923. struct cfg80211_internal_bss *bss, char *current_ev,
  924. char *end_buf)
  925. {
  926. const struct cfg80211_bss_ies *ies;
  927. struct iw_event iwe;
  928. const u8 *ie;
  929. u8 *buf, *cfg, *p;
  930. int rem, i, sig;
  931. bool ismesh = false;
  932. memset(&iwe, 0, sizeof(iwe));
  933. iwe.cmd = SIOCGIWAP;
  934. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  935. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  936. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  937. IW_EV_ADDR_LEN);
  938. memset(&iwe, 0, sizeof(iwe));
  939. iwe.cmd = SIOCGIWFREQ;
  940. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  941. iwe.u.freq.e = 0;
  942. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  943. IW_EV_FREQ_LEN);
  944. memset(&iwe, 0, sizeof(iwe));
  945. iwe.cmd = SIOCGIWFREQ;
  946. iwe.u.freq.m = bss->pub.channel->center_freq;
  947. iwe.u.freq.e = 6;
  948. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  949. IW_EV_FREQ_LEN);
  950. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  951. memset(&iwe, 0, sizeof(iwe));
  952. iwe.cmd = IWEVQUAL;
  953. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  954. IW_QUAL_NOISE_INVALID |
  955. IW_QUAL_QUAL_UPDATED;
  956. switch (wiphy->signal_type) {
  957. case CFG80211_SIGNAL_TYPE_MBM:
  958. sig = bss->pub.signal / 100;
  959. iwe.u.qual.level = sig;
  960. iwe.u.qual.updated |= IW_QUAL_DBM;
  961. if (sig < -110) /* rather bad */
  962. sig = -110;
  963. else if (sig > -40) /* perfect */
  964. sig = -40;
  965. /* will give a range of 0 .. 70 */
  966. iwe.u.qual.qual = sig + 110;
  967. break;
  968. case CFG80211_SIGNAL_TYPE_UNSPEC:
  969. iwe.u.qual.level = bss->pub.signal;
  970. /* will give range 0 .. 100 */
  971. iwe.u.qual.qual = bss->pub.signal;
  972. break;
  973. default:
  974. /* not reached */
  975. break;
  976. }
  977. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  978. &iwe, IW_EV_QUAL_LEN);
  979. }
  980. memset(&iwe, 0, sizeof(iwe));
  981. iwe.cmd = SIOCGIWENCODE;
  982. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  983. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  984. else
  985. iwe.u.data.flags = IW_ENCODE_DISABLED;
  986. iwe.u.data.length = 0;
  987. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  988. &iwe, "");
  989. rcu_read_lock();
  990. ies = rcu_dereference(bss->pub.ies);
  991. if (ies) {
  992. rem = ies->len;
  993. ie = ies->data;
  994. } else {
  995. rem = 0;
  996. ie = NULL;
  997. }
  998. while (ies && rem >= 2) {
  999. /* invalid data */
  1000. if (ie[1] > rem - 2)
  1001. break;
  1002. switch (ie[0]) {
  1003. case WLAN_EID_SSID:
  1004. memset(&iwe, 0, sizeof(iwe));
  1005. iwe.cmd = SIOCGIWESSID;
  1006. iwe.u.data.length = ie[1];
  1007. iwe.u.data.flags = 1;
  1008. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1009. &iwe, (u8 *)ie + 2);
  1010. break;
  1011. case WLAN_EID_MESH_ID:
  1012. memset(&iwe, 0, sizeof(iwe));
  1013. iwe.cmd = SIOCGIWESSID;
  1014. iwe.u.data.length = ie[1];
  1015. iwe.u.data.flags = 1;
  1016. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1017. &iwe, (u8 *)ie + 2);
  1018. break;
  1019. case WLAN_EID_MESH_CONFIG:
  1020. ismesh = true;
  1021. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  1022. break;
  1023. buf = kmalloc(50, GFP_ATOMIC);
  1024. if (!buf)
  1025. break;
  1026. cfg = (u8 *)ie + 2;
  1027. memset(&iwe, 0, sizeof(iwe));
  1028. iwe.cmd = IWEVCUSTOM;
  1029. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  1030. "0x%02X", cfg[0]);
  1031. iwe.u.data.length = strlen(buf);
  1032. current_ev = iwe_stream_add_point(info, current_ev,
  1033. end_buf,
  1034. &iwe, buf);
  1035. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  1036. cfg[1]);
  1037. iwe.u.data.length = strlen(buf);
  1038. current_ev = iwe_stream_add_point(info, current_ev,
  1039. end_buf,
  1040. &iwe, buf);
  1041. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  1042. cfg[2]);
  1043. iwe.u.data.length = strlen(buf);
  1044. current_ev = iwe_stream_add_point(info, current_ev,
  1045. end_buf,
  1046. &iwe, buf);
  1047. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  1048. iwe.u.data.length = strlen(buf);
  1049. current_ev = iwe_stream_add_point(info, current_ev,
  1050. end_buf,
  1051. &iwe, buf);
  1052. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  1053. iwe.u.data.length = strlen(buf);
  1054. current_ev = iwe_stream_add_point(info, current_ev,
  1055. end_buf,
  1056. &iwe, buf);
  1057. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  1058. iwe.u.data.length = strlen(buf);
  1059. current_ev = iwe_stream_add_point(info, current_ev,
  1060. end_buf,
  1061. &iwe, buf);
  1062. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1063. iwe.u.data.length = strlen(buf);
  1064. current_ev = iwe_stream_add_point(info, current_ev,
  1065. end_buf,
  1066. &iwe, buf);
  1067. kfree(buf);
  1068. break;
  1069. case WLAN_EID_SUPP_RATES:
  1070. case WLAN_EID_EXT_SUPP_RATES:
  1071. /* display all supported rates in readable format */
  1072. p = current_ev + iwe_stream_lcp_len(info);
  1073. memset(&iwe, 0, sizeof(iwe));
  1074. iwe.cmd = SIOCGIWRATE;
  1075. /* Those two flags are ignored... */
  1076. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1077. for (i = 0; i < ie[1]; i++) {
  1078. iwe.u.bitrate.value =
  1079. ((ie[i + 2] & 0x7f) * 500000);
  1080. p = iwe_stream_add_value(info, current_ev, p,
  1081. end_buf, &iwe, IW_EV_PARAM_LEN);
  1082. }
  1083. current_ev = p;
  1084. break;
  1085. }
  1086. rem -= ie[1] + 2;
  1087. ie += ie[1] + 2;
  1088. }
  1089. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1090. ismesh) {
  1091. memset(&iwe, 0, sizeof(iwe));
  1092. iwe.cmd = SIOCGIWMODE;
  1093. if (ismesh)
  1094. iwe.u.mode = IW_MODE_MESH;
  1095. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1096. iwe.u.mode = IW_MODE_MASTER;
  1097. else
  1098. iwe.u.mode = IW_MODE_ADHOC;
  1099. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  1100. &iwe, IW_EV_UINT_LEN);
  1101. }
  1102. buf = kmalloc(30, GFP_ATOMIC);
  1103. if (buf) {
  1104. memset(&iwe, 0, sizeof(iwe));
  1105. iwe.cmd = IWEVCUSTOM;
  1106. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  1107. iwe.u.data.length = strlen(buf);
  1108. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1109. &iwe, buf);
  1110. memset(&iwe, 0, sizeof(iwe));
  1111. iwe.cmd = IWEVCUSTOM;
  1112. sprintf(buf, " Last beacon: %ums ago",
  1113. elapsed_jiffies_msecs(bss->ts));
  1114. iwe.u.data.length = strlen(buf);
  1115. current_ev = iwe_stream_add_point(info, current_ev,
  1116. end_buf, &iwe, buf);
  1117. kfree(buf);
  1118. }
  1119. ieee80211_scan_add_ies(info, ies, &current_ev, end_buf);
  1120. rcu_read_unlock();
  1121. return current_ev;
  1122. }
  1123. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  1124. struct iw_request_info *info,
  1125. char *buf, size_t len)
  1126. {
  1127. char *current_ev = buf;
  1128. char *end_buf = buf + len;
  1129. struct cfg80211_internal_bss *bss;
  1130. spin_lock_bh(&dev->bss_lock);
  1131. cfg80211_bss_expire(dev);
  1132. list_for_each_entry(bss, &dev->bss_list, list) {
  1133. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1134. spin_unlock_bh(&dev->bss_lock);
  1135. return -E2BIG;
  1136. }
  1137. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  1138. current_ev, end_buf);
  1139. }
  1140. spin_unlock_bh(&dev->bss_lock);
  1141. return current_ev - buf;
  1142. }
  1143. int cfg80211_wext_giwscan(struct net_device *dev,
  1144. struct iw_request_info *info,
  1145. struct iw_point *data, char *extra)
  1146. {
  1147. struct cfg80211_registered_device *rdev;
  1148. int res;
  1149. if (!netif_running(dev))
  1150. return -ENETDOWN;
  1151. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1152. if (IS_ERR(rdev))
  1153. return PTR_ERR(rdev);
  1154. if (rdev->scan_req) {
  1155. res = -EAGAIN;
  1156. goto out;
  1157. }
  1158. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1159. data->length = 0;
  1160. if (res >= 0) {
  1161. data->length = res;
  1162. res = 0;
  1163. }
  1164. out:
  1165. cfg80211_unlock_rdev(rdev);
  1166. return res;
  1167. }
  1168. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  1169. #endif