scan.c 36 KB

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