scan.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357
  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 (30 * 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 && !ether_addr_equal(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 (a->channel != b->channel)
  294. return b->channel->center_freq - a->channel->center_freq;
  295. if (is_mesh_bss(a) && is_mesh_bss(b)) {
  296. r = cmp_ies(WLAN_EID_MESH_ID,
  297. a->information_elements,
  298. a->len_information_elements,
  299. b->information_elements,
  300. b->len_information_elements);
  301. if (r)
  302. return r;
  303. return cmp_ies(WLAN_EID_MESH_CONFIG,
  304. a->information_elements,
  305. a->len_information_elements,
  306. b->information_elements,
  307. b->len_information_elements);
  308. }
  309. /*
  310. * we can't use compare_ether_addr here since we need a < > operator.
  311. * The binary return value of compare_ether_addr isn't enough
  312. */
  313. return memcmp(a->bssid, b->bssid, sizeof(a->bssid));
  314. }
  315. static int cmp_bss(struct cfg80211_bss *a,
  316. struct cfg80211_bss *b)
  317. {
  318. int r;
  319. r = cmp_bss_core(a, b);
  320. if (r)
  321. return r;
  322. return cmp_ies(WLAN_EID_SSID,
  323. a->information_elements,
  324. a->len_information_elements,
  325. b->information_elements,
  326. b->len_information_elements);
  327. }
  328. static int cmp_hidden_bss(struct cfg80211_bss *a,
  329. struct cfg80211_bss *b)
  330. {
  331. const u8 *ie1;
  332. const u8 *ie2;
  333. int i;
  334. int r;
  335. r = cmp_bss_core(a, b);
  336. if (r)
  337. return r;
  338. ie1 = cfg80211_find_ie(WLAN_EID_SSID,
  339. a->information_elements,
  340. a->len_information_elements);
  341. ie2 = cfg80211_find_ie(WLAN_EID_SSID,
  342. b->information_elements,
  343. b->len_information_elements);
  344. /* Key comparator must use same algorithm in any rb-tree
  345. * search function (order is important), otherwise ordering
  346. * of items in the tree is broken and search gives incorrect
  347. * results. This code uses same order as cmp_ies() does. */
  348. /* sort missing IE before (left of) present IE */
  349. if (!ie1)
  350. return -1;
  351. if (!ie2)
  352. return 1;
  353. /* zero-size SSID is used as an indication of the hidden bss */
  354. if (!ie2[1])
  355. return 0;
  356. /* sort by length first, then by contents */
  357. if (ie1[1] != ie2[1])
  358. return ie2[1] - ie1[1];
  359. /* zeroed SSID ie is another indication of a hidden bss */
  360. for (i = 0; i < ie2[1]; i++)
  361. if (ie2[i + 2])
  362. return -1;
  363. return 0;
  364. }
  365. struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
  366. struct ieee80211_channel *channel,
  367. const u8 *bssid,
  368. const u8 *ssid, size_t ssid_len,
  369. u16 capa_mask, u16 capa_val)
  370. {
  371. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  372. struct cfg80211_internal_bss *bss, *res = NULL;
  373. unsigned long now = jiffies;
  374. spin_lock_bh(&dev->bss_lock);
  375. list_for_each_entry(bss, &dev->bss_list, list) {
  376. if ((bss->pub.capability & capa_mask) != capa_val)
  377. continue;
  378. if (channel && bss->pub.channel != channel)
  379. continue;
  380. /* Don't get expired BSS structs */
  381. if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
  382. !atomic_read(&bss->hold))
  383. continue;
  384. if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
  385. res = bss;
  386. kref_get(&res->ref);
  387. break;
  388. }
  389. }
  390. spin_unlock_bh(&dev->bss_lock);
  391. if (!res)
  392. return NULL;
  393. return &res->pub;
  394. }
  395. EXPORT_SYMBOL(cfg80211_get_bss);
  396. struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
  397. struct ieee80211_channel *channel,
  398. const u8 *meshid, size_t meshidlen,
  399. const u8 *meshcfg)
  400. {
  401. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  402. struct cfg80211_internal_bss *bss, *res = NULL;
  403. spin_lock_bh(&dev->bss_lock);
  404. list_for_each_entry(bss, &dev->bss_list, list) {
  405. if (channel && bss->pub.channel != channel)
  406. continue;
  407. if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
  408. res = bss;
  409. kref_get(&res->ref);
  410. break;
  411. }
  412. }
  413. spin_unlock_bh(&dev->bss_lock);
  414. if (!res)
  415. return NULL;
  416. return &res->pub;
  417. }
  418. EXPORT_SYMBOL(cfg80211_get_mesh);
  419. static void rb_insert_bss(struct cfg80211_registered_device *dev,
  420. struct cfg80211_internal_bss *bss)
  421. {
  422. struct rb_node **p = &dev->bss_tree.rb_node;
  423. struct rb_node *parent = NULL;
  424. struct cfg80211_internal_bss *tbss;
  425. int cmp;
  426. while (*p) {
  427. parent = *p;
  428. tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
  429. cmp = cmp_bss(&bss->pub, &tbss->pub);
  430. if (WARN_ON(!cmp)) {
  431. /* will sort of leak this BSS */
  432. return;
  433. }
  434. if (cmp < 0)
  435. p = &(*p)->rb_left;
  436. else
  437. p = &(*p)->rb_right;
  438. }
  439. rb_link_node(&bss->rbn, parent, p);
  440. rb_insert_color(&bss->rbn, &dev->bss_tree);
  441. }
  442. static struct cfg80211_internal_bss *
  443. rb_find_bss(struct cfg80211_registered_device *dev,
  444. struct cfg80211_internal_bss *res)
  445. {
  446. struct rb_node *n = dev->bss_tree.rb_node;
  447. struct cfg80211_internal_bss *bss;
  448. int r;
  449. while (n) {
  450. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  451. r = cmp_bss(&res->pub, &bss->pub);
  452. if (r == 0)
  453. return bss;
  454. else if (r < 0)
  455. n = n->rb_left;
  456. else
  457. n = n->rb_right;
  458. }
  459. return NULL;
  460. }
  461. static struct cfg80211_internal_bss *
  462. rb_find_hidden_bss(struct cfg80211_registered_device *dev,
  463. struct cfg80211_internal_bss *res)
  464. {
  465. struct rb_node *n = dev->bss_tree.rb_node;
  466. struct cfg80211_internal_bss *bss;
  467. int r;
  468. while (n) {
  469. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  470. r = cmp_hidden_bss(&res->pub, &bss->pub);
  471. if (r == 0)
  472. return bss;
  473. else if (r < 0)
  474. n = n->rb_left;
  475. else
  476. n = n->rb_right;
  477. }
  478. return NULL;
  479. }
  480. static void
  481. copy_hidden_ies(struct cfg80211_internal_bss *res,
  482. struct cfg80211_internal_bss *hidden)
  483. {
  484. if (unlikely(res->pub.beacon_ies))
  485. return;
  486. if (WARN_ON(!hidden->pub.beacon_ies))
  487. return;
  488. res->pub.beacon_ies = kmalloc(hidden->pub.len_beacon_ies, GFP_ATOMIC);
  489. if (unlikely(!res->pub.beacon_ies))
  490. return;
  491. res->beacon_ies_allocated = true;
  492. res->pub.len_beacon_ies = hidden->pub.len_beacon_ies;
  493. memcpy(res->pub.beacon_ies, hidden->pub.beacon_ies,
  494. res->pub.len_beacon_ies);
  495. }
  496. static struct cfg80211_internal_bss *
  497. cfg80211_bss_update(struct cfg80211_registered_device *dev,
  498. struct cfg80211_internal_bss *res)
  499. {
  500. struct cfg80211_internal_bss *found = NULL;
  501. /*
  502. * The reference to "res" is donated to this function.
  503. */
  504. if (WARN_ON(!res->pub.channel)) {
  505. kref_put(&res->ref, bss_release);
  506. return NULL;
  507. }
  508. res->ts = jiffies;
  509. spin_lock_bh(&dev->bss_lock);
  510. found = rb_find_bss(dev, res);
  511. if (found) {
  512. found->pub.beacon_interval = res->pub.beacon_interval;
  513. found->pub.tsf = res->pub.tsf;
  514. found->pub.signal = res->pub.signal;
  515. found->pub.capability = res->pub.capability;
  516. found->ts = res->ts;
  517. /* Update IEs */
  518. if (res->pub.proberesp_ies) {
  519. size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
  520. size_t ielen = res->pub.len_proberesp_ies;
  521. if (found->pub.proberesp_ies &&
  522. !found->proberesp_ies_allocated &&
  523. ksize(found) >= used + ielen) {
  524. memcpy(found->pub.proberesp_ies,
  525. res->pub.proberesp_ies, ielen);
  526. found->pub.len_proberesp_ies = ielen;
  527. } else {
  528. u8 *ies = found->pub.proberesp_ies;
  529. if (found->proberesp_ies_allocated)
  530. ies = krealloc(ies, ielen, GFP_ATOMIC);
  531. else
  532. ies = kmalloc(ielen, GFP_ATOMIC);
  533. if (ies) {
  534. memcpy(ies, res->pub.proberesp_ies,
  535. ielen);
  536. found->proberesp_ies_allocated = true;
  537. found->pub.proberesp_ies = ies;
  538. found->pub.len_proberesp_ies = ielen;
  539. }
  540. }
  541. /* Override possible earlier Beacon frame IEs */
  542. found->pub.information_elements =
  543. found->pub.proberesp_ies;
  544. found->pub.len_information_elements =
  545. found->pub.len_proberesp_ies;
  546. }
  547. if (res->pub.beacon_ies) {
  548. size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
  549. size_t ielen = res->pub.len_beacon_ies;
  550. bool information_elements_is_beacon_ies =
  551. (found->pub.information_elements ==
  552. found->pub.beacon_ies);
  553. if (found->pub.beacon_ies &&
  554. !found->beacon_ies_allocated &&
  555. ksize(found) >= used + ielen) {
  556. memcpy(found->pub.beacon_ies,
  557. res->pub.beacon_ies, ielen);
  558. found->pub.len_beacon_ies = ielen;
  559. } else {
  560. u8 *ies = found->pub.beacon_ies;
  561. if (found->beacon_ies_allocated)
  562. ies = krealloc(ies, ielen, GFP_ATOMIC);
  563. else
  564. ies = kmalloc(ielen, GFP_ATOMIC);
  565. if (ies) {
  566. memcpy(ies, res->pub.beacon_ies,
  567. ielen);
  568. found->beacon_ies_allocated = true;
  569. found->pub.beacon_ies = ies;
  570. found->pub.len_beacon_ies = ielen;
  571. }
  572. }
  573. /* Override IEs if they were from a beacon before */
  574. if (information_elements_is_beacon_ies) {
  575. found->pub.information_elements =
  576. found->pub.beacon_ies;
  577. found->pub.len_information_elements =
  578. found->pub.len_beacon_ies;
  579. }
  580. }
  581. kref_put(&res->ref, bss_release);
  582. } else {
  583. struct cfg80211_internal_bss *hidden;
  584. /* First check if the beacon is a probe response from
  585. * a hidden bss. If so, copy beacon ies (with nullified
  586. * ssid) into the probe response bss entry (with real ssid).
  587. * It is required basically for PSM implementation
  588. * (probe responses do not contain tim ie) */
  589. /* TODO: The code is not trying to update existing probe
  590. * response bss entries when beacon ies are
  591. * getting changed. */
  592. hidden = rb_find_hidden_bss(dev, res);
  593. if (hidden)
  594. copy_hidden_ies(res, hidden);
  595. /* this "consumes" the reference */
  596. list_add_tail(&res->list, &dev->bss_list);
  597. rb_insert_bss(dev, res);
  598. found = res;
  599. }
  600. dev->bss_generation++;
  601. spin_unlock_bh(&dev->bss_lock);
  602. kref_get(&found->ref);
  603. return found;
  604. }
  605. struct cfg80211_bss*
  606. cfg80211_inform_bss(struct wiphy *wiphy,
  607. struct ieee80211_channel *channel,
  608. const u8 *bssid, u64 tsf, u16 capability,
  609. u16 beacon_interval, const u8 *ie, size_t ielen,
  610. s32 signal, gfp_t gfp)
  611. {
  612. struct cfg80211_internal_bss *res;
  613. size_t privsz;
  614. if (WARN_ON(!wiphy))
  615. return NULL;
  616. privsz = wiphy->bss_priv_size;
  617. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  618. (signal < 0 || signal > 100)))
  619. return NULL;
  620. res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
  621. if (!res)
  622. return NULL;
  623. memcpy(res->pub.bssid, bssid, ETH_ALEN);
  624. res->pub.channel = channel;
  625. res->pub.signal = signal;
  626. res->pub.tsf = tsf;
  627. res->pub.beacon_interval = beacon_interval;
  628. res->pub.capability = capability;
  629. /*
  630. * Since we do not know here whether the IEs are from a Beacon or Probe
  631. * Response frame, we need to pick one of the options and only use it
  632. * with the driver that does not provide the full Beacon/Probe Response
  633. * frame. Use Beacon frame pointer to avoid indicating that this should
  634. * override the information_elements pointer should we have received an
  635. * earlier indication of Probe Response data.
  636. *
  637. * The initial buffer for the IEs is allocated with the BSS entry and
  638. * is located after the private area.
  639. */
  640. res->pub.beacon_ies = (u8 *)res + sizeof(*res) + privsz;
  641. memcpy(res->pub.beacon_ies, ie, ielen);
  642. res->pub.len_beacon_ies = ielen;
  643. res->pub.information_elements = res->pub.beacon_ies;
  644. res->pub.len_information_elements = res->pub.len_beacon_ies;
  645. kref_init(&res->ref);
  646. res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
  647. if (!res)
  648. return NULL;
  649. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  650. regulatory_hint_found_beacon(wiphy, channel, gfp);
  651. /* cfg80211_bss_update gives us a referenced result */
  652. return &res->pub;
  653. }
  654. EXPORT_SYMBOL(cfg80211_inform_bss);
  655. struct cfg80211_bss *
  656. cfg80211_inform_bss_frame(struct wiphy *wiphy,
  657. struct ieee80211_channel *channel,
  658. struct ieee80211_mgmt *mgmt, size_t len,
  659. s32 signal, gfp_t gfp)
  660. {
  661. struct cfg80211_internal_bss *res;
  662. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  663. u.probe_resp.variable);
  664. size_t privsz;
  665. if (WARN_ON(!mgmt))
  666. return NULL;
  667. if (WARN_ON(!wiphy))
  668. return NULL;
  669. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  670. (signal < 0 || signal > 100)))
  671. return NULL;
  672. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  673. return NULL;
  674. privsz = wiphy->bss_priv_size;
  675. res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
  676. if (!res)
  677. return NULL;
  678. memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
  679. res->pub.channel = channel;
  680. res->pub.signal = signal;
  681. res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  682. res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  683. res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  684. /*
  685. * The initial buffer for the IEs is allocated with the BSS entry and
  686. * is located after the private area.
  687. */
  688. if (ieee80211_is_probe_resp(mgmt->frame_control)) {
  689. res->pub.proberesp_ies = (u8 *) res + sizeof(*res) + privsz;
  690. memcpy(res->pub.proberesp_ies, mgmt->u.probe_resp.variable,
  691. ielen);
  692. res->pub.len_proberesp_ies = ielen;
  693. res->pub.information_elements = res->pub.proberesp_ies;
  694. res->pub.len_information_elements = res->pub.len_proberesp_ies;
  695. } else {
  696. res->pub.beacon_ies = (u8 *) res + sizeof(*res) + privsz;
  697. memcpy(res->pub.beacon_ies, mgmt->u.beacon.variable, ielen);
  698. res->pub.len_beacon_ies = ielen;
  699. res->pub.information_elements = res->pub.beacon_ies;
  700. res->pub.len_information_elements = res->pub.len_beacon_ies;
  701. }
  702. kref_init(&res->ref);
  703. res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
  704. if (!res)
  705. return NULL;
  706. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  707. regulatory_hint_found_beacon(wiphy, channel, gfp);
  708. /* cfg80211_bss_update gives us a referenced result */
  709. return &res->pub;
  710. }
  711. EXPORT_SYMBOL(cfg80211_inform_bss_frame);
  712. void cfg80211_ref_bss(struct cfg80211_bss *pub)
  713. {
  714. struct cfg80211_internal_bss *bss;
  715. if (!pub)
  716. return;
  717. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  718. kref_get(&bss->ref);
  719. }
  720. EXPORT_SYMBOL(cfg80211_ref_bss);
  721. void cfg80211_put_bss(struct cfg80211_bss *pub)
  722. {
  723. struct cfg80211_internal_bss *bss;
  724. if (!pub)
  725. return;
  726. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  727. kref_put(&bss->ref, bss_release);
  728. }
  729. EXPORT_SYMBOL(cfg80211_put_bss);
  730. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  731. {
  732. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  733. struct cfg80211_internal_bss *bss;
  734. if (WARN_ON(!pub))
  735. return;
  736. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  737. spin_lock_bh(&dev->bss_lock);
  738. if (!list_empty(&bss->list)) {
  739. __cfg80211_unlink_bss(dev, bss);
  740. dev->bss_generation++;
  741. }
  742. spin_unlock_bh(&dev->bss_lock);
  743. }
  744. EXPORT_SYMBOL(cfg80211_unlink_bss);
  745. #ifdef CONFIG_CFG80211_WEXT
  746. int cfg80211_wext_siwscan(struct net_device *dev,
  747. struct iw_request_info *info,
  748. union iwreq_data *wrqu, char *extra)
  749. {
  750. struct cfg80211_registered_device *rdev;
  751. struct wiphy *wiphy;
  752. struct iw_scan_req *wreq = NULL;
  753. struct cfg80211_scan_request *creq = NULL;
  754. int i, err, n_channels = 0;
  755. enum ieee80211_band band;
  756. if (!netif_running(dev))
  757. return -ENETDOWN;
  758. if (wrqu->data.length == sizeof(struct iw_scan_req))
  759. wreq = (struct iw_scan_req *)extra;
  760. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  761. if (IS_ERR(rdev))
  762. return PTR_ERR(rdev);
  763. if (rdev->scan_req) {
  764. err = -EBUSY;
  765. goto out;
  766. }
  767. wiphy = &rdev->wiphy;
  768. /* Determine number of channels, needed to allocate creq */
  769. if (wreq && wreq->num_channels)
  770. n_channels = wreq->num_channels;
  771. else {
  772. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  773. if (wiphy->bands[band])
  774. n_channels += wiphy->bands[band]->n_channels;
  775. }
  776. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  777. n_channels * sizeof(void *),
  778. GFP_ATOMIC);
  779. if (!creq) {
  780. err = -ENOMEM;
  781. goto out;
  782. }
  783. creq->wiphy = wiphy;
  784. creq->dev = dev;
  785. /* SSIDs come after channels */
  786. creq->ssids = (void *)&creq->channels[n_channels];
  787. creq->n_channels = n_channels;
  788. creq->n_ssids = 1;
  789. /* translate "Scan on frequencies" request */
  790. i = 0;
  791. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  792. int j;
  793. if (!wiphy->bands[band])
  794. continue;
  795. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  796. /* ignore disabled channels */
  797. if (wiphy->bands[band]->channels[j].flags &
  798. IEEE80211_CHAN_DISABLED)
  799. continue;
  800. /* If we have a wireless request structure and the
  801. * wireless request specifies frequencies, then search
  802. * for the matching hardware channel.
  803. */
  804. if (wreq && wreq->num_channels) {
  805. int k;
  806. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  807. for (k = 0; k < wreq->num_channels; k++) {
  808. int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
  809. if (wext_freq == wiphy_freq)
  810. goto wext_freq_found;
  811. }
  812. goto wext_freq_not_found;
  813. }
  814. wext_freq_found:
  815. creq->channels[i] = &wiphy->bands[band]->channels[j];
  816. i++;
  817. wext_freq_not_found: ;
  818. }
  819. }
  820. /* No channels found? */
  821. if (!i) {
  822. err = -EINVAL;
  823. goto out;
  824. }
  825. /* Set real number of channels specified in creq->channels[] */
  826. creq->n_channels = i;
  827. /* translate "Scan for SSID" request */
  828. if (wreq) {
  829. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  830. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  831. err = -EINVAL;
  832. goto out;
  833. }
  834. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  835. creq->ssids[0].ssid_len = wreq->essid_len;
  836. }
  837. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  838. creq->n_ssids = 0;
  839. }
  840. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  841. if (wiphy->bands[i])
  842. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  843. rdev->scan_req = creq;
  844. err = rdev->ops->scan(wiphy, dev, creq);
  845. if (err) {
  846. rdev->scan_req = NULL;
  847. /* creq will be freed below */
  848. } else {
  849. nl80211_send_scan_start(rdev, dev);
  850. /* creq now owned by driver */
  851. creq = NULL;
  852. dev_hold(dev);
  853. }
  854. out:
  855. kfree(creq);
  856. cfg80211_unlock_rdev(rdev);
  857. return err;
  858. }
  859. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  860. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  861. struct cfg80211_bss *bss,
  862. char **current_ev, char *end_buf)
  863. {
  864. u8 *pos, *end, *next;
  865. struct iw_event iwe;
  866. if (!bss->information_elements ||
  867. !bss->len_information_elements)
  868. return;
  869. /*
  870. * If needed, fragment the IEs buffer (at IE boundaries) into short
  871. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  872. */
  873. pos = bss->information_elements;
  874. end = pos + bss->len_information_elements;
  875. while (end - pos > IW_GENERIC_IE_MAX) {
  876. next = pos + 2 + pos[1];
  877. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  878. next = next + 2 + next[1];
  879. memset(&iwe, 0, sizeof(iwe));
  880. iwe.cmd = IWEVGENIE;
  881. iwe.u.data.length = next - pos;
  882. *current_ev = iwe_stream_add_point(info, *current_ev,
  883. end_buf, &iwe, pos);
  884. pos = next;
  885. }
  886. if (end > pos) {
  887. memset(&iwe, 0, sizeof(iwe));
  888. iwe.cmd = IWEVGENIE;
  889. iwe.u.data.length = end - pos;
  890. *current_ev = iwe_stream_add_point(info, *current_ev,
  891. end_buf, &iwe, pos);
  892. }
  893. }
  894. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  895. {
  896. unsigned long end = jiffies;
  897. if (end >= start)
  898. return jiffies_to_msecs(end - start);
  899. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  900. }
  901. static char *
  902. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  903. struct cfg80211_internal_bss *bss, char *current_ev,
  904. char *end_buf)
  905. {
  906. struct iw_event iwe;
  907. u8 *buf, *cfg, *p;
  908. u8 *ie = bss->pub.information_elements;
  909. int rem = bss->pub.len_information_elements, i, sig;
  910. bool ismesh = false;
  911. memset(&iwe, 0, sizeof(iwe));
  912. iwe.cmd = SIOCGIWAP;
  913. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  914. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  915. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  916. IW_EV_ADDR_LEN);
  917. memset(&iwe, 0, sizeof(iwe));
  918. iwe.cmd = SIOCGIWFREQ;
  919. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  920. iwe.u.freq.e = 0;
  921. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  922. IW_EV_FREQ_LEN);
  923. memset(&iwe, 0, sizeof(iwe));
  924. iwe.cmd = SIOCGIWFREQ;
  925. iwe.u.freq.m = bss->pub.channel->center_freq;
  926. iwe.u.freq.e = 6;
  927. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  928. IW_EV_FREQ_LEN);
  929. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  930. memset(&iwe, 0, sizeof(iwe));
  931. iwe.cmd = IWEVQUAL;
  932. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  933. IW_QUAL_NOISE_INVALID |
  934. IW_QUAL_QUAL_UPDATED;
  935. switch (wiphy->signal_type) {
  936. case CFG80211_SIGNAL_TYPE_MBM:
  937. sig = bss->pub.signal / 100;
  938. iwe.u.qual.level = sig;
  939. iwe.u.qual.updated |= IW_QUAL_DBM;
  940. if (sig < -110) /* rather bad */
  941. sig = -110;
  942. else if (sig > -40) /* perfect */
  943. sig = -40;
  944. /* will give a range of 0 .. 70 */
  945. iwe.u.qual.qual = sig + 110;
  946. break;
  947. case CFG80211_SIGNAL_TYPE_UNSPEC:
  948. iwe.u.qual.level = bss->pub.signal;
  949. /* will give range 0 .. 100 */
  950. iwe.u.qual.qual = bss->pub.signal;
  951. break;
  952. default:
  953. /* not reached */
  954. break;
  955. }
  956. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  957. &iwe, IW_EV_QUAL_LEN);
  958. }
  959. memset(&iwe, 0, sizeof(iwe));
  960. iwe.cmd = SIOCGIWENCODE;
  961. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  962. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  963. else
  964. iwe.u.data.flags = IW_ENCODE_DISABLED;
  965. iwe.u.data.length = 0;
  966. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  967. &iwe, "");
  968. while (rem >= 2) {
  969. /* invalid data */
  970. if (ie[1] > rem - 2)
  971. break;
  972. switch (ie[0]) {
  973. case WLAN_EID_SSID:
  974. memset(&iwe, 0, sizeof(iwe));
  975. iwe.cmd = SIOCGIWESSID;
  976. iwe.u.data.length = ie[1];
  977. iwe.u.data.flags = 1;
  978. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  979. &iwe, ie + 2);
  980. break;
  981. case WLAN_EID_MESH_ID:
  982. memset(&iwe, 0, sizeof(iwe));
  983. iwe.cmd = SIOCGIWESSID;
  984. iwe.u.data.length = ie[1];
  985. iwe.u.data.flags = 1;
  986. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  987. &iwe, ie + 2);
  988. break;
  989. case WLAN_EID_MESH_CONFIG:
  990. ismesh = true;
  991. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  992. break;
  993. buf = kmalloc(50, GFP_ATOMIC);
  994. if (!buf)
  995. break;
  996. cfg = ie + 2;
  997. memset(&iwe, 0, sizeof(iwe));
  998. iwe.cmd = IWEVCUSTOM;
  999. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  1000. "0x%02X", cfg[0]);
  1001. iwe.u.data.length = strlen(buf);
  1002. current_ev = iwe_stream_add_point(info, current_ev,
  1003. end_buf,
  1004. &iwe, buf);
  1005. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  1006. cfg[1]);
  1007. iwe.u.data.length = strlen(buf);
  1008. current_ev = iwe_stream_add_point(info, current_ev,
  1009. end_buf,
  1010. &iwe, buf);
  1011. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  1012. cfg[2]);
  1013. iwe.u.data.length = strlen(buf);
  1014. current_ev = iwe_stream_add_point(info, current_ev,
  1015. end_buf,
  1016. &iwe, buf);
  1017. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  1018. iwe.u.data.length = strlen(buf);
  1019. current_ev = iwe_stream_add_point(info, current_ev,
  1020. end_buf,
  1021. &iwe, buf);
  1022. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  1023. iwe.u.data.length = strlen(buf);
  1024. current_ev = iwe_stream_add_point(info, current_ev,
  1025. end_buf,
  1026. &iwe, buf);
  1027. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  1028. iwe.u.data.length = strlen(buf);
  1029. current_ev = iwe_stream_add_point(info, current_ev,
  1030. end_buf,
  1031. &iwe, buf);
  1032. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1033. iwe.u.data.length = strlen(buf);
  1034. current_ev = iwe_stream_add_point(info, current_ev,
  1035. end_buf,
  1036. &iwe, buf);
  1037. kfree(buf);
  1038. break;
  1039. case WLAN_EID_SUPP_RATES:
  1040. case WLAN_EID_EXT_SUPP_RATES:
  1041. /* display all supported rates in readable format */
  1042. p = current_ev + iwe_stream_lcp_len(info);
  1043. memset(&iwe, 0, sizeof(iwe));
  1044. iwe.cmd = SIOCGIWRATE;
  1045. /* Those two flags are ignored... */
  1046. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1047. for (i = 0; i < ie[1]; i++) {
  1048. iwe.u.bitrate.value =
  1049. ((ie[i + 2] & 0x7f) * 500000);
  1050. p = iwe_stream_add_value(info, current_ev, p,
  1051. end_buf, &iwe, IW_EV_PARAM_LEN);
  1052. }
  1053. current_ev = p;
  1054. break;
  1055. }
  1056. rem -= ie[1] + 2;
  1057. ie += ie[1] + 2;
  1058. }
  1059. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1060. ismesh) {
  1061. memset(&iwe, 0, sizeof(iwe));
  1062. iwe.cmd = SIOCGIWMODE;
  1063. if (ismesh)
  1064. iwe.u.mode = IW_MODE_MESH;
  1065. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1066. iwe.u.mode = IW_MODE_MASTER;
  1067. else
  1068. iwe.u.mode = IW_MODE_ADHOC;
  1069. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  1070. &iwe, IW_EV_UINT_LEN);
  1071. }
  1072. buf = kmalloc(30, GFP_ATOMIC);
  1073. if (buf) {
  1074. memset(&iwe, 0, sizeof(iwe));
  1075. iwe.cmd = IWEVCUSTOM;
  1076. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  1077. iwe.u.data.length = strlen(buf);
  1078. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1079. &iwe, buf);
  1080. memset(&iwe, 0, sizeof(iwe));
  1081. iwe.cmd = IWEVCUSTOM;
  1082. sprintf(buf, " Last beacon: %ums ago",
  1083. elapsed_jiffies_msecs(bss->ts));
  1084. iwe.u.data.length = strlen(buf);
  1085. current_ev = iwe_stream_add_point(info, current_ev,
  1086. end_buf, &iwe, buf);
  1087. kfree(buf);
  1088. }
  1089. ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
  1090. return current_ev;
  1091. }
  1092. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  1093. struct iw_request_info *info,
  1094. char *buf, size_t len)
  1095. {
  1096. char *current_ev = buf;
  1097. char *end_buf = buf + len;
  1098. struct cfg80211_internal_bss *bss;
  1099. spin_lock_bh(&dev->bss_lock);
  1100. cfg80211_bss_expire(dev);
  1101. list_for_each_entry(bss, &dev->bss_list, list) {
  1102. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1103. spin_unlock_bh(&dev->bss_lock);
  1104. return -E2BIG;
  1105. }
  1106. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  1107. current_ev, end_buf);
  1108. }
  1109. spin_unlock_bh(&dev->bss_lock);
  1110. return current_ev - buf;
  1111. }
  1112. int cfg80211_wext_giwscan(struct net_device *dev,
  1113. struct iw_request_info *info,
  1114. struct iw_point *data, char *extra)
  1115. {
  1116. struct cfg80211_registered_device *rdev;
  1117. int res;
  1118. if (!netif_running(dev))
  1119. return -ENETDOWN;
  1120. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1121. if (IS_ERR(rdev))
  1122. return PTR_ERR(rdev);
  1123. if (rdev->scan_req) {
  1124. res = -EAGAIN;
  1125. goto out;
  1126. }
  1127. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1128. data->length = 0;
  1129. if (res >= 0) {
  1130. data->length = res;
  1131. res = 0;
  1132. }
  1133. out:
  1134. cfg80211_unlock_rdev(rdev);
  1135. return res;
  1136. }
  1137. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  1138. #endif