scan.c 38 KB

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