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