scan.c 38 KB

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