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