scan.c 49 KB

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