scan.c 20 KB

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