scan.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  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.capability = res->pub.capability;
  313. found->ts = res->ts;
  314. kref_put(&res->ref, bss_release);
  315. } else {
  316. /* this "consumes" the reference */
  317. list_add_tail(&res->list, &dev->bss_list);
  318. rb_insert_bss(dev, res);
  319. found = res;
  320. }
  321. dev->bss_generation++;
  322. spin_unlock_bh(&dev->bss_lock);
  323. kref_get(&found->ref);
  324. return found;
  325. }
  326. struct cfg80211_bss *
  327. cfg80211_inform_bss_frame(struct wiphy *wiphy,
  328. struct ieee80211_channel *channel,
  329. struct ieee80211_mgmt *mgmt, size_t len,
  330. s32 signal, gfp_t gfp)
  331. {
  332. struct cfg80211_internal_bss *res;
  333. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  334. u.probe_resp.variable);
  335. bool overwrite;
  336. size_t privsz = wiphy->bss_priv_size;
  337. if (WARN_ON(wiphy->signal_type == NL80211_BSS_SIGNAL_UNSPEC &&
  338. (signal < 0 || signal > 100)))
  339. return NULL;
  340. if (WARN_ON(!mgmt || !wiphy ||
  341. len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  342. return NULL;
  343. res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
  344. if (!res)
  345. return NULL;
  346. memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
  347. res->pub.channel = channel;
  348. res->pub.signal = signal;
  349. res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  350. res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  351. res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  352. /* point to after the private area */
  353. res->pub.information_elements = (u8 *)res + sizeof(*res) + privsz;
  354. memcpy(res->pub.information_elements, mgmt->u.probe_resp.variable, ielen);
  355. res->pub.len_information_elements = ielen;
  356. kref_init(&res->ref);
  357. overwrite = ieee80211_is_probe_resp(mgmt->frame_control);
  358. res = cfg80211_bss_update(wiphy_to_dev(wiphy), res, overwrite);
  359. if (!res)
  360. return NULL;
  361. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  362. regulatory_hint_found_beacon(wiphy, channel, gfp);
  363. /* cfg80211_bss_update gives us a referenced result */
  364. return &res->pub;
  365. }
  366. EXPORT_SYMBOL(cfg80211_inform_bss_frame);
  367. void cfg80211_put_bss(struct cfg80211_bss *pub)
  368. {
  369. struct cfg80211_internal_bss *bss;
  370. if (!pub)
  371. return;
  372. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  373. kref_put(&bss->ref, bss_release);
  374. }
  375. EXPORT_SYMBOL(cfg80211_put_bss);
  376. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  377. {
  378. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  379. struct cfg80211_internal_bss *bss;
  380. if (WARN_ON(!pub))
  381. return;
  382. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  383. spin_lock_bh(&dev->bss_lock);
  384. list_del(&bss->list);
  385. rb_erase(&bss->rbn, &dev->bss_tree);
  386. spin_unlock_bh(&dev->bss_lock);
  387. kref_put(&bss->ref, bss_release);
  388. }
  389. EXPORT_SYMBOL(cfg80211_unlink_bss);
  390. #ifdef CONFIG_WIRELESS_EXT
  391. int cfg80211_wext_siwscan(struct net_device *dev,
  392. struct iw_request_info *info,
  393. union iwreq_data *wrqu, char *extra)
  394. {
  395. struct cfg80211_registered_device *rdev;
  396. struct wiphy *wiphy;
  397. struct iw_scan_req *wreq = NULL;
  398. struct cfg80211_scan_request *creq;
  399. int i, err, n_channels = 0;
  400. enum ieee80211_band band;
  401. if (!netif_running(dev))
  402. return -ENETDOWN;
  403. rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
  404. if (IS_ERR(rdev))
  405. return PTR_ERR(rdev);
  406. if (rdev->scan_req) {
  407. err = -EBUSY;
  408. goto out;
  409. }
  410. wiphy = &rdev->wiphy;
  411. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  412. if (wiphy->bands[band])
  413. n_channels += wiphy->bands[band]->n_channels;
  414. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  415. n_channels * sizeof(void *),
  416. GFP_ATOMIC);
  417. if (!creq) {
  418. err = -ENOMEM;
  419. goto out;
  420. }
  421. creq->wiphy = wiphy;
  422. creq->ifidx = dev->ifindex;
  423. creq->ssids = (void *)(creq + 1);
  424. creq->channels = (void *)(creq->ssids + 1);
  425. creq->n_channels = n_channels;
  426. creq->n_ssids = 1;
  427. /* all channels */
  428. i = 0;
  429. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  430. int j;
  431. if (!wiphy->bands[band])
  432. continue;
  433. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  434. creq->channels[i] = &wiphy->bands[band]->channels[j];
  435. i++;
  436. }
  437. }
  438. /* translate scan request */
  439. if (wrqu->data.length == sizeof(struct iw_scan_req)) {
  440. wreq = (struct iw_scan_req *)extra;
  441. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  442. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN)
  443. return -EINVAL;
  444. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  445. creq->ssids[0].ssid_len = wreq->essid_len;
  446. }
  447. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  448. creq->n_ssids = 0;
  449. }
  450. rdev->scan_req = creq;
  451. err = rdev->ops->scan(wiphy, dev, creq);
  452. if (err) {
  453. rdev->scan_req = NULL;
  454. kfree(creq);
  455. }
  456. out:
  457. cfg80211_put_dev(rdev);
  458. return err;
  459. }
  460. EXPORT_SYMBOL(cfg80211_wext_siwscan);
  461. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  462. struct cfg80211_bss *bss,
  463. char **current_ev, char *end_buf)
  464. {
  465. u8 *pos, *end, *next;
  466. struct iw_event iwe;
  467. if (!bss->information_elements ||
  468. !bss->len_information_elements)
  469. return;
  470. /*
  471. * If needed, fragment the IEs buffer (at IE boundaries) into short
  472. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  473. */
  474. pos = bss->information_elements;
  475. end = pos + bss->len_information_elements;
  476. while (end - pos > IW_GENERIC_IE_MAX) {
  477. next = pos + 2 + pos[1];
  478. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  479. next = next + 2 + next[1];
  480. memset(&iwe, 0, sizeof(iwe));
  481. iwe.cmd = IWEVGENIE;
  482. iwe.u.data.length = next - pos;
  483. *current_ev = iwe_stream_add_point(info, *current_ev,
  484. end_buf, &iwe, pos);
  485. pos = next;
  486. }
  487. if (end > pos) {
  488. memset(&iwe, 0, sizeof(iwe));
  489. iwe.cmd = IWEVGENIE;
  490. iwe.u.data.length = end - pos;
  491. *current_ev = iwe_stream_add_point(info, *current_ev,
  492. end_buf, &iwe, pos);
  493. }
  494. }
  495. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  496. {
  497. unsigned long end = jiffies;
  498. if (end >= start)
  499. return jiffies_to_msecs(end - start);
  500. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  501. }
  502. static char *
  503. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  504. struct cfg80211_internal_bss *bss, char *current_ev,
  505. char *end_buf)
  506. {
  507. struct iw_event iwe;
  508. u8 *buf, *cfg, *p;
  509. u8 *ie = bss->pub.information_elements;
  510. int rem = bss->pub.len_information_elements, i, sig;
  511. bool ismesh = false;
  512. memset(&iwe, 0, sizeof(iwe));
  513. iwe.cmd = SIOCGIWAP;
  514. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  515. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  516. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  517. IW_EV_ADDR_LEN);
  518. memset(&iwe, 0, sizeof(iwe));
  519. iwe.cmd = SIOCGIWFREQ;
  520. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  521. iwe.u.freq.e = 0;
  522. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  523. IW_EV_FREQ_LEN);
  524. memset(&iwe, 0, sizeof(iwe));
  525. iwe.cmd = SIOCGIWFREQ;
  526. iwe.u.freq.m = bss->pub.channel->center_freq;
  527. iwe.u.freq.e = 6;
  528. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  529. IW_EV_FREQ_LEN);
  530. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  531. memset(&iwe, 0, sizeof(iwe));
  532. iwe.cmd = IWEVQUAL;
  533. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  534. IW_QUAL_NOISE_INVALID |
  535. IW_QUAL_QUAL_UPDATED;
  536. switch (wiphy->signal_type) {
  537. case CFG80211_SIGNAL_TYPE_MBM:
  538. sig = bss->pub.signal / 100;
  539. iwe.u.qual.level = sig;
  540. iwe.u.qual.updated |= IW_QUAL_DBM;
  541. if (sig < -110) /* rather bad */
  542. sig = -110;
  543. else if (sig > -40) /* perfect */
  544. sig = -40;
  545. /* will give a range of 0 .. 70 */
  546. iwe.u.qual.qual = sig + 110;
  547. break;
  548. case CFG80211_SIGNAL_TYPE_UNSPEC:
  549. iwe.u.qual.level = bss->pub.signal;
  550. /* will give range 0 .. 100 */
  551. iwe.u.qual.qual = bss->pub.signal;
  552. break;
  553. default:
  554. /* not reached */
  555. break;
  556. }
  557. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  558. &iwe, IW_EV_QUAL_LEN);
  559. }
  560. memset(&iwe, 0, sizeof(iwe));
  561. iwe.cmd = SIOCGIWENCODE;
  562. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  563. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  564. else
  565. iwe.u.data.flags = IW_ENCODE_DISABLED;
  566. iwe.u.data.length = 0;
  567. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  568. &iwe, "");
  569. while (rem >= 2) {
  570. /* invalid data */
  571. if (ie[1] > rem - 2)
  572. break;
  573. switch (ie[0]) {
  574. case WLAN_EID_SSID:
  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_ID:
  583. memset(&iwe, 0, sizeof(iwe));
  584. iwe.cmd = SIOCGIWESSID;
  585. iwe.u.data.length = ie[1];
  586. iwe.u.data.flags = 1;
  587. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  588. &iwe, ie + 2);
  589. break;
  590. case WLAN_EID_MESH_CONFIG:
  591. ismesh = true;
  592. if (ie[1] != IEEE80211_MESH_CONFIG_LEN)
  593. break;
  594. buf = kmalloc(50, GFP_ATOMIC);
  595. if (!buf)
  596. break;
  597. cfg = ie + 2;
  598. memset(&iwe, 0, sizeof(iwe));
  599. iwe.cmd = IWEVCUSTOM;
  600. sprintf(buf, "Mesh network (version %d)", cfg[0]);
  601. iwe.u.data.length = strlen(buf);
  602. current_ev = iwe_stream_add_point(info, current_ev,
  603. end_buf,
  604. &iwe, buf);
  605. sprintf(buf, "Path Selection Protocol ID: "
  606. "0x%02X%02X%02X%02X", cfg[1], cfg[2], cfg[3],
  607. cfg[4]);
  608. iwe.u.data.length = strlen(buf);
  609. current_ev = iwe_stream_add_point(info, current_ev,
  610. end_buf,
  611. &iwe, buf);
  612. sprintf(buf, "Path Selection Metric ID: "
  613. "0x%02X%02X%02X%02X", cfg[5], cfg[6], cfg[7],
  614. cfg[8]);
  615. iwe.u.data.length = strlen(buf);
  616. current_ev = iwe_stream_add_point(info, current_ev,
  617. end_buf,
  618. &iwe, buf);
  619. sprintf(buf, "Congestion Control Mode ID: "
  620. "0x%02X%02X%02X%02X", cfg[9], cfg[10],
  621. cfg[11], cfg[12]);
  622. iwe.u.data.length = strlen(buf);
  623. current_ev = iwe_stream_add_point(info, current_ev,
  624. end_buf,
  625. &iwe, buf);
  626. sprintf(buf, "Channel Precedence: "
  627. "0x%02X%02X%02X%02X", cfg[13], cfg[14],
  628. cfg[15], cfg[16]);
  629. iwe.u.data.length = strlen(buf);
  630. current_ev = iwe_stream_add_point(info, current_ev,
  631. end_buf,
  632. &iwe, buf);
  633. kfree(buf);
  634. break;
  635. case WLAN_EID_SUPP_RATES:
  636. case WLAN_EID_EXT_SUPP_RATES:
  637. /* display all supported rates in readable format */
  638. p = current_ev + iwe_stream_lcp_len(info);
  639. memset(&iwe, 0, sizeof(iwe));
  640. iwe.cmd = SIOCGIWRATE;
  641. /* Those two flags are ignored... */
  642. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  643. for (i = 0; i < ie[1]; i++) {
  644. iwe.u.bitrate.value =
  645. ((ie[i + 2] & 0x7f) * 500000);
  646. p = iwe_stream_add_value(info, current_ev, p,
  647. end_buf, &iwe, IW_EV_PARAM_LEN);
  648. }
  649. current_ev = p;
  650. break;
  651. }
  652. rem -= ie[1] + 2;
  653. ie += ie[1] + 2;
  654. }
  655. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS)
  656. || ismesh) {
  657. memset(&iwe, 0, sizeof(iwe));
  658. iwe.cmd = SIOCGIWMODE;
  659. if (ismesh)
  660. iwe.u.mode = IW_MODE_MESH;
  661. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  662. iwe.u.mode = IW_MODE_MASTER;
  663. else
  664. iwe.u.mode = IW_MODE_ADHOC;
  665. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  666. &iwe, IW_EV_UINT_LEN);
  667. }
  668. buf = kmalloc(30, GFP_ATOMIC);
  669. if (buf) {
  670. memset(&iwe, 0, sizeof(iwe));
  671. iwe.cmd = IWEVCUSTOM;
  672. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  673. iwe.u.data.length = strlen(buf);
  674. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  675. &iwe, buf);
  676. memset(&iwe, 0, sizeof(iwe));
  677. iwe.cmd = IWEVCUSTOM;
  678. sprintf(buf, " Last beacon: %ums ago",
  679. elapsed_jiffies_msecs(bss->ts));
  680. iwe.u.data.length = strlen(buf);
  681. current_ev = iwe_stream_add_point(info, current_ev,
  682. end_buf, &iwe, buf);
  683. kfree(buf);
  684. }
  685. ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
  686. return current_ev;
  687. }
  688. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  689. struct iw_request_info *info,
  690. char *buf, size_t len)
  691. {
  692. char *current_ev = buf;
  693. char *end_buf = buf + len;
  694. struct cfg80211_internal_bss *bss;
  695. spin_lock_bh(&dev->bss_lock);
  696. cfg80211_bss_expire(dev);
  697. list_for_each_entry(bss, &dev->bss_list, list) {
  698. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  699. spin_unlock_bh(&dev->bss_lock);
  700. return -E2BIG;
  701. }
  702. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  703. current_ev, end_buf);
  704. }
  705. spin_unlock_bh(&dev->bss_lock);
  706. return current_ev - buf;
  707. }
  708. int cfg80211_wext_giwscan(struct net_device *dev,
  709. struct iw_request_info *info,
  710. struct iw_point *data, char *extra)
  711. {
  712. struct cfg80211_registered_device *rdev;
  713. int res;
  714. if (!netif_running(dev))
  715. return -ENETDOWN;
  716. rdev = cfg80211_get_dev_from_ifindex(dev->ifindex);
  717. if (IS_ERR(rdev))
  718. return PTR_ERR(rdev);
  719. if (rdev->scan_req) {
  720. res = -EAGAIN;
  721. goto out;
  722. }
  723. res = ieee80211_scan_results(rdev, info, extra, data->length);
  724. data->length = 0;
  725. if (res >= 0) {
  726. data->length = res;
  727. res = 0;
  728. }
  729. out:
  730. cfg80211_put_dev(rdev);
  731. return res;
  732. }
  733. EXPORT_SYMBOL(cfg80211_wext_giwscan);
  734. #endif