scan.c 21 KB

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