scan.c 28 KB

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