scan.c 40 KB

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