scan.c 28 KB

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