scan.c 28 KB

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