scan.c 36 KB

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