scan.c 20 KB

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