scan.c 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cfg80211 scan result handling
  4. *
  5. * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. * Copyright 2016 Intel Deutschland GmbH
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/module.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/wireless.h>
  14. #include <linux/nl80211.h>
  15. #include <linux/etherdevice.h>
  16. #include <net/arp.h>
  17. #include <net/cfg80211.h>
  18. #include <net/cfg80211-wext.h>
  19. #include <net/iw_handler.h>
  20. #include "core.h"
  21. #include "nl80211.h"
  22. #include "wext-compat.h"
  23. #include "rdev-ops.h"
  24. /**
  25. * DOC: BSS tree/list structure
  26. *
  27. * At the top level, the BSS list is kept in both a list in each
  28. * registered device (@bss_list) as well as an RB-tree for faster
  29. * lookup. In the RB-tree, entries can be looked up using their
  30. * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
  31. * for other BSSes.
  32. *
  33. * Due to the possibility of hidden SSIDs, there's a second level
  34. * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
  35. * The hidden_list connects all BSSes belonging to a single AP
  36. * that has a hidden SSID, and connects beacon and probe response
  37. * entries. For a probe response entry for a hidden SSID, the
  38. * hidden_beacon_bss pointer points to the BSS struct holding the
  39. * beacon's information.
  40. *
  41. * Reference counting is done for all these references except for
  42. * the hidden_list, so that a beacon BSS struct that is otherwise
  43. * not referenced has one reference for being on the bss_list and
  44. * one for each probe response entry that points to it using the
  45. * hidden_beacon_bss pointer. When a BSS struct that has such a
  46. * pointer is get/put, the refcount update is also propagated to
  47. * the referenced struct, this ensure that it cannot get removed
  48. * while somebody is using the probe response version.
  49. *
  50. * Note that the hidden_beacon_bss pointer never changes, due to
  51. * the reference counting. Therefore, no locking is needed for
  52. * it.
  53. *
  54. * Also note that the hidden_beacon_bss pointer is only relevant
  55. * if the driver uses something other than the IEs, e.g. private
  56. * data stored stored in the BSS struct, since the beacon IEs are
  57. * also linked into the probe response struct.
  58. */
  59. /*
  60. * Limit the number of BSS entries stored in mac80211. Each one is
  61. * a bit over 4k at most, so this limits to roughly 4-5M of memory.
  62. * If somebody wants to really attack this though, they'd likely
  63. * use small beacons, and only one type of frame, limiting each of
  64. * the entries to a much smaller size (in order to generate more
  65. * entries in total, so overhead is bigger.)
  66. */
  67. static int bss_entries_limit = 1000;
  68. module_param(bss_entries_limit, int, 0644);
  69. MODULE_PARM_DESC(bss_entries_limit,
  70. "limit to number of scan BSS entries (per wiphy, default 1000)");
  71. #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
  72. static void bss_free(struct cfg80211_internal_bss *bss)
  73. {
  74. struct cfg80211_bss_ies *ies;
  75. if (WARN_ON(atomic_read(&bss->hold)))
  76. return;
  77. ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
  78. if (ies && !bss->pub.hidden_beacon_bss)
  79. kfree_rcu(ies, rcu_head);
  80. ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
  81. if (ies)
  82. kfree_rcu(ies, rcu_head);
  83. /*
  84. * This happens when the module is removed, it doesn't
  85. * really matter any more save for completeness
  86. */
  87. if (!list_empty(&bss->hidden_list))
  88. list_del(&bss->hidden_list);
  89. kfree(bss);
  90. }
  91. static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
  92. struct cfg80211_internal_bss *bss)
  93. {
  94. lockdep_assert_held(&rdev->bss_lock);
  95. bss->refcount++;
  96. if (bss->pub.hidden_beacon_bss) {
  97. bss = container_of(bss->pub.hidden_beacon_bss,
  98. struct cfg80211_internal_bss,
  99. pub);
  100. bss->refcount++;
  101. }
  102. }
  103. static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
  104. struct cfg80211_internal_bss *bss)
  105. {
  106. lockdep_assert_held(&rdev->bss_lock);
  107. if (bss->pub.hidden_beacon_bss) {
  108. struct cfg80211_internal_bss *hbss;
  109. hbss = container_of(bss->pub.hidden_beacon_bss,
  110. struct cfg80211_internal_bss,
  111. pub);
  112. hbss->refcount--;
  113. if (hbss->refcount == 0)
  114. bss_free(hbss);
  115. }
  116. bss->refcount--;
  117. if (bss->refcount == 0)
  118. bss_free(bss);
  119. }
  120. static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
  121. struct cfg80211_internal_bss *bss)
  122. {
  123. lockdep_assert_held(&rdev->bss_lock);
  124. if (!list_empty(&bss->hidden_list)) {
  125. /*
  126. * don't remove the beacon entry if it has
  127. * probe responses associated with it
  128. */
  129. if (!bss->pub.hidden_beacon_bss)
  130. return false;
  131. /*
  132. * if it's a probe response entry break its
  133. * link to the other entries in the group
  134. */
  135. list_del_init(&bss->hidden_list);
  136. }
  137. list_del_init(&bss->list);
  138. rb_erase(&bss->rbn, &rdev->bss_tree);
  139. rdev->bss_entries--;
  140. WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
  141. "rdev bss entries[%d]/list[empty:%d] corruption\n",
  142. rdev->bss_entries, list_empty(&rdev->bss_list));
  143. bss_ref_put(rdev, bss);
  144. return true;
  145. }
  146. static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
  147. unsigned long expire_time)
  148. {
  149. struct cfg80211_internal_bss *bss, *tmp;
  150. bool expired = false;
  151. lockdep_assert_held(&rdev->bss_lock);
  152. list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
  153. if (atomic_read(&bss->hold))
  154. continue;
  155. if (!time_after(expire_time, bss->ts))
  156. continue;
  157. if (__cfg80211_unlink_bss(rdev, bss))
  158. expired = true;
  159. }
  160. if (expired)
  161. rdev->bss_generation++;
  162. }
  163. static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
  164. {
  165. struct cfg80211_internal_bss *bss, *oldest = NULL;
  166. bool ret;
  167. lockdep_assert_held(&rdev->bss_lock);
  168. list_for_each_entry(bss, &rdev->bss_list, list) {
  169. if (atomic_read(&bss->hold))
  170. continue;
  171. if (!list_empty(&bss->hidden_list) &&
  172. !bss->pub.hidden_beacon_bss)
  173. continue;
  174. if (oldest && time_before(oldest->ts, bss->ts))
  175. continue;
  176. oldest = bss;
  177. }
  178. if (WARN_ON(!oldest))
  179. return false;
  180. /*
  181. * The callers make sure to increase rdev->bss_generation if anything
  182. * gets removed (and a new entry added), so there's no need to also do
  183. * it here.
  184. */
  185. ret = __cfg80211_unlink_bss(rdev, oldest);
  186. WARN_ON(!ret);
  187. return ret;
  188. }
  189. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
  190. bool send_message)
  191. {
  192. struct cfg80211_scan_request *request;
  193. struct wireless_dev *wdev;
  194. struct sk_buff *msg;
  195. #ifdef CONFIG_CFG80211_WEXT
  196. union iwreq_data wrqu;
  197. #endif
  198. ASSERT_RTNL();
  199. if (rdev->scan_msg) {
  200. nl80211_send_scan_msg(rdev, rdev->scan_msg);
  201. rdev->scan_msg = NULL;
  202. return;
  203. }
  204. request = rdev->scan_req;
  205. if (!request)
  206. return;
  207. wdev = request->wdev;
  208. /*
  209. * This must be before sending the other events!
  210. * Otherwise, wpa_supplicant gets completely confused with
  211. * wext events.
  212. */
  213. if (wdev->netdev)
  214. cfg80211_sme_scan_done(wdev->netdev);
  215. if (!request->info.aborted &&
  216. request->flags & NL80211_SCAN_FLAG_FLUSH) {
  217. /* flush entries from previous scans */
  218. spin_lock_bh(&rdev->bss_lock);
  219. __cfg80211_bss_expire(rdev, request->scan_start);
  220. spin_unlock_bh(&rdev->bss_lock);
  221. }
  222. msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
  223. #ifdef CONFIG_CFG80211_WEXT
  224. if (wdev->netdev && !request->info.aborted) {
  225. memset(&wrqu, 0, sizeof(wrqu));
  226. wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
  227. }
  228. #endif
  229. if (wdev->netdev)
  230. dev_put(wdev->netdev);
  231. rdev->scan_req = NULL;
  232. kfree(request);
  233. if (!send_message)
  234. rdev->scan_msg = msg;
  235. else
  236. nl80211_send_scan_msg(rdev, msg);
  237. }
  238. void __cfg80211_scan_done(struct work_struct *wk)
  239. {
  240. struct cfg80211_registered_device *rdev;
  241. rdev = container_of(wk, struct cfg80211_registered_device,
  242. scan_done_wk);
  243. rtnl_lock();
  244. ___cfg80211_scan_done(rdev, true);
  245. rtnl_unlock();
  246. }
  247. void cfg80211_scan_done(struct cfg80211_scan_request *request,
  248. struct cfg80211_scan_info *info)
  249. {
  250. trace_cfg80211_scan_done(request, info);
  251. WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
  252. request->info = *info;
  253. request->notified = true;
  254. queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
  255. }
  256. EXPORT_SYMBOL(cfg80211_scan_done);
  257. void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
  258. struct cfg80211_sched_scan_request *req)
  259. {
  260. ASSERT_RTNL();
  261. list_add_rcu(&req->list, &rdev->sched_scan_req_list);
  262. }
  263. static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
  264. struct cfg80211_sched_scan_request *req)
  265. {
  266. ASSERT_RTNL();
  267. list_del_rcu(&req->list);
  268. kfree_rcu(req, rcu_head);
  269. }
  270. static struct cfg80211_sched_scan_request *
  271. cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
  272. {
  273. struct cfg80211_sched_scan_request *pos;
  274. WARN_ON_ONCE(!rcu_read_lock_held() && !lockdep_rtnl_is_held());
  275. list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list) {
  276. if (pos->reqid == reqid)
  277. return pos;
  278. }
  279. return NULL;
  280. }
  281. /*
  282. * Determines if a scheduled scan request can be handled. When a legacy
  283. * scheduled scan is running no other scheduled scan is allowed regardless
  284. * whether the request is for legacy or multi-support scan. When a multi-support
  285. * scheduled scan is running a request for legacy scan is not allowed. In this
  286. * case a request for multi-support scan can be handled if resources are
  287. * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
  288. */
  289. int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
  290. bool want_multi)
  291. {
  292. struct cfg80211_sched_scan_request *pos;
  293. int i = 0;
  294. list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
  295. /* request id zero means legacy in progress */
  296. if (!i && !pos->reqid)
  297. return -EINPROGRESS;
  298. i++;
  299. }
  300. if (i) {
  301. /* no legacy allowed when multi request(s) are active */
  302. if (!want_multi)
  303. return -EINPROGRESS;
  304. /* resource limit reached */
  305. if (i == rdev->wiphy.max_sched_scan_reqs)
  306. return -ENOSPC;
  307. }
  308. return 0;
  309. }
  310. void cfg80211_sched_scan_results_wk(struct work_struct *work)
  311. {
  312. struct cfg80211_registered_device *rdev;
  313. struct cfg80211_sched_scan_request *req, *tmp;
  314. rdev = container_of(work, struct cfg80211_registered_device,
  315. sched_scan_res_wk);
  316. rtnl_lock();
  317. list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
  318. if (req->report_results) {
  319. req->report_results = false;
  320. if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
  321. /* flush entries from previous scans */
  322. spin_lock_bh(&rdev->bss_lock);
  323. __cfg80211_bss_expire(rdev, req->scan_start);
  324. spin_unlock_bh(&rdev->bss_lock);
  325. req->scan_start = jiffies;
  326. }
  327. nl80211_send_sched_scan(req,
  328. NL80211_CMD_SCHED_SCAN_RESULTS);
  329. }
  330. }
  331. rtnl_unlock();
  332. }
  333. void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
  334. {
  335. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  336. struct cfg80211_sched_scan_request *request;
  337. trace_cfg80211_sched_scan_results(wiphy, reqid);
  338. /* ignore if we're not scanning */
  339. rcu_read_lock();
  340. request = cfg80211_find_sched_scan_req(rdev, reqid);
  341. if (request) {
  342. request->report_results = true;
  343. queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
  344. }
  345. rcu_read_unlock();
  346. }
  347. EXPORT_SYMBOL(cfg80211_sched_scan_results);
  348. void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
  349. {
  350. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  351. ASSERT_RTNL();
  352. trace_cfg80211_sched_scan_stopped(wiphy, reqid);
  353. __cfg80211_stop_sched_scan(rdev, reqid, true);
  354. }
  355. EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
  356. void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
  357. {
  358. rtnl_lock();
  359. cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
  360. rtnl_unlock();
  361. }
  362. EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
  363. int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
  364. struct cfg80211_sched_scan_request *req,
  365. bool driver_initiated)
  366. {
  367. ASSERT_RTNL();
  368. if (!driver_initiated) {
  369. int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
  370. if (err)
  371. return err;
  372. }
  373. nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
  374. cfg80211_del_sched_scan_req(rdev, req);
  375. return 0;
  376. }
  377. int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
  378. u64 reqid, bool driver_initiated)
  379. {
  380. struct cfg80211_sched_scan_request *sched_scan_req;
  381. ASSERT_RTNL();
  382. sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
  383. if (!sched_scan_req)
  384. return -ENOENT;
  385. return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
  386. driver_initiated);
  387. }
  388. void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
  389. unsigned long age_secs)
  390. {
  391. struct cfg80211_internal_bss *bss;
  392. unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
  393. spin_lock_bh(&rdev->bss_lock);
  394. list_for_each_entry(bss, &rdev->bss_list, list)
  395. bss->ts -= age_jiffies;
  396. spin_unlock_bh(&rdev->bss_lock);
  397. }
  398. void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
  399. {
  400. __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
  401. }
  402. const u8 *cfg80211_find_ie_match(u8 eid, const u8 *ies, int len,
  403. const u8 *match, int match_len,
  404. int match_offset)
  405. {
  406. const struct element *elem;
  407. /* match_offset can't be smaller than 2, unless match_len is
  408. * zero, in which case match_offset must be zero as well.
  409. */
  410. if (WARN_ON((match_len && match_offset < 2) ||
  411. (!match_len && match_offset)))
  412. return NULL;
  413. for_each_element_id(elem, eid, ies, len) {
  414. if (elem->datalen >= match_offset - 2 + match_len &&
  415. !memcmp(elem->data + match_offset - 2, match, match_len))
  416. return (void *)elem;
  417. }
  418. return NULL;
  419. }
  420. EXPORT_SYMBOL(cfg80211_find_ie_match);
  421. const u8 *cfg80211_find_vendor_ie(unsigned int oui, int oui_type,
  422. const u8 *ies, int len)
  423. {
  424. const u8 *ie;
  425. u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
  426. int match_len = (oui_type < 0) ? 3 : sizeof(match);
  427. if (WARN_ON(oui_type > 0xff))
  428. return NULL;
  429. ie = cfg80211_find_ie_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
  430. match, match_len, 2);
  431. if (ie && (ie[1] < 4))
  432. return NULL;
  433. return ie;
  434. }
  435. EXPORT_SYMBOL(cfg80211_find_vendor_ie);
  436. static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
  437. const u8 *ssid, size_t ssid_len)
  438. {
  439. const struct cfg80211_bss_ies *ies;
  440. const u8 *ssidie;
  441. if (bssid && !ether_addr_equal(a->bssid, bssid))
  442. return false;
  443. if (!ssid)
  444. return true;
  445. ies = rcu_access_pointer(a->ies);
  446. if (!ies)
  447. return false;
  448. ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  449. if (!ssidie)
  450. return false;
  451. if (ssidie[1] != ssid_len)
  452. return false;
  453. return memcmp(ssidie + 2, ssid, ssid_len) == 0;
  454. }
  455. /**
  456. * enum bss_compare_mode - BSS compare mode
  457. * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
  458. * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
  459. * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
  460. */
  461. enum bss_compare_mode {
  462. BSS_CMP_REGULAR,
  463. BSS_CMP_HIDE_ZLEN,
  464. BSS_CMP_HIDE_NUL,
  465. };
  466. static int cmp_bss(struct cfg80211_bss *a,
  467. struct cfg80211_bss *b,
  468. enum bss_compare_mode mode)
  469. {
  470. const struct cfg80211_bss_ies *a_ies, *b_ies;
  471. const u8 *ie1 = NULL;
  472. const u8 *ie2 = NULL;
  473. int i, r;
  474. if (a->channel != b->channel)
  475. return b->channel->center_freq - a->channel->center_freq;
  476. a_ies = rcu_access_pointer(a->ies);
  477. if (!a_ies)
  478. return -1;
  479. b_ies = rcu_access_pointer(b->ies);
  480. if (!b_ies)
  481. return 1;
  482. if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
  483. ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  484. a_ies->data, a_ies->len);
  485. if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
  486. ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  487. b_ies->data, b_ies->len);
  488. if (ie1 && ie2) {
  489. int mesh_id_cmp;
  490. if (ie1[1] == ie2[1])
  491. mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  492. else
  493. mesh_id_cmp = ie2[1] - ie1[1];
  494. ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  495. a_ies->data, a_ies->len);
  496. ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  497. b_ies->data, b_ies->len);
  498. if (ie1 && ie2) {
  499. if (mesh_id_cmp)
  500. return mesh_id_cmp;
  501. if (ie1[1] != ie2[1])
  502. return ie2[1] - ie1[1];
  503. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  504. }
  505. }
  506. r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
  507. if (r)
  508. return r;
  509. ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
  510. ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
  511. if (!ie1 && !ie2)
  512. return 0;
  513. /*
  514. * Note that with "hide_ssid", the function returns a match if
  515. * the already-present BSS ("b") is a hidden SSID beacon for
  516. * the new BSS ("a").
  517. */
  518. /* sort missing IE before (left of) present IE */
  519. if (!ie1)
  520. return -1;
  521. if (!ie2)
  522. return 1;
  523. switch (mode) {
  524. case BSS_CMP_HIDE_ZLEN:
  525. /*
  526. * In ZLEN mode we assume the BSS entry we're
  527. * looking for has a zero-length SSID. So if
  528. * the one we're looking at right now has that,
  529. * return 0. Otherwise, return the difference
  530. * in length, but since we're looking for the
  531. * 0-length it's really equivalent to returning
  532. * the length of the one we're looking at.
  533. *
  534. * No content comparison is needed as we assume
  535. * the content length is zero.
  536. */
  537. return ie2[1];
  538. case BSS_CMP_REGULAR:
  539. default:
  540. /* sort by length first, then by contents */
  541. if (ie1[1] != ie2[1])
  542. return ie2[1] - ie1[1];
  543. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  544. case BSS_CMP_HIDE_NUL:
  545. if (ie1[1] != ie2[1])
  546. return ie2[1] - ie1[1];
  547. /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
  548. for (i = 0; i < ie2[1]; i++)
  549. if (ie2[i + 2])
  550. return -1;
  551. return 0;
  552. }
  553. }
  554. static bool cfg80211_bss_type_match(u16 capability,
  555. enum nl80211_band band,
  556. enum ieee80211_bss_type bss_type)
  557. {
  558. bool ret = true;
  559. u16 mask, val;
  560. if (bss_type == IEEE80211_BSS_TYPE_ANY)
  561. return ret;
  562. if (band == NL80211_BAND_60GHZ) {
  563. mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
  564. switch (bss_type) {
  565. case IEEE80211_BSS_TYPE_ESS:
  566. val = WLAN_CAPABILITY_DMG_TYPE_AP;
  567. break;
  568. case IEEE80211_BSS_TYPE_PBSS:
  569. val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
  570. break;
  571. case IEEE80211_BSS_TYPE_IBSS:
  572. val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
  573. break;
  574. default:
  575. return false;
  576. }
  577. } else {
  578. mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
  579. switch (bss_type) {
  580. case IEEE80211_BSS_TYPE_ESS:
  581. val = WLAN_CAPABILITY_ESS;
  582. break;
  583. case IEEE80211_BSS_TYPE_IBSS:
  584. val = WLAN_CAPABILITY_IBSS;
  585. break;
  586. case IEEE80211_BSS_TYPE_MBSS:
  587. val = 0;
  588. break;
  589. default:
  590. return false;
  591. }
  592. }
  593. ret = ((capability & mask) == val);
  594. return ret;
  595. }
  596. /* Returned bss is reference counted and must be cleaned up appropriately. */
  597. struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
  598. struct ieee80211_channel *channel,
  599. const u8 *bssid,
  600. const u8 *ssid, size_t ssid_len,
  601. enum ieee80211_bss_type bss_type,
  602. enum ieee80211_privacy privacy)
  603. {
  604. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  605. struct cfg80211_internal_bss *bss, *res = NULL;
  606. unsigned long now = jiffies;
  607. int bss_privacy;
  608. trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
  609. privacy);
  610. spin_lock_bh(&rdev->bss_lock);
  611. list_for_each_entry(bss, &rdev->bss_list, list) {
  612. if (!cfg80211_bss_type_match(bss->pub.capability,
  613. bss->pub.channel->band, bss_type))
  614. continue;
  615. bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
  616. if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
  617. (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
  618. continue;
  619. if (channel && bss->pub.channel != channel)
  620. continue;
  621. if (!is_valid_ether_addr(bss->pub.bssid))
  622. continue;
  623. /* Don't get expired BSS structs */
  624. if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
  625. !atomic_read(&bss->hold))
  626. continue;
  627. if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
  628. res = bss;
  629. bss_ref_get(rdev, res);
  630. break;
  631. }
  632. }
  633. spin_unlock_bh(&rdev->bss_lock);
  634. if (!res)
  635. return NULL;
  636. trace_cfg80211_return_bss(&res->pub);
  637. return &res->pub;
  638. }
  639. EXPORT_SYMBOL(cfg80211_get_bss);
  640. static void rb_insert_bss(struct cfg80211_registered_device *rdev,
  641. struct cfg80211_internal_bss *bss)
  642. {
  643. struct rb_node **p = &rdev->bss_tree.rb_node;
  644. struct rb_node *parent = NULL;
  645. struct cfg80211_internal_bss *tbss;
  646. int cmp;
  647. while (*p) {
  648. parent = *p;
  649. tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
  650. cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
  651. if (WARN_ON(!cmp)) {
  652. /* will sort of leak this BSS */
  653. return;
  654. }
  655. if (cmp < 0)
  656. p = &(*p)->rb_left;
  657. else
  658. p = &(*p)->rb_right;
  659. }
  660. rb_link_node(&bss->rbn, parent, p);
  661. rb_insert_color(&bss->rbn, &rdev->bss_tree);
  662. }
  663. static struct cfg80211_internal_bss *
  664. rb_find_bss(struct cfg80211_registered_device *rdev,
  665. struct cfg80211_internal_bss *res,
  666. enum bss_compare_mode mode)
  667. {
  668. struct rb_node *n = rdev->bss_tree.rb_node;
  669. struct cfg80211_internal_bss *bss;
  670. int r;
  671. while (n) {
  672. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  673. r = cmp_bss(&res->pub, &bss->pub, mode);
  674. if (r == 0)
  675. return bss;
  676. else if (r < 0)
  677. n = n->rb_left;
  678. else
  679. n = n->rb_right;
  680. }
  681. return NULL;
  682. }
  683. static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
  684. struct cfg80211_internal_bss *new)
  685. {
  686. const struct cfg80211_bss_ies *ies;
  687. struct cfg80211_internal_bss *bss;
  688. const u8 *ie;
  689. int i, ssidlen;
  690. u8 fold = 0;
  691. u32 n_entries = 0;
  692. ies = rcu_access_pointer(new->pub.beacon_ies);
  693. if (WARN_ON(!ies))
  694. return false;
  695. ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  696. if (!ie) {
  697. /* nothing to do */
  698. return true;
  699. }
  700. ssidlen = ie[1];
  701. for (i = 0; i < ssidlen; i++)
  702. fold |= ie[2 + i];
  703. if (fold) {
  704. /* not a hidden SSID */
  705. return true;
  706. }
  707. /* This is the bad part ... */
  708. list_for_each_entry(bss, &rdev->bss_list, list) {
  709. /*
  710. * we're iterating all the entries anyway, so take the
  711. * opportunity to validate the list length accounting
  712. */
  713. n_entries++;
  714. if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
  715. continue;
  716. if (bss->pub.channel != new->pub.channel)
  717. continue;
  718. if (bss->pub.scan_width != new->pub.scan_width)
  719. continue;
  720. if (rcu_access_pointer(bss->pub.beacon_ies))
  721. continue;
  722. ies = rcu_access_pointer(bss->pub.ies);
  723. if (!ies)
  724. continue;
  725. ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  726. if (!ie)
  727. continue;
  728. if (ssidlen && ie[1] != ssidlen)
  729. continue;
  730. if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
  731. continue;
  732. if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
  733. list_del(&bss->hidden_list);
  734. /* combine them */
  735. list_add(&bss->hidden_list, &new->hidden_list);
  736. bss->pub.hidden_beacon_bss = &new->pub;
  737. new->refcount += bss->refcount;
  738. rcu_assign_pointer(bss->pub.beacon_ies,
  739. new->pub.beacon_ies);
  740. }
  741. WARN_ONCE(n_entries != rdev->bss_entries,
  742. "rdev bss entries[%d]/list[len:%d] corruption\n",
  743. rdev->bss_entries, n_entries);
  744. return true;
  745. }
  746. /* Returned bss is reference counted and must be cleaned up appropriately. */
  747. static struct cfg80211_internal_bss *
  748. cfg80211_bss_update(struct cfg80211_registered_device *rdev,
  749. struct cfg80211_internal_bss *tmp,
  750. bool signal_valid)
  751. {
  752. struct cfg80211_internal_bss *found = NULL;
  753. if (WARN_ON(!tmp->pub.channel))
  754. return NULL;
  755. tmp->ts = jiffies;
  756. spin_lock_bh(&rdev->bss_lock);
  757. if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
  758. spin_unlock_bh(&rdev->bss_lock);
  759. return NULL;
  760. }
  761. found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
  762. if (found) {
  763. /* Update IEs */
  764. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  765. const struct cfg80211_bss_ies *old;
  766. old = rcu_access_pointer(found->pub.proberesp_ies);
  767. rcu_assign_pointer(found->pub.proberesp_ies,
  768. tmp->pub.proberesp_ies);
  769. /* Override possible earlier Beacon frame IEs */
  770. rcu_assign_pointer(found->pub.ies,
  771. tmp->pub.proberesp_ies);
  772. if (old)
  773. kfree_rcu((struct cfg80211_bss_ies *)old,
  774. rcu_head);
  775. } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
  776. const struct cfg80211_bss_ies *old;
  777. struct cfg80211_internal_bss *bss;
  778. if (found->pub.hidden_beacon_bss &&
  779. !list_empty(&found->hidden_list)) {
  780. const struct cfg80211_bss_ies *f;
  781. /*
  782. * The found BSS struct is one of the probe
  783. * response members of a group, but we're
  784. * receiving a beacon (beacon_ies in the tmp
  785. * bss is used). This can only mean that the
  786. * AP changed its beacon from not having an
  787. * SSID to showing it, which is confusing so
  788. * drop this information.
  789. */
  790. f = rcu_access_pointer(tmp->pub.beacon_ies);
  791. kfree_rcu((struct cfg80211_bss_ies *)f,
  792. rcu_head);
  793. goto drop;
  794. }
  795. old = rcu_access_pointer(found->pub.beacon_ies);
  796. rcu_assign_pointer(found->pub.beacon_ies,
  797. tmp->pub.beacon_ies);
  798. /* Override IEs if they were from a beacon before */
  799. if (old == rcu_access_pointer(found->pub.ies))
  800. rcu_assign_pointer(found->pub.ies,
  801. tmp->pub.beacon_ies);
  802. /* Assign beacon IEs to all sub entries */
  803. list_for_each_entry(bss, &found->hidden_list,
  804. hidden_list) {
  805. const struct cfg80211_bss_ies *ies;
  806. ies = rcu_access_pointer(bss->pub.beacon_ies);
  807. WARN_ON(ies != old);
  808. rcu_assign_pointer(bss->pub.beacon_ies,
  809. tmp->pub.beacon_ies);
  810. }
  811. if (old)
  812. kfree_rcu((struct cfg80211_bss_ies *)old,
  813. rcu_head);
  814. }
  815. found->pub.beacon_interval = tmp->pub.beacon_interval;
  816. /*
  817. * don't update the signal if beacon was heard on
  818. * adjacent channel.
  819. */
  820. if (signal_valid)
  821. found->pub.signal = tmp->pub.signal;
  822. found->pub.capability = tmp->pub.capability;
  823. found->ts = tmp->ts;
  824. found->ts_boottime = tmp->ts_boottime;
  825. found->parent_tsf = tmp->parent_tsf;
  826. found->pub.chains = tmp->pub.chains;
  827. memcpy(found->pub.chain_signal, tmp->pub.chain_signal,
  828. IEEE80211_MAX_CHAINS);
  829. ether_addr_copy(found->parent_bssid, tmp->parent_bssid);
  830. } else {
  831. struct cfg80211_internal_bss *new;
  832. struct cfg80211_internal_bss *hidden;
  833. struct cfg80211_bss_ies *ies;
  834. /*
  835. * create a copy -- the "res" variable that is passed in
  836. * is allocated on the stack since it's not needed in the
  837. * more common case of an update
  838. */
  839. new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
  840. GFP_ATOMIC);
  841. if (!new) {
  842. ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
  843. if (ies)
  844. kfree_rcu(ies, rcu_head);
  845. ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
  846. if (ies)
  847. kfree_rcu(ies, rcu_head);
  848. goto drop;
  849. }
  850. memcpy(new, tmp, sizeof(*new));
  851. new->refcount = 1;
  852. INIT_LIST_HEAD(&new->hidden_list);
  853. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  854. hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
  855. if (!hidden)
  856. hidden = rb_find_bss(rdev, tmp,
  857. BSS_CMP_HIDE_NUL);
  858. if (hidden) {
  859. new->pub.hidden_beacon_bss = &hidden->pub;
  860. list_add(&new->hidden_list,
  861. &hidden->hidden_list);
  862. hidden->refcount++;
  863. rcu_assign_pointer(new->pub.beacon_ies,
  864. hidden->pub.beacon_ies);
  865. }
  866. } else {
  867. /*
  868. * Ok so we found a beacon, and don't have an entry. If
  869. * it's a beacon with hidden SSID, we might be in for an
  870. * expensive search for any probe responses that should
  871. * be grouped with this beacon for updates ...
  872. */
  873. if (!cfg80211_combine_bsses(rdev, new)) {
  874. kfree(new);
  875. goto drop;
  876. }
  877. }
  878. if (rdev->bss_entries >= bss_entries_limit &&
  879. !cfg80211_bss_expire_oldest(rdev)) {
  880. kfree(new);
  881. goto drop;
  882. }
  883. list_add_tail(&new->list, &rdev->bss_list);
  884. rdev->bss_entries++;
  885. rb_insert_bss(rdev, new);
  886. found = new;
  887. }
  888. rdev->bss_generation++;
  889. bss_ref_get(rdev, found);
  890. spin_unlock_bh(&rdev->bss_lock);
  891. return found;
  892. drop:
  893. spin_unlock_bh(&rdev->bss_lock);
  894. return NULL;
  895. }
  896. /*
  897. * Update RX channel information based on the available frame payload
  898. * information. This is mainly for the 2.4 GHz band where frames can be received
  899. * from neighboring channels and the Beacon frames use the DSSS Parameter Set
  900. * element to indicate the current (transmitting) channel, but this might also
  901. * be needed on other bands if RX frequency does not match with the actual
  902. * operating channel of a BSS.
  903. */
  904. static struct ieee80211_channel *
  905. cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
  906. struct ieee80211_channel *channel,
  907. enum nl80211_bss_scan_width scan_width)
  908. {
  909. const u8 *tmp;
  910. u32 freq;
  911. int channel_number = -1;
  912. struct ieee80211_channel *alt_channel;
  913. tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
  914. if (tmp && tmp[1] == 1) {
  915. channel_number = tmp[2];
  916. } else {
  917. tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
  918. if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
  919. struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
  920. channel_number = htop->primary_chan;
  921. }
  922. }
  923. if (channel_number < 0) {
  924. /* No channel information in frame payload */
  925. return channel;
  926. }
  927. freq = ieee80211_channel_to_frequency(channel_number, channel->band);
  928. alt_channel = ieee80211_get_channel(wiphy, freq);
  929. if (!alt_channel) {
  930. if (channel->band == NL80211_BAND_2GHZ) {
  931. /*
  932. * Better not allow unexpected channels when that could
  933. * be going beyond the 1-11 range (e.g., discovering
  934. * BSS on channel 12 when radio is configured for
  935. * channel 11.
  936. */
  937. return NULL;
  938. }
  939. /* No match for the payload channel number - ignore it */
  940. return channel;
  941. }
  942. if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
  943. scan_width == NL80211_BSS_CHAN_WIDTH_5) {
  944. /*
  945. * Ignore channel number in 5 and 10 MHz channels where there
  946. * may not be an n:1 or 1:n mapping between frequencies and
  947. * channel numbers.
  948. */
  949. return channel;
  950. }
  951. /*
  952. * Use the channel determined through the payload channel number
  953. * instead of the RX channel reported by the driver.
  954. */
  955. if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
  956. return NULL;
  957. return alt_channel;
  958. }
  959. /* Returned bss is reference counted and must be cleaned up appropriately. */
  960. struct cfg80211_bss *
  961. cfg80211_inform_bss_data(struct wiphy *wiphy,
  962. struct cfg80211_inform_bss *data,
  963. enum cfg80211_bss_frame_type ftype,
  964. const u8 *bssid, u64 tsf, u16 capability,
  965. u16 beacon_interval, const u8 *ie, size_t ielen,
  966. gfp_t gfp)
  967. {
  968. struct cfg80211_bss_ies *ies;
  969. struct ieee80211_channel *channel;
  970. struct cfg80211_internal_bss tmp = {}, *res;
  971. int bss_type;
  972. bool signal_valid;
  973. if (WARN_ON(!wiphy))
  974. return NULL;
  975. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  976. (data->signal < 0 || data->signal > 100)))
  977. return NULL;
  978. channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
  979. data->scan_width);
  980. if (!channel)
  981. return NULL;
  982. memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
  983. tmp.pub.channel = channel;
  984. tmp.pub.scan_width = data->scan_width;
  985. tmp.pub.signal = data->signal;
  986. tmp.pub.beacon_interval = beacon_interval;
  987. tmp.pub.capability = capability;
  988. tmp.ts_boottime = data->boottime_ns;
  989. /*
  990. * If we do not know here whether the IEs are from a Beacon or Probe
  991. * Response frame, we need to pick one of the options and only use it
  992. * with the driver that does not provide the full Beacon/Probe Response
  993. * frame. Use Beacon frame pointer to avoid indicating that this should
  994. * override the IEs pointer should we have received an earlier
  995. * indication of Probe Response data.
  996. */
  997. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  998. if (!ies)
  999. return NULL;
  1000. ies->len = ielen;
  1001. ies->tsf = tsf;
  1002. ies->from_beacon = false;
  1003. memcpy(ies->data, ie, ielen);
  1004. switch (ftype) {
  1005. case CFG80211_BSS_FTYPE_BEACON:
  1006. ies->from_beacon = true;
  1007. /* fall through to assign */
  1008. case CFG80211_BSS_FTYPE_UNKNOWN:
  1009. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  1010. break;
  1011. case CFG80211_BSS_FTYPE_PRESP:
  1012. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  1013. break;
  1014. }
  1015. rcu_assign_pointer(tmp.pub.ies, ies);
  1016. signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
  1017. wiphy->max_adj_channel_rssi_comp;
  1018. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
  1019. if (!res)
  1020. return NULL;
  1021. if (channel->band == NL80211_BAND_60GHZ) {
  1022. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  1023. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  1024. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  1025. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1026. } else {
  1027. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  1028. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1029. }
  1030. trace_cfg80211_return_bss(&res->pub);
  1031. /* cfg80211_bss_update gives us a referenced result */
  1032. return &res->pub;
  1033. }
  1034. EXPORT_SYMBOL(cfg80211_inform_bss_data);
  1035. /* cfg80211_inform_bss_width_frame helper */
  1036. struct cfg80211_bss *
  1037. cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
  1038. struct cfg80211_inform_bss *data,
  1039. struct ieee80211_mgmt *mgmt, size_t len,
  1040. gfp_t gfp)
  1041. {
  1042. struct cfg80211_internal_bss tmp = {}, *res;
  1043. struct cfg80211_bss_ies *ies;
  1044. struct ieee80211_channel *channel;
  1045. bool signal_valid;
  1046. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  1047. u.probe_resp.variable);
  1048. int bss_type;
  1049. BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
  1050. offsetof(struct ieee80211_mgmt, u.beacon.variable));
  1051. trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
  1052. if (WARN_ON(!mgmt))
  1053. return NULL;
  1054. if (WARN_ON(!wiphy))
  1055. return NULL;
  1056. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  1057. (data->signal < 0 || data->signal > 100)))
  1058. return NULL;
  1059. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  1060. return NULL;
  1061. channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
  1062. ielen, data->chan, data->scan_width);
  1063. if (!channel)
  1064. return NULL;
  1065. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  1066. if (!ies)
  1067. return NULL;
  1068. ies->len = ielen;
  1069. ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  1070. ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
  1071. memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
  1072. if (ieee80211_is_probe_resp(mgmt->frame_control))
  1073. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  1074. else
  1075. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  1076. rcu_assign_pointer(tmp.pub.ies, ies);
  1077. memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
  1078. tmp.pub.channel = channel;
  1079. tmp.pub.scan_width = data->scan_width;
  1080. tmp.pub.signal = data->signal;
  1081. tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  1082. tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  1083. tmp.ts_boottime = data->boottime_ns;
  1084. tmp.parent_tsf = data->parent_tsf;
  1085. tmp.pub.chains = data->chains;
  1086. memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
  1087. ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
  1088. signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
  1089. wiphy->max_adj_channel_rssi_comp;
  1090. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
  1091. if (!res)
  1092. return NULL;
  1093. if (channel->band == NL80211_BAND_60GHZ) {
  1094. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  1095. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  1096. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  1097. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1098. } else {
  1099. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  1100. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1101. }
  1102. trace_cfg80211_return_bss(&res->pub);
  1103. /* cfg80211_bss_update gives us a referenced result */
  1104. return &res->pub;
  1105. }
  1106. EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
  1107. void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1108. {
  1109. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1110. struct cfg80211_internal_bss *bss;
  1111. if (!pub)
  1112. return;
  1113. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1114. spin_lock_bh(&rdev->bss_lock);
  1115. bss_ref_get(rdev, bss);
  1116. spin_unlock_bh(&rdev->bss_lock);
  1117. }
  1118. EXPORT_SYMBOL(cfg80211_ref_bss);
  1119. void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1120. {
  1121. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1122. struct cfg80211_internal_bss *bss;
  1123. if (!pub)
  1124. return;
  1125. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1126. spin_lock_bh(&rdev->bss_lock);
  1127. bss_ref_put(rdev, bss);
  1128. spin_unlock_bh(&rdev->bss_lock);
  1129. }
  1130. EXPORT_SYMBOL(cfg80211_put_bss);
  1131. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1132. {
  1133. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1134. struct cfg80211_internal_bss *bss;
  1135. if (WARN_ON(!pub))
  1136. return;
  1137. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1138. spin_lock_bh(&rdev->bss_lock);
  1139. if (!list_empty(&bss->list)) {
  1140. if (__cfg80211_unlink_bss(rdev, bss))
  1141. rdev->bss_generation++;
  1142. }
  1143. spin_unlock_bh(&rdev->bss_lock);
  1144. }
  1145. EXPORT_SYMBOL(cfg80211_unlink_bss);
  1146. #ifdef CONFIG_CFG80211_WEXT
  1147. static struct cfg80211_registered_device *
  1148. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
  1149. {
  1150. struct cfg80211_registered_device *rdev;
  1151. struct net_device *dev;
  1152. ASSERT_RTNL();
  1153. dev = dev_get_by_index(net, ifindex);
  1154. if (!dev)
  1155. return ERR_PTR(-ENODEV);
  1156. if (dev->ieee80211_ptr)
  1157. rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
  1158. else
  1159. rdev = ERR_PTR(-ENODEV);
  1160. dev_put(dev);
  1161. return rdev;
  1162. }
  1163. int cfg80211_wext_siwscan(struct net_device *dev,
  1164. struct iw_request_info *info,
  1165. union iwreq_data *wrqu, char *extra)
  1166. {
  1167. struct cfg80211_registered_device *rdev;
  1168. struct wiphy *wiphy;
  1169. struct iw_scan_req *wreq = NULL;
  1170. struct cfg80211_scan_request *creq = NULL;
  1171. int i, err, n_channels = 0;
  1172. enum nl80211_band band;
  1173. if (!netif_running(dev))
  1174. return -ENETDOWN;
  1175. if (wrqu->data.length == sizeof(struct iw_scan_req))
  1176. wreq = (struct iw_scan_req *)extra;
  1177. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1178. if (IS_ERR(rdev))
  1179. return PTR_ERR(rdev);
  1180. if (rdev->scan_req || rdev->scan_msg) {
  1181. err = -EBUSY;
  1182. goto out;
  1183. }
  1184. wiphy = &rdev->wiphy;
  1185. /* Determine number of channels, needed to allocate creq */
  1186. if (wreq && wreq->num_channels)
  1187. n_channels = wreq->num_channels;
  1188. else
  1189. n_channels = ieee80211_get_num_supported_channels(wiphy);
  1190. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  1191. n_channels * sizeof(void *),
  1192. GFP_ATOMIC);
  1193. if (!creq) {
  1194. err = -ENOMEM;
  1195. goto out;
  1196. }
  1197. creq->wiphy = wiphy;
  1198. creq->wdev = dev->ieee80211_ptr;
  1199. /* SSIDs come after channels */
  1200. creq->ssids = (void *)&creq->channels[n_channels];
  1201. creq->n_channels = n_channels;
  1202. creq->n_ssids = 1;
  1203. creq->scan_start = jiffies;
  1204. /* translate "Scan on frequencies" request */
  1205. i = 0;
  1206. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  1207. int j;
  1208. if (!wiphy->bands[band])
  1209. continue;
  1210. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  1211. /* ignore disabled channels */
  1212. if (wiphy->bands[band]->channels[j].flags &
  1213. IEEE80211_CHAN_DISABLED)
  1214. continue;
  1215. /* If we have a wireless request structure and the
  1216. * wireless request specifies frequencies, then search
  1217. * for the matching hardware channel.
  1218. */
  1219. if (wreq && wreq->num_channels) {
  1220. int k;
  1221. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  1222. for (k = 0; k < wreq->num_channels; k++) {
  1223. struct iw_freq *freq =
  1224. &wreq->channel_list[k];
  1225. int wext_freq =
  1226. cfg80211_wext_freq(freq);
  1227. if (wext_freq == wiphy_freq)
  1228. goto wext_freq_found;
  1229. }
  1230. goto wext_freq_not_found;
  1231. }
  1232. wext_freq_found:
  1233. creq->channels[i] = &wiphy->bands[band]->channels[j];
  1234. i++;
  1235. wext_freq_not_found: ;
  1236. }
  1237. }
  1238. /* No channels found? */
  1239. if (!i) {
  1240. err = -EINVAL;
  1241. goto out;
  1242. }
  1243. /* Set real number of channels specified in creq->channels[] */
  1244. creq->n_channels = i;
  1245. /* translate "Scan for SSID" request */
  1246. if (wreq) {
  1247. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  1248. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  1249. err = -EINVAL;
  1250. goto out;
  1251. }
  1252. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  1253. creq->ssids[0].ssid_len = wreq->essid_len;
  1254. }
  1255. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  1256. creq->n_ssids = 0;
  1257. }
  1258. for (i = 0; i < NUM_NL80211_BANDS; i++)
  1259. if (wiphy->bands[i])
  1260. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  1261. eth_broadcast_addr(creq->bssid);
  1262. rdev->scan_req = creq;
  1263. err = rdev_scan(rdev, creq);
  1264. if (err) {
  1265. rdev->scan_req = NULL;
  1266. /* creq will be freed below */
  1267. } else {
  1268. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  1269. /* creq now owned by driver */
  1270. creq = NULL;
  1271. dev_hold(dev);
  1272. }
  1273. out:
  1274. kfree(creq);
  1275. return err;
  1276. }
  1277. EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
  1278. static char *ieee80211_scan_add_ies(struct iw_request_info *info,
  1279. const struct cfg80211_bss_ies *ies,
  1280. char *current_ev, char *end_buf)
  1281. {
  1282. const u8 *pos, *end, *next;
  1283. struct iw_event iwe;
  1284. if (!ies)
  1285. return current_ev;
  1286. /*
  1287. * If needed, fragment the IEs buffer (at IE boundaries) into short
  1288. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  1289. */
  1290. pos = ies->data;
  1291. end = pos + ies->len;
  1292. while (end - pos > IW_GENERIC_IE_MAX) {
  1293. next = pos + 2 + pos[1];
  1294. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  1295. next = next + 2 + next[1];
  1296. memset(&iwe, 0, sizeof(iwe));
  1297. iwe.cmd = IWEVGENIE;
  1298. iwe.u.data.length = next - pos;
  1299. current_ev = iwe_stream_add_point_check(info, current_ev,
  1300. end_buf, &iwe,
  1301. (void *)pos);
  1302. if (IS_ERR(current_ev))
  1303. return current_ev;
  1304. pos = next;
  1305. }
  1306. if (end > pos) {
  1307. memset(&iwe, 0, sizeof(iwe));
  1308. iwe.cmd = IWEVGENIE;
  1309. iwe.u.data.length = end - pos;
  1310. current_ev = iwe_stream_add_point_check(info, current_ev,
  1311. end_buf, &iwe,
  1312. (void *)pos);
  1313. if (IS_ERR(current_ev))
  1314. return current_ev;
  1315. }
  1316. return current_ev;
  1317. }
  1318. static char *
  1319. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  1320. struct cfg80211_internal_bss *bss, char *current_ev,
  1321. char *end_buf)
  1322. {
  1323. const struct cfg80211_bss_ies *ies;
  1324. struct iw_event iwe;
  1325. const u8 *ie;
  1326. u8 buf[50];
  1327. u8 *cfg, *p, *tmp;
  1328. int rem, i, sig;
  1329. bool ismesh = false;
  1330. memset(&iwe, 0, sizeof(iwe));
  1331. iwe.cmd = SIOCGIWAP;
  1332. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1333. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  1334. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1335. IW_EV_ADDR_LEN);
  1336. if (IS_ERR(current_ev))
  1337. return current_ev;
  1338. memset(&iwe, 0, sizeof(iwe));
  1339. iwe.cmd = SIOCGIWFREQ;
  1340. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  1341. iwe.u.freq.e = 0;
  1342. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1343. IW_EV_FREQ_LEN);
  1344. if (IS_ERR(current_ev))
  1345. return current_ev;
  1346. memset(&iwe, 0, sizeof(iwe));
  1347. iwe.cmd = SIOCGIWFREQ;
  1348. iwe.u.freq.m = bss->pub.channel->center_freq;
  1349. iwe.u.freq.e = 6;
  1350. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1351. IW_EV_FREQ_LEN);
  1352. if (IS_ERR(current_ev))
  1353. return current_ev;
  1354. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  1355. memset(&iwe, 0, sizeof(iwe));
  1356. iwe.cmd = IWEVQUAL;
  1357. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  1358. IW_QUAL_NOISE_INVALID |
  1359. IW_QUAL_QUAL_UPDATED;
  1360. switch (wiphy->signal_type) {
  1361. case CFG80211_SIGNAL_TYPE_MBM:
  1362. sig = bss->pub.signal / 100;
  1363. iwe.u.qual.level = sig;
  1364. iwe.u.qual.updated |= IW_QUAL_DBM;
  1365. if (sig < -110) /* rather bad */
  1366. sig = -110;
  1367. else if (sig > -40) /* perfect */
  1368. sig = -40;
  1369. /* will give a range of 0 .. 70 */
  1370. iwe.u.qual.qual = sig + 110;
  1371. break;
  1372. case CFG80211_SIGNAL_TYPE_UNSPEC:
  1373. iwe.u.qual.level = bss->pub.signal;
  1374. /* will give range 0 .. 100 */
  1375. iwe.u.qual.qual = bss->pub.signal;
  1376. break;
  1377. default:
  1378. /* not reached */
  1379. break;
  1380. }
  1381. current_ev = iwe_stream_add_event_check(info, current_ev,
  1382. end_buf, &iwe,
  1383. IW_EV_QUAL_LEN);
  1384. if (IS_ERR(current_ev))
  1385. return current_ev;
  1386. }
  1387. memset(&iwe, 0, sizeof(iwe));
  1388. iwe.cmd = SIOCGIWENCODE;
  1389. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  1390. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1391. else
  1392. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1393. iwe.u.data.length = 0;
  1394. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  1395. &iwe, "");
  1396. if (IS_ERR(current_ev))
  1397. return current_ev;
  1398. rcu_read_lock();
  1399. ies = rcu_dereference(bss->pub.ies);
  1400. rem = ies->len;
  1401. ie = ies->data;
  1402. while (rem >= 2) {
  1403. /* invalid data */
  1404. if (ie[1] > rem - 2)
  1405. break;
  1406. switch (ie[0]) {
  1407. case WLAN_EID_SSID:
  1408. memset(&iwe, 0, sizeof(iwe));
  1409. iwe.cmd = SIOCGIWESSID;
  1410. iwe.u.data.length = ie[1];
  1411. iwe.u.data.flags = 1;
  1412. current_ev = iwe_stream_add_point_check(info,
  1413. current_ev,
  1414. end_buf, &iwe,
  1415. (u8 *)ie + 2);
  1416. if (IS_ERR(current_ev))
  1417. goto unlock;
  1418. break;
  1419. case WLAN_EID_MESH_ID:
  1420. memset(&iwe, 0, sizeof(iwe));
  1421. iwe.cmd = SIOCGIWESSID;
  1422. iwe.u.data.length = ie[1];
  1423. iwe.u.data.flags = 1;
  1424. current_ev = iwe_stream_add_point_check(info,
  1425. current_ev,
  1426. end_buf, &iwe,
  1427. (u8 *)ie + 2);
  1428. if (IS_ERR(current_ev))
  1429. goto unlock;
  1430. break;
  1431. case WLAN_EID_MESH_CONFIG:
  1432. ismesh = true;
  1433. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  1434. break;
  1435. cfg = (u8 *)ie + 2;
  1436. memset(&iwe, 0, sizeof(iwe));
  1437. iwe.cmd = IWEVCUSTOM;
  1438. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  1439. "0x%02X", cfg[0]);
  1440. iwe.u.data.length = strlen(buf);
  1441. current_ev = iwe_stream_add_point_check(info,
  1442. current_ev,
  1443. end_buf,
  1444. &iwe, buf);
  1445. if (IS_ERR(current_ev))
  1446. goto unlock;
  1447. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  1448. cfg[1]);
  1449. iwe.u.data.length = strlen(buf);
  1450. current_ev = iwe_stream_add_point_check(info,
  1451. current_ev,
  1452. end_buf,
  1453. &iwe, buf);
  1454. if (IS_ERR(current_ev))
  1455. goto unlock;
  1456. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  1457. cfg[2]);
  1458. iwe.u.data.length = strlen(buf);
  1459. current_ev = iwe_stream_add_point_check(info,
  1460. current_ev,
  1461. end_buf,
  1462. &iwe, buf);
  1463. if (IS_ERR(current_ev))
  1464. goto unlock;
  1465. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  1466. iwe.u.data.length = strlen(buf);
  1467. current_ev = iwe_stream_add_point_check(info,
  1468. current_ev,
  1469. end_buf,
  1470. &iwe, buf);
  1471. if (IS_ERR(current_ev))
  1472. goto unlock;
  1473. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  1474. iwe.u.data.length = strlen(buf);
  1475. current_ev = iwe_stream_add_point_check(info,
  1476. current_ev,
  1477. end_buf,
  1478. &iwe, buf);
  1479. if (IS_ERR(current_ev))
  1480. goto unlock;
  1481. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  1482. iwe.u.data.length = strlen(buf);
  1483. current_ev = iwe_stream_add_point_check(info,
  1484. current_ev,
  1485. end_buf,
  1486. &iwe, buf);
  1487. if (IS_ERR(current_ev))
  1488. goto unlock;
  1489. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1490. iwe.u.data.length = strlen(buf);
  1491. current_ev = iwe_stream_add_point_check(info,
  1492. current_ev,
  1493. end_buf,
  1494. &iwe, buf);
  1495. if (IS_ERR(current_ev))
  1496. goto unlock;
  1497. break;
  1498. case WLAN_EID_SUPP_RATES:
  1499. case WLAN_EID_EXT_SUPP_RATES:
  1500. /* display all supported rates in readable format */
  1501. p = current_ev + iwe_stream_lcp_len(info);
  1502. memset(&iwe, 0, sizeof(iwe));
  1503. iwe.cmd = SIOCGIWRATE;
  1504. /* Those two flags are ignored... */
  1505. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1506. for (i = 0; i < ie[1]; i++) {
  1507. iwe.u.bitrate.value =
  1508. ((ie[i + 2] & 0x7f) * 500000);
  1509. tmp = p;
  1510. p = iwe_stream_add_value(info, current_ev, p,
  1511. end_buf, &iwe,
  1512. IW_EV_PARAM_LEN);
  1513. if (p == tmp) {
  1514. current_ev = ERR_PTR(-E2BIG);
  1515. goto unlock;
  1516. }
  1517. }
  1518. current_ev = p;
  1519. break;
  1520. }
  1521. rem -= ie[1] + 2;
  1522. ie += ie[1] + 2;
  1523. }
  1524. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1525. ismesh) {
  1526. memset(&iwe, 0, sizeof(iwe));
  1527. iwe.cmd = SIOCGIWMODE;
  1528. if (ismesh)
  1529. iwe.u.mode = IW_MODE_MESH;
  1530. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1531. iwe.u.mode = IW_MODE_MASTER;
  1532. else
  1533. iwe.u.mode = IW_MODE_ADHOC;
  1534. current_ev = iwe_stream_add_event_check(info, current_ev,
  1535. end_buf, &iwe,
  1536. IW_EV_UINT_LEN);
  1537. if (IS_ERR(current_ev))
  1538. goto unlock;
  1539. }
  1540. memset(&iwe, 0, sizeof(iwe));
  1541. iwe.cmd = IWEVCUSTOM;
  1542. sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
  1543. iwe.u.data.length = strlen(buf);
  1544. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  1545. &iwe, buf);
  1546. if (IS_ERR(current_ev))
  1547. goto unlock;
  1548. memset(&iwe, 0, sizeof(iwe));
  1549. iwe.cmd = IWEVCUSTOM;
  1550. sprintf(buf, " Last beacon: %ums ago",
  1551. elapsed_jiffies_msecs(bss->ts));
  1552. iwe.u.data.length = strlen(buf);
  1553. current_ev = iwe_stream_add_point_check(info, current_ev,
  1554. end_buf, &iwe, buf);
  1555. if (IS_ERR(current_ev))
  1556. goto unlock;
  1557. current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
  1558. unlock:
  1559. rcu_read_unlock();
  1560. return current_ev;
  1561. }
  1562. static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
  1563. struct iw_request_info *info,
  1564. char *buf, size_t len)
  1565. {
  1566. char *current_ev = buf;
  1567. char *end_buf = buf + len;
  1568. struct cfg80211_internal_bss *bss;
  1569. int err = 0;
  1570. spin_lock_bh(&rdev->bss_lock);
  1571. cfg80211_bss_expire(rdev);
  1572. list_for_each_entry(bss, &rdev->bss_list, list) {
  1573. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1574. err = -E2BIG;
  1575. break;
  1576. }
  1577. current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
  1578. current_ev, end_buf);
  1579. if (IS_ERR(current_ev)) {
  1580. err = PTR_ERR(current_ev);
  1581. break;
  1582. }
  1583. }
  1584. spin_unlock_bh(&rdev->bss_lock);
  1585. if (err)
  1586. return err;
  1587. return current_ev - buf;
  1588. }
  1589. int cfg80211_wext_giwscan(struct net_device *dev,
  1590. struct iw_request_info *info,
  1591. struct iw_point *data, char *extra)
  1592. {
  1593. struct cfg80211_registered_device *rdev;
  1594. int res;
  1595. if (!netif_running(dev))
  1596. return -ENETDOWN;
  1597. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1598. if (IS_ERR(rdev))
  1599. return PTR_ERR(rdev);
  1600. if (rdev->scan_req || rdev->scan_msg)
  1601. return -EAGAIN;
  1602. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1603. data->length = 0;
  1604. if (res >= 0) {
  1605. data->length = res;
  1606. res = 0;
  1607. }
  1608. return res;
  1609. }
  1610. EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
  1611. #endif