scan.c 21 KB

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