scan.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749
  1. /*
  2. * cfg80211 scan result handling
  3. *
  4. * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
  5. * Copyright 2013-2014 Intel Mobile Communications GmbH
  6. */
  7. #include <linux/kernel.h>
  8. #include <linux/slab.h>
  9. #include <linux/module.h>
  10. #include <linux/netdevice.h>
  11. #include <linux/wireless.h>
  12. #include <linux/nl80211.h>
  13. #include <linux/etherdevice.h>
  14. #include <net/arp.h>
  15. #include <net/cfg80211.h>
  16. #include <net/cfg80211-wext.h>
  17. #include <net/iw_handler.h>
  18. #include "core.h"
  19. #include "nl80211.h"
  20. #include "wext-compat.h"
  21. #include "rdev-ops.h"
  22. /**
  23. * DOC: BSS tree/list structure
  24. *
  25. * At the top level, the BSS list is kept in both a list in each
  26. * registered device (@bss_list) as well as an RB-tree for faster
  27. * lookup. In the RB-tree, entries can be looked up using their
  28. * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
  29. * for other BSSes.
  30. *
  31. * Due to the possibility of hidden SSIDs, there's a second level
  32. * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
  33. * The hidden_list connects all BSSes belonging to a single AP
  34. * that has a hidden SSID, and connects beacon and probe response
  35. * entries. For a probe response entry for a hidden SSID, the
  36. * hidden_beacon_bss pointer points to the BSS struct holding the
  37. * beacon's information.
  38. *
  39. * Reference counting is done for all these references except for
  40. * the hidden_list, so that a beacon BSS struct that is otherwise
  41. * not referenced has one reference for being on the bss_list and
  42. * one for each probe response entry that points to it using the
  43. * hidden_beacon_bss pointer. When a BSS struct that has such a
  44. * pointer is get/put, the refcount update is also propagated to
  45. * the referenced struct, this ensure that it cannot get removed
  46. * while somebody is using the probe response version.
  47. *
  48. * Note that the hidden_beacon_bss pointer never changes, due to
  49. * the reference counting. Therefore, no locking is needed for
  50. * it.
  51. *
  52. * Also note that the hidden_beacon_bss pointer is only relevant
  53. * if the driver uses something other than the IEs, e.g. private
  54. * data stored stored in the BSS struct, since the beacon IEs are
  55. * also linked into the probe response struct.
  56. */
  57. /*
  58. * Limit the number of BSS entries stored in mac80211. Each one is
  59. * a bit over 4k at most, so this limits to roughly 4-5M of memory.
  60. * If somebody wants to really attack this though, they'd likely
  61. * use small beacons, and only one type of frame, limiting each of
  62. * the entries to a much smaller size (in order to generate more
  63. * entries in total, so overhead is bigger.)
  64. */
  65. static int bss_entries_limit = 1000;
  66. module_param(bss_entries_limit, int, 0644);
  67. MODULE_PARM_DESC(bss_entries_limit,
  68. "limit to number of scan BSS entries (per wiphy, default 1000)");
  69. #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
  70. static void bss_free(struct cfg80211_internal_bss *bss)
  71. {
  72. struct cfg80211_bss_ies *ies;
  73. if (WARN_ON(atomic_read(&bss->hold)))
  74. return;
  75. ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
  76. if (ies && !bss->pub.hidden_beacon_bss)
  77. kfree_rcu(ies, rcu_head);
  78. ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
  79. if (ies)
  80. kfree_rcu(ies, rcu_head);
  81. /*
  82. * This happens when the module is removed, it doesn't
  83. * really matter any more save for completeness
  84. */
  85. if (!list_empty(&bss->hidden_list))
  86. list_del(&bss->hidden_list);
  87. kfree(bss);
  88. }
  89. static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
  90. struct cfg80211_internal_bss *bss)
  91. {
  92. lockdep_assert_held(&rdev->bss_lock);
  93. bss->refcount++;
  94. if (bss->pub.hidden_beacon_bss) {
  95. bss = container_of(bss->pub.hidden_beacon_bss,
  96. struct cfg80211_internal_bss,
  97. pub);
  98. bss->refcount++;
  99. }
  100. }
  101. static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
  102. struct cfg80211_internal_bss *bss)
  103. {
  104. lockdep_assert_held(&rdev->bss_lock);
  105. if (bss->pub.hidden_beacon_bss) {
  106. struct cfg80211_internal_bss *hbss;
  107. hbss = container_of(bss->pub.hidden_beacon_bss,
  108. struct cfg80211_internal_bss,
  109. pub);
  110. hbss->refcount--;
  111. if (hbss->refcount == 0)
  112. bss_free(hbss);
  113. }
  114. bss->refcount--;
  115. if (bss->refcount == 0)
  116. bss_free(bss);
  117. }
  118. static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
  119. struct cfg80211_internal_bss *bss)
  120. {
  121. lockdep_assert_held(&rdev->bss_lock);
  122. if (!list_empty(&bss->hidden_list)) {
  123. /*
  124. * don't remove the beacon entry if it has
  125. * probe responses associated with it
  126. */
  127. if (!bss->pub.hidden_beacon_bss)
  128. return false;
  129. /*
  130. * if it's a probe response entry break its
  131. * link to the other entries in the group
  132. */
  133. list_del_init(&bss->hidden_list);
  134. }
  135. list_del_init(&bss->list);
  136. rb_erase(&bss->rbn, &rdev->bss_tree);
  137. rdev->bss_entries--;
  138. WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
  139. "rdev bss entries[%d]/list[empty:%d] corruption\n",
  140. rdev->bss_entries, list_empty(&rdev->bss_list));
  141. bss_ref_put(rdev, bss);
  142. return true;
  143. }
  144. static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
  145. unsigned long expire_time)
  146. {
  147. struct cfg80211_internal_bss *bss, *tmp;
  148. bool expired = false;
  149. lockdep_assert_held(&rdev->bss_lock);
  150. list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
  151. if (atomic_read(&bss->hold))
  152. continue;
  153. if (!time_after(expire_time, bss->ts))
  154. continue;
  155. if (__cfg80211_unlink_bss(rdev, bss))
  156. expired = true;
  157. }
  158. if (expired)
  159. rdev->bss_generation++;
  160. }
  161. static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
  162. {
  163. struct cfg80211_internal_bss *bss, *oldest = NULL;
  164. bool ret;
  165. lockdep_assert_held(&rdev->bss_lock);
  166. list_for_each_entry(bss, &rdev->bss_list, list) {
  167. if (atomic_read(&bss->hold))
  168. continue;
  169. if (!list_empty(&bss->hidden_list) &&
  170. !bss->pub.hidden_beacon_bss)
  171. continue;
  172. if (oldest && time_before(oldest->ts, bss->ts))
  173. continue;
  174. oldest = bss;
  175. }
  176. if (WARN_ON(!oldest))
  177. return false;
  178. /*
  179. * The callers make sure to increase rdev->bss_generation if anything
  180. * gets removed (and a new entry added), so there's no need to also do
  181. * it here.
  182. */
  183. ret = __cfg80211_unlink_bss(rdev, oldest);
  184. WARN_ON(!ret);
  185. return ret;
  186. }
  187. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
  188. bool send_message)
  189. {
  190. struct cfg80211_scan_request *request;
  191. struct wireless_dev *wdev;
  192. struct sk_buff *msg;
  193. #ifdef CONFIG_CFG80211_WEXT
  194. union iwreq_data wrqu;
  195. #endif
  196. ASSERT_RTNL();
  197. if (rdev->scan_msg) {
  198. nl80211_send_scan_result(rdev, rdev->scan_msg);
  199. rdev->scan_msg = NULL;
  200. return;
  201. }
  202. request = rdev->scan_req;
  203. if (!request)
  204. return;
  205. wdev = request->wdev;
  206. /*
  207. * This must be before sending the other events!
  208. * Otherwise, wpa_supplicant gets completely confused with
  209. * wext events.
  210. */
  211. if (wdev->netdev)
  212. cfg80211_sme_scan_done(wdev->netdev);
  213. if (!request->aborted &&
  214. request->flags & NL80211_SCAN_FLAG_FLUSH) {
  215. /* flush entries from previous scans */
  216. spin_lock_bh(&rdev->bss_lock);
  217. __cfg80211_bss_expire(rdev, request->scan_start);
  218. spin_unlock_bh(&rdev->bss_lock);
  219. }
  220. msg = nl80211_build_scan_msg(rdev, wdev, request->aborted);
  221. #ifdef CONFIG_CFG80211_WEXT
  222. if (wdev->netdev && !request->aborted) {
  223. memset(&wrqu, 0, sizeof(wrqu));
  224. wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
  225. }
  226. #endif
  227. if (wdev->netdev)
  228. dev_put(wdev->netdev);
  229. rdev->scan_req = NULL;
  230. kfree(request);
  231. if (!send_message)
  232. rdev->scan_msg = msg;
  233. else
  234. nl80211_send_scan_result(rdev, msg);
  235. }
  236. void __cfg80211_scan_done(struct work_struct *wk)
  237. {
  238. struct cfg80211_registered_device *rdev;
  239. rdev = container_of(wk, struct cfg80211_registered_device,
  240. scan_done_wk);
  241. rtnl_lock();
  242. ___cfg80211_scan_done(rdev, true);
  243. rtnl_unlock();
  244. }
  245. void cfg80211_scan_done(struct cfg80211_scan_request *request, bool aborted)
  246. {
  247. trace_cfg80211_scan_done(request, aborted);
  248. WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
  249. request->aborted = aborted;
  250. request->notified = true;
  251. queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
  252. }
  253. EXPORT_SYMBOL(cfg80211_scan_done);
  254. void __cfg80211_sched_scan_results(struct work_struct *wk)
  255. {
  256. struct cfg80211_registered_device *rdev;
  257. struct cfg80211_sched_scan_request *request;
  258. rdev = container_of(wk, struct cfg80211_registered_device,
  259. sched_scan_results_wk);
  260. rtnl_lock();
  261. request = rtnl_dereference(rdev->sched_scan_req);
  262. /* we don't have sched_scan_req anymore if the scan is stopping */
  263. if (request) {
  264. if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
  265. /* flush entries from previous scans */
  266. spin_lock_bh(&rdev->bss_lock);
  267. __cfg80211_bss_expire(rdev, request->scan_start);
  268. spin_unlock_bh(&rdev->bss_lock);
  269. request->scan_start = jiffies;
  270. }
  271. nl80211_send_sched_scan_results(rdev, request->dev);
  272. }
  273. rtnl_unlock();
  274. }
  275. void cfg80211_sched_scan_results(struct wiphy *wiphy)
  276. {
  277. trace_cfg80211_sched_scan_results(wiphy);
  278. /* ignore if we're not scanning */
  279. if (rcu_access_pointer(wiphy_to_rdev(wiphy)->sched_scan_req))
  280. queue_work(cfg80211_wq,
  281. &wiphy_to_rdev(wiphy)->sched_scan_results_wk);
  282. }
  283. EXPORT_SYMBOL(cfg80211_sched_scan_results);
  284. void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy)
  285. {
  286. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  287. ASSERT_RTNL();
  288. trace_cfg80211_sched_scan_stopped(wiphy);
  289. __cfg80211_stop_sched_scan(rdev, true);
  290. }
  291. EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
  292. void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
  293. {
  294. rtnl_lock();
  295. cfg80211_sched_scan_stopped_rtnl(wiphy);
  296. rtnl_unlock();
  297. }
  298. EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
  299. int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
  300. bool driver_initiated)
  301. {
  302. struct cfg80211_sched_scan_request *sched_scan_req;
  303. struct net_device *dev;
  304. ASSERT_RTNL();
  305. if (!rdev->sched_scan_req)
  306. return -ENOENT;
  307. sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
  308. dev = sched_scan_req->dev;
  309. if (!driver_initiated) {
  310. int err = rdev_sched_scan_stop(rdev, dev);
  311. if (err)
  312. return err;
  313. }
  314. nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
  315. RCU_INIT_POINTER(rdev->sched_scan_req, NULL);
  316. kfree_rcu(sched_scan_req, rcu_head);
  317. return 0;
  318. }
  319. void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
  320. unsigned long age_secs)
  321. {
  322. struct cfg80211_internal_bss *bss;
  323. unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
  324. spin_lock_bh(&rdev->bss_lock);
  325. list_for_each_entry(bss, &rdev->bss_list, list)
  326. bss->ts -= age_jiffies;
  327. spin_unlock_bh(&rdev->bss_lock);
  328. }
  329. void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
  330. {
  331. __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
  332. }
  333. const u8 *cfg80211_find_ie(u8 eid, const u8 *ies, int len)
  334. {
  335. while (len > 2 && ies[0] != eid) {
  336. len -= ies[1] + 2;
  337. ies += ies[1] + 2;
  338. }
  339. if (len < 2)
  340. return NULL;
  341. if (len < 2 + ies[1])
  342. return NULL;
  343. return ies;
  344. }
  345. EXPORT_SYMBOL(cfg80211_find_ie);
  346. const u8 *cfg80211_find_vendor_ie(unsigned int oui, u8 oui_type,
  347. const u8 *ies, int len)
  348. {
  349. struct ieee80211_vendor_ie *ie;
  350. const u8 *pos = ies, *end = ies + len;
  351. int ie_oui;
  352. while (pos < end) {
  353. pos = cfg80211_find_ie(WLAN_EID_VENDOR_SPECIFIC, pos,
  354. end - pos);
  355. if (!pos)
  356. return NULL;
  357. ie = (struct ieee80211_vendor_ie *)pos;
  358. /* make sure we can access ie->len */
  359. BUILD_BUG_ON(offsetof(struct ieee80211_vendor_ie, len) != 1);
  360. if (ie->len < sizeof(*ie))
  361. goto cont;
  362. ie_oui = ie->oui[0] << 16 | ie->oui[1] << 8 | ie->oui[2];
  363. if (ie_oui == oui && ie->oui_type == oui_type)
  364. return pos;
  365. cont:
  366. pos += 2 + ie->len;
  367. }
  368. return NULL;
  369. }
  370. EXPORT_SYMBOL(cfg80211_find_vendor_ie);
  371. static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
  372. const u8 *ssid, size_t ssid_len)
  373. {
  374. const struct cfg80211_bss_ies *ies;
  375. const u8 *ssidie;
  376. if (bssid && !ether_addr_equal(a->bssid, bssid))
  377. return false;
  378. if (!ssid)
  379. return true;
  380. ies = rcu_access_pointer(a->ies);
  381. if (!ies)
  382. return false;
  383. ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  384. if (!ssidie)
  385. return false;
  386. if (ssidie[1] != ssid_len)
  387. return false;
  388. return memcmp(ssidie + 2, ssid, ssid_len) == 0;
  389. }
  390. /**
  391. * enum bss_compare_mode - BSS compare mode
  392. * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
  393. * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
  394. * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
  395. */
  396. enum bss_compare_mode {
  397. BSS_CMP_REGULAR,
  398. BSS_CMP_HIDE_ZLEN,
  399. BSS_CMP_HIDE_NUL,
  400. };
  401. static int cmp_bss(struct cfg80211_bss *a,
  402. struct cfg80211_bss *b,
  403. enum bss_compare_mode mode)
  404. {
  405. const struct cfg80211_bss_ies *a_ies, *b_ies;
  406. const u8 *ie1 = NULL;
  407. const u8 *ie2 = NULL;
  408. int i, r;
  409. if (a->channel != b->channel)
  410. return b->channel->center_freq - a->channel->center_freq;
  411. a_ies = rcu_access_pointer(a->ies);
  412. if (!a_ies)
  413. return -1;
  414. b_ies = rcu_access_pointer(b->ies);
  415. if (!b_ies)
  416. return 1;
  417. if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
  418. ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  419. a_ies->data, a_ies->len);
  420. if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
  421. ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  422. b_ies->data, b_ies->len);
  423. if (ie1 && ie2) {
  424. int mesh_id_cmp;
  425. if (ie1[1] == ie2[1])
  426. mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  427. else
  428. mesh_id_cmp = ie2[1] - ie1[1];
  429. ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  430. a_ies->data, a_ies->len);
  431. ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  432. b_ies->data, b_ies->len);
  433. if (ie1 && ie2) {
  434. if (mesh_id_cmp)
  435. return mesh_id_cmp;
  436. if (ie1[1] != ie2[1])
  437. return ie2[1] - ie1[1];
  438. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  439. }
  440. }
  441. r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
  442. if (r)
  443. return r;
  444. ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
  445. ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
  446. if (!ie1 && !ie2)
  447. return 0;
  448. /*
  449. * Note that with "hide_ssid", the function returns a match if
  450. * the already-present BSS ("b") is a hidden SSID beacon for
  451. * the new BSS ("a").
  452. */
  453. /* sort missing IE before (left of) present IE */
  454. if (!ie1)
  455. return -1;
  456. if (!ie2)
  457. return 1;
  458. switch (mode) {
  459. case BSS_CMP_HIDE_ZLEN:
  460. /*
  461. * In ZLEN mode we assume the BSS entry we're
  462. * looking for has a zero-length SSID. So if
  463. * the one we're looking at right now has that,
  464. * return 0. Otherwise, return the difference
  465. * in length, but since we're looking for the
  466. * 0-length it's really equivalent to returning
  467. * the length of the one we're looking at.
  468. *
  469. * No content comparison is needed as we assume
  470. * the content length is zero.
  471. */
  472. return ie2[1];
  473. case BSS_CMP_REGULAR:
  474. default:
  475. /* sort by length first, then by contents */
  476. if (ie1[1] != ie2[1])
  477. return ie2[1] - ie1[1];
  478. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  479. case BSS_CMP_HIDE_NUL:
  480. if (ie1[1] != ie2[1])
  481. return ie2[1] - ie1[1];
  482. /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
  483. for (i = 0; i < ie2[1]; i++)
  484. if (ie2[i + 2])
  485. return -1;
  486. return 0;
  487. }
  488. }
  489. static bool cfg80211_bss_type_match(u16 capability,
  490. enum ieee80211_band band,
  491. enum ieee80211_bss_type bss_type)
  492. {
  493. bool ret = true;
  494. u16 mask, val;
  495. if (bss_type == IEEE80211_BSS_TYPE_ANY)
  496. return ret;
  497. if (band == IEEE80211_BAND_60GHZ) {
  498. mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
  499. switch (bss_type) {
  500. case IEEE80211_BSS_TYPE_ESS:
  501. val = WLAN_CAPABILITY_DMG_TYPE_AP;
  502. break;
  503. case IEEE80211_BSS_TYPE_PBSS:
  504. val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
  505. break;
  506. case IEEE80211_BSS_TYPE_IBSS:
  507. val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
  508. break;
  509. default:
  510. return false;
  511. }
  512. } else {
  513. mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
  514. switch (bss_type) {
  515. case IEEE80211_BSS_TYPE_ESS:
  516. val = WLAN_CAPABILITY_ESS;
  517. break;
  518. case IEEE80211_BSS_TYPE_IBSS:
  519. val = WLAN_CAPABILITY_IBSS;
  520. break;
  521. case IEEE80211_BSS_TYPE_MBSS:
  522. val = 0;
  523. break;
  524. default:
  525. return false;
  526. }
  527. }
  528. ret = ((capability & mask) == val);
  529. return ret;
  530. }
  531. /* Returned bss is reference counted and must be cleaned up appropriately. */
  532. struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
  533. struct ieee80211_channel *channel,
  534. const u8 *bssid,
  535. const u8 *ssid, size_t ssid_len,
  536. enum ieee80211_bss_type bss_type,
  537. enum ieee80211_privacy privacy)
  538. {
  539. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  540. struct cfg80211_internal_bss *bss, *res = NULL;
  541. unsigned long now = jiffies;
  542. int bss_privacy;
  543. trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
  544. privacy);
  545. spin_lock_bh(&rdev->bss_lock);
  546. list_for_each_entry(bss, &rdev->bss_list, list) {
  547. if (!cfg80211_bss_type_match(bss->pub.capability,
  548. bss->pub.channel->band, bss_type))
  549. continue;
  550. bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
  551. if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
  552. (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
  553. continue;
  554. if (channel && bss->pub.channel != channel)
  555. continue;
  556. if (!is_valid_ether_addr(bss->pub.bssid))
  557. continue;
  558. /* Don't get expired BSS structs */
  559. if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
  560. !atomic_read(&bss->hold))
  561. continue;
  562. if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
  563. res = bss;
  564. bss_ref_get(rdev, res);
  565. break;
  566. }
  567. }
  568. spin_unlock_bh(&rdev->bss_lock);
  569. if (!res)
  570. return NULL;
  571. trace_cfg80211_return_bss(&res->pub);
  572. return &res->pub;
  573. }
  574. EXPORT_SYMBOL(cfg80211_get_bss);
  575. static void rb_insert_bss(struct cfg80211_registered_device *rdev,
  576. struct cfg80211_internal_bss *bss)
  577. {
  578. struct rb_node **p = &rdev->bss_tree.rb_node;
  579. struct rb_node *parent = NULL;
  580. struct cfg80211_internal_bss *tbss;
  581. int cmp;
  582. while (*p) {
  583. parent = *p;
  584. tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
  585. cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
  586. if (WARN_ON(!cmp)) {
  587. /* will sort of leak this BSS */
  588. return;
  589. }
  590. if (cmp < 0)
  591. p = &(*p)->rb_left;
  592. else
  593. p = &(*p)->rb_right;
  594. }
  595. rb_link_node(&bss->rbn, parent, p);
  596. rb_insert_color(&bss->rbn, &rdev->bss_tree);
  597. }
  598. static struct cfg80211_internal_bss *
  599. rb_find_bss(struct cfg80211_registered_device *rdev,
  600. struct cfg80211_internal_bss *res,
  601. enum bss_compare_mode mode)
  602. {
  603. struct rb_node *n = rdev->bss_tree.rb_node;
  604. struct cfg80211_internal_bss *bss;
  605. int r;
  606. while (n) {
  607. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  608. r = cmp_bss(&res->pub, &bss->pub, mode);
  609. if (r == 0)
  610. return bss;
  611. else if (r < 0)
  612. n = n->rb_left;
  613. else
  614. n = n->rb_right;
  615. }
  616. return NULL;
  617. }
  618. static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
  619. struct cfg80211_internal_bss *new)
  620. {
  621. const struct cfg80211_bss_ies *ies;
  622. struct cfg80211_internal_bss *bss;
  623. const u8 *ie;
  624. int i, ssidlen;
  625. u8 fold = 0;
  626. u32 n_entries = 0;
  627. ies = rcu_access_pointer(new->pub.beacon_ies);
  628. if (WARN_ON(!ies))
  629. return false;
  630. ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  631. if (!ie) {
  632. /* nothing to do */
  633. return true;
  634. }
  635. ssidlen = ie[1];
  636. for (i = 0; i < ssidlen; i++)
  637. fold |= ie[2 + i];
  638. if (fold) {
  639. /* not a hidden SSID */
  640. return true;
  641. }
  642. /* This is the bad part ... */
  643. list_for_each_entry(bss, &rdev->bss_list, list) {
  644. /*
  645. * we're iterating all the entries anyway, so take the
  646. * opportunity to validate the list length accounting
  647. */
  648. n_entries++;
  649. if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
  650. continue;
  651. if (bss->pub.channel != new->pub.channel)
  652. continue;
  653. if (bss->pub.scan_width != new->pub.scan_width)
  654. continue;
  655. if (rcu_access_pointer(bss->pub.beacon_ies))
  656. continue;
  657. ies = rcu_access_pointer(bss->pub.ies);
  658. if (!ies)
  659. continue;
  660. ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  661. if (!ie)
  662. continue;
  663. if (ssidlen && ie[1] != ssidlen)
  664. continue;
  665. if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
  666. continue;
  667. if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
  668. list_del(&bss->hidden_list);
  669. /* combine them */
  670. list_add(&bss->hidden_list, &new->hidden_list);
  671. bss->pub.hidden_beacon_bss = &new->pub;
  672. new->refcount += bss->refcount;
  673. rcu_assign_pointer(bss->pub.beacon_ies,
  674. new->pub.beacon_ies);
  675. }
  676. WARN_ONCE(n_entries != rdev->bss_entries,
  677. "rdev bss entries[%d]/list[len:%d] corruption\n",
  678. rdev->bss_entries, n_entries);
  679. return true;
  680. }
  681. /* Returned bss is reference counted and must be cleaned up appropriately. */
  682. static struct cfg80211_internal_bss *
  683. cfg80211_bss_update(struct cfg80211_registered_device *rdev,
  684. struct cfg80211_internal_bss *tmp,
  685. bool signal_valid)
  686. {
  687. struct cfg80211_internal_bss *found = NULL;
  688. if (WARN_ON(!tmp->pub.channel))
  689. return NULL;
  690. tmp->ts = jiffies;
  691. spin_lock_bh(&rdev->bss_lock);
  692. if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
  693. spin_unlock_bh(&rdev->bss_lock);
  694. return NULL;
  695. }
  696. found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
  697. if (found) {
  698. /* Update IEs */
  699. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  700. const struct cfg80211_bss_ies *old;
  701. old = rcu_access_pointer(found->pub.proberesp_ies);
  702. rcu_assign_pointer(found->pub.proberesp_ies,
  703. tmp->pub.proberesp_ies);
  704. /* Override possible earlier Beacon frame IEs */
  705. rcu_assign_pointer(found->pub.ies,
  706. tmp->pub.proberesp_ies);
  707. if (old)
  708. kfree_rcu((struct cfg80211_bss_ies *)old,
  709. rcu_head);
  710. } else if (rcu_access_pointer(tmp->pub.beacon_ies)) {
  711. const struct cfg80211_bss_ies *old;
  712. struct cfg80211_internal_bss *bss;
  713. if (found->pub.hidden_beacon_bss &&
  714. !list_empty(&found->hidden_list)) {
  715. const struct cfg80211_bss_ies *f;
  716. /*
  717. * The found BSS struct is one of the probe
  718. * response members of a group, but we're
  719. * receiving a beacon (beacon_ies in the tmp
  720. * bss is used). This can only mean that the
  721. * AP changed its beacon from not having an
  722. * SSID to showing it, which is confusing so
  723. * drop this information.
  724. */
  725. f = rcu_access_pointer(tmp->pub.beacon_ies);
  726. kfree_rcu((struct cfg80211_bss_ies *)f,
  727. rcu_head);
  728. goto drop;
  729. }
  730. old = rcu_access_pointer(found->pub.beacon_ies);
  731. rcu_assign_pointer(found->pub.beacon_ies,
  732. tmp->pub.beacon_ies);
  733. /* Override IEs if they were from a beacon before */
  734. if (old == rcu_access_pointer(found->pub.ies))
  735. rcu_assign_pointer(found->pub.ies,
  736. tmp->pub.beacon_ies);
  737. /* Assign beacon IEs to all sub entries */
  738. list_for_each_entry(bss, &found->hidden_list,
  739. hidden_list) {
  740. const struct cfg80211_bss_ies *ies;
  741. ies = rcu_access_pointer(bss->pub.beacon_ies);
  742. WARN_ON(ies != old);
  743. rcu_assign_pointer(bss->pub.beacon_ies,
  744. tmp->pub.beacon_ies);
  745. }
  746. if (old)
  747. kfree_rcu((struct cfg80211_bss_ies *)old,
  748. rcu_head);
  749. }
  750. found->pub.beacon_interval = tmp->pub.beacon_interval;
  751. /*
  752. * don't update the signal if beacon was heard on
  753. * adjacent channel.
  754. */
  755. if (signal_valid)
  756. found->pub.signal = tmp->pub.signal;
  757. found->pub.capability = tmp->pub.capability;
  758. found->ts = tmp->ts;
  759. found->ts_boottime = tmp->ts_boottime;
  760. } else {
  761. struct cfg80211_internal_bss *new;
  762. struct cfg80211_internal_bss *hidden;
  763. struct cfg80211_bss_ies *ies;
  764. /*
  765. * create a copy -- the "res" variable that is passed in
  766. * is allocated on the stack since it's not needed in the
  767. * more common case of an update
  768. */
  769. new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
  770. GFP_ATOMIC);
  771. if (!new) {
  772. ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
  773. if (ies)
  774. kfree_rcu(ies, rcu_head);
  775. ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
  776. if (ies)
  777. kfree_rcu(ies, rcu_head);
  778. goto drop;
  779. }
  780. memcpy(new, tmp, sizeof(*new));
  781. new->refcount = 1;
  782. INIT_LIST_HEAD(&new->hidden_list);
  783. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  784. hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
  785. if (!hidden)
  786. hidden = rb_find_bss(rdev, tmp,
  787. BSS_CMP_HIDE_NUL);
  788. if (hidden) {
  789. new->pub.hidden_beacon_bss = &hidden->pub;
  790. list_add(&new->hidden_list,
  791. &hidden->hidden_list);
  792. hidden->refcount++;
  793. rcu_assign_pointer(new->pub.beacon_ies,
  794. hidden->pub.beacon_ies);
  795. }
  796. } else {
  797. /*
  798. * Ok so we found a beacon, and don't have an entry. If
  799. * it's a beacon with hidden SSID, we might be in for an
  800. * expensive search for any probe responses that should
  801. * be grouped with this beacon for updates ...
  802. */
  803. if (!cfg80211_combine_bsses(rdev, new)) {
  804. kfree(new);
  805. goto drop;
  806. }
  807. }
  808. if (rdev->bss_entries >= bss_entries_limit &&
  809. !cfg80211_bss_expire_oldest(rdev)) {
  810. kfree(new);
  811. goto drop;
  812. }
  813. list_add_tail(&new->list, &rdev->bss_list);
  814. rdev->bss_entries++;
  815. rb_insert_bss(rdev, new);
  816. found = new;
  817. }
  818. rdev->bss_generation++;
  819. bss_ref_get(rdev, found);
  820. spin_unlock_bh(&rdev->bss_lock);
  821. return found;
  822. drop:
  823. spin_unlock_bh(&rdev->bss_lock);
  824. return NULL;
  825. }
  826. static struct ieee80211_channel *
  827. cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
  828. struct ieee80211_channel *channel)
  829. {
  830. const u8 *tmp;
  831. u32 freq;
  832. int channel_number = -1;
  833. tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
  834. if (tmp && tmp[1] == 1) {
  835. channel_number = tmp[2];
  836. } else {
  837. tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
  838. if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
  839. struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
  840. channel_number = htop->primary_chan;
  841. }
  842. }
  843. if (channel_number < 0)
  844. return channel;
  845. freq = ieee80211_channel_to_frequency(channel_number, channel->band);
  846. channel = ieee80211_get_channel(wiphy, freq);
  847. if (!channel)
  848. return NULL;
  849. if (channel->flags & IEEE80211_CHAN_DISABLED)
  850. return NULL;
  851. return channel;
  852. }
  853. /* Returned bss is reference counted and must be cleaned up appropriately. */
  854. struct cfg80211_bss *
  855. cfg80211_inform_bss_data(struct wiphy *wiphy,
  856. struct cfg80211_inform_bss *data,
  857. enum cfg80211_bss_frame_type ftype,
  858. const u8 *bssid, u64 tsf, u16 capability,
  859. u16 beacon_interval, const u8 *ie, size_t ielen,
  860. gfp_t gfp)
  861. {
  862. struct cfg80211_bss_ies *ies;
  863. struct ieee80211_channel *channel;
  864. struct cfg80211_internal_bss tmp = {}, *res;
  865. int bss_type;
  866. bool signal_valid;
  867. if (WARN_ON(!wiphy))
  868. return NULL;
  869. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  870. (data->signal < 0 || data->signal > 100)))
  871. return NULL;
  872. channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan);
  873. if (!channel)
  874. return NULL;
  875. memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
  876. tmp.pub.channel = channel;
  877. tmp.pub.scan_width = data->scan_width;
  878. tmp.pub.signal = data->signal;
  879. tmp.pub.beacon_interval = beacon_interval;
  880. tmp.pub.capability = capability;
  881. tmp.ts_boottime = data->boottime_ns;
  882. /*
  883. * If we do not know here whether the IEs are from a Beacon or Probe
  884. * Response frame, we need to pick one of the options and only use it
  885. * with the driver that does not provide the full Beacon/Probe Response
  886. * frame. Use Beacon frame pointer to avoid indicating that this should
  887. * override the IEs pointer should we have received an earlier
  888. * indication of Probe Response data.
  889. */
  890. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  891. if (!ies)
  892. return NULL;
  893. ies->len = ielen;
  894. ies->tsf = tsf;
  895. ies->from_beacon = false;
  896. memcpy(ies->data, ie, ielen);
  897. switch (ftype) {
  898. case CFG80211_BSS_FTYPE_BEACON:
  899. ies->from_beacon = true;
  900. /* fall through to assign */
  901. case CFG80211_BSS_FTYPE_UNKNOWN:
  902. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  903. break;
  904. case CFG80211_BSS_FTYPE_PRESP:
  905. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  906. break;
  907. }
  908. rcu_assign_pointer(tmp.pub.ies, ies);
  909. signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
  910. wiphy->max_adj_channel_rssi_comp;
  911. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
  912. if (!res)
  913. return NULL;
  914. if (channel->band == IEEE80211_BAND_60GHZ) {
  915. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  916. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  917. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  918. regulatory_hint_found_beacon(wiphy, channel, gfp);
  919. } else {
  920. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  921. regulatory_hint_found_beacon(wiphy, channel, gfp);
  922. }
  923. trace_cfg80211_return_bss(&res->pub);
  924. /* cfg80211_bss_update gives us a referenced result */
  925. return &res->pub;
  926. }
  927. EXPORT_SYMBOL(cfg80211_inform_bss_data);
  928. /* cfg80211_inform_bss_width_frame helper */
  929. struct cfg80211_bss *
  930. cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
  931. struct cfg80211_inform_bss *data,
  932. struct ieee80211_mgmt *mgmt, size_t len,
  933. gfp_t gfp)
  934. {
  935. struct cfg80211_internal_bss tmp = {}, *res;
  936. struct cfg80211_bss_ies *ies;
  937. struct ieee80211_channel *channel;
  938. bool signal_valid;
  939. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  940. u.probe_resp.variable);
  941. int bss_type;
  942. BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
  943. offsetof(struct ieee80211_mgmt, u.beacon.variable));
  944. trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
  945. if (WARN_ON(!mgmt))
  946. return NULL;
  947. if (WARN_ON(!wiphy))
  948. return NULL;
  949. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  950. (data->signal < 0 || data->signal > 100)))
  951. return NULL;
  952. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  953. return NULL;
  954. channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
  955. ielen, data->chan);
  956. if (!channel)
  957. return NULL;
  958. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  959. if (!ies)
  960. return NULL;
  961. ies->len = ielen;
  962. ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  963. ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
  964. memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
  965. if (ieee80211_is_probe_resp(mgmt->frame_control))
  966. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  967. else
  968. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  969. rcu_assign_pointer(tmp.pub.ies, ies);
  970. memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
  971. tmp.pub.channel = channel;
  972. tmp.pub.scan_width = data->scan_width;
  973. tmp.pub.signal = data->signal;
  974. tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  975. tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  976. tmp.ts_boottime = data->boottime_ns;
  977. signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
  978. wiphy->max_adj_channel_rssi_comp;
  979. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
  980. if (!res)
  981. return NULL;
  982. if (channel->band == IEEE80211_BAND_60GHZ) {
  983. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  984. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  985. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  986. regulatory_hint_found_beacon(wiphy, channel, gfp);
  987. } else {
  988. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  989. regulatory_hint_found_beacon(wiphy, channel, gfp);
  990. }
  991. trace_cfg80211_return_bss(&res->pub);
  992. /* cfg80211_bss_update gives us a referenced result */
  993. return &res->pub;
  994. }
  995. EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
  996. void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  997. {
  998. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  999. struct cfg80211_internal_bss *bss;
  1000. if (!pub)
  1001. return;
  1002. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1003. spin_lock_bh(&rdev->bss_lock);
  1004. bss_ref_get(rdev, bss);
  1005. spin_unlock_bh(&rdev->bss_lock);
  1006. }
  1007. EXPORT_SYMBOL(cfg80211_ref_bss);
  1008. void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1009. {
  1010. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1011. struct cfg80211_internal_bss *bss;
  1012. if (!pub)
  1013. return;
  1014. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1015. spin_lock_bh(&rdev->bss_lock);
  1016. bss_ref_put(rdev, bss);
  1017. spin_unlock_bh(&rdev->bss_lock);
  1018. }
  1019. EXPORT_SYMBOL(cfg80211_put_bss);
  1020. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1021. {
  1022. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1023. struct cfg80211_internal_bss *bss;
  1024. if (WARN_ON(!pub))
  1025. return;
  1026. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1027. spin_lock_bh(&rdev->bss_lock);
  1028. if (!list_empty(&bss->list)) {
  1029. if (__cfg80211_unlink_bss(rdev, bss))
  1030. rdev->bss_generation++;
  1031. }
  1032. spin_unlock_bh(&rdev->bss_lock);
  1033. }
  1034. EXPORT_SYMBOL(cfg80211_unlink_bss);
  1035. #ifdef CONFIG_CFG80211_WEXT
  1036. static struct cfg80211_registered_device *
  1037. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
  1038. {
  1039. struct cfg80211_registered_device *rdev;
  1040. struct net_device *dev;
  1041. ASSERT_RTNL();
  1042. dev = dev_get_by_index(net, ifindex);
  1043. if (!dev)
  1044. return ERR_PTR(-ENODEV);
  1045. if (dev->ieee80211_ptr)
  1046. rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
  1047. else
  1048. rdev = ERR_PTR(-ENODEV);
  1049. dev_put(dev);
  1050. return rdev;
  1051. }
  1052. int cfg80211_wext_siwscan(struct net_device *dev,
  1053. struct iw_request_info *info,
  1054. union iwreq_data *wrqu, char *extra)
  1055. {
  1056. struct cfg80211_registered_device *rdev;
  1057. struct wiphy *wiphy;
  1058. struct iw_scan_req *wreq = NULL;
  1059. struct cfg80211_scan_request *creq = NULL;
  1060. int i, err, n_channels = 0;
  1061. enum ieee80211_band band;
  1062. if (!netif_running(dev))
  1063. return -ENETDOWN;
  1064. if (wrqu->data.length == sizeof(struct iw_scan_req))
  1065. wreq = (struct iw_scan_req *)extra;
  1066. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1067. if (IS_ERR(rdev))
  1068. return PTR_ERR(rdev);
  1069. if (rdev->scan_req || rdev->scan_msg) {
  1070. err = -EBUSY;
  1071. goto out;
  1072. }
  1073. wiphy = &rdev->wiphy;
  1074. /* Determine number of channels, needed to allocate creq */
  1075. if (wreq && wreq->num_channels)
  1076. n_channels = wreq->num_channels;
  1077. else
  1078. n_channels = ieee80211_get_num_supported_channels(wiphy);
  1079. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  1080. n_channels * sizeof(void *),
  1081. GFP_ATOMIC);
  1082. if (!creq) {
  1083. err = -ENOMEM;
  1084. goto out;
  1085. }
  1086. creq->wiphy = wiphy;
  1087. creq->wdev = dev->ieee80211_ptr;
  1088. /* SSIDs come after channels */
  1089. creq->ssids = (void *)&creq->channels[n_channels];
  1090. creq->n_channels = n_channels;
  1091. creq->n_ssids = 1;
  1092. creq->scan_start = jiffies;
  1093. /* translate "Scan on frequencies" request */
  1094. i = 0;
  1095. for (band = 0; band < IEEE80211_NUM_BANDS; band++) {
  1096. int j;
  1097. if (!wiphy->bands[band])
  1098. continue;
  1099. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  1100. /* ignore disabled channels */
  1101. if (wiphy->bands[band]->channels[j].flags &
  1102. IEEE80211_CHAN_DISABLED)
  1103. continue;
  1104. /* If we have a wireless request structure and the
  1105. * wireless request specifies frequencies, then search
  1106. * for the matching hardware channel.
  1107. */
  1108. if (wreq && wreq->num_channels) {
  1109. int k;
  1110. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  1111. for (k = 0; k < wreq->num_channels; k++) {
  1112. struct iw_freq *freq =
  1113. &wreq->channel_list[k];
  1114. int wext_freq =
  1115. cfg80211_wext_freq(freq);
  1116. if (wext_freq == wiphy_freq)
  1117. goto wext_freq_found;
  1118. }
  1119. goto wext_freq_not_found;
  1120. }
  1121. wext_freq_found:
  1122. creq->channels[i] = &wiphy->bands[band]->channels[j];
  1123. i++;
  1124. wext_freq_not_found: ;
  1125. }
  1126. }
  1127. /* No channels found? */
  1128. if (!i) {
  1129. err = -EINVAL;
  1130. goto out;
  1131. }
  1132. /* Set real number of channels specified in creq->channels[] */
  1133. creq->n_channels = i;
  1134. /* translate "Scan for SSID" request */
  1135. if (wreq) {
  1136. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  1137. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  1138. err = -EINVAL;
  1139. goto out;
  1140. }
  1141. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  1142. creq->ssids[0].ssid_len = wreq->essid_len;
  1143. }
  1144. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  1145. creq->n_ssids = 0;
  1146. }
  1147. for (i = 0; i < IEEE80211_NUM_BANDS; i++)
  1148. if (wiphy->bands[i])
  1149. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  1150. rdev->scan_req = creq;
  1151. err = rdev_scan(rdev, creq);
  1152. if (err) {
  1153. rdev->scan_req = NULL;
  1154. /* creq will be freed below */
  1155. } else {
  1156. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  1157. /* creq now owned by driver */
  1158. creq = NULL;
  1159. dev_hold(dev);
  1160. }
  1161. out:
  1162. kfree(creq);
  1163. return err;
  1164. }
  1165. EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
  1166. static char *ieee80211_scan_add_ies(struct iw_request_info *info,
  1167. const struct cfg80211_bss_ies *ies,
  1168. char *current_ev, char *end_buf)
  1169. {
  1170. const u8 *pos, *end, *next;
  1171. struct iw_event iwe;
  1172. if (!ies)
  1173. return current_ev;
  1174. /*
  1175. * If needed, fragment the IEs buffer (at IE boundaries) into short
  1176. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  1177. */
  1178. pos = ies->data;
  1179. end = pos + ies->len;
  1180. while (end - pos > IW_GENERIC_IE_MAX) {
  1181. next = pos + 2 + pos[1];
  1182. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  1183. next = next + 2 + next[1];
  1184. memset(&iwe, 0, sizeof(iwe));
  1185. iwe.cmd = IWEVGENIE;
  1186. iwe.u.data.length = next - pos;
  1187. current_ev = iwe_stream_add_point_check(info, current_ev,
  1188. end_buf, &iwe,
  1189. (void *)pos);
  1190. if (IS_ERR(current_ev))
  1191. return current_ev;
  1192. pos = next;
  1193. }
  1194. if (end > pos) {
  1195. memset(&iwe, 0, sizeof(iwe));
  1196. iwe.cmd = IWEVGENIE;
  1197. iwe.u.data.length = end - pos;
  1198. current_ev = iwe_stream_add_point_check(info, current_ev,
  1199. end_buf, &iwe,
  1200. (void *)pos);
  1201. if (IS_ERR(current_ev))
  1202. return current_ev;
  1203. }
  1204. return current_ev;
  1205. }
  1206. static char *
  1207. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  1208. struct cfg80211_internal_bss *bss, char *current_ev,
  1209. char *end_buf)
  1210. {
  1211. const struct cfg80211_bss_ies *ies;
  1212. struct iw_event iwe;
  1213. const u8 *ie;
  1214. u8 buf[50];
  1215. u8 *cfg, *p, *tmp;
  1216. int rem, i, sig;
  1217. bool ismesh = false;
  1218. memset(&iwe, 0, sizeof(iwe));
  1219. iwe.cmd = SIOCGIWAP;
  1220. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1221. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  1222. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1223. IW_EV_ADDR_LEN);
  1224. if (IS_ERR(current_ev))
  1225. return current_ev;
  1226. memset(&iwe, 0, sizeof(iwe));
  1227. iwe.cmd = SIOCGIWFREQ;
  1228. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  1229. iwe.u.freq.e = 0;
  1230. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1231. IW_EV_FREQ_LEN);
  1232. if (IS_ERR(current_ev))
  1233. return current_ev;
  1234. memset(&iwe, 0, sizeof(iwe));
  1235. iwe.cmd = SIOCGIWFREQ;
  1236. iwe.u.freq.m = bss->pub.channel->center_freq;
  1237. iwe.u.freq.e = 6;
  1238. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1239. IW_EV_FREQ_LEN);
  1240. if (IS_ERR(current_ev))
  1241. return current_ev;
  1242. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  1243. memset(&iwe, 0, sizeof(iwe));
  1244. iwe.cmd = IWEVQUAL;
  1245. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  1246. IW_QUAL_NOISE_INVALID |
  1247. IW_QUAL_QUAL_UPDATED;
  1248. switch (wiphy->signal_type) {
  1249. case CFG80211_SIGNAL_TYPE_MBM:
  1250. sig = bss->pub.signal / 100;
  1251. iwe.u.qual.level = sig;
  1252. iwe.u.qual.updated |= IW_QUAL_DBM;
  1253. if (sig < -110) /* rather bad */
  1254. sig = -110;
  1255. else if (sig > -40) /* perfect */
  1256. sig = -40;
  1257. /* will give a range of 0 .. 70 */
  1258. iwe.u.qual.qual = sig + 110;
  1259. break;
  1260. case CFG80211_SIGNAL_TYPE_UNSPEC:
  1261. iwe.u.qual.level = bss->pub.signal;
  1262. /* will give range 0 .. 100 */
  1263. iwe.u.qual.qual = bss->pub.signal;
  1264. break;
  1265. default:
  1266. /* not reached */
  1267. break;
  1268. }
  1269. current_ev = iwe_stream_add_event_check(info, current_ev,
  1270. end_buf, &iwe,
  1271. IW_EV_QUAL_LEN);
  1272. if (IS_ERR(current_ev))
  1273. return current_ev;
  1274. }
  1275. memset(&iwe, 0, sizeof(iwe));
  1276. iwe.cmd = SIOCGIWENCODE;
  1277. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  1278. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1279. else
  1280. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1281. iwe.u.data.length = 0;
  1282. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  1283. &iwe, "");
  1284. if (IS_ERR(current_ev))
  1285. return current_ev;
  1286. rcu_read_lock();
  1287. ies = rcu_dereference(bss->pub.ies);
  1288. rem = ies->len;
  1289. ie = ies->data;
  1290. while (rem >= 2) {
  1291. /* invalid data */
  1292. if (ie[1] > rem - 2)
  1293. break;
  1294. switch (ie[0]) {
  1295. case WLAN_EID_SSID:
  1296. memset(&iwe, 0, sizeof(iwe));
  1297. iwe.cmd = SIOCGIWESSID;
  1298. iwe.u.data.length = ie[1];
  1299. iwe.u.data.flags = 1;
  1300. current_ev = iwe_stream_add_point_check(info,
  1301. current_ev,
  1302. end_buf, &iwe,
  1303. (u8 *)ie + 2);
  1304. if (IS_ERR(current_ev))
  1305. goto unlock;
  1306. break;
  1307. case WLAN_EID_MESH_ID:
  1308. memset(&iwe, 0, sizeof(iwe));
  1309. iwe.cmd = SIOCGIWESSID;
  1310. iwe.u.data.length = ie[1];
  1311. iwe.u.data.flags = 1;
  1312. current_ev = iwe_stream_add_point_check(info,
  1313. current_ev,
  1314. end_buf, &iwe,
  1315. (u8 *)ie + 2);
  1316. if (IS_ERR(current_ev))
  1317. goto unlock;
  1318. break;
  1319. case WLAN_EID_MESH_CONFIG:
  1320. ismesh = true;
  1321. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  1322. break;
  1323. cfg = (u8 *)ie + 2;
  1324. memset(&iwe, 0, sizeof(iwe));
  1325. iwe.cmd = IWEVCUSTOM;
  1326. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  1327. "0x%02X", cfg[0]);
  1328. iwe.u.data.length = strlen(buf);
  1329. current_ev = iwe_stream_add_point_check(info,
  1330. current_ev,
  1331. end_buf,
  1332. &iwe, buf);
  1333. if (IS_ERR(current_ev))
  1334. goto unlock;
  1335. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  1336. cfg[1]);
  1337. iwe.u.data.length = strlen(buf);
  1338. current_ev = iwe_stream_add_point_check(info,
  1339. current_ev,
  1340. end_buf,
  1341. &iwe, buf);
  1342. if (IS_ERR(current_ev))
  1343. goto unlock;
  1344. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  1345. cfg[2]);
  1346. iwe.u.data.length = strlen(buf);
  1347. current_ev = iwe_stream_add_point_check(info,
  1348. current_ev,
  1349. end_buf,
  1350. &iwe, buf);
  1351. if (IS_ERR(current_ev))
  1352. goto unlock;
  1353. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  1354. iwe.u.data.length = strlen(buf);
  1355. current_ev = iwe_stream_add_point_check(info,
  1356. current_ev,
  1357. end_buf,
  1358. &iwe, buf);
  1359. if (IS_ERR(current_ev))
  1360. goto unlock;
  1361. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  1362. iwe.u.data.length = strlen(buf);
  1363. current_ev = iwe_stream_add_point_check(info,
  1364. current_ev,
  1365. end_buf,
  1366. &iwe, buf);
  1367. if (IS_ERR(current_ev))
  1368. goto unlock;
  1369. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  1370. iwe.u.data.length = strlen(buf);
  1371. current_ev = iwe_stream_add_point_check(info,
  1372. current_ev,
  1373. end_buf,
  1374. &iwe, buf);
  1375. if (IS_ERR(current_ev))
  1376. goto unlock;
  1377. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1378. iwe.u.data.length = strlen(buf);
  1379. current_ev = iwe_stream_add_point_check(info,
  1380. current_ev,
  1381. end_buf,
  1382. &iwe, buf);
  1383. if (IS_ERR(current_ev))
  1384. goto unlock;
  1385. break;
  1386. case WLAN_EID_SUPP_RATES:
  1387. case WLAN_EID_EXT_SUPP_RATES:
  1388. /* display all supported rates in readable format */
  1389. p = current_ev + iwe_stream_lcp_len(info);
  1390. memset(&iwe, 0, sizeof(iwe));
  1391. iwe.cmd = SIOCGIWRATE;
  1392. /* Those two flags are ignored... */
  1393. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1394. for (i = 0; i < ie[1]; i++) {
  1395. iwe.u.bitrate.value =
  1396. ((ie[i + 2] & 0x7f) * 500000);
  1397. tmp = p;
  1398. p = iwe_stream_add_value(info, current_ev, p,
  1399. end_buf, &iwe,
  1400. IW_EV_PARAM_LEN);
  1401. if (p == tmp) {
  1402. current_ev = ERR_PTR(-E2BIG);
  1403. goto unlock;
  1404. }
  1405. }
  1406. current_ev = p;
  1407. break;
  1408. }
  1409. rem -= ie[1] + 2;
  1410. ie += ie[1] + 2;
  1411. }
  1412. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1413. ismesh) {
  1414. memset(&iwe, 0, sizeof(iwe));
  1415. iwe.cmd = SIOCGIWMODE;
  1416. if (ismesh)
  1417. iwe.u.mode = IW_MODE_MESH;
  1418. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1419. iwe.u.mode = IW_MODE_MASTER;
  1420. else
  1421. iwe.u.mode = IW_MODE_ADHOC;
  1422. current_ev = iwe_stream_add_event_check(info, current_ev,
  1423. end_buf, &iwe,
  1424. IW_EV_UINT_LEN);
  1425. if (IS_ERR(current_ev))
  1426. goto unlock;
  1427. }
  1428. memset(&iwe, 0, sizeof(iwe));
  1429. iwe.cmd = IWEVCUSTOM;
  1430. sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
  1431. iwe.u.data.length = strlen(buf);
  1432. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  1433. &iwe, buf);
  1434. if (IS_ERR(current_ev))
  1435. goto unlock;
  1436. memset(&iwe, 0, sizeof(iwe));
  1437. iwe.cmd = IWEVCUSTOM;
  1438. sprintf(buf, " Last beacon: %ums ago",
  1439. elapsed_jiffies_msecs(bss->ts));
  1440. iwe.u.data.length = strlen(buf);
  1441. current_ev = iwe_stream_add_point_check(info, current_ev,
  1442. end_buf, &iwe, buf);
  1443. if (IS_ERR(current_ev))
  1444. goto unlock;
  1445. current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
  1446. unlock:
  1447. rcu_read_unlock();
  1448. return current_ev;
  1449. }
  1450. static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
  1451. struct iw_request_info *info,
  1452. char *buf, size_t len)
  1453. {
  1454. char *current_ev = buf;
  1455. char *end_buf = buf + len;
  1456. struct cfg80211_internal_bss *bss;
  1457. int err = 0;
  1458. spin_lock_bh(&rdev->bss_lock);
  1459. cfg80211_bss_expire(rdev);
  1460. list_for_each_entry(bss, &rdev->bss_list, list) {
  1461. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1462. err = -E2BIG;
  1463. break;
  1464. }
  1465. current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
  1466. current_ev, end_buf);
  1467. if (IS_ERR(current_ev)) {
  1468. err = PTR_ERR(current_ev);
  1469. break;
  1470. }
  1471. }
  1472. spin_unlock_bh(&rdev->bss_lock);
  1473. if (err)
  1474. return err;
  1475. return current_ev - buf;
  1476. }
  1477. int cfg80211_wext_giwscan(struct net_device *dev,
  1478. struct iw_request_info *info,
  1479. struct iw_point *data, char *extra)
  1480. {
  1481. struct cfg80211_registered_device *rdev;
  1482. int res;
  1483. if (!netif_running(dev))
  1484. return -ENETDOWN;
  1485. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1486. if (IS_ERR(rdev))
  1487. return PTR_ERR(rdev);
  1488. if (rdev->scan_req || rdev->scan_msg)
  1489. return -EAGAIN;
  1490. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1491. data->length = 0;
  1492. if (res >= 0) {
  1493. data->length = res;
  1494. res = 0;
  1495. }
  1496. return res;
  1497. }
  1498. EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
  1499. #endif