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