scan.c 35 KB

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