scan.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839
  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. /* match_offset can't be smaller than 2, unless match_len is
  407. * zero, in which case match_offset must be zero as well.
  408. */
  409. if (WARN_ON((match_len && match_offset < 2) ||
  410. (!match_len && match_offset)))
  411. return NULL;
  412. while (len >= 2 && len >= ies[1] + 2) {
  413. if ((ies[0] == eid) &&
  414. (ies[1] + 2 >= match_offset + match_len) &&
  415. !memcmp(ies + match_offset, match, match_len))
  416. return ies;
  417. len -= ies[1] + 2;
  418. ies += ies[1] + 2;
  419. }
  420. return NULL;
  421. }
  422. EXPORT_SYMBOL(cfg80211_find_ie_match);
  423. const u8 *cfg80211_find_vendor_ie(unsigned int oui, int oui_type,
  424. const u8 *ies, int len)
  425. {
  426. const u8 *ie;
  427. u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
  428. int match_len = (oui_type < 0) ? 3 : sizeof(match);
  429. if (WARN_ON(oui_type > 0xff))
  430. return NULL;
  431. ie = cfg80211_find_ie_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
  432. match, match_len, 2);
  433. if (ie && (ie[1] < 4))
  434. return NULL;
  435. return ie;
  436. }
  437. EXPORT_SYMBOL(cfg80211_find_vendor_ie);
  438. static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
  439. const u8 *ssid, size_t ssid_len)
  440. {
  441. const struct cfg80211_bss_ies *ies;
  442. const u8 *ssidie;
  443. if (bssid && !ether_addr_equal(a->bssid, bssid))
  444. return false;
  445. if (!ssid)
  446. return true;
  447. ies = rcu_access_pointer(a->ies);
  448. if (!ies)
  449. return false;
  450. ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  451. if (!ssidie)
  452. return false;
  453. if (ssidie[1] != ssid_len)
  454. return false;
  455. return memcmp(ssidie + 2, ssid, ssid_len) == 0;
  456. }
  457. /**
  458. * enum bss_compare_mode - BSS compare mode
  459. * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
  460. * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
  461. * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
  462. */
  463. enum bss_compare_mode {
  464. BSS_CMP_REGULAR,
  465. BSS_CMP_HIDE_ZLEN,
  466. BSS_CMP_HIDE_NUL,
  467. };
  468. static int cmp_bss(struct cfg80211_bss *a,
  469. struct cfg80211_bss *b,
  470. enum bss_compare_mode mode)
  471. {
  472. const struct cfg80211_bss_ies *a_ies, *b_ies;
  473. const u8 *ie1 = NULL;
  474. const u8 *ie2 = NULL;
  475. int i, r;
  476. if (a->channel != b->channel)
  477. return b->channel->center_freq - a->channel->center_freq;
  478. a_ies = rcu_access_pointer(a->ies);
  479. if (!a_ies)
  480. return -1;
  481. b_ies = rcu_access_pointer(b->ies);
  482. if (!b_ies)
  483. return 1;
  484. if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
  485. ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  486. a_ies->data, a_ies->len);
  487. if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
  488. ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  489. b_ies->data, b_ies->len);
  490. if (ie1 && ie2) {
  491. int mesh_id_cmp;
  492. if (ie1[1] == ie2[1])
  493. mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  494. else
  495. mesh_id_cmp = ie2[1] - ie1[1];
  496. ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  497. a_ies->data, a_ies->len);
  498. ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  499. b_ies->data, b_ies->len);
  500. if (ie1 && ie2) {
  501. if (mesh_id_cmp)
  502. return mesh_id_cmp;
  503. if (ie1[1] != ie2[1])
  504. return ie2[1] - ie1[1];
  505. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  506. }
  507. }
  508. r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
  509. if (r)
  510. return r;
  511. ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
  512. ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
  513. if (!ie1 && !ie2)
  514. return 0;
  515. /*
  516. * Note that with "hide_ssid", the function returns a match if
  517. * the already-present BSS ("b") is a hidden SSID beacon for
  518. * the new BSS ("a").
  519. */
  520. /* sort missing IE before (left of) present IE */
  521. if (!ie1)
  522. return -1;
  523. if (!ie2)
  524. return 1;
  525. switch (mode) {
  526. case BSS_CMP_HIDE_ZLEN:
  527. /*
  528. * In ZLEN mode we assume the BSS entry we're
  529. * looking for has a zero-length SSID. So if
  530. * the one we're looking at right now has that,
  531. * return 0. Otherwise, return the difference
  532. * in length, but since we're looking for the
  533. * 0-length it's really equivalent to returning
  534. * the length of the one we're looking at.
  535. *
  536. * No content comparison is needed as we assume
  537. * the content length is zero.
  538. */
  539. return ie2[1];
  540. case BSS_CMP_REGULAR:
  541. default:
  542. /* sort by length first, then by contents */
  543. if (ie1[1] != ie2[1])
  544. return ie2[1] - ie1[1];
  545. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  546. case BSS_CMP_HIDE_NUL:
  547. if (ie1[1] != ie2[1])
  548. return ie2[1] - ie1[1];
  549. /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
  550. for (i = 0; i < ie2[1]; i++)
  551. if (ie2[i + 2])
  552. return -1;
  553. return 0;
  554. }
  555. }
  556. static bool cfg80211_bss_type_match(u16 capability,
  557. enum nl80211_band band,
  558. enum ieee80211_bss_type bss_type)
  559. {
  560. bool ret = true;
  561. u16 mask, val;
  562. if (bss_type == IEEE80211_BSS_TYPE_ANY)
  563. return ret;
  564. if (band == NL80211_BAND_60GHZ) {
  565. mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
  566. switch (bss_type) {
  567. case IEEE80211_BSS_TYPE_ESS:
  568. val = WLAN_CAPABILITY_DMG_TYPE_AP;
  569. break;
  570. case IEEE80211_BSS_TYPE_PBSS:
  571. val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
  572. break;
  573. case IEEE80211_BSS_TYPE_IBSS:
  574. val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
  575. break;
  576. default:
  577. return false;
  578. }
  579. } else {
  580. mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
  581. switch (bss_type) {
  582. case IEEE80211_BSS_TYPE_ESS:
  583. val = WLAN_CAPABILITY_ESS;
  584. break;
  585. case IEEE80211_BSS_TYPE_IBSS:
  586. val = WLAN_CAPABILITY_IBSS;
  587. break;
  588. case IEEE80211_BSS_TYPE_MBSS:
  589. val = 0;
  590. break;
  591. default:
  592. return false;
  593. }
  594. }
  595. ret = ((capability & mask) == val);
  596. return ret;
  597. }
  598. /* Returned bss is reference counted and must be cleaned up appropriately. */
  599. struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
  600. struct ieee80211_channel *channel,
  601. const u8 *bssid,
  602. const u8 *ssid, size_t ssid_len,
  603. enum ieee80211_bss_type bss_type,
  604. enum ieee80211_privacy privacy)
  605. {
  606. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  607. struct cfg80211_internal_bss *bss, *res = NULL;
  608. unsigned long now = jiffies;
  609. int bss_privacy;
  610. trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
  611. privacy);
  612. spin_lock_bh(&rdev->bss_lock);
  613. list_for_each_entry(bss, &rdev->bss_list, list) {
  614. if (!cfg80211_bss_type_match(bss->pub.capability,
  615. bss->pub.channel->band, bss_type))
  616. continue;
  617. bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
  618. if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
  619. (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
  620. continue;
  621. if (channel && bss->pub.channel != channel)
  622. continue;
  623. if (!is_valid_ether_addr(bss->pub.bssid))
  624. continue;
  625. /* Don't get expired BSS structs */
  626. if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
  627. !atomic_read(&bss->hold))
  628. continue;
  629. if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
  630. res = bss;
  631. bss_ref_get(rdev, res);
  632. break;
  633. }
  634. }
  635. spin_unlock_bh(&rdev->bss_lock);
  636. if (!res)
  637. return NULL;
  638. trace_cfg80211_return_bss(&res->pub);
  639. return &res->pub;
  640. }
  641. EXPORT_SYMBOL(cfg80211_get_bss);
  642. static void rb_insert_bss(struct cfg80211_registered_device *rdev,
  643. struct cfg80211_internal_bss *bss)
  644. {
  645. struct rb_node **p = &rdev->bss_tree.rb_node;
  646. struct rb_node *parent = NULL;
  647. struct cfg80211_internal_bss *tbss;
  648. int cmp;
  649. while (*p) {
  650. parent = *p;
  651. tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
  652. cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
  653. if (WARN_ON(!cmp)) {
  654. /* will sort of leak this BSS */
  655. return;
  656. }
  657. if (cmp < 0)
  658. p = &(*p)->rb_left;
  659. else
  660. p = &(*p)->rb_right;
  661. }
  662. rb_link_node(&bss->rbn, parent, p);
  663. rb_insert_color(&bss->rbn, &rdev->bss_tree);
  664. }
  665. static struct cfg80211_internal_bss *
  666. rb_find_bss(struct cfg80211_registered_device *rdev,
  667. struct cfg80211_internal_bss *res,
  668. enum bss_compare_mode mode)
  669. {
  670. struct rb_node *n = rdev->bss_tree.rb_node;
  671. struct cfg80211_internal_bss *bss;
  672. int r;
  673. while (n) {
  674. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  675. r = cmp_bss(&res->pub, &bss->pub, mode);
  676. if (r == 0)
  677. return bss;
  678. else if (r < 0)
  679. n = n->rb_left;
  680. else
  681. n = n->rb_right;
  682. }
  683. return NULL;
  684. }
  685. static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
  686. struct cfg80211_internal_bss *new)
  687. {
  688. const struct cfg80211_bss_ies *ies;
  689. struct cfg80211_internal_bss *bss;
  690. const u8 *ie;
  691. int i, ssidlen;
  692. u8 fold = 0;
  693. u32 n_entries = 0;
  694. ies = rcu_access_pointer(new->pub.beacon_ies);
  695. if (WARN_ON(!ies))
  696. return false;
  697. ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  698. if (!ie) {
  699. /* nothing to do */
  700. return true;
  701. }
  702. ssidlen = ie[1];
  703. for (i = 0; i < ssidlen; i++)
  704. fold |= ie[2 + i];
  705. if (fold) {
  706. /* not a hidden SSID */
  707. return true;
  708. }
  709. /* This is the bad part ... */
  710. list_for_each_entry(bss, &rdev->bss_list, list) {
  711. /*
  712. * we're iterating all the entries anyway, so take the
  713. * opportunity to validate the list length accounting
  714. */
  715. n_entries++;
  716. if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
  717. continue;
  718. if (bss->pub.channel != new->pub.channel)
  719. continue;
  720. if (bss->pub.scan_width != new->pub.scan_width)
  721. continue;
  722. if (rcu_access_pointer(bss->pub.beacon_ies))
  723. continue;
  724. ies = rcu_access_pointer(bss->pub.ies);
  725. if (!ies)
  726. continue;
  727. ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  728. if (!ie)
  729. continue;
  730. if (ssidlen && ie[1] != ssidlen)
  731. continue;
  732. if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
  733. continue;
  734. if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
  735. list_del(&bss->hidden_list);
  736. /* combine them */
  737. list_add(&bss->hidden_list, &new->hidden_list);
  738. bss->pub.hidden_beacon_bss = &new->pub;
  739. new->refcount += bss->refcount;
  740. rcu_assign_pointer(bss->pub.beacon_ies,
  741. new->pub.beacon_ies);
  742. }
  743. WARN_ONCE(n_entries != rdev->bss_entries,
  744. "rdev bss entries[%d]/list[len:%d] corruption\n",
  745. rdev->bss_entries, n_entries);
  746. return true;
  747. }
  748. /* Returned bss is reference counted and must be cleaned up appropriately. */
  749. static struct cfg80211_internal_bss *
  750. cfg80211_bss_update(struct cfg80211_registered_device *rdev,
  751. struct cfg80211_internal_bss *tmp,
  752. bool signal_valid)
  753. {
  754. struct cfg80211_internal_bss *found = NULL;
  755. if (WARN_ON(!tmp->pub.channel))
  756. return NULL;
  757. tmp->ts = jiffies;
  758. spin_lock_bh(&rdev->bss_lock);
  759. if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
  760. spin_unlock_bh(&rdev->bss_lock);
  761. return NULL;
  762. }
  763. found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
  764. if (found) {
  765. /* Update IEs */
  766. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  767. const struct cfg80211_bss_ies *old;
  768. old = rcu_access_pointer(found->pub.proberesp_ies);
  769. rcu_assign_pointer(found->pub.proberesp_ies,
  770. tmp->pub.proberesp_ies);
  771. /* Override possible earlier Beacon frame IEs */
  772. rcu_assign_pointer(found->pub.ies,
  773. tmp->pub.proberesp_ies);
  774. if (old)
  775. kfree_rcu((struct cfg80211_bss_ies *)old,
  776. rcu_head);
  777. } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
  778. const struct cfg80211_bss_ies *old;
  779. struct cfg80211_internal_bss *bss;
  780. if (found->pub.hidden_beacon_bss &&
  781. !list_empty(&found->hidden_list)) {
  782. const struct cfg80211_bss_ies *f;
  783. /*
  784. * The found BSS struct is one of the probe
  785. * response members of a group, but we're
  786. * receiving a beacon (beacon_ies in the tmp
  787. * bss is used). This can only mean that the
  788. * AP changed its beacon from not having an
  789. * SSID to showing it, which is confusing so
  790. * drop this information.
  791. */
  792. f = rcu_access_pointer(tmp->pub.beacon_ies);
  793. kfree_rcu((struct cfg80211_bss_ies *)f,
  794. rcu_head);
  795. goto drop;
  796. }
  797. old = rcu_access_pointer(found->pub.beacon_ies);
  798. rcu_assign_pointer(found->pub.beacon_ies,
  799. tmp->pub.beacon_ies);
  800. /* Override IEs if they were from a beacon before */
  801. if (old == rcu_access_pointer(found->pub.ies))
  802. rcu_assign_pointer(found->pub.ies,
  803. tmp->pub.beacon_ies);
  804. /* Assign beacon IEs to all sub entries */
  805. list_for_each_entry(bss, &found->hidden_list,
  806. hidden_list) {
  807. const struct cfg80211_bss_ies *ies;
  808. ies = rcu_access_pointer(bss->pub.beacon_ies);
  809. WARN_ON(ies != old);
  810. rcu_assign_pointer(bss->pub.beacon_ies,
  811. tmp->pub.beacon_ies);
  812. }
  813. if (old)
  814. kfree_rcu((struct cfg80211_bss_ies *)old,
  815. rcu_head);
  816. }
  817. found->pub.beacon_interval = tmp->pub.beacon_interval;
  818. /*
  819. * don't update the signal if beacon was heard on
  820. * adjacent channel.
  821. */
  822. if (signal_valid)
  823. found->pub.signal = tmp->pub.signal;
  824. found->pub.capability = tmp->pub.capability;
  825. found->ts = tmp->ts;
  826. found->ts_boottime = tmp->ts_boottime;
  827. found->parent_tsf = tmp->parent_tsf;
  828. found->pub.chains = tmp->pub.chains;
  829. memcpy(found->pub.chain_signal, tmp->pub.chain_signal,
  830. IEEE80211_MAX_CHAINS);
  831. ether_addr_copy(found->parent_bssid, tmp->parent_bssid);
  832. } else {
  833. struct cfg80211_internal_bss *new;
  834. struct cfg80211_internal_bss *hidden;
  835. struct cfg80211_bss_ies *ies;
  836. /*
  837. * create a copy -- the "res" variable that is passed in
  838. * is allocated on the stack since it's not needed in the
  839. * more common case of an update
  840. */
  841. new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
  842. GFP_ATOMIC);
  843. if (!new) {
  844. ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
  845. if (ies)
  846. kfree_rcu(ies, rcu_head);
  847. ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
  848. if (ies)
  849. kfree_rcu(ies, rcu_head);
  850. goto drop;
  851. }
  852. memcpy(new, tmp, sizeof(*new));
  853. new->refcount = 1;
  854. INIT_LIST_HEAD(&new->hidden_list);
  855. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  856. hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
  857. if (!hidden)
  858. hidden = rb_find_bss(rdev, tmp,
  859. BSS_CMP_HIDE_NUL);
  860. if (hidden) {
  861. new->pub.hidden_beacon_bss = &hidden->pub;
  862. list_add(&new->hidden_list,
  863. &hidden->hidden_list);
  864. hidden->refcount++;
  865. rcu_assign_pointer(new->pub.beacon_ies,
  866. hidden->pub.beacon_ies);
  867. }
  868. } else {
  869. /*
  870. * Ok so we found a beacon, and don't have an entry. If
  871. * it's a beacon with hidden SSID, we might be in for an
  872. * expensive search for any probe responses that should
  873. * be grouped with this beacon for updates ...
  874. */
  875. if (!cfg80211_combine_bsses(rdev, new)) {
  876. kfree(new);
  877. goto drop;
  878. }
  879. }
  880. if (rdev->bss_entries >= bss_entries_limit &&
  881. !cfg80211_bss_expire_oldest(rdev)) {
  882. kfree(new);
  883. goto drop;
  884. }
  885. list_add_tail(&new->list, &rdev->bss_list);
  886. rdev->bss_entries++;
  887. rb_insert_bss(rdev, new);
  888. found = new;
  889. }
  890. rdev->bss_generation++;
  891. bss_ref_get(rdev, found);
  892. spin_unlock_bh(&rdev->bss_lock);
  893. return found;
  894. drop:
  895. spin_unlock_bh(&rdev->bss_lock);
  896. return NULL;
  897. }
  898. static struct ieee80211_channel *
  899. cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
  900. struct ieee80211_channel *channel)
  901. {
  902. const u8 *tmp;
  903. u32 freq;
  904. int channel_number = -1;
  905. tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
  906. if (tmp && tmp[1] == 1) {
  907. channel_number = tmp[2];
  908. } else {
  909. tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
  910. if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
  911. struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
  912. channel_number = htop->primary_chan;
  913. }
  914. }
  915. if (channel_number < 0)
  916. return channel;
  917. freq = ieee80211_channel_to_frequency(channel_number, channel->band);
  918. channel = ieee80211_get_channel(wiphy, freq);
  919. if (!channel)
  920. return NULL;
  921. if (channel->flags & IEEE80211_CHAN_DISABLED)
  922. return NULL;
  923. return channel;
  924. }
  925. /* Returned bss is reference counted and must be cleaned up appropriately. */
  926. struct cfg80211_bss *
  927. cfg80211_inform_bss_data(struct wiphy *wiphy,
  928. struct cfg80211_inform_bss *data,
  929. enum cfg80211_bss_frame_type ftype,
  930. const u8 *bssid, u64 tsf, u16 capability,
  931. u16 beacon_interval, const u8 *ie, size_t ielen,
  932. gfp_t gfp)
  933. {
  934. struct cfg80211_bss_ies *ies;
  935. struct ieee80211_channel *channel;
  936. struct cfg80211_internal_bss tmp = {}, *res;
  937. int bss_type;
  938. bool signal_valid;
  939. if (WARN_ON(!wiphy))
  940. return NULL;
  941. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  942. (data->signal < 0 || data->signal > 100)))
  943. return NULL;
  944. channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan);
  945. if (!channel)
  946. return NULL;
  947. memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
  948. tmp.pub.channel = channel;
  949. tmp.pub.scan_width = data->scan_width;
  950. tmp.pub.signal = data->signal;
  951. tmp.pub.beacon_interval = beacon_interval;
  952. tmp.pub.capability = capability;
  953. tmp.ts_boottime = data->boottime_ns;
  954. /*
  955. * If we do not know here whether the IEs are from a Beacon or Probe
  956. * Response frame, we need to pick one of the options and only use it
  957. * with the driver that does not provide the full Beacon/Probe Response
  958. * frame. Use Beacon frame pointer to avoid indicating that this should
  959. * override the IEs pointer should we have received an earlier
  960. * indication of Probe Response data.
  961. */
  962. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  963. if (!ies)
  964. return NULL;
  965. ies->len = ielen;
  966. ies->tsf = tsf;
  967. ies->from_beacon = false;
  968. memcpy(ies->data, ie, ielen);
  969. switch (ftype) {
  970. case CFG80211_BSS_FTYPE_BEACON:
  971. ies->from_beacon = true;
  972. /* fall through to assign */
  973. case CFG80211_BSS_FTYPE_UNKNOWN:
  974. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  975. break;
  976. case CFG80211_BSS_FTYPE_PRESP:
  977. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  978. break;
  979. }
  980. rcu_assign_pointer(tmp.pub.ies, ies);
  981. signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
  982. wiphy->max_adj_channel_rssi_comp;
  983. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
  984. if (!res)
  985. return NULL;
  986. if (channel->band == NL80211_BAND_60GHZ) {
  987. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  988. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  989. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  990. regulatory_hint_found_beacon(wiphy, channel, gfp);
  991. } else {
  992. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  993. regulatory_hint_found_beacon(wiphy, channel, gfp);
  994. }
  995. trace_cfg80211_return_bss(&res->pub);
  996. /* cfg80211_bss_update gives us a referenced result */
  997. return &res->pub;
  998. }
  999. EXPORT_SYMBOL(cfg80211_inform_bss_data);
  1000. /* cfg80211_inform_bss_width_frame helper */
  1001. struct cfg80211_bss *
  1002. cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
  1003. struct cfg80211_inform_bss *data,
  1004. struct ieee80211_mgmt *mgmt, size_t len,
  1005. gfp_t gfp)
  1006. {
  1007. struct cfg80211_internal_bss tmp = {}, *res;
  1008. struct cfg80211_bss_ies *ies;
  1009. struct ieee80211_channel *channel;
  1010. bool signal_valid;
  1011. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  1012. u.probe_resp.variable);
  1013. int bss_type;
  1014. BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
  1015. offsetof(struct ieee80211_mgmt, u.beacon.variable));
  1016. trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
  1017. if (WARN_ON(!mgmt))
  1018. return NULL;
  1019. if (WARN_ON(!wiphy))
  1020. return NULL;
  1021. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  1022. (data->signal < 0 || data->signal > 100)))
  1023. return NULL;
  1024. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  1025. return NULL;
  1026. channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
  1027. ielen, data->chan);
  1028. if (!channel)
  1029. return NULL;
  1030. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  1031. if (!ies)
  1032. return NULL;
  1033. ies->len = ielen;
  1034. ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  1035. ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
  1036. memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
  1037. if (ieee80211_is_probe_resp(mgmt->frame_control))
  1038. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  1039. else
  1040. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  1041. rcu_assign_pointer(tmp.pub.ies, ies);
  1042. memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
  1043. tmp.pub.channel = channel;
  1044. tmp.pub.scan_width = data->scan_width;
  1045. tmp.pub.signal = data->signal;
  1046. tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  1047. tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  1048. tmp.ts_boottime = data->boottime_ns;
  1049. tmp.parent_tsf = data->parent_tsf;
  1050. tmp.pub.chains = data->chains;
  1051. memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
  1052. ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
  1053. signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
  1054. wiphy->max_adj_channel_rssi_comp;
  1055. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
  1056. if (!res)
  1057. return NULL;
  1058. if (channel->band == NL80211_BAND_60GHZ) {
  1059. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  1060. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  1061. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  1062. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1063. } else {
  1064. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  1065. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1066. }
  1067. trace_cfg80211_return_bss(&res->pub);
  1068. /* cfg80211_bss_update gives us a referenced result */
  1069. return &res->pub;
  1070. }
  1071. EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
  1072. void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1073. {
  1074. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1075. struct cfg80211_internal_bss *bss;
  1076. if (!pub)
  1077. return;
  1078. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1079. spin_lock_bh(&rdev->bss_lock);
  1080. bss_ref_get(rdev, bss);
  1081. spin_unlock_bh(&rdev->bss_lock);
  1082. }
  1083. EXPORT_SYMBOL(cfg80211_ref_bss);
  1084. void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1085. {
  1086. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1087. struct cfg80211_internal_bss *bss;
  1088. if (!pub)
  1089. return;
  1090. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1091. spin_lock_bh(&rdev->bss_lock);
  1092. bss_ref_put(rdev, bss);
  1093. spin_unlock_bh(&rdev->bss_lock);
  1094. }
  1095. EXPORT_SYMBOL(cfg80211_put_bss);
  1096. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1097. {
  1098. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1099. struct cfg80211_internal_bss *bss;
  1100. if (WARN_ON(!pub))
  1101. return;
  1102. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1103. spin_lock_bh(&rdev->bss_lock);
  1104. if (!list_empty(&bss->list)) {
  1105. if (__cfg80211_unlink_bss(rdev, bss))
  1106. rdev->bss_generation++;
  1107. }
  1108. spin_unlock_bh(&rdev->bss_lock);
  1109. }
  1110. EXPORT_SYMBOL(cfg80211_unlink_bss);
  1111. #ifdef CONFIG_CFG80211_WEXT
  1112. static struct cfg80211_registered_device *
  1113. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
  1114. {
  1115. struct cfg80211_registered_device *rdev;
  1116. struct net_device *dev;
  1117. ASSERT_RTNL();
  1118. dev = dev_get_by_index(net, ifindex);
  1119. if (!dev)
  1120. return ERR_PTR(-ENODEV);
  1121. if (dev->ieee80211_ptr)
  1122. rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
  1123. else
  1124. rdev = ERR_PTR(-ENODEV);
  1125. dev_put(dev);
  1126. return rdev;
  1127. }
  1128. int cfg80211_wext_siwscan(struct net_device *dev,
  1129. struct iw_request_info *info,
  1130. union iwreq_data *wrqu, char *extra)
  1131. {
  1132. struct cfg80211_registered_device *rdev;
  1133. struct wiphy *wiphy;
  1134. struct iw_scan_req *wreq = NULL;
  1135. struct cfg80211_scan_request *creq = NULL;
  1136. int i, err, n_channels = 0;
  1137. enum nl80211_band band;
  1138. if (!netif_running(dev))
  1139. return -ENETDOWN;
  1140. if (wrqu->data.length == sizeof(struct iw_scan_req))
  1141. wreq = (struct iw_scan_req *)extra;
  1142. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1143. if (IS_ERR(rdev))
  1144. return PTR_ERR(rdev);
  1145. if (rdev->scan_req || rdev->scan_msg) {
  1146. err = -EBUSY;
  1147. goto out;
  1148. }
  1149. wiphy = &rdev->wiphy;
  1150. /* Determine number of channels, needed to allocate creq */
  1151. if (wreq && wreq->num_channels)
  1152. n_channels = wreq->num_channels;
  1153. else
  1154. n_channels = ieee80211_get_num_supported_channels(wiphy);
  1155. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  1156. n_channels * sizeof(void *),
  1157. GFP_ATOMIC);
  1158. if (!creq) {
  1159. err = -ENOMEM;
  1160. goto out;
  1161. }
  1162. creq->wiphy = wiphy;
  1163. creq->wdev = dev->ieee80211_ptr;
  1164. /* SSIDs come after channels */
  1165. creq->ssids = (void *)&creq->channels[n_channels];
  1166. creq->n_channels = n_channels;
  1167. creq->n_ssids = 1;
  1168. creq->scan_start = jiffies;
  1169. /* translate "Scan on frequencies" request */
  1170. i = 0;
  1171. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  1172. int j;
  1173. if (!wiphy->bands[band])
  1174. continue;
  1175. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  1176. /* ignore disabled channels */
  1177. if (wiphy->bands[band]->channels[j].flags &
  1178. IEEE80211_CHAN_DISABLED)
  1179. continue;
  1180. /* If we have a wireless request structure and the
  1181. * wireless request specifies frequencies, then search
  1182. * for the matching hardware channel.
  1183. */
  1184. if (wreq && wreq->num_channels) {
  1185. int k;
  1186. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  1187. for (k = 0; k < wreq->num_channels; k++) {
  1188. struct iw_freq *freq =
  1189. &wreq->channel_list[k];
  1190. int wext_freq =
  1191. cfg80211_wext_freq(freq);
  1192. if (wext_freq == wiphy_freq)
  1193. goto wext_freq_found;
  1194. }
  1195. goto wext_freq_not_found;
  1196. }
  1197. wext_freq_found:
  1198. creq->channels[i] = &wiphy->bands[band]->channels[j];
  1199. i++;
  1200. wext_freq_not_found: ;
  1201. }
  1202. }
  1203. /* No channels found? */
  1204. if (!i) {
  1205. err = -EINVAL;
  1206. goto out;
  1207. }
  1208. /* Set real number of channels specified in creq->channels[] */
  1209. creq->n_channels = i;
  1210. /* translate "Scan for SSID" request */
  1211. if (wreq) {
  1212. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  1213. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  1214. err = -EINVAL;
  1215. goto out;
  1216. }
  1217. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  1218. creq->ssids[0].ssid_len = wreq->essid_len;
  1219. }
  1220. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  1221. creq->n_ssids = 0;
  1222. }
  1223. for (i = 0; i < NUM_NL80211_BANDS; i++)
  1224. if (wiphy->bands[i])
  1225. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  1226. eth_broadcast_addr(creq->bssid);
  1227. rdev->scan_req = creq;
  1228. err = rdev_scan(rdev, creq);
  1229. if (err) {
  1230. rdev->scan_req = NULL;
  1231. /* creq will be freed below */
  1232. } else {
  1233. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  1234. /* creq now owned by driver */
  1235. creq = NULL;
  1236. dev_hold(dev);
  1237. }
  1238. out:
  1239. kfree(creq);
  1240. return err;
  1241. }
  1242. EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
  1243. static char *ieee80211_scan_add_ies(struct iw_request_info *info,
  1244. const struct cfg80211_bss_ies *ies,
  1245. char *current_ev, char *end_buf)
  1246. {
  1247. const u8 *pos, *end, *next;
  1248. struct iw_event iwe;
  1249. if (!ies)
  1250. return current_ev;
  1251. /*
  1252. * If needed, fragment the IEs buffer (at IE boundaries) into short
  1253. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  1254. */
  1255. pos = ies->data;
  1256. end = pos + ies->len;
  1257. while (end - pos > IW_GENERIC_IE_MAX) {
  1258. next = pos + 2 + pos[1];
  1259. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  1260. next = next + 2 + next[1];
  1261. memset(&iwe, 0, sizeof(iwe));
  1262. iwe.cmd = IWEVGENIE;
  1263. iwe.u.data.length = next - pos;
  1264. current_ev = iwe_stream_add_point_check(info, current_ev,
  1265. end_buf, &iwe,
  1266. (void *)pos);
  1267. if (IS_ERR(current_ev))
  1268. return current_ev;
  1269. pos = next;
  1270. }
  1271. if (end > pos) {
  1272. memset(&iwe, 0, sizeof(iwe));
  1273. iwe.cmd = IWEVGENIE;
  1274. iwe.u.data.length = end - pos;
  1275. current_ev = iwe_stream_add_point_check(info, current_ev,
  1276. end_buf, &iwe,
  1277. (void *)pos);
  1278. if (IS_ERR(current_ev))
  1279. return current_ev;
  1280. }
  1281. return current_ev;
  1282. }
  1283. static char *
  1284. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  1285. struct cfg80211_internal_bss *bss, char *current_ev,
  1286. char *end_buf)
  1287. {
  1288. const struct cfg80211_bss_ies *ies;
  1289. struct iw_event iwe;
  1290. const u8 *ie;
  1291. u8 buf[50];
  1292. u8 *cfg, *p, *tmp;
  1293. int rem, i, sig;
  1294. bool ismesh = false;
  1295. memset(&iwe, 0, sizeof(iwe));
  1296. iwe.cmd = SIOCGIWAP;
  1297. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1298. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  1299. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1300. IW_EV_ADDR_LEN);
  1301. if (IS_ERR(current_ev))
  1302. return current_ev;
  1303. memset(&iwe, 0, sizeof(iwe));
  1304. iwe.cmd = SIOCGIWFREQ;
  1305. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  1306. iwe.u.freq.e = 0;
  1307. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1308. IW_EV_FREQ_LEN);
  1309. if (IS_ERR(current_ev))
  1310. return current_ev;
  1311. memset(&iwe, 0, sizeof(iwe));
  1312. iwe.cmd = SIOCGIWFREQ;
  1313. iwe.u.freq.m = bss->pub.channel->center_freq;
  1314. iwe.u.freq.e = 6;
  1315. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1316. IW_EV_FREQ_LEN);
  1317. if (IS_ERR(current_ev))
  1318. return current_ev;
  1319. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  1320. memset(&iwe, 0, sizeof(iwe));
  1321. iwe.cmd = IWEVQUAL;
  1322. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  1323. IW_QUAL_NOISE_INVALID |
  1324. IW_QUAL_QUAL_UPDATED;
  1325. switch (wiphy->signal_type) {
  1326. case CFG80211_SIGNAL_TYPE_MBM:
  1327. sig = bss->pub.signal / 100;
  1328. iwe.u.qual.level = sig;
  1329. iwe.u.qual.updated |= IW_QUAL_DBM;
  1330. if (sig < -110) /* rather bad */
  1331. sig = -110;
  1332. else if (sig > -40) /* perfect */
  1333. sig = -40;
  1334. /* will give a range of 0 .. 70 */
  1335. iwe.u.qual.qual = sig + 110;
  1336. break;
  1337. case CFG80211_SIGNAL_TYPE_UNSPEC:
  1338. iwe.u.qual.level = bss->pub.signal;
  1339. /* will give range 0 .. 100 */
  1340. iwe.u.qual.qual = bss->pub.signal;
  1341. break;
  1342. default:
  1343. /* not reached */
  1344. break;
  1345. }
  1346. current_ev = iwe_stream_add_event_check(info, current_ev,
  1347. end_buf, &iwe,
  1348. IW_EV_QUAL_LEN);
  1349. if (IS_ERR(current_ev))
  1350. return current_ev;
  1351. }
  1352. memset(&iwe, 0, sizeof(iwe));
  1353. iwe.cmd = SIOCGIWENCODE;
  1354. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  1355. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1356. else
  1357. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1358. iwe.u.data.length = 0;
  1359. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  1360. &iwe, "");
  1361. if (IS_ERR(current_ev))
  1362. return current_ev;
  1363. rcu_read_lock();
  1364. ies = rcu_dereference(bss->pub.ies);
  1365. rem = ies->len;
  1366. ie = ies->data;
  1367. while (rem >= 2) {
  1368. /* invalid data */
  1369. if (ie[1] > rem - 2)
  1370. break;
  1371. switch (ie[0]) {
  1372. case WLAN_EID_SSID:
  1373. memset(&iwe, 0, sizeof(iwe));
  1374. iwe.cmd = SIOCGIWESSID;
  1375. iwe.u.data.length = ie[1];
  1376. iwe.u.data.flags = 1;
  1377. current_ev = iwe_stream_add_point_check(info,
  1378. current_ev,
  1379. end_buf, &iwe,
  1380. (u8 *)ie + 2);
  1381. if (IS_ERR(current_ev))
  1382. goto unlock;
  1383. break;
  1384. case WLAN_EID_MESH_ID:
  1385. memset(&iwe, 0, sizeof(iwe));
  1386. iwe.cmd = SIOCGIWESSID;
  1387. iwe.u.data.length = ie[1];
  1388. iwe.u.data.flags = 1;
  1389. current_ev = iwe_stream_add_point_check(info,
  1390. current_ev,
  1391. end_buf, &iwe,
  1392. (u8 *)ie + 2);
  1393. if (IS_ERR(current_ev))
  1394. goto unlock;
  1395. break;
  1396. case WLAN_EID_MESH_CONFIG:
  1397. ismesh = true;
  1398. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  1399. break;
  1400. cfg = (u8 *)ie + 2;
  1401. memset(&iwe, 0, sizeof(iwe));
  1402. iwe.cmd = IWEVCUSTOM;
  1403. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  1404. "0x%02X", cfg[0]);
  1405. iwe.u.data.length = strlen(buf);
  1406. current_ev = iwe_stream_add_point_check(info,
  1407. current_ev,
  1408. end_buf,
  1409. &iwe, buf);
  1410. if (IS_ERR(current_ev))
  1411. goto unlock;
  1412. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  1413. cfg[1]);
  1414. iwe.u.data.length = strlen(buf);
  1415. current_ev = iwe_stream_add_point_check(info,
  1416. current_ev,
  1417. end_buf,
  1418. &iwe, buf);
  1419. if (IS_ERR(current_ev))
  1420. goto unlock;
  1421. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  1422. cfg[2]);
  1423. iwe.u.data.length = strlen(buf);
  1424. current_ev = iwe_stream_add_point_check(info,
  1425. current_ev,
  1426. end_buf,
  1427. &iwe, buf);
  1428. if (IS_ERR(current_ev))
  1429. goto unlock;
  1430. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  1431. iwe.u.data.length = strlen(buf);
  1432. current_ev = iwe_stream_add_point_check(info,
  1433. current_ev,
  1434. end_buf,
  1435. &iwe, buf);
  1436. if (IS_ERR(current_ev))
  1437. goto unlock;
  1438. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  1439. iwe.u.data.length = strlen(buf);
  1440. current_ev = iwe_stream_add_point_check(info,
  1441. current_ev,
  1442. end_buf,
  1443. &iwe, buf);
  1444. if (IS_ERR(current_ev))
  1445. goto unlock;
  1446. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  1447. iwe.u.data.length = strlen(buf);
  1448. current_ev = iwe_stream_add_point_check(info,
  1449. current_ev,
  1450. end_buf,
  1451. &iwe, buf);
  1452. if (IS_ERR(current_ev))
  1453. goto unlock;
  1454. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1455. iwe.u.data.length = strlen(buf);
  1456. current_ev = iwe_stream_add_point_check(info,
  1457. current_ev,
  1458. end_buf,
  1459. &iwe, buf);
  1460. if (IS_ERR(current_ev))
  1461. goto unlock;
  1462. break;
  1463. case WLAN_EID_SUPP_RATES:
  1464. case WLAN_EID_EXT_SUPP_RATES:
  1465. /* display all supported rates in readable format */
  1466. p = current_ev + iwe_stream_lcp_len(info);
  1467. memset(&iwe, 0, sizeof(iwe));
  1468. iwe.cmd = SIOCGIWRATE;
  1469. /* Those two flags are ignored... */
  1470. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1471. for (i = 0; i < ie[1]; i++) {
  1472. iwe.u.bitrate.value =
  1473. ((ie[i + 2] & 0x7f) * 500000);
  1474. tmp = p;
  1475. p = iwe_stream_add_value(info, current_ev, p,
  1476. end_buf, &iwe,
  1477. IW_EV_PARAM_LEN);
  1478. if (p == tmp) {
  1479. current_ev = ERR_PTR(-E2BIG);
  1480. goto unlock;
  1481. }
  1482. }
  1483. current_ev = p;
  1484. break;
  1485. }
  1486. rem -= ie[1] + 2;
  1487. ie += ie[1] + 2;
  1488. }
  1489. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1490. ismesh) {
  1491. memset(&iwe, 0, sizeof(iwe));
  1492. iwe.cmd = SIOCGIWMODE;
  1493. if (ismesh)
  1494. iwe.u.mode = IW_MODE_MESH;
  1495. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1496. iwe.u.mode = IW_MODE_MASTER;
  1497. else
  1498. iwe.u.mode = IW_MODE_ADHOC;
  1499. current_ev = iwe_stream_add_event_check(info, current_ev,
  1500. end_buf, &iwe,
  1501. IW_EV_UINT_LEN);
  1502. if (IS_ERR(current_ev))
  1503. goto unlock;
  1504. }
  1505. memset(&iwe, 0, sizeof(iwe));
  1506. iwe.cmd = IWEVCUSTOM;
  1507. sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
  1508. iwe.u.data.length = strlen(buf);
  1509. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  1510. &iwe, buf);
  1511. if (IS_ERR(current_ev))
  1512. goto unlock;
  1513. memset(&iwe, 0, sizeof(iwe));
  1514. iwe.cmd = IWEVCUSTOM;
  1515. sprintf(buf, " Last beacon: %ums ago",
  1516. elapsed_jiffies_msecs(bss->ts));
  1517. iwe.u.data.length = strlen(buf);
  1518. current_ev = iwe_stream_add_point_check(info, current_ev,
  1519. end_buf, &iwe, buf);
  1520. if (IS_ERR(current_ev))
  1521. goto unlock;
  1522. current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
  1523. unlock:
  1524. rcu_read_unlock();
  1525. return current_ev;
  1526. }
  1527. static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
  1528. struct iw_request_info *info,
  1529. char *buf, size_t len)
  1530. {
  1531. char *current_ev = buf;
  1532. char *end_buf = buf + len;
  1533. struct cfg80211_internal_bss *bss;
  1534. int err = 0;
  1535. spin_lock_bh(&rdev->bss_lock);
  1536. cfg80211_bss_expire(rdev);
  1537. list_for_each_entry(bss, &rdev->bss_list, list) {
  1538. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1539. err = -E2BIG;
  1540. break;
  1541. }
  1542. current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
  1543. current_ev, end_buf);
  1544. if (IS_ERR(current_ev)) {
  1545. err = PTR_ERR(current_ev);
  1546. break;
  1547. }
  1548. }
  1549. spin_unlock_bh(&rdev->bss_lock);
  1550. if (err)
  1551. return err;
  1552. return current_ev - buf;
  1553. }
  1554. int cfg80211_wext_giwscan(struct net_device *dev,
  1555. struct iw_request_info *info,
  1556. struct iw_point *data, char *extra)
  1557. {
  1558. struct cfg80211_registered_device *rdev;
  1559. int res;
  1560. if (!netif_running(dev))
  1561. return -ENETDOWN;
  1562. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1563. if (IS_ERR(rdev))
  1564. return PTR_ERR(rdev);
  1565. if (rdev->scan_req || rdev->scan_msg)
  1566. return -EAGAIN;
  1567. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1568. data->length = 0;
  1569. if (res >= 0) {
  1570. data->length = res;
  1571. res = 0;
  1572. }
  1573. return res;
  1574. }
  1575. EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
  1576. #endif