scan.c 24 KB

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