scan.c 23 KB

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