scan.c 38 KB

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