scan.c 83 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * cfg80211 scan result handling
  4. *
  5. * Copyright 2008 Johannes Berg <johannes@sipsolutions.net>
  6. * Copyright 2013-2014 Intel Mobile Communications GmbH
  7. * Copyright 2016 Intel Deutschland GmbH
  8. * Copyright (C) 2018-2020 Intel Corporation
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/slab.h>
  12. #include <linux/module.h>
  13. #include <linux/netdevice.h>
  14. #include <linux/wireless.h>
  15. #include <linux/nl80211.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/crc32.h>
  18. #include <linux/bitfield.h>
  19. #include <net/arp.h>
  20. #include <net/cfg80211.h>
  21. #include <net/cfg80211-wext.h>
  22. #include <net/iw_handler.h>
  23. #include "core.h"
  24. #include "nl80211.h"
  25. #include "wext-compat.h"
  26. #include "rdev-ops.h"
  27. /**
  28. * DOC: BSS tree/list structure
  29. *
  30. * At the top level, the BSS list is kept in both a list in each
  31. * registered device (@bss_list) as well as an RB-tree for faster
  32. * lookup. In the RB-tree, entries can be looked up using their
  33. * channel, MESHID, MESHCONF (for MBSSes) or channel, BSSID, SSID
  34. * for other BSSes.
  35. *
  36. * Due to the possibility of hidden SSIDs, there's a second level
  37. * structure, the "hidden_list" and "hidden_beacon_bss" pointer.
  38. * The hidden_list connects all BSSes belonging to a single AP
  39. * that has a hidden SSID, and connects beacon and probe response
  40. * entries. For a probe response entry for a hidden SSID, the
  41. * hidden_beacon_bss pointer points to the BSS struct holding the
  42. * beacon's information.
  43. *
  44. * Reference counting is done for all these references except for
  45. * the hidden_list, so that a beacon BSS struct that is otherwise
  46. * not referenced has one reference for being on the bss_list and
  47. * one for each probe response entry that points to it using the
  48. * hidden_beacon_bss pointer. When a BSS struct that has such a
  49. * pointer is get/put, the refcount update is also propagated to
  50. * the referenced struct, this ensure that it cannot get removed
  51. * while somebody is using the probe response version.
  52. *
  53. * Note that the hidden_beacon_bss pointer never changes, due to
  54. * the reference counting. Therefore, no locking is needed for
  55. * it.
  56. *
  57. * Also note that the hidden_beacon_bss pointer is only relevant
  58. * if the driver uses something other than the IEs, e.g. private
  59. * data stored in the BSS struct, since the beacon IEs are
  60. * also linked into the probe response struct.
  61. */
  62. /*
  63. * Limit the number of BSS entries stored in mac80211. Each one is
  64. * a bit over 4k at most, so this limits to roughly 4-5M of memory.
  65. * If somebody wants to really attack this though, they'd likely
  66. * use small beacons, and only one type of frame, limiting each of
  67. * the entries to a much smaller size (in order to generate more
  68. * entries in total, so overhead is bigger.)
  69. */
  70. static int bss_entries_limit = 1000;
  71. module_param(bss_entries_limit, int, 0644);
  72. MODULE_PARM_DESC(bss_entries_limit,
  73. "limit to number of scan BSS entries (per wiphy, default 1000)");
  74. #define IEEE80211_SCAN_RESULT_EXPIRE (30 * HZ)
  75. /**
  76. * struct cfg80211_colocated_ap - colocated AP information
  77. *
  78. * @list: linked list to all colocated aPS
  79. * @bssid: BSSID of the reported AP
  80. * @ssid: SSID of the reported AP
  81. * @ssid_len: length of the ssid
  82. * @center_freq: frequency the reported AP is on
  83. * @unsolicited_probe: the reported AP is part of an ESS, where all the APs
  84. * that operate in the same channel as the reported AP and that might be
  85. * detected by a STA receiving this frame, are transmitting unsolicited
  86. * Probe Response frames every 20 TUs
  87. * @oct_recommended: OCT is recommended to exchange MMPDUs with the reported AP
  88. * @same_ssid: the reported AP has the same SSID as the reporting AP
  89. * @multi_bss: the reported AP is part of a multiple BSSID set
  90. * @transmitted_bssid: the reported AP is the transmitting BSSID
  91. * @colocated_ess: all the APs that share the same ESS as the reported AP are
  92. * colocated and can be discovered via legacy bands.
  93. * @short_ssid_valid: short_ssid is valid and can be used
  94. * @short_ssid: the short SSID for this SSID
  95. */
  96. struct cfg80211_colocated_ap {
  97. struct list_head list;
  98. u8 bssid[ETH_ALEN];
  99. u8 ssid[IEEE80211_MAX_SSID_LEN];
  100. size_t ssid_len;
  101. u32 short_ssid;
  102. u32 center_freq;
  103. u8 unsolicited_probe:1,
  104. oct_recommended:1,
  105. same_ssid:1,
  106. multi_bss:1,
  107. transmitted_bssid:1,
  108. colocated_ess:1,
  109. short_ssid_valid:1;
  110. };
  111. static void bss_free(struct cfg80211_internal_bss *bss)
  112. {
  113. struct cfg80211_bss_ies *ies;
  114. if (WARN_ON(atomic_read(&bss->hold)))
  115. return;
  116. ies = (void *)rcu_access_pointer(bss->pub.beacon_ies);
  117. if (ies && !bss->pub.hidden_beacon_bss)
  118. kfree_rcu(ies, rcu_head);
  119. ies = (void *)rcu_access_pointer(bss->pub.proberesp_ies);
  120. if (ies)
  121. kfree_rcu(ies, rcu_head);
  122. /*
  123. * This happens when the module is removed, it doesn't
  124. * really matter any more save for completeness
  125. */
  126. if (!list_empty(&bss->hidden_list))
  127. list_del(&bss->hidden_list);
  128. kfree(bss);
  129. }
  130. static inline void bss_ref_get(struct cfg80211_registered_device *rdev,
  131. struct cfg80211_internal_bss *bss)
  132. {
  133. lockdep_assert_held(&rdev->bss_lock);
  134. bss->refcount++;
  135. if (bss->pub.hidden_beacon_bss) {
  136. bss = container_of(bss->pub.hidden_beacon_bss,
  137. struct cfg80211_internal_bss,
  138. pub);
  139. bss->refcount++;
  140. }
  141. if (bss->pub.transmitted_bss) {
  142. bss = container_of(bss->pub.transmitted_bss,
  143. struct cfg80211_internal_bss,
  144. pub);
  145. bss->refcount++;
  146. }
  147. }
  148. static inline void bss_ref_put(struct cfg80211_registered_device *rdev,
  149. struct cfg80211_internal_bss *bss)
  150. {
  151. lockdep_assert_held(&rdev->bss_lock);
  152. if (bss->pub.hidden_beacon_bss) {
  153. struct cfg80211_internal_bss *hbss;
  154. hbss = container_of(bss->pub.hidden_beacon_bss,
  155. struct cfg80211_internal_bss,
  156. pub);
  157. hbss->refcount--;
  158. if (hbss->refcount == 0)
  159. bss_free(hbss);
  160. }
  161. if (bss->pub.transmitted_bss) {
  162. struct cfg80211_internal_bss *tbss;
  163. tbss = container_of(bss->pub.transmitted_bss,
  164. struct cfg80211_internal_bss,
  165. pub);
  166. tbss->refcount--;
  167. if (tbss->refcount == 0)
  168. bss_free(tbss);
  169. }
  170. bss->refcount--;
  171. if (bss->refcount == 0)
  172. bss_free(bss);
  173. }
  174. static bool __cfg80211_unlink_bss(struct cfg80211_registered_device *rdev,
  175. struct cfg80211_internal_bss *bss)
  176. {
  177. lockdep_assert_held(&rdev->bss_lock);
  178. if (!list_empty(&bss->hidden_list)) {
  179. /*
  180. * don't remove the beacon entry if it has
  181. * probe responses associated with it
  182. */
  183. if (!bss->pub.hidden_beacon_bss)
  184. return false;
  185. /*
  186. * if it's a probe response entry break its
  187. * link to the other entries in the group
  188. */
  189. list_del_init(&bss->hidden_list);
  190. }
  191. list_del_init(&bss->list);
  192. list_del_init(&bss->pub.nontrans_list);
  193. rb_erase(&bss->rbn, &rdev->bss_tree);
  194. rdev->bss_entries--;
  195. WARN_ONCE((rdev->bss_entries == 0) ^ list_empty(&rdev->bss_list),
  196. "rdev bss entries[%d]/list[empty:%d] corruption\n",
  197. rdev->bss_entries, list_empty(&rdev->bss_list));
  198. bss_ref_put(rdev, bss);
  199. return true;
  200. }
  201. bool cfg80211_is_element_inherited(const struct element *elem,
  202. const struct element *non_inherit_elem)
  203. {
  204. u8 id_len, ext_id_len, i, loop_len, id;
  205. const u8 *list;
  206. if (elem->id == WLAN_EID_MULTIPLE_BSSID)
  207. return false;
  208. if (!non_inherit_elem || non_inherit_elem->datalen < 2)
  209. return true;
  210. /*
  211. * non inheritance element format is:
  212. * ext ID (56) | IDs list len | list | extension IDs list len | list
  213. * Both lists are optional. Both lengths are mandatory.
  214. * This means valid length is:
  215. * elem_len = 1 (extension ID) + 2 (list len fields) + list lengths
  216. */
  217. id_len = non_inherit_elem->data[1];
  218. if (non_inherit_elem->datalen < 3 + id_len)
  219. return true;
  220. ext_id_len = non_inherit_elem->data[2 + id_len];
  221. if (non_inherit_elem->datalen < 3 + id_len + ext_id_len)
  222. return true;
  223. if (elem->id == WLAN_EID_EXTENSION) {
  224. if (!ext_id_len)
  225. return true;
  226. loop_len = ext_id_len;
  227. list = &non_inherit_elem->data[3 + id_len];
  228. id = elem->data[0];
  229. } else {
  230. if (!id_len)
  231. return true;
  232. loop_len = id_len;
  233. list = &non_inherit_elem->data[2];
  234. id = elem->id;
  235. }
  236. for (i = 0; i < loop_len; i++) {
  237. if (list[i] == id)
  238. return false;
  239. }
  240. return true;
  241. }
  242. EXPORT_SYMBOL(cfg80211_is_element_inherited);
  243. static size_t cfg80211_gen_new_ie(const u8 *ie, size_t ielen,
  244. const u8 *subelement, size_t subie_len,
  245. u8 *new_ie, gfp_t gfp)
  246. {
  247. u8 *pos, *tmp;
  248. const u8 *tmp_old, *tmp_new;
  249. const struct element *non_inherit_elem;
  250. u8 *sub_copy;
  251. /* copy subelement as we need to change its content to
  252. * mark an ie after it is processed.
  253. */
  254. sub_copy = kmemdup(subelement, subie_len, gfp);
  255. if (!sub_copy)
  256. return 0;
  257. pos = &new_ie[0];
  258. /* set new ssid */
  259. tmp_new = cfg80211_find_ie(WLAN_EID_SSID, sub_copy, subie_len);
  260. if (tmp_new) {
  261. memcpy(pos, tmp_new, tmp_new[1] + 2);
  262. pos += (tmp_new[1] + 2);
  263. }
  264. /* get non inheritance list if exists */
  265. non_inherit_elem =
  266. cfg80211_find_ext_elem(WLAN_EID_EXT_NON_INHERITANCE,
  267. sub_copy, subie_len);
  268. /* go through IEs in ie (skip SSID) and subelement,
  269. * merge them into new_ie
  270. */
  271. tmp_old = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
  272. tmp_old = (tmp_old) ? tmp_old + tmp_old[1] + 2 : ie;
  273. while (tmp_old + tmp_old[1] + 2 - ie <= ielen) {
  274. if (tmp_old[0] == 0) {
  275. tmp_old++;
  276. continue;
  277. }
  278. if (tmp_old[0] == WLAN_EID_EXTENSION)
  279. tmp = (u8 *)cfg80211_find_ext_ie(tmp_old[2], sub_copy,
  280. subie_len);
  281. else
  282. tmp = (u8 *)cfg80211_find_ie(tmp_old[0], sub_copy,
  283. subie_len);
  284. if (!tmp) {
  285. const struct element *old_elem = (void *)tmp_old;
  286. /* ie in old ie but not in subelement */
  287. if (cfg80211_is_element_inherited(old_elem,
  288. non_inherit_elem)) {
  289. memcpy(pos, tmp_old, tmp_old[1] + 2);
  290. pos += tmp_old[1] + 2;
  291. }
  292. } else {
  293. /* ie in transmitting ie also in subelement,
  294. * copy from subelement and flag the ie in subelement
  295. * as copied (by setting eid field to WLAN_EID_SSID,
  296. * which is skipped anyway).
  297. * For vendor ie, compare OUI + type + subType to
  298. * determine if they are the same ie.
  299. */
  300. if (tmp_old[0] == WLAN_EID_VENDOR_SPECIFIC) {
  301. if (!memcmp(tmp_old + 2, tmp + 2, 5)) {
  302. /* same vendor ie, copy from
  303. * subelement
  304. */
  305. memcpy(pos, tmp, tmp[1] + 2);
  306. pos += tmp[1] + 2;
  307. tmp[0] = WLAN_EID_SSID;
  308. } else {
  309. memcpy(pos, tmp_old, tmp_old[1] + 2);
  310. pos += tmp_old[1] + 2;
  311. }
  312. } else {
  313. /* copy ie from subelement into new ie */
  314. memcpy(pos, tmp, tmp[1] + 2);
  315. pos += tmp[1] + 2;
  316. tmp[0] = WLAN_EID_SSID;
  317. }
  318. }
  319. if (tmp_old + tmp_old[1] + 2 - ie == ielen)
  320. break;
  321. tmp_old += tmp_old[1] + 2;
  322. }
  323. /* go through subelement again to check if there is any ie not
  324. * copied to new ie, skip ssid, capability, bssid-index ie
  325. */
  326. tmp_new = sub_copy;
  327. while (tmp_new + tmp_new[1] + 2 - sub_copy <= subie_len) {
  328. if (!(tmp_new[0] == WLAN_EID_NON_TX_BSSID_CAP ||
  329. tmp_new[0] == WLAN_EID_SSID)) {
  330. memcpy(pos, tmp_new, tmp_new[1] + 2);
  331. pos += tmp_new[1] + 2;
  332. }
  333. if (tmp_new + tmp_new[1] + 2 - sub_copy == subie_len)
  334. break;
  335. tmp_new += tmp_new[1] + 2;
  336. }
  337. kfree(sub_copy);
  338. return pos - new_ie;
  339. }
  340. static bool is_bss(struct cfg80211_bss *a, const u8 *bssid,
  341. const u8 *ssid, size_t ssid_len)
  342. {
  343. const struct cfg80211_bss_ies *ies;
  344. const u8 *ssidie;
  345. if (bssid && !ether_addr_equal(a->bssid, bssid))
  346. return false;
  347. if (!ssid)
  348. return true;
  349. ies = rcu_access_pointer(a->ies);
  350. if (!ies)
  351. return false;
  352. ssidie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  353. if (!ssidie)
  354. return false;
  355. if (ssidie[1] != ssid_len)
  356. return false;
  357. return memcmp(ssidie + 2, ssid, ssid_len) == 0;
  358. }
  359. static int
  360. cfg80211_add_nontrans_list(struct cfg80211_bss *trans_bss,
  361. struct cfg80211_bss *nontrans_bss)
  362. {
  363. const u8 *ssid;
  364. size_t ssid_len;
  365. struct cfg80211_bss *bss = NULL;
  366. rcu_read_lock();
  367. ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
  368. if (!ssid) {
  369. rcu_read_unlock();
  370. return -EINVAL;
  371. }
  372. ssid_len = ssid[1];
  373. ssid = ssid + 2;
  374. /* check if nontrans_bss is in the list */
  375. list_for_each_entry(bss, &trans_bss->nontrans_list, nontrans_list) {
  376. if (is_bss(bss, nontrans_bss->bssid, ssid, ssid_len)) {
  377. rcu_read_unlock();
  378. return 0;
  379. }
  380. }
  381. rcu_read_unlock();
  382. /* add to the list */
  383. list_add_tail(&nontrans_bss->nontrans_list, &trans_bss->nontrans_list);
  384. return 0;
  385. }
  386. static void __cfg80211_bss_expire(struct cfg80211_registered_device *rdev,
  387. unsigned long expire_time)
  388. {
  389. struct cfg80211_internal_bss *bss, *tmp;
  390. bool expired = false;
  391. lockdep_assert_held(&rdev->bss_lock);
  392. list_for_each_entry_safe(bss, tmp, &rdev->bss_list, list) {
  393. if (atomic_read(&bss->hold))
  394. continue;
  395. if (!time_after(expire_time, bss->ts))
  396. continue;
  397. if (__cfg80211_unlink_bss(rdev, bss))
  398. expired = true;
  399. }
  400. if (expired)
  401. rdev->bss_generation++;
  402. }
  403. static bool cfg80211_bss_expire_oldest(struct cfg80211_registered_device *rdev)
  404. {
  405. struct cfg80211_internal_bss *bss, *oldest = NULL;
  406. bool ret;
  407. lockdep_assert_held(&rdev->bss_lock);
  408. list_for_each_entry(bss, &rdev->bss_list, list) {
  409. if (atomic_read(&bss->hold))
  410. continue;
  411. if (!list_empty(&bss->hidden_list) &&
  412. !bss->pub.hidden_beacon_bss)
  413. continue;
  414. if (oldest && time_before(oldest->ts, bss->ts))
  415. continue;
  416. oldest = bss;
  417. }
  418. if (WARN_ON(!oldest))
  419. return false;
  420. /*
  421. * The callers make sure to increase rdev->bss_generation if anything
  422. * gets removed (and a new entry added), so there's no need to also do
  423. * it here.
  424. */
  425. ret = __cfg80211_unlink_bss(rdev, oldest);
  426. WARN_ON(!ret);
  427. return ret;
  428. }
  429. static u8 cfg80211_parse_bss_param(u8 data,
  430. struct cfg80211_colocated_ap *coloc_ap)
  431. {
  432. coloc_ap->oct_recommended =
  433. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_OCT_RECOMMENDED);
  434. coloc_ap->same_ssid =
  435. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_SAME_SSID);
  436. coloc_ap->multi_bss =
  437. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_MULTI_BSSID);
  438. coloc_ap->transmitted_bssid =
  439. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_TRANSMITTED_BSSID);
  440. coloc_ap->unsolicited_probe =
  441. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_PROBE_ACTIVE);
  442. coloc_ap->colocated_ess =
  443. u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_ESS);
  444. return u8_get_bits(data, IEEE80211_RNR_TBTT_PARAMS_COLOC_AP);
  445. }
  446. static int cfg80211_calc_short_ssid(const struct cfg80211_bss_ies *ies,
  447. const struct element **elem, u32 *s_ssid)
  448. {
  449. *elem = cfg80211_find_elem(WLAN_EID_SSID, ies->data, ies->len);
  450. if (!*elem || (*elem)->datalen > IEEE80211_MAX_SSID_LEN)
  451. return -EINVAL;
  452. *s_ssid = ~crc32_le(~0, (*elem)->data, (*elem)->datalen);
  453. return 0;
  454. }
  455. static void cfg80211_free_coloc_ap_list(struct list_head *coloc_ap_list)
  456. {
  457. struct cfg80211_colocated_ap *ap, *tmp_ap;
  458. list_for_each_entry_safe(ap, tmp_ap, coloc_ap_list, list) {
  459. list_del(&ap->list);
  460. kfree(ap);
  461. }
  462. }
  463. static int cfg80211_parse_ap_info(struct cfg80211_colocated_ap *entry,
  464. const u8 *pos, u8 length,
  465. const struct element *ssid_elem,
  466. int s_ssid_tmp)
  467. {
  468. /* skip the TBTT offset */
  469. pos++;
  470. memcpy(entry->bssid, pos, ETH_ALEN);
  471. pos += ETH_ALEN;
  472. if (length == IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM) {
  473. memcpy(&entry->short_ssid, pos,
  474. sizeof(entry->short_ssid));
  475. entry->short_ssid_valid = true;
  476. pos += 4;
  477. }
  478. /* skip non colocated APs */
  479. if (!cfg80211_parse_bss_param(*pos, entry))
  480. return -EINVAL;
  481. pos++;
  482. if (length == IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM) {
  483. /*
  484. * no information about the short ssid. Consider the entry valid
  485. * for now. It would later be dropped in case there are explicit
  486. * SSIDs that need to be matched
  487. */
  488. if (!entry->same_ssid)
  489. return 0;
  490. }
  491. if (entry->same_ssid) {
  492. entry->short_ssid = s_ssid_tmp;
  493. entry->short_ssid_valid = true;
  494. /*
  495. * This is safe because we validate datalen in
  496. * cfg80211_parse_colocated_ap(), before calling this
  497. * function.
  498. */
  499. memcpy(&entry->ssid, &ssid_elem->data,
  500. ssid_elem->datalen);
  501. entry->ssid_len = ssid_elem->datalen;
  502. }
  503. return 0;
  504. }
  505. static int cfg80211_parse_colocated_ap(const struct cfg80211_bss_ies *ies,
  506. struct list_head *list)
  507. {
  508. struct ieee80211_neighbor_ap_info *ap_info;
  509. const struct element *elem, *ssid_elem;
  510. const u8 *pos, *end;
  511. u32 s_ssid_tmp;
  512. int n_coloc = 0, ret;
  513. LIST_HEAD(ap_list);
  514. elem = cfg80211_find_elem(WLAN_EID_REDUCED_NEIGHBOR_REPORT, ies->data,
  515. ies->len);
  516. if (!elem || elem->datalen > IEEE80211_MAX_SSID_LEN)
  517. return 0;
  518. pos = elem->data;
  519. end = pos + elem->datalen;
  520. ret = cfg80211_calc_short_ssid(ies, &ssid_elem, &s_ssid_tmp);
  521. if (ret)
  522. return ret;
  523. /* RNR IE may contain more than one NEIGHBOR_AP_INFO */
  524. while (pos + sizeof(*ap_info) <= end) {
  525. enum nl80211_band band;
  526. int freq;
  527. u8 length, i, count;
  528. ap_info = (void *)pos;
  529. count = u8_get_bits(ap_info->tbtt_info_hdr,
  530. IEEE80211_AP_INFO_TBTT_HDR_COUNT) + 1;
  531. length = ap_info->tbtt_info_len;
  532. pos += sizeof(*ap_info);
  533. if (!ieee80211_operating_class_to_band(ap_info->op_class,
  534. &band))
  535. break;
  536. freq = ieee80211_channel_to_frequency(ap_info->channel, band);
  537. if (end - pos < count * ap_info->tbtt_info_len)
  538. break;
  539. /*
  540. * TBTT info must include bss param + BSSID +
  541. * (short SSID or same_ssid bit to be set).
  542. * ignore other options, and move to the
  543. * next AP info
  544. */
  545. if (band != NL80211_BAND_6GHZ ||
  546. (length != IEEE80211_TBTT_INFO_OFFSET_BSSID_BSS_PARAM &&
  547. length < IEEE80211_TBTT_INFO_OFFSET_BSSID_SSSID_BSS_PARAM)) {
  548. pos += count * ap_info->tbtt_info_len;
  549. continue;
  550. }
  551. for (i = 0; i < count; i++) {
  552. struct cfg80211_colocated_ap *entry;
  553. entry = kzalloc(sizeof(*entry) + IEEE80211_MAX_SSID_LEN,
  554. GFP_ATOMIC);
  555. if (!entry)
  556. break;
  557. entry->center_freq = freq;
  558. if (!cfg80211_parse_ap_info(entry, pos, length,
  559. ssid_elem, s_ssid_tmp)) {
  560. n_coloc++;
  561. list_add_tail(&entry->list, &ap_list);
  562. } else {
  563. kfree(entry);
  564. }
  565. pos += ap_info->tbtt_info_len;
  566. }
  567. }
  568. if (pos != end) {
  569. cfg80211_free_coloc_ap_list(&ap_list);
  570. return 0;
  571. }
  572. list_splice_tail(&ap_list, list);
  573. return n_coloc;
  574. }
  575. static void cfg80211_scan_req_add_chan(struct cfg80211_scan_request *request,
  576. struct ieee80211_channel *chan,
  577. bool add_to_6ghz)
  578. {
  579. int i;
  580. u32 n_channels = request->n_channels;
  581. struct cfg80211_scan_6ghz_params *params =
  582. &request->scan_6ghz_params[request->n_6ghz_params];
  583. for (i = 0; i < n_channels; i++) {
  584. if (request->channels[i] == chan) {
  585. if (add_to_6ghz)
  586. params->channel_idx = i;
  587. return;
  588. }
  589. }
  590. request->channels[n_channels] = chan;
  591. if (add_to_6ghz)
  592. request->scan_6ghz_params[request->n_6ghz_params].channel_idx =
  593. n_channels;
  594. request->n_channels++;
  595. }
  596. static bool cfg80211_find_ssid_match(struct cfg80211_colocated_ap *ap,
  597. struct cfg80211_scan_request *request)
  598. {
  599. int i;
  600. u32 s_ssid;
  601. for (i = 0; i < request->n_ssids; i++) {
  602. /* wildcard ssid in the scan request */
  603. if (!request->ssids[i].ssid_len) {
  604. if (ap->multi_bss && !ap->transmitted_bssid)
  605. continue;
  606. return true;
  607. }
  608. if (ap->ssid_len &&
  609. ap->ssid_len == request->ssids[i].ssid_len) {
  610. if (!memcmp(request->ssids[i].ssid, ap->ssid,
  611. ap->ssid_len))
  612. return true;
  613. } else if (ap->short_ssid_valid) {
  614. s_ssid = ~crc32_le(~0, request->ssids[i].ssid,
  615. request->ssids[i].ssid_len);
  616. if (ap->short_ssid == s_ssid)
  617. return true;
  618. }
  619. }
  620. return false;
  621. }
  622. static int cfg80211_scan_6ghz(struct cfg80211_registered_device *rdev)
  623. {
  624. u8 i;
  625. struct cfg80211_colocated_ap *ap;
  626. int n_channels, count = 0, err;
  627. struct cfg80211_scan_request *request, *rdev_req = rdev->scan_req;
  628. LIST_HEAD(coloc_ap_list);
  629. bool need_scan_psc;
  630. const struct ieee80211_sband_iftype_data *iftd;
  631. rdev_req->scan_6ghz = true;
  632. if (!rdev->wiphy.bands[NL80211_BAND_6GHZ])
  633. return -EOPNOTSUPP;
  634. iftd = ieee80211_get_sband_iftype_data(rdev->wiphy.bands[NL80211_BAND_6GHZ],
  635. rdev_req->wdev->iftype);
  636. if (!iftd || !iftd->he_cap.has_he)
  637. return -EOPNOTSUPP;
  638. n_channels = rdev->wiphy.bands[NL80211_BAND_6GHZ]->n_channels;
  639. if (rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ) {
  640. struct cfg80211_internal_bss *intbss;
  641. spin_lock_bh(&rdev->bss_lock);
  642. list_for_each_entry(intbss, &rdev->bss_list, list) {
  643. struct cfg80211_bss *res = &intbss->pub;
  644. const struct cfg80211_bss_ies *ies;
  645. ies = rcu_access_pointer(res->ies);
  646. count += cfg80211_parse_colocated_ap(ies,
  647. &coloc_ap_list);
  648. }
  649. spin_unlock_bh(&rdev->bss_lock);
  650. }
  651. request = kzalloc(struct_size(request, channels, n_channels) +
  652. sizeof(*request->scan_6ghz_params) * count,
  653. GFP_KERNEL);
  654. if (!request) {
  655. cfg80211_free_coloc_ap_list(&coloc_ap_list);
  656. return -ENOMEM;
  657. }
  658. *request = *rdev_req;
  659. request->n_channels = 0;
  660. request->scan_6ghz_params =
  661. (void *)&request->channels[n_channels];
  662. /*
  663. * PSC channels should not be scanned if all the reported co-located APs
  664. * are indicating that all APs in the same ESS are co-located
  665. */
  666. if (count) {
  667. need_scan_psc = false;
  668. list_for_each_entry(ap, &coloc_ap_list, list) {
  669. if (!ap->colocated_ess) {
  670. need_scan_psc = true;
  671. break;
  672. }
  673. }
  674. } else {
  675. need_scan_psc = true;
  676. }
  677. /*
  678. * add to the scan request the channels that need to be scanned
  679. * regardless of the collocated APs (PSC channels or all channels
  680. * in case that NL80211_SCAN_FLAG_COLOCATED_6GHZ is not set)
  681. */
  682. for (i = 0; i < rdev_req->n_channels; i++) {
  683. if (rdev_req->channels[i]->band == NL80211_BAND_6GHZ &&
  684. ((need_scan_psc &&
  685. cfg80211_channel_is_psc(rdev_req->channels[i])) ||
  686. !(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))) {
  687. cfg80211_scan_req_add_chan(request,
  688. rdev_req->channels[i],
  689. false);
  690. }
  691. }
  692. if (!(rdev_req->flags & NL80211_SCAN_FLAG_COLOCATED_6GHZ))
  693. goto skip;
  694. list_for_each_entry(ap, &coloc_ap_list, list) {
  695. bool found = false;
  696. struct cfg80211_scan_6ghz_params *scan_6ghz_params =
  697. &request->scan_6ghz_params[request->n_6ghz_params];
  698. struct ieee80211_channel *chan =
  699. ieee80211_get_channel(&rdev->wiphy, ap->center_freq);
  700. if (!chan || chan->flags & IEEE80211_CHAN_DISABLED)
  701. continue;
  702. for (i = 0; i < rdev_req->n_channels; i++) {
  703. if (rdev_req->channels[i] == chan)
  704. found = true;
  705. }
  706. if (!found)
  707. continue;
  708. if (request->n_ssids > 0 &&
  709. !cfg80211_find_ssid_match(ap, request))
  710. continue;
  711. if (!request->n_ssids && ap->multi_bss && !ap->transmitted_bssid)
  712. continue;
  713. cfg80211_scan_req_add_chan(request, chan, true);
  714. memcpy(scan_6ghz_params->bssid, ap->bssid, ETH_ALEN);
  715. scan_6ghz_params->short_ssid = ap->short_ssid;
  716. scan_6ghz_params->short_ssid_valid = ap->short_ssid_valid;
  717. scan_6ghz_params->unsolicited_probe = ap->unsolicited_probe;
  718. /*
  719. * If a PSC channel is added to the scan and 'need_scan_psc' is
  720. * set to false, then all the APs that the scan logic is
  721. * interested with on the channel are collocated and thus there
  722. * is no need to perform the initial PSC channel listen.
  723. */
  724. if (cfg80211_channel_is_psc(chan) && !need_scan_psc)
  725. scan_6ghz_params->psc_no_listen = true;
  726. request->n_6ghz_params++;
  727. }
  728. skip:
  729. cfg80211_free_coloc_ap_list(&coloc_ap_list);
  730. if (request->n_channels) {
  731. struct cfg80211_scan_request *old = rdev->int_scan_req;
  732. rdev->int_scan_req = request;
  733. /*
  734. * If this scan follows a previous scan, save the scan start
  735. * info from the first part of the scan
  736. */
  737. if (old)
  738. rdev->int_scan_req->info = old->info;
  739. err = rdev_scan(rdev, request);
  740. if (err) {
  741. rdev->int_scan_req = old;
  742. kfree(request);
  743. } else {
  744. kfree(old);
  745. }
  746. return err;
  747. }
  748. kfree(request);
  749. return -EINVAL;
  750. }
  751. int cfg80211_scan(struct cfg80211_registered_device *rdev)
  752. {
  753. struct cfg80211_scan_request *request;
  754. struct cfg80211_scan_request *rdev_req = rdev->scan_req;
  755. u32 n_channels = 0, idx, i;
  756. if (!(rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ))
  757. return rdev_scan(rdev, rdev_req);
  758. for (i = 0; i < rdev_req->n_channels; i++) {
  759. if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
  760. n_channels++;
  761. }
  762. if (!n_channels)
  763. return cfg80211_scan_6ghz(rdev);
  764. request = kzalloc(struct_size(request, channels, n_channels),
  765. GFP_KERNEL);
  766. if (!request)
  767. return -ENOMEM;
  768. *request = *rdev_req;
  769. request->n_channels = n_channels;
  770. for (i = idx = 0; i < rdev_req->n_channels; i++) {
  771. if (rdev_req->channels[i]->band != NL80211_BAND_6GHZ)
  772. request->channels[idx++] = rdev_req->channels[i];
  773. }
  774. rdev_req->scan_6ghz = false;
  775. rdev->int_scan_req = request;
  776. return rdev_scan(rdev, request);
  777. }
  778. void ___cfg80211_scan_done(struct cfg80211_registered_device *rdev,
  779. bool send_message)
  780. {
  781. struct cfg80211_scan_request *request, *rdev_req;
  782. struct wireless_dev *wdev;
  783. struct sk_buff *msg;
  784. #ifdef CONFIG_CFG80211_WEXT
  785. union iwreq_data wrqu;
  786. #endif
  787. ASSERT_RTNL();
  788. if (rdev->scan_msg) {
  789. nl80211_send_scan_msg(rdev, rdev->scan_msg);
  790. rdev->scan_msg = NULL;
  791. return;
  792. }
  793. rdev_req = rdev->scan_req;
  794. if (!rdev_req)
  795. return;
  796. wdev = rdev_req->wdev;
  797. request = rdev->int_scan_req ? rdev->int_scan_req : rdev_req;
  798. if (wdev_running(wdev) &&
  799. (rdev->wiphy.flags & WIPHY_FLAG_SPLIT_SCAN_6GHZ) &&
  800. !rdev_req->scan_6ghz && !request->info.aborted &&
  801. !cfg80211_scan_6ghz(rdev))
  802. return;
  803. /*
  804. * This must be before sending the other events!
  805. * Otherwise, wpa_supplicant gets completely confused with
  806. * wext events.
  807. */
  808. if (wdev->netdev)
  809. cfg80211_sme_scan_done(wdev->netdev);
  810. if (!request->info.aborted &&
  811. request->flags & NL80211_SCAN_FLAG_FLUSH) {
  812. /* flush entries from previous scans */
  813. spin_lock_bh(&rdev->bss_lock);
  814. __cfg80211_bss_expire(rdev, request->scan_start);
  815. spin_unlock_bh(&rdev->bss_lock);
  816. }
  817. msg = nl80211_build_scan_msg(rdev, wdev, request->info.aborted);
  818. #ifdef CONFIG_CFG80211_WEXT
  819. if (wdev->netdev && !request->info.aborted) {
  820. memset(&wrqu, 0, sizeof(wrqu));
  821. wireless_send_event(wdev->netdev, SIOCGIWSCAN, &wrqu, NULL);
  822. }
  823. #endif
  824. if (wdev->netdev)
  825. dev_put(wdev->netdev);
  826. kfree(rdev->int_scan_req);
  827. rdev->int_scan_req = NULL;
  828. kfree(rdev->scan_req);
  829. rdev->scan_req = NULL;
  830. if (!send_message)
  831. rdev->scan_msg = msg;
  832. else
  833. nl80211_send_scan_msg(rdev, msg);
  834. }
  835. void __cfg80211_scan_done(struct work_struct *wk)
  836. {
  837. struct cfg80211_registered_device *rdev;
  838. rdev = container_of(wk, struct cfg80211_registered_device,
  839. scan_done_wk);
  840. rtnl_lock();
  841. ___cfg80211_scan_done(rdev, true);
  842. rtnl_unlock();
  843. }
  844. void cfg80211_scan_done(struct cfg80211_scan_request *request,
  845. struct cfg80211_scan_info *info)
  846. {
  847. struct cfg80211_scan_info old_info = request->info;
  848. trace_cfg80211_scan_done(request, info);
  849. WARN_ON(request != wiphy_to_rdev(request->wiphy)->scan_req &&
  850. request != wiphy_to_rdev(request->wiphy)->int_scan_req);
  851. request->info = *info;
  852. /*
  853. * In case the scan is split, the scan_start_tsf and tsf_bssid should
  854. * be of the first part. In such a case old_info.scan_start_tsf should
  855. * be non zero.
  856. */
  857. if (request->scan_6ghz && old_info.scan_start_tsf) {
  858. request->info.scan_start_tsf = old_info.scan_start_tsf;
  859. memcpy(request->info.tsf_bssid, old_info.tsf_bssid,
  860. sizeof(request->info.tsf_bssid));
  861. }
  862. request->notified = true;
  863. queue_work(cfg80211_wq, &wiphy_to_rdev(request->wiphy)->scan_done_wk);
  864. }
  865. EXPORT_SYMBOL(cfg80211_scan_done);
  866. void cfg80211_add_sched_scan_req(struct cfg80211_registered_device *rdev,
  867. struct cfg80211_sched_scan_request *req)
  868. {
  869. ASSERT_RTNL();
  870. list_add_rcu(&req->list, &rdev->sched_scan_req_list);
  871. }
  872. static void cfg80211_del_sched_scan_req(struct cfg80211_registered_device *rdev,
  873. struct cfg80211_sched_scan_request *req)
  874. {
  875. ASSERT_RTNL();
  876. list_del_rcu(&req->list);
  877. kfree_rcu(req, rcu_head);
  878. }
  879. static struct cfg80211_sched_scan_request *
  880. cfg80211_find_sched_scan_req(struct cfg80211_registered_device *rdev, u64 reqid)
  881. {
  882. struct cfg80211_sched_scan_request *pos;
  883. list_for_each_entry_rcu(pos, &rdev->sched_scan_req_list, list,
  884. lockdep_rtnl_is_held()) {
  885. if (pos->reqid == reqid)
  886. return pos;
  887. }
  888. return NULL;
  889. }
  890. /*
  891. * Determines if a scheduled scan request can be handled. When a legacy
  892. * scheduled scan is running no other scheduled scan is allowed regardless
  893. * whether the request is for legacy or multi-support scan. When a multi-support
  894. * scheduled scan is running a request for legacy scan is not allowed. In this
  895. * case a request for multi-support scan can be handled if resources are
  896. * available, ie. struct wiphy::max_sched_scan_reqs limit is not yet reached.
  897. */
  898. int cfg80211_sched_scan_req_possible(struct cfg80211_registered_device *rdev,
  899. bool want_multi)
  900. {
  901. struct cfg80211_sched_scan_request *pos;
  902. int i = 0;
  903. list_for_each_entry(pos, &rdev->sched_scan_req_list, list) {
  904. /* request id zero means legacy in progress */
  905. if (!i && !pos->reqid)
  906. return -EINPROGRESS;
  907. i++;
  908. }
  909. if (i) {
  910. /* no legacy allowed when multi request(s) are active */
  911. if (!want_multi)
  912. return -EINPROGRESS;
  913. /* resource limit reached */
  914. if (i == rdev->wiphy.max_sched_scan_reqs)
  915. return -ENOSPC;
  916. }
  917. return 0;
  918. }
  919. void cfg80211_sched_scan_results_wk(struct work_struct *work)
  920. {
  921. struct cfg80211_registered_device *rdev;
  922. struct cfg80211_sched_scan_request *req, *tmp;
  923. rdev = container_of(work, struct cfg80211_registered_device,
  924. sched_scan_res_wk);
  925. rtnl_lock();
  926. list_for_each_entry_safe(req, tmp, &rdev->sched_scan_req_list, list) {
  927. if (req->report_results) {
  928. req->report_results = false;
  929. if (req->flags & NL80211_SCAN_FLAG_FLUSH) {
  930. /* flush entries from previous scans */
  931. spin_lock_bh(&rdev->bss_lock);
  932. __cfg80211_bss_expire(rdev, req->scan_start);
  933. spin_unlock_bh(&rdev->bss_lock);
  934. req->scan_start = jiffies;
  935. }
  936. nl80211_send_sched_scan(req,
  937. NL80211_CMD_SCHED_SCAN_RESULTS);
  938. }
  939. }
  940. rtnl_unlock();
  941. }
  942. void cfg80211_sched_scan_results(struct wiphy *wiphy, u64 reqid)
  943. {
  944. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  945. struct cfg80211_sched_scan_request *request;
  946. trace_cfg80211_sched_scan_results(wiphy, reqid);
  947. /* ignore if we're not scanning */
  948. rcu_read_lock();
  949. request = cfg80211_find_sched_scan_req(rdev, reqid);
  950. if (request) {
  951. request->report_results = true;
  952. queue_work(cfg80211_wq, &rdev->sched_scan_res_wk);
  953. }
  954. rcu_read_unlock();
  955. }
  956. EXPORT_SYMBOL(cfg80211_sched_scan_results);
  957. void cfg80211_sched_scan_stopped_rtnl(struct wiphy *wiphy, u64 reqid)
  958. {
  959. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  960. ASSERT_RTNL();
  961. trace_cfg80211_sched_scan_stopped(wiphy, reqid);
  962. __cfg80211_stop_sched_scan(rdev, reqid, true);
  963. }
  964. EXPORT_SYMBOL(cfg80211_sched_scan_stopped_rtnl);
  965. void cfg80211_sched_scan_stopped(struct wiphy *wiphy, u64 reqid)
  966. {
  967. rtnl_lock();
  968. cfg80211_sched_scan_stopped_rtnl(wiphy, reqid);
  969. rtnl_unlock();
  970. }
  971. EXPORT_SYMBOL(cfg80211_sched_scan_stopped);
  972. int cfg80211_stop_sched_scan_req(struct cfg80211_registered_device *rdev,
  973. struct cfg80211_sched_scan_request *req,
  974. bool driver_initiated)
  975. {
  976. ASSERT_RTNL();
  977. if (!driver_initiated) {
  978. int err = rdev_sched_scan_stop(rdev, req->dev, req->reqid);
  979. if (err)
  980. return err;
  981. }
  982. nl80211_send_sched_scan(req, NL80211_CMD_SCHED_SCAN_STOPPED);
  983. cfg80211_del_sched_scan_req(rdev, req);
  984. return 0;
  985. }
  986. int __cfg80211_stop_sched_scan(struct cfg80211_registered_device *rdev,
  987. u64 reqid, bool driver_initiated)
  988. {
  989. struct cfg80211_sched_scan_request *sched_scan_req;
  990. ASSERT_RTNL();
  991. sched_scan_req = cfg80211_find_sched_scan_req(rdev, reqid);
  992. if (!sched_scan_req)
  993. return -ENOENT;
  994. return cfg80211_stop_sched_scan_req(rdev, sched_scan_req,
  995. driver_initiated);
  996. }
  997. void cfg80211_bss_age(struct cfg80211_registered_device *rdev,
  998. unsigned long age_secs)
  999. {
  1000. struct cfg80211_internal_bss *bss;
  1001. unsigned long age_jiffies = msecs_to_jiffies(age_secs * MSEC_PER_SEC);
  1002. spin_lock_bh(&rdev->bss_lock);
  1003. list_for_each_entry(bss, &rdev->bss_list, list)
  1004. bss->ts -= age_jiffies;
  1005. spin_unlock_bh(&rdev->bss_lock);
  1006. }
  1007. void cfg80211_bss_expire(struct cfg80211_registered_device *rdev)
  1008. {
  1009. __cfg80211_bss_expire(rdev, jiffies - IEEE80211_SCAN_RESULT_EXPIRE);
  1010. }
  1011. void cfg80211_bss_flush(struct wiphy *wiphy)
  1012. {
  1013. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1014. spin_lock_bh(&rdev->bss_lock);
  1015. __cfg80211_bss_expire(rdev, jiffies);
  1016. spin_unlock_bh(&rdev->bss_lock);
  1017. }
  1018. EXPORT_SYMBOL(cfg80211_bss_flush);
  1019. const struct element *
  1020. cfg80211_find_elem_match(u8 eid, const u8 *ies, unsigned int len,
  1021. const u8 *match, unsigned int match_len,
  1022. unsigned int match_offset)
  1023. {
  1024. const struct element *elem;
  1025. for_each_element_id(elem, eid, ies, len) {
  1026. if (elem->datalen >= match_offset + match_len &&
  1027. !memcmp(elem->data + match_offset, match, match_len))
  1028. return elem;
  1029. }
  1030. return NULL;
  1031. }
  1032. EXPORT_SYMBOL(cfg80211_find_elem_match);
  1033. const struct element *cfg80211_find_vendor_elem(unsigned int oui, int oui_type,
  1034. const u8 *ies,
  1035. unsigned int len)
  1036. {
  1037. const struct element *elem;
  1038. u8 match[] = { oui >> 16, oui >> 8, oui, oui_type };
  1039. int match_len = (oui_type < 0) ? 3 : sizeof(match);
  1040. if (WARN_ON(oui_type > 0xff))
  1041. return NULL;
  1042. elem = cfg80211_find_elem_match(WLAN_EID_VENDOR_SPECIFIC, ies, len,
  1043. match, match_len, 0);
  1044. if (!elem || elem->datalen < 4)
  1045. return NULL;
  1046. return elem;
  1047. }
  1048. EXPORT_SYMBOL(cfg80211_find_vendor_elem);
  1049. /**
  1050. * enum bss_compare_mode - BSS compare mode
  1051. * @BSS_CMP_REGULAR: regular compare mode (for insertion and normal find)
  1052. * @BSS_CMP_HIDE_ZLEN: find hidden SSID with zero-length mode
  1053. * @BSS_CMP_HIDE_NUL: find hidden SSID with NUL-ed out mode
  1054. */
  1055. enum bss_compare_mode {
  1056. BSS_CMP_REGULAR,
  1057. BSS_CMP_HIDE_ZLEN,
  1058. BSS_CMP_HIDE_NUL,
  1059. };
  1060. static int cmp_bss(struct cfg80211_bss *a,
  1061. struct cfg80211_bss *b,
  1062. enum bss_compare_mode mode)
  1063. {
  1064. const struct cfg80211_bss_ies *a_ies, *b_ies;
  1065. const u8 *ie1 = NULL;
  1066. const u8 *ie2 = NULL;
  1067. int i, r;
  1068. if (a->channel != b->channel)
  1069. return b->channel->center_freq - a->channel->center_freq;
  1070. a_ies = rcu_access_pointer(a->ies);
  1071. if (!a_ies)
  1072. return -1;
  1073. b_ies = rcu_access_pointer(b->ies);
  1074. if (!b_ies)
  1075. return 1;
  1076. if (WLAN_CAPABILITY_IS_STA_BSS(a->capability))
  1077. ie1 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  1078. a_ies->data, a_ies->len);
  1079. if (WLAN_CAPABILITY_IS_STA_BSS(b->capability))
  1080. ie2 = cfg80211_find_ie(WLAN_EID_MESH_ID,
  1081. b_ies->data, b_ies->len);
  1082. if (ie1 && ie2) {
  1083. int mesh_id_cmp;
  1084. if (ie1[1] == ie2[1])
  1085. mesh_id_cmp = memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  1086. else
  1087. mesh_id_cmp = ie2[1] - ie1[1];
  1088. ie1 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  1089. a_ies->data, a_ies->len);
  1090. ie2 = cfg80211_find_ie(WLAN_EID_MESH_CONFIG,
  1091. b_ies->data, b_ies->len);
  1092. if (ie1 && ie2) {
  1093. if (mesh_id_cmp)
  1094. return mesh_id_cmp;
  1095. if (ie1[1] != ie2[1])
  1096. return ie2[1] - ie1[1];
  1097. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  1098. }
  1099. }
  1100. r = memcmp(a->bssid, b->bssid, sizeof(a->bssid));
  1101. if (r)
  1102. return r;
  1103. ie1 = cfg80211_find_ie(WLAN_EID_SSID, a_ies->data, a_ies->len);
  1104. ie2 = cfg80211_find_ie(WLAN_EID_SSID, b_ies->data, b_ies->len);
  1105. if (!ie1 && !ie2)
  1106. return 0;
  1107. /*
  1108. * Note that with "hide_ssid", the function returns a match if
  1109. * the already-present BSS ("b") is a hidden SSID beacon for
  1110. * the new BSS ("a").
  1111. */
  1112. /* sort missing IE before (left of) present IE */
  1113. if (!ie1)
  1114. return -1;
  1115. if (!ie2)
  1116. return 1;
  1117. switch (mode) {
  1118. case BSS_CMP_HIDE_ZLEN:
  1119. /*
  1120. * In ZLEN mode we assume the BSS entry we're
  1121. * looking for has a zero-length SSID. So if
  1122. * the one we're looking at right now has that,
  1123. * return 0. Otherwise, return the difference
  1124. * in length, but since we're looking for the
  1125. * 0-length it's really equivalent to returning
  1126. * the length of the one we're looking at.
  1127. *
  1128. * No content comparison is needed as we assume
  1129. * the content length is zero.
  1130. */
  1131. return ie2[1];
  1132. case BSS_CMP_REGULAR:
  1133. default:
  1134. /* sort by length first, then by contents */
  1135. if (ie1[1] != ie2[1])
  1136. return ie2[1] - ie1[1];
  1137. return memcmp(ie1 + 2, ie2 + 2, ie1[1]);
  1138. case BSS_CMP_HIDE_NUL:
  1139. if (ie1[1] != ie2[1])
  1140. return ie2[1] - ie1[1];
  1141. /* this is equivalent to memcmp(zeroes, ie2 + 2, len) */
  1142. for (i = 0; i < ie2[1]; i++)
  1143. if (ie2[i + 2])
  1144. return -1;
  1145. return 0;
  1146. }
  1147. }
  1148. static bool cfg80211_bss_type_match(u16 capability,
  1149. enum nl80211_band band,
  1150. enum ieee80211_bss_type bss_type)
  1151. {
  1152. bool ret = true;
  1153. u16 mask, val;
  1154. if (bss_type == IEEE80211_BSS_TYPE_ANY)
  1155. return ret;
  1156. if (band == NL80211_BAND_60GHZ) {
  1157. mask = WLAN_CAPABILITY_DMG_TYPE_MASK;
  1158. switch (bss_type) {
  1159. case IEEE80211_BSS_TYPE_ESS:
  1160. val = WLAN_CAPABILITY_DMG_TYPE_AP;
  1161. break;
  1162. case IEEE80211_BSS_TYPE_PBSS:
  1163. val = WLAN_CAPABILITY_DMG_TYPE_PBSS;
  1164. break;
  1165. case IEEE80211_BSS_TYPE_IBSS:
  1166. val = WLAN_CAPABILITY_DMG_TYPE_IBSS;
  1167. break;
  1168. default:
  1169. return false;
  1170. }
  1171. } else {
  1172. mask = WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS;
  1173. switch (bss_type) {
  1174. case IEEE80211_BSS_TYPE_ESS:
  1175. val = WLAN_CAPABILITY_ESS;
  1176. break;
  1177. case IEEE80211_BSS_TYPE_IBSS:
  1178. val = WLAN_CAPABILITY_IBSS;
  1179. break;
  1180. case IEEE80211_BSS_TYPE_MBSS:
  1181. val = 0;
  1182. break;
  1183. default:
  1184. return false;
  1185. }
  1186. }
  1187. ret = ((capability & mask) == val);
  1188. return ret;
  1189. }
  1190. /* Returned bss is reference counted and must be cleaned up appropriately. */
  1191. struct cfg80211_bss *cfg80211_get_bss(struct wiphy *wiphy,
  1192. struct ieee80211_channel *channel,
  1193. const u8 *bssid,
  1194. const u8 *ssid, size_t ssid_len,
  1195. enum ieee80211_bss_type bss_type,
  1196. enum ieee80211_privacy privacy)
  1197. {
  1198. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1199. struct cfg80211_internal_bss *bss, *res = NULL;
  1200. unsigned long now = jiffies;
  1201. int bss_privacy;
  1202. trace_cfg80211_get_bss(wiphy, channel, bssid, ssid, ssid_len, bss_type,
  1203. privacy);
  1204. spin_lock_bh(&rdev->bss_lock);
  1205. list_for_each_entry(bss, &rdev->bss_list, list) {
  1206. if (!cfg80211_bss_type_match(bss->pub.capability,
  1207. bss->pub.channel->band, bss_type))
  1208. continue;
  1209. bss_privacy = (bss->pub.capability & WLAN_CAPABILITY_PRIVACY);
  1210. if ((privacy == IEEE80211_PRIVACY_ON && !bss_privacy) ||
  1211. (privacy == IEEE80211_PRIVACY_OFF && bss_privacy))
  1212. continue;
  1213. if (channel && bss->pub.channel != channel)
  1214. continue;
  1215. if (!is_valid_ether_addr(bss->pub.bssid))
  1216. continue;
  1217. /* Don't get expired BSS structs */
  1218. if (time_after(now, bss->ts + IEEE80211_SCAN_RESULT_EXPIRE) &&
  1219. !atomic_read(&bss->hold))
  1220. continue;
  1221. if (is_bss(&bss->pub, bssid, ssid, ssid_len)) {
  1222. res = bss;
  1223. bss_ref_get(rdev, res);
  1224. break;
  1225. }
  1226. }
  1227. spin_unlock_bh(&rdev->bss_lock);
  1228. if (!res)
  1229. return NULL;
  1230. trace_cfg80211_return_bss(&res->pub);
  1231. return &res->pub;
  1232. }
  1233. EXPORT_SYMBOL(cfg80211_get_bss);
  1234. static void rb_insert_bss(struct cfg80211_registered_device *rdev,
  1235. struct cfg80211_internal_bss *bss)
  1236. {
  1237. struct rb_node **p = &rdev->bss_tree.rb_node;
  1238. struct rb_node *parent = NULL;
  1239. struct cfg80211_internal_bss *tbss;
  1240. int cmp;
  1241. while (*p) {
  1242. parent = *p;
  1243. tbss = rb_entry(parent, struct cfg80211_internal_bss, rbn);
  1244. cmp = cmp_bss(&bss->pub, &tbss->pub, BSS_CMP_REGULAR);
  1245. if (WARN_ON(!cmp)) {
  1246. /* will sort of leak this BSS */
  1247. return;
  1248. }
  1249. if (cmp < 0)
  1250. p = &(*p)->rb_left;
  1251. else
  1252. p = &(*p)->rb_right;
  1253. }
  1254. rb_link_node(&bss->rbn, parent, p);
  1255. rb_insert_color(&bss->rbn, &rdev->bss_tree);
  1256. }
  1257. static struct cfg80211_internal_bss *
  1258. rb_find_bss(struct cfg80211_registered_device *rdev,
  1259. struct cfg80211_internal_bss *res,
  1260. enum bss_compare_mode mode)
  1261. {
  1262. struct rb_node *n = rdev->bss_tree.rb_node;
  1263. struct cfg80211_internal_bss *bss;
  1264. int r;
  1265. while (n) {
  1266. bss = rb_entry(n, struct cfg80211_internal_bss, rbn);
  1267. r = cmp_bss(&res->pub, &bss->pub, mode);
  1268. if (r == 0)
  1269. return bss;
  1270. else if (r < 0)
  1271. n = n->rb_left;
  1272. else
  1273. n = n->rb_right;
  1274. }
  1275. return NULL;
  1276. }
  1277. static bool cfg80211_combine_bsses(struct cfg80211_registered_device *rdev,
  1278. struct cfg80211_internal_bss *new)
  1279. {
  1280. const struct cfg80211_bss_ies *ies;
  1281. struct cfg80211_internal_bss *bss;
  1282. const u8 *ie;
  1283. int i, ssidlen;
  1284. u8 fold = 0;
  1285. u32 n_entries = 0;
  1286. ies = rcu_access_pointer(new->pub.beacon_ies);
  1287. if (WARN_ON(!ies))
  1288. return false;
  1289. ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  1290. if (!ie) {
  1291. /* nothing to do */
  1292. return true;
  1293. }
  1294. ssidlen = ie[1];
  1295. for (i = 0; i < ssidlen; i++)
  1296. fold |= ie[2 + i];
  1297. if (fold) {
  1298. /* not a hidden SSID */
  1299. return true;
  1300. }
  1301. /* This is the bad part ... */
  1302. list_for_each_entry(bss, &rdev->bss_list, list) {
  1303. /*
  1304. * we're iterating all the entries anyway, so take the
  1305. * opportunity to validate the list length accounting
  1306. */
  1307. n_entries++;
  1308. if (!ether_addr_equal(bss->pub.bssid, new->pub.bssid))
  1309. continue;
  1310. if (bss->pub.channel != new->pub.channel)
  1311. continue;
  1312. if (bss->pub.scan_width != new->pub.scan_width)
  1313. continue;
  1314. if (rcu_access_pointer(bss->pub.beacon_ies))
  1315. continue;
  1316. ies = rcu_access_pointer(bss->pub.ies);
  1317. if (!ies)
  1318. continue;
  1319. ie = cfg80211_find_ie(WLAN_EID_SSID, ies->data, ies->len);
  1320. if (!ie)
  1321. continue;
  1322. if (ssidlen && ie[1] != ssidlen)
  1323. continue;
  1324. if (WARN_ON_ONCE(bss->pub.hidden_beacon_bss))
  1325. continue;
  1326. if (WARN_ON_ONCE(!list_empty(&bss->hidden_list)))
  1327. list_del(&bss->hidden_list);
  1328. /* combine them */
  1329. list_add(&bss->hidden_list, &new->hidden_list);
  1330. bss->pub.hidden_beacon_bss = &new->pub;
  1331. new->refcount += bss->refcount;
  1332. rcu_assign_pointer(bss->pub.beacon_ies,
  1333. new->pub.beacon_ies);
  1334. }
  1335. WARN_ONCE(n_entries != rdev->bss_entries,
  1336. "rdev bss entries[%d]/list[len:%d] corruption\n",
  1337. rdev->bss_entries, n_entries);
  1338. return true;
  1339. }
  1340. struct cfg80211_non_tx_bss {
  1341. struct cfg80211_bss *tx_bss;
  1342. u8 max_bssid_indicator;
  1343. u8 bssid_index;
  1344. };
  1345. static bool
  1346. cfg80211_update_known_bss(struct cfg80211_registered_device *rdev,
  1347. struct cfg80211_internal_bss *known,
  1348. struct cfg80211_internal_bss *new,
  1349. bool signal_valid)
  1350. {
  1351. lockdep_assert_held(&rdev->bss_lock);
  1352. /* Update IEs */
  1353. if (rcu_access_pointer(new->pub.proberesp_ies)) {
  1354. const struct cfg80211_bss_ies *old;
  1355. old = rcu_access_pointer(known->pub.proberesp_ies);
  1356. rcu_assign_pointer(known->pub.proberesp_ies,
  1357. new->pub.proberesp_ies);
  1358. /* Override possible earlier Beacon frame IEs */
  1359. rcu_assign_pointer(known->pub.ies,
  1360. new->pub.proberesp_ies);
  1361. if (old)
  1362. kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
  1363. } else if (rcu_access_pointer(new->pub.beacon_ies)) {
  1364. const struct cfg80211_bss_ies *old;
  1365. struct cfg80211_internal_bss *bss;
  1366. if (known->pub.hidden_beacon_bss &&
  1367. !list_empty(&known->hidden_list)) {
  1368. const struct cfg80211_bss_ies *f;
  1369. /* The known BSS struct is one of the probe
  1370. * response members of a group, but we're
  1371. * receiving a beacon (beacon_ies in the new
  1372. * bss is used). This can only mean that the
  1373. * AP changed its beacon from not having an
  1374. * SSID to showing it, which is confusing so
  1375. * drop this information.
  1376. */
  1377. f = rcu_access_pointer(new->pub.beacon_ies);
  1378. kfree_rcu((struct cfg80211_bss_ies *)f, rcu_head);
  1379. return false;
  1380. }
  1381. old = rcu_access_pointer(known->pub.beacon_ies);
  1382. rcu_assign_pointer(known->pub.beacon_ies, new->pub.beacon_ies);
  1383. /* Override IEs if they were from a beacon before */
  1384. if (old == rcu_access_pointer(known->pub.ies))
  1385. rcu_assign_pointer(known->pub.ies, new->pub.beacon_ies);
  1386. /* Assign beacon IEs to all sub entries */
  1387. list_for_each_entry(bss, &known->hidden_list, hidden_list) {
  1388. const struct cfg80211_bss_ies *ies;
  1389. ies = rcu_access_pointer(bss->pub.beacon_ies);
  1390. WARN_ON(ies != old);
  1391. rcu_assign_pointer(bss->pub.beacon_ies,
  1392. new->pub.beacon_ies);
  1393. }
  1394. if (old)
  1395. kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
  1396. }
  1397. known->pub.beacon_interval = new->pub.beacon_interval;
  1398. /* don't update the signal if beacon was heard on
  1399. * adjacent channel.
  1400. */
  1401. if (signal_valid)
  1402. known->pub.signal = new->pub.signal;
  1403. known->pub.capability = new->pub.capability;
  1404. known->ts = new->ts;
  1405. known->ts_boottime = new->ts_boottime;
  1406. known->parent_tsf = new->parent_tsf;
  1407. known->pub.chains = new->pub.chains;
  1408. memcpy(known->pub.chain_signal, new->pub.chain_signal,
  1409. IEEE80211_MAX_CHAINS);
  1410. ether_addr_copy(known->parent_bssid, new->parent_bssid);
  1411. known->pub.max_bssid_indicator = new->pub.max_bssid_indicator;
  1412. known->pub.bssid_index = new->pub.bssid_index;
  1413. return true;
  1414. }
  1415. /* Returned bss is reference counted and must be cleaned up appropriately. */
  1416. struct cfg80211_internal_bss *
  1417. cfg80211_bss_update(struct cfg80211_registered_device *rdev,
  1418. struct cfg80211_internal_bss *tmp,
  1419. bool signal_valid, unsigned long ts)
  1420. {
  1421. struct cfg80211_internal_bss *found = NULL;
  1422. if (WARN_ON(!tmp->pub.channel))
  1423. return NULL;
  1424. tmp->ts = ts;
  1425. spin_lock_bh(&rdev->bss_lock);
  1426. if (WARN_ON(!rcu_access_pointer(tmp->pub.ies))) {
  1427. spin_unlock_bh(&rdev->bss_lock);
  1428. return NULL;
  1429. }
  1430. found = rb_find_bss(rdev, tmp, BSS_CMP_REGULAR);
  1431. if (found) {
  1432. if (!cfg80211_update_known_bss(rdev, found, tmp, signal_valid))
  1433. goto drop;
  1434. } else {
  1435. struct cfg80211_internal_bss *new;
  1436. struct cfg80211_internal_bss *hidden;
  1437. struct cfg80211_bss_ies *ies;
  1438. /*
  1439. * create a copy -- the "res" variable that is passed in
  1440. * is allocated on the stack since it's not needed in the
  1441. * more common case of an update
  1442. */
  1443. new = kzalloc(sizeof(*new) + rdev->wiphy.bss_priv_size,
  1444. GFP_ATOMIC);
  1445. if (!new) {
  1446. ies = (void *)rcu_dereference(tmp->pub.beacon_ies);
  1447. if (ies)
  1448. kfree_rcu(ies, rcu_head);
  1449. ies = (void *)rcu_dereference(tmp->pub.proberesp_ies);
  1450. if (ies)
  1451. kfree_rcu(ies, rcu_head);
  1452. goto drop;
  1453. }
  1454. memcpy(new, tmp, sizeof(*new));
  1455. new->refcount = 1;
  1456. INIT_LIST_HEAD(&new->hidden_list);
  1457. INIT_LIST_HEAD(&new->pub.nontrans_list);
  1458. if (rcu_access_pointer(tmp->pub.proberesp_ies)) {
  1459. hidden = rb_find_bss(rdev, tmp, BSS_CMP_HIDE_ZLEN);
  1460. if (!hidden)
  1461. hidden = rb_find_bss(rdev, tmp,
  1462. BSS_CMP_HIDE_NUL);
  1463. if (hidden) {
  1464. new->pub.hidden_beacon_bss = &hidden->pub;
  1465. list_add(&new->hidden_list,
  1466. &hidden->hidden_list);
  1467. hidden->refcount++;
  1468. rcu_assign_pointer(new->pub.beacon_ies,
  1469. hidden->pub.beacon_ies);
  1470. }
  1471. } else {
  1472. /*
  1473. * Ok so we found a beacon, and don't have an entry. If
  1474. * it's a beacon with hidden SSID, we might be in for an
  1475. * expensive search for any probe responses that should
  1476. * be grouped with this beacon for updates ...
  1477. */
  1478. if (!cfg80211_combine_bsses(rdev, new)) {
  1479. bss_ref_put(rdev, new);
  1480. goto drop;
  1481. }
  1482. }
  1483. if (rdev->bss_entries >= bss_entries_limit &&
  1484. !cfg80211_bss_expire_oldest(rdev)) {
  1485. bss_ref_put(rdev, new);
  1486. goto drop;
  1487. }
  1488. /* This must be before the call to bss_ref_get */
  1489. if (tmp->pub.transmitted_bss) {
  1490. struct cfg80211_internal_bss *pbss =
  1491. container_of(tmp->pub.transmitted_bss,
  1492. struct cfg80211_internal_bss,
  1493. pub);
  1494. new->pub.transmitted_bss = tmp->pub.transmitted_bss;
  1495. bss_ref_get(rdev, pbss);
  1496. }
  1497. list_add_tail(&new->list, &rdev->bss_list);
  1498. rdev->bss_entries++;
  1499. rb_insert_bss(rdev, new);
  1500. found = new;
  1501. }
  1502. rdev->bss_generation++;
  1503. bss_ref_get(rdev, found);
  1504. spin_unlock_bh(&rdev->bss_lock);
  1505. return found;
  1506. drop:
  1507. spin_unlock_bh(&rdev->bss_lock);
  1508. return NULL;
  1509. }
  1510. /*
  1511. * Update RX channel information based on the available frame payload
  1512. * information. This is mainly for the 2.4 GHz band where frames can be received
  1513. * from neighboring channels and the Beacon frames use the DSSS Parameter Set
  1514. * element to indicate the current (transmitting) channel, but this might also
  1515. * be needed on other bands if RX frequency does not match with the actual
  1516. * operating channel of a BSS.
  1517. */
  1518. static struct ieee80211_channel *
  1519. cfg80211_get_bss_channel(struct wiphy *wiphy, const u8 *ie, size_t ielen,
  1520. struct ieee80211_channel *channel,
  1521. enum nl80211_bss_scan_width scan_width)
  1522. {
  1523. const u8 *tmp;
  1524. u32 freq;
  1525. int channel_number = -1;
  1526. struct ieee80211_channel *alt_channel;
  1527. if (channel->band == NL80211_BAND_S1GHZ) {
  1528. tmp = cfg80211_find_ie(WLAN_EID_S1G_OPERATION, ie, ielen);
  1529. if (tmp && tmp[1] >= sizeof(struct ieee80211_s1g_oper_ie)) {
  1530. struct ieee80211_s1g_oper_ie *s1gop = (void *)(tmp + 2);
  1531. channel_number = s1gop->primary_ch;
  1532. }
  1533. } else {
  1534. tmp = cfg80211_find_ie(WLAN_EID_DS_PARAMS, ie, ielen);
  1535. if (tmp && tmp[1] == 1) {
  1536. channel_number = tmp[2];
  1537. } else {
  1538. tmp = cfg80211_find_ie(WLAN_EID_HT_OPERATION, ie, ielen);
  1539. if (tmp && tmp[1] >= sizeof(struct ieee80211_ht_operation)) {
  1540. struct ieee80211_ht_operation *htop = (void *)(tmp + 2);
  1541. channel_number = htop->primary_chan;
  1542. }
  1543. }
  1544. }
  1545. if (channel_number < 0) {
  1546. /* No channel information in frame payload */
  1547. return channel;
  1548. }
  1549. freq = ieee80211_channel_to_freq_khz(channel_number, channel->band);
  1550. alt_channel = ieee80211_get_channel_khz(wiphy, freq);
  1551. if (!alt_channel) {
  1552. if (channel->band == NL80211_BAND_2GHZ) {
  1553. /*
  1554. * Better not allow unexpected channels when that could
  1555. * be going beyond the 1-11 range (e.g., discovering
  1556. * BSS on channel 12 when radio is configured for
  1557. * channel 11.
  1558. */
  1559. return NULL;
  1560. }
  1561. /* No match for the payload channel number - ignore it */
  1562. return channel;
  1563. }
  1564. if (scan_width == NL80211_BSS_CHAN_WIDTH_10 ||
  1565. scan_width == NL80211_BSS_CHAN_WIDTH_5) {
  1566. /*
  1567. * Ignore channel number in 5 and 10 MHz channels where there
  1568. * may not be an n:1 or 1:n mapping between frequencies and
  1569. * channel numbers.
  1570. */
  1571. return channel;
  1572. }
  1573. /*
  1574. * Use the channel determined through the payload channel number
  1575. * instead of the RX channel reported by the driver.
  1576. */
  1577. if (alt_channel->flags & IEEE80211_CHAN_DISABLED)
  1578. return NULL;
  1579. return alt_channel;
  1580. }
  1581. /* Returned bss is reference counted and must be cleaned up appropriately. */
  1582. static struct cfg80211_bss *
  1583. cfg80211_inform_single_bss_data(struct wiphy *wiphy,
  1584. struct cfg80211_inform_bss *data,
  1585. enum cfg80211_bss_frame_type ftype,
  1586. const u8 *bssid, u64 tsf, u16 capability,
  1587. u16 beacon_interval, const u8 *ie, size_t ielen,
  1588. struct cfg80211_non_tx_bss *non_tx_data,
  1589. gfp_t gfp)
  1590. {
  1591. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  1592. struct cfg80211_bss_ies *ies;
  1593. struct ieee80211_channel *channel;
  1594. struct cfg80211_internal_bss tmp = {}, *res;
  1595. int bss_type;
  1596. bool signal_valid;
  1597. unsigned long ts;
  1598. if (WARN_ON(!wiphy))
  1599. return NULL;
  1600. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  1601. (data->signal < 0 || data->signal > 100)))
  1602. return NULL;
  1603. channel = cfg80211_get_bss_channel(wiphy, ie, ielen, data->chan,
  1604. data->scan_width);
  1605. if (!channel)
  1606. return NULL;
  1607. memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
  1608. tmp.pub.channel = channel;
  1609. tmp.pub.scan_width = data->scan_width;
  1610. tmp.pub.signal = data->signal;
  1611. tmp.pub.beacon_interval = beacon_interval;
  1612. tmp.pub.capability = capability;
  1613. tmp.ts_boottime = data->boottime_ns;
  1614. if (non_tx_data) {
  1615. tmp.pub.transmitted_bss = non_tx_data->tx_bss;
  1616. ts = bss_from_pub(non_tx_data->tx_bss)->ts;
  1617. tmp.pub.bssid_index = non_tx_data->bssid_index;
  1618. tmp.pub.max_bssid_indicator = non_tx_data->max_bssid_indicator;
  1619. } else {
  1620. ts = jiffies;
  1621. }
  1622. /*
  1623. * If we do not know here whether the IEs are from a Beacon or Probe
  1624. * Response frame, we need to pick one of the options and only use it
  1625. * with the driver that does not provide the full Beacon/Probe Response
  1626. * frame. Use Beacon frame pointer to avoid indicating that this should
  1627. * override the IEs pointer should we have received an earlier
  1628. * indication of Probe Response data.
  1629. */
  1630. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  1631. if (!ies)
  1632. return NULL;
  1633. ies->len = ielen;
  1634. ies->tsf = tsf;
  1635. ies->from_beacon = false;
  1636. memcpy(ies->data, ie, ielen);
  1637. switch (ftype) {
  1638. case CFG80211_BSS_FTYPE_BEACON:
  1639. ies->from_beacon = true;
  1640. fallthrough;
  1641. case CFG80211_BSS_FTYPE_UNKNOWN:
  1642. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  1643. break;
  1644. case CFG80211_BSS_FTYPE_PRESP:
  1645. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  1646. break;
  1647. }
  1648. rcu_assign_pointer(tmp.pub.ies, ies);
  1649. signal_valid = data->chan == channel;
  1650. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid, ts);
  1651. if (!res)
  1652. return NULL;
  1653. if (channel->band == NL80211_BAND_60GHZ) {
  1654. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  1655. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  1656. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  1657. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1658. } else {
  1659. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  1660. regulatory_hint_found_beacon(wiphy, channel, gfp);
  1661. }
  1662. if (non_tx_data) {
  1663. /* this is a nontransmitting bss, we need to add it to
  1664. * transmitting bss' list if it is not there
  1665. */
  1666. spin_lock_bh(&rdev->bss_lock);
  1667. if (cfg80211_add_nontrans_list(non_tx_data->tx_bss,
  1668. &res->pub)) {
  1669. if (__cfg80211_unlink_bss(rdev, res))
  1670. rdev->bss_generation++;
  1671. }
  1672. spin_unlock_bh(&rdev->bss_lock);
  1673. }
  1674. trace_cfg80211_return_bss(&res->pub);
  1675. /* cfg80211_bss_update gives us a referenced result */
  1676. return &res->pub;
  1677. }
  1678. static const struct element
  1679. *cfg80211_get_profile_continuation(const u8 *ie, size_t ielen,
  1680. const struct element *mbssid_elem,
  1681. const struct element *sub_elem)
  1682. {
  1683. const u8 *mbssid_end = mbssid_elem->data + mbssid_elem->datalen;
  1684. const struct element *next_mbssid;
  1685. const struct element *next_sub;
  1686. next_mbssid = cfg80211_find_elem(WLAN_EID_MULTIPLE_BSSID,
  1687. mbssid_end,
  1688. ielen - (mbssid_end - ie));
  1689. /*
  1690. * If it is not the last subelement in current MBSSID IE or there isn't
  1691. * a next MBSSID IE - profile is complete.
  1692. */
  1693. if ((sub_elem->data + sub_elem->datalen < mbssid_end - 1) ||
  1694. !next_mbssid)
  1695. return NULL;
  1696. /* For any length error, just return NULL */
  1697. if (next_mbssid->datalen < 4)
  1698. return NULL;
  1699. next_sub = (void *)&next_mbssid->data[1];
  1700. if (next_mbssid->data + next_mbssid->datalen <
  1701. next_sub->data + next_sub->datalen)
  1702. return NULL;
  1703. if (next_sub->id != 0 || next_sub->datalen < 2)
  1704. return NULL;
  1705. /*
  1706. * Check if the first element in the next sub element is a start
  1707. * of a new profile
  1708. */
  1709. return next_sub->data[0] == WLAN_EID_NON_TX_BSSID_CAP ?
  1710. NULL : next_mbssid;
  1711. }
  1712. size_t cfg80211_merge_profile(const u8 *ie, size_t ielen,
  1713. const struct element *mbssid_elem,
  1714. const struct element *sub_elem,
  1715. u8 *merged_ie, size_t max_copy_len)
  1716. {
  1717. size_t copied_len = sub_elem->datalen;
  1718. const struct element *next_mbssid;
  1719. if (sub_elem->datalen > max_copy_len)
  1720. return 0;
  1721. memcpy(merged_ie, sub_elem->data, sub_elem->datalen);
  1722. while ((next_mbssid = cfg80211_get_profile_continuation(ie, ielen,
  1723. mbssid_elem,
  1724. sub_elem))) {
  1725. const struct element *next_sub = (void *)&next_mbssid->data[1];
  1726. if (copied_len + next_sub->datalen > max_copy_len)
  1727. break;
  1728. memcpy(merged_ie + copied_len, next_sub->data,
  1729. next_sub->datalen);
  1730. copied_len += next_sub->datalen;
  1731. }
  1732. return copied_len;
  1733. }
  1734. EXPORT_SYMBOL(cfg80211_merge_profile);
  1735. static void cfg80211_parse_mbssid_data(struct wiphy *wiphy,
  1736. struct cfg80211_inform_bss *data,
  1737. enum cfg80211_bss_frame_type ftype,
  1738. const u8 *bssid, u64 tsf,
  1739. u16 beacon_interval, const u8 *ie,
  1740. size_t ielen,
  1741. struct cfg80211_non_tx_bss *non_tx_data,
  1742. gfp_t gfp)
  1743. {
  1744. const u8 *mbssid_index_ie;
  1745. const struct element *elem, *sub;
  1746. size_t new_ie_len;
  1747. u8 new_bssid[ETH_ALEN];
  1748. u8 *new_ie, *profile;
  1749. u64 seen_indices = 0;
  1750. u16 capability;
  1751. struct cfg80211_bss *bss;
  1752. if (!non_tx_data)
  1753. return;
  1754. if (!cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
  1755. return;
  1756. if (!wiphy->support_mbssid)
  1757. return;
  1758. if (wiphy->support_only_he_mbssid &&
  1759. !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
  1760. return;
  1761. new_ie = kmalloc(IEEE80211_MAX_DATA_LEN, gfp);
  1762. if (!new_ie)
  1763. return;
  1764. profile = kmalloc(ielen, gfp);
  1765. if (!profile)
  1766. goto out;
  1767. for_each_element_id(elem, WLAN_EID_MULTIPLE_BSSID, ie, ielen) {
  1768. if (elem->datalen < 4)
  1769. continue;
  1770. for_each_element(sub, elem->data + 1, elem->datalen - 1) {
  1771. u8 profile_len;
  1772. if (sub->id != 0 || sub->datalen < 4) {
  1773. /* not a valid BSS profile */
  1774. continue;
  1775. }
  1776. if (sub->data[0] != WLAN_EID_NON_TX_BSSID_CAP ||
  1777. sub->data[1] != 2) {
  1778. /* The first element within the Nontransmitted
  1779. * BSSID Profile is not the Nontransmitted
  1780. * BSSID Capability element.
  1781. */
  1782. continue;
  1783. }
  1784. memset(profile, 0, ielen);
  1785. profile_len = cfg80211_merge_profile(ie, ielen,
  1786. elem,
  1787. sub,
  1788. profile,
  1789. ielen);
  1790. /* found a Nontransmitted BSSID Profile */
  1791. mbssid_index_ie = cfg80211_find_ie
  1792. (WLAN_EID_MULTI_BSSID_IDX,
  1793. profile, profile_len);
  1794. if (!mbssid_index_ie || mbssid_index_ie[1] < 1 ||
  1795. mbssid_index_ie[2] == 0 ||
  1796. mbssid_index_ie[2] > 46) {
  1797. /* No valid Multiple BSSID-Index element */
  1798. continue;
  1799. }
  1800. if (seen_indices & BIT_ULL(mbssid_index_ie[2]))
  1801. /* We don't support legacy split of a profile */
  1802. net_dbg_ratelimited("Partial info for BSSID index %d\n",
  1803. mbssid_index_ie[2]);
  1804. seen_indices |= BIT_ULL(mbssid_index_ie[2]);
  1805. non_tx_data->bssid_index = mbssid_index_ie[2];
  1806. non_tx_data->max_bssid_indicator = elem->data[0];
  1807. cfg80211_gen_new_bssid(bssid,
  1808. non_tx_data->max_bssid_indicator,
  1809. non_tx_data->bssid_index,
  1810. new_bssid);
  1811. memset(new_ie, 0, IEEE80211_MAX_DATA_LEN);
  1812. new_ie_len = cfg80211_gen_new_ie(ie, ielen,
  1813. profile,
  1814. profile_len, new_ie,
  1815. gfp);
  1816. if (!new_ie_len)
  1817. continue;
  1818. capability = get_unaligned_le16(profile + 2);
  1819. bss = cfg80211_inform_single_bss_data(wiphy, data,
  1820. ftype,
  1821. new_bssid, tsf,
  1822. capability,
  1823. beacon_interval,
  1824. new_ie,
  1825. new_ie_len,
  1826. non_tx_data,
  1827. gfp);
  1828. if (!bss)
  1829. break;
  1830. cfg80211_put_bss(wiphy, bss);
  1831. }
  1832. }
  1833. out:
  1834. kfree(new_ie);
  1835. kfree(profile);
  1836. }
  1837. struct cfg80211_bss *
  1838. cfg80211_inform_bss_data(struct wiphy *wiphy,
  1839. struct cfg80211_inform_bss *data,
  1840. enum cfg80211_bss_frame_type ftype,
  1841. const u8 *bssid, u64 tsf, u16 capability,
  1842. u16 beacon_interval, const u8 *ie, size_t ielen,
  1843. gfp_t gfp)
  1844. {
  1845. struct cfg80211_bss *res;
  1846. struct cfg80211_non_tx_bss non_tx_data;
  1847. res = cfg80211_inform_single_bss_data(wiphy, data, ftype, bssid, tsf,
  1848. capability, beacon_interval, ie,
  1849. ielen, NULL, gfp);
  1850. if (!res)
  1851. return NULL;
  1852. non_tx_data.tx_bss = res;
  1853. cfg80211_parse_mbssid_data(wiphy, data, ftype, bssid, tsf,
  1854. beacon_interval, ie, ielen, &non_tx_data,
  1855. gfp);
  1856. return res;
  1857. }
  1858. EXPORT_SYMBOL(cfg80211_inform_bss_data);
  1859. static void
  1860. cfg80211_parse_mbssid_frame_data(struct wiphy *wiphy,
  1861. struct cfg80211_inform_bss *data,
  1862. struct ieee80211_mgmt *mgmt, size_t len,
  1863. struct cfg80211_non_tx_bss *non_tx_data,
  1864. gfp_t gfp)
  1865. {
  1866. enum cfg80211_bss_frame_type ftype;
  1867. const u8 *ie = mgmt->u.probe_resp.variable;
  1868. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  1869. u.probe_resp.variable);
  1870. ftype = ieee80211_is_beacon(mgmt->frame_control) ?
  1871. CFG80211_BSS_FTYPE_BEACON : CFG80211_BSS_FTYPE_PRESP;
  1872. cfg80211_parse_mbssid_data(wiphy, data, ftype, mgmt->bssid,
  1873. le64_to_cpu(mgmt->u.probe_resp.timestamp),
  1874. le16_to_cpu(mgmt->u.probe_resp.beacon_int),
  1875. ie, ielen, non_tx_data, gfp);
  1876. }
  1877. static void
  1878. cfg80211_update_notlisted_nontrans(struct wiphy *wiphy,
  1879. struct cfg80211_bss *nontrans_bss,
  1880. struct ieee80211_mgmt *mgmt, size_t len)
  1881. {
  1882. u8 *ie, *new_ie, *pos;
  1883. const u8 *nontrans_ssid, *trans_ssid, *mbssid;
  1884. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  1885. u.probe_resp.variable);
  1886. size_t new_ie_len;
  1887. struct cfg80211_bss_ies *new_ies;
  1888. const struct cfg80211_bss_ies *old;
  1889. u8 cpy_len;
  1890. lockdep_assert_held(&wiphy_to_rdev(wiphy)->bss_lock);
  1891. ie = mgmt->u.probe_resp.variable;
  1892. new_ie_len = ielen;
  1893. trans_ssid = cfg80211_find_ie(WLAN_EID_SSID, ie, ielen);
  1894. if (!trans_ssid)
  1895. return;
  1896. new_ie_len -= trans_ssid[1];
  1897. mbssid = cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen);
  1898. /*
  1899. * It's not valid to have the MBSSID element before SSID
  1900. * ignore if that happens - the code below assumes it is
  1901. * after (while copying things inbetween).
  1902. */
  1903. if (!mbssid || mbssid < trans_ssid)
  1904. return;
  1905. new_ie_len -= mbssid[1];
  1906. nontrans_ssid = ieee80211_bss_get_ie(nontrans_bss, WLAN_EID_SSID);
  1907. if (!nontrans_ssid)
  1908. return;
  1909. new_ie_len += nontrans_ssid[1];
  1910. /* generate new ie for nontrans BSS
  1911. * 1. replace SSID with nontrans BSS' SSID
  1912. * 2. skip MBSSID IE
  1913. */
  1914. new_ie = kzalloc(new_ie_len, GFP_ATOMIC);
  1915. if (!new_ie)
  1916. return;
  1917. new_ies = kzalloc(sizeof(*new_ies) + new_ie_len, GFP_ATOMIC);
  1918. if (!new_ies)
  1919. goto out_free;
  1920. pos = new_ie;
  1921. /* copy the nontransmitted SSID */
  1922. cpy_len = nontrans_ssid[1] + 2;
  1923. memcpy(pos, nontrans_ssid, cpy_len);
  1924. pos += cpy_len;
  1925. /* copy the IEs between SSID and MBSSID */
  1926. cpy_len = trans_ssid[1] + 2;
  1927. memcpy(pos, (trans_ssid + cpy_len), (mbssid - (trans_ssid + cpy_len)));
  1928. pos += (mbssid - (trans_ssid + cpy_len));
  1929. /* copy the IEs after MBSSID */
  1930. cpy_len = mbssid[1] + 2;
  1931. memcpy(pos, mbssid + cpy_len, ((ie + ielen) - (mbssid + cpy_len)));
  1932. /* update ie */
  1933. new_ies->len = new_ie_len;
  1934. new_ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  1935. new_ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control);
  1936. memcpy(new_ies->data, new_ie, new_ie_len);
  1937. if (ieee80211_is_probe_resp(mgmt->frame_control)) {
  1938. old = rcu_access_pointer(nontrans_bss->proberesp_ies);
  1939. rcu_assign_pointer(nontrans_bss->proberesp_ies, new_ies);
  1940. rcu_assign_pointer(nontrans_bss->ies, new_ies);
  1941. if (old)
  1942. kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
  1943. } else {
  1944. old = rcu_access_pointer(nontrans_bss->beacon_ies);
  1945. rcu_assign_pointer(nontrans_bss->beacon_ies, new_ies);
  1946. rcu_assign_pointer(nontrans_bss->ies, new_ies);
  1947. if (old)
  1948. kfree_rcu((struct cfg80211_bss_ies *)old, rcu_head);
  1949. }
  1950. out_free:
  1951. kfree(new_ie);
  1952. }
  1953. /* cfg80211_inform_bss_width_frame helper */
  1954. static struct cfg80211_bss *
  1955. cfg80211_inform_single_bss_frame_data(struct wiphy *wiphy,
  1956. struct cfg80211_inform_bss *data,
  1957. struct ieee80211_mgmt *mgmt, size_t len,
  1958. gfp_t gfp)
  1959. {
  1960. struct cfg80211_internal_bss tmp = {}, *res;
  1961. struct cfg80211_bss_ies *ies;
  1962. struct ieee80211_channel *channel;
  1963. bool signal_valid;
  1964. struct ieee80211_ext *ext = NULL;
  1965. u8 *bssid, *variable;
  1966. u16 capability, beacon_int;
  1967. size_t ielen, min_hdr_len = offsetof(struct ieee80211_mgmt,
  1968. u.probe_resp.variable);
  1969. int bss_type;
  1970. BUILD_BUG_ON(offsetof(struct ieee80211_mgmt, u.probe_resp.variable) !=
  1971. offsetof(struct ieee80211_mgmt, u.beacon.variable));
  1972. trace_cfg80211_inform_bss_frame(wiphy, data, mgmt, len);
  1973. if (WARN_ON(!mgmt))
  1974. return NULL;
  1975. if (WARN_ON(!wiphy))
  1976. return NULL;
  1977. if (WARN_ON(wiphy->signal_type == CFG80211_SIGNAL_TYPE_UNSPEC &&
  1978. (data->signal < 0 || data->signal > 100)))
  1979. return NULL;
  1980. if (ieee80211_is_s1g_beacon(mgmt->frame_control)) {
  1981. ext = (void *) mgmt;
  1982. min_hdr_len = offsetof(struct ieee80211_ext, u.s1g_beacon);
  1983. if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
  1984. min_hdr_len = offsetof(struct ieee80211_ext,
  1985. u.s1g_short_beacon.variable);
  1986. }
  1987. if (WARN_ON(len < min_hdr_len))
  1988. return NULL;
  1989. ielen = len - min_hdr_len;
  1990. variable = mgmt->u.probe_resp.variable;
  1991. if (ext) {
  1992. if (ieee80211_is_s1g_short_beacon(mgmt->frame_control))
  1993. variable = ext->u.s1g_short_beacon.variable;
  1994. else
  1995. variable = ext->u.s1g_beacon.variable;
  1996. }
  1997. channel = cfg80211_get_bss_channel(wiphy, variable,
  1998. ielen, data->chan, data->scan_width);
  1999. if (!channel)
  2000. return NULL;
  2001. if (ext) {
  2002. const struct ieee80211_s1g_bcn_compat_ie *compat;
  2003. const struct element *elem;
  2004. elem = cfg80211_find_elem(WLAN_EID_S1G_BCN_COMPAT,
  2005. variable, ielen);
  2006. if (!elem)
  2007. return NULL;
  2008. if (elem->datalen < sizeof(*compat))
  2009. return NULL;
  2010. compat = (void *)elem->data;
  2011. bssid = ext->u.s1g_beacon.sa;
  2012. capability = le16_to_cpu(compat->compat_info);
  2013. beacon_int = le16_to_cpu(compat->beacon_int);
  2014. } else {
  2015. bssid = mgmt->bssid;
  2016. beacon_int = le16_to_cpu(mgmt->u.probe_resp.beacon_int);
  2017. capability = le16_to_cpu(mgmt->u.probe_resp.capab_info);
  2018. }
  2019. ies = kzalloc(sizeof(*ies) + ielen, gfp);
  2020. if (!ies)
  2021. return NULL;
  2022. ies->len = ielen;
  2023. ies->tsf = le64_to_cpu(mgmt->u.probe_resp.timestamp);
  2024. ies->from_beacon = ieee80211_is_beacon(mgmt->frame_control) ||
  2025. ieee80211_is_s1g_beacon(mgmt->frame_control);
  2026. memcpy(ies->data, variable, ielen);
  2027. if (ieee80211_is_probe_resp(mgmt->frame_control))
  2028. rcu_assign_pointer(tmp.pub.proberesp_ies, ies);
  2029. else
  2030. rcu_assign_pointer(tmp.pub.beacon_ies, ies);
  2031. rcu_assign_pointer(tmp.pub.ies, ies);
  2032. memcpy(tmp.pub.bssid, bssid, ETH_ALEN);
  2033. tmp.pub.beacon_interval = beacon_int;
  2034. tmp.pub.capability = capability;
  2035. tmp.pub.channel = channel;
  2036. tmp.pub.scan_width = data->scan_width;
  2037. tmp.pub.signal = data->signal;
  2038. tmp.ts_boottime = data->boottime_ns;
  2039. tmp.parent_tsf = data->parent_tsf;
  2040. tmp.pub.chains = data->chains;
  2041. memcpy(tmp.pub.chain_signal, data->chain_signal, IEEE80211_MAX_CHAINS);
  2042. ether_addr_copy(tmp.parent_bssid, data->parent_bssid);
  2043. signal_valid = data->chan == channel;
  2044. res = cfg80211_bss_update(wiphy_to_rdev(wiphy), &tmp, signal_valid,
  2045. jiffies);
  2046. if (!res)
  2047. return NULL;
  2048. if (channel->band == NL80211_BAND_60GHZ) {
  2049. bss_type = res->pub.capability & WLAN_CAPABILITY_DMG_TYPE_MASK;
  2050. if (bss_type == WLAN_CAPABILITY_DMG_TYPE_AP ||
  2051. bss_type == WLAN_CAPABILITY_DMG_TYPE_PBSS)
  2052. regulatory_hint_found_beacon(wiphy, channel, gfp);
  2053. } else {
  2054. if (res->pub.capability & WLAN_CAPABILITY_ESS)
  2055. regulatory_hint_found_beacon(wiphy, channel, gfp);
  2056. }
  2057. trace_cfg80211_return_bss(&res->pub);
  2058. /* cfg80211_bss_update gives us a referenced result */
  2059. return &res->pub;
  2060. }
  2061. struct cfg80211_bss *
  2062. cfg80211_inform_bss_frame_data(struct wiphy *wiphy,
  2063. struct cfg80211_inform_bss *data,
  2064. struct ieee80211_mgmt *mgmt, size_t len,
  2065. gfp_t gfp)
  2066. {
  2067. struct cfg80211_bss *res, *tmp_bss;
  2068. const u8 *ie = mgmt->u.probe_resp.variable;
  2069. const struct cfg80211_bss_ies *ies1, *ies2;
  2070. size_t ielen = len - offsetof(struct ieee80211_mgmt,
  2071. u.probe_resp.variable);
  2072. struct cfg80211_non_tx_bss non_tx_data;
  2073. res = cfg80211_inform_single_bss_frame_data(wiphy, data, mgmt,
  2074. len, gfp);
  2075. if (!res || !wiphy->support_mbssid ||
  2076. !cfg80211_find_ie(WLAN_EID_MULTIPLE_BSSID, ie, ielen))
  2077. return res;
  2078. if (wiphy->support_only_he_mbssid &&
  2079. !cfg80211_find_ext_ie(WLAN_EID_EXT_HE_CAPABILITY, ie, ielen))
  2080. return res;
  2081. non_tx_data.tx_bss = res;
  2082. /* process each non-transmitting bss */
  2083. cfg80211_parse_mbssid_frame_data(wiphy, data, mgmt, len,
  2084. &non_tx_data, gfp);
  2085. spin_lock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
  2086. /* check if the res has other nontransmitting bss which is not
  2087. * in MBSSID IE
  2088. */
  2089. ies1 = rcu_access_pointer(res->ies);
  2090. /* go through nontrans_list, if the timestamp of the BSS is
  2091. * earlier than the timestamp of the transmitting BSS then
  2092. * update it
  2093. */
  2094. list_for_each_entry(tmp_bss, &res->nontrans_list,
  2095. nontrans_list) {
  2096. ies2 = rcu_access_pointer(tmp_bss->ies);
  2097. if (ies2->tsf < ies1->tsf)
  2098. cfg80211_update_notlisted_nontrans(wiphy, tmp_bss,
  2099. mgmt, len);
  2100. }
  2101. spin_unlock_bh(&wiphy_to_rdev(wiphy)->bss_lock);
  2102. return res;
  2103. }
  2104. EXPORT_SYMBOL(cfg80211_inform_bss_frame_data);
  2105. void cfg80211_ref_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  2106. {
  2107. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  2108. struct cfg80211_internal_bss *bss;
  2109. if (!pub)
  2110. return;
  2111. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  2112. spin_lock_bh(&rdev->bss_lock);
  2113. bss_ref_get(rdev, bss);
  2114. spin_unlock_bh(&rdev->bss_lock);
  2115. }
  2116. EXPORT_SYMBOL(cfg80211_ref_bss);
  2117. void cfg80211_put_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  2118. {
  2119. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  2120. struct cfg80211_internal_bss *bss;
  2121. if (!pub)
  2122. return;
  2123. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  2124. spin_lock_bh(&rdev->bss_lock);
  2125. bss_ref_put(rdev, bss);
  2126. spin_unlock_bh(&rdev->bss_lock);
  2127. }
  2128. EXPORT_SYMBOL(cfg80211_put_bss);
  2129. void cfg80211_unlink_bss(struct wiphy *wiphy, struct cfg80211_bss *pub)
  2130. {
  2131. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  2132. struct cfg80211_internal_bss *bss, *tmp1;
  2133. struct cfg80211_bss *nontrans_bss, *tmp;
  2134. if (WARN_ON(!pub))
  2135. return;
  2136. bss = container_of(pub, struct cfg80211_internal_bss, pub);
  2137. spin_lock_bh(&rdev->bss_lock);
  2138. if (list_empty(&bss->list))
  2139. goto out;
  2140. list_for_each_entry_safe(nontrans_bss, tmp,
  2141. &pub->nontrans_list,
  2142. nontrans_list) {
  2143. tmp1 = container_of(nontrans_bss,
  2144. struct cfg80211_internal_bss, pub);
  2145. if (__cfg80211_unlink_bss(rdev, tmp1))
  2146. rdev->bss_generation++;
  2147. }
  2148. if (__cfg80211_unlink_bss(rdev, bss))
  2149. rdev->bss_generation++;
  2150. out:
  2151. spin_unlock_bh(&rdev->bss_lock);
  2152. }
  2153. EXPORT_SYMBOL(cfg80211_unlink_bss);
  2154. void cfg80211_bss_iter(struct wiphy *wiphy,
  2155. struct cfg80211_chan_def *chandef,
  2156. void (*iter)(struct wiphy *wiphy,
  2157. struct cfg80211_bss *bss,
  2158. void *data),
  2159. void *iter_data)
  2160. {
  2161. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  2162. struct cfg80211_internal_bss *bss;
  2163. spin_lock_bh(&rdev->bss_lock);
  2164. list_for_each_entry(bss, &rdev->bss_list, list) {
  2165. if (!chandef || cfg80211_is_sub_chan(chandef, bss->pub.channel))
  2166. iter(wiphy, &bss->pub, iter_data);
  2167. }
  2168. spin_unlock_bh(&rdev->bss_lock);
  2169. }
  2170. EXPORT_SYMBOL(cfg80211_bss_iter);
  2171. void cfg80211_update_assoc_bss_entry(struct wireless_dev *wdev,
  2172. struct ieee80211_channel *chan)
  2173. {
  2174. struct wiphy *wiphy = wdev->wiphy;
  2175. struct cfg80211_registered_device *rdev = wiphy_to_rdev(wiphy);
  2176. struct cfg80211_internal_bss *cbss = wdev->current_bss;
  2177. struct cfg80211_internal_bss *new = NULL;
  2178. struct cfg80211_internal_bss *bss;
  2179. struct cfg80211_bss *nontrans_bss;
  2180. struct cfg80211_bss *tmp;
  2181. spin_lock_bh(&rdev->bss_lock);
  2182. /*
  2183. * Some APs use CSA also for bandwidth changes, i.e., without actually
  2184. * changing the control channel, so no need to update in such a case.
  2185. */
  2186. if (cbss->pub.channel == chan)
  2187. goto done;
  2188. /* use transmitting bss */
  2189. if (cbss->pub.transmitted_bss)
  2190. cbss = container_of(cbss->pub.transmitted_bss,
  2191. struct cfg80211_internal_bss,
  2192. pub);
  2193. cbss->pub.channel = chan;
  2194. list_for_each_entry(bss, &rdev->bss_list, list) {
  2195. if (!cfg80211_bss_type_match(bss->pub.capability,
  2196. bss->pub.channel->band,
  2197. wdev->conn_bss_type))
  2198. continue;
  2199. if (bss == cbss)
  2200. continue;
  2201. if (!cmp_bss(&bss->pub, &cbss->pub, BSS_CMP_REGULAR)) {
  2202. new = bss;
  2203. break;
  2204. }
  2205. }
  2206. if (new) {
  2207. /* to save time, update IEs for transmitting bss only */
  2208. if (cfg80211_update_known_bss(rdev, cbss, new, false)) {
  2209. new->pub.proberesp_ies = NULL;
  2210. new->pub.beacon_ies = NULL;
  2211. }
  2212. list_for_each_entry_safe(nontrans_bss, tmp,
  2213. &new->pub.nontrans_list,
  2214. nontrans_list) {
  2215. bss = container_of(nontrans_bss,
  2216. struct cfg80211_internal_bss, pub);
  2217. if (__cfg80211_unlink_bss(rdev, bss))
  2218. rdev->bss_generation++;
  2219. }
  2220. WARN_ON(atomic_read(&new->hold));
  2221. if (!WARN_ON(!__cfg80211_unlink_bss(rdev, new)))
  2222. rdev->bss_generation++;
  2223. }
  2224. rb_erase(&cbss->rbn, &rdev->bss_tree);
  2225. rb_insert_bss(rdev, cbss);
  2226. rdev->bss_generation++;
  2227. list_for_each_entry_safe(nontrans_bss, tmp,
  2228. &cbss->pub.nontrans_list,
  2229. nontrans_list) {
  2230. bss = container_of(nontrans_bss,
  2231. struct cfg80211_internal_bss, pub);
  2232. bss->pub.channel = chan;
  2233. rb_erase(&bss->rbn, &rdev->bss_tree);
  2234. rb_insert_bss(rdev, bss);
  2235. rdev->bss_generation++;
  2236. }
  2237. done:
  2238. spin_unlock_bh(&rdev->bss_lock);
  2239. }
  2240. #ifdef CONFIG_CFG80211_WEXT
  2241. static struct cfg80211_registered_device *
  2242. cfg80211_get_dev_from_ifindex(struct net *net, int ifindex)
  2243. {
  2244. struct cfg80211_registered_device *rdev;
  2245. struct net_device *dev;
  2246. ASSERT_RTNL();
  2247. dev = dev_get_by_index(net, ifindex);
  2248. if (!dev)
  2249. return ERR_PTR(-ENODEV);
  2250. if (dev->ieee80211_ptr)
  2251. rdev = wiphy_to_rdev(dev->ieee80211_ptr->wiphy);
  2252. else
  2253. rdev = ERR_PTR(-ENODEV);
  2254. dev_put(dev);
  2255. return rdev;
  2256. }
  2257. int cfg80211_wext_siwscan(struct net_device *dev,
  2258. struct iw_request_info *info,
  2259. union iwreq_data *wrqu, char *extra)
  2260. {
  2261. struct cfg80211_registered_device *rdev;
  2262. struct wiphy *wiphy;
  2263. struct iw_scan_req *wreq = NULL;
  2264. struct cfg80211_scan_request *creq = NULL;
  2265. int i, err, n_channels = 0;
  2266. enum nl80211_band band;
  2267. if (!netif_running(dev))
  2268. return -ENETDOWN;
  2269. if (wrqu->data.length == sizeof(struct iw_scan_req))
  2270. wreq = (struct iw_scan_req *)extra;
  2271. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  2272. if (IS_ERR(rdev))
  2273. return PTR_ERR(rdev);
  2274. if (rdev->scan_req || rdev->scan_msg) {
  2275. err = -EBUSY;
  2276. goto out;
  2277. }
  2278. wiphy = &rdev->wiphy;
  2279. /* Determine number of channels, needed to allocate creq */
  2280. if (wreq && wreq->num_channels)
  2281. n_channels = wreq->num_channels;
  2282. else
  2283. n_channels = ieee80211_get_num_supported_channels(wiphy);
  2284. creq = kzalloc(sizeof(*creq) + sizeof(struct cfg80211_ssid) +
  2285. n_channels * sizeof(void *),
  2286. GFP_ATOMIC);
  2287. if (!creq) {
  2288. err = -ENOMEM;
  2289. goto out;
  2290. }
  2291. creq->wiphy = wiphy;
  2292. creq->wdev = dev->ieee80211_ptr;
  2293. /* SSIDs come after channels */
  2294. creq->ssids = (void *)&creq->channels[n_channels];
  2295. creq->n_channels = n_channels;
  2296. creq->n_ssids = 1;
  2297. creq->scan_start = jiffies;
  2298. /* translate "Scan on frequencies" request */
  2299. i = 0;
  2300. for (band = 0; band < NUM_NL80211_BANDS; band++) {
  2301. int j;
  2302. if (!wiphy->bands[band])
  2303. continue;
  2304. for (j = 0; j < wiphy->bands[band]->n_channels; j++) {
  2305. /* ignore disabled channels */
  2306. if (wiphy->bands[band]->channels[j].flags &
  2307. IEEE80211_CHAN_DISABLED)
  2308. continue;
  2309. /* If we have a wireless request structure and the
  2310. * wireless request specifies frequencies, then search
  2311. * for the matching hardware channel.
  2312. */
  2313. if (wreq && wreq->num_channels) {
  2314. int k;
  2315. int wiphy_freq = wiphy->bands[band]->channels[j].center_freq;
  2316. for (k = 0; k < wreq->num_channels; k++) {
  2317. struct iw_freq *freq =
  2318. &wreq->channel_list[k];
  2319. int wext_freq =
  2320. cfg80211_wext_freq(freq);
  2321. if (wext_freq == wiphy_freq)
  2322. goto wext_freq_found;
  2323. }
  2324. goto wext_freq_not_found;
  2325. }
  2326. wext_freq_found:
  2327. creq->channels[i] = &wiphy->bands[band]->channels[j];
  2328. i++;
  2329. wext_freq_not_found: ;
  2330. }
  2331. }
  2332. /* No channels found? */
  2333. if (!i) {
  2334. err = -EINVAL;
  2335. goto out;
  2336. }
  2337. /* Set real number of channels specified in creq->channels[] */
  2338. creq->n_channels = i;
  2339. /* translate "Scan for SSID" request */
  2340. if (wreq) {
  2341. if (wrqu->data.flags & IW_SCAN_THIS_ESSID) {
  2342. if (wreq->essid_len > IEEE80211_MAX_SSID_LEN) {
  2343. err = -EINVAL;
  2344. goto out;
  2345. }
  2346. memcpy(creq->ssids[0].ssid, wreq->essid, wreq->essid_len);
  2347. creq->ssids[0].ssid_len = wreq->essid_len;
  2348. }
  2349. if (wreq->scan_type == IW_SCAN_TYPE_PASSIVE)
  2350. creq->n_ssids = 0;
  2351. }
  2352. for (i = 0; i < NUM_NL80211_BANDS; i++)
  2353. if (wiphy->bands[i])
  2354. creq->rates[i] = (1 << wiphy->bands[i]->n_bitrates) - 1;
  2355. eth_broadcast_addr(creq->bssid);
  2356. rdev->scan_req = creq;
  2357. err = rdev_scan(rdev, creq);
  2358. if (err) {
  2359. rdev->scan_req = NULL;
  2360. /* creq will be freed below */
  2361. } else {
  2362. nl80211_send_scan_start(rdev, dev->ieee80211_ptr);
  2363. /* creq now owned by driver */
  2364. creq = NULL;
  2365. dev_hold(dev);
  2366. }
  2367. out:
  2368. kfree(creq);
  2369. return err;
  2370. }
  2371. EXPORT_WEXT_HANDLER(cfg80211_wext_siwscan);
  2372. static char *ieee80211_scan_add_ies(struct iw_request_info *info,
  2373. const struct cfg80211_bss_ies *ies,
  2374. char *current_ev, char *end_buf)
  2375. {
  2376. const u8 *pos, *end, *next;
  2377. struct iw_event iwe;
  2378. if (!ies)
  2379. return current_ev;
  2380. /*
  2381. * If needed, fragment the IEs buffer (at IE boundaries) into short
  2382. * enough fragments to fit into IW_GENERIC_IE_MAX octet messages.
  2383. */
  2384. pos = ies->data;
  2385. end = pos + ies->len;
  2386. while (end - pos > IW_GENERIC_IE_MAX) {
  2387. next = pos + 2 + pos[1];
  2388. while (next + 2 + next[1] - pos < IW_GENERIC_IE_MAX)
  2389. next = next + 2 + next[1];
  2390. memset(&iwe, 0, sizeof(iwe));
  2391. iwe.cmd = IWEVGENIE;
  2392. iwe.u.data.length = next - pos;
  2393. current_ev = iwe_stream_add_point_check(info, current_ev,
  2394. end_buf, &iwe,
  2395. (void *)pos);
  2396. if (IS_ERR(current_ev))
  2397. return current_ev;
  2398. pos = next;
  2399. }
  2400. if (end > pos) {
  2401. memset(&iwe, 0, sizeof(iwe));
  2402. iwe.cmd = IWEVGENIE;
  2403. iwe.u.data.length = end - pos;
  2404. current_ev = iwe_stream_add_point_check(info, current_ev,
  2405. end_buf, &iwe,
  2406. (void *)pos);
  2407. if (IS_ERR(current_ev))
  2408. return current_ev;
  2409. }
  2410. return current_ev;
  2411. }
  2412. static char *
  2413. ieee80211_bss(struct wiphy *wiphy, struct iw_request_info *info,
  2414. struct cfg80211_internal_bss *bss, char *current_ev,
  2415. char *end_buf)
  2416. {
  2417. const struct cfg80211_bss_ies *ies;
  2418. struct iw_event iwe;
  2419. const u8 *ie;
  2420. u8 buf[50];
  2421. u8 *cfg, *p, *tmp;
  2422. int rem, i, sig;
  2423. bool ismesh = false;
  2424. memset(&iwe, 0, sizeof(iwe));
  2425. iwe.cmd = SIOCGIWAP;
  2426. iwe.u.ap_addr.sa_family = ARPHRD_ETHER;
  2427. memcpy(iwe.u.ap_addr.sa_data, bss->pub.bssid, ETH_ALEN);
  2428. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  2429. IW_EV_ADDR_LEN);
  2430. if (IS_ERR(current_ev))
  2431. return current_ev;
  2432. memset(&iwe, 0, sizeof(iwe));
  2433. iwe.cmd = SIOCGIWFREQ;
  2434. iwe.u.freq.m = ieee80211_frequency_to_channel(bss->pub.channel->center_freq);
  2435. iwe.u.freq.e = 0;
  2436. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  2437. IW_EV_FREQ_LEN);
  2438. if (IS_ERR(current_ev))
  2439. return current_ev;
  2440. memset(&iwe, 0, sizeof(iwe));
  2441. iwe.cmd = SIOCGIWFREQ;
  2442. iwe.u.freq.m = bss->pub.channel->center_freq;
  2443. iwe.u.freq.e = 6;
  2444. current_ev = iwe_stream_add_event_check(info, current_ev, end_buf, &iwe,
  2445. IW_EV_FREQ_LEN);
  2446. if (IS_ERR(current_ev))
  2447. return current_ev;
  2448. if (wiphy->signal_type != CFG80211_SIGNAL_TYPE_NONE) {
  2449. memset(&iwe, 0, sizeof(iwe));
  2450. iwe.cmd = IWEVQUAL;
  2451. iwe.u.qual.updated = IW_QUAL_LEVEL_UPDATED |
  2452. IW_QUAL_NOISE_INVALID |
  2453. IW_QUAL_QUAL_UPDATED;
  2454. switch (wiphy->signal_type) {
  2455. case CFG80211_SIGNAL_TYPE_MBM:
  2456. sig = bss->pub.signal / 100;
  2457. iwe.u.qual.level = sig;
  2458. iwe.u.qual.updated |= IW_QUAL_DBM;
  2459. if (sig < -110) /* rather bad */
  2460. sig = -110;
  2461. else if (sig > -40) /* perfect */
  2462. sig = -40;
  2463. /* will give a range of 0 .. 70 */
  2464. iwe.u.qual.qual = sig + 110;
  2465. break;
  2466. case CFG80211_SIGNAL_TYPE_UNSPEC:
  2467. iwe.u.qual.level = bss->pub.signal;
  2468. /* will give range 0 .. 100 */
  2469. iwe.u.qual.qual = bss->pub.signal;
  2470. break;
  2471. default:
  2472. /* not reached */
  2473. break;
  2474. }
  2475. current_ev = iwe_stream_add_event_check(info, current_ev,
  2476. end_buf, &iwe,
  2477. IW_EV_QUAL_LEN);
  2478. if (IS_ERR(current_ev))
  2479. return current_ev;
  2480. }
  2481. memset(&iwe, 0, sizeof(iwe));
  2482. iwe.cmd = SIOCGIWENCODE;
  2483. if (bss->pub.capability & WLAN_CAPABILITY_PRIVACY)
  2484. iwe.u.data.flags = IW_ENCODE_ENABLED | IW_ENCODE_NOKEY;
  2485. else
  2486. iwe.u.data.flags = IW_ENCODE_DISABLED;
  2487. iwe.u.data.length = 0;
  2488. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  2489. &iwe, "");
  2490. if (IS_ERR(current_ev))
  2491. return current_ev;
  2492. rcu_read_lock();
  2493. ies = rcu_dereference(bss->pub.ies);
  2494. rem = ies->len;
  2495. ie = ies->data;
  2496. while (rem >= 2) {
  2497. /* invalid data */
  2498. if (ie[1] > rem - 2)
  2499. break;
  2500. switch (ie[0]) {
  2501. case WLAN_EID_SSID:
  2502. memset(&iwe, 0, sizeof(iwe));
  2503. iwe.cmd = SIOCGIWESSID;
  2504. iwe.u.data.length = ie[1];
  2505. iwe.u.data.flags = 1;
  2506. current_ev = iwe_stream_add_point_check(info,
  2507. current_ev,
  2508. end_buf, &iwe,
  2509. (u8 *)ie + 2);
  2510. if (IS_ERR(current_ev))
  2511. goto unlock;
  2512. break;
  2513. case WLAN_EID_MESH_ID:
  2514. memset(&iwe, 0, sizeof(iwe));
  2515. iwe.cmd = SIOCGIWESSID;
  2516. iwe.u.data.length = ie[1];
  2517. iwe.u.data.flags = 1;
  2518. current_ev = iwe_stream_add_point_check(info,
  2519. current_ev,
  2520. end_buf, &iwe,
  2521. (u8 *)ie + 2);
  2522. if (IS_ERR(current_ev))
  2523. goto unlock;
  2524. break;
  2525. case WLAN_EID_MESH_CONFIG:
  2526. ismesh = true;
  2527. if (ie[1] != sizeof(struct ieee80211_meshconf_ie))
  2528. break;
  2529. cfg = (u8 *)ie + 2;
  2530. memset(&iwe, 0, sizeof(iwe));
  2531. iwe.cmd = IWEVCUSTOM;
  2532. sprintf(buf, "Mesh Network Path Selection Protocol ID: "
  2533. "0x%02X", cfg[0]);
  2534. iwe.u.data.length = strlen(buf);
  2535. current_ev = iwe_stream_add_point_check(info,
  2536. current_ev,
  2537. end_buf,
  2538. &iwe, buf);
  2539. if (IS_ERR(current_ev))
  2540. goto unlock;
  2541. sprintf(buf, "Path Selection Metric ID: 0x%02X",
  2542. cfg[1]);
  2543. iwe.u.data.length = strlen(buf);
  2544. current_ev = iwe_stream_add_point_check(info,
  2545. current_ev,
  2546. end_buf,
  2547. &iwe, buf);
  2548. if (IS_ERR(current_ev))
  2549. goto unlock;
  2550. sprintf(buf, "Congestion Control Mode ID: 0x%02X",
  2551. cfg[2]);
  2552. iwe.u.data.length = strlen(buf);
  2553. current_ev = iwe_stream_add_point_check(info,
  2554. current_ev,
  2555. end_buf,
  2556. &iwe, buf);
  2557. if (IS_ERR(current_ev))
  2558. goto unlock;
  2559. sprintf(buf, "Synchronization ID: 0x%02X", cfg[3]);
  2560. iwe.u.data.length = strlen(buf);
  2561. current_ev = iwe_stream_add_point_check(info,
  2562. current_ev,
  2563. end_buf,
  2564. &iwe, buf);
  2565. if (IS_ERR(current_ev))
  2566. goto unlock;
  2567. sprintf(buf, "Authentication ID: 0x%02X", cfg[4]);
  2568. iwe.u.data.length = strlen(buf);
  2569. current_ev = iwe_stream_add_point_check(info,
  2570. current_ev,
  2571. end_buf,
  2572. &iwe, buf);
  2573. if (IS_ERR(current_ev))
  2574. goto unlock;
  2575. sprintf(buf, "Formation Info: 0x%02X", cfg[5]);
  2576. iwe.u.data.length = strlen(buf);
  2577. current_ev = iwe_stream_add_point_check(info,
  2578. current_ev,
  2579. end_buf,
  2580. &iwe, buf);
  2581. if (IS_ERR(current_ev))
  2582. goto unlock;
  2583. sprintf(buf, "Capabilities: 0x%02X", cfg[6]);
  2584. iwe.u.data.length = strlen(buf);
  2585. current_ev = iwe_stream_add_point_check(info,
  2586. current_ev,
  2587. end_buf,
  2588. &iwe, buf);
  2589. if (IS_ERR(current_ev))
  2590. goto unlock;
  2591. break;
  2592. case WLAN_EID_SUPP_RATES:
  2593. case WLAN_EID_EXT_SUPP_RATES:
  2594. /* display all supported rates in readable format */
  2595. p = current_ev + iwe_stream_lcp_len(info);
  2596. memset(&iwe, 0, sizeof(iwe));
  2597. iwe.cmd = SIOCGIWRATE;
  2598. /* Those two flags are ignored... */
  2599. iwe.u.bitrate.fixed = iwe.u.bitrate.disabled = 0;
  2600. for (i = 0; i < ie[1]; i++) {
  2601. iwe.u.bitrate.value =
  2602. ((ie[i + 2] & 0x7f) * 500000);
  2603. tmp = p;
  2604. p = iwe_stream_add_value(info, current_ev, p,
  2605. end_buf, &iwe,
  2606. IW_EV_PARAM_LEN);
  2607. if (p == tmp) {
  2608. current_ev = ERR_PTR(-E2BIG);
  2609. goto unlock;
  2610. }
  2611. }
  2612. current_ev = p;
  2613. break;
  2614. }
  2615. rem -= ie[1] + 2;
  2616. ie += ie[1] + 2;
  2617. }
  2618. if (bss->pub.capability & (WLAN_CAPABILITY_ESS | WLAN_CAPABILITY_IBSS) ||
  2619. ismesh) {
  2620. memset(&iwe, 0, sizeof(iwe));
  2621. iwe.cmd = SIOCGIWMODE;
  2622. if (ismesh)
  2623. iwe.u.mode = IW_MODE_MESH;
  2624. else if (bss->pub.capability & WLAN_CAPABILITY_ESS)
  2625. iwe.u.mode = IW_MODE_MASTER;
  2626. else
  2627. iwe.u.mode = IW_MODE_ADHOC;
  2628. current_ev = iwe_stream_add_event_check(info, current_ev,
  2629. end_buf, &iwe,
  2630. IW_EV_UINT_LEN);
  2631. if (IS_ERR(current_ev))
  2632. goto unlock;
  2633. }
  2634. memset(&iwe, 0, sizeof(iwe));
  2635. iwe.cmd = IWEVCUSTOM;
  2636. sprintf(buf, "tsf=%016llx", (unsigned long long)(ies->tsf));
  2637. iwe.u.data.length = strlen(buf);
  2638. current_ev = iwe_stream_add_point_check(info, current_ev, end_buf,
  2639. &iwe, buf);
  2640. if (IS_ERR(current_ev))
  2641. goto unlock;
  2642. memset(&iwe, 0, sizeof(iwe));
  2643. iwe.cmd = IWEVCUSTOM;
  2644. sprintf(buf, " Last beacon: %ums ago",
  2645. elapsed_jiffies_msecs(bss->ts));
  2646. iwe.u.data.length = strlen(buf);
  2647. current_ev = iwe_stream_add_point_check(info, current_ev,
  2648. end_buf, &iwe, buf);
  2649. if (IS_ERR(current_ev))
  2650. goto unlock;
  2651. current_ev = ieee80211_scan_add_ies(info, ies, current_ev, end_buf);
  2652. unlock:
  2653. rcu_read_unlock();
  2654. return current_ev;
  2655. }
  2656. static int ieee80211_scan_results(struct cfg80211_registered_device *rdev,
  2657. struct iw_request_info *info,
  2658. char *buf, size_t len)
  2659. {
  2660. char *current_ev = buf;
  2661. char *end_buf = buf + len;
  2662. struct cfg80211_internal_bss *bss;
  2663. int err = 0;
  2664. spin_lock_bh(&rdev->bss_lock);
  2665. cfg80211_bss_expire(rdev);
  2666. list_for_each_entry(bss, &rdev->bss_list, list) {
  2667. if (buf + len - current_ev <= IW_EV_ADDR_LEN) {
  2668. err = -E2BIG;
  2669. break;
  2670. }
  2671. current_ev = ieee80211_bss(&rdev->wiphy, info, bss,
  2672. current_ev, end_buf);
  2673. if (IS_ERR(current_ev)) {
  2674. err = PTR_ERR(current_ev);
  2675. break;
  2676. }
  2677. }
  2678. spin_unlock_bh(&rdev->bss_lock);
  2679. if (err)
  2680. return err;
  2681. return current_ev - buf;
  2682. }
  2683. int cfg80211_wext_giwscan(struct net_device *dev,
  2684. struct iw_request_info *info,
  2685. struct iw_point *data, char *extra)
  2686. {
  2687. struct cfg80211_registered_device *rdev;
  2688. int res;
  2689. if (!netif_running(dev))
  2690. return -ENETDOWN;
  2691. rdev = cfg80211_get_dev_from_ifindex(dev_net(dev), dev->ifindex);
  2692. if (IS_ERR(rdev))
  2693. return PTR_ERR(rdev);
  2694. if (rdev->scan_req || rdev->scan_msg)
  2695. return -EAGAIN;
  2696. res = ieee80211_scan_results(rdev, info, extra, data->length);
  2697. data->length = 0;
  2698. if (res >= 0) {
  2699. data->length = res;
  2700. res = 0;
  2701. }
  2702. return res;
  2703. }
  2704. EXPORT_WEXT_HANDLER(cfg80211_wext_giwscan);
  2705. #endif