scan.c 28 KB

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