scan.c 43 KB

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