scan.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  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