scan.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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.signal = tmp->pub.signal;
  589. found->pub.capability = tmp->pub.capability;
  590. found->ts = tmp->ts;
  591. /* Update IEs */
  592. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  593. const struct cfg80211_bss_ies *old;
  594. old = rcu_access_pointer(found->pub.proberesp_ies);
  595. rcu_assign_pointer(found->pub.proberesp_ies,
  596. tmp->pub.proberesp_ies);
  597. /* Override possible earlier Beacon frame IEs */
  598. rcu_assign_pointer(found->pub.ies,
  599. tmp->pub.proberesp_ies);
  600. if (old)
  601. kfree_rcu((struct cfg80211_bss_ies *)old,
  602. rcu_head);
  603. } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
  604. const struct cfg80211_bss_ies *old;
  605. struct cfg80211_internal_bss *bss;
  606. if (found->pub.hidden_beacon_bss &&
  607. !list_empty(&found->hidden_list)) {
  608. /*
  609. * The found BSS struct is one of the probe
  610. * response members of a group, but we're
  611. * receiving a beacon (beacon_ies in the tmp
  612. * bss is used). This can only mean that the
  613. * AP changed its beacon from not having an
  614. * SSID to showing it, which is confusing so
  615. * drop this information.
  616. */
  617. goto drop;
  618. }
  619. old = rcu_access_pointer(found->pub.beacon_ies);
  620. rcu_assign_pointer(found->pub.beacon_ies,
  621. tmp->pub.beacon_ies);
  622. /* Override IEs if they were from a beacon before */
  623. if (old == rcu_access_pointer(found->pub.ies))
  624. rcu_assign_pointer(found->pub.ies,
  625. tmp->pub.beacon_ies);
  626. /* Assign beacon IEs to all sub entries */
  627. list_for_each_entry(bss, &found->hidden_list,
  628. hidden_list) {
  629. const struct cfg80211_bss_ies *ies;
  630. ies = rcu_access_pointer(bss->pub.beacon_ies);
  631. WARN_ON(ies != old);
  632. rcu_assign_pointer(bss->pub.beacon_ies,
  633. tmp->pub.beacon_ies);
  634. }
  635. if (old)
  636. kfree_rcu((struct cfg80211_bss_ies *)old,
  637. rcu_head);
  638. }
  639. } else {
  640. struct cfg80211_internal_bss *new;
  641. struct cfg80211_internal_bss *hidden;
  642. struct cfg80211_bss_ies *ies;
  643. /*
  644. * create a copy -- the "res" variable that is passed in
  645. * is allocated on the stack since it's not needed in the
  646. * more common case of an update
  647. */
  648. new = kzalloc(sizeof(*new) + dev->wiphy.bss_priv_size,
  649. GFP_ATOMIC);
  650. if (!new) {
  651. ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
  652. if (ies)
  653. kfree_rcu(ies, rcu_head);
  654. ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
  655. if (ies)
  656. kfree_rcu(ies, rcu_head);
  657. goto drop;
  658. }
  659. memcpy(new, tmp, sizeof(*new));
  660. new->refcount = 1;
  661. INIT_LIST_HEAD(&new->hidden_list);
  662. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  663. hidden = rb_find_bss(dev, tmp, BSS_CMP_HIDE_ZLEN);
  664. if (!hidden)
  665. hidden = rb_find_bss(dev, tmp,
  666. BSS_CMP_HIDE_NUL);
  667. if (hidden) {
  668. new->pub.hidden_beacon_bss = &hidden->pub;
  669. list_add(&new->hidden_list,
  670. &hidden->hidden_list);
  671. hidden->refcount++;
  672. rcu_assign_pointer(new->pub.beacon_ies,
  673. hidden->pub.beacon_ies);
  674. }
  675. } else {
  676. /*
  677. * Ok so we found a beacon, and don't have an entry. If
  678. * it's a beacon with hidden SSID, we might be in for an
  679. * expensive search for any probe responses that should
  680. * be grouped with this beacon for updates ...
  681. */
  682. if (!cfg80211_combine_bsses(dev, new)) {
  683. kfree(new);
  684. goto drop;
  685. }
  686. }
  687. list_add_tail(&new->list, &dev->bss_list);
  688. rb_insert_bss(dev, new);
  689. found = new;
  690. }
  691. dev->bss_generation++;
  692. bss_ref_get(dev, found);
  693. spin_unlock_bh(&dev->bss_lock);
  694. return found;
  695. drop:
  696. spin_unlock_bh(&dev->bss_lock);
  697. return NULL;
  698. }
  699. static struct ieee80211_channel *
  700. cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
  701. struct ieee80211_channel *channel)
  702. {
  703. const u8 *tmp;
  704. u32 freq;
  705. int channel_number = -1;
  706. tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
  707. if (tmp && tmp[1] == 1) {
  708. channel_number = tmp[2];
  709. } else {
  710. tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
  711. if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
  712. struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
  713. channel_number = htop->primary_chan;
  714. }
  715. }
  716. if (channel_number < 0)
  717. return channel;
  718. freq = ieee80211_channel_to_frequency(channel_number, channel->band);
  719. channel = ieee80211_get_channel(wiphy, freq);
  720. if (!channel)
  721. return NULL;
  722. if (channel->flags & IEEE80211_CHAN_DISABLED)
  723. return NULL;
  724. return channel;
  725. }
  726. struct cfg80211_bss*
  727. cfg80211_inform_bss(struct wiphy *wiphy,
  728. struct ieee80211_channel *channel,
  729. const u8 *bssid, u64 tsf, u16 capability,
  730. u16 beacon_interval, const u8 *ie, size_t ielen,
  731. s32 signal, gfp_t gfp)
  732. {
  733. struct cfg80211_bss_ies *ies;
  734. struct cfg80211_internal_bss tmp = {}, *res;
  735. if (WARN_ON(!wiphy))
  736. return NULL;
  737. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  738. (signal < 0 || signal > 100)))
  739. return NULL;
  740. channel = cfg80211_get_bss_channel(wiphy, ie, ielen, channel);
  741. if (!channel)
  742. return NULL;
  743. memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
  744. tmp.pub.channel = channel;
  745. tmp.pub.signal = signal;
  746. tmp.pub.beacon_interval = beacon_interval;
  747. tmp.pub.capability = capability;
  748. /*
  749. * Since we do not know here whether the IEs are from a Beacon or Probe
  750. * Response frame, we need to pick one of the options and only use it
  751. * with the driver that does not provide the full Beacon/Probe Response
  752. * frame. Use Beacon frame pointer to avoid indicating that this should
  753. * override the IEs pointer should we have received an earlier
  754. * indication of Probe Response data.
  755. */
  756. ies = kmalloc(sizeof(*ies) + ielen, gfp);
  757. if (!ies)
  758. return NULL;
  759. ies->len = ielen;
  760. ies->tsf = tsf;
  761. memcpy(ies->data, ie, ielen);
  762. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  763. rcu_assign_pointer(tmp.pub.ies, ies);
  764. res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
  765. if (!res)
  766. return NULL;
  767. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  768. regulatory_hint_found_beacon(wiphy, channel, gfp);
  769. trace_cfg80211_return_bss(&res->pub);
  770. /* cfg80211_bss_update gives us a referenced result */
  771. return &res->pub;
  772. }
  773. EXPORT_SYMBOL(cfg80211_inform_bss);
  774. struct cfg80211_bss *
  775. cfg80211_inform_bss_frame(struct wiphy *wiphy,
  776. struct ieee80211_channel *channel,
  777. struct ieee80211_mgmt *mgmt, size_t len,
  778. s32 signal, gfp_t gfp)
  779. {
  780. struct cfg80211_internal_bss tmp = {}, *res;
  781. struct cfg80211_bss_ies *ies;
  782. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  783. u.probe_resp.variable);
  784. BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
  785. offsetof(struct ieee80211_mgmt, u.beacon.variable));
  786. trace_cfg80211_inform_bss_frame(wiphy, channel, mgmt, len, signal);
  787. if (WARN_ON(!mgmt))
  788. return NULL;
  789. if (WARN_ON(!wiphy))
  790. return NULL;
  791. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  792. (signal < 0 || signal > 100)))
  793. return NULL;
  794. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  795. return NULL;
  796. channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
  797. ielen, channel);
  798. if (!channel)
  799. return NULL;
  800. ies = kmalloc(sizeof(*ies) + ielen, gfp);
  801. if (!ies)
  802. return NULL;
  803. ies->len = ielen;
  804. ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  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.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  815. tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  816. res = cfg80211_bss_update(wiphy_to_dev(wiphy), &tmp);
  817. if (!res)
  818. return NULL;
  819. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  820. regulatory_hint_found_beacon(wiphy, channel, gfp);
  821. trace_cfg80211_return_bss(&res->pub);
  822. /* cfg80211_bss_update gives us a referenced result */
  823. return &res->pub;
  824. }
  825. EXPORT_SYMBOL(cfg80211_inform_bss_frame);
  826. void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  827. {
  828. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  829. struct cfg80211_internal_bss *bss;
  830. if (!pub)
  831. return;
  832. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  833. spin_lock_bh(&dev->bss_lock);
  834. bss_ref_get(dev, bss);
  835. spin_unlock_bh(&dev->bss_lock);
  836. }
  837. EXPORT_SYMBOL(cfg80211_ref_bss);
  838. void cfg80211_put_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_put(dev, bss);
  847. spin_unlock_bh(&dev->bss_lock);
  848. }
  849. EXPORT_SYMBOL(cfg80211_put_bss);
  850. void cfg80211_unlink_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 (WARN_ON(!pub))
  855. return;
  856. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  857. spin_lock_bh(&dev->bss_lock);
  858. if (!list_empty(&bss->list)) {
  859. if (__cfg80211_unlink_bss(dev, bss))
  860. dev->bss_generation++;
  861. }
  862. spin_unlock_bh(&dev->bss_lock);
  863. }
  864. EXPORT_SYMBOL(cfg80211_unlink_bss);
  865. #ifdef CONFIG_CFG80211_WEXT
  866. int cfg80211_wext_siwscan(struct net_device *dev,
  867. struct iw_request_info *info,
  868. union iwreq_data *wrqu, char *extra)
  869. {
  870. struct cfg80211_registered_device *rdev;
  871. struct wiphy *wiphy;
  872. struct iw_scan_req *wreq = NULL;
  873. struct cfg80211_scan_request *creq = NULL;
  874. int i, err, n_channels = 0;
  875. enum ieee80211_band band;
  876. if (!netif_running(dev))
  877. return -ENETDOWN;
  878. if (wrqu->data.length == sizeof(struct iw_scan_req))
  879. wreq = (struct iw_scan_req *)extra;
  880. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  881. if (IS_ERR(rdev))
  882. return PTR_ERR(rdev);
  883. if (rdev->scan_req) {
  884. err = -EBUSY;
  885. goto out;
  886. }
  887. wiphy = &rdev->wiphy;
  888. /* Determine number of channels, needed to allocate creq */
  889. if (wreq && wreq->num_channels)
  890. n_channels = wreq->num_channels;
  891. else {
  892. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  893. if (wiphy->bands[band])
  894. n_channels += wiphy->bands[band]->n_channels;
  895. }
  896. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  897. n_channels * sizeof(void *),
  898. GFP_ATOMIC);
  899. if (!creq) {
  900. err = -ENOMEM;
  901. goto out;
  902. }
  903. creq->wiphy = wiphy;
  904. creq->wdev = dev->ieee80211_ptr;
  905. /* SSIDs come after channels */
  906. creq->ssids = (void *)&creq->channels[n_channels];
  907. creq->n_channels = n_channels;
  908. creq->n_ssids = 1;
  909. creq->scan_start = jiffies;
  910. /* translate "Scan on frequencies" request */
  911. i = 0;
  912. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  913. int j;
  914. if (!wiphy->bands[band])
  915. continue;
  916. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  917. /* ignore disabled channels */
  918. if (wiphy->bands[band]->channels[j].flags &
  919. IEEE80211_CHAN_DISABLED)
  920. continue;
  921. /* If we have a wireless request structure and the
  922. * wireless request specifies frequencies, then search
  923. * for the matching hardware channel.
  924. */
  925. if (wreq && wreq->num_channels) {
  926. int k;
  927. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  928. for (k = 0; k < wreq->num_channels; k++) {
  929. int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
  930. if (wext_freq == wiphy_freq)
  931. goto wext_freq_found;
  932. }
  933. goto wext_freq_not_found;
  934. }
  935. wext_freq_found:
  936. creq->channels[i] = &wiphy->bands[band]->channels[j];
  937. i++;
  938. wext_freq_not_found: ;
  939. }
  940. }
  941. /* No channels found? */
  942. if (!i) {
  943. err = -EINVAL;
  944. goto out;
  945. }
  946. /* Set real number of channels specified in creq->channels[] */
  947. creq->n_channels = i;
  948. /* translate "Scan for SSID" request */
  949. if (wreq) {
  950. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  951. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  952. err = -EINVAL;
  953. goto out;
  954. }
  955. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  956. creq->ssids[0].ssid_len = wreq->essid_len;
  957. }
  958. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  959. creq->n_ssids = 0;
  960. }
  961. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  962. if (wiphy->bands[i])
  963. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  964. rdev->scan_req = creq;
  965. err = rdev_scan(rdev, creq);
  966. if (err) {
  967. rdev->scan_req = NULL;
  968. /* creq will be freed below */
  969. } else {
  970. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  971. /* creq now owned by driver */
  972. creq = NULL;
  973. dev_hold(dev);
  974. }
  975. out:
  976. kfree(creq);
  977. cfg80211_unlock_rdev(rdev);
  978. return err;
  979. }
  980. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  981. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  982. const struct cfg80211_bss_ies *ies,
  983. char **current_ev, char *end_buf)
  984. {
  985. const u8 *pos, *end, *next;
  986. struct iw_event iwe;
  987. if (!ies)
  988. return;
  989. /*
  990. * If needed, fragment the IEs buffer (at IE boundaries) into short
  991. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  992. */
  993. pos = ies->data;
  994. end = pos + ies->len;
  995. while (end - pos > IW_GENERIC_IE_MAX) {
  996. next = pos + 2 + pos[1];
  997. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  998. next = next + 2 + next[1];
  999. memset(&iwe, 0, sizeof(iwe));
  1000. iwe.cmd = IWEVGENIE;
  1001. iwe.u.data.length = next - pos;
  1002. *current_ev = iwe_stream_add_point(info, *current_ev,
  1003. end_buf, &iwe,
  1004. (void *)pos);
  1005. pos = next;
  1006. }
  1007. if (end > pos) {
  1008. memset(&iwe, 0, sizeof(iwe));
  1009. iwe.cmd = IWEVGENIE;
  1010. iwe.u.data.length = end - pos;
  1011. *current_ev = iwe_stream_add_point(info, *current_ev,
  1012. end_buf, &iwe,
  1013. (void *)pos);
  1014. }
  1015. }
  1016. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  1017. {
  1018. unsigned long end = jiffies;
  1019. if (end >= start)
  1020. return jiffies_to_msecs(end - start);
  1021. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  1022. }
  1023. static char *
  1024. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  1025. struct cfg80211_internal_bss *bss, char *current_ev,
  1026. char *end_buf)
  1027. {
  1028. const struct cfg80211_bss_ies *ies;
  1029. struct iw_event iwe;
  1030. const u8 *ie;
  1031. u8 *buf, *cfg, *p;
  1032. int rem, i, sig;
  1033. bool ismesh = false;
  1034. memset(&iwe, 0, sizeof(iwe));
  1035. iwe.cmd = SIOCGIWAP;
  1036. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1037. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  1038. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  1039. IW_EV_ADDR_LEN);
  1040. memset(&iwe, 0, sizeof(iwe));
  1041. iwe.cmd = SIOCGIWFREQ;
  1042. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  1043. iwe.u.freq.e = 0;
  1044. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  1045. IW_EV_FREQ_LEN);
  1046. memset(&iwe, 0, sizeof(iwe));
  1047. iwe.cmd = SIOCGIWFREQ;
  1048. iwe.u.freq.m = bss->pub.channel->center_freq;
  1049. iwe.u.freq.e = 6;
  1050. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  1051. IW_EV_FREQ_LEN);
  1052. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  1053. memset(&iwe, 0, sizeof(iwe));
  1054. iwe.cmd = IWEVQUAL;
  1055. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  1056. IW_QUAL_NOISE_INVALID |
  1057. IW_QUAL_QUAL_UPDATED;
  1058. switch (wiphy->signal_type) {
  1059. case CFG80211_SIGNAL_TYPE_MBM:
  1060. sig = bss->pub.signal / 100;
  1061. iwe.u.qual.level = sig;
  1062. iwe.u.qual.updated |= IW_QUAL_DBM;
  1063. if (sig < -110) /* rather bad */
  1064. sig = -110;
  1065. else if (sig > -40) /* perfect */
  1066. sig = -40;
  1067. /* will give a range of 0 .. 70 */
  1068. iwe.u.qual.qual = sig + 110;
  1069. break;
  1070. case CFG80211_SIGNAL_TYPE_UNSPEC:
  1071. iwe.u.qual.level = bss->pub.signal;
  1072. /* will give range 0 .. 100 */
  1073. iwe.u.qual.qual = bss->pub.signal;
  1074. break;
  1075. default:
  1076. /* not reached */
  1077. break;
  1078. }
  1079. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  1080. &iwe, IW_EV_QUAL_LEN);
  1081. }
  1082. memset(&iwe, 0, sizeof(iwe));
  1083. iwe.cmd = SIOCGIWENCODE;
  1084. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  1085. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1086. else
  1087. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1088. iwe.u.data.length = 0;
  1089. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1090. &iwe, "");
  1091. rcu_read_lock();
  1092. ies = rcu_dereference(bss->pub.ies);
  1093. rem = ies->len;
  1094. ie = ies->data;
  1095. while (rem >= 2) {
  1096. /* invalid data */
  1097. if (ie[1] > rem - 2)
  1098. break;
  1099. switch (ie[0]) {
  1100. case WLAN_EID_SSID:
  1101. memset(&iwe, 0, sizeof(iwe));
  1102. iwe.cmd = SIOCGIWESSID;
  1103. iwe.u.data.length = ie[1];
  1104. iwe.u.data.flags = 1;
  1105. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1106. &iwe, (u8 *)ie + 2);
  1107. break;
  1108. case WLAN_EID_MESH_ID:
  1109. memset(&iwe, 0, sizeof(iwe));
  1110. iwe.cmd = SIOCGIWESSID;
  1111. iwe.u.data.length = ie[1];
  1112. iwe.u.data.flags = 1;
  1113. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1114. &iwe, (u8 *)ie + 2);
  1115. break;
  1116. case WLAN_EID_MESH_CONFIG:
  1117. ismesh = true;
  1118. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  1119. break;
  1120. buf = kmalloc(50, GFP_ATOMIC);
  1121. if (!buf)
  1122. break;
  1123. cfg = (u8 *)ie + 2;
  1124. memset(&iwe, 0, sizeof(iwe));
  1125. iwe.cmd = IWEVCUSTOM;
  1126. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  1127. "0x%02X", cfg[0]);
  1128. iwe.u.data.length = strlen(buf);
  1129. current_ev = iwe_stream_add_point(info, current_ev,
  1130. end_buf,
  1131. &iwe, buf);
  1132. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  1133. cfg[1]);
  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, "Congestion Control Mode ID: 0x%02X",
  1139. cfg[2]);
  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, "Synchronization ID: 0x%02X", cfg[3]);
  1145. iwe.u.data.length = strlen(buf);
  1146. current_ev = iwe_stream_add_point(info, current_ev,
  1147. end_buf,
  1148. &iwe, buf);
  1149. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  1150. iwe.u.data.length = strlen(buf);
  1151. current_ev = iwe_stream_add_point(info, current_ev,
  1152. end_buf,
  1153. &iwe, buf);
  1154. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  1155. iwe.u.data.length = strlen(buf);
  1156. current_ev = iwe_stream_add_point(info, current_ev,
  1157. end_buf,
  1158. &iwe, buf);
  1159. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1160. iwe.u.data.length = strlen(buf);
  1161. current_ev = iwe_stream_add_point(info, current_ev,
  1162. end_buf,
  1163. &iwe, buf);
  1164. kfree(buf);
  1165. break;
  1166. case WLAN_EID_SUPP_RATES:
  1167. case WLAN_EID_EXT_SUPP_RATES:
  1168. /* display all supported rates in readable format */
  1169. p = current_ev + iwe_stream_lcp_len(info);
  1170. memset(&iwe, 0, sizeof(iwe));
  1171. iwe.cmd = SIOCGIWRATE;
  1172. /* Those two flags are ignored... */
  1173. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1174. for (i = 0; i < ie[1]; i++) {
  1175. iwe.u.bitrate.value =
  1176. ((ie[i + 2] & 0x7f) * 500000);
  1177. p = iwe_stream_add_value(info, current_ev, p,
  1178. end_buf, &iwe, IW_EV_PARAM_LEN);
  1179. }
  1180. current_ev = p;
  1181. break;
  1182. }
  1183. rem -= ie[1] + 2;
  1184. ie += ie[1] + 2;
  1185. }
  1186. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1187. ismesh) {
  1188. memset(&iwe, 0, sizeof(iwe));
  1189. iwe.cmd = SIOCGIWMODE;
  1190. if (ismesh)
  1191. iwe.u.mode = IW_MODE_MESH;
  1192. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1193. iwe.u.mode = IW_MODE_MASTER;
  1194. else
  1195. iwe.u.mode = IW_MODE_ADHOC;
  1196. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  1197. &iwe, IW_EV_UINT_LEN);
  1198. }
  1199. buf = kmalloc(31, GFP_ATOMIC);
  1200. if (buf) {
  1201. memset(&iwe, 0, sizeof(iwe));
  1202. iwe.cmd = IWEVCUSTOM;
  1203. sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
  1204. iwe.u.data.length = strlen(buf);
  1205. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1206. &iwe, buf);
  1207. memset(&iwe, 0, sizeof(iwe));
  1208. iwe.cmd = IWEVCUSTOM;
  1209. sprintf(buf, " Last beacon: %ums ago",
  1210. elapsed_jiffies_msecs(bss->ts));
  1211. iwe.u.data.length = strlen(buf);
  1212. current_ev = iwe_stream_add_point(info, current_ev,
  1213. end_buf, &iwe, buf);
  1214. kfree(buf);
  1215. }
  1216. ieee80211_scan_add_ies(info, ies, &current_ev, end_buf);
  1217. rcu_read_unlock();
  1218. return current_ev;
  1219. }
  1220. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  1221. struct iw_request_info *info,
  1222. char *buf, size_t len)
  1223. {
  1224. char *current_ev = buf;
  1225. char *end_buf = buf + len;
  1226. struct cfg80211_internal_bss *bss;
  1227. spin_lock_bh(&dev->bss_lock);
  1228. cfg80211_bss_expire(dev);
  1229. list_for_each_entry(bss, &dev->bss_list, list) {
  1230. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1231. spin_unlock_bh(&dev->bss_lock);
  1232. return -E2BIG;
  1233. }
  1234. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  1235. current_ev, end_buf);
  1236. }
  1237. spin_unlock_bh(&dev->bss_lock);
  1238. return current_ev - buf;
  1239. }
  1240. int cfg80211_wext_giwscan(struct net_device *dev,
  1241. struct iw_request_info *info,
  1242. struct iw_point *data, char *extra)
  1243. {
  1244. struct cfg80211_registered_device *rdev;
  1245. int res;
  1246. if (!netif_running(dev))
  1247. return -ENETDOWN;
  1248. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1249. if (IS_ERR(rdev))
  1250. return PTR_ERR(rdev);
  1251. if (rdev->scan_req) {
  1252. res = -EAGAIN;
  1253. goto out;
  1254. }
  1255. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1256. data->length = 0;
  1257. if (res >= 0) {
  1258. data->length = res;
  1259. res = 0;
  1260. }
  1261. out:
  1262. cfg80211_unlock_rdev(rdev);
  1263. return res;
  1264. }
  1265. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  1266. #endif