scan.c 34 KB

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