scan.c 30 KB

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