scan.c 24 KB

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