scan.c 39 KB

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