scan.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875
  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 (7 * 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. ether_addr_copy(found->parent_bssid, tmp->parent_bssid);
  827. } else {
  828. struct cfg80211_internal_bss *new;
  829. struct cfg80211_internal_bss *hidden;
  830. struct cfg80211_bss_ies *ies;
  831. /*
  832. * create a copy -- the "res" variable that is passed in
  833. * is allocated on the stack since it's not needed in the
  834. * more common case of an update
  835. */
  836. new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
  837. GFP_ATOMIC);
  838. if (!new) {
  839. ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
  840. if (ies)
  841. kfree_rcu(ies, rcu_head);
  842. ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
  843. if (ies)
  844. kfree_rcu(ies, rcu_head);
  845. goto drop;
  846. }
  847. memcpy(new, tmp, sizeof(*new));
  848. new->refcount = 1;
  849. INIT_LIST_HEAD(&new->hidden_list);
  850. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  851. hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
  852. if (!hidden)
  853. hidden = rb_find_bss(rdev, tmp,
  854. BSS_CMP_HIDE_NUL);
  855. if (hidden) {
  856. new->pub.hidden_beacon_bss = &hidden->pub;
  857. list_add(&new->hidden_list,
  858. &hidden->hidden_list);
  859. hidden->refcount++;
  860. rcu_assign_pointer(new->pub.beacon_ies,
  861. hidden->pub.beacon_ies);
  862. }
  863. } else {
  864. /*
  865. * Ok so we found a beacon, and don't have an entry. If
  866. * it's a beacon with hidden SSID, we might be in for an
  867. * expensive search for any probe responses that should
  868. * be grouped with this beacon for updates ...
  869. */
  870. if (!cfg80211_combine_bsses(rdev, new)) {
  871. kfree(new);
  872. goto drop;
  873. }
  874. }
  875. if (rdev->bss_entries >= bss_entries_limit &&
  876. !cfg80211_bss_expire_oldest(rdev)) {
  877. if (!list_empty(&new->hidden_list))
  878. list_del(&new->hidden_list);
  879. kfree(new);
  880. goto drop;
  881. }
  882. list_add_tail(&new->list, &rdev->bss_list);
  883. rdev->bss_entries++;
  884. rb_insert_bss(rdev, new);
  885. found = new;
  886. }
  887. rdev->bss_generation++;
  888. bss_ref_get(rdev, found);
  889. spin_unlock_bh(&rdev->bss_lock);
  890. return found;
  891. drop:
  892. spin_unlock_bh(&rdev->bss_lock);
  893. return NULL;
  894. }
  895. /*
  896. * Update RX channel information based on the available frame payload
  897. * information. This is mainly for the 2.4 GHz band where frames can be received
  898. * from neighboring channels and the Beacon frames use the DSSS Parameter Set
  899. * element to indicate the current (transmitting) channel, but this might also
  900. * be needed on other bands if RX frequency does not match with the actual
  901. * operating channel of a BSS.
  902. */
  903. static struct ieee80211_channel *
  904. cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
  905. struct ieee80211_channel *channel,
  906. enum nl80211_bss_scan_width scan_width)
  907. {
  908. const u8 *tmp;
  909. u32 freq;
  910. int channel_number = -1;
  911. struct ieee80211_channel *alt_channel;
  912. tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
  913. if (tmp && tmp[1] == 1) {
  914. channel_number = tmp[2];
  915. } else {
  916. tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
  917. if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
  918. struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
  919. channel_number = htop->primary_chan;
  920. }
  921. }
  922. if (channel_number < 0) {
  923. /* No channel information in frame payload */
  924. return channel;
  925. }
  926. freq = ieee80211_channel_to_frequency(channel_number, channel->band);
  927. alt_channel = ieee80211_get_channel(wiphy, freq);
  928. if (!alt_channel) {
  929. if (channel->band == NL80211_BAND_2GHZ) {
  930. /*
  931. * Better not allow unexpected channels when that could
  932. * be going beyond the 1-11 range (e.g., discovering
  933. * BSS on channel 12 when radio is configured for
  934. * channel 11.
  935. */
  936. return NULL;
  937. }
  938. /* No match for the payload channel number - ignore it */
  939. return channel;
  940. }
  941. if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
  942. scan_width == NL80211_BSS_CHAN_WIDTH_5) {
  943. /*
  944. * Ignore channel number in 5 and 10 MHz channels where there
  945. * may not be an n:1 or 1:n mapping between frequencies and
  946. * channel numbers.
  947. */
  948. return channel;
  949. }
  950. /*
  951. * Use the channel determined through the payload channel number
  952. * instead of the RX channel reported by the driver.
  953. */
  954. if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
  955. return NULL;
  956. return alt_channel;
  957. }
  958. /* Returned bss is reference counted and must be cleaned up appropriately. */
  959. struct cfg80211_bss *
  960. cfg80211_inform_bss_data(struct wiphy *wiphy,
  961. struct cfg80211_inform_bss *data,
  962. enum cfg80211_bss_frame_type ftype,
  963. const u8 *bssid, u64 tsf, u16 capability,
  964. u16 beacon_interval, const u8 *ie, size_t ielen,
  965. gfp_t gfp)
  966. {
  967. struct cfg80211_bss_ies *ies;
  968. struct ieee80211_channel *channel;
  969. struct cfg80211_internal_bss tmp = {}, *res;
  970. int bss_type;
  971. bool signal_valid;
  972. if (WARN_ON(!wiphy))
  973. return NULL;
  974. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  975. (data->signal < 0 || data->signal > 100)))
  976. return NULL;
  977. channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
  978. data->scan_width);
  979. if (!channel)
  980. return NULL;
  981. memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
  982. tmp.pub.channel = channel;
  983. tmp.pub.scan_width = data->scan_width;
  984. tmp.pub.signal = data->signal;
  985. tmp.pub.beacon_interval = beacon_interval;
  986. tmp.pub.capability = capability;
  987. tmp.ts_boottime = data->boottime_ns;
  988. /*
  989. * If we do not know here whether the IEs are from a Beacon or Probe
  990. * Response frame, we need to pick one of the options and only use it
  991. * with the driver that does not provide the full Beacon/Probe Response
  992. * frame. Use Beacon frame pointer to avoid indicating that this should
  993. * override the IEs pointer should we have received an earlier
  994. * indication of Probe Response data.
  995. */
  996. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  997. if (!ies)
  998. return NULL;
  999. ies->len = ielen;
  1000. ies->tsf = tsf;
  1001. ies->from_beacon = false;
  1002. memcpy(ies->data, ie, ielen);
  1003. switch (ftype) {
  1004. case CFG80211_BSS_FTYPE_BEACON:
  1005. ies->from_beacon = true;
  1006. /* fall through to assign */
  1007. case CFG80211_BSS_FTYPE_UNKNOWN:
  1008. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  1009. break;
  1010. case CFG80211_BSS_FTYPE_PRESP:
  1011. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  1012. break;
  1013. }
  1014. rcu_assign_pointer(tmp.pub.ies, ies);
  1015. signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
  1016. wiphy->max_adj_channel_rssi_comp;
  1017. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
  1018. if (!res)
  1019. return NULL;
  1020. if (channel->band == NL80211_BAND_60GHZ) {
  1021. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  1022. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  1023. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  1024. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1025. } else {
  1026. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  1027. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1028. }
  1029. trace_cfg80211_return_bss(&res->pub);
  1030. /* cfg80211_bss_update gives us a referenced result */
  1031. return &res->pub;
  1032. }
  1033. EXPORT_SYMBOL(cfg80211_inform_bss_data);
  1034. /* cfg80211_inform_bss_width_frame helper */
  1035. struct cfg80211_bss *
  1036. cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
  1037. struct cfg80211_inform_bss *data,
  1038. struct ieee80211_mgmt *mgmt, size_t len,
  1039. gfp_t gfp)
  1040. {
  1041. struct cfg80211_internal_bss tmp = {}, *res;
  1042. struct cfg80211_bss_ies *ies;
  1043. struct ieee80211_channel *channel;
  1044. bool signal_valid;
  1045. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  1046. u.probe_resp.variable);
  1047. int bss_type;
  1048. BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
  1049. offsetof(struct ieee80211_mgmt, u.beacon.variable));
  1050. trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
  1051. if (WARN_ON(!mgmt))
  1052. return NULL;
  1053. if (WARN_ON(!wiphy))
  1054. return NULL;
  1055. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  1056. (data->signal < 0 || data->signal > 100)))
  1057. return NULL;
  1058. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  1059. return NULL;
  1060. channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
  1061. ielen, data->chan, data->scan_width);
  1062. if (!channel)
  1063. return NULL;
  1064. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  1065. if (!ies)
  1066. return NULL;
  1067. ies->len = ielen;
  1068. ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  1069. ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
  1070. memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
  1071. if (ieee80211_is_probe_resp(mgmt->frame_control))
  1072. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  1073. else
  1074. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  1075. rcu_assign_pointer(tmp.pub.ies, ies);
  1076. memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
  1077. tmp.pub.channel = channel;
  1078. tmp.pub.scan_width = data->scan_width;
  1079. tmp.pub.signal = data->signal;
  1080. tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  1081. tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  1082. tmp.ts_boottime = data->boottime_ns;
  1083. tmp.parent_tsf = data->parent_tsf;
  1084. ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
  1085. signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
  1086. wiphy->max_adj_channel_rssi_comp;
  1087. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
  1088. if (!res)
  1089. return NULL;
  1090. if (channel->band == NL80211_BAND_60GHZ) {
  1091. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  1092. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  1093. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  1094. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1095. } else {
  1096. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  1097. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1098. }
  1099. trace_cfg80211_return_bss(&res->pub);
  1100. /* cfg80211_bss_update gives us a referenced result */
  1101. return &res->pub;
  1102. }
  1103. EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
  1104. void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1105. {
  1106. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1107. struct cfg80211_internal_bss *bss;
  1108. if (!pub)
  1109. return;
  1110. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1111. spin_lock_bh(&rdev->bss_lock);
  1112. bss_ref_get(rdev, bss);
  1113. spin_unlock_bh(&rdev->bss_lock);
  1114. }
  1115. EXPORT_SYMBOL(cfg80211_ref_bss);
  1116. void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1117. {
  1118. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1119. struct cfg80211_internal_bss *bss;
  1120. if (!pub)
  1121. return;
  1122. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1123. spin_lock_bh(&rdev->bss_lock);
  1124. bss_ref_put(rdev, bss);
  1125. spin_unlock_bh(&rdev->bss_lock);
  1126. }
  1127. EXPORT_SYMBOL(cfg80211_put_bss);
  1128. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1129. {
  1130. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1131. struct cfg80211_internal_bss *bss;
  1132. if (WARN_ON(!pub))
  1133. return;
  1134. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1135. spin_lock_bh(&rdev->bss_lock);
  1136. if (!list_empty(&bss->list)) {
  1137. if (__cfg80211_unlink_bss(rdev, bss))
  1138. rdev->bss_generation++;
  1139. }
  1140. spin_unlock_bh(&rdev->bss_lock);
  1141. }
  1142. EXPORT_SYMBOL(cfg80211_unlink_bss);
  1143. #ifdef CONFIG_CFG80211_WEXT
  1144. static struct cfg80211_registered_device *
  1145. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
  1146. {
  1147. struct cfg80211_registered_device *rdev;
  1148. struct net_device *dev;
  1149. ASSERT_RTNL();
  1150. dev = dev_get_by_index(net, ifindex);
  1151. if (!dev)
  1152. return ERR_PTR(-ENODEV);
  1153. if (dev->ieee80211_ptr)
  1154. rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
  1155. else
  1156. rdev = ERR_PTR(-ENODEV);
  1157. dev_put(dev);
  1158. return rdev;
  1159. }
  1160. int cfg80211_wext_siwscan(struct net_device *dev,
  1161. struct iw_request_info *info,
  1162. union iwreq_data *wrqu, char *extra)
  1163. {
  1164. struct cfg80211_registered_device *rdev;
  1165. struct wiphy *wiphy;
  1166. struct iw_scan_req *wreq = NULL;
  1167. struct cfg80211_scan_request *creq = NULL;
  1168. int i, err, n_channels = 0;
  1169. enum nl80211_band band;
  1170. if (!netif_running(dev))
  1171. return -ENETDOWN;
  1172. if (wrqu->data.length == sizeof(struct iw_scan_req))
  1173. wreq = (struct iw_scan_req *)extra;
  1174. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1175. if (IS_ERR(rdev))
  1176. return PTR_ERR(rdev);
  1177. if (rdev->scan_req || rdev->scan_msg) {
  1178. err = -EBUSY;
  1179. goto out;
  1180. }
  1181. wiphy = &rdev->wiphy;
  1182. /* Determine number of channels, needed to allocate creq */
  1183. if (wreq && wreq->num_channels)
  1184. n_channels = wreq->num_channels;
  1185. else
  1186. n_channels = ieee80211_get_num_supported_channels(wiphy);
  1187. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  1188. n_channels * sizeof(void *),
  1189. GFP_ATOMIC);
  1190. if (!creq) {
  1191. err = -ENOMEM;
  1192. goto out;
  1193. }
  1194. creq->wiphy = wiphy;
  1195. creq->wdev = dev->ieee80211_ptr;
  1196. /* SSIDs come after channels */
  1197. creq->ssids = (void *)&creq->channels[n_channels];
  1198. creq->n_channels = n_channels;
  1199. creq->n_ssids = 1;
  1200. creq->scan_start = jiffies;
  1201. /* translate "Scan on frequencies" request */
  1202. i = 0;
  1203. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  1204. int j;
  1205. if (!wiphy->bands[band])
  1206. continue;
  1207. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  1208. /* ignore disabled channels */
  1209. if (wiphy->bands[band]->channels[j].flags &
  1210. IEEE80211_CHAN_DISABLED)
  1211. continue;
  1212. /* If we have a wireless request structure and the
  1213. * wireless request specifies frequencies, then search
  1214. * for the matching hardware channel.
  1215. */
  1216. if (wreq && wreq->num_channels) {
  1217. int k;
  1218. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  1219. for (k = 0; k < wreq->num_channels; k++) {
  1220. struct iw_freq *freq =
  1221. &wreq->channel_list[k];
  1222. int wext_freq =
  1223. cfg80211_wext_freq(freq);
  1224. if (wext_freq == wiphy_freq)
  1225. goto wext_freq_found;
  1226. }
  1227. goto wext_freq_not_found;
  1228. }
  1229. wext_freq_found:
  1230. creq->channels[i] = &wiphy->bands[band]->channels[j];
  1231. i++;
  1232. wext_freq_not_found: ;
  1233. }
  1234. }
  1235. /* No channels found? */
  1236. if (!i) {
  1237. err = -EINVAL;
  1238. goto out;
  1239. }
  1240. /* Set real number of channels specified in creq->channels[] */
  1241. creq->n_channels = i;
  1242. /* translate "Scan for SSID" request */
  1243. if (wreq) {
  1244. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  1245. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  1246. err = -EINVAL;
  1247. goto out;
  1248. }
  1249. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  1250. creq->ssids[0].ssid_len = wreq->essid_len;
  1251. }
  1252. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  1253. creq->n_ssids = 0;
  1254. }
  1255. for (i = 0; i < NUM_NL80211_BANDS; i++)
  1256. if (wiphy->bands[i])
  1257. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  1258. eth_broadcast_addr(creq->bssid);
  1259. rdev->scan_req = creq;
  1260. err = rdev_scan(rdev, creq);
  1261. if (err) {
  1262. rdev->scan_req = NULL;
  1263. /* creq will be freed below */
  1264. } else {
  1265. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  1266. /* creq now owned by driver */
  1267. creq = NULL;
  1268. dev_hold(dev);
  1269. }
  1270. out:
  1271. kfree(creq);
  1272. return err;
  1273. }
  1274. EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
  1275. static char *ieee80211_scan_add_ies(struct iw_request_info *info,
  1276. const struct cfg80211_bss_ies *ies,
  1277. char *current_ev, char *end_buf)
  1278. {
  1279. const u8 *pos, *end, *next;
  1280. struct iw_event iwe;
  1281. if (!ies)
  1282. return current_ev;
  1283. /*
  1284. * If needed, fragment the IEs buffer (at IE boundaries) into short
  1285. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  1286. */
  1287. pos = ies->data;
  1288. end = pos + ies->len;
  1289. while (end - pos > IW_GENERIC_IE_MAX) {
  1290. next = pos + 2 + pos[1];
  1291. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  1292. next = next + 2 + next[1];
  1293. memset(&iwe, 0, sizeof(iwe));
  1294. iwe.cmd = IWEVGENIE;
  1295. iwe.u.data.length = next - pos;
  1296. current_ev = iwe_stream_add_point_check(info, current_ev,
  1297. end_buf, &iwe,
  1298. (void *)pos);
  1299. if (IS_ERR(current_ev))
  1300. return current_ev;
  1301. pos = next;
  1302. }
  1303. if (end > pos) {
  1304. memset(&iwe, 0, sizeof(iwe));
  1305. iwe.cmd = IWEVGENIE;
  1306. iwe.u.data.length = end - pos;
  1307. current_ev = iwe_stream_add_point_check(info, current_ev,
  1308. end_buf, &iwe,
  1309. (void *)pos);
  1310. if (IS_ERR(current_ev))
  1311. return current_ev;
  1312. }
  1313. return current_ev;
  1314. }
  1315. static char *
  1316. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  1317. struct cfg80211_internal_bss *bss, char *current_ev,
  1318. char *end_buf)
  1319. {
  1320. const struct cfg80211_bss_ies *ies;
  1321. struct iw_event iwe;
  1322. const u8 *ie;
  1323. u8 buf[50];
  1324. u8 *cfg, *p, *tmp;
  1325. int rem, i, sig;
  1326. bool ismesh = false;
  1327. memset(&iwe, 0, sizeof(iwe));
  1328. iwe.cmd = SIOCGIWAP;
  1329. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1330. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  1331. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1332. IW_EV_ADDR_LEN);
  1333. if (IS_ERR(current_ev))
  1334. return current_ev;
  1335. memset(&iwe, 0, sizeof(iwe));
  1336. iwe.cmd = SIOCGIWFREQ;
  1337. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  1338. iwe.u.freq.e = 0;
  1339. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1340. IW_EV_FREQ_LEN);
  1341. if (IS_ERR(current_ev))
  1342. return current_ev;
  1343. memset(&iwe, 0, sizeof(iwe));
  1344. iwe.cmd = SIOCGIWFREQ;
  1345. iwe.u.freq.m = bss->pub.channel->center_freq;
  1346. iwe.u.freq.e = 6;
  1347. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1348. IW_EV_FREQ_LEN);
  1349. if (IS_ERR(current_ev))
  1350. return current_ev;
  1351. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  1352. memset(&iwe, 0, sizeof(iwe));
  1353. iwe.cmd = IWEVQUAL;
  1354. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  1355. IW_QUAL_NOISE_INVALID |
  1356. IW_QUAL_QUAL_UPDATED;
  1357. switch (wiphy->signal_type) {
  1358. case CFG80211_SIGNAL_TYPE_MBM:
  1359. sig = bss->pub.signal / 100;
  1360. iwe.u.qual.level = sig;
  1361. iwe.u.qual.updated |= IW_QUAL_DBM;
  1362. if (sig < -110) /* rather bad */
  1363. sig = -110;
  1364. else if (sig > -40) /* perfect */
  1365. sig = -40;
  1366. /* will give a range of 0 .. 70 */
  1367. iwe.u.qual.qual = sig + 110;
  1368. break;
  1369. case CFG80211_SIGNAL_TYPE_UNSPEC:
  1370. iwe.u.qual.level = bss->pub.signal;
  1371. /* will give range 0 .. 100 */
  1372. iwe.u.qual.qual = bss->pub.signal;
  1373. break;
  1374. default:
  1375. /* not reached */
  1376. break;
  1377. }
  1378. current_ev = iwe_stream_add_event_check(info, current_ev,
  1379. end_buf, &iwe,
  1380. IW_EV_QUAL_LEN);
  1381. if (IS_ERR(current_ev))
  1382. return current_ev;
  1383. }
  1384. memset(&iwe, 0, sizeof(iwe));
  1385. iwe.cmd = SIOCGIWENCODE;
  1386. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  1387. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1388. else
  1389. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1390. iwe.u.data.length = 0;
  1391. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  1392. &iwe, "");
  1393. if (IS_ERR(current_ev))
  1394. return current_ev;
  1395. rcu_read_lock();
  1396. ies = rcu_dereference(bss->pub.ies);
  1397. rem = ies->len;
  1398. ie = ies->data;
  1399. while (rem >= 2) {
  1400. /* invalid data */
  1401. if (ie[1] > rem - 2)
  1402. break;
  1403. switch (ie[0]) {
  1404. case WLAN_EID_SSID:
  1405. memset(&iwe, 0, sizeof(iwe));
  1406. iwe.cmd = SIOCGIWESSID;
  1407. iwe.u.data.length = ie[1];
  1408. iwe.u.data.flags = 1;
  1409. current_ev = iwe_stream_add_point_check(info,
  1410. current_ev,
  1411. end_buf, &iwe,
  1412. (u8 *)ie + 2);
  1413. if (IS_ERR(current_ev))
  1414. goto unlock;
  1415. break;
  1416. case WLAN_EID_MESH_ID:
  1417. memset(&iwe, 0, sizeof(iwe));
  1418. iwe.cmd = SIOCGIWESSID;
  1419. iwe.u.data.length = ie[1];
  1420. iwe.u.data.flags = 1;
  1421. current_ev = iwe_stream_add_point_check(info,
  1422. current_ev,
  1423. end_buf, &iwe,
  1424. (u8 *)ie + 2);
  1425. if (IS_ERR(current_ev))
  1426. goto unlock;
  1427. break;
  1428. case WLAN_EID_MESH_CONFIG:
  1429. ismesh = true;
  1430. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  1431. break;
  1432. cfg = (u8 *)ie + 2;
  1433. memset(&iwe, 0, sizeof(iwe));
  1434. iwe.cmd = IWEVCUSTOM;
  1435. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  1436. "0x%02X", cfg[0]);
  1437. iwe.u.data.length = strlen(buf);
  1438. current_ev = iwe_stream_add_point_check(info,
  1439. current_ev,
  1440. end_buf,
  1441. &iwe, buf);
  1442. if (IS_ERR(current_ev))
  1443. goto unlock;
  1444. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  1445. cfg[1]);
  1446. iwe.u.data.length = strlen(buf);
  1447. current_ev = iwe_stream_add_point_check(info,
  1448. current_ev,
  1449. end_buf,
  1450. &iwe, buf);
  1451. if (IS_ERR(current_ev))
  1452. goto unlock;
  1453. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  1454. cfg[2]);
  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. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  1463. iwe.u.data.length = strlen(buf);
  1464. current_ev = iwe_stream_add_point_check(info,
  1465. current_ev,
  1466. end_buf,
  1467. &iwe, buf);
  1468. if (IS_ERR(current_ev))
  1469. goto unlock;
  1470. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  1471. iwe.u.data.length = strlen(buf);
  1472. current_ev = iwe_stream_add_point_check(info,
  1473. current_ev,
  1474. end_buf,
  1475. &iwe, buf);
  1476. if (IS_ERR(current_ev))
  1477. goto unlock;
  1478. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  1479. iwe.u.data.length = strlen(buf);
  1480. current_ev = iwe_stream_add_point_check(info,
  1481. current_ev,
  1482. end_buf,
  1483. &iwe, buf);
  1484. if (IS_ERR(current_ev))
  1485. goto unlock;
  1486. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1487. iwe.u.data.length = strlen(buf);
  1488. current_ev = iwe_stream_add_point_check(info,
  1489. current_ev,
  1490. end_buf,
  1491. &iwe, buf);
  1492. if (IS_ERR(current_ev))
  1493. goto unlock;
  1494. break;
  1495. case WLAN_EID_SUPP_RATES:
  1496. case WLAN_EID_EXT_SUPP_RATES:
  1497. /* display all supported rates in readable format */
  1498. p = current_ev + iwe_stream_lcp_len(info);
  1499. memset(&iwe, 0, sizeof(iwe));
  1500. iwe.cmd = SIOCGIWRATE;
  1501. /* Those two flags are ignored... */
  1502. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1503. for (i = 0; i < ie[1]; i++) {
  1504. iwe.u.bitrate.value =
  1505. ((ie[i + 2] & 0x7f) * 500000);
  1506. tmp = p;
  1507. p = iwe_stream_add_value(info, current_ev, p,
  1508. end_buf, &iwe,
  1509. IW_EV_PARAM_LEN);
  1510. if (p == tmp) {
  1511. current_ev = ERR_PTR(-E2BIG);
  1512. goto unlock;
  1513. }
  1514. }
  1515. current_ev = p;
  1516. break;
  1517. }
  1518. rem -= ie[1] + 2;
  1519. ie += ie[1] + 2;
  1520. }
  1521. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1522. ismesh) {
  1523. memset(&iwe, 0, sizeof(iwe));
  1524. iwe.cmd = SIOCGIWMODE;
  1525. if (ismesh)
  1526. iwe.u.mode = IW_MODE_MESH;
  1527. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1528. iwe.u.mode = IW_MODE_MASTER;
  1529. else
  1530. iwe.u.mode = IW_MODE_ADHOC;
  1531. current_ev = iwe_stream_add_event_check(info, current_ev,
  1532. end_buf, &iwe,
  1533. IW_EV_UINT_LEN);
  1534. if (IS_ERR(current_ev))
  1535. goto unlock;
  1536. }
  1537. memset(&iwe, 0, sizeof(iwe));
  1538. iwe.cmd = IWEVCUSTOM;
  1539. sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
  1540. iwe.u.data.length = strlen(buf);
  1541. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  1542. &iwe, buf);
  1543. if (IS_ERR(current_ev))
  1544. goto unlock;
  1545. memset(&iwe, 0, sizeof(iwe));
  1546. iwe.cmd = IWEVCUSTOM;
  1547. sprintf(buf, " Last beacon: %ums ago",
  1548. elapsed_jiffies_msecs(bss->ts));
  1549. iwe.u.data.length = strlen(buf);
  1550. current_ev = iwe_stream_add_point_check(info, current_ev,
  1551. end_buf, &iwe, buf);
  1552. if (IS_ERR(current_ev))
  1553. goto unlock;
  1554. current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
  1555. unlock:
  1556. rcu_read_unlock();
  1557. return current_ev;
  1558. }
  1559. static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
  1560. struct iw_request_info *info,
  1561. char *buf, size_t len)
  1562. {
  1563. char *current_ev = buf;
  1564. char *end_buf = buf + len;
  1565. struct cfg80211_internal_bss *bss;
  1566. int err = 0;
  1567. spin_lock_bh(&rdev->bss_lock);
  1568. cfg80211_bss_expire(rdev);
  1569. list_for_each_entry(bss, &rdev->bss_list, list) {
  1570. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1571. err = -E2BIG;
  1572. break;
  1573. }
  1574. current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
  1575. current_ev, end_buf);
  1576. if (IS_ERR(current_ev)) {
  1577. err = PTR_ERR(current_ev);
  1578. break;
  1579. }
  1580. }
  1581. spin_unlock_bh(&rdev->bss_lock);
  1582. if (err)
  1583. return err;
  1584. return current_ev - buf;
  1585. }
  1586. int cfg80211_wext_giwscan(struct net_device *dev,
  1587. struct iw_request_info *info,
  1588. struct iw_point *data, char *extra)
  1589. {
  1590. struct cfg80211_registered_device *rdev;
  1591. int res;
  1592. if (!netif_running(dev))
  1593. return -ENETDOWN;
  1594. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1595. if (IS_ERR(rdev))
  1596. return PTR_ERR(rdev);
  1597. if (rdev->scan_req || rdev->scan_msg)
  1598. return -EAGAIN;
  1599. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1600. data->length = 0;
  1601. if (res >= 0) {
  1602. data->length = res;
  1603. res = 0;
  1604. }
  1605. return res;
  1606. }
  1607. EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
  1608. #endif