scan.c 39 KB

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