scan.c 24 KB

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