scan.c 47 KB

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