scan.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200
  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. mutex_lock(&rdev->sched_scan_mtx);
  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. mutex_unlock(&rdev->sched_scan_mtx);
  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. mutex_lock(&rdev->sched_scan_mtx);
  101. __cfg80211_stop_sched_scan(rdev, true);
  102. mutex_unlock(&rdev->sched_scan_mtx);
  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. lockdep_assert_held(&rdev->sched_scan_mtx);
  110. if (!rdev->sched_scan_req)
  111. return -ENOENT;
  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. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  713. if (wiphy->bands[i])
  714. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  715. rdev->scan_req = creq;
  716. err = rdev->ops->scan(wiphy, dev, creq);
  717. if (err) {
  718. rdev->scan_req = NULL;
  719. /* creq will be freed below */
  720. } else {
  721. nl80211_send_scan_start(rdev, dev);
  722. /* creq now owned by driver */
  723. creq = NULL;
  724. dev_hold(dev);
  725. }
  726. out:
  727. kfree(creq);
  728. cfg80211_unlock_rdev(rdev);
  729. return err;
  730. }
  731. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  732. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  733. struct cfg80211_bss *bss,
  734. char **current_ev, char *end_buf)
  735. {
  736. u8 *pos, *end, *next;
  737. struct iw_event iwe;
  738. if (!bss->information_elements ||
  739. !bss->len_information_elements)
  740. return;
  741. /*
  742. * If needed, fragment the IEs buffer (at IE boundaries) into short
  743. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  744. */
  745. pos = bss->information_elements;
  746. end = pos + bss->len_information_elements;
  747. while (end - pos > IW_GENERIC_IE_MAX) {
  748. next = pos + 2 + pos[1];
  749. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  750. next = next + 2 + next[1];
  751. memset(&iwe, 0, sizeof(iwe));
  752. iwe.cmd = IWEVGENIE;
  753. iwe.u.data.length = next - pos;
  754. *current_ev = iwe_stream_add_point(info, *current_ev,
  755. end_buf, &iwe, pos);
  756. pos = next;
  757. }
  758. if (end > pos) {
  759. memset(&iwe, 0, sizeof(iwe));
  760. iwe.cmd = IWEVGENIE;
  761. iwe.u.data.length = end - pos;
  762. *current_ev = iwe_stream_add_point(info, *current_ev,
  763. end_buf, &iwe, pos);
  764. }
  765. }
  766. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  767. {
  768. unsigned long end = jiffies;
  769. if (end >= start)
  770. return jiffies_to_msecs(end - start);
  771. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  772. }
  773. static char *
  774. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  775. struct cfg80211_internal_bss *bss, char *current_ev,
  776. char *end_buf)
  777. {
  778. struct iw_event iwe;
  779. u8 *buf, *cfg, *p;
  780. u8 *ie = bss->pub.information_elements;
  781. int rem = bss->pub.len_information_elements, i, sig;
  782. bool ismesh = false;
  783. memset(&iwe, 0, sizeof(iwe));
  784. iwe.cmd = SIOCGIWAP;
  785. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  786. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  787. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  788. IW_EV_ADDR_LEN);
  789. memset(&iwe, 0, sizeof(iwe));
  790. iwe.cmd = SIOCGIWFREQ;
  791. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  792. iwe.u.freq.e = 0;
  793. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  794. IW_EV_FREQ_LEN);
  795. memset(&iwe, 0, sizeof(iwe));
  796. iwe.cmd = SIOCGIWFREQ;
  797. iwe.u.freq.m = bss->pub.channel->center_freq;
  798. iwe.u.freq.e = 6;
  799. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  800. IW_EV_FREQ_LEN);
  801. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  802. memset(&iwe, 0, sizeof(iwe));
  803. iwe.cmd = IWEVQUAL;
  804. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  805. IW_QUAL_NOISE_INVALID |
  806. IW_QUAL_QUAL_UPDATED;
  807. switch (wiphy->signal_type) {
  808. case CFG80211_SIGNAL_TYPE_MBM:
  809. sig = bss->pub.signal / 100;
  810. iwe.u.qual.level = sig;
  811. iwe.u.qual.updated |= IW_QUAL_DBM;
  812. if (sig < -110) /* rather bad */
  813. sig = -110;
  814. else if (sig > -40) /* perfect */
  815. sig = -40;
  816. /* will give a range of 0 .. 70 */
  817. iwe.u.qual.qual = sig + 110;
  818. break;
  819. case CFG80211_SIGNAL_TYPE_UNSPEC:
  820. iwe.u.qual.level = bss->pub.signal;
  821. /* will give range 0 .. 100 */
  822. iwe.u.qual.qual = bss->pub.signal;
  823. break;
  824. default:
  825. /* not reached */
  826. break;
  827. }
  828. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  829. &iwe, IW_EV_QUAL_LEN);
  830. }
  831. memset(&iwe, 0, sizeof(iwe));
  832. iwe.cmd = SIOCGIWENCODE;
  833. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  834. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  835. else
  836. iwe.u.data.flags = IW_ENCODE_DISABLED;
  837. iwe.u.data.length = 0;
  838. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  839. &iwe, "");
  840. while (rem >= 2) {
  841. /* invalid data */
  842. if (ie[1] > rem - 2)
  843. break;
  844. switch (ie[0]) {
  845. case WLAN_EID_SSID:
  846. memset(&iwe, 0, sizeof(iwe));
  847. iwe.cmd = SIOCGIWESSID;
  848. iwe.u.data.length = ie[1];
  849. iwe.u.data.flags = 1;
  850. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  851. &iwe, ie + 2);
  852. break;
  853. case WLAN_EID_MESH_ID:
  854. memset(&iwe, 0, sizeof(iwe));
  855. iwe.cmd = SIOCGIWESSID;
  856. iwe.u.data.length = ie[1];
  857. iwe.u.data.flags = 1;
  858. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  859. &iwe, ie + 2);
  860. break;
  861. case WLAN_EID_MESH_CONFIG:
  862. ismesh = true;
  863. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  864. break;
  865. buf = kmalloc(50, GFP_ATOMIC);
  866. if (!buf)
  867. break;
  868. cfg = ie + 2;
  869. memset(&iwe, 0, sizeof(iwe));
  870. iwe.cmd = IWEVCUSTOM;
  871. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  872. "0x%02X", cfg[0]);
  873. iwe.u.data.length = strlen(buf);
  874. current_ev = iwe_stream_add_point(info, current_ev,
  875. end_buf,
  876. &iwe, buf);
  877. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  878. cfg[1]);
  879. iwe.u.data.length = strlen(buf);
  880. current_ev = iwe_stream_add_point(info, current_ev,
  881. end_buf,
  882. &iwe, buf);
  883. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  884. cfg[2]);
  885. iwe.u.data.length = strlen(buf);
  886. current_ev = iwe_stream_add_point(info, current_ev,
  887. end_buf,
  888. &iwe, buf);
  889. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  890. iwe.u.data.length = strlen(buf);
  891. current_ev = iwe_stream_add_point(info, current_ev,
  892. end_buf,
  893. &iwe, buf);
  894. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  895. iwe.u.data.length = strlen(buf);
  896. current_ev = iwe_stream_add_point(info, current_ev,
  897. end_buf,
  898. &iwe, buf);
  899. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  900. iwe.u.data.length = strlen(buf);
  901. current_ev = iwe_stream_add_point(info, current_ev,
  902. end_buf,
  903. &iwe, buf);
  904. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  905. iwe.u.data.length = strlen(buf);
  906. current_ev = iwe_stream_add_point(info, current_ev,
  907. end_buf,
  908. &iwe, buf);
  909. kfree(buf);
  910. break;
  911. case WLAN_EID_SUPP_RATES:
  912. case WLAN_EID_EXT_SUPP_RATES:
  913. /* display all supported rates in readable format */
  914. p = current_ev + iwe_stream_lcp_len(info);
  915. memset(&iwe, 0, sizeof(iwe));
  916. iwe.cmd = SIOCGIWRATE;
  917. /* Those two flags are ignored... */
  918. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  919. for (i = 0; i < ie[1]; i++) {
  920. iwe.u.bitrate.value =
  921. ((ie[i + 2] & 0x7f) * 500000);
  922. p = iwe_stream_add_value(info, current_ev, p,
  923. end_buf, &iwe, IW_EV_PARAM_LEN);
  924. }
  925. current_ev = p;
  926. break;
  927. }
  928. rem -= ie[1] + 2;
  929. ie += ie[1] + 2;
  930. }
  931. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  932. ismesh) {
  933. memset(&iwe, 0, sizeof(iwe));
  934. iwe.cmd = SIOCGIWMODE;
  935. if (ismesh)
  936. iwe.u.mode = IW_MODE_MESH;
  937. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  938. iwe.u.mode = IW_MODE_MASTER;
  939. else
  940. iwe.u.mode = IW_MODE_ADHOC;
  941. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  942. &iwe, IW_EV_UINT_LEN);
  943. }
  944. buf = kmalloc(30, GFP_ATOMIC);
  945. if (buf) {
  946. memset(&iwe, 0, sizeof(iwe));
  947. iwe.cmd = IWEVCUSTOM;
  948. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  949. iwe.u.data.length = strlen(buf);
  950. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  951. &iwe, buf);
  952. memset(&iwe, 0, sizeof(iwe));
  953. iwe.cmd = IWEVCUSTOM;
  954. sprintf(buf, " Last beacon: %ums ago",
  955. elapsed_jiffies_msecs(bss->ts));
  956. iwe.u.data.length = strlen(buf);
  957. current_ev = iwe_stream_add_point(info, current_ev,
  958. end_buf, &iwe, buf);
  959. kfree(buf);
  960. }
  961. ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
  962. return current_ev;
  963. }
  964. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  965. struct iw_request_info *info,
  966. char *buf, size_t len)
  967. {
  968. char *current_ev = buf;
  969. char *end_buf = buf + len;
  970. struct cfg80211_internal_bss *bss;
  971. spin_lock_bh(&dev->bss_lock);
  972. cfg80211_bss_expire(dev);
  973. list_for_each_entry(bss, &dev->bss_list, list) {
  974. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  975. spin_unlock_bh(&dev->bss_lock);
  976. return -E2BIG;
  977. }
  978. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  979. current_ev, end_buf);
  980. }
  981. spin_unlock_bh(&dev->bss_lock);
  982. return current_ev - buf;
  983. }
  984. int cfg80211_wext_giwscan(struct net_device *dev,
  985. struct iw_request_info *info,
  986. struct iw_point *data, char *extra)
  987. {
  988. struct cfg80211_registered_device *rdev;
  989. int res;
  990. if (!netif_running(dev))
  991. return -ENETDOWN;
  992. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  993. if (IS_ERR(rdev))
  994. return PTR_ERR(rdev);
  995. if (rdev->scan_req) {
  996. res = -EAGAIN;
  997. goto out;
  998. }
  999. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1000. data->length = 0;
  1001. if (res >= 0) {
  1002. data->length = res;
  1003. res = 0;
  1004. }
  1005. out:
  1006. cfg80211_unlock_rdev(rdev);
  1007. return res;
  1008. }
  1009. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  1010. #endif