scan.c 43 KB

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