scan.c 24 KB

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