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. list_del(&bss->list);
  544. dev->bss_generation++;
  545. rb_erase(&bss->rbn, &dev->bss_tree);
  546. spin_unlock_bh(&dev->bss_lock);
  547. kref_put(&bss->ref, bss_release);
  548. }
  549. EXPORT_SYMBOL(cfg80211_unlink_bss);
  550. #ifdef CONFIG_CFG80211_WEXT
  551. int cfg80211_wext_siwscan(struct net_device *dev,
  552. struct iw_request_info *info,
  553. union iwreq_data *wrqu, char *extra)
  554. {
  555. struct cfg80211_registered_device *rdev;
  556. struct wiphy *wiphy;
  557. struct iw_scan_req *wreq = NULL;
  558. struct cfg80211_scan_request *creq = NULL;
  559. int i, err, n_channels = 0;
  560. enum ieee80211_band band;
  561. if (!netif_running(dev))
  562. return -ENETDOWN;
  563. if (wrqu->data.length == sizeof(struct iw_scan_req))
  564. wreq = (struct iw_scan_req *)extra;
  565. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  566. if (IS_ERR(rdev))
  567. return PTR_ERR(rdev);
  568. if (rdev->scan_req) {
  569. err = -EBUSY;
  570. goto out;
  571. }
  572. wiphy = &rdev->wiphy;
  573. /* Determine number of channels, needed to allocate creq */
  574. if (wreq && wreq->num_channels)
  575. n_channels = wreq->num_channels;
  576. else {
  577. for (band = 0; band < IEEE80211_NUM_BANDS; band++)
  578. if (wiphy->bands[band])
  579. n_channels += wiphy->bands[band]->n_channels;
  580. }
  581. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  582. n_channels * sizeof(void *),
  583. GFP_ATOMIC);
  584. if (!creq) {
  585. err = -ENOMEM;
  586. goto out;
  587. }
  588. creq->wiphy = wiphy;
  589. creq->dev = dev;
  590. /* SSIDs come after channels */
  591. creq->ssids = (void *)&creq->channels[n_channels];
  592. creq->n_channels = n_channels;
  593. creq->n_ssids = 1;
  594. /* translate "Scan on frequencies" request */
  595. i = 0;
  596. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  597. int j;
  598. if (!wiphy->bands[band])
  599. continue;
  600. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  601. /* ignore disabled channels */
  602. if (wiphy->bands[band]->channels[j].flags &
  603. IEEE80211_CHAN_DISABLED)
  604. continue;
  605. /* If we have a wireless request structure and the
  606. * wireless request specifies frequencies, then search
  607. * for the matching hardware channel.
  608. */
  609. if (wreq && wreq->num_channels) {
  610. int k;
  611. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  612. for (k = 0; k < wreq->num_channels; k++) {
  613. int wext_freq = cfg80211_wext_freq(wiphy, &wreq->channel_list[k]);
  614. if (wext_freq == wiphy_freq)
  615. goto wext_freq_found;
  616. }
  617. goto wext_freq_not_found;
  618. }
  619. wext_freq_found:
  620. creq->channels[i] = &wiphy->bands[band]->channels[j];
  621. i++;
  622. wext_freq_not_found: ;
  623. }
  624. }
  625. /* No channels found? */
  626. if (!i) {
  627. err = -EINVAL;
  628. goto out;
  629. }
  630. /* Set real number of channels specified in creq->channels[] */
  631. creq->n_channels = i;
  632. /* translate "Scan for SSID" request */
  633. if (wreq) {
  634. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  635. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  636. err = -EINVAL;
  637. goto out;
  638. }
  639. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  640. creq->ssids[0].ssid_len = wreq->essid_len;
  641. }
  642. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  643. creq->n_ssids = 0;
  644. }
  645. rdev->scan_req = creq;
  646. err = rdev->ops->scan(wiphy, dev, creq);
  647. if (err) {
  648. rdev->scan_req = NULL;
  649. /* creq will be freed below */
  650. } else {
  651. nl80211_send_scan_start(rdev, dev);
  652. /* creq now owned by driver */
  653. creq = NULL;
  654. dev_hold(dev);
  655. }
  656. out:
  657. kfree(creq);
  658. cfg80211_unlock_rdev(rdev);
  659. return err;
  660. }
  661. EXPORT_SYMBOL_GPL(cfg80211_wext_siwscan);
  662. static void ieee80211_scan_add_ies(struct iw_request_info *info,
  663. struct cfg80211_bss *bss,
  664. char **current_ev, char *end_buf)
  665. {
  666. u8 *pos, *end, *next;
  667. struct iw_event iwe;
  668. if (!bss->information_elements ||
  669. !bss->len_information_elements)
  670. return;
  671. /*
  672. * If needed, fragment the IEs buffer (at IE boundaries) into short
  673. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  674. */
  675. pos = bss->information_elements;
  676. end = pos + bss->len_information_elements;
  677. while (end - pos > IW_GENERIC_IE_MAX) {
  678. next = pos + 2 + pos[1];
  679. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  680. next = next + 2 + next[1];
  681. memset(&iwe, 0, sizeof(iwe));
  682. iwe.cmd = IWEVGENIE;
  683. iwe.u.data.length = next - pos;
  684. *current_ev = iwe_stream_add_point(info, *current_ev,
  685. end_buf, &iwe, pos);
  686. pos = next;
  687. }
  688. if (end > pos) {
  689. memset(&iwe, 0, sizeof(iwe));
  690. iwe.cmd = IWEVGENIE;
  691. iwe.u.data.length = end - pos;
  692. *current_ev = iwe_stream_add_point(info, *current_ev,
  693. end_buf, &iwe, pos);
  694. }
  695. }
  696. static inline unsigned int elapsed_jiffies_msecs(unsigned long start)
  697. {
  698. unsigned long end = jiffies;
  699. if (end >= start)
  700. return jiffies_to_msecs(end - start);
  701. return jiffies_to_msecs(end + (MAX_JIFFY_OFFSET - start) + 1);
  702. }
  703. static char *
  704. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  705. struct cfg80211_internal_bss *bss, char *current_ev,
  706. char *end_buf)
  707. {
  708. struct iw_event iwe;
  709. u8 *buf, *cfg, *p;
  710. u8 *ie = bss->pub.information_elements;
  711. int rem = bss->pub.len_information_elements, i, sig;
  712. bool ismesh = false;
  713. memset(&iwe, 0, sizeof(iwe));
  714. iwe.cmd = SIOCGIWAP;
  715. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  716. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  717. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  718. IW_EV_ADDR_LEN);
  719. memset(&iwe, 0, sizeof(iwe));
  720. iwe.cmd = SIOCGIWFREQ;
  721. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  722. iwe.u.freq.e = 0;
  723. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  724. IW_EV_FREQ_LEN);
  725. memset(&iwe, 0, sizeof(iwe));
  726. iwe.cmd = SIOCGIWFREQ;
  727. iwe.u.freq.m = bss->pub.channel->center_freq;
  728. iwe.u.freq.e = 6;
  729. current_ev = iwe_stream_add_event(info, current_ev, end_buf, &iwe,
  730. IW_EV_FREQ_LEN);
  731. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  732. memset(&iwe, 0, sizeof(iwe));
  733. iwe.cmd = IWEVQUAL;
  734. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  735. IW_QUAL_NOISE_INVALID |
  736. IW_QUAL_QUAL_UPDATED;
  737. switch (wiphy->signal_type) {
  738. case CFG80211_SIGNAL_TYPE_MBM:
  739. sig = bss->pub.signal / 100;
  740. iwe.u.qual.level = sig;
  741. iwe.u.qual.updated |= IW_QUAL_DBM;
  742. if (sig < -110) /* rather bad */
  743. sig = -110;
  744. else if (sig > -40) /* perfect */
  745. sig = -40;
  746. /* will give a range of 0 .. 70 */
  747. iwe.u.qual.qual = sig + 110;
  748. break;
  749. case CFG80211_SIGNAL_TYPE_UNSPEC:
  750. iwe.u.qual.level = bss->pub.signal;
  751. /* will give range 0 .. 100 */
  752. iwe.u.qual.qual = bss->pub.signal;
  753. break;
  754. default:
  755. /* not reached */
  756. break;
  757. }
  758. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  759. &iwe, IW_EV_QUAL_LEN);
  760. }
  761. memset(&iwe, 0, sizeof(iwe));
  762. iwe.cmd = SIOCGIWENCODE;
  763. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  764. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  765. else
  766. iwe.u.data.flags = IW_ENCODE_DISABLED;
  767. iwe.u.data.length = 0;
  768. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  769. &iwe, "");
  770. while (rem >= 2) {
  771. /* invalid data */
  772. if (ie[1] > rem - 2)
  773. break;
  774. switch (ie[0]) {
  775. case WLAN_EID_SSID:
  776. memset(&iwe, 0, sizeof(iwe));
  777. iwe.cmd = SIOCGIWESSID;
  778. iwe.u.data.length = ie[1];
  779. iwe.u.data.flags = 1;
  780. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  781. &iwe, ie + 2);
  782. break;
  783. case WLAN_EID_MESH_ID:
  784. memset(&iwe, 0, sizeof(iwe));
  785. iwe.cmd = SIOCGIWESSID;
  786. iwe.u.data.length = ie[1];
  787. iwe.u.data.flags = 1;
  788. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  789. &iwe, ie + 2);
  790. break;
  791. case WLAN_EID_MESH_CONFIG:
  792. ismesh = true;
  793. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  794. break;
  795. buf = kmalloc(50, GFP_ATOMIC);
  796. if (!buf)
  797. break;
  798. cfg = ie + 2;
  799. memset(&iwe, 0, sizeof(iwe));
  800. iwe.cmd = IWEVCUSTOM;
  801. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  802. "0x%02X", cfg[0]);
  803. iwe.u.data.length = strlen(buf);
  804. current_ev = iwe_stream_add_point(info, current_ev,
  805. end_buf,
  806. &iwe, buf);
  807. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  808. cfg[1]);
  809. iwe.u.data.length = strlen(buf);
  810. current_ev = iwe_stream_add_point(info, current_ev,
  811. end_buf,
  812. &iwe, buf);
  813. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  814. cfg[2]);
  815. iwe.u.data.length = strlen(buf);
  816. current_ev = iwe_stream_add_point(info, current_ev,
  817. end_buf,
  818. &iwe, buf);
  819. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  820. iwe.u.data.length = strlen(buf);
  821. current_ev = iwe_stream_add_point(info, current_ev,
  822. end_buf,
  823. &iwe, buf);
  824. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  825. iwe.u.data.length = strlen(buf);
  826. current_ev = iwe_stream_add_point(info, current_ev,
  827. end_buf,
  828. &iwe, buf);
  829. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  830. iwe.u.data.length = strlen(buf);
  831. current_ev = iwe_stream_add_point(info, current_ev,
  832. end_buf,
  833. &iwe, buf);
  834. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  835. iwe.u.data.length = strlen(buf);
  836. current_ev = iwe_stream_add_point(info, current_ev,
  837. end_buf,
  838. &iwe, buf);
  839. kfree(buf);
  840. break;
  841. case WLAN_EID_SUPP_RATES:
  842. case WLAN_EID_EXT_SUPP_RATES:
  843. /* display all supported rates in readable format */
  844. p = current_ev + iwe_stream_lcp_len(info);
  845. memset(&iwe, 0, sizeof(iwe));
  846. iwe.cmd = SIOCGIWRATE;
  847. /* Those two flags are ignored... */
  848. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  849. for (i = 0; i < ie[1]; i++) {
  850. iwe.u.bitrate.value =
  851. ((ie[i + 2] & 0x7f) * 500000);
  852. p = iwe_stream_add_value(info, current_ev, p,
  853. end_buf, &iwe, IW_EV_PARAM_LEN);
  854. }
  855. current_ev = p;
  856. break;
  857. }
  858. rem -= ie[1] + 2;
  859. ie += ie[1] + 2;
  860. }
  861. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  862. ismesh) {
  863. memset(&iwe, 0, sizeof(iwe));
  864. iwe.cmd = SIOCGIWMODE;
  865. if (ismesh)
  866. iwe.u.mode = IW_MODE_MESH;
  867. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  868. iwe.u.mode = IW_MODE_MASTER;
  869. else
  870. iwe.u.mode = IW_MODE_ADHOC;
  871. current_ev = iwe_stream_add_event(info, current_ev, end_buf,
  872. &iwe, IW_EV_UINT_LEN);
  873. }
  874. buf = kmalloc(30, GFP_ATOMIC);
  875. if (buf) {
  876. memset(&iwe, 0, sizeof(iwe));
  877. iwe.cmd = IWEVCUSTOM;
  878. sprintf(buf, "tsf=%016llx", (unsigned long long)(bss->pub.tsf));
  879. iwe.u.data.length = strlen(buf);
  880. current_ev = iwe_stream_add_point(info, current_ev, end_buf,
  881. &iwe, buf);
  882. memset(&iwe, 0, sizeof(iwe));
  883. iwe.cmd = IWEVCUSTOM;
  884. sprintf(buf, " Last beacon: %ums ago",
  885. elapsed_jiffies_msecs(bss->ts));
  886. iwe.u.data.length = strlen(buf);
  887. current_ev = iwe_stream_add_point(info, current_ev,
  888. end_buf, &iwe, buf);
  889. kfree(buf);
  890. }
  891. ieee80211_scan_add_ies(info, &bss->pub, &current_ev, end_buf);
  892. return current_ev;
  893. }
  894. static int ieee80211_scan_results(struct cfg80211_registered_device *dev,
  895. struct iw_request_info *info,
  896. char *buf, size_t len)
  897. {
  898. char *current_ev = buf;
  899. char *end_buf = buf + len;
  900. struct cfg80211_internal_bss *bss;
  901. spin_lock_bh(&dev->bss_lock);
  902. cfg80211_bss_expire(dev);
  903. list_for_each_entry(bss, &dev->bss_list, list) {
  904. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  905. spin_unlock_bh(&dev->bss_lock);
  906. return -E2BIG;
  907. }
  908. current_ev = ieee80211_bss(&dev->wiphy, info, bss,
  909. current_ev, end_buf);
  910. }
  911. spin_unlock_bh(&dev->bss_lock);
  912. return current_ev - buf;
  913. }
  914. int cfg80211_wext_giwscan(struct net_device *dev,
  915. struct iw_request_info *info,
  916. struct iw_point *data, char *extra)
  917. {
  918. struct cfg80211_registered_device *rdev;
  919. int res;
  920. if (!netif_running(dev))
  921. return -ENETDOWN;
  922. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  923. if (IS_ERR(rdev))
  924. return PTR_ERR(rdev);
  925. if (rdev->scan_req) {
  926. res = -EAGAIN;
  927. goto out;
  928. }
  929. res = ieee80211_scan_results(rdev, info, extra, data->length);
  930. data->length = 0;
  931. if (res >= 0) {
  932. data->length = res;
  933. res = 0;
  934. }
  935. out:
  936. cfg80211_unlock_rdev(rdev);
  937. return res;
  938. }
  939. EXPORT_SYMBOL_GPL(cfg80211_wext_giwscan);
  940. #endif