scan.c 40 KB

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