scan.c 33 KB

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