scan.c 23 KB

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