scan.c 20 KB

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