scan.c 38 KB

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