scan.c 39 KB

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