scan.c 30 KB

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