scan.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  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. struct cfg80211_bss*
  637. cfg80211_inform_bss(struct wiphy *wiphy,
  638. struct ieee80211_channel *channel,
  639. const u8 *bssid, u64 tsf, u16 capability,
  640. u16 beacon_interval, const u8 *ie, size_t ielen,
  641. s32 signal, gfp_t gfp)
  642. {
  643. struct cfg80211_internal_bss *res;
  644. size_t privsz;
  645. if (WARN_ON(!wiphy))
  646. return NULL;
  647. privsz = wiphy->bss_priv_size;
  648. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  649. (signal < 0 || signal > 100)))
  650. return NULL;
  651. res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
  652. if (!res)
  653. return NULL;
  654. memcpy(res->pub.bssid, bssid, ETH_ALEN);
  655. res->pub.channel = channel;
  656. res->pub.signal = signal;
  657. res->pub.tsf = tsf;
  658. res->pub.beacon_interval = beacon_interval;
  659. res->pub.capability = capability;
  660. /*
  661. * Since we do not know here whether the IEs are from a Beacon or Probe
  662. * Response frame, we need to pick one of the options and only use it
  663. * with the driver that does not provide the full Beacon/Probe Response
  664. * frame. Use Beacon frame pointer to avoid indicating that this should
  665. * override the information_elements pointer should we have received an
  666. * earlier indication of Probe Response data.
  667. *
  668. * The initial buffer for the IEs is allocated with the BSS entry and
  669. * is located after the private area.
  670. */
  671. res->pub.beacon_ies = (u8 *)res + sizeof(*res) + privsz;
  672. memcpy(res->pub.beacon_ies, ie, ielen);
  673. res->pub.len_beacon_ies = ielen;
  674. res->pub.information_elements = res->pub.beacon_ies;
  675. res->pub.len_information_elements = res->pub.len_beacon_ies;
  676. kref_init(&res->ref);
  677. res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
  678. if (!res)
  679. return NULL;
  680. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  681. regulatory_hint_found_beacon(wiphy, channel, gfp);
  682. trace_cfg80211_return_bss(&res->pub);
  683. /* cfg80211_bss_update gives us a referenced result */
  684. return &res->pub;
  685. }
  686. EXPORT_SYMBOL(cfg80211_inform_bss);
  687. struct cfg80211_bss *
  688. cfg80211_inform_bss_frame(struct wiphy *wiphy,
  689. struct ieee80211_channel *channel,
  690. struct ieee80211_mgmt *mgmt, size_t len,
  691. s32 signal, gfp_t gfp)
  692. {
  693. struct cfg80211_internal_bss *res;
  694. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  695. u.probe_resp.variable);
  696. size_t privsz;
  697. trace_cfg80211_inform_bss_frame(wiphy, channel, mgmt, len, signal);
  698. if (WARN_ON(!mgmt))
  699. return NULL;
  700. if (WARN_ON(!wiphy))
  701. return NULL;
  702. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  703. (signal < 0 || signal > 100)))
  704. return NULL;
  705. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  706. return NULL;
  707. privsz = wiphy->bss_priv_size;
  708. res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
  709. if (!res)
  710. return NULL;
  711. memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
  712. res->pub.channel = channel;
  713. res->pub.signal = signal;
  714. res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  715. res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  716. res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  717. /*
  718. * The initial buffer for the IEs is allocated with the BSS entry and
  719. * is located after the private area.
  720. */
  721. if (ieee80211_is_probe_resp(mgmt->frame_control)) {
  722. res->pub.proberesp_ies = (u8 *) res + sizeof(*res) + privsz;
  723. memcpy(res->pub.proberesp_ies, mgmt->u.probe_resp.variable,
  724. ielen);
  725. res->pub.len_proberesp_ies = ielen;
  726. res->pub.information_elements = res->pub.proberesp_ies;
  727. res->pub.len_information_elements = res->pub.len_proberesp_ies;
  728. } else {
  729. res->pub.beacon_ies = (u8 *) res + sizeof(*res) + privsz;
  730. memcpy(res->pub.beacon_ies, mgmt->u.beacon.variable, ielen);
  731. res->pub.len_beacon_ies = ielen;
  732. res->pub.information_elements = res->pub.beacon_ies;
  733. res->pub.len_information_elements = res->pub.len_beacon_ies;
  734. }
  735. kref_init(&res->ref);
  736. res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
  737. if (!res)
  738. return NULL;
  739. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  740. regulatory_hint_found_beacon(wiphy, channel, gfp);
  741. trace_cfg80211_return_bss(&res->pub);
  742. /* cfg80211_bss_update gives us a referenced result */
  743. return &res->pub;
  744. }
  745. EXPORT_SYMBOL(cfg80211_inform_bss_frame);
  746. void cfg80211_ref_bss(struct cfg80211_bss *pub)
  747. {
  748. struct cfg80211_internal_bss *bss;
  749. if (!pub)
  750. return;
  751. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  752. kref_get(&bss->ref);
  753. }
  754. EXPORT_SYMBOL(cfg80211_ref_bss);
  755. void cfg80211_put_bss(struct cfg80211_bss *pub)
  756. {
  757. struct cfg80211_internal_bss *bss;
  758. if (!pub)
  759. return;
  760. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  761. kref_put(&bss->ref, bss_release);
  762. }
  763. EXPORT_SYMBOL(cfg80211_put_bss);
  764. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  765. {
  766. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  767. struct cfg80211_internal_bss *bss;
  768. if (WARN_ON(!pub))
  769. return;
  770. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  771. spin_lock_bh(&dev->bss_lock);
  772. if (!list_empty(&bss->list)) {
  773. __cfg80211_unlink_bss(dev, bss);
  774. dev->bss_generation++;
  775. }
  776. spin_unlock_bh(&dev->bss_lock);
  777. }
  778. EXPORT_SYMBOL(cfg80211_unlink_bss);
  779. #ifdef CONFIG_CFG80211_WEXT
  780. int cfg80211_wext_siwscan(struct net_device *dev,
  781. struct iw_request_info *info,
  782. union iwreq_data *wrqu, char *extra)
  783. {
  784. struct cfg80211_registered_device *rdev;
  785. struct wiphy *wiphy;
  786. struct iw_scan_req *wreq = NULL;
  787. struct cfg80211_scan_request *creq = NULL;
  788. int i, err, n_channels = 0;
  789. enum ieee80211_band band;
  790. if (!netif_running(dev))
  791. return -ENETDOWN;
  792. if (wrqu->data.length == sizeof(struct iw_scan_req))
  793. wreq = (struct iw_scan_req *)extra;
  794. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  795. if (IS_ERR(rdev))
  796. return PTR_ERR(rdev);
  797. if (rdev->scan_req) {
  798. err = -EBUSY;
  799. goto out;
  800. }
  801. wiphy = &rdev->wiphy;
  802. /* Determine number of channels, needed to allocate creq */
  803. if (wreq && wreq->num_channels)
  804. n_channels = wreq->num_channels;
  805. else {
  806. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  807. if (wiphy->bands[band])
  808. n_channels += wiphy->bands[band]->n_channels;
  809. }
  810. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  811. n_channels * sizeof(void *),
  812. GFP_ATOMIC);
  813. if (!creq) {
  814. err = -ENOMEM;
  815. goto out;
  816. }
  817. creq->wiphy = wiphy;
  818. creq->wdev = dev->ieee80211_ptr;
  819. /* SSIDs come after channels */
  820. creq->ssids = (void *)&creq->channels[n_channels];
  821. creq->n_channels = n_channels;
  822. creq->n_ssids = 1;
  823. creq->scan_start = jiffies;
  824. /* translate "Scan on frequencies" request */
  825. i = 0;
  826. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  827. int j;
  828. if (!wiphy->bands[band])
  829. continue;
  830. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  831. /* ignore disabled channels */
  832. if (wiphy->bands[band]->channels[j].flags &
  833. IEEE80211_CHAN_DISABLED)
  834. continue;
  835. /* If we have a wireless request structure and the
  836. * wireless request specifies frequencies, then search
  837. * for the matching hardware channel.
  838. */
  839. if (wreq && wreq->num_channels) {
  840. int k;
  841. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  842. for (k = 0; k < wreq->num_channels; k++) {
  843. int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
  844. if (wext_freq == wiphy_freq)
  845. goto wext_freq_found;
  846. }
  847. goto wext_freq_not_found;
  848. }
  849. wext_freq_found:
  850. creq->channels[i] = &wiphy->bands[band]->channels[j];
  851. i++;
  852. wext_freq_not_found: ;
  853. }
  854. }
  855. /* No channels found? */
  856. if (!i) {
  857. err = -EINVAL;
  858. goto out;
  859. }
  860. /* Set real number of channels specified in creq->channels[] */
  861. creq->n_channels = i;
  862. /* translate "Scan for SSID" request */
  863. if (wreq) {
  864. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  865. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  866. err = -EINVAL;
  867. goto out;
  868. }
  869. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  870. creq->ssids[0].ssid_len = wreq->essid_len;
  871. }
  872. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  873. creq->n_ssids = 0;
  874. }
  875. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  876. if (wiphy->bands[i])
  877. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  878. rdev->scan_req = creq;
  879. err = rdev_scan(rdev, creq);
  880. if (err) {
  881. rdev->scan_req = NULL;
  882. /* creq will be freed below */
  883. } else {
  884. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  885. /* creq now owned by driver */
  886. creq = NULL;
  887. dev_hold(dev);
  888. }
  889. out:
  890. kfree(creq);
  891. cfg80211_unlock_rdev(rdev);
  892. return err;
  893. }
  894. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  895. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  896. struct cfg80211_bss *bss,
  897. char **current_ev, char *end_buf)
  898. {
  899. u8 *pos, *end, *next;
  900. struct iw_event iwe;
  901. if (!bss->information_elements ||
  902. !bss->len_information_elements)
  903. return;
  904. /*
  905. * If needed, fragment the IEs buffer (at IE boundaries) into short
  906. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  907. */
  908. pos = bss->information_elements;
  909. end = pos + bss->len_information_elements;
  910. while (end - pos > IW_GENERIC_IE_MAX) {
  911. next = pos + 2 + pos[1];
  912. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  913. next = next + 2 + next[1];
  914. memset(&iwe, 0, sizeof(iwe));
  915. iwe.cmd = IWEVGENIE;
  916. iwe.u.data.length = next - pos;
  917. *current_ev = iwe_stream_add_point(info, *current_ev,
  918. end_buf, &iwe, pos);
  919. pos = next;
  920. }
  921. if (end > pos) {
  922. memset(&iwe, 0, sizeof(iwe));
  923. iwe.cmd = IWEVGENIE;
  924. iwe.u.data.length = end - pos;
  925. *current_ev = iwe_stream_add_point(info, *current_ev,
  926. end_buf, &iwe, pos);
  927. }
  928. }
  929. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  930. {
  931. unsigned long end = jiffies;
  932. if (end >= start)
  933. return jiffies_to_msecs(end - start);
  934. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  935. }
  936. static char *
  937. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  938. struct cfg80211_internal_bss *bss, char *current_ev,
  939. char *end_buf)
  940. {
  941. struct iw_event iwe;
  942. u8 *buf, *cfg, *p;
  943. u8 *ie = bss->pub.information_elements;
  944. int rem = bss->pub.len_information_elements, i, sig;
  945. bool ismesh = false;
  946. memset(&iwe, 0, sizeof(iwe));
  947. iwe.cmd = SIOCGIWAP;
  948. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  949. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  950. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  951. IW_EV_ADDR_LEN);
  952. memset(&iwe, 0, sizeof(iwe));
  953. iwe.cmd = SIOCGIWFREQ;
  954. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  955. iwe.u.freq.e = 0;
  956. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  957. IW_EV_FREQ_LEN);
  958. memset(&iwe, 0, sizeof(iwe));
  959. iwe.cmd = SIOCGIWFREQ;
  960. iwe.u.freq.m = bss->pub.channel->center_freq;
  961. iwe.u.freq.e = 6;
  962. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  963. IW_EV_FREQ_LEN);
  964. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  965. memset(&iwe, 0, sizeof(iwe));
  966. iwe.cmd = IWEVQUAL;
  967. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  968. IW_QUAL_NOISE_INVALID |
  969. IW_QUAL_QUAL_UPDATED;
  970. switch (wiphy->signal_type) {
  971. case CFG80211_SIGNAL_TYPE_MBM:
  972. sig = bss->pub.signal / 100;
  973. iwe.u.qual.level = sig;
  974. iwe.u.qual.updated |= IW_QUAL_DBM;
  975. if (sig < -110) /* rather bad */
  976. sig = -110;
  977. else if (sig > -40) /* perfect */
  978. sig = -40;
  979. /* will give a range of 0 .. 70 */
  980. iwe.u.qual.qual = sig + 110;
  981. break;
  982. case CFG80211_SIGNAL_TYPE_UNSPEC:
  983. iwe.u.qual.level = bss->pub.signal;
  984. /* will give range 0 .. 100 */
  985. iwe.u.qual.qual = bss->pub.signal;
  986. break;
  987. default:
  988. /* not reached */
  989. break;
  990. }
  991. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  992. &iwe, IW_EV_QUAL_LEN);
  993. }
  994. memset(&iwe, 0, sizeof(iwe));
  995. iwe.cmd = SIOCGIWENCODE;
  996. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  997. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  998. else
  999. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1000. iwe.u.data.length = 0;
  1001. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1002. &iwe, "");
  1003. while (rem >= 2) {
  1004. /* invalid data */
  1005. if (ie[1] > rem - 2)
  1006. break;
  1007. switch (ie[0]) {
  1008. case WLAN_EID_SSID:
  1009. memset(&iwe, 0, sizeof(iwe));
  1010. iwe.cmd = SIOCGIWESSID;
  1011. iwe.u.data.length = ie[1];
  1012. iwe.u.data.flags = 1;
  1013. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1014. &iwe, ie + 2);
  1015. break;
  1016. case WLAN_EID_MESH_ID:
  1017. memset(&iwe, 0, sizeof(iwe));
  1018. iwe.cmd = SIOCGIWESSID;
  1019. iwe.u.data.length = ie[1];
  1020. iwe.u.data.flags = 1;
  1021. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1022. &iwe, ie + 2);
  1023. break;
  1024. case WLAN_EID_MESH_CONFIG:
  1025. ismesh = true;
  1026. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  1027. break;
  1028. buf = kmalloc(50, GFP_ATOMIC);
  1029. if (!buf)
  1030. break;
  1031. cfg = ie + 2;
  1032. memset(&iwe, 0, sizeof(iwe));
  1033. iwe.cmd = IWEVCUSTOM;
  1034. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  1035. "0x%02X", cfg[0]);
  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, "Path Selection Metric ID: 0x%02X",
  1041. cfg[1]);
  1042. iwe.u.data.length = strlen(buf);
  1043. current_ev = iwe_stream_add_point(info, current_ev,
  1044. end_buf,
  1045. &iwe, buf);
  1046. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  1047. cfg[2]);
  1048. iwe.u.data.length = strlen(buf);
  1049. current_ev = iwe_stream_add_point(info, current_ev,
  1050. end_buf,
  1051. &iwe, buf);
  1052. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  1053. iwe.u.data.length = strlen(buf);
  1054. current_ev = iwe_stream_add_point(info, current_ev,
  1055. end_buf,
  1056. &iwe, buf);
  1057. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  1058. iwe.u.data.length = strlen(buf);
  1059. current_ev = iwe_stream_add_point(info, current_ev,
  1060. end_buf,
  1061. &iwe, buf);
  1062. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  1063. iwe.u.data.length = strlen(buf);
  1064. current_ev = iwe_stream_add_point(info, current_ev,
  1065. end_buf,
  1066. &iwe, buf);
  1067. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1068. iwe.u.data.length = strlen(buf);
  1069. current_ev = iwe_stream_add_point(info, current_ev,
  1070. end_buf,
  1071. &iwe, buf);
  1072. kfree(buf);
  1073. break;
  1074. case WLAN_EID_SUPP_RATES:
  1075. case WLAN_EID_EXT_SUPP_RATES:
  1076. /* display all supported rates in readable format */
  1077. p = current_ev + iwe_stream_lcp_len(info);
  1078. memset(&iwe, 0, sizeof(iwe));
  1079. iwe.cmd = SIOCGIWRATE;
  1080. /* Those two flags are ignored... */
  1081. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1082. for (i = 0; i < ie[1]; i++) {
  1083. iwe.u.bitrate.value =
  1084. ((ie[i + 2] & 0x7f) * 500000);
  1085. p = iwe_stream_add_value(info, current_ev, p,
  1086. end_buf, &iwe, IW_EV_PARAM_LEN);
  1087. }
  1088. current_ev = p;
  1089. break;
  1090. }
  1091. rem -= ie[1] + 2;
  1092. ie += ie[1] + 2;
  1093. }
  1094. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1095. ismesh) {
  1096. memset(&iwe, 0, sizeof(iwe));
  1097. iwe.cmd = SIOCGIWMODE;
  1098. if (ismesh)
  1099. iwe.u.mode = IW_MODE_MESH;
  1100. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1101. iwe.u.mode = IW_MODE_MASTER;
  1102. else
  1103. iwe.u.mode = IW_MODE_ADHOC;
  1104. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  1105. &iwe, IW_EV_UINT_LEN);
  1106. }
  1107. buf = kmalloc(30, GFP_ATOMIC);
  1108. if (buf) {
  1109. memset(&iwe, 0, sizeof(iwe));
  1110. iwe.cmd = IWEVCUSTOM;
  1111. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  1112. iwe.u.data.length = strlen(buf);
  1113. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1114. &iwe, buf);
  1115. memset(&iwe, 0, sizeof(iwe));
  1116. iwe.cmd = IWEVCUSTOM;
  1117. sprintf(buf, " Last beacon: %ums ago",
  1118. elapsed_jiffies_msecs(bss->ts));
  1119. iwe.u.data.length = strlen(buf);
  1120. current_ev = iwe_stream_add_point(info, current_ev,
  1121. end_buf, &iwe, buf);
  1122. kfree(buf);
  1123. }
  1124. ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
  1125. return current_ev;
  1126. }
  1127. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  1128. struct iw_request_info *info,
  1129. char *buf, size_t len)
  1130. {
  1131. char *current_ev = buf;
  1132. char *end_buf = buf + len;
  1133. struct cfg80211_internal_bss *bss;
  1134. spin_lock_bh(&dev->bss_lock);
  1135. cfg80211_bss_expire(dev);
  1136. list_for_each_entry(bss, &dev->bss_list, list) {
  1137. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1138. spin_unlock_bh(&dev->bss_lock);
  1139. return -E2BIG;
  1140. }
  1141. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  1142. current_ev, end_buf);
  1143. }
  1144. spin_unlock_bh(&dev->bss_lock);
  1145. return current_ev - buf;
  1146. }
  1147. int cfg80211_wext_giwscan(struct net_device *dev,
  1148. struct iw_request_info *info,
  1149. struct iw_point *data, char *extra)
  1150. {
  1151. struct cfg80211_registered_device *rdev;
  1152. int res;
  1153. if (!netif_running(dev))
  1154. return -ENETDOWN;
  1155. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1156. if (IS_ERR(rdev))
  1157. return PTR_ERR(rdev);
  1158. if (rdev->scan_req) {
  1159. res = -EAGAIN;
  1160. goto out;
  1161. }
  1162. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1163. data->length = 0;
  1164. if (res >= 0) {
  1165. data->length = res;
  1166. res = 0;
  1167. }
  1168. out:
  1169. cfg80211_unlock_rdev(rdev);
  1170. return res;
  1171. }
  1172. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  1173. #endif