scan.c 38 KB

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