scan.c 27 KB

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