scan.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758
  1. /*
  2. * cfg80211 scan result handling
  3. *
  4. * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
  5. * Copyright 2013-2014 Intel Mobile Communications GmbH
  6. * Copyright 2016 Intel Deutschland GmbH
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/wireless.h>
  13. #include <linux/nl80211.h>
  14. #include <linux/etherdevice.h>
  15. #include <net/arp.h>
  16. #include <net/cfg80211.h>
  17. #include <net/cfg80211-wext.h>
  18. #include <net/iw_handler.h>
  19. #include "core.h"
  20. #include "nl80211.h"
  21. #include "wext-compat.h"
  22. #include "rdev-ops.h"
  23. /**
  24. * DOC: BSS tree/list structure
  25. *
  26. * At the top level, the BSS list is kept in both a list in each
  27. * registered device (@bss_list) as well as an RB-tree for faster
  28. * lookup. In the RB-tree, entries can be looked up using their
  29. * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
  30. * for other BSSes.
  31. *
  32. * Due to the possibility of hidden SSIDs, there's a second level
  33. * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
  34. * The hidden_list connects all BSSes belonging to a single AP
  35. * that has a hidden SSID, and connects beacon and probe response
  36. * entries. For a probe response entry for a hidden SSID, the
  37. * hidden_beacon_bss pointer points to the BSS struct holding the
  38. * beacon's information.
  39. *
  40. * Reference counting is done for all these references except for
  41. * the hidden_list, so that a beacon BSS struct that is otherwise
  42. * not referenced has one reference for being on the bss_list and
  43. * one for each probe response entry that points to it using the
  44. * hidden_beacon_bss pointer. When a BSS struct that has such a
  45. * pointer is get/put, the refcount update is also propagated to
  46. * the referenced struct, this ensure that it cannot get removed
  47. * while somebody is using the probe response version.
  48. *
  49. * Note that the hidden_beacon_bss pointer never changes, due to
  50. * the reference counting. Therefore, no locking is needed for
  51. * it.
  52. *
  53. * Also note that the hidden_beacon_bss pointer is only relevant
  54. * if the driver uses something other than the IEs, e.g. private
  55. * data stored stored in the BSS struct, since the beacon IEs are
  56. * also linked into the probe response struct.
  57. */
  58. /*
  59. * Limit the number of BSS entries stored in mac80211. Each one is
  60. * a bit over 4k at most, so this limits to roughly 4-5M of memory.
  61. * If somebody wants to really attack this though, they'd likely
  62. * use small beacons, and only one type of frame, limiting each of
  63. * the entries to a much smaller size (in order to generate more
  64. * entries in total, so overhead is bigger.)
  65. */
  66. static int bss_entries_limit = 1000;
  67. module_param(bss_entries_limit, int, 0644);
  68. MODULE_PARM_DESC(bss_entries_limit,
  69. "limit to number of scan BSS entries (per wiphy, default 1000)");
  70. #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
  71. static void bss_free(struct cfg80211_internal_bss *bss)
  72. {
  73. struct cfg80211_bss_ies *ies;
  74. if (WARN_ON(atomic_read(&bss->hold)))
  75. return;
  76. ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
  77. if (ies && !bss->pub.hidden_beacon_bss)
  78. kfree_rcu(ies, rcu_head);
  79. ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
  80. if (ies)
  81. kfree_rcu(ies, rcu_head);
  82. /*
  83. * This happens when the module is removed, it doesn't
  84. * really matter any more save for completeness
  85. */
  86. if (!list_empty(&bss->hidden_list))
  87. list_del(&bss->hidden_list);
  88. kfree(bss);
  89. }
  90. static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
  91. struct cfg80211_internal_bss *bss)
  92. {
  93. lockdep_assert_held(&rdev->bss_lock);
  94. bss->refcount++;
  95. if (bss->pub.hidden_beacon_bss) {
  96. bss = container_of(bss->pub.hidden_beacon_bss,
  97. struct cfg80211_internal_bss,
  98. pub);
  99. bss->refcount++;
  100. }
  101. }
  102. static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
  103. struct cfg80211_internal_bss *bss)
  104. {
  105. lockdep_assert_held(&rdev->bss_lock);
  106. if (bss->pub.hidden_beacon_bss) {
  107. struct cfg80211_internal_bss *hbss;
  108. hbss = container_of(bss->pub.hidden_beacon_bss,
  109. struct cfg80211_internal_bss,
  110. pub);
  111. hbss->refcount--;
  112. if (hbss->refcount == 0)
  113. bss_free(hbss);
  114. }
  115. bss->refcount--;
  116. if (bss->refcount == 0)
  117. bss_free(bss);
  118. }
  119. static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
  120. struct cfg80211_internal_bss *bss)
  121. {
  122. lockdep_assert_held(&rdev->bss_lock);
  123. if (!list_empty(&bss->hidden_list)) {
  124. /*
  125. * don't remove the beacon entry if it has
  126. * probe responses associated with it
  127. */
  128. if (!bss->pub.hidden_beacon_bss)
  129. return false;
  130. /*
  131. * if it's a probe response entry break its
  132. * link to the other entries in the group
  133. */
  134. list_del_init(&bss->hidden_list);
  135. }
  136. list_del_init(&bss->list);
  137. rb_erase(&bss->rbn, &rdev->bss_tree);
  138. rdev->bss_entries--;
  139. WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
  140. "rdev bss entries[%d]/list[empty:%d] corruption\n",
  141. rdev->bss_entries, list_empty(&rdev->bss_list));
  142. bss_ref_put(rdev, bss);
  143. return true;
  144. }
  145. static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
  146. unsigned long expire_time)
  147. {
  148. struct cfg80211_internal_bss *bss, *tmp;
  149. bool expired = false;
  150. lockdep_assert_held(&rdev->bss_lock);
  151. list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
  152. if (atomic_read(&bss->hold))
  153. continue;
  154. if (!time_after(expire_time, bss->ts))
  155. continue;
  156. if (__cfg80211_unlink_bss(rdev, bss))
  157. expired = true;
  158. }
  159. if (expired)
  160. rdev->bss_generation++;
  161. }
  162. static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
  163. {
  164. struct cfg80211_internal_bss *bss, *oldest = NULL;
  165. bool ret;
  166. lockdep_assert_held(&rdev->bss_lock);
  167. list_for_each_entry(bss, &rdev->bss_list, list) {
  168. if (atomic_read(&bss->hold))
  169. continue;
  170. if (!list_empty(&bss->hidden_list) &&
  171. !bss->pub.hidden_beacon_bss)
  172. continue;
  173. if (oldest && time_before(oldest->ts, bss->ts))
  174. continue;
  175. oldest = bss;
  176. }
  177. if (WARN_ON(!oldest))
  178. return false;
  179. /*
  180. * The callers make sure to increase rdev->bss_generation if anything
  181. * gets removed (and a new entry added), so there's no need to also do
  182. * it here.
  183. */
  184. ret = __cfg80211_unlink_bss(rdev, oldest);
  185. WARN_ON(!ret);
  186. return ret;
  187. }
  188. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
  189. bool send_message)
  190. {
  191. struct cfg80211_scan_request *request;
  192. struct wireless_dev *wdev;
  193. struct sk_buff *msg;
  194. #ifdef CONFIG_CFG80211_WEXT
  195. union iwreq_data wrqu;
  196. #endif
  197. ASSERT_RTNL();
  198. if (rdev->scan_msg) {
  199. nl80211_send_scan_result(rdev, rdev->scan_msg);
  200. rdev->scan_msg = NULL;
  201. return;
  202. }
  203. request = rdev->scan_req;
  204. if (!request)
  205. return;
  206. wdev = request->wdev;
  207. /*
  208. * This must be before sending the other events!
  209. * Otherwise, wpa_supplicant gets completely confused with
  210. * wext events.
  211. */
  212. if (wdev->netdev)
  213. cfg80211_sme_scan_done(wdev->netdev);
  214. if (!request->info.aborted &&
  215. request->flags & NL80211_SCAN_FLAG_FLUSH) {
  216. /* flush entries from previous scans */
  217. spin_lock_bh(&rdev->bss_lock);
  218. __cfg80211_bss_expire(rdev, request->scan_start);
  219. spin_unlock_bh(&rdev->bss_lock);
  220. }
  221. msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
  222. #ifdef CONFIG_CFG80211_WEXT
  223. if (wdev->netdev && !request->info.aborted) {
  224. memset(&wrqu, 0, sizeof(wrqu));
  225. wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
  226. }
  227. #endif
  228. if (wdev->netdev)
  229. dev_put(wdev->netdev);
  230. rdev->scan_req = NULL;
  231. kfree(request);
  232. if (!send_message)
  233. rdev->scan_msg = msg;
  234. else
  235. nl80211_send_scan_result(rdev, msg);
  236. }
  237. void __cfg80211_scan_done(struct work_struct *wk)
  238. {
  239. struct cfg80211_registered_device *rdev;
  240. rdev = container_of(wk, struct cfg80211_registered_device,
  241. scan_done_wk);
  242. rtnl_lock();
  243. ___cfg80211_scan_done(rdev, true);
  244. rtnl_unlock();
  245. }
  246. void cfg80211_scan_done(struct cfg80211_scan_request *request,
  247. struct cfg80211_scan_info *info)
  248. {
  249. trace_cfg80211_scan_done(request, info);
  250. WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req);
  251. request->info = *info;
  252. request->notified = true;
  253. queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
  254. }
  255. EXPORT_SYMBOL(cfg80211_scan_done);
  256. void __cfg80211_sched_scan_results(struct work_struct *wk)
  257. {
  258. struct cfg80211_registered_device *rdev;
  259. struct cfg80211_sched_scan_request *request;
  260. rdev = container_of(wk, struct cfg80211_registered_device,
  261. sched_scan_results_wk);
  262. rtnl_lock();
  263. request = rtnl_dereference(rdev->sched_scan_req);
  264. /* we don't have sched_scan_req anymore if the scan is stopping */
  265. if (request) {
  266. if (request->flags & NL80211_SCAN_FLAG_FLUSH) {
  267. /* flush entries from previous scans */
  268. spin_lock_bh(&rdev->bss_lock);
  269. __cfg80211_bss_expire(rdev, request->scan_start);
  270. spin_unlock_bh(&rdev->bss_lock);
  271. request->scan_start = jiffies;
  272. }
  273. nl80211_send_sched_scan_results(rdev, request->dev);
  274. }
  275. rtnl_unlock();
  276. }
  277. void cfg80211_sched_scan_results(struct wiphy *wiphy)
  278. {
  279. trace_cfg80211_sched_scan_results(wiphy);
  280. /* ignore if we're not scanning */
  281. if (rcu_access_pointer(wiphy_to_rdev(wiphy)->sched_scan_req))
  282. queue_work(cfg80211_wq,
  283. &wiphy_to_rdev(wiphy)->sched_scan_results_wk);
  284. }
  285. EXPORT_SYMBOL(cfg80211_sched_scan_results);
  286. void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy)
  287. {
  288. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  289. ASSERT_RTNL();
  290. trace_cfg80211_sched_scan_stopped(wiphy);
  291. __cfg80211_stop_sched_scan(rdev, true);
  292. }
  293. EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
  294. void cfg80211_sched_scan_stopped(struct wiphy *wiphy)
  295. {
  296. rtnl_lock();
  297. cfg80211_sched_scan_stopped_rtnl(wiphy);
  298. rtnl_unlock();
  299. }
  300. EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
  301. int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
  302. bool driver_initiated)
  303. {
  304. struct cfg80211_sched_scan_request *sched_scan_req;
  305. struct net_device *dev;
  306. ASSERT_RTNL();
  307. if (!rdev->sched_scan_req)
  308. return -ENOENT;
  309. sched_scan_req = rtnl_dereference(rdev->sched_scan_req);
  310. dev = sched_scan_req->dev;
  311. if (!driver_initiated) {
  312. int err = rdev_sched_scan_stop(rdev, dev);
  313. if (err)
  314. return err;
  315. }
  316. nl80211_send_sched_scan(rdev, dev, NL80211_CMD_SCHED_SCAN_STOPPED);
  317. RCU_INIT_POINTER(rdev->sched_scan_req, NULL);
  318. kfree_rcu(sched_scan_req, rcu_head);
  319. return 0;
  320. }
  321. void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
  322. unsigned long age_secs)
  323. {
  324. struct cfg80211_internal_bss *bss;
  325. unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
  326. spin_lock_bh(&rdev->bss_lock);
  327. list_for_each_entry(bss, &rdev->bss_list, list)
  328. bss->ts -= age_jiffies;
  329. spin_unlock_bh(&rdev->bss_lock);
  330. }
  331. void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
  332. {
  333. __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
  334. }
  335. const u8 *cfg80211_find_ie_match(u8 eid, const u8 *ies, int len,
  336. const u8 *match, int match_len,
  337. int match_offset)
  338. {
  339. /* match_offset can't be smaller than 2, unless match_len is
  340. * zero, in which case match_offset must be zero as well.
  341. */
  342. if (WARN_ON((match_len && match_offset < 2) ||
  343. (!match_len && match_offset)))
  344. return NULL;
  345. while (len >= 2 && len >= ies[1] + 2) {
  346. if ((ies[0] == eid) &&
  347. (ies[1] + 2 >= match_offset + match_len) &&
  348. !memcmp(ies + match_offset, match, match_len))
  349. return ies;
  350. len -= ies[1] + 2;
  351. ies += ies[1] + 2;
  352. }
  353. return NULL;
  354. }
  355. EXPORT_SYMBOL(cfg80211_find_ie_match);
  356. const u8 *cfg80211_find_vendor_ie(unsigned int oui, int oui_type,
  357. const u8 *ies, int len)
  358. {
  359. const u8 *ie;
  360. u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
  361. int match_len = (oui_type < 0) ? 3 : sizeof(match);
  362. if (WARN_ON(oui_type > 0xff))
  363. return NULL;
  364. ie = cfg80211_find_ie_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
  365. match, match_len, 2);
  366. if (ie && (ie[1] < 4))
  367. return NULL;
  368. return ie;
  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 nl80211_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 == NL80211_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. found->parent_tsf = tmp->parent_tsf;
  761. ether_addr_copy(found->parent_bssid, tmp->parent_bssid);
  762. } else {
  763. struct cfg80211_internal_bss *new;
  764. struct cfg80211_internal_bss *hidden;
  765. struct cfg80211_bss_ies *ies;
  766. /*
  767. * create a copy -- the "res" variable that is passed in
  768. * is allocated on the stack since it's not needed in the
  769. * more common case of an update
  770. */
  771. new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
  772. GFP_ATOMIC);
  773. if (!new) {
  774. ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
  775. if (ies)
  776. kfree_rcu(ies, rcu_head);
  777. ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
  778. if (ies)
  779. kfree_rcu(ies, rcu_head);
  780. goto drop;
  781. }
  782. memcpy(new, tmp, sizeof(*new));
  783. new->refcount = 1;
  784. INIT_LIST_HEAD(&new->hidden_list);
  785. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  786. hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
  787. if (!hidden)
  788. hidden = rb_find_bss(rdev, tmp,
  789. BSS_CMP_HIDE_NUL);
  790. if (hidden) {
  791. new->pub.hidden_beacon_bss = &hidden->pub;
  792. list_add(&new->hidden_list,
  793. &hidden->hidden_list);
  794. hidden->refcount++;
  795. rcu_assign_pointer(new->pub.beacon_ies,
  796. hidden->pub.beacon_ies);
  797. }
  798. } else {
  799. /*
  800. * Ok so we found a beacon, and don't have an entry. If
  801. * it's a beacon with hidden SSID, we might be in for an
  802. * expensive search for any probe responses that should
  803. * be grouped with this beacon for updates ...
  804. */
  805. if (!cfg80211_combine_bsses(rdev, new)) {
  806. kfree(new);
  807. goto drop;
  808. }
  809. }
  810. if (rdev->bss_entries >= bss_entries_limit &&
  811. !cfg80211_bss_expire_oldest(rdev)) {
  812. kfree(new);
  813. goto drop;
  814. }
  815. list_add_tail(&new->list, &rdev->bss_list);
  816. rdev->bss_entries++;
  817. rb_insert_bss(rdev, new);
  818. found = new;
  819. }
  820. rdev->bss_generation++;
  821. bss_ref_get(rdev, found);
  822. spin_unlock_bh(&rdev->bss_lock);
  823. return found;
  824. drop:
  825. spin_unlock_bh(&rdev->bss_lock);
  826. return NULL;
  827. }
  828. static struct ieee80211_channel *
  829. cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
  830. struct ieee80211_channel *channel)
  831. {
  832. const u8 *tmp;
  833. u32 freq;
  834. int channel_number = -1;
  835. tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
  836. if (tmp && tmp[1] == 1) {
  837. channel_number = tmp[2];
  838. } else {
  839. tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
  840. if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
  841. struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
  842. channel_number = htop->primary_chan;
  843. }
  844. }
  845. if (channel_number < 0)
  846. return channel;
  847. freq = ieee80211_channel_to_frequency(channel_number, channel->band);
  848. channel = ieee80211_get_channel(wiphy, freq);
  849. if (!channel)
  850. return NULL;
  851. if (channel->flags & IEEE80211_CHAN_DISABLED)
  852. return NULL;
  853. return channel;
  854. }
  855. /* Returned bss is reference counted and must be cleaned up appropriately. */
  856. struct cfg80211_bss *
  857. cfg80211_inform_bss_data(struct wiphy *wiphy,
  858. struct cfg80211_inform_bss *data,
  859. enum cfg80211_bss_frame_type ftype,
  860. const u8 *bssid, u64 tsf, u16 capability,
  861. u16 beacon_interval, const u8 *ie, size_t ielen,
  862. gfp_t gfp)
  863. {
  864. struct cfg80211_bss_ies *ies;
  865. struct ieee80211_channel *channel;
  866. struct cfg80211_internal_bss tmp = {}, *res;
  867. int bss_type;
  868. bool signal_valid;
  869. if (WARN_ON(!wiphy))
  870. return NULL;
  871. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  872. (data->signal < 0 || data->signal > 100)))
  873. return NULL;
  874. channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan);
  875. if (!channel)
  876. return NULL;
  877. memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
  878. tmp.pub.channel = channel;
  879. tmp.pub.scan_width = data->scan_width;
  880. tmp.pub.signal = data->signal;
  881. tmp.pub.beacon_interval = beacon_interval;
  882. tmp.pub.capability = capability;
  883. tmp.ts_boottime = data->boottime_ns;
  884. /*
  885. * If we do not know here whether the IEs are from a Beacon or Probe
  886. * Response frame, we need to pick one of the options and only use it
  887. * with the driver that does not provide the full Beacon/Probe Response
  888. * frame. Use Beacon frame pointer to avoid indicating that this should
  889. * override the IEs pointer should we have received an earlier
  890. * indication of Probe Response data.
  891. */
  892. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  893. if (!ies)
  894. return NULL;
  895. ies->len = ielen;
  896. ies->tsf = tsf;
  897. ies->from_beacon = false;
  898. memcpy(ies->data, ie, ielen);
  899. switch (ftype) {
  900. case CFG80211_BSS_FTYPE_BEACON:
  901. ies->from_beacon = true;
  902. /* fall through to assign */
  903. case CFG80211_BSS_FTYPE_UNKNOWN:
  904. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  905. break;
  906. case CFG80211_BSS_FTYPE_PRESP:
  907. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  908. break;
  909. }
  910. rcu_assign_pointer(tmp.pub.ies, ies);
  911. signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
  912. wiphy->max_adj_channel_rssi_comp;
  913. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
  914. if (!res)
  915. return NULL;
  916. if (channel->band == NL80211_BAND_60GHZ) {
  917. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  918. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  919. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  920. regulatory_hint_found_beacon(wiphy, channel, gfp);
  921. } else {
  922. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  923. regulatory_hint_found_beacon(wiphy, channel, gfp);
  924. }
  925. trace_cfg80211_return_bss(&res->pub);
  926. /* cfg80211_bss_update gives us a referenced result */
  927. return &res->pub;
  928. }
  929. EXPORT_SYMBOL(cfg80211_inform_bss_data);
  930. /* cfg80211_inform_bss_width_frame helper */
  931. struct cfg80211_bss *
  932. cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
  933. struct cfg80211_inform_bss *data,
  934. struct ieee80211_mgmt *mgmt, size_t len,
  935. gfp_t gfp)
  936. {
  937. struct cfg80211_internal_bss tmp = {}, *res;
  938. struct cfg80211_bss_ies *ies;
  939. struct ieee80211_channel *channel;
  940. bool signal_valid;
  941. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  942. u.probe_resp.variable);
  943. int bss_type;
  944. BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
  945. offsetof(struct ieee80211_mgmt, u.beacon.variable));
  946. trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
  947. if (WARN_ON(!mgmt))
  948. return NULL;
  949. if (WARN_ON(!wiphy))
  950. return NULL;
  951. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  952. (data->signal < 0 || data->signal > 100)))
  953. return NULL;
  954. if (WARN_ON(len < offsetof(struct ieee80211_mgmt, u.probe_resp.variable)))
  955. return NULL;
  956. channel = cfg80211_get_bss_channel(wiphy, mgmt->u.beacon.variable,
  957. ielen, data->chan);
  958. if (!channel)
  959. return NULL;
  960. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  961. if (!ies)
  962. return NULL;
  963. ies->len = ielen;
  964. ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  965. ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
  966. memcpy(ies->data, mgmt->u.probe_resp.variable, ielen);
  967. if (ieee80211_is_probe_resp(mgmt->frame_control))
  968. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  969. else
  970. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  971. rcu_assign_pointer(tmp.pub.ies, ies);
  972. memcpy(tmp.pub.bssid, mgmt->bssid, ETH_ALEN);
  973. tmp.pub.channel = channel;
  974. tmp.pub.scan_width = data->scan_width;
  975. tmp.pub.signal = data->signal;
  976. tmp.pub.beacon_interval = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  977. tmp.pub.capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  978. tmp.ts_boottime = data->boottime_ns;
  979. tmp.parent_tsf = data->parent_tsf;
  980. ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
  981. signal_valid = abs(data->chan->center_freq - channel->center_freq) <=
  982. wiphy->max_adj_channel_rssi_comp;
  983. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid);
  984. if (!res)
  985. return NULL;
  986. if (channel->band == NL80211_BAND_60GHZ) {
  987. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  988. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  989. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  990. regulatory_hint_found_beacon(wiphy, channel, gfp);
  991. } else {
  992. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  993. regulatory_hint_found_beacon(wiphy, channel, gfp);
  994. }
  995. trace_cfg80211_return_bss(&res->pub);
  996. /* cfg80211_bss_update gives us a referenced result */
  997. return &res->pub;
  998. }
  999. EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
  1000. void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1001. {
  1002. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1003. struct cfg80211_internal_bss *bss;
  1004. if (!pub)
  1005. return;
  1006. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1007. spin_lock_bh(&rdev->bss_lock);
  1008. bss_ref_get(rdev, bss);
  1009. spin_unlock_bh(&rdev->bss_lock);
  1010. }
  1011. EXPORT_SYMBOL(cfg80211_ref_bss);
  1012. void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1013. {
  1014. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1015. struct cfg80211_internal_bss *bss;
  1016. if (!pub)
  1017. return;
  1018. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1019. spin_lock_bh(&rdev->bss_lock);
  1020. bss_ref_put(rdev, bss);
  1021. spin_unlock_bh(&rdev->bss_lock);
  1022. }
  1023. EXPORT_SYMBOL(cfg80211_put_bss);
  1024. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  1025. {
  1026. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1027. struct cfg80211_internal_bss *bss;
  1028. if (WARN_ON(!pub))
  1029. return;
  1030. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  1031. spin_lock_bh(&rdev->bss_lock);
  1032. if (!list_empty(&bss->list)) {
  1033. if (__cfg80211_unlink_bss(rdev, bss))
  1034. rdev->bss_generation++;
  1035. }
  1036. spin_unlock_bh(&rdev->bss_lock);
  1037. }
  1038. EXPORT_SYMBOL(cfg80211_unlink_bss);
  1039. #ifdef CONFIG_CFG80211_WEXT
  1040. static struct cfg80211_registered_device *
  1041. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
  1042. {
  1043. struct cfg80211_registered_device *rdev;
  1044. struct net_device *dev;
  1045. ASSERT_RTNL();
  1046. dev = dev_get_by_index(net, ifindex);
  1047. if (!dev)
  1048. return ERR_PTR(-ENODEV);
  1049. if (dev->ieee80211_ptr)
  1050. rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
  1051. else
  1052. rdev = ERR_PTR(-ENODEV);
  1053. dev_put(dev);
  1054. return rdev;
  1055. }
  1056. int cfg80211_wext_siwscan(struct net_device *dev,
  1057. struct iw_request_info *info,
  1058. union iwreq_data *wrqu, char *extra)
  1059. {
  1060. struct cfg80211_registered_device *rdev;
  1061. struct wiphy *wiphy;
  1062. struct iw_scan_req *wreq = NULL;
  1063. struct cfg80211_scan_request *creq = NULL;
  1064. int i, err, n_channels = 0;
  1065. enum nl80211_band band;
  1066. if (!netif_running(dev))
  1067. return -ENETDOWN;
  1068. if (wrqu->data.length == sizeof(struct iw_scan_req))
  1069. wreq = (struct iw_scan_req *)extra;
  1070. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1071. if (IS_ERR(rdev))
  1072. return PTR_ERR(rdev);
  1073. if (rdev->scan_req || rdev->scan_msg) {
  1074. err = -EBUSY;
  1075. goto out;
  1076. }
  1077. wiphy = &rdev->wiphy;
  1078. /* Determine number of channels, needed to allocate creq */
  1079. if (wreq && wreq->num_channels)
  1080. n_channels = wreq->num_channels;
  1081. else
  1082. n_channels = ieee80211_get_num_supported_channels(wiphy);
  1083. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  1084. n_channels * sizeof(void *),
  1085. GFP_ATOMIC);
  1086. if (!creq) {
  1087. err = -ENOMEM;
  1088. goto out;
  1089. }
  1090. creq->wiphy = wiphy;
  1091. creq->wdev = dev->ieee80211_ptr;
  1092. /* SSIDs come after channels */
  1093. creq->ssids = (void *)&creq->channels[n_channels];
  1094. creq->n_channels = n_channels;
  1095. creq->n_ssids = 1;
  1096. creq->scan_start = jiffies;
  1097. /* translate "Scan on frequencies" request */
  1098. i = 0;
  1099. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  1100. int j;
  1101. if (!wiphy->bands[band])
  1102. continue;
  1103. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  1104. /* ignore disabled channels */
  1105. if (wiphy->bands[band]->channels[j].flags &
  1106. IEEE80211_CHAN_DISABLED)
  1107. continue;
  1108. /* If we have a wireless request structure and the
  1109. * wireless request specifies frequencies, then search
  1110. * for the matching hardware channel.
  1111. */
  1112. if (wreq && wreq->num_channels) {
  1113. int k;
  1114. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  1115. for (k = 0; k < wreq->num_channels; k++) {
  1116. struct iw_freq *freq =
  1117. &wreq->channel_list[k];
  1118. int wext_freq =
  1119. cfg80211_wext_freq(freq);
  1120. if (wext_freq == wiphy_freq)
  1121. goto wext_freq_found;
  1122. }
  1123. goto wext_freq_not_found;
  1124. }
  1125. wext_freq_found:
  1126. creq->channels[i] = &wiphy->bands[band]->channels[j];
  1127. i++;
  1128. wext_freq_not_found: ;
  1129. }
  1130. }
  1131. /* No channels found? */
  1132. if (!i) {
  1133. err = -EINVAL;
  1134. goto out;
  1135. }
  1136. /* Set real number of channels specified in creq->channels[] */
  1137. creq->n_channels = i;
  1138. /* translate "Scan for SSID" request */
  1139. if (wreq) {
  1140. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  1141. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  1142. err = -EINVAL;
  1143. goto out;
  1144. }
  1145. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  1146. creq->ssids[0].ssid_len = wreq->essid_len;
  1147. }
  1148. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  1149. creq->n_ssids = 0;
  1150. }
  1151. for (i = 0; i < NUM_NL80211_BANDS; i++)
  1152. if (wiphy->bands[i])
  1153. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  1154. eth_broadcast_addr(creq->bssid);
  1155. rdev->scan_req = creq;
  1156. err = rdev_scan(rdev, creq);
  1157. if (err) {
  1158. rdev->scan_req = NULL;
  1159. /* creq will be freed below */
  1160. } else {
  1161. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  1162. /* creq now owned by driver */
  1163. creq = NULL;
  1164. dev_hold(dev);
  1165. }
  1166. out:
  1167. kfree(creq);
  1168. return err;
  1169. }
  1170. EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
  1171. static char *ieee80211_scan_add_ies(struct iw_request_info *info,
  1172. const struct cfg80211_bss_ies *ies,
  1173. char *current_ev, char *end_buf)
  1174. {
  1175. const u8 *pos, *end, *next;
  1176. struct iw_event iwe;
  1177. if (!ies)
  1178. return current_ev;
  1179. /*
  1180. * If needed, fragment the IEs buffer (at IE boundaries) into short
  1181. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  1182. */
  1183. pos = ies->data;
  1184. end = pos + ies->len;
  1185. while (end - pos > IW_GENERIC_IE_MAX) {
  1186. next = pos + 2 + pos[1];
  1187. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  1188. next = next + 2 + next[1];
  1189. memset(&iwe, 0, sizeof(iwe));
  1190. iwe.cmd = IWEVGENIE;
  1191. iwe.u.data.length = next - pos;
  1192. current_ev = iwe_stream_add_point_check(info, current_ev,
  1193. end_buf, &iwe,
  1194. (void *)pos);
  1195. if (IS_ERR(current_ev))
  1196. return current_ev;
  1197. pos = next;
  1198. }
  1199. if (end > pos) {
  1200. memset(&iwe, 0, sizeof(iwe));
  1201. iwe.cmd = IWEVGENIE;
  1202. iwe.u.data.length = end - pos;
  1203. current_ev = iwe_stream_add_point_check(info, current_ev,
  1204. end_buf, &iwe,
  1205. (void *)pos);
  1206. if (IS_ERR(current_ev))
  1207. return current_ev;
  1208. }
  1209. return current_ev;
  1210. }
  1211. static char *
  1212. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  1213. struct cfg80211_internal_bss *bss, char *current_ev,
  1214. char *end_buf)
  1215. {
  1216. const struct cfg80211_bss_ies *ies;
  1217. struct iw_event iwe;
  1218. const u8 *ie;
  1219. u8 buf[50];
  1220. u8 *cfg, *p, *tmp;
  1221. int rem, i, sig;
  1222. bool ismesh = false;
  1223. memset(&iwe, 0, sizeof(iwe));
  1224. iwe.cmd = SIOCGIWAP;
  1225. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  1226. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  1227. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1228. IW_EV_ADDR_LEN);
  1229. if (IS_ERR(current_ev))
  1230. return current_ev;
  1231. memset(&iwe, 0, sizeof(iwe));
  1232. iwe.cmd = SIOCGIWFREQ;
  1233. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  1234. iwe.u.freq.e = 0;
  1235. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1236. IW_EV_FREQ_LEN);
  1237. if (IS_ERR(current_ev))
  1238. return current_ev;
  1239. memset(&iwe, 0, sizeof(iwe));
  1240. iwe.cmd = SIOCGIWFREQ;
  1241. iwe.u.freq.m = bss->pub.channel->center_freq;
  1242. iwe.u.freq.e = 6;
  1243. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  1244. IW_EV_FREQ_LEN);
  1245. if (IS_ERR(current_ev))
  1246. return current_ev;
  1247. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  1248. memset(&iwe, 0, sizeof(iwe));
  1249. iwe.cmd = IWEVQUAL;
  1250. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  1251. IW_QUAL_NOISE_INVALID |
  1252. IW_QUAL_QUAL_UPDATED;
  1253. switch (wiphy->signal_type) {
  1254. case CFG80211_SIGNAL_TYPE_MBM:
  1255. sig = bss->pub.signal / 100;
  1256. iwe.u.qual.level = sig;
  1257. iwe.u.qual.updated |= IW_QUAL_DBM;
  1258. if (sig < -110) /* rather bad */
  1259. sig = -110;
  1260. else if (sig > -40) /* perfect */
  1261. sig = -40;
  1262. /* will give a range of 0 .. 70 */
  1263. iwe.u.qual.qual = sig + 110;
  1264. break;
  1265. case CFG80211_SIGNAL_TYPE_UNSPEC:
  1266. iwe.u.qual.level = bss->pub.signal;
  1267. /* will give range 0 .. 100 */
  1268. iwe.u.qual.qual = bss->pub.signal;
  1269. break;
  1270. default:
  1271. /* not reached */
  1272. break;
  1273. }
  1274. current_ev = iwe_stream_add_event_check(info, current_ev,
  1275. end_buf, &iwe,
  1276. IW_EV_QUAL_LEN);
  1277. if (IS_ERR(current_ev))
  1278. return current_ev;
  1279. }
  1280. memset(&iwe, 0, sizeof(iwe));
  1281. iwe.cmd = SIOCGIWENCODE;
  1282. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  1283. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  1284. else
  1285. iwe.u.data.flags = IW_ENCODE_DISABLED;
  1286. iwe.u.data.length = 0;
  1287. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  1288. &iwe, "");
  1289. if (IS_ERR(current_ev))
  1290. return current_ev;
  1291. rcu_read_lock();
  1292. ies = rcu_dereference(bss->pub.ies);
  1293. rem = ies->len;
  1294. ie = ies->data;
  1295. while (rem >= 2) {
  1296. /* invalid data */
  1297. if (ie[1] > rem - 2)
  1298. break;
  1299. switch (ie[0]) {
  1300. case WLAN_EID_SSID:
  1301. memset(&iwe, 0, sizeof(iwe));
  1302. iwe.cmd = SIOCGIWESSID;
  1303. iwe.u.data.length = ie[1];
  1304. iwe.u.data.flags = 1;
  1305. current_ev = iwe_stream_add_point_check(info,
  1306. current_ev,
  1307. end_buf, &iwe,
  1308. (u8 *)ie + 2);
  1309. if (IS_ERR(current_ev))
  1310. goto unlock;
  1311. break;
  1312. case WLAN_EID_MESH_ID:
  1313. memset(&iwe, 0, sizeof(iwe));
  1314. iwe.cmd = SIOCGIWESSID;
  1315. iwe.u.data.length = ie[1];
  1316. iwe.u.data.flags = 1;
  1317. current_ev = iwe_stream_add_point_check(info,
  1318. current_ev,
  1319. end_buf, &iwe,
  1320. (u8 *)ie + 2);
  1321. if (IS_ERR(current_ev))
  1322. goto unlock;
  1323. break;
  1324. case WLAN_EID_MESH_CONFIG:
  1325. ismesh = true;
  1326. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  1327. break;
  1328. cfg = (u8 *)ie + 2;
  1329. memset(&iwe, 0, sizeof(iwe));
  1330. iwe.cmd = IWEVCUSTOM;
  1331. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  1332. "0x%02X", cfg[0]);
  1333. iwe.u.data.length = strlen(buf);
  1334. current_ev = iwe_stream_add_point_check(info,
  1335. current_ev,
  1336. end_buf,
  1337. &iwe, buf);
  1338. if (IS_ERR(current_ev))
  1339. goto unlock;
  1340. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  1341. cfg[1]);
  1342. iwe.u.data.length = strlen(buf);
  1343. current_ev = iwe_stream_add_point_check(info,
  1344. current_ev,
  1345. end_buf,
  1346. &iwe, buf);
  1347. if (IS_ERR(current_ev))
  1348. goto unlock;
  1349. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  1350. cfg[2]);
  1351. iwe.u.data.length = strlen(buf);
  1352. current_ev = iwe_stream_add_point_check(info,
  1353. current_ev,
  1354. end_buf,
  1355. &iwe, buf);
  1356. if (IS_ERR(current_ev))
  1357. goto unlock;
  1358. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  1359. iwe.u.data.length = strlen(buf);
  1360. current_ev = iwe_stream_add_point_check(info,
  1361. current_ev,
  1362. end_buf,
  1363. &iwe, buf);
  1364. if (IS_ERR(current_ev))
  1365. goto unlock;
  1366. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  1367. iwe.u.data.length = strlen(buf);
  1368. current_ev = iwe_stream_add_point_check(info,
  1369. current_ev,
  1370. end_buf,
  1371. &iwe, buf);
  1372. if (IS_ERR(current_ev))
  1373. goto unlock;
  1374. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  1375. iwe.u.data.length = strlen(buf);
  1376. current_ev = iwe_stream_add_point_check(info,
  1377. current_ev,
  1378. end_buf,
  1379. &iwe, buf);
  1380. if (IS_ERR(current_ev))
  1381. goto unlock;
  1382. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  1383. iwe.u.data.length = strlen(buf);
  1384. current_ev = iwe_stream_add_point_check(info,
  1385. current_ev,
  1386. end_buf,
  1387. &iwe, buf);
  1388. if (IS_ERR(current_ev))
  1389. goto unlock;
  1390. break;
  1391. case WLAN_EID_SUPP_RATES:
  1392. case WLAN_EID_EXT_SUPP_RATES:
  1393. /* display all supported rates in readable format */
  1394. p = current_ev + iwe_stream_lcp_len(info);
  1395. memset(&iwe, 0, sizeof(iwe));
  1396. iwe.cmd = SIOCGIWRATE;
  1397. /* Those two flags are ignored... */
  1398. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  1399. for (i = 0; i < ie[1]; i++) {
  1400. iwe.u.bitrate.value =
  1401. ((ie[i + 2] & 0x7f) * 500000);
  1402. tmp = p;
  1403. p = iwe_stream_add_value(info, current_ev, p,
  1404. end_buf, &iwe,
  1405. IW_EV_PARAM_LEN);
  1406. if (p == tmp) {
  1407. current_ev = ERR_PTR(-E2BIG);
  1408. goto unlock;
  1409. }
  1410. }
  1411. current_ev = p;
  1412. break;
  1413. }
  1414. rem -= ie[1] + 2;
  1415. ie += ie[1] + 2;
  1416. }
  1417. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  1418. ismesh) {
  1419. memset(&iwe, 0, sizeof(iwe));
  1420. iwe.cmd = SIOCGIWMODE;
  1421. if (ismesh)
  1422. iwe.u.mode = IW_MODE_MESH;
  1423. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  1424. iwe.u.mode = IW_MODE_MASTER;
  1425. else
  1426. iwe.u.mode = IW_MODE_ADHOC;
  1427. current_ev = iwe_stream_add_event_check(info, current_ev,
  1428. end_buf, &iwe,
  1429. IW_EV_UINT_LEN);
  1430. if (IS_ERR(current_ev))
  1431. goto unlock;
  1432. }
  1433. memset(&iwe, 0, sizeof(iwe));
  1434. iwe.cmd = IWEVCUSTOM;
  1435. sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
  1436. iwe.u.data.length = strlen(buf);
  1437. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  1438. &iwe, buf);
  1439. if (IS_ERR(current_ev))
  1440. goto unlock;
  1441. memset(&iwe, 0, sizeof(iwe));
  1442. iwe.cmd = IWEVCUSTOM;
  1443. sprintf(buf, " Last beacon: %ums ago",
  1444. elapsed_jiffies_msecs(bss->ts));
  1445. iwe.u.data.length = strlen(buf);
  1446. current_ev = iwe_stream_add_point_check(info, current_ev,
  1447. end_buf, &iwe, buf);
  1448. if (IS_ERR(current_ev))
  1449. goto unlock;
  1450. current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
  1451. unlock:
  1452. rcu_read_unlock();
  1453. return current_ev;
  1454. }
  1455. static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
  1456. struct iw_request_info *info,
  1457. char *buf, size_t len)
  1458. {
  1459. char *current_ev = buf;
  1460. char *end_buf = buf + len;
  1461. struct cfg80211_internal_bss *bss;
  1462. int err = 0;
  1463. spin_lock_bh(&rdev->bss_lock);
  1464. cfg80211_bss_expire(rdev);
  1465. list_for_each_entry(bss, &rdev->bss_list, list) {
  1466. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  1467. err = -E2BIG;
  1468. break;
  1469. }
  1470. current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
  1471. current_ev, end_buf);
  1472. if (IS_ERR(current_ev)) {
  1473. err = PTR_ERR(current_ev);
  1474. break;
  1475. }
  1476. }
  1477. spin_unlock_bh(&rdev->bss_lock);
  1478. if (err)
  1479. return err;
  1480. return current_ev - buf;
  1481. }
  1482. int cfg80211_wext_giwscan(struct net_device *dev,
  1483. struct iw_request_info *info,
  1484. struct iw_point *data, char *extra)
  1485. {
  1486. struct cfg80211_registered_device *rdev;
  1487. int res;
  1488. if (!netif_running(dev))
  1489. return -ENETDOWN;
  1490. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  1491. if (IS_ERR(rdev))
  1492. return PTR_ERR(rdev);
  1493. if (rdev->scan_req || rdev->scan_msg)
  1494. return -EAGAIN;
  1495. res = ieee80211_scan_results(rdev, info, extra, data->length);
  1496. data->length = 0;
  1497. if (res >= 0) {
  1498. data->length = res;
  1499. res = 0;
  1500. }
  1501. return res;
  1502. }
  1503. EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
  1504. #endif