scan.c 38 KB

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