scan.c 43 KB

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