scan.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359
  1. /*
  2. * cfg80211 scan result handling
  3. *
  4. * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
  5. */
  6. #include <linux/kernel.h>
  7. #include <linux/slab.h>
  8. #include <linux/module.h>
  9. #include <linux/netdevice.h>
  10. #include <linux/wireless.h>
  11. #include <linux/nl80211.h>
  12. #include <linux/etherdevice.h>
  13. #include <net/arp.h>
  14. #include <net/cfg80211.h>
  15. #include <net/cfg80211-wext.h>
  16. #include <net/iw_handler.h>
  17. #include "core.h"
  18. #include "nl80211.h"
  19. #include "wext-compat.h"
  20. #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
  21. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev, bool leak)
  22. {
  23. struct cfg80211_scan_request *request;
  24. struct wireless_dev *wdev;
  25. #ifdef CONFIG_CFG80211_WEXT
  26. union iwreq_data wrqu;
  27. #endif
  28. ASSERT_RDEV_LOCK(rdev);
  29. request = rdev->scan_req;
  30. if (!request)
  31. return;
  32. wdev = request->wdev;
  33. /*
  34. * This must be before sending the other events!
  35. * Otherwise, wpa_supplicant gets completely confused with
  36. * wext events.
  37. */
  38. if (wdev->netdev)
  39. cfg80211_sme_scan_done(wdev->netdev);
  40. if (request->aborted)
  41. nl80211_send_scan_aborted(rdev, wdev);
  42. else
  43. nl80211_send_scan_done(rdev, wdev);
  44. #ifdef CONFIG_CFG80211_WEXT
  45. if (wdev->netdev && !request->aborted) {
  46. memset(&wrqu, 0, sizeof(wrqu));
  47. wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
  48. }
  49. #endif
  50. if (wdev->netdev)
  51. dev_put(wdev->netdev);
  52. rdev->scan_req = NULL;
  53. /*
  54. * OK. If this is invoked with "leak" then we can't
  55. * free this ... but we've cleaned it up anyway. The
  56. * driver failed to call the scan_done callback, so
  57. * all bets are off, it might still be trying to use
  58. * the scan request or not ... if it accesses the dev
  59. * in there (it shouldn't anyway) then it may crash.
  60. */
  61. if (!leak)
  62. kfree(request);
  63. }
  64. void __cfg80211_scan_done(struct work_struct *wk)
  65. {
  66. struct cfg80211_registered_device *rdev;
  67. rdev = container_of(wk, struct cfg80211_registered_device,
  68. scan_done_wk);
  69. cfg80211_lock_rdev(rdev);
  70. ___cfg80211_scan_done(rdev, false);
  71. cfg80211_unlock_rdev(rdev);
  72. }
  73. void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
  74. {
  75. WARN_ON(request != wiphy_to_dev(request->wiphy)->scan_req);
  76. request->aborted = aborted;
  77. queue_work(cfg80211_wq, &wiphy_to_dev(request->wiphy)->scan_done_wk);
  78. }
  79. EXPORT_SYMBOL(cfg80211_scan_done);
  80. void __cfg80211_sched_scan_results(struct work_struct *wk)
  81. {
  82. struct cfg80211_registered_device *rdev;
  83. rdev = container_of(wk, struct cfg80211_registered_device,
  84. sched_scan_results_wk);
  85. mutex_lock(&rdev->sched_scan_mtx);
  86. /* we don't have sched_scan_req anymore if the scan is stopping */
  87. if (rdev->sched_scan_req)
  88. nl80211_send_sched_scan_results(rdev,
  89. rdev->sched_scan_req->dev);
  90. mutex_unlock(&rdev->sched_scan_mtx);
  91. }
  92. void cfg80211_sched_scan_results(struct wiphy *wiphy)
  93. {
  94. /* ignore if we're not scanning */
  95. if (wiphy_to_dev(wiphy)->sched_scan_req)
  96. queue_work(cfg80211_wq,
  97. &wiphy_to_dev(wiphy)->sched_scan_results_wk);
  98. }
  99. EXPORT_SYMBOL(cfg80211_sched_scan_results);
  100. void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
  101. {
  102. struct cfg80211_registered_device *rdev = wiphy_to_dev(wiphy);
  103. mutex_lock(&rdev->sched_scan_mtx);
  104. __cfg80211_stop_sched_scan(rdev, true);
  105. mutex_unlock(&rdev->sched_scan_mtx);
  106. }
  107. EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
  108. int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
  109. bool driver_initiated)
  110. {
  111. struct net_device *dev;
  112. lockdep_assert_held(&rdev->sched_scan_mtx);
  113. if (!rdev->sched_scan_req)
  114. return -ENOENT;
  115. dev = rdev->sched_scan_req->dev;
  116. if (!driver_initiated) {
  117. int err = rdev->ops->sched_scan_stop(&rdev->wiphy, dev);
  118. if (err)
  119. return err;
  120. }
  121. nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
  122. kfree(rdev->sched_scan_req);
  123. rdev->sched_scan_req = NULL;
  124. return 0;
  125. }
  126. static void bss_release(struct kref *ref)
  127. {
  128. struct cfg80211_internal_bss *bss;
  129. bss = container_of(ref, struct cfg80211_internal_bss, ref);
  130. if (bss->pub.free_priv)
  131. bss->pub.free_priv(&bss->pub);
  132. if (bss->beacon_ies_allocated)
  133. kfree(bss->pub.beacon_ies);
  134. if (bss->proberesp_ies_allocated)
  135. kfree(bss->pub.proberesp_ies);
  136. BUG_ON(atomic_read(&bss->hold));
  137. kfree(bss);
  138. }
  139. /* must hold dev->bss_lock! */
  140. void cfg80211_bss_age(struct cfg80211_registered_device *dev,
  141. unsigned long age_secs)
  142. {
  143. struct cfg80211_internal_bss *bss;
  144. unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
  145. list_for_each_entry(bss, &dev->bss_list, list) {
  146. bss->ts -= age_jiffies;
  147. }
  148. }
  149. /* must hold dev->bss_lock! */
  150. static void __cfg80211_unlink_bss(struct cfg80211_registered_device *dev,
  151. struct cfg80211_internal_bss *bss)
  152. {
  153. list_del_init(&bss->list);
  154. rb_erase(&bss->rbn, &dev->bss_tree);
  155. kref_put(&bss->ref, bss_release);
  156. }
  157. /* must hold dev->bss_lock! */
  158. void cfg80211_bss_expire(struct cfg80211_registered_device *dev)
  159. {
  160. struct cfg80211_internal_bss *bss, *tmp;
  161. bool expired = false;
  162. list_for_each_entry_safe(bss, tmp, &dev->bss_list, list) {
  163. if (atomic_read(&bss->hold))
  164. continue;
  165. if (!time_after(jiffies, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE))
  166. continue;
  167. __cfg80211_unlink_bss(dev, bss);
  168. expired = true;
  169. }
  170. if (expired)
  171. dev->bss_generation++;
  172. }
  173. const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
  174. {
  175. while (len > 2 && ies[0] != eid) {
  176. len -= ies[1] + 2;
  177. ies += ies[1] + 2;
  178. }
  179. if (len < 2)
  180. return NULL;
  181. if (len < 2 + ies[1])
  182. return NULL;
  183. return ies;
  184. }
  185. EXPORT_SYMBOL(cfg80211_find_ie);
  186. const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type,
  187. const u8 *ies, int len)
  188. {
  189. struct ieee80211_vendor_ie *ie;
  190. const u8 *pos = ies, *end = ies + len;
  191. int ie_oui;
  192. while (pos < end) {
  193. pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos,
  194. end - pos);
  195. if (!pos)
  196. return NULL;
  197. if (end - pos < sizeof(*ie))
  198. return NULL;
  199. ie = (struct ieee80211_vendor_ie *)pos;
  200. ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2];
  201. if (ie_oui == oui && ie->oui_type == oui_type)
  202. return pos;
  203. pos += 2 + ie->len;
  204. }
  205. return NULL;
  206. }
  207. EXPORT_SYMBOL(cfg80211_find_vendor_ie);
  208. static int cmp_ies(u8 num, u8 *ies1, size_t len1, u8 *ies2, size_t len2)
  209. {
  210. const u8 *ie1 = cfg80211_find_ie(num, ies1, len1);
  211. const u8 *ie2 = cfg80211_find_ie(num, ies2, len2);
  212. /* equal if both missing */
  213. if (!ie1 && !ie2)
  214. return 0;
  215. /* sort missing IE before (left of) present IE */
  216. if (!ie1)
  217. return -1;
  218. if (!ie2)
  219. return 1;
  220. /* sort by length first, then by contents */
  221. if (ie1[1] != ie2[1])
  222. return ie2[1] - ie1[1];
  223. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  224. }
  225. static bool is_bss(struct cfg80211_bss *a,
  226. const u8 *bssid,
  227. const u8 *ssid, size_t ssid_len)
  228. {
  229. const u8 *ssidie;
  230. if (bssid && !ether_addr_equal(a->bssid, bssid))
  231. return false;
  232. if (!ssid)
  233. return true;
  234. ssidie = cfg80211_find_ie(WLAN_EID_SSID,
  235. a->information_elements,
  236. a->len_information_elements);
  237. if (!ssidie)
  238. return false;
  239. if (ssidie[1] != ssid_len)
  240. return false;
  241. return memcmp(ssidie + 2, ssid, ssid_len) == 0;
  242. }
  243. static bool is_mesh_bss(struct cfg80211_bss *a)
  244. {
  245. const u8 *ie;
  246. if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
  247. return false;
  248. ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
  249. a->information_elements,
  250. a->len_information_elements);
  251. if (!ie)
  252. return false;
  253. ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  254. a->information_elements,
  255. a->len_information_elements);
  256. if (!ie)
  257. return false;
  258. return true;
  259. }
  260. static bool is_mesh(struct cfg80211_bss *a,
  261. const u8 *meshid, size_t meshidlen,
  262. const u8 *meshcfg)
  263. {
  264. const u8 *ie;
  265. if (!WLAN_CAPABILITY_IS_STA_BSS(a->capability))
  266. return false;
  267. ie = cfg80211_find_ie(WLAN_EID_MESH_ID,
  268. a->information_elements,
  269. a->len_information_elements);
  270. if (!ie)
  271. return false;
  272. if (ie[1] != meshidlen)
  273. return false;
  274. if (memcmp(ie + 2, meshid, meshidlen))
  275. return false;
  276. ie = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  277. a->information_elements,
  278. a->len_information_elements);
  279. if (!ie)
  280. return false;
  281. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  282. return false;
  283. /*
  284. * Ignore mesh capability (last two bytes of the IE) when
  285. * comparing since that may differ between stations taking
  286. * part in the same mesh.
  287. */
  288. return memcmp(ie + 2, meshcfg,
  289. sizeof(struct ieee80211_meshconf_ie) - 2) == 0;
  290. }
  291. static int cmp_bss_core(struct cfg80211_bss *a,
  292. struct cfg80211_bss *b)
  293. {
  294. int r;
  295. if (a->channel != b->channel)
  296. return b->channel->center_freq - a->channel->center_freq;
  297. if (is_mesh_bss(a) && is_mesh_bss(b)) {
  298. r = cmp_ies(WLAN_EID_MESH_ID,
  299. a->information_elements,
  300. a->len_information_elements,
  301. b->information_elements,
  302. b->len_information_elements);
  303. if (r)
  304. return r;
  305. return cmp_ies(WLAN_EID_MESH_CONFIG,
  306. a->information_elements,
  307. a->len_information_elements,
  308. b->information_elements,
  309. b->len_information_elements);
  310. }
  311. /*
  312. * we can't use compare_ether_addr here since we need a < > operator.
  313. * The binary return value of compare_ether_addr isn't enough
  314. */
  315. return memcmp(a->bssid, b->bssid, sizeof(a->bssid));
  316. }
  317. static int cmp_bss(struct cfg80211_bss *a,
  318. struct cfg80211_bss *b)
  319. {
  320. int r;
  321. r = cmp_bss_core(a, b);
  322. if (r)
  323. return r;
  324. return cmp_ies(WLAN_EID_SSID,
  325. a->information_elements,
  326. a->len_information_elements,
  327. b->information_elements,
  328. b->len_information_elements);
  329. }
  330. static int cmp_hidden_bss(struct cfg80211_bss *a,
  331. struct cfg80211_bss *b)
  332. {
  333. const u8 *ie1;
  334. const u8 *ie2;
  335. int i;
  336. int r;
  337. r = cmp_bss_core(a, b);
  338. if (r)
  339. return r;
  340. ie1 = cfg80211_find_ie(WLAN_EID_SSID,
  341. a->information_elements,
  342. a->len_information_elements);
  343. ie2 = cfg80211_find_ie(WLAN_EID_SSID,
  344. b->information_elements,
  345. b->len_information_elements);
  346. /* Key comparator must use same algorithm in any rb-tree
  347. * search function (order is important), otherwise ordering
  348. * of items in the tree is broken and search gives incorrect
  349. * results. This code uses same order as cmp_ies() does. */
  350. /* sort missing IE before (left of) present IE */
  351. if (!ie1)
  352. return -1;
  353. if (!ie2)
  354. return 1;
  355. /* zero-size SSID is used as an indication of the hidden bss */
  356. if (!ie2[1])
  357. return 0;
  358. /* sort by length first, then by contents */
  359. if (ie1[1] != ie2[1])
  360. return ie2[1] - ie1[1];
  361. /* zeroed SSID ie is another indication of a hidden bss */
  362. for (i = 0; i < ie2[1]; i++)
  363. if (ie2[i + 2])
  364. return -1;
  365. return 0;
  366. }
  367. struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
  368. struct ieee80211_channel *channel,
  369. const u8 *bssid,
  370. const u8 *ssid, size_t ssid_len,
  371. u16 capa_mask, u16 capa_val)
  372. {
  373. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  374. struct cfg80211_internal_bss *bss, *res = NULL;
  375. unsigned long now = jiffies;
  376. spin_lock_bh(&dev->bss_lock);
  377. list_for_each_entry(bss, &dev->bss_list, list) {
  378. if ((bss->pub.capability & capa_mask) != capa_val)
  379. continue;
  380. if (channel && bss->pub.channel != channel)
  381. continue;
  382. /* Don't get expired BSS structs */
  383. if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
  384. !atomic_read(&bss->hold))
  385. continue;
  386. if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
  387. res = bss;
  388. kref_get(&res->ref);
  389. break;
  390. }
  391. }
  392. spin_unlock_bh(&dev->bss_lock);
  393. if (!res)
  394. return NULL;
  395. return &res->pub;
  396. }
  397. EXPORT_SYMBOL(cfg80211_get_bss);
  398. struct cfg80211_bss *cfg80211_get_mesh(struct wiphy *wiphy,
  399. struct ieee80211_channel *channel,
  400. const u8 *meshid, size_t meshidlen,
  401. const u8 *meshcfg)
  402. {
  403. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  404. struct cfg80211_internal_bss *bss, *res = NULL;
  405. spin_lock_bh(&dev->bss_lock);
  406. list_for_each_entry(bss, &dev->bss_list, list) {
  407. if (channel && bss->pub.channel != channel)
  408. continue;
  409. if (is_mesh(&bss->pub, meshid, meshidlen, meshcfg)) {
  410. res = bss;
  411. kref_get(&res->ref);
  412. break;
  413. }
  414. }
  415. spin_unlock_bh(&dev->bss_lock);
  416. if (!res)
  417. return NULL;
  418. return &res->pub;
  419. }
  420. EXPORT_SYMBOL(cfg80211_get_mesh);
  421. static void rb_insert_bss(struct cfg80211_registered_device *dev,
  422. struct cfg80211_internal_bss *bss)
  423. {
  424. struct rb_node **p = &dev->bss_tree.rb_node;
  425. struct rb_node *parent = NULL;
  426. struct cfg80211_internal_bss *tbss;
  427. int cmp;
  428. while (*p) {
  429. parent = *p;
  430. tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
  431. cmp = cmp_bss(&bss->pub, &tbss->pub);
  432. if (WARN_ON(!cmp)) {
  433. /* will sort of leak this BSS */
  434. return;
  435. }
  436. if (cmp < 0)
  437. p = &(*p)->rb_left;
  438. else
  439. p = &(*p)->rb_right;
  440. }
  441. rb_link_node(&bss->rbn, parent, p);
  442. rb_insert_color(&bss->rbn, &dev->bss_tree);
  443. }
  444. static struct cfg80211_internal_bss *
  445. rb_find_bss(struct cfg80211_registered_device *dev,
  446. struct cfg80211_internal_bss *res)
  447. {
  448. struct rb_node *n = dev->bss_tree.rb_node;
  449. struct cfg80211_internal_bss *bss;
  450. int r;
  451. while (n) {
  452. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  453. r = cmp_bss(&res->pub, &bss->pub);
  454. if (r == 0)
  455. return bss;
  456. else if (r < 0)
  457. n = n->rb_left;
  458. else
  459. n = n->rb_right;
  460. }
  461. return NULL;
  462. }
  463. static struct cfg80211_internal_bss *
  464. rb_find_hidden_bss(struct cfg80211_registered_device *dev,
  465. struct cfg80211_internal_bss *res)
  466. {
  467. struct rb_node *n = dev->bss_tree.rb_node;
  468. struct cfg80211_internal_bss *bss;
  469. int r;
  470. while (n) {
  471. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  472. r = cmp_hidden_bss(&res->pub, &bss->pub);
  473. if (r == 0)
  474. return bss;
  475. else if (r < 0)
  476. n = n->rb_left;
  477. else
  478. n = n->rb_right;
  479. }
  480. return NULL;
  481. }
  482. static void
  483. copy_hidden_ies(struct cfg80211_internal_bss *res,
  484. struct cfg80211_internal_bss *hidden)
  485. {
  486. if (unlikely(res->pub.beacon_ies))
  487. return;
  488. if (WARN_ON(!hidden->pub.beacon_ies))
  489. return;
  490. res->pub.beacon_ies = kmalloc(hidden->pub.len_beacon_ies, GFP_ATOMIC);
  491. if (unlikely(!res->pub.beacon_ies))
  492. return;
  493. res->beacon_ies_allocated = true;
  494. res->pub.len_beacon_ies = hidden->pub.len_beacon_ies;
  495. memcpy(res->pub.beacon_ies, hidden->pub.beacon_ies,
  496. res->pub.len_beacon_ies);
  497. }
  498. static struct cfg80211_internal_bss *
  499. cfg80211_bss_update(struct cfg80211_registered_device *dev,
  500. struct cfg80211_internal_bss *res)
  501. {
  502. struct cfg80211_internal_bss *found = NULL;
  503. /*
  504. * The reference to "res" is donated to this function.
  505. */
  506. if (WARN_ON(!res->pub.channel)) {
  507. kref_put(&res->ref, bss_release);
  508. return NULL;
  509. }
  510. res->ts = jiffies;
  511. spin_lock_bh(&dev->bss_lock);
  512. found = rb_find_bss(dev, res);
  513. if (found) {
  514. found->pub.beacon_interval = res->pub.beacon_interval;
  515. found->pub.tsf = res->pub.tsf;
  516. found->pub.signal = res->pub.signal;
  517. found->pub.capability = res->pub.capability;
  518. found->ts = res->ts;
  519. /* Update IEs */
  520. if (res->pub.proberesp_ies) {
  521. size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
  522. size_t ielen = res->pub.len_proberesp_ies;
  523. if (found->pub.proberesp_ies &&
  524. !found->proberesp_ies_allocated &&
  525. ksize(found) >= used + ielen) {
  526. memcpy(found->pub.proberesp_ies,
  527. res->pub.proberesp_ies, ielen);
  528. found->pub.len_proberesp_ies = ielen;
  529. } else {
  530. u8 *ies = found->pub.proberesp_ies;
  531. if (found->proberesp_ies_allocated)
  532. ies = krealloc(ies, ielen, GFP_ATOMIC);
  533. else
  534. ies = kmalloc(ielen, GFP_ATOMIC);
  535. if (ies) {
  536. memcpy(ies, res->pub.proberesp_ies,
  537. ielen);
  538. found->proberesp_ies_allocated = true;
  539. found->pub.proberesp_ies = ies;
  540. found->pub.len_proberesp_ies = ielen;
  541. }
  542. }
  543. /* Override possible earlier Beacon frame IEs */
  544. found->pub.information_elements =
  545. found->pub.proberesp_ies;
  546. found->pub.len_information_elements =
  547. found->pub.len_proberesp_ies;
  548. }
  549. if (res->pub.beacon_ies) {
  550. size_t used = dev->wiphy.bss_priv_size + sizeof(*res);
  551. size_t ielen = res->pub.len_beacon_ies;
  552. bool information_elements_is_beacon_ies =
  553. (found->pub.information_elements ==
  554. found->pub.beacon_ies);
  555. if (found->pub.beacon_ies &&
  556. !found->beacon_ies_allocated &&
  557. ksize(found) >= used + ielen) {
  558. memcpy(found->pub.beacon_ies,
  559. res->pub.beacon_ies, ielen);
  560. found->pub.len_beacon_ies = ielen;
  561. } else {
  562. u8 *ies = found->pub.beacon_ies;
  563. if (found->beacon_ies_allocated)
  564. ies = krealloc(ies, ielen, GFP_ATOMIC);
  565. else
  566. ies = kmalloc(ielen, GFP_ATOMIC);
  567. if (ies) {
  568. memcpy(ies, res->pub.beacon_ies,
  569. ielen);
  570. found->beacon_ies_allocated = true;
  571. found->pub.beacon_ies = ies;
  572. found->pub.len_beacon_ies = ielen;
  573. }
  574. }
  575. /* Override IEs if they were from a beacon before */
  576. if (information_elements_is_beacon_ies) {
  577. found->pub.information_elements =
  578. found->pub.beacon_ies;
  579. found->pub.len_information_elements =
  580. found->pub.len_beacon_ies;
  581. }
  582. }
  583. kref_put(&res->ref, bss_release);
  584. } else {
  585. struct cfg80211_internal_bss *hidden;
  586. /* First check if the beacon is a probe response from
  587. * a hidden bss. If so, copy beacon ies (with nullified
  588. * ssid) into the probe response bss entry (with real ssid).
  589. * It is required basically for PSM implementation
  590. * (probe responses do not contain tim ie) */
  591. /* TODO: The code is not trying to update existing probe
  592. * response bss entries when beacon ies are
  593. * getting changed. */
  594. hidden = rb_find_hidden_bss(dev, res);
  595. if (hidden)
  596. copy_hidden_ies(res, hidden);
  597. /* this "consumes" the reference */
  598. list_add_tail(&res->list, &dev->bss_list);
  599. rb_insert_bss(dev, res);
  600. found = res;
  601. }
  602. dev->bss_generation++;
  603. spin_unlock_bh(&dev->bss_lock);
  604. kref_get(&found->ref);
  605. return found;
  606. }
  607. struct cfg80211_bss*
  608. cfg80211_inform_bss(struct wiphy *wiphy,
  609. struct ieee80211_channel *channel,
  610. const u8 *bssid, u64 tsf, u16 capability,
  611. u16 beacon_interval, const u8 *ie, size_t ielen,
  612. s32 signal, gfp_t gfp)
  613. {
  614. struct cfg80211_internal_bss *res;
  615. size_t privsz;
  616. if (WARN_ON(!wiphy))
  617. return NULL;
  618. privsz = wiphy->bss_priv_size;
  619. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  620. (signal < 0 || signal > 100)))
  621. return NULL;
  622. res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
  623. if (!res)
  624. return NULL;
  625. memcpy(res->pub.bssid, bssid, ETH_ALEN);
  626. res->pub.channel = channel;
  627. res->pub.signal = signal;
  628. res->pub.tsf = tsf;
  629. res->pub.beacon_interval = beacon_interval;
  630. res->pub.capability = capability;
  631. /*
  632. * Since we do not know here whether the IEs are from a Beacon or Probe
  633. * Response frame, we need to pick one of the options and only use it
  634. * with the driver that does not provide the full Beacon/Probe Response
  635. * frame. Use Beacon frame pointer to avoid indicating that this should
  636. * override the information_elements pointer should we have received an
  637. * earlier indication of Probe Response data.
  638. *
  639. * The initial buffer for the IEs is allocated with the BSS entry and
  640. * is located after the private area.
  641. */
  642. res->pub.beacon_ies = (u8 *)res + sizeof(*res) + privsz;
  643. memcpy(res->pub.beacon_ies, ie, ielen);
  644. res->pub.len_beacon_ies = ielen;
  645. res->pub.information_elements = res->pub.beacon_ies;
  646. res->pub.len_information_elements = res->pub.len_beacon_ies;
  647. kref_init(&res->ref);
  648. res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
  649. if (!res)
  650. return NULL;
  651. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  652. regulatory_hint_found_beacon(wiphy, channel, gfp);
  653. /* cfg80211_bss_update gives us a referenced result */
  654. return &res->pub;
  655. }
  656. EXPORT_SYMBOL(cfg80211_inform_bss);
  657. struct cfg80211_bss *
  658. cfg80211_inform_bss_frame(struct wiphy *wiphy,
  659. struct ieee80211_channel *channel,
  660. struct ieee80211_mgmt *mgmt, size_t len,
  661. s32 signal, gfp_t gfp)
  662. {
  663. struct cfg80211_internal_bss *res;
  664. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  665. u.probe_resp.variable);
  666. size_t privsz;
  667. if (WARN_ON(!mgmt))
  668. return NULL;
  669. if (WARN_ON(!wiphy))
  670. return NULL;
  671. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  672. (signal < 0 || signal > 100)))
  673. return NULL;
  674. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  675. return NULL;
  676. privsz = wiphy->bss_priv_size;
  677. res = kzalloc(sizeof(*res) + privsz + ielen, gfp);
  678. if (!res)
  679. return NULL;
  680. memcpy(res->pub.bssid, mgmt->bssid, ETH_ALEN);
  681. res->pub.channel = channel;
  682. res->pub.signal = signal;
  683. res->pub.tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  684. res->pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  685. res->pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  686. /*
  687. * The initial buffer for the IEs is allocated with the BSS entry and
  688. * is located after the private area.
  689. */
  690. if (ieee80211_is_probe_resp(mgmt->frame_control)) {
  691. res->pub.proberesp_ies = (u8 *) res + sizeof(*res) + privsz;
  692. memcpy(res->pub.proberesp_ies, mgmt->u.probe_resp.variable,
  693. ielen);
  694. res->pub.len_proberesp_ies = ielen;
  695. res->pub.information_elements = res->pub.proberesp_ies;
  696. res->pub.len_information_elements = res->pub.len_proberesp_ies;
  697. } else {
  698. res->pub.beacon_ies = (u8 *) res + sizeof(*res) + privsz;
  699. memcpy(res->pub.beacon_ies, mgmt->u.beacon.variable, ielen);
  700. res->pub.len_beacon_ies = ielen;
  701. res->pub.information_elements = res->pub.beacon_ies;
  702. res->pub.len_information_elements = res->pub.len_beacon_ies;
  703. }
  704. kref_init(&res->ref);
  705. res = cfg80211_bss_update(wiphy_to_dev(wiphy), res);
  706. if (!res)
  707. return NULL;
  708. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  709. regulatory_hint_found_beacon(wiphy, channel, gfp);
  710. /* cfg80211_bss_update gives us a referenced result */
  711. return &res->pub;
  712. }
  713. EXPORT_SYMBOL(cfg80211_inform_bss_frame);
  714. void cfg80211_ref_bss(struct cfg80211_bss *pub)
  715. {
  716. struct cfg80211_internal_bss *bss;
  717. if (!pub)
  718. return;
  719. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  720. kref_get(&bss->ref);
  721. }
  722. EXPORT_SYMBOL(cfg80211_ref_bss);
  723. void cfg80211_put_bss(struct cfg80211_bss *pub)
  724. {
  725. struct cfg80211_internal_bss *bss;
  726. if (!pub)
  727. return;
  728. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  729. kref_put(&bss->ref, bss_release);
  730. }
  731. EXPORT_SYMBOL(cfg80211_put_bss);
  732. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  733. {
  734. struct cfg80211_registered_device *dev = wiphy_to_dev(wiphy);
  735. struct cfg80211_internal_bss *bss;
  736. if (WARN_ON(!pub))
  737. return;
  738. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  739. spin_lock_bh(&dev->bss_lock);
  740. if (!list_empty(&bss->list)) {
  741. __cfg80211_unlink_bss(dev, bss);
  742. dev->bss_generation++;
  743. }
  744. spin_unlock_bh(&dev->bss_lock);
  745. }
  746. EXPORT_SYMBOL(cfg80211_unlink_bss);
  747. #ifdef CONFIG_CFG80211_WEXT
  748. int cfg80211_wext_siwscan(struct net_device *dev,
  749. struct iw_request_info *info,
  750. union iwreq_data *wrqu, char *extra)
  751. {
  752. struct cfg80211_registered_device *rdev;
  753. struct wiphy *wiphy;
  754. struct iw_scan_req *wreq = NULL;
  755. struct cfg80211_scan_request *creq = NULL;
  756. int i, err, n_channels = 0;
  757. enum ieee80211_band band;
  758. if (!netif_running(dev))
  759. return -ENETDOWN;
  760. if (wrqu->data.length == sizeof(struct iw_scan_req))
  761. wreq = (struct iw_scan_req *)extra;
  762. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  763. if (IS_ERR(rdev))
  764. return PTR_ERR(rdev);
  765. if (rdev->scan_req) {
  766. err = -EBUSY;
  767. goto out;
  768. }
  769. wiphy = &rdev->wiphy;
  770. /* Determine number of channels, needed to allocate creq */
  771. if (wreq && wreq->num_channels)
  772. n_channels = wreq->num_channels;
  773. else {
  774. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  775. if (wiphy->bands[band])
  776. n_channels += wiphy->bands[band]->n_channels;
  777. }
  778. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  779. n_channels * sizeof(void *),
  780. GFP_ATOMIC);
  781. if (!creq) {
  782. err = -ENOMEM;
  783. goto out;
  784. }
  785. creq->wiphy = wiphy;
  786. creq->wdev = dev->ieee80211_ptr;
  787. /* SSIDs come after channels */
  788. creq->ssids = (void *)&creq->channels[n_channels];
  789. creq->n_channels = n_channels;
  790. creq->n_ssids = 1;
  791. /* translate "Scan on frequencies" request */
  792. i = 0;
  793. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  794. int j;
  795. if (!wiphy->bands[band])
  796. continue;
  797. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  798. /* ignore disabled channels */
  799. if (wiphy->bands[band]->channels[j].flags &
  800. IEEE80211_CHAN_DISABLED)
  801. continue;
  802. /* If we have a wireless request structure and the
  803. * wireless request specifies frequencies, then search
  804. * for the matching hardware channel.
  805. */
  806. if (wreq && wreq->num_channels) {
  807. int k;
  808. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  809. for (k = 0; k < wreq->num_channels; k++) {
  810. int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
  811. if (wext_freq == wiphy_freq)
  812. goto wext_freq_found;
  813. }
  814. goto wext_freq_not_found;
  815. }
  816. wext_freq_found:
  817. creq->channels[i] = &wiphy->bands[band]->channels[j];
  818. i++;
  819. wext_freq_not_found: ;
  820. }
  821. }
  822. /* No channels found? */
  823. if (!i) {
  824. err = -EINVAL;
  825. goto out;
  826. }
  827. /* Set real number of channels specified in creq->channels[] */
  828. creq->n_channels = i;
  829. /* translate "Scan for SSID" request */
  830. if (wreq) {
  831. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  832. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  833. err = -EINVAL;
  834. goto out;
  835. }
  836. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  837. creq->ssids[0].ssid_len = wreq->essid_len;
  838. }
  839. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  840. creq->n_ssids = 0;
  841. }
  842. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  843. if (wiphy->bands[i])
  844. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  845. rdev->scan_req = creq;
  846. err = rdev->ops->scan(wiphy, creq);
  847. if (err) {
  848. rdev->scan_req = NULL;
  849. /* creq will be freed below */
  850. } else {
  851. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  852. /* creq now owned by driver */
  853. creq = NULL;
  854. dev_hold(dev);
  855. }
  856. out:
  857. kfree(creq);
  858. cfg80211_unlock_rdev(rdev);
  859. return err;
  860. }
  861. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  862. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  863. struct cfg80211_bss *bss,
  864. char **current_ev, char *end_buf)
  865. {
  866. u8 *pos, *end, *next;
  867. struct iw_event iwe;
  868. if (!bss->information_elements ||
  869. !bss->len_information_elements)
  870. return;
  871. /*
  872. * If needed, fragment the IEs buffer (at IE boundaries) into short
  873. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  874. */
  875. pos = bss->information_elements;
  876. end = pos + bss->len_information_elements;
  877. while (end - pos > IW_GENERIC_IE_MAX) {
  878. next = pos + 2 + pos[1];
  879. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  880. next = next + 2 + next[1];
  881. memset(&iwe, 0, sizeof(iwe));
  882. iwe.cmd = IWEVGENIE;
  883. iwe.u.data.length = next - pos;
  884. *current_ev = iwe_stream_add_point(info, *current_ev,
  885. end_buf, &iwe, pos);
  886. pos = next;
  887. }
  888. if (end > pos) {
  889. memset(&iwe, 0, sizeof(iwe));
  890. iwe.cmd = IWEVGENIE;
  891. iwe.u.data.length = end - pos;
  892. *current_ev = iwe_stream_add_point(info, *current_ev,
  893. end_buf, &iwe, pos);
  894. }
  895. }
  896. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  897. {
  898. unsigned long end = jiffies;
  899. if (end >= start)
  900. return jiffies_to_msecs(end - start);
  901. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  902. }
  903. static char *
  904. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  905. struct cfg80211_internal_bss *bss, char *current_ev,
  906. char *end_buf)
  907. {
  908. struct iw_event iwe;
  909. u8 *buf, *cfg, *p;
  910. u8 *ie = bss->pub.information_elements;
  911. int rem = bss->pub.len_information_elements, i, sig;
  912. bool ismesh = false;
  913. memset(&iwe, 0, sizeof(iwe));
  914. iwe.cmd = SIOCGIWAP;
  915. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  916. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  917. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  918. IW_EV_ADDR_LEN);
  919. memset(&iwe, 0, sizeof(iwe));
  920. iwe.cmd = SIOCGIWFREQ;
  921. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  922. iwe.u.freq.e = 0;
  923. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  924. IW_EV_FREQ_LEN);
  925. memset(&iwe, 0, sizeof(iwe));
  926. iwe.cmd = SIOCGIWFREQ;
  927. iwe.u.freq.m = bss->pub.channel->center_freq;
  928. iwe.u.freq.e = 6;
  929. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  930. IW_EV_FREQ_LEN);
  931. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  932. memset(&iwe, 0, sizeof(iwe));
  933. iwe.cmd = IWEVQUAL;
  934. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  935. IW_QUAL_NOISE_INVALID |
  936. IW_QUAL_QUAL_UPDATED;
  937. switch (wiphy->signal_type) {
  938. case CFG80211_SIGNAL_TYPE_MBM:
  939. sig = bss->pub.signal / 100;
  940. iwe.u.qual.level = sig;
  941. iwe.u.qual.updated |= IW_QUAL_DBM;
  942. if (sig < -110) /* rather bad */
  943. sig = -110;
  944. else if (sig > -40) /* perfect */
  945. sig = -40;
  946. /* will give a range of 0 .. 70 */
  947. iwe.u.qual.qual = sig + 110;
  948. break;
  949. case CFG80211_SIGNAL_TYPE_UNSPEC:
  950. iwe.u.qual.level = bss->pub.signal;
  951. /* will give range 0 .. 100 */
  952. iwe.u.qual.qual = bss->pub.signal;
  953. break;
  954. default:
  955. /* not reached */
  956. break;
  957. }
  958. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  959. &iwe, IW_EV_QUAL_LEN);
  960. }
  961. memset(&iwe, 0, sizeof(iwe));
  962. iwe.cmd = SIOCGIWENCODE;
  963. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  964. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  965. else
  966. iwe.u.data.flags = IW_ENCODE_DISABLED;
  967. iwe.u.data.length = 0;
  968. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  969. &iwe, "");
  970. while (rem >= 2) {
  971. /* invalid data */
  972. if (ie[1] > rem - 2)
  973. break;
  974. switch (ie[0]) {
  975. case WLAN_EID_SSID:
  976. memset(&iwe, 0, sizeof(iwe));
  977. iwe.cmd = SIOCGIWESSID;
  978. iwe.u.data.length = ie[1];
  979. iwe.u.data.flags = 1;
  980. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  981. &iwe, ie + 2);
  982. break;
  983. case WLAN_EID_MESH_ID:
  984. memset(&iwe, 0, sizeof(iwe));
  985. iwe.cmd = SIOCGIWESSID;
  986. iwe.u.data.length = ie[1];
  987. iwe.u.data.flags = 1;
  988. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  989. &iwe, ie + 2);
  990. break;
  991. case WLAN_EID_MESH_CONFIG:
  992. ismesh = true;
  993. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  994. break;
  995. buf = kmalloc(50, GFP_ATOMIC);
  996. if (!buf)
  997. break;
  998. cfg = ie + 2;
  999. memset(&iwe, 0, sizeof(iwe));
  1000. iwe.cmd = IWEVCUSTOM;
  1001. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  1002. "0x%02X", cfg[0]);
  1003. iwe.u.data.length = strlen(buf);
  1004. current_ev = iwe_stream_add_point(info, current_ev,
  1005. end_buf,
  1006. &iwe, buf);
  1007. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  1008. cfg[1]);
  1009. iwe.u.data.length = strlen(buf);
  1010. current_ev = iwe_stream_add_point(info, current_ev,
  1011. end_buf,
  1012. &iwe, buf);
  1013. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  1014. cfg[2]);
  1015. iwe.u.data.length = strlen(buf);
  1016. current_ev = iwe_stream_add_point(info, current_ev,
  1017. end_buf,
  1018. &iwe, buf);
  1019. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  1020. iwe.u.data.length = strlen(buf);
  1021. current_ev = iwe_stream_add_point(info, current_ev,
  1022. end_buf,
  1023. &iwe, buf);
  1024. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  1025. iwe.u.data.length = strlen(buf);
  1026. current_ev = iwe_stream_add_point(info, current_ev,
  1027. end_buf,
  1028. &iwe, buf);
  1029. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  1030. iwe.u.data.length = strlen(buf);
  1031. current_ev = iwe_stream_add_point(info, current_ev,
  1032. end_buf,
  1033. &iwe, buf);
  1034. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1035. iwe.u.data.length = strlen(buf);
  1036. current_ev = iwe_stream_add_point(info, current_ev,
  1037. end_buf,
  1038. &iwe, buf);
  1039. kfree(buf);
  1040. break;
  1041. case WLAN_EID_SUPP_RATES:
  1042. case WLAN_EID_EXT_SUPP_RATES:
  1043. /* display all supported rates in readable format */
  1044. p = current_ev + iwe_stream_lcp_len(info);
  1045. memset(&iwe, 0, sizeof(iwe));
  1046. iwe.cmd = SIOCGIWRATE;
  1047. /* Those two flags are ignored... */
  1048. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1049. for (i = 0; i < ie[1]; i++) {
  1050. iwe.u.bitrate.value =
  1051. ((ie[i + 2] & 0x7f) * 500000);
  1052. p = iwe_stream_add_value(info, current_ev, p,
  1053. end_buf, &iwe, IW_EV_PARAM_LEN);
  1054. }
  1055. current_ev = p;
  1056. break;
  1057. }
  1058. rem -= ie[1] + 2;
  1059. ie += ie[1] + 2;
  1060. }
  1061. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1062. ismesh) {
  1063. memset(&iwe, 0, sizeof(iwe));
  1064. iwe.cmd = SIOCGIWMODE;
  1065. if (ismesh)
  1066. iwe.u.mode = IW_MODE_MESH;
  1067. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1068. iwe.u.mode = IW_MODE_MASTER;
  1069. else
  1070. iwe.u.mode = IW_MODE_ADHOC;
  1071. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  1072. &iwe, IW_EV_UINT_LEN);
  1073. }
  1074. buf = kmalloc(30, GFP_ATOMIC);
  1075. if (buf) {
  1076. memset(&iwe, 0, sizeof(iwe));
  1077. iwe.cmd = IWEVCUSTOM;
  1078. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  1079. iwe.u.data.length = strlen(buf);
  1080. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  1081. &iwe, buf);
  1082. memset(&iwe, 0, sizeof(iwe));
  1083. iwe.cmd = IWEVCUSTOM;
  1084. sprintf(buf, " Last beacon: %ums ago",
  1085. elapsed_jiffies_msecs(bss->ts));
  1086. iwe.u.data.length = strlen(buf);
  1087. current_ev = iwe_stream_add_point(info, current_ev,
  1088. end_buf, &iwe, buf);
  1089. kfree(buf);
  1090. }
  1091. ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
  1092. return current_ev;
  1093. }
  1094. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  1095. struct iw_request_info *info,
  1096. char *buf, size_t len)
  1097. {
  1098. char *current_ev = buf;
  1099. char *end_buf = buf + len;
  1100. struct cfg80211_internal_bss *bss;
  1101. spin_lock_bh(&dev->bss_lock);
  1102. cfg80211_bss_expire(dev);
  1103. list_for_each_entry(bss, &dev->bss_list, list) {
  1104. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1105. spin_unlock_bh(&dev->bss_lock);
  1106. return -E2BIG;
  1107. }
  1108. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  1109. current_ev, end_buf);
  1110. }
  1111. spin_unlock_bh(&dev->bss_lock);
  1112. return current_ev - buf;
  1113. }
  1114. int cfg80211_wext_giwscan(struct net_device *dev,
  1115. struct iw_request_info *info,
  1116. struct iw_point *data, char *extra)
  1117. {
  1118. struct cfg80211_registered_device *rdev;
  1119. int res;
  1120. if (!netif_running(dev))
  1121. return -ENETDOWN;
  1122. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1123. if (IS_ERR(rdev))
  1124. return PTR_ERR(rdev);
  1125. if (rdev->scan_req) {
  1126. res = -EAGAIN;
  1127. goto out;
  1128. }
  1129. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1130. data->length = 0;
  1131. if (res >= 0) {
  1132. data->length = res;
  1133. res = 0;
  1134. }
  1135. out:
  1136. cfg80211_unlock_rdev(rdev);
  1137. return res;
  1138. }
  1139. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  1140. #endif