scan.c 43 KB

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