scan.c 30 KB

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