scan.c 83 KB

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