scan.c 22 KB

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