scan.c 30 KB

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